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));
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1024 gtk_combo_box_get_property (GObject *object,
1029 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1030 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
1035 g_value_set_object (value, combo_box->priv->model);
1038 case PROP_WRAP_WIDTH:
1039 g_value_set_int (value, combo_box->priv->wrap_width);
1042 case PROP_ROW_SPAN_COLUMN:
1043 g_value_set_int (value, combo_box->priv->row_column);
1046 case PROP_COLUMN_SPAN_COLUMN:
1047 g_value_set_int (value, combo_box->priv->col_column);
1051 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1054 case PROP_ADD_TEAROFFS:
1055 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1058 case PROP_HAS_FRAME:
1059 g_value_set_boolean (value, combo_box->priv->has_frame);
1062 case PROP_FOCUS_ON_CLICK:
1063 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1066 case PROP_TEAROFF_TITLE:
1067 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1070 case PROP_POPUP_SHOWN:
1071 g_value_set_boolean (value, combo_box->priv->popup_shown);
1074 case PROP_BUTTON_SENSITIVITY:
1075 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1078 case PROP_EDITING_CANCELED:
1079 g_value_set_boolean (value, priv->editing_canceled);
1083 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1089 gtk_combo_box_state_changed (GtkWidget *widget,
1090 GtkStateType previous)
1092 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1093 GtkComboBoxPrivate *priv = combo_box->priv;
1095 if (GTK_WIDGET_REALIZED (widget))
1097 if (priv->tree_view && priv->cell_view)
1098 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1099 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1102 gtk_widget_queue_draw (widget);
1106 gtk_combo_box_button_state_changed (GtkWidget *widget,
1107 GtkStateType previous,
1110 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1111 GtkComboBoxPrivate *priv = combo_box->priv;
1113 if (GTK_WIDGET_REALIZED (widget))
1115 if (!priv->tree_view && priv->cell_view)
1117 if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
1118 (GTK_WIDGET_STATE (priv->cell_view) == GTK_STATE_INSENSITIVE))
1119 gtk_widget_set_sensitive (priv->cell_view, GTK_WIDGET_SENSITIVE (widget));
1121 gtk_widget_set_state (priv->cell_view,
1122 GTK_WIDGET_STATE (widget));
1126 gtk_widget_queue_draw (widget);
1130 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1132 GtkComboBoxPrivate *priv = combo_box->priv;
1133 gboolean appears_as_list;
1135 /* if wrap_width > 0, then we are in grid-mode and forced to use
1138 if (priv->wrap_width)
1139 appears_as_list = FALSE;
1141 gtk_widget_style_get (GTK_WIDGET (combo_box),
1142 "appears-as-list", &appears_as_list,
1145 if (appears_as_list)
1147 /* Destroy all the menu mode widgets, if they exist. */
1148 if (GTK_IS_MENU (priv->popup_widget))
1149 gtk_combo_box_menu_destroy (combo_box);
1151 /* Create the list mode widgets, if they don't already exist. */
1152 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1153 gtk_combo_box_list_setup (combo_box);
1157 /* Destroy all the list mode widgets, if they exist. */
1158 if (GTK_IS_TREE_VIEW (priv->tree_view))
1159 gtk_combo_box_list_destroy (combo_box);
1161 /* Create the menu mode widgets, if they don't already exist. */
1162 if (!GTK_IS_MENU (priv->popup_widget))
1163 gtk_combo_box_menu_setup (combo_box, TRUE);
1166 gtk_widget_style_get (GTK_WIDGET (combo_box),
1167 "shadow-type", &priv->shadow_type,
1172 gtk_combo_box_style_set (GtkWidget *widget,
1175 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1176 GtkComboBoxPrivate *priv = combo_box->priv;
1178 gtk_combo_box_check_appearance (combo_box);
1180 if (priv->tree_view && priv->cell_view)
1181 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1182 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1184 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1185 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1186 GTK_SHADOW_NONE == priv->shadow_type ?
1187 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1191 gtk_combo_box_button_toggled (GtkWidget *widget,
1194 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1196 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1198 if (!combo_box->priv->popup_in_progress)
1199 gtk_combo_box_popup (combo_box);
1202 gtk_combo_box_popdown (combo_box);
1206 gtk_combo_box_add (GtkContainer *container,
1209 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1210 GtkComboBoxPrivate *priv = combo_box->priv;
1212 if (priv->cell_view && priv->cell_view->parent)
1214 gtk_widget_unparent (priv->cell_view);
1215 GTK_BIN (container)->child = NULL;
1216 gtk_widget_queue_resize (GTK_WIDGET (container));
1219 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1220 GTK_BIN (container)->child = widget;
1222 if (priv->cell_view &&
1223 widget != priv->cell_view)
1225 /* since the cell_view was unparented, it's gone now */
1226 priv->cell_view = NULL;
1228 if (!priv->tree_view && priv->separator)
1230 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1232 priv->separator = NULL;
1234 gtk_widget_queue_resize (GTK_WIDGET (container));
1236 else if (priv->cell_view_frame)
1238 gtk_widget_unparent (priv->cell_view_frame);
1239 priv->cell_view_frame = NULL;
1246 gtk_combo_box_remove (GtkContainer *container,
1249 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1250 GtkComboBoxPrivate *priv = combo_box->priv;
1252 gboolean appears_as_list;
1254 if (widget == priv->cell_view)
1255 priv->cell_view = NULL;
1257 gtk_widget_unparent (widget);
1258 GTK_BIN (container)->child = NULL;
1260 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1263 gtk_widget_queue_resize (GTK_WIDGET (container));
1265 if (!priv->tree_view)
1266 appears_as_list = FALSE;
1268 appears_as_list = TRUE;
1270 if (appears_as_list)
1271 gtk_combo_box_list_destroy (combo_box);
1272 else if (GTK_IS_MENU (priv->popup_widget))
1274 gtk_combo_box_menu_destroy (combo_box);
1275 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1276 priv->popup_widget = NULL;
1279 if (!priv->cell_view)
1281 priv->cell_view = gtk_cell_view_new ();
1282 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1283 GTK_BIN (container)->child = priv->cell_view;
1285 gtk_widget_show (priv->cell_view);
1286 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1288 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1292 if (appears_as_list)
1293 gtk_combo_box_list_setup (combo_box);
1295 gtk_combo_box_menu_setup (combo_box, TRUE);
1297 if (gtk_tree_row_reference_valid (priv->active_row))
1299 path = gtk_tree_row_reference_get_path (priv->active_row);
1300 gtk_combo_box_set_active_internal (combo_box, path);
1301 gtk_tree_path_free (path);
1304 gtk_combo_box_set_active_internal (combo_box, NULL);
1307 static ComboCellInfo *
1308 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1309 GtkCellRenderer *cell)
1313 for (i = combo_box->priv->cells; i; i = i->next)
1315 ComboCellInfo *info = (ComboCellInfo *)i->data;
1317 if (info && info->cell == cell)
1325 gtk_combo_box_menu_show (GtkWidget *menu,
1328 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1329 GtkComboBoxPrivate *priv = combo_box->priv;
1331 gtk_combo_box_child_show (menu, user_data);
1333 priv->popup_in_progress = TRUE;
1334 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1336 priv->popup_in_progress = FALSE;
1340 gtk_combo_box_menu_hide (GtkWidget *menu,
1343 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1345 gtk_combo_box_child_hide (menu,user_data);
1347 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1352 gtk_combo_box_detacher (GtkWidget *widget,
1355 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1356 GtkComboBoxPrivate *priv = combo_box->priv;
1358 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1360 g_signal_handlers_disconnect_by_func (menu->toplevel,
1361 gtk_combo_box_menu_show,
1363 g_signal_handlers_disconnect_by_func (menu->toplevel,
1364 gtk_combo_box_menu_hide,
1367 priv->popup_widget = NULL;
1371 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1374 GtkComboBoxPrivate *priv = combo_box->priv;
1376 if (GTK_IS_MENU (priv->popup_widget))
1378 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1379 priv->popup_widget = NULL;
1381 else if (priv->popup_widget)
1383 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1384 priv->popup_widget);
1385 g_object_unref (priv->popup_widget);
1386 priv->popup_widget = NULL;
1389 if (GTK_IS_MENU (popup))
1391 if (priv->popup_window)
1393 gtk_widget_destroy (priv->popup_window);
1394 priv->popup_window = NULL;
1397 priv->popup_widget = popup;
1400 * Note that we connect to show/hide on the toplevel, not the
1401 * menu itself, since the menu is not shown/hidden when it is
1402 * popped up while torn-off.
1404 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1405 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1406 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1407 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1409 gtk_menu_attach_to_widget (GTK_MENU (popup),
1410 GTK_WIDGET (combo_box),
1411 gtk_combo_box_detacher);
1415 if (!priv->popup_window)
1417 GtkWidget *toplevel;
1419 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1420 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1422 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1423 GDK_WINDOW_TYPE_HINT_COMBO);
1425 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1426 G_CALLBACK (gtk_combo_box_child_show),
1428 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1429 G_CALLBACK (gtk_combo_box_child_hide),
1432 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1433 if (GTK_IS_WINDOW (toplevel))
1435 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1436 GTK_WINDOW (priv->popup_window));
1437 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1438 GTK_WINDOW (toplevel));
1441 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1442 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1443 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1445 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1447 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1450 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1453 gtk_widget_show (priv->scrolled_window);
1455 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1456 priv->scrolled_window);
1459 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1462 gtk_widget_show (popup);
1463 g_object_ref (popup);
1464 priv->popup_widget = popup;
1469 gtk_combo_box_menu_position_below (GtkMenu *menu,
1475 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1481 GdkRectangle monitor;
1483 /* FIXME: is using the size request here broken? */
1484 child = GTK_BIN (combo_box)->child;
1488 if (GTK_WIDGET_NO_WINDOW (child))
1490 sx += child->allocation.x;
1491 sy += child->allocation.y;
1494 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1496 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1497 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1499 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1501 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1504 *x = sx + child->allocation.width - req.width;
1507 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1508 monitor_num = gdk_screen_get_monitor_at_window (screen,
1509 GTK_WIDGET (combo_box)->window);
1510 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1514 else if (*x + req.width > monitor.x + monitor.width)
1515 *x = monitor.x + monitor.width - req.width;
1517 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1518 *y += child->allocation.height;
1519 else if (*y - monitor.y >= req.height)
1521 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1522 *y += child->allocation.height;
1530 gtk_combo_box_menu_position_over (GtkMenu *menu,
1536 GtkComboBox *combo_box;
1540 GtkRequisition requisition;
1547 combo_box = GTK_COMBO_BOX (user_data);
1548 widget = GTK_WIDGET (combo_box);
1550 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1551 menu_width = requisition.width;
1553 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1555 menu_xpos = widget->allocation.x;
1556 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1560 gtk_widget_get_child_requisition (active, &requisition);
1561 menu_ypos -= requisition.height / 2;
1564 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1567 child = children->data;
1569 if (active == child)
1572 if (GTK_WIDGET_VISIBLE (child))
1574 gtk_widget_get_child_requisition (child, &requisition);
1575 menu_ypos -= requisition.height;
1578 children = children->next;
1581 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1582 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1584 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1585 &menu_xpos, &menu_ypos);
1587 /* Clamp the position on screen */
1588 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1592 else if ((menu_xpos + menu_width) > screen_width)
1593 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1602 gtk_combo_box_menu_position (GtkMenu *menu,
1608 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1609 GtkComboBoxPrivate *priv = combo_box->priv;
1610 GtkWidget *menu_item;
1613 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1614 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1617 /* FIXME handle nested menus better */
1618 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1620 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1623 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1626 if (!GTK_WIDGET_VISIBLE (GTK_MENU (priv->popup_widget)->toplevel))
1627 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1628 GDK_WINDOW_TYPE_HINT_COMBO);
1632 gtk_combo_box_list_position (GtkComboBox *combo_box,
1638 GtkComboBoxPrivate *priv = combo_box->priv;
1641 GdkRectangle monitor;
1642 GtkRequisition popup_req;
1643 GtkPolicyType hpolicy, vpolicy;
1645 /* under windows, the drop down list is as wide as the combo box itself.
1647 GtkWidget *sample = GTK_WIDGET (combo_box);
1651 if (GTK_WIDGET_NO_WINDOW (sample))
1653 *x += sample->allocation.x;
1654 *y += sample->allocation.y;
1657 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1659 *width = sample->allocation.width;
1661 hpolicy = vpolicy = GTK_POLICY_NEVER;
1662 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1664 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1666 if (popup_req.width > *width)
1668 hpolicy = GTK_POLICY_ALWAYS;
1669 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1671 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1674 *height = popup_req.height;
1676 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1677 monitor_num = gdk_screen_get_monitor_at_window (screen,
1678 GTK_WIDGET (combo_box)->window);
1679 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1683 else if (*x + *width > monitor.x + monitor.width)
1684 *x = monitor.x + monitor.width - *width;
1686 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1687 *y += sample->allocation.height;
1688 else if (*y - *height >= monitor.y)
1690 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1692 *y += sample->allocation.height;
1693 *height = monitor.y + monitor.height - *y;
1697 *height = *y - monitor.y;
1701 if (popup_req.height > *height)
1703 vpolicy = GTK_POLICY_ALWAYS;
1705 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1711 cell_view_is_sensitive (GtkCellView *cell_view)
1713 GList *cells, *list;
1716 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1719 for (list = cells; list; list = list->next)
1721 g_object_get (list->data, "sensitive", &sensitive, NULL);
1726 g_list_free (cells);
1732 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1735 GtkComboBoxPrivate *priv = combo_box->priv;
1736 GList *cells, *list;
1742 if (priv->row_separator_func)
1744 if (priv->row_separator_func (priv->model, iter,
1745 priv->row_separator_data))
1749 gtk_tree_view_column_cell_set_cell_data (priv->column,
1751 iter, FALSE, FALSE);
1753 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1756 for (list = cells; list; list = list->next)
1758 g_object_get (list->data, "sensitive", &sensitive, NULL);
1763 g_list_free (cells);
1769 update_menu_sensitivity (GtkComboBox *combo_box,
1772 GtkComboBoxPrivate *priv = combo_box->priv;
1773 GList *children, *child;
1774 GtkWidget *item, *submenu, *separator;
1775 GtkWidget *cell_view;
1781 children = gtk_container_get_children (GTK_CONTAINER (menu));
1783 for (child = children; child; child = child->next)
1785 item = GTK_WIDGET (child->data);
1786 cell_view = GTK_BIN (item)->child;
1788 if (!GTK_IS_CELL_VIEW (cell_view))
1791 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1792 if (submenu != NULL)
1794 gtk_widget_set_sensitive (item, TRUE);
1795 update_menu_sensitivity (combo_box, submenu);
1799 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1801 if (menu != priv->popup_widget && child == children)
1803 separator = GTK_WIDGET (child->next->data);
1804 g_object_set (item, "visible", sensitive, NULL);
1805 g_object_set (separator, "visible", sensitive, NULL);
1808 gtk_widget_set_sensitive (item, sensitive);
1812 g_list_free (children);
1816 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1818 guint32 activate_time)
1820 GtkComboBoxPrivate *priv = combo_box->priv;
1823 GtkRequisition requisition;
1826 update_menu_sensitivity (combo_box, priv->popup_widget);
1829 if (gtk_tree_row_reference_valid (priv->active_row))
1831 path = gtk_tree_row_reference_get_path (priv->active_row);
1832 active_item = gtk_tree_path_get_indices (path)[0];
1833 gtk_tree_path_free (path);
1835 if (priv->add_tearoffs)
1839 /* FIXME handle nested menus better */
1840 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1842 if (priv->wrap_width == 0)
1844 width = GTK_WIDGET (combo_box)->allocation.width;
1845 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1846 gtk_widget_size_request (priv->popup_widget, &requisition);
1848 gtk_widget_set_size_request (priv->popup_widget,
1849 MAX (width, requisition.width), -1);
1852 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1854 gtk_combo_box_menu_position, combo_box,
1855 button, activate_time);
1859 popup_grab_on_window (GdkWindow *window,
1860 guint32 activate_time,
1861 gboolean grab_keyboard)
1863 if ((gdk_pointer_grab (window, TRUE,
1864 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1865 GDK_POINTER_MOTION_MASK,
1866 NULL, NULL, activate_time) == 0))
1868 if (!grab_keyboard ||
1869 gdk_keyboard_grab (window, TRUE,
1870 activate_time) == 0)
1874 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1884 * gtk_combo_box_popup:
1885 * @combo_box: a #GtkComboBox
1887 * Pops up the menu or dropdown list of @combo_box.
1889 * This function is mostly intended for use by accessibility technologies;
1890 * applications should have little use for it.
1895 gtk_combo_box_popup (GtkComboBox *combo_box)
1897 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1899 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1903 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1905 GtkComboBoxPrivate *priv = combo_box->priv;
1906 gint x, y, width, height;
1907 GtkTreePath *path = NULL, *ppath;
1908 GtkWidget *toplevel;
1910 if (!GTK_WIDGET_REALIZED (combo_box))
1913 if (GTK_WIDGET_MAPPED (priv->popup_widget))
1916 if (GTK_IS_MENU (priv->popup_widget))
1918 gtk_combo_box_menu_popup (combo_box,
1919 priv->activate_button,
1920 priv->activate_time);
1924 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1925 if (GTK_IS_WINDOW (toplevel))
1926 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1927 GTK_WINDOW (priv->popup_window));
1929 gtk_widget_show_all (priv->scrolled_window);
1930 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1932 gtk_widget_set_size_request (priv->popup_window, width, height);
1933 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1935 if (gtk_tree_row_reference_valid (priv->active_row))
1937 path = gtk_tree_row_reference_get_path (priv->active_row);
1938 ppath = gtk_tree_path_copy (path);
1939 if (gtk_tree_path_up (ppath))
1940 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1942 gtk_tree_path_free (ppath);
1944 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1948 gtk_widget_show (priv->popup_window);
1952 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1954 gtk_tree_path_free (path);
1957 gtk_widget_grab_focus (priv->popup_window);
1958 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1961 if (!GTK_WIDGET_HAS_FOCUS (priv->tree_view))
1962 gtk_widget_grab_focus (priv->tree_view);
1964 if (!popup_grab_on_window (priv->popup_window->window,
1965 GDK_CURRENT_TIME, TRUE))
1967 gtk_widget_hide (priv->popup_window);
1971 gtk_grab_add (priv->popup_window);
1975 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1977 if (combo_box->priv->popup_shown)
1979 gtk_combo_box_popdown (combo_box);
1987 * gtk_combo_box_popdown:
1988 * @combo_box: a #GtkComboBox
1990 * Hides the menu or dropdown list of @combo_box.
1992 * This function is mostly intended for use by accessibility technologies;
1993 * applications should have little use for it.
1998 gtk_combo_box_popdown (GtkComboBox *combo_box)
2000 GtkComboBoxPrivate *priv = combo_box->priv;
2002 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2004 if (GTK_IS_MENU (priv->popup_widget))
2006 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2010 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
2013 gtk_grab_remove (priv->popup_window);
2014 gtk_widget_hide_all (priv->popup_window);
2015 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2020 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2023 GtkComboBoxPrivate *priv = combo_box->priv;
2027 if (priv->cell_view)
2028 gtk_widget_style_get (priv->cell_view,
2029 "focus-line-width", &padding,
2034 /* add some pixels for good measure */
2035 padding += BONUS_PADDING;
2037 if (priv->cell_view)
2038 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2043 return req.width + padding;
2047 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2049 GtkComboBoxPrivate *priv = combo_box->priv;
2054 !gtk_tree_model_get_iter_first (priv->model, &iter))
2060 path = gtk_tree_path_new_from_indices (0, -1);
2066 if (priv->cell_view)
2067 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2075 priv->width = MAX (priv->width, req.width);
2076 priv->height = MAX (priv->height, req.height);
2078 gtk_tree_path_next (path);
2080 while (gtk_tree_model_iter_next (priv->model, &iter));
2082 gtk_tree_path_free (path);
2086 gtk_combo_box_size_request (GtkWidget *widget,
2087 GtkRequisition *requisition)
2090 gint focus_width, focus_pad;
2093 GtkRequisition bin_req;
2094 PangoContext *context;
2095 PangoFontMetrics *metrics;
2096 PangoFontDescription *font_desc;
2098 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2099 GtkComboBoxPrivate *priv = combo_box->priv;
2102 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2103 gtk_combo_box_remeasure (combo_box);
2104 bin_req.width = MAX (bin_req.width, priv->width);
2105 bin_req.height = MAX (bin_req.height, priv->height);
2107 gtk_widget_style_get (GTK_WIDGET (widget),
2108 "focus-line-width", &focus_width,
2109 "focus-padding", &focus_pad,
2110 "arrow-size", &arrow_size,
2113 font_desc = GTK_BIN (widget)->child->style->font_desc;
2114 context = gtk_widget_get_pango_context (widget);
2115 metrics = pango_context_get_metrics (context, font_desc,
2116 pango_context_get_language (context));
2117 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2118 pango_font_metrics_get_descent (metrics));
2119 pango_font_metrics_unref (metrics);
2121 arrow_size = MAX (arrow_size, font_size);
2123 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2125 if (!priv->tree_view)
2129 if (priv->cell_view)
2131 GtkRequisition button_req, sep_req, arrow_req;
2132 gint border_width, xthickness, ythickness;
2134 gtk_widget_size_request (priv->button, &button_req);
2135 border_width = GTK_CONTAINER (combo_box)->border_width;
2136 xthickness = priv->button->style->xthickness;
2137 ythickness = priv->button->style->ythickness;
2139 bin_req.width = MAX (bin_req.width, priv->width);
2140 bin_req.height = MAX (bin_req.height, priv->height);
2142 gtk_widget_size_request (priv->separator, &sep_req);
2143 gtk_widget_size_request (priv->arrow, &arrow_req);
2145 height = MAX (sep_req.height, arrow_req.height);
2146 height = MAX (height, bin_req.height);
2148 width = bin_req.width + sep_req.width + arrow_req.width;
2150 height += 2*(border_width + ythickness + focus_width + focus_pad);
2151 width += 2*(border_width + xthickness + focus_width + focus_pad);
2153 requisition->width = width;
2154 requisition->height = height;
2158 GtkRequisition but_req;
2160 gtk_widget_size_request (priv->button, &but_req);
2162 requisition->width = bin_req.width + but_req.width;
2163 requisition->height = MAX (bin_req.height, but_req.height);
2169 GtkRequisition button_req, frame_req;
2171 /* sample + frame */
2172 *requisition = bin_req;
2174 requisition->width += 2 * focus_width;
2176 if (priv->cell_view_frame)
2178 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2179 if (priv->has_frame)
2181 requisition->width += 2 *
2182 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2183 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2184 requisition->height += 2 *
2185 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2186 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2191 gtk_widget_size_request (priv->button, &button_req);
2193 requisition->height = MAX (requisition->height, button_req.height);
2194 requisition->width += button_req.width;
2197 if (GTK_SHADOW_NONE != priv->shadow_type)
2199 requisition->height += 2 * widget->style->ythickness;
2200 requisition->width += 2 * widget->style->xthickness;
2204 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2205 gtk_widget_size_request (combo_box->priv->button, &req); \
2208 child.x = allocation->x + shadow_width; \
2210 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2212 child.y = allocation->y + shadow_height; \
2213 child.width = req.width; \
2214 child.height = allocation->height - 2 * shadow_height; \
2215 child.width = MAX (1, child.width); \
2216 child.height = MAX (1, child.height); \
2218 gtk_widget_size_allocate (combo_box->priv->button, &child);
2221 gtk_combo_box_size_allocate (GtkWidget *widget,
2222 GtkAllocation *allocation)
2224 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2225 GtkComboBoxPrivate *priv = combo_box->priv;
2226 gint shadow_width, shadow_height;
2227 gint focus_width, focus_pad;
2228 GtkAllocation child;
2230 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2232 widget->allocation = *allocation;
2234 gtk_widget_style_get (GTK_WIDGET (widget),
2235 "focus-line-width", &focus_width,
2236 "focus-padding", &focus_pad,
2239 if (GTK_SHADOW_NONE != priv->shadow_type)
2241 shadow_width = widget->style->xthickness;
2242 shadow_height = widget->style->ythickness;
2250 if (!priv->tree_view)
2252 if (priv->cell_view)
2254 gint border_width, xthickness, ythickness;
2258 allocation->x += shadow_width;
2259 allocation->y += shadow_height;
2260 allocation->width -= 2 * shadow_width;
2261 allocation->height -= 2 * shadow_height;
2263 gtk_widget_size_allocate (priv->button, allocation);
2265 /* set some things ready */
2266 border_width = GTK_CONTAINER (priv->button)->border_width;
2267 xthickness = priv->button->style->xthickness;
2268 ythickness = priv->button->style->ythickness;
2270 child.x = allocation->x;
2271 child.y = allocation->y;
2272 width = allocation->width;
2273 child.height = allocation->height;
2275 if (!priv->is_cell_renderer)
2277 child.x += border_width + xthickness + focus_width + focus_pad;
2278 child.y += border_width + ythickness + focus_width + focus_pad;
2279 width -= 2 * (child.x - allocation->x);
2280 child.height -= 2 * (child.y - allocation->y);
2284 /* handle the children */
2285 gtk_widget_size_request (priv->arrow, &req);
2286 child.width = req.width;
2288 child.x += width - req.width;
2289 child.width = MAX (1, child.width);
2290 child.height = MAX (1, child.height);
2291 gtk_widget_size_allocate (priv->arrow, &child);
2293 child.x += req.width;
2294 gtk_widget_size_request (priv->separator, &req);
2295 child.width = req.width;
2297 child.x -= req.width;
2298 child.width = MAX (1, child.width);
2299 child.height = MAX (1, child.height);
2300 gtk_widget_size_allocate (priv->separator, &child);
2304 child.x += req.width;
2305 child.width = allocation->x + allocation->width
2306 - (border_width + xthickness + focus_width + focus_pad)
2311 child.width = child.x;
2312 child.x = allocation->x
2313 + border_width + xthickness + focus_width + focus_pad;
2314 child.width -= child.x;
2317 if (GTK_WIDGET_VISIBLE (priv->popup_widget))
2320 GtkRequisition requisition;
2322 /* Warning here, without the check in the position func */
2323 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2324 if (priv->wrap_width == 0)
2326 width = GTK_WIDGET (combo_box)->allocation.width;
2327 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2328 gtk_widget_size_request (priv->popup_widget, &requisition);
2329 gtk_widget_set_size_request (priv->popup_widget,
2330 MAX (width, requisition.width), -1);
2334 child.width = MAX (1, child.width);
2335 child.height = MAX (1, child.height);
2336 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2340 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2343 child.x = allocation->x + req.width + shadow_width;
2345 child.x = allocation->x + shadow_width;
2346 child.y = allocation->y + shadow_height;
2347 child.width = allocation->width - req.width - 2 * shadow_width;
2348 child.width = MAX (1, child.width);
2349 child.height = MAX (1, child.height);
2350 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2357 /* Combobox thickness + border-width */
2358 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2359 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2362 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2366 child.x = allocation->x + req.width;
2368 child.x = allocation->x;
2370 child.y = allocation->y;
2371 child.width = allocation->width - req.width;
2372 child.height = allocation->height;
2374 if (priv->cell_view_frame)
2378 child.width = MAX (1, child.width - delta_x * 2);
2379 child.height = MAX (1, child.height - delta_y * 2);
2380 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2383 if (priv->has_frame)
2385 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2386 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2387 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2388 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2392 child.width -= delta_x * 2;
2393 child.height -= delta_y * 2;
2400 child.width -= delta_x * 2;
2401 child.height -= delta_y * 2;
2404 if (GTK_WIDGET_VISIBLE (priv->popup_window))
2406 gint x, y, width, height;
2407 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2408 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2409 gtk_widget_set_size_request (priv->popup_window, width, height);
2413 child.width = MAX (1, child.width);
2414 child.height = MAX (1, child.height);
2416 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2420 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2423 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2425 GtkComboBoxPrivate *priv = combo_box->priv;
2429 g_signal_handler_disconnect (priv->model,
2431 g_signal_handler_disconnect (priv->model,
2433 g_signal_handler_disconnect (priv->model,
2434 priv->reordered_id);
2435 g_signal_handler_disconnect (priv->model,
2440 if (!priv->tree_view)
2442 if (priv->popup_widget)
2443 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2444 (GtkCallback)gtk_widget_destroy, NULL);
2449 g_object_unref (priv->model);
2453 if (priv->active_row)
2455 gtk_tree_row_reference_free (priv->active_row);
2456 priv->active_row = NULL;
2459 if (priv->cell_view)
2460 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2464 gtk_combo_box_forall (GtkContainer *container,
2465 gboolean include_internals,
2466 GtkCallback callback,
2467 gpointer callback_data)
2469 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2470 GtkComboBoxPrivate *priv = combo_box->priv;
2472 if (include_internals)
2475 (* callback) (priv->button, callback_data);
2476 if (priv->cell_view_frame)
2477 (* callback) (priv->cell_view_frame, callback_data);
2480 if (GTK_BIN (container)->child)
2481 (* callback) (GTK_BIN (container)->child, callback_data);
2485 gtk_combo_box_child_show (GtkWidget *widget,
2486 GtkComboBox *combo_box)
2488 GtkComboBoxPrivate *priv = combo_box->priv;
2490 priv->popup_shown = TRUE;
2491 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2495 gtk_combo_box_child_hide (GtkWidget *widget,
2496 GtkComboBox *combo_box)
2498 GtkComboBoxPrivate *priv = combo_box->priv;
2500 priv->popup_shown = FALSE;
2501 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2505 gtk_combo_box_expose_event (GtkWidget *widget,
2506 GdkEventExpose *event)
2508 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2509 GtkComboBoxPrivate *priv = combo_box->priv;
2511 if (GTK_WIDGET_DRAWABLE (widget) &&
2512 GTK_SHADOW_NONE != priv->shadow_type)
2514 gtk_paint_shadow (widget->style, widget->window,
2515 GTK_STATE_NORMAL, priv->shadow_type,
2516 NULL, widget, "combobox",
2517 widget->allocation.x, widget->allocation.y,
2518 widget->allocation.width, widget->allocation.height);
2521 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2522 priv->button, event);
2524 if (priv->tree_view && priv->cell_view_frame)
2526 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2527 priv->cell_view_frame, event);
2530 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2531 GTK_BIN (widget)->child, event);
2546 path_visible (GtkTreeView *view,
2552 /* Note that we rely on the fact that collapsed rows don't have nodes
2554 return _gtk_tree_view_find_node (view, path, &tree, &node);
2558 tree_next_func (GtkTreeModel *model,
2563 SearchData *search_data = (SearchData *)data;
2565 if (search_data->found)
2567 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2570 if (search_data->visible &&
2571 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2574 search_data->set = TRUE;
2575 search_data->iter = *iter;
2580 if (gtk_tree_path_compare (path, search_data->path) == 0)
2581 search_data->found = TRUE;
2587 tree_next (GtkComboBox *combo,
2588 GtkTreeModel *model,
2593 SearchData search_data;
2595 search_data.combo = combo;
2596 search_data.path = gtk_tree_model_get_path (model, iter);
2597 search_data.visible = visible;
2598 search_data.found = FALSE;
2599 search_data.set = FALSE;
2601 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2603 *next = search_data.iter;
2605 gtk_tree_path_free (search_data.path);
2607 return search_data.set;
2611 tree_prev_func (GtkTreeModel *model,
2616 SearchData *search_data = (SearchData *)data;
2618 if (gtk_tree_path_compare (path, search_data->path) == 0)
2620 search_data->found = TRUE;
2624 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2627 if (search_data->visible &&
2628 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2631 search_data->set = TRUE;
2632 search_data->iter = *iter;
2638 tree_prev (GtkComboBox *combo,
2639 GtkTreeModel *model,
2644 SearchData search_data;
2646 search_data.combo = combo;
2647 search_data.path = gtk_tree_model_get_path (model, iter);
2648 search_data.visible = visible;
2649 search_data.found = FALSE;
2650 search_data.set = FALSE;
2652 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2654 *prev = search_data.iter;
2656 gtk_tree_path_free (search_data.path);
2658 return search_data.set;
2662 tree_last_func (GtkTreeModel *model,
2667 SearchData *search_data = (SearchData *)data;
2669 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2672 /* Note that we rely on the fact that collapsed rows don't have nodes
2674 if (search_data->visible &&
2675 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2678 search_data->set = TRUE;
2679 search_data->iter = *iter;
2685 tree_last (GtkComboBox *combo,
2686 GtkTreeModel *model,
2690 SearchData search_data;
2692 search_data.combo = combo;
2693 search_data.visible = visible;
2694 search_data.set = FALSE;
2696 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2698 *last = search_data.iter;
2700 return search_data.set;
2705 tree_first_func (GtkTreeModel *model,
2710 SearchData *search_data = (SearchData *)data;
2712 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2715 if (search_data->visible &&
2716 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2719 search_data->set = TRUE;
2720 search_data->iter = *iter;
2726 tree_first (GtkComboBox *combo,
2727 GtkTreeModel *model,
2731 SearchData search_data;
2733 search_data.combo = combo;
2734 search_data.visible = visible;
2735 search_data.set = FALSE;
2737 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2739 *first = search_data.iter;
2741 return search_data.set;
2745 gtk_combo_box_scroll_event (GtkWidget *widget,
2746 GdkEventScroll *event)
2748 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2751 GtkTreeIter new_iter;
2753 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2756 if (event->direction == GDK_SCROLL_UP)
2757 found = tree_prev (combo_box, combo_box->priv->model,
2758 &iter, &new_iter, FALSE);
2760 found = tree_next (combo_box, combo_box->priv->model,
2761 &iter, &new_iter, FALSE);
2764 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2774 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2775 GtkCellLayout *cell_layout)
2777 GtkComboBoxPrivate *priv = combo_box->priv;
2780 for (k = priv->cells; k; k = k->next)
2783 ComboCellInfo *info = (ComboCellInfo *)k->data;
2785 if (info->pack == GTK_PACK_START)
2786 gtk_cell_layout_pack_start (cell_layout,
2787 info->cell, info->expand);
2788 else if (info->pack == GTK_PACK_END)
2789 gtk_cell_layout_pack_end (cell_layout,
2790 info->cell, info->expand);
2792 gtk_cell_layout_set_cell_data_func (cell_layout,
2794 combo_cell_data_func, info, NULL);
2796 for (j = info->attributes; j; j = j->next->next)
2798 gtk_cell_layout_add_attribute (cell_layout,
2801 GPOINTER_TO_INT (j->next->data));
2807 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2808 gboolean add_children)
2810 GtkComboBoxPrivate *priv = combo_box->priv;
2813 if (priv->cell_view)
2815 priv->button = gtk_toggle_button_new ();
2816 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2817 priv->focus_on_click);
2819 g_signal_connect (priv->button, "toggled",
2820 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2821 gtk_widget_set_parent (priv->button,
2822 GTK_BIN (combo_box)->child->parent);
2824 priv->box = gtk_hbox_new (FALSE, 0);
2825 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2827 priv->separator = gtk_vseparator_new ();
2828 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2830 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2831 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2833 gtk_widget_show_all (priv->button);
2837 priv->button = gtk_toggle_button_new ();
2838 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2839 priv->focus_on_click);
2841 g_signal_connect (priv->button, "toggled",
2842 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2843 gtk_widget_set_parent (priv->button,
2844 GTK_BIN (combo_box)->child->parent);
2846 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2847 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2848 gtk_widget_show_all (priv->button);
2851 g_signal_connect (priv->button, "button-press-event",
2852 G_CALLBACK (gtk_combo_box_menu_button_press),
2854 g_signal_connect (priv->button, "state-changed",
2855 G_CALLBACK (gtk_combo_box_button_state_changed),
2858 /* create our funky menu */
2859 menu = gtk_menu_new ();
2860 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2861 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2863 g_signal_connect (menu, "key-press-event",
2864 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2865 gtk_combo_box_set_popup_widget (combo_box, menu);
2869 gtk_combo_box_menu_fill (combo_box);
2871 /* the column is needed in tree_column_row_is_sensitive() */
2872 priv->column = gtk_tree_view_column_new ();
2873 g_object_ref_sink (priv->column);
2874 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2876 gtk_combo_box_update_title (combo_box);
2877 gtk_combo_box_update_sensitivity (combo_box);
2881 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2883 GtkComboBoxPrivate *priv = combo_box->priv;
2889 menu = priv->popup_widget;
2891 if (priv->add_tearoffs)
2893 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2895 gtk_widget_show (tearoff);
2897 if (priv->wrap_width)
2898 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2900 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2903 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2907 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2908 GtkTreeModel *model,
2911 GtkWidget *cell_view;
2916 cell_view = gtk_cell_view_new ();
2917 item = gtk_menu_item_new ();
2918 gtk_container_add (GTK_CONTAINER (item), cell_view);
2920 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2921 path = gtk_tree_model_get_path (model, iter);
2922 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2923 gtk_tree_path_free (path);
2925 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2926 gtk_widget_size_request (cell_view, &req);
2927 gtk_widget_show (cell_view);
2933 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2935 GtkTreeIter *parent)
2937 GtkComboBoxPrivate *priv = combo_box->priv;
2938 GtkTreeModel *model = priv->model;
2939 GtkWidget *item, *submenu, *subitem, *separator;
2941 gboolean is_separator;
2946 n_children = gtk_tree_model_iter_n_children (model, parent);
2949 for (i = 0; i < n_children; i++)
2951 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2953 if (priv->row_separator_func)
2954 is_separator = priv->row_separator_func (priv->model, &iter,
2955 priv->row_separator_data);
2957 is_separator = FALSE;
2961 item = gtk_separator_menu_item_new ();
2962 path = gtk_tree_model_get_path (model, &iter);
2963 g_object_set_data_full (G_OBJECT (item),
2964 I_("gtk-combo-box-item-path"),
2965 gtk_tree_row_reference_new (model, path),
2966 (GDestroyNotify)gtk_tree_row_reference_free);
2967 gtk_tree_path_free (path);
2971 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2972 if (gtk_tree_model_iter_has_child (model, &iter))
2974 submenu = gtk_menu_new ();
2975 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
2976 gtk_widget_show (submenu);
2977 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2979 /* Ugly - since menus can only activate leafs, we have to
2980 * duplicate the item inside the submenu.
2982 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2983 separator = gtk_separator_menu_item_new ();
2984 gtk_widget_show (subitem);
2985 gtk_widget_show (separator);
2986 g_signal_connect (subitem, "activate",
2987 G_CALLBACK (gtk_combo_box_menu_item_activate),
2989 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2990 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2992 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2994 g_signal_connect (item, "activate",
2995 G_CALLBACK (gtk_combo_box_menu_item_activate),
2999 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3000 if (priv->wrap_width && menu == priv->popup_widget)
3001 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3002 gtk_widget_show (item);
3009 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3011 GtkComboBoxPrivate *priv = combo_box->priv;
3013 g_signal_handlers_disconnect_matched (priv->button,
3014 G_SIGNAL_MATCH_DATA,
3016 gtk_combo_box_menu_button_press, NULL);
3017 g_signal_handlers_disconnect_matched (priv->button,
3018 G_SIGNAL_MATCH_DATA,
3020 gtk_combo_box_button_state_changed, combo_box);
3022 /* unparent will remove our latest ref */
3023 gtk_widget_unparent (priv->button);
3026 priv->button = NULL;
3028 priv->separator = NULL;
3030 g_object_unref (priv->column);
3031 priv->column = NULL;
3033 /* changing the popup window will unref the menu and the children */
3041 menu_occupied (GtkMenu *menu,
3045 guint bottom_attach)
3049 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3053 gtk_container_child_get (GTK_CONTAINER (menu),
3057 "bottom-attach", &b,
3061 /* look if this item intersects with the given coordinates */
3062 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3070 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3075 GtkComboBoxPrivate *priv = combo_box->priv;
3076 gint current_col = 0, current_row = 0;
3077 gint rows = 1, cols = 1;
3078 GtkWidget *menu = priv->popup_widget;
3080 if (!GTK_IS_MENU_SHELL (menu))
3083 if (priv->col_column == -1 &&
3084 priv->row_column == -1 &&
3087 gtk_container_child_get (GTK_CONTAINER (menu),
3089 "right-attach", ¤t_col,
3090 "top-attach", ¤t_row,
3092 if (current_col + cols > priv->wrap_width)
3100 if (priv->col_column != -1)
3101 gtk_tree_model_get (priv->model, iter,
3102 priv->col_column, &cols,
3104 if (priv->row_column != -1)
3105 gtk_tree_model_get (priv->model, iter,
3106 priv->row_column, &rows,
3111 if (current_col + cols > priv->wrap_width)
3117 if (!menu_occupied (GTK_MENU (menu),
3118 current_col, current_col + cols,
3119 current_row, current_row + rows))
3126 /* set attach props */
3127 gtk_menu_attach (GTK_MENU (menu), item,
3128 current_col, current_col + cols,
3129 current_row, current_row + rows);
3133 gtk_combo_box_relayout (GtkComboBox *combo_box)
3138 menu = combo_box->priv->popup_widget;
3140 /* do nothing unless we are in menu style and realized */
3141 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3144 list = gtk_container_get_children (GTK_CONTAINER (menu));
3146 for (j = g_list_last (list); j; j = j->prev)
3147 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3149 gtk_combo_box_menu_fill (combo_box);
3156 gtk_combo_box_menu_button_press (GtkWidget *widget,
3157 GdkEventButton *event,
3160 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3161 GtkComboBoxPrivate *priv = combo_box->priv;
3163 if (GTK_IS_MENU (priv->popup_widget) &&
3164 event->type == GDK_BUTTON_PRESS && event->button == 1)
3166 if (priv->focus_on_click &&
3167 !GTK_WIDGET_HAS_FOCUS (priv->button))
3168 gtk_widget_grab_focus (priv->button);
3170 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3179 gtk_combo_box_menu_item_activate (GtkWidget *item,
3182 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3183 GtkWidget *cell_view;
3187 cell_view = GTK_BIN (item)->child;
3189 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3191 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3193 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3195 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3196 gtk_combo_box_set_active_iter (combo_box, &iter);
3199 gtk_tree_path_free (path);
3201 combo_box->priv->editing_canceled = FALSE;
3205 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3208 gboolean sensitive = TRUE; /* fool code checkers */
3210 if (!combo_box->priv->button)
3213 switch (combo_box->priv->button_sensitivity)
3215 case GTK_SENSITIVITY_ON:
3218 case GTK_SENSITIVITY_OFF:
3221 case GTK_SENSITIVITY_AUTO:
3222 sensitive = combo_box->priv->model &&
3223 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3226 g_assert_not_reached ();
3230 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3232 /* In list-mode, we also need to update sensitivity of the event box */
3233 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3234 && combo_box->priv->cell_view)
3235 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3239 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3244 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3246 if (combo_box->priv->tree_view)
3247 gtk_combo_box_list_popup_resize (combo_box);
3249 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3251 gtk_combo_box_update_sensitivity (combo_box);
3255 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3259 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3260 GtkComboBoxPrivate *priv = combo_box->priv;
3262 if (!gtk_tree_row_reference_valid (priv->active_row))
3264 if (priv->cell_view)
3265 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3266 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3269 if (priv->tree_view)
3270 gtk_combo_box_list_popup_resize (combo_box);
3272 gtk_combo_box_menu_row_deleted (model, path, user_data);
3274 gtk_combo_box_update_sensitivity (combo_box);
3278 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3284 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3286 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3288 if (!combo_box->priv->tree_view)
3289 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3293 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3298 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3299 GtkComboBoxPrivate *priv = combo_box->priv;
3300 GtkTreePath *active_path;
3302 /* FIXME this belongs to GtkCellView */
3303 if (gtk_tree_row_reference_valid (priv->active_row))
3305 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3306 if (gtk_tree_path_compare (path, active_path) == 0 &&
3308 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3309 gtk_tree_path_free (active_path);
3312 if (priv->tree_view)
3313 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3315 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3319 list_popup_resize_idle (gpointer user_data)
3321 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3322 GtkComboBoxPrivate *priv = combo_box->priv;
3323 gint x, y, width, height;
3325 if (priv->tree_view && GTK_WIDGET_MAPPED (priv->popup_window))
3327 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3329 gtk_widget_set_size_request (priv->popup_window, width, height);
3330 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3333 priv->resize_idle_id = 0;
3339 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3341 GtkComboBoxPrivate *priv = combo_box->priv;
3343 if (!priv->resize_idle_id)
3344 priv->resize_idle_id =
3345 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3349 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3354 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3356 gtk_combo_box_list_popup_resize (combo_box);
3361 find_menu_by_path (GtkWidget *menu,
3363 gboolean skip_first)
3368 GtkTreeRowReference *mref;
3372 list = gtk_container_get_children (GTK_CONTAINER (menu));
3375 for (i = list; i; i = i->next)
3377 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3379 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3382 else if (!gtk_tree_row_reference_valid (mref))
3385 mpath = gtk_tree_row_reference_get_path (mref);
3387 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3395 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3400 /* this case is necessary, since the row reference of
3401 * the cell view may already be updated after a deletion
3408 if (gtk_tree_path_compare (mpath, path) == 0)
3410 gtk_tree_path_free (mpath);
3414 if (gtk_tree_path_is_ancestor (mpath, path))
3416 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3417 if (submenu != NULL)
3419 gtk_tree_path_free (mpath);
3420 item = find_menu_by_path (submenu, path, TRUE);
3424 gtk_tree_path_free (mpath);
3434 dump_menu_tree (GtkWidget *menu,
3441 list = gtk_container_get_children (GTK_CONTAINER (menu));
3442 for (i = list; i; i = i->next)
3444 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3446 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3447 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3448 gtk_tree_path_free (path);
3450 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3451 if (submenu != NULL)
3452 dump_menu_tree (submenu, level + 1);
3461 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3466 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3467 GtkComboBoxPrivate *priv = combo_box->priv;
3469 GtkWidget *item, *menu, *separator;
3473 gboolean is_separator;
3475 if (!priv->popup_widget)
3478 depth = gtk_tree_path_get_depth (path);
3479 pos = gtk_tree_path_get_indices (path)[depth - 1];
3482 ppath = gtk_tree_path_copy (path);
3483 gtk_tree_path_up (ppath);
3484 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3485 gtk_tree_path_free (ppath);
3487 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3490 menu = gtk_menu_new ();
3491 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3492 gtk_widget_show (menu);
3493 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3495 /* Ugly - since menus can only activate leaves, we have to
3496 * duplicate the item inside the submenu.
3498 gtk_tree_model_iter_parent (model, &piter, iter);
3499 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3500 separator = gtk_separator_menu_item_new ();
3501 g_signal_connect (item, "activate",
3502 G_CALLBACK (gtk_combo_box_menu_item_activate),
3504 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3505 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3506 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3508 gtk_widget_show (item);
3509 gtk_widget_show (separator);
3516 menu = priv->popup_widget;
3517 if (priv->add_tearoffs)
3521 if (priv->row_separator_func)
3522 is_separator = priv->row_separator_func (model, iter,
3523 priv->row_separator_data);
3525 is_separator = FALSE;
3529 item = gtk_separator_menu_item_new ();
3530 g_object_set_data_full (G_OBJECT (item),
3531 I_("gtk-combo-box-item-path"),
3532 gtk_tree_row_reference_new (model, path),
3533 (GDestroyNotify)gtk_tree_row_reference_free);
3537 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3539 g_signal_connect (item, "activate",
3540 G_CALLBACK (gtk_combo_box_menu_item_activate),
3544 gtk_widget_show (item);
3545 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3549 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3553 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3554 GtkComboBoxPrivate *priv = combo_box->priv;
3558 if (!priv->popup_widget)
3561 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3562 menu = gtk_widget_get_parent (item);
3563 gtk_container_remove (GTK_CONTAINER (menu), item);
3565 if (gtk_tree_path_get_depth (path) > 1)
3567 GtkTreePath *parent_path;
3571 parent_path = gtk_tree_path_copy (path);
3572 gtk_tree_path_up (parent_path);
3573 gtk_tree_model_get_iter (model, &iter, parent_path);
3575 if (!gtk_tree_model_iter_has_child (model, &iter))
3577 parent = find_menu_by_path (priv->popup_widget,
3578 parent_path, FALSE);
3579 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3585 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3591 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3593 gtk_combo_box_relayout (combo_box);
3597 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3602 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3603 GtkComboBoxPrivate *priv = combo_box->priv;
3606 gboolean is_separator;
3608 if (!priv->popup_widget)
3611 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3613 if (priv->row_separator_func)
3614 is_separator = priv->row_separator_func (model, iter,
3615 priv->row_separator_data);
3617 is_separator = FALSE;
3619 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3621 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3622 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3625 if (priv->wrap_width && item->parent == priv->popup_widget)
3627 GtkWidget *pitem = NULL;
3630 prev = gtk_tree_path_copy (path);
3632 if (gtk_tree_path_prev (prev))
3633 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3635 gtk_tree_path_free (prev);
3637 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3639 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3644 "bottom-attach", -1,
3647 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3650 width = gtk_combo_box_calc_requested_width (combo_box, path);
3652 if (width > priv->width)
3654 if (priv->cell_view)
3656 gtk_widget_set_size_request (priv->cell_view, width, -1);
3657 gtk_widget_queue_resize (priv->cell_view);
3659 priv->width = width;
3668 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3670 GtkComboBoxPrivate *priv = combo_box->priv;
3671 GtkTreeSelection *sel;
3673 priv->button = gtk_toggle_button_new ();
3674 gtk_widget_set_parent (priv->button,
3675 GTK_BIN (combo_box)->child->parent);
3676 g_signal_connect (priv->button, "button-press-event",
3677 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3678 g_signal_connect (priv->button, "toggled",
3679 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3681 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3682 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3683 priv->separator = NULL;
3684 gtk_widget_show_all (priv->button);
3686 if (priv->cell_view)
3688 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3689 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3691 priv->box = gtk_event_box_new ();
3692 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3695 if (priv->has_frame)
3697 priv->cell_view_frame = gtk_frame_new (NULL);
3698 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3703 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3704 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3708 gtk_widget_set_parent (priv->cell_view_frame,
3709 GTK_BIN (combo_box)->child->parent);
3710 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3711 gtk_widget_show_all (priv->cell_view_frame);
3713 g_signal_connect (priv->box, "button-press-event",
3714 G_CALLBACK (gtk_combo_box_list_button_pressed),
3718 priv->tree_view = gtk_tree_view_new ();
3719 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3720 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3721 gtk_tree_selection_set_select_function (sel,
3722 gtk_combo_box_list_select_func,
3724 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3726 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3728 if (priv->row_separator_func)
3729 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3730 priv->row_separator_func,
3731 priv->row_separator_data,
3734 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3736 priv->column = gtk_tree_view_column_new ();
3737 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3740 gtk_combo_box_sync_cells (combo_box,
3741 GTK_CELL_LAYOUT (priv->column));
3743 if (gtk_tree_row_reference_valid (priv->active_row))
3747 path = gtk_tree_row_reference_get_path (priv->active_row);
3748 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3750 gtk_tree_path_free (path);
3753 /* set sample/popup widgets */
3754 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3756 g_signal_connect (priv->tree_view, "key-press-event",
3757 G_CALLBACK (gtk_combo_box_list_key_press),
3759 g_signal_connect (priv->tree_view, "enter-notify-event",
3760 G_CALLBACK (gtk_combo_box_list_enter_notify),
3762 g_signal_connect (priv->tree_view, "row-expanded",
3763 G_CALLBACK (gtk_combo_box_model_row_expanded),
3765 g_signal_connect (priv->tree_view, "row-collapsed",
3766 G_CALLBACK (gtk_combo_box_model_row_expanded),
3768 g_signal_connect (priv->popup_window, "button-press-event",
3769 G_CALLBACK (gtk_combo_box_list_button_pressed),
3771 g_signal_connect (priv->popup_window, "button-release-event",
3772 G_CALLBACK (gtk_combo_box_list_button_released),
3775 gtk_widget_show (priv->tree_view);
3777 gtk_combo_box_update_sensitivity (combo_box);
3781 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3783 GtkComboBoxPrivate *priv = combo_box->priv;
3785 /* disconnect signals */
3786 g_signal_handlers_disconnect_matched (priv->tree_view,
3787 G_SIGNAL_MATCH_DATA,
3788 0, 0, NULL, NULL, combo_box);
3789 g_signal_handlers_disconnect_matched (priv->button,
3790 G_SIGNAL_MATCH_DATA,
3792 gtk_combo_box_list_button_pressed,
3794 g_signal_handlers_disconnect_matched (priv->popup_window,
3795 G_SIGNAL_MATCH_DATA,
3797 gtk_combo_box_list_button_pressed,
3799 g_signal_handlers_disconnect_matched (priv->popup_window,
3800 G_SIGNAL_MATCH_DATA,
3802 gtk_combo_box_list_button_released,
3805 g_signal_handlers_disconnect_matched (priv->popup_window,
3806 G_SIGNAL_MATCH_DATA,
3808 gtk_combo_box_child_show,
3811 g_signal_handlers_disconnect_matched (priv->popup_window,
3812 G_SIGNAL_MATCH_DATA,
3814 gtk_combo_box_child_hide,
3818 g_signal_handlers_disconnect_matched (priv->box,
3819 G_SIGNAL_MATCH_DATA,
3821 gtk_combo_box_list_button_pressed,
3824 /* destroy things (unparent will kill the latest ref from us)
3825 * last unref on button will destroy the arrow
3827 gtk_widget_unparent (priv->button);
3828 priv->button = NULL;
3831 if (priv->cell_view)
3833 g_object_set (priv->cell_view,
3834 "background-set", FALSE,
3838 if (priv->cell_view_frame)
3840 gtk_widget_unparent (priv->cell_view_frame);
3841 priv->cell_view_frame = NULL;
3845 if (priv->scroll_timer)
3847 g_source_remove (priv->scroll_timer);
3848 priv->scroll_timer = 0;
3851 if (priv->resize_idle_id)
3853 g_source_remove (priv->resize_idle_id);
3854 priv->resize_idle_id = 0;
3857 gtk_widget_destroy (priv->tree_view);
3859 priv->tree_view = NULL;
3860 if (priv->popup_widget)
3862 g_object_unref (priv->popup_widget);
3863 priv->popup_widget = NULL;
3870 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3871 GdkEventButton *event,
3874 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3875 GtkComboBoxPrivate *priv = combo_box->priv;
3877 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3879 if (ewidget == priv->popup_window)
3882 if ((ewidget != priv->button && ewidget != priv->box) ||
3883 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3886 if (priv->focus_on_click &&
3887 !GTK_WIDGET_HAS_FOCUS (priv->button))
3888 gtk_widget_grab_focus (priv->button);
3890 gtk_combo_box_popup (combo_box);
3892 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3894 priv->auto_scroll = FALSE;
3895 if (priv->scroll_timer == 0)
3896 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3897 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3900 priv->popup_in_progress = TRUE;
3906 gtk_combo_box_list_button_released (GtkWidget *widget,
3907 GdkEventButton *event,
3911 GtkTreePath *path = NULL;
3914 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3915 GtkComboBoxPrivate *priv = combo_box->priv;
3917 gboolean popup_in_progress = FALSE;
3919 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3921 if (priv->popup_in_progress)
3923 popup_in_progress = TRUE;
3924 priv->popup_in_progress = FALSE;
3927 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3929 if (priv->scroll_timer)
3931 g_source_remove (priv->scroll_timer);
3932 priv->scroll_timer = 0;
3935 if (ewidget != priv->tree_view)
3937 if ((ewidget == priv->button ||
3938 ewidget == priv->box) &&
3939 !popup_in_progress &&
3940 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3942 gtk_combo_box_popdown (combo_box);
3946 /* released outside treeview */
3947 if (ewidget != priv->button && ewidget != priv->box)
3949 gtk_combo_box_popdown (combo_box);
3957 /* select something cool */
3958 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3964 return TRUE; /* clicked outside window? */
3966 gtk_tree_model_get_iter (priv->model, &iter, path);
3967 gtk_tree_path_free (path);
3969 gtk_combo_box_popdown (combo_box);
3971 if (tree_column_row_is_sensitive (combo_box, &iter))
3972 gtk_combo_box_set_active_iter (combo_box, &iter);
3978 gtk_combo_box_menu_key_press (GtkWidget *widget,
3982 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3984 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3986 /* The menu hasn't managed the
3987 * event, forward it to the combobox
3989 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3996 gtk_combo_box_list_key_press (GtkWidget *widget,
4000 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4003 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
4004 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
4006 GtkTreeModel *model = NULL;
4008 gtk_combo_box_popdown (combo_box);
4010 if (combo_box->priv->model)
4012 GtkTreeSelection *sel;
4014 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4016 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4017 gtk_combo_box_set_active_iter (combo_box, &iter);
4023 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4025 /* The list hasn't managed the
4026 * event, forward it to the combobox
4028 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4035 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4039 GtkWidget *tree_view = combo_box->priv->tree_view;
4043 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4044 if (adj && adj->upper - adj->lower > adj->page_size)
4046 if (x <= tree_view->allocation.x &&
4047 adj->lower < adj->value)
4049 value = adj->value - (tree_view->allocation.x - x + 1);
4050 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
4052 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4053 adj->upper - adj->page_size > adj->value)
4055 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4056 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
4060 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4061 if (adj && adj->upper - adj->lower > adj->page_size)
4063 if (y <= tree_view->allocation.y &&
4064 adj->lower < adj->value)
4066 value = adj->value - (tree_view->allocation.y - y + 1);
4067 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
4069 else if (y >= tree_view->allocation.height &&
4070 adj->upper - adj->page_size > adj->value)
4072 value = adj->value + (y - tree_view->allocation.height + 1);
4073 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
4079 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4081 GtkComboBoxPrivate *priv = combo_box->priv;
4084 if (priv->auto_scroll)
4086 gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
4087 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4094 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4095 GdkEventCrossing *event,
4098 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4100 combo_box->priv->auto_scroll = TRUE;
4106 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4107 GtkTreeModel *model,
4109 gboolean path_currently_selected,
4113 gboolean sensitive = FALSE;
4115 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4117 GList *cells, *cell;
4118 gboolean cell_sensitive, cell_visible;
4120 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4122 if (!column->visible)
4125 gtk_tree_model_get_iter (model, &iter, path);
4126 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4129 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4132 g_object_get (cell->data,
4133 "sensitive", &cell_sensitive,
4134 "visible", &cell_visible,
4137 if (cell_visible && cell_sensitive)
4142 g_list_free (cells);
4144 sensitive = cell_sensitive;
4151 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4156 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4157 GtkComboBoxPrivate *priv = combo_box->priv;
4160 width = gtk_combo_box_calc_requested_width (combo_box, path);
4162 if (width > priv->width)
4164 if (priv->cell_view)
4166 gtk_widget_set_size_request (priv->cell_view, width, -1);
4167 gtk_widget_queue_resize (priv->cell_view);
4169 priv->width = width;
4174 * GtkCellLayout implementation
4178 pack_start_recurse (GtkWidget *menu,
4179 GtkCellRenderer *cell,
4185 list = gtk_container_get_children (GTK_CONTAINER (menu));
4186 for (i = list; i; i = i->next)
4188 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4189 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4192 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4193 if (submenu != NULL)
4194 pack_start_recurse (submenu, cell, expand);
4201 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4202 GtkCellRenderer *cell,
4205 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4206 ComboCellInfo *info;
4207 GtkComboBoxPrivate *priv;
4209 priv = combo_box->priv;
4211 g_object_ref_sink (cell);
4213 info = g_slice_new0 (ComboCellInfo);
4215 info->expand = expand;
4216 info->pack = GTK_PACK_START;
4218 priv->cells = g_slist_append (priv->cells, info);
4220 if (priv->cell_view)
4221 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4225 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4227 if (GTK_IS_MENU (priv->popup_widget))
4228 pack_start_recurse (priv->popup_widget, cell, expand);
4232 pack_end_recurse (GtkWidget *menu,
4233 GtkCellRenderer *cell,
4239 list = gtk_container_get_children (GTK_CONTAINER (menu));
4240 for (i = list; i; i = i->next)
4242 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4243 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4246 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4247 if (submenu != NULL)
4248 pack_end_recurse (submenu, cell, expand);
4255 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4256 GtkCellRenderer *cell,
4259 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4260 ComboCellInfo *info;
4261 GtkComboBoxPrivate *priv;
4263 priv = combo_box->priv;
4265 g_object_ref_sink (cell);
4267 info = g_slice_new0 (ComboCellInfo);
4269 info->expand = expand;
4270 info->pack = GTK_PACK_END;
4272 priv->cells = g_slist_append (priv->cells, info);
4274 if (priv->cell_view)
4275 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4279 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4281 if (GTK_IS_MENU (priv->popup_widget))
4282 pack_end_recurse (priv->popup_widget, cell, expand);
4286 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4288 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4290 GList *retval = NULL;
4292 for (list = combo_box->priv->cells; list; list = list->next)
4294 ComboCellInfo *info = (ComboCellInfo *)list->data;
4296 retval = g_list_prepend (retval, info->cell);
4299 return g_list_reverse (retval);
4303 clear_recurse (GtkWidget *menu)
4308 list = gtk_container_get_children (GTK_CONTAINER (menu));
4309 for (i = list; i; i = i->next)
4311 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4312 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4314 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4315 if (submenu != NULL)
4316 clear_recurse (submenu);
4323 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4325 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4326 GtkComboBoxPrivate *priv = combo_box->priv;
4329 if (priv->cell_view)
4330 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4333 gtk_tree_view_column_clear (priv->column);
4335 for (i = priv->cells; i; i = i->next)
4337 ComboCellInfo *info = (ComboCellInfo *)i->data;
4339 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4340 g_object_unref (info->cell);
4341 g_slice_free (ComboCellInfo, info);
4344 g_slist_free (priv->cells);
4347 if (GTK_IS_MENU (priv->popup_widget))
4348 clear_recurse (priv->popup_widget);
4352 add_attribute_recurse (GtkWidget *menu,
4353 GtkCellRenderer *cell,
4354 const gchar *attribute,
4360 list = gtk_container_get_children (GTK_CONTAINER (menu));
4361 for (i = list; i; i = i->next)
4363 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4364 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4365 cell, attribute, column);
4367 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4368 if (submenu != NULL)
4369 add_attribute_recurse (submenu, cell, attribute, column);
4376 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4377 GtkCellRenderer *cell,
4378 const gchar *attribute,
4381 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4382 ComboCellInfo *info;
4384 info = gtk_combo_box_get_cell_info (combo_box, cell);
4385 g_return_if_fail (info != NULL);
4387 info->attributes = g_slist_prepend (info->attributes,
4388 GINT_TO_POINTER (column));
4389 info->attributes = g_slist_prepend (info->attributes,
4390 g_strdup (attribute));
4392 if (combo_box->priv->cell_view)
4393 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4394 cell, attribute, column);
4396 if (combo_box->priv->column)
4397 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4398 cell, attribute, column);
4400 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4401 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4402 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4406 combo_cell_data_func (GtkCellLayout *cell_layout,
4407 GtkCellRenderer *cell,
4408 GtkTreeModel *tree_model,
4412 ComboCellInfo *info = (ComboCellInfo *)data;
4413 GtkWidget *parent = NULL;
4418 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4420 if (GTK_IS_WIDGET (cell_layout))
4421 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4423 if (GTK_IS_MENU_ITEM (parent) &&
4424 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4425 g_object_set (cell, "sensitive", TRUE, NULL);
4430 set_cell_data_func_recurse (GtkWidget *menu,
4431 GtkCellRenderer *cell,
4432 ComboCellInfo *info)
4436 GtkWidget *cell_view;
4438 list = gtk_container_get_children (GTK_CONTAINER (menu));
4439 for (i = list; i; i = i->next)
4441 cell_view = GTK_BIN (i->data)->child;
4442 if (GTK_IS_CELL_LAYOUT (cell_view))
4444 /* Override sensitivity for inner nodes; we don't
4445 * want menuitems with submenus to appear insensitive
4447 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4449 combo_cell_data_func,
4451 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4452 if (submenu != NULL)
4453 set_cell_data_func_recurse (submenu, cell, info);
4461 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4462 GtkCellRenderer *cell,
4463 GtkCellLayoutDataFunc func,
4465 GDestroyNotify destroy)
4467 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4468 GtkComboBoxPrivate *priv = combo_box->priv;
4469 ComboCellInfo *info;
4471 info = gtk_combo_box_get_cell_info (combo_box, cell);
4472 g_return_if_fail (info != NULL);
4476 GDestroyNotify d = info->destroy;
4478 info->destroy = NULL;
4479 d (info->func_data);
4483 info->func_data = func_data;
4484 info->destroy = destroy;
4486 if (priv->cell_view)
4487 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4490 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4492 if (GTK_IS_MENU (priv->popup_widget))
4493 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4495 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4499 clear_attributes_recurse (GtkWidget *menu,
4500 GtkCellRenderer *cell)
4505 list = gtk_container_get_children (GTK_CONTAINER (menu));
4506 for (i = list; i; i = i->next)
4508 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4509 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4512 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4513 if (submenu != NULL)
4514 clear_attributes_recurse (submenu, cell);
4521 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4522 GtkCellRenderer *cell)
4524 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4525 GtkComboBoxPrivate *priv;
4526 ComboCellInfo *info;
4529 priv = combo_box->priv;
4531 info = gtk_combo_box_get_cell_info (combo_box, cell);
4532 g_return_if_fail (info != NULL);
4534 list = info->attributes;
4535 while (list && list->next)
4537 g_free (list->data);
4538 list = list->next->next;
4540 g_slist_free (info->attributes);
4541 info->attributes = NULL;
4543 if (priv->cell_view)
4544 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4547 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4549 if (GTK_IS_MENU (priv->popup_widget))
4550 clear_attributes_recurse (priv->popup_widget, cell);
4552 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4556 reorder_recurse (GtkWidget *menu,
4557 GtkCellRenderer *cell,
4563 list = gtk_container_get_children (GTK_CONTAINER (menu));
4564 for (i = list; i; i = i->next)
4566 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4567 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4570 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4571 if (submenu != NULL)
4572 reorder_recurse (submenu, cell, position);
4579 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4580 GtkCellRenderer *cell,
4583 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4584 GtkComboBoxPrivate *priv;
4585 ComboCellInfo *info;
4588 priv = combo_box->priv;
4590 info = gtk_combo_box_get_cell_info (combo_box, cell);
4592 g_return_if_fail (info != NULL);
4593 g_return_if_fail (position >= 0);
4595 link = g_slist_find (priv->cells, info);
4597 g_return_if_fail (link != NULL);
4599 priv->cells = g_slist_delete_link (priv->cells, link);
4600 priv->cells = g_slist_insert (priv->cells, info, position);
4602 if (priv->cell_view)
4603 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4607 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4610 if (GTK_IS_MENU (priv->popup_widget))
4611 reorder_recurse (priv->popup_widget, cell, position);
4613 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4621 * gtk_combo_box_new:
4623 * Creates a new empty #GtkComboBox.
4625 * Return value: A new #GtkComboBox.
4630 gtk_combo_box_new (void)
4632 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4636 * gtk_combo_box_new_with_model:
4637 * @model: A #GtkTreeModel.
4639 * Creates a new #GtkComboBox with the model initialized to @model.
4641 * Return value: A new #GtkComboBox.
4646 gtk_combo_box_new_with_model (GtkTreeModel *model)
4648 GtkComboBox *combo_box;
4650 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4652 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4654 return GTK_WIDGET (combo_box);
4658 * gtk_combo_box_get_wrap_width:
4659 * @combo_box: A #GtkComboBox
4661 * Returns the wrap width which is used to determine the number of columns
4662 * for the popup menu. If the wrap width is larger than 1, the combo box
4665 * Returns: the wrap width.
4670 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4672 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4674 return combo_box->priv->wrap_width;
4678 * gtk_combo_box_set_wrap_width:
4679 * @combo_box: A #GtkComboBox
4680 * @width: Preferred number of columns
4682 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4683 * the preferred number of columns when you want the popup to be layed out
4689 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4692 GtkComboBoxPrivate *priv;
4694 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4695 g_return_if_fail (width >= 0);
4697 priv = combo_box->priv;
4699 if (width != priv->wrap_width)
4701 priv->wrap_width = width;
4703 gtk_combo_box_check_appearance (combo_box);
4704 gtk_combo_box_relayout (combo_box);
4706 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4711 * gtk_combo_box_get_row_span_column:
4712 * @combo_box: A #GtkComboBox
4714 * Returns the column with row span information for @combo_box.
4716 * Returns: the row span column.
4721 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4723 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4725 return combo_box->priv->row_column;
4729 * gtk_combo_box_set_row_span_column:
4730 * @combo_box: A #GtkComboBox.
4731 * @row_span: A column in the model passed during construction.
4733 * Sets the column with row span information for @combo_box to be @row_span.
4734 * The row span column contains integers which indicate how many rows
4735 * an item should span.
4740 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4743 GtkComboBoxPrivate *priv;
4746 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4748 priv = combo_box->priv;
4750 col = gtk_tree_model_get_n_columns (priv->model);
4751 g_return_if_fail (row_span >= -1 && row_span < col);
4753 if (row_span != priv->row_column)
4755 priv->row_column = row_span;
4757 gtk_combo_box_relayout (combo_box);
4759 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4764 * gtk_combo_box_get_column_span_column:
4765 * @combo_box: A #GtkComboBox
4767 * Returns the column with column span information for @combo_box.
4769 * Returns: the column span column.
4774 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4776 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4778 return combo_box->priv->col_column;
4782 * gtk_combo_box_set_column_span_column:
4783 * @combo_box: A #GtkComboBox
4784 * @column_span: A column in the model passed during construction
4786 * Sets the column with column span information for @combo_box to be
4787 * @column_span. The column span column contains integers which indicate
4788 * how many columns an item should span.
4793 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4796 GtkComboBoxPrivate *priv;
4799 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4801 priv = combo_box->priv;
4803 col = gtk_tree_model_get_n_columns (priv->model);
4804 g_return_if_fail (column_span >= -1 && column_span < col);
4806 if (column_span != priv->col_column)
4808 priv->col_column = column_span;
4810 gtk_combo_box_relayout (combo_box);
4812 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4817 * gtk_combo_box_get_active:
4818 * @combo_box: A #GtkComboBox
4820 * Returns the index of the currently active item, or -1 if there's no
4821 * active item. If the model is a non-flat treemodel, and the active item
4822 * is not an immediate child of the root of the tree, this function returns
4823 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4824 * <literal>path</literal> is the #GtkTreePath of the active item.
4826 * Return value: An integer which is the index of the currently active item,
4827 * or -1 if there's no active item.
4832 gtk_combo_box_get_active (GtkComboBox *combo_box)
4834 GtkComboBoxPrivate *priv;
4837 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4839 priv = combo_box->priv;
4841 if (gtk_tree_row_reference_valid (priv->active_row))
4845 path = gtk_tree_row_reference_get_path (priv->active_row);
4846 result = gtk_tree_path_get_indices (path)[0];
4847 gtk_tree_path_free (path);
4856 * gtk_combo_box_set_active:
4857 * @combo_box: A #GtkComboBox
4858 * @index_: An index in the model passed during construction, or -1 to have
4861 * Sets the active item of @combo_box to be the item at @index.
4866 gtk_combo_box_set_active (GtkComboBox *combo_box,
4869 GtkTreePath *path = NULL;
4870 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4871 g_return_if_fail (index_ >= -1);
4873 if (combo_box->priv->model == NULL)
4875 /* Save index, in case the model is set after the index */
4876 combo_box->priv->active = index_;
4882 path = gtk_tree_path_new_from_indices (index_, -1);
4884 gtk_combo_box_set_active_internal (combo_box, path);
4887 gtk_tree_path_free (path);
4891 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4894 GtkComboBoxPrivate *priv = combo_box->priv;
4895 GtkTreePath *active_path;
4898 /* Remember whether the initially active row is valid. */
4899 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4901 if (path && is_valid_row_reference)
4903 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4904 path_cmp = gtk_tree_path_compare (path, active_path);
4905 gtk_tree_path_free (active_path);
4910 if (priv->active_row)
4912 gtk_tree_row_reference_free (priv->active_row);
4913 priv->active_row = NULL;
4918 if (priv->tree_view)
4919 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4922 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4924 if (GTK_IS_MENU (menu))
4925 gtk_menu_set_active (menu, -1);
4928 if (priv->cell_view)
4929 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4932 * Do not emit a "changed" signal when an already invalid selection was
4933 * now set to invalid.
4935 if (!is_valid_row_reference)
4941 gtk_tree_row_reference_new (priv->model, path);
4943 if (priv->tree_view)
4945 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4948 else if (GTK_IS_MENU (priv->popup_widget))
4950 /* FIXME handle nested menus better */
4951 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4952 gtk_tree_path_get_indices (path)[0]);
4955 if (priv->cell_view)
4956 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4960 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4961 g_object_notify (G_OBJECT (combo_box), "active");
4966 * gtk_combo_box_get_active_iter:
4967 * @combo_box: A #GtkComboBox
4968 * @iter: The uninitialized #GtkTreeIter
4970 * Sets @iter to point to the current active item, if it exists.
4972 * Return value: %TRUE, if @iter was set
4977 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4983 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4985 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4988 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4989 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4990 gtk_tree_path_free (path);
4996 * gtk_combo_box_set_active_iter:
4997 * @combo_box: A #GtkComboBox
4998 * @iter: The #GtkTreeIter
5000 * Sets the current active item to be the one referenced by @iter.
5001 * @iter must correspond to a path of depth one.
5006 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5011 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5013 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5014 gtk_combo_box_set_active_internal (combo_box, path);
5015 gtk_tree_path_free (path);
5019 * gtk_combo_box_set_model:
5020 * @combo_box: A #GtkComboBox
5021 * @model: A #GtkTreeModel
5023 * Sets the model used by @combo_box to be @model. Will unset a previously set
5024 * model (if applicable). If model is %NULL, then it will unset the model.
5026 * Note that this function does not clear the cell renderers, you have to
5027 * call gtk_cell_layout_clear() yourself if you need to set up different
5028 * cell renderers for the new model.
5033 gtk_combo_box_set_model (GtkComboBox *combo_box,
5034 GtkTreeModel *model)
5036 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5037 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5039 if (model == combo_box->priv->model)
5042 gtk_combo_box_unset_model (combo_box);
5047 combo_box->priv->model = model;
5048 g_object_ref (combo_box->priv->model);
5050 combo_box->priv->inserted_id =
5051 g_signal_connect (combo_box->priv->model, "row-inserted",
5052 G_CALLBACK (gtk_combo_box_model_row_inserted),
5054 combo_box->priv->deleted_id =
5055 g_signal_connect (combo_box->priv->model, "row-deleted",
5056 G_CALLBACK (gtk_combo_box_model_row_deleted),
5058 combo_box->priv->reordered_id =
5059 g_signal_connect (combo_box->priv->model, "rows-reordered",
5060 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5062 combo_box->priv->changed_id =
5063 g_signal_connect (combo_box->priv->model, "row-changed",
5064 G_CALLBACK (gtk_combo_box_model_row_changed),
5067 if (combo_box->priv->tree_view)
5070 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5071 combo_box->priv->model);
5072 gtk_combo_box_list_popup_resize (combo_box);
5077 if (combo_box->priv->popup_widget)
5078 gtk_combo_box_menu_fill (combo_box);
5082 if (combo_box->priv->cell_view)
5083 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5084 combo_box->priv->model);
5086 if (combo_box->priv->active != -1)
5088 /* If an index was set in advance, apply it now */
5089 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5090 combo_box->priv->active = -1;
5094 gtk_combo_box_update_sensitivity (combo_box);
5096 g_object_notify (G_OBJECT (combo_box), "model");
5100 * gtk_combo_box_get_model
5101 * @combo_box: A #GtkComboBox
5103 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5105 * Return value: A #GtkTreeModel which was passed during construction.
5110 gtk_combo_box_get_model (GtkComboBox *combo_box)
5112 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5114 return combo_box->priv->model;
5118 /* convenience API for simple text combos */
5121 * gtk_combo_box_new_text:
5123 * Convenience function which constructs a new text combo box, which is a
5124 * #GtkComboBox just displaying strings. If you use this function to create
5125 * a text combo box, you should only manipulate its data source with the
5126 * following convenience functions: gtk_combo_box_append_text(),
5127 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5128 * gtk_combo_box_remove_text().
5130 * Return value: A new text combo box.
5135 gtk_combo_box_new_text (void)
5137 GtkWidget *combo_box;
5138 GtkCellRenderer *cell;
5139 GtkListStore *store;
5141 store = gtk_list_store_new (1, G_TYPE_STRING);
5142 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5143 g_object_unref (store);
5145 cell = gtk_cell_renderer_text_new ();
5146 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5147 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5155 * gtk_combo_box_append_text:
5156 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5159 * Appends @string to the list of strings stored in @combo_box. Note that
5160 * you can only use this function with combo boxes constructed with
5161 * gtk_combo_box_new_text().
5166 gtk_combo_box_append_text (GtkComboBox *combo_box,
5170 GtkListStore *store;
5172 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5173 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5174 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5176 g_return_if_fail (text != NULL);
5178 store = GTK_LIST_STORE (combo_box->priv->model);
5180 gtk_list_store_append (store, &iter);
5181 gtk_list_store_set (store, &iter, 0, text, -1);
5185 * gtk_combo_box_insert_text:
5186 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5187 * @position: An index to insert @text
5190 * Inserts @string at @position in the list of strings stored in @combo_box.
5191 * Note that you can only use this function with combo boxes constructed
5192 * with gtk_combo_box_new_text().
5197 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5202 GtkListStore *store;
5204 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5205 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5206 g_return_if_fail (position >= 0);
5207 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5209 g_return_if_fail (text != NULL);
5211 store = GTK_LIST_STORE (combo_box->priv->model);
5213 gtk_list_store_insert (store, &iter, position);
5214 gtk_list_store_set (store, &iter, 0, text, -1);
5218 * gtk_combo_box_prepend_text:
5219 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5222 * Prepends @string to the list of strings stored in @combo_box. Note that
5223 * you can only use this function with combo boxes constructed with
5224 * gtk_combo_box_new_text().
5229 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5233 GtkListStore *store;
5235 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5236 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5237 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5239 g_return_if_fail (text != NULL);
5241 store = GTK_LIST_STORE (combo_box->priv->model);
5243 gtk_list_store_prepend (store, &iter);
5244 gtk_list_store_set (store, &iter, 0, text, -1);
5248 * gtk_combo_box_remove_text:
5249 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5250 * @position: Index of the item to remove
5252 * Removes the string at @position from @combo_box. Note that you can only use
5253 * this function with combo boxes constructed with gtk_combo_box_new_text().
5258 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5262 GtkListStore *store;
5264 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5265 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5266 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5268 g_return_if_fail (position >= 0);
5270 store = GTK_LIST_STORE (combo_box->priv->model);
5272 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5274 gtk_list_store_remove (store, &iter);
5278 * gtk_combo_box_get_active_text:
5279 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5281 * Returns the currently active string in @combo_box or %NULL if none
5282 * is selected. Note that you can only use this function with combo
5283 * boxes constructed with gtk_combo_box_new_text() and with
5284 * #GtkComboBoxEntry<!-- -->s.
5286 * Returns: a newly allocated string containing the currently active text.
5287 * Must be freed with g_free().
5292 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5294 GtkComboBoxClass *class;
5296 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5298 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5300 if (class->get_active_text)
5301 return class->get_active_text (combo_box);
5307 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5312 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5313 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5314 == G_TYPE_STRING, NULL);
5316 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5317 gtk_tree_model_get (combo_box->priv->model, &iter,
5324 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5325 GtkScrollType scroll)
5328 GtkTreeIter new_iter;
5329 gboolean active_iter;
5332 if (!combo_box->priv->model)
5334 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5338 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5342 case GTK_SCROLL_STEP_BACKWARD:
5343 case GTK_SCROLL_STEP_UP:
5344 case GTK_SCROLL_STEP_LEFT:
5347 found = tree_prev (combo_box, combo_box->priv->model,
5348 &iter, &new_iter, FALSE);
5351 /* else fall through */
5353 case GTK_SCROLL_PAGE_FORWARD:
5354 case GTK_SCROLL_PAGE_DOWN:
5355 case GTK_SCROLL_PAGE_RIGHT:
5356 case GTK_SCROLL_END:
5357 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5360 case GTK_SCROLL_STEP_FORWARD:
5361 case GTK_SCROLL_STEP_DOWN:
5362 case GTK_SCROLL_STEP_RIGHT:
5365 found = tree_next (combo_box, combo_box->priv->model,
5366 &iter, &new_iter, FALSE);
5369 /* else fall through */
5371 case GTK_SCROLL_PAGE_BACKWARD:
5372 case GTK_SCROLL_PAGE_UP:
5373 case GTK_SCROLL_PAGE_LEFT:
5374 case GTK_SCROLL_START:
5375 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5382 if (found && active_iter)
5384 GtkTreePath *old_path;
5385 GtkTreePath *new_path;
5387 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5388 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5390 if (gtk_tree_path_compare (old_path, new_path) == 0)
5393 gtk_tree_path_free (old_path);
5394 gtk_tree_path_free (new_path);
5399 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5403 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5408 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5409 gboolean group_cycling)
5411 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5413 gtk_widget_grab_focus (combo_box->priv->button);
5419 gtk_combo_box_grab_focus (GtkWidget *widget)
5421 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5423 gtk_widget_grab_focus (combo_box->priv->button);
5427 gtk_combo_box_destroy (GtkObject *object)
5429 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5431 if (combo_box->priv->popup_idle_id > 0)
5433 g_source_remove (combo_box->priv->popup_idle_id);
5434 combo_box->priv->popup_idle_id = 0;
5437 gtk_combo_box_popdown (combo_box);
5439 if (combo_box->priv->row_separator_destroy)
5440 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5442 combo_box->priv->row_separator_func = NULL;
5443 combo_box->priv->row_separator_data = NULL;
5444 combo_box->priv->row_separator_destroy = NULL;
5446 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5447 combo_box->priv->cell_view = NULL;
5451 gtk_combo_box_dispose(GObject* object)
5453 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5455 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5457 gtk_combo_box_menu_destroy (combo_box);
5458 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5459 combo_box->priv->popup_widget = NULL;
5462 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5466 gtk_combo_box_finalize (GObject *object)
5468 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5471 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5472 gtk_combo_box_list_destroy (combo_box);
5474 if (combo_box->priv->popup_window)
5475 gtk_widget_destroy (combo_box->priv->popup_window);
5477 gtk_combo_box_unset_model (combo_box);
5479 for (i = combo_box->priv->cells; i; i = i->next)
5481 ComboCellInfo *info = (ComboCellInfo *)i->data;
5482 GSList *list = info->attributes;
5485 info->destroy (info->func_data);
5487 while (list && list->next)
5489 g_free (list->data);
5490 list = list->next->next;
5492 g_slist_free (info->attributes);
5494 g_object_unref (info->cell);
5495 g_slice_free (ComboCellInfo, info);
5497 g_slist_free (combo_box->priv->cells);
5499 g_free (combo_box->priv->tearoff_title);
5501 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5505 gtk_cell_editable_key_press (GtkWidget *widget,
5509 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5511 if (event->keyval == GDK_Escape)
5513 combo_box->priv->editing_canceled = TRUE;
5515 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5516 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5520 else if (event->keyval == GDK_Return ||
5521 event->keyval == GDK_ISO_Enter ||
5522 event->keyval == GDK_KP_Enter)
5524 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5525 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5534 popdown_idle (gpointer data)
5536 GtkComboBox *combo_box;
5538 combo_box = GTK_COMBO_BOX (data);
5540 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5541 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5543 g_object_unref (combo_box);
5549 popdown_handler (GtkWidget *widget,
5552 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5556 popup_idle (gpointer data)
5558 GtkComboBox *combo_box;
5560 combo_box = GTK_COMBO_BOX (data);
5562 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5563 combo_box->priv->cell_view)
5564 g_signal_connect_object (combo_box->priv->popup_widget,
5565 "unmap", G_CALLBACK (popdown_handler),
5568 /* we unset this if a menu item is activated */
5569 combo_box->priv->editing_canceled = TRUE;
5570 gtk_combo_box_popup (combo_box);
5572 combo_box->priv->popup_idle_id = 0;
5573 combo_box->priv->activate_button = 0;
5574 combo_box->priv->activate_time = 0;
5580 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5583 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5585 combo_box->priv->is_cell_renderer = TRUE;
5587 if (combo_box->priv->cell_view)
5589 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5590 G_CALLBACK (gtk_cell_editable_key_press),
5593 gtk_widget_grab_focus (combo_box->priv->button);
5597 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5598 G_CALLBACK (gtk_cell_editable_key_press),
5601 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5602 GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5605 /* we do the immediate popup only for the optionmenu-like
5608 if (combo_box->priv->is_cell_renderer &&
5609 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5611 if (event && event->type == GDK_BUTTON_PRESS)
5613 GdkEventButton *event_button = (GdkEventButton *)event;
5615 combo_box->priv->activate_button = event_button->button;
5616 combo_box->priv->activate_time = event_button->time;
5619 combo_box->priv->popup_idle_id =
5620 gdk_threads_add_idle (popup_idle, combo_box);
5626 * gtk_combo_box_get_add_tearoffs:
5627 * @combo_box: a #GtkComboBox
5629 * Gets the current value of the :add-tearoffs property.
5631 * Return value: the current value of the :add-tearoffs property.
5634 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5636 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5638 return combo_box->priv->add_tearoffs;
5642 * gtk_combo_box_set_add_tearoffs:
5643 * @combo_box: a #GtkComboBox
5644 * @add_tearoffs: %TRUE to add tearoff menu items
5646 * Sets whether the popup menu should have a tearoff
5652 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5653 gboolean add_tearoffs)
5655 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5657 add_tearoffs = add_tearoffs != FALSE;
5659 if (combo_box->priv->add_tearoffs != add_tearoffs)
5661 combo_box->priv->add_tearoffs = add_tearoffs;
5662 gtk_combo_box_check_appearance (combo_box);
5663 gtk_combo_box_relayout (combo_box);
5664 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5669 * gtk_combo_box_get_title:
5670 * @combo_box: a #GtkComboBox
5672 * Gets the current title of the menu in tearoff mode. See
5673 * gtk_combo_box_set_add_tearoffs().
5675 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5676 * string which must not be freed.
5680 G_CONST_RETURN gchar*
5681 gtk_combo_box_get_title (GtkComboBox *combo_box)
5683 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5685 return combo_box->priv->tearoff_title;
5689 gtk_combo_box_update_title (GtkComboBox *combo_box)
5691 gtk_combo_box_check_appearance (combo_box);
5693 if (combo_box->priv->popup_widget &&
5694 GTK_IS_MENU (combo_box->priv->popup_widget))
5695 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5696 combo_box->priv->tearoff_title);
5700 * gtk_combo_box_set_title:
5701 * @combo_box: a #GtkComboBox
5702 * @title: a title for the menu in tearoff mode
5704 * Sets the menu's title in tearoff mode.
5709 gtk_combo_box_set_title (GtkComboBox *combo_box,
5712 GtkComboBoxPrivate *priv;
5714 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5716 priv = combo_box->priv;
5718 if (strcmp (title ? title : "",
5719 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5721 g_free (priv->tearoff_title);
5722 priv->tearoff_title = g_strdup (title);
5724 gtk_combo_box_update_title (combo_box);
5726 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5731 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5733 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5735 return combo_box->priv->editing_canceled;
5739 * gtk_combo_box_get_popup_accessible:
5740 * @combo_box: a #GtkComboBox
5742 * Gets the accessible object corresponding to the combo box's popup.
5744 * This function is mostly intended for use by accessibility technologies;
5745 * applications should have little use for it.
5747 * Returns: the accessible object corresponding to the combo box's popup.
5752 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5756 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5758 if (combo_box->priv->popup_widget)
5760 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5768 * gtk_combo_box_get_row_separator_func:
5769 * @combo_box: a #GtkComboBox
5771 * Returns the current row separator function.
5773 * Return value: the current row separator function.
5777 GtkTreeViewRowSeparatorFunc
5778 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5780 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5782 return combo_box->priv->row_separator_func;
5786 * gtk_combo_box_set_row_separator_func:
5787 * @combo_box: a #GtkComboBox
5788 * @func: a #GtkTreeViewRowSeparatorFunc
5789 * @data: user data to pass to @func, or %NULL
5790 * @destroy: destroy notifier for @data, or %NULL
5792 * Sets the row separator function, which is used to determine
5793 * whether a row should be drawn as a separator. If the row separator
5794 * function is %NULL, no separators are drawn. This is the default value.
5799 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5800 GtkTreeViewRowSeparatorFunc func,
5802 GDestroyNotify destroy)
5804 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5806 if (combo_box->priv->row_separator_destroy)
5807 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5809 combo_box->priv->row_separator_func = func;
5810 combo_box->priv->row_separator_data = data;
5811 combo_box->priv->row_separator_destroy = destroy;
5813 if (combo_box->priv->tree_view)
5814 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5817 gtk_combo_box_relayout (combo_box);
5819 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5823 * gtk_combo_box_set_button_sensitivity:
5824 * @combo_box: a #GtkComboBox
5825 * @sensitivity: specify the sensitivity of the dropdown button
5827 * Sets whether the dropdown button of the combo box should be
5828 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5829 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5834 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5835 GtkSensitivityType sensitivity)
5837 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5839 if (combo_box->priv->button_sensitivity != sensitivity)
5841 combo_box->priv->button_sensitivity = sensitivity;
5842 gtk_combo_box_update_sensitivity (combo_box);
5844 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5849 * gtk_combo_box_get_button_sensitivity:
5850 * @combo_box: a #GtkComboBox
5852 * Returns whether the combo box sets the dropdown button
5853 * sensitive or not when there are no items in the model.
5855 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5856 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5857 * if the button is always insensitive or
5858 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5859 * the model has one item to be selected.
5864 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5866 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5868 return combo_box->priv->button_sensitivity;
5873 * gtk_combo_box_set_focus_on_click:
5874 * @combo: a #GtkComboBox
5875 * @focus_on_click: whether the combo box grabs focus when clicked
5878 * Sets whether the combo box will grab focus when it is clicked with
5879 * the mouse. Making mouse clicks not grab focus is useful in places
5880 * like toolbars where you don't want the keyboard focus removed from
5881 * the main area of the application.
5886 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5887 gboolean focus_on_click)
5889 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5891 focus_on_click = focus_on_click != FALSE;
5893 if (combo_box->priv->focus_on_click != focus_on_click)
5895 combo_box->priv->focus_on_click = focus_on_click;
5897 if (combo_box->priv->button)
5898 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5901 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5906 * gtk_combo_box_get_focus_on_click:
5907 * @combo: a #GtkComboBox
5909 * Returns whether the combo box grabs focus when it is clicked
5910 * with the mouse. See gtk_combo_box_set_focus_on_click().
5912 * Return value: %TRUE if the combo box grabs focus when it is
5913 * clicked with the mouse.
5918 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5920 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5922 return combo_box->priv->focus_on_click;
5927 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5928 GtkBuilder *builder,
5930 const gchar *tagname,
5931 GMarkupParser *parser,
5934 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5935 tagname, parser, data))
5938 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5939 tagname, parser, data);
5943 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5944 GtkBuilder *builder,
5946 const gchar *tagname,
5949 if (strcmp (tagname, "attributes") == 0)
5950 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5953 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5957 #define __GTK_COMBO_BOX_C__
5958 #include "gtkaliasdef.c"