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