]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombo.c
Replace a lot of idle and timeout calls by the new gdk_threads api.
[~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 <config.h>
34 #include <string.h>
35
36 #include "gtkarrow.h"
37 #include "gtklabel.h"
38 #include "gtklist.h"
39 #include "gtkentry.h"
40 #include "gtkeventbox.h"
41 #include "gtkbutton.h"
42 #include "gtklistitem.h"
43 #include "gtkscrolledwindow.h"
44 #include "gtkmain.h"
45 #include "gtkwindow.h"
46 #include "gdk/gdkkeysyms.h"
47 #include "gtkcombo.h"
48 #include "gtkframe.h"
49 #include "gtkprivate.h"
50 #include "gtkintl.h"
51 #include "gtkalias.h"
52
53 static const gchar gtk_combo_string_key[] = "gtk-combo-string-value";
54
55 #define COMBO_LIST_MAX_HEIGHT   (400)
56 #define EMPTY_LIST_HEIGHT       (15)
57
58 enum {
59   PROP_0,
60   PROP_ENABLE_ARROW_KEYS,
61   PROP_ENABLE_ARROWS_ALWAYS,
62   PROP_CASE_SENSITIVE,
63   PROP_ALLOW_EMPTY,
64   PROP_VALUE_IN_LIST
65 };
66
67 static void         gtk_combo_realize            (GtkWidget        *widget);
68 static void         gtk_combo_unrealize          (GtkWidget        *widget);
69 static void         gtk_combo_destroy            (GtkObject        *combo);
70 static GtkListItem *gtk_combo_find               (GtkCombo         *combo);
71 static gchar *      gtk_combo_func               (GtkListItem      *li);
72 static gboolean     gtk_combo_focus_idle         (GtkCombo         *combo);
73 static gint         gtk_combo_entry_focus_out    (GtkEntry         *entry,
74                                                   GdkEventFocus    *event,
75                                                   GtkCombo         *combo);
76 static void         gtk_combo_get_pos            (GtkCombo         *combo,
77                                                   gint             *x,
78                                                   gint             *y,
79                                                   gint             *height,
80                                                   gint             *width);
81 static void         gtk_combo_popup_list         (GtkCombo         *combo);
82 static void         gtk_combo_popdown_list       (GtkCombo         *combo);
83
84 static void         gtk_combo_activate           (GtkWidget        *widget,
85                                                   GtkCombo         *combo);
86 static gboolean     gtk_combo_popup_button_press (GtkWidget        *button,
87                                                   GdkEventButton   *event,
88                                                   GtkCombo         *combo);
89 static gboolean     gtk_combo_popup_button_leave (GtkWidget        *button,
90                                                   GdkEventCrossing *event,
91                                                   GtkCombo         *combo);
92 static void         gtk_combo_update_entry       (GtkCombo         *combo);
93 static void         gtk_combo_update_list        (GtkEntry         *entry,
94                                                   GtkCombo         *combo);
95 static gint         gtk_combo_button_press       (GtkWidget        *widget,
96                                                   GdkEvent         *event,
97                                                   GtkCombo         *combo);
98 static void         gtk_combo_button_event_after (GtkWidget        *widget,
99                                                   GdkEvent         *event,
100                                                   GtkCombo         *combo);
101 static gint         gtk_combo_list_enter         (GtkWidget        *widget,
102                                                   GdkEventCrossing *event,
103                                                   GtkCombo         *combo);
104 static gint         gtk_combo_list_key_press     (GtkWidget        *widget,
105                                                   GdkEventKey      *event,
106                                                   GtkCombo         *combo);
107 static gint         gtk_combo_entry_key_press    (GtkEntry         *widget,
108                                                   GdkEventKey      *event,
109                                                   GtkCombo         *combo);
110 static gint         gtk_combo_window_key_press   (GtkWidget        *window,
111                                                   GdkEventKey      *event,
112                                                   GtkCombo         *combo);
113 static void         gtk_combo_size_allocate      (GtkWidget        *widget,
114                                                   GtkAllocation   *allocation);
115 static void         gtk_combo_set_property       (GObject         *object,
116                                                   guint            prop_id,
117                                                   const GValue    *value,
118                                                   GParamSpec      *pspec);
119 static void         gtk_combo_get_property       (GObject         *object,
120                                                   guint            prop_id,
121                                                   GValue          *value,
122                                                   GParamSpec      *pspec);
123
124 G_DEFINE_TYPE (GtkCombo, gtk_combo, GTK_TYPE_HBOX)
125
126 static void
127 gtk_combo_class_init (GtkComboClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GtkObjectClass *oclass;
131   GtkWidgetClass *widget_class;
132
133   gobject_class = (GObjectClass *) klass;
134   oclass = (GtkObjectClass *) klass;
135   widget_class = (GtkWidgetClass *) 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                                                          P_("Enable arrow keys"),
144                                                          P_("Whether the arrow keys move through the list of items"),
145                                                          TRUE,
146                                                          GTK_PARAM_READWRITE));
147   g_object_class_install_property (gobject_class,
148                                    PROP_ENABLE_ARROWS_ALWAYS,
149                                    g_param_spec_boolean ("enable-arrows-always",
150                                                          P_("Always enable arrows"),
151                                                          P_("Obsolete property, ignored"),
152                                                          TRUE,
153                                                          GTK_PARAM_READWRITE));
154   g_object_class_install_property (gobject_class,
155                                    PROP_CASE_SENSITIVE,
156                                    g_param_spec_boolean ("case-sensitive",
157                                                          P_("Case sensitive"),
158                                                          P_("Whether list item matching is case sensitive"),
159                                                          FALSE,
160                                                          GTK_PARAM_READWRITE));
161
162   g_object_class_install_property (gobject_class,
163                                    PROP_ALLOW_EMPTY,
164                                    g_param_spec_boolean ("allow-empty",
165                                                          P_("Allow empty"),
166                                                          P_("Whether an empty value may be entered in this field"),
167                                                          TRUE,
168                                                          GTK_PARAM_READWRITE));
169
170   g_object_class_install_property (gobject_class,
171                                    PROP_VALUE_IN_LIST,
172                                    g_param_spec_boolean ("value-in-list",
173                                                          P_("Value in list"),
174                                                          P_("Whether entered values must already be present in the list"),
175                                                          FALSE,
176                                                          GTK_PARAM_READWRITE));
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 (gtk_combo_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_utf8 (cmpl, prefix, &nprefix);
227
228       if (nprefix && strlen (nprefix) > strlen (prefix)) 
229         {
230           gtk_editable_insert_text (editable, g_utf8_offset_to_pointer (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), I_(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         g_source_unref (focus_idle);
415       
416       /*g_signal_stop_emission_by_name (entry, "focus_out_event"); */
417       return TRUE;
418     }
419   return FALSE;
420 }
421
422 static void
423 gtk_combo_get_pos (GtkCombo * combo, gint * x, gint * y, gint * height, gint * width)
424 {
425   GtkBin *popwin;
426   GtkWidget *widget;
427   GtkScrolledWindow *popup;
428   
429   gint real_height;
430   GtkRequisition list_requisition;
431   gboolean show_hscroll = FALSE;
432   gboolean show_vscroll = FALSE;
433   gint avail_height;
434   gint min_height;
435   gint alloc_width;
436   gint work_height;
437   gint old_height;
438   gint old_width;
439   gint scrollbar_spacing;
440   
441   widget = GTK_WIDGET (combo);
442   popup  = GTK_SCROLLED_WINDOW (combo->popup);
443   popwin = GTK_BIN (combo->popwin);
444
445   scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (popup);
446
447   gdk_window_get_origin (combo->entry->window, x, y);
448   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
449     *x -= widget->allocation.width - combo->entry->allocation.width;
450   real_height = MIN (combo->entry->requisition.height, 
451                      combo->entry->allocation.height);
452   *y += real_height;
453   avail_height = gdk_screen_get_height (gtk_widget_get_screen (widget)) - *y;
454   
455   gtk_widget_size_request (combo->list, &list_requisition);
456   min_height = MIN (list_requisition.height, 
457                     popup->vscrollbar->requisition.height);
458   if (!GTK_LIST (combo->list)->children)
459     list_requisition.height += EMPTY_LIST_HEIGHT;
460   
461   alloc_width = (widget->allocation.width -
462                  2 * popwin->child->style->xthickness -
463                  2 * GTK_CONTAINER (popwin->child)->border_width -
464                  2 * GTK_CONTAINER (combo->popup)->border_width -
465                  2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width - 
466                  2 * GTK_BIN (popup)->child->style->xthickness);
467   
468   work_height = (2 * popwin->child->style->ythickness +
469                  2 * GTK_CONTAINER (popwin->child)->border_width +
470                  2 * GTK_CONTAINER (combo->popup)->border_width +
471                  2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width +
472                  2 * GTK_BIN (popup)->child->style->ythickness);
473   
474   do 
475     {
476       old_width = alloc_width;
477       old_height = work_height;
478       
479       if (!show_hscroll &&
480           alloc_width < list_requisition.width)
481         {
482           GtkRequisition requisition;
483           
484           gtk_widget_size_request (popup->hscrollbar, &requisition);
485           work_height += (requisition.height + scrollbar_spacing);
486           
487           show_hscroll = TRUE;
488         }
489       if (!show_vscroll && 
490           work_height + list_requisition.height > avail_height)
491         {
492           GtkRequisition requisition;
493           
494           if (work_height + min_height > avail_height && 
495               *y - real_height > avail_height)
496             {
497               *y -= (work_height + list_requisition.height + real_height);
498               break;
499             }
500           gtk_widget_size_request (popup->hscrollbar, &requisition);
501           alloc_width -= (requisition.width + scrollbar_spacing);
502           show_vscroll = TRUE;
503         }
504     } while (old_width != alloc_width || old_height != work_height);
505   
506   *width = widget->allocation.width;
507   if (show_vscroll)
508     *height = avail_height;
509   else
510     *height = work_height + list_requisition.height;
511   
512   if (*x < 0)
513     *x = 0;
514 }
515
516 static void
517 gtk_combo_popup_list (GtkCombo *combo)
518 {
519   GtkWidget *toplevel;
520   GtkList *list;
521   gint height, width, x, y;
522   gint old_width, old_height;
523
524   old_width = combo->popwin->allocation.width;
525   old_height  = combo->popwin->allocation.height;
526
527   gtk_combo_get_pos (combo, &x, &y, &height, &width);
528
529   /* workaround for gtk_scrolled_window_size_allocate bug */
530   if (old_width != width || old_height != height)
531     {
532       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar);
533       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar);
534     }
535
536   gtk_combo_update_list (GTK_ENTRY (combo->entry), combo);
537
538   /* We need to make sure some child of combo->popwin
539    * is focused to disable GtkWindow's automatic
540    * "focus-the-first-item" code. If there is no selected
541    * child, we focus the list itself with some hackery.
542    */
543   list = GTK_LIST (combo->list);
544   
545   if (list->selection)
546     {
547       gtk_widget_grab_focus (list->selection->data);
548     }
549   else
550     {
551       GTK_WIDGET_SET_FLAGS (list, GTK_CAN_FOCUS);
552       gtk_widget_grab_focus (combo->list);
553       GTK_LIST (combo->list)->last_focus_child = NULL;
554       GTK_WIDGET_UNSET_FLAGS (list, GTK_CAN_FOCUS);
555     }
556   
557   gtk_window_move (GTK_WINDOW (combo->popwin), x, y);
558
559   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo));
560
561   if (GTK_IS_WINDOW (toplevel))
562     {
563       gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
564                                    GTK_WINDOW (combo->popwin));
565       gtk_window_set_transient_for (GTK_WINDOW (combo->popwin), GTK_WINDOW (toplevel));
566     }
567
568   gtk_widget_set_size_request (combo->popwin, width, height);
569   gtk_widget_show (combo->popwin);
570
571   gtk_widget_grab_focus (combo->popwin);
572 }
573
574 static void
575 gtk_combo_popdown_list (GtkCombo *combo)
576 {
577   combo->current_button = 0;
578       
579   if (GTK_BUTTON (combo->button)->in_button)
580     {
581       GTK_BUTTON (combo->button)->in_button = FALSE;
582       gtk_button_released (GTK_BUTTON (combo->button));
583     }
584
585   if (GTK_WIDGET_HAS_GRAB (combo->popwin))
586     {
587       gtk_grab_remove (combo->popwin);
588       gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
589                                   gtk_get_current_event_time ());
590       gdk_display_keyboard_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
591                                    gtk_get_current_event_time ());
592     }
593   
594   gtk_widget_hide (combo->popwin);
595
596   gtk_window_group_add_window (gtk_window_get_group (NULL), GTK_WINDOW (combo->popwin));
597 }
598
599 static gboolean
600 popup_grab_on_window (GdkWindow *window,
601                       guint32    activate_time)
602 {
603   if ((gdk_pointer_grab (window, TRUE,
604                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
605                          GDK_POINTER_MOTION_MASK,
606                          NULL, NULL, activate_time) == 0))
607     {
608       if (gdk_keyboard_grab (window, TRUE,
609                              activate_time) == 0)
610         return TRUE;
611       else
612         {
613           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
614                                       activate_time);
615           return FALSE;
616         }
617     }
618
619   return FALSE;
620 }
621
622 static void        
623 gtk_combo_activate (GtkWidget        *widget,
624                     GtkCombo         *combo)
625 {
626   if (!combo->button->window ||
627       !popup_grab_on_window (combo->button->window,
628                              gtk_get_current_event_time ()))
629     return;
630
631   gtk_combo_popup_list (combo);
632   
633   /* This must succeed since we already have the grab */
634   popup_grab_on_window (combo->popwin->window,
635                         gtk_get_current_event_time ());
636
637   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
638     gtk_widget_grab_focus (combo->entry);
639
640   gtk_grab_add (combo->popwin);
641 }
642
643 static gboolean
644 gtk_combo_popup_button_press (GtkWidget        *button,
645                               GdkEventButton   *event,
646                               GtkCombo         *combo)
647 {
648   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
649     gtk_widget_grab_focus (combo->entry);
650
651   if (event->button != 1)
652     return FALSE;
653
654   if (!popup_grab_on_window (combo->button->window,
655                              gtk_get_current_event_time ()))
656     return FALSE;
657
658   combo->current_button = event->button;
659
660   gtk_combo_popup_list (combo);
661
662   /* This must succeed since we already have the grab */
663   popup_grab_on_window (combo->popwin->window,
664                         gtk_get_current_event_time ());
665
666   gtk_button_pressed (GTK_BUTTON (button));
667
668   gtk_grab_add (combo->popwin);
669
670   return TRUE;
671 }
672
673 static gboolean
674 gtk_combo_popup_button_leave (GtkWidget        *button,
675                               GdkEventCrossing *event,
676                               GtkCombo         *combo)
677 {
678   /* The idea here is that we want to keep the button down if the
679    * popup is popped up.
680    */
681   return combo->current_button != 0;
682 }
683
684 static void
685 gtk_combo_update_entry (GtkCombo * combo)
686 {
687   GtkList *list = GTK_LIST (combo->list);
688   char *text;
689
690   g_signal_handler_block (list, combo->list_change_id);
691   if (list->selection)
692     {
693       text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
694       if (!text)
695         text = "";
696       gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
697     }
698   g_signal_handler_unblock (list, combo->list_change_id);
699 }
700
701 static void
702 gtk_combo_selection_changed (GtkList  *list,
703                              GtkCombo *combo)
704 {
705   if (!GTK_WIDGET_VISIBLE (combo->popwin))
706     gtk_combo_update_entry (combo);
707 }
708
709 static void
710 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
711 {
712   GtkList *list = GTK_LIST (combo->list);
713   GList *slist = list->selection;
714   GtkListItem *li;
715
716   gtk_grab_remove (GTK_WIDGET (combo));
717
718   g_signal_handler_block (entry, combo->entry_change_id);
719   if (slist && slist->data)
720     gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
721   li = gtk_combo_find (combo);
722   if (li)
723     gtk_list_select_child (list, GTK_WIDGET (li));
724   g_signal_handler_unblock (entry, combo->entry_change_id);
725 }
726
727 static gint
728 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
729 {
730   GtkWidget *child;
731
732   child = gtk_get_event_widget (event);
733
734   /* We don't ask for button press events on the grab widget, so
735    *  if an event is reported directly to the grab widget, it must
736    *  be on a window outside the application (and thus we remove
737    *  the popup window). Otherwise, we check if the widget is a child
738    *  of the grab widget, and only remove the popup window if it
739    *  is not.
740    */
741   if (child != widget)
742     {
743       while (child)
744         {
745           if (child == widget)
746             return FALSE;
747           child = child->parent;
748         }
749     }
750
751   gtk_combo_popdown_list (combo);
752
753   return TRUE;
754 }
755
756 static gboolean
757 is_within (GtkWidget *widget,
758            GtkWidget *ancestor)
759 {
760   return widget == ancestor || gtk_widget_is_ancestor (widget, ancestor);
761 }
762
763 static void
764 gtk_combo_button_event_after (GtkWidget *widget,
765                               GdkEvent  *event,
766                               GtkCombo  *combo)
767 {
768   GtkWidget *child;
769
770   if (event->type != GDK_BUTTON_RELEASE)
771     return;
772   
773   child = gtk_get_event_widget ((GdkEvent*) event);
774
775   if ((combo->current_button != 0) && (event->button.button == 1))
776     {
777       /* This was the initial button press */
778
779       combo->current_button = 0;
780
781       /* Check to see if we released inside the button */
782       if (child && is_within (child, combo->button))
783         {
784           gtk_grab_add (combo->popwin);
785           gdk_pointer_grab (combo->popwin->window, TRUE,
786                             GDK_BUTTON_PRESS_MASK | 
787                             GDK_BUTTON_RELEASE_MASK |
788                             GDK_POINTER_MOTION_MASK, 
789                             NULL, NULL, GDK_CURRENT_TIME);
790           return;
791         }
792     }
793
794   if (is_within (child, combo->list))
795     gtk_combo_update_entry (combo);
796     
797   gtk_combo_popdown_list (combo);
798
799 }
800
801 static void
802 find_child_foreach (GtkWidget *widget,
803                     gpointer   data)
804 {
805   GdkEventButton *event = data;
806
807   if (!event->window)
808     {
809       if (event->x >= widget->allocation.x &&
810           event->x < widget->allocation.x + widget->allocation.width &&
811           event->y >= widget->allocation.y &&
812           event->y < widget->allocation.y + widget->allocation.height)
813         event->window = g_object_ref (widget->window);
814     }
815 }
816
817 static void
818 find_child_window (GtkContainer   *container,
819                    GdkEventButton *event)
820 {
821   gtk_container_foreach (container, find_child_foreach, event);
822 }
823
824 static gint         
825 gtk_combo_list_enter (GtkWidget        *widget,
826                       GdkEventCrossing *event,
827                       GtkCombo         *combo)
828 {
829   GtkWidget *event_widget;
830
831   event_widget = gtk_get_event_widget ((GdkEvent*) event);
832
833   if ((event_widget == combo->list) &&
834       (combo->current_button != 0) && 
835       (!GTK_WIDGET_HAS_GRAB (combo->list)))
836     {
837       GdkEvent *tmp_event = gdk_event_new (GDK_BUTTON_PRESS);
838       gint x, y;
839       GdkModifierType mask;
840
841       gtk_grab_remove (combo->popwin);
842
843       /* Transfer the grab over to the list by synthesizing
844        * a button press event
845        */
846       gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
847
848       tmp_event->button.send_event = TRUE;
849       tmp_event->button.time = GDK_CURRENT_TIME; /* bad */
850       tmp_event->button.x = x;
851       tmp_event->button.y = y;
852       /* We leave all the XInput fields unfilled here, in the expectation
853        * that GtkList doesn't care.
854        */
855       tmp_event->button.button = combo->current_button;
856       tmp_event->button.state = mask;
857
858       find_child_window (GTK_CONTAINER (combo->list), &tmp_event->button);
859       if (!tmp_event->button.window)
860         {
861           GtkWidget *child;
862           
863           if (GTK_LIST (combo->list)->children)
864             child = GTK_LIST (combo->list)->children->data;
865           else
866             child = combo->list;
867
868           tmp_event->button.window = g_object_ref (child->window);
869         }
870
871       gtk_widget_event (combo->list, tmp_event);
872       gdk_event_free (tmp_event);
873     }
874
875   return FALSE;
876 }
877
878 static int
879 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
880 {
881   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
882
883   if (event->keyval == GDK_Escape && state == 0)
884     {
885       if (GTK_WIDGET_HAS_GRAB (combo->list))
886         gtk_list_end_drag_selection (GTK_LIST (combo->list));
887
888       gtk_combo_popdown_list (combo);
889       
890       return TRUE;
891     }
892   return FALSE;
893 }
894
895 static void
896 combo_event_box_realize (GtkWidget *widget)
897 {
898   GdkCursor *cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
899                                                   GDK_TOP_LEFT_ARROW);
900   gdk_window_set_cursor (widget->window, cursor);
901   gdk_cursor_unref (cursor);
902 }
903
904 static void
905 gtk_combo_init (GtkCombo * combo)
906 {
907   GtkWidget *arrow;
908   GtkWidget *frame;
909   GtkWidget *event_box;
910
911   combo->case_sensitive = FALSE;
912   combo->value_in_list = FALSE;
913   combo->ok_if_empty = TRUE;
914   combo->use_arrows = TRUE;
915   combo->use_arrows_always = FALSE;
916   combo->entry = gtk_entry_new ();
917   combo->button = gtk_button_new ();
918   combo->current_button = 0;
919   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
920   gtk_widget_show (arrow);
921   gtk_container_add (GTK_CONTAINER (combo->button), arrow);
922   gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
923   gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
924   GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
925   gtk_widget_show (combo->entry);
926   gtk_widget_show (combo->button);
927   combo->entry_change_id = g_signal_connect (combo->entry, "changed",
928                                              G_CALLBACK (gtk_combo_update_list),
929                                              combo);
930   g_signal_connect_after (combo->entry, "key_press_event",
931                           G_CALLBACK (gtk_combo_entry_key_press), combo);
932   g_signal_connect_after (combo->entry, "focus_out_event",
933                           G_CALLBACK (gtk_combo_entry_focus_out), combo);
934   combo->activate_id = g_signal_connect (combo->entry, "activate",
935                                          G_CALLBACK (gtk_combo_activate),
936                                          combo);
937   g_signal_connect (combo->button, "button_press_event",
938                     G_CALLBACK (gtk_combo_popup_button_press), combo);
939   g_signal_connect (combo->button, "leave_notify_event",
940                     G_CALLBACK (gtk_combo_popup_button_leave), combo);
941
942   combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
943   gtk_window_set_type_hint (GTK_WINDOW (combo->popwin), GDK_WINDOW_TYPE_HINT_COMBO);
944   g_object_ref (combo->popwin);
945   gtk_window_set_resizable (GTK_WINDOW (combo->popwin), FALSE);
946
947   g_signal_connect (combo->popwin, "key_press_event",
948                     G_CALLBACK (gtk_combo_window_key_press), combo);
949   
950   gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
951
952   event_box = gtk_event_box_new ();
953   gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
954   g_signal_connect (event_box, "realize",
955                     G_CALLBACK (combo_event_box_realize), NULL);
956   gtk_widget_show (event_box);
957
958
959   frame = gtk_frame_new (NULL);
960   gtk_container_add (GTK_CONTAINER (event_box), frame);
961   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
962   gtk_widget_show (frame);
963
964   combo->popup = gtk_scrolled_window_new (NULL, NULL);
965   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
966                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
967   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
968   GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
969   gtk_container_add (GTK_CONTAINER (frame), combo->popup);
970   gtk_widget_show (combo->popup);
971
972   combo->list = gtk_list_new ();
973   /* We'll use enter notify events to figure out when to transfer
974    * the grab to the list
975    */
976   gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
977
978   gtk_list_set_selection_mode (GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
979   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (combo->popup), combo->list);
980   gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
981                                        gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
982   gtk_container_set_focus_hadjustment (GTK_CONTAINER (combo->list),
983                                        gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
984   gtk_widget_show (combo->list);
985
986   combo->list_change_id = g_signal_connect (combo->list, "selection-changed",
987                                             G_CALLBACK (gtk_combo_selection_changed), combo);
988   
989   g_signal_connect (combo->popwin, "key_press_event",
990                     G_CALLBACK (gtk_combo_list_key_press), combo);
991   g_signal_connect (combo->popwin, "button_press_event",
992                     G_CALLBACK (gtk_combo_button_press), combo);
993
994   g_signal_connect (combo->popwin, "event_after",
995                     G_CALLBACK (gtk_combo_button_event_after), combo);
996   g_signal_connect (combo->list, "event_after",
997                     G_CALLBACK (gtk_combo_button_event_after), combo);
998
999   g_signal_connect (combo->list, "enter_notify_event",
1000                     G_CALLBACK (gtk_combo_list_enter), combo);
1001 }
1002
1003 static void
1004 gtk_combo_realize (GtkWidget *widget)
1005 {
1006   GtkCombo *combo = GTK_COMBO (widget);
1007
1008   gtk_window_set_screen (GTK_WINDOW (combo->popwin), 
1009                          gtk_widget_get_screen (widget));
1010   
1011   GTK_WIDGET_CLASS (gtk_combo_parent_class)->realize (widget);  
1012 }
1013
1014 static void        
1015 gtk_combo_unrealize (GtkWidget *widget)
1016 {
1017   GtkCombo *combo = GTK_COMBO (widget);
1018
1019   gtk_combo_popdown_list (combo);
1020   gtk_widget_unrealize (combo->popwin);
1021   
1022   GTK_WIDGET_CLASS (gtk_combo_parent_class)->unrealize (widget);
1023 }
1024
1025 GtkWidget*
1026 gtk_combo_new (void)
1027 {
1028   return g_object_new (GTK_TYPE_COMBO, NULL);
1029 }
1030
1031 void
1032 gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
1033 {
1034   g_return_if_fail (GTK_IS_COMBO (combo));
1035   val = val != FALSE;
1036   ok_if_empty = ok_if_empty != FALSE;
1037
1038   g_object_freeze_notify (G_OBJECT (combo));
1039   if (combo->value_in_list != val)
1040     {
1041        combo->value_in_list = val;
1042   g_object_notify (G_OBJECT (combo), "value-in-list");
1043     }
1044   if (combo->ok_if_empty != ok_if_empty)
1045     {
1046        combo->ok_if_empty = ok_if_empty;
1047   g_object_notify (G_OBJECT (combo), "allow-empty");
1048     }
1049   g_object_thaw_notify (G_OBJECT (combo));
1050 }
1051
1052 void
1053 gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
1054 {
1055   g_return_if_fail (GTK_IS_COMBO (combo));
1056   val = val != FALSE;
1057
1058   if (combo->case_sensitive != val) 
1059     {
1060   combo->case_sensitive = val;
1061   g_object_notify (G_OBJECT (combo), "case-sensitive");
1062     }
1063 }
1064
1065 void
1066 gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
1067 {
1068   g_return_if_fail (GTK_IS_COMBO (combo));
1069   val = val != FALSE;
1070
1071   if (combo->use_arrows != val) 
1072     {
1073   combo->use_arrows = val;
1074   g_object_notify (G_OBJECT (combo), "enable-arrow-keys");
1075     }
1076 }
1077
1078 void
1079 gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
1080 {
1081   g_return_if_fail (GTK_IS_COMBO (combo));
1082   val = val != FALSE;
1083
1084   if (combo->use_arrows_always != val) 
1085     {
1086        g_object_freeze_notify (G_OBJECT (combo));
1087   combo->use_arrows_always = val;
1088        g_object_notify (G_OBJECT (combo), "enable-arrows-always");
1089
1090        if (combo->use_arrows != TRUE) 
1091          {
1092   combo->use_arrows = TRUE;
1093   g_object_notify (G_OBJECT (combo), "enable-arrow-keys");
1094          }
1095   g_object_thaw_notify (G_OBJECT (combo));
1096     }
1097 }
1098
1099 void
1100 gtk_combo_set_popdown_strings (GtkCombo *combo, 
1101                                GList    *strings)
1102 {
1103   GList *list;
1104   GtkWidget *li;
1105
1106   g_return_if_fail (GTK_IS_COMBO (combo));
1107
1108   gtk_combo_popdown_list (combo);
1109
1110   gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
1111   list = strings;
1112   while (list)
1113     {
1114       li = gtk_list_item_new_with_label ((gchar *) list->data);
1115       gtk_widget_show (li);
1116       gtk_container_add (GTK_CONTAINER (combo->list), li);
1117       list = list->next;
1118     }
1119 }
1120
1121 void
1122 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
1123 {
1124   g_return_if_fail (GTK_IS_COMBO (combo));
1125   g_return_if_fail (item != NULL);
1126
1127   g_object_set_data_full (G_OBJECT (item), I_(gtk_combo_string_key),
1128                           g_strdup (item_value), g_free);
1129 }
1130
1131 static void
1132 gtk_combo_size_allocate (GtkWidget     *widget,
1133                          GtkAllocation *allocation)
1134 {
1135   GtkCombo *combo;
1136
1137   g_return_if_fail (GTK_IS_COMBO (widget));
1138   g_return_if_fail (allocation != NULL);
1139
1140   GTK_WIDGET_CLASS (gtk_combo_parent_class)->size_allocate (widget, allocation);
1141   
1142   combo = GTK_COMBO (widget);
1143
1144   if (combo->entry->allocation.height > combo->entry->requisition.height)
1145     {
1146       GtkAllocation button_allocation;
1147
1148       button_allocation = combo->button->allocation;
1149       button_allocation.height = combo->entry->requisition.height;
1150       button_allocation.y = combo->entry->allocation.y + 
1151         (combo->entry->allocation.height - combo->entry->requisition.height) 
1152         / 2;
1153       gtk_widget_size_allocate (combo->button, &button_allocation);
1154     }
1155 }
1156
1157 void
1158 gtk_combo_disable_activate (GtkCombo* combo)
1159 {
1160   g_return_if_fail (GTK_IS_COMBO (combo));
1161
1162   if ( combo->activate_id ) {
1163     g_signal_handler_disconnect (combo->entry, combo->activate_id);
1164     combo->activate_id = 0;
1165   }
1166 }
1167
1168 static void
1169 gtk_combo_set_property (GObject         *object,
1170                         guint            prop_id,
1171                         const GValue    *value,
1172                         GParamSpec      *pspec)
1173 {
1174   GtkCombo *combo = GTK_COMBO (object);
1175   
1176   switch (prop_id)
1177     {
1178     case PROP_ENABLE_ARROW_KEYS:
1179       gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
1180       break;
1181     case PROP_ENABLE_ARROWS_ALWAYS:
1182       gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
1183       break;
1184     case PROP_CASE_SENSITIVE:
1185       gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
1186       break;
1187     case PROP_ALLOW_EMPTY:
1188       combo->ok_if_empty = g_value_get_boolean (value);
1189       break;
1190     case PROP_VALUE_IN_LIST:
1191       combo->value_in_list = g_value_get_boolean (value);
1192       break;
1193     default:
1194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1195       break;
1196     }
1197   
1198 }
1199
1200 static void
1201 gtk_combo_get_property (GObject         *object,
1202                         guint            prop_id,
1203                         GValue          *value,
1204                         GParamSpec      *pspec)
1205 {
1206   GtkCombo *combo = GTK_COMBO (object);
1207   
1208   switch (prop_id)
1209     {
1210     case PROP_ENABLE_ARROW_KEYS:
1211       g_value_set_boolean (value, combo->use_arrows);
1212       break;
1213     case PROP_ENABLE_ARROWS_ALWAYS:
1214       g_value_set_boolean (value, combo->use_arrows_always);
1215       break;
1216     case PROP_CASE_SENSITIVE:
1217       g_value_set_boolean (value, combo->case_sensitive);
1218       break;
1219     case PROP_ALLOW_EMPTY:
1220       g_value_set_boolean (value, combo->ok_if_empty);
1221       break;
1222     case PROP_VALUE_IN_LIST:
1223       g_value_set_boolean (value, combo->value_in_list);
1224       break;
1225     default:
1226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1227       break;
1228     }
1229    
1230 }
1231
1232 #define __GTK_SMART_COMBO_C__
1233 #include "gtkaliasdef.c"