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.
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>
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 #-------------------------------------------------------------------------
17 # Elements of tk::Priv that are used in this file:
19 # cursor - Saves the -cursor option for the posted menubutton.
20 # focus - Saves the focus during a menu selection operation.
21 # Focus gets restored here when the menu is unposted.
22 # grabGlobal - Used in conjunction with tk::Priv(oldGrab): if
23 # tk::Priv(oldGrab) is non-empty, then tk::Priv(grabGlobal)
24 # contains either an empty string or "-global" to
25 # indicate whether the old grab was a local one or
27 # inMenubutton - The name of the menubutton widget containing
28 # the mouse, or an empty string if the mouse is
29 # not over any menubutton.
30 # menuBar - The name of the menubar that is the root
31 # of the cascade hierarchy which is currently
32 # posted. This is null when there is no menu currently
33 # being pulled down from a menu bar.
34 # oldGrab - Window that had the grab before a menu was posted.
35 # Used to restore the grab state after the menu
36 # is unposted. Empty string means there was no
37 # grab previously set.
38 # popup - If a menu has been popped up via tk_popup, this
39 # gives the name of the menu. Otherwise this
41 # postedMb - Name of the menubutton whose menu is currently
42 # posted, or an empty string if nothing is posted
43 # A grab is set on this widget.
44 # relief - Used to save the original relief of the current
46 # window - When the mouse is over a menu, this holds the
47 # name of the menu; it's cleared when the mouse
49 # tearoff - Whether the last menu posted was a tearoff or not.
50 # This is true always for unix, for tearoffs for Mac
52 # activeMenu - This is the last active menu for use
53 # with the <<MenuSelect>> virtual event.
54 # activeItem - This is the last active menu item for
55 # use with the <<MenuSelect>> virtual event.
56 #-------------------------------------------------------------------------
58 #-------------------------------------------------------------------------
60 # This file is tricky because there are five different ways that menus
63 # 1. As a pulldown from a menubutton. In this style, the variable
64 # tk::Priv(postedMb) identifies the posted menubutton.
65 # 2. As a torn-off menu copied from some other menu. In this style
66 # tk::Priv(postedMb) is empty, and menu's type is "tearoff".
67 # 3. As an option menu, triggered from an option menubutton. In this
68 # style tk::Priv(postedMb) identifies the posted menubutton.
69 # 4. As a popup menu. In this style tk::Priv(postedMb) is empty and
70 # the top-level menu's type is "normal".
71 # 5. As a pulldown from a menubar. The variable tk::Priv(menubar) has
72 # the owning menubar, and the menu itself is of type "normal".
74 # The various binding procedures use the state described above to
75 # distinguish the various cases and take different actions in each
77 #-------------------------------------------------------------------------
79 #-------------------------------------------------------------------------
80 # The code below creates the default class bindings for menus
82 #-------------------------------------------------------------------------
84 bind Menubutton
<FocusIn
> {}
85 bind Menubutton
<Enter
> {
88 bind Menubutton
<Leave
> {
92 if {$tk::Priv(inMenubutton
) ne
""} {
93 tk::MbPost $tk::Priv(inMenubutton
) %X
%Y
96 bind Menubutton
<Motion
> {
97 tk::MbMotion %W up
%X
%Y
99 bind Menubutton
<B1-Motion
> {
100 tk::MbMotion %W down
%X
%Y
102 bind Menubutton
<ButtonRelease-1
> {
105 bind Menubutton
<space
> {
107 tk::MenuFirstEntry [%W cget
-menu]
110 # Must set focus when mouse enters a menu, in order to allow
111 # mixed-mode processing using both the mouse and the keyboard.
112 # Don't set the focus if the event comes from a grab release,
113 # though: such an event can happen after as part of unposting
114 # a cascaded chain of menus, after the focus has already been
115 # restored to wherever it was before menu selection started.
117 bind Menu
<FocusIn
> {}
120 set tk::Priv(window
) %W
121 if {[%W cget
-type] eq
"tearoff"} {
122 if {"%m" ne
"NotifyUngrab"} {
123 if {[tk windowingsystem
] eq
"x11"} {
128 tk::MenuMotion %W
%x
%y
%s
132 tk::MenuLeave %W
%X
%Y
%s
135 tk::MenuMotion %W
%x
%y
%s
137 bind Menu
<ButtonPress
> {
138 tk::MenuButtonDown %W
140 bind Menu
<ButtonRelease
> {
156 tk::MenuRightArrow %W
164 bind Menu
<KeyPress
> {
165 tk::TraverseWithinMenu %W
%A
168 # The following bindings apply to all windows, and are used to
169 # implement keyboard menu traversal.
171 if {[tk windowingsystem
] eq
"x11"} {
172 bind all
<Alt-KeyPress
> {
173 tk::TraverseToMenu %W
%A
180 bind Menubutton
<Alt-KeyPress
> {
181 tk::TraverseToMenu %W
%A
184 bind Menubutton
<F10
> {
190 # This procedure is invoked when the mouse enters a menubutton
191 # widget. It activates the widget unless it is disabled. Note:
192 # this procedure is only invoked when mouse button 1 is *not* down.
193 # The procedure ::tk::MbB1Enter is invoked if the button is down.
196 # w - The name of the widget.
198 proc ::tk::MbEnter w
{
201 if {$Priv(inMenubutton
) ne
""} {
202 MbLeave
$Priv(inMenubutton
)
204 set Priv
(inMenubutton
) $w
205 if {[$w cget
-state] ne
"disabled" && [tk windowingsystem
] ne
"aqua"} {
206 $w configure
-state active
211 # This procedure is invoked when the mouse leaves a menubutton widget.
212 # It de-activates the widget, if the widget still exists.
215 # w - The name of the widget.
217 proc ::tk::MbLeave w
{
220 set Priv
(inMenubutton
) {}
221 if {![winfo exists
$w]} {
224 if {[$w cget
-state] eq
"active" && [tk windowingsystem
] ne
"aqua"} {
225 $w configure
-state normal
230 # Given a menubutton, this procedure does all the work of posting
231 # its associated menu and unposting any other menu that is currently
235 # w - The name of the menubutton widget whose menu
237 # x, y - Root coordinates of cursor, used for positioning
238 # option menus. If not specified, then the center
239 # of the menubutton is used for an option menu.
241 proc ::tk::MbPost {w
{x
{}} {y
{}}} {
246 if {[$w cget
-state] eq
"disabled" ||
$w eq
$Priv(postedMb
)} {
249 set menu [$w cget
-menu]
253 set tearoff
[expr {[tk windowingsystem
] eq
"x11" \
254 ||
[$menu cget
-type] eq
"tearoff"}]
255 if {[string first
$w $menu] != 0} {
256 error "can't post $menu: it isn't a descendant of $w (this is a new requirement in Tk versions 3.0 and later)"
258 set cur
$Priv(postedMb
)
262 if {$::tk_strictMotif} {
263 set Priv
(cursor
) [$w cget
-cursor]
264 $w configure
-cursor arrow
266 if {[tk windowingsystem
] ne
"aqua"} {
267 set Priv
(relief
) [$w cget
-relief]
268 $w configure
-relief raised
270 $w configure
-state active
273 set Priv
(postedMb
) $w
274 set Priv
(focus) [focus]
276 GenerateMenuSelect
$menu
278 # If this looks like an option menubutton then post the menu so
279 # that the current entry is on top of the mouse. Otherwise post
280 # the menu just below the menubutton, as for a pull-down.
284 switch [$w cget
-direction] {
286 set x
[winfo rootx
$w]
287 set y
[expr {[winfo rooty
$w] - [winfo reqheight
$menu]}]
288 # if we go offscreen to the top, show as 'below'
290 set y
[expr {[winfo rooty
$w] + [winfo height
$w]}]
292 PostOverPoint
$menu $x $y
295 set x
[winfo rootx
$w]
296 set y
[expr {[winfo rooty
$w] + [winfo height
$w]}]
297 # if we go offscreen to the bottom, show as 'above'
298 set mh
[winfo reqheight
$menu]
299 if {($y + $mh) > [winfo screenheight
$w]} {
300 set y
[expr {[winfo rooty
$w] - $mh}]
302 PostOverPoint
$menu $x $y
305 set x
[expr {[winfo rootx
$w] - [winfo reqwidth
$menu]}]
306 set y
[expr {(2 * [winfo rooty
$w] + [winfo height
$w]) / 2}]
307 set entry [MenuFindName
$menu [$w cget
-text]]
308 if {[$w cget
-indicatoron] && $entry ne
""} {
309 if {$entry == [$menu index last
]} {
310 incr y
[expr {-([$menu yposition
$entry] \
311 + [winfo reqheight
$menu])/2}]
313 incr y
[expr {-([$menu yposition
$entry] \
314 + [$menu yposition
[expr {$entry+1}]])/2}]
317 PostOverPoint
$menu $x $y
319 && [$menu entrycget
$entry -state] ne
"disabled"} {
320 $menu activate
$entry
321 GenerateMenuSelect
$menu
325 set x
[expr {[winfo rootx
$w] + [winfo width
$w]}]
326 set y
[expr {(2 * [winfo rooty
$w] + [winfo height
$w]) / 2}]
327 set entry [MenuFindName
$menu [$w cget
-text]]
328 if {[$w cget
-indicatoron] && $entry ne
""} {
329 if {$entry == [$menu index last
]} {
330 incr y
[expr {-([$menu yposition
$entry] \
331 + [winfo reqheight
$menu])/2}]
333 incr y
[expr {-([$menu yposition
$entry] \
334 + [$menu yposition
[expr {$entry+1}]])/2}]
337 PostOverPoint
$menu $x $y
339 && [$menu entrycget
$entry -state] ne
"disabled"} {
340 $menu activate
$entry
341 GenerateMenuSelect
$menu
345 if {[$w cget
-indicatoron]} {
347 set x
[expr {[winfo rootx
$w] + [winfo width
$w]/2}]
348 set y
[expr {[winfo rooty
$w] + [winfo height
$w]/2}]
350 PostOverPoint
$menu $x $y [MenuFindName
$menu [$w cget
-text]]
352 PostOverPoint
$menu [winfo rootx
$w] [expr {[winfo rooty
$w]+[winfo height
$w]}]
357 # Error posting menu (e.g. bogus -postcommand). Unpost it and
360 set savedInfo
$errorInfo
362 error $msg $savedInfo
366 set Priv
(tearoff
) $tearoff
369 if {[winfo viewable
$w]} {
376 # ::tk::MenuUnpost --
377 # This procedure unposts a given menu, plus all of its ancestors up
378 # to (and including) a menubutton, if any. It also restores various
379 # values to what they were before the menu was posted, and releases
380 # a grab if there's a menubutton involved. Special notes:
381 # 1. It's important to unpost all menus before releasing the grab, so
382 # that any Enter-Leave events (e.g. from menu back to main
383 # application) have mode NotifyGrab.
384 # 2. Be sure to enclose various groups of commands in "catch" so that
385 # the procedure will complete even if the menubutton or the menu
386 # or the grab window has been deleted.
389 # menu - Name of a menu to unpost. Ignored if there
390 # is a posted menubutton.
392 proc ::tk::MenuUnpost menu {
395 set mb
$Priv(postedMb
)
397 # Restore focus right away (otherwise X will take focus away when
398 # the menu is unmapped and under some window managers (e.g. olvwm)
399 # we'll lose the focus completely).
401 catch {focus $Priv(focus)}
404 # Unpost menu(s) and restore some stuff that's dependent on
407 after cancel
[array get Priv menuActivatedTimer
]
408 unset -nocomplain Priv
(menuActivated
)
409 after cancel
[array get Priv menuDeactivatedTimer
]
410 unset -nocomplain Priv
(menuDeactivated
)
414 set menu [$mb cget
-menu]
416 set Priv
(postedMb
) {}
417 if {$::tk_strictMotif} {
418 $mb configure
-cursor $Priv(cursor
)
420 if {[tk windowingsystem
] ne
"aqua"} {
421 $mb configure
-relief $Priv(relief
)
423 $mb configure
-state normal
425 } elseif
{$Priv(popup
) ne
""} {
428 } elseif
{[$menu cget
-type] ne
"menubar" \
429 && [$menu cget
-type] ne
"tearoff"} {
430 # We're in a cascaded sub-menu from a torn-off menu or popup.
431 # Unpost all the menus up to the toplevel one (but not
432 # including the top-level torn-off one) and deactivate the
433 # top-level torn off menu if there is one.
436 set parent
[winfo parent
$menu]
437 if {[winfo class
$parent] ne
"Menu" \
438 ||
![winfo ismapped
$parent]} {
441 $parent activate none
442 $parent postcascade none
443 GenerateMenuSelect
$parent
444 set type
[$parent cget
-type]
445 if {$type eq
"menubar" ||
$type eq
"tearoff"} {
450 if {[$menu cget
-type] ne
"menubar"} {
456 if {($Priv(tearoff
) != 0) ||
$Priv(menuBar
) ne
""} {
457 # Release grab, if any, and restore the previous grab, if there
460 set grab [grab current
$menu]
466 if {$Priv(menuBar
) ne
""} {
467 if {$::tk_strictMotif} {
468 $Priv(menuBar
) configure
-cursor $Priv(cursor
)
472 if {[tk windowingsystem
] ne
"x11"} {
479 # This procedure handles mouse motion events inside menubuttons, and
480 # also outside menubuttons when a menubutton has a grab (e.g. when a
481 # menu selection operation is in progress).
484 # w - The name of the menubutton widget.
485 # upDown - "down" means button 1 is pressed, "up" means
487 # rootx, rooty - Coordinates of mouse, in (virtual?) root window.
489 proc ::tk::MbMotion {w upDown rootx rooty
} {
492 if {$Priv(inMenubutton
) eq
$w} {
495 set new
[winfo containing
$rootx $rooty]
496 if {$new ne
$Priv(inMenubutton
) \
497 && ($new eq
"" ||
[winfo toplevel $new] eq
[winfo toplevel $w])} {
498 if {$Priv(inMenubutton
) ne
""} {
499 MbLeave
$Priv(inMenubutton
)
502 && [winfo class
$new] eq
"Menubutton" \
503 && ([$new cget
-indicatoron] == 0) \
504 && ([$w cget
-indicatoron] == 0)} {
505 if {$upDown eq
"down"} {
506 MbPost
$new $rootx $rooty
514 # ::tk::MbButtonUp --
515 # This procedure is invoked to handle button 1 releases for menubuttons.
516 # If the release happens inside the menubutton then leave its menu
517 # posted with element 0 activated. Otherwise, unpost the menu.
520 # w - The name of the menubutton widget.
522 proc ::tk::MbButtonUp w
{
526 set menu [$w cget
-menu]
527 set tearoff
[expr {[tk windowingsystem
] eq
"x11" ||
\
528 ($menu ne
"" && [$menu cget
-type] eq
"tearoff")}]
529 if {($tearoff != 0) && $Priv(postedMb
) eq
$w \
530 && $Priv(inMenubutton
) eq
$w} {
531 MenuFirstEntry
[$Priv(postedMb
) cget
-menu]
537 # ::tk::MenuMotion --
538 # This procedure is called to handle mouse motion events for menus.
539 # It does two things. First, it resets the active element in the
540 # menu, if the mouse is over the menu. Second, if a mouse button
541 # is down, it posts and unposts cascade entries to match the mouse
545 # menu - The menu window.
546 # x - The x position of the mouse.
547 # y - The y position of the mouse.
548 # state - Modifier state (tells whether buttons are down).
550 proc ::tk::MenuMotion {menu x y state
} {
552 if {$menu eq
$Priv(window
)} {
553 set activeindex
[$menu index active
]
554 if {[$menu cget
-type] eq
"menubar"} {
555 if {[info exists Priv
(focus)] && $menu ne
$Priv(focus)} {
556 $menu activate
@$x,$y
557 GenerateMenuSelect
$menu
560 $menu activate
@$x,$y
561 GenerateMenuSelect
$menu
563 set index
[$menu index
@$x,$y]
564 if {[info exists Priv
(menuActivated
)] \
565 && $index ne
"none" \
566 && $index ne
$activeindex} {
567 set mode
[option get
$menu clickToFocus ClickToFocus
]
568 if {[string is false
$mode]} {
569 set delay
[expr {[$menu cget
-type] eq
"menubar" ?
0 : 50}]
570 if {[$menu type
$index] eq
"cascade"} {
571 set Priv
(menuActivatedTimer
) \
572 [after $delay [list $menu postcascade active
]]
574 set Priv
(menuDeactivatedTimer
) \
575 [after $delay [list $menu postcascade none
]]
582 # ::tk::MenuButtonDown --
583 # Handles button presses in menus. There are a couple of tricky things
585 # 1. Change the posted cascade entry (if any) to match the mouse position.
586 # 2. If there is a posted menubutton, must grab to the menubutton; this
587 # overrrides the implicit grab on button press, so that the menu
588 # button can track mouse motions over other menubuttons and change
590 # 3. If there's no posted menubutton (e.g. because we're a torn-off menu
591 # or one of its descendants) must grab to the top-level menu so that
592 # we can track mouse motions across the entire menu hierarchy.
595 # menu - The menu window.
597 proc ::tk::MenuButtonDown menu {
601 if {![winfo viewable
$menu]} {
604 $menu postcascade active
605 if {$Priv(postedMb
) ne
"" && [winfo viewable
$Priv(postedMb
)]} {
606 grab -global $Priv(postedMb
)
608 while {[$menu cget
-type] eq
"normal" \
609 && [winfo class
[winfo parent
$menu]] eq
"Menu" \
610 && [winfo ismapped
[winfo parent
$menu]]} {
611 set menu [winfo parent
$menu]
614 if {$Priv(menuBar
) eq
{}} {
615 set Priv
(menuBar
) $menu
616 if {$::tk_strictMotif} {
617 set Priv
(cursor
) [$menu cget
-cursor]
618 $menu configure
-cursor arrow
620 if {[$menu type active
] eq
"cascade"} {
621 set Priv
(menuActivated
) 1
625 # Don't update grab information if the grab window isn't changing.
626 # Otherwise, we'll get an error when we unpost the menus and
627 # restore the grab, since the old grab window will not be viewable
630 if {$menu ne
[grab current
$menu]} {
634 # Must re-grab even if the grab window hasn't changed, in order
635 # to release the implicit grab from the button press.
637 if {[tk windowingsystem
] eq
"x11"} {
644 # This procedure is invoked to handle Leave events for a menu. It
645 # deactivates everything unless the active element is a cascade element
646 # and the mouse is now over the submenu.
649 # menu - The menu window.
650 # rootx, rooty - Root coordinates of mouse.
651 # state - Modifier state.
653 proc ::tk::MenuLeave {menu rootx rooty state
} {
656 if {[$menu index active
] eq
"none"} {
659 if {[$menu type active
] eq
"cascade" \
660 && [winfo containing
$rootx $rooty] eq
\
661 [$menu entrycget active
-menu]} {
665 GenerateMenuSelect
$menu
668 # ::tk::MenuInvoke --
669 # This procedure is invoked when button 1 is released over a menu.
670 # It invokes the appropriate menu action and unposts the menu if
671 # it came from a menubutton.
674 # w - Name of the menu widget.
675 # buttonRelease - 1 means this procedure is called because of
676 # a button release; 0 means because of keystroke.
678 proc ::tk::MenuInvoke {w buttonRelease
} {
681 if {$buttonRelease && $Priv(window
) eq
""} {
682 # Mouse was pressed over a menu without a menu button, then
683 # dragged off the menu (possibly with a cascade posted) and
684 # released. Unpost everything and quit.
688 event generate
$w <<MenuSelect
>>
692 if {[$w type active
] eq
"cascade"} {
693 $w postcascade active
694 set menu [$w entrycget active
-menu]
696 } elseif
{[$w type active
] eq
"tearoff"} {
699 } elseif
{[$w cget
-type] eq
"menubar"} {
701 set active
[$w index active
]
702 set isCascade
[string equal
[$w type
$active] "cascade"]
704 # Only de-activate the active item if it's a cascade; this prevents
705 # the annoying "activation flicker" you otherwise get with
706 # checkbuttons/commands/etc. on menubars
710 event generate
$w <<MenuSelect
>>
715 # If the active item is not a cascade, invoke it. This enables
716 # the use of checkbuttons/commands/etc. on menubars (which is legal,
717 # but not recommended)
720 uplevel #0 [list $w invoke $active]
723 set active
[$w index active
]
724 if {$Priv(popup
) eq
"" ||
$active ne
"none"} {
727 uplevel #0 [list $w invoke active]
731 # ::tk::MenuEscape --
732 # This procedure is invoked for the Cancel (or Escape) key. It unposts
733 # the given menu and, if it is the top-level menu for a menu button,
734 # unposts the menu button as well.
737 # menu - Name of the menu window.
739 proc ::tk::MenuEscape menu {
740 set parent
[winfo parent
$menu]
741 if {[winfo class
$parent] ne
"Menu"} {
743 } elseif
{[$parent cget
-type] eq
"menubar"} {
747 MenuNextMenu
$menu left
751 # The following routines handle arrow keys. Arrow keys behave
752 # differently depending on whether the menu is a menu bar or not.
754 proc ::tk::MenuUpArrow {menu} {
755 if {[$menu cget
-type] eq
"menubar"} {
756 MenuNextMenu
$menu left
758 MenuNextEntry
$menu -1
762 proc ::tk::MenuDownArrow {menu} {
763 if {[$menu cget
-type] eq
"menubar"} {
764 MenuNextMenu
$menu right
766 MenuNextEntry
$menu 1
770 proc ::tk::MenuLeftArrow {menu} {
771 if {[$menu cget
-type] eq
"menubar"} {
772 MenuNextEntry
$menu -1
774 MenuNextMenu
$menu left
778 proc ::tk::MenuRightArrow {menu} {
779 if {[$menu cget
-type] eq
"menubar"} {
780 MenuNextEntry
$menu 1
782 MenuNextMenu
$menu right
786 # ::tk::MenuNextMenu --
787 # This procedure is invoked to handle "left" and "right" traversal
788 # motions in menus. It traverses to the next menu in a menu bar,
789 # or into or out of a cascaded menu.
792 # menu - The menu that received the keyboard
794 # direction - Direction in which to move: "left" or "right"
796 proc ::tk::MenuNextMenu {menu direction
} {
799 # First handle traversals into and out of cascaded menus.
801 if {$direction eq
"right"} {
803 set parent
[winfo parent
$menu]
804 set class
[winfo class
$parent]
805 if {[$menu type active
] eq
"cascade"} {
806 $menu postcascade active
807 set m2
[$menu entrycget active
-menu]
813 set parent
[winfo parent
$menu]
814 while {$parent ne
"."} {
815 if {[winfo class
$parent] eq
"Menu" \
816 && [$parent cget
-type] eq
"menubar"} {
817 tk_menuSetFocus $parent
818 MenuNextEntry
$parent 1
821 set parent
[winfo parent
$parent]
826 set m2
[winfo parent
$menu]
827 if {[winfo class
$m2] eq
"Menu"} {
829 GenerateMenuSelect
$menu
834 if {[$m2 cget
-type] ne
"menubar"} {
840 # Can't traverse into or out of a cascaded menu. Go to the next
841 # or previous menubutton, if that makes sense.
843 set m2
[winfo parent
$menu]
844 if {[winfo class
$m2] eq
"Menu" && [$m2 cget
-type] eq
"menubar"} {
850 set w
$Priv(postedMb
)
854 set buttons
[winfo children
[winfo parent
$w]]
855 set length
[llength $buttons]
856 set i
[expr {[lsearch -exact $buttons $w] + $count}]
861 while {$i >= $length} {
864 set mb
[lindex $buttons $i]
865 if {[winfo class
$mb] eq
"Menubutton" \
866 && [$mb cget
-state] ne
"disabled" \
867 && [$mb cget
-menu] ne
"" \
868 && [[$mb cget
-menu] index last
] ne
"none"} {
877 MenuFirstEntry
[$mb cget
-menu]
880 # ::tk::MenuNextEntry --
881 # Activate the next higher or lower entry in the posted menu,
882 # wrapping around at the ends. Disabled entries are skipped.
885 # menu - Menu window that received the keystroke.
886 # count - 1 means go to the next lower entry,
887 # -1 means go to the next higher entry.
889 proc ::tk::MenuNextEntry {menu count
} {
890 if {[$menu index last
] eq
"none"} {
893 set length
[expr {[$menu index last
]+1}]
894 set quitAfter
$length
895 set active
[$menu index active
]
896 if {$active eq
"none"} {
899 set i
[expr {$active + $count}]
902 if {$quitAfter <= 0} {
903 # We've tried every entry in the menu. Either there are
904 # none, or they're all disabled. Just give up.
911 while {$i >= $length} {
914 if {[catch {$menu entrycget
$i -state} state
] == 0} {
915 if {$state ne
"disabled" && \
916 ($i!=0 ||
[$menu cget
-type] ne
"tearoff" \
917 ||
[$menu type
0] ne
"tearoff")} {
928 GenerateMenuSelect
$menu
930 if {[$menu type
$i] eq
"cascade" && [$menu cget
-type] eq
"menubar"} {
931 set cascade
[$menu entrycget
$i -menu]
932 if {$cascade ne
""} {
933 # Here we auto-post a cascade. This is necessary when
934 # we traverse left/right in the menubar, but undesirable when
935 # we traverse up/down in a menu.
937 MenuFirstEntry
$cascade
943 # This procedure searches the entire window hierarchy under w for
944 # a menubutton that isn't disabled and whose underlined character
945 # is "char" or an entry in a menubar that isn't disabled and whose
946 # underlined character is "char".
947 # It returns the name of that window, if found, or an
948 # empty string if no matching window was found. If "char" is an
949 # empty string then the procedure returns the name of the first
950 # menubutton found that isn't disabled.
953 # w - Name of window where key was typed.
954 # char - Underlined character to search for;
955 # may be either upper or lower case, and
956 # will match either upper or lower case.
958 proc ::tk::MenuFind {w char
} {
959 set char
[string tolower
$char]
960 set windowlist
[winfo child
$w]
962 foreach child
$windowlist {
963 # Don't descend into other toplevels.
964 if {[winfo toplevel $w] ne
[winfo toplevel $child]} {
967 if {[winfo class
$child] eq
"Menu" && \
968 [$child cget
-type] eq
"menubar"} {
972 set last
[$child index last
]
973 for {set i
[$child cget
-tearoff]} {$i <= $last} {incr i
} {
974 if {[$child type
$i] eq
"separator"} {
977 set char2
[string index
[$child entrycget
$i -label] \
978 [$child entrycget
$i -underline]]
979 if {$char eq
[string tolower
$char2] ||
$char eq
""} {
980 if {[$child entrycget
$i -state] ne
"disabled"} {
988 foreach child
$windowlist {
989 # Don't descend into other toplevels.
990 if {[winfo toplevel $w] ne
[winfo toplevel $child]} {
993 switch -- [winfo class
$child] {
995 set char2
[string index
[$child cget
-text] \
996 [$child cget
-underline]]
997 if {$char eq
[string tolower
$char2] ||
$char eq
""} {
998 if {[$child cget
-state] ne
"disabled"} {
1005 set match
[MenuFind
$child $char]
1015 # ::tk::TraverseToMenu --
1016 # This procedure implements keyboard traversal of menus. Given an
1017 # ASCII character "char", it looks for a menubutton with that character
1018 # underlined. If one is found, it posts the menubutton's menu
1021 # w - Window in which the key was typed (selects
1022 # a toplevel window).
1023 # char - Character that selects a menu. The case
1024 # is ignored. If an empty string, nothing
1027 proc ::tk::TraverseToMenu {w char
} {
1032 while {[winfo class
$w] eq
"Menu"} {
1033 if {[$w cget
-type] ne
"menubar" && $Priv(postedMb
) eq
""} {
1036 if {[$w cget
-type] eq
"menubar"} {
1039 set w
[winfo parent
$w]
1041 set w
[MenuFind
[winfo toplevel $w] $char]
1043 if {[winfo class
$w] eq
"Menu"} {
1048 TraverseWithinMenu
$w $char
1051 MenuFirstEntry
[$w cget
-menu]
1056 # ::tk::FirstMenu --
1057 # This procedure traverses to the first menubutton in the toplevel
1058 # for a given window, and posts that menubutton's menu.
1061 # w - Name of a window. Selects which toplevel
1062 # to search for menubuttons.
1064 proc ::tk::FirstMenu w
{
1066 set w
[MenuFind
[winfo toplevel $w] ""]
1068 if {[winfo class
$w] eq
"Menu"} {
1076 MenuFirstEntry
[$w cget
-menu]
1081 # ::tk::TraverseWithinMenu
1082 # This procedure implements keyboard traversal within a menu. It
1083 # searches for an entry in the menu that has "char" underlined. If
1084 # such an entry is found, it is invoked and the menu is unposted.
1087 # w - The name of the menu widget.
1088 # char - The character to look for; case is
1089 # ignored. If the string is empty then
1092 proc ::tk::TraverseWithinMenu {w char
} {
1096 set char
[string tolower
$char]
1097 set last
[$w index last
]
1098 if {$last eq
"none"} {
1101 for {set i
0} {$i <= $last} {incr i
} {
1102 if {[catch {set char2
[string index
\
1103 [$w entrycget
$i -label] [$w entrycget
$i -underline]]}]} {
1106 if {$char eq
[string tolower
$char2]} {
1107 if {[$w type
$i] eq
"cascade"} {
1109 $w postcascade active
1110 event generate
$w <<MenuSelect
>>
1111 set m2
[$w entrycget
$i -menu]
1117 uplevel #0 [list $w invoke $i]
1124 # ::tk::MenuFirstEntry --
1125 # Given a menu, this procedure finds the first entry that isn't
1126 # disabled or a tear-off or separator, and activates that entry.
1127 # However, if there is already an active entry in the menu (e.g.,
1128 # because of a previous call to tk::PostOverPoint) then the active
1129 # entry isn't changed. This procedure also sets the input focus
1133 # menu - Name of the menu window (possibly empty).
1135 proc ::tk::MenuFirstEntry menu {
1139 tk_menuSetFocus $menu
1140 if {[$menu index active
] ne
"none"} {
1143 set last
[$menu index last
]
1144 if {$last eq
"none"} {
1147 for {set i
0} {$i <= $last} {incr i
} {
1148 if {([catch {set state
[$menu entrycget
$i -state]}] == 0) \
1149 && $state ne
"disabled" && [$menu type
$i] ne
"tearoff"} {
1151 GenerateMenuSelect
$menu
1152 # Only post the cascade if the current menu is a menubar;
1153 # otherwise, if the first entry of the cascade is a cascade,
1154 # we can get an annoying cascading effect resulting in a bunch of
1155 # menus getting posted (bug 676)
1156 if {[$menu type
$i] eq
"cascade" \
1157 && [$menu cget
-type] eq
"menubar"} {
1158 set cascade
[$menu entrycget
$i -menu]
1159 if {$cascade ne
""} {
1160 $menu postcascade
$i
1161 MenuFirstEntry
$cascade
1169 # ::tk::MenuFindName --
1170 # Given a menu and a text string, return the index of the menu entry
1171 # that displays the string as its label. If there is no such entry,
1172 # return an empty string. This procedure is tricky because some names
1173 # like "active" have a special meaning in menu commands, so we can't
1174 # always use the "index" widget command.
1177 # menu - Name of the menu widget.
1178 # s - String to look for.
1180 proc ::tk::MenuFindName {menu s
} {
1182 if {![regexp {^active
$|^last
$|^none
$|^
[0-9]|^
@} $s]} {
1183 catch {set i
[$menu index
$s]}
1186 set last
[$menu index last
]
1187 if {$last eq
"none"} {
1190 for {set i
0} {$i <= $last} {incr i
} {
1191 if {![catch {$menu entrycget
$i -label} label]} {
1200 # ::tk::PostOverPoint --
1201 # This procedure posts a given menu such that a given entry in the
1202 # menu is centered over a given point in the root window. It also
1203 # activates the given entry.
1206 # menu - Menu to post.
1207 # x, y - Root coordinates of point.
1208 # entry - Index of entry within menu to center over (x,y).
1209 # If omitted or specified as {}, then the menu's
1210 # upper-left corner goes at (x,y).
1212 proc ::tk::PostOverPoint {menu x y
{entry {}}} {
1216 if {$entry == [$menu index last
]} {
1217 incr y
[expr {-([$menu yposition
$entry] \
1218 + [winfo reqheight
$menu])/2}]
1220 incr y
[expr {-([$menu yposition
$entry] \
1221 + [$menu yposition
[expr {$entry+1}]])/2}]
1223 incr x
[expr {-[winfo reqwidth
$menu]/2}]
1226 if {$tcl_platform(platform
) eq
"windows"} {
1227 # osVersion is not available in safe interps
1229 if {[info exists tcl_platform
(osVersion
)]} {
1230 scan $tcl_platform(osVersion
) %d ver
1233 # We need to fix some problems with menu posting on Windows,
1234 # where, if the menu would overlap top or bottom of screen,
1235 # Windows puts it in the wrong place for us. We must also
1236 # subtract an extra amount for half the height of the current
1237 # entry. To be safe we subtract an extra 10.
1238 # NOTE: this issue appears to have been resolved in the Window
1239 # manager provided with Vista and Windows 7.
1241 set yoffset
[expr {[winfo screenheight
$menu] \
1242 - $y - [winfo reqheight
$menu] - 10}]
1244 # The bottom of the menu is offscreen, so adjust upwards
1246 if {$y < 0} { set y
0 }
1248 # If we're off the top of the screen (either because we were
1249 # originally or because we just adjusted too far upwards),
1250 # then make the menu popup on the top edge.
1257 if {$entry ne
"" && [$menu entrycget
$entry -state] ne
"disabled"} {
1258 $menu activate
$entry
1259 GenerateMenuSelect
$menu
1263 # ::tk::SaveGrabInfo --
1264 # Sets the variables tk::Priv(oldGrab) and tk::Priv(grabStatus) to record
1265 # the state of any existing grab on the w's display.
1268 # w - Name of a window; used to select the display
1269 # whose grab information is to be recorded.
1271 proc tk::SaveGrabInfo w
{
1273 set Priv
(oldGrab
) [grab current
$w]
1274 if {$Priv(oldGrab
) ne
""} {
1275 set Priv
(grabStatus
) [grab status
$Priv(oldGrab
)]
1279 # ::tk::RestoreOldGrab --
1280 # Restores the grab to what it was before TkSaveGrabInfo was called.
1283 proc ::tk::RestoreOldGrab {} {
1286 if {$Priv(oldGrab
) ne
""} {
1287 # Be careful restoring the old grab, since it's window may not
1288 # be visible anymore.
1291 if {$Priv(grabStatus
) eq
"global"} {
1292 grab set -global $Priv(oldGrab
)
1294 grab set $Priv(oldGrab
)
1297 set Priv
(oldGrab
) ""
1301 proc ::tk_menuSetFocus {menu} {
1303 if {![info exists Priv
(focus)] ||
$Priv(focus) eq
""} {
1304 set Priv
(focus) [focus]
1309 proc ::tk::GenerateMenuSelect {menu} {
1312 if {$Priv(activeMenu
) eq
$menu \
1313 && $Priv(activeItem
) eq
[$menu index active
]} {
1317 set Priv
(activeMenu
) $menu
1318 set Priv
(activeItem
) [$menu index active
]
1319 event generate
$menu <<MenuSelect
>>
1323 # This procedure pops up a menu and sets things up for traversing
1324 # the menu and its submenus.
1327 # menu - Name of the menu to be popped up.
1328 # x, y - Root coordinates at which to pop up the
1330 # entry - Index of a menu entry to center over (x,y).
1331 # If omitted or specified as {}, then menu's
1332 # upper-left corner goes at (x,y).
1334 proc ::tk_popup {menu x y
{entry {}}} {
1337 if {$Priv(popup
) ne
"" ||
$Priv(postedMb
) ne
""} {
1340 tk::PostOverPoint $menu $x $y $entry
1341 if {[tk windowingsystem
] eq
"x11" && [winfo viewable
$menu]} {
1342 tk::SaveGrabInfo $menu
1344 set Priv
(popup
) $menu
1345 set Priv
(menuActivated
) 1
1346 tk_menuSetFocus $menu