]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenushell.c
Merge fix from maemo-gtk:
[~andy/gtk] / gtk / gtkmenushell.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #define GTK_MENU_INTERNALS
28
29 #include <config.h>
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkkeyhash.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenu.h"
36 #include "gtkmenubar.h"
37 #include "gtkmenuitem.h"
38 #include "gtkmenushell.h"
39 #include "gtkmnemonichash.h"
40 #include "gtktearoffmenuitem.h"
41 #include "gtkwindow.h"
42 #include "gtkprivate.h"
43 #include "gtkintl.h"
44 #include "gtkalias.h"
45
46 #define MENU_SHELL_TIMEOUT   500
47
48 #define PACK_DIRECTION(m)                                 \
49    (GTK_IS_MENU_BAR (m)                                   \
50      ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
51      : GTK_PACK_DIRECTION_LTR)
52
53 enum {
54   DEACTIVATE,
55   SELECTION_DONE,
56   MOVE_CURRENT,
57   ACTIVATE_CURRENT,
58   CANCEL,
59   CYCLE_FOCUS,
60   LAST_SIGNAL
61 };
62
63 enum {
64   PROP_0,
65   PROP_TAKE_FOCUS
66 };
67
68 /* Terminology:
69  * 
70  * A menu item can be "selected", this means that it is displayed
71  * in the prelight state, and if it has a submenu, that submenu
72  * will be popped up. 
73  * 
74  * A menu is "active" when it is visible onscreen and the user
75  * is selecting from it. A menubar is not active until the user
76  * clicks on one of its menuitems. When a menu is active,
77  * passing the mouse over a submenu will pop it up.
78  *
79  * menu_shell->active_menu_item, is however, not an "active"
80  * menu item (there is no such thing) but rather, the selected
81  * menu item in that MenuShell, if there is one.
82  *
83  * There is also is a concept of the current menu and a current
84  * menu item. The current menu item is the selected menu item
85  * that is furthest down in the heirarchy. (Every active menu_shell
86  * does not necessarily contain a selected menu item, but if
87  * it does, then menu_shell->parent_menu_shell must also contain
88  * a selected menu item. The current menu is the menu that 
89  * contains the current menu_item. It will always have a GTK
90  * grab and receive all key presses.
91  *
92  *
93  * Action signals:
94  *
95  *  ::move_current (GtkMenuDirection *dir)
96  *     Moves the current menu item in direction 'dir':
97  *
98  *       GTK_MENU_DIR_PARENT: To the parent menu shell
99  *       GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
100  *          a submenu.
101  *       GTK_MENU_DIR_NEXT/PREV: To the next or previous item
102  *          in this menu.
103  * 
104  *     As a a bit of a hack to get movement between menus and
105  *     menubars working, if submenu_placement is different for
106  *     the menu and its MenuShell then the following apply:
107  * 
108  *       - For 'parent' the current menu is not just moved to
109  *         the parent, but moved to the previous entry in the parent
110  *       - For 'child', if there is no child, then current is
111  *         moved to the next item in the parent.
112  *
113  *    Note that the above explanation of ::move_current was written
114  *    before menus and menubars had support for RTL flipping and
115  *    different packing directions, and therefore only applies for
116  *    when text direction and packing direction are both left-to-right.
117  * 
118  *  ::activate_current (GBoolean *force_hide)
119  *     Activate the current item. If 'force_hide' is true, hide
120  *     the current menu item always. Otherwise, only hide
121  *     it if menu_item->klass->hide_on_activate is true.
122  *
123  *  ::cancel ()
124  *     Cancels the current selection
125  */
126
127 #define GTK_MENU_SHELL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MENU_SHELL, GtkMenuShellPrivate))
128
129 typedef struct _GtkMenuShellPrivate GtkMenuShellPrivate;
130
131 struct _GtkMenuShellPrivate
132 {
133   GtkMnemonicHash *mnemonic_hash;
134   GtkKeyHash *key_hash;
135
136   guint take_focus : 1;
137   guint activated_submenu : 1;
138 };
139
140 static void gtk_menu_shell_set_property      (GObject           *object,
141                                               guint              prop_id,
142                                               const GValue      *value,
143                                               GParamSpec        *pspec);
144 static void gtk_menu_shell_get_property      (GObject           *object,
145                                               guint              prop_id,
146                                               GValue            *value,
147                                               GParamSpec        *pspec);
148 static void gtk_menu_shell_realize           (GtkWidget         *widget);
149 static void gtk_menu_shell_finalize          (GObject           *object);
150 static gint gtk_menu_shell_button_press      (GtkWidget         *widget,
151                                               GdkEventButton    *event);
152 static gint gtk_menu_shell_button_release    (GtkWidget         *widget,
153                                               GdkEventButton    *event);
154 static gint gtk_menu_shell_key_press         (GtkWidget         *widget,
155                                               GdkEventKey       *event);
156 static gint gtk_menu_shell_enter_notify      (GtkWidget         *widget,
157                                               GdkEventCrossing  *event);
158 static gint gtk_menu_shell_leave_notify      (GtkWidget         *widget,
159                                               GdkEventCrossing  *event);
160 static void gtk_menu_shell_screen_changed    (GtkWidget         *widget,
161                                               GdkScreen         *previous_screen);
162 static gboolean gtk_menu_shell_grab_broken       (GtkWidget         *widget,
163                                               GdkEventGrabBroken *event);
164 static void gtk_menu_shell_add               (GtkContainer      *container,
165                                               GtkWidget         *widget);
166 static void gtk_menu_shell_remove            (GtkContainer      *container,
167                                               GtkWidget         *widget);
168 static void gtk_menu_shell_forall            (GtkContainer      *container,
169                                               gboolean           include_internals,
170                                               GtkCallback        callback,
171                                               gpointer           callback_data);
172 static void gtk_menu_shell_real_insert       (GtkMenuShell *menu_shell,
173                                               GtkWidget    *child,
174                                               gint          position);
175 static void gtk_real_menu_shell_deactivate   (GtkMenuShell      *menu_shell);
176 static gint gtk_menu_shell_is_item           (GtkMenuShell      *menu_shell,
177                                               GtkWidget         *child);
178 static GtkWidget *gtk_menu_shell_get_item    (GtkMenuShell      *menu_shell,
179                                               GdkEvent          *event);
180 static GType    gtk_menu_shell_child_type  (GtkContainer      *container);
181 static void gtk_menu_shell_real_select_item  (GtkMenuShell      *menu_shell,
182                                               GtkWidget         *menu_item);
183 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell   *menu_shell); 
184
185 static void gtk_real_menu_shell_move_current (GtkMenuShell      *menu_shell,
186                                               GtkMenuDirectionType direction);
187 static void gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
188                                                   gboolean           force_hide);
189 static void gtk_real_menu_shell_cancel           (GtkMenuShell      *menu_shell);
190 static void gtk_real_menu_shell_cycle_focus      (GtkMenuShell      *menu_shell,
191                                                   GtkDirectionType   dir);
192
193 static void     gtk_menu_shell_reset_key_hash    (GtkMenuShell *menu_shell);
194 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
195                                                   GdkEventKey  *event);
196
197 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
198
199 G_DEFINE_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
200
201 static void
202 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
203 {
204   GObjectClass *object_class;
205   GtkWidgetClass *widget_class;
206   GtkContainerClass *container_class;
207
208   GtkBindingSet *binding_set;
209
210   object_class = (GObjectClass*) klass;
211   widget_class = (GtkWidgetClass*) klass;
212   container_class = (GtkContainerClass*) klass;
213
214   object_class->set_property = gtk_menu_shell_set_property;
215   object_class->get_property = gtk_menu_shell_get_property;
216   object_class->finalize = gtk_menu_shell_finalize;
217
218   widget_class->realize = gtk_menu_shell_realize;
219   widget_class->button_press_event = gtk_menu_shell_button_press;
220   widget_class->button_release_event = gtk_menu_shell_button_release;
221   widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
222   widget_class->key_press_event = gtk_menu_shell_key_press;
223   widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
224   widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
225   widget_class->screen_changed = gtk_menu_shell_screen_changed;
226
227   container_class->add = gtk_menu_shell_add;
228   container_class->remove = gtk_menu_shell_remove;
229   container_class->forall = gtk_menu_shell_forall;
230   container_class->child_type = gtk_menu_shell_child_type;
231
232   klass->submenu_placement = GTK_TOP_BOTTOM;
233   klass->deactivate = gtk_real_menu_shell_deactivate;
234   klass->selection_done = NULL;
235   klass->move_current = gtk_real_menu_shell_move_current;
236   klass->activate_current = gtk_real_menu_shell_activate_current;
237   klass->cancel = gtk_real_menu_shell_cancel;
238   klass->select_item = gtk_menu_shell_real_select_item;
239   klass->insert = gtk_menu_shell_real_insert;
240
241   menu_shell_signals[DEACTIVATE] =
242     g_signal_new (I_("deactivate"),
243                   G_OBJECT_CLASS_TYPE (object_class),
244                   G_SIGNAL_RUN_FIRST,
245                   G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
246                   NULL, NULL,
247                   _gtk_marshal_VOID__VOID,
248                   G_TYPE_NONE, 0);
249   menu_shell_signals[SELECTION_DONE] =
250     g_signal_new (I_("selection-done"),
251                   G_OBJECT_CLASS_TYPE (object_class),
252                   G_SIGNAL_RUN_FIRST,
253                   G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
254                   NULL, NULL,
255                   _gtk_marshal_VOID__VOID,
256                   G_TYPE_NONE, 0);
257   menu_shell_signals[MOVE_CURRENT] =
258     g_signal_new (I_("move_current"),
259                   G_OBJECT_CLASS_TYPE (object_class),
260                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
261                   G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
262                   NULL, NULL,
263                   _gtk_marshal_VOID__ENUM,
264                   G_TYPE_NONE, 1, 
265                   GTK_TYPE_MENU_DIRECTION_TYPE);
266   menu_shell_signals[ACTIVATE_CURRENT] =
267     g_signal_new (I_("activate_current"),
268                   G_OBJECT_CLASS_TYPE (object_class),
269                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
270                   G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
271                   NULL, NULL,
272                   _gtk_marshal_VOID__BOOLEAN,
273                   G_TYPE_NONE, 1, 
274                   G_TYPE_BOOLEAN);
275   menu_shell_signals[CANCEL] =
276     g_signal_new (I_("cancel"),
277                   G_OBJECT_CLASS_TYPE (object_class),
278                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
279                   G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
280                   NULL, NULL,
281                   _gtk_marshal_VOID__VOID,
282                   G_TYPE_NONE, 0);
283   menu_shell_signals[CYCLE_FOCUS] =
284     _gtk_binding_signal_new (I_("cycle_focus"),
285                              G_OBJECT_CLASS_TYPE (object_class),
286                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
287                              G_CALLBACK (gtk_real_menu_shell_cycle_focus),
288                              NULL, NULL,
289                              _gtk_marshal_VOID__ENUM,
290                              G_TYPE_NONE, 1,
291                              GTK_TYPE_DIRECTION_TYPE);
292
293
294   binding_set = gtk_binding_set_by_class (klass);
295   gtk_binding_entry_add_signal (binding_set,
296                                 GDK_Escape, 0,
297                                 "cancel", 0);
298   gtk_binding_entry_add_signal (binding_set,
299                                 GDK_Return, 0,
300                                 "activate_current", 1,
301                                 G_TYPE_BOOLEAN,
302                                 TRUE);
303   gtk_binding_entry_add_signal (binding_set,
304                                 GDK_KP_Enter, 0,
305                                 "activate_current", 1,
306                                 G_TYPE_BOOLEAN,
307                                 TRUE);
308   gtk_binding_entry_add_signal (binding_set,
309                                 GDK_space, 0,
310                                 "activate_current", 1,
311                                 G_TYPE_BOOLEAN,
312                                 FALSE);
313   gtk_binding_entry_add_signal (binding_set,
314                                 GDK_KP_Space, 0,
315                                 "activate_current", 1,
316                                 G_TYPE_BOOLEAN,
317                                 FALSE);
318   gtk_binding_entry_add_signal (binding_set,
319                                 GDK_F10, 0,
320                                 "cycle_focus", 1,
321                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
322   gtk_binding_entry_add_signal (binding_set,
323                                 GDK_F10, GDK_SHIFT_MASK,
324                                 "cycle_focus", 1,
325                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
326
327   /**
328    * GtkMenuShell:take-focus:
329    *
330    * A boolean that determines whether the menu and its submenus grab the
331    * keyboard focus. See gtk_menu_shell_set_take_focus() and
332    * gtk_menu_shell_get_take_focus().
333    *
334    * Since: 2.8
335    **/
336   g_object_class_install_property (object_class,
337                                    PROP_TAKE_FOCUS,
338                                    g_param_spec_boolean ("take-focus",
339                                                          P_("Take Focus"),
340                                                          P_("A boolean that determines whether the menu grabs the keyboard focus"),
341                                                          TRUE,
342                                                          GTK_PARAM_READWRITE));
343
344   g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
345 }
346
347 static GType
348 gtk_menu_shell_child_type (GtkContainer     *container)
349 {
350   return GTK_TYPE_MENU_ITEM;
351 }
352
353 static void
354 gtk_menu_shell_init (GtkMenuShell *menu_shell)
355 {
356   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
357
358   menu_shell->children = NULL;
359   menu_shell->active_menu_item = NULL;
360   menu_shell->parent_menu_shell = NULL;
361   menu_shell->active = FALSE;
362   menu_shell->have_grab = FALSE;
363   menu_shell->have_xgrab = FALSE;
364   menu_shell->button = 0;
365   menu_shell->activate_time = 0;
366
367   priv->mnemonic_hash = NULL;
368   priv->key_hash = NULL;
369   priv->take_focus = TRUE;
370   priv->activated_submenu = FALSE;
371 }
372
373 static void
374 gtk_menu_shell_set_property (GObject      *object,
375                              guint         prop_id,
376                              const GValue *value,
377                              GParamSpec   *pspec)
378 {
379   GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
380
381   switch (prop_id)
382     {
383     case PROP_TAKE_FOCUS:
384       gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
385       break;
386     default:
387       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388       break;
389     }
390 }
391
392 static void
393 gtk_menu_shell_get_property (GObject     *object,
394                              guint        prop_id,
395                              GValue      *value,
396                              GParamSpec  *pspec)
397 {
398   GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
399
400   switch (prop_id)
401     {
402     case PROP_TAKE_FOCUS:
403       g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
404       break;
405     default:
406       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
407       break;
408     }
409 }
410
411 static void
412 gtk_menu_shell_finalize (GObject *object)
413 {
414   GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
415   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
416
417   if (priv->mnemonic_hash)
418     _gtk_mnemonic_hash_free (priv->mnemonic_hash);
419   if (priv->key_hash)
420     _gtk_key_hash_free (priv->key_hash);
421
422   G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
423 }
424
425
426 void
427 gtk_menu_shell_append (GtkMenuShell *menu_shell,
428                        GtkWidget    *child)
429 {
430   gtk_menu_shell_insert (menu_shell, child, -1);
431 }
432
433 void
434 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
435                         GtkWidget    *child)
436 {
437   gtk_menu_shell_insert (menu_shell, child, 0);
438 }
439
440 void
441 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
442                        GtkWidget    *child,
443                        gint          position)
444 {
445   GtkMenuShellClass *class;
446
447   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
448   g_return_if_fail (GTK_IS_MENU_ITEM (child));
449
450   class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
451
452   if (class->insert)
453     class->insert (menu_shell, child, position);
454 }
455
456 static void
457 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
458                             GtkWidget    *child,
459                             gint          position)
460 {
461   menu_shell->children = g_list_insert (menu_shell->children, child, position);
462
463   gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
464 }
465
466 void
467 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
468 {
469   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
470
471   g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
472 }
473
474 static void
475 gtk_menu_shell_realize (GtkWidget *widget)
476 {
477   GdkWindowAttr attributes;
478   gint attributes_mask;
479
480   g_return_if_fail (GTK_IS_MENU_SHELL (widget));
481
482   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
483
484   attributes.x = widget->allocation.x;
485   attributes.y = widget->allocation.y;
486   attributes.width = widget->allocation.width;
487   attributes.height = widget->allocation.height;
488   attributes.window_type = GDK_WINDOW_CHILD;
489   attributes.wclass = GDK_INPUT_OUTPUT;
490   attributes.visual = gtk_widget_get_visual (widget);
491   attributes.colormap = gtk_widget_get_colormap (widget);
492   attributes.event_mask = gtk_widget_get_events (widget);
493   attributes.event_mask |= (GDK_EXPOSURE_MASK |
494                             GDK_BUTTON_PRESS_MASK |
495                             GDK_BUTTON_RELEASE_MASK |
496                             GDK_KEY_PRESS_MASK |
497                             GDK_ENTER_NOTIFY_MASK |
498                             GDK_LEAVE_NOTIFY_MASK);
499
500   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
501   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
502   gdk_window_set_user_data (widget->window, widget);
503
504   widget->style = gtk_style_attach (widget->style, widget->window);
505   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
506 }
507
508 void
509 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
510 {
511   if (!menu_shell->active)
512     {
513       gtk_grab_add (GTK_WIDGET (menu_shell));
514       menu_shell->have_grab = TRUE;
515       menu_shell->active = TRUE;
516     }
517 }
518
519 static gint
520 gtk_menu_shell_button_press (GtkWidget      *widget,
521                              GdkEventButton *event)
522 {
523   GtkMenuShell *menu_shell;
524   GtkWidget *menu_item;
525
526   if (event->type != GDK_BUTTON_PRESS)
527     return FALSE;
528
529   menu_shell = GTK_MENU_SHELL (widget);
530
531   if (menu_shell->parent_menu_shell)
532     return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
533
534   menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
535
536   if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
537       menu_item != GTK_MENU_SHELL (menu_item->parent)->active_menu_item)
538     {
539       /*  select the menu item *before* activating the shell, so submenus
540        *  which might be open are closed the friendly way. If we activate
541        *  (and thus grab) this menu shell first, we might get grab_broken
542        *  events which will close the entire menu hierarchy. Selecting the
543        *  menu item also fixes up the state as if enter_notify() would
544        *  have run before (which normally selects the item).
545        */
546       if (GTK_MENU_SHELL_GET_CLASS (menu_item->parent)->submenu_placement != GTK_TOP_BOTTOM)
547         {
548           gtk_menu_shell_select_item (GTK_MENU_SHELL (menu_item->parent), menu_item);
549         }
550     }
551
552   if (!menu_shell->active || !menu_shell->button)
553     {
554       _gtk_menu_shell_activate (menu_shell);
555
556       menu_shell->button = event->button;
557
558       if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
559           menu_item->parent == widget &&
560           menu_item != menu_shell->active_menu_item)
561         {
562           if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
563             {
564               menu_shell->activate_time = event->time;
565               gtk_menu_shell_select_item (menu_shell, menu_item);
566             }
567         }
568     }
569   else
570     {
571       widget = gtk_get_event_widget ((GdkEvent*) event);
572       if (widget == GTK_WIDGET (menu_shell))
573         {
574           gtk_menu_shell_deactivate (menu_shell);
575           g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
576         }
577     }
578
579   if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
580       GTK_MENU_ITEM (menu_item)->submenu != NULL &&
581       !GTK_WIDGET_VISIBLE (GTK_MENU_ITEM (menu_item)->submenu))
582     {
583       GtkMenuShellPrivate *priv;
584
585       _gtk_menu_item_popup_submenu (menu_item, FALSE);
586
587       priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
588       priv->activated_submenu = TRUE;
589     }
590
591   return TRUE;
592 }
593
594 static gboolean
595 gtk_menu_shell_grab_broken (GtkWidget          *widget,
596                             GdkEventGrabBroken *event)
597 {
598   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
599
600   if (menu_shell->have_xgrab && event->grab_window == NULL)
601     {
602       /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
603        */
604       
605       gtk_menu_shell_deselect (menu_shell);
606       
607       gtk_menu_shell_deactivate (menu_shell);
608       g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
609     }
610
611   return TRUE;
612 }
613
614 static gint
615 gtk_menu_shell_button_release (GtkWidget      *widget,
616                                GdkEventButton *event)
617 {
618   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
619   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (widget);
620
621   if (menu_shell->active)
622     {
623       GtkWidget *menu_item;
624       gboolean   deactivate = TRUE;
625
626       if (menu_shell->button && (event->button != menu_shell->button))
627         {
628           menu_shell->button = 0;
629           if (menu_shell->parent_menu_shell)
630             return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
631         }
632
633       menu_shell->button = 0;
634       menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
635
636       if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
637         {
638           if (menu_item && (menu_shell->active_menu_item == menu_item) &&
639               _gtk_menu_item_is_selectable (menu_item))
640             {
641               GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->submenu;
642
643               if (submenu == NULL)
644                 {
645                   gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
646
647                   deactivate = FALSE;
648                 }
649               else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
650                        priv->activated_submenu)
651                 {
652                   gint popdown_delay;
653                   GTimeVal *popup_time;
654                   gint64 usec_since_popup = 0;
655
656                   g_object_get (gtk_widget_get_settings (widget),
657                                 "gtk-menu-popdown-delay", &popdown_delay,
658                                 NULL);
659
660                   popup_time = g_object_get_data (G_OBJECT (submenu),
661                                                   "gtk-menu-exact-popup-time");
662
663                   if (popup_time)
664                     {
665                       GTimeVal current_time;
666
667                       g_get_current_time (&current_time);
668
669                       usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
670                                           (gint64) current_time.tv_usec -
671                                           (gint64) popup_time->tv_sec * 1000 * 1000 -
672                                           (gint64) popup_time->tv_usec);
673
674                       g_object_set_data (G_OBJECT (submenu),
675                                          "gtk-menu-exact-popup-time", NULL);
676                     }
677
678                   /*  only close the submenu on click if we opened the
679                    *  menu explicitely (usec_since_popup == 0) or
680                    *  enough time has passed since it was opened by
681                    *  GtkMenuItem's timeout (usec_since_popup > delay).
682                    */
683                   if (!priv->activated_submenu &&
684                       (usec_since_popup == 0 ||
685                        usec_since_popup > popdown_delay * 1000))
686                     {
687                       _gtk_menu_item_popdown_submenu (menu_item);
688                     }
689                   else
690                     {
691                       gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
692                     }
693
694                   deactivate = FALSE;
695                 }
696             }
697           else if (menu_item &&
698                    !_gtk_menu_item_is_selectable (menu_item) &&
699                    GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
700             {
701               deactivate = FALSE;
702             }
703           else if (menu_shell->parent_menu_shell)
704             {
705               menu_shell->active = TRUE;
706               gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
707               deactivate = FALSE;
708             }
709
710           /* If we ended up on an item with a submenu, leave the menu up.
711            */
712           if (menu_item && (menu_shell->active_menu_item == menu_item) &&
713               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
714             {
715               deactivate = FALSE;
716             }
717         }
718       else /* a very fast press-release */
719         {
720           /* We only ever want to prevent deactivation on the first
721            * press/release. Setting the time to zero is a bit of a
722            * hack, since we could be being triggered in the first
723            * few fractions of a second after a server time wraparound.
724            * the chances of that happening are ~1/10^6, without
725            * serious harm if we lose.
726            */
727           menu_shell->activate_time = 0;
728           deactivate = FALSE;
729         }
730
731       if (deactivate)
732         {
733           gtk_menu_shell_deactivate (menu_shell);
734           g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
735         }
736
737       priv->activated_submenu = FALSE;
738     }
739
740   return TRUE;
741 }
742
743 static gint
744 gtk_menu_shell_key_press (GtkWidget   *widget,
745                           GdkEventKey *event)
746 {
747   GtkMenuShell *menu_shell;
748   gboolean enable_mnemonics;
749   
750   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
751   g_return_val_if_fail (event != NULL, FALSE);
752       
753   menu_shell = GTK_MENU_SHELL (widget);
754
755   if (!menu_shell->active_menu_item && menu_shell->parent_menu_shell)
756     return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
757   
758   if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
759     return TRUE;
760
761   g_object_get (gtk_widget_get_settings (widget),
762                 "gtk-enable-mnemonics", &enable_mnemonics,
763                 NULL);
764
765   if (enable_mnemonics)
766     return gtk_menu_shell_activate_mnemonic (menu_shell, event);
767
768   return FALSE;
769 }
770
771 static gint
772 gtk_menu_shell_enter_notify (GtkWidget        *widget,
773                              GdkEventCrossing *event)
774 {
775   GtkMenuShell *menu_shell;
776
777   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
778   g_return_val_if_fail (event != NULL, FALSE);
779
780   menu_shell = GTK_MENU_SHELL (widget);
781
782   if (menu_shell->active)
783     {
784       GtkWidget *menu_item;
785
786       menu_item = gtk_get_event_widget ((GdkEvent*) event);
787
788       if (!menu_item ||
789           (GTK_IS_MENU_ITEM (menu_item) && 
790            !_gtk_menu_item_is_selectable (menu_item)))
791         return TRUE;
792
793       if (menu_item->parent == widget &&
794           GTK_IS_MENU_ITEM (menu_item))
795         {
796           if (menu_shell->ignore_enter)
797             return TRUE;
798
799           if (event->detail != GDK_NOTIFY_INFERIOR)
800             {
801               if (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT)
802                 gtk_menu_shell_select_item (menu_shell, menu_item);
803
804               /* If any mouse button is down, and there is a submenu
805                * that is not yet visible, activate it. It's sufficient
806                * to check for any button's mask (not only the one
807                * matching menu_shell->button), because there is no
808                * situation a mouse button could be pressed while
809                * entering a menu item where we wouldn't want to show
810                * its submenu.
811                */
812               if ((event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) &&
813                   GTK_MENU_ITEM (menu_item)->submenu != NULL)
814                 {
815                   GtkMenuShellPrivate *priv;
816
817                   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
818                   priv->activated_submenu = TRUE;
819
820                   if (!GTK_WIDGET_VISIBLE (GTK_MENU_ITEM (menu_item)->submenu))
821                     {
822                       gboolean touchscreen_mode;
823
824                       g_object_get (gtk_widget_get_settings (widget),
825                                     "gtk-touchscreen-mode", &touchscreen_mode,
826                                     NULL);
827
828                       if (touchscreen_mode)
829                         _gtk_menu_item_popup_submenu (menu_item, TRUE);
830                     }
831                 }
832             }
833         }
834       else if (menu_shell->parent_menu_shell)
835         {
836           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
837         }
838     }
839
840   return TRUE;
841 }
842
843 static gint
844 gtk_menu_shell_leave_notify (GtkWidget        *widget,
845                              GdkEventCrossing *event)
846 {
847   GtkMenuShell *menu_shell;
848   GtkMenuItem *menu_item;
849   GtkWidget *event_widget;
850
851   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
852   g_return_val_if_fail (event != NULL, FALSE);
853
854   if (GTK_WIDGET_VISIBLE (widget))
855     {
856       menu_shell = GTK_MENU_SHELL (widget);
857       event_widget = gtk_get_event_widget ((GdkEvent*) event);
858
859       if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
860         return TRUE;
861
862       menu_item = GTK_MENU_ITEM (event_widget);
863
864       if (!_gtk_menu_item_is_selectable (event_widget))
865         return TRUE;
866
867       if ((menu_shell->active_menu_item == event_widget) &&
868           (menu_item->submenu == NULL))
869         {
870           if ((event->detail != GDK_NOTIFY_INFERIOR) &&
871               (GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
872             {
873               gtk_menu_shell_deselect (menu_shell);
874             }
875         }
876       else if (menu_shell->parent_menu_shell)
877         {
878           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
879         }
880     }
881
882   return TRUE;
883 }
884
885 static void
886 gtk_menu_shell_screen_changed (GtkWidget *widget,
887                                GdkScreen *previous_screen)
888 {
889   gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
890 }
891
892 static void
893 gtk_menu_shell_add (GtkContainer *container,
894                     GtkWidget    *widget)
895 {
896   gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
897 }
898
899 static void
900 gtk_menu_shell_remove (GtkContainer *container,
901                        GtkWidget    *widget)
902 {
903   GtkMenuShell *menu_shell;
904   gint was_visible;
905   
906   g_return_if_fail (GTK_IS_MENU_SHELL (container));
907   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
908   
909   was_visible = GTK_WIDGET_VISIBLE (widget);
910   menu_shell = GTK_MENU_SHELL (container);
911   menu_shell->children = g_list_remove (menu_shell->children, widget);
912   
913   if (widget == menu_shell->active_menu_item)
914     {
915       gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
916       menu_shell->active_menu_item = NULL;
917     }
918
919   gtk_widget_unparent (widget);
920   
921   /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
922    * since that's what is needed by toplevels.
923    */
924   if (was_visible)
925     gtk_widget_queue_resize (GTK_WIDGET (container));
926 }
927
928 static void
929 gtk_menu_shell_forall (GtkContainer *container,
930                        gboolean      include_internals,
931                        GtkCallback   callback,
932                        gpointer      callback_data)
933 {
934   GtkMenuShell *menu_shell;
935   GtkWidget *child;
936   GList *children;
937
938   g_return_if_fail (GTK_IS_MENU_SHELL (container));
939   g_return_if_fail (callback != NULL);
940
941   menu_shell = GTK_MENU_SHELL (container);
942
943   children = menu_shell->children;
944   while (children)
945     {
946       child = children->data;
947       children = children->next;
948
949       (* callback) (child, callback_data);
950     }
951 }
952
953
954 static void
955 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
956 {
957   if (menu_shell->active)
958     {
959       menu_shell->button = 0;
960       menu_shell->active = FALSE;
961       menu_shell->activate_time = 0;
962
963       if (menu_shell->active_menu_item)
964         {
965           gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
966           menu_shell->active_menu_item = NULL;
967         }
968
969       if (menu_shell->have_grab)
970         {
971           menu_shell->have_grab = FALSE;
972           gtk_grab_remove (GTK_WIDGET (menu_shell));
973         }
974       if (menu_shell->have_xgrab)
975         {
976           GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
977           
978           menu_shell->have_xgrab = FALSE;
979           gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
980           gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
981         }
982     }
983 }
984
985 static gint
986 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
987                         GtkWidget    *child)
988 {
989   GtkWidget *parent;
990
991   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
992   g_return_val_if_fail (child != NULL, FALSE);
993
994   parent = child->parent;
995   while (parent && GTK_IS_MENU_SHELL (parent))
996     {
997       if (parent == (GtkWidget*) menu_shell)
998         return TRUE;
999       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1000     }
1001
1002   return FALSE;
1003 }
1004
1005 static GtkWidget*
1006 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1007                          GdkEvent     *event)
1008 {
1009   GtkWidget *menu_item;
1010
1011   menu_item = gtk_get_event_widget ((GdkEvent*) event);
1012   
1013   while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1014     menu_item = menu_item->parent;
1015
1016   if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1017     return menu_item;
1018   else
1019     return NULL;
1020 }
1021
1022 /* Handlers for action signals */
1023
1024 void
1025 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1026                             GtkWidget    *menu_item)
1027 {
1028   GtkMenuShellClass *class;
1029
1030   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1031   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1032
1033   class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1034
1035   if (class->select_item &&
1036       !(menu_shell->active &&
1037         menu_shell->active_menu_item == menu_item))
1038     class->select_item (menu_shell, menu_item);
1039 }
1040
1041 void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1042                                    GtkSubmenuPlacement  placement);
1043
1044 static void
1045 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1046                                  GtkWidget    *menu_item)
1047 {
1048   GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1049
1050   gtk_menu_shell_deselect (menu_shell);
1051
1052   if (!_gtk_menu_item_is_selectable (menu_item))
1053     return;
1054
1055   menu_shell->active_menu_item = menu_item;
1056   if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1057     _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1058                                   GTK_LEFT_RIGHT);
1059   else
1060     _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1061                                   GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1062   gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1063
1064   /* This allows the bizarre radio buttons-with-submenus-display-history
1065    * behavior
1066    */
1067   if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1068     gtk_widget_activate (menu_shell->active_menu_item);
1069 }
1070
1071 void
1072 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1073 {
1074   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1075
1076   if (menu_shell->active_menu_item)
1077     {
1078       gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1079       menu_shell->active_menu_item = NULL;
1080     }
1081 }
1082
1083 void
1084 gtk_menu_shell_activate_item (GtkMenuShell      *menu_shell,
1085                               GtkWidget         *menu_item,
1086                               gboolean           force_deactivate)
1087 {
1088   GSList *slist, *shells = NULL;
1089   gboolean deactivate = force_deactivate;
1090
1091   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1092   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1093
1094   if (!deactivate)
1095     deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1096
1097   g_object_ref (menu_shell);
1098   g_object_ref (menu_item);
1099
1100   if (deactivate)
1101     {
1102       GtkMenuShell *parent_menu_shell = menu_shell;
1103
1104       do
1105         {
1106           g_object_ref (parent_menu_shell);
1107           shells = g_slist_prepend (shells, parent_menu_shell);
1108           parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1109         }
1110       while (parent_menu_shell);
1111       shells = g_slist_reverse (shells);
1112
1113       gtk_menu_shell_deactivate (menu_shell);
1114   
1115       /* flush the x-queue, so any grabs are removed and
1116        * the menu is actually taken down
1117        */
1118       gdk_display_sync (gtk_widget_get_display (menu_item));
1119     }
1120
1121   gtk_widget_activate (menu_item);
1122
1123   for (slist = shells; slist; slist = slist->next)
1124     {
1125       g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1126       g_object_unref (slist->data);
1127     }
1128   g_slist_free (shells);
1129
1130   g_object_unref (menu_shell);
1131   g_object_unref (menu_item);
1132 }
1133
1134 /* Distance should be +/- 1 */
1135 static void
1136 gtk_menu_shell_move_selected (GtkMenuShell  *menu_shell, 
1137                               gint           distance)
1138 {
1139   if (menu_shell->active_menu_item)
1140     {
1141       GList *node = g_list_find (menu_shell->children,
1142                                  menu_shell->active_menu_item);
1143       GList *start_node = node;
1144       gboolean wrap_around;
1145
1146       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1147                     "gtk-keynav-wrap-around", &wrap_around,
1148                     NULL);
1149
1150       if (distance > 0)
1151         {
1152           node = node->next;
1153           while (node != start_node && 
1154                  (!node || !_gtk_menu_item_is_selectable (node->data)))
1155             {
1156               if (node)
1157                 node = node->next;
1158               else if (wrap_around)
1159                 node = menu_shell->children;
1160               else
1161                 {
1162                   gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1163                   break;
1164                 }
1165             }
1166         }
1167       else
1168         {
1169           node = node->prev;
1170           while (node != start_node &&
1171                  (!node || !_gtk_menu_item_is_selectable (node->data)))
1172             {
1173               if (node)
1174                 node = node->prev;
1175               else if (wrap_around)
1176                 node = g_list_last (menu_shell->children);
1177               else
1178                 {
1179                   gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1180                   break;
1181                 }
1182             }
1183         }
1184       
1185       if (node)
1186         gtk_menu_shell_select_item (menu_shell, node->data);
1187     }
1188 }
1189
1190 /**
1191  * gtk_menu_shell_select_first:
1192  * @menu_shell: a #GtkMenuShell
1193  * @search_sensitive: if %TRUE, search for the first selectable
1194  *                    menu item, otherwise select nothing if
1195  *                    the first item isn't sensitive. This
1196  *                    should be %FALSE if the menu is being
1197  *                    popped up initially.
1198  * 
1199  * Select the first visible or selectable child of the menu shell;
1200  * don't select tearoff items unless the only item is a tearoff
1201  * item.
1202  *
1203  * Since: 2.2
1204  **/
1205 void
1206 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1207                              gboolean      search_sensitive)
1208 {
1209   GtkWidget *to_select = NULL;
1210   GList *tmp_list;
1211
1212   tmp_list = menu_shell->children;
1213   while (tmp_list)
1214     {
1215       GtkWidget *child = tmp_list->data;
1216       
1217       if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1218           _gtk_menu_item_is_selectable (child))
1219         {
1220           to_select = child;
1221           if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1222             break;
1223         }
1224       
1225       tmp_list = tmp_list->next;
1226     }
1227
1228   if (to_select)
1229     gtk_menu_shell_select_item (menu_shell, to_select);
1230 }
1231
1232 void
1233 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1234                              gboolean      search_sensitive)
1235 {
1236   GtkWidget *to_select = NULL;
1237   GList *tmp_list;
1238
1239   tmp_list = g_list_last (menu_shell->children);
1240   while (tmp_list)
1241     {
1242       GtkWidget *child = tmp_list->data;
1243       
1244       if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1245           _gtk_menu_item_is_selectable (child))
1246         {
1247           to_select = child;
1248           if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1249             break;
1250         }
1251       
1252       tmp_list = tmp_list->prev;
1253     }
1254
1255   if (to_select)
1256     gtk_menu_shell_select_item (menu_shell, to_select);
1257 }
1258
1259 static gboolean
1260 gtk_menu_shell_select_submenu_first (GtkMenuShell     *menu_shell)
1261 {
1262   GtkMenuItem *menu_item;
1263
1264   menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item); 
1265   
1266   if (menu_item->submenu)
1267     {
1268       _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1269       gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1270       if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1271         return TRUE;
1272     }
1273
1274   return FALSE;
1275 }
1276
1277 static void
1278 gtk_real_menu_shell_move_current (GtkMenuShell         *menu_shell,
1279                                   GtkMenuDirectionType  direction)
1280 {
1281   GtkMenuShell *parent_menu_shell = NULL;
1282   gboolean had_selection;
1283
1284   had_selection = menu_shell->active_menu_item != NULL;
1285
1286   if (menu_shell->parent_menu_shell)
1287     parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1288
1289   switch (direction)
1290     {
1291     case GTK_MENU_DIR_PARENT:
1292       if (parent_menu_shell)
1293         {
1294           gboolean touchscreen_mode;
1295
1296           g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1297                         "gtk-touchscreen-mode", &touchscreen_mode,
1298                         NULL);
1299
1300           if (touchscreen_mode)
1301             {
1302               /* close menu when returning from submenu. */
1303               _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->parent_menu_item);
1304               break;
1305             }
1306
1307           if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1308               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1309             gtk_menu_shell_deselect (menu_shell);
1310           else
1311             {
1312               if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1313                 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1314               else
1315                 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1316               gtk_menu_shell_select_submenu_first (parent_menu_shell);
1317             }
1318         }
1319       /* If there is no parent and the submenu is in the opposite direction
1320        * to the menu, then make the PARENT direction wrap around to
1321        * the bottom of the submenu.
1322        */
1323       else if (menu_shell->active_menu_item &&
1324                _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1325                GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1326         {
1327           GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1328
1329           if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1330               GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1331             _gtk_menu_shell_select_last (submenu, TRUE);
1332         }
1333       break;
1334
1335     case GTK_MENU_DIR_CHILD:
1336       if (menu_shell->active_menu_item &&
1337           _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1338           GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1339         {
1340           if (gtk_menu_shell_select_submenu_first (menu_shell))
1341             break;
1342         }
1343
1344       /* Try to find a menu running the opposite direction */
1345       while (parent_menu_shell &&
1346              (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1347               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1348         {
1349           parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->parent_menu_shell);
1350         }
1351
1352       if (parent_menu_shell)
1353         {
1354           if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1355             gtk_menu_shell_move_selected (parent_menu_shell, 1);
1356           else
1357             gtk_menu_shell_move_selected (parent_menu_shell, -1);
1358
1359           gtk_menu_shell_select_submenu_first (parent_menu_shell);
1360         }
1361       break;
1362
1363     case GTK_MENU_DIR_PREV:
1364       gtk_menu_shell_move_selected (menu_shell, -1);
1365       if (!had_selection &&
1366           !menu_shell->active_menu_item &&
1367           menu_shell->children)
1368         _gtk_menu_shell_select_last (menu_shell, TRUE);
1369       break;
1370
1371     case GTK_MENU_DIR_NEXT:
1372       gtk_menu_shell_move_selected (menu_shell, 1);
1373       if (!had_selection &&
1374           !menu_shell->active_menu_item &&
1375           menu_shell->children)
1376         gtk_menu_shell_select_first (menu_shell, TRUE);
1377       break;
1378     }
1379 }
1380
1381 static void
1382 gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
1383                                       gboolean           force_hide)
1384 {
1385   if (menu_shell->active_menu_item &&
1386       _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1387   {
1388     if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1389       gtk_menu_shell_activate_item (menu_shell,
1390                                     menu_shell->active_menu_item,
1391                                     force_hide);
1392     else
1393       _gtk_menu_item_popup_submenu (menu_shell->active_menu_item, FALSE);
1394   }
1395 }
1396
1397 static void
1398 gtk_real_menu_shell_cancel (GtkMenuShell      *menu_shell)
1399 {
1400   /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1401    */
1402   gtk_menu_shell_deselect (menu_shell);
1403   
1404   gtk_menu_shell_deactivate (menu_shell);
1405   g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1406 }
1407
1408 static void
1409 gtk_real_menu_shell_cycle_focus (GtkMenuShell      *menu_shell,
1410                                  GtkDirectionType   dir)
1411 {
1412   while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1413     {
1414       if (menu_shell->parent_menu_shell)
1415         menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1416       else
1417         menu_shell = NULL;
1418     }
1419
1420   if (menu_shell)
1421     _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1422 }
1423
1424 gint
1425 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1426 {
1427   GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1428   
1429   if (klass->get_popup_delay)
1430     {
1431       return klass->get_popup_delay (menu_shell);
1432     }
1433   else
1434     {
1435       gint popup_delay;
1436       GtkWidget *widget = GTK_WIDGET (menu_shell);
1437       
1438       g_object_get (gtk_widget_get_settings (widget),
1439                     "gtk-menu-popup-delay", &popup_delay,
1440                     NULL);
1441       
1442       return popup_delay;
1443     }
1444 }
1445
1446 /**
1447  * gtk_menu_shell_cancel:
1448  * @menu_shell: a #GtkMenuShell
1449  * 
1450  * Cancels the selection within the menu shell.  
1451  * 
1452  * Since: 2.4
1453  */
1454 void
1455 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1456 {
1457   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1458
1459   g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1460 }
1461
1462 static GtkMnemonicHash *
1463 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1464                                   gboolean      create)
1465 {
1466   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1467
1468   if (!private->mnemonic_hash && create)
1469     private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1470   
1471   return private->mnemonic_hash;
1472 }
1473
1474 static void
1475 menu_shell_add_mnemonic_foreach (guint    keyval,
1476                                  GSList  *targets,
1477                                  gpointer data)
1478 {
1479   GtkKeyHash *key_hash = data;
1480
1481   _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1482 }
1483
1484 static GtkKeyHash *
1485 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1486                              gboolean      create)
1487 {
1488   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1489   GtkWidget *widget = GTK_WIDGET (menu_shell);
1490
1491   if (!private->key_hash && create && gtk_widget_has_screen (widget))
1492     {
1493       GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1494       GdkScreen *screen = gtk_widget_get_screen (widget);
1495       GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1496
1497       if (!mnemonic_hash)
1498         return NULL;
1499       
1500       private->key_hash = _gtk_key_hash_new (keymap, NULL);
1501
1502       _gtk_mnemonic_hash_foreach (mnemonic_hash,
1503                                   menu_shell_add_mnemonic_foreach,
1504                                   private->key_hash);
1505     }
1506   
1507   return private->key_hash;
1508 }
1509
1510 static void
1511 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1512 {
1513   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1514
1515   if (private->key_hash)
1516     {
1517       _gtk_key_hash_free (private->key_hash);
1518       private->key_hash = NULL;
1519     }
1520 }
1521
1522 static gboolean
1523 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1524                                   GdkEventKey  *event)
1525 {
1526   GtkMnemonicHash *mnemonic_hash;
1527   GtkKeyHash *key_hash;
1528   GSList *entries;
1529   gboolean result = FALSE;
1530
1531   mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1532   if (!mnemonic_hash)
1533     return FALSE;
1534
1535   key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1536   if (!key_hash)
1537     return FALSE;
1538   
1539   entries = _gtk_key_hash_lookup (key_hash,
1540                                   event->hardware_keycode,
1541                                   event->state,
1542                                   gtk_accelerator_get_default_mod_mask (),
1543                                   event->group);
1544
1545   if (entries)
1546     result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1547                                           GPOINTER_TO_UINT (entries->data));
1548
1549   return result;
1550 }
1551
1552 void
1553 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1554                               guint      keyval,
1555                               GtkWidget *target)
1556 {
1557   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1558   g_return_if_fail (GTK_IS_WIDGET (target));
1559
1560   _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1561                           keyval, target);
1562   gtk_menu_shell_reset_key_hash (menu_shell);
1563 }
1564
1565 void
1566 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1567                                  guint      keyval,
1568                                  GtkWidget *target)
1569 {
1570   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1571   g_return_if_fail (GTK_IS_WIDGET (target));
1572   
1573   _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1574                              keyval, target);
1575   gtk_menu_shell_reset_key_hash (menu_shell);
1576 }
1577
1578 /**
1579  * gtk_menu_shell_get_take_focus:
1580  * @menu_shell: a #GtkMenuShell
1581  *
1582  * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1583  *
1584  * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1585  *
1586  * Since: 2.8
1587  **/
1588 gboolean
1589 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1590 {
1591   GtkMenuShellPrivate *priv;
1592
1593   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1594
1595   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1596
1597   return priv->take_focus;
1598 }
1599
1600 /**
1601  * gtk_menu_shell_set_take_focus:
1602  * @menu_shell: a #GtkMenuShell
1603  * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1604  *
1605  * If @take_focus is %TRUE (the default) the menu shell will take the keyboard 
1606  * focus so that it will receive all keyboard events which is needed to enable
1607  * keyboard navigation in menus.
1608  *
1609  * Setting @take_focus to %FALSE is useful only for special applications
1610  * like virtual keyboard implementations which should not take keyboard
1611  * focus.
1612  *
1613  * The @take_focus state of a menu or menu bar is automatically propagated
1614  * to submenus whenever a submenu is popped up, so you don't have to worry
1615  * about recursively setting it for your entire menu hierarchy. Only when
1616  * programmatically picking a submenu and popping it up manually, the
1617  * @take_focus property of the submenu needs to be set explicitely.
1618  *
1619  * Note that setting it to %FALSE has side-effects:
1620  *
1621  * If the focus is in some other app, it keeps the focus and keynav in
1622  * the menu doesn't work. Consequently, keynav on the menu will only
1623  * work if the focus is on some toplevel owned by the onscreen keyboard.
1624  *
1625  * To avoid confusing the user, menus with @take_focus set to %FALSE
1626  * should not display mnemonics or accelerators, since it cannot be
1627  * guaranteed that they will work.
1628  *
1629  * See also gdk_keyboard_grab()
1630  *
1631  * Since: 2.8
1632  **/
1633 void
1634 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1635                                gboolean      take_focus)
1636 {
1637   GtkMenuShellPrivate *priv;
1638
1639   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1640
1641   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1642
1643   if (priv->take_focus != take_focus)
1644     {
1645       priv->take_focus = take_focus;
1646       g_object_notify (G_OBJECT (menu_shell), "take-focus");
1647     }
1648 }
1649
1650 #define __GTK_MENU_SHELL_C__
1651 #include "gtkaliasdef.c"