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