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