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