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.19
  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. }
  287. #----------------------------------------------------------------------
  288. # Define common dialogs on platforms where they are not implemented using
  289. # compiled code.
  290. #----------------------------------------------------------------------
  291. if {![llength [info commands tk_chooseColor]]} {
  292. proc ::tk_chooseColor {args} {
  293. return [tk::dialog::color:: {*}$args]
  294. }
  295. }
  296. if {![llength [info commands tk_getOpenFile]]} {
  297. proc ::tk_getOpenFile {args} {
  298. if {$::tk_strictMotif} {
  299. return [tk::MotifFDialog open {*}$args]
  300. } else {
  301. return [::tk::dialog::file:: open {*}$args]
  302. }
  303. }
  304. }
  305. if {![llength [info commands tk_getSaveFile]]} {
  306. proc ::tk_getSaveFile {args} {
  307. if {$::tk_strictMotif} {
  308. return [tk::MotifFDialog save {*}$args]
  309. } else {
  310. return [::tk::dialog::file:: save {*}$args]
  311. }
  312. }
  313. }
  314. if {![llength [info commands tk_messageBox]]} {
  315. proc ::tk_messageBox {args} {
  316. return [tk::MessageBox {*}$args]
  317. }
  318. }
  319. if {![llength [info command tk_chooseDirectory]]} {
  320. proc ::tk_chooseDirectory {args} {
  321. return [::tk::dialog::file::chooseDir:: {*}$args]
  322. }
  323. }
  324. #----------------------------------------------------------------------
  325. # Define the set of common virtual events.
  326. #----------------------------------------------------------------------
  327. switch -exact -- [tk windowingsystem] {
  328. "x11" {
  329. event add <<Cut>> <Control-Key-x> <Key-F20> <Control-Lock-Key-X>
  330. event add <<Copy>> <Control-Key-c> <Key-F16> <Control-Lock-Key-C>
  331. event add <<Paste>> <Control-Key-v> <Key-F18> <Control-Lock-Key-V>
  332. event add <<PasteSelection>> <ButtonRelease-2>
  333. event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z>
  334. event add <<Redo>> <Control-Key-Z> <Control-Lock-Key-z>
  335. # Some OS's define a goofy (as in, not <Shift-Tab>) keysym that is
  336. # returned when the user presses <Shift-Tab>. In order for tab
  337. # traversal to work, we have to add these keysyms to the PrevWindow
  338. # event. We use catch just in case the keysym isn't recognized. This
  339. # is needed for XFree86 systems
  340. catch { event add <<PrevWindow>> <ISO_Left_Tab> }
  341. # This seems to be correct on *some* HP systems.
  342. catch { event add <<PrevWindow>> <hpBackTab> }
  343. trace add variable ::tk_strictMotif write ::tk::EventMotifBindings
  344. set ::tk_strictMotif $::tk_strictMotif
  345. # On unix, we want to always display entry/text selection, regardless
  346. # of which window has focus
  347. set ::tk::AlwaysShowSelection 1
  348. }
  349. "win32" {
  350. event add <<Cut>> <Control-Key-x> <Shift-Key-Delete> \
  351. <Control-Lock-Key-X>
  352. event add <<Copy>> <Control-Key-c> <Control-Key-Insert> \
  353. <Control-Lock-Key-C>
  354. event add <<Paste>> <Control-Key-v> <Shift-Key-Insert> \
  355. <Control-Lock-Key-V>
  356. event add <<PasteSelection>> <ButtonRelease-2>
  357. event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z>
  358. event add <<Redo>> <Control-Key-y> <Control-Lock-Key-Y>
  359. }
  360. "aqua" {
  361. event add <<Cut>> <Command-Key-x> <Key-F2> <Control-Lock-Key-X>
  362. event add <<Copy>> <Command-Key-c> <Key-F3> <Control-Lock-Key-C>
  363. event add <<Paste>> <Command-Key-v> <Key-F4> <Control-Lock-Key-V>
  364. event add <<PasteSelection>> <ButtonRelease-2>
  365. event add <<Clear>> <Clear>
  366. event add <<Undo>> <Command-Key-z> <Control-Lock-Key-Z>
  367. event add <<Redo>> <Command-Key-y> <Control-Lock-Key-Y>
  368. }
  369. }
  370. # ----------------------------------------------------------------------
  371. # Read in files that define all of the class bindings.
  372. # ----------------------------------------------------------------------
  373. if {$::tk_library ne ""} {
  374. proc ::tk::SourceLibFile {file} {
  375. namespace eval :: [list source [file join $::tk_library $file.tcl]]
  376. }
  377. namespace eval ::tk {
  378. SourceLibFile button
  379. SourceLibFile entry
  380. SourceLibFile listbox
  381. SourceLibFile menu
  382. SourceLibFile panedwindow
  383. SourceLibFile scale
  384. SourceLibFile scrlbar
  385. SourceLibFile spinbox
  386. SourceLibFile text
  387. }
  388. }
  389. # ----------------------------------------------------------------------
  390. # Default bindings for keyboard traversal.
  391. # ----------------------------------------------------------------------
  392. event add <<PrevWindow>> <Shift-Tab>
  393. bind all <Tab> {tk::TabToWindow [tk_focusNext %W]}
  394. bind all <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]}
  395. # ::tk::CancelRepeat --
  396. # This procedure is invoked to cancel an auto-repeat action described by
  397. # ::tk::Priv(afterId). It's used by several widgets to auto-scroll the widget
  398. # when the mouse is dragged out of the widget with a button pressed.
  399. #
  400. # Arguments:
  401. # None.
  402. proc ::tk::CancelRepeat {} {
  403. variable ::tk::Priv
  404. after cancel $Priv(afterId)
  405. set Priv(afterId) {}
  406. }
  407. # ::tk::TabToWindow --
  408. # This procedure moves the focus to the given widget.
  409. # It sends a <<TraverseOut>> virtual event to the previous focus window, if
  410. # any, before changing the focus, and a <<TraverseIn>> event to the new focus
  411. # window afterwards.
  412. #
  413. # Arguments:
  414. # w - Window to which focus should be set.
  415. proc ::tk::TabToWindow {w} {
  416. set focus [focus]
  417. if {$focus ne ""} {
  418. event generate $focus <<TraverseOut>>
  419. }
  420. focus $w
  421. event generate $w <<TraverseIn>>
  422. }
  423. # ::tk::UnderlineAmpersand --
  424. # This procedure takes some text with ampersand and returns text w/o ampersand
  425. # and position of the ampersand. Double ampersands are converted to single
  426. # ones. Position returned is -1 when there is no ampersand.
  427. #
  428. proc ::tk::UnderlineAmpersand {text} {
  429. set s [string map {&& & & \ufeff} $text]
  430. set idx [string first \ufeff $s]
  431. return [list [string map {\ufeff {}} $s] $idx]
  432. }
  433. # ::tk::SetAmpText --
  434. # Given widget path and text with "magic ampersands", sets -text and
  435. # -underline options for the widget
  436. #
  437. proc ::tk::SetAmpText {widget text} {
  438. lassign [UnderlineAmpersand $text] newtext under
  439. $widget configure -text $newtext -underline $under
  440. }
  441. # ::tk::AmpWidget --
  442. # Creates new widget, turning -text option into -text and -underline options,
  443. # returned by ::tk::UnderlineAmpersand.
  444. #
  445. proc ::tk::AmpWidget {class path args} {
  446. set options {}
  447. foreach {opt val} $args {
  448. if {$opt eq "-text"} {
  449. lassign [UnderlineAmpersand $val] newtext under
  450. lappend options -text $newtext -underline $under
  451. } else {
  452. lappend options $opt $val
  453. }
  454. }
  455. set result [$class $path {*}$options]
  456. if {[string match "*button" $class]} {
  457. bind $path <<AltUnderlined>> [list $path invoke]
  458. }
  459. return $result
  460. }
  461. # ::tk::AmpMenuArgs --
  462. # Processes arguments for a menu entry, turning -label option into -label and
  463. # -underline options, returned by ::tk::UnderlineAmpersand.
  464. #
  465. proc ::tk::AmpMenuArgs {widget add type args} {
  466. set options {}
  467. foreach {opt val} $args {
  468. if {$opt eq "-label"} {
  469. lassign [UnderlineAmpersand $val] newlabel under
  470. lappend options -label $newlabel -underline $under
  471. } else {
  472. lappend options $opt $val
  473. }
  474. }
  475. $widget add $type {*}$options
  476. }
  477. # ::tk::FindAltKeyTarget --
  478. # Search recursively through the hierarchy of visible widgets to find button
  479. # or label which has $char as underlined character
  480. #
  481. proc ::tk::FindAltKeyTarget {path char} {
  482. switch -- [winfo class $path] {
  483. Button - Label -
  484. TButton - TLabel - TCheckbutton {
  485. if {[string equal -nocase $char \
  486. [string index [$path cget -text] [$path cget -underline]]]} {
  487. return $path
  488. } else {
  489. return {}
  490. }
  491. }
  492. default {
  493. foreach child [concat [grid slaves $path] \
  494. [pack slaves $path] [place slaves $path]] {
  495. set target [FindAltKeyTarget $child $char]
  496. if {$target ne ""} {
  497. return $target
  498. }
  499. }
  500. }
  501. }
  502. return {}
  503. }
  504. # ::tk::AltKeyInDialog --
  505. # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>> to
  506. # button or label which has appropriate underlined character
  507. #
  508. proc ::tk::AltKeyInDialog {path key} {
  509. set target [FindAltKeyTarget $path $key]
  510. if { $target eq ""} return
  511. event generate $target <<AltUnderlined>>
  512. }
  513. # ::tk::mcmaxamp --
  514. # Replacement for mcmax, used for texts with "magic ampersand" in it.
  515. #
  516. proc ::tk::mcmaxamp {args} {
  517. set maxlen 0
  518. foreach arg $args {
  519. # Should we run [mc] in caller's namespace?
  520. lassign [UnderlineAmpersand [mc $arg]] msg
  521. set length [string length $msg]
  522. if {$length > $maxlen} {
  523. set maxlen $length
  524. }
  525. }
  526. return $maxlen
  527. }
  528. # For now, turn off the custom mdef proc for the mac:
  529. if {[tk windowingsystem] eq "aqua"} {
  530. namespace eval ::tk::mac {
  531. variable useCustomMDEF 0
  532. }
  533. }
  534. # Run the Ttk themed widget set initialization
  535. if {$::ttk::library ne ""} {
  536. uplevel \#0 [list source [file join $::ttk::library ttk.tcl]]
  537. }
  538. # Local Variables:
  539. # mode: tcl
  540. # fill-column: 78
  541. # End: