2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcombobox.h"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
32 #include "gtkliststore.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkseparator.h"
42 #include "gtkwindow.h"
43 #include "gtktypebuiltins.h"
44 #include "gtkprivate.h"
46 #include <gobject/gvaluecollector.h>
51 #include "gtkmarshalers.h"
54 #include "gtkentryprivate.h"
55 #include "gtktreeprivate.h"
56 #include "a11y/gtkcomboboxaccessible.h"
61 * @Short_description: A widget used to choose from a list of items
63 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
65 * A GtkComboBox is a widget that allows the user to choose from a list of
66 * valid choices. The GtkComboBox displays the selected choice. When
67 * activated, the GtkComboBox displays a popup which allows the user to
68 * make a new choice. The style in which the selected value is displayed,
69 * and the style of the popup is determined by the current theme. It may
70 * be similar to a Windows-style combo box.
72 * The GtkComboBox uses the model-view pattern; the list of valid choices
73 * is specified in the form of a tree model, and the display of the choices
74 * can be adapted to the data in the model by using cell renderers, as you
75 * would in a tree view. This is possible since GtkComboBox implements the
76 * #GtkCellLayout interface. The tree model holding the valid choices is
77 * not restricted to a flat list, it can be a real tree, and the popup will
78 * reflect the tree structure.
80 * To allow the user to enter values not in the model, the 'has-entry'
81 * property allows the GtkComboBox to contain a #GtkEntry. This entry
82 * can be accessed by calling gtk_bin_get_child() on the combo box.
84 * For a simple list of textual choices, the model-view API of GtkComboBox
85 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
91 /* WELCOME, to THE house of evil code */
92 struct _GtkComboBoxPrivate
102 GtkShadowType shadow_type;
104 gint active; /* Only temporary */
105 GtkTreeRowReference *active_row;
107 GtkWidget *tree_view;
109 GtkWidget *cell_view;
110 GtkWidget *cell_view_frame;
115 GtkWidget *separator;
117 GtkWidget *popup_widget;
118 GtkWidget *popup_window;
119 GtkWidget *scrolled_window;
126 guint activate_button;
127 guint32 activate_time;
129 guint resize_idle_id;
131 /* For "has-entry" specific behavior we track
132 * an automated cell renderer and text column
135 GtkCellRenderer *text_renderer;
139 guint popup_in_progress : 1;
140 guint popup_shown : 1;
141 guint add_tearoffs : 1;
143 guint is_cell_renderer : 1;
144 guint editing_canceled : 1;
145 guint auto_scroll : 1;
146 guint focus_on_click : 1;
147 guint button_sensitivity : 2;
149 guint popup_fixed_width : 1;
151 GtkTreeViewRowSeparatorFunc row_separator_func;
152 gpointer row_separator_data;
153 GDestroyNotify row_separator_destroy;
155 GdkDevice *grab_pointer;
156 GdkDevice *grab_keyboard;
158 gchar *tearoff_title;
161 /* While debugging this evil code, I have learned that
162 * there are actually 4 modes to this widget, which can
163 * be characterized as follows
165 * 1) menu mode, no child added
168 * cell_view -> GtkCellView, regular child
169 * cell_view_frame -> NULL
170 * button -> GtkToggleButton set_parent to combo
171 * arrow -> GtkArrow set_parent to button
172 * separator -> GtkVSepator set_parent to button
173 * popup_widget -> GtkMenu
174 * popup_window -> NULL
175 * scrolled_window -> NULL
177 * 2) menu mode, child added
181 * cell_view_frame -> NULL
182 * button -> GtkToggleButton set_parent to combo
183 * arrow -> GtkArrow, child of button
185 * popup_widget -> GtkMenu
186 * popup_window -> NULL
187 * scrolled_window -> NULL
189 * 3) list mode, no child added
191 * tree_view -> GtkTreeView, child of scrolled_window
192 * cell_view -> GtkCellView, regular child
193 * cell_view_frame -> GtkFrame, set parent to combo
194 * button -> GtkToggleButton, set_parent to combo
195 * arrow -> GtkArrow, child of button
197 * popup_widget -> tree_view
198 * popup_window -> GtkWindow
199 * scrolled_window -> GtkScrolledWindow, child of popup_window
201 * 4) list mode, child added
203 * tree_view -> GtkTreeView, child of scrolled_window
205 * cell_view_frame -> NULL
206 * button -> GtkToggleButton, set_parent to combo
207 * arrow -> GtkArrow, child of button
209 * popup_widget -> tree_view
210 * popup_window -> GtkWindow
211 * scrolled_window -> GtkScrolledWindow, child of popup_window
227 PROP_ROW_SPAN_COLUMN,
228 PROP_COLUMN_SPAN_COLUMN,
235 PROP_BUTTON_SENSITIVITY,
236 PROP_EDITING_CANCELED,
238 PROP_ENTRY_TEXT_COLUMN,
239 PROP_POPUP_FIXED_WIDTH,
245 static guint combo_box_signals[LAST_SIGNAL] = {0,};
247 #define SCROLL_TIME 100
251 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
252 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
253 static GObject *gtk_combo_box_constructor (GType type,
254 guint n_construct_properties,
255 GObjectConstructParam *construct_properties);
256 static void gtk_combo_box_dispose (GObject *object);
257 static void gtk_combo_box_finalize (GObject *object);
258 static void gtk_combo_box_destroy (GtkWidget *widget);
260 static void gtk_combo_box_set_property (GObject *object,
264 static void gtk_combo_box_get_property (GObject *object,
269 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
270 GtkStateFlags previous);
271 static void gtk_combo_box_grab_focus (GtkWidget *widget);
272 static void gtk_combo_box_style_updated (GtkWidget *widget);
273 static void gtk_combo_box_button_toggled (GtkWidget *widget,
275 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
276 GtkStateFlags previous,
278 static void gtk_combo_box_add (GtkContainer *container,
280 static void gtk_combo_box_remove (GtkContainer *container,
283 static void gtk_combo_box_menu_show (GtkWidget *menu,
285 static void gtk_combo_box_menu_hide (GtkWidget *menu,
288 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
290 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
295 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
300 static void gtk_combo_box_menu_position (GtkMenu *menu,
306 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
308 static void gtk_combo_box_size_allocate (GtkWidget *widget,
309 GtkAllocation *allocation);
310 static void gtk_combo_box_forall (GtkContainer *container,
311 gboolean include_internals,
312 GtkCallback callback,
313 gpointer callback_data);
314 static gboolean gtk_combo_box_draw (GtkWidget *widget,
316 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
317 GdkEventScroll *event);
318 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
321 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
322 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
323 GtkScrollType scroll);
324 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
325 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
327 /* listening to the model */
328 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
332 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
335 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
340 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
344 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
350 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
355 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
356 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
358 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
359 GdkEventButton *event,
361 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
364 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
365 GdkEventCrossing *event,
367 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
370 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
371 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
372 GdkEventButton *event,
375 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
378 gboolean path_currently_selected,
381 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
385 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
388 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
389 gboolean add_children);
390 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
391 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
394 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
395 GdkEventButton *event,
397 static void gtk_combo_box_menu_activate (GtkWidget *menu,
399 GtkComboBox *combo_box);
400 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
401 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
404 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
406 guint32 activate_time);
409 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
411 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
412 gboolean group_cycling);
414 static void gtk_combo_box_child_show (GtkWidget *widget,
415 GtkComboBox *combo_box);
416 static void gtk_combo_box_child_hide (GtkWidget *widget,
417 GtkComboBox *combo_box);
419 /* GtkComboBox:has-entry callbacks */
420 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
422 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
426 /* GtkBuildable method implementation */
427 static GtkBuildableIface *parent_buildable_iface;
429 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
430 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
433 const gchar *tagname,
434 GMarkupParser *parser,
436 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
439 const gchar *tagname,
441 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
443 const gchar *childname);
446 /* GtkCellEditable method implementations */
447 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
450 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
453 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
456 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
460 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
464 static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container,
466 static void gtk_combo_box_direction_changed (GtkWidget *widget,
467 GtkTextDirection previous_direction);
469 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
470 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
471 gtk_combo_box_cell_layout_init)
472 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
473 gtk_combo_box_cell_editable_init)
474 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
475 gtk_combo_box_buildable_init))
480 gtk_combo_box_class_init (GtkComboBoxClass *klass)
482 GObjectClass *object_class;
483 GtkContainerClass *container_class;
484 GtkWidgetClass *widget_class;
485 GtkBindingSet *binding_set;
487 container_class = (GtkContainerClass *)klass;
488 container_class->forall = gtk_combo_box_forall;
489 container_class->add = gtk_combo_box_add;
490 container_class->remove = gtk_combo_box_remove;
491 container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
493 widget_class = (GtkWidgetClass *)klass;
494 widget_class->size_allocate = gtk_combo_box_size_allocate;
495 widget_class->draw = gtk_combo_box_draw;
496 widget_class->scroll_event = gtk_combo_box_scroll_event;
497 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
498 widget_class->grab_focus = gtk_combo_box_grab_focus;
499 widget_class->style_updated = gtk_combo_box_style_updated;
500 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
501 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
502 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
503 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
504 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
505 widget_class->destroy = gtk_combo_box_destroy;
506 widget_class->direction_changed = gtk_combo_box_direction_changed;
508 object_class = (GObjectClass *)klass;
509 object_class->constructor = gtk_combo_box_constructor;
510 object_class->dispose = gtk_combo_box_dispose;
511 object_class->finalize = gtk_combo_box_finalize;
512 object_class->set_property = gtk_combo_box_set_property;
513 object_class->get_property = gtk_combo_box_get_property;
517 * GtkComboBox::changed:
518 * @widget: the object which received the signal
520 * The changed signal is emitted when the active
521 * item is changed. The can be due to the user selecting
522 * a different item from the list, or due to a
523 * call to gtk_combo_box_set_active_iter().
524 * It will also be emitted while typing into the entry of a combo box
529 combo_box_signals[CHANGED] =
530 g_signal_new (I_("changed"),
531 G_OBJECT_CLASS_TYPE (klass),
533 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
535 g_cclosure_marshal_VOID__VOID,
538 * GtkComboBox::move-active:
539 * @widget: the object that received the signal
540 * @scroll_type: a #GtkScrollType
542 * The ::move-active signal is a
543 * <link linkend="keybinding-signals">keybinding signal</link>
544 * which gets emitted to move the active selection.
548 combo_box_signals[MOVE_ACTIVE] =
549 g_signal_new_class_handler (I_("move-active"),
550 G_OBJECT_CLASS_TYPE (klass),
551 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
552 G_CALLBACK (gtk_combo_box_real_move_active),
554 g_cclosure_marshal_VOID__ENUM,
556 GTK_TYPE_SCROLL_TYPE);
559 * GtkComboBox::popup:
560 * @widget: the object that received the signal
562 * The ::popup signal is a
563 * <link linkend="keybinding-signals">keybinding signal</link>
564 * which gets emitted to popup the combo box list.
566 * The default binding for this signal is Alt+Down.
570 combo_box_signals[POPUP] =
571 g_signal_new_class_handler (I_("popup"),
572 G_OBJECT_CLASS_TYPE (klass),
573 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
574 G_CALLBACK (gtk_combo_box_real_popup),
576 g_cclosure_marshal_VOID__VOID,
579 * GtkComboBox::popdown:
580 * @button: the object which received the signal
582 * The ::popdown signal is a
583 * <link linkend="keybinding-signals">keybinding signal</link>
584 * which gets emitted to popdown the combo box list.
586 * The default bindings for this signal are Alt+Up and Escape.
590 combo_box_signals[POPDOWN] =
591 g_signal_new_class_handler (I_("popdown"),
592 G_OBJECT_CLASS_TYPE (klass),
593 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
594 G_CALLBACK (gtk_combo_box_real_popdown),
596 _gtk_marshal_BOOLEAN__VOID,
600 binding_set = gtk_binding_set_by_class (widget_class);
602 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
604 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
607 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
609 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
611 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
614 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
616 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
617 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
619 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
620 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
622 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
623 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
625 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
626 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
628 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
629 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
631 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
633 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
635 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
636 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
638 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
639 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
641 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
642 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
644 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
645 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
647 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
648 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
650 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
653 g_object_class_override_property (object_class,
654 PROP_EDITING_CANCELED,
660 * The model from which the combo box takes the values shown
665 g_object_class_install_property (object_class,
667 g_param_spec_object ("model",
668 P_("ComboBox model"),
669 P_("The model for the combo box"),
671 GTK_PARAM_READWRITE));
674 * GtkComboBox:wrap-width:
676 * If wrap-width is set to a positive value, the list will be
677 * displayed in multiple columns, the number of columns is
678 * determined by wrap-width.
682 g_object_class_install_property (object_class,
684 g_param_spec_int ("wrap-width",
686 P_("Wrap width for laying out the items in a grid"),
690 GTK_PARAM_READWRITE));
694 * GtkComboBox:row-span-column:
696 * If this is set to a non-negative value, it must be the index of a column
697 * of type %G_TYPE_INT in the model.
699 * The values of that column are used to determine how many rows a value in
700 * the list will span. Therefore, the values in the model column pointed to
701 * by this property must be greater than zero and not larger than wrap-width.
705 g_object_class_install_property (object_class,
706 PROP_ROW_SPAN_COLUMN,
707 g_param_spec_int ("row-span-column",
708 P_("Row span column"),
709 P_("TreeModel column containing the row span values"),
713 GTK_PARAM_READWRITE));
717 * GtkComboBox:column-span-column:
719 * If this is set to a non-negative value, it must be the index of a column
720 * of type %G_TYPE_INT in the model.
722 * The values of that column are used to determine how many columns a value
723 * in the list will span.
727 g_object_class_install_property (object_class,
728 PROP_COLUMN_SPAN_COLUMN,
729 g_param_spec_int ("column-span-column",
730 P_("Column span column"),
731 P_("TreeModel column containing the column span values"),
735 GTK_PARAM_READWRITE));
739 * GtkComboBox:active:
741 * The item which is currently active. If the model is a non-flat treemodel,
742 * and the active item is not an immediate child of the root of the tree,
743 * this property has the value
744 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
745 * where <literal>path</literal> is the #GtkTreePath of the active item.
749 g_object_class_install_property (object_class,
751 g_param_spec_int ("active",
753 P_("The item which is currently active"),
757 GTK_PARAM_READWRITE));
760 * GtkComboBox:add-tearoffs:
762 * The add-tearoffs property controls whether generated menus
763 * have tearoff menu items.
765 * Note that this only affects menu style combo boxes.
769 g_object_class_install_property (object_class,
771 g_param_spec_boolean ("add-tearoffs",
772 P_("Add tearoffs to menus"),
773 P_("Whether dropdowns should have a tearoff menu item"),
775 GTK_PARAM_READWRITE));
778 * GtkComboBox:has-frame:
780 * The has-frame property controls whether a frame
781 * is drawn around the entry.
785 g_object_class_install_property (object_class,
787 g_param_spec_boolean ("has-frame",
789 P_("Whether the combo box draws a frame around the child"),
791 GTK_PARAM_READWRITE));
793 g_object_class_install_property (object_class,
795 g_param_spec_boolean ("focus-on-click",
796 P_("Focus on click"),
797 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
799 GTK_PARAM_READWRITE));
802 * GtkComboBox:tearoff-title:
804 * A title that may be displayed by the window manager
805 * when the popup is torn-off.
809 g_object_class_install_property (object_class,
811 g_param_spec_string ("tearoff-title",
813 P_("A title that may be displayed by the window manager when the popup is torn-off"),
815 GTK_PARAM_READWRITE));
819 * GtkComboBox:popup-shown:
821 * Whether the combo boxes dropdown is popped up.
822 * Note that this property is mainly useful, because
823 * it allows you to connect to notify::popup-shown.
827 g_object_class_install_property (object_class,
829 g_param_spec_boolean ("popup-shown",
831 P_("Whether the combo's dropdown is shown"),
833 GTK_PARAM_READABLE));
837 * GtkComboBox:button-sensitivity:
839 * Whether the dropdown button is sensitive when
840 * the model is empty.
844 g_object_class_install_property (object_class,
845 PROP_BUTTON_SENSITIVITY,
846 g_param_spec_enum ("button-sensitivity",
847 P_("Button Sensitivity"),
848 P_("Whether the dropdown button is sensitive when the model is empty"),
849 GTK_TYPE_SENSITIVITY_TYPE,
850 GTK_SENSITIVITY_AUTO,
851 GTK_PARAM_READWRITE));
854 * GtkComboBox:has-entry:
856 * Whether the combo box has an entry.
860 g_object_class_install_property (object_class,
862 g_param_spec_boolean ("has-entry",
864 P_("Whether combo box has an entry"),
866 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
869 * GtkComboBox:entry-text-column:
871 * The column in the combo box's model to associate with strings from the entry
872 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
876 g_object_class_install_property (object_class,
877 PROP_ENTRY_TEXT_COLUMN,
878 g_param_spec_int ("entry-text-column",
879 P_("Entry Text Column"),
880 P_("The column in the combo box's model to associate "
881 "with strings from the entry if the combo was "
882 "created with #GtkComboBox:has-entry = %TRUE"),
884 GTK_PARAM_READWRITE));
887 * GtkComboBox:id-column:
889 * The column in the combo box's model that provides string
890 * IDs for the values in the model, if != -1.
894 g_object_class_install_property (object_class,
896 g_param_spec_int ("id-column",
898 P_("The column in the combo box's model that provides "
899 "string IDs for the values in the model"),
901 GTK_PARAM_READWRITE));
904 * GtkComboBox:active-id:
906 * The value of the ID column of the active row.
910 g_object_class_install_property (object_class,
912 g_param_spec_string ("active-id",
914 P_("The value of the id column "
915 "for the active row"),
916 NULL, GTK_PARAM_READWRITE));
919 * GtkComboBox:popup-fixed-width:
921 * Whether the popup's width should be a fixed width matching the
922 * allocated width of the combo box.
926 g_object_class_install_property (object_class,
927 PROP_POPUP_FIXED_WIDTH,
928 g_param_spec_boolean ("popup-fixed-width",
929 P_("Popup Fixed Width"),
930 P_("Whether the popup's width should be a "
931 "fixed width matching the allocated width "
934 GTK_PARAM_READWRITE));
937 * GtkComboBox:cell-area:
939 * The #GtkCellArea used to layout cell renderers for this combo box.
941 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
942 * a horizontally oriented #GtkCellAreaBox will be used.
946 g_object_class_install_property (object_class,
948 g_param_spec_object ("cell-area",
950 P_("The GtkCellArea used to layout cells"),
952 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
954 gtk_widget_class_install_style_property (widget_class,
955 g_param_spec_boolean ("appears-as-list",
956 P_("Appears as list"),
957 P_("Whether dropdowns should look like lists rather than menus"),
959 GTK_PARAM_READABLE));
962 * GtkComboBox:arrow-size:
964 * Sets the minimum size of the arrow in the combo box. Note
965 * that the arrow size is coupled to the font size, so in case
966 * a larger font is used, the arrow will be larger than set
971 gtk_widget_class_install_style_property (widget_class,
972 g_param_spec_int ("arrow-size",
974 P_("The minimum size of the arrow in the combo box"),
978 GTK_PARAM_READABLE));
981 * GtkComboBox:arrow-scaling:
983 * Sets the amount of space used up by the combobox arrow,
984 * proportional to the font size.
988 gtk_widget_class_install_style_property (widget_class,
989 g_param_spec_float ("arrow-scaling",
991 P_("The amount of space used by the arrow"),
995 GTK_PARAM_READABLE));
998 * GtkComboBox:shadow-type:
1000 * Which kind of shadow to draw around the combo box.
1004 gtk_widget_class_install_style_property (widget_class,
1005 g_param_spec_enum ("shadow-type",
1007 P_("Which kind of shadow to draw around the combo box"),
1008 GTK_TYPE_SHADOW_TYPE,
1010 GTK_PARAM_READABLE));
1012 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1014 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1018 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1020 parent_buildable_iface = g_type_interface_peek_parent (iface);
1021 iface->add_child = _gtk_cell_layout_buildable_add_child;
1022 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1023 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1024 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1028 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1030 iface->get_area = gtk_combo_box_cell_layout_get_area;
1034 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1036 iface->start_editing = gtk_combo_box_start_editing;
1040 gtk_combo_box_init (GtkComboBox *combo_box)
1042 GtkComboBoxPrivate *priv;
1044 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1046 GtkComboBoxPrivate);
1047 priv = combo_box->priv;
1049 priv->wrap_width = 0;
1052 priv->active_row = NULL;
1053 priv->col_column = -1;
1054 priv->row_column = -1;
1056 priv->popup_shown = FALSE;
1057 priv->add_tearoffs = FALSE;
1058 priv->has_frame = TRUE;
1059 priv->is_cell_renderer = FALSE;
1060 priv->editing_canceled = FALSE;
1061 priv->auto_scroll = FALSE;
1062 priv->focus_on_click = TRUE;
1063 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1064 priv->has_entry = FALSE;
1065 priv->popup_fixed_width = TRUE;
1067 priv->text_column = -1;
1068 priv->text_renderer = NULL;
1069 priv->id_column = -1;
1073 gtk_combo_box_set_property (GObject *object,
1075 const GValue *value,
1078 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1079 GtkComboBoxPrivate *priv = combo_box->priv;
1085 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1088 case PROP_WRAP_WIDTH:
1089 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1092 case PROP_ROW_SPAN_COLUMN:
1093 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1096 case PROP_COLUMN_SPAN_COLUMN:
1097 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1101 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1104 case PROP_ADD_TEAROFFS:
1105 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1108 case PROP_HAS_FRAME:
1109 priv->has_frame = g_value_get_boolean (value);
1111 if (priv->has_entry)
1115 child = gtk_bin_get_child (GTK_BIN (combo_box));
1117 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1122 case PROP_FOCUS_ON_CLICK:
1123 gtk_combo_box_set_focus_on_click (combo_box,
1124 g_value_get_boolean (value));
1127 case PROP_TEAROFF_TITLE:
1128 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1131 case PROP_POPUP_SHOWN:
1132 if (g_value_get_boolean (value))
1133 gtk_combo_box_popup (combo_box);
1135 gtk_combo_box_popdown (combo_box);
1138 case PROP_BUTTON_SENSITIVITY:
1139 gtk_combo_box_set_button_sensitivity (combo_box,
1140 g_value_get_enum (value));
1143 case PROP_POPUP_FIXED_WIDTH:
1144 gtk_combo_box_set_popup_fixed_width (combo_box,
1145 g_value_get_boolean (value));
1148 case PROP_EDITING_CANCELED:
1149 priv->editing_canceled = g_value_get_boolean (value);
1152 case PROP_HAS_ENTRY:
1153 priv->has_entry = g_value_get_boolean (value);
1156 case PROP_ENTRY_TEXT_COLUMN:
1157 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1160 case PROP_ID_COLUMN:
1161 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1164 case PROP_ACTIVE_ID:
1165 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1168 case PROP_CELL_AREA:
1169 /* Construct-only, can only be assigned once */
1170 area = g_value_get_object (value);
1173 if (priv->area != NULL)
1175 g_warning ("cell-area has already been set, ignoring construct property");
1176 g_object_ref_sink (area);
1177 g_object_unref (area);
1180 priv->area = g_object_ref_sink (area);
1185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1191 gtk_combo_box_get_property (GObject *object,
1196 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1197 GtkComboBoxPrivate *priv = combo_box->priv;
1202 g_value_set_object (value, combo_box->priv->model);
1205 case PROP_WRAP_WIDTH:
1206 g_value_set_int (value, combo_box->priv->wrap_width);
1209 case PROP_ROW_SPAN_COLUMN:
1210 g_value_set_int (value, combo_box->priv->row_column);
1213 case PROP_COLUMN_SPAN_COLUMN:
1214 g_value_set_int (value, combo_box->priv->col_column);
1218 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1221 case PROP_ADD_TEAROFFS:
1222 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1225 case PROP_HAS_FRAME:
1226 g_value_set_boolean (value, combo_box->priv->has_frame);
1229 case PROP_FOCUS_ON_CLICK:
1230 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1233 case PROP_TEAROFF_TITLE:
1234 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1237 case PROP_POPUP_SHOWN:
1238 g_value_set_boolean (value, combo_box->priv->popup_shown);
1241 case PROP_BUTTON_SENSITIVITY:
1242 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1245 case PROP_POPUP_FIXED_WIDTH:
1246 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1249 case PROP_EDITING_CANCELED:
1250 g_value_set_boolean (value, priv->editing_canceled);
1253 case PROP_HAS_ENTRY:
1254 g_value_set_boolean (value, priv->has_entry);
1257 case PROP_ENTRY_TEXT_COLUMN:
1258 g_value_set_int (value, priv->text_column);
1261 case PROP_ID_COLUMN:
1262 g_value_set_int (value, priv->id_column);
1265 case PROP_ACTIVE_ID:
1266 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1269 case PROP_CELL_AREA:
1270 g_value_set_object (value, priv->area);
1274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1280 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1281 GtkStateFlags previous)
1283 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1284 GtkComboBoxPrivate *priv = combo_box->priv;
1286 if (gtk_widget_get_realized (widget))
1288 if (priv->tree_view && priv->cell_view)
1290 GtkStyleContext *context;
1291 GtkStateFlags state;
1294 context = gtk_widget_get_style_context (widget);
1295 state = gtk_widget_get_state_flags (widget);
1296 gtk_style_context_get_background_color (context, state, &color);
1298 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1302 gtk_widget_queue_draw (widget);
1306 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1307 GtkStateFlags previous,
1310 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1311 GtkComboBoxPrivate *priv = combo_box->priv;
1313 if (gtk_widget_get_realized (widget))
1315 if (!priv->tree_view && priv->cell_view)
1316 gtk_widget_set_state_flags (priv->cell_view,
1317 gtk_widget_get_state_flags (widget),
1321 gtk_widget_queue_draw (widget);
1325 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1327 gtk_container_forall (GTK_CONTAINER (combo_box),
1328 (GtkCallback) gtk_widget_reset_style,
1333 gtk_combo_box_direction_changed (GtkWidget *widget,
1334 GtkTextDirection previous_direction)
1336 gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1339 static GtkWidgetPath *
1340 gtk_combo_box_get_path_for_child (GtkContainer *container,
1343 GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1344 GtkWidgetPath *path;
1346 gboolean found = FALSE;
1347 GList *visible_children, *l;
1348 GtkWidgetPath *sibling_path;
1351 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
1353 if (gtk_widget_get_visible (child))
1355 visible_children = NULL;
1357 if (priv->button && gtk_widget_get_visible (priv->button))
1358 visible_children = g_list_prepend (visible_children, priv->button);
1360 if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1361 visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1363 widget = gtk_bin_get_child (GTK_BIN (container));
1364 if (widget && gtk_widget_get_visible (widget))
1365 visible_children = g_list_prepend (visible_children, widget);
1367 if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1368 visible_children = g_list_reverse (visible_children);
1372 for (l = visible_children; l; l = l->next)
1376 if (widget == child)
1388 sibling_path = gtk_widget_path_new ();
1390 for (l = visible_children; l; l = l->next)
1391 gtk_widget_path_append_for_widget (sibling_path, l->data);
1393 gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1395 g_list_free (visible_children);
1396 gtk_widget_path_unref (sibling_path);
1400 gtk_widget_path_append_for_widget (path, child);
1407 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1409 GtkComboBoxPrivate *priv = combo_box->priv;
1410 gboolean appears_as_list;
1412 /* if wrap_width > 0, then we are in grid-mode and forced to use
1415 if (priv->wrap_width)
1416 appears_as_list = FALSE;
1418 gtk_widget_style_get (GTK_WIDGET (combo_box),
1419 "appears-as-list", &appears_as_list,
1422 if (appears_as_list)
1424 /* Destroy all the menu mode widgets, if they exist. */
1425 if (GTK_IS_MENU (priv->popup_widget))
1426 gtk_combo_box_menu_destroy (combo_box);
1428 /* Create the list mode widgets, if they don't already exist. */
1429 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1430 gtk_combo_box_list_setup (combo_box);
1434 /* Destroy all the list mode widgets, if they exist. */
1435 if (GTK_IS_TREE_VIEW (priv->tree_view))
1436 gtk_combo_box_list_destroy (combo_box);
1438 /* Create the menu mode widgets, if they don't already exist. */
1439 if (!GTK_IS_MENU (priv->popup_widget))
1440 gtk_combo_box_menu_setup (combo_box, TRUE);
1443 gtk_widget_style_get (GTK_WIDGET (combo_box),
1444 "shadow-type", &priv->shadow_type,
1449 gtk_combo_box_style_updated (GtkWidget *widget)
1451 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1452 GtkComboBoxPrivate *priv = combo_box->priv;
1455 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1457 gtk_combo_box_check_appearance (combo_box);
1459 if (priv->tree_view && priv->cell_view)
1461 GtkStyleContext *context;
1462 GtkStateFlags state;
1465 context = gtk_widget_get_style_context (widget);
1466 state = gtk_widget_get_state_flags (widget);
1467 gtk_style_context_get_background_color (context, state, &color);
1469 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1472 child = gtk_bin_get_child (GTK_BIN (combo_box));
1473 if (GTK_IS_ENTRY (child))
1474 g_object_set (child, "shadow-type",
1475 GTK_SHADOW_NONE == priv->shadow_type ?
1476 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1480 gtk_combo_box_button_toggled (GtkWidget *widget,
1483 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1485 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1487 if (!combo_box->priv->popup_in_progress)
1488 gtk_combo_box_popup (combo_box);
1491 gtk_combo_box_popdown (combo_box);
1495 gtk_combo_box_add (GtkContainer *container,
1498 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1499 GtkComboBoxPrivate *priv = combo_box->priv;
1501 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1503 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1504 "(need an instance of GtkEntry or of a subclass)",
1505 G_OBJECT_TYPE_NAME (widget));
1509 if (priv->cell_view &&
1510 gtk_widget_get_parent (priv->cell_view))
1512 gtk_widget_unparent (priv->cell_view);
1513 _gtk_bin_set_child (GTK_BIN (container), NULL);
1514 gtk_widget_queue_resize (GTK_WIDGET (container));
1517 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1518 _gtk_bin_set_child (GTK_BIN (container), widget);
1520 if (priv->cell_view &&
1521 widget != priv->cell_view)
1523 /* since the cell_view was unparented, it's gone now */
1524 priv->cell_view = NULL;
1526 if (!priv->tree_view && priv->separator)
1528 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1530 priv->separator = NULL;
1532 gtk_widget_queue_resize (GTK_WIDGET (container));
1534 else if (priv->cell_view_frame)
1536 gtk_widget_unparent (priv->cell_view_frame);
1537 priv->cell_view_frame = NULL;
1542 if (priv->has_entry)
1544 /* this flag is a hack to tell the entry to fill its allocation.
1546 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1548 g_signal_connect (widget, "changed",
1549 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1552 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1557 gtk_combo_box_remove (GtkContainer *container,
1560 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1561 GtkComboBoxPrivate *priv = combo_box->priv;
1563 gboolean appears_as_list;
1565 if (priv->has_entry)
1567 GtkWidget *child_widget;
1569 child_widget = gtk_bin_get_child (GTK_BIN (container));
1570 if (widget && widget == child_widget)
1572 g_signal_handlers_disconnect_by_func (widget,
1573 gtk_combo_box_entry_contents_changed,
1575 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1579 if (widget == priv->cell_view)
1580 priv->cell_view = NULL;
1582 gtk_widget_unparent (widget);
1583 _gtk_bin_set_child (GTK_BIN (container), NULL);
1585 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1588 gtk_widget_queue_resize (GTK_WIDGET (container));
1590 if (!priv->tree_view)
1591 appears_as_list = FALSE;
1593 appears_as_list = TRUE;
1595 if (appears_as_list)
1596 gtk_combo_box_list_destroy (combo_box);
1597 else if (GTK_IS_MENU (priv->popup_widget))
1599 gtk_combo_box_menu_destroy (combo_box);
1600 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1601 priv->popup_widget = NULL;
1604 if (!priv->cell_view)
1606 priv->cell_view = gtk_cell_view_new ();
1607 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1608 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1610 gtk_widget_show (priv->cell_view);
1611 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1616 if (appears_as_list)
1617 gtk_combo_box_list_setup (combo_box);
1619 gtk_combo_box_menu_setup (combo_box, TRUE);
1621 if (gtk_tree_row_reference_valid (priv->active_row))
1623 path = gtk_tree_row_reference_get_path (priv->active_row);
1624 gtk_combo_box_set_active_internal (combo_box, path);
1625 gtk_tree_path_free (path);
1628 gtk_combo_box_set_active_internal (combo_box, NULL);
1632 gtk_combo_box_menu_show (GtkWidget *menu,
1635 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1636 GtkComboBoxPrivate *priv = combo_box->priv;
1638 gtk_combo_box_child_show (menu, user_data);
1640 priv->popup_in_progress = TRUE;
1641 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1643 priv->popup_in_progress = FALSE;
1647 gtk_combo_box_menu_hide (GtkWidget *menu,
1650 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1652 gtk_combo_box_child_hide (menu,user_data);
1654 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1659 gtk_combo_box_detacher (GtkWidget *widget,
1662 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1663 GtkComboBoxPrivate *priv = combo_box->priv;
1665 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1667 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1668 gtk_combo_box_menu_show,
1670 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1671 gtk_combo_box_menu_hide,
1674 priv->popup_widget = NULL;
1678 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1681 GtkComboBoxPrivate *priv = combo_box->priv;
1683 if (GTK_IS_MENU (priv->popup_widget))
1685 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1686 priv->popup_widget = NULL;
1688 else if (priv->popup_widget)
1690 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1691 priv->popup_widget);
1692 g_object_unref (priv->popup_widget);
1693 priv->popup_widget = NULL;
1696 if (GTK_IS_MENU (popup))
1698 if (priv->popup_window)
1700 gtk_widget_destroy (priv->popup_window);
1701 priv->popup_window = NULL;
1704 priv->popup_widget = popup;
1707 * Note that we connect to show/hide on the toplevel, not the
1708 * menu itself, since the menu is not shown/hidden when it is
1709 * popped up while torn-off.
1711 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1712 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1713 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1714 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1716 gtk_menu_attach_to_widget (GTK_MENU (popup),
1717 GTK_WIDGET (combo_box),
1718 gtk_combo_box_detacher);
1722 if (!priv->popup_window)
1724 GtkWidget *toplevel;
1726 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1727 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1729 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1730 GDK_WINDOW_TYPE_HINT_COMBO);
1732 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1733 G_CALLBACK (gtk_combo_box_child_show),
1735 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1736 G_CALLBACK (gtk_combo_box_child_hide),
1739 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1740 if (GTK_IS_WINDOW (toplevel))
1742 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1743 GTK_WINDOW (priv->popup_window));
1744 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1745 GTK_WINDOW (toplevel));
1748 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1749 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1750 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1752 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1754 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1757 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1760 gtk_widget_show (priv->scrolled_window);
1762 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1763 priv->scrolled_window);
1766 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1769 gtk_widget_show (popup);
1770 g_object_ref (popup);
1771 priv->popup_widget = popup;
1776 get_widget_padding (GtkWidget *widget,
1779 GtkStyleContext *context;
1780 GtkStateFlags state;
1782 context = gtk_widget_get_style_context (widget);
1783 state = gtk_style_context_get_state (context);
1785 gtk_style_context_get_padding (context, state, padding);
1789 gtk_combo_box_menu_position_below (GtkMenu *menu,
1795 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1796 GtkAllocation child_allocation;
1802 GdkRectangle monitor;
1805 /* FIXME: is using the size request here broken? */
1806 child = gtk_bin_get_child (GTK_BIN (combo_box));
1810 gtk_widget_get_allocation (child, &child_allocation);
1812 if (!gtk_widget_get_has_window (child))
1814 sx += child_allocation.x;
1815 sy += child_allocation.y;
1818 gdk_window_get_root_coords (gtk_widget_get_window (child),
1820 get_widget_padding (GTK_WIDGET (combo_box), &padding);
1823 if (combo_box->priv->popup_fixed_width)
1824 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1826 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1828 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1831 *x = sx + child_allocation.width - req.width;
1834 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1835 monitor_num = gdk_screen_get_monitor_at_window (screen,
1836 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1837 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1841 else if (*x + req.width > monitor.x + monitor.width)
1842 *x = monitor.x + monitor.width - req.width;
1844 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1845 *y += child_allocation.height;
1846 else if (*y - monitor.y >= req.height)
1848 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1849 *y += child_allocation.height;
1857 gtk_combo_box_menu_position_over (GtkMenu *menu,
1863 GtkComboBox *combo_box;
1867 GtkAllocation allocation;
1868 GtkAllocation child_allocation;
1875 combo_box = GTK_COMBO_BOX (user_data);
1876 widget = GTK_WIDGET (combo_box);
1878 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1880 gtk_widget_get_allocation (widget, &allocation);
1882 menu_xpos = allocation.x;
1883 menu_ypos = allocation.y + allocation.height / 2 - 2;
1885 if (combo_box->priv->popup_fixed_width)
1886 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1888 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1892 gtk_widget_get_allocation (active, &child_allocation);
1893 menu_ypos -= child_allocation.height / 2;
1896 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1899 child = children->data;
1901 if (active == child)
1904 if (gtk_widget_get_visible (child))
1906 gtk_widget_get_allocation (child, &child_allocation);
1908 menu_ypos -= child_allocation.height;
1911 children = children->next;
1914 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1915 menu_xpos = menu_xpos + allocation.width - menu_width;
1917 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1918 menu_xpos, menu_ypos,
1919 &menu_xpos, &menu_ypos);
1921 /* Clamp the position on screen */
1922 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1926 else if ((menu_xpos + menu_width) > screen_width)
1927 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1936 gtk_combo_box_menu_position (GtkMenu *menu,
1942 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1943 GtkComboBoxPrivate *priv = combo_box->priv;
1944 GtkWidget *menu_item;
1946 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1947 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1950 /* FIXME handle nested menus better */
1951 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1953 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1956 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1959 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1960 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1961 GDK_WINDOW_TYPE_HINT_COMBO);
1965 gtk_combo_box_list_position (GtkComboBox *combo_box,
1971 GtkComboBoxPrivate *priv = combo_box->priv;
1972 GtkAllocation allocation;
1975 GdkRectangle monitor;
1976 GtkRequisition popup_req;
1977 GtkPolicyType hpolicy, vpolicy;
1980 /* under windows, the drop down list is as wide as the combo box itself.
1982 GtkWidget *widget = GTK_WIDGET (combo_box);
1986 gtk_widget_get_allocation (widget, &allocation);
1988 if (!gtk_widget_get_has_window (widget))
1994 window = gtk_widget_get_window (widget);
1996 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1999 *width = allocation.width;
2001 hpolicy = vpolicy = GTK_POLICY_NEVER;
2002 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2005 if (combo_box->priv->popup_fixed_width)
2007 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2009 if (popup_req.width > *width)
2011 hpolicy = GTK_POLICY_ALWAYS;
2012 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2018 /* XXX This code depends on treeviews properly reporting their natural width
2019 * list-mode menus won't fill up to their natural width until then */
2020 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2022 if (popup_req.width > *width)
2024 hpolicy = GTK_POLICY_NEVER;
2025 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2028 *width = popup_req.width;
2032 *height = popup_req.height;
2034 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2035 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2036 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2038 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2039 *x = *x + allocation.width - *width;
2043 else if (*x + *width > monitor.x + monitor.width)
2044 *x = monitor.x + monitor.width - *width;
2046 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2047 *y += allocation.height;
2048 else if (*y - *height >= monitor.y)
2050 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2052 *y += allocation.height;
2053 *height = monitor.y + monitor.height - *y;
2057 *height = *y - monitor.y;
2061 if (popup_req.height > *height)
2063 vpolicy = GTK_POLICY_ALWAYS;
2065 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2071 cell_layout_is_sensitive (GtkCellLayout *layout)
2073 GList *cells, *list;
2076 cells = gtk_cell_layout_get_cells (layout);
2079 for (list = cells; list; list = list->next)
2081 g_object_get (list->data, "sensitive", &sensitive, NULL);
2086 g_list_free (cells);
2092 cell_is_sensitive (GtkCellRenderer *cell,
2095 gboolean *sensitive = data;
2097 g_object_get (cell, "sensitive", sensitive, NULL);
2103 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2106 GtkComboBoxPrivate *priv = combo_box->priv;
2108 if (priv->row_separator_func)
2110 if (priv->row_separator_func (priv->model, iter,
2111 priv->row_separator_data))
2119 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2123 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2132 update_menu_sensitivity (GtkComboBox *combo_box,
2135 GtkComboBoxPrivate *priv = combo_box->priv;
2136 GList *children, *child;
2137 GtkWidget *item, *submenu, *separator;
2138 GtkWidget *cell_view;
2144 children = gtk_container_get_children (GTK_CONTAINER (menu));
2146 for (child = children; child; child = child->next)
2148 item = GTK_WIDGET (child->data);
2149 cell_view = gtk_bin_get_child (GTK_BIN (item));
2151 if (!GTK_IS_CELL_VIEW (cell_view))
2154 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2155 if (submenu != NULL)
2157 gtk_widget_set_sensitive (item, TRUE);
2158 update_menu_sensitivity (combo_box, submenu);
2162 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2164 if (menu != priv->popup_widget && child == children)
2166 separator = GTK_WIDGET (child->next->data);
2167 g_object_set (item, "visible", sensitive, NULL);
2168 g_object_set (separator, "visible", sensitive, NULL);
2171 gtk_widget_set_sensitive (item, sensitive);
2175 g_list_free (children);
2179 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2181 guint32 activate_time)
2183 GtkComboBoxPrivate *priv = combo_box->priv;
2186 gint width, min_width, nat_width;
2188 update_menu_sensitivity (combo_box, priv->popup_widget);
2191 if (gtk_tree_row_reference_valid (priv->active_row))
2193 path = gtk_tree_row_reference_get_path (priv->active_row);
2194 active_item = gtk_tree_path_get_indices (path)[0];
2195 gtk_tree_path_free (path);
2197 if (priv->add_tearoffs)
2201 /* FIXME handle nested menus better */
2202 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2204 if (priv->wrap_width == 0)
2206 GtkAllocation allocation;
2208 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2209 width = allocation.width;
2210 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2211 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2213 if (combo_box->priv->popup_fixed_width)
2214 width = MAX (width, min_width);
2216 width = MAX (width, nat_width);
2218 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2221 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2223 gtk_combo_box_menu_position, combo_box,
2224 button, activate_time);
2228 popup_grab_on_window (GdkWindow *window,
2229 GdkDevice *keyboard,
2231 guint32 activate_time)
2234 gdk_device_grab (keyboard, window,
2235 GDK_OWNERSHIP_WINDOW, TRUE,
2236 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2237 NULL, activate_time) != GDK_GRAB_SUCCESS)
2241 gdk_device_grab (pointer, window,
2242 GDK_OWNERSHIP_WINDOW, TRUE,
2243 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2244 GDK_POINTER_MOTION_MASK,
2245 NULL, activate_time) != GDK_GRAB_SUCCESS)
2248 gdk_device_ungrab (keyboard, activate_time);
2257 * gtk_combo_box_popup:
2258 * @combo_box: a #GtkComboBox
2260 * Pops up the menu or dropdown list of @combo_box.
2262 * This function is mostly intended for use by accessibility technologies;
2263 * applications should have little use for it.
2268 gtk_combo_box_popup (GtkComboBox *combo_box)
2270 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2272 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2276 * gtk_combo_box_popup_for_device:
2277 * @combo_box: a #GtkComboBox
2278 * @device: a #GdkDevice
2280 * Pops up the menu or dropdown list of @combo_box, the popup window
2281 * will be grabbed so only @device and its associated pointer/keyboard
2282 * are the only #GdkDevice<!-- -->s able to send events to it.
2287 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2290 GtkComboBoxPrivate *priv = combo_box->priv;
2291 gint x, y, width, height;
2292 GtkTreePath *path = NULL, *ppath;
2293 GtkWidget *toplevel;
2294 GdkDevice *keyboard, *pointer;
2297 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2298 g_return_if_fail (GDK_IS_DEVICE (device));
2300 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2303 if (gtk_widget_get_mapped (priv->popup_widget))
2306 if (priv->grab_pointer && priv->grab_keyboard)
2309 time = gtk_get_current_event_time ();
2311 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2314 pointer = gdk_device_get_associated_device (device);
2319 keyboard = gdk_device_get_associated_device (device);
2322 if (GTK_IS_MENU (priv->popup_widget))
2324 gtk_combo_box_menu_popup (combo_box,
2325 priv->activate_button,
2326 priv->activate_time);
2330 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2331 if (GTK_IS_WINDOW (toplevel))
2332 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2333 GTK_WINDOW (priv->popup_window));
2335 gtk_widget_show_all (priv->scrolled_window);
2336 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2338 gtk_widget_set_size_request (priv->popup_window, width, height);
2339 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2341 if (gtk_tree_row_reference_valid (priv->active_row))
2343 path = gtk_tree_row_reference_get_path (priv->active_row);
2344 ppath = gtk_tree_path_copy (path);
2345 if (gtk_tree_path_up (ppath))
2346 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2348 gtk_tree_path_free (ppath);
2350 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2354 gtk_widget_show (priv->popup_window);
2358 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2360 gtk_tree_path_free (path);
2363 gtk_widget_grab_focus (priv->popup_window);
2364 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2367 if (!gtk_widget_has_focus (priv->tree_view))
2368 gtk_widget_grab_focus (priv->tree_view);
2370 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2371 keyboard, pointer, time))
2373 gtk_widget_hide (priv->popup_window);
2377 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2378 priv->grab_pointer = pointer;
2379 priv->grab_keyboard = keyboard;
2383 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2387 device = gtk_get_current_event_device ();
2391 GdkDeviceManager *device_manager;
2392 GdkDisplay *display;
2395 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2396 device_manager = gdk_display_get_device_manager (display);
2398 /* No device was set, pick the first master device */
2399 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2400 device = devices->data;
2401 g_list_free (devices);
2404 gtk_combo_box_popup_for_device (combo_box, device);
2408 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2410 if (combo_box->priv->popup_shown)
2412 gtk_combo_box_popdown (combo_box);
2420 * gtk_combo_box_popdown:
2421 * @combo_box: a #GtkComboBox
2423 * Hides the menu or dropdown list of @combo_box.
2425 * This function is mostly intended for use by accessibility technologies;
2426 * applications should have little use for it.
2431 gtk_combo_box_popdown (GtkComboBox *combo_box)
2433 GtkComboBoxPrivate *priv = combo_box->priv;
2435 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2437 if (GTK_IS_MENU (priv->popup_widget))
2439 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2443 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2446 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2447 gtk_widget_hide (priv->popup_window);
2448 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2451 priv->grab_pointer = NULL;
2452 priv->grab_keyboard = NULL;
2455 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2456 gtk_widget_get_preferred_size (combo_box->priv->button, \
2460 child.x = allocation->x + padding.right; \
2462 child.x = allocation->x + allocation->width - req.width - padding.left; \
2464 child.y = allocation->y + padding.top; \
2465 child.width = req.width; \
2466 child.height = allocation->height - (padding.top + padding.bottom); \
2467 child.width = MAX (1, child.width); \
2468 child.height = MAX (1, child.height); \
2470 gtk_widget_size_allocate (combo_box->priv->button, &child);
2474 gtk_combo_box_size_allocate (GtkWidget *widget,
2475 GtkAllocation *allocation)
2477 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2478 GtkComboBoxPrivate *priv = combo_box->priv;
2479 GtkWidget *child_widget;
2480 gint focus_width, focus_pad;
2481 GtkAllocation child;
2483 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2486 gtk_widget_set_allocation (widget, allocation);
2487 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2488 get_widget_padding (widget, &padding);
2490 gtk_widget_style_get (widget,
2491 "focus-line-width", &focus_width,
2492 "focus-padding", &focus_pad,
2495 if (!priv->tree_view)
2497 if (priv->cell_view)
2499 GtkBorder button_padding;
2503 gtk_widget_size_allocate (priv->button, allocation);
2506 allocation->x += padding.left;
2507 allocation->y += padding.top;
2508 allocation->width -= padding.left + padding.right;
2509 allocation->height -= padding.top + padding.bottom;
2511 /* set some things ready */
2512 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2513 get_widget_padding (priv->button, &button_padding);
2515 child.x = allocation->x;
2516 child.y = allocation->y;
2517 width = allocation->width;
2518 child.height = allocation->height;
2520 if (!priv->is_cell_renderer)
2522 child.x += border_width + button_padding.left + focus_width + focus_pad;
2523 child.y += border_width + button_padding.top + focus_width + focus_pad;
2524 width -= (2 * (border_width + focus_width + focus_pad)) +
2525 button_padding.left + button_padding.right;
2526 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2527 button_padding.top + button_padding.bottom;
2530 /* handle the children */
2531 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2532 child.width = req.width;
2534 child.x += width - req.width - button_padding.right;
2535 child.width = MAX (1, child.width);
2536 child.height = MAX (1, child.height);
2537 gtk_widget_size_allocate (priv->arrow, &child);
2539 child.x += req.width;
2540 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2541 child.width = req.width;
2543 child.x -= req.width;
2544 child.width = MAX (1, child.width);
2545 child.height = MAX (1, child.height);
2546 gtk_widget_size_allocate (priv->separator, &child);
2550 child.x += req.width;
2551 child.width = allocation->x + allocation->width
2552 - (border_width + focus_width + focus_pad)
2557 child.width = child.x;
2558 child.x = allocation->x
2559 + border_width + button_padding.left + focus_width + focus_pad;
2560 child.width -= child.x;
2563 if (gtk_widget_get_visible (priv->popup_widget))
2565 gint width, menu_width;
2567 if (priv->wrap_width == 0)
2569 GtkAllocation combo_box_allocation;
2571 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2572 width = combo_box_allocation.width;
2573 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2575 if (combo_box->priv->popup_fixed_width)
2576 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2578 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2580 gtk_widget_set_size_request (priv->popup_widget,
2581 MAX (width, menu_width), -1);
2584 /* reposition the menu after giving it a new width */
2585 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2588 child.width = MAX (1, child.width);
2589 child.height = MAX (1, child.height);
2590 gtk_widget_size_allocate (child_widget, &child);
2594 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2597 child.x = allocation->x + req.width + padding.right;
2599 child.x = allocation->x + padding.left;
2600 child.y = allocation->y + padding.top;
2601 child.width = allocation->width - req.width - (padding.left + padding.right);
2602 child.width = MAX (1, child.width);
2603 child.height = MAX (1, child.height);
2604 gtk_widget_size_allocate (child_widget, &child);
2610 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2613 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2617 child.x = allocation->x + req.width;
2619 child.x = allocation->x;
2621 child.y = allocation->y;
2622 child.width = allocation->width - req.width;
2623 child.height = allocation->height;
2625 if (priv->cell_view_frame)
2627 child.x += padding.left + border_width;
2628 child.y += padding.top + border_width;
2629 child.width = MAX (1, child.width - (2 * border_width) - (padding.left + padding.right));
2630 child.height = MAX (1, child.height - (2 * border_width) - (padding.top + padding.bottom));
2631 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2634 if (priv->has_frame)
2636 GtkBorder frame_padding;
2638 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2639 get_widget_padding (priv->cell_view_frame, &frame_padding);
2641 child.x += border_width + frame_padding.left;
2642 child.y += border_width + frame_padding.right;
2643 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2644 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2649 child.x += padding.left + border_width;
2650 child.y += padding.top + border_width;
2651 child.width -= (2 * border_width) - (padding.left + padding.right);
2652 child.height -= (2 * border_width) - (padding.top + padding.bottom);
2655 if (gtk_widget_get_visible (priv->popup_window))
2657 gint x, y, width, height;
2658 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2659 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2660 gtk_widget_set_size_request (priv->popup_window, width, height);
2663 child.width = MAX (1, child.width);
2664 child.height = MAX (1, child.height);
2665 gtk_widget_size_allocate (child_widget, &child);
2669 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2672 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2674 GtkComboBoxPrivate *priv = combo_box->priv;
2678 g_signal_handler_disconnect (priv->model,
2680 g_signal_handler_disconnect (priv->model,
2682 g_signal_handler_disconnect (priv->model,
2683 priv->reordered_id);
2684 g_signal_handler_disconnect (priv->model,
2690 g_object_unref (priv->model);
2694 if (priv->active_row)
2696 gtk_tree_row_reference_free (priv->active_row);
2697 priv->active_row = NULL;
2700 if (priv->cell_view)
2701 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2705 gtk_combo_box_forall (GtkContainer *container,
2706 gboolean include_internals,
2707 GtkCallback callback,
2708 gpointer callback_data)
2710 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2711 GtkComboBoxPrivate *priv = combo_box->priv;
2714 if (include_internals)
2717 (* callback) (priv->button, callback_data);
2718 if (priv->cell_view_frame)
2719 (* callback) (priv->cell_view_frame, callback_data);
2722 child = gtk_bin_get_child (GTK_BIN (container));
2724 (* callback) (child, callback_data);
2728 gtk_combo_box_child_show (GtkWidget *widget,
2729 GtkComboBox *combo_box)
2731 GtkComboBoxPrivate *priv = combo_box->priv;
2733 priv->popup_shown = TRUE;
2734 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2738 gtk_combo_box_child_hide (GtkWidget *widget,
2739 GtkComboBox *combo_box)
2741 GtkComboBoxPrivate *priv = combo_box->priv;
2743 priv->popup_shown = FALSE;
2744 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2748 gtk_combo_box_draw (GtkWidget *widget,
2751 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2752 GtkComboBoxPrivate *priv = combo_box->priv;
2754 if (priv->shadow_type != GTK_SHADOW_NONE)
2756 GtkStyleContext *context;
2757 GtkStateFlags state;
2759 context = gtk_widget_get_style_context (widget);
2760 state = gtk_widget_get_state_flags (widget);
2761 gtk_style_context_set_state (context, state);
2763 gtk_render_background (context, cr, 0, 0,
2764 gtk_widget_get_allocated_width (widget),
2765 gtk_widget_get_allocated_height (widget));
2766 gtk_render_frame (context, cr, 0, 0,
2767 gtk_widget_get_allocated_width (widget),
2768 gtk_widget_get_allocated_height (widget));
2771 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2774 if (priv->tree_view && priv->cell_view_frame)
2776 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2777 priv->cell_view_frame, cr);
2780 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2781 gtk_bin_get_child (GTK_BIN (widget)),
2797 path_visible (GtkTreeView *view,
2803 /* Note that we rely on the fact that collapsed rows don't have nodes
2805 return _gtk_tree_view_find_node (view, path, &tree, &node);
2809 tree_next_func (GtkTreeModel *model,
2814 SearchData *search_data = (SearchData *)data;
2816 if (search_data->found)
2818 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2821 if (search_data->visible &&
2822 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2825 search_data->set = TRUE;
2826 search_data->iter = *iter;
2831 if (gtk_tree_path_compare (path, search_data->path) == 0)
2832 search_data->found = TRUE;
2838 tree_next (GtkComboBox *combo,
2839 GtkTreeModel *model,
2844 SearchData search_data;
2846 search_data.combo = combo;
2847 search_data.path = gtk_tree_model_get_path (model, iter);
2848 search_data.visible = visible;
2849 search_data.found = FALSE;
2850 search_data.set = FALSE;
2852 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2854 *next = search_data.iter;
2856 gtk_tree_path_free (search_data.path);
2858 return search_data.set;
2862 tree_prev_func (GtkTreeModel *model,
2867 SearchData *search_data = (SearchData *)data;
2869 if (gtk_tree_path_compare (path, search_data->path) == 0)
2871 search_data->found = TRUE;
2875 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2878 if (search_data->visible &&
2879 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2882 search_data->set = TRUE;
2883 search_data->iter = *iter;
2889 tree_prev (GtkComboBox *combo,
2890 GtkTreeModel *model,
2895 SearchData search_data;
2897 search_data.combo = combo;
2898 search_data.path = gtk_tree_model_get_path (model, iter);
2899 search_data.visible = visible;
2900 search_data.found = FALSE;
2901 search_data.set = FALSE;
2903 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2905 *prev = search_data.iter;
2907 gtk_tree_path_free (search_data.path);
2909 return search_data.set;
2913 tree_last_func (GtkTreeModel *model,
2918 SearchData *search_data = (SearchData *)data;
2920 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2923 /* Note that we rely on the fact that collapsed rows don't have nodes
2925 if (search_data->visible &&
2926 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2929 search_data->set = TRUE;
2930 search_data->iter = *iter;
2936 tree_last (GtkComboBox *combo,
2937 GtkTreeModel *model,
2941 SearchData search_data;
2943 search_data.combo = combo;
2944 search_data.visible = visible;
2945 search_data.set = FALSE;
2947 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2949 *last = search_data.iter;
2951 return search_data.set;
2956 tree_first_func (GtkTreeModel *model,
2961 SearchData *search_data = (SearchData *)data;
2963 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2966 if (search_data->visible &&
2967 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2970 search_data->set = TRUE;
2971 search_data->iter = *iter;
2977 tree_first (GtkComboBox *combo,
2978 GtkTreeModel *model,
2982 SearchData search_data;
2984 search_data.combo = combo;
2985 search_data.visible = visible;
2986 search_data.set = FALSE;
2988 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2990 *first = search_data.iter;
2992 return search_data.set;
2996 gtk_combo_box_scroll_event (GtkWidget *widget,
2997 GdkEventScroll *event)
2999 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3002 GtkTreeIter new_iter;
3004 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3007 if (event->direction == GDK_SCROLL_UP)
3008 found = tree_prev (combo_box, combo_box->priv->model,
3009 &iter, &new_iter, FALSE);
3011 found = tree_next (combo_box, combo_box->priv->model,
3012 &iter, &new_iter, FALSE);
3015 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3024 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3028 GtkComboBoxPrivate *priv = combo->priv;
3030 if (priv->row_separator_func)
3031 return priv->row_separator_func (model, iter, priv->row_separator_data);
3037 gtk_combo_box_header_func (GtkTreeModel *model,
3041 /* Every submenu has a selectable header, however we
3042 * can expose a method to make that configurable by
3043 * the user (like row_separator_func is done) */
3048 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3049 gboolean add_children)
3051 GtkComboBoxPrivate *priv = combo_box->priv;
3055 child = gtk_bin_get_child (GTK_BIN (combo_box));
3057 if (priv->cell_view)
3059 priv->button = gtk_toggle_button_new ();
3060 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3061 priv->focus_on_click);
3063 g_signal_connect (priv->button, "toggled",
3064 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3065 gtk_widget_set_parent (priv->button,
3066 gtk_widget_get_parent (child));
3068 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3069 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3071 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3072 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3074 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3075 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3077 gtk_widget_show_all (priv->button);
3081 priv->button = gtk_toggle_button_new ();
3082 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3083 priv->focus_on_click);
3085 g_signal_connect (priv->button, "toggled",
3086 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3087 gtk_widget_set_parent (priv->button,
3088 gtk_widget_get_parent (child));
3090 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3091 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3092 gtk_widget_show_all (priv->button);
3095 g_signal_connect (priv->button, "button-press-event",
3096 G_CALLBACK (gtk_combo_box_menu_button_press),
3098 g_signal_connect (priv->button, "state-flags-changed",
3099 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3102 /* create our funky menu */
3103 menu = _gtk_tree_menu_new_with_area (priv->area);
3104 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3106 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3108 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3109 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3110 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3111 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3112 combo_box->priv->add_tearoffs);
3114 g_signal_connect (menu, "menu-activate",
3115 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3117 /* Chain our row_separator_func through */
3118 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3119 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3122 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3123 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3126 g_signal_connect (menu, "key-press-event",
3127 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3128 gtk_combo_box_set_popup_widget (combo_box, menu);
3130 gtk_combo_box_update_title (combo_box);
3134 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3136 GtkComboBoxPrivate *priv = combo_box->priv;
3138 g_signal_handlers_disconnect_matched (priv->button,
3139 G_SIGNAL_MATCH_DATA,
3141 gtk_combo_box_menu_button_press, NULL);
3142 g_signal_handlers_disconnect_matched (priv->button,
3143 G_SIGNAL_MATCH_DATA,
3145 gtk_combo_box_button_state_flags_changed, combo_box);
3146 g_signal_handlers_disconnect_matched (priv->popup_widget,
3147 G_SIGNAL_MATCH_DATA,
3149 gtk_combo_box_menu_activate, combo_box);
3151 /* unparent will remove our latest ref */
3152 gtk_widget_unparent (priv->button);
3155 priv->button = NULL;
3157 priv->separator = NULL;
3159 /* changing the popup window will unref the menu and the children */
3164 gtk_combo_box_menu_button_press (GtkWidget *widget,
3165 GdkEventButton *event,
3168 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3169 GtkComboBoxPrivate *priv = combo_box->priv;
3171 if (GTK_IS_MENU (priv->popup_widget) &&
3172 event->type == GDK_BUTTON_PRESS && event->button == 1)
3174 if (priv->focus_on_click &&
3175 !gtk_widget_has_focus (priv->button))
3176 gtk_widget_grab_focus (priv->button);
3178 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3187 gtk_combo_box_menu_activate (GtkWidget *menu,
3189 GtkComboBox *combo_box)
3193 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3194 gtk_combo_box_set_active_iter (combo_box, &iter);
3196 g_object_set (combo_box,
3197 "editing-canceled", FALSE,
3203 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3206 gboolean sensitive = TRUE; /* fool code checkers */
3208 if (!combo_box->priv->button)
3211 switch (combo_box->priv->button_sensitivity)
3213 case GTK_SENSITIVITY_ON:
3216 case GTK_SENSITIVITY_OFF:
3219 case GTK_SENSITIVITY_AUTO:
3220 sensitive = combo_box->priv->model &&
3221 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3224 g_assert_not_reached ();
3228 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3230 /* In list-mode, we also need to update sensitivity of the event box */
3231 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3232 && combo_box->priv->cell_view)
3233 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3237 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3242 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3244 if (combo_box->priv->tree_view)
3245 gtk_combo_box_list_popup_resize (combo_box);
3247 gtk_combo_box_update_sensitivity (combo_box);
3251 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3255 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3256 GtkComboBoxPrivate *priv = combo_box->priv;
3258 if (!gtk_tree_row_reference_valid (priv->active_row))
3260 if (priv->cell_view)
3261 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3262 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3265 if (priv->tree_view)
3266 gtk_combo_box_list_popup_resize (combo_box);
3268 gtk_combo_box_update_sensitivity (combo_box);
3272 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3278 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3282 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3287 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3288 GtkComboBoxPrivate *priv = combo_box->priv;
3289 GtkTreePath *active_path;
3291 /* FIXME this belongs to GtkCellView */
3292 if (gtk_tree_row_reference_valid (priv->active_row))
3294 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3295 if (gtk_tree_path_compare (path, active_path) == 0 &&
3297 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3298 gtk_tree_path_free (active_path);
3301 if (priv->tree_view)
3302 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3306 list_popup_resize_idle (gpointer user_data)
3308 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3309 GtkComboBoxPrivate *priv = combo_box->priv;
3310 gint x, y, width, height;
3312 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3314 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3316 gtk_widget_set_size_request (priv->popup_window, width, height);
3317 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3320 priv->resize_idle_id = 0;
3326 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3328 GtkComboBoxPrivate *priv = combo_box->priv;
3330 if (!priv->resize_idle_id)
3331 priv->resize_idle_id =
3332 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3336 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3341 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3343 gtk_combo_box_list_popup_resize (combo_box);
3352 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3354 GtkComboBoxPrivate *priv = combo_box->priv;
3355 GtkTreeSelection *sel;
3357 GtkWidget *widget = GTK_WIDGET (combo_box);
3359 priv->button = gtk_toggle_button_new ();
3360 child = gtk_bin_get_child (GTK_BIN (combo_box));
3361 gtk_widget_set_parent (priv->button,
3362 gtk_widget_get_parent (child));
3363 g_signal_connect (priv->button, "button-press-event",
3364 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3365 g_signal_connect (priv->button, "toggled",
3366 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3368 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3369 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3370 priv->separator = NULL;
3371 gtk_widget_show_all (priv->button);
3373 if (priv->cell_view)
3375 GtkStyleContext *context;
3376 GtkStateFlags state;
3379 context = gtk_widget_get_style_context (widget);
3380 state = gtk_widget_get_state_flags (widget);
3381 gtk_style_context_get_background_color (context, state, &color);
3383 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3385 priv->box = gtk_event_box_new ();
3386 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3389 if (priv->has_frame)
3391 priv->cell_view_frame = gtk_frame_new (NULL);
3392 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3397 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3398 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3402 gtk_widget_set_parent (priv->cell_view_frame,
3403 gtk_widget_get_parent (child));
3404 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3405 gtk_widget_show_all (priv->cell_view_frame);
3407 g_signal_connect (priv->box, "button-press-event",
3408 G_CALLBACK (gtk_combo_box_list_button_pressed),
3412 priv->tree_view = gtk_tree_view_new ();
3413 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3414 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3415 gtk_tree_selection_set_select_function (sel,
3416 gtk_combo_box_list_select_func,
3418 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3420 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3423 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3424 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3428 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3430 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3431 gtk_tree_view_column_new_with_area (priv->area));
3433 if (gtk_tree_row_reference_valid (priv->active_row))
3437 path = gtk_tree_row_reference_get_path (priv->active_row);
3438 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3440 gtk_tree_path_free (path);
3443 /* set sample/popup widgets */
3444 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3446 g_signal_connect (priv->tree_view, "key-press-event",
3447 G_CALLBACK (gtk_combo_box_list_key_press),
3449 g_signal_connect (priv->tree_view, "enter-notify-event",
3450 G_CALLBACK (gtk_combo_box_list_enter_notify),
3452 g_signal_connect (priv->tree_view, "row-expanded",
3453 G_CALLBACK (gtk_combo_box_model_row_expanded),
3455 g_signal_connect (priv->tree_view, "row-collapsed",
3456 G_CALLBACK (gtk_combo_box_model_row_expanded),
3458 g_signal_connect (priv->popup_window, "button-press-event",
3459 G_CALLBACK (gtk_combo_box_list_button_pressed),
3461 g_signal_connect (priv->popup_window, "button-release-event",
3462 G_CALLBACK (gtk_combo_box_list_button_released),
3465 gtk_widget_show (priv->tree_view);
3467 gtk_combo_box_update_sensitivity (combo_box);
3471 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3473 GtkComboBoxPrivate *priv = combo_box->priv;
3475 /* disconnect signals */
3476 g_signal_handlers_disconnect_matched (priv->tree_view,
3477 G_SIGNAL_MATCH_DATA,
3478 0, 0, NULL, NULL, combo_box);
3479 g_signal_handlers_disconnect_matched (priv->button,
3480 G_SIGNAL_MATCH_DATA,
3482 gtk_combo_box_list_button_pressed,
3484 g_signal_handlers_disconnect_matched (priv->popup_window,
3485 G_SIGNAL_MATCH_DATA,
3487 gtk_combo_box_list_button_pressed,
3489 g_signal_handlers_disconnect_matched (priv->popup_window,
3490 G_SIGNAL_MATCH_DATA,
3492 gtk_combo_box_list_button_released,
3495 g_signal_handlers_disconnect_matched (priv->popup_window,
3496 G_SIGNAL_MATCH_DATA,
3498 gtk_combo_box_child_show,
3501 g_signal_handlers_disconnect_matched (priv->popup_window,
3502 G_SIGNAL_MATCH_DATA,
3504 gtk_combo_box_child_hide,
3508 g_signal_handlers_disconnect_matched (priv->box,
3509 G_SIGNAL_MATCH_DATA,
3511 gtk_combo_box_list_button_pressed,
3514 /* destroy things (unparent will kill the latest ref from us)
3515 * last unref on button will destroy the arrow
3517 gtk_widget_unparent (priv->button);
3518 priv->button = NULL;
3521 if (priv->cell_view)
3523 g_object_set (priv->cell_view,
3524 "background-set", FALSE,
3528 if (priv->cell_view_frame)
3530 gtk_widget_unparent (priv->cell_view_frame);
3531 priv->cell_view_frame = NULL;
3535 if (priv->scroll_timer)
3537 g_source_remove (priv->scroll_timer);
3538 priv->scroll_timer = 0;
3541 if (priv->resize_idle_id)
3543 g_source_remove (priv->resize_idle_id);
3544 priv->resize_idle_id = 0;
3547 gtk_widget_destroy (priv->tree_view);
3549 priv->tree_view = NULL;
3550 if (priv->popup_widget)
3552 g_object_unref (priv->popup_widget);
3553 priv->popup_widget = NULL;
3560 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3561 GdkEventButton *event,
3564 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3565 GtkComboBoxPrivate *priv = combo_box->priv;
3567 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3569 if (ewidget == priv->popup_window)
3572 if ((ewidget != priv->button && ewidget != priv->box) ||
3573 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3576 if (priv->focus_on_click &&
3577 !gtk_widget_has_focus (priv->button))
3578 gtk_widget_grab_focus (priv->button);
3580 gtk_combo_box_popup_for_device (combo_box, event->device);
3582 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3584 priv->auto_scroll = FALSE;
3585 if (priv->scroll_timer == 0)
3586 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3587 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3590 priv->popup_in_progress = TRUE;
3596 gtk_combo_box_list_button_released (GtkWidget *widget,
3597 GdkEventButton *event,
3601 GtkTreePath *path = NULL;
3604 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3605 GtkComboBoxPrivate *priv = combo_box->priv;
3607 gboolean popup_in_progress = FALSE;
3609 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3611 if (priv->popup_in_progress)
3613 popup_in_progress = TRUE;
3614 priv->popup_in_progress = FALSE;
3617 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3619 if (priv->scroll_timer)
3621 g_source_remove (priv->scroll_timer);
3622 priv->scroll_timer = 0;
3625 if (ewidget != priv->tree_view)
3627 if ((ewidget == priv->button ||
3628 ewidget == priv->box) &&
3629 !popup_in_progress &&
3630 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3632 gtk_combo_box_popdown (combo_box);
3636 /* released outside treeview */
3637 if (ewidget != priv->button && ewidget != priv->box)
3639 gtk_combo_box_popdown (combo_box);
3647 /* select something cool */
3648 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3654 return TRUE; /* clicked outside window? */
3656 gtk_tree_model_get_iter (priv->model, &iter, path);
3657 gtk_tree_path_free (path);
3659 gtk_combo_box_popdown (combo_box);
3661 if (tree_column_row_is_sensitive (combo_box, &iter))
3662 gtk_combo_box_set_active_iter (combo_box, &iter);
3668 gtk_combo_box_menu_key_press (GtkWidget *widget,
3672 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3674 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3676 /* The menu hasn't managed the
3677 * event, forward it to the combobox
3679 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3686 gtk_combo_box_list_key_press (GtkWidget *widget,
3690 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3693 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3694 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3696 GtkTreeModel *model = NULL;
3698 gtk_combo_box_popdown (combo_box);
3700 if (combo_box->priv->model)
3702 GtkTreeSelection *sel;
3704 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3706 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3707 gtk_combo_box_set_active_iter (combo_box, &iter);
3713 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3715 /* The list hasn't managed the
3716 * event, forward it to the combobox
3718 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3725 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3730 GtkAllocation allocation;
3731 GtkWidget *tree_view = combo_box->priv->tree_view;
3734 gtk_widget_get_allocation (tree_view, &allocation);
3736 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3737 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3739 if (x <= allocation.x &&
3740 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3742 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3743 gtk_adjustment_set_value (adj, value);
3745 else if (x >= allocation.x + allocation.width &&
3746 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3748 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3749 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3753 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3754 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3756 if (y <= allocation.y &&
3757 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3759 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3760 gtk_adjustment_set_value (adj, value);
3762 else if (y >= allocation.height &&
3763 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3765 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3766 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3772 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3774 GtkComboBoxPrivate *priv = combo_box->priv;
3777 if (priv->auto_scroll)
3779 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3782 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3789 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3790 GdkEventCrossing *event,
3793 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3795 combo_box->priv->auto_scroll = TRUE;
3801 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3802 GtkTreeModel *model,
3804 gboolean path_currently_selected,
3807 GList *list, *columns;
3808 gboolean sensitive = FALSE;
3810 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3812 for (list = columns; list && !sensitive; list = list->next)
3814 GList *cells, *cell;
3815 gboolean cell_sensitive, cell_visible;
3817 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3819 if (!gtk_tree_view_column_get_visible (column))
3822 gtk_tree_model_get_iter (model, &iter, path);
3823 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3826 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3829 g_object_get (cell->data,
3830 "sensitive", &cell_sensitive,
3831 "visible", &cell_visible,
3834 if (cell_visible && cell_sensitive)
3839 g_list_free (cells);
3841 sensitive = cell_sensitive;
3844 g_list_free (columns);
3850 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3855 /* XXX Do nothing ? */
3859 * GtkCellLayout implementation
3861 static GtkCellArea *
3862 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3864 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3865 GtkComboBoxPrivate *priv = combo->priv;
3867 if (G_UNLIKELY (!priv->area))
3869 priv->area = gtk_cell_area_box_new ();
3870 g_object_ref_sink (priv->area);
3881 * gtk_combo_box_new:
3883 * Creates a new empty #GtkComboBox.
3885 * Return value: A new #GtkComboBox.
3890 gtk_combo_box_new (void)
3892 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3896 * gtk_combo_box_new_with_area:
3897 * @area: the #GtkCellArea to use to layout cell renderers
3899 * Creates a new empty #GtkComboBox using @area to layout cells.
3901 * Return value: A new #GtkComboBox.
3904 gtk_combo_box_new_with_area (GtkCellArea *area)
3906 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3910 * gtk_combo_box_new_with_area_and_entry:
3911 * @area: the #GtkCellArea to use to layout cell renderers
3913 * Creates a new empty #GtkComboBox with an entry.
3915 * The new combo box will use @area to layout cells.
3917 * Return value: A new #GtkComboBox.
3920 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3922 return g_object_new (GTK_TYPE_COMBO_BOX,
3930 * gtk_combo_box_new_with_entry:
3932 * Creates a new empty #GtkComboBox with an entry.
3934 * Return value: A new #GtkComboBox.
3937 gtk_combo_box_new_with_entry (void)
3939 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3943 * gtk_combo_box_new_with_model:
3944 * @model: A #GtkTreeModel.
3946 * Creates a new #GtkComboBox with the model initialized to @model.
3948 * Return value: A new #GtkComboBox.
3953 gtk_combo_box_new_with_model (GtkTreeModel *model)
3955 GtkComboBox *combo_box;
3957 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3959 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3961 return GTK_WIDGET (combo_box);
3965 * gtk_combo_box_new_with_model_and_entry:
3966 * @model: A #GtkTreeModel
3968 * Creates a new empty #GtkComboBox with an entry
3969 * and with the model initialized to @model.
3971 * Return value: A new #GtkComboBox
3974 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3976 return g_object_new (GTK_TYPE_COMBO_BOX,
3983 * gtk_combo_box_get_wrap_width:
3984 * @combo_box: A #GtkComboBox
3986 * Returns the wrap width which is used to determine the number of columns
3987 * for the popup menu. If the wrap width is larger than 1, the combo box
3990 * Returns: the wrap width.
3995 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3997 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3999 return combo_box->priv->wrap_width;
4003 * gtk_combo_box_set_wrap_width:
4004 * @combo_box: A #GtkComboBox
4005 * @width: Preferred number of columns
4007 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4008 * the preferred number of columns when you want the popup to be layed out
4014 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4017 GtkComboBoxPrivate *priv;
4019 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4020 g_return_if_fail (width >= 0);
4022 priv = combo_box->priv;
4024 if (width != priv->wrap_width)
4026 priv->wrap_width = width;
4028 gtk_combo_box_check_appearance (combo_box);
4030 if (GTK_IS_TREE_MENU (priv->popup_widget))
4031 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4033 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4038 * gtk_combo_box_get_row_span_column:
4039 * @combo_box: A #GtkComboBox
4041 * Returns the column with row span information for @combo_box.
4043 * Returns: the row span column.
4048 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4050 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4052 return combo_box->priv->row_column;
4056 * gtk_combo_box_set_row_span_column:
4057 * @combo_box: A #GtkComboBox.
4058 * @row_span: A column in the model passed during construction.
4060 * Sets the column with row span information for @combo_box to be @row_span.
4061 * The row span column contains integers which indicate how many rows
4062 * an item should span.
4067 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4070 GtkComboBoxPrivate *priv;
4073 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4075 priv = combo_box->priv;
4077 col = gtk_tree_model_get_n_columns (priv->model);
4078 g_return_if_fail (row_span >= -1 && row_span < col);
4080 if (row_span != priv->row_column)
4082 priv->row_column = row_span;
4084 if (GTK_IS_TREE_MENU (priv->popup_widget))
4085 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4087 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4092 * gtk_combo_box_get_column_span_column:
4093 * @combo_box: A #GtkComboBox
4095 * Returns the column with column span information for @combo_box.
4097 * Returns: the column span column.
4102 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4104 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4106 return combo_box->priv->col_column;
4110 * gtk_combo_box_set_column_span_column:
4111 * @combo_box: A #GtkComboBox
4112 * @column_span: A column in the model passed during construction
4114 * Sets the column with column span information for @combo_box to be
4115 * @column_span. The column span column contains integers which indicate
4116 * how many columns an item should span.
4121 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4124 GtkComboBoxPrivate *priv;
4127 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4129 priv = combo_box->priv;
4131 col = gtk_tree_model_get_n_columns (priv->model);
4132 g_return_if_fail (column_span >= -1 && column_span < col);
4134 if (column_span != priv->col_column)
4136 priv->col_column = column_span;
4138 if (GTK_IS_TREE_MENU (priv->popup_widget))
4139 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4141 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4146 * gtk_combo_box_get_active:
4147 * @combo_box: A #GtkComboBox
4149 * Returns the index of the currently active item, or -1 if there's no
4150 * active item. If the model is a non-flat treemodel, and the active item
4151 * is not an immediate child of the root of the tree, this function returns
4152 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4153 * <literal>path</literal> is the #GtkTreePath of the active item.
4155 * Return value: An integer which is the index of the currently active item,
4156 * or -1 if there's no active item.
4161 gtk_combo_box_get_active (GtkComboBox *combo_box)
4163 GtkComboBoxPrivate *priv;
4166 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4168 priv = combo_box->priv;
4170 if (gtk_tree_row_reference_valid (priv->active_row))
4174 path = gtk_tree_row_reference_get_path (priv->active_row);
4175 result = gtk_tree_path_get_indices (path)[0];
4176 gtk_tree_path_free (path);
4185 * gtk_combo_box_set_active:
4186 * @combo_box: A #GtkComboBox
4187 * @index_: An index in the model passed during construction, or -1 to have
4190 * Sets the active item of @combo_box to be the item at @index.
4195 gtk_combo_box_set_active (GtkComboBox *combo_box,
4198 GtkTreePath *path = NULL;
4199 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4200 g_return_if_fail (index_ >= -1);
4202 if (combo_box->priv->model == NULL)
4204 /* Save index, in case the model is set after the index */
4205 combo_box->priv->active = index_;
4211 path = gtk_tree_path_new_from_indices (index_, -1);
4213 gtk_combo_box_set_active_internal (combo_box, path);
4216 gtk_tree_path_free (path);
4220 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4223 GtkComboBoxPrivate *priv = combo_box->priv;
4224 GtkTreePath *active_path;
4227 /* Remember whether the initially active row is valid. */
4228 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4230 if (path && is_valid_row_reference)
4232 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4233 path_cmp = gtk_tree_path_compare (path, active_path);
4234 gtk_tree_path_free (active_path);
4239 if (priv->active_row)
4241 gtk_tree_row_reference_free (priv->active_row);
4242 priv->active_row = NULL;
4247 if (priv->tree_view)
4248 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4251 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4253 if (GTK_IS_MENU (menu))
4254 gtk_menu_set_active (menu, -1);
4257 if (priv->cell_view)
4258 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4261 * Do not emit a "changed" signal when an already invalid selection was
4262 * now set to invalid.
4264 if (!is_valid_row_reference)
4270 gtk_tree_row_reference_new (priv->model, path);
4272 if (priv->tree_view)
4274 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4277 else if (GTK_IS_MENU (priv->popup_widget))
4279 /* FIXME handle nested menus better */
4280 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4281 gtk_tree_path_get_indices (path)[0]);
4284 if (priv->cell_view)
4285 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4289 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4290 g_object_notify (G_OBJECT (combo_box), "active");
4291 if (combo_box->priv->id_column >= 0)
4292 g_object_notify (G_OBJECT (combo_box), "active-id");
4297 * gtk_combo_box_get_active_iter:
4298 * @combo_box: A #GtkComboBox
4299 * @iter: (out): The uninitialized #GtkTreeIter
4301 * Sets @iter to point to the current active item, if it exists.
4303 * Return value: %TRUE, if @iter was set
4308 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4314 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4316 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4319 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4320 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4321 gtk_tree_path_free (path);
4327 * gtk_combo_box_set_active_iter:
4328 * @combo_box: A #GtkComboBox
4329 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4331 * Sets the current active item to be the one referenced by @iter, or
4332 * unsets the active item if @iter is %NULL.
4337 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4340 GtkTreePath *path = NULL;
4342 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4345 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4347 gtk_combo_box_set_active_internal (combo_box, path);
4348 gtk_tree_path_free (path);
4352 * gtk_combo_box_set_model:
4353 * @combo_box: A #GtkComboBox
4354 * @model: (allow-none): A #GtkTreeModel
4356 * Sets the model used by @combo_box to be @model. Will unset a previously set
4357 * model (if applicable). If model is %NULL, then it will unset the model.
4359 * Note that this function does not clear the cell renderers, you have to
4360 * call gtk_cell_layout_clear() yourself if you need to set up different
4361 * cell renderers for the new model.
4366 gtk_combo_box_set_model (GtkComboBox *combo_box,
4367 GtkTreeModel *model)
4369 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4370 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4372 if (model == combo_box->priv->model)
4375 gtk_combo_box_unset_model (combo_box);
4380 combo_box->priv->model = model;
4381 g_object_ref (combo_box->priv->model);
4383 combo_box->priv->inserted_id =
4384 g_signal_connect (combo_box->priv->model, "row-inserted",
4385 G_CALLBACK (gtk_combo_box_model_row_inserted),
4387 combo_box->priv->deleted_id =
4388 g_signal_connect (combo_box->priv->model, "row-deleted",
4389 G_CALLBACK (gtk_combo_box_model_row_deleted),
4391 combo_box->priv->reordered_id =
4392 g_signal_connect (combo_box->priv->model, "rows-reordered",
4393 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4395 combo_box->priv->changed_id =
4396 g_signal_connect (combo_box->priv->model, "row-changed",
4397 G_CALLBACK (gtk_combo_box_model_row_changed),
4400 if (combo_box->priv->tree_view)
4403 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4404 combo_box->priv->model);
4405 gtk_combo_box_list_popup_resize (combo_box);
4408 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4411 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4412 combo_box->priv->model);
4415 if (combo_box->priv->cell_view)
4416 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4417 combo_box->priv->model);
4419 if (combo_box->priv->active != -1)
4421 /* If an index was set in advance, apply it now */
4422 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4423 combo_box->priv->active = -1;
4427 gtk_combo_box_update_sensitivity (combo_box);
4429 g_object_notify (G_OBJECT (combo_box), "model");
4433 * gtk_combo_box_get_model:
4434 * @combo_box: A #GtkComboBox
4436 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4438 * Return value: (transfer none): A #GtkTreeModel which was passed
4439 * during construction.
4444 gtk_combo_box_get_model (GtkComboBox *combo_box)
4446 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4448 return combo_box->priv->model;
4452 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4453 GtkScrollType scroll)
4456 GtkTreeIter new_iter;
4457 gboolean active_iter;
4460 if (!combo_box->priv->model)
4462 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4466 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4470 case GTK_SCROLL_STEP_BACKWARD:
4471 case GTK_SCROLL_STEP_UP:
4472 case GTK_SCROLL_STEP_LEFT:
4475 found = tree_prev (combo_box, combo_box->priv->model,
4476 &iter, &new_iter, FALSE);
4479 /* else fall through */
4481 case GTK_SCROLL_PAGE_FORWARD:
4482 case GTK_SCROLL_PAGE_DOWN:
4483 case GTK_SCROLL_PAGE_RIGHT:
4484 case GTK_SCROLL_END:
4485 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4488 case GTK_SCROLL_STEP_FORWARD:
4489 case GTK_SCROLL_STEP_DOWN:
4490 case GTK_SCROLL_STEP_RIGHT:
4493 found = tree_next (combo_box, combo_box->priv->model,
4494 &iter, &new_iter, FALSE);
4497 /* else fall through */
4499 case GTK_SCROLL_PAGE_BACKWARD:
4500 case GTK_SCROLL_PAGE_UP:
4501 case GTK_SCROLL_PAGE_LEFT:
4502 case GTK_SCROLL_START:
4503 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4510 if (found && active_iter)
4512 GtkTreePath *old_path;
4513 GtkTreePath *new_path;
4515 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4516 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4518 if (gtk_tree_path_compare (old_path, new_path) == 0)
4521 gtk_tree_path_free (old_path);
4522 gtk_tree_path_free (new_path);
4527 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4531 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4536 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4537 gboolean group_cycling)
4539 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4541 if (combo_box->priv->has_entry)
4545 child = gtk_bin_get_child (GTK_BIN (combo_box));
4547 gtk_widget_grab_focus (child);
4550 gtk_widget_grab_focus (combo_box->priv->button);
4556 gtk_combo_box_grab_focus (GtkWidget *widget)
4558 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4560 if (combo_box->priv->has_entry)
4564 child = gtk_bin_get_child (GTK_BIN (combo_box));
4566 gtk_widget_grab_focus (child);
4569 gtk_widget_grab_focus (combo_box->priv->button);
4573 gtk_combo_box_destroy (GtkWidget *widget)
4575 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4577 if (combo_box->priv->popup_idle_id > 0)
4579 g_source_remove (combo_box->priv->popup_idle_id);
4580 combo_box->priv->popup_idle_id = 0;
4583 gtk_combo_box_popdown (combo_box);
4585 if (combo_box->priv->row_separator_destroy)
4586 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4588 combo_box->priv->row_separator_func = NULL;
4589 combo_box->priv->row_separator_data = NULL;
4590 combo_box->priv->row_separator_destroy = NULL;
4592 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4593 combo_box->priv->cell_view = NULL;
4597 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4600 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4603 * Fixes regression reported in bug #574059. The old functionality relied on
4604 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4605 * when the selection was already set to -1.
4607 if (gtk_combo_box_get_active(combo_box) == -1)
4608 g_signal_emit_by_name (combo_box, "changed");
4610 gtk_combo_box_set_active (combo_box, -1);
4614 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4617 GtkComboBoxPrivate *priv = combo_box->priv;
4618 GtkTreeModel *model;
4621 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4623 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4627 GValue value = {0,};
4629 g_signal_handlers_block_by_func (entry,
4630 gtk_combo_box_entry_contents_changed,
4633 model = gtk_combo_box_get_model (combo_box);
4635 gtk_tree_model_get_value (model, &iter,
4636 priv->text_column, &value);
4637 g_object_set_property (G_OBJECT (entry), "text", &value);
4638 g_value_unset (&value);
4640 g_signal_handlers_unblock_by_func (entry,
4641 gtk_combo_box_entry_contents_changed,
4648 gtk_combo_box_constructor (GType type,
4649 guint n_construct_properties,
4650 GObjectConstructParam *construct_properties)
4653 GtkComboBox *combo_box;
4654 GtkComboBoxPrivate *priv;
4656 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4657 (type, n_construct_properties, construct_properties);
4659 combo_box = GTK_COMBO_BOX (object);
4660 priv = combo_box->priv;
4664 priv->area = gtk_cell_area_box_new ();
4665 g_object_ref_sink (priv->area);
4668 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4669 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4670 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4671 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4672 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4673 gtk_widget_show (priv->cell_view);
4675 gtk_combo_box_check_appearance (combo_box);
4677 if (priv->has_entry)
4680 GtkStyleContext *context;
4682 entry = gtk_entry_new ();
4683 gtk_widget_show (entry);
4684 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4686 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4687 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4689 priv->text_renderer = gtk_cell_renderer_text_new ();
4690 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4691 priv->text_renderer, TRUE);
4693 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4695 g_signal_connect (combo_box, "changed",
4696 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4704 gtk_combo_box_dispose(GObject* object)
4706 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4708 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4710 gtk_combo_box_menu_destroy (combo_box);
4711 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4712 combo_box->priv->popup_widget = NULL;
4715 if (combo_box->priv->area)
4717 g_object_unref (combo_box->priv->area);
4718 combo_box->priv->area = NULL;
4721 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4722 gtk_combo_box_list_destroy (combo_box);
4724 if (combo_box->priv->popup_window)
4726 gtk_widget_destroy (combo_box->priv->popup_window);
4727 combo_box->priv->popup_window = NULL;
4730 gtk_combo_box_unset_model (combo_box);
4732 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4736 gtk_combo_box_finalize (GObject *object)
4738 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4740 g_free (combo_box->priv->tearoff_title);
4742 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4746 gtk_cell_editable_key_press (GtkWidget *widget,
4750 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4752 if (event->keyval == GDK_KEY_Escape)
4754 g_object_set (combo_box,
4755 "editing-canceled", TRUE,
4757 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4758 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4762 else if (event->keyval == GDK_KEY_Return ||
4763 event->keyval == GDK_KEY_ISO_Enter ||
4764 event->keyval == GDK_KEY_KP_Enter)
4766 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4767 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4776 popdown_idle (gpointer data)
4778 GtkComboBox *combo_box;
4780 combo_box = GTK_COMBO_BOX (data);
4782 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4783 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4785 g_object_unref (combo_box);
4791 popdown_handler (GtkWidget *widget,
4794 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4798 popup_idle (gpointer data)
4800 GtkComboBox *combo_box;
4802 combo_box = GTK_COMBO_BOX (data);
4804 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4805 combo_box->priv->cell_view)
4806 g_signal_connect_object (combo_box->priv->popup_widget,
4807 "unmap", G_CALLBACK (popdown_handler),
4810 /* we unset this if a menu item is activated */
4811 g_object_set (combo_box,
4812 "editing-canceled", TRUE,
4814 gtk_combo_box_popup (combo_box);
4816 combo_box->priv->popup_idle_id = 0;
4817 combo_box->priv->activate_button = 0;
4818 combo_box->priv->activate_time = 0;
4824 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4827 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4830 combo_box->priv->is_cell_renderer = TRUE;
4832 if (combo_box->priv->cell_view)
4834 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4835 G_CALLBACK (gtk_cell_editable_key_press),
4838 gtk_widget_grab_focus (combo_box->priv->button);
4842 child = gtk_bin_get_child (GTK_BIN (combo_box));
4844 g_signal_connect_object (child, "key-press-event",
4845 G_CALLBACK (gtk_cell_editable_key_press),
4848 gtk_widget_grab_focus (child);
4849 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4852 /* we do the immediate popup only for the optionmenu-like
4855 if (combo_box->priv->is_cell_renderer &&
4856 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4858 if (event && event->type == GDK_BUTTON_PRESS)
4860 GdkEventButton *event_button = (GdkEventButton *)event;
4862 combo_box->priv->activate_button = event_button->button;
4863 combo_box->priv->activate_time = event_button->time;
4866 combo_box->priv->popup_idle_id =
4867 gdk_threads_add_idle (popup_idle, combo_box);
4873 * gtk_combo_box_get_add_tearoffs:
4874 * @combo_box: a #GtkComboBox
4876 * Gets the current value of the :add-tearoffs property.
4878 * Return value: the current value of the :add-tearoffs property.
4881 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4883 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4885 return combo_box->priv->add_tearoffs;
4889 * gtk_combo_box_set_add_tearoffs:
4890 * @combo_box: a #GtkComboBox
4891 * @add_tearoffs: %TRUE to add tearoff menu items
4893 * Sets whether the popup menu should have a tearoff
4899 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4900 gboolean add_tearoffs)
4902 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4904 add_tearoffs = add_tearoffs != FALSE;
4906 if (combo_box->priv->add_tearoffs != add_tearoffs)
4908 combo_box->priv->add_tearoffs = add_tearoffs;
4909 gtk_combo_box_check_appearance (combo_box);
4911 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4912 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4913 combo_box->priv->add_tearoffs);
4915 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4920 * gtk_combo_box_get_title:
4921 * @combo_box: a #GtkComboBox
4923 * Gets the current title of the menu in tearoff mode. See
4924 * gtk_combo_box_set_add_tearoffs().
4926 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4927 * string which must not be freed.
4932 gtk_combo_box_get_title (GtkComboBox *combo_box)
4934 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4936 return combo_box->priv->tearoff_title;
4940 gtk_combo_box_update_title (GtkComboBox *combo_box)
4942 gtk_combo_box_check_appearance (combo_box);
4944 if (combo_box->priv->popup_widget &&
4945 GTK_IS_MENU (combo_box->priv->popup_widget))
4946 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4947 combo_box->priv->tearoff_title);
4951 * gtk_combo_box_set_title:
4952 * @combo_box: a #GtkComboBox
4953 * @title: a title for the menu in tearoff mode
4955 * Sets the menu's title in tearoff mode.
4960 gtk_combo_box_set_title (GtkComboBox *combo_box,
4963 GtkComboBoxPrivate *priv;
4965 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4967 priv = combo_box->priv;
4969 if (strcmp (title ? title : "",
4970 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4972 g_free (priv->tearoff_title);
4973 priv->tearoff_title = g_strdup (title);
4975 gtk_combo_box_update_title (combo_box);
4977 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4983 * gtk_combo_box_set_popup_fixed_width:
4984 * @combo_box: a #GtkComboBox
4985 * @fixed: whether to use a fixed popup width
4987 * Specifies whether the popup's width should be a fixed width
4988 * matching the allocated width of the combo box.
4993 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4996 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4998 if (combo_box->priv->popup_fixed_width != fixed)
5000 combo_box->priv->popup_fixed_width = fixed;
5002 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5007 * gtk_combo_box_get_popup_fixed_width:
5008 * @combo_box: a #GtkComboBox
5010 * Gets whether the popup uses a fixed width matching
5011 * the allocated width of the combo box.
5013 * Returns: %TRUE if the popup uses a fixed width
5018 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5020 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5022 return combo_box->priv->popup_fixed_width;
5027 * gtk_combo_box_get_popup_accessible:
5028 * @combo_box: a #GtkComboBox
5030 * Gets the accessible object corresponding to the combo box's popup.
5032 * This function is mostly intended for use by accessibility technologies;
5033 * applications should have little use for it.
5035 * Returns: (transfer none): the accessible object corresponding
5036 * to the combo box's popup.
5041 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5045 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5047 if (combo_box->priv->popup_widget)
5049 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5057 * gtk_combo_box_get_row_separator_func: (skip)
5058 * @combo_box: a #GtkComboBox
5060 * Returns the current row separator function.
5062 * Return value: the current row separator function.
5066 GtkTreeViewRowSeparatorFunc
5067 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5069 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5071 return combo_box->priv->row_separator_func;
5075 * gtk_combo_box_set_row_separator_func:
5076 * @combo_box: a #GtkComboBox
5077 * @func: a #GtkTreeViewRowSeparatorFunc
5078 * @data: (allow-none): user data to pass to @func, or %NULL
5079 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5081 * Sets the row separator function, which is used to determine
5082 * whether a row should be drawn as a separator. If the row separator
5083 * function is %NULL, no separators are drawn. This is the default value.
5088 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5089 GtkTreeViewRowSeparatorFunc func,
5091 GDestroyNotify destroy)
5093 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5095 if (combo_box->priv->row_separator_destroy)
5096 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5098 combo_box->priv->row_separator_func = func;
5099 combo_box->priv->row_separator_data = data;
5100 combo_box->priv->row_separator_destroy = destroy;
5102 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5103 if (combo_box->priv->tree_view)
5105 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5106 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5109 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5111 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5112 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5115 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5119 * gtk_combo_box_set_button_sensitivity:
5120 * @combo_box: a #GtkComboBox
5121 * @sensitivity: specify the sensitivity of the dropdown button
5123 * Sets whether the dropdown button of the combo box should be
5124 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5125 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5130 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5131 GtkSensitivityType sensitivity)
5133 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5135 if (combo_box->priv->button_sensitivity != sensitivity)
5137 combo_box->priv->button_sensitivity = sensitivity;
5138 gtk_combo_box_update_sensitivity (combo_box);
5140 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5145 * gtk_combo_box_get_button_sensitivity:
5146 * @combo_box: a #GtkComboBox
5148 * Returns whether the combo box sets the dropdown button
5149 * sensitive or not when there are no items in the model.
5151 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5152 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5153 * if the button is always insensitive or
5154 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5155 * the model has one item to be selected.
5160 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5162 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5164 return combo_box->priv->button_sensitivity;
5169 * gtk_combo_box_get_has_entry:
5170 * @combo_box: a #GtkComboBox
5172 * Returns whether the combo box has an entry.
5174 * Return Value: whether there is an entry in @combo_box.
5179 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5181 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5183 return combo_box->priv->has_entry;
5187 * gtk_combo_box_set_entry_text_column:
5188 * @combo_box: A #GtkComboBox
5189 * @text_column: A column in @model to get the strings from for
5190 * the internal entry
5192 * Sets the model column which @combo_box should use to get strings from
5193 * to be @text_column. The column @text_column in the model of @combo_box
5194 * must be of type %G_TYPE_STRING.
5196 * This is only relevant if @combo_box has been created with
5197 * #GtkComboBox:has-entry as %TRUE.
5202 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5205 GtkComboBoxPrivate *priv = combo_box->priv;
5206 GtkTreeModel *model;
5208 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5210 model = gtk_combo_box_get_model (combo_box);
5212 g_return_if_fail (text_column >= 0);
5213 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5215 priv->text_column = text_column;
5217 if (priv->text_renderer != NULL)
5218 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5219 priv->text_renderer,
5220 "text", text_column,
5225 * gtk_combo_box_get_entry_text_column:
5226 * @combo_box: A #GtkComboBox.
5228 * Returns the column which @combo_box is using to get the strings
5229 * from to display in the internal entry.
5231 * Return value: A column in the data source model of @combo_box.
5236 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5238 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5240 return combo_box->priv->text_column;
5244 * gtk_combo_box_set_focus_on_click:
5245 * @combo: a #GtkComboBox
5246 * @focus_on_click: whether the combo box grabs focus when clicked
5249 * Sets whether the combo box will grab focus when it is clicked with
5250 * the mouse. Making mouse clicks not grab focus is useful in places
5251 * like toolbars where you don't want the keyboard focus removed from
5252 * the main area of the application.
5257 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5258 gboolean focus_on_click)
5260 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5262 focus_on_click = focus_on_click != FALSE;
5264 if (combo_box->priv->focus_on_click != focus_on_click)
5266 combo_box->priv->focus_on_click = focus_on_click;
5268 if (combo_box->priv->button)
5269 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5272 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5277 * gtk_combo_box_get_focus_on_click:
5278 * @combo: a #GtkComboBox
5280 * Returns whether the combo box grabs focus when it is clicked
5281 * with the mouse. See gtk_combo_box_set_focus_on_click().
5283 * Return value: %TRUE if the combo box grabs focus when it is
5284 * clicked with the mouse.
5289 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5291 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5293 return combo_box->priv->focus_on_click;
5298 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5299 GtkBuilder *builder,
5301 const gchar *tagname,
5302 GMarkupParser *parser,
5305 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5306 tagname, parser, data))
5309 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5310 tagname, parser, data);
5314 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5315 GtkBuilder *builder,
5317 const gchar *tagname,
5320 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5321 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5325 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5326 GtkBuilder *builder,
5327 const gchar *childname)
5329 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5331 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5332 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5334 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5338 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5342 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5343 GtkComboBoxPrivate *priv = combo_box->priv;
5344 gint focus_width, focus_pad;
5345 gint font_size, arrow_size;
5346 PangoContext *context;
5347 PangoFontMetrics *metrics;
5348 const PangoFontDescription *font_desc;
5350 gint minimum_width = 0, natural_width = 0;
5351 gint child_min, child_nat;
5352 GtkStyleContext *style_context;
5353 GtkStateFlags state;
5355 gfloat arrow_scaling;
5357 child = gtk_bin_get_child (GTK_BIN (widget));
5360 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5362 gtk_widget_style_get (GTK_WIDGET (widget),
5363 "focus-line-width", &focus_width,
5364 "focus-padding", &focus_pad,
5365 "arrow-size", &arrow_size,
5366 "arrow-scaling", &arrow_scaling,
5369 style_context = gtk_widget_get_style_context (widget);
5370 state = gtk_widget_get_state_flags (widget);
5372 get_widget_padding (widget, &padding);
5373 font_desc = gtk_style_context_get_font (style_context, state);
5375 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5376 metrics = pango_context_get_metrics (context, font_desc,
5377 pango_context_get_language (context));
5378 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5379 pango_font_metrics_get_descent (metrics));
5380 pango_font_metrics_unref (metrics);
5382 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5384 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5386 if (!priv->tree_view)
5389 if (priv->cell_view)
5391 gint sep_width, arrow_width;
5392 gint border_width, xpad;
5393 GtkBorder button_padding;
5395 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5396 get_widget_padding (priv->button, &button_padding);
5398 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5399 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5401 xpad = 2 * (border_width + focus_width + focus_pad) +
5402 button_padding.left + button_padding.right + padding.left + padding.right;
5404 minimum_width = child_min + sep_width + arrow_width + xpad;
5405 natural_width = child_nat + sep_width + arrow_width + xpad;
5409 gint but_width, but_nat_width;
5411 gtk_widget_get_preferred_width (priv->button,
5412 &but_width, &but_nat_width);
5414 minimum_width = child_min + but_width;
5415 natural_width = child_nat + but_nat_width;
5421 gint button_width, button_nat_width;
5423 /* sample + frame */
5424 minimum_width = child_min;
5425 natural_width = child_nat;
5427 minimum_width += 2 * focus_width;
5428 natural_width += 2 * focus_width;
5430 if (priv->cell_view_frame)
5432 if (priv->has_frame)
5434 gint border_width, xpad;
5435 GtkBorder frame_padding;
5437 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5438 get_widget_padding (priv->cell_view_frame, &frame_padding);
5439 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5441 minimum_width += xpad;
5442 natural_width += xpad;
5447 gtk_widget_get_preferred_width (priv->button,
5448 &button_width, &button_nat_width);
5450 minimum_width += button_width;
5451 natural_width += button_nat_width;
5454 minimum_width += padding.left + padding.right;
5455 natural_width += padding.left + padding.right;
5458 *minimum_size = minimum_width;
5461 *natural_size = natural_width;
5465 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5471 /* Combo box is height-for-width only
5472 * (so we always just reserve enough height for the minimum width) */
5473 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5474 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5478 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5483 /* Combo box is height-for-width only
5484 * (so we assume we always reserved enough height for the minimum width) */
5485 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5490 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5495 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5496 GtkComboBoxPrivate *priv = combo_box->priv;
5497 gint focus_width, focus_pad;
5498 gint min_height = 0, nat_height = 0;
5503 gtk_widget_style_get (GTK_WIDGET (widget),
5504 "focus-line-width", &focus_width,
5505 "focus-padding", &focus_pad,
5508 child = gtk_bin_get_child (GTK_BIN (widget));
5510 get_widget_padding (widget, &padding);
5513 if (!priv->tree_view)
5516 if (priv->cell_view)
5518 /* calculate x/y padding and separator/arrow size */
5519 gint sep_width, arrow_width, sep_height, arrow_height;
5520 gint border_width, xpad, ypad;
5521 GtkBorder button_padding;
5523 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5524 get_widget_padding (priv->button, &button_padding);
5526 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5527 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5528 gtk_widget_get_preferred_height_for_width (priv->separator,
5529 sep_width, &sep_height, NULL);
5530 gtk_widget_get_preferred_height_for_width (priv->arrow,
5531 arrow_width, &arrow_height, NULL);
5533 xpad = 2 * (border_width + focus_width + focus_pad) +
5534 button_padding.left + button_padding.right;
5535 ypad = 2 * (border_width + focus_width + focus_pad) +
5536 button_padding.top + button_padding.bottom;
5538 size -= sep_width + arrow_width + xpad;
5540 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5541 * and fitting the whole treemodel */
5542 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5544 arrow_height = MAX (arrow_height, sep_height);
5545 min_height = MAX (min_height, arrow_height);
5546 nat_height = MAX (nat_height, arrow_height);
5553 /* there is a custom child widget inside (no priv->cell_view) */
5554 gint but_width, but_height;
5556 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5557 gtk_widget_get_preferred_height_for_width (priv->button,
5558 but_width, &but_height, NULL);
5562 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5563 * and fitting the whole treemodel */
5564 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5566 min_height = MAX (min_height, but_height);
5567 nat_height = MAX (nat_height, but_height);
5573 gint but_width, but_height;
5574 gint xpad = 0, ypad = 0;
5576 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5577 gtk_widget_get_preferred_height_for_width (priv->button,
5578 but_width, &but_height, NULL);
5580 if (priv->cell_view_frame && priv->has_frame)
5582 GtkBorder frame_padding;
5585 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5586 get_widget_padding (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5588 xpad = (2 * border_width) + padding.left + frame_padding.right;
5589 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5593 size -= 2 * focus_width;
5596 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5597 * and fitting the whole treemodel */
5598 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5600 min_height = MAX (min_height, but_height);
5601 nat_height = MAX (nat_height, but_height);
5607 min_height += padding.top + padding.bottom;
5608 nat_height += padding.top + padding.bottom;
5611 *minimum_size = min_height;
5614 *natural_size = nat_height;
5618 * gtk_combo_box_set_id_column:
5619 * @combo_box: A #GtkComboBox
5620 * @id_column: A column in @model to get string IDs for values from
5622 * Sets the model column which @combo_box should use to get string IDs
5623 * for values from. The column @id_column in the model of @combo_box
5624 * must be of type %G_TYPE_STRING.
5629 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5632 GtkComboBoxPrivate *priv = combo_box->priv;
5633 GtkTreeModel *model;
5635 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5637 if (id_column != priv->id_column)
5639 model = gtk_combo_box_get_model (combo_box);
5641 g_return_if_fail (id_column >= 0);
5642 g_return_if_fail (model == NULL ||
5643 id_column < gtk_tree_model_get_n_columns (model));
5645 priv->id_column = id_column;
5647 g_object_notify (G_OBJECT (combo_box), "id-column");
5648 g_object_notify (G_OBJECT (combo_box), "active-id");
5653 * gtk_combo_box_get_id_column:
5654 * @combo_box: A #GtkComboBox
5656 * Returns the column which @combo_box is using to get string IDs
5659 * Return value: A column in the data source model of @combo_box.
5664 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5666 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5668 return combo_box->priv->id_column;
5672 * gtk_combo_box_get_active_id:
5673 * @combo_box: a #GtkComboBox
5675 * Returns the ID of the active row of @combo_box. This value is taken
5676 * from the active row and the column specified by the #GtkComboBox:id-column
5677 * property of @combo_box (see gtk_combo_box_set_id_column()).
5679 * The returned value is an interned string which means that you can
5680 * compare the pointer by value to other interned strings and that you
5683 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5684 * no row is active, or if the active row has a %NULL ID value, then %NULL
5687 * Return value: the ID of the active row, or %NULL
5692 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5694 GtkTreeModel *model;
5698 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5700 column = combo_box->priv->id_column;
5705 model = gtk_combo_box_get_model (combo_box);
5706 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5707 G_TYPE_STRING, NULL);
5709 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5711 const gchar *interned;
5714 gtk_tree_model_get (model, &iter, column, &id, -1);
5715 interned = g_intern_string (id);
5725 * gtk_combo_box_set_active_id:
5726 * @combo_box: a #GtkComboBox
5727 * @active_id: (allow-none): the ID of the row to select, or %NULL
5729 * Changes the active row of @combo_box to the one that has an ID equal to
5730 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5731 * a %NULL ID string cannot be made active by this function.
5733 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5734 * row has the given ID then the function does nothing and returns %FALSE.
5736 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5737 * @active_id was given to unset the active row, the function
5738 * always returns %TRUE.
5743 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5744 const gchar *active_id)
5746 GtkTreeModel *model;
5748 gboolean match = FALSE;
5751 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5753 if (active_id == NULL)
5755 gtk_combo_box_set_active (combo_box, -1);
5756 return TRUE; /* active row was successfully unset */
5759 column = combo_box->priv->id_column;
5764 model = gtk_combo_box_get_model (combo_box);
5765 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5766 G_TYPE_STRING, FALSE);
5768 if (gtk_tree_model_get_iter_first (model, &iter))
5772 gtk_tree_model_get (model, &iter, column, &id, -1);
5774 match = strcmp (id, active_id) == 0;
5779 gtk_combo_box_set_active_iter (combo_box, &iter);
5782 } while (gtk_tree_model_iter_next (model, &iter));