]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~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 #define GTK_MENU_INTERNALS
28
29 #include <config.h>
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkmenubar.h"
35 #include "gtkmenuitem.h"
36 #include "gtksettings.h"
37 #include "gtkintl.h"
38 #include "gtkwindow.h"
39
40
41 #define BORDER_SPACING  0
42 #define DEFAULT_IPADDING 1
43
44 static void gtk_menu_bar_class_init        (GtkMenuBarClass *klass);
45 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
46                                             GtkRequisition  *requisition);
47 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
48                                             GtkAllocation   *allocation);
49 static void gtk_menu_bar_paint             (GtkWidget       *widget,
50                                             GdkRectangle    *area);
51 static gint gtk_menu_bar_expose            (GtkWidget       *widget,
52                                             GdkEventExpose  *event);
53 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
54                                             GtkWidget       *old_toplevel);
55 static gint gtk_menu_bar_get_popup_delay  (GtkMenuShell    *menu_shell);
56                                             
57
58 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
59
60 static GtkMenuShellClass *parent_class = NULL;
61
62 GType
63 gtk_menu_bar_get_type (void)
64 {
65   static GType menu_bar_type = 0;
66
67   if (!menu_bar_type)
68     {
69       static const GTypeInfo menu_bar_info =
70       {
71         sizeof (GtkMenuBarClass),
72         NULL,           /* base_init */
73         NULL,           /* base_finalize */
74         (GClassInitFunc) gtk_menu_bar_class_init,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         sizeof (GtkMenuBar),
78         0,              /* n_preallocs */
79         NULL,           /* instance_init */
80       };
81
82       menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenuBar",
83                                               &menu_bar_info, 0);
84     }
85
86   return menu_bar_type;
87 }
88
89 static void
90 gtk_menu_bar_class_init (GtkMenuBarClass *class)
91 {
92   GtkObjectClass *object_class;
93   GtkWidgetClass *widget_class;
94   GtkMenuShellClass *menu_shell_class;
95
96   GtkBindingSet *binding_set;
97
98   parent_class = g_type_class_peek_parent (class);
99   
100   object_class = (GtkObjectClass*) class;
101   widget_class = (GtkWidgetClass*) class;
102   menu_shell_class = (GtkMenuShellClass*) class;
103
104   widget_class->size_request = gtk_menu_bar_size_request;
105   widget_class->size_allocate = gtk_menu_bar_size_allocate;
106   widget_class->expose_event = gtk_menu_bar_expose;
107   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
108   
109   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
110   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
111
112   binding_set = gtk_binding_set_by_class (class);
113   gtk_binding_entry_add_signal (binding_set,
114                                 GDK_Left, 0,
115                                 "move_current", 1,
116                                 GTK_TYPE_MENU_DIRECTION_TYPE,
117                                 GTK_MENU_DIR_PREV);
118   gtk_binding_entry_add_signal (binding_set,
119                                 GDK_KP_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_Right, 0,
125                                 "move_current", 1,
126                                 GTK_TYPE_MENU_DIRECTION_TYPE,
127                                 GTK_MENU_DIR_NEXT);
128   gtk_binding_entry_add_signal (binding_set,
129                                 GDK_KP_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_Up, 0,
135                                 "move_current", 1,
136                                 GTK_TYPE_MENU_DIRECTION_TYPE,
137                                 GTK_MENU_DIR_PARENT);
138   gtk_binding_entry_add_signal (binding_set,
139                                 GDK_KP_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_Down, 0,
145                                 "move_current", 1,
146                                 GTK_TYPE_MENU_DIRECTION_TYPE,
147                                 GTK_MENU_DIR_CHILD);
148   gtk_binding_entry_add_signal (binding_set,
149                                 GDK_KP_Down, 0,
150                                 "move_current", 1,
151                                 GTK_TYPE_MENU_DIRECTION_TYPE,
152                                 GTK_MENU_DIR_CHILD);
153
154   gtk_widget_class_install_style_property (widget_class,
155                                            g_param_spec_enum ("shadow_type",
156                                                               P_("Shadow type"),
157                                                               P_("Style of bevel around the menubar"),
158                                                               GTK_TYPE_SHADOW_TYPE,
159                                                               GTK_SHADOW_OUT,
160                                                               G_PARAM_READABLE));
161
162   gtk_widget_class_install_style_property (widget_class,
163                                            g_param_spec_int ("internal_padding",
164                                                              P_("Internal padding"),
165                                                              P_("Amount of border space between the menubar shadow and the menu items"),
166                                                              0,
167                                                              G_MAXINT,
168                                                              DEFAULT_IPADDING,
169                                                              G_PARAM_READABLE));
170
171   gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
172                                                    P_("Delay before drop down menus appear"),
173                                                    P_("Delay before the submenus of a menu bar appear"),
174                                                    0,
175                                                    G_MAXINT,
176                                                    0,
177                                                    G_PARAM_READWRITE));
178 }
179  
180 GtkWidget*
181 gtk_menu_bar_new (void)
182 {
183   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
184 }
185
186 static void
187 gtk_menu_bar_size_request (GtkWidget      *widget,
188                            GtkRequisition *requisition)
189 {
190   GtkMenuBar *menu_bar;
191   GtkMenuShell *menu_shell;
192   GtkWidget *child;
193   GList *children;
194   gint nchildren;
195   GtkRequisition child_requisition;
196   gint ipadding;
197
198   g_return_if_fail (GTK_IS_MENU_BAR (widget));
199   g_return_if_fail (requisition != NULL);
200
201   requisition->width = 0;
202   requisition->height = 0;
203   
204   if (GTK_WIDGET_VISIBLE (widget))
205     {
206       menu_bar = GTK_MENU_BAR (widget);
207       menu_shell = GTK_MENU_SHELL (widget);
208
209       nchildren = 0;
210       children = menu_shell->children;
211
212       while (children)
213         {
214           child = children->data;
215           children = children->next;
216
217           if (GTK_WIDGET_VISIBLE (child))
218             {
219               gint toggle_size;
220               
221               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
222               gtk_widget_size_request (child, &child_requisition);
223               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
224                                                  &toggle_size);
225               
226               requisition->width += child_requisition.width;
227               requisition->width += toggle_size;
228               
229               requisition->height = MAX (requisition->height, child_requisition.height);
230               nchildren += 1;
231             }
232         }
233
234       gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
235       
236       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
237                              ipadding + 
238                              BORDER_SPACING) * 2;
239       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
240                               ipadding +
241                               BORDER_SPACING) * 2;
242
243       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
244         {
245           requisition->width += widget->style->xthickness * 2;
246           requisition->height += widget->style->ythickness * 2;
247         }
248     }
249 }
250
251 static void
252 gtk_menu_bar_size_allocate (GtkWidget     *widget,
253                             GtkAllocation *allocation)
254 {
255   GtkMenuBar *menu_bar;
256   GtkMenuShell *menu_shell;
257   GtkWidget *child;
258   GList *children;
259   GtkAllocation child_allocation;
260   GtkRequisition child_requisition;
261   guint offset;
262   GtkTextDirection direction;
263   gint ltr_x;
264   gint ipadding;
265
266   g_return_if_fail (GTK_IS_MENU_BAR (widget));
267   g_return_if_fail (allocation != NULL);
268
269   menu_bar = GTK_MENU_BAR (widget);
270   menu_shell = GTK_MENU_SHELL (widget);
271
272   direction = gtk_widget_get_direction (widget);
273
274   widget->allocation = *allocation;
275   if (GTK_WIDGET_REALIZED (widget))
276     gdk_window_move_resize (widget->window,
277                             allocation->x, allocation->y,
278                             allocation->width, allocation->height);
279
280   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
281   
282   if (menu_shell->children)
283     {
284       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
285                             ipadding + 
286                             BORDER_SPACING);
287       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
288                             BORDER_SPACING);
289
290       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
291         {
292           child_allocation.x += widget->style->xthickness;
293           child_allocation.y += widget->style->ythickness;
294         }
295       
296       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
297
298       offset = child_allocation.x;      /* Window edge to menubar start */
299       ltr_x = child_allocation.x;
300
301       children = menu_shell->children;
302       while (children)
303         {
304           gint toggle_size;          
305
306           child = children->data;
307           children = children->next;
308
309           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
310                                              &toggle_size);
311           gtk_widget_get_child_requisition (child, &child_requisition);
312
313           child_requisition.width += toggle_size;
314           
315           /* Support for the right justified help menu */
316           if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
317               && (GTK_MENU_ITEM(child)->right_justify)) 
318             {
319               ltr_x = allocation->width -
320                 child_requisition.width - offset;
321             }
322           if (GTK_WIDGET_VISIBLE (child))
323             {
324               if (direction == GTK_TEXT_DIR_LTR) 
325                 child_allocation.x = ltr_x;
326               else
327                 child_allocation.x = allocation->width -
328                   child_requisition.width - ltr_x; 
329
330               child_allocation.width = child_requisition.width;
331
332               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
333                                                   toggle_size);
334               gtk_widget_size_allocate (child, &child_allocation);
335
336               ltr_x += child_allocation.width;
337             }
338         }
339     }
340 }
341
342 static void
343 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
344 {
345   g_return_if_fail (GTK_IS_MENU_BAR (widget));
346
347   if (GTK_WIDGET_DRAWABLE (widget))
348     {
349       gint border;
350
351       border = GTK_CONTAINER (widget)->border_width;
352       
353       gtk_paint_box (widget->style,
354                      widget->window,
355                      GTK_WIDGET_STATE (widget),
356                      get_shadow_type (GTK_MENU_BAR (widget)),
357                      area, widget, "menubar",
358                      border, border,
359                      widget->allocation.width - border * 2,
360                      widget->allocation.height - border * 2);
361     }
362 }
363
364 static gint
365 gtk_menu_bar_expose (GtkWidget      *widget,
366                      GdkEventExpose *event)
367 {
368   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
369   g_return_val_if_fail (event != NULL, FALSE);
370
371   if (GTK_WIDGET_DRAWABLE (widget))
372     {
373       gtk_menu_bar_paint (widget, &event->area);
374
375       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
376     }
377
378   return FALSE;
379 }
380
381 static GList *
382 get_menu_bars (GtkWindow *window)
383 {
384   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
385 }
386
387 static GList *
388 get_viewable_menu_bars (GtkWindow *window)
389 {
390   GList *menu_bars;
391   GList *viewable_menu_bars = NULL;
392
393   for (menu_bars = get_menu_bars (window);
394        menu_bars;
395        menu_bars = menu_bars->next)
396     {
397       GtkWidget *widget = menu_bars->data;
398       gboolean viewable = TRUE;
399       
400       while (widget)
401         {
402           if (!GTK_WIDGET_MAPPED (widget))
403             viewable = FALSE;
404           
405           widget = widget->parent;
406         }
407
408       if (viewable)
409         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
410     }
411
412   return g_list_reverse (viewable_menu_bars);
413 }
414
415 static void
416 set_menu_bars (GtkWindow *window,
417                GList     *menubars)
418 {
419   g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
420 }
421
422 static gboolean
423 window_key_press_handler (GtkWidget   *widget,
424                           GdkEventKey *event,
425                           gpointer     data)
426 {
427   gchar *accel = NULL;
428   gboolean retval = FALSE;
429   
430   g_object_get (gtk_widget_get_settings (widget),
431                 "gtk-menu-bar-accel",
432                 &accel,
433                 NULL);
434
435   if (accel)
436     {
437       guint keyval = 0;
438       GdkModifierType mods = 0;
439
440       gtk_accelerator_parse (accel, &keyval, &mods);
441
442       if (keyval == 0)
443         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
444
445       /* FIXME this is wrong, needs to be in the global accel resolution
446        * thing, to properly consider i18n etc., but that probably requires
447        * AccelGroup changes etc.
448        */
449       if (event->keyval == keyval &&
450           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
451            (mods & gtk_accelerator_get_default_mod_mask ())))
452         {
453           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
454           GList *menubars;
455
456           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
457                                                 GTK_DIR_TAB_FORWARD, NULL);
458           g_list_free (tmp_menubars);
459           
460           if (menubars)
461             {
462               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
463
464               _gtk_menu_shell_activate (menu_shell);
465               gtk_menu_shell_select_first (menu_shell, FALSE);
466               
467               g_list_free (menubars);
468               
469               retval = TRUE;          
470             }
471         }
472
473       g_free (accel);
474     }
475
476   return retval;
477 }
478
479 static void
480 add_to_window (GtkWindow  *window,
481                GtkMenuBar *menubar)
482 {
483   GList *menubars = get_menu_bars (window);
484
485   if (!menubars)
486     {
487       g_signal_connect (window,
488                         "key_press_event",
489                         G_CALLBACK (window_key_press_handler),
490                         NULL);
491     }
492
493   set_menu_bars (window, g_list_prepend (menubars, menubar));
494 }
495
496 static void
497 remove_from_window (GtkWindow  *window,
498                     GtkMenuBar *menubar)
499 {
500   GList *menubars = get_menu_bars (window);
501
502   menubars = g_object_get_data (G_OBJECT (window),
503                                     "gtk-menu-bar-list");
504
505   menubars = g_list_remove (menubars, menubar);
506
507   if (!menubars)
508     {
509       g_signal_handlers_disconnect_by_func (window,
510                                             window_key_press_handler,
511                                             NULL);
512     }
513
514   set_menu_bars (window, menubars);
515 }
516
517 static void
518 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
519                                 GtkWidget *old_toplevel)
520 {
521   GtkWidget *toplevel;  
522   GtkMenuBar *menubar;
523
524   menubar = GTK_MENU_BAR (widget);
525
526   toplevel = gtk_widget_get_toplevel (widget);
527
528   if (old_toplevel)
529     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
530   
531   if (GTK_WIDGET_TOPLEVEL (toplevel))
532     add_to_window (GTK_WINDOW (toplevel), menubar);
533 }
534
535 /**
536  * _gtk_menu_bar_cycle_focus:
537  * @menubar: a #GtkMenuBar
538  * @dir: direction in which to cycle the focus
539  * 
540  * Move the focus between menubars in the toplevel.
541  **/
542 void
543 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
544                            GtkDirectionType  dir)
545 {
546   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
547   GtkMenuItem *to_activate = NULL;
548
549   if (GTK_WIDGET_TOPLEVEL (toplevel))
550     {
551       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
552       GList *menubars;
553       GList *current;
554
555       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
556                                             dir, GTK_WIDGET (menubar));
557       g_list_free (tmp_menubars);
558
559       if (menubars)
560         {
561           current = g_list_find (menubars, menubar);
562
563           if (current && current->next)
564             {
565               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
566               if (new_menushell->children)
567                 to_activate = new_menushell->children->data;
568             }
569         }
570           
571       g_list_free (menubars);
572     }
573
574   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
575
576   if (to_activate)
577     g_signal_emit_by_name (to_activate, "activate_item");
578 }
579
580 static GtkShadowType
581 get_shadow_type (GtkMenuBar *menubar)
582 {
583   GtkShadowType shadow_type = GTK_SHADOW_OUT;
584   
585   gtk_widget_style_get (GTK_WIDGET (menubar),
586                         "shadow_type", &shadow_type,
587                         NULL);
588
589   return shadow_type;
590 }
591
592 static gint
593 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
594 {
595   gint popup_delay;
596   
597   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
598                 "gtk-menu-bar-popup-delay", &popup_delay,
599                 NULL);
600
601   return popup_delay;
602 }