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