3 # This file defines the default bindings for entry widgets and provides
4 # procedures that help in implementing those bindings.
6 # Copyright (c) 1992-1994 The Regents of the University of California.
7 # Copyright (c) 1994-1995 Sun Microsystems, Inc.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 #-------------------------------------------------------------------------
14 # The code below creates the default class bindings for entries.
15 #-------------------------------------------------------------------------
18 ckEntrySetCursor %W [expr [%W index insert] - 1]
21 ckEntrySetCursor %W [expr [%W index insert] + 1]
27 ckEntrySetCursor %W end
30 if [%W selection present] {
31 %W delete sel.first sel.last
36 bind Entry <ASCIIDelete> {
37 if [%W selection present] {
38 %W delete sel.first sel.last
43 bind Entry <BackSpace> {
47 %W selection from insert
49 bind Entry <KeyPress> {
52 bind Entry <Control> {# nothing}
53 bind Entry <Escape> {# nothing}
54 bind Entry <Return> {# nothing}
55 bind Entry <Linefeed> {# nothing}
56 bind Entry <Tab> {# nothing}
57 bind Entry <BackTab> {# nothing}
59 bind Entry <Button-1> {
60 if [ckFocusOK %W] {%W icursor @%x ; focus %W}
63 # Additional emacs-like bindings:
65 bind Entry <Control-a> {
68 bind Entry <Control-b> {
69 ckEntrySetCursor %W [expr [%W index insert] - 1]
71 bind Entry <Control-d> {
74 bind Entry <Control-e> {
75 ckEntrySetCursor %W end
77 bind Entry <Control-f> {
78 ckEntrySetCursor %W [expr [%W index insert] + 1]
80 bind Entry <Control-h> {
83 bind Entry <Control-k> {
86 bind Entry <Control-t> {
91 # This procedure is invoked when stroking out selections using the
92 # keyboard. It moves the cursor to a new position, then extends
93 # the selection to that position.
96 # w - The entry window.
97 # new - A new position for the insertion cursor (the cursor hasn't
98 # actually been moved to this position yet).
100 proc ckEntryKeySelect {w new} {
101 if ![$w selection present] {
102 $w selection from insert
105 $w selection adjust $new
111 # Insert a string into an entry at the point of the insertion cursor.
112 # If there is a selection in the entry, and it covers the point of the
113 # insertion cursor, then delete the selection before inserting.
116 # w - The entry window in which to insert the string
117 # s - The string to insert (usually just a single character)
119 proc ckEntryInsert {w s} {
122 set insert [$w index insert]
123 if {([$w index sel.first] <= $insert)
124 && ([$w index sel.last] >= $insert)} {
125 $w delete sel.first sel.last
132 # ckEntryBackspace --
133 # Backspace over the character just before the insertion cursor.
134 # If backspacing would move the cursor off the left edge of the
135 # window, reposition the cursor at about the middle of the window.
138 # w - The entry window in which to backspace.
140 proc ckEntryBackspace w {
141 if [$w selection present] {
142 $w delete sel.first sel.last
144 set x [expr {[$w index insert] - 1}]
145 if {$x >= 0} {$w delete $x}
146 if {[$w index @0] >= [$w index insert]} {
148 set left [lindex $range 0]
149 set right [lindex $range 1]
150 $w xview moveto [expr $left - ($right - $left)/2.0]
155 # ckEntrySeeInsert --
156 # Make sure that the insertion cursor is visible in the entry window.
157 # If not, adjust the view so that it is.
160 # w - The entry window.
162 proc ckEntrySeeInsert w {
163 set c [$w index insert]
164 set left [$w index @0]
169 set x [winfo width $w]
170 while {([$w index @$x] <= $c) && ($left < $c)} {
177 # Move the insertion cursor to a given position in an entry. Also
178 # clears the selection, if there is one in the entry, and makes sure
179 # that the insertion cursor is visible.
182 # w - The entry window.
183 # pos - The desired new position for the cursor in the window.
185 proc ckEntrySetCursor {w pos} {
192 # This procedure implements the "transpose" function for entry widgets.
193 # It tranposes the characters on either side of the insertion cursor,
194 # unless the cursor is at the end of the line. In this case it
195 # transposes the two characters to the left of the cursor. In either
196 # case, the cursor ends up to the right of the transposed characters.
199 # w - The entry window.
201 proc ckEntryTranspose w {
202 set i [$w index insert]
203 if {$i < [$w index end]} {
206 set first [expr $i-2]
210 set new [string index [$w get] [expr $i-1]][string index [$w get] $first]
212 $w insert insert $new