e00dad9fa27d082da2ab87c2a8a087d2ca0e2090
[msysgit.git] / mingw / lib / tk8.5 / menu.tcl
blobe00dad9fa27d082da2ab87c2a8a087d2ca0e2090
1 # menu.tcl --
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
26 # a global one.
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
40 # value is empty.
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
45 # menubutton.
46 # window - When the mouse is over a menu, this holds the
47 # name of the menu; it's cleared when the mouse
48 # leaves the menu.
49 # tearoff - Whether the last menu posted was a tearoff or not.
50 # This is true always for unix, for tearoffs for Mac
51 # and Windows.
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 #-------------------------------------------------------------------------
59 # Overall note:
60 # This file is tricky because there are five different ways that menus
61 # can be used:
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
76 # case.
77 #-------------------------------------------------------------------------
79 #-------------------------------------------------------------------------
80 # The code below creates the default class bindings for menus
81 # and menubuttons.
82 #-------------------------------------------------------------------------
84 bind Menubutton <FocusIn> {}
85 bind Menubutton <Enter> {
86 tk::MbEnter %W
88 bind Menubutton <Leave> {
89 tk::MbLeave %W
91 bind Menubutton <1> {
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> {
103 tk::MbButtonUp %W
105 bind Menubutton <space> {
106 tk::MbPost %W
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> {}
119 bind Menu <Enter> {
120 set tk::Priv(window) %W
121 if {[%W cget -type] eq "tearoff"} {
122 if {"%m" ne "NotifyUngrab"} {
123 if {[tk windowingsystem] eq "x11"} {
124 tk_menuSetFocus %W
128 tk::MenuMotion %W %x %y %s
131 bind Menu <Leave> {
132 tk::MenuLeave %W %X %Y %s
134 bind Menu <Motion> {
135 tk::MenuMotion %W %x %y %s
137 bind Menu <ButtonPress> {
138 tk::MenuButtonDown %W
140 bind Menu <ButtonRelease> {
141 tk::MenuInvoke %W 1
143 bind Menu <space> {
144 tk::MenuInvoke %W 0
146 bind Menu <Return> {
147 tk::MenuInvoke %W 0
149 bind Menu <Escape> {
150 tk::MenuEscape %W
152 bind Menu <Left> {
153 tk::MenuLeftArrow %W
155 bind Menu <Right> {
156 tk::MenuRightArrow %W
158 bind Menu <Up> {
159 tk::MenuUpArrow %W
161 bind Menu <Down> {
162 tk::MenuDownArrow %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
176 bind all <F10> {
177 tk::FirstMenu %W
179 } else {
180 bind Menubutton <Alt-KeyPress> {
181 tk::TraverseToMenu %W %A
184 bind Menubutton <F10> {
185 tk::FirstMenu %W
189 # ::tk::MbEnter --
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.
195 # Arguments:
196 # w - The name of the widget.
198 proc ::tk::MbEnter w {
199 variable ::tk::Priv
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
210 # ::tk::MbLeave --
211 # This procedure is invoked when the mouse leaves a menubutton widget.
212 # It de-activates the widget, if the widget still exists.
214 # Arguments:
215 # w - The name of the widget.
217 proc ::tk::MbLeave w {
218 variable ::tk::Priv
220 set Priv(inMenubutton) {}
221 if {![winfo exists $w]} {
222 return
224 if {[$w cget -state] eq "active" && [tk windowingsystem] ne "aqua"} {
225 $w configure -state normal
229 # ::tk::MbPost --
230 # Given a menubutton, this procedure does all the work of posting
231 # its associated menu and unposting any other menu that is currently
232 # posted.
234 # Arguments:
235 # w - The name of the menubutton widget whose menu
236 # is to be posted.
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 {}}} {
242 global errorInfo
243 variable ::tk::Priv
244 global tcl_platform
246 if {[$w cget -state] eq "disabled" || $w eq $Priv(postedMb)} {
247 return
249 set menu [$w cget -menu]
250 if {$menu eq ""} {
251 return
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)
259 if {$cur ne ""} {
260 MenuUnpost {}
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
269 } else {
270 $w configure -state active
273 set Priv(postedMb) $w
274 set Priv(focus) [focus]
275 $menu activate none
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.
282 update idletasks
283 if {[catch {
284 switch [$w cget -direction] {
285 above {
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'
289 if {$y < 0} {
290 set y [expr {[winfo rooty $w] + [winfo height $w]}]
292 PostOverPoint $menu $x $y
294 below {
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
304 left {
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}]
312 } else {
313 incr y [expr {-([$menu yposition $entry] \
314 + [$menu yposition [expr {$entry+1}]])/2}]
317 PostOverPoint $menu $x $y
318 if {$entry ne "" \
319 && [$menu entrycget $entry -state] ne "disabled"} {
320 $menu activate $entry
321 GenerateMenuSelect $menu
324 right {
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}]
332 } else {
333 incr y [expr {-([$menu yposition $entry] \
334 + [$menu yposition [expr {$entry+1}]])/2}]
337 PostOverPoint $menu $x $y
338 if {$entry ne "" \
339 && [$menu entrycget $entry -state] ne "disabled"} {
340 $menu activate $entry
341 GenerateMenuSelect $menu
344 default {
345 if {[$w cget -indicatoron]} {
346 if {$y eq ""} {
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]]
351 } else {
352 PostOverPoint $menu [winfo rootx $w] [expr {[winfo rooty $w]+[winfo height $w]}]
356 } msg]} {
357 # Error posting menu (e.g. bogus -postcommand). Unpost it and
358 # reflect the error.
360 set savedInfo $errorInfo
361 MenuUnpost {}
362 error $msg $savedInfo
366 set Priv(tearoff) $tearoff
367 if {$tearoff != 0} {
368 focus $menu
369 if {[winfo viewable $w]} {
370 SaveGrabInfo $w
371 grab -global $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.
388 # Arguments:
389 # menu - Name of a menu to unpost. Ignored if there
390 # is a posted menubutton.
392 proc ::tk::MenuUnpost menu {
393 global tcl_platform
394 variable ::tk::Priv
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)}
402 set Priv(focus) ""
404 # Unpost menu(s) and restore some stuff that's dependent on
405 # what was posted.
407 after cancel [array get Priv menuActivatedTimer]
408 unset -nocomplain Priv(menuActivated)
409 after cancel [array get Priv menuDeactivatedTimer]
410 unset -nocomplain Priv(menuDeactivated)
412 catch {
413 if {$mb ne ""} {
414 set menu [$mb cget -menu]
415 $menu unpost
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)
422 } else {
423 $mb configure -state normal
425 } elseif {$Priv(popup) ne ""} {
426 $Priv(popup) unpost
427 set Priv(popup) {}
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.
435 while {1} {
436 set parent [winfo parent $menu]
437 if {[winfo class $parent] ne "Menu" \
438 || ![winfo ismapped $parent]} {
439 break
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"} {
446 break
448 set menu $parent
450 if {[$menu cget -type] ne "menubar"} {
451 $menu unpost
456 if {($Priv(tearoff) != 0) || $Priv(menuBar) ne ""} {
457 # Release grab, if any, and restore the previous grab, if there
458 # was one.
459 if {$menu ne ""} {
460 set grab [grab current $menu]
461 if {$grab ne ""} {
462 grab release $grab
465 RestoreOldGrab
466 if {$Priv(menuBar) ne ""} {
467 if {$::tk_strictMotif} {
468 $Priv(menuBar) configure -cursor $Priv(cursor)
470 set Priv(menuBar) {}
472 if {[tk windowingsystem] ne "x11"} {
473 set Priv(tearoff) 0
478 # ::tk::MbMotion --
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).
483 # Arguments:
484 # w - The name of the menubutton widget.
485 # upDown - "down" means button 1 is pressed, "up" means
486 # it isn't.
487 # rootx, rooty - Coordinates of mouse, in (virtual?) root window.
489 proc ::tk::MbMotion {w upDown rootx rooty} {
490 variable ::tk::Priv
492 if {$Priv(inMenubutton) eq $w} {
493 return
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)
501 if {$new ne "" \
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
507 } else {
508 MbEnter $new
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.
519 # Arguments:
520 # w - The name of the menubutton widget.
522 proc ::tk::MbButtonUp w {
523 variable ::tk::Priv
524 global tcl_platform
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]
532 } else {
533 MenuUnpost {}
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
542 # position.
544 # Arguments:
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} {
551 variable ::tk::Priv
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
559 } else {
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]]
573 } else {
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
584 # here:
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
589 # the posted menu.
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.
594 # Arguments:
595 # menu - The menu window.
597 proc ::tk::MenuButtonDown menu {
598 variable ::tk::Priv
599 global tcl_platform
601 if {![winfo viewable $menu]} {
602 return
604 $menu postcascade active
605 if {$Priv(postedMb) ne "" && [winfo viewable $Priv(postedMb)]} {
606 grab -global $Priv(postedMb)
607 } else {
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
628 # anymore.
630 if {$menu ne [grab current $menu]} {
631 SaveGrabInfo $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"} {
638 grab -global $menu
643 # ::tk::MenuLeave --
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.
648 # Arguments:
649 # menu - The menu window.
650 # rootx, rooty - Root coordinates of mouse.
651 # state - Modifier state.
653 proc ::tk::MenuLeave {menu rootx rooty state} {
654 variable ::tk::Priv
655 set Priv(window) {}
656 if {[$menu index active] eq "none"} {
657 return
659 if {[$menu type active] eq "cascade" \
660 && [winfo containing $rootx $rooty] eq \
661 [$menu entrycget active -menu]} {
662 return
664 $menu activate none
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.
673 # Arguments:
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} {
679 variable ::tk::Priv
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.
686 $w postcascade none
687 $w activate none
688 event generate $w <<MenuSelect>>
689 MenuUnpost $w
690 return
692 if {[$w type active] eq "cascade"} {
693 $w postcascade active
694 set menu [$w entrycget active -menu]
695 MenuFirstEntry $menu
696 } elseif {[$w type active] eq "tearoff"} {
697 ::tk::TearOffMenu $w
698 MenuUnpost $w
699 } elseif {[$w cget -type] eq "menubar"} {
700 $w postcascade none
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
708 if { $isCascade } {
709 $w activate none
710 event generate $w <<MenuSelect>>
713 MenuUnpost $w
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)
719 if { !$isCascade } {
720 uplevel #0 [list $w invoke $active]
722 } else {
723 set active [$w index active]
724 if {$Priv(popup) eq "" || $active ne "none"} {
725 MenuUnpost $w
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.
736 # Arguments:
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"} {
742 MenuUnpost $menu
743 } elseif {[$parent cget -type] eq "menubar"} {
744 MenuUnpost $menu
745 RestoreOldGrab
746 } else {
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
757 } else {
758 MenuNextEntry $menu -1
762 proc ::tk::MenuDownArrow {menu} {
763 if {[$menu cget -type] eq "menubar"} {
764 MenuNextMenu $menu right
765 } else {
766 MenuNextEntry $menu 1
770 proc ::tk::MenuLeftArrow {menu} {
771 if {[$menu cget -type] eq "menubar"} {
772 MenuNextEntry $menu -1
773 } else {
774 MenuNextMenu $menu left
778 proc ::tk::MenuRightArrow {menu} {
779 if {[$menu cget -type] eq "menubar"} {
780 MenuNextEntry $menu 1
781 } else {
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.
791 # Arguments:
792 # menu - The menu that received the keyboard
793 # event.
794 # direction - Direction in which to move: "left" or "right"
796 proc ::tk::MenuNextMenu {menu direction} {
797 variable ::tk::Priv
799 # First handle traversals into and out of cascaded menus.
801 if {$direction eq "right"} {
802 set count 1
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]
808 if {$m2 ne ""} {
809 MenuFirstEntry $m2
811 return
812 } else {
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
819 return
821 set parent [winfo parent $parent]
824 } else {
825 set count -1
826 set m2 [winfo parent $menu]
827 if {[winfo class $m2] eq "Menu"} {
828 $menu activate none
829 GenerateMenuSelect $menu
830 tk_menuSetFocus $m2
832 $m2 postcascade none
834 if {[$m2 cget -type] ne "menubar"} {
835 return
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"} {
845 tk_menuSetFocus $m2
846 MenuNextEntry $m2 -1
847 return
850 set w $Priv(postedMb)
851 if {$w eq ""} {
852 return
854 set buttons [winfo children [winfo parent $w]]
855 set length [llength $buttons]
856 set i [expr {[lsearch -exact $buttons $w] + $count}]
857 while {1} {
858 while {$i < 0} {
859 incr i $length
861 while {$i >= $length} {
862 incr 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"} {
869 break
871 if {$mb eq $w} {
872 return
874 incr i $count
876 MbPost $mb
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.
884 # Arguments:
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"} {
891 return
893 set length [expr {[$menu index last]+1}]
894 set quitAfter $length
895 set active [$menu index active]
896 if {$active eq "none"} {
897 set i 0
898 } else {
899 set i [expr {$active + $count}]
901 while {1} {
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.
906 return
908 while {$i < 0} {
909 incr i $length
911 while {$i >= $length} {
912 incr 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")} {
918 break
921 if {$i == $active} {
922 return
924 incr i $count
925 incr quitAfter -1
927 $menu activate $i
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.
936 $menu postcascade $i
937 MenuFirstEntry $cascade
942 # ::tk::MenuFind --
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.
952 # Arguments:
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]} {
965 continue
967 if {[winfo class $child] eq "Menu" && \
968 [$child cget -type] eq "menubar"} {
969 if {$char eq ""} {
970 return $child
972 set last [$child index last]
973 for {set i [$child cget -tearoff]} {$i <= $last} {incr i} {
974 if {[$child type $i] eq "separator"} {
975 continue
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"} {
981 return $child
988 foreach child $windowlist {
989 # Don't descend into other toplevels.
990 if {[winfo toplevel $w] ne [winfo toplevel $child]} {
991 continue
993 switch -- [winfo class $child] {
994 Menubutton {
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"} {
999 return $child
1004 default {
1005 set match [MenuFind $child $char]
1006 if {$match ne ""} {
1007 return $match
1012 return {}
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
1020 # Arguments:
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
1025 # happens.
1027 proc ::tk::TraverseToMenu {w char} {
1028 variable ::tk::Priv
1029 if {$char eq ""} {
1030 return
1032 while {[winfo class $w] eq "Menu"} {
1033 if {[$w cget -type] ne "menubar" && $Priv(postedMb) eq ""} {
1034 return
1036 if {[$w cget -type] eq "menubar"} {
1037 break
1039 set w [winfo parent $w]
1041 set w [MenuFind [winfo toplevel $w] $char]
1042 if {$w ne ""} {
1043 if {[winfo class $w] eq "Menu"} {
1044 tk_menuSetFocus $w
1045 set Priv(window) $w
1046 SaveGrabInfo $w
1047 grab -global $w
1048 TraverseWithinMenu $w $char
1049 } else {
1050 MbPost $w
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.
1060 # Arguments:
1061 # w - Name of a window. Selects which toplevel
1062 # to search for menubuttons.
1064 proc ::tk::FirstMenu w {
1065 variable ::tk::Priv
1066 set w [MenuFind [winfo toplevel $w] ""]
1067 if {$w ne ""} {
1068 if {[winfo class $w] eq "Menu"} {
1069 tk_menuSetFocus $w
1070 set Priv(window) $w
1071 SaveGrabInfo $w
1072 grab -global $w
1073 MenuFirstEntry $w
1074 } else {
1075 MbPost $w
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.
1086 # Arguments:
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
1090 # nothing happens.
1092 proc ::tk::TraverseWithinMenu {w char} {
1093 if {$char eq ""} {
1094 return
1096 set char [string tolower $char]
1097 set last [$w index last]
1098 if {$last eq "none"} {
1099 return
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]]}]} {
1104 continue
1106 if {$char eq [string tolower $char2]} {
1107 if {[$w type $i] eq "cascade"} {
1108 $w activate $i
1109 $w postcascade active
1110 event generate $w <<MenuSelect>>
1111 set m2 [$w entrycget $i -menu]
1112 if {$m2 ne ""} {
1113 MenuFirstEntry $m2
1115 } else {
1116 MenuUnpost $w
1117 uplevel #0 [list $w invoke $i]
1119 return
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
1130 # to the menu.
1132 # Arguments:
1133 # menu - Name of the menu window (possibly empty).
1135 proc ::tk::MenuFirstEntry menu {
1136 if {$menu eq ""} {
1137 return
1139 tk_menuSetFocus $menu
1140 if {[$menu index active] ne "none"} {
1141 return
1143 set last [$menu index last]
1144 if {$last eq "none"} {
1145 return
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"} {
1150 $menu activate $i
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
1164 return
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.
1176 # Arguments:
1177 # menu - Name of the menu widget.
1178 # s - String to look for.
1180 proc ::tk::MenuFindName {menu s} {
1181 set i ""
1182 if {![regexp {^active$|^last$|^none$|^[0-9]|^@} $s]} {
1183 catch {set i [$menu index $s]}
1184 return $i
1186 set last [$menu index last]
1187 if {$last eq "none"} {
1188 return
1190 for {set i 0} {$i <= $last} {incr i} {
1191 if {![catch {$menu entrycget $i -label} label]} {
1192 if {$label eq $s} {
1193 return $i
1197 return ""
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.
1205 # Arguments:
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 {}}} {
1213 global tcl_platform
1215 if {$entry ne ""} {
1216 if {$entry == [$menu index last]} {
1217 incr y [expr {-([$menu yposition $entry] \
1218 + [winfo reqheight $menu])/2}]
1219 } else {
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
1228 set ver 5
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.
1240 if {$ver < 6} {
1241 set yoffset [expr {[winfo screenheight $menu] \
1242 - $y - [winfo reqheight $menu] - 10}]
1243 if {$yoffset < 0} {
1244 # The bottom of the menu is offscreen, so adjust upwards
1245 incr y $yoffset
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.
1251 if {$y < 0} {
1252 set y 0
1256 $menu post $x $y
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.
1267 # Arguments:
1268 # w - Name of a window; used to select the display
1269 # whose grab information is to be recorded.
1271 proc tk::SaveGrabInfo w {
1272 variable ::tk::Priv
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 {} {
1284 variable ::tk::Priv
1286 if {$Priv(oldGrab) ne ""} {
1287 # Be careful restoring the old grab, since it's window may not
1288 # be visible anymore.
1290 catch {
1291 if {$Priv(grabStatus) eq "global"} {
1292 grab set -global $Priv(oldGrab)
1293 } else {
1294 grab set $Priv(oldGrab)
1297 set Priv(oldGrab) ""
1301 proc ::tk_menuSetFocus {menu} {
1302 variable ::tk::Priv
1303 if {![info exists Priv(focus)] || $Priv(focus) eq ""} {
1304 set Priv(focus) [focus]
1306 focus $menu
1309 proc ::tk::GenerateMenuSelect {menu} {
1310 variable ::tk::Priv
1312 if {$Priv(activeMenu) eq $menu \
1313 && $Priv(activeItem) eq [$menu index active]} {
1314 return
1317 set Priv(activeMenu) $menu
1318 set Priv(activeItem) [$menu index active]
1319 event generate $menu <<MenuSelect>>
1322 # ::tk_popup --
1323 # This procedure pops up a menu and sets things up for traversing
1324 # the menu and its submenus.
1326 # Arguments:
1327 # menu - Name of the menu to be popped up.
1328 # x, y - Root coordinates at which to pop up the
1329 # menu.
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 {}}} {
1335 variable ::tk::Priv
1336 global tcl_platform
1337 if {$Priv(popup) ne "" || $Priv(postedMb) ne ""} {
1338 tk::MenuUnpost {}
1340 tk::PostOverPoint $menu $x $y $entry
1341 if {[tk windowingsystem] eq "x11" && [winfo viewable $menu]} {
1342 tk::SaveGrabInfo $menu
1343 grab -global $menu
1344 set Priv(popup) $menu
1345 set Priv(menuActivated) 1
1346 tk_menuSetFocus $menu