menu.tcl 38 KB

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