2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcombobox.h"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
32 #include "gtkliststore.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "deprecated/gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkseparator.h"
42 #include "gtkwindow.h"
43 #include "gtktypebuiltins.h"
44 #include "gtkprivate.h"
46 #include <gobject/gvaluecollector.h>
51 #include "gtkmarshalers.h"
54 #include "gtkentryprivate.h"
55 #include "gtktreeprivate.h"
56 #include "a11y/gtkcomboboxaccessible.h"
61 * @Short_description: A widget used to choose from a list of items
63 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
65 * A GtkComboBox is a widget that allows the user to choose from a list of
66 * valid choices. The GtkComboBox displays the selected choice. When
67 * activated, the GtkComboBox displays a popup which allows the user to
68 * make a new choice. The style in which the selected value is displayed,
69 * and the style of the popup is determined by the current theme. It may
70 * be similar to a Windows-style combo box.
72 * The GtkComboBox uses the model-view pattern; the list of valid choices
73 * is specified in the form of a tree model, and the display of the choices
74 * can be adapted to the data in the model by using cell renderers, as you
75 * would in a tree view. This is possible since GtkComboBox implements the
76 * #GtkCellLayout interface. The tree model holding the valid choices is
77 * not restricted to a flat list, it can be a real tree, and the popup will
78 * reflect the tree structure.
80 * To allow the user to enter values not in the model, the 'has-entry'
81 * property allows the GtkComboBox to contain a #GtkEntry. This entry
82 * can be accessed by calling gtk_bin_get_child() on the combo box.
84 * For a simple list of textual choices, the model-view API of GtkComboBox
85 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
91 /* WELCOME, to THE house of evil code */
92 struct _GtkComboBoxPrivate
102 GtkShadowType shadow_type;
104 gint active; /* Only temporary */
105 GtkTreeRowReference *active_row;
107 GtkWidget *tree_view;
109 GtkWidget *cell_view;
110 GtkWidget *cell_view_frame;
115 GtkWidget *separator;
117 GtkWidget *popup_widget;
118 GtkWidget *popup_window;
119 GtkWidget *scrolled_window;
126 guint activate_button;
127 guint32 activate_time;
129 guint resize_idle_id;
131 /* For "has-entry" specific behavior we track
132 * an automated cell renderer and text column
135 GtkCellRenderer *text_renderer;
139 guint popup_in_progress : 1;
140 guint popup_shown : 1;
141 guint add_tearoffs : 1;
143 guint is_cell_renderer : 1;
144 guint editing_canceled : 1;
145 guint auto_scroll : 1;
146 guint focus_on_click : 1;
147 guint button_sensitivity : 2;
149 guint popup_fixed_width : 1;
151 GtkTreeViewRowSeparatorFunc row_separator_func;
152 gpointer row_separator_data;
153 GDestroyNotify row_separator_destroy;
155 GdkDevice *grab_pointer;
156 GdkDevice *grab_keyboard;
158 gchar *tearoff_title;
161 /* While debugging this evil code, I have learned that
162 * there are actually 4 modes to this widget, which can
163 * be characterized as follows
165 * 1) menu mode, no child added
168 * cell_view -> GtkCellView, regular child
169 * cell_view_frame -> NULL
170 * button -> GtkToggleButton set_parent to combo
171 * arrow -> GtkArrow set_parent to button
172 * separator -> GtkVSepator set_parent to button
173 * popup_widget -> GtkMenu
174 * popup_window -> NULL
175 * scrolled_window -> NULL
177 * 2) menu mode, child added
181 * cell_view_frame -> NULL
182 * button -> GtkToggleButton set_parent to combo
183 * arrow -> GtkArrow, child of button
185 * popup_widget -> GtkMenu
186 * popup_window -> NULL
187 * scrolled_window -> NULL
189 * 3) list mode, no child added
191 * tree_view -> GtkTreeView, child of scrolled_window
192 * cell_view -> GtkCellView, regular child
193 * cell_view_frame -> GtkFrame, set parent to combo
194 * button -> GtkToggleButton, set_parent to combo
195 * arrow -> GtkArrow, child of button
197 * popup_widget -> tree_view
198 * popup_window -> GtkWindow
199 * scrolled_window -> GtkScrolledWindow, child of popup_window
201 * 4) list mode, child added
203 * tree_view -> GtkTreeView, child of scrolled_window
205 * cell_view_frame -> NULL
206 * button -> GtkToggleButton, set_parent to combo
207 * arrow -> GtkArrow, child of button
209 * popup_widget -> tree_view
210 * popup_window -> GtkWindow
211 * scrolled_window -> GtkScrolledWindow, child of popup_window
228 PROP_ROW_SPAN_COLUMN,
229 PROP_COLUMN_SPAN_COLUMN,
236 PROP_BUTTON_SENSITIVITY,
237 PROP_EDITING_CANCELED,
239 PROP_ENTRY_TEXT_COLUMN,
240 PROP_POPUP_FIXED_WIDTH,
246 static guint combo_box_signals[LAST_SIGNAL] = {0,};
248 #define SCROLL_TIME 100
252 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
253 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
254 static GObject *gtk_combo_box_constructor (GType type,
255 guint n_construct_properties,
256 GObjectConstructParam *construct_properties);
257 static void gtk_combo_box_dispose (GObject *object);
258 static void gtk_combo_box_finalize (GObject *object);
259 static void gtk_combo_box_destroy (GtkWidget *widget);
261 static void gtk_combo_box_set_property (GObject *object,
265 static void gtk_combo_box_get_property (GObject *object,
270 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
271 GtkStateFlags previous);
272 static void gtk_combo_box_grab_focus (GtkWidget *widget);
273 static void gtk_combo_box_style_updated (GtkWidget *widget);
274 static void gtk_combo_box_button_toggled (GtkWidget *widget,
276 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
277 GtkStateFlags previous,
279 static void gtk_combo_box_add (GtkContainer *container,
281 static void gtk_combo_box_remove (GtkContainer *container,
284 static void gtk_combo_box_menu_show (GtkWidget *menu,
286 static void gtk_combo_box_menu_hide (GtkWidget *menu,
289 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
291 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
296 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
301 static void gtk_combo_box_menu_position (GtkMenu *menu,
307 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
309 static void gtk_combo_box_size_allocate (GtkWidget *widget,
310 GtkAllocation *allocation);
311 static void gtk_combo_box_forall (GtkContainer *container,
312 gboolean include_internals,
313 GtkCallback callback,
314 gpointer callback_data);
315 static gboolean gtk_combo_box_draw (GtkWidget *widget,
317 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
318 GdkEventScroll *event);
319 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
322 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
323 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
324 GtkScrollType scroll);
325 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
326 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
328 /* listening to the model */
329 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
333 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
336 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
341 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
345 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
351 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
356 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
357 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
359 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
360 GdkEventButton *event,
362 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
365 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
366 GdkEventCrossing *event,
368 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
371 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
372 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
373 GdkEventButton *event,
376 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
379 gboolean path_currently_selected,
382 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
386 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
389 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
390 gboolean add_children);
391 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
392 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
395 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
396 GdkEventButton *event,
398 static void gtk_combo_box_menu_activate (GtkWidget *menu,
400 GtkComboBox *combo_box);
401 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
402 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
405 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
407 guint32 activate_time);
410 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
412 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
413 gboolean group_cycling);
415 static void gtk_combo_box_child_show (GtkWidget *widget,
416 GtkComboBox *combo_box);
417 static void gtk_combo_box_child_hide (GtkWidget *widget,
418 GtkComboBox *combo_box);
420 /* GtkComboBox:has-entry callbacks */
421 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
423 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
425 static gchar *gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
428 /* GtkBuildable method implementation */
429 static GtkBuildableIface *parent_buildable_iface;
431 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
432 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
435 const gchar *tagname,
436 GMarkupParser *parser,
438 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
441 const gchar *tagname,
443 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
445 const gchar *childname);
448 /* GtkCellEditable method implementations */
449 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
452 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
455 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
458 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
462 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
466 static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container,
468 static void gtk_combo_box_direction_changed (GtkWidget *widget,
469 GtkTextDirection previous_direction);
471 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
472 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
473 gtk_combo_box_cell_layout_init)
474 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
475 gtk_combo_box_cell_editable_init)
476 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
477 gtk_combo_box_buildable_init))
482 gtk_combo_box_class_init (GtkComboBoxClass *klass)
484 GObjectClass *object_class;
485 GtkContainerClass *container_class;
486 GtkWidgetClass *widget_class;
487 GtkBindingSet *binding_set;
489 container_class = (GtkContainerClass *)klass;
490 container_class->forall = gtk_combo_box_forall;
491 container_class->add = gtk_combo_box_add;
492 container_class->remove = gtk_combo_box_remove;
493 container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
495 gtk_container_class_handle_border_width (container_class);
497 widget_class = (GtkWidgetClass *)klass;
498 widget_class->size_allocate = gtk_combo_box_size_allocate;
499 widget_class->draw = gtk_combo_box_draw;
500 widget_class->scroll_event = gtk_combo_box_scroll_event;
501 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
502 widget_class->grab_focus = gtk_combo_box_grab_focus;
503 widget_class->style_updated = gtk_combo_box_style_updated;
504 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
505 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
506 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
507 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
508 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
509 widget_class->destroy = gtk_combo_box_destroy;
510 widget_class->direction_changed = gtk_combo_box_direction_changed;
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);
1383 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1385 gtk_container_forall (GTK_CONTAINER (combo_box),
1386 (GtkCallback) gtk_widget_reset_style,
1391 gtk_combo_box_direction_changed (GtkWidget *widget,
1392 GtkTextDirection previous_direction)
1394 gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1397 static GtkWidgetPath *
1398 gtk_combo_box_get_path_for_child (GtkContainer *container,
1401 GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1402 GtkWidgetPath *path;
1404 gboolean found = FALSE;
1405 GList *visible_children, *l;
1406 GtkWidgetPath *sibling_path;
1409 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
1411 if (gtk_widget_get_visible (child))
1413 visible_children = NULL;
1415 if (priv->button && gtk_widget_get_visible (priv->button))
1416 visible_children = g_list_prepend (visible_children, priv->button);
1418 if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1419 visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1421 widget = gtk_bin_get_child (GTK_BIN (container));
1422 if (widget && gtk_widget_get_visible (widget))
1423 visible_children = g_list_prepend (visible_children, widget);
1425 if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1426 visible_children = g_list_reverse (visible_children);
1430 for (l = visible_children; l; l = l->next)
1434 if (widget == child)
1446 sibling_path = gtk_widget_path_new ();
1448 for (l = visible_children; l; l = l->next)
1449 gtk_widget_path_append_for_widget (sibling_path, l->data);
1451 gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1453 g_list_free (visible_children);
1454 gtk_widget_path_unref (sibling_path);
1458 gtk_widget_path_append_for_widget (path, child);
1465 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1467 GtkComboBoxPrivate *priv = combo_box->priv;
1468 gboolean appears_as_list;
1470 /* if wrap_width > 0, then we are in grid-mode and forced to use
1473 if (priv->wrap_width)
1474 appears_as_list = FALSE;
1476 gtk_widget_style_get (GTK_WIDGET (combo_box),
1477 "appears-as-list", &appears_as_list,
1480 if (appears_as_list)
1482 /* Destroy all the menu mode widgets, if they exist. */
1483 if (GTK_IS_MENU (priv->popup_widget))
1484 gtk_combo_box_menu_destroy (combo_box);
1486 /* Create the list mode widgets, if they don't already exist. */
1487 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1488 gtk_combo_box_list_setup (combo_box);
1492 /* Destroy all the list mode widgets, if they exist. */
1493 if (GTK_IS_TREE_VIEW (priv->tree_view))
1494 gtk_combo_box_list_destroy (combo_box);
1496 /* Create the menu mode widgets, if they don't already exist. */
1497 if (!GTK_IS_MENU (priv->popup_widget))
1498 gtk_combo_box_menu_setup (combo_box, TRUE);
1501 gtk_widget_style_get (GTK_WIDGET (combo_box),
1502 "shadow-type", &priv->shadow_type,
1507 gtk_combo_box_style_updated (GtkWidget *widget)
1509 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1510 GtkComboBoxPrivate *priv = combo_box->priv;
1513 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1515 gtk_combo_box_check_appearance (combo_box);
1517 if (priv->tree_view && priv->cell_view)
1519 GtkStyleContext *context;
1520 GtkStateFlags state;
1523 context = gtk_widget_get_style_context (widget);
1524 state = gtk_widget_get_state_flags (widget);
1525 gtk_style_context_get_background_color (context, state, &color);
1527 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1530 child = gtk_bin_get_child (GTK_BIN (combo_box));
1531 if (GTK_IS_ENTRY (child))
1532 g_object_set (child, "shadow-type",
1533 GTK_SHADOW_NONE == priv->shadow_type ?
1534 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1538 gtk_combo_box_button_toggled (GtkWidget *widget,
1541 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1543 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1545 if (!combo_box->priv->popup_in_progress)
1546 gtk_combo_box_popup (combo_box);
1549 gtk_combo_box_popdown (combo_box);
1553 gtk_combo_box_add (GtkContainer *container,
1556 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1557 GtkComboBoxPrivate *priv = combo_box->priv;
1559 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1561 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1562 "(need an instance of GtkEntry or of a subclass)",
1563 G_OBJECT_TYPE_NAME (widget));
1567 if (priv->cell_view &&
1568 gtk_widget_get_parent (priv->cell_view))
1570 gtk_widget_unparent (priv->cell_view);
1571 _gtk_bin_set_child (GTK_BIN (container), NULL);
1572 gtk_widget_queue_resize (GTK_WIDGET (container));
1575 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1576 _gtk_bin_set_child (GTK_BIN (container), widget);
1578 if (priv->cell_view &&
1579 widget != priv->cell_view)
1581 /* since the cell_view was unparented, it's gone now */
1582 priv->cell_view = NULL;
1584 if (!priv->tree_view && priv->separator)
1586 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1588 priv->separator = NULL;
1590 gtk_widget_queue_resize (GTK_WIDGET (container));
1592 else if (priv->cell_view_frame)
1594 gtk_widget_unparent (priv->cell_view_frame);
1595 priv->cell_view_frame = NULL;
1600 if (priv->has_entry)
1602 /* this flag is a hack to tell the entry to fill its allocation.
1604 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1606 g_signal_connect (widget, "changed",
1607 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1610 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1615 gtk_combo_box_remove (GtkContainer *container,
1618 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1619 GtkComboBoxPrivate *priv = combo_box->priv;
1621 gboolean appears_as_list;
1623 if (priv->has_entry)
1625 GtkWidget *child_widget;
1627 child_widget = gtk_bin_get_child (GTK_BIN (container));
1628 if (widget && widget == child_widget)
1630 g_signal_handlers_disconnect_by_func (widget,
1631 gtk_combo_box_entry_contents_changed,
1633 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1637 if (widget == priv->cell_view)
1638 priv->cell_view = NULL;
1640 gtk_widget_unparent (widget);
1641 _gtk_bin_set_child (GTK_BIN (container), NULL);
1643 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1646 gtk_widget_queue_resize (GTK_WIDGET (container));
1648 if (!priv->tree_view)
1649 appears_as_list = FALSE;
1651 appears_as_list = TRUE;
1653 if (appears_as_list)
1654 gtk_combo_box_list_destroy (combo_box);
1655 else if (GTK_IS_MENU (priv->popup_widget))
1657 gtk_combo_box_menu_destroy (combo_box);
1658 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1659 priv->popup_widget = NULL;
1662 if (!priv->cell_view)
1664 priv->cell_view = gtk_cell_view_new ();
1665 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1666 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1668 gtk_widget_show (priv->cell_view);
1669 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1674 if (appears_as_list)
1675 gtk_combo_box_list_setup (combo_box);
1677 gtk_combo_box_menu_setup (combo_box, TRUE);
1679 if (gtk_tree_row_reference_valid (priv->active_row))
1681 path = gtk_tree_row_reference_get_path (priv->active_row);
1682 gtk_combo_box_set_active_internal (combo_box, path);
1683 gtk_tree_path_free (path);
1686 gtk_combo_box_set_active_internal (combo_box, NULL);
1690 gtk_combo_box_menu_show (GtkWidget *menu,
1693 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1694 GtkComboBoxPrivate *priv = combo_box->priv;
1696 gtk_combo_box_child_show (menu, user_data);
1698 priv->popup_in_progress = TRUE;
1699 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1701 priv->popup_in_progress = FALSE;
1705 gtk_combo_box_menu_hide (GtkWidget *menu,
1708 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1710 gtk_combo_box_child_hide (menu,user_data);
1712 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1717 gtk_combo_box_detacher (GtkWidget *widget,
1720 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1721 GtkComboBoxPrivate *priv = combo_box->priv;
1723 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1725 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1726 gtk_combo_box_menu_show,
1728 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1729 gtk_combo_box_menu_hide,
1732 priv->popup_widget = NULL;
1736 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1739 GtkComboBoxPrivate *priv = combo_box->priv;
1741 if (GTK_IS_MENU (priv->popup_widget))
1743 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1744 priv->popup_widget = NULL;
1746 else if (priv->popup_widget)
1748 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1749 priv->popup_widget);
1750 g_object_unref (priv->popup_widget);
1751 priv->popup_widget = NULL;
1754 if (GTK_IS_MENU (popup))
1756 if (priv->popup_window)
1758 gtk_widget_destroy (priv->popup_window);
1759 priv->popup_window = NULL;
1762 priv->popup_widget = popup;
1765 * Note that we connect to show/hide on the toplevel, not the
1766 * menu itself, since the menu is not shown/hidden when it is
1767 * popped up while torn-off.
1769 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1770 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1771 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1772 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1774 gtk_menu_attach_to_widget (GTK_MENU (popup),
1775 GTK_WIDGET (combo_box),
1776 gtk_combo_box_detacher);
1780 if (!priv->popup_window)
1782 GtkWidget *toplevel;
1784 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1785 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1787 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1788 GDK_WINDOW_TYPE_HINT_COMBO);
1790 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1791 G_CALLBACK (gtk_combo_box_child_show),
1793 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1794 G_CALLBACK (gtk_combo_box_child_hide),
1797 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1798 if (GTK_IS_WINDOW (toplevel))
1800 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1801 GTK_WINDOW (priv->popup_window));
1802 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1803 GTK_WINDOW (toplevel));
1806 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1807 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1808 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1810 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1812 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1815 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1818 gtk_widget_show (priv->scrolled_window);
1820 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1821 priv->scrolled_window);
1824 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1827 gtk_widget_show (popup);
1828 g_object_ref (popup);
1829 priv->popup_widget = popup;
1834 get_widget_padding_and_border (GtkWidget *widget,
1837 GtkStyleContext *context;
1838 GtkStateFlags state;
1841 context = gtk_widget_get_style_context (widget);
1842 state = gtk_style_context_get_state (context);
1844 gtk_style_context_get_padding (context, state, padding);
1845 gtk_style_context_get_border (context, state, &tmp);
1847 padding->top += tmp.top;
1848 padding->right += tmp.right;
1849 padding->bottom += tmp.bottom;
1850 padding->left += tmp.left;
1854 gtk_combo_box_menu_position_below (GtkMenu *menu,
1860 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1861 GtkAllocation child_allocation;
1867 GdkRectangle monitor;
1870 /* FIXME: is using the size request here broken? */
1871 child = gtk_bin_get_child (GTK_BIN (combo_box));
1875 gtk_widget_get_allocation (child, &child_allocation);
1877 if (!gtk_widget_get_has_window (child))
1879 sx += child_allocation.x;
1880 sy += child_allocation.y;
1883 gdk_window_get_root_coords (gtk_widget_get_window (child),
1885 get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding);
1887 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1892 if (combo_box->priv->popup_fixed_width)
1893 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1895 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1897 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1900 *x = sx + child_allocation.width - req.width;
1903 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1904 monitor_num = gdk_screen_get_monitor_at_window (screen,
1905 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1906 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1910 else if (*x + req.width > monitor.x + monitor.width)
1911 *x = monitor.x + monitor.width - req.width;
1913 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1914 *y += child_allocation.height;
1915 else if (*y - monitor.y >= req.height)
1917 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1918 *y += child_allocation.height;
1926 gtk_combo_box_menu_position_over (GtkMenu *menu,
1932 GtkComboBox *combo_box;
1936 GtkAllocation allocation;
1937 GtkAllocation child_allocation;
1944 combo_box = GTK_COMBO_BOX (user_data);
1945 widget = GTK_WIDGET (combo_box);
1947 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1949 gtk_widget_get_allocation (widget, &allocation);
1951 menu_xpos = allocation.x;
1952 menu_ypos = allocation.y + allocation.height / 2 - 2;
1954 if (combo_box->priv->popup_fixed_width)
1955 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1957 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1961 gtk_widget_get_allocation (active, &child_allocation);
1962 menu_ypos -= child_allocation.height / 2;
1965 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1968 child = children->data;
1970 if (active == child)
1973 if (gtk_widget_get_visible (child))
1975 gtk_widget_get_allocation (child, &child_allocation);
1977 menu_ypos -= child_allocation.height;
1980 children = children->next;
1983 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1984 menu_xpos = menu_xpos + allocation.width - menu_width;
1986 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1987 menu_xpos, menu_ypos,
1988 &menu_xpos, &menu_ypos);
1990 /* Clamp the position on screen */
1991 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1995 else if ((menu_xpos + menu_width) > screen_width)
1996 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
2005 gtk_combo_box_menu_position (GtkMenu *menu,
2011 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2012 GtkComboBoxPrivate *priv = combo_box->priv;
2013 GtkWidget *menu_item;
2015 if (priv->wrap_width > 0 || priv->cell_view == NULL)
2016 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2019 /* FIXME handle nested menus better */
2020 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2022 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2025 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2028 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2029 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2030 GDK_WINDOW_TYPE_HINT_COMBO);
2034 gtk_combo_box_list_position (GtkComboBox *combo_box,
2040 GtkComboBoxPrivate *priv = combo_box->priv;
2041 GtkAllocation allocation;
2044 GdkRectangle monitor;
2045 GtkRequisition popup_req;
2046 GtkPolicyType hpolicy, vpolicy;
2049 /* under windows, the drop down list is as wide as the combo box itself.
2051 GtkWidget *widget = GTK_WIDGET (combo_box);
2055 gtk_widget_get_allocation (widget, &allocation);
2057 if (!gtk_widget_get_has_window (widget))
2063 window = gtk_widget_get_window (widget);
2065 gdk_window_get_root_coords (gtk_widget_get_window (widget),
2068 *width = allocation.width;
2070 hpolicy = vpolicy = GTK_POLICY_NEVER;
2071 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2074 if (combo_box->priv->popup_fixed_width)
2076 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2078 if (popup_req.width > *width)
2080 hpolicy = GTK_POLICY_ALWAYS;
2081 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2087 /* XXX This code depends on treeviews properly reporting their natural width
2088 * list-mode menus won't fill up to their natural width until then */
2089 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2091 if (popup_req.width > *width)
2093 hpolicy = GTK_POLICY_NEVER;
2094 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2097 *width = popup_req.width;
2101 *height = popup_req.height;
2103 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2104 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2105 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2107 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2108 *x = *x + allocation.width - *width;
2112 else if (*x + *width > monitor.x + monitor.width)
2113 *x = monitor.x + monitor.width - *width;
2115 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2116 *y += allocation.height;
2117 else if (*y - *height >= monitor.y)
2119 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2121 *y += allocation.height;
2122 *height = monitor.y + monitor.height - *y;
2126 *height = *y - monitor.y;
2130 if (popup_req.height > *height)
2132 vpolicy = GTK_POLICY_ALWAYS;
2134 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2140 cell_layout_is_sensitive (GtkCellLayout *layout)
2142 GList *cells, *list;
2145 cells = gtk_cell_layout_get_cells (layout);
2148 for (list = cells; list; list = list->next)
2150 g_object_get (list->data, "sensitive", &sensitive, NULL);
2155 g_list_free (cells);
2161 cell_is_sensitive (GtkCellRenderer *cell,
2164 gboolean *sensitive = data;
2166 g_object_get (cell, "sensitive", sensitive, NULL);
2172 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2175 GtkComboBoxPrivate *priv = combo_box->priv;
2177 if (priv->row_separator_func)
2179 if (priv->row_separator_func (priv->model, iter,
2180 priv->row_separator_data))
2188 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2192 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2201 update_menu_sensitivity (GtkComboBox *combo_box,
2204 GtkComboBoxPrivate *priv = combo_box->priv;
2205 GList *children, *child;
2206 GtkWidget *item, *submenu, *separator;
2207 GtkWidget *cell_view;
2213 children = gtk_container_get_children (GTK_CONTAINER (menu));
2215 for (child = children; child; child = child->next)
2217 item = GTK_WIDGET (child->data);
2218 cell_view = gtk_bin_get_child (GTK_BIN (item));
2220 if (!GTK_IS_CELL_VIEW (cell_view))
2223 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2224 if (submenu != NULL)
2226 gtk_widget_set_sensitive (item, TRUE);
2227 update_menu_sensitivity (combo_box, submenu);
2231 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2233 if (menu != priv->popup_widget && child == children)
2235 separator = GTK_WIDGET (child->next->data);
2236 g_object_set (item, "visible", sensitive, NULL);
2237 g_object_set (separator, "visible", sensitive, NULL);
2240 gtk_widget_set_sensitive (item, sensitive);
2244 g_list_free (children);
2248 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2250 guint32 activate_time)
2252 GtkComboBoxPrivate *priv = combo_box->priv;
2255 gint width, min_width, nat_width;
2257 update_menu_sensitivity (combo_box, priv->popup_widget);
2260 if (gtk_tree_row_reference_valid (priv->active_row))
2262 path = gtk_tree_row_reference_get_path (priv->active_row);
2263 active_item = gtk_tree_path_get_indices (path)[0];
2264 gtk_tree_path_free (path);
2266 if (priv->add_tearoffs)
2270 /* FIXME handle nested menus better */
2271 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2273 if (priv->wrap_width == 0)
2275 GtkAllocation allocation;
2277 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2278 width = allocation.width;
2279 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2280 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2282 if (combo_box->priv->popup_fixed_width)
2283 width = MAX (width, min_width);
2285 width = MAX (width, nat_width);
2287 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2290 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2292 gtk_combo_box_menu_position, combo_box,
2293 button, activate_time);
2297 popup_grab_on_window (GdkWindow *window,
2298 GdkDevice *keyboard,
2300 guint32 activate_time)
2303 gdk_device_grab (keyboard, window,
2304 GDK_OWNERSHIP_WINDOW, TRUE,
2305 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2306 NULL, activate_time) != GDK_GRAB_SUCCESS)
2310 gdk_device_grab (pointer, window,
2311 GDK_OWNERSHIP_WINDOW, TRUE,
2312 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2313 GDK_POINTER_MOTION_MASK,
2314 NULL, activate_time) != GDK_GRAB_SUCCESS)
2317 gdk_device_ungrab (keyboard, activate_time);
2326 * gtk_combo_box_popup:
2327 * @combo_box: a #GtkComboBox
2329 * Pops up the menu or dropdown list of @combo_box.
2331 * This function is mostly intended for use by accessibility technologies;
2332 * applications should have little use for it.
2337 gtk_combo_box_popup (GtkComboBox *combo_box)
2339 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2341 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2345 * gtk_combo_box_popup_for_device:
2346 * @combo_box: a #GtkComboBox
2347 * @device: a #GdkDevice
2349 * Pops up the menu or dropdown list of @combo_box, the popup window
2350 * will be grabbed so only @device and its associated pointer/keyboard
2351 * are the only #GdkDevice<!-- -->s able to send events to it.
2356 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2359 GtkComboBoxPrivate *priv = combo_box->priv;
2360 gint x, y, width, height;
2361 GtkTreePath *path = NULL, *ppath;
2362 GtkWidget *toplevel;
2363 GdkDevice *keyboard, *pointer;
2366 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2367 g_return_if_fail (GDK_IS_DEVICE (device));
2369 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2372 if (gtk_widget_get_mapped (priv->popup_widget))
2375 if (priv->grab_pointer && priv->grab_keyboard)
2378 time = gtk_get_current_event_time ();
2380 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2383 pointer = gdk_device_get_associated_device (device);
2388 keyboard = gdk_device_get_associated_device (device);
2391 if (GTK_IS_MENU (priv->popup_widget))
2393 gtk_combo_box_menu_popup (combo_box,
2394 priv->activate_button,
2395 priv->activate_time);
2399 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2400 if (GTK_IS_WINDOW (toplevel))
2401 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2402 GTK_WINDOW (priv->popup_window));
2404 gtk_widget_show_all (priv->scrolled_window);
2405 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2407 gtk_widget_set_size_request (priv->popup_window, width, height);
2408 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2410 if (gtk_tree_row_reference_valid (priv->active_row))
2412 path = gtk_tree_row_reference_get_path (priv->active_row);
2413 ppath = gtk_tree_path_copy (path);
2414 if (gtk_tree_path_up (ppath))
2415 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2417 gtk_tree_path_free (ppath);
2419 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2423 gtk_widget_show (priv->popup_window);
2427 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2429 gtk_tree_path_free (path);
2432 gtk_widget_grab_focus (priv->popup_window);
2433 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2436 if (!gtk_widget_has_focus (priv->tree_view))
2437 gtk_widget_grab_focus (priv->tree_view);
2439 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2440 keyboard, pointer, time))
2442 gtk_widget_hide (priv->popup_window);
2446 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2447 priv->grab_pointer = pointer;
2448 priv->grab_keyboard = keyboard;
2452 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2456 device = gtk_get_current_event_device ();
2460 GdkDeviceManager *device_manager;
2461 GdkDisplay *display;
2464 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2465 device_manager = gdk_display_get_device_manager (display);
2467 /* No device was set, pick the first master device */
2468 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2469 device = devices->data;
2470 g_list_free (devices);
2473 gtk_combo_box_popup_for_device (combo_box, device);
2477 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2479 if (combo_box->priv->popup_shown)
2481 gtk_combo_box_popdown (combo_box);
2489 * gtk_combo_box_popdown:
2490 * @combo_box: a #GtkComboBox
2492 * Hides the menu or dropdown list of @combo_box.
2494 * This function is mostly intended for use by accessibility technologies;
2495 * applications should have little use for it.
2500 gtk_combo_box_popdown (GtkComboBox *combo_box)
2502 GtkComboBoxPrivate *priv = combo_box->priv;
2504 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2506 if (GTK_IS_MENU (priv->popup_widget))
2508 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2512 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2515 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2516 gtk_widget_hide (priv->popup_window);
2517 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2520 priv->grab_pointer = NULL;
2521 priv->grab_keyboard = NULL;
2524 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2525 GtkAllocation button_allocation; \
2526 gtk_widget_get_preferred_size (combo_box->priv->button, \
2530 button_allocation.x = allocation->x; \
2532 button_allocation.x = allocation->x + allocation->width \
2535 button_allocation.y = allocation->y; \
2536 button_allocation.width = MAX (1, req.width); \
2537 button_allocation.height = allocation->height; \
2538 button_allocation.height = MAX (1, button_allocation.height); \
2540 gtk_widget_size_allocate (combo_box->priv->button, \
2541 &button_allocation);
2545 gtk_combo_box_size_allocate (GtkWidget *widget,
2546 GtkAllocation *allocation)
2548 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2549 GtkComboBoxPrivate *priv = combo_box->priv;
2550 GtkWidget *child_widget;
2551 GtkAllocation child;
2553 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2556 gtk_widget_set_allocation (widget, allocation);
2557 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2558 get_widget_padding_and_border (widget, &padding);
2560 allocation->x += padding.left;
2561 allocation->y += padding.top;
2562 allocation->width -= padding.left + padding.right;
2563 allocation->height -= padding.top + padding.bottom;
2565 if (!priv->tree_view)
2567 if (priv->cell_view)
2569 GtkBorder button_padding;
2573 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2574 get_widget_padding_and_border (priv->button, &button_padding);
2576 /* menu mode; child_widget is priv->cell_view.
2577 * Allocate the button to the full combobox allocation (minus the
2580 gtk_widget_size_allocate (priv->button, allocation);
2582 child.x = allocation->x;
2583 child.y = allocation->y;
2584 width = allocation->width;
2585 child.height = allocation->height;
2587 if (!priv->is_cell_renderer)
2589 /* restrict allocation of the child into the button box
2590 * if we're not in cell renderer mode.
2592 child.x += border_width + button_padding.left;
2593 child.y += border_width + button_padding.top;
2594 width -= 2 * border_width +
2595 button_padding.left + button_padding.right;
2596 child.height -= 2 * border_width +
2597 button_padding.top + button_padding.bottom;
2600 /* allocate the box containing the separator and the arrow */
2601 gtk_widget_get_preferred_size (priv->box, &req, NULL);
2602 child.width = req.width;
2604 child.x += width - req.width;
2605 child.width = MAX (1, child.width);
2606 child.height = MAX (1, child.height);
2607 gtk_widget_size_allocate (priv->box, &child);
2611 child.x += req.width;
2612 child.width = allocation->x + allocation->width
2613 - border_width - child.x - button_padding.right;
2617 child.width = child.x;
2618 child.x = allocation->x
2619 + border_width + button_padding.left;
2620 child.width -= child.x;
2623 if (gtk_widget_get_visible (priv->popup_widget))
2625 gint width, menu_width;
2627 if (priv->wrap_width == 0)
2629 GtkAllocation combo_box_allocation;
2631 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2632 width = combo_box_allocation.width;
2633 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2635 if (combo_box->priv->popup_fixed_width)
2636 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2638 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2640 gtk_widget_set_size_request (priv->popup_widget,
2641 MAX (width, menu_width), -1);
2644 /* reposition the menu after giving it a new width */
2645 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2648 child.width = MAX (1, child.width);
2649 child.height = MAX (1, child.height);
2650 gtk_widget_size_allocate (child_widget, &child);
2654 /* menu mode; child_widget has been set with gtk_container_add().
2655 * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2656 * Allocate the button at the far end, according to the direction
2659 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2661 /* After the macro, button_allocation has the button allocation rect */
2664 child.x = button_allocation.x + button_allocation.width;
2666 child.x = allocation->x;
2668 child.y = allocation->y;
2669 child.width = allocation->width - button_allocation.width;
2670 child.height = button_allocation.height;
2672 child.width = MAX (1, child.width);
2674 gtk_widget_size_allocate (child_widget, &child);
2679 /* list mode; child_widget might be either priv->cell_view or a child
2680 * added with gtk_container_add().
2683 /* After the macro, button_allocation has the button allocation rect */
2684 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2687 child.x = button_allocation.x + button_allocation.width;
2689 child.x = allocation->x;
2691 child.y = allocation->y;
2692 child.width = allocation->width - button_allocation.width;
2693 child.height = button_allocation.height;
2695 if (priv->cell_view_frame)
2697 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2699 /* restrict allocation of the child into the frame box if it's present */
2700 if (priv->has_frame)
2702 GtkBorder frame_padding;
2705 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2706 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2708 child.x += border_width + frame_padding.left;
2709 child.y += border_width + frame_padding.right;
2710 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2711 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2715 if (gtk_widget_get_visible (priv->popup_window))
2717 gint x, y, width, height;
2718 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2719 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2720 gtk_widget_set_size_request (priv->popup_window, width, height);
2723 /* allocate the child */
2724 child.width = MAX (1, child.width);
2725 child.height = MAX (1, child.height);
2726 gtk_widget_size_allocate (child_widget, &child);
2730 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2733 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2735 GtkComboBoxPrivate *priv = combo_box->priv;
2739 g_signal_handler_disconnect (priv->model,
2741 g_signal_handler_disconnect (priv->model,
2743 g_signal_handler_disconnect (priv->model,
2744 priv->reordered_id);
2745 g_signal_handler_disconnect (priv->model,
2751 g_object_unref (priv->model);
2755 if (priv->active_row)
2757 gtk_tree_row_reference_free (priv->active_row);
2758 priv->active_row = NULL;
2761 if (priv->cell_view)
2762 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2766 gtk_combo_box_forall (GtkContainer *container,
2767 gboolean include_internals,
2768 GtkCallback callback,
2769 gpointer callback_data)
2771 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2772 GtkComboBoxPrivate *priv = combo_box->priv;
2775 if (include_internals)
2778 (* callback) (priv->button, callback_data);
2779 if (priv->cell_view_frame)
2780 (* callback) (priv->cell_view_frame, callback_data);
2783 child = gtk_bin_get_child (GTK_BIN (container));
2785 (* callback) (child, callback_data);
2789 gtk_combo_box_child_show (GtkWidget *widget,
2790 GtkComboBox *combo_box)
2792 GtkComboBoxPrivate *priv = combo_box->priv;
2794 priv->popup_shown = TRUE;
2795 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2799 gtk_combo_box_child_hide (GtkWidget *widget,
2800 GtkComboBox *combo_box)
2802 GtkComboBoxPrivate *priv = combo_box->priv;
2804 priv->popup_shown = FALSE;
2805 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2809 gtk_combo_box_draw (GtkWidget *widget,
2812 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2813 GtkComboBoxPrivate *priv = combo_box->priv;
2815 if (priv->shadow_type != GTK_SHADOW_NONE)
2817 GtkStyleContext *context;
2819 context = gtk_widget_get_style_context (widget);
2821 gtk_render_background (context, cr, 0, 0,
2822 gtk_widget_get_allocated_width (widget),
2823 gtk_widget_get_allocated_height (widget));
2824 gtk_render_frame (context, cr, 0, 0,
2825 gtk_widget_get_allocated_width (widget),
2826 gtk_widget_get_allocated_height (widget));
2829 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2832 if (priv->tree_view && priv->cell_view_frame)
2834 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2835 priv->cell_view_frame, cr);
2838 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2839 gtk_bin_get_child (GTK_BIN (widget)),
2855 path_visible (GtkTreeView *view,
2861 /* Note that we rely on the fact that collapsed rows don't have nodes
2863 return _gtk_tree_view_find_node (view, path, &tree, &node);
2867 tree_next_func (GtkTreeModel *model,
2872 SearchData *search_data = (SearchData *)data;
2874 if (search_data->found)
2876 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2879 if (search_data->visible &&
2880 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2883 search_data->set = TRUE;
2884 search_data->iter = *iter;
2889 if (gtk_tree_path_compare (path, search_data->path) == 0)
2890 search_data->found = TRUE;
2896 tree_next (GtkComboBox *combo,
2897 GtkTreeModel *model,
2902 SearchData search_data;
2904 search_data.combo = combo;
2905 search_data.path = gtk_tree_model_get_path (model, iter);
2906 search_data.visible = visible;
2907 search_data.found = FALSE;
2908 search_data.set = FALSE;
2910 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2912 *next = search_data.iter;
2914 gtk_tree_path_free (search_data.path);
2916 return search_data.set;
2920 tree_prev_func (GtkTreeModel *model,
2925 SearchData *search_data = (SearchData *)data;
2927 if (gtk_tree_path_compare (path, search_data->path) == 0)
2929 search_data->found = TRUE;
2933 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2936 if (search_data->visible &&
2937 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2940 search_data->set = TRUE;
2941 search_data->iter = *iter;
2947 tree_prev (GtkComboBox *combo,
2948 GtkTreeModel *model,
2953 SearchData search_data;
2955 search_data.combo = combo;
2956 search_data.path = gtk_tree_model_get_path (model, iter);
2957 search_data.visible = visible;
2958 search_data.found = FALSE;
2959 search_data.set = FALSE;
2961 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2963 *prev = search_data.iter;
2965 gtk_tree_path_free (search_data.path);
2967 return search_data.set;
2971 tree_last_func (GtkTreeModel *model,
2976 SearchData *search_data = (SearchData *)data;
2978 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2981 /* Note that we rely on the fact that collapsed rows don't have nodes
2983 if (search_data->visible &&
2984 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2987 search_data->set = TRUE;
2988 search_data->iter = *iter;
2994 tree_last (GtkComboBox *combo,
2995 GtkTreeModel *model,
2999 SearchData search_data;
3001 search_data.combo = combo;
3002 search_data.visible = visible;
3003 search_data.set = FALSE;
3005 gtk_tree_model_foreach (model, tree_last_func, &search_data);
3007 *last = search_data.iter;
3009 return search_data.set;
3014 tree_first_func (GtkTreeModel *model,
3019 SearchData *search_data = (SearchData *)data;
3021 if (!tree_column_row_is_sensitive (search_data->combo, iter))
3024 if (search_data->visible &&
3025 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3028 search_data->set = TRUE;
3029 search_data->iter = *iter;
3035 tree_first (GtkComboBox *combo,
3036 GtkTreeModel *model,
3040 SearchData search_data;
3042 search_data.combo = combo;
3043 search_data.visible = visible;
3044 search_data.set = FALSE;
3046 gtk_tree_model_foreach (model, tree_first_func, &search_data);
3048 *first = search_data.iter;
3050 return search_data.set;
3054 gtk_combo_box_scroll_event (GtkWidget *widget,
3055 GdkEventScroll *event)
3057 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3060 GtkTreeIter new_iter;
3062 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3065 if (event->direction == GDK_SCROLL_UP)
3066 found = tree_prev (combo_box, combo_box->priv->model,
3067 &iter, &new_iter, FALSE);
3069 found = tree_next (combo_box, combo_box->priv->model,
3070 &iter, &new_iter, FALSE);
3073 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3082 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3086 GtkComboBoxPrivate *priv = combo->priv;
3088 if (priv->row_separator_func)
3089 return priv->row_separator_func (model, iter, priv->row_separator_data);
3095 gtk_combo_box_header_func (GtkTreeModel *model,
3099 /* Every submenu has a selectable header, however we
3100 * can expose a method to make that configurable by
3101 * the user (like row_separator_func is done) */
3106 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3107 gboolean add_children)
3109 GtkComboBoxPrivate *priv = combo_box->priv;
3113 child = gtk_bin_get_child (GTK_BIN (combo_box));
3115 if (priv->cell_view)
3117 priv->button = gtk_toggle_button_new ();
3118 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3119 priv->focus_on_click);
3121 g_signal_connect (priv->button, "toggled",
3122 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3123 gtk_widget_set_parent (priv->button,
3124 gtk_widget_get_parent (child));
3126 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3127 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3129 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3130 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3132 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3133 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3135 gtk_widget_show_all (priv->button);
3139 priv->button = gtk_toggle_button_new ();
3140 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3141 priv->focus_on_click);
3143 g_signal_connect (priv->button, "toggled",
3144 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3145 gtk_widget_set_parent (priv->button,
3146 gtk_widget_get_parent (child));
3148 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3149 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3150 gtk_widget_show_all (priv->button);
3153 g_signal_connect (priv->button, "button-press-event",
3154 G_CALLBACK (gtk_combo_box_menu_button_press),
3156 g_signal_connect (priv->button, "state-flags-changed",
3157 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3160 /* create our funky menu */
3161 menu = _gtk_tree_menu_new_with_area (priv->area);
3162 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3164 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3166 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3167 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3168 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3169 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3170 combo_box->priv->add_tearoffs);
3172 g_signal_connect (menu, "menu-activate",
3173 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3175 /* Chain our row_separator_func through */
3176 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3177 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3180 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3181 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3184 g_signal_connect (menu, "key-press-event",
3185 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3186 gtk_combo_box_set_popup_widget (combo_box, menu);
3188 gtk_combo_box_update_title (combo_box);
3192 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3194 GtkComboBoxPrivate *priv = combo_box->priv;
3196 g_signal_handlers_disconnect_matched (priv->button,
3197 G_SIGNAL_MATCH_DATA,
3199 gtk_combo_box_menu_button_press, NULL);
3200 g_signal_handlers_disconnect_matched (priv->button,
3201 G_SIGNAL_MATCH_DATA,
3203 gtk_combo_box_button_state_flags_changed, combo_box);
3204 g_signal_handlers_disconnect_matched (priv->popup_widget,
3205 G_SIGNAL_MATCH_DATA,
3207 gtk_combo_box_menu_activate, combo_box);
3209 /* unparent will remove our latest ref */
3210 gtk_widget_unparent (priv->button);
3213 priv->button = NULL;
3215 priv->separator = NULL;
3217 /* changing the popup window will unref the menu and the children */
3222 gtk_combo_box_menu_button_press (GtkWidget *widget,
3223 GdkEventButton *event,
3226 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3227 GtkComboBoxPrivate *priv = combo_box->priv;
3229 if (GTK_IS_MENU (priv->popup_widget) &&
3230 event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3232 if (priv->focus_on_click &&
3233 !gtk_widget_has_focus (priv->button))
3234 gtk_widget_grab_focus (priv->button);
3236 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3245 gtk_combo_box_menu_activate (GtkWidget *menu,
3247 GtkComboBox *combo_box)
3251 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3252 gtk_combo_box_set_active_iter (combo_box, &iter);
3254 g_object_set (combo_box,
3255 "editing-canceled", FALSE,
3261 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3264 gboolean sensitive = TRUE; /* fool code checkers */
3266 if (!combo_box->priv->button)
3269 switch (combo_box->priv->button_sensitivity)
3271 case GTK_SENSITIVITY_ON:
3274 case GTK_SENSITIVITY_OFF:
3277 case GTK_SENSITIVITY_AUTO:
3278 sensitive = combo_box->priv->model &&
3279 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3282 g_assert_not_reached ();
3286 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3288 /* In list-mode, we also need to update sensitivity of the event box */
3289 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3290 && combo_box->priv->cell_view)
3291 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3295 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3300 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3302 if (combo_box->priv->tree_view)
3303 gtk_combo_box_list_popup_resize (combo_box);
3305 gtk_combo_box_update_sensitivity (combo_box);
3309 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3313 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3314 GtkComboBoxPrivate *priv = combo_box->priv;
3316 if (!gtk_tree_row_reference_valid (priv->active_row))
3318 if (priv->cell_view)
3319 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3320 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3323 if (priv->tree_view)
3324 gtk_combo_box_list_popup_resize (combo_box);
3326 gtk_combo_box_update_sensitivity (combo_box);
3330 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3336 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3340 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3345 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3346 GtkComboBoxPrivate *priv = combo_box->priv;
3347 GtkTreePath *active_path;
3349 /* FIXME this belongs to GtkCellView */
3350 if (gtk_tree_row_reference_valid (priv->active_row))
3352 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3353 if (gtk_tree_path_compare (path, active_path) == 0 &&
3355 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3356 gtk_tree_path_free (active_path);
3359 if (priv->tree_view)
3360 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3364 list_popup_resize_idle (gpointer user_data)
3366 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3367 GtkComboBoxPrivate *priv = combo_box->priv;
3368 gint x, y, width, height;
3370 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3372 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3374 gtk_widget_set_size_request (priv->popup_window, width, height);
3375 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3378 priv->resize_idle_id = 0;
3384 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3386 GtkComboBoxPrivate *priv = combo_box->priv;
3388 if (!priv->resize_idle_id)
3389 priv->resize_idle_id =
3390 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3394 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3399 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3401 gtk_combo_box_list_popup_resize (combo_box);
3410 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3412 GtkComboBoxPrivate *priv = combo_box->priv;
3413 GtkTreeSelection *sel;
3415 GtkWidget *widget = GTK_WIDGET (combo_box);
3417 priv->button = gtk_toggle_button_new ();
3418 child = gtk_bin_get_child (GTK_BIN (combo_box));
3419 gtk_widget_set_parent (priv->button,
3420 gtk_widget_get_parent (child));
3421 g_signal_connect (priv->button, "button-press-event",
3422 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3423 g_signal_connect (priv->button, "toggled",
3424 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3426 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3427 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3428 priv->separator = NULL;
3429 gtk_widget_show_all (priv->button);
3431 if (priv->cell_view)
3433 GtkStyleContext *context;
3434 GtkStateFlags state;
3437 context = gtk_widget_get_style_context (widget);
3438 state = gtk_widget_get_state_flags (widget);
3439 gtk_style_context_get_background_color (context, state, &color);
3441 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3443 priv->box = gtk_event_box_new ();
3444 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3447 if (priv->has_frame)
3449 priv->cell_view_frame = gtk_frame_new (NULL);
3450 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3455 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3456 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3460 gtk_widget_set_parent (priv->cell_view_frame,
3461 gtk_widget_get_parent (child));
3462 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3463 gtk_widget_show_all (priv->cell_view_frame);
3465 g_signal_connect (priv->box, "button-press-event",
3466 G_CALLBACK (gtk_combo_box_list_button_pressed),
3470 priv->tree_view = gtk_tree_view_new ();
3471 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3472 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3473 gtk_tree_selection_set_select_function (sel,
3474 gtk_combo_box_list_select_func,
3476 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3478 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3481 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3482 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3486 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3488 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3489 gtk_tree_view_column_new_with_area (priv->area));
3491 if (gtk_tree_row_reference_valid (priv->active_row))
3495 path = gtk_tree_row_reference_get_path (priv->active_row);
3496 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3498 gtk_tree_path_free (path);
3501 /* set sample/popup widgets */
3502 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3504 g_signal_connect (priv->tree_view, "key-press-event",
3505 G_CALLBACK (gtk_combo_box_list_key_press),
3507 g_signal_connect (priv->tree_view, "enter-notify-event",
3508 G_CALLBACK (gtk_combo_box_list_enter_notify),
3510 g_signal_connect (priv->tree_view, "row-expanded",
3511 G_CALLBACK (gtk_combo_box_model_row_expanded),
3513 g_signal_connect (priv->tree_view, "row-collapsed",
3514 G_CALLBACK (gtk_combo_box_model_row_expanded),
3516 g_signal_connect (priv->popup_window, "button-press-event",
3517 G_CALLBACK (gtk_combo_box_list_button_pressed),
3519 g_signal_connect (priv->popup_window, "button-release-event",
3520 G_CALLBACK (gtk_combo_box_list_button_released),
3523 gtk_widget_show (priv->tree_view);
3525 gtk_combo_box_update_sensitivity (combo_box);
3529 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3531 GtkComboBoxPrivate *priv = combo_box->priv;
3533 /* disconnect signals */
3534 g_signal_handlers_disconnect_matched (priv->tree_view,
3535 G_SIGNAL_MATCH_DATA,
3536 0, 0, NULL, NULL, combo_box);
3537 g_signal_handlers_disconnect_matched (priv->button,
3538 G_SIGNAL_MATCH_DATA,
3540 gtk_combo_box_list_button_pressed,
3542 g_signal_handlers_disconnect_matched (priv->popup_window,
3543 G_SIGNAL_MATCH_DATA,
3545 gtk_combo_box_list_button_pressed,
3547 g_signal_handlers_disconnect_matched (priv->popup_window,
3548 G_SIGNAL_MATCH_DATA,
3550 gtk_combo_box_list_button_released,
3553 g_signal_handlers_disconnect_matched (priv->popup_window,
3554 G_SIGNAL_MATCH_DATA,
3556 gtk_combo_box_child_show,
3559 g_signal_handlers_disconnect_matched (priv->popup_window,
3560 G_SIGNAL_MATCH_DATA,
3562 gtk_combo_box_child_hide,
3566 g_signal_handlers_disconnect_matched (priv->box,
3567 G_SIGNAL_MATCH_DATA,
3569 gtk_combo_box_list_button_pressed,
3572 /* destroy things (unparent will kill the latest ref from us)
3573 * last unref on button will destroy the arrow
3575 gtk_widget_unparent (priv->button);
3576 priv->button = NULL;
3579 if (priv->cell_view)
3581 g_object_set (priv->cell_view,
3582 "background-set", FALSE,
3586 if (priv->cell_view_frame)
3588 gtk_widget_unparent (priv->cell_view_frame);
3589 priv->cell_view_frame = NULL;
3593 if (priv->scroll_timer)
3595 g_source_remove (priv->scroll_timer);
3596 priv->scroll_timer = 0;
3599 if (priv->resize_idle_id)
3601 g_source_remove (priv->resize_idle_id);
3602 priv->resize_idle_id = 0;
3605 gtk_widget_destroy (priv->tree_view);
3607 priv->tree_view = NULL;
3608 if (priv->popup_widget)
3610 g_object_unref (priv->popup_widget);
3611 priv->popup_widget = NULL;
3618 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3619 GdkEventButton *event,
3622 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3623 GtkComboBoxPrivate *priv = combo_box->priv;
3625 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3627 if (ewidget == priv->popup_window)
3630 if ((ewidget != priv->button && ewidget != priv->box) ||
3631 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3634 if (priv->focus_on_click &&
3635 !gtk_widget_has_focus (priv->button))
3636 gtk_widget_grab_focus (priv->button);
3638 gtk_combo_box_popup_for_device (combo_box, event->device);
3640 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3642 priv->auto_scroll = FALSE;
3643 if (priv->scroll_timer == 0)
3644 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3645 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3648 priv->popup_in_progress = TRUE;
3654 gtk_combo_box_list_button_released (GtkWidget *widget,
3655 GdkEventButton *event,
3659 GtkTreePath *path = NULL;
3662 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3663 GtkComboBoxPrivate *priv = combo_box->priv;
3665 gboolean popup_in_progress = FALSE;
3667 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3669 if (priv->popup_in_progress)
3671 popup_in_progress = TRUE;
3672 priv->popup_in_progress = FALSE;
3675 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3677 if (priv->scroll_timer)
3679 g_source_remove (priv->scroll_timer);
3680 priv->scroll_timer = 0;
3683 if (ewidget != priv->tree_view)
3685 if ((ewidget == priv->button ||
3686 ewidget == priv->box) &&
3687 !popup_in_progress &&
3688 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3690 gtk_combo_box_popdown (combo_box);
3694 /* released outside treeview */
3695 if (ewidget != priv->button && ewidget != priv->box)
3697 gtk_combo_box_popdown (combo_box);
3705 /* select something cool */
3706 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3712 return TRUE; /* clicked outside window? */
3714 gtk_tree_model_get_iter (priv->model, &iter, path);
3715 gtk_tree_path_free (path);
3717 gtk_combo_box_popdown (combo_box);
3719 if (tree_column_row_is_sensitive (combo_box, &iter))
3720 gtk_combo_box_set_active_iter (combo_box, &iter);
3726 gtk_combo_box_menu_key_press (GtkWidget *widget,
3730 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3732 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3734 /* The menu hasn't managed the
3735 * event, forward it to the combobox
3737 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3744 gtk_combo_box_list_key_press (GtkWidget *widget,
3748 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3751 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3752 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3754 GtkTreeModel *model = NULL;
3756 gtk_combo_box_popdown (combo_box);
3758 if (combo_box->priv->model)
3760 GtkTreeSelection *sel;
3762 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3764 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3765 gtk_combo_box_set_active_iter (combo_box, &iter);
3771 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3773 /* The list hasn't managed the
3774 * event, forward it to the combobox
3776 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3783 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3788 GtkAllocation allocation;
3789 GtkWidget *tree_view = combo_box->priv->tree_view;
3792 gtk_widget_get_allocation (tree_view, &allocation);
3794 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3795 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3797 if (x <= allocation.x &&
3798 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3800 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3801 gtk_adjustment_set_value (adj, value);
3803 else if (x >= allocation.x + allocation.width &&
3804 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3806 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3807 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3811 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3812 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3814 if (y <= allocation.y &&
3815 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3817 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3818 gtk_adjustment_set_value (adj, value);
3820 else if (y >= allocation.height &&
3821 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3823 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3824 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3830 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3832 GtkComboBoxPrivate *priv = combo_box->priv;
3835 if (priv->auto_scroll)
3837 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3840 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3847 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3848 GdkEventCrossing *event,
3851 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3853 combo_box->priv->auto_scroll = TRUE;
3859 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3860 GtkTreeModel *model,
3862 gboolean path_currently_selected,
3865 GList *list, *columns;
3866 gboolean sensitive = FALSE;
3868 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3870 for (list = columns; list && !sensitive; list = list->next)
3872 GList *cells, *cell;
3873 gboolean cell_sensitive, cell_visible;
3875 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3877 if (!gtk_tree_view_column_get_visible (column))
3880 gtk_tree_model_get_iter (model, &iter, path);
3881 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3884 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3887 g_object_get (cell->data,
3888 "sensitive", &cell_sensitive,
3889 "visible", &cell_visible,
3892 if (cell_visible && cell_sensitive)
3901 g_list_free (cells);
3904 g_list_free (columns);
3910 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3915 /* XXX Do nothing ? */
3919 * GtkCellLayout implementation
3921 static GtkCellArea *
3922 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3924 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3925 GtkComboBoxPrivate *priv = combo->priv;
3927 if (G_UNLIKELY (!priv->area))
3929 priv->area = gtk_cell_area_box_new ();
3930 g_object_ref_sink (priv->area);
3941 * gtk_combo_box_new:
3943 * Creates a new empty #GtkComboBox.
3945 * Return value: A new #GtkComboBox.
3950 gtk_combo_box_new (void)
3952 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3956 * gtk_combo_box_new_with_area:
3957 * @area: the #GtkCellArea to use to layout cell renderers
3959 * Creates a new empty #GtkComboBox using @area to layout cells.
3961 * Return value: A new #GtkComboBox.
3964 gtk_combo_box_new_with_area (GtkCellArea *area)
3966 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3970 * gtk_combo_box_new_with_area_and_entry:
3971 * @area: the #GtkCellArea to use to layout cell renderers
3973 * Creates a new empty #GtkComboBox with an entry.
3975 * The new combo box will use @area to layout cells.
3977 * Return value: A new #GtkComboBox.
3980 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3982 return g_object_new (GTK_TYPE_COMBO_BOX,
3990 * gtk_combo_box_new_with_entry:
3992 * Creates a new empty #GtkComboBox with an entry.
3994 * Return value: A new #GtkComboBox.
3997 gtk_combo_box_new_with_entry (void)
3999 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
4003 * gtk_combo_box_new_with_model:
4004 * @model: A #GtkTreeModel.
4006 * Creates a new #GtkComboBox with the model initialized to @model.
4008 * Return value: A new #GtkComboBox.
4013 gtk_combo_box_new_with_model (GtkTreeModel *model)
4015 GtkComboBox *combo_box;
4017 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4019 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4021 return GTK_WIDGET (combo_box);
4025 * gtk_combo_box_new_with_model_and_entry:
4026 * @model: A #GtkTreeModel
4028 * Creates a new empty #GtkComboBox with an entry
4029 * and with the model initialized to @model.
4031 * Return value: A new #GtkComboBox
4034 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4036 return g_object_new (GTK_TYPE_COMBO_BOX,
4043 * gtk_combo_box_get_wrap_width:
4044 * @combo_box: A #GtkComboBox
4046 * Returns the wrap width which is used to determine the number of columns
4047 * for the popup menu. If the wrap width is larger than 1, the combo box
4050 * Returns: the wrap width.
4055 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4057 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4059 return combo_box->priv->wrap_width;
4063 * gtk_combo_box_set_wrap_width:
4064 * @combo_box: A #GtkComboBox
4065 * @width: Preferred number of columns
4067 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4068 * the preferred number of columns when you want the popup to be layed out
4074 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4077 GtkComboBoxPrivate *priv;
4079 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4080 g_return_if_fail (width >= 0);
4082 priv = combo_box->priv;
4084 if (width != priv->wrap_width)
4086 priv->wrap_width = width;
4088 gtk_combo_box_check_appearance (combo_box);
4090 if (GTK_IS_TREE_MENU (priv->popup_widget))
4091 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4093 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4098 * gtk_combo_box_get_row_span_column:
4099 * @combo_box: A #GtkComboBox
4101 * Returns the column with row span information for @combo_box.
4103 * Returns: the row span column.
4108 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4110 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4112 return combo_box->priv->row_column;
4116 * gtk_combo_box_set_row_span_column:
4117 * @combo_box: A #GtkComboBox.
4118 * @row_span: A column in the model passed during construction.
4120 * Sets the column with row span information for @combo_box to be @row_span.
4121 * The row span column contains integers which indicate how many rows
4122 * an item should span.
4127 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4130 GtkComboBoxPrivate *priv;
4133 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4135 priv = combo_box->priv;
4137 col = gtk_tree_model_get_n_columns (priv->model);
4138 g_return_if_fail (row_span >= -1 && row_span < col);
4140 if (row_span != priv->row_column)
4142 priv->row_column = row_span;
4144 if (GTK_IS_TREE_MENU (priv->popup_widget))
4145 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4147 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4152 * gtk_combo_box_get_column_span_column:
4153 * @combo_box: A #GtkComboBox
4155 * Returns the column with column span information for @combo_box.
4157 * Returns: the column span column.
4162 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4164 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4166 return combo_box->priv->col_column;
4170 * gtk_combo_box_set_column_span_column:
4171 * @combo_box: A #GtkComboBox
4172 * @column_span: A column in the model passed during construction
4174 * Sets the column with column span information for @combo_box to be
4175 * @column_span. The column span column contains integers which indicate
4176 * how many columns an item should span.
4181 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4184 GtkComboBoxPrivate *priv;
4187 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4189 priv = combo_box->priv;
4191 col = gtk_tree_model_get_n_columns (priv->model);
4192 g_return_if_fail (column_span >= -1 && column_span < col);
4194 if (column_span != priv->col_column)
4196 priv->col_column = column_span;
4198 if (GTK_IS_TREE_MENU (priv->popup_widget))
4199 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4201 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4206 * gtk_combo_box_get_active:
4207 * @combo_box: A #GtkComboBox
4209 * Returns the index of the currently active item, or -1 if there's no
4210 * active item. If the model is a non-flat treemodel, and the active item
4211 * is not an immediate child of the root of the tree, this function returns
4212 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4213 * <literal>path</literal> is the #GtkTreePath of the active item.
4215 * Return value: An integer which is the index of the currently active item,
4216 * or -1 if there's no active item.
4221 gtk_combo_box_get_active (GtkComboBox *combo_box)
4223 GtkComboBoxPrivate *priv;
4226 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4228 priv = combo_box->priv;
4230 if (gtk_tree_row_reference_valid (priv->active_row))
4234 path = gtk_tree_row_reference_get_path (priv->active_row);
4235 result = gtk_tree_path_get_indices (path)[0];
4236 gtk_tree_path_free (path);
4245 * gtk_combo_box_set_active:
4246 * @combo_box: A #GtkComboBox
4247 * @index_: An index in the model passed during construction, or -1 to have
4250 * Sets the active item of @combo_box to be the item at @index.
4255 gtk_combo_box_set_active (GtkComboBox *combo_box,
4258 GtkTreePath *path = NULL;
4259 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4260 g_return_if_fail (index_ >= -1);
4262 if (combo_box->priv->model == NULL)
4264 /* Save index, in case the model is set after the index */
4265 combo_box->priv->active = index_;
4271 path = gtk_tree_path_new_from_indices (index_, -1);
4273 gtk_combo_box_set_active_internal (combo_box, path);
4276 gtk_tree_path_free (path);
4280 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4283 GtkComboBoxPrivate *priv = combo_box->priv;
4284 GtkTreePath *active_path;
4287 /* Remember whether the initially active row is valid. */
4288 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4290 if (path && is_valid_row_reference)
4292 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4293 path_cmp = gtk_tree_path_compare (path, active_path);
4294 gtk_tree_path_free (active_path);
4299 if (priv->active_row)
4301 gtk_tree_row_reference_free (priv->active_row);
4302 priv->active_row = NULL;
4307 if (priv->tree_view)
4308 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4311 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4313 if (GTK_IS_MENU (menu))
4314 gtk_menu_set_active (menu, -1);
4317 if (priv->cell_view)
4318 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4321 * Do not emit a "changed" signal when an already invalid selection was
4322 * now set to invalid.
4324 if (!is_valid_row_reference)
4330 gtk_tree_row_reference_new (priv->model, path);
4332 if (priv->tree_view)
4334 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4337 else if (GTK_IS_MENU (priv->popup_widget))
4339 /* FIXME handle nested menus better */
4340 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4341 gtk_tree_path_get_indices (path)[0]);
4344 if (priv->cell_view)
4345 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4349 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4350 g_object_notify (G_OBJECT (combo_box), "active");
4351 if (combo_box->priv->id_column >= 0)
4352 g_object_notify (G_OBJECT (combo_box), "active-id");
4357 * gtk_combo_box_get_active_iter:
4358 * @combo_box: A #GtkComboBox
4359 * @iter: (out): The uninitialized #GtkTreeIter
4361 * Sets @iter to point to the current active item, if it exists.
4363 * Return value: %TRUE, if @iter was set
4368 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4374 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4376 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4379 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4380 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4381 gtk_tree_path_free (path);
4387 * gtk_combo_box_set_active_iter:
4388 * @combo_box: A #GtkComboBox
4389 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4391 * Sets the current active item to be the one referenced by @iter, or
4392 * unsets the active item if @iter is %NULL.
4397 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4400 GtkTreePath *path = NULL;
4402 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4405 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4407 gtk_combo_box_set_active_internal (combo_box, path);
4408 gtk_tree_path_free (path);
4412 * gtk_combo_box_set_model:
4413 * @combo_box: A #GtkComboBox
4414 * @model: (allow-none): A #GtkTreeModel
4416 * Sets the model used by @combo_box to be @model. Will unset a previously set
4417 * model (if applicable). If model is %NULL, then it will unset the model.
4419 * Note that this function does not clear the cell renderers, you have to
4420 * call gtk_cell_layout_clear() yourself if you need to set up different
4421 * cell renderers for the new model.
4426 gtk_combo_box_set_model (GtkComboBox *combo_box,
4427 GtkTreeModel *model)
4429 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4430 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4432 if (model == combo_box->priv->model)
4435 gtk_combo_box_unset_model (combo_box);
4440 combo_box->priv->model = model;
4441 g_object_ref (combo_box->priv->model);
4443 combo_box->priv->inserted_id =
4444 g_signal_connect (combo_box->priv->model, "row-inserted",
4445 G_CALLBACK (gtk_combo_box_model_row_inserted),
4447 combo_box->priv->deleted_id =
4448 g_signal_connect (combo_box->priv->model, "row-deleted",
4449 G_CALLBACK (gtk_combo_box_model_row_deleted),
4451 combo_box->priv->reordered_id =
4452 g_signal_connect (combo_box->priv->model, "rows-reordered",
4453 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4455 combo_box->priv->changed_id =
4456 g_signal_connect (combo_box->priv->model, "row-changed",
4457 G_CALLBACK (gtk_combo_box_model_row_changed),
4460 if (combo_box->priv->tree_view)
4463 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4464 combo_box->priv->model);
4465 gtk_combo_box_list_popup_resize (combo_box);
4468 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4471 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4472 combo_box->priv->model);
4475 if (combo_box->priv->cell_view)
4476 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4477 combo_box->priv->model);
4479 if (combo_box->priv->active != -1)
4481 /* If an index was set in advance, apply it now */
4482 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4483 combo_box->priv->active = -1;
4487 gtk_combo_box_update_sensitivity (combo_box);
4489 g_object_notify (G_OBJECT (combo_box), "model");
4493 * gtk_combo_box_get_model:
4494 * @combo_box: A #GtkComboBox
4496 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4498 * Return value: (transfer none): A #GtkTreeModel which was passed
4499 * during construction.
4504 gtk_combo_box_get_model (GtkComboBox *combo_box)
4506 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4508 return combo_box->priv->model;
4512 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4513 GtkScrollType scroll)
4516 GtkTreeIter new_iter;
4517 gboolean active_iter;
4520 if (!combo_box->priv->model)
4522 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4526 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4530 case GTK_SCROLL_STEP_BACKWARD:
4531 case GTK_SCROLL_STEP_UP:
4532 case GTK_SCROLL_STEP_LEFT:
4535 found = tree_prev (combo_box, combo_box->priv->model,
4536 &iter, &new_iter, FALSE);
4539 /* else fall through */
4541 case GTK_SCROLL_PAGE_FORWARD:
4542 case GTK_SCROLL_PAGE_DOWN:
4543 case GTK_SCROLL_PAGE_RIGHT:
4544 case GTK_SCROLL_END:
4545 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4548 case GTK_SCROLL_STEP_FORWARD:
4549 case GTK_SCROLL_STEP_DOWN:
4550 case GTK_SCROLL_STEP_RIGHT:
4553 found = tree_next (combo_box, combo_box->priv->model,
4554 &iter, &new_iter, FALSE);
4557 /* else fall through */
4559 case GTK_SCROLL_PAGE_BACKWARD:
4560 case GTK_SCROLL_PAGE_UP:
4561 case GTK_SCROLL_PAGE_LEFT:
4562 case GTK_SCROLL_START:
4563 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4570 if (found && active_iter)
4572 GtkTreePath *old_path;
4573 GtkTreePath *new_path;
4575 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4576 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4578 if (gtk_tree_path_compare (old_path, new_path) == 0)
4581 gtk_tree_path_free (old_path);
4582 gtk_tree_path_free (new_path);
4587 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4591 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4596 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4597 gboolean group_cycling)
4599 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4601 if (combo_box->priv->has_entry)
4605 child = gtk_bin_get_child (GTK_BIN (combo_box));
4607 gtk_widget_grab_focus (child);
4610 gtk_widget_grab_focus (combo_box->priv->button);
4616 gtk_combo_box_grab_focus (GtkWidget *widget)
4618 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4620 if (combo_box->priv->has_entry)
4624 child = gtk_bin_get_child (GTK_BIN (combo_box));
4626 gtk_widget_grab_focus (child);
4629 gtk_widget_grab_focus (combo_box->priv->button);
4633 gtk_combo_box_destroy (GtkWidget *widget)
4635 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4637 if (combo_box->priv->popup_idle_id > 0)
4639 g_source_remove (combo_box->priv->popup_idle_id);
4640 combo_box->priv->popup_idle_id = 0;
4643 gtk_combo_box_popdown (combo_box);
4645 if (combo_box->priv->row_separator_destroy)
4646 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4648 combo_box->priv->row_separator_func = NULL;
4649 combo_box->priv->row_separator_data = NULL;
4650 combo_box->priv->row_separator_destroy = NULL;
4652 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4653 combo_box->priv->cell_view = NULL;
4657 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4660 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4663 * Fixes regression reported in bug #574059. The old functionality relied on
4664 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4665 * when the selection was already set to -1.
4667 if (gtk_combo_box_get_active(combo_box) == -1)
4668 g_signal_emit_by_name (combo_box, "changed");
4670 gtk_combo_box_set_active (combo_box, -1);
4674 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4677 GtkTreeModel *model;
4680 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4682 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4690 model = gtk_combo_box_get_model (combo_box);
4691 path = gtk_tree_model_get_path (model, &iter);
4692 path_str = gtk_tree_path_to_string (path);
4694 g_signal_handlers_block_by_func (entry,
4695 gtk_combo_box_entry_contents_changed,
4699 g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0,
4702 gtk_entry_set_text (entry, text);
4704 g_signal_handlers_unblock_by_func (entry,
4705 gtk_combo_box_entry_contents_changed,
4708 gtk_tree_path_free (path);
4716 gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
4719 GtkComboBoxPrivate *priv = combo_box->priv;
4720 GtkTreeModel *model;
4724 if (priv->text_column >= 0)
4726 model = gtk_combo_box_get_model (combo_box);
4727 gtk_tree_model_get_iter_from_string (model, &iter, path);
4729 gtk_tree_model_get (model, &iter,
4730 priv->text_column, &text,
4739 gtk_combo_box_constructor (GType type,
4740 guint n_construct_properties,
4741 GObjectConstructParam *construct_properties)
4744 GtkComboBox *combo_box;
4745 GtkComboBoxPrivate *priv;
4747 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4748 (type, n_construct_properties, construct_properties);
4750 combo_box = GTK_COMBO_BOX (object);
4751 priv = combo_box->priv;
4755 priv->area = gtk_cell_area_box_new ();
4756 g_object_ref_sink (priv->area);
4759 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4760 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4761 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4762 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4763 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4764 gtk_widget_show (priv->cell_view);
4766 gtk_combo_box_check_appearance (combo_box);
4768 if (priv->has_entry)
4771 GtkStyleContext *context;
4773 entry = gtk_entry_new ();
4774 gtk_widget_show (entry);
4775 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4777 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4778 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4780 priv->text_renderer = gtk_cell_renderer_text_new ();
4781 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4782 priv->text_renderer, TRUE);
4784 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4786 g_signal_connect (combo_box, "changed",
4787 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4795 gtk_combo_box_dispose(GObject* object)
4797 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4799 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4801 gtk_combo_box_menu_destroy (combo_box);
4802 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4803 combo_box->priv->popup_widget = NULL;
4806 if (combo_box->priv->area)
4808 g_object_unref (combo_box->priv->area);
4809 combo_box->priv->area = NULL;
4812 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4813 gtk_combo_box_list_destroy (combo_box);
4815 if (combo_box->priv->popup_window)
4817 gtk_widget_destroy (combo_box->priv->popup_window);
4818 combo_box->priv->popup_window = NULL;
4821 gtk_combo_box_unset_model (combo_box);
4823 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4827 gtk_combo_box_finalize (GObject *object)
4829 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4831 g_free (combo_box->priv->tearoff_title);
4833 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4837 gtk_cell_editable_key_press (GtkWidget *widget,
4841 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4843 if (event->keyval == GDK_KEY_Escape)
4845 g_object_set (combo_box,
4846 "editing-canceled", TRUE,
4848 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4849 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4853 else if (event->keyval == GDK_KEY_Return ||
4854 event->keyval == GDK_KEY_ISO_Enter ||
4855 event->keyval == GDK_KEY_KP_Enter)
4857 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4858 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4867 popdown_idle (gpointer data)
4869 GtkComboBox *combo_box;
4871 combo_box = GTK_COMBO_BOX (data);
4873 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4874 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4876 g_object_unref (combo_box);
4882 popdown_handler (GtkWidget *widget,
4885 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4889 popup_idle (gpointer data)
4891 GtkComboBox *combo_box;
4893 combo_box = GTK_COMBO_BOX (data);
4895 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4896 combo_box->priv->cell_view)
4897 g_signal_connect_object (combo_box->priv->popup_widget,
4898 "unmap", G_CALLBACK (popdown_handler),
4901 /* we unset this if a menu item is activated */
4902 g_object_set (combo_box,
4903 "editing-canceled", TRUE,
4905 gtk_combo_box_popup (combo_box);
4907 combo_box->priv->popup_idle_id = 0;
4908 combo_box->priv->activate_button = 0;
4909 combo_box->priv->activate_time = 0;
4915 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4918 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4921 combo_box->priv->is_cell_renderer = TRUE;
4923 if (combo_box->priv->cell_view)
4925 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4926 G_CALLBACK (gtk_cell_editable_key_press),
4929 gtk_widget_grab_focus (combo_box->priv->button);
4933 child = gtk_bin_get_child (GTK_BIN (combo_box));
4935 g_signal_connect_object (child, "key-press-event",
4936 G_CALLBACK (gtk_cell_editable_key_press),
4939 gtk_widget_grab_focus (child);
4940 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4943 /* we do the immediate popup only for the optionmenu-like
4946 if (combo_box->priv->is_cell_renderer &&
4947 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4949 if (event && event->type == GDK_BUTTON_PRESS)
4951 GdkEventButton *event_button = (GdkEventButton *)event;
4953 combo_box->priv->activate_button = event_button->button;
4954 combo_box->priv->activate_time = event_button->time;
4957 combo_box->priv->popup_idle_id =
4958 gdk_threads_add_idle (popup_idle, combo_box);
4964 * gtk_combo_box_get_add_tearoffs:
4965 * @combo_box: a #GtkComboBox
4967 * Gets the current value of the :add-tearoffs property.
4969 * Return value: the current value of the :add-tearoffs property.
4972 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4974 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4976 return combo_box->priv->add_tearoffs;
4980 * gtk_combo_box_set_add_tearoffs:
4981 * @combo_box: a #GtkComboBox
4982 * @add_tearoffs: %TRUE to add tearoff menu items
4984 * Sets whether the popup menu should have a tearoff
4990 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4991 gboolean add_tearoffs)
4993 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4995 add_tearoffs = add_tearoffs != FALSE;
4997 if (combo_box->priv->add_tearoffs != add_tearoffs)
4999 combo_box->priv->add_tearoffs = add_tearoffs;
5000 gtk_combo_box_check_appearance (combo_box);
5002 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5003 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
5004 combo_box->priv->add_tearoffs);
5006 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5011 * gtk_combo_box_get_title:
5012 * @combo_box: a #GtkComboBox
5014 * Gets the current title of the menu in tearoff mode. See
5015 * gtk_combo_box_set_add_tearoffs().
5017 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5018 * string which must not be freed.
5023 gtk_combo_box_get_title (GtkComboBox *combo_box)
5025 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5027 return combo_box->priv->tearoff_title;
5031 gtk_combo_box_update_title (GtkComboBox *combo_box)
5033 gtk_combo_box_check_appearance (combo_box);
5035 if (combo_box->priv->popup_widget &&
5036 GTK_IS_MENU (combo_box->priv->popup_widget))
5037 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5038 combo_box->priv->tearoff_title);
5042 * gtk_combo_box_set_title:
5043 * @combo_box: a #GtkComboBox
5044 * @title: a title for the menu in tearoff mode
5046 * Sets the menu's title in tearoff mode.
5051 gtk_combo_box_set_title (GtkComboBox *combo_box,
5054 GtkComboBoxPrivate *priv;
5056 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5058 priv = combo_box->priv;
5060 if (strcmp (title ? title : "",
5061 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5063 g_free (priv->tearoff_title);
5064 priv->tearoff_title = g_strdup (title);
5066 gtk_combo_box_update_title (combo_box);
5068 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5074 * gtk_combo_box_set_popup_fixed_width:
5075 * @combo_box: a #GtkComboBox
5076 * @fixed: whether to use a fixed popup width
5078 * Specifies whether the popup's width should be a fixed width
5079 * matching the allocated width of the combo box.
5084 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5087 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5089 if (combo_box->priv->popup_fixed_width != fixed)
5091 combo_box->priv->popup_fixed_width = fixed;
5093 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5098 * gtk_combo_box_get_popup_fixed_width:
5099 * @combo_box: a #GtkComboBox
5101 * Gets whether the popup uses a fixed width matching
5102 * the allocated width of the combo box.
5104 * Returns: %TRUE if the popup uses a fixed width
5109 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5111 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5113 return combo_box->priv->popup_fixed_width;
5118 * gtk_combo_box_get_popup_accessible:
5119 * @combo_box: a #GtkComboBox
5121 * Gets the accessible object corresponding to the combo box's popup.
5123 * This function is mostly intended for use by accessibility technologies;
5124 * applications should have little use for it.
5126 * Returns: (transfer none): the accessible object corresponding
5127 * to the combo box's popup.
5132 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5136 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5138 if (combo_box->priv->popup_widget)
5140 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5148 * gtk_combo_box_get_row_separator_func: (skip)
5149 * @combo_box: a #GtkComboBox
5151 * Returns the current row separator function.
5153 * Return value: the current row separator function.
5157 GtkTreeViewRowSeparatorFunc
5158 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5160 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5162 return combo_box->priv->row_separator_func;
5166 * gtk_combo_box_set_row_separator_func:
5167 * @combo_box: a #GtkComboBox
5168 * @func: a #GtkTreeViewRowSeparatorFunc
5169 * @data: (allow-none): user data to pass to @func, or %NULL
5170 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5172 * Sets the row separator function, which is used to determine
5173 * whether a row should be drawn as a separator. If the row separator
5174 * function is %NULL, no separators are drawn. This is the default value.
5179 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5180 GtkTreeViewRowSeparatorFunc func,
5182 GDestroyNotify destroy)
5184 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5186 if (combo_box->priv->row_separator_destroy)
5187 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5189 combo_box->priv->row_separator_func = func;
5190 combo_box->priv->row_separator_data = data;
5191 combo_box->priv->row_separator_destroy = destroy;
5193 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5194 if (combo_box->priv->tree_view)
5196 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5197 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5200 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5202 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5203 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5206 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5210 * gtk_combo_box_set_button_sensitivity:
5211 * @combo_box: a #GtkComboBox
5212 * @sensitivity: specify the sensitivity of the dropdown button
5214 * Sets whether the dropdown button of the combo box should be
5215 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5216 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5221 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5222 GtkSensitivityType sensitivity)
5224 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5226 if (combo_box->priv->button_sensitivity != sensitivity)
5228 combo_box->priv->button_sensitivity = sensitivity;
5229 gtk_combo_box_update_sensitivity (combo_box);
5231 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5236 * gtk_combo_box_get_button_sensitivity:
5237 * @combo_box: a #GtkComboBox
5239 * Returns whether the combo box sets the dropdown button
5240 * sensitive or not when there are no items in the model.
5242 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5243 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5244 * if the button is always insensitive or
5245 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5246 * the model has one item to be selected.
5251 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5253 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5255 return combo_box->priv->button_sensitivity;
5260 * gtk_combo_box_get_has_entry:
5261 * @combo_box: a #GtkComboBox
5263 * Returns whether the combo box has an entry.
5265 * Return Value: whether there is an entry in @combo_box.
5270 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5272 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5274 return combo_box->priv->has_entry;
5278 * gtk_combo_box_set_entry_text_column:
5279 * @combo_box: A #GtkComboBox
5280 * @text_column: A column in @model to get the strings from for
5281 * the internal entry
5283 * Sets the model column which @combo_box should use to get strings from
5284 * to be @text_column. The column @text_column in the model of @combo_box
5285 * must be of type %G_TYPE_STRING.
5287 * This is only relevant if @combo_box has been created with
5288 * #GtkComboBox:has-entry as %TRUE.
5293 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5296 GtkComboBoxPrivate *priv = combo_box->priv;
5297 GtkTreeModel *model;
5299 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5301 model = gtk_combo_box_get_model (combo_box);
5303 g_return_if_fail (text_column >= 0);
5304 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5306 priv->text_column = text_column;
5308 if (priv->text_renderer != NULL)
5309 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5310 priv->text_renderer,
5311 "text", text_column,
5316 * gtk_combo_box_get_entry_text_column:
5317 * @combo_box: A #GtkComboBox.
5319 * Returns the column which @combo_box is using to get the strings
5320 * from to display in the internal entry.
5322 * Return value: A column in the data source model of @combo_box.
5327 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5329 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5331 return combo_box->priv->text_column;
5335 * gtk_combo_box_set_focus_on_click:
5336 * @combo: a #GtkComboBox
5337 * @focus_on_click: whether the combo box grabs focus when clicked
5340 * Sets whether the combo box will grab focus when it is clicked with
5341 * the mouse. Making mouse clicks not grab focus is useful in places
5342 * like toolbars where you don't want the keyboard focus removed from
5343 * the main area of the application.
5348 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5349 gboolean focus_on_click)
5351 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5353 focus_on_click = focus_on_click != FALSE;
5355 if (combo_box->priv->focus_on_click != focus_on_click)
5357 combo_box->priv->focus_on_click = focus_on_click;
5359 if (combo_box->priv->button)
5360 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5363 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5368 * gtk_combo_box_get_focus_on_click:
5369 * @combo: a #GtkComboBox
5371 * Returns whether the combo box grabs focus when it is clicked
5372 * with the mouse. See gtk_combo_box_set_focus_on_click().
5374 * Return value: %TRUE if the combo box grabs focus when it is
5375 * clicked with the mouse.
5380 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5382 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5384 return combo_box->priv->focus_on_click;
5389 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5390 GtkBuilder *builder,
5392 const gchar *tagname,
5393 GMarkupParser *parser,
5396 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5397 tagname, parser, data))
5400 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5401 tagname, parser, data);
5405 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5406 GtkBuilder *builder,
5408 const gchar *tagname,
5411 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5412 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5416 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5417 GtkBuilder *builder,
5418 const gchar *childname)
5420 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5422 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5423 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5425 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5429 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5433 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5434 GtkComboBoxPrivate *priv = combo_box->priv;
5435 gint font_size, arrow_size;
5436 PangoContext *context;
5437 PangoFontMetrics *metrics;
5438 const PangoFontDescription *font_desc;
5440 gint minimum_width = 0, natural_width = 0;
5441 gint child_min, child_nat;
5442 GtkStyleContext *style_context;
5443 GtkStateFlags state;
5445 gfloat arrow_scaling;
5447 child = gtk_bin_get_child (GTK_BIN (widget));
5450 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5452 gtk_widget_style_get (GTK_WIDGET (widget),
5453 "arrow-size", &arrow_size,
5454 "arrow-scaling", &arrow_scaling,
5457 style_context = gtk_widget_get_style_context (widget);
5458 state = gtk_widget_get_state_flags (widget);
5460 get_widget_padding_and_border (widget, &padding);
5461 font_desc = gtk_style_context_get_font (style_context, state);
5463 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5464 metrics = pango_context_get_metrics (context, font_desc,
5465 pango_context_get_language (context));
5466 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5467 pango_font_metrics_get_descent (metrics));
5468 pango_font_metrics_unref (metrics);
5470 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5472 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5474 if (!priv->tree_view)
5477 if (priv->cell_view)
5479 gint box_width, xpad;
5480 GtkBorder button_padding;
5482 get_widget_padding_and_border (priv->button, &button_padding);
5484 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5485 xpad = button_padding.left + button_padding.right + padding.left + padding.right;
5487 minimum_width = child_min + box_width + xpad;
5488 natural_width = child_nat + box_width + xpad;
5492 gint but_width, but_nat_width;
5494 gtk_widget_get_preferred_width (priv->button,
5495 &but_width, &but_nat_width);
5497 minimum_width = child_min + but_width;
5498 natural_width = child_nat + but_nat_width;
5504 gint button_width, button_nat_width;
5506 /* sample + frame */
5507 minimum_width = child_min;
5508 natural_width = child_nat;
5510 if (priv->cell_view_frame)
5512 if (priv->has_frame)
5514 gint border_width, xpad;
5515 GtkBorder frame_padding;
5517 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5518 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5519 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5521 minimum_width += xpad;
5522 natural_width += xpad;
5527 gtk_widget_get_preferred_width (priv->button,
5528 &button_width, &button_nat_width);
5530 minimum_width += button_width;
5531 natural_width += button_nat_width;
5534 minimum_width += padding.left + padding.right;
5535 natural_width += padding.left + padding.right;
5538 *minimum_size = minimum_width;
5541 *natural_size = natural_width;
5545 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5551 /* Combo box is height-for-width only
5552 * (so we always just reserve enough height for the minimum width) */
5553 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5554 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5558 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5563 /* Combo box is height-for-width only
5564 * (so we assume we always reserved enough height for the minimum width) */
5565 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5570 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5575 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5576 GtkComboBoxPrivate *priv = combo_box->priv;
5577 gint min_height = 0, nat_height = 0;
5582 child = gtk_bin_get_child (GTK_BIN (widget));
5584 get_widget_padding_and_border (widget, &padding);
5587 if (!priv->tree_view)
5590 if (priv->cell_view)
5592 /* calculate x/y padding and separator/arrow size */
5593 gint box_width, box_height;
5595 GtkBorder button_padding;
5597 get_widget_padding_and_border (priv->button, &button_padding);
5599 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5600 gtk_widget_get_preferred_height_for_width (priv->box,
5601 box_width, &box_height, NULL);
5603 xpad = button_padding.left + button_padding.right;
5604 ypad = button_padding.top + button_padding.bottom;
5606 size -= box_width + xpad;
5608 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5609 * and fitting the whole treemodel */
5610 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5612 min_height = MAX (min_height, box_height);
5613 nat_height = MAX (nat_height, box_height);
5620 /* there is a custom child widget inside (no priv->cell_view) */
5621 gint but_width, but_height;
5623 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5624 gtk_widget_get_preferred_height_for_width (priv->button,
5625 but_width, &but_height, NULL);
5629 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5630 * and fitting the whole treemodel */
5631 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5633 min_height = MAX (min_height, but_height);
5634 nat_height = MAX (nat_height, but_height);
5640 gint but_width, but_height;
5641 gint xpad = 0, ypad = 0;
5643 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5644 gtk_widget_get_preferred_height_for_width (priv->button,
5645 but_width, &but_height, NULL);
5647 if (priv->cell_view_frame && priv->has_frame)
5649 GtkBorder frame_padding;
5652 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5653 get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5655 xpad = (2 * border_width) + padding.left + frame_padding.right;
5656 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5662 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5663 * and fitting the whole treemodel */
5664 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5666 min_height = MAX (min_height, but_height);
5667 nat_height = MAX (nat_height, but_height);
5673 min_height += padding.top + padding.bottom;
5674 nat_height += padding.top + padding.bottom;
5677 *minimum_size = min_height;
5680 *natural_size = nat_height;
5684 * gtk_combo_box_set_id_column:
5685 * @combo_box: A #GtkComboBox
5686 * @id_column: A column in @model to get string IDs for values from
5688 * Sets the model column which @combo_box should use to get string IDs
5689 * for values from. The column @id_column in the model of @combo_box
5690 * must be of type %G_TYPE_STRING.
5695 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5698 GtkComboBoxPrivate *priv = combo_box->priv;
5699 GtkTreeModel *model;
5701 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5703 if (id_column != priv->id_column)
5705 model = gtk_combo_box_get_model (combo_box);
5707 g_return_if_fail (id_column >= 0);
5708 g_return_if_fail (model == NULL ||
5709 id_column < gtk_tree_model_get_n_columns (model));
5711 priv->id_column = id_column;
5713 g_object_notify (G_OBJECT (combo_box), "id-column");
5714 g_object_notify (G_OBJECT (combo_box), "active-id");
5719 * gtk_combo_box_get_id_column:
5720 * @combo_box: A #GtkComboBox
5722 * Returns the column which @combo_box is using to get string IDs
5725 * Return value: A column in the data source model of @combo_box.
5730 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5732 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5734 return combo_box->priv->id_column;
5738 * gtk_combo_box_get_active_id:
5739 * @combo_box: a #GtkComboBox
5741 * Returns the ID of the active row of @combo_box. This value is taken
5742 * from the active row and the column specified by the #GtkComboBox:id-column
5743 * property of @combo_box (see gtk_combo_box_set_id_column()).
5745 * The returned value is an interned string which means that you can
5746 * compare the pointer by value to other interned strings and that you
5749 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5750 * no row is active, or if the active row has a %NULL ID value, then %NULL
5753 * Return value: the ID of the active row, or %NULL
5758 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5760 GtkTreeModel *model;
5764 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5766 column = combo_box->priv->id_column;
5771 model = gtk_combo_box_get_model (combo_box);
5772 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5773 G_TYPE_STRING, NULL);
5775 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5777 const gchar *interned;
5780 gtk_tree_model_get (model, &iter, column, &id, -1);
5781 interned = g_intern_string (id);
5791 * gtk_combo_box_set_active_id:
5792 * @combo_box: a #GtkComboBox
5793 * @active_id: (allow-none): the ID of the row to select, or %NULL
5795 * Changes the active row of @combo_box to the one that has an ID equal to
5796 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5797 * a %NULL ID string cannot be made active by this function.
5799 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5800 * row has the given ID then the function does nothing and returns %FALSE.
5802 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5803 * @active_id was given to unset the active row, the function
5804 * always returns %TRUE.
5809 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5810 const gchar *active_id)
5812 GtkTreeModel *model;
5814 gboolean match = FALSE;
5817 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5819 if (active_id == NULL)
5821 gtk_combo_box_set_active (combo_box, -1);
5822 return TRUE; /* active row was successfully unset */
5825 column = combo_box->priv->id_column;
5830 model = gtk_combo_box_get_model (combo_box);
5831 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5832 G_TYPE_STRING, FALSE);
5834 if (gtk_tree_model_get_iter_first (model, &iter))
5838 gtk_tree_model_get (model, &iter, column, &id, -1);
5840 match = strcmp (id, active_id) == 0;
5845 gtk_combo_box_set_active_iter (combo_box, &iter);
5848 } while (gtk_tree_model_iter_next (model, &iter));