diff options
Diffstat (limited to 'library/tk.tcl')
-rw-r--r-- | library/tk.tcl | 364 |
1 files changed, 183 insertions, 181 deletions
diff --git a/library/tk.tcl b/library/tk.tcl index b9c3872..8e967cb 100644 --- a/library/tk.tcl +++ b/library/tk.tcl @@ -1,18 +1,21 @@ # tk.tcl -- # -# Initialization script normally executed in the interpreter for each -# Tk-based application. Arranges class bindings for widgets. +# Initialization script normally executed in the interpreter for each Tk-based +# application. Arranges class bindings for widgets. # # Copyright (c) 1992-1994 The Regents of the University of California. # Copyright (c) 1994-1996 Sun Microsystems, Inc. # Copyright (c) 1998-2000 Ajuba Solutions. # -# See the file "license.terms" for information on usage and redistribution -# of this file, and for a DISCLAIMER OF ALL WARRANTIES. +# See the file "license.terms" for information on usage and redistribution of +# this file, and for a DISCLAIMER OF ALL WARRANTIES. -# Insist on running with compatible versions of Tcl and Tk. -package require -exact Tk 8.4 -package require -exact Tcl 8.4 +package require Tcl 8.5 ;# Guard against [source] in an 8.4- interp before + ;# using 8.5 [package] features. +# Insist on running with compatible version of Tcl +package require Tcl 8.5.0 +# Verify that we have Tk binary and script components from the same release +package require -exact Tk 8.5.11 # Create a ::tk namespace namespace eval ::tk { @@ -20,10 +23,10 @@ namespace eval ::tk { namespace eval msgcat { namespace export mc mcmax if {[interp issafe] || [catch {package require msgcat}]} { - # The msgcat package is not available. Supply our own - # minimal replacement. + # The msgcat package is not available. Supply our own minimal + # replacement. proc mc {src args} { - return [eval [list format $src] $args] + return [format $src {*}$args] } proc mcmax {args} { set max 0 @@ -44,26 +47,33 @@ namespace eval ::tk { } namespace import ::tk::msgcat::* } +# and a ::ttk namespace +namespace eval ::ttk { + if {$::tk_library ne ""} { + # avoid file join to work in safe interps, but this is also x-plat ok + variable library $::tk_library/ttk + } +} -# Add Tk's directory to the end of the auto-load search path, if it +# Add Ttk & Tk's directory to the end of the auto-load search path, if it # isn't already on the path: -if {[info exists ::auto_path] && $::tk_library ne "" && \ - [lsearch -exact $::auto_path $::tk_library] < 0} { - lappend ::auto_path $::tk_library +if {[info exists ::auto_path] && ($::tk_library ne "") + && ($::tk_library ni $::auto_path)} { + lappend ::auto_path $::tk_library $::ttk::library } # Turn off strict Motif look and feel as a default. set ::tk_strictMotif 0 -# Turn on useinputmethods (X Input Methods) by default. -# We catch this because safe interpreters may not allow the call. +# Turn on useinputmethods (X Input Methods) by default. We catch this because +# safe interpreters may not allow the call. catch {tk useinputmethods 1} - + # ::tk::PlaceWindow -- -# place a toplevel at a particular position +# Place a toplevel at a particular position # Arguments: # toplevel name of toplevel window # ?placement? pointer ?center? ; places $w centered on the pointer @@ -77,12 +87,11 @@ proc ::tk::PlaceWindow {w {place ""} {anchor ""}} { wm withdraw $w update idletasks set checkBounds 1 - set place_len [string length $place] if {$place eq ""} { set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}] set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}] set checkBounds 0 - } elseif {[string equal -length $place_len $place "pointer"]} { + } elseif {[string equal -length [string length $place] $place "pointer"]} { ## place at POINTER (centered if $anchor == center) if {[string equal -length [string length $anchor] $anchor "center"]} { set x [expr {[winfo pointerx $w]-[winfo reqwidth $w]/2}] @@ -91,7 +100,7 @@ proc ::tk::PlaceWindow {w {place ""} {anchor ""}} { set x [winfo pointerx $w] set y [winfo pointery $w] } - } elseif {[string equal -length $place_len $place "widget"] && \ + } elseif {[string equal -length [string length $place] $place "widget"] && \ [winfo exists $anchor] && [winfo ismapped $anchor]} { ## center about WIDGET $anchor, widget must be mapped set x [expr {[winfo rootx $anchor] + \ @@ -103,10 +112,7 @@ proc ::tk::PlaceWindow {w {place ""} {anchor ""}} { set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}] set checkBounds 0 } - - set windowingsystem [tk windowingsystem] - - if {$windowingsystem eq "win32"} { + if {[tk windowingsystem] eq "win32"} { # Bug 533519: win32 multiple desktops may produce negative geometry. set checkBounds 0 } @@ -121,7 +127,7 @@ proc ::tk::PlaceWindow {w {place ""} {anchor ""}} { } elseif {$y > ([winfo screenheight $w]-[winfo reqheight $w])} { set y [expr {[winfo screenheight $w]-[winfo reqheight $w]}] } - if {$windowingsystem eq "classic" || $windowingsystem eq "aqua"} { + if {[tk windowingsystem] eq "aqua"} { # Avoid the native menu bar which sits on top of everything. if {$y < 22} { set y 22 } } @@ -129,9 +135,9 @@ proc ::tk::PlaceWindow {w {place ""} {anchor ""}} { wm geometry $w +$x+$y wm deiconify $w } - + # ::tk::SetFocusGrab -- -# swap out current focus and grab temporarily (for dialogs) +# Swap out current focus and grab temporarily (for dialogs) # Arguments: # grab new window to grab # focus window to give focus to @@ -148,8 +154,8 @@ proc ::tk::SetFocusGrab {grab {focus {}}} { if {[winfo exists $oldGrab]} { lappend data [grab status $oldGrab] } - # The "grab" command will fail if another application - # already holds the grab. So catch it. + # The "grab" command will fail if another application already holds the + # grab. So catch it. catch {grab $grab} if {[winfo exists $focus]} { focus $focus @@ -157,7 +163,7 @@ proc ::tk::SetFocusGrab {grab {focus {}}} { } # ::tk::RestoreFocusGrab -- -# restore old focus and grab (for dialogs) +# Restore old focus and grab (for dialogs) # Arguments: # grab window that had taken grab # focus window that had taken focus @@ -189,12 +195,12 @@ proc ::tk::RestoreFocusGrab {grab focus {destroy destroy}} { } } } - + # ::tk::GetSelection -- -# This tries to obtain the default selection. On Unix, we first try -# and get a UTF8_STRING, a type supported by modern Unix apps for -# passing Unicode data safely. We fall back on the default STRING -# type otherwise. On Windows, only the STRING type is necessary. +# This tries to obtain the default selection. On Unix, we first try and get +# a UTF8_STRING, a type supported by modern Unix apps for passing Unicode +# data safely. We fall back on the default STRING type otherwise. On +# Windows, only the STRING type is necessary. # Arguments: # w The widget for which the selection will be retrieved. # Important for the -displayof property. @@ -221,18 +227,18 @@ if {$tcl_platform(platform) eq "unix"} { } } } - + # ::tk::ScreenChanged -- -# This procedure is invoked by the binding mechanism whenever the -# "current" screen is changing. The procedure does two things. -# First, it uses "upvar" to make variable "::tk::Priv" point at an -# array variable that holds state for the current display. Second, -# it initializes the array if it didn't already exist. +# This procedure is invoked by the binding mechanism whenever the "current" +# screen is changing. The procedure does two things. First, it uses "upvar" +# to make variable "::tk::Priv" point at an array variable that holds state +# for the current display. Second, it initializes the array if it didn't +# already exist. # # Arguments: # screen - The name of the new screen. -proc ::tk::ScreenChanged screen { +proc ::tk::ScreenChanged {screen} { set x [string last . $screen] if {$x > 0} { set disp [string range $screen 0 [expr {$x - 1}]] @@ -245,7 +251,7 @@ proc ::tk::ScreenChanged screen { # display names. [Bug 2912473] set disp [string map {:: _doublecolon_} $disp] - uplevel #0 upvar #0 ::tk::Priv.$disp ::tk::Priv + uplevel #0 [list upvar #0 ::tk::Priv.$disp ::tk::Priv] variable ::tk::Priv global tcl_platform @@ -285,11 +291,10 @@ proc ::tk::ScreenChanged screen { # value, which will cause trouble later). tk::ScreenChanged [winfo screen .] - + # ::tk::EventMotifBindings -- -# This procedure is invoked as a trace whenever ::tk_strictMotif is -# changed. It is used to turn on or turn off the motif virtual -# bindings. +# This procedure is invoked as a trace whenever ::tk_strictMotif is changed. +# It is used to turn on or turn off the motif virtual bindings. # # Arguments: # n1 - the name of the variable being changed ("::tk_strictMotif"). @@ -308,119 +313,102 @@ proc ::tk::EventMotifBindings {n1 dummy dummy} { event $op <<Paste>> <Control-Key-y> event $op <<Undo>> <Control-underscore> } - + #---------------------------------------------------------------------- -# Define common dialogs on platforms where they are not implemented -# using compiled code. +# Define common dialogs on platforms where they are not implemented using +# compiled code. #---------------------------------------------------------------------- -if {[info commands tk_chooseColor] eq ""} { +if {![llength [info commands tk_chooseColor]]} { proc ::tk_chooseColor {args} { - return [eval tk::dialog::color:: $args] + return [tk::dialog::color:: {*}$args] } } -if {[info commands tk_getOpenFile] eq ""} { +if {![llength [info commands tk_getOpenFile]]} { proc ::tk_getOpenFile {args} { if {$::tk_strictMotif} { - return [eval tk::MotifFDialog open $args] + return [tk::MotifFDialog open {*}$args] } else { - return [eval ::tk::dialog::file:: open $args] + return [::tk::dialog::file:: open {*}$args] } } } -if {[info commands tk_getSaveFile] eq ""} { +if {![llength [info commands tk_getSaveFile]]} { proc ::tk_getSaveFile {args} { if {$::tk_strictMotif} { - return [eval tk::MotifFDialog save $args] + return [tk::MotifFDialog save {*}$args] } else { - return [eval ::tk::dialog::file:: save $args] + return [::tk::dialog::file:: save {*}$args] } } } -if {[info commands tk_messageBox] eq ""} { +if {![llength [info commands tk_messageBox]]} { proc ::tk_messageBox {args} { - return [eval tk::MessageBox $args] + return [tk::MessageBox {*}$args] } } -if {[info command tk_chooseDirectory] eq ""} { +if {![llength [info command tk_chooseDirectory]]} { proc ::tk_chooseDirectory {args} { - return [eval ::tk::dialog::file::chooseDir:: $args] + return [::tk::dialog::file::chooseDir:: {*}$args] } } - + #---------------------------------------------------------------------- # Define the set of common virtual events. #---------------------------------------------------------------------- -switch [tk windowingsystem] { +switch -exact -- [tk windowingsystem] { "x11" { - event add <<Cut>> <Control-Key-x> <Key-F20> - event add <<Copy>> <Control-Key-c> <Key-F16> - event add <<Paste>> <Control-Key-v> <Key-F18> + event add <<Cut>> <Control-Key-x> <Key-F20> <Control-Lock-Key-X> + event add <<Copy>> <Control-Key-c> <Key-F16> <Control-Lock-Key-C> + event add <<Paste>> <Control-Key-v> <Key-F18> <Control-Lock-Key-V> event add <<PasteSelection>> <ButtonRelease-2> - event add <<Undo>> <Control-Key-z> - event add <<Redo>> <Control-Key-Z> - # Some OS's define a goofy (as in, not <Shift-Tab>) keysym - # that is returned when the user presses <Shift-Tab>. In order for - # tab traversal to work, we have to add these keysyms to the - # PrevWindow event. - # We use catch just in case the keysym isn't recognized. - # This is needed for XFree86 systems + event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z> + event add <<Redo>> <Control-Key-Z> <Control-Lock-Key-z> + # Some OS's define a goofy (as in, not <Shift-Tab>) keysym that is + # returned when the user presses <Shift-Tab>. In order for tab + # traversal to work, we have to add these keysyms to the PrevWindow + # event. We use catch just in case the keysym isn't recognized. This + # is needed for XFree86 systems catch { event add <<PrevWindow>> <ISO_Left_Tab> } # This seems to be correct on *some* HP systems. catch { event add <<PrevWindow>> <hpBackTab> } trace add variable ::tk_strictMotif write ::tk::EventMotifBindings set ::tk_strictMotif $::tk_strictMotif - # On unix, we want to always display entry/text selection, - # regardless of which window has focus + # On unix, we want to always display entry/text selection, regardless + # of which window has focus set ::tk::AlwaysShowSelection 1 } "win32" { - event add <<Cut>> <Control-Key-x> <Shift-Key-Delete> - event add <<Copy>> <Control-Key-c> <Control-Key-Insert> - event add <<Paste>> <Control-Key-v> <Shift-Key-Insert> + event add <<Cut>> <Control-Key-x> <Shift-Key-Delete> \ + <Control-Lock-Key-X> + event add <<Copy>> <Control-Key-c> <Control-Key-Insert> \ + <Control-Lock-Key-C> + event add <<Paste>> <Control-Key-v> <Shift-Key-Insert> \ + <Control-Lock-Key-V> event add <<PasteSelection>> <ButtonRelease-2> - event add <<Undo>> <Control-Key-z> - event add <<Redo>> <Control-Key-y> + event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z> + event add <<Redo>> <Control-Key-y> <Control-Lock-Key-Y> } "aqua" { - event add <<Cut>> <Command-Key-x> <Key-F2> - event add <<Copy>> <Command-Key-c> <Key-F3> - event add <<Paste>> <Command-Key-v> <Key-F4> + event add <<Cut>> <Command-Key-x> <Key-F2> <Control-Lock-Key-X> + event add <<Copy>> <Command-Key-c> <Key-F3> <Control-Lock-Key-C> + event add <<Paste>> <Command-Key-v> <Key-F4> <Control-Lock-Key-V> event add <<PasteSelection>> <ButtonRelease-2> event add <<Clear>> <Clear> - event add <<Undo>> <Command-Key-z> - event add <<Redo>> <Command-Key-y> - } - "classic" { - event add <<Cut>> <Control-Key-x> <Key-F2> - event add <<Copy>> <Control-Key-c> <Key-F3> - event add <<Paste>> <Control-Key-v> <Key-F4> - event add <<PasteSelection>> <ButtonRelease-2> - event add <<Clear>> <Clear> - event add <<Undo>> <Control-Key-z> <Key-F1> - event add <<Redo>> <Control-Key-Z> + event add <<Undo>> <Command-Key-z> <Control-Lock-Key-Z> + event add <<Redo>> <Command-Key-y> <Control-Lock-Key-Y> } } + # ---------------------------------------------------------------------- # Read in files that define all of the class bindings. # ---------------------------------------------------------------------- if {$::tk_library ne ""} { - if {$tcl_platform(platform) eq "macintosh"} { - proc ::tk::SourceLibFile {file} { - if {[catch { - namespace eval :: \ - [list source [file join $::tk_library $file.tcl]] - }]} { - namespace eval :: [list source -rsrc $file] - } - } - } else { - proc ::tk::SourceLibFile {file} { - namespace eval :: [list source [file join $::tk_library $file.tcl]] - } + proc ::tk::SourceLibFile {file} { + namespace eval :: [list source [file join $::tk_library $file.tcl]] } namespace eval ::tk { SourceLibFile button @@ -434,6 +422,7 @@ if {$::tk_library ne ""} { SourceLibFile text } } + # ---------------------------------------------------------------------- # Default bindings for keyboard traversal. # ---------------------------------------------------------------------- @@ -441,12 +430,11 @@ if {$::tk_library ne ""} { event add <<PrevWindow>> <Shift-Tab> bind all <Tab> {tk::TabToWindow [tk_focusNext %W]} bind all <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]} - + # ::tk::CancelRepeat -- -# This procedure is invoked to cancel an auto-repeat action described -# by ::tk::Priv(afterId). It's used by several widgets to auto-scroll -# the widget when the mouse is dragged out of the widget with a -# button pressed. +# This procedure is invoked to cancel an auto-repeat action described by +# ::tk::Priv(afterId). It's used by several widgets to auto-scroll the widget +# when the mouse is dragged out of the widget with a button pressed. # # Arguments: # None. @@ -456,102 +444,103 @@ proc ::tk::CancelRepeat {} { after cancel $Priv(afterId) set Priv(afterId) {} } - + # ::tk::TabToWindow -- -# This procedure moves the focus to the given widget. If the widget -# is an entry or a spinbox, it selects the entire contents of the widget. +# This procedure moves the focus to the given widget. +# It sends a <<TraverseOut>> virtual event to the previous focus window, if +# any, before changing the focus, and a <<TraverseIn>> event to the new focus +# window afterwards. # # Arguments: # w - Window to which focus should be set. proc ::tk::TabToWindow {w} { - set wclass [winfo class $w] - - if {$wclass eq "Entry" || $wclass eq "Spinbox"} { - $w selection range 0 end - $w icursor end + set focus [focus] + if {$focus ne ""} { + event generate $focus <<TraverseOut>> } focus $w + event generate $w <<TraverseIn>> } - + # ::tk::UnderlineAmpersand -- -# This procedure takes some text with ampersand and returns -# text w/o ampersand and position of the ampersand. -# Double ampersands are converted to single ones. -# Position returned is -1 when there is no ampersand. +# This procedure takes some text with ampersand and returns text w/o ampersand +# and position of the ampersand. Double ampersands are converted to single +# ones. Position returned is -1 when there is no ampersand. # proc ::tk::UnderlineAmpersand {text} { - set idx [string first "&" $text] - if {$idx >= 0} { - set underline $idx - # ignore "&&" - while {[string match "&" [string index $text [expr {$idx + 1}]]]} { - set base [expr {$idx + 2}] - set idx [string first "&" [string range $text $base end]] - if {$idx < 0} { - break - } else { - set underline [expr {$underline + $idx + 1}] - incr idx $base - } - } - } - if {$idx >= 0} { - regsub -all -- {&([^&])} $text {\1} text - } - return [list $text $idx] + set s [string map {&& & & \ufeff} $text] + set idx [string first \ufeff $s] + return [list [string map {\ufeff {}} $s] $idx] } # ::tk::SetAmpText -- -# Given widget path and text with "magic ampersands", -# sets -text and -underline options for the widget +# Given widget path and text with "magic ampersands", sets -text and +# -underline options for the widget # proc ::tk::SetAmpText {widget text} { - foreach {newtext under} [::tk::UnderlineAmpersand $text] { - $widget configure -text $newtext -underline $under - } + lassign [UnderlineAmpersand $text] newtext under + $widget configure -text $newtext -underline $under } # ::tk::AmpWidget -- -# Creates new widget, turning -text option into -text and -# -underline options, returned by ::tk::UnderlineAmpersand. +# Creates new widget, turning -text option into -text and -underline options, +# returned by ::tk::UnderlineAmpersand. # proc ::tk::AmpWidget {class path args} { - set wcmd [list $class $path] + set options {} foreach {opt val} $args { if {$opt eq "-text"} { - foreach {newtext under} [::tk::UnderlineAmpersand $val] { - lappend wcmd -text $newtext -underline $under - } + lassign [UnderlineAmpersand $val] newtext under + lappend options -text $newtext -underline $under } else { - lappend wcmd $opt $val + lappend options $opt $val } } - eval $wcmd - if {$class eq "button"} { + set result [$class $path {*}$options] + if {[string match "*button" $class]} { bind $path <<AltUnderlined>> [list $path invoke] } - return $path + return $result } +# ::tk::AmpMenuArgs -- +# Processes arguments for a menu entry, turning -label option into -label and +# -underline options, returned by ::tk::UnderlineAmpersand. +# +proc ::tk::AmpMenuArgs {widget add type args} { + set options {} + foreach {opt val} $args { + if {$opt eq "-label"} { + lassign [UnderlineAmpersand $val] newlabel under + lappend options -label $newlabel -underline $under + } else { + lappend options $opt $val + } + } + $widget add $type {*}$options +} + # ::tk::FindAltKeyTarget -- -# search recursively through the hierarchy of visible widgets -# to find button or label which has $char as underlined character +# Search recursively through the hierarchy of visible widgets to find button +# or label which has $char as underlined character # proc ::tk::FindAltKeyTarget {path char} { - switch [winfo class $path] { - Button - - Label { + switch -- [winfo class $path] { + Button - Label - + TButton - TLabel - TCheckbutton { if {[string equal -nocase $char \ - [string index [$path cget -text] \ - [$path cget -underline]]]} {return $path} else {return {}} + [string index [$path cget -text] [$path cget -underline]]]} { + return $path + } else { + return {} + } } default { - foreach child \ - [concat [grid slaves $path] \ - [pack slaves $path] \ - [place slaves $path] ] { - if {"" ne [set target [::tk::FindAltKeyTarget $child $char]]} { + foreach child [concat [grid slaves $path] \ + [pack slaves $path] [place slaves $path]] { + set target [FindAltKeyTarget $child $char] + if {$target ne ""} { return $target } } @@ -561,15 +550,15 @@ proc ::tk::FindAltKeyTarget {path char} { } # ::tk::AltKeyInDialog -- -# <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>> -# to button or label which has appropriate underlined character +# <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>> to +# button or label which has appropriate underlined character # proc ::tk::AltKeyInDialog {path key} { - set target [::tk::FindAltKeyTarget $path $key] + set target [FindAltKeyTarget $path $key] if { $target eq ""} return event generate $target <<AltUnderlined>> } - + # ::tk::mcmaxamp -- # Replacement for mcmax, used for texts with "magic ampersand" in it. # @@ -577,17 +566,30 @@ proc ::tk::AltKeyInDialog {path key} { proc ::tk::mcmaxamp {args} { set maxlen 0 foreach arg $args { - set length [string length [lindex [::tk::UnderlineAmpersand [mc $arg]] 0]] - if {$length>$maxlen} { + # Should we run [mc] in caller's namespace? + lassign [UnderlineAmpersand [mc $arg]] msg + set length [string length $msg] + if {$length > $maxlen} { set maxlen $length } } return $maxlen } + # For now, turn off the custom mdef proc for the mac: if {[tk windowingsystem] eq "aqua"} { namespace eval ::tk::mac { - set useCustomMDEF 0 + variable useCustomMDEF 0 } } + +# Run the Ttk themed widget set initialization +if {$::ttk::library ne ""} { + uplevel \#0 [list source [file join $::ttk::library ttk.tcl]] +} + +# Local Variables: +# mode: tcl +# fill-column: 78 +# End: |