]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenushell.c
Initial revision
[~andy/gtk] / gtk / gtkmenushell.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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include "gtkmain.h"
19 #include "gtkmenuitem.h"
20 #include "gtkmenushell.h"
21 #include "gtksignal.h"
22
23
24 #define MENU_SHELL_TIMEOUT   500
25 #define MENU_SHELL_CLASS(w)  GTK_MENU_SHELL_CLASS (GTK_OBJECT (w)->klass)
26
27
28 enum {
29   DEACTIVATE,
30   LAST_SIGNAL
31 };
32
33
34 static void gtk_menu_shell_class_init        (GtkMenuShellClass *klass);
35 static void gtk_menu_shell_init              (GtkMenuShell      *menu_shell);
36 static void gtk_menu_shell_destroy           (GtkObject         *object);
37 static void gtk_menu_shell_map               (GtkWidget         *widget);
38 static void gtk_menu_shell_realize           (GtkWidget         *widget);
39 static gint gtk_menu_shell_button_press      (GtkWidget         *widget,
40                                               GdkEventButton    *event);
41 static gint gtk_menu_shell_button_release    (GtkWidget         *widget,
42                                               GdkEventButton    *event);
43 static gint gtk_menu_shell_enter_notify      (GtkWidget         *widget,
44                                               GdkEventCrossing  *event);
45 static gint gtk_menu_shell_leave_notify      (GtkWidget         *widget,
46                                               GdkEventCrossing  *event);
47 static void gtk_menu_shell_add               (GtkContainer      *container,
48                                               GtkWidget         *widget);
49 static void gtk_menu_shell_remove            (GtkContainer      *container,
50                                               GtkWidget         *widget);
51 static void gtk_menu_shell_foreach           (GtkContainer      *container,
52                                               GtkCallback        callback,
53                                               gpointer           callback_data);
54 static void gtk_real_menu_shell_deactivate   (GtkMenuShell      *menu_shell);
55 static gint gtk_menu_shell_is_item           (GtkMenuShell      *menu_shell,
56                                               GtkWidget         *child);
57
58
59 static GtkContainerClass *parent_class = NULL;
60 static gint menu_shell_signals[LAST_SIGNAL] = { 0 };
61
62
63 guint
64 gtk_menu_shell_get_type ()
65 {
66   static guint menu_shell_type = 0;
67
68   if (!menu_shell_type)
69     {
70       GtkTypeInfo menu_shell_info =
71       {
72         "GtkMenuShell",
73         sizeof (GtkMenuShell),
74         sizeof (GtkMenuShellClass),
75         (GtkClassInitFunc) gtk_menu_shell_class_init,
76         (GtkObjectInitFunc) gtk_menu_shell_init,
77         (GtkArgFunc) NULL,
78       };
79
80       menu_shell_type = gtk_type_unique (gtk_container_get_type (), &menu_shell_info);
81     }
82
83   return menu_shell_type;
84 }
85
86 static void
87 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
88 {
89   GtkObjectClass *object_class;
90   GtkWidgetClass *widget_class;
91   GtkContainerClass *container_class;
92
93   object_class = (GtkObjectClass*) klass;
94   widget_class = (GtkWidgetClass*) klass;
95   container_class = (GtkContainerClass*) klass;
96
97   parent_class = gtk_type_class (gtk_container_get_type ());
98
99   menu_shell_signals[DEACTIVATE] =
100     gtk_signal_new ("deactivate",
101                     GTK_RUN_FIRST,
102                     object_class->type,
103                     GTK_SIGNAL_OFFSET (GtkMenuShellClass, deactivate),
104                     gtk_signal_default_marshaller,
105                     GTK_TYPE_NONE, 0);
106
107   gtk_object_class_add_signals (object_class, menu_shell_signals, LAST_SIGNAL);
108
109   object_class->destroy = gtk_menu_shell_destroy;
110
111   widget_class->map = gtk_menu_shell_map;
112   widget_class->realize = gtk_menu_shell_realize;
113   widget_class->button_press_event = gtk_menu_shell_button_press;
114   widget_class->button_release_event = gtk_menu_shell_button_release;
115   widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
116   widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
117
118   container_class->add = gtk_menu_shell_add;
119   container_class->remove = gtk_menu_shell_remove;
120   container_class->foreach = gtk_menu_shell_foreach;
121
122   klass->submenu_placement = GTK_TOP_BOTTOM;
123   klass->deactivate = gtk_real_menu_shell_deactivate;
124 }
125
126 static void
127 gtk_menu_shell_init (GtkMenuShell *menu_shell)
128 {
129   menu_shell->children = NULL;
130   menu_shell->active_menu_item = NULL;
131   menu_shell->parent_menu_shell = NULL;
132   menu_shell->active = FALSE;
133   menu_shell->have_grab = FALSE;
134   menu_shell->have_xgrab = FALSE;
135   menu_shell->ignore_leave = FALSE;
136   menu_shell->button = 0;
137   menu_shell->menu_flag = 0;
138   menu_shell->activate_time = 0;
139 }
140
141 void
142 gtk_menu_shell_append (GtkMenuShell *menu_shell,
143                        GtkWidget    *child)
144 {
145   gtk_menu_shell_insert (menu_shell, child, -1);
146 }
147
148 void
149 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
150                         GtkWidget    *child)
151 {
152   gtk_menu_shell_insert (menu_shell, child, 0);
153 }
154
155 void
156 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
157                        GtkWidget    *child,
158                        gint          position)
159 {
160   GList *tmp_list;
161   GList *new_list;
162   gint nchildren;
163
164   g_return_if_fail (menu_shell != NULL);
165   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
166   g_return_if_fail (child != NULL);
167   g_return_if_fail (GTK_IS_MENU_ITEM (child));
168
169   gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
170
171   if (GTK_WIDGET_VISIBLE (child->parent))
172     {
173       if (GTK_WIDGET_REALIZED (child->parent) &&
174           !GTK_WIDGET_REALIZED (child))
175         gtk_widget_realize (child);
176
177       if (GTK_WIDGET_MAPPED (child->parent) &&
178           !GTK_WIDGET_MAPPED (child))
179         gtk_widget_map (child);
180     }
181
182   nchildren = g_list_length (menu_shell->children);
183   if ((position < 0) || (position > nchildren))
184     position = nchildren;
185
186   if (position == nchildren)
187     {
188       menu_shell->children = g_list_append (menu_shell->children, child);
189     }
190   else
191     {
192       tmp_list = g_list_nth (menu_shell->children, position);
193       new_list = g_list_alloc ();
194       new_list->data = child;
195
196       if (tmp_list->prev)
197         tmp_list->prev->next = new_list;
198       new_list->next = tmp_list;
199       new_list->prev = tmp_list->prev;
200       tmp_list->prev = new_list;
201
202       if (tmp_list == menu_shell->children)
203         menu_shell->children = new_list;
204     }
205
206   if (GTK_WIDGET_VISIBLE (menu_shell))
207     gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
208 }
209
210 void
211 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
212 {
213   g_return_if_fail (menu_shell != NULL);
214   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
215
216   gtk_signal_emit (GTK_OBJECT (menu_shell), menu_shell_signals[DEACTIVATE]);
217 }
218
219 static void
220 gtk_menu_shell_destroy (GtkObject *object)
221 {
222   GtkMenuShell *menu_shell;
223   GtkWidget *child;
224   GList *children;
225
226   g_return_if_fail (object != NULL);
227   g_return_if_fail (GTK_IS_MENU_SHELL (object));
228
229   menu_shell = GTK_MENU_SHELL (object);
230
231   children = menu_shell->children;
232   while (children)
233     {
234       child = children->data;
235       children = children->next;
236
237       child->parent = NULL;
238       gtk_object_unref (GTK_OBJECT (child));
239       gtk_widget_destroy (child);
240     }
241
242   g_list_free (menu_shell->children);
243
244   if (GTK_OBJECT_CLASS (parent_class)->destroy)
245     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
246 }
247
248 static void
249 gtk_menu_shell_map (GtkWidget *widget)
250 {
251   GtkMenuShell *menu_shell;
252   GtkWidget *child;
253   GList *children;
254
255   g_return_if_fail (widget != NULL);
256   g_return_if_fail (GTK_IS_MENU_SHELL (widget));
257
258   menu_shell = GTK_MENU_SHELL (widget);
259   GTK_WIDGET_SET_FLAGS (menu_shell, GTK_MAPPED);
260   gdk_window_show (widget->window);
261
262   children = menu_shell->children;
263   while (children)
264     {
265       child = children->data;
266       children = children->next;
267
268       if (GTK_WIDGET_VISIBLE (child) && !GTK_WIDGET_MAPPED (child))
269         gtk_widget_map (child);
270     }
271 }
272
273 static void
274 gtk_menu_shell_realize (GtkWidget *widget)
275 {
276   GdkWindowAttr attributes;
277   gint attributes_mask;
278
279   g_return_if_fail (widget != NULL);
280   g_return_if_fail (GTK_IS_MENU_SHELL (widget));
281
282   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
283
284   attributes.x = widget->allocation.x;
285   attributes.y = widget->allocation.y;
286   attributes.width = widget->allocation.width;
287   attributes.height = widget->allocation.height;
288   attributes.window_type = GDK_WINDOW_CHILD;
289   attributes.wclass = GDK_INPUT_OUTPUT;
290   attributes.visual = gtk_widget_get_visual (widget);
291   attributes.colormap = gtk_widget_get_colormap (widget);
292   attributes.event_mask = gtk_widget_get_events (widget);
293   attributes.event_mask |= (GDK_EXPOSURE_MASK |
294                             GDK_BUTTON_PRESS_MASK |
295                             GDK_BUTTON_RELEASE_MASK |
296                             GDK_ENTER_NOTIFY_MASK |
297                             GDK_LEAVE_NOTIFY_MASK);
298
299   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
300   widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
301   gdk_window_set_user_data (widget->window, widget);
302
303   widget->style = gtk_style_attach (widget->style, widget->window);
304   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
305 }
306
307 static gint
308 gtk_menu_shell_button_press (GtkWidget      *widget,
309                              GdkEventButton *event)
310 {
311   GtkMenuShell *menu_shell;
312   GtkWidget *menu_item;
313
314   g_return_val_if_fail (widget != NULL, FALSE);
315   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
316   g_return_val_if_fail (event != NULL, FALSE);
317
318   if (event->type != GDK_BUTTON_PRESS)
319     return FALSE;
320
321   menu_shell = GTK_MENU_SHELL (widget);
322
323   if (menu_shell->parent_menu_shell)
324     {
325       gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
326     }
327   else if (!menu_shell->active || !menu_shell->button)
328     {
329       if (!menu_shell->active)
330         {
331           gtk_grab_add (GTK_WIDGET (widget));
332           menu_shell->have_grab = TRUE;
333         }
334       menu_shell->active = TRUE;
335
336       menu_item = gtk_get_event_widget ((GdkEvent*) event);
337       if (GTK_IS_MENU_ITEM (menu_item) && gtk_menu_shell_is_item (menu_shell, menu_item))
338         {
339           if ((menu_item->parent == widget) &&
340               (menu_item != menu_shell->active_menu_item))
341             {
342               if (menu_shell->active_menu_item)
343                 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
344
345               menu_shell->active_menu_item = menu_item;
346               gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
347                                            MENU_SHELL_CLASS (menu_shell)->submenu_placement);
348               gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
349             }
350         }
351       else if (!menu_shell->button)
352         {
353           gtk_menu_shell_deactivate (menu_shell);
354         }
355
356       if (menu_shell->active)
357         menu_shell->button = event->button;
358     }
359   else
360     {
361       widget = gtk_get_event_widget ((GdkEvent*) event);
362       if (widget == GTK_WIDGET (menu_shell))
363         gtk_menu_shell_deactivate (menu_shell);
364     }
365
366   return TRUE;
367 }
368
369 static gint
370 gtk_menu_shell_button_release (GtkWidget      *widget,
371                                GdkEventButton *event)
372 {
373   GtkMenuShell *menu_shell;
374   GtkWidget *menu_item;
375   gint deactivate;
376
377   g_return_val_if_fail (widget != NULL, FALSE);
378   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
379   g_return_val_if_fail (event != NULL, FALSE);
380
381   menu_shell = GTK_MENU_SHELL (widget);
382   if (menu_shell->active)
383     {
384       if (menu_shell->button && (event->button != menu_shell->button))
385         {
386           menu_shell->button = 0;
387           if (menu_shell->parent_menu_shell)
388             gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
389           return TRUE;
390         }
391
392       menu_shell->button = 0;
393       menu_item = gtk_get_event_widget ((GdkEvent*) event);
394       deactivate = TRUE;
395
396       if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
397         {
398           if (menu_shell->active_menu_item == menu_item)
399             {
400               if (GTK_MENU_ITEM (menu_item)->submenu == NULL)
401                 {
402                   gtk_menu_shell_deactivate (menu_shell);
403                   gtk_widget_activate (menu_item);
404                   return TRUE;
405                 }
406             }
407           else if (menu_shell->parent_menu_shell)
408             {
409               menu_shell->active = TRUE;
410               gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
411               return TRUE;
412             }
413         }
414       else
415         deactivate = FALSE;
416
417       if ((!deactivate || (menu_shell->active_menu_item == menu_item)) &&
418           (gdk_pointer_grab (widget->window, TRUE,
419                              GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
420                              GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK,
421                              NULL, NULL, event->time) == 0))
422         {
423           deactivate = FALSE;
424           menu_shell->have_xgrab = TRUE;
425           menu_shell->ignore_leave = TRUE;
426         }
427       else
428         deactivate = TRUE;
429
430       if (deactivate)
431         gtk_menu_shell_deactivate (menu_shell);
432     }
433
434   return TRUE;
435 }
436
437 static gint
438 gtk_menu_shell_enter_notify (GtkWidget        *widget,
439                              GdkEventCrossing *event)
440 {
441   GtkMenuShell *menu_shell;
442   GtkWidget *menu_item;
443
444   g_return_val_if_fail (widget != NULL, FALSE);
445   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
446   g_return_val_if_fail (event != NULL, FALSE);
447
448   menu_shell = GTK_MENU_SHELL (widget);
449   if (menu_shell->active)
450     {
451       menu_item = gtk_get_event_widget ((GdkEvent*) event);
452
453       if (!GTK_WIDGET_IS_SENSITIVE (menu_item))
454         return TRUE;
455
456       if ((menu_item->parent == widget) &&
457           (menu_shell->active_menu_item != menu_item) &&
458           GTK_IS_MENU_ITEM (menu_item))
459         {
460           if ((event->detail != GDK_NOTIFY_INFERIOR) &&
461               (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
462             {
463               if (menu_shell->active_menu_item)
464                 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
465
466               menu_shell->active_menu_item = menu_item;
467               gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
468                                            MENU_SHELL_CLASS (menu_shell)->submenu_placement);
469               gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
470             }
471         }
472       else if (menu_shell->parent_menu_shell)
473         {
474           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
475         }
476     }
477
478   return TRUE;
479 }
480
481 static gint
482 gtk_menu_shell_leave_notify (GtkWidget        *widget,
483                              GdkEventCrossing *event)
484 {
485   GtkMenuShell *menu_shell;
486   GtkMenuItem *menu_item;
487   GtkWidget *event_widget;
488
489   g_return_val_if_fail (widget != NULL, FALSE);
490   g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
491   g_return_val_if_fail (event != NULL, FALSE);
492
493   if (GTK_WIDGET_VISIBLE (widget))
494     {
495       menu_shell = GTK_MENU_SHELL (widget);
496       event_widget = gtk_get_event_widget ((GdkEvent*) event);
497
498       if (!GTK_IS_MENU_ITEM (event_widget))
499         return TRUE;
500
501       menu_item = GTK_MENU_ITEM (event_widget);
502
503       if (!GTK_WIDGET_IS_SENSITIVE (menu_item))
504         return TRUE;
505
506       if (menu_shell->ignore_leave)
507         {
508           menu_shell->ignore_leave = FALSE;
509           return TRUE;
510         }
511
512       if ((menu_shell->active_menu_item == event_widget) &&
513           (menu_item->submenu == NULL))
514         {
515           if ((event->detail != GDK_NOTIFY_INFERIOR) &&
516               (GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
517             {
518               gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
519               menu_shell->active_menu_item = NULL;
520             }
521         }
522       else if (menu_shell->parent_menu_shell)
523         {
524           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
525         }
526     }
527
528   return TRUE;
529 }
530
531 static void
532 gtk_menu_shell_add (GtkContainer *container,
533                     GtkWidget    *widget)
534 {
535   gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
536 }
537
538 static void
539 gtk_menu_shell_remove (GtkContainer *container,
540                        GtkWidget    *widget)
541 {
542   GtkMenuShell *menu_shell;
543
544   g_return_if_fail (container != NULL);
545   g_return_if_fail (GTK_IS_MENU_SHELL (container));
546   g_return_if_fail (widget != NULL);
547   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
548
549   gtk_widget_unparent (widget);
550
551   menu_shell = GTK_MENU_SHELL (container);
552   menu_shell->children = g_list_remove (menu_shell->children, widget);
553
554   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (container))
555     gtk_widget_queue_resize (GTK_WIDGET (container));
556 }
557
558 static void
559 gtk_menu_shell_foreach (GtkContainer *container,
560                         GtkCallback   callback,
561                         gpointer      callback_data)
562 {
563   GtkMenuShell *menu_shell;
564   GtkWidget *child;
565   GList *children;
566
567   g_return_if_fail (container != NULL);
568   g_return_if_fail (GTK_IS_MENU_SHELL (container));
569   g_return_if_fail (callback != NULL);
570
571   menu_shell = GTK_MENU_SHELL (container);
572
573   children = menu_shell->children;
574   while (children)
575     {
576       child = children->data;
577       children = children->next;
578
579       (* callback) (child, callback_data);
580     }
581 }
582
583
584 static void
585 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
586 {
587   g_return_if_fail (menu_shell != NULL);
588   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
589
590   if (menu_shell->active)
591     {
592       menu_shell->button = 0;
593       menu_shell->active = FALSE;
594
595       if (menu_shell->active_menu_item)
596         {
597           gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
598           menu_shell->active_menu_item = NULL;
599         }
600
601       if (menu_shell->have_grab)
602         {
603           menu_shell->have_grab = FALSE;
604           gtk_grab_remove (GTK_WIDGET (menu_shell));
605         }
606       if (menu_shell->have_xgrab)
607         {
608           menu_shell->have_xgrab = FALSE;
609           gdk_pointer_ungrab (GDK_CURRENT_TIME);
610         }
611     }
612 }
613
614 static gint
615 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
616                         GtkWidget    *child)
617 {
618   GtkMenuShell *parent;
619
620   g_return_val_if_fail (menu_shell != NULL, FALSE);
621   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
622   g_return_val_if_fail (child != NULL, FALSE);
623
624   parent = GTK_MENU_SHELL (child->parent);
625   while (parent && GTK_IS_MENU_SHELL (parent))
626     {
627       if (parent == menu_shell)
628         return TRUE;
629       parent = GTK_MENU_SHELL (parent->parent_menu_shell);
630     }
631
632   return FALSE;
633 }