3 # This file defines the default bindings for Tk entry widgets and provides
4 # procedures that help in implementing those bindings.
6 # RCS: @(#) $Id: entry.tcl,v 1.26 2007/12/13 15:26:27 dgp Exp $
8 # Copyright (c) 1992-1994 The Regents of the University of California.
9 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 #-------------------------------------------------------------------------
16 # Elements of tk::Priv that are used in this file:
18 # afterId - If non-null, it means that auto-scanning is underway
19 # and it gives the "after" id for the next auto-scan
20 # command to be executed.
21 # mouseMoved - Non-zero means the mouse has moved a significant
22 # amount since the button went down (so, for example,
23 # start dragging out a selection).
24 # pressX - X-coordinate at which the mouse button was pressed.
25 # selectMode - The style of selection currently underway:
26 # char, word, or line.
27 # x, y - Last known mouse coordinates for scanning
29 # data - Used for Cut and Copy
30 #-------------------------------------------------------------------------
32 #-------------------------------------------------------------------------
33 # The code below creates the default class bindings for entries.
34 #-------------------------------------------------------------------------
36 if {![catch {tk::EntryGetSelection %W
} tk::Priv(data
)]} {
37 clipboard clear
-displayof %W
38 clipboard append -displayof %W
$tk::Priv(data
)
39 %W delete sel.first sel.last
44 if {![catch {tk::EntryGetSelection %W
} tk::Priv(data
)]} {
45 clipboard clear
-displayof %W
46 clipboard append -displayof %W
$tk::Priv(data
)
50 bind Entry
<<Paste
>> {
53 if {[tk windowingsystem
] ne
"x11"} {
55 %W delete sel.first sel.last
58 %W insert insert
[::tk::GetSelection %W CLIPBOARD
]
62 bind Entry
<<Clear
>> {
63 # ignore if there is no selection
64 catch { %W delete sel.first sel.last
}
66 bind Entry
<<PasteSelection
>> {
67 if {$tk_strictMotif ||
![info exists
tk::Priv(mouseMoved
)]
68 ||
!$tk::Priv(mouseMoved
)} {
73 bind Entry
<<TraverseIn
>> {
74 %W
selection range
0 end
78 # Standard Motif bindings:
81 tk::EntryButton1 %W
%x
84 bind Entry
<B1-Motion
> {
86 tk::EntryMouseSelect %W
%x
88 bind Entry
<Double-1
> {
89 set tk::Priv(selectMode
) word
90 tk::EntryMouseSelect %W
%x
91 catch {%W icursor sel.last
}
93 bind Entry
<Triple-1
> {
94 set tk::Priv(selectMode
) line
95 tk::EntryMouseSelect %W
%x
96 catch {%W icursor sel.last
}
98 bind Entry
<Shift-1
> {
99 set tk::Priv(selectMode
) char
100 %W
selection adjust
@%x
102 bind Entry
<Double-Shift-1
> {
103 set tk::Priv(selectMode
) word
104 tk::EntryMouseSelect %W
%x
106 bind Entry
<Triple-Shift-1
> {
107 set tk::Priv(selectMode
) line
108 tk::EntryMouseSelect %W
%x
110 bind Entry
<B1-Leave
> {
114 bind Entry
<B1-Enter
> {
117 bind Entry
<ButtonRelease-1
> {
120 bind Entry
<Control-1
> {
125 tk::EntrySetCursor %W
[expr {[%W index insert
] - 1}]
128 tk::EntrySetCursor %W
[expr {[%W index insert
] + 1}]
130 bind Entry
<Shift-Left
> {
131 tk::EntryKeySelect %W
[expr {[%W index insert
] - 1}]
132 tk::EntrySeeInsert %W
134 bind Entry
<Shift-Right
> {
135 tk::EntryKeySelect %W
[expr {[%W index insert
] + 1}]
136 tk::EntrySeeInsert %W
138 bind Entry
<Control-Left
> {
139 tk::EntrySetCursor %W
[tk::EntryPreviousWord %W insert
]
141 bind Entry
<Control-Right
> {
142 tk::EntrySetCursor %W
[tk::EntryNextWord %W insert
]
144 bind Entry
<Shift-Control-Left
> {
145 tk::EntryKeySelect %W
[tk::EntryPreviousWord %W insert
]
146 tk::EntrySeeInsert %W
148 bind Entry
<Shift-Control-Right
> {
149 tk::EntryKeySelect %W
[tk::EntryNextWord %W insert
]
150 tk::EntrySeeInsert %W
153 tk::EntrySetCursor %W
0
155 bind Entry
<Shift-Home
> {
156 tk::EntryKeySelect %W
0
157 tk::EntrySeeInsert %W
160 tk::EntrySetCursor %W end
162 bind Entry
<Shift-End
> {
163 tk::EntryKeySelect %W end
164 tk::EntrySeeInsert %W
167 bind Entry
<Delete
> {
168 if {[%W
selection present
]} {
169 %W delete sel.first sel.last
174 bind Entry
<BackSpace
> {
175 tk::EntryBackspace %W
178 bind Entry
<Control-space
> {
179 %W
selection from insert
181 bind Entry
<Select
> {
182 %W
selection from insert
184 bind Entry
<Control-Shift-space
> {
185 %W
selection adjust insert
187 bind Entry
<Shift-Select
> {
188 %W
selection adjust insert
190 bind Entry
<Control-slash
> {
191 %W
selection range
0 end
193 bind Entry
<Control-backslash
> {
196 bind Entry
<KeyPress
> {
198 tk::EntryInsert %W
%A
201 # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
202 # Otherwise, if a widget binding for one of these is defined, the
203 # <KeyPress> class binding will also fire and insert the character,
204 # which is wrong. Ditto for Escape, Return, and Tab.
206 bind Entry
<Alt-KeyPress
> {# nothing}
207 bind Entry
<Meta-KeyPress
> {# nothing}
208 bind Entry
<Control-KeyPress
> {# nothing}
209 bind Entry
<Escape
> {# nothing}
210 bind Entry
<Return
> {# nothing}
211 bind Entry
<KP_Enter
> {# nothing}
212 bind Entry
<Tab
> {# nothing}
213 if {[tk windowingsystem
] eq
"aqua"} {
214 bind Entry
<Command-KeyPress
> {# nothing}
217 # On Windows, paste is done using Shift-Insert. Shift-Insert already
218 # generates the <<Paste>> event, so we don't need to do anything here.
219 if {$tcl_platform(platform
) ne
"windows"} {
220 bind Entry
<Insert
> {
221 catch {tk::EntryInsert %W
[::tk::GetSelection %W PRIMARY
]}
225 # Additional emacs-like bindings:
227 bind Entry
<Control-a
> {
228 if {!$tk_strictMotif} {
229 tk::EntrySetCursor %W
0
232 bind Entry
<Control-b
> {
233 if {!$tk_strictMotif} {
234 tk::EntrySetCursor %W
[expr {[%W index insert
] - 1}]
237 bind Entry
<Control-d
> {
238 if {!$tk_strictMotif} {
242 bind Entry
<Control-e
> {
243 if {!$tk_strictMotif} {
244 tk::EntrySetCursor %W end
247 bind Entry
<Control-f
> {
248 if {!$tk_strictMotif} {
249 tk::EntrySetCursor %W
[expr {[%W index insert
] + 1}]
252 bind Entry
<Control-h
> {
253 if {!$tk_strictMotif} {
254 tk::EntryBackspace %W
257 bind Entry
<Control-k
> {
258 if {!$tk_strictMotif} {
262 bind Entry
<Control-t
> {
263 if {!$tk_strictMotif} {
264 tk::EntryTranspose %W
267 bind Entry
<Meta-b
> {
268 if {!$tk_strictMotif} {
269 tk::EntrySetCursor %W
[tk::EntryPreviousWord %W insert
]
272 bind Entry
<Meta-d
> {
273 if {!$tk_strictMotif} {
274 %W delete insert
[tk::EntryNextWord %W insert
]
277 bind Entry
<Meta-f
> {
278 if {!$tk_strictMotif} {
279 tk::EntrySetCursor %W
[tk::EntryNextWord %W insert
]
282 bind Entry
<Meta-BackSpace
> {
283 if {!$tk_strictMotif} {
284 %W delete
[tk::EntryPreviousWord %W insert
] insert
287 bind Entry
<Meta-Delete
> {
288 if {!$tk_strictMotif} {
289 %W delete
[tk::EntryPreviousWord %W insert
] insert
293 # A few additional bindings of my own.
296 if {!$tk_strictMotif} {
297 ::tk::EntryScanMark %W
%x
300 bind Entry
<B2-Motion
> {
301 if {!$tk_strictMotif} {
302 ::tk::EntryScanDrag %W
%x
306 # ::tk::EntryClosestGap --
307 # Given x and y coordinates, this procedure finds the closest boundary
308 # between characters to the given coordinates and returns the index
309 # of the character just after the boundary.
312 # w - The entry window.
313 # x - X-coordinate within the window.
315 proc ::tk::EntryClosestGap {w x
} {
316 set pos
[$w index
@$x]
317 set bbox
[$w bbox
$pos]
318 if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
324 # ::tk::EntryButton1 --
325 # This procedure is invoked to handle button-1 presses in entry
326 # widgets. It moves the insertion cursor, sets the selection anchor,
327 # and claims the input focus.
330 # w - The entry window in which the button was pressed.
331 # x - The x-coordinate of the button press.
333 proc ::tk::EntryButton1 {w x
} {
336 set Priv
(selectMode
) char
337 set Priv
(mouseMoved
) 0
339 $w icursor
[EntryClosestGap
$w $x]
340 $w selection from insert
341 if {"disabled" ne
[$w cget
-state]} {
346 # ::tk::EntryMouseSelect --
347 # This procedure is invoked when dragging out a selection with
348 # the mouse. Depending on the selection mode (character, word,
349 # line) it selects in different-sized units. This procedure
350 # ignores mouse motions initially until the mouse has moved from
351 # one character to another or until there have been multiple clicks.
354 # w - The entry window in which the button was pressed.
355 # x - The x-coordinate of the mouse.
357 proc ::tk::EntryMouseSelect {w x
} {
360 set cur
[EntryClosestGap
$w $x]
361 set anchor
[$w index anchor
]
362 if {($cur != $anchor) ||
(abs
($Priv(pressX
) - $x) >= 3)} {
363 set Priv
(mouseMoved
) 1
365 switch $Priv(selectMode
) {
367 if {$Priv(mouseMoved
)} {
368 if {$cur < $anchor} {
369 $w selection range
$cur $anchor
370 } elseif
{$cur > $anchor} {
371 $w selection range
$anchor $cur
378 if {$cur < [$w index anchor
]} {
379 set before
[tcl_wordBreakBefore [$w get
] $cur]
380 set after [tcl_wordBreakAfter [$w get
] [expr {$anchor-1}]]
382 set before
[tcl_wordBreakBefore [$w get
] $anchor]
383 set after [tcl_wordBreakAfter [$w get
] [expr {$cur - 1}]]
391 $w selection range
$before $after
394 $w selection range
0 end
397 if {$Priv(mouseMoved
)} {
403 # ::tk::EntryPaste --
404 # This procedure sets the insertion cursor to the current mouse position,
405 # pastes the selection there, and sets the focus to the window.
408 # w - The entry window.
409 # x - X position of the mouse.
411 proc ::tk::EntryPaste {w x
} {
412 $w icursor
[EntryClosestGap
$w $x]
413 catch {$w insert insert
[::tk::GetSelection $w PRIMARY
]}
414 if {"disabled" ne
[$w cget
-state]} {
419 # ::tk::EntryAutoScan --
420 # This procedure is invoked when the mouse leaves an entry window
421 # with button 1 down. It scrolls the window left or right,
422 # depending on where the mouse is, and reschedules itself as an
423 # "after" command so that the window continues to scroll until the
424 # mouse moves back into the window or the mouse button is released.
427 # w - The entry window.
429 proc ::tk::EntryAutoScan {w
} {
432 if {![winfo exists
$w]} {
435 if {$x >= [winfo width
$w]} {
436 $w xview scroll
2 units
437 EntryMouseSelect
$w $x
439 $w xview scroll
-2 units
440 EntryMouseSelect
$w $x
442 set Priv
(afterId
) [after 50 [list tk::EntryAutoScan $w]]
445 # ::tk::EntryKeySelect --
446 # This procedure is invoked when stroking out selections using the
447 # keyboard. It moves the cursor to a new position, then extends
448 # the selection to that position.
451 # w - The entry window.
452 # new - A new position for the insertion cursor (the cursor hasn't
453 # actually been moved to this position yet).
455 proc ::tk::EntryKeySelect {w new
} {
456 if {![$w selection present
]} {
457 $w selection from insert
460 $w selection adjust
$new
465 # ::tk::EntryInsert --
466 # Insert a string into an entry at the point of the insertion cursor.
467 # If there is a selection in the entry, and it covers the point of the
468 # insertion cursor, then delete the selection before inserting.
471 # w - The entry window in which to insert the string
472 # s - The string to insert (usually just a single character)
474 proc ::tk::EntryInsert {w s
} {
479 set insert
[$w index insert
]
480 if {([$w index sel.first
] <= $insert)
481 && ([$w index sel.last
] >= $insert)} {
482 $w delete sel.first sel.last
489 # ::tk::EntryBackspace --
490 # Backspace over the character just before the insertion cursor.
491 # If backspacing would move the cursor off the left edge of the
492 # window, reposition the cursor at about the middle of the window.
495 # w - The entry window in which to backspace.
497 proc ::tk::EntryBackspace w
{
498 if {[$w selection present
]} {
499 $w delete sel.first sel.last
501 set x
[expr {[$w index insert
] - 1}]
505 if {[$w index
@0] >= [$w index insert
]} {
507 set left
[lindex $range 0]
508 set right
[lindex $range 1]
509 $w xview moveto
[expr {$left - ($right - $left)/2.0}]
514 # ::tk::EntrySeeInsert --
515 # Make sure that the insertion cursor is visible in the entry window.
516 # If not, adjust the view so that it is.
519 # w - The entry window.
521 proc ::tk::EntrySeeInsert w
{
522 set c
[$w index insert
]
523 if {($c < [$w index
@0]) ||
($c > [$w index
@[winfo width
$w]])} {
528 # ::tk::EntrySetCursor -
529 # Move the insertion cursor to a given position in an entry. Also
530 # clears the selection, if there is one in the entry, and makes sure
531 # that the insertion cursor is visible.
534 # w - The entry window.
535 # pos - The desired new position for the cursor in the window.
537 proc ::tk::EntrySetCursor {w pos
} {
543 # ::tk::EntryTranspose -
544 # This procedure implements the "transpose" function for entry widgets.
545 # It tranposes the characters on either side of the insertion cursor,
546 # unless the cursor is at the end of the line. In this case it
547 # transposes the two characters to the left of the cursor. In either
548 # case, the cursor ends up to the right of the transposed characters.
551 # w - The entry window.
553 proc ::tk::EntryTranspose w
{
554 set i
[$w index insert
]
555 if {$i < [$w index end
]} {
558 set first
[expr {$i-2}]
563 set new
[string index
$data [expr {$i-1}]][string index
$data $first]
565 $w insert insert
$new
569 # ::tk::EntryNextWord --
570 # Returns the index of the next word position after a given position in the
571 # entry. The next word is platform dependent and may be either the next
572 # end-of-word position or the next start-of-word position after the next
573 # end-of-word position.
576 # w - The entry window in which the cursor is to move.
577 # start - Position at which to start search.
579 if {$tcl_platform(platform
) eq
"windows"} {
580 proc ::tk::EntryNextWord {w start
} {
581 set pos
[tcl_endOfWord [$w get
] [$w index
$start]]
583 set pos
[tcl_startOfNextWord [$w get
] $pos]
591 proc ::tk::EntryNextWord {w start
} {
592 set pos
[tcl_endOfWord [$w get
] [$w index
$start]]
600 # ::tk::EntryPreviousWord --
602 # Returns the index of the previous word position before a given
603 # position in the entry.
606 # w - The entry window in which the cursor is to move.
607 # start - Position at which to start search.
609 proc ::tk::EntryPreviousWord {w start
} {
610 set pos
[tcl_startOfPreviousWord [$w get
] [$w index
$start]]
617 # ::tk::EntryScanMark --
619 # Marks the start of a possible scan drag operation
622 # w - The entry window from which the text to get
623 # x - x location on screen
625 proc ::tk::EntryScanMark {w x
} {
628 set ::tk::Priv(y
) 0 ; # not used
629 set ::tk::Priv(mouseMoved
) 0
632 # ::tk::EntryScanDrag --
634 # Marks the start of a possible scan drag operation
637 # w - The entry window from which the text to get
638 # x - x location on screen
640 proc ::tk::EntryScanDrag {w x
} {
641 # Make sure these exist, as some weird situations can trigger the
642 # motion binding without the initial press. [Bug #220269]
643 if {![info exists
::tk::Priv(x
)]} { set ::tk::Priv(x
) $x }
645 if {abs
($x-$::tk::Priv(x
)) > 2} {
646 set ::tk::Priv(mouseMoved
) 1
651 # ::tk::EntryGetSelection --
653 # Returns the selected text of the entry with respect to the -show option.
656 # w - The entry window from which the text to get
658 proc ::tk::EntryGetSelection {w
} {
659 set entryString
[string range
[$w get
] [$w index sel.first
] \
660 [expr {[$w index sel.last
] - 1}]]
661 if {[$w cget
-show] ne
""} {
662 return [string repeat
[string index
[$w cget
-show] 0] \
663 [string length
$entryString]]