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.
9 # Copyright (c) 1992-1994 The Regents of the University of California.
10 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 # Copyright (c) 1998-1999 by Scriptics Corporation.
13 # See the file "license.terms" for information on usage and redistribution
14 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 #-------------------------------------------------------------------------
18 # Elements of tkPriv that are used in this file:
20 # cursor - Saves the -cursor option for the posted menubutton.
21 # focus - Saves the focus during a menu selection operation.
22 # Focus gets restored here when the menu is unposted.
23 # grabGlobal - Used in conjunction with tkPriv(oldGrab): if
24 # tkPriv(oldGrab) is non-empty, then tkPriv(grabGlobal)
25 # contains either an empty string or "-global" to
26 # indicate whether the old grab was a local one or
28 # inMenubutton - The name of the menubutton widget containing
29 # the mouse, or an empty string if the mouse is
30 # not over any menubutton.
31 # menuBar - The name of the menubar that is the root
32 # of the cascade hierarchy which is currently
33 # posted. This is null when there is no menu currently
34 # being pulled down from a menu bar.
35 # oldGrab - Window that had the grab before a menu was posted.
36 # Used to restore the grab state after the menu
37 # is unposted. Empty string means there was no
38 # grab previously set.
39 # popup - If a menu has been popped up via tk_popup, this
40 # gives the name of the menu. Otherwise this
42 # postedMb - Name of the menubutton whose menu is currently
43 # posted, or an empty string if nothing is posted
44 # A grab is set on this widget.
45 # relief - Used to save the original relief of the current
47 # window - When the mouse is over a menu, this holds the
48 # name of the menu; it's cleared when the mouse
50 # tearoff - Whether the last menu posted was a tearoff or not.
51 # This is true always for unix, for tearoffs for Mac
53 # activeMenu - This is the last active menu for use
54 # with the <<MenuSelect>> virtual event.
55 # activeItem - This is the last active menu item for
56 # use with the <<MenuSelect>> virtual event.
57 #-------------------------------------------------------------------------
59 #-------------------------------------------------------------------------
61 # This file is tricky because there are five different ways that menus
64 # 1. As a pulldown from a menubutton. In this style, the variable
65 # tkPriv(postedMb) identifies the posted menubutton.
66 # 2. As a torn-off menu copied from some other menu. In this style
67 # tkPriv(postedMb) is empty, and menu's type is "tearoff".
68 # 3. As an option menu, triggered from an option menubutton. In this
69 # style tkPriv(postedMb) identifies the posted menubutton.
70 # 4. As a popup menu. In this style tkPriv(postedMb) is empty and
71 # the top-level menu's type is "normal".
72 # 5. As a pulldown from a menubar. The variable tkPriv(menubar) has
73 # the owning menubar, and the menu itself is of type "normal".
75 # The various binding procedures use the state described above to
76 # distinguish the various cases and take different actions in each
78 #-------------------------------------------------------------------------
80 #-------------------------------------------------------------------------
81 # The code below creates the default class bindings for menus
83 #-------------------------------------------------------------------------
85 bind Menubutton <FocusIn> {}
86 bind Menubutton <Enter> {
89 bind Menubutton <Leave> {
93 if {[string compare $tkPriv(inMenubutton) ""]} {
94 tkMbPost $tkPriv(inMenubutton) %X %Y
97 bind Menubutton <Motion> {
98 tkMbMotion %W up %X %Y
100 bind Menubutton <B1-Motion> {
101 tkMbMotion %W down %X %Y
103 bind Menubutton <ButtonRelease-1> {
106 bind Menubutton <space> {
108 tkMenuFirstEntry [%W cget -menu]
111 # Must set focus when mouse enters a menu, in order to allow
112 # mixed-mode processing using both the mouse and the keyboard.
113 # Don't set the focus if the event comes from a grab release,
114 # though: such an event can happen after as part of unposting
115 # a cascaded chain of menus, after the focus has already been
116 # restored to wherever it was before menu selection started.
118 bind Menu <FocusIn> {}
121 set tkPriv(window) %W
122 if {[string equal [%W cget -type] "tearoff"]} {
123 if {[string compare "%m" "NotifyUngrab"]} {
124 if {[string equal $tcl_platform(platform) "unix"]} {
129 tkMenuMotion %W %x %y %s
133 tkMenuLeave %W %X %Y %s
136 tkMenuMotion %W %x %y %s
138 bind Menu <ButtonPress> {
141 bind Menu <ButtonRelease> {
165 bind Menu <KeyPress> {
166 tkTraverseWithinMenu %W %A
169 # The following bindings apply to all windows, and are used to
170 # implement keyboard menu traversal.
172 if {[string equal $tcl_platform(platform) "unix"]} {
173 bind all <Alt-KeyPress> {
174 tkTraverseToMenu %W %A
181 bind Menubutton <Alt-KeyPress> {
182 tkTraverseToMenu %W %A
185 bind Menubutton <F10> {
191 # This procedure is invoked when the mouse enters a menubutton
192 # widget. It activates the widget unless it is disabled. Note:
193 # this procedure is only invoked when mouse button 1 is *not* down.
194 # The procedure tkMbB1Enter is invoked if the button is down.
197 # w - The name of the widget.
202 if {[string compare $tkPriv(inMenubutton) ""]} {
203 tkMbLeave $tkPriv(inMenubutton)
205 set tkPriv(inMenubutton) $w
206 if {[string compare [$w cget -state] "disabled"]} {
207 $w configure -state active
212 # This procedure is invoked when the mouse leaves a menubutton widget.
213 # It de-activates the widget, if the widget still exists.
216 # w - The name of the widget.
221 set tkPriv(inMenubutton) {}
222 if {![winfo exists $w]} {
225 if {[string equal [$w cget -state] "active"]} {
226 $w configure -state normal
231 # Given a menubutton, this procedure does all the work of posting
232 # its associated menu and unposting any other menu that is currently
236 # w - The name of the menubutton widget whose menu
238 # x, y - Root coordinates of cursor, used for positioning
239 # option menus. If not specified, then the center
240 # of the menubutton is used for an option menu.
242 proc tkMbPost {w {x {}} {y {}}} {
243 global tkPriv errorInfo
246 if {[string equal [$w cget -state] "disabled"] || \
247 [string equal $w $tkPriv(postedMb)]} {
250 set menu [$w cget -menu]
251 if {[string equal $menu ""]} {
254 set tearoff [expr {[string equal $tcl_platform(platform) "unix"] \
255 || [string equal [$menu cget -type] "tearoff"]}]
256 if {[string first $w $menu] != 0} {
257 error "can't post $menu: it isn't a descendant of $w (this is a new requirement in Tk versions 3.0 and later)"
259 set cur $tkPriv(postedMb)
260 if {[string compare $cur ""]} {
263 set tkPriv(cursor) [$w cget -cursor]
264 set tkPriv(relief) [$w cget -relief]
265 $w configure -cursor arrow
266 $w configure -relief raised
268 set tkPriv(postedMb) $w
269 set tkPriv(focus) [focus]
271 tkGenerateMenuSelect $menu
273 # If this looks like an option menubutton then post the menu so
274 # that the current entry is on top of the mouse. Otherwise post
275 # the menu just below the menubutton, as for a pull-down.
279 switch [$w cget -direction] {
281 set x [winfo rootx $w]
282 set y [expr {[winfo rooty $w] - [winfo reqheight $menu]}]
286 set x [winfo rootx $w]
287 set y [expr {[winfo rooty $w] + [winfo height $w]}]
291 set x [expr {[winfo rootx $w] - [winfo reqwidth $menu]}]
292 set y [expr {(2 * [winfo rooty $w] + [winfo height $w]) / 2}]
293 set entry [tkMenuFindName $menu [$w cget -text]]
294 if {[$w cget -indicatoron]} {
295 if {$entry == [$menu index last]} {
296 incr y [expr {-([$menu yposition $entry] \
297 + [winfo reqheight $menu])/2}]
299 incr y [expr {-([$menu yposition $entry] \
300 + [$menu yposition [expr {$entry+1}]])/2}]
304 if {[string compare $entry {}] && [string compare [$menu entrycget $entry -state] "disabled"]} {
305 $menu activate $entry
306 tkGenerateMenuSelect $menu
310 set x [expr {[winfo rootx $w] + [winfo width $w]}]
311 set y [expr {(2 * [winfo rooty $w] + [winfo height $w]) / 2}]
312 set entry [tkMenuFindName $menu [$w cget -text]]
313 if {[$w cget -indicatoron]} {
314 if {$entry == [$menu index last]} {
315 incr y [expr {-([$menu yposition $entry] \
316 + [winfo reqheight $menu])/2}]
318 incr y [expr {-([$menu yposition $entry] \
319 + [$menu yposition [expr {$entry+1}]])/2}]
323 if {[string compare $entry {}] && [string compare [$menu entrycget $entry -state] "disabled"]} {
324 $menu activate $entry
325 tkGenerateMenuSelect $menu
329 if {[$w cget -indicatoron]} {
330 if {[string equal $y {}]} {
331 set x [expr {[winfo rootx $w] + [winfo width $w]/2}]
332 set y [expr {[winfo rooty $w] + [winfo height $w]/2}]
334 tkPostOverPoint $menu $x $y [tkMenuFindName $menu [$w cget -text]]
336 $menu post [winfo rootx $w] [expr {[winfo rooty $w]+[winfo height $w]}]
341 # Error posting menu (e.g. bogus -postcommand). Unpost it and
344 set savedInfo $errorInfo
346 error $msg $savedInfo
350 set tkPriv(tearoff) $tearoff
353 if {[winfo viewable $w]} {
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.
373 # menu - Name of a menu to unpost. Ignored if there
374 # is a posted menubutton.
376 proc tkMenuUnpost menu {
379 set mb $tkPriv(postedMb)
381 # Restore focus right away (otherwise X will take focus away when
382 # the menu is unmapped and under some window managers (e.g. olvwm)
383 # we'll lose the focus completely).
385 catch {focus $tkPriv(focus)}
388 # Unpost menu(s) and restore some stuff that's dependent on
392 if {[string compare $mb ""]} {
393 set menu [$mb cget -menu]
395 set tkPriv(postedMb) {}
396 $mb configure -cursor $tkPriv(cursor)
397 $mb configure -relief $tkPriv(relief)
398 } elseif {[string compare $tkPriv(popup) ""]} {
399 $tkPriv(popup) unpost
401 } elseif {[string compare [$menu cget -type] "menubar"] \
402 && [string compare [$menu cget -type] "tearoff"]} {
403 # We're in a cascaded sub-menu from a torn-off menu or popup.
404 # Unpost all the menus up to the toplevel one (but not
405 # including the top-level torn-off one) and deactivate the
406 # top-level torn off menu if there is one.
409 set parent [winfo parent $menu]
410 if {[string compare [winfo class $parent] "Menu"] \
411 || ![winfo ismapped $parent]} {
414 $parent activate none
415 $parent postcascade none
416 tkGenerateMenuSelect $parent
417 set type [$parent cget -type]
418 if {[string equal $type "menubar"] || \
419 [string equal $type "tearoff"]} {
424 if {[string compare [$menu cget -type] "menubar"]} {
430 if {($tkPriv(tearoff) != 0) || [string compare $tkPriv(menuBar) ""]} {
431 # Release grab, if any, and restore the previous grab, if there
433 if {[string compare $menu ""]} {
434 set grab [grab current $menu]
435 if {[string compare $grab ""]} {
440 if {[string compare $tkPriv(menuBar) ""]} {
441 $tkPriv(menuBar) configure -cursor $tkPriv(cursor)
442 set tkPriv(menuBar) {}
444 if {[string compare $tcl_platform(platform) "unix"]} {
445 set tkPriv(tearoff) 0
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).
456 # w - The name of the menubutton widget.
457 # upDown - "down" means button 1 is pressed, "up" means
459 # rootx, rooty - Coordinates of mouse, in (virtual?) root window.
461 proc tkMbMotion {w upDown rootx rooty} {
464 if {[string equal $tkPriv(inMenubutton) $w]} {
467 set new [winfo containing $rootx $rooty]
468 if {[string compare $new $tkPriv(inMenubutton)] \
469 && ([string equal $new ""] \
470 || [string equal [winfo toplevel $new] [winfo toplevel $w]])} {
471 if {[string compare $tkPriv(inMenubutton) ""]} {
472 tkMbLeave $tkPriv(inMenubutton)
474 if {[string compare $new ""] \
475 && [string equal [winfo class $new] "Menubutton"] \
476 && ([$new cget -indicatoron] == 0) \
477 && ([$w cget -indicatoron] == 0)} {
478 if {[string equal $upDown "down"]} {
479 tkMbPost $new $rootx $rooty
488 # This procedure is invoked to handle button 1 releases for menubuttons.
489 # If the release happens inside the menubutton then leave its menu
490 # posted with element 0 activated. Otherwise, unpost the menu.
493 # w - The name of the menubutton widget.
495 proc tkMbButtonUp w {
499 set menu [$w cget -menu]
500 set tearoff [expr {[string equal $tcl_platform(platform) "unix"] || \
501 ([string compare $menu {}] && \
502 [string equal [$menu cget -type] "tearoff"])}]
503 if {($tearoff != 0) && [string equal $tkPriv(postedMb) $w] \
504 && [string equal $tkPriv(inMenubutton) $w]} {
505 tkMenuFirstEntry [$tkPriv(postedMb) cget -menu]
512 # This procedure is called to handle mouse motion events for menus.
513 # It does two things. First, it resets the active element in the
514 # menu, if the mouse is over the menu. Second, if a mouse button
515 # is down, it posts and unposts cascade entries to match the mouse
519 # menu - The menu window.
520 # x - The x position of the mouse.
521 # y - The y position of the mouse.
522 # state - Modifier state (tells whether buttons are down).
524 proc tkMenuMotion {menu x y state} {
526 if {[string equal $menu $tkPriv(window)]} {
527 if {[string equal [$menu cget -type] "menubar"]} {
528 if {[info exists tkPriv(focus)] && \
529 [string compare $menu $tkPriv(focus)]} {
530 $menu activate @$x,$y
531 tkGenerateMenuSelect $menu
534 $menu activate @$x,$y
535 tkGenerateMenuSelect $menu
538 if {($state & 0x1f00) != 0} {
539 $menu postcascade active
543 # tkMenuButtonDown --
544 # Handles button presses in menus. There are a couple of tricky things
546 # 1. Change the posted cascade entry (if any) to match the mouse position.
547 # 2. If there is a posted menubutton, must grab to the menubutton; this
548 # overrrides the implicit grab on button press, so that the menu
549 # button can track mouse motions over other menubuttons and change
551 # 3. If there's no posted menubutton (e.g. because we're a torn-off menu
552 # or one of its descendants) must grab to the top-level menu so that
553 # we can track mouse motions across the entire menu hierarchy.
556 # menu - The menu window.
558 proc tkMenuButtonDown menu {
562 if {![winfo viewable $menu]} {
565 $menu postcascade active
566 if {[string compare $tkPriv(postedMb) ""] && \
567 [winfo viewable $tkPriv(postedMb)]} {
568 grab -global $tkPriv(postedMb)
570 while {[string equal [$menu cget -type] "normal"] \
571 && [string equal [winfo class [winfo parent $menu]] "Menu"] \
572 && [winfo ismapped [winfo parent $menu]]} {
573 set menu [winfo parent $menu]
576 if {[string equal $tkPriv(menuBar) {}]} {
577 set tkPriv(menuBar) $menu
578 set tkPriv(cursor) [$menu cget -cursor]
579 $menu configure -cursor arrow
582 # Don't update grab information if the grab window isn't changing.
583 # Otherwise, we'll get an error when we unpost the menus and
584 # restore the grab, since the old grab window will not be viewable
587 if {[string compare $menu [grab current $menu]]} {
591 # Must re-grab even if the grab window hasn't changed, in order
592 # to release the implicit grab from the button press.
594 if {[string equal $tcl_platform(platform) "unix"]} {
601 # This procedure is invoked to handle Leave events for a menu. It
602 # deactivates everything unless the active element is a cascade element
603 # and the mouse is now over the submenu.
606 # menu - The menu window.
607 # rootx, rooty - Root coordinates of mouse.
608 # state - Modifier state.
610 proc tkMenuLeave {menu rootx rooty state} {
612 set tkPriv(window) {}
613 if {[string equal [$menu index active] "none"]} {
616 if {[string equal [$menu type active] "cascade"]
617 && [string equal [winfo containing $rootx $rooty] \
618 [$menu entrycget active -menu]]} {
622 tkGenerateMenuSelect $menu
626 # This procedure is invoked when button 1 is released over a menu.
627 # It invokes the appropriate menu action and unposts the menu if
628 # it came from a menubutton.
631 # w - Name of the menu widget.
632 # buttonRelease - 1 means this procedure is called because of
633 # a button release; 0 means because of keystroke.
635 proc tkMenuInvoke {w buttonRelease} {
638 if {$buttonRelease && [string equal $tkPriv(window) {}]} {
639 # Mouse was pressed over a menu without a menu button, then
640 # dragged off the menu (possibly with a cascade posted) and
641 # released. Unpost everything and quit.
645 event generate $w <<MenuSelect>>
649 if {[string equal [$w type active] "cascade"]} {
650 $w postcascade active
651 set menu [$w entrycget active -menu]
652 tkMenuFirstEntry $menu
653 } elseif {[string equal [$w type active] "tearoff"]} {
656 } elseif {[string equal [$w cget -type] "menubar"]} {
658 set active [$w index active]
659 set isCascade [string equal [$w type $active] "cascade"]
661 # Only de-activate the active item if it's a cascade; this prevents
662 # the annoying "activation flicker" you otherwise get with
663 # checkbuttons/commands/etc. on menubars
667 event generate $w <<MenuSelect>>
672 # If the active item is not a cascade, invoke it. This enables
673 # the use of checkbuttons/commands/etc. on menubars (which is legal,
674 # but not recommended)
677 uplevel #0 [list $w invoke $active]
681 uplevel #0 [list $w invoke active]
686 # This procedure is invoked for the Cancel (or Escape) key. It unposts
687 # the given menu and, if it is the top-level menu for a menu button,
688 # unposts the menu button as well.
691 # menu - Name of the menu window.
693 proc tkMenuEscape menu {
694 set parent [winfo parent $menu]
695 if {[string compare [winfo class $parent] "Menu"]} {
697 } elseif {[string equal [$parent cget -type] "menubar"]} {
701 tkMenuNextMenu $menu left
705 # The following routines handle arrow keys. Arrow keys behave
706 # differently depending on whether the menu is a menu bar or not.
708 proc tkMenuUpArrow {menu} {
709 if {[string equal [$menu cget -type] "menubar"]} {
710 tkMenuNextMenu $menu left
712 tkMenuNextEntry $menu -1
716 proc tkMenuDownArrow {menu} {
717 if {[string equal [$menu cget -type] "menubar"]} {
718 tkMenuNextMenu $menu right
720 tkMenuNextEntry $menu 1
724 proc tkMenuLeftArrow {menu} {
725 if {[string equal [$menu cget -type] "menubar"]} {
726 tkMenuNextEntry $menu -1
728 tkMenuNextMenu $menu left
732 proc tkMenuRightArrow {menu} {
733 if {[string equal [$menu cget -type] "menubar"]} {
734 tkMenuNextEntry $menu 1
736 tkMenuNextMenu $menu right
741 # This procedure is invoked to handle "left" and "right" traversal
742 # motions in menus. It traverses to the next menu in a menu bar,
743 # or into or out of a cascaded menu.
746 # menu - The menu that received the keyboard
748 # direction - Direction in which to move: "left" or "right"
750 proc tkMenuNextMenu {menu direction} {
753 # First handle traversals into and out of cascaded menus.
755 if {[string equal $direction "right"]} {
757 set parent [winfo parent $menu]
758 set class [winfo class $parent]
759 if {[string equal [$menu type active] "cascade"]} {
760 $menu postcascade active
761 set m2 [$menu entrycget active -menu]
762 if {[string compare $m2 ""]} {
767 set parent [winfo parent $menu]
768 while {[string compare $parent "."]} {
769 if {[string equal [winfo class $parent] "Menu"] \
770 && [string equal [$parent cget -type] "menubar"]} {
771 tk_menuSetFocus $parent
772 tkMenuNextEntry $parent 1
775 set parent [winfo parent $parent]
780 set m2 [winfo parent $menu]
781 if {[string equal [winfo class $m2] "Menu"]} {
782 if {[string compare [$m2 cget -type] "menubar"]} {
784 tkGenerateMenuSelect $menu
787 # This code unposts any posted submenu in the parent.
789 set tmp [$m2 index active]
797 # Can't traverse into or out of a cascaded menu. Go to the next
798 # or previous menubutton, if that makes sense.
800 set m2 [winfo parent $menu]
801 if {[string equal [winfo class $m2] "Menu"]} {
802 if {[string equal [$m2 cget -type] "menubar"]} {
804 tkMenuNextEntry $m2 -1
809 set w $tkPriv(postedMb)
810 if {[string equal $w ""]} {
813 set buttons [winfo children [winfo parent $w]]
814 set length [llength $buttons]
815 set i [expr {[lsearch -exact $buttons $w] + $count}]
820 while {$i >= $length} {
823 set mb [lindex $buttons $i]
824 if {[string equal [winfo class $mb] "Menubutton"] \
825 && [string compare [$mb cget -state] "disabled"] \
826 && [string compare [$mb cget -menu] ""] \
827 && [string compare [[$mb cget -menu] index last] "none"]} {
830 if {[string equal $mb $w]} {
836 tkMenuFirstEntry [$mb cget -menu]
840 # Activate the next higher or lower entry in the posted menu,
841 # wrapping around at the ends. Disabled entries are skipped.
844 # menu - Menu window that received the keystroke.
845 # count - 1 means go to the next lower entry,
846 # -1 means go to the next higher entry.
848 proc tkMenuNextEntry {menu count} {
851 if {[string equal [$menu index last] "none"]} {
854 set length [expr {[$menu index last]+1}]
855 set quitAfter $length
856 set active [$menu index active]
857 if {[string equal $active "none"]} {
860 set i [expr {$active + $count}]
863 if {$quitAfter <= 0} {
864 # We've tried every entry in the menu. Either there are
865 # none, or they're all disabled. Just give up.
872 while {$i >= $length} {
875 if {[catch {$menu entrycget $i -state} state] == 0} {
876 if {[string compare $state "disabled"]} {
887 tkGenerateMenuSelect $menu
888 if {[string equal [$menu type $i] "cascade"]} {
889 set cascade [$menu entrycget $i -menu]
890 if {[string compare $cascade ""]} {
891 # Here we auto-post a cascade. This is necessary when
892 # we traverse left/right in the menubar, but undesirable when
893 # we traverse up/down in a menu.
895 tkMenuFirstEntry $cascade
901 # This procedure searches the entire window hierarchy under w for
902 # a menubutton that isn't disabled and whose underlined character
903 # is "char" or an entry in a menubar that isn't disabled and whose
904 # underlined character is "char".
905 # It returns the name of that window, if found, or an
906 # empty string if no matching window was found. If "char" is an
907 # empty string then the procedure returns the name of the first
908 # menubutton found that isn't disabled.
911 # w - Name of window where key was typed.
912 # char - Underlined character to search for;
913 # may be either upper or lower case, and
914 # will match either upper or lower case.
916 proc tkMenuFind {w char} {
918 set char [string tolower $char]
919 set windowlist [winfo child $w]
921 foreach child $windowlist {
922 # Don't descend into other toplevels.
923 if {[string compare [winfo toplevel [focus]] \
924 [winfo toplevel $child]]} {
927 if {[string equal [winfo class $child] "Menu"] && \
928 [string equal [$child cget -type] "menubar"]} {
929 if {[string equal $char ""]} {
932 set last [$child index last]
933 for {set i [$child cget -tearoff]} {$i <= $last} {incr i} {
934 if {[string equal [$child type $i] "separator"]} {
937 set char2 [string index [$child entrycget $i -label] \
938 [$child entrycget $i -underline]]
939 if {[string equal $char [string tolower $char2]] \
940 || [string equal $char ""]} {
941 if {[string compare [$child entrycget $i -state] "disabled"]} {
949 foreach child $windowlist {
950 # Don't descend into other toplevels.
951 if {[string compare [winfo toplevel [focus]] \
952 [winfo toplevel $child]]} {
955 switch [winfo class $child] {
957 set char2 [string index [$child cget -text] \
958 [$child cget -underline]]
959 if {[string equal $char [string tolower $char2]] \
960 || [string equal $char ""]} {
961 if {[string compare [$child cget -state] "disabled"]} {
968 set match [tkMenuFind $child $char]
969 if {[string compare $match ""]} {
978 # tkTraverseToMenu --
979 # This procedure implements keyboard traversal of menus. Given an
980 # ASCII character "char", it looks for a menubutton with that character
981 # underlined. If one is found, it posts the menubutton's menu
984 # w - Window in which the key was typed (selects
985 # a toplevel window).
986 # char - Character that selects a menu. The case
987 # is ignored. If an empty string, nothing
990 proc tkTraverseToMenu {w char} {
992 if {[string equal $char ""]} {
995 while {[string equal [winfo class $w] "Menu"]} {
996 if {[string compare [$w cget -type] "menubar"] \
997 && [string equal $tkPriv(postedMb) ""]} {
1000 if {[string equal [$w cget -type] "menubar"]} {
1003 set w [winfo parent $w]
1005 set w [tkMenuFind [winfo toplevel $w] $char]
1006 if {[string compare $w ""]} {
1007 if {[string equal [winfo class $w] "Menu"]} {
1009 set tkPriv(window) $w
1012 tkTraverseWithinMenu $w $char
1015 tkMenuFirstEntry [$w cget -menu]
1021 # This procedure traverses to the first menubutton in the toplevel
1022 # for a given window, and posts that menubutton's menu.
1025 # w - Name of a window. Selects which toplevel
1026 # to search for menubuttons.
1028 proc tkFirstMenu w {
1029 set w [tkMenuFind [winfo toplevel $w] ""]
1030 if {[string compare $w ""]} {
1031 if {[string equal [winfo class $w] "Menu"]} {
1033 set tkPriv(window) $w
1039 tkMenuFirstEntry [$w cget -menu]
1044 # tkTraverseWithinMenu
1045 # This procedure implements keyboard traversal within a menu. It
1046 # searches for an entry in the menu that has "char" underlined. If
1047 # such an entry is found, it is invoked and the menu is unposted.
1050 # w - The name of the menu widget.
1051 # char - The character to look for; case is
1052 # ignored. If the string is empty then
1055 proc tkTraverseWithinMenu {w char} {
1056 if {[string equal $char ""]} {
1059 set char [string tolower $char]
1060 set last [$w index last]
1061 if {[string equal $last "none"]} {
1064 for {set i 0} {$i <= $last} {incr i} {
1065 if {[catch {set char2 [string index \
1066 [$w entrycget $i -label] [$w entrycget $i -underline]]}]} {
1069 if {[string equal $char [string tolower $char2]]} {
1070 if {[string equal [$w type $i] "cascade"]} {
1072 $w postcascade active
1073 event generate $w <<MenuSelect>>
1074 set m2 [$w entrycget $i -menu]
1075 if {[string compare $m2 ""]} {
1076 tkMenuFirstEntry $m2
1080 uplevel #0 [list $w invoke $i]
1087 # tkMenuFirstEntry --
1088 # Given a menu, this procedure finds the first entry that isn't
1089 # disabled or a tear-off or separator, and activates that entry.
1090 # However, if there is already an active entry in the menu (e.g.,
1091 # because of a previous call to tkPostOverPoint) then the active
1092 # entry isn't changed. This procedure also sets the input focus
1096 # menu - Name of the menu window (possibly empty).
1098 proc tkMenuFirstEntry menu {
1099 if {[string equal $menu ""]} {
1102 tk_menuSetFocus $menu
1103 if {[string compare [$menu index active] "none"]} {
1106 set last [$menu index last]
1107 if {[string equal $last "none"]} {
1110 for {set i 0} {$i <= $last} {incr i} {
1111 if {([catch {set state [$menu entrycget $i -state]}] == 0) \
1112 && [string compare $state "disabled"] \
1113 && [string compare [$menu type $i] "tearoff"]} {
1115 tkGenerateMenuSelect $menu
1116 # Only post the cascade if the current menu is a menubar;
1117 # otherwise, if the first entry of the cascade is a cascade,
1118 # we can get an annoying cascading effect resulting in a bunch of
1119 # menus getting posted (bug 676)
1120 if {[string equal [$menu type $i] "cascade"] && \
1121 [string equal [$menu cget -type] "menubar"]} {
1122 set cascade [$menu entrycget $i -menu]
1123 if {[string compare $cascade ""]} {
1124 $menu postcascade $i
1125 tkMenuFirstEntry $cascade
1134 # Given a menu and a text string, return the index of the menu entry
1135 # that displays the string as its label. If there is no such entry,
1136 # return an empty string. This procedure is tricky because some names
1137 # like "active" have a special meaning in menu commands, so we can't
1138 # always use the "index" widget command.
1141 # menu - Name of the menu widget.
1142 # s - String to look for.
1144 proc tkMenuFindName {menu s} {
1146 if {![regexp {^active$|^last$|^none$|^[0-9]|^@} $s]} {
1147 catch {set i [$menu index $s]}
1150 set last [$menu index last]
1151 if {[string equal $last "none"]} {
1154 for {set i 0} {$i <= $last} {incr i} {
1155 if {![catch {$menu entrycget $i -label} label]} {
1156 if {[string equal $label $s]} {
1164 # tkPostOverPoint --
1165 # This procedure posts a given menu such that a given entry in the
1166 # menu is centered over a given point in the root window. It also
1167 # activates the given entry.
1170 # menu - Menu to post.
1171 # x, y - Root coordinates of point.
1172 # entry - Index of entry within menu to center over (x,y).
1173 # If omitted or specified as {}, then the menu's
1174 # upper-left corner goes at (x,y).
1176 proc tkPostOverPoint {menu x y {entry {}}} {
1179 if {[string compare $entry {}]} {
1180 if {$entry == [$menu index last]} {
1181 incr y [expr {-([$menu yposition $entry] \
1182 + [winfo reqheight $menu])/2}]
1184 incr y [expr {-([$menu yposition $entry] \
1185 + [$menu yposition [expr {$entry+1}]])/2}]
1187 incr x [expr {-[winfo reqwidth $menu]/2}]
1190 if {[string compare $entry {}] \
1191 && [string compare [$menu entrycget $entry -state] "disabled"]} {
1192 $menu activate $entry
1193 tkGenerateMenuSelect $menu
1198 # Sets the variables tkPriv(oldGrab) and tkPriv(grabStatus) to record
1199 # the state of any existing grab on the w's display.
1202 # w - Name of a window; used to select the display
1203 # whose grab information is to be recorded.
1205 proc tkSaveGrabInfo w {
1207 set tkPriv(oldGrab) [grab current $w]
1208 if {[string compare $tkPriv(oldGrab) ""]} {
1209 set tkPriv(grabStatus) [grab status $tkPriv(oldGrab)]
1213 # tkRestoreOldGrab --
1214 # Restores the grab to what it was before TkSaveGrabInfo was called.
1217 proc tkRestoreOldGrab {} {
1220 if {[string compare $tkPriv(oldGrab) ""]} {
1222 # Be careful restoring the old grab, since it's window may not
1223 # be visible anymore.
1226 if {[string equal $tkPriv(grabStatus) "global"]} {
1227 grab set -global $tkPriv(oldGrab)
1229 grab set $tkPriv(oldGrab)
1232 set tkPriv(oldGrab) ""
1236 proc tk_menuSetFocus {menu} {
1238 if {![info exists tkPriv(focus)] || [string equal $tkPriv(focus) {}]} {
1239 set tkPriv(focus) [focus]
1244 proc tkGenerateMenuSelect {menu} {
1247 if {[string equal $tkPriv(activeMenu) $menu] \
1248 && [string equal $tkPriv(activeItem) [$menu index active]]} {
1252 set tkPriv(activeMenu) $menu
1253 set tkPriv(activeItem) [$menu index active]
1254 event generate $menu <<MenuSelect>>
1258 # This procedure pops up a menu and sets things up for traversing
1259 # the menu and its submenus.
1262 # menu - Name of the menu to be popped up.
1263 # x, y - Root coordinates at which to pop up the
1265 # entry - Index of a menu entry to center over (x,y).
1266 # If omitted or specified as {}, then menu's
1267 # upper-left corner goes at (x,y).
1269 proc tk_popup {menu x y {entry {}}} {
1272 if {[string compare $tkPriv(popup) ""] \
1273 || [string compare $tkPriv(postedMb) ""]} {
1276 tkPostOverPoint $menu $x $y $entry
1277 if {[string equal $tcl_platform(platform) "unix"] \
1278 && [winfo viewable $menu]} {
1279 tkSaveGrabInfo $menu
1281 set tkPriv(popup) $menu
1282 tk_menuSetFocus $menu