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, see <http://www.gnu.org/licenses/>.
20 #include "gtkcombobox.h"
22 #include "gtkadjustment.h"
23 #include "gtkcellareabox.h"
24 #include "gtktreemenu.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcellview.h"
30 #include "gtkeventbox.h"
33 #include "gtkliststore.h"
35 #include "gtkmenuprivate.h"
36 #include "gtkmenushellprivate.h"
37 #include "gtkscrolledwindow.h"
38 #include "gtkseparatormenuitem.h"
39 #include "deprecated/gtktearoffmenuitem.h"
40 #include "gtktogglebutton.h"
41 #include "gtktreeselection.h"
42 #include "gtkseparator.h"
43 #include "gtkwidgetpath.h"
44 #include "gtkwidgetprivate.h"
45 #include "gtkwindow.h"
46 #include "gtktypebuiltins.h"
47 #include "gtkprivate.h"
49 #include <gobject/gvaluecollector.h>
54 #include "gtkmarshalers.h"
57 #include "gtkentryprivate.h"
58 #include "gtktreeprivate.h"
59 #include "a11y/gtkcomboboxaccessible.h"
64 * @Short_description: A widget used to choose from a list of items
66 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
68 * A GtkComboBox is a widget that allows the user to choose from a list of
69 * valid choices. The GtkComboBox displays the selected choice. When
70 * activated, the GtkComboBox displays a popup which allows the user to
71 * make a new choice. The style in which the selected value is displayed,
72 * and the style of the popup is determined by the current theme. It may
73 * be similar to a Windows-style combo box.
75 * The GtkComboBox uses the model-view pattern; the list of valid choices
76 * is specified in the form of a tree model, and the display of the choices
77 * can be adapted to the data in the model by using cell renderers, as you
78 * would in a tree view. This is possible since GtkComboBox implements the
79 * #GtkCellLayout interface. The tree model holding the valid choices is
80 * not restricted to a flat list, it can be a real tree, and the popup will
81 * reflect the tree structure.
83 * To allow the user to enter values not in the model, the 'has-entry'
84 * property allows the GtkComboBox to contain a #GtkEntry. This entry
85 * can be accessed by calling gtk_bin_get_child() on the combo box.
87 * For a simple list of textual choices, the model-view API of GtkComboBox
88 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
89 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
94 /* WELCOME, to THE house of evil code */
95 struct _GtkComboBoxPrivate
105 GtkShadowType shadow_type;
107 gint active; /* Only temporary */
108 GtkTreeRowReference *active_row;
110 GtkWidget *tree_view;
112 GtkWidget *cell_view;
113 GtkWidget *cell_view_frame;
118 GtkWidget *separator;
120 GtkWidget *popup_widget;
121 GtkWidget *popup_window;
122 GtkWidget *scrolled_window;
129 guint activate_button;
130 guint32 activate_time;
132 guint resize_idle_id;
134 /* For "has-entry" specific behavior we track
135 * an automated cell renderer and text column
138 GtkCellRenderer *text_renderer;
142 guint popup_in_progress : 1;
143 guint popup_shown : 1;
144 guint add_tearoffs : 1;
146 guint is_cell_renderer : 1;
147 guint editing_canceled : 1;
148 guint auto_scroll : 1;
149 guint focus_on_click : 1;
150 guint button_sensitivity : 2;
152 guint popup_fixed_width : 1;
154 GtkTreeViewRowSeparatorFunc row_separator_func;
155 gpointer row_separator_data;
156 GDestroyNotify row_separator_destroy;
158 GdkDevice *grab_pointer;
159 GdkDevice *grab_keyboard;
161 gchar *tearoff_title;
164 /* While debugging this evil code, I have learned that
165 * there are actually 4 modes to this widget, which can
166 * be characterized as follows
168 * 1) menu mode, no child added
171 * cell_view -> GtkCellView, regular child
172 * cell_view_frame -> NULL
173 * button -> GtkToggleButton set_parent to combo
174 * arrow -> GtkArrow set_parent to button
175 * separator -> GtkVSepator set_parent to button
176 * popup_widget -> GtkMenu
177 * popup_window -> NULL
178 * scrolled_window -> NULL
180 * 2) menu mode, child added
184 * cell_view_frame -> NULL
185 * button -> GtkToggleButton set_parent to combo
186 * arrow -> GtkArrow, child of button
188 * popup_widget -> GtkMenu
189 * popup_window -> NULL
190 * scrolled_window -> NULL
192 * 3) list mode, no child added
194 * tree_view -> GtkTreeView, child of scrolled_window
195 * cell_view -> GtkCellView, regular child
196 * cell_view_frame -> GtkFrame, set parent to combo
197 * button -> GtkToggleButton, set_parent to combo
198 * arrow -> GtkArrow, child of button
200 * popup_widget -> tree_view
201 * popup_window -> GtkWindow
202 * scrolled_window -> GtkScrolledWindow, child of popup_window
204 * 4) list mode, child added
206 * tree_view -> GtkTreeView, child of scrolled_window
208 * cell_view_frame -> NULL
209 * button -> GtkToggleButton, set_parent to combo
210 * arrow -> GtkArrow, child of button
212 * popup_widget -> tree_view
213 * popup_window -> GtkWindow
214 * scrolled_window -> GtkScrolledWindow, child of popup_window
231 PROP_ROW_SPAN_COLUMN,
232 PROP_COLUMN_SPAN_COLUMN,
239 PROP_BUTTON_SENSITIVITY,
240 PROP_EDITING_CANCELED,
242 PROP_ENTRY_TEXT_COLUMN,
243 PROP_POPUP_FIXED_WIDTH,
249 static guint combo_box_signals[LAST_SIGNAL] = {0,};
251 #define SCROLL_TIME 100
255 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
256 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
257 static GObject *gtk_combo_box_constructor (GType type,
258 guint n_construct_properties,
259 GObjectConstructParam *construct_properties);
260 static void gtk_combo_box_dispose (GObject *object);
261 static void gtk_combo_box_finalize (GObject *object);
262 static void gtk_combo_box_destroy (GtkWidget *widget);
264 static void gtk_combo_box_set_property (GObject *object,
268 static void gtk_combo_box_get_property (GObject *object,
273 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
274 GtkStateFlags previous);
275 static void gtk_combo_box_grab_focus (GtkWidget *widget);
276 static void gtk_combo_box_style_updated (GtkWidget *widget);
277 static void gtk_combo_box_button_toggled (GtkWidget *widget,
279 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
280 GtkStateFlags previous,
282 static void gtk_combo_box_add (GtkContainer *container,
284 static void gtk_combo_box_remove (GtkContainer *container,
287 static void gtk_combo_box_menu_show (GtkWidget *menu,
289 static void gtk_combo_box_menu_hide (GtkWidget *menu,
292 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
294 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
299 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
304 static void gtk_combo_box_menu_position (GtkMenu *menu,
310 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
312 static void gtk_combo_box_size_allocate (GtkWidget *widget,
313 GtkAllocation *allocation);
314 static void gtk_combo_box_forall (GtkContainer *container,
315 gboolean include_internals,
316 GtkCallback callback,
317 gpointer callback_data);
318 static gboolean gtk_combo_box_draw (GtkWidget *widget,
320 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
321 GdkEventScroll *event);
322 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
325 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
326 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
327 GtkScrollType scroll);
328 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
329 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
331 /* listening to the model */
332 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
336 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
339 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
344 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
348 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
354 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
359 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
360 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
362 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
363 GdkEventButton *event,
365 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
368 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
369 GdkEventCrossing *event,
371 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
374 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
375 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
376 GdkEventButton *event,
379 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
382 gboolean path_currently_selected,
385 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
389 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
392 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
393 gboolean add_children);
394 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
395 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
398 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
399 GdkEventButton *event,
401 static void gtk_combo_box_menu_activate (GtkWidget *menu,
403 GtkComboBox *combo_box);
404 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
405 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
408 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
410 guint32 activate_time);
413 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
415 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
416 gboolean group_cycling);
418 static void gtk_combo_box_child_show (GtkWidget *widget,
419 GtkComboBox *combo_box);
420 static void gtk_combo_box_child_hide (GtkWidget *widget,
421 GtkComboBox *combo_box);
423 /* GtkComboBox:has-entry callbacks */
424 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
426 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
428 static gchar *gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
431 /* GtkBuildable method implementation */
432 static GtkBuildableIface *parent_buildable_iface;
434 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
435 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
438 const gchar *tagname,
439 GMarkupParser *parser,
441 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
444 const gchar *tagname,
446 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
448 const gchar *childname);
451 /* GtkCellEditable method implementations */
452 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
455 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
458 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
461 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
465 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
469 static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container,
472 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
473 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
474 gtk_combo_box_cell_layout_init)
475 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
476 gtk_combo_box_cell_editable_init)
477 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
478 gtk_combo_box_buildable_init))
483 gtk_combo_box_class_init (GtkComboBoxClass *klass)
485 GObjectClass *object_class;
486 GtkContainerClass *container_class;
487 GtkWidgetClass *widget_class;
488 GtkBindingSet *binding_set;
490 container_class = (GtkContainerClass *)klass;
491 container_class->forall = gtk_combo_box_forall;
492 container_class->add = gtk_combo_box_add;
493 container_class->remove = gtk_combo_box_remove;
494 container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
496 gtk_container_class_handle_border_width (container_class);
498 widget_class = (GtkWidgetClass *)klass;
499 widget_class->size_allocate = gtk_combo_box_size_allocate;
500 widget_class->draw = gtk_combo_box_draw;
501 widget_class->scroll_event = gtk_combo_box_scroll_event;
502 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
503 widget_class->grab_focus = gtk_combo_box_grab_focus;
504 widget_class->style_updated = gtk_combo_box_style_updated;
505 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
506 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
507 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
508 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
509 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
510 widget_class->destroy = gtk_combo_box_destroy;
512 object_class = (GObjectClass *)klass;
513 object_class->constructor = gtk_combo_box_constructor;
514 object_class->dispose = gtk_combo_box_dispose;
515 object_class->finalize = gtk_combo_box_finalize;
516 object_class->set_property = gtk_combo_box_set_property;
517 object_class->get_property = gtk_combo_box_get_property;
519 klass->format_entry_text = gtk_combo_box_format_entry_text;
523 * GtkComboBox::changed:
524 * @widget: the object which received the signal
526 * The changed signal is emitted when the active
527 * item is changed. The can be due to the user selecting
528 * a different item from the list, or due to a
529 * call to gtk_combo_box_set_active_iter().
530 * It will also be emitted while typing into the entry of a combo box
535 combo_box_signals[CHANGED] =
536 g_signal_new (I_("changed"),
537 G_OBJECT_CLASS_TYPE (klass),
539 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
541 g_cclosure_marshal_VOID__VOID,
544 * GtkComboBox::move-active:
545 * @widget: the object that received the signal
546 * @scroll_type: a #GtkScrollType
548 * The ::move-active signal is a
549 * <link linkend="keybinding-signals">keybinding signal</link>
550 * which gets emitted to move the active selection.
554 combo_box_signals[MOVE_ACTIVE] =
555 g_signal_new_class_handler (I_("move-active"),
556 G_OBJECT_CLASS_TYPE (klass),
557 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
558 G_CALLBACK (gtk_combo_box_real_move_active),
560 g_cclosure_marshal_VOID__ENUM,
562 GTK_TYPE_SCROLL_TYPE);
565 * GtkComboBox::popup:
566 * @widget: the object that received the signal
568 * The ::popup signal is a
569 * <link linkend="keybinding-signals">keybinding signal</link>
570 * which gets emitted to popup the combo box list.
572 * The default binding for this signal is Alt+Down.
576 combo_box_signals[POPUP] =
577 g_signal_new_class_handler (I_("popup"),
578 G_OBJECT_CLASS_TYPE (klass),
579 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
580 G_CALLBACK (gtk_combo_box_real_popup),
582 g_cclosure_marshal_VOID__VOID,
585 * GtkComboBox::popdown:
586 * @button: the object which received the signal
588 * The ::popdown signal is a
589 * <link linkend="keybinding-signals">keybinding signal</link>
590 * which gets emitted to popdown the combo box list.
592 * The default bindings for this signal are Alt+Up and Escape.
596 combo_box_signals[POPDOWN] =
597 g_signal_new_class_handler (I_("popdown"),
598 G_OBJECT_CLASS_TYPE (klass),
599 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
600 G_CALLBACK (gtk_combo_box_real_popdown),
602 _gtk_marshal_BOOLEAN__VOID,
606 * GtkComboBox::format-entry-text:
607 * @combo: the object which received the signal
608 * @path: the GtkTreePath string from the combo box's current model to format text for
610 * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
612 * A signal which allows you to change how the text displayed in a combo box's
613 * entry is displayed.
615 * Connect a signal handler which returns an allocated string representing
616 * @path. That string will then be used to set the text in the combo box's entry.
617 * The default signal handler uses the text from the GtkComboBox::entry-text-column
620 * Here's an example signal handler which fetches data from the model and
621 * displays it in the entry.
624 * format_entry_text_callback (GtkComboBox *combo,
626 * gpointer user_data)
629 * GtkTreeModel model;
632 * model = gtk_combo_box_get_model (combo);
634 * gtk_tree_model_get_iter_from_string (model, &iter, path);
635 * gtk_tree_model_get (model, &iter,
636 * THE_DOUBLE_VALUE_COLUMN, &value,
639 * return g_strdup_printf ("%g", value);
643 * Return value: (transfer full): a newly allocated string representing @path
644 * for the current GtkComboBox model.
648 combo_box_signals[FORMAT_ENTRY_TEXT] =
649 g_signal_new (I_("format-entry-text"),
650 G_TYPE_FROM_CLASS (klass),
652 G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text),
653 _gtk_single_string_accumulator, NULL,
654 _gtk_marshal_STRING__STRING,
655 G_TYPE_STRING, 1, G_TYPE_STRING);
658 binding_set = gtk_binding_set_by_class (widget_class);
660 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
662 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
665 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
667 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
669 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
672 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
674 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
675 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
677 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
678 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
680 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
681 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
683 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
684 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
686 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
687 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
689 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
691 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
693 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
694 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
696 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
697 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
699 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
700 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
702 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
703 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
705 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
706 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
708 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
711 g_object_class_override_property (object_class,
712 PROP_EDITING_CANCELED,
718 * The model from which the combo box takes the values shown
723 g_object_class_install_property (object_class,
725 g_param_spec_object ("model",
726 P_("ComboBox model"),
727 P_("The model for the combo box"),
729 GTK_PARAM_READWRITE));
732 * GtkComboBox:wrap-width:
734 * If wrap-width is set to a positive value, the list will be
735 * displayed in multiple columns, the number of columns is
736 * determined by wrap-width.
740 g_object_class_install_property (object_class,
742 g_param_spec_int ("wrap-width",
744 P_("Wrap width for laying out the items in a grid"),
748 GTK_PARAM_READWRITE));
752 * GtkComboBox:row-span-column:
754 * If this is set to a non-negative value, it must be the index of a column
755 * of type %G_TYPE_INT in the model.
757 * The values of that column are used to determine how many rows a value in
758 * the list will span. Therefore, the values in the model column pointed to
759 * by this property must be greater than zero and not larger than wrap-width.
763 g_object_class_install_property (object_class,
764 PROP_ROW_SPAN_COLUMN,
765 g_param_spec_int ("row-span-column",
766 P_("Row span column"),
767 P_("TreeModel column containing the row span values"),
771 GTK_PARAM_READWRITE));
775 * GtkComboBox:column-span-column:
777 * If this is set to a non-negative value, it must be the index of a column
778 * of type %G_TYPE_INT in the model.
780 * The values of that column are used to determine how many columns a value
781 * in the list will span.
785 g_object_class_install_property (object_class,
786 PROP_COLUMN_SPAN_COLUMN,
787 g_param_spec_int ("column-span-column",
788 P_("Column span column"),
789 P_("TreeModel column containing the column span values"),
793 GTK_PARAM_READWRITE));
797 * GtkComboBox:active:
799 * The item which is currently active. If the model is a non-flat treemodel,
800 * and the active item is not an immediate child of the root of the tree,
801 * this property has the value
802 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
803 * where <literal>path</literal> is the #GtkTreePath of the active item.
807 g_object_class_install_property (object_class,
809 g_param_spec_int ("active",
811 P_("The item which is currently active"),
815 GTK_PARAM_READWRITE));
818 * GtkComboBox:add-tearoffs:
820 * The add-tearoffs property controls whether generated menus
821 * have tearoff menu items.
823 * Note that this only affects menu style combo boxes.
827 g_object_class_install_property (object_class,
829 g_param_spec_boolean ("add-tearoffs",
830 P_("Add tearoffs to menus"),
831 P_("Whether dropdowns should have a tearoff menu item"),
833 GTK_PARAM_READWRITE));
836 * GtkComboBox:has-frame:
838 * The has-frame property controls whether a frame
839 * is drawn around the entry.
843 g_object_class_install_property (object_class,
845 g_param_spec_boolean ("has-frame",
847 P_("Whether the combo box draws a frame around the child"),
849 GTK_PARAM_READWRITE));
851 g_object_class_install_property (object_class,
853 g_param_spec_boolean ("focus-on-click",
854 P_("Focus on click"),
855 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
857 GTK_PARAM_READWRITE));
860 * GtkComboBox:tearoff-title:
862 * A title that may be displayed by the window manager
863 * when the popup is torn-off.
867 g_object_class_install_property (object_class,
869 g_param_spec_string ("tearoff-title",
871 P_("A title that may be displayed by the window manager when the popup is torn-off"),
873 GTK_PARAM_READWRITE));
877 * GtkComboBox:popup-shown:
879 * Whether the combo boxes dropdown is popped up.
880 * Note that this property is mainly useful, because
881 * it allows you to connect to notify::popup-shown.
885 g_object_class_install_property (object_class,
887 g_param_spec_boolean ("popup-shown",
889 P_("Whether the combo's dropdown is shown"),
891 GTK_PARAM_READABLE));
895 * GtkComboBox:button-sensitivity:
897 * Whether the dropdown button is sensitive when
898 * the model is empty.
902 g_object_class_install_property (object_class,
903 PROP_BUTTON_SENSITIVITY,
904 g_param_spec_enum ("button-sensitivity",
905 P_("Button Sensitivity"),
906 P_("Whether the dropdown button is sensitive when the model is empty"),
907 GTK_TYPE_SENSITIVITY_TYPE,
908 GTK_SENSITIVITY_AUTO,
909 GTK_PARAM_READWRITE));
912 * GtkComboBox:has-entry:
914 * Whether the combo box has an entry.
918 g_object_class_install_property (object_class,
920 g_param_spec_boolean ("has-entry",
922 P_("Whether combo box has an entry"),
924 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
927 * GtkComboBox:entry-text-column:
929 * The column in the combo box's model to associate with strings from the entry
930 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
934 g_object_class_install_property (object_class,
935 PROP_ENTRY_TEXT_COLUMN,
936 g_param_spec_int ("entry-text-column",
937 P_("Entry Text Column"),
938 P_("The column in the combo box's model to associate "
939 "with strings from the entry if the combo was "
940 "created with #GtkComboBox:has-entry = %TRUE"),
942 GTK_PARAM_READWRITE));
945 * GtkComboBox:id-column:
947 * The column in the combo box's model that provides string
948 * IDs for the values in the model, if != -1.
952 g_object_class_install_property (object_class,
954 g_param_spec_int ("id-column",
956 P_("The column in the combo box's model that provides "
957 "string IDs for the values in the model"),
959 GTK_PARAM_READWRITE));
962 * GtkComboBox:active-id:
964 * The value of the ID column of the active row.
968 g_object_class_install_property (object_class,
970 g_param_spec_string ("active-id",
972 P_("The value of the id column "
973 "for the active row"),
974 NULL, GTK_PARAM_READWRITE));
977 * GtkComboBox:popup-fixed-width:
979 * Whether the popup's width should be a fixed width matching the
980 * allocated width of the combo box.
984 g_object_class_install_property (object_class,
985 PROP_POPUP_FIXED_WIDTH,
986 g_param_spec_boolean ("popup-fixed-width",
987 P_("Popup Fixed Width"),
988 P_("Whether the popup's width should be a "
989 "fixed width matching the allocated width "
992 GTK_PARAM_READWRITE));
995 * GtkComboBox:cell-area:
997 * The #GtkCellArea used to layout cell renderers for this combo box.
999 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
1000 * a horizontally oriented #GtkCellAreaBox will be used.
1004 g_object_class_install_property (object_class,
1006 g_param_spec_object ("cell-area",
1008 P_("The GtkCellArea used to layout cells"),
1010 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1012 gtk_widget_class_install_style_property (widget_class,
1013 g_param_spec_boolean ("appears-as-list",
1014 P_("Appears as list"),
1015 P_("Whether dropdowns should look like lists rather than menus"),
1017 GTK_PARAM_READABLE));
1020 * GtkComboBox:arrow-size:
1022 * Sets the minimum size of the arrow in the combo box. Note
1023 * that the arrow size is coupled to the font size, so in case
1024 * a larger font is used, the arrow will be larger than set
1029 gtk_widget_class_install_style_property (widget_class,
1030 g_param_spec_int ("arrow-size",
1032 P_("The minimum size of the arrow in the combo box"),
1036 GTK_PARAM_READABLE));
1039 * GtkComboBox:arrow-scaling:
1041 * Sets the amount of space used up by the combobox arrow,
1042 * proportional to the font size.
1046 gtk_widget_class_install_style_property (widget_class,
1047 g_param_spec_float ("arrow-scaling",
1048 P_("Arrow Scaling"),
1049 P_("The amount of space used by the arrow"),
1053 GTK_PARAM_READABLE));
1056 * GtkComboBox:shadow-type:
1058 * Which kind of shadow to draw around the combo box.
1062 gtk_widget_class_install_style_property (widget_class,
1063 g_param_spec_enum ("shadow-type",
1065 P_("Which kind of shadow to draw around the combo box"),
1066 GTK_TYPE_SHADOW_TYPE,
1068 GTK_PARAM_READABLE));
1070 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1072 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1076 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1078 parent_buildable_iface = g_type_interface_peek_parent (iface);
1079 iface->add_child = _gtk_cell_layout_buildable_add_child;
1080 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1081 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1082 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1086 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1088 iface->get_area = gtk_combo_box_cell_layout_get_area;
1092 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1094 iface->start_editing = gtk_combo_box_start_editing;
1098 gtk_combo_box_init (GtkComboBox *combo_box)
1100 GtkComboBoxPrivate *priv;
1102 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1104 GtkComboBoxPrivate);
1105 priv = combo_box->priv;
1107 priv->wrap_width = 0;
1110 priv->active_row = NULL;
1111 priv->col_column = -1;
1112 priv->row_column = -1;
1114 priv->popup_shown = FALSE;
1115 priv->add_tearoffs = FALSE;
1116 priv->has_frame = TRUE;
1117 priv->is_cell_renderer = FALSE;
1118 priv->editing_canceled = FALSE;
1119 priv->auto_scroll = FALSE;
1120 priv->focus_on_click = TRUE;
1121 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1122 priv->has_entry = FALSE;
1123 priv->popup_fixed_width = TRUE;
1125 priv->text_column = -1;
1126 priv->text_renderer = NULL;
1127 priv->id_column = -1;
1131 gtk_combo_box_set_property (GObject *object,
1133 const GValue *value,
1136 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1137 GtkComboBoxPrivate *priv = combo_box->priv;
1143 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1146 case PROP_WRAP_WIDTH:
1147 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1150 case PROP_ROW_SPAN_COLUMN:
1151 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1154 case PROP_COLUMN_SPAN_COLUMN:
1155 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1159 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1162 case PROP_ADD_TEAROFFS:
1163 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1166 case PROP_HAS_FRAME:
1167 priv->has_frame = g_value_get_boolean (value);
1169 if (priv->has_entry)
1173 child = gtk_bin_get_child (GTK_BIN (combo_box));
1175 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1180 case PROP_FOCUS_ON_CLICK:
1181 gtk_combo_box_set_focus_on_click (combo_box,
1182 g_value_get_boolean (value));
1185 case PROP_TEAROFF_TITLE:
1186 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1189 case PROP_POPUP_SHOWN:
1190 if (g_value_get_boolean (value))
1191 gtk_combo_box_popup (combo_box);
1193 gtk_combo_box_popdown (combo_box);
1196 case PROP_BUTTON_SENSITIVITY:
1197 gtk_combo_box_set_button_sensitivity (combo_box,
1198 g_value_get_enum (value));
1201 case PROP_POPUP_FIXED_WIDTH:
1202 gtk_combo_box_set_popup_fixed_width (combo_box,
1203 g_value_get_boolean (value));
1206 case PROP_EDITING_CANCELED:
1207 priv->editing_canceled = g_value_get_boolean (value);
1210 case PROP_HAS_ENTRY:
1211 priv->has_entry = g_value_get_boolean (value);
1214 case PROP_ENTRY_TEXT_COLUMN:
1215 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1218 case PROP_ID_COLUMN:
1219 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1222 case PROP_ACTIVE_ID:
1223 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1226 case PROP_CELL_AREA:
1227 /* Construct-only, can only be assigned once */
1228 area = g_value_get_object (value);
1231 if (priv->area != NULL)
1233 g_warning ("cell-area has already been set, ignoring construct property");
1234 g_object_ref_sink (area);
1235 g_object_unref (area);
1238 priv->area = g_object_ref_sink (area);
1243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1249 gtk_combo_box_get_property (GObject *object,
1254 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1255 GtkComboBoxPrivate *priv = combo_box->priv;
1260 g_value_set_object (value, combo_box->priv->model);
1263 case PROP_WRAP_WIDTH:
1264 g_value_set_int (value, combo_box->priv->wrap_width);
1267 case PROP_ROW_SPAN_COLUMN:
1268 g_value_set_int (value, combo_box->priv->row_column);
1271 case PROP_COLUMN_SPAN_COLUMN:
1272 g_value_set_int (value, combo_box->priv->col_column);
1276 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1279 case PROP_ADD_TEAROFFS:
1280 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1283 case PROP_HAS_FRAME:
1284 g_value_set_boolean (value, combo_box->priv->has_frame);
1287 case PROP_FOCUS_ON_CLICK:
1288 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1291 case PROP_TEAROFF_TITLE:
1292 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1295 case PROP_POPUP_SHOWN:
1296 g_value_set_boolean (value, combo_box->priv->popup_shown);
1299 case PROP_BUTTON_SENSITIVITY:
1300 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1303 case PROP_POPUP_FIXED_WIDTH:
1304 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1307 case PROP_EDITING_CANCELED:
1308 g_value_set_boolean (value, priv->editing_canceled);
1311 case PROP_HAS_ENTRY:
1312 g_value_set_boolean (value, priv->has_entry);
1315 case PROP_ENTRY_TEXT_COLUMN:
1316 g_value_set_int (value, priv->text_column);
1319 case PROP_ID_COLUMN:
1320 g_value_set_int (value, priv->id_column);
1323 case PROP_ACTIVE_ID:
1324 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1327 case PROP_CELL_AREA:
1328 g_value_set_object (value, priv->area);
1332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1338 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1339 GtkStateFlags previous)
1341 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1342 GtkComboBoxPrivate *priv = combo_box->priv;
1344 if (gtk_widget_get_realized (widget))
1346 if (priv->tree_view && priv->cell_view)
1348 GtkStyleContext *context;
1349 GtkStateFlags state;
1352 context = gtk_widget_get_style_context (widget);
1353 state = gtk_widget_get_state_flags (widget);
1354 gtk_style_context_get_background_color (context, state, &color);
1356 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1360 gtk_widget_queue_draw (widget);
1364 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1365 GtkStateFlags previous,
1368 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1369 GtkComboBoxPrivate *priv = combo_box->priv;
1371 if (gtk_widget_get_realized (widget))
1373 if (!priv->tree_view && priv->cell_view)
1374 gtk_widget_set_state_flags (priv->cell_view,
1375 gtk_widget_get_state_flags (widget),
1379 gtk_widget_queue_draw (widget);
1382 static GtkWidgetPath *
1383 gtk_combo_box_get_path_for_child (GtkContainer *container,
1386 GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1387 GtkWidgetPath *path;
1389 gboolean found = FALSE;
1390 GList *visible_children, *l;
1391 GtkWidgetPath *sibling_path;
1394 path = _gtk_widget_create_path (GTK_WIDGET (container));
1396 if (gtk_widget_get_visible (child))
1398 visible_children = NULL;
1400 if (priv->button && gtk_widget_get_visible (priv->button))
1401 visible_children = g_list_prepend (visible_children, priv->button);
1403 if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1404 visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1406 widget = gtk_bin_get_child (GTK_BIN (container));
1407 if (widget && gtk_widget_get_visible (widget))
1408 visible_children = g_list_prepend (visible_children, widget);
1412 for (l = visible_children; l; l = l->next)
1416 if (widget == child)
1428 sibling_path = gtk_widget_path_new ();
1430 for (l = visible_children; l; l = l->next)
1431 gtk_widget_path_append_for_widget (sibling_path, l->data);
1433 gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1435 g_list_free (visible_children);
1436 gtk_widget_path_unref (sibling_path);
1440 gtk_widget_path_append_for_widget (path, child);
1447 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1449 GtkComboBoxPrivate *priv = combo_box->priv;
1450 gboolean appears_as_list;
1452 /* if wrap_width > 0, then we are in grid-mode and forced to use
1455 if (priv->wrap_width)
1456 appears_as_list = FALSE;
1458 gtk_widget_style_get (GTK_WIDGET (combo_box),
1459 "appears-as-list", &appears_as_list,
1462 if (appears_as_list)
1464 /* Destroy all the menu mode widgets, if they exist. */
1465 if (GTK_IS_MENU (priv->popup_widget))
1466 gtk_combo_box_menu_destroy (combo_box);
1468 /* Create the list mode widgets, if they don't already exist. */
1469 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1470 gtk_combo_box_list_setup (combo_box);
1474 /* Destroy all the list mode widgets, if they exist. */
1475 if (GTK_IS_TREE_VIEW (priv->tree_view))
1476 gtk_combo_box_list_destroy (combo_box);
1478 /* Create the menu mode widgets, if they don't already exist. */
1479 if (!GTK_IS_MENU (priv->popup_widget))
1480 gtk_combo_box_menu_setup (combo_box, TRUE);
1483 gtk_widget_style_get (GTK_WIDGET (combo_box),
1484 "shadow-type", &priv->shadow_type,
1489 gtk_combo_box_style_updated (GtkWidget *widget)
1491 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1492 GtkComboBoxPrivate *priv = combo_box->priv;
1495 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1497 gtk_combo_box_check_appearance (combo_box);
1499 if (priv->tree_view && priv->cell_view)
1501 GtkStyleContext *context;
1502 GtkStateFlags state;
1505 context = gtk_widget_get_style_context (widget);
1506 state = gtk_widget_get_state_flags (widget);
1507 gtk_style_context_get_background_color (context, state, &color);
1509 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1512 child = gtk_bin_get_child (GTK_BIN (combo_box));
1513 if (GTK_IS_ENTRY (child))
1514 g_object_set (child, "shadow-type",
1515 GTK_SHADOW_NONE == priv->shadow_type ?
1516 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1520 gtk_combo_box_button_toggled (GtkWidget *widget,
1523 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1525 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1527 if (!combo_box->priv->popup_in_progress)
1528 gtk_combo_box_popup (combo_box);
1531 gtk_combo_box_popdown (combo_box);
1535 gtk_combo_box_add (GtkContainer *container,
1538 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1539 GtkComboBoxPrivate *priv = combo_box->priv;
1541 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1543 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1544 "(need an instance of GtkEntry or of a subclass)",
1545 G_OBJECT_TYPE_NAME (widget));
1549 if (priv->cell_view &&
1550 gtk_widget_get_parent (priv->cell_view))
1552 gtk_widget_unparent (priv->cell_view);
1553 _gtk_bin_set_child (GTK_BIN (container), NULL);
1554 gtk_widget_queue_resize (GTK_WIDGET (container));
1557 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1558 _gtk_bin_set_child (GTK_BIN (container), widget);
1560 if (priv->cell_view &&
1561 widget != priv->cell_view)
1563 /* since the cell_view was unparented, it's gone now */
1564 priv->cell_view = NULL;
1566 if (!priv->tree_view && priv->separator)
1568 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1570 priv->separator = NULL;
1572 gtk_widget_queue_resize (GTK_WIDGET (container));
1574 else if (priv->cell_view_frame)
1576 gtk_widget_unparent (priv->cell_view_frame);
1577 priv->cell_view_frame = NULL;
1582 if (priv->has_entry)
1584 /* this flag is a hack to tell the entry to fill its allocation.
1586 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1588 g_signal_connect (widget, "changed",
1589 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1592 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1597 gtk_combo_box_remove (GtkContainer *container,
1600 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1601 GtkComboBoxPrivate *priv = combo_box->priv;
1603 gboolean appears_as_list;
1605 if (priv->has_entry)
1607 GtkWidget *child_widget;
1609 child_widget = gtk_bin_get_child (GTK_BIN (container));
1610 if (widget && widget == child_widget)
1612 g_signal_handlers_disconnect_by_func (widget,
1613 gtk_combo_box_entry_contents_changed,
1615 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1619 if (widget == priv->cell_view)
1620 priv->cell_view = NULL;
1622 gtk_widget_unparent (widget);
1623 _gtk_bin_set_child (GTK_BIN (container), NULL);
1625 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1628 gtk_widget_queue_resize (GTK_WIDGET (container));
1630 if (!priv->tree_view)
1631 appears_as_list = FALSE;
1633 appears_as_list = TRUE;
1635 if (appears_as_list)
1636 gtk_combo_box_list_destroy (combo_box);
1637 else if (GTK_IS_MENU (priv->popup_widget))
1639 gtk_combo_box_menu_destroy (combo_box);
1640 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1641 priv->popup_widget = NULL;
1644 if (!priv->cell_view)
1646 priv->cell_view = gtk_cell_view_new ();
1647 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1648 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1650 gtk_widget_show (priv->cell_view);
1651 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1656 if (appears_as_list)
1657 gtk_combo_box_list_setup (combo_box);
1659 gtk_combo_box_menu_setup (combo_box, TRUE);
1661 if (gtk_tree_row_reference_valid (priv->active_row))
1663 path = gtk_tree_row_reference_get_path (priv->active_row);
1664 gtk_combo_box_set_active_internal (combo_box, path);
1665 gtk_tree_path_free (path);
1668 gtk_combo_box_set_active_internal (combo_box, NULL);
1672 gtk_combo_box_menu_show (GtkWidget *menu,
1675 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1676 GtkComboBoxPrivate *priv = combo_box->priv;
1678 gtk_combo_box_child_show (menu, user_data);
1680 priv->popup_in_progress = TRUE;
1681 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1683 priv->popup_in_progress = FALSE;
1687 gtk_combo_box_menu_hide (GtkWidget *menu,
1690 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1692 gtk_combo_box_child_hide (menu,user_data);
1694 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1699 gtk_combo_box_detacher (GtkWidget *widget,
1702 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1703 GtkComboBoxPrivate *priv = combo_box->priv;
1705 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1707 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1708 gtk_combo_box_menu_show,
1710 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1711 gtk_combo_box_menu_hide,
1714 priv->popup_widget = NULL;
1718 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1721 GtkComboBoxPrivate *priv = combo_box->priv;
1723 if (GTK_IS_MENU (priv->popup_widget))
1725 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1726 priv->popup_widget = NULL;
1728 else if (priv->popup_widget)
1730 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1731 priv->popup_widget);
1732 g_object_unref (priv->popup_widget);
1733 priv->popup_widget = NULL;
1736 if (GTK_IS_MENU (popup))
1738 if (priv->popup_window)
1740 gtk_widget_destroy (priv->popup_window);
1741 priv->popup_window = NULL;
1744 priv->popup_widget = popup;
1747 * Note that we connect to show/hide on the toplevel, not the
1748 * menu itself, since the menu is not shown/hidden when it is
1749 * popped up while torn-off.
1751 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1752 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1753 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1754 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1756 gtk_menu_attach_to_widget (GTK_MENU (popup),
1757 GTK_WIDGET (combo_box),
1758 gtk_combo_box_detacher);
1762 if (!priv->popup_window)
1764 GtkWidget *toplevel;
1766 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1767 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1769 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1770 GDK_WINDOW_TYPE_HINT_COMBO);
1772 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1773 G_CALLBACK (gtk_combo_box_child_show),
1775 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1776 G_CALLBACK (gtk_combo_box_child_hide),
1779 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1780 if (GTK_IS_WINDOW (toplevel))
1782 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1783 GTK_WINDOW (priv->popup_window));
1784 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1785 GTK_WINDOW (toplevel));
1788 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1789 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1790 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1792 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1794 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1797 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1800 gtk_widget_show (priv->scrolled_window);
1802 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1803 priv->scrolled_window);
1806 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1809 gtk_widget_show (popup);
1810 g_object_ref (popup);
1811 priv->popup_widget = popup;
1816 get_widget_padding_and_border (GtkWidget *widget,
1819 GtkStyleContext *context;
1820 GtkStateFlags state;
1823 context = gtk_widget_get_style_context (widget);
1824 state = gtk_style_context_get_state (context);
1826 gtk_style_context_get_padding (context, state, padding);
1827 gtk_style_context_get_border (context, state, &tmp);
1829 padding->top += tmp.top;
1830 padding->right += tmp.right;
1831 padding->bottom += tmp.bottom;
1832 padding->left += tmp.left;
1836 gtk_combo_box_menu_position_below (GtkMenu *menu,
1842 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1843 GtkAllocation child_allocation;
1849 GdkRectangle monitor;
1852 /* FIXME: is using the size request here broken? */
1853 child = gtk_bin_get_child (GTK_BIN (combo_box));
1857 gtk_widget_get_allocation (child, &child_allocation);
1859 if (!gtk_widget_get_has_window (child))
1861 sx += child_allocation.x;
1862 sy += child_allocation.y;
1865 gdk_window_get_root_coords (gtk_widget_get_window (child),
1867 get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding);
1869 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1874 if (combo_box->priv->popup_fixed_width)
1875 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1877 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1879 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1882 *x = sx + child_allocation.width - req.width;
1885 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1886 monitor_num = gdk_screen_get_monitor_at_window (screen,
1887 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1888 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1892 else if (*x + req.width > monitor.x + monitor.width)
1893 *x = monitor.x + monitor.width - req.width;
1895 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1896 *y += child_allocation.height;
1897 else if (*y - monitor.y >= req.height)
1899 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1900 *y += child_allocation.height;
1908 gtk_combo_box_menu_position_over (GtkMenu *menu,
1914 GtkComboBox *combo_box;
1918 GtkAllocation allocation;
1919 GtkAllocation child_allocation;
1926 combo_box = GTK_COMBO_BOX (user_data);
1927 widget = GTK_WIDGET (combo_box);
1929 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1931 gtk_widget_get_allocation (widget, &allocation);
1933 menu_xpos = allocation.x;
1934 menu_ypos = allocation.y + allocation.height / 2 - 2;
1936 if (combo_box->priv->popup_fixed_width)
1937 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1939 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1943 gtk_widget_get_allocation (active, &child_allocation);
1944 menu_ypos -= child_allocation.height / 2;
1947 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1950 child = children->data;
1952 if (active == child)
1955 if (gtk_widget_get_visible (child))
1957 gtk_widget_get_allocation (child, &child_allocation);
1959 menu_ypos -= child_allocation.height;
1962 children = children->next;
1965 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1966 menu_xpos = menu_xpos + allocation.width - menu_width;
1968 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1969 menu_xpos, menu_ypos,
1970 &menu_xpos, &menu_ypos);
1972 /* Clamp the position on screen */
1973 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1977 else if ((menu_xpos + menu_width) > screen_width)
1978 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1987 gtk_combo_box_menu_position (GtkMenu *menu,
1993 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1994 GtkComboBoxPrivate *priv = combo_box->priv;
1995 GtkWidget *menu_item;
1997 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1998 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2001 /* FIXME handle nested menus better */
2002 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2004 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2007 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2010 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2011 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2012 GDK_WINDOW_TYPE_HINT_COMBO);
2016 gtk_combo_box_list_position (GtkComboBox *combo_box,
2022 GtkComboBoxPrivate *priv = combo_box->priv;
2023 GtkAllocation allocation;
2026 GdkRectangle monitor;
2027 GtkRequisition popup_req;
2028 GtkPolicyType hpolicy, vpolicy;
2031 /* under windows, the drop down list is as wide as the combo box itself.
2033 GtkWidget *widget = GTK_WIDGET (combo_box);
2037 gtk_widget_get_allocation (widget, &allocation);
2039 if (!gtk_widget_get_has_window (widget))
2045 window = gtk_widget_get_window (widget);
2047 gdk_window_get_root_coords (gtk_widget_get_window (widget),
2050 *width = allocation.width;
2052 hpolicy = vpolicy = GTK_POLICY_NEVER;
2053 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2056 if (combo_box->priv->popup_fixed_width)
2058 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2060 if (popup_req.width > *width)
2062 hpolicy = GTK_POLICY_ALWAYS;
2063 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2069 /* XXX This code depends on treeviews properly reporting their natural width
2070 * list-mode menus won't fill up to their natural width until then */
2071 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2073 if (popup_req.width > *width)
2075 hpolicy = GTK_POLICY_NEVER;
2076 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2079 *width = popup_req.width;
2083 *height = popup_req.height;
2085 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2086 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2087 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2089 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2090 *x = *x + allocation.width - *width;
2094 else if (*x + *width > monitor.x + monitor.width)
2095 *x = monitor.x + monitor.width - *width;
2097 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2098 *y += allocation.height;
2099 else if (*y - *height >= monitor.y)
2101 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2103 *y += allocation.height;
2104 *height = monitor.y + monitor.height - *y;
2108 *height = *y - monitor.y;
2112 if (popup_req.height > *height)
2114 vpolicy = GTK_POLICY_ALWAYS;
2116 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2122 cell_layout_is_sensitive (GtkCellLayout *layout)
2124 GList *cells, *list;
2127 cells = gtk_cell_layout_get_cells (layout);
2130 for (list = cells; list; list = list->next)
2132 g_object_get (list->data, "sensitive", &sensitive, NULL);
2137 g_list_free (cells);
2143 cell_is_sensitive (GtkCellRenderer *cell,
2146 gboolean *sensitive = data;
2148 g_object_get (cell, "sensitive", sensitive, NULL);
2154 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2157 GtkComboBoxPrivate *priv = combo_box->priv;
2159 if (priv->row_separator_func)
2161 if (priv->row_separator_func (priv->model, iter,
2162 priv->row_separator_data))
2170 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2174 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2183 update_menu_sensitivity (GtkComboBox *combo_box,
2186 GtkComboBoxPrivate *priv = combo_box->priv;
2187 GList *children, *child;
2188 GtkWidget *item, *submenu, *separator;
2189 GtkWidget *cell_view;
2195 children = gtk_container_get_children (GTK_CONTAINER (menu));
2197 for (child = children; child; child = child->next)
2199 item = GTK_WIDGET (child->data);
2200 cell_view = gtk_bin_get_child (GTK_BIN (item));
2202 if (!GTK_IS_CELL_VIEW (cell_view))
2205 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2206 if (submenu != NULL)
2208 gtk_widget_set_sensitive (item, TRUE);
2209 update_menu_sensitivity (combo_box, submenu);
2213 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2215 if (menu != priv->popup_widget && child == children)
2217 separator = GTK_WIDGET (child->next->data);
2218 g_object_set (item, "visible", sensitive, NULL);
2219 g_object_set (separator, "visible", sensitive, NULL);
2222 gtk_widget_set_sensitive (item, sensitive);
2226 g_list_free (children);
2230 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2232 guint32 activate_time)
2234 GtkComboBoxPrivate *priv = combo_box->priv;
2237 gint width, min_width, nat_width;
2239 update_menu_sensitivity (combo_box, priv->popup_widget);
2242 if (gtk_tree_row_reference_valid (priv->active_row))
2244 path = gtk_tree_row_reference_get_path (priv->active_row);
2245 active_item = gtk_tree_path_get_indices (path)[0];
2246 gtk_tree_path_free (path);
2248 if (priv->add_tearoffs)
2252 /* FIXME handle nested menus better */
2253 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2255 if (priv->wrap_width == 0)
2257 GtkAllocation allocation;
2259 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2260 width = allocation.width;
2261 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2262 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2264 if (combo_box->priv->popup_fixed_width)
2265 width = MAX (width, min_width);
2267 width = MAX (width, nat_width);
2269 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2272 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2274 gtk_combo_box_menu_position, combo_box,
2275 button, activate_time);
2279 popup_grab_on_window (GdkWindow *window,
2280 GdkDevice *keyboard,
2282 guint32 activate_time)
2285 gdk_device_grab (keyboard, window,
2286 GDK_OWNERSHIP_WINDOW, TRUE,
2287 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2288 NULL, activate_time) != GDK_GRAB_SUCCESS)
2292 gdk_device_grab (pointer, window,
2293 GDK_OWNERSHIP_WINDOW, TRUE,
2294 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2295 GDK_POINTER_MOTION_MASK,
2296 NULL, activate_time) != GDK_GRAB_SUCCESS)
2299 gdk_device_ungrab (keyboard, activate_time);
2308 * gtk_combo_box_popup:
2309 * @combo_box: a #GtkComboBox
2311 * Pops up the menu or dropdown list of @combo_box.
2313 * This function is mostly intended for use by accessibility technologies;
2314 * applications should have little use for it.
2319 gtk_combo_box_popup (GtkComboBox *combo_box)
2321 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2323 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2327 * gtk_combo_box_popup_for_device:
2328 * @combo_box: a #GtkComboBox
2329 * @device: a #GdkDevice
2331 * Pops up the menu or dropdown list of @combo_box, the popup window
2332 * will be grabbed so only @device and its associated pointer/keyboard
2333 * are the only #GdkDevice<!-- -->s able to send events to it.
2338 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2341 GtkComboBoxPrivate *priv = combo_box->priv;
2342 gint x, y, width, height;
2343 GtkTreePath *path = NULL, *ppath;
2344 GtkWidget *toplevel;
2345 GdkDevice *keyboard, *pointer;
2348 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2349 g_return_if_fail (GDK_IS_DEVICE (device));
2351 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2354 if (gtk_widget_get_mapped (priv->popup_widget))
2357 if (priv->grab_pointer && priv->grab_keyboard)
2360 time = gtk_get_current_event_time ();
2362 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2365 pointer = gdk_device_get_associated_device (device);
2370 keyboard = gdk_device_get_associated_device (device);
2373 if (GTK_IS_MENU (priv->popup_widget))
2375 gtk_combo_box_menu_popup (combo_box,
2376 priv->activate_button,
2377 priv->activate_time);
2381 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2382 if (GTK_IS_WINDOW (toplevel))
2383 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2384 GTK_WINDOW (priv->popup_window));
2386 gtk_widget_show_all (priv->scrolled_window);
2387 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2389 gtk_widget_set_size_request (priv->popup_window, width, height);
2390 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2392 if (gtk_tree_row_reference_valid (priv->active_row))
2394 path = gtk_tree_row_reference_get_path (priv->active_row);
2395 ppath = gtk_tree_path_copy (path);
2396 if (gtk_tree_path_up (ppath))
2397 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2399 gtk_tree_path_free (ppath);
2401 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2405 gtk_widget_show (priv->popup_window);
2409 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2411 gtk_tree_path_free (path);
2414 gtk_widget_grab_focus (priv->popup_window);
2415 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2418 if (!gtk_widget_has_focus (priv->tree_view))
2419 gtk_widget_grab_focus (priv->tree_view);
2421 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2422 keyboard, pointer, time))
2424 gtk_widget_hide (priv->popup_window);
2428 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2429 priv->grab_pointer = pointer;
2430 priv->grab_keyboard = keyboard;
2434 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2438 device = gtk_get_current_event_device ();
2442 GdkDeviceManager *device_manager;
2443 GdkDisplay *display;
2446 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2447 device_manager = gdk_display_get_device_manager (display);
2449 /* No device was set, pick the first master device */
2450 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2451 device = devices->data;
2452 g_list_free (devices);
2455 gtk_combo_box_popup_for_device (combo_box, device);
2459 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2461 if (combo_box->priv->popup_shown)
2463 gtk_combo_box_popdown (combo_box);
2471 * gtk_combo_box_popdown:
2472 * @combo_box: a #GtkComboBox
2474 * Hides the menu or dropdown list of @combo_box.
2476 * This function is mostly intended for use by accessibility technologies;
2477 * applications should have little use for it.
2482 gtk_combo_box_popdown (GtkComboBox *combo_box)
2484 GtkComboBoxPrivate *priv = combo_box->priv;
2486 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2488 if (GTK_IS_MENU (priv->popup_widget))
2490 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2494 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2497 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2498 gtk_widget_hide (priv->popup_window);
2499 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2502 priv->grab_pointer = NULL;
2503 priv->grab_keyboard = NULL;
2506 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2507 GtkAllocation button_allocation; \
2508 gtk_widget_get_preferred_size (combo_box->priv->button, \
2512 button_allocation.x = allocation->x; \
2514 button_allocation.x = allocation->x + allocation->width \
2517 button_allocation.y = allocation->y; \
2518 button_allocation.width = MAX (1, req.width); \
2519 button_allocation.height = allocation->height; \
2520 button_allocation.height = MAX (1, button_allocation.height); \
2522 gtk_widget_size_allocate (combo_box->priv->button, \
2523 &button_allocation);
2527 gtk_combo_box_size_allocate (GtkWidget *widget,
2528 GtkAllocation *allocation)
2530 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2531 GtkComboBoxPrivate *priv = combo_box->priv;
2532 GtkWidget *child_widget;
2533 GtkAllocation child;
2535 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2538 gtk_widget_set_allocation (widget, allocation);
2539 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2540 get_widget_padding_and_border (widget, &padding);
2542 allocation->x += padding.left;
2543 allocation->y += padding.top;
2544 allocation->width -= padding.left + padding.right;
2545 allocation->height -= padding.top + padding.bottom;
2547 if (!priv->tree_view)
2549 if (priv->cell_view)
2551 GtkBorder button_padding;
2555 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2556 get_widget_padding_and_border (priv->button, &button_padding);
2558 /* menu mode; child_widget is priv->cell_view.
2559 * Allocate the button to the full combobox allocation (minus the
2562 gtk_widget_size_allocate (priv->button, allocation);
2564 child.x = allocation->x;
2565 child.y = allocation->y;
2566 width = allocation->width;
2567 child.height = allocation->height;
2569 if (!priv->is_cell_renderer)
2571 /* restrict allocation of the child into the button box
2572 * if we're not in cell renderer mode.
2574 child.x += border_width + button_padding.left;
2575 child.y += border_width + button_padding.top;
2576 width -= 2 * border_width +
2577 button_padding.left + button_padding.right;
2578 child.height -= 2 * border_width +
2579 button_padding.top + button_padding.bottom;
2582 /* allocate the box containing the separator and the arrow */
2583 gtk_widget_get_preferred_size (priv->box, &req, NULL);
2584 child.width = req.width;
2586 child.x += width - req.width;
2587 child.width = MAX (1, child.width);
2588 child.height = MAX (1, child.height);
2589 gtk_widget_size_allocate (priv->box, &child);
2593 child.x += req.width;
2594 child.width = allocation->x + allocation->width
2595 - border_width - child.x - button_padding.right;
2599 child.width = child.x;
2600 child.x = allocation->x
2601 + border_width + button_padding.left;
2602 child.width -= child.x;
2605 if (gtk_widget_get_visible (priv->popup_widget))
2607 gint width, menu_width;
2609 if (priv->wrap_width == 0)
2611 GtkAllocation combo_box_allocation;
2613 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2614 width = combo_box_allocation.width;
2615 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2617 if (combo_box->priv->popup_fixed_width)
2618 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2620 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2622 gtk_widget_set_size_request (priv->popup_widget,
2623 MAX (width, menu_width), -1);
2626 /* reposition the menu after giving it a new width */
2627 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2630 child.width = MAX (1, child.width);
2631 child.height = MAX (1, child.height);
2632 gtk_widget_size_allocate (child_widget, &child);
2636 /* menu mode; child_widget has been set with gtk_container_add().
2637 * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2638 * Allocate the button at the far end, according to the direction
2641 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2643 /* After the macro, button_allocation has the button allocation rect */
2646 child.x = button_allocation.x + button_allocation.width;
2648 child.x = allocation->x;
2650 child.y = allocation->y;
2651 child.width = allocation->width - button_allocation.width;
2652 child.height = button_allocation.height;
2654 child.width = MAX (1, child.width);
2656 gtk_widget_size_allocate (child_widget, &child);
2661 /* list mode; child_widget might be either priv->cell_view or a child
2662 * added with gtk_container_add().
2665 /* After the macro, button_allocation has the button allocation rect */
2666 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2669 child.x = button_allocation.x + button_allocation.width;
2671 child.x = allocation->x;
2673 child.y = allocation->y;
2674 child.width = allocation->width - button_allocation.width;
2675 child.height = button_allocation.height;
2677 if (priv->cell_view_frame)
2679 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2681 /* restrict allocation of the child into the frame box if it's present */
2682 if (priv->has_frame)
2684 GtkBorder frame_padding;
2687 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2688 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2690 child.x += border_width + frame_padding.left;
2691 child.y += border_width + frame_padding.right;
2692 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2693 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2697 if (gtk_widget_get_visible (priv->popup_window))
2699 gint x, y, width, height;
2700 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2701 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2702 gtk_widget_set_size_request (priv->popup_window, width, height);
2705 /* allocate the child */
2706 child.width = MAX (1, child.width);
2707 child.height = MAX (1, child.height);
2708 gtk_widget_size_allocate (child_widget, &child);
2712 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2715 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2717 GtkComboBoxPrivate *priv = combo_box->priv;
2721 g_signal_handler_disconnect (priv->model,
2723 g_signal_handler_disconnect (priv->model,
2725 g_signal_handler_disconnect (priv->model,
2726 priv->reordered_id);
2727 g_signal_handler_disconnect (priv->model,
2733 g_object_unref (priv->model);
2737 if (priv->active_row)
2739 gtk_tree_row_reference_free (priv->active_row);
2740 priv->active_row = NULL;
2743 if (priv->cell_view)
2744 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2748 gtk_combo_box_forall (GtkContainer *container,
2749 gboolean include_internals,
2750 GtkCallback callback,
2751 gpointer callback_data)
2753 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2754 GtkComboBoxPrivate *priv = combo_box->priv;
2757 if (include_internals)
2760 (* callback) (priv->button, callback_data);
2761 if (priv->cell_view_frame)
2762 (* callback) (priv->cell_view_frame, callback_data);
2765 child = gtk_bin_get_child (GTK_BIN (container));
2767 (* callback) (child, callback_data);
2771 gtk_combo_box_child_show (GtkWidget *widget,
2772 GtkComboBox *combo_box)
2774 GtkComboBoxPrivate *priv = combo_box->priv;
2776 priv->popup_shown = TRUE;
2777 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2781 gtk_combo_box_child_hide (GtkWidget *widget,
2782 GtkComboBox *combo_box)
2784 GtkComboBoxPrivate *priv = combo_box->priv;
2786 priv->popup_shown = FALSE;
2787 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2791 gtk_combo_box_draw (GtkWidget *widget,
2794 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2795 GtkComboBoxPrivate *priv = combo_box->priv;
2797 if (priv->shadow_type != GTK_SHADOW_NONE)
2799 GtkStyleContext *context;
2801 context = gtk_widget_get_style_context (widget);
2803 gtk_render_background (context, cr, 0, 0,
2804 gtk_widget_get_allocated_width (widget),
2805 gtk_widget_get_allocated_height (widget));
2806 gtk_render_frame (context, cr, 0, 0,
2807 gtk_widget_get_allocated_width (widget),
2808 gtk_widget_get_allocated_height (widget));
2811 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2814 if (priv->tree_view && priv->cell_view_frame)
2816 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2817 priv->cell_view_frame, cr);
2820 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2821 gtk_bin_get_child (GTK_BIN (widget)),
2837 path_visible (GtkTreeView *view,
2843 /* Note that we rely on the fact that collapsed rows don't have nodes
2845 return _gtk_tree_view_find_node (view, path, &tree, &node);
2849 tree_next_func (GtkTreeModel *model,
2854 SearchData *search_data = (SearchData *)data;
2856 if (search_data->found)
2858 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2861 if (search_data->visible &&
2862 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2865 search_data->set = TRUE;
2866 search_data->iter = *iter;
2871 if (gtk_tree_path_compare (path, search_data->path) == 0)
2872 search_data->found = TRUE;
2878 tree_next (GtkComboBox *combo,
2879 GtkTreeModel *model,
2884 SearchData search_data;
2886 search_data.combo = combo;
2887 search_data.path = gtk_tree_model_get_path (model, iter);
2888 search_data.visible = visible;
2889 search_data.found = FALSE;
2890 search_data.set = FALSE;
2892 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2894 *next = search_data.iter;
2896 gtk_tree_path_free (search_data.path);
2898 return search_data.set;
2902 tree_prev_func (GtkTreeModel *model,
2907 SearchData *search_data = (SearchData *)data;
2909 if (gtk_tree_path_compare (path, search_data->path) == 0)
2911 search_data->found = TRUE;
2915 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2918 if (search_data->visible &&
2919 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2922 search_data->set = TRUE;
2923 search_data->iter = *iter;
2929 tree_prev (GtkComboBox *combo,
2930 GtkTreeModel *model,
2935 SearchData search_data;
2937 search_data.combo = combo;
2938 search_data.path = gtk_tree_model_get_path (model, iter);
2939 search_data.visible = visible;
2940 search_data.found = FALSE;
2941 search_data.set = FALSE;
2943 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2945 *prev = search_data.iter;
2947 gtk_tree_path_free (search_data.path);
2949 return search_data.set;
2953 tree_last_func (GtkTreeModel *model,
2958 SearchData *search_data = (SearchData *)data;
2960 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2963 /* Note that we rely on the fact that collapsed rows don't have nodes
2965 if (search_data->visible &&
2966 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2969 search_data->set = TRUE;
2970 search_data->iter = *iter;
2976 tree_last (GtkComboBox *combo,
2977 GtkTreeModel *model,
2981 SearchData search_data;
2983 search_data.combo = combo;
2984 search_data.visible = visible;
2985 search_data.set = FALSE;
2987 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2989 *last = search_data.iter;
2991 return search_data.set;
2996 tree_first_func (GtkTreeModel *model,
3001 SearchData *search_data = (SearchData *)data;
3003 if (!tree_column_row_is_sensitive (search_data->combo, iter))
3006 if (search_data->visible &&
3007 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3010 search_data->set = TRUE;
3011 search_data->iter = *iter;
3017 tree_first (GtkComboBox *combo,
3018 GtkTreeModel *model,
3022 SearchData search_data;
3024 search_data.combo = combo;
3025 search_data.visible = visible;
3026 search_data.set = FALSE;
3028 gtk_tree_model_foreach (model, tree_first_func, &search_data);
3030 *first = search_data.iter;
3032 return search_data.set;
3036 gtk_combo_box_scroll_event (GtkWidget *widget,
3037 GdkEventScroll *event)
3039 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3042 GtkTreeIter new_iter;
3044 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3047 if (event->direction == GDK_SCROLL_UP)
3048 found = tree_prev (combo_box, combo_box->priv->model,
3049 &iter, &new_iter, FALSE);
3051 found = tree_next (combo_box, combo_box->priv->model,
3052 &iter, &new_iter, FALSE);
3055 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3064 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3068 GtkComboBoxPrivate *priv = combo->priv;
3070 if (priv->row_separator_func)
3071 return priv->row_separator_func (model, iter, priv->row_separator_data);
3077 gtk_combo_box_header_func (GtkTreeModel *model,
3081 /* Every submenu has a selectable header, however we
3082 * can expose a method to make that configurable by
3083 * the user (like row_separator_func is done) */
3088 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3089 gboolean add_children)
3091 GtkComboBoxPrivate *priv = combo_box->priv;
3095 child = gtk_bin_get_child (GTK_BIN (combo_box));
3097 if (priv->cell_view)
3099 priv->button = gtk_toggle_button_new ();
3100 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3101 priv->focus_on_click);
3103 g_signal_connect (priv->button, "toggled",
3104 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3105 gtk_widget_set_parent (priv->button,
3106 gtk_widget_get_parent (child));
3108 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3109 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3111 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3112 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3114 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3115 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3116 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3118 gtk_widget_show_all (priv->button);
3122 priv->button = gtk_toggle_button_new ();
3123 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3124 priv->focus_on_click);
3126 g_signal_connect (priv->button, "toggled",
3127 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3128 gtk_widget_set_parent (priv->button,
3129 gtk_widget_get_parent (child));
3131 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3132 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3133 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3134 gtk_widget_show_all (priv->button);
3137 g_signal_connect (priv->button, "button-press-event",
3138 G_CALLBACK (gtk_combo_box_menu_button_press),
3140 g_signal_connect (priv->button, "state-flags-changed",
3141 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3144 /* create our funky menu */
3145 menu = _gtk_tree_menu_new_with_area (priv->area);
3146 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3148 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3150 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3151 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3152 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3153 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3154 combo_box->priv->add_tearoffs);
3156 g_signal_connect (menu, "menu-activate",
3157 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3159 /* Chain our row_separator_func through */
3160 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3161 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3164 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3165 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3168 g_signal_connect (menu, "key-press-event",
3169 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3170 gtk_combo_box_set_popup_widget (combo_box, menu);
3172 gtk_combo_box_update_title (combo_box);
3176 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3178 GtkComboBoxPrivate *priv = combo_box->priv;
3180 g_signal_handlers_disconnect_matched (priv->button,
3181 G_SIGNAL_MATCH_DATA,
3183 gtk_combo_box_menu_button_press, NULL);
3184 g_signal_handlers_disconnect_matched (priv->button,
3185 G_SIGNAL_MATCH_DATA,
3187 gtk_combo_box_button_state_flags_changed, combo_box);
3188 g_signal_handlers_disconnect_matched (priv->popup_widget,
3189 G_SIGNAL_MATCH_DATA,
3191 gtk_combo_box_menu_activate, combo_box);
3193 /* unparent will remove our latest ref */
3194 gtk_widget_unparent (priv->button);
3197 priv->button = NULL;
3199 priv->separator = NULL;
3201 /* changing the popup window will unref the menu and the children */
3206 gtk_combo_box_menu_button_press (GtkWidget *widget,
3207 GdkEventButton *event,
3210 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3211 GtkComboBoxPrivate *priv = combo_box->priv;
3213 if (GTK_IS_MENU (priv->popup_widget) &&
3214 event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3216 if (priv->focus_on_click &&
3217 !gtk_widget_has_focus (priv->button))
3218 gtk_widget_grab_focus (priv->button);
3220 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3229 gtk_combo_box_menu_activate (GtkWidget *menu,
3231 GtkComboBox *combo_box)
3235 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3236 gtk_combo_box_set_active_iter (combo_box, &iter);
3238 g_object_set (combo_box,
3239 "editing-canceled", FALSE,
3245 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3248 gboolean sensitive = TRUE; /* fool code checkers */
3250 if (!combo_box->priv->button)
3253 switch (combo_box->priv->button_sensitivity)
3255 case GTK_SENSITIVITY_ON:
3258 case GTK_SENSITIVITY_OFF:
3261 case GTK_SENSITIVITY_AUTO:
3262 sensitive = combo_box->priv->model &&
3263 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3266 g_assert_not_reached ();
3270 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3272 /* In list-mode, we also need to update sensitivity of the event box */
3273 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3274 && combo_box->priv->cell_view)
3275 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3279 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3284 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3286 if (combo_box->priv->tree_view)
3287 gtk_combo_box_list_popup_resize (combo_box);
3289 gtk_combo_box_update_sensitivity (combo_box);
3293 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3297 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3298 GtkComboBoxPrivate *priv = combo_box->priv;
3300 if (!gtk_tree_row_reference_valid (priv->active_row))
3302 if (priv->cell_view)
3303 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3304 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3307 if (priv->tree_view)
3308 gtk_combo_box_list_popup_resize (combo_box);
3310 gtk_combo_box_update_sensitivity (combo_box);
3314 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3320 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3324 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3329 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3330 GtkComboBoxPrivate *priv = combo_box->priv;
3331 GtkTreePath *active_path;
3333 /* FIXME this belongs to GtkCellView */
3334 if (gtk_tree_row_reference_valid (priv->active_row))
3336 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3337 if (gtk_tree_path_compare (path, active_path) == 0 &&
3339 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3340 gtk_tree_path_free (active_path);
3343 if (priv->tree_view)
3344 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3348 list_popup_resize_idle (gpointer user_data)
3350 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3351 GtkComboBoxPrivate *priv = combo_box->priv;
3352 gint x, y, width, height;
3354 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3356 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3358 gtk_widget_set_size_request (priv->popup_window, width, height);
3359 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3362 priv->resize_idle_id = 0;
3368 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3370 GtkComboBoxPrivate *priv = combo_box->priv;
3372 if (!priv->resize_idle_id)
3373 priv->resize_idle_id =
3374 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3378 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3383 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3385 gtk_combo_box_list_popup_resize (combo_box);
3394 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3396 GtkComboBoxPrivate *priv = combo_box->priv;
3397 GtkTreeSelection *sel;
3399 GtkWidget *widget = GTK_WIDGET (combo_box);
3401 priv->button = gtk_toggle_button_new ();
3402 child = gtk_bin_get_child (GTK_BIN (combo_box));
3403 gtk_widget_set_parent (priv->button,
3404 gtk_widget_get_parent (child));
3405 g_signal_connect (priv->button, "button-press-event",
3406 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3407 g_signal_connect (priv->button, "toggled",
3408 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3410 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3411 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3412 priv->separator = NULL;
3413 gtk_widget_show_all (priv->button);
3415 if (priv->cell_view)
3417 GtkStyleContext *context;
3418 GtkStateFlags state;
3421 context = gtk_widget_get_style_context (widget);
3422 state = gtk_widget_get_state_flags (widget);
3423 gtk_style_context_get_background_color (context, state, &color);
3425 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3427 priv->box = gtk_event_box_new ();
3428 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3431 if (priv->has_frame)
3433 priv->cell_view_frame = gtk_frame_new (NULL);
3434 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3439 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3440 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3444 gtk_widget_set_parent (priv->cell_view_frame,
3445 gtk_widget_get_parent (child));
3446 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3447 gtk_widget_show_all (priv->cell_view_frame);
3449 g_signal_connect (priv->box, "button-press-event",
3450 G_CALLBACK (gtk_combo_box_list_button_pressed),
3454 priv->tree_view = gtk_tree_view_new ();
3455 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3456 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3457 gtk_tree_selection_set_select_function (sel,
3458 gtk_combo_box_list_select_func,
3460 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3462 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3465 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3466 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3470 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3472 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3473 gtk_tree_view_column_new_with_area (priv->area));
3475 if (gtk_tree_row_reference_valid (priv->active_row))
3479 path = gtk_tree_row_reference_get_path (priv->active_row);
3480 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3482 gtk_tree_path_free (path);
3485 /* set sample/popup widgets */
3486 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3488 g_signal_connect (priv->tree_view, "key-press-event",
3489 G_CALLBACK (gtk_combo_box_list_key_press),
3491 g_signal_connect (priv->tree_view, "enter-notify-event",
3492 G_CALLBACK (gtk_combo_box_list_enter_notify),
3494 g_signal_connect (priv->tree_view, "row-expanded",
3495 G_CALLBACK (gtk_combo_box_model_row_expanded),
3497 g_signal_connect (priv->tree_view, "row-collapsed",
3498 G_CALLBACK (gtk_combo_box_model_row_expanded),
3500 g_signal_connect (priv->popup_window, "button-press-event",
3501 G_CALLBACK (gtk_combo_box_list_button_pressed),
3503 g_signal_connect (priv->popup_window, "button-release-event",
3504 G_CALLBACK (gtk_combo_box_list_button_released),
3507 gtk_widget_show (priv->tree_view);
3509 gtk_combo_box_update_sensitivity (combo_box);
3513 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3515 GtkComboBoxPrivate *priv = combo_box->priv;
3517 /* disconnect signals */
3518 g_signal_handlers_disconnect_matched (priv->tree_view,
3519 G_SIGNAL_MATCH_DATA,
3520 0, 0, NULL, NULL, combo_box);
3521 g_signal_handlers_disconnect_matched (priv->button,
3522 G_SIGNAL_MATCH_DATA,
3524 gtk_combo_box_list_button_pressed,
3526 g_signal_handlers_disconnect_matched (priv->popup_window,
3527 G_SIGNAL_MATCH_DATA,
3529 gtk_combo_box_list_button_pressed,
3531 g_signal_handlers_disconnect_matched (priv->popup_window,
3532 G_SIGNAL_MATCH_DATA,
3534 gtk_combo_box_list_button_released,
3537 g_signal_handlers_disconnect_matched (priv->popup_window,
3538 G_SIGNAL_MATCH_DATA,
3540 gtk_combo_box_child_show,
3543 g_signal_handlers_disconnect_matched (priv->popup_window,
3544 G_SIGNAL_MATCH_DATA,
3546 gtk_combo_box_child_hide,
3550 g_signal_handlers_disconnect_matched (priv->box,
3551 G_SIGNAL_MATCH_DATA,
3553 gtk_combo_box_list_button_pressed,
3556 /* destroy things (unparent will kill the latest ref from us)
3557 * last unref on button will destroy the arrow
3559 gtk_widget_unparent (priv->button);
3560 priv->button = NULL;
3563 if (priv->cell_view)
3565 g_object_set (priv->cell_view,
3566 "background-set", FALSE,
3570 if (priv->cell_view_frame)
3572 gtk_widget_unparent (priv->cell_view_frame);
3573 priv->cell_view_frame = NULL;
3577 if (priv->scroll_timer)
3579 g_source_remove (priv->scroll_timer);
3580 priv->scroll_timer = 0;
3583 if (priv->resize_idle_id)
3585 g_source_remove (priv->resize_idle_id);
3586 priv->resize_idle_id = 0;
3589 gtk_widget_destroy (priv->tree_view);
3591 priv->tree_view = NULL;
3592 if (priv->popup_widget)
3594 g_object_unref (priv->popup_widget);
3595 priv->popup_widget = NULL;
3602 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3603 GdkEventButton *event,
3606 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3607 GtkComboBoxPrivate *priv = combo_box->priv;
3609 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3611 if (ewidget == priv->popup_window)
3614 if ((ewidget != priv->button && ewidget != priv->box) ||
3615 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3618 if (priv->focus_on_click &&
3619 !gtk_widget_has_focus (priv->button))
3620 gtk_widget_grab_focus (priv->button);
3622 gtk_combo_box_popup_for_device (combo_box, event->device);
3624 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3626 priv->auto_scroll = FALSE;
3627 if (priv->scroll_timer == 0)
3628 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3629 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3632 priv->popup_in_progress = TRUE;
3638 gtk_combo_box_list_button_released (GtkWidget *widget,
3639 GdkEventButton *event,
3643 GtkTreePath *path = NULL;
3646 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3647 GtkComboBoxPrivate *priv = combo_box->priv;
3649 gboolean popup_in_progress = FALSE;
3651 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3653 if (priv->popup_in_progress)
3655 popup_in_progress = TRUE;
3656 priv->popup_in_progress = FALSE;
3659 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3661 if (priv->scroll_timer)
3663 g_source_remove (priv->scroll_timer);
3664 priv->scroll_timer = 0;
3667 if (ewidget != priv->tree_view)
3669 if ((ewidget == priv->button ||
3670 ewidget == priv->box) &&
3671 !popup_in_progress &&
3672 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3674 gtk_combo_box_popdown (combo_box);
3678 /* released outside treeview */
3679 if (ewidget != priv->button && ewidget != priv->box)
3681 gtk_combo_box_popdown (combo_box);
3689 /* select something cool */
3690 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3696 return TRUE; /* clicked outside window? */
3698 gtk_tree_model_get_iter (priv->model, &iter, path);
3699 gtk_tree_path_free (path);
3701 gtk_combo_box_popdown (combo_box);
3703 if (tree_column_row_is_sensitive (combo_box, &iter))
3704 gtk_combo_box_set_active_iter (combo_box, &iter);
3710 gtk_combo_box_menu_key_press (GtkWidget *widget,
3714 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3716 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3718 /* The menu hasn't managed the
3719 * event, forward it to the combobox
3721 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3728 gtk_combo_box_list_key_press (GtkWidget *widget,
3732 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3735 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3736 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3738 GtkTreeModel *model = NULL;
3740 gtk_combo_box_popdown (combo_box);
3742 if (combo_box->priv->model)
3744 GtkTreeSelection *sel;
3746 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3748 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3749 gtk_combo_box_set_active_iter (combo_box, &iter);
3755 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3757 /* The list hasn't managed the
3758 * event, forward it to the combobox
3760 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3767 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3772 GtkAllocation allocation;
3773 GtkWidget *tree_view = combo_box->priv->tree_view;
3776 gtk_widget_get_allocation (tree_view, &allocation);
3778 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3779 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3781 if (x <= allocation.x &&
3782 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3784 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3785 gtk_adjustment_set_value (adj, value);
3787 else if (x >= allocation.x + allocation.width &&
3788 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3790 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3791 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3795 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3796 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3798 if (y <= allocation.y &&
3799 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3801 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3802 gtk_adjustment_set_value (adj, value);
3804 else if (y >= allocation.height &&
3805 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3807 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3808 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3814 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3816 GtkComboBoxPrivate *priv = combo_box->priv;
3819 if (priv->auto_scroll)
3821 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3824 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3831 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3832 GdkEventCrossing *event,
3835 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3837 combo_box->priv->auto_scroll = TRUE;
3843 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3844 GtkTreeModel *model,
3846 gboolean path_currently_selected,
3849 GList *list, *columns;
3850 gboolean sensitive = FALSE;
3852 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3854 for (list = columns; list && !sensitive; list = list->next)
3856 GList *cells, *cell;
3857 gboolean cell_sensitive, cell_visible;
3859 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3861 if (!gtk_tree_view_column_get_visible (column))
3864 gtk_tree_model_get_iter (model, &iter, path);
3865 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3868 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3871 g_object_get (cell->data,
3872 "sensitive", &cell_sensitive,
3873 "visible", &cell_visible,
3876 if (cell_visible && cell_sensitive)
3885 g_list_free (cells);
3888 g_list_free (columns);
3894 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3899 /* XXX Do nothing ? */
3903 * GtkCellLayout implementation
3905 static GtkCellArea *
3906 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3908 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3909 GtkComboBoxPrivate *priv = combo->priv;
3911 if (G_UNLIKELY (!priv->area))
3913 priv->area = gtk_cell_area_box_new ();
3914 g_object_ref_sink (priv->area);
3925 * gtk_combo_box_new:
3927 * Creates a new empty #GtkComboBox.
3929 * Return value: A new #GtkComboBox.
3934 gtk_combo_box_new (void)
3936 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3940 * gtk_combo_box_new_with_area:
3941 * @area: the #GtkCellArea to use to layout cell renderers
3943 * Creates a new empty #GtkComboBox using @area to layout cells.
3945 * Return value: A new #GtkComboBox.
3948 gtk_combo_box_new_with_area (GtkCellArea *area)
3950 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3954 * gtk_combo_box_new_with_area_and_entry:
3955 * @area: the #GtkCellArea to use to layout cell renderers
3957 * Creates a new empty #GtkComboBox with an entry.
3959 * The new combo box will use @area to layout cells.
3961 * Return value: A new #GtkComboBox.
3964 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3966 return g_object_new (GTK_TYPE_COMBO_BOX,
3974 * gtk_combo_box_new_with_entry:
3976 * Creates a new empty #GtkComboBox with an entry.
3978 * Return value: A new #GtkComboBox.
3981 gtk_combo_box_new_with_entry (void)
3983 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3987 * gtk_combo_box_new_with_model:
3988 * @model: A #GtkTreeModel.
3990 * Creates a new #GtkComboBox with the model initialized to @model.
3992 * Return value: A new #GtkComboBox.
3997 gtk_combo_box_new_with_model (GtkTreeModel *model)
3999 GtkComboBox *combo_box;
4001 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4003 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4005 return GTK_WIDGET (combo_box);
4009 * gtk_combo_box_new_with_model_and_entry:
4010 * @model: A #GtkTreeModel
4012 * Creates a new empty #GtkComboBox with an entry
4013 * and with the model initialized to @model.
4015 * Return value: A new #GtkComboBox
4018 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4020 return g_object_new (GTK_TYPE_COMBO_BOX,
4027 * gtk_combo_box_get_wrap_width:
4028 * @combo_box: A #GtkComboBox
4030 * Returns the wrap width which is used to determine the number of columns
4031 * for the popup menu. If the wrap width is larger than 1, the combo box
4034 * Returns: the wrap width.
4039 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4041 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4043 return combo_box->priv->wrap_width;
4047 * gtk_combo_box_set_wrap_width:
4048 * @combo_box: A #GtkComboBox
4049 * @width: Preferred number of columns
4051 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4052 * the preferred number of columns when you want the popup to be layed out
4058 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4061 GtkComboBoxPrivate *priv;
4063 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4064 g_return_if_fail (width >= 0);
4066 priv = combo_box->priv;
4068 if (width != priv->wrap_width)
4070 priv->wrap_width = width;
4072 gtk_combo_box_check_appearance (combo_box);
4074 if (GTK_IS_TREE_MENU (priv->popup_widget))
4075 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4077 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4082 * gtk_combo_box_get_row_span_column:
4083 * @combo_box: A #GtkComboBox
4085 * Returns the column with row span information for @combo_box.
4087 * Returns: the row span column.
4092 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4094 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4096 return combo_box->priv->row_column;
4100 * gtk_combo_box_set_row_span_column:
4101 * @combo_box: A #GtkComboBox.
4102 * @row_span: A column in the model passed during construction.
4104 * Sets the column with row span information for @combo_box to be @row_span.
4105 * The row span column contains integers which indicate how many rows
4106 * an item should span.
4111 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4114 GtkComboBoxPrivate *priv;
4117 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4119 priv = combo_box->priv;
4121 col = gtk_tree_model_get_n_columns (priv->model);
4122 g_return_if_fail (row_span >= -1 && row_span < col);
4124 if (row_span != priv->row_column)
4126 priv->row_column = row_span;
4128 if (GTK_IS_TREE_MENU (priv->popup_widget))
4129 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4131 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4136 * gtk_combo_box_get_column_span_column:
4137 * @combo_box: A #GtkComboBox
4139 * Returns the column with column span information for @combo_box.
4141 * Returns: the column span column.
4146 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4148 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4150 return combo_box->priv->col_column;
4154 * gtk_combo_box_set_column_span_column:
4155 * @combo_box: A #GtkComboBox
4156 * @column_span: A column in the model passed during construction
4158 * Sets the column with column span information for @combo_box to be
4159 * @column_span. The column span column contains integers which indicate
4160 * how many columns an item should span.
4165 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4168 GtkComboBoxPrivate *priv;
4171 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4173 priv = combo_box->priv;
4175 col = gtk_tree_model_get_n_columns (priv->model);
4176 g_return_if_fail (column_span >= -1 && column_span < col);
4178 if (column_span != priv->col_column)
4180 priv->col_column = column_span;
4182 if (GTK_IS_TREE_MENU (priv->popup_widget))
4183 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4185 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4190 * gtk_combo_box_get_active:
4191 * @combo_box: A #GtkComboBox
4193 * Returns the index of the currently active item, or -1 if there's no
4194 * active item. If the model is a non-flat treemodel, and the active item
4195 * is not an immediate child of the root of the tree, this function returns
4196 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4197 * <literal>path</literal> is the #GtkTreePath of the active item.
4199 * Return value: An integer which is the index of the currently active item,
4200 * or -1 if there's no active item.
4205 gtk_combo_box_get_active (GtkComboBox *combo_box)
4207 GtkComboBoxPrivate *priv;
4210 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4212 priv = combo_box->priv;
4214 if (gtk_tree_row_reference_valid (priv->active_row))
4218 path = gtk_tree_row_reference_get_path (priv->active_row);
4219 result = gtk_tree_path_get_indices (path)[0];
4220 gtk_tree_path_free (path);
4229 * gtk_combo_box_set_active:
4230 * @combo_box: A #GtkComboBox
4231 * @index_: An index in the model passed during construction, or -1 to have
4234 * Sets the active item of @combo_box to be the item at @index.
4239 gtk_combo_box_set_active (GtkComboBox *combo_box,
4242 GtkTreePath *path = NULL;
4243 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4244 g_return_if_fail (index_ >= -1);
4246 if (combo_box->priv->model == NULL)
4248 /* Save index, in case the model is set after the index */
4249 combo_box->priv->active = index_;
4255 path = gtk_tree_path_new_from_indices (index_, -1);
4257 gtk_combo_box_set_active_internal (combo_box, path);
4260 gtk_tree_path_free (path);
4264 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4267 GtkComboBoxPrivate *priv = combo_box->priv;
4268 GtkTreePath *active_path;
4271 /* Remember whether the initially active row is valid. */
4272 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4274 if (path && is_valid_row_reference)
4276 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4277 path_cmp = gtk_tree_path_compare (path, active_path);
4278 gtk_tree_path_free (active_path);
4283 if (priv->active_row)
4285 gtk_tree_row_reference_free (priv->active_row);
4286 priv->active_row = NULL;
4291 if (priv->tree_view)
4292 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4295 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4297 if (GTK_IS_MENU (menu))
4298 gtk_menu_set_active (menu, -1);
4301 if (priv->cell_view)
4302 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4305 * Do not emit a "changed" signal when an already invalid selection was
4306 * now set to invalid.
4308 if (!is_valid_row_reference)
4314 gtk_tree_row_reference_new (priv->model, path);
4316 if (priv->tree_view)
4318 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4321 else if (GTK_IS_MENU (priv->popup_widget))
4323 /* FIXME handle nested menus better */
4324 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4325 gtk_tree_path_get_indices (path)[0]);
4328 if (priv->cell_view)
4329 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4333 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4334 g_object_notify (G_OBJECT (combo_box), "active");
4335 if (combo_box->priv->id_column >= 0)
4336 g_object_notify (G_OBJECT (combo_box), "active-id");
4341 * gtk_combo_box_get_active_iter:
4342 * @combo_box: A #GtkComboBox
4343 * @iter: (out): The uninitialized #GtkTreeIter
4345 * Sets @iter to point to the current active item, if it exists.
4347 * Return value: %TRUE, if @iter was set
4352 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4358 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4360 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4363 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4364 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4365 gtk_tree_path_free (path);
4371 * gtk_combo_box_set_active_iter:
4372 * @combo_box: A #GtkComboBox
4373 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4375 * Sets the current active item to be the one referenced by @iter, or
4376 * unsets the active item if @iter is %NULL.
4381 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4384 GtkTreePath *path = NULL;
4386 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4389 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4391 gtk_combo_box_set_active_internal (combo_box, path);
4392 gtk_tree_path_free (path);
4396 * gtk_combo_box_set_model:
4397 * @combo_box: A #GtkComboBox
4398 * @model: (allow-none): A #GtkTreeModel
4400 * Sets the model used by @combo_box to be @model. Will unset a previously set
4401 * model (if applicable). If model is %NULL, then it will unset the model.
4403 * Note that this function does not clear the cell renderers, you have to
4404 * call gtk_cell_layout_clear() yourself if you need to set up different
4405 * cell renderers for the new model.
4410 gtk_combo_box_set_model (GtkComboBox *combo_box,
4411 GtkTreeModel *model)
4413 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4414 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4416 if (model == combo_box->priv->model)
4419 gtk_combo_box_unset_model (combo_box);
4424 combo_box->priv->model = model;
4425 g_object_ref (combo_box->priv->model);
4427 combo_box->priv->inserted_id =
4428 g_signal_connect (combo_box->priv->model, "row-inserted",
4429 G_CALLBACK (gtk_combo_box_model_row_inserted),
4431 combo_box->priv->deleted_id =
4432 g_signal_connect (combo_box->priv->model, "row-deleted",
4433 G_CALLBACK (gtk_combo_box_model_row_deleted),
4435 combo_box->priv->reordered_id =
4436 g_signal_connect (combo_box->priv->model, "rows-reordered",
4437 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4439 combo_box->priv->changed_id =
4440 g_signal_connect (combo_box->priv->model, "row-changed",
4441 G_CALLBACK (gtk_combo_box_model_row_changed),
4444 if (combo_box->priv->tree_view)
4447 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4448 combo_box->priv->model);
4449 gtk_combo_box_list_popup_resize (combo_box);
4452 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4455 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4456 combo_box->priv->model);
4459 if (combo_box->priv->cell_view)
4460 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4461 combo_box->priv->model);
4463 if (combo_box->priv->active != -1)
4465 /* If an index was set in advance, apply it now */
4466 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4467 combo_box->priv->active = -1;
4471 gtk_combo_box_update_sensitivity (combo_box);
4473 g_object_notify (G_OBJECT (combo_box), "model");
4477 * gtk_combo_box_get_model:
4478 * @combo_box: A #GtkComboBox
4480 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4482 * Return value: (transfer none): A #GtkTreeModel which was passed
4483 * during construction.
4488 gtk_combo_box_get_model (GtkComboBox *combo_box)
4490 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4492 return combo_box->priv->model;
4496 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4497 GtkScrollType scroll)
4500 GtkTreeIter new_iter;
4501 gboolean active_iter;
4504 if (!combo_box->priv->model)
4506 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4510 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4514 case GTK_SCROLL_STEP_BACKWARD:
4515 case GTK_SCROLL_STEP_UP:
4516 case GTK_SCROLL_STEP_LEFT:
4519 found = tree_prev (combo_box, combo_box->priv->model,
4520 &iter, &new_iter, FALSE);
4523 /* else fall through */
4525 case GTK_SCROLL_PAGE_FORWARD:
4526 case GTK_SCROLL_PAGE_DOWN:
4527 case GTK_SCROLL_PAGE_RIGHT:
4528 case GTK_SCROLL_END:
4529 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4532 case GTK_SCROLL_STEP_FORWARD:
4533 case GTK_SCROLL_STEP_DOWN:
4534 case GTK_SCROLL_STEP_RIGHT:
4537 found = tree_next (combo_box, combo_box->priv->model,
4538 &iter, &new_iter, FALSE);
4541 /* else fall through */
4543 case GTK_SCROLL_PAGE_BACKWARD:
4544 case GTK_SCROLL_PAGE_UP:
4545 case GTK_SCROLL_PAGE_LEFT:
4546 case GTK_SCROLL_START:
4547 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4554 if (found && active_iter)
4556 GtkTreePath *old_path;
4557 GtkTreePath *new_path;
4559 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4560 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4562 if (gtk_tree_path_compare (old_path, new_path) == 0)
4565 gtk_tree_path_free (old_path);
4566 gtk_tree_path_free (new_path);
4571 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4575 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4580 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4581 gboolean group_cycling)
4583 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4585 if (combo_box->priv->has_entry)
4589 child = gtk_bin_get_child (GTK_BIN (combo_box));
4591 gtk_widget_grab_focus (child);
4594 gtk_widget_grab_focus (combo_box->priv->button);
4600 gtk_combo_box_grab_focus (GtkWidget *widget)
4602 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4604 if (combo_box->priv->has_entry)
4608 child = gtk_bin_get_child (GTK_BIN (combo_box));
4610 gtk_widget_grab_focus (child);
4613 gtk_widget_grab_focus (combo_box->priv->button);
4617 gtk_combo_box_destroy (GtkWidget *widget)
4619 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4621 if (combo_box->priv->popup_idle_id > 0)
4623 g_source_remove (combo_box->priv->popup_idle_id);
4624 combo_box->priv->popup_idle_id = 0;
4627 gtk_combo_box_popdown (combo_box);
4629 if (combo_box->priv->row_separator_destroy)
4630 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4632 combo_box->priv->row_separator_func = NULL;
4633 combo_box->priv->row_separator_data = NULL;
4634 combo_box->priv->row_separator_destroy = NULL;
4636 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4637 combo_box->priv->cell_view = NULL;
4641 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4644 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4647 * Fixes regression reported in bug #574059. The old functionality relied on
4648 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4649 * when the selection was already set to -1.
4651 if (gtk_combo_box_get_active(combo_box) == -1)
4652 g_signal_emit_by_name (combo_box, "changed");
4654 gtk_combo_box_set_active (combo_box, -1);
4658 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4661 GtkTreeModel *model;
4664 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4666 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4674 model = gtk_combo_box_get_model (combo_box);
4675 path = gtk_tree_model_get_path (model, &iter);
4676 path_str = gtk_tree_path_to_string (path);
4678 g_signal_handlers_block_by_func (entry,
4679 gtk_combo_box_entry_contents_changed,
4683 g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0,
4686 gtk_entry_set_text (entry, text);
4688 g_signal_handlers_unblock_by_func (entry,
4689 gtk_combo_box_entry_contents_changed,
4692 gtk_tree_path_free (path);
4700 gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
4703 GtkComboBoxPrivate *priv = combo_box->priv;
4704 GtkTreeModel *model;
4708 if (priv->text_column >= 0)
4710 model = gtk_combo_box_get_model (combo_box);
4711 gtk_tree_model_get_iter_from_string (model, &iter, path);
4713 gtk_tree_model_get (model, &iter,
4714 priv->text_column, &text,
4723 gtk_combo_box_constructor (GType type,
4724 guint n_construct_properties,
4725 GObjectConstructParam *construct_properties)
4728 GtkComboBox *combo_box;
4729 GtkComboBoxPrivate *priv;
4731 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4732 (type, n_construct_properties, construct_properties);
4734 combo_box = GTK_COMBO_BOX (object);
4735 priv = combo_box->priv;
4739 priv->area = gtk_cell_area_box_new ();
4740 g_object_ref_sink (priv->area);
4743 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4744 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4745 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4746 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4747 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4748 gtk_widget_show (priv->cell_view);
4750 gtk_combo_box_check_appearance (combo_box);
4752 if (priv->has_entry)
4755 GtkStyleContext *context;
4757 entry = gtk_entry_new ();
4758 gtk_widget_show (entry);
4759 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4761 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4762 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4764 priv->text_renderer = gtk_cell_renderer_text_new ();
4765 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4766 priv->text_renderer, TRUE);
4768 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4770 g_signal_connect (combo_box, "changed",
4771 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4779 gtk_combo_box_dispose(GObject* object)
4781 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4783 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4785 gtk_combo_box_menu_destroy (combo_box);
4786 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4787 combo_box->priv->popup_widget = NULL;
4790 if (combo_box->priv->area)
4792 g_object_unref (combo_box->priv->area);
4793 combo_box->priv->area = NULL;
4796 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4797 gtk_combo_box_list_destroy (combo_box);
4799 if (combo_box->priv->popup_window)
4801 gtk_widget_destroy (combo_box->priv->popup_window);
4802 combo_box->priv->popup_window = NULL;
4805 gtk_combo_box_unset_model (combo_box);
4807 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4811 gtk_combo_box_finalize (GObject *object)
4813 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4815 g_free (combo_box->priv->tearoff_title);
4817 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4821 gtk_cell_editable_key_press (GtkWidget *widget,
4825 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4827 if (event->keyval == GDK_KEY_Escape)
4829 g_object_set (combo_box,
4830 "editing-canceled", TRUE,
4832 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4833 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4837 else if (event->keyval == GDK_KEY_Return ||
4838 event->keyval == GDK_KEY_ISO_Enter ||
4839 event->keyval == GDK_KEY_KP_Enter)
4841 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4842 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4851 popdown_idle (gpointer data)
4853 GtkComboBox *combo_box;
4855 combo_box = GTK_COMBO_BOX (data);
4857 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4858 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4860 g_object_unref (combo_box);
4866 popdown_handler (GtkWidget *widget,
4869 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4873 popup_idle (gpointer data)
4875 GtkComboBox *combo_box;
4877 combo_box = GTK_COMBO_BOX (data);
4879 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4880 combo_box->priv->cell_view)
4881 g_signal_connect_object (combo_box->priv->popup_widget,
4882 "unmap", G_CALLBACK (popdown_handler),
4885 /* we unset this if a menu item is activated */
4886 g_object_set (combo_box,
4887 "editing-canceled", TRUE,
4889 gtk_combo_box_popup (combo_box);
4891 combo_box->priv->popup_idle_id = 0;
4892 combo_box->priv->activate_button = 0;
4893 combo_box->priv->activate_time = 0;
4899 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4902 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4905 combo_box->priv->is_cell_renderer = TRUE;
4907 if (combo_box->priv->cell_view)
4909 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4910 G_CALLBACK (gtk_cell_editable_key_press),
4913 gtk_widget_grab_focus (combo_box->priv->button);
4917 child = gtk_bin_get_child (GTK_BIN (combo_box));
4919 g_signal_connect_object (child, "key-press-event",
4920 G_CALLBACK (gtk_cell_editable_key_press),
4923 gtk_widget_grab_focus (child);
4924 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4927 /* we do the immediate popup only for the optionmenu-like
4930 if (combo_box->priv->is_cell_renderer &&
4931 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4933 if (event && event->type == GDK_BUTTON_PRESS)
4935 GdkEventButton *event_button = (GdkEventButton *)event;
4937 combo_box->priv->activate_button = event_button->button;
4938 combo_box->priv->activate_time = event_button->time;
4941 combo_box->priv->popup_idle_id =
4942 gdk_threads_add_idle (popup_idle, combo_box);
4948 * gtk_combo_box_get_add_tearoffs:
4949 * @combo_box: a #GtkComboBox
4951 * Gets the current value of the :add-tearoffs property.
4953 * Return value: the current value of the :add-tearoffs property.
4956 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4958 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4960 return combo_box->priv->add_tearoffs;
4964 * gtk_combo_box_set_add_tearoffs:
4965 * @combo_box: a #GtkComboBox
4966 * @add_tearoffs: %TRUE to add tearoff menu items
4968 * Sets whether the popup menu should have a tearoff
4974 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4975 gboolean add_tearoffs)
4977 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4979 add_tearoffs = add_tearoffs != FALSE;
4981 if (combo_box->priv->add_tearoffs != add_tearoffs)
4983 combo_box->priv->add_tearoffs = add_tearoffs;
4984 gtk_combo_box_check_appearance (combo_box);
4986 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4987 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4988 combo_box->priv->add_tearoffs);
4990 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4995 * gtk_combo_box_get_title:
4996 * @combo_box: a #GtkComboBox
4998 * Gets the current title of the menu in tearoff mode. See
4999 * gtk_combo_box_set_add_tearoffs().
5001 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5002 * string which must not be freed.
5007 gtk_combo_box_get_title (GtkComboBox *combo_box)
5009 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5011 return combo_box->priv->tearoff_title;
5015 gtk_combo_box_update_title (GtkComboBox *combo_box)
5017 gtk_combo_box_check_appearance (combo_box);
5019 if (combo_box->priv->popup_widget &&
5020 GTK_IS_MENU (combo_box->priv->popup_widget))
5021 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5022 combo_box->priv->tearoff_title);
5026 * gtk_combo_box_set_title:
5027 * @combo_box: a #GtkComboBox
5028 * @title: a title for the menu in tearoff mode
5030 * Sets the menu's title in tearoff mode.
5035 gtk_combo_box_set_title (GtkComboBox *combo_box,
5038 GtkComboBoxPrivate *priv;
5040 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5042 priv = combo_box->priv;
5044 if (strcmp (title ? title : "",
5045 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5047 g_free (priv->tearoff_title);
5048 priv->tearoff_title = g_strdup (title);
5050 gtk_combo_box_update_title (combo_box);
5052 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5058 * gtk_combo_box_set_popup_fixed_width:
5059 * @combo_box: a #GtkComboBox
5060 * @fixed: whether to use a fixed popup width
5062 * Specifies whether the popup's width should be a fixed width
5063 * matching the allocated width of the combo box.
5068 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5071 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5073 if (combo_box->priv->popup_fixed_width != fixed)
5075 combo_box->priv->popup_fixed_width = fixed;
5077 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5082 * gtk_combo_box_get_popup_fixed_width:
5083 * @combo_box: a #GtkComboBox
5085 * Gets whether the popup uses a fixed width matching
5086 * the allocated width of the combo box.
5088 * Returns: %TRUE if the popup uses a fixed width
5093 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5095 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5097 return combo_box->priv->popup_fixed_width;
5102 * gtk_combo_box_get_popup_accessible:
5103 * @combo_box: a #GtkComboBox
5105 * Gets the accessible object corresponding to the combo box's popup.
5107 * This function is mostly intended for use by accessibility technologies;
5108 * applications should have little use for it.
5110 * Returns: (transfer none): the accessible object corresponding
5111 * to the combo box's popup.
5116 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5120 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5122 if (combo_box->priv->popup_widget)
5124 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5132 * gtk_combo_box_get_row_separator_func: (skip)
5133 * @combo_box: a #GtkComboBox
5135 * Returns the current row separator function.
5137 * Return value: the current row separator function.
5141 GtkTreeViewRowSeparatorFunc
5142 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5144 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5146 return combo_box->priv->row_separator_func;
5150 * gtk_combo_box_set_row_separator_func:
5151 * @combo_box: a #GtkComboBox
5152 * @func: a #GtkTreeViewRowSeparatorFunc
5153 * @data: (allow-none): user data to pass to @func, or %NULL
5154 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5156 * Sets the row separator function, which is used to determine
5157 * whether a row should be drawn as a separator. If the row separator
5158 * function is %NULL, no separators are drawn. This is the default value.
5163 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5164 GtkTreeViewRowSeparatorFunc func,
5166 GDestroyNotify destroy)
5168 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5170 if (combo_box->priv->row_separator_destroy)
5171 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5173 combo_box->priv->row_separator_func = func;
5174 combo_box->priv->row_separator_data = data;
5175 combo_box->priv->row_separator_destroy = destroy;
5177 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5178 if (combo_box->priv->tree_view)
5180 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5181 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5184 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5186 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5187 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5190 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5194 * gtk_combo_box_set_button_sensitivity:
5195 * @combo_box: a #GtkComboBox
5196 * @sensitivity: specify the sensitivity of the dropdown button
5198 * Sets whether the dropdown button of the combo box should be
5199 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5200 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5205 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5206 GtkSensitivityType sensitivity)
5208 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5210 if (combo_box->priv->button_sensitivity != sensitivity)
5212 combo_box->priv->button_sensitivity = sensitivity;
5213 gtk_combo_box_update_sensitivity (combo_box);
5215 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5220 * gtk_combo_box_get_button_sensitivity:
5221 * @combo_box: a #GtkComboBox
5223 * Returns whether the combo box sets the dropdown button
5224 * sensitive or not when there are no items in the model.
5226 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5227 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5228 * if the button is always insensitive or
5229 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5230 * the model has one item to be selected.
5235 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5237 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5239 return combo_box->priv->button_sensitivity;
5244 * gtk_combo_box_get_has_entry:
5245 * @combo_box: a #GtkComboBox
5247 * Returns whether the combo box has an entry.
5249 * Return Value: whether there is an entry in @combo_box.
5254 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5256 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5258 return combo_box->priv->has_entry;
5262 * gtk_combo_box_set_entry_text_column:
5263 * @combo_box: A #GtkComboBox
5264 * @text_column: A column in @model to get the strings from for
5265 * the internal entry
5267 * Sets the model column which @combo_box should use to get strings from
5268 * to be @text_column. The column @text_column in the model of @combo_box
5269 * must be of type %G_TYPE_STRING.
5271 * This is only relevant if @combo_box has been created with
5272 * #GtkComboBox:has-entry as %TRUE.
5277 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5280 GtkComboBoxPrivate *priv = combo_box->priv;
5281 GtkTreeModel *model;
5283 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5285 model = gtk_combo_box_get_model (combo_box);
5287 g_return_if_fail (text_column >= 0);
5288 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5290 priv->text_column = text_column;
5292 if (priv->text_renderer != NULL)
5293 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5294 priv->text_renderer,
5295 "text", text_column,
5300 * gtk_combo_box_get_entry_text_column:
5301 * @combo_box: A #GtkComboBox.
5303 * Returns the column which @combo_box is using to get the strings
5304 * from to display in the internal entry.
5306 * Return value: A column in the data source model of @combo_box.
5311 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5313 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5315 return combo_box->priv->text_column;
5319 * gtk_combo_box_set_focus_on_click:
5320 * @combo: a #GtkComboBox
5321 * @focus_on_click: whether the combo box grabs focus when clicked
5324 * Sets whether the combo box will grab focus when it is clicked with
5325 * the mouse. Making mouse clicks not grab focus is useful in places
5326 * like toolbars where you don't want the keyboard focus removed from
5327 * the main area of the application.
5332 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5333 gboolean focus_on_click)
5335 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5337 focus_on_click = focus_on_click != FALSE;
5339 if (combo_box->priv->focus_on_click != focus_on_click)
5341 combo_box->priv->focus_on_click = focus_on_click;
5343 if (combo_box->priv->button)
5344 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5347 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5352 * gtk_combo_box_get_focus_on_click:
5353 * @combo: a #GtkComboBox
5355 * Returns whether the combo box grabs focus when it is clicked
5356 * with the mouse. See gtk_combo_box_set_focus_on_click().
5358 * Return value: %TRUE if the combo box grabs focus when it is
5359 * clicked with the mouse.
5364 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5366 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5368 return combo_box->priv->focus_on_click;
5373 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5374 GtkBuilder *builder,
5376 const gchar *tagname,
5377 GMarkupParser *parser,
5380 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5381 tagname, parser, data))
5384 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5385 tagname, parser, data);
5389 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5390 GtkBuilder *builder,
5392 const gchar *tagname,
5395 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5396 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5400 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5401 GtkBuilder *builder,
5402 const gchar *childname)
5404 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5406 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5407 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5409 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5413 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5417 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5418 GtkComboBoxPrivate *priv = combo_box->priv;
5419 gint font_size, arrow_size;
5420 PangoContext *context;
5421 PangoFontMetrics *metrics;
5423 gint minimum_width = 0, natural_width = 0;
5424 gint child_min, child_nat;
5426 gfloat arrow_scaling;
5428 child = gtk_bin_get_child (GTK_BIN (widget));
5431 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5433 gtk_widget_style_get (GTK_WIDGET (widget),
5434 "arrow-size", &arrow_size,
5435 "arrow-scaling", &arrow_scaling,
5438 get_widget_padding_and_border (widget, &padding);
5440 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5441 metrics = pango_context_get_metrics (context,
5442 pango_context_get_font_description (context),
5443 pango_context_get_language (context));
5444 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5445 pango_font_metrics_get_descent (metrics));
5446 pango_font_metrics_unref (metrics);
5448 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5450 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5452 if (!priv->tree_view)
5455 if (priv->cell_view)
5457 gint box_width, xpad;
5458 GtkBorder button_padding;
5460 get_widget_padding_and_border (priv->button, &button_padding);
5462 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5463 xpad = button_padding.left + button_padding.right + padding.left + padding.right;
5465 minimum_width = child_min + box_width + xpad;
5466 natural_width = child_nat + box_width + xpad;
5470 gint but_width, but_nat_width;
5472 gtk_widget_get_preferred_width (priv->button,
5473 &but_width, &but_nat_width);
5475 minimum_width = child_min + but_width;
5476 natural_width = child_nat + but_nat_width;
5482 gint button_width, button_nat_width;
5484 /* sample + frame */
5485 minimum_width = child_min;
5486 natural_width = child_nat;
5488 if (priv->cell_view_frame)
5490 if (priv->has_frame)
5492 gint border_width, xpad;
5493 GtkBorder frame_padding;
5495 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5496 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5497 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5499 minimum_width += xpad;
5500 natural_width += xpad;
5505 gtk_widget_get_preferred_width (priv->button,
5506 &button_width, &button_nat_width);
5508 minimum_width += button_width;
5509 natural_width += button_nat_width;
5512 minimum_width += padding.left + padding.right;
5513 natural_width += padding.left + padding.right;
5516 *minimum_size = minimum_width;
5519 *natural_size = natural_width;
5523 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5529 /* Combo box is height-for-width only
5530 * (so we always just reserve enough height for the minimum width) */
5531 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5532 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5536 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5541 /* Combo box is height-for-width only
5542 * (so we assume we always reserved enough height for the minimum width) */
5543 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5548 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5553 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5554 GtkComboBoxPrivate *priv = combo_box->priv;
5555 gint min_height = 0, nat_height = 0;
5560 child = gtk_bin_get_child (GTK_BIN (widget));
5562 get_widget_padding_and_border (widget, &padding);
5565 if (!priv->tree_view)
5568 if (priv->cell_view)
5570 /* calculate x/y padding and separator/arrow size */
5571 gint box_width, box_height;
5573 GtkBorder button_padding;
5575 get_widget_padding_and_border (priv->button, &button_padding);
5577 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5578 gtk_widget_get_preferred_height_for_width (priv->box,
5579 box_width, &box_height, NULL);
5581 xpad = button_padding.left + button_padding.right;
5582 ypad = button_padding.top + button_padding.bottom;
5584 size -= box_width + xpad;
5586 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5587 * and fitting the whole treemodel */
5588 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5590 min_height = MAX (min_height, box_height);
5591 nat_height = MAX (nat_height, box_height);
5598 /* there is a custom child widget inside (no priv->cell_view) */
5599 gint but_width, but_height;
5601 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5602 gtk_widget_get_preferred_height_for_width (priv->button,
5603 but_width, &but_height, NULL);
5607 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5608 * and fitting the whole treemodel */
5609 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5611 min_height = MAX (min_height, but_height);
5612 nat_height = MAX (nat_height, but_height);
5618 gint but_width, but_height;
5619 gint xpad = 0, ypad = 0;
5621 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5622 gtk_widget_get_preferred_height_for_width (priv->button,
5623 but_width, &but_height, NULL);
5625 if (priv->cell_view_frame && priv->has_frame)
5627 GtkBorder frame_padding;
5630 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5631 get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5633 xpad = (2 * border_width) + padding.left + frame_padding.right;
5634 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5640 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5641 * and fitting the whole treemodel */
5642 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5644 min_height = MAX (min_height, but_height);
5645 nat_height = MAX (nat_height, but_height);
5651 min_height += padding.top + padding.bottom;
5652 nat_height += padding.top + padding.bottom;
5655 *minimum_size = min_height;
5658 *natural_size = nat_height;
5662 * gtk_combo_box_set_id_column:
5663 * @combo_box: A #GtkComboBox
5664 * @id_column: A column in @model to get string IDs for values from
5666 * Sets the model column which @combo_box should use to get string IDs
5667 * for values from. The column @id_column in the model of @combo_box
5668 * must be of type %G_TYPE_STRING.
5673 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5676 GtkComboBoxPrivate *priv = combo_box->priv;
5677 GtkTreeModel *model;
5679 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5681 if (id_column != priv->id_column)
5683 model = gtk_combo_box_get_model (combo_box);
5685 g_return_if_fail (id_column >= 0);
5686 g_return_if_fail (model == NULL ||
5687 id_column < gtk_tree_model_get_n_columns (model));
5689 priv->id_column = id_column;
5691 g_object_notify (G_OBJECT (combo_box), "id-column");
5692 g_object_notify (G_OBJECT (combo_box), "active-id");
5697 * gtk_combo_box_get_id_column:
5698 * @combo_box: A #GtkComboBox
5700 * Returns the column which @combo_box is using to get string IDs
5703 * Return value: A column in the data source model of @combo_box.
5708 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5710 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5712 return combo_box->priv->id_column;
5716 * gtk_combo_box_get_active_id:
5717 * @combo_box: a #GtkComboBox
5719 * Returns the ID of the active row of @combo_box. This value is taken
5720 * from the active row and the column specified by the #GtkComboBox:id-column
5721 * property of @combo_box (see gtk_combo_box_set_id_column()).
5723 * The returned value is an interned string which means that you can
5724 * compare the pointer by value to other interned strings and that you
5727 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5728 * no row is active, or if the active row has a %NULL ID value, then %NULL
5731 * Return value: the ID of the active row, or %NULL
5736 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5738 GtkTreeModel *model;
5742 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5744 column = combo_box->priv->id_column;
5749 model = gtk_combo_box_get_model (combo_box);
5750 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5751 G_TYPE_STRING, NULL);
5753 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5755 const gchar *interned;
5758 gtk_tree_model_get (model, &iter, column, &id, -1);
5759 interned = g_intern_string (id);
5769 * gtk_combo_box_set_active_id:
5770 * @combo_box: a #GtkComboBox
5771 * @active_id: (allow-none): the ID of the row to select, or %NULL
5773 * Changes the active row of @combo_box to the one that has an ID equal to
5774 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5775 * a %NULL ID string cannot be made active by this function.
5777 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5778 * row has the given ID then the function does nothing and returns %FALSE.
5780 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5781 * @active_id was given to unset the active row, the function
5782 * always returns %TRUE.
5787 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5788 const gchar *active_id)
5790 GtkTreeModel *model;
5792 gboolean match = FALSE;
5795 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5797 if (active_id == NULL)
5799 gtk_combo_box_set_active (combo_box, -1);
5800 return TRUE; /* active row was successfully unset */
5803 column = combo_box->priv->id_column;
5808 model = gtk_combo_box_get_model (combo_box);
5809 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5810 G_TYPE_STRING, FALSE);
5812 if (gtk_tree_model_get_iter_first (model, &iter))
5816 gtk_tree_model_get (model, &iter, column, &id, -1);
5818 match = strcmp (id, active_id) == 0;
5823 gtk_combo_box_set_active_iter (combo_box, &iter);
5826 } while (gtk_tree_model_iter_next (model, &iter));