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