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 <gdk/gdkkeysyms.h>
47 #include <gobject/gvaluecollector.h>
52 #include "gtkmarshalers.h"
55 #include "gtkentryprivate.h"
56 #include "gtktreeprivate.h"
61 * @Short_description: A widget used to choose from a list of items
63 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
65 * A GtkComboBox is a widget that allows the user to choose from a list of
66 * valid choices. The GtkComboBox displays the selected choice. When
67 * activated, the GtkComboBox displays a popup which allows the user to
68 * make a new choice. The style in which the selected value is displayed,
69 * and the style of the popup is determined by the current theme. It may
70 * be similar to a Windows-style combo box.
72 * The GtkComboBox uses the model-view pattern; the list of valid choices
73 * is specified in the form of a tree model, and the display of the choices
74 * can be adapted to the data in the model by using cell renderers, as you
75 * would in a tree view. This is possible since GtkComboBox implements the
76 * #GtkCellLayout interface. The tree model holding the valid choices is
77 * not restricted to a flat list, it can be a real tree, and the popup will
78 * reflect the tree structure.
80 * For a simple list of textual choices, the model-view API of GtkComboBox
81 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86 /* WELCOME, to THE house of evil code */
87 struct _GtkComboBoxPrivate
97 GtkShadowType shadow_type;
99 gint active; /* Only temporary */
100 GtkTreeRowReference *active_row;
102 GtkWidget *tree_view;
103 GtkTreeViewColumn *column;
105 GtkWidget *cell_view;
106 GtkWidget *cell_view_frame;
111 GtkWidget *separator;
113 GtkWidget *popup_widget;
114 GtkWidget *popup_window;
115 GtkWidget *scrolled_window;
122 guint activate_button;
123 guint32 activate_time;
125 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->minimum_width = 0;
1022 priv->natural_width = 0;
1024 priv->wrap_width = 0;
1027 priv->active_row = NULL;
1028 priv->col_column = -1;
1029 priv->row_column = -1;
1031 priv->popup_shown = FALSE;
1032 priv->add_tearoffs = FALSE;
1033 priv->has_frame = TRUE;
1034 priv->is_cell_renderer = FALSE;
1035 priv->editing_canceled = FALSE;
1036 priv->auto_scroll = FALSE;
1037 priv->focus_on_click = TRUE;
1038 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1039 priv->has_entry = FALSE;
1040 priv->popup_fixed_width = TRUE;
1042 priv->text_column = -1;
1043 priv->text_renderer = NULL;
1044 priv->id_column = -1;
1048 gtk_combo_box_set_property (GObject *object,
1050 const GValue *value,
1053 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1059 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1062 case PROP_WRAP_WIDTH:
1063 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1066 case PROP_ROW_SPAN_COLUMN:
1067 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1070 case PROP_COLUMN_SPAN_COLUMN:
1071 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1075 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1078 case PROP_ADD_TEAROFFS:
1079 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1082 case PROP_HAS_FRAME:
1083 combo_box->priv->has_frame = g_value_get_boolean (value);
1085 if (combo_box->priv->has_entry)
1089 child = gtk_bin_get_child (GTK_BIN (combo_box));
1091 gtk_entry_set_has_frame (GTK_ENTRY (child),
1092 combo_box->priv->has_frame);
1097 case PROP_FOCUS_ON_CLICK:
1098 gtk_combo_box_set_focus_on_click (combo_box,
1099 g_value_get_boolean (value));
1102 case PROP_TEAROFF_TITLE:
1103 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1106 case PROP_POPUP_SHOWN:
1107 if (g_value_get_boolean (value))
1108 gtk_combo_box_popup (combo_box);
1110 gtk_combo_box_popdown (combo_box);
1113 case PROP_BUTTON_SENSITIVITY:
1114 gtk_combo_box_set_button_sensitivity (combo_box,
1115 g_value_get_enum (value));
1118 case PROP_POPUP_FIXED_WIDTH:
1119 gtk_combo_box_set_popup_fixed_width (combo_box,
1120 g_value_get_boolean (value));
1123 case PROP_EDITING_CANCELED:
1124 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1127 case PROP_HAS_ENTRY:
1128 combo_box->priv->has_entry = g_value_get_boolean (value);
1131 case PROP_ENTRY_TEXT_COLUMN:
1132 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1135 case PROP_ID_COLUMN:
1136 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1139 case PROP_ACTIVE_ID:
1140 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1143 case PROP_CELL_AREA:
1144 /* Construct-only, can only be assigned once */
1145 area = g_value_get_object (value);
1148 combo_box->priv->area = g_object_ref_sink (area);
1152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1158 gtk_combo_box_get_property (GObject *object,
1163 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1164 GtkComboBoxPrivate *priv = combo_box->priv;
1169 g_value_set_object (value, combo_box->priv->model);
1172 case PROP_WRAP_WIDTH:
1173 g_value_set_int (value, combo_box->priv->wrap_width);
1176 case PROP_ROW_SPAN_COLUMN:
1177 g_value_set_int (value, combo_box->priv->row_column);
1180 case PROP_COLUMN_SPAN_COLUMN:
1181 g_value_set_int (value, combo_box->priv->col_column);
1185 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1188 case PROP_ADD_TEAROFFS:
1189 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1192 case PROP_HAS_FRAME:
1193 g_value_set_boolean (value, combo_box->priv->has_frame);
1196 case PROP_FOCUS_ON_CLICK:
1197 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1200 case PROP_TEAROFF_TITLE:
1201 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1204 case PROP_POPUP_SHOWN:
1205 g_value_set_boolean (value, combo_box->priv->popup_shown);
1208 case PROP_BUTTON_SENSITIVITY:
1209 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1212 case PROP_POPUP_FIXED_WIDTH:
1213 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1216 case PROP_EDITING_CANCELED:
1217 g_value_set_boolean (value, priv->editing_canceled);
1220 case PROP_HAS_ENTRY:
1221 g_value_set_boolean (value, priv->has_entry);
1224 case PROP_ENTRY_TEXT_COLUMN:
1225 g_value_set_int (value, priv->text_column);
1228 case PROP_ID_COLUMN:
1229 g_value_set_int (value, priv->id_column);
1232 case PROP_ACTIVE_ID:
1233 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1236 case PROP_CELL_AREA:
1237 g_value_set_object (value, priv->area);
1241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1247 gtk_combo_box_state_changed (GtkWidget *widget,
1248 GtkStateType previous)
1250 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1251 GtkComboBoxPrivate *priv = combo_box->priv;
1253 if (gtk_widget_get_realized (widget))
1255 if (priv->tree_view && priv->cell_view)
1257 GtkStyleContext *context;
1258 GtkStateFlags state;
1261 context = gtk_widget_get_style_context (widget);
1262 state = gtk_widget_get_state_flags (widget);
1264 gtk_style_context_get (context, state,
1265 "background-color", &color,
1268 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
1269 gdk_rgba_free (color);
1273 gtk_widget_queue_draw (widget);
1277 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1278 GtkStateFlags previous,
1281 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1282 GtkComboBoxPrivate *priv = combo_box->priv;
1284 if (gtk_widget_get_realized (widget))
1286 if (!priv->tree_view && priv->cell_view)
1287 gtk_widget_set_state_flags (priv->cell_view,
1288 gtk_widget_get_state_flags (widget),
1292 gtk_widget_queue_draw (widget);
1296 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1298 GtkComboBoxPrivate *priv = combo_box->priv;
1299 gboolean appears_as_list;
1301 /* if wrap_width > 0, then we are in grid-mode and forced to use
1304 if (priv->wrap_width)
1305 appears_as_list = FALSE;
1307 gtk_widget_style_get (GTK_WIDGET (combo_box),
1308 "appears-as-list", &appears_as_list,
1311 if (appears_as_list)
1313 /* Destroy all the menu mode widgets, if they exist. */
1314 if (GTK_IS_MENU (priv->popup_widget))
1315 gtk_combo_box_menu_destroy (combo_box);
1317 /* Create the list mode widgets, if they don't already exist. */
1318 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1319 gtk_combo_box_list_setup (combo_box);
1323 /* Destroy all the list mode widgets, if they exist. */
1324 if (GTK_IS_TREE_VIEW (priv->tree_view))
1325 gtk_combo_box_list_destroy (combo_box);
1327 /* Create the menu mode widgets, if they don't already exist. */
1328 if (!GTK_IS_MENU (priv->popup_widget))
1329 gtk_combo_box_menu_setup (combo_box, TRUE);
1332 gtk_widget_style_get (GTK_WIDGET (combo_box),
1333 "shadow-type", &priv->shadow_type,
1338 gtk_combo_box_style_updated (GtkWidget *widget)
1340 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1341 GtkComboBoxPrivate *priv = combo_box->priv;
1344 gtk_combo_box_check_appearance (combo_box);
1346 if (priv->tree_view && priv->cell_view)
1348 GtkStyleContext *context;
1351 context = gtk_widget_get_style_context (widget);
1352 gtk_style_context_get (context, 0,
1353 "background-color", &color,
1356 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view),
1359 gdk_rgba_free (color);
1362 child = gtk_bin_get_child (GTK_BIN (combo_box));
1363 if (GTK_IS_ENTRY (child))
1364 g_object_set (child, "shadow-type",
1365 GTK_SHADOW_NONE == priv->shadow_type ?
1366 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1370 gtk_combo_box_button_toggled (GtkWidget *widget,
1373 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1375 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1377 if (!combo_box->priv->popup_in_progress)
1378 gtk_combo_box_popup (combo_box);
1381 gtk_combo_box_popdown (combo_box);
1385 gtk_combo_box_add (GtkContainer *container,
1388 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1389 GtkComboBoxPrivate *priv = combo_box->priv;
1391 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1393 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1394 "(need an instance of GtkEntry or of a subclass)",
1395 G_OBJECT_TYPE_NAME (widget));
1399 if (priv->cell_view &&
1400 gtk_widget_get_parent (priv->cell_view))
1402 gtk_widget_unparent (priv->cell_view);
1403 _gtk_bin_set_child (GTK_BIN (container), NULL);
1404 gtk_widget_queue_resize (GTK_WIDGET (container));
1407 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1408 _gtk_bin_set_child (GTK_BIN (container), widget);
1410 if (priv->cell_view &&
1411 widget != priv->cell_view)
1413 /* since the cell_view was unparented, it's gone now */
1414 priv->cell_view = NULL;
1416 if (!priv->tree_view && priv->separator)
1418 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1420 priv->separator = NULL;
1422 gtk_widget_queue_resize (GTK_WIDGET (container));
1424 else if (priv->cell_view_frame)
1426 gtk_widget_unparent (priv->cell_view_frame);
1427 priv->cell_view_frame = NULL;
1432 if (priv->has_entry)
1434 /* this flag is a hack to tell the entry to fill its allocation.
1436 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1438 g_signal_connect (widget, "changed",
1439 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1442 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1447 gtk_combo_box_remove (GtkContainer *container,
1450 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1451 GtkComboBoxPrivate *priv = combo_box->priv;
1453 gboolean appears_as_list;
1455 if (priv->has_entry)
1457 GtkWidget *child_widget;
1459 child_widget = gtk_bin_get_child (GTK_BIN (container));
1460 if (widget && widget == child_widget)
1462 g_signal_handlers_disconnect_by_func (widget,
1463 gtk_combo_box_entry_contents_changed,
1465 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1469 if (widget == priv->cell_view)
1470 priv->cell_view = NULL;
1472 gtk_widget_unparent (widget);
1473 _gtk_bin_set_child (GTK_BIN (container), NULL);
1475 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1478 gtk_widget_queue_resize (GTK_WIDGET (container));
1480 if (!priv->tree_view)
1481 appears_as_list = FALSE;
1483 appears_as_list = TRUE;
1485 if (appears_as_list)
1486 gtk_combo_box_list_destroy (combo_box);
1487 else if (GTK_IS_MENU (priv->popup_widget))
1489 gtk_combo_box_menu_destroy (combo_box);
1490 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1491 priv->popup_widget = NULL;
1494 if (!priv->cell_view)
1496 priv->cell_view = gtk_cell_view_new ();
1497 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1498 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1500 gtk_widget_show (priv->cell_view);
1501 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1506 if (appears_as_list)
1507 gtk_combo_box_list_setup (combo_box);
1509 gtk_combo_box_menu_setup (combo_box, TRUE);
1511 if (gtk_tree_row_reference_valid (priv->active_row))
1513 path = gtk_tree_row_reference_get_path (priv->active_row);
1514 gtk_combo_box_set_active_internal (combo_box, path);
1515 gtk_tree_path_free (path);
1518 gtk_combo_box_set_active_internal (combo_box, NULL);
1522 gtk_combo_box_menu_show (GtkWidget *menu,
1525 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1526 GtkComboBoxPrivate *priv = combo_box->priv;
1528 gtk_combo_box_child_show (menu, user_data);
1530 priv->popup_in_progress = TRUE;
1531 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1533 priv->popup_in_progress = FALSE;
1537 gtk_combo_box_menu_hide (GtkWidget *menu,
1540 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1542 gtk_combo_box_child_hide (menu,user_data);
1544 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1549 gtk_combo_box_detacher (GtkWidget *widget,
1552 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1553 GtkComboBoxPrivate *priv = combo_box->priv;
1555 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1557 g_signal_handlers_disconnect_by_func (menu->toplevel,
1558 gtk_combo_box_menu_show,
1560 g_signal_handlers_disconnect_by_func (menu->toplevel,
1561 gtk_combo_box_menu_hide,
1564 priv->popup_widget = NULL;
1568 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1571 GtkComboBoxPrivate *priv = combo_box->priv;
1573 if (GTK_IS_MENU (priv->popup_widget))
1575 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1576 priv->popup_widget = NULL;
1578 else if (priv->popup_widget)
1580 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1581 priv->popup_widget);
1582 g_object_unref (priv->popup_widget);
1583 priv->popup_widget = NULL;
1586 if (GTK_IS_MENU (popup))
1588 if (priv->popup_window)
1590 gtk_widget_destroy (priv->popup_window);
1591 priv->popup_window = NULL;
1594 priv->popup_widget = popup;
1597 * Note that we connect to show/hide on the toplevel, not the
1598 * menu itself, since the menu is not shown/hidden when it is
1599 * popped up while torn-off.
1601 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1602 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1603 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1604 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1606 gtk_menu_attach_to_widget (GTK_MENU (popup),
1607 GTK_WIDGET (combo_box),
1608 gtk_combo_box_detacher);
1612 if (!priv->popup_window)
1614 GtkWidget *toplevel;
1616 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1617 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1619 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1620 GDK_WINDOW_TYPE_HINT_COMBO);
1622 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1623 G_CALLBACK (gtk_combo_box_child_show),
1625 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1626 G_CALLBACK (gtk_combo_box_child_hide),
1629 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1630 if (GTK_IS_WINDOW (toplevel))
1632 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1633 GTK_WINDOW (priv->popup_window));
1634 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1635 GTK_WINDOW (toplevel));
1638 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1639 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1640 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1642 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1644 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1647 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1650 gtk_widget_show (priv->scrolled_window);
1652 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1653 priv->scrolled_window);
1656 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1659 gtk_widget_show (popup);
1660 g_object_ref (popup);
1661 priv->popup_widget = popup;
1666 get_widget_border (GtkWidget *widget,
1669 GtkStyleContext *context;
1670 GtkBorder *border_width;
1672 context = gtk_widget_get_style_context (widget);
1674 gtk_style_context_get (context,
1675 gtk_widget_get_state_flags (widget),
1676 "border-width", &border_width,
1679 *border = *border_width;
1680 gtk_border_free (border_width);
1684 gtk_combo_box_menu_position_below (GtkMenu *menu,
1690 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1691 GtkAllocation child_allocation;
1697 GdkRectangle monitor;
1700 /* FIXME: is using the size request here broken? */
1701 child = gtk_bin_get_child (GTK_BIN (combo_box));
1705 gtk_widget_get_allocation (child, &child_allocation);
1707 if (!gtk_widget_get_has_window (child))
1709 sx += child_allocation.x;
1710 sy += child_allocation.y;
1713 gdk_window_get_root_coords (gtk_widget_get_window (child),
1715 get_widget_border (GTK_WIDGET (combo_box), &border);
1718 if (combo_box->priv->popup_fixed_width)
1719 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1721 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1723 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1726 *x = sx + child_allocation.width - req.width;
1729 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1730 monitor_num = gdk_screen_get_monitor_at_window (screen,
1731 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1732 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1736 else if (*x + req.width > monitor.x + monitor.width)
1737 *x = monitor.x + monitor.width - req.width;
1739 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1740 *y += child_allocation.height;
1741 else if (*y - monitor.y >= req.height)
1743 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1744 *y += child_allocation.height;
1752 gtk_combo_box_menu_position_over (GtkMenu *menu,
1758 GtkComboBox *combo_box;
1762 GtkAllocation allocation;
1763 GtkAllocation child_allocation;
1770 combo_box = GTK_COMBO_BOX (user_data);
1771 widget = GTK_WIDGET (combo_box);
1773 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1775 gtk_widget_get_allocation (widget, &allocation);
1777 menu_xpos = allocation.x;
1778 menu_ypos = allocation.y + allocation.height / 2 - 2;
1780 if (combo_box->priv->popup_fixed_width)
1781 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1783 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1787 gtk_widget_get_allocation (active, &child_allocation);
1788 menu_ypos -= child_allocation.height / 2;
1791 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1794 child = children->data;
1796 if (active == child)
1799 if (gtk_widget_get_visible (child))
1801 gtk_widget_get_allocation (child, &child_allocation);
1803 menu_ypos -= child_allocation.height;
1806 children = children->next;
1809 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1810 menu_xpos = menu_xpos + allocation.width - menu_width;
1812 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1813 menu_xpos, menu_ypos,
1814 &menu_xpos, &menu_ypos);
1816 /* Clamp the position on screen */
1817 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1821 else if ((menu_xpos + menu_width) > screen_width)
1822 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1831 gtk_combo_box_menu_position (GtkMenu *menu,
1837 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1838 GtkComboBoxPrivate *priv = combo_box->priv;
1839 GtkWidget *menu_item;
1841 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1842 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1845 /* FIXME handle nested menus better */
1846 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1848 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1851 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1854 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1855 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1856 GDK_WINDOW_TYPE_HINT_COMBO);
1860 gtk_combo_box_list_position (GtkComboBox *combo_box,
1866 GtkComboBoxPrivate *priv = combo_box->priv;
1867 GtkAllocation allocation;
1870 GdkRectangle monitor;
1871 GtkRequisition popup_req;
1872 GtkPolicyType hpolicy, vpolicy;
1875 /* under windows, the drop down list is as wide as the combo box itself.
1877 GtkWidget *widget = GTK_WIDGET (combo_box);
1881 gtk_widget_get_allocation (widget, &allocation);
1883 if (!gtk_widget_get_has_window (widget))
1889 window = gtk_widget_get_window (widget);
1891 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1894 *width = allocation.width;
1896 hpolicy = vpolicy = GTK_POLICY_NEVER;
1897 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1900 /* XXX This set_size_request call is part of the hack outlined below and can
1901 * go away once height-for-width is implemented on treeviews. */
1902 gtk_widget_set_size_request (priv->tree_view, -1, -1);
1904 if (combo_box->priv->popup_fixed_width)
1906 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1908 if (popup_req.width > *width)
1910 hpolicy = GTK_POLICY_ALWAYS;
1911 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1917 if (priv->natural_width > *width)
1919 hpolicy = GTK_POLICY_NEVER;
1920 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1924 /* XXX Currently we set the size-request on the internal treeview to be
1925 * the natural width of the cells, this hack can go away once our
1926 * treeview does height-for-width properly (i.e. just adjust *width
1927 * here to be the natural width request of the scrolled-window).
1929 * I can't tell why the magic number 5 is needed here (i.e. without it
1930 * treeviews are left ellipsizing) , however it this all should be
1931 * removed with height-for-width treeviews.
1933 gtk_widget_set_size_request (priv->tree_view, priv->natural_width + 5, -1);
1934 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1936 *width = popup_req.width;
1940 *height = popup_req.height;
1942 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1943 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1944 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1946 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1947 *x = *x + allocation.width - *width;
1951 else if (*x + *width > monitor.x + monitor.width)
1952 *x = monitor.x + monitor.width - *width;
1954 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1955 *y += allocation.height;
1956 else if (*y - *height >= monitor.y)
1958 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1960 *y += allocation.height;
1961 *height = monitor.y + monitor.height - *y;
1965 *height = *y - monitor.y;
1969 if (popup_req.height > *height)
1971 vpolicy = GTK_POLICY_ALWAYS;
1973 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1979 cell_view_is_sensitive (GtkCellView *cell_view)
1981 GList *cells, *list;
1984 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1987 for (list = cells; list; list = list->next)
1989 g_object_get (list->data, "sensitive", &sensitive, NULL);
1994 g_list_free (cells);
2000 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2003 GtkComboBoxPrivate *priv = combo_box->priv;
2004 GList *cells, *list;
2010 if (priv->row_separator_func)
2012 if (priv->row_separator_func (priv->model, iter,
2013 priv->row_separator_data))
2017 gtk_tree_view_column_cell_set_cell_data (priv->column,
2019 iter, FALSE, FALSE);
2021 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
2024 for (list = cells; list; list = list->next)
2026 g_object_get (list->data, "sensitive", &sensitive, NULL);
2031 g_list_free (cells);
2037 update_menu_sensitivity (GtkComboBox *combo_box,
2040 GtkComboBoxPrivate *priv = combo_box->priv;
2041 GList *children, *child;
2042 GtkWidget *item, *submenu, *separator;
2043 GtkWidget *cell_view;
2049 children = gtk_container_get_children (GTK_CONTAINER (menu));
2051 for (child = children; child; child = child->next)
2053 item = GTK_WIDGET (child->data);
2054 cell_view = gtk_bin_get_child (GTK_BIN (item));
2056 if (!GTK_IS_CELL_VIEW (cell_view))
2059 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2060 if (submenu != NULL)
2062 gtk_widget_set_sensitive (item, TRUE);
2063 update_menu_sensitivity (combo_box, submenu);
2067 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
2069 if (menu != priv->popup_widget && child == children)
2071 separator = GTK_WIDGET (child->next->data);
2072 g_object_set (item, "visible", sensitive, NULL);
2073 g_object_set (separator, "visible", sensitive, NULL);
2076 gtk_widget_set_sensitive (item, sensitive);
2080 g_list_free (children);
2084 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2086 guint32 activate_time)
2088 GtkComboBoxPrivate *priv = combo_box->priv;
2091 gint width, min_width, nat_width;
2093 update_menu_sensitivity (combo_box, priv->popup_widget);
2096 if (gtk_tree_row_reference_valid (priv->active_row))
2098 path = gtk_tree_row_reference_get_path (priv->active_row);
2099 active_item = gtk_tree_path_get_indices (path)[0];
2100 gtk_tree_path_free (path);
2102 if (priv->add_tearoffs)
2106 /* FIXME handle nested menus better */
2107 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2109 if (priv->wrap_width == 0)
2111 GtkAllocation allocation;
2113 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2114 width = allocation.width;
2115 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2116 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2118 if (combo_box->priv->popup_fixed_width)
2119 width = MAX (width, min_width);
2121 width = MAX (width, nat_width);
2123 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2126 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2128 gtk_combo_box_menu_position, combo_box,
2129 button, activate_time);
2133 popup_grab_on_window (GdkWindow *window,
2134 GdkDevice *keyboard,
2136 guint32 activate_time)
2139 gdk_device_grab (keyboard, window,
2140 GDK_OWNERSHIP_WINDOW, TRUE,
2141 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2142 NULL, activate_time) != GDK_GRAB_SUCCESS)
2146 gdk_device_grab (pointer, window,
2147 GDK_OWNERSHIP_WINDOW, TRUE,
2148 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2149 GDK_POINTER_MOTION_MASK,
2150 NULL, activate_time) != GDK_GRAB_SUCCESS)
2153 gdk_device_ungrab (keyboard, activate_time);
2162 * gtk_combo_box_popup:
2163 * @combo_box: a #GtkComboBox
2165 * Pops up the menu or dropdown list of @combo_box.
2167 * This function is mostly intended for use by accessibility technologies;
2168 * applications should have little use for it.
2173 gtk_combo_box_popup (GtkComboBox *combo_box)
2175 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2177 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2181 * gtk_combo_box_popup_for_device:
2182 * @combo_box: a #GtkComboBox
2183 * @device: a #GdkDevice
2185 * Pops up the menu or dropdown list of @combo_box, the popup window
2186 * will be grabbed so only @device and its associated pointer/keyboard
2187 * are the only #GdkDevice<!-- -->s able to send events to it.
2192 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2195 GtkComboBoxPrivate *priv = combo_box->priv;
2196 gint x, y, width, height;
2197 GtkTreePath *path = NULL, *ppath;
2198 GtkWidget *toplevel;
2199 GdkDevice *keyboard, *pointer;
2202 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2203 g_return_if_fail (GDK_IS_DEVICE (device));
2205 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2208 if (gtk_widget_get_mapped (priv->popup_widget))
2211 if (priv->grab_pointer && priv->grab_keyboard)
2214 time = gtk_get_current_event_time ();
2216 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2219 pointer = gdk_device_get_associated_device (device);
2224 keyboard = gdk_device_get_associated_device (device);
2227 if (GTK_IS_MENU (priv->popup_widget))
2229 gtk_combo_box_menu_popup (combo_box,
2230 priv->activate_button,
2231 priv->activate_time);
2235 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2236 if (GTK_IS_WINDOW (toplevel))
2237 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2238 GTK_WINDOW (priv->popup_window));
2240 gtk_widget_show_all (priv->scrolled_window);
2241 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2243 gtk_widget_set_size_request (priv->popup_window, width, height);
2244 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2246 if (gtk_tree_row_reference_valid (priv->active_row))
2248 path = gtk_tree_row_reference_get_path (priv->active_row);
2249 ppath = gtk_tree_path_copy (path);
2250 if (gtk_tree_path_up (ppath))
2251 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2253 gtk_tree_path_free (ppath);
2255 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2259 gtk_widget_show (priv->popup_window);
2263 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2265 gtk_tree_path_free (path);
2268 gtk_widget_grab_focus (priv->popup_window);
2269 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2272 if (!gtk_widget_has_focus (priv->tree_view))
2273 gtk_widget_grab_focus (priv->tree_view);
2275 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2276 keyboard, pointer, time))
2278 gtk_widget_hide (priv->popup_window);
2282 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2283 priv->grab_pointer = pointer;
2284 priv->grab_keyboard = keyboard;
2288 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2292 device = gtk_get_current_event_device ();
2296 GdkDeviceManager *device_manager;
2297 GdkDisplay *display;
2300 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2301 device_manager = gdk_display_get_device_manager (display);
2303 /* No device was set, pick the first master device */
2304 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2305 device = devices->data;
2306 g_list_free (devices);
2309 gtk_combo_box_popup_for_device (combo_box, device);
2313 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2315 if (combo_box->priv->popup_shown)
2317 gtk_combo_box_popdown (combo_box);
2325 * gtk_combo_box_popdown:
2326 * @combo_box: a #GtkComboBox
2328 * Hides the menu or dropdown list of @combo_box.
2330 * This function is mostly intended for use by accessibility technologies;
2331 * applications should have little use for it.
2336 gtk_combo_box_popdown (GtkComboBox *combo_box)
2338 GtkComboBoxPrivate *priv = combo_box->priv;
2340 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2342 if (GTK_IS_MENU (priv->popup_widget))
2344 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2348 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2351 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2352 gtk_widget_hide (priv->popup_window);
2353 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2356 priv->grab_pointer = NULL;
2357 priv->grab_keyboard = NULL;
2360 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2361 gtk_widget_get_preferred_size (combo_box->priv->button, \
2365 child.x = allocation->x + border.right; \
2367 child.x = allocation->x + allocation->width - req.width - border.left; \
2369 child.y = allocation->y + border.top; \
2370 child.width = req.width; \
2371 child.height = allocation->height - (border.top + border.bottom); \
2372 child.width = MAX (1, child.width); \
2373 child.height = MAX (1, child.height); \
2375 gtk_widget_size_allocate (combo_box->priv->button, &child);
2379 gtk_combo_box_size_allocate (GtkWidget *widget,
2380 GtkAllocation *allocation)
2382 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2383 GtkComboBoxPrivate *priv = combo_box->priv;
2384 GtkWidget *child_widget;
2385 gint focus_width, focus_pad;
2386 GtkAllocation child;
2388 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2391 gtk_widget_set_allocation (widget, allocation);
2392 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2393 get_widget_border (widget, &border);
2395 gtk_widget_style_get (widget,
2396 "focus-line-width", &focus_width,
2397 "focus-padding", &focus_pad,
2400 if (!priv->tree_view)
2402 if (priv->cell_view)
2404 GtkBorder button_border;
2409 allocation->x += border.left;
2410 allocation->y += border.top;
2411 allocation->width -= border.left + border.right;
2412 allocation->height -= border.top + border.bottom;
2414 gtk_widget_size_allocate (priv->button, allocation);
2416 /* set some things ready */
2417 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2418 get_widget_border (priv->button, &button_border);
2420 child.x = allocation->x;
2421 child.y = allocation->y;
2422 width = allocation->width;
2423 child.height = allocation->height;
2425 if (!priv->is_cell_renderer)
2427 child.x += border_width + button_border.left + focus_width + focus_pad;
2428 child.y += border_width + button_border.top + focus_width + focus_pad;
2429 width -= (2 * (border_width + focus_width + focus_pad)) +
2430 button_border.left + button_border.right;
2431 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2432 button_border.top + button_border.bottom;
2435 /* handle the children */
2436 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2437 child.width = req.width;
2439 child.x += width - req.width;
2440 child.width = MAX (1, child.width);
2441 child.height = MAX (1, child.height);
2442 gtk_widget_size_allocate (priv->arrow, &child);
2444 child.x += req.width;
2445 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2446 child.width = req.width;
2448 child.x -= req.width;
2449 child.width = MAX (1, child.width);
2450 child.height = MAX (1, child.height);
2451 gtk_widget_size_allocate (priv->separator, &child);
2455 child.x += req.width;
2456 child.width = allocation->x + allocation->width
2457 - (border_width + button_border.right + focus_width + focus_pad)
2462 child.width = child.x;
2463 child.x = allocation->x
2464 + border_width + button_border.left + focus_width + focus_pad;
2465 child.width -= child.x;
2468 if (gtk_widget_get_visible (priv->popup_widget))
2470 gint width, menu_width;
2472 if (priv->wrap_width == 0)
2474 GtkAllocation combo_box_allocation;
2476 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2477 width = combo_box_allocation.width;
2478 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2480 if (combo_box->priv->popup_fixed_width)
2481 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2483 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2485 gtk_widget_set_size_request (priv->popup_widget,
2486 MAX (width, menu_width), -1);
2489 /* reposition the menu after giving it a new width */
2490 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2493 child.width = MAX (1, child.width);
2494 child.height = MAX (1, child.height);
2495 gtk_widget_size_allocate (child_widget, &child);
2499 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2502 child.x = allocation->x + req.width + border.right;
2504 child.x = allocation->x + border.left;
2505 child.y = allocation->y + border.top;
2506 child.width = allocation->width - req.width - (border.left + border.right);
2507 child.width = MAX (1, child.width);
2508 child.height = MAX (1, child.height);
2509 gtk_widget_size_allocate (child_widget, &child);
2515 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2518 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2522 child.x = allocation->x + req.width;
2524 child.x = allocation->x;
2526 child.y = allocation->y;
2527 child.width = allocation->width - req.width;
2528 child.height = allocation->height;
2530 if (priv->cell_view_frame)
2532 child.x += border.left + border_width;
2533 child.y += border.top + border_width;
2534 child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right));
2535 child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom));
2536 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2539 if (priv->has_frame)
2541 GtkBorder frame_border;
2543 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2544 get_widget_border (priv->cell_view_frame, &frame_border);
2546 child.x += border_width + frame_border.left;
2547 child.y += border_width + frame_border.right;
2548 child.width -= (2 * border_width) + frame_border.left + frame_border.right;
2549 child.height -= (2 * border_width) + frame_border.top + frame_border.bottom;
2554 child.x += border.left + border_width;
2555 child.y += border.top + border_width;
2556 child.width -= (2 * border_width) - (border.left + border.right);
2557 child.height -= (2 * border_width) - (border.top + border.bottom);
2560 if (gtk_widget_get_visible (priv->popup_window))
2562 gint x, y, width, height;
2563 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2564 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2565 gtk_widget_set_size_request (priv->popup_window, width, height);
2569 child.width = MAX (1, child.width);
2570 child.height = MAX (1, child.height);
2572 gtk_widget_size_allocate (child_widget, &child);
2576 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2579 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2581 GtkComboBoxPrivate *priv = combo_box->priv;
2585 g_signal_handler_disconnect (priv->model,
2587 g_signal_handler_disconnect (priv->model,
2589 g_signal_handler_disconnect (priv->model,
2590 priv->reordered_id);
2591 g_signal_handler_disconnect (priv->model,
2597 g_object_unref (priv->model);
2601 if (priv->active_row)
2603 gtk_tree_row_reference_free (priv->active_row);
2604 priv->active_row = NULL;
2607 if (priv->cell_view)
2608 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2612 gtk_combo_box_forall (GtkContainer *container,
2613 gboolean include_internals,
2614 GtkCallback callback,
2615 gpointer callback_data)
2617 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2618 GtkComboBoxPrivate *priv = combo_box->priv;
2621 if (include_internals)
2624 (* callback) (priv->button, callback_data);
2625 if (priv->cell_view_frame)
2626 (* callback) (priv->cell_view_frame, callback_data);
2629 child = gtk_bin_get_child (GTK_BIN (container));
2631 (* callback) (child, callback_data);
2635 gtk_combo_box_child_show (GtkWidget *widget,
2636 GtkComboBox *combo_box)
2638 GtkComboBoxPrivate *priv = combo_box->priv;
2640 priv->popup_shown = TRUE;
2641 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2645 gtk_combo_box_child_hide (GtkWidget *widget,
2646 GtkComboBox *combo_box)
2648 GtkComboBoxPrivate *priv = combo_box->priv;
2650 priv->popup_shown = FALSE;
2651 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2655 gtk_combo_box_draw (GtkWidget *widget,
2658 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2659 GtkComboBoxPrivate *priv = combo_box->priv;
2661 if (priv->shadow_type != GTK_SHADOW_NONE)
2663 GtkStyleContext *context;
2664 GtkStateFlags state;
2666 context = gtk_widget_get_style_context (widget);
2667 state = gtk_widget_get_state_flags (widget);
2668 gtk_style_context_set_state (context, state);
2670 gtk_render_background (context, cr, 0, 0,
2671 gtk_widget_get_allocated_width (widget),
2672 gtk_widget_get_allocated_height (widget));
2673 gtk_render_frame (context, cr, 0, 0,
2674 gtk_widget_get_allocated_width (widget),
2675 gtk_widget_get_allocated_height (widget));
2678 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2681 if (priv->tree_view && priv->cell_view_frame)
2683 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2684 priv->cell_view_frame, cr);
2687 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2688 gtk_bin_get_child (GTK_BIN (widget)),
2704 path_visible (GtkTreeView *view,
2710 /* Note that we rely on the fact that collapsed rows don't have nodes
2712 return _gtk_tree_view_find_node (view, path, &tree, &node);
2716 tree_next_func (GtkTreeModel *model,
2721 SearchData *search_data = (SearchData *)data;
2723 if (search_data->found)
2725 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2728 if (search_data->visible &&
2729 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2732 search_data->set = TRUE;
2733 search_data->iter = *iter;
2738 if (gtk_tree_path_compare (path, search_data->path) == 0)
2739 search_data->found = TRUE;
2745 tree_next (GtkComboBox *combo,
2746 GtkTreeModel *model,
2751 SearchData search_data;
2753 search_data.combo = combo;
2754 search_data.path = gtk_tree_model_get_path (model, iter);
2755 search_data.visible = visible;
2756 search_data.found = FALSE;
2757 search_data.set = FALSE;
2759 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2761 *next = search_data.iter;
2763 gtk_tree_path_free (search_data.path);
2765 return search_data.set;
2769 tree_prev_func (GtkTreeModel *model,
2774 SearchData *search_data = (SearchData *)data;
2776 if (gtk_tree_path_compare (path, search_data->path) == 0)
2778 search_data->found = TRUE;
2782 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2785 if (search_data->visible &&
2786 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2789 search_data->set = TRUE;
2790 search_data->iter = *iter;
2796 tree_prev (GtkComboBox *combo,
2797 GtkTreeModel *model,
2802 SearchData search_data;
2804 search_data.combo = combo;
2805 search_data.path = gtk_tree_model_get_path (model, iter);
2806 search_data.visible = visible;
2807 search_data.found = FALSE;
2808 search_data.set = FALSE;
2810 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2812 *prev = search_data.iter;
2814 gtk_tree_path_free (search_data.path);
2816 return search_data.set;
2820 tree_last_func (GtkTreeModel *model,
2825 SearchData *search_data = (SearchData *)data;
2827 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2830 /* Note that we rely on the fact that collapsed rows don't have nodes
2832 if (search_data->visible &&
2833 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2836 search_data->set = TRUE;
2837 search_data->iter = *iter;
2843 tree_last (GtkComboBox *combo,
2844 GtkTreeModel *model,
2848 SearchData search_data;
2850 search_data.combo = combo;
2851 search_data.visible = visible;
2852 search_data.set = FALSE;
2854 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2856 *last = search_data.iter;
2858 return search_data.set;
2863 tree_first_func (GtkTreeModel *model,
2868 SearchData *search_data = (SearchData *)data;
2870 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2873 if (search_data->visible &&
2874 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2877 search_data->set = TRUE;
2878 search_data->iter = *iter;
2884 tree_first (GtkComboBox *combo,
2885 GtkTreeModel *model,
2889 SearchData search_data;
2891 search_data.combo = combo;
2892 search_data.visible = visible;
2893 search_data.set = FALSE;
2895 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2897 *first = search_data.iter;
2899 return search_data.set;
2903 gtk_combo_box_scroll_event (GtkWidget *widget,
2904 GdkEventScroll *event)
2906 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2909 GtkTreeIter new_iter;
2911 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2914 if (event->direction == GDK_SCROLL_UP)
2915 found = tree_prev (combo_box, combo_box->priv->model,
2916 &iter, &new_iter, FALSE);
2918 found = tree_next (combo_box, combo_box->priv->model,
2919 &iter, &new_iter, FALSE);
2922 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2932 gtk_combo_box_row_separator_func (GtkTreeModel *model,
2936 GtkComboBoxPrivate *priv = combo->priv;
2938 if (priv->row_separator_func)
2939 return priv->row_separator_func (model, iter, priv->row_separator_data);
2945 gtk_combo_box_header_func (GtkTreeModel *model,
2949 /* Every submenu has a selectable header, however we
2950 * can expose a method to make that configurable by
2951 * the user (like row_separator_func is done) */
2956 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2957 gboolean add_children)
2959 GtkComboBoxPrivate *priv = combo_box->priv;
2963 child = gtk_bin_get_child (GTK_BIN (combo_box));
2965 if (priv->cell_view)
2967 priv->button = gtk_toggle_button_new ();
2968 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2969 priv->focus_on_click);
2971 g_signal_connect (priv->button, "toggled",
2972 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2973 gtk_widget_set_parent (priv->button,
2974 gtk_widget_get_parent (child));
2976 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2977 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2979 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
2980 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2982 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2983 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2985 gtk_widget_show_all (priv->button);
2989 priv->button = gtk_toggle_button_new ();
2990 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2991 priv->focus_on_click);
2993 g_signal_connect (priv->button, "toggled",
2994 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2995 gtk_widget_set_parent (priv->button,
2996 gtk_widget_get_parent (child));
2998 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2999 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3000 gtk_widget_show_all (priv->button);
3003 g_signal_connect (priv->button, "button-press-event",
3004 G_CALLBACK (gtk_combo_box_menu_button_press),
3006 g_signal_connect (priv->button, "state-flags-changed",
3007 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3010 /* create our funky menu */
3011 menu = gtk_tree_menu_new_with_area (priv->area);
3012 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3014 gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3016 gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3017 gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3018 gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3019 gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3020 combo_box->priv->add_tearoffs);
3022 g_signal_connect (menu, "menu-activate",
3023 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3025 /* Chain our row_separator_func through */
3026 gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3027 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3030 gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3031 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3034 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3036 g_signal_connect (menu, "key-press-event",
3037 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3038 gtk_combo_box_set_popup_widget (combo_box, menu);
3040 gtk_combo_box_update_title (combo_box);
3044 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3046 GtkComboBoxPrivate *priv = combo_box->priv;
3048 g_signal_handlers_disconnect_matched (priv->button,
3049 G_SIGNAL_MATCH_DATA,
3051 gtk_combo_box_menu_button_press, NULL);
3052 g_signal_handlers_disconnect_matched (priv->button,
3053 G_SIGNAL_MATCH_DATA,
3055 gtk_combo_box_button_state_flags_changed, combo_box);
3056 g_signal_handlers_disconnect_matched (priv->popup_widget,
3057 G_SIGNAL_MATCH_DATA,
3059 gtk_combo_box_menu_activate, combo_box);
3061 /* unparent will remove our latest ref */
3062 gtk_widget_unparent (priv->button);
3065 priv->button = NULL;
3067 priv->separator = NULL;
3069 priv->column = NULL;
3071 /* changing the popup window will unref the menu and the children */
3076 gtk_combo_box_menu_button_press (GtkWidget *widget,
3077 GdkEventButton *event,
3080 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3081 GtkComboBoxPrivate *priv = combo_box->priv;
3083 if (GTK_IS_MENU (priv->popup_widget) &&
3084 event->type == GDK_BUTTON_PRESS && event->button == 1)
3086 if (priv->focus_on_click &&
3087 !gtk_widget_has_focus (priv->button))
3088 gtk_widget_grab_focus (priv->button);
3090 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3099 gtk_combo_box_menu_activate (GtkWidget *menu,
3101 GtkComboBox *combo_box)
3105 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3106 gtk_combo_box_set_active_iter (combo_box, &iter);
3108 g_object_set (combo_box,
3109 "editing-canceled", FALSE,
3115 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3118 gboolean sensitive = TRUE; /* fool code checkers */
3120 if (!combo_box->priv->button)
3123 switch (combo_box->priv->button_sensitivity)
3125 case GTK_SENSITIVITY_ON:
3128 case GTK_SENSITIVITY_OFF:
3131 case GTK_SENSITIVITY_AUTO:
3132 sensitive = combo_box->priv->model &&
3133 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3136 g_assert_not_reached ();
3140 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3142 /* In list-mode, we also need to update sensitivity of the event box */
3143 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3144 && combo_box->priv->cell_view)
3145 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3149 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3154 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3156 if (combo_box->priv->tree_view)
3157 gtk_combo_box_list_popup_resize (combo_box);
3159 gtk_combo_box_update_sensitivity (combo_box);
3163 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3167 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3168 GtkComboBoxPrivate *priv = combo_box->priv;
3170 if (!gtk_tree_row_reference_valid (priv->active_row))
3172 if (priv->cell_view)
3173 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3174 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3177 if (priv->tree_view)
3178 gtk_combo_box_list_popup_resize (combo_box);
3180 gtk_combo_box_update_sensitivity (combo_box);
3184 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3190 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3194 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3199 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3200 GtkComboBoxPrivate *priv = combo_box->priv;
3201 GtkTreePath *active_path;
3203 /* FIXME this belongs to GtkCellView */
3204 if (gtk_tree_row_reference_valid (priv->active_row))
3206 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3207 if (gtk_tree_path_compare (path, active_path) == 0 &&
3209 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3210 gtk_tree_path_free (active_path);
3213 if (priv->tree_view)
3214 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3218 list_popup_resize_idle (gpointer user_data)
3220 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3221 GtkComboBoxPrivate *priv = combo_box->priv;
3222 gint x, y, width, height;
3224 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3226 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3228 gtk_widget_set_size_request (priv->popup_window, width, height);
3229 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3232 priv->resize_idle_id = 0;
3238 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3240 GtkComboBoxPrivate *priv = combo_box->priv;
3242 if (!priv->resize_idle_id)
3243 priv->resize_idle_id =
3244 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3248 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3253 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3255 gtk_combo_box_list_popup_resize (combo_box);
3264 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3266 GtkComboBoxPrivate *priv = combo_box->priv;
3267 GtkTreeSelection *sel;
3269 GtkWidget *widget = GTK_WIDGET (combo_box);
3271 priv->button = gtk_toggle_button_new ();
3272 child = gtk_bin_get_child (GTK_BIN (combo_box));
3273 gtk_widget_set_parent (priv->button,
3274 gtk_widget_get_parent (child));
3275 g_signal_connect (priv->button, "button-press-event",
3276 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3277 g_signal_connect (priv->button, "toggled",
3278 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3280 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3281 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3282 priv->separator = NULL;
3283 gtk_widget_show_all (priv->button);
3285 if (priv->cell_view)
3287 GtkStyleContext *context;
3288 GtkStateFlags state;
3291 context = gtk_widget_get_style_context (widget);
3292 state = gtk_widget_get_state_flags (widget);
3294 gtk_style_context_get (context, state,
3295 "background-color", &color,
3298 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
3299 gdk_rgba_free (color);
3301 priv->box = gtk_event_box_new ();
3302 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3305 if (priv->has_frame)
3307 priv->cell_view_frame = gtk_frame_new (NULL);
3308 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3313 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3314 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3318 gtk_widget_set_parent (priv->cell_view_frame,
3319 gtk_widget_get_parent (child));
3320 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3321 gtk_widget_show_all (priv->cell_view_frame);
3323 g_signal_connect (priv->box, "button-press-event",
3324 G_CALLBACK (gtk_combo_box_list_button_pressed),
3328 priv->tree_view = gtk_tree_view_new ();
3329 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3330 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3331 gtk_tree_selection_set_select_function (sel,
3332 gtk_combo_box_list_select_func,
3334 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3336 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3339 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3340 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3344 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3346 priv->column = gtk_tree_view_column_new_with_area (priv->area);
3347 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3349 if (gtk_tree_row_reference_valid (priv->active_row))
3353 path = gtk_tree_row_reference_get_path (priv->active_row);
3354 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3356 gtk_tree_path_free (path);
3359 /* set sample/popup widgets */
3360 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3362 g_signal_connect (priv->tree_view, "key-press-event",
3363 G_CALLBACK (gtk_combo_box_list_key_press),
3365 g_signal_connect (priv->tree_view, "enter-notify-event",
3366 G_CALLBACK (gtk_combo_box_list_enter_notify),
3368 g_signal_connect (priv->tree_view, "row-expanded",
3369 G_CALLBACK (gtk_combo_box_model_row_expanded),
3371 g_signal_connect (priv->tree_view, "row-collapsed",
3372 G_CALLBACK (gtk_combo_box_model_row_expanded),
3374 g_signal_connect (priv->popup_window, "button-press-event",
3375 G_CALLBACK (gtk_combo_box_list_button_pressed),
3377 g_signal_connect (priv->popup_window, "button-release-event",
3378 G_CALLBACK (gtk_combo_box_list_button_released),
3381 gtk_widget_show (priv->tree_view);
3383 gtk_combo_box_update_sensitivity (combo_box);
3387 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3389 GtkComboBoxPrivate *priv = combo_box->priv;
3391 /* disconnect signals */
3392 g_signal_handlers_disconnect_matched (priv->tree_view,
3393 G_SIGNAL_MATCH_DATA,
3394 0, 0, NULL, NULL, combo_box);
3395 g_signal_handlers_disconnect_matched (priv->button,
3396 G_SIGNAL_MATCH_DATA,
3398 gtk_combo_box_list_button_pressed,
3400 g_signal_handlers_disconnect_matched (priv->popup_window,
3401 G_SIGNAL_MATCH_DATA,
3403 gtk_combo_box_list_button_pressed,
3405 g_signal_handlers_disconnect_matched (priv->popup_window,
3406 G_SIGNAL_MATCH_DATA,
3408 gtk_combo_box_list_button_released,
3411 g_signal_handlers_disconnect_matched (priv->popup_window,
3412 G_SIGNAL_MATCH_DATA,
3414 gtk_combo_box_child_show,
3417 g_signal_handlers_disconnect_matched (priv->popup_window,
3418 G_SIGNAL_MATCH_DATA,
3420 gtk_combo_box_child_hide,
3424 g_signal_handlers_disconnect_matched (priv->box,
3425 G_SIGNAL_MATCH_DATA,
3427 gtk_combo_box_list_button_pressed,
3430 /* destroy things (unparent will kill the latest ref from us)
3431 * last unref on button will destroy the arrow
3433 gtk_widget_unparent (priv->button);
3434 priv->button = NULL;
3437 if (priv->cell_view)
3439 g_object_set (priv->cell_view,
3440 "background-set", FALSE,
3444 if (priv->cell_view_frame)
3446 gtk_widget_unparent (priv->cell_view_frame);
3447 priv->cell_view_frame = NULL;
3451 if (priv->scroll_timer)
3453 g_source_remove (priv->scroll_timer);
3454 priv->scroll_timer = 0;
3457 if (priv->resize_idle_id)
3459 g_source_remove (priv->resize_idle_id);
3460 priv->resize_idle_id = 0;
3463 gtk_widget_destroy (priv->tree_view);
3465 priv->tree_view = NULL;
3466 if (priv->popup_widget)
3468 g_object_unref (priv->popup_widget);
3469 priv->popup_widget = NULL;
3476 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3477 GdkEventButton *event,
3480 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3481 GtkComboBoxPrivate *priv = combo_box->priv;
3483 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3485 if (ewidget == priv->popup_window)
3488 if ((ewidget != priv->button && ewidget != priv->box) ||
3489 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3492 if (priv->focus_on_click &&
3493 !gtk_widget_has_focus (priv->button))
3494 gtk_widget_grab_focus (priv->button);
3496 gtk_combo_box_popup_for_device (combo_box, event->device);
3498 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3500 priv->auto_scroll = FALSE;
3501 if (priv->scroll_timer == 0)
3502 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3503 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3506 priv->popup_in_progress = TRUE;
3512 gtk_combo_box_list_button_released (GtkWidget *widget,
3513 GdkEventButton *event,
3517 GtkTreePath *path = NULL;
3520 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3521 GtkComboBoxPrivate *priv = combo_box->priv;
3523 gboolean popup_in_progress = FALSE;
3525 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3527 if (priv->popup_in_progress)
3529 popup_in_progress = TRUE;
3530 priv->popup_in_progress = FALSE;
3533 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3535 if (priv->scroll_timer)
3537 g_source_remove (priv->scroll_timer);
3538 priv->scroll_timer = 0;
3541 if (ewidget != priv->tree_view)
3543 if ((ewidget == priv->button ||
3544 ewidget == priv->box) &&
3545 !popup_in_progress &&
3546 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3548 gtk_combo_box_popdown (combo_box);
3552 /* released outside treeview */
3553 if (ewidget != priv->button && ewidget != priv->box)
3555 gtk_combo_box_popdown (combo_box);
3563 /* select something cool */
3564 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3570 return TRUE; /* clicked outside window? */
3572 gtk_tree_model_get_iter (priv->model, &iter, path);
3573 gtk_tree_path_free (path);
3575 gtk_combo_box_popdown (combo_box);
3577 if (tree_column_row_is_sensitive (combo_box, &iter))
3578 gtk_combo_box_set_active_iter (combo_box, &iter);
3584 gtk_combo_box_menu_key_press (GtkWidget *widget,
3588 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3590 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3592 /* The menu hasn't managed the
3593 * event, forward it to the combobox
3595 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3602 gtk_combo_box_list_key_press (GtkWidget *widget,
3606 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3609 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3610 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3612 GtkTreeModel *model = NULL;
3614 gtk_combo_box_popdown (combo_box);
3616 if (combo_box->priv->model)
3618 GtkTreeSelection *sel;
3620 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3622 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3623 gtk_combo_box_set_active_iter (combo_box, &iter);
3629 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3631 /* The list hasn't managed the
3632 * event, forward it to the combobox
3634 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3641 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3646 GtkAllocation allocation;
3647 GtkWidget *tree_view = combo_box->priv->tree_view;
3650 gtk_widget_get_allocation (tree_view, &allocation);
3652 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3653 if (adj && adj->upper - adj->lower > adj->page_size)
3655 if (x <= allocation.x &&
3656 adj->lower < adj->value)
3658 value = adj->value - (allocation.x - x + 1);
3659 gtk_adjustment_set_value (adj, value);
3661 else if (x >= allocation.x + allocation.width &&
3662 adj->upper - adj->page_size > adj->value)
3664 value = adj->value + (x - allocation.x - allocation.width + 1);
3665 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3669 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3670 if (adj && adj->upper - adj->lower > adj->page_size)
3672 if (y <= allocation.y &&
3673 adj->lower < adj->value)
3675 value = adj->value - (allocation.y - y + 1);
3676 gtk_adjustment_set_value (adj, value);
3678 else if (y >= allocation.height &&
3679 adj->upper - adj->page_size > adj->value)
3681 value = adj->value + (y - allocation.height + 1);
3682 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3688 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3690 GtkComboBoxPrivate *priv = combo_box->priv;
3693 if (priv->auto_scroll)
3695 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3698 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3705 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3706 GdkEventCrossing *event,
3709 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3711 combo_box->priv->auto_scroll = TRUE;
3717 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3718 GtkTreeModel *model,
3720 gboolean path_currently_selected,
3723 GList *list, *columns;
3724 gboolean sensitive = FALSE;
3726 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3728 for (list = columns; list && !sensitive; list = list->next)
3730 GList *cells, *cell;
3731 gboolean cell_sensitive, cell_visible;
3733 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3735 if (!gtk_tree_view_column_get_visible (column))
3738 gtk_tree_model_get_iter (model, &iter, path);
3739 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3742 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3745 g_object_get (cell->data,
3746 "sensitive", &cell_sensitive,
3747 "visible", &cell_visible,
3750 if (cell_visible && cell_sensitive)
3755 g_list_free (cells);
3757 sensitive = cell_sensitive;
3760 g_list_free (columns);
3766 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3771 /* XXX Do nothing ? */
3775 * GtkCellLayout implementation
3778 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3780 return GTK_COMBO_BOX (cell_layout)->priv->area;
3788 * gtk_combo_box_new:
3790 * Creates a new empty #GtkComboBox.
3792 * Return value: A new #GtkComboBox.
3797 gtk_combo_box_new (void)
3799 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3803 * gtk_combo_box_new_with_entry:
3805 * Creates a new empty #GtkComboBox with an entry.
3807 * Return value: A new #GtkComboBox.
3810 gtk_combo_box_new_with_entry (void)
3812 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3816 * gtk_combo_box_new_with_model:
3817 * @model: A #GtkTreeModel.
3819 * Creates a new #GtkComboBox with the model initialized to @model.
3821 * Return value: A new #GtkComboBox.
3826 gtk_combo_box_new_with_model (GtkTreeModel *model)
3828 GtkComboBox *combo_box;
3830 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3832 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3834 return GTK_WIDGET (combo_box);
3838 * gtk_combo_box_new_with_model_and_entry:
3839 * @model: A #GtkTreeModel
3841 * Creates a new empty #GtkComboBox with an entry
3842 * and with the model initialized to @model.
3844 * Return value: A new #GtkComboBox
3847 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3849 return g_object_new (GTK_TYPE_COMBO_BOX,
3856 * gtk_combo_box_get_wrap_width:
3857 * @combo_box: A #GtkComboBox
3859 * Returns the wrap width which is used to determine the number of columns
3860 * for the popup menu. If the wrap width is larger than 1, the combo box
3863 * Returns: the wrap width.
3868 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3870 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3872 return combo_box->priv->wrap_width;
3876 * gtk_combo_box_set_wrap_width:
3877 * @combo_box: A #GtkComboBox
3878 * @width: Preferred number of columns
3880 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3881 * the preferred number of columns when you want the popup to be layed out
3887 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3890 GtkComboBoxPrivate *priv;
3892 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3893 g_return_if_fail (width >= 0);
3895 priv = combo_box->priv;
3897 if (width != priv->wrap_width)
3899 priv->wrap_width = width;
3901 gtk_combo_box_check_appearance (combo_box);
3903 if (GTK_IS_TREE_MENU (priv->popup_widget))
3904 gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
3906 g_object_notify (G_OBJECT (combo_box), "wrap-width");
3911 * gtk_combo_box_get_row_span_column:
3912 * @combo_box: A #GtkComboBox
3914 * Returns the column with row span information for @combo_box.
3916 * Returns: the row span column.
3921 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3923 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3925 return combo_box->priv->row_column;
3929 * gtk_combo_box_set_row_span_column:
3930 * @combo_box: A #GtkComboBox.
3931 * @row_span: A column in the model passed during construction.
3933 * Sets the column with row span information for @combo_box to be @row_span.
3934 * The row span column contains integers which indicate how many rows
3935 * an item should span.
3940 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3943 GtkComboBoxPrivate *priv;
3946 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3948 priv = combo_box->priv;
3950 col = gtk_tree_model_get_n_columns (priv->model);
3951 g_return_if_fail (row_span >= -1 && row_span < col);
3953 if (row_span != priv->row_column)
3955 priv->row_column = row_span;
3957 if (GTK_IS_TREE_MENU (priv->popup_widget))
3958 gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
3960 g_object_notify (G_OBJECT (combo_box), "row-span-column");
3965 * gtk_combo_box_get_column_span_column:
3966 * @combo_box: A #GtkComboBox
3968 * Returns the column with column span information for @combo_box.
3970 * Returns: the column span column.
3975 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
3977 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3979 return combo_box->priv->col_column;
3983 * gtk_combo_box_set_column_span_column:
3984 * @combo_box: A #GtkComboBox
3985 * @column_span: A column in the model passed during construction
3987 * Sets the column with column span information for @combo_box to be
3988 * @column_span. The column span column contains integers which indicate
3989 * how many columns an item should span.
3994 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
3997 GtkComboBoxPrivate *priv;
4000 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4002 priv = combo_box->priv;
4004 col = gtk_tree_model_get_n_columns (priv->model);
4005 g_return_if_fail (column_span >= -1 && column_span < col);
4007 if (column_span != priv->col_column)
4009 priv->col_column = column_span;
4011 if (GTK_IS_TREE_MENU (priv->popup_widget))
4012 gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4014 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4019 * gtk_combo_box_get_active:
4020 * @combo_box: A #GtkComboBox
4022 * Returns the index of the currently active item, or -1 if there's no
4023 * active item. If the model is a non-flat treemodel, and the active item
4024 * is not an immediate child of the root of the tree, this function returns
4025 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4026 * <literal>path</literal> is the #GtkTreePath of the active item.
4028 * Return value: An integer which is the index of the currently active item,
4029 * or -1 if there's no active item.
4034 gtk_combo_box_get_active (GtkComboBox *combo_box)
4036 GtkComboBoxPrivate *priv;
4039 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4041 priv = combo_box->priv;
4043 if (gtk_tree_row_reference_valid (priv->active_row))
4047 path = gtk_tree_row_reference_get_path (priv->active_row);
4048 result = gtk_tree_path_get_indices (path)[0];
4049 gtk_tree_path_free (path);
4058 * gtk_combo_box_set_active:
4059 * @combo_box: A #GtkComboBox
4060 * @index_: An index in the model passed during construction, or -1 to have
4063 * Sets the active item of @combo_box to be the item at @index.
4068 gtk_combo_box_set_active (GtkComboBox *combo_box,
4071 GtkTreePath *path = NULL;
4072 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4073 g_return_if_fail (index_ >= -1);
4075 if (combo_box->priv->model == NULL)
4077 /* Save index, in case the model is set after the index */
4078 combo_box->priv->active = index_;
4084 path = gtk_tree_path_new_from_indices (index_, -1);
4086 gtk_combo_box_set_active_internal (combo_box, path);
4089 gtk_tree_path_free (path);
4093 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4096 GtkComboBoxPrivate *priv = combo_box->priv;
4097 GtkTreePath *active_path;
4100 /* Remember whether the initially active row is valid. */
4101 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4103 if (path && is_valid_row_reference)
4105 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4106 path_cmp = gtk_tree_path_compare (path, active_path);
4107 gtk_tree_path_free (active_path);
4112 if (priv->active_row)
4114 gtk_tree_row_reference_free (priv->active_row);
4115 priv->active_row = NULL;
4120 if (priv->tree_view)
4121 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4124 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4126 if (GTK_IS_MENU (menu))
4127 gtk_menu_set_active (menu, -1);
4130 if (priv->cell_view)
4131 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4134 * Do not emit a "changed" signal when an already invalid selection was
4135 * now set to invalid.
4137 if (!is_valid_row_reference)
4143 gtk_tree_row_reference_new (priv->model, path);
4145 if (priv->tree_view)
4147 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4150 else if (GTK_IS_MENU (priv->popup_widget))
4152 /* FIXME handle nested menus better */
4153 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4154 gtk_tree_path_get_indices (path)[0]);
4157 if (priv->cell_view)
4158 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4162 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4163 g_object_notify (G_OBJECT (combo_box), "active");
4164 if (combo_box->priv->id_column >= 0)
4165 g_object_notify (G_OBJECT (combo_box), "active-id");
4170 * gtk_combo_box_get_active_iter:
4171 * @combo_box: A #GtkComboBox
4172 * @iter: (out): The uninitialized #GtkTreeIter
4174 * Sets @iter to point to the current active item, if it exists.
4176 * Return value: %TRUE, if @iter was set
4181 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4187 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4189 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4192 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4193 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4194 gtk_tree_path_free (path);
4200 * gtk_combo_box_set_active_iter:
4201 * @combo_box: A #GtkComboBox
4202 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4204 * Sets the current active item to be the one referenced by @iter, or
4205 * unsets the active item if @iter is %NULL.
4210 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4213 GtkTreePath *path = NULL;
4215 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4218 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4220 gtk_combo_box_set_active_internal (combo_box, path);
4221 gtk_tree_path_free (path);
4225 * gtk_combo_box_set_model:
4226 * @combo_box: A #GtkComboBox
4227 * @model: (allow-none): A #GtkTreeModel
4229 * Sets the model used by @combo_box to be @model. Will unset a previously set
4230 * model (if applicable). If model is %NULL, then it will unset the model.
4232 * Note that this function does not clear the cell renderers, you have to
4233 * call gtk_cell_layout_clear() yourself if you need to set up different
4234 * cell renderers for the new model.
4239 gtk_combo_box_set_model (GtkComboBox *combo_box,
4240 GtkTreeModel *model)
4242 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4243 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4245 if (model == combo_box->priv->model)
4248 gtk_combo_box_unset_model (combo_box);
4253 combo_box->priv->model = model;
4254 g_object_ref (combo_box->priv->model);
4256 combo_box->priv->inserted_id =
4257 g_signal_connect (combo_box->priv->model, "row-inserted",
4258 G_CALLBACK (gtk_combo_box_model_row_inserted),
4260 combo_box->priv->deleted_id =
4261 g_signal_connect (combo_box->priv->model, "row-deleted",
4262 G_CALLBACK (gtk_combo_box_model_row_deleted),
4264 combo_box->priv->reordered_id =
4265 g_signal_connect (combo_box->priv->model, "rows-reordered",
4266 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4268 combo_box->priv->changed_id =
4269 g_signal_connect (combo_box->priv->model, "row-changed",
4270 G_CALLBACK (gtk_combo_box_model_row_changed),
4273 if (combo_box->priv->tree_view)
4276 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4277 combo_box->priv->model);
4278 gtk_combo_box_list_popup_resize (combo_box);
4281 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4284 gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4285 combo_box->priv->model);
4288 if (combo_box->priv->cell_view)
4289 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4290 combo_box->priv->model);
4292 if (combo_box->priv->active != -1)
4294 /* If an index was set in advance, apply it now */
4295 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4296 combo_box->priv->active = -1;
4300 gtk_combo_box_update_sensitivity (combo_box);
4302 g_object_notify (G_OBJECT (combo_box), "model");
4306 * gtk_combo_box_get_model:
4307 * @combo_box: A #GtkComboBox
4309 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4311 * Return value: (transfer none): A #GtkTreeModel which was passed
4312 * during construction.
4317 gtk_combo_box_get_model (GtkComboBox *combo_box)
4319 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4321 return combo_box->priv->model;
4325 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4326 GtkScrollType scroll)
4329 GtkTreeIter new_iter;
4330 gboolean active_iter;
4333 if (!combo_box->priv->model)
4335 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4339 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4343 case GTK_SCROLL_STEP_BACKWARD:
4344 case GTK_SCROLL_STEP_UP:
4345 case GTK_SCROLL_STEP_LEFT:
4348 found = tree_prev (combo_box, combo_box->priv->model,
4349 &iter, &new_iter, FALSE);
4352 /* else fall through */
4354 case GTK_SCROLL_PAGE_FORWARD:
4355 case GTK_SCROLL_PAGE_DOWN:
4356 case GTK_SCROLL_PAGE_RIGHT:
4357 case GTK_SCROLL_END:
4358 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4361 case GTK_SCROLL_STEP_FORWARD:
4362 case GTK_SCROLL_STEP_DOWN:
4363 case GTK_SCROLL_STEP_RIGHT:
4366 found = tree_next (combo_box, combo_box->priv->model,
4367 &iter, &new_iter, FALSE);
4370 /* else fall through */
4372 case GTK_SCROLL_PAGE_BACKWARD:
4373 case GTK_SCROLL_PAGE_UP:
4374 case GTK_SCROLL_PAGE_LEFT:
4375 case GTK_SCROLL_START:
4376 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4383 if (found && active_iter)
4385 GtkTreePath *old_path;
4386 GtkTreePath *new_path;
4388 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4389 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4391 if (gtk_tree_path_compare (old_path, new_path) == 0)
4394 gtk_tree_path_free (old_path);
4395 gtk_tree_path_free (new_path);
4400 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4404 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4409 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4410 gboolean group_cycling)
4412 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4414 if (combo_box->priv->has_entry)
4418 child = gtk_bin_get_child (GTK_BIN (combo_box));
4420 gtk_widget_grab_focus (child);
4423 gtk_widget_grab_focus (combo_box->priv->button);
4429 gtk_combo_box_grab_focus (GtkWidget *widget)
4431 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4433 if (combo_box->priv->has_entry)
4437 child = gtk_bin_get_child (GTK_BIN (combo_box));
4439 gtk_widget_grab_focus (child);
4442 gtk_widget_grab_focus (combo_box->priv->button);
4446 gtk_combo_box_destroy (GtkWidget *widget)
4448 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4450 if (combo_box->priv->popup_idle_id > 0)
4452 g_source_remove (combo_box->priv->popup_idle_id);
4453 combo_box->priv->popup_idle_id = 0;
4456 gtk_combo_box_popdown (combo_box);
4458 if (combo_box->priv->row_separator_destroy)
4459 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4461 combo_box->priv->row_separator_func = NULL;
4462 combo_box->priv->row_separator_data = NULL;
4463 combo_box->priv->row_separator_destroy = NULL;
4465 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4466 combo_box->priv->cell_view = NULL;
4470 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4473 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4476 * Fixes regression reported in bug #574059. The old functionality relied on
4477 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4478 * when the selection was already set to -1.
4480 if (gtk_combo_box_get_active(combo_box) == -1)
4481 g_signal_emit_by_name (combo_box, "changed");
4483 gtk_combo_box_set_active (combo_box, -1);
4487 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4490 GtkComboBoxPrivate *priv = combo_box->priv;
4491 GtkTreeModel *model;
4494 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4496 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4500 GValue value = {0,};
4502 g_signal_handlers_block_by_func (entry,
4503 gtk_combo_box_entry_contents_changed,
4506 model = gtk_combo_box_get_model (combo_box);
4508 gtk_tree_model_get_value (model, &iter,
4509 priv->text_column, &value);
4510 g_object_set_property (G_OBJECT (entry), "text", &value);
4511 g_value_unset (&value);
4513 g_signal_handlers_unblock_by_func (entry,
4514 gtk_combo_box_entry_contents_changed,
4521 gtk_combo_box_constructor (GType type,
4522 guint n_construct_properties,
4523 GObjectConstructParam *construct_properties)
4526 GtkComboBox *combo_box;
4527 GtkComboBoxPrivate *priv;
4528 GtkStyleContext *context;
4530 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4531 (type, n_construct_properties, construct_properties);
4533 combo_box = GTK_COMBO_BOX (object);
4534 priv = combo_box->priv;
4538 GtkCellArea *area = gtk_cell_area_box_new ();
4540 priv->area = g_object_ref_sink (area);
4543 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4544 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4545 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4546 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4547 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4548 gtk_widget_show (priv->cell_view);
4550 gtk_combo_box_check_appearance (combo_box);
4552 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4553 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
4555 if (priv->has_entry)
4559 entry = gtk_entry_new ();
4560 gtk_widget_show (entry);
4561 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4563 priv->text_renderer = gtk_cell_renderer_text_new ();
4564 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4565 priv->text_renderer, TRUE);
4567 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4569 g_signal_connect (combo_box, "changed",
4570 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4578 gtk_combo_box_dispose(GObject* object)
4580 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4582 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4584 gtk_combo_box_menu_destroy (combo_box);
4585 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4586 combo_box->priv->popup_widget = NULL;
4589 if (combo_box->priv->area)
4591 g_object_unref (combo_box->priv->area);
4592 combo_box->priv->area = NULL;
4595 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4596 gtk_combo_box_list_destroy (combo_box);
4598 if (combo_box->priv->popup_window)
4600 gtk_widget_destroy (combo_box->priv->popup_window);
4601 combo_box->priv->popup_window = NULL;
4604 gtk_combo_box_unset_model (combo_box);
4606 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4610 gtk_combo_box_finalize (GObject *object)
4612 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4614 g_free (combo_box->priv->tearoff_title);
4616 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4620 gtk_cell_editable_key_press (GtkWidget *widget,
4624 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4626 if (event->keyval == GDK_KEY_Escape)
4628 g_object_set (combo_box,
4629 "editing-canceled", TRUE,
4631 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4632 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4636 else if (event->keyval == GDK_KEY_Return ||
4637 event->keyval == GDK_KEY_ISO_Enter ||
4638 event->keyval == GDK_KEY_KP_Enter)
4640 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4641 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4650 popdown_idle (gpointer data)
4652 GtkComboBox *combo_box;
4654 combo_box = GTK_COMBO_BOX (data);
4656 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4657 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4659 g_object_unref (combo_box);
4665 popdown_handler (GtkWidget *widget,
4668 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4672 popup_idle (gpointer data)
4674 GtkComboBox *combo_box;
4676 combo_box = GTK_COMBO_BOX (data);
4678 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4679 combo_box->priv->cell_view)
4680 g_signal_connect_object (combo_box->priv->popup_widget,
4681 "unmap", G_CALLBACK (popdown_handler),
4684 /* we unset this if a menu item is activated */
4685 g_object_set (combo_box,
4686 "editing-canceled", TRUE,
4688 gtk_combo_box_popup (combo_box);
4690 combo_box->priv->popup_idle_id = 0;
4691 combo_box->priv->activate_button = 0;
4692 combo_box->priv->activate_time = 0;
4698 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4701 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4704 combo_box->priv->is_cell_renderer = TRUE;
4706 if (combo_box->priv->cell_view)
4708 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4709 G_CALLBACK (gtk_cell_editable_key_press),
4712 gtk_widget_grab_focus (combo_box->priv->button);
4716 child = gtk_bin_get_child (GTK_BIN (combo_box));
4718 g_signal_connect_object (child, "key-press-event",
4719 G_CALLBACK (gtk_cell_editable_key_press),
4722 gtk_widget_grab_focus (child);
4723 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4726 /* we do the immediate popup only for the optionmenu-like
4729 if (combo_box->priv->is_cell_renderer &&
4730 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4732 if (event && event->type == GDK_BUTTON_PRESS)
4734 GdkEventButton *event_button = (GdkEventButton *)event;
4736 combo_box->priv->activate_button = event_button->button;
4737 combo_box->priv->activate_time = event_button->time;
4740 combo_box->priv->popup_idle_id =
4741 gdk_threads_add_idle (popup_idle, combo_box);
4747 * gtk_combo_box_get_add_tearoffs:
4748 * @combo_box: a #GtkComboBox
4750 * Gets the current value of the :add-tearoffs property.
4752 * Return value: the current value of the :add-tearoffs property.
4755 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4757 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4759 return combo_box->priv->add_tearoffs;
4763 * gtk_combo_box_set_add_tearoffs:
4764 * @combo_box: a #GtkComboBox
4765 * @add_tearoffs: %TRUE to add tearoff menu items
4767 * Sets whether the popup menu should have a tearoff
4773 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4774 gboolean add_tearoffs)
4776 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4778 add_tearoffs = add_tearoffs != FALSE;
4780 if (combo_box->priv->add_tearoffs != add_tearoffs)
4782 combo_box->priv->add_tearoffs = add_tearoffs;
4783 gtk_combo_box_check_appearance (combo_box);
4784 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4789 * gtk_combo_box_get_title:
4790 * @combo_box: a #GtkComboBox
4792 * Gets the current title of the menu in tearoff mode. See
4793 * gtk_combo_box_set_add_tearoffs().
4795 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4796 * string which must not be freed.
4800 G_CONST_RETURN gchar*
4801 gtk_combo_box_get_title (GtkComboBox *combo_box)
4803 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4805 return combo_box->priv->tearoff_title;
4809 gtk_combo_box_update_title (GtkComboBox *combo_box)
4811 gtk_combo_box_check_appearance (combo_box);
4813 if (combo_box->priv->popup_widget &&
4814 GTK_IS_MENU (combo_box->priv->popup_widget))
4815 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4816 combo_box->priv->tearoff_title);
4820 * gtk_combo_box_set_title:
4821 * @combo_box: a #GtkComboBox
4822 * @title: a title for the menu in tearoff mode
4824 * Sets the menu's title in tearoff mode.
4829 gtk_combo_box_set_title (GtkComboBox *combo_box,
4832 GtkComboBoxPrivate *priv;
4834 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4836 priv = combo_box->priv;
4838 if (strcmp (title ? title : "",
4839 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4841 g_free (priv->tearoff_title);
4842 priv->tearoff_title = g_strdup (title);
4844 gtk_combo_box_update_title (combo_box);
4846 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4852 * gtk_combo_box_set_popup_fixed_width:
4853 * @combo_box: a #GtkComboBox
4854 * @fixed: whether to use a fixed popup width
4856 * Specifies whether the popup's width should be a fixed width
4857 * matching the allocated width of the combo box.
4862 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4865 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4867 if (combo_box->priv->popup_fixed_width != fixed)
4869 combo_box->priv->popup_fixed_width = fixed;
4871 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
4876 * gtk_combo_box_get_popup_fixed_width:
4877 * @combo_box: a #GtkComboBox
4879 * Gets whether the popup uses a fixed width matching
4880 * the allocated width of the combo box.
4882 * Returns: %TRUE if the popup uses a fixed width
4887 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
4889 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4891 return combo_box->priv->popup_fixed_width;
4896 * gtk_combo_box_get_popup_accessible:
4897 * @combo_box: a #GtkComboBox
4899 * Gets the accessible object corresponding to the combo box's popup.
4901 * This function is mostly intended for use by accessibility technologies;
4902 * applications should have little use for it.
4904 * Returns: (transfer none): the accessible object corresponding
4905 * to the combo box's popup.
4910 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
4914 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4916 if (combo_box->priv->popup_widget)
4918 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
4926 * gtk_combo_box_get_row_separator_func:
4927 * @combo_box: a #GtkComboBox
4929 * Returns the current row separator function.
4931 * Return value: the current row separator function.
4935 GtkTreeViewRowSeparatorFunc
4936 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
4938 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4940 return combo_box->priv->row_separator_func;
4944 * gtk_combo_box_set_row_separator_func:
4945 * @combo_box: a #GtkComboBox
4946 * @func: a #GtkTreeViewRowSeparatorFunc
4947 * @data: (allow-none): user data to pass to @func, or %NULL
4948 * @destroy: (allow-none): destroy notifier for @data, or %NULL
4950 * Sets the row separator function, which is used to determine
4951 * whether a row should be drawn as a separator. If the row separator
4952 * function is %NULL, no separators are drawn. This is the default value.
4957 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
4958 GtkTreeViewRowSeparatorFunc func,
4960 GDestroyNotify destroy)
4962 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4964 if (combo_box->priv->row_separator_destroy)
4965 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4967 combo_box->priv->row_separator_func = func;
4968 combo_box->priv->row_separator_data = data;
4969 combo_box->priv->row_separator_destroy = destroy;
4971 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
4972 if (combo_box->priv->tree_view)
4974 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
4975 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
4978 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4980 gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
4981 gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
4984 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4988 * gtk_combo_box_set_button_sensitivity:
4989 * @combo_box: a #GtkComboBox
4990 * @sensitivity: specify the sensitivity of the dropdown button
4992 * Sets whether the dropdown button of the combo box should be
4993 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
4994 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
4999 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5000 GtkSensitivityType sensitivity)
5002 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5004 if (combo_box->priv->button_sensitivity != sensitivity)
5006 combo_box->priv->button_sensitivity = sensitivity;
5007 gtk_combo_box_update_sensitivity (combo_box);
5009 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5014 * gtk_combo_box_get_button_sensitivity:
5015 * @combo_box: a #GtkComboBox
5017 * Returns whether the combo box sets the dropdown button
5018 * sensitive or not when there are no items in the model.
5020 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5021 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5022 * if the button is always insensitive or
5023 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5024 * the model has one item to be selected.
5029 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5031 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5033 return combo_box->priv->button_sensitivity;
5038 * gtk_combo_box_get_has_entry:
5039 * @combo_box: a #GtkComboBox
5041 * Returns whether the combo box has an entry.
5043 * Return Value: whether there is an entry in @combo_box.
5048 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5050 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5052 return combo_box->priv->has_entry;
5056 * gtk_combo_box_set_entry_text_column:
5057 * @combo_box: A #GtkComboBox
5058 * @text_column: A column in @model to get the strings from for
5059 * the internal entry
5061 * Sets the model column which @combo_box should use to get strings from
5062 * to be @text_column. The column @text_column in the model of @combo_box
5063 * must be of type %G_TYPE_STRING.
5065 * This is only relevant if @combo_box has been created with
5066 * #GtkComboBox:has-entry as %TRUE.
5071 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5074 GtkComboBoxPrivate *priv = combo_box->priv;
5075 GtkTreeModel *model;
5077 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5079 model = gtk_combo_box_get_model (combo_box);
5081 g_return_if_fail (text_column >= 0);
5082 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5084 priv->text_column = text_column;
5086 if (priv->text_renderer != NULL)
5087 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5088 priv->text_renderer,
5089 "text", text_column,
5094 * gtk_combo_box_get_entry_text_column:
5095 * @combo_box: A #GtkComboBox.
5097 * Returns the column which @combo_box is using to get the strings
5098 * from to display in the internal entry.
5100 * Return value: A column in the data source model of @combo_box.
5105 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5107 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5109 return combo_box->priv->text_column;
5113 * gtk_combo_box_set_focus_on_click:
5114 * @combo: a #GtkComboBox
5115 * @focus_on_click: whether the combo box grabs focus when clicked
5118 * Sets whether the combo box will grab focus when it is clicked with
5119 * the mouse. Making mouse clicks not grab focus is useful in places
5120 * like toolbars where you don't want the keyboard focus removed from
5121 * the main area of the application.
5126 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5127 gboolean focus_on_click)
5129 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5131 focus_on_click = focus_on_click != FALSE;
5133 if (combo_box->priv->focus_on_click != focus_on_click)
5135 combo_box->priv->focus_on_click = focus_on_click;
5137 if (combo_box->priv->button)
5138 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5141 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5146 * gtk_combo_box_get_focus_on_click:
5147 * @combo: a #GtkComboBox
5149 * Returns whether the combo box grabs focus when it is clicked
5150 * with the mouse. See gtk_combo_box_set_focus_on_click().
5152 * Return value: %TRUE if the combo box grabs focus when it is
5153 * clicked with the mouse.
5158 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5160 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5162 return combo_box->priv->focus_on_click;
5167 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5168 GtkBuilder *builder,
5170 const gchar *tagname,
5171 GMarkupParser *parser,
5174 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5175 tagname, parser, data))
5178 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5179 tagname, parser, data);
5183 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5184 GtkBuilder *builder,
5186 const gchar *tagname,
5189 if (strcmp (tagname, "attributes") == 0)
5190 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5193 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5198 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5199 GtkBuilder *builder,
5200 const gchar *childname)
5202 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5204 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5205 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5207 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5211 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5215 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5216 GtkComboBoxPrivate *priv = combo_box->priv;
5217 gint focus_width, focus_pad;
5218 gint font_size, arrow_size;
5219 PangoContext *context;
5220 PangoFontMetrics *metrics;
5221 PangoFontDescription *font_desc;
5223 gint minimum_width = 0, natural_width = 0;
5224 gint child_min, child_nat;
5225 GtkStyleContext *style_context;
5226 GtkStateFlags state;
5229 child = gtk_bin_get_child (GTK_BIN (widget));
5232 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5234 gtk_widget_style_get (GTK_WIDGET (widget),
5235 "focus-line-width", &focus_width,
5236 "focus-padding", &focus_pad,
5237 "arrow-size", &arrow_size,
5240 style_context = gtk_widget_get_style_context (widget);
5241 state = gtk_widget_get_state_flags (widget);
5243 gtk_style_context_get (style_context, state,
5245 "border-width", &border,
5248 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5249 metrics = pango_context_get_metrics (context, font_desc,
5250 pango_context_get_language (context));
5251 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5252 pango_font_metrics_get_descent (metrics));
5253 pango_font_metrics_unref (metrics);
5254 pango_font_description_free (font_desc);
5256 arrow_size = MAX (arrow_size, font_size);
5258 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5260 if (!priv->tree_view)
5263 if (priv->cell_view)
5265 gint sep_width, arrow_width;
5266 gint border_width, xpad;
5267 GtkBorder button_border;
5269 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5270 get_widget_border (priv->button, &button_border);
5272 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5273 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5275 xpad = 2 * (border_width + focus_width + focus_pad) +
5276 button_border.left + button_border.right;
5278 minimum_width = child_min + sep_width + arrow_width + xpad;
5279 natural_width = child_nat + sep_width + arrow_width + xpad;
5283 gint but_width, but_nat_width;
5285 gtk_widget_get_preferred_width (priv->button,
5286 &but_width, &but_nat_width);
5288 minimum_width = child_min + but_width;
5289 natural_width = child_nat + but_nat_width;
5295 gint button_width, button_nat_width;
5297 /* sample + frame */
5298 minimum_width = child_min;
5299 natural_width = child_nat;
5301 minimum_width += 2 * focus_width;
5302 natural_width += 2 * focus_width;
5304 if (priv->cell_view_frame)
5306 if (priv->has_frame)
5308 gint border_width, xpad;
5309 GtkBorder frame_border;
5311 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5312 get_widget_border (priv->cell_view_frame, &frame_border);
5313 xpad = (2 * border_width) + frame_border.left + frame_border.right;
5315 minimum_width += xpad;
5316 natural_width += xpad;
5321 gtk_widget_get_preferred_width (priv->button,
5322 &button_width, &button_nat_width);
5324 minimum_width += button_width;
5325 natural_width += button_nat_width;
5328 minimum_width += border->left + border->right;
5329 natural_width += border->left + border->right;
5330 gtk_border_free (border);
5333 *minimum_size = minimum_width;
5336 *natural_size = natural_width;
5340 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5346 /* Combo box is height-for-width only
5347 * (so we always just reserve enough height for the minimum width) */
5348 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5349 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5353 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5358 /* Combo box is height-for-width only
5359 * (so we assume we always reserved enough height for the minimum width) */
5360 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5365 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5370 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5371 GtkComboBoxPrivate *priv = combo_box->priv;
5372 gint focus_width, focus_pad;
5373 gint min_height = 0, nat_height = 0;
5378 gtk_widget_style_get (GTK_WIDGET (widget),
5379 "focus-line-width", &focus_width,
5380 "focus-padding", &focus_pad,
5383 child = gtk_bin_get_child (GTK_BIN (widget));
5385 get_widget_border (widget, &border);
5386 size = avail_size - border.left;
5388 if (!priv->tree_view)
5391 if (priv->cell_view)
5393 /* calculate x/y padding and separator/arrow size */
5394 gint sep_width, arrow_width, sep_height, arrow_height;
5395 gint border_width, xpad, ypad;
5396 GtkBorder button_border;
5398 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5399 get_widget_border (priv->button, &button_border);
5401 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5402 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5403 gtk_widget_get_preferred_height_for_width (priv->separator,
5404 sep_width, &sep_height, NULL);
5405 gtk_widget_get_preferred_height_for_width (priv->arrow,
5406 arrow_width, &arrow_height, NULL);
5408 xpad = 2 * (border_width + focus_width + focus_pad) +
5409 button_border.left + button_border.right;
5410 ypad = 2 * (border_width + focus_width + focus_pad) +
5411 button_border.top + button_border.bottom;
5413 size -= sep_width + arrow_width + xpad;
5415 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5416 * and fitting the whole treemodel */
5417 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5419 arrow_height = MAX (arrow_height, sep_height);
5420 min_height = MAX (min_height, arrow_height);
5421 nat_height = MAX (nat_height, arrow_height);
5428 /* there is a custom child widget inside (no priv->cell_view) */
5429 gint but_width, but_height;
5431 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5432 gtk_widget_get_preferred_height_for_width (priv->button,
5433 but_width, &but_height, NULL);
5437 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5438 * and fitting the whole treemodel */
5439 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5441 min_height = MAX (min_height, but_height);
5442 nat_height = MAX (nat_height, but_height);
5448 gint but_width, but_height;
5449 gint xpad = 0, ypad = 0;
5451 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5452 gtk_widget_get_preferred_height_for_width (priv->button,
5453 but_width, &but_height, NULL);
5455 if (priv->cell_view_frame && priv->has_frame)
5457 GtkBorder frame_border;
5460 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5461 get_widget_border (GTK_WIDGET (priv->cell_view_frame), &frame_border);
5463 xpad = (2 * border_width) + border.left + frame_border.right;
5464 ypad = (2 * border_width) + border.top + frame_border.bottom;
5468 size -= 2 * focus_width;
5471 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5472 * and fitting the whole treemodel */
5473 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5475 min_height = MAX (min_height, but_height);
5476 nat_height = MAX (nat_height, but_height);
5482 min_height += border.top + border.bottom;
5483 nat_height += border.top + border.bottom;
5486 *minimum_size = min_height;
5489 *natural_size = nat_height;
5493 * gtk_combo_box_set_id_column:
5494 * @combo_box: A #GtkComboBox
5495 * @id_column: A column in @model to get string IDs for values from
5497 * Sets the model column which @combo_box should use to get string IDs
5498 * for values from. The column @id_column in the model of @combo_box
5499 * must be of type %G_TYPE_STRING.
5504 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5507 GtkComboBoxPrivate *priv = combo_box->priv;
5508 GtkTreeModel *model;
5510 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5512 if (id_column != priv->id_column)
5514 model = gtk_combo_box_get_model (combo_box);
5516 g_return_if_fail (id_column >= 0);
5517 g_return_if_fail (model == NULL ||
5518 id_column < gtk_tree_model_get_n_columns (model));
5520 priv->id_column = id_column;
5522 g_object_notify (G_OBJECT (combo_box), "id-column");
5523 g_object_notify (G_OBJECT (combo_box), "active-id");
5528 * gtk_combo_box_get_id_column:
5529 * @combo_box: A #GtkComboBox
5531 * Returns the column which @combo_box is using to get string IDs
5534 * Return value: A column in the data source model of @combo_box.
5539 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5541 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5543 return combo_box->priv->id_column;
5547 * gtk_combo_box_get_active_id:
5548 * @combo_box: a #GtkComboBox
5550 * Returns the ID of the active row of @combo_box. This value is taken
5551 * from the active row and the column specified by the 'id-column'
5552 * property of @combo_box (see gtk_combo_box_set_id_column()).
5554 * The returned value is an interned string which means that you can
5555 * compare the pointer by value to other interned strings and that you
5558 * If the 'id-column' property of @combo_box is not set or if no row is
5559 * selected then %NULL is returned.
5561 * Return value: the ID of the active row, or %NULL
5566 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5568 GtkTreeModel *model;
5572 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5574 column = combo_box->priv->id_column;
5579 model = gtk_combo_box_get_model (combo_box);
5580 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5581 G_TYPE_STRING, NULL);
5583 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5585 const gchar *interned;
5588 gtk_tree_model_get (model, &iter, column, &id, -1);
5589 interned = g_intern_string (id);
5599 * gtk_combo_box_set_active_id:
5600 * @combo_box: a #GtkComboBox
5601 * @active_id: the ID of the row to select
5603 * Changes the active row of @combo_box to the one that has an ID equal to @id.
5605 * If the 'id-column' property of @combo_box is unset or if no row has
5606 * the given ID then nothing happens.
5611 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5612 const gchar *active_id)
5614 GtkTreeModel *model;
5618 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5620 column = combo_box->priv->id_column;
5625 model = gtk_combo_box_get_model (combo_box);
5626 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
5629 if (gtk_tree_model_get_iter_first (model, &iter))
5634 gtk_tree_model_get (model, &iter, column, &id, -1);
5635 match = strcmp (id, active_id) == 0;
5640 gtk_combo_box_set_active_iter (combo_box, &iter);
5643 } while (gtk_tree_model_iter_next (model, &iter));