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