menu.tcl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. # menu.tcl --
  2. #
  3. # This file defines the default bindings for Tk menus and menubuttons.
  4. # It also implements keyboard traversal of menus and implements a few
  5. # other utility procedures related to menus.
  6. #
  7. # Copyright © 1992-1994 The Regents of the University of California.
  8. # Copyright © 1994-1997 Sun Microsystems, Inc.
  9. # Copyright © 1998-1999 Scriptics Corporation.
  10. # Copyright © 2007 Daniel A. Steffen <das@users.sourceforge.net>
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15. #-------------------------------------------------------------------------
  16. # Elements of tk::Priv that are used in this file:
  17. #
  18. # cursor - Saves the -cursor option for the posted menubutton.
  19. # focus - Saves the focus during a menu selection operation.
  20. # Focus gets restored here when the menu is unposted.
  21. # grabGlobal - Used in conjunction with tk::Priv(oldGrab): if
  22. # tk::Priv(oldGrab) is non-empty, then tk::Priv(grabGlobal)
  23. # contains either an empty string or "-global" to
  24. # indicate whether the old grab was a local one or
  25. # a global one.
  26. # inMenubutton - The name of the menubutton widget containing
  27. # the mouse, or an empty string if the mouse is
  28. # not over any menubutton.
  29. # menuBar - The name of the menubar that is the root
  30. # of the cascade hierarchy which is currently
  31. # posted. This is null when there is no menu currently
  32. # being pulled down from a menu bar.
  33. # oldGrab - Window that had the grab before a menu was posted.
  34. # Used to restore the grab state after the menu
  35. # is unposted. Empty string means there was no
  36. # grab previously set.
  37. # popup - If a menu has been popped up via tk_popup, this
  38. # gives the name of the menu. Otherwise this
  39. # value is empty.
  40. # postedMb - Name of the menubutton whose menu is currently
  41. # posted, or an empty string if nothing is posted
  42. # A grab is set on this widget.
  43. # relief - Used to save the original relief of the current
  44. # menubutton.
  45. # window - When the mouse is over a menu, this holds the
  46. # name of the menu; it's cleared when the mouse
  47. # leaves the menu.
  48. # tearoff - Whether the last menu posted was a tearoff or not.
  49. # This is true always for unix, for tearoffs for Mac
  50. # and Windows.
  51. # activeMenu - This is the last active menu for use
  52. # with the <<MenuSelect>> virtual event.
  53. # activeItem - This is the last active menu item for
  54. # use with the <<MenuSelect>> virtual event.
  55. #-------------------------------------------------------------------------
  56. #-------------------------------------------------------------------------
  57. # Overall note:
  58. # This file is tricky because there are five different ways that menus
  59. # can be used:
  60. #
  61. # 1. As a pulldown from a menubutton. In this style, the variable
  62. # tk::Priv(postedMb) identifies the posted menubutton.
  63. # 2. As a torn-off menu copied from some other menu. In this style
  64. # tk::Priv(postedMb) is empty, and menu's type is "tearoff".
  65. # 3. As an option menu, triggered from an option menubutton. In this
  66. # style tk::Priv(postedMb) identifies the posted menubutton.
  67. # 4. As a popup menu. In this style tk::Priv(postedMb) is empty and
  68. # the top-level menu's type is "normal".
  69. # 5. As a pulldown from a menubar. The variable tk::Priv(menubar) has
  70. # the owning menubar, and the menu itself is of type "normal".
  71. #
  72. # The various binding procedures use the state described above to
  73. # distinguish the various cases and take different actions in each
  74. # case.
  75. #-------------------------------------------------------------------------
  76. #-------------------------------------------------------------------------
  77. # The code below creates the default class bindings for menus
  78. # and menubuttons.
  79. #-------------------------------------------------------------------------
  80. bind Menubutton <FocusIn> {}
  81. bind Menubutton <Enter> {
  82. tk::MbEnter %W
  83. }
  84. bind Menubutton <Leave> {
  85. tk::MbLeave %W
  86. }
  87. bind Menubutton <Button-1> {
  88. if {$tk::Priv(inMenubutton) ne ""} {
  89. tk::MbPost $tk::Priv(inMenubutton) %X %Y
  90. }
  91. }
  92. bind Menubutton <Motion> {
  93. tk::MbMotion %W up %X %Y
  94. }
  95. bind Menubutton <B1-Motion> {
  96. tk::MbMotion %W down %X %Y
  97. }
  98. bind Menubutton <ButtonRelease-1> {
  99. tk::MbButtonUp %W
  100. }
  101. bind Menubutton <space> {
  102. tk::MbPost %W
  103. tk::MenuFirstEntry [%W cget -menu]
  104. }
  105. bind Menubutton <<Invoke>> {
  106. tk::MbPost %W
  107. tk::MenuFirstEntry [%W cget -menu]
  108. }
  109. # Must set focus when mouse enters a menu, in order to allow
  110. # mixed-mode processing using both the mouse and the keyboard.
  111. # Don't set the focus if the event comes from a grab release,
  112. # though: such an event can happen after as part of unposting
  113. # a cascaded chain of menus, after the focus has already been
  114. # restored to wherever it was before menu selection started.
  115. bind Menu <FocusIn> {}
  116. bind Menu <Enter> {
  117. set tk::Priv(window) %W
  118. if {[%W cget -type] eq "tearoff"} {
  119. if {"%m" ne "NotifyUngrab"} {
  120. if {[tk windowingsystem] eq "x11"} {
  121. tk_menuSetFocus %W
  122. }
  123. }
  124. }
  125. tk::MenuMotion %W %x %y %s
  126. }
  127. bind Menu <Leave> {
  128. tk::MenuLeave %W %X %Y %s
  129. }
  130. bind Menu <Motion> {
  131. tk::MenuMotion %W %x %y %s
  132. }
  133. bind Menu <Button> {
  134. tk::MenuButtonDown %W
  135. }
  136. bind Menu <ButtonRelease> {
  137. tk::MenuInvoke %W 1
  138. }
  139. bind Menu <space> {
  140. tk::MenuInvoke %W 0
  141. }
  142. bind Menu <<Invoke>> {
  143. tk::MenuInvoke %W 0
  144. }
  145. bind Menu <Return> {
  146. tk::MenuInvoke %W 0
  147. }
  148. bind Menu <Escape> {
  149. tk::MenuEscape %W
  150. }
  151. bind Menu <<PrevChar>> {
  152. tk::MenuLeftArrow %W
  153. }
  154. bind Menu <<NextChar>> {
  155. tk::MenuRightArrow %W
  156. }
  157. bind Menu <<PrevLine>> {
  158. tk::MenuUpArrow %W
  159. }
  160. bind Menu <<NextLine>> {
  161. tk::MenuDownArrow %W
  162. }
  163. bind Menu <Key> {
  164. tk::TraverseWithinMenu %W %A
  165. break
  166. }
  167. # The following bindings apply to all windows, and are used to
  168. # implement keyboard menu traversal.
  169. if {[tk windowingsystem] eq "x11"} {
  170. bind all <Alt-Key> {
  171. tk::TraverseToMenu %W %A
  172. }
  173. bind all <F10> {
  174. tk::FirstMenu %W
  175. }
  176. } else {
  177. bind Menubutton <Alt-Key> {
  178. tk::TraverseToMenu %W %A
  179. }
  180. bind Menubutton <F10> {
  181. tk::FirstMenu %W
  182. }
  183. }
  184. # ::tk::MbEnter --
  185. # This procedure is invoked when the mouse enters a menubutton
  186. # widget. It activates the widget unless it is disabled. Note:
  187. # this procedure is only invoked when mouse button 1 is *not* down.
  188. # The procedure ::tk::MbB1Enter is invoked if the button is down.
  189. #
  190. # Arguments:
  191. # w - The name of the widget.
  192. proc ::tk::MbEnter w {
  193. variable ::tk::Priv
  194. if {$Priv(inMenubutton) ne ""} {
  195. MbLeave $Priv(inMenubutton)
  196. }
  197. set Priv(inMenubutton) $w
  198. if {[$w cget -state] ne "disabled" && [tk windowingsystem] ne "aqua"} {
  199. $w configure -state active
  200. }
  201. }
  202. # ::tk::MbLeave --
  203. # This procedure is invoked when the mouse leaves a menubutton widget.
  204. # It de-activates the widget, if the widget still exists.
  205. #
  206. # Arguments:
  207. # w - The name of the widget.
  208. proc ::tk::MbLeave w {
  209. variable ::tk::Priv
  210. set Priv(inMenubutton) {}
  211. if {![winfo exists $w]} {
  212. return
  213. }
  214. if {[$w cget -state] eq "active" && [tk windowingsystem] ne "aqua"} {
  215. $w configure -state normal
  216. }
  217. }
  218. # ::tk::MbPost --
  219. # Given a menubutton, this procedure does all the work of posting
  220. # its associated menu and unposting any other menu that is currently
  221. # posted.
  222. #
  223. # Arguments:
  224. # w - The name of the menubutton widget whose menu
  225. # is to be posted.
  226. # x, y - Root coordinates of cursor, used for positioning
  227. # option menus. If not specified, then the center
  228. # of the menubutton is used for an option menu.
  229. proc ::tk::MbPost {w {x {}} {y {}}} {
  230. global errorInfo
  231. variable ::tk::Priv
  232. if {[$w cget -state] eq "disabled" || $w eq $Priv(postedMb)} {
  233. return
  234. }
  235. set menu [$w cget -menu]
  236. if {$menu eq ""} {
  237. return
  238. }
  239. set tearoff [expr {[tk windowingsystem] eq "x11" \
  240. || [$menu cget -type] eq "tearoff"}]
  241. if {[string first $w $menu] != 0} {
  242. return -code error -errorcode {TK MENUBUTTON POST_NONCHILD} \
  243. "can't post $menu: it isn't a descendant of $w"
  244. }
  245. set cur $Priv(postedMb)
  246. if {$cur ne ""} {
  247. MenuUnpost {}
  248. }
  249. if {$::tk_strictMotif} {
  250. set Priv(cursor) [$w cget -cursor]
  251. $w configure -cursor arrow
  252. }
  253. if {[tk windowingsystem] ne "aqua"} {
  254. set Priv(relief) [$w cget -relief]
  255. $w configure -relief raised
  256. } else {
  257. $w configure -state active
  258. }
  259. set Priv(postedMb) $w
  260. set Priv(focus) [focus]
  261. $menu activate {}
  262. GenerateMenuSelect $menu
  263. update idletasks
  264. if {[catch {PostMenubuttonMenu $w $menu $x $y} msg opt]} {
  265. # Error posting menu (e.g. bogus -postcommand). Unpost it and
  266. # reflect the error.
  267. MenuUnpost {}
  268. return -options $opt $msg
  269. }
  270. set Priv(tearoff) $tearoff
  271. if {$tearoff != 0 && [tk windowingsystem] ne "aqua"} {
  272. focus $menu
  273. if {[winfo viewable $w]} {
  274. SaveGrabInfo $w
  275. grab -global $w
  276. }
  277. }
  278. }
  279. # ::tk::MenuUnpost --
  280. # This procedure unposts a given menu, plus all of its ancestors up
  281. # to (and including) a menubutton, if any. It also restores various
  282. # values to what they were before the menu was posted, and releases
  283. # a grab if there's a menubutton involved. Special notes:
  284. # 1. It's important to unpost all menus before releasing the grab, so
  285. # that any Enter-Leave events (e.g. from menu back to main
  286. # application) have mode NotifyGrab.
  287. # 2. Be sure to enclose various groups of commands in "catch" so that
  288. # the procedure will complete even if the menubutton or the menu
  289. # or the grab window has been deleted.
  290. #
  291. # Arguments:
  292. # menu - Name of a menu to unpost. Ignored if there
  293. # is a posted menubutton.
  294. proc ::tk::MenuUnpost menu {
  295. variable ::tk::Priv
  296. set mb $Priv(postedMb)
  297. # Restore focus right away (otherwise X will take focus away when
  298. # the menu is unmapped and under some window managers (e.g. olvwm)
  299. # we'll lose the focus completely).
  300. catch {focus $Priv(focus)}
  301. set Priv(focus) ""
  302. # Unpost menu(s) and restore some stuff that's dependent on
  303. # what was posted.
  304. after cancel [array get Priv menuActivatedTimer]
  305. unset -nocomplain Priv(menuActivated)
  306. after cancel [array get Priv menuDeactivatedTimer]
  307. unset -nocomplain Priv(menuDeactivated)
  308. catch {
  309. if {$mb ne ""} {
  310. set menu [$mb cget -menu]
  311. $menu unpost
  312. set Priv(postedMb) {}
  313. if {$::tk_strictMotif} {
  314. $mb configure -cursor $Priv(cursor)
  315. }
  316. if {[tk windowingsystem] ne "aqua"} {
  317. $mb configure -relief $Priv(relief)
  318. } else {
  319. $mb configure -state normal
  320. }
  321. } elseif {$Priv(popup) ne ""} {
  322. $Priv(popup) unpost
  323. set Priv(popup) {}
  324. } elseif {[$menu cget -type] ne "menubar" && [$menu cget -type] ne "tearoff"} {
  325. # We're in a cascaded sub-menu from a torn-off menu or popup.
  326. # Unpost all the menus up to the toplevel one (but not
  327. # including the top-level torn-off one) and deactivate the
  328. # top-level torn off menu if there is one.
  329. while {1} {
  330. set parent [winfo parent $menu]
  331. if {[winfo class $parent] ne "Menu" || ![winfo ismapped $parent]} {
  332. break
  333. }
  334. $parent activate {}
  335. $parent postcascade {}
  336. GenerateMenuSelect $parent
  337. set type [$parent cget -type]
  338. if {$type eq "menubar" || $type eq "tearoff"} {
  339. break
  340. }
  341. set menu $parent
  342. }
  343. if {[$menu cget -type] ne "menubar"} {
  344. $menu unpost
  345. }
  346. }
  347. }
  348. if {($Priv(tearoff) != 0) || $Priv(menuBar) ne ""} {
  349. # Release grab, if any, and restore the previous grab, if there
  350. # was one.
  351. if {$menu ne ""} {
  352. set grab [grab current $menu]
  353. if {$grab ne ""} {
  354. grab release $grab
  355. }
  356. }
  357. RestoreOldGrab
  358. if {$Priv(menuBar) ne ""} {
  359. if {$::tk_strictMotif} {
  360. $Priv(menuBar) configure -cursor $Priv(cursor)
  361. }
  362. set Priv(menuBar) {}
  363. }
  364. if {[tk windowingsystem] ne "x11"} {
  365. set Priv(tearoff) 0
  366. }
  367. }
  368. }
  369. # ::tk::MbMotion --
  370. # This procedure handles mouse motion events inside menubuttons, and
  371. # also outside menubuttons when a menubutton has a grab (e.g. when a
  372. # menu selection operation is in progress).
  373. #
  374. # Arguments:
  375. # w - The name of the menubutton widget.
  376. # upDown - "down" means button 1 is pressed, "up" means
  377. # it isn't.
  378. # rootx, rooty - Coordinates of mouse, in (virtual?) root window.
  379. proc ::tk::MbMotion {w upDown rootx rooty} {
  380. variable ::tk::Priv
  381. if {$Priv(inMenubutton) eq $w} {
  382. return
  383. }
  384. set new [winfo containing $rootx $rooty]
  385. if {$new ne $Priv(inMenubutton) \
  386. && ($new eq "" || [winfo toplevel $new] eq [winfo toplevel $w])} {
  387. if {$Priv(inMenubutton) ne ""} {
  388. MbLeave $Priv(inMenubutton)
  389. }
  390. if {$new ne "" \
  391. && [winfo class $new] eq "Menubutton" \
  392. && ([$new cget -indicatoron] == 0) \
  393. && ([$w cget -indicatoron] == 0)} {
  394. if {$upDown eq "down"} {
  395. MbPost $new $rootx $rooty
  396. } else {
  397. MbEnter $new
  398. }
  399. }
  400. }
  401. }
  402. # ::tk::MbButtonUp --
  403. # This procedure is invoked to handle button 1 releases for menubuttons.
  404. # If the release happens inside the menubutton then leave its menu
  405. # posted with element 0 activated. Otherwise, unpost the menu.
  406. #
  407. # Arguments:
  408. # w - The name of the menubutton widget.
  409. proc ::tk::MbButtonUp w {
  410. variable ::tk::Priv
  411. set menu [$w cget -menu]
  412. set tearoff [expr {[tk windowingsystem] eq "x11" || \
  413. ($menu ne "" && [$menu cget -type] eq "tearoff")}]
  414. if {($tearoff != 0) && $Priv(postedMb) eq $w \
  415. && $Priv(inMenubutton) eq $w} {
  416. MenuFirstEntry [$Priv(postedMb) cget -menu]
  417. } else {
  418. MenuUnpost {}
  419. }
  420. }
  421. # ::tk::MenuMotion --
  422. # This procedure is called to handle mouse motion events for menus.
  423. # It does two things. First, it resets the active element in the
  424. # menu, if the mouse is over the menu. Second, if a mouse button
  425. # is down, it posts and unposts cascade entries to match the mouse
  426. # position.
  427. #
  428. # Arguments:
  429. # menu - The menu window.
  430. # x - The x position of the mouse.
  431. # y - The y position of the mouse.
  432. # state - Modifier state (tells whether buttons are down).
  433. proc ::tk::MenuMotion {menu x y state} {
  434. variable ::tk::Priv
  435. if {$menu eq $Priv(window)} {
  436. set activeindex [$menu index active]
  437. if {[$menu cget -type] eq "menubar"} {
  438. if {[info exists Priv(focus)] && $menu ne $Priv(focus)} {
  439. $menu activate @$x,$y
  440. GenerateMenuSelect $menu
  441. }
  442. } else {
  443. $menu activate @$x,$y
  444. GenerateMenuSelect $menu
  445. }
  446. set index [$menu index @$x,$y]
  447. if {[info exists Priv(menuActivated)] \
  448. && $index >= 0 \
  449. && $index ne $activeindex} {
  450. set mode [option get $menu clickToFocus ClickToFocus]
  451. if {[string is false $mode]} {
  452. set delay [expr {[$menu cget -type] eq "menubar" ? 0 : 50}]
  453. if {[$menu type $index] eq "cascade"} {
  454. # Catch these postcascade commands since the menu could be
  455. # destroyed before they run.
  456. set Priv(menuActivatedTimer) \
  457. [after $delay [list catch [list \
  458. $menu postcascade active]]]
  459. } else {
  460. set Priv(menuDeactivatedTimer) \
  461. [after $delay [list catch [list
  462. $menu postcascade {}]]]
  463. }
  464. }
  465. }
  466. }
  467. }
  468. # ::tk::MenuButtonDown --
  469. # Handles button presses in menus. There are a couple of tricky things
  470. # here:
  471. # 1. Change the posted cascade entry (if any) to match the mouse position.
  472. # 2. If there is a posted menubutton, must grab to the menubutton; this
  473. # overrrides the implicit grab on button press, so that the menu
  474. # button can track mouse motions over other menubuttons and change
  475. # the posted menu.
  476. # 3. If there's no posted menubutton (e.g. because we're a torn-off menu
  477. # or one of its descendants) must grab to the top-level menu so that
  478. # we can track mouse motions across the entire menu hierarchy.
  479. #
  480. # Arguments:
  481. # menu - The menu window.
  482. proc ::tk::MenuButtonDown menu {
  483. variable ::tk::Priv
  484. if {![winfo viewable $menu]} {
  485. return
  486. }
  487. if {[$menu index active] < 0} {
  488. if {[$menu cget -type] ne "menubar" } {
  489. set Priv(window) {}
  490. }
  491. return
  492. }
  493. $menu postcascade active
  494. if {$Priv(postedMb) ne "" && [winfo viewable $Priv(postedMb)]} {
  495. grab -global $Priv(postedMb)
  496. } else {
  497. while {[$menu cget -type] eq "normal" \
  498. && [winfo class [winfo parent $menu]] eq "Menu" \
  499. && [winfo ismapped [winfo parent $menu]]} {
  500. set menu [winfo parent $menu]
  501. }
  502. if {$Priv(menuBar) eq {}} {
  503. set Priv(menuBar) $menu
  504. if {$::tk_strictMotif} {
  505. set Priv(cursor) [$menu cget -cursor]
  506. $menu configure -cursor arrow
  507. }
  508. if {[$menu type active] eq "cascade"} {
  509. set Priv(menuActivated) 1
  510. }
  511. }
  512. # Don't update grab information if the grab window isn't changing.
  513. # Otherwise, we'll get an error when we unpost the menus and
  514. # restore the grab, since the old grab window will not be viewable
  515. # anymore.
  516. if {$menu ne [grab current $menu]} {
  517. SaveGrabInfo $menu
  518. }
  519. # Must re-grab even if the grab window hasn't changed, in order
  520. # to release the implicit grab from the button press.
  521. if {[tk windowingsystem] eq "x11"} {
  522. grab -global $menu
  523. }
  524. }
  525. }
  526. # ::tk::MenuLeave --
  527. # This procedure is invoked to handle Leave events for a menu. It
  528. # deactivates everything unless the active element is a cascade element
  529. # and the mouse is now over the submenu.
  530. #
  531. # Arguments:
  532. # menu - The menu window.
  533. # rootx, rooty - Root coordinates of mouse.
  534. # state - Modifier state.
  535. proc ::tk::MenuLeave {menu rootx rooty state} {
  536. variable ::tk::Priv
  537. set Priv(window) {}
  538. if {[$menu index active] < 0} {
  539. return
  540. }
  541. if {[$menu type active] eq "cascade" \
  542. && [winfo containing $rootx $rooty] eq \
  543. [$menu entrycget active -menu]} {
  544. return
  545. }
  546. $menu activate {}
  547. GenerateMenuSelect $menu
  548. }
  549. # ::tk::MenuInvoke --
  550. # This procedure is invoked when button 1 is released over a menu.
  551. # It invokes the appropriate menu action and unposts the menu if
  552. # it came from a menubutton.
  553. #
  554. # Arguments:
  555. # w - Name of the menu widget.
  556. # buttonRelease - 1 means this procedure is called because of
  557. # a button release; 0 means because of keystroke.
  558. proc ::tk::MenuInvoke {w buttonRelease} {
  559. variable ::tk::Priv
  560. if {$buttonRelease && $Priv(window) eq ""} {
  561. # Mouse was pressed over a menu without a menu button, then
  562. # dragged off the menu (possibly with a cascade posted) and
  563. # released. Unpost everything and quit.
  564. $w postcascade {}
  565. $w activate {}
  566. event generate $w <<MenuSelect>>
  567. MenuUnpost $w
  568. return
  569. }
  570. if {[$w type active] eq "cascade"} {
  571. $w postcascade active
  572. set menu [$w entrycget active -menu]
  573. MenuFirstEntry $menu
  574. } elseif {[$w type active] eq "tearoff"} {
  575. ::tk::TearOffMenu $w
  576. MenuUnpost $w
  577. } elseif {[$w cget -type] eq "menubar"} {
  578. $w postcascade {}
  579. set activeindex [$w index active]
  580. set isCascade [string equal [$w type $activeindex] "cascade"]
  581. # Only de-activate the active item if it's a cascade; this prevents
  582. # the annoying "activation flicker" you otherwise get with
  583. # checkbuttons/commands/etc. on menubars
  584. if { $isCascade } {
  585. $w activate {}
  586. event generate $w <<MenuSelect>>
  587. }
  588. MenuUnpost $w
  589. # If the active item is not a cascade, invoke it. This enables
  590. # the use of checkbuttons/commands/etc. on menubars (which is legal,
  591. # but not recommended)
  592. if { !$isCascade } {
  593. uplevel #0 [list $w invoke $activeindex]
  594. }
  595. } else {
  596. if {$Priv(popup) eq "" || [$w index active] >= 0} {
  597. MenuUnpost $w
  598. }
  599. uplevel #0 [list $w invoke active]
  600. }
  601. }
  602. # ::tk::MenuEscape --
  603. # This procedure is invoked for the Cancel (or Escape) key. It unposts
  604. # the given menu and, if it is the top-level menu for a menu button,
  605. # unposts the menu button as well.
  606. #
  607. # Arguments:
  608. # menu - Name of the menu window.
  609. proc ::tk::MenuEscape menu {
  610. set parent [winfo parent $menu]
  611. if {[winfo class $parent] ne "Menu"} {
  612. MenuUnpost $menu
  613. } elseif {[$parent cget -type] eq "menubar"} {
  614. MenuUnpost $menu
  615. RestoreOldGrab
  616. } else {
  617. MenuNextMenu $menu left
  618. }
  619. }
  620. # The following routines handle arrow keys. Arrow keys behave
  621. # differently depending on whether the menu is a menu bar or not.
  622. proc ::tk::MenuUpArrow {menu} {
  623. if {[$menu cget -type] eq "menubar"} {
  624. MenuNextMenu $menu left
  625. } else {
  626. MenuNextEntry $menu -1
  627. }
  628. }
  629. proc ::tk::MenuDownArrow {menu} {
  630. if {[$menu cget -type] eq "menubar"} {
  631. MenuNextMenu $menu right
  632. } else {
  633. MenuNextEntry $menu 1
  634. }
  635. }
  636. proc ::tk::MenuLeftArrow {menu} {
  637. if {[$menu cget -type] eq "menubar"} {
  638. MenuNextEntry $menu -1
  639. } else {
  640. MenuNextMenu $menu left
  641. }
  642. }
  643. proc ::tk::MenuRightArrow {menu} {
  644. if {[$menu cget -type] eq "menubar"} {
  645. MenuNextEntry $menu 1
  646. } else {
  647. MenuNextMenu $menu right
  648. }
  649. }
  650. # ::tk::MenuNextMenu --
  651. # This procedure is invoked to handle "left" and "right" traversal
  652. # motions in menus. It traverses to the next menu in a menu bar,
  653. # or into or out of a cascaded menu.
  654. #
  655. # Arguments:
  656. # menu - The menu that received the keyboard
  657. # event.
  658. # direction - Direction in which to move: "left" or "right"
  659. proc ::tk::MenuNextMenu {menu direction} {
  660. variable ::tk::Priv
  661. # First handle traversals into and out of cascaded menus.
  662. if {$direction eq "right"} {
  663. set count 1
  664. set parent [winfo parent $menu]
  665. set class [winfo class $parent]
  666. if {[$menu type active] eq "cascade"} {
  667. $menu postcascade active
  668. set m2 [$menu entrycget active -menu]
  669. if {$m2 ne ""} {
  670. MenuFirstEntry $m2
  671. }
  672. return
  673. } else {
  674. set parent [winfo parent $menu]
  675. while {$parent ne "."} {
  676. if {[winfo class $parent] eq "Menu" \
  677. && [$parent cget -type] eq "menubar"} {
  678. tk_menuSetFocus $parent
  679. MenuNextEntry $parent 1
  680. return
  681. }
  682. set parent [winfo parent $parent]
  683. }
  684. }
  685. } else {
  686. set count -1
  687. set m2 [winfo parent $menu]
  688. if {[winfo class $m2] eq "Menu"} {
  689. $menu activate {}
  690. GenerateMenuSelect $menu
  691. tk_menuSetFocus $m2
  692. $m2 postcascade {}
  693. if {[$m2 cget -type] ne "menubar"} {
  694. return
  695. }
  696. }
  697. }
  698. # Can't traverse into or out of a cascaded menu. Go to the next
  699. # or previous menubutton, if that makes sense.
  700. set m2 [winfo parent $menu]
  701. if {[winfo class $m2] eq "Menu" && [$m2 cget -type] eq "menubar"} {
  702. tk_menuSetFocus $m2
  703. MenuNextEntry $m2 -1
  704. return
  705. }
  706. set w $Priv(postedMb)
  707. if {$w eq ""} {
  708. return
  709. }
  710. set buttons [winfo children [winfo parent $w]]
  711. set length [llength $buttons]
  712. set i [expr {[lsearch -exact $buttons $w] + $count}]
  713. while {1} {
  714. while {$i < 0} {
  715. incr i $length
  716. }
  717. while {$i >= $length} {
  718. incr i -$length
  719. }
  720. set mb [lindex $buttons $i]
  721. if {[winfo class $mb] eq "Menubutton" \
  722. && [$mb cget -state] ne "disabled" \
  723. && [$mb cget -menu] ne "" \
  724. && [[$mb cget -menu] index last] >= 0} {
  725. break
  726. }
  727. if {$mb eq $w} {
  728. return
  729. }
  730. incr i $count
  731. }
  732. MbPost $mb
  733. MenuFirstEntry [$mb cget -menu]
  734. }
  735. # ::tk::MenuNextEntry --
  736. # Activate the next higher or lower entry in the posted menu,
  737. # wrapping around at the ends. Disabled entries are skipped.
  738. #
  739. # Arguments:
  740. # menu - Menu window that received the keystroke.
  741. # count - 1 means go to the next lower entry,
  742. # -1 means go to the next higher entry.
  743. proc ::tk::MenuNextEntry {menu count} {
  744. set last [$menu index last]
  745. if {$last < 0} {
  746. return
  747. }
  748. set length [expr {$last+1}]
  749. set quitAfter $length
  750. set activeindex [$menu index active]
  751. if {$activeindex < 0} {
  752. set i 0
  753. } else {
  754. set i [expr {$activeindex + $count}]
  755. }
  756. while {1} {
  757. if {$quitAfter <= 0} {
  758. # We've tried every entry in the menu. Either there are
  759. # none, or they're all disabled. Just give up.
  760. return
  761. }
  762. while {$i < 0} {
  763. incr i $length
  764. }
  765. while {$i >= $length} {
  766. incr i -$length
  767. }
  768. if {[catch {$menu entrycget $i -state} state] == 0} {
  769. if {$state ne "disabled" && \
  770. ($i!=0 || [$menu cget -type] ne "tearoff" \
  771. || [$menu type 0] ne "tearoff")} {
  772. break
  773. }
  774. }
  775. if {$i == $activeindex} {
  776. return
  777. }
  778. incr i $count
  779. incr quitAfter -1
  780. }
  781. $menu activate $i
  782. GenerateMenuSelect $menu
  783. if {[$menu type $i] eq "cascade" && [$menu cget -type] eq "menubar"} {
  784. set cascade [$menu entrycget $i -menu]
  785. if {$cascade ne ""} {
  786. # Here we auto-post a cascade. This is necessary when
  787. # we traverse left/right in the menubar, but undesirable when
  788. # we traverse up/down in a menu.
  789. $menu postcascade $i
  790. MenuFirstEntry $cascade
  791. }
  792. }
  793. }
  794. # ::tk::MenuFind --
  795. # This procedure searches the entire window hierarchy under w for
  796. # a menubutton that isn't disabled and whose underlined character
  797. # is "char" or an entry in a menubar that isn't disabled and whose
  798. # underlined character is "char".
  799. # It returns the name of that window, if found, or an
  800. # empty string if no matching window was found. If "char" is an
  801. # empty string then the procedure returns the name of the first
  802. # menubutton found that isn't disabled.
  803. #
  804. # Arguments:
  805. # w - Name of window where key was typed.
  806. # char - Underlined character to search for;
  807. # may be either upper or lower case, and
  808. # will match either upper or lower case.
  809. proc ::tk::MenuFind {w char} {
  810. set char [string tolower $char]
  811. set windowlist [winfo child $w]
  812. foreach child $windowlist {
  813. # Don't descend into other toplevels.
  814. if {[winfo toplevel $w] ne [winfo toplevel $child]} {
  815. continue
  816. }
  817. if {[winfo class $child] eq "Menu" && \
  818. [$child cget -type] eq "menubar"} {
  819. if {$char eq ""} {
  820. return $child
  821. }
  822. set last [$child index last]
  823. for {set i [$child cget -tearoff]} {$i <= $last} {incr i} {
  824. if {([$child type $i] eq "separator") || ([$child entrycget $i -state] eq "disabled")} {
  825. continue
  826. }
  827. set underline [$child entrycget $i -underline]
  828. if {$underline >= 0} {
  829. if {$char eq [string tolower [string index [$child entrycget $i -label] $underline]]} {
  830. return $child
  831. }
  832. }
  833. }
  834. }
  835. }
  836. foreach child $windowlist {
  837. # Don't descend into other toplevels.
  838. if {[winfo toplevel $w] ne [winfo toplevel $child]} {
  839. continue
  840. }
  841. switch -- [winfo class $child] {
  842. Menubutton {
  843. set char2 [string index [$child cget -text] \
  844. [$child cget -underline]]
  845. if {$char eq [string tolower $char2] || $char eq ""} {
  846. if {[$child cget -state] ne "disabled"} {
  847. return $child
  848. }
  849. }
  850. }
  851. default {
  852. set match [MenuFind $child $char]
  853. if {$match ne ""} {
  854. return $match
  855. }
  856. }
  857. }
  858. }
  859. return {}
  860. }
  861. # ::tk::TraverseToMenu --
  862. # This procedure implements keyboard traversal of menus. Given an
  863. # ASCII character "char", it looks for a menubutton with that character
  864. # underlined. If one is found, it posts the menubutton's menu
  865. #
  866. # Arguments:
  867. # w - Window in which the key was typed (selects
  868. # a toplevel window).
  869. # char - Character that selects a menu. The case
  870. # is ignored. If an empty string, nothing
  871. # happens.
  872. proc ::tk::TraverseToMenu {w char} {
  873. variable ::tk::Priv
  874. if {![winfo exists $w] || $char eq ""} {
  875. return
  876. }
  877. while {[winfo class $w] eq "Menu"} {
  878. if {[$w cget -type] eq "menubar"} {
  879. break
  880. } elseif {$Priv(postedMb) eq ""} {
  881. return
  882. }
  883. set w [winfo parent $w]
  884. }
  885. set w [MenuFind [winfo toplevel $w] $char]
  886. if {$w ne ""} {
  887. if {[winfo class $w] eq "Menu"} {
  888. tk_menuSetFocus $w
  889. set Priv(window) $w
  890. SaveGrabInfo $w
  891. grab -global $w
  892. TraverseWithinMenu $w $char
  893. } else {
  894. MbPost $w
  895. MenuFirstEntry [$w cget -menu]
  896. }
  897. }
  898. }
  899. # ::tk::FirstMenu --
  900. # This procedure traverses to the first menubutton in the toplevel
  901. # for a given window, and posts that menubutton's menu.
  902. #
  903. # Arguments:
  904. # w - Name of a window. Selects which toplevel
  905. # to search for menubuttons.
  906. proc ::tk::FirstMenu w {
  907. variable ::tk::Priv
  908. set w [MenuFind [winfo toplevel $w] ""]
  909. if {$w ne ""} {
  910. if {[winfo class $w] eq "Menu"} {
  911. tk_menuSetFocus $w
  912. set Priv(window) $w
  913. SaveGrabInfo $w
  914. grab -global $w
  915. MenuFirstEntry $w
  916. } else {
  917. MbPost $w
  918. MenuFirstEntry [$w cget -menu]
  919. }
  920. }
  921. }
  922. # ::tk::TraverseWithinMenu
  923. # This procedure implements keyboard traversal within a menu. It
  924. # searches for an entry in the menu that has "char" underlined. If
  925. # such an entry is found, it is invoked and the menu is unposted.
  926. #
  927. # Arguments:
  928. # w - The name of the menu widget.
  929. # char - The character to look for; case is
  930. # ignored. If the string is empty then
  931. # nothing happens.
  932. proc ::tk::TraverseWithinMenu {w char} {
  933. if {$char eq ""} {
  934. return
  935. }
  936. set char [string tolower $char]
  937. set last [$w index last]
  938. for {set i 0} {$i <= $last} {incr i} {
  939. if {[catch {set char2 [string index \
  940. [$w entrycget $i -label] [$w entrycget $i -underline]]}]} {
  941. continue
  942. }
  943. if {$char eq [string tolower $char2]} {
  944. if {[$w type $i] eq "cascade"} {
  945. $w activate $i
  946. $w postcascade active
  947. event generate $w <<MenuSelect>>
  948. set m2 [$w entrycget $i -menu]
  949. if {$m2 ne ""} {
  950. MenuFirstEntry $m2
  951. }
  952. } else {
  953. MenuUnpost $w
  954. uplevel #0 [list $w invoke $i]
  955. }
  956. return
  957. }
  958. }
  959. }
  960. # ::tk::MenuFirstEntry --
  961. # Given a menu, this procedure finds the first entry that isn't
  962. # disabled or a tear-off or separator, and activates that entry.
  963. # However, if there is already an active entry in the menu (e.g.,
  964. # because of a previous call to tk::PostOverPoint) then the active
  965. # entry isn't changed. This procedure also sets the input focus
  966. # to the menu.
  967. #
  968. # Arguments:
  969. # menu - Name of the menu window (possibly empty).
  970. proc ::tk::MenuFirstEntry menu {
  971. if {$menu eq ""} {
  972. return
  973. }
  974. tk_menuSetFocus $menu
  975. if {[$menu index active] >= 0} {
  976. return
  977. }
  978. set last [$menu index last]
  979. for {set i 0} {$i <= $last} {incr i} {
  980. if {([catch {set state [$menu entrycget $i -state]}] == 0) \
  981. && $state ne "disabled" && [$menu type $i] ne "tearoff"} {
  982. $menu activate $i
  983. GenerateMenuSelect $menu
  984. # Only post the cascade if the current menu is a menubar;
  985. # otherwise, if the first entry of the cascade is a cascade,
  986. # we can get an annoying cascading effect resulting in a bunch of
  987. # menus getting posted (bug 676)
  988. if {[$menu type $i] eq "cascade" && [$menu cget -type] eq "menubar"} {
  989. set cascade [$menu entrycget $i -menu]
  990. if {$cascade ne ""} {
  991. $menu postcascade $i
  992. MenuFirstEntry $cascade
  993. }
  994. }
  995. return
  996. }
  997. }
  998. }
  999. # ::tk::MenuFindName --
  1000. # Given a menu and a text string, return the index of the menu entry
  1001. # that displays the string as its label. If there is no such entry,
  1002. # return an empty string. This procedure is tricky because some names
  1003. # like "active" have a special meaning in menu commands, so we can't
  1004. # always use the "index" widget command.
  1005. #
  1006. # Arguments:
  1007. # menu - Name of the menu widget.
  1008. # s - String to look for.
  1009. proc ::tk::MenuFindName {menu s} {
  1010. set i ""
  1011. if {![regexp {^active$|^last$|^none$|^[0-9]|^@} $s]} {
  1012. catch {set i [$menu index $s]}
  1013. return $i
  1014. }
  1015. set last [$menu index last]
  1016. for {set i 0} {$i <= $last} {incr i} {
  1017. if {![catch {$menu entrycget $i -label} label]} {
  1018. if {$label eq $s} {
  1019. return $i
  1020. }
  1021. }
  1022. }
  1023. return ""
  1024. }
  1025. # ::tk::PostMenubuttonMenu --
  1026. #
  1027. # Given a menubutton and a menu, this procedure posts the menu at the
  1028. # appropriate location. If the menubutton looks like an option
  1029. # menubutton, meaning that the indicator is on and the direction is
  1030. # neither above nor below, then the menu is posted so that the current
  1031. # entry is vertically aligned with the menubutton. On the Mac this
  1032. # will expose a small amount of the blue indicator on the right hand
  1033. # side. On other platforms the entry is centered over the button.
  1034. if {[tk windowingsystem] eq "aqua"} {
  1035. proc ::tk::PostMenubuttonMenu {button menu cx cy} {
  1036. set entry ""
  1037. if {[$button cget -indicatoron]} {
  1038. set entry [MenuFindName $menu [$button cget -text]]
  1039. if {$entry eq ""} {
  1040. set entry 0
  1041. }
  1042. }
  1043. set x [winfo rootx $button]
  1044. set y [expr {2 + [winfo rooty $button]}]
  1045. switch [$button cget -direction] {
  1046. above {
  1047. set entry ""
  1048. incr y [expr {4 - [winfo reqheight $menu]}]
  1049. }
  1050. below {
  1051. set entry ""
  1052. incr y [expr {2 + [winfo height $button]}]
  1053. }
  1054. left {
  1055. incr x [expr {-[winfo reqwidth $menu]}]
  1056. }
  1057. right {
  1058. incr x [winfo width $button]
  1059. }
  1060. default { # flush
  1061. incr x [expr {[winfo width $button] - [winfo reqwidth $menu] - 5}]
  1062. }
  1063. }
  1064. PostOverPoint $menu $x $y $entry
  1065. }
  1066. } else {
  1067. proc ::tk::PostMenubuttonMenu {button menu cx cy} {
  1068. set entry ""
  1069. if {[$button cget -indicatoron]} {
  1070. set entry [MenuFindName $menu [$button cget -text]]
  1071. if {$entry eq ""} {
  1072. set entry 0
  1073. }
  1074. }
  1075. set x [winfo rootx $button]
  1076. set y [winfo rooty $button]
  1077. switch [$button cget -direction] {
  1078. above {
  1079. incr y [expr {-[winfo reqheight $menu]}]
  1080. # if we go offscreen to the top, show as 'below'
  1081. if {$y < [winfo vrooty $button]} {
  1082. set y [expr {[winfo vrooty $button] + [winfo rooty $button]\
  1083. + [winfo reqheight $button]}]
  1084. }
  1085. set entry {}
  1086. }
  1087. below {
  1088. incr y [winfo height $button]
  1089. # if we go offscreen to the bottom, show as 'above'
  1090. set mh [winfo reqheight $menu]
  1091. if {($y + $mh) > ([winfo vrooty $button] + [winfo vrootheight $button])} {
  1092. set y [expr {[winfo vrooty $button] + [winfo vrootheight $button] \
  1093. + [winfo rooty $button] - $mh}]
  1094. }
  1095. set entry {}
  1096. }
  1097. left {
  1098. incr x [expr {- [winfo reqwidth $menu]}]
  1099. }
  1100. right {
  1101. incr x [expr {[winfo width $button]}]
  1102. }
  1103. default { # flush
  1104. if {[$button cget -indicatoron]} {
  1105. if {$cx ne ""} {
  1106. set x [expr {$cx - [winfo reqwidth $menu] / 2}]
  1107. set l [font metrics [$menu cget -font] -linespace]
  1108. set y [expr {$cy - $l/2 - 2}]
  1109. } else {
  1110. incr x [expr {([winfo width $button] - \
  1111. [winfo reqwidth $menu])/ 2}]
  1112. }
  1113. } else {
  1114. incr y [winfo height $button]
  1115. }
  1116. }
  1117. }
  1118. PostOverPoint $menu $x $y $entry
  1119. }
  1120. }
  1121. # ::tk::PostOverPoint --
  1122. #
  1123. # This procedure posts a menu on the screen so that a given entry in
  1124. # the menu is positioned with its upper left corner at a given point
  1125. # in the root window. The procedure also activates that entry. If no
  1126. # entry is specified the upper left corner of the entire menu is
  1127. # placed at the point.
  1128. #
  1129. # Arguments:
  1130. # menu - Menu to post.
  1131. # x, y - Root coordinates of point.
  1132. # entry - Index of entry within menu to center over (x,y).
  1133. # If omitted or specified as {}, then the menu's
  1134. # upper-left corner goes at (x,y).
  1135. if {[tk windowingsystem] ne "win32"} {
  1136. proc ::tk::PostOverPoint {menu x y {entry {}}} {
  1137. if {$entry ne ""} {
  1138. $menu post $x $y $entry
  1139. if {[$menu type $entry] ni {separator tearoff} &&
  1140. [$menu entrycget $entry -state] ne "disabled"} {
  1141. $menu activate $entry
  1142. GenerateMenuSelect $menu
  1143. }
  1144. } else {
  1145. $menu post $x $y
  1146. }
  1147. return
  1148. }
  1149. } else {
  1150. proc ::tk::PostOverPoint {menu x y {entry {}}} {
  1151. if {$entry ne ""} {
  1152. incr y [expr {-[$menu yposition $entry]}]
  1153. }
  1154. # osVersion is not available in safe interps
  1155. set ver 5
  1156. if {[info exists ::tcl_platform(osVersion)]} {
  1157. scan $::tcl_platform(osVersion) %d ver
  1158. }
  1159. # We need to fix some problems with menu posting on Windows,
  1160. # where, if the menu would overlap top or bottom of screen,
  1161. # Windows puts it in the wrong place for us. We must also
  1162. # subtract an extra amount for half the height of the current
  1163. # entry. To be safe we subtract an extra 10.
  1164. # NOTE: this issue appears to have been resolved in the Window
  1165. # manager provided with Vista and Windows 7.
  1166. if {$ver < 6} {
  1167. set yoffset [expr {[winfo screenheight $menu] \
  1168. - $y - [winfo reqheight $menu] - 10}]
  1169. if {$yoffset < [winfo vrooty $menu]} {
  1170. # The bottom of the menu is offscreen, so adjust upwards
  1171. incr y [expr {$yoffset - [winfo vrooty $menu]}]
  1172. }
  1173. # If we're off the top of the screen (either because we were
  1174. # originally or because we just adjusted too far upwards),
  1175. # then make the menu popup on the top edge.
  1176. if {$y < [winfo vrooty $menu]} {
  1177. set y [winfo vrooty $menu]
  1178. }
  1179. }
  1180. $menu post $x $y
  1181. if {$entry ne "" && [$menu entrycget $entry -state] ne "disabled"} {
  1182. $menu activate $entry
  1183. GenerateMenuSelect $menu
  1184. }
  1185. }
  1186. }
  1187. # ::tk::SaveGrabInfo --
  1188. # Sets the variables tk::Priv(oldGrab) and tk::Priv(grabStatus) to record
  1189. # the state of any existing grab on the w's display.
  1190. #
  1191. # Arguments:
  1192. # w - Name of a window; used to select the display
  1193. # whose grab information is to be recorded.
  1194. proc tk::SaveGrabInfo w {
  1195. variable ::tk::Priv
  1196. set Priv(oldGrab) [grab current $w]
  1197. if {$Priv(oldGrab) ne ""} {
  1198. set Priv(grabStatus) [grab status $Priv(oldGrab)]
  1199. }
  1200. }
  1201. # ::tk::RestoreOldGrab --
  1202. # Restores the grab to what it was before TkSaveGrabInfo was called.
  1203. #
  1204. proc ::tk::RestoreOldGrab {} {
  1205. variable ::tk::Priv
  1206. if {$Priv(oldGrab) ne ""} {
  1207. # Be careful restoring the old grab, since it's window may not
  1208. # be visible anymore.
  1209. catch {
  1210. if {$Priv(grabStatus) eq "global"} {
  1211. grab set -global $Priv(oldGrab)
  1212. } else {
  1213. grab set $Priv(oldGrab)
  1214. }
  1215. }
  1216. set Priv(oldGrab) ""
  1217. }
  1218. }
  1219. proc ::tk_menuSetFocus {menu} {
  1220. variable ::tk::Priv
  1221. if {![info exists Priv(focus)] || $Priv(focus) eq ""} {
  1222. set Priv(focus) [focus]
  1223. }
  1224. focus $menu
  1225. }
  1226. proc ::tk::GenerateMenuSelect {menu} {
  1227. variable ::tk::Priv
  1228. if {$Priv(activeMenu) ne $menu \
  1229. || $Priv(activeItem) ne [$menu index active]} {
  1230. set Priv(activeMenu) $menu
  1231. set Priv(activeItem) [$menu index active]
  1232. event generate $menu <<MenuSelect>>
  1233. }
  1234. }
  1235. # ::tk_popup --
  1236. # This procedure pops up a menu and sets things up for traversing
  1237. # the menu and its submenus.
  1238. #
  1239. # Arguments:
  1240. # menu - Name of the menu to be popped up.
  1241. # x, y - Root coordinates at which to pop up the
  1242. # menu.
  1243. # entry - Index of a menu entry to center over (x,y).
  1244. # If omitted or specified as {}, then menu's
  1245. # upper-left corner goes at (x,y).
  1246. proc ::tk_popup {menu x y {entry {}}} {
  1247. variable ::tk::Priv
  1248. if {$Priv(popup) ne "" || $Priv(postedMb) ne ""} {
  1249. tk::MenuUnpost {}
  1250. }
  1251. tk::PostOverPoint $menu $x $y $entry
  1252. if {[tk windowingsystem] eq "x11" && [winfo viewable $menu]} {
  1253. tk::SaveGrabInfo $menu
  1254. grab -global $menu
  1255. set Priv(popup) $menu
  1256. set Priv(window) $menu
  1257. set Priv(menuActivated) 1
  1258. tk_menuSetFocus $menu
  1259. }
  1260. }