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_set_property (GObject *object,
149
static void gtk_menu_shell_get_property (GObject *object,
153
static void gtk_menu_shell_realize (GtkWidget *widget);
154
static void gtk_menu_shell_finalize (GObject *object);
155
static gint gtk_menu_shell_button_press (GtkWidget *widget,
156
GdkEventButton *event);
157
static gint gtk_menu_shell_button_release (GtkWidget *widget,
158
GdkEventButton *event);
159
static gint gtk_menu_shell_key_press (GtkWidget *widget,
161
static gint gtk_menu_shell_enter_notify (GtkWidget *widget,
162
GdkEventCrossing *event);
163
static gint gtk_menu_shell_leave_notify (GtkWidget *widget,
164
GdkEventCrossing *event);
165
static void gtk_menu_shell_screen_changed (GtkWidget *widget,
166
GdkScreen *previous_screen);
167
static gboolean gtk_menu_shell_grab_broken (GtkWidget *widget,
168
GdkEventGrabBroken *event);
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 guint menu_shell_signals[LAST_SIGNAL] = { 0 };
204
G_DEFINE_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
207
gtk_menu_shell_class_init (GtkMenuShellClass *klass)
209
GObjectClass *object_class;
210
GtkWidgetClass *widget_class;
211
GtkContainerClass *container_class;
213
GtkBindingSet *binding_set;
215
object_class = (GObjectClass*) klass;
216
widget_class = (GtkWidgetClass*) klass;
217
container_class = (GtkContainerClass*) klass;
219
object_class->set_property = gtk_menu_shell_set_property;
220
object_class->get_property = gtk_menu_shell_get_property;
221
object_class->finalize = gtk_menu_shell_finalize;
223
widget_class->realize = gtk_menu_shell_realize;
224
widget_class->button_press_event = gtk_menu_shell_button_press;
225
widget_class->button_release_event = gtk_menu_shell_button_release;
226
widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
227
widget_class->key_press_event = gtk_menu_shell_key_press;
228
widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
229
widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
230
widget_class->screen_changed = gtk_menu_shell_screen_changed;
232
container_class->add = gtk_menu_shell_add;
233
container_class->remove = gtk_menu_shell_remove;
234
container_class->forall = gtk_menu_shell_forall;
235
container_class->child_type = gtk_menu_shell_child_type;
237
klass->submenu_placement = GTK_TOP_BOTTOM;
238
klass->deactivate = gtk_real_menu_shell_deactivate;
239
klass->selection_done = NULL;
240
klass->move_current = gtk_real_menu_shell_move_current;
241
klass->activate_current = gtk_real_menu_shell_activate_current;
242
klass->cancel = gtk_real_menu_shell_cancel;
243
klass->select_item = gtk_menu_shell_real_select_item;
244
klass->insert = gtk_menu_shell_real_insert;
246
menu_shell_signals[DEACTIVATE] =
247
g_signal_new (I_("deactivate"),
248
G_OBJECT_CLASS_TYPE (object_class),
250
G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
252
_gtk_marshal_VOID__VOID,
254
menu_shell_signals[SELECTION_DONE] =
255
g_signal_new (I_("selection-done"),
256
G_OBJECT_CLASS_TYPE (object_class),
258
G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
260
_gtk_marshal_VOID__VOID,
262
menu_shell_signals[MOVE_CURRENT] =
263
g_signal_new (I_("move_current"),
264
G_OBJECT_CLASS_TYPE (object_class),
265
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
266
G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
268
_gtk_marshal_VOID__ENUM,
270
GTK_TYPE_MENU_DIRECTION_TYPE);
271
menu_shell_signals[ACTIVATE_CURRENT] =
272
g_signal_new (I_("activate_current"),
273
G_OBJECT_CLASS_TYPE (object_class),
274
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
275
G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
277
_gtk_marshal_VOID__BOOLEAN,
280
menu_shell_signals[CANCEL] =
281
g_signal_new (I_("cancel"),
282
G_OBJECT_CLASS_TYPE (object_class),
283
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
284
G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
286
_gtk_marshal_VOID__VOID,
288
menu_shell_signals[CYCLE_FOCUS] =
289
_gtk_binding_signal_new (I_("cycle_focus"),
290
G_OBJECT_CLASS_TYPE (object_class),
291
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
292
G_CALLBACK (gtk_real_menu_shell_cycle_focus),
294
_gtk_marshal_VOID__ENUM,
296
GTK_TYPE_DIRECTION_TYPE);
299
binding_set = gtk_binding_set_by_class (klass);
300
gtk_binding_entry_add_signal (binding_set,
303
gtk_binding_entry_add_signal (binding_set,
305
"activate_current", 1,
308
gtk_binding_entry_add_signal (binding_set,
310
"activate_current", 1,
313
gtk_binding_entry_add_signal (binding_set,
315
"activate_current", 1,
318
gtk_binding_entry_add_signal (binding_set,
320
"activate_current", 1,
323
gtk_binding_entry_add_signal (binding_set,
326
GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
327
gtk_binding_entry_add_signal (binding_set,
328
GDK_F10, GDK_SHIFT_MASK,
330
GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
333
* GtkMenuShell:take-focus:
335
* A boolean that determines whether the menu and its submenus grab the
336
* keyboard focus. See gtk_menu_shell_set_take_focus() and
337
* gtk_menu_shell_get_take_focus().
341
g_object_class_install_property (object_class,
343
g_param_spec_boolean ("take-focus",
345
P_("A boolean that determines whether the menu grabs the keyboard focus"),
347
GTK_PARAM_READWRITE));
349
g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
353
gtk_menu_shell_child_type (GtkContainer *container)
355
return GTK_TYPE_MENU_ITEM;
359
gtk_menu_shell_init (GtkMenuShell *menu_shell)
361
GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
363
menu_shell->children = NULL;
364
menu_shell->active_menu_item = NULL;
365
menu_shell->parent_menu_shell = NULL;
366
menu_shell->active = FALSE;
367
menu_shell->have_grab = FALSE;
368
menu_shell->have_xgrab = FALSE;
369
menu_shell->button = 0;
370
menu_shell->activate_time = 0;
372
priv->mnemonic_hash = NULL;
373
priv->key_hash = NULL;
374
priv->take_focus = TRUE;
378
gtk_menu_shell_set_property (GObject *object,
383
GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
387
case PROP_TAKE_FOCUS:
388
gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
391
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397
gtk_menu_shell_get_property (GObject *object,
402
GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
406
case PROP_TAKE_FOCUS:
407
g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
410
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416
gtk_menu_shell_finalize (GObject *object)
418
GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
419
GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
421
if (priv->mnemonic_hash)
422
_gtk_mnemonic_hash_free (priv->mnemonic_hash);
424
_gtk_key_hash_free (priv->key_hash);
426
G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
431
gtk_menu_shell_append (GtkMenuShell *menu_shell,
434
gtk_menu_shell_insert (menu_shell, child, -1);
438
gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
441
gtk_menu_shell_insert (menu_shell, child, 0);
445
gtk_menu_shell_insert (GtkMenuShell *menu_shell,
449
GtkMenuShellClass *class;
451
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
452
g_return_if_fail (GTK_IS_MENU_ITEM (child));
454
class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
457
class->insert (menu_shell, child, position);
461
gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
465
menu_shell->children = g_list_insert (menu_shell->children, child, position);
467
gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
471
gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
473
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
475
g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
479
gtk_menu_shell_realize (GtkWidget *widget)
481
GdkWindowAttr attributes;
482
gint attributes_mask;
484
g_return_if_fail (GTK_IS_MENU_SHELL (widget));
486
GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
488
attributes.x = widget->allocation.x;
489
attributes.y = widget->allocation.y;
490
attributes.width = widget->allocation.width;
491
attributes.height = widget->allocation.height;
492
attributes.window_type = GDK_WINDOW_CHILD;
493
attributes.wclass = GDK_INPUT_OUTPUT;
494
attributes.visual = gtk_widget_get_visual (widget);
495
attributes.colormap = gtk_widget_get_colormap (widget);
496
attributes.event_mask = gtk_widget_get_events (widget);
497
attributes.event_mask |= (GDK_EXPOSURE_MASK |
498
GDK_BUTTON_PRESS_MASK |
499
GDK_BUTTON_RELEASE_MASK |
501
GDK_ENTER_NOTIFY_MASK |
502
GDK_LEAVE_NOTIFY_MASK);
504
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
505
widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
506
gdk_window_set_user_data (widget->window, widget);
508
widget->style = gtk_style_attach (widget->style, widget->window);
509
gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
513
_gtk_menu_shell_activate (GtkMenuShell *menu_shell)
515
if (!menu_shell->active)
517
gtk_grab_add (GTK_WIDGET (menu_shell));
518
menu_shell->have_grab = TRUE;
519
menu_shell->active = TRUE;
524
gtk_menu_shell_button_press (GtkWidget *widget,
525
GdkEventButton *event)
527
GtkMenuShell *menu_shell;
528
GtkWidget *menu_item;
530
g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
531
g_return_val_if_fail (event != NULL, FALSE);
533
if (event->type != GDK_BUTTON_PRESS)
536
menu_shell = GTK_MENU_SHELL (widget);
538
if (menu_shell->parent_menu_shell)
540
return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
542
else if (!menu_shell->active || !menu_shell->button)
544
_gtk_menu_shell_activate (menu_shell);
546
menu_shell->button = event->button;
548
menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
550
if (menu_item && _gtk_menu_item_is_selectable (menu_item))
552
if ((menu_item->parent == widget) &&
553
(menu_item != menu_shell->active_menu_item))
555
if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
557
menu_shell->activate_time = event->time;
560
gtk_menu_shell_select_item (menu_shell, menu_item);
566
widget = gtk_get_event_widget ((GdkEvent*) event);
567
if (widget == GTK_WIDGET (menu_shell))
569
gtk_menu_shell_deactivate (menu_shell);
570
g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
578
gtk_menu_shell_grab_broken (GtkWidget *widget,
579
GdkEventGrabBroken *event)
581
GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
583
if (menu_shell->have_xgrab && event->grab_window == NULL)
585
/* Unset the active menu item so gtk_menu_popdown() doesn't see it.
588
gtk_menu_shell_deselect (menu_shell);
590
gtk_menu_shell_deactivate (menu_shell);
591
g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
598
gtk_menu_shell_button_release (GtkWidget *widget,
599
GdkEventButton *event)
601
GtkMenuShell *menu_shell;
602
GtkWidget *menu_item;
605
g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
606
g_return_val_if_fail (event != NULL, FALSE);
608
menu_shell = GTK_MENU_SHELL (widget);
609
if (menu_shell->active)
611
if (menu_shell->button && (event->button != menu_shell->button))
613
menu_shell->button = 0;
614
if (menu_shell->parent_menu_shell)
615
return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
618
menu_shell->button = 0;
619
menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
623
if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
625
if (menu_item && (menu_shell->active_menu_item == menu_item) &&
626
_gtk_menu_item_is_selectable (menu_item))
628
if (GTK_MENU_ITEM (menu_item)->submenu == NULL)
630
gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
633
else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
635
gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
639
else if (menu_item &&
640
!_gtk_menu_item_is_selectable (menu_item) &&
641
GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
645
else if (menu_shell->parent_menu_shell)
647
menu_shell->active = TRUE;
648
gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
652
/* If we ended up on an item with a submenu, leave the menu up.
654
if (menu_item && (menu_shell->active_menu_item == menu_item) &&
655
GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
660
else /* a very fast press-release */
662
/* We only ever want to prevent deactivation on the first
663
* press/release. Setting the time to zero is a bit of a
664
* hack, since we could be being triggered in the first
665
* few fractions of a second after a server time wraparound.
666
* the chances of that happening are ~1/10^6, without
667
* serious harm if we lose.
669
menu_shell->activate_time = 0;
675
gtk_menu_shell_deactivate (menu_shell);
676
g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
684
gtk_menu_shell_key_press (GtkWidget *widget,
687
GtkMenuShell *menu_shell;
689
g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
690
g_return_val_if_fail (event != NULL, FALSE);
692
menu_shell = GTK_MENU_SHELL (widget);
694
if (!menu_shell->active_menu_item && menu_shell->parent_menu_shell)
695
return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
697
if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
700
return gtk_menu_shell_activate_mnemonic (menu_shell, event);
704
gtk_menu_shell_enter_notify (GtkWidget *widget,
705
GdkEventCrossing *event)
707
GtkMenuShell *menu_shell;
708
GtkWidget *menu_item;
710
g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
711
g_return_val_if_fail (event != NULL, FALSE);
713
menu_shell = GTK_MENU_SHELL (widget);
715
if (menu_shell->active)
717
menu_item = gtk_get_event_widget ((GdkEvent*) event);
720
(GTK_IS_MENU_ITEM (menu_item) &&
721
!_gtk_menu_item_is_selectable (menu_item)))
724
if ((menu_item->parent == widget) &&
725
(menu_shell->active_menu_item != menu_item) &&
726
GTK_IS_MENU_ITEM (menu_item))
728
if (menu_shell->ignore_enter)
731
if ((event->detail != GDK_NOTIFY_INFERIOR) &&
732
(GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
734
gtk_menu_shell_select_item (menu_shell, menu_item);
737
else if (menu_shell->parent_menu_shell)
739
gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
747
gtk_menu_shell_leave_notify (GtkWidget *widget,
748
GdkEventCrossing *event)
750
GtkMenuShell *menu_shell;
751
GtkMenuItem *menu_item;
752
GtkWidget *event_widget;
754
g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
755
g_return_val_if_fail (event != NULL, FALSE);
757
if (GTK_WIDGET_VISIBLE (widget))
759
menu_shell = GTK_MENU_SHELL (widget);
760
event_widget = gtk_get_event_widget ((GdkEvent*) event);
762
if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
765
menu_item = GTK_MENU_ITEM (event_widget);
767
if (!_gtk_menu_item_is_selectable (event_widget))
770
if ((menu_shell->active_menu_item == event_widget) &&
771
(menu_item->submenu == NULL))
773
if ((event->detail != GDK_NOTIFY_INFERIOR) &&
774
(GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
776
gtk_menu_shell_deselect (menu_shell);
779
else if (menu_shell->parent_menu_shell)
781
gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
789
gtk_menu_shell_screen_changed (GtkWidget *widget,
790
GdkScreen *previous_screen)
792
gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
796
gtk_menu_shell_add (GtkContainer *container,
799
gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
803
gtk_menu_shell_remove (GtkContainer *container,
806
GtkMenuShell *menu_shell;
809
g_return_if_fail (GTK_IS_MENU_SHELL (container));
810
g_return_if_fail (GTK_IS_MENU_ITEM (widget));
812
was_visible = GTK_WIDGET_VISIBLE (widget);
813
menu_shell = GTK_MENU_SHELL (container);
814
menu_shell->children = g_list_remove (menu_shell->children, widget);
816
if (widget == menu_shell->active_menu_item)
818
gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
819
menu_shell->active_menu_item = NULL;
822
gtk_widget_unparent (widget);
824
/* queue resize regardless of GTK_WIDGET_VISIBLE (container),
825
* since that's what is needed by toplevels.
828
gtk_widget_queue_resize (GTK_WIDGET (container));
832
gtk_menu_shell_forall (GtkContainer *container,
833
gboolean include_internals,
834
GtkCallback callback,
835
gpointer callback_data)
837
GtkMenuShell *menu_shell;
841
g_return_if_fail (GTK_IS_MENU_SHELL (container));
842
g_return_if_fail (callback != NULL);
844
menu_shell = GTK_MENU_SHELL (container);
846
children = menu_shell->children;
849
child = children->data;
850
children = children->next;
852
(* callback) (child, callback_data);
858
gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
860
if (menu_shell->active)
862
menu_shell->button = 0;
863
menu_shell->active = FALSE;
864
menu_shell->activate_time = 0;
866
if (menu_shell->active_menu_item)
868
gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
869
menu_shell->active_menu_item = NULL;
872
if (menu_shell->have_grab)
874
menu_shell->have_grab = FALSE;
875
gtk_grab_remove (GTK_WIDGET (menu_shell));
877
if (menu_shell->have_xgrab)
879
GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
881
menu_shell->have_xgrab = FALSE;
882
gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
883
gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
889
gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
894
g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
895
g_return_val_if_fail (child != NULL, FALSE);
897
parent = child->parent;
898
while (parent && GTK_IS_MENU_SHELL (parent))
900
if (parent == (GtkWidget*) menu_shell)
902
parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
909
gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
912
GtkWidget *menu_item;
914
menu_item = gtk_get_event_widget ((GdkEvent*) event);
916
while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
917
menu_item = menu_item->parent;
919
if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
925
/* Handlers for action signals */
928
gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
929
GtkWidget *menu_item)
931
GtkMenuShellClass *class;
933
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
934
g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
936
class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
938
if (class->select_item &&
939
!(menu_shell->active &&
940
menu_shell->active_menu_item == menu_item))
941
class->select_item (menu_shell, menu_item);
944
void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
945
GtkSubmenuPlacement placement);
948
gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
949
GtkWidget *menu_item)
951
GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
953
gtk_menu_shell_deselect (menu_shell);
955
if (!_gtk_menu_item_is_selectable (menu_item))
958
menu_shell->active_menu_item = menu_item;
959
if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
960
_gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
963
_gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
964
GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
965
gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
967
/* This allows the bizarre radio buttons-with-submenus-display-history
970
if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
971
gtk_widget_activate (menu_shell->active_menu_item);
975
gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
977
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
979
if (menu_shell->active_menu_item)
981
gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
982
menu_shell->active_menu_item = NULL;
987
gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
988
GtkWidget *menu_item,
989
gboolean force_deactivate)
991
GSList *slist, *shells = NULL;
992
gboolean deactivate = force_deactivate;
994
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
995
g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
998
deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1000
g_object_ref (menu_shell);
1001
g_object_ref (menu_item);
1005
GtkMenuShell *parent_menu_shell = menu_shell;
1009
g_object_ref (parent_menu_shell);
1010
shells = g_slist_prepend (shells, parent_menu_shell);
1011
parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1013
while (parent_menu_shell);
1014
shells = g_slist_reverse (shells);
1016
gtk_menu_shell_deactivate (menu_shell);
1018
/* flush the x-queue, so any grabs are removed and
1019
* the menu is actually taken down
1021
gdk_display_sync (gtk_widget_get_display (menu_item));
1024
gtk_widget_activate (menu_item);
1026
for (slist = shells; slist; slist = slist->next)
1028
g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1029
g_object_unref (slist->data);
1031
g_slist_free (shells);
1033
g_object_unref (menu_shell);
1034
g_object_unref (menu_item);
1037
/* Distance should be +/- 1 */
1039
gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1042
if (menu_shell->active_menu_item)
1044
GList *node = g_list_find (menu_shell->children,
1045
menu_shell->active_menu_item);
1046
GList *start_node = node;
1051
while (node != start_node &&
1052
(!node || !_gtk_menu_item_is_selectable (node->data)))
1055
node = menu_shell->children;
1063
while (node != start_node &&
1064
(!node || !_gtk_menu_item_is_selectable (node->data)))
1067
node = g_list_last (menu_shell->children);
1074
gtk_menu_shell_select_item (menu_shell, node->data);
1079
* gtk_menu_shell_select_first:
1080
* @menu_shell: a #GtkMenuShell
1081
* @search_sensitive: if %TRUE, search for the first selectable
1082
* menu item, otherwise select nothing if
1083
* the first item isn't sensitive. This
1084
* should be %FALSE if the menu is being
1085
* popped up initially.
1087
* Select the first visible or selectable child of the menu shell;
1088
* don't select tearoff items unless the only item is a tearoff
1094
gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1095
gboolean search_sensitive)
1097
GtkWidget *to_select = NULL;
1100
tmp_list = menu_shell->children;
1103
GtkWidget *child = tmp_list->data;
1105
if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1106
_gtk_menu_item_is_selectable (child))
1109
if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1113
tmp_list = tmp_list->next;
1117
gtk_menu_shell_select_item (menu_shell, to_select);
1121
_gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1122
gboolean search_sensitive)
1124
GtkWidget *to_select = NULL;
1127
tmp_list = g_list_last (menu_shell->children);
1130
GtkWidget *child = tmp_list->data;
1132
if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1133
_gtk_menu_item_is_selectable (child))
1136
if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1140
tmp_list = tmp_list->prev;
1144
gtk_menu_shell_select_item (menu_shell, to_select);
1148
gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1150
GtkMenuItem *menu_item;
1152
menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1154
if (menu_item->submenu)
1156
_gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item));
1157
gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1158
if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1166
gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1167
GtkMenuDirectionType direction)
1169
GtkMenuShell *parent_menu_shell = NULL;
1170
gboolean had_selection;
1172
had_selection = menu_shell->active_menu_item != NULL;
1174
if (menu_shell->parent_menu_shell)
1175
parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1179
case GTK_MENU_DIR_PARENT:
1180
if (parent_menu_shell)
1182
if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1183
GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1184
gtk_menu_shell_deselect (menu_shell);
1187
if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1188
gtk_menu_shell_move_selected (parent_menu_shell, -1);
1190
gtk_menu_shell_move_selected (parent_menu_shell, 1);
1191
gtk_menu_shell_select_submenu_first (parent_menu_shell);
1194
/* If there is no parent and the submenu is in the opposite direction
1195
* to the menu, then make the PARENT direction wrap around to
1196
* the bottom of the submenu.
1198
else if (menu_shell->active_menu_item &&
1199
_gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1200
GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1202
GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1204
if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1205
GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1206
_gtk_menu_shell_select_last (submenu, TRUE);
1210
case GTK_MENU_DIR_CHILD:
1211
if (menu_shell->active_menu_item &&
1212
_gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1213
GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1215
if (gtk_menu_shell_select_submenu_first (menu_shell))
1219
/* Try to find a menu running the opposite direction */
1220
while (parent_menu_shell &&
1221
(GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1222
GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1224
GtkWidget *tmp_widget = parent_menu_shell->parent_menu_shell;
1227
parent_menu_shell = GTK_MENU_SHELL (tmp_widget);
1229
parent_menu_shell = NULL;
1232
if (parent_menu_shell)
1234
if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1235
gtk_menu_shell_move_selected (parent_menu_shell, 1);
1237
gtk_menu_shell_move_selected (parent_menu_shell, -1);
1239
gtk_menu_shell_select_submenu_first (parent_menu_shell);
1243
case GTK_MENU_DIR_PREV:
1244
gtk_menu_shell_move_selected (menu_shell, -1);
1245
if (!had_selection &&
1246
!menu_shell->active_menu_item &&
1247
menu_shell->children)
1248
_gtk_menu_shell_select_last (menu_shell, TRUE);
1250
case GTK_MENU_DIR_NEXT:
1251
gtk_menu_shell_move_selected (menu_shell, 1);
1252
if (!had_selection &&
1253
!menu_shell->active_menu_item &&
1254
menu_shell->children)
1255
gtk_menu_shell_select_first (menu_shell, TRUE);
1261
gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1262
gboolean force_hide)
1264
if (menu_shell->active_menu_item &&
1265
_gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1268
if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1269
gtk_menu_shell_activate_item (menu_shell,
1270
menu_shell->active_menu_item,
1273
_gtk_menu_item_popup_submenu (menu_shell->active_menu_item);
1278
gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1280
/* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1282
gtk_menu_shell_deselect (menu_shell);
1284
gtk_menu_shell_deactivate (menu_shell);
1285
g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1289
gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1290
GtkDirectionType dir)
1292
while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1294
if (menu_shell->parent_menu_shell)
1295
menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1301
_gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1305
_gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1307
GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1309
if (klass->get_popup_delay)
1311
return klass->get_popup_delay (menu_shell);
1316
GtkWidget *widget = GTK_WIDGET (menu_shell);
1318
g_object_get (gtk_widget_get_settings (widget),
1319
"gtk-menu-popup-delay", &popup_delay,
1327
* gtk_menu_shell_cancel:
1328
* @menu_shell: a #GtkMenuShell
1330
* Cancels the selection within the menu shell.
1335
gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1337
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1339
g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1342
static GtkMnemonicHash *
1343
gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1346
GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1348
if (!private->mnemonic_hash && create)
1349
private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1351
return private->mnemonic_hash;
1355
menu_shell_add_mnemonic_foreach (guint keyval,
1359
GtkKeyHash *key_hash = data;
1361
_gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1365
gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1368
GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1369
GtkWidget *widget = GTK_WIDGET (menu_shell);
1371
if (!private->key_hash && create && gtk_widget_has_screen (widget))
1373
GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1374
GdkScreen *screen = gtk_widget_get_screen (widget);
1375
GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1380
private->key_hash = _gtk_key_hash_new (keymap, NULL);
1382
_gtk_mnemonic_hash_foreach (mnemonic_hash,
1383
menu_shell_add_mnemonic_foreach,
1387
return private->key_hash;
1391
gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1393
GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1395
if (private->key_hash)
1397
_gtk_key_hash_free (private->key_hash);
1398
private->key_hash = NULL;
1403
gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1406
GtkMnemonicHash *mnemonic_hash;
1407
GtkKeyHash *key_hash;
1409
gboolean result = FALSE;
1411
mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1415
key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1419
entries = _gtk_key_hash_lookup (key_hash,
1420
event->hardware_keycode,
1422
gtk_accelerator_get_default_mod_mask (),
1426
result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1427
GPOINTER_TO_UINT (entries->data));
1433
_gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1437
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1438
g_return_if_fail (GTK_IS_WIDGET (target));
1440
_gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1442
gtk_menu_shell_reset_key_hash (menu_shell);
1446
_gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1450
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1451
g_return_if_fail (GTK_IS_WIDGET (target));
1453
_gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1455
gtk_menu_shell_reset_key_hash (menu_shell);
1459
* gtk_menu_shell_get_take_focus:
1460
* @menu_shell: a #GtkMenuShell
1462
* Returns %TRUE if the menu shell will take the keyboard focus on popup.
1464
* Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1469
gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1471
GtkMenuShellPrivate *priv;
1473
g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1475
priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1477
return priv->take_focus;
1481
* gtk_menu_shell_set_take_focus:
1482
* @menu_shell: a #GtkMenuShell
1483
* @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1485
* If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1486
* focus so that it will receive all keyboard events which is needed to enable
1487
* keyboard navigation in menus.
1489
* Setting @take_focus to %FALSE is useful only for special applications
1490
* like virtual keyboard implementations which should not take keyboard
1493
* The @take_focus state of a menu or menu bar is automatically propagated
1494
* to submenus whenever a submenu is popped up, so you don't have to worry
1495
* about recursively setting it for your entire menu hierarchy. Only when
1496
* programmatically picking a submenu and popping it up manually, the
1497
* @take_focus property of the submenu needs to be set explicitely.
1499
* Note that setting it to %FALSE has side-effects:
1501
* If the focus is in some other app, it keeps the focus and keynav in
1502
* the menu doesn't work. Consequently, keynav on the menu will only
1503
* work if the focus is on some toplevel owned by the onscreen keyboard.
1505
* To avoid confusing the user, menus with @take_focus set to %FALSE
1506
* should not display mnemonics or accelerators, since it cannot be
1507
* guaranteed that they will work.
1509
* See also gdk_keyboard_grab()
1514
gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1515
gboolean take_focus)
1517
GtkMenuShellPrivate *priv;
1519
g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1521
priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1523
if (priv->take_focus != take_focus)
1525
priv->take_focus = take_focus;
1526
g_object_notify (G_OBJECT (menu_shell), "take-focus");
1530
#define __GTK_MENU_SHELL_C__
1531
#include "gtkaliasdef.c"