Version 5 of Move cursor by display line in a text widget

Updated 2002-12-19 00:04:57

Christian Heide Damm responded in comp.lang.tcl [L1 ] on how to move the cursor up and down visible lines instead of real text widget lines:

...namely binding up/down to something like this:

     # Find the coordinates of the cursor and set the new height
     # manually. Note: errors rounding off, since
     # coordinates don't match character positions exactly.
     lset {lines char} [split [$textWidget index insert] .]
     lset {x y textWidth textHeight} [$textWidget bbox [$textWidget
     index insert]]
     lset {_ maxy _ _} [$textWidget bbox "end - 1 char"]
     # When updating position, make sure y is within text boundaries
     switch -- $upOrDown {
       "up" {
          set y [max [expr $y-$textHeight] 0]
       }
       "down" {
          set y [min [expr $y+$textHeight] $maxy]
       }
     }
     lset {newx newy width _} [$textWidget bbox [$textWidget index
     @$x,$y]]
     # Test on which side of the character
     # we should position the cursor
     if {$x>[expr $newx+$width/2]} {
       set x [expr $newx+$width+1]
     }
     set newIndex [$textWidget index @$x,$y]

Here is the above code in a function, with definitions for missing functions min/max and with missing function lset replaced with calls to scan. The code has also been modified to correctly handle more than a screenful of text. This code still lacks the feature found in the default tkTextUpDownLine where the original column is maintained across repeated operations even though some lines passed through don't have enough columns -- Brian Theado:

 proc min args {lindex [lsort -real $args] 0}
 proc max args {lindex [lsort -real $args] end}
 proc moveUpDown {textWidget upOrDown} {
     # Make the insertion cursor visible so bbox doesn't return empty list
     $textWidget see insert

     # Find the coordinates of the cursor and set the new height
     # manually. Note: errors rounding off, since
     # coordinates don't match character positions exactly.
     scan [$textWidget index insert] {%d.%d} lines char
     scan [$textWidget bbox [$textWidget index insert]] {%d %d %d %d} x y textWidth textHeight
     scan [$textWidget bbox @[winfo width $textWidget],[winfo height $textWidget]] {%*d %d %*d %*d} maxy
     # When updating position, make sure y is within text boundaries
     switch -- $upOrDown {
       "up" {
           if {$y <= $textHeight} {
               $textWidget yview scroll -1 units
           } else {
               set y [max [expr $y-$textHeight] 0]
           }
       }
       "down" {
           if {$y >= $maxy} {
                $textWidget yview scroll 1 units
           } else {
                set y [min [expr $y+$textHeight] $maxy]
           }
       }
     }
     scan [$textWidget bbox [$textWidget index @$x,$y]] {%d %d %d %*d} newx newy width

     # Test on which side of the character
     # we should position the cursor
     if {$x>[expr $newx+$width/2]} {
       set x [expr $newx+$width+1]
     }
     return [$textWidget index @$x,$y]
 }

 # Replace the default Text widget bindings to try it out
 bind Text <Up> {
    tkTextSetCursor %W [moveUpDown %W up]
 }
 bind Text <Down> {
    tkTextSetCursor %W [moveUpDown %W down]
 }

 # Selection via the keyboard should be re-bound for consistency (added 12/18/02)
 bind Text <Shift-Up> {
    tkTextKeySelect %W [moveUpDown %W up]
 }
 bind Text <Shift-Down> {
    tkTextKeySelect %W [moveUpDown %W down]
 }

Category GUI