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);
649 gtk_combo_popup_button_leave (GtkWidget *button,
650 GdkEventCrossing *event,
653 /* The idea here is that we want to keep the button down if the
654 * popup is popped up.
656 return combo->current_button != 0;
660 gtk_combo_update_entry (GtkList * list, GtkCombo * combo)
664 gtk_grab_remove (GTK_WIDGET (combo));
665 g_signal_handler_block (list, combo->list_change_id);
668 text = gtk_combo_func (GTK_LIST_ITEM (list->selection->data));
671 gtk_entry_set_text (GTK_ENTRY (combo->entry), text);
673 g_signal_handler_unblock (list, combo->list_change_id);
677 gtk_combo_update_list (GtkEntry * entry, GtkCombo * combo)
679 GtkList *list = GTK_LIST (combo->list);
680 GList *slist = list->selection;
683 gtk_grab_remove (GTK_WIDGET (combo));
685 g_signal_handler_block (entry, combo->entry_change_id);
686 if (slist && slist->data)
687 gtk_list_unselect_child (list, GTK_WIDGET (slist->data));
688 li = gtk_combo_find (combo);
690 gtk_list_select_child (list, GTK_WIDGET (li));
691 g_signal_handler_unblock (entry, combo->entry_change_id);
695 gtk_combo_button_press (GtkWidget * widget, GdkEvent * event, GtkCombo * combo)
699 child = gtk_get_event_widget (event);
701 /* We don't ask for button press events on the grab widget, so
702 * if an event is reported directly to the grab widget, it must
703 * be on a window outside the application (and thus we remove
704 * the popup window). Otherwise, we check if the widget is a child
705 * of the grab widget, and only remove the popup window if it
714 child = child->parent;
718 gtk_combo_popdown_list (combo);
724 gtk_combo_button_event_after (GtkWidget *widget,
730 if (event->type != GDK_BUTTON_RELEASE)
733 if ((combo->current_button != 0) && (event->button.button == 1))
735 /* This was the initial button press */
737 combo->current_button = 0;
739 /* Check to see if we released inside the button */
740 child = gtk_get_event_widget ((GdkEvent*) event);
742 while (child && child != (combo->button))
743 child = child->parent;
745 if (child == combo->button)
747 gtk_grab_add (combo->popwin);
748 gdk_pointer_grab (combo->popwin->window, TRUE,
749 GDK_BUTTON_PRESS_MASK |
750 GDK_BUTTON_RELEASE_MASK |
751 GDK_POINTER_MOTION_MASK,
752 NULL, NULL, GDK_CURRENT_TIME);
757 gtk_combo_popdown_list (combo);
761 find_child_foreach (GtkWidget *widget,
764 GdkEventButton *event = data;
768 if (event->x >= widget->allocation.x &&
769 event->x < widget->allocation.x + widget->allocation.width &&
770 event->y >= widget->allocation.y &&
771 event->y < widget->allocation.y + widget->allocation.height)
772 event->window = g_object_ref (widget->window);
777 find_child_window (GtkContainer *container,
778 GdkEventButton *event)
780 gtk_container_foreach (container, find_child_foreach, event);
784 gtk_combo_list_enter (GtkWidget *widget,
785 GdkEventCrossing *event,
788 GtkWidget *event_widget;
790 event_widget = gtk_get_event_widget ((GdkEvent*) event);
792 if ((event_widget == combo->list) &&
793 (combo->current_button != 0) &&
794 (!GTK_WIDGET_HAS_GRAB (combo->list)))
796 GdkEvent *tmp_event = gdk_event_new (GDK_BUTTON_PRESS);
798 GdkModifierType mask;
800 gtk_grab_remove (combo->popwin);
802 /* Transfer the grab over to the list by synthesizing
803 * a button press event
805 gdk_window_get_pointer (combo->list->window, &x, &y, &mask);
807 tmp_event->button.send_event = TRUE;
808 tmp_event->button.time = GDK_CURRENT_TIME; /* bad */
809 tmp_event->button.x = x;
810 tmp_event->button.y = y;
811 /* We leave all the XInput fields unfilled here, in the expectation
812 * that GtkList doesn't care.
814 tmp_event->button.button = combo->current_button;
815 tmp_event->button.state = mask;
817 find_child_window (GTK_CONTAINER (combo->list), &tmp_event->button);
818 if (!tmp_event->button.window)
822 if (GTK_LIST (combo->list)->children)
823 child = GTK_LIST (combo->list)->children->data;
827 tmp_event->button.window = g_object_ref (child->window);
830 gtk_widget_event (combo->list, tmp_event);
831 gdk_event_free (tmp_event);
838 gtk_combo_list_key_press (GtkWidget * widget, GdkEventKey * event, GtkCombo * combo)
840 if (event->keyval == GDK_Escape)
842 if (GTK_WIDGET_HAS_GRAB (combo->list))
843 gtk_list_end_drag_selection (GTK_LIST (combo->list));
845 gtk_combo_popdown_list (combo);
853 combo_event_box_realize (GtkWidget *widget)
855 GdkCursor *cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
857 gdk_window_set_cursor (widget->window, cursor);
858 gdk_cursor_unref (cursor);
862 gtk_combo_init (GtkCombo * combo)
866 GtkWidget *event_box;
868 combo->case_sensitive = FALSE;
869 combo->value_in_list = FALSE;
870 combo->ok_if_empty = TRUE;
871 combo->use_arrows = TRUE;
872 combo->use_arrows_always = FALSE;
873 combo->entry = gtk_entry_new ();
874 combo->button = gtk_button_new ();
875 combo->current_button = 0;
876 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
877 gtk_widget_show (arrow);
878 gtk_container_add (GTK_CONTAINER (combo->button), arrow);
879 gtk_box_pack_start (GTK_BOX (combo), combo->entry, TRUE, TRUE, 0);
880 gtk_box_pack_end (GTK_BOX (combo), combo->button, FALSE, FALSE, 0);
881 GTK_WIDGET_UNSET_FLAGS (combo->button, GTK_CAN_FOCUS);
882 gtk_widget_show (combo->entry);
883 gtk_widget_show (combo->button);
884 combo->entry_change_id = g_signal_connect (combo->entry, "changed",
885 G_CALLBACK (gtk_combo_update_list),
887 g_signal_connect (combo->entry, "key_press_event",
888 G_CALLBACK (gtk_combo_entry_key_press), combo);
889 g_signal_connect_after (combo->entry, "focus_out_event",
890 G_CALLBACK (gtk_combo_entry_focus_out), combo);
891 combo->activate_id = g_signal_connect (combo->entry, "activate",
892 G_CALLBACK (gtk_combo_activate),
894 g_signal_connect (combo->button, "button_press_event",
895 G_CALLBACK (gtk_combo_popup_button_press), combo);
896 g_signal_connect (combo->button, "leave_notify_event",
897 G_CALLBACK (gtk_combo_popup_button_leave), combo);
899 combo->popwin = gtk_window_new (GTK_WINDOW_POPUP);
900 g_object_ref (combo->popwin);
901 gtk_window_set_resizable (GTK_WINDOW (combo->popwin), FALSE);
903 g_signal_connect (combo->popwin, "key_press_event",
904 G_CALLBACK (gtk_combo_window_key_press), combo);
906 gtk_widget_set_events (combo->popwin, GDK_KEY_PRESS_MASK);
908 event_box = gtk_event_box_new ();
909 gtk_container_add (GTK_CONTAINER (combo->popwin), event_box);
910 g_signal_connect (event_box, "realize",
911 G_CALLBACK (combo_event_box_realize), NULL);
912 gtk_widget_show (event_box);
915 frame = gtk_frame_new (NULL);
916 gtk_container_add (GTK_CONTAINER (event_box), frame);
917 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
918 gtk_widget_show (frame);
920 combo->popup = gtk_scrolled_window_new (NULL, NULL);
921 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo->popup),
922 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
923 GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->hscrollbar, GTK_CAN_FOCUS);
924 GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (combo->popup)->vscrollbar, GTK_CAN_FOCUS);
925 gtk_container_add (GTK_CONTAINER (frame), combo->popup);
926 gtk_widget_show (combo->popup);
928 combo->list = gtk_list_new ();
929 /* We'll use enter notify events to figure out when to transfer
930 * the grab to the list
932 gtk_widget_set_events (combo->list, GDK_ENTER_NOTIFY_MASK);
934 gtk_list_set_selection_mode (GTK_LIST(combo->list), GTK_SELECTION_BROWSE);
935 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (combo->popup), combo->list);
936 gtk_container_set_focus_vadjustment (GTK_CONTAINER (combo->list),
937 gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
938 gtk_container_set_focus_hadjustment (GTK_CONTAINER (combo->list),
939 gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo->popup)));
940 gtk_widget_show (combo->list);
942 combo->list_change_id = g_signal_connect (combo->list,
944 G_CALLBACK (gtk_combo_update_entry),
946 g_signal_connect (combo->popwin, "key_press_event",
947 G_CALLBACK (gtk_combo_list_key_press), combo);
948 g_signal_connect (combo->popwin, "button_press_event",
949 G_CALLBACK (gtk_combo_button_press), combo);
951 g_signal_connect (combo->popwin, "event_after",
952 G_CALLBACK (gtk_combo_button_event_after), combo);
953 g_signal_connect (combo->list, "event_after",
954 G_CALLBACK (gtk_combo_button_event_after), combo);
956 g_signal_connect (combo->list, "enter_notify_event",
957 G_CALLBACK (gtk_combo_list_enter), combo);
961 gtk_combo_realize (GtkWidget *widget)
963 GtkCombo *combo = GTK_COMBO (widget);
965 gtk_window_set_screen (GTK_WINDOW (combo->popwin),
966 gtk_widget_get_screen (widget));
968 GTK_WIDGET_CLASS( parent_class )->realize (widget);
972 gtk_combo_unrealize (GtkWidget *widget)
974 GtkCombo *combo = GTK_COMBO (widget);
976 gtk_combo_popdown_list (combo);
977 gtk_widget_unrealize (combo->popwin);
979 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
983 gtk_combo_get_type (void)
985 static GType combo_type = 0;
989 static const GTypeInfo combo_info =
991 sizeof (GtkComboClass),
992 NULL, /* base_init */
993 NULL, /* base_finalize */
994 (GClassInitFunc) gtk_combo_class_init,
995 NULL, /* class_finalize */
996 NULL, /* class_data */
999 (GInstanceInitFunc) gtk_combo_init,
1002 combo_type = g_type_register_static (GTK_TYPE_HBOX, "GtkCombo",
1010 gtk_combo_new (void)
1012 return g_object_new (GTK_TYPE_COMBO, NULL);
1016 gtk_combo_set_value_in_list (GtkCombo * combo, gboolean val, gboolean ok_if_empty)
1018 g_return_if_fail (GTK_IS_COMBO (combo));
1020 ok_if_empty = ok_if_empty != FALSE;
1022 g_object_freeze_notify (G_OBJECT (combo));
1023 if (combo->value_in_list != val)
1025 combo->value_in_list = val;
1026 g_object_notify (G_OBJECT (combo), "value_in_list");
1028 if (combo->ok_if_empty != ok_if_empty)
1030 combo->ok_if_empty = ok_if_empty;
1031 g_object_notify (G_OBJECT (combo), "allow_empty");
1033 g_object_thaw_notify (G_OBJECT (combo));
1037 gtk_combo_set_case_sensitive (GtkCombo * combo, gboolean val)
1039 g_return_if_fail (GTK_IS_COMBO (combo));
1042 if (combo->case_sensitive != val)
1044 combo->case_sensitive = val;
1045 g_object_notify (G_OBJECT (combo), "case_sensitive");
1050 gtk_combo_set_use_arrows (GtkCombo * combo, gboolean val)
1052 g_return_if_fail (GTK_IS_COMBO (combo));
1055 if (combo->use_arrows != val)
1057 combo->use_arrows = val;
1058 g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1063 gtk_combo_set_use_arrows_always (GtkCombo * combo, gboolean val)
1065 g_return_if_fail (GTK_IS_COMBO (combo));
1068 if (combo->use_arrows_always != val)
1070 g_object_freeze_notify (G_OBJECT (combo));
1071 combo->use_arrows_always = val;
1072 g_object_notify (G_OBJECT (combo), "enable_arrows_always");
1074 if (combo->use_arrows != TRUE)
1076 combo->use_arrows = TRUE;
1077 g_object_notify (G_OBJECT (combo), "enable_arrow_keys");
1079 g_object_thaw_notify (G_OBJECT (combo));
1084 gtk_combo_set_popdown_strings (GtkCombo * combo, GList * strings)
1089 g_return_if_fail (GTK_IS_COMBO (combo));
1090 g_return_if_fail (strings != NULL);
1092 gtk_combo_popdown_list (combo);
1094 gtk_list_clear_items (GTK_LIST (combo->list), 0, -1);
1098 li = gtk_list_item_new_with_label ((gchar *) list->data);
1099 gtk_widget_show (li);
1100 gtk_container_add (GTK_CONTAINER (combo->list), li);
1106 gtk_combo_set_item_string (GtkCombo * combo, GtkItem * item, const gchar * item_value)
1108 g_return_if_fail (GTK_IS_COMBO (combo));
1109 g_return_if_fail (item != NULL);
1111 g_object_set_data_full (G_OBJECT (item), gtk_combo_string_key,
1112 g_strdup (item_value), g_free);
1116 gtk_combo_size_allocate (GtkWidget *widget,
1117 GtkAllocation *allocation)
1121 g_return_if_fail (GTK_IS_COMBO (widget));
1122 g_return_if_fail (allocation != NULL);
1124 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
1126 combo = GTK_COMBO (widget);
1128 if (combo->entry->allocation.height > combo->entry->requisition.height)
1130 GtkAllocation button_allocation;
1132 button_allocation = combo->button->allocation;
1133 button_allocation.height = combo->entry->requisition.height;
1134 button_allocation.y = combo->entry->allocation.y +
1135 (combo->entry->allocation.height - combo->entry->requisition.height)
1137 gtk_widget_size_allocate (combo->button, &button_allocation);
1142 gtk_combo_disable_activate (GtkCombo* combo)
1144 g_return_if_fail (GTK_IS_COMBO (combo));
1146 if ( combo->activate_id ) {
1147 g_signal_handler_disconnect (combo->entry, combo->activate_id);
1148 combo->activate_id = 0;
1153 gtk_combo_set_property (GObject *object,
1155 const GValue *value,
1158 GtkCombo *combo = GTK_COMBO (object);
1162 case PROP_ENABLE_ARROW_KEYS:
1163 gtk_combo_set_use_arrows (combo, g_value_get_boolean (value));
1165 case PROP_ENABLE_ARROWS_ALWAYS:
1166 gtk_combo_set_use_arrows_always (combo, g_value_get_boolean (value));
1168 case PROP_CASE_SENSITIVE:
1169 gtk_combo_set_case_sensitive (combo, g_value_get_boolean (value));
1171 case PROP_ALLOW_EMPTY:
1172 combo->ok_if_empty = g_value_get_boolean (value);
1174 case PROP_VALUE_IN_LIST:
1175 combo->value_in_list = g_value_get_boolean (value);
1178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1185 gtk_combo_get_property (GObject *object,
1190 GtkCombo *combo = GTK_COMBO (object);
1194 case PROP_ENABLE_ARROW_KEYS:
1195 g_value_set_boolean (value, combo->use_arrows);
1197 case PROP_ENABLE_ARROWS_ALWAYS:
1198 g_value_set_boolean (value, combo->use_arrows_always);
1200 case PROP_CASE_SENSITIVE:
1201 g_value_set_boolean (value, combo->case_sensitive);
1203 case PROP_ALLOW_EMPTY:
1204 g_value_set_boolean (value, combo->ok_if_empty);
1206 case PROP_VALUE_IN_LIST:
1207 g_value_set_boolean (value, combo->value_in_list);
1210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);