tk.tcl 18 KB

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