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 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,
204 static GtkContainerClass *parent_class = NULL;
205 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
209 gtk_menu_shell_get_type (void)
211 static GType menu_shell_type = 0;
213 if (!menu_shell_type)
215 static const GTypeInfo menu_shell_info =
217 sizeof (GtkMenuShellClass),
218 NULL, /* base_init */
219 NULL, /* base_finalize */
220 (GClassInitFunc) gtk_menu_shell_class_init,
221 NULL, /* class_finalize */
222 NULL, /* class_data */
223 sizeof (GtkMenuShell),
225 (GInstanceInitFunc) gtk_menu_shell_init,
226 NULL, /* value_table */
230 g_type_register_static (GTK_TYPE_CONTAINER, g_intern_static_string ("GtkMenuShell"),
231 &menu_shell_info, G_TYPE_FLAG_ABSTRACT);
234 return menu_shell_type;
238 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
240 GObjectClass *object_class;
241 GtkWidgetClass *widget_class;
242 GtkContainerClass *container_class;
244 GtkBindingSet *binding_set;
246 object_class = (GObjectClass*) klass;
247 widget_class = (GtkWidgetClass*) klass;
248 container_class = (GtkContainerClass*) klass;
250 parent_class = g_type_class_peek_parent (klass);
252 object_class->set_property = gtk_menu_shell_set_property;
253 object_class->get_property = gtk_menu_shell_get_property;
254 object_class->finalize = gtk_menu_shell_finalize;
256 widget_class->realize = gtk_menu_shell_realize;
257 widget_class->button_press_event = gtk_menu_shell_button_press;
258 widget_class->button_release_event = gtk_menu_shell_button_release;
259 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
260 widget_class->key_press_event = gtk_menu_shell_key_press;
261 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
262 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
263 widget_class->screen_changed = gtk_menu_shell_screen_changed;
265 container_class->add = gtk_menu_shell_add;
266 container_class->remove = gtk_menu_shell_remove;
267 container_class->forall = gtk_menu_shell_forall;
268 container_class->child_type = gtk_menu_shell_child_type;
270 klass->submenu_placement = GTK_TOP_BOTTOM;
271 klass->deactivate = gtk_real_menu_shell_deactivate;
272 klass->selection_done = NULL;
273 klass->move_current = gtk_real_menu_shell_move_current;
274 klass->activate_current = gtk_real_menu_shell_activate_current;
275 klass->cancel = gtk_real_menu_shell_cancel;
276 klass->select_item = gtk_menu_shell_real_select_item;
277 klass->insert = gtk_menu_shell_real_insert;
279 menu_shell_signals[DEACTIVATE] =
280 g_signal_new ("deactivate",
281 G_OBJECT_CLASS_TYPE (object_class),
283 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
285 _gtk_marshal_VOID__VOID,
287 menu_shell_signals[SELECTION_DONE] =
288 g_signal_new ("selection-done",
289 G_OBJECT_CLASS_TYPE (object_class),
291 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
293 _gtk_marshal_VOID__VOID,
295 menu_shell_signals[MOVE_CURRENT] =
296 g_signal_new ("move_current",
297 G_OBJECT_CLASS_TYPE (object_class),
298 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
299 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
301 _gtk_marshal_VOID__ENUM,
303 GTK_TYPE_MENU_DIRECTION_TYPE);
304 menu_shell_signals[ACTIVATE_CURRENT] =
305 g_signal_new ("activate_current",
306 G_OBJECT_CLASS_TYPE (object_class),
307 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
308 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
310 _gtk_marshal_VOID__BOOLEAN,
313 menu_shell_signals[CANCEL] =
314 g_signal_new ("cancel",
315 G_OBJECT_CLASS_TYPE (object_class),
316 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
317 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
319 _gtk_marshal_VOID__VOID,
321 menu_shell_signals[CYCLE_FOCUS] =
322 _gtk_binding_signal_new ("cycle_focus",
323 G_OBJECT_CLASS_TYPE (object_class),
324 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
325 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
327 _gtk_marshal_VOID__ENUM,
329 GTK_TYPE_DIRECTION_TYPE);
332 binding_set = gtk_binding_set_by_class (klass);
333 gtk_binding_entry_add_signal (binding_set,
336 gtk_binding_entry_add_signal (binding_set,
338 "activate_current", 1,
341 gtk_binding_entry_add_signal (binding_set,
343 "activate_current", 1,
346 gtk_binding_entry_add_signal (binding_set,
348 "activate_current", 1,
351 gtk_binding_entry_add_signal (binding_set,
353 "activate_current", 1,
356 gtk_binding_entry_add_signal (binding_set,
359 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
360 gtk_binding_entry_add_signal (binding_set,
361 GDK_F10, GDK_SHIFT_MASK,
363 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
366 * GtkMenuShell:take-focus:
368 * A boolean that determines whether the menu and its submenus grab the
369 * keyboard focus. See gtk_menu_shell_set_take_focus() and
370 * gtk_menu_shell_get_take_focus().
374 g_object_class_install_property (object_class,
376 g_param_spec_boolean ("take-focus",
378 P_("A boolean that determines whether the menu grabs the keyboard focus"),
380 GTK_PARAM_READWRITE));
382 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
386 gtk_menu_shell_child_type (GtkContainer *container)
388 return GTK_TYPE_MENU_ITEM;
392 gtk_menu_shell_init (GtkMenuShell *menu_shell)
394 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
396 menu_shell->children = NULL;
397 menu_shell->active_menu_item = NULL;
398 menu_shell->parent_menu_shell = NULL;
399 menu_shell->active = FALSE;
400 menu_shell->have_grab = FALSE;
401 menu_shell->have_xgrab = FALSE;
402 menu_shell->button = 0;
403 menu_shell->activate_time = 0;
405 priv->mnemonic_hash = NULL;
406 priv->key_hash = NULL;
407 priv->take_focus = TRUE;
411 gtk_menu_shell_set_property (GObject *object,
416 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
420 case PROP_TAKE_FOCUS:
421 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
424 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430 gtk_menu_shell_get_property (GObject *object,
435 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
439 case PROP_TAKE_FOCUS:
440 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_menu_shell_finalize (GObject *object)
451 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
452 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
454 if (priv->mnemonic_hash)
455 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
457 _gtk_key_hash_free (priv->key_hash);
459 G_OBJECT_CLASS (parent_class)->finalize (object);
464 gtk_menu_shell_append (GtkMenuShell *menu_shell,
467 gtk_menu_shell_insert (menu_shell, child, -1);
471 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
474 gtk_menu_shell_insert (menu_shell, child, 0);
478 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
482 GtkMenuShellClass *class;
484 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
485 g_return_if_fail (GTK_IS_MENU_ITEM (child));
487 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
490 class->insert (menu_shell, child, position);
494 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
498 menu_shell->children = g_list_insert (menu_shell->children, child, position);
500 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
504 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
506 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
508 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
512 gtk_menu_shell_realize (GtkWidget *widget)
514 GdkWindowAttr attributes;
515 gint attributes_mask;
517 g_return_if_fail (GTK_IS_MENU_SHELL (widget));
519 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
521 attributes.x = widget->allocation.x;
522 attributes.y = widget->allocation.y;
523 attributes.width = widget->allocation.width;
524 attributes.height = widget->allocation.height;
525 attributes.window_type = GDK_WINDOW_CHILD;
526 attributes.wclass = GDK_INPUT_OUTPUT;
527 attributes.visual = gtk_widget_get_visual (widget);
528 attributes.colormap = gtk_widget_get_colormap (widget);
529 attributes.event_mask = gtk_widget_get_events (widget);
530 attributes.event_mask |= (GDK_EXPOSURE_MASK |
531 GDK_BUTTON_PRESS_MASK |
532 GDK_BUTTON_RELEASE_MASK |
534 GDK_ENTER_NOTIFY_MASK |
535 GDK_LEAVE_NOTIFY_MASK);
537 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
538 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
539 gdk_window_set_user_data (widget->window, widget);
541 widget->style = gtk_style_attach (widget->style, widget->window);
542 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
546 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
548 if (!menu_shell->active)
550 gtk_grab_add (GTK_WIDGET (menu_shell));
551 menu_shell->have_grab = TRUE;
552 menu_shell->active = TRUE;
557 gtk_menu_shell_button_press (GtkWidget *widget,
558 GdkEventButton *event)
560 GtkMenuShell *menu_shell;
561 GtkWidget *menu_item;
563 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
564 g_return_val_if_fail (event != NULL, FALSE);
566 if (event->type != GDK_BUTTON_PRESS)
569 menu_shell = GTK_MENU_SHELL (widget);
571 if (menu_shell->parent_menu_shell)
573 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
575 else if (!menu_shell->active || !menu_shell->button)
577 _gtk_menu_shell_activate (menu_shell);
579 menu_shell->button = event->button;
581 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
583 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
585 if ((menu_item->parent == widget) &&
586 (menu_item != menu_shell->active_menu_item))
588 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
590 menu_shell->activate_time = event->time;
593 gtk_menu_shell_select_item (menu_shell, menu_item);
599 widget = gtk_get_event_widget ((GdkEvent*) event);
600 if (widget == GTK_WIDGET (menu_shell))
602 gtk_menu_shell_deactivate (menu_shell);
603 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
611 gtk_menu_shell_grab_broken (GtkWidget *widget,
612 GdkEventGrabBroken *event)
614 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
616 if (menu_shell->have_xgrab && event->grab_window == NULL)
618 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
621 gtk_menu_shell_deselect (menu_shell);
623 gtk_menu_shell_deactivate (menu_shell);
624 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
631 gtk_menu_shell_button_release (GtkWidget *widget,
632 GdkEventButton *event)
634 GtkMenuShell *menu_shell;
635 GtkWidget *menu_item;
638 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
639 g_return_val_if_fail (event != NULL, FALSE);
641 menu_shell = GTK_MENU_SHELL (widget);
642 if (menu_shell->active)
644 if (menu_shell->button && (event->button != menu_shell->button))
646 menu_shell->button = 0;
647 if (menu_shell->parent_menu_shell)
648 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
651 menu_shell->button = 0;
652 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
656 if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
658 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
659 _gtk_menu_item_is_selectable (menu_item))
661 if (GTK_MENU_ITEM (menu_item)->submenu == NULL)
663 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
666 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
668 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
672 else if (menu_item &&
673 !_gtk_menu_item_is_selectable (menu_item) &&
674 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
678 else if (menu_shell->parent_menu_shell)
680 menu_shell->active = TRUE;
681 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
685 /* If we ended up on an item with a submenu, leave the menu up.
687 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
688 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
693 else /* a very fast press-release */
695 /* We only ever want to prevent deactivation on the first
696 * press/release. Setting the time to zero is a bit of a
697 * hack, since we could be being triggered in the first
698 * few fractions of a second after a server time wraparound.
699 * the chances of that happening are ~1/10^6, without
700 * serious harm if we lose.
702 menu_shell->activate_time = 0;
708 gtk_menu_shell_deactivate (menu_shell);
709 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
717 gtk_menu_shell_key_press (GtkWidget *widget,
720 GtkMenuShell *menu_shell;
722 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
723 g_return_val_if_fail (event != NULL, FALSE);
725 menu_shell = GTK_MENU_SHELL (widget);
727 if (!menu_shell->active_menu_item && menu_shell->parent_menu_shell)
728 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
730 if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
733 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
737 gtk_menu_shell_enter_notify (GtkWidget *widget,
738 GdkEventCrossing *event)
740 GtkMenuShell *menu_shell;
741 GtkWidget *menu_item;
743 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
744 g_return_val_if_fail (event != NULL, FALSE);
746 menu_shell = GTK_MENU_SHELL (widget);
748 if (menu_shell->active)
750 menu_item = gtk_get_event_widget ((GdkEvent*) event);
753 (GTK_IS_MENU_ITEM (menu_item) &&
754 !_gtk_menu_item_is_selectable (menu_item)))
757 if ((menu_item->parent == widget) &&
758 (menu_shell->active_menu_item != menu_item) &&
759 GTK_IS_MENU_ITEM (menu_item))
761 if (menu_shell->ignore_enter)
764 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
765 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
767 gtk_menu_shell_select_item (menu_shell, menu_item);
770 else if (menu_shell->parent_menu_shell)
772 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
780 gtk_menu_shell_leave_notify (GtkWidget *widget,
781 GdkEventCrossing *event)
783 GtkMenuShell *menu_shell;
784 GtkMenuItem *menu_item;
785 GtkWidget *event_widget;
787 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
788 g_return_val_if_fail (event != NULL, FALSE);
790 if (GTK_WIDGET_VISIBLE (widget))
792 menu_shell = GTK_MENU_SHELL (widget);
793 event_widget = gtk_get_event_widget ((GdkEvent*) event);
795 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
798 menu_item = GTK_MENU_ITEM (event_widget);
800 if (!_gtk_menu_item_is_selectable (event_widget))
803 if ((menu_shell->active_menu_item == event_widget) &&
804 (menu_item->submenu == NULL))
806 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
807 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
809 gtk_menu_shell_deselect (menu_shell);
812 else if (menu_shell->parent_menu_shell)
814 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
822 gtk_menu_shell_screen_changed (GtkWidget *widget,
823 GdkScreen *previous_screen)
825 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
829 gtk_menu_shell_add (GtkContainer *container,
832 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
836 gtk_menu_shell_remove (GtkContainer *container,
839 GtkMenuShell *menu_shell;
842 g_return_if_fail (GTK_IS_MENU_SHELL (container));
843 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
845 was_visible = GTK_WIDGET_VISIBLE (widget);
846 menu_shell = GTK_MENU_SHELL (container);
847 menu_shell->children = g_list_remove (menu_shell->children, widget);
849 if (widget == menu_shell->active_menu_item)
851 gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
852 menu_shell->active_menu_item = NULL;
855 gtk_widget_unparent (widget);
857 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
858 * since that's what is needed by toplevels.
861 gtk_widget_queue_resize (GTK_WIDGET (container));
865 gtk_menu_shell_forall (GtkContainer *container,
866 gboolean include_internals,
867 GtkCallback callback,
868 gpointer callback_data)
870 GtkMenuShell *menu_shell;
874 g_return_if_fail (GTK_IS_MENU_SHELL (container));
875 g_return_if_fail (callback != NULL);
877 menu_shell = GTK_MENU_SHELL (container);
879 children = menu_shell->children;
882 child = children->data;
883 children = children->next;
885 (* callback) (child, callback_data);
891 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
893 if (menu_shell->active)
895 menu_shell->button = 0;
896 menu_shell->active = FALSE;
897 menu_shell->activate_time = 0;
899 if (menu_shell->active_menu_item)
901 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
902 menu_shell->active_menu_item = NULL;
905 if (menu_shell->have_grab)
907 menu_shell->have_grab = FALSE;
908 gtk_grab_remove (GTK_WIDGET (menu_shell));
910 if (menu_shell->have_xgrab)
912 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
914 menu_shell->have_xgrab = FALSE;
915 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
916 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
922 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
927 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
928 g_return_val_if_fail (child != NULL, FALSE);
930 parent = child->parent;
931 while (parent && GTK_IS_MENU_SHELL (parent))
933 if (parent == (GtkWidget*) menu_shell)
935 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
942 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
945 GtkWidget *menu_item;
947 menu_item = gtk_get_event_widget ((GdkEvent*) event);
949 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
950 menu_item = menu_item->parent;
952 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
958 /* Handlers for action signals */
961 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
962 GtkWidget *menu_item)
964 GtkMenuShellClass *class;
966 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
967 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
969 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
971 if (class->select_item &&
972 !(menu_shell->active &&
973 menu_shell->active_menu_item == menu_item))
974 class->select_item (menu_shell, menu_item);
977 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
978 GtkSubmenuPlacement placement);
981 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
982 GtkWidget *menu_item)
984 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
986 gtk_menu_shell_deselect (menu_shell);
988 if (!_gtk_menu_item_is_selectable (menu_item))
991 menu_shell->active_menu_item = menu_item;
992 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
993 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
996 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
997 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
998 gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1000 /* This allows the bizarre radio buttons-with-submenus-display-history
1003 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1004 gtk_widget_activate (menu_shell->active_menu_item);
1008 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1010 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1012 if (menu_shell->active_menu_item)
1014 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1015 menu_shell->active_menu_item = NULL;
1020 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
1021 GtkWidget *menu_item,
1022 gboolean force_deactivate)
1024 GSList *slist, *shells = NULL;
1025 gboolean deactivate = force_deactivate;
1027 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1028 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1031 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1033 g_object_ref (menu_shell);
1034 g_object_ref (menu_item);
1038 GtkMenuShell *parent_menu_shell = menu_shell;
1042 g_object_ref (parent_menu_shell);
1043 shells = g_slist_prepend (shells, parent_menu_shell);
1044 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1046 while (parent_menu_shell);
1047 shells = g_slist_reverse (shells);
1049 gtk_menu_shell_deactivate (menu_shell);
1051 /* flush the x-queue, so any grabs are removed and
1052 * the menu is actually taken down
1054 gdk_display_sync (gtk_widget_get_display (menu_item));
1057 gtk_widget_activate (menu_item);
1059 for (slist = shells; slist; slist = slist->next)
1061 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1062 g_object_unref (slist->data);
1064 g_slist_free (shells);
1066 g_object_unref (menu_shell);
1067 g_object_unref (menu_item);
1070 /* Distance should be +/- 1 */
1072 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1075 if (menu_shell->active_menu_item)
1077 GList *node = g_list_find (menu_shell->children,
1078 menu_shell->active_menu_item);
1079 GList *start_node = node;
1084 while (node != start_node &&
1085 (!node || !_gtk_menu_item_is_selectable (node->data)))
1088 node = menu_shell->children;
1096 while (node != start_node &&
1097 (!node || !_gtk_menu_item_is_selectable (node->data)))
1100 node = g_list_last (menu_shell->children);
1107 gtk_menu_shell_select_item (menu_shell, node->data);
1112 * gtk_menu_shell_select_first:
1113 * @menu_shell: a #GtkMenuShell
1114 * @search_sensitive: if %TRUE, search for the first selectable
1115 * menu item, otherwise select nothing if
1116 * the first item isn't sensitive. This
1117 * should be %FALSE if the menu is being
1118 * popped up initially.
1120 * Select the first visible or selectable child of the menu shell;
1121 * don't select tearoff items unless the only item is a tearoff
1127 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1128 gboolean search_sensitive)
1130 GtkWidget *to_select = NULL;
1133 tmp_list = menu_shell->children;
1136 GtkWidget *child = tmp_list->data;
1138 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1139 _gtk_menu_item_is_selectable (child))
1142 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1146 tmp_list = tmp_list->next;
1150 gtk_menu_shell_select_item (menu_shell, to_select);
1154 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1155 gboolean search_sensitive)
1157 GtkWidget *to_select = NULL;
1160 tmp_list = g_list_last (menu_shell->children);
1163 GtkWidget *child = tmp_list->data;
1165 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1166 _gtk_menu_item_is_selectable (child))
1169 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1173 tmp_list = tmp_list->prev;
1177 gtk_menu_shell_select_item (menu_shell, to_select);
1181 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1183 GtkMenuItem *menu_item;
1185 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1187 if (menu_item->submenu)
1189 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item));
1190 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1191 if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1199 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1200 GtkMenuDirectionType direction)
1202 GtkMenuShell *parent_menu_shell = NULL;
1203 gboolean had_selection;
1205 had_selection = menu_shell->active_menu_item != NULL;
1207 if (menu_shell->parent_menu_shell)
1208 parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1212 case GTK_MENU_DIR_PARENT:
1213 if (parent_menu_shell)
1215 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1216 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1217 gtk_menu_shell_deselect (menu_shell);
1220 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1221 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1223 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1224 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1227 /* If there is no parent and the submenu is in the opposite direction
1228 * to the menu, then make the PARENT direction wrap around to
1229 * the bottom of the submenu.
1231 else if (menu_shell->active_menu_item &&
1232 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1233 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1235 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1237 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1238 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1239 _gtk_menu_shell_select_last (submenu, TRUE);
1243 case GTK_MENU_DIR_CHILD:
1244 if (menu_shell->active_menu_item &&
1245 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1246 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1248 if (gtk_menu_shell_select_submenu_first (menu_shell))
1252 /* Try to find a menu running the opposite direction */
1253 while (parent_menu_shell &&
1254 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1255 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1257 GtkWidget *tmp_widget = parent_menu_shell->parent_menu_shell;
1260 parent_menu_shell = GTK_MENU_SHELL (tmp_widget);
1262 parent_menu_shell = NULL;
1265 if (parent_menu_shell)
1267 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1268 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1270 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1272 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1276 case GTK_MENU_DIR_PREV:
1277 gtk_menu_shell_move_selected (menu_shell, -1);
1278 if (!had_selection &&
1279 !menu_shell->active_menu_item &&
1280 menu_shell->children)
1281 _gtk_menu_shell_select_last (menu_shell, TRUE);
1283 case GTK_MENU_DIR_NEXT:
1284 gtk_menu_shell_move_selected (menu_shell, 1);
1285 if (!had_selection &&
1286 !menu_shell->active_menu_item &&
1287 menu_shell->children)
1288 gtk_menu_shell_select_first (menu_shell, TRUE);
1294 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1295 gboolean force_hide)
1297 if (menu_shell->active_menu_item &&
1298 _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1301 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1302 gtk_menu_shell_activate_item (menu_shell,
1303 menu_shell->active_menu_item,
1306 _gtk_menu_item_popup_submenu (menu_shell->active_menu_item);
1311 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1313 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1315 gtk_menu_shell_deselect (menu_shell);
1317 gtk_menu_shell_deactivate (menu_shell);
1318 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1322 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1323 GtkDirectionType dir)
1325 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1327 if (menu_shell->parent_menu_shell)
1328 menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1334 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1338 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1340 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1342 if (klass->get_popup_delay)
1344 return klass->get_popup_delay (menu_shell);
1349 GtkWidget *widget = GTK_WIDGET (menu_shell);
1351 g_object_get (gtk_widget_get_settings (widget),
1352 "gtk-menu-popup-delay", &popup_delay,
1360 * gtk_menu_shell_cancel:
1361 * @menu_shell: a #GtkMenuShell
1363 * Cancels the selection within the menu shell.
1368 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1370 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1372 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1375 static GtkMnemonicHash *
1376 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1379 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1381 if (!private->mnemonic_hash && create)
1382 private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1384 return private->mnemonic_hash;
1388 menu_shell_add_mnemonic_foreach (guint keyval,
1392 GtkKeyHash *key_hash = data;
1394 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1398 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1401 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1402 GtkWidget *widget = GTK_WIDGET (menu_shell);
1404 if (!private->key_hash && create && gtk_widget_has_screen (widget))
1406 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1407 GdkScreen *screen = gtk_widget_get_screen (widget);
1408 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1413 private->key_hash = _gtk_key_hash_new (keymap, NULL);
1415 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1416 menu_shell_add_mnemonic_foreach,
1420 return private->key_hash;
1424 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1426 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1428 if (private->key_hash)
1430 _gtk_key_hash_free (private->key_hash);
1431 private->key_hash = NULL;
1436 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1439 GtkMnemonicHash *mnemonic_hash;
1440 GtkKeyHash *key_hash;
1442 gboolean result = FALSE;
1444 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1448 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1452 entries = _gtk_key_hash_lookup (key_hash,
1453 event->hardware_keycode,
1455 gtk_accelerator_get_default_mod_mask (),
1459 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1460 GPOINTER_TO_UINT (entries->data));
1466 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1470 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1471 g_return_if_fail (GTK_IS_WIDGET (target));
1473 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1475 gtk_menu_shell_reset_key_hash (menu_shell);
1479 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1483 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1484 g_return_if_fail (GTK_IS_WIDGET (target));
1486 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1488 gtk_menu_shell_reset_key_hash (menu_shell);
1492 * gtk_menu_shell_get_take_focus:
1493 * @menu_shell: a #GtkMenuShell
1495 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1497 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1502 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1504 GtkMenuShellPrivate *priv;
1506 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1508 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1510 return priv->take_focus;
1514 * gtk_menu_shell_set_take_focus:
1515 * @menu_shell: a #GtkMenuShell
1516 * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1518 * If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1519 * focus so that it will receive all keyboard events which is needed to enable
1520 * keyboard navigation in menus.
1522 * Setting @take_focus to %FALSE is useful only for special applications
1523 * like virtual keyboard implementations which should not take keyboard
1526 * The @take_focus state of a menu or menu bar is automatically propagated
1527 * to submenus whenever a submenu is popped up, so you don't have to worry
1528 * about recursively setting it for your entire menu hierarchy. Only when
1529 * programmatically picking a submenu and popping it up manually, the
1530 * @take_focus property of the submenu needs to be set explicitely.
1532 * Note that setting it to %FALSE has side-effects:
1534 * If the focus is in some other app, it keeps the focus and keynav in
1535 * the menu doesn't work. Consequently, keynav on the menu will only
1536 * work if the focus is on some toplevel owned by the onscreen keyboard.
1538 * To avoid confusing the user, menus with @take_focus set to %FALSE
1539 * should not display mnemonics or accelerators, since it cannot be
1540 * guaranteed that they will work.
1542 * See also gdk_keyboard_grab()
1547 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1548 gboolean take_focus)
1550 GtkMenuShellPrivate *priv;
1552 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1554 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1556 if (priv->take_focus != take_focus)
1558 priv->take_focus = take_focus;
1559 g_object_notify (G_OBJECT (menu_shell), "take-focus");
1563 #define __GTK_MENU_SHELL_C__
1564 #include "gtkaliasdef.c"