]> Pileus Git - ~andy/gtk/blob - gtk/gtkoptionmenu.c
Clip the retrieved image data to the screen, using a server grab to avoid
[~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 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 "gtkintl.h"
28 #include "gtkmenu.h"
29 #include "gtkmenuitem.h"
30 #include "gtkoptionmenu.h"
31 #include "gtksignal.h"
32 #include "gdk/gdkkeysyms.h"
33
34
35 #define CHILD_LEFT_SPACING        5
36 #define CHILD_RIGHT_SPACING       1
37 #define CHILD_TOP_SPACING         1
38 #define CHILD_BOTTOM_SPACING      1
39
40 typedef struct _GtkOptionMenuProps GtkOptionMenuProps;
41
42 struct _GtkOptionMenuProps
43 {
44   gboolean interior_focus;
45   GtkRequisition indicator_size;
46   GtkBorder indicator_spacing;
47 };
48
49 static GtkOptionMenuProps default_props = {
50   FALSE,
51   { 7, 13 },
52   { 7, 5, 2, 2 }                /* Left, right, top, bottom */
53 };
54
55 static void gtk_option_menu_class_init      (GtkOptionMenuClass *klass);
56 static void gtk_option_menu_init            (GtkOptionMenu      *option_menu);
57 static void gtk_option_menu_destroy         (GtkObject          *object);
58 static void gtk_option_menu_size_request    (GtkWidget          *widget,
59                                              GtkRequisition     *requisition);
60 static void gtk_option_menu_size_allocate   (GtkWidget          *widget,
61                                              GtkAllocation      *allocation);
62 static void gtk_option_menu_paint           (GtkWidget          *widget,
63                                              GdkRectangle       *area);
64 static gint gtk_option_menu_expose          (GtkWidget          *widget,
65                                              GdkEventExpose     *event);
66 static gint gtk_option_menu_button_press    (GtkWidget          *widget,
67                                              GdkEventButton     *event);
68 static gint gtk_option_menu_key_press       (GtkWidget          *widget,
69                                              GdkEventKey        *event);
70 static void gtk_option_menu_deactivate      (GtkMenuShell       *menu_shell,
71                                              GtkOptionMenu      *option_menu);
72 static void gtk_option_menu_update_contents (GtkOptionMenu      *option_menu);
73 static void gtk_option_menu_remove_contents (GtkOptionMenu      *option_menu);
74 static void gtk_option_menu_calc_size       (GtkOptionMenu      *option_menu);
75 static void gtk_option_menu_position        (GtkMenu            *menu,
76                                              gint               *x,
77                                              gint               *y,
78                                              gint               *scroll_offet,
79                                              gpointer            user_data);
80 static void gtk_option_menu_show_all        (GtkWidget          *widget);
81 static void gtk_option_menu_hide_all        (GtkWidget          *widget);
82 static GtkType gtk_option_menu_child_type   (GtkContainer       *container);
83
84 enum
85 {
86   CHANGED,
87   LAST_SIGNAL
88 };
89
90 static GtkButtonClass *parent_class = NULL;
91 static guint           signals[LAST_SIGNAL] = { 0 };
92
93
94 GtkType
95 gtk_option_menu_get_type (void)
96 {
97   static GtkType option_menu_type = 0;
98
99   if (!option_menu_type)
100     {
101       static const GtkTypeInfo option_menu_info =
102       {
103         "GtkOptionMenu",
104         sizeof (GtkOptionMenu),
105         sizeof (GtkOptionMenuClass),
106         (GtkClassInitFunc) gtk_option_menu_class_init,
107         (GtkObjectInitFunc) gtk_option_menu_init,
108         /* reserved_1 */ NULL,
109         /* reserved_2 */ NULL,
110         (GtkClassInitFunc) NULL,
111       };
112
113       option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
114     }
115
116   return option_menu_type;
117 }
118
119 static void
120 gtk_option_menu_class_init (GtkOptionMenuClass *class)
121 {
122   GtkObjectClass *object_class;
123   GtkWidgetClass *widget_class;
124   GtkButtonClass *button_class;
125   GtkContainerClass *container_class;
126
127   object_class = (GtkObjectClass*) class;
128   widget_class = (GtkWidgetClass*) class;
129   button_class = (GtkButtonClass*) class;
130   container_class = (GtkContainerClass*) class;
131
132   parent_class = gtk_type_class (gtk_button_get_type ());
133
134   signals[CHANGED] =
135     g_signal_newc ("changed",
136                    G_OBJECT_CLASS_TYPE (class),
137                    G_SIGNAL_RUN_LAST,
138                    G_STRUCT_OFFSET (GtkOptionMenuClass, changed),
139                    NULL, NULL,
140                    gtk_marshal_VOID__VOID,
141                    G_TYPE_NONE, 0);
142   
143   object_class->destroy = gtk_option_menu_destroy;
144   
145   widget_class->size_request = gtk_option_menu_size_request;
146   widget_class->size_allocate = gtk_option_menu_size_allocate;
147   widget_class->expose_event = gtk_option_menu_expose;
148   widget_class->button_press_event = gtk_option_menu_button_press;
149   widget_class->key_press_event = gtk_option_menu_key_press;
150   widget_class->show_all = gtk_option_menu_show_all;
151   widget_class->hide_all = gtk_option_menu_hide_all;
152
153   container_class->child_type = gtk_option_menu_child_type;
154
155   gtk_widget_class_install_style_property (widget_class,
156                                            g_param_spec_boxed ("indicator_size",
157                                                                _("Indicator Size"),
158                                                                _("Size of dropdown indicator"),
159                                                                GTK_TYPE_REQUISITION,
160                                                                G_PARAM_READABLE));
161   gtk_widget_class_install_style_property (widget_class,
162                                            g_param_spec_boxed ("indicator_spacing",
163                                                                _("Indicator Spacing"),
164                                                                _("Spacing around indicator"),
165                                                                GTK_TYPE_BORDER,
166                                                                G_PARAM_READABLE));
167 }
168
169 static GtkType
170 gtk_option_menu_child_type (GtkContainer       *container)
171 {
172   return GTK_TYPE_NONE;
173 }
174
175 static void
176 gtk_option_menu_init (GtkOptionMenu *option_menu)
177 {
178   GTK_WIDGET_SET_FLAGS (option_menu, GTK_CAN_FOCUS);
179   GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_DEFAULT | GTK_RECEIVES_DEFAULT);
180
181   option_menu->menu = NULL;
182   option_menu->menu_item = NULL;
183   option_menu->width = 0;
184   option_menu->height = 0;
185 }
186
187 GtkWidget*
188 gtk_option_menu_new (void)
189 {
190   return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
191 }
192
193 GtkWidget*
194 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
195 {
196   g_return_val_if_fail (option_menu != NULL, NULL);
197   g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
198
199   return option_menu->menu;
200 }
201
202 static void
203 gtk_option_menu_detacher (GtkWidget     *widget,
204                           GtkMenu       *menu)
205 {
206   GtkOptionMenu *option_menu;
207
208   g_return_if_fail (widget != NULL);
209   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
210
211   option_menu = GTK_OPTION_MENU (widget);
212   g_return_if_fail (option_menu->menu == (GtkWidget*) menu);
213
214   gtk_option_menu_remove_contents (option_menu);
215   gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
216                                  option_menu);
217   
218   option_menu->menu = NULL;
219 }
220
221 void
222 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
223                           GtkWidget     *menu)
224 {
225   g_return_if_fail (option_menu != NULL);
226   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
227   g_return_if_fail (menu != NULL);
228   g_return_if_fail (GTK_IS_MENU (menu));
229
230   if (option_menu->menu != menu)
231     {
232       gtk_option_menu_remove_menu (option_menu);
233
234       option_menu->menu = menu;
235       gtk_menu_attach_to_widget (GTK_MENU (menu),
236                                  GTK_WIDGET (option_menu),
237                                  gtk_option_menu_detacher);
238
239       gtk_option_menu_calc_size (option_menu);
240
241       gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
242                           (GtkSignalFunc) gtk_option_menu_deactivate,
243                           option_menu);
244       gtk_signal_connect_object (GTK_OBJECT (option_menu->menu), "size_request",
245                                  (GtkSignalFunc) gtk_option_menu_calc_size,
246                                  GTK_OBJECT (option_menu));
247
248       if (GTK_WIDGET (option_menu)->parent)
249         gtk_widget_queue_resize (GTK_WIDGET (option_menu));
250
251       gtk_option_menu_update_contents (option_menu);
252     }
253 }
254
255 void
256 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
257 {
258   g_return_if_fail (option_menu != NULL);
259   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
260
261   if (option_menu->menu)
262     gtk_menu_detach (GTK_MENU (option_menu->menu));
263 }
264
265 void
266 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
267                              guint          index)
268 {
269   GtkWidget *menu_item;
270
271   g_return_if_fail (option_menu != NULL);
272   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
273
274   if (option_menu->menu)
275     {
276       gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
277       menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
278
279       if (menu_item != option_menu->menu_item)
280         gtk_option_menu_update_contents (option_menu);
281     }
282 }
283
284 /**
285  * gtk_option_menu_get_history:
286  * @option_menu: a #GtkOptionMenu
287  * 
288  * Retrieves the index of the currently selected menu item. The menu
289  * items are numbered from top to bottom, starting with 0. 
290  * 
291  * Return value: index of the selected menu item, or -1 if there are no menu items
292  **/
293 gint
294 gtk_option_menu_get_history (GtkOptionMenu *option_menu)
295 {
296   GtkWidget *active_widget;
297   
298   g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), -1);
299
300   if (option_menu->menu)
301     {
302       active_widget = gtk_menu_get_active (GTK_MENU (option_menu->menu));
303
304       if (active_widget)
305         return g_list_index (GTK_MENU_SHELL (option_menu->menu)->children,
306                              active_widget);
307       else
308         return -1;
309     }
310   else
311     return -1;
312 }
313
314 static void
315 gtk_option_menu_destroy (GtkObject *object)
316 {
317   GtkOptionMenu *option_menu;
318
319   g_return_if_fail (object != NULL);
320   g_return_if_fail (GTK_IS_OPTION_MENU (object));
321
322   option_menu = GTK_OPTION_MENU (object);
323
324   if (option_menu->menu)
325     gtk_widget_destroy (option_menu->menu);
326
327   if (GTK_OBJECT_CLASS (parent_class)->destroy)
328     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
329 }
330
331 static void
332 gtk_option_menu_get_props (GtkOptionMenu       *option_menu,
333                            GtkOptionMenuProps  *props)
334 {
335   GtkRequisition *indicator_size;
336   GtkBorder *indicator_spacing;
337   
338   gtk_widget_style_get (GTK_WIDGET (option_menu),
339                         "indicator_size", &indicator_size,
340                         "indicator_spacing", &indicator_spacing,
341                         "interior_focus", &props->interior_focus,
342                         NULL);
343
344   if (indicator_size)
345     props->indicator_size = *indicator_size;
346   else
347     props->indicator_size = default_props.indicator_size;
348
349   if (indicator_spacing)
350     props->indicator_spacing = *indicator_spacing;
351   else
352     props->indicator_spacing = default_props.indicator_spacing;
353
354   g_free (indicator_size);
355   g_free (indicator_spacing);
356 }
357
358 static void
359 gtk_option_menu_size_request (GtkWidget      *widget,
360                               GtkRequisition *requisition)
361 {
362   GtkOptionMenu *option_menu = GTK_OPTION_MENU (widget);
363   GtkOptionMenuProps props;
364   gint tmp;
365   GtkRequisition child_requisition = { 0, 0 };
366       
367   gtk_option_menu_get_props (option_menu, &props);
368  
369   if (GTK_BIN (option_menu)->child && GTK_WIDGET_VISIBLE (GTK_BIN (option_menu)->child))
370     {
371       gtk_widget_size_request (GTK_BIN (option_menu)->child, &child_requisition);
372
373       requisition->width += child_requisition.width;
374       requisition->height += child_requisition.height;
375     }
376   
377   requisition->width = ((GTK_CONTAINER (widget)->border_width +
378                          GTK_WIDGET (widget)->style->xthickness) * 2 +
379                         MAX (child_requisition.width, option_menu->width) +
380                         props.indicator_size.width +
381                         props.indicator_spacing.left + props.indicator_spacing.right +
382                         CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING + 2);
383   requisition->height = ((GTK_CONTAINER (widget)->border_width +
384                           GTK_WIDGET (widget)->style->ythickness) * 2 +
385                          MAX (child_requisition.height, option_menu->height) +
386                          CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING + 2);
387
388   tmp = (requisition->height - option_menu->height +
389          props.indicator_size.height + props.indicator_spacing.top + props.indicator_spacing.bottom);
390   requisition->height = MAX (requisition->height, tmp);
391 }
392
393 static void
394 gtk_option_menu_size_allocate (GtkWidget     *widget,
395                                GtkAllocation *allocation)
396 {
397   GtkWidget *child;
398   GtkAllocation child_allocation;
399   GtkOptionMenuProps props;
400   gint border_width;
401     
402   gtk_option_menu_get_props (GTK_OPTION_MENU (widget), &props);
403   border_width = GTK_CONTAINER (widget)->border_width;
404
405   widget->allocation = *allocation;
406   if (GTK_WIDGET_REALIZED (widget))
407     gdk_window_move_resize (widget->window,
408                             allocation->x + border_width, allocation->y + border_width,
409                             allocation->width - border_width * 2, allocation->height - border_width * 2);
410
411   child = GTK_BIN (widget)->child;
412   if (child && GTK_WIDGET_VISIBLE (child))
413     {
414       child_allocation.x = GTK_WIDGET (widget)->style->xthickness + 1;
415       child_allocation.y = GTK_CONTAINER (widget)->border_width + 1;
416       child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2 - border_width * 2 -
417                                     props.indicator_size.width - props.indicator_spacing.left - props.indicator_spacing.right -
418                                     CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING - 2);
419       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2 - border_width * 2 -
420                                      CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING - 2);
421       child_allocation.x += CHILD_LEFT_SPACING;
422       child_allocation.y += CHILD_TOP_SPACING;
423
424       gtk_widget_size_allocate (child, &child_allocation);
425     }
426 }
427
428 static void
429 gtk_option_menu_paint (GtkWidget    *widget,
430                        GdkRectangle *area)
431 {
432   GdkRectangle button_area;
433   GtkOptionMenuProps props;
434   gint border_width;
435
436   g_return_if_fail (widget != NULL);
437   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
438   g_return_if_fail (area != NULL);
439
440   if (GTK_WIDGET_DRAWABLE (widget))
441     {
442       border_width = GTK_CONTAINER (widget)->border_width;
443       gtk_option_menu_get_props (GTK_OPTION_MENU (widget), &props);
444
445       button_area.x = 0;
446       button_area.y = 0;
447       button_area.width = widget->allocation.width - 2 * border_width;
448       button_area.height = widget->allocation.height - 2 * border_width;
449
450       if (!props.interior_focus)
451         {
452           button_area.x += 1;
453           button_area.y += 1;
454           button_area.width -= 2;
455           button_area.height -= 2;
456
457           /* This is evil, and should be elimated here and in the button
458            * code. The point is to clear the focus, and make it
459            * sort of transparent if it isn't there.
460            */
461           gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
462           gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
463         }
464
465       gtk_paint_box(widget->style, widget->window,
466                     GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
467                     area, widget, "optionmenu",
468                     button_area.x, button_area.y,
469                     button_area.width, button_area.height);
470       
471       gtk_paint_tab (widget->style, widget->window,
472                      GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
473                      area, widget, "optionmenutab",
474                      button_area.x + button_area.width - 
475                      props.indicator_size.width - props.indicator_spacing.right -
476                      widget->style->xthickness,
477                      button_area.y + (button_area.height - props.indicator_size.height) / 2,
478                      props.indicator_size.width, props.indicator_size.height);
479       
480       if (GTK_WIDGET_HAS_FOCUS (widget))
481         {
482           if (props.interior_focus)
483             {
484               button_area.x += widget->style->xthickness + 1;
485               button_area.y += widget->style->ythickness + 1;
486               button_area.width -= 2 * (widget->style->xthickness + 1)
487                 + props.indicator_spacing.left + props.indicator_spacing.right + props.indicator_size.width;
488               button_area.height -= 2 * (widget->style->ythickness + 1);
489             }
490           else
491             {
492               button_area.x -= 1;
493               button_area.y -= 1;
494               button_area.width += 2;
495               button_area.height += 2;
496             }
497             
498           gtk_paint_focus (widget->style, widget->window,
499                            area, widget, "button",
500                            button_area.x, 
501                            button_area.y, 
502                            button_area.width - 1,
503                            button_area.height - 1);
504         }
505     }
506 }
507
508 static gint
509 gtk_option_menu_expose (GtkWidget      *widget,
510                         GdkEventExpose *event)
511 {
512   g_return_val_if_fail (widget != NULL, FALSE);
513   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
514   g_return_val_if_fail (event != NULL, FALSE);
515
516   if (GTK_WIDGET_DRAWABLE (widget))
517     {
518       gtk_option_menu_paint (widget, &event->area);
519
520
521       /* The following code tries to draw the child in two places at
522        * once. It fails miserably for several reasons
523        *
524        * - If the child is not no-window, removing generates
525        *   more expose events. Bad, bad, bad.
526        * 
527        * - Even if the child is no-window, removing it now (properly)
528        *   clears the space where it was, so it does no good
529        */
530       
531 #if 0
532       remove_child = FALSE;
533       child = GTK_BUTTON (widget)->child;
534
535       if (!child)
536         {
537           if (!GTK_OPTION_MENU (widget)->menu)
538             return FALSE;
539           gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
540           child = GTK_BUTTON (widget)->child;
541           if (!child)
542             return FALSE;
543           remove_child = TRUE;
544         }
545
546       child_event = *event;
547
548       if (GTK_WIDGET_NO_WINDOW (child) &&
549           gtk_widget_intersect (child, &event->area, &child_event.area))
550         gtk_widget_event (child, (GdkEvent*) &child_event);
551
552       if (remove_child)
553         gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
554 #else
555       if (GTK_BIN (widget)->child)
556         gtk_container_propagate_expose (GTK_CONTAINER (widget),
557                                         GTK_BIN (widget)->child,
558                                         event);
559 #endif /* 0 */
560     }
561
562   return FALSE;
563 }
564
565 static gint
566 gtk_option_menu_button_press (GtkWidget      *widget,
567                               GdkEventButton *event)
568 {
569   GtkOptionMenu *option_menu;
570   GtkWidget *menu_item;
571
572   g_return_val_if_fail (widget != NULL, FALSE);
573   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
574   g_return_val_if_fail (event != NULL, FALSE);
575
576   option_menu = GTK_OPTION_MENU (widget);
577
578   if ((event->type == GDK_BUTTON_PRESS) &&
579       (event->button == 1))
580     {
581       gtk_option_menu_remove_contents (option_menu);
582       gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
583                       gtk_option_menu_position, option_menu,
584                       event->button, event->time);
585       menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
586       if (menu_item)
587         gtk_menu_shell_select_item (GTK_MENU_SHELL (option_menu->menu), menu_item);
588       return TRUE;
589     }
590
591   return FALSE;
592 }
593
594 static gint
595 gtk_option_menu_key_press (GtkWidget   *widget,
596                            GdkEventKey *event)
597 {
598   GtkOptionMenu *option_menu;
599   GtkWidget *menu_item;
600
601   g_return_val_if_fail (widget != NULL, FALSE);
602   g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
603   g_return_val_if_fail (event != NULL, FALSE);
604
605   option_menu = GTK_OPTION_MENU (widget);
606
607   switch (event->keyval)
608     {
609     case GDK_KP_Space:
610     case GDK_space:
611       gtk_option_menu_remove_contents (option_menu);
612       gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
613                       gtk_option_menu_position, option_menu,
614                       0, event->time);
615       menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
616       if (menu_item)
617         gtk_menu_shell_select_item (GTK_MENU_SHELL (option_menu->menu), menu_item);
618       return TRUE;
619     }
620   
621   return FALSE;
622 }
623
624 static void
625 gtk_option_menu_deactivate (GtkMenuShell  *menu_shell,
626                             GtkOptionMenu *option_menu)
627 {
628   g_return_if_fail (menu_shell != NULL);
629   g_return_if_fail (option_menu != NULL);
630   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
631
632   gtk_option_menu_update_contents (option_menu);
633 }
634
635 static void
636 gtk_option_menu_changed (GtkOptionMenu *option_menu)
637 {
638   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
639
640   g_signal_emit (G_OBJECT (option_menu), signals[CHANGED], 0);
641 }
642
643 static void
644 gtk_option_menu_select_first_sensitive (GtkOptionMenu *option_menu)
645 {
646   if (option_menu->menu)
647     {
648       GList *children = GTK_MENU_SHELL (option_menu->menu)->children;
649       gint index = 0;
650
651       while (children)
652         {
653           if (GTK_WIDGET_SENSITIVE (children->data))
654             {
655               gtk_option_menu_set_history (option_menu, index);
656               return;
657             }
658           
659           children = children->next;
660           index++;
661         }
662     }
663 }
664
665 static void
666 gtk_option_menu_item_state_changed_cb (GtkWidget      *widget,
667                                        GtkStateType    previous_state,
668                                        GtkOptionMenu  *option_menu)
669 {
670   GtkWidget *child = GTK_BIN (option_menu)->child;
671
672   if (child && GTK_WIDGET_SENSITIVE (child) != GTK_WIDGET_IS_SENSITIVE (widget))
673     gtk_widget_set_sensitive (child, GTK_WIDGET_IS_SENSITIVE (widget));
674 }
675
676 static void
677 gtk_option_menu_item_destroy_cb (GtkWidget     *widget,
678                                  GtkOptionMenu *option_menu)
679 {
680   GtkWidget *child = GTK_BIN (option_menu)->child;
681
682   if (child)
683     {
684       gtk_widget_ref (child);
685       gtk_option_menu_remove_contents (option_menu);
686       gtk_widget_destroy (child);
687       gtk_widget_unref (child);
688
689       gtk_option_menu_select_first_sensitive (option_menu);
690     }
691 }
692
693 static void
694 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
695 {
696   GtkWidget *child;
697   GtkRequisition child_requisition;
698
699   g_return_if_fail (option_menu != NULL);
700   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
701
702   if (option_menu->menu)
703     {
704       GtkWidget *old_item = option_menu->menu_item;
705       
706       gtk_option_menu_remove_contents (option_menu);
707
708       option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
709       if (option_menu->menu_item)
710         {
711           gtk_widget_ref (option_menu->menu_item);
712           child = GTK_BIN (option_menu->menu_item)->child;
713           if (child)
714             {
715               if (!GTK_WIDGET_IS_SENSITIVE (option_menu->menu_item))
716                 gtk_widget_set_sensitive (child, FALSE);
717               gtk_widget_reparent (child, GTK_WIDGET (option_menu));
718             }
719
720           gtk_signal_connect (GTK_OBJECT (option_menu->menu_item), "state_changed",
721                               GTK_SIGNAL_FUNC (gtk_option_menu_item_state_changed_cb), option_menu);
722           gtk_signal_connect (GTK_OBJECT (option_menu->menu_item), "destroy",
723                               GTK_SIGNAL_FUNC (gtk_option_menu_item_destroy_cb), option_menu);
724
725           gtk_widget_size_request (child, &child_requisition);
726           gtk_widget_size_allocate (GTK_WIDGET (option_menu),
727                                     &(GTK_WIDGET (option_menu)->allocation));
728
729           if (GTK_WIDGET_DRAWABLE (option_menu))
730             gtk_widget_queue_draw (GTK_WIDGET (option_menu));
731         }
732
733       if (old_item != option_menu->menu_item)
734         gtk_option_menu_changed (option_menu);
735     }
736 }
737
738 static void
739 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
740 {
741   GtkWidget *child;
742   
743   g_return_if_fail (option_menu != NULL);
744   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
745
746   if (option_menu->menu_item)
747     {
748       child = GTK_BIN (option_menu)->child;
749   
750       if (child)
751         {
752           gtk_widget_set_sensitive (child, TRUE);
753           gtk_widget_reparent (child, option_menu->menu_item);
754         }
755
756       gtk_signal_disconnect_by_func (GTK_OBJECT (option_menu->menu_item),
757                                      GTK_SIGNAL_FUNC (gtk_option_menu_item_state_changed_cb),
758                                      option_menu);                                   
759       gtk_signal_disconnect_by_func (GTK_OBJECT (option_menu->menu_item),
760                                      GTK_SIGNAL_FUNC (gtk_option_menu_item_destroy_cb),
761                                      option_menu);   
762       
763       gtk_widget_unref (option_menu->menu_item);
764       option_menu->menu_item = NULL;
765     }
766 }
767
768 static void
769 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
770 {
771   GtkWidget *child;
772   GList *children;
773   GtkRequisition child_requisition;
774   gint old_width = option_menu->width;
775   gint old_height = option_menu->height;
776
777   g_return_if_fail (option_menu != NULL);
778   g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
779
780   option_menu->width = 0;
781   option_menu->height = 0;
782
783   if (option_menu->menu)
784     {
785       children = GTK_MENU_SHELL (option_menu->menu)->children;
786       while (children)
787         {
788           child = children->data;
789           children = children->next;
790
791           if (GTK_WIDGET_VISIBLE (child))
792             {
793               gtk_widget_size_request (child, &child_requisition);
794
795               option_menu->width = MAX (option_menu->width, child_requisition.width);
796               option_menu->height = MAX (option_menu->height, child_requisition.height);
797             }
798         }
799     }
800
801   if (old_width != option_menu->width || old_height != option_menu->height)
802     gtk_widget_queue_resize (GTK_WIDGET (option_menu));
803 }
804
805 static void
806 gtk_option_menu_position (GtkMenu  *menu,
807                           gint     *x,
808                           gint     *y,
809                           gboolean *push_in,
810                           gpointer  user_data)
811 {
812   GtkOptionMenu *option_menu;
813   GtkWidget *active;
814   GtkWidget *child;
815   GtkRequisition requisition;
816   GList *children;
817   gint screen_width;
818   gint menu_xpos;
819   gint menu_ypos;
820   gint menu_width;
821
822   g_return_if_fail (user_data != NULL);
823   g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
824
825   option_menu = GTK_OPTION_MENU (user_data);
826
827   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
828   menu_width = requisition.width;
829
830   active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
831   gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
832
833   menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
834
835   if (active != NULL)
836     {
837       gtk_widget_get_child_requisition (active, &requisition);
838       menu_ypos -= requisition.height / 2;
839     }
840
841   children = GTK_MENU_SHELL (option_menu->menu)->children;
842   while (children)
843     {
844       child = children->data;
845
846       if (active == child)
847         break;
848
849       if (GTK_WIDGET_VISIBLE (child))
850         {
851           gtk_widget_get_child_requisition (child, &requisition);
852           menu_ypos -= requisition.height;
853         }
854
855       children = children->next;
856     }
857
858   screen_width = gdk_screen_width ();
859   
860   if (menu_xpos < 0)
861     menu_xpos = 0;
862   else if ((menu_xpos + menu_width) > screen_width)
863     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
864
865   *x = menu_xpos;
866   *y = menu_ypos;
867   *push_in = TRUE;
868 }
869
870
871 static void
872 gtk_option_menu_show_all (GtkWidget *widget)
873 {
874   GtkContainer *container;
875   GtkOptionMenu *option_menu;
876   
877   g_return_if_fail (widget != NULL);
878   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
879   container = GTK_CONTAINER (widget);
880   option_menu = GTK_OPTION_MENU (widget);
881
882   gtk_widget_show (widget);
883   gtk_container_foreach (container, (GtkCallback) gtk_widget_show_all, NULL);
884   if (option_menu->menu)
885     gtk_widget_show_all (option_menu->menu);
886   if (option_menu->menu_item)
887     gtk_widget_show_all (option_menu->menu_item);
888 }
889
890
891 static void
892 gtk_option_menu_hide_all (GtkWidget *widget)
893 {
894   GtkContainer *container;
895
896   g_return_if_fail (widget != NULL);
897   g_return_if_fail (GTK_IS_OPTION_MENU (widget));
898   container = GTK_CONTAINER (widget);
899
900   gtk_widget_hide (widget);
901   gtk_container_foreach (container, (GtkCallback) gtk_widget_hide_all, NULL);
902 }
903