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"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellview.h"
28 #include "gtkeventbox.h"
31 #include "gtkliststore.h"
34 #include "gtkscrolledwindow.h"
35 #include "gtkseparatormenuitem.h"
36 #include "gtktearoffmenuitem.h"
37 #include "gtktogglebutton.h"
38 #include "gtktreeselection.h"
39 #include "gtkvseparator.h"
40 #include "gtkwindow.h"
41 #include "gtkprivate.h"
43 #include <gdk/gdkkeysyms.h>
45 #include <gobject/gvaluecollector.h>
50 #include "gtkmarshalers.h"
53 #include "gtktreeprivate.h"
58 * @Short_description: A widget used to choose from a list of items
60 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
62 * A GtkComboBox is a widget that allows the user to choose from a list of
63 * valid choices. The GtkComboBox displays the selected choice. When
64 * activated, the GtkComboBox displays a popup which allows the user to
65 * make a new choice. The style in which the selected value is displayed,
66 * and the style of the popup is determined by the current theme. It may
67 * be similar to a Windows-style combo box.
69 * The GtkComboBox uses the model-view pattern; the list of valid choices
70 * is specified in the form of a tree model, and the display of the choices
71 * can be adapted to the data in the model by using cell renderers, as you
72 * would in a tree view. This is possible since GtkComboBox implements the
73 * #GtkCellLayout interface. The tree model holding the valid choices is
74 * not restricted to a flat list, it can be a real tree, and the popup will
75 * reflect the tree structure.
77 * For a simple list of textual choices, the model-view API of GtkComboBox
78 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
83 /* WELCOME, to THE house of evil code */
85 typedef struct _ComboCellInfo ComboCellInfo;
88 GtkCellRenderer *cell;
91 GtkCellLayoutDataFunc func;
93 GDestroyNotify destroy;
100 struct _GtkComboBoxPrivate
108 GtkShadowType shadow_type;
110 gint active; /* Only temporary */
111 GtkTreeRowReference *active_row;
113 GtkWidget *tree_view;
114 GtkTreeViewColumn *column;
116 GtkWidget *cell_view;
117 GtkWidget *cell_view_frame;
122 GtkWidget *separator;
124 GtkWidget *popup_widget;
125 GtkWidget *popup_window;
126 GtkWidget *scrolled_window;
133 guint activate_button;
134 guint32 activate_time;
136 guint resize_idle_id;
141 /* For "has-entry" specific behavior we track
142 * an automated cell renderer and text column */
144 GtkCellRenderer *text_renderer;
150 guint popup_in_progress : 1;
151 guint popup_shown : 1;
152 guint add_tearoffs : 1;
154 guint is_cell_renderer : 1;
155 guint editing_canceled : 1;
156 guint auto_scroll : 1;
157 guint focus_on_click : 1;
158 guint button_sensitivity : 2;
160 guint popup_fixed_width : 1;
162 GtkTreeViewRowSeparatorFunc row_separator_func;
163 gpointer row_separator_data;
164 GDestroyNotify row_separator_destroy;
166 GdkDevice *grab_pointer;
167 GdkDevice *grab_keyboard;
169 gchar *tearoff_title;
172 /* While debugging this evil code, I have learned that
173 * there are actually 4 modes to this widget, which can
174 * be characterized as follows
176 * 1) menu mode, no child added
179 * cell_view -> GtkCellView, regular child
180 * cell_view_frame -> NULL
181 * button -> GtkToggleButton set_parent to combo
182 * arrow -> GtkArrow set_parent to button
183 * separator -> GtkVSepator set_parent to button
184 * popup_widget -> GtkMenu
185 * popup_window -> NULL
186 * scrolled_window -> NULL
188 * 2) menu mode, child added
192 * cell_view_frame -> NULL
193 * button -> GtkToggleButton set_parent to combo
194 * arrow -> GtkArrow, child of button
196 * popup_widget -> GtkMenu
197 * popup_window -> NULL
198 * scrolled_window -> NULL
200 * 3) list mode, no child added
202 * tree_view -> GtkTreeView, child of scrolled_window
203 * cell_view -> GtkCellView, regular child
204 * cell_view_frame -> GtkFrame, set parent to combo
205 * button -> GtkToggleButton, set_parent to combo
206 * arrow -> GtkArrow, child of button
208 * popup_widget -> tree_view
209 * popup_window -> GtkWindow
210 * scrolled_window -> GtkScrolledWindow, child of popup_window
212 * 4) list mode, child added
214 * tree_view -> GtkTreeView, child of scrolled_window
216 * cell_view_frame -> NULL
217 * button -> GtkToggleButton, set_parent to combo
218 * arrow -> GtkArrow, child of button
220 * popup_widget -> tree_view
221 * popup_window -> GtkWindow
222 * scrolled_window -> GtkScrolledWindow, child of popup_window
238 PROP_ROW_SPAN_COLUMN,
239 PROP_COLUMN_SPAN_COLUMN,
246 PROP_BUTTON_SENSITIVITY,
247 PROP_EDITING_CANCELED,
249 PROP_ENTRY_TEXT_COLUMN,
250 PROP_POPUP_FIXED_WIDTH,
255 static guint combo_box_signals[LAST_SIGNAL] = {0,};
257 #define BONUS_PADDING 4
258 #define SCROLL_TIME 100
262 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
263 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
264 static GObject *gtk_combo_box_constructor (GType type,
265 guint n_construct_properties,
266 GObjectConstructParam *construct_properties);
267 static void gtk_combo_box_dispose (GObject *object);
268 static void gtk_combo_box_finalize (GObject *object);
269 static void gtk_combo_box_destroy (GtkWidget *widget);
271 static void gtk_combo_box_set_property (GObject *object,
275 static void gtk_combo_box_get_property (GObject *object,
280 static void gtk_combo_box_state_changed (GtkWidget *widget,
281 GtkStateType previous);
282 static void gtk_combo_box_grab_focus (GtkWidget *widget);
283 static void gtk_combo_box_style_updated (GtkWidget *widget);
284 static void gtk_combo_box_button_toggled (GtkWidget *widget,
286 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
287 GtkStateFlags previous,
289 static void gtk_combo_box_add (GtkContainer *container,
291 static void gtk_combo_box_remove (GtkContainer *container,
294 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
295 GtkCellRenderer *cell);
297 static void gtk_combo_box_menu_show (GtkWidget *menu,
299 static void gtk_combo_box_menu_hide (GtkWidget *menu,
302 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
304 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
309 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
314 static void gtk_combo_box_menu_position (GtkMenu *menu,
320 static void gtk_combo_box_update_requested_width(GtkComboBox *combo_box,
322 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
324 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
326 static void gtk_combo_box_size_allocate (GtkWidget *widget,
327 GtkAllocation *allocation);
328 static void gtk_combo_box_forall (GtkContainer *container,
329 gboolean include_internals,
330 GtkCallback callback,
331 gpointer callback_data);
332 static gboolean gtk_combo_box_draw (GtkWidget *widget,
334 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
335 GdkEventScroll *event);
336 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
339 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
340 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
341 GtkScrollType scroll);
342 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
343 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
345 /* listening to the model */
346 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
350 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
353 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
358 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
362 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
368 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
373 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
374 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
376 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
377 GdkEventButton *event,
379 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
382 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
383 GdkEventCrossing *event,
385 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
388 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
389 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
390 GdkEventButton *event,
393 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
396 gboolean path_currently_selected,
399 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
403 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
406 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
407 gboolean add_children);
408 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
409 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
412 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
413 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
415 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
419 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
421 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
422 GdkEventButton *event,
424 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
427 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
428 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
432 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
435 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
440 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
444 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
447 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
449 guint32 activate_time);
450 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
455 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
456 GtkCellRenderer *cell,
458 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
459 GtkCellRenderer *cell,
461 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
462 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
463 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
464 GtkCellRenderer *cell,
465 const gchar *attribute,
467 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
468 GtkCellRenderer *cell,
469 GtkCellLayoutDataFunc func,
471 GDestroyNotify destroy);
472 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
473 GtkCellRenderer *cell);
474 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
475 GtkCellRenderer *cell,
477 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
478 gboolean group_cycling);
480 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
481 GtkCellLayout *cell_layout);
482 static void combo_cell_data_func (GtkCellLayout *cell_layout,
483 GtkCellRenderer *cell,
484 GtkTreeModel *tree_model,
487 static void gtk_combo_box_child_show (GtkWidget *widget,
488 GtkComboBox *combo_box);
489 static void gtk_combo_box_child_hide (GtkWidget *widget,
490 GtkComboBox *combo_box);
492 /* GtkComboBox:has-entry callbacks */
493 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
495 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
499 /* GtkBuildable method implementation */
500 static GtkBuildableIface *parent_buildable_iface;
502 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
503 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
506 const gchar *tagname,
507 GMarkupParser *parser,
509 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
512 const gchar *tagname,
514 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
516 const gchar *childname);
519 /* GtkCellEditable method implementations */
520 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
523 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
526 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
529 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
533 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
539 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
540 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
541 gtk_combo_box_cell_layout_init)
542 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
543 gtk_combo_box_cell_editable_init)
544 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
545 gtk_combo_box_buildable_init))
550 gtk_combo_box_class_init (GtkComboBoxClass *klass)
552 GObjectClass *object_class;
553 GtkContainerClass *container_class;
554 GtkWidgetClass *widget_class;
555 GtkBindingSet *binding_set;
557 container_class = (GtkContainerClass *)klass;
558 container_class->forall = gtk_combo_box_forall;
559 container_class->add = gtk_combo_box_add;
560 container_class->remove = gtk_combo_box_remove;
562 widget_class = (GtkWidgetClass *)klass;
563 widget_class->size_allocate = gtk_combo_box_size_allocate;
564 widget_class->draw = gtk_combo_box_draw;
565 widget_class->scroll_event = gtk_combo_box_scroll_event;
566 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
567 widget_class->grab_focus = gtk_combo_box_grab_focus;
568 widget_class->style_updated = gtk_combo_box_style_updated;
569 widget_class->state_changed = gtk_combo_box_state_changed;
570 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
571 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
572 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
573 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
574 widget_class->destroy = gtk_combo_box_destroy;
576 object_class = (GObjectClass *)klass;
577 object_class->constructor = gtk_combo_box_constructor;
578 object_class->dispose = gtk_combo_box_dispose;
579 object_class->finalize = gtk_combo_box_finalize;
580 object_class->set_property = gtk_combo_box_set_property;
581 object_class->get_property = gtk_combo_box_get_property;
585 * GtkComboBox::changed:
586 * @widget: the object which received the signal
588 * The changed signal is emitted when the active
589 * item is changed. The can be due to the user selecting
590 * a different item from the list, or due to a
591 * call to gtk_combo_box_set_active_iter().
592 * It will also be emitted while typing into the entry of a combo box
597 combo_box_signals[CHANGED] =
598 g_signal_new (I_("changed"),
599 G_OBJECT_CLASS_TYPE (klass),
601 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
603 g_cclosure_marshal_VOID__VOID,
606 * GtkComboBox::move-active:
607 * @widget: the object that received the signal
608 * @scroll_type: a #GtkScrollType
610 * The ::move-active signal is a
611 * <link linkend="keybinding-signals">keybinding signal</link>
612 * which gets emitted to move the active selection.
616 combo_box_signals[MOVE_ACTIVE] =
617 g_signal_new_class_handler (I_("move-active"),
618 G_OBJECT_CLASS_TYPE (klass),
619 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
620 G_CALLBACK (gtk_combo_box_real_move_active),
622 g_cclosure_marshal_VOID__ENUM,
624 GTK_TYPE_SCROLL_TYPE);
627 * GtkComboBox::popup:
628 * @widget: the object that received the signal
630 * The ::popup signal is a
631 * <link linkend="keybinding-signals">keybinding signal</link>
632 * which gets emitted to popup the combo box list.
634 * The default binding for this signal is Alt+Down.
638 combo_box_signals[POPUP] =
639 g_signal_new_class_handler (I_("popup"),
640 G_OBJECT_CLASS_TYPE (klass),
641 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
642 G_CALLBACK (gtk_combo_box_real_popup),
644 g_cclosure_marshal_VOID__VOID,
647 * GtkComboBox::popdown:
648 * @button: the object which received the signal
650 * The ::popdown signal is a
651 * <link linkend="keybinding-signals">keybinding signal</link>
652 * which gets emitted to popdown the combo box list.
654 * The default bindings for this signal are Alt+Up and Escape.
658 combo_box_signals[POPDOWN] =
659 g_signal_new_class_handler (I_("popdown"),
660 G_OBJECT_CLASS_TYPE (klass),
661 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
662 G_CALLBACK (gtk_combo_box_real_popdown),
664 _gtk_marshal_BOOLEAN__VOID,
668 binding_set = gtk_binding_set_by_class (widget_class);
670 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
672 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
675 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
677 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
679 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
682 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
684 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
685 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
687 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
688 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
690 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
691 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
693 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
694 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
696 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
697 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
699 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
701 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
703 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
704 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
706 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
707 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
709 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
710 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
712 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
713 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
715 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
716 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
718 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
721 g_object_class_override_property (object_class,
722 PROP_EDITING_CANCELED,
728 * The model from which the combo box takes the values shown
733 g_object_class_install_property (object_class,
735 g_param_spec_object ("model",
736 P_("ComboBox model"),
737 P_("The model for the combo box"),
739 GTK_PARAM_READWRITE));
742 * GtkComboBox:wrap-width:
744 * If wrap-width is set to a positive value, the list will be
745 * displayed in multiple columns, the number of columns is
746 * determined by wrap-width.
750 g_object_class_install_property (object_class,
752 g_param_spec_int ("wrap-width",
754 P_("Wrap width for laying out the items in a grid"),
758 GTK_PARAM_READWRITE));
762 * GtkComboBox:row-span-column:
764 * If this is set to a non-negative value, it must be the index of a column
765 * of type %G_TYPE_INT in the model.
767 * The values of that column are used to determine how many rows a value in
768 * the list will span. Therefore, the values in the model column pointed to
769 * by this property must be greater than zero and not larger than wrap-width.
773 g_object_class_install_property (object_class,
774 PROP_ROW_SPAN_COLUMN,
775 g_param_spec_int ("row-span-column",
776 P_("Row span column"),
777 P_("TreeModel column containing the row span values"),
781 GTK_PARAM_READWRITE));
785 * GtkComboBox:column-span-column:
787 * If this is set to a non-negative value, it must be the index of a column
788 * of type %G_TYPE_INT in the model.
790 * The values of that column are used to determine how many columns a value
791 * in the list will span.
795 g_object_class_install_property (object_class,
796 PROP_COLUMN_SPAN_COLUMN,
797 g_param_spec_int ("column-span-column",
798 P_("Column span column"),
799 P_("TreeModel column containing the column span values"),
803 GTK_PARAM_READWRITE));
807 * GtkComboBox:active:
809 * The item which is currently active. If the model is a non-flat treemodel,
810 * and the active item is not an immediate child of the root of the tree,
811 * this property has the value
812 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
813 * where <literal>path</literal> is the #GtkTreePath of the active item.
817 g_object_class_install_property (object_class,
819 g_param_spec_int ("active",
821 P_("The item which is currently active"),
825 GTK_PARAM_READWRITE));
828 * GtkComboBox:add-tearoffs:
830 * The add-tearoffs property controls whether generated menus
831 * have tearoff menu items.
833 * Note that this only affects menu style combo boxes.
837 g_object_class_install_property (object_class,
839 g_param_spec_boolean ("add-tearoffs",
840 P_("Add tearoffs to menus"),
841 P_("Whether dropdowns should have a tearoff menu item"),
843 GTK_PARAM_READWRITE));
846 * GtkComboBox:has-frame:
848 * The has-frame property controls whether a frame
849 * is drawn around the entry.
853 g_object_class_install_property (object_class,
855 g_param_spec_boolean ("has-frame",
857 P_("Whether the combo box draws a frame around the child"),
859 GTK_PARAM_READWRITE));
861 g_object_class_install_property (object_class,
863 g_param_spec_boolean ("focus-on-click",
864 P_("Focus on click"),
865 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
867 GTK_PARAM_READWRITE));
870 * GtkComboBox:tearoff-title:
872 * A title that may be displayed by the window manager
873 * when the popup is torn-off.
877 g_object_class_install_property (object_class,
879 g_param_spec_string ("tearoff-title",
881 P_("A title that may be displayed by the window manager when the popup is torn-off"),
883 GTK_PARAM_READWRITE));
887 * GtkComboBox:popup-shown:
889 * Whether the combo boxes dropdown is popped up.
890 * Note that this property is mainly useful, because
891 * it allows you to connect to notify::popup-shown.
895 g_object_class_install_property (object_class,
897 g_param_spec_boolean ("popup-shown",
899 P_("Whether the combo's dropdown is shown"),
901 GTK_PARAM_READABLE));
905 * GtkComboBox:button-sensitivity:
907 * Whether the dropdown button is sensitive when
908 * the model is empty.
912 g_object_class_install_property (object_class,
913 PROP_BUTTON_SENSITIVITY,
914 g_param_spec_enum ("button-sensitivity",
915 P_("Button Sensitivity"),
916 P_("Whether the dropdown button is sensitive when the model is empty"),
917 GTK_TYPE_SENSITIVITY_TYPE,
918 GTK_SENSITIVITY_AUTO,
919 GTK_PARAM_READWRITE));
922 * GtkComboBox:has-entry:
924 * Whether the combo box has an entry.
928 g_object_class_install_property (object_class,
930 g_param_spec_boolean ("has-entry",
932 P_("Whether combo box has an entry"),
934 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
937 * GtkComboBox:entry-text-column:
939 * The column in the combo box's model to associate with strings from the entry
940 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
944 g_object_class_install_property (object_class,
945 PROP_ENTRY_TEXT_COLUMN,
946 g_param_spec_int ("entry-text-column",
947 P_("Entry Text Column"),
948 P_("The column in the combo box's model to associate "
949 "with strings from the entry if the combo was "
950 "created with #GtkComboBox:has-entry = %TRUE"),
952 GTK_PARAM_READWRITE));
955 * GtkComboBox:id-column:
957 * The column in the combo box's model that provides string
958 * IDs for the values in the model, if != -1.
962 g_object_class_install_property (object_class,
964 g_param_spec_int ("id-column",
966 P_("The column in the combo box's model that provides "
967 "string IDs for the values in the model"),
969 GTK_PARAM_READWRITE));
972 * GtkComboBox:active-id:
974 * The value of the ID column of the active row.
978 g_object_class_install_property (object_class,
980 g_param_spec_string ("active-id",
982 P_("The value of the id column "
983 "for the active row"),
984 NULL, GTK_PARAM_READWRITE));
987 * GtkComboBox:popup-fixed-width:
989 * Whether the popup's width should be a fixed width matching the
990 * allocated width of the combo box.
994 g_object_class_install_property (object_class,
995 PROP_POPUP_FIXED_WIDTH,
996 g_param_spec_boolean ("popup-fixed-width",
997 P_("Popup Fixed Width"),
998 P_("Whether the popup's width should be a "
999 "fixed width matching the allocated width "
1000 "of the combo box"),
1002 GTK_PARAM_READWRITE));
1004 gtk_widget_class_install_style_property (widget_class,
1005 g_param_spec_boolean ("appears-as-list",
1006 P_("Appears as list"),
1007 P_("Whether dropdowns should look like lists rather than menus"),
1009 GTK_PARAM_READABLE));
1012 * GtkComboBox:arrow-size:
1014 * Sets the minimum size of the arrow in the combo box. Note
1015 * that the arrow size is coupled to the font size, so in case
1016 * a larger font is used, the arrow will be larger than set
1021 gtk_widget_class_install_style_property (widget_class,
1022 g_param_spec_int ("arrow-size",
1024 P_("The minimum size of the arrow in the combo box"),
1028 GTK_PARAM_READABLE));
1031 * GtkComboBox:shadow-type:
1033 * Which kind of shadow to draw around the combo box.
1037 gtk_widget_class_install_style_property (widget_class,
1038 g_param_spec_enum ("shadow-type",
1040 P_("Which kind of shadow to draw around the combo box"),
1041 GTK_TYPE_SHADOW_TYPE,
1043 GTK_PARAM_READABLE));
1045 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1049 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1051 parent_buildable_iface = g_type_interface_peek_parent (iface);
1052 iface->add_child = _gtk_cell_layout_buildable_add_child;
1053 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1054 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1055 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1059 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1061 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
1062 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
1063 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
1064 iface->clear = gtk_combo_box_cell_layout_clear;
1065 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
1066 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
1067 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
1068 iface->reorder = gtk_combo_box_cell_layout_reorder;
1072 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1074 iface->start_editing = gtk_combo_box_start_editing;
1078 gtk_combo_box_init (GtkComboBox *combo_box)
1080 GtkComboBoxPrivate *priv;
1081 GtkStyleContext *context;
1083 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1085 GtkComboBoxPrivate);
1086 priv = combo_box->priv;
1088 priv->cell_view = gtk_cell_view_new ();
1089 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
1090 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
1091 gtk_widget_show (priv->cell_view);
1093 priv->minimum_width = 0;
1094 priv->natural_width = 0;
1096 priv->wrap_width = 0;
1099 priv->active_row = NULL;
1100 priv->col_column = -1;
1101 priv->row_column = -1;
1103 priv->popup_shown = FALSE;
1104 priv->add_tearoffs = FALSE;
1105 priv->has_frame = TRUE;
1106 priv->is_cell_renderer = FALSE;
1107 priv->editing_canceled = FALSE;
1108 priv->auto_scroll = FALSE;
1109 priv->focus_on_click = TRUE;
1110 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1111 priv->has_entry = FALSE;
1112 priv->popup_fixed_width = TRUE;
1114 priv->text_column = -1;
1115 priv->text_renderer = NULL;
1116 priv->id_column = -1;
1118 gtk_combo_box_check_appearance (combo_box);
1120 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
1121 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
1125 gtk_combo_box_set_property (GObject *object,
1127 const GValue *value,
1130 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1135 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1138 case PROP_WRAP_WIDTH:
1139 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1142 case PROP_ROW_SPAN_COLUMN:
1143 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1146 case PROP_COLUMN_SPAN_COLUMN:
1147 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1151 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1154 case PROP_ADD_TEAROFFS:
1155 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1158 case PROP_HAS_FRAME:
1159 combo_box->priv->has_frame = g_value_get_boolean (value);
1161 if (combo_box->priv->has_entry)
1165 child = gtk_bin_get_child (GTK_BIN (combo_box));
1167 gtk_entry_set_has_frame (GTK_ENTRY (child),
1168 combo_box->priv->has_frame);
1173 case PROP_FOCUS_ON_CLICK:
1174 gtk_combo_box_set_focus_on_click (combo_box,
1175 g_value_get_boolean (value));
1178 case PROP_TEAROFF_TITLE:
1179 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1182 case PROP_POPUP_SHOWN:
1183 if (g_value_get_boolean (value))
1184 gtk_combo_box_popup (combo_box);
1186 gtk_combo_box_popdown (combo_box);
1189 case PROP_BUTTON_SENSITIVITY:
1190 gtk_combo_box_set_button_sensitivity (combo_box,
1191 g_value_get_enum (value));
1194 case PROP_POPUP_FIXED_WIDTH:
1195 gtk_combo_box_set_popup_fixed_width (combo_box,
1196 g_value_get_boolean (value));
1199 case PROP_EDITING_CANCELED:
1200 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1203 case PROP_HAS_ENTRY:
1204 combo_box->priv->has_entry = g_value_get_boolean (value);
1207 case PROP_ENTRY_TEXT_COLUMN:
1208 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1211 case PROP_ID_COLUMN:
1212 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1215 case PROP_ACTIVE_ID:
1216 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1226 gtk_combo_box_get_property (GObject *object,
1231 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1232 GtkComboBoxPrivate *priv = combo_box->priv;
1237 g_value_set_object (value, combo_box->priv->model);
1240 case PROP_WRAP_WIDTH:
1241 g_value_set_int (value, combo_box->priv->wrap_width);
1244 case PROP_ROW_SPAN_COLUMN:
1245 g_value_set_int (value, combo_box->priv->row_column);
1248 case PROP_COLUMN_SPAN_COLUMN:
1249 g_value_set_int (value, combo_box->priv->col_column);
1253 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1256 case PROP_ADD_TEAROFFS:
1257 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1260 case PROP_HAS_FRAME:
1261 g_value_set_boolean (value, combo_box->priv->has_frame);
1264 case PROP_FOCUS_ON_CLICK:
1265 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1268 case PROP_TEAROFF_TITLE:
1269 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1272 case PROP_POPUP_SHOWN:
1273 g_value_set_boolean (value, combo_box->priv->popup_shown);
1276 case PROP_BUTTON_SENSITIVITY:
1277 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1280 case PROP_POPUP_FIXED_WIDTH:
1281 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1284 case PROP_EDITING_CANCELED:
1285 g_value_set_boolean (value, priv->editing_canceled);
1288 case PROP_HAS_ENTRY:
1289 g_value_set_boolean (value, priv->has_entry);
1292 case PROP_ENTRY_TEXT_COLUMN:
1293 g_value_set_int (value, priv->text_column);
1296 case PROP_ID_COLUMN:
1297 g_value_set_int (value, priv->id_column);
1300 case PROP_ACTIVE_ID:
1301 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1311 gtk_combo_box_state_changed (GtkWidget *widget,
1312 GtkStateType previous)
1314 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1315 GtkComboBoxPrivate *priv = combo_box->priv;
1317 if (gtk_widget_get_realized (widget))
1319 if (priv->tree_view && priv->cell_view)
1321 GtkStyleContext *context;
1322 GtkStateFlags state;
1325 context = gtk_widget_get_style_context (widget);
1326 state = gtk_widget_get_state_flags (widget);
1328 gtk_style_context_get (context, state,
1329 "background-color", &color,
1332 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
1333 gdk_rgba_free (color);
1337 gtk_widget_queue_draw (widget);
1341 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1342 GtkStateFlags previous,
1345 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1346 GtkComboBoxPrivate *priv = combo_box->priv;
1348 if (gtk_widget_get_realized (widget))
1350 if (!priv->tree_view && priv->cell_view)
1351 gtk_widget_set_state_flags (priv->cell_view,
1352 gtk_widget_get_state_flags (widget),
1356 gtk_widget_queue_draw (widget);
1360 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1362 GtkComboBoxPrivate *priv = combo_box->priv;
1363 gboolean appears_as_list;
1365 /* if wrap_width > 0, then we are in grid-mode and forced to use
1368 if (priv->wrap_width)
1369 appears_as_list = FALSE;
1371 gtk_widget_style_get (GTK_WIDGET (combo_box),
1372 "appears-as-list", &appears_as_list,
1375 if (appears_as_list)
1377 /* Destroy all the menu mode widgets, if they exist. */
1378 if (GTK_IS_MENU (priv->popup_widget))
1379 gtk_combo_box_menu_destroy (combo_box);
1381 /* Create the list mode widgets, if they don't already exist. */
1382 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1383 gtk_combo_box_list_setup (combo_box);
1387 /* Destroy all the list mode widgets, if they exist. */
1388 if (GTK_IS_TREE_VIEW (priv->tree_view))
1389 gtk_combo_box_list_destroy (combo_box);
1391 /* Create the menu mode widgets, if they don't already exist. */
1392 if (!GTK_IS_MENU (priv->popup_widget))
1393 gtk_combo_box_menu_setup (combo_box, TRUE);
1396 gtk_widget_style_get (GTK_WIDGET (combo_box),
1397 "shadow-type", &priv->shadow_type,
1402 gtk_combo_box_style_updated (GtkWidget *widget)
1404 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1405 GtkComboBoxPrivate *priv = combo_box->priv;
1408 gtk_combo_box_check_appearance (combo_box);
1410 if (priv->tree_view && priv->cell_view)
1412 GtkStyleContext *context;
1415 context = gtk_widget_get_style_context (widget);
1416 gtk_style_context_get (context, 0,
1417 "background-color", &color,
1420 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view),
1423 gdk_rgba_free (color);
1426 child = gtk_bin_get_child (GTK_BIN (combo_box));
1427 if (GTK_IS_ENTRY (child))
1428 g_object_set (child, "shadow-type",
1429 GTK_SHADOW_NONE == priv->shadow_type ?
1430 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1434 gtk_combo_box_button_toggled (GtkWidget *widget,
1437 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1439 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1441 if (!combo_box->priv->popup_in_progress)
1442 gtk_combo_box_popup (combo_box);
1445 gtk_combo_box_popdown (combo_box);
1449 gtk_combo_box_add (GtkContainer *container,
1452 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1453 GtkComboBoxPrivate *priv = combo_box->priv;
1455 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1457 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1458 "(need an instance of GtkEntry or of a subclass)",
1459 G_OBJECT_TYPE_NAME (widget));
1463 if (priv->cell_view &&
1464 gtk_widget_get_parent (priv->cell_view))
1466 gtk_widget_unparent (priv->cell_view);
1467 _gtk_bin_set_child (GTK_BIN (container), NULL);
1468 gtk_widget_queue_resize (GTK_WIDGET (container));
1471 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1472 _gtk_bin_set_child (GTK_BIN (container), widget);
1474 if (priv->cell_view &&
1475 widget != priv->cell_view)
1477 /* since the cell_view was unparented, it's gone now */
1478 priv->cell_view = NULL;
1480 if (!priv->tree_view && priv->separator)
1482 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1484 priv->separator = NULL;
1486 gtk_widget_queue_resize (GTK_WIDGET (container));
1488 else if (priv->cell_view_frame)
1490 gtk_widget_unparent (priv->cell_view_frame);
1491 priv->cell_view_frame = NULL;
1496 if (priv->has_entry)
1498 g_signal_connect (widget, "changed",
1499 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1502 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1507 gtk_combo_box_remove (GtkContainer *container,
1510 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1511 GtkComboBoxPrivate *priv = combo_box->priv;
1513 gboolean appears_as_list;
1515 if (priv->has_entry)
1517 GtkWidget *child_widget;
1519 child_widget = gtk_bin_get_child (GTK_BIN (container));
1520 if (widget && widget == child_widget)
1522 g_signal_handlers_disconnect_by_func (widget,
1523 gtk_combo_box_entry_contents_changed,
1528 if (widget == priv->cell_view)
1529 priv->cell_view = NULL;
1531 gtk_widget_unparent (widget);
1532 _gtk_bin_set_child (GTK_BIN (container), NULL);
1534 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1537 gtk_widget_queue_resize (GTK_WIDGET (container));
1539 if (!priv->tree_view)
1540 appears_as_list = FALSE;
1542 appears_as_list = TRUE;
1544 if (appears_as_list)
1545 gtk_combo_box_list_destroy (combo_box);
1546 else if (GTK_IS_MENU (priv->popup_widget))
1548 gtk_combo_box_menu_destroy (combo_box);
1549 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1550 priv->popup_widget = NULL;
1553 if (!priv->cell_view)
1555 priv->cell_view = gtk_cell_view_new ();
1556 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1557 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1559 gtk_widget_show (priv->cell_view);
1560 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1562 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1566 if (appears_as_list)
1567 gtk_combo_box_list_setup (combo_box);
1569 gtk_combo_box_menu_setup (combo_box, TRUE);
1571 if (gtk_tree_row_reference_valid (priv->active_row))
1573 path = gtk_tree_row_reference_get_path (priv->active_row);
1574 gtk_combo_box_set_active_internal (combo_box, path);
1575 gtk_tree_path_free (path);
1578 gtk_combo_box_set_active_internal (combo_box, NULL);
1581 static ComboCellInfo *
1582 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1583 GtkCellRenderer *cell)
1587 for (i = combo_box->priv->cells; i; i = i->next)
1589 ComboCellInfo *info = (ComboCellInfo *)i->data;
1591 if (info && info->cell == cell)
1599 gtk_combo_box_menu_show (GtkWidget *menu,
1602 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1603 GtkComboBoxPrivate *priv = combo_box->priv;
1605 gtk_combo_box_child_show (menu, user_data);
1607 priv->popup_in_progress = TRUE;
1608 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1610 priv->popup_in_progress = FALSE;
1614 gtk_combo_box_menu_hide (GtkWidget *menu,
1617 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1619 gtk_combo_box_child_hide (menu,user_data);
1621 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1626 gtk_combo_box_detacher (GtkWidget *widget,
1629 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1630 GtkComboBoxPrivate *priv = combo_box->priv;
1632 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1634 g_signal_handlers_disconnect_by_func (menu->toplevel,
1635 gtk_combo_box_menu_show,
1637 g_signal_handlers_disconnect_by_func (menu->toplevel,
1638 gtk_combo_box_menu_hide,
1641 priv->popup_widget = NULL;
1645 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1648 GtkComboBoxPrivate *priv = combo_box->priv;
1650 if (GTK_IS_MENU (priv->popup_widget))
1652 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1653 priv->popup_widget = NULL;
1655 else if (priv->popup_widget)
1657 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1658 priv->popup_widget);
1659 g_object_unref (priv->popup_widget);
1660 priv->popup_widget = NULL;
1663 if (GTK_IS_MENU (popup))
1665 if (priv->popup_window)
1667 gtk_widget_destroy (priv->popup_window);
1668 priv->popup_window = NULL;
1671 priv->popup_widget = popup;
1674 * Note that we connect to show/hide on the toplevel, not the
1675 * menu itself, since the menu is not shown/hidden when it is
1676 * popped up while torn-off.
1678 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1679 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1680 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1681 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1683 gtk_menu_attach_to_widget (GTK_MENU (popup),
1684 GTK_WIDGET (combo_box),
1685 gtk_combo_box_detacher);
1689 if (!priv->popup_window)
1691 GtkWidget *toplevel;
1693 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1694 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1696 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1697 GDK_WINDOW_TYPE_HINT_COMBO);
1699 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1700 G_CALLBACK (gtk_combo_box_child_show),
1702 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1703 G_CALLBACK (gtk_combo_box_child_hide),
1706 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1707 if (GTK_IS_WINDOW (toplevel))
1709 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1710 GTK_WINDOW (priv->popup_window));
1711 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1712 GTK_WINDOW (toplevel));
1715 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1716 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1717 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1719 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1721 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1724 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1727 gtk_widget_show (priv->scrolled_window);
1729 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1730 priv->scrolled_window);
1733 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1736 gtk_widget_show (popup);
1737 g_object_ref (popup);
1738 priv->popup_widget = popup;
1743 get_widget_border (GtkWidget *widget,
1746 GtkStyleContext *context;
1747 GtkBorder *border_width;
1749 context = gtk_widget_get_style_context (widget);
1751 gtk_style_context_get (context,
1752 gtk_widget_get_state_flags (widget),
1753 "border-width", &border_width,
1756 *border = *border_width;
1757 gtk_border_free (border_width);
1761 gtk_combo_box_menu_position_below (GtkMenu *menu,
1767 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1768 GtkAllocation child_allocation;
1774 GdkRectangle monitor;
1777 /* FIXME: is using the size request here broken? */
1778 child = gtk_bin_get_child (GTK_BIN (combo_box));
1782 gtk_widget_get_allocation (child, &child_allocation);
1784 if (!gtk_widget_get_has_window (child))
1786 sx += child_allocation.x;
1787 sy += child_allocation.y;
1790 gdk_window_get_root_coords (gtk_widget_get_window (child),
1792 get_widget_border (GTK_WIDGET (combo_box), &border);
1795 if (combo_box->priv->popup_fixed_width)
1796 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1798 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1800 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1803 *x = sx + child_allocation.width - req.width;
1806 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1807 monitor_num = gdk_screen_get_monitor_at_window (screen,
1808 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1809 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1813 else if (*x + req.width > monitor.x + monitor.width)
1814 *x = monitor.x + monitor.width - req.width;
1816 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1817 *y += child_allocation.height;
1818 else if (*y - monitor.y >= req.height)
1820 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1821 *y += child_allocation.height;
1829 gtk_combo_box_menu_position_over (GtkMenu *menu,
1835 GtkComboBox *combo_box;
1839 GtkAllocation allocation;
1840 GtkAllocation child_allocation;
1847 combo_box = GTK_COMBO_BOX (user_data);
1848 widget = GTK_WIDGET (combo_box);
1850 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1852 gtk_widget_get_allocation (widget, &allocation);
1854 menu_xpos = allocation.x;
1855 menu_ypos = allocation.y + allocation.height / 2 - 2;
1857 if (combo_box->priv->popup_fixed_width)
1858 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1860 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1864 gtk_widget_get_allocation (active, &child_allocation);
1865 menu_ypos -= child_allocation.height / 2;
1868 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1871 child = children->data;
1873 if (active == child)
1876 if (gtk_widget_get_visible (child))
1878 gtk_widget_get_allocation (child, &child_allocation);
1880 menu_ypos -= child_allocation.height;
1883 children = children->next;
1886 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1887 menu_xpos = menu_xpos + allocation.width - menu_width;
1889 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1890 menu_xpos, menu_ypos,
1891 &menu_xpos, &menu_ypos);
1893 /* Clamp the position on screen */
1894 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1898 else if ((menu_xpos + menu_width) > screen_width)
1899 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1908 gtk_combo_box_menu_position (GtkMenu *menu,
1914 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1915 GtkComboBoxPrivate *priv = combo_box->priv;
1916 GtkWidget *menu_item;
1918 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1919 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1922 /* FIXME handle nested menus better */
1923 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1925 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1928 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1931 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1932 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1933 GDK_WINDOW_TYPE_HINT_COMBO);
1937 gtk_combo_box_list_position (GtkComboBox *combo_box,
1943 GtkComboBoxPrivate *priv = combo_box->priv;
1944 GtkAllocation allocation;
1947 GdkRectangle monitor;
1948 GtkRequisition popup_req;
1949 GtkPolicyType hpolicy, vpolicy;
1952 /* under windows, the drop down list is as wide as the combo box itself.
1954 GtkWidget *widget = GTK_WIDGET (combo_box);
1958 gtk_widget_get_allocation (widget, &allocation);
1960 if (!gtk_widget_get_has_window (widget))
1966 window = gtk_widget_get_window (widget);
1968 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1971 *width = allocation.width;
1973 hpolicy = vpolicy = GTK_POLICY_NEVER;
1974 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1977 /* XXX This set_size_request call is part of the hack outlined below and can
1978 * go away once height-for-width is implemented on treeviews. */
1979 gtk_widget_set_size_request (priv->tree_view, -1, -1);
1981 if (combo_box->priv->popup_fixed_width)
1983 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1985 if (popup_req.width > *width)
1987 hpolicy = GTK_POLICY_ALWAYS;
1988 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1994 gtk_combo_box_remeasure (combo_box);
1996 if (priv->natural_width > *width)
1998 hpolicy = GTK_POLICY_NEVER;
1999 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2003 /* XXX Currently we set the size-request on the internal treeview to be
2004 * the natural width of the cells, this hack can go away once our
2005 * treeview does height-for-width properly (i.e. just adjust *width
2006 * here to be the natural width request of the scrolled-window).
2008 * I can't tell why the magic number 5 is needed here (i.e. without it
2009 * treeviews are left ellipsizing) , however it this all should be
2010 * removed with height-for-width treeviews.
2012 gtk_widget_set_size_request (priv->tree_view, priv->natural_width + 5, -1);
2013 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2015 *width = popup_req.width;
2019 *height = popup_req.height;
2021 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2022 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2023 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2025 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2026 *x = *x + allocation.width - *width;
2030 else if (*x + *width > monitor.x + monitor.width)
2031 *x = monitor.x + monitor.width - *width;
2033 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2034 *y += allocation.height;
2035 else if (*y - *height >= monitor.y)
2037 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2039 *y += allocation.height;
2040 *height = monitor.y + monitor.height - *y;
2044 *height = *y - monitor.y;
2048 if (popup_req.height > *height)
2050 vpolicy = GTK_POLICY_ALWAYS;
2052 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2058 cell_view_is_sensitive (GtkCellView *cell_view)
2060 GList *cells, *list;
2063 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
2066 for (list = cells; list; list = list->next)
2068 g_object_get (list->data, "sensitive", &sensitive, NULL);
2073 g_list_free (cells);
2079 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2082 GtkComboBoxPrivate *priv = combo_box->priv;
2083 GList *cells, *list;
2089 if (priv->row_separator_func)
2091 if (priv->row_separator_func (priv->model, iter,
2092 priv->row_separator_data))
2096 gtk_tree_view_column_cell_set_cell_data (priv->column,
2098 iter, FALSE, FALSE);
2100 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
2103 for (list = cells; list; list = list->next)
2105 g_object_get (list->data, "sensitive", &sensitive, NULL);
2110 g_list_free (cells);
2116 update_menu_sensitivity (GtkComboBox *combo_box,
2119 GtkComboBoxPrivate *priv = combo_box->priv;
2120 GList *children, *child;
2121 GtkWidget *item, *submenu, *separator;
2122 GtkWidget *cell_view;
2128 children = gtk_container_get_children (GTK_CONTAINER (menu));
2130 for (child = children; child; child = child->next)
2132 item = GTK_WIDGET (child->data);
2133 cell_view = gtk_bin_get_child (GTK_BIN (item));
2135 if (!GTK_IS_CELL_VIEW (cell_view))
2138 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2139 if (submenu != NULL)
2141 gtk_widget_set_sensitive (item, TRUE);
2142 update_menu_sensitivity (combo_box, submenu);
2146 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
2148 if (menu != priv->popup_widget && child == children)
2150 separator = GTK_WIDGET (child->next->data);
2151 g_object_set (item, "visible", sensitive, NULL);
2152 g_object_set (separator, "visible", sensitive, NULL);
2155 gtk_widget_set_sensitive (item, sensitive);
2159 g_list_free (children);
2163 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2165 guint32 activate_time)
2167 GtkComboBoxPrivate *priv = combo_box->priv;
2170 gint width, min_width, nat_width;
2172 update_menu_sensitivity (combo_box, priv->popup_widget);
2175 if (gtk_tree_row_reference_valid (priv->active_row))
2177 path = gtk_tree_row_reference_get_path (priv->active_row);
2178 active_item = gtk_tree_path_get_indices (path)[0];
2179 gtk_tree_path_free (path);
2181 if (priv->add_tearoffs)
2185 /* FIXME handle nested menus better */
2186 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2188 if (priv->wrap_width == 0)
2190 GtkAllocation allocation;
2192 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2193 width = allocation.width;
2194 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2195 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2197 if (combo_box->priv->popup_fixed_width)
2198 width = MAX (width, min_width);
2200 width = MAX (width, nat_width);
2202 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2205 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2207 gtk_combo_box_menu_position, combo_box,
2208 button, activate_time);
2212 popup_grab_on_window (GdkWindow *window,
2213 GdkDevice *keyboard,
2215 guint32 activate_time)
2218 gdk_device_grab (keyboard, window,
2219 GDK_OWNERSHIP_WINDOW, TRUE,
2220 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2221 NULL, activate_time) != GDK_GRAB_SUCCESS)
2225 gdk_device_grab (pointer, window,
2226 GDK_OWNERSHIP_WINDOW, TRUE,
2227 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2228 GDK_POINTER_MOTION_MASK,
2229 NULL, activate_time) != GDK_GRAB_SUCCESS)
2232 gdk_device_ungrab (keyboard, activate_time);
2241 * gtk_combo_box_popup:
2242 * @combo_box: a #GtkComboBox
2244 * Pops up the menu or dropdown list of @combo_box.
2246 * This function is mostly intended for use by accessibility technologies;
2247 * applications should have little use for it.
2252 gtk_combo_box_popup (GtkComboBox *combo_box)
2254 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2256 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2260 * gtk_combo_box_popup_for_device:
2261 * @combo_box: a #GtkComboBox
2262 * @device: a #GdkDevice
2264 * Pops up the menu or dropdown list of @combo_box, the popup window
2265 * will be grabbed so only @device and its associated pointer/keyboard
2266 * are the only #GdkDevice<!-- -->s able to send events to it.
2271 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2274 GtkComboBoxPrivate *priv = combo_box->priv;
2275 gint x, y, width, height;
2276 GtkTreePath *path = NULL, *ppath;
2277 GtkWidget *toplevel;
2278 GdkDevice *keyboard, *pointer;
2281 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2282 g_return_if_fail (GDK_IS_DEVICE (device));
2284 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2287 if (gtk_widget_get_mapped (priv->popup_widget))
2290 if (priv->grab_pointer && priv->grab_keyboard)
2293 time = gtk_get_current_event_time ();
2295 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2298 pointer = gdk_device_get_associated_device (device);
2303 keyboard = gdk_device_get_associated_device (device);
2306 if (GTK_IS_MENU (priv->popup_widget))
2308 gtk_combo_box_menu_popup (combo_box,
2309 priv->activate_button,
2310 priv->activate_time);
2314 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2315 if (GTK_IS_WINDOW (toplevel))
2316 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2317 GTK_WINDOW (priv->popup_window));
2319 gtk_widget_show_all (priv->scrolled_window);
2320 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2322 gtk_widget_set_size_request (priv->popup_window, width, height);
2323 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2325 if (gtk_tree_row_reference_valid (priv->active_row))
2327 path = gtk_tree_row_reference_get_path (priv->active_row);
2328 ppath = gtk_tree_path_copy (path);
2329 if (gtk_tree_path_up (ppath))
2330 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2332 gtk_tree_path_free (ppath);
2334 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2338 gtk_widget_show (priv->popup_window);
2342 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2344 gtk_tree_path_free (path);
2347 gtk_widget_grab_focus (priv->popup_window);
2348 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2351 if (!gtk_widget_has_focus (priv->tree_view))
2352 gtk_widget_grab_focus (priv->tree_view);
2354 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2355 keyboard, pointer, time))
2357 gtk_widget_hide (priv->popup_window);
2361 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2362 priv->grab_pointer = pointer;
2363 priv->grab_keyboard = keyboard;
2367 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2371 device = gtk_get_current_event_device ();
2375 GdkDeviceManager *device_manager;
2376 GdkDisplay *display;
2379 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2380 device_manager = gdk_display_get_device_manager (display);
2382 /* No device was set, pick the first master device */
2383 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2384 device = devices->data;
2385 g_list_free (devices);
2388 gtk_combo_box_popup_for_device (combo_box, device);
2392 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2394 if (combo_box->priv->popup_shown)
2396 gtk_combo_box_popdown (combo_box);
2404 * gtk_combo_box_popdown:
2405 * @combo_box: a #GtkComboBox
2407 * Hides the menu or dropdown list of @combo_box.
2409 * This function is mostly intended for use by accessibility technologies;
2410 * applications should have little use for it.
2415 gtk_combo_box_popdown (GtkComboBox *combo_box)
2417 GtkComboBoxPrivate *priv = combo_box->priv;
2419 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2421 if (GTK_IS_MENU (priv->popup_widget))
2423 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2427 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2430 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2431 gtk_widget_hide (priv->popup_window);
2432 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2435 priv->grab_pointer = NULL;
2436 priv->grab_keyboard = NULL;
2440 gtk_combo_box_update_requested_width (GtkComboBox *combo_box,
2443 GtkComboBoxPrivate *priv = combo_box->priv;
2444 gint padding, min_width, nat_width;
2446 if (priv->cell_view)
2447 gtk_widget_style_get (priv->cell_view,
2448 "focus-line-width", &padding,
2453 /* add some pixels for good measure */
2454 padding += BONUS_PADDING;
2456 if (priv->cell_view)
2457 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
2458 path, &min_width, &nat_width);
2460 min_width = nat_width = 0;
2462 min_width += padding;
2463 nat_width += padding;
2465 if (min_width > priv->minimum_width || nat_width > priv->natural_width)
2467 priv->minimum_width = MAX (priv->minimum_width, min_width);
2468 priv->natural_width = MAX (priv->natural_width, nat_width);
2470 if (priv->cell_view)
2472 gtk_widget_set_size_request (priv->cell_view, min_width, -1);
2473 gtk_widget_queue_resize (priv->cell_view);
2478 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2479 gtk_widget_get_preferred_size (combo_box->priv->button, \
2483 child.x = allocation->x + border.right; \
2485 child.x = allocation->x + allocation->width - req.width - border.left; \
2487 child.y = allocation->y + border.top; \
2488 child.width = req.width; \
2489 child.height = allocation->height - (border.top + border.bottom); \
2490 child.width = MAX (1, child.width); \
2491 child.height = MAX (1, child.height); \
2493 gtk_widget_size_allocate (combo_box->priv->button, &child);
2496 gtk_combo_box_size_allocate (GtkWidget *widget,
2497 GtkAllocation *allocation)
2499 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2500 GtkComboBoxPrivate *priv = combo_box->priv;
2501 GtkWidget *child_widget;
2502 gint focus_width, focus_pad;
2503 GtkAllocation child;
2506 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2508 gtk_widget_set_allocation (widget, allocation);
2509 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2510 get_widget_border (widget, &border);
2512 gtk_widget_style_get (widget,
2513 "focus-line-width", &focus_width,
2514 "focus-padding", &focus_pad,
2517 if (!priv->tree_view)
2519 if (priv->cell_view)
2521 GtkBorder button_border;
2526 allocation->x += border.left;
2527 allocation->y += border.top;
2528 allocation->width -= border.left + border.right;
2529 allocation->height -= border.top + border.bottom;
2531 gtk_widget_size_allocate (priv->button, allocation);
2533 /* set some things ready */
2534 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2535 get_widget_border (priv->button, &button_border);
2537 child.x = allocation->x;
2538 child.y = allocation->y;
2539 width = allocation->width;
2540 child.height = allocation->height;
2542 if (!priv->is_cell_renderer)
2544 child.x += border_width + button_border.left + focus_width + focus_pad;
2545 child.y += border_width + button_border.top + focus_width + focus_pad;
2546 width -= (2 * (border_width + focus_width + focus_pad)) +
2547 button_border.left + button_border.right;
2548 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2549 button_border.top + button_border.bottom;
2553 /* handle the children */
2554 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2555 child.width = req.width;
2557 child.x += width - req.width;
2558 child.width = MAX (1, child.width);
2559 child.height = MAX (1, child.height);
2560 gtk_widget_size_allocate (priv->arrow, &child);
2562 child.x += req.width;
2563 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2564 child.width = req.width;
2566 child.x -= req.width;
2567 child.width = MAX (1, child.width);
2568 child.height = MAX (1, child.height);
2569 gtk_widget_size_allocate (priv->separator, &child);
2573 child.x += req.width;
2574 child.width = allocation->x + allocation->width
2575 - (border_width + button_border.right + focus_width + focus_pad)
2580 child.width = child.x;
2581 child.x = allocation->x
2582 + border_width + button_border.left + focus_width + focus_pad;
2583 child.width -= child.x;
2586 if (gtk_widget_get_visible (priv->popup_widget))
2588 gint width, menu_width;
2590 if (priv->wrap_width == 0)
2592 GtkAllocation combo_box_allocation;
2594 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2595 width = combo_box_allocation.width;
2596 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2598 if (combo_box->priv->popup_fixed_width)
2599 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2601 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2603 gtk_widget_set_size_request (priv->popup_widget,
2604 MAX (width, menu_width), -1);
2607 /* reposition the menu after giving it a new width */
2608 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2611 child.width = MAX (1, child.width);
2612 child.height = MAX (1, child.height);
2613 gtk_widget_size_allocate (child_widget, &child);
2617 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2620 child.x = allocation->x + req.width + border.right;
2622 child.x = allocation->x + border.left;
2623 child.y = allocation->y + border.top;
2624 child.width = allocation->width - req.width - (border.left + border.right);
2625 child.width = MAX (1, child.width);
2626 child.height = MAX (1, child.height);
2627 gtk_widget_size_allocate (child_widget, &child);
2633 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2636 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2640 child.x = allocation->x + req.width;
2642 child.x = allocation->x;
2644 child.y = allocation->y;
2645 child.width = allocation->width - req.width;
2646 child.height = allocation->height;
2648 if (priv->cell_view_frame)
2650 child.x += border.left + border_width;
2651 child.y += border.top + border_width;
2652 child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right));
2653 child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom));
2654 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2657 if (priv->has_frame)
2659 GtkBorder frame_border;
2661 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2662 get_widget_border (priv->cell_view_frame, &frame_border);
2664 child.x += border_width + frame_border.left;
2665 child.y += border_width + frame_border.right;
2666 child.width -= (2 * border_width) + frame_border.left + frame_border.right;
2667 child.height -= (2 * border_width) + frame_border.top + frame_border.bottom;
2672 child.x += border.left + border_width;
2673 child.y += border.top + border_width;
2674 child.width -= (2 * border_width) - (border.left + border.right);
2675 child.height -= (2 * border_width) - (border.top + border.bottom);
2678 if (gtk_widget_get_visible (priv->popup_window))
2680 gint x, y, width, height;
2681 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2682 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2683 gtk_widget_set_size_request (priv->popup_window, width, height);
2687 child.width = MAX (1, child.width);
2688 child.height = MAX (1, child.height);
2690 gtk_widget_size_allocate (child_widget, &child);
2694 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2697 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2699 GtkComboBoxPrivate *priv = combo_box->priv;
2703 g_signal_handler_disconnect (priv->model,
2705 g_signal_handler_disconnect (priv->model,
2707 g_signal_handler_disconnect (priv->model,
2708 priv->reordered_id);
2709 g_signal_handler_disconnect (priv->model,
2714 if (!priv->tree_view)
2716 if (priv->popup_widget)
2717 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2718 (GtkCallback)gtk_widget_destroy, NULL);
2723 g_object_unref (priv->model);
2727 if (priv->active_row)
2729 gtk_tree_row_reference_free (priv->active_row);
2730 priv->active_row = NULL;
2733 if (priv->cell_view)
2734 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2738 gtk_combo_box_forall (GtkContainer *container,
2739 gboolean include_internals,
2740 GtkCallback callback,
2741 gpointer callback_data)
2743 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2744 GtkComboBoxPrivate *priv = combo_box->priv;
2747 if (include_internals)
2750 (* callback) (priv->button, callback_data);
2751 if (priv->cell_view_frame)
2752 (* callback) (priv->cell_view_frame, callback_data);
2755 child = gtk_bin_get_child (GTK_BIN (container));
2757 (* callback) (child, callback_data);
2761 gtk_combo_box_child_show (GtkWidget *widget,
2762 GtkComboBox *combo_box)
2764 GtkComboBoxPrivate *priv = combo_box->priv;
2766 priv->popup_shown = TRUE;
2767 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2771 gtk_combo_box_child_hide (GtkWidget *widget,
2772 GtkComboBox *combo_box)
2774 GtkComboBoxPrivate *priv = combo_box->priv;
2776 priv->popup_shown = FALSE;
2777 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2781 gtk_combo_box_draw (GtkWidget *widget,
2784 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2785 GtkComboBoxPrivate *priv = combo_box->priv;
2787 if (priv->shadow_type != GTK_SHADOW_NONE)
2789 GtkStyleContext *context;
2790 GtkStateFlags state;
2792 context = gtk_widget_get_style_context (widget);
2793 state = gtk_widget_get_state_flags (widget);
2794 gtk_style_context_set_state (context, state);
2796 gtk_render_background (context, cr, 0, 0,
2797 gtk_widget_get_allocated_width (widget),
2798 gtk_widget_get_allocated_height (widget));
2799 gtk_render_frame (context, cr, 0, 0,
2800 gtk_widget_get_allocated_width (widget),
2801 gtk_widget_get_allocated_height (widget));
2804 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2807 if (priv->tree_view && priv->cell_view_frame)
2809 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2810 priv->cell_view_frame, cr);
2813 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2814 gtk_bin_get_child (GTK_BIN (widget)),
2830 path_visible (GtkTreeView *view,
2836 /* Note that we rely on the fact that collapsed rows don't have nodes
2838 return _gtk_tree_view_find_node (view, path, &tree, &node);
2842 tree_next_func (GtkTreeModel *model,
2847 SearchData *search_data = (SearchData *)data;
2849 if (search_data->found)
2851 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2854 if (search_data->visible &&
2855 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2858 search_data->set = TRUE;
2859 search_data->iter = *iter;
2864 if (gtk_tree_path_compare (path, search_data->path) == 0)
2865 search_data->found = TRUE;
2871 tree_next (GtkComboBox *combo,
2872 GtkTreeModel *model,
2877 SearchData search_data;
2879 search_data.combo = combo;
2880 search_data.path = gtk_tree_model_get_path (model, iter);
2881 search_data.visible = visible;
2882 search_data.found = FALSE;
2883 search_data.set = FALSE;
2885 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2887 *next = search_data.iter;
2889 gtk_tree_path_free (search_data.path);
2891 return search_data.set;
2895 tree_prev_func (GtkTreeModel *model,
2900 SearchData *search_data = (SearchData *)data;
2902 if (gtk_tree_path_compare (path, search_data->path) == 0)
2904 search_data->found = TRUE;
2908 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2911 if (search_data->visible &&
2912 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2915 search_data->set = TRUE;
2916 search_data->iter = *iter;
2922 tree_prev (GtkComboBox *combo,
2923 GtkTreeModel *model,
2928 SearchData search_data;
2930 search_data.combo = combo;
2931 search_data.path = gtk_tree_model_get_path (model, iter);
2932 search_data.visible = visible;
2933 search_data.found = FALSE;
2934 search_data.set = FALSE;
2936 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2938 *prev = search_data.iter;
2940 gtk_tree_path_free (search_data.path);
2942 return search_data.set;
2946 tree_last_func (GtkTreeModel *model,
2951 SearchData *search_data = (SearchData *)data;
2953 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2956 /* Note that we rely on the fact that collapsed rows don't have nodes
2958 if (search_data->visible &&
2959 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2962 search_data->set = TRUE;
2963 search_data->iter = *iter;
2969 tree_last (GtkComboBox *combo,
2970 GtkTreeModel *model,
2974 SearchData search_data;
2976 search_data.combo = combo;
2977 search_data.visible = visible;
2978 search_data.set = FALSE;
2980 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2982 *last = search_data.iter;
2984 return search_data.set;
2989 tree_first_func (GtkTreeModel *model,
2994 SearchData *search_data = (SearchData *)data;
2996 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2999 if (search_data->visible &&
3000 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3003 search_data->set = TRUE;
3004 search_data->iter = *iter;
3010 tree_first (GtkComboBox *combo,
3011 GtkTreeModel *model,
3015 SearchData search_data;
3017 search_data.combo = combo;
3018 search_data.visible = visible;
3019 search_data.set = FALSE;
3021 gtk_tree_model_foreach (model, tree_first_func, &search_data);
3023 *first = search_data.iter;
3025 return search_data.set;
3029 gtk_combo_box_scroll_event (GtkWidget *widget,
3030 GdkEventScroll *event)
3032 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3035 GtkTreeIter new_iter;
3037 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3040 if (event->direction == GDK_SCROLL_UP)
3041 found = tree_prev (combo_box, combo_box->priv->model,
3042 &iter, &new_iter, FALSE);
3044 found = tree_next (combo_box, combo_box->priv->model,
3045 &iter, &new_iter, FALSE);
3048 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3058 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
3059 GtkCellLayout *cell_layout)
3061 GtkComboBoxPrivate *priv = combo_box->priv;
3064 for (k = priv->cells; k; k = k->next)
3067 ComboCellInfo *info = (ComboCellInfo *)k->data;
3069 if (info->pack == GTK_PACK_START)
3070 gtk_cell_layout_pack_start (cell_layout,
3071 info->cell, info->expand);
3072 else if (info->pack == GTK_PACK_END)
3073 gtk_cell_layout_pack_end (cell_layout,
3074 info->cell, info->expand);
3076 gtk_cell_layout_set_cell_data_func (cell_layout,
3078 combo_cell_data_func, info, NULL);
3080 for (j = info->attributes; j; j = j->next->next)
3082 gtk_cell_layout_add_attribute (cell_layout,
3085 GPOINTER_TO_INT (j->next->data));
3091 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3092 gboolean add_children)
3094 GtkComboBoxPrivate *priv = combo_box->priv;
3098 child = gtk_bin_get_child (GTK_BIN (combo_box));
3100 if (priv->cell_view)
3102 priv->button = gtk_toggle_button_new ();
3103 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3104 priv->focus_on_click);
3106 g_signal_connect (priv->button, "toggled",
3107 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3108 gtk_widget_set_parent (priv->button,
3109 gtk_widget_get_parent (child));
3111 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3112 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3114 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3115 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3117 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3118 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3120 gtk_widget_show_all (priv->button);
3124 priv->button = gtk_toggle_button_new ();
3125 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3126 priv->focus_on_click);
3128 g_signal_connect (priv->button, "toggled",
3129 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3130 gtk_widget_set_parent (priv->button,
3131 gtk_widget_get_parent (child));
3133 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3134 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3135 gtk_widget_show_all (priv->button);
3138 g_signal_connect (priv->button, "button-press-event",
3139 G_CALLBACK (gtk_combo_box_menu_button_press),
3141 g_signal_connect (priv->button, "state-flags-changed",
3142 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3145 /* create our funky menu */
3146 menu = gtk_menu_new ();
3147 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3148 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3150 g_signal_connect (menu, "key-press-event",
3151 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3152 gtk_combo_box_set_popup_widget (combo_box, menu);
3156 gtk_combo_box_menu_fill (combo_box);
3158 /* the column is needed in tree_column_row_is_sensitive() */
3159 priv->column = gtk_tree_view_column_new ();
3160 g_object_ref_sink (priv->column);
3161 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
3163 gtk_combo_box_update_title (combo_box);
3164 gtk_combo_box_update_sensitivity (combo_box);
3168 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
3170 GtkComboBoxPrivate *priv = combo_box->priv;
3176 menu = priv->popup_widget;
3178 if (priv->add_tearoffs)
3180 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
3182 gtk_widget_show (tearoff);
3184 if (priv->wrap_width)
3185 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
3187 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
3190 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
3194 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
3195 GtkTreeModel *model,
3198 GtkWidget *cell_view;
3203 cell_view = gtk_cell_view_new ();
3204 item = gtk_menu_item_new ();
3205 gtk_container_add (GTK_CONTAINER (item), cell_view);
3207 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
3208 path = gtk_tree_model_get_path (model, iter);
3209 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
3210 gtk_tree_path_free (path);
3212 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
3213 gtk_widget_get_preferred_size (cell_view, &req, NULL);
3214 gtk_widget_show (cell_view);
3220 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
3222 GtkTreeIter *parent)
3224 GtkComboBoxPrivate *priv = combo_box->priv;
3225 GtkTreeModel *model = priv->model;
3226 GtkWidget *item, *submenu, *subitem, *separator;
3228 gboolean is_separator;
3233 n_children = gtk_tree_model_iter_n_children (model, parent);
3236 for (i = 0; i < n_children; i++)
3238 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
3240 if (priv->row_separator_func)
3241 is_separator = priv->row_separator_func (priv->model, &iter,
3242 priv->row_separator_data);
3244 is_separator = FALSE;
3248 item = gtk_separator_menu_item_new ();
3249 path = gtk_tree_model_get_path (model, &iter);
3250 g_object_set_data_full (G_OBJECT (item),
3251 I_("gtk-combo-box-item-path"),
3252 gtk_tree_row_reference_new (model, path),
3253 (GDestroyNotify)gtk_tree_row_reference_free);
3254 gtk_tree_path_free (path);
3258 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3259 if (gtk_tree_model_iter_has_child (model, &iter))
3261 submenu = gtk_menu_new ();
3262 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
3263 gtk_widget_show (submenu);
3264 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
3266 /* Ugly - since menus can only activate leafs, we have to
3267 * duplicate the item inside the submenu.
3269 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3270 separator = gtk_separator_menu_item_new ();
3271 gtk_widget_show (subitem);
3272 gtk_widget_show (separator);
3273 g_signal_connect (subitem, "activate",
3274 G_CALLBACK (gtk_combo_box_menu_item_activate),
3276 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
3277 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
3279 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
3281 g_signal_connect (item, "activate",
3282 G_CALLBACK (gtk_combo_box_menu_item_activate),
3286 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3287 if (priv->wrap_width && menu == priv->popup_widget)
3288 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3289 gtk_widget_show (item);
3296 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3298 GtkComboBoxPrivate *priv = combo_box->priv;
3300 g_signal_handlers_disconnect_matched (priv->button,
3301 G_SIGNAL_MATCH_DATA,
3303 gtk_combo_box_menu_button_press, NULL);
3304 g_signal_handlers_disconnect_matched (priv->button,
3305 G_SIGNAL_MATCH_DATA,
3307 gtk_combo_box_button_state_flags_changed, combo_box);
3309 /* unparent will remove our latest ref */
3310 gtk_widget_unparent (priv->button);
3313 priv->button = NULL;
3315 priv->separator = NULL;
3317 g_object_unref (priv->column);
3318 priv->column = NULL;
3320 /* changing the popup window will unref the menu and the children */
3328 menu_occupied (GtkMenu *menu,
3332 guint bottom_attach)
3336 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3340 gtk_container_child_get (GTK_CONTAINER (menu),
3344 "bottom-attach", &b,
3348 /* look if this item intersects with the given coordinates */
3349 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3357 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3362 GtkComboBoxPrivate *priv = combo_box->priv;
3363 gint current_col = 0, current_row = 0;
3364 gint rows = 1, cols = 1;
3365 GtkWidget *menu = priv->popup_widget;
3367 if (!GTK_IS_MENU_SHELL (menu))
3370 if (priv->col_column == -1 &&
3371 priv->row_column == -1 &&
3374 gtk_container_child_get (GTK_CONTAINER (menu),
3376 "right-attach", ¤t_col,
3377 "top-attach", ¤t_row,
3379 if (current_col + cols > priv->wrap_width)
3387 if (priv->col_column != -1)
3388 gtk_tree_model_get (priv->model, iter,
3389 priv->col_column, &cols,
3391 if (priv->row_column != -1)
3392 gtk_tree_model_get (priv->model, iter,
3393 priv->row_column, &rows,
3398 if (current_col + cols > priv->wrap_width)
3404 if (!menu_occupied (GTK_MENU (menu),
3405 current_col, current_col + cols,
3406 current_row, current_row + rows))
3413 /* set attach props */
3414 gtk_menu_attach (GTK_MENU (menu), item,
3415 current_col, current_col + cols,
3416 current_row, current_row + rows);
3420 gtk_combo_box_relayout (GtkComboBox *combo_box)
3425 menu = combo_box->priv->popup_widget;
3427 /* do nothing unless we are in menu style and realized */
3428 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3431 list = gtk_container_get_children (GTK_CONTAINER (menu));
3433 for (j = g_list_last (list); j; j = j->prev)
3434 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3436 gtk_combo_box_menu_fill (combo_box);
3443 gtk_combo_box_menu_button_press (GtkWidget *widget,
3444 GdkEventButton *event,
3447 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3448 GtkComboBoxPrivate *priv = combo_box->priv;
3450 if (GTK_IS_MENU (priv->popup_widget) &&
3451 event->type == GDK_BUTTON_PRESS && event->button == 1)
3453 if (priv->focus_on_click &&
3454 !gtk_widget_has_focus (priv->button))
3455 gtk_widget_grab_focus (priv->button);
3457 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3466 gtk_combo_box_menu_item_activate (GtkWidget *item,
3469 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3470 GtkWidget *cell_view;
3474 cell_view = gtk_bin_get_child (GTK_BIN (item));
3476 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3478 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3480 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3482 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3483 gtk_combo_box_set_active_iter (combo_box, &iter);
3486 gtk_tree_path_free (path);
3488 g_object_set (combo_box,
3489 "editing-canceled", FALSE,
3494 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3497 gboolean sensitive = TRUE; /* fool code checkers */
3499 if (!combo_box->priv->button)
3502 switch (combo_box->priv->button_sensitivity)
3504 case GTK_SENSITIVITY_ON:
3507 case GTK_SENSITIVITY_OFF:
3510 case GTK_SENSITIVITY_AUTO:
3511 sensitive = combo_box->priv->model &&
3512 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3515 g_assert_not_reached ();
3519 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3521 /* In list-mode, we also need to update sensitivity of the event box */
3522 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3523 && combo_box->priv->cell_view)
3524 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3528 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3533 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3535 if (combo_box->priv->tree_view)
3536 gtk_combo_box_list_popup_resize (combo_box);
3538 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3540 gtk_combo_box_update_sensitivity (combo_box);
3544 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3548 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3549 GtkComboBoxPrivate *priv = combo_box->priv;
3551 if (!gtk_tree_row_reference_valid (priv->active_row))
3553 if (priv->cell_view)
3554 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3555 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3558 if (priv->tree_view)
3559 gtk_combo_box_list_popup_resize (combo_box);
3561 gtk_combo_box_menu_row_deleted (model, path, user_data);
3563 gtk_combo_box_update_sensitivity (combo_box);
3567 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3573 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3575 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3577 if (!combo_box->priv->tree_view)
3578 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3582 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3587 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3588 GtkComboBoxPrivate *priv = combo_box->priv;
3589 GtkTreePath *active_path;
3591 /* FIXME this belongs to GtkCellView */
3592 if (gtk_tree_row_reference_valid (priv->active_row))
3594 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3595 if (gtk_tree_path_compare (path, active_path) == 0 &&
3597 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3598 gtk_tree_path_free (active_path);
3601 if (priv->tree_view)
3602 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3604 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3608 list_popup_resize_idle (gpointer user_data)
3610 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3611 GtkComboBoxPrivate *priv = combo_box->priv;
3612 gint x, y, width, height;
3614 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3616 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3618 gtk_widget_set_size_request (priv->popup_window, width, height);
3619 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3622 priv->resize_idle_id = 0;
3628 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3630 GtkComboBoxPrivate *priv = combo_box->priv;
3632 if (!priv->resize_idle_id)
3633 priv->resize_idle_id =
3634 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3638 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3643 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3645 gtk_combo_box_list_popup_resize (combo_box);
3650 find_menu_by_path (GtkWidget *menu,
3652 gboolean skip_first)
3658 GtkTreeRowReference *mref;
3662 list = gtk_container_get_children (GTK_CONTAINER (menu));
3665 for (i = list; i; i = i->next)
3667 child = gtk_bin_get_child (i->data);
3668 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3670 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3673 else if (!gtk_tree_row_reference_valid (mref))
3676 mpath = gtk_tree_row_reference_get_path (mref);
3678 else if (GTK_IS_CELL_VIEW (child))
3686 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (child));
3691 /* this case is necessary, since the row reference of
3692 * the cell view may already be updated after a deletion
3699 if (gtk_tree_path_compare (mpath, path) == 0)
3701 gtk_tree_path_free (mpath);
3705 if (gtk_tree_path_is_ancestor (mpath, path))
3707 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3708 if (submenu != NULL)
3710 gtk_tree_path_free (mpath);
3711 item = find_menu_by_path (submenu, path, TRUE);
3715 gtk_tree_path_free (mpath);
3725 dump_menu_tree (GtkWidget *menu,
3732 list = gtk_container_get_children (GTK_CONTAINER (menu));
3733 for (i = list; i; i = i->next)
3735 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3737 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3738 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3739 gtk_tree_path_free (path);
3741 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3742 if (submenu != NULL)
3743 dump_menu_tree (submenu, level + 1);
3752 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3757 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3758 GtkComboBoxPrivate *priv = combo_box->priv;
3760 GtkWidget *item, *menu, *separator;
3764 gboolean is_separator;
3766 if (!priv->popup_widget)
3769 depth = gtk_tree_path_get_depth (path);
3770 pos = gtk_tree_path_get_indices (path)[depth - 1];
3773 ppath = gtk_tree_path_copy (path);
3774 gtk_tree_path_up (ppath);
3775 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3776 gtk_tree_path_free (ppath);
3778 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3781 menu = gtk_menu_new ();
3782 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3783 gtk_widget_show (menu);
3784 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3786 /* Ugly - since menus can only activate leaves, we have to
3787 * duplicate the item inside the submenu.
3789 gtk_tree_model_iter_parent (model, &piter, iter);
3790 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3791 separator = gtk_separator_menu_item_new ();
3792 g_signal_connect (item, "activate",
3793 G_CALLBACK (gtk_combo_box_menu_item_activate),
3795 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3796 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3797 if (cell_view_is_sensitive (GTK_CELL_VIEW (gtk_bin_get_child (GTK_BIN (item)))))
3799 gtk_widget_show (item);
3800 gtk_widget_show (separator);
3807 menu = priv->popup_widget;
3808 if (priv->add_tearoffs)
3812 if (priv->row_separator_func)
3813 is_separator = priv->row_separator_func (model, iter,
3814 priv->row_separator_data);
3816 is_separator = FALSE;
3820 item = gtk_separator_menu_item_new ();
3821 g_object_set_data_full (G_OBJECT (item),
3822 I_("gtk-combo-box-item-path"),
3823 gtk_tree_row_reference_new (model, path),
3824 (GDestroyNotify)gtk_tree_row_reference_free);
3828 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3830 g_signal_connect (item, "activate",
3831 G_CALLBACK (gtk_combo_box_menu_item_activate),
3835 gtk_widget_show (item);
3836 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3840 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3844 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3845 GtkComboBoxPrivate *priv = combo_box->priv;
3849 if (!priv->popup_widget)
3852 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3853 menu = gtk_widget_get_parent (item);
3854 gtk_container_remove (GTK_CONTAINER (menu), item);
3856 if (gtk_tree_path_get_depth (path) > 1)
3858 GtkTreePath *parent_path;
3862 parent_path = gtk_tree_path_copy (path);
3863 gtk_tree_path_up (parent_path);
3864 gtk_tree_model_get_iter (model, &iter, parent_path);
3866 if (!gtk_tree_model_iter_has_child (model, &iter))
3868 parent = find_menu_by_path (priv->popup_widget,
3869 parent_path, FALSE);
3870 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3876 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3882 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3884 gtk_combo_box_relayout (combo_box);
3888 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3893 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3894 GtkComboBoxPrivate *priv = combo_box->priv;
3896 gboolean is_separator;
3898 if (!priv->popup_widget)
3901 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3903 if (priv->row_separator_func)
3904 is_separator = priv->row_separator_func (model, iter,
3905 priv->row_separator_data);
3907 is_separator = FALSE;
3909 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3911 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3912 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3915 if (priv->wrap_width &&
3916 gtk_widget_get_parent (item) == priv->popup_widget)
3918 GtkWidget *pitem = NULL;
3921 prev = gtk_tree_path_copy (path);
3923 if (gtk_tree_path_prev (prev))
3924 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3926 gtk_tree_path_free (prev);
3928 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3930 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3935 "bottom-attach", -1,
3938 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3941 gtk_combo_box_update_requested_width (combo_box, path);
3949 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3951 GtkComboBoxPrivate *priv = combo_box->priv;
3952 GtkTreeSelection *sel;
3954 GtkWidget *widget = GTK_WIDGET (combo_box);
3956 priv->button = gtk_toggle_button_new ();
3957 child = gtk_bin_get_child (GTK_BIN (combo_box));
3958 gtk_widget_set_parent (priv->button,
3959 gtk_widget_get_parent (child));
3960 g_signal_connect (priv->button, "button-press-event",
3961 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3962 g_signal_connect (priv->button, "toggled",
3963 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3965 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3966 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3967 priv->separator = NULL;
3968 gtk_widget_show_all (priv->button);
3970 if (priv->cell_view)
3972 GtkStyleContext *context;
3973 GtkStateFlags state;
3976 context = gtk_widget_get_style_context (widget);
3977 state = gtk_widget_get_state_flags (widget);
3979 gtk_style_context_get (context, state,
3980 "background-color", &color,
3983 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
3984 gdk_rgba_free (color);
3986 priv->box = gtk_event_box_new ();
3987 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3990 if (priv->has_frame)
3992 priv->cell_view_frame = gtk_frame_new (NULL);
3993 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3998 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3999 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
4003 gtk_widget_set_parent (priv->cell_view_frame,
4004 gtk_widget_get_parent (child));
4005 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
4006 gtk_widget_show_all (priv->cell_view_frame);
4008 g_signal_connect (priv->box, "button-press-event",
4009 G_CALLBACK (gtk_combo_box_list_button_pressed),
4013 priv->tree_view = gtk_tree_view_new ();
4014 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
4015 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
4016 gtk_tree_selection_set_select_function (sel,
4017 gtk_combo_box_list_select_func,
4019 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
4021 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
4023 if (priv->row_separator_func)
4024 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
4025 priv->row_separator_func,
4026 priv->row_separator_data,
4029 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
4031 priv->column = gtk_tree_view_column_new ();
4032 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
4035 gtk_combo_box_sync_cells (combo_box,
4036 GTK_CELL_LAYOUT (priv->column));
4038 if (gtk_tree_row_reference_valid (priv->active_row))
4042 path = gtk_tree_row_reference_get_path (priv->active_row);
4043 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4045 gtk_tree_path_free (path);
4048 /* set sample/popup widgets */
4049 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
4051 g_signal_connect (priv->tree_view, "key-press-event",
4052 G_CALLBACK (gtk_combo_box_list_key_press),
4054 g_signal_connect (priv->tree_view, "enter-notify-event",
4055 G_CALLBACK (gtk_combo_box_list_enter_notify),
4057 g_signal_connect (priv->tree_view, "row-expanded",
4058 G_CALLBACK (gtk_combo_box_model_row_expanded),
4060 g_signal_connect (priv->tree_view, "row-collapsed",
4061 G_CALLBACK (gtk_combo_box_model_row_expanded),
4063 g_signal_connect (priv->popup_window, "button-press-event",
4064 G_CALLBACK (gtk_combo_box_list_button_pressed),
4066 g_signal_connect (priv->popup_window, "button-release-event",
4067 G_CALLBACK (gtk_combo_box_list_button_released),
4070 gtk_widget_show (priv->tree_view);
4072 gtk_combo_box_update_sensitivity (combo_box);
4076 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
4078 GtkComboBoxPrivate *priv = combo_box->priv;
4080 /* disconnect signals */
4081 g_signal_handlers_disconnect_matched (priv->tree_view,
4082 G_SIGNAL_MATCH_DATA,
4083 0, 0, NULL, NULL, combo_box);
4084 g_signal_handlers_disconnect_matched (priv->button,
4085 G_SIGNAL_MATCH_DATA,
4087 gtk_combo_box_list_button_pressed,
4089 g_signal_handlers_disconnect_matched (priv->popup_window,
4090 G_SIGNAL_MATCH_DATA,
4092 gtk_combo_box_list_button_pressed,
4094 g_signal_handlers_disconnect_matched (priv->popup_window,
4095 G_SIGNAL_MATCH_DATA,
4097 gtk_combo_box_list_button_released,
4100 g_signal_handlers_disconnect_matched (priv->popup_window,
4101 G_SIGNAL_MATCH_DATA,
4103 gtk_combo_box_child_show,
4106 g_signal_handlers_disconnect_matched (priv->popup_window,
4107 G_SIGNAL_MATCH_DATA,
4109 gtk_combo_box_child_hide,
4113 g_signal_handlers_disconnect_matched (priv->box,
4114 G_SIGNAL_MATCH_DATA,
4116 gtk_combo_box_list_button_pressed,
4119 /* destroy things (unparent will kill the latest ref from us)
4120 * last unref on button will destroy the arrow
4122 gtk_widget_unparent (priv->button);
4123 priv->button = NULL;
4126 if (priv->cell_view)
4128 g_object_set (priv->cell_view,
4129 "background-set", FALSE,
4133 if (priv->cell_view_frame)
4135 gtk_widget_unparent (priv->cell_view_frame);
4136 priv->cell_view_frame = NULL;
4140 if (priv->scroll_timer)
4142 g_source_remove (priv->scroll_timer);
4143 priv->scroll_timer = 0;
4146 if (priv->resize_idle_id)
4148 g_source_remove (priv->resize_idle_id);
4149 priv->resize_idle_id = 0;
4152 gtk_widget_destroy (priv->tree_view);
4154 priv->tree_view = NULL;
4155 if (priv->popup_widget)
4157 g_object_unref (priv->popup_widget);
4158 priv->popup_widget = NULL;
4165 gtk_combo_box_list_button_pressed (GtkWidget *widget,
4166 GdkEventButton *event,
4169 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4170 GtkComboBoxPrivate *priv = combo_box->priv;
4172 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
4174 if (ewidget == priv->popup_window)
4177 if ((ewidget != priv->button && ewidget != priv->box) ||
4178 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4181 if (priv->focus_on_click &&
4182 !gtk_widget_has_focus (priv->button))
4183 gtk_widget_grab_focus (priv->button);
4185 gtk_combo_box_popup_for_device (combo_box, event->device);
4187 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
4189 priv->auto_scroll = FALSE;
4190 if (priv->scroll_timer == 0)
4191 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
4192 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
4195 priv->popup_in_progress = TRUE;
4201 gtk_combo_box_list_button_released (GtkWidget *widget,
4202 GdkEventButton *event,
4206 GtkTreePath *path = NULL;
4209 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4210 GtkComboBoxPrivate *priv = combo_box->priv;
4212 gboolean popup_in_progress = FALSE;
4214 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
4216 if (priv->popup_in_progress)
4218 popup_in_progress = TRUE;
4219 priv->popup_in_progress = FALSE;
4222 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
4224 if (priv->scroll_timer)
4226 g_source_remove (priv->scroll_timer);
4227 priv->scroll_timer = 0;
4230 if (ewidget != priv->tree_view)
4232 if ((ewidget == priv->button ||
4233 ewidget == priv->box) &&
4234 !popup_in_progress &&
4235 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4237 gtk_combo_box_popdown (combo_box);
4241 /* released outside treeview */
4242 if (ewidget != priv->button && ewidget != priv->box)
4244 gtk_combo_box_popdown (combo_box);
4252 /* select something cool */
4253 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
4259 return TRUE; /* clicked outside window? */
4261 gtk_tree_model_get_iter (priv->model, &iter, path);
4262 gtk_tree_path_free (path);
4264 gtk_combo_box_popdown (combo_box);
4266 if (tree_column_row_is_sensitive (combo_box, &iter))
4267 gtk_combo_box_set_active_iter (combo_box, &iter);
4273 gtk_combo_box_menu_key_press (GtkWidget *widget,
4277 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4279 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
4281 /* The menu hasn't managed the
4282 * event, forward it to the combobox
4284 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
4291 gtk_combo_box_list_key_press (GtkWidget *widget,
4295 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4298 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
4299 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
4301 GtkTreeModel *model = NULL;
4303 gtk_combo_box_popdown (combo_box);
4305 if (combo_box->priv->model)
4307 GtkTreeSelection *sel;
4309 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4311 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4312 gtk_combo_box_set_active_iter (combo_box, &iter);
4318 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
4320 /* The list hasn't managed the
4321 * event, forward it to the combobox
4323 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
4330 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4335 GtkAllocation allocation;
4336 GtkWidget *tree_view = combo_box->priv->tree_view;
4339 gtk_widget_get_allocation (tree_view, &allocation);
4341 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4342 if (adj && adj->upper - adj->lower > adj->page_size)
4344 if (x <= allocation.x &&
4345 adj->lower < adj->value)
4347 value = adj->value - (allocation.x - x + 1);
4348 gtk_adjustment_set_value (adj, value);
4350 else if (x >= allocation.x + allocation.width &&
4351 adj->upper - adj->page_size > adj->value)
4353 value = adj->value + (x - allocation.x - allocation.width + 1);
4354 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4358 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4359 if (adj && adj->upper - adj->lower > adj->page_size)
4361 if (y <= allocation.y &&
4362 adj->lower < adj->value)
4364 value = adj->value - (allocation.y - y + 1);
4365 gtk_adjustment_set_value (adj, value);
4367 else if (y >= allocation.height &&
4368 adj->upper - adj->page_size > adj->value)
4370 value = adj->value + (y - allocation.height + 1);
4371 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4377 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4379 GtkComboBoxPrivate *priv = combo_box->priv;
4382 if (priv->auto_scroll)
4384 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
4387 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4394 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4395 GdkEventCrossing *event,
4398 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4400 combo_box->priv->auto_scroll = TRUE;
4406 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4407 GtkTreeModel *model,
4409 gboolean path_currently_selected,
4413 gboolean sensitive = FALSE;
4415 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4417 GList *cells, *cell;
4418 gboolean cell_sensitive, cell_visible;
4420 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4422 if (!gtk_tree_view_column_get_visible (column))
4425 gtk_tree_model_get_iter (model, &iter, path);
4426 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4429 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4432 g_object_get (cell->data,
4433 "sensitive", &cell_sensitive,
4434 "visible", &cell_visible,
4437 if (cell_visible && cell_sensitive)
4442 g_list_free (cells);
4444 sensitive = cell_sensitive;
4451 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4456 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4458 gtk_combo_box_update_requested_width (combo_box, path);
4462 * GtkCellLayout implementation
4466 pack_start_recurse (GtkWidget *menu,
4467 GtkCellRenderer *cell,
4474 list = gtk_container_get_children (GTK_CONTAINER (menu));
4475 for (i = list; i; i = i->next)
4477 child = gtk_bin_get_child (GTK_BIN (i->data));
4478 if (GTK_IS_CELL_LAYOUT (child))
4479 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (child),
4482 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4483 if (submenu != NULL)
4484 pack_start_recurse (submenu, cell, expand);
4491 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4492 GtkCellRenderer *cell,
4495 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4496 ComboCellInfo *info;
4497 GtkComboBoxPrivate *priv;
4499 priv = combo_box->priv;
4501 g_object_ref_sink (cell);
4503 info = g_slice_new0 (ComboCellInfo);
4505 info->expand = expand;
4506 info->pack = GTK_PACK_START;
4508 priv->cells = g_slist_append (priv->cells, info);
4510 if (priv->cell_view)
4512 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4518 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4520 if (GTK_IS_MENU (priv->popup_widget))
4521 pack_start_recurse (priv->popup_widget, cell, expand);
4525 pack_end_recurse (GtkWidget *menu,
4526 GtkCellRenderer *cell,
4533 list = gtk_container_get_children (GTK_CONTAINER (menu));
4534 for (i = list; i; i = i->next)
4536 child = gtk_bin_get_child (GTK_BIN (i->data));
4537 if (GTK_IS_CELL_LAYOUT (child))
4538 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (child),
4541 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4542 if (submenu != NULL)
4543 pack_end_recurse (submenu, cell, expand);
4550 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4551 GtkCellRenderer *cell,
4554 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4555 ComboCellInfo *info;
4556 GtkComboBoxPrivate *priv;
4558 priv = combo_box->priv;
4560 g_object_ref_sink (cell);
4562 info = g_slice_new0 (ComboCellInfo);
4564 info->expand = expand;
4565 info->pack = GTK_PACK_END;
4567 priv->cells = g_slist_append (priv->cells, info);
4569 if (priv->cell_view)
4570 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4574 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4576 if (GTK_IS_MENU (priv->popup_widget))
4577 pack_end_recurse (priv->popup_widget, cell, expand);
4581 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4583 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4585 GList *retval = NULL;
4587 for (list = combo_box->priv->cells; list; list = list->next)
4589 ComboCellInfo *info = (ComboCellInfo *)list->data;
4591 retval = g_list_prepend (retval, info->cell);
4594 return g_list_reverse (retval);
4598 clear_recurse (GtkWidget *menu)
4604 list = gtk_container_get_children (GTK_CONTAINER (menu));
4605 for (i = list; i; i = i->next)
4607 child = gtk_bin_get_child (GTK_BIN (i->data));
4608 if (GTK_IS_CELL_LAYOUT (child))
4609 gtk_cell_layout_clear (GTK_CELL_LAYOUT (child));
4611 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4612 if (submenu != NULL)
4613 clear_recurse (submenu);
4620 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4622 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4623 GtkComboBoxPrivate *priv = combo_box->priv;
4626 if (priv->cell_view)
4627 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4630 gtk_tree_view_column_clear (priv->column);
4632 for (i = priv->cells; i; i = i->next)
4634 ComboCellInfo *info = (ComboCellInfo *)i->data;
4636 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4637 g_object_unref (info->cell);
4638 g_slice_free (ComboCellInfo, info);
4641 g_slist_free (priv->cells);
4644 if (GTK_IS_MENU (priv->popup_widget))
4645 clear_recurse (priv->popup_widget);
4649 add_attribute_recurse (GtkWidget *menu,
4650 GtkCellRenderer *cell,
4651 const gchar *attribute,
4658 list = gtk_container_get_children (GTK_CONTAINER (menu));
4659 for (i = list; i; i = i->next)
4661 child = gtk_bin_get_child (GTK_BIN (i->data));
4662 if (GTK_IS_CELL_LAYOUT (child))
4663 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (child),
4664 cell, attribute, column);
4666 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4667 if (submenu != NULL)
4668 add_attribute_recurse (submenu, cell, attribute, column);
4675 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4676 GtkCellRenderer *cell,
4677 const gchar *attribute,
4680 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4681 ComboCellInfo *info;
4683 info = gtk_combo_box_get_cell_info (combo_box, cell);
4684 g_return_if_fail (info != NULL);
4686 info->attributes = g_slist_prepend (info->attributes,
4687 GINT_TO_POINTER (column));
4688 info->attributes = g_slist_prepend (info->attributes,
4689 g_strdup (attribute));
4691 if (combo_box->priv->cell_view)
4692 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4693 cell, attribute, column);
4695 if (combo_box->priv->column)
4696 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4697 cell, attribute, column);
4699 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4700 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4701 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4705 combo_cell_data_func (GtkCellLayout *cell_layout,
4706 GtkCellRenderer *cell,
4707 GtkTreeModel *tree_model,
4711 ComboCellInfo *info = (ComboCellInfo *)data;
4712 GtkWidget *parent = NULL;
4717 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4719 if (GTK_IS_WIDGET (cell_layout))
4720 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4722 if (GTK_IS_MENU_ITEM (parent) &&
4723 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4724 g_object_set (cell, "sensitive", TRUE, NULL);
4729 set_cell_data_func_recurse (GtkWidget *menu,
4730 GtkCellRenderer *cell,
4731 ComboCellInfo *info)
4735 GtkWidget *cell_view;
4737 list = gtk_container_get_children (GTK_CONTAINER (menu));
4738 for (i = list; i; i = i->next)
4740 cell_view = gtk_bin_get_child (GTK_BIN (i->data));
4741 if (GTK_IS_CELL_LAYOUT (cell_view))
4743 /* Override sensitivity for inner nodes; we don't
4744 * want menuitems with submenus to appear insensitive
4746 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4748 combo_cell_data_func,
4750 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4751 if (submenu != NULL)
4752 set_cell_data_func_recurse (submenu, cell, info);
4760 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4761 GtkCellRenderer *cell,
4762 GtkCellLayoutDataFunc func,
4764 GDestroyNotify destroy)
4766 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4767 GtkComboBoxPrivate *priv = combo_box->priv;
4768 ComboCellInfo *info;
4770 info = gtk_combo_box_get_cell_info (combo_box, cell);
4771 g_return_if_fail (info != NULL);
4775 GDestroyNotify d = info->destroy;
4777 info->destroy = NULL;
4778 d (info->func_data);
4782 info->func_data = func_data;
4783 info->destroy = destroy;
4785 if (priv->cell_view)
4786 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4789 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4791 if (GTK_IS_MENU (priv->popup_widget))
4792 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4794 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4798 clear_attributes_recurse (GtkWidget *menu,
4799 GtkCellRenderer *cell)
4805 list = gtk_container_get_children (GTK_CONTAINER (menu));
4806 for (i = list; i; i = i->next)
4808 child = gtk_bin_get_child (GTK_BIN (i->data));
4809 if (GTK_IS_CELL_LAYOUT (child))
4810 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (child), cell);
4812 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4813 if (submenu != NULL)
4814 clear_attributes_recurse (submenu, cell);
4821 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4822 GtkCellRenderer *cell)
4824 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4825 GtkComboBoxPrivate *priv;
4826 ComboCellInfo *info;
4829 priv = combo_box->priv;
4831 info = gtk_combo_box_get_cell_info (combo_box, cell);
4832 g_return_if_fail (info != NULL);
4834 list = info->attributes;
4835 while (list && list->next)
4837 g_free (list->data);
4838 list = list->next->next;
4840 g_slist_free (info->attributes);
4841 info->attributes = NULL;
4843 if (priv->cell_view)
4844 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4847 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4849 if (GTK_IS_MENU (priv->popup_widget))
4850 clear_attributes_recurse (priv->popup_widget, cell);
4852 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4856 reorder_recurse (GtkWidget *menu,
4857 GtkCellRenderer *cell,
4864 list = gtk_container_get_children (GTK_CONTAINER (menu));
4865 for (i = list; i; i = i->next)
4867 child = gtk_bin_get_child (GTK_BIN (i->data));
4868 if (GTK_IS_CELL_LAYOUT (child))
4869 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (child),
4872 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4873 if (submenu != NULL)
4874 reorder_recurse (submenu, cell, position);
4881 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4882 GtkCellRenderer *cell,
4885 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4886 GtkComboBoxPrivate *priv;
4887 ComboCellInfo *info;
4890 priv = combo_box->priv;
4892 info = gtk_combo_box_get_cell_info (combo_box, cell);
4894 g_return_if_fail (info != NULL);
4895 g_return_if_fail (position >= 0);
4897 link = g_slist_find (priv->cells, info);
4899 g_return_if_fail (link != NULL);
4901 priv->cells = g_slist_delete_link (priv->cells, link);
4902 priv->cells = g_slist_insert (priv->cells, info, position);
4904 if (priv->cell_view)
4905 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4909 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4912 if (GTK_IS_MENU (priv->popup_widget))
4913 reorder_recurse (priv->popup_widget, cell, position);
4915 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4923 * gtk_combo_box_new:
4925 * Creates a new empty #GtkComboBox.
4927 * Return value: A new #GtkComboBox.
4932 gtk_combo_box_new (void)
4934 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4938 * gtk_combo_box_new_with_entry:
4940 * Creates a new empty #GtkComboBox with an entry.
4942 * Return value: A new #GtkComboBox.
4945 gtk_combo_box_new_with_entry (void)
4947 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
4951 * gtk_combo_box_new_with_model:
4952 * @model: A #GtkTreeModel.
4954 * Creates a new #GtkComboBox with the model initialized to @model.
4956 * Return value: A new #GtkComboBox.
4961 gtk_combo_box_new_with_model (GtkTreeModel *model)
4963 GtkComboBox *combo_box;
4965 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4967 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4969 return GTK_WIDGET (combo_box);
4973 * gtk_combo_box_new_with_model_and_entry:
4974 * @model: A #GtkTreeModel
4976 * Creates a new empty #GtkComboBox with an entry
4977 * and with the model initialized to @model.
4979 * Return value: A new #GtkComboBox
4982 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4984 return g_object_new (GTK_TYPE_COMBO_BOX,
4991 * gtk_combo_box_get_wrap_width:
4992 * @combo_box: A #GtkComboBox
4994 * Returns the wrap width which is used to determine the number of columns
4995 * for the popup menu. If the wrap width is larger than 1, the combo box
4998 * Returns: the wrap width.
5003 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
5005 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
5007 return combo_box->priv->wrap_width;
5011 * gtk_combo_box_set_wrap_width:
5012 * @combo_box: A #GtkComboBox
5013 * @width: Preferred number of columns
5015 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
5016 * the preferred number of columns when you want the popup to be layed out
5022 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
5025 GtkComboBoxPrivate *priv;
5027 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5028 g_return_if_fail (width >= 0);
5030 priv = combo_box->priv;
5032 if (width != priv->wrap_width)
5034 priv->wrap_width = width;
5036 gtk_combo_box_check_appearance (combo_box);
5037 gtk_combo_box_relayout (combo_box);
5039 g_object_notify (G_OBJECT (combo_box), "wrap-width");
5044 * gtk_combo_box_get_row_span_column:
5045 * @combo_box: A #GtkComboBox
5047 * Returns the column with row span information for @combo_box.
5049 * Returns: the row span column.
5054 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
5056 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
5058 return combo_box->priv->row_column;
5062 * gtk_combo_box_set_row_span_column:
5063 * @combo_box: A #GtkComboBox.
5064 * @row_span: A column in the model passed during construction.
5066 * Sets the column with row span information for @combo_box to be @row_span.
5067 * The row span column contains integers which indicate how many rows
5068 * an item should span.
5073 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
5076 GtkComboBoxPrivate *priv;
5079 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5081 priv = combo_box->priv;
5083 col = gtk_tree_model_get_n_columns (priv->model);
5084 g_return_if_fail (row_span >= -1 && row_span < col);
5086 if (row_span != priv->row_column)
5088 priv->row_column = row_span;
5090 gtk_combo_box_relayout (combo_box);
5092 g_object_notify (G_OBJECT (combo_box), "row-span-column");
5097 * gtk_combo_box_get_column_span_column:
5098 * @combo_box: A #GtkComboBox
5100 * Returns the column with column span information for @combo_box.
5102 * Returns: the column span column.
5107 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
5109 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
5111 return combo_box->priv->col_column;
5115 * gtk_combo_box_set_column_span_column:
5116 * @combo_box: A #GtkComboBox
5117 * @column_span: A column in the model passed during construction
5119 * Sets the column with column span information for @combo_box to be
5120 * @column_span. The column span column contains integers which indicate
5121 * how many columns an item should span.
5126 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
5129 GtkComboBoxPrivate *priv;
5132 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5134 priv = combo_box->priv;
5136 col = gtk_tree_model_get_n_columns (priv->model);
5137 g_return_if_fail (column_span >= -1 && column_span < col);
5139 if (column_span != priv->col_column)
5141 priv->col_column = column_span;
5143 gtk_combo_box_relayout (combo_box);
5145 g_object_notify (G_OBJECT (combo_box), "column-span-column");
5150 * gtk_combo_box_get_active:
5151 * @combo_box: A #GtkComboBox
5153 * Returns the index of the currently active item, or -1 if there's no
5154 * active item. If the model is a non-flat treemodel, and the active item
5155 * is not an immediate child of the root of the tree, this function returns
5156 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
5157 * <literal>path</literal> is the #GtkTreePath of the active item.
5159 * Return value: An integer which is the index of the currently active item,
5160 * or -1 if there's no active item.
5165 gtk_combo_box_get_active (GtkComboBox *combo_box)
5167 GtkComboBoxPrivate *priv;
5170 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5172 priv = combo_box->priv;
5174 if (gtk_tree_row_reference_valid (priv->active_row))
5178 path = gtk_tree_row_reference_get_path (priv->active_row);
5179 result = gtk_tree_path_get_indices (path)[0];
5180 gtk_tree_path_free (path);
5189 * gtk_combo_box_set_active:
5190 * @combo_box: A #GtkComboBox
5191 * @index_: An index in the model passed during construction, or -1 to have
5194 * Sets the active item of @combo_box to be the item at @index.
5199 gtk_combo_box_set_active (GtkComboBox *combo_box,
5202 GtkTreePath *path = NULL;
5203 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5204 g_return_if_fail (index_ >= -1);
5206 if (combo_box->priv->model == NULL)
5208 /* Save index, in case the model is set after the index */
5209 combo_box->priv->active = index_;
5215 path = gtk_tree_path_new_from_indices (index_, -1);
5217 gtk_combo_box_set_active_internal (combo_box, path);
5220 gtk_tree_path_free (path);
5224 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
5227 GtkComboBoxPrivate *priv = combo_box->priv;
5228 GtkTreePath *active_path;
5231 /* Remember whether the initially active row is valid. */
5232 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
5234 if (path && is_valid_row_reference)
5236 active_path = gtk_tree_row_reference_get_path (priv->active_row);
5237 path_cmp = gtk_tree_path_compare (path, active_path);
5238 gtk_tree_path_free (active_path);
5243 if (priv->active_row)
5245 gtk_tree_row_reference_free (priv->active_row);
5246 priv->active_row = NULL;
5251 if (priv->tree_view)
5252 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
5255 GtkMenu *menu = GTK_MENU (priv->popup_widget);
5257 if (GTK_IS_MENU (menu))
5258 gtk_menu_set_active (menu, -1);
5261 if (priv->cell_view)
5262 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
5265 * Do not emit a "changed" signal when an already invalid selection was
5266 * now set to invalid.
5268 if (!is_valid_row_reference)
5274 gtk_tree_row_reference_new (priv->model, path);
5276 if (priv->tree_view)
5278 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
5281 else if (GTK_IS_MENU (priv->popup_widget))
5283 /* FIXME handle nested menus better */
5284 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
5285 gtk_tree_path_get_indices (path)[0]);
5288 if (priv->cell_view)
5289 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
5293 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
5294 g_object_notify (G_OBJECT (combo_box), "active");
5295 if (combo_box->priv->id_column >= 0)
5296 g_object_notify (G_OBJECT (combo_box), "active-id");
5301 * gtk_combo_box_get_active_iter:
5302 * @combo_box: A #GtkComboBox
5303 * @iter: (out): The uninitialized #GtkTreeIter
5305 * Sets @iter to point to the current active item, if it exists.
5307 * Return value: %TRUE, if @iter was set
5312 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
5318 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5320 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
5323 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
5324 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
5325 gtk_tree_path_free (path);
5331 * gtk_combo_box_set_active_iter:
5332 * @combo_box: A #GtkComboBox
5333 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5335 * Sets the current active item to be the one referenced by @iter, or
5336 * unsets the active item if @iter is %NULL.
5341 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5344 GtkTreePath *path = NULL;
5346 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5349 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5351 gtk_combo_box_set_active_internal (combo_box, path);
5352 gtk_tree_path_free (path);
5356 * gtk_combo_box_set_model:
5357 * @combo_box: A #GtkComboBox
5358 * @model: (allow-none): A #GtkTreeModel
5360 * Sets the model used by @combo_box to be @model. Will unset a previously set
5361 * model (if applicable). If model is %NULL, then it will unset the model.
5363 * Note that this function does not clear the cell renderers, you have to
5364 * call gtk_cell_layout_clear() yourself if you need to set up different
5365 * cell renderers for the new model.
5370 gtk_combo_box_set_model (GtkComboBox *combo_box,
5371 GtkTreeModel *model)
5373 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5374 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5376 if (model == combo_box->priv->model)
5379 gtk_combo_box_unset_model (combo_box);
5384 combo_box->priv->model = model;
5385 g_object_ref (combo_box->priv->model);
5387 combo_box->priv->inserted_id =
5388 g_signal_connect (combo_box->priv->model, "row-inserted",
5389 G_CALLBACK (gtk_combo_box_model_row_inserted),
5391 combo_box->priv->deleted_id =
5392 g_signal_connect (combo_box->priv->model, "row-deleted",
5393 G_CALLBACK (gtk_combo_box_model_row_deleted),
5395 combo_box->priv->reordered_id =
5396 g_signal_connect (combo_box->priv->model, "rows-reordered",
5397 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5399 combo_box->priv->changed_id =
5400 g_signal_connect (combo_box->priv->model, "row-changed",
5401 G_CALLBACK (gtk_combo_box_model_row_changed),
5404 if (combo_box->priv->tree_view)
5407 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5408 combo_box->priv->model);
5409 gtk_combo_box_list_popup_resize (combo_box);
5414 if (combo_box->priv->popup_widget)
5415 gtk_combo_box_menu_fill (combo_box);
5419 if (combo_box->priv->cell_view)
5420 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5421 combo_box->priv->model);
5423 if (combo_box->priv->active != -1)
5425 /* If an index was set in advance, apply it now */
5426 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5427 combo_box->priv->active = -1;
5431 gtk_combo_box_update_sensitivity (combo_box);
5433 g_object_notify (G_OBJECT (combo_box), "model");
5437 * gtk_combo_box_get_model:
5438 * @combo_box: A #GtkComboBox
5440 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5442 * Return value: (transfer none): A #GtkTreeModel which was passed
5443 * during construction.
5448 gtk_combo_box_get_model (GtkComboBox *combo_box)
5450 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5452 return combo_box->priv->model;
5456 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5457 GtkScrollType scroll)
5460 GtkTreeIter new_iter;
5461 gboolean active_iter;
5464 if (!combo_box->priv->model)
5466 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5470 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5474 case GTK_SCROLL_STEP_BACKWARD:
5475 case GTK_SCROLL_STEP_UP:
5476 case GTK_SCROLL_STEP_LEFT:
5479 found = tree_prev (combo_box, combo_box->priv->model,
5480 &iter, &new_iter, FALSE);
5483 /* else fall through */
5485 case GTK_SCROLL_PAGE_FORWARD:
5486 case GTK_SCROLL_PAGE_DOWN:
5487 case GTK_SCROLL_PAGE_RIGHT:
5488 case GTK_SCROLL_END:
5489 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5492 case GTK_SCROLL_STEP_FORWARD:
5493 case GTK_SCROLL_STEP_DOWN:
5494 case GTK_SCROLL_STEP_RIGHT:
5497 found = tree_next (combo_box, combo_box->priv->model,
5498 &iter, &new_iter, FALSE);
5501 /* else fall through */
5503 case GTK_SCROLL_PAGE_BACKWARD:
5504 case GTK_SCROLL_PAGE_UP:
5505 case GTK_SCROLL_PAGE_LEFT:
5506 case GTK_SCROLL_START:
5507 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5514 if (found && active_iter)
5516 GtkTreePath *old_path;
5517 GtkTreePath *new_path;
5519 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5520 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5522 if (gtk_tree_path_compare (old_path, new_path) == 0)
5525 gtk_tree_path_free (old_path);
5526 gtk_tree_path_free (new_path);
5531 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5535 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5540 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5541 gboolean group_cycling)
5543 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5545 if (combo_box->priv->has_entry)
5549 child = gtk_bin_get_child (GTK_BIN (combo_box));
5551 gtk_widget_grab_focus (child);
5554 gtk_widget_grab_focus (combo_box->priv->button);
5560 gtk_combo_box_grab_focus (GtkWidget *widget)
5562 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5564 if (combo_box->priv->has_entry)
5568 child = gtk_bin_get_child (GTK_BIN (combo_box));
5570 gtk_widget_grab_focus (child);
5573 gtk_widget_grab_focus (combo_box->priv->button);
5577 gtk_combo_box_destroy (GtkWidget *widget)
5579 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5581 if (combo_box->priv->popup_idle_id > 0)
5583 g_source_remove (combo_box->priv->popup_idle_id);
5584 combo_box->priv->popup_idle_id = 0;
5587 gtk_combo_box_popdown (combo_box);
5589 if (combo_box->priv->row_separator_destroy)
5590 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5592 combo_box->priv->row_separator_func = NULL;
5593 combo_box->priv->row_separator_data = NULL;
5594 combo_box->priv->row_separator_destroy = NULL;
5596 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
5597 combo_box->priv->cell_view = NULL;
5601 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
5604 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
5607 * Fixes regression reported in bug #574059. The old functionality relied on
5608 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
5609 * when the selection was already set to -1.
5611 if (gtk_combo_box_get_active(combo_box) == -1)
5612 g_signal_emit_by_name (combo_box, "changed");
5614 gtk_combo_box_set_active (combo_box, -1);
5618 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
5621 GtkComboBoxPrivate *priv = combo_box->priv;
5622 GtkTreeModel *model;
5625 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5627 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
5631 GValue value = {0,};
5633 g_signal_handlers_block_by_func (entry,
5634 gtk_combo_box_entry_contents_changed,
5637 model = gtk_combo_box_get_model (combo_box);
5639 gtk_tree_model_get_value (model, &iter,
5640 priv->text_column, &value);
5641 g_object_set_property (G_OBJECT (entry), "text", &value);
5642 g_value_unset (&value);
5644 g_signal_handlers_unblock_by_func (entry,
5645 gtk_combo_box_entry_contents_changed,
5652 gtk_combo_box_constructor (GType type,
5653 guint n_construct_properties,
5654 GObjectConstructParam *construct_properties)
5657 GtkComboBox *combo_box;
5658 GtkComboBoxPrivate *priv;
5660 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
5661 (type, n_construct_properties, construct_properties);
5663 combo_box = GTK_COMBO_BOX (object);
5664 priv = combo_box->priv;
5666 if (priv->has_entry)
5670 entry = gtk_entry_new ();
5671 gtk_widget_show (entry);
5672 gtk_container_add (GTK_CONTAINER (combo_box), entry);
5674 priv->text_renderer = gtk_cell_renderer_text_new ();
5675 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
5676 priv->text_renderer, TRUE);
5678 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
5680 g_signal_connect (combo_box, "changed",
5681 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
5689 gtk_combo_box_dispose(GObject* object)
5691 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5693 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5695 gtk_combo_box_menu_destroy (combo_box);
5696 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5697 combo_box->priv->popup_widget = NULL;
5700 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5704 gtk_combo_box_finalize (GObject *object)
5706 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5709 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5710 gtk_combo_box_list_destroy (combo_box);
5712 if (combo_box->priv->popup_window)
5713 gtk_widget_destroy (combo_box->priv->popup_window);
5715 gtk_combo_box_unset_model (combo_box);
5717 for (i = combo_box->priv->cells; i; i = i->next)
5719 ComboCellInfo *info = (ComboCellInfo *)i->data;
5720 GSList *list = info->attributes;
5723 info->destroy (info->func_data);
5725 while (list && list->next)
5727 g_free (list->data);
5728 list = list->next->next;
5730 g_slist_free (info->attributes);
5732 g_object_unref (info->cell);
5733 g_slice_free (ComboCellInfo, info);
5735 g_slist_free (combo_box->priv->cells);
5737 g_free (combo_box->priv->tearoff_title);
5739 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5744 gtk_cell_editable_key_press (GtkWidget *widget,
5748 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5750 if (event->keyval == GDK_KEY_Escape)
5752 g_object_set (combo_box,
5753 "editing-canceled", TRUE,
5755 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5756 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5760 else if (event->keyval == GDK_KEY_Return ||
5761 event->keyval == GDK_KEY_ISO_Enter ||
5762 event->keyval == GDK_KEY_KP_Enter)
5764 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5765 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5774 popdown_idle (gpointer data)
5776 GtkComboBox *combo_box;
5778 combo_box = GTK_COMBO_BOX (data);
5780 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5781 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5783 g_object_unref (combo_box);
5789 popdown_handler (GtkWidget *widget,
5792 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5796 popup_idle (gpointer data)
5798 GtkComboBox *combo_box;
5800 combo_box = GTK_COMBO_BOX (data);
5802 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5803 combo_box->priv->cell_view)
5804 g_signal_connect_object (combo_box->priv->popup_widget,
5805 "unmap", G_CALLBACK (popdown_handler),
5808 /* we unset this if a menu item is activated */
5809 g_object_set (combo_box,
5810 "editing-canceled", TRUE,
5812 gtk_combo_box_popup (combo_box);
5814 combo_box->priv->popup_idle_id = 0;
5815 combo_box->priv->activate_button = 0;
5816 combo_box->priv->activate_time = 0;
5822 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5825 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5828 combo_box->priv->is_cell_renderer = TRUE;
5830 if (combo_box->priv->cell_view)
5832 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5833 G_CALLBACK (gtk_cell_editable_key_press),
5836 gtk_widget_grab_focus (combo_box->priv->button);
5840 child = gtk_bin_get_child (GTK_BIN (combo_box));
5842 g_signal_connect_object (child, "key-press-event",
5843 G_CALLBACK (gtk_cell_editable_key_press),
5846 gtk_widget_grab_focus (child);
5847 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5850 /* we do the immediate popup only for the optionmenu-like
5853 if (combo_box->priv->is_cell_renderer &&
5854 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5856 if (event && event->type == GDK_BUTTON_PRESS)
5858 GdkEventButton *event_button = (GdkEventButton *)event;
5860 combo_box->priv->activate_button = event_button->button;
5861 combo_box->priv->activate_time = event_button->time;
5864 combo_box->priv->popup_idle_id =
5865 gdk_threads_add_idle (popup_idle, combo_box);
5871 * gtk_combo_box_get_add_tearoffs:
5872 * @combo_box: a #GtkComboBox
5874 * Gets the current value of the :add-tearoffs property.
5876 * Return value: the current value of the :add-tearoffs property.
5879 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5881 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5883 return combo_box->priv->add_tearoffs;
5887 * gtk_combo_box_set_add_tearoffs:
5888 * @combo_box: a #GtkComboBox
5889 * @add_tearoffs: %TRUE to add tearoff menu items
5891 * Sets whether the popup menu should have a tearoff
5897 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5898 gboolean add_tearoffs)
5900 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5902 add_tearoffs = add_tearoffs != FALSE;
5904 if (combo_box->priv->add_tearoffs != add_tearoffs)
5906 combo_box->priv->add_tearoffs = add_tearoffs;
5907 gtk_combo_box_check_appearance (combo_box);
5908 gtk_combo_box_relayout (combo_box);
5909 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5914 * gtk_combo_box_get_title:
5915 * @combo_box: a #GtkComboBox
5917 * Gets the current title of the menu in tearoff mode. See
5918 * gtk_combo_box_set_add_tearoffs().
5920 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5921 * string which must not be freed.
5925 G_CONST_RETURN gchar*
5926 gtk_combo_box_get_title (GtkComboBox *combo_box)
5928 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5930 return combo_box->priv->tearoff_title;
5934 gtk_combo_box_update_title (GtkComboBox *combo_box)
5936 gtk_combo_box_check_appearance (combo_box);
5938 if (combo_box->priv->popup_widget &&
5939 GTK_IS_MENU (combo_box->priv->popup_widget))
5940 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5941 combo_box->priv->tearoff_title);
5945 * gtk_combo_box_set_title:
5946 * @combo_box: a #GtkComboBox
5947 * @title: a title for the menu in tearoff mode
5949 * Sets the menu's title in tearoff mode.
5954 gtk_combo_box_set_title (GtkComboBox *combo_box,
5957 GtkComboBoxPrivate *priv;
5959 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5961 priv = combo_box->priv;
5963 if (strcmp (title ? title : "",
5964 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5966 g_free (priv->tearoff_title);
5967 priv->tearoff_title = g_strdup (title);
5969 gtk_combo_box_update_title (combo_box);
5971 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5977 * gtk_combo_box_set_popup_fixed_width:
5978 * @combo_box: a #GtkComboBox
5979 * @fixed: whether to use a fixed popup width
5981 * Specifies whether the popup's width should be a fixed width
5982 * matching the allocated width of the combo box.
5987 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5990 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5992 if (combo_box->priv->popup_fixed_width != fixed)
5994 combo_box->priv->popup_fixed_width = fixed;
5996 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
6001 * gtk_combo_box_get_popup_fixed_width:
6002 * @combo_box: a #GtkComboBox
6004 * Gets whether the popup uses a fixed width matching
6005 * the allocated width of the combo box.
6007 * Returns: %TRUE if the popup uses a fixed width
6012 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
6014 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6016 return combo_box->priv->popup_fixed_width;
6021 * gtk_combo_box_get_popup_accessible:
6022 * @combo_box: a #GtkComboBox
6024 * Gets the accessible object corresponding to the combo box's popup.
6026 * This function is mostly intended for use by accessibility technologies;
6027 * applications should have little use for it.
6029 * Returns: (transfer none): the accessible object corresponding
6030 * to the combo box's popup.
6035 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
6039 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
6041 if (combo_box->priv->popup_widget)
6043 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
6051 * gtk_combo_box_get_row_separator_func:
6052 * @combo_box: a #GtkComboBox
6054 * Returns the current row separator function.
6056 * Return value: the current row separator function.
6060 GtkTreeViewRowSeparatorFunc
6061 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
6063 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
6065 return combo_box->priv->row_separator_func;
6069 * gtk_combo_box_set_row_separator_func:
6070 * @combo_box: a #GtkComboBox
6071 * @func: a #GtkTreeViewRowSeparatorFunc
6072 * @data: (allow-none): user data to pass to @func, or %NULL
6073 * @destroy: (allow-none): destroy notifier for @data, or %NULL
6075 * Sets the row separator function, which is used to determine
6076 * whether a row should be drawn as a separator. If the row separator
6077 * function is %NULL, no separators are drawn. This is the default value.
6082 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
6083 GtkTreeViewRowSeparatorFunc func,
6085 GDestroyNotify destroy)
6087 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6089 if (combo_box->priv->row_separator_destroy)
6090 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
6092 combo_box->priv->row_separator_func = func;
6093 combo_box->priv->row_separator_data = data;
6094 combo_box->priv->row_separator_destroy = destroy;
6096 if (combo_box->priv->tree_view)
6097 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
6100 gtk_combo_box_relayout (combo_box);
6102 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
6106 * gtk_combo_box_set_button_sensitivity:
6107 * @combo_box: a #GtkComboBox
6108 * @sensitivity: specify the sensitivity of the dropdown button
6110 * Sets whether the dropdown button of the combo box should be
6111 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
6112 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
6117 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
6118 GtkSensitivityType sensitivity)
6120 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6122 if (combo_box->priv->button_sensitivity != sensitivity)
6124 combo_box->priv->button_sensitivity = sensitivity;
6125 gtk_combo_box_update_sensitivity (combo_box);
6127 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
6132 * gtk_combo_box_get_button_sensitivity:
6133 * @combo_box: a #GtkComboBox
6135 * Returns whether the combo box sets the dropdown button
6136 * sensitive or not when there are no items in the model.
6138 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
6139 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
6140 * if the button is always insensitive or
6141 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
6142 * the model has one item to be selected.
6147 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
6149 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6151 return combo_box->priv->button_sensitivity;
6156 * gtk_combo_box_get_has_entry:
6157 * @combo_box: a #GtkComboBox
6159 * Returns whether the combo box has an entry.
6161 * Return Value: whether there is an entry in @combo_box.
6166 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
6168 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6170 return combo_box->priv->has_entry;
6174 * gtk_combo_box_set_entry_text_column:
6175 * @combo_box: A #GtkComboBox
6176 * @text_column: A column in @model to get the strings from for
6177 * the internal entry
6179 * Sets the model column which @combo_box should use to get strings from
6180 * to be @text_column. The column @text_column in the model of @combo_box
6181 * must be of type %G_TYPE_STRING.
6183 * This is only relevant if @combo_box has been created with
6184 * #GtkComboBox:has-entry as %TRUE.
6189 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
6192 GtkComboBoxPrivate *priv = combo_box->priv;
6193 GtkTreeModel *model;
6195 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6197 model = gtk_combo_box_get_model (combo_box);
6199 g_return_if_fail (text_column >= 0);
6200 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
6202 priv->text_column = text_column;
6204 if (priv->text_renderer != NULL)
6205 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
6206 priv->text_renderer,
6207 "text", text_column,
6212 * gtk_combo_box_get_entry_text_column:
6213 * @combo_box: A #GtkComboBox.
6215 * Returns the column which @combo_box is using to get the strings
6216 * from to display in the internal entry.
6218 * Return value: A column in the data source model of @combo_box.
6223 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
6225 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
6227 return combo_box->priv->text_column;
6231 * gtk_combo_box_set_focus_on_click:
6232 * @combo: a #GtkComboBox
6233 * @focus_on_click: whether the combo box grabs focus when clicked
6236 * Sets whether the combo box will grab focus when it is clicked with
6237 * the mouse. Making mouse clicks not grab focus is useful in places
6238 * like toolbars where you don't want the keyboard focus removed from
6239 * the main area of the application.
6244 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
6245 gboolean focus_on_click)
6247 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6249 focus_on_click = focus_on_click != FALSE;
6251 if (combo_box->priv->focus_on_click != focus_on_click)
6253 combo_box->priv->focus_on_click = focus_on_click;
6255 if (combo_box->priv->button)
6256 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
6259 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
6264 * gtk_combo_box_get_focus_on_click:
6265 * @combo: a #GtkComboBox
6267 * Returns whether the combo box grabs focus when it is clicked
6268 * with the mouse. See gtk_combo_box_set_focus_on_click().
6270 * Return value: %TRUE if the combo box grabs focus when it is
6271 * clicked with the mouse.
6276 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
6278 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6280 return combo_box->priv->focus_on_click;
6285 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
6286 GtkBuilder *builder,
6288 const gchar *tagname,
6289 GMarkupParser *parser,
6292 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6293 tagname, parser, data))
6296 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6297 tagname, parser, data);
6301 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
6302 GtkBuilder *builder,
6304 const gchar *tagname,
6307 if (strcmp (tagname, "attributes") == 0)
6308 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
6311 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
6316 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
6317 GtkBuilder *builder,
6318 const gchar *childname)
6320 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
6322 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
6323 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
6325 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
6329 gtk_combo_box_remeasure (GtkComboBox *combo_box)
6331 GtkComboBoxPrivate *priv = combo_box->priv;
6336 !gtk_tree_model_get_iter_first (priv->model, &iter))
6339 priv->minimum_width = priv->natural_width = 0;
6341 path = gtk_tree_path_new_from_indices (0, -1);
6345 gint row_min = 0, row_nat = 0;
6347 if (priv->cell_view)
6348 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
6349 path, &row_min, &row_nat);
6351 priv->minimum_width = MAX (priv->minimum_width, row_min);
6352 priv->natural_width = MAX (priv->natural_width, row_nat);
6354 gtk_tree_path_next (path);
6356 while (gtk_tree_model_iter_next (priv->model, &iter));
6358 gtk_tree_path_free (path);
6363 gtk_combo_box_measure_height_for_width (GtkComboBox *combo_box,
6369 GtkComboBoxPrivate *priv = combo_box->priv;
6372 gint child_min, child_nat;
6374 child = gtk_bin_get_child (GTK_BIN (combo_box));
6376 gtk_widget_get_preferred_height_for_width (child, avail_width,
6377 &child_min, &child_nat);
6380 !gtk_tree_model_get_iter_first (priv->model, &iter))
6383 path = gtk_tree_path_new_from_indices (0, -1);
6387 gint row_min = 0, row_nat = 0;
6389 if (priv->cell_view)
6390 gtk_cell_view_get_desired_height_for_width_of_row (GTK_CELL_VIEW (priv->cell_view),
6392 &row_min, &row_nat);
6394 child_min = MAX (child_min, row_min);
6395 child_nat = MAX (child_nat, row_nat);
6397 gtk_tree_path_next (path);
6399 while (gtk_tree_model_iter_next (priv->model, &iter));
6401 gtk_tree_path_free (path);
6406 *min_height = child_min;
6408 *nat_height = child_nat;
6413 gtk_combo_box_get_preferred_width (GtkWidget *widget,
6417 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
6418 GtkComboBoxPrivate *priv = combo_box->priv;
6419 gint focus_width, focus_pad;
6420 gint font_size, arrow_size;
6421 PangoContext *context;
6422 PangoFontMetrics *metrics;
6423 PangoFontDescription *font_desc;
6425 gint minimum_width, natural_width;
6426 gint child_min, child_nat;
6427 GtkStyleContext *style_context;
6428 GtkStateFlags state;
6431 child = gtk_bin_get_child (GTK_BIN (widget));
6434 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
6435 gtk_combo_box_remeasure (combo_box);
6437 child_min = MAX (child_min, priv->minimum_width);
6438 child_nat = MAX (child_nat, priv->natural_width);
6440 gtk_widget_style_get (GTK_WIDGET (widget),
6441 "focus-line-width", &focus_width,
6442 "focus-padding", &focus_pad,
6443 "arrow-size", &arrow_size,
6446 style_context = gtk_widget_get_style_context (widget);
6447 state = gtk_widget_get_state_flags (widget);
6449 gtk_style_context_get (style_context, state,
6451 "border-width", &border,
6454 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
6455 metrics = pango_context_get_metrics (context, font_desc,
6456 pango_context_get_language (context));
6457 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
6458 pango_font_metrics_get_descent (metrics));
6459 pango_font_metrics_unref (metrics);
6460 pango_font_description_free (font_desc);
6462 arrow_size = MAX (arrow_size, font_size);
6464 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
6466 if (!priv->tree_view)
6470 if (priv->cell_view)
6472 gint sep_width, arrow_width;
6473 gint border_width, xpad;
6474 GtkBorder button_border;
6476 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
6477 get_widget_border (priv->button, &button_border);
6479 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
6480 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
6482 xpad = 2 * (border_width + focus_width + focus_pad) +
6483 button_border.left + button_border.right;
6485 minimum_width = child_min + sep_width + arrow_width + xpad;
6486 natural_width = child_nat + sep_width + arrow_width + xpad;
6490 gint but_width, but_nat_width;
6492 gtk_widget_get_preferred_width (priv->button,
6493 &but_width, &but_nat_width);
6495 minimum_width = child_min + but_width;
6496 natural_width = child_nat + but_nat_width;
6502 gint button_width, button_nat_width;
6504 /* sample + frame */
6505 minimum_width = child_min;
6506 natural_width = child_nat;
6508 minimum_width += 2 * focus_width;
6509 natural_width += 2 * focus_width;
6511 if (priv->cell_view_frame)
6513 if (priv->has_frame)
6515 gint border_width, xpad;
6516 GtkBorder frame_border;
6518 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
6519 get_widget_border (priv->cell_view_frame, &frame_border);
6520 xpad = (2 * border_width) + frame_border.left + frame_border.right;
6522 minimum_width += xpad;
6523 natural_width += xpad;
6528 gtk_widget_get_preferred_width (priv->button,
6529 &button_width, &button_nat_width);
6531 minimum_width += button_width;
6532 natural_width += button_nat_width;
6535 minimum_width += border->left + border->right;
6536 natural_width += border->left + border->right;
6537 gtk_border_free (border);
6540 *minimum_size = minimum_width;
6543 *natural_size = natural_width;
6547 gtk_combo_box_get_preferred_height (GtkWidget *widget,
6553 /* Combo box is height-for-width only
6554 * (so we always just reserve enough height for the minimum width) */
6555 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
6556 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
6560 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
6565 /* Combo box is height-for-width only
6566 * (so we assume we always reserved enough height for the minimum width) */
6567 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
6572 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
6577 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
6578 GtkComboBoxPrivate *priv = combo_box->priv;
6579 gint focus_width, focus_pad;
6580 gint min_height, nat_height;
6584 gtk_widget_style_get (GTK_WIDGET (widget),
6585 "focus-line-width", &focus_width,
6586 "focus-padding", &focus_pad,
6589 get_widget_border (widget, &border);
6590 size = avail_size - border.left;
6592 if (!priv->tree_view)
6595 if (priv->cell_view)
6597 /* calculate x/y padding and separator/arrow size */
6598 gint sep_width, arrow_width, sep_height, arrow_height;
6599 gint border_width, xpad, ypad;
6600 GtkBorder button_border;
6602 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
6603 get_widget_border (priv->button, &button_border);
6605 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
6606 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
6607 gtk_widget_get_preferred_height_for_width (priv->separator,
6608 sep_width, &sep_height, NULL);
6609 gtk_widget_get_preferred_height_for_width (priv->arrow,
6610 arrow_width, &arrow_height, NULL);
6612 xpad = 2 * (border_width + focus_width + focus_pad) +
6613 button_border.left + button_border.right;
6614 ypad = 2 * (border_width + focus_width + focus_pad) +
6615 button_border.top + button_border.bottom;
6617 size -= sep_width + arrow_width + xpad;
6619 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6621 arrow_height = MAX (arrow_height, sep_height);
6622 min_height = MAX (min_height, arrow_height);
6623 nat_height = MAX (nat_height, arrow_height);
6630 /* there is a custom child widget inside (no priv->cell_view) */
6631 gint but_width, but_height;
6633 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
6634 gtk_widget_get_preferred_height_for_width (priv->button,
6635 but_width, &but_height, NULL);
6639 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6641 min_height = MAX (min_height, but_height);
6642 nat_height = MAX (nat_height, but_height);
6648 gint but_width, but_height;
6649 gint xpad = 0, ypad = 0;
6651 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
6652 gtk_widget_get_preferred_height_for_width (priv->button,
6653 but_width, &but_height, NULL);
6655 if (priv->cell_view_frame && priv->has_frame)
6657 GtkBorder frame_border;
6660 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
6661 get_widget_border (GTK_WIDGET (priv->cell_view_frame), &frame_border);
6663 xpad = (2 * border_width) + border.left + frame_border.right;
6664 ypad = (2 * border_width) + border.top + frame_border.bottom;
6668 size -= 2 * focus_width;
6671 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6673 min_height = MAX (min_height, but_height);
6674 nat_height = MAX (nat_height, but_height);
6680 min_height += border.top + border.bottom;
6681 nat_height += border.top + border.bottom;
6684 *minimum_size = min_height;
6687 *natural_size = nat_height;
6691 * gtk_combo_box_set_id_column:
6692 * @combo_box: A #GtkComboBox
6693 * @id_column: A column in @model to get string IDs for values from
6695 * Sets the model column which @combo_box should use to get string IDs
6696 * for values from. The column @id_column in the model of @combo_box
6697 * must be of type %G_TYPE_STRING.
6702 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
6705 GtkComboBoxPrivate *priv = combo_box->priv;
6706 GtkTreeModel *model;
6708 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6710 if (id_column != priv->id_column)
6712 model = gtk_combo_box_get_model (combo_box);
6714 g_return_if_fail (id_column >= 0);
6715 g_return_if_fail (model == NULL ||
6716 id_column < gtk_tree_model_get_n_columns (model));
6718 priv->id_column = id_column;
6720 g_object_notify (G_OBJECT (combo_box), "id-column");
6721 g_object_notify (G_OBJECT (combo_box), "active-id");
6726 * gtk_combo_box_get_id_column:
6727 * @combo_box: A #GtkComboBox
6729 * Returns the column which @combo_box is using to get string IDs
6732 * Return value: A column in the data source model of @combo_box.
6737 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
6739 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
6741 return combo_box->priv->id_column;
6745 * gtk_combo_box_get_active_id:
6746 * @combo_box: a #GtkComboBox
6748 * Returns the ID of the active row of @combo_box. This value is taken
6749 * from the active row and the column specified by the 'id-column'
6750 * property of @combo_box (see gtk_combo_box_set_id_column()).
6752 * The returned value is an interned string which means that you can
6753 * compare the pointer by value to other interned strings and that you
6756 * If the 'id-column' property of @combo_box is not set or if no row is
6757 * selected then %NULL is returned.
6759 * Return value: the ID of the active row, or %NULL
6764 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
6766 GtkTreeModel *model;
6770 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
6772 column = combo_box->priv->id_column;
6777 model = gtk_combo_box_get_model (combo_box);
6778 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
6779 G_TYPE_STRING, NULL);
6781 if (gtk_combo_box_get_active_iter (combo_box, &iter))
6783 const gchar *interned;
6786 gtk_tree_model_get (model, &iter, column, &id, -1);
6787 interned = g_intern_string (id);
6797 * gtk_combo_box_set_active_id:
6798 * @combo_box: a #GtkComboBox
6799 * @active_id: the ID of the row to select
6801 * Changes the active row of @combo_box to the one that has an ID equal to @id.
6803 * If the 'id-column' property of @combo_box is unset or if no row has
6804 * the given ID then nothing happens.
6809 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
6810 const gchar *active_id)
6812 GtkTreeModel *model;
6816 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6818 column = combo_box->priv->id_column;
6823 model = gtk_combo_box_get_model (combo_box);
6824 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
6827 if (gtk_tree_model_get_iter_first (model, &iter))
6832 gtk_tree_model_get (model, &iter, column, &id, -1);
6833 match = strcmp (id, active_id) == 0;
6838 gtk_combo_box_set_active_iter (combo_box, &iter);
6841 } while (gtk_tree_model_iter_next (model, &iter));