]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombo.c
ba368797cde47d8f3c753a794ed7113157002a17
[~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   if (list->selection)
675     {
676       text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
677       if (!text)
678         text = "";
679       gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
680     }
681 }
682
683 static void
684 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
685 {
686   GtkList *list = GTK_LIST (combo->list);
687   GList *slist = list->selection;
688   GtkListItem *li;
689
690   gtk_grab_remove (GTK_WIDGET (combo));
691
692   g_signal_handler_block (entry, combo->entry_change_id);
693   if (slist && slist->data)
694     gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
695   li = gtk_combo_find (combo);
696   if (li)
697     gtk_list_select_child (list, GTK_WIDGET (li));
698   g_signal_handler_unblock (entry, combo->entry_change_id);
699 }
700
701 static gint
702 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
703 {
704   GtkWidget *child;
705
706   child = gtk_get_event_widget (event);
707
708   /* We don't ask for button press events on the grab widget, so
709    *  if an event is reported directly to the grab widget, it must
710    *  be on a window outside the application (and thus we remove
711    *  the popup window). Otherwise, we check if the widget is a child
712    *  of the grab widget, and only remove the popup window if it
713    *  is not.
714    */
715   if (child != widget)
716     {
717       while (child)
718         {
719           if (child == widget)
720             return FALSE;
721           child = child->parent;
722         }
723     }
724
725   gtk_combo_popdown_list (combo);
726
727   return TRUE;
728 }
729
730 static gboolean
731 is_within (GtkWidget *widget,
732            GtkWidget *ancestor)
733 {
734   return widget == ancestor || gtk_widget_is_ancestor (widget, ancestor);
735 }
736
737 static void
738 gtk_combo_button_event_after (GtkWidget *widget,
739                               GdkEvent  *event,
740                               GtkCombo  *combo)
741 {
742   GtkWidget *child;
743
744   if (event->type != GDK_BUTTON_RELEASE)
745     return;
746   
747   child = gtk_get_event_widget ((GdkEvent*) event);
748
749   if ((combo->current_button != 0) && (event->button.button == 1))
750     {
751       /* This was the initial button press */
752
753       combo->current_button = 0;
754
755       /* Check to see if we released inside the button */
756       if (child && is_within (child, combo->button))
757         {
758           gtk_grab_add (combo->popwin);
759           gdk_pointer_grab (combo->popwin->window, TRUE,
760                             GDK_BUTTON_PRESS_MASK | 
761                             GDK_BUTTON_RELEASE_MASK |
762                             GDK_POINTER_MOTION_MASK, 
763                             NULL, NULL, GDK_CURRENT_TIME);
764           return;
765         }
766     }
767
768   if (is_within (child, combo->list))
769     gtk_combo_update_entry (combo);
770     
771   gtk_combo_popdown_list (combo);
772
773 }
774
775 static void
776 find_child_foreach (GtkWidget *widget,
777                     gpointer   data)
778 {
779   GdkEventButton *event = data;
780
781   if (!event->window)
782     {
783       if (event->x >= widget->allocation.x &&
784           event->x < widget->allocation.x + widget->allocation.width &&
785           event->y >= widget->allocation.y &&
786           event->y < widget->allocation.y + widget->allocation.height)
787         event->window = g_object_ref (widget->window);
788     }
789 }
790
791 static void
792 find_child_window (GtkContainer   *container,
793                    GdkEventButton *event)
794 {
795   gtk_container_foreach (container, find_child_foreach, event);
796 }
797
798 static gint         
799 gtk_combo_list_enter (GtkWidget        *widget,
800                       GdkEventCrossing *event,
801                       GtkCombo         *combo)
802 {
803   GtkWidget *event_widget;
804
805   event_widget = gtk_get_event_widget ((GdkEvent*) event);
806
807   if ((event_widget == combo->list) &&
808       (combo->current_button != 0) && 
809       (!GTK_WIDGET_HAS_GRAB (combo->list)))
810     {
811       GdkEvent *tmp_event = gdk_event_new (GDK_BUTTON_PRESS);
812       gint x, y;
813       GdkModifierType mask;
814
815       gtk_grab_remove (combo->popwin);
816
817       /* Transfer the grab over to the list by synthesizing
818        * a button press event
819        */
820       gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
821
822       tmp_event->button.send_event = TRUE;
823       tmp_event->button.time = GDK_CURRENT_TIME; /* bad */
824       tmp_event->button.x = x;
825       tmp_event->button.y = y;
826       /* We leave all the XInput fields unfilled here, in the expectation
827        * that GtkList doesn't care.
828        */
829       tmp_event->button.button = combo->current_button;
830       tmp_event->button.state = mask;
831
832       find_child_window (GTK_CONTAINER (combo->list), &tmp_event->button);
833       if (!tmp_event->button.window)
834         {
835           GtkWidget *child;
836           
837           if (GTK_LIST (combo->list)->children)
838             child = GTK_LIST (combo->list)->children->data;
839           else
840             child = combo->list;
841
842           tmp_event->button.window = g_object_ref (child->window);
843         }
844
845       gtk_widget_event (combo->list, tmp_event);
846       gdk_event_free (tmp_event);
847     }
848
849   return FALSE;
850 }
851
852 static int
853 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
854 {
855   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
856
857   if (event->keyval == GDK_Escape && state == 0)
858     {
859       if (GTK_WIDGET_HAS_GRAB (combo->list))
860         gtk_list_end_drag_selection (GTK_LIST (combo->list));
861
862       gtk_combo_popdown_list (combo);
863       
864       return TRUE;
865     }
866   return FALSE;
867 }
868
869 static void
870 combo_event_box_realize (GtkWidget *widget)
871 {
872   GdkCursor *cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
873                                                   GDK_TOP_LEFT_ARROW);
874   gdk_window_set_cursor (widget->window, cursor);
875   gdk_cursor_unref (cursor);
876 }
877
878 static void
879 gtk_combo_init (GtkCombo * combo)
880 {
881   GtkWidget *arrow;
882   GtkWidget *frame;
883   GtkWidget *event_box;
884
885   combo->case_sensitive = FALSE;
886   combo->value_in_list = FALSE;
887   combo->ok_if_empty = TRUE;
888   combo->use_arrows = TRUE;
889   combo->use_arrows_always = FALSE;
890   combo->entry = gtk_entry_new ();
891   combo->button = gtk_button_new ();
892   combo->current_button = 0;
893   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
894   gtk_widget_show (arrow);
895   gtk_container_add (GTK_CONTAINER (combo->button), arrow);
896   gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
897   gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
898   GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
899   gtk_widget_show (combo->entry);
900   gtk_widget_show (combo->button);
901   combo->entry_change_id = g_signal_connect (combo->entry, "changed",
902                                              G_CALLBACK (gtk_combo_update_list),
903                                              combo);
904   g_signal_connect (combo->entry, "key_press_event",
905                     G_CALLBACK (gtk_combo_entry_key_press), combo);
906   g_signal_connect_after (combo->entry, "focus_out_event",
907                           G_CALLBACK (gtk_combo_entry_focus_out), combo);
908   combo->activate_id = g_signal_connect (combo->entry, "activate",
909                                          G_CALLBACK (gtk_combo_activate),
910                                          combo);
911   g_signal_connect (combo->button, "button_press_event",
912                     G_CALLBACK (gtk_combo_popup_button_press), combo);
913   g_signal_connect (combo->button, "leave_notify_event",
914                     G_CALLBACK (gtk_combo_popup_button_leave), combo);
915
916   combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
917   g_object_ref (combo->popwin);
918   gtk_window_set_resizable (GTK_WINDOW (combo->popwin), FALSE);
919
920   g_signal_connect (combo->popwin, "key_press_event",
921                     G_CALLBACK (gtk_combo_window_key_press), combo);
922   
923   gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
924
925   event_box = gtk_event_box_new ();
926   gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
927   g_signal_connect (event_box, "realize",
928                     G_CALLBACK (combo_event_box_realize), NULL);
929   gtk_widget_show (event_box);
930
931
932   frame = gtk_frame_new (NULL);
933   gtk_container_add (GTK_CONTAINER (event_box), frame);
934   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
935   gtk_widget_show (frame);
936
937   combo->popup = gtk_scrolled_window_new (NULL, NULL);
938   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
939                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
940   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
941   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
942   gtk_container_add (GTK_CONTAINER (frame), combo->popup);
943   gtk_widget_show (combo->popup);
944
945   combo->list = gtk_list_new ();
946   /* We'll use enter notify events to figure out when to transfer
947    * the grab to the list
948    */
949   gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
950
951   gtk_list_set_selection_mode (GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
952   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (combo->popup), combo->list);
953   gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
954                                        gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
955   gtk_container_set_focus_hadjustment (GTK_CONTAINER (combo->list),
956                                        gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
957   gtk_widget_show (combo->list);
958
959   g_signal_connect (combo->popwin, "key_press_event",
960                     G_CALLBACK (gtk_combo_list_key_press), combo);
961   g_signal_connect (combo->popwin, "button_press_event",
962                     G_CALLBACK (gtk_combo_button_press), combo);
963
964   g_signal_connect (combo->popwin, "event_after",
965                     G_CALLBACK (gtk_combo_button_event_after), combo);
966   g_signal_connect (combo->list, "event_after",
967                     G_CALLBACK (gtk_combo_button_event_after), combo);
968
969   g_signal_connect (combo->list, "enter_notify_event",
970                     G_CALLBACK (gtk_combo_list_enter), combo);
971 }
972
973 static void
974 gtk_combo_realize (GtkWidget *widget)
975 {
976   GtkCombo *combo = GTK_COMBO (widget);
977
978   gtk_window_set_screen (GTK_WINDOW (combo->popwin), 
979                          gtk_widget_get_screen (widget));
980   
981   GTK_WIDGET_CLASS( parent_class )->realize (widget);  
982 }
983
984 static void        
985 gtk_combo_unrealize (GtkWidget *widget)
986 {
987   GtkCombo *combo = GTK_COMBO (widget);
988
989   gtk_combo_popdown_list (combo);
990   gtk_widget_unrealize (combo->popwin);
991   
992   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
993 }
994
995 GType
996 gtk_combo_get_type (void)
997 {
998   static GType combo_type = 0;
999
1000   if (!combo_type)
1001     {
1002       static const GTypeInfo combo_info =
1003       {
1004         sizeof (GtkComboClass),
1005         NULL,           /* base_init */
1006         NULL,           /* base_finalize */
1007         (GClassInitFunc) gtk_combo_class_init,
1008         NULL,           /* class_finalize */
1009         NULL,           /* class_data */
1010         sizeof (GtkCombo),
1011         0,              /* n_preallocs */
1012         (GInstanceInitFunc) gtk_combo_init,
1013       };
1014
1015       combo_type = g_type_register_static (GTK_TYPE_HBOX, "GtkCombo",
1016                                            &combo_info, 0);
1017     }
1018
1019   return combo_type;
1020 }
1021
1022 GtkWidget*
1023 gtk_combo_new (void)
1024 {
1025   return g_object_new (GTK_TYPE_COMBO, NULL);
1026 }
1027
1028 void
1029 gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
1030 {
1031   g_return_if_fail (GTK_IS_COMBO (combo));
1032   val = val != FALSE;
1033   ok_if_empty = ok_if_empty != FALSE;
1034
1035   g_object_freeze_notify (G_OBJECT (combo));
1036   if (combo->value_in_list != val)
1037     {
1038        combo->value_in_list = val;
1039   g_object_notify (G_OBJECT (combo), "value_in_list");
1040     }
1041   if (combo->ok_if_empty != ok_if_empty)
1042     {
1043        combo->ok_if_empty = ok_if_empty;
1044   g_object_notify (G_OBJECT (combo), "allow_empty");
1045     }
1046   g_object_thaw_notify (G_OBJECT (combo));
1047 }
1048
1049 void
1050 gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
1051 {
1052   g_return_if_fail (GTK_IS_COMBO (combo));
1053   val = val != FALSE;
1054
1055   if (combo->case_sensitive != val) 
1056     {
1057   combo->case_sensitive = val;
1058   g_object_notify (G_OBJECT (combo), "case_sensitive");
1059     }
1060 }
1061
1062 void
1063 gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
1064 {
1065   g_return_if_fail (GTK_IS_COMBO (combo));
1066   val = val != FALSE;
1067
1068   if (combo->use_arrows != val) 
1069     {
1070   combo->use_arrows = val;
1071   g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1072     }
1073 }
1074
1075 void
1076 gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
1077 {
1078   g_return_if_fail (GTK_IS_COMBO (combo));
1079   val = val != FALSE;
1080
1081   if (combo->use_arrows_always != val) 
1082     {
1083        g_object_freeze_notify (G_OBJECT (combo));
1084   combo->use_arrows_always = val;
1085        g_object_notify (G_OBJECT (combo), "enable_arrows_always");
1086
1087        if (combo->use_arrows != TRUE) 
1088          {
1089   combo->use_arrows = TRUE;
1090   g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1091          }
1092   g_object_thaw_notify (G_OBJECT (combo));
1093     }
1094 }
1095
1096 void
1097 gtk_combo_set_popdown_strings (GtkCombo * combo, GList * strings)
1098 {
1099   GList *list;
1100   GtkWidget *li;
1101
1102   g_return_if_fail (GTK_IS_COMBO (combo));
1103   g_return_if_fail (strings != NULL);
1104
1105   gtk_combo_popdown_list (combo);
1106
1107   gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
1108   list = strings;
1109   while (list)
1110     {
1111       li = gtk_list_item_new_with_label ((gchar *) list->data);
1112       gtk_widget_show (li);
1113       gtk_container_add (GTK_CONTAINER (combo->list), li);
1114       list = list->next;
1115     }
1116 }
1117
1118 void
1119 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
1120 {
1121   g_return_if_fail (GTK_IS_COMBO (combo));
1122   g_return_if_fail (item != NULL);
1123
1124   g_object_set_data_full (G_OBJECT (item), gtk_combo_string_key,
1125                           g_strdup (item_value), g_free);
1126 }
1127
1128 static void
1129 gtk_combo_size_allocate (GtkWidget     *widget,
1130                          GtkAllocation *allocation)
1131 {
1132   GtkCombo *combo;
1133
1134   g_return_if_fail (GTK_IS_COMBO (widget));
1135   g_return_if_fail (allocation != NULL);
1136
1137   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
1138   
1139   combo = GTK_COMBO (widget);
1140
1141   if (combo->entry->allocation.height > combo->entry->requisition.height)
1142     {
1143       GtkAllocation button_allocation;
1144
1145       button_allocation = combo->button->allocation;
1146       button_allocation.height = combo->entry->requisition.height;
1147       button_allocation.y = combo->entry->allocation.y + 
1148         (combo->entry->allocation.height - combo->entry->requisition.height) 
1149         / 2;
1150       gtk_widget_size_allocate (combo->button, &button_allocation);
1151     }
1152 }
1153
1154 void
1155 gtk_combo_disable_activate (GtkCombo* combo)
1156 {
1157   g_return_if_fail (GTK_IS_COMBO (combo));
1158
1159   if ( combo->activate_id ) {
1160     g_signal_handler_disconnect (combo->entry, combo->activate_id);
1161     combo->activate_id = 0;
1162   }
1163 }
1164
1165 static void
1166 gtk_combo_set_property (GObject         *object,
1167                         guint            prop_id,
1168                         const GValue    *value,
1169                         GParamSpec      *pspec)
1170 {
1171   GtkCombo *combo = GTK_COMBO (object);
1172   
1173   switch (prop_id)
1174     {
1175     case PROP_ENABLE_ARROW_KEYS:
1176       gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
1177       break;
1178     case PROP_ENABLE_ARROWS_ALWAYS:
1179       gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
1180       break;
1181     case PROP_CASE_SENSITIVE:
1182       gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
1183       break;
1184     case PROP_ALLOW_EMPTY:
1185       combo->ok_if_empty = g_value_get_boolean (value);
1186       break;
1187     case PROP_VALUE_IN_LIST:
1188       combo->value_in_list = g_value_get_boolean (value);
1189       break;
1190     default:
1191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1192       break;
1193     }
1194   
1195 }
1196
1197 static void
1198 gtk_combo_get_property (GObject         *object,
1199                         guint            prop_id,
1200                         GValue          *value,
1201                         GParamSpec      *pspec)
1202 {
1203   GtkCombo *combo = GTK_COMBO (object);
1204   
1205   switch (prop_id)
1206     {
1207     case PROP_ENABLE_ARROW_KEYS:
1208       g_value_set_boolean (value, combo->use_arrows);
1209       break;
1210     case PROP_ENABLE_ARROWS_ALWAYS:
1211       g_value_set_boolean (value, combo->use_arrows_always);
1212       break;
1213     case PROP_CASE_SENSITIVE:
1214       g_value_set_boolean (value, combo->case_sensitive);
1215       break;
1216     case PROP_ALLOW_EMPTY:
1217       g_value_set_boolean (value, combo->ok_if_empty);
1218       break;
1219     case PROP_VALUE_IN_LIST:
1220       g_value_set_boolean (value, combo->value_in_list);
1221       break;
1222     default:
1223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1224       break;
1225     }
1226    
1227 }