tk.tcl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.14
  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 {[winfo exists $grab]} {
  169. if {$destroy eq "withdraw"} {
  170. wm withdraw $grab
  171. } else {
  172. destroy $grab
  173. }
  174. }
  175. if {[winfo exists $oldGrab] && [winfo ismapped $oldGrab]} {
  176. # The "grab" command will fail if another application
  177. # already holds the grab on a window with the same name.
  178. # So catch it. See [7447ed20ec] for an example.
  179. if {$oldStatus eq "global"} {
  180. catch {grab -global $oldGrab}
  181. } else {
  182. catch {grab $oldGrab}
  183. }
  184. }
  185. }
  186. # ::tk::GetSelection --
  187. # This tries to obtain the default selection. On Unix, we first try
  188. # and get a UTF8_STRING, a type supported by modern Unix apps for
  189. # passing Unicode data safely. We fall back on the default STRING
  190. # type otherwise. On Windows, only the STRING type is necessary.
  191. # Arguments:
  192. # w The widget for which the selection will be retrieved.
  193. # Important for the -displayof property.
  194. # sel The source of the selection (PRIMARY or CLIPBOARD)
  195. # Results:
  196. # Returns the selection, or an error if none could be found
  197. #
  198. if {[tk windowingsystem] ne "win32"} {
  199. proc ::tk::GetSelection {w {sel PRIMARY}} {
  200. if {[catch {
  201. selection get -displayof $w -selection $sel -type UTF8_STRING
  202. } txt] && [catch {
  203. selection get -displayof $w -selection $sel
  204. } txt]} then {
  205. return -code error -errorcode {TK SELECTION NONE} \
  206. "could not find default selection"
  207. } else {
  208. return $txt
  209. }
  210. }
  211. } else {
  212. proc ::tk::GetSelection {w {sel PRIMARY}} {
  213. if {[catch {
  214. selection get -displayof $w -selection $sel
  215. } txt]} then {
  216. return -code error -errorcode {TK SELECTION NONE} \
  217. "could not find default selection"
  218. } else {
  219. return $txt
  220. }
  221. }
  222. }
  223. # ::tk::ScreenChanged --
  224. # This procedure is invoked by the binding mechanism whenever the
  225. # "current" screen is changing. The procedure does two things.
  226. # First, it uses "upvar" to make variable "::tk::Priv" point at an
  227. # array variable that holds state for the current display. Second,
  228. # it initializes the array if it didn't already exist.
  229. #
  230. # Arguments:
  231. # screen - The name of the new screen.
  232. proc ::tk::ScreenChanged screen {
  233. # Extract the display name.
  234. set disp [string range $screen 0 [string last . $screen]-1]
  235. # Ensure that namespace separators never occur in the display name (as
  236. # they cause problems in variable names). Double-colons exist in some VNC
  237. # display names. [Bug 2912473]
  238. set disp [string map {:: _doublecolon_} $disp]
  239. uplevel #0 [list upvar #0 ::tk::Priv.$disp ::tk::Priv]
  240. variable ::tk::Priv
  241. if {[info exists Priv]} {
  242. set Priv(screen) $screen
  243. return
  244. }
  245. array set Priv {
  246. activeMenu {}
  247. activeItem {}
  248. afterId {}
  249. buttons 0
  250. buttonWindow {}
  251. dragging 0
  252. focus {}
  253. grab {}
  254. initPos {}
  255. inMenubutton {}
  256. listboxPrev {}
  257. menuBar {}
  258. mouseMoved 0
  259. oldGrab {}
  260. popup {}
  261. postedMb {}
  262. pressX 0
  263. pressY 0
  264. prevPos 0
  265. selectMode char
  266. }
  267. set Priv(screen) $screen
  268. set Priv(tearoff) [string equal [tk windowingsystem] "x11"]
  269. set Priv(window) {}
  270. }
  271. # Do initial setup for Priv, so that it is always bound to something
  272. # (otherwise, if someone references it, it may get set to a non-upvar-ed
  273. # value, which will cause trouble later).
  274. tk::ScreenChanged [winfo screen .]
  275. # ::tk::EventMotifBindings --
  276. # This procedure is invoked as a trace whenever ::tk_strictMotif is
  277. # changed. It is used to turn on or turn off the motif virtual
  278. # bindings.
  279. #
  280. # Arguments:
  281. # n1 - the name of the variable being changed ("::tk_strictMotif").
  282. proc ::tk::EventMotifBindings {n1 dummy dummy} {
  283. upvar $n1 name
  284. if {$name} {
  285. set op delete
  286. } else {
  287. set op add
  288. }
  289. event $op <<Cut>> <Control-Key-w> <Control-Lock-Key-W> <Shift-Key-Delete>
  290. event $op <<Copy>> <Meta-Key-w> <Meta-Lock-Key-W> <Control-Key-Insert>
  291. event $op <<Paste>> <Control-Key-y> <Control-Lock-Key-Y> <Shift-Key-Insert>
  292. event $op <<PrevChar>> <Control-Key-b> <Control-Lock-Key-B>
  293. event $op <<NextChar>> <Control-Key-f> <Control-Lock-Key-F>
  294. event $op <<PrevLine>> <Control-Key-p> <Control-Lock-Key-P>
  295. event $op <<NextLine>> <Control-Key-n> <Control-Lock-Key-N>
  296. event $op <<LineStart>> <Control-Key-a> <Control-Lock-Key-A>
  297. event $op <<LineEnd>> <Control-Key-e> <Control-Lock-Key-E>
  298. event $op <<SelectPrevChar>> <Control-Key-B> <Control-Lock-Key-b>
  299. event $op <<SelectNextChar>> <Control-Key-F> <Control-Lock-Key-f>
  300. event $op <<SelectPrevLine>> <Control-Key-P> <Control-Lock-Key-p>
  301. event $op <<SelectNextLine>> <Control-Key-N> <Control-Lock-Key-n>
  302. event $op <<SelectLineStart>> <Control-Key-A> <Control-Lock-Key-a>
  303. event $op <<SelectLineEnd>> <Control-Key-E> <Control-Lock-Key-e>
  304. }
  305. #----------------------------------------------------------------------
  306. # Define common dialogs on platforms where they are not implemented
  307. # using compiled code.
  308. #----------------------------------------------------------------------
  309. if {![llength [info commands tk_chooseColor]]} {
  310. proc ::tk_chooseColor {args} {
  311. return [::tk::dialog::color:: {*}$args]
  312. }
  313. }
  314. if {![llength [info commands tk_getOpenFile]]} {
  315. proc ::tk_getOpenFile {args} {
  316. if {$::tk_strictMotif} {
  317. return [::tk::MotifFDialog open {*}$args]
  318. } else {
  319. return [::tk::dialog::file:: open {*}$args]
  320. }
  321. }
  322. }
  323. if {![llength [info commands tk_getSaveFile]]} {
  324. proc ::tk_getSaveFile {args} {
  325. if {$::tk_strictMotif} {
  326. return [::tk::MotifFDialog save {*}$args]
  327. } else {
  328. return [::tk::dialog::file:: save {*}$args]
  329. }
  330. }
  331. }
  332. if {![llength [info commands tk_messageBox]]} {
  333. proc ::tk_messageBox {args} {
  334. return [::tk::MessageBox {*}$args]
  335. }
  336. }
  337. if {![llength [info command tk_chooseDirectory]]} {
  338. proc ::tk_chooseDirectory {args} {
  339. return [::tk::dialog::file::chooseDir:: {*}$args]
  340. }
  341. }
  342. #----------------------------------------------------------------------
  343. # Define the set of common virtual events.
  344. #----------------------------------------------------------------------
  345. switch -exact -- [tk windowingsystem] {
  346. "x11" {
  347. event add <<Cut>> <Control-Key-x> <Key-F20> <Control-Lock-Key-X>
  348. event add <<Copy>> <Control-Key-c> <Key-F16> <Control-Lock-Key-C>
  349. event add <<Paste>> <Control-Key-v> <Key-F18> <Control-Lock-Key-V>
  350. event add <<PasteSelection>> <ButtonRelease-2>
  351. event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z>
  352. event add <<Redo>> <Control-Key-Z> <Control-Lock-Key-z>
  353. event add <<ContextMenu>> <Button-3>
  354. # On Darwin/Aqua, buttons from left to right are 1,3,2. On Darwin/X11 with recent
  355. # XQuartz as the X server, they are 1,2,3; other X servers may differ.
  356. event add <<SelectAll>> <Control-Key-slash>
  357. event add <<SelectNone>> <Control-Key-backslash>
  358. event add <<NextChar>> <Right>
  359. event add <<SelectNextChar>> <Shift-Right>
  360. event add <<PrevChar>> <Left>
  361. event add <<SelectPrevChar>> <Shift-Left>
  362. event add <<NextWord>> <Control-Right>
  363. event add <<SelectNextWord>> <Control-Shift-Right>
  364. event add <<PrevWord>> <Control-Left>
  365. event add <<SelectPrevWord>> <Control-Shift-Left>
  366. event add <<LineStart>> <Home>
  367. event add <<SelectLineStart>> <Shift-Home>
  368. event add <<LineEnd>> <End>
  369. event add <<SelectLineEnd>> <Shift-End>
  370. event add <<PrevLine>> <Up>
  371. event add <<NextLine>> <Down>
  372. event add <<SelectPrevLine>> <Shift-Up>
  373. event add <<SelectNextLine>> <Shift-Down>
  374. event add <<PrevPara>> <Control-Up>
  375. event add <<NextPara>> <Control-Down>
  376. event add <<SelectPrevPara>> <Control-Shift-Up>
  377. event add <<SelectNextPara>> <Control-Shift-Down>
  378. event add <<ToggleSelection>> <Control-Button-1>
  379. # Some OS's define a goofy (as in, not <Shift-Tab>) keysym that is
  380. # returned when the user presses <Shift-Tab>. In order for tab
  381. # traversal to work, we have to add these keysyms to the PrevWindow
  382. # event. We use catch just in case the keysym isn't recognized.
  383. # This is needed for XFree86 systems
  384. catch { event add <<PrevWindow>> <ISO_Left_Tab> }
  385. # This seems to be correct on *some* HP systems.
  386. catch { event add <<PrevWindow>> <hpBackTab> }
  387. trace add variable ::tk_strictMotif write ::tk::EventMotifBindings
  388. set ::tk_strictMotif $::tk_strictMotif
  389. # On unix, we want to always display entry/text selection,
  390. # regardless of which window has focus
  391. set ::tk::AlwaysShowSelection 1
  392. }
  393. "win32" {
  394. event add <<Cut>> <Control-Key-x> <Shift-Key-Delete> <Control-Lock-Key-X>
  395. event add <<Copy>> <Control-Key-c> <Control-Key-Insert> <Control-Lock-Key-C>
  396. event add <<Paste>> <Control-Key-v> <Shift-Key-Insert> <Control-Lock-Key-V>
  397. event add <<PasteSelection>> <ButtonRelease-2>
  398. event add <<Undo>> <Control-Key-z> <Control-Lock-Key-Z>
  399. event add <<Redo>> <Control-Key-y> <Control-Lock-Key-Y>
  400. event add <<ContextMenu>> <Button-3>
  401. event add <<SelectAll>> <Control-Key-slash> <Control-Key-a> <Control-Lock-Key-A>
  402. event add <<SelectNone>> <Control-Key-backslash>
  403. event add <<NextChar>> <Right>
  404. event add <<SelectNextChar>> <Shift-Right>
  405. event add <<PrevChar>> <Left>
  406. event add <<SelectPrevChar>> <Shift-Left>
  407. event add <<NextWord>> <Control-Right>
  408. event add <<SelectNextWord>> <Control-Shift-Right>
  409. event add <<PrevWord>> <Control-Left>
  410. event add <<SelectPrevWord>> <Control-Shift-Left>
  411. event add <<LineStart>> <Home>
  412. event add <<SelectLineStart>> <Shift-Home>
  413. event add <<LineEnd>> <End>
  414. event add <<SelectLineEnd>> <Shift-End>
  415. event add <<PrevLine>> <Up>
  416. event add <<NextLine>> <Down>
  417. event add <<SelectPrevLine>> <Shift-Up>
  418. event add <<SelectNextLine>> <Shift-Down>
  419. event add <<PrevPara>> <Control-Up>
  420. event add <<NextPara>> <Control-Down>
  421. event add <<SelectPrevPara>> <Control-Shift-Up>
  422. event add <<SelectNextPara>> <Control-Shift-Down>
  423. event add <<ToggleSelection>> <Control-Button-1>
  424. }
  425. "aqua" {
  426. event add <<Cut>> <Command-Key-x> <Key-F2> <Command-Lock-Key-X>
  427. event add <<Copy>> <Command-Key-c> <Key-F3> <Command-Lock-Key-C>
  428. event add <<Paste>> <Command-Key-v> <Key-F4> <Command-Lock-Key-V>
  429. event add <<PasteSelection>> <ButtonRelease-3>
  430. event add <<Clear>> <Clear>
  431. event add <<ContextMenu>> <Button-2>
  432. # Official bindings
  433. # See https://support.apple.com/en-us/HT201236
  434. event add <<SelectAll>> <Command-Key-a>
  435. event add <<Undo>> <Command-Key-z> <Command-Lock-Key-Z>
  436. event add <<Redo>> <Shift-Command-Key-z> <Shift-Command-Lock-Key-z>
  437. event add <<NextChar>> <Right> <Control-Key-f> <Control-Lock-Key-F>
  438. event add <<SelectNextChar>> <Shift-Right> <Shift-Control-Key-F> <Shift-Control-Lock-Key-F>
  439. event add <<PrevChar>> <Left> <Control-Key-b> <Control-Lock-Key-B>
  440. event add <<SelectPrevChar>> <Shift-Left> <Shift-Control-Key-B> <Shift-Control-Lock-Key-B>
  441. event add <<NextWord>> <Option-Right>
  442. event add <<SelectNextWord>> <Shift-Option-Right>
  443. event add <<PrevWord>> <Option-Left>
  444. event add <<SelectPrevWord>> <Shift-Option-Left>
  445. event add <<LineStart>> <Home> <Command-Left> <Control-Key-a> <Control-Lock-Key-A>
  446. event add <<SelectLineStart>> <Shift-Home> <Shift-Command-Left> <Shift-Control-Key-A> <Shift-Control-Lock-Key-A>
  447. event add <<LineEnd>> <End> <Command-Right> <Control-Key-e> <Control-Lock-Key-E>
  448. event add <<SelectLineEnd>> <Shift-End> <Shift-Command-Right> <Shift-Control-Key-E> <Shift-Control-Lock-Key-E>
  449. event add <<PrevLine>> <Up> <Control-Key-p> <Control-Lock-Key-P>
  450. event add <<SelectPrevLine>> <Shift-Up> <Shift-Control-Key-P> <Shift-Control-Lock-Key-P>
  451. event add <<NextLine>> <Down> <Control-Key-n> <Control-Lock-Key-N>
  452. event add <<SelectNextLine>> <Shift-Down> <Shift-Control-Key-N> <Shift-Control-Lock-Key-N>
  453. # Not official, but logical extensions of above. Also derived from
  454. # bindings present in MS Word on OSX.
  455. event add <<PrevPara>> <Option-Up>
  456. event add <<NextPara>> <Option-Down>
  457. event add <<SelectPrevPara>> <Shift-Option-Up>
  458. event add <<SelectNextPara>> <Shift-Option-Down>
  459. event add <<ToggleSelection>> <Command-Button-1>
  460. }
  461. }
  462. # ----------------------------------------------------------------------
  463. # Read in files that define all of the class bindings.
  464. # ----------------------------------------------------------------------
  465. if {$::tk_library ne ""} {
  466. proc ::tk::SourceLibFile {file} {
  467. namespace eval :: [list source -encoding utf-8 [file join $::tk_library $file.tcl]]
  468. }
  469. namespace eval ::tk {
  470. SourceLibFile icons
  471. SourceLibFile button
  472. SourceLibFile entry
  473. SourceLibFile listbox
  474. SourceLibFile menu
  475. SourceLibFile panedwindow
  476. SourceLibFile scale
  477. SourceLibFile scrlbar
  478. SourceLibFile spinbox
  479. SourceLibFile text
  480. }
  481. }
  482. # ----------------------------------------------------------------------
  483. # Default bindings for keyboard traversal.
  484. # ----------------------------------------------------------------------
  485. event add <<PrevWindow>> <Shift-Tab>
  486. event add <<NextWindow>> <Tab>
  487. bind all <<NextWindow>> {tk::TabToWindow [tk_focusNext %W]}
  488. bind all <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]}
  489. # ::tk::CancelRepeat --
  490. # This procedure is invoked to cancel an auto-repeat action described
  491. # by ::tk::Priv(afterId). It's used by several widgets to auto-scroll
  492. # the widget when the mouse is dragged out of the widget with a
  493. # button pressed.
  494. #
  495. # Arguments:
  496. # None.
  497. proc ::tk::CancelRepeat {} {
  498. variable ::tk::Priv
  499. after cancel $Priv(afterId)
  500. set Priv(afterId) {}
  501. }
  502. # ::tk::TabToWindow --
  503. # This procedure moves the focus to the given widget.
  504. # It sends a <<TraverseOut>> virtual event to the previous focus window,
  505. # if any, before changing the focus, and a <<TraverseIn>> event
  506. # to the new focus window afterwards.
  507. #
  508. # Arguments:
  509. # w - Window to which focus should be set.
  510. proc ::tk::TabToWindow {w} {
  511. set focus [focus]
  512. if {$focus ne ""} {
  513. event generate $focus <<TraverseOut>>
  514. }
  515. focus $w
  516. event generate $w <<TraverseIn>>
  517. }
  518. # ::tk::UnderlineAmpersand --
  519. # This procedure takes some text with ampersand and returns text w/o
  520. # ampersand and position of the ampersand. Double ampersands are
  521. # converted to single ones. Position returned is -1 when there is no
  522. # ampersand.
  523. #
  524. proc ::tk::UnderlineAmpersand {text} {
  525. set s [string map {&& & & \ufeff} $text]
  526. set idx [string first \ufeff $s]
  527. return [list [string map {\ufeff {}} $s] $idx]
  528. }
  529. # ::tk::SetAmpText --
  530. # Given widget path and text with "magic ampersands", sets -text and
  531. # -underline options for the widget
  532. #
  533. proc ::tk::SetAmpText {widget text} {
  534. lassign [UnderlineAmpersand $text] newtext under
  535. $widget configure -text $newtext -underline $under
  536. }
  537. # ::tk::AmpWidget --
  538. # Creates new widget, turning -text option into -text and -underline
  539. # options, returned by ::tk::UnderlineAmpersand.
  540. #
  541. proc ::tk::AmpWidget {class path args} {
  542. set options {}
  543. foreach {opt val} $args {
  544. if {$opt eq "-text"} {
  545. lassign [UnderlineAmpersand $val] newtext under
  546. lappend options -text $newtext -underline $under
  547. } else {
  548. lappend options $opt $val
  549. }
  550. }
  551. set result [$class $path {*}$options]
  552. if {[string match "*button" $class]} {
  553. bind $path <<AltUnderlined>> [list $path invoke]
  554. }
  555. return $result
  556. }
  557. # ::tk::AmpMenuArgs --
  558. # Processes arguments for a menu entry, turning -label option into
  559. # -label and -underline options, returned by ::tk::UnderlineAmpersand.
  560. # The cmd argument is supposed to be either "add" or "entryconfigure"
  561. #
  562. proc ::tk::AmpMenuArgs {widget cmd type args} {
  563. set options {}
  564. foreach {opt val} $args {
  565. if {$opt eq "-label"} {
  566. lassign [UnderlineAmpersand $val] newlabel under
  567. lappend options -label $newlabel -underline $under
  568. } else {
  569. lappend options $opt $val
  570. }
  571. }
  572. $widget $cmd $type {*}$options
  573. }
  574. # ::tk::FindAltKeyTarget --
  575. # Search recursively through the hierarchy of visible widgets to find
  576. # button or label which has $char as underlined character.
  577. #
  578. proc ::tk::FindAltKeyTarget {path char} {
  579. set class [winfo class $path]
  580. if {$class in {
  581. Button Checkbutton Label Radiobutton
  582. TButton TCheckbutton TLabel TRadiobutton
  583. } && [string equal -nocase $char \
  584. [string index [$path cget -text] [$path cget -underline]]]} {
  585. return $path
  586. }
  587. set subwins [concat [grid slaves $path] [pack slaves $path] \
  588. [place slaves $path]]
  589. if {$class eq "Canvas"} {
  590. foreach item [$path find all] {
  591. if {[$path type $item] eq "window"} {
  592. set w [$path itemcget $item -window]
  593. if {$w ne ""} {lappend subwins $w}
  594. }
  595. }
  596. } elseif {$class eq "Text"} {
  597. lappend subwins {*}[$path window names]
  598. }
  599. foreach child $subwins {
  600. set target [FindAltKeyTarget $child $char]
  601. if {$target ne ""} {
  602. return $target
  603. }
  604. }
  605. }
  606. # ::tk::AltKeyInDialog --
  607. # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>>
  608. # to button or label which has appropriate underlined character.
  609. #
  610. proc ::tk::AltKeyInDialog {path key} {
  611. set target [FindAltKeyTarget $path $key]
  612. if {$target ne ""} {
  613. event generate $target <<AltUnderlined>>
  614. }
  615. }
  616. # ::tk::mcmaxamp --
  617. # Replacement for mcmax, used for texts with "magic ampersand" in it.
  618. #
  619. proc ::tk::mcmaxamp {args} {
  620. set maxlen 0
  621. foreach arg $args {
  622. # Should we run [mc] in caller's namespace?
  623. lassign [UnderlineAmpersand [mc $arg]] msg
  624. set length [string length $msg]
  625. if {$length > $maxlen} {
  626. set maxlen $length
  627. }
  628. }
  629. return $maxlen
  630. }
  631. if {[tk windowingsystem] eq "aqua"} {
  632. #stub procedures to respond to "do script" Apple Events
  633. proc ::tk::mac::DoScriptFile {file} {
  634. uplevel #0 $file
  635. source -encoding utf-8 $file
  636. }
  637. proc ::tk::mac::DoScriptText {script} {
  638. uplevel #0 $script
  639. eval $script
  640. }
  641. }
  642. # Create a dictionary to store the starting index of the IME marked
  643. # text in an Entry or Text widget.
  644. set ::tk::Priv(IMETextMark) [dict create]
  645. # Run the Ttk themed widget set initialization
  646. if {$::ttk::library ne ""} {
  647. uplevel \#0 [list source -encoding utf-8 $::ttk::library/ttk.tcl]
  648. }
  649. # Local Variables:
  650. # mode: tcl
  651. # fill-column: 78
  652. # End: