3 # This file defines the default bindings for Tk 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-1997 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 # Elements of tk::Priv that are used in this file:
16 # afterId - If non-null, it means that auto-scanning is underway
17 # and it gives the "after" id for the next auto-scan
18 # command to be executed.
19 # mouseMoved - Non-zero means the mouse has moved a significant
20 # amount since the button went down (so, for example,
21 # start dragging out a selection).
22 # pressX - X-coordinate at which the mouse button was pressed.
23 # selectMode - The style of selection currently underway:
24 # char, word, or line.
25 # x, y - Last known mouse coordinates for scanning
27 # data - Used for Cut and Copy
28 #-------------------------------------------------------------------------
30 #-------------------------------------------------------------------------
31 # The code below creates the default class bindings for entries.
32 #-------------------------------------------------------------------------
34 if {![catch {tk::EntryGetSelection %W} tk::Priv(data)]} {
35 clipboard clear -displayof %W
36 clipboard append -displayof %W $tk::Priv(data)
37 %W delete sel.first sel.last
42 if {![catch {tk::EntryGetSelection %W} tk::Priv(data)]} {
43 clipboard clear -displayof %W
44 clipboard append -displayof %W $tk::Priv(data)
48 bind Entry <<Paste>> {
51 if {[tk windowingsystem] ne "x11"} {
53 %W delete sel.first sel.last
56 %W insert insert [::tk::GetSelection %W CLIPBOARD]
60 bind Entry <<Clear>> {
61 # ignore if there is no selection
62 catch { %W delete sel.first sel.last }
64 bind Entry <<PasteSelection>> {
65 if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
66 || !$tk::Priv(mouseMoved)} {
71 bind Entry <<TraverseIn>> {
72 %W selection range 0 end
76 # Standard Motif bindings:
79 tk::EntryButton1 %W %x
82 bind Entry <B1-Motion> {
84 tk::EntryMouseSelect %W %x
86 bind Entry <Double-1> {
87 set tk::Priv(selectMode) word
88 tk::EntryMouseSelect %W %x
89 catch {%W icursor sel.last}
91 bind Entry <Triple-1> {
92 set tk::Priv(selectMode) line
93 tk::EntryMouseSelect %W %x
94 catch {%W icursor sel.last}
96 bind Entry <Shift-1> {
97 set tk::Priv(selectMode) char
98 %W selection adjust @%x
100 bind Entry <Double-Shift-1> {
101 set tk::Priv(selectMode) word
102 tk::EntryMouseSelect %W %x
104 bind Entry <Triple-Shift-1> {
105 set tk::Priv(selectMode) line
106 tk::EntryMouseSelect %W %x
108 bind Entry <B1-Leave> {
112 bind Entry <B1-Enter> {
115 bind Entry <ButtonRelease-1> {
118 bind Entry <Control-1> {
122 bind Entry <<PrevChar>> {
123 tk::EntrySetCursor %W [expr {[%W index insert] - 1}]
125 bind Entry <<NextChar>> {
126 tk::EntrySetCursor %W [expr {[%W index insert] + 1}]
128 bind Entry <<SelectPrevChar>> {
129 tk::EntryKeySelect %W [expr {[%W index insert] - 1}]
130 tk::EntrySeeInsert %W
132 bind Entry <<SelectNextChar>> {
133 tk::EntryKeySelect %W [expr {[%W index insert] + 1}]
134 tk::EntrySeeInsert %W
136 bind Entry <<PrevWord>> {
137 tk::EntrySetCursor %W [tk::EntryPreviousWord %W insert]
139 bind Entry <<NextWord>> {
140 tk::EntrySetCursor %W [tk::EntryNextWord %W insert]
142 bind Entry <<SelectPrevWord>> {
143 tk::EntryKeySelect %W [tk::EntryPreviousWord %W insert]
144 tk::EntrySeeInsert %W
146 bind Entry <<SelectNextWord>> {
147 tk::EntryKeySelect %W [tk::EntryNextWord %W insert]
148 tk::EntrySeeInsert %W
150 bind Entry <<LineStart>> {
151 tk::EntrySetCursor %W 0
153 bind Entry <<SelectLineStart>> {
154 tk::EntryKeySelect %W 0
155 tk::EntrySeeInsert %W
157 bind Entry <<LineEnd>> {
158 tk::EntrySetCursor %W end
160 bind Entry <<SelectLineEnd>> {
161 tk::EntryKeySelect %W end
162 tk::EntrySeeInsert %W
165 bind Entry <Delete> {
166 if {[%W selection present]} {
167 %W delete sel.first sel.last
172 bind Entry <BackSpace> {
173 tk::EntryBackspace %W
176 bind Entry <Control-space> {
177 %W selection from insert
179 bind Entry <Select> {
180 %W selection from insert
182 bind Entry <Control-Shift-space> {
183 %W selection adjust insert
185 bind Entry <Shift-Select> {
186 %W selection adjust insert
188 bind Entry <<SelectAll>> {
189 %W selection range 0 end
191 bind Entry <<SelectNone>> {
194 bind Entry <KeyPress> {
196 tk::EntryInsert %W %A
199 # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
200 # Otherwise, if a widget binding for one of these is defined, the
201 # <KeyPress> class binding will also fire and insert the character,
202 # which is wrong. Ditto for Escape, Return, and Tab.
204 bind Entry <Alt-KeyPress> {# nothing}
205 bind Entry <Meta-KeyPress> {# nothing}
206 bind Entry <Control-KeyPress> {# nothing}
207 bind Entry <Escape> {# nothing}
208 bind Entry <Return> {# nothing}
209 bind Entry <KP_Enter> {# nothing}
210 bind Entry <Tab> {# nothing}
211 bind Entry <Prior> {# nothing}
212 bind Entry <Next> {# nothing}
213 if {[tk windowingsystem] eq "aqua"} {
214 bind Entry <Command-KeyPress> {# nothing}
216 # Tk-on-Cocoa generates characters for these two keys. [Bug 2971663]
217 bind Entry <<NextLine>> {# nothing}
218 bind Entry <<PrevLine>> {# nothing}
220 # On Windows, paste is done using Shift-Insert. Shift-Insert already
221 # generates the <<Paste>> event, so we don't need to do anything here.
222 if {[tk windowingsystem] ne "win32"} {
223 bind Entry <Insert> {
224 catch {tk::EntryInsert %W [::tk::GetSelection %W PRIMARY]}
228 # Additional emacs-like bindings:
230 bind Entry <Control-d> {
231 if {!$tk_strictMotif} {
235 bind Entry <Control-h> {
236 if {!$tk_strictMotif} {
237 tk::EntryBackspace %W
240 bind Entry <Control-k> {
241 if {!$tk_strictMotif} {
245 bind Entry <Control-t> {
246 if {!$tk_strictMotif} {
247 tk::EntryTranspose %W
250 bind Entry <Meta-b> {
251 if {!$tk_strictMotif} {
252 tk::EntrySetCursor %W [tk::EntryPreviousWord %W insert]
255 bind Entry <Meta-d> {
256 if {!$tk_strictMotif} {
257 %W delete insert [tk::EntryNextWord %W insert]
260 bind Entry <Meta-f> {
261 if {!$tk_strictMotif} {
262 tk::EntrySetCursor %W [tk::EntryNextWord %W insert]
265 bind Entry <Meta-BackSpace> {
266 if {!$tk_strictMotif} {
267 %W delete [tk::EntryPreviousWord %W insert] insert
270 bind Entry <Meta-Delete> {
271 if {!$tk_strictMotif} {
272 %W delete [tk::EntryPreviousWord %W insert] insert
276 # A few additional bindings of my own.
279 if {!$tk_strictMotif} {
280 ::tk::EntryScanMark %W %x
283 bind Entry <B2-Motion> {
284 if {!$tk_strictMotif} {
285 ::tk::EntryScanDrag %W %x
289 # ::tk::EntryClosestGap --
290 # Given x and y coordinates, this procedure finds the closest boundary
291 # between characters to the given coordinates and returns the index
292 # of the character just after the boundary.
295 # w - The entry window.
296 # x - X-coordinate within the window.
298 proc ::tk::EntryClosestGap {w x} {
299 set pos [$w index @$x]
300 set bbox [$w bbox $pos]
301 if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
307 # ::tk::EntryButton1 --
308 # This procedure is invoked to handle button-1 presses in entry
309 # widgets. It moves the insertion cursor, sets the selection anchor,
310 # and claims the input focus.
313 # w - The entry window in which the button was pressed.
314 # x - The x-coordinate of the button press.
316 proc ::tk::EntryButton1 {w x} {
319 set Priv(selectMode) char
320 set Priv(mouseMoved) 0
322 $w icursor [EntryClosestGap $w $x]
323 $w selection from insert
324 if {"disabled" ne [$w cget -state]} {
329 # ::tk::EntryMouseSelect --
330 # This procedure is invoked when dragging out a selection with
331 # the mouse. Depending on the selection mode (character, word,
332 # line) it selects in different-sized units. This procedure
333 # ignores mouse motions initially until the mouse has moved from
334 # one character to another or until there have been multiple clicks.
337 # w - The entry window in which the button was pressed.
338 # x - The x-coordinate of the mouse.
340 proc ::tk::EntryMouseSelect {w x} {
343 set cur [EntryClosestGap $w $x]
344 set anchor [$w index anchor]
345 if {($cur != $anchor) || (abs($Priv(pressX) - $x) >= 3)} {
346 set Priv(mouseMoved) 1
348 switch $Priv(selectMode) {
350 if {$Priv(mouseMoved)} {
351 if {$cur < $anchor} {
352 $w selection range $cur $anchor
353 } elseif {$cur > $anchor} {
354 $w selection range $anchor $cur
361 if {$cur < [$w index anchor]} {
362 set before [tcl_wordBreakBefore [$w get] $cur]
363 set after [tcl_wordBreakAfter [$w get] [expr {$anchor-1}]]
365 set before [tcl_wordBreakBefore [$w get] $anchor]
366 set after [tcl_wordBreakAfter [$w get] [expr {$cur - 1}]]
374 $w selection range $before $after
377 $w selection range 0 end
380 if {$Priv(mouseMoved)} {
386 # ::tk::EntryPaste --
387 # This procedure sets the insertion cursor to the current mouse position,
388 # pastes the selection there, and sets the focus to the window.
391 # w - The entry window.
392 # x - X position of the mouse.
394 proc ::tk::EntryPaste {w x} {
395 $w icursor [EntryClosestGap $w $x]
396 catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
397 if {"disabled" ne [$w cget -state]} {
402 # ::tk::EntryAutoScan --
403 # This procedure is invoked when the mouse leaves an entry window
404 # with button 1 down. It scrolls the window left or right,
405 # depending on where the mouse is, and reschedules itself as an
406 # "after" command so that the window continues to scroll until the
407 # mouse moves back into the window or the mouse button is released.
410 # w - The entry window.
412 proc ::tk::EntryAutoScan {w} {
415 if {![winfo exists $w]} {
418 if {$x >= [winfo width $w]} {
419 $w xview scroll 2 units
420 EntryMouseSelect $w $x
422 $w xview scroll -2 units
423 EntryMouseSelect $w $x
425 set Priv(afterId) [after 50 [list tk::EntryAutoScan $w]]
428 # ::tk::EntryKeySelect --
429 # This procedure is invoked when stroking out selections using the
430 # keyboard. It moves the cursor to a new position, then extends
431 # the selection to that position.
434 # w - The entry window.
435 # new - A new position for the insertion cursor (the cursor hasn't
436 # actually been moved to this position yet).
438 proc ::tk::EntryKeySelect {w new} {
439 if {![$w selection present]} {
440 $w selection from insert
443 $w selection adjust $new
448 # ::tk::EntryInsert --
449 # Insert a string into an entry at the point of the insertion cursor.
450 # If there is a selection in the entry, and it covers the point of the
451 # insertion cursor, then delete the selection before inserting.
454 # w - The entry window in which to insert the string
455 # s - The string to insert (usually just a single character)
457 proc ::tk::EntryInsert {w s} {
462 set insert [$w index insert]
463 if {([$w index sel.first] <= $insert)
464 && ([$w index sel.last] >= $insert)} {
465 $w delete sel.first sel.last
472 # ::tk::EntryBackspace --
473 # Backspace over the character just before the insertion cursor.
474 # If backspacing would move the cursor off the left edge of the
475 # window, reposition the cursor at about the middle of the window.
478 # w - The entry window in which to backspace.
480 proc ::tk::EntryBackspace w {
481 if {[$w selection present]} {
482 $w delete sel.first sel.last
484 set x [expr {[$w index insert] - 1}]
488 if {[$w index @0] >= [$w index insert]} {
490 set left [lindex $range 0]
491 set right [lindex $range 1]
492 $w xview moveto [expr {$left - ($right - $left)/2.0}]
497 # ::tk::EntrySeeInsert --
498 # Make sure that the insertion cursor is visible in the entry window.
499 # If not, adjust the view so that it is.
502 # w - The entry window.
504 proc ::tk::EntrySeeInsert w {
505 set c [$w index insert]
506 if {($c < [$w index @0]) || ($c > [$w index @[winfo width $w]])} {
511 # ::tk::EntrySetCursor -
512 # Move the insertion cursor to a given position in an entry. Also
513 # clears the selection, if there is one in the entry, and makes sure
514 # that the insertion cursor is visible.
517 # w - The entry window.
518 # pos - The desired new position for the cursor in the window.
520 proc ::tk::EntrySetCursor {w pos} {
526 # ::tk::EntryTranspose -
527 # This procedure implements the "transpose" function for entry widgets.
528 # It tranposes the characters on either side of the insertion cursor,
529 # unless the cursor is at the end of the line. In this case it
530 # transposes the two characters to the left of the cursor. In either
531 # case, the cursor ends up to the right of the transposed characters.
534 # w - The entry window.
536 proc ::tk::EntryTranspose w {
537 set i [$w index insert]
538 if {$i < [$w index end]} {
541 set first [expr {$i-2}]
546 set new [string index $data [expr {$i-1}]][string index $data $first]
548 $w insert insert $new
552 # ::tk::EntryNextWord --
553 # Returns the index of the next word position after a given position in the
554 # entry. The next word is platform dependent and may be either the next
555 # end-of-word position or the next start-of-word position after the next
556 # end-of-word position.
559 # w - The entry window in which the cursor is to move.
560 # start - Position at which to start search.
562 if {[tk windowingsystem] eq "win32"} {
563 proc ::tk::EntryNextWord {w start} {
564 set pos [tcl_endOfWord [$w get] [$w index $start]]
566 set pos [tcl_startOfNextWord [$w get] $pos]
574 proc ::tk::EntryNextWord {w start} {
575 set pos [tcl_endOfWord [$w get] [$w index $start]]
583 # ::tk::EntryPreviousWord --
585 # Returns the index of the previous word position before a given
586 # position in the entry.
589 # w - The entry window in which the cursor is to move.
590 # start - Position at which to start search.
592 proc ::tk::EntryPreviousWord {w start} {
593 set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
600 # ::tk::EntryScanMark --
602 # Marks the start of a possible scan drag operation
605 # w - The entry window from which the text to get
606 # x - x location on screen
608 proc ::tk::EntryScanMark {w x} {
611 set ::tk::Priv(y) 0 ; # not used
612 set ::tk::Priv(mouseMoved) 0
615 # ::tk::EntryScanDrag --
617 # Marks the start of a possible scan drag operation
620 # w - The entry window from which the text to get
621 # x - x location on screen
623 proc ::tk::EntryScanDrag {w x} {
624 # Make sure these exist, as some weird situations can trigger the
625 # motion binding without the initial press. [Bug #220269]
626 if {![info exists ::tk::Priv(x)]} { set ::tk::Priv(x) $x }
628 if {abs($x-$::tk::Priv(x)) > 2} {
629 set ::tk::Priv(mouseMoved) 1
634 # ::tk::EntryGetSelection --
636 # Returns the selected text of the entry with respect to the -show option.
639 # w - The entry window from which the text to get
641 proc ::tk::EntryGetSelection {w} {
642 set entryString [string range [$w get] [$w index sel.first] \
643 [expr {[$w index sel.last] - 1}]]
644 if {[$w cget -show] ne ""} {
645 return [string repeat [string index [$w cget -show] 0] \
646 [string length $entryString]]