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-1995 Sun Microsystems, Inc.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 #-------------------------------------------------------------------------
15 # Elements of ckPriv that are used in this file:
17 # focus - Saves the focus during a menu selection operation.
18 # Focus gets restored here when the menu is unposted.
19 # postedMb - Name of the menubutton whose menu is currently
20 # posted, or an empty string if nothing is posted
21 # popup - If a menu has been popped up via ck_popup, this
22 # gives the name of the menu. Otherwise this value
24 #-------------------------------------------------------------------------
26 set ckPriv(postedMb) ""
29 #-------------------------------------------------------------------------
30 # The code below creates the default class bindings for menus
32 #-------------------------------------------------------------------------
34 bind Menubutton <FocusIn> {}
35 bind Menubutton <space> {
37 ckMenuFirstEntry [%W cget -menu]
40 bind Menubutton <Return> {
42 ckMenuFirstEntry [%W cget -menu]
45 bind Menubutton <Linefeed> {
47 ckMenuFirstEntry [%W cget -menu]
50 bind Menubutton <Button-1> {
52 ckMenuFirstEntry [%W cget -menu]
62 bind Menu <Linefeed> {
66 ckMenuEscape %W ; break
69 ckMenuLeftRight %W left
72 ckMenuLeftRight %W right
80 bind Menu <KeyPress> {
81 ckTraverseWithinMenu %W %A
83 bind Menu <Button-1> {
93 # Given a menubutton, this procedure does all the work of posting
94 # its associated menu and unposting any other menu that is currently
98 # w - The name of the menubutton widget whose menu
100 # x, y - Root coordinates of cursor, used for positioning
101 # option menus. If not specified, then the center
102 # of the menubutton is used for an option menu.
104 proc ckMbPost {w {x {}} {y {}}} {
106 if {([$w cget -state] == "disabled") || ($w == $ckPriv(postedMb))} {
109 set menu [$w cget -menu]
113 if ![string match $w.* $menu] {
114 error "can't post $menu: it isn't a descendant of $w"
116 set cur $ckPriv(postedMb)
120 set ckPriv(postedMb) $w
121 set ckPriv(focus) [focus]
124 # If this looks like an option menubutton then post the menu so
125 # that the current entry is on top of the mouse. Otherwise post
126 # the menu just below the menubutton, as for a pull-down.
128 if {([$w cget -indicatoron] == 1) && ([$w cget -textvariable] != "")} {
130 set x [expr [winfo rootx $w] + [winfo width $w]/2]
131 set y [expr [winfo rooty $w] + [winfo height $w]/2]
133 ckPostOverPoint $menu $x $y [ckMenuFindName $menu [$w cget -text]]
135 $menu post [winfo rootx $w] [expr [winfo rooty $w]+[winfo height $w]]
138 $w configure -state active
143 # This procedure unposts a given menu, plus all of its ancestors up
144 # to (and including) a menubutton, if any. It also restores various
145 # values to what they were before the menu was posted, and releases
146 # a grab if there's a menubutton involved. Special notes:
147 # Be sure to enclose various groups of commands in "catch" so that
148 # the procedure will complete even if the menubutton or the menu
152 # menu - Name of a menu to unpost. Ignored if there
153 # is a posted menubutton.
155 proc ckMenuUnpost menu {
157 set mb $ckPriv(postedMb)
160 $mb configure -state normal
161 catch {focus $ckPriv(focus)}
163 set menu [$mb cget -menu]
165 set ckPriv(postedMb) {}
166 } elseif {[string length $ckPriv(popup)]} {
167 catch {focus $ckPriv(focus)}
169 $ckPriv(popup) unpost
176 # This procedure is invoked when button 1 is released over a menu.
177 # It invokes the appropriate menu action and unposts the menu if
178 # it came from a menubutton.
181 # w - Name of the menu widget.
183 proc ckMenuInvoke w {
184 if {[$w type active] == "cascade"} {
185 $w postcascade active
186 set menu [$w entrycget active -menu]
187 ckMenuFirstEntry $menu
190 uplevel #0 [list $w invoke active]
195 # This procedure is invoked for the Cancel (or Escape) key. It unposts
196 # the given menu and, if it is the top-level menu for a menu button,
197 # unposts the menu button as well.
200 # menu - Name of the menu window.
202 proc ckMenuEscape menu {
203 if {[winfo class [winfo parent $menu]] != "Menu"} {
206 ckMenuLeftRight $menu -1
211 # This procedure is invoked to handle "left" and "right" traversal
212 # motions in menus. It traverses to the next menu in a menu bar,
213 # or into or out of a cascaded menu.
216 # menu - The menu that received the keyboard
218 # direction - Direction in which to move: "left" or "right"
220 proc ckMenuLeftRight {menu direction} {
223 # First handle traversals into and out of cascaded menus.
225 if {$direction == "right"} {
227 if {[$menu type active] == "cascade"} {
228 $menu postcascade active
229 set m2 [$menu entrycget active -menu]
237 set m2 [winfo parent $menu]
238 if {[winfo class $m2] == "Menu"} {
245 # Can't traverse into or out of a cascaded menu. Go to the next
246 # or previous menubutton, if that makes sense.
248 set w $ckPriv(postedMb)
252 set buttons [winfo children [winfo parent $w]]
253 set length [llength $buttons]
254 set i [expr [lsearch -exact $buttons $w] + $count]
259 while {$i >= $length} {
262 set mb [lindex $buttons $i]
263 if {([winfo class $mb] == "Menubutton")
264 && ([$mb cget -state] != "disabled")
265 && ([$mb cget -menu] != "")
266 && ([[$mb cget -menu] index last] != "none")} {
274 if {[ckMbPost $mb]} {
275 ckMenuFirstEntry [$mb cget -menu]
280 # Activate the next higher or lower entry in the posted menu,
281 # wrapping around at the ends. Disabled entries are skipped.
284 # menu - Menu window that received the keystroke.
285 # count - 1 means go to the next lower entry,
286 # -1 means go to the next higher entry.
288 proc ckMenuNextEntry {menu count} {
290 if {[$menu index last] == "none"} {
293 set length [expr [$menu index last]+1]
294 set active [$menu index active]
295 if {$active == "none"} {
298 set i [expr $active + $count]
304 while {$i >= $length} {
307 if {[catch {$menu entrycget $i -state} state] == 0} {
308 if {$state != "disabled"} {
321 # This procedure searches the entire window hierarchy under w for
322 # a menubutton that isn't disabled and whose underlined character
323 # is "char". It returns the name of that window, if found, or an
324 # empty string if no matching window was found. If "char" is an
325 # empty string then the procedure returns the name of the first
326 # menubutton found that isn't disabled.
329 # w - Name of window where key was typed.
330 # char - Underlined character to search for;
331 # may be either upper or lower case, and
332 # will match either upper or lower case.
334 proc ckMenuFind {w char} {
336 set char [string tolower $char]
338 foreach child [winfo child $w] {
339 switch [winfo class $child] {
341 set char2 [string index [$child cget -text] \
342 [$child cget -underline]]
343 if {([string compare $char [string tolower $char2]] == 0)
345 if {[$child cget -state] != "disabled"} {
351 set match [ckMenuFind $child $char]
362 # This procedure traverses to the first menubutton in the toplevel
363 # for a given window, and posts that menubutton's menu.
366 # w - Name of a window. Selects which toplevel
367 # to search for menubuttons.
370 set w [ckMenuFind [winfo toplevel $w] ""]
373 ckMenuFirstEntry [$w cget -menu]
378 # ckTraverseWithinMenu
379 # This procedure implements keyboard traversal within a menu. It
380 # searches for an entry in the menu that has "char" underlined. If
381 # such an entry is found, it is invoked and the menu is unposted.
384 # w - The name of the menu widget.
385 # char - The character to look for; case is
386 # ignored. If the string is empty then
389 proc ckTraverseWithinMenu {w char} {
393 set char [string tolower $char]
394 set last [$w index last]
395 if {$last == "none"} {
398 for {set i 0} {$i <= $last} {incr i} {
399 if [catch {set char2 [string index \
400 [$w entrycget $i -label] \
401 [$w entrycget $i -underline]]}] {
404 if {[string compare $char [string tolower $char2]] == 0} {
405 if {[$w type $i] == "cascade"} {
408 set m2 [$w entrycget $i -menu]
414 uplevel #0 [list $w invoke $i]
421 # ckMenuFirstEntry --
422 # Given a menu, this procedure finds the first entry that isn't
423 # disabled or a tear-off or separator, and activates that entry.
424 # However, if there is already an active entry in the menu (e.g.,
425 # because of a previous call to tkPostOverPoint) then the active
426 # entry isn't changed. This procedure also sets the input focus
430 # menu - Name of the menu window (possibly empty).
432 proc ckMenuFirstEntry menu {
437 if {[$menu index active] != "none"} {
440 set last [$menu index last]
441 if {$last == "none"} {
444 for {set i 0} {$i <= $last} {incr i} {
445 if {([catch {set state [$menu entrycget $i -state]}] == 0)
446 && ($state != "disabled")} {
454 # Given a menu and a text string, return the index of the menu entry
455 # that displays the string as its label. If there is no such entry,
456 # return an empty string. This procedure is tricky because some names
457 # like "active" have a special meaning in menu commands, so we can't
458 # always use the "index" widget command.
461 # menu - Name of the menu widget.
462 # s - String to look for.
464 proc ckMenuFindName {menu s} {
466 if {![regexp {^active$|^last$|^none$|^[0-9]|^@} $s]} {
467 catch {set i [$menu index $s]}
470 set last [$menu index last]
471 if {$last == "none"} {
474 for {set i 0} {$i <= $last} {incr i} {
475 if ![catch {$menu entrycget $i -label} label] {
485 # This procedure posts a given menu such that a given entry in the
486 # menu is centered over a given point in the root window. It also
487 # activates the given entry.
490 # menu - Menu to post.
491 # x, y - Root coordinates of point.
492 # entry - Index of entry within menu to center over (x,y).
493 # If omitted or specified as {}, then the menu's
494 # upper-left corner goes at (x,y).
496 proc ckPostOverPoint {menu x y {entry {}}} {
498 if {$entry == [$menu index last]} {
499 incr y [expr -([$menu yposition $entry] \
500 + [winfo reqheight $menu])/2]
502 incr y [expr -([$menu yposition $entry] \
503 + [$menu yposition [expr $entry+1]])/2]
505 incr x [expr -[winfo reqwidth $menu]/2]
508 if {($entry != {}) && ([$menu entrycget $entry -state] != "disabled")} {
509 $menu activate $entry
514 # This procedure pops up a menu and sets things up for traversing
515 # the menu and its submenus.
518 # menu - Name of the menu to be popped up.
519 # x, y - Root coordinates at which to pop up the
521 # entry - Index of a menu entry to center over (x,y).
522 # If omitted or specified as {}, then menu's
523 # upper-left corner goes at (x,y).
525 proc ck_popup {menu x y {entry {}}} {
527 if {($ckPriv(popup) != "") || ($ckPriv(postedMb) != "")} {
530 ckPostOverPoint $menu $x $y $entry
531 set ckPriv(focus) [focus]
532 set ckPriv(popup) $menu