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