menu.tcl 37 KB

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