]> Pileus Git - ~andy/gtk/blob - gtk/gtkoptionmenu.c
Initial revision
[~andy/gtk] / gtk / gtkoptionmenu.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 "gtkmenu.h"
19 #include "gtkmenuitem.h"
20 #include "gtkoptionmenu.h"
21 #include "gtksignal.h"
22
23
24 #define CHILD_LEFT_SPACING        5
25 #define CHILD_RIGHT_SPACING       1
26 #define CHILD_TOP_SPACING         1
27 #define CHILD_BOTTOM_SPACING      1
28 #define OPTION_INDICATOR_WIDTH    12
29 #define OPTION_INDICATOR_HEIGHT   8
30 #define OPTION_INDICATOR_SPACING  2
31
32
33 static void gtk_option_menu_class_init      (GtkOptionMenuClass *klass);
34 static void gtk_option_menu_init            (GtkOptionMenu      *option_menu);
35 static void gtk_option_menu_destroy         (GtkObject          *object);
36 static void gtk_option_menu_size_request    (GtkWidget          *widget,
37                                              GtkRequisition     *requisition);
38 static void gtk_option_menu_size_allocate   (GtkWidget          *widget,
39                                              GtkAllocation      *allocation);
40 static void gtk_option_menu_paint           (GtkWidget          *widget,
41                                              GdkRectangle       *area);
42 static void gtk_option_menu_draw            (GtkWidget          *widget,
43                                              GdkRectangle       *area);
44 static gint gtk_option_menu_expose          (GtkWidget          *widget,
45                                              GdkEventExpose     *event);
46 static gint gtk_option_menu_button_press    (GtkWidget          *widget,
47                                              GdkEventButton     *event);
48 static void gtk_option_menu_deactivate      (GtkMenuShell       *menu_shell,
49                                              GtkOptionMenu      *option_menu);
50 static void gtk_option_menu_update_contents (GtkOptionMenu      *option_menu);
51 static void gtk_option_menu_remove_contents (GtkOptionMenu      *option_menu);
52 static void gtk_option_menu_calc_size       (GtkOptionMenu      *option_menu);
53 static void gtk_option_menu_position        (GtkMenu            *menu,
54                                              gint               *x,
55                                              gint               *y,
56                                              gpointer            user_data);
57
58
59 static GtkButtonClass *parent_class = NULL;
60
61
62 guint
63 gtk_option_menu_get_type ()
64 {
65   static guint option_menu_type = 0;
66
67   if (!option_menu_type)
68     {
69       GtkTypeInfo option_menu_info =
70       {
71         "GtkOptionMenu",
72         sizeof (GtkOptionMenu),
73         sizeof (GtkOptionMenuClass),
74         (GtkClassInitFunc) gtk_option_menu_class_init,
75         (GtkObjectInitFunc) gtk_option_menu_init,
76         (GtkArgFunc) NULL,
77       };
78
79       option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
80     }
81
82   return option_menu_type;
83 }
84
85 static void
86 gtk_option_menu_class_init (GtkOptionMenuClass *class)
87 {
88   GtkObjectClass *object_class;
89   GtkWidgetClass *widget_class;
90   GtkButtonClass *button_class;
91
92   object_class = (GtkObjectClass*) class;
93   widget_class = (GtkWidgetClass*) class;
94   button_class = (GtkButtonClass*) class;
95
96   parent_class = gtk_type_class (gtk_button_get_type ());
97
98   object_class->destroy = gtk_option_menu_destroy;
99
100   widget_class->draw = gtk_option_menu_draw;
101   widget_class->draw_focus = NULL;
102   widget_class->size_request = gtk_option_menu_size_request;
103   widget_class->size_allocate = gtk_option_menu_size_allocate;
104   widget_class->expose_event = gtk_option_menu_expose;
105   widget_class->button_press_event = gtk_option_menu_button_press;
106 }
107
108 static void
109 gtk_option_menu_init (GtkOptionMenu *option_menu)
110 {
111   GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_FOCUS);
112
113   option_menu->menu = NULL;
114   option_menu->menu_item = NULL;
115   option_menu->width = 0;
116   option_menu->height = 0;
117 }
118
119 GtkWidget*
120 gtk_option_menu_new ()
121 {
122   return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
123 }
124
125 GtkWidget*
126 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
127 {
128   g_return_val_if_fail (option_menu != NULL, NULL);
129   g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
130
131   return option_menu->menu;
132 }
133
134 void
135 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
136                           GtkWidget     *menu)
137 {
138   g_return_if_fail (option_menu != NULL);
139   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
140   g_return_if_fail (menu != NULL);
141   g_return_if_fail (GTK_IS_MENU (menu));
142
143   gtk_option_menu_remove_menu (option_menu);
144
145   option_menu->menu = menu;
146   gtk_object_ref (GTK_OBJECT (option_menu->menu));
147
148   gtk_option_menu_calc_size (option_menu);
149
150   gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
151                       (GtkSignalFunc) gtk_option_menu_deactivate,
152                       option_menu);
153
154   if (GTK_WIDGET (option_menu)->parent)
155     gtk_widget_queue_resize (GTK_WIDGET (option_menu));
156
157   gtk_option_menu_update_contents (option_menu);
158 }
159
160 void
161 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
162 {
163   g_return_if_fail (option_menu != NULL);
164   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
165
166   if (option_menu->menu)
167     {
168       gtk_option_menu_remove_contents (option_menu);
169       gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
170                                      option_menu);
171
172       gtk_object_unref (GTK_OBJECT (option_menu->menu));
173       option_menu->menu = NULL;
174     }
175 }
176
177 void
178 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
179                              gint           index)
180 {
181   GtkWidget *menu_item;
182
183   g_return_if_fail (option_menu != NULL);
184   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
185
186   if (option_menu->menu)
187     {
188       gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
189       menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
190
191       if (menu_item != option_menu->menu_item)
192         {
193           gtk_option_menu_remove_contents (option_menu);
194           gtk_option_menu_update_contents (option_menu);
195         }
196     }
197 }
198
199
200 static void
201 gtk_option_menu_destroy (GtkObject *object)
202 {
203   GtkOptionMenu *option_menu;
204
205   g_return_if_fail (object != NULL);
206   g_return_if_fail (GTK_IS_OPTION_MENU (object));
207
208   option_menu = GTK_OPTION_MENU (object);
209
210   gtk_option_menu_remove_contents (option_menu);
211   if (option_menu->menu)
212     {
213       gtk_object_unref (GTK_OBJECT (option_menu->menu));
214       gtk_widget_destroy (option_menu->menu);
215     }
216
217   if (GTK_OBJECT_CLASS (parent_class)->destroy)
218     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
219 }
220
221 static void
222 gtk_option_menu_size_request (GtkWidget      *widget,
223                               GtkRequisition *requisition)
224 {
225   GtkOptionMenu *option_menu;
226   gint tmp;
227
228   g_return_if_fail (widget != NULL);
229   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
230   g_return_if_fail (requisition != NULL);
231
232   option_menu = GTK_OPTION_MENU (widget);
233
234   requisition->width = ((GTK_CONTAINER (widget)->border_width +
235                          GTK_WIDGET (widget)->style->klass->xthickness) * 2 +
236                         option_menu->width +
237                         OPTION_INDICATOR_WIDTH +
238                         OPTION_INDICATOR_SPACING * 5 +
239                         CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING);
240   requisition->height = ((GTK_CONTAINER (widget)->border_width +
241                           GTK_WIDGET (widget)->style->klass->ythickness) * 2 +
242                          option_menu->height +
243                          CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING);
244
245   tmp = (requisition->height - option_menu->height +
246          OPTION_INDICATOR_HEIGHT + OPTION_INDICATOR_SPACING * 2);
247   requisition->height = MAX (requisition->height, tmp);
248 }
249
250 static void
251 gtk_option_menu_size_allocate (GtkWidget     *widget,
252                                GtkAllocation *allocation)
253 {
254   GtkWidget *child;
255   GtkAllocation child_allocation;
256
257   g_return_if_fail (widget != NULL);
258   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
259   g_return_if_fail (allocation != NULL);
260
261   widget->allocation = *allocation;
262   if (GTK_WIDGET_REALIZED (widget))
263     gdk_window_move_resize (widget->window,
264                             allocation->x, allocation->y,
265                             allocation->width, allocation->height);
266
267   child = GTK_BUTTON (widget)->child;
268   if (child && GTK_WIDGET_VISIBLE (child))
269     {
270       child_allocation.x = (GTK_CONTAINER (widget)->border_width +
271                             GTK_WIDGET (widget)->style->klass->xthickness);
272       child_allocation.y = (GTK_CONTAINER (widget)->border_width +
273                             GTK_WIDGET (widget)->style->klass->ythickness);
274       child_allocation.width = (allocation->width - child_allocation.x * 2 -
275                                 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 5 -
276                                 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING);
277       child_allocation.height = (allocation->height - child_allocation.y * 2 -
278                                  CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING);
279       child_allocation.x += CHILD_LEFT_SPACING;
280       child_allocation.y += CHILD_RIGHT_SPACING;
281
282       gtk_widget_size_allocate (child, &child_allocation);
283     }
284 }
285
286 static void
287 gtk_option_menu_paint (GtkWidget    *widget,
288                        GdkRectangle *area)
289 {
290   GdkRectangle restrict_area;
291   GdkRectangle new_area;
292
293   g_return_if_fail (widget != NULL);
294   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
295   g_return_if_fail (area != NULL);
296
297   if (GTK_WIDGET_DRAWABLE (widget))
298     {
299       restrict_area.x = GTK_CONTAINER (widget)->border_width;
300       restrict_area.y = GTK_CONTAINER (widget)->border_width;
301       restrict_area.width = widget->allocation.width - restrict_area.x * 2;
302       restrict_area.height = widget->allocation.height - restrict_area.y * 2;
303
304       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
305         {
306           gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (widget));
307           gdk_window_clear_area (widget->window,
308                                  new_area.x, new_area.y,
309                                  new_area.width, new_area.height);
310
311           gtk_draw_shadow (widget->style, widget->window,
312                            GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
313                            restrict_area.x, restrict_area.y,
314                            restrict_area.width, restrict_area.height);
315
316           gtk_draw_shadow (widget->style, widget->window,
317                            GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
318                            restrict_area.x + restrict_area.width - restrict_area.x -
319                            OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 4,
320                            restrict_area.y + (restrict_area.height - OPTION_INDICATOR_HEIGHT) / 2,
321                            OPTION_INDICATOR_WIDTH, OPTION_INDICATOR_HEIGHT);
322         }
323     }
324 }
325
326 static void
327 gtk_option_menu_draw (GtkWidget    *widget,
328                       GdkRectangle *area)
329 {
330   GtkWidget *child;
331   GdkRectangle child_area;
332
333   g_return_if_fail (widget != NULL);
334   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
335   g_return_if_fail (area != NULL);
336
337   if (GTK_WIDGET_DRAWABLE (widget))
338     {
339       gtk_option_menu_paint (widget, area);
340
341       child = GTK_BUTTON (widget)->child;
342       if (child && gtk_widget_intersect (child, area, &child_area))
343         gtk_widget_draw (child, &child_area);
344     }
345 }
346
347 static gint
348 gtk_option_menu_expose (GtkWidget      *widget,
349                         GdkEventExpose *event)
350 {
351   GtkWidget *child;
352   GdkEventExpose child_event;
353   gint remove_child;
354
355   g_return_val_if_fail (widget != NULL, FALSE);
356   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
357   g_return_val_if_fail (event != NULL, FALSE);
358
359   if (GTK_WIDGET_DRAWABLE (widget))
360     {
361       gtk_option_menu_paint (widget, &event->area);
362
363       remove_child = FALSE;
364       child = GTK_BUTTON (widget)->child;
365
366       if (!child)
367         {
368           if (!GTK_OPTION_MENU (widget)->menu)
369             return FALSE;
370           gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
371           child = GTK_BUTTON (widget)->child;
372           if (!child)
373             return FALSE;
374           remove_child = TRUE;
375         }
376
377       child_event = *event;
378
379       if (GTK_WIDGET_NO_WINDOW (child) &&
380           gtk_widget_intersect (child, &event->area, &child_event.area))
381         gtk_widget_event (child, (GdkEvent*) &child_event);
382
383       if (remove_child)
384         gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
385     }
386
387   return FALSE;
388 }
389
390 static gint
391 gtk_option_menu_button_press (GtkWidget      *widget,
392                               GdkEventButton *event)
393 {
394   GtkOptionMenu *option_menu;
395
396   g_return_val_if_fail (widget != NULL, FALSE);
397   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
398   g_return_val_if_fail (event != NULL, FALSE);
399
400   if ((event->type == GDK_BUTTON_PRESS) &&
401       (event->button == 1))
402     {
403       option_menu = GTK_OPTION_MENU (widget);
404       gtk_option_menu_remove_contents (option_menu);
405       gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
406                       gtk_option_menu_position, option_menu,
407                       event->button, event->time);
408     }
409
410   return FALSE;
411 }
412
413 static void
414 gtk_option_menu_deactivate (GtkMenuShell  *menu_shell,
415                             GtkOptionMenu *option_menu)
416 {
417   g_return_if_fail (menu_shell != NULL);
418   g_return_if_fail (option_menu != NULL);
419   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
420
421   gtk_option_menu_update_contents (option_menu);
422 }
423
424 static void
425 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
426 {
427   GtkWidget *child;
428
429   g_return_if_fail (option_menu != NULL);
430   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
431
432   if (option_menu->menu)
433     {
434       gtk_option_menu_remove_contents (option_menu);
435
436       option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
437       if (option_menu->menu_item)
438         {
439           child = GTK_BIN (option_menu->menu_item)->child;
440           if (child)
441             {
442               gtk_container_block_resize (GTK_CONTAINER (option_menu));
443               if (GTK_WIDGET (option_menu)->state != child->state)
444                 gtk_widget_set_state (child, GTK_WIDGET (option_menu)->state);
445               gtk_widget_reparent (child, GTK_WIDGET (option_menu));
446               gtk_container_unblock_resize (GTK_CONTAINER (option_menu));
447             }
448
449           gtk_widget_size_allocate (GTK_WIDGET (option_menu),
450                                     &(GTK_WIDGET (option_menu)->allocation));
451
452           if (GTK_WIDGET_DRAWABLE (option_menu))
453             gtk_widget_queue_draw (GTK_WIDGET (option_menu));
454         }
455     }
456 }
457
458 static void
459 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
460 {
461   g_return_if_fail (option_menu != NULL);
462   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
463
464   if (GTK_BUTTON (option_menu)->child)
465     {
466       gtk_container_block_resize (GTK_CONTAINER (option_menu));
467       if (GTK_WIDGET (option_menu->menu_item)->state != GTK_BUTTON (option_menu)->child->state)
468         gtk_widget_set_state (GTK_BUTTON (option_menu)->child,
469                               GTK_WIDGET (option_menu->menu_item)->state);
470       GTK_WIDGET_UNSET_FLAGS (GTK_BUTTON (option_menu)->child, GTK_MAPPED | GTK_REALIZED);
471       gtk_widget_reparent (GTK_BUTTON (option_menu)->child, option_menu->menu_item);
472       gtk_container_unblock_resize (GTK_CONTAINER (option_menu));
473       option_menu->menu_item = NULL;
474     }
475 }
476
477 static void
478 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
479 {
480   GtkWidget *child;
481   GList *children;
482
483   g_return_if_fail (option_menu != NULL);
484   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
485
486   option_menu->width = 0;
487   option_menu->height = 0;
488
489   if (option_menu->menu)
490     {
491       children = GTK_MENU_SHELL (option_menu->menu)->children;
492       while (children)
493         {
494           child = children->data;
495           children = children->next;
496
497           if (GTK_WIDGET_VISIBLE (child))
498             {
499               gtk_widget_size_request (child, &child->requisition);
500
501               option_menu->width = MAX (option_menu->width, child->requisition.width);
502               option_menu->height = MAX (option_menu->height, child->requisition.height);
503             }
504         }
505     }
506 }
507
508 static void
509 gtk_option_menu_position (GtkMenu  *menu,
510                           gint     *x,
511                           gint     *y,
512                           gpointer  user_data)
513 {
514   GtkOptionMenu *option_menu;
515   GtkWidget *active;
516   GtkWidget *child;
517   GList *children;
518   gint shift_menu;
519   gint screen_width;
520   gint screen_height;
521   gint menu_xpos;
522   gint menu_ypos;
523   gint width;
524   gint height;
525
526   g_return_if_fail (user_data != NULL);
527   g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
528
529   option_menu = GTK_OPTION_MENU (user_data);
530
531   width = GTK_WIDGET (menu)->allocation.width;
532   height = GTK_WIDGET (menu)->allocation.height;
533
534   active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
535   children = GTK_MENU_SHELL (option_menu->menu)->children;
536   gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
537
538   menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
539
540   if (active != NULL)
541     menu_ypos -= active->requisition.height / 2;
542
543   while (children)
544     {
545       child = children->data;
546
547       if (active == child)
548         break;
549
550       menu_ypos -= child->allocation.height;
551       children = children->next;
552     }
553
554   screen_width = gdk_screen_width ();
555   screen_height = gdk_screen_height ();
556
557   shift_menu = FALSE;
558   if (menu_ypos < 0)
559     {
560       menu_ypos = 0;
561       shift_menu = TRUE;
562     }
563   else if ((menu_ypos + height) > screen_height)
564     {
565       menu_ypos -= ((menu_ypos + height) - screen_height);
566       shift_menu = TRUE;
567     }
568
569   if (shift_menu)
570     {
571       if ((menu_xpos + GTK_WIDGET (option_menu)->allocation.width + width) <= screen_width)
572         menu_xpos += GTK_WIDGET (option_menu)->allocation.width;
573       else
574         menu_xpos -= width;
575     }
576
577   if (menu_xpos < 0)
578     menu_xpos = 0;
579   else if ((menu_xpos + width) > screen_width)
580     menu_xpos -= ((menu_xpos + width) - screen_width);
581
582   *x = menu_xpos;
583   *y = menu_ypos;
584 }