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