# Initialization script normally executed in the interpreter for each
# Tk-based application. Arranges class bindings for widgets.
# RCS: @(#) $Id: tk.tcl,v 1.46.2.2 2004/10/29 11:16:37 patthoyts Exp $
# 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.
# Insist on running with compatible versions of Tcl and Tk.
package require
-exact Tk
8.4
package require
-exact Tcl 8.4
# Create a ::tk namespace
# Set up the msgcat commands
namespace export mc mcmax
if {[interp issafe
] ||
[catch {package require
msgcat}]} {
# The msgcat package is not available. Supply our own
return [eval [list format $src] $args]
set len
[string length
$string]
# Get the commands from the msgcat package that Tk uses.
namespace import
::msgcat::mc
namespace import
::msgcat::mcmax
::msgcat::mcload [file join $::tk_library msgs
]
namespace import
::tk::msgcat::*
# Add 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] && [string compare
{} $::tk_library] && \
[lsearch -exact $::auto_path $::tk_library] < 0} {
lappend ::auto_path $::tk_library
# Turn off strict Motif look and feel as a default.
# Turn on useinputmethods (X Input Methods) by default.
# We catch this because safe interpreters may not allow the call.
catch {tk useinputmethods
1}
# place a toplevel at a particular position
# toplevel name of toplevel window
# ?placement? pointer ?center? ; places $w centered on the pointer
# widget widgetPath ; centers $w over widget_name
# defaults to placing toplevel in the middle of the screen
# ?anchor? center or widgetPath
proc ::tk::PlaceWindow {w
{place ""} {anchor
""}} {
set x
[expr {([winfo screenwidth
$w]-[winfo reqwidth
$w])/2}]
set y
[expr {([winfo screenheight
$w]-[winfo reqheight
$w])/2}]
} elseif
{[string equal
-len [string length
$place] $place "pointer"]} {
## place at POINTER (centered if $anchor == center)
if {[string equal
-len [string length
$anchor] $anchor "center"]} {
set x
[expr {[winfo pointerx
$w]-[winfo reqwidth
$w]/2}]
set y
[expr {[winfo pointery
$w]-[winfo reqheight
$w]/2}]
set x
[winfo pointerx
$w]
set y
[winfo pointery
$w]
} elseif
{[string equal
-len [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] + \
([winfo width
$anchor]-[winfo reqwidth
$w])/2}]
set y
[expr {[winfo rooty
$anchor] + \
([winfo height
$anchor]-[winfo reqheight
$w])/2}]
set x
[expr {([winfo screenwidth
$w]-[winfo reqwidth
$w])/2}]
set y
[expr {([winfo screenheight
$w]-[winfo reqheight
$w])/2}]
if {[tk windowingsystem
] eq
"win32"} {
# Bug 533519: win32 multiple desktops may produce negative geometry.
} elseif
{$x > ([winfo screenwidth
$w]-[winfo reqwidth
$w])} {
set x
[expr {[winfo screenwidth
$w]-[winfo reqwidth
$w]}]
} elseif
{$y > ([winfo screenheight
$w]-[winfo reqheight
$w])} {
set y
[expr {[winfo screenheight
$w]-[winfo reqheight
$w]}]
if {[tk windowingsystem
] eq
"macintosh" \
||
[tk windowingsystem
] eq
"aqua"} {
# Avoid the native menu bar which sits on top of everything.
if {$y < 20} { set y
20 }
# swap out current focus and grab temporarily (for dialogs)
# grab new window to grab
# focus window to give focus to
proc ::tk::SetFocusGrab {grab {focus {}}} {
upvar ::tk::FocusGrab($index) data
set oldGrab
[grab current
$grab]
if {[winfo exists
$oldGrab]} {
lappend data
[grab status
$oldGrab]
# The "grab" command will fail if another application
# already holds the grab. So catch it.
if {[winfo exists
$focus]} {
# ::tk::RestoreFocusGrab --
# restore old focus and grab (for dialogs)
# grab window that had taken grab
# focus window that had taken focus
# destroy destroy|withdraw - how to handle the old grabbed window
proc ::tk::RestoreFocusGrab {grab focus {destroy destroy}} {
if {[info exists
::tk::FocusGrab($index)]} {
foreach {oldFocus oldGrab oldStatus
} $::tk::FocusGrab($index) { break }
unset ::tk::FocusGrab($index)
if {[string equal
$destroy "withdraw"]} {
if {[winfo exists
$oldGrab] && [winfo ismapped
$oldGrab]} {
if {[string equal
$oldStatus "global"]} {
# 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.
# w The widget for which the selection will be retrieved.
# Important for the -displayof property.
# sel The source of the selection (PRIMARY or CLIPBOARD)
# Returns the selection, or an error if none could be found
if {[string equal
$tcl_platform(platform
) "unix"]} {
proc ::tk::GetSelection {w
{sel PRIMARY
}} {
if {[catch {selection get
-displayof $w -selection $sel \
-type UTF8_STRING
} txt
] \
&& [catch {selection get
-displayof $w -selection $sel} txt
]} {
return -code error "could not find default selection"
proc ::tk::GetSelection {w
{sel PRIMARY
}} {
if {[catch {selection get
-displayof $w -selection $sel} txt
]} {
return -code error "could not find default selection"
# 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.
# screen - The name of the new screen.
proc ::tk::ScreenChanged screen
{
set x
[string last .
$screen]
set disp
[string range
$screen 0 [expr {$x - 1}]]
uplevel #0 upvar #0 ::tk::Priv.$disp ::tk::Priv
if {[info exists Priv
]} {
set Priv
(tearoff
) [string equal
[tk windowingsystem
] "x11"]
# Do initial setup for Priv, so that it is always bound to something
# (otherwise, if someone references it, it may get set to a non-upvar-ed
# 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
# n1 - the name of the variable being changed ("::tk_strictMotif").
proc ::tk::EventMotifBindings {n1 dummy dummy
} {
event $op <<Cut
>> <Control-Key-w
>
event $op <<Copy
>> <Meta-Key-w
>
event $op <<Paste
>> <Control-Key-y
>
event $op <<Undo
>> <Control-underscore
>
#----------------------------------------------------------------------
# Define common dialogs on platforms where they are not implemented
#----------------------------------------------------------------------
if {[string equal
[info commands
tk_chooseColor] ""]} {
proc ::tk_chooseColor {args
} {
return [eval tk::dialog::color:: $args]
if {[string equal
[info commands
tk_getOpenFile] ""]} {
proc ::tk_getOpenFile {args
} {
return [eval tk::MotifFDialog open $args]
return [eval ::tk::dialog::file:: open $args]
if {[string equal
[info commands
tk_getSaveFile] ""]} {
proc ::tk_getSaveFile {args
} {
return [eval tk::MotifFDialog save
$args]
return [eval ::tk::dialog::file:: save
$args]
if {[string equal
[info commands
tk_messageBox] ""]} {
proc ::tk_messageBox {args
} {
return [eval tk::MessageBox $args]
if {[string equal
[info command
tk_chooseDirectory] ""]} {
proc ::tk_chooseDirectory {args
} {
return [eval ::tk::dialog::file::chooseDir:: $args]
#----------------------------------------------------------------------
# Define the set of common virtual events.
#----------------------------------------------------------------------
switch [tk windowingsystem
] {
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
<<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
# 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 variable ::tk_strictMotif w
::tk::EventMotifBindings
set ::tk_strictMotif $::tk_strictMotif
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
<<PasteSelection
>> <ButtonRelease-2
>
event add
<<Undo
>> <Control-Key-z
>
event add
<<Redo
>> <Control-Key-y
>
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
<<PasteSelection
>> <ButtonRelease-2
>
event add
<<Clear
>> <Clear
>
event add
<<Undo
>> <Command-Key-z
>
event add
<<Redo
>> <Command-Key-y
>
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
>
# ----------------------------------------------------------------------
# Read in files that define all of the class bindings.
# ----------------------------------------------------------------------
if {$::tk_library ne
""} {
if {[string equal
$tcl_platform(platform
) "macintosh"]} {
proc ::tk::SourceLibFile {file} {
[list source [file join $::tk_library $file.tcl
]]
namespace eval :: [list source -rsrc $file]
proc ::tk::SourceLibFile {file} {
namespace eval :: [list source [file join $::tk_library $file.tcl
]]
SourceLibFile
panedwindow
# ----------------------------------------------------------------------
# Default bindings for keyboard traversal.
# ----------------------------------------------------------------------
event add
<<PrevWindow
>> <Shift-Tab
>
bind all
<Tab
> {tk::TabToWindow [tk_focusNext %W
]}
bind all
<<PrevWindow
>> {tk::TabToWindow [tk_focusPrev %W
]}
# 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
proc ::tk::CancelRepeat {} {
after cancel
$Priv(afterId
)
# 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.
# w - Window to which focus should be set.
proc ::tk::TabToWindow {w
} {
if {[string equal
[winfo class
$w] Entry
] \
||
[string equal
[winfo class
$w] Spinbox
]} {
# ::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.
proc ::tk::UnderlineAmpersand {text} {
set idx
[string first
"&" $text]
while {[string match
"&" [string index
$text [expr {$idx + 1}]]]} {
set base
[expr {$idx + 2}]
set idx
[string first
"&" [string range
$text $base end
]]
set underline
[expr {$underline + $idx + 1}]
regsub -all -- {&([^
&])} $text {\1} text
# 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
# 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]
foreach {opt val
} $args {
if {[string equal
$opt {-text}]} {
foreach {newtext under
} [::tk::UnderlineAmpersand $val] {
lappend wcmd
-text $newtext -underline $under
bind $path <<AltUnderlined
>> [list $path invoke
]
# ::tk::FindAltKeyTarget --
# 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] {
if {[string equal
-nocase $char \
[string index
[$path cget
-text] \
[$path cget
-underline]]]} {return $path} else {return {}}
[concat [grid slaves
$path] \
if {""!=[set target
[::tk::FindAltKeyTarget $child $char]]} {
# ::tk::AltKeyInDialog --
# <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]
if { $target == ""} return
event generate
$target <<AltUnderlined
>>
# Replacement for mcmax, used for texts with "magic ampersand" in it.
proc ::tk::mcmaxamp {args
} {
set length
[string length
[lindex [::tk::UnderlineAmpersand [mc
$arg]] 0]]
# For now, turn off the custom mdef proc for the mac:
if {[string equal
[tk windowingsystem
] "aqua"]} {
namespace eval ::tk::mac {