]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Added gdk_text/string_extents() - too calculate all the metrics at once of
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gdk/gdkkeysyms.h"
20 #include "gtkbindings.h"
21 #include "gtkmain.h"
22 #include "gtkmenubar.h"
23 #include "gtkmenuitem.h"
24
25
26 #define BORDER_SPACING  2
27 #define CHILD_SPACING   3
28
29
30 static void gtk_menu_bar_class_init    (GtkMenuBarClass *klass);
31 static void gtk_menu_bar_init          (GtkMenuBar      *menu_bar);
32 static void gtk_menu_bar_size_request  (GtkWidget       *widget,
33                                         GtkRequisition  *requisition);
34 static void gtk_menu_bar_size_allocate (GtkWidget       *widget,
35                                         GtkAllocation   *allocation);
36 static void gtk_menu_bar_paint         (GtkWidget       *widget);
37 static void gtk_menu_bar_draw          (GtkWidget       *widget,
38                                         GdkRectangle    *area);
39 static gint gtk_menu_bar_expose        (GtkWidget       *widget,
40                                         GdkEventExpose  *event);
41
42
43 GtkType
44 gtk_menu_bar_get_type (void)
45 {
46   static GtkType menu_bar_type = 0;
47
48   if (!menu_bar_type)
49     {
50       GtkTypeInfo menu_bar_info =
51       {
52         "GtkMenuBar",
53         sizeof (GtkMenuBar),
54         sizeof (GtkMenuBarClass),
55         (GtkClassInitFunc) gtk_menu_bar_class_init,
56         (GtkObjectInitFunc) gtk_menu_bar_init,
57         /* reserved_1 */ NULL,
58         /* reserved_2 */ NULL,
59         (GtkClassInitFunc) NULL,
60       };
61
62       menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
63     }
64
65   return menu_bar_type;
66 }
67
68 static void
69 gtk_menu_bar_class_init (GtkMenuBarClass *class)
70 {
71   GtkWidgetClass *widget_class;
72   GtkMenuShellClass *menu_shell_class;
73
74   GtkBindingSet *binding_set;
75
76   widget_class = (GtkWidgetClass*) class;
77   menu_shell_class = (GtkMenuShellClass*) class;
78
79   widget_class->draw = gtk_menu_bar_draw;
80   widget_class->size_request = gtk_menu_bar_size_request;
81   widget_class->size_allocate = gtk_menu_bar_size_allocate;
82   widget_class->expose_event = gtk_menu_bar_expose;
83
84   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
85
86   binding_set = gtk_binding_set_by_class (class);
87   gtk_binding_entry_add_signal (binding_set,
88                                 GDK_Left, 0,
89                                 "move_current", 1,
90                                 GTK_TYPE_MENU_DIRECTION_TYPE,
91                                 GTK_MENU_DIR_PREV);
92   gtk_binding_entry_add_signal (binding_set,
93                                 GDK_Right, 0,
94                                 "move_current", 1,
95                                 GTK_TYPE_MENU_DIRECTION_TYPE,
96                                 GTK_MENU_DIR_NEXT);
97   gtk_binding_entry_add_signal (binding_set,
98                                 GDK_Up, 0,
99                                 "move_current", 1,
100                                 GTK_TYPE_MENU_DIRECTION_TYPE,
101                                 GTK_MENU_DIR_PARENT);
102   gtk_binding_entry_add_signal (binding_set,
103                                 GDK_Down, 0,
104                                 "move_current", 1,
105                                 GTK_TYPE_MENU_DIRECTION_TYPE,
106                                 GTK_MENU_DIR_CHILD);
107 }
108
109 static void
110 gtk_menu_bar_init (GtkMenuBar *menu_bar)
111 {
112 }
113
114 GtkWidget*
115 gtk_menu_bar_new (void)
116 {
117   return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
118 }
119
120 void
121 gtk_menu_bar_append (GtkMenuBar *menu_bar,
122                      GtkWidget  *child)
123 {
124   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
125 }
126
127 void
128 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
129                       GtkWidget  *child)
130 {
131   gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
132 }
133
134 void
135 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
136                      GtkWidget  *child,
137                      gint        position)
138 {
139   gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
140 }
141
142
143 static void
144 gtk_menu_bar_size_request (GtkWidget      *widget,
145                            GtkRequisition *requisition)
146 {
147   GtkMenuBar *menu_bar;
148   GtkMenuShell *menu_shell;
149   GtkWidget *child;
150   GList *children;
151   gint nchildren;
152
153   g_return_if_fail (widget != NULL);
154   g_return_if_fail (GTK_IS_MENU_BAR (widget));
155   g_return_if_fail (requisition != NULL);
156
157   requisition->width = 0;
158   requisition->height = 0;
159
160   if (GTK_WIDGET_VISIBLE (widget))
161     {
162       menu_bar = GTK_MENU_BAR (widget);
163       menu_shell = GTK_MENU_SHELL (widget);
164
165       nchildren = 0;
166       children = menu_shell->children;
167
168       while (children)
169         {
170           child = children->data;
171           children = children->next;
172
173           if (GTK_WIDGET_VISIBLE (child))
174             {
175               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
176               gtk_widget_size_request (child, &child->requisition);
177
178               requisition->width += child->requisition.width;
179               requisition->height = MAX (requisition->height, child->requisition.height);
180               /* Support for the right justified help menu */
181               if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
182                    && (GTK_MENU_ITEM(child)->right_justify))
183                 {
184                   requisition->width += CHILD_SPACING;
185                 }
186
187               nchildren += 1;
188             }
189         }
190
191       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
192                              widget->style->klass->xthickness +
193                              BORDER_SPACING) * 2;
194       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
195                               widget->style->klass->ythickness +
196                               BORDER_SPACING) * 2;
197
198       if (nchildren > 0)
199         requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
200     }
201 }
202
203 static void
204 gtk_menu_bar_size_allocate (GtkWidget     *widget,
205                             GtkAllocation *allocation)
206 {
207   GtkMenuBar *menu_bar;
208   GtkMenuShell *menu_shell;
209   GtkWidget *child;
210   GList *children;
211   GtkAllocation child_allocation;
212   guint offset;
213   
214   g_return_if_fail (widget != NULL);
215   g_return_if_fail (GTK_IS_MENU_BAR (widget));
216   g_return_if_fail (allocation != NULL);
217
218   menu_bar = GTK_MENU_BAR (widget);
219   menu_shell = GTK_MENU_SHELL (widget);
220
221   widget->allocation = *allocation;
222   if (GTK_WIDGET_REALIZED (widget))
223     gdk_window_move_resize (widget->window,
224                             allocation->x, allocation->y,
225                             allocation->width, allocation->height);
226
227   if (menu_shell->children)
228     {
229       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
230                             widget->style->klass->xthickness +
231                             BORDER_SPACING);
232       offset = child_allocation.x;      /* Window edge to menubar start */
233
234       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
235                             widget->style->klass->ythickness +
236                             BORDER_SPACING);
237       child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2);
238
239       children = menu_shell->children;
240       while (children)
241         {
242           child = children->data;
243           children = children->next;
244
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               child_allocation.x = allocation->width -
250                   child->requisition.width - CHILD_SPACING - offset;
251             }
252           if (GTK_WIDGET_VISIBLE (child))
253             {
254               child_allocation.width = child->requisition.width;
255
256               gtk_widget_size_allocate (child, &child_allocation);
257
258               child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
259             }
260         }
261     }
262 }
263
264 static void
265 gtk_menu_bar_paint (GtkWidget *widget)
266 {
267   g_return_if_fail (widget != NULL);
268   g_return_if_fail (GTK_IS_MENU_BAR (widget));
269
270   if (GTK_WIDGET_DRAWABLE (widget))
271     {
272       gtk_draw_shadow (widget->style,
273                        widget->window,
274                        GTK_STATE_NORMAL,
275                        GTK_SHADOW_OUT,
276                        0, 0,
277                        widget->allocation.width,
278                        widget->allocation.height);
279     }
280 }
281
282 static void
283 gtk_menu_bar_draw (GtkWidget    *widget,
284                    GdkRectangle *area)
285 {
286   GtkMenuShell *menu_shell;
287   GtkWidget *child;
288   GdkRectangle child_area;
289   GList *children;
290
291   g_return_if_fail (widget != NULL);
292   g_return_if_fail (GTK_IS_MENU_BAR (widget));
293   g_return_if_fail (area != NULL);
294
295   if (GTK_WIDGET_DRAWABLE (widget))
296     {
297       gtk_menu_bar_paint (widget);
298
299       menu_shell = GTK_MENU_SHELL (widget);
300
301       children = menu_shell->children;
302       while (children)
303         {
304           child = children->data;
305           children = children->next;
306
307           if (gtk_widget_intersect (child, area, &child_area))
308             gtk_widget_draw (child, &child_area);
309         }
310     }
311 }
312
313 static gint
314 gtk_menu_bar_expose (GtkWidget      *widget,
315                      GdkEventExpose *event)
316 {
317   GtkMenuShell *menu_shell;
318   GtkWidget *child;
319   GdkEventExpose child_event;
320   GList *children;
321
322   g_return_val_if_fail (widget != NULL, FALSE);
323   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
324   g_return_val_if_fail (event != NULL, FALSE);
325
326   if (GTK_WIDGET_DRAWABLE (widget))
327     {
328       gtk_menu_bar_paint (widget);
329
330       menu_shell = GTK_MENU_SHELL (widget);
331       child_event = *event;
332
333       children = menu_shell->children;
334       while (children)
335         {
336           child = children->data;
337           children = children->next;
338
339           if (GTK_WIDGET_NO_WINDOW (child) &&
340               gtk_widget_intersect (child, &event->area, &child_event.area))
341             gtk_widget_event (child, (GdkEvent*) &child_event);
342         }
343     }
344
345   return FALSE;
346 }