]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
e6e7baf2dd1dace247a74becad208a9285b0016b
[~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
33 enum {
34   ARG_0,
35   ARG_SHADOW
36 };
37
38
39 #define BORDER_SPACING  0
40 #define CHILD_SPACING   3
41
42
43 static void gtk_menu_bar_class_init    (GtkMenuBarClass *klass);
44 static void gtk_menu_bar_init          (GtkMenuBar      *menu_bar);
45 static void gtk_menu_bar_set_arg       (GtkObject      *object,
46                                         GtkArg         *arg,
47                                         guint           arg_id);
48 static void gtk_menu_bar_get_arg       (GtkObject      *object,
49                                         GtkArg         *arg,
50                                         guint           arg_id);
51 static void gtk_menu_bar_size_request  (GtkWidget       *widget,
52                                         GtkRequisition  *requisition);
53 static void gtk_menu_bar_size_allocate (GtkWidget       *widget,
54                                         GtkAllocation   *allocation);
55 static void gtk_menu_bar_paint         (GtkWidget       *widget,
56                                         GdkRectangle    *area);
57 static gint gtk_menu_bar_expose        (GtkWidget       *widget,
58                                         GdkEventExpose  *event);
59
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) gtk_menu_bar_init,
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   object_class = (GtkObjectClass*) class;
96   widget_class = (GtkWidgetClass*) class;
97   menu_shell_class = (GtkMenuShellClass*) class;
98
99   gtk_object_add_arg_type ("GtkMenuBar::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
100
101   object_class->set_arg = gtk_menu_bar_set_arg;
102   object_class->get_arg = gtk_menu_bar_get_arg;
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
108   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
109
110   binding_set = gtk_binding_set_by_class (class);
111   gtk_binding_entry_add_signal (binding_set,
112                                 GDK_Left, 0,
113                                 "move_current", 1,
114                                 GTK_TYPE_MENU_DIRECTION_TYPE,
115                                 GTK_MENU_DIR_PREV);
116   gtk_binding_entry_add_signal (binding_set,
117                                 GDK_Right, 0,
118                                 "move_current", 1,
119                                 GTK_TYPE_MENU_DIRECTION_TYPE,
120                                 GTK_MENU_DIR_NEXT);
121   gtk_binding_entry_add_signal (binding_set,
122                                 GDK_Up, 0,
123                                 "move_current", 1,
124                                 GTK_TYPE_MENU_DIRECTION_TYPE,
125                                 GTK_MENU_DIR_PARENT);
126   gtk_binding_entry_add_signal (binding_set,
127                                 GDK_Down, 0,
128                                 "move_current", 1,
129                                 GTK_TYPE_MENU_DIRECTION_TYPE,
130                                 GTK_MENU_DIR_CHILD);
131 }
132
133 static void
134 gtk_menu_bar_init (GtkMenuBar *menu_bar)
135 {
136   menu_bar->shadow_type = GTK_SHADOW_OUT;
137 }
138
139 static void
140 gtk_menu_bar_set_arg (GtkObject      *object,
141                       GtkArg         *arg,
142                       guint           arg_id)
143 {
144   GtkMenuBar *menu_bar;
145
146   menu_bar = GTK_MENU_BAR (object);
147
148   switch (arg_id)
149     {
150     case ARG_SHADOW:
151       gtk_menu_bar_set_shadow_type (menu_bar, GTK_VALUE_ENUM (*arg));
152       break;
153     default:
154       break;
155     }
156 }
157
158 static void
159 gtk_menu_bar_get_arg (GtkObject      *object,
160                       GtkArg         *arg,
161                       guint           arg_id)
162 {
163   GtkMenuBar *menu_bar;
164
165   menu_bar = GTK_MENU_BAR (object);
166
167   switch (arg_id)
168     {
169     case ARG_SHADOW:
170       GTK_VALUE_ENUM (*arg) = menu_bar->shadow_type;
171       break;
172     default:
173       arg->type = GTK_TYPE_INVALID;
174       break;
175     }
176 }
177  
178 GtkWidget*
179 gtk_menu_bar_new (void)
180 {
181   return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
182 }
183
184 void
185 gtk_menu_bar_append (GtkMenuBar *menu_bar,
186                      GtkWidget  *child)
187 {
188   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
189 }
190
191 void
192 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
193                       GtkWidget  *child)
194 {
195   gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
196 }
197
198 void
199 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
200                      GtkWidget  *child,
201                      gint        position)
202 {
203   gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
204 }
205
206
207 static void
208 gtk_menu_bar_size_request (GtkWidget      *widget,
209                            GtkRequisition *requisition)
210 {
211   GtkMenuBar *menu_bar;
212   GtkMenuShell *menu_shell;
213   GtkWidget *child;
214   GList *children;
215   gint nchildren;
216   GtkRequisition child_requisition;
217
218   g_return_if_fail (widget != NULL);
219   g_return_if_fail (GTK_IS_MENU_BAR (widget));
220   g_return_if_fail (requisition != NULL);
221
222   requisition->width = 0;
223   requisition->height = 0;
224
225   if (GTK_WIDGET_VISIBLE (widget))
226     {
227       menu_bar = GTK_MENU_BAR (widget);
228       menu_shell = GTK_MENU_SHELL (widget);
229
230       nchildren = 0;
231       children = menu_shell->children;
232
233       while (children)
234         {
235           child = children->data;
236           children = children->next;
237
238           if (GTK_WIDGET_VISIBLE (child))
239             {
240               gint toggle_size;
241               
242               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
243               gtk_widget_size_request (child, &child_requisition);
244               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
245                                                  &toggle_size);
246               
247               requisition->width += child_requisition.width;
248               requisition->width += toggle_size;
249               
250               requisition->height = MAX (requisition->height, child_requisition.height);
251               /* Support for the right justified help menu */
252               if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
253                   GTK_MENU_ITEM(child)->right_justify)
254                 {
255                   requisition->width += CHILD_SPACING;
256                 }
257
258               nchildren += 1;
259             }
260         }
261
262       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
263                              widget->style->xthickness +
264                              BORDER_SPACING) * 2;
265       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
266                               widget->style->ythickness +
267                               BORDER_SPACING) * 2;
268
269       if (nchildren > 0)
270         requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
271     }
272 }
273
274 static void
275 gtk_menu_bar_size_allocate (GtkWidget     *widget,
276                             GtkAllocation *allocation)
277 {
278   GtkMenuBar *menu_bar;
279   GtkMenuShell *menu_shell;
280   GtkWidget *child;
281   GList *children;
282   GtkAllocation child_allocation;
283   GtkRequisition child_requisition;
284   guint offset;
285   
286   g_return_if_fail (widget != NULL);
287   g_return_if_fail (GTK_IS_MENU_BAR (widget));
288   g_return_if_fail (allocation != NULL);
289
290   menu_bar = GTK_MENU_BAR (widget);
291   menu_shell = GTK_MENU_SHELL (widget);
292
293   widget->allocation = *allocation;
294   if (GTK_WIDGET_REALIZED (widget))
295     gdk_window_move_resize (widget->window,
296                             allocation->x, allocation->y,
297                             allocation->width, allocation->height);
298
299   if (menu_shell->children)
300     {
301       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
302                             widget->style->xthickness +
303                             BORDER_SPACING);
304       offset = child_allocation.x;      /* Window edge to menubar start */
305
306       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
307                             widget->style->ythickness +
308                             BORDER_SPACING);
309       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
310
311       children = menu_shell->children;
312       while (children)
313         {
314           gint toggle_size;          
315
316           child = children->data;
317           children = children->next;
318
319           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
320                                              &toggle_size);
321           gtk_widget_get_child_requisition (child, &child_requisition);
322
323           child_requisition.width += toggle_size;
324           
325           /* Support for the right justified help menu */
326           if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
327               && (GTK_MENU_ITEM(child)->right_justify)) 
328             {
329               child_allocation.x = allocation->width -
330                   child_requisition.width - CHILD_SPACING - offset;
331             }
332           if (GTK_WIDGET_VISIBLE (child))
333             {
334               child_allocation.width = child_requisition.width;
335
336               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
337                                                   toggle_size);
338               gtk_widget_size_allocate (child, &child_allocation);
339
340               child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
341             }
342         }
343     }
344 }
345
346 void
347 gtk_menu_bar_set_shadow_type (GtkMenuBar    *menu_bar,
348                               GtkShadowType  type)
349 {
350   g_return_if_fail (menu_bar != NULL);
351   g_return_if_fail (GTK_IS_MENU_BAR (menu_bar));
352
353   if ((GtkShadowType) menu_bar->shadow_type != type)
354     {
355       menu_bar->shadow_type = type;
356
357       if (GTK_WIDGET_DRAWABLE (menu_bar))
358         {
359           gtk_widget_queue_clear (GTK_WIDGET (menu_bar));
360         }
361       gtk_widget_queue_resize (GTK_WIDGET (menu_bar));
362     }
363 }
364
365 static void
366 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
367 {
368   g_return_if_fail (widget != NULL);
369   g_return_if_fail (GTK_IS_MENU_BAR (widget));
370
371   if (GTK_WIDGET_DRAWABLE (widget))
372     {
373       gtk_paint_box (widget->style,
374                      widget->window,
375                      GTK_STATE_NORMAL,
376                      GTK_MENU_BAR (widget)->shadow_type,
377                      area, widget, "menubar",
378                      0, 0,
379                      -1,-1);
380     }
381 }
382
383 static gint
384 gtk_menu_bar_expose (GtkWidget      *widget,
385                      GdkEventExpose *event)
386 {
387   GtkMenuShell *menu_shell;
388   GdkEventExpose child_event;
389   GList *children;
390   GtkWidget *child;
391
392   g_return_val_if_fail (widget != NULL, FALSE);
393   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
394   g_return_val_if_fail (event != NULL, FALSE);
395
396   if (GTK_WIDGET_DRAWABLE (widget))
397     {
398       gtk_menu_bar_paint (widget, &event->area);
399
400       menu_shell = GTK_MENU_SHELL (widget);
401       child_event = *event;
402
403       children = menu_shell->children;
404       while (children)
405         {
406           child = children->data;
407           children = children->next;
408
409           if (GTK_WIDGET_NO_WINDOW (child) &&
410               gtk_widget_intersect (child, &event->area, &child_event.area))
411             gtk_widget_event (child, (GdkEvent*) &child_event);
412         }
413     }
414
415   return FALSE;
416 }