tk.tcl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 © 1992-1994 The Regents of the University of California.
  7. # Copyright © 1994-1996 Sun Microsystems, Inc.
  8. # Copyright © 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 9.0.1
  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-w> <Control-Lock-W> <Shift-Delete>
  290. event $op <<Copy>> <Meta-w> <Meta-Lock-W> <Control-Insert>
  291. event $op <<Paste>> <Control-y> <Control-Lock-Y> <Shift-Insert>
  292. event $op <<PrevChar>> <Control-b> <Control-Lock-B>
  293. event $op <<NextChar>> <Control-f> <Control-Lock-F>
  294. event $op <<PrevLine>> <Control-p> <Control-Lock-P>
  295. event $op <<NextLine>> <Control-n> <Control-Lock-N>
  296. event $op <<LineStart>> <Control-a> <Control-Lock-A>
  297. event $op <<LineEnd>> <Control-e> <Control-Lock-E>
  298. event $op <<SelectPrevChar>> <Control-B> <Control-Lock-b>
  299. event $op <<SelectNextChar>> <Control-F> <Control-Lock-f>
  300. event $op <<SelectPrevLine>> <Control-P> <Control-Lock-p>
  301. event $op <<SelectNextLine>> <Control-N> <Control-Lock-n>
  302. event $op <<SelectLineStart>> <Control-A> <Control-Lock-a>
  303. event $op <<SelectLineEnd>> <Control-E> <Control-Lock-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. event add <<ContextMenu>> <Button-3>
  346. event add <<PasteSelection>> <ButtonRelease-2>
  347. switch -exact -- [tk windowingsystem] {
  348. "x11" {
  349. event add <<Cut>> <Control-x> <F20> <Control-Lock-X>
  350. event add <<Copy>> <Control-c> <F16> <Control-Lock-C>
  351. event add <<Paste>> <Control-v> <F18> <Control-Lock-V>
  352. event add <<Undo>> <Control-z> <Control-Lock-Z>
  353. event add <<Redo>> <Control-Z> <Control-Lock-z>
  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-/>
  357. event add <<SelectNone>> <Control-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-x> <Shift-Delete> <Control-Lock-X>
  395. event add <<Copy>> <Control-c> <Control-Insert> <Control-Lock-C>
  396. event add <<Paste>> <Control-v> <Shift-Insert> <Control-Lock-V>
  397. event add <<Undo>> <Control-z> <Control-Lock-Z>
  398. event add <<Redo>> <Control-y> <Control-Lock-Y>
  399. event add <<SelectAll>> <Control-/> <Control-a> <Control-Lock-A>
  400. event add <<SelectNone>> <Control-backslash>
  401. event add <<NextChar>> <Right>
  402. event add <<SelectNextChar>> <Shift-Right>
  403. event add <<PrevChar>> <Left>
  404. event add <<SelectPrevChar>> <Shift-Left>
  405. event add <<NextWord>> <Control-Right>
  406. event add <<SelectNextWord>> <Control-Shift-Right>
  407. event add <<PrevWord>> <Control-Left>
  408. event add <<SelectPrevWord>> <Control-Shift-Left>
  409. event add <<LineStart>> <Home>
  410. event add <<SelectLineStart>> <Shift-Home>
  411. event add <<LineEnd>> <End>
  412. event add <<SelectLineEnd>> <Shift-End>
  413. event add <<PrevLine>> <Up>
  414. event add <<NextLine>> <Down>
  415. event add <<SelectPrevLine>> <Shift-Up>
  416. event add <<SelectNextLine>> <Shift-Down>
  417. event add <<PrevPara>> <Control-Up>
  418. event add <<NextPara>> <Control-Down>
  419. event add <<SelectPrevPara>> <Control-Shift-Up>
  420. event add <<SelectNextPara>> <Control-Shift-Down>
  421. event add <<ToggleSelection>> <Control-Button-1>
  422. }
  423. "aqua" {
  424. event add <<Cut>> <Command-x> <F2> <Command-Lock-X>
  425. event add <<Copy>> <Command-c> <F3> <Command-Lock-C>
  426. event add <<Paste>> <Command-v> <F4> <Command-Lock-V>
  427. event add <<Clear>> <Clear>
  428. # Official bindings
  429. # See https://support.apple.com/en-us/HT201236
  430. event add <<SelectAll>> <Command-a>
  431. event add <<Undo>> <Command-Key-z> <Command-Lock-Key-Z>
  432. event add <<Redo>> <Shift-Command-Key-z> <Shift-Command-Lock-Key-z>
  433. event add <<NextChar>> <Right> <Control-Key-f> <Control-Lock-Key-F>
  434. event add <<SelectNextChar>> <Shift-Right> <Shift-Control-Key-F> <Shift-Control-Lock-Key-F>
  435. event add <<PrevChar>> <Left> <Control-Key-b> <Control-Lock-Key-B>
  436. event add <<SelectPrevChar>> <Shift-Left> <Shift-Control-Key-B> <Shift-Control-Lock-Key-B>
  437. event add <<NextWord>> <Option-Right>
  438. event add <<SelectNextWord>> <Shift-Option-Right>
  439. event add <<PrevWord>> <Option-Left>
  440. event add <<SelectPrevWord>> <Shift-Option-Left>
  441. event add <<LineStart>> <Home> <Command-Left> <Control-a> <Control-Lock-A>
  442. event add <<SelectLineStart>> <Shift-Home> <Shift-Command-Left> <Shift-Control-A> <Shift-Control-Lock-A>
  443. event add <<LineEnd>> <End> <Command-Right> <Control-e> <Control-Lock-E>
  444. event add <<SelectLineEnd>> <Shift-End> <Shift-Command-Right> <Shift-Control-E> <Shift-Control-Lock-E>
  445. event add <<PrevLine>> <Up> <Control-p> <Control-Lock-P>
  446. event add <<SelectPrevLine>> <Shift-Up> <Shift-Control-P> <Shift-Control-Lock-P>
  447. event add <<NextLine>> <Down> <Control-n> <Control-Lock-N>
  448. event add <<SelectNextLine>> <Shift-Down> <Shift-Control-N> <Shift-Control-Lock-N>
  449. # Not official, but logical extensions of above. Also derived from
  450. # bindings present in MS Word on OSX.
  451. event add <<PrevPara>> <Option-Up>
  452. event add <<NextPara>> <Option-Down>
  453. event add <<SelectPrevPara>> <Shift-Option-Up>
  454. event add <<SelectNextPara>> <Shift-Option-Down>
  455. event add <<ToggleSelection>> <Command-Button-1>
  456. }
  457. }
  458. # ----------------------------------------------------------------------
  459. # Read in files that define all of the class bindings.
  460. # ----------------------------------------------------------------------
  461. if {$::tk_library ne ""} {
  462. proc ::tk::SourceLibFile {file} {
  463. namespace eval :: [list source [file join $::tk_library $file.tcl]]
  464. }
  465. namespace eval ::tk {
  466. SourceLibFile icons
  467. SourceLibFile iconbadges
  468. SourceLibFile button
  469. SourceLibFile entry
  470. SourceLibFile listbox
  471. SourceLibFile menu
  472. SourceLibFile panedwindow
  473. SourceLibFile print
  474. SourceLibFile scale
  475. SourceLibFile scrlbar
  476. SourceLibFile spinbox
  477. if {![interp issafe]} {
  478. SourceLibFile systray
  479. }
  480. SourceLibFile text
  481. }
  482. }
  483. # ----------------------------------------------------------------------
  484. # Default bindings for keyboard traversal.
  485. # ----------------------------------------------------------------------
  486. event add <<PrevWindow>> <Shift-Tab>
  487. event add <<NextWindow>> <Tab>
  488. bind all <<NextWindow>> {tk::TabToWindow [tk_focusNext %W]}
  489. bind all <<PrevWindow>> {tk::TabToWindow [tk_focusPrev %W]}
  490. # ::tk::CancelRepeat --
  491. # This procedure is invoked to cancel an auto-repeat action described
  492. # by ::tk::Priv(afterId). It's used by several widgets to auto-scroll
  493. # the widget when the mouse is dragged out of the widget with a
  494. # button pressed.
  495. #
  496. # Arguments:
  497. # None.
  498. proc ::tk::CancelRepeat {} {
  499. variable ::tk::Priv
  500. after cancel $Priv(afterId)
  501. set Priv(afterId) {}
  502. }
  503. ## ::tk::MouseWheel $w $dir $amount $factor $units
  504. proc ::tk::MouseWheel {w dir amount {factor -120.0} {units units}} {
  505. $w ${dir}view scroll [expr {$amount/$factor}] $units
  506. }
  507. ## ::tk::PreciseScrollDeltas $dxdy
  508. proc ::tk::PreciseScrollDeltas {dxdy} {
  509. set deltaX [expr {$dxdy >> 16}]
  510. set low [expr {$dxdy & 0xffff}]
  511. set deltaY [expr {$low < 0x8000 ? $low : $low - 0x10000}]
  512. return [list $deltaX $deltaY]
  513. }
  514. ## Helper for smooth scrolling of widgets that support xview moveto and
  515. ## yview moveto.
  516. proc ::tk::ScrollByPixels {w deltaX deltaY} {
  517. set fracX [lindex [$w xview] 0]
  518. set fracY [lindex [$w yview] 0]
  519. set width [expr {1.0 * [winfo width $w]}]
  520. set height [expr {1.0 * [winfo height $w]}]
  521. $w xview moveto [expr {$fracX - $deltaX / $width}]
  522. $w yview moveto [expr {$fracY - $deltaY / $height}]
  523. }
  524. # ::tk::TabToWindow --
  525. # This procedure moves the focus to the given widget.
  526. # It sends a <<TraverseOut>> virtual event to the previous focus window,
  527. # if any, before changing the focus, and a <<TraverseIn>> event
  528. # to the new focus window afterwards.
  529. #
  530. # Arguments:
  531. # w - Window to which focus should be set.
  532. proc ::tk::TabToWindow {w} {
  533. set focus [focus]
  534. if {$focus ne ""} {
  535. event generate $focus <<TraverseOut>>
  536. }
  537. focus $w
  538. event generate $w <<TraverseIn>>
  539. }
  540. # ::tk::UnderlineAmpersand --
  541. # This procedure takes some text with ampersand and returns text w/o
  542. # ampersand and position of the ampersand. Double ampersands are
  543. # converted to single ones. Position returned is -1 when there is no
  544. # ampersand.
  545. #
  546. proc ::tk::UnderlineAmpersand {text} {
  547. set s [string map {&& & & \ufeff} $text]
  548. set idx [string first \ufeff $s]
  549. return [list [string map {\ufeff {}} $s] $idx]
  550. }
  551. # ::tk::SetAmpText --
  552. # Given widget path and text with "magic ampersands", sets -text and
  553. # -underline options for the widget
  554. #
  555. proc ::tk::SetAmpText {widget text} {
  556. lassign [UnderlineAmpersand $text] newtext under
  557. $widget configure -text $newtext -underline $under
  558. }
  559. # ::tk::AmpWidget --
  560. # Creates new widget, turning -text option into -text and -underline
  561. # options, returned by ::tk::UnderlineAmpersand.
  562. #
  563. proc ::tk::AmpWidget {class path args} {
  564. set options {}
  565. foreach {opt val} $args {
  566. if {$opt eq "-text"} {
  567. lassign [UnderlineAmpersand $val] newtext under
  568. lappend options -text $newtext -underline $under
  569. } else {
  570. lappend options $opt $val
  571. }
  572. }
  573. set result [$class $path {*}$options]
  574. if {[string match "*button" $class]} {
  575. bind $path <<AltUnderlined>> [list $path invoke]
  576. }
  577. return $result
  578. }
  579. # ::tk::AmpMenuArgs --
  580. # Processes arguments for a menu entry, turning -label option into
  581. # -label and -underline options, returned by ::tk::UnderlineAmpersand.
  582. # The cmd argument is supposed to be either "add" or "entryconfigure"
  583. #
  584. proc ::tk::AmpMenuArgs {widget cmd type args} {
  585. set options {}
  586. foreach {opt val} $args {
  587. if {$opt eq "-label"} {
  588. lassign [UnderlineAmpersand $val] newlabel under
  589. lappend options -label $newlabel -underline $under
  590. } else {
  591. lappend options $opt $val
  592. }
  593. }
  594. $widget $cmd $type {*}$options
  595. }
  596. # ::tk::FindAltKeyTarget --
  597. # Search recursively through the hierarchy of visible widgets to find
  598. # button or label which has $char as underlined character.
  599. #
  600. proc ::tk::FindAltKeyTarget {path char} {
  601. set class [winfo class $path]
  602. if {$class in {
  603. Button Checkbutton Label Radiobutton
  604. TButton TCheckbutton TLabel TRadiobutton
  605. } && ([$path cget -underline] >= 0) && [string equal -nocase $char \
  606. [string index [$path cget -text] [$path cget -underline]]]} {
  607. return $path
  608. }
  609. set subwins [concat [grid content $path] [pack content $path] \
  610. [place content $path]]
  611. if {$class eq "Canvas"} {
  612. foreach item [$path find all] {
  613. if {[$path type $item] eq "window"} {
  614. set w [$path itemcget $item -window]
  615. if {$w ne ""} {lappend subwins $w}
  616. }
  617. }
  618. } elseif {$class eq "Text"} {
  619. lappend subwins {*}[$path window names]
  620. }
  621. foreach child $subwins {
  622. set target [FindAltKeyTarget $child $char]
  623. if {$target ne ""} {
  624. return $target
  625. }
  626. }
  627. }
  628. # ::tk::AltKeyInDialog --
  629. # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>>
  630. # to button or label which has appropriate underlined character.
  631. #
  632. proc ::tk::AltKeyInDialog {path key} {
  633. set target [FindAltKeyTarget $path $key]
  634. if {$target ne ""} {
  635. event generate $target <<AltUnderlined>>
  636. }
  637. }
  638. # ::tk::mcmaxamp --
  639. # Replacement for mcmax, used for texts with "magic ampersand" in it.
  640. #
  641. proc ::tk::mcmaxamp {args} {
  642. set maxlen 0
  643. foreach arg $args {
  644. # Should we run [mc] in caller's namespace?
  645. lassign [UnderlineAmpersand [mc $arg]] msg
  646. set length [string length $msg]
  647. if {$length > $maxlen} {
  648. set maxlen $length
  649. }
  650. }
  651. return $maxlen
  652. }
  653. if {[tk windowingsystem] eq "aqua"} {
  654. #stub procedures to respond to "do script" Apple Events
  655. proc ::tk::mac::DoScriptFile {file} {
  656. uplevel #0 $file
  657. source $file
  658. }
  659. proc ::tk::mac::DoScriptText {script} {
  660. uplevel #0 $script
  661. eval $script
  662. }
  663. #This procedure is required to silence warnings generated
  664. #by inline AppleScript execution.
  665. proc ::tk::mac::GetDynamicSdef {} {
  666. puts ""
  667. }
  668. }
  669. if {[info commands ::tk::endOfWord] eq ""} {
  670. proc ::tk::endOfWord {str start {locale {}}} {
  671. if {$start < 0} {
  672. set start -1
  673. }
  674. set start [tcl_endOfWord $str $start]
  675. if {$start < 0} {
  676. set start ""
  677. }
  678. return $start
  679. }
  680. }
  681. if {[info commands ::tk::startOfNextWord] eq ""} {
  682. proc ::tk::startOfNextWord {str start {locale {}}} {
  683. if {$start < 0} {
  684. set start -1
  685. }
  686. set start [tcl_startOfNextWord $str $start]
  687. if {$start < 0} {
  688. set start ""
  689. }
  690. return $start
  691. }
  692. }
  693. if {[info commands ::tk::startOfPreviousWord] eq ""} {
  694. proc ::tk::startOfPreviousWord {str start {locale {}}} {
  695. if {$start < 0} {
  696. set start -1
  697. }
  698. set start [tcl_startOfPreviousWord $str $start]
  699. if {$start < 0} {
  700. set start ""
  701. }
  702. return $start
  703. }
  704. }
  705. if {[info commands ::tk::wordBreakBefore] eq ""} {
  706. proc ::tk::wordBreakBefore {str start {locale {}}} {
  707. if {$start < 0} {
  708. set start -1
  709. }
  710. set start [tcl_wordBreakBefore $str $start]
  711. if {$start < 0} {
  712. set start ""
  713. }
  714. return $start
  715. }
  716. }
  717. if {[info commands ::tk::wordBreakAfter] eq ""} {
  718. proc ::tk::wordBreakAfter {str start {locale {}}} {
  719. if {$start < 0} {
  720. set start -1
  721. }
  722. set start [tcl_wordBreakAfter $str $start]
  723. if {$start < 0} {
  724. set start ""
  725. }
  726. return $start
  727. }
  728. }
  729. if {[info commands ::tk::endOfCluster] eq ""} {
  730. proc ::tk::endOfCluster {str start {locale {}}} {
  731. if {$start < 0} {
  732. set start -1
  733. } elseif {$start eq "end"} {
  734. set start [expr {[string length $str]-1}]
  735. } elseif {[string match end-* $start]} {
  736. set start [expr {[string length $str]-1-[string range $start 4 end]}]
  737. } elseif {$start >= [string length $str]} {
  738. return ""
  739. }
  740. incr start
  741. return $start
  742. }
  743. }
  744. if {[info commands ::tk::startOfCluster] eq ""} {
  745. proc ::tk::startOfCluster {str start {locale {}}} {
  746. if {$start < 0} {
  747. set start -1
  748. } elseif {$start eq "end"} {
  749. set start [expr {[string length $str]-1}]
  750. } elseif {[string match end-* $start]} {
  751. set start [expr {[string length $str]-1-[string range $start 4 end]}]
  752. } elseif {$start >= [string length $str]} {
  753. return [string length $str]
  754. }
  755. if {$start < 0} {
  756. return ""
  757. }
  758. return $start
  759. }
  760. }
  761. # Create a dictionary to store the starting index of the IME marked
  762. # text in an Entry or Text widget.
  763. set ::tk::Priv(IMETextMark) [dict create]
  764. # Scale the default parameters of the panedwindow sash
  765. option add *Panedwindow.handlePad 6p widgetDefault
  766. option add *Panedwindow.handleSize 6p widgetDefault
  767. option add *Panedwindow.sashWidth 2.25p widgetDefault
  768. # Scale the default size of the scale widget and its slider
  769. option add *Scale.length 75p widgetDefault
  770. option add *Scale.sliderLength 22.5p widgetDefault
  771. option add *Scale.width 11.25p widgetDefault
  772. # Scale the default scrollbar width on X11
  773. if {[tk windowingsystem] eq "x11"} {
  774. option add *Scrollbar.width 8.25p widgetDefault
  775. }
  776. # Run the Ttk themed widget set initialization
  777. if {$::ttk::library ne ""} {
  778. uplevel \#0 [list source $::ttk::library/ttk.tcl]
  779. }
  780. # Local Variables:
  781. # mode: tcl
  782. # fill-column: 78
  783. # End: