1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkbindings.h"
30 #include "gtkkeyhash.h"
33 #include "gtkmarshalers.h"
35 #include "gtkmenubar.h"
36 #include "gtkmenuitem.h"
37 #include "gtkmenushell.h"
38 #include "gtkmenuprivate.h"
39 #include "gtkmnemonichash.h"
40 #include "gtktearoffmenuitem.h"
41 #include "gtkwindow.h"
42 #include "gtkprivate.h"
45 #define MENU_SHELL_TIMEOUT 500
47 #define PACK_DIRECTION(m) \
48 (GTK_IS_MENU_BAR (m) \
49 ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
50 : GTK_PACK_DIRECTION_LTR)
70 * A menu item can be "selected", this means that it is displayed
71 * in the prelight state, and if it has a submenu, that submenu
74 * A menu is "active" when it is visible onscreen and the user
75 * is selecting from it. A menubar is not active until the user
76 * clicks on one of its menuitems. When a menu is active,
77 * passing the mouse over a submenu will pop it up.
79 * menu_shell->active_menu_item, is however, not an "active"
80 * menu item (there is no such thing) but rather, the selected
81 * menu item in that MenuShell, if there is one.
83 * There is also is a concept of the current menu and a current
84 * menu item. The current menu item is the selected menu item
85 * that is furthest down in the hierarchy. (Every active menu_shell
86 * does not necessarily contain a selected menu item, but if
87 * it does, then menu_shell->parent_menu_shell must also contain
88 * a selected menu item. The current menu is the menu that
89 * contains the current menu_item. It will always have a GTK
90 * grab and receive all key presses.
95 * ::move_current (GtkMenuDirection *dir)
96 * Moves the current menu item in direction 'dir':
98 * GTK_MENU_DIR_PARENT: To the parent menu shell
99 * GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
101 * GTK_MENU_DIR_NEXT/PREV: To the next or previous item
104 * As a a bit of a hack to get movement between menus and
105 * menubars working, if submenu_placement is different for
106 * the menu and its MenuShell then the following apply:
108 * - For 'parent' the current menu is not just moved to
109 * the parent, but moved to the previous entry in the parent
110 * - For 'child', if there is no child, then current is
111 * moved to the next item in the parent.
113 * Note that the above explanation of ::move_current was written
114 * before menus and menubars had support for RTL flipping and
115 * different packing directions, and therefore only applies for
116 * when text direction and packing direction are both left-to-right.
118 * ::activate_current (GBoolean *force_hide)
119 * Activate the current item. If 'force_hide' is true, hide
120 * the current menu item always. Otherwise, only hide
121 * it if menu_item->klass->hide_on_activate is true.
124 * Cancels the current selection
127 #define GTK_MENU_SHELL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MENU_SHELL, GtkMenuShellPrivate))
129 typedef struct _GtkMenuShellPrivate GtkMenuShellPrivate;
131 struct _GtkMenuShellPrivate
133 GtkMnemonicHash *mnemonic_hash;
134 GtkKeyHash *key_hash;
136 GdkDevice *grab_pointer;
138 guint take_focus : 1;
139 guint activated_submenu : 1;
140 /* This flag is a crutch to keep mnemonics in the same menu
141 * if the user moves the mouse over an unselectable menuitem.
143 guint in_unselectable_item : 1;
146 static void gtk_menu_shell_set_property (GObject *object,
150 static void gtk_menu_shell_get_property (GObject *object,
154 static void gtk_menu_shell_realize (GtkWidget *widget);
155 static void gtk_menu_shell_finalize (GObject *object);
156 static void gtk_menu_shell_dispose (GObject *object);
157 static gint gtk_menu_shell_button_press (GtkWidget *widget,
158 GdkEventButton *event);
159 static gint gtk_menu_shell_button_release (GtkWidget *widget,
160 GdkEventButton *event);
161 static gint gtk_menu_shell_key_press (GtkWidget *widget,
163 static gint gtk_menu_shell_enter_notify (GtkWidget *widget,
164 GdkEventCrossing *event);
165 static gint gtk_menu_shell_leave_notify (GtkWidget *widget,
166 GdkEventCrossing *event);
167 static void gtk_menu_shell_screen_changed (GtkWidget *widget,
168 GdkScreen *previous_screen);
169 static gboolean gtk_menu_shell_grab_broken (GtkWidget *widget,
170 GdkEventGrabBroken *event);
171 static void gtk_menu_shell_add (GtkContainer *container,
173 static void gtk_menu_shell_remove (GtkContainer *container,
175 static void gtk_menu_shell_forall (GtkContainer *container,
176 gboolean include_internals,
177 GtkCallback callback,
178 gpointer callback_data);
179 static void gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
182 static void gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell);
183 static gint gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
185 static GtkWidget *gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
187 static GType gtk_menu_shell_child_type (GtkContainer *container);
188 static void gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
189 GtkWidget *menu_item);
190 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell);
192 static void gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
193 GtkMenuDirectionType direction);
194 static void gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
195 gboolean force_hide);
196 static void gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell);
197 static void gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
198 GtkDirectionType dir);
200 static void gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell);
201 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
203 static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
206 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
208 G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
211 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
213 GObjectClass *object_class;
214 GtkWidgetClass *widget_class;
215 GtkContainerClass *container_class;
217 GtkBindingSet *binding_set;
219 object_class = (GObjectClass*) klass;
220 widget_class = (GtkWidgetClass*) klass;
221 container_class = (GtkContainerClass*) klass;
223 object_class->set_property = gtk_menu_shell_set_property;
224 object_class->get_property = gtk_menu_shell_get_property;
225 object_class->finalize = gtk_menu_shell_finalize;
226 object_class->dispose = gtk_menu_shell_dispose;
228 widget_class->realize = gtk_menu_shell_realize;
229 widget_class->button_press_event = gtk_menu_shell_button_press;
230 widget_class->button_release_event = gtk_menu_shell_button_release;
231 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
232 widget_class->key_press_event = gtk_menu_shell_key_press;
233 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
234 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
235 widget_class->screen_changed = gtk_menu_shell_screen_changed;
237 container_class->add = gtk_menu_shell_add;
238 container_class->remove = gtk_menu_shell_remove;
239 container_class->forall = gtk_menu_shell_forall;
240 container_class->child_type = gtk_menu_shell_child_type;
242 klass->submenu_placement = GTK_TOP_BOTTOM;
243 klass->deactivate = gtk_real_menu_shell_deactivate;
244 klass->selection_done = NULL;
245 klass->move_current = gtk_real_menu_shell_move_current;
246 klass->activate_current = gtk_real_menu_shell_activate_current;
247 klass->cancel = gtk_real_menu_shell_cancel;
248 klass->select_item = gtk_menu_shell_real_select_item;
249 klass->insert = gtk_menu_shell_real_insert;
250 klass->move_selected = gtk_menu_shell_real_move_selected;
252 menu_shell_signals[DEACTIVATE] =
253 g_signal_new (I_("deactivate"),
254 G_OBJECT_CLASS_TYPE (object_class),
256 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
258 _gtk_marshal_VOID__VOID,
261 menu_shell_signals[SELECTION_DONE] =
262 g_signal_new (I_("selection-done"),
263 G_OBJECT_CLASS_TYPE (object_class),
265 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
267 _gtk_marshal_VOID__VOID,
270 menu_shell_signals[MOVE_CURRENT] =
271 g_signal_new (I_("move-current"),
272 G_OBJECT_CLASS_TYPE (object_class),
273 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
274 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
276 _gtk_marshal_VOID__ENUM,
278 GTK_TYPE_MENU_DIRECTION_TYPE);
280 menu_shell_signals[ACTIVATE_CURRENT] =
281 g_signal_new (I_("activate-current"),
282 G_OBJECT_CLASS_TYPE (object_class),
283 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
284 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
286 _gtk_marshal_VOID__BOOLEAN,
290 menu_shell_signals[CANCEL] =
291 g_signal_new (I_("cancel"),
292 G_OBJECT_CLASS_TYPE (object_class),
293 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
294 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
296 _gtk_marshal_VOID__VOID,
299 menu_shell_signals[CYCLE_FOCUS] =
300 g_signal_new_class_handler (I_("cycle-focus"),
301 G_OBJECT_CLASS_TYPE (object_class),
302 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
303 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
305 _gtk_marshal_VOID__ENUM,
307 GTK_TYPE_DIRECTION_TYPE);
310 * GtkMenuShell::move-selected:
311 * @menu_shell: the object on which the signal is emitted
312 * @distance: +1 to move to the next item, -1 to move to the previous
314 * The ::move-selected signal is emitted to move the selection to
317 * Returns: %TRUE to stop the signal emission, %FALSE to continue
321 menu_shell_signals[MOVE_SELECTED] =
322 g_signal_new (I_("move-selected"),
323 G_OBJECT_CLASS_TYPE (object_class),
325 G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
326 _gtk_boolean_handled_accumulator, NULL,
327 _gtk_marshal_BOOLEAN__INT,
331 binding_set = gtk_binding_set_by_class (klass);
332 gtk_binding_entry_add_signal (binding_set,
335 gtk_binding_entry_add_signal (binding_set,
337 "activate-current", 1,
340 gtk_binding_entry_add_signal (binding_set,
341 GDK_KEY_ISO_Enter, 0,
342 "activate-current", 1,
345 gtk_binding_entry_add_signal (binding_set,
347 "activate-current", 1,
350 gtk_binding_entry_add_signal (binding_set,
352 "activate-current", 1,
355 gtk_binding_entry_add_signal (binding_set,
357 "activate-current", 1,
360 gtk_binding_entry_add_signal (binding_set,
363 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
364 gtk_binding_entry_add_signal (binding_set,
365 GDK_KEY_F10, GDK_SHIFT_MASK,
367 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
370 * GtkMenuShell:take-focus:
372 * A boolean that determines whether the menu and its submenus grab the
373 * keyboard focus. See gtk_menu_shell_set_take_focus() and
374 * gtk_menu_shell_get_take_focus().
378 g_object_class_install_property (object_class,
380 g_param_spec_boolean ("take-focus",
382 P_("A boolean that determines whether the menu grabs the keyboard focus"),
384 GTK_PARAM_READWRITE));
386 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
390 gtk_menu_shell_child_type (GtkContainer *container)
392 return GTK_TYPE_MENU_ITEM;
396 gtk_menu_shell_init (GtkMenuShell *menu_shell)
398 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
400 menu_shell->children = NULL;
401 menu_shell->active_menu_item = NULL;
402 menu_shell->parent_menu_shell = NULL;
403 menu_shell->active = FALSE;
404 menu_shell->have_grab = FALSE;
405 menu_shell->have_xgrab = FALSE;
406 menu_shell->button = 0;
407 menu_shell->activate_time = 0;
409 priv->mnemonic_hash = NULL;
410 priv->key_hash = NULL;
411 priv->take_focus = TRUE;
412 priv->activated_submenu = FALSE;
416 gtk_menu_shell_set_property (GObject *object,
421 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
425 case PROP_TAKE_FOCUS:
426 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
429 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 gtk_menu_shell_get_property (GObject *object,
440 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
444 case PROP_TAKE_FOCUS:
445 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
454 gtk_menu_shell_finalize (GObject *object)
456 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
457 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
459 if (priv->mnemonic_hash)
460 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
462 _gtk_key_hash_free (priv->key_hash);
464 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
469 gtk_menu_shell_dispose (GObject *object)
471 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
473 gtk_menu_shell_deactivate (menu_shell);
475 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->dispose (object);
479 gtk_menu_shell_append (GtkMenuShell *menu_shell,
482 gtk_menu_shell_insert (menu_shell, child, -1);
486 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
489 gtk_menu_shell_insert (menu_shell, child, 0);
493 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
497 GtkMenuShellClass *class;
499 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
500 g_return_if_fail (GTK_IS_MENU_ITEM (child));
502 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
505 class->insert (menu_shell, child, position);
509 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
513 menu_shell->children = g_list_insert (menu_shell->children, child, position);
515 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
519 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
521 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
523 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
527 gtk_menu_shell_realize (GtkWidget *widget)
529 GtkAllocation allocation;
531 GdkWindowAttr attributes;
532 gint attributes_mask;
534 gtk_widget_set_realized (widget, TRUE);
536 gtk_widget_get_allocation (widget, &allocation);
538 attributes.x = allocation.x;
539 attributes.y = allocation.y;
540 attributes.width = allocation.width;
541 attributes.height = allocation.height;
542 attributes.window_type = GDK_WINDOW_CHILD;
543 attributes.wclass = GDK_INPUT_OUTPUT;
544 attributes.visual = gtk_widget_get_visual (widget);
545 attributes.event_mask = gtk_widget_get_events (widget);
546 attributes.event_mask |= (GDK_EXPOSURE_MASK |
547 GDK_BUTTON_PRESS_MASK |
548 GDK_BUTTON_RELEASE_MASK |
550 GDK_ENTER_NOTIFY_MASK |
551 GDK_LEAVE_NOTIFY_MASK);
553 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
555 window = gdk_window_new (gtk_widget_get_parent_window (widget),
556 &attributes, attributes_mask);
557 gtk_widget_set_window (widget, window);
558 gdk_window_set_user_data (window, widget);
560 gtk_widget_style_attach (widget);
561 gtk_style_set_background (gtk_widget_get_style (widget), window, GTK_STATE_NORMAL);
565 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
567 if (!menu_shell->active)
571 device = gtk_get_current_event_device ();
573 _gtk_menu_shell_set_grab_device (menu_shell, device);
574 gtk_device_grab_add (GTK_WIDGET (menu_shell), device, TRUE);
576 menu_shell->have_grab = TRUE;
577 menu_shell->active = TRUE;
582 gtk_menu_shell_button_press (GtkWidget *widget,
583 GdkEventButton *event)
585 GtkMenuShell *menu_shell;
586 GtkWidget *menu_item;
589 if (event->type != GDK_BUTTON_PRESS)
592 menu_shell = GTK_MENU_SHELL (widget);
594 if (menu_shell->parent_menu_shell)
595 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
597 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
599 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
601 parent = gtk_widget_get_parent (menu_item);
603 if (menu_item != GTK_MENU_SHELL (parent)->active_menu_item)
605 /* select the menu item *before* activating the shell, so submenus
606 * which might be open are closed the friendly way. If we activate
607 * (and thus grab) this menu shell first, we might get grab_broken
608 * events which will close the entire menu hierarchy. Selecting the
609 * menu item also fixes up the state as if enter_notify() would
610 * have run before (which normally selects the item).
612 if (GTK_MENU_SHELL_GET_CLASS (parent)->submenu_placement != GTK_TOP_BOTTOM)
613 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), menu_item);
617 if (!menu_shell->active || !menu_shell->button)
619 gboolean initially_active = menu_shell->active;
621 menu_shell->button = event->button;
625 if (_gtk_menu_item_is_selectable (menu_item) &&
626 gtk_widget_get_parent (menu_item) == widget &&
627 menu_item != menu_shell->active_menu_item)
629 _gtk_menu_shell_activate (menu_shell);
630 menu_shell->button = event->button;
632 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
634 menu_shell->activate_time = event->time;
635 gtk_menu_shell_select_item (menu_shell, menu_item);
641 if (!initially_active)
643 gboolean window_drag = FALSE;
645 gtk_widget_style_get (widget,
646 "window-dragging", &window_drag,
651 gtk_menu_shell_deactivate (menu_shell);
652 gtk_window_begin_move_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
663 widget = gtk_get_event_widget ((GdkEvent*) event);
664 if (widget == GTK_WIDGET (menu_shell))
666 gtk_menu_shell_deactivate (menu_shell);
667 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
671 if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
672 GTK_MENU_ITEM (menu_item)->submenu != NULL &&
673 !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
675 GtkMenuShellPrivate *priv;
677 _gtk_menu_item_popup_submenu (menu_item, FALSE);
679 priv = GTK_MENU_SHELL_GET_PRIVATE (gtk_widget_get_parent (menu_item));
680 priv->activated_submenu = TRUE;
687 gtk_menu_shell_grab_broken (GtkWidget *widget,
688 GdkEventGrabBroken *event)
690 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
692 if (menu_shell->have_xgrab && event->grab_window == NULL)
694 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
697 gtk_menu_shell_deselect (menu_shell);
699 gtk_menu_shell_deactivate (menu_shell);
700 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
707 gtk_menu_shell_button_release (GtkWidget *widget,
708 GdkEventButton *event)
710 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
711 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (widget);
713 if (menu_shell->active)
715 GtkWidget *menu_item;
716 gboolean deactivate = TRUE;
718 if (menu_shell->button && (event->button != menu_shell->button))
720 menu_shell->button = 0;
721 if (menu_shell->parent_menu_shell)
722 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
725 menu_shell->button = 0;
726 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
728 if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
730 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
731 _gtk_menu_item_is_selectable (menu_item))
733 GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->submenu;
737 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
741 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
742 priv->activated_submenu)
745 GTimeVal *popup_time;
746 gint64 usec_since_popup = 0;
748 g_object_get (gtk_widget_get_settings (widget),
749 "gtk-menu-popdown-delay", &popdown_delay,
752 popup_time = g_object_get_data (G_OBJECT (submenu),
753 "gtk-menu-exact-popup-time");
757 GTimeVal current_time;
759 g_get_current_time (¤t_time);
761 usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
762 (gint64) current_time.tv_usec -
763 (gint64) popup_time->tv_sec * 1000 * 1000 -
764 (gint64) popup_time->tv_usec);
766 g_object_set_data (G_OBJECT (submenu),
767 "gtk-menu-exact-popup-time", NULL);
770 /* only close the submenu on click if we opened the
771 * menu explicitely (usec_since_popup == 0) or
772 * enough time has passed since it was opened by
773 * GtkMenuItem's timeout (usec_since_popup > delay).
775 if (!priv->activated_submenu &&
776 (usec_since_popup == 0 ||
777 usec_since_popup > popdown_delay * 1000))
779 _gtk_menu_item_popdown_submenu (menu_item);
783 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
789 else if (menu_item &&
790 !_gtk_menu_item_is_selectable (menu_item) &&
791 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
795 else if (menu_shell->parent_menu_shell)
797 menu_shell->active = TRUE;
798 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
802 /* If we ended up on an item with a submenu, leave the menu up.
804 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
805 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
810 else /* a very fast press-release */
812 /* We only ever want to prevent deactivation on the first
813 * press/release. Setting the time to zero is a bit of a
814 * hack, since we could be being triggered in the first
815 * few fractions of a second after a server time wraparound.
816 * the chances of that happening are ~1/10^6, without
817 * serious harm if we lose.
819 menu_shell->activate_time = 0;
825 gtk_menu_shell_deactivate (menu_shell);
826 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
829 priv->activated_submenu = FALSE;
836 _gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
837 gboolean keyboard_mode)
839 menu_shell->keyboard_mode = keyboard_mode;
843 _gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
845 return menu_shell->keyboard_mode;
849 _gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
851 GtkMenuShell *target;
852 gboolean auto_mnemonics;
854 gboolean mnemonics_visible;
856 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
857 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
866 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (target);
867 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));
869 /* The idea with keyboard mode is that once you start using
870 * the keyboard to navigate the menus, we show mnemonics
871 * until the menu navigation is over. To that end, we spread
872 * the keyboard mode upwards in the menu hierarchy here.
873 * Also see gtk_menu_popup, where we inherit it downwards.
875 if (menu_shell->keyboard_mode)
876 target->keyboard_mode = TRUE;
878 /* While navigating menus, the first parent menu with an active
879 * item is the one where mnemonics are effective, as can be seen
880 * in gtk_menu_shell_key_press below.
881 * We also show mnemonics in context menus. The grab condition is
882 * necessary to ensure we remove underlines from menu bars when
885 mnemonics_visible = target->keyboard_mode &&
886 (((target->active_menu_item || priv->in_unselectable_item) && !found) ||
887 (target == menu_shell &&
888 !target->parent_menu_shell &&
889 gtk_widget_has_grab (GTK_WIDGET (target))));
891 /* While menus are up, only show underlines inside the menubar,
892 * not in the entire window.
894 if (GTK_IS_MENU_BAR (target))
896 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
897 _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
901 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
903 if (target->active_menu_item || priv->in_unselectable_item)
906 target = GTK_MENU_SHELL (target->parent_menu_shell);
911 gtk_menu_shell_key_press (GtkWidget *widget,
914 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
915 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
916 gboolean enable_mnemonics;
918 menu_shell->keyboard_mode = TRUE;
920 if (!(menu_shell->active_menu_item || priv->in_unselectable_item) && menu_shell->parent_menu_shell)
921 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
923 if (gtk_bindings_activate_event (G_OBJECT (widget), event))
926 g_object_get (gtk_widget_get_settings (widget),
927 "gtk-enable-mnemonics", &enable_mnemonics,
930 if (enable_mnemonics)
931 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
937 gtk_menu_shell_enter_notify (GtkWidget *widget,
938 GdkEventCrossing *event)
940 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
942 if (event->mode == GDK_CROSSING_GTK_GRAB ||
943 event->mode == GDK_CROSSING_GTK_UNGRAB ||
944 event->mode == GDK_CROSSING_STATE_CHANGED)
947 if (menu_shell->active)
949 GtkWidget *menu_item;
952 menu_item = gtk_get_event_widget ((GdkEvent*) event);
957 if (GTK_IS_MENU_ITEM (menu_item) &&
958 !_gtk_menu_item_is_selectable (menu_item))
960 GtkMenuShellPrivate *priv;
962 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
963 priv->in_unselectable_item = TRUE;
968 parent = gtk_widget_get_parent (menu_item);
969 if (parent == widget &&
970 GTK_IS_MENU_ITEM (menu_item))
972 if (menu_shell->ignore_enter)
975 if (event->detail != GDK_NOTIFY_INFERIOR)
977 if (gtk_widget_get_state (menu_item) != GTK_STATE_PRELIGHT)
978 gtk_menu_shell_select_item (menu_shell, menu_item);
980 /* If any mouse button is down, and there is a submenu
981 * that is not yet visible, activate it. It's sufficient
982 * to check for any button's mask (not only the one
983 * matching menu_shell->button), because there is no
984 * situation a mouse button could be pressed while
985 * entering a menu item where we wouldn't want to show
988 if ((event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) &&
989 GTK_MENU_ITEM (menu_item)->submenu != NULL)
991 GtkMenuShellPrivate *priv;
993 priv = GTK_MENU_SHELL_GET_PRIVATE (parent);
994 priv->activated_submenu = TRUE;
996 if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
998 gboolean touchscreen_mode;
1000 g_object_get (gtk_widget_get_settings (widget),
1001 "gtk-touchscreen-mode", &touchscreen_mode,
1004 if (touchscreen_mode)
1005 _gtk_menu_item_popup_submenu (menu_item, TRUE);
1010 else if (menu_shell->parent_menu_shell)
1012 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
1020 gtk_menu_shell_leave_notify (GtkWidget *widget,
1021 GdkEventCrossing *event)
1023 if (event->mode == GDK_CROSSING_GTK_GRAB ||
1024 event->mode == GDK_CROSSING_GTK_GRAB ||
1025 event->mode == GDK_CROSSING_STATE_CHANGED)
1028 if (gtk_widget_get_visible (widget))
1030 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1031 GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
1032 GtkMenuItem *menu_item;
1034 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1037 menu_item = GTK_MENU_ITEM (event_widget);
1039 if (!_gtk_menu_item_is_selectable (event_widget))
1041 GtkMenuShellPrivate *priv;
1043 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1044 priv->in_unselectable_item = TRUE;
1049 if ((menu_shell->active_menu_item == event_widget) &&
1050 (menu_item->submenu == NULL))
1052 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1053 (gtk_widget_get_state (GTK_WIDGET (menu_item)) != GTK_STATE_NORMAL))
1055 gtk_menu_shell_deselect (menu_shell);
1058 else if (menu_shell->parent_menu_shell)
1060 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
1068 gtk_menu_shell_screen_changed (GtkWidget *widget,
1069 GdkScreen *previous_screen)
1071 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
1075 gtk_menu_shell_add (GtkContainer *container,
1078 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
1082 gtk_menu_shell_remove (GtkContainer *container,
1085 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1088 was_visible = gtk_widget_get_visible (widget);
1089 menu_shell->children = g_list_remove (menu_shell->children, widget);
1091 if (widget == menu_shell->active_menu_item)
1093 g_signal_emit_by_name (menu_shell->active_menu_item, "deselect");
1094 menu_shell->active_menu_item = NULL;
1097 gtk_widget_unparent (widget);
1099 /* queue resize regardless of gtk_widget_get_visible (container),
1100 * since that's what is needed by toplevels.
1103 gtk_widget_queue_resize (GTK_WIDGET (container));
1107 gtk_menu_shell_forall (GtkContainer *container,
1108 gboolean include_internals,
1109 GtkCallback callback,
1110 gpointer callback_data)
1112 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1116 children = menu_shell->children;
1119 child = children->data;
1120 children = children->next;
1122 (* callback) (child, callback_data);
1128 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
1130 if (menu_shell->active)
1132 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1134 menu_shell->button = 0;
1135 menu_shell->active = FALSE;
1136 menu_shell->activate_time = 0;
1138 if (menu_shell->active_menu_item)
1140 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1141 menu_shell->active_menu_item = NULL;
1144 if (menu_shell->have_grab)
1146 menu_shell->have_grab = FALSE;
1147 gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1149 if (menu_shell->have_xgrab)
1151 GdkDevice *keyboard;
1153 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1154 keyboard = gdk_device_get_associated_device (priv->grab_pointer);
1157 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1159 menu_shell->have_xgrab = FALSE;
1162 menu_shell->keyboard_mode = FALSE;
1163 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1165 _gtk_menu_shell_update_mnemonics (menu_shell);
1170 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1175 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1176 g_return_val_if_fail (child != NULL, FALSE);
1178 parent = gtk_widget_get_parent (child);
1179 while (GTK_IS_MENU_SHELL (parent))
1181 if (parent == (GtkWidget*) menu_shell)
1183 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1190 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1193 GtkWidget *menu_item;
1195 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1197 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1198 menu_item = gtk_widget_get_parent (menu_item);
1200 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1206 /* Handlers for action signals */
1209 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1210 GtkWidget *menu_item)
1212 GtkMenuShellClass *class;
1214 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1215 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1217 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1219 if (class->select_item &&
1220 !(menu_shell->active &&
1221 menu_shell->active_menu_item == menu_item))
1222 class->select_item (menu_shell, menu_item);
1225 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1226 GtkSubmenuPlacement placement);
1229 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1230 GtkWidget *menu_item)
1232 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1234 if (menu_shell->active_menu_item)
1236 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1237 menu_shell->active_menu_item = NULL;
1240 if (!_gtk_menu_item_is_selectable (menu_item))
1242 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1244 priv->in_unselectable_item = TRUE;
1245 _gtk_menu_shell_update_mnemonics (menu_shell);
1250 menu_shell->active_menu_item = menu_item;
1251 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1252 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1255 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1256 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1257 gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1259 _gtk_menu_shell_update_mnemonics (menu_shell);
1261 /* This allows the bizarre radio buttons-with-submenus-display-history
1264 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1265 gtk_widget_activate (menu_shell->active_menu_item);
1269 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1271 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1273 if (menu_shell->active_menu_item)
1275 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1276 menu_shell->active_menu_item = NULL;
1277 _gtk_menu_shell_update_mnemonics (menu_shell);
1282 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
1283 GtkWidget *menu_item,
1284 gboolean force_deactivate)
1286 GSList *slist, *shells = NULL;
1287 gboolean deactivate = force_deactivate;
1289 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1290 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1293 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1295 g_object_ref (menu_shell);
1296 g_object_ref (menu_item);
1300 GtkMenuShell *parent_menu_shell = menu_shell;
1304 g_object_ref (parent_menu_shell);
1305 shells = g_slist_prepend (shells, parent_menu_shell);
1306 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1308 while (parent_menu_shell);
1309 shells = g_slist_reverse (shells);
1311 gtk_menu_shell_deactivate (menu_shell);
1313 /* flush the x-queue, so any grabs are removed and
1314 * the menu is actually taken down
1316 gdk_display_sync (gtk_widget_get_display (menu_item));
1319 gtk_widget_activate (menu_item);
1321 for (slist = shells; slist; slist = slist->next)
1323 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1324 g_object_unref (slist->data);
1326 g_slist_free (shells);
1328 g_object_unref (menu_shell);
1329 g_object_unref (menu_item);
1332 /* Distance should be +/- 1 */
1334 gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
1337 if (menu_shell->active_menu_item)
1339 GList *node = g_list_find (menu_shell->children,
1340 menu_shell->active_menu_item);
1341 GList *start_node = node;
1342 gboolean wrap_around;
1344 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1345 "gtk-keynav-wrap-around", &wrap_around,
1351 while (node != start_node &&
1352 (!node || !_gtk_menu_item_is_selectable (node->data)))
1356 else if (wrap_around)
1357 node = menu_shell->children;
1360 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1368 while (node != start_node &&
1369 (!node || !_gtk_menu_item_is_selectable (node->data)))
1373 else if (wrap_around)
1374 node = g_list_last (menu_shell->children);
1377 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1384 gtk_menu_shell_select_item (menu_shell, node->data);
1390 /* Distance should be +/- 1 */
1392 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1395 gboolean handled = FALSE;
1397 g_signal_emit (menu_shell, menu_shell_signals[MOVE_SELECTED], 0,
1398 distance, &handled);
1402 * gtk_menu_shell_select_first:
1403 * @menu_shell: a #GtkMenuShell
1404 * @search_sensitive: if %TRUE, search for the first selectable
1405 * menu item, otherwise select nothing if
1406 * the first item isn't sensitive. This
1407 * should be %FALSE if the menu is being
1408 * popped up initially.
1410 * Select the first visible or selectable child of the menu shell;
1411 * don't select tearoff items unless the only item is a tearoff
1417 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1418 gboolean search_sensitive)
1420 GtkWidget *to_select = NULL;
1423 tmp_list = menu_shell->children;
1426 GtkWidget *child = tmp_list->data;
1428 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1429 _gtk_menu_item_is_selectable (child))
1432 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1436 tmp_list = tmp_list->next;
1440 gtk_menu_shell_select_item (menu_shell, to_select);
1444 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1445 gboolean search_sensitive)
1447 GtkWidget *to_select = NULL;
1450 tmp_list = g_list_last (menu_shell->children);
1453 GtkWidget *child = tmp_list->data;
1455 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1456 _gtk_menu_item_is_selectable (child))
1459 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1463 tmp_list = tmp_list->prev;
1467 gtk_menu_shell_select_item (menu_shell, to_select);
1471 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1473 GtkMenuItem *menu_item;
1475 if (menu_shell->active_menu_item == NULL)
1478 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1480 if (menu_item->submenu)
1482 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1483 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1484 if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1492 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1493 GtkMenuDirectionType direction)
1495 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1496 GtkMenuShell *parent_menu_shell = NULL;
1497 gboolean had_selection;
1498 gboolean touchscreen_mode;
1500 priv->in_unselectable_item = FALSE;
1502 had_selection = menu_shell->active_menu_item != NULL;
1504 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1505 "gtk-touchscreen-mode", &touchscreen_mode,
1508 if (menu_shell->parent_menu_shell)
1509 parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1513 case GTK_MENU_DIR_PARENT:
1514 if (touchscreen_mode &&
1515 menu_shell->active_menu_item &&
1516 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
1517 gtk_widget_get_visible (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu))
1519 /* if we are on a menu item that has an open submenu but the
1520 * focus is not in that submenu (e.g. because it's empty or
1521 * has only insensitive items), close that submenu instead
1522 * of running into the code below which would close *this*
1525 _gtk_menu_item_popdown_submenu (menu_shell->active_menu_item);
1526 _gtk_menu_shell_update_mnemonics (menu_shell);
1528 else if (parent_menu_shell)
1530 if (touchscreen_mode)
1532 /* close menu when returning from submenu. */
1533 _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->parent_menu_item);
1534 _gtk_menu_shell_update_mnemonics (parent_menu_shell);
1538 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1539 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1540 gtk_menu_shell_deselect (menu_shell);
1543 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1544 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1546 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1547 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1550 /* If there is no parent and the submenu is in the opposite direction
1551 * to the menu, then make the PARENT direction wrap around to
1552 * the bottom of the submenu.
1554 else if (menu_shell->active_menu_item &&
1555 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1556 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1558 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1560 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1561 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1562 _gtk_menu_shell_select_last (submenu, TRUE);
1566 case GTK_MENU_DIR_CHILD:
1567 if (menu_shell->active_menu_item &&
1568 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1569 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1571 if (gtk_menu_shell_select_submenu_first (menu_shell))
1575 /* Try to find a menu running the opposite direction */
1576 while (parent_menu_shell &&
1577 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1578 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1580 parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->parent_menu_shell);
1583 if (parent_menu_shell)
1585 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1586 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1588 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1590 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1594 case GTK_MENU_DIR_PREV:
1595 gtk_menu_shell_move_selected (menu_shell, -1);
1596 if (!had_selection &&
1597 !menu_shell->active_menu_item &&
1598 menu_shell->children)
1599 _gtk_menu_shell_select_last (menu_shell, TRUE);
1602 case GTK_MENU_DIR_NEXT:
1603 gtk_menu_shell_move_selected (menu_shell, 1);
1604 if (!had_selection &&
1605 !menu_shell->active_menu_item &&
1606 menu_shell->children)
1607 gtk_menu_shell_select_first (menu_shell, TRUE);
1613 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1614 gboolean force_hide)
1616 if (menu_shell->active_menu_item &&
1617 _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1619 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1620 gtk_menu_shell_activate_item (menu_shell,
1621 menu_shell->active_menu_item,
1624 _gtk_menu_item_popup_submenu (menu_shell->active_menu_item, FALSE);
1629 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1631 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1633 gtk_menu_shell_deselect (menu_shell);
1635 gtk_menu_shell_deactivate (menu_shell);
1636 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1640 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1641 GtkDirectionType dir)
1643 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1645 if (menu_shell->parent_menu_shell)
1646 menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1652 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1656 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1658 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1660 if (klass->get_popup_delay)
1662 return klass->get_popup_delay (menu_shell);
1667 GtkWidget *widget = GTK_WIDGET (menu_shell);
1669 g_object_get (gtk_widget_get_settings (widget),
1670 "gtk-menu-popup-delay", &popup_delay,
1678 * gtk_menu_shell_cancel:
1679 * @menu_shell: a #GtkMenuShell
1681 * Cancels the selection within the menu shell.
1686 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1688 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1690 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1693 static GtkMnemonicHash *
1694 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1697 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1699 if (!private->mnemonic_hash && create)
1700 private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1702 return private->mnemonic_hash;
1706 menu_shell_add_mnemonic_foreach (guint keyval,
1710 GtkKeyHash *key_hash = data;
1712 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1716 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1719 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1720 GtkWidget *widget = GTK_WIDGET (menu_shell);
1722 if (!private->key_hash && create && gtk_widget_has_screen (widget))
1724 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1725 GdkScreen *screen = gtk_widget_get_screen (widget);
1726 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1731 private->key_hash = _gtk_key_hash_new (keymap, NULL);
1733 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1734 menu_shell_add_mnemonic_foreach,
1738 return private->key_hash;
1742 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1744 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1746 if (private->key_hash)
1748 _gtk_key_hash_free (private->key_hash);
1749 private->key_hash = NULL;
1754 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1757 GtkMnemonicHash *mnemonic_hash;
1758 GtkKeyHash *key_hash;
1760 gboolean result = FALSE;
1762 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1766 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1770 entries = _gtk_key_hash_lookup (key_hash,
1771 event->hardware_keycode,
1773 gtk_accelerator_get_default_mod_mask (),
1777 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1778 GPOINTER_TO_UINT (entries->data));
1784 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1788 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1789 g_return_if_fail (GTK_IS_WIDGET (target));
1791 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1793 gtk_menu_shell_reset_key_hash (menu_shell);
1797 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1801 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1802 g_return_if_fail (GTK_IS_WIDGET (target));
1804 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1806 gtk_menu_shell_reset_key_hash (menu_shell);
1810 _gtk_menu_shell_set_grab_device (GtkMenuShell *menu_shell,
1813 GtkMenuShellPrivate *priv;
1815 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1816 g_return_if_fail (!device || GDK_IS_DEVICE (device));
1818 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1821 priv->grab_pointer = NULL;
1822 else if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1823 priv->grab_pointer = gdk_device_get_associated_device (device);
1825 priv->grab_pointer = device;
1829 _gtk_menu_shell_get_grab_device (GtkMenuShell *menu_shell)
1831 GtkMenuShellPrivate *priv;
1833 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
1835 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1837 return priv->grab_pointer;
1841 * gtk_menu_shell_get_take_focus:
1842 * @menu_shell: a #GtkMenuShell
1844 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1846 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1851 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1853 GtkMenuShellPrivate *priv;
1855 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1857 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1859 return priv->take_focus;
1863 * gtk_menu_shell_set_take_focus:
1864 * @menu_shell: a #GtkMenuShell
1865 * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1867 * If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1868 * focus so that it will receive all keyboard events which is needed to enable
1869 * keyboard navigation in menus.
1871 * Setting @take_focus to %FALSE is useful only for special applications
1872 * like virtual keyboard implementations which should not take keyboard
1875 * The @take_focus state of a menu or menu bar is automatically propagated
1876 * to submenus whenever a submenu is popped up, so you don't have to worry
1877 * about recursively setting it for your entire menu hierarchy. Only when
1878 * programmatically picking a submenu and popping it up manually, the
1879 * @take_focus property of the submenu needs to be set explicitely.
1881 * Note that setting it to %FALSE has side-effects:
1883 * If the focus is in some other app, it keeps the focus and keynav in
1884 * the menu doesn't work. Consequently, keynav on the menu will only
1885 * work if the focus is on some toplevel owned by the onscreen keyboard.
1887 * To avoid confusing the user, menus with @take_focus set to %FALSE
1888 * should not display mnemonics or accelerators, since it cannot be
1889 * guaranteed that they will work.
1891 * See also gdk_keyboard_grab()
1896 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1897 gboolean take_focus)
1899 GtkMenuShellPrivate *priv;
1901 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1903 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1905 if (priv->take_focus != take_focus)
1907 priv->take_focus = take_focus;
1908 g_object_notify (G_OBJECT (menu_shell), "take-focus");