2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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 #include "gtkcombobox.h"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
32 #include "gtkliststore.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkvseparator.h"
42 #include "gtkwindow.h"
43 #include "gtkprivate.h"
45 #include <gdk/gdkkeysyms.h>
47 #include <gobject/gvaluecollector.h>
52 #include "gtkmarshalers.h"
55 #include "gtkentryprivate.h"
56 #include "gtktreeprivate.h"
61 * @Short_description: A widget used to choose from a list of items
63 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
65 * A GtkComboBox is a widget that allows the user to choose from a list of
66 * valid choices. The GtkComboBox displays the selected choice. When
67 * activated, the GtkComboBox displays a popup which allows the user to
68 * make a new choice. The style in which the selected value is displayed,
69 * and the style of the popup is determined by the current theme. It may
70 * be similar to a Windows-style combo box.
72 * The GtkComboBox uses the model-view pattern; the list of valid choices
73 * is specified in the form of a tree model, and the display of the choices
74 * can be adapted to the data in the model by using cell renderers, as you
75 * would in a tree view. This is possible since GtkComboBox implements the
76 * #GtkCellLayout interface. The tree model holding the valid choices is
77 * not restricted to a flat list, it can be a real tree, and the popup will
78 * reflect the tree structure.
80 * For a simple list of textual choices, the model-view API of GtkComboBox
81 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86 /* WELCOME, to THE house of evil code */
87 struct _GtkComboBoxPrivate
97 GtkShadowType shadow_type;
99 gint active; /* Only temporary */
100 GtkTreeRowReference *active_row;
102 GtkWidget *tree_view;
103 GtkTreeViewColumn *column;
105 GtkWidget *cell_view;
106 GtkWidget *cell_view_frame;
111 GtkWidget *separator;
113 GtkWidget *popup_widget;
114 GtkWidget *popup_window;
115 GtkWidget *scrolled_window;
122 guint activate_button;
123 guint32 activate_time;
125 guint resize_idle_id;
127 /* For "has-entry" specific behavior we track
128 * an automated cell renderer and text column */
130 GtkCellRenderer *text_renderer;
134 guint popup_in_progress : 1;
135 guint popup_shown : 1;
136 guint add_tearoffs : 1;
138 guint is_cell_renderer : 1;
139 guint editing_canceled : 1;
140 guint auto_scroll : 1;
141 guint focus_on_click : 1;
142 guint button_sensitivity : 2;
144 guint popup_fixed_width : 1;
146 GtkTreeViewRowSeparatorFunc row_separator_func;
147 gpointer row_separator_data;
148 GDestroyNotify row_separator_destroy;
150 GdkDevice *grab_pointer;
151 GdkDevice *grab_keyboard;
153 gchar *tearoff_title;
156 /* While debugging this evil code, I have learned that
157 * there are actually 4 modes to this widget, which can
158 * be characterized as follows
160 * 1) menu mode, no child added
163 * cell_view -> GtkCellView, regular child
164 * cell_view_frame -> NULL
165 * button -> GtkToggleButton set_parent to combo
166 * arrow -> GtkArrow set_parent to button
167 * separator -> GtkVSepator set_parent to button
168 * popup_widget -> GtkMenu
169 * popup_window -> NULL
170 * scrolled_window -> NULL
172 * 2) menu mode, child added
176 * cell_view_frame -> NULL
177 * button -> GtkToggleButton set_parent to combo
178 * arrow -> GtkArrow, child of button
180 * popup_widget -> GtkMenu
181 * popup_window -> NULL
182 * scrolled_window -> NULL
184 * 3) list mode, no child added
186 * tree_view -> GtkTreeView, child of scrolled_window
187 * cell_view -> GtkCellView, regular child
188 * cell_view_frame -> GtkFrame, set parent to combo
189 * button -> GtkToggleButton, set_parent to combo
190 * arrow -> GtkArrow, child of button
192 * popup_widget -> tree_view
193 * popup_window -> GtkWindow
194 * scrolled_window -> GtkScrolledWindow, child of popup_window
196 * 4) list mode, child added
198 * tree_view -> GtkTreeView, child of scrolled_window
200 * cell_view_frame -> NULL
201 * button -> GtkToggleButton, set_parent to combo
202 * arrow -> GtkArrow, child of button
204 * popup_widget -> tree_view
205 * popup_window -> GtkWindow
206 * scrolled_window -> GtkScrolledWindow, child of popup_window
222 PROP_ROW_SPAN_COLUMN,
223 PROP_COLUMN_SPAN_COLUMN,
230 PROP_BUTTON_SENSITIVITY,
231 PROP_EDITING_CANCELED,
233 PROP_ENTRY_TEXT_COLUMN,
234 PROP_POPUP_FIXED_WIDTH,
240 static guint combo_box_signals[LAST_SIGNAL] = {0,};
242 #define BONUS_PADDING 4
243 #define SCROLL_TIME 100
247 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
248 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
249 static GObject *gtk_combo_box_constructor (GType type,
250 guint n_construct_properties,
251 GObjectConstructParam *construct_properties);
252 static void gtk_combo_box_dispose (GObject *object);
253 static void gtk_combo_box_finalize (GObject *object);
254 static void gtk_combo_box_destroy (GtkWidget *widget);
256 static void gtk_combo_box_set_property (GObject *object,
260 static void gtk_combo_box_get_property (GObject *object,
265 static void gtk_combo_box_state_changed (GtkWidget *widget,
266 GtkStateType previous);
267 static void gtk_combo_box_grab_focus (GtkWidget *widget);
268 static void gtk_combo_box_style_updated (GtkWidget *widget);
269 static void gtk_combo_box_button_toggled (GtkWidget *widget,
271 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
272 GtkStateFlags previous,
274 static void gtk_combo_box_add (GtkContainer *container,
276 static void gtk_combo_box_remove (GtkContainer *container,
279 static void gtk_combo_box_menu_show (GtkWidget *menu,
281 static void gtk_combo_box_menu_hide (GtkWidget *menu,
284 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
286 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
291 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
296 static void gtk_combo_box_menu_position (GtkMenu *menu,
302 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
304 static void gtk_combo_box_size_allocate (GtkWidget *widget,
305 GtkAllocation *allocation);
306 static void gtk_combo_box_forall (GtkContainer *container,
307 gboolean include_internals,
308 GtkCallback callback,
309 gpointer callback_data);
310 static gboolean gtk_combo_box_draw (GtkWidget *widget,
312 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
313 GdkEventScroll *event);
314 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
317 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
318 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
319 GtkScrollType scroll);
320 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
321 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
323 /* listening to the model */
324 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
328 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
331 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
336 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
340 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
346 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
351 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
352 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
354 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
355 GdkEventButton *event,
357 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
360 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
361 GdkEventCrossing *event,
363 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
366 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
367 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
368 GdkEventButton *event,
371 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
374 gboolean path_currently_selected,
377 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
381 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
384 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
385 gboolean add_children);
386 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
387 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
390 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
391 GdkEventButton *event,
393 static void gtk_combo_box_menu_activate (GtkWidget *menu,
395 GtkComboBox *combo_box);
396 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
397 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
400 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
402 guint32 activate_time);
405 GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
407 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
408 gboolean group_cycling);
410 static void gtk_combo_box_child_show (GtkWidget *widget,
411 GtkComboBox *combo_box);
412 static void gtk_combo_box_child_hide (GtkWidget *widget,
413 GtkComboBox *combo_box);
415 /* GtkComboBox:has-entry callbacks */
416 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
418 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
422 /* GtkBuildable method implementation */
423 static GtkBuildableIface *parent_buildable_iface;
425 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
426 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
429 const gchar *tagname,
430 GMarkupParser *parser,
432 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
435 const gchar *tagname,
437 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
439 const gchar *childname);
442 /* GtkCellEditable method implementations */
443 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
446 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
449 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
452 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
456 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
462 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
463 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
464 gtk_combo_box_cell_layout_init)
465 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
466 gtk_combo_box_cell_editable_init)
467 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
468 gtk_combo_box_buildable_init))
473 gtk_combo_box_class_init (GtkComboBoxClass *klass)
475 GObjectClass *object_class;
476 GtkContainerClass *container_class;
477 GtkWidgetClass *widget_class;
478 GtkBindingSet *binding_set;
480 container_class = (GtkContainerClass *)klass;
481 container_class->forall = gtk_combo_box_forall;
482 container_class->add = gtk_combo_box_add;
483 container_class->remove = gtk_combo_box_remove;
485 widget_class = (GtkWidgetClass *)klass;
486 widget_class->size_allocate = gtk_combo_box_size_allocate;
487 widget_class->draw = gtk_combo_box_draw;
488 widget_class->scroll_event = gtk_combo_box_scroll_event;
489 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
490 widget_class->grab_focus = gtk_combo_box_grab_focus;
491 widget_class->style_updated = gtk_combo_box_style_updated;
492 widget_class->state_changed = gtk_combo_box_state_changed;
493 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
494 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
495 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
496 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
497 widget_class->destroy = gtk_combo_box_destroy;
499 object_class = (GObjectClass *)klass;
500 object_class->constructor = gtk_combo_box_constructor;
501 object_class->dispose = gtk_combo_box_dispose;
502 object_class->finalize = gtk_combo_box_finalize;
503 object_class->set_property = gtk_combo_box_set_property;
504 object_class->get_property = gtk_combo_box_get_property;
508 * GtkComboBox::changed:
509 * @widget: the object which received the signal
511 * The changed signal is emitted when the active
512 * item is changed. The can be due to the user selecting
513 * a different item from the list, or due to a
514 * call to gtk_combo_box_set_active_iter().
515 * It will also be emitted while typing into the entry of a combo box
520 combo_box_signals[CHANGED] =
521 g_signal_new (I_("changed"),
522 G_OBJECT_CLASS_TYPE (klass),
524 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
526 g_cclosure_marshal_VOID__VOID,
529 * GtkComboBox::move-active:
530 * @widget: the object that received the signal
531 * @scroll_type: a #GtkScrollType
533 * The ::move-active signal is a
534 * <link linkend="keybinding-signals">keybinding signal</link>
535 * which gets emitted to move the active selection.
539 combo_box_signals[MOVE_ACTIVE] =
540 g_signal_new_class_handler (I_("move-active"),
541 G_OBJECT_CLASS_TYPE (klass),
542 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
543 G_CALLBACK (gtk_combo_box_real_move_active),
545 g_cclosure_marshal_VOID__ENUM,
547 GTK_TYPE_SCROLL_TYPE);
550 * GtkComboBox::popup:
551 * @widget: the object that received the signal
553 * The ::popup signal is a
554 * <link linkend="keybinding-signals">keybinding signal</link>
555 * which gets emitted to popup the combo box list.
557 * The default binding for this signal is Alt+Down.
561 combo_box_signals[POPUP] =
562 g_signal_new_class_handler (I_("popup"),
563 G_OBJECT_CLASS_TYPE (klass),
564 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
565 G_CALLBACK (gtk_combo_box_real_popup),
567 g_cclosure_marshal_VOID__VOID,
570 * GtkComboBox::popdown:
571 * @button: the object which received the signal
573 * The ::popdown signal is a
574 * <link linkend="keybinding-signals">keybinding signal</link>
575 * which gets emitted to popdown the combo box list.
577 * The default bindings for this signal are Alt+Up and Escape.
581 combo_box_signals[POPDOWN] =
582 g_signal_new_class_handler (I_("popdown"),
583 G_OBJECT_CLASS_TYPE (klass),
584 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
585 G_CALLBACK (gtk_combo_box_real_popdown),
587 _gtk_marshal_BOOLEAN__VOID,
591 binding_set = gtk_binding_set_by_class (widget_class);
593 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
595 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
598 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
600 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
602 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
605 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
607 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
608 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
610 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
611 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
613 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
614 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
616 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
617 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
619 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
620 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
622 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
624 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
626 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
627 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
629 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
630 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
632 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
633 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
635 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
636 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
638 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
639 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
641 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
644 g_object_class_override_property (object_class,
645 PROP_EDITING_CANCELED,
651 * The model from which the combo box takes the values shown
656 g_object_class_install_property (object_class,
658 g_param_spec_object ("model",
659 P_("ComboBox model"),
660 P_("The model for the combo box"),
662 GTK_PARAM_READWRITE));
665 * GtkComboBox:wrap-width:
667 * If wrap-width is set to a positive value, the list will be
668 * displayed in multiple columns, the number of columns is
669 * determined by wrap-width.
673 g_object_class_install_property (object_class,
675 g_param_spec_int ("wrap-width",
677 P_("Wrap width for laying out the items in a grid"),
681 GTK_PARAM_READWRITE));
685 * GtkComboBox:row-span-column:
687 * If this is set to a non-negative value, it must be the index of a column
688 * of type %G_TYPE_INT in the model.
690 * The values of that column are used to determine how many rows a value in
691 * the list will span. Therefore, the values in the model column pointed to
692 * by this property must be greater than zero and not larger than wrap-width.
696 g_object_class_install_property (object_class,
697 PROP_ROW_SPAN_COLUMN,
698 g_param_spec_int ("row-span-column",
699 P_("Row span column"),
700 P_("TreeModel column containing the row span values"),
704 GTK_PARAM_READWRITE));
708 * GtkComboBox:column-span-column:
710 * If this is set to a non-negative value, it must be the index of a column
711 * of type %G_TYPE_INT in the model.
713 * The values of that column are used to determine how many columns a value
714 * in the list will span.
718 g_object_class_install_property (object_class,
719 PROP_COLUMN_SPAN_COLUMN,
720 g_param_spec_int ("column-span-column",
721 P_("Column span column"),
722 P_("TreeModel column containing the column span values"),
726 GTK_PARAM_READWRITE));
730 * GtkComboBox:active:
732 * The item which is currently active. If the model is a non-flat treemodel,
733 * and the active item is not an immediate child of the root of the tree,
734 * this property has the value
735 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
736 * where <literal>path</literal> is the #GtkTreePath of the active item.
740 g_object_class_install_property (object_class,
742 g_param_spec_int ("active",
744 P_("The item which is currently active"),
748 GTK_PARAM_READWRITE));
751 * GtkComboBox:add-tearoffs:
753 * The add-tearoffs property controls whether generated menus
754 * have tearoff menu items.
756 * Note that this only affects menu style combo boxes.
760 g_object_class_install_property (object_class,
762 g_param_spec_boolean ("add-tearoffs",
763 P_("Add tearoffs to menus"),
764 P_("Whether dropdowns should have a tearoff menu item"),
766 GTK_PARAM_READWRITE));
769 * GtkComboBox:has-frame:
771 * The has-frame property controls whether a frame
772 * is drawn around the entry.
776 g_object_class_install_property (object_class,
778 g_param_spec_boolean ("has-frame",
780 P_("Whether the combo box draws a frame around the child"),
782 GTK_PARAM_READWRITE));
784 g_object_class_install_property (object_class,
786 g_param_spec_boolean ("focus-on-click",
787 P_("Focus on click"),
788 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
790 GTK_PARAM_READWRITE));
793 * GtkComboBox:tearoff-title:
795 * A title that may be displayed by the window manager
796 * when the popup is torn-off.
800 g_object_class_install_property (object_class,
802 g_param_spec_string ("tearoff-title",
804 P_("A title that may be displayed by the window manager when the popup is torn-off"),
806 GTK_PARAM_READWRITE));
810 * GtkComboBox:popup-shown:
812 * Whether the combo boxes dropdown is popped up.
813 * Note that this property is mainly useful, because
814 * it allows you to connect to notify::popup-shown.
818 g_object_class_install_property (object_class,
820 g_param_spec_boolean ("popup-shown",
822 P_("Whether the combo's dropdown is shown"),
824 GTK_PARAM_READABLE));
828 * GtkComboBox:button-sensitivity:
830 * Whether the dropdown button is sensitive when
831 * the model is empty.
835 g_object_class_install_property (object_class,
836 PROP_BUTTON_SENSITIVITY,
837 g_param_spec_enum ("button-sensitivity",
838 P_("Button Sensitivity"),
839 P_("Whether the dropdown button is sensitive when the model is empty"),
840 GTK_TYPE_SENSITIVITY_TYPE,
841 GTK_SENSITIVITY_AUTO,
842 GTK_PARAM_READWRITE));
845 * GtkComboBox:has-entry:
847 * Whether the combo box has an entry.
851 g_object_class_install_property (object_class,
853 g_param_spec_boolean ("has-entry",
855 P_("Whether combo box has an entry"),
857 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
860 * GtkComboBox:entry-text-column:
862 * The column in the combo box's model to associate with strings from the entry
863 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
867 g_object_class_install_property (object_class,
868 PROP_ENTRY_TEXT_COLUMN,
869 g_param_spec_int ("entry-text-column",
870 P_("Entry Text Column"),
871 P_("The column in the combo box's model to associate "
872 "with strings from the entry if the combo was "
873 "created with #GtkComboBox:has-entry = %TRUE"),
875 GTK_PARAM_READWRITE));
878 * GtkComboBox:id-column:
880 * The column in the combo box's model that provides string
881 * IDs for the values in the model, if != -1.
885 g_object_class_install_property (object_class,
887 g_param_spec_int ("id-column",
889 P_("The column in the combo box's model that provides "
890 "string IDs for the values in the model"),
892 GTK_PARAM_READWRITE));
895 * GtkComboBox:active-id:
897 * The value of the ID column of the active row.
901 g_object_class_install_property (object_class,
903 g_param_spec_string ("active-id",
905 P_("The value of the id column "
906 "for the active row"),
907 NULL, GTK_PARAM_READWRITE));
910 * GtkComboBox:popup-fixed-width:
912 * Whether the popup's width should be a fixed width matching the
913 * allocated width of the combo box.
917 g_object_class_install_property (object_class,
918 PROP_POPUP_FIXED_WIDTH,
919 g_param_spec_boolean ("popup-fixed-width",
920 P_("Popup Fixed Width"),
921 P_("Whether the popup's width should be a "
922 "fixed width matching the allocated width "
925 GTK_PARAM_READWRITE));
928 * GtkComboBox:cell-area:
930 * The #GtkCellArea used to layout cell renderers for this combo box.
934 g_object_class_install_property (object_class,
936 g_param_spec_object ("cell-area",
938 P_("The GtkCellArea used to layout cells"),
940 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
942 gtk_widget_class_install_style_property (widget_class,
943 g_param_spec_boolean ("appears-as-list",
944 P_("Appears as list"),
945 P_("Whether dropdowns should look like lists rather than menus"),
947 GTK_PARAM_READABLE));
950 * GtkComboBox:arrow-size:
952 * Sets the minimum size of the arrow in the combo box. Note
953 * that the arrow size is coupled to the font size, so in case
954 * a larger font is used, the arrow will be larger than set
959 gtk_widget_class_install_style_property (widget_class,
960 g_param_spec_int ("arrow-size",
962 P_("The minimum size of the arrow in the combo box"),
966 GTK_PARAM_READABLE));
969 * GtkComboBox:shadow-type:
971 * Which kind of shadow to draw around the combo box.
975 gtk_widget_class_install_style_property (widget_class,
976 g_param_spec_enum ("shadow-type",
978 P_("Which kind of shadow to draw around the combo box"),
979 GTK_TYPE_SHADOW_TYPE,
981 GTK_PARAM_READABLE));
983 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
987 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
989 parent_buildable_iface = g_type_interface_peek_parent (iface);
990 iface->add_child = _gtk_cell_layout_buildable_add_child;
991 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
992 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
993 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
997 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
999 iface->get_area = gtk_combo_box_cell_layout_get_area;
1003 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1005 iface->start_editing = gtk_combo_box_start_editing;
1009 gtk_combo_box_init (GtkComboBox *combo_box)
1011 GtkComboBoxPrivate *priv;
1013 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1015 GtkComboBoxPrivate);
1016 priv = combo_box->priv;
1018 priv->wrap_width = 0;
1021 priv->active_row = NULL;
1022 priv->col_column = -1;
1023 priv->row_column = -1;
1025 priv->popup_shown = FALSE;
1026 priv->add_tearoffs = FALSE;
1027 priv->has_frame = TRUE;
1028 priv->is_cell_renderer = FALSE;
1029 priv->editing_canceled = FALSE;
1030 priv->auto_scroll = FALSE;
1031 priv->focus_on_click = TRUE;
1032 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1033 priv->has_entry = FALSE;
1034 priv->popup_fixed_width = TRUE;
1036 priv->text_column = -1;
1037 priv->text_renderer = NULL;
1038 priv->id_column = -1;
1042 gtk_combo_box_set_property (GObject *object,
1044 const GValue *value,
1047 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1053 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1056 case PROP_WRAP_WIDTH:
1057 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1060 case PROP_ROW_SPAN_COLUMN:
1061 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1064 case PROP_COLUMN_SPAN_COLUMN:
1065 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1069 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1072 case PROP_ADD_TEAROFFS:
1073 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1076 case PROP_HAS_FRAME:
1077 combo_box->priv->has_frame = g_value_get_boolean (value);
1079 if (combo_box->priv->has_entry)
1083 child = gtk_bin_get_child (GTK_BIN (combo_box));
1085 gtk_entry_set_has_frame (GTK_ENTRY (child),
1086 combo_box->priv->has_frame);
1091 case PROP_FOCUS_ON_CLICK:
1092 gtk_combo_box_set_focus_on_click (combo_box,
1093 g_value_get_boolean (value));
1096 case PROP_TEAROFF_TITLE:
1097 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1100 case PROP_POPUP_SHOWN:
1101 if (g_value_get_boolean (value))
1102 gtk_combo_box_popup (combo_box);
1104 gtk_combo_box_popdown (combo_box);
1107 case PROP_BUTTON_SENSITIVITY:
1108 gtk_combo_box_set_button_sensitivity (combo_box,
1109 g_value_get_enum (value));
1112 case PROP_POPUP_FIXED_WIDTH:
1113 gtk_combo_box_set_popup_fixed_width (combo_box,
1114 g_value_get_boolean (value));
1117 case PROP_EDITING_CANCELED:
1118 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1121 case PROP_HAS_ENTRY:
1122 combo_box->priv->has_entry = g_value_get_boolean (value);
1125 case PROP_ENTRY_TEXT_COLUMN:
1126 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1129 case PROP_ID_COLUMN:
1130 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1133 case PROP_ACTIVE_ID:
1134 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1137 case PROP_CELL_AREA:
1138 /* Construct-only, can only be assigned once */
1139 area = g_value_get_object (value);
1142 combo_box->priv->area = g_object_ref_sink (area);
1146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1152 gtk_combo_box_get_property (GObject *object,
1157 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1158 GtkComboBoxPrivate *priv = combo_box->priv;
1163 g_value_set_object (value, combo_box->priv->model);
1166 case PROP_WRAP_WIDTH:
1167 g_value_set_int (value, combo_box->priv->wrap_width);
1170 case PROP_ROW_SPAN_COLUMN:
1171 g_value_set_int (value, combo_box->priv->row_column);
1174 case PROP_COLUMN_SPAN_COLUMN:
1175 g_value_set_int (value, combo_box->priv->col_column);
1179 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1182 case PROP_ADD_TEAROFFS:
1183 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1186 case PROP_HAS_FRAME:
1187 g_value_set_boolean (value, combo_box->priv->has_frame);
1190 case PROP_FOCUS_ON_CLICK:
1191 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1194 case PROP_TEAROFF_TITLE:
1195 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1198 case PROP_POPUP_SHOWN:
1199 g_value_set_boolean (value, combo_box->priv->popup_shown);
1202 case PROP_BUTTON_SENSITIVITY:
1203 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1206 case PROP_POPUP_FIXED_WIDTH:
1207 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1210 case PROP_EDITING_CANCELED:
1211 g_value_set_boolean (value, priv->editing_canceled);
1214 case PROP_HAS_ENTRY:
1215 g_value_set_boolean (value, priv->has_entry);
1218 case PROP_ENTRY_TEXT_COLUMN:
1219 g_value_set_int (value, priv->text_column);
1222 case PROP_ID_COLUMN:
1223 g_value_set_int (value, priv->id_column);
1226 case PROP_ACTIVE_ID:
1227 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1230 case PROP_CELL_AREA:
1231 g_value_set_object (value, priv->area);
1235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1241 gtk_combo_box_state_changed (GtkWidget *widget,
1242 GtkStateType previous)
1244 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1245 GtkComboBoxPrivate *priv = combo_box->priv;
1247 if (gtk_widget_get_realized (widget))
1249 if (priv->tree_view && priv->cell_view)
1251 GtkStyleContext *context;
1252 GtkStateFlags state;
1255 context = gtk_widget_get_style_context (widget);
1256 state = gtk_widget_get_state_flags (widget);
1258 gtk_style_context_get (context, state,
1259 "background-color", &color,
1262 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
1263 gdk_rgba_free (color);
1267 gtk_widget_queue_draw (widget);
1271 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1272 GtkStateFlags previous,
1275 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1276 GtkComboBoxPrivate *priv = combo_box->priv;
1278 if (gtk_widget_get_realized (widget))
1280 if (!priv->tree_view && priv->cell_view)
1281 gtk_widget_set_state_flags (priv->cell_view,
1282 gtk_widget_get_state_flags (widget),
1286 gtk_widget_queue_draw (widget);
1290 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1292 GtkComboBoxPrivate *priv = combo_box->priv;
1293 gboolean appears_as_list;
1295 /* if wrap_width > 0, then we are in grid-mode and forced to use
1298 if (priv->wrap_width)
1299 appears_as_list = FALSE;
1301 gtk_widget_style_get (GTK_WIDGET (combo_box),
1302 "appears-as-list", &appears_as_list,
1305 if (appears_as_list)
1307 /* Destroy all the menu mode widgets, if they exist. */
1308 if (GTK_IS_MENU (priv->popup_widget))
1309 gtk_combo_box_menu_destroy (combo_box);
1311 /* Create the list mode widgets, if they don't already exist. */
1312 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1313 gtk_combo_box_list_setup (combo_box);
1317 /* Destroy all the list mode widgets, if they exist. */
1318 if (GTK_IS_TREE_VIEW (priv->tree_view))
1319 gtk_combo_box_list_destroy (combo_box);
1321 /* Create the menu mode widgets, if they don't already exist. */
1322 if (!GTK_IS_MENU (priv->popup_widget))
1323 gtk_combo_box_menu_setup (combo_box, TRUE);
1326 gtk_widget_style_get (GTK_WIDGET (combo_box),
1327 "shadow-type", &priv->shadow_type,
1332 gtk_combo_box_style_updated (GtkWidget *widget)
1334 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1335 GtkComboBoxPrivate *priv = combo_box->priv;
1338 gtk_combo_box_check_appearance (combo_box);
1340 if (priv->tree_view && priv->cell_view)
1342 GtkStyleContext *context;
1345 context = gtk_widget_get_style_context (widget);
1346 gtk_style_context_get (context, 0,
1347 "background-color", &color,
1350 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view),
1353 gdk_rgba_free (color);
1356 child = gtk_bin_get_child (GTK_BIN (combo_box));
1357 if (GTK_IS_ENTRY (child))
1358 g_object_set (child, "shadow-type",
1359 GTK_SHADOW_NONE == priv->shadow_type ?
1360 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1364 gtk_combo_box_button_toggled (GtkWidget *widget,
1367 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1369 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1371 if (!combo_box->priv->popup_in_progress)
1372 gtk_combo_box_popup (combo_box);
1375 gtk_combo_box_popdown (combo_box);
1379 gtk_combo_box_add (GtkContainer *container,
1382 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1383 GtkComboBoxPrivate *priv = combo_box->priv;
1385 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1387 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1388 "(need an instance of GtkEntry or of a subclass)",
1389 G_OBJECT_TYPE_NAME (widget));
1393 if (priv->cell_view &&
1394 gtk_widget_get_parent (priv->cell_view))
1396 gtk_widget_unparent (priv->cell_view);
1397 _gtk_bin_set_child (GTK_BIN (container), NULL);
1398 gtk_widget_queue_resize (GTK_WIDGET (container));
1401 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1402 _gtk_bin_set_child (GTK_BIN (container), widget);
1404 if (priv->cell_view &&
1405 widget != priv->cell_view)
1407 /* since the cell_view was unparented, it's gone now */
1408 priv->cell_view = NULL;
1410 if (!priv->tree_view && priv->separator)
1412 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1414 priv->separator = NULL;
1416 gtk_widget_queue_resize (GTK_WIDGET (container));
1418 else if (priv->cell_view_frame)
1420 gtk_widget_unparent (priv->cell_view_frame);
1421 priv->cell_view_frame = NULL;
1426 if (priv->has_entry)
1428 /* this flag is a hack to tell the entry to fill its allocation.
1430 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1432 g_signal_connect (widget, "changed",
1433 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1436 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1441 gtk_combo_box_remove (GtkContainer *container,
1444 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1445 GtkComboBoxPrivate *priv = combo_box->priv;
1447 gboolean appears_as_list;
1449 if (priv->has_entry)
1451 GtkWidget *child_widget;
1453 child_widget = gtk_bin_get_child (GTK_BIN (container));
1454 if (widget && widget == child_widget)
1456 g_signal_handlers_disconnect_by_func (widget,
1457 gtk_combo_box_entry_contents_changed,
1459 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1463 if (widget == priv->cell_view)
1464 priv->cell_view = NULL;
1466 gtk_widget_unparent (widget);
1467 _gtk_bin_set_child (GTK_BIN (container), NULL);
1469 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1472 gtk_widget_queue_resize (GTK_WIDGET (container));
1474 if (!priv->tree_view)
1475 appears_as_list = FALSE;
1477 appears_as_list = TRUE;
1479 if (appears_as_list)
1480 gtk_combo_box_list_destroy (combo_box);
1481 else if (GTK_IS_MENU (priv->popup_widget))
1483 gtk_combo_box_menu_destroy (combo_box);
1484 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1485 priv->popup_widget = NULL;
1488 if (!priv->cell_view)
1490 priv->cell_view = gtk_cell_view_new ();
1491 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1492 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1494 gtk_widget_show (priv->cell_view);
1495 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1500 if (appears_as_list)
1501 gtk_combo_box_list_setup (combo_box);
1503 gtk_combo_box_menu_setup (combo_box, TRUE);
1505 if (gtk_tree_row_reference_valid (priv->active_row))
1507 path = gtk_tree_row_reference_get_path (priv->active_row);
1508 gtk_combo_box_set_active_internal (combo_box, path);
1509 gtk_tree_path_free (path);
1512 gtk_combo_box_set_active_internal (combo_box, NULL);
1516 gtk_combo_box_menu_show (GtkWidget *menu,
1519 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1520 GtkComboBoxPrivate *priv = combo_box->priv;
1522 gtk_combo_box_child_show (menu, user_data);
1524 priv->popup_in_progress = TRUE;
1525 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1527 priv->popup_in_progress = FALSE;
1531 gtk_combo_box_menu_hide (GtkWidget *menu,
1534 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1536 gtk_combo_box_child_hide (menu,user_data);
1538 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1543 gtk_combo_box_detacher (GtkWidget *widget,
1546 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1547 GtkComboBoxPrivate *priv = combo_box->priv;
1549 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1551 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1552 gtk_combo_box_menu_show,
1554 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1555 gtk_combo_box_menu_hide,
1558 priv->popup_widget = NULL;
1562 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1565 GtkComboBoxPrivate *priv = combo_box->priv;
1567 if (GTK_IS_MENU (priv->popup_widget))
1569 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1570 priv->popup_widget = NULL;
1572 else if (priv->popup_widget)
1574 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1575 priv->popup_widget);
1576 g_object_unref (priv->popup_widget);
1577 priv->popup_widget = NULL;
1580 if (GTK_IS_MENU (popup))
1582 if (priv->popup_window)
1584 gtk_widget_destroy (priv->popup_window);
1585 priv->popup_window = NULL;
1588 priv->popup_widget = popup;
1591 * Note that we connect to show/hide on the toplevel, not the
1592 * menu itself, since the menu is not shown/hidden when it is
1593 * popped up while torn-off.
1595 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1596 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1597 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1598 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1600 gtk_menu_attach_to_widget (GTK_MENU (popup),
1601 GTK_WIDGET (combo_box),
1602 gtk_combo_box_detacher);
1606 if (!priv->popup_window)
1608 GtkWidget *toplevel;
1610 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1611 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1613 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1614 GDK_WINDOW_TYPE_HINT_COMBO);
1616 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1617 G_CALLBACK (gtk_combo_box_child_show),
1619 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1620 G_CALLBACK (gtk_combo_box_child_hide),
1623 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1624 if (GTK_IS_WINDOW (toplevel))
1626 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1627 GTK_WINDOW (priv->popup_window));
1628 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1629 GTK_WINDOW (toplevel));
1632 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1633 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1634 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1636 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1638 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1641 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1644 gtk_widget_show (priv->scrolled_window);
1646 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1647 priv->scrolled_window);
1650 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1653 gtk_widget_show (popup);
1654 g_object_ref (popup);
1655 priv->popup_widget = popup;
1660 get_widget_border (GtkWidget *widget,
1663 GtkStyleContext *context;
1664 GtkBorder *border_width;
1666 context = gtk_widget_get_style_context (widget);
1668 gtk_style_context_get (context,
1669 gtk_widget_get_state_flags (widget),
1670 "border-width", &border_width,
1673 *border = *border_width;
1674 gtk_border_free (border_width);
1678 gtk_combo_box_menu_position_below (GtkMenu *menu,
1684 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1685 GtkAllocation child_allocation;
1691 GdkRectangle monitor;
1694 /* FIXME: is using the size request here broken? */
1695 child = gtk_bin_get_child (GTK_BIN (combo_box));
1699 gtk_widget_get_allocation (child, &child_allocation);
1701 if (!gtk_widget_get_has_window (child))
1703 sx += child_allocation.x;
1704 sy += child_allocation.y;
1707 gdk_window_get_root_coords (gtk_widget_get_window (child),
1709 get_widget_border (GTK_WIDGET (combo_box), &border);
1712 if (combo_box->priv->popup_fixed_width)
1713 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1715 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1717 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1720 *x = sx + child_allocation.width - req.width;
1723 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1724 monitor_num = gdk_screen_get_monitor_at_window (screen,
1725 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1726 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1730 else if (*x + req.width > monitor.x + monitor.width)
1731 *x = monitor.x + monitor.width - req.width;
1733 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1734 *y += child_allocation.height;
1735 else if (*y - monitor.y >= req.height)
1737 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1738 *y += child_allocation.height;
1746 gtk_combo_box_menu_position_over (GtkMenu *menu,
1752 GtkComboBox *combo_box;
1756 GtkAllocation allocation;
1757 GtkAllocation child_allocation;
1764 combo_box = GTK_COMBO_BOX (user_data);
1765 widget = GTK_WIDGET (combo_box);
1767 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1769 gtk_widget_get_allocation (widget, &allocation);
1771 menu_xpos = allocation.x;
1772 menu_ypos = allocation.y + allocation.height / 2 - 2;
1774 if (combo_box->priv->popup_fixed_width)
1775 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1777 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1781 gtk_widget_get_allocation (active, &child_allocation);
1782 menu_ypos -= child_allocation.height / 2;
1785 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1788 child = children->data;
1790 if (active == child)
1793 if (gtk_widget_get_visible (child))
1795 gtk_widget_get_allocation (child, &child_allocation);
1797 menu_ypos -= child_allocation.height;
1800 children = children->next;
1803 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1804 menu_xpos = menu_xpos + allocation.width - menu_width;
1806 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1807 menu_xpos, menu_ypos,
1808 &menu_xpos, &menu_ypos);
1810 /* Clamp the position on screen */
1811 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1815 else if ((menu_xpos + menu_width) > screen_width)
1816 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1825 gtk_combo_box_menu_position (GtkMenu *menu,
1831 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1832 GtkComboBoxPrivate *priv = combo_box->priv;
1833 GtkWidget *menu_item;
1835 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1836 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1839 /* FIXME handle nested menus better */
1840 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1842 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1845 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1848 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1849 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1850 GDK_WINDOW_TYPE_HINT_COMBO);
1854 gtk_combo_box_list_position (GtkComboBox *combo_box,
1860 GtkComboBoxPrivate *priv = combo_box->priv;
1861 GtkAllocation allocation;
1864 GdkRectangle monitor;
1865 GtkRequisition popup_req;
1866 GtkPolicyType hpolicy, vpolicy;
1869 /* under windows, the drop down list is as wide as the combo box itself.
1871 GtkWidget *widget = GTK_WIDGET (combo_box);
1875 gtk_widget_get_allocation (widget, &allocation);
1877 if (!gtk_widget_get_has_window (widget))
1883 window = gtk_widget_get_window (widget);
1885 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1888 *width = allocation.width;
1890 hpolicy = vpolicy = GTK_POLICY_NEVER;
1891 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1894 if (combo_box->priv->popup_fixed_width)
1896 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1898 if (popup_req.width > *width)
1900 hpolicy = GTK_POLICY_ALWAYS;
1901 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1907 /* XXX This code depends on treeviews properly reporting their natural width
1908 * list-mode menus won't fill up to their natural width until then */
1909 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1911 if (popup_req.width > *width)
1913 hpolicy = GTK_POLICY_NEVER;
1914 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1917 *width = popup_req.width;
1921 *height = popup_req.height;
1923 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1924 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1925 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1927 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1928 *x = *x + allocation.width - *width;
1932 else if (*x + *width > monitor.x + monitor.width)
1933 *x = monitor.x + monitor.width - *width;
1935 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1936 *y += allocation.height;
1937 else if (*y - *height >= monitor.y)
1939 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1941 *y += allocation.height;
1942 *height = monitor.y + monitor.height - *y;
1946 *height = *y - monitor.y;
1950 if (popup_req.height > *height)
1952 vpolicy = GTK_POLICY_ALWAYS;
1954 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1960 cell_view_is_sensitive (GtkCellView *cell_view)
1962 GList *cells, *list;
1965 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1968 for (list = cells; list; list = list->next)
1970 g_object_get (list->data, "sensitive", &sensitive, NULL);
1975 g_list_free (cells);
1981 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1984 GtkComboBoxPrivate *priv = combo_box->priv;
1985 GList *cells, *list;
1991 if (priv->row_separator_func)
1993 if (priv->row_separator_func (priv->model, iter,
1994 priv->row_separator_data))
1998 gtk_tree_view_column_cell_set_cell_data (priv->column,
2000 iter, FALSE, FALSE);
2002 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
2005 for (list = cells; list; list = list->next)
2007 g_object_get (list->data, "sensitive", &sensitive, NULL);
2012 g_list_free (cells);
2018 update_menu_sensitivity (GtkComboBox *combo_box,
2021 GtkComboBoxPrivate *priv = combo_box->priv;
2022 GList *children, *child;
2023 GtkWidget *item, *submenu, *separator;
2024 GtkWidget *cell_view;
2030 children = gtk_container_get_children (GTK_CONTAINER (menu));
2032 for (child = children; child; child = child->next)
2034 item = GTK_WIDGET (child->data);
2035 cell_view = gtk_bin_get_child (GTK_BIN (item));
2037 if (!GTK_IS_CELL_VIEW (cell_view))
2040 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2041 if (submenu != NULL)
2043 gtk_widget_set_sensitive (item, TRUE);
2044 update_menu_sensitivity (combo_box, submenu);
2048 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
2050 if (menu != priv->popup_widget && child == children)
2052 separator = GTK_WIDGET (child->next->data);
2053 g_object_set (item, "visible", sensitive, NULL);
2054 g_object_set (separator, "visible", sensitive, NULL);
2057 gtk_widget_set_sensitive (item, sensitive);
2061 g_list_free (children);
2065 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2067 guint32 activate_time)
2069 GtkComboBoxPrivate *priv = combo_box->priv;
2072 gint width, min_width, nat_width;
2074 update_menu_sensitivity (combo_box, priv->popup_widget);
2077 if (gtk_tree_row_reference_valid (priv->active_row))
2079 path = gtk_tree_row_reference_get_path (priv->active_row);
2080 active_item = gtk_tree_path_get_indices (path)[0];
2081 gtk_tree_path_free (path);
2083 if (priv->add_tearoffs)
2087 /* FIXME handle nested menus better */
2088 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2090 if (priv->wrap_width == 0)
2092 GtkAllocation allocation;
2094 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2095 width = allocation.width;
2096 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2097 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2099 if (combo_box->priv->popup_fixed_width)
2100 width = MAX (width, min_width);
2102 width = MAX (width, nat_width);
2104 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2107 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2109 gtk_combo_box_menu_position, combo_box,
2110 button, activate_time);
2114 popup_grab_on_window (GdkWindow *window,
2115 GdkDevice *keyboard,
2117 guint32 activate_time)
2120 gdk_device_grab (keyboard, window,
2121 GDK_OWNERSHIP_WINDOW, TRUE,
2122 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2123 NULL, activate_time) != GDK_GRAB_SUCCESS)
2127 gdk_device_grab (pointer, window,
2128 GDK_OWNERSHIP_WINDOW, TRUE,
2129 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2130 GDK_POINTER_MOTION_MASK,
2131 NULL, activate_time) != GDK_GRAB_SUCCESS)
2134 gdk_device_ungrab (keyboard, activate_time);
2143 * gtk_combo_box_popup:
2144 * @combo_box: a #GtkComboBox
2146 * Pops up the menu or dropdown list of @combo_box.
2148 * This function is mostly intended for use by accessibility technologies;
2149 * applications should have little use for it.
2154 gtk_combo_box_popup (GtkComboBox *combo_box)
2156 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2158 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2162 * gtk_combo_box_popup_for_device:
2163 * @combo_box: a #GtkComboBox
2164 * @device: a #GdkDevice
2166 * Pops up the menu or dropdown list of @combo_box, the popup window
2167 * will be grabbed so only @device and its associated pointer/keyboard
2168 * are the only #GdkDevice<!-- -->s able to send events to it.
2173 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2176 GtkComboBoxPrivate *priv = combo_box->priv;
2177 gint x, y, width, height;
2178 GtkTreePath *path = NULL, *ppath;
2179 GtkWidget *toplevel;
2180 GdkDevice *keyboard, *pointer;
2183 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2184 g_return_if_fail (GDK_IS_DEVICE (device));
2186 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2189 if (gtk_widget_get_mapped (priv->popup_widget))
2192 if (priv->grab_pointer && priv->grab_keyboard)
2195 time = gtk_get_current_event_time ();
2197 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2200 pointer = gdk_device_get_associated_device (device);
2205 keyboard = gdk_device_get_associated_device (device);
2208 if (GTK_IS_MENU (priv->popup_widget))
2210 gtk_combo_box_menu_popup (combo_box,
2211 priv->activate_button,
2212 priv->activate_time);
2216 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2217 if (GTK_IS_WINDOW (toplevel))
2218 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2219 GTK_WINDOW (priv->popup_window));
2221 gtk_widget_show_all (priv->scrolled_window);
2222 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2224 gtk_widget_set_size_request (priv->popup_window, width, height);
2225 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2227 if (gtk_tree_row_reference_valid (priv->active_row))
2229 path = gtk_tree_row_reference_get_path (priv->active_row);
2230 ppath = gtk_tree_path_copy (path);
2231 if (gtk_tree_path_up (ppath))
2232 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2234 gtk_tree_path_free (ppath);
2236 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2240 gtk_widget_show (priv->popup_window);
2244 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2246 gtk_tree_path_free (path);
2249 gtk_widget_grab_focus (priv->popup_window);
2250 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2253 if (!gtk_widget_has_focus (priv->tree_view))
2254 gtk_widget_grab_focus (priv->tree_view);
2256 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2257 keyboard, pointer, time))
2259 gtk_widget_hide (priv->popup_window);
2263 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2264 priv->grab_pointer = pointer;
2265 priv->grab_keyboard = keyboard;
2269 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2273 device = gtk_get_current_event_device ();
2277 GdkDeviceManager *device_manager;
2278 GdkDisplay *display;
2281 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2282 device_manager = gdk_display_get_device_manager (display);
2284 /* No device was set, pick the first master device */
2285 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2286 device = devices->data;
2287 g_list_free (devices);
2290 gtk_combo_box_popup_for_device (combo_box, device);
2294 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2296 if (combo_box->priv->popup_shown)
2298 gtk_combo_box_popdown (combo_box);
2306 * gtk_combo_box_popdown:
2307 * @combo_box: a #GtkComboBox
2309 * Hides the menu or dropdown list of @combo_box.
2311 * This function is mostly intended for use by accessibility technologies;
2312 * applications should have little use for it.
2317 gtk_combo_box_popdown (GtkComboBox *combo_box)
2319 GtkComboBoxPrivate *priv = combo_box->priv;
2321 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2323 if (GTK_IS_MENU (priv->popup_widget))
2325 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2329 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2332 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2333 gtk_widget_hide (priv->popup_window);
2334 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2337 priv->grab_pointer = NULL;
2338 priv->grab_keyboard = NULL;
2341 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2342 gtk_widget_get_preferred_size (combo_box->priv->button, \
2346 child.x = allocation->x + border.right; \
2348 child.x = allocation->x + allocation->width - req.width - border.left; \
2350 child.y = allocation->y + border.top; \
2351 child.width = req.width; \
2352 child.height = allocation->height - (border.top + border.bottom); \
2353 child.width = MAX (1, child.width); \
2354 child.height = MAX (1, child.height); \
2356 gtk_widget_size_allocate (combo_box->priv->button, &child);
2360 gtk_combo_box_size_allocate (GtkWidget *widget,
2361 GtkAllocation *allocation)
2363 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2364 GtkComboBoxPrivate *priv = combo_box->priv;
2365 GtkWidget *child_widget;
2366 gint focus_width, focus_pad;
2367 GtkAllocation child;
2369 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2372 gtk_widget_set_allocation (widget, allocation);
2373 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2374 get_widget_border (widget, &border);
2376 gtk_widget_style_get (widget,
2377 "focus-line-width", &focus_width,
2378 "focus-padding", &focus_pad,
2381 if (!priv->tree_view)
2383 if (priv->cell_view)
2385 GtkBorder button_border;
2390 allocation->x += border.left;
2391 allocation->y += border.top;
2392 allocation->width -= border.left + border.right;
2393 allocation->height -= border.top + border.bottom;
2395 gtk_widget_size_allocate (priv->button, allocation);
2397 /* set some things ready */
2398 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2399 get_widget_border (priv->button, &button_border);
2401 child.x = allocation->x;
2402 child.y = allocation->y;
2403 width = allocation->width;
2404 child.height = allocation->height;
2406 if (!priv->is_cell_renderer)
2408 child.x += border_width + button_border.left + focus_width + focus_pad;
2409 child.y += border_width + button_border.top + focus_width + focus_pad;
2410 width -= (2 * (border_width + focus_width + focus_pad)) +
2411 button_border.left + button_border.right;
2412 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2413 button_border.top + button_border.bottom;
2416 /* handle the children */
2417 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2418 child.width = req.width;
2420 child.x += width - req.width;
2421 child.width = MAX (1, child.width);
2422 child.height = MAX (1, child.height);
2423 gtk_widget_size_allocate (priv->arrow, &child);
2425 child.x += req.width;
2426 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2427 child.width = req.width;
2429 child.x -= req.width;
2430 child.width = MAX (1, child.width);
2431 child.height = MAX (1, child.height);
2432 gtk_widget_size_allocate (priv->separator, &child);
2436 child.x += req.width;
2437 child.width = allocation->x + allocation->width
2438 - (border_width + button_border.right + focus_width + focus_pad)
2443 child.width = child.x;
2444 child.x = allocation->x
2445 + border_width + button_border.left + focus_width + focus_pad;
2446 child.width -= child.x;
2449 if (gtk_widget_get_visible (priv->popup_widget))
2451 gint width, menu_width;
2453 if (priv->wrap_width == 0)
2455 GtkAllocation combo_box_allocation;
2457 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2458 width = combo_box_allocation.width;
2459 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2461 if (combo_box->priv->popup_fixed_width)
2462 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2464 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2466 gtk_widget_set_size_request (priv->popup_widget,
2467 MAX (width, menu_width), -1);
2470 /* reposition the menu after giving it a new width */
2471 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2474 child.width = MAX (1, child.width);
2475 child.height = MAX (1, child.height);
2476 gtk_widget_size_allocate (child_widget, &child);
2480 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2483 child.x = allocation->x + req.width + border.right;
2485 child.x = allocation->x + border.left;
2486 child.y = allocation->y + border.top;
2487 child.width = allocation->width - req.width - (border.left + border.right);
2488 child.width = MAX (1, child.width);
2489 child.height = MAX (1, child.height);
2490 gtk_widget_size_allocate (child_widget, &child);
2496 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2499 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2503 child.x = allocation->x + req.width;
2505 child.x = allocation->x;
2507 child.y = allocation->y;
2508 child.width = allocation->width - req.width;
2509 child.height = allocation->height;
2511 if (priv->cell_view_frame)
2513 child.x += border.left + border_width;
2514 child.y += border.top + border_width;
2515 child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right));
2516 child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom));
2517 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2520 if (priv->has_frame)
2522 GtkBorder frame_border;
2524 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2525 get_widget_border (priv->cell_view_frame, &frame_border);
2527 child.x += border_width + frame_border.left;
2528 child.y += border_width + frame_border.right;
2529 child.width -= (2 * border_width) + frame_border.left + frame_border.right;
2530 child.height -= (2 * border_width) + frame_border.top + frame_border.bottom;
2535 child.x += border.left + border_width;
2536 child.y += border.top + border_width;
2537 child.width -= (2 * border_width) - (border.left + border.right);
2538 child.height -= (2 * border_width) - (border.top + border.bottom);
2541 if (gtk_widget_get_visible (priv->popup_window))
2543 gint x, y, width, height;
2544 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2545 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2546 gtk_widget_set_size_request (priv->popup_window, width, height);
2550 child.width = MAX (1, child.width);
2551 child.height = MAX (1, child.height);
2553 gtk_widget_size_allocate (child_widget, &child);
2557 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2560 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2562 GtkComboBoxPrivate *priv = combo_box->priv;
2566 g_signal_handler_disconnect (priv->model,
2568 g_signal_handler_disconnect (priv->model,
2570 g_signal_handler_disconnect (priv->model,
2571 priv->reordered_id);
2572 g_signal_handler_disconnect (priv->model,
2578 g_object_unref (priv->model);
2582 if (priv->active_row)
2584 gtk_tree_row_reference_free (priv->active_row);
2585 priv->active_row = NULL;
2588 if (priv->cell_view)
2589 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2593 gtk_combo_box_forall (GtkContainer *container,
2594 gboolean include_internals,
2595 GtkCallback callback,
2596 gpointer callback_data)
2598 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2599 GtkComboBoxPrivate *priv = combo_box->priv;
2602 if (include_internals)
2605 (* callback) (priv->button, callback_data);
2606 if (priv->cell_view_frame)
2607 (* callback) (priv->cell_view_frame, callback_data);
2610 child = gtk_bin_get_child (GTK_BIN (container));
2612 (* callback) (child, callback_data);
2616 gtk_combo_box_child_show (GtkWidget *widget,
2617 GtkComboBox *combo_box)
2619 GtkComboBoxPrivate *priv = combo_box->priv;
2621 priv->popup_shown = TRUE;
2622 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2626 gtk_combo_box_child_hide (GtkWidget *widget,
2627 GtkComboBox *combo_box)
2629 GtkComboBoxPrivate *priv = combo_box->priv;
2631 priv->popup_shown = FALSE;
2632 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2636 gtk_combo_box_draw (GtkWidget *widget,
2639 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2640 GtkComboBoxPrivate *priv = combo_box->priv;
2642 if (priv->shadow_type != GTK_SHADOW_NONE)
2644 GtkStyleContext *context;
2645 GtkStateFlags state;
2647 context = gtk_widget_get_style_context (widget);
2648 state = gtk_widget_get_state_flags (widget);
2649 gtk_style_context_set_state (context, state);
2651 gtk_render_background (context, cr, 0, 0,
2652 gtk_widget_get_allocated_width (widget),
2653 gtk_widget_get_allocated_height (widget));
2654 gtk_render_frame (context, cr, 0, 0,
2655 gtk_widget_get_allocated_width (widget),
2656 gtk_widget_get_allocated_height (widget));
2659 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2662 if (priv->tree_view && priv->cell_view_frame)
2664 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2665 priv->cell_view_frame, cr);
2668 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2669 gtk_bin_get_child (GTK_BIN (widget)),
2685 path_visible (GtkTreeView *view,
2691 /* Note that we rely on the fact that collapsed rows don't have nodes
2693 return _gtk_tree_view_find_node (view, path, &tree, &node);
2697 tree_next_func (GtkTreeModel *model,
2702 SearchData *search_data = (SearchData *)data;
2704 if (search_data->found)
2706 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2709 if (search_data->visible &&
2710 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2713 search_data->set = TRUE;
2714 search_data->iter = *iter;
2719 if (gtk_tree_path_compare (path, search_data->path) == 0)
2720 search_data->found = TRUE;
2726 tree_next (GtkComboBox *combo,
2727 GtkTreeModel *model,
2732 SearchData search_data;
2734 search_data.combo = combo;
2735 search_data.path = gtk_tree_model_get_path (model, iter);
2736 search_data.visible = visible;
2737 search_data.found = FALSE;
2738 search_data.set = FALSE;
2740 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2742 *next = search_data.iter;
2744 gtk_tree_path_free (search_data.path);
2746 return search_data.set;
2750 tree_prev_func (GtkTreeModel *model,
2755 SearchData *search_data = (SearchData *)data;
2757 if (gtk_tree_path_compare (path, search_data->path) == 0)
2759 search_data->found = TRUE;
2763 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2766 if (search_data->visible &&
2767 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2770 search_data->set = TRUE;
2771 search_data->iter = *iter;
2777 tree_prev (GtkComboBox *combo,
2778 GtkTreeModel *model,
2783 SearchData search_data;
2785 search_data.combo = combo;
2786 search_data.path = gtk_tree_model_get_path (model, iter);
2787 search_data.visible = visible;
2788 search_data.found = FALSE;
2789 search_data.set = FALSE;
2791 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2793 *prev = search_data.iter;
2795 gtk_tree_path_free (search_data.path);
2797 return search_data.set;
2801 tree_last_func (GtkTreeModel *model,
2806 SearchData *search_data = (SearchData *)data;
2808 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2811 /* Note that we rely on the fact that collapsed rows don't have nodes
2813 if (search_data->visible &&
2814 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2817 search_data->set = TRUE;
2818 search_data->iter = *iter;
2824 tree_last (GtkComboBox *combo,
2825 GtkTreeModel *model,
2829 SearchData search_data;
2831 search_data.combo = combo;
2832 search_data.visible = visible;
2833 search_data.set = FALSE;
2835 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2837 *last = search_data.iter;
2839 return search_data.set;
2844 tree_first_func (GtkTreeModel *model,
2849 SearchData *search_data = (SearchData *)data;
2851 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2854 if (search_data->visible &&
2855 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2858 search_data->set = TRUE;
2859 search_data->iter = *iter;
2865 tree_first (GtkComboBox *combo,
2866 GtkTreeModel *model,
2870 SearchData search_data;
2872 search_data.combo = combo;
2873 search_data.visible = visible;
2874 search_data.set = FALSE;
2876 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2878 *first = search_data.iter;
2880 return search_data.set;
2884 gtk_combo_box_scroll_event (GtkWidget *widget,
2885 GdkEventScroll *event)
2887 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2890 GtkTreeIter new_iter;
2892 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2895 if (event->direction == GDK_SCROLL_UP)
2896 found = tree_prev (combo_box, combo_box->priv->model,
2897 &iter, &new_iter, FALSE);
2899 found = tree_next (combo_box, combo_box->priv->model,
2900 &iter, &new_iter, FALSE);
2903 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2913 gtk_combo_box_row_separator_func (GtkTreeModel *model,
2917 GtkComboBoxPrivate *priv = combo->priv;
2919 if (priv->row_separator_func)
2920 return priv->row_separator_func (model, iter, priv->row_separator_data);
2926 gtk_combo_box_header_func (GtkTreeModel *model,
2930 /* Every submenu has a selectable header, however we
2931 * can expose a method to make that configurable by
2932 * the user (like row_separator_func is done) */
2937 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2938 gboolean add_children)
2940 GtkComboBoxPrivate *priv = combo_box->priv;
2944 child = gtk_bin_get_child (GTK_BIN (combo_box));
2946 if (priv->cell_view)
2948 priv->button = gtk_toggle_button_new ();
2949 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2950 priv->focus_on_click);
2952 g_signal_connect (priv->button, "toggled",
2953 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2954 gtk_widget_set_parent (priv->button,
2955 gtk_widget_get_parent (child));
2957 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2958 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2960 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
2961 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2963 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2964 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2966 gtk_widget_show_all (priv->button);
2970 priv->button = gtk_toggle_button_new ();
2971 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2972 priv->focus_on_click);
2974 g_signal_connect (priv->button, "toggled",
2975 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2976 gtk_widget_set_parent (priv->button,
2977 gtk_widget_get_parent (child));
2979 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2980 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2981 gtk_widget_show_all (priv->button);
2984 g_signal_connect (priv->button, "button-press-event",
2985 G_CALLBACK (gtk_combo_box_menu_button_press),
2987 g_signal_connect (priv->button, "state-flags-changed",
2988 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
2991 /* create our funky menu */
2992 menu = _gtk_tree_menu_new_with_area (priv->area);
2993 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2995 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
2997 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
2998 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
2999 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3000 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3001 combo_box->priv->add_tearoffs);
3003 g_signal_connect (menu, "menu-activate",
3004 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3006 /* Chain our row_separator_func through */
3007 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3008 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3011 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3012 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3015 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3017 g_signal_connect (menu, "key-press-event",
3018 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3019 gtk_combo_box_set_popup_widget (combo_box, menu);
3021 gtk_combo_box_update_title (combo_box);
3025 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3027 GtkComboBoxPrivate *priv = combo_box->priv;
3029 g_signal_handlers_disconnect_matched (priv->button,
3030 G_SIGNAL_MATCH_DATA,
3032 gtk_combo_box_menu_button_press, NULL);
3033 g_signal_handlers_disconnect_matched (priv->button,
3034 G_SIGNAL_MATCH_DATA,
3036 gtk_combo_box_button_state_flags_changed, combo_box);
3037 g_signal_handlers_disconnect_matched (priv->popup_widget,
3038 G_SIGNAL_MATCH_DATA,
3040 gtk_combo_box_menu_activate, combo_box);
3042 /* unparent will remove our latest ref */
3043 gtk_widget_unparent (priv->button);
3046 priv->button = NULL;
3048 priv->separator = NULL;
3050 priv->column = NULL;
3052 /* changing the popup window will unref the menu and the children */
3057 gtk_combo_box_menu_button_press (GtkWidget *widget,
3058 GdkEventButton *event,
3061 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3062 GtkComboBoxPrivate *priv = combo_box->priv;
3064 if (GTK_IS_MENU (priv->popup_widget) &&
3065 event->type == GDK_BUTTON_PRESS && event->button == 1)
3067 if (priv->focus_on_click &&
3068 !gtk_widget_has_focus (priv->button))
3069 gtk_widget_grab_focus (priv->button);
3071 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3080 gtk_combo_box_menu_activate (GtkWidget *menu,
3082 GtkComboBox *combo_box)
3086 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3087 gtk_combo_box_set_active_iter (combo_box, &iter);
3089 g_object_set (combo_box,
3090 "editing-canceled", FALSE,
3096 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3099 gboolean sensitive = TRUE; /* fool code checkers */
3101 if (!combo_box->priv->button)
3104 switch (combo_box->priv->button_sensitivity)
3106 case GTK_SENSITIVITY_ON:
3109 case GTK_SENSITIVITY_OFF:
3112 case GTK_SENSITIVITY_AUTO:
3113 sensitive = combo_box->priv->model &&
3114 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3117 g_assert_not_reached ();
3121 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3123 /* In list-mode, we also need to update sensitivity of the event box */
3124 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3125 && combo_box->priv->cell_view)
3126 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3130 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3135 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3137 if (combo_box->priv->tree_view)
3138 gtk_combo_box_list_popup_resize (combo_box);
3140 gtk_combo_box_update_sensitivity (combo_box);
3144 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3148 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3149 GtkComboBoxPrivate *priv = combo_box->priv;
3151 if (!gtk_tree_row_reference_valid (priv->active_row))
3153 if (priv->cell_view)
3154 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3155 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3158 if (priv->tree_view)
3159 gtk_combo_box_list_popup_resize (combo_box);
3161 gtk_combo_box_update_sensitivity (combo_box);
3165 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3171 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3175 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3180 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3181 GtkComboBoxPrivate *priv = combo_box->priv;
3182 GtkTreePath *active_path;
3184 /* FIXME this belongs to GtkCellView */
3185 if (gtk_tree_row_reference_valid (priv->active_row))
3187 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3188 if (gtk_tree_path_compare (path, active_path) == 0 &&
3190 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3191 gtk_tree_path_free (active_path);
3194 if (priv->tree_view)
3195 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3199 list_popup_resize_idle (gpointer user_data)
3201 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3202 GtkComboBoxPrivate *priv = combo_box->priv;
3203 gint x, y, width, height;
3205 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3207 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3209 gtk_widget_set_size_request (priv->popup_window, width, height);
3210 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3213 priv->resize_idle_id = 0;
3219 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3221 GtkComboBoxPrivate *priv = combo_box->priv;
3223 if (!priv->resize_idle_id)
3224 priv->resize_idle_id =
3225 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3229 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3234 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3236 gtk_combo_box_list_popup_resize (combo_box);
3245 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3247 GtkComboBoxPrivate *priv = combo_box->priv;
3248 GtkTreeSelection *sel;
3250 GtkWidget *widget = GTK_WIDGET (combo_box);
3252 priv->button = gtk_toggle_button_new ();
3253 child = gtk_bin_get_child (GTK_BIN (combo_box));
3254 gtk_widget_set_parent (priv->button,
3255 gtk_widget_get_parent (child));
3256 g_signal_connect (priv->button, "button-press-event",
3257 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3258 g_signal_connect (priv->button, "toggled",
3259 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3261 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3262 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3263 priv->separator = NULL;
3264 gtk_widget_show_all (priv->button);
3266 if (priv->cell_view)
3268 GtkStyleContext *context;
3269 GtkStateFlags state;
3272 context = gtk_widget_get_style_context (widget);
3273 state = gtk_widget_get_state_flags (widget);
3275 gtk_style_context_get (context, state,
3276 "background-color", &color,
3279 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
3280 gdk_rgba_free (color);
3282 priv->box = gtk_event_box_new ();
3283 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3286 if (priv->has_frame)
3288 priv->cell_view_frame = gtk_frame_new (NULL);
3289 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3294 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3295 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3299 gtk_widget_set_parent (priv->cell_view_frame,
3300 gtk_widget_get_parent (child));
3301 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3302 gtk_widget_show_all (priv->cell_view_frame);
3304 g_signal_connect (priv->box, "button-press-event",
3305 G_CALLBACK (gtk_combo_box_list_button_pressed),
3309 priv->tree_view = gtk_tree_view_new ();
3310 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3311 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3312 gtk_tree_selection_set_select_function (sel,
3313 gtk_combo_box_list_select_func,
3315 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3317 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3320 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3321 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3325 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3327 priv->column = gtk_tree_view_column_new_with_area (priv->area);
3328 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3330 if (gtk_tree_row_reference_valid (priv->active_row))
3334 path = gtk_tree_row_reference_get_path (priv->active_row);
3335 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3337 gtk_tree_path_free (path);
3340 /* set sample/popup widgets */
3341 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3343 g_signal_connect (priv->tree_view, "key-press-event",
3344 G_CALLBACK (gtk_combo_box_list_key_press),
3346 g_signal_connect (priv->tree_view, "enter-notify-event",
3347 G_CALLBACK (gtk_combo_box_list_enter_notify),
3349 g_signal_connect (priv->tree_view, "row-expanded",
3350 G_CALLBACK (gtk_combo_box_model_row_expanded),
3352 g_signal_connect (priv->tree_view, "row-collapsed",
3353 G_CALLBACK (gtk_combo_box_model_row_expanded),
3355 g_signal_connect (priv->popup_window, "button-press-event",
3356 G_CALLBACK (gtk_combo_box_list_button_pressed),
3358 g_signal_connect (priv->popup_window, "button-release-event",
3359 G_CALLBACK (gtk_combo_box_list_button_released),
3362 gtk_widget_show (priv->tree_view);
3364 gtk_combo_box_update_sensitivity (combo_box);
3368 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3370 GtkComboBoxPrivate *priv = combo_box->priv;
3372 /* disconnect signals */
3373 g_signal_handlers_disconnect_matched (priv->tree_view,
3374 G_SIGNAL_MATCH_DATA,
3375 0, 0, NULL, NULL, combo_box);
3376 g_signal_handlers_disconnect_matched (priv->button,
3377 G_SIGNAL_MATCH_DATA,
3379 gtk_combo_box_list_button_pressed,
3381 g_signal_handlers_disconnect_matched (priv->popup_window,
3382 G_SIGNAL_MATCH_DATA,
3384 gtk_combo_box_list_button_pressed,
3386 g_signal_handlers_disconnect_matched (priv->popup_window,
3387 G_SIGNAL_MATCH_DATA,
3389 gtk_combo_box_list_button_released,
3392 g_signal_handlers_disconnect_matched (priv->popup_window,
3393 G_SIGNAL_MATCH_DATA,
3395 gtk_combo_box_child_show,
3398 g_signal_handlers_disconnect_matched (priv->popup_window,
3399 G_SIGNAL_MATCH_DATA,
3401 gtk_combo_box_child_hide,
3405 g_signal_handlers_disconnect_matched (priv->box,
3406 G_SIGNAL_MATCH_DATA,
3408 gtk_combo_box_list_button_pressed,
3411 /* destroy things (unparent will kill the latest ref from us)
3412 * last unref on button will destroy the arrow
3414 gtk_widget_unparent (priv->button);
3415 priv->button = NULL;
3418 if (priv->cell_view)
3420 g_object_set (priv->cell_view,
3421 "background-set", FALSE,
3425 if (priv->cell_view_frame)
3427 gtk_widget_unparent (priv->cell_view_frame);
3428 priv->cell_view_frame = NULL;
3432 if (priv->scroll_timer)
3434 g_source_remove (priv->scroll_timer);
3435 priv->scroll_timer = 0;
3438 if (priv->resize_idle_id)
3440 g_source_remove (priv->resize_idle_id);
3441 priv->resize_idle_id = 0;
3444 gtk_widget_destroy (priv->tree_view);
3446 priv->tree_view = NULL;
3447 if (priv->popup_widget)
3449 g_object_unref (priv->popup_widget);
3450 priv->popup_widget = NULL;
3457 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3458 GdkEventButton *event,
3461 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3462 GtkComboBoxPrivate *priv = combo_box->priv;
3464 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3466 if (ewidget == priv->popup_window)
3469 if ((ewidget != priv->button && ewidget != priv->box) ||
3470 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3473 if (priv->focus_on_click &&
3474 !gtk_widget_has_focus (priv->button))
3475 gtk_widget_grab_focus (priv->button);
3477 gtk_combo_box_popup_for_device (combo_box, event->device);
3479 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3481 priv->auto_scroll = FALSE;
3482 if (priv->scroll_timer == 0)
3483 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3484 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3487 priv->popup_in_progress = TRUE;
3493 gtk_combo_box_list_button_released (GtkWidget *widget,
3494 GdkEventButton *event,
3498 GtkTreePath *path = NULL;
3501 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3502 GtkComboBoxPrivate *priv = combo_box->priv;
3504 gboolean popup_in_progress = FALSE;
3506 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3508 if (priv->popup_in_progress)
3510 popup_in_progress = TRUE;
3511 priv->popup_in_progress = FALSE;
3514 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3516 if (priv->scroll_timer)
3518 g_source_remove (priv->scroll_timer);
3519 priv->scroll_timer = 0;
3522 if (ewidget != priv->tree_view)
3524 if ((ewidget == priv->button ||
3525 ewidget == priv->box) &&
3526 !popup_in_progress &&
3527 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3529 gtk_combo_box_popdown (combo_box);
3533 /* released outside treeview */
3534 if (ewidget != priv->button && ewidget != priv->box)
3536 gtk_combo_box_popdown (combo_box);
3544 /* select something cool */
3545 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3551 return TRUE; /* clicked outside window? */
3553 gtk_tree_model_get_iter (priv->model, &iter, path);
3554 gtk_tree_path_free (path);
3556 gtk_combo_box_popdown (combo_box);
3558 if (tree_column_row_is_sensitive (combo_box, &iter))
3559 gtk_combo_box_set_active_iter (combo_box, &iter);
3565 gtk_combo_box_menu_key_press (GtkWidget *widget,
3569 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3571 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3573 /* The menu hasn't managed the
3574 * event, forward it to the combobox
3576 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3583 gtk_combo_box_list_key_press (GtkWidget *widget,
3587 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3590 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3591 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3593 GtkTreeModel *model = NULL;
3595 gtk_combo_box_popdown (combo_box);
3597 if (combo_box->priv->model)
3599 GtkTreeSelection *sel;
3601 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3603 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3604 gtk_combo_box_set_active_iter (combo_box, &iter);
3610 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3612 /* The list hasn't managed the
3613 * event, forward it to the combobox
3615 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3622 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3627 GtkAllocation allocation;
3628 GtkWidget *tree_view = combo_box->priv->tree_view;
3631 gtk_widget_get_allocation (tree_view, &allocation);
3633 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3634 if (adj && adj->upper - adj->lower > adj->page_size)
3636 if (x <= allocation.x &&
3637 adj->lower < adj->value)
3639 value = adj->value - (allocation.x - x + 1);
3640 gtk_adjustment_set_value (adj, value);
3642 else if (x >= allocation.x + allocation.width &&
3643 adj->upper - adj->page_size > adj->value)
3645 value = adj->value + (x - allocation.x - allocation.width + 1);
3646 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3650 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3651 if (adj && adj->upper - adj->lower > adj->page_size)
3653 if (y <= allocation.y &&
3654 adj->lower < adj->value)
3656 value = adj->value - (allocation.y - y + 1);
3657 gtk_adjustment_set_value (adj, value);
3659 else if (y >= allocation.height &&
3660 adj->upper - adj->page_size > adj->value)
3662 value = adj->value + (y - allocation.height + 1);
3663 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3669 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3671 GtkComboBoxPrivate *priv = combo_box->priv;
3674 if (priv->auto_scroll)
3676 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3679 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3686 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3687 GdkEventCrossing *event,
3690 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3692 combo_box->priv->auto_scroll = TRUE;
3698 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3699 GtkTreeModel *model,
3701 gboolean path_currently_selected,
3704 GList *list, *columns;
3705 gboolean sensitive = FALSE;
3707 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3709 for (list = columns; list && !sensitive; list = list->next)
3711 GList *cells, *cell;
3712 gboolean cell_sensitive, cell_visible;
3714 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3716 if (!gtk_tree_view_column_get_visible (column))
3719 gtk_tree_model_get_iter (model, &iter, path);
3720 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3723 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3726 g_object_get (cell->data,
3727 "sensitive", &cell_sensitive,
3728 "visible", &cell_visible,
3731 if (cell_visible && cell_sensitive)
3736 g_list_free (cells);
3738 sensitive = cell_sensitive;
3741 g_list_free (columns);
3747 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3752 /* XXX Do nothing ? */
3756 * GtkCellLayout implementation
3759 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3761 return GTK_COMBO_BOX (cell_layout)->priv->area;
3769 * gtk_combo_box_new:
3771 * Creates a new empty #GtkComboBox.
3773 * Return value: A new #GtkComboBox.
3778 gtk_combo_box_new (void)
3780 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3784 * gtk_combo_box_new_with_area:
3785 * @area: the #GtkCellArea to use to layout cell renderers
3787 * Creates a new empty #GtkComboBox using @area to layout cells.
3789 * Return value: A new #GtkComboBox.
3792 gtk_combo_box_new_with_area (GtkCellArea *area)
3794 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3798 * gtk_combo_box_new_with_area_and_entry:
3799 * @area: the #GtkCellArea to use to layout cell renderers
3801 * Creates a new empty #GtkComboBox with an entry.
3803 * The new combo box will use @area to layout cells.
3805 * Return value: A new #GtkComboBox.
3808 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3810 return g_object_new (GTK_TYPE_COMBO_BOX,
3818 * gtk_combo_box_new_with_entry:
3820 * Creates a new empty #GtkComboBox with an entry.
3822 * Return value: A new #GtkComboBox.
3825 gtk_combo_box_new_with_entry (void)
3827 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3831 * gtk_combo_box_new_with_model:
3832 * @model: A #GtkTreeModel.
3834 * Creates a new #GtkComboBox with the model initialized to @model.
3836 * Return value: A new #GtkComboBox.
3841 gtk_combo_box_new_with_model (GtkTreeModel *model)
3843 GtkComboBox *combo_box;
3845 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3847 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3849 return GTK_WIDGET (combo_box);
3853 * gtk_combo_box_new_with_model_and_entry:
3854 * @model: A #GtkTreeModel
3856 * Creates a new empty #GtkComboBox with an entry
3857 * and with the model initialized to @model.
3859 * Return value: A new #GtkComboBox
3862 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3864 return g_object_new (GTK_TYPE_COMBO_BOX,
3871 * gtk_combo_box_get_wrap_width:
3872 * @combo_box: A #GtkComboBox
3874 * Returns the wrap width which is used to determine the number of columns
3875 * for the popup menu. If the wrap width is larger than 1, the combo box
3878 * Returns: the wrap width.
3883 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3885 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3887 return combo_box->priv->wrap_width;
3891 * gtk_combo_box_set_wrap_width:
3892 * @combo_box: A #GtkComboBox
3893 * @width: Preferred number of columns
3895 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3896 * the preferred number of columns when you want the popup to be layed out
3902 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3905 GtkComboBoxPrivate *priv;
3907 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3908 g_return_if_fail (width >= 0);
3910 priv = combo_box->priv;
3912 if (width != priv->wrap_width)
3914 priv->wrap_width = width;
3916 gtk_combo_box_check_appearance (combo_box);
3918 if (GTK_IS_TREE_MENU (priv->popup_widget))
3919 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
3921 g_object_notify (G_OBJECT (combo_box), "wrap-width");
3926 * gtk_combo_box_get_row_span_column:
3927 * @combo_box: A #GtkComboBox
3929 * Returns the column with row span information for @combo_box.
3931 * Returns: the row span column.
3936 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3938 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3940 return combo_box->priv->row_column;
3944 * gtk_combo_box_set_row_span_column:
3945 * @combo_box: A #GtkComboBox.
3946 * @row_span: A column in the model passed during construction.
3948 * Sets the column with row span information for @combo_box to be @row_span.
3949 * The row span column contains integers which indicate how many rows
3950 * an item should span.
3955 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3958 GtkComboBoxPrivate *priv;
3961 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3963 priv = combo_box->priv;
3965 col = gtk_tree_model_get_n_columns (priv->model);
3966 g_return_if_fail (row_span >= -1 && row_span < col);
3968 if (row_span != priv->row_column)
3970 priv->row_column = row_span;
3972 if (GTK_IS_TREE_MENU (priv->popup_widget))
3973 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
3975 g_object_notify (G_OBJECT (combo_box), "row-span-column");
3980 * gtk_combo_box_get_column_span_column:
3981 * @combo_box: A #GtkComboBox
3983 * Returns the column with column span information for @combo_box.
3985 * Returns: the column span column.
3990 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
3992 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3994 return combo_box->priv->col_column;
3998 * gtk_combo_box_set_column_span_column:
3999 * @combo_box: A #GtkComboBox
4000 * @column_span: A column in the model passed during construction
4002 * Sets the column with column span information for @combo_box to be
4003 * @column_span. The column span column contains integers which indicate
4004 * how many columns an item should span.
4009 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4012 GtkComboBoxPrivate *priv;
4015 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4017 priv = combo_box->priv;
4019 col = gtk_tree_model_get_n_columns (priv->model);
4020 g_return_if_fail (column_span >= -1 && column_span < col);
4022 if (column_span != priv->col_column)
4024 priv->col_column = column_span;
4026 if (GTK_IS_TREE_MENU (priv->popup_widget))
4027 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4029 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4034 * gtk_combo_box_get_active:
4035 * @combo_box: A #GtkComboBox
4037 * Returns the index of the currently active item, or -1 if there's no
4038 * active item. If the model is a non-flat treemodel, and the active item
4039 * is not an immediate child of the root of the tree, this function returns
4040 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4041 * <literal>path</literal> is the #GtkTreePath of the active item.
4043 * Return value: An integer which is the index of the currently active item,
4044 * or -1 if there's no active item.
4049 gtk_combo_box_get_active (GtkComboBox *combo_box)
4051 GtkComboBoxPrivate *priv;
4054 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4056 priv = combo_box->priv;
4058 if (gtk_tree_row_reference_valid (priv->active_row))
4062 path = gtk_tree_row_reference_get_path (priv->active_row);
4063 result = gtk_tree_path_get_indices (path)[0];
4064 gtk_tree_path_free (path);
4073 * gtk_combo_box_set_active:
4074 * @combo_box: A #GtkComboBox
4075 * @index_: An index in the model passed during construction, or -1 to have
4078 * Sets the active item of @combo_box to be the item at @index.
4083 gtk_combo_box_set_active (GtkComboBox *combo_box,
4086 GtkTreePath *path = NULL;
4087 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4088 g_return_if_fail (index_ >= -1);
4090 if (combo_box->priv->model == NULL)
4092 /* Save index, in case the model is set after the index */
4093 combo_box->priv->active = index_;
4099 path = gtk_tree_path_new_from_indices (index_, -1);
4101 gtk_combo_box_set_active_internal (combo_box, path);
4104 gtk_tree_path_free (path);
4108 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4111 GtkComboBoxPrivate *priv = combo_box->priv;
4112 GtkTreePath *active_path;
4115 /* Remember whether the initially active row is valid. */
4116 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4118 if (path && is_valid_row_reference)
4120 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4121 path_cmp = gtk_tree_path_compare (path, active_path);
4122 gtk_tree_path_free (active_path);
4127 if (priv->active_row)
4129 gtk_tree_row_reference_free (priv->active_row);
4130 priv->active_row = NULL;
4135 if (priv->tree_view)
4136 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4139 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4141 if (GTK_IS_MENU (menu))
4142 gtk_menu_set_active (menu, -1);
4145 if (priv->cell_view)
4146 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4149 * Do not emit a "changed" signal when an already invalid selection was
4150 * now set to invalid.
4152 if (!is_valid_row_reference)
4158 gtk_tree_row_reference_new (priv->model, path);
4160 if (priv->tree_view)
4162 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4165 else if (GTK_IS_MENU (priv->popup_widget))
4167 /* FIXME handle nested menus better */
4168 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4169 gtk_tree_path_get_indices (path)[0]);
4172 if (priv->cell_view)
4173 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4177 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4178 g_object_notify (G_OBJECT (combo_box), "active");
4179 if (combo_box->priv->id_column >= 0)
4180 g_object_notify (G_OBJECT (combo_box), "active-id");
4185 * gtk_combo_box_get_active_iter:
4186 * @combo_box: A #GtkComboBox
4187 * @iter: (out): The uninitialized #GtkTreeIter
4189 * Sets @iter to point to the current active item, if it exists.
4191 * Return value: %TRUE, if @iter was set
4196 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4202 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4204 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4207 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4208 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4209 gtk_tree_path_free (path);
4215 * gtk_combo_box_set_active_iter:
4216 * @combo_box: A #GtkComboBox
4217 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4219 * Sets the current active item to be the one referenced by @iter, or
4220 * unsets the active item if @iter is %NULL.
4225 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4228 GtkTreePath *path = NULL;
4230 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4233 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4235 gtk_combo_box_set_active_internal (combo_box, path);
4236 gtk_tree_path_free (path);
4240 * gtk_combo_box_set_model:
4241 * @combo_box: A #GtkComboBox
4242 * @model: (allow-none): A #GtkTreeModel
4244 * Sets the model used by @combo_box to be @model. Will unset a previously set
4245 * model (if applicable). If model is %NULL, then it will unset the model.
4247 * Note that this function does not clear the cell renderers, you have to
4248 * call gtk_cell_layout_clear() yourself if you need to set up different
4249 * cell renderers for the new model.
4254 gtk_combo_box_set_model (GtkComboBox *combo_box,
4255 GtkTreeModel *model)
4257 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4258 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4260 if (model == combo_box->priv->model)
4263 gtk_combo_box_unset_model (combo_box);
4268 combo_box->priv->model = model;
4269 g_object_ref (combo_box->priv->model);
4271 combo_box->priv->inserted_id =
4272 g_signal_connect (combo_box->priv->model, "row-inserted",
4273 G_CALLBACK (gtk_combo_box_model_row_inserted),
4275 combo_box->priv->deleted_id =
4276 g_signal_connect (combo_box->priv->model, "row-deleted",
4277 G_CALLBACK (gtk_combo_box_model_row_deleted),
4279 combo_box->priv->reordered_id =
4280 g_signal_connect (combo_box->priv->model, "rows-reordered",
4281 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4283 combo_box->priv->changed_id =
4284 g_signal_connect (combo_box->priv->model, "row-changed",
4285 G_CALLBACK (gtk_combo_box_model_row_changed),
4288 if (combo_box->priv->tree_view)
4291 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4292 combo_box->priv->model);
4293 gtk_combo_box_list_popup_resize (combo_box);
4296 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4299 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4300 combo_box->priv->model);
4303 if (combo_box->priv->cell_view)
4304 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4305 combo_box->priv->model);
4307 if (combo_box->priv->active != -1)
4309 /* If an index was set in advance, apply it now */
4310 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4311 combo_box->priv->active = -1;
4315 gtk_combo_box_update_sensitivity (combo_box);
4317 g_object_notify (G_OBJECT (combo_box), "model");
4321 * gtk_combo_box_get_model:
4322 * @combo_box: A #GtkComboBox
4324 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4326 * Return value: (transfer none): A #GtkTreeModel which was passed
4327 * during construction.
4332 gtk_combo_box_get_model (GtkComboBox *combo_box)
4334 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4336 return combo_box->priv->model;
4340 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4341 GtkScrollType scroll)
4344 GtkTreeIter new_iter;
4345 gboolean active_iter;
4348 if (!combo_box->priv->model)
4350 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4354 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4358 case GTK_SCROLL_STEP_BACKWARD:
4359 case GTK_SCROLL_STEP_UP:
4360 case GTK_SCROLL_STEP_LEFT:
4363 found = tree_prev (combo_box, combo_box->priv->model,
4364 &iter, &new_iter, FALSE);
4367 /* else fall through */
4369 case GTK_SCROLL_PAGE_FORWARD:
4370 case GTK_SCROLL_PAGE_DOWN:
4371 case GTK_SCROLL_PAGE_RIGHT:
4372 case GTK_SCROLL_END:
4373 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4376 case GTK_SCROLL_STEP_FORWARD:
4377 case GTK_SCROLL_STEP_DOWN:
4378 case GTK_SCROLL_STEP_RIGHT:
4381 found = tree_next (combo_box, combo_box->priv->model,
4382 &iter, &new_iter, FALSE);
4385 /* else fall through */
4387 case GTK_SCROLL_PAGE_BACKWARD:
4388 case GTK_SCROLL_PAGE_UP:
4389 case GTK_SCROLL_PAGE_LEFT:
4390 case GTK_SCROLL_START:
4391 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4398 if (found && active_iter)
4400 GtkTreePath *old_path;
4401 GtkTreePath *new_path;
4403 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4404 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4406 if (gtk_tree_path_compare (old_path, new_path) == 0)
4409 gtk_tree_path_free (old_path);
4410 gtk_tree_path_free (new_path);
4415 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4419 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4424 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4425 gboolean group_cycling)
4427 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4429 if (combo_box->priv->has_entry)
4433 child = gtk_bin_get_child (GTK_BIN (combo_box));
4435 gtk_widget_grab_focus (child);
4438 gtk_widget_grab_focus (combo_box->priv->button);
4444 gtk_combo_box_grab_focus (GtkWidget *widget)
4446 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4448 if (combo_box->priv->has_entry)
4452 child = gtk_bin_get_child (GTK_BIN (combo_box));
4454 gtk_widget_grab_focus (child);
4457 gtk_widget_grab_focus (combo_box->priv->button);
4461 gtk_combo_box_destroy (GtkWidget *widget)
4463 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4465 if (combo_box->priv->popup_idle_id > 0)
4467 g_source_remove (combo_box->priv->popup_idle_id);
4468 combo_box->priv->popup_idle_id = 0;
4471 gtk_combo_box_popdown (combo_box);
4473 if (combo_box->priv->row_separator_destroy)
4474 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4476 combo_box->priv->row_separator_func = NULL;
4477 combo_box->priv->row_separator_data = NULL;
4478 combo_box->priv->row_separator_destroy = NULL;
4480 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4481 combo_box->priv->cell_view = NULL;
4485 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4488 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4491 * Fixes regression reported in bug #574059. The old functionality relied on
4492 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4493 * when the selection was already set to -1.
4495 if (gtk_combo_box_get_active(combo_box) == -1)
4496 g_signal_emit_by_name (combo_box, "changed");
4498 gtk_combo_box_set_active (combo_box, -1);
4502 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4505 GtkComboBoxPrivate *priv = combo_box->priv;
4506 GtkTreeModel *model;
4509 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4511 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4515 GValue value = {0,};
4517 g_signal_handlers_block_by_func (entry,
4518 gtk_combo_box_entry_contents_changed,
4521 model = gtk_combo_box_get_model (combo_box);
4523 gtk_tree_model_get_value (model, &iter,
4524 priv->text_column, &value);
4525 g_object_set_property (G_OBJECT (entry), "text", &value);
4526 g_value_unset (&value);
4528 g_signal_handlers_unblock_by_func (entry,
4529 gtk_combo_box_entry_contents_changed,
4536 gtk_combo_box_constructor (GType type,
4537 guint n_construct_properties,
4538 GObjectConstructParam *construct_properties)
4541 GtkComboBox *combo_box;
4542 GtkComboBoxPrivate *priv;
4543 GtkStyleContext *context;
4545 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4546 (type, n_construct_properties, construct_properties);
4548 combo_box = GTK_COMBO_BOX (object);
4549 priv = combo_box->priv;
4553 GtkCellArea *area = gtk_cell_area_box_new ();
4555 priv->area = g_object_ref_sink (area);
4558 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4559 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4560 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4561 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4562 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4563 gtk_widget_show (priv->cell_view);
4565 gtk_combo_box_check_appearance (combo_box);
4567 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4568 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
4570 if (priv->has_entry)
4574 entry = gtk_entry_new ();
4575 gtk_widget_show (entry);
4576 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4578 priv->text_renderer = gtk_cell_renderer_text_new ();
4579 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4580 priv->text_renderer, TRUE);
4582 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4584 g_signal_connect (combo_box, "changed",
4585 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4593 gtk_combo_box_dispose(GObject* object)
4595 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4597 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4599 gtk_combo_box_menu_destroy (combo_box);
4600 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4601 combo_box->priv->popup_widget = NULL;
4604 if (combo_box->priv->area)
4606 g_object_unref (combo_box->priv->area);
4607 combo_box->priv->area = NULL;
4610 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4611 gtk_combo_box_list_destroy (combo_box);
4613 if (combo_box->priv->popup_window)
4615 gtk_widget_destroy (combo_box->priv->popup_window);
4616 combo_box->priv->popup_window = NULL;
4619 gtk_combo_box_unset_model (combo_box);
4621 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4625 gtk_combo_box_finalize (GObject *object)
4627 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4629 g_free (combo_box->priv->tearoff_title);
4631 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4635 gtk_cell_editable_key_press (GtkWidget *widget,
4639 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4641 if (event->keyval == GDK_KEY_Escape)
4643 g_object_set (combo_box,
4644 "editing-canceled", TRUE,
4646 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4647 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4651 else if (event->keyval == GDK_KEY_Return ||
4652 event->keyval == GDK_KEY_ISO_Enter ||
4653 event->keyval == GDK_KEY_KP_Enter)
4655 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4656 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4665 popdown_idle (gpointer data)
4667 GtkComboBox *combo_box;
4669 combo_box = GTK_COMBO_BOX (data);
4671 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4672 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4674 g_object_unref (combo_box);
4680 popdown_handler (GtkWidget *widget,
4683 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4687 popup_idle (gpointer data)
4689 GtkComboBox *combo_box;
4691 combo_box = GTK_COMBO_BOX (data);
4693 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4694 combo_box->priv->cell_view)
4695 g_signal_connect_object (combo_box->priv->popup_widget,
4696 "unmap", G_CALLBACK (popdown_handler),
4699 /* we unset this if a menu item is activated */
4700 g_object_set (combo_box,
4701 "editing-canceled", TRUE,
4703 gtk_combo_box_popup (combo_box);
4705 combo_box->priv->popup_idle_id = 0;
4706 combo_box->priv->activate_button = 0;
4707 combo_box->priv->activate_time = 0;
4713 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4716 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4719 combo_box->priv->is_cell_renderer = TRUE;
4721 if (combo_box->priv->cell_view)
4723 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4724 G_CALLBACK (gtk_cell_editable_key_press),
4727 gtk_widget_grab_focus (combo_box->priv->button);
4731 child = gtk_bin_get_child (GTK_BIN (combo_box));
4733 g_signal_connect_object (child, "key-press-event",
4734 G_CALLBACK (gtk_cell_editable_key_press),
4737 gtk_widget_grab_focus (child);
4738 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4741 /* we do the immediate popup only for the optionmenu-like
4744 if (combo_box->priv->is_cell_renderer &&
4745 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4747 if (event && event->type == GDK_BUTTON_PRESS)
4749 GdkEventButton *event_button = (GdkEventButton *)event;
4751 combo_box->priv->activate_button = event_button->button;
4752 combo_box->priv->activate_time = event_button->time;
4755 combo_box->priv->popup_idle_id =
4756 gdk_threads_add_idle (popup_idle, combo_box);
4762 * gtk_combo_box_get_add_tearoffs:
4763 * @combo_box: a #GtkComboBox
4765 * Gets the current value of the :add-tearoffs property.
4767 * Return value: the current value of the :add-tearoffs property.
4770 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4772 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4774 return combo_box->priv->add_tearoffs;
4778 * gtk_combo_box_set_add_tearoffs:
4779 * @combo_box: a #GtkComboBox
4780 * @add_tearoffs: %TRUE to add tearoff menu items
4782 * Sets whether the popup menu should have a tearoff
4788 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4789 gboolean add_tearoffs)
4791 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4793 add_tearoffs = add_tearoffs != FALSE;
4795 if (combo_box->priv->add_tearoffs != add_tearoffs)
4797 combo_box->priv->add_tearoffs = add_tearoffs;
4798 gtk_combo_box_check_appearance (combo_box);
4800 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4801 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4802 combo_box->priv->add_tearoffs);
4804 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4809 * gtk_combo_box_get_title:
4810 * @combo_box: a #GtkComboBox
4812 * Gets the current title of the menu in tearoff mode. See
4813 * gtk_combo_box_set_add_tearoffs().
4815 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4816 * string which must not be freed.
4820 G_CONST_RETURN gchar*
4821 gtk_combo_box_get_title (GtkComboBox *combo_box)
4823 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4825 return combo_box->priv->tearoff_title;
4829 gtk_combo_box_update_title (GtkComboBox *combo_box)
4831 gtk_combo_box_check_appearance (combo_box);
4833 if (combo_box->priv->popup_widget &&
4834 GTK_IS_MENU (combo_box->priv->popup_widget))
4835 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4836 combo_box->priv->tearoff_title);
4840 * gtk_combo_box_set_title:
4841 * @combo_box: a #GtkComboBox
4842 * @title: a title for the menu in tearoff mode
4844 * Sets the menu's title in tearoff mode.
4849 gtk_combo_box_set_title (GtkComboBox *combo_box,
4852 GtkComboBoxPrivate *priv;
4854 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4856 priv = combo_box->priv;
4858 if (strcmp (title ? title : "",
4859 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4861 g_free (priv->tearoff_title);
4862 priv->tearoff_title = g_strdup (title);
4864 gtk_combo_box_update_title (combo_box);
4866 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4872 * gtk_combo_box_set_popup_fixed_width:
4873 * @combo_box: a #GtkComboBox
4874 * @fixed: whether to use a fixed popup width
4876 * Specifies whether the popup's width should be a fixed width
4877 * matching the allocated width of the combo box.
4882 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4885 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4887 if (combo_box->priv->popup_fixed_width != fixed)
4889 combo_box->priv->popup_fixed_width = fixed;
4891 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
4896 * gtk_combo_box_get_popup_fixed_width:
4897 * @combo_box: a #GtkComboBox
4899 * Gets whether the popup uses a fixed width matching
4900 * the allocated width of the combo box.
4902 * Returns: %TRUE if the popup uses a fixed width
4907 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
4909 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4911 return combo_box->priv->popup_fixed_width;
4916 * gtk_combo_box_get_popup_accessible:
4917 * @combo_box: a #GtkComboBox
4919 * Gets the accessible object corresponding to the combo box's popup.
4921 * This function is mostly intended for use by accessibility technologies;
4922 * applications should have little use for it.
4924 * Returns: (transfer none): the accessible object corresponding
4925 * to the combo box's popup.
4930 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
4934 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4936 if (combo_box->priv->popup_widget)
4938 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
4946 * gtk_combo_box_get_row_separator_func:
4947 * @combo_box: a #GtkComboBox
4949 * Returns the current row separator function.
4951 * Return value: the current row separator function.
4955 GtkTreeViewRowSeparatorFunc
4956 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
4958 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4960 return combo_box->priv->row_separator_func;
4964 * gtk_combo_box_set_row_separator_func:
4965 * @combo_box: a #GtkComboBox
4966 * @func: a #GtkTreeViewRowSeparatorFunc
4967 * @data: (allow-none): user data to pass to @func, or %NULL
4968 * @destroy: (allow-none): destroy notifier for @data, or %NULL
4970 * Sets the row separator function, which is used to determine
4971 * whether a row should be drawn as a separator. If the row separator
4972 * function is %NULL, no separators are drawn. This is the default value.
4977 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
4978 GtkTreeViewRowSeparatorFunc func,
4980 GDestroyNotify destroy)
4982 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4984 if (combo_box->priv->row_separator_destroy)
4985 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4987 combo_box->priv->row_separator_func = func;
4988 combo_box->priv->row_separator_data = data;
4989 combo_box->priv->row_separator_destroy = destroy;
4991 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
4992 if (combo_box->priv->tree_view)
4994 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
4995 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
4998 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5000 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5001 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5004 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5008 * gtk_combo_box_set_button_sensitivity:
5009 * @combo_box: a #GtkComboBox
5010 * @sensitivity: specify the sensitivity of the dropdown button
5012 * Sets whether the dropdown button of the combo box should be
5013 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5014 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5019 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5020 GtkSensitivityType sensitivity)
5022 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5024 if (combo_box->priv->button_sensitivity != sensitivity)
5026 combo_box->priv->button_sensitivity = sensitivity;
5027 gtk_combo_box_update_sensitivity (combo_box);
5029 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5034 * gtk_combo_box_get_button_sensitivity:
5035 * @combo_box: a #GtkComboBox
5037 * Returns whether the combo box sets the dropdown button
5038 * sensitive or not when there are no items in the model.
5040 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5041 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5042 * if the button is always insensitive or
5043 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5044 * the model has one item to be selected.
5049 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5051 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5053 return combo_box->priv->button_sensitivity;
5058 * gtk_combo_box_get_has_entry:
5059 * @combo_box: a #GtkComboBox
5061 * Returns whether the combo box has an entry.
5063 * Return Value: whether there is an entry in @combo_box.
5068 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5070 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5072 return combo_box->priv->has_entry;
5076 * gtk_combo_box_set_entry_text_column:
5077 * @combo_box: A #GtkComboBox
5078 * @text_column: A column in @model to get the strings from for
5079 * the internal entry
5081 * Sets the model column which @combo_box should use to get strings from
5082 * to be @text_column. The column @text_column in the model of @combo_box
5083 * must be of type %G_TYPE_STRING.
5085 * This is only relevant if @combo_box has been created with
5086 * #GtkComboBox:has-entry as %TRUE.
5091 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5094 GtkComboBoxPrivate *priv = combo_box->priv;
5095 GtkTreeModel *model;
5097 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5099 model = gtk_combo_box_get_model (combo_box);
5101 g_return_if_fail (text_column >= 0);
5102 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5104 priv->text_column = text_column;
5106 if (priv->text_renderer != NULL)
5107 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5108 priv->text_renderer,
5109 "text", text_column,
5114 * gtk_combo_box_get_entry_text_column:
5115 * @combo_box: A #GtkComboBox.
5117 * Returns the column which @combo_box is using to get the strings
5118 * from to display in the internal entry.
5120 * Return value: A column in the data source model of @combo_box.
5125 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5127 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5129 return combo_box->priv->text_column;
5133 * gtk_combo_box_set_focus_on_click:
5134 * @combo: a #GtkComboBox
5135 * @focus_on_click: whether the combo box grabs focus when clicked
5138 * Sets whether the combo box will grab focus when it is clicked with
5139 * the mouse. Making mouse clicks not grab focus is useful in places
5140 * like toolbars where you don't want the keyboard focus removed from
5141 * the main area of the application.
5146 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5147 gboolean focus_on_click)
5149 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5151 focus_on_click = focus_on_click != FALSE;
5153 if (combo_box->priv->focus_on_click != focus_on_click)
5155 combo_box->priv->focus_on_click = focus_on_click;
5157 if (combo_box->priv->button)
5158 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5161 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5166 * gtk_combo_box_get_focus_on_click:
5167 * @combo: a #GtkComboBox
5169 * Returns whether the combo box grabs focus when it is clicked
5170 * with the mouse. See gtk_combo_box_set_focus_on_click().
5172 * Return value: %TRUE if the combo box grabs focus when it is
5173 * clicked with the mouse.
5178 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5180 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5182 return combo_box->priv->focus_on_click;
5187 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5188 GtkBuilder *builder,
5190 const gchar *tagname,
5191 GMarkupParser *parser,
5194 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5195 tagname, parser, data))
5198 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5199 tagname, parser, data);
5203 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5204 GtkBuilder *builder,
5206 const gchar *tagname,
5209 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5210 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5214 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5215 GtkBuilder *builder,
5216 const gchar *childname)
5218 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5220 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5221 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5223 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5227 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5231 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5232 GtkComboBoxPrivate *priv = combo_box->priv;
5233 gint focus_width, focus_pad;
5234 gint font_size, arrow_size;
5235 PangoContext *context;
5236 PangoFontMetrics *metrics;
5237 PangoFontDescription *font_desc;
5239 gint minimum_width = 0, natural_width = 0;
5240 gint child_min, child_nat;
5241 GtkStyleContext *style_context;
5242 GtkStateFlags state;
5245 child = gtk_bin_get_child (GTK_BIN (widget));
5248 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5250 gtk_widget_style_get (GTK_WIDGET (widget),
5251 "focus-line-width", &focus_width,
5252 "focus-padding", &focus_pad,
5253 "arrow-size", &arrow_size,
5256 style_context = gtk_widget_get_style_context (widget);
5257 state = gtk_widget_get_state_flags (widget);
5259 gtk_style_context_get (style_context, state,
5261 "border-width", &border,
5264 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5265 metrics = pango_context_get_metrics (context, font_desc,
5266 pango_context_get_language (context));
5267 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5268 pango_font_metrics_get_descent (metrics));
5269 pango_font_metrics_unref (metrics);
5270 pango_font_description_free (font_desc);
5272 arrow_size = MAX (arrow_size, font_size);
5274 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5276 if (!priv->tree_view)
5279 if (priv->cell_view)
5281 gint sep_width, arrow_width;
5282 gint border_width, xpad;
5283 GtkBorder button_border;
5285 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5286 get_widget_border (priv->button, &button_border);
5288 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5289 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5291 xpad = 2 * (border_width + focus_width + focus_pad) +
5292 button_border.left + button_border.right;
5294 minimum_width = child_min + sep_width + arrow_width + xpad;
5295 natural_width = child_nat + sep_width + arrow_width + xpad;
5299 gint but_width, but_nat_width;
5301 gtk_widget_get_preferred_width (priv->button,
5302 &but_width, &but_nat_width);
5304 minimum_width = child_min + but_width;
5305 natural_width = child_nat + but_nat_width;
5311 gint button_width, button_nat_width;
5313 /* sample + frame */
5314 minimum_width = child_min;
5315 natural_width = child_nat;
5317 minimum_width += 2 * focus_width;
5318 natural_width += 2 * focus_width;
5320 if (priv->cell_view_frame)
5322 if (priv->has_frame)
5324 gint border_width, xpad;
5325 GtkBorder frame_border;
5327 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5328 get_widget_border (priv->cell_view_frame, &frame_border);
5329 xpad = (2 * border_width) + frame_border.left + frame_border.right;
5331 minimum_width += xpad;
5332 natural_width += xpad;
5337 gtk_widget_get_preferred_width (priv->button,
5338 &button_width, &button_nat_width);
5340 minimum_width += button_width;
5341 natural_width += button_nat_width;
5344 minimum_width += border->left + border->right;
5345 natural_width += border->left + border->right;
5346 gtk_border_free (border);
5349 *minimum_size = minimum_width;
5352 *natural_size = natural_width;
5356 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5362 /* Combo box is height-for-width only
5363 * (so we always just reserve enough height for the minimum width) */
5364 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5365 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5369 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5374 /* Combo box is height-for-width only
5375 * (so we assume we always reserved enough height for the minimum width) */
5376 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5381 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5386 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5387 GtkComboBoxPrivate *priv = combo_box->priv;
5388 gint focus_width, focus_pad;
5389 gint min_height = 0, nat_height = 0;
5394 gtk_widget_style_get (GTK_WIDGET (widget),
5395 "focus-line-width", &focus_width,
5396 "focus-padding", &focus_pad,
5399 child = gtk_bin_get_child (GTK_BIN (widget));
5401 get_widget_border (widget, &border);
5402 size = avail_size - border.left;
5404 if (!priv->tree_view)
5407 if (priv->cell_view)
5409 /* calculate x/y padding and separator/arrow size */
5410 gint sep_width, arrow_width, sep_height, arrow_height;
5411 gint border_width, xpad, ypad;
5412 GtkBorder button_border;
5414 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5415 get_widget_border (priv->button, &button_border);
5417 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5418 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5419 gtk_widget_get_preferred_height_for_width (priv->separator,
5420 sep_width, &sep_height, NULL);
5421 gtk_widget_get_preferred_height_for_width (priv->arrow,
5422 arrow_width, &arrow_height, NULL);
5424 xpad = 2 * (border_width + focus_width + focus_pad) +
5425 button_border.left + button_border.right;
5426 ypad = 2 * (border_width + focus_width + focus_pad) +
5427 button_border.top + button_border.bottom;
5429 size -= sep_width + arrow_width + xpad;
5431 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5432 * and fitting the whole treemodel */
5433 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5435 arrow_height = MAX (arrow_height, sep_height);
5436 min_height = MAX (min_height, arrow_height);
5437 nat_height = MAX (nat_height, arrow_height);
5444 /* there is a custom child widget inside (no priv->cell_view) */
5445 gint but_width, but_height;
5447 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5448 gtk_widget_get_preferred_height_for_width (priv->button,
5449 but_width, &but_height, NULL);
5453 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5454 * and fitting the whole treemodel */
5455 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5457 min_height = MAX (min_height, but_height);
5458 nat_height = MAX (nat_height, but_height);
5464 gint but_width, but_height;
5465 gint xpad = 0, ypad = 0;
5467 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5468 gtk_widget_get_preferred_height_for_width (priv->button,
5469 but_width, &but_height, NULL);
5471 if (priv->cell_view_frame && priv->has_frame)
5473 GtkBorder frame_border;
5476 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5477 get_widget_border (GTK_WIDGET (priv->cell_view_frame), &frame_border);
5479 xpad = (2 * border_width) + border.left + frame_border.right;
5480 ypad = (2 * border_width) + border.top + frame_border.bottom;
5484 size -= 2 * focus_width;
5487 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5488 * and fitting the whole treemodel */
5489 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5491 min_height = MAX (min_height, but_height);
5492 nat_height = MAX (nat_height, but_height);
5498 min_height += border.top + border.bottom;
5499 nat_height += border.top + border.bottom;
5502 *minimum_size = min_height;
5505 *natural_size = nat_height;
5509 * gtk_combo_box_set_id_column:
5510 * @combo_box: A #GtkComboBox
5511 * @id_column: A column in @model to get string IDs for values from
5513 * Sets the model column which @combo_box should use to get string IDs
5514 * for values from. The column @id_column in the model of @combo_box
5515 * must be of type %G_TYPE_STRING.
5520 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5523 GtkComboBoxPrivate *priv = combo_box->priv;
5524 GtkTreeModel *model;
5526 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5528 if (id_column != priv->id_column)
5530 model = gtk_combo_box_get_model (combo_box);
5532 g_return_if_fail (id_column >= 0);
5533 g_return_if_fail (model == NULL ||
5534 id_column < gtk_tree_model_get_n_columns (model));
5536 priv->id_column = id_column;
5538 g_object_notify (G_OBJECT (combo_box), "id-column");
5539 g_object_notify (G_OBJECT (combo_box), "active-id");
5544 * gtk_combo_box_get_id_column:
5545 * @combo_box: A #GtkComboBox
5547 * Returns the column which @combo_box is using to get string IDs
5550 * Return value: A column in the data source model of @combo_box.
5555 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5557 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5559 return combo_box->priv->id_column;
5563 * gtk_combo_box_get_active_id:
5564 * @combo_box: a #GtkComboBox
5566 * Returns the ID of the active row of @combo_box. This value is taken
5567 * from the active row and the column specified by the 'id-column'
5568 * property of @combo_box (see gtk_combo_box_set_id_column()).
5570 * The returned value is an interned string which means that you can
5571 * compare the pointer by value to other interned strings and that you
5574 * If the 'id-column' property of @combo_box is not set or if no row is
5575 * selected then %NULL is returned.
5577 * Return value: the ID of the active row, or %NULL
5582 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5584 GtkTreeModel *model;
5588 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5590 column = combo_box->priv->id_column;
5595 model = gtk_combo_box_get_model (combo_box);
5596 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5597 G_TYPE_STRING, NULL);
5599 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5601 const gchar *interned;
5604 gtk_tree_model_get (model, &iter, column, &id, -1);
5605 interned = g_intern_string (id);
5615 * gtk_combo_box_set_active_id:
5616 * @combo_box: a #GtkComboBox
5617 * @active_id: the ID of the row to select
5619 * Changes the active row of @combo_box to the one that has an ID equal to @id.
5621 * If the 'id-column' property of @combo_box is unset or if no row has
5622 * the given ID then nothing happens.
5627 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5628 const gchar *active_id)
5630 GtkTreeModel *model;
5634 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5636 column = combo_box->priv->id_column;
5641 model = gtk_combo_box_get_model (combo_box);
5642 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
5645 if (gtk_tree_model_get_iter_first (model, &iter))
5650 gtk_tree_model_get (model, &iter, column, &id, -1);
5651 match = strcmp (id, active_id) == 0;
5656 gtk_combo_box_set_active_iter (combo_box, &iter);
5659 } while (gtk_tree_model_iter_next (model, &iter));