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_set (GtkWidget *widget,
285 static void gtk_combo_box_button_toggled (GtkWidget *widget,
287 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
288 GtkStateType previous,
290 static void gtk_combo_box_add (GtkContainer *container,
292 static void gtk_combo_box_remove (GtkContainer *container,
295 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
296 GtkCellRenderer *cell);
298 static void gtk_combo_box_menu_show (GtkWidget *menu,
300 static void gtk_combo_box_menu_hide (GtkWidget *menu,
303 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
305 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
310 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
315 static void gtk_combo_box_menu_position (GtkMenu *menu,
321 static void gtk_combo_box_update_requested_width(GtkComboBox *combo_box,
323 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
325 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
327 static void gtk_combo_box_size_allocate (GtkWidget *widget,
328 GtkAllocation *allocation);
329 static void gtk_combo_box_forall (GtkContainer *container,
330 gboolean include_internals,
331 GtkCallback callback,
332 gpointer callback_data);
333 static gboolean gtk_combo_box_draw (GtkWidget *widget,
335 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
336 GdkEventScroll *event);
337 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
340 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
341 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
342 GtkScrollType scroll);
343 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
344 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
346 /* listening to the model */
347 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
351 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
354 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
359 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
363 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
369 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
374 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
375 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
377 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
378 GdkEventButton *event,
380 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
383 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
384 GdkEventCrossing *event,
386 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
389 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
390 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
391 GdkEventButton *event,
394 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
397 gboolean path_currently_selected,
400 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
404 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
407 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
408 gboolean add_children);
409 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
410 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
413 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
414 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
416 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
420 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
422 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
423 GdkEventButton *event,
425 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
428 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
429 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
433 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
436 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
441 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
445 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
448 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
450 guint32 activate_time);
451 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
456 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
457 GtkCellRenderer *cell,
459 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
460 GtkCellRenderer *cell,
462 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
463 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
464 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
465 GtkCellRenderer *cell,
466 const gchar *attribute,
468 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
469 GtkCellRenderer *cell,
470 GtkCellLayoutDataFunc func,
472 GDestroyNotify destroy);
473 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
474 GtkCellRenderer *cell);
475 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
476 GtkCellRenderer *cell,
478 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
479 gboolean group_cycling);
481 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
482 GtkCellLayout *cell_layout);
483 static void combo_cell_data_func (GtkCellLayout *cell_layout,
484 GtkCellRenderer *cell,
485 GtkTreeModel *tree_model,
488 static void gtk_combo_box_child_show (GtkWidget *widget,
489 GtkComboBox *combo_box);
490 static void gtk_combo_box_child_hide (GtkWidget *widget,
491 GtkComboBox *combo_box);
493 /* GtkComboBox:has-entry callbacks */
494 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
496 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
500 /* GtkBuildable method implementation */
501 static GtkBuildableIface *parent_buildable_iface;
503 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
504 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
507 const gchar *tagname,
508 GMarkupParser *parser,
510 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
513 const gchar *tagname,
515 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
517 const gchar *childname);
520 /* GtkCellEditable method implementations */
521 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
524 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
527 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
530 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
534 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
540 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
541 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
542 gtk_combo_box_cell_layout_init)
543 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
544 gtk_combo_box_cell_editable_init)
545 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
546 gtk_combo_box_buildable_init))
551 gtk_combo_box_class_init (GtkComboBoxClass *klass)
553 GObjectClass *object_class;
554 GtkContainerClass *container_class;
555 GtkWidgetClass *widget_class;
556 GtkBindingSet *binding_set;
558 container_class = (GtkContainerClass *)klass;
559 container_class->forall = gtk_combo_box_forall;
560 container_class->add = gtk_combo_box_add;
561 container_class->remove = gtk_combo_box_remove;
563 widget_class = (GtkWidgetClass *)klass;
564 widget_class->size_allocate = gtk_combo_box_size_allocate;
565 widget_class->draw = gtk_combo_box_draw;
566 widget_class->scroll_event = gtk_combo_box_scroll_event;
567 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
568 widget_class->grab_focus = gtk_combo_box_grab_focus;
569 widget_class->style_set = gtk_combo_box_style_set;
570 widget_class->state_changed = gtk_combo_box_state_changed;
571 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
572 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
573 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
574 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
575 widget_class->destroy = gtk_combo_box_destroy;
577 object_class = (GObjectClass *)klass;
578 object_class->constructor = gtk_combo_box_constructor;
579 object_class->dispose = gtk_combo_box_dispose;
580 object_class->finalize = gtk_combo_box_finalize;
581 object_class->set_property = gtk_combo_box_set_property;
582 object_class->get_property = gtk_combo_box_get_property;
586 * GtkComboBox::changed:
587 * @widget: the object which received the signal
589 * The changed signal is emitted when the active
590 * item is changed. The can be due to the user selecting
591 * a different item from the list, or due to a
592 * call to gtk_combo_box_set_active_iter().
593 * It will also be emitted while typing into the entry of a combo box
598 combo_box_signals[CHANGED] =
599 g_signal_new (I_("changed"),
600 G_OBJECT_CLASS_TYPE (klass),
602 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
604 g_cclosure_marshal_VOID__VOID,
607 * GtkComboBox::move-active:
608 * @widget: the object that received the signal
609 * @scroll_type: a #GtkScrollType
611 * The ::move-active signal is a
612 * <link linkend="keybinding-signals">keybinding signal</link>
613 * which gets emitted to move the active selection.
617 combo_box_signals[MOVE_ACTIVE] =
618 g_signal_new_class_handler (I_("move-active"),
619 G_OBJECT_CLASS_TYPE (klass),
620 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
621 G_CALLBACK (gtk_combo_box_real_move_active),
623 g_cclosure_marshal_VOID__ENUM,
625 GTK_TYPE_SCROLL_TYPE);
628 * GtkComboBox::popup:
629 * @widget: the object that received the signal
631 * The ::popup signal is a
632 * <link linkend="keybinding-signals">keybinding signal</link>
633 * which gets emitted to popup the combo box list.
635 * The default binding for this signal is Alt+Down.
639 combo_box_signals[POPUP] =
640 g_signal_new_class_handler (I_("popup"),
641 G_OBJECT_CLASS_TYPE (klass),
642 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
643 G_CALLBACK (gtk_combo_box_real_popup),
645 g_cclosure_marshal_VOID__VOID,
648 * GtkComboBox::popdown:
649 * @button: the object which received the signal
651 * The ::popdown signal is a
652 * <link linkend="keybinding-signals">keybinding signal</link>
653 * which gets emitted to popdown the combo box list.
655 * The default bindings for this signal are Alt+Up and Escape.
659 combo_box_signals[POPDOWN] =
660 g_signal_new_class_handler (I_("popdown"),
661 G_OBJECT_CLASS_TYPE (klass),
662 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
663 G_CALLBACK (gtk_combo_box_real_popdown),
665 _gtk_marshal_BOOLEAN__VOID,
669 binding_set = gtk_binding_set_by_class (widget_class);
671 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
673 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
676 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
678 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
680 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
683 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
685 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
686 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
688 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
689 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
691 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
692 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
694 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
695 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
697 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
698 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
700 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
702 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
704 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
705 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
707 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
708 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
710 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
711 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
713 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
714 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
716 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
717 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
719 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
722 g_object_class_override_property (object_class,
723 PROP_EDITING_CANCELED,
729 * The model from which the combo box takes the values shown
734 g_object_class_install_property (object_class,
736 g_param_spec_object ("model",
737 P_("ComboBox model"),
738 P_("The model for the combo box"),
740 GTK_PARAM_READWRITE));
743 * GtkComboBox:wrap-width:
745 * If wrap-width is set to a positive value, the list will be
746 * displayed in multiple columns, the number of columns is
747 * determined by wrap-width.
751 g_object_class_install_property (object_class,
753 g_param_spec_int ("wrap-width",
755 P_("Wrap width for laying out the items in a grid"),
759 GTK_PARAM_READWRITE));
763 * GtkComboBox:row-span-column:
765 * If this is set to a non-negative value, it must be the index of a column
766 * of type %G_TYPE_INT in the model.
768 * The values of that column are used to determine how many rows a value in
769 * the list will span. Therefore, the values in the model column pointed to
770 * by this property must be greater than zero and not larger than wrap-width.
774 g_object_class_install_property (object_class,
775 PROP_ROW_SPAN_COLUMN,
776 g_param_spec_int ("row-span-column",
777 P_("Row span column"),
778 P_("TreeModel column containing the row span values"),
782 GTK_PARAM_READWRITE));
786 * GtkComboBox:column-span-column:
788 * If this is set to a non-negative value, it must be the index of a column
789 * of type %G_TYPE_INT in the model.
791 * The values of that column are used to determine how many columns a value
792 * in the list will span.
796 g_object_class_install_property (object_class,
797 PROP_COLUMN_SPAN_COLUMN,
798 g_param_spec_int ("column-span-column",
799 P_("Column span column"),
800 P_("TreeModel column containing the column span values"),
804 GTK_PARAM_READWRITE));
808 * GtkComboBox:active:
810 * The item which is currently active. If the model is a non-flat treemodel,
811 * and the active item is not an immediate child of the root of the tree,
812 * this property has the value
813 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
814 * where <literal>path</literal> is the #GtkTreePath of the active item.
818 g_object_class_install_property (object_class,
820 g_param_spec_int ("active",
822 P_("The item which is currently active"),
826 GTK_PARAM_READWRITE));
829 * GtkComboBox:add-tearoffs:
831 * The add-tearoffs property controls whether generated menus
832 * have tearoff menu items.
834 * Note that this only affects menu style combo boxes.
838 g_object_class_install_property (object_class,
840 g_param_spec_boolean ("add-tearoffs",
841 P_("Add tearoffs to menus"),
842 P_("Whether dropdowns should have a tearoff menu item"),
844 GTK_PARAM_READWRITE));
847 * GtkComboBox:has-frame:
849 * The has-frame property controls whether a frame
850 * is drawn around the entry.
854 g_object_class_install_property (object_class,
856 g_param_spec_boolean ("has-frame",
858 P_("Whether the combo box draws a frame around the child"),
860 GTK_PARAM_READWRITE));
862 g_object_class_install_property (object_class,
864 g_param_spec_boolean ("focus-on-click",
865 P_("Focus on click"),
866 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
868 GTK_PARAM_READWRITE));
871 * GtkComboBox:tearoff-title:
873 * A title that may be displayed by the window manager
874 * when the popup is torn-off.
878 g_object_class_install_property (object_class,
880 g_param_spec_string ("tearoff-title",
882 P_("A title that may be displayed by the window manager when the popup is torn-off"),
884 GTK_PARAM_READWRITE));
888 * GtkComboBox:popup-shown:
890 * Whether the combo boxes dropdown is popped up.
891 * Note that this property is mainly useful, because
892 * it allows you to connect to notify::popup-shown.
896 g_object_class_install_property (object_class,
898 g_param_spec_boolean ("popup-shown",
900 P_("Whether the combo's dropdown is shown"),
902 GTK_PARAM_READABLE));
906 * GtkComboBox:button-sensitivity:
908 * Whether the dropdown button is sensitive when
909 * the model is empty.
913 g_object_class_install_property (object_class,
914 PROP_BUTTON_SENSITIVITY,
915 g_param_spec_enum ("button-sensitivity",
916 P_("Button Sensitivity"),
917 P_("Whether the dropdown button is sensitive when the model is empty"),
918 GTK_TYPE_SENSITIVITY_TYPE,
919 GTK_SENSITIVITY_AUTO,
920 GTK_PARAM_READWRITE));
923 * GtkComboBox:has-entry:
925 * Whether the combo box has an entry.
929 g_object_class_install_property (object_class,
931 g_param_spec_boolean ("has-entry",
933 P_("Whether combo box has an entry"),
935 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
938 * GtkComboBox:entry-text-column:
940 * The column in the combo box's model to associate with strings from the entry
941 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
945 g_object_class_install_property (object_class,
946 PROP_ENTRY_TEXT_COLUMN,
947 g_param_spec_int ("entry-text-column",
948 P_("Entry Text Column"),
949 P_("The column in the combo box's model to associate "
950 "with strings from the entry if the combo was "
951 "created with #GtkComboBox:has-entry = %TRUE"),
953 GTK_PARAM_READWRITE));
956 * GtkComboBox:id-column:
958 * The column in the combo box's model that provides string
959 * IDs for the values in the model, if != -1.
963 g_object_class_install_property (object_class,
965 g_param_spec_int ("id-column",
967 P_("The column in the combo box's model that provides "
968 "string IDs for the values in the model"),
970 GTK_PARAM_READWRITE));
973 * GtkComboBox:active-id:
975 * The value of the ID column of the active row.
979 g_object_class_install_property (object_class,
981 g_param_spec_string ("active-id",
983 P_("The value of the id column "
984 "for the active row"),
985 NULL, GTK_PARAM_READWRITE));
988 * GtkComboBox:popup-fixed-width:
990 * Whether the popup's width should be a fixed width matching the
991 * allocated width of the combo box.
995 g_object_class_install_property (object_class,
996 PROP_POPUP_FIXED_WIDTH,
997 g_param_spec_boolean ("popup-fixed-width",
998 P_("Popup Fixed Width"),
999 P_("Whether the popup's width should be a "
1000 "fixed width matching the allocated width "
1001 "of the combo box"),
1003 GTK_PARAM_READWRITE));
1005 gtk_widget_class_install_style_property (widget_class,
1006 g_param_spec_boolean ("appears-as-list",
1007 P_("Appears as list"),
1008 P_("Whether dropdowns should look like lists rather than menus"),
1010 GTK_PARAM_READABLE));
1013 * GtkComboBox:arrow-size:
1015 * Sets the minimum size of the arrow in the combo box. Note
1016 * that the arrow size is coupled to the font size, so in case
1017 * a larger font is used, the arrow will be larger than set
1022 gtk_widget_class_install_style_property (widget_class,
1023 g_param_spec_int ("arrow-size",
1025 P_("The minimum size of the arrow in the combo box"),
1029 GTK_PARAM_READABLE));
1032 * GtkComboBox:shadow-type:
1034 * Which kind of shadow to draw around the combo box.
1038 gtk_widget_class_install_style_property (widget_class,
1039 g_param_spec_enum ("shadow-type",
1041 P_("Which kind of shadow to draw around the combo box"),
1042 GTK_TYPE_SHADOW_TYPE,
1044 GTK_PARAM_READABLE));
1046 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1050 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1052 parent_buildable_iface = g_type_interface_peek_parent (iface);
1053 iface->add_child = _gtk_cell_layout_buildable_add_child;
1054 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1055 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1056 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1060 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1062 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
1063 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
1064 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
1065 iface->clear = gtk_combo_box_cell_layout_clear;
1066 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
1067 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
1068 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
1069 iface->reorder = gtk_combo_box_cell_layout_reorder;
1073 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1075 iface->start_editing = gtk_combo_box_start_editing;
1079 gtk_combo_box_init (GtkComboBox *combo_box)
1081 GtkComboBoxPrivate *priv;
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);
1122 gtk_combo_box_set_property (GObject *object,
1124 const GValue *value,
1127 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1132 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1135 case PROP_WRAP_WIDTH:
1136 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1139 case PROP_ROW_SPAN_COLUMN:
1140 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1143 case PROP_COLUMN_SPAN_COLUMN:
1144 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1148 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1151 case PROP_ADD_TEAROFFS:
1152 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1155 case PROP_HAS_FRAME:
1156 combo_box->priv->has_frame = g_value_get_boolean (value);
1158 if (combo_box->priv->has_entry)
1162 child = gtk_bin_get_child (GTK_BIN (combo_box));
1164 gtk_entry_set_has_frame (GTK_ENTRY (child),
1165 combo_box->priv->has_frame);
1170 case PROP_FOCUS_ON_CLICK:
1171 gtk_combo_box_set_focus_on_click (combo_box,
1172 g_value_get_boolean (value));
1175 case PROP_TEAROFF_TITLE:
1176 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1179 case PROP_POPUP_SHOWN:
1180 if (g_value_get_boolean (value))
1181 gtk_combo_box_popup (combo_box);
1183 gtk_combo_box_popdown (combo_box);
1186 case PROP_BUTTON_SENSITIVITY:
1187 gtk_combo_box_set_button_sensitivity (combo_box,
1188 g_value_get_enum (value));
1191 case PROP_POPUP_FIXED_WIDTH:
1192 gtk_combo_box_set_popup_fixed_width (combo_box,
1193 g_value_get_boolean (value));
1196 case PROP_EDITING_CANCELED:
1197 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1200 case PROP_HAS_ENTRY:
1201 combo_box->priv->has_entry = g_value_get_boolean (value);
1204 case PROP_ENTRY_TEXT_COLUMN:
1205 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1208 case PROP_ID_COLUMN:
1209 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1212 case PROP_ACTIVE_ID:
1213 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1223 gtk_combo_box_get_property (GObject *object,
1228 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1229 GtkComboBoxPrivate *priv = combo_box->priv;
1234 g_value_set_object (value, combo_box->priv->model);
1237 case PROP_WRAP_WIDTH:
1238 g_value_set_int (value, combo_box->priv->wrap_width);
1241 case PROP_ROW_SPAN_COLUMN:
1242 g_value_set_int (value, combo_box->priv->row_column);
1245 case PROP_COLUMN_SPAN_COLUMN:
1246 g_value_set_int (value, combo_box->priv->col_column);
1250 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1253 case PROP_ADD_TEAROFFS:
1254 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1257 case PROP_HAS_FRAME:
1258 g_value_set_boolean (value, combo_box->priv->has_frame);
1261 case PROP_FOCUS_ON_CLICK:
1262 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1265 case PROP_TEAROFF_TITLE:
1266 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1269 case PROP_POPUP_SHOWN:
1270 g_value_set_boolean (value, combo_box->priv->popup_shown);
1273 case PROP_BUTTON_SENSITIVITY:
1274 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1277 case PROP_POPUP_FIXED_WIDTH:
1278 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1281 case PROP_EDITING_CANCELED:
1282 g_value_set_boolean (value, priv->editing_canceled);
1285 case PROP_HAS_ENTRY:
1286 g_value_set_boolean (value, priv->has_entry);
1289 case PROP_ENTRY_TEXT_COLUMN:
1290 g_value_set_int (value, priv->text_column);
1293 case PROP_ID_COLUMN:
1294 g_value_set_int (value, priv->id_column);
1297 case PROP_ACTIVE_ID:
1298 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1308 gtk_combo_box_state_changed (GtkWidget *widget,
1309 GtkStateType previous)
1311 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1312 GtkComboBoxPrivate *priv = combo_box->priv;
1314 if (gtk_widget_get_realized (widget))
1316 if (priv->tree_view && priv->cell_view)
1317 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1318 >k_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
1321 gtk_widget_queue_draw (widget);
1325 gtk_combo_box_button_state_changed (GtkWidget *widget,
1326 GtkStateType previous,
1329 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1330 GtkComboBoxPrivate *priv = combo_box->priv;
1332 if (gtk_widget_get_realized (widget))
1334 if (!priv->tree_view && priv->cell_view)
1336 if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) !=
1337 (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE))
1338 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1340 gtk_widget_set_state (priv->cell_view,
1341 gtk_widget_get_state (widget));
1345 gtk_widget_queue_draw (widget);
1349 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1351 GtkComboBoxPrivate *priv = combo_box->priv;
1352 gboolean appears_as_list;
1354 /* if wrap_width > 0, then we are in grid-mode and forced to use
1357 if (priv->wrap_width)
1358 appears_as_list = FALSE;
1360 gtk_widget_style_get (GTK_WIDGET (combo_box),
1361 "appears-as-list", &appears_as_list,
1364 if (appears_as_list)
1366 /* Destroy all the menu mode widgets, if they exist. */
1367 if (GTK_IS_MENU (priv->popup_widget))
1368 gtk_combo_box_menu_destroy (combo_box);
1370 /* Create the list mode widgets, if they don't already exist. */
1371 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1372 gtk_combo_box_list_setup (combo_box);
1376 /* Destroy all the list mode widgets, if they exist. */
1377 if (GTK_IS_TREE_VIEW (priv->tree_view))
1378 gtk_combo_box_list_destroy (combo_box);
1380 /* Create the menu mode widgets, if they don't already exist. */
1381 if (!GTK_IS_MENU (priv->popup_widget))
1382 gtk_combo_box_menu_setup (combo_box, TRUE);
1385 gtk_widget_style_get (GTK_WIDGET (combo_box),
1386 "shadow-type", &priv->shadow_type,
1391 gtk_combo_box_style_set (GtkWidget *widget,
1394 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1395 GtkComboBoxPrivate *priv = combo_box->priv;
1398 gtk_combo_box_check_appearance (combo_box);
1400 if (priv->tree_view && priv->cell_view)
1401 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1402 >k_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
1404 child = gtk_bin_get_child (GTK_BIN (combo_box));
1405 if (GTK_IS_ENTRY (child))
1406 g_object_set (child, "shadow-type",
1407 GTK_SHADOW_NONE == priv->shadow_type ?
1408 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1412 gtk_combo_box_button_toggled (GtkWidget *widget,
1415 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1417 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1419 if (!combo_box->priv->popup_in_progress)
1420 gtk_combo_box_popup (combo_box);
1423 gtk_combo_box_popdown (combo_box);
1427 gtk_combo_box_add (GtkContainer *container,
1430 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1431 GtkComboBoxPrivate *priv = combo_box->priv;
1433 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1435 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1436 "(need an instance of GtkEntry or of a subclass)",
1437 G_OBJECT_TYPE_NAME (widget));
1441 if (priv->cell_view &&
1442 gtk_widget_get_parent (priv->cell_view))
1444 gtk_widget_unparent (priv->cell_view);
1445 _gtk_bin_set_child (GTK_BIN (container), NULL);
1446 gtk_widget_queue_resize (GTK_WIDGET (container));
1449 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1450 _gtk_bin_set_child (GTK_BIN (container), widget);
1452 if (priv->cell_view &&
1453 widget != priv->cell_view)
1455 /* since the cell_view was unparented, it's gone now */
1456 priv->cell_view = NULL;
1458 if (!priv->tree_view && priv->separator)
1460 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1462 priv->separator = NULL;
1464 gtk_widget_queue_resize (GTK_WIDGET (container));
1466 else if (priv->cell_view_frame)
1468 gtk_widget_unparent (priv->cell_view_frame);
1469 priv->cell_view_frame = NULL;
1474 if (priv->has_entry)
1476 g_signal_connect (widget, "changed",
1477 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1480 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1485 gtk_combo_box_remove (GtkContainer *container,
1488 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1489 GtkComboBoxPrivate *priv = combo_box->priv;
1491 gboolean appears_as_list;
1493 if (priv->has_entry)
1495 GtkWidget *child_widget;
1497 child_widget = gtk_bin_get_child (GTK_BIN (container));
1498 if (widget && widget == child_widget)
1500 g_signal_handlers_disconnect_by_func (widget,
1501 gtk_combo_box_entry_contents_changed,
1506 if (widget == priv->cell_view)
1507 priv->cell_view = NULL;
1509 gtk_widget_unparent (widget);
1510 _gtk_bin_set_child (GTK_BIN (container), NULL);
1512 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1515 gtk_widget_queue_resize (GTK_WIDGET (container));
1517 if (!priv->tree_view)
1518 appears_as_list = FALSE;
1520 appears_as_list = TRUE;
1522 if (appears_as_list)
1523 gtk_combo_box_list_destroy (combo_box);
1524 else if (GTK_IS_MENU (priv->popup_widget))
1526 gtk_combo_box_menu_destroy (combo_box);
1527 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1528 priv->popup_widget = NULL;
1531 if (!priv->cell_view)
1533 priv->cell_view = gtk_cell_view_new ();
1534 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1535 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1537 gtk_widget_show (priv->cell_view);
1538 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1540 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1544 if (appears_as_list)
1545 gtk_combo_box_list_setup (combo_box);
1547 gtk_combo_box_menu_setup (combo_box, TRUE);
1549 if (gtk_tree_row_reference_valid (priv->active_row))
1551 path = gtk_tree_row_reference_get_path (priv->active_row);
1552 gtk_combo_box_set_active_internal (combo_box, path);
1553 gtk_tree_path_free (path);
1556 gtk_combo_box_set_active_internal (combo_box, NULL);
1559 static ComboCellInfo *
1560 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1561 GtkCellRenderer *cell)
1565 for (i = combo_box->priv->cells; i; i = i->next)
1567 ComboCellInfo *info = (ComboCellInfo *)i->data;
1569 if (info && info->cell == cell)
1577 gtk_combo_box_menu_show (GtkWidget *menu,
1580 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1581 GtkComboBoxPrivate *priv = combo_box->priv;
1583 gtk_combo_box_child_show (menu, user_data);
1585 priv->popup_in_progress = TRUE;
1586 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1588 priv->popup_in_progress = FALSE;
1592 gtk_combo_box_menu_hide (GtkWidget *menu,
1595 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1597 gtk_combo_box_child_hide (menu,user_data);
1599 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1604 gtk_combo_box_detacher (GtkWidget *widget,
1607 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1608 GtkComboBoxPrivate *priv = combo_box->priv;
1610 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1612 g_signal_handlers_disconnect_by_func (menu->toplevel,
1613 gtk_combo_box_menu_show,
1615 g_signal_handlers_disconnect_by_func (menu->toplevel,
1616 gtk_combo_box_menu_hide,
1619 priv->popup_widget = NULL;
1623 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1626 GtkComboBoxPrivate *priv = combo_box->priv;
1628 if (GTK_IS_MENU (priv->popup_widget))
1630 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1631 priv->popup_widget = NULL;
1633 else if (priv->popup_widget)
1635 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1636 priv->popup_widget);
1637 g_object_unref (priv->popup_widget);
1638 priv->popup_widget = NULL;
1641 if (GTK_IS_MENU (popup))
1643 if (priv->popup_window)
1645 gtk_widget_destroy (priv->popup_window);
1646 priv->popup_window = NULL;
1649 priv->popup_widget = popup;
1652 * Note that we connect to show/hide on the toplevel, not the
1653 * menu itself, since the menu is not shown/hidden when it is
1654 * popped up while torn-off.
1656 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1657 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1658 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1659 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1661 gtk_menu_attach_to_widget (GTK_MENU (popup),
1662 GTK_WIDGET (combo_box),
1663 gtk_combo_box_detacher);
1667 if (!priv->popup_window)
1669 GtkWidget *toplevel;
1671 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1672 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1674 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1675 GDK_WINDOW_TYPE_HINT_COMBO);
1677 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1678 G_CALLBACK (gtk_combo_box_child_show),
1680 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1681 G_CALLBACK (gtk_combo_box_child_hide),
1684 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1685 if (GTK_IS_WINDOW (toplevel))
1687 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1688 GTK_WINDOW (priv->popup_window));
1689 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1690 GTK_WINDOW (toplevel));
1693 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1694 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1695 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1697 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1699 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1702 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1705 gtk_widget_show (priv->scrolled_window);
1707 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1708 priv->scrolled_window);
1711 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1714 gtk_widget_show (popup);
1715 g_object_ref (popup);
1716 priv->popup_widget = popup;
1721 gtk_combo_box_menu_position_below (GtkMenu *menu,
1727 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1728 GtkAllocation child_allocation;
1734 GdkRectangle monitor;
1736 /* FIXME: is using the size request here broken? */
1737 child = gtk_bin_get_child (GTK_BIN (combo_box));
1741 gtk_widget_get_allocation (child, &child_allocation);
1743 if (!gtk_widget_get_has_window (child))
1745 sx += child_allocation.x;
1746 sy += child_allocation.y;
1749 gdk_window_get_root_coords (gtk_widget_get_window (child),
1752 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1753 sx -= gtk_widget_get_style (GTK_WIDGET (combo_box))->xthickness;
1755 if (combo_box->priv->popup_fixed_width)
1756 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1758 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1760 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1763 *x = sx + child_allocation.width - req.width;
1766 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1767 monitor_num = gdk_screen_get_monitor_at_window (screen,
1768 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1769 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1773 else if (*x + req.width > monitor.x + monitor.width)
1774 *x = monitor.x + monitor.width - req.width;
1776 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1777 *y += child_allocation.height;
1778 else if (*y - monitor.y >= req.height)
1780 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1781 *y += child_allocation.height;
1789 gtk_combo_box_menu_position_over (GtkMenu *menu,
1795 GtkComboBox *combo_box;
1799 GtkAllocation allocation;
1800 GtkAllocation child_allocation;
1807 combo_box = GTK_COMBO_BOX (user_data);
1808 widget = GTK_WIDGET (combo_box);
1810 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1812 gtk_widget_get_allocation (widget, &allocation);
1814 menu_xpos = allocation.x;
1815 menu_ypos = allocation.y + allocation.height / 2 - 2;
1817 if (combo_box->priv->popup_fixed_width)
1818 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1820 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1824 gtk_widget_get_allocation (active, &child_allocation);
1825 menu_ypos -= child_allocation.height / 2;
1828 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1831 child = children->data;
1833 if (active == child)
1836 if (gtk_widget_get_visible (child))
1838 gtk_widget_get_allocation (child, &child_allocation);
1840 menu_ypos -= child_allocation.height;
1843 children = children->next;
1846 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1847 menu_xpos = menu_xpos + allocation.width - menu_width;
1849 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1850 menu_xpos, menu_ypos,
1851 &menu_xpos, &menu_ypos);
1853 /* Clamp the position on screen */
1854 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1858 else if ((menu_xpos + menu_width) > screen_width)
1859 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1868 gtk_combo_box_menu_position (GtkMenu *menu,
1874 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1875 GtkComboBoxPrivate *priv = combo_box->priv;
1876 GtkWidget *menu_item;
1878 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1879 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1882 /* FIXME handle nested menus better */
1883 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1885 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1888 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1891 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1892 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1893 GDK_WINDOW_TYPE_HINT_COMBO);
1897 gtk_combo_box_list_position (GtkComboBox *combo_box,
1903 GtkComboBoxPrivate *priv = combo_box->priv;
1904 GtkAllocation allocation;
1907 GdkRectangle monitor;
1908 GtkRequisition popup_req;
1909 GtkPolicyType hpolicy, vpolicy;
1912 /* under windows, the drop down list is as wide as the combo box itself.
1914 GtkWidget *widget = GTK_WIDGET (combo_box);
1918 gtk_widget_get_allocation (widget, &allocation);
1920 if (!gtk_widget_get_has_window (widget))
1926 window = gtk_widget_get_window (widget);
1928 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1931 *width = allocation.width;
1933 hpolicy = vpolicy = GTK_POLICY_NEVER;
1934 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1937 /* XXX This set_size_request call is part of the hack outlined below and can
1938 * go away once height-for-width is implemented on treeviews. */
1939 gtk_widget_set_size_request (priv->tree_view, -1, -1);
1941 if (combo_box->priv->popup_fixed_width)
1943 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1945 if (popup_req.width > *width)
1947 hpolicy = GTK_POLICY_ALWAYS;
1948 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1954 gtk_combo_box_remeasure (combo_box);
1956 if (priv->natural_width > *width)
1958 hpolicy = GTK_POLICY_NEVER;
1959 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1963 /* XXX Currently we set the size-request on the internal treeview to be
1964 * the natural width of the cells, this hack can go away once our
1965 * treeview does height-for-width properly (i.e. just adjust *width
1966 * here to be the natural width request of the scrolled-window).
1968 * I can't tell why the magic number 5 is needed here (i.e. without it
1969 * treeviews are left ellipsizing) , however it this all should be
1970 * removed with height-for-width treeviews.
1972 gtk_widget_set_size_request (priv->tree_view, priv->natural_width + 5, -1);
1973 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1975 *width = popup_req.width;
1979 *height = popup_req.height;
1981 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1982 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1983 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1985 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1986 *x = *x + allocation.width - *width;
1990 else if (*x + *width > monitor.x + monitor.width)
1991 *x = monitor.x + monitor.width - *width;
1993 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1994 *y += allocation.height;
1995 else if (*y - *height >= monitor.y)
1997 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1999 *y += allocation.height;
2000 *height = monitor.y + monitor.height - *y;
2004 *height = *y - monitor.y;
2008 if (popup_req.height > *height)
2010 vpolicy = GTK_POLICY_ALWAYS;
2012 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2018 cell_view_is_sensitive (GtkCellView *cell_view)
2020 GList *cells, *list;
2023 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
2026 for (list = cells; list; list = list->next)
2028 g_object_get (list->data, "sensitive", &sensitive, NULL);
2033 g_list_free (cells);
2039 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2042 GtkComboBoxPrivate *priv = combo_box->priv;
2043 GList *cells, *list;
2049 if (priv->row_separator_func)
2051 if (priv->row_separator_func (priv->model, iter,
2052 priv->row_separator_data))
2056 gtk_tree_view_column_cell_set_cell_data (priv->column,
2058 iter, FALSE, FALSE);
2060 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
2063 for (list = cells; list; list = list->next)
2065 g_object_get (list->data, "sensitive", &sensitive, NULL);
2070 g_list_free (cells);
2076 update_menu_sensitivity (GtkComboBox *combo_box,
2079 GtkComboBoxPrivate *priv = combo_box->priv;
2080 GList *children, *child;
2081 GtkWidget *item, *submenu, *separator;
2082 GtkWidget *cell_view;
2088 children = gtk_container_get_children (GTK_CONTAINER (menu));
2090 for (child = children; child; child = child->next)
2092 item = GTK_WIDGET (child->data);
2093 cell_view = gtk_bin_get_child (GTK_BIN (item));
2095 if (!GTK_IS_CELL_VIEW (cell_view))
2098 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2099 if (submenu != NULL)
2101 gtk_widget_set_sensitive (item, TRUE);
2102 update_menu_sensitivity (combo_box, submenu);
2106 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
2108 if (menu != priv->popup_widget && child == children)
2110 separator = GTK_WIDGET (child->next->data);
2111 g_object_set (item, "visible", sensitive, NULL);
2112 g_object_set (separator, "visible", sensitive, NULL);
2115 gtk_widget_set_sensitive (item, sensitive);
2119 g_list_free (children);
2123 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2125 guint32 activate_time)
2127 GtkComboBoxPrivate *priv = combo_box->priv;
2130 gint width, min_width, nat_width;
2132 update_menu_sensitivity (combo_box, priv->popup_widget);
2135 if (gtk_tree_row_reference_valid (priv->active_row))
2137 path = gtk_tree_row_reference_get_path (priv->active_row);
2138 active_item = gtk_tree_path_get_indices (path)[0];
2139 gtk_tree_path_free (path);
2141 if (priv->add_tearoffs)
2145 /* FIXME handle nested menus better */
2146 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2148 if (priv->wrap_width == 0)
2150 GtkAllocation allocation;
2152 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2153 width = allocation.width;
2154 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2155 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2157 if (combo_box->priv->popup_fixed_width)
2158 width = MAX (width, min_width);
2160 width = MAX (width, nat_width);
2162 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2165 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2167 gtk_combo_box_menu_position, combo_box,
2168 button, activate_time);
2172 popup_grab_on_window (GdkWindow *window,
2173 GdkDevice *keyboard,
2175 guint32 activate_time)
2178 gdk_device_grab (keyboard, window,
2179 GDK_OWNERSHIP_WINDOW, TRUE,
2180 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2181 NULL, activate_time) != GDK_GRAB_SUCCESS)
2185 gdk_device_grab (pointer, window,
2186 GDK_OWNERSHIP_WINDOW, TRUE,
2187 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2188 GDK_POINTER_MOTION_MASK,
2189 NULL, activate_time) != GDK_GRAB_SUCCESS)
2192 gdk_device_ungrab (keyboard, activate_time);
2201 * gtk_combo_box_popup:
2202 * @combo_box: a #GtkComboBox
2204 * Pops up the menu or dropdown list of @combo_box.
2206 * This function is mostly intended for use by accessibility technologies;
2207 * applications should have little use for it.
2212 gtk_combo_box_popup (GtkComboBox *combo_box)
2214 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2216 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2220 * gtk_combo_box_popup_for_device:
2221 * @combo_box: a #GtkComboBox
2222 * @device: a #GdkDevice
2224 * Pops up the menu or dropdown list of @combo_box, the popup window
2225 * will be grabbed so only @device and its associated pointer/keyboard
2226 * are the only #GdkDevice<!-- -->s able to send events to it.
2231 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2234 GtkComboBoxPrivate *priv = combo_box->priv;
2235 gint x, y, width, height;
2236 GtkTreePath *path = NULL, *ppath;
2237 GtkWidget *toplevel;
2238 GdkDevice *keyboard, *pointer;
2241 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2242 g_return_if_fail (GDK_IS_DEVICE (device));
2244 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2247 if (gtk_widget_get_mapped (priv->popup_widget))
2250 if (priv->grab_pointer && priv->grab_keyboard)
2253 time = gtk_get_current_event_time ();
2255 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2258 pointer = gdk_device_get_associated_device (device);
2263 keyboard = gdk_device_get_associated_device (device);
2266 if (GTK_IS_MENU (priv->popup_widget))
2268 gtk_combo_box_menu_popup (combo_box,
2269 priv->activate_button,
2270 priv->activate_time);
2274 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2275 if (GTK_IS_WINDOW (toplevel))
2276 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2277 GTK_WINDOW (priv->popup_window));
2279 gtk_widget_show_all (priv->scrolled_window);
2280 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2282 gtk_widget_set_size_request (priv->popup_window, width, height);
2283 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2285 if (gtk_tree_row_reference_valid (priv->active_row))
2287 path = gtk_tree_row_reference_get_path (priv->active_row);
2288 ppath = gtk_tree_path_copy (path);
2289 if (gtk_tree_path_up (ppath))
2290 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2292 gtk_tree_path_free (ppath);
2294 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2298 gtk_widget_show (priv->popup_window);
2302 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2304 gtk_tree_path_free (path);
2307 gtk_widget_grab_focus (priv->popup_window);
2308 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2311 if (!gtk_widget_has_focus (priv->tree_view))
2312 gtk_widget_grab_focus (priv->tree_view);
2314 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2315 keyboard, pointer, time))
2317 gtk_widget_hide (priv->popup_window);
2321 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2322 priv->grab_pointer = pointer;
2323 priv->grab_keyboard = keyboard;
2327 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2331 device = gtk_get_current_event_device ();
2335 GdkDeviceManager *device_manager;
2336 GdkDisplay *display;
2339 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2340 device_manager = gdk_display_get_device_manager (display);
2342 /* No device was set, pick the first master device */
2343 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2344 device = devices->data;
2345 g_list_free (devices);
2348 gtk_combo_box_popup_for_device (combo_box, device);
2352 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2354 if (combo_box->priv->popup_shown)
2356 gtk_combo_box_popdown (combo_box);
2364 * gtk_combo_box_popdown:
2365 * @combo_box: a #GtkComboBox
2367 * Hides the menu or dropdown list of @combo_box.
2369 * This function is mostly intended for use by accessibility technologies;
2370 * applications should have little use for it.
2375 gtk_combo_box_popdown (GtkComboBox *combo_box)
2377 GtkComboBoxPrivate *priv = combo_box->priv;
2379 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2381 if (GTK_IS_MENU (priv->popup_widget))
2383 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2387 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2390 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2391 gtk_widget_hide (priv->popup_window);
2392 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2395 priv->grab_pointer = NULL;
2396 priv->grab_keyboard = NULL;
2400 gtk_combo_box_update_requested_width (GtkComboBox *combo_box,
2403 GtkComboBoxPrivate *priv = combo_box->priv;
2404 gint padding, min_width, nat_width;
2406 if (priv->cell_view)
2407 gtk_widget_style_get (priv->cell_view,
2408 "focus-line-width", &padding,
2413 /* add some pixels for good measure */
2414 padding += BONUS_PADDING;
2416 if (priv->cell_view)
2417 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
2418 path, &min_width, &nat_width);
2420 min_width = nat_width = 0;
2422 min_width += padding;
2423 nat_width += padding;
2425 if (min_width > priv->minimum_width || nat_width > priv->natural_width)
2427 priv->minimum_width = MAX (priv->minimum_width, min_width);
2428 priv->natural_width = MAX (priv->natural_width, nat_width);
2430 if (priv->cell_view)
2432 gtk_widget_set_size_request (priv->cell_view, min_width, -1);
2433 gtk_widget_queue_resize (priv->cell_view);
2438 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2439 gtk_widget_get_preferred_size (combo_box->priv->button, \
2443 child.x = allocation->x + shadow_width; \
2445 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2447 child.y = allocation->y + shadow_height; \
2448 child.width = req.width; \
2449 child.height = allocation->height - 2 * shadow_height; \
2450 child.width = MAX (1, child.width); \
2451 child.height = MAX (1, child.height); \
2453 gtk_widget_size_allocate (combo_box->priv->button, &child);
2456 gtk_combo_box_size_allocate (GtkWidget *widget,
2457 GtkAllocation *allocation)
2459 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2460 GtkComboBoxPrivate *priv = combo_box->priv;
2461 GtkWidget *child_widget;
2462 gint shadow_width, shadow_height;
2463 gint focus_width, focus_pad;
2464 GtkAllocation child;
2467 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2469 gtk_widget_set_allocation (widget, allocation);
2470 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2472 style = gtk_widget_get_style (widget);
2473 gtk_widget_style_get (widget,
2474 "focus-line-width", &focus_width,
2475 "focus-padding", &focus_pad,
2478 if (GTK_SHADOW_NONE != priv->shadow_type)
2480 shadow_width = style->xthickness;
2481 shadow_height = style->ythickness;
2489 if (!priv->tree_view)
2491 if (priv->cell_view)
2493 gint xthickness, ythickness;
2498 allocation->x += shadow_width;
2499 allocation->y += shadow_height;
2500 allocation->width -= 2 * shadow_width;
2501 allocation->height -= 2 * shadow_height;
2503 gtk_widget_size_allocate (priv->button, allocation);
2505 /* set some things ready */
2506 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2507 style = gtk_widget_get_style (priv->button);
2508 xthickness = style->xthickness;
2509 ythickness = style->ythickness;
2511 child.x = allocation->x;
2512 child.y = allocation->y;
2513 width = allocation->width;
2514 child.height = allocation->height;
2516 if (!priv->is_cell_renderer)
2518 child.x += border_width + xthickness + focus_width + focus_pad;
2519 child.y += border_width + ythickness + focus_width + focus_pad;
2520 width -= 2 * (child.x - allocation->x);
2521 child.height -= 2 * (child.y - allocation->y);
2525 /* handle the children */
2526 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2527 child.width = req.width;
2529 child.x += width - req.width;
2530 child.width = MAX (1, child.width);
2531 child.height = MAX (1, child.height);
2532 gtk_widget_size_allocate (priv->arrow, &child);
2534 child.x += req.width;
2535 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2536 child.width = req.width;
2538 child.x -= req.width;
2539 child.width = MAX (1, child.width);
2540 child.height = MAX (1, child.height);
2541 gtk_widget_size_allocate (priv->separator, &child);
2545 child.x += req.width;
2546 child.width = allocation->x + allocation->width
2547 - (border_width + xthickness + focus_width + focus_pad)
2552 child.width = child.x;
2553 child.x = allocation->x
2554 + border_width + xthickness + focus_width + focus_pad;
2555 child.width -= child.x;
2558 if (gtk_widget_get_visible (priv->popup_widget))
2560 gint width, menu_width;
2562 if (priv->wrap_width == 0)
2564 GtkAllocation combo_box_allocation;
2566 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2567 width = combo_box_allocation.width;
2568 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2570 if (combo_box->priv->popup_fixed_width)
2571 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2573 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2575 gtk_widget_set_size_request (priv->popup_widget,
2576 MAX (width, menu_width), -1);
2579 /* reposition the menu after giving it a new width */
2580 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2583 child.width = MAX (1, child.width);
2584 child.height = MAX (1, child.height);
2585 gtk_widget_size_allocate (child_widget, &child);
2589 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2592 child.x = allocation->x + req.width + shadow_width;
2594 child.x = allocation->x + shadow_width;
2595 child.y = allocation->y + shadow_height;
2596 child.width = allocation->width - req.width - 2 * shadow_width;
2597 child.width = MAX (1, child.width);
2598 child.height = MAX (1, child.height);
2599 gtk_widget_size_allocate (child_widget, &child);
2606 /* Combobox thickness + border-width */
2607 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2608 int delta_x = shadow_width + border_width;
2609 int delta_y = shadow_height + border_width;
2612 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2616 child.x = allocation->x + req.width;
2618 child.x = allocation->x;
2620 child.y = allocation->y;
2621 child.width = allocation->width - req.width;
2622 child.height = allocation->height;
2624 if (priv->cell_view_frame)
2628 child.width = MAX (1, child.width - delta_x * 2);
2629 child.height = MAX (1, child.height - delta_y * 2);
2630 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2633 if (priv->has_frame)
2635 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2636 style = gtk_widget_get_style (priv->cell_view_frame);
2637 delta_x = border_width + style->xthickness;
2638 delta_y = border_width + style->ythickness;
2642 child.width -= delta_x * 2;
2643 child.height -= delta_y * 2;
2650 child.width -= delta_x * 2;
2651 child.height -= delta_y * 2;
2654 if (gtk_widget_get_visible (priv->popup_window))
2656 gint x, y, width, height;
2657 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2658 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2659 gtk_widget_set_size_request (priv->popup_window, width, height);
2663 child.width = MAX (1, child.width);
2664 child.height = MAX (1, child.height);
2666 gtk_widget_size_allocate (child_widget, &child);
2670 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2673 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2675 GtkComboBoxPrivate *priv = combo_box->priv;
2679 g_signal_handler_disconnect (priv->model,
2681 g_signal_handler_disconnect (priv->model,
2683 g_signal_handler_disconnect (priv->model,
2684 priv->reordered_id);
2685 g_signal_handler_disconnect (priv->model,
2690 if (!priv->tree_view)
2692 if (priv->popup_widget)
2693 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2694 (GtkCallback)gtk_widget_destroy, NULL);
2699 g_object_unref (priv->model);
2703 if (priv->active_row)
2705 gtk_tree_row_reference_free (priv->active_row);
2706 priv->active_row = NULL;
2709 if (priv->cell_view)
2710 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2714 gtk_combo_box_forall (GtkContainer *container,
2715 gboolean include_internals,
2716 GtkCallback callback,
2717 gpointer callback_data)
2719 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2720 GtkComboBoxPrivate *priv = combo_box->priv;
2723 if (include_internals)
2726 (* callback) (priv->button, callback_data);
2727 if (priv->cell_view_frame)
2728 (* callback) (priv->cell_view_frame, callback_data);
2731 child = gtk_bin_get_child (GTK_BIN (container));
2733 (* callback) (child, callback_data);
2737 gtk_combo_box_child_show (GtkWidget *widget,
2738 GtkComboBox *combo_box)
2740 GtkComboBoxPrivate *priv = combo_box->priv;
2742 priv->popup_shown = TRUE;
2743 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2747 gtk_combo_box_child_hide (GtkWidget *widget,
2748 GtkComboBox *combo_box)
2750 GtkComboBoxPrivate *priv = combo_box->priv;
2752 priv->popup_shown = FALSE;
2753 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2757 gtk_combo_box_draw (GtkWidget *widget,
2760 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2761 GtkComboBoxPrivate *priv = combo_box->priv;
2763 if (priv->shadow_type != GTK_SHADOW_NONE)
2765 gtk_paint_shadow (gtk_widget_get_style (widget),
2767 GTK_STATE_NORMAL, priv->shadow_type,
2770 gtk_widget_get_allocated_width (widget),
2771 gtk_widget_get_allocated_height (widget));
2774 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2777 if (priv->tree_view && priv->cell_view_frame)
2779 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2780 priv->cell_view_frame, cr);
2783 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2784 gtk_bin_get_child (GTK_BIN (widget)),
2800 path_visible (GtkTreeView *view,
2806 /* Note that we rely on the fact that collapsed rows don't have nodes
2808 return _gtk_tree_view_find_node (view, path, &tree, &node);
2812 tree_next_func (GtkTreeModel *model,
2817 SearchData *search_data = (SearchData *)data;
2819 if (search_data->found)
2821 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2824 if (search_data->visible &&
2825 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2828 search_data->set = TRUE;
2829 search_data->iter = *iter;
2834 if (gtk_tree_path_compare (path, search_data->path) == 0)
2835 search_data->found = TRUE;
2841 tree_next (GtkComboBox *combo,
2842 GtkTreeModel *model,
2847 SearchData search_data;
2849 search_data.combo = combo;
2850 search_data.path = gtk_tree_model_get_path (model, iter);
2851 search_data.visible = visible;
2852 search_data.found = FALSE;
2853 search_data.set = FALSE;
2855 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2857 *next = search_data.iter;
2859 gtk_tree_path_free (search_data.path);
2861 return search_data.set;
2865 tree_prev_func (GtkTreeModel *model,
2870 SearchData *search_data = (SearchData *)data;
2872 if (gtk_tree_path_compare (path, search_data->path) == 0)
2874 search_data->found = TRUE;
2878 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2881 if (search_data->visible &&
2882 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2885 search_data->set = TRUE;
2886 search_data->iter = *iter;
2892 tree_prev (GtkComboBox *combo,
2893 GtkTreeModel *model,
2898 SearchData search_data;
2900 search_data.combo = combo;
2901 search_data.path = gtk_tree_model_get_path (model, iter);
2902 search_data.visible = visible;
2903 search_data.found = FALSE;
2904 search_data.set = FALSE;
2906 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2908 *prev = search_data.iter;
2910 gtk_tree_path_free (search_data.path);
2912 return search_data.set;
2916 tree_last_func (GtkTreeModel *model,
2921 SearchData *search_data = (SearchData *)data;
2923 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2926 /* Note that we rely on the fact that collapsed rows don't have nodes
2928 if (search_data->visible &&
2929 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2932 search_data->set = TRUE;
2933 search_data->iter = *iter;
2939 tree_last (GtkComboBox *combo,
2940 GtkTreeModel *model,
2944 SearchData search_data;
2946 search_data.combo = combo;
2947 search_data.visible = visible;
2948 search_data.set = FALSE;
2950 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2952 *last = search_data.iter;
2954 return search_data.set;
2959 tree_first_func (GtkTreeModel *model,
2964 SearchData *search_data = (SearchData *)data;
2966 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2969 if (search_data->visible &&
2970 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2973 search_data->set = TRUE;
2974 search_data->iter = *iter;
2980 tree_first (GtkComboBox *combo,
2981 GtkTreeModel *model,
2985 SearchData search_data;
2987 search_data.combo = combo;
2988 search_data.visible = visible;
2989 search_data.set = FALSE;
2991 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2993 *first = search_data.iter;
2995 return search_data.set;
2999 gtk_combo_box_scroll_event (GtkWidget *widget,
3000 GdkEventScroll *event)
3002 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3005 GtkTreeIter new_iter;
3007 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3010 if (event->direction == GDK_SCROLL_UP)
3011 found = tree_prev (combo_box, combo_box->priv->model,
3012 &iter, &new_iter, FALSE);
3014 found = tree_next (combo_box, combo_box->priv->model,
3015 &iter, &new_iter, FALSE);
3018 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3028 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
3029 GtkCellLayout *cell_layout)
3031 GtkComboBoxPrivate *priv = combo_box->priv;
3034 for (k = priv->cells; k; k = k->next)
3037 ComboCellInfo *info = (ComboCellInfo *)k->data;
3039 if (info->pack == GTK_PACK_START)
3040 gtk_cell_layout_pack_start (cell_layout,
3041 info->cell, info->expand);
3042 else if (info->pack == GTK_PACK_END)
3043 gtk_cell_layout_pack_end (cell_layout,
3044 info->cell, info->expand);
3046 gtk_cell_layout_set_cell_data_func (cell_layout,
3048 combo_cell_data_func, info, NULL);
3050 for (j = info->attributes; j; j = j->next->next)
3052 gtk_cell_layout_add_attribute (cell_layout,
3055 GPOINTER_TO_INT (j->next->data));
3061 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3062 gboolean add_children)
3064 GtkComboBoxPrivate *priv = combo_box->priv;
3068 child = gtk_bin_get_child (GTK_BIN (combo_box));
3070 if (priv->cell_view)
3072 priv->button = gtk_toggle_button_new ();
3073 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3074 priv->focus_on_click);
3076 g_signal_connect (priv->button, "toggled",
3077 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3078 gtk_widget_set_parent (priv->button,
3079 gtk_widget_get_parent (child));
3081 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3082 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3084 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3085 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3087 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3088 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3090 gtk_widget_show_all (priv->button);
3094 priv->button = gtk_toggle_button_new ();
3095 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3096 priv->focus_on_click);
3098 g_signal_connect (priv->button, "toggled",
3099 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3100 gtk_widget_set_parent (priv->button,
3101 gtk_widget_get_parent (child));
3103 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3104 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3105 gtk_widget_show_all (priv->button);
3108 g_signal_connect (priv->button, "button-press-event",
3109 G_CALLBACK (gtk_combo_box_menu_button_press),
3111 g_signal_connect (priv->button, "state-changed",
3112 G_CALLBACK (gtk_combo_box_button_state_changed),
3115 /* create our funky menu */
3116 menu = gtk_menu_new ();
3117 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3118 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3120 g_signal_connect (menu, "key-press-event",
3121 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3122 gtk_combo_box_set_popup_widget (combo_box, menu);
3126 gtk_combo_box_menu_fill (combo_box);
3128 /* the column is needed in tree_column_row_is_sensitive() */
3129 priv->column = gtk_tree_view_column_new ();
3130 g_object_ref_sink (priv->column);
3131 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
3133 gtk_combo_box_update_title (combo_box);
3134 gtk_combo_box_update_sensitivity (combo_box);
3138 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
3140 GtkComboBoxPrivate *priv = combo_box->priv;
3146 menu = priv->popup_widget;
3148 if (priv->add_tearoffs)
3150 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
3152 gtk_widget_show (tearoff);
3154 if (priv->wrap_width)
3155 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
3157 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
3160 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
3164 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
3165 GtkTreeModel *model,
3168 GtkWidget *cell_view;
3173 cell_view = gtk_cell_view_new ();
3174 item = gtk_menu_item_new ();
3175 gtk_container_add (GTK_CONTAINER (item), cell_view);
3177 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
3178 path = gtk_tree_model_get_path (model, iter);
3179 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
3180 gtk_tree_path_free (path);
3182 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
3183 gtk_widget_get_preferred_size (cell_view, &req, NULL);
3184 gtk_widget_show (cell_view);
3190 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
3192 GtkTreeIter *parent)
3194 GtkComboBoxPrivate *priv = combo_box->priv;
3195 GtkTreeModel *model = priv->model;
3196 GtkWidget *item, *submenu, *subitem, *separator;
3198 gboolean is_separator;
3203 n_children = gtk_tree_model_iter_n_children (model, parent);
3206 for (i = 0; i < n_children; i++)
3208 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
3210 if (priv->row_separator_func)
3211 is_separator = priv->row_separator_func (priv->model, &iter,
3212 priv->row_separator_data);
3214 is_separator = FALSE;
3218 item = gtk_separator_menu_item_new ();
3219 path = gtk_tree_model_get_path (model, &iter);
3220 g_object_set_data_full (G_OBJECT (item),
3221 I_("gtk-combo-box-item-path"),
3222 gtk_tree_row_reference_new (model, path),
3223 (GDestroyNotify)gtk_tree_row_reference_free);
3224 gtk_tree_path_free (path);
3228 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3229 if (gtk_tree_model_iter_has_child (model, &iter))
3231 submenu = gtk_menu_new ();
3232 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
3233 gtk_widget_show (submenu);
3234 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
3236 /* Ugly - since menus can only activate leafs, we have to
3237 * duplicate the item inside the submenu.
3239 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3240 separator = gtk_separator_menu_item_new ();
3241 gtk_widget_show (subitem);
3242 gtk_widget_show (separator);
3243 g_signal_connect (subitem, "activate",
3244 G_CALLBACK (gtk_combo_box_menu_item_activate),
3246 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
3247 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
3249 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
3251 g_signal_connect (item, "activate",
3252 G_CALLBACK (gtk_combo_box_menu_item_activate),
3256 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3257 if (priv->wrap_width && menu == priv->popup_widget)
3258 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3259 gtk_widget_show (item);
3266 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3268 GtkComboBoxPrivate *priv = combo_box->priv;
3270 g_signal_handlers_disconnect_matched (priv->button,
3271 G_SIGNAL_MATCH_DATA,
3273 gtk_combo_box_menu_button_press, NULL);
3274 g_signal_handlers_disconnect_matched (priv->button,
3275 G_SIGNAL_MATCH_DATA,
3277 gtk_combo_box_button_state_changed, combo_box);
3279 /* unparent will remove our latest ref */
3280 gtk_widget_unparent (priv->button);
3283 priv->button = NULL;
3285 priv->separator = NULL;
3287 g_object_unref (priv->column);
3288 priv->column = NULL;
3290 /* changing the popup window will unref the menu and the children */
3298 menu_occupied (GtkMenu *menu,
3302 guint bottom_attach)
3306 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3310 gtk_container_child_get (GTK_CONTAINER (menu),
3314 "bottom-attach", &b,
3318 /* look if this item intersects with the given coordinates */
3319 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3327 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3332 GtkComboBoxPrivate *priv = combo_box->priv;
3333 gint current_col = 0, current_row = 0;
3334 gint rows = 1, cols = 1;
3335 GtkWidget *menu = priv->popup_widget;
3337 if (!GTK_IS_MENU_SHELL (menu))
3340 if (priv->col_column == -1 &&
3341 priv->row_column == -1 &&
3344 gtk_container_child_get (GTK_CONTAINER (menu),
3346 "right-attach", ¤t_col,
3347 "top-attach", ¤t_row,
3349 if (current_col + cols > priv->wrap_width)
3357 if (priv->col_column != -1)
3358 gtk_tree_model_get (priv->model, iter,
3359 priv->col_column, &cols,
3361 if (priv->row_column != -1)
3362 gtk_tree_model_get (priv->model, iter,
3363 priv->row_column, &rows,
3368 if (current_col + cols > priv->wrap_width)
3374 if (!menu_occupied (GTK_MENU (menu),
3375 current_col, current_col + cols,
3376 current_row, current_row + rows))
3383 /* set attach props */
3384 gtk_menu_attach (GTK_MENU (menu), item,
3385 current_col, current_col + cols,
3386 current_row, current_row + rows);
3390 gtk_combo_box_relayout (GtkComboBox *combo_box)
3395 menu = combo_box->priv->popup_widget;
3397 /* do nothing unless we are in menu style and realized */
3398 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3401 list = gtk_container_get_children (GTK_CONTAINER (menu));
3403 for (j = g_list_last (list); j; j = j->prev)
3404 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3406 gtk_combo_box_menu_fill (combo_box);
3413 gtk_combo_box_menu_button_press (GtkWidget *widget,
3414 GdkEventButton *event,
3417 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3418 GtkComboBoxPrivate *priv = combo_box->priv;
3420 if (GTK_IS_MENU (priv->popup_widget) &&
3421 event->type == GDK_BUTTON_PRESS && event->button == 1)
3423 if (priv->focus_on_click &&
3424 !gtk_widget_has_focus (priv->button))
3425 gtk_widget_grab_focus (priv->button);
3427 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3436 gtk_combo_box_menu_item_activate (GtkWidget *item,
3439 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3440 GtkWidget *cell_view;
3444 cell_view = gtk_bin_get_child (GTK_BIN (item));
3446 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3448 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3450 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3452 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3453 gtk_combo_box_set_active_iter (combo_box, &iter);
3456 gtk_tree_path_free (path);
3458 g_object_set (combo_box,
3459 "editing-canceled", FALSE,
3464 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3467 gboolean sensitive = TRUE; /* fool code checkers */
3469 if (!combo_box->priv->button)
3472 switch (combo_box->priv->button_sensitivity)
3474 case GTK_SENSITIVITY_ON:
3477 case GTK_SENSITIVITY_OFF:
3480 case GTK_SENSITIVITY_AUTO:
3481 sensitive = combo_box->priv->model &&
3482 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3485 g_assert_not_reached ();
3489 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3491 /* In list-mode, we also need to update sensitivity of the event box */
3492 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3493 && combo_box->priv->cell_view)
3494 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3498 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3503 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3505 if (combo_box->priv->tree_view)
3506 gtk_combo_box_list_popup_resize (combo_box);
3508 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3510 gtk_combo_box_update_sensitivity (combo_box);
3514 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3518 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3519 GtkComboBoxPrivate *priv = combo_box->priv;
3521 if (!gtk_tree_row_reference_valid (priv->active_row))
3523 if (priv->cell_view)
3524 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3525 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3528 if (priv->tree_view)
3529 gtk_combo_box_list_popup_resize (combo_box);
3531 gtk_combo_box_menu_row_deleted (model, path, user_data);
3533 gtk_combo_box_update_sensitivity (combo_box);
3537 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3543 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3545 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3547 if (!combo_box->priv->tree_view)
3548 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3552 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3557 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3558 GtkComboBoxPrivate *priv = combo_box->priv;
3559 GtkTreePath *active_path;
3561 /* FIXME this belongs to GtkCellView */
3562 if (gtk_tree_row_reference_valid (priv->active_row))
3564 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3565 if (gtk_tree_path_compare (path, active_path) == 0 &&
3567 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3568 gtk_tree_path_free (active_path);
3571 if (priv->tree_view)
3572 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3574 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3578 list_popup_resize_idle (gpointer user_data)
3580 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3581 GtkComboBoxPrivate *priv = combo_box->priv;
3582 gint x, y, width, height;
3584 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3586 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3588 gtk_widget_set_size_request (priv->popup_window, width, height);
3589 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3592 priv->resize_idle_id = 0;
3598 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3600 GtkComboBoxPrivate *priv = combo_box->priv;
3602 if (!priv->resize_idle_id)
3603 priv->resize_idle_id =
3604 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3608 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3613 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3615 gtk_combo_box_list_popup_resize (combo_box);
3620 find_menu_by_path (GtkWidget *menu,
3622 gboolean skip_first)
3628 GtkTreeRowReference *mref;
3632 list = gtk_container_get_children (GTK_CONTAINER (menu));
3635 for (i = list; i; i = i->next)
3637 child = gtk_bin_get_child (i->data);
3638 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3640 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3643 else if (!gtk_tree_row_reference_valid (mref))
3646 mpath = gtk_tree_row_reference_get_path (mref);
3648 else if (GTK_IS_CELL_VIEW (child))
3656 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (child));
3661 /* this case is necessary, since the row reference of
3662 * the cell view may already be updated after a deletion
3669 if (gtk_tree_path_compare (mpath, path) == 0)
3671 gtk_tree_path_free (mpath);
3675 if (gtk_tree_path_is_ancestor (mpath, path))
3677 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3678 if (submenu != NULL)
3680 gtk_tree_path_free (mpath);
3681 item = find_menu_by_path (submenu, path, TRUE);
3685 gtk_tree_path_free (mpath);
3695 dump_menu_tree (GtkWidget *menu,
3702 list = gtk_container_get_children (GTK_CONTAINER (menu));
3703 for (i = list; i; i = i->next)
3705 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3707 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3708 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3709 gtk_tree_path_free (path);
3711 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3712 if (submenu != NULL)
3713 dump_menu_tree (submenu, level + 1);
3722 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3727 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3728 GtkComboBoxPrivate *priv = combo_box->priv;
3730 GtkWidget *item, *menu, *separator;
3734 gboolean is_separator;
3736 if (!priv->popup_widget)
3739 depth = gtk_tree_path_get_depth (path);
3740 pos = gtk_tree_path_get_indices (path)[depth - 1];
3743 ppath = gtk_tree_path_copy (path);
3744 gtk_tree_path_up (ppath);
3745 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3746 gtk_tree_path_free (ppath);
3748 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3751 menu = gtk_menu_new ();
3752 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3753 gtk_widget_show (menu);
3754 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3756 /* Ugly - since menus can only activate leaves, we have to
3757 * duplicate the item inside the submenu.
3759 gtk_tree_model_iter_parent (model, &piter, iter);
3760 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3761 separator = gtk_separator_menu_item_new ();
3762 g_signal_connect (item, "activate",
3763 G_CALLBACK (gtk_combo_box_menu_item_activate),
3765 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3766 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3767 if (cell_view_is_sensitive (GTK_CELL_VIEW (gtk_bin_get_child (GTK_BIN (item)))))
3769 gtk_widget_show (item);
3770 gtk_widget_show (separator);
3777 menu = priv->popup_widget;
3778 if (priv->add_tearoffs)
3782 if (priv->row_separator_func)
3783 is_separator = priv->row_separator_func (model, iter,
3784 priv->row_separator_data);
3786 is_separator = FALSE;
3790 item = gtk_separator_menu_item_new ();
3791 g_object_set_data_full (G_OBJECT (item),
3792 I_("gtk-combo-box-item-path"),
3793 gtk_tree_row_reference_new (model, path),
3794 (GDestroyNotify)gtk_tree_row_reference_free);
3798 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3800 g_signal_connect (item, "activate",
3801 G_CALLBACK (gtk_combo_box_menu_item_activate),
3805 gtk_widget_show (item);
3806 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3810 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3814 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3815 GtkComboBoxPrivate *priv = combo_box->priv;
3819 if (!priv->popup_widget)
3822 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3823 menu = gtk_widget_get_parent (item);
3824 gtk_container_remove (GTK_CONTAINER (menu), item);
3826 if (gtk_tree_path_get_depth (path) > 1)
3828 GtkTreePath *parent_path;
3832 parent_path = gtk_tree_path_copy (path);
3833 gtk_tree_path_up (parent_path);
3834 gtk_tree_model_get_iter (model, &iter, parent_path);
3836 if (!gtk_tree_model_iter_has_child (model, &iter))
3838 parent = find_menu_by_path (priv->popup_widget,
3839 parent_path, FALSE);
3840 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3846 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3852 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3854 gtk_combo_box_relayout (combo_box);
3858 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3863 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3864 GtkComboBoxPrivate *priv = combo_box->priv;
3866 gboolean is_separator;
3868 if (!priv->popup_widget)
3871 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3873 if (priv->row_separator_func)
3874 is_separator = priv->row_separator_func (model, iter,
3875 priv->row_separator_data);
3877 is_separator = FALSE;
3879 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3881 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3882 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3885 if (priv->wrap_width &&
3886 gtk_widget_get_parent (item) == priv->popup_widget)
3888 GtkWidget *pitem = NULL;
3891 prev = gtk_tree_path_copy (path);
3893 if (gtk_tree_path_prev (prev))
3894 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3896 gtk_tree_path_free (prev);
3898 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3900 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3905 "bottom-attach", -1,
3908 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3911 gtk_combo_box_update_requested_width (combo_box, path);
3919 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3921 GtkComboBoxPrivate *priv = combo_box->priv;
3922 GtkTreeSelection *sel;
3925 GtkWidget *widget = GTK_WIDGET (combo_box);
3927 priv->button = gtk_toggle_button_new ();
3928 child = gtk_bin_get_child (GTK_BIN (combo_box));
3929 gtk_widget_set_parent (priv->button,
3930 gtk_widget_get_parent (child));
3931 g_signal_connect (priv->button, "button-press-event",
3932 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3933 g_signal_connect (priv->button, "toggled",
3934 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3936 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3937 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3938 priv->separator = NULL;
3939 gtk_widget_show_all (priv->button);
3941 if (priv->cell_view)
3943 style = gtk_widget_get_style (widget);
3944 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3945 &style->base[gtk_widget_get_state (widget)]);
3947 priv->box = gtk_event_box_new ();
3948 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3951 if (priv->has_frame)
3953 priv->cell_view_frame = gtk_frame_new (NULL);
3954 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3959 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3960 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3964 gtk_widget_set_parent (priv->cell_view_frame,
3965 gtk_widget_get_parent (child));
3966 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3967 gtk_widget_show_all (priv->cell_view_frame);
3969 g_signal_connect (priv->box, "button-press-event",
3970 G_CALLBACK (gtk_combo_box_list_button_pressed),
3974 priv->tree_view = gtk_tree_view_new ();
3975 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3976 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3977 gtk_tree_selection_set_select_function (sel,
3978 gtk_combo_box_list_select_func,
3980 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3982 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3984 if (priv->row_separator_func)
3985 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3986 priv->row_separator_func,
3987 priv->row_separator_data,
3990 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3992 priv->column = gtk_tree_view_column_new ();
3993 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3996 gtk_combo_box_sync_cells (combo_box,
3997 GTK_CELL_LAYOUT (priv->column));
3999 if (gtk_tree_row_reference_valid (priv->active_row))
4003 path = gtk_tree_row_reference_get_path (priv->active_row);
4004 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4006 gtk_tree_path_free (path);
4009 /* set sample/popup widgets */
4010 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
4012 g_signal_connect (priv->tree_view, "key-press-event",
4013 G_CALLBACK (gtk_combo_box_list_key_press),
4015 g_signal_connect (priv->tree_view, "enter-notify-event",
4016 G_CALLBACK (gtk_combo_box_list_enter_notify),
4018 g_signal_connect (priv->tree_view, "row-expanded",
4019 G_CALLBACK (gtk_combo_box_model_row_expanded),
4021 g_signal_connect (priv->tree_view, "row-collapsed",
4022 G_CALLBACK (gtk_combo_box_model_row_expanded),
4024 g_signal_connect (priv->popup_window, "button-press-event",
4025 G_CALLBACK (gtk_combo_box_list_button_pressed),
4027 g_signal_connect (priv->popup_window, "button-release-event",
4028 G_CALLBACK (gtk_combo_box_list_button_released),
4031 gtk_widget_show (priv->tree_view);
4033 gtk_combo_box_update_sensitivity (combo_box);
4037 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
4039 GtkComboBoxPrivate *priv = combo_box->priv;
4041 /* disconnect signals */
4042 g_signal_handlers_disconnect_matched (priv->tree_view,
4043 G_SIGNAL_MATCH_DATA,
4044 0, 0, NULL, NULL, combo_box);
4045 g_signal_handlers_disconnect_matched (priv->button,
4046 G_SIGNAL_MATCH_DATA,
4048 gtk_combo_box_list_button_pressed,
4050 g_signal_handlers_disconnect_matched (priv->popup_window,
4051 G_SIGNAL_MATCH_DATA,
4053 gtk_combo_box_list_button_pressed,
4055 g_signal_handlers_disconnect_matched (priv->popup_window,
4056 G_SIGNAL_MATCH_DATA,
4058 gtk_combo_box_list_button_released,
4061 g_signal_handlers_disconnect_matched (priv->popup_window,
4062 G_SIGNAL_MATCH_DATA,
4064 gtk_combo_box_child_show,
4067 g_signal_handlers_disconnect_matched (priv->popup_window,
4068 G_SIGNAL_MATCH_DATA,
4070 gtk_combo_box_child_hide,
4074 g_signal_handlers_disconnect_matched (priv->box,
4075 G_SIGNAL_MATCH_DATA,
4077 gtk_combo_box_list_button_pressed,
4080 /* destroy things (unparent will kill the latest ref from us)
4081 * last unref on button will destroy the arrow
4083 gtk_widget_unparent (priv->button);
4084 priv->button = NULL;
4087 if (priv->cell_view)
4089 g_object_set (priv->cell_view,
4090 "background-set", FALSE,
4094 if (priv->cell_view_frame)
4096 gtk_widget_unparent (priv->cell_view_frame);
4097 priv->cell_view_frame = NULL;
4101 if (priv->scroll_timer)
4103 g_source_remove (priv->scroll_timer);
4104 priv->scroll_timer = 0;
4107 if (priv->resize_idle_id)
4109 g_source_remove (priv->resize_idle_id);
4110 priv->resize_idle_id = 0;
4113 gtk_widget_destroy (priv->tree_view);
4115 priv->tree_view = NULL;
4116 if (priv->popup_widget)
4118 g_object_unref (priv->popup_widget);
4119 priv->popup_widget = NULL;
4126 gtk_combo_box_list_button_pressed (GtkWidget *widget,
4127 GdkEventButton *event,
4130 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4131 GtkComboBoxPrivate *priv = combo_box->priv;
4133 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
4135 if (ewidget == priv->popup_window)
4138 if ((ewidget != priv->button && ewidget != priv->box) ||
4139 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4142 if (priv->focus_on_click &&
4143 !gtk_widget_has_focus (priv->button))
4144 gtk_widget_grab_focus (priv->button);
4146 gtk_combo_box_popup_for_device (combo_box, event->device);
4148 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
4150 priv->auto_scroll = FALSE;
4151 if (priv->scroll_timer == 0)
4152 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
4153 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
4156 priv->popup_in_progress = TRUE;
4162 gtk_combo_box_list_button_released (GtkWidget *widget,
4163 GdkEventButton *event,
4167 GtkTreePath *path = NULL;
4170 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4171 GtkComboBoxPrivate *priv = combo_box->priv;
4173 gboolean popup_in_progress = FALSE;
4175 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
4177 if (priv->popup_in_progress)
4179 popup_in_progress = TRUE;
4180 priv->popup_in_progress = FALSE;
4183 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
4185 if (priv->scroll_timer)
4187 g_source_remove (priv->scroll_timer);
4188 priv->scroll_timer = 0;
4191 if (ewidget != priv->tree_view)
4193 if ((ewidget == priv->button ||
4194 ewidget == priv->box) &&
4195 !popup_in_progress &&
4196 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4198 gtk_combo_box_popdown (combo_box);
4202 /* released outside treeview */
4203 if (ewidget != priv->button && ewidget != priv->box)
4205 gtk_combo_box_popdown (combo_box);
4213 /* select something cool */
4214 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
4220 return TRUE; /* clicked outside window? */
4222 gtk_tree_model_get_iter (priv->model, &iter, path);
4223 gtk_tree_path_free (path);
4225 gtk_combo_box_popdown (combo_box);
4227 if (tree_column_row_is_sensitive (combo_box, &iter))
4228 gtk_combo_box_set_active_iter (combo_box, &iter);
4234 gtk_combo_box_menu_key_press (GtkWidget *widget,
4238 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4240 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
4242 /* The menu hasn't managed the
4243 * event, forward it to the combobox
4245 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
4252 gtk_combo_box_list_key_press (GtkWidget *widget,
4256 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4259 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
4260 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
4262 GtkTreeModel *model = NULL;
4264 gtk_combo_box_popdown (combo_box);
4266 if (combo_box->priv->model)
4268 GtkTreeSelection *sel;
4270 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4272 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4273 gtk_combo_box_set_active_iter (combo_box, &iter);
4279 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
4281 /* The list 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_auto_scroll (GtkComboBox *combo_box,
4296 GtkAllocation allocation;
4297 GtkWidget *tree_view = combo_box->priv->tree_view;
4300 gtk_widget_get_allocation (tree_view, &allocation);
4302 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4303 if (adj && adj->upper - adj->lower > adj->page_size)
4305 if (x <= allocation.x &&
4306 adj->lower < adj->value)
4308 value = adj->value - (allocation.x - x + 1);
4309 gtk_adjustment_set_value (adj, value);
4311 else if (x >= allocation.x + allocation.width &&
4312 adj->upper - adj->page_size > adj->value)
4314 value = adj->value + (x - allocation.x - allocation.width + 1);
4315 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4319 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4320 if (adj && adj->upper - adj->lower > adj->page_size)
4322 if (y <= allocation.y &&
4323 adj->lower < adj->value)
4325 value = adj->value - (allocation.y - y + 1);
4326 gtk_adjustment_set_value (adj, value);
4328 else if (y >= allocation.height &&
4329 adj->upper - adj->page_size > adj->value)
4331 value = adj->value + (y - allocation.height + 1);
4332 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4338 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4340 GtkComboBoxPrivate *priv = combo_box->priv;
4343 if (priv->auto_scroll)
4345 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
4348 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4355 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4356 GdkEventCrossing *event,
4359 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4361 combo_box->priv->auto_scroll = TRUE;
4367 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4368 GtkTreeModel *model,
4370 gboolean path_currently_selected,
4374 gboolean sensitive = FALSE;
4376 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4378 GList *cells, *cell;
4379 gboolean cell_sensitive, cell_visible;
4381 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4383 if (!gtk_tree_view_column_get_visible (column))
4386 gtk_tree_model_get_iter (model, &iter, path);
4387 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4390 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4393 g_object_get (cell->data,
4394 "sensitive", &cell_sensitive,
4395 "visible", &cell_visible,
4398 if (cell_visible && cell_sensitive)
4403 g_list_free (cells);
4405 sensitive = cell_sensitive;
4412 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4417 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4419 gtk_combo_box_update_requested_width (combo_box, path);
4423 * GtkCellLayout implementation
4427 pack_start_recurse (GtkWidget *menu,
4428 GtkCellRenderer *cell,
4435 list = gtk_container_get_children (GTK_CONTAINER (menu));
4436 for (i = list; i; i = i->next)
4438 child = gtk_bin_get_child (GTK_BIN (i->data));
4439 if (GTK_IS_CELL_LAYOUT (child))
4440 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (child),
4443 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4444 if (submenu != NULL)
4445 pack_start_recurse (submenu, cell, expand);
4452 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4453 GtkCellRenderer *cell,
4456 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4457 ComboCellInfo *info;
4458 GtkComboBoxPrivate *priv;
4460 priv = combo_box->priv;
4462 g_object_ref_sink (cell);
4464 info = g_slice_new0 (ComboCellInfo);
4466 info->expand = expand;
4467 info->pack = GTK_PACK_START;
4469 priv->cells = g_slist_append (priv->cells, info);
4471 if (priv->cell_view)
4473 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4479 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4481 if (GTK_IS_MENU (priv->popup_widget))
4482 pack_start_recurse (priv->popup_widget, cell, expand);
4486 pack_end_recurse (GtkWidget *menu,
4487 GtkCellRenderer *cell,
4494 list = gtk_container_get_children (GTK_CONTAINER (menu));
4495 for (i = list; i; i = i->next)
4497 child = gtk_bin_get_child (GTK_BIN (i->data));
4498 if (GTK_IS_CELL_LAYOUT (child))
4499 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (child),
4502 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4503 if (submenu != NULL)
4504 pack_end_recurse (submenu, cell, expand);
4511 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4512 GtkCellRenderer *cell,
4515 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4516 ComboCellInfo *info;
4517 GtkComboBoxPrivate *priv;
4519 priv = combo_box->priv;
4521 g_object_ref_sink (cell);
4523 info = g_slice_new0 (ComboCellInfo);
4525 info->expand = expand;
4526 info->pack = GTK_PACK_END;
4528 priv->cells = g_slist_append (priv->cells, info);
4530 if (priv->cell_view)
4531 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4535 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4537 if (GTK_IS_MENU (priv->popup_widget))
4538 pack_end_recurse (priv->popup_widget, cell, expand);
4542 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4544 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4546 GList *retval = NULL;
4548 for (list = combo_box->priv->cells; list; list = list->next)
4550 ComboCellInfo *info = (ComboCellInfo *)list->data;
4552 retval = g_list_prepend (retval, info->cell);
4555 return g_list_reverse (retval);
4559 clear_recurse (GtkWidget *menu)
4565 list = gtk_container_get_children (GTK_CONTAINER (menu));
4566 for (i = list; i; i = i->next)
4568 child = gtk_bin_get_child (GTK_BIN (i->data));
4569 if (GTK_IS_CELL_LAYOUT (child))
4570 gtk_cell_layout_clear (GTK_CELL_LAYOUT (child));
4572 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4573 if (submenu != NULL)
4574 clear_recurse (submenu);
4581 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4583 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4584 GtkComboBoxPrivate *priv = combo_box->priv;
4587 if (priv->cell_view)
4588 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4591 gtk_tree_view_column_clear (priv->column);
4593 for (i = priv->cells; i; i = i->next)
4595 ComboCellInfo *info = (ComboCellInfo *)i->data;
4597 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4598 g_object_unref (info->cell);
4599 g_slice_free (ComboCellInfo, info);
4602 g_slist_free (priv->cells);
4605 if (GTK_IS_MENU (priv->popup_widget))
4606 clear_recurse (priv->popup_widget);
4610 add_attribute_recurse (GtkWidget *menu,
4611 GtkCellRenderer *cell,
4612 const gchar *attribute,
4619 list = gtk_container_get_children (GTK_CONTAINER (menu));
4620 for (i = list; i; i = i->next)
4622 child = gtk_bin_get_child (GTK_BIN (i->data));
4623 if (GTK_IS_CELL_LAYOUT (child))
4624 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (child),
4625 cell, attribute, column);
4627 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4628 if (submenu != NULL)
4629 add_attribute_recurse (submenu, cell, attribute, column);
4636 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4637 GtkCellRenderer *cell,
4638 const gchar *attribute,
4641 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4642 ComboCellInfo *info;
4644 info = gtk_combo_box_get_cell_info (combo_box, cell);
4645 g_return_if_fail (info != NULL);
4647 info->attributes = g_slist_prepend (info->attributes,
4648 GINT_TO_POINTER (column));
4649 info->attributes = g_slist_prepend (info->attributes,
4650 g_strdup (attribute));
4652 if (combo_box->priv->cell_view)
4653 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4654 cell, attribute, column);
4656 if (combo_box->priv->column)
4657 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4658 cell, attribute, column);
4660 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4661 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4662 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4666 combo_cell_data_func (GtkCellLayout *cell_layout,
4667 GtkCellRenderer *cell,
4668 GtkTreeModel *tree_model,
4672 ComboCellInfo *info = (ComboCellInfo *)data;
4673 GtkWidget *parent = NULL;
4678 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4680 if (GTK_IS_WIDGET (cell_layout))
4681 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4683 if (GTK_IS_MENU_ITEM (parent) &&
4684 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4685 g_object_set (cell, "sensitive", TRUE, NULL);
4690 set_cell_data_func_recurse (GtkWidget *menu,
4691 GtkCellRenderer *cell,
4692 ComboCellInfo *info)
4696 GtkWidget *cell_view;
4698 list = gtk_container_get_children (GTK_CONTAINER (menu));
4699 for (i = list; i; i = i->next)
4701 cell_view = gtk_bin_get_child (GTK_BIN (i->data));
4702 if (GTK_IS_CELL_LAYOUT (cell_view))
4704 /* Override sensitivity for inner nodes; we don't
4705 * want menuitems with submenus to appear insensitive
4707 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4709 combo_cell_data_func,
4711 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4712 if (submenu != NULL)
4713 set_cell_data_func_recurse (submenu, cell, info);
4721 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4722 GtkCellRenderer *cell,
4723 GtkCellLayoutDataFunc func,
4725 GDestroyNotify destroy)
4727 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4728 GtkComboBoxPrivate *priv = combo_box->priv;
4729 ComboCellInfo *info;
4731 info = gtk_combo_box_get_cell_info (combo_box, cell);
4732 g_return_if_fail (info != NULL);
4736 GDestroyNotify d = info->destroy;
4738 info->destroy = NULL;
4739 d (info->func_data);
4743 info->func_data = func_data;
4744 info->destroy = destroy;
4746 if (priv->cell_view)
4747 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4750 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4752 if (GTK_IS_MENU (priv->popup_widget))
4753 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4755 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4759 clear_attributes_recurse (GtkWidget *menu,
4760 GtkCellRenderer *cell)
4766 list = gtk_container_get_children (GTK_CONTAINER (menu));
4767 for (i = list; i; i = i->next)
4769 child = gtk_bin_get_child (GTK_BIN (i->data));
4770 if (GTK_IS_CELL_LAYOUT (child))
4771 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (child), cell);
4773 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4774 if (submenu != NULL)
4775 clear_attributes_recurse (submenu, cell);
4782 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4783 GtkCellRenderer *cell)
4785 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4786 GtkComboBoxPrivate *priv;
4787 ComboCellInfo *info;
4790 priv = combo_box->priv;
4792 info = gtk_combo_box_get_cell_info (combo_box, cell);
4793 g_return_if_fail (info != NULL);
4795 list = info->attributes;
4796 while (list && list->next)
4798 g_free (list->data);
4799 list = list->next->next;
4801 g_slist_free (info->attributes);
4802 info->attributes = NULL;
4804 if (priv->cell_view)
4805 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4808 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4810 if (GTK_IS_MENU (priv->popup_widget))
4811 clear_attributes_recurse (priv->popup_widget, cell);
4813 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4817 reorder_recurse (GtkWidget *menu,
4818 GtkCellRenderer *cell,
4825 list = gtk_container_get_children (GTK_CONTAINER (menu));
4826 for (i = list; i; i = i->next)
4828 child = gtk_bin_get_child (GTK_BIN (i->data));
4829 if (GTK_IS_CELL_LAYOUT (child))
4830 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (child),
4833 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4834 if (submenu != NULL)
4835 reorder_recurse (submenu, cell, position);
4842 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4843 GtkCellRenderer *cell,
4846 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4847 GtkComboBoxPrivate *priv;
4848 ComboCellInfo *info;
4851 priv = combo_box->priv;
4853 info = gtk_combo_box_get_cell_info (combo_box, cell);
4855 g_return_if_fail (info != NULL);
4856 g_return_if_fail (position >= 0);
4858 link = g_slist_find (priv->cells, info);
4860 g_return_if_fail (link != NULL);
4862 priv->cells = g_slist_delete_link (priv->cells, link);
4863 priv->cells = g_slist_insert (priv->cells, info, position);
4865 if (priv->cell_view)
4866 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4870 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4873 if (GTK_IS_MENU (priv->popup_widget))
4874 reorder_recurse (priv->popup_widget, cell, position);
4876 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4884 * gtk_combo_box_new:
4886 * Creates a new empty #GtkComboBox.
4888 * Return value: A new #GtkComboBox.
4893 gtk_combo_box_new (void)
4895 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4899 * gtk_combo_box_new_with_entry:
4901 * Creates a new empty #GtkComboBox with an entry.
4903 * Return value: A new #GtkComboBox.
4906 gtk_combo_box_new_with_entry (void)
4908 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
4912 * gtk_combo_box_new_with_model:
4913 * @model: A #GtkTreeModel.
4915 * Creates a new #GtkComboBox with the model initialized to @model.
4917 * Return value: A new #GtkComboBox.
4922 gtk_combo_box_new_with_model (GtkTreeModel *model)
4924 GtkComboBox *combo_box;
4926 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4928 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4930 return GTK_WIDGET (combo_box);
4934 * gtk_combo_box_new_with_model_and_entry:
4935 * @model: A #GtkTreeModel
4937 * Creates a new empty #GtkComboBox with an entry
4938 * and with the model initialized to @model.
4940 * Return value: A new #GtkComboBox
4943 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4945 return g_object_new (GTK_TYPE_COMBO_BOX,
4952 * gtk_combo_box_get_wrap_width:
4953 * @combo_box: A #GtkComboBox
4955 * Returns the wrap width which is used to determine the number of columns
4956 * for the popup menu. If the wrap width is larger than 1, the combo box
4959 * Returns: the wrap width.
4964 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4966 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4968 return combo_box->priv->wrap_width;
4972 * gtk_combo_box_set_wrap_width:
4973 * @combo_box: A #GtkComboBox
4974 * @width: Preferred number of columns
4976 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4977 * the preferred number of columns when you want the popup to be layed out
4983 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4986 GtkComboBoxPrivate *priv;
4988 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4989 g_return_if_fail (width >= 0);
4991 priv = combo_box->priv;
4993 if (width != priv->wrap_width)
4995 priv->wrap_width = width;
4997 gtk_combo_box_check_appearance (combo_box);
4998 gtk_combo_box_relayout (combo_box);
5000 g_object_notify (G_OBJECT (combo_box), "wrap-width");
5005 * gtk_combo_box_get_row_span_column:
5006 * @combo_box: A #GtkComboBox
5008 * Returns the column with row span information for @combo_box.
5010 * Returns: the row span column.
5015 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
5017 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
5019 return combo_box->priv->row_column;
5023 * gtk_combo_box_set_row_span_column:
5024 * @combo_box: A #GtkComboBox.
5025 * @row_span: A column in the model passed during construction.
5027 * Sets the column with row span information for @combo_box to be @row_span.
5028 * The row span column contains integers which indicate how many rows
5029 * an item should span.
5034 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
5037 GtkComboBoxPrivate *priv;
5040 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5042 priv = combo_box->priv;
5044 col = gtk_tree_model_get_n_columns (priv->model);
5045 g_return_if_fail (row_span >= -1 && row_span < col);
5047 if (row_span != priv->row_column)
5049 priv->row_column = row_span;
5051 gtk_combo_box_relayout (combo_box);
5053 g_object_notify (G_OBJECT (combo_box), "row-span-column");
5058 * gtk_combo_box_get_column_span_column:
5059 * @combo_box: A #GtkComboBox
5061 * Returns the column with column span information for @combo_box.
5063 * Returns: the column span column.
5068 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
5070 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
5072 return combo_box->priv->col_column;
5076 * gtk_combo_box_set_column_span_column:
5077 * @combo_box: A #GtkComboBox
5078 * @column_span: A column in the model passed during construction
5080 * Sets the column with column span information for @combo_box to be
5081 * @column_span. The column span column contains integers which indicate
5082 * how many columns an item should span.
5087 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
5090 GtkComboBoxPrivate *priv;
5093 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5095 priv = combo_box->priv;
5097 col = gtk_tree_model_get_n_columns (priv->model);
5098 g_return_if_fail (column_span >= -1 && column_span < col);
5100 if (column_span != priv->col_column)
5102 priv->col_column = column_span;
5104 gtk_combo_box_relayout (combo_box);
5106 g_object_notify (G_OBJECT (combo_box), "column-span-column");
5111 * gtk_combo_box_get_active:
5112 * @combo_box: A #GtkComboBox
5114 * Returns the index of the currently active item, or -1 if there's no
5115 * active item. If the model is a non-flat treemodel, and the active item
5116 * is not an immediate child of the root of the tree, this function returns
5117 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
5118 * <literal>path</literal> is the #GtkTreePath of the active item.
5120 * Return value: An integer which is the index of the currently active item,
5121 * or -1 if there's no active item.
5126 gtk_combo_box_get_active (GtkComboBox *combo_box)
5128 GtkComboBoxPrivate *priv;
5131 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5133 priv = combo_box->priv;
5135 if (gtk_tree_row_reference_valid (priv->active_row))
5139 path = gtk_tree_row_reference_get_path (priv->active_row);
5140 result = gtk_tree_path_get_indices (path)[0];
5141 gtk_tree_path_free (path);
5150 * gtk_combo_box_set_active:
5151 * @combo_box: A #GtkComboBox
5152 * @index_: An index in the model passed during construction, or -1 to have
5155 * Sets the active item of @combo_box to be the item at @index.
5160 gtk_combo_box_set_active (GtkComboBox *combo_box,
5163 GtkTreePath *path = NULL;
5164 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5165 g_return_if_fail (index_ >= -1);
5167 if (combo_box->priv->model == NULL)
5169 /* Save index, in case the model is set after the index */
5170 combo_box->priv->active = index_;
5176 path = gtk_tree_path_new_from_indices (index_, -1);
5178 gtk_combo_box_set_active_internal (combo_box, path);
5181 gtk_tree_path_free (path);
5185 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
5188 GtkComboBoxPrivate *priv = combo_box->priv;
5189 GtkTreePath *active_path;
5192 /* Remember whether the initially active row is valid. */
5193 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
5195 if (path && is_valid_row_reference)
5197 active_path = gtk_tree_row_reference_get_path (priv->active_row);
5198 path_cmp = gtk_tree_path_compare (path, active_path);
5199 gtk_tree_path_free (active_path);
5204 if (priv->active_row)
5206 gtk_tree_row_reference_free (priv->active_row);
5207 priv->active_row = NULL;
5212 if (priv->tree_view)
5213 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
5216 GtkMenu *menu = GTK_MENU (priv->popup_widget);
5218 if (GTK_IS_MENU (menu))
5219 gtk_menu_set_active (menu, -1);
5222 if (priv->cell_view)
5223 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
5226 * Do not emit a "changed" signal when an already invalid selection was
5227 * now set to invalid.
5229 if (!is_valid_row_reference)
5235 gtk_tree_row_reference_new (priv->model, path);
5237 if (priv->tree_view)
5239 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
5242 else if (GTK_IS_MENU (priv->popup_widget))
5244 /* FIXME handle nested menus better */
5245 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
5246 gtk_tree_path_get_indices (path)[0]);
5249 if (priv->cell_view)
5250 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
5254 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
5255 g_object_notify (G_OBJECT (combo_box), "active");
5256 if (combo_box->priv->id_column >= 0)
5257 g_object_notify (G_OBJECT (combo_box), "active-id");
5262 * gtk_combo_box_get_active_iter:
5263 * @combo_box: A #GtkComboBox
5264 * @iter: (out): The uninitialized #GtkTreeIter
5266 * Sets @iter to point to the current active item, if it exists.
5268 * Return value: %TRUE, if @iter was set
5273 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
5279 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5281 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
5284 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
5285 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
5286 gtk_tree_path_free (path);
5292 * gtk_combo_box_set_active_iter:
5293 * @combo_box: A #GtkComboBox
5294 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5296 * Sets the current active item to be the one referenced by @iter, or
5297 * unsets the active item if @iter is %NULL.
5302 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5305 GtkTreePath *path = NULL;
5307 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5310 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5312 gtk_combo_box_set_active_internal (combo_box, path);
5313 gtk_tree_path_free (path);
5317 * gtk_combo_box_set_model:
5318 * @combo_box: A #GtkComboBox
5319 * @model: (allow-none): A #GtkTreeModel
5321 * Sets the model used by @combo_box to be @model. Will unset a previously set
5322 * model (if applicable). If model is %NULL, then it will unset the model.
5324 * Note that this function does not clear the cell renderers, you have to
5325 * call gtk_cell_layout_clear() yourself if you need to set up different
5326 * cell renderers for the new model.
5331 gtk_combo_box_set_model (GtkComboBox *combo_box,
5332 GtkTreeModel *model)
5334 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5335 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5337 if (model == combo_box->priv->model)
5340 gtk_combo_box_unset_model (combo_box);
5345 combo_box->priv->model = model;
5346 g_object_ref (combo_box->priv->model);
5348 combo_box->priv->inserted_id =
5349 g_signal_connect (combo_box->priv->model, "row-inserted",
5350 G_CALLBACK (gtk_combo_box_model_row_inserted),
5352 combo_box->priv->deleted_id =
5353 g_signal_connect (combo_box->priv->model, "row-deleted",
5354 G_CALLBACK (gtk_combo_box_model_row_deleted),
5356 combo_box->priv->reordered_id =
5357 g_signal_connect (combo_box->priv->model, "rows-reordered",
5358 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5360 combo_box->priv->changed_id =
5361 g_signal_connect (combo_box->priv->model, "row-changed",
5362 G_CALLBACK (gtk_combo_box_model_row_changed),
5365 if (combo_box->priv->tree_view)
5368 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5369 combo_box->priv->model);
5370 gtk_combo_box_list_popup_resize (combo_box);
5375 if (combo_box->priv->popup_widget)
5376 gtk_combo_box_menu_fill (combo_box);
5380 if (combo_box->priv->cell_view)
5381 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5382 combo_box->priv->model);
5384 if (combo_box->priv->active != -1)
5386 /* If an index was set in advance, apply it now */
5387 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5388 combo_box->priv->active = -1;
5392 gtk_combo_box_update_sensitivity (combo_box);
5394 g_object_notify (G_OBJECT (combo_box), "model");
5398 * gtk_combo_box_get_model:
5399 * @combo_box: A #GtkComboBox
5401 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5403 * Return value: (transfer none): A #GtkTreeModel which was passed
5404 * during construction.
5409 gtk_combo_box_get_model (GtkComboBox *combo_box)
5411 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5413 return combo_box->priv->model;
5417 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5418 GtkScrollType scroll)
5421 GtkTreeIter new_iter;
5422 gboolean active_iter;
5425 if (!combo_box->priv->model)
5427 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5431 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5435 case GTK_SCROLL_STEP_BACKWARD:
5436 case GTK_SCROLL_STEP_UP:
5437 case GTK_SCROLL_STEP_LEFT:
5440 found = tree_prev (combo_box, combo_box->priv->model,
5441 &iter, &new_iter, FALSE);
5444 /* else fall through */
5446 case GTK_SCROLL_PAGE_FORWARD:
5447 case GTK_SCROLL_PAGE_DOWN:
5448 case GTK_SCROLL_PAGE_RIGHT:
5449 case GTK_SCROLL_END:
5450 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5453 case GTK_SCROLL_STEP_FORWARD:
5454 case GTK_SCROLL_STEP_DOWN:
5455 case GTK_SCROLL_STEP_RIGHT:
5458 found = tree_next (combo_box, combo_box->priv->model,
5459 &iter, &new_iter, FALSE);
5462 /* else fall through */
5464 case GTK_SCROLL_PAGE_BACKWARD:
5465 case GTK_SCROLL_PAGE_UP:
5466 case GTK_SCROLL_PAGE_LEFT:
5467 case GTK_SCROLL_START:
5468 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5475 if (found && active_iter)
5477 GtkTreePath *old_path;
5478 GtkTreePath *new_path;
5480 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5481 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5483 if (gtk_tree_path_compare (old_path, new_path) == 0)
5486 gtk_tree_path_free (old_path);
5487 gtk_tree_path_free (new_path);
5492 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5496 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5501 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5502 gboolean group_cycling)
5504 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5506 if (combo_box->priv->has_entry)
5510 child = gtk_bin_get_child (GTK_BIN (combo_box));
5512 gtk_widget_grab_focus (child);
5515 gtk_widget_grab_focus (combo_box->priv->button);
5521 gtk_combo_box_grab_focus (GtkWidget *widget)
5523 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5525 if (combo_box->priv->has_entry)
5529 child = gtk_bin_get_child (GTK_BIN (combo_box));
5531 gtk_widget_grab_focus (child);
5534 gtk_widget_grab_focus (combo_box->priv->button);
5538 gtk_combo_box_destroy (GtkWidget *widget)
5540 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5542 if (combo_box->priv->popup_idle_id > 0)
5544 g_source_remove (combo_box->priv->popup_idle_id);
5545 combo_box->priv->popup_idle_id = 0;
5548 gtk_combo_box_popdown (combo_box);
5550 if (combo_box->priv->row_separator_destroy)
5551 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5553 combo_box->priv->row_separator_func = NULL;
5554 combo_box->priv->row_separator_data = NULL;
5555 combo_box->priv->row_separator_destroy = NULL;
5557 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
5558 combo_box->priv->cell_view = NULL;
5562 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
5565 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
5568 * Fixes regression reported in bug #574059. The old functionality relied on
5569 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
5570 * when the selection was already set to -1.
5572 if (gtk_combo_box_get_active(combo_box) == -1)
5573 g_signal_emit_by_name (combo_box, "changed");
5575 gtk_combo_box_set_active (combo_box, -1);
5579 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
5582 GtkComboBoxPrivate *priv = combo_box->priv;
5583 GtkTreeModel *model;
5586 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5588 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
5592 GValue value = {0,};
5594 g_signal_handlers_block_by_func (entry,
5595 gtk_combo_box_entry_contents_changed,
5598 model = gtk_combo_box_get_model (combo_box);
5600 gtk_tree_model_get_value (model, &iter,
5601 priv->text_column, &value);
5602 g_object_set_property (G_OBJECT (entry), "text", &value);
5603 g_value_unset (&value);
5605 g_signal_handlers_unblock_by_func (entry,
5606 gtk_combo_box_entry_contents_changed,
5613 gtk_combo_box_constructor (GType type,
5614 guint n_construct_properties,
5615 GObjectConstructParam *construct_properties)
5618 GtkComboBox *combo_box;
5619 GtkComboBoxPrivate *priv;
5621 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
5622 (type, n_construct_properties, construct_properties);
5624 combo_box = GTK_COMBO_BOX (object);
5625 priv = combo_box->priv;
5627 if (priv->has_entry)
5631 entry = gtk_entry_new ();
5632 gtk_widget_show (entry);
5633 gtk_container_add (GTK_CONTAINER (combo_box), entry);
5635 priv->text_renderer = gtk_cell_renderer_text_new ();
5636 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
5637 priv->text_renderer, TRUE);
5639 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
5641 g_signal_connect (combo_box, "changed",
5642 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
5650 gtk_combo_box_dispose(GObject* object)
5652 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5654 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5656 gtk_combo_box_menu_destroy (combo_box);
5657 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5658 combo_box->priv->popup_widget = NULL;
5661 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5665 gtk_combo_box_finalize (GObject *object)
5667 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5670 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5671 gtk_combo_box_list_destroy (combo_box);
5673 if (combo_box->priv->popup_window)
5674 gtk_widget_destroy (combo_box->priv->popup_window);
5676 gtk_combo_box_unset_model (combo_box);
5678 for (i = combo_box->priv->cells; i; i = i->next)
5680 ComboCellInfo *info = (ComboCellInfo *)i->data;
5681 GSList *list = info->attributes;
5684 info->destroy (info->func_data);
5686 while (list && list->next)
5688 g_free (list->data);
5689 list = list->next->next;
5691 g_slist_free (info->attributes);
5693 g_object_unref (info->cell);
5694 g_slice_free (ComboCellInfo, info);
5696 g_slist_free (combo_box->priv->cells);
5698 g_free (combo_box->priv->tearoff_title);
5700 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5705 gtk_cell_editable_key_press (GtkWidget *widget,
5709 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5711 if (event->keyval == GDK_KEY_Escape)
5713 g_object_set (combo_box,
5714 "editing-canceled", TRUE,
5716 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5717 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5721 else if (event->keyval == GDK_KEY_Return ||
5722 event->keyval == GDK_KEY_ISO_Enter ||
5723 event->keyval == GDK_KEY_KP_Enter)
5725 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5726 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5735 popdown_idle (gpointer data)
5737 GtkComboBox *combo_box;
5739 combo_box = GTK_COMBO_BOX (data);
5741 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5742 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5744 g_object_unref (combo_box);
5750 popdown_handler (GtkWidget *widget,
5753 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5757 popup_idle (gpointer data)
5759 GtkComboBox *combo_box;
5761 combo_box = GTK_COMBO_BOX (data);
5763 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5764 combo_box->priv->cell_view)
5765 g_signal_connect_object (combo_box->priv->popup_widget,
5766 "unmap", G_CALLBACK (popdown_handler),
5769 /* we unset this if a menu item is activated */
5770 g_object_set (combo_box,
5771 "editing-canceled", TRUE,
5773 gtk_combo_box_popup (combo_box);
5775 combo_box->priv->popup_idle_id = 0;
5776 combo_box->priv->activate_button = 0;
5777 combo_box->priv->activate_time = 0;
5783 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5786 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5789 combo_box->priv->is_cell_renderer = TRUE;
5791 if (combo_box->priv->cell_view)
5793 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5794 G_CALLBACK (gtk_cell_editable_key_press),
5797 gtk_widget_grab_focus (combo_box->priv->button);
5801 child = gtk_bin_get_child (GTK_BIN (combo_box));
5803 g_signal_connect_object (child, "key-press-event",
5804 G_CALLBACK (gtk_cell_editable_key_press),
5807 gtk_widget_grab_focus (child);
5808 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5811 /* we do the immediate popup only for the optionmenu-like
5814 if (combo_box->priv->is_cell_renderer &&
5815 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5817 if (event && event->type == GDK_BUTTON_PRESS)
5819 GdkEventButton *event_button = (GdkEventButton *)event;
5821 combo_box->priv->activate_button = event_button->button;
5822 combo_box->priv->activate_time = event_button->time;
5825 combo_box->priv->popup_idle_id =
5826 gdk_threads_add_idle (popup_idle, combo_box);
5832 * gtk_combo_box_get_add_tearoffs:
5833 * @combo_box: a #GtkComboBox
5835 * Gets the current value of the :add-tearoffs property.
5837 * Return value: the current value of the :add-tearoffs property.
5840 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5842 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5844 return combo_box->priv->add_tearoffs;
5848 * gtk_combo_box_set_add_tearoffs:
5849 * @combo_box: a #GtkComboBox
5850 * @add_tearoffs: %TRUE to add tearoff menu items
5852 * Sets whether the popup menu should have a tearoff
5858 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5859 gboolean add_tearoffs)
5861 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5863 add_tearoffs = add_tearoffs != FALSE;
5865 if (combo_box->priv->add_tearoffs != add_tearoffs)
5867 combo_box->priv->add_tearoffs = add_tearoffs;
5868 gtk_combo_box_check_appearance (combo_box);
5869 gtk_combo_box_relayout (combo_box);
5870 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5875 * gtk_combo_box_get_title:
5876 * @combo_box: a #GtkComboBox
5878 * Gets the current title of the menu in tearoff mode. See
5879 * gtk_combo_box_set_add_tearoffs().
5881 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5882 * string which must not be freed.
5886 G_CONST_RETURN gchar*
5887 gtk_combo_box_get_title (GtkComboBox *combo_box)
5889 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5891 return combo_box->priv->tearoff_title;
5895 gtk_combo_box_update_title (GtkComboBox *combo_box)
5897 gtk_combo_box_check_appearance (combo_box);
5899 if (combo_box->priv->popup_widget &&
5900 GTK_IS_MENU (combo_box->priv->popup_widget))
5901 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5902 combo_box->priv->tearoff_title);
5906 * gtk_combo_box_set_title:
5907 * @combo_box: a #GtkComboBox
5908 * @title: a title for the menu in tearoff mode
5910 * Sets the menu's title in tearoff mode.
5915 gtk_combo_box_set_title (GtkComboBox *combo_box,
5918 GtkComboBoxPrivate *priv;
5920 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5922 priv = combo_box->priv;
5924 if (strcmp (title ? title : "",
5925 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5927 g_free (priv->tearoff_title);
5928 priv->tearoff_title = g_strdup (title);
5930 gtk_combo_box_update_title (combo_box);
5932 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5938 * gtk_combo_box_set_popup_fixed_width:
5939 * @combo_box: a #GtkComboBox
5940 * @fixed: whether to use a fixed popup width
5942 * Specifies whether the popup's width should be a fixed width
5943 * matching the allocated width of the combo box.
5948 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5951 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5953 if (combo_box->priv->popup_fixed_width != fixed)
5955 combo_box->priv->popup_fixed_width = fixed;
5957 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5962 * gtk_combo_box_get_popup_fixed_width:
5963 * @combo_box: a #GtkComboBox
5965 * Gets whether the popup uses a fixed width matching
5966 * the allocated width of the combo box.
5968 * Returns: %TRUE if the popup uses a fixed width
5973 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5975 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5977 return combo_box->priv->popup_fixed_width;
5982 * gtk_combo_box_get_popup_accessible:
5983 * @combo_box: a #GtkComboBox
5985 * Gets the accessible object corresponding to the combo box's popup.
5987 * This function is mostly intended for use by accessibility technologies;
5988 * applications should have little use for it.
5990 * Returns: (transfer none): the accessible object corresponding
5991 * to the combo box's popup.
5996 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
6000 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
6002 if (combo_box->priv->popup_widget)
6004 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
6012 * gtk_combo_box_get_row_separator_func:
6013 * @combo_box: a #GtkComboBox
6015 * Returns the current row separator function.
6017 * Return value: the current row separator function.
6021 GtkTreeViewRowSeparatorFunc
6022 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
6024 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
6026 return combo_box->priv->row_separator_func;
6030 * gtk_combo_box_set_row_separator_func:
6031 * @combo_box: a #GtkComboBox
6032 * @func: a #GtkTreeViewRowSeparatorFunc
6033 * @data: (allow-none): user data to pass to @func, or %NULL
6034 * @destroy: (allow-none): destroy notifier for @data, or %NULL
6036 * Sets the row separator function, which is used to determine
6037 * whether a row should be drawn as a separator. If the row separator
6038 * function is %NULL, no separators are drawn. This is the default value.
6043 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
6044 GtkTreeViewRowSeparatorFunc func,
6046 GDestroyNotify destroy)
6048 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6050 if (combo_box->priv->row_separator_destroy)
6051 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
6053 combo_box->priv->row_separator_func = func;
6054 combo_box->priv->row_separator_data = data;
6055 combo_box->priv->row_separator_destroy = destroy;
6057 if (combo_box->priv->tree_view)
6058 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
6061 gtk_combo_box_relayout (combo_box);
6063 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
6067 * gtk_combo_box_set_button_sensitivity:
6068 * @combo_box: a #GtkComboBox
6069 * @sensitivity: specify the sensitivity of the dropdown button
6071 * Sets whether the dropdown button of the combo box should be
6072 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
6073 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
6078 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
6079 GtkSensitivityType sensitivity)
6081 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6083 if (combo_box->priv->button_sensitivity != sensitivity)
6085 combo_box->priv->button_sensitivity = sensitivity;
6086 gtk_combo_box_update_sensitivity (combo_box);
6088 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
6093 * gtk_combo_box_get_button_sensitivity:
6094 * @combo_box: a #GtkComboBox
6096 * Returns whether the combo box sets the dropdown button
6097 * sensitive or not when there are no items in the model.
6099 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
6100 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
6101 * if the button is always insensitive or
6102 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
6103 * the model has one item to be selected.
6108 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
6110 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6112 return combo_box->priv->button_sensitivity;
6117 * gtk_combo_box_get_has_entry:
6118 * @combo_box: a #GtkComboBox
6120 * Returns whether the combo box has an entry.
6122 * Return Value: whether there is an entry in @combo_box.
6127 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
6129 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6131 return combo_box->priv->has_entry;
6135 * gtk_combo_box_set_entry_text_column:
6136 * @combo_box: A #GtkComboBox
6137 * @text_column: A column in @model to get the strings from for
6138 * the internal entry
6140 * Sets the model column which @combo_box should use to get strings from
6141 * to be @text_column. The column @text_column in the model of @combo_box
6142 * must be of type %G_TYPE_STRING.
6144 * This is only relevant if @combo_box has been created with
6145 * #GtkComboBox:has-entry as %TRUE.
6150 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
6153 GtkComboBoxPrivate *priv = combo_box->priv;
6154 GtkTreeModel *model;
6156 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6158 model = gtk_combo_box_get_model (combo_box);
6160 g_return_if_fail (text_column >= 0);
6161 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
6163 priv->text_column = text_column;
6165 if (priv->text_renderer != NULL)
6166 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
6167 priv->text_renderer,
6168 "text", text_column,
6173 * gtk_combo_box_get_entry_text_column:
6174 * @combo_box: A #GtkComboBox.
6176 * Returns the column which @combo_box is using to get the strings
6177 * from to display in the internal entry.
6179 * Return value: A column in the data source model of @combo_box.
6184 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
6186 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
6188 return combo_box->priv->text_column;
6192 * gtk_combo_box_set_focus_on_click:
6193 * @combo: a #GtkComboBox
6194 * @focus_on_click: whether the combo box grabs focus when clicked
6197 * Sets whether the combo box will grab focus when it is clicked with
6198 * the mouse. Making mouse clicks not grab focus is useful in places
6199 * like toolbars where you don't want the keyboard focus removed from
6200 * the main area of the application.
6205 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
6206 gboolean focus_on_click)
6208 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6210 focus_on_click = focus_on_click != FALSE;
6212 if (combo_box->priv->focus_on_click != focus_on_click)
6214 combo_box->priv->focus_on_click = focus_on_click;
6216 if (combo_box->priv->button)
6217 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
6220 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
6225 * gtk_combo_box_get_focus_on_click:
6226 * @combo: a #GtkComboBox
6228 * Returns whether the combo box grabs focus when it is clicked
6229 * with the mouse. See gtk_combo_box_set_focus_on_click().
6231 * Return value: %TRUE if the combo box grabs focus when it is
6232 * clicked with the mouse.
6237 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
6239 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6241 return combo_box->priv->focus_on_click;
6246 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
6247 GtkBuilder *builder,
6249 const gchar *tagname,
6250 GMarkupParser *parser,
6253 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6254 tagname, parser, data))
6257 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6258 tagname, parser, data);
6262 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
6263 GtkBuilder *builder,
6265 const gchar *tagname,
6268 if (strcmp (tagname, "attributes") == 0)
6269 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
6272 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
6277 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
6278 GtkBuilder *builder,
6279 const gchar *childname)
6281 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
6283 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
6284 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
6286 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
6290 gtk_combo_box_remeasure (GtkComboBox *combo_box)
6292 GtkComboBoxPrivate *priv = combo_box->priv;
6297 !gtk_tree_model_get_iter_first (priv->model, &iter))
6300 priv->minimum_width = priv->natural_width = 0;
6302 path = gtk_tree_path_new_from_indices (0, -1);
6306 gint row_min = 0, row_nat = 0;
6308 if (priv->cell_view)
6309 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
6310 path, &row_min, &row_nat);
6312 priv->minimum_width = MAX (priv->minimum_width, row_min);
6313 priv->natural_width = MAX (priv->natural_width, row_nat);
6315 gtk_tree_path_next (path);
6317 while (gtk_tree_model_iter_next (priv->model, &iter));
6319 gtk_tree_path_free (path);
6324 gtk_combo_box_measure_height_for_width (GtkComboBox *combo_box,
6330 GtkComboBoxPrivate *priv = combo_box->priv;
6333 gint child_min, child_nat;
6335 child = gtk_bin_get_child (GTK_BIN (combo_box));
6337 gtk_widget_get_preferred_height_for_width (child, avail_width,
6338 &child_min, &child_nat);
6341 !gtk_tree_model_get_iter_first (priv->model, &iter))
6344 path = gtk_tree_path_new_from_indices (0, -1);
6348 gint row_min = 0, row_nat = 0;
6350 if (priv->cell_view)
6351 gtk_cell_view_get_desired_height_for_width_of_row (GTK_CELL_VIEW (priv->cell_view),
6353 &row_min, &row_nat);
6355 child_min = MAX (child_min, row_min);
6356 child_nat = MAX (child_nat, row_nat);
6358 gtk_tree_path_next (path);
6360 while (gtk_tree_model_iter_next (priv->model, &iter));
6362 gtk_tree_path_free (path);
6367 *min_height = child_min;
6369 *nat_height = child_nat;
6374 gtk_combo_box_get_preferred_width (GtkWidget *widget,
6378 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
6379 GtkComboBoxPrivate *priv = combo_box->priv;
6381 gint focus_width, focus_pad;
6382 gint font_size, arrow_size;
6383 PangoContext *context;
6384 PangoFontMetrics *metrics;
6385 PangoFontDescription *font_desc;
6387 gint minimum_width, natural_width;
6388 gint child_min, child_nat;
6390 child = gtk_bin_get_child (GTK_BIN (widget));
6393 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
6394 gtk_combo_box_remeasure (combo_box);
6396 child_min = MAX (child_min, priv->minimum_width);
6397 child_nat = MAX (child_nat, priv->natural_width);
6399 gtk_widget_style_get (GTK_WIDGET (widget),
6400 "focus-line-width", &focus_width,
6401 "focus-padding", &focus_pad,
6402 "arrow-size", &arrow_size,
6405 font_desc = gtk_widget_get_style (child)->font_desc;
6406 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
6407 metrics = pango_context_get_metrics (context, font_desc,
6408 pango_context_get_language (context));
6409 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
6410 pango_font_metrics_get_descent (metrics));
6411 pango_font_metrics_unref (metrics);
6413 arrow_size = MAX (arrow_size, font_size);
6415 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
6417 if (!priv->tree_view)
6421 if (priv->cell_view)
6423 gint sep_width, arrow_width;
6424 gint border_width, xthickness, xpad;
6426 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
6427 xthickness = gtk_widget_get_style (priv->button)->xthickness;
6429 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
6430 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
6432 xpad = 2*(border_width + xthickness + focus_width + focus_pad);
6434 minimum_width = child_min + sep_width + arrow_width + xpad;
6435 natural_width = child_nat + sep_width + arrow_width + xpad;
6439 gint but_width, but_nat_width;
6441 gtk_widget_get_preferred_width (priv->button,
6442 &but_width, &but_nat_width);
6444 minimum_width = child_min + but_width;
6445 natural_width = child_nat + but_nat_width;
6451 gint button_width, button_nat_width;
6453 /* sample + frame */
6454 minimum_width = child_min;
6455 natural_width = child_nat;
6457 minimum_width += 2 * focus_width;
6458 natural_width += 2 * focus_width;
6460 if (priv->cell_view_frame)
6462 if (priv->has_frame)
6464 gint border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
6465 gint xpad = 2 * (border_width + gtk_widget_get_style (GTK_WIDGET (priv->cell_view_frame))->xthickness);
6467 minimum_width += xpad;
6468 natural_width += xpad;
6473 gtk_widget_get_preferred_width (priv->button,
6474 &button_width, &button_nat_width);
6476 minimum_width += button_width;
6477 natural_width += button_nat_width;
6480 if (GTK_SHADOW_NONE != priv->shadow_type)
6482 style = gtk_widget_get_style (GTK_WIDGET (widget));
6484 minimum_width += 2 * style->xthickness;
6485 natural_width += 2 * style->xthickness;
6489 *minimum_size = minimum_width;
6492 *natural_size = natural_width;
6496 gtk_combo_box_get_preferred_height (GtkWidget *widget,
6502 /* Combo box is height-for-width only
6503 * (so we always just reserve enough height for the minimum width) */
6504 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
6505 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
6509 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
6514 /* Combo box is height-for-width only
6515 * (so we assume we always reserved enough height for the minimum width) */
6516 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
6521 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
6526 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
6527 GtkComboBoxPrivate *priv = combo_box->priv;
6529 gint focus_width, focus_pad;
6530 gint min_height, nat_height;
6533 gtk_widget_style_get (GTK_WIDGET (widget),
6534 "focus-line-width", &focus_width,
6535 "focus-padding", &focus_pad,
6540 if (GTK_SHADOW_NONE != priv->shadow_type)
6541 size -= gtk_widget_get_style (GTK_WIDGET (widget))->xthickness;
6543 if (!priv->tree_view)
6546 if (priv->cell_view)
6548 GtkStyle *button_style;
6549 /* calculate x/y padding and separator/arrow size */
6550 gint sep_width, arrow_width, sep_height, arrow_height;
6551 gint border_width, xthickness, ythickness, xpad, ypad;
6553 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
6554 button_style = gtk_widget_get_style (priv->button);
6556 xthickness = button_style->xthickness;
6557 ythickness = button_style->ythickness;
6559 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
6560 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
6561 gtk_widget_get_preferred_height_for_width (priv->separator,
6562 sep_width, &sep_height, NULL);
6563 gtk_widget_get_preferred_height_for_width (priv->arrow,
6564 arrow_width, &arrow_height, NULL);
6566 xpad = 2*(border_width + xthickness + focus_width + focus_pad);
6567 ypad = 2*(border_width + ythickness + focus_width + focus_pad);
6569 size -= sep_width + arrow_width + xpad;
6571 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6573 arrow_height = MAX (arrow_height, sep_height);
6574 min_height = MAX (min_height, arrow_height);
6575 nat_height = MAX (nat_height, arrow_height);
6582 /* there is a custom child widget inside (no priv->cell_view) */
6583 gint but_width, but_height;
6585 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
6586 gtk_widget_get_preferred_height_for_width (priv->button,
6587 but_width, &but_height, NULL);
6591 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6593 min_height = MAX (min_height, but_height);
6594 nat_height = MAX (nat_height, but_height);
6600 gint but_width, but_height;
6601 gint xpad = 0, ypad = 0;
6603 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
6604 gtk_widget_get_preferred_height_for_width (priv->button,
6605 but_width, &but_height, NULL);
6607 if (priv->cell_view_frame && priv->has_frame)
6609 GtkStyle *cell_style;
6612 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
6613 cell_style = gtk_widget_get_style (GTK_WIDGET (priv->cell_view_frame));
6615 xpad = 2 * (border_width + cell_style->xthickness);
6616 ypad = 2 * (border_width + cell_style->ythickness);
6620 size -= 2 * focus_width;
6623 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6625 min_height = MAX (min_height, but_height);
6626 nat_height = MAX (nat_height, but_height);
6632 if (GTK_SHADOW_NONE != priv->shadow_type)
6634 style = gtk_widget_get_style (GTK_WIDGET (widget));
6636 min_height += 2 * style->ythickness;
6637 nat_height += 2 * style->ythickness;
6641 *minimum_size = min_height;
6644 *natural_size = nat_height;
6648 * gtk_combo_box_set_id_column:
6649 * @combo_box: A #GtkComboBox
6650 * @id_column: A column in @model to get string IDs for values from
6652 * Sets the model column which @combo_box should use to get string IDs
6653 * for values from. The column @id_column in the model of @combo_box
6654 * must be of type %G_TYPE_STRING.
6659 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
6662 GtkComboBoxPrivate *priv = combo_box->priv;
6663 GtkTreeModel *model;
6665 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6667 if (id_column != priv->id_column)
6669 model = gtk_combo_box_get_model (combo_box);
6671 g_return_if_fail (id_column >= 0);
6672 g_return_if_fail (model == NULL ||
6673 id_column < gtk_tree_model_get_n_columns (model));
6675 priv->id_column = id_column;
6677 g_object_notify (G_OBJECT (combo_box), "id-column");
6678 g_object_notify (G_OBJECT (combo_box), "active-id");
6683 * gtk_combo_box_get_id_column:
6684 * @combo_box: A #GtkComboBox
6686 * Returns the column which @combo_box is using to get string IDs
6689 * Return value: A column in the data source model of @combo_box.
6694 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
6696 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
6698 return combo_box->priv->id_column;
6702 * gtk_combo_box_get_active_id:
6703 * @combo_box: a #GtkComboBox
6705 * Returns the ID of the active row of @combo_box. This value is taken
6706 * from the active row and the column specified by the 'id-column'
6707 * property of @combo_box (see gtk_combo_box_set_id_column()).
6709 * The returned value is an interned string which means that you can
6710 * compare the pointer by value to other interned strings and that you
6713 * If the 'id-column' property of @combo_box is not set or if no row is
6714 * selected then %NULL is returned.
6716 * Return value: the ID of the active row, or %NULL
6721 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
6723 GtkTreeModel *model;
6727 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
6729 column = combo_box->priv->id_column;
6734 model = gtk_combo_box_get_model (combo_box);
6735 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
6736 G_TYPE_STRING, NULL);
6738 if (gtk_combo_box_get_active_iter (combo_box, &iter))
6740 const gchar *interned;
6743 gtk_tree_model_get (model, &iter, column, &id, -1);
6744 interned = g_intern_string (id);
6754 * gtk_combo_box_set_active_id:
6755 * @combo_box: a #GtkComboBox
6756 * @active_id: the ID of the row to select
6758 * Changes the active row of @combo_box to the one that has an ID equal to @id.
6760 * If the 'id-column' property of @combo_box is unset or if no row has
6761 * the given ID then nothing happens.
6766 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
6767 const gchar *active_id)
6769 GtkTreeModel *model;
6773 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
6775 column = combo_box->priv->id_column;
6780 model = gtk_combo_box_get_model (combo_box);
6781 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
6784 if (gtk_tree_model_get_iter_first (model, &iter))
6789 gtk_tree_model_get (model, &iter, column, &id, -1);
6790 match = strcmp (id, active_id) == 0;
6795 gtk_combo_box_set_active_iter (combo_box, &iter);
6798 } while (gtk_tree_model_iter_next (model, &iter));