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