3 # Initialization script normally executed in the interpreter for each Tk-based
4 # application. Arranges class bindings for widgets.
6 # Copyright (c) 1992-1994 The Regents of the University of California.
7 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
8 # Copyright (c) 1998-2000 Ajuba Solutions.
10 # See the file "license.terms" for information on usage and redistribution of
11 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 package require
Tcl 8.5 ;# Guard against [source] in an 8.4- interp before
14 ;# using 8.5 [package] features.
15 # Insist on running with compatible version of Tcl
16 package require
Tcl 8.5.0
17 # Verify that we have Tk binary and script components from the same release
18 package require
-exact Tk
8.5.11
20 # Create a ::tk namespace
22 # Set up the msgcat commands
23 namespace eval msgcat {
24 namespace export mc mcmax
25 if {[interp issafe
] ||
[catch {package require
msgcat}]} {
26 # The msgcat package is not available. Supply our own minimal
29 return [format $src {*}$args]
33 foreach string $args {
34 set len
[string length
$string]
42 # Get the commands from the msgcat package that Tk uses.
43 namespace import
::msgcat::mc
44 namespace import
::msgcat::mcmax
45 ::msgcat::mcload [file join $::tk_library msgs
]
48 namespace import
::tk::msgcat::*
50 # and a ::ttk namespace
51 namespace eval ::ttk {
52 if {$::tk_library ne
""} {
53 # avoid file join to work in safe interps, but this is also x-plat ok
54 variable library
$::tk_library/ttk
58 # Add Ttk & Tk's directory to the end of the auto-load search path, if it
59 # isn't already on the path:
61 if {[info exists
::auto_path] && ($::tk_library ne
"")
62 && ($::tk_library ni
$::auto_path)} {
63 lappend ::auto_path $::tk_library $::ttk::library
66 # Turn off strict Motif look and feel as a default.
68 set ::tk_strictMotif 0
70 # Turn on useinputmethods (X Input Methods) by default. We catch this because
71 # safe interpreters may not allow the call.
73 catch {tk useinputmethods
1}
75 # ::tk::PlaceWindow --
76 # Place a toplevel at a particular position
78 # toplevel name of toplevel window
79 # ?placement? pointer ?center? ; places $w centered on the pointer
80 # widget widgetPath ; centers $w over widget_name
81 # defaults to placing toplevel in the middle of the screen
82 # ?anchor? center or widgetPath
86 proc ::tk::PlaceWindow {w
{place ""} {anchor
""}} {
91 set x
[expr {([winfo screenwidth
$w]-[winfo reqwidth
$w])/2}]
92 set y
[expr {([winfo screenheight
$w]-[winfo reqheight
$w])/2}]
94 } elseif
{[string equal
-length [string length
$place] $place "pointer"]} {
95 ## place at POINTER (centered if $anchor == center)
96 if {[string equal
-length [string length
$anchor] $anchor "center"]} {
97 set x
[expr {[winfo pointerx
$w]-[winfo reqwidth
$w]/2}]
98 set y
[expr {[winfo pointery
$w]-[winfo reqheight
$w]/2}]
100 set x
[winfo pointerx
$w]
101 set y
[winfo pointery
$w]
103 } elseif
{[string equal
-length [string length
$place] $place "widget"] && \
104 [winfo exists
$anchor] && [winfo ismapped
$anchor]} {
105 ## center about WIDGET $anchor, widget must be mapped
106 set x
[expr {[winfo rootx
$anchor] + \
107 ([winfo width
$anchor]-[winfo reqwidth
$w])/2}]
108 set y
[expr {[winfo rooty
$anchor] + \
109 ([winfo height
$anchor]-[winfo reqheight
$w])/2}]
111 set x
[expr {([winfo screenwidth
$w]-[winfo reqwidth
$w])/2}]
112 set y
[expr {([winfo screenheight
$w]-[winfo reqheight
$w])/2}]
115 if {[tk windowingsystem
] eq
"win32"} {
116 # Bug 533519: win32 multiple desktops may produce negative geometry.
122 } elseif
{$x > ([winfo screenwidth
$w]-[winfo reqwidth
$w])} {
123 set x
[expr {[winfo screenwidth
$w]-[winfo reqwidth
$w]}]
127 } elseif
{$y > ([winfo screenheight
$w]-[winfo reqheight
$w])} {
128 set y
[expr {[winfo screenheight
$w]-[winfo reqheight
$w]}]
130 if {[tk windowingsystem
] eq
"aqua"} {
131 # Avoid the native menu bar which sits on top of everything.
132 if {$y < 22} { set y
22 }
135 wm geometry
$w +$x+$y
139 # ::tk::SetFocusGrab --
140 # Swap out current focus and grab temporarily (for dialogs)
142 # grab new window to grab
143 # focus window to give focus to
147 proc ::tk::SetFocusGrab {grab {focus {}}} {
148 set index
"$grab,$focus"
149 upvar ::tk::FocusGrab($index) data
152 set oldGrab
[grab current
$grab]
153 lappend data
$oldGrab
154 if {[winfo exists
$oldGrab]} {
155 lappend data
[grab status
$oldGrab]
157 # The "grab" command will fail if another application already holds the
160 if {[winfo exists
$focus]} {
165 # ::tk::RestoreFocusGrab --
166 # Restore old focus and grab (for dialogs)
168 # grab window that had taken grab
169 # focus window that had taken focus
170 # destroy destroy|withdraw - how to handle the old grabbed window
174 proc ::tk::RestoreFocusGrab {grab focus {destroy destroy}} {
175 set index
"$grab,$focus"
176 if {[info exists
::tk::FocusGrab($index)]} {
177 foreach {oldFocus oldGrab oldStatus
} $::tk::FocusGrab($index) { break }
178 unset ::tk::FocusGrab($index)
183 catch {focus $oldFocus}
185 if {$destroy eq
"withdraw"} {
190 if {[winfo exists
$oldGrab] && [winfo ismapped
$oldGrab]} {
191 if {$oldStatus eq
"global"} {
192 grab -global $oldGrab
199 # ::tk::GetSelection --
200 # This tries to obtain the default selection. On Unix, we first try and get
201 # a UTF8_STRING, a type supported by modern Unix apps for passing Unicode
202 # data safely. We fall back on the default STRING type otherwise. On
203 # Windows, only the STRING type is necessary.
205 # w The widget for which the selection will be retrieved.
206 # Important for the -displayof property.
207 # sel The source of the selection (PRIMARY or CLIPBOARD)
209 # Returns the selection, or an error if none could be found
211 if {$tcl_platform(platform
) eq
"unix"} {
212 proc ::tk::GetSelection {w
{sel PRIMARY
}} {
213 if {[catch {selection get
-displayof $w -selection $sel \
214 -type UTF8_STRING
} txt
] \
215 && [catch {selection get
-displayof $w -selection $sel} txt
]} {
216 return -code error "could not find default selection"
222 proc ::tk::GetSelection {w
{sel PRIMARY
}} {
223 if {[catch {selection get
-displayof $w -selection $sel} txt
]} {
224 return -code error "could not find default selection"
231 # ::tk::ScreenChanged --
232 # This procedure is invoked by the binding mechanism whenever the "current"
233 # screen is changing. The procedure does two things. First, it uses "upvar"
234 # to make variable "::tk::Priv" point at an array variable that holds state
235 # for the current display. Second, it initializes the array if it didn't
239 # screen - The name of the new screen.
241 proc ::tk::ScreenChanged {screen
} {
242 set x
[string last .
$screen]
244 set disp
[string range
$screen 0 [expr {$x - 1}]]
249 # Ensure that namespace separators never occur in the display name (as
250 # they cause problems in variable names). Double-colons exist in some VNC
251 # display names. [Bug 2912473]
252 set disp
[string map
{:: _doublecolon_
} $disp]
254 uplevel #0 [list upvar #0 ::tk::Priv.$disp ::tk::Priv]
258 if {[info exists Priv
]} {
259 set Priv
(screen
) $screen
284 set Priv
(screen
) $screen
285 set Priv
(tearoff
) [string equal
[tk windowingsystem
] "x11"]
289 # Do initial setup for Priv, so that it is always bound to something
290 # (otherwise, if someone references it, it may get set to a non-upvar-ed
291 # value, which will cause trouble later).
293 tk::ScreenChanged [winfo screen .
]
295 # ::tk::EventMotifBindings --
296 # This procedure is invoked as a trace whenever ::tk_strictMotif is changed.
297 # It is used to turn on or turn off the motif virtual bindings.
300 # n1 - the name of the variable being changed ("::tk_strictMotif").
302 proc ::tk::EventMotifBindings {n1 dummy dummy
} {
311 event $op <<Cut
>> <Control-Key-w
>
312 event $op <<Copy
>> <Meta-Key-w
>
313 event $op <<Paste
>> <Control-Key-y
>
314 event $op <<Undo
>> <Control-underscore
>
317 #----------------------------------------------------------------------
318 # Define common dialogs on platforms where they are not implemented using
320 #----------------------------------------------------------------------
322 if {![llength [info commands
tk_chooseColor]]} {
323 proc ::tk_chooseColor {args
} {
324 return [tk::dialog::color:: {*}$args]
327 if {![llength [info commands
tk_getOpenFile]]} {
328 proc ::tk_getOpenFile {args
} {
329 if {$::tk_strictMotif} {
330 return [tk::MotifFDialog open {*}$args]
332 return [::tk::dialog::file:: open {*}$args]
336 if {![llength [info commands
tk_getSaveFile]]} {
337 proc ::tk_getSaveFile {args
} {
338 if {$::tk_strictMotif} {
339 return [tk::MotifFDialog save
{*}$args]
341 return [::tk::dialog::file:: save
{*}$args]
345 if {![llength [info commands
tk_messageBox]]} {
346 proc ::tk_messageBox {args
} {
347 return [tk::MessageBox {*}$args]
350 if {![llength [info command
tk_chooseDirectory]]} {
351 proc ::tk_chooseDirectory {args
} {
352 return [::tk::dialog::file::chooseDir:: {*}$args]
356 #----------------------------------------------------------------------
357 # Define the set of common virtual events.
358 #----------------------------------------------------------------------
360 switch -exact -- [tk windowingsystem
] {
362 event add
<<Cut
>> <Control-Key-x
> <Key-F20
> <Control-Lock-Key-X
>
363 event add
<<Copy
>> <Control-Key-c
> <Key-F16
> <Control-Lock-Key-C
>
364 event add
<<Paste
>> <Control-Key-v
> <Key-F18
> <Control-Lock-Key-V
>
365 event add
<<PasteSelection
>> <ButtonRelease-2
>
366 event add
<<Undo
>> <Control-Key-z
> <Control-Lock-Key-Z
>
367 event add
<<Redo
>> <Control-Key-Z
> <Control-Lock-Key-z
>
368 # Some OS's define a goofy (as in, not <Shift-Tab>) keysym that is
369 # returned when the user presses <Shift-Tab>. In order for tab
370 # traversal to work, we have to add these keysyms to the PrevWindow
371 # event. We use catch just in case the keysym isn't recognized. This
372 # is needed for XFree86 systems
373 catch { event add
<<PrevWindow
>> <ISO_Left_Tab
> }
374 # This seems to be correct on *some* HP systems.
375 catch { event add
<<PrevWindow
>> <hpBackTab
> }
377 trace add
variable ::tk_strictMotif write
::tk::EventMotifBindings
378 set ::tk_strictMotif $::tk_strictMotif
379 # On unix, we want to always display entry/text selection, regardless
380 # of which window has focus
381 set ::tk::AlwaysShowSelection 1
384 event add
<<Cut
>> <Control-Key-x
> <Shift-Key-Delete
> \
386 event add
<<Copy
>> <Control-Key-c
> <Control-Key-Insert
> \
388 event add
<<Paste
>> <Control-Key-v
> <Shift-Key-Insert
> \
390 event add
<<PasteSelection
>> <ButtonRelease-2
>
391 event add
<<Undo
>> <Control-Key-z
> <Control-Lock-Key-Z
>
392 event add
<<Redo
>> <Control-Key-y
> <Control-Lock-Key-Y
>
395 event add
<<Cut
>> <Command-Key-x
> <Key-F2
> <Control-Lock-Key-X
>
396 event add
<<Copy
>> <Command-Key-c
> <Key-F3
> <Control-Lock-Key-C
>
397 event add
<<Paste
>> <Command-Key-v
> <Key-F4
> <Control-Lock-Key-V
>
398 event add
<<PasteSelection
>> <ButtonRelease-2
>
399 event add
<<Clear
>> <Clear
>
400 event add
<<Undo
>> <Command-Key-z
> <Control-Lock-Key-Z
>
401 event add
<<Redo
>> <Command-Key-y
> <Control-Lock-Key-Y
>
405 # ----------------------------------------------------------------------
406 # Read in files that define all of the class bindings.
407 # ----------------------------------------------------------------------
409 if {$::tk_library ne
""} {
410 proc ::tk::SourceLibFile {file} {
411 namespace eval :: [list source [file join $::tk_library $file.tcl
]]
413 namespace eval ::tk {
416 SourceLibFile
listbox
418 SourceLibFile
panedwindow
420 SourceLibFile scrlbar
421 SourceLibFile
spinbox
426 # ----------------------------------------------------------------------
427 # Default bindings for keyboard traversal.
428 # ----------------------------------------------------------------------
430 event add
<<PrevWindow
>> <Shift-Tab
>
431 bind all
<Tab
> {tk::TabToWindow [tk_focusNext %W
]}
432 bind all
<<PrevWindow
>> {tk::TabToWindow [tk_focusPrev %W
]}
434 # ::tk::CancelRepeat --
435 # This procedure is invoked to cancel an auto-repeat action described by
436 # ::tk::Priv(afterId). It's used by several widgets to auto-scroll the widget
437 # when the mouse is dragged out of the widget with a button pressed.
442 proc ::tk::CancelRepeat {} {
444 after cancel
$Priv(afterId
)
448 # ::tk::TabToWindow --
449 # This procedure moves the focus to the given widget.
450 # It sends a <<TraverseOut>> virtual event to the previous focus window, if
451 # any, before changing the focus, and a <<TraverseIn>> event to the new focus
455 # w - Window to which focus should be set.
457 proc ::tk::TabToWindow {w
} {
460 event generate
$focus <<TraverseOut
>>
463 event generate
$w <<TraverseIn
>>
466 # ::tk::UnderlineAmpersand --
467 # This procedure takes some text with ampersand and returns text w/o ampersand
468 # and position of the ampersand. Double ampersands are converted to single
469 # ones. Position returned is -1 when there is no ampersand.
471 proc ::tk::UnderlineAmpersand {text} {
472 set s
[string map
{&& & & \ufeff} $text]
473 set idx
[string first
\ufeff $s]
474 return [list [string map
{\ufeff {}} $s] $idx]
477 # ::tk::SetAmpText --
478 # Given widget path and text with "magic ampersands", sets -text and
479 # -underline options for the widget
481 proc ::tk::SetAmpText {widget
text} {
482 lassign
[UnderlineAmpersand
$text] newtext under
483 $widget configure
-text $newtext -underline $under
487 # Creates new widget, turning -text option into -text and -underline options,
488 # returned by ::tk::UnderlineAmpersand.
490 proc ::tk::AmpWidget {class path args
} {
492 foreach {opt val
} $args {
493 if {$opt eq
"-text"} {
494 lassign
[UnderlineAmpersand
$val] newtext under
495 lappend options -text $newtext -underline $under
497 lappend options $opt $val
500 set result
[$class $path {*}$options]
501 if {[string match
"*button" $class]} {
502 bind $path <<AltUnderlined
>> [list $path invoke
]
507 # ::tk::AmpMenuArgs --
508 # Processes arguments for a menu entry, turning -label option into -label and
509 # -underline options, returned by ::tk::UnderlineAmpersand.
511 proc ::tk::AmpMenuArgs {widget add type args
} {
513 foreach {opt val
} $args {
514 if {$opt eq
"-label"} {
515 lassign
[UnderlineAmpersand
$val] newlabel under
516 lappend options -label $newlabel -underline $under
518 lappend options $opt $val
521 $widget add
$type {*}$options
524 # ::tk::FindAltKeyTarget --
525 # Search recursively through the hierarchy of visible widgets to find button
526 # or label which has $char as underlined character
528 proc ::tk::FindAltKeyTarget {path char
} {
529 switch -- [winfo class
$path] {
531 TButton
- TLabel
- TCheckbutton
{
532 if {[string equal
-nocase $char \
533 [string index
[$path cget
-text] [$path cget
-underline]]]} {
540 foreach child
[concat [grid slaves
$path] \
541 [pack slaves
$path] [place slaves
$path]] {
542 set target
[FindAltKeyTarget
$child $char]
552 # ::tk::AltKeyInDialog --
553 # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>> to
554 # button or label which has appropriate underlined character
556 proc ::tk::AltKeyInDialog {path key
} {
557 set target
[FindAltKeyTarget
$path $key]
558 if { $target eq
""} return
559 event generate
$target <<AltUnderlined
>>
563 # Replacement for mcmax, used for texts with "magic ampersand" in it.
566 proc ::tk::mcmaxamp {args
} {
569 # Should we run [mc] in caller's namespace?
570 lassign
[UnderlineAmpersand
[mc
$arg]] msg
571 set length
[string length
$msg]
572 if {$length > $maxlen} {
579 # For now, turn off the custom mdef proc for the mac:
581 if {[tk windowingsystem
] eq
"aqua"} {
582 namespace eval ::tk::mac {
583 variable useCustomMDEF
0
587 # Run the Ttk themed widget set initialization
588 if {$::ttk::library ne
""} {
589 uplevel \#0 [list source [file join $::ttk::library ttk.tcl]]