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 GdkDevice *grab_pointer;
131 GdkDevice *grab_keyboard;
133 gchar *tearoff_title;
136 /* While debugging this evil code, I have learned that
137 * there are actually 4 modes to this widget, which can
138 * be characterized as follows
140 * 1) menu mode, no child added
143 * cell_view -> GtkCellView, regular child
144 * cell_view_frame -> NULL
145 * button -> GtkToggleButton set_parent to combo
146 * arrow -> GtkArrow set_parent to button
147 * separator -> GtkVSepator set_parent to button
148 * popup_widget -> GtkMenu
149 * popup_window -> NULL
150 * scrolled_window -> NULL
152 * 2) menu mode, child added
156 * cell_view_frame -> NULL
157 * button -> GtkToggleButton set_parent to combo
158 * arrow -> GtkArrow, child of button
160 * popup_widget -> GtkMenu
161 * popup_window -> NULL
162 * scrolled_window -> NULL
164 * 3) list mode, no child added
166 * tree_view -> GtkTreeView, child of scrolled_window
167 * cell_view -> GtkCellView, regular child
168 * cell_view_frame -> GtkFrame, set parent to combo
169 * button -> GtkToggleButton, set_parent to combo
170 * arrow -> GtkArrow, child of button
172 * popup_widget -> tree_view
173 * popup_window -> GtkWindow
174 * scrolled_window -> GtkScrolledWindow, child of popup_window
176 * 4) list mode, child added
178 * tree_view -> GtkTreeView, child of scrolled_window
180 * cell_view_frame -> NULL
181 * button -> GtkToggleButton, set_parent to combo
182 * arrow -> GtkArrow, child of button
184 * popup_widget -> tree_view
185 * popup_window -> GtkWindow
186 * scrolled_window -> GtkScrolledWindow, child of popup_window
202 PROP_ROW_SPAN_COLUMN,
203 PROP_COLUMN_SPAN_COLUMN,
210 PROP_BUTTON_SENSITIVITY,
211 PROP_EDITING_CANCELED
214 static guint combo_box_signals[LAST_SIGNAL] = {0,};
216 #define BONUS_PADDING 4
217 #define SCROLL_TIME 100
221 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
222 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
223 static void gtk_combo_box_dispose (GObject *object);
224 static void gtk_combo_box_finalize (GObject *object);
225 static void gtk_combo_box_destroy (GtkObject *object);
227 static void gtk_combo_box_set_property (GObject *object,
231 static void gtk_combo_box_get_property (GObject *object,
236 static void gtk_combo_box_state_changed (GtkWidget *widget,
237 GtkStateType previous);
238 static void gtk_combo_box_grab_focus (GtkWidget *widget);
239 static void gtk_combo_box_style_set (GtkWidget *widget,
241 static void gtk_combo_box_button_toggled (GtkWidget *widget,
243 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
244 GtkStateType previous,
246 static void gtk_combo_box_add (GtkContainer *container,
248 static void gtk_combo_box_remove (GtkContainer *container,
251 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
252 GtkCellRenderer *cell);
254 static void gtk_combo_box_menu_show (GtkWidget *menu,
256 static void gtk_combo_box_menu_hide (GtkWidget *menu,
259 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
261 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
266 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
271 static void gtk_combo_box_menu_position (GtkMenu *menu,
277 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
279 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
281 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
283 static void gtk_combo_box_size_request (GtkWidget *widget,
284 GtkRequisition *requisition);
285 static void gtk_combo_box_size_allocate (GtkWidget *widget,
286 GtkAllocation *allocation);
287 static void gtk_combo_box_forall (GtkContainer *container,
288 gboolean include_internals,
289 GtkCallback callback,
290 gpointer callback_data);
291 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
292 GdkEventExpose *event);
293 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
294 GdkEventScroll *event);
295 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
298 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
299 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
300 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
301 GtkScrollType scroll);
302 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
303 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
305 /* listening to the model */
306 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
310 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
313 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
318 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
322 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
328 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
333 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
334 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
336 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
337 GdkEventButton *event,
339 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
342 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
343 GdkEventCrossing *event,
345 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
348 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
349 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
350 GdkEventButton *event,
353 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
356 gboolean path_currently_selected,
359 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
363 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
366 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
367 gboolean add_children);
368 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
369 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
372 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
373 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
375 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
379 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
381 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
382 GdkEventButton *event,
384 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
387 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
388 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
392 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
395 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
400 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
404 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
407 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
409 guint32 activate_time);
410 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
415 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
416 GtkCellRenderer *cell,
418 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
419 GtkCellRenderer *cell,
421 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
422 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
423 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
424 GtkCellRenderer *cell,
425 const gchar *attribute,
427 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
428 GtkCellRenderer *cell,
429 GtkCellLayoutDataFunc func,
431 GDestroyNotify destroy);
432 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
433 GtkCellRenderer *cell);
434 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
435 GtkCellRenderer *cell,
437 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
438 gboolean group_cycling);
440 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
441 GtkCellLayout *cell_layout);
442 static void combo_cell_data_func (GtkCellLayout *cell_layout,
443 GtkCellRenderer *cell,
444 GtkTreeModel *tree_model,
447 static void gtk_combo_box_child_show (GtkWidget *widget,
448 GtkComboBox *combo_box);
449 static void gtk_combo_box_child_hide (GtkWidget *widget,
450 GtkComboBox *combo_box);
452 /* GtkBuildable method implementation */
453 static GtkBuildableIface *parent_buildable_iface;
455 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
456 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
459 const gchar *tagname,
460 GMarkupParser *parser,
462 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
465 const gchar *tagname,
468 /* GtkCellEditable method implementations */
469 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
473 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
474 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
475 gtk_combo_box_cell_layout_init)
476 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
477 gtk_combo_box_cell_editable_init)
478 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
479 gtk_combo_box_buildable_init))
484 gtk_combo_box_class_init (GtkComboBoxClass *klass)
486 GObjectClass *object_class;
487 GtkObjectClass *gtk_object_class;
488 GtkContainerClass *container_class;
489 GtkWidgetClass *widget_class;
490 GtkBindingSet *binding_set;
492 klass->get_active_text = gtk_combo_box_real_get_active_text;
494 container_class = (GtkContainerClass *)klass;
495 container_class->forall = gtk_combo_box_forall;
496 container_class->add = gtk_combo_box_add;
497 container_class->remove = gtk_combo_box_remove;
499 widget_class = (GtkWidgetClass *)klass;
500 widget_class->size_allocate = gtk_combo_box_size_allocate;
501 widget_class->size_request = gtk_combo_box_size_request;
502 widget_class->expose_event = gtk_combo_box_expose_event;
503 widget_class->scroll_event = gtk_combo_box_scroll_event;
504 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
505 widget_class->grab_focus = gtk_combo_box_grab_focus;
506 widget_class->style_set = gtk_combo_box_style_set;
507 widget_class->state_changed = gtk_combo_box_state_changed;
509 gtk_object_class = (GtkObjectClass *)klass;
510 gtk_object_class->destroy = gtk_combo_box_destroy;
512 object_class = (GObjectClass *)klass;
513 object_class->dispose = gtk_combo_box_dispose;
514 object_class->finalize = gtk_combo_box_finalize;
515 object_class->set_property = gtk_combo_box_set_property;
516 object_class->get_property = gtk_combo_box_get_property;
520 * GtkComboBox::changed:
521 * @widget: the object which received the signal
523 * The changed signal is emitted when the active
524 * item is changed. The can be due to the user selecting
525 * a different item from the list, or due to a
526 * call to gtk_combo_box_set_active_iter().
527 * It will also be emitted while typing into a GtkComboBoxEntry,
528 * as well as when selecting an item from the GtkComboBoxEntry's list.
532 combo_box_signals[CHANGED] =
533 g_signal_new (I_("changed"),
534 G_OBJECT_CLASS_TYPE (klass),
536 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
538 g_cclosure_marshal_VOID__VOID,
541 * GtkComboBox::move-active:
542 * @widget: the object that received the signal
543 * @scroll_type: a #GtkScrollType
545 * The ::move-active signal is a
546 * <link linkend="keybinding-signals">keybinding signal</link>
547 * which gets emitted to move the active selection.
551 combo_box_signals[MOVE_ACTIVE] =
552 g_signal_new_class_handler (I_("move-active"),
553 G_OBJECT_CLASS_TYPE (klass),
554 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
555 G_CALLBACK (gtk_combo_box_real_move_active),
557 g_cclosure_marshal_VOID__ENUM,
559 GTK_TYPE_SCROLL_TYPE);
562 * GtkComboBox::popup:
563 * @widget: the object that received the signal
565 * The ::popup signal is a
566 * <link linkend="keybinding-signals">keybinding signal</link>
567 * which gets emitted to popup the combo box list.
569 * The default binding for this signal is Alt+Down.
573 combo_box_signals[POPUP] =
574 g_signal_new_class_handler (I_("popup"),
575 G_OBJECT_CLASS_TYPE (klass),
576 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
577 G_CALLBACK (gtk_combo_box_real_popup),
579 g_cclosure_marshal_VOID__VOID,
582 * GtkComboBox::popdown:
583 * @button: the object which received the signal
585 * The ::popdown signal is a
586 * <link linkend="keybinding-signals">keybinding signal</link>
587 * which gets emitted to popdown the combo box list.
589 * The default bindings for this signal are Alt+Up and Escape.
593 combo_box_signals[POPDOWN] =
594 g_signal_new_class_handler (I_("popdown"),
595 G_OBJECT_CLASS_TYPE (klass),
596 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
597 G_CALLBACK (gtk_combo_box_real_popdown),
599 _gtk_marshal_BOOLEAN__VOID,
603 binding_set = gtk_binding_set_by_class (widget_class);
605 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
607 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
610 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
612 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
614 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
617 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
619 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
620 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
622 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
623 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
625 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
626 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
628 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
629 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
631 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
632 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
634 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
636 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
638 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
639 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
641 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
642 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
644 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
645 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
647 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
648 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
650 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
651 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
653 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
656 g_object_class_override_property (object_class,
657 PROP_EDITING_CANCELED,
663 * The model from which the combo box takes the values shown
668 g_object_class_install_property (object_class,
670 g_param_spec_object ("model",
671 P_("ComboBox model"),
672 P_("The model for the combo box"),
674 GTK_PARAM_READWRITE));
677 * GtkComboBox:wrap-width:
679 * If wrap-width is set to a positive value, the list will be
680 * displayed in multiple columns, the number of columns is
681 * determined by wrap-width.
685 g_object_class_install_property (object_class,
687 g_param_spec_int ("wrap-width",
689 P_("Wrap width for laying out the items in a grid"),
693 GTK_PARAM_READWRITE));
697 * GtkComboBox:row-span-column:
699 * If this is set to a non-negative value, it must be the index of a column
700 * of type %G_TYPE_INT in the model.
702 * The values of that column are used to determine how many rows a value in
703 * the list will span. Therefore, the values in the model column pointed to
704 * by this property must be greater than zero and not larger than wrap-width.
708 g_object_class_install_property (object_class,
709 PROP_ROW_SPAN_COLUMN,
710 g_param_spec_int ("row-span-column",
711 P_("Row span column"),
712 P_("TreeModel column containing the row span values"),
716 GTK_PARAM_READWRITE));
720 * GtkComboBox:column-span-column:
722 * If this is set to a non-negative value, it must be the index of a column
723 * of type %G_TYPE_INT in the model.
725 * The values of that column are used to determine how many columns a value
726 * in the list will span.
730 g_object_class_install_property (object_class,
731 PROP_COLUMN_SPAN_COLUMN,
732 g_param_spec_int ("column-span-column",
733 P_("Column span column"),
734 P_("TreeModel column containing the column span values"),
738 GTK_PARAM_READWRITE));
742 * GtkComboBox:active:
744 * The item which is currently active. If the model is a non-flat treemodel,
745 * and the active item is not an immediate child of the root of the tree,
746 * this property has the value
747 * <literal>gtk_tree_path_get_indices (path, NULL)[0]</literal>,
748 * where <literal>path</literal> is the #GtkTreePath of the active item.
752 g_object_class_install_property (object_class,
754 g_param_spec_int ("active",
756 P_("The item which is currently active"),
760 GTK_PARAM_READWRITE));
763 * GtkComboBox:add-tearoffs:
765 * The add-tearoffs property controls whether generated menus
766 * have tearoff menu items.
768 * Note that this only affects menu style combo boxes.
772 g_object_class_install_property (object_class,
774 g_param_spec_boolean ("add-tearoffs",
775 P_("Add tearoffs to menus"),
776 P_("Whether dropdowns should have a tearoff menu item"),
778 GTK_PARAM_READWRITE));
781 * GtkComboBox:has-frame:
783 * The has-frame property controls whether a frame
784 * is drawn around the entry.
788 g_object_class_install_property (object_class,
790 g_param_spec_boolean ("has-frame",
792 P_("Whether the combo box draws a frame around the child"),
794 GTK_PARAM_READWRITE));
796 g_object_class_install_property (object_class,
798 g_param_spec_boolean ("focus-on-click",
799 P_("Focus on click"),
800 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
802 GTK_PARAM_READWRITE));
805 * GtkComboBox:tearoff-title:
807 * A title that may be displayed by the window manager
808 * when the popup is torn-off.
812 g_object_class_install_property (object_class,
814 g_param_spec_string ("tearoff-title",
816 P_("A title that may be displayed by the window manager when the popup is torn-off"),
818 GTK_PARAM_READWRITE));
822 * GtkComboBox:popup-shown:
824 * Whether the combo boxes dropdown is popped up.
825 * Note that this property is mainly useful, because
826 * it allows you to connect to notify::popup-shown.
830 g_object_class_install_property (object_class,
832 g_param_spec_boolean ("popup-shown",
834 P_("Whether the combo's dropdown is shown"),
836 GTK_PARAM_READABLE));
840 * GtkComboBox:button-sensitivity:
842 * Whether the dropdown button is sensitive when
843 * the model is empty.
847 g_object_class_install_property (object_class,
848 PROP_BUTTON_SENSITIVITY,
849 g_param_spec_enum ("button-sensitivity",
850 P_("Button Sensitivity"),
851 P_("Whether the dropdown button is sensitive when the model is empty"),
852 GTK_TYPE_SENSITIVITY_TYPE,
853 GTK_SENSITIVITY_AUTO,
854 GTK_PARAM_READWRITE));
856 gtk_widget_class_install_style_property (widget_class,
857 g_param_spec_boolean ("appears-as-list",
858 P_("Appears as list"),
859 P_("Whether dropdowns should look like lists rather than menus"),
861 GTK_PARAM_READABLE));
864 * GtkComboBox:arrow-size:
866 * Sets the minimum size of the arrow in the combo box. Note
867 * that the arrow size is coupled to the font size, so in case
868 * a larger font is used, the arrow will be larger than set
873 gtk_widget_class_install_style_property (widget_class,
874 g_param_spec_int ("arrow-size",
876 P_("The minimum size of the arrow in the combo box"),
880 GTK_PARAM_READABLE));
883 * GtkComboBox:shadow-type:
885 * Which kind of shadow to draw around the combo box.
889 gtk_widget_class_install_style_property (widget_class,
890 g_param_spec_enum ("shadow-type",
892 P_("Which kind of shadow to draw around the combo box"),
893 GTK_TYPE_SHADOW_TYPE,
895 GTK_PARAM_READABLE));
897 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
901 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
903 parent_buildable_iface = g_type_interface_peek_parent (iface);
904 iface->add_child = _gtk_cell_layout_buildable_add_child;
905 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
906 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
910 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
912 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
913 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
914 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
915 iface->clear = gtk_combo_box_cell_layout_clear;
916 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
917 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
918 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
919 iface->reorder = gtk_combo_box_cell_layout_reorder;
923 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
925 iface->start_editing = gtk_combo_box_start_editing;
929 gtk_combo_box_init (GtkComboBox *combo_box)
931 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
933 priv->cell_view = gtk_cell_view_new ();
934 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
935 GTK_BIN (combo_box)->child = priv->cell_view;
936 gtk_widget_show (priv->cell_view);
940 priv->wrap_width = 0;
943 priv->active_row = NULL;
944 priv->col_column = -1;
945 priv->row_column = -1;
947 priv->popup_shown = FALSE;
948 priv->add_tearoffs = FALSE;
949 priv->has_frame = TRUE;
950 priv->is_cell_renderer = FALSE;
951 priv->editing_canceled = FALSE;
952 priv->auto_scroll = FALSE;
953 priv->focus_on_click = TRUE;
954 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
956 combo_box->priv = priv;
958 gtk_combo_box_check_appearance (combo_box);
962 gtk_combo_box_set_property (GObject *object,
967 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
972 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
975 case PROP_WRAP_WIDTH:
976 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
979 case PROP_ROW_SPAN_COLUMN:
980 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
983 case PROP_COLUMN_SPAN_COLUMN:
984 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
988 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
991 case PROP_ADD_TEAROFFS:
992 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
996 combo_box->priv->has_frame = g_value_get_boolean (value);
999 case PROP_FOCUS_ON_CLICK:
1000 gtk_combo_box_set_focus_on_click (combo_box,
1001 g_value_get_boolean (value));
1004 case PROP_TEAROFF_TITLE:
1005 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1008 case PROP_POPUP_SHOWN:
1009 if (g_value_get_boolean (value))
1010 gtk_combo_box_popup (combo_box);
1012 gtk_combo_box_popdown (combo_box);
1015 case PROP_BUTTON_SENSITIVITY:
1016 gtk_combo_box_set_button_sensitivity (combo_box,
1017 g_value_get_enum (value));
1020 case PROP_EDITING_CANCELED:
1021 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1025 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1031 gtk_combo_box_get_property (GObject *object,
1036 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1037 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
1042 g_value_set_object (value, combo_box->priv->model);
1045 case PROP_WRAP_WIDTH:
1046 g_value_set_int (value, combo_box->priv->wrap_width);
1049 case PROP_ROW_SPAN_COLUMN:
1050 g_value_set_int (value, combo_box->priv->row_column);
1053 case PROP_COLUMN_SPAN_COLUMN:
1054 g_value_set_int (value, combo_box->priv->col_column);
1058 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1061 case PROP_ADD_TEAROFFS:
1062 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1065 case PROP_HAS_FRAME:
1066 g_value_set_boolean (value, combo_box->priv->has_frame);
1069 case PROP_FOCUS_ON_CLICK:
1070 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1073 case PROP_TEAROFF_TITLE:
1074 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1077 case PROP_POPUP_SHOWN:
1078 g_value_set_boolean (value, combo_box->priv->popup_shown);
1081 case PROP_BUTTON_SENSITIVITY:
1082 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1085 case PROP_EDITING_CANCELED:
1086 g_value_set_boolean (value, priv->editing_canceled);
1090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1096 gtk_combo_box_state_changed (GtkWidget *widget,
1097 GtkStateType previous)
1099 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1100 GtkComboBoxPrivate *priv = combo_box->priv;
1102 if (gtk_widget_get_realized (widget))
1104 if (priv->tree_view && priv->cell_view)
1105 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1106 &widget->style->base[gtk_widget_get_state (widget)]);
1109 gtk_widget_queue_draw (widget);
1113 gtk_combo_box_button_state_changed (GtkWidget *widget,
1114 GtkStateType previous,
1117 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1118 GtkComboBoxPrivate *priv = combo_box->priv;
1120 if (gtk_widget_get_realized (widget))
1122 if (!priv->tree_view && priv->cell_view)
1124 if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) !=
1125 (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE))
1126 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1128 gtk_widget_set_state (priv->cell_view,
1129 gtk_widget_get_state (widget));
1133 gtk_widget_queue_draw (widget);
1137 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1139 GtkComboBoxPrivate *priv = combo_box->priv;
1140 gboolean appears_as_list;
1142 /* if wrap_width > 0, then we are in grid-mode and forced to use
1145 if (priv->wrap_width)
1146 appears_as_list = FALSE;
1148 gtk_widget_style_get (GTK_WIDGET (combo_box),
1149 "appears-as-list", &appears_as_list,
1152 if (appears_as_list)
1154 /* Destroy all the menu mode widgets, if they exist. */
1155 if (GTK_IS_MENU (priv->popup_widget))
1156 gtk_combo_box_menu_destroy (combo_box);
1158 /* Create the list mode widgets, if they don't already exist. */
1159 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1160 gtk_combo_box_list_setup (combo_box);
1164 /* Destroy all the list mode widgets, if they exist. */
1165 if (GTK_IS_TREE_VIEW (priv->tree_view))
1166 gtk_combo_box_list_destroy (combo_box);
1168 /* Create the menu mode widgets, if they don't already exist. */
1169 if (!GTK_IS_MENU (priv->popup_widget))
1170 gtk_combo_box_menu_setup (combo_box, TRUE);
1173 gtk_widget_style_get (GTK_WIDGET (combo_box),
1174 "shadow-type", &priv->shadow_type,
1179 gtk_combo_box_style_set (GtkWidget *widget,
1182 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1183 GtkComboBoxPrivate *priv = combo_box->priv;
1185 gtk_combo_box_check_appearance (combo_box);
1187 if (priv->tree_view && priv->cell_view)
1188 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1189 &widget->style->base[gtk_widget_get_state (widget)]);
1191 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1192 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1193 GTK_SHADOW_NONE == priv->shadow_type ?
1194 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1198 gtk_combo_box_button_toggled (GtkWidget *widget,
1201 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1203 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1205 if (!combo_box->priv->popup_in_progress)
1206 gtk_combo_box_popup (combo_box);
1209 gtk_combo_box_popdown (combo_box);
1213 gtk_combo_box_add (GtkContainer *container,
1216 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1217 GtkComboBoxPrivate *priv = combo_box->priv;
1219 if (priv->cell_view && priv->cell_view->parent)
1221 gtk_widget_unparent (priv->cell_view);
1222 GTK_BIN (container)->child = NULL;
1223 gtk_widget_queue_resize (GTK_WIDGET (container));
1226 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1227 GTK_BIN (container)->child = widget;
1229 if (priv->cell_view &&
1230 widget != priv->cell_view)
1232 /* since the cell_view was unparented, it's gone now */
1233 priv->cell_view = NULL;
1235 if (!priv->tree_view && priv->separator)
1237 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1239 priv->separator = NULL;
1241 gtk_widget_queue_resize (GTK_WIDGET (container));
1243 else if (priv->cell_view_frame)
1245 gtk_widget_unparent (priv->cell_view_frame);
1246 priv->cell_view_frame = NULL;
1253 gtk_combo_box_remove (GtkContainer *container,
1256 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1257 GtkComboBoxPrivate *priv = combo_box->priv;
1259 gboolean appears_as_list;
1261 if (widget == priv->cell_view)
1262 priv->cell_view = NULL;
1264 gtk_widget_unparent (widget);
1265 GTK_BIN (container)->child = NULL;
1267 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1270 gtk_widget_queue_resize (GTK_WIDGET (container));
1272 if (!priv->tree_view)
1273 appears_as_list = FALSE;
1275 appears_as_list = TRUE;
1277 if (appears_as_list)
1278 gtk_combo_box_list_destroy (combo_box);
1279 else if (GTK_IS_MENU (priv->popup_widget))
1281 gtk_combo_box_menu_destroy (combo_box);
1282 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1283 priv->popup_widget = NULL;
1286 if (!priv->cell_view)
1288 priv->cell_view = gtk_cell_view_new ();
1289 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1290 GTK_BIN (container)->child = priv->cell_view;
1292 gtk_widget_show (priv->cell_view);
1293 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1295 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1299 if (appears_as_list)
1300 gtk_combo_box_list_setup (combo_box);
1302 gtk_combo_box_menu_setup (combo_box, TRUE);
1304 if (gtk_tree_row_reference_valid (priv->active_row))
1306 path = gtk_tree_row_reference_get_path (priv->active_row);
1307 gtk_combo_box_set_active_internal (combo_box, path);
1308 gtk_tree_path_free (path);
1311 gtk_combo_box_set_active_internal (combo_box, NULL);
1314 static ComboCellInfo *
1315 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1316 GtkCellRenderer *cell)
1320 for (i = combo_box->priv->cells; i; i = i->next)
1322 ComboCellInfo *info = (ComboCellInfo *)i->data;
1324 if (info && info->cell == cell)
1332 gtk_combo_box_menu_show (GtkWidget *menu,
1335 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1336 GtkComboBoxPrivate *priv = combo_box->priv;
1338 gtk_combo_box_child_show (menu, user_data);
1340 priv->popup_in_progress = TRUE;
1341 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1343 priv->popup_in_progress = FALSE;
1347 gtk_combo_box_menu_hide (GtkWidget *menu,
1350 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1352 gtk_combo_box_child_hide (menu,user_data);
1354 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1359 gtk_combo_box_detacher (GtkWidget *widget,
1362 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1363 GtkComboBoxPrivate *priv = combo_box->priv;
1365 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1367 g_signal_handlers_disconnect_by_func (menu->toplevel,
1368 gtk_combo_box_menu_show,
1370 g_signal_handlers_disconnect_by_func (menu->toplevel,
1371 gtk_combo_box_menu_hide,
1374 priv->popup_widget = NULL;
1378 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1381 GtkComboBoxPrivate *priv = combo_box->priv;
1383 if (GTK_IS_MENU (priv->popup_widget))
1385 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1386 priv->popup_widget = NULL;
1388 else if (priv->popup_widget)
1390 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1391 priv->popup_widget);
1392 g_object_unref (priv->popup_widget);
1393 priv->popup_widget = NULL;
1396 if (GTK_IS_MENU (popup))
1398 if (priv->popup_window)
1400 gtk_widget_destroy (priv->popup_window);
1401 priv->popup_window = NULL;
1404 priv->popup_widget = popup;
1407 * Note that we connect to show/hide on the toplevel, not the
1408 * menu itself, since the menu is not shown/hidden when it is
1409 * popped up while torn-off.
1411 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1412 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1413 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1414 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1416 gtk_menu_attach_to_widget (GTK_MENU (popup),
1417 GTK_WIDGET (combo_box),
1418 gtk_combo_box_detacher);
1422 if (!priv->popup_window)
1424 GtkWidget *toplevel;
1426 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1427 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1429 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1430 GDK_WINDOW_TYPE_HINT_COMBO);
1432 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1433 G_CALLBACK (gtk_combo_box_child_show),
1435 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1436 G_CALLBACK (gtk_combo_box_child_hide),
1439 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1440 if (GTK_IS_WINDOW (toplevel))
1442 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1443 GTK_WINDOW (priv->popup_window));
1444 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1445 GTK_WINDOW (toplevel));
1448 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1449 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1450 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1452 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1454 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1457 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1460 gtk_widget_show (priv->scrolled_window);
1462 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1463 priv->scrolled_window);
1466 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1469 gtk_widget_show (popup);
1470 g_object_ref (popup);
1471 priv->popup_widget = popup;
1476 gtk_combo_box_menu_position_below (GtkMenu *menu,
1482 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1488 GdkRectangle monitor;
1490 /* FIXME: is using the size request here broken? */
1491 child = GTK_BIN (combo_box)->child;
1495 if (!gtk_widget_get_has_window (child))
1497 sx += child->allocation.x;
1498 sy += child->allocation.y;
1501 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1503 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1504 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1506 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1508 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1511 *x = sx + child->allocation.width - req.width;
1514 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1515 monitor_num = gdk_screen_get_monitor_at_window (screen,
1516 GTK_WIDGET (combo_box)->window);
1517 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1521 else if (*x + req.width > monitor.x + monitor.width)
1522 *x = monitor.x + monitor.width - req.width;
1524 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1525 *y += child->allocation.height;
1526 else if (*y - monitor.y >= req.height)
1528 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1529 *y += child->allocation.height;
1537 gtk_combo_box_menu_position_over (GtkMenu *menu,
1543 GtkComboBox *combo_box;
1547 GtkRequisition requisition;
1554 combo_box = GTK_COMBO_BOX (user_data);
1555 widget = GTK_WIDGET (combo_box);
1557 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1558 menu_width = requisition.width;
1560 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1562 menu_xpos = widget->allocation.x;
1563 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1567 gtk_widget_get_child_requisition (active, &requisition);
1568 menu_ypos -= requisition.height / 2;
1571 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1574 child = children->data;
1576 if (active == child)
1579 if (gtk_widget_get_visible (child))
1581 gtk_widget_get_child_requisition (child, &requisition);
1582 menu_ypos -= requisition.height;
1585 children = children->next;
1588 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1589 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1591 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1592 &menu_xpos, &menu_ypos);
1594 /* Clamp the position on screen */
1595 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1599 else if ((menu_xpos + menu_width) > screen_width)
1600 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1609 gtk_combo_box_menu_position (GtkMenu *menu,
1615 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1616 GtkComboBoxPrivate *priv = combo_box->priv;
1617 GtkWidget *menu_item;
1620 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1621 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1624 /* FIXME handle nested menus better */
1625 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1627 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1630 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1633 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1634 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1635 GDK_WINDOW_TYPE_HINT_COMBO);
1639 gtk_combo_box_list_position (GtkComboBox *combo_box,
1645 GtkComboBoxPrivate *priv = combo_box->priv;
1648 GdkRectangle monitor;
1649 GtkRequisition popup_req;
1650 GtkPolicyType hpolicy, vpolicy;
1652 /* under windows, the drop down list is as wide as the combo box itself.
1654 GtkWidget *sample = GTK_WIDGET (combo_box);
1658 if (!gtk_widget_get_has_window (sample))
1660 *x += sample->allocation.x;
1661 *y += sample->allocation.y;
1664 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1666 *width = sample->allocation.width;
1668 hpolicy = vpolicy = GTK_POLICY_NEVER;
1669 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1671 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1673 if (popup_req.width > *width)
1675 hpolicy = GTK_POLICY_ALWAYS;
1676 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1678 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1681 *height = popup_req.height;
1683 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1684 monitor_num = gdk_screen_get_monitor_at_window (screen,
1685 GTK_WIDGET (combo_box)->window);
1686 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1690 else if (*x + *width > monitor.x + monitor.width)
1691 *x = monitor.x + monitor.width - *width;
1693 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1694 *y += sample->allocation.height;
1695 else if (*y - *height >= monitor.y)
1697 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1699 *y += sample->allocation.height;
1700 *height = monitor.y + monitor.height - *y;
1704 *height = *y - monitor.y;
1708 if (popup_req.height > *height)
1710 vpolicy = GTK_POLICY_ALWAYS;
1712 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1718 cell_view_is_sensitive (GtkCellView *cell_view)
1720 GList *cells, *list;
1723 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1726 for (list = cells; list; list = list->next)
1728 g_object_get (list->data, "sensitive", &sensitive, NULL);
1733 g_list_free (cells);
1739 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1742 GtkComboBoxPrivate *priv = combo_box->priv;
1743 GList *cells, *list;
1749 if (priv->row_separator_func)
1751 if (priv->row_separator_func (priv->model, iter,
1752 priv->row_separator_data))
1756 gtk_tree_view_column_cell_set_cell_data (priv->column,
1758 iter, FALSE, FALSE);
1760 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1763 for (list = cells; list; list = list->next)
1765 g_object_get (list->data, "sensitive", &sensitive, NULL);
1770 g_list_free (cells);
1776 update_menu_sensitivity (GtkComboBox *combo_box,
1779 GtkComboBoxPrivate *priv = combo_box->priv;
1780 GList *children, *child;
1781 GtkWidget *item, *submenu, *separator;
1782 GtkWidget *cell_view;
1788 children = gtk_container_get_children (GTK_CONTAINER (menu));
1790 for (child = children; child; child = child->next)
1792 item = GTK_WIDGET (child->data);
1793 cell_view = GTK_BIN (item)->child;
1795 if (!GTK_IS_CELL_VIEW (cell_view))
1798 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1799 if (submenu != NULL)
1801 gtk_widget_set_sensitive (item, TRUE);
1802 update_menu_sensitivity (combo_box, submenu);
1806 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1808 if (menu != priv->popup_widget && child == children)
1810 separator = GTK_WIDGET (child->next->data);
1811 g_object_set (item, "visible", sensitive, NULL);
1812 g_object_set (separator, "visible", sensitive, NULL);
1815 gtk_widget_set_sensitive (item, sensitive);
1819 g_list_free (children);
1823 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1825 guint32 activate_time)
1827 GtkComboBoxPrivate *priv = combo_box->priv;
1830 GtkRequisition requisition;
1833 update_menu_sensitivity (combo_box, priv->popup_widget);
1836 if (gtk_tree_row_reference_valid (priv->active_row))
1838 path = gtk_tree_row_reference_get_path (priv->active_row);
1839 active_item = gtk_tree_path_get_indices (path, NULL)[0];
1840 gtk_tree_path_free (path);
1842 if (priv->add_tearoffs)
1846 /* FIXME handle nested menus better */
1847 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1849 if (priv->wrap_width == 0)
1851 width = GTK_WIDGET (combo_box)->allocation.width;
1852 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1853 gtk_widget_size_request (priv->popup_widget, &requisition);
1855 gtk_widget_set_size_request (priv->popup_widget,
1856 MAX (width, requisition.width), -1);
1859 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1861 gtk_combo_box_menu_position, combo_box,
1862 button, activate_time);
1866 popup_grab_on_window (GdkWindow *window,
1867 GdkDevice *keyboard,
1869 guint32 activate_time)
1872 gdk_device_grab (keyboard, window,
1873 GDK_OWNERSHIP_WINDOW, TRUE,
1874 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1875 NULL, activate_time) != GDK_GRAB_SUCCESS)
1879 gdk_device_grab (pointer, window,
1880 GDK_OWNERSHIP_WINDOW, TRUE,
1881 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1882 GDK_POINTER_MOTION_MASK,
1883 NULL, activate_time) != GDK_GRAB_SUCCESS)
1886 gdk_device_ungrab (keyboard, activate_time);
1895 * gtk_combo_box_popup:
1896 * @combo_box: a #GtkComboBox
1898 * Pops up the menu or dropdown list of @combo_box.
1900 * This function is mostly intended for use by accessibility technologies;
1901 * applications should have little use for it.
1906 gtk_combo_box_popup (GtkComboBox *combo_box)
1908 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1910 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1914 * gtk_combo_box_popup_for_device:
1915 * @combo_box: a #GtkComboBox
1916 * @device: a #GdkDevice
1918 * Pops up the menu or dropdown list of @combo_box, the popup window
1919 * will be grabbed so only @device and its associated pointer/keyboard
1920 * are the only #GdkDevice<!-- -->s able to send events to it.
1925 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
1928 GtkComboBoxPrivate *priv = combo_box->priv;
1929 gint x, y, width, height;
1930 GtkTreePath *path = NULL, *ppath;
1931 GtkWidget *toplevel;
1932 GdkDevice *keyboard, *pointer;
1935 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1936 g_return_if_fail (GDK_IS_DEVICE (device));
1938 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
1941 if (gtk_widget_get_mapped (priv->popup_widget))
1944 if (priv->grab_pointer && priv->grab_keyboard)
1947 time = gtk_get_current_event_time ();
1949 if (device->source == GDK_SOURCE_KEYBOARD)
1952 pointer = gdk_device_get_associated_device (device);
1957 keyboard = gdk_device_get_associated_device (device);
1960 if (GTK_IS_MENU (priv->popup_widget))
1962 gtk_combo_box_menu_popup (combo_box,
1963 priv->activate_button,
1964 priv->activate_time);
1968 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1969 if (GTK_IS_WINDOW (toplevel))
1970 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1971 GTK_WINDOW (priv->popup_window));
1973 gtk_widget_show_all (priv->scrolled_window);
1974 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1976 gtk_widget_set_size_request (priv->popup_window, width, height);
1977 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1979 if (gtk_tree_row_reference_valid (priv->active_row))
1981 path = gtk_tree_row_reference_get_path (priv->active_row);
1982 ppath = gtk_tree_path_copy (path);
1983 if (gtk_tree_path_up (ppath))
1984 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1986 gtk_tree_path_free (ppath);
1988 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1992 gtk_widget_show (priv->popup_window);
1996 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1998 gtk_tree_path_free (path);
2001 gtk_widget_grab_focus (priv->popup_window);
2002 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2005 if (!gtk_widget_has_focus (priv->tree_view))
2006 gtk_widget_grab_focus (priv->tree_view);
2008 if (!popup_grab_on_window (priv->popup_window->window,
2009 keyboard, pointer, time))
2011 gtk_widget_hide (priv->popup_window);
2015 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2016 priv->grab_pointer = pointer;
2017 priv->grab_keyboard = keyboard;
2021 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2025 device = gtk_get_current_event_device ();
2029 GdkDeviceManager *device_manager;
2030 GdkDisplay *display;
2033 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2034 device_manager = gdk_display_get_device_manager (display);
2036 /* No device was set, pick the first master device */
2037 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2038 device = devices->data;
2039 g_list_free (devices);
2042 gtk_combo_box_popup_for_device (combo_box, device);
2046 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2048 if (combo_box->priv->popup_shown)
2050 gtk_combo_box_popdown (combo_box);
2058 * gtk_combo_box_popdown:
2059 * @combo_box: a #GtkComboBox
2061 * Hides the menu or dropdown list of @combo_box.
2063 * This function is mostly intended for use by accessibility technologies;
2064 * applications should have little use for it.
2069 gtk_combo_box_popdown (GtkComboBox *combo_box)
2071 GtkComboBoxPrivate *priv = combo_box->priv;
2073 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2075 if (GTK_IS_MENU (priv->popup_widget))
2077 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2081 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2084 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2085 gtk_widget_hide_all (priv->popup_window);
2086 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2089 priv->grab_pointer = NULL;
2090 priv->grab_keyboard = NULL;
2094 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2097 GtkComboBoxPrivate *priv = combo_box->priv;
2101 if (priv->cell_view)
2102 gtk_widget_style_get (priv->cell_view,
2103 "focus-line-width", &padding,
2108 /* add some pixels for good measure */
2109 padding += BONUS_PADDING;
2111 if (priv->cell_view)
2112 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2117 return req.width + padding;
2121 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2123 GtkComboBoxPrivate *priv = combo_box->priv;
2128 !gtk_tree_model_get_iter_first (priv->model, &iter))
2134 path = gtk_tree_path_new_from_indices (0, -1);
2140 if (priv->cell_view)
2141 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2149 priv->width = MAX (priv->width, req.width);
2150 priv->height = MAX (priv->height, req.height);
2152 gtk_tree_path_next (path);
2154 while (gtk_tree_model_iter_next (priv->model, &iter));
2156 gtk_tree_path_free (path);
2160 gtk_combo_box_size_request (GtkWidget *widget,
2161 GtkRequisition *requisition)
2164 gint focus_width, focus_pad;
2167 GtkRequisition bin_req;
2168 PangoContext *context;
2169 PangoFontMetrics *metrics;
2170 PangoFontDescription *font_desc;
2172 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2173 GtkComboBoxPrivate *priv = combo_box->priv;
2176 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2177 gtk_combo_box_remeasure (combo_box);
2178 bin_req.width = MAX (bin_req.width, priv->width);
2179 bin_req.height = MAX (bin_req.height, priv->height);
2181 gtk_widget_style_get (GTK_WIDGET (widget),
2182 "focus-line-width", &focus_width,
2183 "focus-padding", &focus_pad,
2184 "arrow-size", &arrow_size,
2187 font_desc = GTK_BIN (widget)->child->style->font_desc;
2188 context = gtk_widget_get_pango_context (widget);
2189 metrics = pango_context_get_metrics (context, font_desc,
2190 pango_context_get_language (context));
2191 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2192 pango_font_metrics_get_descent (metrics));
2193 pango_font_metrics_unref (metrics);
2195 arrow_size = MAX (arrow_size, font_size);
2197 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2199 if (!priv->tree_view)
2203 if (priv->cell_view)
2205 GtkRequisition button_req, sep_req, arrow_req;
2206 gint border_width, xthickness, ythickness;
2208 gtk_widget_size_request (priv->button, &button_req);
2209 border_width = GTK_CONTAINER (combo_box)->border_width;
2210 xthickness = priv->button->style->xthickness;
2211 ythickness = priv->button->style->ythickness;
2213 bin_req.width = MAX (bin_req.width, priv->width);
2214 bin_req.height = MAX (bin_req.height, priv->height);
2216 gtk_widget_size_request (priv->separator, &sep_req);
2217 gtk_widget_size_request (priv->arrow, &arrow_req);
2219 height = MAX (sep_req.height, arrow_req.height);
2220 height = MAX (height, bin_req.height);
2222 width = bin_req.width + sep_req.width + arrow_req.width;
2224 height += 2*(border_width + ythickness + focus_width + focus_pad);
2225 width += 2*(border_width + xthickness + focus_width + focus_pad);
2227 requisition->width = width;
2228 requisition->height = height;
2232 GtkRequisition but_req;
2234 gtk_widget_size_request (priv->button, &but_req);
2236 requisition->width = bin_req.width + but_req.width;
2237 requisition->height = MAX (bin_req.height, but_req.height);
2243 GtkRequisition button_req, frame_req;
2245 /* sample + frame */
2246 *requisition = bin_req;
2248 requisition->width += 2 * focus_width;
2250 if (priv->cell_view_frame)
2252 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2253 if (priv->has_frame)
2255 requisition->width += 2 *
2256 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2257 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2258 requisition->height += 2 *
2259 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2260 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2265 gtk_widget_size_request (priv->button, &button_req);
2267 requisition->height = MAX (requisition->height, button_req.height);
2268 requisition->width += button_req.width;
2271 if (GTK_SHADOW_NONE != priv->shadow_type)
2273 requisition->height += 2 * widget->style->ythickness;
2274 requisition->width += 2 * widget->style->xthickness;
2278 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2279 gtk_widget_size_request (combo_box->priv->button, &req); \
2282 child.x = allocation->x + shadow_width; \
2284 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2286 child.y = allocation->y + shadow_height; \
2287 child.width = req.width; \
2288 child.height = allocation->height - 2 * shadow_height; \
2289 child.width = MAX (1, child.width); \
2290 child.height = MAX (1, child.height); \
2292 gtk_widget_size_allocate (combo_box->priv->button, &child);
2295 gtk_combo_box_size_allocate (GtkWidget *widget,
2296 GtkAllocation *allocation)
2298 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2299 GtkComboBoxPrivate *priv = combo_box->priv;
2300 gint shadow_width, shadow_height;
2301 gint focus_width, focus_pad;
2302 GtkAllocation child;
2304 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2306 widget->allocation = *allocation;
2308 gtk_widget_style_get (GTK_WIDGET (widget),
2309 "focus-line-width", &focus_width,
2310 "focus-padding", &focus_pad,
2313 if (GTK_SHADOW_NONE != priv->shadow_type)
2315 shadow_width = widget->style->xthickness;
2316 shadow_height = widget->style->ythickness;
2324 if (!priv->tree_view)
2326 if (priv->cell_view)
2328 gint border_width, xthickness, ythickness;
2332 allocation->x += shadow_width;
2333 allocation->y += shadow_height;
2334 allocation->width -= 2 * shadow_width;
2335 allocation->height -= 2 * shadow_height;
2337 gtk_widget_size_allocate (priv->button, allocation);
2339 /* set some things ready */
2340 border_width = GTK_CONTAINER (priv->button)->border_width;
2341 xthickness = priv->button->style->xthickness;
2342 ythickness = priv->button->style->ythickness;
2344 child.x = allocation->x;
2345 child.y = allocation->y;
2346 width = allocation->width;
2347 child.height = allocation->height;
2349 if (!priv->is_cell_renderer)
2351 child.x += border_width + xthickness + focus_width + focus_pad;
2352 child.y += border_width + ythickness + focus_width + focus_pad;
2353 width -= 2 * (child.x - allocation->x);
2354 child.height -= 2 * (child.y - allocation->y);
2358 /* handle the children */
2359 gtk_widget_size_request (priv->arrow, &req);
2360 child.width = req.width;
2362 child.x += width - req.width;
2363 child.width = MAX (1, child.width);
2364 child.height = MAX (1, child.height);
2365 gtk_widget_size_allocate (priv->arrow, &child);
2367 child.x += req.width;
2368 gtk_widget_size_request (priv->separator, &req);
2369 child.width = req.width;
2371 child.x -= req.width;
2372 child.width = MAX (1, child.width);
2373 child.height = MAX (1, child.height);
2374 gtk_widget_size_allocate (priv->separator, &child);
2378 child.x += req.width;
2379 child.width = allocation->x + allocation->width
2380 - (border_width + xthickness + focus_width + focus_pad)
2385 child.width = child.x;
2386 child.x = allocation->x
2387 + border_width + xthickness + focus_width + focus_pad;
2388 child.width -= child.x;
2391 if (gtk_widget_get_visible (priv->popup_widget))
2394 GtkRequisition requisition;
2396 /* Warning here, without the check in the position func */
2397 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2398 if (priv->wrap_width == 0)
2400 width = GTK_WIDGET (combo_box)->allocation.width;
2401 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2402 gtk_widget_size_request (priv->popup_widget, &requisition);
2403 gtk_widget_set_size_request (priv->popup_widget,
2404 MAX (width, requisition.width), -1);
2408 child.width = MAX (1, child.width);
2409 child.height = MAX (1, child.height);
2410 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2414 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2417 child.x = allocation->x + req.width + shadow_width;
2419 child.x = allocation->x + shadow_width;
2420 child.y = allocation->y + shadow_height;
2421 child.width = allocation->width - req.width - 2 * shadow_width;
2422 child.width = MAX (1, child.width);
2423 child.height = MAX (1, child.height);
2424 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2431 /* Combobox thickness + border-width */
2432 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2433 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2436 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2440 child.x = allocation->x + req.width;
2442 child.x = allocation->x;
2444 child.y = allocation->y;
2445 child.width = allocation->width - req.width;
2446 child.height = allocation->height;
2448 if (priv->cell_view_frame)
2452 child.width = MAX (1, child.width - delta_x * 2);
2453 child.height = MAX (1, child.height - delta_y * 2);
2454 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2457 if (priv->has_frame)
2459 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2460 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2461 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2462 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2466 child.width -= delta_x * 2;
2467 child.height -= delta_y * 2;
2474 child.width -= delta_x * 2;
2475 child.height -= delta_y * 2;
2478 if (gtk_widget_get_visible (priv->popup_window))
2480 gint x, y, width, height;
2481 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2482 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2483 gtk_widget_set_size_request (priv->popup_window, width, height);
2487 child.width = MAX (1, child.width);
2488 child.height = MAX (1, child.height);
2490 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2494 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2497 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2499 GtkComboBoxPrivate *priv = combo_box->priv;
2503 g_signal_handler_disconnect (priv->model,
2505 g_signal_handler_disconnect (priv->model,
2507 g_signal_handler_disconnect (priv->model,
2508 priv->reordered_id);
2509 g_signal_handler_disconnect (priv->model,
2514 if (!priv->tree_view)
2516 if (priv->popup_widget)
2517 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2518 (GtkCallback)gtk_widget_destroy, NULL);
2523 g_object_unref (priv->model);
2527 if (priv->active_row)
2529 gtk_tree_row_reference_free (priv->active_row);
2530 priv->active_row = NULL;
2533 if (priv->cell_view)
2534 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2538 gtk_combo_box_forall (GtkContainer *container,
2539 gboolean include_internals,
2540 GtkCallback callback,
2541 gpointer callback_data)
2543 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2544 GtkComboBoxPrivate *priv = combo_box->priv;
2546 if (include_internals)
2549 (* callback) (priv->button, callback_data);
2550 if (priv->cell_view_frame)
2551 (* callback) (priv->cell_view_frame, callback_data);
2554 if (GTK_BIN (container)->child)
2555 (* callback) (GTK_BIN (container)->child, callback_data);
2559 gtk_combo_box_child_show (GtkWidget *widget,
2560 GtkComboBox *combo_box)
2562 GtkComboBoxPrivate *priv = combo_box->priv;
2564 priv->popup_shown = TRUE;
2565 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2569 gtk_combo_box_child_hide (GtkWidget *widget,
2570 GtkComboBox *combo_box)
2572 GtkComboBoxPrivate *priv = combo_box->priv;
2574 priv->popup_shown = FALSE;
2575 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2579 gtk_combo_box_expose_event (GtkWidget *widget,
2580 GdkEventExpose *event)
2582 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2583 GtkComboBoxPrivate *priv = combo_box->priv;
2585 if (gtk_widget_is_drawable (widget) &&
2586 GTK_SHADOW_NONE != priv->shadow_type)
2588 gtk_paint_shadow (widget->style, widget->window,
2589 GTK_STATE_NORMAL, priv->shadow_type,
2590 NULL, widget, "combobox",
2591 widget->allocation.x, widget->allocation.y,
2592 widget->allocation.width, widget->allocation.height);
2595 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2596 priv->button, event);
2598 if (priv->tree_view && priv->cell_view_frame)
2600 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2601 priv->cell_view_frame, event);
2604 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2605 GTK_BIN (widget)->child, event);
2620 path_visible (GtkTreeView *view,
2626 /* Note that we rely on the fact that collapsed rows don't have nodes
2628 return _gtk_tree_view_find_node (view, path, &tree, &node);
2632 tree_next_func (GtkTreeModel *model,
2637 SearchData *search_data = (SearchData *)data;
2639 if (search_data->found)
2641 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2644 if (search_data->visible &&
2645 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2648 search_data->set = TRUE;
2649 search_data->iter = *iter;
2654 if (gtk_tree_path_compare (path, search_data->path) == 0)
2655 search_data->found = TRUE;
2661 tree_next (GtkComboBox *combo,
2662 GtkTreeModel *model,
2667 SearchData search_data;
2669 search_data.combo = combo;
2670 search_data.path = gtk_tree_model_get_path (model, iter);
2671 search_data.visible = visible;
2672 search_data.found = FALSE;
2673 search_data.set = FALSE;
2675 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2677 *next = search_data.iter;
2679 gtk_tree_path_free (search_data.path);
2681 return search_data.set;
2685 tree_prev_func (GtkTreeModel *model,
2690 SearchData *search_data = (SearchData *)data;
2692 if (gtk_tree_path_compare (path, search_data->path) == 0)
2694 search_data->found = TRUE;
2698 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2701 if (search_data->visible &&
2702 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2705 search_data->set = TRUE;
2706 search_data->iter = *iter;
2712 tree_prev (GtkComboBox *combo,
2713 GtkTreeModel *model,
2718 SearchData search_data;
2720 search_data.combo = combo;
2721 search_data.path = gtk_tree_model_get_path (model, iter);
2722 search_data.visible = visible;
2723 search_data.found = FALSE;
2724 search_data.set = FALSE;
2726 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2728 *prev = search_data.iter;
2730 gtk_tree_path_free (search_data.path);
2732 return search_data.set;
2736 tree_last_func (GtkTreeModel *model,
2741 SearchData *search_data = (SearchData *)data;
2743 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2746 /* Note that we rely on the fact that collapsed rows don't have nodes
2748 if (search_data->visible &&
2749 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2752 search_data->set = TRUE;
2753 search_data->iter = *iter;
2759 tree_last (GtkComboBox *combo,
2760 GtkTreeModel *model,
2764 SearchData search_data;
2766 search_data.combo = combo;
2767 search_data.visible = visible;
2768 search_data.set = FALSE;
2770 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2772 *last = search_data.iter;
2774 return search_data.set;
2779 tree_first_func (GtkTreeModel *model,
2784 SearchData *search_data = (SearchData *)data;
2786 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2789 if (search_data->visible &&
2790 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2793 search_data->set = TRUE;
2794 search_data->iter = *iter;
2800 tree_first (GtkComboBox *combo,
2801 GtkTreeModel *model,
2805 SearchData search_data;
2807 search_data.combo = combo;
2808 search_data.visible = visible;
2809 search_data.set = FALSE;
2811 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2813 *first = search_data.iter;
2815 return search_data.set;
2819 gtk_combo_box_scroll_event (GtkWidget *widget,
2820 GdkEventScroll *event)
2822 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2825 GtkTreeIter new_iter;
2827 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2830 if (event->direction == GDK_SCROLL_UP)
2831 found = tree_prev (combo_box, combo_box->priv->model,
2832 &iter, &new_iter, FALSE);
2834 found = tree_next (combo_box, combo_box->priv->model,
2835 &iter, &new_iter, FALSE);
2838 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2848 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2849 GtkCellLayout *cell_layout)
2851 GtkComboBoxPrivate *priv = combo_box->priv;
2854 for (k = priv->cells; k; k = k->next)
2857 ComboCellInfo *info = (ComboCellInfo *)k->data;
2859 if (info->pack == GTK_PACK_START)
2860 gtk_cell_layout_pack_start (cell_layout,
2861 info->cell, info->expand);
2862 else if (info->pack == GTK_PACK_END)
2863 gtk_cell_layout_pack_end (cell_layout,
2864 info->cell, info->expand);
2866 gtk_cell_layout_set_cell_data_func (cell_layout,
2868 combo_cell_data_func, info, NULL);
2870 for (j = info->attributes; j; j = j->next->next)
2872 gtk_cell_layout_add_attribute (cell_layout,
2875 GPOINTER_TO_INT (j->next->data));
2881 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2882 gboolean add_children)
2884 GtkComboBoxPrivate *priv = combo_box->priv;
2887 if (priv->cell_view)
2889 priv->button = gtk_toggle_button_new ();
2890 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2891 priv->focus_on_click);
2893 g_signal_connect (priv->button, "toggled",
2894 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2895 gtk_widget_set_parent (priv->button,
2896 GTK_BIN (combo_box)->child->parent);
2898 priv->box = gtk_hbox_new (FALSE, 0);
2899 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2901 priv->separator = gtk_vseparator_new ();
2902 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2904 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2905 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2907 gtk_widget_show_all (priv->button);
2911 priv->button = gtk_toggle_button_new ();
2912 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2913 priv->focus_on_click);
2915 g_signal_connect (priv->button, "toggled",
2916 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2917 gtk_widget_set_parent (priv->button,
2918 GTK_BIN (combo_box)->child->parent);
2920 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2921 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2922 gtk_widget_show_all (priv->button);
2925 g_signal_connect (priv->button, "button-press-event",
2926 G_CALLBACK (gtk_combo_box_menu_button_press),
2928 g_signal_connect (priv->button, "state-changed",
2929 G_CALLBACK (gtk_combo_box_button_state_changed),
2932 /* create our funky menu */
2933 menu = gtk_menu_new ();
2934 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2935 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2937 g_signal_connect (menu, "key-press-event",
2938 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2939 gtk_combo_box_set_popup_widget (combo_box, menu);
2943 gtk_combo_box_menu_fill (combo_box);
2945 /* the column is needed in tree_column_row_is_sensitive() */
2946 priv->column = gtk_tree_view_column_new ();
2947 g_object_ref_sink (priv->column);
2948 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2950 gtk_combo_box_update_title (combo_box);
2951 gtk_combo_box_update_sensitivity (combo_box);
2955 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2957 GtkComboBoxPrivate *priv = combo_box->priv;
2963 menu = priv->popup_widget;
2965 if (priv->add_tearoffs)
2967 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2969 gtk_widget_show (tearoff);
2971 if (priv->wrap_width)
2972 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2974 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2977 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2981 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2982 GtkTreeModel *model,
2985 GtkWidget *cell_view;
2990 cell_view = gtk_cell_view_new ();
2991 item = gtk_menu_item_new ();
2992 gtk_container_add (GTK_CONTAINER (item), cell_view);
2994 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2995 path = gtk_tree_model_get_path (model, iter);
2996 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2997 gtk_tree_path_free (path);
2999 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
3000 gtk_widget_size_request (cell_view, &req);
3001 gtk_widget_show (cell_view);
3007 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
3009 GtkTreeIter *parent)
3011 GtkComboBoxPrivate *priv = combo_box->priv;
3012 GtkTreeModel *model = priv->model;
3013 GtkWidget *item, *submenu, *subitem, *separator;
3015 gboolean is_separator;
3020 n_children = gtk_tree_model_iter_n_children (model, parent);
3023 for (i = 0; i < n_children; i++)
3025 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
3027 if (priv->row_separator_func)
3028 is_separator = priv->row_separator_func (priv->model, &iter,
3029 priv->row_separator_data);
3031 is_separator = FALSE;
3035 item = gtk_separator_menu_item_new ();
3036 path = gtk_tree_model_get_path (model, &iter);
3037 g_object_set_data_full (G_OBJECT (item),
3038 I_("gtk-combo-box-item-path"),
3039 gtk_tree_row_reference_new (model, path),
3040 (GDestroyNotify)gtk_tree_row_reference_free);
3041 gtk_tree_path_free (path);
3045 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3046 if (gtk_tree_model_iter_has_child (model, &iter))
3048 submenu = gtk_menu_new ();
3049 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
3050 gtk_widget_show (submenu);
3051 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
3053 /* Ugly - since menus can only activate leafs, we have to
3054 * duplicate the item inside the submenu.
3056 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3057 separator = gtk_separator_menu_item_new ();
3058 gtk_widget_show (subitem);
3059 gtk_widget_show (separator);
3060 g_signal_connect (subitem, "activate",
3061 G_CALLBACK (gtk_combo_box_menu_item_activate),
3063 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
3064 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
3066 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
3068 g_signal_connect (item, "activate",
3069 G_CALLBACK (gtk_combo_box_menu_item_activate),
3073 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3074 if (priv->wrap_width && menu == priv->popup_widget)
3075 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3076 gtk_widget_show (item);
3083 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3085 GtkComboBoxPrivate *priv = combo_box->priv;
3087 g_signal_handlers_disconnect_matched (priv->button,
3088 G_SIGNAL_MATCH_DATA,
3090 gtk_combo_box_menu_button_press, NULL);
3091 g_signal_handlers_disconnect_matched (priv->button,
3092 G_SIGNAL_MATCH_DATA,
3094 gtk_combo_box_button_state_changed, combo_box);
3096 /* unparent will remove our latest ref */
3097 gtk_widget_unparent (priv->button);
3100 priv->button = NULL;
3102 priv->separator = NULL;
3104 g_object_unref (priv->column);
3105 priv->column = NULL;
3107 /* changing the popup window will unref the menu and the children */
3115 menu_occupied (GtkMenu *menu,
3119 guint bottom_attach)
3123 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3127 gtk_container_child_get (GTK_CONTAINER (menu),
3131 "bottom-attach", &b,
3135 /* look if this item intersects with the given coordinates */
3136 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3144 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3149 GtkComboBoxPrivate *priv = combo_box->priv;
3150 gint current_col = 0, current_row = 0;
3151 gint rows = 1, cols = 1;
3152 GtkWidget *menu = priv->popup_widget;
3154 if (!GTK_IS_MENU_SHELL (menu))
3157 if (priv->col_column == -1 &&
3158 priv->row_column == -1 &&
3161 gtk_container_child_get (GTK_CONTAINER (menu),
3163 "right-attach", ¤t_col,
3164 "top-attach", ¤t_row,
3166 if (current_col + cols > priv->wrap_width)
3174 if (priv->col_column != -1)
3175 gtk_tree_model_get (priv->model, iter,
3176 priv->col_column, &cols,
3178 if (priv->row_column != -1)
3179 gtk_tree_model_get (priv->model, iter,
3180 priv->row_column, &rows,
3185 if (current_col + cols > priv->wrap_width)
3191 if (!menu_occupied (GTK_MENU (menu),
3192 current_col, current_col + cols,
3193 current_row, current_row + rows))
3200 /* set attach props */
3201 gtk_menu_attach (GTK_MENU (menu), item,
3202 current_col, current_col + cols,
3203 current_row, current_row + rows);
3207 gtk_combo_box_relayout (GtkComboBox *combo_box)
3212 menu = combo_box->priv->popup_widget;
3214 /* do nothing unless we are in menu style and realized */
3215 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3218 list = gtk_container_get_children (GTK_CONTAINER (menu));
3220 for (j = g_list_last (list); j; j = j->prev)
3221 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3223 gtk_combo_box_menu_fill (combo_box);
3230 gtk_combo_box_menu_button_press (GtkWidget *widget,
3231 GdkEventButton *event,
3234 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3235 GtkComboBoxPrivate *priv = combo_box->priv;
3237 if (GTK_IS_MENU (priv->popup_widget) &&
3238 event->type == GDK_BUTTON_PRESS && event->button == 1)
3240 if (priv->focus_on_click &&
3241 !gtk_widget_has_focus (priv->button))
3242 gtk_widget_grab_focus (priv->button);
3244 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3253 gtk_combo_box_menu_item_activate (GtkWidget *item,
3256 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3257 GtkWidget *cell_view;
3261 cell_view = GTK_BIN (item)->child;
3263 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3265 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3267 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3269 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3270 gtk_combo_box_set_active_iter (combo_box, &iter);
3273 gtk_tree_path_free (path);
3275 g_object_set (combo_box,
3276 "editing-canceled", FALSE,
3281 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3284 gboolean sensitive = TRUE; /* fool code checkers */
3286 if (!combo_box->priv->button)
3289 switch (combo_box->priv->button_sensitivity)
3291 case GTK_SENSITIVITY_ON:
3294 case GTK_SENSITIVITY_OFF:
3297 case GTK_SENSITIVITY_AUTO:
3298 sensitive = combo_box->priv->model &&
3299 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3302 g_assert_not_reached ();
3306 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3308 /* In list-mode, we also need to update sensitivity of the event box */
3309 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3310 && combo_box->priv->cell_view)
3311 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3315 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3320 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3322 if (combo_box->priv->tree_view)
3323 gtk_combo_box_list_popup_resize (combo_box);
3325 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3327 gtk_combo_box_update_sensitivity (combo_box);
3331 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3335 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3336 GtkComboBoxPrivate *priv = combo_box->priv;
3338 if (!gtk_tree_row_reference_valid (priv->active_row))
3340 if (priv->cell_view)
3341 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3342 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3345 if (priv->tree_view)
3346 gtk_combo_box_list_popup_resize (combo_box);
3348 gtk_combo_box_menu_row_deleted (model, path, user_data);
3350 gtk_combo_box_update_sensitivity (combo_box);
3354 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3360 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3362 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3364 if (!combo_box->priv->tree_view)
3365 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3369 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3374 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3375 GtkComboBoxPrivate *priv = combo_box->priv;
3376 GtkTreePath *active_path;
3378 /* FIXME this belongs to GtkCellView */
3379 if (gtk_tree_row_reference_valid (priv->active_row))
3381 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3382 if (gtk_tree_path_compare (path, active_path) == 0 &&
3384 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3385 gtk_tree_path_free (active_path);
3388 if (priv->tree_view)
3389 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3391 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3395 list_popup_resize_idle (gpointer user_data)
3397 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3398 GtkComboBoxPrivate *priv = combo_box->priv;
3399 gint x, y, width, height;
3401 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3403 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3405 gtk_widget_set_size_request (priv->popup_window, width, height);
3406 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3409 priv->resize_idle_id = 0;
3415 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3417 GtkComboBoxPrivate *priv = combo_box->priv;
3419 if (!priv->resize_idle_id)
3420 priv->resize_idle_id =
3421 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3425 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3430 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3432 gtk_combo_box_list_popup_resize (combo_box);
3437 find_menu_by_path (GtkWidget *menu,
3439 gboolean skip_first)
3444 GtkTreeRowReference *mref;
3448 list = gtk_container_get_children (GTK_CONTAINER (menu));
3451 for (i = list; i; i = i->next)
3453 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3455 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3458 else if (!gtk_tree_row_reference_valid (mref))
3461 mpath = gtk_tree_row_reference_get_path (mref);
3463 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3471 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3476 /* this case is necessary, since the row reference of
3477 * the cell view may already be updated after a deletion
3484 if (gtk_tree_path_compare (mpath, path) == 0)
3486 gtk_tree_path_free (mpath);
3490 if (gtk_tree_path_is_ancestor (mpath, path))
3492 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3493 if (submenu != NULL)
3495 gtk_tree_path_free (mpath);
3496 item = find_menu_by_path (submenu, path, TRUE);
3500 gtk_tree_path_free (mpath);
3510 dump_menu_tree (GtkWidget *menu,
3517 list = gtk_container_get_children (GTK_CONTAINER (menu));
3518 for (i = list; i; i = i->next)
3520 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3522 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3523 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3524 gtk_tree_path_free (path);
3526 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3527 if (submenu != NULL)
3528 dump_menu_tree (submenu, level + 1);
3537 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3542 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3543 GtkComboBoxPrivate *priv = combo_box->priv;
3545 GtkWidget *item, *menu, *separator;
3549 gboolean is_separator;
3551 if (!priv->popup_widget)
3554 depth = gtk_tree_path_get_depth (path);
3555 pos = gtk_tree_path_get_indices (path, NULL)[depth - 1];
3558 ppath = gtk_tree_path_copy (path);
3559 gtk_tree_path_up (ppath);
3560 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3561 gtk_tree_path_free (ppath);
3563 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3566 menu = gtk_menu_new ();
3567 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3568 gtk_widget_show (menu);
3569 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3571 /* Ugly - since menus can only activate leaves, we have to
3572 * duplicate the item inside the submenu.
3574 gtk_tree_model_iter_parent (model, &piter, iter);
3575 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3576 separator = gtk_separator_menu_item_new ();
3577 g_signal_connect (item, "activate",
3578 G_CALLBACK (gtk_combo_box_menu_item_activate),
3580 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3581 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3582 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3584 gtk_widget_show (item);
3585 gtk_widget_show (separator);
3592 menu = priv->popup_widget;
3593 if (priv->add_tearoffs)
3597 if (priv->row_separator_func)
3598 is_separator = priv->row_separator_func (model, iter,
3599 priv->row_separator_data);
3601 is_separator = FALSE;
3605 item = gtk_separator_menu_item_new ();
3606 g_object_set_data_full (G_OBJECT (item),
3607 I_("gtk-combo-box-item-path"),
3608 gtk_tree_row_reference_new (model, path),
3609 (GDestroyNotify)gtk_tree_row_reference_free);
3613 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3615 g_signal_connect (item, "activate",
3616 G_CALLBACK (gtk_combo_box_menu_item_activate),
3620 gtk_widget_show (item);
3621 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3625 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3629 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3630 GtkComboBoxPrivate *priv = combo_box->priv;
3634 if (!priv->popup_widget)
3637 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3638 menu = gtk_widget_get_parent (item);
3639 gtk_container_remove (GTK_CONTAINER (menu), item);
3641 if (gtk_tree_path_get_depth (path) > 1)
3643 GtkTreePath *parent_path;
3647 parent_path = gtk_tree_path_copy (path);
3648 gtk_tree_path_up (parent_path);
3649 gtk_tree_model_get_iter (model, &iter, parent_path);
3651 if (!gtk_tree_model_iter_has_child (model, &iter))
3653 parent = find_menu_by_path (priv->popup_widget,
3654 parent_path, FALSE);
3655 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3661 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3667 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3669 gtk_combo_box_relayout (combo_box);
3673 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3678 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3679 GtkComboBoxPrivate *priv = combo_box->priv;
3682 gboolean is_separator;
3684 if (!priv->popup_widget)
3687 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3689 if (priv->row_separator_func)
3690 is_separator = priv->row_separator_func (model, iter,
3691 priv->row_separator_data);
3693 is_separator = FALSE;
3695 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3697 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3698 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3701 if (priv->wrap_width && item->parent == priv->popup_widget)
3703 GtkWidget *pitem = NULL;
3706 prev = gtk_tree_path_copy (path);
3708 if (gtk_tree_path_prev (prev))
3709 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3711 gtk_tree_path_free (prev);
3713 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3715 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3720 "bottom-attach", -1,
3723 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3726 width = gtk_combo_box_calc_requested_width (combo_box, path);
3728 if (width > priv->width)
3730 if (priv->cell_view)
3732 gtk_widget_set_size_request (priv->cell_view, width, -1);
3733 gtk_widget_queue_resize (priv->cell_view);
3735 priv->width = width;
3744 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3746 GtkComboBoxPrivate *priv = combo_box->priv;
3747 GtkTreeSelection *sel;
3749 GtkWidget *widget = GTK_WIDGET (combo_box);
3751 priv->button = gtk_toggle_button_new ();
3752 gtk_widget_set_parent (priv->button,
3753 GTK_BIN (combo_box)->child->parent);
3754 g_signal_connect (priv->button, "button-press-event",
3755 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3756 g_signal_connect (priv->button, "toggled",
3757 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3759 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3760 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3761 priv->separator = NULL;
3762 gtk_widget_show_all (priv->button);
3764 if (priv->cell_view)
3766 style = gtk_widget_get_style (widget);
3767 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3768 &style->base[gtk_widget_get_state (widget)]);
3770 priv->box = gtk_event_box_new ();
3771 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3774 if (priv->has_frame)
3776 priv->cell_view_frame = gtk_frame_new (NULL);
3777 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3782 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3783 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3787 gtk_widget_set_parent (priv->cell_view_frame,
3788 GTK_BIN (combo_box)->child->parent);
3789 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3790 gtk_widget_show_all (priv->cell_view_frame);
3792 g_signal_connect (priv->box, "button-press-event",
3793 G_CALLBACK (gtk_combo_box_list_button_pressed),
3797 priv->tree_view = gtk_tree_view_new ();
3798 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3799 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3800 gtk_tree_selection_set_select_function (sel,
3801 gtk_combo_box_list_select_func,
3803 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3805 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3807 if (priv->row_separator_func)
3808 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3809 priv->row_separator_func,
3810 priv->row_separator_data,
3813 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3815 priv->column = gtk_tree_view_column_new ();
3816 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3819 gtk_combo_box_sync_cells (combo_box,
3820 GTK_CELL_LAYOUT (priv->column));
3822 if (gtk_tree_row_reference_valid (priv->active_row))
3826 path = gtk_tree_row_reference_get_path (priv->active_row);
3827 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3829 gtk_tree_path_free (path);
3832 /* set sample/popup widgets */
3833 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3835 g_signal_connect (priv->tree_view, "key-press-event",
3836 G_CALLBACK (gtk_combo_box_list_key_press),
3838 g_signal_connect (priv->tree_view, "enter-notify-event",
3839 G_CALLBACK (gtk_combo_box_list_enter_notify),
3841 g_signal_connect (priv->tree_view, "row-expanded",
3842 G_CALLBACK (gtk_combo_box_model_row_expanded),
3844 g_signal_connect (priv->tree_view, "row-collapsed",
3845 G_CALLBACK (gtk_combo_box_model_row_expanded),
3847 g_signal_connect (priv->popup_window, "button-press-event",
3848 G_CALLBACK (gtk_combo_box_list_button_pressed),
3850 g_signal_connect (priv->popup_window, "button-release-event",
3851 G_CALLBACK (gtk_combo_box_list_button_released),
3854 gtk_widget_show (priv->tree_view);
3856 gtk_combo_box_update_sensitivity (combo_box);
3860 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3862 GtkComboBoxPrivate *priv = combo_box->priv;
3864 /* disconnect signals */
3865 g_signal_handlers_disconnect_matched (priv->tree_view,
3866 G_SIGNAL_MATCH_DATA,
3867 0, 0, NULL, NULL, combo_box);
3868 g_signal_handlers_disconnect_matched (priv->button,
3869 G_SIGNAL_MATCH_DATA,
3871 gtk_combo_box_list_button_pressed,
3873 g_signal_handlers_disconnect_matched (priv->popup_window,
3874 G_SIGNAL_MATCH_DATA,
3876 gtk_combo_box_list_button_pressed,
3878 g_signal_handlers_disconnect_matched (priv->popup_window,
3879 G_SIGNAL_MATCH_DATA,
3881 gtk_combo_box_list_button_released,
3884 g_signal_handlers_disconnect_matched (priv->popup_window,
3885 G_SIGNAL_MATCH_DATA,
3887 gtk_combo_box_child_show,
3890 g_signal_handlers_disconnect_matched (priv->popup_window,
3891 G_SIGNAL_MATCH_DATA,
3893 gtk_combo_box_child_hide,
3897 g_signal_handlers_disconnect_matched (priv->box,
3898 G_SIGNAL_MATCH_DATA,
3900 gtk_combo_box_list_button_pressed,
3903 /* destroy things (unparent will kill the latest ref from us)
3904 * last unref on button will destroy the arrow
3906 gtk_widget_unparent (priv->button);
3907 priv->button = NULL;
3910 if (priv->cell_view)
3912 g_object_set (priv->cell_view,
3913 "background-set", FALSE,
3917 if (priv->cell_view_frame)
3919 gtk_widget_unparent (priv->cell_view_frame);
3920 priv->cell_view_frame = NULL;
3924 if (priv->scroll_timer)
3926 g_source_remove (priv->scroll_timer);
3927 priv->scroll_timer = 0;
3930 if (priv->resize_idle_id)
3932 g_source_remove (priv->resize_idle_id);
3933 priv->resize_idle_id = 0;
3936 gtk_widget_destroy (priv->tree_view);
3938 priv->tree_view = NULL;
3939 if (priv->popup_widget)
3941 g_object_unref (priv->popup_widget);
3942 priv->popup_widget = NULL;
3949 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3950 GdkEventButton *event,
3953 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3954 GtkComboBoxPrivate *priv = combo_box->priv;
3956 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3958 if (ewidget == priv->popup_window)
3961 if ((ewidget != priv->button && ewidget != priv->box) ||
3962 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3965 if (priv->focus_on_click &&
3966 !gtk_widget_has_focus (priv->button))
3967 gtk_widget_grab_focus (priv->button);
3969 gtk_combo_box_popup_for_device (combo_box, event->device);
3971 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3973 priv->auto_scroll = FALSE;
3974 if (priv->scroll_timer == 0)
3975 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3976 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3979 priv->popup_in_progress = TRUE;
3985 gtk_combo_box_list_button_released (GtkWidget *widget,
3986 GdkEventButton *event,
3990 GtkTreePath *path = NULL;
3993 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3994 GtkComboBoxPrivate *priv = combo_box->priv;
3996 gboolean popup_in_progress = FALSE;
3998 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
4000 if (priv->popup_in_progress)
4002 popup_in_progress = TRUE;
4003 priv->popup_in_progress = FALSE;
4006 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
4008 if (priv->scroll_timer)
4010 g_source_remove (priv->scroll_timer);
4011 priv->scroll_timer = 0;
4014 if (ewidget != priv->tree_view)
4016 if ((ewidget == priv->button ||
4017 ewidget == priv->box) &&
4018 !popup_in_progress &&
4019 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4021 gtk_combo_box_popdown (combo_box);
4025 /* released outside treeview */
4026 if (ewidget != priv->button && ewidget != priv->box)
4028 gtk_combo_box_popdown (combo_box);
4036 /* select something cool */
4037 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
4043 return TRUE; /* clicked outside window? */
4045 gtk_tree_model_get_iter (priv->model, &iter, path);
4046 gtk_tree_path_free (path);
4048 gtk_combo_box_popdown (combo_box);
4050 if (tree_column_row_is_sensitive (combo_box, &iter))
4051 gtk_combo_box_set_active_iter (combo_box, &iter);
4057 gtk_combo_box_menu_key_press (GtkWidget *widget,
4061 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4063 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4065 /* The menu hasn't managed the
4066 * event, forward it to the combobox
4068 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4075 gtk_combo_box_list_key_press (GtkWidget *widget,
4079 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4082 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
4083 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
4085 GtkTreeModel *model = NULL;
4087 gtk_combo_box_popdown (combo_box);
4089 if (combo_box->priv->model)
4091 GtkTreeSelection *sel;
4093 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4095 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4096 gtk_combo_box_set_active_iter (combo_box, &iter);
4102 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4104 /* The list hasn't managed the
4105 * event, forward it to the combobox
4107 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4114 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4118 GtkWidget *tree_view = combo_box->priv->tree_view;
4122 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4123 if (adj && adj->upper - adj->lower > adj->page_size)
4125 if (x <= tree_view->allocation.x &&
4126 adj->lower < adj->value)
4128 value = adj->value - (tree_view->allocation.x - x + 1);
4129 gtk_adjustment_set_value (adj, value);
4131 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4132 adj->upper - adj->page_size > adj->value)
4134 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4135 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4139 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4140 if (adj && adj->upper - adj->lower > adj->page_size)
4142 if (y <= tree_view->allocation.y &&
4143 adj->lower < adj->value)
4145 value = adj->value - (tree_view->allocation.y - y + 1);
4146 gtk_adjustment_set_value (adj, value);
4148 else if (y >= tree_view->allocation.height &&
4149 adj->upper - adj->page_size > adj->value)
4151 value = adj->value + (y - tree_view->allocation.height + 1);
4152 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4158 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4160 GtkComboBoxPrivate *priv = combo_box->priv;
4163 if (priv->auto_scroll)
4165 gdk_window_get_device_position (priv->tree_view->window,
4168 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4175 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4176 GdkEventCrossing *event,
4179 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4181 combo_box->priv->auto_scroll = TRUE;
4187 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4188 GtkTreeModel *model,
4190 gboolean path_currently_selected,
4194 gboolean sensitive = FALSE;
4196 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4198 GList *cells, *cell;
4199 gboolean cell_sensitive, cell_visible;
4201 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4203 if (!column->visible)
4206 gtk_tree_model_get_iter (model, &iter, path);
4207 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4210 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4213 g_object_get (cell->data,
4214 "sensitive", &cell_sensitive,
4215 "visible", &cell_visible,
4218 if (cell_visible && cell_sensitive)
4223 g_list_free (cells);
4225 sensitive = cell_sensitive;
4232 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4237 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4238 GtkComboBoxPrivate *priv = combo_box->priv;
4241 width = gtk_combo_box_calc_requested_width (combo_box, path);
4243 if (width > priv->width)
4245 if (priv->cell_view)
4247 gtk_widget_set_size_request (priv->cell_view, width, -1);
4248 gtk_widget_queue_resize (priv->cell_view);
4250 priv->width = width;
4255 * GtkCellLayout implementation
4259 pack_start_recurse (GtkWidget *menu,
4260 GtkCellRenderer *cell,
4266 list = gtk_container_get_children (GTK_CONTAINER (menu));
4267 for (i = list; i; i = i->next)
4269 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4270 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4273 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4274 if (submenu != NULL)
4275 pack_start_recurse (submenu, cell, expand);
4282 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4283 GtkCellRenderer *cell,
4286 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4287 ComboCellInfo *info;
4288 GtkComboBoxPrivate *priv;
4290 priv = combo_box->priv;
4292 g_object_ref_sink (cell);
4294 info = g_slice_new0 (ComboCellInfo);
4296 info->expand = expand;
4297 info->pack = GTK_PACK_START;
4299 priv->cells = g_slist_append (priv->cells, info);
4301 if (priv->cell_view)
4302 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4306 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4308 if (GTK_IS_MENU (priv->popup_widget))
4309 pack_start_recurse (priv->popup_widget, cell, expand);
4313 pack_end_recurse (GtkWidget *menu,
4314 GtkCellRenderer *cell,
4320 list = gtk_container_get_children (GTK_CONTAINER (menu));
4321 for (i = list; i; i = i->next)
4323 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4324 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4327 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4328 if (submenu != NULL)
4329 pack_end_recurse (submenu, cell, expand);
4336 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4337 GtkCellRenderer *cell,
4340 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4341 ComboCellInfo *info;
4342 GtkComboBoxPrivate *priv;
4344 priv = combo_box->priv;
4346 g_object_ref_sink (cell);
4348 info = g_slice_new0 (ComboCellInfo);
4350 info->expand = expand;
4351 info->pack = GTK_PACK_END;
4353 priv->cells = g_slist_append (priv->cells, info);
4355 if (priv->cell_view)
4356 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4360 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4362 if (GTK_IS_MENU (priv->popup_widget))
4363 pack_end_recurse (priv->popup_widget, cell, expand);
4367 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4369 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4371 GList *retval = NULL;
4373 for (list = combo_box->priv->cells; list; list = list->next)
4375 ComboCellInfo *info = (ComboCellInfo *)list->data;
4377 retval = g_list_prepend (retval, info->cell);
4380 return g_list_reverse (retval);
4384 clear_recurse (GtkWidget *menu)
4389 list = gtk_container_get_children (GTK_CONTAINER (menu));
4390 for (i = list; i; i = i->next)
4392 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4393 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4395 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4396 if (submenu != NULL)
4397 clear_recurse (submenu);
4404 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4406 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4407 GtkComboBoxPrivate *priv = combo_box->priv;
4410 if (priv->cell_view)
4411 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4414 gtk_tree_view_column_clear (priv->column);
4416 for (i = priv->cells; i; i = i->next)
4418 ComboCellInfo *info = (ComboCellInfo *)i->data;
4420 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4421 g_object_unref (info->cell);
4422 g_slice_free (ComboCellInfo, info);
4425 g_slist_free (priv->cells);
4428 if (GTK_IS_MENU (priv->popup_widget))
4429 clear_recurse (priv->popup_widget);
4433 add_attribute_recurse (GtkWidget *menu,
4434 GtkCellRenderer *cell,
4435 const gchar *attribute,
4441 list = gtk_container_get_children (GTK_CONTAINER (menu));
4442 for (i = list; i; i = i->next)
4444 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4445 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4446 cell, attribute, column);
4448 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4449 if (submenu != NULL)
4450 add_attribute_recurse (submenu, cell, attribute, column);
4457 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4458 GtkCellRenderer *cell,
4459 const gchar *attribute,
4462 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4463 ComboCellInfo *info;
4465 info = gtk_combo_box_get_cell_info (combo_box, cell);
4466 g_return_if_fail (info != NULL);
4468 info->attributes = g_slist_prepend (info->attributes,
4469 GINT_TO_POINTER (column));
4470 info->attributes = g_slist_prepend (info->attributes,
4471 g_strdup (attribute));
4473 if (combo_box->priv->cell_view)
4474 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4475 cell, attribute, column);
4477 if (combo_box->priv->column)
4478 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4479 cell, attribute, column);
4481 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4482 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4483 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4487 combo_cell_data_func (GtkCellLayout *cell_layout,
4488 GtkCellRenderer *cell,
4489 GtkTreeModel *tree_model,
4493 ComboCellInfo *info = (ComboCellInfo *)data;
4494 GtkWidget *parent = NULL;
4499 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4501 if (GTK_IS_WIDGET (cell_layout))
4502 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4504 if (GTK_IS_MENU_ITEM (parent) &&
4505 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4506 g_object_set (cell, "sensitive", TRUE, NULL);
4511 set_cell_data_func_recurse (GtkWidget *menu,
4512 GtkCellRenderer *cell,
4513 ComboCellInfo *info)
4517 GtkWidget *cell_view;
4519 list = gtk_container_get_children (GTK_CONTAINER (menu));
4520 for (i = list; i; i = i->next)
4522 cell_view = GTK_BIN (i->data)->child;
4523 if (GTK_IS_CELL_LAYOUT (cell_view))
4525 /* Override sensitivity for inner nodes; we don't
4526 * want menuitems with submenus to appear insensitive
4528 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4530 combo_cell_data_func,
4532 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4533 if (submenu != NULL)
4534 set_cell_data_func_recurse (submenu, cell, info);
4542 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4543 GtkCellRenderer *cell,
4544 GtkCellLayoutDataFunc func,
4546 GDestroyNotify destroy)
4548 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4549 GtkComboBoxPrivate *priv = combo_box->priv;
4550 ComboCellInfo *info;
4552 info = gtk_combo_box_get_cell_info (combo_box, cell);
4553 g_return_if_fail (info != NULL);
4557 GDestroyNotify d = info->destroy;
4559 info->destroy = NULL;
4560 d (info->func_data);
4564 info->func_data = func_data;
4565 info->destroy = destroy;
4567 if (priv->cell_view)
4568 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4571 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4573 if (GTK_IS_MENU (priv->popup_widget))
4574 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4576 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4580 clear_attributes_recurse (GtkWidget *menu,
4581 GtkCellRenderer *cell)
4586 list = gtk_container_get_children (GTK_CONTAINER (menu));
4587 for (i = list; i; i = i->next)
4589 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4590 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4593 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4594 if (submenu != NULL)
4595 clear_attributes_recurse (submenu, cell);
4602 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4603 GtkCellRenderer *cell)
4605 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4606 GtkComboBoxPrivate *priv;
4607 ComboCellInfo *info;
4610 priv = combo_box->priv;
4612 info = gtk_combo_box_get_cell_info (combo_box, cell);
4613 g_return_if_fail (info != NULL);
4615 list = info->attributes;
4616 while (list && list->next)
4618 g_free (list->data);
4619 list = list->next->next;
4621 g_slist_free (info->attributes);
4622 info->attributes = NULL;
4624 if (priv->cell_view)
4625 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4628 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4630 if (GTK_IS_MENU (priv->popup_widget))
4631 clear_attributes_recurse (priv->popup_widget, cell);
4633 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4637 reorder_recurse (GtkWidget *menu,
4638 GtkCellRenderer *cell,
4644 list = gtk_container_get_children (GTK_CONTAINER (menu));
4645 for (i = list; i; i = i->next)
4647 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4648 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4651 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4652 if (submenu != NULL)
4653 reorder_recurse (submenu, cell, position);
4660 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4661 GtkCellRenderer *cell,
4664 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4665 GtkComboBoxPrivate *priv;
4666 ComboCellInfo *info;
4669 priv = combo_box->priv;
4671 info = gtk_combo_box_get_cell_info (combo_box, cell);
4673 g_return_if_fail (info != NULL);
4674 g_return_if_fail (position >= 0);
4676 link = g_slist_find (priv->cells, info);
4678 g_return_if_fail (link != NULL);
4680 priv->cells = g_slist_delete_link (priv->cells, link);
4681 priv->cells = g_slist_insert (priv->cells, info, position);
4683 if (priv->cell_view)
4684 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4688 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4691 if (GTK_IS_MENU (priv->popup_widget))
4692 reorder_recurse (priv->popup_widget, cell, position);
4694 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4702 * gtk_combo_box_new:
4704 * Creates a new empty #GtkComboBox.
4706 * Return value: A new #GtkComboBox.
4711 gtk_combo_box_new (void)
4713 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4717 * gtk_combo_box_new_with_model:
4718 * @model: A #GtkTreeModel.
4720 * Creates a new #GtkComboBox with the model initialized to @model.
4722 * Return value: A new #GtkComboBox.
4727 gtk_combo_box_new_with_model (GtkTreeModel *model)
4729 GtkComboBox *combo_box;
4731 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4733 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4735 return GTK_WIDGET (combo_box);
4739 * gtk_combo_box_get_wrap_width:
4740 * @combo_box: A #GtkComboBox
4742 * Returns the wrap width which is used to determine the number of columns
4743 * for the popup menu. If the wrap width is larger than 1, the combo box
4746 * Returns: the wrap width.
4751 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4753 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4755 return combo_box->priv->wrap_width;
4759 * gtk_combo_box_set_wrap_width:
4760 * @combo_box: A #GtkComboBox
4761 * @width: Preferred number of columns
4763 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4764 * the preferred number of columns when you want the popup to be layed out
4770 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4773 GtkComboBoxPrivate *priv;
4775 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4776 g_return_if_fail (width >= 0);
4778 priv = combo_box->priv;
4780 if (width != priv->wrap_width)
4782 priv->wrap_width = width;
4784 gtk_combo_box_check_appearance (combo_box);
4785 gtk_combo_box_relayout (combo_box);
4787 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4792 * gtk_combo_box_get_row_span_column:
4793 * @combo_box: A #GtkComboBox
4795 * Returns the column with row span information for @combo_box.
4797 * Returns: the row span column.
4802 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4804 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4806 return combo_box->priv->row_column;
4810 * gtk_combo_box_set_row_span_column:
4811 * @combo_box: A #GtkComboBox.
4812 * @row_span: A column in the model passed during construction.
4814 * Sets the column with row span information for @combo_box to be @row_span.
4815 * The row span column contains integers which indicate how many rows
4816 * an item should span.
4821 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4824 GtkComboBoxPrivate *priv;
4827 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4829 priv = combo_box->priv;
4831 col = gtk_tree_model_get_n_columns (priv->model);
4832 g_return_if_fail (row_span >= -1 && row_span < col);
4834 if (row_span != priv->row_column)
4836 priv->row_column = row_span;
4838 gtk_combo_box_relayout (combo_box);
4840 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4845 * gtk_combo_box_get_column_span_column:
4846 * @combo_box: A #GtkComboBox
4848 * Returns the column with column span information for @combo_box.
4850 * Returns: the column span column.
4855 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4857 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4859 return combo_box->priv->col_column;
4863 * gtk_combo_box_set_column_span_column:
4864 * @combo_box: A #GtkComboBox
4865 * @column_span: A column in the model passed during construction
4867 * Sets the column with column span information for @combo_box to be
4868 * @column_span. The column span column contains integers which indicate
4869 * how many columns an item should span.
4874 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4877 GtkComboBoxPrivate *priv;
4880 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4882 priv = combo_box->priv;
4884 col = gtk_tree_model_get_n_columns (priv->model);
4885 g_return_if_fail (column_span >= -1 && column_span < col);
4887 if (column_span != priv->col_column)
4889 priv->col_column = column_span;
4891 gtk_combo_box_relayout (combo_box);
4893 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4898 * gtk_combo_box_get_active:
4899 * @combo_box: A #GtkComboBox
4901 * Returns the index of the currently active item, or -1 if there's no
4902 * active item. If the model is a non-flat treemodel, and the active item
4903 * is not an immediate child of the root of the tree, this function returns
4904 * <literal>gtk_tree_path_get_indices (path, NULL)[0]</literal>, where
4905 * <literal>path</literal> is the #GtkTreePath of the active item.
4907 * Return value: An integer which is the index of the currently active item,
4908 * or -1 if there's no active item.
4913 gtk_combo_box_get_active (GtkComboBox *combo_box)
4915 GtkComboBoxPrivate *priv;
4918 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4920 priv = combo_box->priv;
4922 if (gtk_tree_row_reference_valid (priv->active_row))
4926 path = gtk_tree_row_reference_get_path (priv->active_row);
4927 result = gtk_tree_path_get_indices (path, NULL)[0];
4928 gtk_tree_path_free (path);
4937 * gtk_combo_box_set_active:
4938 * @combo_box: A #GtkComboBox
4939 * @index_: An index in the model passed during construction, or -1 to have
4942 * Sets the active item of @combo_box to be the item at @index.
4947 gtk_combo_box_set_active (GtkComboBox *combo_box,
4950 GtkTreePath *path = NULL;
4951 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4952 g_return_if_fail (index_ >= -1);
4954 if (combo_box->priv->model == NULL)
4956 /* Save index, in case the model is set after the index */
4957 combo_box->priv->active = index_;
4963 path = gtk_tree_path_new_from_indices (index_, -1);
4965 gtk_combo_box_set_active_internal (combo_box, path);
4968 gtk_tree_path_free (path);
4972 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4975 GtkComboBoxPrivate *priv = combo_box->priv;
4976 GtkTreePath *active_path;
4979 /* Remember whether the initially active row is valid. */
4980 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4982 if (path && is_valid_row_reference)
4984 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4985 path_cmp = gtk_tree_path_compare (path, active_path);
4986 gtk_tree_path_free (active_path);
4991 if (priv->active_row)
4993 gtk_tree_row_reference_free (priv->active_row);
4994 priv->active_row = NULL;
4999 if (priv->tree_view)
5000 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
5003 GtkMenu *menu = GTK_MENU (priv->popup_widget);
5005 if (GTK_IS_MENU (menu))
5006 gtk_menu_set_active (menu, -1);
5009 if (priv->cell_view)
5010 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
5013 * Do not emit a "changed" signal when an already invalid selection was
5014 * now set to invalid.
5016 if (!is_valid_row_reference)
5022 gtk_tree_row_reference_new (priv->model, path);
5024 if (priv->tree_view)
5026 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
5029 else if (GTK_IS_MENU (priv->popup_widget))
5031 /* FIXME handle nested menus better */
5032 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
5033 gtk_tree_path_get_indices (path, NULL)[0]);
5036 if (priv->cell_view)
5037 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
5041 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
5042 g_object_notify (G_OBJECT (combo_box), "active");
5047 * gtk_combo_box_get_active_iter:
5048 * @combo_box: A #GtkComboBox
5049 * @iter: The uninitialized #GtkTreeIter
5051 * Sets @iter to point to the current active item, if it exists.
5053 * Return value: %TRUE, if @iter was set
5058 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
5064 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5066 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
5069 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
5070 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
5071 gtk_tree_path_free (path);
5077 * gtk_combo_box_set_active_iter:
5078 * @combo_box: A #GtkComboBox
5079 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5081 * Sets the current active item to be the one referenced by @iter, or
5082 * unsets the active item if @iter is %NULL.
5084 * @iter must correspond to a path of depth one, or be %NULL.
5089 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5092 GtkTreePath *path = NULL;
5094 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5097 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5099 gtk_combo_box_set_active_internal (combo_box, path);
5100 gtk_tree_path_free (path);
5104 * gtk_combo_box_set_model:
5105 * @combo_box: A #GtkComboBox
5106 * @model: (allow-none): A #GtkTreeModel
5108 * Sets the model used by @combo_box to be @model. Will unset a previously set
5109 * model (if applicable). If model is %NULL, then it will unset the model.
5111 * Note that this function does not clear the cell renderers, you have to
5112 * call gtk_cell_layout_clear() yourself if you need to set up different
5113 * cell renderers for the new model.
5118 gtk_combo_box_set_model (GtkComboBox *combo_box,
5119 GtkTreeModel *model)
5121 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5122 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5124 if (model == combo_box->priv->model)
5127 gtk_combo_box_unset_model (combo_box);
5132 combo_box->priv->model = model;
5133 g_object_ref (combo_box->priv->model);
5135 combo_box->priv->inserted_id =
5136 g_signal_connect (combo_box->priv->model, "row-inserted",
5137 G_CALLBACK (gtk_combo_box_model_row_inserted),
5139 combo_box->priv->deleted_id =
5140 g_signal_connect (combo_box->priv->model, "row-deleted",
5141 G_CALLBACK (gtk_combo_box_model_row_deleted),
5143 combo_box->priv->reordered_id =
5144 g_signal_connect (combo_box->priv->model, "rows-reordered",
5145 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5147 combo_box->priv->changed_id =
5148 g_signal_connect (combo_box->priv->model, "row-changed",
5149 G_CALLBACK (gtk_combo_box_model_row_changed),
5152 if (combo_box->priv->tree_view)
5155 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5156 combo_box->priv->model);
5157 gtk_combo_box_list_popup_resize (combo_box);
5162 if (combo_box->priv->popup_widget)
5163 gtk_combo_box_menu_fill (combo_box);
5167 if (combo_box->priv->cell_view)
5168 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5169 combo_box->priv->model);
5171 if (combo_box->priv->active != -1)
5173 /* If an index was set in advance, apply it now */
5174 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5175 combo_box->priv->active = -1;
5179 gtk_combo_box_update_sensitivity (combo_box);
5181 g_object_notify (G_OBJECT (combo_box), "model");
5185 * gtk_combo_box_get_model
5186 * @combo_box: A #GtkComboBox
5188 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5190 * Return value: (transfer none): A #GtkTreeModel which was passed during construction.
5195 gtk_combo_box_get_model (GtkComboBox *combo_box)
5197 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5199 return combo_box->priv->model;
5203 /* convenience API for simple text combos */
5206 * gtk_combo_box_new_text:
5208 * Convenience function which constructs a new text combo box, which is a
5209 * #GtkComboBox just displaying strings. If you use this function to create
5210 * a text combo box, you should only manipulate its data source with the
5211 * following convenience functions: gtk_combo_box_append_text(),
5212 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5213 * gtk_combo_box_remove_text().
5215 * Return value: (transfer none): A new text combo box.
5220 gtk_combo_box_new_text (void)
5222 GtkWidget *combo_box;
5223 GtkCellRenderer *cell;
5224 GtkListStore *store;
5226 store = gtk_list_store_new (1, G_TYPE_STRING);
5227 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5228 g_object_unref (store);
5230 cell = gtk_cell_renderer_text_new ();
5231 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5232 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5240 * gtk_combo_box_append_text:
5241 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5244 * Appends @string to the list of strings stored in @combo_box. Note that
5245 * you can only use this function with combo boxes constructed with
5246 * gtk_combo_box_new_text().
5251 gtk_combo_box_append_text (GtkComboBox *combo_box,
5255 GtkListStore *store;
5257 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5258 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5259 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5261 g_return_if_fail (text != NULL);
5263 store = GTK_LIST_STORE (combo_box->priv->model);
5265 gtk_list_store_append (store, &iter);
5266 gtk_list_store_set (store, &iter, 0, text, -1);
5270 * gtk_combo_box_insert_text:
5271 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5272 * @position: An index to insert @text
5275 * Inserts @string at @position in the list of strings stored in @combo_box.
5276 * Note that you can only use this function with combo boxes constructed
5277 * with gtk_combo_box_new_text().
5282 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5287 GtkListStore *store;
5289 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5290 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5291 g_return_if_fail (position >= 0);
5292 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5294 g_return_if_fail (text != NULL);
5296 store = GTK_LIST_STORE (combo_box->priv->model);
5298 gtk_list_store_insert (store, &iter, position);
5299 gtk_list_store_set (store, &iter, 0, text, -1);
5303 * gtk_combo_box_prepend_text:
5304 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5307 * Prepends @string to the list of strings stored in @combo_box. Note that
5308 * you can only use this function with combo boxes constructed with
5309 * gtk_combo_box_new_text().
5314 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5318 GtkListStore *store;
5320 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5321 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5322 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5324 g_return_if_fail (text != NULL);
5326 store = GTK_LIST_STORE (combo_box->priv->model);
5328 gtk_list_store_prepend (store, &iter);
5329 gtk_list_store_set (store, &iter, 0, text, -1);
5333 * gtk_combo_box_remove_text:
5334 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5335 * @position: Index of the item to remove
5337 * Removes the string at @position from @combo_box. Note that you can only use
5338 * this function with combo boxes constructed with gtk_combo_box_new_text().
5343 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5347 GtkListStore *store;
5349 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5350 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5351 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5353 g_return_if_fail (position >= 0);
5355 store = GTK_LIST_STORE (combo_box->priv->model);
5357 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5359 gtk_list_store_remove (store, &iter);
5363 * gtk_combo_box_get_active_text:
5364 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5366 * Returns the currently active string in @combo_box or %NULL if none
5367 * is selected. Note that you can only use this function with combo
5368 * boxes constructed with gtk_combo_box_new_text() and with
5369 * #GtkComboBoxEntry<!-- -->s.
5371 * Returns: a newly allocated string containing the currently active text.
5372 * Must be freed with g_free().
5377 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5379 GtkComboBoxClass *class;
5381 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5383 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5385 if (class->get_active_text)
5386 return class->get_active_text (combo_box);
5392 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5397 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5398 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5399 == G_TYPE_STRING, NULL);
5401 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5402 gtk_tree_model_get (combo_box->priv->model, &iter,
5409 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5410 GtkScrollType scroll)
5413 GtkTreeIter new_iter;
5414 gboolean active_iter;
5417 if (!combo_box->priv->model)
5419 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5423 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5427 case GTK_SCROLL_STEP_BACKWARD:
5428 case GTK_SCROLL_STEP_UP:
5429 case GTK_SCROLL_STEP_LEFT:
5432 found = tree_prev (combo_box, combo_box->priv->model,
5433 &iter, &new_iter, FALSE);
5436 /* else fall through */
5438 case GTK_SCROLL_PAGE_FORWARD:
5439 case GTK_SCROLL_PAGE_DOWN:
5440 case GTK_SCROLL_PAGE_RIGHT:
5441 case GTK_SCROLL_END:
5442 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5445 case GTK_SCROLL_STEP_FORWARD:
5446 case GTK_SCROLL_STEP_DOWN:
5447 case GTK_SCROLL_STEP_RIGHT:
5450 found = tree_next (combo_box, combo_box->priv->model,
5451 &iter, &new_iter, FALSE);
5454 /* else fall through */
5456 case GTK_SCROLL_PAGE_BACKWARD:
5457 case GTK_SCROLL_PAGE_UP:
5458 case GTK_SCROLL_PAGE_LEFT:
5459 case GTK_SCROLL_START:
5460 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5467 if (found && active_iter)
5469 GtkTreePath *old_path;
5470 GtkTreePath *new_path;
5472 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5473 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5475 if (gtk_tree_path_compare (old_path, new_path) == 0)
5478 gtk_tree_path_free (old_path);
5479 gtk_tree_path_free (new_path);
5484 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5488 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5493 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5494 gboolean group_cycling)
5496 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5498 gtk_widget_grab_focus (combo_box->priv->button);
5504 gtk_combo_box_grab_focus (GtkWidget *widget)
5506 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5508 gtk_widget_grab_focus (combo_box->priv->button);
5512 gtk_combo_box_destroy (GtkObject *object)
5514 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5516 if (combo_box->priv->popup_idle_id > 0)
5518 g_source_remove (combo_box->priv->popup_idle_id);
5519 combo_box->priv->popup_idle_id = 0;
5522 gtk_combo_box_popdown (combo_box);
5524 if (combo_box->priv->row_separator_destroy)
5525 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5527 combo_box->priv->row_separator_func = NULL;
5528 combo_box->priv->row_separator_data = NULL;
5529 combo_box->priv->row_separator_destroy = NULL;
5531 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5532 combo_box->priv->cell_view = NULL;
5536 gtk_combo_box_dispose(GObject* object)
5538 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5540 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5542 gtk_combo_box_menu_destroy (combo_box);
5543 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5544 combo_box->priv->popup_widget = NULL;
5547 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5551 gtk_combo_box_finalize (GObject *object)
5553 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5556 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5557 gtk_combo_box_list_destroy (combo_box);
5559 if (combo_box->priv->popup_window)
5560 gtk_widget_destroy (combo_box->priv->popup_window);
5562 gtk_combo_box_unset_model (combo_box);
5564 for (i = combo_box->priv->cells; i; i = i->next)
5566 ComboCellInfo *info = (ComboCellInfo *)i->data;
5567 GSList *list = info->attributes;
5570 info->destroy (info->func_data);
5572 while (list && list->next)
5574 g_free (list->data);
5575 list = list->next->next;
5577 g_slist_free (info->attributes);
5579 g_object_unref (info->cell);
5580 g_slice_free (ComboCellInfo, info);
5582 g_slist_free (combo_box->priv->cells);
5584 g_free (combo_box->priv->tearoff_title);
5586 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5590 gtk_cell_editable_key_press (GtkWidget *widget,
5594 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5596 if (event->keyval == GDK_Escape)
5598 g_object_set (combo_box,
5599 "editing-canceled", TRUE,
5601 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5602 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5606 else if (event->keyval == GDK_Return ||
5607 event->keyval == GDK_ISO_Enter ||
5608 event->keyval == GDK_KP_Enter)
5610 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5611 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5620 popdown_idle (gpointer data)
5622 GtkComboBox *combo_box;
5624 combo_box = GTK_COMBO_BOX (data);
5626 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5627 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5629 g_object_unref (combo_box);
5635 popdown_handler (GtkWidget *widget,
5638 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5642 popup_idle (gpointer data)
5644 GtkComboBox *combo_box;
5646 combo_box = GTK_COMBO_BOX (data);
5648 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5649 combo_box->priv->cell_view)
5650 g_signal_connect_object (combo_box->priv->popup_widget,
5651 "unmap", G_CALLBACK (popdown_handler),
5654 /* we unset this if a menu item is activated */
5655 g_object_set (combo_box,
5656 "editing-canceled", TRUE,
5658 gtk_combo_box_popup (combo_box);
5660 combo_box->priv->popup_idle_id = 0;
5661 combo_box->priv->activate_button = 0;
5662 combo_box->priv->activate_time = 0;
5668 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5671 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5673 combo_box->priv->is_cell_renderer = TRUE;
5675 if (combo_box->priv->cell_view)
5677 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5678 G_CALLBACK (gtk_cell_editable_key_press),
5681 gtk_widget_grab_focus (combo_box->priv->button);
5685 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5686 G_CALLBACK (gtk_cell_editable_key_press),
5689 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5690 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5693 /* we do the immediate popup only for the optionmenu-like
5696 if (combo_box->priv->is_cell_renderer &&
5697 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5699 if (event && event->type == GDK_BUTTON_PRESS)
5701 GdkEventButton *event_button = (GdkEventButton *)event;
5703 combo_box->priv->activate_button = event_button->button;
5704 combo_box->priv->activate_time = event_button->time;
5707 combo_box->priv->popup_idle_id =
5708 gdk_threads_add_idle (popup_idle, combo_box);
5714 * gtk_combo_box_get_add_tearoffs:
5715 * @combo_box: a #GtkComboBox
5717 * Gets the current value of the :add-tearoffs property.
5719 * Return value: the current value of the :add-tearoffs property.
5722 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5724 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5726 return combo_box->priv->add_tearoffs;
5730 * gtk_combo_box_set_add_tearoffs:
5731 * @combo_box: a #GtkComboBox
5732 * @add_tearoffs: %TRUE to add tearoff menu items
5734 * Sets whether the popup menu should have a tearoff
5740 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5741 gboolean add_tearoffs)
5743 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5745 add_tearoffs = add_tearoffs != FALSE;
5747 if (combo_box->priv->add_tearoffs != add_tearoffs)
5749 combo_box->priv->add_tearoffs = add_tearoffs;
5750 gtk_combo_box_check_appearance (combo_box);
5751 gtk_combo_box_relayout (combo_box);
5752 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5757 * gtk_combo_box_get_title:
5758 * @combo_box: a #GtkComboBox
5760 * Gets the current title of the menu in tearoff mode. See
5761 * gtk_combo_box_set_add_tearoffs().
5763 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5764 * string which must not be freed.
5768 G_CONST_RETURN gchar*
5769 gtk_combo_box_get_title (GtkComboBox *combo_box)
5771 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5773 return combo_box->priv->tearoff_title;
5777 gtk_combo_box_update_title (GtkComboBox *combo_box)
5779 gtk_combo_box_check_appearance (combo_box);
5781 if (combo_box->priv->popup_widget &&
5782 GTK_IS_MENU (combo_box->priv->popup_widget))
5783 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5784 combo_box->priv->tearoff_title);
5788 * gtk_combo_box_set_title:
5789 * @combo_box: a #GtkComboBox
5790 * @title: a title for the menu in tearoff mode
5792 * Sets the menu's title in tearoff mode.
5797 gtk_combo_box_set_title (GtkComboBox *combo_box,
5800 GtkComboBoxPrivate *priv;
5802 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5804 priv = combo_box->priv;
5806 if (strcmp (title ? title : "",
5807 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5809 g_free (priv->tearoff_title);
5810 priv->tearoff_title = g_strdup (title);
5812 gtk_combo_box_update_title (combo_box);
5814 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5819 * gtk_combo_box_get_popup_accessible:
5820 * @combo_box: a #GtkComboBox
5822 * Gets the accessible object corresponding to the combo box's popup.
5824 * This function is mostly intended for use by accessibility technologies;
5825 * applications should have little use for it.
5827 * Returns: the accessible object corresponding to the combo box's popup.
5832 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5836 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5838 if (combo_box->priv->popup_widget)
5840 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5848 * gtk_combo_box_get_row_separator_func:
5849 * @combo_box: a #GtkComboBox
5851 * Returns the current row separator function.
5853 * Return value: the current row separator function.
5857 GtkTreeViewRowSeparatorFunc
5858 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5860 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5862 return combo_box->priv->row_separator_func;
5866 * gtk_combo_box_set_row_separator_func:
5867 * @combo_box: a #GtkComboBox
5868 * @func: a #GtkTreeViewRowSeparatorFunc
5869 * @data: (allow-none): user data to pass to @func, or %NULL
5870 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5872 * Sets the row separator function, which is used to determine
5873 * whether a row should be drawn as a separator. If the row separator
5874 * function is %NULL, no separators are drawn. This is the default value.
5879 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5880 GtkTreeViewRowSeparatorFunc func,
5882 GDestroyNotify destroy)
5884 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5886 if (combo_box->priv->row_separator_destroy)
5887 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5889 combo_box->priv->row_separator_func = func;
5890 combo_box->priv->row_separator_data = data;
5891 combo_box->priv->row_separator_destroy = destroy;
5893 if (combo_box->priv->tree_view)
5894 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5897 gtk_combo_box_relayout (combo_box);
5899 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5903 * gtk_combo_box_set_button_sensitivity:
5904 * @combo_box: a #GtkComboBox
5905 * @sensitivity: specify the sensitivity of the dropdown button
5907 * Sets whether the dropdown button of the combo box should be
5908 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5909 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5914 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5915 GtkSensitivityType sensitivity)
5917 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5919 if (combo_box->priv->button_sensitivity != sensitivity)
5921 combo_box->priv->button_sensitivity = sensitivity;
5922 gtk_combo_box_update_sensitivity (combo_box);
5924 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5929 * gtk_combo_box_get_button_sensitivity:
5930 * @combo_box: a #GtkComboBox
5932 * Returns whether the combo box sets the dropdown button
5933 * sensitive or not when there are no items in the model.
5935 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5936 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5937 * if the button is always insensitive or
5938 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5939 * the model has one item to be selected.
5944 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5946 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5948 return combo_box->priv->button_sensitivity;
5953 * gtk_combo_box_set_focus_on_click:
5954 * @combo: a #GtkComboBox
5955 * @focus_on_click: whether the combo box grabs focus when clicked
5958 * Sets whether the combo box will grab focus when it is clicked with
5959 * the mouse. Making mouse clicks not grab focus is useful in places
5960 * like toolbars where you don't want the keyboard focus removed from
5961 * the main area of the application.
5966 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5967 gboolean focus_on_click)
5969 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5971 focus_on_click = focus_on_click != FALSE;
5973 if (combo_box->priv->focus_on_click != focus_on_click)
5975 combo_box->priv->focus_on_click = focus_on_click;
5977 if (combo_box->priv->button)
5978 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5981 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5986 * gtk_combo_box_get_focus_on_click:
5987 * @combo: a #GtkComboBox
5989 * Returns whether the combo box grabs focus when it is clicked
5990 * with the mouse. See gtk_combo_box_set_focus_on_click().
5992 * Return value: %TRUE if the combo box grabs focus when it is
5993 * clicked with the mouse.
5998 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
6000 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6002 return combo_box->priv->focus_on_click;
6007 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
6008 GtkBuilder *builder,
6010 const gchar *tagname,
6011 GMarkupParser *parser,
6014 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6015 tagname, parser, data))
6018 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6019 tagname, parser, data);
6023 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
6024 GtkBuilder *builder,
6026 const gchar *tagname,
6029 if (strcmp (tagname, "attributes") == 0)
6030 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
6033 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
6037 #define __GTK_COMBO_BOX_C__
6038 #include "gtkaliasdef.c"