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