]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombo.c
Register all stock icons "unscaled" so that we don't have icons only
[~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 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 /* Do NOT, I repeat, NOT, copy any of the code in this file.
28  * The code here relies on all sorts of internal details of GTK+
29  */
30
31 #include <string.h>
32
33 #include "gtkarrow.h"
34 #include "gtklabel.h"
35 #include "gtklist.h"
36 #include "gtkentry.h"
37 #include "gtkeventbox.h"
38 #include "gtkbutton.h"
39 #include "gtklistitem.h"
40 #include "gtkscrolledwindow.h"
41 #include "gtkmain.h"
42 #include "gtksignal.h"
43 #include "gtkwindow.h"
44 #include "gdk/gdkkeysyms.h"
45 #include "gtkcombo.h"
46 #include "gtkframe.h"
47 #include "gtkintl.h"
48
49 const gchar *gtk_combo_string_key = "gtk-combo-string-value";
50
51 #define COMBO_LIST_MAX_HEIGHT   (400)
52 #define EMPTY_LIST_HEIGHT       (15)
53
54 enum {
55   PROP_0,
56   PROP_ENABLE_ARROW_KEYS,
57   PROP_ENABLE_ARROWS_ALWAYS,
58   PROP_CASE_SENSITIVE,
59   PROP_ALLOW_EMPTY,
60   PROP_VALUE_IN_LIST
61 };
62
63 static void         gtk_combo_class_init         (GtkComboClass    *klass);
64 static void         gtk_combo_init               (GtkCombo         *combo);
65 static void         gtk_combo_destroy            (GtkObject        *combo);
66 static GtkListItem *gtk_combo_find               (GtkCombo         *combo);
67 static gchar *      gtk_combo_func               (GtkListItem      *li);
68 static gint         gtk_combo_focus_idle         (GtkCombo         *combo);
69 static gint         gtk_combo_entry_focus_out    (GtkEntry         *entry,
70                                                   GdkEventFocus    *event,
71                                                   GtkCombo         *combo);
72 static void         gtk_combo_get_pos            (GtkCombo         *combo,
73                                                   gint             *x,
74                                                   gint             *y,
75                                                   gint             *height,
76                                                   gint             *width);
77 static void         gtk_combo_popup_list         (GtkCombo         *combo);
78 static void         gtk_combo_activate           (GtkWidget        *widget,
79                                                   GtkCombo         *combo);
80 static gboolean     gtk_combo_popup_button_press (GtkWidget        *button,
81                                                   GdkEventButton   *event,
82                                                   GtkCombo         *combo);
83 static gboolean     gtk_combo_popup_button_leave (GtkWidget        *button,
84                                                   GdkEventCrossing *event,
85                                                   GtkCombo         *combo);
86 static void         gtk_combo_update_entry       (GtkList          *list,
87                                                   GtkCombo         *combo);
88 static void         gtk_combo_update_list        (GtkEntry         *entry,
89                                                   GtkCombo         *combo);
90 static gint         gtk_combo_button_press       (GtkWidget        *widget,
91                                                   GdkEvent         *event,
92                                                   GtkCombo         *combo);
93 static void         gtk_combo_button_event_after (GtkWidget        *widget,
94                                                   GdkEvent         *event,
95                                                   GtkCombo         *combo);
96 static gint         gtk_combo_list_enter         (GtkWidget        *widget,
97                                                   GdkEventCrossing *event,
98                                                   GtkCombo         *combo);
99 static gint         gtk_combo_list_key_press     (GtkWidget        *widget,
100                                                   GdkEventKey      *event,
101                                                   GtkCombo         *combo);
102 static gint         gtk_combo_entry_key_press    (GtkEntry         *widget,
103                                                   GdkEventKey      *event,
104                                                   GtkCombo         *combo);
105 static gint         gtk_combo_window_key_press   (GtkWidget        *window,
106                                                   GdkEventKey      *event,
107                                                   GtkCombo         *combo);
108 static void         gtk_combo_item_destroy       (GtkObject        *object);
109 static void         gtk_combo_size_allocate      (GtkWidget        *widget,
110                                                   GtkAllocation   *allocation);
111 static void         gtk_combo_set_property       (GObject         *object,
112                                                   guint            prop_id,
113                                                   const GValue    *value,
114                                                   GParamSpec      *pspec);
115 static void         gtk_combo_get_property       (GObject         *object,
116                                                   guint            prop_id,
117                                                   GValue          *value,
118                                                   GParamSpec      *pspec);
119 static GtkHBoxClass *parent_class = NULL;
120
121 static void
122 gtk_combo_class_init (GtkComboClass * klass)
123 {
124   GObjectClass *gobject_class;
125   GtkObjectClass *oclass;
126   GtkWidgetClass *widget_class;
127
128   gobject_class = (GObjectClass *) klass;
129   parent_class = gtk_type_class (GTK_TYPE_HBOX);
130   oclass = (GtkObjectClass *) klass;
131   widget_class = (GtkWidgetClass *) klass;
132
133   gobject_class->set_property = gtk_combo_set_property; 
134   gobject_class->get_property = gtk_combo_get_property; 
135
136   g_object_class_install_property (gobject_class,
137                                    PROP_ENABLE_ARROW_KEYS,
138                                    g_param_spec_boolean ("enable_arrow_keys",
139                                                          _("Enable arrow keys"),
140                                                          _("Whether the arrow keys move through the list of items"),
141                                                          TRUE,
142                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
143   g_object_class_install_property (gobject_class,
144                                    PROP_ENABLE_ARROWS_ALWAYS,
145                                    g_param_spec_boolean ("enable_arrows_always",
146                                                          _("Always enable arrows"),
147                                                          _("Whether the arrow keys work, even if the entry contents are not in the list"),
148                                                          TRUE,
149                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
150   g_object_class_install_property (gobject_class,
151                                    PROP_CASE_SENSITIVE,
152                                    g_param_spec_boolean ("case_sensitive",
153                                                          _("Case sensitive"),
154                                                          _("Whether list item matching is case sensitive"),
155                                                          FALSE,
156                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
157
158   g_object_class_install_property (gobject_class,
159                                    PROP_ALLOW_EMPTY,
160                                    g_param_spec_boolean ("allow_empty",
161                                                          _("Allow empty"),
162                                                          _("Whether an empty value may be entered in this field"),
163                                                          TRUE,
164                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
165
166   g_object_class_install_property (gobject_class,
167                                    PROP_VALUE_IN_LIST,
168                                    g_param_spec_boolean ("value_in_list",
169                                                          _("Value in list"),
170                                                          _("Whether entered values must already be present in the list"),
171                                                          FALSE,
172                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
173   
174    
175   oclass->destroy = gtk_combo_destroy;
176   
177   widget_class->size_allocate = gtk_combo_size_allocate;
178 }
179
180 static void
181 gtk_combo_destroy (GtkObject *object)
182 {
183   GtkCombo *combo = GTK_COMBO (object);
184
185   if (combo->popwin)
186     {
187       gtk_widget_destroy (combo->popwin);
188       gtk_widget_unref (combo->popwin);
189       combo->popwin = NULL;
190     }
191
192   GTK_OBJECT_CLASS (parent_class)->destroy (object);
193 }
194
195 static int
196 gtk_combo_entry_key_press (GtkEntry * entry, GdkEventKey * event, GtkCombo * combo)
197 {
198   GList *li;
199
200   /* completion */
201   if ((event->keyval == GDK_Tab ||
202        event->keyval == GDK_KP_Tab) &&
203       (event->state & GDK_MOD1_MASK)) 
204     {
205       GtkEditable *editable = GTK_EDITABLE (entry);
206     GCompletion * cmpl;
207     gchar* prefix;
208     gchar* nprefix = NULL;
209     gint pos;
210
211     if ( !GTK_LIST (combo->list)->children )
212       return FALSE;
213     
214     gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
215
216     cmpl = g_completion_new ((GCompletionFunc)gtk_combo_func);
217     g_completion_add_items (cmpl, GTK_LIST (combo->list)->children);
218
219     pos = gtk_editable_get_position (editable);
220     prefix = gtk_editable_get_chars (editable, 0, pos);
221
222     g_completion_complete (cmpl, prefix, &nprefix);
223
224     if (nprefix && strlen (nprefix) > strlen (prefix)) 
225       {
226         gtk_editable_insert_text (editable, nprefix + pos, 
227                                   strlen (nprefix) - strlen (prefix), &pos);
228         gtk_editable_set_position (editable, pos);
229     }
230
231     if (nprefix)
232       g_free (nprefix);
233     g_free (prefix);
234     g_completion_free (cmpl);
235
236     return TRUE;
237   }
238
239   if (!combo->use_arrows || !GTK_LIST (combo->list)->children)
240     return FALSE;
241
242   li = g_list_find (GTK_LIST (combo->list)->children, gtk_combo_find (combo));
243
244   if ((event->keyval == GDK_Up)
245       || (event->keyval == GDK_KP_Up)
246       || ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'p') || (event->keyval == 'P'))))
247     {
248       if (li)
249         li = li->prev;
250       if (!li && combo->use_arrows_always)
251         {
252           li = g_list_last (GTK_LIST (combo->list)->children);
253         }
254       if (li)
255         {
256           gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
257           gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
258           return TRUE;
259         }
260     }
261   else if ((event->keyval == GDK_Down)
262            || (event->keyval == GDK_KP_Down)
263            || ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'n') || (event->keyval == 'N'))))
264     {
265       if (li)
266         li = li->next;
267       if (!li && combo->use_arrows_always)
268         {
269           li = GTK_LIST (combo->list)->children;
270         }
271       if (li)
272         {
273           gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
274           gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "key_press_event");
275           return TRUE;
276         }
277     }
278   return FALSE;
279 }
280
281 static int
282 gtk_combo_window_key_press (GtkWidget   *window,
283                             GdkEventKey *event,
284                             GtkCombo    *combo)
285 {
286   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)
287     {
288       if (GTK_WIDGET_VISIBLE (combo->popwin))
289         {
290           gtk_widget_hide (combo->popwin);
291           
292           if (GTK_WIDGET_HAS_GRAB (combo->popwin))
293             {
294               gtk_grab_remove (combo->popwin);
295               gdk_pointer_ungrab (event->time);
296             }
297         }
298
299       gtk_signal_emit_stop_by_name (GTK_OBJECT (window), "key_press_event");
300
301       return TRUE;
302     }
303
304   return FALSE;
305 }
306
307 static GtkListItem *
308 gtk_combo_find (GtkCombo * combo)
309 {
310   const gchar *text;
311   GtkListItem *found = NULL;
312   gchar *ltext;
313   gchar *compare_text;
314   GList *clist;
315
316   text = gtk_entry_get_text (GTK_ENTRY (combo->entry));
317   if (combo->case_sensitive)
318     compare_text = (gchar *)text;
319   else
320     compare_text = g_utf8_casefold (text, -1);
321   
322   clist = GTK_LIST (combo->list)->children;
323
324   while (!found && clist && clist->data)
325     {
326       ltext = gtk_combo_func (GTK_LIST_ITEM (clist->data));
327       if (!ltext)
328         continue;
329
330       if (!combo->case_sensitive)
331         ltext = g_utf8_casefold (ltext, -1);
332
333       if (strcmp (ltext, compare_text) == 0)
334         found = clist->data;
335
336       if (!combo->case_sensitive)
337         g_free (ltext);
338
339       clist = clist->next;
340     }
341
342   if (!combo->case_sensitive)
343     g_free (compare_text);
344
345   return found;
346 }
347
348 static gchar *
349 gtk_combo_func (GtkListItem * li)
350 {
351   GtkWidget *label;
352   gchar *ltext = NULL;
353
354   ltext = (gchar *) gtk_object_get_data (GTK_OBJECT (li), gtk_combo_string_key);
355   if (!ltext)
356     {
357       label = GTK_BIN (li)->child;
358       if (!label || !GTK_IS_LABEL (label))
359         return NULL;
360       gtk_label_get (GTK_LABEL (label), &ltext);
361     }
362   return ltext;
363 }
364
365 static gint
366 gtk_combo_focus_idle (GtkCombo * combo)
367 {
368   if (combo)
369     {
370       GDK_THREADS_ENTER ();
371       gtk_widget_grab_focus (combo->entry);
372       GDK_THREADS_LEAVE ();
373     }
374   return FALSE;
375 }
376
377 static gint
378 gtk_combo_entry_focus_out (GtkEntry * entry, GdkEventFocus * event, GtkCombo * combo)
379 {
380
381   if (combo->value_in_list && !gtk_combo_find (combo))
382     {
383       /* gdk_beep(); *//* this can be annoying */
384       if (combo->ok_if_empty && !strcmp (gtk_entry_get_text (entry), ""))
385         return FALSE;
386 #ifdef TEST
387       printf ("INVALID ENTRY: `%s'\n", gtk_entry_get_text (entry));
388 #endif
389       gtk_grab_add (GTK_WIDGET (combo));
390       /* this is needed because if we call gtk_widget_grab_focus() 
391          it isn't guaranteed it's the *last* call before the main-loop,
392          so the focus can be lost anyway...
393          the signal_emit_stop doesn't seem to work either...
394        */
395       gtk_idle_add ((GtkFunction) gtk_combo_focus_idle, combo);
396       /*gtk_signal_emit_stop_by_name (GTK_OBJECT (entry), "focus_out_event"); */
397       return TRUE;
398     }
399   return FALSE;
400 }
401
402 static void
403 gtk_combo_get_pos (GtkCombo * combo, gint * x, gint * y, gint * height, gint * width)
404 {
405   GtkBin *popwin;
406   GtkWidget *widget;
407   GtkScrolledWindow *popup;
408   
409   gint real_height;
410   GtkRequisition list_requisition;
411   gboolean show_hscroll = FALSE;
412   gboolean show_vscroll = FALSE;
413   gint avail_height;
414   gint min_height;
415   gint alloc_width;
416   gint work_height;
417   gint old_height;
418   gint old_width;
419   
420   widget = GTK_WIDGET (combo);
421   popup  = GTK_SCROLLED_WINDOW (combo->popup);
422   popwin = GTK_BIN (combo->popwin);
423   
424   gdk_window_get_origin (combo->entry->window, x, y);
425   real_height = MIN (combo->entry->requisition.height, 
426                      combo->entry->allocation.height);
427   *y += real_height;
428   avail_height = gdk_screen_height () - *y;
429   
430   gtk_widget_size_request (combo->list, &list_requisition);
431   min_height = MIN (list_requisition.height, 
432                     popup->vscrollbar->requisition.height);
433   if (!GTK_LIST (combo->list)->children)
434     list_requisition.height += EMPTY_LIST_HEIGHT;
435   
436   alloc_width = (widget->allocation.width -
437                  2 * popwin->child->style->xthickness -
438                  2 * GTK_CONTAINER (popwin->child)->border_width -
439                  2 * GTK_CONTAINER (combo->popup)->border_width -
440                  2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width - 
441                  2 * GTK_BIN (popup)->child->style->xthickness);
442   
443   work_height = (2 * popwin->child->style->ythickness +
444                  2 * GTK_CONTAINER (popwin->child)->border_width +
445                  2 * GTK_CONTAINER (combo->popup)->border_width +
446                  2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width +
447                  2 * GTK_BIN (popup)->child->style->xthickness);
448   
449   do 
450     {
451       old_width = alloc_width;
452       old_height = work_height;
453       
454       if (!show_hscroll &&
455           alloc_width < list_requisition.width)
456         {
457           work_height += (popup->hscrollbar->requisition.height +
458                           GTK_SCROLLED_WINDOW_GET_CLASS (combo->popup)->scrollbar_spacing);
459           show_hscroll = TRUE;
460         }
461       if (!show_vscroll && 
462           work_height + list_requisition.height > avail_height)
463         {
464           if (work_height + min_height > avail_height && 
465               *y - real_height > avail_height)
466             {
467               *y -= (work_height + list_requisition.height + real_height);
468               break;
469             }
470           alloc_width -= (popup->vscrollbar->requisition.width +
471                           GTK_SCROLLED_WINDOW_GET_CLASS (combo->popup)->scrollbar_spacing);
472           show_vscroll = TRUE;
473         }
474     } while (old_width != alloc_width || old_height != work_height);
475   
476   *width = widget->allocation.width;
477   if (show_vscroll)
478     *height = avail_height;
479   else
480     *height = work_height + list_requisition.height;
481   
482   if (*x < 0)
483     *x = 0;
484 }
485
486 static void
487 gtk_combo_popup_list (GtkCombo * combo)
488 {
489   gint height, width, x, y;
490   gint old_width, old_height;
491
492   old_width = combo->popwin->allocation.width;
493   old_height  = combo->popwin->allocation.height;
494
495   gtk_combo_get_pos (combo, &x, &y, &height, &width);
496
497   /* workaround for gtk_scrolled_window_size_allocate bug */
498   if (old_width != width || old_height != height)
499     {
500       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar);
501       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar);
502     }
503
504   gtk_window_move (GTK_WINDOW (combo->popwin), x, y);
505   gtk_widget_set_usize (combo->popwin, width, height);
506   gtk_widget_show (combo->popwin);
507
508   gtk_widget_grab_focus (combo->popwin);
509 }
510
511 static void
512 gtk_combo_popdown_list (GtkCombo *combo)
513 {
514   combo->current_button = 0;
515       
516   if (GTK_BUTTON (combo->button)->in_button)
517     {
518       GTK_BUTTON (combo->button)->in_button = FALSE;
519       gtk_button_released (GTK_BUTTON (combo->button));
520     }
521
522   if (GTK_WIDGET_HAS_GRAB (combo->popwin))
523     {
524       gtk_grab_remove (combo->popwin);
525       gdk_pointer_ungrab (GDK_CURRENT_TIME);
526     }
527   
528   gtk_widget_hide (combo->popwin);
529 }
530
531 static void        
532 gtk_combo_activate (GtkWidget        *widget,
533                     GtkCombo         *combo)
534 {
535   gtk_combo_popup_list (combo);
536
537   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
538     gtk_widget_grab_focus (combo->entry);
539
540   gtk_grab_add (combo->popwin);
541   gdk_pointer_grab (combo->popwin->window, TRUE,
542                     GDK_BUTTON_PRESS_MASK | 
543                     GDK_BUTTON_RELEASE_MASK |
544                     GDK_POINTER_MOTION_MASK, 
545                     NULL, NULL, GDK_CURRENT_TIME);
546 }
547
548 static gboolean
549 gtk_combo_popup_button_press (GtkWidget        *button,
550                               GdkEventButton   *event,
551                               GtkCombo         *combo)
552 {
553   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
554     gtk_widget_grab_focus (combo->entry);
555
556   if (event->button != 1)
557     return FALSE;
558
559   combo->current_button = event->button;
560
561   gtk_combo_popup_list (combo);
562   gtk_button_pressed (GTK_BUTTON (button));
563
564   gtk_grab_add (combo->popwin);
565   gdk_pointer_grab (combo->popwin->window, TRUE,
566                     GDK_BUTTON_PRESS_MASK | 
567                     GDK_BUTTON_RELEASE_MASK |
568                     GDK_POINTER_MOTION_MASK, 
569                     NULL, NULL, GDK_CURRENT_TIME);
570
571   GTK_LIST (combo->list)->drag_selection = TRUE;
572   gtk_grab_add (combo->list);
573
574   return TRUE;
575 }
576
577 static gboolean
578 gtk_combo_popup_button_leave (GtkWidget        *button,
579                               GdkEventCrossing *event,
580                               GtkCombo         *combo)
581 {
582   /* The idea here is that we want to keep the button down if the
583    * popup is popped up.
584    */
585   return combo->current_button != 0;
586 }
587
588 static void
589 gtk_combo_update_entry (GtkList * list, GtkCombo * combo)
590 {
591   char *text;
592
593   gtk_grab_remove (GTK_WIDGET (combo));
594   gtk_signal_handler_block (GTK_OBJECT (list), combo->list_change_id);
595   if (list->selection)
596     {
597       text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
598       if (!text)
599         text = "";
600       gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
601     }
602   gtk_signal_handler_unblock (GTK_OBJECT (list), combo->list_change_id);
603 }
604
605 static void
606 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
607 {
608   GtkList *list = GTK_LIST (combo->list);
609   GList *slist = list->selection;
610   GtkListItem *li;
611
612   gtk_grab_remove (GTK_WIDGET (combo));
613
614   gtk_signal_handler_block (GTK_OBJECT (entry), combo->entry_change_id);
615   if (slist && slist->data)
616     gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
617   li = gtk_combo_find (combo);
618   if (li)
619     gtk_list_select_child (list, GTK_WIDGET (li));
620   gtk_signal_handler_unblock (GTK_OBJECT (entry), combo->entry_change_id);
621 }
622
623 static gint
624 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
625 {
626   GtkWidget *child;
627
628   child = gtk_get_event_widget (event);
629
630   /* We don't ask for button press events on the grab widget, so
631    *  if an event is reported directly to the grab widget, it must
632    *  be on a window outside the application (and thus we remove
633    *  the popup window). Otherwise, we check if the widget is a child
634    *  of the grab widget, and only remove the popup window if it
635    *  is not.
636    */
637   if (child != widget)
638     {
639       while (child)
640         {
641           if (child == widget)
642             return FALSE;
643           child = child->parent;
644         }
645     }
646
647   gtk_combo_popdown_list (combo);
648
649   return TRUE;
650 }
651
652 static void
653 gtk_combo_button_event_after (GtkWidget *widget,
654                               GdkEvent  *event,
655                               GtkCombo  *combo)
656 {
657   GtkWidget *child;
658
659   if (event->type != GDK_BUTTON_RELEASE)
660     return;
661   
662   if ((combo->current_button != 0) && (event->button.button == 1))
663     {
664       /* This was the initial button press */
665
666       combo->current_button = 0;
667
668       /* Check to see if we released inside the button */
669       child = gtk_get_event_widget ((GdkEvent*) event);
670
671       while (child && child != (combo->button))
672         child = child->parent;
673
674       if (child == combo->button)
675         {
676           gtk_grab_add (combo->popwin);
677           gdk_pointer_grab (combo->popwin->window, TRUE,
678                             GDK_BUTTON_PRESS_MASK | 
679                             GDK_BUTTON_RELEASE_MASK |
680                             GDK_POINTER_MOTION_MASK, 
681                             NULL, NULL, GDK_CURRENT_TIME);
682           return;
683         }
684     }
685
686   gtk_combo_popdown_list (combo);
687 }
688
689 static gint         
690 gtk_combo_list_enter (GtkWidget        *widget,
691                       GdkEventCrossing *event,
692                       GtkCombo         *combo)
693 {
694   GtkWidget *event_widget;
695
696   event_widget = gtk_get_event_widget ((GdkEvent*) event);
697   
698   if ((event_widget == combo->list) &&
699       (combo->current_button != 0) && 
700       (!GTK_WIDGET_HAS_GRAB (combo->list)))
701     {
702       GdkEvent tmp_event;
703       gint x, y;
704       GdkModifierType mask;
705
706       gtk_grab_remove (combo->popwin);
707
708       /* Transfer the grab over to the list by synthesizing
709        * a button press event
710        */
711       gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
712
713       tmp_event.button.type = GDK_BUTTON_PRESS;
714       tmp_event.button.window = combo->list->window;
715       tmp_event.button.send_event = TRUE;
716       tmp_event.button.time = GDK_CURRENT_TIME; /* bad */
717       tmp_event.button.x = x;
718       tmp_event.button.y = y;
719       /* We leave all the XInput fields unfilled here, in the expectation
720        * that GtkList doesn't care.
721        */
722       tmp_event.button.button = combo->current_button;
723       tmp_event.button.state = mask;
724
725       gtk_widget_event (combo->list, &tmp_event);
726     }
727
728   return FALSE;
729 }
730
731 static int
732 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
733 {
734   if (event->keyval == GDK_Escape)
735     {
736       if (GTK_WIDGET_HAS_GRAB (combo->list))
737         gtk_list_end_drag_selection (GTK_LIST (combo->list));
738
739       gtk_combo_popdown_list (combo);
740       
741       return TRUE;
742     }
743   return FALSE;
744 }
745
746 static void
747 gtk_combo_init (GtkCombo * combo)
748 {
749   GtkWidget *arrow;
750   GtkWidget *frame;
751   GtkWidget *event_box;
752   GdkCursor *cursor;
753
754   combo->case_sensitive = FALSE;
755   combo->value_in_list = FALSE;
756   combo->ok_if_empty = TRUE;
757   combo->use_arrows = TRUE;
758   combo->use_arrows_always = FALSE;
759   combo->entry = gtk_entry_new ();
760   combo->button = gtk_button_new ();
761   combo->current_button = 0;
762   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
763   gtk_widget_show (arrow);
764   gtk_container_add (GTK_CONTAINER (combo->button), arrow);
765   gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
766   gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
767   GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
768   gtk_widget_show (combo->entry);
769   gtk_widget_show (combo->button);
770   combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed",
771                               (GtkSignalFunc) gtk_combo_update_list, combo);
772   gtk_signal_connect (GTK_OBJECT (combo->entry), "key_press_event",
773                       (GtkSignalFunc) gtk_combo_entry_key_press, combo);
774   gtk_signal_connect_after (GTK_OBJECT (combo->entry), "focus_out_event",
775                             (GtkSignalFunc) gtk_combo_entry_focus_out, combo);
776   combo->activate_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "activate",
777                       (GtkSignalFunc) gtk_combo_activate, combo);
778   gtk_signal_connect (GTK_OBJECT (combo->button), "button_press_event",
779                       (GtkSignalFunc) gtk_combo_popup_button_press, combo);
780   gtk_signal_connect (GTK_OBJECT (combo->button), "leave_notify_event",
781                       (GtkSignalFunc) gtk_combo_popup_button_leave, combo);
782
783   combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
784   gtk_widget_ref (combo->popwin);
785   gtk_window_set_resizable (GTK_WINDOW (combo->popwin), FALSE);
786
787   gtk_signal_connect (GTK_OBJECT (combo->popwin), "key_press_event",
788                       GTK_SIGNAL_FUNC (gtk_combo_window_key_press), combo);
789   
790   gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
791
792   event_box = gtk_event_box_new ();
793   gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
794   gtk_widget_show (event_box);
795
796   gtk_widget_realize (event_box);
797   cursor = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
798   gdk_window_set_cursor (event_box->window, cursor);
799   gdk_cursor_destroy (cursor);
800
801   frame = gtk_frame_new (NULL);
802   gtk_container_add (GTK_CONTAINER (event_box), frame);
803   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
804   gtk_widget_show (frame);
805
806   combo->popup = gtk_scrolled_window_new (NULL, NULL);
807   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
808                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
809   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
810   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
811   gtk_container_add (GTK_CONTAINER (frame), combo->popup);
812   gtk_widget_show (combo->popup);
813
814   combo->list = gtk_list_new ();
815   /* We'll use enter notify events to figure out when to transfer
816    * the grab to the list
817    */
818   gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
819
820   gtk_list_set_selection_mode (GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
821   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (combo->popup), combo->list);
822   gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
823                                        gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
824   gtk_container_set_focus_hadjustment (GTK_CONTAINER (combo->list),
825                                        gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
826   gtk_widget_show (combo->list);
827
828   combo->list_change_id = gtk_signal_connect (GTK_OBJECT (combo->list), "selection_changed",
829                              (GtkSignalFunc) gtk_combo_update_entry, combo);
830   gtk_signal_connect (GTK_OBJECT (combo->popwin), "key_press_event",
831                       (GtkSignalFunc) gtk_combo_list_key_press, combo);
832   gtk_signal_connect (GTK_OBJECT (combo->popwin), "button_press_event",
833                       GTK_SIGNAL_FUNC (gtk_combo_button_press), combo);
834
835   gtk_signal_connect (GTK_OBJECT (combo->list), "event_after",
836                       (GtkSignalFunc) gtk_combo_button_event_after, combo);
837   /* We connect here on the button, because we'll have a grab on it
838    * when the event occurs. But we are actually interested in enters
839    * for the combo->list.
840    */
841   gtk_signal_connect (GTK_OBJECT (combo->button), "enter_notify_event",
842                       GTK_SIGNAL_FUNC (gtk_combo_list_enter), combo);
843 }
844
845 GtkType
846 gtk_combo_get_type (void)
847 {
848   static GtkType combo_type = 0;
849
850   if (!combo_type)
851     {
852       static const GtkTypeInfo combo_info =
853       {
854         "GtkCombo",
855         sizeof (GtkCombo),
856         sizeof (GtkComboClass),
857         (GtkClassInitFunc) gtk_combo_class_init,
858         (GtkObjectInitFunc) gtk_combo_init,
859         /* reserved_1 */ NULL,
860         /* reserved_2 */ NULL,
861         (GtkClassInitFunc) NULL,
862       };
863       combo_type = gtk_type_unique (GTK_TYPE_HBOX, &combo_info);
864     }
865   return combo_type;
866 }
867
868 GtkWidget*
869 gtk_combo_new (void)
870 {
871   return GTK_WIDGET (gtk_type_new (GTK_TYPE_COMBO));
872 }
873
874 void
875 gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
876 {
877   g_return_if_fail (GTK_IS_COMBO (combo));
878   val = val != FALSE;
879   ok_if_empty = ok_if_empty != FALSE;
880
881   g_object_freeze_notify (G_OBJECT (combo));
882   if (combo->value_in_list != val)
883     {
884        combo->value_in_list = val;
885   g_object_notify (G_OBJECT (combo), "value_in_list");
886     }
887   if (combo->ok_if_empty != ok_if_empty)
888     {
889        combo->ok_if_empty = ok_if_empty;
890   g_object_notify (G_OBJECT (combo), "allow_empty");
891     }
892   g_object_thaw_notify (G_OBJECT (combo));
893 }
894
895 void
896 gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
897 {
898   g_return_if_fail (GTK_IS_COMBO (combo));
899   val = val != FALSE;
900
901   if (combo->case_sensitive != val) 
902     {
903   combo->case_sensitive = val;
904   g_object_notify (G_OBJECT (combo), "case_sensitive");
905     }
906 }
907
908 void
909 gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
910 {
911   g_return_if_fail (GTK_IS_COMBO (combo));
912   val = val != FALSE;
913
914   if (combo->use_arrows != val) 
915     {
916   combo->use_arrows = val;
917   g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
918     }
919 }
920
921 void
922 gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
923 {
924   g_return_if_fail (GTK_IS_COMBO (combo));
925   val = val != FALSE;
926
927   if (combo->use_arrows_always != val) 
928     {
929        g_object_freeze_notify (G_OBJECT (combo));
930   combo->use_arrows_always = val;
931        g_object_notify (G_OBJECT (combo), "enable_arrows_always");
932
933        if (combo->use_arrows != TRUE) 
934          {
935   combo->use_arrows = TRUE;
936   g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
937          }
938   g_object_thaw_notify (G_OBJECT (combo));
939     }
940 }
941
942 void
943 gtk_combo_set_popdown_strings (GtkCombo * combo, GList * strings)
944 {
945   GList *list;
946   GtkWidget *li;
947
948   g_return_if_fail (GTK_IS_COMBO (combo));
949   g_return_if_fail (strings != NULL);
950
951   gtk_combo_popdown_list (combo);
952
953   gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
954   list = strings;
955   while (list)
956     {
957       li = gtk_list_item_new_with_label ((gchar *) list->data);
958       gtk_widget_show (li);
959       gtk_container_add (GTK_CONTAINER (combo->list), li);
960       list = list->next;
961     }
962 }
963
964 static void
965 gtk_combo_item_destroy (GtkObject * object)
966 {
967   gchar *key;
968
969   key = gtk_object_get_data (object, gtk_combo_string_key);
970   if (key)
971     {
972       gtk_object_remove_data (object, gtk_combo_string_key);
973       g_free (key);
974     }
975 }
976
977 void
978 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
979 {
980   gchar *val;
981   gint connected = 0;
982
983   g_return_if_fail (GTK_IS_COMBO (combo));
984   g_return_if_fail (item != NULL);
985
986   val = gtk_object_get_data (GTK_OBJECT (item), gtk_combo_string_key);
987   if (val) 
988     {
989       g_free (val);
990       connected = 1;
991     }
992   if (item_value)
993     {
994       val = g_strdup (item_value);
995       gtk_object_set_data (GTK_OBJECT (item), gtk_combo_string_key, val);
996       if (!connected)
997         gtk_signal_connect (GTK_OBJECT (item), "destroy",
998                           (GtkSignalFunc) gtk_combo_item_destroy, val);
999     }
1000   else 
1001     {
1002       gtk_object_set_data (GTK_OBJECT (item), gtk_combo_string_key, NULL);
1003       if (connected)
1004         gtk_signal_disconnect_by_data(GTK_OBJECT (item), val);
1005     }
1006 }
1007
1008 static void
1009 gtk_combo_size_allocate (GtkWidget     *widget,
1010                          GtkAllocation *allocation)
1011 {
1012   GtkCombo *combo;
1013
1014   g_return_if_fail (GTK_IS_COMBO (widget));
1015   g_return_if_fail (allocation != NULL);
1016
1017   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
1018   
1019   combo = GTK_COMBO (widget);
1020
1021   if (combo->entry->allocation.height > combo->entry->requisition.height)
1022     {
1023       GtkAllocation button_allocation;
1024
1025       button_allocation = combo->button->allocation;
1026       button_allocation.height = combo->entry->requisition.height;
1027       button_allocation.y = combo->entry->allocation.y + 
1028         (combo->entry->allocation.height - combo->entry->requisition.height) 
1029         / 2;
1030       gtk_widget_size_allocate (combo->button, &button_allocation);
1031     }
1032 }
1033
1034 void
1035 gtk_combo_disable_activate (GtkCombo* combo)
1036 {
1037   g_return_if_fail (GTK_IS_COMBO (combo));
1038
1039   if ( combo->activate_id ) {
1040     gtk_signal_disconnect (GTK_OBJECT(combo->entry), combo->activate_id);
1041     combo->activate_id = 0;
1042   }
1043 }
1044
1045 static void
1046 gtk_combo_set_property (GObject         *object,
1047                         guint            prop_id,
1048                         const GValue    *value,
1049                         GParamSpec      *pspec)
1050 {
1051   GtkCombo *combo = GTK_COMBO (object);
1052   
1053   switch (prop_id)
1054     {
1055     case PROP_ENABLE_ARROW_KEYS:
1056       gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
1057       break;
1058     case PROP_ENABLE_ARROWS_ALWAYS:
1059       gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
1060       break;
1061     case PROP_CASE_SENSITIVE:
1062       gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
1063       break;
1064     case PROP_ALLOW_EMPTY:
1065       combo->ok_if_empty = g_value_get_boolean (value);
1066       break;
1067     case PROP_VALUE_IN_LIST:
1068       combo->value_in_list = g_value_get_boolean (value);
1069       break;
1070     default:
1071       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1072       break;
1073     }
1074   
1075 }
1076
1077 static void
1078 gtk_combo_get_property (GObject         *object,
1079                         guint            prop_id,
1080                         GValue          *value,
1081                         GParamSpec      *pspec)
1082 {
1083   GtkCombo *combo = GTK_COMBO (object);
1084   
1085   switch (prop_id)
1086     {
1087     case PROP_ENABLE_ARROW_KEYS:
1088       g_value_set_boolean (value, combo->use_arrows);
1089       break;
1090     case PROP_ENABLE_ARROWS_ALWAYS:
1091       g_value_set_boolean (value, combo->use_arrows_always);
1092       break;
1093     case PROP_CASE_SENSITIVE:
1094       g_value_set_boolean (value, combo->case_sensitive);
1095       break;
1096     case PROP_ALLOW_EMPTY:
1097       g_value_set_boolean (value, combo->ok_if_empty);
1098       break;
1099     case PROP_VALUE_IN_LIST:
1100       g_value_set_boolean (value, combo->value_in_list);
1101       break;
1102     default:
1103       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1104       break;
1105     }
1106    
1107 }