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"
56 /* WELCOME, to THE house of evil code */
58 typedef struct _ComboCellInfo ComboCellInfo;
61 GtkCellRenderer *cell;
64 GtkCellLayoutDataFunc func;
66 GDestroyNotify destroy;
72 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
74 struct _GtkComboBoxPrivate
82 GtkShadowType shadow_type;
84 gint active; /* Only temporary */
85 GtkTreeRowReference *active_row;
88 GtkTreeViewColumn *column;
91 GtkWidget *cell_view_frame;
98 GtkWidget *popup_widget;
99 GtkWidget *popup_window;
100 GtkWidget *scrolled_window;
107 guint activate_button;
108 guint32 activate_time;
110 guint resize_idle_id;
116 guint popup_in_progress : 1;
117 guint popup_shown : 1;
118 guint add_tearoffs : 1;
120 guint is_cell_renderer : 1;
121 guint editing_canceled : 1;
122 guint auto_scroll : 1;
123 guint focus_on_click : 1;
124 guint button_sensitivity : 2;
126 GtkTreeViewRowSeparatorFunc row_separator_func;
127 gpointer row_separator_data;
128 GDestroyNotify row_separator_destroy;
130 gchar *tearoff_title;
133 /* While debugging this evil code, I have learned that
134 * there are actually 4 modes to this widget, which can
135 * be characterized as follows
137 * 1) menu mode, no child added
140 * cell_view -> GtkCellView, regular child
141 * cell_view_frame -> NULL
142 * button -> GtkToggleButton set_parent to combo
143 * arrow -> GtkArrow set_parent to button
144 * separator -> GtkVSepator set_parent to button
145 * popup_widget -> GtkMenu
146 * popup_window -> NULL
147 * scrolled_window -> NULL
149 * 2) menu mode, child added
153 * cell_view_frame -> NULL
154 * button -> GtkToggleButton set_parent to combo
155 * arrow -> GtkArrow, child of button
157 * popup_widget -> GtkMenu
158 * popup_window -> NULL
159 * scrolled_window -> NULL
161 * 3) list mode, no child added
163 * tree_view -> GtkTreeView, child of scrolled_window
164 * cell_view -> GtkCellView, regular child
165 * cell_view_frame -> GtkFrame, set parent to combo
166 * button -> GtkToggleButton, set_parent to combo
167 * arrow -> GtkArrow, child of button
169 * popup_widget -> tree_view
170 * popup_window -> GtkWindow
171 * scrolled_window -> GtkScrolledWindow, child of popup_window
173 * 4) list mode, child added
175 * tree_view -> GtkTreeView, child of scrolled_window
177 * cell_view_frame -> NULL
178 * button -> GtkToggleButton, set_parent to combo
179 * arrow -> GtkArrow, child of button
181 * popup_widget -> tree_view
182 * popup_window -> GtkWindow
183 * scrolled_window -> GtkScrolledWindow, child of popup_window
199 PROP_ROW_SPAN_COLUMN,
200 PROP_COLUMN_SPAN_COLUMN,
207 PROP_BUTTON_SENSITIVITY,
208 PROP_EDITING_CANCELED
211 static guint combo_box_signals[LAST_SIGNAL] = {0,};
213 #define BONUS_PADDING 4
214 #define SCROLL_TIME 100
218 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
219 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
220 static void gtk_combo_box_dispose (GObject *object);
221 static void gtk_combo_box_finalize (GObject *object);
222 static void gtk_combo_box_destroy (GtkObject *object);
224 static void gtk_combo_box_set_property (GObject *object,
228 static void gtk_combo_box_get_property (GObject *object,
233 static void gtk_combo_box_state_changed (GtkWidget *widget,
234 GtkStateType previous);
235 static void gtk_combo_box_grab_focus (GtkWidget *widget);
236 static void gtk_combo_box_style_set (GtkWidget *widget,
238 static void gtk_combo_box_button_toggled (GtkWidget *widget,
240 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
241 GtkStateType previous,
243 static void gtk_combo_box_add (GtkContainer *container,
245 static void gtk_combo_box_remove (GtkContainer *container,
248 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
249 GtkCellRenderer *cell);
251 static void gtk_combo_box_menu_show (GtkWidget *menu,
253 static void gtk_combo_box_menu_hide (GtkWidget *menu,
256 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
258 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
263 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
268 static void gtk_combo_box_menu_position (GtkMenu *menu,
274 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
276 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
278 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
280 static void gtk_combo_box_size_request (GtkWidget *widget,
281 GtkRequisition *requisition);
282 static void gtk_combo_box_size_allocate (GtkWidget *widget,
283 GtkAllocation *allocation);
284 static void gtk_combo_box_forall (GtkContainer *container,
285 gboolean include_internals,
286 GtkCallback callback,
287 gpointer callback_data);
288 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
289 GdkEventExpose *event);
290 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
291 GdkEventScroll *event);
292 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
295 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
296 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
297 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
298 GtkScrollType scroll);
299 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
300 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
302 /* listening to the model */
303 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
307 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
310 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
315 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
319 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
325 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
330 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
331 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
333 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
334 GdkEventButton *event,
336 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
339 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
340 GdkEventCrossing *event,
342 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
345 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
346 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
347 GdkEventButton *event,
350 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
353 gboolean path_currently_selected,
356 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
360 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
363 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
364 gboolean add_children);
365 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
366 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
369 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
370 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
372 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
376 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
378 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
379 GdkEventButton *event,
381 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
384 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
385 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
389 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
392 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
397 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
401 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
404 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
406 guint32 activate_time);
407 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
412 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
413 GtkCellRenderer *cell,
415 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
416 GtkCellRenderer *cell,
418 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
419 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
420 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
421 GtkCellRenderer *cell,
422 const gchar *attribute,
424 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
425 GtkCellRenderer *cell,
426 GtkCellLayoutDataFunc func,
428 GDestroyNotify destroy);
429 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
430 GtkCellRenderer *cell);
431 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
432 GtkCellRenderer *cell,
434 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
435 gboolean group_cycling);
437 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
438 GtkCellLayout *cell_layout);
439 static void combo_cell_data_func (GtkCellLayout *cell_layout,
440 GtkCellRenderer *cell,
441 GtkTreeModel *tree_model,
444 static void gtk_combo_box_child_show (GtkWidget *widget,
445 GtkComboBox *combo_box);
446 static void gtk_combo_box_child_hide (GtkWidget *widget,
447 GtkComboBox *combo_box);
449 /* GtkBuildable method implementation */
450 static GtkBuildableIface *parent_buildable_iface;
452 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
453 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
456 const gchar *tagname,
457 GMarkupParser *parser,
459 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
462 const gchar *tagname,
465 /* GtkCellEditable method implementations */
466 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
470 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
471 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
472 gtk_combo_box_cell_layout_init)
473 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
474 gtk_combo_box_cell_editable_init)
475 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
476 gtk_combo_box_buildable_init))
481 gtk_combo_box_class_init (GtkComboBoxClass *klass)
483 GObjectClass *object_class;
484 GtkObjectClass *gtk_object_class;
485 GtkContainerClass *container_class;
486 GtkWidgetClass *widget_class;
487 GtkBindingSet *binding_set;
489 klass->get_active_text = gtk_combo_box_real_get_active_text;
491 container_class = (GtkContainerClass *)klass;
492 container_class->forall = gtk_combo_box_forall;
493 container_class->add = gtk_combo_box_add;
494 container_class->remove = gtk_combo_box_remove;
496 widget_class = (GtkWidgetClass *)klass;
497 widget_class->size_allocate = gtk_combo_box_size_allocate;
498 widget_class->size_request = gtk_combo_box_size_request;
499 widget_class->expose_event = gtk_combo_box_expose_event;
500 widget_class->scroll_event = gtk_combo_box_scroll_event;
501 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
502 widget_class->grab_focus = gtk_combo_box_grab_focus;
503 widget_class->style_set = gtk_combo_box_style_set;
504 widget_class->state_changed = gtk_combo_box_state_changed;
506 gtk_object_class = (GtkObjectClass *)klass;
507 gtk_object_class->destroy = gtk_combo_box_destroy;
509 object_class = (GObjectClass *)klass;
510 object_class->dispose = gtk_combo_box_dispose;
511 object_class->finalize = gtk_combo_box_finalize;
512 object_class->set_property = gtk_combo_box_set_property;
513 object_class->get_property = gtk_combo_box_get_property;
517 * GtkComboBox::changed:
518 * @widget: the object which received the signal
520 * The changed signal is emitted when the active
521 * item is changed. The can be due to the user selecting
522 * a different item from the list, or due to a
523 * call to gtk_combo_box_set_active_iter().
524 * It will also be emitted while typing into a GtkComboBoxEntry,
525 * as well as when selecting an item from the GtkComboBoxEntry's list.
529 combo_box_signals[CHANGED] =
530 g_signal_new (I_("changed"),
531 G_OBJECT_CLASS_TYPE (klass),
533 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
535 g_cclosure_marshal_VOID__VOID,
538 * GtkComboBox::move-active:
539 * @widget: the object that received the signal
540 * @scroll_type: a #GtkScrollType
542 * The ::move-active signal is a
543 * <link linkend="keybinding-signals">keybinding signal</link>
544 * which gets emitted to move the active selection.
548 combo_box_signals[MOVE_ACTIVE] =
549 g_signal_new_class_handler (I_("move-active"),
550 G_OBJECT_CLASS_TYPE (klass),
551 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
552 G_CALLBACK (gtk_combo_box_real_move_active),
554 g_cclosure_marshal_VOID__ENUM,
556 GTK_TYPE_SCROLL_TYPE);
559 * GtkComboBox::popup:
560 * @widget: the object that received the signal
562 * The ::popup signal is a
563 * <link linkend="keybinding-signals">keybinding signal</link>
564 * which gets emitted to popup the combo box list.
566 * The default binding for this signal is Alt+Down.
570 combo_box_signals[POPUP] =
571 g_signal_new_class_handler (I_("popup"),
572 G_OBJECT_CLASS_TYPE (klass),
573 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
574 G_CALLBACK (gtk_combo_box_real_popup),
576 g_cclosure_marshal_VOID__VOID,
579 * GtkComboBox::popdown:
580 * @button: the object which received the signal
582 * The ::popdown signal is a
583 * <link linkend="keybinding-signals">keybinding signal</link>
584 * which gets emitted to popdown the combo box list.
586 * The default bindings for this signal are Alt+Up and Escape.
590 combo_box_signals[POPDOWN] =
591 g_signal_new_class_handler (I_("popdown"),
592 G_OBJECT_CLASS_TYPE (klass),
593 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
594 G_CALLBACK (gtk_combo_box_real_popdown),
596 _gtk_marshal_BOOLEAN__VOID,
600 binding_set = gtk_binding_set_by_class (widget_class);
602 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
604 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
607 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
609 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
611 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
614 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
616 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
617 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
619 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
620 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
622 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
623 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
625 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
626 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
628 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
629 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
631 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
633 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
635 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
636 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
638 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
639 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
641 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
642 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
644 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
645 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
647 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
648 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
650 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
653 g_object_class_override_property (object_class,
654 PROP_EDITING_CANCELED,
660 * The model from which the combo box takes the values shown
665 g_object_class_install_property (object_class,
667 g_param_spec_object ("model",
668 P_("ComboBox model"),
669 P_("The model for the combo box"),
671 GTK_PARAM_READWRITE));
674 * GtkComboBox:wrap-width:
676 * If wrap-width is set to a positive value, the list will be
677 * displayed in multiple columns, the number of columns is
678 * determined by wrap-width.
682 g_object_class_install_property (object_class,
684 g_param_spec_int ("wrap-width",
686 P_("Wrap width for laying out the items in a grid"),
690 GTK_PARAM_READWRITE));
694 * GtkComboBox:row-span-column:
696 * If this is set to a non-negative value, it must be the index of a column
697 * of type %G_TYPE_INT in the model.
699 * The values of that column are used to determine how many rows a value in
700 * the list will span. Therefore, the values in the model column pointed to
701 * by this property must be greater than zero and not larger than wrap-width.
705 g_object_class_install_property (object_class,
706 PROP_ROW_SPAN_COLUMN,
707 g_param_spec_int ("row-span-column",
708 P_("Row span column"),
709 P_("TreeModel column containing the row span values"),
713 GTK_PARAM_READWRITE));
717 * GtkComboBox:column-span-column:
719 * If this is set to a non-negative value, it must be the index of a column
720 * of type %G_TYPE_INT in the model.
722 * The values of that column are used to determine how many columns a value
723 * in the list will span.
727 g_object_class_install_property (object_class,
728 PROP_COLUMN_SPAN_COLUMN,
729 g_param_spec_int ("column-span-column",
730 P_("Column span column"),
731 P_("TreeModel column containing the column span values"),
735 GTK_PARAM_READWRITE));
739 * GtkComboBox:active:
741 * The item which is currently active. If the model is a non-flat treemodel,
742 * and the active item is not an immediate child of the root of the tree,
743 * this property has the value
744 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
745 * where <literal>path</literal> is the #GtkTreePath of the active item.
749 g_object_class_install_property (object_class,
751 g_param_spec_int ("active",
753 P_("The item which is currently active"),
757 GTK_PARAM_READWRITE));
760 * GtkComboBox:add-tearoffs:
762 * The add-tearoffs property controls whether generated menus
763 * have tearoff menu items.
765 * Note that this only affects menu style combo boxes.
769 g_object_class_install_property (object_class,
771 g_param_spec_boolean ("add-tearoffs",
772 P_("Add tearoffs to menus"),
773 P_("Whether dropdowns should have a tearoff menu item"),
775 GTK_PARAM_READWRITE));
778 * GtkComboBox:has-frame:
780 * The has-frame property controls whether a frame
781 * is drawn around the entry.
785 g_object_class_install_property (object_class,
787 g_param_spec_boolean ("has-frame",
789 P_("Whether the combo box draws a frame around the child"),
791 GTK_PARAM_READWRITE));
793 g_object_class_install_property (object_class,
795 g_param_spec_boolean ("focus-on-click",
796 P_("Focus on click"),
797 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
799 GTK_PARAM_READWRITE));
802 * GtkComboBox:tearoff-title:
804 * A title that may be displayed by the window manager
805 * when the popup is torn-off.
809 g_object_class_install_property (object_class,
811 g_param_spec_string ("tearoff-title",
813 P_("A title that may be displayed by the window manager when the popup is torn-off"),
815 GTK_PARAM_READWRITE));
819 * GtkComboBox:popup-shown:
821 * Whether the combo boxes dropdown is popped up.
822 * Note that this property is mainly useful, because
823 * it allows you to connect to notify::popup-shown.
827 g_object_class_install_property (object_class,
829 g_param_spec_boolean ("popup-shown",
831 P_("Whether the combo's dropdown is shown"),
833 GTK_PARAM_READABLE));
837 * GtkComboBox:button-sensitivity:
839 * Whether the dropdown button is sensitive when
840 * the model is empty.
844 g_object_class_install_property (object_class,
845 PROP_BUTTON_SENSITIVITY,
846 g_param_spec_enum ("button-sensitivity",
847 P_("Button Sensitivity"),
848 P_("Whether the dropdown button is sensitive when the model is empty"),
849 GTK_TYPE_SENSITIVITY_TYPE,
850 GTK_SENSITIVITY_AUTO,
851 GTK_PARAM_READWRITE));
853 gtk_widget_class_install_style_property (widget_class,
854 g_param_spec_boolean ("appears-as-list",
855 P_("Appears as list"),
856 P_("Whether dropdowns should look like lists rather than menus"),
858 GTK_PARAM_READABLE));
861 * GtkComboBox:arrow-size:
863 * Sets the minimum size of the arrow in the combo box. Note
864 * that the arrow size is coupled to the font size, so in case
865 * a larger font is used, the arrow will be larger than set
870 gtk_widget_class_install_style_property (widget_class,
871 g_param_spec_int ("arrow-size",
873 P_("The minimum size of the arrow in the combo box"),
877 GTK_PARAM_READABLE));
880 * GtkComboBox:shadow-type:
882 * Which kind of shadow to draw around the combo box.
886 gtk_widget_class_install_style_property (widget_class,
887 g_param_spec_enum ("shadow-type",
889 P_("Which kind of shadow to draw around the combo box"),
890 GTK_TYPE_SHADOW_TYPE,
892 GTK_PARAM_READABLE));
894 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
898 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
900 parent_buildable_iface = g_type_interface_peek_parent (iface);
901 iface->add_child = _gtk_cell_layout_buildable_add_child;
902 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
903 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
907 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
909 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
910 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
911 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
912 iface->clear = gtk_combo_box_cell_layout_clear;
913 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
914 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
915 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
916 iface->reorder = gtk_combo_box_cell_layout_reorder;
920 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
922 iface->start_editing = gtk_combo_box_start_editing;
926 gtk_combo_box_init (GtkComboBox *combo_box)
928 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
930 priv->cell_view = gtk_cell_view_new ();
931 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
932 GTK_BIN (combo_box)->child = priv->cell_view;
933 gtk_widget_show (priv->cell_view);
937 priv->wrap_width = 0;
940 priv->active_row = NULL;
941 priv->col_column = -1;
942 priv->row_column = -1;
944 priv->popup_shown = FALSE;
945 priv->add_tearoffs = FALSE;
946 priv->has_frame = TRUE;
947 priv->is_cell_renderer = FALSE;
948 priv->editing_canceled = FALSE;
949 priv->auto_scroll = FALSE;
950 priv->focus_on_click = TRUE;
951 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
953 combo_box->priv = priv;
955 gtk_combo_box_check_appearance (combo_box);
959 gtk_combo_box_set_property (GObject *object,
964 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
969 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
972 case PROP_WRAP_WIDTH:
973 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
976 case PROP_ROW_SPAN_COLUMN:
977 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
980 case PROP_COLUMN_SPAN_COLUMN:
981 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
985 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
988 case PROP_ADD_TEAROFFS:
989 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
993 combo_box->priv->has_frame = g_value_get_boolean (value);
996 case PROP_FOCUS_ON_CLICK:
997 gtk_combo_box_set_focus_on_click (combo_box,
998 g_value_get_boolean (value));
1001 case PROP_TEAROFF_TITLE:
1002 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1005 case PROP_POPUP_SHOWN:
1006 if (g_value_get_boolean (value))
1007 gtk_combo_box_popup (combo_box);
1009 gtk_combo_box_popdown (combo_box);
1012 case PROP_BUTTON_SENSITIVITY:
1013 gtk_combo_box_set_button_sensitivity (combo_box,
1014 g_value_get_enum (value));
1017 case PROP_EDITING_CANCELED:
1018 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1022 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1028 gtk_combo_box_get_property (GObject *object,
1033 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1034 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
1039 g_value_set_object (value, combo_box->priv->model);
1042 case PROP_WRAP_WIDTH:
1043 g_value_set_int (value, combo_box->priv->wrap_width);
1046 case PROP_ROW_SPAN_COLUMN:
1047 g_value_set_int (value, combo_box->priv->row_column);
1050 case PROP_COLUMN_SPAN_COLUMN:
1051 g_value_set_int (value, combo_box->priv->col_column);
1055 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1058 case PROP_ADD_TEAROFFS:
1059 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1062 case PROP_HAS_FRAME:
1063 g_value_set_boolean (value, combo_box->priv->has_frame);
1066 case PROP_FOCUS_ON_CLICK:
1067 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1070 case PROP_TEAROFF_TITLE:
1071 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1074 case PROP_POPUP_SHOWN:
1075 g_value_set_boolean (value, combo_box->priv->popup_shown);
1078 case PROP_BUTTON_SENSITIVITY:
1079 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1082 case PROP_EDITING_CANCELED:
1083 g_value_set_boolean (value, priv->editing_canceled);
1087 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1093 gtk_combo_box_state_changed (GtkWidget *widget,
1094 GtkStateType previous)
1096 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1097 GtkComboBoxPrivate *priv = combo_box->priv;
1099 if (GTK_WIDGET_REALIZED (widget))
1101 if (priv->tree_view && priv->cell_view)
1102 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1103 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1106 gtk_widget_queue_draw (widget);
1110 gtk_combo_box_button_state_changed (GtkWidget *widget,
1111 GtkStateType previous,
1114 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1115 GtkComboBoxPrivate *priv = combo_box->priv;
1117 if (GTK_WIDGET_REALIZED (widget))
1119 if (!priv->tree_view && priv->cell_view)
1121 if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
1122 (GTK_WIDGET_STATE (priv->cell_view) == GTK_STATE_INSENSITIVE))
1123 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1125 gtk_widget_set_state (priv->cell_view,
1126 GTK_WIDGET_STATE (widget));
1130 gtk_widget_queue_draw (widget);
1134 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1136 GtkComboBoxPrivate *priv = combo_box->priv;
1137 gboolean appears_as_list;
1139 /* if wrap_width > 0, then we are in grid-mode and forced to use
1142 if (priv->wrap_width)
1143 appears_as_list = FALSE;
1145 gtk_widget_style_get (GTK_WIDGET (combo_box),
1146 "appears-as-list", &appears_as_list,
1149 if (appears_as_list)
1151 /* Destroy all the menu mode widgets, if they exist. */
1152 if (GTK_IS_MENU (priv->popup_widget))
1153 gtk_combo_box_menu_destroy (combo_box);
1155 /* Create the list mode widgets, if they don't already exist. */
1156 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1157 gtk_combo_box_list_setup (combo_box);
1161 /* Destroy all the list mode widgets, if they exist. */
1162 if (GTK_IS_TREE_VIEW (priv->tree_view))
1163 gtk_combo_box_list_destroy (combo_box);
1165 /* Create the menu mode widgets, if they don't already exist. */
1166 if (!GTK_IS_MENU (priv->popup_widget))
1167 gtk_combo_box_menu_setup (combo_box, TRUE);
1170 gtk_widget_style_get (GTK_WIDGET (combo_box),
1171 "shadow-type", &priv->shadow_type,
1176 gtk_combo_box_style_set (GtkWidget *widget,
1179 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1180 GtkComboBoxPrivate *priv = combo_box->priv;
1182 gtk_combo_box_check_appearance (combo_box);
1184 if (priv->tree_view && priv->cell_view)
1185 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1186 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1188 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1189 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1190 GTK_SHADOW_NONE == priv->shadow_type ?
1191 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1195 gtk_combo_box_button_toggled (GtkWidget *widget,
1198 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1200 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1202 if (!combo_box->priv->popup_in_progress)
1203 gtk_combo_box_popup (combo_box);
1206 gtk_combo_box_popdown (combo_box);
1210 gtk_combo_box_add (GtkContainer *container,
1213 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1214 GtkComboBoxPrivate *priv = combo_box->priv;
1216 if (priv->cell_view && priv->cell_view->parent)
1218 gtk_widget_unparent (priv->cell_view);
1219 GTK_BIN (container)->child = NULL;
1220 gtk_widget_queue_resize (GTK_WIDGET (container));
1223 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1224 GTK_BIN (container)->child = widget;
1226 if (priv->cell_view &&
1227 widget != priv->cell_view)
1229 /* since the cell_view was unparented, it's gone now */
1230 priv->cell_view = NULL;
1232 if (!priv->tree_view && priv->separator)
1234 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1236 priv->separator = NULL;
1238 gtk_widget_queue_resize (GTK_WIDGET (container));
1240 else if (priv->cell_view_frame)
1242 gtk_widget_unparent (priv->cell_view_frame);
1243 priv->cell_view_frame = NULL;
1250 gtk_combo_box_remove (GtkContainer *container,
1253 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1254 GtkComboBoxPrivate *priv = combo_box->priv;
1256 gboolean appears_as_list;
1258 if (widget == priv->cell_view)
1259 priv->cell_view = NULL;
1261 gtk_widget_unparent (widget);
1262 GTK_BIN (container)->child = NULL;
1264 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1267 gtk_widget_queue_resize (GTK_WIDGET (container));
1269 if (!priv->tree_view)
1270 appears_as_list = FALSE;
1272 appears_as_list = TRUE;
1274 if (appears_as_list)
1275 gtk_combo_box_list_destroy (combo_box);
1276 else if (GTK_IS_MENU (priv->popup_widget))
1278 gtk_combo_box_menu_destroy (combo_box);
1279 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1280 priv->popup_widget = NULL;
1283 if (!priv->cell_view)
1285 priv->cell_view = gtk_cell_view_new ();
1286 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1287 GTK_BIN (container)->child = priv->cell_view;
1289 gtk_widget_show (priv->cell_view);
1290 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1292 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1296 if (appears_as_list)
1297 gtk_combo_box_list_setup (combo_box);
1299 gtk_combo_box_menu_setup (combo_box, TRUE);
1301 if (gtk_tree_row_reference_valid (priv->active_row))
1303 path = gtk_tree_row_reference_get_path (priv->active_row);
1304 gtk_combo_box_set_active_internal (combo_box, path);
1305 gtk_tree_path_free (path);
1308 gtk_combo_box_set_active_internal (combo_box, NULL);
1311 static ComboCellInfo *
1312 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1313 GtkCellRenderer *cell)
1317 for (i = combo_box->priv->cells; i; i = i->next)
1319 ComboCellInfo *info = (ComboCellInfo *)i->data;
1321 if (info && info->cell == cell)
1329 gtk_combo_box_menu_show (GtkWidget *menu,
1332 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1333 GtkComboBoxPrivate *priv = combo_box->priv;
1335 gtk_combo_box_child_show (menu, user_data);
1337 priv->popup_in_progress = TRUE;
1338 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1340 priv->popup_in_progress = FALSE;
1344 gtk_combo_box_menu_hide (GtkWidget *menu,
1347 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1349 gtk_combo_box_child_hide (menu,user_data);
1351 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1356 gtk_combo_box_detacher (GtkWidget *widget,
1359 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1360 GtkComboBoxPrivate *priv = combo_box->priv;
1362 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1364 g_signal_handlers_disconnect_by_func (menu->toplevel,
1365 gtk_combo_box_menu_show,
1367 g_signal_handlers_disconnect_by_func (menu->toplevel,
1368 gtk_combo_box_menu_hide,
1371 priv->popup_widget = NULL;
1375 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1378 GtkComboBoxPrivate *priv = combo_box->priv;
1380 if (GTK_IS_MENU (priv->popup_widget))
1382 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1383 priv->popup_widget = NULL;
1385 else if (priv->popup_widget)
1387 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1388 priv->popup_widget);
1389 g_object_unref (priv->popup_widget);
1390 priv->popup_widget = NULL;
1393 if (GTK_IS_MENU (popup))
1395 if (priv->popup_window)
1397 gtk_widget_destroy (priv->popup_window);
1398 priv->popup_window = NULL;
1401 priv->popup_widget = popup;
1404 * Note that we connect to show/hide on the toplevel, not the
1405 * menu itself, since the menu is not shown/hidden when it is
1406 * popped up while torn-off.
1408 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1409 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1410 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1411 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1413 gtk_menu_attach_to_widget (GTK_MENU (popup),
1414 GTK_WIDGET (combo_box),
1415 gtk_combo_box_detacher);
1419 if (!priv->popup_window)
1421 GtkWidget *toplevel;
1423 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1424 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1426 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1427 GDK_WINDOW_TYPE_HINT_COMBO);
1429 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1430 G_CALLBACK (gtk_combo_box_child_show),
1432 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1433 G_CALLBACK (gtk_combo_box_child_hide),
1436 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1437 if (GTK_IS_WINDOW (toplevel))
1439 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1440 GTK_WINDOW (priv->popup_window));
1441 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1442 GTK_WINDOW (toplevel));
1445 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1446 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1447 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1449 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1451 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1454 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1457 gtk_widget_show (priv->scrolled_window);
1459 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1460 priv->scrolled_window);
1463 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1466 gtk_widget_show (popup);
1467 g_object_ref (popup);
1468 priv->popup_widget = popup;
1473 gtk_combo_box_menu_position_below (GtkMenu *menu,
1479 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1485 GdkRectangle monitor;
1487 /* FIXME: is using the size request here broken? */
1488 child = GTK_BIN (combo_box)->child;
1492 if (!gtk_widget_get_has_window (child))
1494 sx += child->allocation.x;
1495 sy += child->allocation.y;
1498 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1500 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1501 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1503 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1505 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1508 *x = sx + child->allocation.width - req.width;
1511 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1512 monitor_num = gdk_screen_get_monitor_at_window (screen,
1513 GTK_WIDGET (combo_box)->window);
1514 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1518 else if (*x + req.width > monitor.x + monitor.width)
1519 *x = monitor.x + monitor.width - req.width;
1521 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1522 *y += child->allocation.height;
1523 else if (*y - monitor.y >= req.height)
1525 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1526 *y += child->allocation.height;
1534 gtk_combo_box_menu_position_over (GtkMenu *menu,
1540 GtkComboBox *combo_box;
1544 GtkRequisition requisition;
1551 combo_box = GTK_COMBO_BOX (user_data);
1552 widget = GTK_WIDGET (combo_box);
1554 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1555 menu_width = requisition.width;
1557 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1559 menu_xpos = widget->allocation.x;
1560 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1564 gtk_widget_get_child_requisition (active, &requisition);
1565 menu_ypos -= requisition.height / 2;
1568 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1571 child = children->data;
1573 if (active == child)
1576 if (gtk_widget_get_visible (child))
1578 gtk_widget_get_child_requisition (child, &requisition);
1579 menu_ypos -= requisition.height;
1582 children = children->next;
1585 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1586 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1588 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1589 &menu_xpos, &menu_ypos);
1591 /* Clamp the position on screen */
1592 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1596 else if ((menu_xpos + menu_width) > screen_width)
1597 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1606 gtk_combo_box_menu_position (GtkMenu *menu,
1612 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1613 GtkComboBoxPrivate *priv = combo_box->priv;
1614 GtkWidget *menu_item;
1617 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1618 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1621 /* FIXME handle nested menus better */
1622 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1624 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1627 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1630 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1631 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1632 GDK_WINDOW_TYPE_HINT_COMBO);
1636 gtk_combo_box_list_position (GtkComboBox *combo_box,
1642 GtkComboBoxPrivate *priv = combo_box->priv;
1645 GdkRectangle monitor;
1646 GtkRequisition popup_req;
1647 GtkPolicyType hpolicy, vpolicy;
1649 /* under windows, the drop down list is as wide as the combo box itself.
1651 GtkWidget *sample = GTK_WIDGET (combo_box);
1655 if (!gtk_widget_get_has_window (sample))
1657 *x += sample->allocation.x;
1658 *y += sample->allocation.y;
1661 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1663 *width = sample->allocation.width;
1665 hpolicy = vpolicy = GTK_POLICY_NEVER;
1666 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1668 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1670 if (popup_req.width > *width)
1672 hpolicy = GTK_POLICY_ALWAYS;
1673 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1675 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1678 *height = popup_req.height;
1680 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1681 monitor_num = gdk_screen_get_monitor_at_window (screen,
1682 GTK_WIDGET (combo_box)->window);
1683 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1687 else if (*x + *width > monitor.x + monitor.width)
1688 *x = monitor.x + monitor.width - *width;
1690 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1691 *y += sample->allocation.height;
1692 else if (*y - *height >= monitor.y)
1694 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1696 *y += sample->allocation.height;
1697 *height = monitor.y + monitor.height - *y;
1701 *height = *y - monitor.y;
1705 if (popup_req.height > *height)
1707 vpolicy = GTK_POLICY_ALWAYS;
1709 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1715 cell_view_is_sensitive (GtkCellView *cell_view)
1717 GList *cells, *list;
1720 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1723 for (list = cells; list; list = list->next)
1725 g_object_get (list->data, "sensitive", &sensitive, NULL);
1730 g_list_free (cells);
1736 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1739 GtkComboBoxPrivate *priv = combo_box->priv;
1740 GList *cells, *list;
1746 if (priv->row_separator_func)
1748 if (priv->row_separator_func (priv->model, iter,
1749 priv->row_separator_data))
1753 gtk_tree_view_column_cell_set_cell_data (priv->column,
1755 iter, FALSE, FALSE);
1757 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1760 for (list = cells; list; list = list->next)
1762 g_object_get (list->data, "sensitive", &sensitive, NULL);
1767 g_list_free (cells);
1773 update_menu_sensitivity (GtkComboBox *combo_box,
1776 GtkComboBoxPrivate *priv = combo_box->priv;
1777 GList *children, *child;
1778 GtkWidget *item, *submenu, *separator;
1779 GtkWidget *cell_view;
1785 children = gtk_container_get_children (GTK_CONTAINER (menu));
1787 for (child = children; child; child = child->next)
1789 item = GTK_WIDGET (child->data);
1790 cell_view = GTK_BIN (item)->child;
1792 if (!GTK_IS_CELL_VIEW (cell_view))
1795 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1796 if (submenu != NULL)
1798 gtk_widget_set_sensitive (item, TRUE);
1799 update_menu_sensitivity (combo_box, submenu);
1803 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1805 if (menu != priv->popup_widget && child == children)
1807 separator = GTK_WIDGET (child->next->data);
1808 g_object_set (item, "visible", sensitive, NULL);
1809 g_object_set (separator, "visible", sensitive, NULL);
1812 gtk_widget_set_sensitive (item, sensitive);
1816 g_list_free (children);
1820 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1822 guint32 activate_time)
1824 GtkComboBoxPrivate *priv = combo_box->priv;
1827 GtkRequisition requisition;
1830 update_menu_sensitivity (combo_box, priv->popup_widget);
1833 if (gtk_tree_row_reference_valid (priv->active_row))
1835 path = gtk_tree_row_reference_get_path (priv->active_row);
1836 active_item = gtk_tree_path_get_indices (path)[0];
1837 gtk_tree_path_free (path);
1839 if (priv->add_tearoffs)
1843 /* FIXME handle nested menus better */
1844 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1846 if (priv->wrap_width == 0)
1848 width = GTK_WIDGET (combo_box)->allocation.width;
1849 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1850 gtk_widget_size_request (priv->popup_widget, &requisition);
1852 gtk_widget_set_size_request (priv->popup_widget,
1853 MAX (width, requisition.width), -1);
1856 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1858 gtk_combo_box_menu_position, combo_box,
1859 button, activate_time);
1863 popup_grab_on_window (GdkWindow *window,
1864 guint32 activate_time,
1865 gboolean grab_keyboard)
1867 if ((gdk_pointer_grab (window, TRUE,
1868 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1869 GDK_POINTER_MOTION_MASK,
1870 NULL, NULL, activate_time) == 0))
1872 if (!grab_keyboard ||
1873 gdk_keyboard_grab (window, TRUE,
1874 activate_time) == 0)
1878 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1888 * gtk_combo_box_popup:
1889 * @combo_box: a #GtkComboBox
1891 * Pops up the menu or dropdown list of @combo_box.
1893 * This function is mostly intended for use by accessibility technologies;
1894 * applications should have little use for it.
1899 gtk_combo_box_popup (GtkComboBox *combo_box)
1901 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1903 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1907 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1909 GtkComboBoxPrivate *priv = combo_box->priv;
1910 gint x, y, width, height;
1911 GtkTreePath *path = NULL, *ppath;
1912 GtkWidget *toplevel;
1914 if (!GTK_WIDGET_REALIZED (combo_box))
1917 if (GTK_WIDGET_MAPPED (priv->popup_widget))
1920 if (GTK_IS_MENU (priv->popup_widget))
1922 gtk_combo_box_menu_popup (combo_box,
1923 priv->activate_button,
1924 priv->activate_time);
1928 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1929 if (GTK_IS_WINDOW (toplevel))
1930 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1931 GTK_WINDOW (priv->popup_window));
1933 gtk_widget_show_all (priv->scrolled_window);
1934 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1936 gtk_widget_set_size_request (priv->popup_window, width, height);
1937 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1939 if (gtk_tree_row_reference_valid (priv->active_row))
1941 path = gtk_tree_row_reference_get_path (priv->active_row);
1942 ppath = gtk_tree_path_copy (path);
1943 if (gtk_tree_path_up (ppath))
1944 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1946 gtk_tree_path_free (ppath);
1948 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1952 gtk_widget_show (priv->popup_window);
1956 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1958 gtk_tree_path_free (path);
1961 gtk_widget_grab_focus (priv->popup_window);
1962 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1965 if (!gtk_widget_has_focus (priv->tree_view))
1966 gtk_widget_grab_focus (priv->tree_view);
1968 if (!popup_grab_on_window (priv->popup_window->window,
1969 GDK_CURRENT_TIME, TRUE))
1971 gtk_widget_hide (priv->popup_window);
1975 gtk_grab_add (priv->popup_window);
1979 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1981 if (combo_box->priv->popup_shown)
1983 gtk_combo_box_popdown (combo_box);
1991 * gtk_combo_box_popdown:
1992 * @combo_box: a #GtkComboBox
1994 * Hides the menu or dropdown list of @combo_box.
1996 * This function is mostly intended for use by accessibility technologies;
1997 * applications should have little use for it.
2002 gtk_combo_box_popdown (GtkComboBox *combo_box)
2004 GtkComboBoxPrivate *priv = combo_box->priv;
2006 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2008 if (GTK_IS_MENU (priv->popup_widget))
2010 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2014 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
2017 gtk_grab_remove (priv->popup_window);
2018 gtk_widget_hide_all (priv->popup_window);
2019 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2024 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2027 GtkComboBoxPrivate *priv = combo_box->priv;
2031 if (priv->cell_view)
2032 gtk_widget_style_get (priv->cell_view,
2033 "focus-line-width", &padding,
2038 /* add some pixels for good measure */
2039 padding += BONUS_PADDING;
2041 if (priv->cell_view)
2042 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2047 return req.width + padding;
2051 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2053 GtkComboBoxPrivate *priv = combo_box->priv;
2058 !gtk_tree_model_get_iter_first (priv->model, &iter))
2064 path = gtk_tree_path_new_from_indices (0, -1);
2070 if (priv->cell_view)
2071 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2079 priv->width = MAX (priv->width, req.width);
2080 priv->height = MAX (priv->height, req.height);
2082 gtk_tree_path_next (path);
2084 while (gtk_tree_model_iter_next (priv->model, &iter));
2086 gtk_tree_path_free (path);
2090 gtk_combo_box_size_request (GtkWidget *widget,
2091 GtkRequisition *requisition)
2094 gint focus_width, focus_pad;
2097 GtkRequisition bin_req;
2098 PangoContext *context;
2099 PangoFontMetrics *metrics;
2100 PangoFontDescription *font_desc;
2102 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2103 GtkComboBoxPrivate *priv = combo_box->priv;
2106 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2107 gtk_combo_box_remeasure (combo_box);
2108 bin_req.width = MAX (bin_req.width, priv->width);
2109 bin_req.height = MAX (bin_req.height, priv->height);
2111 gtk_widget_style_get (GTK_WIDGET (widget),
2112 "focus-line-width", &focus_width,
2113 "focus-padding", &focus_pad,
2114 "arrow-size", &arrow_size,
2117 font_desc = GTK_BIN (widget)->child->style->font_desc;
2118 context = gtk_widget_get_pango_context (widget);
2119 metrics = pango_context_get_metrics (context, font_desc,
2120 pango_context_get_language (context));
2121 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2122 pango_font_metrics_get_descent (metrics));
2123 pango_font_metrics_unref (metrics);
2125 arrow_size = MAX (arrow_size, font_size);
2127 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2129 if (!priv->tree_view)
2133 if (priv->cell_view)
2135 GtkRequisition button_req, sep_req, arrow_req;
2136 gint border_width, xthickness, ythickness;
2138 gtk_widget_size_request (priv->button, &button_req);
2139 border_width = GTK_CONTAINER (combo_box)->border_width;
2140 xthickness = priv->button->style->xthickness;
2141 ythickness = priv->button->style->ythickness;
2143 bin_req.width = MAX (bin_req.width, priv->width);
2144 bin_req.height = MAX (bin_req.height, priv->height);
2146 gtk_widget_size_request (priv->separator, &sep_req);
2147 gtk_widget_size_request (priv->arrow, &arrow_req);
2149 height = MAX (sep_req.height, arrow_req.height);
2150 height = MAX (height, bin_req.height);
2152 width = bin_req.width + sep_req.width + arrow_req.width;
2154 height += 2*(border_width + ythickness + focus_width + focus_pad);
2155 width += 2*(border_width + xthickness + focus_width + focus_pad);
2157 requisition->width = width;
2158 requisition->height = height;
2162 GtkRequisition but_req;
2164 gtk_widget_size_request (priv->button, &but_req);
2166 requisition->width = bin_req.width + but_req.width;
2167 requisition->height = MAX (bin_req.height, but_req.height);
2173 GtkRequisition button_req, frame_req;
2175 /* sample + frame */
2176 *requisition = bin_req;
2178 requisition->width += 2 * focus_width;
2180 if (priv->cell_view_frame)
2182 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2183 if (priv->has_frame)
2185 requisition->width += 2 *
2186 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2187 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2188 requisition->height += 2 *
2189 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2190 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2195 gtk_widget_size_request (priv->button, &button_req);
2197 requisition->height = MAX (requisition->height, button_req.height);
2198 requisition->width += button_req.width;
2201 if (GTK_SHADOW_NONE != priv->shadow_type)
2203 requisition->height += 2 * widget->style->ythickness;
2204 requisition->width += 2 * widget->style->xthickness;
2208 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2209 gtk_widget_size_request (combo_box->priv->button, &req); \
2212 child.x = allocation->x + shadow_width; \
2214 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2216 child.y = allocation->y + shadow_height; \
2217 child.width = req.width; \
2218 child.height = allocation->height - 2 * shadow_height; \
2219 child.width = MAX (1, child.width); \
2220 child.height = MAX (1, child.height); \
2222 gtk_widget_size_allocate (combo_box->priv->button, &child);
2225 gtk_combo_box_size_allocate (GtkWidget *widget,
2226 GtkAllocation *allocation)
2228 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2229 GtkComboBoxPrivate *priv = combo_box->priv;
2230 gint shadow_width, shadow_height;
2231 gint focus_width, focus_pad;
2232 GtkAllocation child;
2234 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2236 widget->allocation = *allocation;
2238 gtk_widget_style_get (GTK_WIDGET (widget),
2239 "focus-line-width", &focus_width,
2240 "focus-padding", &focus_pad,
2243 if (GTK_SHADOW_NONE != priv->shadow_type)
2245 shadow_width = widget->style->xthickness;
2246 shadow_height = widget->style->ythickness;
2254 if (!priv->tree_view)
2256 if (priv->cell_view)
2258 gint border_width, xthickness, ythickness;
2262 allocation->x += shadow_width;
2263 allocation->y += shadow_height;
2264 allocation->width -= 2 * shadow_width;
2265 allocation->height -= 2 * shadow_height;
2267 gtk_widget_size_allocate (priv->button, allocation);
2269 /* set some things ready */
2270 border_width = GTK_CONTAINER (priv->button)->border_width;
2271 xthickness = priv->button->style->xthickness;
2272 ythickness = priv->button->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_size_request (priv->arrow, &req);
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_size_request (priv->separator, &req);
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))
2324 GtkRequisition requisition;
2326 /* Warning here, without the check in the position func */
2327 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2328 if (priv->wrap_width == 0)
2330 width = GTK_WIDGET (combo_box)->allocation.width;
2331 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2332 gtk_widget_size_request (priv->popup_widget, &requisition);
2333 gtk_widget_set_size_request (priv->popup_widget,
2334 MAX (width, requisition.width), -1);
2338 child.width = MAX (1, child.width);
2339 child.height = MAX (1, child.height);
2340 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2344 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2347 child.x = allocation->x + req.width + shadow_width;
2349 child.x = allocation->x + shadow_width;
2350 child.y = allocation->y + shadow_height;
2351 child.width = allocation->width - req.width - 2 * shadow_width;
2352 child.width = MAX (1, child.width);
2353 child.height = MAX (1, child.height);
2354 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2361 /* Combobox thickness + border-width */
2362 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2363 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2366 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2370 child.x = allocation->x + req.width;
2372 child.x = allocation->x;
2374 child.y = allocation->y;
2375 child.width = allocation->width - req.width;
2376 child.height = allocation->height;
2378 if (priv->cell_view_frame)
2382 child.width = MAX (1, child.width - delta_x * 2);
2383 child.height = MAX (1, child.height - delta_y * 2);
2384 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2387 if (priv->has_frame)
2389 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2390 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2391 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2392 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2396 child.width -= delta_x * 2;
2397 child.height -= delta_y * 2;
2404 child.width -= delta_x * 2;
2405 child.height -= delta_y * 2;
2408 if (gtk_widget_get_visible (priv->popup_window))
2410 gint x, y, width, height;
2411 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2412 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2413 gtk_widget_set_size_request (priv->popup_window, width, height);
2417 child.width = MAX (1, child.width);
2418 child.height = MAX (1, child.height);
2420 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2424 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2427 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2429 GtkComboBoxPrivate *priv = combo_box->priv;
2433 g_signal_handler_disconnect (priv->model,
2435 g_signal_handler_disconnect (priv->model,
2437 g_signal_handler_disconnect (priv->model,
2438 priv->reordered_id);
2439 g_signal_handler_disconnect (priv->model,
2444 if (!priv->tree_view)
2446 if (priv->popup_widget)
2447 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2448 (GtkCallback)gtk_widget_destroy, NULL);
2453 g_object_unref (priv->model);
2457 if (priv->active_row)
2459 gtk_tree_row_reference_free (priv->active_row);
2460 priv->active_row = NULL;
2463 if (priv->cell_view)
2464 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2468 gtk_combo_box_forall (GtkContainer *container,
2469 gboolean include_internals,
2470 GtkCallback callback,
2471 gpointer callback_data)
2473 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2474 GtkComboBoxPrivate *priv = combo_box->priv;
2476 if (include_internals)
2479 (* callback) (priv->button, callback_data);
2480 if (priv->cell_view_frame)
2481 (* callback) (priv->cell_view_frame, callback_data);
2484 if (GTK_BIN (container)->child)
2485 (* callback) (GTK_BIN (container)->child, callback_data);
2489 gtk_combo_box_child_show (GtkWidget *widget,
2490 GtkComboBox *combo_box)
2492 GtkComboBoxPrivate *priv = combo_box->priv;
2494 priv->popup_shown = TRUE;
2495 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2499 gtk_combo_box_child_hide (GtkWidget *widget,
2500 GtkComboBox *combo_box)
2502 GtkComboBoxPrivate *priv = combo_box->priv;
2504 priv->popup_shown = FALSE;
2505 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2509 gtk_combo_box_expose_event (GtkWidget *widget,
2510 GdkEventExpose *event)
2512 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2513 GtkComboBoxPrivate *priv = combo_box->priv;
2515 if (gtk_widget_is_drawable (widget) &&
2516 GTK_SHADOW_NONE != priv->shadow_type)
2518 gtk_paint_shadow (widget->style, widget->window,
2519 GTK_STATE_NORMAL, priv->shadow_type,
2520 NULL, widget, "combobox",
2521 widget->allocation.x, widget->allocation.y,
2522 widget->allocation.width, widget->allocation.height);
2525 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2526 priv->button, event);
2528 if (priv->tree_view && priv->cell_view_frame)
2530 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2531 priv->cell_view_frame, event);
2534 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2535 GTK_BIN (widget)->child, event);
2550 path_visible (GtkTreeView *view,
2556 /* Note that we rely on the fact that collapsed rows don't have nodes
2558 return _gtk_tree_view_find_node (view, path, &tree, &node);
2562 tree_next_func (GtkTreeModel *model,
2567 SearchData *search_data = (SearchData *)data;
2569 if (search_data->found)
2571 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2574 if (search_data->visible &&
2575 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2578 search_data->set = TRUE;
2579 search_data->iter = *iter;
2584 if (gtk_tree_path_compare (path, search_data->path) == 0)
2585 search_data->found = TRUE;
2591 tree_next (GtkComboBox *combo,
2592 GtkTreeModel *model,
2597 SearchData search_data;
2599 search_data.combo = combo;
2600 search_data.path = gtk_tree_model_get_path (model, iter);
2601 search_data.visible = visible;
2602 search_data.found = FALSE;
2603 search_data.set = FALSE;
2605 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2607 *next = search_data.iter;
2609 gtk_tree_path_free (search_data.path);
2611 return search_data.set;
2615 tree_prev_func (GtkTreeModel *model,
2620 SearchData *search_data = (SearchData *)data;
2622 if (gtk_tree_path_compare (path, search_data->path) == 0)
2624 search_data->found = TRUE;
2628 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2631 if (search_data->visible &&
2632 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2635 search_data->set = TRUE;
2636 search_data->iter = *iter;
2642 tree_prev (GtkComboBox *combo,
2643 GtkTreeModel *model,
2648 SearchData search_data;
2650 search_data.combo = combo;
2651 search_data.path = gtk_tree_model_get_path (model, iter);
2652 search_data.visible = visible;
2653 search_data.found = FALSE;
2654 search_data.set = FALSE;
2656 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2658 *prev = search_data.iter;
2660 gtk_tree_path_free (search_data.path);
2662 return search_data.set;
2666 tree_last_func (GtkTreeModel *model,
2671 SearchData *search_data = (SearchData *)data;
2673 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2676 /* Note that we rely on the fact that collapsed rows don't have nodes
2678 if (search_data->visible &&
2679 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2682 search_data->set = TRUE;
2683 search_data->iter = *iter;
2689 tree_last (GtkComboBox *combo,
2690 GtkTreeModel *model,
2694 SearchData search_data;
2696 search_data.combo = combo;
2697 search_data.visible = visible;
2698 search_data.set = FALSE;
2700 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2702 *last = search_data.iter;
2704 return search_data.set;
2709 tree_first_func (GtkTreeModel *model,
2714 SearchData *search_data = (SearchData *)data;
2716 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2719 if (search_data->visible &&
2720 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2723 search_data->set = TRUE;
2724 search_data->iter = *iter;
2730 tree_first (GtkComboBox *combo,
2731 GtkTreeModel *model,
2735 SearchData search_data;
2737 search_data.combo = combo;
2738 search_data.visible = visible;
2739 search_data.set = FALSE;
2741 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2743 *first = search_data.iter;
2745 return search_data.set;
2749 gtk_combo_box_scroll_event (GtkWidget *widget,
2750 GdkEventScroll *event)
2752 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2755 GtkTreeIter new_iter;
2757 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2760 if (event->direction == GDK_SCROLL_UP)
2761 found = tree_prev (combo_box, combo_box->priv->model,
2762 &iter, &new_iter, FALSE);
2764 found = tree_next (combo_box, combo_box->priv->model,
2765 &iter, &new_iter, FALSE);
2768 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2778 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2779 GtkCellLayout *cell_layout)
2781 GtkComboBoxPrivate *priv = combo_box->priv;
2784 for (k = priv->cells; k; k = k->next)
2787 ComboCellInfo *info = (ComboCellInfo *)k->data;
2789 if (info->pack == GTK_PACK_START)
2790 gtk_cell_layout_pack_start (cell_layout,
2791 info->cell, info->expand);
2792 else if (info->pack == GTK_PACK_END)
2793 gtk_cell_layout_pack_end (cell_layout,
2794 info->cell, info->expand);
2796 gtk_cell_layout_set_cell_data_func (cell_layout,
2798 combo_cell_data_func, info, NULL);
2800 for (j = info->attributes; j; j = j->next->next)
2802 gtk_cell_layout_add_attribute (cell_layout,
2805 GPOINTER_TO_INT (j->next->data));
2811 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2812 gboolean add_children)
2814 GtkComboBoxPrivate *priv = combo_box->priv;
2817 if (priv->cell_view)
2819 priv->button = gtk_toggle_button_new ();
2820 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2821 priv->focus_on_click);
2823 g_signal_connect (priv->button, "toggled",
2824 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2825 gtk_widget_set_parent (priv->button,
2826 GTK_BIN (combo_box)->child->parent);
2828 priv->box = gtk_hbox_new (FALSE, 0);
2829 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2831 priv->separator = gtk_vseparator_new ();
2832 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2834 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2835 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2837 gtk_widget_show_all (priv->button);
2841 priv->button = gtk_toggle_button_new ();
2842 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2843 priv->focus_on_click);
2845 g_signal_connect (priv->button, "toggled",
2846 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2847 gtk_widget_set_parent (priv->button,
2848 GTK_BIN (combo_box)->child->parent);
2850 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2851 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2852 gtk_widget_show_all (priv->button);
2855 g_signal_connect (priv->button, "button-press-event",
2856 G_CALLBACK (gtk_combo_box_menu_button_press),
2858 g_signal_connect (priv->button, "state-changed",
2859 G_CALLBACK (gtk_combo_box_button_state_changed),
2862 /* create our funky menu */
2863 menu = gtk_menu_new ();
2864 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2865 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2867 g_signal_connect (menu, "key-press-event",
2868 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2869 gtk_combo_box_set_popup_widget (combo_box, menu);
2873 gtk_combo_box_menu_fill (combo_box);
2875 /* the column is needed in tree_column_row_is_sensitive() */
2876 priv->column = gtk_tree_view_column_new ();
2877 g_object_ref_sink (priv->column);
2878 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2880 gtk_combo_box_update_title (combo_box);
2881 gtk_combo_box_update_sensitivity (combo_box);
2885 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2887 GtkComboBoxPrivate *priv = combo_box->priv;
2893 menu = priv->popup_widget;
2895 if (priv->add_tearoffs)
2897 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2899 gtk_widget_show (tearoff);
2901 if (priv->wrap_width)
2902 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2904 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2907 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2911 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2912 GtkTreeModel *model,
2915 GtkWidget *cell_view;
2920 cell_view = gtk_cell_view_new ();
2921 item = gtk_menu_item_new ();
2922 gtk_container_add (GTK_CONTAINER (item), cell_view);
2924 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2925 path = gtk_tree_model_get_path (model, iter);
2926 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2927 gtk_tree_path_free (path);
2929 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2930 gtk_widget_size_request (cell_view, &req);
2931 gtk_widget_show (cell_view);
2937 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2939 GtkTreeIter *parent)
2941 GtkComboBoxPrivate *priv = combo_box->priv;
2942 GtkTreeModel *model = priv->model;
2943 GtkWidget *item, *submenu, *subitem, *separator;
2945 gboolean is_separator;
2950 n_children = gtk_tree_model_iter_n_children (model, parent);
2953 for (i = 0; i < n_children; i++)
2955 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2957 if (priv->row_separator_func)
2958 is_separator = priv->row_separator_func (priv->model, &iter,
2959 priv->row_separator_data);
2961 is_separator = FALSE;
2965 item = gtk_separator_menu_item_new ();
2966 path = gtk_tree_model_get_path (model, &iter);
2967 g_object_set_data_full (G_OBJECT (item),
2968 I_("gtk-combo-box-item-path"),
2969 gtk_tree_row_reference_new (model, path),
2970 (GDestroyNotify)gtk_tree_row_reference_free);
2971 gtk_tree_path_free (path);
2975 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2976 if (gtk_tree_model_iter_has_child (model, &iter))
2978 submenu = gtk_menu_new ();
2979 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
2980 gtk_widget_show (submenu);
2981 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2983 /* Ugly - since menus can only activate leafs, we have to
2984 * duplicate the item inside the submenu.
2986 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2987 separator = gtk_separator_menu_item_new ();
2988 gtk_widget_show (subitem);
2989 gtk_widget_show (separator);
2990 g_signal_connect (subitem, "activate",
2991 G_CALLBACK (gtk_combo_box_menu_item_activate),
2993 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2994 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2996 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2998 g_signal_connect (item, "activate",
2999 G_CALLBACK (gtk_combo_box_menu_item_activate),
3003 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3004 if (priv->wrap_width && menu == priv->popup_widget)
3005 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3006 gtk_widget_show (item);
3013 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3015 GtkComboBoxPrivate *priv = combo_box->priv;
3017 g_signal_handlers_disconnect_matched (priv->button,
3018 G_SIGNAL_MATCH_DATA,
3020 gtk_combo_box_menu_button_press, NULL);
3021 g_signal_handlers_disconnect_matched (priv->button,
3022 G_SIGNAL_MATCH_DATA,
3024 gtk_combo_box_button_state_changed, combo_box);
3026 /* unparent will remove our latest ref */
3027 gtk_widget_unparent (priv->button);
3030 priv->button = NULL;
3032 priv->separator = NULL;
3034 g_object_unref (priv->column);
3035 priv->column = NULL;
3037 /* changing the popup window will unref the menu and the children */
3045 menu_occupied (GtkMenu *menu,
3049 guint bottom_attach)
3053 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3057 gtk_container_child_get (GTK_CONTAINER (menu),
3061 "bottom-attach", &b,
3065 /* look if this item intersects with the given coordinates */
3066 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3074 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3079 GtkComboBoxPrivate *priv = combo_box->priv;
3080 gint current_col = 0, current_row = 0;
3081 gint rows = 1, cols = 1;
3082 GtkWidget *menu = priv->popup_widget;
3084 if (!GTK_IS_MENU_SHELL (menu))
3087 if (priv->col_column == -1 &&
3088 priv->row_column == -1 &&
3091 gtk_container_child_get (GTK_CONTAINER (menu),
3093 "right-attach", ¤t_col,
3094 "top-attach", ¤t_row,
3096 if (current_col + cols > priv->wrap_width)
3104 if (priv->col_column != -1)
3105 gtk_tree_model_get (priv->model, iter,
3106 priv->col_column, &cols,
3108 if (priv->row_column != -1)
3109 gtk_tree_model_get (priv->model, iter,
3110 priv->row_column, &rows,
3115 if (current_col + cols > priv->wrap_width)
3121 if (!menu_occupied (GTK_MENU (menu),
3122 current_col, current_col + cols,
3123 current_row, current_row + rows))
3130 /* set attach props */
3131 gtk_menu_attach (GTK_MENU (menu), item,
3132 current_col, current_col + cols,
3133 current_row, current_row + rows);
3137 gtk_combo_box_relayout (GtkComboBox *combo_box)
3142 menu = combo_box->priv->popup_widget;
3144 /* do nothing unless we are in menu style and realized */
3145 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3148 list = gtk_container_get_children (GTK_CONTAINER (menu));
3150 for (j = g_list_last (list); j; j = j->prev)
3151 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3153 gtk_combo_box_menu_fill (combo_box);
3160 gtk_combo_box_menu_button_press (GtkWidget *widget,
3161 GdkEventButton *event,
3164 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3165 GtkComboBoxPrivate *priv = combo_box->priv;
3167 if (GTK_IS_MENU (priv->popup_widget) &&
3168 event->type == GDK_BUTTON_PRESS && event->button == 1)
3170 if (priv->focus_on_click &&
3171 !gtk_widget_has_focus (priv->button))
3172 gtk_widget_grab_focus (priv->button);
3174 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3183 gtk_combo_box_menu_item_activate (GtkWidget *item,
3186 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3187 GtkWidget *cell_view;
3191 cell_view = GTK_BIN (item)->child;
3193 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3195 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3197 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3199 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3200 gtk_combo_box_set_active_iter (combo_box, &iter);
3203 gtk_tree_path_free (path);
3205 g_object_set (combo_box,
3206 "editing-canceled", FALSE,
3211 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3214 gboolean sensitive = TRUE; /* fool code checkers */
3216 if (!combo_box->priv->button)
3219 switch (combo_box->priv->button_sensitivity)
3221 case GTK_SENSITIVITY_ON:
3224 case GTK_SENSITIVITY_OFF:
3227 case GTK_SENSITIVITY_AUTO:
3228 sensitive = combo_box->priv->model &&
3229 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3232 g_assert_not_reached ();
3236 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3238 /* In list-mode, we also need to update sensitivity of the event box */
3239 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3240 && combo_box->priv->cell_view)
3241 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3245 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3250 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3252 if (combo_box->priv->tree_view)
3253 gtk_combo_box_list_popup_resize (combo_box);
3255 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3257 gtk_combo_box_update_sensitivity (combo_box);
3261 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3265 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3266 GtkComboBoxPrivate *priv = combo_box->priv;
3268 if (!gtk_tree_row_reference_valid (priv->active_row))
3270 if (priv->cell_view)
3271 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3272 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3275 if (priv->tree_view)
3276 gtk_combo_box_list_popup_resize (combo_box);
3278 gtk_combo_box_menu_row_deleted (model, path, user_data);
3280 gtk_combo_box_update_sensitivity (combo_box);
3284 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3290 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3292 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3294 if (!combo_box->priv->tree_view)
3295 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3299 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3304 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3305 GtkComboBoxPrivate *priv = combo_box->priv;
3306 GtkTreePath *active_path;
3308 /* FIXME this belongs to GtkCellView */
3309 if (gtk_tree_row_reference_valid (priv->active_row))
3311 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3312 if (gtk_tree_path_compare (path, active_path) == 0 &&
3314 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3315 gtk_tree_path_free (active_path);
3318 if (priv->tree_view)
3319 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3321 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3325 list_popup_resize_idle (gpointer user_data)
3327 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3328 GtkComboBoxPrivate *priv = combo_box->priv;
3329 gint x, y, width, height;
3331 if (priv->tree_view && GTK_WIDGET_MAPPED (priv->popup_window))
3333 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3335 gtk_widget_set_size_request (priv->popup_window, width, height);
3336 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3339 priv->resize_idle_id = 0;
3345 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3347 GtkComboBoxPrivate *priv = combo_box->priv;
3349 if (!priv->resize_idle_id)
3350 priv->resize_idle_id =
3351 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3355 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3360 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3362 gtk_combo_box_list_popup_resize (combo_box);
3367 find_menu_by_path (GtkWidget *menu,
3369 gboolean skip_first)
3374 GtkTreeRowReference *mref;
3378 list = gtk_container_get_children (GTK_CONTAINER (menu));
3381 for (i = list; i; i = i->next)
3383 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3385 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3388 else if (!gtk_tree_row_reference_valid (mref))
3391 mpath = gtk_tree_row_reference_get_path (mref);
3393 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3401 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3406 /* this case is necessary, since the row reference of
3407 * the cell view may already be updated after a deletion
3414 if (gtk_tree_path_compare (mpath, path) == 0)
3416 gtk_tree_path_free (mpath);
3420 if (gtk_tree_path_is_ancestor (mpath, path))
3422 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3423 if (submenu != NULL)
3425 gtk_tree_path_free (mpath);
3426 item = find_menu_by_path (submenu, path, TRUE);
3430 gtk_tree_path_free (mpath);
3440 dump_menu_tree (GtkWidget *menu,
3447 list = gtk_container_get_children (GTK_CONTAINER (menu));
3448 for (i = list; i; i = i->next)
3450 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3452 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3453 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3454 gtk_tree_path_free (path);
3456 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3457 if (submenu != NULL)
3458 dump_menu_tree (submenu, level + 1);
3467 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3472 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3473 GtkComboBoxPrivate *priv = combo_box->priv;
3475 GtkWidget *item, *menu, *separator;
3479 gboolean is_separator;
3481 if (!priv->popup_widget)
3484 depth = gtk_tree_path_get_depth (path);
3485 pos = gtk_tree_path_get_indices (path)[depth - 1];
3488 ppath = gtk_tree_path_copy (path);
3489 gtk_tree_path_up (ppath);
3490 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3491 gtk_tree_path_free (ppath);
3493 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3496 menu = gtk_menu_new ();
3497 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3498 gtk_widget_show (menu);
3499 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3501 /* Ugly - since menus can only activate leaves, we have to
3502 * duplicate the item inside the submenu.
3504 gtk_tree_model_iter_parent (model, &piter, iter);
3505 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3506 separator = gtk_separator_menu_item_new ();
3507 g_signal_connect (item, "activate",
3508 G_CALLBACK (gtk_combo_box_menu_item_activate),
3510 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3511 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3512 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3514 gtk_widget_show (item);
3515 gtk_widget_show (separator);
3522 menu = priv->popup_widget;
3523 if (priv->add_tearoffs)
3527 if (priv->row_separator_func)
3528 is_separator = priv->row_separator_func (model, iter,
3529 priv->row_separator_data);
3531 is_separator = FALSE;
3535 item = gtk_separator_menu_item_new ();
3536 g_object_set_data_full (G_OBJECT (item),
3537 I_("gtk-combo-box-item-path"),
3538 gtk_tree_row_reference_new (model, path),
3539 (GDestroyNotify)gtk_tree_row_reference_free);
3543 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3545 g_signal_connect (item, "activate",
3546 G_CALLBACK (gtk_combo_box_menu_item_activate),
3550 gtk_widget_show (item);
3551 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3555 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3559 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3560 GtkComboBoxPrivate *priv = combo_box->priv;
3564 if (!priv->popup_widget)
3567 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3568 menu = gtk_widget_get_parent (item);
3569 gtk_container_remove (GTK_CONTAINER (menu), item);
3571 if (gtk_tree_path_get_depth (path) > 1)
3573 GtkTreePath *parent_path;
3577 parent_path = gtk_tree_path_copy (path);
3578 gtk_tree_path_up (parent_path);
3579 gtk_tree_model_get_iter (model, &iter, parent_path);
3581 if (!gtk_tree_model_iter_has_child (model, &iter))
3583 parent = find_menu_by_path (priv->popup_widget,
3584 parent_path, FALSE);
3585 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3591 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3597 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3599 gtk_combo_box_relayout (combo_box);
3603 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3608 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3609 GtkComboBoxPrivate *priv = combo_box->priv;
3612 gboolean is_separator;
3614 if (!priv->popup_widget)
3617 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3619 if (priv->row_separator_func)
3620 is_separator = priv->row_separator_func (model, iter,
3621 priv->row_separator_data);
3623 is_separator = FALSE;
3625 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3627 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3628 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3631 if (priv->wrap_width && item->parent == priv->popup_widget)
3633 GtkWidget *pitem = NULL;
3636 prev = gtk_tree_path_copy (path);
3638 if (gtk_tree_path_prev (prev))
3639 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3641 gtk_tree_path_free (prev);
3643 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3645 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3650 "bottom-attach", -1,
3653 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3656 width = gtk_combo_box_calc_requested_width (combo_box, path);
3658 if (width > priv->width)
3660 if (priv->cell_view)
3662 gtk_widget_set_size_request (priv->cell_view, width, -1);
3663 gtk_widget_queue_resize (priv->cell_view);
3665 priv->width = width;
3674 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3676 GtkComboBoxPrivate *priv = combo_box->priv;
3677 GtkTreeSelection *sel;
3679 priv->button = gtk_toggle_button_new ();
3680 gtk_widget_set_parent (priv->button,
3681 GTK_BIN (combo_box)->child->parent);
3682 g_signal_connect (priv->button, "button-press-event",
3683 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3684 g_signal_connect (priv->button, "toggled",
3685 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3687 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3688 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3689 priv->separator = NULL;
3690 gtk_widget_show_all (priv->button);
3692 if (priv->cell_view)
3694 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3695 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3697 priv->box = gtk_event_box_new ();
3698 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3701 if (priv->has_frame)
3703 priv->cell_view_frame = gtk_frame_new (NULL);
3704 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3709 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3710 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3714 gtk_widget_set_parent (priv->cell_view_frame,
3715 GTK_BIN (combo_box)->child->parent);
3716 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3717 gtk_widget_show_all (priv->cell_view_frame);
3719 g_signal_connect (priv->box, "button-press-event",
3720 G_CALLBACK (gtk_combo_box_list_button_pressed),
3724 priv->tree_view = gtk_tree_view_new ();
3725 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3726 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3727 gtk_tree_selection_set_select_function (sel,
3728 gtk_combo_box_list_select_func,
3730 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3732 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3734 if (priv->row_separator_func)
3735 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3736 priv->row_separator_func,
3737 priv->row_separator_data,
3740 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3742 priv->column = gtk_tree_view_column_new ();
3743 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3746 gtk_combo_box_sync_cells (combo_box,
3747 GTK_CELL_LAYOUT (priv->column));
3749 if (gtk_tree_row_reference_valid (priv->active_row))
3753 path = gtk_tree_row_reference_get_path (priv->active_row);
3754 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3756 gtk_tree_path_free (path);
3759 /* set sample/popup widgets */
3760 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3762 g_signal_connect (priv->tree_view, "key-press-event",
3763 G_CALLBACK (gtk_combo_box_list_key_press),
3765 g_signal_connect (priv->tree_view, "enter-notify-event",
3766 G_CALLBACK (gtk_combo_box_list_enter_notify),
3768 g_signal_connect (priv->tree_view, "row-expanded",
3769 G_CALLBACK (gtk_combo_box_model_row_expanded),
3771 g_signal_connect (priv->tree_view, "row-collapsed",
3772 G_CALLBACK (gtk_combo_box_model_row_expanded),
3774 g_signal_connect (priv->popup_window, "button-press-event",
3775 G_CALLBACK (gtk_combo_box_list_button_pressed),
3777 g_signal_connect (priv->popup_window, "button-release-event",
3778 G_CALLBACK (gtk_combo_box_list_button_released),
3781 gtk_widget_show (priv->tree_view);
3783 gtk_combo_box_update_sensitivity (combo_box);
3787 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3789 GtkComboBoxPrivate *priv = combo_box->priv;
3791 /* disconnect signals */
3792 g_signal_handlers_disconnect_matched (priv->tree_view,
3793 G_SIGNAL_MATCH_DATA,
3794 0, 0, NULL, NULL, combo_box);
3795 g_signal_handlers_disconnect_matched (priv->button,
3796 G_SIGNAL_MATCH_DATA,
3798 gtk_combo_box_list_button_pressed,
3800 g_signal_handlers_disconnect_matched (priv->popup_window,
3801 G_SIGNAL_MATCH_DATA,
3803 gtk_combo_box_list_button_pressed,
3805 g_signal_handlers_disconnect_matched (priv->popup_window,
3806 G_SIGNAL_MATCH_DATA,
3808 gtk_combo_box_list_button_released,
3811 g_signal_handlers_disconnect_matched (priv->popup_window,
3812 G_SIGNAL_MATCH_DATA,
3814 gtk_combo_box_child_show,
3817 g_signal_handlers_disconnect_matched (priv->popup_window,
3818 G_SIGNAL_MATCH_DATA,
3820 gtk_combo_box_child_hide,
3824 g_signal_handlers_disconnect_matched (priv->box,
3825 G_SIGNAL_MATCH_DATA,
3827 gtk_combo_box_list_button_pressed,
3830 /* destroy things (unparent will kill the latest ref from us)
3831 * last unref on button will destroy the arrow
3833 gtk_widget_unparent (priv->button);
3834 priv->button = NULL;
3837 if (priv->cell_view)
3839 g_object_set (priv->cell_view,
3840 "background-set", FALSE,
3844 if (priv->cell_view_frame)
3846 gtk_widget_unparent (priv->cell_view_frame);
3847 priv->cell_view_frame = NULL;
3851 if (priv->scroll_timer)
3853 g_source_remove (priv->scroll_timer);
3854 priv->scroll_timer = 0;
3857 if (priv->resize_idle_id)
3859 g_source_remove (priv->resize_idle_id);
3860 priv->resize_idle_id = 0;
3863 gtk_widget_destroy (priv->tree_view);
3865 priv->tree_view = NULL;
3866 if (priv->popup_widget)
3868 g_object_unref (priv->popup_widget);
3869 priv->popup_widget = NULL;
3876 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3877 GdkEventButton *event,
3880 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3881 GtkComboBoxPrivate *priv = combo_box->priv;
3883 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3885 if (ewidget == priv->popup_window)
3888 if ((ewidget != priv->button && ewidget != priv->box) ||
3889 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3892 if (priv->focus_on_click &&
3893 !gtk_widget_has_focus (priv->button))
3894 gtk_widget_grab_focus (priv->button);
3896 gtk_combo_box_popup (combo_box);
3898 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3900 priv->auto_scroll = FALSE;
3901 if (priv->scroll_timer == 0)
3902 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3903 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3906 priv->popup_in_progress = TRUE;
3912 gtk_combo_box_list_button_released (GtkWidget *widget,
3913 GdkEventButton *event,
3917 GtkTreePath *path = NULL;
3920 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3921 GtkComboBoxPrivate *priv = combo_box->priv;
3923 gboolean popup_in_progress = FALSE;
3925 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3927 if (priv->popup_in_progress)
3929 popup_in_progress = TRUE;
3930 priv->popup_in_progress = FALSE;
3933 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3935 if (priv->scroll_timer)
3937 g_source_remove (priv->scroll_timer);
3938 priv->scroll_timer = 0;
3941 if (ewidget != priv->tree_view)
3943 if ((ewidget == priv->button ||
3944 ewidget == priv->box) &&
3945 !popup_in_progress &&
3946 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3948 gtk_combo_box_popdown (combo_box);
3952 /* released outside treeview */
3953 if (ewidget != priv->button && ewidget != priv->box)
3955 gtk_combo_box_popdown (combo_box);
3963 /* select something cool */
3964 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3970 return TRUE; /* clicked outside window? */
3972 gtk_tree_model_get_iter (priv->model, &iter, path);
3973 gtk_tree_path_free (path);
3975 gtk_combo_box_popdown (combo_box);
3977 if (tree_column_row_is_sensitive (combo_box, &iter))
3978 gtk_combo_box_set_active_iter (combo_box, &iter);
3984 gtk_combo_box_menu_key_press (GtkWidget *widget,
3988 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3990 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3992 /* The menu hasn't managed the
3993 * event, forward it to the combobox
3995 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4002 gtk_combo_box_list_key_press (GtkWidget *widget,
4006 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4009 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
4010 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
4012 GtkTreeModel *model = NULL;
4014 gtk_combo_box_popdown (combo_box);
4016 if (combo_box->priv->model)
4018 GtkTreeSelection *sel;
4020 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4022 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4023 gtk_combo_box_set_active_iter (combo_box, &iter);
4029 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4031 /* The list hasn't managed the
4032 * event, forward it to the combobox
4034 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4041 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4045 GtkWidget *tree_view = combo_box->priv->tree_view;
4049 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4050 if (adj && adj->upper - adj->lower > adj->page_size)
4052 if (x <= tree_view->allocation.x &&
4053 adj->lower < adj->value)
4055 value = adj->value - (tree_view->allocation.x - x + 1);
4056 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
4058 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4059 adj->upper - adj->page_size > adj->value)
4061 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4062 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
4066 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4067 if (adj && adj->upper - adj->lower > adj->page_size)
4069 if (y <= tree_view->allocation.y &&
4070 adj->lower < adj->value)
4072 value = adj->value - (tree_view->allocation.y - y + 1);
4073 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
4075 else if (y >= tree_view->allocation.height &&
4076 adj->upper - adj->page_size > adj->value)
4078 value = adj->value + (y - tree_view->allocation.height + 1);
4079 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
4085 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4087 GtkComboBoxPrivate *priv = combo_box->priv;
4090 if (priv->auto_scroll)
4092 gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
4093 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4100 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4101 GdkEventCrossing *event,
4104 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4106 combo_box->priv->auto_scroll = TRUE;
4112 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4113 GtkTreeModel *model,
4115 gboolean path_currently_selected,
4119 gboolean sensitive = FALSE;
4121 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4123 GList *cells, *cell;
4124 gboolean cell_sensitive, cell_visible;
4126 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4128 if (!column->visible)
4131 gtk_tree_model_get_iter (model, &iter, path);
4132 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4135 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4138 g_object_get (cell->data,
4139 "sensitive", &cell_sensitive,
4140 "visible", &cell_visible,
4143 if (cell_visible && cell_sensitive)
4148 g_list_free (cells);
4150 sensitive = cell_sensitive;
4157 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4162 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4163 GtkComboBoxPrivate *priv = combo_box->priv;
4166 width = gtk_combo_box_calc_requested_width (combo_box, path);
4168 if (width > priv->width)
4170 if (priv->cell_view)
4172 gtk_widget_set_size_request (priv->cell_view, width, -1);
4173 gtk_widget_queue_resize (priv->cell_view);
4175 priv->width = width;
4180 * GtkCellLayout implementation
4184 pack_start_recurse (GtkWidget *menu,
4185 GtkCellRenderer *cell,
4191 list = gtk_container_get_children (GTK_CONTAINER (menu));
4192 for (i = list; i; i = i->next)
4194 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4195 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4198 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4199 if (submenu != NULL)
4200 pack_start_recurse (submenu, cell, expand);
4207 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4208 GtkCellRenderer *cell,
4211 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4212 ComboCellInfo *info;
4213 GtkComboBoxPrivate *priv;
4215 priv = combo_box->priv;
4217 g_object_ref_sink (cell);
4219 info = g_slice_new0 (ComboCellInfo);
4221 info->expand = expand;
4222 info->pack = GTK_PACK_START;
4224 priv->cells = g_slist_append (priv->cells, info);
4226 if (priv->cell_view)
4227 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4231 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4233 if (GTK_IS_MENU (priv->popup_widget))
4234 pack_start_recurse (priv->popup_widget, cell, expand);
4238 pack_end_recurse (GtkWidget *menu,
4239 GtkCellRenderer *cell,
4245 list = gtk_container_get_children (GTK_CONTAINER (menu));
4246 for (i = list; i; i = i->next)
4248 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4249 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4252 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4253 if (submenu != NULL)
4254 pack_end_recurse (submenu, cell, expand);
4261 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4262 GtkCellRenderer *cell,
4265 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4266 ComboCellInfo *info;
4267 GtkComboBoxPrivate *priv;
4269 priv = combo_box->priv;
4271 g_object_ref_sink (cell);
4273 info = g_slice_new0 (ComboCellInfo);
4275 info->expand = expand;
4276 info->pack = GTK_PACK_END;
4278 priv->cells = g_slist_append (priv->cells, info);
4280 if (priv->cell_view)
4281 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4285 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4287 if (GTK_IS_MENU (priv->popup_widget))
4288 pack_end_recurse (priv->popup_widget, cell, expand);
4292 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4294 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4296 GList *retval = NULL;
4298 for (list = combo_box->priv->cells; list; list = list->next)
4300 ComboCellInfo *info = (ComboCellInfo *)list->data;
4302 retval = g_list_prepend (retval, info->cell);
4305 return g_list_reverse (retval);
4309 clear_recurse (GtkWidget *menu)
4314 list = gtk_container_get_children (GTK_CONTAINER (menu));
4315 for (i = list; i; i = i->next)
4317 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4318 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4320 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4321 if (submenu != NULL)
4322 clear_recurse (submenu);
4329 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4331 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4332 GtkComboBoxPrivate *priv = combo_box->priv;
4335 if (priv->cell_view)
4336 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4339 gtk_tree_view_column_clear (priv->column);
4341 for (i = priv->cells; i; i = i->next)
4343 ComboCellInfo *info = (ComboCellInfo *)i->data;
4345 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4346 g_object_unref (info->cell);
4347 g_slice_free (ComboCellInfo, info);
4350 g_slist_free (priv->cells);
4353 if (GTK_IS_MENU (priv->popup_widget))
4354 clear_recurse (priv->popup_widget);
4358 add_attribute_recurse (GtkWidget *menu,
4359 GtkCellRenderer *cell,
4360 const gchar *attribute,
4366 list = gtk_container_get_children (GTK_CONTAINER (menu));
4367 for (i = list; i; i = i->next)
4369 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4370 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4371 cell, attribute, column);
4373 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4374 if (submenu != NULL)
4375 add_attribute_recurse (submenu, cell, attribute, column);
4382 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4383 GtkCellRenderer *cell,
4384 const gchar *attribute,
4387 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4388 ComboCellInfo *info;
4390 info = gtk_combo_box_get_cell_info (combo_box, cell);
4391 g_return_if_fail (info != NULL);
4393 info->attributes = g_slist_prepend (info->attributes,
4394 GINT_TO_POINTER (column));
4395 info->attributes = g_slist_prepend (info->attributes,
4396 g_strdup (attribute));
4398 if (combo_box->priv->cell_view)
4399 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4400 cell, attribute, column);
4402 if (combo_box->priv->column)
4403 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4404 cell, attribute, column);
4406 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4407 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4408 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4412 combo_cell_data_func (GtkCellLayout *cell_layout,
4413 GtkCellRenderer *cell,
4414 GtkTreeModel *tree_model,
4418 ComboCellInfo *info = (ComboCellInfo *)data;
4419 GtkWidget *parent = NULL;
4424 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4426 if (GTK_IS_WIDGET (cell_layout))
4427 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4429 if (GTK_IS_MENU_ITEM (parent) &&
4430 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4431 g_object_set (cell, "sensitive", TRUE, NULL);
4436 set_cell_data_func_recurse (GtkWidget *menu,
4437 GtkCellRenderer *cell,
4438 ComboCellInfo *info)
4442 GtkWidget *cell_view;
4444 list = gtk_container_get_children (GTK_CONTAINER (menu));
4445 for (i = list; i; i = i->next)
4447 cell_view = GTK_BIN (i->data)->child;
4448 if (GTK_IS_CELL_LAYOUT (cell_view))
4450 /* Override sensitivity for inner nodes; we don't
4451 * want menuitems with submenus to appear insensitive
4453 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4455 combo_cell_data_func,
4457 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4458 if (submenu != NULL)
4459 set_cell_data_func_recurse (submenu, cell, info);
4467 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4468 GtkCellRenderer *cell,
4469 GtkCellLayoutDataFunc func,
4471 GDestroyNotify destroy)
4473 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4474 GtkComboBoxPrivate *priv = combo_box->priv;
4475 ComboCellInfo *info;
4477 info = gtk_combo_box_get_cell_info (combo_box, cell);
4478 g_return_if_fail (info != NULL);
4482 GDestroyNotify d = info->destroy;
4484 info->destroy = NULL;
4485 d (info->func_data);
4489 info->func_data = func_data;
4490 info->destroy = destroy;
4492 if (priv->cell_view)
4493 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4496 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4498 if (GTK_IS_MENU (priv->popup_widget))
4499 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4501 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4505 clear_attributes_recurse (GtkWidget *menu,
4506 GtkCellRenderer *cell)
4511 list = gtk_container_get_children (GTK_CONTAINER (menu));
4512 for (i = list; i; i = i->next)
4514 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4515 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4518 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4519 if (submenu != NULL)
4520 clear_attributes_recurse (submenu, cell);
4527 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4528 GtkCellRenderer *cell)
4530 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4531 GtkComboBoxPrivate *priv;
4532 ComboCellInfo *info;
4535 priv = combo_box->priv;
4537 info = gtk_combo_box_get_cell_info (combo_box, cell);
4538 g_return_if_fail (info != NULL);
4540 list = info->attributes;
4541 while (list && list->next)
4543 g_free (list->data);
4544 list = list->next->next;
4546 g_slist_free (info->attributes);
4547 info->attributes = NULL;
4549 if (priv->cell_view)
4550 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4553 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4555 if (GTK_IS_MENU (priv->popup_widget))
4556 clear_attributes_recurse (priv->popup_widget, cell);
4558 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4562 reorder_recurse (GtkWidget *menu,
4563 GtkCellRenderer *cell,
4569 list = gtk_container_get_children (GTK_CONTAINER (menu));
4570 for (i = list; i; i = i->next)
4572 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4573 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4576 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4577 if (submenu != NULL)
4578 reorder_recurse (submenu, cell, position);
4585 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4586 GtkCellRenderer *cell,
4589 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4590 GtkComboBoxPrivate *priv;
4591 ComboCellInfo *info;
4594 priv = combo_box->priv;
4596 info = gtk_combo_box_get_cell_info (combo_box, cell);
4598 g_return_if_fail (info != NULL);
4599 g_return_if_fail (position >= 0);
4601 link = g_slist_find (priv->cells, info);
4603 g_return_if_fail (link != NULL);
4605 priv->cells = g_slist_delete_link (priv->cells, link);
4606 priv->cells = g_slist_insert (priv->cells, info, position);
4608 if (priv->cell_view)
4609 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4613 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4616 if (GTK_IS_MENU (priv->popup_widget))
4617 reorder_recurse (priv->popup_widget, cell, position);
4619 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4627 * gtk_combo_box_new:
4629 * Creates a new empty #GtkComboBox.
4631 * Return value: A new #GtkComboBox.
4636 gtk_combo_box_new (void)
4638 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4642 * gtk_combo_box_new_with_model:
4643 * @model: A #GtkTreeModel.
4645 * Creates a new #GtkComboBox with the model initialized to @model.
4647 * Return value: A new #GtkComboBox.
4652 gtk_combo_box_new_with_model (GtkTreeModel *model)
4654 GtkComboBox *combo_box;
4656 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4658 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4660 return GTK_WIDGET (combo_box);
4664 * gtk_combo_box_get_wrap_width:
4665 * @combo_box: A #GtkComboBox
4667 * Returns the wrap width which is used to determine the number of columns
4668 * for the popup menu. If the wrap width is larger than 1, the combo box
4671 * Returns: the wrap width.
4676 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4678 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4680 return combo_box->priv->wrap_width;
4684 * gtk_combo_box_set_wrap_width:
4685 * @combo_box: A #GtkComboBox
4686 * @width: Preferred number of columns
4688 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4689 * the preferred number of columns when you want the popup to be layed out
4695 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4698 GtkComboBoxPrivate *priv;
4700 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4701 g_return_if_fail (width >= 0);
4703 priv = combo_box->priv;
4705 if (width != priv->wrap_width)
4707 priv->wrap_width = width;
4709 gtk_combo_box_check_appearance (combo_box);
4710 gtk_combo_box_relayout (combo_box);
4712 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4717 * gtk_combo_box_get_row_span_column:
4718 * @combo_box: A #GtkComboBox
4720 * Returns the column with row span information for @combo_box.
4722 * Returns: the row span column.
4727 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4729 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4731 return combo_box->priv->row_column;
4735 * gtk_combo_box_set_row_span_column:
4736 * @combo_box: A #GtkComboBox.
4737 * @row_span: A column in the model passed during construction.
4739 * Sets the column with row span information for @combo_box to be @row_span.
4740 * The row span column contains integers which indicate how many rows
4741 * an item should span.
4746 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4749 GtkComboBoxPrivate *priv;
4752 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4754 priv = combo_box->priv;
4756 col = gtk_tree_model_get_n_columns (priv->model);
4757 g_return_if_fail (row_span >= -1 && row_span < col);
4759 if (row_span != priv->row_column)
4761 priv->row_column = row_span;
4763 gtk_combo_box_relayout (combo_box);
4765 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4770 * gtk_combo_box_get_column_span_column:
4771 * @combo_box: A #GtkComboBox
4773 * Returns the column with column span information for @combo_box.
4775 * Returns: the column span column.
4780 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4782 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4784 return combo_box->priv->col_column;
4788 * gtk_combo_box_set_column_span_column:
4789 * @combo_box: A #GtkComboBox
4790 * @column_span: A column in the model passed during construction
4792 * Sets the column with column span information for @combo_box to be
4793 * @column_span. The column span column contains integers which indicate
4794 * how many columns an item should span.
4799 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4802 GtkComboBoxPrivate *priv;
4805 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4807 priv = combo_box->priv;
4809 col = gtk_tree_model_get_n_columns (priv->model);
4810 g_return_if_fail (column_span >= -1 && column_span < col);
4812 if (column_span != priv->col_column)
4814 priv->col_column = column_span;
4816 gtk_combo_box_relayout (combo_box);
4818 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4823 * gtk_combo_box_get_active:
4824 * @combo_box: A #GtkComboBox
4826 * Returns the index of the currently active item, or -1 if there's no
4827 * active item. If the model is a non-flat treemodel, and the active item
4828 * is not an immediate child of the root of the tree, this function returns
4829 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4830 * <literal>path</literal> is the #GtkTreePath of the active item.
4832 * Return value: An integer which is the index of the currently active item,
4833 * or -1 if there's no active item.
4838 gtk_combo_box_get_active (GtkComboBox *combo_box)
4840 GtkComboBoxPrivate *priv;
4843 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4845 priv = combo_box->priv;
4847 if (gtk_tree_row_reference_valid (priv->active_row))
4851 path = gtk_tree_row_reference_get_path (priv->active_row);
4852 result = gtk_tree_path_get_indices (path)[0];
4853 gtk_tree_path_free (path);
4862 * gtk_combo_box_set_active:
4863 * @combo_box: A #GtkComboBox
4864 * @index_: An index in the model passed during construction, or -1 to have
4867 * Sets the active item of @combo_box to be the item at @index.
4872 gtk_combo_box_set_active (GtkComboBox *combo_box,
4875 GtkTreePath *path = NULL;
4876 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4877 g_return_if_fail (index_ >= -1);
4879 if (combo_box->priv->model == NULL)
4881 /* Save index, in case the model is set after the index */
4882 combo_box->priv->active = index_;
4888 path = gtk_tree_path_new_from_indices (index_, -1);
4890 gtk_combo_box_set_active_internal (combo_box, path);
4893 gtk_tree_path_free (path);
4897 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4900 GtkComboBoxPrivate *priv = combo_box->priv;
4901 GtkTreePath *active_path;
4904 /* Remember whether the initially active row is valid. */
4905 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4907 if (path && is_valid_row_reference)
4909 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4910 path_cmp = gtk_tree_path_compare (path, active_path);
4911 gtk_tree_path_free (active_path);
4916 if (priv->active_row)
4918 gtk_tree_row_reference_free (priv->active_row);
4919 priv->active_row = NULL;
4924 if (priv->tree_view)
4925 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4928 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4930 if (GTK_IS_MENU (menu))
4931 gtk_menu_set_active (menu, -1);
4934 if (priv->cell_view)
4935 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4938 * Do not emit a "changed" signal when an already invalid selection was
4939 * now set to invalid.
4941 if (!is_valid_row_reference)
4947 gtk_tree_row_reference_new (priv->model, path);
4949 if (priv->tree_view)
4951 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4954 else if (GTK_IS_MENU (priv->popup_widget))
4956 /* FIXME handle nested menus better */
4957 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4958 gtk_tree_path_get_indices (path)[0]);
4961 if (priv->cell_view)
4962 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4966 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4967 g_object_notify (G_OBJECT (combo_box), "active");
4972 * gtk_combo_box_get_active_iter:
4973 * @combo_box: A #GtkComboBox
4974 * @iter: The uninitialized #GtkTreeIter
4976 * Sets @iter to point to the current active item, if it exists.
4978 * Return value: %TRUE, if @iter was set
4983 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4989 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4991 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4994 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4995 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4996 gtk_tree_path_free (path);
5002 * gtk_combo_box_set_active_iter:
5003 * @combo_box: A #GtkComboBox
5004 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5006 * Sets the current active item to be the one referenced by @iter, or
5007 * unsets the active item if @iter is %NULL.
5009 * @iter must correspond to a path of depth one, or be %NULL.
5014 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5017 GtkTreePath *path = NULL;
5019 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5022 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5024 gtk_combo_box_set_active_internal (combo_box, path);
5025 gtk_tree_path_free (path);
5029 * gtk_combo_box_set_model:
5030 * @combo_box: A #GtkComboBox
5031 * @model: (allow-none): A #GtkTreeModel
5033 * Sets the model used by @combo_box to be @model. Will unset a previously set
5034 * model (if applicable). If model is %NULL, then it will unset the model.
5036 * Note that this function does not clear the cell renderers, you have to
5037 * call gtk_cell_layout_clear() yourself if you need to set up different
5038 * cell renderers for the new model.
5043 gtk_combo_box_set_model (GtkComboBox *combo_box,
5044 GtkTreeModel *model)
5046 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5047 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5049 if (model == combo_box->priv->model)
5052 gtk_combo_box_unset_model (combo_box);
5057 combo_box->priv->model = model;
5058 g_object_ref (combo_box->priv->model);
5060 combo_box->priv->inserted_id =
5061 g_signal_connect (combo_box->priv->model, "row-inserted",
5062 G_CALLBACK (gtk_combo_box_model_row_inserted),
5064 combo_box->priv->deleted_id =
5065 g_signal_connect (combo_box->priv->model, "row-deleted",
5066 G_CALLBACK (gtk_combo_box_model_row_deleted),
5068 combo_box->priv->reordered_id =
5069 g_signal_connect (combo_box->priv->model, "rows-reordered",
5070 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5072 combo_box->priv->changed_id =
5073 g_signal_connect (combo_box->priv->model, "row-changed",
5074 G_CALLBACK (gtk_combo_box_model_row_changed),
5077 if (combo_box->priv->tree_view)
5080 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5081 combo_box->priv->model);
5082 gtk_combo_box_list_popup_resize (combo_box);
5087 if (combo_box->priv->popup_widget)
5088 gtk_combo_box_menu_fill (combo_box);
5092 if (combo_box->priv->cell_view)
5093 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5094 combo_box->priv->model);
5096 if (combo_box->priv->active != -1)
5098 /* If an index was set in advance, apply it now */
5099 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5100 combo_box->priv->active = -1;
5104 gtk_combo_box_update_sensitivity (combo_box);
5106 g_object_notify (G_OBJECT (combo_box), "model");
5110 * gtk_combo_box_get_model
5111 * @combo_box: A #GtkComboBox
5113 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5115 * Return value: (transfer none): A #GtkTreeModel which was passed during construction.
5120 gtk_combo_box_get_model (GtkComboBox *combo_box)
5122 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5124 return combo_box->priv->model;
5128 /* convenience API for simple text combos */
5131 * gtk_combo_box_new_text:
5133 * Convenience function which constructs a new text combo box, which is a
5134 * #GtkComboBox just displaying strings. If you use this function to create
5135 * a text combo box, you should only manipulate its data source with the
5136 * following convenience functions: gtk_combo_box_append_text(),
5137 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5138 * gtk_combo_box_remove_text().
5140 * Return value: (transfer none): A new text combo box.
5145 gtk_combo_box_new_text (void)
5147 GtkWidget *combo_box;
5148 GtkCellRenderer *cell;
5149 GtkListStore *store;
5151 store = gtk_list_store_new (1, G_TYPE_STRING);
5152 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5153 g_object_unref (store);
5155 cell = gtk_cell_renderer_text_new ();
5156 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5157 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5165 * gtk_combo_box_append_text:
5166 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5169 * Appends @string to the list of strings stored in @combo_box. Note that
5170 * you can only use this function with combo boxes constructed with
5171 * gtk_combo_box_new_text().
5176 gtk_combo_box_append_text (GtkComboBox *combo_box,
5180 GtkListStore *store;
5182 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5183 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5184 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5186 g_return_if_fail (text != NULL);
5188 store = GTK_LIST_STORE (combo_box->priv->model);
5190 gtk_list_store_append (store, &iter);
5191 gtk_list_store_set (store, &iter, 0, text, -1);
5195 * gtk_combo_box_insert_text:
5196 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5197 * @position: An index to insert @text
5200 * Inserts @string at @position in the list of strings stored in @combo_box.
5201 * Note that you can only use this function with combo boxes constructed
5202 * with gtk_combo_box_new_text().
5207 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5212 GtkListStore *store;
5214 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5215 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5216 g_return_if_fail (position >= 0);
5217 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5219 g_return_if_fail (text != NULL);
5221 store = GTK_LIST_STORE (combo_box->priv->model);
5223 gtk_list_store_insert (store, &iter, position);
5224 gtk_list_store_set (store, &iter, 0, text, -1);
5228 * gtk_combo_box_prepend_text:
5229 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5232 * Prepends @string to the list of strings stored in @combo_box. Note that
5233 * you can only use this function with combo boxes constructed with
5234 * gtk_combo_box_new_text().
5239 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5243 GtkListStore *store;
5245 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5246 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5247 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5249 g_return_if_fail (text != NULL);
5251 store = GTK_LIST_STORE (combo_box->priv->model);
5253 gtk_list_store_prepend (store, &iter);
5254 gtk_list_store_set (store, &iter, 0, text, -1);
5258 * gtk_combo_box_remove_text:
5259 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5260 * @position: Index of the item to remove
5262 * Removes the string at @position from @combo_box. Note that you can only use
5263 * this function with combo boxes constructed with gtk_combo_box_new_text().
5268 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5272 GtkListStore *store;
5274 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5275 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5276 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5278 g_return_if_fail (position >= 0);
5280 store = GTK_LIST_STORE (combo_box->priv->model);
5282 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5284 gtk_list_store_remove (store, &iter);
5288 * gtk_combo_box_get_active_text:
5289 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5291 * Returns the currently active string in @combo_box or %NULL if none
5292 * is selected. Note that you can only use this function with combo
5293 * boxes constructed with gtk_combo_box_new_text() and with
5294 * #GtkComboBoxEntry<!-- -->s.
5296 * Returns: a newly allocated string containing the currently active text.
5297 * Must be freed with g_free().
5302 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5304 GtkComboBoxClass *class;
5306 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5308 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5310 if (class->get_active_text)
5311 return class->get_active_text (combo_box);
5317 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5322 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5323 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5324 == G_TYPE_STRING, NULL);
5326 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5327 gtk_tree_model_get (combo_box->priv->model, &iter,
5334 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5335 GtkScrollType scroll)
5338 GtkTreeIter new_iter;
5339 gboolean active_iter;
5342 if (!combo_box->priv->model)
5344 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5348 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5352 case GTK_SCROLL_STEP_BACKWARD:
5353 case GTK_SCROLL_STEP_UP:
5354 case GTK_SCROLL_STEP_LEFT:
5357 found = tree_prev (combo_box, combo_box->priv->model,
5358 &iter, &new_iter, FALSE);
5361 /* else fall through */
5363 case GTK_SCROLL_PAGE_FORWARD:
5364 case GTK_SCROLL_PAGE_DOWN:
5365 case GTK_SCROLL_PAGE_RIGHT:
5366 case GTK_SCROLL_END:
5367 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5370 case GTK_SCROLL_STEP_FORWARD:
5371 case GTK_SCROLL_STEP_DOWN:
5372 case GTK_SCROLL_STEP_RIGHT:
5375 found = tree_next (combo_box, combo_box->priv->model,
5376 &iter, &new_iter, FALSE);
5379 /* else fall through */
5381 case GTK_SCROLL_PAGE_BACKWARD:
5382 case GTK_SCROLL_PAGE_UP:
5383 case GTK_SCROLL_PAGE_LEFT:
5384 case GTK_SCROLL_START:
5385 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5392 if (found && active_iter)
5394 GtkTreePath *old_path;
5395 GtkTreePath *new_path;
5397 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5398 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5400 if (gtk_tree_path_compare (old_path, new_path) == 0)
5403 gtk_tree_path_free (old_path);
5404 gtk_tree_path_free (new_path);
5409 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5413 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5418 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5419 gboolean group_cycling)
5421 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5423 gtk_widget_grab_focus (combo_box->priv->button);
5429 gtk_combo_box_grab_focus (GtkWidget *widget)
5431 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5433 gtk_widget_grab_focus (combo_box->priv->button);
5437 gtk_combo_box_destroy (GtkObject *object)
5439 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5441 if (combo_box->priv->popup_idle_id > 0)
5443 g_source_remove (combo_box->priv->popup_idle_id);
5444 combo_box->priv->popup_idle_id = 0;
5447 gtk_combo_box_popdown (combo_box);
5449 if (combo_box->priv->row_separator_destroy)
5450 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5452 combo_box->priv->row_separator_func = NULL;
5453 combo_box->priv->row_separator_data = NULL;
5454 combo_box->priv->row_separator_destroy = NULL;
5456 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5457 combo_box->priv->cell_view = NULL;
5461 gtk_combo_box_dispose(GObject* object)
5463 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5465 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5467 gtk_combo_box_menu_destroy (combo_box);
5468 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5469 combo_box->priv->popup_widget = NULL;
5472 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5476 gtk_combo_box_finalize (GObject *object)
5478 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5481 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5482 gtk_combo_box_list_destroy (combo_box);
5484 if (combo_box->priv->popup_window)
5485 gtk_widget_destroy (combo_box->priv->popup_window);
5487 gtk_combo_box_unset_model (combo_box);
5489 for (i = combo_box->priv->cells; i; i = i->next)
5491 ComboCellInfo *info = (ComboCellInfo *)i->data;
5492 GSList *list = info->attributes;
5495 info->destroy (info->func_data);
5497 while (list && list->next)
5499 g_free (list->data);
5500 list = list->next->next;
5502 g_slist_free (info->attributes);
5504 g_object_unref (info->cell);
5505 g_slice_free (ComboCellInfo, info);
5507 g_slist_free (combo_box->priv->cells);
5509 g_free (combo_box->priv->tearoff_title);
5511 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5515 gtk_cell_editable_key_press (GtkWidget *widget,
5519 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5521 if (event->keyval == GDK_Escape)
5523 g_object_set (combo_box,
5524 "editing-canceled", TRUE,
5526 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5527 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5531 else if (event->keyval == GDK_Return ||
5532 event->keyval == GDK_ISO_Enter ||
5533 event->keyval == GDK_KP_Enter)
5535 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5536 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5545 popdown_idle (gpointer data)
5547 GtkComboBox *combo_box;
5549 combo_box = GTK_COMBO_BOX (data);
5551 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5552 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5554 g_object_unref (combo_box);
5560 popdown_handler (GtkWidget *widget,
5563 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5567 popup_idle (gpointer data)
5569 GtkComboBox *combo_box;
5571 combo_box = GTK_COMBO_BOX (data);
5573 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5574 combo_box->priv->cell_view)
5575 g_signal_connect_object (combo_box->priv->popup_widget,
5576 "unmap", G_CALLBACK (popdown_handler),
5579 /* we unset this if a menu item is activated */
5580 g_object_set (combo_box,
5581 "editing-canceled", TRUE,
5583 gtk_combo_box_popup (combo_box);
5585 combo_box->priv->popup_idle_id = 0;
5586 combo_box->priv->activate_button = 0;
5587 combo_box->priv->activate_time = 0;
5593 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5596 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5598 combo_box->priv->is_cell_renderer = TRUE;
5600 if (combo_box->priv->cell_view)
5602 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5603 G_CALLBACK (gtk_cell_editable_key_press),
5606 gtk_widget_grab_focus (combo_box->priv->button);
5610 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5611 G_CALLBACK (gtk_cell_editable_key_press),
5614 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5615 GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5618 /* we do the immediate popup only for the optionmenu-like
5621 if (combo_box->priv->is_cell_renderer &&
5622 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5624 if (event && event->type == GDK_BUTTON_PRESS)
5626 GdkEventButton *event_button = (GdkEventButton *)event;
5628 combo_box->priv->activate_button = event_button->button;
5629 combo_box->priv->activate_time = event_button->time;
5632 combo_box->priv->popup_idle_id =
5633 gdk_threads_add_idle (popup_idle, combo_box);
5639 * gtk_combo_box_get_add_tearoffs:
5640 * @combo_box: a #GtkComboBox
5642 * Gets the current value of the :add-tearoffs property.
5644 * Return value: the current value of the :add-tearoffs property.
5647 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5649 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5651 return combo_box->priv->add_tearoffs;
5655 * gtk_combo_box_set_add_tearoffs:
5656 * @combo_box: a #GtkComboBox
5657 * @add_tearoffs: %TRUE to add tearoff menu items
5659 * Sets whether the popup menu should have a tearoff
5665 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5666 gboolean add_tearoffs)
5668 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5670 add_tearoffs = add_tearoffs != FALSE;
5672 if (combo_box->priv->add_tearoffs != add_tearoffs)
5674 combo_box->priv->add_tearoffs = add_tearoffs;
5675 gtk_combo_box_check_appearance (combo_box);
5676 gtk_combo_box_relayout (combo_box);
5677 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5682 * gtk_combo_box_get_title:
5683 * @combo_box: a #GtkComboBox
5685 * Gets the current title of the menu in tearoff mode. See
5686 * gtk_combo_box_set_add_tearoffs().
5688 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5689 * string which must not be freed.
5693 G_CONST_RETURN gchar*
5694 gtk_combo_box_get_title (GtkComboBox *combo_box)
5696 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5698 return combo_box->priv->tearoff_title;
5702 gtk_combo_box_update_title (GtkComboBox *combo_box)
5704 gtk_combo_box_check_appearance (combo_box);
5706 if (combo_box->priv->popup_widget &&
5707 GTK_IS_MENU (combo_box->priv->popup_widget))
5708 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5709 combo_box->priv->tearoff_title);
5713 * gtk_combo_box_set_title:
5714 * @combo_box: a #GtkComboBox
5715 * @title: a title for the menu in tearoff mode
5717 * Sets the menu's title in tearoff mode.
5722 gtk_combo_box_set_title (GtkComboBox *combo_box,
5725 GtkComboBoxPrivate *priv;
5727 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5729 priv = combo_box->priv;
5731 if (strcmp (title ? title : "",
5732 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5734 g_free (priv->tearoff_title);
5735 priv->tearoff_title = g_strdup (title);
5737 gtk_combo_box_update_title (combo_box);
5739 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5744 * gtk_combo_box_get_popup_accessible:
5745 * @combo_box: a #GtkComboBox
5747 * Gets the accessible object corresponding to the combo box's popup.
5749 * This function is mostly intended for use by accessibility technologies;
5750 * applications should have little use for it.
5752 * Returns: the accessible object corresponding to the combo box's popup.
5757 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5761 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5763 if (combo_box->priv->popup_widget)
5765 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5773 * gtk_combo_box_get_row_separator_func:
5774 * @combo_box: a #GtkComboBox
5776 * Returns the current row separator function.
5778 * Return value: the current row separator function.
5782 GtkTreeViewRowSeparatorFunc
5783 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5785 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5787 return combo_box->priv->row_separator_func;
5791 * gtk_combo_box_set_row_separator_func:
5792 * @combo_box: a #GtkComboBox
5793 * @func: a #GtkTreeViewRowSeparatorFunc
5794 * @data: (allow-none): user data to pass to @func, or %NULL
5795 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5797 * Sets the row separator function, which is used to determine
5798 * whether a row should be drawn as a separator. If the row separator
5799 * function is %NULL, no separators are drawn. This is the default value.
5804 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5805 GtkTreeViewRowSeparatorFunc func,
5807 GDestroyNotify destroy)
5809 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5811 if (combo_box->priv->row_separator_destroy)
5812 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5814 combo_box->priv->row_separator_func = func;
5815 combo_box->priv->row_separator_data = data;
5816 combo_box->priv->row_separator_destroy = destroy;
5818 if (combo_box->priv->tree_view)
5819 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5822 gtk_combo_box_relayout (combo_box);
5824 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5828 * gtk_combo_box_set_button_sensitivity:
5829 * @combo_box: a #GtkComboBox
5830 * @sensitivity: specify the sensitivity of the dropdown button
5832 * Sets whether the dropdown button of the combo box should be
5833 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5834 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5839 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5840 GtkSensitivityType sensitivity)
5842 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5844 if (combo_box->priv->button_sensitivity != sensitivity)
5846 combo_box->priv->button_sensitivity = sensitivity;
5847 gtk_combo_box_update_sensitivity (combo_box);
5849 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5854 * gtk_combo_box_get_button_sensitivity:
5855 * @combo_box: a #GtkComboBox
5857 * Returns whether the combo box sets the dropdown button
5858 * sensitive or not when there are no items in the model.
5860 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5861 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5862 * if the button is always insensitive or
5863 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5864 * the model has one item to be selected.
5869 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5871 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5873 return combo_box->priv->button_sensitivity;
5878 * gtk_combo_box_set_focus_on_click:
5879 * @combo: a #GtkComboBox
5880 * @focus_on_click: whether the combo box grabs focus when clicked
5883 * Sets whether the combo box will grab focus when it is clicked with
5884 * the mouse. Making mouse clicks not grab focus is useful in places
5885 * like toolbars where you don't want the keyboard focus removed from
5886 * the main area of the application.
5891 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5892 gboolean focus_on_click)
5894 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5896 focus_on_click = focus_on_click != FALSE;
5898 if (combo_box->priv->focus_on_click != focus_on_click)
5900 combo_box->priv->focus_on_click = focus_on_click;
5902 if (combo_box->priv->button)
5903 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5906 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5911 * gtk_combo_box_get_focus_on_click:
5912 * @combo: a #GtkComboBox
5914 * Returns whether the combo box grabs focus when it is clicked
5915 * with the mouse. See gtk_combo_box_set_focus_on_click().
5917 * Return value: %TRUE if the combo box grabs focus when it is
5918 * clicked with the mouse.
5923 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5925 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5927 return combo_box->priv->focus_on_click;
5932 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5933 GtkBuilder *builder,
5935 const gchar *tagname,
5936 GMarkupParser *parser,
5939 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5940 tagname, parser, data))
5943 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5944 tagname, parser, data);
5948 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5949 GtkBuilder *builder,
5951 const gchar *tagname,
5954 if (strcmp (tagname, "attributes") == 0)
5955 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5958 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5962 #define __GTK_COMBO_BOX_C__
5963 #include "gtkaliasdef.c"