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