2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gtkcombobox.h"
22 #include "gtkadjustment.h"
23 #include "gtkcellareabox.h"
24 #include "gtktreemenu.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcellview.h"
30 #include "gtkeventbox.h"
33 #include "gtkliststore.h"
35 #include "gtkmenuprivate.h"
36 #include "gtkmenushellprivate.h"
37 #include "gtkscrolledwindow.h"
38 #include "gtkseparatormenuitem.h"
39 #include "deprecated/gtktearoffmenuitem.h"
40 #include "gtktogglebutton.h"
41 #include "gtktreeselection.h"
42 #include "gtkseparator.h"
43 #include "gtkwidgetpath.h"
44 #include "gtkwindow.h"
45 #include "gtktypebuiltins.h"
46 #include "gtkprivate.h"
48 #include <gobject/gvaluecollector.h>
53 #include "gtkmarshalers.h"
56 #include "gtkentryprivate.h"
57 #include "gtktreeprivate.h"
58 #include "a11y/gtkcomboboxaccessible.h"
63 * @Short_description: A widget used to choose from a list of items
65 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
67 * A GtkComboBox is a widget that allows the user to choose from a list of
68 * valid choices. The GtkComboBox displays the selected choice. When
69 * activated, the GtkComboBox displays a popup which allows the user to
70 * make a new choice. The style in which the selected value is displayed,
71 * and the style of the popup is determined by the current theme. It may
72 * be similar to a Windows-style combo box.
74 * The GtkComboBox uses the model-view pattern; the list of valid choices
75 * is specified in the form of a tree model, and the display of the choices
76 * can be adapted to the data in the model by using cell renderers, as you
77 * would in a tree view. This is possible since GtkComboBox implements the
78 * #GtkCellLayout interface. The tree model holding the valid choices is
79 * not restricted to a flat list, it can be a real tree, and the popup will
80 * reflect the tree structure.
82 * To allow the user to enter values not in the model, the 'has-entry'
83 * property allows the GtkComboBox to contain a #GtkEntry. This entry
84 * can be accessed by calling gtk_bin_get_child() on the combo box.
86 * For a simple list of textual choices, the model-view API of GtkComboBox
87 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
88 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
93 /* WELCOME, to THE house of evil code */
94 struct _GtkComboBoxPrivate
104 GtkShadowType shadow_type;
106 gint active; /* Only temporary */
107 GtkTreeRowReference *active_row;
109 GtkWidget *tree_view;
111 GtkWidget *cell_view;
112 GtkWidget *cell_view_frame;
117 GtkWidget *separator;
119 GtkWidget *popup_widget;
120 GtkWidget *popup_window;
121 GtkWidget *scrolled_window;
128 guint activate_button;
129 guint32 activate_time;
131 guint resize_idle_id;
133 /* For "has-entry" specific behavior we track
134 * an automated cell renderer and text column
137 GtkCellRenderer *text_renderer;
141 guint popup_in_progress : 1;
142 guint popup_shown : 1;
143 guint add_tearoffs : 1;
145 guint is_cell_renderer : 1;
146 guint editing_canceled : 1;
147 guint auto_scroll : 1;
148 guint focus_on_click : 1;
149 guint button_sensitivity : 2;
151 guint popup_fixed_width : 1;
153 GtkTreeViewRowSeparatorFunc row_separator_func;
154 gpointer row_separator_data;
155 GDestroyNotify row_separator_destroy;
157 GdkDevice *grab_pointer;
158 GdkDevice *grab_keyboard;
160 gchar *tearoff_title;
163 /* While debugging this evil code, I have learned that
164 * there are actually 4 modes to this widget, which can
165 * be characterized as follows
167 * 1) menu mode, no child added
170 * cell_view -> GtkCellView, regular child
171 * cell_view_frame -> NULL
172 * button -> GtkToggleButton set_parent to combo
173 * arrow -> GtkArrow set_parent to button
174 * separator -> GtkVSepator set_parent to button
175 * popup_widget -> GtkMenu
176 * popup_window -> NULL
177 * scrolled_window -> NULL
179 * 2) menu mode, child added
183 * cell_view_frame -> NULL
184 * button -> GtkToggleButton set_parent to combo
185 * arrow -> GtkArrow, child of button
187 * popup_widget -> GtkMenu
188 * popup_window -> NULL
189 * scrolled_window -> NULL
191 * 3) list mode, no child added
193 * tree_view -> GtkTreeView, child of scrolled_window
194 * cell_view -> GtkCellView, regular child
195 * cell_view_frame -> GtkFrame, set parent to combo
196 * button -> GtkToggleButton, set_parent to combo
197 * arrow -> GtkArrow, child of button
199 * popup_widget -> tree_view
200 * popup_window -> GtkWindow
201 * scrolled_window -> GtkScrolledWindow, child of popup_window
203 * 4) list mode, child added
205 * tree_view -> GtkTreeView, child of scrolled_window
207 * cell_view_frame -> NULL
208 * button -> GtkToggleButton, set_parent to combo
209 * arrow -> GtkArrow, child of button
211 * popup_widget -> tree_view
212 * popup_window -> GtkWindow
213 * scrolled_window -> GtkScrolledWindow, child of popup_window
230 PROP_ROW_SPAN_COLUMN,
231 PROP_COLUMN_SPAN_COLUMN,
238 PROP_BUTTON_SENSITIVITY,
239 PROP_EDITING_CANCELED,
241 PROP_ENTRY_TEXT_COLUMN,
242 PROP_POPUP_FIXED_WIDTH,
248 static guint combo_box_signals[LAST_SIGNAL] = {0,};
250 #define SCROLL_TIME 100
254 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
255 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
256 static GObject *gtk_combo_box_constructor (GType type,
257 guint n_construct_properties,
258 GObjectConstructParam *construct_properties);
259 static void gtk_combo_box_dispose (GObject *object);
260 static void gtk_combo_box_finalize (GObject *object);
261 static void gtk_combo_box_destroy (GtkWidget *widget);
263 static void gtk_combo_box_set_property (GObject *object,
267 static void gtk_combo_box_get_property (GObject *object,
272 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
273 GtkStateFlags previous);
274 static void gtk_combo_box_grab_focus (GtkWidget *widget);
275 static void gtk_combo_box_style_updated (GtkWidget *widget);
276 static void gtk_combo_box_button_toggled (GtkWidget *widget,
278 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
279 GtkStateFlags previous,
281 static void gtk_combo_box_add (GtkContainer *container,
283 static void gtk_combo_box_remove (GtkContainer *container,
286 static void gtk_combo_box_menu_show (GtkWidget *menu,
288 static void gtk_combo_box_menu_hide (GtkWidget *menu,
291 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
293 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
298 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
303 static void gtk_combo_box_menu_position (GtkMenu *menu,
309 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
311 static void gtk_combo_box_size_allocate (GtkWidget *widget,
312 GtkAllocation *allocation);
313 static void gtk_combo_box_forall (GtkContainer *container,
314 gboolean include_internals,
315 GtkCallback callback,
316 gpointer callback_data);
317 static gboolean gtk_combo_box_draw (GtkWidget *widget,
319 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
320 GdkEventScroll *event);
321 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
324 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
325 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
326 GtkScrollType scroll);
327 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
328 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
330 /* listening to the model */
331 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
335 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
338 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
343 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
347 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
353 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
358 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
359 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
361 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
362 GdkEventButton *event,
364 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
367 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
368 GdkEventCrossing *event,
370 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
373 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
374 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
375 GdkEventButton *event,
378 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
381 gboolean path_currently_selected,
384 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
388 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
391 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
392 gboolean add_children);
393 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
394 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
397 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
398 GdkEventButton *event,
400 static void gtk_combo_box_menu_activate (GtkWidget *menu,
402 GtkComboBox *combo_box);
403 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
404 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
407 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
409 guint32 activate_time);
412 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
414 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
415 gboolean group_cycling);
417 static void gtk_combo_box_child_show (GtkWidget *widget,
418 GtkComboBox *combo_box);
419 static void gtk_combo_box_child_hide (GtkWidget *widget,
420 GtkComboBox *combo_box);
422 /* GtkComboBox:has-entry callbacks */
423 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
425 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
427 static gchar *gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
430 /* GtkBuildable method implementation */
431 static GtkBuildableIface *parent_buildable_iface;
433 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
434 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
437 const gchar *tagname,
438 GMarkupParser *parser,
440 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
443 const gchar *tagname,
445 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
447 const gchar *childname);
450 /* GtkCellEditable method implementations */
451 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
454 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
457 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
460 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
464 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
468 static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container,
470 static void gtk_combo_box_direction_changed (GtkWidget *widget,
471 GtkTextDirection previous_direction);
473 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
474 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
475 gtk_combo_box_cell_layout_init)
476 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
477 gtk_combo_box_cell_editable_init)
478 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
479 gtk_combo_box_buildable_init))
484 gtk_combo_box_class_init (GtkComboBoxClass *klass)
486 GObjectClass *object_class;
487 GtkContainerClass *container_class;
488 GtkWidgetClass *widget_class;
489 GtkBindingSet *binding_set;
491 container_class = (GtkContainerClass *)klass;
492 container_class->forall = gtk_combo_box_forall;
493 container_class->add = gtk_combo_box_add;
494 container_class->remove = gtk_combo_box_remove;
495 container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
497 gtk_container_class_handle_border_width (container_class);
499 widget_class = (GtkWidgetClass *)klass;
500 widget_class->size_allocate = gtk_combo_box_size_allocate;
501 widget_class->draw = gtk_combo_box_draw;
502 widget_class->scroll_event = gtk_combo_box_scroll_event;
503 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
504 widget_class->grab_focus = gtk_combo_box_grab_focus;
505 widget_class->style_updated = gtk_combo_box_style_updated;
506 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
507 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
508 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
509 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
510 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
511 widget_class->destroy = gtk_combo_box_destroy;
512 widget_class->direction_changed = gtk_combo_box_direction_changed;
514 object_class = (GObjectClass *)klass;
515 object_class->constructor = gtk_combo_box_constructor;
516 object_class->dispose = gtk_combo_box_dispose;
517 object_class->finalize = gtk_combo_box_finalize;
518 object_class->set_property = gtk_combo_box_set_property;
519 object_class->get_property = gtk_combo_box_get_property;
521 klass->format_entry_text = gtk_combo_box_format_entry_text;
525 * GtkComboBox::changed:
526 * @widget: the object which received the signal
528 * The changed signal is emitted when the active
529 * item is changed. The can be due to the user selecting
530 * a different item from the list, or due to a
531 * call to gtk_combo_box_set_active_iter().
532 * It will also be emitted while typing into the entry of a combo box
537 combo_box_signals[CHANGED] =
538 g_signal_new (I_("changed"),
539 G_OBJECT_CLASS_TYPE (klass),
541 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
543 g_cclosure_marshal_VOID__VOID,
546 * GtkComboBox::move-active:
547 * @widget: the object that received the signal
548 * @scroll_type: a #GtkScrollType
550 * The ::move-active signal is a
551 * <link linkend="keybinding-signals">keybinding signal</link>
552 * which gets emitted to move the active selection.
556 combo_box_signals[MOVE_ACTIVE] =
557 g_signal_new_class_handler (I_("move-active"),
558 G_OBJECT_CLASS_TYPE (klass),
559 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
560 G_CALLBACK (gtk_combo_box_real_move_active),
562 g_cclosure_marshal_VOID__ENUM,
564 GTK_TYPE_SCROLL_TYPE);
567 * GtkComboBox::popup:
568 * @widget: the object that received the signal
570 * The ::popup signal is a
571 * <link linkend="keybinding-signals">keybinding signal</link>
572 * which gets emitted to popup the combo box list.
574 * The default binding for this signal is Alt+Down.
578 combo_box_signals[POPUP] =
579 g_signal_new_class_handler (I_("popup"),
580 G_OBJECT_CLASS_TYPE (klass),
581 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
582 G_CALLBACK (gtk_combo_box_real_popup),
584 g_cclosure_marshal_VOID__VOID,
587 * GtkComboBox::popdown:
588 * @button: the object which received the signal
590 * The ::popdown signal is a
591 * <link linkend="keybinding-signals">keybinding signal</link>
592 * which gets emitted to popdown the combo box list.
594 * The default bindings for this signal are Alt+Up and Escape.
598 combo_box_signals[POPDOWN] =
599 g_signal_new_class_handler (I_("popdown"),
600 G_OBJECT_CLASS_TYPE (klass),
601 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
602 G_CALLBACK (gtk_combo_box_real_popdown),
604 _gtk_marshal_BOOLEAN__VOID,
608 * GtkComboBox::format-entry-text:
609 * @combo: the object which received the signal
610 * @path: the GtkTreePath string from the combo box's current model to format text for
612 * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
614 * A signal which allows you to change how the text displayed in a combo box's
615 * entry is displayed.
617 * Connect a signal handler which returns an allocated string representing
618 * @path. That string will then be used to set the text in the combo box's entry.
619 * The default signal handler uses the text from the GtkComboBox::entry-text-column
622 * Here's an example signal handler which fetches data from the model and
623 * displays it in the entry.
626 * format_entry_text_callback (GtkComboBox *combo,
628 * gpointer user_data)
631 * GtkTreeModel model;
634 * model = gtk_combo_box_get_model (combo);
636 * gtk_tree_model_get_iter_from_string (model, &iter, path);
637 * gtk_tree_model_get (model, &iter,
638 * THE_DOUBLE_VALUE_COLUMN, &value,
641 * return g_strdup_printf ("%g", value);
645 * Return value: (transfer full): a newly allocated string representing @path
646 * for the current GtkComboBox model.
650 combo_box_signals[FORMAT_ENTRY_TEXT] =
651 g_signal_new (I_("format-entry-text"),
652 G_TYPE_FROM_CLASS (klass),
654 G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text),
655 _gtk_single_string_accumulator, NULL,
656 _gtk_marshal_STRING__STRING,
657 G_TYPE_STRING, 1, G_TYPE_STRING);
660 binding_set = gtk_binding_set_by_class (widget_class);
662 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
664 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
667 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
669 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
671 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
674 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
676 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
677 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
679 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
680 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
682 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
683 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
685 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
686 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
688 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
689 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
691 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
693 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
695 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
696 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
698 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
699 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
701 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
702 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
704 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
705 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
707 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
708 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
710 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
713 g_object_class_override_property (object_class,
714 PROP_EDITING_CANCELED,
720 * The model from which the combo box takes the values shown
725 g_object_class_install_property (object_class,
727 g_param_spec_object ("model",
728 P_("ComboBox model"),
729 P_("The model for the combo box"),
731 GTK_PARAM_READWRITE));
734 * GtkComboBox:wrap-width:
736 * If wrap-width is set to a positive value, the list will be
737 * displayed in multiple columns, the number of columns is
738 * determined by wrap-width.
742 g_object_class_install_property (object_class,
744 g_param_spec_int ("wrap-width",
746 P_("Wrap width for laying out the items in a grid"),
750 GTK_PARAM_READWRITE));
754 * GtkComboBox:row-span-column:
756 * If this is set to a non-negative value, it must be the index of a column
757 * of type %G_TYPE_INT in the model.
759 * The values of that column are used to determine how many rows a value in
760 * the list will span. Therefore, the values in the model column pointed to
761 * by this property must be greater than zero and not larger than wrap-width.
765 g_object_class_install_property (object_class,
766 PROP_ROW_SPAN_COLUMN,
767 g_param_spec_int ("row-span-column",
768 P_("Row span column"),
769 P_("TreeModel column containing the row span values"),
773 GTK_PARAM_READWRITE));
777 * GtkComboBox:column-span-column:
779 * If this is set to a non-negative value, it must be the index of a column
780 * of type %G_TYPE_INT in the model.
782 * The values of that column are used to determine how many columns a value
783 * in the list will span.
787 g_object_class_install_property (object_class,
788 PROP_COLUMN_SPAN_COLUMN,
789 g_param_spec_int ("column-span-column",
790 P_("Column span column"),
791 P_("TreeModel column containing the column span values"),
795 GTK_PARAM_READWRITE));
799 * GtkComboBox:active:
801 * The item which is currently active. If the model is a non-flat treemodel,
802 * and the active item is not an immediate child of the root of the tree,
803 * this property has the value
804 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
805 * where <literal>path</literal> is the #GtkTreePath of the active item.
809 g_object_class_install_property (object_class,
811 g_param_spec_int ("active",
813 P_("The item which is currently active"),
817 GTK_PARAM_READWRITE));
820 * GtkComboBox:add-tearoffs:
822 * The add-tearoffs property controls whether generated menus
823 * have tearoff menu items.
825 * Note that this only affects menu style combo boxes.
829 g_object_class_install_property (object_class,
831 g_param_spec_boolean ("add-tearoffs",
832 P_("Add tearoffs to menus"),
833 P_("Whether dropdowns should have a tearoff menu item"),
835 GTK_PARAM_READWRITE));
838 * GtkComboBox:has-frame:
840 * The has-frame property controls whether a frame
841 * is drawn around the entry.
845 g_object_class_install_property (object_class,
847 g_param_spec_boolean ("has-frame",
849 P_("Whether the combo box draws a frame around the child"),
851 GTK_PARAM_READWRITE));
853 g_object_class_install_property (object_class,
855 g_param_spec_boolean ("focus-on-click",
856 P_("Focus on click"),
857 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
859 GTK_PARAM_READWRITE));
862 * GtkComboBox:tearoff-title:
864 * A title that may be displayed by the window manager
865 * when the popup is torn-off.
869 g_object_class_install_property (object_class,
871 g_param_spec_string ("tearoff-title",
873 P_("A title that may be displayed by the window manager when the popup is torn-off"),
875 GTK_PARAM_READWRITE));
879 * GtkComboBox:popup-shown:
881 * Whether the combo boxes dropdown is popped up.
882 * Note that this property is mainly useful, because
883 * it allows you to connect to notify::popup-shown.
887 g_object_class_install_property (object_class,
889 g_param_spec_boolean ("popup-shown",
891 P_("Whether the combo's dropdown is shown"),
893 GTK_PARAM_READABLE));
897 * GtkComboBox:button-sensitivity:
899 * Whether the dropdown button is sensitive when
900 * the model is empty.
904 g_object_class_install_property (object_class,
905 PROP_BUTTON_SENSITIVITY,
906 g_param_spec_enum ("button-sensitivity",
907 P_("Button Sensitivity"),
908 P_("Whether the dropdown button is sensitive when the model is empty"),
909 GTK_TYPE_SENSITIVITY_TYPE,
910 GTK_SENSITIVITY_AUTO,
911 GTK_PARAM_READWRITE));
914 * GtkComboBox:has-entry:
916 * Whether the combo box has an entry.
920 g_object_class_install_property (object_class,
922 g_param_spec_boolean ("has-entry",
924 P_("Whether combo box has an entry"),
926 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
929 * GtkComboBox:entry-text-column:
931 * The column in the combo box's model to associate with strings from the entry
932 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
936 g_object_class_install_property (object_class,
937 PROP_ENTRY_TEXT_COLUMN,
938 g_param_spec_int ("entry-text-column",
939 P_("Entry Text Column"),
940 P_("The column in the combo box's model to associate "
941 "with strings from the entry if the combo was "
942 "created with #GtkComboBox:has-entry = %TRUE"),
944 GTK_PARAM_READWRITE));
947 * GtkComboBox:id-column:
949 * The column in the combo box's model that provides string
950 * IDs for the values in the model, if != -1.
954 g_object_class_install_property (object_class,
956 g_param_spec_int ("id-column",
958 P_("The column in the combo box's model that provides "
959 "string IDs for the values in the model"),
961 GTK_PARAM_READWRITE));
964 * GtkComboBox:active-id:
966 * The value of the ID column of the active row.
970 g_object_class_install_property (object_class,
972 g_param_spec_string ("active-id",
974 P_("The value of the id column "
975 "for the active row"),
976 NULL, GTK_PARAM_READWRITE));
979 * GtkComboBox:popup-fixed-width:
981 * Whether the popup's width should be a fixed width matching the
982 * allocated width of the combo box.
986 g_object_class_install_property (object_class,
987 PROP_POPUP_FIXED_WIDTH,
988 g_param_spec_boolean ("popup-fixed-width",
989 P_("Popup Fixed Width"),
990 P_("Whether the popup's width should be a "
991 "fixed width matching the allocated width "
994 GTK_PARAM_READWRITE));
997 * GtkComboBox:cell-area:
999 * The #GtkCellArea used to layout cell renderers for this combo box.
1001 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
1002 * a horizontally oriented #GtkCellAreaBox will be used.
1006 g_object_class_install_property (object_class,
1008 g_param_spec_object ("cell-area",
1010 P_("The GtkCellArea used to layout cells"),
1012 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1014 gtk_widget_class_install_style_property (widget_class,
1015 g_param_spec_boolean ("appears-as-list",
1016 P_("Appears as list"),
1017 P_("Whether dropdowns should look like lists rather than menus"),
1019 GTK_PARAM_READABLE));
1022 * GtkComboBox:arrow-size:
1024 * Sets the minimum size of the arrow in the combo box. Note
1025 * that the arrow size is coupled to the font size, so in case
1026 * a larger font is used, the arrow will be larger than set
1031 gtk_widget_class_install_style_property (widget_class,
1032 g_param_spec_int ("arrow-size",
1034 P_("The minimum size of the arrow in the combo box"),
1038 GTK_PARAM_READABLE));
1041 * GtkComboBox:arrow-scaling:
1043 * Sets the amount of space used up by the combobox arrow,
1044 * proportional to the font size.
1048 gtk_widget_class_install_style_property (widget_class,
1049 g_param_spec_float ("arrow-scaling",
1050 P_("Arrow Scaling"),
1051 P_("The amount of space used by the arrow"),
1055 GTK_PARAM_READABLE));
1058 * GtkComboBox:shadow-type:
1060 * Which kind of shadow to draw around the combo box.
1064 gtk_widget_class_install_style_property (widget_class,
1065 g_param_spec_enum ("shadow-type",
1067 P_("Which kind of shadow to draw around the combo box"),
1068 GTK_TYPE_SHADOW_TYPE,
1070 GTK_PARAM_READABLE));
1072 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1074 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1078 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1080 parent_buildable_iface = g_type_interface_peek_parent (iface);
1081 iface->add_child = _gtk_cell_layout_buildable_add_child;
1082 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1083 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1084 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1088 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1090 iface->get_area = gtk_combo_box_cell_layout_get_area;
1094 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1096 iface->start_editing = gtk_combo_box_start_editing;
1100 gtk_combo_box_init (GtkComboBox *combo_box)
1102 GtkComboBoxPrivate *priv;
1104 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1106 GtkComboBoxPrivate);
1107 priv = combo_box->priv;
1109 priv->wrap_width = 0;
1112 priv->active_row = NULL;
1113 priv->col_column = -1;
1114 priv->row_column = -1;
1116 priv->popup_shown = FALSE;
1117 priv->add_tearoffs = FALSE;
1118 priv->has_frame = TRUE;
1119 priv->is_cell_renderer = FALSE;
1120 priv->editing_canceled = FALSE;
1121 priv->auto_scroll = FALSE;
1122 priv->focus_on_click = TRUE;
1123 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1124 priv->has_entry = FALSE;
1125 priv->popup_fixed_width = TRUE;
1127 priv->text_column = -1;
1128 priv->text_renderer = NULL;
1129 priv->id_column = -1;
1133 gtk_combo_box_set_property (GObject *object,
1135 const GValue *value,
1138 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1139 GtkComboBoxPrivate *priv = combo_box->priv;
1145 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1148 case PROP_WRAP_WIDTH:
1149 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1152 case PROP_ROW_SPAN_COLUMN:
1153 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1156 case PROP_COLUMN_SPAN_COLUMN:
1157 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1161 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1164 case PROP_ADD_TEAROFFS:
1165 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1168 case PROP_HAS_FRAME:
1169 priv->has_frame = g_value_get_boolean (value);
1171 if (priv->has_entry)
1175 child = gtk_bin_get_child (GTK_BIN (combo_box));
1177 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1182 case PROP_FOCUS_ON_CLICK:
1183 gtk_combo_box_set_focus_on_click (combo_box,
1184 g_value_get_boolean (value));
1187 case PROP_TEAROFF_TITLE:
1188 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1191 case PROP_POPUP_SHOWN:
1192 if (g_value_get_boolean (value))
1193 gtk_combo_box_popup (combo_box);
1195 gtk_combo_box_popdown (combo_box);
1198 case PROP_BUTTON_SENSITIVITY:
1199 gtk_combo_box_set_button_sensitivity (combo_box,
1200 g_value_get_enum (value));
1203 case PROP_POPUP_FIXED_WIDTH:
1204 gtk_combo_box_set_popup_fixed_width (combo_box,
1205 g_value_get_boolean (value));
1208 case PROP_EDITING_CANCELED:
1209 priv->editing_canceled = g_value_get_boolean (value);
1212 case PROP_HAS_ENTRY:
1213 priv->has_entry = g_value_get_boolean (value);
1216 case PROP_ENTRY_TEXT_COLUMN:
1217 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1220 case PROP_ID_COLUMN:
1221 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1224 case PROP_ACTIVE_ID:
1225 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1228 case PROP_CELL_AREA:
1229 /* Construct-only, can only be assigned once */
1230 area = g_value_get_object (value);
1233 if (priv->area != NULL)
1235 g_warning ("cell-area has already been set, ignoring construct property");
1236 g_object_ref_sink (area);
1237 g_object_unref (area);
1240 priv->area = g_object_ref_sink (area);
1245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1251 gtk_combo_box_get_property (GObject *object,
1256 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1257 GtkComboBoxPrivate *priv = combo_box->priv;
1262 g_value_set_object (value, combo_box->priv->model);
1265 case PROP_WRAP_WIDTH:
1266 g_value_set_int (value, combo_box->priv->wrap_width);
1269 case PROP_ROW_SPAN_COLUMN:
1270 g_value_set_int (value, combo_box->priv->row_column);
1273 case PROP_COLUMN_SPAN_COLUMN:
1274 g_value_set_int (value, combo_box->priv->col_column);
1278 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1281 case PROP_ADD_TEAROFFS:
1282 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1285 case PROP_HAS_FRAME:
1286 g_value_set_boolean (value, combo_box->priv->has_frame);
1289 case PROP_FOCUS_ON_CLICK:
1290 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1293 case PROP_TEAROFF_TITLE:
1294 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1297 case PROP_POPUP_SHOWN:
1298 g_value_set_boolean (value, combo_box->priv->popup_shown);
1301 case PROP_BUTTON_SENSITIVITY:
1302 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1305 case PROP_POPUP_FIXED_WIDTH:
1306 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1309 case PROP_EDITING_CANCELED:
1310 g_value_set_boolean (value, priv->editing_canceled);
1313 case PROP_HAS_ENTRY:
1314 g_value_set_boolean (value, priv->has_entry);
1317 case PROP_ENTRY_TEXT_COLUMN:
1318 g_value_set_int (value, priv->text_column);
1321 case PROP_ID_COLUMN:
1322 g_value_set_int (value, priv->id_column);
1325 case PROP_ACTIVE_ID:
1326 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1329 case PROP_CELL_AREA:
1330 g_value_set_object (value, priv->area);
1334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1340 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1341 GtkStateFlags previous)
1343 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1344 GtkComboBoxPrivate *priv = combo_box->priv;
1346 if (gtk_widget_get_realized (widget))
1348 if (priv->tree_view && priv->cell_view)
1350 GtkStyleContext *context;
1351 GtkStateFlags state;
1354 context = gtk_widget_get_style_context (widget);
1355 state = gtk_widget_get_state_flags (widget);
1356 gtk_style_context_get_background_color (context, state, &color);
1358 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1362 gtk_widget_queue_draw (widget);
1366 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1367 GtkStateFlags previous,
1370 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1371 GtkComboBoxPrivate *priv = combo_box->priv;
1373 if (gtk_widget_get_realized (widget))
1375 if (!priv->tree_view && priv->cell_view)
1376 gtk_widget_set_state_flags (priv->cell_view,
1377 gtk_widget_get_state_flags (widget),
1381 gtk_widget_queue_draw (widget);
1385 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1387 gtk_container_forall (GTK_CONTAINER (combo_box),
1388 (GtkCallback) gtk_widget_reset_style,
1393 gtk_combo_box_direction_changed (GtkWidget *widget,
1394 GtkTextDirection previous_direction)
1396 gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1399 static GtkWidgetPath *
1400 gtk_combo_box_get_path_for_child (GtkContainer *container,
1403 GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1404 GtkWidgetPath *path;
1406 gboolean found = FALSE;
1407 GList *visible_children, *l;
1408 GtkWidgetPath *sibling_path;
1411 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
1413 if (gtk_widget_get_visible (child))
1415 visible_children = NULL;
1417 if (priv->button && gtk_widget_get_visible (priv->button))
1418 visible_children = g_list_prepend (visible_children, priv->button);
1420 if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1421 visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1423 widget = gtk_bin_get_child (GTK_BIN (container));
1424 if (widget && gtk_widget_get_visible (widget))
1425 visible_children = g_list_prepend (visible_children, widget);
1427 if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1428 visible_children = g_list_reverse (visible_children);
1432 for (l = visible_children; l; l = l->next)
1436 if (widget == child)
1448 sibling_path = gtk_widget_path_new ();
1450 for (l = visible_children; l; l = l->next)
1451 gtk_widget_path_append_for_widget (sibling_path, l->data);
1453 gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1455 g_list_free (visible_children);
1456 gtk_widget_path_unref (sibling_path);
1460 gtk_widget_path_append_for_widget (path, child);
1467 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1469 GtkComboBoxPrivate *priv = combo_box->priv;
1470 gboolean appears_as_list;
1472 /* if wrap_width > 0, then we are in grid-mode and forced to use
1475 if (priv->wrap_width)
1476 appears_as_list = FALSE;
1478 gtk_widget_style_get (GTK_WIDGET (combo_box),
1479 "appears-as-list", &appears_as_list,
1482 if (appears_as_list)
1484 /* Destroy all the menu mode widgets, if they exist. */
1485 if (GTK_IS_MENU (priv->popup_widget))
1486 gtk_combo_box_menu_destroy (combo_box);
1488 /* Create the list mode widgets, if they don't already exist. */
1489 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1490 gtk_combo_box_list_setup (combo_box);
1494 /* Destroy all the list mode widgets, if they exist. */
1495 if (GTK_IS_TREE_VIEW (priv->tree_view))
1496 gtk_combo_box_list_destroy (combo_box);
1498 /* Create the menu mode widgets, if they don't already exist. */
1499 if (!GTK_IS_MENU (priv->popup_widget))
1500 gtk_combo_box_menu_setup (combo_box, TRUE);
1503 gtk_widget_style_get (GTK_WIDGET (combo_box),
1504 "shadow-type", &priv->shadow_type,
1509 gtk_combo_box_style_updated (GtkWidget *widget)
1511 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1512 GtkComboBoxPrivate *priv = combo_box->priv;
1515 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1517 gtk_combo_box_check_appearance (combo_box);
1519 if (priv->tree_view && priv->cell_view)
1521 GtkStyleContext *context;
1522 GtkStateFlags state;
1525 context = gtk_widget_get_style_context (widget);
1526 state = gtk_widget_get_state_flags (widget);
1527 gtk_style_context_get_background_color (context, state, &color);
1529 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1532 child = gtk_bin_get_child (GTK_BIN (combo_box));
1533 if (GTK_IS_ENTRY (child))
1534 g_object_set (child, "shadow-type",
1535 GTK_SHADOW_NONE == priv->shadow_type ?
1536 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1540 gtk_combo_box_button_toggled (GtkWidget *widget,
1543 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1545 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1547 if (!combo_box->priv->popup_in_progress)
1548 gtk_combo_box_popup (combo_box);
1551 gtk_combo_box_popdown (combo_box);
1555 gtk_combo_box_add (GtkContainer *container,
1558 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1559 GtkComboBoxPrivate *priv = combo_box->priv;
1561 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1563 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1564 "(need an instance of GtkEntry or of a subclass)",
1565 G_OBJECT_TYPE_NAME (widget));
1569 if (priv->cell_view &&
1570 gtk_widget_get_parent (priv->cell_view))
1572 gtk_widget_unparent (priv->cell_view);
1573 _gtk_bin_set_child (GTK_BIN (container), NULL);
1574 gtk_widget_queue_resize (GTK_WIDGET (container));
1577 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1578 _gtk_bin_set_child (GTK_BIN (container), widget);
1580 if (priv->cell_view &&
1581 widget != priv->cell_view)
1583 /* since the cell_view was unparented, it's gone now */
1584 priv->cell_view = NULL;
1586 if (!priv->tree_view && priv->separator)
1588 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1590 priv->separator = NULL;
1592 gtk_widget_queue_resize (GTK_WIDGET (container));
1594 else if (priv->cell_view_frame)
1596 gtk_widget_unparent (priv->cell_view_frame);
1597 priv->cell_view_frame = NULL;
1602 if (priv->has_entry)
1604 /* this flag is a hack to tell the entry to fill its allocation.
1606 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1608 g_signal_connect (widget, "changed",
1609 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1612 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1617 gtk_combo_box_remove (GtkContainer *container,
1620 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1621 GtkComboBoxPrivate *priv = combo_box->priv;
1623 gboolean appears_as_list;
1625 if (priv->has_entry)
1627 GtkWidget *child_widget;
1629 child_widget = gtk_bin_get_child (GTK_BIN (container));
1630 if (widget && widget == child_widget)
1632 g_signal_handlers_disconnect_by_func (widget,
1633 gtk_combo_box_entry_contents_changed,
1635 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1639 if (widget == priv->cell_view)
1640 priv->cell_view = NULL;
1642 gtk_widget_unparent (widget);
1643 _gtk_bin_set_child (GTK_BIN (container), NULL);
1645 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1648 gtk_widget_queue_resize (GTK_WIDGET (container));
1650 if (!priv->tree_view)
1651 appears_as_list = FALSE;
1653 appears_as_list = TRUE;
1655 if (appears_as_list)
1656 gtk_combo_box_list_destroy (combo_box);
1657 else if (GTK_IS_MENU (priv->popup_widget))
1659 gtk_combo_box_menu_destroy (combo_box);
1660 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1661 priv->popup_widget = NULL;
1664 if (!priv->cell_view)
1666 priv->cell_view = gtk_cell_view_new ();
1667 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1668 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1670 gtk_widget_show (priv->cell_view);
1671 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1676 if (appears_as_list)
1677 gtk_combo_box_list_setup (combo_box);
1679 gtk_combo_box_menu_setup (combo_box, TRUE);
1681 if (gtk_tree_row_reference_valid (priv->active_row))
1683 path = gtk_tree_row_reference_get_path (priv->active_row);
1684 gtk_combo_box_set_active_internal (combo_box, path);
1685 gtk_tree_path_free (path);
1688 gtk_combo_box_set_active_internal (combo_box, NULL);
1692 gtk_combo_box_menu_show (GtkWidget *menu,
1695 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1696 GtkComboBoxPrivate *priv = combo_box->priv;
1698 gtk_combo_box_child_show (menu, user_data);
1700 priv->popup_in_progress = TRUE;
1701 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1703 priv->popup_in_progress = FALSE;
1707 gtk_combo_box_menu_hide (GtkWidget *menu,
1710 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1712 gtk_combo_box_child_hide (menu,user_data);
1714 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1719 gtk_combo_box_detacher (GtkWidget *widget,
1722 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1723 GtkComboBoxPrivate *priv = combo_box->priv;
1725 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1727 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1728 gtk_combo_box_menu_show,
1730 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1731 gtk_combo_box_menu_hide,
1734 priv->popup_widget = NULL;
1738 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1741 GtkComboBoxPrivate *priv = combo_box->priv;
1743 if (GTK_IS_MENU (priv->popup_widget))
1745 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1746 priv->popup_widget = NULL;
1748 else if (priv->popup_widget)
1750 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1751 priv->popup_widget);
1752 g_object_unref (priv->popup_widget);
1753 priv->popup_widget = NULL;
1756 if (GTK_IS_MENU (popup))
1758 if (priv->popup_window)
1760 gtk_widget_destroy (priv->popup_window);
1761 priv->popup_window = NULL;
1764 priv->popup_widget = popup;
1767 * Note that we connect to show/hide on the toplevel, not the
1768 * menu itself, since the menu is not shown/hidden when it is
1769 * popped up while torn-off.
1771 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1772 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1773 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1774 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1776 gtk_menu_attach_to_widget (GTK_MENU (popup),
1777 GTK_WIDGET (combo_box),
1778 gtk_combo_box_detacher);
1782 if (!priv->popup_window)
1784 GtkWidget *toplevel;
1786 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1787 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1789 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1790 GDK_WINDOW_TYPE_HINT_COMBO);
1792 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1793 G_CALLBACK (gtk_combo_box_child_show),
1795 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1796 G_CALLBACK (gtk_combo_box_child_hide),
1799 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1800 if (GTK_IS_WINDOW (toplevel))
1802 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1803 GTK_WINDOW (priv->popup_window));
1804 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1805 GTK_WINDOW (toplevel));
1808 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1809 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1810 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1812 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1814 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1817 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1820 gtk_widget_show (priv->scrolled_window);
1822 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1823 priv->scrolled_window);
1826 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1829 gtk_widget_show (popup);
1830 g_object_ref (popup);
1831 priv->popup_widget = popup;
1836 get_widget_padding_and_border (GtkWidget *widget,
1839 GtkStyleContext *context;
1840 GtkStateFlags state;
1843 context = gtk_widget_get_style_context (widget);
1844 state = gtk_style_context_get_state (context);
1846 gtk_style_context_get_padding (context, state, padding);
1847 gtk_style_context_get_border (context, state, &tmp);
1849 padding->top += tmp.top;
1850 padding->right += tmp.right;
1851 padding->bottom += tmp.bottom;
1852 padding->left += tmp.left;
1856 gtk_combo_box_menu_position_below (GtkMenu *menu,
1862 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1863 GtkAllocation child_allocation;
1869 GdkRectangle monitor;
1872 /* FIXME: is using the size request here broken? */
1873 child = gtk_bin_get_child (GTK_BIN (combo_box));
1877 gtk_widget_get_allocation (child, &child_allocation);
1879 if (!gtk_widget_get_has_window (child))
1881 sx += child_allocation.x;
1882 sy += child_allocation.y;
1885 gdk_window_get_root_coords (gtk_widget_get_window (child),
1887 get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding);
1889 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1894 if (combo_box->priv->popup_fixed_width)
1895 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1897 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1899 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1902 *x = sx + child_allocation.width - req.width;
1905 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1906 monitor_num = gdk_screen_get_monitor_at_window (screen,
1907 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1908 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1912 else if (*x + req.width > monitor.x + monitor.width)
1913 *x = monitor.x + monitor.width - req.width;
1915 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1916 *y += child_allocation.height;
1917 else if (*y - monitor.y >= req.height)
1919 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1920 *y += child_allocation.height;
1928 gtk_combo_box_menu_position_over (GtkMenu *menu,
1934 GtkComboBox *combo_box;
1938 GtkAllocation allocation;
1939 GtkAllocation child_allocation;
1946 combo_box = GTK_COMBO_BOX (user_data);
1947 widget = GTK_WIDGET (combo_box);
1949 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1951 gtk_widget_get_allocation (widget, &allocation);
1953 menu_xpos = allocation.x;
1954 menu_ypos = allocation.y + allocation.height / 2 - 2;
1956 if (combo_box->priv->popup_fixed_width)
1957 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1959 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1963 gtk_widget_get_allocation (active, &child_allocation);
1964 menu_ypos -= child_allocation.height / 2;
1967 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1970 child = children->data;
1972 if (active == child)
1975 if (gtk_widget_get_visible (child))
1977 gtk_widget_get_allocation (child, &child_allocation);
1979 menu_ypos -= child_allocation.height;
1982 children = children->next;
1985 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1986 menu_xpos = menu_xpos + allocation.width - menu_width;
1988 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1989 menu_xpos, menu_ypos,
1990 &menu_xpos, &menu_ypos);
1992 /* Clamp the position on screen */
1993 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1997 else if ((menu_xpos + menu_width) > screen_width)
1998 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
2007 gtk_combo_box_menu_position (GtkMenu *menu,
2013 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2014 GtkComboBoxPrivate *priv = combo_box->priv;
2015 GtkWidget *menu_item;
2017 if (priv->wrap_width > 0 || priv->cell_view == NULL)
2018 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2021 /* FIXME handle nested menus better */
2022 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2024 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2027 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2030 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2031 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2032 GDK_WINDOW_TYPE_HINT_COMBO);
2036 gtk_combo_box_list_position (GtkComboBox *combo_box,
2042 GtkComboBoxPrivate *priv = combo_box->priv;
2043 GtkAllocation allocation;
2046 GdkRectangle monitor;
2047 GtkRequisition popup_req;
2048 GtkPolicyType hpolicy, vpolicy;
2051 /* under windows, the drop down list is as wide as the combo box itself.
2053 GtkWidget *widget = GTK_WIDGET (combo_box);
2057 gtk_widget_get_allocation (widget, &allocation);
2059 if (!gtk_widget_get_has_window (widget))
2065 window = gtk_widget_get_window (widget);
2067 gdk_window_get_root_coords (gtk_widget_get_window (widget),
2070 *width = allocation.width;
2072 hpolicy = vpolicy = GTK_POLICY_NEVER;
2073 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2076 if (combo_box->priv->popup_fixed_width)
2078 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2080 if (popup_req.width > *width)
2082 hpolicy = GTK_POLICY_ALWAYS;
2083 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2089 /* XXX This code depends on treeviews properly reporting their natural width
2090 * list-mode menus won't fill up to their natural width until then */
2091 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2093 if (popup_req.width > *width)
2095 hpolicy = GTK_POLICY_NEVER;
2096 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2099 *width = popup_req.width;
2103 *height = popup_req.height;
2105 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2106 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2107 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2109 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2110 *x = *x + allocation.width - *width;
2114 else if (*x + *width > monitor.x + monitor.width)
2115 *x = monitor.x + monitor.width - *width;
2117 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2118 *y += allocation.height;
2119 else if (*y - *height >= monitor.y)
2121 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2123 *y += allocation.height;
2124 *height = monitor.y + monitor.height - *y;
2128 *height = *y - monitor.y;
2132 if (popup_req.height > *height)
2134 vpolicy = GTK_POLICY_ALWAYS;
2136 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2142 cell_layout_is_sensitive (GtkCellLayout *layout)
2144 GList *cells, *list;
2147 cells = gtk_cell_layout_get_cells (layout);
2150 for (list = cells; list; list = list->next)
2152 g_object_get (list->data, "sensitive", &sensitive, NULL);
2157 g_list_free (cells);
2163 cell_is_sensitive (GtkCellRenderer *cell,
2166 gboolean *sensitive = data;
2168 g_object_get (cell, "sensitive", sensitive, NULL);
2174 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2177 GtkComboBoxPrivate *priv = combo_box->priv;
2179 if (priv->row_separator_func)
2181 if (priv->row_separator_func (priv->model, iter,
2182 priv->row_separator_data))
2190 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2194 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2203 update_menu_sensitivity (GtkComboBox *combo_box,
2206 GtkComboBoxPrivate *priv = combo_box->priv;
2207 GList *children, *child;
2208 GtkWidget *item, *submenu, *separator;
2209 GtkWidget *cell_view;
2215 children = gtk_container_get_children (GTK_CONTAINER (menu));
2217 for (child = children; child; child = child->next)
2219 item = GTK_WIDGET (child->data);
2220 cell_view = gtk_bin_get_child (GTK_BIN (item));
2222 if (!GTK_IS_CELL_VIEW (cell_view))
2225 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2226 if (submenu != NULL)
2228 gtk_widget_set_sensitive (item, TRUE);
2229 update_menu_sensitivity (combo_box, submenu);
2233 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2235 if (menu != priv->popup_widget && child == children)
2237 separator = GTK_WIDGET (child->next->data);
2238 g_object_set (item, "visible", sensitive, NULL);
2239 g_object_set (separator, "visible", sensitive, NULL);
2242 gtk_widget_set_sensitive (item, sensitive);
2246 g_list_free (children);
2250 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2252 guint32 activate_time)
2254 GtkComboBoxPrivate *priv = combo_box->priv;
2257 gint width, min_width, nat_width;
2259 update_menu_sensitivity (combo_box, priv->popup_widget);
2262 if (gtk_tree_row_reference_valid (priv->active_row))
2264 path = gtk_tree_row_reference_get_path (priv->active_row);
2265 active_item = gtk_tree_path_get_indices (path)[0];
2266 gtk_tree_path_free (path);
2268 if (priv->add_tearoffs)
2272 /* FIXME handle nested menus better */
2273 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2275 if (priv->wrap_width == 0)
2277 GtkAllocation allocation;
2279 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2280 width = allocation.width;
2281 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2282 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2284 if (combo_box->priv->popup_fixed_width)
2285 width = MAX (width, min_width);
2287 width = MAX (width, nat_width);
2289 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2292 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2294 gtk_combo_box_menu_position, combo_box,
2295 button, activate_time);
2299 popup_grab_on_window (GdkWindow *window,
2300 GdkDevice *keyboard,
2302 guint32 activate_time)
2305 gdk_device_grab (keyboard, window,
2306 GDK_OWNERSHIP_WINDOW, TRUE,
2307 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2308 NULL, activate_time) != GDK_GRAB_SUCCESS)
2312 gdk_device_grab (pointer, window,
2313 GDK_OWNERSHIP_WINDOW, TRUE,
2314 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2315 GDK_POINTER_MOTION_MASK,
2316 NULL, activate_time) != GDK_GRAB_SUCCESS)
2319 gdk_device_ungrab (keyboard, activate_time);
2328 * gtk_combo_box_popup:
2329 * @combo_box: a #GtkComboBox
2331 * Pops up the menu or dropdown list of @combo_box.
2333 * This function is mostly intended for use by accessibility technologies;
2334 * applications should have little use for it.
2339 gtk_combo_box_popup (GtkComboBox *combo_box)
2341 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2343 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2347 * gtk_combo_box_popup_for_device:
2348 * @combo_box: a #GtkComboBox
2349 * @device: a #GdkDevice
2351 * Pops up the menu or dropdown list of @combo_box, the popup window
2352 * will be grabbed so only @device and its associated pointer/keyboard
2353 * are the only #GdkDevice<!-- -->s able to send events to it.
2358 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2361 GtkComboBoxPrivate *priv = combo_box->priv;
2362 gint x, y, width, height;
2363 GtkTreePath *path = NULL, *ppath;
2364 GtkWidget *toplevel;
2365 GdkDevice *keyboard, *pointer;
2368 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2369 g_return_if_fail (GDK_IS_DEVICE (device));
2371 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2374 if (gtk_widget_get_mapped (priv->popup_widget))
2377 if (priv->grab_pointer && priv->grab_keyboard)
2380 time = gtk_get_current_event_time ();
2382 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2385 pointer = gdk_device_get_associated_device (device);
2390 keyboard = gdk_device_get_associated_device (device);
2393 if (GTK_IS_MENU (priv->popup_widget))
2395 gtk_combo_box_menu_popup (combo_box,
2396 priv->activate_button,
2397 priv->activate_time);
2401 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2402 if (GTK_IS_WINDOW (toplevel))
2403 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2404 GTK_WINDOW (priv->popup_window));
2406 gtk_widget_show_all (priv->scrolled_window);
2407 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2409 gtk_widget_set_size_request (priv->popup_window, width, height);
2410 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2412 if (gtk_tree_row_reference_valid (priv->active_row))
2414 path = gtk_tree_row_reference_get_path (priv->active_row);
2415 ppath = gtk_tree_path_copy (path);
2416 if (gtk_tree_path_up (ppath))
2417 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2419 gtk_tree_path_free (ppath);
2421 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2425 gtk_widget_show (priv->popup_window);
2429 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2431 gtk_tree_path_free (path);
2434 gtk_widget_grab_focus (priv->popup_window);
2435 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2438 if (!gtk_widget_has_focus (priv->tree_view))
2439 gtk_widget_grab_focus (priv->tree_view);
2441 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2442 keyboard, pointer, time))
2444 gtk_widget_hide (priv->popup_window);
2448 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2449 priv->grab_pointer = pointer;
2450 priv->grab_keyboard = keyboard;
2454 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2458 device = gtk_get_current_event_device ();
2462 GdkDeviceManager *device_manager;
2463 GdkDisplay *display;
2466 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2467 device_manager = gdk_display_get_device_manager (display);
2469 /* No device was set, pick the first master device */
2470 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2471 device = devices->data;
2472 g_list_free (devices);
2475 gtk_combo_box_popup_for_device (combo_box, device);
2479 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2481 if (combo_box->priv->popup_shown)
2483 gtk_combo_box_popdown (combo_box);
2491 * gtk_combo_box_popdown:
2492 * @combo_box: a #GtkComboBox
2494 * Hides the menu or dropdown list of @combo_box.
2496 * This function is mostly intended for use by accessibility technologies;
2497 * applications should have little use for it.
2502 gtk_combo_box_popdown (GtkComboBox *combo_box)
2504 GtkComboBoxPrivate *priv = combo_box->priv;
2506 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2508 if (GTK_IS_MENU (priv->popup_widget))
2510 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2514 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2517 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2518 gtk_widget_hide (priv->popup_window);
2519 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2522 priv->grab_pointer = NULL;
2523 priv->grab_keyboard = NULL;
2526 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2527 GtkAllocation button_allocation; \
2528 gtk_widget_get_preferred_size (combo_box->priv->button, \
2532 button_allocation.x = allocation->x; \
2534 button_allocation.x = allocation->x + allocation->width \
2537 button_allocation.y = allocation->y; \
2538 button_allocation.width = MAX (1, req.width); \
2539 button_allocation.height = allocation->height; \
2540 button_allocation.height = MAX (1, button_allocation.height); \
2542 gtk_widget_size_allocate (combo_box->priv->button, \
2543 &button_allocation);
2547 gtk_combo_box_size_allocate (GtkWidget *widget,
2548 GtkAllocation *allocation)
2550 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2551 GtkComboBoxPrivate *priv = combo_box->priv;
2552 GtkWidget *child_widget;
2553 GtkAllocation child;
2555 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2558 gtk_widget_set_allocation (widget, allocation);
2559 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2560 get_widget_padding_and_border (widget, &padding);
2562 allocation->x += padding.left;
2563 allocation->y += padding.top;
2564 allocation->width -= padding.left + padding.right;
2565 allocation->height -= padding.top + padding.bottom;
2567 if (!priv->tree_view)
2569 if (priv->cell_view)
2571 GtkBorder button_padding;
2575 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2576 get_widget_padding_and_border (priv->button, &button_padding);
2578 /* menu mode; child_widget is priv->cell_view.
2579 * Allocate the button to the full combobox allocation (minus the
2582 gtk_widget_size_allocate (priv->button, allocation);
2584 child.x = allocation->x;
2585 child.y = allocation->y;
2586 width = allocation->width;
2587 child.height = allocation->height;
2589 if (!priv->is_cell_renderer)
2591 /* restrict allocation of the child into the button box
2592 * if we're not in cell renderer mode.
2594 child.x += border_width + button_padding.left;
2595 child.y += border_width + button_padding.top;
2596 width -= 2 * border_width +
2597 button_padding.left + button_padding.right;
2598 child.height -= 2 * border_width +
2599 button_padding.top + button_padding.bottom;
2602 /* allocate the box containing the separator and the arrow */
2603 gtk_widget_get_preferred_size (priv->box, &req, NULL);
2604 child.width = req.width;
2606 child.x += width - req.width;
2607 child.width = MAX (1, child.width);
2608 child.height = MAX (1, child.height);
2609 gtk_widget_size_allocate (priv->box, &child);
2613 child.x += req.width;
2614 child.width = allocation->x + allocation->width
2615 - border_width - child.x - button_padding.right;
2619 child.width = child.x;
2620 child.x = allocation->x
2621 + border_width + button_padding.left;
2622 child.width -= child.x;
2625 if (gtk_widget_get_visible (priv->popup_widget))
2627 gint width, menu_width;
2629 if (priv->wrap_width == 0)
2631 GtkAllocation combo_box_allocation;
2633 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2634 width = combo_box_allocation.width;
2635 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2637 if (combo_box->priv->popup_fixed_width)
2638 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2640 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2642 gtk_widget_set_size_request (priv->popup_widget,
2643 MAX (width, menu_width), -1);
2646 /* reposition the menu after giving it a new width */
2647 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2650 child.width = MAX (1, child.width);
2651 child.height = MAX (1, child.height);
2652 gtk_widget_size_allocate (child_widget, &child);
2656 /* menu mode; child_widget has been set with gtk_container_add().
2657 * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2658 * Allocate the button at the far end, according to the direction
2661 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2663 /* After the macro, button_allocation has the button allocation rect */
2666 child.x = button_allocation.x + button_allocation.width;
2668 child.x = allocation->x;
2670 child.y = allocation->y;
2671 child.width = allocation->width - button_allocation.width;
2672 child.height = button_allocation.height;
2674 child.width = MAX (1, child.width);
2676 gtk_widget_size_allocate (child_widget, &child);
2681 /* list mode; child_widget might be either priv->cell_view or a child
2682 * added with gtk_container_add().
2685 /* After the macro, button_allocation has the button allocation rect */
2686 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2689 child.x = button_allocation.x + button_allocation.width;
2691 child.x = allocation->x;
2693 child.y = allocation->y;
2694 child.width = allocation->width - button_allocation.width;
2695 child.height = button_allocation.height;
2697 if (priv->cell_view_frame)
2699 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2701 /* restrict allocation of the child into the frame box if it's present */
2702 if (priv->has_frame)
2704 GtkBorder frame_padding;
2707 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2708 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2710 child.x += border_width + frame_padding.left;
2711 child.y += border_width + frame_padding.right;
2712 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2713 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2717 if (gtk_widget_get_visible (priv->popup_window))
2719 gint x, y, width, height;
2720 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2721 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2722 gtk_widget_set_size_request (priv->popup_window, width, height);
2725 /* allocate the child */
2726 child.width = MAX (1, child.width);
2727 child.height = MAX (1, child.height);
2728 gtk_widget_size_allocate (child_widget, &child);
2732 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2735 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2737 GtkComboBoxPrivate *priv = combo_box->priv;
2741 g_signal_handler_disconnect (priv->model,
2743 g_signal_handler_disconnect (priv->model,
2745 g_signal_handler_disconnect (priv->model,
2746 priv->reordered_id);
2747 g_signal_handler_disconnect (priv->model,
2753 g_object_unref (priv->model);
2757 if (priv->active_row)
2759 gtk_tree_row_reference_free (priv->active_row);
2760 priv->active_row = NULL;
2763 if (priv->cell_view)
2764 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2768 gtk_combo_box_forall (GtkContainer *container,
2769 gboolean include_internals,
2770 GtkCallback callback,
2771 gpointer callback_data)
2773 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2774 GtkComboBoxPrivate *priv = combo_box->priv;
2777 if (include_internals)
2780 (* callback) (priv->button, callback_data);
2781 if (priv->cell_view_frame)
2782 (* callback) (priv->cell_view_frame, callback_data);
2785 child = gtk_bin_get_child (GTK_BIN (container));
2787 (* callback) (child, callback_data);
2791 gtk_combo_box_child_show (GtkWidget *widget,
2792 GtkComboBox *combo_box)
2794 GtkComboBoxPrivate *priv = combo_box->priv;
2796 priv->popup_shown = TRUE;
2797 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2801 gtk_combo_box_child_hide (GtkWidget *widget,
2802 GtkComboBox *combo_box)
2804 GtkComboBoxPrivate *priv = combo_box->priv;
2806 priv->popup_shown = FALSE;
2807 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2811 gtk_combo_box_draw (GtkWidget *widget,
2814 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2815 GtkComboBoxPrivate *priv = combo_box->priv;
2817 if (priv->shadow_type != GTK_SHADOW_NONE)
2819 GtkStyleContext *context;
2821 context = gtk_widget_get_style_context (widget);
2823 gtk_render_background (context, cr, 0, 0,
2824 gtk_widget_get_allocated_width (widget),
2825 gtk_widget_get_allocated_height (widget));
2826 gtk_render_frame (context, cr, 0, 0,
2827 gtk_widget_get_allocated_width (widget),
2828 gtk_widget_get_allocated_height (widget));
2831 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2834 if (priv->tree_view && priv->cell_view_frame)
2836 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2837 priv->cell_view_frame, cr);
2840 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2841 gtk_bin_get_child (GTK_BIN (widget)),
2857 path_visible (GtkTreeView *view,
2863 /* Note that we rely on the fact that collapsed rows don't have nodes
2865 return _gtk_tree_view_find_node (view, path, &tree, &node);
2869 tree_next_func (GtkTreeModel *model,
2874 SearchData *search_data = (SearchData *)data;
2876 if (search_data->found)
2878 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2881 if (search_data->visible &&
2882 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2885 search_data->set = TRUE;
2886 search_data->iter = *iter;
2891 if (gtk_tree_path_compare (path, search_data->path) == 0)
2892 search_data->found = TRUE;
2898 tree_next (GtkComboBox *combo,
2899 GtkTreeModel *model,
2904 SearchData search_data;
2906 search_data.combo = combo;
2907 search_data.path = gtk_tree_model_get_path (model, iter);
2908 search_data.visible = visible;
2909 search_data.found = FALSE;
2910 search_data.set = FALSE;
2912 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2914 *next = search_data.iter;
2916 gtk_tree_path_free (search_data.path);
2918 return search_data.set;
2922 tree_prev_func (GtkTreeModel *model,
2927 SearchData *search_data = (SearchData *)data;
2929 if (gtk_tree_path_compare (path, search_data->path) == 0)
2931 search_data->found = TRUE;
2935 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2938 if (search_data->visible &&
2939 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2942 search_data->set = TRUE;
2943 search_data->iter = *iter;
2949 tree_prev (GtkComboBox *combo,
2950 GtkTreeModel *model,
2955 SearchData search_data;
2957 search_data.combo = combo;
2958 search_data.path = gtk_tree_model_get_path (model, iter);
2959 search_data.visible = visible;
2960 search_data.found = FALSE;
2961 search_data.set = FALSE;
2963 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2965 *prev = search_data.iter;
2967 gtk_tree_path_free (search_data.path);
2969 return search_data.set;
2973 tree_last_func (GtkTreeModel *model,
2978 SearchData *search_data = (SearchData *)data;
2980 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2983 /* Note that we rely on the fact that collapsed rows don't have nodes
2985 if (search_data->visible &&
2986 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2989 search_data->set = TRUE;
2990 search_data->iter = *iter;
2996 tree_last (GtkComboBox *combo,
2997 GtkTreeModel *model,
3001 SearchData search_data;
3003 search_data.combo = combo;
3004 search_data.visible = visible;
3005 search_data.set = FALSE;
3007 gtk_tree_model_foreach (model, tree_last_func, &search_data);
3009 *last = search_data.iter;
3011 return search_data.set;
3016 tree_first_func (GtkTreeModel *model,
3021 SearchData *search_data = (SearchData *)data;
3023 if (!tree_column_row_is_sensitive (search_data->combo, iter))
3026 if (search_data->visible &&
3027 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3030 search_data->set = TRUE;
3031 search_data->iter = *iter;
3037 tree_first (GtkComboBox *combo,
3038 GtkTreeModel *model,
3042 SearchData search_data;
3044 search_data.combo = combo;
3045 search_data.visible = visible;
3046 search_data.set = FALSE;
3048 gtk_tree_model_foreach (model, tree_first_func, &search_data);
3050 *first = search_data.iter;
3052 return search_data.set;
3056 gtk_combo_box_scroll_event (GtkWidget *widget,
3057 GdkEventScroll *event)
3059 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3062 GtkTreeIter new_iter;
3064 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3067 if (event->direction == GDK_SCROLL_UP)
3068 found = tree_prev (combo_box, combo_box->priv->model,
3069 &iter, &new_iter, FALSE);
3071 found = tree_next (combo_box, combo_box->priv->model,
3072 &iter, &new_iter, FALSE);
3075 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3084 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3088 GtkComboBoxPrivate *priv = combo->priv;
3090 if (priv->row_separator_func)
3091 return priv->row_separator_func (model, iter, priv->row_separator_data);
3097 gtk_combo_box_header_func (GtkTreeModel *model,
3101 /* Every submenu has a selectable header, however we
3102 * can expose a method to make that configurable by
3103 * the user (like row_separator_func is done) */
3108 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3109 gboolean add_children)
3111 GtkComboBoxPrivate *priv = combo_box->priv;
3115 child = gtk_bin_get_child (GTK_BIN (combo_box));
3117 if (priv->cell_view)
3119 priv->button = gtk_toggle_button_new ();
3120 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3121 priv->focus_on_click);
3123 g_signal_connect (priv->button, "toggled",
3124 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3125 gtk_widget_set_parent (priv->button,
3126 gtk_widget_get_parent (child));
3128 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3129 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3131 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3132 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3134 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3135 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3136 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3138 gtk_widget_show_all (priv->button);
3142 priv->button = gtk_toggle_button_new ();
3143 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3144 priv->focus_on_click);
3146 g_signal_connect (priv->button, "toggled",
3147 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3148 gtk_widget_set_parent (priv->button,
3149 gtk_widget_get_parent (child));
3151 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3152 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3153 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3154 gtk_widget_show_all (priv->button);
3157 g_signal_connect (priv->button, "button-press-event",
3158 G_CALLBACK (gtk_combo_box_menu_button_press),
3160 g_signal_connect (priv->button, "state-flags-changed",
3161 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3164 /* create our funky menu */
3165 menu = _gtk_tree_menu_new_with_area (priv->area);
3166 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3168 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3170 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3171 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3172 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3173 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3174 combo_box->priv->add_tearoffs);
3176 g_signal_connect (menu, "menu-activate",
3177 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3179 /* Chain our row_separator_func through */
3180 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3181 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3184 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3185 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3188 g_signal_connect (menu, "key-press-event",
3189 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3190 gtk_combo_box_set_popup_widget (combo_box, menu);
3192 gtk_combo_box_update_title (combo_box);
3196 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3198 GtkComboBoxPrivate *priv = combo_box->priv;
3200 g_signal_handlers_disconnect_matched (priv->button,
3201 G_SIGNAL_MATCH_DATA,
3203 gtk_combo_box_menu_button_press, NULL);
3204 g_signal_handlers_disconnect_matched (priv->button,
3205 G_SIGNAL_MATCH_DATA,
3207 gtk_combo_box_button_state_flags_changed, combo_box);
3208 g_signal_handlers_disconnect_matched (priv->popup_widget,
3209 G_SIGNAL_MATCH_DATA,
3211 gtk_combo_box_menu_activate, combo_box);
3213 /* unparent will remove our latest ref */
3214 gtk_widget_unparent (priv->button);
3217 priv->button = NULL;
3219 priv->separator = NULL;
3221 /* changing the popup window will unref the menu and the children */
3226 gtk_combo_box_menu_button_press (GtkWidget *widget,
3227 GdkEventButton *event,
3230 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3231 GtkComboBoxPrivate *priv = combo_box->priv;
3233 if (GTK_IS_MENU (priv->popup_widget) &&
3234 event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3236 if (priv->focus_on_click &&
3237 !gtk_widget_has_focus (priv->button))
3238 gtk_widget_grab_focus (priv->button);
3240 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3249 gtk_combo_box_menu_activate (GtkWidget *menu,
3251 GtkComboBox *combo_box)
3255 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3256 gtk_combo_box_set_active_iter (combo_box, &iter);
3258 g_object_set (combo_box,
3259 "editing-canceled", FALSE,
3265 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3268 gboolean sensitive = TRUE; /* fool code checkers */
3270 if (!combo_box->priv->button)
3273 switch (combo_box->priv->button_sensitivity)
3275 case GTK_SENSITIVITY_ON:
3278 case GTK_SENSITIVITY_OFF:
3281 case GTK_SENSITIVITY_AUTO:
3282 sensitive = combo_box->priv->model &&
3283 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3286 g_assert_not_reached ();
3290 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3292 /* In list-mode, we also need to update sensitivity of the event box */
3293 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3294 && combo_box->priv->cell_view)
3295 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3299 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3304 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3306 if (combo_box->priv->tree_view)
3307 gtk_combo_box_list_popup_resize (combo_box);
3309 gtk_combo_box_update_sensitivity (combo_box);
3313 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3317 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3318 GtkComboBoxPrivate *priv = combo_box->priv;
3320 if (!gtk_tree_row_reference_valid (priv->active_row))
3322 if (priv->cell_view)
3323 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3324 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3327 if (priv->tree_view)
3328 gtk_combo_box_list_popup_resize (combo_box);
3330 gtk_combo_box_update_sensitivity (combo_box);
3334 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3340 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3344 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3349 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3350 GtkComboBoxPrivate *priv = combo_box->priv;
3351 GtkTreePath *active_path;
3353 /* FIXME this belongs to GtkCellView */
3354 if (gtk_tree_row_reference_valid (priv->active_row))
3356 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3357 if (gtk_tree_path_compare (path, active_path) == 0 &&
3359 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3360 gtk_tree_path_free (active_path);
3363 if (priv->tree_view)
3364 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3368 list_popup_resize_idle (gpointer user_data)
3370 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3371 GtkComboBoxPrivate *priv = combo_box->priv;
3372 gint x, y, width, height;
3374 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3376 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3378 gtk_widget_set_size_request (priv->popup_window, width, height);
3379 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3382 priv->resize_idle_id = 0;
3388 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3390 GtkComboBoxPrivate *priv = combo_box->priv;
3392 if (!priv->resize_idle_id)
3393 priv->resize_idle_id =
3394 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3398 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3403 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3405 gtk_combo_box_list_popup_resize (combo_box);
3414 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3416 GtkComboBoxPrivate *priv = combo_box->priv;
3417 GtkTreeSelection *sel;
3419 GtkWidget *widget = GTK_WIDGET (combo_box);
3421 priv->button = gtk_toggle_button_new ();
3422 child = gtk_bin_get_child (GTK_BIN (combo_box));
3423 gtk_widget_set_parent (priv->button,
3424 gtk_widget_get_parent (child));
3425 g_signal_connect (priv->button, "button-press-event",
3426 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3427 g_signal_connect (priv->button, "toggled",
3428 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3430 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3431 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3432 priv->separator = NULL;
3433 gtk_widget_show_all (priv->button);
3435 if (priv->cell_view)
3437 GtkStyleContext *context;
3438 GtkStateFlags state;
3441 context = gtk_widget_get_style_context (widget);
3442 state = gtk_widget_get_state_flags (widget);
3443 gtk_style_context_get_background_color (context, state, &color);
3445 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3447 priv->box = gtk_event_box_new ();
3448 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3451 if (priv->has_frame)
3453 priv->cell_view_frame = gtk_frame_new (NULL);
3454 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3459 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3460 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3464 gtk_widget_set_parent (priv->cell_view_frame,
3465 gtk_widget_get_parent (child));
3466 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3467 gtk_widget_show_all (priv->cell_view_frame);
3469 g_signal_connect (priv->box, "button-press-event",
3470 G_CALLBACK (gtk_combo_box_list_button_pressed),
3474 priv->tree_view = gtk_tree_view_new ();
3475 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3476 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3477 gtk_tree_selection_set_select_function (sel,
3478 gtk_combo_box_list_select_func,
3480 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3482 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3485 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3486 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3490 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3492 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3493 gtk_tree_view_column_new_with_area (priv->area));
3495 if (gtk_tree_row_reference_valid (priv->active_row))
3499 path = gtk_tree_row_reference_get_path (priv->active_row);
3500 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3502 gtk_tree_path_free (path);
3505 /* set sample/popup widgets */
3506 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3508 g_signal_connect (priv->tree_view, "key-press-event",
3509 G_CALLBACK (gtk_combo_box_list_key_press),
3511 g_signal_connect (priv->tree_view, "enter-notify-event",
3512 G_CALLBACK (gtk_combo_box_list_enter_notify),
3514 g_signal_connect (priv->tree_view, "row-expanded",
3515 G_CALLBACK (gtk_combo_box_model_row_expanded),
3517 g_signal_connect (priv->tree_view, "row-collapsed",
3518 G_CALLBACK (gtk_combo_box_model_row_expanded),
3520 g_signal_connect (priv->popup_window, "button-press-event",
3521 G_CALLBACK (gtk_combo_box_list_button_pressed),
3523 g_signal_connect (priv->popup_window, "button-release-event",
3524 G_CALLBACK (gtk_combo_box_list_button_released),
3527 gtk_widget_show (priv->tree_view);
3529 gtk_combo_box_update_sensitivity (combo_box);
3533 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3535 GtkComboBoxPrivate *priv = combo_box->priv;
3537 /* disconnect signals */
3538 g_signal_handlers_disconnect_matched (priv->tree_view,
3539 G_SIGNAL_MATCH_DATA,
3540 0, 0, NULL, NULL, combo_box);
3541 g_signal_handlers_disconnect_matched (priv->button,
3542 G_SIGNAL_MATCH_DATA,
3544 gtk_combo_box_list_button_pressed,
3546 g_signal_handlers_disconnect_matched (priv->popup_window,
3547 G_SIGNAL_MATCH_DATA,
3549 gtk_combo_box_list_button_pressed,
3551 g_signal_handlers_disconnect_matched (priv->popup_window,
3552 G_SIGNAL_MATCH_DATA,
3554 gtk_combo_box_list_button_released,
3557 g_signal_handlers_disconnect_matched (priv->popup_window,
3558 G_SIGNAL_MATCH_DATA,
3560 gtk_combo_box_child_show,
3563 g_signal_handlers_disconnect_matched (priv->popup_window,
3564 G_SIGNAL_MATCH_DATA,
3566 gtk_combo_box_child_hide,
3570 g_signal_handlers_disconnect_matched (priv->box,
3571 G_SIGNAL_MATCH_DATA,
3573 gtk_combo_box_list_button_pressed,
3576 /* destroy things (unparent will kill the latest ref from us)
3577 * last unref on button will destroy the arrow
3579 gtk_widget_unparent (priv->button);
3580 priv->button = NULL;
3583 if (priv->cell_view)
3585 g_object_set (priv->cell_view,
3586 "background-set", FALSE,
3590 if (priv->cell_view_frame)
3592 gtk_widget_unparent (priv->cell_view_frame);
3593 priv->cell_view_frame = NULL;
3597 if (priv->scroll_timer)
3599 g_source_remove (priv->scroll_timer);
3600 priv->scroll_timer = 0;
3603 if (priv->resize_idle_id)
3605 g_source_remove (priv->resize_idle_id);
3606 priv->resize_idle_id = 0;
3609 gtk_widget_destroy (priv->tree_view);
3611 priv->tree_view = NULL;
3612 if (priv->popup_widget)
3614 g_object_unref (priv->popup_widget);
3615 priv->popup_widget = NULL;
3622 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3623 GdkEventButton *event,
3626 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3627 GtkComboBoxPrivate *priv = combo_box->priv;
3629 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3631 if (ewidget == priv->popup_window)
3634 if ((ewidget != priv->button && ewidget != priv->box) ||
3635 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3638 if (priv->focus_on_click &&
3639 !gtk_widget_has_focus (priv->button))
3640 gtk_widget_grab_focus (priv->button);
3642 gtk_combo_box_popup_for_device (combo_box, event->device);
3644 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3646 priv->auto_scroll = FALSE;
3647 if (priv->scroll_timer == 0)
3648 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3649 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3652 priv->popup_in_progress = TRUE;
3658 gtk_combo_box_list_button_released (GtkWidget *widget,
3659 GdkEventButton *event,
3663 GtkTreePath *path = NULL;
3666 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3667 GtkComboBoxPrivate *priv = combo_box->priv;
3669 gboolean popup_in_progress = FALSE;
3671 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3673 if (priv->popup_in_progress)
3675 popup_in_progress = TRUE;
3676 priv->popup_in_progress = FALSE;
3679 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3681 if (priv->scroll_timer)
3683 g_source_remove (priv->scroll_timer);
3684 priv->scroll_timer = 0;
3687 if (ewidget != priv->tree_view)
3689 if ((ewidget == priv->button ||
3690 ewidget == priv->box) &&
3691 !popup_in_progress &&
3692 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3694 gtk_combo_box_popdown (combo_box);
3698 /* released outside treeview */
3699 if (ewidget != priv->button && ewidget != priv->box)
3701 gtk_combo_box_popdown (combo_box);
3709 /* select something cool */
3710 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3716 return TRUE; /* clicked outside window? */
3718 gtk_tree_model_get_iter (priv->model, &iter, path);
3719 gtk_tree_path_free (path);
3721 gtk_combo_box_popdown (combo_box);
3723 if (tree_column_row_is_sensitive (combo_box, &iter))
3724 gtk_combo_box_set_active_iter (combo_box, &iter);
3730 gtk_combo_box_menu_key_press (GtkWidget *widget,
3734 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3736 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3738 /* The menu hasn't managed the
3739 * event, forward it to the combobox
3741 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3748 gtk_combo_box_list_key_press (GtkWidget *widget,
3752 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3755 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3756 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3758 GtkTreeModel *model = NULL;
3760 gtk_combo_box_popdown (combo_box);
3762 if (combo_box->priv->model)
3764 GtkTreeSelection *sel;
3766 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3768 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3769 gtk_combo_box_set_active_iter (combo_box, &iter);
3775 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3777 /* The list hasn't managed the
3778 * event, forward it to the combobox
3780 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3787 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3792 GtkAllocation allocation;
3793 GtkWidget *tree_view = combo_box->priv->tree_view;
3796 gtk_widget_get_allocation (tree_view, &allocation);
3798 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3799 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3801 if (x <= allocation.x &&
3802 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3804 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3805 gtk_adjustment_set_value (adj, value);
3807 else if (x >= allocation.x + allocation.width &&
3808 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3810 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3811 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3815 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3816 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3818 if (y <= allocation.y &&
3819 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3821 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3822 gtk_adjustment_set_value (adj, value);
3824 else if (y >= allocation.height &&
3825 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3827 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3828 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3834 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3836 GtkComboBoxPrivate *priv = combo_box->priv;
3839 if (priv->auto_scroll)
3841 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3844 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3851 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3852 GdkEventCrossing *event,
3855 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3857 combo_box->priv->auto_scroll = TRUE;
3863 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3864 GtkTreeModel *model,
3866 gboolean path_currently_selected,
3869 GList *list, *columns;
3870 gboolean sensitive = FALSE;
3872 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3874 for (list = columns; list && !sensitive; list = list->next)
3876 GList *cells, *cell;
3877 gboolean cell_sensitive, cell_visible;
3879 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3881 if (!gtk_tree_view_column_get_visible (column))
3884 gtk_tree_model_get_iter (model, &iter, path);
3885 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3888 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3891 g_object_get (cell->data,
3892 "sensitive", &cell_sensitive,
3893 "visible", &cell_visible,
3896 if (cell_visible && cell_sensitive)
3905 g_list_free (cells);
3908 g_list_free (columns);
3914 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3919 /* XXX Do nothing ? */
3923 * GtkCellLayout implementation
3925 static GtkCellArea *
3926 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3928 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3929 GtkComboBoxPrivate *priv = combo->priv;
3931 if (G_UNLIKELY (!priv->area))
3933 priv->area = gtk_cell_area_box_new ();
3934 g_object_ref_sink (priv->area);
3945 * gtk_combo_box_new:
3947 * Creates a new empty #GtkComboBox.
3949 * Return value: A new #GtkComboBox.
3954 gtk_combo_box_new (void)
3956 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3960 * gtk_combo_box_new_with_area:
3961 * @area: the #GtkCellArea to use to layout cell renderers
3963 * Creates a new empty #GtkComboBox using @area to layout cells.
3965 * Return value: A new #GtkComboBox.
3968 gtk_combo_box_new_with_area (GtkCellArea *area)
3970 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3974 * gtk_combo_box_new_with_area_and_entry:
3975 * @area: the #GtkCellArea to use to layout cell renderers
3977 * Creates a new empty #GtkComboBox with an entry.
3979 * The new combo box will use @area to layout cells.
3981 * Return value: A new #GtkComboBox.
3984 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3986 return g_object_new (GTK_TYPE_COMBO_BOX,
3994 * gtk_combo_box_new_with_entry:
3996 * Creates a new empty #GtkComboBox with an entry.
3998 * Return value: A new #GtkComboBox.
4001 gtk_combo_box_new_with_entry (void)
4003 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
4007 * gtk_combo_box_new_with_model:
4008 * @model: A #GtkTreeModel.
4010 * Creates a new #GtkComboBox with the model initialized to @model.
4012 * Return value: A new #GtkComboBox.
4017 gtk_combo_box_new_with_model (GtkTreeModel *model)
4019 GtkComboBox *combo_box;
4021 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4023 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4025 return GTK_WIDGET (combo_box);
4029 * gtk_combo_box_new_with_model_and_entry:
4030 * @model: A #GtkTreeModel
4032 * Creates a new empty #GtkComboBox with an entry
4033 * and with the model initialized to @model.
4035 * Return value: A new #GtkComboBox
4038 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4040 return g_object_new (GTK_TYPE_COMBO_BOX,
4047 * gtk_combo_box_get_wrap_width:
4048 * @combo_box: A #GtkComboBox
4050 * Returns the wrap width which is used to determine the number of columns
4051 * for the popup menu. If the wrap width is larger than 1, the combo box
4054 * Returns: the wrap width.
4059 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4061 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4063 return combo_box->priv->wrap_width;
4067 * gtk_combo_box_set_wrap_width:
4068 * @combo_box: A #GtkComboBox
4069 * @width: Preferred number of columns
4071 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4072 * the preferred number of columns when you want the popup to be layed out
4078 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4081 GtkComboBoxPrivate *priv;
4083 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4084 g_return_if_fail (width >= 0);
4086 priv = combo_box->priv;
4088 if (width != priv->wrap_width)
4090 priv->wrap_width = width;
4092 gtk_combo_box_check_appearance (combo_box);
4094 if (GTK_IS_TREE_MENU (priv->popup_widget))
4095 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4097 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4102 * gtk_combo_box_get_row_span_column:
4103 * @combo_box: A #GtkComboBox
4105 * Returns the column with row span information for @combo_box.
4107 * Returns: the row span column.
4112 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4114 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4116 return combo_box->priv->row_column;
4120 * gtk_combo_box_set_row_span_column:
4121 * @combo_box: A #GtkComboBox.
4122 * @row_span: A column in the model passed during construction.
4124 * Sets the column with row span information for @combo_box to be @row_span.
4125 * The row span column contains integers which indicate how many rows
4126 * an item should span.
4131 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4134 GtkComboBoxPrivate *priv;
4137 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4139 priv = combo_box->priv;
4141 col = gtk_tree_model_get_n_columns (priv->model);
4142 g_return_if_fail (row_span >= -1 && row_span < col);
4144 if (row_span != priv->row_column)
4146 priv->row_column = row_span;
4148 if (GTK_IS_TREE_MENU (priv->popup_widget))
4149 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4151 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4156 * gtk_combo_box_get_column_span_column:
4157 * @combo_box: A #GtkComboBox
4159 * Returns the column with column span information for @combo_box.
4161 * Returns: the column span column.
4166 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4168 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4170 return combo_box->priv->col_column;
4174 * gtk_combo_box_set_column_span_column:
4175 * @combo_box: A #GtkComboBox
4176 * @column_span: A column in the model passed during construction
4178 * Sets the column with column span information for @combo_box to be
4179 * @column_span. The column span column contains integers which indicate
4180 * how many columns an item should span.
4185 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4188 GtkComboBoxPrivate *priv;
4191 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4193 priv = combo_box->priv;
4195 col = gtk_tree_model_get_n_columns (priv->model);
4196 g_return_if_fail (column_span >= -1 && column_span < col);
4198 if (column_span != priv->col_column)
4200 priv->col_column = column_span;
4202 if (GTK_IS_TREE_MENU (priv->popup_widget))
4203 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4205 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4210 * gtk_combo_box_get_active:
4211 * @combo_box: A #GtkComboBox
4213 * Returns the index of the currently active item, or -1 if there's no
4214 * active item. If the model is a non-flat treemodel, and the active item
4215 * is not an immediate child of the root of the tree, this function returns
4216 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4217 * <literal>path</literal> is the #GtkTreePath of the active item.
4219 * Return value: An integer which is the index of the currently active item,
4220 * or -1 if there's no active item.
4225 gtk_combo_box_get_active (GtkComboBox *combo_box)
4227 GtkComboBoxPrivate *priv;
4230 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4232 priv = combo_box->priv;
4234 if (gtk_tree_row_reference_valid (priv->active_row))
4238 path = gtk_tree_row_reference_get_path (priv->active_row);
4239 result = gtk_tree_path_get_indices (path)[0];
4240 gtk_tree_path_free (path);
4249 * gtk_combo_box_set_active:
4250 * @combo_box: A #GtkComboBox
4251 * @index_: An index in the model passed during construction, or -1 to have
4254 * Sets the active item of @combo_box to be the item at @index.
4259 gtk_combo_box_set_active (GtkComboBox *combo_box,
4262 GtkTreePath *path = NULL;
4263 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4264 g_return_if_fail (index_ >= -1);
4266 if (combo_box->priv->model == NULL)
4268 /* Save index, in case the model is set after the index */
4269 combo_box->priv->active = index_;
4275 path = gtk_tree_path_new_from_indices (index_, -1);
4277 gtk_combo_box_set_active_internal (combo_box, path);
4280 gtk_tree_path_free (path);
4284 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4287 GtkComboBoxPrivate *priv = combo_box->priv;
4288 GtkTreePath *active_path;
4291 /* Remember whether the initially active row is valid. */
4292 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4294 if (path && is_valid_row_reference)
4296 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4297 path_cmp = gtk_tree_path_compare (path, active_path);
4298 gtk_tree_path_free (active_path);
4303 if (priv->active_row)
4305 gtk_tree_row_reference_free (priv->active_row);
4306 priv->active_row = NULL;
4311 if (priv->tree_view)
4312 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4315 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4317 if (GTK_IS_MENU (menu))
4318 gtk_menu_set_active (menu, -1);
4321 if (priv->cell_view)
4322 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4325 * Do not emit a "changed" signal when an already invalid selection was
4326 * now set to invalid.
4328 if (!is_valid_row_reference)
4334 gtk_tree_row_reference_new (priv->model, path);
4336 if (priv->tree_view)
4338 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4341 else if (GTK_IS_MENU (priv->popup_widget))
4343 /* FIXME handle nested menus better */
4344 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4345 gtk_tree_path_get_indices (path)[0]);
4348 if (priv->cell_view)
4349 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4353 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4354 g_object_notify (G_OBJECT (combo_box), "active");
4355 if (combo_box->priv->id_column >= 0)
4356 g_object_notify (G_OBJECT (combo_box), "active-id");
4361 * gtk_combo_box_get_active_iter:
4362 * @combo_box: A #GtkComboBox
4363 * @iter: (out): The uninitialized #GtkTreeIter
4365 * Sets @iter to point to the current active item, if it exists.
4367 * Return value: %TRUE, if @iter was set
4372 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4378 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4380 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4383 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4384 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4385 gtk_tree_path_free (path);
4391 * gtk_combo_box_set_active_iter:
4392 * @combo_box: A #GtkComboBox
4393 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4395 * Sets the current active item to be the one referenced by @iter, or
4396 * unsets the active item if @iter is %NULL.
4401 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4404 GtkTreePath *path = NULL;
4406 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4409 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4411 gtk_combo_box_set_active_internal (combo_box, path);
4412 gtk_tree_path_free (path);
4416 * gtk_combo_box_set_model:
4417 * @combo_box: A #GtkComboBox
4418 * @model: (allow-none): A #GtkTreeModel
4420 * Sets the model used by @combo_box to be @model. Will unset a previously set
4421 * model (if applicable). If model is %NULL, then it will unset the model.
4423 * Note that this function does not clear the cell renderers, you have to
4424 * call gtk_cell_layout_clear() yourself if you need to set up different
4425 * cell renderers for the new model.
4430 gtk_combo_box_set_model (GtkComboBox *combo_box,
4431 GtkTreeModel *model)
4433 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4434 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4436 if (model == combo_box->priv->model)
4439 gtk_combo_box_unset_model (combo_box);
4444 combo_box->priv->model = model;
4445 g_object_ref (combo_box->priv->model);
4447 combo_box->priv->inserted_id =
4448 g_signal_connect (combo_box->priv->model, "row-inserted",
4449 G_CALLBACK (gtk_combo_box_model_row_inserted),
4451 combo_box->priv->deleted_id =
4452 g_signal_connect (combo_box->priv->model, "row-deleted",
4453 G_CALLBACK (gtk_combo_box_model_row_deleted),
4455 combo_box->priv->reordered_id =
4456 g_signal_connect (combo_box->priv->model, "rows-reordered",
4457 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4459 combo_box->priv->changed_id =
4460 g_signal_connect (combo_box->priv->model, "row-changed",
4461 G_CALLBACK (gtk_combo_box_model_row_changed),
4464 if (combo_box->priv->tree_view)
4467 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4468 combo_box->priv->model);
4469 gtk_combo_box_list_popup_resize (combo_box);
4472 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4475 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4476 combo_box->priv->model);
4479 if (combo_box->priv->cell_view)
4480 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4481 combo_box->priv->model);
4483 if (combo_box->priv->active != -1)
4485 /* If an index was set in advance, apply it now */
4486 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4487 combo_box->priv->active = -1;
4491 gtk_combo_box_update_sensitivity (combo_box);
4493 g_object_notify (G_OBJECT (combo_box), "model");
4497 * gtk_combo_box_get_model:
4498 * @combo_box: A #GtkComboBox
4500 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4502 * Return value: (transfer none): A #GtkTreeModel which was passed
4503 * during construction.
4508 gtk_combo_box_get_model (GtkComboBox *combo_box)
4510 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4512 return combo_box->priv->model;
4516 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4517 GtkScrollType scroll)
4520 GtkTreeIter new_iter;
4521 gboolean active_iter;
4524 if (!combo_box->priv->model)
4526 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4530 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4534 case GTK_SCROLL_STEP_BACKWARD:
4535 case GTK_SCROLL_STEP_UP:
4536 case GTK_SCROLL_STEP_LEFT:
4539 found = tree_prev (combo_box, combo_box->priv->model,
4540 &iter, &new_iter, FALSE);
4543 /* else fall through */
4545 case GTK_SCROLL_PAGE_FORWARD:
4546 case GTK_SCROLL_PAGE_DOWN:
4547 case GTK_SCROLL_PAGE_RIGHT:
4548 case GTK_SCROLL_END:
4549 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4552 case GTK_SCROLL_STEP_FORWARD:
4553 case GTK_SCROLL_STEP_DOWN:
4554 case GTK_SCROLL_STEP_RIGHT:
4557 found = tree_next (combo_box, combo_box->priv->model,
4558 &iter, &new_iter, FALSE);
4561 /* else fall through */
4563 case GTK_SCROLL_PAGE_BACKWARD:
4564 case GTK_SCROLL_PAGE_UP:
4565 case GTK_SCROLL_PAGE_LEFT:
4566 case GTK_SCROLL_START:
4567 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4574 if (found && active_iter)
4576 GtkTreePath *old_path;
4577 GtkTreePath *new_path;
4579 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4580 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4582 if (gtk_tree_path_compare (old_path, new_path) == 0)
4585 gtk_tree_path_free (old_path);
4586 gtk_tree_path_free (new_path);
4591 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4595 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4600 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4601 gboolean group_cycling)
4603 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4605 if (combo_box->priv->has_entry)
4609 child = gtk_bin_get_child (GTK_BIN (combo_box));
4611 gtk_widget_grab_focus (child);
4614 gtk_widget_grab_focus (combo_box->priv->button);
4620 gtk_combo_box_grab_focus (GtkWidget *widget)
4622 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4624 if (combo_box->priv->has_entry)
4628 child = gtk_bin_get_child (GTK_BIN (combo_box));
4630 gtk_widget_grab_focus (child);
4633 gtk_widget_grab_focus (combo_box->priv->button);
4637 gtk_combo_box_destroy (GtkWidget *widget)
4639 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4641 if (combo_box->priv->popup_idle_id > 0)
4643 g_source_remove (combo_box->priv->popup_idle_id);
4644 combo_box->priv->popup_idle_id = 0;
4647 gtk_combo_box_popdown (combo_box);
4649 if (combo_box->priv->row_separator_destroy)
4650 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4652 combo_box->priv->row_separator_func = NULL;
4653 combo_box->priv->row_separator_data = NULL;
4654 combo_box->priv->row_separator_destroy = NULL;
4656 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4657 combo_box->priv->cell_view = NULL;
4661 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4664 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4667 * Fixes regression reported in bug #574059. The old functionality relied on
4668 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4669 * when the selection was already set to -1.
4671 if (gtk_combo_box_get_active(combo_box) == -1)
4672 g_signal_emit_by_name (combo_box, "changed");
4674 gtk_combo_box_set_active (combo_box, -1);
4678 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4681 GtkTreeModel *model;
4684 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4686 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4694 model = gtk_combo_box_get_model (combo_box);
4695 path = gtk_tree_model_get_path (model, &iter);
4696 path_str = gtk_tree_path_to_string (path);
4698 g_signal_handlers_block_by_func (entry,
4699 gtk_combo_box_entry_contents_changed,
4703 g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0,
4706 gtk_entry_set_text (entry, text);
4708 g_signal_handlers_unblock_by_func (entry,
4709 gtk_combo_box_entry_contents_changed,
4712 gtk_tree_path_free (path);
4720 gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
4723 GtkComboBoxPrivate *priv = combo_box->priv;
4724 GtkTreeModel *model;
4728 if (priv->text_column >= 0)
4730 model = gtk_combo_box_get_model (combo_box);
4731 gtk_tree_model_get_iter_from_string (model, &iter, path);
4733 gtk_tree_model_get (model, &iter,
4734 priv->text_column, &text,
4743 gtk_combo_box_constructor (GType type,
4744 guint n_construct_properties,
4745 GObjectConstructParam *construct_properties)
4748 GtkComboBox *combo_box;
4749 GtkComboBoxPrivate *priv;
4751 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4752 (type, n_construct_properties, construct_properties);
4754 combo_box = GTK_COMBO_BOX (object);
4755 priv = combo_box->priv;
4759 priv->area = gtk_cell_area_box_new ();
4760 g_object_ref_sink (priv->area);
4763 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4764 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4765 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4766 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4767 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4768 gtk_widget_show (priv->cell_view);
4770 gtk_combo_box_check_appearance (combo_box);
4772 if (priv->has_entry)
4775 GtkStyleContext *context;
4777 entry = gtk_entry_new ();
4778 gtk_widget_show (entry);
4779 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4781 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4782 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4784 priv->text_renderer = gtk_cell_renderer_text_new ();
4785 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4786 priv->text_renderer, TRUE);
4788 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4790 g_signal_connect (combo_box, "changed",
4791 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4799 gtk_combo_box_dispose(GObject* object)
4801 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4803 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4805 gtk_combo_box_menu_destroy (combo_box);
4806 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4807 combo_box->priv->popup_widget = NULL;
4810 if (combo_box->priv->area)
4812 g_object_unref (combo_box->priv->area);
4813 combo_box->priv->area = NULL;
4816 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4817 gtk_combo_box_list_destroy (combo_box);
4819 if (combo_box->priv->popup_window)
4821 gtk_widget_destroy (combo_box->priv->popup_window);
4822 combo_box->priv->popup_window = NULL;
4825 gtk_combo_box_unset_model (combo_box);
4827 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4831 gtk_combo_box_finalize (GObject *object)
4833 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4835 g_free (combo_box->priv->tearoff_title);
4837 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4841 gtk_cell_editable_key_press (GtkWidget *widget,
4845 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4847 if (event->keyval == GDK_KEY_Escape)
4849 g_object_set (combo_box,
4850 "editing-canceled", TRUE,
4852 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4853 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4857 else if (event->keyval == GDK_KEY_Return ||
4858 event->keyval == GDK_KEY_ISO_Enter ||
4859 event->keyval == GDK_KEY_KP_Enter)
4861 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4862 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4871 popdown_idle (gpointer data)
4873 GtkComboBox *combo_box;
4875 combo_box = GTK_COMBO_BOX (data);
4877 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4878 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4880 g_object_unref (combo_box);
4886 popdown_handler (GtkWidget *widget,
4889 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4893 popup_idle (gpointer data)
4895 GtkComboBox *combo_box;
4897 combo_box = GTK_COMBO_BOX (data);
4899 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4900 combo_box->priv->cell_view)
4901 g_signal_connect_object (combo_box->priv->popup_widget,
4902 "unmap", G_CALLBACK (popdown_handler),
4905 /* we unset this if a menu item is activated */
4906 g_object_set (combo_box,
4907 "editing-canceled", TRUE,
4909 gtk_combo_box_popup (combo_box);
4911 combo_box->priv->popup_idle_id = 0;
4912 combo_box->priv->activate_button = 0;
4913 combo_box->priv->activate_time = 0;
4919 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4922 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4925 combo_box->priv->is_cell_renderer = TRUE;
4927 if (combo_box->priv->cell_view)
4929 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4930 G_CALLBACK (gtk_cell_editable_key_press),
4933 gtk_widget_grab_focus (combo_box->priv->button);
4937 child = gtk_bin_get_child (GTK_BIN (combo_box));
4939 g_signal_connect_object (child, "key-press-event",
4940 G_CALLBACK (gtk_cell_editable_key_press),
4943 gtk_widget_grab_focus (child);
4944 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4947 /* we do the immediate popup only for the optionmenu-like
4950 if (combo_box->priv->is_cell_renderer &&
4951 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4953 if (event && event->type == GDK_BUTTON_PRESS)
4955 GdkEventButton *event_button = (GdkEventButton *)event;
4957 combo_box->priv->activate_button = event_button->button;
4958 combo_box->priv->activate_time = event_button->time;
4961 combo_box->priv->popup_idle_id =
4962 gdk_threads_add_idle (popup_idle, combo_box);
4968 * gtk_combo_box_get_add_tearoffs:
4969 * @combo_box: a #GtkComboBox
4971 * Gets the current value of the :add-tearoffs property.
4973 * Return value: the current value of the :add-tearoffs property.
4976 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4978 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4980 return combo_box->priv->add_tearoffs;
4984 * gtk_combo_box_set_add_tearoffs:
4985 * @combo_box: a #GtkComboBox
4986 * @add_tearoffs: %TRUE to add tearoff menu items
4988 * Sets whether the popup menu should have a tearoff
4994 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4995 gboolean add_tearoffs)
4997 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4999 add_tearoffs = add_tearoffs != FALSE;
5001 if (combo_box->priv->add_tearoffs != add_tearoffs)
5003 combo_box->priv->add_tearoffs = add_tearoffs;
5004 gtk_combo_box_check_appearance (combo_box);
5006 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5007 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
5008 combo_box->priv->add_tearoffs);
5010 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5015 * gtk_combo_box_get_title:
5016 * @combo_box: a #GtkComboBox
5018 * Gets the current title of the menu in tearoff mode. See
5019 * gtk_combo_box_set_add_tearoffs().
5021 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5022 * string which must not be freed.
5027 gtk_combo_box_get_title (GtkComboBox *combo_box)
5029 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5031 return combo_box->priv->tearoff_title;
5035 gtk_combo_box_update_title (GtkComboBox *combo_box)
5037 gtk_combo_box_check_appearance (combo_box);
5039 if (combo_box->priv->popup_widget &&
5040 GTK_IS_MENU (combo_box->priv->popup_widget))
5041 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5042 combo_box->priv->tearoff_title);
5046 * gtk_combo_box_set_title:
5047 * @combo_box: a #GtkComboBox
5048 * @title: a title for the menu in tearoff mode
5050 * Sets the menu's title in tearoff mode.
5055 gtk_combo_box_set_title (GtkComboBox *combo_box,
5058 GtkComboBoxPrivate *priv;
5060 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5062 priv = combo_box->priv;
5064 if (strcmp (title ? title : "",
5065 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5067 g_free (priv->tearoff_title);
5068 priv->tearoff_title = g_strdup (title);
5070 gtk_combo_box_update_title (combo_box);
5072 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5078 * gtk_combo_box_set_popup_fixed_width:
5079 * @combo_box: a #GtkComboBox
5080 * @fixed: whether to use a fixed popup width
5082 * Specifies whether the popup's width should be a fixed width
5083 * matching the allocated width of the combo box.
5088 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5091 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5093 if (combo_box->priv->popup_fixed_width != fixed)
5095 combo_box->priv->popup_fixed_width = fixed;
5097 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5102 * gtk_combo_box_get_popup_fixed_width:
5103 * @combo_box: a #GtkComboBox
5105 * Gets whether the popup uses a fixed width matching
5106 * the allocated width of the combo box.
5108 * Returns: %TRUE if the popup uses a fixed width
5113 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5115 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5117 return combo_box->priv->popup_fixed_width;
5122 * gtk_combo_box_get_popup_accessible:
5123 * @combo_box: a #GtkComboBox
5125 * Gets the accessible object corresponding to the combo box's popup.
5127 * This function is mostly intended for use by accessibility technologies;
5128 * applications should have little use for it.
5130 * Returns: (transfer none): the accessible object corresponding
5131 * to the combo box's popup.
5136 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5140 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5142 if (combo_box->priv->popup_widget)
5144 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5152 * gtk_combo_box_get_row_separator_func: (skip)
5153 * @combo_box: a #GtkComboBox
5155 * Returns the current row separator function.
5157 * Return value: the current row separator function.
5161 GtkTreeViewRowSeparatorFunc
5162 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5164 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5166 return combo_box->priv->row_separator_func;
5170 * gtk_combo_box_set_row_separator_func:
5171 * @combo_box: a #GtkComboBox
5172 * @func: a #GtkTreeViewRowSeparatorFunc
5173 * @data: (allow-none): user data to pass to @func, or %NULL
5174 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5176 * Sets the row separator function, which is used to determine
5177 * whether a row should be drawn as a separator. If the row separator
5178 * function is %NULL, no separators are drawn. This is the default value.
5183 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5184 GtkTreeViewRowSeparatorFunc func,
5186 GDestroyNotify destroy)
5188 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5190 if (combo_box->priv->row_separator_destroy)
5191 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5193 combo_box->priv->row_separator_func = func;
5194 combo_box->priv->row_separator_data = data;
5195 combo_box->priv->row_separator_destroy = destroy;
5197 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5198 if (combo_box->priv->tree_view)
5200 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5201 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5204 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5206 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5207 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5210 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5214 * gtk_combo_box_set_button_sensitivity:
5215 * @combo_box: a #GtkComboBox
5216 * @sensitivity: specify the sensitivity of the dropdown button
5218 * Sets whether the dropdown button of the combo box should be
5219 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5220 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5225 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5226 GtkSensitivityType sensitivity)
5228 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5230 if (combo_box->priv->button_sensitivity != sensitivity)
5232 combo_box->priv->button_sensitivity = sensitivity;
5233 gtk_combo_box_update_sensitivity (combo_box);
5235 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5240 * gtk_combo_box_get_button_sensitivity:
5241 * @combo_box: a #GtkComboBox
5243 * Returns whether the combo box sets the dropdown button
5244 * sensitive or not when there are no items in the model.
5246 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5247 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5248 * if the button is always insensitive or
5249 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5250 * the model has one item to be selected.
5255 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5257 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5259 return combo_box->priv->button_sensitivity;
5264 * gtk_combo_box_get_has_entry:
5265 * @combo_box: a #GtkComboBox
5267 * Returns whether the combo box has an entry.
5269 * Return Value: whether there is an entry in @combo_box.
5274 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5276 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5278 return combo_box->priv->has_entry;
5282 * gtk_combo_box_set_entry_text_column:
5283 * @combo_box: A #GtkComboBox
5284 * @text_column: A column in @model to get the strings from for
5285 * the internal entry
5287 * Sets the model column which @combo_box should use to get strings from
5288 * to be @text_column. The column @text_column in the model of @combo_box
5289 * must be of type %G_TYPE_STRING.
5291 * This is only relevant if @combo_box has been created with
5292 * #GtkComboBox:has-entry as %TRUE.
5297 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5300 GtkComboBoxPrivate *priv = combo_box->priv;
5301 GtkTreeModel *model;
5303 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5305 model = gtk_combo_box_get_model (combo_box);
5307 g_return_if_fail (text_column >= 0);
5308 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5310 priv->text_column = text_column;
5312 if (priv->text_renderer != NULL)
5313 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5314 priv->text_renderer,
5315 "text", text_column,
5320 * gtk_combo_box_get_entry_text_column:
5321 * @combo_box: A #GtkComboBox.
5323 * Returns the column which @combo_box is using to get the strings
5324 * from to display in the internal entry.
5326 * Return value: A column in the data source model of @combo_box.
5331 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5333 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5335 return combo_box->priv->text_column;
5339 * gtk_combo_box_set_focus_on_click:
5340 * @combo: a #GtkComboBox
5341 * @focus_on_click: whether the combo box grabs focus when clicked
5344 * Sets whether the combo box will grab focus when it is clicked with
5345 * the mouse. Making mouse clicks not grab focus is useful in places
5346 * like toolbars where you don't want the keyboard focus removed from
5347 * the main area of the application.
5352 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5353 gboolean focus_on_click)
5355 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5357 focus_on_click = focus_on_click != FALSE;
5359 if (combo_box->priv->focus_on_click != focus_on_click)
5361 combo_box->priv->focus_on_click = focus_on_click;
5363 if (combo_box->priv->button)
5364 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5367 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5372 * gtk_combo_box_get_focus_on_click:
5373 * @combo: a #GtkComboBox
5375 * Returns whether the combo box grabs focus when it is clicked
5376 * with the mouse. See gtk_combo_box_set_focus_on_click().
5378 * Return value: %TRUE if the combo box grabs focus when it is
5379 * clicked with the mouse.
5384 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5386 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5388 return combo_box->priv->focus_on_click;
5393 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5394 GtkBuilder *builder,
5396 const gchar *tagname,
5397 GMarkupParser *parser,
5400 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5401 tagname, parser, data))
5404 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5405 tagname, parser, data);
5409 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5410 GtkBuilder *builder,
5412 const gchar *tagname,
5415 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5416 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5420 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5421 GtkBuilder *builder,
5422 const gchar *childname)
5424 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5426 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5427 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5429 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5433 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5437 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5438 GtkComboBoxPrivate *priv = combo_box->priv;
5439 gint font_size, arrow_size;
5440 PangoContext *context;
5441 PangoFontMetrics *metrics;
5442 const PangoFontDescription *font_desc;
5444 gint minimum_width = 0, natural_width = 0;
5445 gint child_min, child_nat;
5446 GtkStyleContext *style_context;
5447 GtkStateFlags state;
5449 gfloat arrow_scaling;
5451 child = gtk_bin_get_child (GTK_BIN (widget));
5454 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5456 gtk_widget_style_get (GTK_WIDGET (widget),
5457 "arrow-size", &arrow_size,
5458 "arrow-scaling", &arrow_scaling,
5461 style_context = gtk_widget_get_style_context (widget);
5462 state = gtk_widget_get_state_flags (widget);
5464 get_widget_padding_and_border (widget, &padding);
5465 font_desc = gtk_style_context_get_font (style_context, state);
5467 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5468 metrics = pango_context_get_metrics (context, font_desc,
5469 pango_context_get_language (context));
5470 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5471 pango_font_metrics_get_descent (metrics));
5472 pango_font_metrics_unref (metrics);
5474 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5476 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5478 if (!priv->tree_view)
5481 if (priv->cell_view)
5483 gint box_width, xpad;
5484 GtkBorder button_padding;
5486 get_widget_padding_and_border (priv->button, &button_padding);
5488 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5489 xpad = button_padding.left + button_padding.right + padding.left + padding.right;
5491 minimum_width = child_min + box_width + xpad;
5492 natural_width = child_nat + box_width + xpad;
5496 gint but_width, but_nat_width;
5498 gtk_widget_get_preferred_width (priv->button,
5499 &but_width, &but_nat_width);
5501 minimum_width = child_min + but_width;
5502 natural_width = child_nat + but_nat_width;
5508 gint button_width, button_nat_width;
5510 /* sample + frame */
5511 minimum_width = child_min;
5512 natural_width = child_nat;
5514 if (priv->cell_view_frame)
5516 if (priv->has_frame)
5518 gint border_width, xpad;
5519 GtkBorder frame_padding;
5521 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5522 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5523 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5525 minimum_width += xpad;
5526 natural_width += xpad;
5531 gtk_widget_get_preferred_width (priv->button,
5532 &button_width, &button_nat_width);
5534 minimum_width += button_width;
5535 natural_width += button_nat_width;
5538 minimum_width += padding.left + padding.right;
5539 natural_width += padding.left + padding.right;
5542 *minimum_size = minimum_width;
5545 *natural_size = natural_width;
5549 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5555 /* Combo box is height-for-width only
5556 * (so we always just reserve enough height for the minimum width) */
5557 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5558 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5562 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5567 /* Combo box is height-for-width only
5568 * (so we assume we always reserved enough height for the minimum width) */
5569 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5574 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5579 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5580 GtkComboBoxPrivate *priv = combo_box->priv;
5581 gint min_height = 0, nat_height = 0;
5586 child = gtk_bin_get_child (GTK_BIN (widget));
5588 get_widget_padding_and_border (widget, &padding);
5591 if (!priv->tree_view)
5594 if (priv->cell_view)
5596 /* calculate x/y padding and separator/arrow size */
5597 gint box_width, box_height;
5599 GtkBorder button_padding;
5601 get_widget_padding_and_border (priv->button, &button_padding);
5603 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5604 gtk_widget_get_preferred_height_for_width (priv->box,
5605 box_width, &box_height, NULL);
5607 xpad = button_padding.left + button_padding.right;
5608 ypad = button_padding.top + button_padding.bottom;
5610 size -= box_width + xpad;
5612 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5613 * and fitting the whole treemodel */
5614 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5616 min_height = MAX (min_height, box_height);
5617 nat_height = MAX (nat_height, box_height);
5624 /* there is a custom child widget inside (no priv->cell_view) */
5625 gint but_width, but_height;
5627 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5628 gtk_widget_get_preferred_height_for_width (priv->button,
5629 but_width, &but_height, NULL);
5633 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5634 * and fitting the whole treemodel */
5635 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5637 min_height = MAX (min_height, but_height);
5638 nat_height = MAX (nat_height, but_height);
5644 gint but_width, but_height;
5645 gint xpad = 0, ypad = 0;
5647 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5648 gtk_widget_get_preferred_height_for_width (priv->button,
5649 but_width, &but_height, NULL);
5651 if (priv->cell_view_frame && priv->has_frame)
5653 GtkBorder frame_padding;
5656 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5657 get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5659 xpad = (2 * border_width) + padding.left + frame_padding.right;
5660 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5666 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5667 * and fitting the whole treemodel */
5668 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5670 min_height = MAX (min_height, but_height);
5671 nat_height = MAX (nat_height, but_height);
5677 min_height += padding.top + padding.bottom;
5678 nat_height += padding.top + padding.bottom;
5681 *minimum_size = min_height;
5684 *natural_size = nat_height;
5688 * gtk_combo_box_set_id_column:
5689 * @combo_box: A #GtkComboBox
5690 * @id_column: A column in @model to get string IDs for values from
5692 * Sets the model column which @combo_box should use to get string IDs
5693 * for values from. The column @id_column in the model of @combo_box
5694 * must be of type %G_TYPE_STRING.
5699 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5702 GtkComboBoxPrivate *priv = combo_box->priv;
5703 GtkTreeModel *model;
5705 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5707 if (id_column != priv->id_column)
5709 model = gtk_combo_box_get_model (combo_box);
5711 g_return_if_fail (id_column >= 0);
5712 g_return_if_fail (model == NULL ||
5713 id_column < gtk_tree_model_get_n_columns (model));
5715 priv->id_column = id_column;
5717 g_object_notify (G_OBJECT (combo_box), "id-column");
5718 g_object_notify (G_OBJECT (combo_box), "active-id");
5723 * gtk_combo_box_get_id_column:
5724 * @combo_box: A #GtkComboBox
5726 * Returns the column which @combo_box is using to get string IDs
5729 * Return value: A column in the data source model of @combo_box.
5734 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5736 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5738 return combo_box->priv->id_column;
5742 * gtk_combo_box_get_active_id:
5743 * @combo_box: a #GtkComboBox
5745 * Returns the ID of the active row of @combo_box. This value is taken
5746 * from the active row and the column specified by the #GtkComboBox:id-column
5747 * property of @combo_box (see gtk_combo_box_set_id_column()).
5749 * The returned value is an interned string which means that you can
5750 * compare the pointer by value to other interned strings and that you
5753 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5754 * no row is active, or if the active row has a %NULL ID value, then %NULL
5757 * Return value: the ID of the active row, or %NULL
5762 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5764 GtkTreeModel *model;
5768 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5770 column = combo_box->priv->id_column;
5775 model = gtk_combo_box_get_model (combo_box);
5776 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5777 G_TYPE_STRING, NULL);
5779 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5781 const gchar *interned;
5784 gtk_tree_model_get (model, &iter, column, &id, -1);
5785 interned = g_intern_string (id);
5795 * gtk_combo_box_set_active_id:
5796 * @combo_box: a #GtkComboBox
5797 * @active_id: (allow-none): the ID of the row to select, or %NULL
5799 * Changes the active row of @combo_box to the one that has an ID equal to
5800 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5801 * a %NULL ID string cannot be made active by this function.
5803 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5804 * row has the given ID then the function does nothing and returns %FALSE.
5806 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5807 * @active_id was given to unset the active row, the function
5808 * always returns %TRUE.
5813 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5814 const gchar *active_id)
5816 GtkTreeModel *model;
5818 gboolean match = FALSE;
5821 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5823 if (active_id == NULL)
5825 gtk_combo_box_set_active (combo_box, -1);
5826 return TRUE; /* active row was successfully unset */
5829 column = combo_box->priv->id_column;
5834 model = gtk_combo_box_get_model (combo_box);
5835 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5836 G_TYPE_STRING, FALSE);
5838 if (gtk_tree_model_get_iter_first (model, &iter))
5842 gtk_tree_model_get (model, &iter, column, &id, -1);
5844 match = strcmp (id, active_id) == 0;
5849 gtk_combo_box_set_active_iter (combo_box, &iter);
5852 } while (gtk_tree_model_iter_next (model, &iter));