]> Pileus Git - ~andy/gtk/blob - gtk/gtkoptionmenu.c
set RECEIVES_DEFAULT on init, and a button press on the widget doesn't
[~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
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "gtkmenu.h"
20 #include "gtkmenuitem.h"
21 #include "gtkoptionmenu.h"
22 #include "gtksignal.h"
23 #include "gdk/gdkkeysyms.h"
24
25
26 #define CHILD_LEFT_SPACING        5
27 #define CHILD_RIGHT_SPACING       1
28 #define CHILD_TOP_SPACING         1
29 #define CHILD_BOTTOM_SPACING      1
30 #define OPTION_INDICATOR_WIDTH    12
31 #define OPTION_INDICATOR_HEIGHT   8
32 #define OPTION_INDICATOR_SPACING  2
33
34
35 static void gtk_option_menu_class_init      (GtkOptionMenuClass *klass);
36 static void gtk_option_menu_init            (GtkOptionMenu      *option_menu);
37 static void gtk_option_menu_destroy         (GtkObject          *object);
38 static void gtk_option_menu_size_request    (GtkWidget          *widget,
39                                              GtkRequisition     *requisition);
40 static void gtk_option_menu_size_allocate   (GtkWidget          *widget,
41                                              GtkAllocation      *allocation);
42 static void gtk_option_menu_paint           (GtkWidget          *widget,
43                                              GdkRectangle       *area);
44 static void gtk_option_menu_draw            (GtkWidget          *widget,
45                                              GdkRectangle       *area);
46 static gint gtk_option_menu_expose          (GtkWidget          *widget,
47                                              GdkEventExpose     *event);
48 static gint gtk_option_menu_button_press    (GtkWidget          *widget,
49                                              GdkEventButton     *event);
50 static gint gtk_option_menu_key_press       (GtkWidget          *widget,
51                                              GdkEventKey        *event);
52 static void gtk_option_menu_deactivate      (GtkMenuShell       *menu_shell,
53                                              GtkOptionMenu      *option_menu);
54 static void gtk_option_menu_update_contents (GtkOptionMenu      *option_menu);
55 static void gtk_option_menu_remove_contents (GtkOptionMenu      *option_menu);
56 static void gtk_option_menu_calc_size       (GtkOptionMenu      *option_menu);
57 static void gtk_option_menu_position        (GtkMenu            *menu,
58                                              gint               *x,
59                                              gint               *y,
60                                              gpointer            user_data);
61 static void gtk_option_menu_show_all        (GtkWidget          *widget);
62 static void gtk_option_menu_hide_all        (GtkWidget          *widget);
63 static GtkType gtk_option_menu_child_type   (GtkContainer       *container);
64
65                                        
66
67 static GtkButtonClass *parent_class = NULL;
68
69
70 GtkType
71 gtk_option_menu_get_type (void)
72 {
73   static GtkType option_menu_type = 0;
74
75   if (!option_menu_type)
76     {
77       static const GtkTypeInfo option_menu_info =
78       {
79         "GtkOptionMenu",
80         sizeof (GtkOptionMenu),
81         sizeof (GtkOptionMenuClass),
82         (GtkClassInitFunc) gtk_option_menu_class_init,
83         (GtkObjectInitFunc) gtk_option_menu_init,
84         /* reserved_1 */ NULL,
85         /* reserved_2 */ NULL,
86         (GtkClassInitFunc) NULL,
87       };
88
89       option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
90     }
91
92   return option_menu_type;
93 }
94
95 static void
96 gtk_option_menu_class_init (GtkOptionMenuClass *class)
97 {
98   GtkObjectClass *object_class;
99   GtkWidgetClass *widget_class;
100   GtkButtonClass *button_class;
101   GtkContainerClass *container_class;
102
103   object_class = (GtkObjectClass*) class;
104   widget_class = (GtkWidgetClass*) class;
105   button_class = (GtkButtonClass*) class;
106   container_class = (GtkContainerClass*) class;
107
108   parent_class = gtk_type_class (gtk_button_get_type ());
109
110   object_class->destroy = gtk_option_menu_destroy;
111
112   widget_class->draw = gtk_option_menu_draw;
113   widget_class->size_request = gtk_option_menu_size_request;
114   widget_class->size_allocate = gtk_option_menu_size_allocate;
115   widget_class->expose_event = gtk_option_menu_expose;
116   widget_class->button_press_event = gtk_option_menu_button_press;
117   widget_class->key_press_event = gtk_option_menu_key_press;
118   widget_class->show_all = gtk_option_menu_show_all;
119   widget_class->hide_all = gtk_option_menu_hide_all;
120
121   container_class->child_type = gtk_option_menu_child_type;
122 }
123
124 static GtkType
125 gtk_option_menu_child_type (GtkContainer       *container)
126 {
127   return GTK_TYPE_NONE;
128 }
129
130 static void
131 gtk_option_menu_init (GtkOptionMenu *option_menu)
132 {
133   GTK_WIDGET_SET_FLAGS (option_menu, GTK_CAN_FOCUS);
134   GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_DEFAULT | GTK_RECEIVES_DEFAULT);
135
136   option_menu->menu = NULL;
137   option_menu->menu_item = NULL;
138   option_menu->width = 0;
139   option_menu->height = 0;
140 }
141
142 GtkWidget*
143 gtk_option_menu_new (void)
144 {
145   return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
146 }
147
148 GtkWidget*
149 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
150 {
151   g_return_val_if_fail (option_menu != NULL, NULL);
152   g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
153
154   return option_menu->menu;
155 }
156
157 static void
158 gtk_option_menu_detacher (GtkWidget     *widget,
159                           GtkMenu       *menu)
160 {
161   GtkOptionMenu *option_menu;
162
163   g_return_if_fail (widget != NULL);
164   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
165
166   option_menu = GTK_OPTION_MENU (widget);
167   g_return_if_fail (option_menu->menu == (GtkWidget*) menu);
168
169   gtk_option_menu_remove_contents (option_menu);
170   gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
171                                  option_menu);
172   
173   option_menu->menu = NULL;
174 }
175
176 void
177 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
178                           GtkWidget     *menu)
179 {
180   g_return_if_fail (option_menu != NULL);
181   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
182   g_return_if_fail (menu != NULL);
183   g_return_if_fail (GTK_IS_MENU (menu));
184
185   if (option_menu->menu != menu)
186     {
187       gtk_option_menu_remove_menu (option_menu);
188
189       option_menu->menu = menu;
190       gtk_menu_attach_to_widget (GTK_MENU (menu),
191                                  GTK_WIDGET (option_menu),
192                                  gtk_option_menu_detacher);
193
194       gtk_option_menu_calc_size (option_menu);
195
196       gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
197                           (GtkSignalFunc) gtk_option_menu_deactivate,
198                           option_menu);
199
200       if (GTK_WIDGET (option_menu)->parent)
201         gtk_widget_queue_resize (GTK_WIDGET (option_menu));
202
203       gtk_option_menu_update_contents (option_menu);
204     }
205 }
206
207 void
208 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
209 {
210   g_return_if_fail (option_menu != NULL);
211   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
212
213   if (option_menu->menu)
214     gtk_menu_detach (GTK_MENU (option_menu->menu));
215 }
216
217 void
218 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
219                              guint          index)
220 {
221   GtkWidget *menu_item;
222
223   g_return_if_fail (option_menu != NULL);
224   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
225
226   if (option_menu->menu)
227     {
228       gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
229       menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
230
231       if (menu_item != option_menu->menu_item)
232         {
233           gtk_option_menu_remove_contents (option_menu);
234           gtk_option_menu_update_contents (option_menu);
235         }
236     }
237 }
238
239
240 static void
241 gtk_option_menu_destroy (GtkObject *object)
242 {
243   GtkOptionMenu *option_menu;
244
245   g_return_if_fail (object != NULL);
246   g_return_if_fail (GTK_IS_OPTION_MENU (object));
247
248   option_menu = GTK_OPTION_MENU (object);
249
250   if (option_menu->menu)
251     gtk_widget_destroy (option_menu->menu);
252
253   if (GTK_OBJECT_CLASS (parent_class)->destroy)
254     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
255 }
256
257 static void
258 gtk_option_menu_size_request (GtkWidget      *widget,
259                               GtkRequisition *requisition)
260 {
261   GtkOptionMenu *option_menu;
262   gint tmp;
263
264   g_return_if_fail (widget != NULL);
265   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
266   g_return_if_fail (requisition != NULL);
267
268   option_menu = GTK_OPTION_MENU (widget);
269
270   requisition->width = ((GTK_CONTAINER (widget)->border_width +
271                          GTK_WIDGET (widget)->style->klass->xthickness) * 2 +
272                         option_menu->width +
273                         OPTION_INDICATOR_WIDTH +
274                         OPTION_INDICATOR_SPACING * 5 +
275                         CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING + 2);
276   requisition->height = ((GTK_CONTAINER (widget)->border_width +
277                           GTK_WIDGET (widget)->style->klass->ythickness) * 2 +
278                          option_menu->height +
279                          CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING + 2);
280
281   tmp = (requisition->height - option_menu->height +
282          OPTION_INDICATOR_HEIGHT + OPTION_INDICATOR_SPACING * 2);
283   requisition->height = MAX (requisition->height, tmp);
284 }
285
286 static void
287 gtk_option_menu_size_allocate (GtkWidget     *widget,
288                                GtkAllocation *allocation)
289 {
290   GtkWidget *child;
291   GtkAllocation child_allocation;
292
293   g_return_if_fail (widget != NULL);
294   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
295   g_return_if_fail (allocation != NULL);
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   child = GTK_BIN (widget)->child;
304   if (child && GTK_WIDGET_VISIBLE (child))
305     {
306       child_allocation.x = (GTK_CONTAINER (widget)->border_width +
307                             GTK_WIDGET (widget)->style->klass->xthickness) + 1;
308       child_allocation.y = (GTK_CONTAINER (widget)->border_width +
309                             GTK_WIDGET (widget)->style->klass->ythickness) + 1;
310       child_allocation.width = (allocation->width - child_allocation.x * 2 -
311                                 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 5 -
312                                 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING) - 2;
313       child_allocation.height = (allocation->height - child_allocation.y * 2 -
314                                  CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING) - 2;
315       child_allocation.x += CHILD_LEFT_SPACING;
316       child_allocation.y += CHILD_RIGHT_SPACING;
317
318       gtk_widget_size_allocate (child, &child_allocation);
319     }
320 }
321
322 static void
323 gtk_option_menu_paint (GtkWidget    *widget,
324                        GdkRectangle *area)
325 {
326   GdkRectangle button_area;
327
328   g_return_if_fail (widget != NULL);
329   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
330   g_return_if_fail (area != NULL);
331
332   if (GTK_WIDGET_DRAWABLE (widget))
333     {
334       button_area.x = GTK_CONTAINER (widget)->border_width + 1;
335       button_area.y = GTK_CONTAINER (widget)->border_width + 1;
336       button_area.width = widget->allocation.width - button_area.x * 2;
337       button_area.height = widget->allocation.height - button_area.y * 2;
338
339       /* This is evil, and should be elimated here and in the button
340        * code. The point is to clear the focus, and make it
341        * sort of transparent if it isn't there.
342        */
343       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
344       gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
345
346       gtk_paint_box(widget->style, widget->window,
347                     GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
348                     area, widget, "optionmenu",
349                     button_area.x, button_area.y,
350                     button_area.width, button_area.height);
351       
352       gtk_paint_tab (widget->style, widget->window,
353                      GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
354                      area, widget, "optionmenutab",
355                      button_area.x + button_area.width - button_area.x -
356                      OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 4,
357                      button_area.y + (button_area.height - OPTION_INDICATOR_HEIGHT) / 2,
358                      OPTION_INDICATOR_WIDTH, OPTION_INDICATOR_HEIGHT);
359       
360       if (GTK_WIDGET_HAS_FOCUS (widget))
361         gtk_paint_focus (widget->style, widget->window,
362                          area, widget, "button",
363                          button_area.x - 1, 
364                          button_area.y - 1, 
365                          button_area.width + 1,
366                          button_area.height + 1);
367     }
368 }
369
370 static void
371 gtk_option_menu_draw (GtkWidget    *widget,
372                       GdkRectangle *area)
373 {
374   GtkWidget *child;
375   GdkRectangle child_area;
376
377   g_return_if_fail (widget != NULL);
378   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
379   g_return_if_fail (area != NULL);
380
381   if (GTK_WIDGET_DRAWABLE (widget))
382     {
383       gtk_option_menu_paint (widget, area);
384
385       child = GTK_BIN (widget)->child;
386       if (child && gtk_widget_intersect (child, area, &child_area))
387         gtk_widget_draw (child, &child_area);
388     }
389 }
390
391 static gint
392 gtk_option_menu_expose (GtkWidget      *widget,
393                         GdkEventExpose *event)
394 {
395   GtkWidget *child;
396   GdkEventExpose child_event;
397   gint remove_child;
398
399   g_return_val_if_fail (widget != NULL, FALSE);
400   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
401   g_return_val_if_fail (event != NULL, FALSE);
402
403   if (GTK_WIDGET_DRAWABLE (widget))
404     {
405       gtk_option_menu_paint (widget, &event->area);
406
407
408       /* The following code tries to draw the child in two places at
409        * once. It fails miserably for several reasons
410        *
411        * - If the child is not no-window, removing generates
412        *   more expose events. Bad, bad, bad.
413        * 
414        * - Even if the child is no-window, removing it now (properly)
415        *   clears the space where it was, so it does no good
416        */
417       
418 #if 0
419       remove_child = FALSE;
420       child = GTK_BUTTON (widget)->child;
421
422       if (!child)
423         {
424           if (!GTK_OPTION_MENU (widget)->menu)
425             return FALSE;
426           gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
427           child = GTK_BUTTON (widget)->child;
428           if (!child)
429             return FALSE;
430           remove_child = TRUE;
431         }
432
433       child_event = *event;
434
435       if (GTK_WIDGET_NO_WINDOW (child) &&
436           gtk_widget_intersect (child, &event->area, &child_event.area))
437         gtk_widget_event (child, (GdkEvent*) &child_event);
438
439       if (remove_child)
440         gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
441 #else
442       remove_child = FALSE;
443       child = GTK_BIN (widget)->child;
444       child_event = *event;
445       if (child && GTK_WIDGET_NO_WINDOW (child) &&
446           gtk_widget_intersect (child, &event->area, &child_event.area))
447         gtk_widget_event (child, (GdkEvent*) &child_event);
448
449 #endif /* 0 */
450     }
451
452   return FALSE;
453 }
454
455 static gint
456 gtk_option_menu_button_press (GtkWidget      *widget,
457                               GdkEventButton *event)
458 {
459   GtkOptionMenu *option_menu;
460
461   g_return_val_if_fail (widget != NULL, FALSE);
462   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
463   g_return_val_if_fail (event != NULL, FALSE);
464
465   option_menu = GTK_OPTION_MENU (widget);
466
467   if ((event->type == GDK_BUTTON_PRESS) &&
468       (event->button == 1))
469     {
470       gtk_option_menu_remove_contents (option_menu);
471       gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
472                       gtk_option_menu_position, option_menu,
473                       event->button, event->time);
474       return TRUE;
475     }
476
477   return FALSE;
478 }
479
480 static gint
481 gtk_option_menu_key_press (GtkWidget   *widget,
482                            GdkEventKey *event)
483 {
484   GtkOptionMenu *option_menu;
485
486   g_return_val_if_fail (widget != NULL, FALSE);
487   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
488   g_return_val_if_fail (event != NULL, FALSE);
489
490   option_menu = GTK_OPTION_MENU (widget);
491
492   switch (event->keyval)
493     {
494     case GDK_space:
495       gtk_option_menu_remove_contents (option_menu);
496       gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
497                       gtk_option_menu_position, option_menu,
498                       0, event->time);
499       return TRUE;
500     }
501   
502   return FALSE;
503 }
504
505 static void
506 gtk_option_menu_deactivate (GtkMenuShell  *menu_shell,
507                             GtkOptionMenu *option_menu)
508 {
509   g_return_if_fail (menu_shell != NULL);
510   g_return_if_fail (option_menu != NULL);
511   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
512
513   gtk_option_menu_update_contents (option_menu);
514 }
515
516 static void
517 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
518 {
519   GtkWidget *child;
520
521   g_return_if_fail (option_menu != NULL);
522   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
523
524   if (option_menu->menu)
525     {
526       gtk_option_menu_remove_contents (option_menu);
527
528       option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
529       if (option_menu->menu_item)
530         {
531           gtk_widget_ref (option_menu->menu_item);
532           child = GTK_BIN (option_menu->menu_item)->child;
533           if (child)
534             {
535               if (GTK_BIN (option_menu)->child)
536                 gtk_container_remove (GTK_CONTAINER (option_menu),
537                                       GTK_BIN (option_menu)->child);
538               if (GTK_WIDGET (option_menu)->state != child->state)
539                 gtk_widget_set_state (child, GTK_WIDGET (option_menu)->state);
540               gtk_widget_reparent (child, GTK_WIDGET (option_menu));
541             }
542
543           gtk_widget_size_request (child, &child->requisition);
544           gtk_widget_size_allocate (GTK_WIDGET (option_menu),
545                                     &(GTK_WIDGET (option_menu)->allocation));
546
547           if (GTK_WIDGET_DRAWABLE (option_menu))
548             gtk_widget_queue_draw (GTK_WIDGET (option_menu));
549         }
550     }
551 }
552
553 static void
554 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
555 {
556   g_return_if_fail (option_menu != NULL);
557   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
558
559   if (GTK_BIN (option_menu)->child)
560     {
561       if (GTK_WIDGET (option_menu->menu_item)->state != GTK_BIN (option_menu)->child->state)
562         gtk_widget_set_state (GTK_BIN (option_menu)->child,
563                               GTK_WIDGET (option_menu->menu_item)->state);
564       gtk_widget_reparent (GTK_BIN (option_menu)->child, option_menu->menu_item);
565       gtk_widget_unref (option_menu->menu_item);
566       option_menu->menu_item = NULL;
567     }
568 }
569
570 static void
571 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
572 {
573   GtkWidget *child;
574   GList *children;
575
576   g_return_if_fail (option_menu != NULL);
577   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
578
579   option_menu->width = 0;
580   option_menu->height = 0;
581
582   if (option_menu->menu)
583     {
584       children = GTK_MENU_SHELL (option_menu->menu)->children;
585       while (children)
586         {
587           child = children->data;
588           children = children->next;
589
590           if (GTK_WIDGET_VISIBLE (child))
591             {
592               gtk_widget_size_request (child, &child->requisition);
593
594               option_menu->width = MAX (option_menu->width, child->requisition.width);
595               option_menu->height = MAX (option_menu->height, child->requisition.height);
596             }
597         }
598     }
599 }
600
601 static void
602 gtk_option_menu_position (GtkMenu  *menu,
603                           gint     *x,
604                           gint     *y,
605                           gpointer  user_data)
606 {
607   GtkOptionMenu *option_menu;
608   GtkWidget *active;
609   GtkWidget *child;
610   GList *children;
611   gint shift_menu;
612   gint screen_width;
613   gint screen_height;
614   gint menu_xpos;
615   gint menu_ypos;
616   gint width;
617   gint height;
618
619   g_return_if_fail (user_data != NULL);
620   g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
621
622   option_menu = GTK_OPTION_MENU (user_data);
623
624   width = GTK_WIDGET (menu)->allocation.width;
625   height = GTK_WIDGET (menu)->allocation.height;
626
627   active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
628   children = GTK_MENU_SHELL (option_menu->menu)->children;
629   gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
630
631   menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
632
633   if (active != NULL)
634     menu_ypos -= active->requisition.height / 2;
635
636   while (children)
637     {
638       child = children->data;
639
640       if (active == child)
641         break;
642
643       if (GTK_WIDGET_VISIBLE (child))
644         menu_ypos -= child->allocation.height;
645
646       children = children->next;
647     }
648
649   screen_width = gdk_screen_width ();
650   screen_height = gdk_screen_height ();
651
652   shift_menu = FALSE;
653   if (menu_ypos < 0)
654     {
655       menu_ypos = 0;
656       shift_menu = TRUE;
657     }
658   else if ((menu_ypos + height) > screen_height)
659     {
660       menu_ypos -= ((menu_ypos + height) - screen_height);
661       shift_menu = TRUE;
662     }
663
664   if (shift_menu)
665     {
666       if ((menu_xpos + GTK_WIDGET (option_menu)->allocation.width + width) <= screen_width)
667         menu_xpos += GTK_WIDGET (option_menu)->allocation.width;
668       else
669         menu_xpos -= width;
670     }
671
672   if (menu_xpos < 0)
673     menu_xpos = 0;
674   else if ((menu_xpos + width) > screen_width)
675     menu_xpos -= ((menu_xpos + width) - screen_width);
676
677   *x = menu_xpos;
678   *y = menu_ypos;
679 }
680
681
682 static void
683 gtk_option_menu_show_all (GtkWidget *widget)
684 {
685   GtkContainer *container;
686   GtkOptionMenu *option_menu;
687   
688   g_return_if_fail (widget != NULL);
689   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
690   container = GTK_CONTAINER (widget);
691   option_menu = GTK_OPTION_MENU (widget);
692
693   gtk_widget_show (widget);
694   gtk_container_foreach (container, (GtkCallback) gtk_widget_show_all, NULL);
695   if (option_menu->menu)
696     gtk_widget_show_all (option_menu->menu);
697   if (option_menu->menu_item)
698     gtk_widget_show_all (option_menu->menu_item);
699 }
700
701
702 static void
703 gtk_option_menu_hide_all (GtkWidget *widget)
704 {
705   GtkContainer *container;
706
707   g_return_if_fail (widget != NULL);
708   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
709   container = GTK_CONTAINER (widget);
710
711   gtk_widget_hide (widget);
712   gtk_container_foreach (container, (GtkCallback) gtk_widget_hide_all, NULL);
713 }
714