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