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: #GtkComboBoxEntry, #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 * In addition to the model-view API, GtkComboBox offers a simple API which
78 * is suitable for text-only combo boxes, and hides the complexity of
79 * managing the data in a model. It consists of the functions
80 * gtk_combo_box_new_text(), gtk_combo_box_append_text(),
81 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text(),
82 * gtk_combo_box_remove_text() and gtk_combo_box_get_active_text().
86 /* WELCOME, to THE house of evil code */
88 typedef struct _ComboCellInfo ComboCellInfo;
91 GtkCellRenderer *cell;
94 GtkCellLayoutDataFunc func;
96 GDestroyNotify destroy;
103 struct _GtkComboBoxPrivate
111 GtkShadowType shadow_type;
113 gint active; /* Only temporary */
114 GtkTreeRowReference *active_row;
116 GtkWidget *tree_view;
117 GtkTreeViewColumn *column;
119 GtkWidget *cell_view;
120 GtkWidget *cell_view_frame;
125 GtkWidget *separator;
127 GtkWidget *popup_widget;
128 GtkWidget *popup_window;
129 GtkWidget *scrolled_window;
136 guint activate_button;
137 guint32 activate_time;
139 guint resize_idle_id;
146 guint popup_in_progress : 1;
147 guint popup_shown : 1;
148 guint add_tearoffs : 1;
150 guint is_cell_renderer : 1;
151 guint editing_canceled : 1;
152 guint auto_scroll : 1;
153 guint focus_on_click : 1;
154 guint button_sensitivity : 2;
156 GtkTreeViewRowSeparatorFunc row_separator_func;
157 gpointer row_separator_data;
158 GDestroyNotify row_separator_destroy;
160 GdkDevice *grab_pointer;
161 GdkDevice *grab_keyboard;
163 gchar *tearoff_title;
166 /* While debugging this evil code, I have learned that
167 * there are actually 4 modes to this widget, which can
168 * be characterized as follows
170 * 1) menu mode, no child added
173 * cell_view -> GtkCellView, regular child
174 * cell_view_frame -> NULL
175 * button -> GtkToggleButton set_parent to combo
176 * arrow -> GtkArrow set_parent to button
177 * separator -> GtkVSepator set_parent to button
178 * popup_widget -> GtkMenu
179 * popup_window -> NULL
180 * scrolled_window -> NULL
182 * 2) menu mode, child added
186 * cell_view_frame -> NULL
187 * button -> GtkToggleButton set_parent to combo
188 * arrow -> GtkArrow, child of button
190 * popup_widget -> GtkMenu
191 * popup_window -> NULL
192 * scrolled_window -> NULL
194 * 3) list mode, no child added
196 * tree_view -> GtkTreeView, child of scrolled_window
197 * cell_view -> GtkCellView, regular child
198 * cell_view_frame -> GtkFrame, set parent to combo
199 * button -> GtkToggleButton, set_parent to combo
200 * arrow -> GtkArrow, child of button
202 * popup_widget -> tree_view
203 * popup_window -> GtkWindow
204 * scrolled_window -> GtkScrolledWindow, child of popup_window
206 * 4) list mode, child added
208 * tree_view -> GtkTreeView, child of scrolled_window
210 * cell_view_frame -> NULL
211 * button -> GtkToggleButton, set_parent to combo
212 * arrow -> GtkArrow, child of button
214 * popup_widget -> tree_view
215 * popup_window -> GtkWindow
216 * scrolled_window -> GtkScrolledWindow, child of popup_window
232 PROP_ROW_SPAN_COLUMN,
233 PROP_COLUMN_SPAN_COLUMN,
240 PROP_BUTTON_SENSITIVITY,
241 PROP_EDITING_CANCELED
244 static guint combo_box_signals[LAST_SIGNAL] = {0,};
246 #define BONUS_PADDING 4
247 #define SCROLL_TIME 100
251 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
252 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
253 static void gtk_combo_box_dispose (GObject *object);
254 static void gtk_combo_box_finalize (GObject *object);
255 static void gtk_combo_box_destroy (GtkWidget *widget);
257 static void gtk_combo_box_set_property (GObject *object,
261 static void gtk_combo_box_get_property (GObject *object,
266 static void gtk_combo_box_state_changed (GtkWidget *widget,
267 GtkStateType previous);
268 static void gtk_combo_box_grab_focus (GtkWidget *widget);
269 static void gtk_combo_box_style_set (GtkWidget *widget,
271 static void gtk_combo_box_button_toggled (GtkWidget *widget,
273 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
274 GtkStateType previous,
276 static void gtk_combo_box_add (GtkContainer *container,
278 static void gtk_combo_box_remove (GtkContainer *container,
281 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
282 GtkCellRenderer *cell);
284 static void gtk_combo_box_menu_show (GtkWidget *menu,
286 static void gtk_combo_box_menu_hide (GtkWidget *menu,
289 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
291 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
296 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
301 static void gtk_combo_box_menu_position (GtkMenu *menu,
307 static void gtk_combo_box_update_requested_width(GtkComboBox *combo_box,
309 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
311 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
313 static void gtk_combo_box_size_allocate (GtkWidget *widget,
314 GtkAllocation *allocation);
315 static void gtk_combo_box_forall (GtkContainer *container,
316 gboolean include_internals,
317 GtkCallback callback,
318 gpointer callback_data);
319 static gboolean gtk_combo_box_draw (GtkWidget *widget,
321 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
322 GdkEventScroll *event);
323 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
326 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
327 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
328 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
329 GtkScrollType scroll);
330 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
331 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
333 /* listening to the model */
334 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
338 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
341 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
346 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
350 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
356 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
361 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
362 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
364 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
365 GdkEventButton *event,
367 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
370 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
371 GdkEventCrossing *event,
373 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
376 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
377 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
378 GdkEventButton *event,
381 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
384 gboolean path_currently_selected,
387 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
391 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
394 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
395 gboolean add_children);
396 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
397 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
400 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
401 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
403 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
407 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
409 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
410 GdkEventButton *event,
412 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
415 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
416 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
420 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
423 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
428 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
432 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
435 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
437 guint32 activate_time);
438 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
443 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
444 GtkCellRenderer *cell,
446 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
447 GtkCellRenderer *cell,
449 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
450 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
451 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
452 GtkCellRenderer *cell,
453 const gchar *attribute,
455 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
456 GtkCellRenderer *cell,
457 GtkCellLayoutDataFunc func,
459 GDestroyNotify destroy);
460 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
461 GtkCellRenderer *cell);
462 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
463 GtkCellRenderer *cell,
465 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
466 gboolean group_cycling);
468 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
469 GtkCellLayout *cell_layout);
470 static void combo_cell_data_func (GtkCellLayout *cell_layout,
471 GtkCellRenderer *cell,
472 GtkTreeModel *tree_model,
475 static void gtk_combo_box_child_show (GtkWidget *widget,
476 GtkComboBox *combo_box);
477 static void gtk_combo_box_child_hide (GtkWidget *widget,
478 GtkComboBox *combo_box);
480 /* GtkBuildable method implementation */
481 static GtkBuildableIface *parent_buildable_iface;
483 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
484 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
487 const gchar *tagname,
488 GMarkupParser *parser,
490 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
493 const gchar *tagname,
496 /* GtkCellEditable method implementations */
497 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
500 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
503 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
506 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
510 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
516 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
517 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
518 gtk_combo_box_cell_layout_init)
519 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
520 gtk_combo_box_cell_editable_init)
521 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
522 gtk_combo_box_buildable_init))
527 gtk_combo_box_class_init (GtkComboBoxClass *klass)
529 GObjectClass *object_class;
530 GtkContainerClass *container_class;
531 GtkWidgetClass *widget_class;
532 GtkBindingSet *binding_set;
534 klass->get_active_text = gtk_combo_box_real_get_active_text;
536 container_class = (GtkContainerClass *)klass;
537 container_class->forall = gtk_combo_box_forall;
538 container_class->add = gtk_combo_box_add;
539 container_class->remove = gtk_combo_box_remove;
541 widget_class = (GtkWidgetClass *)klass;
542 widget_class->size_allocate = gtk_combo_box_size_allocate;
543 widget_class->draw = gtk_combo_box_draw;
544 widget_class->scroll_event = gtk_combo_box_scroll_event;
545 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
546 widget_class->grab_focus = gtk_combo_box_grab_focus;
547 widget_class->style_set = gtk_combo_box_style_set;
548 widget_class->state_changed = gtk_combo_box_state_changed;
549 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
550 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
551 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
552 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
553 widget_class->destroy = gtk_combo_box_destroy;
555 object_class = (GObjectClass *)klass;
556 object_class->dispose = gtk_combo_box_dispose;
557 object_class->finalize = gtk_combo_box_finalize;
558 object_class->set_property = gtk_combo_box_set_property;
559 object_class->get_property = gtk_combo_box_get_property;
563 * GtkComboBox::changed:
564 * @widget: the object which received the signal
566 * The changed signal is emitted when the active
567 * item is changed. The can be due to the user selecting
568 * a different item from the list, or due to a
569 * call to gtk_combo_box_set_active_iter().
570 * It will also be emitted while typing into a GtkComboBoxEntry,
571 * as well as when selecting an item from the GtkComboBoxEntry's list.
575 combo_box_signals[CHANGED] =
576 g_signal_new (I_("changed"),
577 G_OBJECT_CLASS_TYPE (klass),
579 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
581 g_cclosure_marshal_VOID__VOID,
584 * GtkComboBox::move-active:
585 * @widget: the object that received the signal
586 * @scroll_type: a #GtkScrollType
588 * The ::move-active signal is a
589 * <link linkend="keybinding-signals">keybinding signal</link>
590 * which gets emitted to move the active selection.
594 combo_box_signals[MOVE_ACTIVE] =
595 g_signal_new_class_handler (I_("move-active"),
596 G_OBJECT_CLASS_TYPE (klass),
597 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
598 G_CALLBACK (gtk_combo_box_real_move_active),
600 g_cclosure_marshal_VOID__ENUM,
602 GTK_TYPE_SCROLL_TYPE);
605 * GtkComboBox::popup:
606 * @widget: the object that received the signal
608 * The ::popup signal is a
609 * <link linkend="keybinding-signals">keybinding signal</link>
610 * which gets emitted to popup the combo box list.
612 * The default binding for this signal is Alt+Down.
616 combo_box_signals[POPUP] =
617 g_signal_new_class_handler (I_("popup"),
618 G_OBJECT_CLASS_TYPE (klass),
619 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
620 G_CALLBACK (gtk_combo_box_real_popup),
622 g_cclosure_marshal_VOID__VOID,
625 * GtkComboBox::popdown:
626 * @button: the object which received the signal
628 * The ::popdown signal is a
629 * <link linkend="keybinding-signals">keybinding signal</link>
630 * which gets emitted to popdown the combo box list.
632 * The default bindings for this signal are Alt+Up and Escape.
636 combo_box_signals[POPDOWN] =
637 g_signal_new_class_handler (I_("popdown"),
638 G_OBJECT_CLASS_TYPE (klass),
639 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
640 G_CALLBACK (gtk_combo_box_real_popdown),
642 _gtk_marshal_BOOLEAN__VOID,
646 binding_set = gtk_binding_set_by_class (widget_class);
648 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
650 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
653 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
655 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
657 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
660 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
662 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
663 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
665 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
666 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
668 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
669 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
671 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
672 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
674 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
675 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
677 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
679 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
681 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
682 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
684 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
685 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
687 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
688 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
690 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
691 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
693 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
694 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
696 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
699 g_object_class_override_property (object_class,
700 PROP_EDITING_CANCELED,
706 * The model from which the combo box takes the values shown
711 g_object_class_install_property (object_class,
713 g_param_spec_object ("model",
714 P_("ComboBox model"),
715 P_("The model for the combo box"),
717 GTK_PARAM_READWRITE));
720 * GtkComboBox:wrap-width:
722 * If wrap-width is set to a positive value, the list will be
723 * displayed in multiple columns, the number of columns is
724 * determined by wrap-width.
728 g_object_class_install_property (object_class,
730 g_param_spec_int ("wrap-width",
732 P_("Wrap width for laying out the items in a grid"),
736 GTK_PARAM_READWRITE));
740 * GtkComboBox:row-span-column:
742 * If this is set to a non-negative value, it must be the index of a column
743 * of type %G_TYPE_INT in the model.
745 * The values of that column are used to determine how many rows a value in
746 * the list will span. Therefore, the values in the model column pointed to
747 * by this property must be greater than zero and not larger than wrap-width.
751 g_object_class_install_property (object_class,
752 PROP_ROW_SPAN_COLUMN,
753 g_param_spec_int ("row-span-column",
754 P_("Row span column"),
755 P_("TreeModel column containing the row span values"),
759 GTK_PARAM_READWRITE));
763 * GtkComboBox:column-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 columns a value
769 * in the list will span.
773 g_object_class_install_property (object_class,
774 PROP_COLUMN_SPAN_COLUMN,
775 g_param_spec_int ("column-span-column",
776 P_("Column span column"),
777 P_("TreeModel column containing the column span values"),
781 GTK_PARAM_READWRITE));
785 * GtkComboBox:active:
787 * The item which is currently active. If the model is a non-flat treemodel,
788 * and the active item is not an immediate child of the root of the tree,
789 * this property has the value
790 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
791 * where <literal>path</literal> is the #GtkTreePath of the active item.
795 g_object_class_install_property (object_class,
797 g_param_spec_int ("active",
799 P_("The item which is currently active"),
803 GTK_PARAM_READWRITE));
806 * GtkComboBox:add-tearoffs:
808 * The add-tearoffs property controls whether generated menus
809 * have tearoff menu items.
811 * Note that this only affects menu style combo boxes.
815 g_object_class_install_property (object_class,
817 g_param_spec_boolean ("add-tearoffs",
818 P_("Add tearoffs to menus"),
819 P_("Whether dropdowns should have a tearoff menu item"),
821 GTK_PARAM_READWRITE));
824 * GtkComboBox:has-frame:
826 * The has-frame property controls whether a frame
827 * is drawn around the entry.
831 g_object_class_install_property (object_class,
833 g_param_spec_boolean ("has-frame",
835 P_("Whether the combo box draws a frame around the child"),
837 GTK_PARAM_READWRITE));
839 g_object_class_install_property (object_class,
841 g_param_spec_boolean ("focus-on-click",
842 P_("Focus on click"),
843 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
845 GTK_PARAM_READWRITE));
848 * GtkComboBox:tearoff-title:
850 * A title that may be displayed by the window manager
851 * when the popup is torn-off.
855 g_object_class_install_property (object_class,
857 g_param_spec_string ("tearoff-title",
859 P_("A title that may be displayed by the window manager when the popup is torn-off"),
861 GTK_PARAM_READWRITE));
865 * GtkComboBox:popup-shown:
867 * Whether the combo boxes dropdown is popped up.
868 * Note that this property is mainly useful, because
869 * it allows you to connect to notify::popup-shown.
873 g_object_class_install_property (object_class,
875 g_param_spec_boolean ("popup-shown",
877 P_("Whether the combo's dropdown is shown"),
879 GTK_PARAM_READABLE));
883 * GtkComboBox:button-sensitivity:
885 * Whether the dropdown button is sensitive when
886 * the model is empty.
890 g_object_class_install_property (object_class,
891 PROP_BUTTON_SENSITIVITY,
892 g_param_spec_enum ("button-sensitivity",
893 P_("Button Sensitivity"),
894 P_("Whether the dropdown button is sensitive when the model is empty"),
895 GTK_TYPE_SENSITIVITY_TYPE,
896 GTK_SENSITIVITY_AUTO,
897 GTK_PARAM_READWRITE));
899 gtk_widget_class_install_style_property (widget_class,
900 g_param_spec_boolean ("appears-as-list",
901 P_("Appears as list"),
902 P_("Whether dropdowns should look like lists rather than menus"),
904 GTK_PARAM_READABLE));
907 * GtkComboBox:arrow-size:
909 * Sets the minimum size of the arrow in the combo box. Note
910 * that the arrow size is coupled to the font size, so in case
911 * a larger font is used, the arrow will be larger than set
916 gtk_widget_class_install_style_property (widget_class,
917 g_param_spec_int ("arrow-size",
919 P_("The minimum size of the arrow in the combo box"),
923 GTK_PARAM_READABLE));
926 * GtkComboBox:shadow-type:
928 * Which kind of shadow to draw around the combo box.
932 gtk_widget_class_install_style_property (widget_class,
933 g_param_spec_enum ("shadow-type",
935 P_("Which kind of shadow to draw around the combo box"),
936 GTK_TYPE_SHADOW_TYPE,
938 GTK_PARAM_READABLE));
940 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
944 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
946 parent_buildable_iface = g_type_interface_peek_parent (iface);
947 iface->add_child = _gtk_cell_layout_buildable_add_child;
948 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
949 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
953 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
955 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
956 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
957 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
958 iface->clear = gtk_combo_box_cell_layout_clear;
959 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
960 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
961 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
962 iface->reorder = gtk_combo_box_cell_layout_reorder;
966 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
968 iface->start_editing = gtk_combo_box_start_editing;
972 gtk_combo_box_init (GtkComboBox *combo_box)
974 GtkComboBoxPrivate *priv;
976 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
979 priv = combo_box->priv;
981 priv->cell_view = gtk_cell_view_new ();
982 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
983 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
984 gtk_widget_show (priv->cell_view);
986 priv->minimum_width = 0;
987 priv->natural_width = 0;
989 priv->wrap_width = 0;
992 priv->active_row = NULL;
993 priv->col_column = -1;
994 priv->row_column = -1;
996 priv->popup_shown = FALSE;
997 priv->add_tearoffs = FALSE;
998 priv->has_frame = TRUE;
999 priv->is_cell_renderer = FALSE;
1000 priv->editing_canceled = FALSE;
1001 priv->auto_scroll = FALSE;
1002 priv->focus_on_click = TRUE;
1003 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1005 gtk_combo_box_check_appearance (combo_box);
1009 gtk_combo_box_set_property (GObject *object,
1011 const GValue *value,
1014 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1019 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1022 case PROP_WRAP_WIDTH:
1023 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1026 case PROP_ROW_SPAN_COLUMN:
1027 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1030 case PROP_COLUMN_SPAN_COLUMN:
1031 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1035 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1038 case PROP_ADD_TEAROFFS:
1039 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1042 case PROP_HAS_FRAME:
1043 combo_box->priv->has_frame = g_value_get_boolean (value);
1046 case PROP_FOCUS_ON_CLICK:
1047 gtk_combo_box_set_focus_on_click (combo_box,
1048 g_value_get_boolean (value));
1051 case PROP_TEAROFF_TITLE:
1052 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1055 case PROP_POPUP_SHOWN:
1056 if (g_value_get_boolean (value))
1057 gtk_combo_box_popup (combo_box);
1059 gtk_combo_box_popdown (combo_box);
1062 case PROP_BUTTON_SENSITIVITY:
1063 gtk_combo_box_set_button_sensitivity (combo_box,
1064 g_value_get_enum (value));
1067 case PROP_EDITING_CANCELED:
1068 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1072 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1078 gtk_combo_box_get_property (GObject *object,
1083 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1084 GtkComboBoxPrivate *priv = combo_box->priv;
1089 g_value_set_object (value, combo_box->priv->model);
1092 case PROP_WRAP_WIDTH:
1093 g_value_set_int (value, combo_box->priv->wrap_width);
1096 case PROP_ROW_SPAN_COLUMN:
1097 g_value_set_int (value, combo_box->priv->row_column);
1100 case PROP_COLUMN_SPAN_COLUMN:
1101 g_value_set_int (value, combo_box->priv->col_column);
1105 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1108 case PROP_ADD_TEAROFFS:
1109 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1112 case PROP_HAS_FRAME:
1113 g_value_set_boolean (value, combo_box->priv->has_frame);
1116 case PROP_FOCUS_ON_CLICK:
1117 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1120 case PROP_TEAROFF_TITLE:
1121 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1124 case PROP_POPUP_SHOWN:
1125 g_value_set_boolean (value, combo_box->priv->popup_shown);
1128 case PROP_BUTTON_SENSITIVITY:
1129 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1132 case PROP_EDITING_CANCELED:
1133 g_value_set_boolean (value, priv->editing_canceled);
1137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1143 gtk_combo_box_state_changed (GtkWidget *widget,
1144 GtkStateType previous)
1146 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1147 GtkComboBoxPrivate *priv = combo_box->priv;
1149 if (gtk_widget_get_realized (widget))
1151 if (priv->tree_view && priv->cell_view)
1152 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1153 >k_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
1156 gtk_widget_queue_draw (widget);
1160 gtk_combo_box_button_state_changed (GtkWidget *widget,
1161 GtkStateType previous,
1164 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1165 GtkComboBoxPrivate *priv = combo_box->priv;
1167 if (gtk_widget_get_realized (widget))
1169 if (!priv->tree_view && priv->cell_view)
1171 if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) !=
1172 (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE))
1173 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1175 gtk_widget_set_state (priv->cell_view,
1176 gtk_widget_get_state (widget));
1180 gtk_widget_queue_draw (widget);
1184 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1186 GtkComboBoxPrivate *priv = combo_box->priv;
1187 gboolean appears_as_list;
1189 /* if wrap_width > 0, then we are in grid-mode and forced to use
1192 if (priv->wrap_width)
1193 appears_as_list = FALSE;
1195 gtk_widget_style_get (GTK_WIDGET (combo_box),
1196 "appears-as-list", &appears_as_list,
1199 if (appears_as_list)
1201 /* Destroy all the menu mode widgets, if they exist. */
1202 if (GTK_IS_MENU (priv->popup_widget))
1203 gtk_combo_box_menu_destroy (combo_box);
1205 /* Create the list mode widgets, if they don't already exist. */
1206 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1207 gtk_combo_box_list_setup (combo_box);
1211 /* Destroy all the list mode widgets, if they exist. */
1212 if (GTK_IS_TREE_VIEW (priv->tree_view))
1213 gtk_combo_box_list_destroy (combo_box);
1215 /* Create the menu mode widgets, if they don't already exist. */
1216 if (!GTK_IS_MENU (priv->popup_widget))
1217 gtk_combo_box_menu_setup (combo_box, TRUE);
1220 gtk_widget_style_get (GTK_WIDGET (combo_box),
1221 "shadow-type", &priv->shadow_type,
1226 gtk_combo_box_style_set (GtkWidget *widget,
1229 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1230 GtkComboBoxPrivate *priv = combo_box->priv;
1233 gtk_combo_box_check_appearance (combo_box);
1235 if (priv->tree_view && priv->cell_view)
1236 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1237 >k_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
1239 child = gtk_bin_get_child (GTK_BIN (combo_box));
1240 if (GTK_IS_ENTRY (child))
1241 g_object_set (child, "shadow-type",
1242 GTK_SHADOW_NONE == priv->shadow_type ?
1243 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1247 gtk_combo_box_button_toggled (GtkWidget *widget,
1250 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1252 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1254 if (!combo_box->priv->popup_in_progress)
1255 gtk_combo_box_popup (combo_box);
1258 gtk_combo_box_popdown (combo_box);
1262 gtk_combo_box_add (GtkContainer *container,
1265 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1266 GtkComboBoxPrivate *priv = combo_box->priv;
1268 if (priv->cell_view &&
1269 gtk_widget_get_parent (priv->cell_view))
1271 gtk_widget_unparent (priv->cell_view);
1272 _gtk_bin_set_child (GTK_BIN (container), NULL);
1273 gtk_widget_queue_resize (GTK_WIDGET (container));
1276 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1277 _gtk_bin_set_child (GTK_BIN (container), widget);
1279 if (priv->cell_view &&
1280 widget != priv->cell_view)
1282 /* since the cell_view was unparented, it's gone now */
1283 priv->cell_view = NULL;
1285 if (!priv->tree_view && priv->separator)
1287 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1289 priv->separator = NULL;
1291 gtk_widget_queue_resize (GTK_WIDGET (container));
1293 else if (priv->cell_view_frame)
1295 gtk_widget_unparent (priv->cell_view_frame);
1296 priv->cell_view_frame = NULL;
1303 gtk_combo_box_remove (GtkContainer *container,
1306 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1307 GtkComboBoxPrivate *priv = combo_box->priv;
1309 gboolean appears_as_list;
1311 if (widget == priv->cell_view)
1312 priv->cell_view = NULL;
1314 gtk_widget_unparent (widget);
1315 _gtk_bin_set_child (GTK_BIN (container), NULL);
1317 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1320 gtk_widget_queue_resize (GTK_WIDGET (container));
1322 if (!priv->tree_view)
1323 appears_as_list = FALSE;
1325 appears_as_list = TRUE;
1327 if (appears_as_list)
1328 gtk_combo_box_list_destroy (combo_box);
1329 else if (GTK_IS_MENU (priv->popup_widget))
1331 gtk_combo_box_menu_destroy (combo_box);
1332 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1333 priv->popup_widget = NULL;
1336 if (!priv->cell_view)
1338 priv->cell_view = gtk_cell_view_new ();
1339 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1340 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1342 gtk_widget_show (priv->cell_view);
1343 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1345 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1349 if (appears_as_list)
1350 gtk_combo_box_list_setup (combo_box);
1352 gtk_combo_box_menu_setup (combo_box, TRUE);
1354 if (gtk_tree_row_reference_valid (priv->active_row))
1356 path = gtk_tree_row_reference_get_path (priv->active_row);
1357 gtk_combo_box_set_active_internal (combo_box, path);
1358 gtk_tree_path_free (path);
1361 gtk_combo_box_set_active_internal (combo_box, NULL);
1364 static ComboCellInfo *
1365 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1366 GtkCellRenderer *cell)
1370 for (i = combo_box->priv->cells; i; i = i->next)
1372 ComboCellInfo *info = (ComboCellInfo *)i->data;
1374 if (info && info->cell == cell)
1382 gtk_combo_box_menu_show (GtkWidget *menu,
1385 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1386 GtkComboBoxPrivate *priv = combo_box->priv;
1388 gtk_combo_box_child_show (menu, user_data);
1390 priv->popup_in_progress = TRUE;
1391 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1393 priv->popup_in_progress = FALSE;
1397 gtk_combo_box_menu_hide (GtkWidget *menu,
1400 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1402 gtk_combo_box_child_hide (menu,user_data);
1404 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1409 gtk_combo_box_detacher (GtkWidget *widget,
1412 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1413 GtkComboBoxPrivate *priv = combo_box->priv;
1415 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1417 g_signal_handlers_disconnect_by_func (menu->toplevel,
1418 gtk_combo_box_menu_show,
1420 g_signal_handlers_disconnect_by_func (menu->toplevel,
1421 gtk_combo_box_menu_hide,
1424 priv->popup_widget = NULL;
1428 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1431 GtkComboBoxPrivate *priv = combo_box->priv;
1433 if (GTK_IS_MENU (priv->popup_widget))
1435 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1436 priv->popup_widget = NULL;
1438 else if (priv->popup_widget)
1440 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1441 priv->popup_widget);
1442 g_object_unref (priv->popup_widget);
1443 priv->popup_widget = NULL;
1446 if (GTK_IS_MENU (popup))
1448 if (priv->popup_window)
1450 gtk_widget_destroy (priv->popup_window);
1451 priv->popup_window = NULL;
1454 priv->popup_widget = popup;
1457 * Note that we connect to show/hide on the toplevel, not the
1458 * menu itself, since the menu is not shown/hidden when it is
1459 * popped up while torn-off.
1461 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1462 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1463 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1464 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1466 gtk_menu_attach_to_widget (GTK_MENU (popup),
1467 GTK_WIDGET (combo_box),
1468 gtk_combo_box_detacher);
1472 if (!priv->popup_window)
1474 GtkWidget *toplevel;
1476 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1477 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1479 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1480 GDK_WINDOW_TYPE_HINT_COMBO);
1482 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1483 G_CALLBACK (gtk_combo_box_child_show),
1485 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1486 G_CALLBACK (gtk_combo_box_child_hide),
1489 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1490 if (GTK_IS_WINDOW (toplevel))
1492 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1493 GTK_WINDOW (priv->popup_window));
1494 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1495 GTK_WINDOW (toplevel));
1498 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1499 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1500 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1502 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1504 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1507 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1510 gtk_widget_show (priv->scrolled_window);
1512 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1513 priv->scrolled_window);
1516 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1519 gtk_widget_show (popup);
1520 g_object_ref (popup);
1521 priv->popup_widget = popup;
1526 gtk_combo_box_menu_position_below (GtkMenu *menu,
1532 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1533 GtkAllocation child_allocation;
1539 GdkRectangle monitor;
1541 /* FIXME: is using the size request here broken? */
1542 child = gtk_bin_get_child (GTK_BIN (combo_box));
1546 gtk_widget_get_allocation (child, &child_allocation);
1548 if (!gtk_widget_get_has_window (child))
1550 sx += child_allocation.x;
1551 sy += child_allocation.y;
1554 gdk_window_get_root_coords (gtk_widget_get_window (child),
1557 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1558 sx -= gtk_widget_get_style (GTK_WIDGET (combo_box))->xthickness;
1560 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
1563 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1566 *x = sx + child_allocation.width - req.width;
1569 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1570 monitor_num = gdk_screen_get_monitor_at_window (screen,
1571 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1572 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1576 else if (*x + req.width > monitor.x + monitor.width)
1577 *x = monitor.x + monitor.width - req.width;
1579 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1580 *y += child_allocation.height;
1581 else if (*y - monitor.y >= req.height)
1583 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1584 *y += child_allocation.height;
1592 gtk_combo_box_menu_position_over (GtkMenu *menu,
1598 GtkComboBox *combo_box;
1602 GtkAllocation allocation;
1603 GtkAllocation child_allocation;
1610 combo_box = GTK_COMBO_BOX (user_data);
1611 widget = GTK_WIDGET (combo_box);
1613 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1615 gtk_widget_get_allocation (widget, &allocation);
1617 menu_xpos = allocation.x;
1618 menu_ypos = allocation.y + allocation.height / 2 - 2;
1620 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1624 gtk_widget_get_allocation (active, &child_allocation);
1625 menu_ypos -= child_allocation.height / 2;
1628 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1631 child = children->data;
1633 if (active == child)
1636 if (gtk_widget_get_visible (child))
1638 gtk_widget_get_allocation (child, &child_allocation);
1640 menu_ypos -= child_allocation.height;
1643 children = children->next;
1646 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1647 menu_xpos = menu_xpos + allocation.width - menu_width;
1649 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1650 menu_xpos, menu_ypos,
1651 &menu_xpos, &menu_ypos);
1653 /* Clamp the position on screen */
1654 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1658 else if ((menu_xpos + menu_width) > screen_width)
1659 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1668 gtk_combo_box_menu_position (GtkMenu *menu,
1674 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1675 GtkComboBoxPrivate *priv = combo_box->priv;
1676 GtkWidget *menu_item;
1679 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1680 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1683 /* FIXME handle nested menus better */
1684 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1686 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1689 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1692 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1693 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1694 GDK_WINDOW_TYPE_HINT_COMBO);
1698 gtk_combo_box_list_position (GtkComboBox *combo_box,
1704 GtkComboBoxPrivate *priv = combo_box->priv;
1705 GtkAllocation allocation;
1708 GdkRectangle monitor;
1709 GtkRequisition popup_req;
1710 GtkPolicyType hpolicy, vpolicy;
1713 /* under windows, the drop down list is as wide as the combo box itself.
1715 GtkWidget *widget = GTK_WIDGET (combo_box);
1719 gtk_widget_get_allocation (widget, &allocation);
1721 if (!gtk_widget_get_has_window (widget))
1727 window = gtk_widget_get_window (widget);
1729 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1732 *width = allocation.width;
1734 hpolicy = vpolicy = GTK_POLICY_NEVER;
1735 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1737 gtk_widget_get_preferred_size (priv->scrolled_window,
1740 if (popup_req.width > *width)
1742 hpolicy = GTK_POLICY_ALWAYS;
1743 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1745 gtk_widget_get_preferred_size (priv->scrolled_window,
1749 *height = popup_req.height;
1751 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1752 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1753 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1757 else if (*x + *width > monitor.x + monitor.width)
1758 *x = monitor.x + monitor.width - *width;
1760 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1761 *y += allocation.height;
1762 else if (*y - *height >= monitor.y)
1764 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1766 *y += allocation.height;
1767 *height = monitor.y + monitor.height - *y;
1771 *height = *y - monitor.y;
1775 if (popup_req.height > *height)
1777 vpolicy = GTK_POLICY_ALWAYS;
1779 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1785 cell_view_is_sensitive (GtkCellView *cell_view)
1787 GList *cells, *list;
1790 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1793 for (list = cells; list; list = list->next)
1795 g_object_get (list->data, "sensitive", &sensitive, NULL);
1800 g_list_free (cells);
1806 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1809 GtkComboBoxPrivate *priv = combo_box->priv;
1810 GList *cells, *list;
1816 if (priv->row_separator_func)
1818 if (priv->row_separator_func (priv->model, iter,
1819 priv->row_separator_data))
1823 gtk_tree_view_column_cell_set_cell_data (priv->column,
1825 iter, FALSE, FALSE);
1827 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1830 for (list = cells; list; list = list->next)
1832 g_object_get (list->data, "sensitive", &sensitive, NULL);
1837 g_list_free (cells);
1843 update_menu_sensitivity (GtkComboBox *combo_box,
1846 GtkComboBoxPrivate *priv = combo_box->priv;
1847 GList *children, *child;
1848 GtkWidget *item, *submenu, *separator;
1849 GtkWidget *cell_view;
1855 children = gtk_container_get_children (GTK_CONTAINER (menu));
1857 for (child = children; child; child = child->next)
1859 item = GTK_WIDGET (child->data);
1860 cell_view = gtk_bin_get_child (GTK_BIN (item));
1862 if (!GTK_IS_CELL_VIEW (cell_view))
1865 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1866 if (submenu != NULL)
1868 gtk_widget_set_sensitive (item, TRUE);
1869 update_menu_sensitivity (combo_box, submenu);
1873 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1875 if (menu != priv->popup_widget && child == children)
1877 separator = GTK_WIDGET (child->next->data);
1878 g_object_set (item, "visible", sensitive, NULL);
1879 g_object_set (separator, "visible", sensitive, NULL);
1882 gtk_widget_set_sensitive (item, sensitive);
1886 g_list_free (children);
1890 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1892 guint32 activate_time)
1894 GtkComboBoxPrivate *priv = combo_box->priv;
1897 gint width, min_width;
1899 update_menu_sensitivity (combo_box, priv->popup_widget);
1902 if (gtk_tree_row_reference_valid (priv->active_row))
1904 path = gtk_tree_row_reference_get_path (priv->active_row);
1905 active_item = gtk_tree_path_get_indices (path)[0];
1906 gtk_tree_path_free (path);
1908 if (priv->add_tearoffs)
1912 /* FIXME handle nested menus better */
1913 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1915 if (priv->wrap_width == 0)
1917 GtkAllocation allocation;
1919 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
1920 width = allocation.width;
1921 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1922 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, NULL);
1924 gtk_widget_set_size_request (priv->popup_widget,
1925 MAX (width, min_width), -1);
1928 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1930 gtk_combo_box_menu_position, combo_box,
1931 button, activate_time);
1935 popup_grab_on_window (GdkWindow *window,
1936 GdkDevice *keyboard,
1938 guint32 activate_time)
1941 gdk_device_grab (keyboard, window,
1942 GDK_OWNERSHIP_WINDOW, TRUE,
1943 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1944 NULL, activate_time) != GDK_GRAB_SUCCESS)
1948 gdk_device_grab (pointer, window,
1949 GDK_OWNERSHIP_WINDOW, TRUE,
1950 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1951 GDK_POINTER_MOTION_MASK,
1952 NULL, activate_time) != GDK_GRAB_SUCCESS)
1955 gdk_device_ungrab (keyboard, activate_time);
1964 * gtk_combo_box_popup:
1965 * @combo_box: a #GtkComboBox
1967 * Pops up the menu or dropdown list of @combo_box.
1969 * This function is mostly intended for use by accessibility technologies;
1970 * applications should have little use for it.
1975 gtk_combo_box_popup (GtkComboBox *combo_box)
1977 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1979 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1983 * gtk_combo_box_popup_for_device:
1984 * @combo_box: a #GtkComboBox
1985 * @device: a #GdkDevice
1987 * Pops up the menu or dropdown list of @combo_box, the popup window
1988 * will be grabbed so only @device and its associated pointer/keyboard
1989 * are the only #GdkDevice<!-- -->s able to send events to it.
1994 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
1997 GtkComboBoxPrivate *priv = combo_box->priv;
1998 gint x, y, width, height;
1999 GtkTreePath *path = NULL, *ppath;
2000 GtkWidget *toplevel;
2001 GdkDevice *keyboard, *pointer;
2004 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2005 g_return_if_fail (GDK_IS_DEVICE (device));
2007 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2010 if (gtk_widget_get_mapped (priv->popup_widget))
2013 if (priv->grab_pointer && priv->grab_keyboard)
2016 time = gtk_get_current_event_time ();
2018 if (device->source == GDK_SOURCE_KEYBOARD)
2021 pointer = gdk_device_get_associated_device (device);
2026 keyboard = gdk_device_get_associated_device (device);
2029 if (GTK_IS_MENU (priv->popup_widget))
2031 gtk_combo_box_menu_popup (combo_box,
2032 priv->activate_button,
2033 priv->activate_time);
2037 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2038 if (GTK_IS_WINDOW (toplevel))
2039 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2040 GTK_WINDOW (priv->popup_window));
2042 gtk_widget_show_all (priv->scrolled_window);
2043 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2045 gtk_widget_set_size_request (priv->popup_window, width, height);
2046 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2048 if (gtk_tree_row_reference_valid (priv->active_row))
2050 path = gtk_tree_row_reference_get_path (priv->active_row);
2051 ppath = gtk_tree_path_copy (path);
2052 if (gtk_tree_path_up (ppath))
2053 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2055 gtk_tree_path_free (ppath);
2057 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2061 gtk_widget_show (priv->popup_window);
2065 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2067 gtk_tree_path_free (path);
2070 gtk_widget_grab_focus (priv->popup_window);
2071 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2074 if (!gtk_widget_has_focus (priv->tree_view))
2075 gtk_widget_grab_focus (priv->tree_view);
2077 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2078 keyboard, pointer, time))
2080 gtk_widget_hide (priv->popup_window);
2084 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2085 priv->grab_pointer = pointer;
2086 priv->grab_keyboard = keyboard;
2090 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2094 device = gtk_get_current_event_device ();
2098 GdkDeviceManager *device_manager;
2099 GdkDisplay *display;
2102 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2103 device_manager = gdk_display_get_device_manager (display);
2105 /* No device was set, pick the first master device */
2106 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2107 device = devices->data;
2108 g_list_free (devices);
2111 gtk_combo_box_popup_for_device (combo_box, device);
2115 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2117 if (combo_box->priv->popup_shown)
2119 gtk_combo_box_popdown (combo_box);
2127 * gtk_combo_box_popdown:
2128 * @combo_box: a #GtkComboBox
2130 * Hides the menu or dropdown list of @combo_box.
2132 * This function is mostly intended for use by accessibility technologies;
2133 * applications should have little use for it.
2138 gtk_combo_box_popdown (GtkComboBox *combo_box)
2140 GtkComboBoxPrivate *priv = combo_box->priv;
2142 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2144 if (GTK_IS_MENU (priv->popup_widget))
2146 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2150 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2153 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2154 gtk_widget_hide_all (priv->popup_window);
2155 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2158 priv->grab_pointer = NULL;
2159 priv->grab_keyboard = NULL;
2163 gtk_combo_box_update_requested_width (GtkComboBox *combo_box,
2166 GtkComboBoxPrivate *priv = combo_box->priv;
2167 gint padding, min_width, nat_width;
2169 if (priv->cell_view)
2170 gtk_widget_style_get (priv->cell_view,
2171 "focus-line-width", &padding,
2176 /* add some pixels for good measure */
2177 padding += BONUS_PADDING;
2179 if (priv->cell_view)
2180 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
2181 path, &min_width, &nat_width);
2183 min_width = nat_width = 0;
2185 min_width += padding;
2186 nat_width += padding;
2188 if (min_width > priv->minimum_width || nat_width > priv->natural_width)
2190 priv->minimum_width = MAX (priv->minimum_width, min_width);
2191 priv->natural_width = MAX (priv->natural_width, nat_width);
2193 if (priv->cell_view)
2195 gtk_widget_set_size_request (priv->cell_view, min_width, -1);
2196 gtk_widget_queue_resize (priv->cell_view);
2201 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2202 gtk_widget_get_preferred_size (combo_box->priv->button, \
2206 child.x = allocation->x + shadow_width; \
2208 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2210 child.y = allocation->y + shadow_height; \
2211 child.width = req.width; \
2212 child.height = allocation->height - 2 * shadow_height; \
2213 child.width = MAX (1, child.width); \
2214 child.height = MAX (1, child.height); \
2216 gtk_widget_size_allocate (combo_box->priv->button, &child);
2219 gtk_combo_box_size_allocate (GtkWidget *widget,
2220 GtkAllocation *allocation)
2222 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2223 GtkComboBoxPrivate *priv = combo_box->priv;
2224 GtkWidget *child_widget;
2225 gint shadow_width, shadow_height;
2226 gint focus_width, focus_pad;
2227 GtkAllocation child;
2230 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2232 gtk_widget_set_allocation (widget, allocation);
2233 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2235 style = gtk_widget_get_style (widget);
2236 gtk_widget_style_get (widget,
2237 "focus-line-width", &focus_width,
2238 "focus-padding", &focus_pad,
2241 if (GTK_SHADOW_NONE != priv->shadow_type)
2243 shadow_width = style->xthickness;
2244 shadow_height = style->ythickness;
2252 if (!priv->tree_view)
2254 if (priv->cell_view)
2256 gint xthickness, ythickness;
2261 allocation->x += shadow_width;
2262 allocation->y += shadow_height;
2263 allocation->width -= 2 * shadow_width;
2264 allocation->height -= 2 * shadow_height;
2266 gtk_widget_size_allocate (priv->button, allocation);
2268 /* set some things ready */
2269 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2270 style = gtk_widget_get_style (priv->button);
2271 xthickness = style->xthickness;
2272 ythickness = style->ythickness;
2274 child.x = allocation->x;
2275 child.y = allocation->y;
2276 width = allocation->width;
2277 child.height = allocation->height;
2279 if (!priv->is_cell_renderer)
2281 child.x += border_width + xthickness + focus_width + focus_pad;
2282 child.y += border_width + ythickness + focus_width + focus_pad;
2283 width -= 2 * (child.x - allocation->x);
2284 child.height -= 2 * (child.y - allocation->y);
2288 /* handle the children */
2289 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2290 child.width = req.width;
2292 child.x += width - req.width;
2293 child.width = MAX (1, child.width);
2294 child.height = MAX (1, child.height);
2295 gtk_widget_size_allocate (priv->arrow, &child);
2297 child.x += req.width;
2298 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2299 child.width = req.width;
2301 child.x -= req.width;
2302 child.width = MAX (1, child.width);
2303 child.height = MAX (1, child.height);
2304 gtk_widget_size_allocate (priv->separator, &child);
2308 child.x += req.width;
2309 child.width = allocation->x + allocation->width
2310 - (border_width + xthickness + focus_width + focus_pad)
2315 child.width = child.x;
2316 child.x = allocation->x
2317 + border_width + xthickness + focus_width + focus_pad;
2318 child.width -= child.x;
2321 if (gtk_widget_get_visible (priv->popup_widget))
2323 gint width, min_width;
2325 if (priv->wrap_width == 0)
2327 GtkAllocation combo_box_allocation;
2329 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2330 width = combo_box_allocation.width;
2331 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2332 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, NULL);
2333 gtk_widget_set_size_request (priv->popup_widget,
2334 MAX (width, min_width), -1);
2337 /* reposition the menu after giving it a new width */
2338 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2341 child.width = MAX (1, child.width);
2342 child.height = MAX (1, child.height);
2343 gtk_widget_size_allocate (child_widget, &child);
2347 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2350 child.x = allocation->x + req.width + shadow_width;
2352 child.x = allocation->x + shadow_width;
2353 child.y = allocation->y + shadow_height;
2354 child.width = allocation->width - req.width - 2 * shadow_width;
2355 child.width = MAX (1, child.width);
2356 child.height = MAX (1, child.height);
2357 gtk_widget_size_allocate (child_widget, &child);
2364 /* Combobox thickness + border-width */
2365 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2366 int delta_x = shadow_width + border_width;
2367 int delta_y = shadow_height + border_width;
2370 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2374 child.x = allocation->x + req.width;
2376 child.x = allocation->x;
2378 child.y = allocation->y;
2379 child.width = allocation->width - req.width;
2380 child.height = allocation->height;
2382 if (priv->cell_view_frame)
2386 child.width = MAX (1, child.width - delta_x * 2);
2387 child.height = MAX (1, child.height - delta_y * 2);
2388 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2391 if (priv->has_frame)
2393 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2394 style = gtk_widget_get_style (priv->cell_view_frame);
2395 delta_x = border_width + style->xthickness;
2396 delta_y = border_width + style->ythickness;
2400 child.width -= delta_x * 2;
2401 child.height -= delta_y * 2;
2408 child.width -= delta_x * 2;
2409 child.height -= delta_y * 2;
2412 if (gtk_widget_get_visible (priv->popup_window))
2414 gint x, y, width, height;
2415 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2416 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2417 gtk_widget_set_size_request (priv->popup_window, width, height);
2421 child.width = MAX (1, child.width);
2422 child.height = MAX (1, child.height);
2424 gtk_widget_size_allocate (child_widget, &child);
2428 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2431 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2433 GtkComboBoxPrivate *priv = combo_box->priv;
2437 g_signal_handler_disconnect (priv->model,
2439 g_signal_handler_disconnect (priv->model,
2441 g_signal_handler_disconnect (priv->model,
2442 priv->reordered_id);
2443 g_signal_handler_disconnect (priv->model,
2448 if (!priv->tree_view)
2450 if (priv->popup_widget)
2451 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2452 (GtkCallback)gtk_widget_destroy, NULL);
2457 g_object_unref (priv->model);
2461 if (priv->active_row)
2463 gtk_tree_row_reference_free (priv->active_row);
2464 priv->active_row = NULL;
2467 if (priv->cell_view)
2468 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2472 gtk_combo_box_forall (GtkContainer *container,
2473 gboolean include_internals,
2474 GtkCallback callback,
2475 gpointer callback_data)
2477 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2478 GtkComboBoxPrivate *priv = combo_box->priv;
2481 if (include_internals)
2484 (* callback) (priv->button, callback_data);
2485 if (priv->cell_view_frame)
2486 (* callback) (priv->cell_view_frame, callback_data);
2489 child = gtk_bin_get_child (GTK_BIN (container));
2491 (* callback) (child, callback_data);
2495 gtk_combo_box_child_show (GtkWidget *widget,
2496 GtkComboBox *combo_box)
2498 GtkComboBoxPrivate *priv = combo_box->priv;
2500 priv->popup_shown = TRUE;
2501 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2505 gtk_combo_box_child_hide (GtkWidget *widget,
2506 GtkComboBox *combo_box)
2508 GtkComboBoxPrivate *priv = combo_box->priv;
2510 priv->popup_shown = FALSE;
2511 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2515 gtk_combo_box_draw (GtkWidget *widget,
2518 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2519 GtkComboBoxPrivate *priv = combo_box->priv;
2521 if (priv->shadow_type != GTK_SHADOW_NONE)
2523 gtk_paint_shadow (gtk_widget_get_style (widget),
2525 GTK_STATE_NORMAL, priv->shadow_type,
2528 gtk_widget_get_allocated_width (widget),
2529 gtk_widget_get_allocated_height (widget));
2532 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2535 if (priv->tree_view && priv->cell_view_frame)
2537 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2538 priv->cell_view_frame, cr);
2541 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2542 gtk_bin_get_child (GTK_BIN (widget)),
2558 path_visible (GtkTreeView *view,
2564 /* Note that we rely on the fact that collapsed rows don't have nodes
2566 return _gtk_tree_view_find_node (view, path, &tree, &node);
2570 tree_next_func (GtkTreeModel *model,
2575 SearchData *search_data = (SearchData *)data;
2577 if (search_data->found)
2579 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2582 if (search_data->visible &&
2583 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2586 search_data->set = TRUE;
2587 search_data->iter = *iter;
2592 if (gtk_tree_path_compare (path, search_data->path) == 0)
2593 search_data->found = TRUE;
2599 tree_next (GtkComboBox *combo,
2600 GtkTreeModel *model,
2605 SearchData search_data;
2607 search_data.combo = combo;
2608 search_data.path = gtk_tree_model_get_path (model, iter);
2609 search_data.visible = visible;
2610 search_data.found = FALSE;
2611 search_data.set = FALSE;
2613 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2615 *next = search_data.iter;
2617 gtk_tree_path_free (search_data.path);
2619 return search_data.set;
2623 tree_prev_func (GtkTreeModel *model,
2628 SearchData *search_data = (SearchData *)data;
2630 if (gtk_tree_path_compare (path, search_data->path) == 0)
2632 search_data->found = TRUE;
2636 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2639 if (search_data->visible &&
2640 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2643 search_data->set = TRUE;
2644 search_data->iter = *iter;
2650 tree_prev (GtkComboBox *combo,
2651 GtkTreeModel *model,
2656 SearchData search_data;
2658 search_data.combo = combo;
2659 search_data.path = gtk_tree_model_get_path (model, iter);
2660 search_data.visible = visible;
2661 search_data.found = FALSE;
2662 search_data.set = FALSE;
2664 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2666 *prev = search_data.iter;
2668 gtk_tree_path_free (search_data.path);
2670 return search_data.set;
2674 tree_last_func (GtkTreeModel *model,
2679 SearchData *search_data = (SearchData *)data;
2681 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2684 /* Note that we rely on the fact that collapsed rows don't have nodes
2686 if (search_data->visible &&
2687 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2690 search_data->set = TRUE;
2691 search_data->iter = *iter;
2697 tree_last (GtkComboBox *combo,
2698 GtkTreeModel *model,
2702 SearchData search_data;
2704 search_data.combo = combo;
2705 search_data.visible = visible;
2706 search_data.set = FALSE;
2708 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2710 *last = search_data.iter;
2712 return search_data.set;
2717 tree_first_func (GtkTreeModel *model,
2722 SearchData *search_data = (SearchData *)data;
2724 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2727 if (search_data->visible &&
2728 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2731 search_data->set = TRUE;
2732 search_data->iter = *iter;
2738 tree_first (GtkComboBox *combo,
2739 GtkTreeModel *model,
2743 SearchData search_data;
2745 search_data.combo = combo;
2746 search_data.visible = visible;
2747 search_data.set = FALSE;
2749 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2751 *first = search_data.iter;
2753 return search_data.set;
2757 gtk_combo_box_scroll_event (GtkWidget *widget,
2758 GdkEventScroll *event)
2760 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2763 GtkTreeIter new_iter;
2765 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2768 if (event->direction == GDK_SCROLL_UP)
2769 found = tree_prev (combo_box, combo_box->priv->model,
2770 &iter, &new_iter, FALSE);
2772 found = tree_next (combo_box, combo_box->priv->model,
2773 &iter, &new_iter, FALSE);
2776 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2786 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2787 GtkCellLayout *cell_layout)
2789 GtkComboBoxPrivate *priv = combo_box->priv;
2792 for (k = priv->cells; k; k = k->next)
2795 ComboCellInfo *info = (ComboCellInfo *)k->data;
2797 if (info->pack == GTK_PACK_START)
2798 gtk_cell_layout_pack_start (cell_layout,
2799 info->cell, info->expand);
2800 else if (info->pack == GTK_PACK_END)
2801 gtk_cell_layout_pack_end (cell_layout,
2802 info->cell, info->expand);
2804 gtk_cell_layout_set_cell_data_func (cell_layout,
2806 combo_cell_data_func, info, NULL);
2808 for (j = info->attributes; j; j = j->next->next)
2810 gtk_cell_layout_add_attribute (cell_layout,
2813 GPOINTER_TO_INT (j->next->data));
2819 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2820 gboolean add_children)
2822 GtkComboBoxPrivate *priv = combo_box->priv;
2826 child = gtk_bin_get_child (GTK_BIN (combo_box));
2828 if (priv->cell_view)
2830 priv->button = gtk_toggle_button_new ();
2831 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2832 priv->focus_on_click);
2834 g_signal_connect (priv->button, "toggled",
2835 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2836 gtk_widget_set_parent (priv->button,
2837 gtk_widget_get_parent (child));
2839 priv->box = gtk_hbox_new (FALSE, 0);
2840 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2842 priv->separator = gtk_vseparator_new ();
2843 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2845 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2846 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2848 gtk_widget_show_all (priv->button);
2852 priv->button = gtk_toggle_button_new ();
2853 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2854 priv->focus_on_click);
2856 g_signal_connect (priv->button, "toggled",
2857 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2858 gtk_widget_set_parent (priv->button,
2859 gtk_widget_get_parent (child));
2861 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2862 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2863 gtk_widget_show_all (priv->button);
2866 g_signal_connect (priv->button, "button-press-event",
2867 G_CALLBACK (gtk_combo_box_menu_button_press),
2869 g_signal_connect (priv->button, "state-changed",
2870 G_CALLBACK (gtk_combo_box_button_state_changed),
2873 /* create our funky menu */
2874 menu = gtk_menu_new ();
2875 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2876 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2878 g_signal_connect (menu, "key-press-event",
2879 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2880 gtk_combo_box_set_popup_widget (combo_box, menu);
2884 gtk_combo_box_menu_fill (combo_box);
2886 /* the column is needed in tree_column_row_is_sensitive() */
2887 priv->column = gtk_tree_view_column_new ();
2888 g_object_ref_sink (priv->column);
2889 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2891 gtk_combo_box_update_title (combo_box);
2892 gtk_combo_box_update_sensitivity (combo_box);
2896 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2898 GtkComboBoxPrivate *priv = combo_box->priv;
2904 menu = priv->popup_widget;
2906 if (priv->add_tearoffs)
2908 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2910 gtk_widget_show (tearoff);
2912 if (priv->wrap_width)
2913 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2915 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2918 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2922 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2923 GtkTreeModel *model,
2926 GtkWidget *cell_view;
2931 cell_view = gtk_cell_view_new ();
2932 item = gtk_menu_item_new ();
2933 gtk_container_add (GTK_CONTAINER (item), cell_view);
2935 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2936 path = gtk_tree_model_get_path (model, iter);
2937 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2938 gtk_tree_path_free (path);
2940 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2941 gtk_widget_get_preferred_size (cell_view, &req, NULL);
2942 gtk_widget_show (cell_view);
2948 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2950 GtkTreeIter *parent)
2952 GtkComboBoxPrivate *priv = combo_box->priv;
2953 GtkTreeModel *model = priv->model;
2954 GtkWidget *item, *submenu, *subitem, *separator;
2956 gboolean is_separator;
2961 n_children = gtk_tree_model_iter_n_children (model, parent);
2964 for (i = 0; i < n_children; i++)
2966 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2968 if (priv->row_separator_func)
2969 is_separator = priv->row_separator_func (priv->model, &iter,
2970 priv->row_separator_data);
2972 is_separator = FALSE;
2976 item = gtk_separator_menu_item_new ();
2977 path = gtk_tree_model_get_path (model, &iter);
2978 g_object_set_data_full (G_OBJECT (item),
2979 I_("gtk-combo-box-item-path"),
2980 gtk_tree_row_reference_new (model, path),
2981 (GDestroyNotify)gtk_tree_row_reference_free);
2982 gtk_tree_path_free (path);
2986 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2987 if (gtk_tree_model_iter_has_child (model, &iter))
2989 submenu = gtk_menu_new ();
2990 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
2991 gtk_widget_show (submenu);
2992 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2994 /* Ugly - since menus can only activate leafs, we have to
2995 * duplicate the item inside the submenu.
2997 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2998 separator = gtk_separator_menu_item_new ();
2999 gtk_widget_show (subitem);
3000 gtk_widget_show (separator);
3001 g_signal_connect (subitem, "activate",
3002 G_CALLBACK (gtk_combo_box_menu_item_activate),
3004 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
3005 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
3007 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
3009 g_signal_connect (item, "activate",
3010 G_CALLBACK (gtk_combo_box_menu_item_activate),
3014 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3015 if (priv->wrap_width && menu == priv->popup_widget)
3016 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3017 gtk_widget_show (item);
3024 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3026 GtkComboBoxPrivate *priv = combo_box->priv;
3028 g_signal_handlers_disconnect_matched (priv->button,
3029 G_SIGNAL_MATCH_DATA,
3031 gtk_combo_box_menu_button_press, NULL);
3032 g_signal_handlers_disconnect_matched (priv->button,
3033 G_SIGNAL_MATCH_DATA,
3035 gtk_combo_box_button_state_changed, combo_box);
3037 /* unparent will remove our latest ref */
3038 gtk_widget_unparent (priv->button);
3041 priv->button = NULL;
3043 priv->separator = NULL;
3045 g_object_unref (priv->column);
3046 priv->column = NULL;
3048 /* changing the popup window will unref the menu and the children */
3056 menu_occupied (GtkMenu *menu,
3060 guint bottom_attach)
3064 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3068 gtk_container_child_get (GTK_CONTAINER (menu),
3072 "bottom-attach", &b,
3076 /* look if this item intersects with the given coordinates */
3077 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3085 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3090 GtkComboBoxPrivate *priv = combo_box->priv;
3091 gint current_col = 0, current_row = 0;
3092 gint rows = 1, cols = 1;
3093 GtkWidget *menu = priv->popup_widget;
3095 if (!GTK_IS_MENU_SHELL (menu))
3098 if (priv->col_column == -1 &&
3099 priv->row_column == -1 &&
3102 gtk_container_child_get (GTK_CONTAINER (menu),
3104 "right-attach", ¤t_col,
3105 "top-attach", ¤t_row,
3107 if (current_col + cols > priv->wrap_width)
3115 if (priv->col_column != -1)
3116 gtk_tree_model_get (priv->model, iter,
3117 priv->col_column, &cols,
3119 if (priv->row_column != -1)
3120 gtk_tree_model_get (priv->model, iter,
3121 priv->row_column, &rows,
3126 if (current_col + cols > priv->wrap_width)
3132 if (!menu_occupied (GTK_MENU (menu),
3133 current_col, current_col + cols,
3134 current_row, current_row + rows))
3141 /* set attach props */
3142 gtk_menu_attach (GTK_MENU (menu), item,
3143 current_col, current_col + cols,
3144 current_row, current_row + rows);
3148 gtk_combo_box_relayout (GtkComboBox *combo_box)
3153 menu = combo_box->priv->popup_widget;
3155 /* do nothing unless we are in menu style and realized */
3156 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3159 list = gtk_container_get_children (GTK_CONTAINER (menu));
3161 for (j = g_list_last (list); j; j = j->prev)
3162 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3164 gtk_combo_box_menu_fill (combo_box);
3171 gtk_combo_box_menu_button_press (GtkWidget *widget,
3172 GdkEventButton *event,
3175 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3176 GtkComboBoxPrivate *priv = combo_box->priv;
3178 if (GTK_IS_MENU (priv->popup_widget) &&
3179 event->type == GDK_BUTTON_PRESS && event->button == 1)
3181 if (priv->focus_on_click &&
3182 !gtk_widget_has_focus (priv->button))
3183 gtk_widget_grab_focus (priv->button);
3185 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3194 gtk_combo_box_menu_item_activate (GtkWidget *item,
3197 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3198 GtkWidget *cell_view;
3202 cell_view = gtk_bin_get_child (GTK_BIN (item));
3204 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3206 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3208 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3210 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3211 gtk_combo_box_set_active_iter (combo_box, &iter);
3214 gtk_tree_path_free (path);
3216 g_object_set (combo_box,
3217 "editing-canceled", FALSE,
3222 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3225 gboolean sensitive = TRUE; /* fool code checkers */
3227 if (!combo_box->priv->button)
3230 switch (combo_box->priv->button_sensitivity)
3232 case GTK_SENSITIVITY_ON:
3235 case GTK_SENSITIVITY_OFF:
3238 case GTK_SENSITIVITY_AUTO:
3239 sensitive = combo_box->priv->model &&
3240 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3243 g_assert_not_reached ();
3247 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3249 /* In list-mode, we also need to update sensitivity of the event box */
3250 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3251 && combo_box->priv->cell_view)
3252 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3256 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3261 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3263 if (combo_box->priv->tree_view)
3264 gtk_combo_box_list_popup_resize (combo_box);
3266 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3268 gtk_combo_box_update_sensitivity (combo_box);
3272 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3276 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3277 GtkComboBoxPrivate *priv = combo_box->priv;
3279 if (!gtk_tree_row_reference_valid (priv->active_row))
3281 if (priv->cell_view)
3282 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3283 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3286 if (priv->tree_view)
3287 gtk_combo_box_list_popup_resize (combo_box);
3289 gtk_combo_box_menu_row_deleted (model, path, user_data);
3291 gtk_combo_box_update_sensitivity (combo_box);
3295 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3301 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3303 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3305 if (!combo_box->priv->tree_view)
3306 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3310 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3315 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3316 GtkComboBoxPrivate *priv = combo_box->priv;
3317 GtkTreePath *active_path;
3319 /* FIXME this belongs to GtkCellView */
3320 if (gtk_tree_row_reference_valid (priv->active_row))
3322 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3323 if (gtk_tree_path_compare (path, active_path) == 0 &&
3325 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3326 gtk_tree_path_free (active_path);
3329 if (priv->tree_view)
3330 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3332 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3336 list_popup_resize_idle (gpointer user_data)
3338 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3339 GtkComboBoxPrivate *priv = combo_box->priv;
3340 gint x, y, width, height;
3342 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3344 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3346 gtk_widget_set_size_request (priv->popup_window, width, height);
3347 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3350 priv->resize_idle_id = 0;
3356 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3358 GtkComboBoxPrivate *priv = combo_box->priv;
3360 if (!priv->resize_idle_id)
3361 priv->resize_idle_id =
3362 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3366 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3371 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3373 gtk_combo_box_list_popup_resize (combo_box);
3378 find_menu_by_path (GtkWidget *menu,
3380 gboolean skip_first)
3386 GtkTreeRowReference *mref;
3390 list = gtk_container_get_children (GTK_CONTAINER (menu));
3393 for (i = list; i; i = i->next)
3395 child = gtk_bin_get_child (i->data);
3396 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3398 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3401 else if (!gtk_tree_row_reference_valid (mref))
3404 mpath = gtk_tree_row_reference_get_path (mref);
3406 else if (GTK_IS_CELL_VIEW (child))
3414 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (child));
3419 /* this case is necessary, since the row reference of
3420 * the cell view may already be updated after a deletion
3427 if (gtk_tree_path_compare (mpath, path) == 0)
3429 gtk_tree_path_free (mpath);
3433 if (gtk_tree_path_is_ancestor (mpath, path))
3435 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3436 if (submenu != NULL)
3438 gtk_tree_path_free (mpath);
3439 item = find_menu_by_path (submenu, path, TRUE);
3443 gtk_tree_path_free (mpath);
3453 dump_menu_tree (GtkWidget *menu,
3460 list = gtk_container_get_children (GTK_CONTAINER (menu));
3461 for (i = list; i; i = i->next)
3463 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3465 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3466 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3467 gtk_tree_path_free (path);
3469 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3470 if (submenu != NULL)
3471 dump_menu_tree (submenu, level + 1);
3480 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3485 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3486 GtkComboBoxPrivate *priv = combo_box->priv;
3488 GtkWidget *item, *menu, *separator;
3492 gboolean is_separator;
3494 if (!priv->popup_widget)
3497 depth = gtk_tree_path_get_depth (path);
3498 pos = gtk_tree_path_get_indices (path)[depth - 1];
3501 ppath = gtk_tree_path_copy (path);
3502 gtk_tree_path_up (ppath);
3503 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3504 gtk_tree_path_free (ppath);
3506 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3509 menu = gtk_menu_new ();
3510 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3511 gtk_widget_show (menu);
3512 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3514 /* Ugly - since menus can only activate leaves, we have to
3515 * duplicate the item inside the submenu.
3517 gtk_tree_model_iter_parent (model, &piter, iter);
3518 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3519 separator = gtk_separator_menu_item_new ();
3520 g_signal_connect (item, "activate",
3521 G_CALLBACK (gtk_combo_box_menu_item_activate),
3523 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3524 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3525 if (cell_view_is_sensitive (GTK_CELL_VIEW (gtk_bin_get_child (GTK_BIN (item)))))
3527 gtk_widget_show (item);
3528 gtk_widget_show (separator);
3535 menu = priv->popup_widget;
3536 if (priv->add_tearoffs)
3540 if (priv->row_separator_func)
3541 is_separator = priv->row_separator_func (model, iter,
3542 priv->row_separator_data);
3544 is_separator = FALSE;
3548 item = gtk_separator_menu_item_new ();
3549 g_object_set_data_full (G_OBJECT (item),
3550 I_("gtk-combo-box-item-path"),
3551 gtk_tree_row_reference_new (model, path),
3552 (GDestroyNotify)gtk_tree_row_reference_free);
3556 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3558 g_signal_connect (item, "activate",
3559 G_CALLBACK (gtk_combo_box_menu_item_activate),
3563 gtk_widget_show (item);
3564 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3568 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3572 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3573 GtkComboBoxPrivate *priv = combo_box->priv;
3577 if (!priv->popup_widget)
3580 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3581 menu = gtk_widget_get_parent (item);
3582 gtk_container_remove (GTK_CONTAINER (menu), item);
3584 if (gtk_tree_path_get_depth (path) > 1)
3586 GtkTreePath *parent_path;
3590 parent_path = gtk_tree_path_copy (path);
3591 gtk_tree_path_up (parent_path);
3592 gtk_tree_model_get_iter (model, &iter, parent_path);
3594 if (!gtk_tree_model_iter_has_child (model, &iter))
3596 parent = find_menu_by_path (priv->popup_widget,
3597 parent_path, FALSE);
3598 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3604 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3610 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3612 gtk_combo_box_relayout (combo_box);
3616 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3621 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3622 GtkComboBoxPrivate *priv = combo_box->priv;
3624 gboolean is_separator;
3626 if (!priv->popup_widget)
3629 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3631 if (priv->row_separator_func)
3632 is_separator = priv->row_separator_func (model, iter,
3633 priv->row_separator_data);
3635 is_separator = FALSE;
3637 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3639 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3640 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3643 if (priv->wrap_width &&
3644 gtk_widget_get_parent (item) == priv->popup_widget)
3646 GtkWidget *pitem = NULL;
3649 prev = gtk_tree_path_copy (path);
3651 if (gtk_tree_path_prev (prev))
3652 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3654 gtk_tree_path_free (prev);
3656 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3658 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3663 "bottom-attach", -1,
3666 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3669 gtk_combo_box_update_requested_width (combo_box, path);
3677 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3679 GtkComboBoxPrivate *priv = combo_box->priv;
3680 GtkTreeSelection *sel;
3683 GtkWidget *widget = GTK_WIDGET (combo_box);
3685 priv->button = gtk_toggle_button_new ();
3686 child = gtk_bin_get_child (GTK_BIN (combo_box));
3687 gtk_widget_set_parent (priv->button,
3688 gtk_widget_get_parent (child));
3689 g_signal_connect (priv->button, "button-press-event",
3690 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3691 g_signal_connect (priv->button, "toggled",
3692 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3694 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3695 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3696 priv->separator = NULL;
3697 gtk_widget_show_all (priv->button);
3699 if (priv->cell_view)
3701 style = gtk_widget_get_style (widget);
3702 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3703 &style->base[gtk_widget_get_state (widget)]);
3705 priv->box = gtk_event_box_new ();
3706 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3709 if (priv->has_frame)
3711 priv->cell_view_frame = gtk_frame_new (NULL);
3712 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3717 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3718 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3722 gtk_widget_set_parent (priv->cell_view_frame,
3723 gtk_widget_get_parent (child));
3724 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3725 gtk_widget_show_all (priv->cell_view_frame);
3727 g_signal_connect (priv->box, "button-press-event",
3728 G_CALLBACK (gtk_combo_box_list_button_pressed),
3732 priv->tree_view = gtk_tree_view_new ();
3733 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3734 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3735 gtk_tree_selection_set_select_function (sel,
3736 gtk_combo_box_list_select_func,
3738 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3740 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3742 if (priv->row_separator_func)
3743 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3744 priv->row_separator_func,
3745 priv->row_separator_data,
3748 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3750 priv->column = gtk_tree_view_column_new ();
3751 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3754 gtk_combo_box_sync_cells (combo_box,
3755 GTK_CELL_LAYOUT (priv->column));
3757 if (gtk_tree_row_reference_valid (priv->active_row))
3761 path = gtk_tree_row_reference_get_path (priv->active_row);
3762 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3764 gtk_tree_path_free (path);
3767 /* set sample/popup widgets */
3768 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3770 g_signal_connect (priv->tree_view, "key-press-event",
3771 G_CALLBACK (gtk_combo_box_list_key_press),
3773 g_signal_connect (priv->tree_view, "enter-notify-event",
3774 G_CALLBACK (gtk_combo_box_list_enter_notify),
3776 g_signal_connect (priv->tree_view, "row-expanded",
3777 G_CALLBACK (gtk_combo_box_model_row_expanded),
3779 g_signal_connect (priv->tree_view, "row-collapsed",
3780 G_CALLBACK (gtk_combo_box_model_row_expanded),
3782 g_signal_connect (priv->popup_window, "button-press-event",
3783 G_CALLBACK (gtk_combo_box_list_button_pressed),
3785 g_signal_connect (priv->popup_window, "button-release-event",
3786 G_CALLBACK (gtk_combo_box_list_button_released),
3789 gtk_widget_show (priv->tree_view);
3791 gtk_combo_box_update_sensitivity (combo_box);
3795 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3797 GtkComboBoxPrivate *priv = combo_box->priv;
3799 /* disconnect signals */
3800 g_signal_handlers_disconnect_matched (priv->tree_view,
3801 G_SIGNAL_MATCH_DATA,
3802 0, 0, NULL, NULL, combo_box);
3803 g_signal_handlers_disconnect_matched (priv->button,
3804 G_SIGNAL_MATCH_DATA,
3806 gtk_combo_box_list_button_pressed,
3808 g_signal_handlers_disconnect_matched (priv->popup_window,
3809 G_SIGNAL_MATCH_DATA,
3811 gtk_combo_box_list_button_pressed,
3813 g_signal_handlers_disconnect_matched (priv->popup_window,
3814 G_SIGNAL_MATCH_DATA,
3816 gtk_combo_box_list_button_released,
3819 g_signal_handlers_disconnect_matched (priv->popup_window,
3820 G_SIGNAL_MATCH_DATA,
3822 gtk_combo_box_child_show,
3825 g_signal_handlers_disconnect_matched (priv->popup_window,
3826 G_SIGNAL_MATCH_DATA,
3828 gtk_combo_box_child_hide,
3832 g_signal_handlers_disconnect_matched (priv->box,
3833 G_SIGNAL_MATCH_DATA,
3835 gtk_combo_box_list_button_pressed,
3838 /* destroy things (unparent will kill the latest ref from us)
3839 * last unref on button will destroy the arrow
3841 gtk_widget_unparent (priv->button);
3842 priv->button = NULL;
3845 if (priv->cell_view)
3847 g_object_set (priv->cell_view,
3848 "background-set", FALSE,
3852 if (priv->cell_view_frame)
3854 gtk_widget_unparent (priv->cell_view_frame);
3855 priv->cell_view_frame = NULL;
3859 if (priv->scroll_timer)
3861 g_source_remove (priv->scroll_timer);
3862 priv->scroll_timer = 0;
3865 if (priv->resize_idle_id)
3867 g_source_remove (priv->resize_idle_id);
3868 priv->resize_idle_id = 0;
3871 gtk_widget_destroy (priv->tree_view);
3873 priv->tree_view = NULL;
3874 if (priv->popup_widget)
3876 g_object_unref (priv->popup_widget);
3877 priv->popup_widget = NULL;
3884 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3885 GdkEventButton *event,
3888 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3889 GtkComboBoxPrivate *priv = combo_box->priv;
3891 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3893 if (ewidget == priv->popup_window)
3896 if ((ewidget != priv->button && ewidget != priv->box) ||
3897 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3900 if (priv->focus_on_click &&
3901 !gtk_widget_has_focus (priv->button))
3902 gtk_widget_grab_focus (priv->button);
3904 gtk_combo_box_popup_for_device (combo_box, event->device);
3906 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3908 priv->auto_scroll = FALSE;
3909 if (priv->scroll_timer == 0)
3910 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3911 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3914 priv->popup_in_progress = TRUE;
3920 gtk_combo_box_list_button_released (GtkWidget *widget,
3921 GdkEventButton *event,
3925 GtkTreePath *path = NULL;
3928 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3929 GtkComboBoxPrivate *priv = combo_box->priv;
3931 gboolean popup_in_progress = FALSE;
3933 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3935 if (priv->popup_in_progress)
3937 popup_in_progress = TRUE;
3938 priv->popup_in_progress = FALSE;
3941 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3943 if (priv->scroll_timer)
3945 g_source_remove (priv->scroll_timer);
3946 priv->scroll_timer = 0;
3949 if (ewidget != priv->tree_view)
3951 if ((ewidget == priv->button ||
3952 ewidget == priv->box) &&
3953 !popup_in_progress &&
3954 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3956 gtk_combo_box_popdown (combo_box);
3960 /* released outside treeview */
3961 if (ewidget != priv->button && ewidget != priv->box)
3963 gtk_combo_box_popdown (combo_box);
3971 /* select something cool */
3972 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3978 return TRUE; /* clicked outside window? */
3980 gtk_tree_model_get_iter (priv->model, &iter, path);
3981 gtk_tree_path_free (path);
3983 gtk_combo_box_popdown (combo_box);
3985 if (tree_column_row_is_sensitive (combo_box, &iter))
3986 gtk_combo_box_set_active_iter (combo_box, &iter);
3992 gtk_combo_box_menu_key_press (GtkWidget *widget,
3996 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3998 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
4000 /* The menu hasn't managed the
4001 * event, forward it to the combobox
4003 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
4010 gtk_combo_box_list_key_press (GtkWidget *widget,
4014 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4017 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
4018 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
4020 GtkTreeModel *model = NULL;
4022 gtk_combo_box_popdown (combo_box);
4024 if (combo_box->priv->model)
4026 GtkTreeSelection *sel;
4028 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4030 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4031 gtk_combo_box_set_active_iter (combo_box, &iter);
4037 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
4039 /* The list hasn't managed the
4040 * event, forward it to the combobox
4042 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
4049 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4054 GtkAllocation allocation;
4055 GtkWidget *tree_view = combo_box->priv->tree_view;
4058 gtk_widget_get_allocation (tree_view, &allocation);
4060 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4061 if (adj && adj->upper - adj->lower > adj->page_size)
4063 if (x <= allocation.x &&
4064 adj->lower < adj->value)
4066 value = adj->value - (allocation.x - x + 1);
4067 gtk_adjustment_set_value (adj, value);
4069 else if (x >= allocation.x + allocation.width &&
4070 adj->upper - adj->page_size > adj->value)
4072 value = adj->value + (x - allocation.x - allocation.width + 1);
4073 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4077 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4078 if (adj && adj->upper - adj->lower > adj->page_size)
4080 if (y <= allocation.y &&
4081 adj->lower < adj->value)
4083 value = adj->value - (allocation.y - y + 1);
4084 gtk_adjustment_set_value (adj, value);
4086 else if (y >= allocation.height &&
4087 adj->upper - adj->page_size > adj->value)
4089 value = adj->value + (y - allocation.height + 1);
4090 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4096 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4098 GtkComboBoxPrivate *priv = combo_box->priv;
4101 if (priv->auto_scroll)
4103 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
4106 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4113 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4114 GdkEventCrossing *event,
4117 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4119 combo_box->priv->auto_scroll = TRUE;
4125 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4126 GtkTreeModel *model,
4128 gboolean path_currently_selected,
4132 gboolean sensitive = FALSE;
4134 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4136 GList *cells, *cell;
4137 gboolean cell_sensitive, cell_visible;
4139 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4141 if (!column->visible)
4144 gtk_tree_model_get_iter (model, &iter, path);
4145 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4148 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4151 g_object_get (cell->data,
4152 "sensitive", &cell_sensitive,
4153 "visible", &cell_visible,
4156 if (cell_visible && cell_sensitive)
4161 g_list_free (cells);
4163 sensitive = cell_sensitive;
4170 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4175 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4177 gtk_combo_box_update_requested_width (combo_box, path);
4181 * GtkCellLayout implementation
4185 pack_start_recurse (GtkWidget *menu,
4186 GtkCellRenderer *cell,
4193 list = gtk_container_get_children (GTK_CONTAINER (menu));
4194 for (i = list; i; i = i->next)
4196 child = gtk_bin_get_child (GTK_BIN (i->data));
4197 if (GTK_IS_CELL_LAYOUT (child))
4198 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (child),
4201 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4202 if (submenu != NULL)
4203 pack_start_recurse (submenu, cell, expand);
4210 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4211 GtkCellRenderer *cell,
4214 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4215 ComboCellInfo *info;
4216 GtkComboBoxPrivate *priv;
4218 priv = combo_box->priv;
4220 g_object_ref_sink (cell);
4222 info = g_slice_new0 (ComboCellInfo);
4224 info->expand = expand;
4225 info->pack = GTK_PACK_START;
4227 priv->cells = g_slist_append (priv->cells, info);
4229 if (priv->cell_view)
4231 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4237 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4239 if (GTK_IS_MENU (priv->popup_widget))
4240 pack_start_recurse (priv->popup_widget, cell, expand);
4244 pack_end_recurse (GtkWidget *menu,
4245 GtkCellRenderer *cell,
4252 list = gtk_container_get_children (GTK_CONTAINER (menu));
4253 for (i = list; i; i = i->next)
4255 child = gtk_bin_get_child (GTK_BIN (i->data));
4256 if (GTK_IS_CELL_LAYOUT (child))
4257 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (child),
4260 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4261 if (submenu != NULL)
4262 pack_end_recurse (submenu, cell, expand);
4269 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4270 GtkCellRenderer *cell,
4273 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4274 ComboCellInfo *info;
4275 GtkComboBoxPrivate *priv;
4277 priv = combo_box->priv;
4279 g_object_ref_sink (cell);
4281 info = g_slice_new0 (ComboCellInfo);
4283 info->expand = expand;
4284 info->pack = GTK_PACK_END;
4286 priv->cells = g_slist_append (priv->cells, info);
4288 if (priv->cell_view)
4289 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4293 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4295 if (GTK_IS_MENU (priv->popup_widget))
4296 pack_end_recurse (priv->popup_widget, cell, expand);
4300 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4302 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4304 GList *retval = NULL;
4306 for (list = combo_box->priv->cells; list; list = list->next)
4308 ComboCellInfo *info = (ComboCellInfo *)list->data;
4310 retval = g_list_prepend (retval, info->cell);
4313 return g_list_reverse (retval);
4317 clear_recurse (GtkWidget *menu)
4323 list = gtk_container_get_children (GTK_CONTAINER (menu));
4324 for (i = list; i; i = i->next)
4326 child = gtk_bin_get_child (GTK_BIN (i->data));
4327 if (GTK_IS_CELL_LAYOUT (child))
4328 gtk_cell_layout_clear (GTK_CELL_LAYOUT (child));
4330 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4331 if (submenu != NULL)
4332 clear_recurse (submenu);
4339 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4341 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4342 GtkComboBoxPrivate *priv = combo_box->priv;
4345 if (priv->cell_view)
4346 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4349 gtk_tree_view_column_clear (priv->column);
4351 for (i = priv->cells; i; i = i->next)
4353 ComboCellInfo *info = (ComboCellInfo *)i->data;
4355 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4356 g_object_unref (info->cell);
4357 g_slice_free (ComboCellInfo, info);
4360 g_slist_free (priv->cells);
4363 if (GTK_IS_MENU (priv->popup_widget))
4364 clear_recurse (priv->popup_widget);
4368 add_attribute_recurse (GtkWidget *menu,
4369 GtkCellRenderer *cell,
4370 const gchar *attribute,
4377 list = gtk_container_get_children (GTK_CONTAINER (menu));
4378 for (i = list; i; i = i->next)
4380 child = gtk_bin_get_child (GTK_BIN (i->data));
4381 if (GTK_IS_CELL_LAYOUT (child))
4382 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (child),
4383 cell, attribute, column);
4385 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4386 if (submenu != NULL)
4387 add_attribute_recurse (submenu, cell, attribute, column);
4394 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4395 GtkCellRenderer *cell,
4396 const gchar *attribute,
4399 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4400 ComboCellInfo *info;
4402 info = gtk_combo_box_get_cell_info (combo_box, cell);
4403 g_return_if_fail (info != NULL);
4405 info->attributes = g_slist_prepend (info->attributes,
4406 GINT_TO_POINTER (column));
4407 info->attributes = g_slist_prepend (info->attributes,
4408 g_strdup (attribute));
4410 if (combo_box->priv->cell_view)
4411 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4412 cell, attribute, column);
4414 if (combo_box->priv->column)
4415 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4416 cell, attribute, column);
4418 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4419 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4420 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4424 combo_cell_data_func (GtkCellLayout *cell_layout,
4425 GtkCellRenderer *cell,
4426 GtkTreeModel *tree_model,
4430 ComboCellInfo *info = (ComboCellInfo *)data;
4431 GtkWidget *parent = NULL;
4436 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4438 if (GTK_IS_WIDGET (cell_layout))
4439 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4441 if (GTK_IS_MENU_ITEM (parent) &&
4442 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4443 g_object_set (cell, "sensitive", TRUE, NULL);
4448 set_cell_data_func_recurse (GtkWidget *menu,
4449 GtkCellRenderer *cell,
4450 ComboCellInfo *info)
4454 GtkWidget *cell_view;
4456 list = gtk_container_get_children (GTK_CONTAINER (menu));
4457 for (i = list; i; i = i->next)
4459 cell_view = gtk_bin_get_child (GTK_BIN (i->data));
4460 if (GTK_IS_CELL_LAYOUT (cell_view))
4462 /* Override sensitivity for inner nodes; we don't
4463 * want menuitems with submenus to appear insensitive
4465 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4467 combo_cell_data_func,
4469 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4470 if (submenu != NULL)
4471 set_cell_data_func_recurse (submenu, cell, info);
4479 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4480 GtkCellRenderer *cell,
4481 GtkCellLayoutDataFunc func,
4483 GDestroyNotify destroy)
4485 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4486 GtkComboBoxPrivate *priv = combo_box->priv;
4487 ComboCellInfo *info;
4489 info = gtk_combo_box_get_cell_info (combo_box, cell);
4490 g_return_if_fail (info != NULL);
4494 GDestroyNotify d = info->destroy;
4496 info->destroy = NULL;
4497 d (info->func_data);
4501 info->func_data = func_data;
4502 info->destroy = destroy;
4504 if (priv->cell_view)
4505 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4508 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4510 if (GTK_IS_MENU (priv->popup_widget))
4511 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4513 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4517 clear_attributes_recurse (GtkWidget *menu,
4518 GtkCellRenderer *cell)
4524 list = gtk_container_get_children (GTK_CONTAINER (menu));
4525 for (i = list; i; i = i->next)
4527 child = gtk_bin_get_child (GTK_BIN (i->data));
4528 if (GTK_IS_CELL_LAYOUT (child))
4529 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (child), cell);
4531 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4532 if (submenu != NULL)
4533 clear_attributes_recurse (submenu, cell);
4540 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4541 GtkCellRenderer *cell)
4543 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4544 GtkComboBoxPrivate *priv;
4545 ComboCellInfo *info;
4548 priv = combo_box->priv;
4550 info = gtk_combo_box_get_cell_info (combo_box, cell);
4551 g_return_if_fail (info != NULL);
4553 list = info->attributes;
4554 while (list && list->next)
4556 g_free (list->data);
4557 list = list->next->next;
4559 g_slist_free (info->attributes);
4560 info->attributes = NULL;
4562 if (priv->cell_view)
4563 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4566 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4568 if (GTK_IS_MENU (priv->popup_widget))
4569 clear_attributes_recurse (priv->popup_widget, cell);
4571 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4575 reorder_recurse (GtkWidget *menu,
4576 GtkCellRenderer *cell,
4583 list = gtk_container_get_children (GTK_CONTAINER (menu));
4584 for (i = list; i; i = i->next)
4586 child = gtk_bin_get_child (GTK_BIN (i->data));
4587 if (GTK_IS_CELL_LAYOUT (child))
4588 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (child),
4591 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4592 if (submenu != NULL)
4593 reorder_recurse (submenu, cell, position);
4600 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4601 GtkCellRenderer *cell,
4604 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4605 GtkComboBoxPrivate *priv;
4606 ComboCellInfo *info;
4609 priv = combo_box->priv;
4611 info = gtk_combo_box_get_cell_info (combo_box, cell);
4613 g_return_if_fail (info != NULL);
4614 g_return_if_fail (position >= 0);
4616 link = g_slist_find (priv->cells, info);
4618 g_return_if_fail (link != NULL);
4620 priv->cells = g_slist_delete_link (priv->cells, link);
4621 priv->cells = g_slist_insert (priv->cells, info, position);
4623 if (priv->cell_view)
4624 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4628 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4631 if (GTK_IS_MENU (priv->popup_widget))
4632 reorder_recurse (priv->popup_widget, cell, position);
4634 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4642 * gtk_combo_box_new:
4644 * Creates a new empty #GtkComboBox.
4646 * Return value: A new #GtkComboBox.
4651 gtk_combo_box_new (void)
4653 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4657 * gtk_combo_box_new_with_model:
4658 * @model: A #GtkTreeModel.
4660 * Creates a new #GtkComboBox with the model initialized to @model.
4662 * Return value: A new #GtkComboBox.
4667 gtk_combo_box_new_with_model (GtkTreeModel *model)
4669 GtkComboBox *combo_box;
4671 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4673 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4675 return GTK_WIDGET (combo_box);
4679 * gtk_combo_box_get_wrap_width:
4680 * @combo_box: A #GtkComboBox
4682 * Returns the wrap width which is used to determine the number of columns
4683 * for the popup menu. If the wrap width is larger than 1, the combo box
4686 * Returns: the wrap width.
4691 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4693 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4695 return combo_box->priv->wrap_width;
4699 * gtk_combo_box_set_wrap_width:
4700 * @combo_box: A #GtkComboBox
4701 * @width: Preferred number of columns
4703 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4704 * the preferred number of columns when you want the popup to be layed out
4710 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4713 GtkComboBoxPrivate *priv;
4715 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4716 g_return_if_fail (width >= 0);
4718 priv = combo_box->priv;
4720 if (width != priv->wrap_width)
4722 priv->wrap_width = width;
4724 gtk_combo_box_check_appearance (combo_box);
4725 gtk_combo_box_relayout (combo_box);
4727 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4732 * gtk_combo_box_get_row_span_column:
4733 * @combo_box: A #GtkComboBox
4735 * Returns the column with row span information for @combo_box.
4737 * Returns: the row span column.
4742 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4744 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4746 return combo_box->priv->row_column;
4750 * gtk_combo_box_set_row_span_column:
4751 * @combo_box: A #GtkComboBox.
4752 * @row_span: A column in the model passed during construction.
4754 * Sets the column with row span information for @combo_box to be @row_span.
4755 * The row span column contains integers which indicate how many rows
4756 * an item should span.
4761 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4764 GtkComboBoxPrivate *priv;
4767 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4769 priv = combo_box->priv;
4771 col = gtk_tree_model_get_n_columns (priv->model);
4772 g_return_if_fail (row_span >= -1 && row_span < col);
4774 if (row_span != priv->row_column)
4776 priv->row_column = row_span;
4778 gtk_combo_box_relayout (combo_box);
4780 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4785 * gtk_combo_box_get_column_span_column:
4786 * @combo_box: A #GtkComboBox
4788 * Returns the column with column span information for @combo_box.
4790 * Returns: the column span column.
4795 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4797 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4799 return combo_box->priv->col_column;
4803 * gtk_combo_box_set_column_span_column:
4804 * @combo_box: A #GtkComboBox
4805 * @column_span: A column in the model passed during construction
4807 * Sets the column with column span information for @combo_box to be
4808 * @column_span. The column span column contains integers which indicate
4809 * how many columns an item should span.
4814 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4817 GtkComboBoxPrivate *priv;
4820 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4822 priv = combo_box->priv;
4824 col = gtk_tree_model_get_n_columns (priv->model);
4825 g_return_if_fail (column_span >= -1 && column_span < col);
4827 if (column_span != priv->col_column)
4829 priv->col_column = column_span;
4831 gtk_combo_box_relayout (combo_box);
4833 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4838 * gtk_combo_box_get_active:
4839 * @combo_box: A #GtkComboBox
4841 * Returns the index of the currently active item, or -1 if there's no
4842 * active item. If the model is a non-flat treemodel, and the active item
4843 * is not an immediate child of the root of the tree, this function returns
4844 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4845 * <literal>path</literal> is the #GtkTreePath of the active item.
4847 * Return value: An integer which is the index of the currently active item,
4848 * or -1 if there's no active item.
4853 gtk_combo_box_get_active (GtkComboBox *combo_box)
4855 GtkComboBoxPrivate *priv;
4858 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4860 priv = combo_box->priv;
4862 if (gtk_tree_row_reference_valid (priv->active_row))
4866 path = gtk_tree_row_reference_get_path (priv->active_row);
4867 result = gtk_tree_path_get_indices (path)[0];
4868 gtk_tree_path_free (path);
4877 * gtk_combo_box_set_active:
4878 * @combo_box: A #GtkComboBox
4879 * @index_: An index in the model passed during construction, or -1 to have
4882 * Sets the active item of @combo_box to be the item at @index.
4887 gtk_combo_box_set_active (GtkComboBox *combo_box,
4890 GtkTreePath *path = NULL;
4891 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4892 g_return_if_fail (index_ >= -1);
4894 if (combo_box->priv->model == NULL)
4896 /* Save index, in case the model is set after the index */
4897 combo_box->priv->active = index_;
4903 path = gtk_tree_path_new_from_indices (index_, -1);
4905 gtk_combo_box_set_active_internal (combo_box, path);
4908 gtk_tree_path_free (path);
4912 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4915 GtkComboBoxPrivate *priv = combo_box->priv;
4916 GtkTreePath *active_path;
4919 /* Remember whether the initially active row is valid. */
4920 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4922 if (path && is_valid_row_reference)
4924 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4925 path_cmp = gtk_tree_path_compare (path, active_path);
4926 gtk_tree_path_free (active_path);
4931 if (priv->active_row)
4933 gtk_tree_row_reference_free (priv->active_row);
4934 priv->active_row = NULL;
4939 if (priv->tree_view)
4940 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4943 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4945 if (GTK_IS_MENU (menu))
4946 gtk_menu_set_active (menu, -1);
4949 if (priv->cell_view)
4950 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4953 * Do not emit a "changed" signal when an already invalid selection was
4954 * now set to invalid.
4956 if (!is_valid_row_reference)
4962 gtk_tree_row_reference_new (priv->model, path);
4964 if (priv->tree_view)
4966 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4969 else if (GTK_IS_MENU (priv->popup_widget))
4971 /* FIXME handle nested menus better */
4972 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4973 gtk_tree_path_get_indices (path)[0]);
4976 if (priv->cell_view)
4977 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4981 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4982 g_object_notify (G_OBJECT (combo_box), "active");
4987 * gtk_combo_box_get_active_iter:
4988 * @combo_box: A #GtkComboBox
4989 * @iter: (out): The uninitialized #GtkTreeIter
4991 * Sets @iter to point to the current active item, if it exists.
4993 * Return value: %TRUE, if @iter was set
4998 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
5004 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5006 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
5009 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
5010 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
5011 gtk_tree_path_free (path);
5017 * gtk_combo_box_set_active_iter:
5018 * @combo_box: A #GtkComboBox
5019 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5021 * Sets the current active item to be the one referenced by @iter, or
5022 * unsets the active item if @iter is %NULL.
5027 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5030 GtkTreePath *path = NULL;
5032 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5035 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5037 gtk_combo_box_set_active_internal (combo_box, path);
5038 gtk_tree_path_free (path);
5042 * gtk_combo_box_set_model:
5043 * @combo_box: A #GtkComboBox
5044 * @model: (allow-none): A #GtkTreeModel
5046 * Sets the model used by @combo_box to be @model. Will unset a previously set
5047 * model (if applicable). If model is %NULL, then it will unset the model.
5049 * Note that this function does not clear the cell renderers, you have to
5050 * call gtk_cell_layout_clear() yourself if you need to set up different
5051 * cell renderers for the new model.
5056 gtk_combo_box_set_model (GtkComboBox *combo_box,
5057 GtkTreeModel *model)
5059 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5060 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5062 if (model == combo_box->priv->model)
5065 gtk_combo_box_unset_model (combo_box);
5070 combo_box->priv->model = model;
5071 g_object_ref (combo_box->priv->model);
5073 combo_box->priv->inserted_id =
5074 g_signal_connect (combo_box->priv->model, "row-inserted",
5075 G_CALLBACK (gtk_combo_box_model_row_inserted),
5077 combo_box->priv->deleted_id =
5078 g_signal_connect (combo_box->priv->model, "row-deleted",
5079 G_CALLBACK (gtk_combo_box_model_row_deleted),
5081 combo_box->priv->reordered_id =
5082 g_signal_connect (combo_box->priv->model, "rows-reordered",
5083 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5085 combo_box->priv->changed_id =
5086 g_signal_connect (combo_box->priv->model, "row-changed",
5087 G_CALLBACK (gtk_combo_box_model_row_changed),
5090 if (combo_box->priv->tree_view)
5093 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5094 combo_box->priv->model);
5095 gtk_combo_box_list_popup_resize (combo_box);
5100 if (combo_box->priv->popup_widget)
5101 gtk_combo_box_menu_fill (combo_box);
5105 if (combo_box->priv->cell_view)
5106 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5107 combo_box->priv->model);
5109 if (combo_box->priv->active != -1)
5111 /* If an index was set in advance, apply it now */
5112 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5113 combo_box->priv->active = -1;
5117 gtk_combo_box_update_sensitivity (combo_box);
5119 g_object_notify (G_OBJECT (combo_box), "model");
5123 * gtk_combo_box_get_model:
5124 * @combo_box: A #GtkComboBox
5126 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5128 * Return value: (transfer none): A #GtkTreeModel which was passed
5129 * during construction.
5134 gtk_combo_box_get_model (GtkComboBox *combo_box)
5136 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5138 return combo_box->priv->model;
5142 /* convenience API for simple text combos */
5145 * gtk_combo_box_new_text:
5147 * Convenience function which constructs a new text combo box, which is a
5148 * #GtkComboBox just displaying strings. If you use this function to create
5149 * a text combo box, you should only manipulate its data source with the
5150 * following convenience functions: gtk_combo_box_append_text(),
5151 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5152 * gtk_combo_box_remove_text().
5154 * Return value: (transfer none): A new text combo box.
5159 gtk_combo_box_new_text (void)
5161 GtkWidget *combo_box;
5162 GtkCellRenderer *cell;
5163 GtkListStore *store;
5165 store = gtk_list_store_new (1, G_TYPE_STRING);
5166 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5167 g_object_unref (store);
5169 cell = gtk_cell_renderer_text_new ();
5170 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5171 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5179 * gtk_combo_box_append_text:
5180 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5183 * Appends @string to the list of strings stored in @combo_box. Note that
5184 * you can only use this function with combo boxes constructed with
5185 * gtk_combo_box_new_text().
5190 gtk_combo_box_append_text (GtkComboBox *combo_box,
5194 GtkListStore *store;
5196 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5197 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5198 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5200 g_return_if_fail (text != NULL);
5202 store = GTK_LIST_STORE (combo_box->priv->model);
5204 gtk_list_store_append (store, &iter);
5205 gtk_list_store_set (store, &iter, 0, text, -1);
5209 * gtk_combo_box_insert_text:
5210 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5211 * @position: An index to insert @text
5214 * Inserts @string at @position in the list of strings stored in @combo_box.
5215 * Note that you can only use this function with combo boxes constructed
5216 * with gtk_combo_box_new_text().
5221 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5226 GtkListStore *store;
5228 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5229 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5230 g_return_if_fail (position >= 0);
5231 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5233 g_return_if_fail (text != NULL);
5235 store = GTK_LIST_STORE (combo_box->priv->model);
5237 gtk_list_store_insert (store, &iter, position);
5238 gtk_list_store_set (store, &iter, 0, text, -1);
5242 * gtk_combo_box_prepend_text:
5243 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5246 * Prepends @string to the list of strings stored in @combo_box. Note that
5247 * you can only use this function with combo boxes constructed with
5248 * gtk_combo_box_new_text().
5253 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5257 GtkListStore *store;
5259 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5260 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5261 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5263 g_return_if_fail (text != NULL);
5265 store = GTK_LIST_STORE (combo_box->priv->model);
5267 gtk_list_store_prepend (store, &iter);
5268 gtk_list_store_set (store, &iter, 0, text, -1);
5272 * gtk_combo_box_remove_text:
5273 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5274 * @position: Index of the item to remove
5276 * Removes the string at @position from @combo_box. Note that you can only use
5277 * this function with combo boxes constructed with gtk_combo_box_new_text().
5282 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5286 GtkListStore *store;
5288 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5289 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5290 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5292 g_return_if_fail (position >= 0);
5294 store = GTK_LIST_STORE (combo_box->priv->model);
5296 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5298 gtk_list_store_remove (store, &iter);
5302 * gtk_combo_box_get_active_text:
5303 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5305 * Returns the currently active string in @combo_box or %NULL if none
5306 * is selected. Note that you can only use this function with combo
5307 * boxes constructed with gtk_combo_box_new_text() and with
5308 * #GtkComboBoxEntry<!-- -->s.
5310 * Returns: a newly allocated string containing the currently active text.
5311 * Must be freed with g_free().
5316 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5318 GtkComboBoxClass *class;
5320 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5322 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5324 if (class->get_active_text)
5325 return class->get_active_text (combo_box);
5331 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5336 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5337 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5338 == G_TYPE_STRING, NULL);
5340 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5341 gtk_tree_model_get (combo_box->priv->model, &iter,
5348 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5349 GtkScrollType scroll)
5352 GtkTreeIter new_iter;
5353 gboolean active_iter;
5356 if (!combo_box->priv->model)
5358 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5362 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5366 case GTK_SCROLL_STEP_BACKWARD:
5367 case GTK_SCROLL_STEP_UP:
5368 case GTK_SCROLL_STEP_LEFT:
5371 found = tree_prev (combo_box, combo_box->priv->model,
5372 &iter, &new_iter, FALSE);
5375 /* else fall through */
5377 case GTK_SCROLL_PAGE_FORWARD:
5378 case GTK_SCROLL_PAGE_DOWN:
5379 case GTK_SCROLL_PAGE_RIGHT:
5380 case GTK_SCROLL_END:
5381 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5384 case GTK_SCROLL_STEP_FORWARD:
5385 case GTK_SCROLL_STEP_DOWN:
5386 case GTK_SCROLL_STEP_RIGHT:
5389 found = tree_next (combo_box, combo_box->priv->model,
5390 &iter, &new_iter, FALSE);
5393 /* else fall through */
5395 case GTK_SCROLL_PAGE_BACKWARD:
5396 case GTK_SCROLL_PAGE_UP:
5397 case GTK_SCROLL_PAGE_LEFT:
5398 case GTK_SCROLL_START:
5399 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5406 if (found && active_iter)
5408 GtkTreePath *old_path;
5409 GtkTreePath *new_path;
5411 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5412 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5414 if (gtk_tree_path_compare (old_path, new_path) == 0)
5417 gtk_tree_path_free (old_path);
5418 gtk_tree_path_free (new_path);
5423 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5427 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5432 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5433 gboolean group_cycling)
5435 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5437 gtk_widget_grab_focus (combo_box->priv->button);
5443 gtk_combo_box_grab_focus (GtkWidget *widget)
5445 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5447 gtk_widget_grab_focus (combo_box->priv->button);
5451 gtk_combo_box_destroy (GtkWidget *widget)
5453 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5455 if (combo_box->priv->popup_idle_id > 0)
5457 g_source_remove (combo_box->priv->popup_idle_id);
5458 combo_box->priv->popup_idle_id = 0;
5461 gtk_combo_box_popdown (combo_box);
5463 if (combo_box->priv->row_separator_destroy)
5464 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5466 combo_box->priv->row_separator_func = NULL;
5467 combo_box->priv->row_separator_data = NULL;
5468 combo_box->priv->row_separator_destroy = NULL;
5470 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
5471 combo_box->priv->cell_view = NULL;
5475 gtk_combo_box_dispose(GObject* object)
5477 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5479 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5481 gtk_combo_box_menu_destroy (combo_box);
5482 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5483 combo_box->priv->popup_widget = NULL;
5486 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5490 gtk_combo_box_finalize (GObject *object)
5492 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5495 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5496 gtk_combo_box_list_destroy (combo_box);
5498 if (combo_box->priv->popup_window)
5499 gtk_widget_destroy (combo_box->priv->popup_window);
5501 gtk_combo_box_unset_model (combo_box);
5503 for (i = combo_box->priv->cells; i; i = i->next)
5505 ComboCellInfo *info = (ComboCellInfo *)i->data;
5506 GSList *list = info->attributes;
5509 info->destroy (info->func_data);
5511 while (list && list->next)
5513 g_free (list->data);
5514 list = list->next->next;
5516 g_slist_free (info->attributes);
5518 g_object_unref (info->cell);
5519 g_slice_free (ComboCellInfo, info);
5521 g_slist_free (combo_box->priv->cells);
5523 g_free (combo_box->priv->tearoff_title);
5525 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5529 gtk_cell_editable_key_press (GtkWidget *widget,
5533 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5535 if (event->keyval == GDK_KEY_Escape)
5537 g_object_set (combo_box,
5538 "editing-canceled", TRUE,
5540 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5541 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5545 else if (event->keyval == GDK_KEY_Return ||
5546 event->keyval == GDK_KEY_ISO_Enter ||
5547 event->keyval == GDK_KEY_KP_Enter)
5549 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5550 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5559 popdown_idle (gpointer data)
5561 GtkComboBox *combo_box;
5563 combo_box = GTK_COMBO_BOX (data);
5565 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5566 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5568 g_object_unref (combo_box);
5574 popdown_handler (GtkWidget *widget,
5577 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5581 popup_idle (gpointer data)
5583 GtkComboBox *combo_box;
5585 combo_box = GTK_COMBO_BOX (data);
5587 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5588 combo_box->priv->cell_view)
5589 g_signal_connect_object (combo_box->priv->popup_widget,
5590 "unmap", G_CALLBACK (popdown_handler),
5593 /* we unset this if a menu item is activated */
5594 g_object_set (combo_box,
5595 "editing-canceled", TRUE,
5597 gtk_combo_box_popup (combo_box);
5599 combo_box->priv->popup_idle_id = 0;
5600 combo_box->priv->activate_button = 0;
5601 combo_box->priv->activate_time = 0;
5607 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5610 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5613 combo_box->priv->is_cell_renderer = TRUE;
5615 if (combo_box->priv->cell_view)
5617 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5618 G_CALLBACK (gtk_cell_editable_key_press),
5621 gtk_widget_grab_focus (combo_box->priv->button);
5625 child = gtk_bin_get_child (GTK_BIN (combo_box));
5627 g_signal_connect_object (child, "key-press-event",
5628 G_CALLBACK (gtk_cell_editable_key_press),
5631 gtk_widget_grab_focus (child);
5632 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5635 /* we do the immediate popup only for the optionmenu-like
5638 if (combo_box->priv->is_cell_renderer &&
5639 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5641 if (event && event->type == GDK_BUTTON_PRESS)
5643 GdkEventButton *event_button = (GdkEventButton *)event;
5645 combo_box->priv->activate_button = event_button->button;
5646 combo_box->priv->activate_time = event_button->time;
5649 combo_box->priv->popup_idle_id =
5650 gdk_threads_add_idle (popup_idle, combo_box);
5656 * gtk_combo_box_get_add_tearoffs:
5657 * @combo_box: a #GtkComboBox
5659 * Gets the current value of the :add-tearoffs property.
5661 * Return value: the current value of the :add-tearoffs property.
5664 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5666 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5668 return combo_box->priv->add_tearoffs;
5672 * gtk_combo_box_set_add_tearoffs:
5673 * @combo_box: a #GtkComboBox
5674 * @add_tearoffs: %TRUE to add tearoff menu items
5676 * Sets whether the popup menu should have a tearoff
5682 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5683 gboolean add_tearoffs)
5685 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5687 add_tearoffs = add_tearoffs != FALSE;
5689 if (combo_box->priv->add_tearoffs != add_tearoffs)
5691 combo_box->priv->add_tearoffs = add_tearoffs;
5692 gtk_combo_box_check_appearance (combo_box);
5693 gtk_combo_box_relayout (combo_box);
5694 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5699 * gtk_combo_box_get_title:
5700 * @combo_box: a #GtkComboBox
5702 * Gets the current title of the menu in tearoff mode. See
5703 * gtk_combo_box_set_add_tearoffs().
5705 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5706 * string which must not be freed.
5710 G_CONST_RETURN gchar*
5711 gtk_combo_box_get_title (GtkComboBox *combo_box)
5713 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5715 return combo_box->priv->tearoff_title;
5719 gtk_combo_box_update_title (GtkComboBox *combo_box)
5721 gtk_combo_box_check_appearance (combo_box);
5723 if (combo_box->priv->popup_widget &&
5724 GTK_IS_MENU (combo_box->priv->popup_widget))
5725 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5726 combo_box->priv->tearoff_title);
5730 * gtk_combo_box_set_title:
5731 * @combo_box: a #GtkComboBox
5732 * @title: a title for the menu in tearoff mode
5734 * Sets the menu's title in tearoff mode.
5739 gtk_combo_box_set_title (GtkComboBox *combo_box,
5742 GtkComboBoxPrivate *priv;
5744 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5746 priv = combo_box->priv;
5748 if (strcmp (title ? title : "",
5749 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5751 g_free (priv->tearoff_title);
5752 priv->tearoff_title = g_strdup (title);
5754 gtk_combo_box_update_title (combo_box);
5756 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5761 * gtk_combo_box_get_popup_accessible:
5762 * @combo_box: a #GtkComboBox
5764 * Gets the accessible object corresponding to the combo box's popup.
5766 * This function is mostly intended for use by accessibility technologies;
5767 * applications should have little use for it.
5769 * Returns: (transfer none): the accessible object corresponding
5770 * to the combo box's popup.
5775 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5779 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5781 if (combo_box->priv->popup_widget)
5783 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5791 * gtk_combo_box_get_row_separator_func:
5792 * @combo_box: a #GtkComboBox
5794 * Returns the current row separator function.
5796 * Return value: the current row separator function.
5800 GtkTreeViewRowSeparatorFunc
5801 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5803 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5805 return combo_box->priv->row_separator_func;
5809 * gtk_combo_box_set_row_separator_func:
5810 * @combo_box: a #GtkComboBox
5811 * @func: a #GtkTreeViewRowSeparatorFunc
5812 * @data: (allow-none): user data to pass to @func, or %NULL
5813 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5815 * Sets the row separator function, which is used to determine
5816 * whether a row should be drawn as a separator. If the row separator
5817 * function is %NULL, no separators are drawn. This is the default value.
5822 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5823 GtkTreeViewRowSeparatorFunc func,
5825 GDestroyNotify destroy)
5827 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5829 if (combo_box->priv->row_separator_destroy)
5830 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5832 combo_box->priv->row_separator_func = func;
5833 combo_box->priv->row_separator_data = data;
5834 combo_box->priv->row_separator_destroy = destroy;
5836 if (combo_box->priv->tree_view)
5837 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5840 gtk_combo_box_relayout (combo_box);
5842 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5846 * gtk_combo_box_set_button_sensitivity:
5847 * @combo_box: a #GtkComboBox
5848 * @sensitivity: specify the sensitivity of the dropdown button
5850 * Sets whether the dropdown button of the combo box should be
5851 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5852 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5857 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5858 GtkSensitivityType sensitivity)
5860 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5862 if (combo_box->priv->button_sensitivity != sensitivity)
5864 combo_box->priv->button_sensitivity = sensitivity;
5865 gtk_combo_box_update_sensitivity (combo_box);
5867 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5872 * gtk_combo_box_get_button_sensitivity:
5873 * @combo_box: a #GtkComboBox
5875 * Returns whether the combo box sets the dropdown button
5876 * sensitive or not when there are no items in the model.
5878 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5879 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5880 * if the button is always insensitive or
5881 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5882 * the model has one item to be selected.
5887 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5889 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5891 return combo_box->priv->button_sensitivity;
5896 * gtk_combo_box_set_focus_on_click:
5897 * @combo: a #GtkComboBox
5898 * @focus_on_click: whether the combo box grabs focus when clicked
5901 * Sets whether the combo box will grab focus when it is clicked with
5902 * the mouse. Making mouse clicks not grab focus is useful in places
5903 * like toolbars where you don't want the keyboard focus removed from
5904 * the main area of the application.
5909 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5910 gboolean focus_on_click)
5912 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5914 focus_on_click = focus_on_click != FALSE;
5916 if (combo_box->priv->focus_on_click != focus_on_click)
5918 combo_box->priv->focus_on_click = focus_on_click;
5920 if (combo_box->priv->button)
5921 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5924 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5929 * gtk_combo_box_get_focus_on_click:
5930 * @combo: a #GtkComboBox
5932 * Returns whether the combo box grabs focus when it is clicked
5933 * with the mouse. See gtk_combo_box_set_focus_on_click().
5935 * Return value: %TRUE if the combo box grabs focus when it is
5936 * clicked with the mouse.
5941 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5943 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5945 return combo_box->priv->focus_on_click;
5950 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5951 GtkBuilder *builder,
5953 const gchar *tagname,
5954 GMarkupParser *parser,
5957 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5958 tagname, parser, data))
5961 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5962 tagname, parser, data);
5966 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5967 GtkBuilder *builder,
5969 const gchar *tagname,
5972 if (strcmp (tagname, "attributes") == 0)
5973 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5976 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5982 gtk_combo_box_remeasure (GtkComboBox *combo_box)
5984 GtkComboBoxPrivate *priv = combo_box->priv;
5989 !gtk_tree_model_get_iter_first (priv->model, &iter))
5992 priv->minimum_width = priv->natural_width = 0;
5994 path = gtk_tree_path_new_from_indices (0, -1);
5998 gint row_min = 0, row_nat = 0;
6000 if (priv->cell_view)
6001 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
6002 path, &row_min, &row_nat);
6004 priv->minimum_width = MAX (priv->minimum_width, row_min);
6005 priv->natural_width = MAX (priv->natural_width, row_nat);
6007 gtk_tree_path_next (path);
6009 while (gtk_tree_model_iter_next (priv->model, &iter));
6011 gtk_tree_path_free (path);
6016 gtk_combo_box_measure_height_for_width (GtkComboBox *combo_box,
6022 GtkComboBoxPrivate *priv = combo_box->priv;
6025 gint child_min, child_nat;
6027 child = gtk_bin_get_child (GTK_BIN (combo_box));
6029 gtk_widget_get_preferred_height_for_width (child, avail_width,
6030 &child_min, &child_nat);
6033 !gtk_tree_model_get_iter_first (priv->model, &iter))
6036 path = gtk_tree_path_new_from_indices (0, -1);
6040 gint row_min = 0, row_nat = 0;
6042 if (priv->cell_view)
6043 gtk_cell_view_get_desired_height_for_width_of_row (GTK_CELL_VIEW (priv->cell_view),
6045 &row_min, &row_nat);
6047 child_min = MAX (child_min, row_min);
6048 child_nat = MAX (child_nat, row_nat);
6050 gtk_tree_path_next (path);
6052 while (gtk_tree_model_iter_next (priv->model, &iter));
6054 gtk_tree_path_free (path);
6059 *min_height = child_min;
6061 *nat_height = child_nat;
6066 gtk_combo_box_get_preferred_width (GtkWidget *widget,
6070 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
6071 GtkComboBoxPrivate *priv = combo_box->priv;
6073 gint focus_width, focus_pad;
6074 gint font_size, arrow_size;
6075 PangoContext *context;
6076 PangoFontMetrics *metrics;
6077 PangoFontDescription *font_desc;
6079 gint minimum_width, natural_width;
6080 gint child_min, child_nat;
6082 child = gtk_bin_get_child (GTK_BIN (widget));
6085 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
6086 gtk_combo_box_remeasure (combo_box);
6088 child_min = MAX (child_min, priv->minimum_width);
6089 child_nat = MAX (child_nat, priv->natural_width);
6091 gtk_widget_style_get (GTK_WIDGET (widget),
6092 "focus-line-width", &focus_width,
6093 "focus-padding", &focus_pad,
6094 "arrow-size", &arrow_size,
6097 font_desc = gtk_widget_get_style (child)->font_desc;
6098 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
6099 metrics = pango_context_get_metrics (context, font_desc,
6100 pango_context_get_language (context));
6101 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
6102 pango_font_metrics_get_descent (metrics));
6103 pango_font_metrics_unref (metrics);
6105 arrow_size = MAX (arrow_size, font_size);
6107 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
6109 if (!priv->tree_view)
6113 if (priv->cell_view)
6115 gint sep_width, arrow_width;
6116 gint border_width, xthickness, xpad;
6118 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
6119 xthickness = gtk_widget_get_style (priv->button)->xthickness;
6121 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
6122 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
6124 xpad = 2*(border_width + xthickness + focus_width + focus_pad);
6126 minimum_width = child_min + sep_width + arrow_width + xpad;
6127 natural_width = child_nat + sep_width + arrow_width + xpad;
6131 gint but_width, but_nat_width;
6133 gtk_widget_get_preferred_width (priv->button,
6134 &but_width, &but_nat_width);
6136 minimum_width = child_min + but_width;
6137 natural_width = child_nat + but_nat_width;
6143 gint button_width, button_nat_width;
6145 /* sample + frame */
6146 minimum_width = child_min;
6147 natural_width = child_nat;
6149 minimum_width += 2 * focus_width;
6150 natural_width += 2 * focus_width;
6152 if (priv->cell_view_frame)
6154 if (priv->has_frame)
6156 gint border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
6157 gint xpad = 2 * (border_width + gtk_widget_get_style (GTK_WIDGET (priv->cell_view_frame))->xthickness);
6159 minimum_width += xpad;
6160 natural_width += xpad;
6165 gtk_widget_get_preferred_width (priv->button,
6166 &button_width, &button_nat_width);
6168 minimum_width += button_width;
6169 natural_width += button_nat_width;
6172 if (GTK_SHADOW_NONE != priv->shadow_type)
6174 style = gtk_widget_get_style (GTK_WIDGET (widget));
6176 minimum_width += 2 * style->xthickness;
6177 natural_width += 2 * style->xthickness;
6181 *minimum_size = minimum_width;
6184 *natural_size = natural_width;
6188 gtk_combo_box_get_preferred_height (GtkWidget *widget,
6194 /* Combo box is height-for-width only
6195 * (so we always just reserve enough height for the minimum width) */
6196 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
6197 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
6201 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
6206 /* Combo box is height-for-width only
6207 * (so we assume we always reserved enough height for the minimum width) */
6208 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
6213 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
6218 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
6219 GtkComboBoxPrivate *priv = combo_box->priv;
6221 gint focus_width, focus_pad;
6222 gint min_height, nat_height;
6225 gtk_widget_style_get (GTK_WIDGET (widget),
6226 "focus-line-width", &focus_width,
6227 "focus-padding", &focus_pad,
6232 if (GTK_SHADOW_NONE != priv->shadow_type)
6233 size -= gtk_widget_get_style (GTK_WIDGET (widget))->xthickness;
6235 if (!priv->tree_view)
6238 if (priv->cell_view)
6240 GtkStyle *button_style;
6241 /* calculate x/y padding and separator/arrow size */
6242 gint sep_width, arrow_width, sep_height, arrow_height;
6243 gint border_width, xthickness, ythickness, xpad, ypad;
6245 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
6246 button_style = gtk_widget_get_style (priv->button);
6248 xthickness = button_style->xthickness;
6249 ythickness = button_style->ythickness;
6251 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
6252 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
6253 gtk_widget_get_preferred_height_for_width (priv->separator,
6254 sep_width, &sep_height, NULL);
6255 gtk_widget_get_preferred_height_for_width (priv->arrow,
6256 arrow_width, &arrow_height, NULL);
6258 xpad = 2*(border_width + xthickness + focus_width + focus_pad);
6259 ypad = 2*(border_width + ythickness + focus_width + focus_pad);
6261 size -= sep_width + arrow_width + xpad;
6263 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6265 arrow_height = MAX (arrow_height, sep_height);
6266 min_height = MAX (min_height, arrow_height);
6267 nat_height = MAX (nat_height, arrow_height);
6274 /* there is a custom child widget inside (no priv->cell_view) */
6275 gint but_width, but_height;
6277 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
6278 gtk_widget_get_preferred_height_for_width (priv->button,
6279 but_width, &but_height, NULL);
6283 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6285 min_height = MAX (min_height, but_height);
6286 nat_height = MAX (nat_height, but_height);
6292 gint but_width, but_height;
6293 gint xpad = 0, ypad = 0;
6295 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
6296 gtk_widget_get_preferred_height_for_width (priv->button,
6297 but_width, &but_height, NULL);
6299 if (priv->cell_view_frame && priv->has_frame)
6301 GtkStyle *cell_style;
6304 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
6305 cell_style = gtk_widget_get_style (GTK_WIDGET (priv->cell_view_frame));
6307 xpad = 2 * (border_width + cell_style->xthickness);
6308 ypad = 2 * (border_width + cell_style->ythickness);
6312 size -= 2 * focus_width;
6315 gtk_combo_box_measure_height_for_width (combo_box, size, &min_height, &nat_height);
6317 min_height = MAX (min_height, but_height);
6318 nat_height = MAX (nat_height, but_height);
6324 if (GTK_SHADOW_NONE != priv->shadow_type)
6326 style = gtk_widget_get_style (GTK_WIDGET (widget));
6328 min_height += 2 * style->ythickness;
6329 nat_height += 2 * style->ythickness;
6333 *minimum_size = min_height;
6336 *natural_size = nat_height;