]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Removed GtkMenuItem->show_submenu_indicator flag
[~andy/gtk] / gtk / gtkmenubar.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 #include "config.h"
28
29 #include "gtkmenubar.h"
30
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbindings.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenuitemprivate.h"
36 #include "gtkmenuprivate.h"
37 #include "gtkmenushellprivate.h"
38 #include "gtksettings.h"
39 #include "gtksizerequest.h"
40 #include "gtkwindow.h"
41
42 #include "gtkintl.h"
43 #include "gtkprivate.h"
44
45 #define BORDER_SPACING  0
46 #define DEFAULT_IPADDING 1
47
48 /* Properties */
49 enum {
50   PROP_0,
51   PROP_PACK_DIRECTION,
52   PROP_CHILD_PACK_DIRECTION
53 };
54
55 struct _GtkMenuBarPrivate
56 {
57   GtkPackDirection pack_direction;
58   GtkPackDirection child_pack_direction;
59 };
60
61
62 static void gtk_menu_bar_set_property      (GObject             *object,
63                                             guint                prop_id,
64                                             const GValue        *value,
65                                             GParamSpec          *pspec);
66 static void gtk_menu_bar_get_property      (GObject             *object,
67                                             guint                prop_id,
68                                             GValue              *value,
69                                             GParamSpec          *pspec);
70 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
71                                             GtkRequisition  *requisition);
72 static void gtk_menu_bar_get_preferred_width (GtkWidget     *widget,
73                                               gint          *minimum,
74                                               gint          *natural);
75 static void gtk_menu_bar_get_preferred_height (GtkWidget    *widget,
76                                                gint         *minimum,
77                                                gint         *natural);
78 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
79                                             GtkAllocation   *allocation);
80 static gint gtk_menu_bar_draw              (GtkWidget       *widget,
81                                             cairo_t         *cr);
82 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
83                                             GtkWidget       *old_toplevel);
84 static gint gtk_menu_bar_get_popup_delay   (GtkMenuShell    *menu_shell);
85 static void gtk_menu_bar_move_current      (GtkMenuShell     *menu_shell,
86                                             GtkMenuDirectionType direction);
87
88 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
89
90 G_DEFINE_TYPE (GtkMenuBar, gtk_menu_bar, GTK_TYPE_MENU_SHELL)
91
92 static void
93 gtk_menu_bar_class_init (GtkMenuBarClass *class)
94 {
95   GObjectClass *gobject_class;
96   GtkWidgetClass *widget_class;
97   GtkMenuShellClass *menu_shell_class;
98
99   GtkBindingSet *binding_set;
100
101   gobject_class = (GObjectClass*) class;
102   widget_class = (GtkWidgetClass*) class;
103   menu_shell_class = (GtkMenuShellClass*) class;
104
105   gobject_class->get_property = gtk_menu_bar_get_property;
106   gobject_class->set_property = gtk_menu_bar_set_property;
107
108   widget_class->get_preferred_width = gtk_menu_bar_get_preferred_width;
109   widget_class->get_preferred_height = gtk_menu_bar_get_preferred_height;
110   widget_class->size_allocate = gtk_menu_bar_size_allocate;
111   widget_class->draw = gtk_menu_bar_draw;
112   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
113   
114   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
115   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
116   menu_shell_class->move_current = gtk_menu_bar_move_current;
117
118   binding_set = gtk_binding_set_by_class (class);
119   gtk_binding_entry_add_signal (binding_set,
120                                 GDK_KEY_Left, 0,
121                                 "move-current", 1,
122                                 GTK_TYPE_MENU_DIRECTION_TYPE,
123                                 GTK_MENU_DIR_PREV);
124   gtk_binding_entry_add_signal (binding_set,
125                                 GDK_KEY_KP_Left, 0,
126                                 "move-current", 1,
127                                 GTK_TYPE_MENU_DIRECTION_TYPE,
128                                 GTK_MENU_DIR_PREV);
129   gtk_binding_entry_add_signal (binding_set,
130                                 GDK_KEY_Right, 0,
131                                 "move-current", 1,
132                                 GTK_TYPE_MENU_DIRECTION_TYPE,
133                                 GTK_MENU_DIR_NEXT);
134   gtk_binding_entry_add_signal (binding_set,
135                                 GDK_KEY_KP_Right, 0,
136                                 "move-current", 1,
137                                 GTK_TYPE_MENU_DIRECTION_TYPE,
138                                 GTK_MENU_DIR_NEXT);
139   gtk_binding_entry_add_signal (binding_set,
140                                 GDK_KEY_Up, 0,
141                                 "move-current", 1,
142                                 GTK_TYPE_MENU_DIRECTION_TYPE,
143                                 GTK_MENU_DIR_PARENT);
144   gtk_binding_entry_add_signal (binding_set,
145                                 GDK_KEY_KP_Up, 0,
146                                 "move-current", 1,
147                                 GTK_TYPE_MENU_DIRECTION_TYPE,
148                                 GTK_MENU_DIR_PARENT);
149   gtk_binding_entry_add_signal (binding_set,
150                                 GDK_KEY_Down, 0,
151                                 "move-current", 1,
152                                 GTK_TYPE_MENU_DIRECTION_TYPE,
153                                 GTK_MENU_DIR_CHILD);
154   gtk_binding_entry_add_signal (binding_set,
155                                 GDK_KEY_KP_Down, 0,
156                                 "move-current", 1,
157                                 GTK_TYPE_MENU_DIRECTION_TYPE,
158                                 GTK_MENU_DIR_CHILD);
159
160   /**
161    * GtkMenuBar:pack-direction:
162    *
163    * The pack direction of the menubar. It determines how
164    * menuitems are arranged in the menubar.
165    *
166    * Since: 2.8
167    */
168   g_object_class_install_property (gobject_class,
169                                    PROP_PACK_DIRECTION,
170                                    g_param_spec_enum ("pack-direction",
171                                                       P_("Pack direction"),
172                                                       P_("The pack direction of the menubar"),
173                                                       GTK_TYPE_PACK_DIRECTION,
174                                                       GTK_PACK_DIRECTION_LTR,
175                                                       GTK_PARAM_READWRITE));
176   
177   /**
178    * GtkMenuBar:child-pack-direction:
179    *
180    * The child pack direction of the menubar. It determines how
181    * the widgets contained in child menuitems are arranged.
182    *
183    * Since: 2.8
184    */
185   g_object_class_install_property (gobject_class,
186                                    PROP_CHILD_PACK_DIRECTION,
187                                    g_param_spec_enum ("child-pack-direction",
188                                                       P_("Child Pack direction"),
189                                                       P_("The child pack direction of the menubar"),
190                                                       GTK_TYPE_PACK_DIRECTION,
191                                                       GTK_PACK_DIRECTION_LTR,
192                                                       GTK_PARAM_READWRITE));
193   
194
195   gtk_widget_class_install_style_property (widget_class,
196                                            g_param_spec_enum ("shadow-type",
197                                                               P_("Shadow type"),
198                                                               P_("Style of bevel around the menubar"),
199                                                               GTK_TYPE_SHADOW_TYPE,
200                                                               GTK_SHADOW_OUT,
201                                                               GTK_PARAM_READABLE));
202
203   gtk_widget_class_install_style_property (widget_class,
204                                            g_param_spec_int ("internal-padding",
205                                                              P_("Internal padding"),
206                                                              P_("Amount of border space between the menubar shadow and the menu items"),
207                                                              0,
208                                                              G_MAXINT,
209                                                              DEFAULT_IPADDING,
210                                                              GTK_PARAM_READABLE));
211
212   g_type_class_add_private (gobject_class, sizeof (GtkMenuBarPrivate));
213 }
214
215 static void
216 gtk_menu_bar_init (GtkMenuBar *menu_bar)
217 {
218   GtkStyleContext *context;
219
220   menu_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_bar,
221                                                 GTK_TYPE_MENU_BAR,
222                                                 GtkMenuBarPrivate);
223
224   context = gtk_widget_get_style_context (GTK_WIDGET (menu_bar));
225   gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
226 }
227
228 GtkWidget*
229 gtk_menu_bar_new (void)
230 {
231   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
232 }
233
234 static void
235 gtk_menu_bar_set_property (GObject      *object,
236                            guint         prop_id,
237                            const GValue *value,
238                            GParamSpec   *pspec)
239 {
240   GtkMenuBar *menubar = GTK_MENU_BAR (object);
241   
242   switch (prop_id)
243     {
244     case PROP_PACK_DIRECTION:
245       gtk_menu_bar_set_pack_direction (menubar, g_value_get_enum (value));
246       break;
247     case PROP_CHILD_PACK_DIRECTION:
248       gtk_menu_bar_set_child_pack_direction (menubar, g_value_get_enum (value));
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253     }
254 }
255
256 static void
257 gtk_menu_bar_get_property (GObject    *object,
258                            guint       prop_id,
259                            GValue     *value,
260                            GParamSpec *pspec)
261 {
262   GtkMenuBar *menubar = GTK_MENU_BAR (object);
263   
264   switch (prop_id)
265     {
266     case PROP_PACK_DIRECTION:
267       g_value_set_enum (value, gtk_menu_bar_get_pack_direction (menubar));
268       break;
269     case PROP_CHILD_PACK_DIRECTION:
270       g_value_set_enum (value, gtk_menu_bar_get_child_pack_direction (menubar));
271       break;
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275     }
276 }
277
278 static void
279 gtk_menu_bar_size_request (GtkWidget      *widget,
280                            GtkRequisition *requisition)
281 {
282   GtkMenuBar *menu_bar;
283   GtkMenuBarPrivate *priv;
284   GtkMenuShell *menu_shell;
285   GtkWidget *child;
286   GList *children;
287   gint nchildren;
288   GtkRequisition child_requisition;
289   gint ipadding;
290   guint border_width;
291
292   g_return_if_fail (GTK_IS_MENU_BAR (widget));
293   g_return_if_fail (requisition != NULL);
294
295   requisition->width = 0;
296   requisition->height = 0;
297   
298   if (gtk_widget_get_visible (widget))
299     {
300       menu_bar = GTK_MENU_BAR (widget);
301       menu_shell = GTK_MENU_SHELL (widget);
302       priv = menu_bar->priv;
303
304       nchildren = 0;
305       children = menu_shell->priv->children;
306
307       while (children)
308         {
309           child = children->data;
310           children = children->next;
311
312           if (gtk_widget_get_visible (child))
313             {
314               gint toggle_size;
315
316               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
317               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
318                                                  &toggle_size);
319
320               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
321                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
322                 child_requisition.width += toggle_size;
323               else
324                 child_requisition.height += toggle_size;
325
326               if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
327                   priv->pack_direction == GTK_PACK_DIRECTION_RTL)
328                 {
329                   requisition->width += child_requisition.width;
330                   requisition->height = MAX (requisition->height, child_requisition.height);
331                 }
332               else
333                 {
334                   requisition->width = MAX (requisition->width, child_requisition.width);
335                   requisition->height += child_requisition.height;
336                 }
337               nchildren += 1;
338             }
339         }
340
341       gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
342
343       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
344       requisition->width += (border_width +
345                              ipadding + 
346                              BORDER_SPACING) * 2;
347       requisition->height += (border_width +
348                               ipadding +
349                               BORDER_SPACING) * 2;
350
351       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
352         {
353           GtkStyleContext *context;
354           GtkBorder *border;
355
356           context = gtk_widget_get_style_context (widget);
357
358           gtk_style_context_get (context, 0,
359                                  "border-width", &border,
360                                  NULL);
361
362           requisition->width += border->left + border->right;
363           requisition->height += border->top + border->bottom;
364           gtk_border_free (border);
365         }
366     }
367 }
368
369 static void
370 gtk_menu_bar_get_preferred_width (GtkWidget *widget,
371                                   gint      *minimum,
372                                   gint      *natural)
373 {
374   GtkRequisition requisition;
375
376   gtk_menu_bar_size_request (widget, &requisition);
377
378   *minimum = *natural = requisition.width;
379 }
380
381 static void
382 gtk_menu_bar_get_preferred_height (GtkWidget *widget,
383                                    gint      *minimum,
384                                    gint      *natural)
385 {
386   GtkRequisition requisition;
387
388   gtk_menu_bar_size_request (widget, &requisition);
389
390   *minimum = *natural = requisition.height;
391 }
392
393 static void
394 gtk_menu_bar_size_allocate (GtkWidget     *widget,
395                             GtkAllocation *allocation)
396 {
397   GtkMenuBar *menu_bar;
398   GtkMenuShell *menu_shell;
399   GtkMenuBarPrivate *priv;
400   GtkWidget *child;
401   GList *children;
402   GtkAllocation child_allocation;
403   GtkRequisition child_requisition;
404   guint offset;
405   GtkTextDirection direction;
406   gint ltr_x, ltr_y;
407   gint ipadding;
408   guint border_width;
409
410   g_return_if_fail (GTK_IS_MENU_BAR (widget));
411   g_return_if_fail (allocation != NULL);
412
413   menu_bar = GTK_MENU_BAR (widget);
414   menu_shell = GTK_MENU_SHELL (widget);
415   priv = menu_bar->priv;
416
417   direction = gtk_widget_get_direction (widget);
418
419   gtk_widget_set_allocation (widget, allocation);
420
421   if (gtk_widget_get_realized (widget))
422     gdk_window_move_resize (gtk_widget_get_window (widget),
423                             allocation->x, allocation->y,
424                             allocation->width, allocation->height);
425
426   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
427   
428   if (menu_shell->priv->children)
429     {
430       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
431       child_allocation.x = (border_width +
432                             ipadding + 
433                             BORDER_SPACING);
434       child_allocation.y = (border_width +
435                             BORDER_SPACING);
436       
437       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
438         {
439           GtkStyleContext *context;
440           GtkBorder *border;
441
442           context = gtk_widget_get_style_context (widget);
443           gtk_style_context_get (context, 0,
444                                  "border-width", &border,
445                                  NULL);
446
447           child_allocation.x += border->left;
448           child_allocation.y += border->top;
449
450           gtk_border_free (border);
451         }
452       
453       if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
454           priv->pack_direction == GTK_PACK_DIRECTION_RTL)
455         {
456           child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
457
458           offset = child_allocation.x;  /* Window edge to menubar start */
459           ltr_x = child_allocation.x;
460
461           children = menu_shell->priv->children;
462           while (children)
463             {
464               gint toggle_size;
465
466               child = children->data;
467               children = children->next;
468               
469               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
470                                                  &toggle_size);
471               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
472
473               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
474                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
475                 child_requisition.width += toggle_size;
476               else
477                 child_requisition.height += toggle_size;
478
479               /* Support for the right justified help menu */
480               if (children == NULL &&
481                   GTK_IS_MENU_ITEM (child) &&
482                   GTK_MENU_ITEM (child)->priv->right_justify)
483                 {
484                   ltr_x = allocation->width -
485                     child_requisition.width - offset;
486                 }
487               if (gtk_widget_get_visible (child))
488                 {
489                   if ((direction == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR))
490                     child_allocation.x = ltr_x;
491                   else
492                     child_allocation.x = allocation->width -
493                       child_requisition.width - ltr_x; 
494                   
495                   child_allocation.width = child_requisition.width;
496                   
497                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
498                                                       toggle_size);
499                   gtk_widget_size_allocate (child, &child_allocation);
500                   
501                   ltr_x += child_allocation.width;
502                 }
503             }
504         }
505       else
506         {
507           child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
508
509           offset = child_allocation.y;  /* Window edge to menubar start */
510           ltr_y = child_allocation.y;
511
512           children = menu_shell->priv->children;
513           while (children)
514             {
515               gint toggle_size;
516
517               child = children->data;
518               children = children->next;
519               
520               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
521                                                  &toggle_size);
522               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
523
524               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
525                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
526                 child_requisition.width += toggle_size;
527               else
528                 child_requisition.height += toggle_size;
529
530               /* Support for the right justified help menu */
531               if (children == NULL &&
532                   GTK_IS_MENU_ITEM (child) &&
533                   GTK_MENU_ITEM (child)->priv->right_justify)
534                 {
535                   ltr_y = allocation->height -
536                     child_requisition.height - offset;
537                 }
538               if (gtk_widget_get_visible (child))
539                 {
540                   if ((direction == GTK_TEXT_DIR_LTR) ==
541                       (priv->pack_direction == GTK_PACK_DIRECTION_TTB))
542                     child_allocation.y = ltr_y;
543                   else
544                     child_allocation.y = allocation->height -
545                       child_requisition.height - ltr_y; 
546                   child_allocation.height = child_requisition.height;
547                   
548                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
549                                                       toggle_size);
550                   gtk_widget_size_allocate (child, &child_allocation);
551                   
552                   ltr_y += child_allocation.height;
553                 }
554             }
555         }
556     }
557 }
558
559 static gint
560 gtk_menu_bar_draw (GtkWidget *widget,
561                    cairo_t   *cr)
562 {
563   GtkStyleContext *context;
564   GtkStateFlags state;
565   int border;
566
567   border = gtk_container_get_border_width (GTK_CONTAINER (widget));
568   context = gtk_widget_get_style_context (widget);
569
570   state = gtk_widget_get_state_flags (widget);
571   gtk_style_context_set_state (context, state);
572
573   if (get_shadow_type (GTK_MENU_BAR (widget)) != GTK_SHADOW_NONE)
574     gtk_render_background (context, cr,
575                            border, border,
576                            gtk_widget_get_allocated_width (widget) - border * 2,
577                            gtk_widget_get_allocated_height (widget) - border * 2);
578
579   gtk_render_frame (context, cr,
580                     border, border,
581                     gtk_widget_get_allocated_width (widget) - border * 2,
582                     gtk_widget_get_allocated_height (widget) - border * 2);
583
584   GTK_WIDGET_CLASS (gtk_menu_bar_parent_class)->draw (widget, cr);
585
586   return FALSE;
587 }
588
589 static GList *
590 get_menu_bars (GtkWindow *window)
591 {
592   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
593 }
594
595 static GList *
596 get_viewable_menu_bars (GtkWindow *window)
597 {
598   GList *menu_bars;
599   GList *viewable_menu_bars = NULL;
600
601   for (menu_bars = get_menu_bars (window);
602        menu_bars;
603        menu_bars = menu_bars->next)
604     {
605       GtkWidget *widget = menu_bars->data;
606       gboolean viewable = TRUE;
607       
608       while (widget)
609         {
610           if (!gtk_widget_get_mapped (widget))
611             viewable = FALSE;
612
613           widget = gtk_widget_get_parent (widget);
614         }
615
616       if (viewable)
617         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
618     }
619
620   return g_list_reverse (viewable_menu_bars);
621 }
622
623 static void
624 set_menu_bars (GtkWindow *window,
625                GList     *menubars)
626 {
627   g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
628 }
629
630 static gboolean
631 window_key_press_handler (GtkWidget   *widget,
632                           GdkEventKey *event,
633                           gpointer     data)
634 {
635   gchar *accel = NULL;
636   gboolean retval = FALSE;
637   
638   g_object_get (gtk_widget_get_settings (widget),
639                 "gtk-menu-bar-accel", &accel,
640                 NULL);
641
642   if (accel && *accel)
643     {
644       guint keyval = 0;
645       GdkModifierType mods = 0;
646
647       gtk_accelerator_parse (accel, &keyval, &mods);
648
649       if (keyval == 0)
650         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
651
652       /* FIXME this is wrong, needs to be in the global accel resolution
653        * thing, to properly consider i18n etc., but that probably requires
654        * AccelGroup changes etc.
655        */
656       if (event->keyval == keyval &&
657           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
658            (mods & gtk_accelerator_get_default_mod_mask ())))
659         {
660           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
661           GList *menubars;
662
663           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
664                                                 GTK_DIR_TAB_FORWARD, NULL);
665           g_list_free (tmp_menubars);
666           
667           if (menubars)
668             {
669               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
670
671               _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
672               _gtk_menu_shell_activate (menu_shell);
673               gtk_menu_shell_select_first (menu_shell, FALSE);
674               
675               g_list_free (menubars);
676               
677               retval = TRUE;          
678             }
679         }
680     }
681
682   g_free (accel);
683
684   return retval;
685 }
686
687 static void
688 add_to_window (GtkWindow  *window,
689                GtkMenuBar *menubar)
690 {
691   GList *menubars = get_menu_bars (window);
692
693   if (!menubars)
694     {
695       g_signal_connect (window,
696                         "key-press-event",
697                         G_CALLBACK (window_key_press_handler),
698                         NULL);
699     }
700
701   set_menu_bars (window, g_list_prepend (menubars, menubar));
702 }
703
704 static void
705 remove_from_window (GtkWindow  *window,
706                     GtkMenuBar *menubar)
707 {
708   GList *menubars = get_menu_bars (window);
709
710   menubars = g_list_remove (menubars, menubar);
711
712   if (!menubars)
713     {
714       g_signal_handlers_disconnect_by_func (window,
715                                             window_key_press_handler,
716                                             NULL);
717     }
718
719   set_menu_bars (window, menubars);
720 }
721
722 static void
723 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
724                                 GtkWidget *old_toplevel)
725 {
726   GtkWidget *toplevel;  
727   GtkMenuBar *menubar;
728
729   menubar = GTK_MENU_BAR (widget);
730
731   toplevel = gtk_widget_get_toplevel (widget);
732
733   if (old_toplevel)
734     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
735   
736   if (gtk_widget_is_toplevel (toplevel))
737     add_to_window (GTK_WINDOW (toplevel), menubar);
738 }
739
740 /**
741  * _gtk_menu_bar_cycle_focus:
742  * @menubar: a #GtkMenuBar
743  * @dir: direction in which to cycle the focus
744  * 
745  * Move the focus between menubars in the toplevel.
746  **/
747 void
748 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
749                            GtkDirectionType  dir)
750 {
751   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
752   GtkMenuItem *to_activate = NULL;
753
754   if (gtk_widget_is_toplevel (toplevel))
755     {
756       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
757       GList *menubars;
758       GList *current;
759
760       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
761                                             dir, GTK_WIDGET (menubar));
762       g_list_free (tmp_menubars);
763
764       if (menubars)
765         {
766           current = g_list_find (menubars, menubar);
767
768           if (current && current->next)
769             {
770               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
771               if (new_menushell->priv->children)
772                 to_activate = new_menushell->priv->children->data;
773             }
774         }
775           
776       g_list_free (menubars);
777     }
778
779   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
780
781   if (to_activate)
782     g_signal_emit_by_name (to_activate, "activate_item");
783 }
784
785 static GtkShadowType
786 get_shadow_type (GtkMenuBar *menubar)
787 {
788   GtkShadowType shadow_type = GTK_SHADOW_OUT;
789   
790   gtk_widget_style_get (GTK_WIDGET (menubar),
791                         "shadow-type", &shadow_type,
792                         NULL);
793
794   return shadow_type;
795 }
796
797 static gint
798 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
799 {
800   gint popup_delay;
801   
802   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
803                 "gtk-menu-bar-popup-delay", &popup_delay,
804                 NULL);
805
806   return popup_delay;
807 }
808
809 static void
810 gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
811                            GtkMenuDirectionType  direction)
812 {
813   GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
814   GtkTextDirection text_dir;
815   GtkPackDirection pack_dir;
816
817   text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
818   pack_dir = gtk_menu_bar_get_pack_direction (menubar);
819   
820   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
821      {
822       if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
823         {
824           switch (direction) 
825             {      
826             case GTK_MENU_DIR_PREV:
827               direction = GTK_MENU_DIR_NEXT;
828               break;
829             case GTK_MENU_DIR_NEXT:
830               direction = GTK_MENU_DIR_PREV;
831               break;
832             default: ;
833             }
834         }
835     }
836   else
837     {
838       switch (direction) 
839         {
840         case GTK_MENU_DIR_PARENT:
841           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
842             direction = GTK_MENU_DIR_PREV;
843           else
844             direction = GTK_MENU_DIR_NEXT;
845           break;
846         case GTK_MENU_DIR_CHILD:
847           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
848             direction = GTK_MENU_DIR_NEXT;
849           else
850             direction = GTK_MENU_DIR_PREV;
851           break;
852         case GTK_MENU_DIR_PREV:
853           if (text_dir == GTK_TEXT_DIR_RTL)       
854             direction = GTK_MENU_DIR_CHILD;
855           else
856             direction = GTK_MENU_DIR_PARENT;
857           break;
858         case GTK_MENU_DIR_NEXT:
859           if (text_dir == GTK_TEXT_DIR_RTL)       
860             direction = GTK_MENU_DIR_PARENT;
861           else
862             direction = GTK_MENU_DIR_CHILD;
863           break;
864         default: ;
865         }
866     }
867   
868   GTK_MENU_SHELL_CLASS (gtk_menu_bar_parent_class)->move_current (menu_shell, direction);
869 }
870
871 /**
872  * gtk_menu_bar_get_pack_direction:
873  * @menubar: a #GtkMenuBar
874  * 
875  * Retrieves the current pack direction of the menubar. 
876  * See gtk_menu_bar_set_pack_direction().
877  *
878  * Return value: the pack direction
879  *
880  * Since: 2.8
881  */
882 GtkPackDirection
883 gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
884 {
885   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
886                         GTK_PACK_DIRECTION_LTR);
887
888   return menubar->priv->pack_direction;
889 }
890
891 /**
892  * gtk_menu_bar_set_pack_direction:
893  * @menubar: a #GtkMenuBar
894  * @pack_dir: a new #GtkPackDirection
895  * 
896  * Sets how items should be packed inside a menubar.
897  * 
898  * Since: 2.8
899  */
900 void
901 gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
902                                  GtkPackDirection  pack_dir)
903 {
904   GtkMenuBarPrivate *priv;
905   GList *l;
906
907   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
908
909   priv = menubar->priv;
910
911   if (priv->pack_direction != pack_dir)
912     {
913       priv->pack_direction = pack_dir;
914
915       gtk_widget_queue_resize (GTK_WIDGET (menubar));
916
917       for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
918         gtk_widget_queue_resize (GTK_WIDGET (l->data));
919
920       g_object_notify (G_OBJECT (menubar), "pack-direction");
921     }
922 }
923
924 /**
925  * gtk_menu_bar_get_child_pack_direction:
926  * @menubar: a #GtkMenuBar
927  * 
928  * Retrieves the current child pack direction of the menubar.
929  * See gtk_menu_bar_set_child_pack_direction().
930  *
931  * Return value: the child pack direction
932  *
933  * Since: 2.8
934  */
935 GtkPackDirection
936 gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
937 {
938   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
939                         GTK_PACK_DIRECTION_LTR);
940
941   return menubar->priv->child_pack_direction;
942 }
943
944 /**
945  * gtk_menu_bar_set_child_pack_direction:
946  * @menubar: a #GtkMenuBar
947  * @child_pack_dir: a new #GtkPackDirection
948  * 
949  * Sets how widgets should be packed inside the children of a menubar.
950  * 
951  * Since: 2.8
952  */
953 void
954 gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
955                                        GtkPackDirection  child_pack_dir)
956 {
957   GtkMenuBarPrivate *priv;
958   GList *l;
959
960   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
961
962   priv = menubar->priv;
963
964   if (priv->child_pack_direction != child_pack_dir)
965     {
966       priv->child_pack_direction = child_pack_dir;
967
968       gtk_widget_queue_resize (GTK_WIDGET (menubar));
969
970       for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
971         gtk_widget_queue_resize (GTK_WIDGET (l->data));
972
973       g_object_notify (G_OBJECT (menubar), "child-pack-direction");
974     }
975 }