tk.tcl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. # Verify that we have Tk binary and script components from the same release
  13. package require -exact Tk 8.6.11
  14. # Create a ::tk namespace
  15. namespace eval ::tk {
  16. # Set up the msgcat commands
  17. namespace eval msgcat {
  18. namespace export mc mcmax
  19. if {[interp issafe] || [catch {package require msgcat}]} {
  20. # The msgcat package is not available. Supply our own
  21. # minimal replacement.
  22. proc mc {src args} {
  23. return [format $src {*}$args]
  24. }
  25. proc mcmax {args} {
  26. set max 0
  27. foreach string $args {
  28. set len [string length $string]
  29. if {$len>$max} {
  30. set max $len
  31. }
  32. }
  33. return $max
  34. }
  35. } else {
  36. # Get the commands from the msgcat package that Tk uses.
  37. namespace import ::msgcat::mc
  38. namespace import ::msgcat::mcmax
  39. ::msgcat::mcload [file join $::tk_library msgs]
  40. }
  41. }
  42. namespace import ::tk::msgcat::*
  43. }
  44. # and a ::ttk namespace
  45. namespace eval ::ttk {
  46. if {$::tk_library ne ""} {
  47. # avoid file join to work in safe interps, but this is also x-plat ok
  48. variable library $::tk_library/ttk
  49. }
  50. }
  51. # Add Ttk & Tk's directory to the end of the auto-load search path, if it
  52. # isn't already on the path:
  53. if {[info exists ::auto_path] && ($::tk_library ne "")
  54. && ($::tk_library ni $::auto_path)
  55. } then {
  56. lappend ::auto_path $::tk_library $::ttk::library
  57. }
  58. # Turn off strict Motif look and feel as a default.
  59. set ::tk_strictMotif 0
  60. # Turn on useinputmethods (X Input Methods) by default.
  61. # We catch this because safe interpreters may not allow the call.
  62. catch {tk useinputmethods 1}
  63. # ::tk::PlaceWindow --
  64. # place a toplevel at a particular position
  65. # Arguments:
  66. # toplevel name of toplevel window
  67. # ?placement? pointer ?center? ; places $w centered on the pointer
  68. # widget widgetPath ; centers $w over widget_name
  69. # defaults to placing toplevel in the middle of the screen
  70. # ?anchor? center or widgetPath
  71. # Results:
  72. # Returns nothing
  73. #
  74. proc ::tk::PlaceWindow {w {place ""} {anchor ""}} {
  75. wm withdraw $w
  76. update idletasks
  77. set checkBounds 1
  78. if {$place eq ""} {
  79. set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}]
  80. set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}]
  81. set checkBounds 0
  82. } elseif {[string equal -length [string length $place] $place "pointer"]} {
  83. ## place at POINTER (centered if $anchor == center)
  84. if {[string equal -length [string length $anchor] $anchor "center"]} {
  85. set x [expr {[winfo pointerx $w]-[winfo reqwidth $w]/2}]
  86. set y [expr {[winfo pointery $w]-[winfo reqheight $w]/2}]
  87. } else {
  88. set x [winfo pointerx $w]
  89. set y [winfo pointery $w]
  90. }
  91. } elseif {[string equal -length [string length $place] $place "widget"] && \
  92. [winfo exists $anchor] && [winfo ismapped $anchor]} {
  93. ## center about WIDGET $anchor, widget must be mapped
  94. set x [expr {[winfo rootx $anchor] + \
  95. ([winfo width $anchor]-[winfo reqwidth $w])/2}]
  96. set y [expr {[winfo rooty $anchor] + \
  97. ([winfo height $anchor]-[winfo reqheight $w])/2}]
  98. } else {
  99. set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}]
  100. set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}]
  101. set checkBounds 0
  102. }
  103. if {$checkBounds} {
  104. if {$x < [winfo vrootx $w]} {
  105. set x [winfo vrootx $w]
  106. } elseif {$x > ([winfo vrootx $w]+[winfo vrootwidth $w]-[winfo reqwidth $w])} {
  107. set x [expr {[winfo vrootx $w]+[winfo vrootwidth $w]-[winfo reqwidth $w]}]
  108. }
  109. if {$y < [winfo vrooty $w]} {
  110. set y [winfo vrooty $w]
  111. } elseif {$y > ([winfo vrooty $w]+[winfo vrootheight $w]-[winfo reqheight $w])} {
  112. set y [expr {[winfo vrooty $w]+[winfo vrootheight $w]-[winfo reqheight $w]}]
  113. }
  114. if {[tk windowingsystem] eq "aqua"} {
  115. # Avoid the native menu bar which sits on top of everything.
  116. if {$y < 22} {
  117. set y 22
  118. }
  119. }
  120. }
  121. wm maxsize $w [winfo vrootwidth $w] [winfo vrootheight $w]
  122. wm geometry $w +$x+$y
  123. wm deiconify $w
  124. }
  125. # ::tk::SetFocusGrab --
  126. # swap out current focus and grab temporarily (for dialogs)
  127. # Arguments:
  128. # grab new window to grab
  129. # focus window to give focus to
  130. # Results:
  131. # Returns nothing
  132. #
  133. proc ::tk::SetFocusGrab {grab {focus {}}} {
  134. set index "$grab,$focus"
  135. upvar ::tk::FocusGrab($index) data
  136. lappend data [focus]
  137. set oldGrab [grab current $grab]
  138. lappend data $oldGrab
  139. if {[winfo exists $oldGrab]} {
  140. lappend data [grab status $oldGrab]
  141. }
  142. # The "grab" command will fail if another application
  143. # already holds the grab. So catch it.
  144. catch {grab $grab}
  145. if {[winfo exists $focus]} {
  146. focus $focus
  147. }
  148. }
  149. # ::tk::RestoreFocusGrab --
  150. # restore old focus and grab (for dialogs)
  151. # Arguments:
  152. # grab window that had taken grab
  153. # focus window that had taken focus
  154. # destroy destroy|withdraw - how to handle the old grabbed window
  155. # Results:
  156. # Returns nothing
  157. #
  158. proc ::tk::RestoreFocusGrab {grab focus {destroy destroy}} {
  159. set index "$grab,$focus"
  160. if {[info exists ::tk::FocusGrab($index)]} {
  161. foreach {oldFocus oldGrab oldStatus} $::tk::FocusGrab($index) { break }
  162. unset ::tk::FocusGrab($index)
  163. } else {
  164. set oldGrab ""
  165. }
  166. catch {focus $oldFocus}
  167. grab release $grab
  168. if {$destroy eq "withdraw"} {
  169. wm withdraw $grab
  170. } else {
  171. destroy $grab
  172. }
  173. if {[winfo exists $oldGrab] && [winfo ismapped $oldGrab]} {
  174. if {$oldStatus eq "global"} {
  175. grab -global $oldGrab
  176. } else {
  177. grab $oldGrab
  178. }
  179. }
  180. }
  181. # ::tk::GetSelection --
  182. # This tries to obtain the default selection. On Unix, we first try
  183. # and get a UTF8_STRING, a type supported by modern Unix apps for
  184. # passing Unicode data safely. We fall back on the default STRING
  185. # type otherwise. On Windows, only the STRING type is necessary.
  186. # Arguments:
  187. # w The widget for which the selection will be retrieved.
  188. # Important for the -displayof property.
  189. # sel The source of the selection (PRIMARY or CLIPBOARD)
  190. # Results:
  191. # Returns the selection, or an error if none could be found
  192. #
  193. if {[tk windowingsystem] ne "win32"} {
  194. proc ::tk::GetSelection {w {sel PRIMARY}} {
  195. if {[catch {
  196. selection get -displayof $w -selection $sel -type UTF8_STRING
  197. } txt] && [catch {
  198. selection get -displayof $w -selection $sel
  199. } txt]} then {
  200. return -code error -errorcode {TK SELECTION NONE} \
  201. "could not find default selection"
  202. } else {
  203. return $txt
  204. }
  205. }
  206. } else {
  207. proc ::tk::GetSelection {w {sel PRIMARY}} {
  208. if {[catch {
  209. selection get -displayof $w -selection $sel
  210. } txt]} then {
  211. return -code error -errorcode {TK SELECTION NONE} \
  212. "could not find default selection"
  213. } else {
  214. return $txt
  215. }
  216. }
  217. }
  218. # ::tk::ScreenChanged --
  219. # This procedure is invoked by the binding mechanism whenever the
  220. # "current" screen is changing. The procedure does two things.
  221. # First, it uses "upvar" to make variable "::tk::Priv" point at an
  222. # array variable that holds state for the current display. Second,
  223. # it initializes the array if it didn't already exist.
  224. #
  225. # Arguments:
  226. # screen - The name of the new screen.
  227. proc ::tk::ScreenChanged screen {
  228. # Extract the display name.
  229. set disp [string range $screen 0 [string last . $screen]-1]
  230. # Ensure that namespace separators never occur in the display name (as
  231. # they cause problems in variable names). Double-colons exist in some VNC
  232. # display names. [Bug 2912473]
  233. set disp [string map {:: _doublecolon_} $disp]
  234. uplevel #0 [list upvar #0 ::tk::Priv.$disp ::tk::Priv]
  235. variable ::tk::Priv
  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
  272. # changed. It is used to turn on or turn off the motif virtual
  273. # bindings.
  274. #
  275. # Arguments:
  276. # n1 - the name of the variable being changed ("::tk_strictMotif").
  277. proc ::tk::EventMotifBindings {n1 dummy dummy} {
  278. upvar $n1 name
  279. if {$name} {
  280. set op delete
  281. } else {
  282. set op add
  283. }
  284. event $op <<Cut>> <Control-Key-w> <Control-Lock-Key-W> <Shift-Key-Delete>
  285. event $op <<Copy>> <Meta-Key-w> <Meta-Lock-Key-W> <Control-Key-Insert>
  286. event $op <<Paste>> <Control-Key-y> <Control-Lock-Key-Y> <Shift-Key-Insert>
  287. event $op <<PrevChar>> <Control-Key-b> <Control-Lock-Key-B>
  288. event $op <<NextChar>> <Control-Key-f> <Control-Lock-Key-F>
  289. event $op <<PrevLine>> <Control-Key-p> <Control-Lock-Key-P>
  290. event $op <<NextLine>> <Control-Key-n> <Control-Lock-Key-N>
  291. event $op <<LineStart>> <Control-Key-a> <Control-Lock-Key-A>
  292. event $op <<LineEnd>> <Control-Key-e> <Control-Lock-Key-E>
  293. event $op <<SelectPrevChar>> <Control-Key-B> <Control-Lock-Key-b>
  294. event $op <<SelectNextChar>> <Control-Key-F> <Control-Lock-Key-f>
  295. event $op <<SelectPrevLine>> <Control-Key-P> <Control-Lock-Key-p>
  296. event $op <<SelectNextLine>> <Control-Key-N> <Control-Lock-Key-n>
  297. event $op <<SelectLineStart>> <Control-Key-A> <Control-Lock-Key-a>
  298. event $op <<SelectLineEnd>> <Control-Key-E> <Control-Lock-Key-e>
  299. }
  300. #----------------------------------------------------------------------
  301. # Define common dialogs on platforms where they are not implemented
  302. # using compiled code.
  303. #----------------------------------------------------------------------
  304. if {![llength [info commands tk_chooseColor]]} {
  305. proc ::tk_chooseColor {args} {
  306. return [::tk::dialog::color:: {*}$args]
  307. }
  308. }
  309. if {![llength [info commands tk_getOpenFile]]} {
  310. proc ::tk_getOpenFile {args} {
  311. if {$::tk_strictMotif} {
  312. return [::tk::MotifFDialog open {*}$args]
  313. } else {
  314. return [::tk::dialog::file:: open {*}$args]
  315. }
  316. }
  317. }
  318. if {![llength [info commands tk_getSaveFile]]} {
  319. proc ::tk_getSaveFile {args} {
  320. if {$::tk_strictMotif} {
  321. return [::tk::MotifFDialog save {*}$args]
  322. } else {
  323. return [::tk::dialog::file:: save {*}$args]
  324. }
  325. }
  326. }
  327. if {![llength [info commands tk_messageBox]]} {
  328. proc ::tk_messageBox {args} {
  329. return [::tk::MessageBox {*}$args]
  330. }
  331. }
  332. if {![llength [info command tk_chooseDirectory]]} {
  333. proc ::tk_chooseDirectory {args} {
  334. return [::tk::dialog::file::chooseDir:: {*}$args]
  335. }
  336. }
  337. #----------------------------------------------------------------------
  338. # Define the set of common virtual events.
  339. #----------------------------------------------------------------------
  340. switch -exact -- [tk windowingsystem] {
  341. "x11" {
  342. event add <<Cut>> <Control-Key-x> <Key-F20> <Control-Lock-Key-X>
  343. event add <<Copy>> <Control-Key-c> <Key-F16> <Control-Lock-Key-C>
  344. event add <<Paste>> <Control-Key-v> <Key-F18> <Control-Lock-Key-V>
  345. event add <<PasteSelection>> <ButtonRelease-2>
  346. event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z>
  347. event add <<Redo>> <Control-Key-Z> <Control-Lock-Key-z>
  348. event add <<ContextMenu>> <Button-3>
  349. # On Darwin/Aqua, buttons from left to right are 1,3,2. On Darwin/X11 with recent
  350. # XQuartz as the X server, they are 1,2,3; other X servers may differ.
  351. event add <<SelectAll>> <Control-Key-slash>
  352. event add <<SelectNone>> <Control-Key-backslash>
  353. event add <<NextChar>> <Right>
  354. event add <<SelectNextChar>> <Shift-Right>
  355. event add <<PrevChar>> <Left>
  356. event add <<SelectPrevChar>> <Shift-Left>
  357. event add <<NextWord>> <Control-Right>
  358. event add <<SelectNextWord>> <Control-Shift-Right>
  359. event add <<PrevWord>> <Control-Left>
  360. event add <<SelectPrevWord>> <Control-Shift-Left>
  361. event add <<LineStart>> <Home>
  362. event add <<SelectLineStart>> <Shift-Home>
  363. event add <<LineEnd>> <End>
  364. event add <<SelectLineEnd>> <Shift-End>
  365. event add <<PrevLine>> <Up>
  366. event add <<NextLine>> <Down>
  367. event add <<SelectPrevLine>> <Shift-Up>
  368. event add <<SelectNextLine>> <Shift-Down>
  369. event add <<PrevPara>> <Control-Up>
  370. event add <<NextPara>> <Control-Down>
  371. event add <<SelectPrevPara>> <Control-Shift-Up>
  372. event add <<SelectNextPara>> <Control-Shift-Down>
  373. event add <<ToggleSelection>> <Control-Button-1>
  374. # Some OS's define a goofy (as in, not <Shift-Tab>) keysym that is
  375. # returned when the user presses <Shift-Tab>. In order for tab
  376. # traversal to work, we have to add these keysyms to the PrevWindow
  377. # event. We use catch just in case the keysym isn't recognized.
  378. # This is needed for XFree86 systems
  379. catch { event add <<PrevWindow>> <ISO_Left_Tab> }
  380. # This seems to be correct on *some* HP systems.
  381. catch { event add <<PrevWindow>> <hpBackTab> }
  382. trace add variable ::tk_strictMotif write ::tk::EventMotifBindings
  383. set ::tk_strictMotif $::tk_strictMotif
  384. # On unix, we want to always display entry/text selection,
  385. # regardless of which window has focus
  386. set ::tk::AlwaysShowSelection 1
  387. }
  388. "win32" {
  389. event add <<Cut>> <Control-Key-x> <Shift-Key-Delete> <Control-Lock-Key-X>
  390. event add <<Copy>> <Control-Key-c> <Control-Key-Insert> <Control-Lock-Key-C>
  391. event add <<Paste>> <Control-Key-v> <Shift-Key-Insert> <Control-Lock-Key-V>
  392. event add <<PasteSelection>> <ButtonRelease-2>
  393. event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z>
  394. event add <<Redo>> <Control-Key-y> <Control-Lock-Key-Y>
  395. event add <<ContextMenu>> <Button-3>
  396. event add <<SelectAll>> <Control-Key-slash> <Control-Key-a> <Control-Lock-Key-A>
  397. event add <<SelectNone>> <Control-Key-backslash>
  398. event add <<NextChar>> <Right>
  399. event add <<SelectNextChar>> <Shift-Right>
  400. event add <<PrevChar>> <Left>
  401. event add <<SelectPrevChar>> <Shift-Left>
  402. event add <<NextWord>> <Control-Right>
  403. event add <<SelectNextWord>> <Control-Shift-Right>
  404. event add <<PrevWord>> <Control-Left>
  405. event add <<SelectPrevWord>> <Control-Shift-Left>
  406. event add <<LineStart>> <Home>
  407. event add <<SelectLineStart>> <Shift-Home>
  408. event add <<LineEnd>> <End>
  409. event add <<SelectLineEnd>> <Shift-End>
  410. event add <<PrevLine>> <Up>
  411. event add <<NextLine>> <Down>
  412. event add <<SelectPrevLine>> <Shift-Up>
  413. event add <<SelectNextLine>> <Shift-Down>
  414. event add <<PrevPara>> <Control-Up>
  415. event add <<NextPara>> <Control-Down>
  416. event add <<SelectPrevPara>> <Control-Shift-Up>
  417. event add <<SelectNextPara>> <Control-Shift-Down>
  418. event add <<ToggleSelection>> <Control-Button-1>
  419. }
  420. "aqua" {
  421. event add <<Cut>> <Command-Key-x> <Key-F2> <Command-Lock-Key-X>
  422. event add <<Copy>> <Command-Key-c> <Key-F3> <Command-Lock-Key-C>
  423. event add <<Paste>> <Command-Key-v> <Key-F4> <Command-Lock-Key-V>
  424. event add <<PasteSelection>> <ButtonRelease-3>
  425. event add <<Clear>> <Clear>
  426. event add <<ContextMenu>> <Button-2>
  427. # Official bindings
  428. # See http://support.apple.com/kb/HT1343
  429. event add <<SelectAll>> <Command-Key-a>
  430. event add <<Undo>> <Command-Key-z> <Command-Lock-Key-Z>
  431. event add <<Redo>> <Shift-Command-Key-z> <Shift-Command-Lock-Key-z>
  432. event add <<NextChar>> <Right> <Control-Key-f> <Control-Lock-Key-F>
  433. event add <<SelectNextChar>> <Shift-Right> <Shift-Control-Key-F> <Shift-Control-Lock-Key-F>
  434. event add <<PrevChar>> <Left> <Control-Key-b> <Control-Lock-Key-B>
  435. event add <<SelectPrevChar>> <Shift-Left> <Shift-Control-Key-B> <Shift-Control-Lock-Key-B>
  436. event add <<NextWord>> <Option-Right>
  437. event add <<SelectNextWord>> <Shift-Option-Right>
  438. event add <<PrevWord>> <Option-Left>
  439. event add <<SelectPrevWord>> <Shift-Option-Left>
  440. event add <<LineStart>> <Home> <Command-Left> <Control-Key-a> <Control-Lock-Key-A>
  441. event add <<SelectLineStart>> <Shift-Home> <Shift-Command-Left> <Shift-Control-Key-A> <Shift-Control-Lock-Key-A>
  442. event add <<LineEnd>> <End> <Command-Right> <Control-Key-e> <Control-Lock-Key-E>
  443. event add <<SelectLineEnd>> <Shift-End> <Shift-Command-Right> <Shift-Control-Key-E> <Shift-Control-Lock-Key-E>
  444. event add <<PrevLine>> <Up> <Control-Key-p> <Control-Lock-Key-P>
  445. event add <<SelectPrevLine>> <Shift-Up> <Shift-Control-Key-P> <Shift-Control-Lock-Key-P>
  446. event add <<NextLine>> <Down> <Control-Key-n> <Control-Lock-Key-N>
  447. event add <<SelectNextLine>> <Shift-Down> <Shift-Control-Key-N> <Shift-Control-Lock-Key-N>
  448. # Not official, but logical extensions of above. Also derived from
  449. # bindings present in MS Word on OSX.
  450. event add <<PrevPara>> <Option-Up>
  451. event add <<NextPara>> <Option-Down>
  452. event add <<SelectPrevPara>> <Shift-Option-Up>
  453. event add <<SelectNextPara>> <Shift-Option-Down>
  454. event add <<ToggleSelection>> <Command-Button-1>
  455. }
  456. }
  457. # ----------------------------------------------------------------------
  458. # Read in files that define all of the class bindings.
  459. # ----------------------------------------------------------------------
  460. if {$::tk_library ne ""} {
  461. proc ::tk::SourceLibFile {file} {
  462. namespace eval :: [list source -encoding utf-8 [file join $::tk_library $file.tcl]]
  463. }
  464. namespace eval ::tk {
  465. SourceLibFile icons
  466. SourceLibFile button
  467. SourceLibFile entry
  468. SourceLibFile listbox
  469. SourceLibFile menu
  470. SourceLibFile panedwindow
  471. SourceLibFile scale
  472. SourceLibFile scrlbar
  473. SourceLibFile spinbox
  474. SourceLibFile text
  475. }
  476. }
  477. # ----------------------------------------------------------------------
  478. # Default bindings for keyboard traversal.
  479. # ----------------------------------------------------------------------
  480. event add <<PrevWindow>> <Shift-Tab>
  481. event add <<NextWindow>> <Tab>
  482. bind all <<NextWindow>> {tk::TabToWindow [tk_focusNext %W]}
  483. bind all <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]}
  484. # ::tk::CancelRepeat --
  485. # This procedure is invoked to cancel an auto-repeat action described
  486. # by ::tk::Priv(afterId). It's used by several widgets to auto-scroll
  487. # the widget when the mouse is dragged out of the widget with a
  488. # button pressed.
  489. #
  490. # Arguments:
  491. # None.
  492. proc ::tk::CancelRepeat {} {
  493. variable ::tk::Priv
  494. after cancel $Priv(afterId)
  495. set Priv(afterId) {}
  496. }
  497. # ::tk::TabToWindow --
  498. # This procedure moves the focus to the given widget.
  499. # It sends a <<TraverseOut>> virtual event to the previous focus window,
  500. # if any, before changing the focus, and a <<TraverseIn>> event
  501. # to the new focus window afterwards.
  502. #
  503. # Arguments:
  504. # w - Window to which focus should be set.
  505. proc ::tk::TabToWindow {w} {
  506. set focus [focus]
  507. if {$focus ne ""} {
  508. event generate $focus <<TraverseOut>>
  509. }
  510. focus $w
  511. event generate $w <<TraverseIn>>
  512. }
  513. # ::tk::UnderlineAmpersand --
  514. # This procedure takes some text with ampersand and returns text w/o
  515. # ampersand and position of the ampersand. Double ampersands are
  516. # converted to single ones. Position returned is -1 when there is no
  517. # ampersand.
  518. #
  519. proc ::tk::UnderlineAmpersand {text} {
  520. set s [string map {&& & & \ufeff} $text]
  521. set idx [string first \ufeff $s]
  522. return [list [string map {\ufeff {}} $s] $idx]
  523. }
  524. # ::tk::SetAmpText --
  525. # Given widget path and text with "magic ampersands", sets -text and
  526. # -underline options for the widget
  527. #
  528. proc ::tk::SetAmpText {widget text} {
  529. lassign [UnderlineAmpersand $text] newtext under
  530. $widget configure -text $newtext -underline $under
  531. }
  532. # ::tk::AmpWidget --
  533. # Creates new widget, turning -text option into -text and -underline
  534. # options, returned by ::tk::UnderlineAmpersand.
  535. #
  536. proc ::tk::AmpWidget {class path args} {
  537. set options {}
  538. foreach {opt val} $args {
  539. if {$opt eq "-text"} {
  540. lassign [UnderlineAmpersand $val] newtext under
  541. lappend options -text $newtext -underline $under
  542. } else {
  543. lappend options $opt $val
  544. }
  545. }
  546. set result [$class $path {*}$options]
  547. if {[string match "*button" $class]} {
  548. bind $path <<AltUnderlined>> [list $path invoke]
  549. }
  550. return $result
  551. }
  552. # ::tk::AmpMenuArgs --
  553. # Processes arguments for a menu entry, turning -label option into
  554. # -label and -underline options, returned by ::tk::UnderlineAmpersand.
  555. # The cmd argument is supposed to be either "add" or "entryconfigure"
  556. #
  557. proc ::tk::AmpMenuArgs {widget cmd type args} {
  558. set options {}
  559. foreach {opt val} $args {
  560. if {$opt eq "-label"} {
  561. lassign [UnderlineAmpersand $val] newlabel under
  562. lappend options -label $newlabel -underline $under
  563. } else {
  564. lappend options $opt $val
  565. }
  566. }
  567. $widget $cmd $type {*}$options
  568. }
  569. # ::tk::FindAltKeyTarget --
  570. # Search recursively through the hierarchy of visible widgets to find
  571. # button or label which has $char as underlined character.
  572. #
  573. proc ::tk::FindAltKeyTarget {path char} {
  574. set class [winfo class $path]
  575. if {$class in {
  576. Button Checkbutton Label Radiobutton
  577. TButton TCheckbutton TLabel TRadiobutton
  578. } && [string equal -nocase $char \
  579. [string index [$path cget -text] [$path cget -underline]]]} {
  580. return $path
  581. }
  582. set subwins [concat [grid slaves $path] [pack slaves $path] \
  583. [place slaves $path]]
  584. if {$class eq "Canvas"} {
  585. foreach item [$path find all] {
  586. if {[$path type $item] eq "window"} {
  587. set w [$path itemcget $item -window]
  588. if {$w ne ""} {lappend subwins $w}
  589. }
  590. }
  591. } elseif {$class eq "Text"} {
  592. lappend subwins {*}[$path window names]
  593. }
  594. foreach child $subwins {
  595. set target [FindAltKeyTarget $child $char]
  596. if {$target ne ""} {
  597. return $target
  598. }
  599. }
  600. }
  601. # ::tk::AltKeyInDialog --
  602. # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>>
  603. # to button or label which has appropriate underlined character.
  604. #
  605. proc ::tk::AltKeyInDialog {path key} {
  606. set target [FindAltKeyTarget $path $key]
  607. if {$target ne ""} {
  608. event generate $target <<AltUnderlined>>
  609. }
  610. }
  611. # ::tk::mcmaxamp --
  612. # Replacement for mcmax, used for texts with "magic ampersand" in it.
  613. #
  614. proc ::tk::mcmaxamp {args} {
  615. set maxlen 0
  616. foreach arg $args {
  617. # Should we run [mc] in caller's namespace?
  618. lassign [UnderlineAmpersand [mc $arg]] msg
  619. set length [string length $msg]
  620. if {$length > $maxlen} {
  621. set maxlen $length
  622. }
  623. }
  624. return $maxlen
  625. }
  626. # For now, turn off the custom mdef proc for the Mac:
  627. if {[tk windowingsystem] eq "aqua"} {
  628. namespace eval ::tk::mac {
  629. set useCustomMDEF 0
  630. }
  631. }
  632. if {[tk windowingsystem] eq "aqua"} {
  633. #stub procedures to respond to "do script" Apple Events
  634. proc ::tk::mac::DoScriptFile {file} {
  635. uplevel #0 $file
  636. source -encoding utf-8 $file
  637. }
  638. proc ::tk::mac::DoScriptText {script} {
  639. uplevel #0 $script
  640. eval $script
  641. }
  642. }
  643. # Create a dictionary to store the starting index of the IME marked
  644. # text in an Entry or Text widget.
  645. set ::tk::Priv(IMETextMark) [dict create]
  646. # Run the Ttk themed widget set initialization
  647. if {$::ttk::library ne ""} {
  648. uplevel \#0 [list source -encoding utf-8 $::ttk::library/ttk.tcl]
  649. }
  650. # Local Variables:
  651. # mode: tcl
  652. # fill-column: 78
  653. # End: