]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombo.c
e7963f9609a26ca656590051771ff8b80b0dc327
[~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 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_class_init         (GtkComboClass    *klass);
68 static void         gtk_combo_init               (GtkCombo         *combo);
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 gint         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 static GtkHBoxClass *parent_class = NULL;
126
127 static void
128 gtk_combo_class_init (GtkComboClass * klass)
129 {
130   GObjectClass *gobject_class;
131   GtkObjectClass *oclass;
132   GtkWidgetClass *widget_class;
133
134   gobject_class = (GObjectClass *) klass;
135   oclass = (GtkObjectClass *) klass;
136   widget_class = (GtkWidgetClass *) klass;
137
138   parent_class = g_type_class_peek_parent (klass);
139
140   gobject_class->set_property = gtk_combo_set_property; 
141   gobject_class->get_property = gtk_combo_get_property; 
142
143   g_object_class_install_property (gobject_class,
144                                    PROP_ENABLE_ARROW_KEYS,
145                                    g_param_spec_boolean ("enable-arrow-keys",
146                                                          P_("Enable arrow keys"),
147                                                          P_("Whether the arrow keys move through the list of items"),
148                                                          TRUE,
149                                                          GTK_PARAM_READWRITE));
150   g_object_class_install_property (gobject_class,
151                                    PROP_ENABLE_ARROWS_ALWAYS,
152                                    g_param_spec_boolean ("enable-arrows-always",
153                                                          P_("Always enable arrows"),
154                                                          P_("Obsolete property, ignored"),
155                                                          TRUE,
156                                                          GTK_PARAM_READWRITE));
157   g_object_class_install_property (gobject_class,
158                                    PROP_CASE_SENSITIVE,
159                                    g_param_spec_boolean ("case-sensitive",
160                                                          P_("Case sensitive"),
161                                                          P_("Whether list item matching is case sensitive"),
162                                                          FALSE,
163                                                          GTK_PARAM_READWRITE));
164
165   g_object_class_install_property (gobject_class,
166                                    PROP_ALLOW_EMPTY,
167                                    g_param_spec_boolean ("allow-empty",
168                                                          P_("Allow empty"),
169                                                          P_("Whether an empty value may be entered in this field"),
170                                                          TRUE,
171                                                          GTK_PARAM_READWRITE));
172
173   g_object_class_install_property (gobject_class,
174                                    PROP_VALUE_IN_LIST,
175                                    g_param_spec_boolean ("value-in-list",
176                                                          P_("Value in list"),
177                                                          P_("Whether entered values must already be present in the list"),
178                                                          FALSE,
179                                                          GTK_PARAM_READWRITE));
180   
181    
182   oclass->destroy = gtk_combo_destroy;
183   
184   widget_class->size_allocate = gtk_combo_size_allocate;
185   widget_class->realize = gtk_combo_realize;
186   widget_class->unrealize = gtk_combo_unrealize;
187 }
188
189 static void
190 gtk_combo_destroy (GtkObject *object)
191 {
192   GtkCombo *combo = GTK_COMBO (object);
193
194   if (combo->popwin)
195     {
196       gtk_widget_destroy (combo->popwin);
197       g_object_unref (combo->popwin);
198       combo->popwin = NULL;
199     }
200
201   GTK_OBJECT_CLASS (parent_class)->destroy (object);
202 }
203
204 static int
205 gtk_combo_entry_key_press (GtkEntry * entry, GdkEventKey * event, GtkCombo * combo)
206 {
207   GList *li;
208   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
209
210   /* completion */
211   if ((event->keyval == GDK_Tab ||  event->keyval == GDK_KP_Tab) &&
212       state == GDK_MOD1_MASK)
213     {
214       GtkEditable *editable = GTK_EDITABLE (entry);
215       GCompletion * cmpl;
216       gchar* prefix;
217       gchar* nprefix = NULL;
218       gint pos;
219
220       if ( !GTK_LIST (combo->list)->children )
221         return FALSE;
222     
223       cmpl = g_completion_new ((GCompletionFunc)gtk_combo_func);
224       g_completion_add_items (cmpl, GTK_LIST (combo->list)->children);
225
226       pos = gtk_editable_get_position (editable);
227       prefix = gtk_editable_get_chars (editable, 0, pos);
228
229       g_completion_complete_utf8 (cmpl, prefix, &nprefix);
230
231       if (nprefix && strlen (nprefix) > strlen (prefix)) 
232         {
233           gtk_editable_insert_text (editable, g_utf8_offset_to_pointer (nprefix, pos), 
234                                     strlen (nprefix) - strlen (prefix), &pos);
235           gtk_editable_set_position (editable, pos);
236         }
237
238       if (nprefix)
239         g_free (nprefix);
240       g_free (prefix);
241       g_completion_free (cmpl);
242
243       return TRUE;
244     }
245
246   if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
247       state == GDK_MOD1_MASK)
248     {
249       gtk_combo_activate (NULL, combo);
250       return TRUE;
251     }
252
253   if (!combo->use_arrows || !GTK_LIST (combo->list)->children)
254     return FALSE;
255
256   gtk_combo_update_list (GTK_ENTRY (combo->entry), combo);
257   li = g_list_find (GTK_LIST (combo->list)->children, gtk_combo_find (combo));
258
259   if (((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && state == 0) ||
260       ((event->keyval == 'p' || event->keyval == 'P') && state == GDK_MOD1_MASK))
261     {
262       if (!li)
263         li = g_list_last (GTK_LIST (combo->list)->children);
264       else
265         li = li->prev;
266
267       if (li)
268         {
269           gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
270           gtk_combo_update_entry (combo);
271         }
272       
273       return TRUE;
274     }
275   if (((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) && state == 0) ||
276       ((event->keyval == 'n' || event->keyval == 'N') && state == GDK_MOD1_MASK))
277     {
278       if (!li)
279         li = GTK_LIST (combo->list)->children;
280       else if (li)
281         li = li->next;
282       if (li)
283         {
284           gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
285           gtk_combo_update_entry (combo);
286         }
287       
288       return TRUE;
289     }
290   return FALSE;
291 }
292
293 static int
294 gtk_combo_window_key_press (GtkWidget   *window,
295                             GdkEventKey *event,
296                             GtkCombo    *combo)
297 {
298   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
299
300   if ((event->keyval == GDK_Return || 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), g_intern_static_string (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       
418       /*g_signal_stop_emission_by_name (entry, "focus_out_event"); */
419       return TRUE;
420     }
421   return FALSE;
422 }
423
424 static void
425 gtk_combo_get_pos (GtkCombo * combo, gint * x, gint * y, gint * height, gint * width)
426 {
427   GtkBin *popwin;
428   GtkWidget *widget;
429   GtkScrolledWindow *popup;
430   
431   gint real_height;
432   GtkRequisition list_requisition;
433   gboolean show_hscroll = FALSE;
434   gboolean show_vscroll = FALSE;
435   gint avail_height;
436   gint min_height;
437   gint alloc_width;
438   gint work_height;
439   gint old_height;
440   gint old_width;
441   gint scrollbar_spacing;
442   
443   widget = GTK_WIDGET (combo);
444   popup  = GTK_SCROLLED_WINDOW (combo->popup);
445   popwin = GTK_BIN (combo->popwin);
446
447   scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (popup);
448
449   gdk_window_get_origin (combo->entry->window, x, y);
450   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
451     *x -= widget->allocation.width - combo->entry->allocation.width;
452   real_height = MIN (combo->entry->requisition.height, 
453                      combo->entry->allocation.height);
454   *y += real_height;
455   avail_height = gdk_screen_get_height (gtk_widget_get_screen (widget)) - *y;
456   
457   gtk_widget_size_request (combo->list, &list_requisition);
458   min_height = MIN (list_requisition.height, 
459                     popup->vscrollbar->requisition.height);
460   if (!GTK_LIST (combo->list)->children)
461     list_requisition.height += EMPTY_LIST_HEIGHT;
462   
463   alloc_width = (widget->allocation.width -
464                  2 * popwin->child->style->xthickness -
465                  2 * GTK_CONTAINER (popwin->child)->border_width -
466                  2 * GTK_CONTAINER (combo->popup)->border_width -
467                  2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width - 
468                  2 * GTK_BIN (popup)->child->style->xthickness);
469   
470   work_height = (2 * popwin->child->style->ythickness +
471                  2 * GTK_CONTAINER (popwin->child)->border_width +
472                  2 * GTK_CONTAINER (combo->popup)->border_width +
473                  2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width +
474                  2 * GTK_BIN (popup)->child->style->ythickness);
475   
476   do 
477     {
478       old_width = alloc_width;
479       old_height = work_height;
480       
481       if (!show_hscroll &&
482           alloc_width < list_requisition.width)
483         {
484           GtkRequisition requisition;
485           
486           gtk_widget_size_request (popup->hscrollbar, &requisition);
487           work_height += (requisition.height + scrollbar_spacing);
488           
489           show_hscroll = TRUE;
490         }
491       if (!show_vscroll && 
492           work_height + list_requisition.height > avail_height)
493         {
494           GtkRequisition requisition;
495           
496           if (work_height + min_height > avail_height && 
497               *y - real_height > avail_height)
498             {
499               *y -= (work_height + list_requisition.height + real_height);
500               break;
501             }
502           gtk_widget_size_request (popup->hscrollbar, &requisition);
503           alloc_width -= (requisition.width + scrollbar_spacing);
504           show_vscroll = TRUE;
505         }
506     } while (old_width != alloc_width || old_height != work_height);
507   
508   *width = widget->allocation.width;
509   if (show_vscroll)
510     *height = avail_height;
511   else
512     *height = work_height + list_requisition.height;
513   
514   if (*x < 0)
515     *x = 0;
516 }
517
518 static void
519 gtk_combo_popup_list (GtkCombo *combo)
520 {
521   GtkWidget *toplevel;
522   GtkList *list;
523   gint height, width, x, y;
524   gint old_width, old_height;
525
526   old_width = combo->popwin->allocation.width;
527   old_height  = combo->popwin->allocation.height;
528
529   gtk_combo_get_pos (combo, &x, &y, &height, &width);
530
531   /* workaround for gtk_scrolled_window_size_allocate bug */
532   if (old_width != width || old_height != height)
533     {
534       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar);
535       gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar);
536     }
537
538   gtk_combo_update_list (GTK_ENTRY (combo->entry), combo);
539
540   /* We need to make sure some child of combo->popwin
541    * is focused to disable GtkWindow's automatic
542    * "focus-the-first-item" code. If there is no selected
543    * child, we focus the list itself with some hackery.
544    */
545   list = GTK_LIST (combo->list);
546   
547   if (list->selection)
548     {
549       gtk_widget_grab_focus (list->selection->data);
550     }
551   else
552     {
553       GTK_WIDGET_SET_FLAGS (list, GTK_CAN_FOCUS);
554       gtk_widget_grab_focus (combo->list);
555       GTK_LIST (combo->list)->last_focus_child = NULL;
556       GTK_WIDGET_UNSET_FLAGS (list, GTK_CAN_FOCUS);
557     }
558   
559   gtk_window_move (GTK_WINDOW (combo->popwin), x, y);
560
561   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo));
562
563   if (GTK_IS_WINDOW (toplevel))
564     {
565       gtk_window_group_add_window (_gtk_window_get_group (GTK_WINDOW (toplevel)), 
566                                    GTK_WINDOW (combo->popwin));
567     }
568
569   gtk_widget_set_size_request (combo->popwin, width, height);
570   gtk_widget_show (combo->popwin);
571
572   gtk_widget_grab_focus (combo->popwin);
573 }
574
575 static void
576 gtk_combo_popdown_list (GtkCombo *combo)
577 {
578   combo->current_button = 0;
579       
580   if (GTK_BUTTON (combo->button)->in_button)
581     {
582       GTK_BUTTON (combo->button)->in_button = FALSE;
583       gtk_button_released (GTK_BUTTON (combo->button));
584     }
585
586   if (GTK_WIDGET_HAS_GRAB (combo->popwin))
587     {
588       gtk_grab_remove (combo->popwin);
589       gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
590                                   gtk_get_current_event_time ());
591       gdk_display_keyboard_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
592                                    gtk_get_current_event_time ());
593     }
594   
595   gtk_widget_hide (combo->popwin);
596
597   gtk_window_group_add_window (_gtk_window_get_group (NULL), GTK_WINDOW (combo->popwin));
598 }
599
600 static gboolean
601 popup_grab_on_window (GdkWindow *window,
602                       guint32    activate_time)
603 {
604   if ((gdk_pointer_grab (window, TRUE,
605                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
606                          GDK_POINTER_MOTION_MASK,
607                          NULL, NULL, activate_time) == 0))
608     {
609       if (gdk_keyboard_grab (window, TRUE,
610                              activate_time) == 0)
611         return TRUE;
612       else
613         {
614           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
615                                       activate_time);
616           return FALSE;
617         }
618     }
619
620   return FALSE;
621 }
622
623 static void        
624 gtk_combo_activate (GtkWidget        *widget,
625                     GtkCombo         *combo)
626 {
627   if (!combo->button->window ||
628       !popup_grab_on_window (combo->button->window,
629                              gtk_get_current_event_time ()))
630     return;
631
632   gtk_combo_popup_list (combo);
633   
634   /* This must succeed since we already have the grab */
635   popup_grab_on_window (combo->popwin->window,
636                         gtk_get_current_event_time ());
637
638   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
639     gtk_widget_grab_focus (combo->entry);
640
641   gtk_grab_add (combo->popwin);
642 }
643
644 static gboolean
645 gtk_combo_popup_button_press (GtkWidget        *button,
646                               GdkEventButton   *event,
647                               GtkCombo         *combo)
648 {
649   if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
650     gtk_widget_grab_focus (combo->entry);
651
652   if (event->button != 1)
653     return FALSE;
654
655   if (!popup_grab_on_window (combo->button->window,
656                              gtk_get_current_event_time ()))
657     return FALSE;
658
659   combo->current_button = event->button;
660
661   gtk_combo_popup_list (combo);
662
663   /* This must succeed since we already have the grab */
664   popup_grab_on_window (combo->popwin->window,
665                         gtk_get_current_event_time ());
666
667   gtk_button_pressed (GTK_BUTTON (button));
668
669   gtk_grab_add (combo->popwin);
670
671   return TRUE;
672 }
673
674 static gboolean
675 gtk_combo_popup_button_leave (GtkWidget        *button,
676                               GdkEventCrossing *event,
677                               GtkCombo         *combo)
678 {
679   /* The idea here is that we want to keep the button down if the
680    * popup is popped up.
681    */
682   return combo->current_button != 0;
683 }
684
685 static void
686 gtk_combo_update_entry (GtkCombo * combo)
687 {
688   GtkList *list = GTK_LIST (combo->list);
689   char *text;
690
691   g_signal_handler_block (list, combo->list_change_id);
692   if (list->selection)
693     {
694       text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
695       if (!text)
696         text = "";
697       gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
698     }
699   g_signal_handler_unblock (list, combo->list_change_id);
700 }
701
702 static void
703 gtk_combo_selection_changed (GtkList  *list,
704                              GtkCombo *combo)
705 {
706   if (!GTK_WIDGET_VISIBLE (combo->popwin))
707     gtk_combo_update_entry (combo);
708 }
709
710 static void
711 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
712 {
713   GtkList *list = GTK_LIST (combo->list);
714   GList *slist = list->selection;
715   GtkListItem *li;
716
717   gtk_grab_remove (GTK_WIDGET (combo));
718
719   g_signal_handler_block (entry, combo->entry_change_id);
720   if (slist && slist->data)
721     gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
722   li = gtk_combo_find (combo);
723   if (li)
724     gtk_list_select_child (list, GTK_WIDGET (li));
725   g_signal_handler_unblock (entry, combo->entry_change_id);
726 }
727
728 static gint
729 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
730 {
731   GtkWidget *child;
732
733   child = gtk_get_event_widget (event);
734
735   /* We don't ask for button press events on the grab widget, so
736    *  if an event is reported directly to the grab widget, it must
737    *  be on a window outside the application (and thus we remove
738    *  the popup window). Otherwise, we check if the widget is a child
739    *  of the grab widget, and only remove the popup window if it
740    *  is not.
741    */
742   if (child != widget)
743     {
744       while (child)
745         {
746           if (child == widget)
747             return FALSE;
748           child = child->parent;
749         }
750     }
751
752   gtk_combo_popdown_list (combo);
753
754   return TRUE;
755 }
756
757 static gboolean
758 is_within (GtkWidget *widget,
759            GtkWidget *ancestor)
760 {
761   return widget == ancestor || gtk_widget_is_ancestor (widget, ancestor);
762 }
763
764 static void
765 gtk_combo_button_event_after (GtkWidget *widget,
766                               GdkEvent  *event,
767                               GtkCombo  *combo)
768 {
769   GtkWidget *child;
770
771   if (event->type != GDK_BUTTON_RELEASE)
772     return;
773   
774   child = gtk_get_event_widget ((GdkEvent*) event);
775
776   if ((combo->current_button != 0) && (event->button.button == 1))
777     {
778       /* This was the initial button press */
779
780       combo->current_button = 0;
781
782       /* Check to see if we released inside the button */
783       if (child && is_within (child, combo->button))
784         {
785           gtk_grab_add (combo->popwin);
786           gdk_pointer_grab (combo->popwin->window, TRUE,
787                             GDK_BUTTON_PRESS_MASK | 
788                             GDK_BUTTON_RELEASE_MASK |
789                             GDK_POINTER_MOTION_MASK, 
790                             NULL, NULL, GDK_CURRENT_TIME);
791           return;
792         }
793     }
794
795   if (is_within (child, combo->list))
796     gtk_combo_update_entry (combo);
797     
798   gtk_combo_popdown_list (combo);
799
800 }
801
802 static void
803 find_child_foreach (GtkWidget *widget,
804                     gpointer   data)
805 {
806   GdkEventButton *event = data;
807
808   if (!event->window)
809     {
810       if (event->x >= widget->allocation.x &&
811           event->x < widget->allocation.x + widget->allocation.width &&
812           event->y >= widget->allocation.y &&
813           event->y < widget->allocation.y + widget->allocation.height)
814         event->window = g_object_ref (widget->window);
815     }
816 }
817
818 static void
819 find_child_window (GtkContainer   *container,
820                    GdkEventButton *event)
821 {
822   gtk_container_foreach (container, find_child_foreach, event);
823 }
824
825 static gint         
826 gtk_combo_list_enter (GtkWidget        *widget,
827                       GdkEventCrossing *event,
828                       GtkCombo         *combo)
829 {
830   GtkWidget *event_widget;
831
832   event_widget = gtk_get_event_widget ((GdkEvent*) event);
833
834   if ((event_widget == combo->list) &&
835       (combo->current_button != 0) && 
836       (!GTK_WIDGET_HAS_GRAB (combo->list)))
837     {
838       GdkEvent *tmp_event = gdk_event_new (GDK_BUTTON_PRESS);
839       gint x, y;
840       GdkModifierType mask;
841
842       gtk_grab_remove (combo->popwin);
843
844       /* Transfer the grab over to the list by synthesizing
845        * a button press event
846        */
847       gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
848
849       tmp_event->button.send_event = TRUE;
850       tmp_event->button.time = GDK_CURRENT_TIME; /* bad */
851       tmp_event->button.x = x;
852       tmp_event->button.y = y;
853       /* We leave all the XInput fields unfilled here, in the expectation
854        * that GtkList doesn't care.
855        */
856       tmp_event->button.button = combo->current_button;
857       tmp_event->button.state = mask;
858
859       find_child_window (GTK_CONTAINER (combo->list), &tmp_event->button);
860       if (!tmp_event->button.window)
861         {
862           GtkWidget *child;
863           
864           if (GTK_LIST (combo->list)->children)
865             child = GTK_LIST (combo->list)->children->data;
866           else
867             child = combo->list;
868
869           tmp_event->button.window = g_object_ref (child->window);
870         }
871
872       gtk_widget_event (combo->list, tmp_event);
873       gdk_event_free (tmp_event);
874     }
875
876   return FALSE;
877 }
878
879 static int
880 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
881 {
882   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
883
884   if (event->keyval == GDK_Escape && state == 0)
885     {
886       if (GTK_WIDGET_HAS_GRAB (combo->list))
887         gtk_list_end_drag_selection (GTK_LIST (combo->list));
888
889       gtk_combo_popdown_list (combo);
890       
891       return TRUE;
892     }
893   return FALSE;
894 }
895
896 static void
897 combo_event_box_realize (GtkWidget *widget)
898 {
899   GdkCursor *cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
900                                                   GDK_TOP_LEFT_ARROW);
901   gdk_window_set_cursor (widget->window, cursor);
902   gdk_cursor_unref (cursor);
903 }
904
905 static void
906 gtk_combo_init (GtkCombo * combo)
907 {
908   GtkWidget *arrow;
909   GtkWidget *frame;
910   GtkWidget *event_box;
911
912   combo->case_sensitive = FALSE;
913   combo->value_in_list = FALSE;
914   combo->ok_if_empty = TRUE;
915   combo->use_arrows = TRUE;
916   combo->use_arrows_always = FALSE;
917   combo->entry = gtk_entry_new ();
918   combo->button = gtk_button_new ();
919   combo->current_button = 0;
920   arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
921   gtk_widget_show (arrow);
922   gtk_container_add (GTK_CONTAINER (combo->button), arrow);
923   gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
924   gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
925   GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
926   gtk_widget_show (combo->entry);
927   gtk_widget_show (combo->button);
928   combo->entry_change_id = g_signal_connect (combo->entry, "changed",
929                                              G_CALLBACK (gtk_combo_update_list),
930                                              combo);
931   g_signal_connect_after (combo->entry, "key_press_event",
932                           G_CALLBACK (gtk_combo_entry_key_press), combo);
933   g_signal_connect_after (combo->entry, "focus_out_event",
934                           G_CALLBACK (gtk_combo_entry_focus_out), combo);
935   combo->activate_id = g_signal_connect (combo->entry, "activate",
936                                          G_CALLBACK (gtk_combo_activate),
937                                          combo);
938   g_signal_connect (combo->button, "button_press_event",
939                     G_CALLBACK (gtk_combo_popup_button_press), combo);
940   g_signal_connect (combo->button, "leave_notify_event",
941                     G_CALLBACK (gtk_combo_popup_button_leave), combo);
942
943   combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
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( 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 (parent_class)->unrealize (widget);
1023 }
1024
1025 GType
1026 gtk_combo_get_type (void)
1027 {
1028   static GType combo_type = 0;
1029
1030   if (!combo_type)
1031     {
1032       static const GTypeInfo combo_info =
1033       {
1034         sizeof (GtkComboClass),
1035         NULL,           /* base_init */
1036         NULL,           /* base_finalize */
1037         (GClassInitFunc) gtk_combo_class_init,
1038         NULL,           /* class_finalize */
1039         NULL,           /* class_data */
1040         sizeof (GtkCombo),
1041         0,              /* n_preallocs */
1042         (GInstanceInitFunc) gtk_combo_init,
1043       };
1044
1045       combo_type = g_type_register_static (GTK_TYPE_HBOX, g_intern_static_string ("GtkCombo"),
1046                                            &combo_info, 0);
1047     }
1048
1049   return combo_type;
1050 }
1051
1052 GtkWidget*
1053 gtk_combo_new (void)
1054 {
1055   return g_object_new (GTK_TYPE_COMBO, NULL);
1056 }
1057
1058 void
1059 gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
1060 {
1061   g_return_if_fail (GTK_IS_COMBO (combo));
1062   val = val != FALSE;
1063   ok_if_empty = ok_if_empty != FALSE;
1064
1065   g_object_freeze_notify (G_OBJECT (combo));
1066   if (combo->value_in_list != val)
1067     {
1068        combo->value_in_list = val;
1069   g_object_notify (G_OBJECT (combo), "value_in_list");
1070     }
1071   if (combo->ok_if_empty != ok_if_empty)
1072     {
1073        combo->ok_if_empty = ok_if_empty;
1074   g_object_notify (G_OBJECT (combo), "allow_empty");
1075     }
1076   g_object_thaw_notify (G_OBJECT (combo));
1077 }
1078
1079 void
1080 gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
1081 {
1082   g_return_if_fail (GTK_IS_COMBO (combo));
1083   val = val != FALSE;
1084
1085   if (combo->case_sensitive != val) 
1086     {
1087   combo->case_sensitive = val;
1088   g_object_notify (G_OBJECT (combo), "case_sensitive");
1089     }
1090 }
1091
1092 void
1093 gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
1094 {
1095   g_return_if_fail (GTK_IS_COMBO (combo));
1096   val = val != FALSE;
1097
1098   if (combo->use_arrows != val) 
1099     {
1100   combo->use_arrows = val;
1101   g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1102     }
1103 }
1104
1105 void
1106 gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
1107 {
1108   g_return_if_fail (GTK_IS_COMBO (combo));
1109   val = val != FALSE;
1110
1111   if (combo->use_arrows_always != val) 
1112     {
1113        g_object_freeze_notify (G_OBJECT (combo));
1114   combo->use_arrows_always = val;
1115        g_object_notify (G_OBJECT (combo), "enable_arrows_always");
1116
1117        if (combo->use_arrows != TRUE) 
1118          {
1119   combo->use_arrows = TRUE;
1120   g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1121          }
1122   g_object_thaw_notify (G_OBJECT (combo));
1123     }
1124 }
1125
1126 void
1127 gtk_combo_set_popdown_strings (GtkCombo *combo, 
1128                                GList    *strings)
1129 {
1130   GList *list;
1131   GtkWidget *li;
1132
1133   g_return_if_fail (GTK_IS_COMBO (combo));
1134
1135   gtk_combo_popdown_list (combo);
1136
1137   gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
1138   list = strings;
1139   while (list)
1140     {
1141       li = gtk_list_item_new_with_label ((gchar *) list->data);
1142       gtk_widget_show (li);
1143       gtk_container_add (GTK_CONTAINER (combo->list), li);
1144       list = list->next;
1145     }
1146 }
1147
1148 void
1149 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
1150 {
1151   g_return_if_fail (GTK_IS_COMBO (combo));
1152   g_return_if_fail (item != NULL);
1153
1154   g_object_set_data_full (G_OBJECT (item), g_intern_static_string (gtk_combo_string_key),
1155                           g_strdup (item_value), g_free);
1156 }
1157
1158 static void
1159 gtk_combo_size_allocate (GtkWidget     *widget,
1160                          GtkAllocation *allocation)
1161 {
1162   GtkCombo *combo;
1163
1164   g_return_if_fail (GTK_IS_COMBO (widget));
1165   g_return_if_fail (allocation != NULL);
1166
1167   GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
1168   
1169   combo = GTK_COMBO (widget);
1170
1171   if (combo->entry->allocation.height > combo->entry->requisition.height)
1172     {
1173       GtkAllocation button_allocation;
1174
1175       button_allocation = combo->button->allocation;
1176       button_allocation.height = combo->entry->requisition.height;
1177       button_allocation.y = combo->entry->allocation.y + 
1178         (combo->entry->allocation.height - combo->entry->requisition.height) 
1179         / 2;
1180       gtk_widget_size_allocate (combo->button, &button_allocation);
1181     }
1182 }
1183
1184 void
1185 gtk_combo_disable_activate (GtkCombo* combo)
1186 {
1187   g_return_if_fail (GTK_IS_COMBO (combo));
1188
1189   if ( combo->activate_id ) {
1190     g_signal_handler_disconnect (combo->entry, combo->activate_id);
1191     combo->activate_id = 0;
1192   }
1193 }
1194
1195 static void
1196 gtk_combo_set_property (GObject         *object,
1197                         guint            prop_id,
1198                         const GValue    *value,
1199                         GParamSpec      *pspec)
1200 {
1201   GtkCombo *combo = GTK_COMBO (object);
1202   
1203   switch (prop_id)
1204     {
1205     case PROP_ENABLE_ARROW_KEYS:
1206       gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
1207       break;
1208     case PROP_ENABLE_ARROWS_ALWAYS:
1209       gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
1210       break;
1211     case PROP_CASE_SENSITIVE:
1212       gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
1213       break;
1214     case PROP_ALLOW_EMPTY:
1215       combo->ok_if_empty = g_value_get_boolean (value);
1216       break;
1217     case PROP_VALUE_IN_LIST:
1218       combo->value_in_list = g_value_get_boolean (value);
1219       break;
1220     default:
1221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1222       break;
1223     }
1224   
1225 }
1226
1227 static void
1228 gtk_combo_get_property (GObject         *object,
1229                         guint            prop_id,
1230                         GValue          *value,
1231                         GParamSpec      *pspec)
1232 {
1233   GtkCombo *combo = GTK_COMBO (object);
1234   
1235   switch (prop_id)
1236     {
1237     case PROP_ENABLE_ARROW_KEYS:
1238       g_value_set_boolean (value, combo->use_arrows);
1239       break;
1240     case PROP_ENABLE_ARROWS_ALWAYS:
1241       g_value_set_boolean (value, combo->use_arrows_always);
1242       break;
1243     case PROP_CASE_SENSITIVE:
1244       g_value_set_boolean (value, combo->case_sensitive);
1245       break;
1246     case PROP_ALLOW_EMPTY:
1247       g_value_set_boolean (value, combo->ok_if_empty);
1248       break;
1249     case PROP_VALUE_IN_LIST:
1250       g_value_set_boolean (value, combo->value_in_list);
1251       break;
1252     default:
1253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1254       break;
1255     }
1256    
1257 }
1258
1259 #define __GTK_SMART_COMBO_C__
1260 #include "gtkaliasdef.c"