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/.
27 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkkeyhash.h"
35 #include "gtkmarshalers.h"
37 #include "gtkmenubar.h"
38 #include "gtkmenuitem.h"
39 #include "gtkmenushell.h"
40 #include "gtkmnemonichash.h"
41 #include "gtktearoffmenuitem.h"
42 #include "gtkwindow.h"
43 #include "gtkprivate.h"
47 #define MENU_SHELL_TIMEOUT 500
49 #define PACK_DIRECTION(m) \
50 (GTK_IS_MENU_BAR (m) \
51 ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
52 : GTK_PACK_DIRECTION_LTR)
72 * A menu item can be "selected", this means that it is displayed
73 * in the prelight state, and if it has a submenu, that submenu
76 * A menu is "active" when it is visible onscreen and the user
77 * is selecting from it. A menubar is not active until the user
78 * clicks on one of its menuitems. When a menu is active,
79 * passing the mouse over a submenu will pop it up.
81 * menu_shell->active_menu_item, is however, not an "active"
82 * menu item (there is no such thing) but rather, the selected
83 * menu item in that MenuShell, if there is one.
85 * There is also is a concept of the current menu and a current
86 * menu item. The current menu item is the selected menu item
87 * that is furthest down in the hierarchy. (Every active menu_shell
88 * does not necessarily contain a selected menu item, but if
89 * it does, then menu_shell->parent_menu_shell must also contain
90 * a selected menu item. The current menu is the menu that
91 * contains the current menu_item. It will always have a GTK
92 * grab and receive all key presses.
97 * ::move_current (GtkMenuDirection *dir)
98 * Moves the current menu item in direction 'dir':
100 * GTK_MENU_DIR_PARENT: To the parent menu shell
101 * GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
103 * GTK_MENU_DIR_NEXT/PREV: To the next or previous item
106 * As a a bit of a hack to get movement between menus and
107 * menubars working, if submenu_placement is different for
108 * the menu and its MenuShell then the following apply:
110 * - For 'parent' the current menu is not just moved to
111 * the parent, but moved to the previous entry in the parent
112 * - For 'child', if there is no child, then current is
113 * moved to the next item in the parent.
115 * Note that the above explanation of ::move_current was written
116 * before menus and menubars had support for RTL flipping and
117 * different packing directions, and therefore only applies for
118 * when text direction and packing direction are both left-to-right.
120 * ::activate_current (GBoolean *force_hide)
121 * Activate the current item. If 'force_hide' is true, hide
122 * the current menu item always. Otherwise, only hide
123 * it if menu_item->klass->hide_on_activate is true.
126 * Cancels the current selection
129 #define GTK_MENU_SHELL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MENU_SHELL, GtkMenuShellPrivate))
131 typedef struct _GtkMenuShellPrivate GtkMenuShellPrivate;
133 struct _GtkMenuShellPrivate
135 GtkMnemonicHash *mnemonic_hash;
136 GtkKeyHash *key_hash;
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 gint gtk_menu_shell_button_press (GtkWidget *widget,
157 GdkEventButton *event);
158 static gint gtk_menu_shell_button_release (GtkWidget *widget,
159 GdkEventButton *event);
160 static gint gtk_menu_shell_key_press (GtkWidget *widget,
162 static gint gtk_menu_shell_enter_notify (GtkWidget *widget,
163 GdkEventCrossing *event);
164 static gint gtk_menu_shell_leave_notify (GtkWidget *widget,
165 GdkEventCrossing *event);
166 static void gtk_menu_shell_screen_changed (GtkWidget *widget,
167 GdkScreen *previous_screen);
168 static gboolean gtk_menu_shell_grab_broken (GtkWidget *widget,
169 GdkEventGrabBroken *event);
170 static void gtk_menu_shell_add (GtkContainer *container,
172 static void gtk_menu_shell_remove (GtkContainer *container,
174 static void gtk_menu_shell_forall (GtkContainer *container,
175 gboolean include_internals,
176 GtkCallback callback,
177 gpointer callback_data);
178 static void gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
181 static void gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell);
182 static gint gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
184 static GtkWidget *gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
186 static GType gtk_menu_shell_child_type (GtkContainer *container);
187 static void gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
188 GtkWidget *menu_item);
189 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell);
191 static void gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
192 GtkMenuDirectionType direction);
193 static void gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
194 gboolean force_hide);
195 static void gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell);
196 static void gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
197 GtkDirectionType dir);
199 static void gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell);
200 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
202 static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
205 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
207 G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
210 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
212 GObjectClass *object_class;
213 GtkWidgetClass *widget_class;
214 GtkContainerClass *container_class;
216 GtkBindingSet *binding_set;
218 object_class = (GObjectClass*) klass;
219 widget_class = (GtkWidgetClass*) klass;
220 container_class = (GtkContainerClass*) klass;
222 object_class->set_property = gtk_menu_shell_set_property;
223 object_class->get_property = gtk_menu_shell_get_property;
224 object_class->finalize = gtk_menu_shell_finalize;
226 widget_class->realize = gtk_menu_shell_realize;
227 widget_class->button_press_event = gtk_menu_shell_button_press;
228 widget_class->button_release_event = gtk_menu_shell_button_release;
229 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
230 widget_class->key_press_event = gtk_menu_shell_key_press;
231 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
232 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
233 widget_class->screen_changed = gtk_menu_shell_screen_changed;
235 container_class->add = gtk_menu_shell_add;
236 container_class->remove = gtk_menu_shell_remove;
237 container_class->forall = gtk_menu_shell_forall;
238 container_class->child_type = gtk_menu_shell_child_type;
240 klass->submenu_placement = GTK_TOP_BOTTOM;
241 klass->deactivate = gtk_real_menu_shell_deactivate;
242 klass->selection_done = NULL;
243 klass->move_current = gtk_real_menu_shell_move_current;
244 klass->activate_current = gtk_real_menu_shell_activate_current;
245 klass->cancel = gtk_real_menu_shell_cancel;
246 klass->select_item = gtk_menu_shell_real_select_item;
247 klass->insert = gtk_menu_shell_real_insert;
248 klass->move_selected = gtk_menu_shell_real_move_selected;
250 menu_shell_signals[DEACTIVATE] =
251 g_signal_new (I_("deactivate"),
252 G_OBJECT_CLASS_TYPE (object_class),
254 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
256 _gtk_marshal_VOID__VOID,
259 menu_shell_signals[SELECTION_DONE] =
260 g_signal_new (I_("selection-done"),
261 G_OBJECT_CLASS_TYPE (object_class),
263 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
265 _gtk_marshal_VOID__VOID,
268 menu_shell_signals[MOVE_CURRENT] =
269 g_signal_new (I_("move-current"),
270 G_OBJECT_CLASS_TYPE (object_class),
271 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
272 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
274 _gtk_marshal_VOID__ENUM,
276 GTK_TYPE_MENU_DIRECTION_TYPE);
278 menu_shell_signals[ACTIVATE_CURRENT] =
279 g_signal_new (I_("activate-current"),
280 G_OBJECT_CLASS_TYPE (object_class),
281 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
282 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
284 _gtk_marshal_VOID__BOOLEAN,
288 menu_shell_signals[CANCEL] =
289 g_signal_new (I_("cancel"),
290 G_OBJECT_CLASS_TYPE (object_class),
291 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
292 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
294 _gtk_marshal_VOID__VOID,
297 menu_shell_signals[CYCLE_FOCUS] =
298 g_signal_new_class_handler (I_("cycle-focus"),
299 G_OBJECT_CLASS_TYPE (object_class),
300 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
301 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
303 _gtk_marshal_VOID__ENUM,
305 GTK_TYPE_DIRECTION_TYPE);
308 * GtkMenuShell::move-selected:
309 * @menu_shell: the object on which the signal is emitted
310 * @distance: +1 to move to the next item, -1 to move to the previous
312 * The ::move-selected signal is emitted to move the selection to
315 * Returns: %TRUE to stop the signal emission, %FALSE to continue
319 menu_shell_signals[MOVE_SELECTED] =
320 g_signal_new (I_("move-selected"),
321 G_OBJECT_CLASS_TYPE (object_class),
323 G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
324 _gtk_boolean_handled_accumulator, NULL,
325 _gtk_marshal_BOOLEAN__INT,
329 binding_set = gtk_binding_set_by_class (klass);
330 gtk_binding_entry_add_signal (binding_set,
333 gtk_binding_entry_add_signal (binding_set,
335 "activate-current", 1,
338 gtk_binding_entry_add_signal (binding_set,
340 "activate-current", 1,
343 gtk_binding_entry_add_signal (binding_set,
345 "activate-current", 1,
348 gtk_binding_entry_add_signal (binding_set,
350 "activate-current", 1,
353 gtk_binding_entry_add_signal (binding_set,
355 "activate-current", 1,
358 gtk_binding_entry_add_signal (binding_set,
361 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
362 gtk_binding_entry_add_signal (binding_set,
363 GDK_F10, GDK_SHIFT_MASK,
365 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
368 * GtkMenuShell:take-focus:
370 * A boolean that determines whether the menu and its submenus grab the
371 * keyboard focus. See gtk_menu_shell_set_take_focus() and
372 * gtk_menu_shell_get_take_focus().
376 g_object_class_install_property (object_class,
378 g_param_spec_boolean ("take-focus",
380 P_("A boolean that determines whether the menu grabs the keyboard focus"),
382 GTK_PARAM_READWRITE));
384 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
388 gtk_menu_shell_child_type (GtkContainer *container)
390 return GTK_TYPE_MENU_ITEM;
394 gtk_menu_shell_init (GtkMenuShell *menu_shell)
396 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
398 menu_shell->children = NULL;
399 menu_shell->active_menu_item = NULL;
400 menu_shell->parent_menu_shell = NULL;
401 menu_shell->active = FALSE;
402 menu_shell->have_grab = FALSE;
403 menu_shell->have_xgrab = FALSE;
404 menu_shell->button = 0;
405 menu_shell->activate_time = 0;
407 priv->mnemonic_hash = NULL;
408 priv->key_hash = NULL;
409 priv->take_focus = TRUE;
410 priv->activated_submenu = FALSE;
414 gtk_menu_shell_set_property (GObject *object,
419 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
423 case PROP_TAKE_FOCUS:
424 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
433 gtk_menu_shell_get_property (GObject *object,
438 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
442 case PROP_TAKE_FOCUS:
443 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 gtk_menu_shell_finalize (GObject *object)
454 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
455 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
457 if (priv->mnemonic_hash)
458 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
460 _gtk_key_hash_free (priv->key_hash);
462 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
467 gtk_menu_shell_append (GtkMenuShell *menu_shell,
470 gtk_menu_shell_insert (menu_shell, child, -1);
474 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
477 gtk_menu_shell_insert (menu_shell, child, 0);
481 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
485 GtkMenuShellClass *class;
487 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
488 g_return_if_fail (GTK_IS_MENU_ITEM (child));
490 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
493 class->insert (menu_shell, child, position);
497 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
501 menu_shell->children = g_list_insert (menu_shell->children, child, position);
503 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
507 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
509 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
511 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
515 gtk_menu_shell_realize (GtkWidget *widget)
517 GdkWindowAttr attributes;
518 gint attributes_mask;
520 gtk_widget_set_realized (widget, TRUE);
522 attributes.x = widget->allocation.x;
523 attributes.y = widget->allocation.y;
524 attributes.width = widget->allocation.width;
525 attributes.height = widget->allocation.height;
526 attributes.window_type = GDK_WINDOW_CHILD;
527 attributes.wclass = GDK_INPUT_OUTPUT;
528 attributes.visual = gtk_widget_get_visual (widget);
529 attributes.colormap = gtk_widget_get_colormap (widget);
530 attributes.event_mask = gtk_widget_get_events (widget);
531 attributes.event_mask |= (GDK_EXPOSURE_MASK |
532 GDK_BUTTON_PRESS_MASK |
533 GDK_BUTTON_RELEASE_MASK |
535 GDK_ENTER_NOTIFY_MASK |
536 GDK_LEAVE_NOTIFY_MASK);
538 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
539 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
540 gdk_window_set_user_data (widget->window, widget);
542 widget->style = gtk_style_attach (widget->style, widget->window);
543 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
547 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
549 if (!menu_shell->active)
551 gtk_grab_add (GTK_WIDGET (menu_shell));
552 menu_shell->have_grab = TRUE;
553 menu_shell->active = TRUE;
558 gtk_menu_shell_button_press (GtkWidget *widget,
559 GdkEventButton *event)
561 GtkMenuShell *menu_shell;
562 GtkWidget *menu_item;
564 if (event->type != GDK_BUTTON_PRESS)
567 menu_shell = GTK_MENU_SHELL (widget);
569 if (menu_shell->parent_menu_shell)
570 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
572 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
574 if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
575 menu_item != GTK_MENU_SHELL (menu_item->parent)->active_menu_item)
577 /* select the menu item *before* activating the shell, so submenus
578 * which might be open are closed the friendly way. If we activate
579 * (and thus grab) this menu shell first, we might get grab_broken
580 * events which will close the entire menu hierarchy. Selecting the
581 * menu item also fixes up the state as if enter_notify() would
582 * have run before (which normally selects the item).
584 if (GTK_MENU_SHELL_GET_CLASS (menu_item->parent)->submenu_placement != GTK_TOP_BOTTOM)
586 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu_item->parent), menu_item);
590 if (!menu_shell->active || !menu_shell->button)
592 _gtk_menu_shell_activate (menu_shell);
594 menu_shell->button = event->button;
596 if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
597 menu_item->parent == widget &&
598 menu_item != menu_shell->active_menu_item)
600 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
602 menu_shell->activate_time = event->time;
603 gtk_menu_shell_select_item (menu_shell, menu_item);
609 widget = gtk_get_event_widget ((GdkEvent*) event);
610 if (widget == GTK_WIDGET (menu_shell))
612 gtk_menu_shell_deactivate (menu_shell);
613 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
617 if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
618 GTK_MENU_ITEM (menu_item)->submenu != NULL &&
619 !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
621 GtkMenuShellPrivate *priv;
623 _gtk_menu_item_popup_submenu (menu_item, FALSE);
625 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
626 priv->activated_submenu = TRUE;
633 gtk_menu_shell_grab_broken (GtkWidget *widget,
634 GdkEventGrabBroken *event)
636 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
638 if (menu_shell->have_xgrab && event->grab_window == NULL)
640 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
643 gtk_menu_shell_deselect (menu_shell);
645 gtk_menu_shell_deactivate (menu_shell);
646 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
653 gtk_menu_shell_button_release (GtkWidget *widget,
654 GdkEventButton *event)
656 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
657 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (widget);
659 if (menu_shell->active)
661 GtkWidget *menu_item;
662 gboolean deactivate = TRUE;
664 if (menu_shell->button && (event->button != menu_shell->button))
666 menu_shell->button = 0;
667 if (menu_shell->parent_menu_shell)
668 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
671 menu_shell->button = 0;
672 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
674 if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
676 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
677 _gtk_menu_item_is_selectable (menu_item))
679 GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->submenu;
683 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
687 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
688 priv->activated_submenu)
691 GTimeVal *popup_time;
692 gint64 usec_since_popup = 0;
694 g_object_get (gtk_widget_get_settings (widget),
695 "gtk-menu-popdown-delay", &popdown_delay,
698 popup_time = g_object_get_data (G_OBJECT (submenu),
699 "gtk-menu-exact-popup-time");
703 GTimeVal current_time;
705 g_get_current_time (¤t_time);
707 usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
708 (gint64) current_time.tv_usec -
709 (gint64) popup_time->tv_sec * 1000 * 1000 -
710 (gint64) popup_time->tv_usec);
712 g_object_set_data (G_OBJECT (submenu),
713 "gtk-menu-exact-popup-time", NULL);
716 /* only close the submenu on click if we opened the
717 * menu explicitely (usec_since_popup == 0) or
718 * enough time has passed since it was opened by
719 * GtkMenuItem's timeout (usec_since_popup > delay).
721 if (!priv->activated_submenu &&
722 (usec_since_popup == 0 ||
723 usec_since_popup > popdown_delay * 1000))
725 _gtk_menu_item_popdown_submenu (menu_item);
729 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
735 else if (menu_item &&
736 !_gtk_menu_item_is_selectable (menu_item) &&
737 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
741 else if (menu_shell->parent_menu_shell)
743 menu_shell->active = TRUE;
744 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
748 /* If we ended up on an item with a submenu, leave the menu up.
750 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
751 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
756 else /* a very fast press-release */
758 /* We only ever want to prevent deactivation on the first
759 * press/release. Setting the time to zero is a bit of a
760 * hack, since we could be being triggered in the first
761 * few fractions of a second after a server time wraparound.
762 * the chances of that happening are ~1/10^6, without
763 * serious harm if we lose.
765 menu_shell->activate_time = 0;
771 gtk_menu_shell_deactivate (menu_shell);
772 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
775 priv->activated_submenu = FALSE;
782 _gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
783 gboolean keyboard_mode)
785 menu_shell->keyboard_mode = keyboard_mode;
789 _gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
791 return menu_shell->keyboard_mode;
795 _gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
797 GtkMenuShell *target;
798 gboolean auto_mnemonics;
800 gboolean mnemonics_visible;
802 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
803 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
812 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (target);
813 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));
815 /* The idea with keyboard mode is that once you start using
816 * the keyboard to navigate the menus, we show mnemonics
817 * until the menu navigation is over. To that end, we spread
818 * the keyboard mode upwards in the menu hierarchy here.
819 * Also see gtk_menu_popup, where we inherit it downwards.
821 if (menu_shell->keyboard_mode)
822 target->keyboard_mode = TRUE;
824 /* While navigating menus, the first parent menu with an active
825 * item is the one where mnemonics are effective, as can be seen
826 * in gtk_menu_shell_key_press below.
827 * We also show mnemonics in context menus. The grab condition is
828 * necessary to ensure we remove underlines from menu bars when
831 mnemonics_visible = target->keyboard_mode &&
832 (((target->active_menu_item || priv->in_unselectable_item) && !found) ||
833 (target == menu_shell &&
834 !target->parent_menu_shell &&
835 gtk_widget_has_grab (GTK_WIDGET (target))));
837 /* While menus are up, only show underlines inside the menubar,
838 * not in the entire window.
840 if (GTK_IS_MENU_BAR (target))
842 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
843 _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
847 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
849 if (target->active_menu_item || priv->in_unselectable_item)
852 target = GTK_MENU_SHELL (target->parent_menu_shell);
857 gtk_menu_shell_key_press (GtkWidget *widget,
860 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
861 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
862 gboolean enable_mnemonics;
864 menu_shell->keyboard_mode = TRUE;
866 if (!(menu_shell->active_menu_item || priv->in_unselectable_item) && menu_shell->parent_menu_shell)
867 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
869 if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
872 g_object_get (gtk_widget_get_settings (widget),
873 "gtk-enable-mnemonics", &enable_mnemonics,
876 if (enable_mnemonics)
877 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
883 gtk_menu_shell_enter_notify (GtkWidget *widget,
884 GdkEventCrossing *event)
886 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
888 if (event->mode == GDK_CROSSING_GTK_GRAB ||
889 event->mode == GDK_CROSSING_GTK_UNGRAB ||
890 event->mode == GDK_CROSSING_STATE_CHANGED)
893 if (menu_shell->active)
895 GtkWidget *menu_item;
897 menu_item = gtk_get_event_widget ((GdkEvent*) event);
902 if (GTK_IS_MENU_ITEM (menu_item) &&
903 !_gtk_menu_item_is_selectable (menu_item))
905 GtkMenuShellPrivate *priv;
907 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
908 priv->in_unselectable_item = TRUE;
913 if (menu_item->parent == widget &&
914 GTK_IS_MENU_ITEM (menu_item))
916 if (menu_shell->ignore_enter)
919 if (event->detail != GDK_NOTIFY_INFERIOR)
921 if (gtk_widget_get_state (menu_item) != GTK_STATE_PRELIGHT)
922 gtk_menu_shell_select_item (menu_shell, menu_item);
924 /* If any mouse button is down, and there is a submenu
925 * that is not yet visible, activate it. It's sufficient
926 * to check for any button's mask (not only the one
927 * matching menu_shell->button), because there is no
928 * situation a mouse button could be pressed while
929 * entering a menu item where we wouldn't want to show
932 if ((event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) &&
933 GTK_MENU_ITEM (menu_item)->submenu != NULL)
935 GtkMenuShellPrivate *priv;
937 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
938 priv->activated_submenu = TRUE;
940 if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
942 gboolean touchscreen_mode;
944 g_object_get (gtk_widget_get_settings (widget),
945 "gtk-touchscreen-mode", &touchscreen_mode,
948 if (touchscreen_mode)
949 _gtk_menu_item_popup_submenu (menu_item, TRUE);
954 else if (menu_shell->parent_menu_shell)
956 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
964 gtk_menu_shell_leave_notify (GtkWidget *widget,
965 GdkEventCrossing *event)
967 if (event->mode == GDK_CROSSING_GTK_GRAB ||
968 event->mode == GDK_CROSSING_GTK_GRAB ||
969 event->mode == GDK_CROSSING_STATE_CHANGED)
972 if (gtk_widget_get_visible (widget))
974 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
975 GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
976 GtkMenuItem *menu_item;
978 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
981 menu_item = GTK_MENU_ITEM (event_widget);
983 if (!_gtk_menu_item_is_selectable (event_widget))
985 GtkMenuShellPrivate *priv;
987 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
988 priv->in_unselectable_item = TRUE;
993 if ((menu_shell->active_menu_item == event_widget) &&
994 (menu_item->submenu == NULL))
996 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
997 (gtk_widget_get_state (GTK_WIDGET (menu_item)) != GTK_STATE_NORMAL))
999 gtk_menu_shell_deselect (menu_shell);
1002 else if (menu_shell->parent_menu_shell)
1004 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
1012 gtk_menu_shell_screen_changed (GtkWidget *widget,
1013 GdkScreen *previous_screen)
1015 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
1019 gtk_menu_shell_add (GtkContainer *container,
1022 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
1026 gtk_menu_shell_remove (GtkContainer *container,
1029 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1032 was_visible = gtk_widget_get_visible (widget);
1033 menu_shell->children = g_list_remove (menu_shell->children, widget);
1035 if (widget == menu_shell->active_menu_item)
1037 gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
1038 menu_shell->active_menu_item = NULL;
1041 gtk_widget_unparent (widget);
1043 /* queue resize regardless of gtk_widget_get_visible (container),
1044 * since that's what is needed by toplevels.
1047 gtk_widget_queue_resize (GTK_WIDGET (container));
1051 gtk_menu_shell_forall (GtkContainer *container,
1052 gboolean include_internals,
1053 GtkCallback callback,
1054 gpointer callback_data)
1056 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1060 children = menu_shell->children;
1063 child = children->data;
1064 children = children->next;
1066 (* callback) (child, callback_data);
1072 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
1074 if (menu_shell->active)
1076 menu_shell->button = 0;
1077 menu_shell->active = FALSE;
1078 menu_shell->activate_time = 0;
1080 if (menu_shell->active_menu_item)
1082 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1083 menu_shell->active_menu_item = NULL;
1086 if (menu_shell->have_grab)
1088 menu_shell->have_grab = FALSE;
1089 gtk_grab_remove (GTK_WIDGET (menu_shell));
1091 if (menu_shell->have_xgrab)
1093 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
1095 menu_shell->have_xgrab = FALSE;
1096 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1097 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1100 menu_shell->keyboard_mode = FALSE;
1102 _gtk_menu_shell_update_mnemonics (menu_shell);
1107 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1112 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1113 g_return_val_if_fail (child != NULL, FALSE);
1115 parent = child->parent;
1116 while (GTK_IS_MENU_SHELL (parent))
1118 if (parent == (GtkWidget*) menu_shell)
1120 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1127 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1130 GtkWidget *menu_item;
1132 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1134 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1135 menu_item = menu_item->parent;
1137 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1143 /* Handlers for action signals */
1146 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1147 GtkWidget *menu_item)
1149 GtkMenuShellClass *class;
1151 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1152 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1154 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1156 if (class->select_item &&
1157 !(menu_shell->active &&
1158 menu_shell->active_menu_item == menu_item))
1159 class->select_item (menu_shell, menu_item);
1162 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1163 GtkSubmenuPlacement placement);
1166 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1167 GtkWidget *menu_item)
1169 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1171 gtk_menu_shell_deselect (menu_shell);
1173 if (!_gtk_menu_item_is_selectable (menu_item))
1175 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1177 priv->in_unselectable_item = TRUE;
1178 _gtk_menu_shell_update_mnemonics (menu_shell);
1183 menu_shell->active_menu_item = menu_item;
1184 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1185 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1188 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1189 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1190 gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1192 _gtk_menu_shell_update_mnemonics (menu_shell);
1194 /* This allows the bizarre radio buttons-with-submenus-display-history
1197 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1198 gtk_widget_activate (menu_shell->active_menu_item);
1202 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1204 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1206 if (menu_shell->active_menu_item)
1208 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1209 menu_shell->active_menu_item = NULL;
1210 _gtk_menu_shell_update_mnemonics (menu_shell);
1215 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
1216 GtkWidget *menu_item,
1217 gboolean force_deactivate)
1219 GSList *slist, *shells = NULL;
1220 gboolean deactivate = force_deactivate;
1222 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1223 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1226 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1228 g_object_ref (menu_shell);
1229 g_object_ref (menu_item);
1233 GtkMenuShell *parent_menu_shell = menu_shell;
1237 g_object_ref (parent_menu_shell);
1238 shells = g_slist_prepend (shells, parent_menu_shell);
1239 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1241 while (parent_menu_shell);
1242 shells = g_slist_reverse (shells);
1244 gtk_menu_shell_deactivate (menu_shell);
1246 /* flush the x-queue, so any grabs are removed and
1247 * the menu is actually taken down
1249 gdk_display_sync (gtk_widget_get_display (menu_item));
1252 gtk_widget_activate (menu_item);
1254 for (slist = shells; slist; slist = slist->next)
1256 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1257 g_object_unref (slist->data);
1259 g_slist_free (shells);
1261 g_object_unref (menu_shell);
1262 g_object_unref (menu_item);
1265 /* Distance should be +/- 1 */
1267 gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
1270 if (menu_shell->active_menu_item)
1272 GList *node = g_list_find (menu_shell->children,
1273 menu_shell->active_menu_item);
1274 GList *start_node = node;
1275 gboolean wrap_around;
1277 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1278 "gtk-keynav-wrap-around", &wrap_around,
1284 while (node != start_node &&
1285 (!node || !_gtk_menu_item_is_selectable (node->data)))
1289 else if (wrap_around)
1290 node = menu_shell->children;
1293 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1301 while (node != start_node &&
1302 (!node || !_gtk_menu_item_is_selectable (node->data)))
1306 else if (wrap_around)
1307 node = g_list_last (menu_shell->children);
1310 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1317 gtk_menu_shell_select_item (menu_shell, node->data);
1323 /* Distance should be +/- 1 */
1325 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1328 gboolean handled = FALSE;
1330 g_signal_emit (menu_shell, menu_shell_signals[MOVE_SELECTED], 0,
1331 distance, &handled);
1335 * gtk_menu_shell_select_first:
1336 * @menu_shell: a #GtkMenuShell
1337 * @search_sensitive: if %TRUE, search for the first selectable
1338 * menu item, otherwise select nothing if
1339 * the first item isn't sensitive. This
1340 * should be %FALSE if the menu is being
1341 * popped up initially.
1343 * Select the first visible or selectable child of the menu shell;
1344 * don't select tearoff items unless the only item is a tearoff
1350 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1351 gboolean search_sensitive)
1353 GtkWidget *to_select = NULL;
1356 tmp_list = menu_shell->children;
1359 GtkWidget *child = tmp_list->data;
1361 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1362 _gtk_menu_item_is_selectable (child))
1365 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1369 tmp_list = tmp_list->next;
1373 gtk_menu_shell_select_item (menu_shell, to_select);
1377 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1378 gboolean search_sensitive)
1380 GtkWidget *to_select = NULL;
1383 tmp_list = g_list_last (menu_shell->children);
1386 GtkWidget *child = tmp_list->data;
1388 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1389 _gtk_menu_item_is_selectable (child))
1392 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1396 tmp_list = tmp_list->prev;
1400 gtk_menu_shell_select_item (menu_shell, to_select);
1404 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1406 GtkMenuItem *menu_item;
1408 if (menu_shell->active_menu_item == NULL)
1411 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1413 if (menu_item->submenu)
1415 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1416 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1417 if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1425 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1426 GtkMenuDirectionType direction)
1428 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1429 GtkMenuShell *parent_menu_shell = NULL;
1430 gboolean had_selection;
1431 gboolean touchscreen_mode;
1433 priv->in_unselectable_item = FALSE;
1435 had_selection = menu_shell->active_menu_item != NULL;
1437 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1438 "gtk-touchscreen-mode", &touchscreen_mode,
1441 if (menu_shell->parent_menu_shell)
1442 parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1446 case GTK_MENU_DIR_PARENT:
1447 if (touchscreen_mode &&
1448 menu_shell->active_menu_item &&
1449 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
1450 gtk_widget_get_visible (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu))
1452 /* if we are on a menu item that has an open submenu but the
1453 * focus is not in that submenu (e.g. because it's empty or
1454 * has only insensitive items), close that submenu instead
1455 * of running into the code below which would close *this*
1458 _gtk_menu_item_popdown_submenu (menu_shell->active_menu_item);
1459 _gtk_menu_shell_update_mnemonics (menu_shell);
1461 else if (parent_menu_shell)
1463 if (touchscreen_mode)
1465 /* close menu when returning from submenu. */
1466 _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->parent_menu_item);
1467 _gtk_menu_shell_update_mnemonics (parent_menu_shell);
1471 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1472 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1473 gtk_menu_shell_deselect (menu_shell);
1476 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1477 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1479 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1480 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1483 /* If there is no parent and the submenu is in the opposite direction
1484 * to the menu, then make the PARENT direction wrap around to
1485 * the bottom of the submenu.
1487 else if (menu_shell->active_menu_item &&
1488 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1489 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1491 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1493 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1494 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1495 _gtk_menu_shell_select_last (submenu, TRUE);
1499 case GTK_MENU_DIR_CHILD:
1500 if (menu_shell->active_menu_item &&
1501 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1502 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1504 if (gtk_menu_shell_select_submenu_first (menu_shell))
1508 /* Try to find a menu running the opposite direction */
1509 while (parent_menu_shell &&
1510 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1511 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1513 parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->parent_menu_shell);
1516 if (parent_menu_shell)
1518 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1519 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1521 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1523 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1527 case GTK_MENU_DIR_PREV:
1528 gtk_menu_shell_move_selected (menu_shell, -1);
1529 if (!had_selection &&
1530 !menu_shell->active_menu_item &&
1531 menu_shell->children)
1532 _gtk_menu_shell_select_last (menu_shell, TRUE);
1535 case GTK_MENU_DIR_NEXT:
1536 gtk_menu_shell_move_selected (menu_shell, 1);
1537 if (!had_selection &&
1538 !menu_shell->active_menu_item &&
1539 menu_shell->children)
1540 gtk_menu_shell_select_first (menu_shell, TRUE);
1546 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1547 gboolean force_hide)
1549 if (menu_shell->active_menu_item &&
1550 _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1552 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1553 gtk_menu_shell_activate_item (menu_shell,
1554 menu_shell->active_menu_item,
1557 _gtk_menu_item_popup_submenu (menu_shell->active_menu_item, FALSE);
1562 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1564 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1566 gtk_menu_shell_deselect (menu_shell);
1568 gtk_menu_shell_deactivate (menu_shell);
1569 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1573 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1574 GtkDirectionType dir)
1576 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1578 if (menu_shell->parent_menu_shell)
1579 menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1585 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1589 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1591 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1593 if (klass->get_popup_delay)
1595 return klass->get_popup_delay (menu_shell);
1600 GtkWidget *widget = GTK_WIDGET (menu_shell);
1602 g_object_get (gtk_widget_get_settings (widget),
1603 "gtk-menu-popup-delay", &popup_delay,
1611 * gtk_menu_shell_cancel:
1612 * @menu_shell: a #GtkMenuShell
1614 * Cancels the selection within the menu shell.
1619 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1621 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1623 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1626 static GtkMnemonicHash *
1627 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1630 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1632 if (!private->mnemonic_hash && create)
1633 private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1635 return private->mnemonic_hash;
1639 menu_shell_add_mnemonic_foreach (guint keyval,
1643 GtkKeyHash *key_hash = data;
1645 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1649 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1652 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1653 GtkWidget *widget = GTK_WIDGET (menu_shell);
1655 if (!private->key_hash && create && gtk_widget_has_screen (widget))
1657 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1658 GdkScreen *screen = gtk_widget_get_screen (widget);
1659 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1664 private->key_hash = _gtk_key_hash_new (keymap, NULL);
1666 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1667 menu_shell_add_mnemonic_foreach,
1671 return private->key_hash;
1675 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1677 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1679 if (private->key_hash)
1681 _gtk_key_hash_free (private->key_hash);
1682 private->key_hash = NULL;
1687 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1690 GtkMnemonicHash *mnemonic_hash;
1691 GtkKeyHash *key_hash;
1693 gboolean result = FALSE;
1695 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1699 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1703 entries = _gtk_key_hash_lookup (key_hash,
1704 event->hardware_keycode,
1706 gtk_accelerator_get_default_mod_mask (),
1710 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1711 GPOINTER_TO_UINT (entries->data));
1717 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1721 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1722 g_return_if_fail (GTK_IS_WIDGET (target));
1724 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1726 gtk_menu_shell_reset_key_hash (menu_shell);
1730 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1734 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1735 g_return_if_fail (GTK_IS_WIDGET (target));
1737 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1739 gtk_menu_shell_reset_key_hash (menu_shell);
1743 * gtk_menu_shell_get_take_focus:
1744 * @menu_shell: a #GtkMenuShell
1746 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1748 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1753 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1755 GtkMenuShellPrivate *priv;
1757 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1759 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1761 return priv->take_focus;
1765 * gtk_menu_shell_set_take_focus:
1766 * @menu_shell: a #GtkMenuShell
1767 * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1769 * If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1770 * focus so that it will receive all keyboard events which is needed to enable
1771 * keyboard navigation in menus.
1773 * Setting @take_focus to %FALSE is useful only for special applications
1774 * like virtual keyboard implementations which should not take keyboard
1777 * The @take_focus state of a menu or menu bar is automatically propagated
1778 * to submenus whenever a submenu is popped up, so you don't have to worry
1779 * about recursively setting it for your entire menu hierarchy. Only when
1780 * programmatically picking a submenu and popping it up manually, the
1781 * @take_focus property of the submenu needs to be set explicitely.
1783 * Note that setting it to %FALSE has side-effects:
1785 * If the focus is in some other app, it keeps the focus and keynav in
1786 * the menu doesn't work. Consequently, keynav on the menu will only
1787 * work if the focus is on some toplevel owned by the onscreen keyboard.
1789 * To avoid confusing the user, menus with @take_focus set to %FALSE
1790 * should not display mnemonics or accelerators, since it cannot be
1791 * guaranteed that they will work.
1793 * See also gdk_keyboard_grab()
1798 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1799 gboolean take_focus)
1801 GtkMenuShellPrivate *priv;
1803 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1805 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1807 if (priv->take_focus != take_focus)
1809 priv->take_focus = take_focus;
1810 g_object_notify (G_OBJECT (menu_shell), "take-focus");
1814 #define __GTK_MENU_SHELL_C__
1815 #include "gtkaliasdef.c"