]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
fix warning
[~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 "gdk/gdkkeysyms.h"
28 #include "gtkbindings.h"
29 #include "gtkmain.h"
30 #include "gtkmenubar.h"
31 #include "gtkmenuitem.h"
32 #include "gtksettings.h"
33 #include "gtkintl.h"
34 #include "gtkwindow.h"
35 #include "gtksignal.h"
36
37 enum {
38   ARG_0,
39   ARG_SHADOW
40 };
41
42
43 #define BORDER_SPACING  0
44 #define CHILD_SPACING   3
45
46
47 static void gtk_menu_bar_class_init    (GtkMenuBarClass *klass);
48 static void gtk_menu_bar_size_request  (GtkWidget       *widget,
49                                         GtkRequisition  *requisition);
50 static void gtk_menu_bar_size_allocate (GtkWidget       *widget,
51                                         GtkAllocation   *allocation);
52 static void gtk_menu_bar_paint         (GtkWidget       *widget,
53                                         GdkRectangle    *area);
54 static gint gtk_menu_bar_expose        (GtkWidget       *widget,
55                                         GdkEventExpose  *event);
56 static void gtk_menu_bar_hierarchy_changed (GtkWidget   *widget);                                            
57 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
58
59 static GtkMenuShellClass *parent_class = NULL;
60
61 GtkType
62 gtk_menu_bar_get_type (void)
63 {
64   static GtkType menu_bar_type = 0;
65
66   if (!menu_bar_type)
67     {
68       static const GtkTypeInfo menu_bar_info =
69       {
70         "GtkMenuBar",
71         sizeof (GtkMenuBar),
72         sizeof (GtkMenuBarClass),
73         (GtkClassInitFunc) gtk_menu_bar_class_init,
74         (GtkObjectInitFunc) NULL,
75         /* reserved_1 */ NULL,
76         /* reserved_2 */ NULL,
77         (GtkClassInitFunc) NULL,
78       };
79
80       menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
81     }
82
83   return menu_bar_type;
84 }
85
86 static void
87 gtk_menu_bar_class_init (GtkMenuBarClass *class)
88 {
89   GtkObjectClass *object_class;
90   GtkWidgetClass *widget_class;
91   GtkMenuShellClass *menu_shell_class;
92
93   GtkBindingSet *binding_set;
94
95   parent_class = g_type_class_peek_parent (class);
96   
97   object_class = (GtkObjectClass*) class;
98   widget_class = (GtkWidgetClass*) class;
99   menu_shell_class = (GtkMenuShellClass*) class;
100
101   widget_class->size_request = gtk_menu_bar_size_request;
102   widget_class->size_allocate = gtk_menu_bar_size_allocate;
103   widget_class->expose_event = gtk_menu_bar_expose;
104   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
105   
106   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
107
108   binding_set = gtk_binding_set_by_class (class);
109   gtk_binding_entry_add_signal (binding_set,
110                                 GDK_Left, 0,
111                                 "move_current", 1,
112                                 GTK_TYPE_MENU_DIRECTION_TYPE,
113                                 GTK_MENU_DIR_PREV);
114   gtk_binding_entry_add_signal (binding_set,
115                                 GDK_Right, 0,
116                                 "move_current", 1,
117                                 GTK_TYPE_MENU_DIRECTION_TYPE,
118                                 GTK_MENU_DIR_NEXT);
119   gtk_binding_entry_add_signal (binding_set,
120                                 GDK_Up, 0,
121                                 "move_current", 1,
122                                 GTK_TYPE_MENU_DIRECTION_TYPE,
123                                 GTK_MENU_DIR_PARENT);
124   gtk_binding_entry_add_signal (binding_set,
125                                 GDK_Down, 0,
126                                 "move_current", 1,
127                                 GTK_TYPE_MENU_DIRECTION_TYPE,
128                                 GTK_MENU_DIR_CHILD);
129
130   gtk_settings_install_property (gtk_settings_get_global (),
131                                  g_param_spec_string ("gtk-menu-bar-accel",
132                                                       _("Menu bar accelerator"),
133                                                       _("Keybinding to activate the menu bar"),
134                                                       "F10",
135                                                       G_PARAM_READWRITE));
136
137   gtk_widget_class_install_style_property (widget_class,
138                                            g_param_spec_enum ("shadow_type",
139                                                               _("Shadow type"),
140                                                               _("Style of bevel around the menubar"),
141                                                               GTK_TYPE_SHADOW_TYPE,
142                                                               GTK_SHADOW_OUT,
143                                                               G_PARAM_READABLE));
144 }
145  
146 GtkWidget*
147 gtk_menu_bar_new (void)
148 {
149   return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
150 }
151
152 void
153 gtk_menu_bar_append (GtkMenuBar *menu_bar,
154                      GtkWidget  *child)
155 {
156   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
157 }
158
159 void
160 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
161                       GtkWidget  *child)
162 {
163   gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
164 }
165
166 void
167 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
168                      GtkWidget  *child,
169                      gint        position)
170 {
171   gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
172 }
173
174
175 static void
176 gtk_menu_bar_size_request (GtkWidget      *widget,
177                            GtkRequisition *requisition)
178 {
179   GtkMenuBar *menu_bar;
180   GtkMenuShell *menu_shell;
181   GtkWidget *child;
182   GList *children;
183   gint nchildren;
184   GtkRequisition child_requisition;
185
186   g_return_if_fail (widget != NULL);
187   g_return_if_fail (GTK_IS_MENU_BAR (widget));
188   g_return_if_fail (requisition != NULL);
189
190   requisition->width = 0;
191   requisition->height = 0;
192
193   if (GTK_WIDGET_VISIBLE (widget))
194     {
195       menu_bar = GTK_MENU_BAR (widget);
196       menu_shell = GTK_MENU_SHELL (widget);
197
198       nchildren = 0;
199       children = menu_shell->children;
200
201       while (children)
202         {
203           child = children->data;
204           children = children->next;
205
206           if (GTK_WIDGET_VISIBLE (child))
207             {
208               gint toggle_size;
209               
210               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
211               gtk_widget_size_request (child, &child_requisition);
212               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
213                                                  &toggle_size);
214               
215               requisition->width += child_requisition.width;
216               requisition->width += toggle_size;
217               
218               requisition->height = MAX (requisition->height, child_requisition.height);
219               /* Support for the right justified help menu */
220               if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
221                   GTK_MENU_ITEM(child)->right_justify)
222                 {
223                   requisition->width += CHILD_SPACING;
224                 }
225
226               nchildren += 1;
227             }
228         }
229
230       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
231                              widget->style->xthickness +
232                              BORDER_SPACING) * 2;
233       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
234                               widget->style->ythickness +
235                               BORDER_SPACING) * 2;
236
237       if (nchildren > 0)
238         requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
239     }
240 }
241
242 static void
243 gtk_menu_bar_size_allocate (GtkWidget     *widget,
244                             GtkAllocation *allocation)
245 {
246   GtkMenuBar *menu_bar;
247   GtkMenuShell *menu_shell;
248   GtkWidget *child;
249   GList *children;
250   GtkAllocation child_allocation;
251   GtkRequisition child_requisition;
252   guint offset;
253   
254   g_return_if_fail (widget != NULL);
255   g_return_if_fail (GTK_IS_MENU_BAR (widget));
256   g_return_if_fail (allocation != NULL);
257
258   menu_bar = GTK_MENU_BAR (widget);
259   menu_shell = GTK_MENU_SHELL (widget);
260
261   widget->allocation = *allocation;
262   if (GTK_WIDGET_REALIZED (widget))
263     gdk_window_move_resize (widget->window,
264                             allocation->x, allocation->y,
265                             allocation->width, allocation->height);
266
267   if (menu_shell->children)
268     {
269       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
270                             widget->style->xthickness +
271                             BORDER_SPACING);
272       offset = child_allocation.x;      /* Window edge to menubar start */
273
274       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
275                             widget->style->ythickness +
276                             BORDER_SPACING);
277       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
278
279       children = menu_shell->children;
280       while (children)
281         {
282           gint toggle_size;          
283
284           child = children->data;
285           children = children->next;
286
287           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
288                                              &toggle_size);
289           gtk_widget_get_child_requisition (child, &child_requisition);
290
291           child_requisition.width += toggle_size;
292           
293           /* Support for the right justified help menu */
294           if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
295               && (GTK_MENU_ITEM(child)->right_justify)) 
296             {
297               child_allocation.x = allocation->width -
298                   child_requisition.width - CHILD_SPACING - offset;
299             }
300           if (GTK_WIDGET_VISIBLE (child))
301             {
302               child_allocation.width = child_requisition.width;
303
304               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
305                                                   toggle_size);
306               gtk_widget_size_allocate (child, &child_allocation);
307
308               child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
309             }
310         }
311     }
312 }
313
314 static void
315 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
316 {
317   g_return_if_fail (widget != NULL);
318   g_return_if_fail (GTK_IS_MENU_BAR (widget));
319
320   if (GTK_WIDGET_DRAWABLE (widget))
321     {
322       gtk_paint_box (widget->style,
323                      widget->window,
324                      GTK_STATE_NORMAL,
325                      get_shadow_type (GTK_MENU_BAR (widget)),
326                      area, widget, "menubar",
327                      0, 0,
328                      -1,-1);
329     }
330 }
331
332 static gint
333 gtk_menu_bar_expose (GtkWidget      *widget,
334                      GdkEventExpose *event)
335 {
336   g_return_val_if_fail (widget != NULL, FALSE);
337   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
338   g_return_val_if_fail (event != NULL, FALSE);
339
340   if (GTK_WIDGET_DRAWABLE (widget))
341     {
342       gtk_menu_bar_paint (widget, &event->area);
343
344       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
345     }
346
347   return FALSE;
348 }
349
350 static gboolean
351 window_key_press_handler (GtkWidget   *widget,
352                           GdkEventKey *event,
353                           gpointer     data)
354 {
355   gchar *accel = NULL;
356   gboolean retval = FALSE;
357   
358   g_object_get (G_OBJECT (gtk_settings_get_global ()),
359                 "gtk-menu-bar-accel",
360                 &accel,
361                 NULL);
362
363   if (accel)
364     {
365       guint keyval = 0;
366       GdkModifierType mods = 0;
367
368       gtk_accelerator_parse (accel, &keyval, &mods);
369
370       if (keyval == 0)
371         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
372
373       /* FIXME this is wrong, needs to be in the global accel resolution
374        * thing, to properly consider i18n etc., but that probably requires
375        * AccelGroup changes etc.
376        */
377       if (event->keyval == keyval &&
378           (mods & event->state) == mods)
379         {
380           GtkMenuBar *menubar;
381           GtkMenuShell *menushell;
382           
383           menubar = GTK_MENU_BAR (data);
384           menushell = GTK_MENU_SHELL (menubar);
385
386           if (menushell->children)
387             {
388               gtk_signal_emit_by_name (GTK_OBJECT (menushell->children->data),
389                                        "activate_item");
390               
391               retval = TRUE;
392             }
393         }
394
395       g_free (accel);
396     }
397
398   return retval;
399 }
400
401 static void
402 add_to_window (GtkWindow  *window,
403                GtkMenuBar *menubar)
404 {
405   GtkMenuBar *old_menubar;
406
407   old_menubar = g_object_get_data (G_OBJECT (window),
408                                    "gtk-menu-bar");
409   
410   if (old_menubar)
411     return; /* ignore this case; app programmer on crack, but
412              * shouldn't spew stuff, just don't support the accel
413              * for menubar #2
414              */
415
416   g_object_set_data (G_OBJECT (window),
417                      "gtk-menu-bar",
418                      menubar);
419
420   g_signal_connect_data (G_OBJECT (window),
421                          "key_press_event",
422                          G_CALLBACK (window_key_press_handler),
423                          menubar,
424                          NULL, FALSE, FALSE);
425
426   menubar->toplevel = GTK_WIDGET (window);
427 }
428
429 /* Hack-around */
430 #define g_signal_handlers_disconnect_by_func(obj, func, data) g_signal_handlers_disconnect_matched (obj, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL, func, data)
431
432 static void
433 remove_from_window (GtkWindow  *window,
434                     GtkMenuBar *menubar)
435 {
436   g_return_if_fail (menubar->toplevel == GTK_WIDGET (window));
437
438   g_signal_handlers_disconnect_by_func (G_OBJECT (window),
439                                         G_CALLBACK (window_key_press_handler),
440                                         menubar);
441
442   /* dnotify zeroes menubar->toplevel */
443   g_object_set_data (G_OBJECT (window),
444                      "gtk-menu-bar",
445                      NULL);
446
447   menubar->toplevel = NULL;
448 }
449
450 static void
451 gtk_menu_bar_hierarchy_changed (GtkWidget *widget)
452 {
453   GtkWidget *toplevel;  
454   GtkMenuBar *menubar;
455
456   menubar = GTK_MENU_BAR (widget);
457
458   toplevel = gtk_widget_get_toplevel (widget);
459
460   if (menubar->toplevel &&
461       toplevel != menubar->toplevel)
462     {
463       remove_from_window (GTK_WINDOW (menubar->toplevel),
464                           menubar);
465     }
466   
467   if (toplevel &&
468       GTK_IS_WINDOW (toplevel))
469     {
470       add_to_window (GTK_WINDOW (toplevel),
471                      menubar);
472     }
473 }
474
475 static GtkShadowType
476 get_shadow_type (GtkMenuBar *menubar)
477 {
478   GtkShadowType shadow_type = GTK_SHADOW_OUT;
479   
480   gtk_widget_style_get (GTK_WIDGET (menubar),
481                         "shadow_type", &shadow_type,
482                         NULL);
483
484
485   return shadow_type;
486 }