]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombo.c
do not just return if the combo-list is empty since that will cause the
[~andy/gtk] / gtk / gtkcombo.c
1 /* gtkcombo - combo widget for gtk+
2  * Copyright 1997 Paolo Molaro
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
20 #include <string.h>
21
22 #include "gtkarrow.h"
23 #include "gtklabel.h"
24 #include "gtklist.h"
25 #include "gtkentry.h"
26 #include "gtkeventbox.h"
27 #include "gtkbutton.h"
28 #include "gtklistitem.h"
29 #include "gtkscrolledwindow.h"
30 #include "gtkmain.h"
31 #include "gtksignal.h"
32 #include "gtkwindow.h"
33 #include "gdk/gdkkeysyms.h"
34 #include "gtkcombo.h"
35 #include "gtkframe.h"
36
37 const gchar *gtk_combo_string_key = "gtk-combo-string-value";
38
39 #define COMBO_LIST_MAX_HEIGHT   (400)
40 #define EMPTY_LIST_HEIGHT       (15)
41
42 static void         gtk_combo_class_init      (GtkComboClass *klass);
43 static void         gtk_combo_init            (GtkCombo      *combo);
44 static void         gtk_combo_destroy         (GtkObject     *combo);
45 static GtkListItem *gtk_combo_find            (GtkCombo      *combo);
46 static gchar *      gtk_combo_func            (GtkListItem  *li);
47 static gint         gtk_combo_focus_idle      (GtkCombo      *combo);
48 static gint         gtk_combo_entry_focus_out (GtkEntry      *entry, 
49                                                GdkEventFocus *event, 
50                                                GtkCombo      *combo);
51 static void         gtk_combo_get_pos         (GtkCombo      *combo, 
52                                                gint          *x, 
53                                                gint          *y, 
54                                                gint          *height, 
55                                                gint          *width);
56 static void         gtk_combo_popup_list      (GtkCombo      *combo);
57 static void         gtk_combo_activate        (GtkWidget        *widget,
58                                                GtkCombo         *combo);
59 static void         gtk_combo_popup_button_press (GtkWidget        *button,
60                                                   GdkEventButton   *event,
61                                                   GtkCombo         *combo);
62 static void         gtk_combo_popup_button_leave (GtkWidget        *button,
63                                                   GdkEventCrossing *event,
64                                                   GtkCombo         *combo);
65 static void         gtk_combo_update_entry    (GtkList       *list, 
66                                                GtkCombo      *combo);
67 static void         gtk_combo_update_list     (GtkEntry      *entry, 
68                                                GtkCombo      *combo);
69 static gint         gtk_combo_button_press    (GtkWidget     *widget,
70                                                GdkEvent      *event,
71                                                GtkCombo      *combo);
72 static gint         gtk_combo_button_release  (GtkWidget     *widget,
73                                                GdkEvent      *event,
74                                                GtkCombo      *combo);
75 static gint         gtk_combo_list_enter      (GtkWidget        *widget,
76                                                GdkEventCrossing *event,
77                                                GtkCombo         *combo);
78 static gint         gtk_combo_list_key_press  (GtkWidget     *widget, 
79                                                GdkEventKey   *event, 
80                                                GtkCombo      *combo);
81 static gint         gtk_combo_entry_key_press (GtkEntry      *widget, 
82                                                GdkEventKey   *event, 
83                                                GtkCombo      *combo);
84 static void         gtk_combo_item_destroy    (GtkObject     *object);
85 static void         gtk_combo_size_allocate   (GtkWidget     *widget,
86                                                GtkAllocation *allocation);
87
88 static GtkHBoxClass *parent_class = NULL;
89
90 static void
91 gtk_combo_class_init (GtkComboClass * klass)
92 {
93   GtkObjectClass *oclass;
94   GtkWidgetClass *widget_class;
95
96   parent_class = gtk_type_class (gtk_hbox_get_type ());
97   oclass = (GtkObjectClass *) klass;
98   widget_class = (GtkWidgetClass *) klass;
99
100   oclass->destroy = gtk_combo_destroy;
101   
102   widget_class->size_allocate = gtk_combo_size_allocate;
103 }
104
105 static void
106 gtk_combo_destroy (GtkObject * combo)
107 {
108   gtk_widget_destroy (GTK_COMBO (combo)->popwin);
109   gtk_widget_unref (GTK_COMBO (combo)->popwin);
110
111   if (GTK_OBJECT_CLASS (parent_class)->destroy)
112     (*GTK_OBJECT_CLASS (parent_class)->destroy) (combo);
113 }
114
115 static int
116 gtk_combo_entry_key_press (GtkEntry * entry, GdkEventKey * event, GtkCombo * combo)
117 {
118   GList *li;
119
120   /* completion? */
121   /*if ( event->keyval == GDK_Tab ) {
122      gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
123      return TRUE;
124      } else */
125   if (!combo->use_arrows || !GTK_LIST (combo->list)->children)
126     return FALSE;
127   li = g_list_find (GTK_LIST (combo->list)->children, gtk_combo_find (combo));
128
129   if ((event->keyval == GDK_Up)
130       || (event->keyval == GDK_KP_Up)
131       || ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'p') || (event->keyval == 'P'))))
132     {
133       if (li)
134         li = li->prev;
135       if (!li && combo->use_arrows_always)
136         {
137           li = g_list_last (GTK_LIST (combo->list)->children);
138         }
139       if (li)
140         {
141           gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
142           gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
143           return TRUE;
144         }
145     }
146   else if ((event->keyval == GDK_Down)
147            || (event->keyval == GDK_KP_Down)
148            || ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'n') || (event->keyval == 'N'))))
149     {
150       if (li)
151         li = li->next;
152       if (!li && combo->use_arrows_always)
153         {
154           li = GTK_LIST (combo->list)->children;
155         }
156       if (li)
157         {
158           gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
159           gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
160           return TRUE;
161         }
162     }
163   return FALSE;
164 }
165
166 static GtkListItem *
167 gtk_combo_find (GtkCombo * combo)
168 {
169   gchar *text;
170   gchar *ltext;
171   GList *clist;
172   int (*string_compare) (const char *, const char *);
173
174   if (combo->case_sensitive)
175     string_compare = strcmp;
176   else
177     string_compare = g_strcasecmp;
178
179   text = gtk_entry_get_text (GTK_ENTRY (combo->entry));
180   clist = GTK_LIST (combo->list)->children;
181
182   while (clist && clist->data)
183     {
184       ltext = gtk_combo_func (GTK_LIST_ITEM (clist->data));
185       if (!ltext)
186         continue;
187       if (!(*string_compare) (ltext, text))
188         return (GtkListItem *) clist->data;
189       clist = clist->next;
190     }
191
192   return NULL;
193 }
194
195 static gchar *
196 gtk_combo_func (GtkListItem * li)
197 {
198   GtkWidget *label;
199   gchar *ltext = NULL;
200
201   ltext = (gchar *) gtk_object_get_data (GTK_OBJECT (li), gtk_combo_string_key);
202   if (!ltext)
203     {
204       label = GTK_BIN (li)->child;
205       if (!label || !GTK_IS_LABEL (label))
206         return NULL;
207       gtk_label_get (GTK_LABEL (label), &ltext);
208     }
209   return ltext;
210 }
211
212 static gint
213 gtk_combo_focus_idle (GtkCombo * combo)
214 {
215   if (combo)
216     gtk_widget_grab_focus (combo->entry);
217   return FALSE;
218 }
219
220 static gint
221 gtk_combo_entry_focus_out (GtkEntry * entry, GdkEventFocus * event, GtkCombo * combo)
222 {
223
224   if (combo->value_in_list && !gtk_combo_find (combo))
225     {
226       /* gdk_beep(); *//* this can be annoying */
227       if (combo->ok_if_empty && !strcmp (gtk_entry_get_text (entry), ""))
228         return FALSE;
229 #ifdef TEST
230       printf ("INVALID ENTRY: `%s'\n", gtk_entry_get_text (entry));
231 #endif
232       gtk_grab_add (GTK_WIDGET (combo));
233       /* this is needed because if we call gtk_widget_grab_focus() 
234          it isn't guaranteed it's the *last* call before the main-loop,
235          so the focus can be lost anyway...
236          the signal_emit_stop doesn't seem to work either...
237        */
238       gtk_idle_add ((GtkFunction) gtk_combo_focus_idle, combo);
239       /*gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "focus_out_event"); */
240       return TRUE;
241     }
242   return FALSE;
243 }
244
245 static void
246 gtk_combo_get_pos (GtkCombo * combo, gint * x, gint * y, gint * height, gint * width)
247 {
248   GtkBin *popwin;
249   GtkWidget *widget;
250   GtkScrolledWindow *popup;
251   
252   gint real_height;
253   GtkRequisition list_requisition;
254   gboolean show_hscroll = FALSE;
255   gboolean show_vscroll = FALSE;
256   gint avail_height;
257   gint min_height;
258   gint alloc_width;
259   gint work_height;
260   gint old_height;
261   gint old_width;
262   
263   widget = GTK_WIDGET(combo);
264   popup  = GTK_SCROLLED_WINDOW (combo->popup);
265   popwin = GTK_BIN (combo->popwin);
266   
267   gdk_window_get_origin (combo->entry->window, x, y);
268   real_height = MIN (combo->entry->requisition.height, 
269                      combo->entry->allocation.height);
270   *y += real_height;
271   avail_height = gdk_screen_height () - *y;
272   
273   gtk_widget_size_request (combo->list, &list_requisition);
274   min_height = MIN (list_requisition.height, 
275                     popup->vscrollbar->requisition.height);
276   if (!GTK_LIST (combo->list)->children)
277     list_requisition.height += EMPTY_LIST_HEIGHT;
278   
279   alloc_width = (widget->allocation.width -
280                  2 * popwin->child->style->klass->xthickness -
281                  2 * GTK_CONTAINER (popwin->child)->border_width -
282                  2 * GTK_CONTAINER (combo->popup)->border_width -
283                  2 * GTK_CONTAINER (popup->viewport)->border_width - 
284                  2 * popup->viewport->style->klass->xthickness);
285   
286   work_height = (2 * popwin->child->style->klass->ythickness +
287                  2 * GTK_CONTAINER (popwin->child)->border_width +
288                  2 * GTK_CONTAINER (combo->popup)->border_width +
289                  2 * GTK_CONTAINER (popup->viewport)->border_width +
290                  2 * popup->viewport->style->klass->xthickness);
291   
292   do 
293     {
294       old_width = alloc_width;
295       old_height = work_height;
296       
297       if (!show_hscroll &&
298           alloc_width < list_requisition.width)
299         {
300           work_height += popup->hscrollbar->requisition.height +
301             GTK_SCROLLED_WINDOW_CLASS 
302             (GTK_OBJECT (combo->popup)->klass)->scrollbar_spacing;
303           show_hscroll = TRUE;
304         }
305       if (!show_vscroll && 
306           work_height + list_requisition.height > avail_height)
307         {
308           if (work_height + min_height > avail_height && 
309               *y - real_height > avail_height)
310             {
311               *y -= (work_height + list_requisition.height + real_height);
312               break;
313             }
314           alloc_width -= 
315             popup->vscrollbar->requisition.width +
316             GTK_SCROLLED_WINDOW_CLASS 
317             (GTK_OBJECT (combo->popup)->klass)->scrollbar_spacing;
318           show_vscroll = TRUE;
319         }
320     } while (old_width != alloc_width || old_height != work_height);
321   
322   *width = widget->allocation.width;
323   if (show_vscroll)
324     *height = avail_height;
325   else
326     *height = work_height + list_requisition.height;
327   
328   if (*x < 0)
329     *x = 0;
330 }
331
332 static void
333 gtk_combo_popup_list (GtkCombo * combo)
334 {
335   gint height, width, x, y;
336   gint old_width, old_height;
337
338   old_width = combo->popwin->allocation.width;
339   old_height  = combo->popwin->allocation.height;
340
341   gtk_combo_get_pos (combo, &x, &y, &height, &width);
342
343   /* workaround for gtk_scrolled_window_size_allocate bug */
344   if (old_width != width || old_height != height)
345     {
346       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar);
347       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar);
348     }
349
350   gtk_widget_set_uposition (combo->popwin, x, y);
351   gtk_widget_set_usize (combo->popwin, width, height);
352   gtk_widget_realize (combo->popwin);
353   gdk_window_resize (combo->popwin->window, width, height);
354   gtk_widget_show (combo->popwin);
355
356   gtk_widget_grab_focus (combo->popwin);
357 }
358
359 static void        
360 gtk_combo_activate (GtkWidget        *widget,
361                     GtkCombo         *combo)
362 {
363   gtk_combo_popup_list (combo);
364
365   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
366     gtk_widget_grab_focus (combo->entry);
367   gtk_grab_add (combo->popwin);
368   gdk_pointer_grab (combo->popwin->window, TRUE,
369                     GDK_BUTTON_PRESS_MASK | 
370                     GDK_BUTTON_RELEASE_MASK |
371                     GDK_POINTER_MOTION_MASK, 
372                     NULL, NULL, GDK_CURRENT_TIME);
373 }
374
375 static void        
376 gtk_combo_popup_button_press (GtkWidget        *button,
377                               GdkEventButton   *event,
378                               GtkCombo         *combo)
379 {
380   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
381     gtk_widget_grab_focus (combo->entry);
382   if (!combo->current_button && (event->button == 1))
383     gtk_combo_popup_list (combo);
384
385   combo->current_button = event->button;
386 }
387
388 static void         
389 gtk_combo_popup_button_leave (GtkWidget        *button,
390                               GdkEventCrossing *event,
391                               GtkCombo         *combo)
392 {
393   if (combo->current_button)
394     gtk_signal_emit_stop_by_name (GTK_OBJECT (button), "leave_notify_event");
395 }
396
397
398 static void
399 gtk_combo_update_entry (GtkList * list, GtkCombo * combo)
400 {
401   char *text;
402
403   gtk_grab_remove (GTK_WIDGET (combo));
404   gtk_signal_handler_block (GTK_OBJECT (list), combo->list_change_id);
405   if (list->selection)
406     {
407       text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
408       if (!text)
409         text = "";
410       gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
411     }
412   gtk_signal_handler_unblock (GTK_OBJECT (list), combo->list_change_id);
413 }
414
415 static void
416 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
417 {
418   GtkList *list = GTK_LIST (combo->list);
419   GList *slist = list->selection;
420   GtkListItem *li;
421
422   gtk_grab_remove (GTK_WIDGET (combo));
423
424   gtk_signal_handler_block (GTK_OBJECT (entry), combo->entry_change_id);
425   if (slist && slist->data)
426     gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
427   li = gtk_combo_find (combo);
428   if (li)
429     gtk_list_select_child (list, GTK_WIDGET (li));
430   gtk_signal_handler_unblock (GTK_OBJECT (entry), combo->entry_change_id);
431 }
432
433 static gint
434 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
435 {
436   GtkWidget *child;
437
438   child = gtk_get_event_widget (event);
439
440   /* We don't ask for button press events on the grab widget, so
441    *  if an event is reported directly to the grab widget, it must
442    *  be on a window outside the application (and thus we remove
443    *  the popup window). Otherwise, we check if the widget is a child
444    *  of the grab widget, and only remove the popup window if it
445    *  is not.
446    */
447   if (child != widget)
448     {
449       while (child)
450         {
451           if (child == widget)
452             return FALSE;
453           child = child->parent;
454         }
455     }
456
457   gtk_widget_hide (combo->popwin);
458   gtk_grab_remove (combo->popwin);
459   gdk_pointer_ungrab (event->button.time);
460
461   return TRUE;
462 }
463
464 static gint
465 gtk_combo_button_release (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
466 {
467   GtkWidget *child;
468
469   if ((combo->current_button != 0) && (event->button.button == 1))
470     {
471       /* This was the initial button press */
472
473       GdkEventCrossing tmp_event;
474
475       combo->current_button = 0;
476
477       if (widget != combo->button)
478         gtk_widget_event (combo->button, event);
479
480       /* Un-pre-hightlight */
481       
482       tmp_event.type = GDK_LEAVE_NOTIFY;
483       tmp_event.window = combo->button->window;
484       tmp_event.send_event = TRUE;
485       tmp_event.subwindow = NULL;
486       tmp_event.detail = GDK_NOTIFY_ANCESTOR;
487       
488       gtk_widget_event (combo->button, (GdkEvent *)&tmp_event);
489
490       /* Check to see if we released inside the button */
491       child = gtk_get_event_widget ((GdkEvent*) event);
492
493       while (child && child != (combo->button))
494         child = child->parent;
495
496       if (child == combo->button)
497         {
498           gtk_grab_add (combo->popwin);
499           gdk_pointer_grab (combo->popwin->window, TRUE,
500                             GDK_BUTTON_PRESS_MASK | 
501                             GDK_BUTTON_RELEASE_MASK |
502                             GDK_POINTER_MOTION_MASK, 
503                             NULL, NULL, GDK_CURRENT_TIME);
504           return FALSE;
505         }
506     }
507   else
508     {
509       /* The user has clicked inside the popwin and released */
510
511       gtk_grab_remove (combo->popwin);
512       gdk_pointer_ungrab (event->button.time);
513     }
514   
515   gtk_widget_hide (combo->popwin);
516
517   return TRUE;
518 }
519
520 static gint         
521 gtk_combo_list_enter (GtkWidget        *widget,
522                       GdkEventCrossing *event,
523                       GtkCombo         *combo)
524 {
525   GtkWidget *event_widget;
526
527   event_widget = gtk_get_event_widget ((GdkEvent*) event);
528   
529   if ((event_widget == combo->list) &&
530       (combo->current_button != 0) && 
531       (!GTK_WIDGET_HAS_GRAB (combo->list)))
532     {
533       GdkEvent tmp_event;
534       gint x, y;
535       GdkModifierType mask;
536
537       gtk_grab_remove (combo->popwin);
538
539       /* Transfer the grab over to the list by synthesizing
540        * a button press event
541        */
542       gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
543
544       tmp_event.button.type = GDK_BUTTON_PRESS;
545       tmp_event.button.window = combo->list->window;
546       tmp_event.button.send_event = TRUE;
547       tmp_event.button.time = GDK_CURRENT_TIME; /* bad */
548       tmp_event.button.x = x;
549       tmp_event.button.y = y;
550       /* We leave all the XInput fields unfilled here, in the expectation
551        * that GtkList doesn't care.
552        */
553       tmp_event.button.button = combo->current_button;
554       tmp_event.button.state = mask;
555
556       gtk_widget_event (combo->list, &tmp_event);
557     }
558
559   return FALSE;
560 }
561
562 static int
563 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
564 {
565   if (event->keyval == GDK_Escape)
566     {
567       gtk_widget_hide (combo->popwin);
568       gtk_grab_remove (combo->popwin);
569       gdk_pointer_ungrab (GDK_CURRENT_TIME);
570       return TRUE;
571     }
572   return FALSE;
573 }
574
575 static void
576 gtk_combo_init (GtkCombo * combo)
577 {
578   GtkWidget *arrow;
579   GtkWidget *frame;
580   GtkWidget *event_box;
581   GdkCursor *cursor;
582
583   combo->case_sensitive = 0;
584   combo->value_in_list = 0;
585   combo->ok_if_empty = 1;
586   combo->use_arrows = 1;
587   combo->use_arrows_always = 0;
588   combo->entry = gtk_entry_new ();
589   combo->button = gtk_button_new ();
590   combo->current_button = 0;
591   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
592   gtk_widget_show (arrow);
593   gtk_container_add (GTK_CONTAINER (combo->button), arrow);
594   gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
595   gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
596   GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
597   gtk_widget_show (combo->entry);
598   gtk_widget_show (combo->button);
599   combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
600                               (GtkSignalFunc) gtk_combo_update_list, combo);
601   gtk_signal_connect (GTK_OBJECT (combo->entry), "key_press_event",
602                       (GtkSignalFunc) gtk_combo_entry_key_press, combo);
603   gtk_signal_connect_after (GTK_OBJECT (combo->entry), "focus_out_event",
604                             (GtkSignalFunc) gtk_combo_entry_focus_out, combo);
605   combo->activate_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "activate",
606                       (GtkSignalFunc) gtk_combo_activate, combo);
607   gtk_signal_connect_after (GTK_OBJECT (combo->button), "button_press_event",
608                             (GtkSignalFunc) gtk_combo_popup_button_press, combo);
609   gtk_signal_connect_after (GTK_OBJECT (combo->button), "button_release_event",
610                             (GtkSignalFunc) gtk_combo_button_release, combo);
611   gtk_signal_connect (GTK_OBJECT (combo->button), "leave_notify_event",
612                       (GtkSignalFunc) gtk_combo_popup_button_leave, combo);
613   /*gtk_signal_connect(GTK_OBJECT(combo->button), "clicked",
614      (GtkSignalFunc)prelight_bug, combo); */
615
616   combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
617   gtk_widget_ref (combo->popwin);
618   gtk_window_set_policy (GTK_WINDOW (combo->popwin), 1, 1, 0);
619   
620   gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
621
622   event_box = gtk_event_box_new ();
623   gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
624   gtk_widget_show (event_box);
625
626   gtk_widget_realize (event_box);
627   cursor = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
628   gdk_window_set_cursor (event_box->window, cursor);
629   gdk_cursor_destroy (cursor);
630
631   frame = gtk_frame_new (NULL);
632   gtk_container_add (GTK_CONTAINER (event_box), frame);
633   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
634   gtk_widget_show (frame);
635
636   combo->popup = gtk_scrolled_window_new (NULL, NULL);
637   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
638                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
639   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
640   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
641   gtk_container_add (GTK_CONTAINER (frame), combo->popup);
642   gtk_widget_show (combo->popup);
643
644   combo->list = gtk_list_new ();
645   /* We'll use enter notify events to figure out when to transfer
646    * the grab to the list
647    */
648   gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
649
650   gtk_list_set_selection_mode(GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
651   gtk_container_add (GTK_CONTAINER (combo->popup), combo->list);
652   gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
653                                        gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
654   gtk_widget_show (combo->list);
655
656   combo->list_change_id = gtk_signal_connect (GTK_OBJECT (combo->list), "selection_changed",
657                              (GtkSignalFunc) gtk_combo_update_entry, combo);
658   gtk_signal_connect (GTK_OBJECT (combo->popwin), "key_press_event",
659                       (GtkSignalFunc) gtk_combo_list_key_press, combo);
660   gtk_signal_connect (GTK_OBJECT (combo->popwin), "button_press_event",
661                       GTK_SIGNAL_FUNC (gtk_combo_button_press), combo);
662
663   gtk_signal_connect (GTK_OBJECT (combo->list), "button_release_event",
664                       GTK_SIGNAL_FUNC (gtk_combo_button_release), combo);
665   /* We connect here on the button, because we'll have a grab on it
666    * when the event occurs. But we are actually interested in enters
667    * for the combo->list.
668    */
669   gtk_signal_connect (GTK_OBJECT (combo->button), "enter_notify_event",
670                       GTK_SIGNAL_FUNC (gtk_combo_list_enter), combo);
671 }
672
673 guint
674 gtk_combo_get_type ()
675 {
676   static guint combo_type = 0;
677
678   if (!combo_type)
679     {
680       GtkTypeInfo combo_info =
681       {
682         "GtkCombo",
683         sizeof (GtkCombo),
684         sizeof (GtkComboClass),
685         (GtkClassInitFunc) gtk_combo_class_init,
686         (GtkObjectInitFunc) gtk_combo_init,
687         (GtkArgSetFunc) NULL,
688         (GtkArgGetFunc) NULL,
689       };
690       combo_type = gtk_type_unique (gtk_hbox_get_type (), &combo_info);
691     }
692   return combo_type;
693 }
694
695 GtkWidget *
696 gtk_combo_new ()
697 {
698   return GTK_WIDGET (gtk_type_new (gtk_combo_get_type ()));
699 }
700
701 void
702 gtk_combo_set_value_in_list (GtkCombo * combo, gint val, gint ok_if_empty)
703 {
704   g_return_if_fail (combo != NULL);
705   g_return_if_fail (GTK_IS_COMBO (combo));
706
707   combo->value_in_list = val;
708   combo->ok_if_empty = ok_if_empty;
709 }
710
711 void
712 gtk_combo_set_case_sensitive (GtkCombo * combo, gint val)
713 {
714   g_return_if_fail (combo != NULL);
715   g_return_if_fail (GTK_IS_COMBO (combo));
716
717   combo->case_sensitive = val;
718 }
719
720 void
721 gtk_combo_set_use_arrows (GtkCombo * combo, gint val)
722 {
723   g_return_if_fail (combo != NULL);
724   g_return_if_fail (GTK_IS_COMBO (combo));
725
726   combo->use_arrows = val;
727 }
728
729 void
730 gtk_combo_set_use_arrows_always (GtkCombo * combo, gint val)
731 {
732   g_return_if_fail (combo != NULL);
733   g_return_if_fail (GTK_IS_COMBO (combo));
734
735   combo->use_arrows_always = val;
736   combo->use_arrows = 1;
737 }
738
739 void
740 gtk_combo_set_popdown_strings (GtkCombo * combo, GList * strings)
741 {
742   GList *list;
743   GtkWidget *li;
744
745   g_return_if_fail (combo != NULL);
746   g_return_if_fail (GTK_IS_COMBO (combo));
747   g_return_if_fail (strings != NULL);
748
749   gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
750   list = strings;
751   while (list)
752     {
753       li = gtk_list_item_new_with_label ((gchar *) list->data);
754       gtk_widget_show (li);
755       gtk_container_add (GTK_CONTAINER (combo->list), li);
756       list = list->next;
757     }
758 }
759
760 static void
761 gtk_combo_item_destroy (GtkObject * object)
762 {
763   gchar *key;
764
765   key = gtk_object_get_data (object, gtk_combo_string_key);
766   if (key)
767     g_free (key);
768 }
769
770 void
771 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
772 {
773   gchar *val;
774   gint connected = 0;
775
776   g_return_if_fail (combo != NULL);
777   g_return_if_fail (GTK_IS_COMBO (combo));
778   g_return_if_fail (item != NULL);
779
780   val = gtk_object_get_data (GTK_OBJECT (item), gtk_combo_string_key);
781   if (val) 
782     {
783       g_free (val);
784       connected = 1;
785     }
786   if (item_value)
787     {
788       val = g_strdup(item_value);
789       gtk_object_set_data (GTK_OBJECT (item), gtk_combo_string_key, val);
790       if (!connected)
791         gtk_signal_connect (GTK_OBJECT (item), "destroy",
792                           (GtkSignalFunc) gtk_combo_item_destroy, val);
793     }
794   else 
795     {
796       gtk_object_set_data (GTK_OBJECT (item), gtk_combo_string_key, NULL);
797       if (connected)
798         gtk_signal_disconnect_by_data(GTK_OBJECT (item), val);
799     }
800 }
801
802 static void
803 gtk_combo_size_allocate (GtkWidget     *widget,
804                          GtkAllocation *allocation)
805 {
806   GtkCombo *combo;
807
808   g_return_if_fail (widget != NULL);
809   g_return_if_fail (GTK_IS_COMBO (widget));
810   g_return_if_fail (allocation != NULL);
811
812   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
813   
814   combo = GTK_COMBO (widget);
815
816   if (combo->entry->allocation.height > combo->entry->requisition.height)
817     {
818       GtkAllocation button_allocation;
819
820       button_allocation = combo->button->allocation;
821       button_allocation.height = combo->entry->requisition.height;
822       button_allocation.y = combo->entry->allocation.y + 
823         (combo->entry->allocation.height - combo->entry->requisition.height) 
824         / 2;
825       gtk_widget_size_allocate (combo->button, &button_allocation);
826     }
827 }
828
829 void
830 gtk_combo_disable_activate (GtkCombo* combo)
831 {
832   g_return_if_fail (combo != NULL);
833   g_return_if_fail (GTK_IS_COMBO (combo));
834
835   if ( combo->activate_id ) {
836     gtk_signal_disconnect(GTK_OBJECT(combo->entry), combo->activate_id);
837     combo->activate_id = 0;
838   }
839 }