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"
55 /* WELCOME, to THE house of evil code */
57 typedef struct _ComboCellInfo ComboCellInfo;
60 GtkCellRenderer *cell;
63 GtkCellLayoutDataFunc func;
65 GDestroyNotify destroy;
71 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
73 struct _GtkComboBoxPrivate
81 GtkShadowType shadow_type;
83 gint active; /* Only temporary */
84 GtkTreeRowReference *active_row;
87 GtkTreeViewColumn *column;
90 GtkWidget *cell_view_frame;
97 GtkWidget *popup_widget;
98 GtkWidget *popup_window;
99 GtkWidget *scrolled_window;
106 guint activate_button;
107 guint32 activate_time;
109 guint resize_idle_id;
115 guint popup_in_progress : 1;
116 guint popup_shown : 1;
117 guint add_tearoffs : 1;
119 guint is_cell_renderer : 1;
120 guint editing_canceled : 1;
121 guint auto_scroll : 1;
122 guint focus_on_click : 1;
123 guint button_sensitivity : 2;
125 GtkTreeViewRowSeparatorFunc row_separator_func;
126 gpointer row_separator_data;
127 GDestroyNotify row_separator_destroy;
129 GdkDevice *grab_pointer;
130 GdkDevice *grab_keyboard;
132 gchar *tearoff_title;
135 /* While debugging this evil code, I have learned that
136 * there are actually 4 modes to this widget, which can
137 * be characterized as follows
139 * 1) menu mode, no child added
142 * cell_view -> GtkCellView, regular child
143 * cell_view_frame -> NULL
144 * button -> GtkToggleButton set_parent to combo
145 * arrow -> GtkArrow set_parent to button
146 * separator -> GtkVSepator set_parent to button
147 * popup_widget -> GtkMenu
148 * popup_window -> NULL
149 * scrolled_window -> NULL
151 * 2) menu mode, child added
155 * cell_view_frame -> NULL
156 * button -> GtkToggleButton set_parent to combo
157 * arrow -> GtkArrow, child of button
159 * popup_widget -> GtkMenu
160 * popup_window -> NULL
161 * scrolled_window -> NULL
163 * 3) list mode, no child added
165 * tree_view -> GtkTreeView, child of scrolled_window
166 * cell_view -> GtkCellView, regular child
167 * cell_view_frame -> GtkFrame, set parent to combo
168 * button -> GtkToggleButton, set_parent to combo
169 * arrow -> GtkArrow, child of button
171 * popup_widget -> tree_view
172 * popup_window -> GtkWindow
173 * scrolled_window -> GtkScrolledWindow, child of popup_window
175 * 4) list mode, child added
177 * tree_view -> GtkTreeView, child of scrolled_window
179 * cell_view_frame -> NULL
180 * button -> GtkToggleButton, set_parent to combo
181 * arrow -> GtkArrow, child of button
183 * popup_widget -> tree_view
184 * popup_window -> GtkWindow
185 * scrolled_window -> GtkScrolledWindow, child of popup_window
201 PROP_ROW_SPAN_COLUMN,
202 PROP_COLUMN_SPAN_COLUMN,
209 PROP_BUTTON_SENSITIVITY,
210 PROP_EDITING_CANCELED
213 static guint combo_box_signals[LAST_SIGNAL] = {0,};
215 #define BONUS_PADDING 4
216 #define SCROLL_TIME 100
220 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
221 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
222 static void gtk_combo_box_dispose (GObject *object);
223 static void gtk_combo_box_finalize (GObject *object);
224 static void gtk_combo_box_destroy (GtkObject *object);
226 static void gtk_combo_box_set_property (GObject *object,
230 static void gtk_combo_box_get_property (GObject *object,
235 static void gtk_combo_box_state_changed (GtkWidget *widget,
236 GtkStateType previous);
237 static void gtk_combo_box_grab_focus (GtkWidget *widget);
238 static void gtk_combo_box_style_set (GtkWidget *widget,
240 static void gtk_combo_box_button_toggled (GtkWidget *widget,
242 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
243 GtkStateType previous,
245 static void gtk_combo_box_add (GtkContainer *container,
247 static void gtk_combo_box_remove (GtkContainer *container,
250 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
251 GtkCellRenderer *cell);
253 static void gtk_combo_box_menu_show (GtkWidget *menu,
255 static void gtk_combo_box_menu_hide (GtkWidget *menu,
258 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
260 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
265 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
270 static void gtk_combo_box_menu_position (GtkMenu *menu,
276 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
278 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
280 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
282 static void gtk_combo_box_size_request (GtkWidget *widget,
283 GtkRequisition *requisition);
284 static void gtk_combo_box_size_allocate (GtkWidget *widget,
285 GtkAllocation *allocation);
286 static void gtk_combo_box_forall (GtkContainer *container,
287 gboolean include_internals,
288 GtkCallback callback,
289 gpointer callback_data);
290 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
291 GdkEventExpose *event);
292 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
293 GdkEventScroll *event);
294 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
297 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
298 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
299 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
300 GtkScrollType scroll);
301 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
302 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
304 /* listening to the model */
305 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
309 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
312 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
317 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
321 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
327 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
332 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
333 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
335 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
336 GdkEventButton *event,
338 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
341 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
342 GdkEventCrossing *event,
344 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
347 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
348 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
349 GdkEventButton *event,
352 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
355 gboolean path_currently_selected,
358 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
362 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
365 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
366 gboolean add_children);
367 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
368 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
371 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
372 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
374 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
378 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
380 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
381 GdkEventButton *event,
383 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
386 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
387 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
391 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
394 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
399 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
403 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
406 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
408 guint32 activate_time);
409 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
414 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
415 GtkCellRenderer *cell,
417 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
418 GtkCellRenderer *cell,
420 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
421 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
422 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
423 GtkCellRenderer *cell,
424 const gchar *attribute,
426 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
427 GtkCellRenderer *cell,
428 GtkCellLayoutDataFunc func,
430 GDestroyNotify destroy);
431 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
432 GtkCellRenderer *cell);
433 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
434 GtkCellRenderer *cell,
436 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
437 gboolean group_cycling);
439 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
440 GtkCellLayout *cell_layout);
441 static void combo_cell_data_func (GtkCellLayout *cell_layout,
442 GtkCellRenderer *cell,
443 GtkTreeModel *tree_model,
446 static void gtk_combo_box_child_show (GtkWidget *widget,
447 GtkComboBox *combo_box);
448 static void gtk_combo_box_child_hide (GtkWidget *widget,
449 GtkComboBox *combo_box);
451 /* GtkBuildable method implementation */
452 static GtkBuildableIface *parent_buildable_iface;
454 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
455 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
458 const gchar *tagname,
459 GMarkupParser *parser,
461 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
464 const gchar *tagname,
467 /* GtkCellEditable method implementations */
468 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
472 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
473 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
474 gtk_combo_box_cell_layout_init)
475 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
476 gtk_combo_box_cell_editable_init)
477 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
478 gtk_combo_box_buildable_init))
483 gtk_combo_box_class_init (GtkComboBoxClass *klass)
485 GObjectClass *object_class;
486 GtkObjectClass *gtk_object_class;
487 GtkContainerClass *container_class;
488 GtkWidgetClass *widget_class;
489 GtkBindingSet *binding_set;
491 klass->get_active_text = gtk_combo_box_real_get_active_text;
493 container_class = (GtkContainerClass *)klass;
494 container_class->forall = gtk_combo_box_forall;
495 container_class->add = gtk_combo_box_add;
496 container_class->remove = gtk_combo_box_remove;
498 widget_class = (GtkWidgetClass *)klass;
499 widget_class->size_allocate = gtk_combo_box_size_allocate;
500 widget_class->size_request = gtk_combo_box_size_request;
501 widget_class->expose_event = gtk_combo_box_expose_event;
502 widget_class->scroll_event = gtk_combo_box_scroll_event;
503 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
504 widget_class->grab_focus = gtk_combo_box_grab_focus;
505 widget_class->style_set = gtk_combo_box_style_set;
506 widget_class->state_changed = gtk_combo_box_state_changed;
508 gtk_object_class = (GtkObjectClass *)klass;
509 gtk_object_class->destroy = gtk_combo_box_destroy;
511 object_class = (GObjectClass *)klass;
512 object_class->dispose = gtk_combo_box_dispose;
513 object_class->finalize = gtk_combo_box_finalize;
514 object_class->set_property = gtk_combo_box_set_property;
515 object_class->get_property = gtk_combo_box_get_property;
519 * GtkComboBox::changed:
520 * @widget: the object which received the signal
522 * The changed signal is emitted when the active
523 * item is changed. The can be due to the user selecting
524 * a different item from the list, or due to a
525 * call to gtk_combo_box_set_active_iter().
526 * It will also be emitted while typing into a GtkComboBoxEntry,
527 * as well as when selecting an item from the GtkComboBoxEntry's list.
531 combo_box_signals[CHANGED] =
532 g_signal_new (I_("changed"),
533 G_OBJECT_CLASS_TYPE (klass),
535 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
537 g_cclosure_marshal_VOID__VOID,
540 * GtkComboBox::move-active:
541 * @widget: the object that received the signal
542 * @scroll_type: a #GtkScrollType
544 * The ::move-active signal is a
545 * <link linkend="keybinding-signals">keybinding signal</link>
546 * which gets emitted to move the active selection.
550 combo_box_signals[MOVE_ACTIVE] =
551 g_signal_new_class_handler (I_("move-active"),
552 G_OBJECT_CLASS_TYPE (klass),
553 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
554 G_CALLBACK (gtk_combo_box_real_move_active),
556 g_cclosure_marshal_VOID__ENUM,
558 GTK_TYPE_SCROLL_TYPE);
561 * GtkComboBox::popup:
562 * @widget: the object that received the signal
564 * The ::popup signal is a
565 * <link linkend="keybinding-signals">keybinding signal</link>
566 * which gets emitted to popup the combo box list.
568 * The default binding for this signal is Alt+Down.
572 combo_box_signals[POPUP] =
573 g_signal_new_class_handler (I_("popup"),
574 G_OBJECT_CLASS_TYPE (klass),
575 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
576 G_CALLBACK (gtk_combo_box_real_popup),
578 g_cclosure_marshal_VOID__VOID,
581 * GtkComboBox::popdown:
582 * @button: the object which received the signal
584 * The ::popdown signal is a
585 * <link linkend="keybinding-signals">keybinding signal</link>
586 * which gets emitted to popdown the combo box list.
588 * The default bindings for this signal are Alt+Up and Escape.
592 combo_box_signals[POPDOWN] =
593 g_signal_new_class_handler (I_("popdown"),
594 G_OBJECT_CLASS_TYPE (klass),
595 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
596 G_CALLBACK (gtk_combo_box_real_popdown),
598 _gtk_marshal_BOOLEAN__VOID,
602 binding_set = gtk_binding_set_by_class (widget_class);
604 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
606 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
609 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
611 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
613 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
616 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
618 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
619 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
621 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
622 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
624 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
625 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
627 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
628 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
630 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
631 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
633 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
635 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
637 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
638 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
640 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
641 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
643 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
644 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
646 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
647 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
649 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
650 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
652 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
655 g_object_class_override_property (object_class,
656 PROP_EDITING_CANCELED,
662 * The model from which the combo box takes the values shown
667 g_object_class_install_property (object_class,
669 g_param_spec_object ("model",
670 P_("ComboBox model"),
671 P_("The model for the combo box"),
673 GTK_PARAM_READWRITE));
676 * GtkComboBox:wrap-width:
678 * If wrap-width is set to a positive value, the list will be
679 * displayed in multiple columns, the number of columns is
680 * determined by wrap-width.
684 g_object_class_install_property (object_class,
686 g_param_spec_int ("wrap-width",
688 P_("Wrap width for laying out the items in a grid"),
692 GTK_PARAM_READWRITE));
696 * GtkComboBox:row-span-column:
698 * If this is set to a non-negative value, it must be the index of a column
699 * of type %G_TYPE_INT in the model.
701 * The values of that column are used to determine how many rows a value in
702 * the list will span. Therefore, the values in the model column pointed to
703 * by this property must be greater than zero and not larger than wrap-width.
707 g_object_class_install_property (object_class,
708 PROP_ROW_SPAN_COLUMN,
709 g_param_spec_int ("row-span-column",
710 P_("Row span column"),
711 P_("TreeModel column containing the row span values"),
715 GTK_PARAM_READWRITE));
719 * GtkComboBox:column-span-column:
721 * If this is set to a non-negative value, it must be the index of a column
722 * of type %G_TYPE_INT in the model.
724 * The values of that column are used to determine how many columns a value
725 * in the list will span.
729 g_object_class_install_property (object_class,
730 PROP_COLUMN_SPAN_COLUMN,
731 g_param_spec_int ("column-span-column",
732 P_("Column span column"),
733 P_("TreeModel column containing the column span values"),
737 GTK_PARAM_READWRITE));
741 * GtkComboBox:active:
743 * The item which is currently active. If the model is a non-flat treemodel,
744 * and the active item is not an immediate child of the root of the tree,
745 * this property has the value
746 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
747 * where <literal>path</literal> is the #GtkTreePath of the active item.
751 g_object_class_install_property (object_class,
753 g_param_spec_int ("active",
755 P_("The item which is currently active"),
759 GTK_PARAM_READWRITE));
762 * GtkComboBox:add-tearoffs:
764 * The add-tearoffs property controls whether generated menus
765 * have tearoff menu items.
767 * Note that this only affects menu style combo boxes.
771 g_object_class_install_property (object_class,
773 g_param_spec_boolean ("add-tearoffs",
774 P_("Add tearoffs to menus"),
775 P_("Whether dropdowns should have a tearoff menu item"),
777 GTK_PARAM_READWRITE));
780 * GtkComboBox:has-frame:
782 * The has-frame property controls whether a frame
783 * is drawn around the entry.
787 g_object_class_install_property (object_class,
789 g_param_spec_boolean ("has-frame",
791 P_("Whether the combo box draws a frame around the child"),
793 GTK_PARAM_READWRITE));
795 g_object_class_install_property (object_class,
797 g_param_spec_boolean ("focus-on-click",
798 P_("Focus on click"),
799 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
801 GTK_PARAM_READWRITE));
804 * GtkComboBox:tearoff-title:
806 * A title that may be displayed by the window manager
807 * when the popup is torn-off.
811 g_object_class_install_property (object_class,
813 g_param_spec_string ("tearoff-title",
815 P_("A title that may be displayed by the window manager when the popup is torn-off"),
817 GTK_PARAM_READWRITE));
821 * GtkComboBox:popup-shown:
823 * Whether the combo boxes dropdown is popped up.
824 * Note that this property is mainly useful, because
825 * it allows you to connect to notify::popup-shown.
829 g_object_class_install_property (object_class,
831 g_param_spec_boolean ("popup-shown",
833 P_("Whether the combo's dropdown is shown"),
835 GTK_PARAM_READABLE));
839 * GtkComboBox:button-sensitivity:
841 * Whether the dropdown button is sensitive when
842 * the model is empty.
846 g_object_class_install_property (object_class,
847 PROP_BUTTON_SENSITIVITY,
848 g_param_spec_enum ("button-sensitivity",
849 P_("Button Sensitivity"),
850 P_("Whether the dropdown button is sensitive when the model is empty"),
851 GTK_TYPE_SENSITIVITY_TYPE,
852 GTK_SENSITIVITY_AUTO,
853 GTK_PARAM_READWRITE));
855 gtk_widget_class_install_style_property (widget_class,
856 g_param_spec_boolean ("appears-as-list",
857 P_("Appears as list"),
858 P_("Whether dropdowns should look like lists rather than menus"),
860 GTK_PARAM_READABLE));
863 * GtkComboBox:arrow-size:
865 * Sets the minimum size of the arrow in the combo box. Note
866 * that the arrow size is coupled to the font size, so in case
867 * a larger font is used, the arrow will be larger than set
872 gtk_widget_class_install_style_property (widget_class,
873 g_param_spec_int ("arrow-size",
875 P_("The minimum size of the arrow in the combo box"),
879 GTK_PARAM_READABLE));
882 * GtkComboBox:shadow-type:
884 * Which kind of shadow to draw around the combo box.
888 gtk_widget_class_install_style_property (widget_class,
889 g_param_spec_enum ("shadow-type",
891 P_("Which kind of shadow to draw around the combo box"),
892 GTK_TYPE_SHADOW_TYPE,
894 GTK_PARAM_READABLE));
896 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
900 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
902 parent_buildable_iface = g_type_interface_peek_parent (iface);
903 iface->add_child = _gtk_cell_layout_buildable_add_child;
904 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
905 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
909 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
911 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
912 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
913 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
914 iface->clear = gtk_combo_box_cell_layout_clear;
915 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
916 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
917 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
918 iface->reorder = gtk_combo_box_cell_layout_reorder;
922 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
924 iface->start_editing = gtk_combo_box_start_editing;
928 gtk_combo_box_init (GtkComboBox *combo_box)
930 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
932 priv->cell_view = gtk_cell_view_new ();
933 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
934 GTK_BIN (combo_box)->child = priv->cell_view;
935 gtk_widget_show (priv->cell_view);
939 priv->wrap_width = 0;
942 priv->active_row = NULL;
943 priv->col_column = -1;
944 priv->row_column = -1;
946 priv->popup_shown = FALSE;
947 priv->add_tearoffs = FALSE;
948 priv->has_frame = TRUE;
949 priv->is_cell_renderer = FALSE;
950 priv->editing_canceled = FALSE;
951 priv->auto_scroll = FALSE;
952 priv->focus_on_click = TRUE;
953 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
955 combo_box->priv = priv;
957 gtk_combo_box_check_appearance (combo_box);
961 gtk_combo_box_set_property (GObject *object,
966 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
971 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
974 case PROP_WRAP_WIDTH:
975 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
978 case PROP_ROW_SPAN_COLUMN:
979 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
982 case PROP_COLUMN_SPAN_COLUMN:
983 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
987 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
990 case PROP_ADD_TEAROFFS:
991 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
995 combo_box->priv->has_frame = g_value_get_boolean (value);
998 case PROP_FOCUS_ON_CLICK:
999 gtk_combo_box_set_focus_on_click (combo_box,
1000 g_value_get_boolean (value));
1003 case PROP_TEAROFF_TITLE:
1004 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1007 case PROP_POPUP_SHOWN:
1008 if (g_value_get_boolean (value))
1009 gtk_combo_box_popup (combo_box);
1011 gtk_combo_box_popdown (combo_box);
1014 case PROP_BUTTON_SENSITIVITY:
1015 gtk_combo_box_set_button_sensitivity (combo_box,
1016 g_value_get_enum (value));
1019 case PROP_EDITING_CANCELED:
1020 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1024 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1030 gtk_combo_box_get_property (GObject *object,
1035 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1036 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
1041 g_value_set_object (value, combo_box->priv->model);
1044 case PROP_WRAP_WIDTH:
1045 g_value_set_int (value, combo_box->priv->wrap_width);
1048 case PROP_ROW_SPAN_COLUMN:
1049 g_value_set_int (value, combo_box->priv->row_column);
1052 case PROP_COLUMN_SPAN_COLUMN:
1053 g_value_set_int (value, combo_box->priv->col_column);
1057 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1060 case PROP_ADD_TEAROFFS:
1061 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1064 case PROP_HAS_FRAME:
1065 g_value_set_boolean (value, combo_box->priv->has_frame);
1068 case PROP_FOCUS_ON_CLICK:
1069 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1072 case PROP_TEAROFF_TITLE:
1073 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1076 case PROP_POPUP_SHOWN:
1077 g_value_set_boolean (value, combo_box->priv->popup_shown);
1080 case PROP_BUTTON_SENSITIVITY:
1081 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1084 case PROP_EDITING_CANCELED:
1085 g_value_set_boolean (value, priv->editing_canceled);
1089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1095 gtk_combo_box_state_changed (GtkWidget *widget,
1096 GtkStateType previous)
1098 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1099 GtkComboBoxPrivate *priv = combo_box->priv;
1101 if (gtk_widget_get_realized (widget))
1103 if (priv->tree_view && priv->cell_view)
1104 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1105 &widget->style->base[gtk_widget_get_state (widget)]);
1108 gtk_widget_queue_draw (widget);
1112 gtk_combo_box_button_state_changed (GtkWidget *widget,
1113 GtkStateType previous,
1116 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1117 GtkComboBoxPrivate *priv = combo_box->priv;
1119 if (gtk_widget_get_realized (widget))
1121 if (!priv->tree_view && priv->cell_view)
1123 if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) !=
1124 (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE))
1125 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1127 gtk_widget_set_state (priv->cell_view,
1128 gtk_widget_get_state (widget));
1132 gtk_widget_queue_draw (widget);
1136 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1138 GtkComboBoxPrivate *priv = combo_box->priv;
1139 gboolean appears_as_list;
1141 /* if wrap_width > 0, then we are in grid-mode and forced to use
1144 if (priv->wrap_width)
1145 appears_as_list = FALSE;
1147 gtk_widget_style_get (GTK_WIDGET (combo_box),
1148 "appears-as-list", &appears_as_list,
1151 if (appears_as_list)
1153 /* Destroy all the menu mode widgets, if they exist. */
1154 if (GTK_IS_MENU (priv->popup_widget))
1155 gtk_combo_box_menu_destroy (combo_box);
1157 /* Create the list mode widgets, if they don't already exist. */
1158 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1159 gtk_combo_box_list_setup (combo_box);
1163 /* Destroy all the list mode widgets, if they exist. */
1164 if (GTK_IS_TREE_VIEW (priv->tree_view))
1165 gtk_combo_box_list_destroy (combo_box);
1167 /* Create the menu mode widgets, if they don't already exist. */
1168 if (!GTK_IS_MENU (priv->popup_widget))
1169 gtk_combo_box_menu_setup (combo_box, TRUE);
1172 gtk_widget_style_get (GTK_WIDGET (combo_box),
1173 "shadow-type", &priv->shadow_type,
1178 gtk_combo_box_style_set (GtkWidget *widget,
1181 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1182 GtkComboBoxPrivate *priv = combo_box->priv;
1184 gtk_combo_box_check_appearance (combo_box);
1186 if (priv->tree_view && priv->cell_view)
1187 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1188 &widget->style->base[gtk_widget_get_state (widget)]);
1190 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1191 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1192 GTK_SHADOW_NONE == priv->shadow_type ?
1193 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1197 gtk_combo_box_button_toggled (GtkWidget *widget,
1200 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1202 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1204 if (!combo_box->priv->popup_in_progress)
1205 gtk_combo_box_popup (combo_box);
1208 gtk_combo_box_popdown (combo_box);
1212 gtk_combo_box_add (GtkContainer *container,
1215 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1216 GtkComboBoxPrivate *priv = combo_box->priv;
1218 if (priv->cell_view && priv->cell_view->parent)
1220 gtk_widget_unparent (priv->cell_view);
1221 GTK_BIN (container)->child = NULL;
1222 gtk_widget_queue_resize (GTK_WIDGET (container));
1225 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1226 GTK_BIN (container)->child = widget;
1228 if (priv->cell_view &&
1229 widget != priv->cell_view)
1231 /* since the cell_view was unparented, it's gone now */
1232 priv->cell_view = NULL;
1234 if (!priv->tree_view && priv->separator)
1236 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1238 priv->separator = NULL;
1240 gtk_widget_queue_resize (GTK_WIDGET (container));
1242 else if (priv->cell_view_frame)
1244 gtk_widget_unparent (priv->cell_view_frame);
1245 priv->cell_view_frame = NULL;
1252 gtk_combo_box_remove (GtkContainer *container,
1255 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1256 GtkComboBoxPrivate *priv = combo_box->priv;
1258 gboolean appears_as_list;
1260 if (widget == priv->cell_view)
1261 priv->cell_view = NULL;
1263 gtk_widget_unparent (widget);
1264 GTK_BIN (container)->child = NULL;
1266 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1269 gtk_widget_queue_resize (GTK_WIDGET (container));
1271 if (!priv->tree_view)
1272 appears_as_list = FALSE;
1274 appears_as_list = TRUE;
1276 if (appears_as_list)
1277 gtk_combo_box_list_destroy (combo_box);
1278 else if (GTK_IS_MENU (priv->popup_widget))
1280 gtk_combo_box_menu_destroy (combo_box);
1281 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1282 priv->popup_widget = NULL;
1285 if (!priv->cell_view)
1287 priv->cell_view = gtk_cell_view_new ();
1288 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1289 GTK_BIN (container)->child = priv->cell_view;
1291 gtk_widget_show (priv->cell_view);
1292 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1294 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1298 if (appears_as_list)
1299 gtk_combo_box_list_setup (combo_box);
1301 gtk_combo_box_menu_setup (combo_box, TRUE);
1303 if (gtk_tree_row_reference_valid (priv->active_row))
1305 path = gtk_tree_row_reference_get_path (priv->active_row);
1306 gtk_combo_box_set_active_internal (combo_box, path);
1307 gtk_tree_path_free (path);
1310 gtk_combo_box_set_active_internal (combo_box, NULL);
1313 static ComboCellInfo *
1314 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1315 GtkCellRenderer *cell)
1319 for (i = combo_box->priv->cells; i; i = i->next)
1321 ComboCellInfo *info = (ComboCellInfo *)i->data;
1323 if (info && info->cell == cell)
1331 gtk_combo_box_menu_show (GtkWidget *menu,
1334 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1335 GtkComboBoxPrivate *priv = combo_box->priv;
1337 gtk_combo_box_child_show (menu, user_data);
1339 priv->popup_in_progress = TRUE;
1340 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1342 priv->popup_in_progress = FALSE;
1346 gtk_combo_box_menu_hide (GtkWidget *menu,
1349 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1351 gtk_combo_box_child_hide (menu,user_data);
1353 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1358 gtk_combo_box_detacher (GtkWidget *widget,
1361 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1362 GtkComboBoxPrivate *priv = combo_box->priv;
1364 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1366 g_signal_handlers_disconnect_by_func (menu->toplevel,
1367 gtk_combo_box_menu_show,
1369 g_signal_handlers_disconnect_by_func (menu->toplevel,
1370 gtk_combo_box_menu_hide,
1373 priv->popup_widget = NULL;
1377 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1380 GtkComboBoxPrivate *priv = combo_box->priv;
1382 if (GTK_IS_MENU (priv->popup_widget))
1384 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1385 priv->popup_widget = NULL;
1387 else if (priv->popup_widget)
1389 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1390 priv->popup_widget);
1391 g_object_unref (priv->popup_widget);
1392 priv->popup_widget = NULL;
1395 if (GTK_IS_MENU (popup))
1397 if (priv->popup_window)
1399 gtk_widget_destroy (priv->popup_window);
1400 priv->popup_window = NULL;
1403 priv->popup_widget = popup;
1406 * Note that we connect to show/hide on the toplevel, not the
1407 * menu itself, since the menu is not shown/hidden when it is
1408 * popped up while torn-off.
1410 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1411 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1412 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1413 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1415 gtk_menu_attach_to_widget (GTK_MENU (popup),
1416 GTK_WIDGET (combo_box),
1417 gtk_combo_box_detacher);
1421 if (!priv->popup_window)
1423 GtkWidget *toplevel;
1425 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1426 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1428 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1429 GDK_WINDOW_TYPE_HINT_COMBO);
1431 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1432 G_CALLBACK (gtk_combo_box_child_show),
1434 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1435 G_CALLBACK (gtk_combo_box_child_hide),
1438 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1439 if (GTK_IS_WINDOW (toplevel))
1441 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1442 GTK_WINDOW (priv->popup_window));
1443 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1444 GTK_WINDOW (toplevel));
1447 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1448 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1449 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1451 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1453 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1456 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1459 gtk_widget_show (priv->scrolled_window);
1461 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1462 priv->scrolled_window);
1465 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1468 gtk_widget_show (popup);
1469 g_object_ref (popup);
1470 priv->popup_widget = popup;
1475 gtk_combo_box_menu_position_below (GtkMenu *menu,
1481 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1487 GdkRectangle monitor;
1489 /* FIXME: is using the size request here broken? */
1490 child = GTK_BIN (combo_box)->child;
1494 if (!gtk_widget_get_has_window (child))
1496 sx += child->allocation.x;
1497 sy += child->allocation.y;
1500 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1502 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1503 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1505 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1507 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1510 *x = sx + child->allocation.width - req.width;
1513 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1514 monitor_num = gdk_screen_get_monitor_at_window (screen,
1515 GTK_WIDGET (combo_box)->window);
1516 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1520 else if (*x + req.width > monitor.x + monitor.width)
1521 *x = monitor.x + monitor.width - req.width;
1523 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1524 *y += child->allocation.height;
1525 else if (*y - monitor.y >= req.height)
1527 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1528 *y += child->allocation.height;
1536 gtk_combo_box_menu_position_over (GtkMenu *menu,
1542 GtkComboBox *combo_box;
1546 GtkRequisition requisition;
1553 combo_box = GTK_COMBO_BOX (user_data);
1554 widget = GTK_WIDGET (combo_box);
1556 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1557 menu_width = requisition.width;
1559 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1561 menu_xpos = widget->allocation.x;
1562 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1566 gtk_widget_get_child_requisition (active, &requisition);
1567 menu_ypos -= requisition.height / 2;
1570 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1573 child = children->data;
1575 if (active == child)
1578 if (gtk_widget_get_visible (child))
1580 gtk_widget_get_child_requisition (child, &requisition);
1581 menu_ypos -= requisition.height;
1584 children = children->next;
1587 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1588 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1590 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1591 &menu_xpos, &menu_ypos);
1593 /* Clamp the position on screen */
1594 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1598 else if ((menu_xpos + menu_width) > screen_width)
1599 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1608 gtk_combo_box_menu_position (GtkMenu *menu,
1614 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1615 GtkComboBoxPrivate *priv = combo_box->priv;
1616 GtkWidget *menu_item;
1619 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1620 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1623 /* FIXME handle nested menus better */
1624 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1626 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1629 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1632 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1633 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1634 GDK_WINDOW_TYPE_HINT_COMBO);
1638 gtk_combo_box_list_position (GtkComboBox *combo_box,
1644 GtkComboBoxPrivate *priv = combo_box->priv;
1647 GdkRectangle monitor;
1648 GtkRequisition popup_req;
1649 GtkPolicyType hpolicy, vpolicy;
1651 /* under windows, the drop down list is as wide as the combo box itself.
1653 GtkWidget *sample = GTK_WIDGET (combo_box);
1657 if (!gtk_widget_get_has_window (sample))
1659 *x += sample->allocation.x;
1660 *y += sample->allocation.y;
1663 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1665 *width = sample->allocation.width;
1667 hpolicy = vpolicy = GTK_POLICY_NEVER;
1668 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1670 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1672 if (popup_req.width > *width)
1674 hpolicy = GTK_POLICY_ALWAYS;
1675 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1677 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1680 *height = popup_req.height;
1682 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1683 monitor_num = gdk_screen_get_monitor_at_window (screen,
1684 GTK_WIDGET (combo_box)->window);
1685 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1689 else if (*x + *width > monitor.x + monitor.width)
1690 *x = monitor.x + monitor.width - *width;
1692 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1693 *y += sample->allocation.height;
1694 else if (*y - *height >= monitor.y)
1696 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1698 *y += sample->allocation.height;
1699 *height = monitor.y + monitor.height - *y;
1703 *height = *y - monitor.y;
1707 if (popup_req.height > *height)
1709 vpolicy = GTK_POLICY_ALWAYS;
1711 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1717 cell_view_is_sensitive (GtkCellView *cell_view)
1719 GList *cells, *list;
1722 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1725 for (list = cells; list; list = list->next)
1727 g_object_get (list->data, "sensitive", &sensitive, NULL);
1732 g_list_free (cells);
1738 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1741 GtkComboBoxPrivate *priv = combo_box->priv;
1742 GList *cells, *list;
1748 if (priv->row_separator_func)
1750 if (priv->row_separator_func (priv->model, iter,
1751 priv->row_separator_data))
1755 gtk_tree_view_column_cell_set_cell_data (priv->column,
1757 iter, FALSE, FALSE);
1759 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1762 for (list = cells; list; list = list->next)
1764 g_object_get (list->data, "sensitive", &sensitive, NULL);
1769 g_list_free (cells);
1775 update_menu_sensitivity (GtkComboBox *combo_box,
1778 GtkComboBoxPrivate *priv = combo_box->priv;
1779 GList *children, *child;
1780 GtkWidget *item, *submenu, *separator;
1781 GtkWidget *cell_view;
1787 children = gtk_container_get_children (GTK_CONTAINER (menu));
1789 for (child = children; child; child = child->next)
1791 item = GTK_WIDGET (child->data);
1792 cell_view = GTK_BIN (item)->child;
1794 if (!GTK_IS_CELL_VIEW (cell_view))
1797 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1798 if (submenu != NULL)
1800 gtk_widget_set_sensitive (item, TRUE);
1801 update_menu_sensitivity (combo_box, submenu);
1805 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1807 if (menu != priv->popup_widget && child == children)
1809 separator = GTK_WIDGET (child->next->data);
1810 g_object_set (item, "visible", sensitive, NULL);
1811 g_object_set (separator, "visible", sensitive, NULL);
1814 gtk_widget_set_sensitive (item, sensitive);
1818 g_list_free (children);
1822 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1824 guint32 activate_time)
1826 GtkComboBoxPrivate *priv = combo_box->priv;
1829 GtkRequisition requisition;
1832 update_menu_sensitivity (combo_box, priv->popup_widget);
1835 if (gtk_tree_row_reference_valid (priv->active_row))
1837 path = gtk_tree_row_reference_get_path (priv->active_row);
1838 active_item = gtk_tree_path_get_indices (path)[0];
1839 gtk_tree_path_free (path);
1841 if (priv->add_tearoffs)
1845 /* FIXME handle nested menus better */
1846 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1848 if (priv->wrap_width == 0)
1850 width = GTK_WIDGET (combo_box)->allocation.width;
1851 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1852 gtk_widget_size_request (priv->popup_widget, &requisition);
1854 gtk_widget_set_size_request (priv->popup_widget,
1855 MAX (width, requisition.width), -1);
1858 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1860 gtk_combo_box_menu_position, combo_box,
1861 button, activate_time);
1865 popup_grab_on_window (GdkWindow *window,
1866 GdkDevice *keyboard,
1868 guint32 activate_time)
1871 gdk_device_grab (keyboard, window,
1872 GDK_OWNERSHIP_WINDOW, TRUE,
1873 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1874 NULL, activate_time) != GDK_GRAB_SUCCESS)
1878 gdk_device_grab (pointer, window,
1879 GDK_OWNERSHIP_WINDOW, TRUE,
1880 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1881 GDK_POINTER_MOTION_MASK,
1882 NULL, activate_time) != GDK_GRAB_SUCCESS)
1885 gdk_device_ungrab (keyboard, activate_time);
1894 * gtk_combo_box_popup:
1895 * @combo_box: a #GtkComboBox
1897 * Pops up the menu or dropdown list of @combo_box.
1899 * This function is mostly intended for use by accessibility technologies;
1900 * applications should have little use for it.
1905 gtk_combo_box_popup (GtkComboBox *combo_box)
1907 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1909 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1913 * gtk_combo_box_popup_for_device:
1914 * @combo_box: a #GtkComboBox
1915 * @device: a #GdkDevice
1917 * Pops up the menu or dropdown list of @combo_box, the popup window
1918 * will be grabbed so only @device and its associated pointer/keyboard
1919 * are the only #GdkDevice<!-- -->s able to send events to it.
1924 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
1927 GtkComboBoxPrivate *priv = combo_box->priv;
1928 gint x, y, width, height;
1929 GtkTreePath *path = NULL, *ppath;
1930 GtkWidget *toplevel;
1931 GdkDevice *keyboard, *pointer;
1934 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1935 g_return_if_fail (GDK_IS_DEVICE (device));
1937 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
1940 if (gtk_widget_get_mapped (priv->popup_widget))
1943 if (priv->grab_pointer && priv->grab_keyboard)
1946 time = gtk_get_current_event_time ();
1948 if (device->source == GDK_SOURCE_KEYBOARD)
1951 pointer = gdk_device_get_associated_device (device);
1956 keyboard = gdk_device_get_associated_device (device);
1959 if (GTK_IS_MENU (priv->popup_widget))
1961 gtk_combo_box_menu_popup (combo_box,
1962 priv->activate_button,
1963 priv->activate_time);
1967 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1968 if (GTK_IS_WINDOW (toplevel))
1969 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1970 GTK_WINDOW (priv->popup_window));
1972 gtk_widget_show_all (priv->scrolled_window);
1973 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1975 gtk_widget_set_size_request (priv->popup_window, width, height);
1976 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1978 if (gtk_tree_row_reference_valid (priv->active_row))
1980 path = gtk_tree_row_reference_get_path (priv->active_row);
1981 ppath = gtk_tree_path_copy (path);
1982 if (gtk_tree_path_up (ppath))
1983 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1985 gtk_tree_path_free (ppath);
1987 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1991 gtk_widget_show (priv->popup_window);
1995 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1997 gtk_tree_path_free (path);
2000 gtk_widget_grab_focus (priv->popup_window);
2001 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2004 if (!gtk_widget_has_focus (priv->tree_view))
2005 gtk_widget_grab_focus (priv->tree_view);
2007 if (!popup_grab_on_window (priv->popup_window->window,
2008 keyboard, pointer, time))
2010 gtk_widget_hide (priv->popup_window);
2014 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2015 priv->grab_pointer = pointer;
2016 priv->grab_keyboard = keyboard;
2020 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2024 device = gtk_get_current_event_device ();
2028 GdkDeviceManager *device_manager;
2029 GdkDisplay *display;
2032 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2033 device_manager = gdk_display_get_device_manager (display);
2035 /* No device was set, pick the first master device */
2036 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2037 device = devices->data;
2038 g_list_free (devices);
2041 gtk_combo_box_popup_for_device (combo_box, device);
2045 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2047 if (combo_box->priv->popup_shown)
2049 gtk_combo_box_popdown (combo_box);
2057 * gtk_combo_box_popdown:
2058 * @combo_box: a #GtkComboBox
2060 * Hides the menu or dropdown list of @combo_box.
2062 * This function is mostly intended for use by accessibility technologies;
2063 * applications should have little use for it.
2068 gtk_combo_box_popdown (GtkComboBox *combo_box)
2070 GtkComboBoxPrivate *priv = combo_box->priv;
2072 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2074 if (GTK_IS_MENU (priv->popup_widget))
2076 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2080 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2083 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2084 gtk_widget_hide_all (priv->popup_window);
2085 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2088 priv->grab_pointer = NULL;
2089 priv->grab_keyboard = NULL;
2093 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2096 GtkComboBoxPrivate *priv = combo_box->priv;
2100 if (priv->cell_view)
2101 gtk_widget_style_get (priv->cell_view,
2102 "focus-line-width", &padding,
2107 /* add some pixels for good measure */
2108 padding += BONUS_PADDING;
2110 if (priv->cell_view)
2111 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2116 return req.width + padding;
2120 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2122 GtkComboBoxPrivate *priv = combo_box->priv;
2127 !gtk_tree_model_get_iter_first (priv->model, &iter))
2133 path = gtk_tree_path_new_from_indices (0, -1);
2139 if (priv->cell_view)
2140 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2148 priv->width = MAX (priv->width, req.width);
2149 priv->height = MAX (priv->height, req.height);
2151 gtk_tree_path_next (path);
2153 while (gtk_tree_model_iter_next (priv->model, &iter));
2155 gtk_tree_path_free (path);
2159 gtk_combo_box_size_request (GtkWidget *widget,
2160 GtkRequisition *requisition)
2163 gint focus_width, focus_pad;
2166 GtkRequisition bin_req;
2167 PangoContext *context;
2168 PangoFontMetrics *metrics;
2169 PangoFontDescription *font_desc;
2171 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2172 GtkComboBoxPrivate *priv = combo_box->priv;
2175 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2176 gtk_combo_box_remeasure (combo_box);
2177 bin_req.width = MAX (bin_req.width, priv->width);
2178 bin_req.height = MAX (bin_req.height, priv->height);
2180 gtk_widget_style_get (GTK_WIDGET (widget),
2181 "focus-line-width", &focus_width,
2182 "focus-padding", &focus_pad,
2183 "arrow-size", &arrow_size,
2186 font_desc = GTK_BIN (widget)->child->style->font_desc;
2187 context = gtk_widget_get_pango_context (widget);
2188 metrics = pango_context_get_metrics (context, font_desc,
2189 pango_context_get_language (context));
2190 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2191 pango_font_metrics_get_descent (metrics));
2192 pango_font_metrics_unref (metrics);
2194 arrow_size = MAX (arrow_size, font_size);
2196 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2198 if (!priv->tree_view)
2202 if (priv->cell_view)
2204 GtkRequisition button_req, sep_req, arrow_req;
2205 gint border_width, xthickness, ythickness;
2207 gtk_widget_size_request (priv->button, &button_req);
2208 border_width = GTK_CONTAINER (combo_box)->border_width;
2209 xthickness = priv->button->style->xthickness;
2210 ythickness = priv->button->style->ythickness;
2212 bin_req.width = MAX (bin_req.width, priv->width);
2213 bin_req.height = MAX (bin_req.height, priv->height);
2215 gtk_widget_size_request (priv->separator, &sep_req);
2216 gtk_widget_size_request (priv->arrow, &arrow_req);
2218 height = MAX (sep_req.height, arrow_req.height);
2219 height = MAX (height, bin_req.height);
2221 width = bin_req.width + sep_req.width + arrow_req.width;
2223 height += 2*(border_width + ythickness + focus_width + focus_pad);
2224 width += 2*(border_width + xthickness + focus_width + focus_pad);
2226 requisition->width = width;
2227 requisition->height = height;
2231 GtkRequisition but_req;
2233 gtk_widget_size_request (priv->button, &but_req);
2235 requisition->width = bin_req.width + but_req.width;
2236 requisition->height = MAX (bin_req.height, but_req.height);
2242 GtkRequisition button_req, frame_req;
2244 /* sample + frame */
2245 *requisition = bin_req;
2247 requisition->width += 2 * focus_width;
2249 if (priv->cell_view_frame)
2251 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2252 if (priv->has_frame)
2254 requisition->width += 2 *
2255 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2256 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2257 requisition->height += 2 *
2258 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2259 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2264 gtk_widget_size_request (priv->button, &button_req);
2266 requisition->height = MAX (requisition->height, button_req.height);
2267 requisition->width += button_req.width;
2270 if (GTK_SHADOW_NONE != priv->shadow_type)
2272 requisition->height += 2 * widget->style->ythickness;
2273 requisition->width += 2 * widget->style->xthickness;
2277 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2278 gtk_widget_size_request (combo_box->priv->button, &req); \
2281 child.x = allocation->x + shadow_width; \
2283 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2285 child.y = allocation->y + shadow_height; \
2286 child.width = req.width; \
2287 child.height = allocation->height - 2 * shadow_height; \
2288 child.width = MAX (1, child.width); \
2289 child.height = MAX (1, child.height); \
2291 gtk_widget_size_allocate (combo_box->priv->button, &child);
2294 gtk_combo_box_size_allocate (GtkWidget *widget,
2295 GtkAllocation *allocation)
2297 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2298 GtkComboBoxPrivate *priv = combo_box->priv;
2299 gint shadow_width, shadow_height;
2300 gint focus_width, focus_pad;
2301 GtkAllocation child;
2303 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2305 widget->allocation = *allocation;
2307 gtk_widget_style_get (GTK_WIDGET (widget),
2308 "focus-line-width", &focus_width,
2309 "focus-padding", &focus_pad,
2312 if (GTK_SHADOW_NONE != priv->shadow_type)
2314 shadow_width = widget->style->xthickness;
2315 shadow_height = widget->style->ythickness;
2323 if (!priv->tree_view)
2325 if (priv->cell_view)
2327 gint border_width, xthickness, ythickness;
2331 allocation->x += shadow_width;
2332 allocation->y += shadow_height;
2333 allocation->width -= 2 * shadow_width;
2334 allocation->height -= 2 * shadow_height;
2336 gtk_widget_size_allocate (priv->button, allocation);
2338 /* set some things ready */
2339 border_width = GTK_CONTAINER (priv->button)->border_width;
2340 xthickness = priv->button->style->xthickness;
2341 ythickness = priv->button->style->ythickness;
2343 child.x = allocation->x;
2344 child.y = allocation->y;
2345 width = allocation->width;
2346 child.height = allocation->height;
2348 if (!priv->is_cell_renderer)
2350 child.x += border_width + xthickness + focus_width + focus_pad;
2351 child.y += border_width + ythickness + focus_width + focus_pad;
2352 width -= 2 * (child.x - allocation->x);
2353 child.height -= 2 * (child.y - allocation->y);
2357 /* handle the children */
2358 gtk_widget_size_request (priv->arrow, &req);
2359 child.width = req.width;
2361 child.x += width - req.width;
2362 child.width = MAX (1, child.width);
2363 child.height = MAX (1, child.height);
2364 gtk_widget_size_allocate (priv->arrow, &child);
2366 child.x += req.width;
2367 gtk_widget_size_request (priv->separator, &req);
2368 child.width = req.width;
2370 child.x -= req.width;
2371 child.width = MAX (1, child.width);
2372 child.height = MAX (1, child.height);
2373 gtk_widget_size_allocate (priv->separator, &child);
2377 child.x += req.width;
2378 child.width = allocation->x + allocation->width
2379 - (border_width + xthickness + focus_width + focus_pad)
2384 child.width = child.x;
2385 child.x = allocation->x
2386 + border_width + xthickness + focus_width + focus_pad;
2387 child.width -= child.x;
2390 if (gtk_widget_get_visible (priv->popup_widget))
2393 GtkRequisition requisition;
2395 /* Warning here, without the check in the position func */
2396 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2397 if (priv->wrap_width == 0)
2399 width = GTK_WIDGET (combo_box)->allocation.width;
2400 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2401 gtk_widget_size_request (priv->popup_widget, &requisition);
2402 gtk_widget_set_size_request (priv->popup_widget,
2403 MAX (width, requisition.width), -1);
2407 child.width = MAX (1, child.width);
2408 child.height = MAX (1, child.height);
2409 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2413 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2416 child.x = allocation->x + req.width + shadow_width;
2418 child.x = allocation->x + shadow_width;
2419 child.y = allocation->y + shadow_height;
2420 child.width = allocation->width - req.width - 2 * shadow_width;
2421 child.width = MAX (1, child.width);
2422 child.height = MAX (1, child.height);
2423 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2430 /* Combobox thickness + border-width */
2431 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2432 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2435 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2439 child.x = allocation->x + req.width;
2441 child.x = allocation->x;
2443 child.y = allocation->y;
2444 child.width = allocation->width - req.width;
2445 child.height = allocation->height;
2447 if (priv->cell_view_frame)
2451 child.width = MAX (1, child.width - delta_x * 2);
2452 child.height = MAX (1, child.height - delta_y * 2);
2453 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2456 if (priv->has_frame)
2458 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2459 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2460 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2461 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2465 child.width -= delta_x * 2;
2466 child.height -= delta_y * 2;
2473 child.width -= delta_x * 2;
2474 child.height -= delta_y * 2;
2477 if (gtk_widget_get_visible (priv->popup_window))
2479 gint x, y, width, height;
2480 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2481 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2482 gtk_widget_set_size_request (priv->popup_window, width, height);
2486 child.width = MAX (1, child.width);
2487 child.height = MAX (1, child.height);
2489 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2493 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2496 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2498 GtkComboBoxPrivate *priv = combo_box->priv;
2502 g_signal_handler_disconnect (priv->model,
2504 g_signal_handler_disconnect (priv->model,
2506 g_signal_handler_disconnect (priv->model,
2507 priv->reordered_id);
2508 g_signal_handler_disconnect (priv->model,
2513 if (!priv->tree_view)
2515 if (priv->popup_widget)
2516 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2517 (GtkCallback)gtk_widget_destroy, NULL);
2522 g_object_unref (priv->model);
2526 if (priv->active_row)
2528 gtk_tree_row_reference_free (priv->active_row);
2529 priv->active_row = NULL;
2532 if (priv->cell_view)
2533 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2537 gtk_combo_box_forall (GtkContainer *container,
2538 gboolean include_internals,
2539 GtkCallback callback,
2540 gpointer callback_data)
2542 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2543 GtkComboBoxPrivate *priv = combo_box->priv;
2545 if (include_internals)
2548 (* callback) (priv->button, callback_data);
2549 if (priv->cell_view_frame)
2550 (* callback) (priv->cell_view_frame, callback_data);
2553 if (GTK_BIN (container)->child)
2554 (* callback) (GTK_BIN (container)->child, callback_data);
2558 gtk_combo_box_child_show (GtkWidget *widget,
2559 GtkComboBox *combo_box)
2561 GtkComboBoxPrivate *priv = combo_box->priv;
2563 priv->popup_shown = TRUE;
2564 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2568 gtk_combo_box_child_hide (GtkWidget *widget,
2569 GtkComboBox *combo_box)
2571 GtkComboBoxPrivate *priv = combo_box->priv;
2573 priv->popup_shown = FALSE;
2574 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2578 gtk_combo_box_expose_event (GtkWidget *widget,
2579 GdkEventExpose *event)
2581 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2582 GtkComboBoxPrivate *priv = combo_box->priv;
2584 if (gtk_widget_is_drawable (widget) &&
2585 GTK_SHADOW_NONE != priv->shadow_type)
2587 gtk_paint_shadow (widget->style, widget->window,
2588 GTK_STATE_NORMAL, priv->shadow_type,
2589 NULL, widget, "combobox",
2590 widget->allocation.x, widget->allocation.y,
2591 widget->allocation.width, widget->allocation.height);
2594 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2595 priv->button, event);
2597 if (priv->tree_view && priv->cell_view_frame)
2599 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2600 priv->cell_view_frame, event);
2603 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2604 GTK_BIN (widget)->child, event);
2619 path_visible (GtkTreeView *view,
2625 /* Note that we rely on the fact that collapsed rows don't have nodes
2627 return _gtk_tree_view_find_node (view, path, &tree, &node);
2631 tree_next_func (GtkTreeModel *model,
2636 SearchData *search_data = (SearchData *)data;
2638 if (search_data->found)
2640 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2643 if (search_data->visible &&
2644 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2647 search_data->set = TRUE;
2648 search_data->iter = *iter;
2653 if (gtk_tree_path_compare (path, search_data->path) == 0)
2654 search_data->found = TRUE;
2660 tree_next (GtkComboBox *combo,
2661 GtkTreeModel *model,
2666 SearchData search_data;
2668 search_data.combo = combo;
2669 search_data.path = gtk_tree_model_get_path (model, iter);
2670 search_data.visible = visible;
2671 search_data.found = FALSE;
2672 search_data.set = FALSE;
2674 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2676 *next = search_data.iter;
2678 gtk_tree_path_free (search_data.path);
2680 return search_data.set;
2684 tree_prev_func (GtkTreeModel *model,
2689 SearchData *search_data = (SearchData *)data;
2691 if (gtk_tree_path_compare (path, search_data->path) == 0)
2693 search_data->found = TRUE;
2697 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2700 if (search_data->visible &&
2701 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2704 search_data->set = TRUE;
2705 search_data->iter = *iter;
2711 tree_prev (GtkComboBox *combo,
2712 GtkTreeModel *model,
2717 SearchData search_data;
2719 search_data.combo = combo;
2720 search_data.path = gtk_tree_model_get_path (model, iter);
2721 search_data.visible = visible;
2722 search_data.found = FALSE;
2723 search_data.set = FALSE;
2725 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2727 *prev = search_data.iter;
2729 gtk_tree_path_free (search_data.path);
2731 return search_data.set;
2735 tree_last_func (GtkTreeModel *model,
2740 SearchData *search_data = (SearchData *)data;
2742 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2745 /* Note that we rely on the fact that collapsed rows don't have nodes
2747 if (search_data->visible &&
2748 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2751 search_data->set = TRUE;
2752 search_data->iter = *iter;
2758 tree_last (GtkComboBox *combo,
2759 GtkTreeModel *model,
2763 SearchData search_data;
2765 search_data.combo = combo;
2766 search_data.visible = visible;
2767 search_data.set = FALSE;
2769 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2771 *last = search_data.iter;
2773 return search_data.set;
2778 tree_first_func (GtkTreeModel *model,
2783 SearchData *search_data = (SearchData *)data;
2785 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2788 if (search_data->visible &&
2789 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2792 search_data->set = TRUE;
2793 search_data->iter = *iter;
2799 tree_first (GtkComboBox *combo,
2800 GtkTreeModel *model,
2804 SearchData search_data;
2806 search_data.combo = combo;
2807 search_data.visible = visible;
2808 search_data.set = FALSE;
2810 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2812 *first = search_data.iter;
2814 return search_data.set;
2818 gtk_combo_box_scroll_event (GtkWidget *widget,
2819 GdkEventScroll *event)
2821 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2824 GtkTreeIter new_iter;
2826 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2829 if (event->direction == GDK_SCROLL_UP)
2830 found = tree_prev (combo_box, combo_box->priv->model,
2831 &iter, &new_iter, FALSE);
2833 found = tree_next (combo_box, combo_box->priv->model,
2834 &iter, &new_iter, FALSE);
2837 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2847 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2848 GtkCellLayout *cell_layout)
2850 GtkComboBoxPrivate *priv = combo_box->priv;
2853 for (k = priv->cells; k; k = k->next)
2856 ComboCellInfo *info = (ComboCellInfo *)k->data;
2858 if (info->pack == GTK_PACK_START)
2859 gtk_cell_layout_pack_start (cell_layout,
2860 info->cell, info->expand);
2861 else if (info->pack == GTK_PACK_END)
2862 gtk_cell_layout_pack_end (cell_layout,
2863 info->cell, info->expand);
2865 gtk_cell_layout_set_cell_data_func (cell_layout,
2867 combo_cell_data_func, info, NULL);
2869 for (j = info->attributes; j; j = j->next->next)
2871 gtk_cell_layout_add_attribute (cell_layout,
2874 GPOINTER_TO_INT (j->next->data));
2880 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2881 gboolean add_children)
2883 GtkComboBoxPrivate *priv = combo_box->priv;
2886 if (priv->cell_view)
2888 priv->button = gtk_toggle_button_new ();
2889 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2890 priv->focus_on_click);
2892 g_signal_connect (priv->button, "toggled",
2893 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2894 gtk_widget_set_parent (priv->button,
2895 GTK_BIN (combo_box)->child->parent);
2897 priv->box = gtk_hbox_new (FALSE, 0);
2898 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2900 priv->separator = gtk_vseparator_new ();
2901 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2903 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2904 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2906 gtk_widget_show_all (priv->button);
2910 priv->button = gtk_toggle_button_new ();
2911 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2912 priv->focus_on_click);
2914 g_signal_connect (priv->button, "toggled",
2915 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2916 gtk_widget_set_parent (priv->button,
2917 GTK_BIN (combo_box)->child->parent);
2919 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2920 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2921 gtk_widget_show_all (priv->button);
2924 g_signal_connect (priv->button, "button-press-event",
2925 G_CALLBACK (gtk_combo_box_menu_button_press),
2927 g_signal_connect (priv->button, "state-changed",
2928 G_CALLBACK (gtk_combo_box_button_state_changed),
2931 /* create our funky menu */
2932 menu = gtk_menu_new ();
2933 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2934 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2936 g_signal_connect (menu, "key-press-event",
2937 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2938 gtk_combo_box_set_popup_widget (combo_box, menu);
2942 gtk_combo_box_menu_fill (combo_box);
2944 /* the column is needed in tree_column_row_is_sensitive() */
2945 priv->column = gtk_tree_view_column_new ();
2946 g_object_ref_sink (priv->column);
2947 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2949 gtk_combo_box_update_title (combo_box);
2950 gtk_combo_box_update_sensitivity (combo_box);
2954 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2956 GtkComboBoxPrivate *priv = combo_box->priv;
2962 menu = priv->popup_widget;
2964 if (priv->add_tearoffs)
2966 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2968 gtk_widget_show (tearoff);
2970 if (priv->wrap_width)
2971 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2973 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2976 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2980 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2981 GtkTreeModel *model,
2984 GtkWidget *cell_view;
2989 cell_view = gtk_cell_view_new ();
2990 item = gtk_menu_item_new ();
2991 gtk_container_add (GTK_CONTAINER (item), cell_view);
2993 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2994 path = gtk_tree_model_get_path (model, iter);
2995 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2996 gtk_tree_path_free (path);
2998 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2999 gtk_widget_size_request (cell_view, &req);
3000 gtk_widget_show (cell_view);
3006 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
3008 GtkTreeIter *parent)
3010 GtkComboBoxPrivate *priv = combo_box->priv;
3011 GtkTreeModel *model = priv->model;
3012 GtkWidget *item, *submenu, *subitem, *separator;
3014 gboolean is_separator;
3019 n_children = gtk_tree_model_iter_n_children (model, parent);
3022 for (i = 0; i < n_children; i++)
3024 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
3026 if (priv->row_separator_func)
3027 is_separator = priv->row_separator_func (priv->model, &iter,
3028 priv->row_separator_data);
3030 is_separator = FALSE;
3034 item = gtk_separator_menu_item_new ();
3035 path = gtk_tree_model_get_path (model, &iter);
3036 g_object_set_data_full (G_OBJECT (item),
3037 I_("gtk-combo-box-item-path"),
3038 gtk_tree_row_reference_new (model, path),
3039 (GDestroyNotify)gtk_tree_row_reference_free);
3040 gtk_tree_path_free (path);
3044 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3045 if (gtk_tree_model_iter_has_child (model, &iter))
3047 submenu = gtk_menu_new ();
3048 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
3049 gtk_widget_show (submenu);
3050 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
3052 /* Ugly - since menus can only activate leafs, we have to
3053 * duplicate the item inside the submenu.
3055 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3056 separator = gtk_separator_menu_item_new ();
3057 gtk_widget_show (subitem);
3058 gtk_widget_show (separator);
3059 g_signal_connect (subitem, "activate",
3060 G_CALLBACK (gtk_combo_box_menu_item_activate),
3062 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
3063 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
3065 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
3067 g_signal_connect (item, "activate",
3068 G_CALLBACK (gtk_combo_box_menu_item_activate),
3072 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3073 if (priv->wrap_width && menu == priv->popup_widget)
3074 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3075 gtk_widget_show (item);
3082 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3084 GtkComboBoxPrivate *priv = combo_box->priv;
3086 g_signal_handlers_disconnect_matched (priv->button,
3087 G_SIGNAL_MATCH_DATA,
3089 gtk_combo_box_menu_button_press, NULL);
3090 g_signal_handlers_disconnect_matched (priv->button,
3091 G_SIGNAL_MATCH_DATA,
3093 gtk_combo_box_button_state_changed, combo_box);
3095 /* unparent will remove our latest ref */
3096 gtk_widget_unparent (priv->button);
3099 priv->button = NULL;
3101 priv->separator = NULL;
3103 g_object_unref (priv->column);
3104 priv->column = NULL;
3106 /* changing the popup window will unref the menu and the children */
3114 menu_occupied (GtkMenu *menu,
3118 guint bottom_attach)
3122 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3126 gtk_container_child_get (GTK_CONTAINER (menu),
3130 "bottom-attach", &b,
3134 /* look if this item intersects with the given coordinates */
3135 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3143 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3148 GtkComboBoxPrivate *priv = combo_box->priv;
3149 gint current_col = 0, current_row = 0;
3150 gint rows = 1, cols = 1;
3151 GtkWidget *menu = priv->popup_widget;
3153 if (!GTK_IS_MENU_SHELL (menu))
3156 if (priv->col_column == -1 &&
3157 priv->row_column == -1 &&
3160 gtk_container_child_get (GTK_CONTAINER (menu),
3162 "right-attach", ¤t_col,
3163 "top-attach", ¤t_row,
3165 if (current_col + cols > priv->wrap_width)
3173 if (priv->col_column != -1)
3174 gtk_tree_model_get (priv->model, iter,
3175 priv->col_column, &cols,
3177 if (priv->row_column != -1)
3178 gtk_tree_model_get (priv->model, iter,
3179 priv->row_column, &rows,
3184 if (current_col + cols > priv->wrap_width)
3190 if (!menu_occupied (GTK_MENU (menu),
3191 current_col, current_col + cols,
3192 current_row, current_row + rows))
3199 /* set attach props */
3200 gtk_menu_attach (GTK_MENU (menu), item,
3201 current_col, current_col + cols,
3202 current_row, current_row + rows);
3206 gtk_combo_box_relayout (GtkComboBox *combo_box)
3211 menu = combo_box->priv->popup_widget;
3213 /* do nothing unless we are in menu style and realized */
3214 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3217 list = gtk_container_get_children (GTK_CONTAINER (menu));
3219 for (j = g_list_last (list); j; j = j->prev)
3220 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3222 gtk_combo_box_menu_fill (combo_box);
3229 gtk_combo_box_menu_button_press (GtkWidget *widget,
3230 GdkEventButton *event,
3233 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3234 GtkComboBoxPrivate *priv = combo_box->priv;
3236 if (GTK_IS_MENU (priv->popup_widget) &&
3237 event->type == GDK_BUTTON_PRESS && event->button == 1)
3239 if (priv->focus_on_click &&
3240 !gtk_widget_has_focus (priv->button))
3241 gtk_widget_grab_focus (priv->button);
3243 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3252 gtk_combo_box_menu_item_activate (GtkWidget *item,
3255 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3256 GtkWidget *cell_view;
3260 cell_view = GTK_BIN (item)->child;
3262 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3264 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3266 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3268 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3269 gtk_combo_box_set_active_iter (combo_box, &iter);
3272 gtk_tree_path_free (path);
3274 g_object_set (combo_box,
3275 "editing-canceled", FALSE,
3280 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3283 gboolean sensitive = TRUE; /* fool code checkers */
3285 if (!combo_box->priv->button)
3288 switch (combo_box->priv->button_sensitivity)
3290 case GTK_SENSITIVITY_ON:
3293 case GTK_SENSITIVITY_OFF:
3296 case GTK_SENSITIVITY_AUTO:
3297 sensitive = combo_box->priv->model &&
3298 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3301 g_assert_not_reached ();
3305 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3307 /* In list-mode, we also need to update sensitivity of the event box */
3308 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3309 && combo_box->priv->cell_view)
3310 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3314 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3319 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3321 if (combo_box->priv->tree_view)
3322 gtk_combo_box_list_popup_resize (combo_box);
3324 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3326 gtk_combo_box_update_sensitivity (combo_box);
3330 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3334 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3335 GtkComboBoxPrivate *priv = combo_box->priv;
3337 if (!gtk_tree_row_reference_valid (priv->active_row))
3339 if (priv->cell_view)
3340 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3341 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3344 if (priv->tree_view)
3345 gtk_combo_box_list_popup_resize (combo_box);
3347 gtk_combo_box_menu_row_deleted (model, path, user_data);
3349 gtk_combo_box_update_sensitivity (combo_box);
3353 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3359 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3361 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3363 if (!combo_box->priv->tree_view)
3364 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3368 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3373 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3374 GtkComboBoxPrivate *priv = combo_box->priv;
3375 GtkTreePath *active_path;
3377 /* FIXME this belongs to GtkCellView */
3378 if (gtk_tree_row_reference_valid (priv->active_row))
3380 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3381 if (gtk_tree_path_compare (path, active_path) == 0 &&
3383 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3384 gtk_tree_path_free (active_path);
3387 if (priv->tree_view)
3388 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3390 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3394 list_popup_resize_idle (gpointer user_data)
3396 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3397 GtkComboBoxPrivate *priv = combo_box->priv;
3398 gint x, y, width, height;
3400 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3402 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3404 gtk_widget_set_size_request (priv->popup_window, width, height);
3405 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3408 priv->resize_idle_id = 0;
3414 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3416 GtkComboBoxPrivate *priv = combo_box->priv;
3418 if (!priv->resize_idle_id)
3419 priv->resize_idle_id =
3420 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3424 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3429 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3431 gtk_combo_box_list_popup_resize (combo_box);
3436 find_menu_by_path (GtkWidget *menu,
3438 gboolean skip_first)
3443 GtkTreeRowReference *mref;
3447 list = gtk_container_get_children (GTK_CONTAINER (menu));
3450 for (i = list; i; i = i->next)
3452 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3454 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3457 else if (!gtk_tree_row_reference_valid (mref))
3460 mpath = gtk_tree_row_reference_get_path (mref);
3462 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3470 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3475 /* this case is necessary, since the row reference of
3476 * the cell view may already be updated after a deletion
3483 if (gtk_tree_path_compare (mpath, path) == 0)
3485 gtk_tree_path_free (mpath);
3489 if (gtk_tree_path_is_ancestor (mpath, path))
3491 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3492 if (submenu != NULL)
3494 gtk_tree_path_free (mpath);
3495 item = find_menu_by_path (submenu, path, TRUE);
3499 gtk_tree_path_free (mpath);
3509 dump_menu_tree (GtkWidget *menu,
3516 list = gtk_container_get_children (GTK_CONTAINER (menu));
3517 for (i = list; i; i = i->next)
3519 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3521 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3522 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3523 gtk_tree_path_free (path);
3525 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3526 if (submenu != NULL)
3527 dump_menu_tree (submenu, level + 1);
3536 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3541 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3542 GtkComboBoxPrivate *priv = combo_box->priv;
3544 GtkWidget *item, *menu, *separator;
3548 gboolean is_separator;
3550 if (!priv->popup_widget)
3553 depth = gtk_tree_path_get_depth (path);
3554 pos = gtk_tree_path_get_indices (path)[depth - 1];
3557 ppath = gtk_tree_path_copy (path);
3558 gtk_tree_path_up (ppath);
3559 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3560 gtk_tree_path_free (ppath);
3562 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3565 menu = gtk_menu_new ();
3566 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3567 gtk_widget_show (menu);
3568 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3570 /* Ugly - since menus can only activate leaves, we have to
3571 * duplicate the item inside the submenu.
3573 gtk_tree_model_iter_parent (model, &piter, iter);
3574 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3575 separator = gtk_separator_menu_item_new ();
3576 g_signal_connect (item, "activate",
3577 G_CALLBACK (gtk_combo_box_menu_item_activate),
3579 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3580 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3581 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3583 gtk_widget_show (item);
3584 gtk_widget_show (separator);
3591 menu = priv->popup_widget;
3592 if (priv->add_tearoffs)
3596 if (priv->row_separator_func)
3597 is_separator = priv->row_separator_func (model, iter,
3598 priv->row_separator_data);
3600 is_separator = FALSE;
3604 item = gtk_separator_menu_item_new ();
3605 g_object_set_data_full (G_OBJECT (item),
3606 I_("gtk-combo-box-item-path"),
3607 gtk_tree_row_reference_new (model, path),
3608 (GDestroyNotify)gtk_tree_row_reference_free);
3612 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3614 g_signal_connect (item, "activate",
3615 G_CALLBACK (gtk_combo_box_menu_item_activate),
3619 gtk_widget_show (item);
3620 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3624 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3628 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3629 GtkComboBoxPrivate *priv = combo_box->priv;
3633 if (!priv->popup_widget)
3636 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3637 menu = gtk_widget_get_parent (item);
3638 gtk_container_remove (GTK_CONTAINER (menu), item);
3640 if (gtk_tree_path_get_depth (path) > 1)
3642 GtkTreePath *parent_path;
3646 parent_path = gtk_tree_path_copy (path);
3647 gtk_tree_path_up (parent_path);
3648 gtk_tree_model_get_iter (model, &iter, parent_path);
3650 if (!gtk_tree_model_iter_has_child (model, &iter))
3652 parent = find_menu_by_path (priv->popup_widget,
3653 parent_path, FALSE);
3654 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3660 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3666 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3668 gtk_combo_box_relayout (combo_box);
3672 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3677 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3678 GtkComboBoxPrivate *priv = combo_box->priv;
3681 gboolean is_separator;
3683 if (!priv->popup_widget)
3686 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3688 if (priv->row_separator_func)
3689 is_separator = priv->row_separator_func (model, iter,
3690 priv->row_separator_data);
3692 is_separator = FALSE;
3694 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3696 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3697 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3700 if (priv->wrap_width && item->parent == priv->popup_widget)
3702 GtkWidget *pitem = NULL;
3705 prev = gtk_tree_path_copy (path);
3707 if (gtk_tree_path_prev (prev))
3708 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3710 gtk_tree_path_free (prev);
3712 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3714 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3719 "bottom-attach", -1,
3722 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3725 width = gtk_combo_box_calc_requested_width (combo_box, path);
3727 if (width > priv->width)
3729 if (priv->cell_view)
3731 gtk_widget_set_size_request (priv->cell_view, width, -1);
3732 gtk_widget_queue_resize (priv->cell_view);
3734 priv->width = width;
3743 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3745 GtkComboBoxPrivate *priv = combo_box->priv;
3746 GtkTreeSelection *sel;
3748 GtkWidget *widget = GTK_WIDGET (combo_box);
3750 priv->button = gtk_toggle_button_new ();
3751 gtk_widget_set_parent (priv->button,
3752 GTK_BIN (combo_box)->child->parent);
3753 g_signal_connect (priv->button, "button-press-event",
3754 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3755 g_signal_connect (priv->button, "toggled",
3756 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3758 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3759 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3760 priv->separator = NULL;
3761 gtk_widget_show_all (priv->button);
3763 if (priv->cell_view)
3765 style = gtk_widget_get_style (widget);
3766 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3767 &style->base[gtk_widget_get_state (widget)]);
3769 priv->box = gtk_event_box_new ();
3770 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3773 if (priv->has_frame)
3775 priv->cell_view_frame = gtk_frame_new (NULL);
3776 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3781 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3782 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3786 gtk_widget_set_parent (priv->cell_view_frame,
3787 GTK_BIN (combo_box)->child->parent);
3788 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3789 gtk_widget_show_all (priv->cell_view_frame);
3791 g_signal_connect (priv->box, "button-press-event",
3792 G_CALLBACK (gtk_combo_box_list_button_pressed),
3796 priv->tree_view = gtk_tree_view_new ();
3797 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3798 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3799 gtk_tree_selection_set_select_function (sel,
3800 gtk_combo_box_list_select_func,
3802 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3804 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3806 if (priv->row_separator_func)
3807 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3808 priv->row_separator_func,
3809 priv->row_separator_data,
3812 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3814 priv->column = gtk_tree_view_column_new ();
3815 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3818 gtk_combo_box_sync_cells (combo_box,
3819 GTK_CELL_LAYOUT (priv->column));
3821 if (gtk_tree_row_reference_valid (priv->active_row))
3825 path = gtk_tree_row_reference_get_path (priv->active_row);
3826 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3828 gtk_tree_path_free (path);
3831 /* set sample/popup widgets */
3832 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3834 g_signal_connect (priv->tree_view, "key-press-event",
3835 G_CALLBACK (gtk_combo_box_list_key_press),
3837 g_signal_connect (priv->tree_view, "enter-notify-event",
3838 G_CALLBACK (gtk_combo_box_list_enter_notify),
3840 g_signal_connect (priv->tree_view, "row-expanded",
3841 G_CALLBACK (gtk_combo_box_model_row_expanded),
3843 g_signal_connect (priv->tree_view, "row-collapsed",
3844 G_CALLBACK (gtk_combo_box_model_row_expanded),
3846 g_signal_connect (priv->popup_window, "button-press-event",
3847 G_CALLBACK (gtk_combo_box_list_button_pressed),
3849 g_signal_connect (priv->popup_window, "button-release-event",
3850 G_CALLBACK (gtk_combo_box_list_button_released),
3853 gtk_widget_show (priv->tree_view);
3855 gtk_combo_box_update_sensitivity (combo_box);
3859 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3861 GtkComboBoxPrivate *priv = combo_box->priv;
3863 /* disconnect signals */
3864 g_signal_handlers_disconnect_matched (priv->tree_view,
3865 G_SIGNAL_MATCH_DATA,
3866 0, 0, NULL, NULL, combo_box);
3867 g_signal_handlers_disconnect_matched (priv->button,
3868 G_SIGNAL_MATCH_DATA,
3870 gtk_combo_box_list_button_pressed,
3872 g_signal_handlers_disconnect_matched (priv->popup_window,
3873 G_SIGNAL_MATCH_DATA,
3875 gtk_combo_box_list_button_pressed,
3877 g_signal_handlers_disconnect_matched (priv->popup_window,
3878 G_SIGNAL_MATCH_DATA,
3880 gtk_combo_box_list_button_released,
3883 g_signal_handlers_disconnect_matched (priv->popup_window,
3884 G_SIGNAL_MATCH_DATA,
3886 gtk_combo_box_child_show,
3889 g_signal_handlers_disconnect_matched (priv->popup_window,
3890 G_SIGNAL_MATCH_DATA,
3892 gtk_combo_box_child_hide,
3896 g_signal_handlers_disconnect_matched (priv->box,
3897 G_SIGNAL_MATCH_DATA,
3899 gtk_combo_box_list_button_pressed,
3902 /* destroy things (unparent will kill the latest ref from us)
3903 * last unref on button will destroy the arrow
3905 gtk_widget_unparent (priv->button);
3906 priv->button = NULL;
3909 if (priv->cell_view)
3911 g_object_set (priv->cell_view,
3912 "background-set", FALSE,
3916 if (priv->cell_view_frame)
3918 gtk_widget_unparent (priv->cell_view_frame);
3919 priv->cell_view_frame = NULL;
3923 if (priv->scroll_timer)
3925 g_source_remove (priv->scroll_timer);
3926 priv->scroll_timer = 0;
3929 if (priv->resize_idle_id)
3931 g_source_remove (priv->resize_idle_id);
3932 priv->resize_idle_id = 0;
3935 gtk_widget_destroy (priv->tree_view);
3937 priv->tree_view = NULL;
3938 if (priv->popup_widget)
3940 g_object_unref (priv->popup_widget);
3941 priv->popup_widget = NULL;
3948 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3949 GdkEventButton *event,
3952 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3953 GtkComboBoxPrivate *priv = combo_box->priv;
3955 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3957 if (ewidget == priv->popup_window)
3960 if ((ewidget != priv->button && ewidget != priv->box) ||
3961 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3964 if (priv->focus_on_click &&
3965 !gtk_widget_has_focus (priv->button))
3966 gtk_widget_grab_focus (priv->button);
3968 gtk_combo_box_popup_for_device (combo_box, event->device);
3970 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3972 priv->auto_scroll = FALSE;
3973 if (priv->scroll_timer == 0)
3974 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3975 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3978 priv->popup_in_progress = TRUE;
3984 gtk_combo_box_list_button_released (GtkWidget *widget,
3985 GdkEventButton *event,
3989 GtkTreePath *path = NULL;
3992 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3993 GtkComboBoxPrivate *priv = combo_box->priv;
3995 gboolean popup_in_progress = FALSE;
3997 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3999 if (priv->popup_in_progress)
4001 popup_in_progress = TRUE;
4002 priv->popup_in_progress = FALSE;
4005 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
4007 if (priv->scroll_timer)
4009 g_source_remove (priv->scroll_timer);
4010 priv->scroll_timer = 0;
4013 if (ewidget != priv->tree_view)
4015 if ((ewidget == priv->button ||
4016 ewidget == priv->box) &&
4017 !popup_in_progress &&
4018 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4020 gtk_combo_box_popdown (combo_box);
4024 /* released outside treeview */
4025 if (ewidget != priv->button && ewidget != priv->box)
4027 gtk_combo_box_popdown (combo_box);
4035 /* select something cool */
4036 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
4042 return TRUE; /* clicked outside window? */
4044 gtk_tree_model_get_iter (priv->model, &iter, path);
4045 gtk_tree_path_free (path);
4047 gtk_combo_box_popdown (combo_box);
4049 if (tree_column_row_is_sensitive (combo_box, &iter))
4050 gtk_combo_box_set_active_iter (combo_box, &iter);
4056 gtk_combo_box_menu_key_press (GtkWidget *widget,
4060 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4062 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4064 /* The menu hasn't managed the
4065 * event, forward it to the combobox
4067 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4074 gtk_combo_box_list_key_press (GtkWidget *widget,
4078 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4081 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
4082 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
4084 GtkTreeModel *model = NULL;
4086 gtk_combo_box_popdown (combo_box);
4088 if (combo_box->priv->model)
4090 GtkTreeSelection *sel;
4092 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4094 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4095 gtk_combo_box_set_active_iter (combo_box, &iter);
4101 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4103 /* The list hasn't managed the
4104 * event, forward it to the combobox
4106 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4113 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4117 GtkWidget *tree_view = combo_box->priv->tree_view;
4121 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4122 if (adj && adj->upper - adj->lower > adj->page_size)
4124 if (x <= tree_view->allocation.x &&
4125 adj->lower < adj->value)
4127 value = adj->value - (tree_view->allocation.x - x + 1);
4128 gtk_adjustment_set_value (adj, value);
4130 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4131 adj->upper - adj->page_size > adj->value)
4133 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4134 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4138 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4139 if (adj && adj->upper - adj->lower > adj->page_size)
4141 if (y <= tree_view->allocation.y &&
4142 adj->lower < adj->value)
4144 value = adj->value - (tree_view->allocation.y - y + 1);
4145 gtk_adjustment_set_value (adj, value);
4147 else if (y >= tree_view->allocation.height &&
4148 adj->upper - adj->page_size > adj->value)
4150 value = adj->value + (y - tree_view->allocation.height + 1);
4151 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4157 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4159 GtkComboBoxPrivate *priv = combo_box->priv;
4162 if (priv->auto_scroll)
4164 gdk_window_get_device_position (priv->tree_view->window,
4167 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4174 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4175 GdkEventCrossing *event,
4178 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4180 combo_box->priv->auto_scroll = TRUE;
4186 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4187 GtkTreeModel *model,
4189 gboolean path_currently_selected,
4193 gboolean sensitive = FALSE;
4195 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4197 GList *cells, *cell;
4198 gboolean cell_sensitive, cell_visible;
4200 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4202 if (!column->visible)
4205 gtk_tree_model_get_iter (model, &iter, path);
4206 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4209 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4212 g_object_get (cell->data,
4213 "sensitive", &cell_sensitive,
4214 "visible", &cell_visible,
4217 if (cell_visible && cell_sensitive)
4222 g_list_free (cells);
4224 sensitive = cell_sensitive;
4231 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4236 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4237 GtkComboBoxPrivate *priv = combo_box->priv;
4240 width = gtk_combo_box_calc_requested_width (combo_box, path);
4242 if (width > priv->width)
4244 if (priv->cell_view)
4246 gtk_widget_set_size_request (priv->cell_view, width, -1);
4247 gtk_widget_queue_resize (priv->cell_view);
4249 priv->width = width;
4254 * GtkCellLayout implementation
4258 pack_start_recurse (GtkWidget *menu,
4259 GtkCellRenderer *cell,
4265 list = gtk_container_get_children (GTK_CONTAINER (menu));
4266 for (i = list; i; i = i->next)
4268 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4269 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4272 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4273 if (submenu != NULL)
4274 pack_start_recurse (submenu, cell, expand);
4281 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4282 GtkCellRenderer *cell,
4285 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4286 ComboCellInfo *info;
4287 GtkComboBoxPrivate *priv;
4289 priv = combo_box->priv;
4291 g_object_ref_sink (cell);
4293 info = g_slice_new0 (ComboCellInfo);
4295 info->expand = expand;
4296 info->pack = GTK_PACK_START;
4298 priv->cells = g_slist_append (priv->cells, info);
4300 if (priv->cell_view)
4301 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4305 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4307 if (GTK_IS_MENU (priv->popup_widget))
4308 pack_start_recurse (priv->popup_widget, cell, expand);
4312 pack_end_recurse (GtkWidget *menu,
4313 GtkCellRenderer *cell,
4319 list = gtk_container_get_children (GTK_CONTAINER (menu));
4320 for (i = list; i; i = i->next)
4322 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4323 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4326 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4327 if (submenu != NULL)
4328 pack_end_recurse (submenu, cell, expand);
4335 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4336 GtkCellRenderer *cell,
4339 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4340 ComboCellInfo *info;
4341 GtkComboBoxPrivate *priv;
4343 priv = combo_box->priv;
4345 g_object_ref_sink (cell);
4347 info = g_slice_new0 (ComboCellInfo);
4349 info->expand = expand;
4350 info->pack = GTK_PACK_END;
4352 priv->cells = g_slist_append (priv->cells, info);
4354 if (priv->cell_view)
4355 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4359 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4361 if (GTK_IS_MENU (priv->popup_widget))
4362 pack_end_recurse (priv->popup_widget, cell, expand);
4366 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4368 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4370 GList *retval = NULL;
4372 for (list = combo_box->priv->cells; list; list = list->next)
4374 ComboCellInfo *info = (ComboCellInfo *)list->data;
4376 retval = g_list_prepend (retval, info->cell);
4379 return g_list_reverse (retval);
4383 clear_recurse (GtkWidget *menu)
4388 list = gtk_container_get_children (GTK_CONTAINER (menu));
4389 for (i = list; i; i = i->next)
4391 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4392 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4394 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4395 if (submenu != NULL)
4396 clear_recurse (submenu);
4403 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4405 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4406 GtkComboBoxPrivate *priv = combo_box->priv;
4409 if (priv->cell_view)
4410 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4413 gtk_tree_view_column_clear (priv->column);
4415 for (i = priv->cells; i; i = i->next)
4417 ComboCellInfo *info = (ComboCellInfo *)i->data;
4419 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4420 g_object_unref (info->cell);
4421 g_slice_free (ComboCellInfo, info);
4424 g_slist_free (priv->cells);
4427 if (GTK_IS_MENU (priv->popup_widget))
4428 clear_recurse (priv->popup_widget);
4432 add_attribute_recurse (GtkWidget *menu,
4433 GtkCellRenderer *cell,
4434 const gchar *attribute,
4440 list = gtk_container_get_children (GTK_CONTAINER (menu));
4441 for (i = list; i; i = i->next)
4443 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4444 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4445 cell, attribute, column);
4447 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4448 if (submenu != NULL)
4449 add_attribute_recurse (submenu, cell, attribute, column);
4456 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4457 GtkCellRenderer *cell,
4458 const gchar *attribute,
4461 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4462 ComboCellInfo *info;
4464 info = gtk_combo_box_get_cell_info (combo_box, cell);
4465 g_return_if_fail (info != NULL);
4467 info->attributes = g_slist_prepend (info->attributes,
4468 GINT_TO_POINTER (column));
4469 info->attributes = g_slist_prepend (info->attributes,
4470 g_strdup (attribute));
4472 if (combo_box->priv->cell_view)
4473 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4474 cell, attribute, column);
4476 if (combo_box->priv->column)
4477 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4478 cell, attribute, column);
4480 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4481 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4482 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4486 combo_cell_data_func (GtkCellLayout *cell_layout,
4487 GtkCellRenderer *cell,
4488 GtkTreeModel *tree_model,
4492 ComboCellInfo *info = (ComboCellInfo *)data;
4493 GtkWidget *parent = NULL;
4498 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4500 if (GTK_IS_WIDGET (cell_layout))
4501 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4503 if (GTK_IS_MENU_ITEM (parent) &&
4504 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4505 g_object_set (cell, "sensitive", TRUE, NULL);
4510 set_cell_data_func_recurse (GtkWidget *menu,
4511 GtkCellRenderer *cell,
4512 ComboCellInfo *info)
4516 GtkWidget *cell_view;
4518 list = gtk_container_get_children (GTK_CONTAINER (menu));
4519 for (i = list; i; i = i->next)
4521 cell_view = GTK_BIN (i->data)->child;
4522 if (GTK_IS_CELL_LAYOUT (cell_view))
4524 /* Override sensitivity for inner nodes; we don't
4525 * want menuitems with submenus to appear insensitive
4527 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4529 combo_cell_data_func,
4531 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4532 if (submenu != NULL)
4533 set_cell_data_func_recurse (submenu, cell, info);
4541 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4542 GtkCellRenderer *cell,
4543 GtkCellLayoutDataFunc func,
4545 GDestroyNotify destroy)
4547 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4548 GtkComboBoxPrivate *priv = combo_box->priv;
4549 ComboCellInfo *info;
4551 info = gtk_combo_box_get_cell_info (combo_box, cell);
4552 g_return_if_fail (info != NULL);
4556 GDestroyNotify d = info->destroy;
4558 info->destroy = NULL;
4559 d (info->func_data);
4563 info->func_data = func_data;
4564 info->destroy = destroy;
4566 if (priv->cell_view)
4567 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4570 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4572 if (GTK_IS_MENU (priv->popup_widget))
4573 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4575 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4579 clear_attributes_recurse (GtkWidget *menu,
4580 GtkCellRenderer *cell)
4585 list = gtk_container_get_children (GTK_CONTAINER (menu));
4586 for (i = list; i; i = i->next)
4588 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4589 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4592 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4593 if (submenu != NULL)
4594 clear_attributes_recurse (submenu, cell);
4601 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4602 GtkCellRenderer *cell)
4604 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4605 GtkComboBoxPrivate *priv;
4606 ComboCellInfo *info;
4609 priv = combo_box->priv;
4611 info = gtk_combo_box_get_cell_info (combo_box, cell);
4612 g_return_if_fail (info != NULL);
4614 list = info->attributes;
4615 while (list && list->next)
4617 g_free (list->data);
4618 list = list->next->next;
4620 g_slist_free (info->attributes);
4621 info->attributes = NULL;
4623 if (priv->cell_view)
4624 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4627 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4629 if (GTK_IS_MENU (priv->popup_widget))
4630 clear_attributes_recurse (priv->popup_widget, cell);
4632 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4636 reorder_recurse (GtkWidget *menu,
4637 GtkCellRenderer *cell,
4643 list = gtk_container_get_children (GTK_CONTAINER (menu));
4644 for (i = list; i; i = i->next)
4646 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4647 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4650 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4651 if (submenu != NULL)
4652 reorder_recurse (submenu, cell, position);
4659 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4660 GtkCellRenderer *cell,
4663 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4664 GtkComboBoxPrivate *priv;
4665 ComboCellInfo *info;
4668 priv = combo_box->priv;
4670 info = gtk_combo_box_get_cell_info (combo_box, cell);
4672 g_return_if_fail (info != NULL);
4673 g_return_if_fail (position >= 0);
4675 link = g_slist_find (priv->cells, info);
4677 g_return_if_fail (link != NULL);
4679 priv->cells = g_slist_delete_link (priv->cells, link);
4680 priv->cells = g_slist_insert (priv->cells, info, position);
4682 if (priv->cell_view)
4683 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4687 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4690 if (GTK_IS_MENU (priv->popup_widget))
4691 reorder_recurse (priv->popup_widget, cell, position);
4693 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4701 * gtk_combo_box_new:
4703 * Creates a new empty #GtkComboBox.
4705 * Return value: A new #GtkComboBox.
4710 gtk_combo_box_new (void)
4712 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4716 * gtk_combo_box_new_with_model:
4717 * @model: A #GtkTreeModel.
4719 * Creates a new #GtkComboBox with the model initialized to @model.
4721 * Return value: A new #GtkComboBox.
4726 gtk_combo_box_new_with_model (GtkTreeModel *model)
4728 GtkComboBox *combo_box;
4730 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4732 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4734 return GTK_WIDGET (combo_box);
4738 * gtk_combo_box_get_wrap_width:
4739 * @combo_box: A #GtkComboBox
4741 * Returns the wrap width which is used to determine the number of columns
4742 * for the popup menu. If the wrap width is larger than 1, the combo box
4745 * Returns: the wrap width.
4750 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4752 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4754 return combo_box->priv->wrap_width;
4758 * gtk_combo_box_set_wrap_width:
4759 * @combo_box: A #GtkComboBox
4760 * @width: Preferred number of columns
4762 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4763 * the preferred number of columns when you want the popup to be layed out
4769 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4772 GtkComboBoxPrivate *priv;
4774 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4775 g_return_if_fail (width >= 0);
4777 priv = combo_box->priv;
4779 if (width != priv->wrap_width)
4781 priv->wrap_width = width;
4783 gtk_combo_box_check_appearance (combo_box);
4784 gtk_combo_box_relayout (combo_box);
4786 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4791 * gtk_combo_box_get_row_span_column:
4792 * @combo_box: A #GtkComboBox
4794 * Returns the column with row span information for @combo_box.
4796 * Returns: the row span column.
4801 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4803 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4805 return combo_box->priv->row_column;
4809 * gtk_combo_box_set_row_span_column:
4810 * @combo_box: A #GtkComboBox.
4811 * @row_span: A column in the model passed during construction.
4813 * Sets the column with row span information for @combo_box to be @row_span.
4814 * The row span column contains integers which indicate how many rows
4815 * an item should span.
4820 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4823 GtkComboBoxPrivate *priv;
4826 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4828 priv = combo_box->priv;
4830 col = gtk_tree_model_get_n_columns (priv->model);
4831 g_return_if_fail (row_span >= -1 && row_span < col);
4833 if (row_span != priv->row_column)
4835 priv->row_column = row_span;
4837 gtk_combo_box_relayout (combo_box);
4839 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4844 * gtk_combo_box_get_column_span_column:
4845 * @combo_box: A #GtkComboBox
4847 * Returns the column with column span information for @combo_box.
4849 * Returns: the column span column.
4854 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4856 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4858 return combo_box->priv->col_column;
4862 * gtk_combo_box_set_column_span_column:
4863 * @combo_box: A #GtkComboBox
4864 * @column_span: A column in the model passed during construction
4866 * Sets the column with column span information for @combo_box to be
4867 * @column_span. The column span column contains integers which indicate
4868 * how many columns an item should span.
4873 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4876 GtkComboBoxPrivate *priv;
4879 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4881 priv = combo_box->priv;
4883 col = gtk_tree_model_get_n_columns (priv->model);
4884 g_return_if_fail (column_span >= -1 && column_span < col);
4886 if (column_span != priv->col_column)
4888 priv->col_column = column_span;
4890 gtk_combo_box_relayout (combo_box);
4892 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4897 * gtk_combo_box_get_active:
4898 * @combo_box: A #GtkComboBox
4900 * Returns the index of the currently active item, or -1 if there's no
4901 * active item. If the model is a non-flat treemodel, and the active item
4902 * is not an immediate child of the root of the tree, this function returns
4903 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4904 * <literal>path</literal> is the #GtkTreePath of the active item.
4906 * Return value: An integer which is the index of the currently active item,
4907 * or -1 if there's no active item.
4912 gtk_combo_box_get_active (GtkComboBox *combo_box)
4914 GtkComboBoxPrivate *priv;
4917 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4919 priv = combo_box->priv;
4921 if (gtk_tree_row_reference_valid (priv->active_row))
4925 path = gtk_tree_row_reference_get_path (priv->active_row);
4926 result = gtk_tree_path_get_indices (path)[0];
4927 gtk_tree_path_free (path);
4936 * gtk_combo_box_set_active:
4937 * @combo_box: A #GtkComboBox
4938 * @index_: An index in the model passed during construction, or -1 to have
4941 * Sets the active item of @combo_box to be the item at @index.
4946 gtk_combo_box_set_active (GtkComboBox *combo_box,
4949 GtkTreePath *path = NULL;
4950 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4951 g_return_if_fail (index_ >= -1);
4953 if (combo_box->priv->model == NULL)
4955 /* Save index, in case the model is set after the index */
4956 combo_box->priv->active = index_;
4962 path = gtk_tree_path_new_from_indices (index_, -1);
4964 gtk_combo_box_set_active_internal (combo_box, path);
4967 gtk_tree_path_free (path);
4971 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4974 GtkComboBoxPrivate *priv = combo_box->priv;
4975 GtkTreePath *active_path;
4978 /* Remember whether the initially active row is valid. */
4979 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4981 if (path && is_valid_row_reference)
4983 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4984 path_cmp = gtk_tree_path_compare (path, active_path);
4985 gtk_tree_path_free (active_path);
4990 if (priv->active_row)
4992 gtk_tree_row_reference_free (priv->active_row);
4993 priv->active_row = NULL;
4998 if (priv->tree_view)
4999 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
5002 GtkMenu *menu = GTK_MENU (priv->popup_widget);
5004 if (GTK_IS_MENU (menu))
5005 gtk_menu_set_active (menu, -1);
5008 if (priv->cell_view)
5009 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
5012 * Do not emit a "changed" signal when an already invalid selection was
5013 * now set to invalid.
5015 if (!is_valid_row_reference)
5021 gtk_tree_row_reference_new (priv->model, path);
5023 if (priv->tree_view)
5025 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
5028 else if (GTK_IS_MENU (priv->popup_widget))
5030 /* FIXME handle nested menus better */
5031 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
5032 gtk_tree_path_get_indices (path)[0]);
5035 if (priv->cell_view)
5036 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
5040 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
5041 g_object_notify (G_OBJECT (combo_box), "active");
5046 * gtk_combo_box_get_active_iter:
5047 * @combo_box: A #GtkComboBox
5048 * @iter: (out): The uninitialized #GtkTreeIter
5050 * Sets @iter to point to the current active item, if it exists.
5052 * Return value: %TRUE, if @iter was set
5057 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
5063 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5065 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
5068 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
5069 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
5070 gtk_tree_path_free (path);
5076 * gtk_combo_box_set_active_iter:
5077 * @combo_box: A #GtkComboBox
5078 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5080 * Sets the current active item to be the one referenced by @iter, or
5081 * unsets the active item if @iter is %NULL.
5083 * @iter must correspond to a path of depth one, or be %NULL.
5088 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5091 GtkTreePath *path = NULL;
5093 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5096 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5098 gtk_combo_box_set_active_internal (combo_box, path);
5099 gtk_tree_path_free (path);
5103 * gtk_combo_box_set_model:
5104 * @combo_box: A #GtkComboBox
5105 * @model: (allow-none): A #GtkTreeModel
5107 * Sets the model used by @combo_box to be @model. Will unset a previously set
5108 * model (if applicable). If model is %NULL, then it will unset the model.
5110 * Note that this function does not clear the cell renderers, you have to
5111 * call gtk_cell_layout_clear() yourself if you need to set up different
5112 * cell renderers for the new model.
5117 gtk_combo_box_set_model (GtkComboBox *combo_box,
5118 GtkTreeModel *model)
5120 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5121 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5123 if (model == combo_box->priv->model)
5126 gtk_combo_box_unset_model (combo_box);
5131 combo_box->priv->model = model;
5132 g_object_ref (combo_box->priv->model);
5134 combo_box->priv->inserted_id =
5135 g_signal_connect (combo_box->priv->model, "row-inserted",
5136 G_CALLBACK (gtk_combo_box_model_row_inserted),
5138 combo_box->priv->deleted_id =
5139 g_signal_connect (combo_box->priv->model, "row-deleted",
5140 G_CALLBACK (gtk_combo_box_model_row_deleted),
5142 combo_box->priv->reordered_id =
5143 g_signal_connect (combo_box->priv->model, "rows-reordered",
5144 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5146 combo_box->priv->changed_id =
5147 g_signal_connect (combo_box->priv->model, "row-changed",
5148 G_CALLBACK (gtk_combo_box_model_row_changed),
5151 if (combo_box->priv->tree_view)
5154 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5155 combo_box->priv->model);
5156 gtk_combo_box_list_popup_resize (combo_box);
5161 if (combo_box->priv->popup_widget)
5162 gtk_combo_box_menu_fill (combo_box);
5166 if (combo_box->priv->cell_view)
5167 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5168 combo_box->priv->model);
5170 if (combo_box->priv->active != -1)
5172 /* If an index was set in advance, apply it now */
5173 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5174 combo_box->priv->active = -1;
5178 gtk_combo_box_update_sensitivity (combo_box);
5180 g_object_notify (G_OBJECT (combo_box), "model");
5184 * gtk_combo_box_get_model
5185 * @combo_box: A #GtkComboBox
5187 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5189 * Return value: (transfer none): A #GtkTreeModel which was passed during construction.
5194 gtk_combo_box_get_model (GtkComboBox *combo_box)
5196 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5198 return combo_box->priv->model;
5202 /* convenience API for simple text combos */
5205 * gtk_combo_box_new_text:
5207 * Convenience function which constructs a new text combo box, which is a
5208 * #GtkComboBox just displaying strings. If you use this function to create
5209 * a text combo box, you should only manipulate its data source with the
5210 * following convenience functions: gtk_combo_box_append_text(),
5211 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5212 * gtk_combo_box_remove_text().
5214 * Return value: (transfer none): A new text combo box.
5219 gtk_combo_box_new_text (void)
5221 GtkWidget *combo_box;
5222 GtkCellRenderer *cell;
5223 GtkListStore *store;
5225 store = gtk_list_store_new (1, G_TYPE_STRING);
5226 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5227 g_object_unref (store);
5229 cell = gtk_cell_renderer_text_new ();
5230 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5231 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5239 * gtk_combo_box_append_text:
5240 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5243 * Appends @string to the list of strings stored in @combo_box. Note that
5244 * you can only use this function with combo boxes constructed with
5245 * gtk_combo_box_new_text().
5250 gtk_combo_box_append_text (GtkComboBox *combo_box,
5254 GtkListStore *store;
5256 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5257 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5258 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5260 g_return_if_fail (text != NULL);
5262 store = GTK_LIST_STORE (combo_box->priv->model);
5264 gtk_list_store_append (store, &iter);
5265 gtk_list_store_set (store, &iter, 0, text, -1);
5269 * gtk_combo_box_insert_text:
5270 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5271 * @position: An index to insert @text
5274 * Inserts @string at @position in the list of strings stored in @combo_box.
5275 * Note that you can only use this function with combo boxes constructed
5276 * with gtk_combo_box_new_text().
5281 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5286 GtkListStore *store;
5288 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5289 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5290 g_return_if_fail (position >= 0);
5291 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5293 g_return_if_fail (text != NULL);
5295 store = GTK_LIST_STORE (combo_box->priv->model);
5297 gtk_list_store_insert (store, &iter, position);
5298 gtk_list_store_set (store, &iter, 0, text, -1);
5302 * gtk_combo_box_prepend_text:
5303 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5306 * Prepends @string to the list of strings stored in @combo_box. Note that
5307 * you can only use this function with combo boxes constructed with
5308 * gtk_combo_box_new_text().
5313 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5317 GtkListStore *store;
5319 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5320 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5321 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5323 g_return_if_fail (text != NULL);
5325 store = GTK_LIST_STORE (combo_box->priv->model);
5327 gtk_list_store_prepend (store, &iter);
5328 gtk_list_store_set (store, &iter, 0, text, -1);
5332 * gtk_combo_box_remove_text:
5333 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5334 * @position: Index of the item to remove
5336 * Removes the string at @position from @combo_box. Note that you can only use
5337 * this function with combo boxes constructed with gtk_combo_box_new_text().
5342 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5346 GtkListStore *store;
5348 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5349 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5350 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5352 g_return_if_fail (position >= 0);
5354 store = GTK_LIST_STORE (combo_box->priv->model);
5356 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5358 gtk_list_store_remove (store, &iter);
5362 * gtk_combo_box_get_active_text:
5363 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5365 * Returns the currently active string in @combo_box or %NULL if none
5366 * is selected. Note that you can only use this function with combo
5367 * boxes constructed with gtk_combo_box_new_text() and with
5368 * #GtkComboBoxEntry<!-- -->s.
5370 * Returns: a newly allocated string containing the currently active text.
5371 * Must be freed with g_free().
5376 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5378 GtkComboBoxClass *class;
5380 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5382 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5384 if (class->get_active_text)
5385 return class->get_active_text (combo_box);
5391 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5396 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5397 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5398 == G_TYPE_STRING, NULL);
5400 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5401 gtk_tree_model_get (combo_box->priv->model, &iter,
5408 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5409 GtkScrollType scroll)
5412 GtkTreeIter new_iter;
5413 gboolean active_iter;
5416 if (!combo_box->priv->model)
5418 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5422 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5426 case GTK_SCROLL_STEP_BACKWARD:
5427 case GTK_SCROLL_STEP_UP:
5428 case GTK_SCROLL_STEP_LEFT:
5431 found = tree_prev (combo_box, combo_box->priv->model,
5432 &iter, &new_iter, FALSE);
5435 /* else fall through */
5437 case GTK_SCROLL_PAGE_FORWARD:
5438 case GTK_SCROLL_PAGE_DOWN:
5439 case GTK_SCROLL_PAGE_RIGHT:
5440 case GTK_SCROLL_END:
5441 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5444 case GTK_SCROLL_STEP_FORWARD:
5445 case GTK_SCROLL_STEP_DOWN:
5446 case GTK_SCROLL_STEP_RIGHT:
5449 found = tree_next (combo_box, combo_box->priv->model,
5450 &iter, &new_iter, FALSE);
5453 /* else fall through */
5455 case GTK_SCROLL_PAGE_BACKWARD:
5456 case GTK_SCROLL_PAGE_UP:
5457 case GTK_SCROLL_PAGE_LEFT:
5458 case GTK_SCROLL_START:
5459 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5466 if (found && active_iter)
5468 GtkTreePath *old_path;
5469 GtkTreePath *new_path;
5471 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5472 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5474 if (gtk_tree_path_compare (old_path, new_path) == 0)
5477 gtk_tree_path_free (old_path);
5478 gtk_tree_path_free (new_path);
5483 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5487 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5492 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5493 gboolean group_cycling)
5495 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5497 gtk_widget_grab_focus (combo_box->priv->button);
5503 gtk_combo_box_grab_focus (GtkWidget *widget)
5505 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5507 gtk_widget_grab_focus (combo_box->priv->button);
5511 gtk_combo_box_destroy (GtkObject *object)
5513 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5515 if (combo_box->priv->popup_idle_id > 0)
5517 g_source_remove (combo_box->priv->popup_idle_id);
5518 combo_box->priv->popup_idle_id = 0;
5521 gtk_combo_box_popdown (combo_box);
5523 if (combo_box->priv->row_separator_destroy)
5524 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5526 combo_box->priv->row_separator_func = NULL;
5527 combo_box->priv->row_separator_data = NULL;
5528 combo_box->priv->row_separator_destroy = NULL;
5530 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5531 combo_box->priv->cell_view = NULL;
5535 gtk_combo_box_dispose(GObject* object)
5537 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5539 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5541 gtk_combo_box_menu_destroy (combo_box);
5542 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5543 combo_box->priv->popup_widget = NULL;
5546 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5550 gtk_combo_box_finalize (GObject *object)
5552 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5555 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5556 gtk_combo_box_list_destroy (combo_box);
5558 if (combo_box->priv->popup_window)
5559 gtk_widget_destroy (combo_box->priv->popup_window);
5561 gtk_combo_box_unset_model (combo_box);
5563 for (i = combo_box->priv->cells; i; i = i->next)
5565 ComboCellInfo *info = (ComboCellInfo *)i->data;
5566 GSList *list = info->attributes;
5569 info->destroy (info->func_data);
5571 while (list && list->next)
5573 g_free (list->data);
5574 list = list->next->next;
5576 g_slist_free (info->attributes);
5578 g_object_unref (info->cell);
5579 g_slice_free (ComboCellInfo, info);
5581 g_slist_free (combo_box->priv->cells);
5583 g_free (combo_box->priv->tearoff_title);
5585 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5589 gtk_cell_editable_key_press (GtkWidget *widget,
5593 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5595 if (event->keyval == GDK_Escape)
5597 g_object_set (combo_box,
5598 "editing-canceled", TRUE,
5600 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5601 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5605 else if (event->keyval == GDK_Return ||
5606 event->keyval == GDK_ISO_Enter ||
5607 event->keyval == GDK_KP_Enter)
5609 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5610 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5619 popdown_idle (gpointer data)
5621 GtkComboBox *combo_box;
5623 combo_box = GTK_COMBO_BOX (data);
5625 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5626 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5628 g_object_unref (combo_box);
5634 popdown_handler (GtkWidget *widget,
5637 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5641 popup_idle (gpointer data)
5643 GtkComboBox *combo_box;
5645 combo_box = GTK_COMBO_BOX (data);
5647 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5648 combo_box->priv->cell_view)
5649 g_signal_connect_object (combo_box->priv->popup_widget,
5650 "unmap", G_CALLBACK (popdown_handler),
5653 /* we unset this if a menu item is activated */
5654 g_object_set (combo_box,
5655 "editing-canceled", TRUE,
5657 gtk_combo_box_popup (combo_box);
5659 combo_box->priv->popup_idle_id = 0;
5660 combo_box->priv->activate_button = 0;
5661 combo_box->priv->activate_time = 0;
5667 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5670 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5672 combo_box->priv->is_cell_renderer = TRUE;
5674 if (combo_box->priv->cell_view)
5676 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5677 G_CALLBACK (gtk_cell_editable_key_press),
5680 gtk_widget_grab_focus (combo_box->priv->button);
5684 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5685 G_CALLBACK (gtk_cell_editable_key_press),
5688 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5689 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5692 /* we do the immediate popup only for the optionmenu-like
5695 if (combo_box->priv->is_cell_renderer &&
5696 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5698 if (event && event->type == GDK_BUTTON_PRESS)
5700 GdkEventButton *event_button = (GdkEventButton *)event;
5702 combo_box->priv->activate_button = event_button->button;
5703 combo_box->priv->activate_time = event_button->time;
5706 combo_box->priv->popup_idle_id =
5707 gdk_threads_add_idle (popup_idle, combo_box);
5713 * gtk_combo_box_get_add_tearoffs:
5714 * @combo_box: a #GtkComboBox
5716 * Gets the current value of the :add-tearoffs property.
5718 * Return value: the current value of the :add-tearoffs property.
5721 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5723 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5725 return combo_box->priv->add_tearoffs;
5729 * gtk_combo_box_set_add_tearoffs:
5730 * @combo_box: a #GtkComboBox
5731 * @add_tearoffs: %TRUE to add tearoff menu items
5733 * Sets whether the popup menu should have a tearoff
5739 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5740 gboolean add_tearoffs)
5742 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5744 add_tearoffs = add_tearoffs != FALSE;
5746 if (combo_box->priv->add_tearoffs != add_tearoffs)
5748 combo_box->priv->add_tearoffs = add_tearoffs;
5749 gtk_combo_box_check_appearance (combo_box);
5750 gtk_combo_box_relayout (combo_box);
5751 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5756 * gtk_combo_box_get_title:
5757 * @combo_box: a #GtkComboBox
5759 * Gets the current title of the menu in tearoff mode. See
5760 * gtk_combo_box_set_add_tearoffs().
5762 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5763 * string which must not be freed.
5767 G_CONST_RETURN gchar*
5768 gtk_combo_box_get_title (GtkComboBox *combo_box)
5770 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5772 return combo_box->priv->tearoff_title;
5776 gtk_combo_box_update_title (GtkComboBox *combo_box)
5778 gtk_combo_box_check_appearance (combo_box);
5780 if (combo_box->priv->popup_widget &&
5781 GTK_IS_MENU (combo_box->priv->popup_widget))
5782 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5783 combo_box->priv->tearoff_title);
5787 * gtk_combo_box_set_title:
5788 * @combo_box: a #GtkComboBox
5789 * @title: a title for the menu in tearoff mode
5791 * Sets the menu's title in tearoff mode.
5796 gtk_combo_box_set_title (GtkComboBox *combo_box,
5799 GtkComboBoxPrivate *priv;
5801 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5803 priv = combo_box->priv;
5805 if (strcmp (title ? title : "",
5806 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5808 g_free (priv->tearoff_title);
5809 priv->tearoff_title = g_strdup (title);
5811 gtk_combo_box_update_title (combo_box);
5813 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5818 * gtk_combo_box_get_popup_accessible:
5819 * @combo_box: a #GtkComboBox
5821 * Gets the accessible object corresponding to the combo box's popup.
5823 * This function is mostly intended for use by accessibility technologies;
5824 * applications should have little use for it.
5826 * Returns: the accessible object corresponding to the combo box's popup.
5831 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5835 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5837 if (combo_box->priv->popup_widget)
5839 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5847 * gtk_combo_box_get_row_separator_func:
5848 * @combo_box: a #GtkComboBox
5850 * Returns the current row separator function.
5852 * Return value: the current row separator function.
5856 GtkTreeViewRowSeparatorFunc
5857 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5859 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5861 return combo_box->priv->row_separator_func;
5865 * gtk_combo_box_set_row_separator_func:
5866 * @combo_box: a #GtkComboBox
5867 * @func: a #GtkTreeViewRowSeparatorFunc
5868 * @data: (allow-none): user data to pass to @func, or %NULL
5869 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5871 * Sets the row separator function, which is used to determine
5872 * whether a row should be drawn as a separator. If the row separator
5873 * function is %NULL, no separators are drawn. This is the default value.
5878 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5879 GtkTreeViewRowSeparatorFunc func,
5881 GDestroyNotify destroy)
5883 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5885 if (combo_box->priv->row_separator_destroy)
5886 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5888 combo_box->priv->row_separator_func = func;
5889 combo_box->priv->row_separator_data = data;
5890 combo_box->priv->row_separator_destroy = destroy;
5892 if (combo_box->priv->tree_view)
5893 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5896 gtk_combo_box_relayout (combo_box);
5898 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5902 * gtk_combo_box_set_button_sensitivity:
5903 * @combo_box: a #GtkComboBox
5904 * @sensitivity: specify the sensitivity of the dropdown button
5906 * Sets whether the dropdown button of the combo box should be
5907 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5908 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5913 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5914 GtkSensitivityType sensitivity)
5916 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5918 if (combo_box->priv->button_sensitivity != sensitivity)
5920 combo_box->priv->button_sensitivity = sensitivity;
5921 gtk_combo_box_update_sensitivity (combo_box);
5923 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5928 * gtk_combo_box_get_button_sensitivity:
5929 * @combo_box: a #GtkComboBox
5931 * Returns whether the combo box sets the dropdown button
5932 * sensitive or not when there are no items in the model.
5934 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5935 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5936 * if the button is always insensitive or
5937 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5938 * the model has one item to be selected.
5943 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5945 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5947 return combo_box->priv->button_sensitivity;
5952 * gtk_combo_box_set_focus_on_click:
5953 * @combo: a #GtkComboBox
5954 * @focus_on_click: whether the combo box grabs focus when clicked
5957 * Sets whether the combo box will grab focus when it is clicked with
5958 * the mouse. Making mouse clicks not grab focus is useful in places
5959 * like toolbars where you don't want the keyboard focus removed from
5960 * the main area of the application.
5965 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5966 gboolean focus_on_click)
5968 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5970 focus_on_click = focus_on_click != FALSE;
5972 if (combo_box->priv->focus_on_click != focus_on_click)
5974 combo_box->priv->focus_on_click = focus_on_click;
5976 if (combo_box->priv->button)
5977 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5980 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5985 * gtk_combo_box_get_focus_on_click:
5986 * @combo: a #GtkComboBox
5988 * Returns whether the combo box grabs focus when it is clicked
5989 * with the mouse. See gtk_combo_box_set_focus_on_click().
5991 * Return value: %TRUE if the combo box grabs focus when it is
5992 * clicked with the mouse.
5997 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5999 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6001 return combo_box->priv->focus_on_click;
6006 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
6007 GtkBuilder *builder,
6009 const gchar *tagname,
6010 GMarkupParser *parser,
6013 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6014 tagname, parser, data))
6017 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6018 tagname, parser, data);
6022 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
6023 GtkBuilder *builder,
6025 const gchar *tagname,
6028 if (strcmp (tagname, "attributes") == 0)
6029 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
6032 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,