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"
34 #include "gtkmarshalers.h"
35 #include "gtkmenubar.h"
36 #include "gtkmenuitem.h"
37 #include "gtkmenushell.h"
38 #include "gtkmnemonichash.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
41 #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)
67 typedef void (*GtkMenuShellSignal1) (GtkObject *object,
68 GtkMenuDirectionType arg1,
70 typedef void (*GtkMenuShellSignal2) (GtkObject *object,
76 * A menu item can be "selected", this means that it is displayed
77 * in the prelight state, and if it has a submenu, that submenu
80 * A menu is "active" when it is visible onscreen and the user
81 * is selecting from it. A menubar is not active until the user
82 * clicks on one of its menuitems. When a menu is active,
83 * passing the mouse over a submenu will pop it up.
85 * menu_shell->active_menu_item, is however, not an "active"
86 * menu item (there is no such thing) but rather, the selected
87 * menu item in that MenuShell, if there is one.
89 * There is also is a concept of the current menu and a current
90 * menu item. The current menu item is the selected menu item
91 * that is furthest down in the heirarchy. (Every active menu_shell
92 * does not necessarily contain a selected menu item, but if
93 * it does, then menu_shell->parent_menu_shell must also contain
94 * a selected menu item. The current menu is the menu that
95 * contains the current menu_item. It will always have a GTK
96 * grab and receive all key presses.
101 * ::move_current (GtkMenuDirection *dir)
102 * Moves the current menu item in direction 'dir':
104 * GTK_MENU_DIR_PARENT: To the parent menu shell
105 * GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
107 * GTK_MENU_DIR_NEXT/PREV: To the next or previous item
110 * As a a bit of a hack to get movement between menus and
111 * menubars working, if submenu_placement is different for
112 * the menu and its MenuShell then the following apply:
114 * - For 'parent' the current menu is not just moved to
115 * the parent, but moved to the previous entry in the parent
116 * - For 'child', if there is no child, then current is
117 * moved to the next item in the parent.
119 * Note that the above explanation of ::move_current was written
120 * before menus and menubars had support for RTL flipping and
121 * different packing directions, and therefore only applies for
122 * when text direction and packing direction are both left-to-right.
124 * ::activate_current (GBoolean *force_hide)
125 * Activate the current item. If 'force_hide' is true, hide
126 * the current menu item always. Otherwise, only hide
127 * it if menu_item->klass->hide_on_activate is true.
130 * Cancels the current selection
133 #define GTK_MENU_SHELL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MENU_SHELL, GtkMenuShellPrivate))
135 typedef struct _GtkMenuShellPrivate GtkMenuShellPrivate;
137 struct _GtkMenuShellPrivate
139 GtkMnemonicHash *mnemonic_hash;
140 GtkKeyHash *key_hash;
145 static void gtk_menu_shell_class_init (GtkMenuShellClass *klass);
146 static void gtk_menu_shell_init (GtkMenuShell *menu_shell);
147 static void gtk_menu_shell_set_property (GObject *object,
151 static void gtk_menu_shell_get_property (GObject *object,
155 static void gtk_menu_shell_realize (GtkWidget *widget);
156 static void gtk_menu_shell_finalize (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 void gtk_menu_shell_add (GtkContainer *container,
171 static void gtk_menu_shell_remove (GtkContainer *container,
173 static void gtk_menu_shell_forall (GtkContainer *container,
174 gboolean include_internals,
175 GtkCallback callback,
176 gpointer callback_data);
177 static void gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
180 static void gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell);
181 static gint gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
183 static GtkWidget *gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
185 static GType gtk_menu_shell_child_type (GtkContainer *container);
186 static void gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
187 GtkWidget *menu_item);
188 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell);
190 static void gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
191 GtkMenuDirectionType direction);
192 static void gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
193 gboolean force_hide);
194 static void gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell);
195 static void gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
196 GtkDirectionType dir);
198 static void gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell);
199 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
202 static GtkContainerClass *parent_class = NULL;
203 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
207 gtk_menu_shell_get_type (void)
209 static GType menu_shell_type = 0;
211 if (!menu_shell_type)
213 static const GTypeInfo menu_shell_info =
215 sizeof (GtkMenuShellClass),
216 NULL, /* base_init */
217 NULL, /* base_finalize */
218 (GClassInitFunc) gtk_menu_shell_class_init,
219 NULL, /* class_finalize */
220 NULL, /* class_data */
221 sizeof (GtkMenuShell),
223 (GInstanceInitFunc) gtk_menu_shell_init,
224 NULL, /* value_table */
228 g_type_register_static (GTK_TYPE_CONTAINER, "GtkMenuShell",
229 &menu_shell_info, G_TYPE_FLAG_ABSTRACT);
232 return menu_shell_type;
236 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
238 GObjectClass *object_class;
239 GtkWidgetClass *widget_class;
240 GtkContainerClass *container_class;
242 GtkBindingSet *binding_set;
244 object_class = (GObjectClass*) klass;
245 widget_class = (GtkWidgetClass*) klass;
246 container_class = (GtkContainerClass*) klass;
248 parent_class = g_type_class_peek_parent (klass);
250 object_class->set_property = gtk_menu_shell_set_property;
251 object_class->get_property = gtk_menu_shell_get_property;
252 object_class->finalize = gtk_menu_shell_finalize;
254 widget_class->realize = gtk_menu_shell_realize;
255 widget_class->button_press_event = gtk_menu_shell_button_press;
256 widget_class->button_release_event = gtk_menu_shell_button_release;
257 widget_class->key_press_event = gtk_menu_shell_key_press;
258 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
259 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
260 widget_class->screen_changed = gtk_menu_shell_screen_changed;
262 container_class->add = gtk_menu_shell_add;
263 container_class->remove = gtk_menu_shell_remove;
264 container_class->forall = gtk_menu_shell_forall;
265 container_class->child_type = gtk_menu_shell_child_type;
267 klass->submenu_placement = GTK_TOP_BOTTOM;
268 klass->deactivate = gtk_real_menu_shell_deactivate;
269 klass->selection_done = NULL;
270 klass->move_current = gtk_real_menu_shell_move_current;
271 klass->activate_current = gtk_real_menu_shell_activate_current;
272 klass->cancel = gtk_real_menu_shell_cancel;
273 klass->select_item = gtk_menu_shell_real_select_item;
274 klass->insert = gtk_menu_shell_real_insert;
276 menu_shell_signals[DEACTIVATE] =
277 g_signal_new ("deactivate",
278 G_OBJECT_CLASS_TYPE (object_class),
280 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
282 _gtk_marshal_VOID__VOID,
284 menu_shell_signals[SELECTION_DONE] =
285 g_signal_new ("selection-done",
286 G_OBJECT_CLASS_TYPE (object_class),
288 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
290 _gtk_marshal_VOID__VOID,
292 menu_shell_signals[MOVE_CURRENT] =
293 g_signal_new ("move_current",
294 G_OBJECT_CLASS_TYPE (object_class),
295 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
296 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
298 _gtk_marshal_VOID__ENUM,
300 GTK_TYPE_MENU_DIRECTION_TYPE);
301 menu_shell_signals[ACTIVATE_CURRENT] =
302 g_signal_new ("activate_current",
303 G_OBJECT_CLASS_TYPE (object_class),
304 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
305 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
307 _gtk_marshal_VOID__BOOLEAN,
310 menu_shell_signals[CANCEL] =
311 g_signal_new ("cancel",
312 G_OBJECT_CLASS_TYPE (object_class),
313 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
314 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
316 _gtk_marshal_VOID__VOID,
318 menu_shell_signals[CYCLE_FOCUS] =
319 _gtk_binding_signal_new ("cycle_focus",
320 G_OBJECT_CLASS_TYPE (object_class),
321 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
322 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
324 _gtk_marshal_VOID__ENUM,
326 GTK_TYPE_DIRECTION_TYPE);
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,
356 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
357 gtk_binding_entry_add_signal (binding_set,
358 GDK_F10, GDK_SHIFT_MASK,
360 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
363 * GtkMenuShell:take-focus:
365 * A boolean that determines whether the menu and its submenus grab the
366 * keyboard focus. See gtk_menu_shell_set_take_focus() and
367 * gtk_menu_shell_get_take_focus().
371 g_object_class_install_property (object_class,
373 g_param_spec_boolean ("take-focus",
375 P_("A boolean that determines whether the menu grabs the keyboard focus"),
377 GTK_PARAM_READWRITE));
379 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
383 gtk_menu_shell_child_type (GtkContainer *container)
385 return GTK_TYPE_MENU_ITEM;
389 gtk_menu_shell_init (GtkMenuShell *menu_shell)
391 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
393 menu_shell->children = NULL;
394 menu_shell->active_menu_item = NULL;
395 menu_shell->parent_menu_shell = NULL;
396 menu_shell->active = FALSE;
397 menu_shell->have_grab = FALSE;
398 menu_shell->have_xgrab = FALSE;
399 menu_shell->button = 0;
400 menu_shell->activate_time = 0;
402 priv->mnemonic_hash = NULL;
403 priv->key_hash = NULL;
404 priv->take_focus = TRUE;
408 gtk_menu_shell_set_property (GObject *object,
413 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
417 case PROP_TAKE_FOCUS:
418 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
421 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
427 gtk_menu_shell_get_property (GObject *object,
432 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
436 case PROP_TAKE_FOCUS:
437 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
440 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446 gtk_menu_shell_finalize (GObject *object)
448 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
449 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
451 if (priv->mnemonic_hash)
452 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
454 _gtk_key_hash_free (priv->key_hash);
456 G_OBJECT_CLASS (parent_class)->finalize (object);
461 gtk_menu_shell_append (GtkMenuShell *menu_shell,
464 gtk_menu_shell_insert (menu_shell, child, -1);
468 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
471 gtk_menu_shell_insert (menu_shell, child, 0);
475 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
479 GtkMenuShellClass *class;
481 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
482 g_return_if_fail (GTK_IS_MENU_ITEM (child));
484 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
487 class->insert (menu_shell, child, position);
491 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
495 menu_shell->children = g_list_insert (menu_shell->children, child, position);
497 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
501 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
503 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
505 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
509 gtk_menu_shell_realize (GtkWidget *widget)
511 GdkWindowAttr attributes;
512 gint attributes_mask;
514 g_return_if_fail (GTK_IS_MENU_SHELL (widget));
516 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
518 attributes.x = widget->allocation.x;
519 attributes.y = widget->allocation.y;
520 attributes.width = widget->allocation.width;
521 attributes.height = widget->allocation.height;
522 attributes.window_type = GDK_WINDOW_CHILD;
523 attributes.wclass = GDK_INPUT_OUTPUT;
524 attributes.visual = gtk_widget_get_visual (widget);
525 attributes.colormap = gtk_widget_get_colormap (widget);
526 attributes.event_mask = gtk_widget_get_events (widget);
527 attributes.event_mask |= (GDK_EXPOSURE_MASK |
528 GDK_BUTTON_PRESS_MASK |
529 GDK_BUTTON_RELEASE_MASK |
531 GDK_ENTER_NOTIFY_MASK |
532 GDK_LEAVE_NOTIFY_MASK);
534 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
535 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
536 gdk_window_set_user_data (widget->window, widget);
538 widget->style = gtk_style_attach (widget->style, widget->window);
539 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
543 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
545 if (!menu_shell->active)
547 gtk_grab_add (GTK_WIDGET (menu_shell));
548 menu_shell->have_grab = TRUE;
549 menu_shell->active = TRUE;
554 gtk_menu_shell_button_press (GtkWidget *widget,
555 GdkEventButton *event)
557 GtkMenuShell *menu_shell;
558 GtkWidget *menu_item;
560 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
561 g_return_val_if_fail (event != NULL, FALSE);
563 if (event->type != GDK_BUTTON_PRESS)
566 menu_shell = GTK_MENU_SHELL (widget);
568 if (menu_shell->parent_menu_shell)
570 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
572 else if (!menu_shell->active || !menu_shell->button)
574 _gtk_menu_shell_activate (menu_shell);
576 menu_shell->button = event->button;
578 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
580 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
582 if ((menu_item->parent == widget) &&
583 (menu_item != menu_shell->active_menu_item))
585 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
587 menu_shell->activate_time = event->time;
590 gtk_menu_shell_select_item (menu_shell, menu_item);
596 widget = gtk_get_event_widget ((GdkEvent*) event);
597 if (widget == GTK_WIDGET (menu_shell))
599 gtk_menu_shell_deactivate (menu_shell);
600 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
608 gtk_menu_shell_button_release (GtkWidget *widget,
609 GdkEventButton *event)
611 GtkMenuShell *menu_shell;
612 GtkWidget *menu_item;
615 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
616 g_return_val_if_fail (event != NULL, FALSE);
618 menu_shell = GTK_MENU_SHELL (widget);
619 if (menu_shell->active)
621 if (menu_shell->button && (event->button != menu_shell->button))
623 menu_shell->button = 0;
624 if (menu_shell->parent_menu_shell)
625 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
628 menu_shell->button = 0;
629 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
633 if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
635 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
636 _gtk_menu_item_is_selectable (menu_item))
638 if (GTK_MENU_ITEM (menu_item)->submenu == NULL)
640 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
643 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
645 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
649 else if (menu_item &&
650 !_gtk_menu_item_is_selectable (menu_item) &&
651 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
655 else if (menu_shell->parent_menu_shell)
657 menu_shell->active = TRUE;
658 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
662 /* If we ended up on an item with a submenu, leave the menu up.
664 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
665 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
670 else /* a very fast press-release */
672 /* We only ever want to prevent deactivation on the first
673 * press/release. Setting the time to zero is a bit of a
674 * hack, since we could be being triggered in the first
675 * few fractions of a second after a server time wraparound.
676 * the chances of that happening are ~1/10^6, without
677 * serious harm if we lose.
679 menu_shell->activate_time = 0;
685 gtk_menu_shell_deactivate (menu_shell);
686 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
694 gtk_menu_shell_key_press (GtkWidget *widget,
697 GtkMenuShell *menu_shell;
699 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
700 g_return_val_if_fail (event != NULL, FALSE);
702 menu_shell = GTK_MENU_SHELL (widget);
704 if (!menu_shell->active_menu_item && menu_shell->parent_menu_shell)
705 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
707 if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
710 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
714 gtk_menu_shell_enter_notify (GtkWidget *widget,
715 GdkEventCrossing *event)
717 GtkMenuShell *menu_shell;
718 GtkWidget *menu_item;
720 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
721 g_return_val_if_fail (event != NULL, FALSE);
723 menu_shell = GTK_MENU_SHELL (widget);
725 if (menu_shell->active)
727 menu_item = gtk_get_event_widget ((GdkEvent*) event);
730 (GTK_IS_MENU_ITEM (menu_item) &&
731 !_gtk_menu_item_is_selectable (menu_item)))
734 if ((menu_item->parent == widget) &&
735 (menu_shell->active_menu_item != menu_item) &&
736 GTK_IS_MENU_ITEM (menu_item))
738 if (menu_shell->ignore_enter)
741 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
742 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
744 gtk_menu_shell_select_item (menu_shell, menu_item);
747 else if (menu_shell->parent_menu_shell)
749 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
757 gtk_menu_shell_leave_notify (GtkWidget *widget,
758 GdkEventCrossing *event)
760 GtkMenuShell *menu_shell;
761 GtkMenuItem *menu_item;
762 GtkWidget *event_widget;
764 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
765 g_return_val_if_fail (event != NULL, FALSE);
767 if (GTK_WIDGET_VISIBLE (widget))
769 menu_shell = GTK_MENU_SHELL (widget);
770 event_widget = gtk_get_event_widget ((GdkEvent*) event);
772 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
775 menu_item = GTK_MENU_ITEM (event_widget);
777 if (!_gtk_menu_item_is_selectable (event_widget))
780 if ((menu_shell->active_menu_item == event_widget) &&
781 (menu_item->submenu == NULL))
783 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
784 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
786 gtk_menu_shell_deselect (menu_shell);
789 else if (menu_shell->parent_menu_shell)
791 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
799 gtk_menu_shell_screen_changed (GtkWidget *widget,
800 GdkScreen *previous_screen)
802 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
806 gtk_menu_shell_add (GtkContainer *container,
809 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
813 gtk_menu_shell_remove (GtkContainer *container,
816 GtkMenuShell *menu_shell;
819 g_return_if_fail (GTK_IS_MENU_SHELL (container));
820 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
822 was_visible = GTK_WIDGET_VISIBLE (widget);
823 menu_shell = GTK_MENU_SHELL (container);
824 menu_shell->children = g_list_remove (menu_shell->children, widget);
826 if (widget == menu_shell->active_menu_item)
828 gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
829 menu_shell->active_menu_item = NULL;
832 gtk_widget_unparent (widget);
834 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
835 * since that's what is needed by toplevels.
838 gtk_widget_queue_resize (GTK_WIDGET (container));
842 gtk_menu_shell_forall (GtkContainer *container,
843 gboolean include_internals,
844 GtkCallback callback,
845 gpointer callback_data)
847 GtkMenuShell *menu_shell;
851 g_return_if_fail (GTK_IS_MENU_SHELL (container));
852 g_return_if_fail (callback != NULL);
854 menu_shell = GTK_MENU_SHELL (container);
856 children = menu_shell->children;
859 child = children->data;
860 children = children->next;
862 (* callback) (child, callback_data);
868 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
870 if (menu_shell->active)
872 menu_shell->button = 0;
873 menu_shell->active = FALSE;
874 menu_shell->activate_time = 0;
876 if (menu_shell->active_menu_item)
878 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
879 menu_shell->active_menu_item = NULL;
882 if (menu_shell->have_grab)
884 menu_shell->have_grab = FALSE;
885 gtk_grab_remove (GTK_WIDGET (menu_shell));
887 if (menu_shell->have_xgrab)
889 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
891 menu_shell->have_xgrab = FALSE;
892 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
893 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
899 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
904 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
905 g_return_val_if_fail (child != NULL, FALSE);
907 parent = child->parent;
908 while (parent && GTK_IS_MENU_SHELL (parent))
910 if (parent == (GtkWidget*) menu_shell)
912 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
919 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
922 GtkWidget *menu_item;
924 menu_item = gtk_get_event_widget ((GdkEvent*) event);
926 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
927 menu_item = menu_item->parent;
929 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
935 /* Handlers for action signals */
938 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
939 GtkWidget *menu_item)
941 GtkMenuShellClass *class;
943 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
944 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
946 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
948 if (class->select_item &&
949 !(menu_shell->active &&
950 menu_shell->active_menu_item == menu_item))
951 class->select_item (menu_shell, menu_item);
954 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
955 GtkSubmenuPlacement placement);
958 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
959 GtkWidget *menu_item)
961 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
963 gtk_menu_shell_deselect (menu_shell);
965 if (!_gtk_menu_item_is_selectable (menu_item))
968 menu_shell->active_menu_item = menu_item;
969 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
970 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
973 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
974 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
975 gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
977 /* This allows the bizarre radio buttons-with-submenus-display-history
980 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
981 gtk_widget_activate (menu_shell->active_menu_item);
985 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
987 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
989 if (menu_shell->active_menu_item)
991 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
992 menu_shell->active_menu_item = NULL;
997 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
998 GtkWidget *menu_item,
999 gboolean force_deactivate)
1001 GSList *slist, *shells = NULL;
1002 gboolean deactivate = force_deactivate;
1004 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1005 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1008 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1010 g_object_ref (menu_shell);
1011 g_object_ref (menu_item);
1015 GtkMenuShell *parent_menu_shell = menu_shell;
1019 g_object_ref (parent_menu_shell);
1020 shells = g_slist_prepend (shells, parent_menu_shell);
1021 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1023 while (parent_menu_shell);
1024 shells = g_slist_reverse (shells);
1026 gtk_menu_shell_deactivate (menu_shell);
1028 /* flush the x-queue, so any grabs are removed and
1029 * the menu is actually taken down
1031 gdk_display_sync (gtk_widget_get_display (menu_item));
1034 gtk_widget_activate (menu_item);
1036 for (slist = shells; slist; slist = slist->next)
1038 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1039 g_object_unref (slist->data);
1041 g_slist_free (shells);
1043 g_object_unref (menu_shell);
1044 g_object_unref (menu_item);
1047 /* Distance should be +/- 1 */
1049 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1052 if (menu_shell->active_menu_item)
1054 GList *node = g_list_find (menu_shell->children,
1055 menu_shell->active_menu_item);
1056 GList *start_node = node;
1061 while (node != start_node &&
1062 (!node || !_gtk_menu_item_is_selectable (node->data)))
1065 node = menu_shell->children;
1073 while (node != start_node &&
1074 (!node || !_gtk_menu_item_is_selectable (node->data)))
1077 node = g_list_last (menu_shell->children);
1084 gtk_menu_shell_select_item (menu_shell, node->data);
1089 * gtk_menu_shell_select_first:
1090 * @menu_shell: a #GtkMenuShell
1091 * @search_sensitive: if %TRUE, search for the first selectable
1092 * menu item, otherwise select nothing if
1093 * the first item isn't sensitive. This
1094 * should be %FALSE if the menu is being
1095 * popped up initially.
1097 * Select the first visible or selectable child of the menu shell;
1098 * don't select tearoff items unless the only item is a tearoff
1104 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1105 gboolean search_sensitive)
1107 GtkWidget *to_select = NULL;
1110 tmp_list = menu_shell->children;
1113 GtkWidget *child = tmp_list->data;
1115 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1116 _gtk_menu_item_is_selectable (child))
1119 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1123 tmp_list = tmp_list->next;
1127 gtk_menu_shell_select_item (menu_shell, to_select);
1131 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1132 gboolean search_sensitive)
1134 GtkWidget *to_select = NULL;
1137 tmp_list = g_list_last (menu_shell->children);
1140 GtkWidget *child = tmp_list->data;
1142 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1143 _gtk_menu_item_is_selectable (child))
1146 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1150 tmp_list = tmp_list->prev;
1154 gtk_menu_shell_select_item (menu_shell, to_select);
1158 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1160 GtkMenuItem *menu_item;
1162 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1164 if (menu_item->submenu)
1166 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item));
1167 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1168 if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1176 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1177 GtkMenuDirectionType direction)
1179 GtkMenuShell *parent_menu_shell = NULL;
1180 gboolean had_selection;
1182 had_selection = menu_shell->active_menu_item != NULL;
1184 if (menu_shell->parent_menu_shell)
1185 parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1189 case GTK_MENU_DIR_PARENT:
1190 if (parent_menu_shell)
1192 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1193 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1194 gtk_menu_shell_deselect (menu_shell);
1197 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1198 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1200 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1201 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1204 /* If there is no parent and the submenu is in the opposite direction
1205 * to the menu, then make the PARENT direction wrap around to
1206 * the bottom of the submenu.
1208 else if (menu_shell->active_menu_item &&
1209 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1210 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1212 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1214 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1215 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1216 _gtk_menu_shell_select_last (submenu, TRUE);
1220 case GTK_MENU_DIR_CHILD:
1221 if (menu_shell->active_menu_item &&
1222 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1223 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1225 if (gtk_menu_shell_select_submenu_first (menu_shell))
1229 /* Try to find a menu running the opposite direction */
1230 while (parent_menu_shell &&
1231 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1232 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1234 GtkWidget *tmp_widget = parent_menu_shell->parent_menu_shell;
1237 parent_menu_shell = GTK_MENU_SHELL (tmp_widget);
1239 parent_menu_shell = NULL;
1242 if (parent_menu_shell)
1244 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1245 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1247 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1249 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1253 case GTK_MENU_DIR_PREV:
1254 gtk_menu_shell_move_selected (menu_shell, -1);
1255 if (!had_selection &&
1256 !menu_shell->active_menu_item &&
1257 menu_shell->children)
1258 _gtk_menu_shell_select_last (menu_shell, TRUE);
1260 case GTK_MENU_DIR_NEXT:
1261 gtk_menu_shell_move_selected (menu_shell, 1);
1262 if (!had_selection &&
1263 !menu_shell->active_menu_item &&
1264 menu_shell->children)
1265 gtk_menu_shell_select_first (menu_shell, TRUE);
1271 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1272 gboolean force_hide)
1274 if (menu_shell->active_menu_item &&
1275 _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1278 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1279 gtk_menu_shell_activate_item (menu_shell,
1280 menu_shell->active_menu_item,
1283 _gtk_menu_item_popup_submenu (menu_shell->active_menu_item);
1288 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1290 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1292 gtk_menu_shell_deselect (menu_shell);
1294 gtk_menu_shell_deactivate (menu_shell);
1295 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1299 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1300 GtkDirectionType dir)
1302 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1304 if (menu_shell->parent_menu_shell)
1305 menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1311 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1315 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1317 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1319 if (klass->get_popup_delay)
1321 return klass->get_popup_delay (menu_shell);
1326 GtkWidget *widget = GTK_WIDGET (menu_shell);
1328 g_object_get (gtk_widget_get_settings (widget),
1329 "gtk-menu-popup-delay", &popup_delay,
1337 * gtk_menu_shell_cancel:
1338 * @menu_shell: a #GtkMenuShell
1340 * Cancels the selection within the menu shell.
1345 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1347 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1349 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1352 static GtkMnemonicHash *
1353 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1356 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1358 if (!private->mnemonic_hash && create)
1359 private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1361 return private->mnemonic_hash;
1365 menu_shell_add_mnemonic_foreach (guint keyval,
1369 GtkKeyHash *key_hash = data;
1371 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1375 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1378 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1379 GtkWidget *widget = GTK_WIDGET (menu_shell);
1381 if (!private->key_hash && create && gtk_widget_has_screen (widget))
1383 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1384 GdkScreen *screen = gtk_widget_get_screen (widget);
1385 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1390 private->key_hash = _gtk_key_hash_new (keymap, NULL);
1392 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1393 menu_shell_add_mnemonic_foreach,
1397 return private->key_hash;
1401 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1403 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1405 if (private->key_hash)
1407 _gtk_key_hash_free (private->key_hash);
1408 private->key_hash = NULL;
1413 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1416 GtkMnemonicHash *mnemonic_hash;
1417 GtkKeyHash *key_hash;
1419 gboolean result = FALSE;
1421 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1425 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1429 entries = _gtk_key_hash_lookup (key_hash,
1430 event->hardware_keycode,
1432 gtk_accelerator_get_default_mod_mask (),
1436 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1437 GPOINTER_TO_UINT (entries->data));
1443 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1447 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1448 g_return_if_fail (GTK_IS_WIDGET (target));
1450 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1452 gtk_menu_shell_reset_key_hash (menu_shell);
1456 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1460 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1461 g_return_if_fail (GTK_IS_WIDGET (target));
1463 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1465 gtk_menu_shell_reset_key_hash (menu_shell);
1469 * gtk_menu_shell_get_take_focus:
1470 * @menu_shell: a #GtkMenuShell
1472 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1474 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1479 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1481 GtkMenuShellPrivate *priv;
1483 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1485 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1487 return priv->take_focus;
1491 * gtk_menu_shell_set_take_focus:
1492 * @menu_shell: a #GtkMenuShell
1493 * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1495 * If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1496 * focus so that it will receive all keyboard events which is needed to enable
1497 * keyboard navigation in menus.
1499 * Setting @take_focus to %FALSE is useful only for special applications
1500 * like virtual keyboard implementations which should not take keyboard
1503 * The @take_focus state of a menu or menu bar is automatically propagated
1504 * to submenus whenever a submenu is popped up, so you don't have to worry
1505 * about recursively setting it for your entire menu hierarchy. Only when
1506 * programmatically picking a submenu and popping it up manually, the
1507 * @take_focus property of the submenu needs to be set explicitely.
1509 * Note that setting it to %FALSE has side-effects:
1511 * If the focus is in some other app, it keeps the focus and keynav in
1512 * the menu doesn't work. Consequently, keynav on the menu will only
1513 * work if the focus is on some toplevel owned by the onscreen keyboard.
1515 * To avoid confusing the user, menus with @take_focus set to %FALSE
1516 * should not display mnemonics or accelerators, since it cannot be
1517 * guaranteed that they will work.
1519 * See also gdk_keyboard_grab()
1524 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1525 gboolean take_focus)
1527 GtkMenuShellPrivate *priv;
1529 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1531 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1533 if (priv->take_focus != take_focus)
1535 priv->take_focus = take_focus;
1536 g_object_notify (G_OBJECT (menu_shell), "take-focus");
1540 #define __GTK_MENU_SHELL_C__
1541 #include "gtkaliasdef.c"