1 /* gtkcombo - combo widget for gtk+
2 * Copyright 1997 Paolo Molaro
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.
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.
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.
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/.
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+
37 #include "gtkeventbox.h"
38 #include "gtkbutton.h"
39 #include "gtklistitem.h"
40 #include "gtkscrolledwindow.h"
42 #include "gtkwindow.h"
43 #include "gdk/gdkkeysyms.h"
48 const gchar *gtk_combo_string_key = "gtk-combo-string-value";
50 #define COMBO_LIST_MAX_HEIGHT (400)
51 #define EMPTY_LIST_HEIGHT (15)
55 PROP_ENABLE_ARROW_KEYS,
56 PROP_ENABLE_ARROWS_ALWAYS,
62 static void gtk_combo_class_init (GtkComboClass *klass);
63 static void gtk_combo_init (GtkCombo *combo);
64 static void gtk_combo_realize (GtkWidget *widget);
65 static void gtk_combo_unrealize (GtkWidget *widget);
66 static void gtk_combo_destroy (GtkObject *combo);
67 static GtkListItem *gtk_combo_find (GtkCombo *combo);
68 static gchar * gtk_combo_func (GtkListItem *li);
69 static gint gtk_combo_focus_idle (GtkCombo *combo);
70 static gint gtk_combo_entry_focus_out (GtkEntry *entry,
73 static void gtk_combo_get_pos (GtkCombo *combo,
78 static void gtk_combo_popup_list (GtkCombo *combo);
79 static void gtk_combo_activate (GtkWidget *widget,
81 static gboolean gtk_combo_popup_button_press (GtkWidget *button,
82 GdkEventButton *event,
84 static gboolean gtk_combo_popup_button_leave (GtkWidget *button,
85 GdkEventCrossing *event,
87 static void gtk_combo_update_entry (GtkList *list,
89 static void gtk_combo_update_list (GtkEntry *entry,
91 static gint gtk_combo_button_press (GtkWidget *widget,
94 static void gtk_combo_button_event_after (GtkWidget *widget,
97 static gint gtk_combo_list_enter (GtkWidget *widget,
98 GdkEventCrossing *event,
100 static gint gtk_combo_list_key_press (GtkWidget *widget,
103 static gint gtk_combo_entry_key_press (GtkEntry *widget,
106 static gint gtk_combo_window_key_press (GtkWidget *window,
109 static void gtk_combo_size_allocate (GtkWidget *widget,
110 GtkAllocation *allocation);
111 static void gtk_combo_set_property (GObject *object,
115 static void gtk_combo_get_property (GObject *object,
119 static GtkHBoxClass *parent_class = NULL;
122 gtk_combo_class_init (GtkComboClass * klass)
124 GObjectClass *gobject_class;
125 GtkObjectClass *oclass;
126 GtkWidgetClass *widget_class;
128 gobject_class = (GObjectClass *) klass;
129 oclass = (GtkObjectClass *) klass;
130 widget_class = (GtkWidgetClass *) klass;
132 parent_class = g_type_class_peek_parent (klass);
134 gobject_class->set_property = gtk_combo_set_property;
135 gobject_class->get_property = gtk_combo_get_property;
137 g_object_class_install_property (gobject_class,
138 PROP_ENABLE_ARROW_KEYS,
139 g_param_spec_boolean ("enable_arrow_keys",
140 _("Enable arrow keys"),
141 _("Whether the arrow keys move through the list of items"),
143 G_PARAM_READABLE | G_PARAM_WRITABLE));
144 g_object_class_install_property (gobject_class,
145 PROP_ENABLE_ARROWS_ALWAYS,
146 g_param_spec_boolean ("enable_arrows_always",
147 _("Always enable arrows"),
148 _("Whether the arrow keys work, even if the entry contents are not in the list"),
150 G_PARAM_READABLE | G_PARAM_WRITABLE));
151 g_object_class_install_property (gobject_class,
153 g_param_spec_boolean ("case_sensitive",
155 _("Whether list item matching is case sensitive"),
157 G_PARAM_READABLE | G_PARAM_WRITABLE));
159 g_object_class_install_property (gobject_class,
161 g_param_spec_boolean ("allow_empty",
163 _("Whether an empty value may be entered in this field"),
165 G_PARAM_READABLE | G_PARAM_WRITABLE));
167 g_object_class_install_property (gobject_class,
169 g_param_spec_boolean ("value_in_list",
171 _("Whether entered values must already be present in the list"),
173 G_PARAM_READABLE | G_PARAM_WRITABLE));
176 oclass->destroy = gtk_combo_destroy;
178 widget_class->size_allocate = gtk_combo_size_allocate;
179 widget_class->realize = gtk_combo_realize;
180 widget_class->unrealize = gtk_combo_unrealize;
184 gtk_combo_destroy (GtkObject *object)
186 GtkCombo *combo = GTK_COMBO (object);
190 gtk_widget_destroy (combo->popwin);
191 g_object_unref (combo->popwin);
192 combo->popwin = NULL;
195 GTK_OBJECT_CLASS (parent_class)->destroy (object);
199 gtk_combo_entry_key_press (GtkEntry * entry, GdkEventKey * event, GtkCombo * combo)
204 if ((event->keyval == GDK_Tab ||
205 event->keyval == GDK_KP_Tab) &&
206 (event->state & GDK_MOD1_MASK))
208 GtkEditable *editable = GTK_EDITABLE (entry);
211 gchar* nprefix = NULL;
214 if ( !GTK_LIST (combo->list)->children )
217 g_signal_stop_emission_by_name (entry, "key_press_event");
219 cmpl = g_completion_new ((GCompletionFunc)gtk_combo_func);
220 g_completion_add_items (cmpl, GTK_LIST (combo->list)->children);
222 pos = gtk_editable_get_position (editable);
223 prefix = gtk_editable_get_chars (editable, 0, pos);
225 g_completion_complete (cmpl, prefix, &nprefix);
227 if (nprefix && strlen (nprefix) > strlen (prefix))
229 gtk_editable_insert_text (editable, nprefix + pos,
230 strlen (nprefix) - strlen (prefix), &pos);
231 gtk_editable_set_position (editable, pos);
237 g_completion_free (cmpl);
242 if (!combo->use_arrows || !GTK_LIST (combo->list)->children)
245 li = g_list_find (GTK_LIST (combo->list)->children, gtk_combo_find (combo));
247 if ((event->keyval == GDK_Up)
248 || (event->keyval == GDK_KP_Up)
249 || ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'p') || (event->keyval == 'P'))))
253 if (!li && combo->use_arrows_always)
255 li = g_list_last (GTK_LIST (combo->list)->children);
259 gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
260 g_signal_stop_emission_by_name (entry, "key_press_event");
264 else if ((event->keyval == GDK_Down)
265 || (event->keyval == GDK_KP_Down)
266 || ((event->state & GDK_MOD1_MASK) && ((event->keyval == 'n') || (event->keyval == 'N'))))
270 if (!li && combo->use_arrows_always)
272 li = GTK_LIST (combo->list)->children;
276 gtk_list_select_child (GTK_LIST (combo->list), GTK_WIDGET (li->data));
277 g_signal_stop_emission_by_name (entry, "key_press_event");
285 gtk_combo_window_key_press (GtkWidget *window,
289 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)
291 if (GTK_WIDGET_VISIBLE (combo->popwin))
293 gtk_widget_hide (combo->popwin);
295 if (GTK_WIDGET_HAS_GRAB (combo->popwin))
297 gtk_grab_remove (combo->popwin);
298 gdk_display_pointer_ungrab (gtk_widget_get_display (window),
300 gdk_display_keyboard_ungrab (gtk_widget_get_display (window),
305 g_signal_stop_emission_by_name (window, "key_press_event");
314 gtk_combo_find (GtkCombo * combo)
317 GtkListItem *found = NULL;
322 text = gtk_entry_get_text (GTK_ENTRY (combo->entry));
323 if (combo->case_sensitive)
324 compare_text = (gchar *)text;
326 compare_text = g_utf8_casefold (text, -1);
328 for (clist = GTK_LIST (combo->list)->children;
332 ltext = gtk_combo_func (GTK_LIST_ITEM (clist->data));
336 if (!combo->case_sensitive)
337 ltext = g_utf8_casefold (ltext, -1);
339 if (strcmp (ltext, compare_text) == 0)
342 if (!combo->case_sensitive)
346 if (!combo->case_sensitive)
347 g_free (compare_text);
353 gtk_combo_func (GtkListItem * li)
358 ltext = g_object_get_data (G_OBJECT (li), gtk_combo_string_key);
361 label = GTK_BIN (li)->child;
362 if (!label || !GTK_IS_LABEL (label))
364 ltext = (gchar *) gtk_label_get_text (GTK_LABEL (label));
370 gtk_combo_focus_idle (GtkCombo * combo)
374 GDK_THREADS_ENTER ();
375 gtk_widget_grab_focus (combo->entry);
376 GDK_THREADS_LEAVE ();
382 gtk_combo_entry_focus_out (GtkEntry * entry, GdkEventFocus * event, GtkCombo * combo)
385 if (combo->value_in_list && !gtk_combo_find (combo))
389 /* gdk_beep(); *//* this can be annoying */
390 if (combo->ok_if_empty && !strcmp (gtk_entry_get_text (entry), ""))
393 printf ("INVALID ENTRY: `%s'\n", gtk_entry_get_text (entry));
395 gtk_grab_add (GTK_WIDGET (combo));
396 /* this is needed because if we call gtk_widget_grab_focus()
397 it isn't guaranteed it's the *last* call before the main-loop,
398 so the focus can be lost anyway...
399 the signal_stop_emission doesn't seem to work either...
401 focus_idle = g_idle_source_new ();
402 g_source_set_closure (focus_idle,
403 g_cclosure_new_object (G_CALLBACK (gtk_combo_focus_idle),
405 g_source_attach (focus_idle, NULL);
407 /*g_signal_stop_emission_by_name (entry, "focus_out_event"); */
414 gtk_combo_get_pos (GtkCombo * combo, gint * x, gint * y, gint * height, gint * width)
418 GtkScrolledWindow *popup;
421 GtkRequisition list_requisition;
422 gboolean show_hscroll = FALSE;
423 gboolean show_vscroll = FALSE;
430 gint scrollbar_spacing;
432 widget = GTK_WIDGET (combo);
433 popup = GTK_SCROLLED_WINDOW (combo->popup);
434 popwin = GTK_BIN (combo->popwin);
436 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (popup);
438 gdk_window_get_origin (combo->entry->window, x, y);
439 real_height = MIN (combo->entry->requisition.height,
440 combo->entry->allocation.height);
442 avail_height = gdk_screen_get_height (gtk_widget_get_screen (widget)) - *y;
444 gtk_widget_size_request (combo->list, &list_requisition);
445 min_height = MIN (list_requisition.height,
446 popup->vscrollbar->requisition.height);
447 if (!GTK_LIST (combo->list)->children)
448 list_requisition.height += EMPTY_LIST_HEIGHT;
450 alloc_width = (widget->allocation.width -
451 2 * popwin->child->style->xthickness -
452 2 * GTK_CONTAINER (popwin->child)->border_width -
453 2 * GTK_CONTAINER (combo->popup)->border_width -
454 2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width -
455 2 * GTK_BIN (popup)->child->style->xthickness);
457 work_height = (2 * popwin->child->style->ythickness +
458 2 * GTK_CONTAINER (popwin->child)->border_width +
459 2 * GTK_CONTAINER (combo->popup)->border_width +
460 2 * GTK_CONTAINER (GTK_BIN (popup)->child)->border_width +
461 2 * GTK_BIN (popup)->child->style->ythickness);
465 old_width = alloc_width;
466 old_height = work_height;
469 alloc_width < list_requisition.width)
471 GtkRequisition requisition;
473 gtk_widget_size_request (popup->hscrollbar, &requisition);
474 work_height += (requisition.height + scrollbar_spacing);
479 work_height + list_requisition.height > avail_height)
481 GtkRequisition requisition;
483 if (work_height + min_height > avail_height &&
484 *y - real_height > avail_height)
486 *y -= (work_height + list_requisition.height + real_height);
489 gtk_widget_size_request (popup->hscrollbar, &requisition);
490 alloc_width -= (requisition.width + scrollbar_spacing);
493 } while (old_width != alloc_width || old_height != work_height);
495 *width = widget->allocation.width;
497 *height = avail_height;
499 *height = work_height + list_requisition.height;
506 gtk_combo_popup_list (GtkCombo * combo)
509 gint height, width, x, y;
510 gint old_width, old_height;
512 old_width = combo->popwin->allocation.width;
513 old_height = combo->popwin->allocation.height;
515 gtk_combo_get_pos (combo, &x, &y, &height, &width);
517 /* workaround for gtk_scrolled_window_size_allocate bug */
518 if (old_width != width || old_height != height)
520 gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar);
521 gtk_widget_hide (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar);
524 gtk_combo_update_list (GTK_ENTRY (combo->entry), combo);
526 /* We need to make sure some child of combo->popwin
527 * is focused to disable GtkWindow's automatic
528 * "focus-the-first-item" code. If there is no selected
529 * child, we focus the list itself with some hackery.
531 list = GTK_LIST (combo->list);
535 gtk_widget_grab_focus (list->selection->data);
539 GTK_WIDGET_SET_FLAGS (list, GTK_CAN_FOCUS);
540 gtk_widget_grab_focus (combo->list);
541 GTK_WIDGET_UNSET_FLAGS (list, GTK_CAN_FOCUS);
544 gtk_window_move (GTK_WINDOW (combo->popwin), x, y);
545 gtk_widget_set_size_request (combo->popwin, width, height);
546 gtk_widget_show (combo->popwin);
548 gtk_widget_grab_focus (combo->popwin);
552 gtk_combo_popdown_list (GtkCombo *combo)
554 combo->current_button = 0;
556 if (GTK_BUTTON (combo->button)->in_button)
558 GTK_BUTTON (combo->button)->in_button = FALSE;
559 gtk_button_released (GTK_BUTTON (combo->button));
562 if (GTK_WIDGET_HAS_GRAB (combo->popwin))
564 gtk_grab_remove (combo->popwin);
565 gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
566 gtk_get_current_event_time ());
567 gdk_display_keyboard_ungrab (gtk_widget_get_display (GTK_WIDGET (combo)),
568 gtk_get_current_event_time ());
571 gtk_widget_hide (combo->popwin);
575 popup_grab_on_window (GdkWindow *window,
576 guint32 activate_time)
578 if ((gdk_pointer_grab (window, TRUE,
579 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
580 GDK_POINTER_MOTION_MASK,
581 NULL, NULL, activate_time) == 0))
583 if (gdk_keyboard_grab (window, TRUE,
588 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
598 gtk_combo_activate (GtkWidget *widget,
601 if (!combo->button->window ||
602 !popup_grab_on_window (combo->button->window,
603 gtk_get_current_event_time ()))
606 gtk_combo_popup_list (combo);
608 /* This must succeed since we already have the grab */
609 popup_grab_on_window (combo->popwin->window,
610 gtk_get_current_event_time ());
612 if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
613 gtk_widget_grab_focus (combo->entry);
615 gtk_grab_add (combo->popwin);
619 gtk_combo_popup_button_press (GtkWidget *button,
620 GdkEventButton *event,
623 if (!GTK_WIDGET_HAS_FOCUS (combo->entry))
624 gtk_widget_grab_focus (combo->entry);
626 if (event->button != 1)
629 if (!popup_grab_on_window (combo->button->window,
630 gtk_get_current_event_time ()))
633 combo->current_button = event->button;
635 gtk_combo_popup_list (combo);
637 /* This must succeed since we already have the grab */
638 popup_grab_on_window (combo->popwin->window,
639 gtk_get_current_event_time ());
641 gtk_button_pressed (GTK_BUTTON (button));
643 gtk_grab_add (combo->popwin);
644 GTK_LIST (combo->list)->drag_selection = TRUE;
645 gtk_grab_add (combo->list);
651 gtk_combo_popup_button_leave (GtkWidget *button,
652 GdkEventCrossing *event,
655 /* The idea here is that we want to keep the button down if the
656 * popup is popped up.
658 return combo->current_button != 0;
662 gtk_combo_update_entry (GtkList * list, GtkCombo * combo)
666 gtk_grab_remove (GTK_WIDGET (combo));
667 g_signal_handler_block (list, combo->list_change_id);
670 text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
673 gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
675 g_signal_handler_unblock (list, combo->list_change_id);
679 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
681 GtkList *list = GTK_LIST (combo->list);
682 GList *slist = list->selection;
685 gtk_grab_remove (GTK_WIDGET (combo));
687 g_signal_handler_block (entry, combo->entry_change_id);
688 if (slist && slist->data)
689 gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
690 li = gtk_combo_find (combo);
692 gtk_list_select_child (list, GTK_WIDGET (li));
693 g_signal_handler_unblock (entry, combo->entry_change_id);
697 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
701 child = gtk_get_event_widget (event);
703 /* We don't ask for button press events on the grab widget, so
704 * if an event is reported directly to the grab widget, it must
705 * be on a window outside the application (and thus we remove
706 * the popup window). Otherwise, we check if the widget is a child
707 * of the grab widget, and only remove the popup window if it
716 child = child->parent;
720 gtk_combo_popdown_list (combo);
726 gtk_combo_button_event_after (GtkWidget *widget,
732 if (event->type != GDK_BUTTON_RELEASE)
735 if ((combo->current_button != 0) && (event->button.button == 1))
737 /* This was the initial button press */
739 combo->current_button = 0;
741 /* Check to see if we released inside the button */
742 child = gtk_get_event_widget ((GdkEvent*) event);
744 while (child && child != (combo->button))
745 child = child->parent;
747 if (child == combo->button)
749 gtk_grab_add (combo->popwin);
750 gdk_pointer_grab (combo->popwin->window, TRUE,
751 GDK_BUTTON_PRESS_MASK |
752 GDK_BUTTON_RELEASE_MASK |
753 GDK_POINTER_MOTION_MASK,
754 NULL, NULL, GDK_CURRENT_TIME);
759 gtk_combo_popdown_list (combo);
763 gtk_combo_list_enter (GtkWidget *widget,
764 GdkEventCrossing *event,
767 GtkWidget *event_widget;
769 event_widget = gtk_get_event_widget ((GdkEvent*) event);
771 if ((event_widget == combo->list) &&
772 (combo->current_button != 0) &&
773 (!GTK_WIDGET_HAS_GRAB (combo->list)))
775 GdkEvent *tmp_event = gdk_event_new (GDK_BUTTON_PRESS);
777 GdkModifierType mask;
779 gtk_grab_remove (combo->popwin);
781 /* Transfer the grab over to the list by synthesizing
782 * a button press event
784 gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
786 tmp_event->button.window = g_object_ref (combo->list->window);
787 tmp_event->button.send_event = TRUE;
788 tmp_event->button.time = GDK_CURRENT_TIME; /* bad */
789 tmp_event->button.x = x;
790 tmp_event->button.y = y;
791 /* We leave all the XInput fields unfilled here, in the expectation
792 * that GtkList doesn't care.
794 tmp_event->button.button = combo->current_button;
795 tmp_event->button.state = mask;
797 gtk_widget_event (combo->list, tmp_event);
798 gdk_event_free (tmp_event);
805 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
807 if (event->keyval == GDK_Escape)
809 if (GTK_WIDGET_HAS_GRAB (combo->list))
810 gtk_list_end_drag_selection (GTK_LIST (combo->list));
812 gtk_combo_popdown_list (combo);
820 combo_event_box_realize (GtkWidget *widget)
822 GdkCursor *cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
824 gdk_window_set_cursor (widget->window, cursor);
825 gdk_cursor_unref (cursor);
829 gtk_combo_init (GtkCombo * combo)
833 GtkWidget *event_box;
835 combo->case_sensitive = FALSE;
836 combo->value_in_list = FALSE;
837 combo->ok_if_empty = TRUE;
838 combo->use_arrows = TRUE;
839 combo->use_arrows_always = FALSE;
840 combo->entry = gtk_entry_new ();
841 combo->button = gtk_button_new ();
842 combo->current_button = 0;
843 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
844 gtk_widget_show (arrow);
845 gtk_container_add (GTK_CONTAINER (combo->button), arrow);
846 gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
847 gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
848 GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
849 gtk_widget_show (combo->entry);
850 gtk_widget_show (combo->button);
851 combo->entry_change_id = g_signal_connect (combo->entry, "changed",
852 G_CALLBACK (gtk_combo_update_list),
854 g_signal_connect (combo->entry, "key_press_event",
855 G_CALLBACK (gtk_combo_entry_key_press), combo);
856 g_signal_connect_after (combo->entry, "focus_out_event",
857 G_CALLBACK (gtk_combo_entry_focus_out), combo);
858 combo->activate_id = g_signal_connect (combo->entry, "activate",
859 G_CALLBACK (gtk_combo_activate),
861 g_signal_connect (combo->button, "button_press_event",
862 G_CALLBACK (gtk_combo_popup_button_press), combo);
863 g_signal_connect (combo->button, "leave_notify_event",
864 G_CALLBACK (gtk_combo_popup_button_leave), combo);
866 combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
867 g_object_ref (combo->popwin);
868 gtk_window_set_resizable (GTK_WINDOW (combo->popwin), FALSE);
870 g_signal_connect (combo->popwin, "key_press_event",
871 G_CALLBACK (gtk_combo_window_key_press), combo);
873 gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
875 event_box = gtk_event_box_new ();
876 gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
877 g_signal_connect (event_box, "realize",
878 G_CALLBACK (combo_event_box_realize), NULL);
879 gtk_widget_show (event_box);
882 frame = gtk_frame_new (NULL);
883 gtk_container_add (GTK_CONTAINER (event_box), frame);
884 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
885 gtk_widget_show (frame);
887 combo->popup = gtk_scrolled_window_new (NULL, NULL);
888 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
889 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
890 GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
891 GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
892 gtk_container_add (GTK_CONTAINER (frame), combo->popup);
893 gtk_widget_show (combo->popup);
895 combo->list = gtk_list_new ();
896 /* We'll use enter notify events to figure out when to transfer
897 * the grab to the list
899 gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
901 gtk_list_set_selection_mode (GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
902 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (combo->popup), combo->list);
903 gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
904 gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
905 gtk_container_set_focus_hadjustment (GTK_CONTAINER (combo->list),
906 gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
907 gtk_widget_show (combo->list);
909 combo->list_change_id = g_signal_connect (combo->list,
911 G_CALLBACK (gtk_combo_update_entry),
913 g_signal_connect (combo->popwin, "key_press_event",
914 G_CALLBACK (gtk_combo_list_key_press), combo);
915 g_signal_connect (combo->popwin, "button_press_event",
916 G_CALLBACK (gtk_combo_button_press), combo);
918 g_signal_connect (combo->list, "event_after",
919 G_CALLBACK (gtk_combo_button_event_after), combo);
921 /* We connect here on the button, because we'll have a grab on it
922 * when the event occurs. But we are actually interested in enters
923 * for the combo->list.
925 g_signal_connect (combo->button, "enter_notify_event",
926 G_CALLBACK (gtk_combo_list_enter), combo);
930 gtk_combo_realize (GtkWidget *widget)
932 GtkCombo *combo = GTK_COMBO (widget);
934 gtk_window_set_screen (GTK_WINDOW (combo->popwin),
935 gtk_widget_get_screen (widget));
937 GTK_WIDGET_CLASS( parent_class )->realize (widget);
941 gtk_combo_unrealize (GtkWidget *widget)
943 GtkCombo *combo = GTK_COMBO (widget);
945 gtk_combo_popdown_list (combo);
946 gtk_widget_unrealize (combo->popwin);
948 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
952 gtk_combo_get_type (void)
954 static GType combo_type = 0;
958 static const GTypeInfo combo_info =
960 sizeof (GtkComboClass),
961 NULL, /* base_init */
962 NULL, /* base_finalize */
963 (GClassInitFunc) gtk_combo_class_init,
964 NULL, /* class_finalize */
965 NULL, /* class_data */
968 (GInstanceInitFunc) gtk_combo_init,
971 combo_type = g_type_register_static (GTK_TYPE_HBOX, "GtkCombo",
981 return g_object_new (GTK_TYPE_COMBO, NULL);
985 gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
987 g_return_if_fail (GTK_IS_COMBO (combo));
989 ok_if_empty = ok_if_empty != FALSE;
991 g_object_freeze_notify (G_OBJECT (combo));
992 if (combo->value_in_list != val)
994 combo->value_in_list = val;
995 g_object_notify (G_OBJECT (combo), "value_in_list");
997 if (combo->ok_if_empty != ok_if_empty)
999 combo->ok_if_empty = ok_if_empty;
1000 g_object_notify (G_OBJECT (combo), "allow_empty");
1002 g_object_thaw_notify (G_OBJECT (combo));
1006 gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
1008 g_return_if_fail (GTK_IS_COMBO (combo));
1011 if (combo->case_sensitive != val)
1013 combo->case_sensitive = val;
1014 g_object_notify (G_OBJECT (combo), "case_sensitive");
1019 gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
1021 g_return_if_fail (GTK_IS_COMBO (combo));
1024 if (combo->use_arrows != val)
1026 combo->use_arrows = val;
1027 g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1032 gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
1034 g_return_if_fail (GTK_IS_COMBO (combo));
1037 if (combo->use_arrows_always != val)
1039 g_object_freeze_notify (G_OBJECT (combo));
1040 combo->use_arrows_always = val;
1041 g_object_notify (G_OBJECT (combo), "enable_arrows_always");
1043 if (combo->use_arrows != TRUE)
1045 combo->use_arrows = TRUE;
1046 g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1048 g_object_thaw_notify (G_OBJECT (combo));
1053 gtk_combo_set_popdown_strings (GtkCombo * combo, GList * strings)
1058 g_return_if_fail (GTK_IS_COMBO (combo));
1059 g_return_if_fail (strings != NULL);
1061 gtk_combo_popdown_list (combo);
1063 gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
1067 li = gtk_list_item_new_with_label ((gchar *) list->data);
1068 gtk_widget_show (li);
1069 gtk_container_add (GTK_CONTAINER (combo->list), li);
1075 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
1077 g_return_if_fail (GTK_IS_COMBO (combo));
1078 g_return_if_fail (item != NULL);
1080 g_object_set_data_full (G_OBJECT (item), gtk_combo_string_key,
1081 g_strdup (item_value), g_free);
1085 gtk_combo_size_allocate (GtkWidget *widget,
1086 GtkAllocation *allocation)
1090 g_return_if_fail (GTK_IS_COMBO (widget));
1091 g_return_if_fail (allocation != NULL);
1093 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
1095 combo = GTK_COMBO (widget);
1097 if (combo->entry->allocation.height > combo->entry->requisition.height)
1099 GtkAllocation button_allocation;
1101 button_allocation = combo->button->allocation;
1102 button_allocation.height = combo->entry->requisition.height;
1103 button_allocation.y = combo->entry->allocation.y +
1104 (combo->entry->allocation.height - combo->entry->requisition.height)
1106 gtk_widget_size_allocate (combo->button, &button_allocation);
1111 gtk_combo_disable_activate (GtkCombo* combo)
1113 g_return_if_fail (GTK_IS_COMBO (combo));
1115 if ( combo->activate_id ) {
1116 g_signal_handler_disconnect (combo->entry, combo->activate_id);
1117 combo->activate_id = 0;
1122 gtk_combo_set_property (GObject *object,
1124 const GValue *value,
1127 GtkCombo *combo = GTK_COMBO (object);
1131 case PROP_ENABLE_ARROW_KEYS:
1132 gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
1134 case PROP_ENABLE_ARROWS_ALWAYS:
1135 gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
1137 case PROP_CASE_SENSITIVE:
1138 gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
1140 case PROP_ALLOW_EMPTY:
1141 combo->ok_if_empty = g_value_get_boolean (value);
1143 case PROP_VALUE_IN_LIST:
1144 combo->value_in_list = g_value_get_boolean (value);
1147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1154 gtk_combo_get_property (GObject *object,
1159 GtkCombo *combo = GTK_COMBO (object);
1163 case PROP_ENABLE_ARROW_KEYS:
1164 g_value_set_boolean (value, combo->use_arrows);
1166 case PROP_ENABLE_ARROWS_ALWAYS:
1167 g_value_set_boolean (value, combo->use_arrows_always);
1169 case PROP_CASE_SENSITIVE:
1170 g_value_set_boolean (value, combo->case_sensitive);
1172 case PROP_ALLOW_EMPTY:
1173 g_value_set_boolean (value, combo->ok_if_empty);
1175 case PROP_VALUE_IN_LIST:
1176 g_value_set_boolean (value, combo->value_in_list);
1179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);