Upgrade to Tcl/Tk 8.5b2
[msysgit.git] / mingw / lib / tk8.5 / tk.tcl
blob4efa18ad89d6113addd7bfdf98e7348c8b4138aa
1 # tk.tcl --
3 # Initialization script normally executed in the interpreter for each
4 # Tk-based application. Arranges class bindings for widgets.
6 # RCS: @(#) $Id: tk.tcl,v 1.67 2007/10/26 15:23:05 dgp Exp $
8 # Copyright (c) 1992-1994 The Regents of the University of California.
9 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
10 # Copyright (c) 1998-2000 Ajuba Solutions.
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 package require Tcl 8.5 ;# Guard against [source] in an 8.4- interp
16 ;# before using 8.5 [package] features.
17 # Insist on running with compatible version of Tcl
18 package require Tcl 8.5b1-8.6
19 # Verify that we have Tk binary and script components from the same release
20 package require -exact Tk 8.5b2
22 # Create a ::tk namespace
23 namespace eval ::tk {
24 # Set up the msgcat commands
25 namespace eval msgcat {
26 namespace export mc mcmax
27 if {[interp issafe] || [catch {package require msgcat}]} {
28 # The msgcat package is not available. Supply our own
29 # minimal replacement.
30 proc mc {src args} {
31 return [format $src {*}$args]
33 proc mcmax {args} {
34 set max 0
35 foreach string $args {
36 set len [string length $string]
37 if {$len>$max} {
38 set max $len
41 return $max
43 } else {
44 # Get the commands from the msgcat package that Tk uses.
45 namespace import ::msgcat::mc
46 namespace import ::msgcat::mcmax
47 ::msgcat::mcload [file join $::tk_library msgs]
50 namespace import ::tk::msgcat::*
52 # and a ::ttk namespace
53 namespace eval ::ttk {
54 if {$::tk_library ne ""} {
55 # avoid file join to work in safe interps, but this is also x-plat ok
56 variable library $::tk_library/ttk
60 # Add Ttk & Tk's directory to the end of the auto-load search path, if it
61 # isn't already on the path:
63 if {[info exists ::auto_path] && ($::tk_library ne "")
64 && ($::tk_library ni $::auto_path)} {
65 lappend ::auto_path $::tk_library $::ttk::library
68 # Turn off strict Motif look and feel as a default.
70 set ::tk_strictMotif 0
72 # Turn on useinputmethods (X Input Methods) by default.
73 # We catch this because safe interpreters may not allow the call.
75 catch {tk useinputmethods 1}
77 # ::tk::PlaceWindow --
78 # place a toplevel at a particular position
79 # Arguments:
80 # toplevel name of toplevel window
81 # ?placement? pointer ?center? ; places $w centered on the pointer
82 # widget widgetPath ; centers $w over widget_name
83 # defaults to placing toplevel in the middle of the screen
84 # ?anchor? center or widgetPath
85 # Results:
86 # Returns nothing
88 proc ::tk::PlaceWindow {w {place ""} {anchor ""}} {
89 wm withdraw $w
90 update idletasks
91 set checkBounds 1
92 if {$place eq ""} {
93 set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}]
94 set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}]
95 set checkBounds 0
96 } elseif {[string equal -length [string length $place] $place "pointer"]} {
97 ## place at POINTER (centered if $anchor == center)
98 if {[string equal -length [string length $anchor] $anchor "center"]} {
99 set x [expr {[winfo pointerx $w]-[winfo reqwidth $w]/2}]
100 set y [expr {[winfo pointery $w]-[winfo reqheight $w]/2}]
101 } else {
102 set x [winfo pointerx $w]
103 set y [winfo pointery $w]
105 } elseif {[string equal -length [string length $place] $place "widget"] && \
106 [winfo exists $anchor] && [winfo ismapped $anchor]} {
107 ## center about WIDGET $anchor, widget must be mapped
108 set x [expr {[winfo rootx $anchor] + \
109 ([winfo width $anchor]-[winfo reqwidth $w])/2}]
110 set y [expr {[winfo rooty $anchor] + \
111 ([winfo height $anchor]-[winfo reqheight $w])/2}]
112 } else {
113 set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}]
114 set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}]
115 set checkBounds 0
117 if {[tk windowingsystem] eq "win32"} {
118 # Bug 533519: win32 multiple desktops may produce negative geometry.
119 set checkBounds 0
121 if {$checkBounds} {
122 if {$x < 0} {
123 set x 0
124 } elseif {$x > ([winfo screenwidth $w]-[winfo reqwidth $w])} {
125 set x [expr {[winfo screenwidth $w]-[winfo reqwidth $w]}]
127 if {$y < 0} {
128 set y 0
129 } elseif {$y > ([winfo screenheight $w]-[winfo reqheight $w])} {
130 set y [expr {[winfo screenheight $w]-[winfo reqheight $w]}]
132 if {[tk windowingsystem] eq "aqua"} {
133 # Avoid the native menu bar which sits on top of everything.
134 if {$y < 22} { set y 22 }
137 wm geometry $w +$x+$y
138 wm deiconify $w
141 # ::tk::SetFocusGrab --
142 # swap out current focus and grab temporarily (for dialogs)
143 # Arguments:
144 # grab new window to grab
145 # focus window to give focus to
146 # Results:
147 # Returns nothing
149 proc ::tk::SetFocusGrab {grab {focus {}}} {
150 set index "$grab,$focus"
151 upvar ::tk::FocusGrab($index) data
153 lappend data [focus]
154 set oldGrab [grab current $grab]
155 lappend data $oldGrab
156 if {[winfo exists $oldGrab]} {
157 lappend data [grab status $oldGrab]
159 # The "grab" command will fail if another application
160 # already holds the grab. So catch it.
161 catch {grab $grab}
162 if {[winfo exists $focus]} {
163 focus $focus
167 # ::tk::RestoreFocusGrab --
168 # restore old focus and grab (for dialogs)
169 # Arguments:
170 # grab window that had taken grab
171 # focus window that had taken focus
172 # destroy destroy|withdraw - how to handle the old grabbed window
173 # Results:
174 # Returns nothing
176 proc ::tk::RestoreFocusGrab {grab focus {destroy destroy}} {
177 set index "$grab,$focus"
178 if {[info exists ::tk::FocusGrab($index)]} {
179 foreach {oldFocus oldGrab oldStatus} $::tk::FocusGrab($index) { break }
180 unset ::tk::FocusGrab($index)
181 } else {
182 set oldGrab ""
185 catch {focus $oldFocus}
186 grab release $grab
187 if {$destroy eq "withdraw"} {
188 wm withdraw $grab
189 } else {
190 destroy $grab
192 if {[winfo exists $oldGrab] && [winfo ismapped $oldGrab]} {
193 if {$oldStatus eq "global"} {
194 grab -global $oldGrab
195 } else {
196 grab $oldGrab
201 # ::tk::GetSelection --
202 # This tries to obtain the default selection. On Unix, we first try
203 # and get a UTF8_STRING, a type supported by modern Unix apps for
204 # passing Unicode data safely. We fall back on the default STRING
205 # type otherwise. On Windows, only the STRING type is necessary.
206 # Arguments:
207 # w The widget for which the selection will be retrieved.
208 # Important for the -displayof property.
209 # sel The source of the selection (PRIMARY or CLIPBOARD)
210 # Results:
211 # Returns the selection, or an error if none could be found
213 if {$tcl_platform(platform) eq "unix"} {
214 proc ::tk::GetSelection {w {sel PRIMARY}} {
215 if {[catch {selection get -displayof $w -selection $sel \
216 -type UTF8_STRING} txt] \
217 && [catch {selection get -displayof $w -selection $sel} txt]} {
218 return -code error "could not find default selection"
219 } else {
220 return $txt
223 } else {
224 proc ::tk::GetSelection {w {sel PRIMARY}} {
225 if {[catch {selection get -displayof $w -selection $sel} txt]} {
226 return -code error "could not find default selection"
227 } else {
228 return $txt
233 # ::tk::ScreenChanged --
234 # This procedure is invoked by the binding mechanism whenever the
235 # "current" screen is changing. The procedure does two things.
236 # First, it uses "upvar" to make variable "::tk::Priv" point at an
237 # array variable that holds state for the current display. Second,
238 # it initializes the array if it didn't already exist.
240 # Arguments:
241 # screen - The name of the new screen.
243 proc ::tk::ScreenChanged screen {
244 set x [string last . $screen]
245 if {$x > 0} {
246 set disp [string range $screen 0 [expr {$x - 1}]]
247 } else {
248 set disp $screen
251 uplevel #0 upvar #0 ::tk::Priv.$disp ::tk::Priv
252 variable ::tk::Priv
253 global tcl_platform
255 if {[info exists Priv]} {
256 set Priv(screen) $screen
257 return
259 array set Priv {
260 activeMenu {}
261 activeItem {}
262 afterId {}
263 buttons 0
264 buttonWindow {}
265 dragging 0
266 focus {}
267 grab {}
268 initPos {}
269 inMenubutton {}
270 listboxPrev {}
271 menuBar {}
272 mouseMoved 0
273 oldGrab {}
274 popup {}
275 postedMb {}
276 pressX 0
277 pressY 0
278 prevPos 0
279 selectMode char
281 set Priv(screen) $screen
282 set Priv(tearoff) [string equal [tk windowingsystem] "x11"]
283 set Priv(window) {}
286 # Do initial setup for Priv, so that it is always bound to something
287 # (otherwise, if someone references it, it may get set to a non-upvar-ed
288 # value, which will cause trouble later).
290 tk::ScreenChanged [winfo screen .]
292 # ::tk::EventMotifBindings --
293 # This procedure is invoked as a trace whenever ::tk_strictMotif is
294 # changed. It is used to turn on or turn off the motif virtual
295 # bindings.
297 # Arguments:
298 # n1 - the name of the variable being changed ("::tk_strictMotif").
300 proc ::tk::EventMotifBindings {n1 dummy dummy} {
301 upvar $n1 name
303 if {$name} {
304 set op delete
305 } else {
306 set op add
309 event $op <<Cut>> <Control-Key-w>
310 event $op <<Copy>> <Meta-Key-w>
311 event $op <<Paste>> <Control-Key-y>
312 event $op <<Undo>> <Control-underscore>
315 #----------------------------------------------------------------------
316 # Define common dialogs on platforms where they are not implemented
317 # using compiled code.
318 #----------------------------------------------------------------------
320 if {![llength [info commands tk_chooseColor]]} {
321 proc ::tk_chooseColor {args} {
322 return [tk::dialog::color:: {*}$args]
325 if {![llength [info commands tk_getOpenFile]]} {
326 proc ::tk_getOpenFile {args} {
327 if {$::tk_strictMotif} {
328 return [tk::MotifFDialog open {*}$args]
329 } else {
330 return [::tk::dialog::file:: open {*}$args]
334 if {![llength [info commands tk_getSaveFile]]} {
335 proc ::tk_getSaveFile {args} {
336 if {$::tk_strictMotif} {
337 return [tk::MotifFDialog save {*}$args]
338 } else {
339 return [::tk::dialog::file:: save {*}$args]
343 if {![llength [info commands tk_messageBox]]} {
344 proc ::tk_messageBox {args} {
345 return [tk::MessageBox {*}$args]
348 if {![llength [info command tk_chooseDirectory]]} {
349 proc ::tk_chooseDirectory {args} {
350 return [::tk::dialog::file::chooseDir:: {*}$args]
354 #----------------------------------------------------------------------
355 # Define the set of common virtual events.
356 #----------------------------------------------------------------------
358 switch -- [tk windowingsystem] {
359 "x11" {
360 event add <<Cut>> <Control-Key-x> <Key-F20>
361 event add <<Copy>> <Control-Key-c> <Key-F16>
362 event add <<Paste>> <Control-Key-v> <Key-F18>
363 event add <<PasteSelection>> <ButtonRelease-2>
364 event add <<Undo>> <Control-Key-z>
365 event add <<Redo>> <Control-Key-Z>
366 # Some OS's define a goofy (as in, not <Shift-Tab>) keysym
367 # that is returned when the user presses <Shift-Tab>. In order for
368 # tab traversal to work, we have to add these keysyms to the
369 # PrevWindow event.
370 # We use catch just in case the keysym isn't recognized.
371 # This is needed for XFree86 systems
372 catch { event add <<PrevWindow>> <ISO_Left_Tab> }
373 # This seems to be correct on *some* HP systems.
374 catch { event add <<PrevWindow>> <hpBackTab> }
376 trace add variable ::tk_strictMotif write ::tk::EventMotifBindings
377 set ::tk_strictMotif $::tk_strictMotif
378 # On unix, we want to always display entry/text selection,
379 # regardless of which window has focus
380 set ::tk::AlwaysShowSelection 1
382 "win32" {
383 event add <<Cut>> <Control-Key-x> <Shift-Key-Delete>
384 event add <<Copy>> <Control-Key-c> <Control-Key-Insert>
385 event add <<Paste>> <Control-Key-v> <Shift-Key-Insert>
386 event add <<PasteSelection>> <ButtonRelease-2>
387 event add <<Undo>> <Control-Key-z>
388 event add <<Redo>> <Control-Key-y>
390 "aqua" {
391 event add <<Cut>> <Command-Key-x> <Key-F2>
392 event add <<Copy>> <Command-Key-c> <Key-F3>
393 event add <<Paste>> <Command-Key-v> <Key-F4>
394 event add <<PasteSelection>> <ButtonRelease-2>
395 event add <<Clear>> <Clear>
396 event add <<Undo>> <Command-Key-z>
397 event add <<Redo>> <Command-Key-y>
400 # ----------------------------------------------------------------------
401 # Read in files that define all of the class bindings.
402 # ----------------------------------------------------------------------
404 if {$::tk_library ne ""} {
405 proc ::tk::SourceLibFile {file} {
406 namespace eval :: [list source [file join $::tk_library $file.tcl]]
408 namespace eval ::tk {
409 SourceLibFile button
410 SourceLibFile entry
411 SourceLibFile listbox
412 SourceLibFile menu
413 SourceLibFile panedwindow
414 SourceLibFile scale
415 SourceLibFile scrlbar
416 SourceLibFile spinbox
417 SourceLibFile text
420 # ----------------------------------------------------------------------
421 # Default bindings for keyboard traversal.
422 # ----------------------------------------------------------------------
424 event add <<PrevWindow>> <Shift-Tab>
425 bind all <Tab> {tk::TabToWindow [tk_focusNext %W]}
426 bind all <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]}
428 # ::tk::CancelRepeat --
429 # This procedure is invoked to cancel an auto-repeat action described
430 # by ::tk::Priv(afterId). It's used by several widgets to auto-scroll
431 # the widget when the mouse is dragged out of the widget with a
432 # button pressed.
434 # Arguments:
435 # None.
437 proc ::tk::CancelRepeat {} {
438 variable ::tk::Priv
439 after cancel $Priv(afterId)
440 set Priv(afterId) {}
443 # ::tk::TabToWindow --
444 # This procedure moves the focus to the given widget.
445 # It sends a <<TraverseOut>> virtual event to the previous focus window,
446 # if any, before changing the focus, and a <<TraverseIn>> event
447 # to the new focus window afterwards.
449 # Arguments:
450 # w - Window to which focus should be set.
452 proc ::tk::TabToWindow {w} {
453 set focus [focus]
454 if {$focus ne ""} {
455 event generate $focus <<TraverseOut>>
457 focus $w
458 event generate $w <<TraverseIn>>
461 # ::tk::UnderlineAmpersand --
462 # This procedure takes some text with ampersand and returns
463 # text w/o ampersand and position of the ampersand.
464 # Double ampersands are converted to single ones.
465 # Position returned is -1 when there is no ampersand.
467 proc ::tk::UnderlineAmpersand {text} {
468 set idx [string first "&" $text]
469 if {$idx >= 0} {
470 set underline $idx
471 # ignore "&&"
472 while {[string match "&" [string index $text [expr {$idx + 1}]]]} {
473 set base [expr {$idx + 2}]
474 set idx [string first "&" [string range $text $base end]]
475 if {$idx < 0} {
476 break
477 } else {
478 set underline [expr {$underline + $idx + 1}]
479 incr idx $base
483 if {$idx >= 0} {
484 regsub -all -- {&([^&])} $text {\1} text
486 return [list $text $idx]
489 # ::tk::SetAmpText --
490 # Given widget path and text with "magic ampersands",
491 # sets -text and -underline options for the widget
493 proc ::tk::SetAmpText {widget text} {
494 lassign [UnderlineAmpersand $text] newtext under
495 $widget configure -text $newtext -underline $under
498 # ::tk::AmpWidget --
499 # Creates new widget, turning -text option into -text and
500 # -underline options, returned by ::tk::UnderlineAmpersand.
502 proc ::tk::AmpWidget {class path args} {
503 set options {}
504 foreach {opt val} $args {
505 if {$opt eq "-text"} {
506 lassign [UnderlineAmpersand $val] newtext under
507 lappend options -text $newtext -underline $under
508 } else {
509 lappend options $opt $val
512 set result [$class $path {*}$options]
513 if {$class eq "button"} {
514 bind $path <<AltUnderlined>> [list $path invoke]
516 return $result
519 # ::tk::AmpMenuArgs --
520 # Processes arguments for a menu entry, turning -label option into
521 # -label and -underline options, returned by ::tk::UnderlineAmpersand.
523 proc ::tk::AmpMenuArgs {widget add type args} {
524 set options {}
525 foreach {opt val} $args {
526 if {$opt eq "-label"} {
527 lassign [UnderlineAmpersand $val] newlabel under
528 lappend options -label $newlabel -underline $under
529 } else {
530 lappend options $opt $val
533 $widget add $type {*}$options
536 # ::tk::FindAltKeyTarget --
537 # search recursively through the hierarchy of visible widgets
538 # to find button or label which has $char as underlined character
540 proc ::tk::FindAltKeyTarget {path char} {
541 switch -- [winfo class $path] {
542 Button -
543 Label {
544 if {[string equal -nocase $char \
545 [string index [$path cget -text] [$path cget -underline]]]} {
546 return $path
547 } else {
548 return {}
551 default {
552 foreach child [concat [grid slaves $path] \
553 [pack slaves $path] [place slaves $path]] {
554 set target [FindAltKeyTarget $child $char]
555 if {$target ne ""} {
556 return $target
561 return {}
564 # ::tk::AltKeyInDialog --
565 # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>>
566 # to button or label which has appropriate underlined character
568 proc ::tk::AltKeyInDialog {path key} {
569 set target [FindAltKeyTarget $path $key]
570 if { $target eq ""} return
571 event generate $target <<AltUnderlined>>
574 # ::tk::mcmaxamp --
575 # Replacement for mcmax, used for texts with "magic ampersand" in it.
578 proc ::tk::mcmaxamp {args} {
579 set maxlen 0
580 foreach arg $args {
581 # Should we run [mc] in caller's namespace?
582 lassign [UnderlineAmpersand [mc $arg]] msg
583 set length [string length $msg]
584 if {$length > $maxlen} {
585 set maxlen $length
588 return $maxlen
590 # For now, turn off the custom mdef proc for the mac:
592 if {[tk windowingsystem] eq "aqua"} {
593 namespace eval ::tk::mac {
594 set useCustomMDEF 0
598 # Run the Ttk themed widget set initialization
599 if {$::ttk::library ne ""} {
600 uplevel \#0 [list source $::ttk::library/ttk.tcl]