2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcombobox.h"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
32 #include "gtkliststore.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkvseparator.h"
42 #include "gtkwindow.h"
43 #include "gtkprivate.h"
45 #include <gobject/gvaluecollector.h>
50 #include "gtkmarshalers.h"
53 #include "gtkentryprivate.h"
54 #include "gtktreeprivate.h"
59 * @Short_description: A widget used to choose from a list of items
61 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
63 * A GtkComboBox is a widget that allows the user to choose from a list of
64 * valid choices. The GtkComboBox displays the selected choice. When
65 * activated, the GtkComboBox displays a popup which allows the user to
66 * make a new choice. The style in which the selected value is displayed,
67 * and the style of the popup is determined by the current theme. It may
68 * be similar to a Windows-style combo box.
70 * The GtkComboBox uses the model-view pattern; the list of valid choices
71 * is specified in the form of a tree model, and the display of the choices
72 * can be adapted to the data in the model by using cell renderers, as you
73 * would in a tree view. This is possible since GtkComboBox implements the
74 * #GtkCellLayout interface. The tree model holding the valid choices is
75 * not restricted to a flat list, it can be a real tree, and the popup will
76 * reflect the tree structure.
78 * To allow the user to enter values not in the model, the 'has-entry'
79 * property allows the GtkComboBox to contain a #GtkEntry. This entry
80 * can be accessed by calling gtk_bin_get_child() on the combo box.
82 * For a simple list of textual choices, the model-view API of GtkComboBox
83 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
84 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
89 /* WELCOME, to THE house of evil code */
90 struct _GtkComboBoxPrivate
100 GtkShadowType shadow_type;
102 gint active; /* Only temporary */
103 GtkTreeRowReference *active_row;
105 GtkWidget *tree_view;
106 GtkTreeViewColumn *column;
108 GtkWidget *cell_view;
109 GtkWidget *cell_view_frame;
114 GtkWidget *separator;
116 GtkWidget *popup_widget;
117 GtkWidget *popup_window;
118 GtkWidget *scrolled_window;
125 guint activate_button;
126 guint32 activate_time;
128 guint resize_idle_id;
130 /* For "has-entry" specific behavior we track
131 * an automated cell renderer and text column */
133 GtkCellRenderer *text_renderer;
137 guint popup_in_progress : 1;
138 guint popup_shown : 1;
139 guint add_tearoffs : 1;
141 guint is_cell_renderer : 1;
142 guint editing_canceled : 1;
143 guint auto_scroll : 1;
144 guint focus_on_click : 1;
145 guint button_sensitivity : 2;
147 guint popup_fixed_width : 1;
149 GtkTreeViewRowSeparatorFunc row_separator_func;
150 gpointer row_separator_data;
151 GDestroyNotify row_separator_destroy;
153 GdkDevice *grab_pointer;
154 GdkDevice *grab_keyboard;
156 gchar *tearoff_title;
159 /* While debugging this evil code, I have learned that
160 * there are actually 4 modes to this widget, which can
161 * be characterized as follows
163 * 1) menu mode, no child added
166 * cell_view -> GtkCellView, regular child
167 * cell_view_frame -> NULL
168 * button -> GtkToggleButton set_parent to combo
169 * arrow -> GtkArrow set_parent to button
170 * separator -> GtkVSepator set_parent to button
171 * popup_widget -> GtkMenu
172 * popup_window -> NULL
173 * scrolled_window -> NULL
175 * 2) menu mode, child added
179 * cell_view_frame -> NULL
180 * button -> GtkToggleButton set_parent to combo
181 * arrow -> GtkArrow, child of button
183 * popup_widget -> GtkMenu
184 * popup_window -> NULL
185 * scrolled_window -> NULL
187 * 3) list mode, no child added
189 * tree_view -> GtkTreeView, child of scrolled_window
190 * cell_view -> GtkCellView, regular child
191 * cell_view_frame -> GtkFrame, set parent to combo
192 * button -> GtkToggleButton, set_parent to combo
193 * arrow -> GtkArrow, child of button
195 * popup_widget -> tree_view
196 * popup_window -> GtkWindow
197 * scrolled_window -> GtkScrolledWindow, child of popup_window
199 * 4) list mode, child added
201 * tree_view -> GtkTreeView, child of scrolled_window
203 * cell_view_frame -> NULL
204 * button -> GtkToggleButton, set_parent to combo
205 * arrow -> GtkArrow, child of button
207 * popup_widget -> tree_view
208 * popup_window -> GtkWindow
209 * scrolled_window -> GtkScrolledWindow, child of popup_window
225 PROP_ROW_SPAN_COLUMN,
226 PROP_COLUMN_SPAN_COLUMN,
233 PROP_BUTTON_SENSITIVITY,
234 PROP_EDITING_CANCELED,
236 PROP_ENTRY_TEXT_COLUMN,
237 PROP_POPUP_FIXED_WIDTH,
243 static guint combo_box_signals[LAST_SIGNAL] = {0,};
245 #define BONUS_PADDING 4
246 #define SCROLL_TIME 100
250 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
251 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
252 static GObject *gtk_combo_box_constructor (GType type,
253 guint n_construct_properties,
254 GObjectConstructParam *construct_properties);
255 static void gtk_combo_box_dispose (GObject *object);
256 static void gtk_combo_box_finalize (GObject *object);
257 static void gtk_combo_box_destroy (GtkWidget *widget);
259 static void gtk_combo_box_set_property (GObject *object,
263 static void gtk_combo_box_get_property (GObject *object,
268 static void gtk_combo_box_state_changed (GtkWidget *widget,
269 GtkStateType previous);
270 static void gtk_combo_box_grab_focus (GtkWidget *widget);
271 static void gtk_combo_box_style_updated (GtkWidget *widget);
272 static void gtk_combo_box_button_toggled (GtkWidget *widget,
274 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
275 GtkStateFlags previous,
277 static void gtk_combo_box_add (GtkContainer *container,
279 static void gtk_combo_box_remove (GtkContainer *container,
282 static void gtk_combo_box_menu_show (GtkWidget *menu,
284 static void gtk_combo_box_menu_hide (GtkWidget *menu,
287 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
289 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
294 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
299 static void gtk_combo_box_menu_position (GtkMenu *menu,
305 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
307 static void gtk_combo_box_size_allocate (GtkWidget *widget,
308 GtkAllocation *allocation);
309 static void gtk_combo_box_forall (GtkContainer *container,
310 gboolean include_internals,
311 GtkCallback callback,
312 gpointer callback_data);
313 static gboolean gtk_combo_box_draw (GtkWidget *widget,
315 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
316 GdkEventScroll *event);
317 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
320 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
321 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
322 GtkScrollType scroll);
323 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
324 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
326 /* listening to the model */
327 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
331 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
334 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
339 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
343 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
349 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
354 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
355 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
357 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
358 GdkEventButton *event,
360 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
363 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
364 GdkEventCrossing *event,
366 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
369 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
370 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
371 GdkEventButton *event,
374 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
377 gboolean path_currently_selected,
380 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
384 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
387 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
388 gboolean add_children);
389 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
390 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
393 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
394 GdkEventButton *event,
396 static void gtk_combo_box_menu_activate (GtkWidget *menu,
398 GtkComboBox *combo_box);
399 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
400 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
403 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
405 guint32 activate_time);
408 GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
410 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
411 gboolean group_cycling);
413 static void gtk_combo_box_child_show (GtkWidget *widget,
414 GtkComboBox *combo_box);
415 static void gtk_combo_box_child_hide (GtkWidget *widget,
416 GtkComboBox *combo_box);
418 /* GtkComboBox:has-entry callbacks */
419 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
421 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
425 /* GtkBuildable method implementation */
426 static GtkBuildableIface *parent_buildable_iface;
428 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
429 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
432 const gchar *tagname,
433 GMarkupParser *parser,
435 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
438 const gchar *tagname,
440 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
442 const gchar *childname);
445 /* GtkCellEditable method implementations */
446 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
449 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
452 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
455 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
459 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
465 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
466 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
467 gtk_combo_box_cell_layout_init)
468 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
469 gtk_combo_box_cell_editable_init)
470 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
471 gtk_combo_box_buildable_init))
476 gtk_combo_box_class_init (GtkComboBoxClass *klass)
478 GObjectClass *object_class;
479 GtkContainerClass *container_class;
480 GtkWidgetClass *widget_class;
481 GtkBindingSet *binding_set;
483 container_class = (GtkContainerClass *)klass;
484 container_class->forall = gtk_combo_box_forall;
485 container_class->add = gtk_combo_box_add;
486 container_class->remove = gtk_combo_box_remove;
488 widget_class = (GtkWidgetClass *)klass;
489 widget_class->size_allocate = gtk_combo_box_size_allocate;
490 widget_class->draw = gtk_combo_box_draw;
491 widget_class->scroll_event = gtk_combo_box_scroll_event;
492 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
493 widget_class->grab_focus = gtk_combo_box_grab_focus;
494 widget_class->style_updated = gtk_combo_box_style_updated;
495 widget_class->state_changed = gtk_combo_box_state_changed;
496 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
497 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
498 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
499 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
500 widget_class->destroy = gtk_combo_box_destroy;
502 object_class = (GObjectClass *)klass;
503 object_class->constructor = gtk_combo_box_constructor;
504 object_class->dispose = gtk_combo_box_dispose;
505 object_class->finalize = gtk_combo_box_finalize;
506 object_class->set_property = gtk_combo_box_set_property;
507 object_class->get_property = gtk_combo_box_get_property;
511 * GtkComboBox::changed:
512 * @widget: the object which received the signal
514 * The changed signal is emitted when the active
515 * item is changed. The can be due to the user selecting
516 * a different item from the list, or due to a
517 * call to gtk_combo_box_set_active_iter().
518 * It will also be emitted while typing into the entry of a combo box
523 combo_box_signals[CHANGED] =
524 g_signal_new (I_("changed"),
525 G_OBJECT_CLASS_TYPE (klass),
527 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
529 g_cclosure_marshal_VOID__VOID,
532 * GtkComboBox::move-active:
533 * @widget: the object that received the signal
534 * @scroll_type: a #GtkScrollType
536 * The ::move-active signal is a
537 * <link linkend="keybinding-signals">keybinding signal</link>
538 * which gets emitted to move the active selection.
542 combo_box_signals[MOVE_ACTIVE] =
543 g_signal_new_class_handler (I_("move-active"),
544 G_OBJECT_CLASS_TYPE (klass),
545 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
546 G_CALLBACK (gtk_combo_box_real_move_active),
548 g_cclosure_marshal_VOID__ENUM,
550 GTK_TYPE_SCROLL_TYPE);
553 * GtkComboBox::popup:
554 * @widget: the object that received the signal
556 * The ::popup signal is a
557 * <link linkend="keybinding-signals">keybinding signal</link>
558 * which gets emitted to popup the combo box list.
560 * The default binding for this signal is Alt+Down.
564 combo_box_signals[POPUP] =
565 g_signal_new_class_handler (I_("popup"),
566 G_OBJECT_CLASS_TYPE (klass),
567 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
568 G_CALLBACK (gtk_combo_box_real_popup),
570 g_cclosure_marshal_VOID__VOID,
573 * GtkComboBox::popdown:
574 * @button: the object which received the signal
576 * The ::popdown signal is a
577 * <link linkend="keybinding-signals">keybinding signal</link>
578 * which gets emitted to popdown the combo box list.
580 * The default bindings for this signal are Alt+Up and Escape.
584 combo_box_signals[POPDOWN] =
585 g_signal_new_class_handler (I_("popdown"),
586 G_OBJECT_CLASS_TYPE (klass),
587 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
588 G_CALLBACK (gtk_combo_box_real_popdown),
590 _gtk_marshal_BOOLEAN__VOID,
594 binding_set = gtk_binding_set_by_class (widget_class);
596 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
598 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
601 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
603 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
608 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
610 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
611 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
613 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
614 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
616 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
617 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
619 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
620 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
622 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
623 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
625 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
627 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
629 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
630 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
632 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
633 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
635 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
636 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
638 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
639 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
641 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
642 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
644 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
647 g_object_class_override_property (object_class,
648 PROP_EDITING_CANCELED,
654 * The model from which the combo box takes the values shown
659 g_object_class_install_property (object_class,
661 g_param_spec_object ("model",
662 P_("ComboBox model"),
663 P_("The model for the combo box"),
665 GTK_PARAM_READWRITE));
668 * GtkComboBox:wrap-width:
670 * If wrap-width is set to a positive value, the list will be
671 * displayed in multiple columns, the number of columns is
672 * determined by wrap-width.
676 g_object_class_install_property (object_class,
678 g_param_spec_int ("wrap-width",
680 P_("Wrap width for laying out the items in a grid"),
684 GTK_PARAM_READWRITE));
688 * GtkComboBox:row-span-column:
690 * If this is set to a non-negative value, it must be the index of a column
691 * of type %G_TYPE_INT in the model.
693 * The values of that column are used to determine how many rows a value in
694 * the list will span. Therefore, the values in the model column pointed to
695 * by this property must be greater than zero and not larger than wrap-width.
699 g_object_class_install_property (object_class,
700 PROP_ROW_SPAN_COLUMN,
701 g_param_spec_int ("row-span-column",
702 P_("Row span column"),
703 P_("TreeModel column containing the row span values"),
707 GTK_PARAM_READWRITE));
711 * GtkComboBox:column-span-column:
713 * If this is set to a non-negative value, it must be the index of a column
714 * of type %G_TYPE_INT in the model.
716 * The values of that column are used to determine how many columns a value
717 * in the list will span.
721 g_object_class_install_property (object_class,
722 PROP_COLUMN_SPAN_COLUMN,
723 g_param_spec_int ("column-span-column",
724 P_("Column span column"),
725 P_("TreeModel column containing the column span values"),
729 GTK_PARAM_READWRITE));
733 * GtkComboBox:active:
735 * The item which is currently active. If the model is a non-flat treemodel,
736 * and the active item is not an immediate child of the root of the tree,
737 * this property has the value
738 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
739 * where <literal>path</literal> is the #GtkTreePath of the active item.
743 g_object_class_install_property (object_class,
745 g_param_spec_int ("active",
747 P_("The item which is currently active"),
751 GTK_PARAM_READWRITE));
754 * GtkComboBox:add-tearoffs:
756 * The add-tearoffs property controls whether generated menus
757 * have tearoff menu items.
759 * Note that this only affects menu style combo boxes.
763 g_object_class_install_property (object_class,
765 g_param_spec_boolean ("add-tearoffs",
766 P_("Add tearoffs to menus"),
767 P_("Whether dropdowns should have a tearoff menu item"),
769 GTK_PARAM_READWRITE));
772 * GtkComboBox:has-frame:
774 * The has-frame property controls whether a frame
775 * is drawn around the entry.
779 g_object_class_install_property (object_class,
781 g_param_spec_boolean ("has-frame",
783 P_("Whether the combo box draws a frame around the child"),
785 GTK_PARAM_READWRITE));
787 g_object_class_install_property (object_class,
789 g_param_spec_boolean ("focus-on-click",
790 P_("Focus on click"),
791 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
793 GTK_PARAM_READWRITE));
796 * GtkComboBox:tearoff-title:
798 * A title that may be displayed by the window manager
799 * when the popup is torn-off.
803 g_object_class_install_property (object_class,
805 g_param_spec_string ("tearoff-title",
807 P_("A title that may be displayed by the window manager when the popup is torn-off"),
809 GTK_PARAM_READWRITE));
813 * GtkComboBox:popup-shown:
815 * Whether the combo boxes dropdown is popped up.
816 * Note that this property is mainly useful, because
817 * it allows you to connect to notify::popup-shown.
821 g_object_class_install_property (object_class,
823 g_param_spec_boolean ("popup-shown",
825 P_("Whether the combo's dropdown is shown"),
827 GTK_PARAM_READABLE));
831 * GtkComboBox:button-sensitivity:
833 * Whether the dropdown button is sensitive when
834 * the model is empty.
838 g_object_class_install_property (object_class,
839 PROP_BUTTON_SENSITIVITY,
840 g_param_spec_enum ("button-sensitivity",
841 P_("Button Sensitivity"),
842 P_("Whether the dropdown button is sensitive when the model is empty"),
843 GTK_TYPE_SENSITIVITY_TYPE,
844 GTK_SENSITIVITY_AUTO,
845 GTK_PARAM_READWRITE));
848 * GtkComboBox:has-entry:
850 * Whether the combo box has an entry.
854 g_object_class_install_property (object_class,
856 g_param_spec_boolean ("has-entry",
858 P_("Whether combo box has an entry"),
860 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
863 * GtkComboBox:entry-text-column:
865 * The column in the combo box's model to associate with strings from the entry
866 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
870 g_object_class_install_property (object_class,
871 PROP_ENTRY_TEXT_COLUMN,
872 g_param_spec_int ("entry-text-column",
873 P_("Entry Text Column"),
874 P_("The column in the combo box's model to associate "
875 "with strings from the entry if the combo was "
876 "created with #GtkComboBox:has-entry = %TRUE"),
878 GTK_PARAM_READWRITE));
881 * GtkComboBox:id-column:
883 * The column in the combo box's model that provides string
884 * IDs for the values in the model, if != -1.
888 g_object_class_install_property (object_class,
890 g_param_spec_int ("id-column",
892 P_("The column in the combo box's model that provides "
893 "string IDs for the values in the model"),
895 GTK_PARAM_READWRITE));
898 * GtkComboBox:active-id:
900 * The value of the ID column of the active row.
904 g_object_class_install_property (object_class,
906 g_param_spec_string ("active-id",
908 P_("The value of the id column "
909 "for the active row"),
910 NULL, GTK_PARAM_READWRITE));
913 * GtkComboBox:popup-fixed-width:
915 * Whether the popup's width should be a fixed width matching the
916 * allocated width of the combo box.
920 g_object_class_install_property (object_class,
921 PROP_POPUP_FIXED_WIDTH,
922 g_param_spec_boolean ("popup-fixed-width",
923 P_("Popup Fixed Width"),
924 P_("Whether the popup's width should be a "
925 "fixed width matching the allocated width "
928 GTK_PARAM_READWRITE));
931 * GtkComboBox:cell-area:
933 * The #GtkCellArea used to layout cell renderers for this combo box.
937 g_object_class_install_property (object_class,
939 g_param_spec_object ("cell-area",
941 P_("The GtkCellArea used to layout cells"),
943 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
945 gtk_widget_class_install_style_property (widget_class,
946 g_param_spec_boolean ("appears-as-list",
947 P_("Appears as list"),
948 P_("Whether dropdowns should look like lists rather than menus"),
950 GTK_PARAM_READABLE));
953 * GtkComboBox:arrow-size:
955 * Sets the minimum size of the arrow in the combo box. Note
956 * that the arrow size is coupled to the font size, so in case
957 * a larger font is used, the arrow will be larger than set
962 gtk_widget_class_install_style_property (widget_class,
963 g_param_spec_int ("arrow-size",
965 P_("The minimum size of the arrow in the combo box"),
969 GTK_PARAM_READABLE));
972 * GtkComboBox:shadow-type:
974 * Which kind of shadow to draw around the combo box.
978 gtk_widget_class_install_style_property (widget_class,
979 g_param_spec_enum ("shadow-type",
981 P_("Which kind of shadow to draw around the combo box"),
982 GTK_TYPE_SHADOW_TYPE,
984 GTK_PARAM_READABLE));
986 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
990 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
992 parent_buildable_iface = g_type_interface_peek_parent (iface);
993 iface->add_child = _gtk_cell_layout_buildable_add_child;
994 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
995 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
996 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1000 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1002 iface->get_area = gtk_combo_box_cell_layout_get_area;
1006 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1008 iface->start_editing = gtk_combo_box_start_editing;
1012 gtk_combo_box_init (GtkComboBox *combo_box)
1014 GtkComboBoxPrivate *priv;
1016 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1018 GtkComboBoxPrivate);
1019 priv = combo_box->priv;
1021 priv->wrap_width = 0;
1024 priv->active_row = NULL;
1025 priv->col_column = -1;
1026 priv->row_column = -1;
1028 priv->popup_shown = FALSE;
1029 priv->add_tearoffs = FALSE;
1030 priv->has_frame = TRUE;
1031 priv->is_cell_renderer = FALSE;
1032 priv->editing_canceled = FALSE;
1033 priv->auto_scroll = FALSE;
1034 priv->focus_on_click = TRUE;
1035 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1036 priv->has_entry = FALSE;
1037 priv->popup_fixed_width = TRUE;
1039 priv->text_column = -1;
1040 priv->text_renderer = NULL;
1041 priv->id_column = -1;
1045 gtk_combo_box_set_property (GObject *object,
1047 const GValue *value,
1050 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1056 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1059 case PROP_WRAP_WIDTH:
1060 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1063 case PROP_ROW_SPAN_COLUMN:
1064 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1067 case PROP_COLUMN_SPAN_COLUMN:
1068 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1072 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1075 case PROP_ADD_TEAROFFS:
1076 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1079 case PROP_HAS_FRAME:
1080 combo_box->priv->has_frame = g_value_get_boolean (value);
1082 if (combo_box->priv->has_entry)
1086 child = gtk_bin_get_child (GTK_BIN (combo_box));
1088 gtk_entry_set_has_frame (GTK_ENTRY (child),
1089 combo_box->priv->has_frame);
1094 case PROP_FOCUS_ON_CLICK:
1095 gtk_combo_box_set_focus_on_click (combo_box,
1096 g_value_get_boolean (value));
1099 case PROP_TEAROFF_TITLE:
1100 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1103 case PROP_POPUP_SHOWN:
1104 if (g_value_get_boolean (value))
1105 gtk_combo_box_popup (combo_box);
1107 gtk_combo_box_popdown (combo_box);
1110 case PROP_BUTTON_SENSITIVITY:
1111 gtk_combo_box_set_button_sensitivity (combo_box,
1112 g_value_get_enum (value));
1115 case PROP_POPUP_FIXED_WIDTH:
1116 gtk_combo_box_set_popup_fixed_width (combo_box,
1117 g_value_get_boolean (value));
1120 case PROP_EDITING_CANCELED:
1121 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1124 case PROP_HAS_ENTRY:
1125 combo_box->priv->has_entry = g_value_get_boolean (value);
1128 case PROP_ENTRY_TEXT_COLUMN:
1129 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1132 case PROP_ID_COLUMN:
1133 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1136 case PROP_ACTIVE_ID:
1137 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1140 case PROP_CELL_AREA:
1141 /* Construct-only, can only be assigned once */
1142 area = g_value_get_object (value);
1145 combo_box->priv->area = g_object_ref_sink (area);
1149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1155 gtk_combo_box_get_property (GObject *object,
1160 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1161 GtkComboBoxPrivate *priv = combo_box->priv;
1166 g_value_set_object (value, combo_box->priv->model);
1169 case PROP_WRAP_WIDTH:
1170 g_value_set_int (value, combo_box->priv->wrap_width);
1173 case PROP_ROW_SPAN_COLUMN:
1174 g_value_set_int (value, combo_box->priv->row_column);
1177 case PROP_COLUMN_SPAN_COLUMN:
1178 g_value_set_int (value, combo_box->priv->col_column);
1182 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1185 case PROP_ADD_TEAROFFS:
1186 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1189 case PROP_HAS_FRAME:
1190 g_value_set_boolean (value, combo_box->priv->has_frame);
1193 case PROP_FOCUS_ON_CLICK:
1194 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1197 case PROP_TEAROFF_TITLE:
1198 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1201 case PROP_POPUP_SHOWN:
1202 g_value_set_boolean (value, combo_box->priv->popup_shown);
1205 case PROP_BUTTON_SENSITIVITY:
1206 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1209 case PROP_POPUP_FIXED_WIDTH:
1210 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1213 case PROP_EDITING_CANCELED:
1214 g_value_set_boolean (value, priv->editing_canceled);
1217 case PROP_HAS_ENTRY:
1218 g_value_set_boolean (value, priv->has_entry);
1221 case PROP_ENTRY_TEXT_COLUMN:
1222 g_value_set_int (value, priv->text_column);
1225 case PROP_ID_COLUMN:
1226 g_value_set_int (value, priv->id_column);
1229 case PROP_ACTIVE_ID:
1230 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1233 case PROP_CELL_AREA:
1234 g_value_set_object (value, priv->area);
1238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1244 gtk_combo_box_state_changed (GtkWidget *widget,
1245 GtkStateType previous)
1247 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1248 GtkComboBoxPrivate *priv = combo_box->priv;
1250 if (gtk_widget_get_realized (widget))
1252 if (priv->tree_view && priv->cell_view)
1254 GtkStyleContext *context;
1255 GtkStateFlags state;
1258 context = gtk_widget_get_style_context (widget);
1259 state = gtk_widget_get_state_flags (widget);
1261 gtk_style_context_get (context, state,
1262 "background-color", &color,
1265 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
1266 gdk_rgba_free (color);
1270 gtk_widget_queue_draw (widget);
1274 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1275 GtkStateFlags previous,
1278 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1279 GtkComboBoxPrivate *priv = combo_box->priv;
1281 if (gtk_widget_get_realized (widget))
1283 if (!priv->tree_view && priv->cell_view)
1284 gtk_widget_set_state_flags (priv->cell_view,
1285 gtk_widget_get_state_flags (widget),
1289 gtk_widget_queue_draw (widget);
1293 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1295 GtkComboBoxPrivate *priv = combo_box->priv;
1296 gboolean appears_as_list;
1298 /* if wrap_width > 0, then we are in grid-mode and forced to use
1301 if (priv->wrap_width)
1302 appears_as_list = FALSE;
1304 gtk_widget_style_get (GTK_WIDGET (combo_box),
1305 "appears-as-list", &appears_as_list,
1308 if (appears_as_list)
1310 /* Destroy all the menu mode widgets, if they exist. */
1311 if (GTK_IS_MENU (priv->popup_widget))
1312 gtk_combo_box_menu_destroy (combo_box);
1314 /* Create the list mode widgets, if they don't already exist. */
1315 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1316 gtk_combo_box_list_setup (combo_box);
1320 /* Destroy all the list mode widgets, if they exist. */
1321 if (GTK_IS_TREE_VIEW (priv->tree_view))
1322 gtk_combo_box_list_destroy (combo_box);
1324 /* Create the menu mode widgets, if they don't already exist. */
1325 if (!GTK_IS_MENU (priv->popup_widget))
1326 gtk_combo_box_menu_setup (combo_box, TRUE);
1329 gtk_widget_style_get (GTK_WIDGET (combo_box),
1330 "shadow-type", &priv->shadow_type,
1335 gtk_combo_box_style_updated (GtkWidget *widget)
1337 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1338 GtkComboBoxPrivate *priv = combo_box->priv;
1341 gtk_combo_box_check_appearance (combo_box);
1343 if (priv->tree_view && priv->cell_view)
1345 GtkStyleContext *context;
1348 context = gtk_widget_get_style_context (widget);
1349 gtk_style_context_get (context, 0,
1350 "background-color", &color,
1353 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view),
1356 gdk_rgba_free (color);
1359 child = gtk_bin_get_child (GTK_BIN (combo_box));
1360 if (GTK_IS_ENTRY (child))
1361 g_object_set (child, "shadow-type",
1362 GTK_SHADOW_NONE == priv->shadow_type ?
1363 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1367 gtk_combo_box_button_toggled (GtkWidget *widget,
1370 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1372 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1374 if (!combo_box->priv->popup_in_progress)
1375 gtk_combo_box_popup (combo_box);
1378 gtk_combo_box_popdown (combo_box);
1382 gtk_combo_box_add (GtkContainer *container,
1385 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1386 GtkComboBoxPrivate *priv = combo_box->priv;
1388 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1390 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1391 "(need an instance of GtkEntry or of a subclass)",
1392 G_OBJECT_TYPE_NAME (widget));
1396 if (priv->cell_view &&
1397 gtk_widget_get_parent (priv->cell_view))
1399 gtk_widget_unparent (priv->cell_view);
1400 _gtk_bin_set_child (GTK_BIN (container), NULL);
1401 gtk_widget_queue_resize (GTK_WIDGET (container));
1404 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1405 _gtk_bin_set_child (GTK_BIN (container), widget);
1407 if (priv->cell_view &&
1408 widget != priv->cell_view)
1410 /* since the cell_view was unparented, it's gone now */
1411 priv->cell_view = NULL;
1413 if (!priv->tree_view && priv->separator)
1415 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1417 priv->separator = NULL;
1419 gtk_widget_queue_resize (GTK_WIDGET (container));
1421 else if (priv->cell_view_frame)
1423 gtk_widget_unparent (priv->cell_view_frame);
1424 priv->cell_view_frame = NULL;
1429 if (priv->has_entry)
1431 /* this flag is a hack to tell the entry to fill its allocation.
1433 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1435 g_signal_connect (widget, "changed",
1436 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1439 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1444 gtk_combo_box_remove (GtkContainer *container,
1447 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1448 GtkComboBoxPrivate *priv = combo_box->priv;
1450 gboolean appears_as_list;
1452 if (priv->has_entry)
1454 GtkWidget *child_widget;
1456 child_widget = gtk_bin_get_child (GTK_BIN (container));
1457 if (widget && widget == child_widget)
1459 g_signal_handlers_disconnect_by_func (widget,
1460 gtk_combo_box_entry_contents_changed,
1462 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1466 if (widget == priv->cell_view)
1467 priv->cell_view = NULL;
1469 gtk_widget_unparent (widget);
1470 _gtk_bin_set_child (GTK_BIN (container), NULL);
1472 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1475 gtk_widget_queue_resize (GTK_WIDGET (container));
1477 if (!priv->tree_view)
1478 appears_as_list = FALSE;
1480 appears_as_list = TRUE;
1482 if (appears_as_list)
1483 gtk_combo_box_list_destroy (combo_box);
1484 else if (GTK_IS_MENU (priv->popup_widget))
1486 gtk_combo_box_menu_destroy (combo_box);
1487 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1488 priv->popup_widget = NULL;
1491 if (!priv->cell_view)
1493 priv->cell_view = gtk_cell_view_new ();
1494 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1495 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1497 gtk_widget_show (priv->cell_view);
1498 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1503 if (appears_as_list)
1504 gtk_combo_box_list_setup (combo_box);
1506 gtk_combo_box_menu_setup (combo_box, TRUE);
1508 if (gtk_tree_row_reference_valid (priv->active_row))
1510 path = gtk_tree_row_reference_get_path (priv->active_row);
1511 gtk_combo_box_set_active_internal (combo_box, path);
1512 gtk_tree_path_free (path);
1515 gtk_combo_box_set_active_internal (combo_box, NULL);
1519 gtk_combo_box_menu_show (GtkWidget *menu,
1522 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1523 GtkComboBoxPrivate *priv = combo_box->priv;
1525 gtk_combo_box_child_show (menu, user_data);
1527 priv->popup_in_progress = TRUE;
1528 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1530 priv->popup_in_progress = FALSE;
1534 gtk_combo_box_menu_hide (GtkWidget *menu,
1537 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1539 gtk_combo_box_child_hide (menu,user_data);
1541 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1546 gtk_combo_box_detacher (GtkWidget *widget,
1549 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1550 GtkComboBoxPrivate *priv = combo_box->priv;
1552 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1554 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1555 gtk_combo_box_menu_show,
1557 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1558 gtk_combo_box_menu_hide,
1561 priv->popup_widget = NULL;
1565 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1568 GtkComboBoxPrivate *priv = combo_box->priv;
1570 if (GTK_IS_MENU (priv->popup_widget))
1572 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1573 priv->popup_widget = NULL;
1575 else if (priv->popup_widget)
1577 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1578 priv->popup_widget);
1579 g_object_unref (priv->popup_widget);
1580 priv->popup_widget = NULL;
1583 if (GTK_IS_MENU (popup))
1585 if (priv->popup_window)
1587 gtk_widget_destroy (priv->popup_window);
1588 priv->popup_window = NULL;
1591 priv->popup_widget = popup;
1594 * Note that we connect to show/hide on the toplevel, not the
1595 * menu itself, since the menu is not shown/hidden when it is
1596 * popped up while torn-off.
1598 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1599 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1600 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1601 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1603 gtk_menu_attach_to_widget (GTK_MENU (popup),
1604 GTK_WIDGET (combo_box),
1605 gtk_combo_box_detacher);
1609 if (!priv->popup_window)
1611 GtkWidget *toplevel;
1613 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1614 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1616 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1617 GDK_WINDOW_TYPE_HINT_COMBO);
1619 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1620 G_CALLBACK (gtk_combo_box_child_show),
1622 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1623 G_CALLBACK (gtk_combo_box_child_hide),
1626 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1627 if (GTK_IS_WINDOW (toplevel))
1629 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1630 GTK_WINDOW (priv->popup_window));
1631 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1632 GTK_WINDOW (toplevel));
1635 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1636 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1637 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1639 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1641 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1644 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1647 gtk_widget_show (priv->scrolled_window);
1649 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1650 priv->scrolled_window);
1653 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1656 gtk_widget_show (popup);
1657 g_object_ref (popup);
1658 priv->popup_widget = popup;
1663 get_widget_border (GtkWidget *widget,
1666 GtkStyleContext *context;
1667 GtkBorder *border_width;
1669 context = gtk_widget_get_style_context (widget);
1671 gtk_style_context_get (context,
1672 gtk_widget_get_state_flags (widget),
1673 "border-width", &border_width,
1676 *border = *border_width;
1677 gtk_border_free (border_width);
1681 gtk_combo_box_menu_position_below (GtkMenu *menu,
1687 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1688 GtkAllocation child_allocation;
1694 GdkRectangle monitor;
1697 /* FIXME: is using the size request here broken? */
1698 child = gtk_bin_get_child (GTK_BIN (combo_box));
1702 gtk_widget_get_allocation (child, &child_allocation);
1704 if (!gtk_widget_get_has_window (child))
1706 sx += child_allocation.x;
1707 sy += child_allocation.y;
1710 gdk_window_get_root_coords (gtk_widget_get_window (child),
1712 get_widget_border (GTK_WIDGET (combo_box), &border);
1715 if (combo_box->priv->popup_fixed_width)
1716 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1718 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1720 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1723 *x = sx + child_allocation.width - req.width;
1726 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1727 monitor_num = gdk_screen_get_monitor_at_window (screen,
1728 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1729 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1733 else if (*x + req.width > monitor.x + monitor.width)
1734 *x = monitor.x + monitor.width - req.width;
1736 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1737 *y += child_allocation.height;
1738 else if (*y - monitor.y >= req.height)
1740 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1741 *y += child_allocation.height;
1749 gtk_combo_box_menu_position_over (GtkMenu *menu,
1755 GtkComboBox *combo_box;
1759 GtkAllocation allocation;
1760 GtkAllocation child_allocation;
1767 combo_box = GTK_COMBO_BOX (user_data);
1768 widget = GTK_WIDGET (combo_box);
1770 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1772 gtk_widget_get_allocation (widget, &allocation);
1774 menu_xpos = allocation.x;
1775 menu_ypos = allocation.y + allocation.height / 2 - 2;
1777 if (combo_box->priv->popup_fixed_width)
1778 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1780 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1784 gtk_widget_get_allocation (active, &child_allocation);
1785 menu_ypos -= child_allocation.height / 2;
1788 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1791 child = children->data;
1793 if (active == child)
1796 if (gtk_widget_get_visible (child))
1798 gtk_widget_get_allocation (child, &child_allocation);
1800 menu_ypos -= child_allocation.height;
1803 children = children->next;
1806 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1807 menu_xpos = menu_xpos + allocation.width - menu_width;
1809 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1810 menu_xpos, menu_ypos,
1811 &menu_xpos, &menu_ypos);
1813 /* Clamp the position on screen */
1814 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1818 else if ((menu_xpos + menu_width) > screen_width)
1819 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1828 gtk_combo_box_menu_position (GtkMenu *menu,
1834 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1835 GtkComboBoxPrivate *priv = combo_box->priv;
1836 GtkWidget *menu_item;
1838 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1839 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1842 /* FIXME handle nested menus better */
1843 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1845 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1848 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1851 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1852 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1853 GDK_WINDOW_TYPE_HINT_COMBO);
1857 gtk_combo_box_list_position (GtkComboBox *combo_box,
1863 GtkComboBoxPrivate *priv = combo_box->priv;
1864 GtkAllocation allocation;
1867 GdkRectangle monitor;
1868 GtkRequisition popup_req;
1869 GtkPolicyType hpolicy, vpolicy;
1872 /* under windows, the drop down list is as wide as the combo box itself.
1874 GtkWidget *widget = GTK_WIDGET (combo_box);
1878 gtk_widget_get_allocation (widget, &allocation);
1880 if (!gtk_widget_get_has_window (widget))
1886 window = gtk_widget_get_window (widget);
1888 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1891 *width = allocation.width;
1893 hpolicy = vpolicy = GTK_POLICY_NEVER;
1894 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1897 if (combo_box->priv->popup_fixed_width)
1899 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1901 if (popup_req.width > *width)
1903 hpolicy = GTK_POLICY_ALWAYS;
1904 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1910 /* XXX This code depends on treeviews properly reporting their natural width
1911 * list-mode menus won't fill up to their natural width until then */
1912 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1914 if (popup_req.width > *width)
1916 hpolicy = GTK_POLICY_NEVER;
1917 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1920 *width = popup_req.width;
1924 *height = popup_req.height;
1926 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1927 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1928 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1930 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1931 *x = *x + allocation.width - *width;
1935 else if (*x + *width > monitor.x + monitor.width)
1936 *x = monitor.x + monitor.width - *width;
1938 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1939 *y += allocation.height;
1940 else if (*y - *height >= monitor.y)
1942 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1944 *y += allocation.height;
1945 *height = monitor.y + monitor.height - *y;
1949 *height = *y - monitor.y;
1953 if (popup_req.height > *height)
1955 vpolicy = GTK_POLICY_ALWAYS;
1957 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1963 cell_view_is_sensitive (GtkCellView *cell_view)
1965 GList *cells, *list;
1968 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1971 for (list = cells; list; list = list->next)
1973 g_object_get (list->data, "sensitive", &sensitive, NULL);
1978 g_list_free (cells);
1984 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1987 GtkComboBoxPrivate *priv = combo_box->priv;
1988 GList *cells, *list;
1994 if (priv->row_separator_func)
1996 if (priv->row_separator_func (priv->model, iter,
1997 priv->row_separator_data))
2001 gtk_tree_view_column_cell_set_cell_data (priv->column,
2003 iter, FALSE, FALSE);
2005 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
2008 for (list = cells; list; list = list->next)
2010 g_object_get (list->data, "sensitive", &sensitive, NULL);
2015 g_list_free (cells);
2021 update_menu_sensitivity (GtkComboBox *combo_box,
2024 GtkComboBoxPrivate *priv = combo_box->priv;
2025 GList *children, *child;
2026 GtkWidget *item, *submenu, *separator;
2027 GtkWidget *cell_view;
2033 children = gtk_container_get_children (GTK_CONTAINER (menu));
2035 for (child = children; child; child = child->next)
2037 item = GTK_WIDGET (child->data);
2038 cell_view = gtk_bin_get_child (GTK_BIN (item));
2040 if (!GTK_IS_CELL_VIEW (cell_view))
2043 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2044 if (submenu != NULL)
2046 gtk_widget_set_sensitive (item, TRUE);
2047 update_menu_sensitivity (combo_box, submenu);
2051 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
2053 if (menu != priv->popup_widget && child == children)
2055 separator = GTK_WIDGET (child->next->data);
2056 g_object_set (item, "visible", sensitive, NULL);
2057 g_object_set (separator, "visible", sensitive, NULL);
2060 gtk_widget_set_sensitive (item, sensitive);
2064 g_list_free (children);
2068 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2070 guint32 activate_time)
2072 GtkComboBoxPrivate *priv = combo_box->priv;
2075 gint width, min_width, nat_width;
2077 update_menu_sensitivity (combo_box, priv->popup_widget);
2080 if (gtk_tree_row_reference_valid (priv->active_row))
2082 path = gtk_tree_row_reference_get_path (priv->active_row);
2083 active_item = gtk_tree_path_get_indices (path)[0];
2084 gtk_tree_path_free (path);
2086 if (priv->add_tearoffs)
2090 /* FIXME handle nested menus better */
2091 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2093 if (priv->wrap_width == 0)
2095 GtkAllocation allocation;
2097 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2098 width = allocation.width;
2099 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2100 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2102 if (combo_box->priv->popup_fixed_width)
2103 width = MAX (width, min_width);
2105 width = MAX (width, nat_width);
2107 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2110 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2112 gtk_combo_box_menu_position, combo_box,
2113 button, activate_time);
2117 popup_grab_on_window (GdkWindow *window,
2118 GdkDevice *keyboard,
2120 guint32 activate_time)
2123 gdk_device_grab (keyboard, window,
2124 GDK_OWNERSHIP_WINDOW, TRUE,
2125 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2126 NULL, activate_time) != GDK_GRAB_SUCCESS)
2130 gdk_device_grab (pointer, window,
2131 GDK_OWNERSHIP_WINDOW, TRUE,
2132 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2133 GDK_POINTER_MOTION_MASK,
2134 NULL, activate_time) != GDK_GRAB_SUCCESS)
2137 gdk_device_ungrab (keyboard, activate_time);
2146 * gtk_combo_box_popup:
2147 * @combo_box: a #GtkComboBox
2149 * Pops up the menu or dropdown list of @combo_box.
2151 * This function is mostly intended for use by accessibility technologies;
2152 * applications should have little use for it.
2157 gtk_combo_box_popup (GtkComboBox *combo_box)
2159 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2161 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2165 * gtk_combo_box_popup_for_device:
2166 * @combo_box: a #GtkComboBox
2167 * @device: a #GdkDevice
2169 * Pops up the menu or dropdown list of @combo_box, the popup window
2170 * will be grabbed so only @device and its associated pointer/keyboard
2171 * are the only #GdkDevice<!-- -->s able to send events to it.
2176 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2179 GtkComboBoxPrivate *priv = combo_box->priv;
2180 gint x, y, width, height;
2181 GtkTreePath *path = NULL, *ppath;
2182 GtkWidget *toplevel;
2183 GdkDevice *keyboard, *pointer;
2186 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2187 g_return_if_fail (GDK_IS_DEVICE (device));
2189 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2192 if (gtk_widget_get_mapped (priv->popup_widget))
2195 if (priv->grab_pointer && priv->grab_keyboard)
2198 time = gtk_get_current_event_time ();
2200 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2203 pointer = gdk_device_get_associated_device (device);
2208 keyboard = gdk_device_get_associated_device (device);
2211 if (GTK_IS_MENU (priv->popup_widget))
2213 gtk_combo_box_menu_popup (combo_box,
2214 priv->activate_button,
2215 priv->activate_time);
2219 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2220 if (GTK_IS_WINDOW (toplevel))
2221 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2222 GTK_WINDOW (priv->popup_window));
2224 gtk_widget_show_all (priv->scrolled_window);
2225 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2227 gtk_widget_set_size_request (priv->popup_window, width, height);
2228 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2230 if (gtk_tree_row_reference_valid (priv->active_row))
2232 path = gtk_tree_row_reference_get_path (priv->active_row);
2233 ppath = gtk_tree_path_copy (path);
2234 if (gtk_tree_path_up (ppath))
2235 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2237 gtk_tree_path_free (ppath);
2239 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2243 gtk_widget_show (priv->popup_window);
2247 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2249 gtk_tree_path_free (path);
2252 gtk_widget_grab_focus (priv->popup_window);
2253 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2256 if (!gtk_widget_has_focus (priv->tree_view))
2257 gtk_widget_grab_focus (priv->tree_view);
2259 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2260 keyboard, pointer, time))
2262 gtk_widget_hide (priv->popup_window);
2266 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2267 priv->grab_pointer = pointer;
2268 priv->grab_keyboard = keyboard;
2272 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2276 device = gtk_get_current_event_device ();
2280 GdkDeviceManager *device_manager;
2281 GdkDisplay *display;
2284 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2285 device_manager = gdk_display_get_device_manager (display);
2287 /* No device was set, pick the first master device */
2288 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2289 device = devices->data;
2290 g_list_free (devices);
2293 gtk_combo_box_popup_for_device (combo_box, device);
2297 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2299 if (combo_box->priv->popup_shown)
2301 gtk_combo_box_popdown (combo_box);
2309 * gtk_combo_box_popdown:
2310 * @combo_box: a #GtkComboBox
2312 * Hides the menu or dropdown list of @combo_box.
2314 * This function is mostly intended for use by accessibility technologies;
2315 * applications should have little use for it.
2320 gtk_combo_box_popdown (GtkComboBox *combo_box)
2322 GtkComboBoxPrivate *priv = combo_box->priv;
2324 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2326 if (GTK_IS_MENU (priv->popup_widget))
2328 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2332 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2335 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2336 gtk_widget_hide (priv->popup_window);
2337 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2340 priv->grab_pointer = NULL;
2341 priv->grab_keyboard = NULL;
2344 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2345 gtk_widget_get_preferred_size (combo_box->priv->button, \
2349 child.x = allocation->x + border.right; \
2351 child.x = allocation->x + allocation->width - req.width - border.left; \
2353 child.y = allocation->y + border.top; \
2354 child.width = req.width; \
2355 child.height = allocation->height - (border.top + border.bottom); \
2356 child.width = MAX (1, child.width); \
2357 child.height = MAX (1, child.height); \
2359 gtk_widget_size_allocate (combo_box->priv->button, &child);
2363 gtk_combo_box_size_allocate (GtkWidget *widget,
2364 GtkAllocation *allocation)
2366 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2367 GtkComboBoxPrivate *priv = combo_box->priv;
2368 GtkWidget *child_widget;
2369 gint focus_width, focus_pad;
2370 GtkAllocation child;
2372 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2375 gtk_widget_set_allocation (widget, allocation);
2376 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2377 get_widget_border (widget, &border);
2379 gtk_widget_style_get (widget,
2380 "focus-line-width", &focus_width,
2381 "focus-padding", &focus_pad,
2384 if (!priv->tree_view)
2386 if (priv->cell_view)
2388 GtkBorder button_border;
2393 allocation->x += border.left;
2394 allocation->y += border.top;
2395 allocation->width -= border.left + border.right;
2396 allocation->height -= border.top + border.bottom;
2398 gtk_widget_size_allocate (priv->button, allocation);
2400 /* set some things ready */
2401 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2402 get_widget_border (priv->button, &button_border);
2404 child.x = allocation->x;
2405 child.y = allocation->y;
2406 width = allocation->width;
2407 child.height = allocation->height;
2409 if (!priv->is_cell_renderer)
2411 child.x += border_width + button_border.left + focus_width + focus_pad;
2412 child.y += border_width + button_border.top + focus_width + focus_pad;
2413 width -= (2 * (border_width + focus_width + focus_pad)) +
2414 button_border.left + button_border.right;
2415 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2416 button_border.top + button_border.bottom;
2419 /* handle the children */
2420 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2421 child.width = req.width;
2423 child.x += width - req.width;
2424 child.width = MAX (1, child.width);
2425 child.height = MAX (1, child.height);
2426 gtk_widget_size_allocate (priv->arrow, &child);
2428 child.x += req.width;
2429 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2430 child.width = req.width;
2432 child.x -= req.width;
2433 child.width = MAX (1, child.width);
2434 child.height = MAX (1, child.height);
2435 gtk_widget_size_allocate (priv->separator, &child);
2439 child.x += req.width;
2440 child.width = allocation->x + allocation->width
2441 - (border_width + button_border.right + focus_width + focus_pad)
2446 child.width = child.x;
2447 child.x = allocation->x
2448 + border_width + button_border.left + focus_width + focus_pad;
2449 child.width -= child.x;
2452 if (gtk_widget_get_visible (priv->popup_widget))
2454 gint width, menu_width;
2456 if (priv->wrap_width == 0)
2458 GtkAllocation combo_box_allocation;
2460 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2461 width = combo_box_allocation.width;
2462 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2464 if (combo_box->priv->popup_fixed_width)
2465 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2467 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2469 gtk_widget_set_size_request (priv->popup_widget,
2470 MAX (width, menu_width), -1);
2473 /* reposition the menu after giving it a new width */
2474 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2477 child.width = MAX (1, child.width);
2478 child.height = MAX (1, child.height);
2479 gtk_widget_size_allocate (child_widget, &child);
2483 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2486 child.x = allocation->x + req.width + border.right;
2488 child.x = allocation->x + border.left;
2489 child.y = allocation->y + border.top;
2490 child.width = allocation->width - req.width - (border.left + border.right);
2491 child.width = MAX (1, child.width);
2492 child.height = MAX (1, child.height);
2493 gtk_widget_size_allocate (child_widget, &child);
2499 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2502 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2506 child.x = allocation->x + req.width;
2508 child.x = allocation->x;
2510 child.y = allocation->y;
2511 child.width = allocation->width - req.width;
2512 child.height = allocation->height;
2514 if (priv->cell_view_frame)
2516 child.x += border.left + border_width;
2517 child.y += border.top + border_width;
2518 child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right));
2519 child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom));
2520 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2523 if (priv->has_frame)
2525 GtkBorder frame_border;
2527 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2528 get_widget_border (priv->cell_view_frame, &frame_border);
2530 child.x += border_width + frame_border.left;
2531 child.y += border_width + frame_border.right;
2532 child.width -= (2 * border_width) + frame_border.left + frame_border.right;
2533 child.height -= (2 * border_width) + frame_border.top + frame_border.bottom;
2538 child.x += border.left + border_width;
2539 child.y += border.top + border_width;
2540 child.width -= (2 * border_width) - (border.left + border.right);
2541 child.height -= (2 * border_width) - (border.top + border.bottom);
2544 if (gtk_widget_get_visible (priv->popup_window))
2546 gint x, y, width, height;
2547 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2548 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2549 gtk_widget_set_size_request (priv->popup_window, width, height);
2552 child.width = MAX (1, child.width);
2553 child.height = MAX (1, child.height);
2554 gtk_widget_size_allocate (child_widget, &child);
2558 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2561 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2563 GtkComboBoxPrivate *priv = combo_box->priv;
2567 g_signal_handler_disconnect (priv->model,
2569 g_signal_handler_disconnect (priv->model,
2571 g_signal_handler_disconnect (priv->model,
2572 priv->reordered_id);
2573 g_signal_handler_disconnect (priv->model,
2579 g_object_unref (priv->model);
2583 if (priv->active_row)
2585 gtk_tree_row_reference_free (priv->active_row);
2586 priv->active_row = NULL;
2589 if (priv->cell_view)
2590 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2594 gtk_combo_box_forall (GtkContainer *container,
2595 gboolean include_internals,
2596 GtkCallback callback,
2597 gpointer callback_data)
2599 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2600 GtkComboBoxPrivate *priv = combo_box->priv;
2603 if (include_internals)
2606 (* callback) (priv->button, callback_data);
2607 if (priv->cell_view_frame)
2608 (* callback) (priv->cell_view_frame, callback_data);
2611 child = gtk_bin_get_child (GTK_BIN (container));
2613 (* callback) (child, callback_data);
2617 gtk_combo_box_child_show (GtkWidget *widget,
2618 GtkComboBox *combo_box)
2620 GtkComboBoxPrivate *priv = combo_box->priv;
2622 priv->popup_shown = TRUE;
2623 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2627 gtk_combo_box_child_hide (GtkWidget *widget,
2628 GtkComboBox *combo_box)
2630 GtkComboBoxPrivate *priv = combo_box->priv;
2632 priv->popup_shown = FALSE;
2633 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2637 gtk_combo_box_draw (GtkWidget *widget,
2640 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2641 GtkComboBoxPrivate *priv = combo_box->priv;
2643 if (priv->shadow_type != GTK_SHADOW_NONE)
2645 GtkStyleContext *context;
2646 GtkStateFlags state;
2648 context = gtk_widget_get_style_context (widget);
2649 state = gtk_widget_get_state_flags (widget);
2650 gtk_style_context_set_state (context, state);
2652 gtk_render_background (context, cr, 0, 0,
2653 gtk_widget_get_allocated_width (widget),
2654 gtk_widget_get_allocated_height (widget));
2655 gtk_render_frame (context, cr, 0, 0,
2656 gtk_widget_get_allocated_width (widget),
2657 gtk_widget_get_allocated_height (widget));
2660 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2663 if (priv->tree_view && priv->cell_view_frame)
2665 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2666 priv->cell_view_frame, cr);
2669 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2670 gtk_bin_get_child (GTK_BIN (widget)),
2686 path_visible (GtkTreeView *view,
2692 /* Note that we rely on the fact that collapsed rows don't have nodes
2694 return _gtk_tree_view_find_node (view, path, &tree, &node);
2698 tree_next_func (GtkTreeModel *model,
2703 SearchData *search_data = (SearchData *)data;
2705 if (search_data->found)
2707 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2710 if (search_data->visible &&
2711 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2714 search_data->set = TRUE;
2715 search_data->iter = *iter;
2720 if (gtk_tree_path_compare (path, search_data->path) == 0)
2721 search_data->found = TRUE;
2727 tree_next (GtkComboBox *combo,
2728 GtkTreeModel *model,
2733 SearchData search_data;
2735 search_data.combo = combo;
2736 search_data.path = gtk_tree_model_get_path (model, iter);
2737 search_data.visible = visible;
2738 search_data.found = FALSE;
2739 search_data.set = FALSE;
2741 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2743 *next = search_data.iter;
2745 gtk_tree_path_free (search_data.path);
2747 return search_data.set;
2751 tree_prev_func (GtkTreeModel *model,
2756 SearchData *search_data = (SearchData *)data;
2758 if (gtk_tree_path_compare (path, search_data->path) == 0)
2760 search_data->found = TRUE;
2764 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2767 if (search_data->visible &&
2768 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2771 search_data->set = TRUE;
2772 search_data->iter = *iter;
2778 tree_prev (GtkComboBox *combo,
2779 GtkTreeModel *model,
2784 SearchData search_data;
2786 search_data.combo = combo;
2787 search_data.path = gtk_tree_model_get_path (model, iter);
2788 search_data.visible = visible;
2789 search_data.found = FALSE;
2790 search_data.set = FALSE;
2792 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2794 *prev = search_data.iter;
2796 gtk_tree_path_free (search_data.path);
2798 return search_data.set;
2802 tree_last_func (GtkTreeModel *model,
2807 SearchData *search_data = (SearchData *)data;
2809 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2812 /* Note that we rely on the fact that collapsed rows don't have nodes
2814 if (search_data->visible &&
2815 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2818 search_data->set = TRUE;
2819 search_data->iter = *iter;
2825 tree_last (GtkComboBox *combo,
2826 GtkTreeModel *model,
2830 SearchData search_data;
2832 search_data.combo = combo;
2833 search_data.visible = visible;
2834 search_data.set = FALSE;
2836 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2838 *last = search_data.iter;
2840 return search_data.set;
2845 tree_first_func (GtkTreeModel *model,
2850 SearchData *search_data = (SearchData *)data;
2852 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2855 if (search_data->visible &&
2856 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2859 search_data->set = TRUE;
2860 search_data->iter = *iter;
2866 tree_first (GtkComboBox *combo,
2867 GtkTreeModel *model,
2871 SearchData search_data;
2873 search_data.combo = combo;
2874 search_data.visible = visible;
2875 search_data.set = FALSE;
2877 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2879 *first = search_data.iter;
2881 return search_data.set;
2885 gtk_combo_box_scroll_event (GtkWidget *widget,
2886 GdkEventScroll *event)
2888 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2891 GtkTreeIter new_iter;
2893 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2896 if (event->direction == GDK_SCROLL_UP)
2897 found = tree_prev (combo_box, combo_box->priv->model,
2898 &iter, &new_iter, FALSE);
2900 found = tree_next (combo_box, combo_box->priv->model,
2901 &iter, &new_iter, FALSE);
2904 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2913 gtk_combo_box_row_separator_func (GtkTreeModel *model,
2917 GtkComboBoxPrivate *priv = combo->priv;
2919 if (priv->row_separator_func)
2920 return priv->row_separator_func (model, iter, priv->row_separator_data);
2926 gtk_combo_box_header_func (GtkTreeModel *model,
2930 /* Every submenu has a selectable header, however we
2931 * can expose a method to make that configurable by
2932 * the user (like row_separator_func is done) */
2937 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2938 gboolean add_children)
2940 GtkComboBoxPrivate *priv = combo_box->priv;
2944 child = gtk_bin_get_child (GTK_BIN (combo_box));
2946 if (priv->cell_view)
2948 priv->button = gtk_toggle_button_new ();
2949 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2950 priv->focus_on_click);
2952 g_signal_connect (priv->button, "toggled",
2953 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2954 gtk_widget_set_parent (priv->button,
2955 gtk_widget_get_parent (child));
2957 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2958 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2960 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
2961 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2963 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2964 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2966 gtk_widget_show_all (priv->button);
2970 priv->button = gtk_toggle_button_new ();
2971 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2972 priv->focus_on_click);
2974 g_signal_connect (priv->button, "toggled",
2975 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2976 gtk_widget_set_parent (priv->button,
2977 gtk_widget_get_parent (child));
2979 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2980 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2981 gtk_widget_show_all (priv->button);
2984 g_signal_connect (priv->button, "button-press-event",
2985 G_CALLBACK (gtk_combo_box_menu_button_press),
2987 g_signal_connect (priv->button, "state-flags-changed",
2988 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
2991 /* create our funky menu */
2992 menu = _gtk_tree_menu_new_with_area (priv->area);
2993 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2995 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
2997 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
2998 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
2999 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3000 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3001 combo_box->priv->add_tearoffs);
3003 g_signal_connect (menu, "menu-activate",
3004 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3006 /* Chain our row_separator_func through */
3007 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3008 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3011 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3012 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3015 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3017 g_signal_connect (menu, "key-press-event",
3018 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3019 gtk_combo_box_set_popup_widget (combo_box, menu);
3021 gtk_combo_box_update_title (combo_box);
3025 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3027 GtkComboBoxPrivate *priv = combo_box->priv;
3029 g_signal_handlers_disconnect_matched (priv->button,
3030 G_SIGNAL_MATCH_DATA,
3032 gtk_combo_box_menu_button_press, NULL);
3033 g_signal_handlers_disconnect_matched (priv->button,
3034 G_SIGNAL_MATCH_DATA,
3036 gtk_combo_box_button_state_flags_changed, combo_box);
3037 g_signal_handlers_disconnect_matched (priv->popup_widget,
3038 G_SIGNAL_MATCH_DATA,
3040 gtk_combo_box_menu_activate, combo_box);
3042 /* unparent will remove our latest ref */
3043 gtk_widget_unparent (priv->button);
3046 priv->button = NULL;
3048 priv->separator = NULL;
3050 priv->column = NULL;
3052 /* changing the popup window will unref the menu and the children */
3057 gtk_combo_box_menu_button_press (GtkWidget *widget,
3058 GdkEventButton *event,
3061 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3062 GtkComboBoxPrivate *priv = combo_box->priv;
3064 if (GTK_IS_MENU (priv->popup_widget) &&
3065 event->type == GDK_BUTTON_PRESS && event->button == 1)
3067 if (priv->focus_on_click &&
3068 !gtk_widget_has_focus (priv->button))
3069 gtk_widget_grab_focus (priv->button);
3071 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3080 gtk_combo_box_menu_activate (GtkWidget *menu,
3082 GtkComboBox *combo_box)
3086 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3087 gtk_combo_box_set_active_iter (combo_box, &iter);
3089 g_object_set (combo_box,
3090 "editing-canceled", FALSE,
3096 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3099 gboolean sensitive = TRUE; /* fool code checkers */
3101 if (!combo_box->priv->button)
3104 switch (combo_box->priv->button_sensitivity)
3106 case GTK_SENSITIVITY_ON:
3109 case GTK_SENSITIVITY_OFF:
3112 case GTK_SENSITIVITY_AUTO:
3113 sensitive = combo_box->priv->model &&
3114 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3117 g_assert_not_reached ();
3121 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3123 /* In list-mode, we also need to update sensitivity of the event box */
3124 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3125 && combo_box->priv->cell_view)
3126 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3130 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3135 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3137 if (combo_box->priv->tree_view)
3138 gtk_combo_box_list_popup_resize (combo_box);
3140 gtk_combo_box_update_sensitivity (combo_box);
3144 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3148 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3149 GtkComboBoxPrivate *priv = combo_box->priv;
3151 if (!gtk_tree_row_reference_valid (priv->active_row))
3153 if (priv->cell_view)
3154 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3155 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3158 if (priv->tree_view)
3159 gtk_combo_box_list_popup_resize (combo_box);
3161 gtk_combo_box_update_sensitivity (combo_box);
3165 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3171 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3175 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3180 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3181 GtkComboBoxPrivate *priv = combo_box->priv;
3182 GtkTreePath *active_path;
3184 /* FIXME this belongs to GtkCellView */
3185 if (gtk_tree_row_reference_valid (priv->active_row))
3187 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3188 if (gtk_tree_path_compare (path, active_path) == 0 &&
3190 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3191 gtk_tree_path_free (active_path);
3194 if (priv->tree_view)
3195 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3199 list_popup_resize_idle (gpointer user_data)
3201 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3202 GtkComboBoxPrivate *priv = combo_box->priv;
3203 gint x, y, width, height;
3205 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3207 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3209 gtk_widget_set_size_request (priv->popup_window, width, height);
3210 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3213 priv->resize_idle_id = 0;
3219 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3221 GtkComboBoxPrivate *priv = combo_box->priv;
3223 if (!priv->resize_idle_id)
3224 priv->resize_idle_id =
3225 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3229 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3234 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3236 gtk_combo_box_list_popup_resize (combo_box);
3245 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3247 GtkComboBoxPrivate *priv = combo_box->priv;
3248 GtkTreeSelection *sel;
3250 GtkWidget *widget = GTK_WIDGET (combo_box);
3252 priv->button = gtk_toggle_button_new ();
3253 child = gtk_bin_get_child (GTK_BIN (combo_box));
3254 gtk_widget_set_parent (priv->button,
3255 gtk_widget_get_parent (child));
3256 g_signal_connect (priv->button, "button-press-event",
3257 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3258 g_signal_connect (priv->button, "toggled",
3259 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3261 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3262 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3263 priv->separator = NULL;
3264 gtk_widget_show_all (priv->button);
3266 if (priv->cell_view)
3268 GtkStyleContext *context;
3269 GtkStateFlags state;
3272 context = gtk_widget_get_style_context (widget);
3273 state = gtk_widget_get_state_flags (widget);
3275 gtk_style_context_get (context, state,
3276 "background-color", &color,
3279 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
3280 gdk_rgba_free (color);
3282 priv->box = gtk_event_box_new ();
3283 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3286 if (priv->has_frame)
3288 priv->cell_view_frame = gtk_frame_new (NULL);
3289 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3294 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3295 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3299 gtk_widget_set_parent (priv->cell_view_frame,
3300 gtk_widget_get_parent (child));
3301 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3302 gtk_widget_show_all (priv->cell_view_frame);
3304 g_signal_connect (priv->box, "button-press-event",
3305 G_CALLBACK (gtk_combo_box_list_button_pressed),
3309 priv->tree_view = gtk_tree_view_new ();
3310 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3311 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3312 gtk_tree_selection_set_select_function (sel,
3313 gtk_combo_box_list_select_func,
3315 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3317 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3320 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3321 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3325 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3327 priv->column = gtk_tree_view_column_new_with_area (priv->area);
3328 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3330 if (gtk_tree_row_reference_valid (priv->active_row))
3334 path = gtk_tree_row_reference_get_path (priv->active_row);
3335 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3337 gtk_tree_path_free (path);
3340 /* set sample/popup widgets */
3341 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3343 g_signal_connect (priv->tree_view, "key-press-event",
3344 G_CALLBACK (gtk_combo_box_list_key_press),
3346 g_signal_connect (priv->tree_view, "enter-notify-event",
3347 G_CALLBACK (gtk_combo_box_list_enter_notify),
3349 g_signal_connect (priv->tree_view, "row-expanded",
3350 G_CALLBACK (gtk_combo_box_model_row_expanded),
3352 g_signal_connect (priv->tree_view, "row-collapsed",
3353 G_CALLBACK (gtk_combo_box_model_row_expanded),
3355 g_signal_connect (priv->popup_window, "button-press-event",
3356 G_CALLBACK (gtk_combo_box_list_button_pressed),
3358 g_signal_connect (priv->popup_window, "button-release-event",
3359 G_CALLBACK (gtk_combo_box_list_button_released),
3362 gtk_widget_show (priv->tree_view);
3364 gtk_combo_box_update_sensitivity (combo_box);
3368 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3370 GtkComboBoxPrivate *priv = combo_box->priv;
3372 /* disconnect signals */
3373 g_signal_handlers_disconnect_matched (priv->tree_view,
3374 G_SIGNAL_MATCH_DATA,
3375 0, 0, NULL, NULL, combo_box);
3376 g_signal_handlers_disconnect_matched (priv->button,
3377 G_SIGNAL_MATCH_DATA,
3379 gtk_combo_box_list_button_pressed,
3381 g_signal_handlers_disconnect_matched (priv->popup_window,
3382 G_SIGNAL_MATCH_DATA,
3384 gtk_combo_box_list_button_pressed,
3386 g_signal_handlers_disconnect_matched (priv->popup_window,
3387 G_SIGNAL_MATCH_DATA,
3389 gtk_combo_box_list_button_released,
3392 g_signal_handlers_disconnect_matched (priv->popup_window,
3393 G_SIGNAL_MATCH_DATA,
3395 gtk_combo_box_child_show,
3398 g_signal_handlers_disconnect_matched (priv->popup_window,
3399 G_SIGNAL_MATCH_DATA,
3401 gtk_combo_box_child_hide,
3405 g_signal_handlers_disconnect_matched (priv->box,
3406 G_SIGNAL_MATCH_DATA,
3408 gtk_combo_box_list_button_pressed,
3411 /* destroy things (unparent will kill the latest ref from us)
3412 * last unref on button will destroy the arrow
3414 gtk_widget_unparent (priv->button);
3415 priv->button = NULL;
3418 if (priv->cell_view)
3420 g_object_set (priv->cell_view,
3421 "background-set", FALSE,
3425 if (priv->cell_view_frame)
3427 gtk_widget_unparent (priv->cell_view_frame);
3428 priv->cell_view_frame = NULL;
3432 if (priv->scroll_timer)
3434 g_source_remove (priv->scroll_timer);
3435 priv->scroll_timer = 0;
3438 if (priv->resize_idle_id)
3440 g_source_remove (priv->resize_idle_id);
3441 priv->resize_idle_id = 0;
3444 gtk_widget_destroy (priv->tree_view);
3446 priv->tree_view = NULL;
3447 if (priv->popup_widget)
3449 g_object_unref (priv->popup_widget);
3450 priv->popup_widget = NULL;
3457 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3458 GdkEventButton *event,
3461 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3462 GtkComboBoxPrivate *priv = combo_box->priv;
3464 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3466 if (ewidget == priv->popup_window)
3469 if ((ewidget != priv->button && ewidget != priv->box) ||
3470 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3473 if (priv->focus_on_click &&
3474 !gtk_widget_has_focus (priv->button))
3475 gtk_widget_grab_focus (priv->button);
3477 gtk_combo_box_popup_for_device (combo_box, event->device);
3479 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3481 priv->auto_scroll = FALSE;
3482 if (priv->scroll_timer == 0)
3483 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3484 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3487 priv->popup_in_progress = TRUE;
3493 gtk_combo_box_list_button_released (GtkWidget *widget,
3494 GdkEventButton *event,
3498 GtkTreePath *path = NULL;
3501 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3502 GtkComboBoxPrivate *priv = combo_box->priv;
3504 gboolean popup_in_progress = FALSE;
3506 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3508 if (priv->popup_in_progress)
3510 popup_in_progress = TRUE;
3511 priv->popup_in_progress = FALSE;
3514 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3516 if (priv->scroll_timer)
3518 g_source_remove (priv->scroll_timer);
3519 priv->scroll_timer = 0;
3522 if (ewidget != priv->tree_view)
3524 if ((ewidget == priv->button ||
3525 ewidget == priv->box) &&
3526 !popup_in_progress &&
3527 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3529 gtk_combo_box_popdown (combo_box);
3533 /* released outside treeview */
3534 if (ewidget != priv->button && ewidget != priv->box)
3536 gtk_combo_box_popdown (combo_box);
3544 /* select something cool */
3545 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3551 return TRUE; /* clicked outside window? */
3553 gtk_tree_model_get_iter (priv->model, &iter, path);
3554 gtk_tree_path_free (path);
3556 gtk_combo_box_popdown (combo_box);
3558 if (tree_column_row_is_sensitive (combo_box, &iter))
3559 gtk_combo_box_set_active_iter (combo_box, &iter);
3565 gtk_combo_box_menu_key_press (GtkWidget *widget,
3569 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3571 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3573 /* The menu hasn't managed the
3574 * event, forward it to the combobox
3576 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3583 gtk_combo_box_list_key_press (GtkWidget *widget,
3587 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3590 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3591 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3593 GtkTreeModel *model = NULL;
3595 gtk_combo_box_popdown (combo_box);
3597 if (combo_box->priv->model)
3599 GtkTreeSelection *sel;
3601 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3603 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3604 gtk_combo_box_set_active_iter (combo_box, &iter);
3610 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3612 /* The list hasn't managed the
3613 * event, forward it to the combobox
3615 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3622 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3627 GtkAllocation allocation;
3628 GtkWidget *tree_view = combo_box->priv->tree_view;
3631 gtk_widget_get_allocation (tree_view, &allocation);
3633 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3634 if (adj && adj->upper - adj->lower > adj->page_size)
3636 if (x <= allocation.x &&
3637 adj->lower < adj->value)
3639 value = adj->value - (allocation.x - x + 1);
3640 gtk_adjustment_set_value (adj, value);
3642 else if (x >= allocation.x + allocation.width &&
3643 adj->upper - adj->page_size > adj->value)
3645 value = adj->value + (x - allocation.x - allocation.width + 1);
3646 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3650 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3651 if (adj && adj->upper - adj->lower > adj->page_size)
3653 if (y <= allocation.y &&
3654 adj->lower < adj->value)
3656 value = adj->value - (allocation.y - y + 1);
3657 gtk_adjustment_set_value (adj, value);
3659 else if (y >= allocation.height &&
3660 adj->upper - adj->page_size > adj->value)
3662 value = adj->value + (y - allocation.height + 1);
3663 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3669 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3671 GtkComboBoxPrivate *priv = combo_box->priv;
3674 if (priv->auto_scroll)
3676 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3679 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3686 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3687 GdkEventCrossing *event,
3690 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3692 combo_box->priv->auto_scroll = TRUE;
3698 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3699 GtkTreeModel *model,
3701 gboolean path_currently_selected,
3704 GList *list, *columns;
3705 gboolean sensitive = FALSE;
3707 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3709 for (list = columns; list && !sensitive; list = list->next)
3711 GList *cells, *cell;
3712 gboolean cell_sensitive, cell_visible;
3714 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3716 if (!gtk_tree_view_column_get_visible (column))
3719 gtk_tree_model_get_iter (model, &iter, path);
3720 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3723 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3726 g_object_get (cell->data,
3727 "sensitive", &cell_sensitive,
3728 "visible", &cell_visible,
3731 if (cell_visible && cell_sensitive)
3736 g_list_free (cells);
3738 sensitive = cell_sensitive;
3741 g_list_free (columns);
3747 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3752 /* XXX Do nothing ? */
3756 * GtkCellLayout implementation
3759 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3761 return GTK_COMBO_BOX (cell_layout)->priv->area;
3769 * gtk_combo_box_new:
3771 * Creates a new empty #GtkComboBox.
3773 * Return value: A new #GtkComboBox.
3778 gtk_combo_box_new (void)
3780 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3784 * gtk_combo_box_new_with_area:
3785 * @area: the #GtkCellArea to use to layout cell renderers
3787 * Creates a new empty #GtkComboBox using @area to layout cells.
3789 * Return value: A new #GtkComboBox.
3792 gtk_combo_box_new_with_area (GtkCellArea *area)
3794 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3798 * gtk_combo_box_new_with_area_and_entry:
3799 * @area: the #GtkCellArea to use to layout cell renderers
3801 * Creates a new empty #GtkComboBox with an entry.
3803 * The new combo box will use @area to layout cells.
3805 * Return value: A new #GtkComboBox.
3808 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3810 return g_object_new (GTK_TYPE_COMBO_BOX,
3818 * gtk_combo_box_new_with_entry:
3820 * Creates a new empty #GtkComboBox with an entry.
3822 * Return value: A new #GtkComboBox.
3825 gtk_combo_box_new_with_entry (void)
3827 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3831 * gtk_combo_box_new_with_model:
3832 * @model: A #GtkTreeModel.
3834 * Creates a new #GtkComboBox with the model initialized to @model.
3836 * Return value: A new #GtkComboBox.
3841 gtk_combo_box_new_with_model (GtkTreeModel *model)
3843 GtkComboBox *combo_box;
3845 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3847 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3849 return GTK_WIDGET (combo_box);
3853 * gtk_combo_box_new_with_model_and_entry:
3854 * @model: A #GtkTreeModel
3856 * Creates a new empty #GtkComboBox with an entry
3857 * and with the model initialized to @model.
3859 * Return value: A new #GtkComboBox
3862 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3864 return g_object_new (GTK_TYPE_COMBO_BOX,
3871 * gtk_combo_box_get_wrap_width:
3872 * @combo_box: A #GtkComboBox
3874 * Returns the wrap width which is used to determine the number of columns
3875 * for the popup menu. If the wrap width is larger than 1, the combo box
3878 * Returns: the wrap width.
3883 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3885 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3887 return combo_box->priv->wrap_width;
3891 * gtk_combo_box_set_wrap_width:
3892 * @combo_box: A #GtkComboBox
3893 * @width: Preferred number of columns
3895 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3896 * the preferred number of columns when you want the popup to be layed out
3902 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3905 GtkComboBoxPrivate *priv;
3907 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3908 g_return_if_fail (width >= 0);
3910 priv = combo_box->priv;
3912 if (width != priv->wrap_width)
3914 priv->wrap_width = width;
3916 gtk_combo_box_check_appearance (combo_box);
3918 if (GTK_IS_TREE_MENU (priv->popup_widget))
3919 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
3921 g_object_notify (G_OBJECT (combo_box), "wrap-width");
3926 * gtk_combo_box_get_row_span_column:
3927 * @combo_box: A #GtkComboBox
3929 * Returns the column with row span information for @combo_box.
3931 * Returns: the row span column.
3936 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3938 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3940 return combo_box->priv->row_column;
3944 * gtk_combo_box_set_row_span_column:
3945 * @combo_box: A #GtkComboBox.
3946 * @row_span: A column in the model passed during construction.
3948 * Sets the column with row span information for @combo_box to be @row_span.
3949 * The row span column contains integers which indicate how many rows
3950 * an item should span.
3955 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3958 GtkComboBoxPrivate *priv;
3961 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3963 priv = combo_box->priv;
3965 col = gtk_tree_model_get_n_columns (priv->model);
3966 g_return_if_fail (row_span >= -1 && row_span < col);
3968 if (row_span != priv->row_column)
3970 priv->row_column = row_span;
3972 if (GTK_IS_TREE_MENU (priv->popup_widget))
3973 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
3975 g_object_notify (G_OBJECT (combo_box), "row-span-column");
3980 * gtk_combo_box_get_column_span_column:
3981 * @combo_box: A #GtkComboBox
3983 * Returns the column with column span information for @combo_box.
3985 * Returns: the column span column.
3990 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
3992 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3994 return combo_box->priv->col_column;
3998 * gtk_combo_box_set_column_span_column:
3999 * @combo_box: A #GtkComboBox
4000 * @column_span: A column in the model passed during construction
4002 * Sets the column with column span information for @combo_box to be
4003 * @column_span. The column span column contains integers which indicate
4004 * how many columns an item should span.
4009 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4012 GtkComboBoxPrivate *priv;
4015 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4017 priv = combo_box->priv;
4019 col = gtk_tree_model_get_n_columns (priv->model);
4020 g_return_if_fail (column_span >= -1 && column_span < col);
4022 if (column_span != priv->col_column)
4024 priv->col_column = column_span;
4026 if (GTK_IS_TREE_MENU (priv->popup_widget))
4027 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4029 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4034 * gtk_combo_box_get_active:
4035 * @combo_box: A #GtkComboBox
4037 * Returns the index of the currently active item, or -1 if there's no
4038 * active item. If the model is a non-flat treemodel, and the active item
4039 * is not an immediate child of the root of the tree, this function returns
4040 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4041 * <literal>path</literal> is the #GtkTreePath of the active item.
4043 * Return value: An integer which is the index of the currently active item,
4044 * or -1 if there's no active item.
4049 gtk_combo_box_get_active (GtkComboBox *combo_box)
4051 GtkComboBoxPrivate *priv;
4054 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4056 priv = combo_box->priv;
4058 if (gtk_tree_row_reference_valid (priv->active_row))
4062 path = gtk_tree_row_reference_get_path (priv->active_row);
4063 result = gtk_tree_path_get_indices (path)[0];
4064 gtk_tree_path_free (path);
4073 * gtk_combo_box_set_active:
4074 * @combo_box: A #GtkComboBox
4075 * @index_: An index in the model passed during construction, or -1 to have
4078 * Sets the active item of @combo_box to be the item at @index.
4083 gtk_combo_box_set_active (GtkComboBox *combo_box,
4086 GtkTreePath *path = NULL;
4087 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4088 g_return_if_fail (index_ >= -1);
4090 if (combo_box->priv->model == NULL)
4092 /* Save index, in case the model is set after the index */
4093 combo_box->priv->active = index_;
4099 path = gtk_tree_path_new_from_indices (index_, -1);
4101 gtk_combo_box_set_active_internal (combo_box, path);
4104 gtk_tree_path_free (path);
4108 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4111 GtkComboBoxPrivate *priv = combo_box->priv;
4112 GtkTreePath *active_path;
4115 /* Remember whether the initially active row is valid. */
4116 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4118 if (path && is_valid_row_reference)
4120 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4121 path_cmp = gtk_tree_path_compare (path, active_path);
4122 gtk_tree_path_free (active_path);
4127 if (priv->active_row)
4129 gtk_tree_row_reference_free (priv->active_row);
4130 priv->active_row = NULL;
4135 if (priv->tree_view)
4136 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4139 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4141 if (GTK_IS_MENU (menu))
4142 gtk_menu_set_active (menu, -1);
4145 if (priv->cell_view)
4146 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4149 * Do not emit a "changed" signal when an already invalid selection was
4150 * now set to invalid.
4152 if (!is_valid_row_reference)
4158 gtk_tree_row_reference_new (priv->model, path);
4160 if (priv->tree_view)
4162 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4165 else if (GTK_IS_MENU (priv->popup_widget))
4167 /* FIXME handle nested menus better */
4168 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4169 gtk_tree_path_get_indices (path)[0]);
4172 if (priv->cell_view)
4173 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4177 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4178 g_object_notify (G_OBJECT (combo_box), "active");
4179 if (combo_box->priv->id_column >= 0)
4180 g_object_notify (G_OBJECT (combo_box), "active-id");
4185 * gtk_combo_box_get_active_iter:
4186 * @combo_box: A #GtkComboBox
4187 * @iter: (out): The uninitialized #GtkTreeIter
4189 * Sets @iter to point to the current active item, if it exists.
4191 * Return value: %TRUE, if @iter was set
4196 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4202 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4204 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4207 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4208 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4209 gtk_tree_path_free (path);
4215 * gtk_combo_box_set_active_iter:
4216 * @combo_box: A #GtkComboBox
4217 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4219 * Sets the current active item to be the one referenced by @iter, or
4220 * unsets the active item if @iter is %NULL.
4225 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4228 GtkTreePath *path = NULL;
4230 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4233 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4235 gtk_combo_box_set_active_internal (combo_box, path);
4236 gtk_tree_path_free (path);
4240 * gtk_combo_box_set_model:
4241 * @combo_box: A #GtkComboBox
4242 * @model: (allow-none): A #GtkTreeModel
4244 * Sets the model used by @combo_box to be @model. Will unset a previously set
4245 * model (if applicable). If model is %NULL, then it will unset the model.
4247 * Note that this function does not clear the cell renderers, you have to
4248 * call gtk_cell_layout_clear() yourself if you need to set up different
4249 * cell renderers for the new model.
4254 gtk_combo_box_set_model (GtkComboBox *combo_box,
4255 GtkTreeModel *model)
4257 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4258 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4260 if (model == combo_box->priv->model)
4263 gtk_combo_box_unset_model (combo_box);
4268 combo_box->priv->model = model;
4269 g_object_ref (combo_box->priv->model);
4271 combo_box->priv->inserted_id =
4272 g_signal_connect (combo_box->priv->model, "row-inserted",
4273 G_CALLBACK (gtk_combo_box_model_row_inserted),
4275 combo_box->priv->deleted_id =
4276 g_signal_connect (combo_box->priv->model, "row-deleted",
4277 G_CALLBACK (gtk_combo_box_model_row_deleted),
4279 combo_box->priv->reordered_id =
4280 g_signal_connect (combo_box->priv->model, "rows-reordered",
4281 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4283 combo_box->priv->changed_id =
4284 g_signal_connect (combo_box->priv->model, "row-changed",
4285 G_CALLBACK (gtk_combo_box_model_row_changed),
4288 if (combo_box->priv->tree_view)
4291 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4292 combo_box->priv->model);
4293 gtk_combo_box_list_popup_resize (combo_box);
4296 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4299 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4300 combo_box->priv->model);
4303 if (combo_box->priv->cell_view)
4304 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4305 combo_box->priv->model);
4307 if (combo_box->priv->active != -1)
4309 /* If an index was set in advance, apply it now */
4310 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4311 combo_box->priv->active = -1;
4315 gtk_combo_box_update_sensitivity (combo_box);
4317 g_object_notify (G_OBJECT (combo_box), "model");
4321 * gtk_combo_box_get_model:
4322 * @combo_box: A #GtkComboBox
4324 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4326 * Return value: (transfer none): A #GtkTreeModel which was passed
4327 * during construction.
4332 gtk_combo_box_get_model (GtkComboBox *combo_box)
4334 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4336 return combo_box->priv->model;
4340 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4341 GtkScrollType scroll)
4344 GtkTreeIter new_iter;
4345 gboolean active_iter;
4348 if (!combo_box->priv->model)
4350 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4354 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4358 case GTK_SCROLL_STEP_BACKWARD:
4359 case GTK_SCROLL_STEP_UP:
4360 case GTK_SCROLL_STEP_LEFT:
4363 found = tree_prev (combo_box, combo_box->priv->model,
4364 &iter, &new_iter, FALSE);
4367 /* else fall through */
4369 case GTK_SCROLL_PAGE_FORWARD:
4370 case GTK_SCROLL_PAGE_DOWN:
4371 case GTK_SCROLL_PAGE_RIGHT:
4372 case GTK_SCROLL_END:
4373 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4376 case GTK_SCROLL_STEP_FORWARD:
4377 case GTK_SCROLL_STEP_DOWN:
4378 case GTK_SCROLL_STEP_RIGHT:
4381 found = tree_next (combo_box, combo_box->priv->model,
4382 &iter, &new_iter, FALSE);
4385 /* else fall through */
4387 case GTK_SCROLL_PAGE_BACKWARD:
4388 case GTK_SCROLL_PAGE_UP:
4389 case GTK_SCROLL_PAGE_LEFT:
4390 case GTK_SCROLL_START:
4391 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4398 if (found && active_iter)
4400 GtkTreePath *old_path;
4401 GtkTreePath *new_path;
4403 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4404 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4406 if (gtk_tree_path_compare (old_path, new_path) == 0)
4409 gtk_tree_path_free (old_path);
4410 gtk_tree_path_free (new_path);
4415 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4419 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4424 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4425 gboolean group_cycling)
4427 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4429 if (combo_box->priv->has_entry)
4433 child = gtk_bin_get_child (GTK_BIN (combo_box));
4435 gtk_widget_grab_focus (child);
4438 gtk_widget_grab_focus (combo_box->priv->button);
4444 gtk_combo_box_grab_focus (GtkWidget *widget)
4446 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4448 if (combo_box->priv->has_entry)
4452 child = gtk_bin_get_child (GTK_BIN (combo_box));
4454 gtk_widget_grab_focus (child);
4457 gtk_widget_grab_focus (combo_box->priv->button);
4461 gtk_combo_box_destroy (GtkWidget *widget)
4463 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4465 if (combo_box->priv->popup_idle_id > 0)
4467 g_source_remove (combo_box->priv->popup_idle_id);
4468 combo_box->priv->popup_idle_id = 0;
4471 gtk_combo_box_popdown (combo_box);
4473 if (combo_box->priv->row_separator_destroy)
4474 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4476 combo_box->priv->row_separator_func = NULL;
4477 combo_box->priv->row_separator_data = NULL;
4478 combo_box->priv->row_separator_destroy = NULL;
4480 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4481 combo_box->priv->cell_view = NULL;
4485 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4488 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4491 * Fixes regression reported in bug #574059. The old functionality relied on
4492 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4493 * when the selection was already set to -1.
4495 if (gtk_combo_box_get_active(combo_box) == -1)
4496 g_signal_emit_by_name (combo_box, "changed");
4498 gtk_combo_box_set_active (combo_box, -1);
4502 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4505 GtkComboBoxPrivate *priv = combo_box->priv;
4506 GtkTreeModel *model;
4509 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4511 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4515 GValue value = {0,};
4517 g_signal_handlers_block_by_func (entry,
4518 gtk_combo_box_entry_contents_changed,
4521 model = gtk_combo_box_get_model (combo_box);
4523 gtk_tree_model_get_value (model, &iter,
4524 priv->text_column, &value);
4525 g_object_set_property (G_OBJECT (entry), "text", &value);
4526 g_value_unset (&value);
4528 g_signal_handlers_unblock_by_func (entry,
4529 gtk_combo_box_entry_contents_changed,
4536 gtk_combo_box_constructor (GType type,
4537 guint n_construct_properties,
4538 GObjectConstructParam *construct_properties)
4541 GtkComboBox *combo_box;
4542 GtkComboBoxPrivate *priv;
4543 GtkStyleContext *context;
4545 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4546 (type, n_construct_properties, construct_properties);
4548 combo_box = GTK_COMBO_BOX (object);
4549 priv = combo_box->priv;
4553 GtkCellArea *area = gtk_cell_area_box_new ();
4555 priv->area = g_object_ref_sink (area);
4558 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4559 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4560 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4561 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4562 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4563 gtk_widget_show (priv->cell_view);
4565 gtk_combo_box_check_appearance (combo_box);
4567 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4568 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
4570 if (priv->has_entry)
4574 entry = gtk_entry_new ();
4575 gtk_widget_show (entry);
4576 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4578 priv->text_renderer = gtk_cell_renderer_text_new ();
4579 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4580 priv->text_renderer, TRUE);
4582 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4584 g_signal_connect (combo_box, "changed",
4585 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4593 gtk_combo_box_dispose(GObject* object)
4595 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4597 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4599 gtk_combo_box_menu_destroy (combo_box);
4600 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4601 combo_box->priv->popup_widget = NULL;
4604 if (combo_box->priv->area)
4606 g_object_unref (combo_box->priv->area);
4607 combo_box->priv->area = NULL;
4610 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4611 gtk_combo_box_list_destroy (combo_box);
4613 if (combo_box->priv->popup_window)
4615 gtk_widget_destroy (combo_box->priv->popup_window);
4616 combo_box->priv->popup_window = NULL;
4619 gtk_combo_box_unset_model (combo_box);
4621 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4625 gtk_combo_box_finalize (GObject *object)
4627 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4629 g_free (combo_box->priv->tearoff_title);
4631 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4635 gtk_cell_editable_key_press (GtkWidget *widget,
4639 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4641 if (event->keyval == GDK_KEY_Escape)
4643 g_object_set (combo_box,
4644 "editing-canceled", TRUE,
4646 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4647 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4651 else if (event->keyval == GDK_KEY_Return ||
4652 event->keyval == GDK_KEY_ISO_Enter ||
4653 event->keyval == GDK_KEY_KP_Enter)
4655 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4656 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4665 popdown_idle (gpointer data)
4667 GtkComboBox *combo_box;
4669 combo_box = GTK_COMBO_BOX (data);
4671 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4672 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4674 g_object_unref (combo_box);
4680 popdown_handler (GtkWidget *widget,
4683 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4687 popup_idle (gpointer data)
4689 GtkComboBox *combo_box;
4691 combo_box = GTK_COMBO_BOX (data);
4693 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4694 combo_box->priv->cell_view)
4695 g_signal_connect_object (combo_box->priv->popup_widget,
4696 "unmap", G_CALLBACK (popdown_handler),
4699 /* we unset this if a menu item is activated */
4700 g_object_set (combo_box,
4701 "editing-canceled", TRUE,
4703 gtk_combo_box_popup (combo_box);
4705 combo_box->priv->popup_idle_id = 0;
4706 combo_box->priv->activate_button = 0;
4707 combo_box->priv->activate_time = 0;
4713 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4716 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4719 combo_box->priv->is_cell_renderer = TRUE;
4721 if (combo_box->priv->cell_view)
4723 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4724 G_CALLBACK (gtk_cell_editable_key_press),
4727 gtk_widget_grab_focus (combo_box->priv->button);
4731 child = gtk_bin_get_child (GTK_BIN (combo_box));
4733 g_signal_connect_object (child, "key-press-event",
4734 G_CALLBACK (gtk_cell_editable_key_press),
4737 gtk_widget_grab_focus (child);
4738 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4741 /* we do the immediate popup only for the optionmenu-like
4744 if (combo_box->priv->is_cell_renderer &&
4745 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4747 if (event && event->type == GDK_BUTTON_PRESS)
4749 GdkEventButton *event_button = (GdkEventButton *)event;
4751 combo_box->priv->activate_button = event_button->button;
4752 combo_box->priv->activate_time = event_button->time;
4755 combo_box->priv->popup_idle_id =
4756 gdk_threads_add_idle (popup_idle, combo_box);
4762 * gtk_combo_box_get_add_tearoffs:
4763 * @combo_box: a #GtkComboBox
4765 * Gets the current value of the :add-tearoffs property.
4767 * Return value: the current value of the :add-tearoffs property.
4770 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4772 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4774 return combo_box->priv->add_tearoffs;
4778 * gtk_combo_box_set_add_tearoffs:
4779 * @combo_box: a #GtkComboBox
4780 * @add_tearoffs: %TRUE to add tearoff menu items
4782 * Sets whether the popup menu should have a tearoff
4788 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4789 gboolean add_tearoffs)
4791 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4793 add_tearoffs = add_tearoffs != FALSE;
4795 if (combo_box->priv->add_tearoffs != add_tearoffs)
4797 combo_box->priv->add_tearoffs = add_tearoffs;
4798 gtk_combo_box_check_appearance (combo_box);
4800 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4801 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4802 combo_box->priv->add_tearoffs);
4804 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4809 * gtk_combo_box_get_title:
4810 * @combo_box: a #GtkComboBox
4812 * Gets the current title of the menu in tearoff mode. See
4813 * gtk_combo_box_set_add_tearoffs().
4815 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4816 * string which must not be freed.
4820 G_CONST_RETURN gchar*
4821 gtk_combo_box_get_title (GtkComboBox *combo_box)
4823 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4825 return combo_box->priv->tearoff_title;
4829 gtk_combo_box_update_title (GtkComboBox *combo_box)
4831 gtk_combo_box_check_appearance (combo_box);
4833 if (combo_box->priv->popup_widget &&
4834 GTK_IS_MENU (combo_box->priv->popup_widget))
4835 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4836 combo_box->priv->tearoff_title);
4840 * gtk_combo_box_set_title:
4841 * @combo_box: a #GtkComboBox
4842 * @title: a title for the menu in tearoff mode
4844 * Sets the menu's title in tearoff mode.
4849 gtk_combo_box_set_title (GtkComboBox *combo_box,
4852 GtkComboBoxPrivate *priv;
4854 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4856 priv = combo_box->priv;
4858 if (strcmp (title ? title : "",
4859 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4861 g_free (priv->tearoff_title);
4862 priv->tearoff_title = g_strdup (title);
4864 gtk_combo_box_update_title (combo_box);
4866 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4872 * gtk_combo_box_set_popup_fixed_width:
4873 * @combo_box: a #GtkComboBox
4874 * @fixed: whether to use a fixed popup width
4876 * Specifies whether the popup's width should be a fixed width
4877 * matching the allocated width of the combo box.
4882 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4885 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4887 if (combo_box->priv->popup_fixed_width != fixed)
4889 combo_box->priv->popup_fixed_width = fixed;
4891 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
4896 * gtk_combo_box_get_popup_fixed_width:
4897 * @combo_box: a #GtkComboBox
4899 * Gets whether the popup uses a fixed width matching
4900 * the allocated width of the combo box.
4902 * Returns: %TRUE if the popup uses a fixed width
4907 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
4909 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4911 return combo_box->priv->popup_fixed_width;
4916 * gtk_combo_box_get_popup_accessible:
4917 * @combo_box: a #GtkComboBox
4919 * Gets the accessible object corresponding to the combo box's popup.
4921 * This function is mostly intended for use by accessibility technologies;
4922 * applications should have little use for it.
4924 * Returns: (transfer none): the accessible object corresponding
4925 * to the combo box's popup.
4930 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
4934 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4936 if (combo_box->priv->popup_widget)
4938 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
4946 * gtk_combo_box_get_row_separator_func:
4947 * @combo_box: a #GtkComboBox
4949 * Returns the current row separator function.
4951 * Return value: the current row separator function.
4955 GtkTreeViewRowSeparatorFunc
4956 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
4958 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4960 return combo_box->priv->row_separator_func;
4964 * gtk_combo_box_set_row_separator_func:
4965 * @combo_box: a #GtkComboBox
4966 * @func: a #GtkTreeViewRowSeparatorFunc
4967 * @data: (allow-none): user data to pass to @func, or %NULL
4968 * @destroy: (allow-none): destroy notifier for @data, or %NULL
4970 * Sets the row separator function, which is used to determine
4971 * whether a row should be drawn as a separator. If the row separator
4972 * function is %NULL, no separators are drawn. This is the default value.
4977 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
4978 GtkTreeViewRowSeparatorFunc func,
4980 GDestroyNotify destroy)
4982 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4984 if (combo_box->priv->row_separator_destroy)
4985 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4987 combo_box->priv->row_separator_func = func;
4988 combo_box->priv->row_separator_data = data;
4989 combo_box->priv->row_separator_destroy = destroy;
4991 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
4992 if (combo_box->priv->tree_view)
4994 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
4995 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
4998 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5000 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5001 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5004 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5008 * gtk_combo_box_set_button_sensitivity:
5009 * @combo_box: a #GtkComboBox
5010 * @sensitivity: specify the sensitivity of the dropdown button
5012 * Sets whether the dropdown button of the combo box should be
5013 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5014 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5019 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5020 GtkSensitivityType sensitivity)
5022 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5024 if (combo_box->priv->button_sensitivity != sensitivity)
5026 combo_box->priv->button_sensitivity = sensitivity;
5027 gtk_combo_box_update_sensitivity (combo_box);
5029 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5034 * gtk_combo_box_get_button_sensitivity:
5035 * @combo_box: a #GtkComboBox
5037 * Returns whether the combo box sets the dropdown button
5038 * sensitive or not when there are no items in the model.
5040 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5041 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5042 * if the button is always insensitive or
5043 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5044 * the model has one item to be selected.
5049 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5051 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5053 return combo_box->priv->button_sensitivity;
5058 * gtk_combo_box_get_has_entry:
5059 * @combo_box: a #GtkComboBox
5061 * Returns whether the combo box has an entry.
5063 * Return Value: whether there is an entry in @combo_box.
5068 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5070 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5072 return combo_box->priv->has_entry;
5076 * gtk_combo_box_set_entry_text_column:
5077 * @combo_box: A #GtkComboBox
5078 * @text_column: A column in @model to get the strings from for
5079 * the internal entry
5081 * Sets the model column which @combo_box should use to get strings from
5082 * to be @text_column. The column @text_column in the model of @combo_box
5083 * must be of type %G_TYPE_STRING.
5085 * This is only relevant if @combo_box has been created with
5086 * #GtkComboBox:has-entry as %TRUE.
5091 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5094 GtkComboBoxPrivate *priv = combo_box->priv;
5095 GtkTreeModel *model;
5097 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5099 model = gtk_combo_box_get_model (combo_box);
5101 g_return_if_fail (text_column >= 0);
5102 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5104 priv->text_column = text_column;
5106 if (priv->text_renderer != NULL)
5107 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5108 priv->text_renderer,
5109 "text", text_column,
5114 * gtk_combo_box_get_entry_text_column:
5115 * @combo_box: A #GtkComboBox.
5117 * Returns the column which @combo_box is using to get the strings
5118 * from to display in the internal entry.
5120 * Return value: A column in the data source model of @combo_box.
5125 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5127 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5129 return combo_box->priv->text_column;
5133 * gtk_combo_box_set_focus_on_click:
5134 * @combo: a #GtkComboBox
5135 * @focus_on_click: whether the combo box grabs focus when clicked
5138 * Sets whether the combo box will grab focus when it is clicked with
5139 * the mouse. Making mouse clicks not grab focus is useful in places
5140 * like toolbars where you don't want the keyboard focus removed from
5141 * the main area of the application.
5146 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5147 gboolean focus_on_click)
5149 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5151 focus_on_click = focus_on_click != FALSE;
5153 if (combo_box->priv->focus_on_click != focus_on_click)
5155 combo_box->priv->focus_on_click = focus_on_click;
5157 if (combo_box->priv->button)
5158 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5161 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5166 * gtk_combo_box_get_focus_on_click:
5167 * @combo: a #GtkComboBox
5169 * Returns whether the combo box grabs focus when it is clicked
5170 * with the mouse. See gtk_combo_box_set_focus_on_click().
5172 * Return value: %TRUE if the combo box grabs focus when it is
5173 * clicked with the mouse.
5178 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5180 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5182 return combo_box->priv->focus_on_click;
5187 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5188 GtkBuilder *builder,
5190 const gchar *tagname,
5191 GMarkupParser *parser,
5194 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5195 tagname, parser, data))
5198 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5199 tagname, parser, data);
5203 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5204 GtkBuilder *builder,
5206 const gchar *tagname,
5209 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5210 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5214 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5215 GtkBuilder *builder,
5216 const gchar *childname)
5218 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5220 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5221 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5223 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5227 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5231 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5232 GtkComboBoxPrivate *priv = combo_box->priv;
5233 gint focus_width, focus_pad;
5234 gint font_size, arrow_size;
5235 PangoContext *context;
5236 PangoFontMetrics *metrics;
5237 PangoFontDescription *font_desc;
5239 gint minimum_width = 0, natural_width = 0;
5240 gint child_min, child_nat;
5241 GtkStyleContext *style_context;
5242 GtkStateFlags state;
5245 child = gtk_bin_get_child (GTK_BIN (widget));
5248 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5250 gtk_widget_style_get (GTK_WIDGET (widget),
5251 "focus-line-width", &focus_width,
5252 "focus-padding", &focus_pad,
5253 "arrow-size", &arrow_size,
5256 style_context = gtk_widget_get_style_context (widget);
5257 state = gtk_widget_get_state_flags (widget);
5259 gtk_style_context_get (style_context, state,
5261 "border-width", &border,
5264 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5265 metrics = pango_context_get_metrics (context, font_desc,
5266 pango_context_get_language (context));
5267 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5268 pango_font_metrics_get_descent (metrics));
5269 pango_font_metrics_unref (metrics);
5270 pango_font_description_free (font_desc);
5272 arrow_size = MAX (arrow_size, font_size);
5274 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5276 if (!priv->tree_view)
5279 if (priv->cell_view)
5281 gint sep_width, arrow_width;
5282 gint border_width, xpad;
5283 GtkBorder button_border;
5285 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5286 get_widget_border (priv->button, &button_border);
5288 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5289 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5291 xpad = 2 * (border_width + focus_width + focus_pad) +
5292 button_border.left + button_border.right;
5294 minimum_width = child_min + sep_width + arrow_width + xpad;
5295 natural_width = child_nat + sep_width + arrow_width + xpad;
5299 gint but_width, but_nat_width;
5301 gtk_widget_get_preferred_width (priv->button,
5302 &but_width, &but_nat_width);
5304 minimum_width = child_min + but_width;
5305 natural_width = child_nat + but_nat_width;
5311 gint button_width, button_nat_width;
5313 /* sample + frame */
5314 minimum_width = child_min;
5315 natural_width = child_nat;
5317 minimum_width += 2 * focus_width;
5318 natural_width += 2 * focus_width;
5320 if (priv->cell_view_frame)
5322 if (priv->has_frame)
5324 gint border_width, xpad;
5325 GtkBorder frame_border;
5327 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5328 get_widget_border (priv->cell_view_frame, &frame_border);
5329 xpad = (2 * border_width) + frame_border.left + frame_border.right;
5331 minimum_width += xpad;
5332 natural_width += xpad;
5337 gtk_widget_get_preferred_width (priv->button,
5338 &button_width, &button_nat_width);
5340 minimum_width += button_width;
5341 natural_width += button_nat_width;
5344 minimum_width += border->left + border->right;
5345 natural_width += border->left + border->right;
5346 gtk_border_free (border);
5349 *minimum_size = minimum_width;
5352 *natural_size = natural_width;
5356 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5362 /* Combo box is height-for-width only
5363 * (so we always just reserve enough height for the minimum width) */
5364 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5365 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5369 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5374 /* Combo box is height-for-width only
5375 * (so we assume we always reserved enough height for the minimum width) */
5376 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5381 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5386 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5387 GtkComboBoxPrivate *priv = combo_box->priv;
5388 gint focus_width, focus_pad;
5389 gint min_height = 0, nat_height = 0;
5394 gtk_widget_style_get (GTK_WIDGET (widget),
5395 "focus-line-width", &focus_width,
5396 "focus-padding", &focus_pad,
5399 child = gtk_bin_get_child (GTK_BIN (widget));
5401 get_widget_border (widget, &border);
5402 size = avail_size - border.left;
5404 if (!priv->tree_view)
5407 if (priv->cell_view)
5409 /* calculate x/y padding and separator/arrow size */
5410 gint sep_width, arrow_width, sep_height, arrow_height;
5411 gint border_width, xpad, ypad;
5412 GtkBorder button_border;
5414 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5415 get_widget_border (priv->button, &button_border);
5417 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5418 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5419 gtk_widget_get_preferred_height_for_width (priv->separator,
5420 sep_width, &sep_height, NULL);
5421 gtk_widget_get_preferred_height_for_width (priv->arrow,
5422 arrow_width, &arrow_height, NULL);
5424 xpad = 2 * (border_width + focus_width + focus_pad) +
5425 button_border.left + button_border.right;
5426 ypad = 2 * (border_width + focus_width + focus_pad) +
5427 button_border.top + button_border.bottom;
5429 size -= sep_width + arrow_width + xpad;
5431 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5432 * and fitting the whole treemodel */
5433 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5435 arrow_height = MAX (arrow_height, sep_height);
5436 min_height = MAX (min_height, arrow_height);
5437 nat_height = MAX (nat_height, arrow_height);
5444 /* there is a custom child widget inside (no priv->cell_view) */
5445 gint but_width, but_height;
5447 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5448 gtk_widget_get_preferred_height_for_width (priv->button,
5449 but_width, &but_height, NULL);
5453 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5454 * and fitting the whole treemodel */
5455 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5457 min_height = MAX (min_height, but_height);
5458 nat_height = MAX (nat_height, but_height);
5464 gint but_width, but_height;
5465 gint xpad = 0, ypad = 0;
5467 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5468 gtk_widget_get_preferred_height_for_width (priv->button,
5469 but_width, &but_height, NULL);
5471 if (priv->cell_view_frame && priv->has_frame)
5473 GtkBorder frame_border;
5476 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5477 get_widget_border (GTK_WIDGET (priv->cell_view_frame), &frame_border);
5479 xpad = (2 * border_width) + border.left + frame_border.right;
5480 ypad = (2 * border_width) + border.top + frame_border.bottom;
5484 size -= 2 * focus_width;
5487 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5488 * and fitting the whole treemodel */
5489 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5491 min_height = MAX (min_height, but_height);
5492 nat_height = MAX (nat_height, but_height);
5498 min_height += border.top + border.bottom;
5499 nat_height += border.top + border.bottom;
5502 *minimum_size = min_height;
5505 *natural_size = nat_height;
5509 * gtk_combo_box_set_id_column:
5510 * @combo_box: A #GtkComboBox
5511 * @id_column: A column in @model to get string IDs for values from
5513 * Sets the model column which @combo_box should use to get string IDs
5514 * for values from. The column @id_column in the model of @combo_box
5515 * must be of type %G_TYPE_STRING.
5520 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5523 GtkComboBoxPrivate *priv = combo_box->priv;
5524 GtkTreeModel *model;
5526 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5528 if (id_column != priv->id_column)
5530 model = gtk_combo_box_get_model (combo_box);
5532 g_return_if_fail (id_column >= 0);
5533 g_return_if_fail (model == NULL ||
5534 id_column < gtk_tree_model_get_n_columns (model));
5536 priv->id_column = id_column;
5538 g_object_notify (G_OBJECT (combo_box), "id-column");
5539 g_object_notify (G_OBJECT (combo_box), "active-id");
5544 * gtk_combo_box_get_id_column:
5545 * @combo_box: A #GtkComboBox
5547 * Returns the column which @combo_box is using to get string IDs
5550 * Return value: A column in the data source model of @combo_box.
5555 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5557 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5559 return combo_box->priv->id_column;
5563 * gtk_combo_box_get_active_id:
5564 * @combo_box: a #GtkComboBox
5566 * Returns the ID of the active row of @combo_box. This value is taken
5567 * from the active row and the column specified by the 'id-column'
5568 * property of @combo_box (see gtk_combo_box_set_id_column()).
5570 * The returned value is an interned string which means that you can
5571 * compare the pointer by value to other interned strings and that you
5574 * If the 'id-column' property of @combo_box is not set or if no row is
5575 * selected then %NULL is returned.
5577 * Return value: the ID of the active row, or %NULL
5582 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5584 GtkTreeModel *model;
5588 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5590 column = combo_box->priv->id_column;
5595 model = gtk_combo_box_get_model (combo_box);
5596 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5597 G_TYPE_STRING, NULL);
5599 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5601 const gchar *interned;
5604 gtk_tree_model_get (model, &iter, column, &id, -1);
5605 interned = g_intern_string (id);
5615 * gtk_combo_box_set_active_id:
5616 * @combo_box: a #GtkComboBox
5617 * @active_id: the ID of the row to select
5619 * Changes the active row of @combo_box to the one that has an ID equal to @id.
5621 * If the 'id-column' property of @combo_box is unset or if no row has
5622 * the given ID then nothing happens.
5627 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5628 const gchar *active_id)
5630 GtkTreeModel *model;
5634 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5636 column = combo_box->priv->id_column;
5641 model = gtk_combo_box_get_model (combo_box);
5642 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
5645 if (gtk_tree_model_get_iter_first (model, &iter))
5650 gtk_tree_model_get (model, &iter, column, &id, -1);
5651 match = strcmp (id, active_id) == 0;
5656 gtk_combo_box_set_active_iter (combo_box, &iter);
5659 } while (gtk_tree_model_iter_next (model, &iter));