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;
72 struct _GtkComboBoxPrivate
80 GtkShadowType shadow_type;
82 gint active; /* Only temporary */
83 GtkTreeRowReference *active_row;
86 GtkTreeViewColumn *column;
89 GtkWidget *cell_view_frame;
96 GtkWidget *popup_widget;
97 GtkWidget *popup_window;
98 GtkWidget *scrolled_window;
105 guint activate_button;
106 guint32 activate_time;
108 guint resize_idle_id;
114 guint popup_in_progress : 1;
115 guint popup_shown : 1;
116 guint add_tearoffs : 1;
118 guint is_cell_renderer : 1;
119 guint editing_canceled : 1;
120 guint auto_scroll : 1;
121 guint focus_on_click : 1;
122 guint button_sensitivity : 2;
124 GtkTreeViewRowSeparatorFunc row_separator_func;
125 gpointer row_separator_data;
126 GDestroyNotify row_separator_destroy;
128 GdkDevice *grab_pointer;
129 GdkDevice *grab_keyboard;
131 gchar *tearoff_title;
134 /* While debugging this evil code, I have learned that
135 * there are actually 4 modes to this widget, which can
136 * be characterized as follows
138 * 1) menu mode, no child added
141 * cell_view -> GtkCellView, regular child
142 * cell_view_frame -> NULL
143 * button -> GtkToggleButton set_parent to combo
144 * arrow -> GtkArrow set_parent to button
145 * separator -> GtkVSepator set_parent to button
146 * popup_widget -> GtkMenu
147 * popup_window -> NULL
148 * scrolled_window -> NULL
150 * 2) menu mode, child added
154 * cell_view_frame -> NULL
155 * button -> GtkToggleButton set_parent to combo
156 * arrow -> GtkArrow, child of button
158 * popup_widget -> GtkMenu
159 * popup_window -> NULL
160 * scrolled_window -> NULL
162 * 3) list mode, no child added
164 * tree_view -> GtkTreeView, child of scrolled_window
165 * cell_view -> GtkCellView, regular child
166 * cell_view_frame -> GtkFrame, set parent to combo
167 * button -> GtkToggleButton, set_parent to combo
168 * arrow -> GtkArrow, child of button
170 * popup_widget -> tree_view
171 * popup_window -> GtkWindow
172 * scrolled_window -> GtkScrolledWindow, child of popup_window
174 * 4) list mode, child added
176 * tree_view -> GtkTreeView, child of scrolled_window
178 * cell_view_frame -> NULL
179 * button -> GtkToggleButton, set_parent to combo
180 * arrow -> GtkArrow, child of button
182 * popup_widget -> tree_view
183 * popup_window -> GtkWindow
184 * scrolled_window -> GtkScrolledWindow, child of popup_window
200 PROP_ROW_SPAN_COLUMN,
201 PROP_COLUMN_SPAN_COLUMN,
208 PROP_BUTTON_SENSITIVITY,
209 PROP_EDITING_CANCELED
212 static guint combo_box_signals[LAST_SIGNAL] = {0,};
214 #define BONUS_PADDING 4
215 #define SCROLL_TIME 100
219 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
220 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
221 static void gtk_combo_box_dispose (GObject *object);
222 static void gtk_combo_box_finalize (GObject *object);
223 static void gtk_combo_box_destroy (GtkObject *object);
225 static void gtk_combo_box_set_property (GObject *object,
229 static void gtk_combo_box_get_property (GObject *object,
234 static void gtk_combo_box_state_changed (GtkWidget *widget,
235 GtkStateType previous);
236 static void gtk_combo_box_grab_focus (GtkWidget *widget);
237 static void gtk_combo_box_style_set (GtkWidget *widget,
239 static void gtk_combo_box_button_toggled (GtkWidget *widget,
241 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
242 GtkStateType previous,
244 static void gtk_combo_box_add (GtkContainer *container,
246 static void gtk_combo_box_remove (GtkContainer *container,
249 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
250 GtkCellRenderer *cell);
252 static void gtk_combo_box_menu_show (GtkWidget *menu,
254 static void gtk_combo_box_menu_hide (GtkWidget *menu,
257 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
259 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
264 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
269 static void gtk_combo_box_menu_position (GtkMenu *menu,
275 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
277 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
279 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
281 static void gtk_combo_box_size_request (GtkWidget *widget,
282 GtkRequisition *requisition);
283 static void gtk_combo_box_size_allocate (GtkWidget *widget,
284 GtkAllocation *allocation);
285 static void gtk_combo_box_forall (GtkContainer *container,
286 gboolean include_internals,
287 GtkCallback callback,
288 gpointer callback_data);
289 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
290 GdkEventExpose *event);
291 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
292 GdkEventScroll *event);
293 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
296 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
297 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
298 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
299 GtkScrollType scroll);
300 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
301 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
303 /* listening to the model */
304 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
308 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
311 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
316 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
320 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
326 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
331 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
332 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
334 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
335 GdkEventButton *event,
337 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
340 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
341 GdkEventCrossing *event,
343 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
346 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
347 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
348 GdkEventButton *event,
351 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
354 gboolean path_currently_selected,
357 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
361 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
364 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
365 gboolean add_children);
366 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
367 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
370 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
371 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
373 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
377 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
379 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
380 GdkEventButton *event,
382 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
385 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
386 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
390 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
393 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
398 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
402 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
405 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
407 guint32 activate_time);
408 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
413 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
414 GtkCellRenderer *cell,
416 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
417 GtkCellRenderer *cell,
419 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
420 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
421 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
422 GtkCellRenderer *cell,
423 const gchar *attribute,
425 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
426 GtkCellRenderer *cell,
427 GtkCellLayoutDataFunc func,
429 GDestroyNotify destroy);
430 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
431 GtkCellRenderer *cell);
432 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
433 GtkCellRenderer *cell,
435 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
436 gboolean group_cycling);
438 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
439 GtkCellLayout *cell_layout);
440 static void combo_cell_data_func (GtkCellLayout *cell_layout,
441 GtkCellRenderer *cell,
442 GtkTreeModel *tree_model,
445 static void gtk_combo_box_child_show (GtkWidget *widget,
446 GtkComboBox *combo_box);
447 static void gtk_combo_box_child_hide (GtkWidget *widget,
448 GtkComboBox *combo_box);
450 /* GtkBuildable method implementation */
451 static GtkBuildableIface *parent_buildable_iface;
453 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
454 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
457 const gchar *tagname,
458 GMarkupParser *parser,
460 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
463 const gchar *tagname,
466 /* GtkCellEditable method implementations */
467 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
471 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
472 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
473 gtk_combo_box_cell_layout_init)
474 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
475 gtk_combo_box_cell_editable_init)
476 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
477 gtk_combo_box_buildable_init))
482 gtk_combo_box_class_init (GtkComboBoxClass *klass)
484 GObjectClass *object_class;
485 GtkObjectClass *gtk_object_class;
486 GtkContainerClass *container_class;
487 GtkWidgetClass *widget_class;
488 GtkBindingSet *binding_set;
490 klass->get_active_text = gtk_combo_box_real_get_active_text;
492 container_class = (GtkContainerClass *)klass;
493 container_class->forall = gtk_combo_box_forall;
494 container_class->add = gtk_combo_box_add;
495 container_class->remove = gtk_combo_box_remove;
497 widget_class = (GtkWidgetClass *)klass;
498 widget_class->size_allocate = gtk_combo_box_size_allocate;
499 widget_class->size_request = gtk_combo_box_size_request;
500 widget_class->expose_event = gtk_combo_box_expose_event;
501 widget_class->scroll_event = gtk_combo_box_scroll_event;
502 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
503 widget_class->grab_focus = gtk_combo_box_grab_focus;
504 widget_class->style_set = gtk_combo_box_style_set;
505 widget_class->state_changed = gtk_combo_box_state_changed;
507 gtk_object_class = (GtkObjectClass *)klass;
508 gtk_object_class->destroy = gtk_combo_box_destroy;
510 object_class = (GObjectClass *)klass;
511 object_class->dispose = gtk_combo_box_dispose;
512 object_class->finalize = gtk_combo_box_finalize;
513 object_class->set_property = gtk_combo_box_set_property;
514 object_class->get_property = gtk_combo_box_get_property;
518 * GtkComboBox::changed:
519 * @widget: the object which received the signal
521 * The changed signal is emitted when the active
522 * item is changed. The can be due to the user selecting
523 * a different item from the list, or due to a
524 * call to gtk_combo_box_set_active_iter().
525 * It will also be emitted while typing into a GtkComboBoxEntry,
526 * as well as when selecting an item from the GtkComboBoxEntry's list.
530 combo_box_signals[CHANGED] =
531 g_signal_new (I_("changed"),
532 G_OBJECT_CLASS_TYPE (klass),
534 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
536 g_cclosure_marshal_VOID__VOID,
539 * GtkComboBox::move-active:
540 * @widget: the object that received the signal
541 * @scroll_type: a #GtkScrollType
543 * The ::move-active signal is a
544 * <link linkend="keybinding-signals">keybinding signal</link>
545 * which gets emitted to move the active selection.
549 combo_box_signals[MOVE_ACTIVE] =
550 g_signal_new_class_handler (I_("move-active"),
551 G_OBJECT_CLASS_TYPE (klass),
552 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
553 G_CALLBACK (gtk_combo_box_real_move_active),
555 g_cclosure_marshal_VOID__ENUM,
557 GTK_TYPE_SCROLL_TYPE);
560 * GtkComboBox::popup:
561 * @widget: the object that received the signal
563 * The ::popup signal is a
564 * <link linkend="keybinding-signals">keybinding signal</link>
565 * which gets emitted to popup the combo box list.
567 * The default binding for this signal is Alt+Down.
571 combo_box_signals[POPUP] =
572 g_signal_new_class_handler (I_("popup"),
573 G_OBJECT_CLASS_TYPE (klass),
574 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
575 G_CALLBACK (gtk_combo_box_real_popup),
577 g_cclosure_marshal_VOID__VOID,
580 * GtkComboBox::popdown:
581 * @button: the object which received the signal
583 * The ::popdown signal is a
584 * <link linkend="keybinding-signals">keybinding signal</link>
585 * which gets emitted to popdown the combo box list.
587 * The default bindings for this signal are Alt+Up and Escape.
591 combo_box_signals[POPDOWN] =
592 g_signal_new_class_handler (I_("popdown"),
593 G_OBJECT_CLASS_TYPE (klass),
594 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
595 G_CALLBACK (gtk_combo_box_real_popdown),
597 _gtk_marshal_BOOLEAN__VOID,
601 binding_set = gtk_binding_set_by_class (widget_class);
603 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
608 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
610 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
612 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
615 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
617 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
618 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
620 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
621 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
623 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
624 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
626 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
627 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
629 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
630 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
632 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
634 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
636 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
637 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
639 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
640 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
642 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
643 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
645 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
646 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
648 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
649 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
651 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
654 g_object_class_override_property (object_class,
655 PROP_EDITING_CANCELED,
661 * The model from which the combo box takes the values shown
666 g_object_class_install_property (object_class,
668 g_param_spec_object ("model",
669 P_("ComboBox model"),
670 P_("The model for the combo box"),
672 GTK_PARAM_READWRITE));
675 * GtkComboBox:wrap-width:
677 * If wrap-width is set to a positive value, the list will be
678 * displayed in multiple columns, the number of columns is
679 * determined by wrap-width.
683 g_object_class_install_property (object_class,
685 g_param_spec_int ("wrap-width",
687 P_("Wrap width for laying out the items in a grid"),
691 GTK_PARAM_READWRITE));
695 * GtkComboBox:row-span-column:
697 * If this is set to a non-negative value, it must be the index of a column
698 * of type %G_TYPE_INT in the model.
700 * The values of that column are used to determine how many rows a value in
701 * the list will span. Therefore, the values in the model column pointed to
702 * by this property must be greater than zero and not larger than wrap-width.
706 g_object_class_install_property (object_class,
707 PROP_ROW_SPAN_COLUMN,
708 g_param_spec_int ("row-span-column",
709 P_("Row span column"),
710 P_("TreeModel column containing the row span values"),
714 GTK_PARAM_READWRITE));
718 * GtkComboBox:column-span-column:
720 * If this is set to a non-negative value, it must be the index of a column
721 * of type %G_TYPE_INT in the model.
723 * The values of that column are used to determine how many columns a value
724 * in the list will span.
728 g_object_class_install_property (object_class,
729 PROP_COLUMN_SPAN_COLUMN,
730 g_param_spec_int ("column-span-column",
731 P_("Column span column"),
732 P_("TreeModel column containing the column span values"),
736 GTK_PARAM_READWRITE));
740 * GtkComboBox:active:
742 * The item which is currently active. If the model is a non-flat treemodel,
743 * and the active item is not an immediate child of the root of the tree,
744 * this property has the value
745 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
746 * where <literal>path</literal> is the #GtkTreePath of the active item.
750 g_object_class_install_property (object_class,
752 g_param_spec_int ("active",
754 P_("The item which is currently active"),
758 GTK_PARAM_READWRITE));
761 * GtkComboBox:add-tearoffs:
763 * The add-tearoffs property controls whether generated menus
764 * have tearoff menu items.
766 * Note that this only affects menu style combo boxes.
770 g_object_class_install_property (object_class,
772 g_param_spec_boolean ("add-tearoffs",
773 P_("Add tearoffs to menus"),
774 P_("Whether dropdowns should have a tearoff menu item"),
776 GTK_PARAM_READWRITE));
779 * GtkComboBox:has-frame:
781 * The has-frame property controls whether a frame
782 * is drawn around the entry.
786 g_object_class_install_property (object_class,
788 g_param_spec_boolean ("has-frame",
790 P_("Whether the combo box draws a frame around the child"),
792 GTK_PARAM_READWRITE));
794 g_object_class_install_property (object_class,
796 g_param_spec_boolean ("focus-on-click",
797 P_("Focus on click"),
798 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
800 GTK_PARAM_READWRITE));
803 * GtkComboBox:tearoff-title:
805 * A title that may be displayed by the window manager
806 * when the popup is torn-off.
810 g_object_class_install_property (object_class,
812 g_param_spec_string ("tearoff-title",
814 P_("A title that may be displayed by the window manager when the popup is torn-off"),
816 GTK_PARAM_READWRITE));
820 * GtkComboBox:popup-shown:
822 * Whether the combo boxes dropdown is popped up.
823 * Note that this property is mainly useful, because
824 * it allows you to connect to notify::popup-shown.
828 g_object_class_install_property (object_class,
830 g_param_spec_boolean ("popup-shown",
832 P_("Whether the combo's dropdown is shown"),
834 GTK_PARAM_READABLE));
838 * GtkComboBox:button-sensitivity:
840 * Whether the dropdown button is sensitive when
841 * the model is empty.
845 g_object_class_install_property (object_class,
846 PROP_BUTTON_SENSITIVITY,
847 g_param_spec_enum ("button-sensitivity",
848 P_("Button Sensitivity"),
849 P_("Whether the dropdown button is sensitive when the model is empty"),
850 GTK_TYPE_SENSITIVITY_TYPE,
851 GTK_SENSITIVITY_AUTO,
852 GTK_PARAM_READWRITE));
854 gtk_widget_class_install_style_property (widget_class,
855 g_param_spec_boolean ("appears-as-list",
856 P_("Appears as list"),
857 P_("Whether dropdowns should look like lists rather than menus"),
859 GTK_PARAM_READABLE));
862 * GtkComboBox:arrow-size:
864 * Sets the minimum size of the arrow in the combo box. Note
865 * that the arrow size is coupled to the font size, so in case
866 * a larger font is used, the arrow will be larger than set
871 gtk_widget_class_install_style_property (widget_class,
872 g_param_spec_int ("arrow-size",
874 P_("The minimum size of the arrow in the combo box"),
878 GTK_PARAM_READABLE));
881 * GtkComboBox:shadow-type:
883 * Which kind of shadow to draw around the combo box.
887 gtk_widget_class_install_style_property (widget_class,
888 g_param_spec_enum ("shadow-type",
890 P_("Which kind of shadow to draw around the combo box"),
891 GTK_TYPE_SHADOW_TYPE,
893 GTK_PARAM_READABLE));
895 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
899 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
901 parent_buildable_iface = g_type_interface_peek_parent (iface);
902 iface->add_child = _gtk_cell_layout_buildable_add_child;
903 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
904 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
908 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
910 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
911 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
912 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
913 iface->clear = gtk_combo_box_cell_layout_clear;
914 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
915 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
916 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
917 iface->reorder = gtk_combo_box_cell_layout_reorder;
921 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
923 iface->start_editing = gtk_combo_box_start_editing;
927 gtk_combo_box_init (GtkComboBox *combo_box)
929 GtkComboBoxPrivate *priv;
931 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
934 priv = combo_box->priv;
936 priv->cell_view = gtk_cell_view_new ();
937 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
938 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
939 gtk_widget_show (priv->cell_view);
943 priv->wrap_width = 0;
946 priv->active_row = NULL;
947 priv->col_column = -1;
948 priv->row_column = -1;
950 priv->popup_shown = FALSE;
951 priv->add_tearoffs = FALSE;
952 priv->has_frame = TRUE;
953 priv->is_cell_renderer = FALSE;
954 priv->editing_canceled = FALSE;
955 priv->auto_scroll = FALSE;
956 priv->focus_on_click = TRUE;
957 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
959 gtk_combo_box_check_appearance (combo_box);
963 gtk_combo_box_set_property (GObject *object,
968 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
973 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
976 case PROP_WRAP_WIDTH:
977 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
980 case PROP_ROW_SPAN_COLUMN:
981 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
984 case PROP_COLUMN_SPAN_COLUMN:
985 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
989 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
992 case PROP_ADD_TEAROFFS:
993 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
997 combo_box->priv->has_frame = g_value_get_boolean (value);
1000 case PROP_FOCUS_ON_CLICK:
1001 gtk_combo_box_set_focus_on_click (combo_box,
1002 g_value_get_boolean (value));
1005 case PROP_TEAROFF_TITLE:
1006 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1009 case PROP_POPUP_SHOWN:
1010 if (g_value_get_boolean (value))
1011 gtk_combo_box_popup (combo_box);
1013 gtk_combo_box_popdown (combo_box);
1016 case PROP_BUTTON_SENSITIVITY:
1017 gtk_combo_box_set_button_sensitivity (combo_box,
1018 g_value_get_enum (value));
1021 case PROP_EDITING_CANCELED:
1022 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1026 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1032 gtk_combo_box_get_property (GObject *object,
1037 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1038 GtkComboBoxPrivate *priv = combo_box->priv;
1043 g_value_set_object (value, combo_box->priv->model);
1046 case PROP_WRAP_WIDTH:
1047 g_value_set_int (value, combo_box->priv->wrap_width);
1050 case PROP_ROW_SPAN_COLUMN:
1051 g_value_set_int (value, combo_box->priv->row_column);
1054 case PROP_COLUMN_SPAN_COLUMN:
1055 g_value_set_int (value, combo_box->priv->col_column);
1059 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1062 case PROP_ADD_TEAROFFS:
1063 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1066 case PROP_HAS_FRAME:
1067 g_value_set_boolean (value, combo_box->priv->has_frame);
1070 case PROP_FOCUS_ON_CLICK:
1071 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1074 case PROP_TEAROFF_TITLE:
1075 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1078 case PROP_POPUP_SHOWN:
1079 g_value_set_boolean (value, combo_box->priv->popup_shown);
1082 case PROP_BUTTON_SENSITIVITY:
1083 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1086 case PROP_EDITING_CANCELED:
1087 g_value_set_boolean (value, priv->editing_canceled);
1091 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1097 gtk_combo_box_state_changed (GtkWidget *widget,
1098 GtkStateType previous)
1100 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1101 GtkComboBoxPrivate *priv = combo_box->priv;
1103 if (gtk_widget_get_realized (widget))
1105 if (priv->tree_view && priv->cell_view)
1106 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1107 &widget->style->base[gtk_widget_get_state (widget)]);
1110 gtk_widget_queue_draw (widget);
1114 gtk_combo_box_button_state_changed (GtkWidget *widget,
1115 GtkStateType previous,
1118 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1119 GtkComboBoxPrivate *priv = combo_box->priv;
1121 if (gtk_widget_get_realized (widget))
1123 if (!priv->tree_view && priv->cell_view)
1125 if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) !=
1126 (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE))
1127 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1129 gtk_widget_set_state (priv->cell_view,
1130 gtk_widget_get_state (widget));
1134 gtk_widget_queue_draw (widget);
1138 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1140 GtkComboBoxPrivate *priv = combo_box->priv;
1141 gboolean appears_as_list;
1143 /* if wrap_width > 0, then we are in grid-mode and forced to use
1146 if (priv->wrap_width)
1147 appears_as_list = FALSE;
1149 gtk_widget_style_get (GTK_WIDGET (combo_box),
1150 "appears-as-list", &appears_as_list,
1153 if (appears_as_list)
1155 /* Destroy all the menu mode widgets, if they exist. */
1156 if (GTK_IS_MENU (priv->popup_widget))
1157 gtk_combo_box_menu_destroy (combo_box);
1159 /* Create the list mode widgets, if they don't already exist. */
1160 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1161 gtk_combo_box_list_setup (combo_box);
1165 /* Destroy all the list mode widgets, if they exist. */
1166 if (GTK_IS_TREE_VIEW (priv->tree_view))
1167 gtk_combo_box_list_destroy (combo_box);
1169 /* Create the menu mode widgets, if they don't already exist. */
1170 if (!GTK_IS_MENU (priv->popup_widget))
1171 gtk_combo_box_menu_setup (combo_box, TRUE);
1174 gtk_widget_style_get (GTK_WIDGET (combo_box),
1175 "shadow-type", &priv->shadow_type,
1180 gtk_combo_box_style_set (GtkWidget *widget,
1183 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1184 GtkComboBoxPrivate *priv = combo_box->priv;
1187 gtk_combo_box_check_appearance (combo_box);
1189 if (priv->tree_view && priv->cell_view)
1190 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1191 &widget->style->base[gtk_widget_get_state (widget)]);
1193 child = gtk_bin_get_child (GTK_BIN (combo_box));
1194 if (GTK_IS_ENTRY (child))
1195 g_object_set (child, "shadow-type",
1196 GTK_SHADOW_NONE == priv->shadow_type ?
1197 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1201 gtk_combo_box_button_toggled (GtkWidget *widget,
1204 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1206 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1208 if (!combo_box->priv->popup_in_progress)
1209 gtk_combo_box_popup (combo_box);
1212 gtk_combo_box_popdown (combo_box);
1216 gtk_combo_box_add (GtkContainer *container,
1219 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1220 GtkComboBoxPrivate *priv = combo_box->priv;
1222 if (priv->cell_view && priv->cell_view->parent)
1224 gtk_widget_unparent (priv->cell_view);
1225 _gtk_bin_set_child (GTK_BIN (container), NULL);
1226 gtk_widget_queue_resize (GTK_WIDGET (container));
1229 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1230 _gtk_bin_set_child (GTK_BIN (container), widget);
1232 if (priv->cell_view &&
1233 widget != priv->cell_view)
1235 /* since the cell_view was unparented, it's gone now */
1236 priv->cell_view = NULL;
1238 if (!priv->tree_view && priv->separator)
1240 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1242 priv->separator = NULL;
1244 gtk_widget_queue_resize (GTK_WIDGET (container));
1246 else if (priv->cell_view_frame)
1248 gtk_widget_unparent (priv->cell_view_frame);
1249 priv->cell_view_frame = NULL;
1256 gtk_combo_box_remove (GtkContainer *container,
1259 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1260 GtkComboBoxPrivate *priv = combo_box->priv;
1262 gboolean appears_as_list;
1264 if (widget == priv->cell_view)
1265 priv->cell_view = NULL;
1267 gtk_widget_unparent (widget);
1268 _gtk_bin_set_child (GTK_BIN (container), NULL);
1270 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1273 gtk_widget_queue_resize (GTK_WIDGET (container));
1275 if (!priv->tree_view)
1276 appears_as_list = FALSE;
1278 appears_as_list = TRUE;
1280 if (appears_as_list)
1281 gtk_combo_box_list_destroy (combo_box);
1282 else if (GTK_IS_MENU (priv->popup_widget))
1284 gtk_combo_box_menu_destroy (combo_box);
1285 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1286 priv->popup_widget = NULL;
1289 if (!priv->cell_view)
1291 priv->cell_view = gtk_cell_view_new ();
1292 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1293 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1295 gtk_widget_show (priv->cell_view);
1296 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1298 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1302 if (appears_as_list)
1303 gtk_combo_box_list_setup (combo_box);
1305 gtk_combo_box_menu_setup (combo_box, TRUE);
1307 if (gtk_tree_row_reference_valid (priv->active_row))
1309 path = gtk_tree_row_reference_get_path (priv->active_row);
1310 gtk_combo_box_set_active_internal (combo_box, path);
1311 gtk_tree_path_free (path);
1314 gtk_combo_box_set_active_internal (combo_box, NULL);
1317 static ComboCellInfo *
1318 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1319 GtkCellRenderer *cell)
1323 for (i = combo_box->priv->cells; i; i = i->next)
1325 ComboCellInfo *info = (ComboCellInfo *)i->data;
1327 if (info && info->cell == cell)
1335 gtk_combo_box_menu_show (GtkWidget *menu,
1338 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1339 GtkComboBoxPrivate *priv = combo_box->priv;
1341 gtk_combo_box_child_show (menu, user_data);
1343 priv->popup_in_progress = TRUE;
1344 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1346 priv->popup_in_progress = FALSE;
1350 gtk_combo_box_menu_hide (GtkWidget *menu,
1353 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1355 gtk_combo_box_child_hide (menu,user_data);
1357 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1362 gtk_combo_box_detacher (GtkWidget *widget,
1365 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1366 GtkComboBoxPrivate *priv = combo_box->priv;
1368 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1370 g_signal_handlers_disconnect_by_func (menu->toplevel,
1371 gtk_combo_box_menu_show,
1373 g_signal_handlers_disconnect_by_func (menu->toplevel,
1374 gtk_combo_box_menu_hide,
1377 priv->popup_widget = NULL;
1381 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1384 GtkComboBoxPrivate *priv = combo_box->priv;
1386 if (GTK_IS_MENU (priv->popup_widget))
1388 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1389 priv->popup_widget = NULL;
1391 else if (priv->popup_widget)
1393 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1394 priv->popup_widget);
1395 g_object_unref (priv->popup_widget);
1396 priv->popup_widget = NULL;
1399 if (GTK_IS_MENU (popup))
1401 if (priv->popup_window)
1403 gtk_widget_destroy (priv->popup_window);
1404 priv->popup_window = NULL;
1407 priv->popup_widget = popup;
1410 * Note that we connect to show/hide on the toplevel, not the
1411 * menu itself, since the menu is not shown/hidden when it is
1412 * popped up while torn-off.
1414 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1415 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1416 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1417 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1419 gtk_menu_attach_to_widget (GTK_MENU (popup),
1420 GTK_WIDGET (combo_box),
1421 gtk_combo_box_detacher);
1425 if (!priv->popup_window)
1427 GtkWidget *toplevel;
1429 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1430 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1432 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1433 GDK_WINDOW_TYPE_HINT_COMBO);
1435 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1436 G_CALLBACK (gtk_combo_box_child_show),
1438 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1439 G_CALLBACK (gtk_combo_box_child_hide),
1442 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1443 if (GTK_IS_WINDOW (toplevel))
1445 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1446 GTK_WINDOW (priv->popup_window));
1447 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1448 GTK_WINDOW (toplevel));
1451 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1452 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1453 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1455 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1457 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1460 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1463 gtk_widget_show (priv->scrolled_window);
1465 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1466 priv->scrolled_window);
1469 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1472 gtk_widget_show (popup);
1473 g_object_ref (popup);
1474 priv->popup_widget = popup;
1479 gtk_combo_box_menu_position_below (GtkMenu *menu,
1485 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1491 GdkRectangle monitor;
1493 /* FIXME: is using the size request here broken? */
1494 child = gtk_bin_get_child (GTK_BIN (combo_box));
1498 if (!gtk_widget_get_has_window (child))
1500 sx += child->allocation.x;
1501 sy += child->allocation.y;
1504 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1506 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1507 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1509 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1511 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1514 *x = sx + child->allocation.width - req.width;
1517 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1518 monitor_num = gdk_screen_get_monitor_at_window (screen,
1519 GTK_WIDGET (combo_box)->window);
1520 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1524 else if (*x + req.width > monitor.x + monitor.width)
1525 *x = monitor.x + monitor.width - req.width;
1527 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1528 *y += child->allocation.height;
1529 else if (*y - monitor.y >= req.height)
1531 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1532 *y += child->allocation.height;
1540 gtk_combo_box_menu_position_over (GtkMenu *menu,
1546 GtkComboBox *combo_box;
1550 GtkRequisition requisition;
1557 combo_box = GTK_COMBO_BOX (user_data);
1558 widget = GTK_WIDGET (combo_box);
1560 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1561 menu_width = requisition.width;
1563 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1565 menu_xpos = widget->allocation.x;
1566 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1570 gtk_widget_get_child_requisition (active, &requisition);
1571 menu_ypos -= requisition.height / 2;
1574 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1577 child = children->data;
1579 if (active == child)
1582 if (gtk_widget_get_visible (child))
1584 gtk_widget_get_child_requisition (child, &requisition);
1585 menu_ypos -= requisition.height;
1588 children = children->next;
1591 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1592 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1594 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1595 &menu_xpos, &menu_ypos);
1597 /* Clamp the position on screen */
1598 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1602 else if ((menu_xpos + menu_width) > screen_width)
1603 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1612 gtk_combo_box_menu_position (GtkMenu *menu,
1618 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1619 GtkComboBoxPrivate *priv = combo_box->priv;
1620 GtkWidget *menu_item;
1623 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1624 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1627 /* FIXME handle nested menus better */
1628 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1630 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1633 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1636 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1637 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1638 GDK_WINDOW_TYPE_HINT_COMBO);
1642 gtk_combo_box_list_position (GtkComboBox *combo_box,
1648 GtkComboBoxPrivate *priv = combo_box->priv;
1651 GdkRectangle monitor;
1652 GtkRequisition popup_req;
1653 GtkPolicyType hpolicy, vpolicy;
1655 /* under windows, the drop down list is as wide as the combo box itself.
1657 GtkWidget *sample = GTK_WIDGET (combo_box);
1661 if (!gtk_widget_get_has_window (sample))
1663 *x += sample->allocation.x;
1664 *y += sample->allocation.y;
1667 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1669 *width = sample->allocation.width;
1671 hpolicy = vpolicy = GTK_POLICY_NEVER;
1672 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1674 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1676 if (popup_req.width > *width)
1678 hpolicy = GTK_POLICY_ALWAYS;
1679 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1681 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1684 *height = popup_req.height;
1686 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1687 monitor_num = gdk_screen_get_monitor_at_window (screen,
1688 GTK_WIDGET (combo_box)->window);
1689 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1693 else if (*x + *width > monitor.x + monitor.width)
1694 *x = monitor.x + monitor.width - *width;
1696 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1697 *y += sample->allocation.height;
1698 else if (*y - *height >= monitor.y)
1700 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1702 *y += sample->allocation.height;
1703 *height = monitor.y + monitor.height - *y;
1707 *height = *y - monitor.y;
1711 if (popup_req.height > *height)
1713 vpolicy = GTK_POLICY_ALWAYS;
1715 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1721 cell_view_is_sensitive (GtkCellView *cell_view)
1723 GList *cells, *list;
1726 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1729 for (list = cells; list; list = list->next)
1731 g_object_get (list->data, "sensitive", &sensitive, NULL);
1736 g_list_free (cells);
1742 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1745 GtkComboBoxPrivate *priv = combo_box->priv;
1746 GList *cells, *list;
1752 if (priv->row_separator_func)
1754 if (priv->row_separator_func (priv->model, iter,
1755 priv->row_separator_data))
1759 gtk_tree_view_column_cell_set_cell_data (priv->column,
1761 iter, FALSE, FALSE);
1763 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1766 for (list = cells; list; list = list->next)
1768 g_object_get (list->data, "sensitive", &sensitive, NULL);
1773 g_list_free (cells);
1779 update_menu_sensitivity (GtkComboBox *combo_box,
1782 GtkComboBoxPrivate *priv = combo_box->priv;
1783 GList *children, *child;
1784 GtkWidget *item, *submenu, *separator;
1785 GtkWidget *cell_view;
1791 children = gtk_container_get_children (GTK_CONTAINER (menu));
1793 for (child = children; child; child = child->next)
1795 item = GTK_WIDGET (child->data);
1796 cell_view = gtk_bin_get_child (GTK_BIN (item));
1798 if (!GTK_IS_CELL_VIEW (cell_view))
1801 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1802 if (submenu != NULL)
1804 gtk_widget_set_sensitive (item, TRUE);
1805 update_menu_sensitivity (combo_box, submenu);
1809 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1811 if (menu != priv->popup_widget && child == children)
1813 separator = GTK_WIDGET (child->next->data);
1814 g_object_set (item, "visible", sensitive, NULL);
1815 g_object_set (separator, "visible", sensitive, NULL);
1818 gtk_widget_set_sensitive (item, sensitive);
1822 g_list_free (children);
1826 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1828 guint32 activate_time)
1830 GtkComboBoxPrivate *priv = combo_box->priv;
1833 GtkRequisition requisition;
1836 update_menu_sensitivity (combo_box, priv->popup_widget);
1839 if (gtk_tree_row_reference_valid (priv->active_row))
1841 path = gtk_tree_row_reference_get_path (priv->active_row);
1842 active_item = gtk_tree_path_get_indices (path)[0];
1843 gtk_tree_path_free (path);
1845 if (priv->add_tearoffs)
1849 /* FIXME handle nested menus better */
1850 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1852 if (priv->wrap_width == 0)
1854 width = GTK_WIDGET (combo_box)->allocation.width;
1855 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1856 gtk_widget_size_request (priv->popup_widget, &requisition);
1858 gtk_widget_set_size_request (priv->popup_widget,
1859 MAX (width, requisition.width), -1);
1862 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1864 gtk_combo_box_menu_position, combo_box,
1865 button, activate_time);
1869 popup_grab_on_window (GdkWindow *window,
1870 GdkDevice *keyboard,
1872 guint32 activate_time)
1875 gdk_device_grab (keyboard, window,
1876 GDK_OWNERSHIP_WINDOW, TRUE,
1877 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1878 NULL, activate_time) != GDK_GRAB_SUCCESS)
1882 gdk_device_grab (pointer, window,
1883 GDK_OWNERSHIP_WINDOW, TRUE,
1884 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1885 GDK_POINTER_MOTION_MASK,
1886 NULL, activate_time) != GDK_GRAB_SUCCESS)
1889 gdk_device_ungrab (keyboard, activate_time);
1898 * gtk_combo_box_popup:
1899 * @combo_box: a #GtkComboBox
1901 * Pops up the menu or dropdown list of @combo_box.
1903 * This function is mostly intended for use by accessibility technologies;
1904 * applications should have little use for it.
1909 gtk_combo_box_popup (GtkComboBox *combo_box)
1911 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1913 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1917 * gtk_combo_box_popup_for_device:
1918 * @combo_box: a #GtkComboBox
1919 * @device: a #GdkDevice
1921 * Pops up the menu or dropdown list of @combo_box, the popup window
1922 * will be grabbed so only @device and its associated pointer/keyboard
1923 * are the only #GdkDevice<!-- -->s able to send events to it.
1928 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
1931 GtkComboBoxPrivate *priv = combo_box->priv;
1932 gint x, y, width, height;
1933 GtkTreePath *path = NULL, *ppath;
1934 GtkWidget *toplevel;
1935 GdkDevice *keyboard, *pointer;
1938 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1939 g_return_if_fail (GDK_IS_DEVICE (device));
1941 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
1944 if (gtk_widget_get_mapped (priv->popup_widget))
1947 if (priv->grab_pointer && priv->grab_keyboard)
1950 time = gtk_get_current_event_time ();
1952 if (device->source == GDK_SOURCE_KEYBOARD)
1955 pointer = gdk_device_get_associated_device (device);
1960 keyboard = gdk_device_get_associated_device (device);
1963 if (GTK_IS_MENU (priv->popup_widget))
1965 gtk_combo_box_menu_popup (combo_box,
1966 priv->activate_button,
1967 priv->activate_time);
1971 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1972 if (GTK_IS_WINDOW (toplevel))
1973 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1974 GTK_WINDOW (priv->popup_window));
1976 gtk_widget_show_all (priv->scrolled_window);
1977 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1979 gtk_widget_set_size_request (priv->popup_window, width, height);
1980 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1982 if (gtk_tree_row_reference_valid (priv->active_row))
1984 path = gtk_tree_row_reference_get_path (priv->active_row);
1985 ppath = gtk_tree_path_copy (path);
1986 if (gtk_tree_path_up (ppath))
1987 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1989 gtk_tree_path_free (ppath);
1991 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1995 gtk_widget_show (priv->popup_window);
1999 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2001 gtk_tree_path_free (path);
2004 gtk_widget_grab_focus (priv->popup_window);
2005 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2008 if (!gtk_widget_has_focus (priv->tree_view))
2009 gtk_widget_grab_focus (priv->tree_view);
2011 if (!popup_grab_on_window (priv->popup_window->window,
2012 keyboard, pointer, time))
2014 gtk_widget_hide (priv->popup_window);
2018 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2019 priv->grab_pointer = pointer;
2020 priv->grab_keyboard = keyboard;
2024 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2028 device = gtk_get_current_event_device ();
2032 GdkDeviceManager *device_manager;
2033 GdkDisplay *display;
2036 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2037 device_manager = gdk_display_get_device_manager (display);
2039 /* No device was set, pick the first master device */
2040 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2041 device = devices->data;
2042 g_list_free (devices);
2045 gtk_combo_box_popup_for_device (combo_box, device);
2049 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2051 if (combo_box->priv->popup_shown)
2053 gtk_combo_box_popdown (combo_box);
2061 * gtk_combo_box_popdown:
2062 * @combo_box: a #GtkComboBox
2064 * Hides the menu or dropdown list of @combo_box.
2066 * This function is mostly intended for use by accessibility technologies;
2067 * applications should have little use for it.
2072 gtk_combo_box_popdown (GtkComboBox *combo_box)
2074 GtkComboBoxPrivate *priv = combo_box->priv;
2076 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2078 if (GTK_IS_MENU (priv->popup_widget))
2080 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2084 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2087 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2088 gtk_widget_hide_all (priv->popup_window);
2089 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2092 priv->grab_pointer = NULL;
2093 priv->grab_keyboard = NULL;
2097 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2100 GtkComboBoxPrivate *priv = combo_box->priv;
2104 if (priv->cell_view)
2105 gtk_widget_style_get (priv->cell_view,
2106 "focus-line-width", &padding,
2111 /* add some pixels for good measure */
2112 padding += BONUS_PADDING;
2114 if (priv->cell_view)
2115 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2120 return req.width + padding;
2124 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2126 GtkComboBoxPrivate *priv = combo_box->priv;
2131 !gtk_tree_model_get_iter_first (priv->model, &iter))
2137 path = gtk_tree_path_new_from_indices (0, -1);
2143 if (priv->cell_view)
2144 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2152 priv->width = MAX (priv->width, req.width);
2153 priv->height = MAX (priv->height, req.height);
2155 gtk_tree_path_next (path);
2157 while (gtk_tree_model_iter_next (priv->model, &iter));
2159 gtk_tree_path_free (path);
2163 gtk_combo_box_size_request (GtkWidget *widget,
2164 GtkRequisition *requisition)
2167 gint focus_width, focus_pad;
2171 GtkRequisition bin_req;
2172 PangoContext *context;
2173 PangoFontMetrics *metrics;
2174 PangoFontDescription *font_desc;
2176 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2177 GtkComboBoxPrivate *priv = combo_box->priv;
2180 gtk_widget_size_request (gtk_bin_get_child (GTK_BIN (widget)), &bin_req);
2181 gtk_combo_box_remeasure (combo_box);
2182 bin_req.width = MAX (bin_req.width, priv->width);
2183 bin_req.height = MAX (bin_req.height, priv->height);
2185 gtk_widget_style_get (GTK_WIDGET (widget),
2186 "focus-line-width", &focus_width,
2187 "focus-padding", &focus_pad,
2188 "arrow-size", &arrow_size,
2191 font_desc = gtk_bin_get_child (GTK_BIN (widget))->style->font_desc;
2192 context = gtk_widget_get_pango_context (widget);
2193 metrics = pango_context_get_metrics (context, font_desc,
2194 pango_context_get_language (context));
2195 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2196 pango_font_metrics_get_descent (metrics));
2197 pango_font_metrics_unref (metrics);
2199 arrow_size = MAX (arrow_size, font_size);
2201 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2203 if (!priv->tree_view)
2207 if (priv->cell_view)
2209 GtkRequisition button_req, sep_req, arrow_req;
2210 gint xthickness, ythickness;
2212 gtk_widget_size_request (priv->button, &button_req);
2213 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
2214 xthickness = priv->button->style->xthickness;
2215 ythickness = priv->button->style->ythickness;
2217 bin_req.width = MAX (bin_req.width, priv->width);
2218 bin_req.height = MAX (bin_req.height, priv->height);
2220 gtk_widget_size_request (priv->separator, &sep_req);
2221 gtk_widget_size_request (priv->arrow, &arrow_req);
2223 height = MAX (sep_req.height, arrow_req.height);
2224 height = MAX (height, bin_req.height);
2226 width = bin_req.width + sep_req.width + arrow_req.width;
2228 height += 2*(border_width + ythickness + focus_width + focus_pad);
2229 width += 2*(border_width + xthickness + focus_width + focus_pad);
2231 requisition->width = width;
2232 requisition->height = height;
2236 GtkRequisition but_req;
2238 gtk_widget_size_request (priv->button, &but_req);
2240 requisition->width = bin_req.width + but_req.width;
2241 requisition->height = MAX (bin_req.height, but_req.height);
2247 GtkRequisition button_req, frame_req;
2249 /* sample + frame */
2250 *requisition = bin_req;
2252 requisition->width += 2 * focus_width;
2254 if (priv->cell_view_frame)
2256 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2257 if (priv->has_frame)
2259 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2261 requisition->width += 2 * (border_width + GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2262 requisition->height += 2 * (border_width + GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2267 gtk_widget_size_request (priv->button, &button_req);
2269 requisition->height = MAX (requisition->height, button_req.height);
2270 requisition->width += button_req.width;
2273 if (GTK_SHADOW_NONE != priv->shadow_type)
2275 requisition->height += 2 * widget->style->ythickness;
2276 requisition->width += 2 * widget->style->xthickness;
2280 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2281 gtk_widget_size_request (combo_box->priv->button, &req); \
2284 child.x = allocation->x + shadow_width; \
2286 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2288 child.y = allocation->y + shadow_height; \
2289 child.width = req.width; \
2290 child.height = allocation->height - 2 * shadow_height; \
2291 child.width = MAX (1, child.width); \
2292 child.height = MAX (1, child.height); \
2294 gtk_widget_size_allocate (combo_box->priv->button, &child);
2297 gtk_combo_box_size_allocate (GtkWidget *widget,
2298 GtkAllocation *allocation)
2300 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2301 GtkComboBoxPrivate *priv = combo_box->priv;
2302 GtkWidget *child_widget;
2303 gint shadow_width, shadow_height;
2304 gint focus_width, focus_pad;
2305 GtkAllocation child;
2307 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2309 widget->allocation = *allocation;
2310 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2312 gtk_widget_style_get (GTK_WIDGET (widget),
2313 "focus-line-width", &focus_width,
2314 "focus-padding", &focus_pad,
2317 if (GTK_SHADOW_NONE != priv->shadow_type)
2319 shadow_width = widget->style->xthickness;
2320 shadow_height = widget->style->ythickness;
2328 if (!priv->tree_view)
2330 if (priv->cell_view)
2332 gint xthickness, ythickness;
2337 allocation->x += shadow_width;
2338 allocation->y += shadow_height;
2339 allocation->width -= 2 * shadow_width;
2340 allocation->height -= 2 * shadow_height;
2342 gtk_widget_size_allocate (priv->button, allocation);
2344 /* set some things ready */
2345 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2346 xthickness = priv->button->style->xthickness;
2347 ythickness = priv->button->style->ythickness;
2349 child.x = allocation->x;
2350 child.y = allocation->y;
2351 width = allocation->width;
2352 child.height = allocation->height;
2354 if (!priv->is_cell_renderer)
2356 child.x += border_width + xthickness + focus_width + focus_pad;
2357 child.y += border_width + ythickness + focus_width + focus_pad;
2358 width -= 2 * (child.x - allocation->x);
2359 child.height -= 2 * (child.y - allocation->y);
2363 /* handle the children */
2364 gtk_widget_size_request (priv->arrow, &req);
2365 child.width = req.width;
2367 child.x += width - req.width;
2368 child.width = MAX (1, child.width);
2369 child.height = MAX (1, child.height);
2370 gtk_widget_size_allocate (priv->arrow, &child);
2372 child.x += req.width;
2373 gtk_widget_size_request (priv->separator, &req);
2374 child.width = req.width;
2376 child.x -= req.width;
2377 child.width = MAX (1, child.width);
2378 child.height = MAX (1, child.height);
2379 gtk_widget_size_allocate (priv->separator, &child);
2383 child.x += req.width;
2384 child.width = allocation->x + allocation->width
2385 - (border_width + xthickness + focus_width + focus_pad)
2390 child.width = child.x;
2391 child.x = allocation->x
2392 + border_width + xthickness + focus_width + focus_pad;
2393 child.width -= child.x;
2396 if (gtk_widget_get_visible (priv->popup_widget))
2399 GtkRequisition requisition;
2401 /* Warning here, without the check in the position func */
2402 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2403 if (priv->wrap_width == 0)
2405 width = GTK_WIDGET (combo_box)->allocation.width;
2406 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2407 gtk_widget_size_request (priv->popup_widget, &requisition);
2408 gtk_widget_set_size_request (priv->popup_widget,
2409 MAX (width, requisition.width), -1);
2413 child.width = MAX (1, child.width);
2414 child.height = MAX (1, child.height);
2415 gtk_widget_size_allocate (child_widget, &child);
2419 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2422 child.x = allocation->x + req.width + shadow_width;
2424 child.x = allocation->x + shadow_width;
2425 child.y = allocation->y + shadow_height;
2426 child.width = allocation->width - req.width - 2 * shadow_width;
2427 child.width = MAX (1, child.width);
2428 child.height = MAX (1, child.height);
2429 gtk_widget_size_allocate (child_widget, &child);
2436 /* Combobox thickness + border-width */
2437 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2438 int delta_x = shadow_width + border_width;
2439 int delta_y = shadow_height + border_width;
2442 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2446 child.x = allocation->x + req.width;
2448 child.x = allocation->x;
2450 child.y = allocation->y;
2451 child.width = allocation->width - req.width;
2452 child.height = allocation->height;
2454 if (priv->cell_view_frame)
2458 child.width = MAX (1, child.width - delta_x * 2);
2459 child.height = MAX (1, child.height - delta_y * 2);
2460 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2463 if (priv->has_frame)
2465 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2466 delta_x = border_width + GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2467 delta_y = border_width + GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2471 child.width -= delta_x * 2;
2472 child.height -= delta_y * 2;
2479 child.width -= delta_x * 2;
2480 child.height -= delta_y * 2;
2483 if (gtk_widget_get_visible (priv->popup_window))
2485 gint x, y, width, height;
2486 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2487 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2488 gtk_widget_set_size_request (priv->popup_window, width, height);
2492 child.width = MAX (1, child.width);
2493 child.height = MAX (1, child.height);
2495 gtk_widget_size_allocate (child_widget, &child);
2499 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2502 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2504 GtkComboBoxPrivate *priv = combo_box->priv;
2508 g_signal_handler_disconnect (priv->model,
2510 g_signal_handler_disconnect (priv->model,
2512 g_signal_handler_disconnect (priv->model,
2513 priv->reordered_id);
2514 g_signal_handler_disconnect (priv->model,
2519 if (!priv->tree_view)
2521 if (priv->popup_widget)
2522 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2523 (GtkCallback)gtk_widget_destroy, NULL);
2528 g_object_unref (priv->model);
2532 if (priv->active_row)
2534 gtk_tree_row_reference_free (priv->active_row);
2535 priv->active_row = NULL;
2538 if (priv->cell_view)
2539 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2543 gtk_combo_box_forall (GtkContainer *container,
2544 gboolean include_internals,
2545 GtkCallback callback,
2546 gpointer callback_data)
2548 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2549 GtkComboBoxPrivate *priv = combo_box->priv;
2552 if (include_internals)
2555 (* callback) (priv->button, callback_data);
2556 if (priv->cell_view_frame)
2557 (* callback) (priv->cell_view_frame, callback_data);
2560 child = gtk_bin_get_child (GTK_BIN (container));
2562 (* callback) (child, callback_data);
2566 gtk_combo_box_child_show (GtkWidget *widget,
2567 GtkComboBox *combo_box)
2569 GtkComboBoxPrivate *priv = combo_box->priv;
2571 priv->popup_shown = TRUE;
2572 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2576 gtk_combo_box_child_hide (GtkWidget *widget,
2577 GtkComboBox *combo_box)
2579 GtkComboBoxPrivate *priv = combo_box->priv;
2581 priv->popup_shown = FALSE;
2582 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2586 gtk_combo_box_expose_event (GtkWidget *widget,
2587 GdkEventExpose *event)
2589 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2590 GtkComboBoxPrivate *priv = combo_box->priv;
2592 if (gtk_widget_is_drawable (widget) &&
2593 GTK_SHADOW_NONE != priv->shadow_type)
2595 gtk_paint_shadow (widget->style, widget->window,
2596 GTK_STATE_NORMAL, priv->shadow_type,
2597 NULL, widget, "combobox",
2598 widget->allocation.x, widget->allocation.y,
2599 widget->allocation.width, widget->allocation.height);
2602 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2603 priv->button, event);
2605 if (priv->tree_view && priv->cell_view_frame)
2607 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2608 priv->cell_view_frame, event);
2611 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2612 gtk_bin_get_child (GTK_BIN (widget)),
2628 path_visible (GtkTreeView *view,
2634 /* Note that we rely on the fact that collapsed rows don't have nodes
2636 return _gtk_tree_view_find_node (view, path, &tree, &node);
2640 tree_next_func (GtkTreeModel *model,
2645 SearchData *search_data = (SearchData *)data;
2647 if (search_data->found)
2649 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2652 if (search_data->visible &&
2653 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2656 search_data->set = TRUE;
2657 search_data->iter = *iter;
2662 if (gtk_tree_path_compare (path, search_data->path) == 0)
2663 search_data->found = TRUE;
2669 tree_next (GtkComboBox *combo,
2670 GtkTreeModel *model,
2675 SearchData search_data;
2677 search_data.combo = combo;
2678 search_data.path = gtk_tree_model_get_path (model, iter);
2679 search_data.visible = visible;
2680 search_data.found = FALSE;
2681 search_data.set = FALSE;
2683 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2685 *next = search_data.iter;
2687 gtk_tree_path_free (search_data.path);
2689 return search_data.set;
2693 tree_prev_func (GtkTreeModel *model,
2698 SearchData *search_data = (SearchData *)data;
2700 if (gtk_tree_path_compare (path, search_data->path) == 0)
2702 search_data->found = TRUE;
2706 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2709 if (search_data->visible &&
2710 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2713 search_data->set = TRUE;
2714 search_data->iter = *iter;
2720 tree_prev (GtkComboBox *combo,
2721 GtkTreeModel *model,
2726 SearchData search_data;
2728 search_data.combo = combo;
2729 search_data.path = gtk_tree_model_get_path (model, iter);
2730 search_data.visible = visible;
2731 search_data.found = FALSE;
2732 search_data.set = FALSE;
2734 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2736 *prev = search_data.iter;
2738 gtk_tree_path_free (search_data.path);
2740 return search_data.set;
2744 tree_last_func (GtkTreeModel *model,
2749 SearchData *search_data = (SearchData *)data;
2751 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2754 /* Note that we rely on the fact that collapsed rows don't have nodes
2756 if (search_data->visible &&
2757 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2760 search_data->set = TRUE;
2761 search_data->iter = *iter;
2767 tree_last (GtkComboBox *combo,
2768 GtkTreeModel *model,
2772 SearchData search_data;
2774 search_data.combo = combo;
2775 search_data.visible = visible;
2776 search_data.set = FALSE;
2778 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2780 *last = search_data.iter;
2782 return search_data.set;
2787 tree_first_func (GtkTreeModel *model,
2792 SearchData *search_data = (SearchData *)data;
2794 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2797 if (search_data->visible &&
2798 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2801 search_data->set = TRUE;
2802 search_data->iter = *iter;
2808 tree_first (GtkComboBox *combo,
2809 GtkTreeModel *model,
2813 SearchData search_data;
2815 search_data.combo = combo;
2816 search_data.visible = visible;
2817 search_data.set = FALSE;
2819 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2821 *first = search_data.iter;
2823 return search_data.set;
2827 gtk_combo_box_scroll_event (GtkWidget *widget,
2828 GdkEventScroll *event)
2830 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2833 GtkTreeIter new_iter;
2835 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2838 if (event->direction == GDK_SCROLL_UP)
2839 found = tree_prev (combo_box, combo_box->priv->model,
2840 &iter, &new_iter, FALSE);
2842 found = tree_next (combo_box, combo_box->priv->model,
2843 &iter, &new_iter, FALSE);
2846 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2856 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2857 GtkCellLayout *cell_layout)
2859 GtkComboBoxPrivate *priv = combo_box->priv;
2862 for (k = priv->cells; k; k = k->next)
2865 ComboCellInfo *info = (ComboCellInfo *)k->data;
2867 if (info->pack == GTK_PACK_START)
2868 gtk_cell_layout_pack_start (cell_layout,
2869 info->cell, info->expand);
2870 else if (info->pack == GTK_PACK_END)
2871 gtk_cell_layout_pack_end (cell_layout,
2872 info->cell, info->expand);
2874 gtk_cell_layout_set_cell_data_func (cell_layout,
2876 combo_cell_data_func, info, NULL);
2878 for (j = info->attributes; j; j = j->next->next)
2880 gtk_cell_layout_add_attribute (cell_layout,
2883 GPOINTER_TO_INT (j->next->data));
2889 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2890 gboolean add_children)
2892 GtkComboBoxPrivate *priv = combo_box->priv;
2896 child = gtk_bin_get_child (GTK_BIN (combo_box));
2898 if (priv->cell_view)
2900 priv->button = gtk_toggle_button_new ();
2901 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2902 priv->focus_on_click);
2904 g_signal_connect (priv->button, "toggled",
2905 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2906 gtk_widget_set_parent (priv->button,
2907 gtk_widget_get_parent (child));
2909 priv->box = gtk_hbox_new (FALSE, 0);
2910 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2912 priv->separator = gtk_vseparator_new ();
2913 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2915 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2916 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2918 gtk_widget_show_all (priv->button);
2922 priv->button = gtk_toggle_button_new ();
2923 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2924 priv->focus_on_click);
2926 g_signal_connect (priv->button, "toggled",
2927 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2928 gtk_widget_set_parent (priv->button,
2929 gtk_widget_get_parent (child));
2931 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2932 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2933 gtk_widget_show_all (priv->button);
2936 g_signal_connect (priv->button, "button-press-event",
2937 G_CALLBACK (gtk_combo_box_menu_button_press),
2939 g_signal_connect (priv->button, "state-changed",
2940 G_CALLBACK (gtk_combo_box_button_state_changed),
2943 /* create our funky menu */
2944 menu = gtk_menu_new ();
2945 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2946 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2948 g_signal_connect (menu, "key-press-event",
2949 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2950 gtk_combo_box_set_popup_widget (combo_box, menu);
2954 gtk_combo_box_menu_fill (combo_box);
2956 /* the column is needed in tree_column_row_is_sensitive() */
2957 priv->column = gtk_tree_view_column_new ();
2958 g_object_ref_sink (priv->column);
2959 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2961 gtk_combo_box_update_title (combo_box);
2962 gtk_combo_box_update_sensitivity (combo_box);
2966 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2968 GtkComboBoxPrivate *priv = combo_box->priv;
2974 menu = priv->popup_widget;
2976 if (priv->add_tearoffs)
2978 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2980 gtk_widget_show (tearoff);
2982 if (priv->wrap_width)
2983 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2985 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2988 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2992 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2993 GtkTreeModel *model,
2996 GtkWidget *cell_view;
3001 cell_view = gtk_cell_view_new ();
3002 item = gtk_menu_item_new ();
3003 gtk_container_add (GTK_CONTAINER (item), cell_view);
3005 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
3006 path = gtk_tree_model_get_path (model, iter);
3007 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
3008 gtk_tree_path_free (path);
3010 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
3011 gtk_widget_size_request (cell_view, &req);
3012 gtk_widget_show (cell_view);
3018 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
3020 GtkTreeIter *parent)
3022 GtkComboBoxPrivate *priv = combo_box->priv;
3023 GtkTreeModel *model = priv->model;
3024 GtkWidget *item, *submenu, *subitem, *separator;
3026 gboolean is_separator;
3031 n_children = gtk_tree_model_iter_n_children (model, parent);
3034 for (i = 0; i < n_children; i++)
3036 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
3038 if (priv->row_separator_func)
3039 is_separator = priv->row_separator_func (priv->model, &iter,
3040 priv->row_separator_data);
3042 is_separator = FALSE;
3046 item = gtk_separator_menu_item_new ();
3047 path = gtk_tree_model_get_path (model, &iter);
3048 g_object_set_data_full (G_OBJECT (item),
3049 I_("gtk-combo-box-item-path"),
3050 gtk_tree_row_reference_new (model, path),
3051 (GDestroyNotify)gtk_tree_row_reference_free);
3052 gtk_tree_path_free (path);
3056 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3057 if (gtk_tree_model_iter_has_child (model, &iter))
3059 submenu = gtk_menu_new ();
3060 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
3061 gtk_widget_show (submenu);
3062 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
3064 /* Ugly - since menus can only activate leafs, we have to
3065 * duplicate the item inside the submenu.
3067 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
3068 separator = gtk_separator_menu_item_new ();
3069 gtk_widget_show (subitem);
3070 gtk_widget_show (separator);
3071 g_signal_connect (subitem, "activate",
3072 G_CALLBACK (gtk_combo_box_menu_item_activate),
3074 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
3075 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
3077 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
3079 g_signal_connect (item, "activate",
3080 G_CALLBACK (gtk_combo_box_menu_item_activate),
3084 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3085 if (priv->wrap_width && menu == priv->popup_widget)
3086 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
3087 gtk_widget_show (item);
3094 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3096 GtkComboBoxPrivate *priv = combo_box->priv;
3098 g_signal_handlers_disconnect_matched (priv->button,
3099 G_SIGNAL_MATCH_DATA,
3101 gtk_combo_box_menu_button_press, NULL);
3102 g_signal_handlers_disconnect_matched (priv->button,
3103 G_SIGNAL_MATCH_DATA,
3105 gtk_combo_box_button_state_changed, combo_box);
3107 /* unparent will remove our latest ref */
3108 gtk_widget_unparent (priv->button);
3111 priv->button = NULL;
3113 priv->separator = NULL;
3115 g_object_unref (priv->column);
3116 priv->column = NULL;
3118 /* changing the popup window will unref the menu and the children */
3126 menu_occupied (GtkMenu *menu,
3130 guint bottom_attach)
3134 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3138 gtk_container_child_get (GTK_CONTAINER (menu),
3142 "bottom-attach", &b,
3146 /* look if this item intersects with the given coordinates */
3147 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3155 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3160 GtkComboBoxPrivate *priv = combo_box->priv;
3161 gint current_col = 0, current_row = 0;
3162 gint rows = 1, cols = 1;
3163 GtkWidget *menu = priv->popup_widget;
3165 if (!GTK_IS_MENU_SHELL (menu))
3168 if (priv->col_column == -1 &&
3169 priv->row_column == -1 &&
3172 gtk_container_child_get (GTK_CONTAINER (menu),
3174 "right-attach", ¤t_col,
3175 "top-attach", ¤t_row,
3177 if (current_col + cols > priv->wrap_width)
3185 if (priv->col_column != -1)
3186 gtk_tree_model_get (priv->model, iter,
3187 priv->col_column, &cols,
3189 if (priv->row_column != -1)
3190 gtk_tree_model_get (priv->model, iter,
3191 priv->row_column, &rows,
3196 if (current_col + cols > priv->wrap_width)
3202 if (!menu_occupied (GTK_MENU (menu),
3203 current_col, current_col + cols,
3204 current_row, current_row + rows))
3211 /* set attach props */
3212 gtk_menu_attach (GTK_MENU (menu), item,
3213 current_col, current_col + cols,
3214 current_row, current_row + rows);
3218 gtk_combo_box_relayout (GtkComboBox *combo_box)
3223 menu = combo_box->priv->popup_widget;
3225 /* do nothing unless we are in menu style and realized */
3226 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3229 list = gtk_container_get_children (GTK_CONTAINER (menu));
3231 for (j = g_list_last (list); j; j = j->prev)
3232 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3234 gtk_combo_box_menu_fill (combo_box);
3241 gtk_combo_box_menu_button_press (GtkWidget *widget,
3242 GdkEventButton *event,
3245 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3246 GtkComboBoxPrivate *priv = combo_box->priv;
3248 if (GTK_IS_MENU (priv->popup_widget) &&
3249 event->type == GDK_BUTTON_PRESS && event->button == 1)
3251 if (priv->focus_on_click &&
3252 !gtk_widget_has_focus (priv->button))
3253 gtk_widget_grab_focus (priv->button);
3255 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3264 gtk_combo_box_menu_item_activate (GtkWidget *item,
3267 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3268 GtkWidget *cell_view;
3272 cell_view = gtk_bin_get_child (GTK_BIN (item));
3274 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3276 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3278 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3280 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3281 gtk_combo_box_set_active_iter (combo_box, &iter);
3284 gtk_tree_path_free (path);
3286 g_object_set (combo_box,
3287 "editing-canceled", FALSE,
3292 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3295 gboolean sensitive = TRUE; /* fool code checkers */
3297 if (!combo_box->priv->button)
3300 switch (combo_box->priv->button_sensitivity)
3302 case GTK_SENSITIVITY_ON:
3305 case GTK_SENSITIVITY_OFF:
3308 case GTK_SENSITIVITY_AUTO:
3309 sensitive = combo_box->priv->model &&
3310 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3313 g_assert_not_reached ();
3317 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3319 /* In list-mode, we also need to update sensitivity of the event box */
3320 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3321 && combo_box->priv->cell_view)
3322 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3326 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3331 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3333 if (combo_box->priv->tree_view)
3334 gtk_combo_box_list_popup_resize (combo_box);
3336 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3338 gtk_combo_box_update_sensitivity (combo_box);
3342 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3346 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3347 GtkComboBoxPrivate *priv = combo_box->priv;
3349 if (!gtk_tree_row_reference_valid (priv->active_row))
3351 if (priv->cell_view)
3352 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3353 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3356 if (priv->tree_view)
3357 gtk_combo_box_list_popup_resize (combo_box);
3359 gtk_combo_box_menu_row_deleted (model, path, user_data);
3361 gtk_combo_box_update_sensitivity (combo_box);
3365 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3371 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3373 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3375 if (!combo_box->priv->tree_view)
3376 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3380 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3385 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3386 GtkComboBoxPrivate *priv = combo_box->priv;
3387 GtkTreePath *active_path;
3389 /* FIXME this belongs to GtkCellView */
3390 if (gtk_tree_row_reference_valid (priv->active_row))
3392 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3393 if (gtk_tree_path_compare (path, active_path) == 0 &&
3395 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3396 gtk_tree_path_free (active_path);
3399 if (priv->tree_view)
3400 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3402 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3406 list_popup_resize_idle (gpointer user_data)
3408 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3409 GtkComboBoxPrivate *priv = combo_box->priv;
3410 gint x, y, width, height;
3412 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3414 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3416 gtk_widget_set_size_request (priv->popup_window, width, height);
3417 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3420 priv->resize_idle_id = 0;
3426 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3428 GtkComboBoxPrivate *priv = combo_box->priv;
3430 if (!priv->resize_idle_id)
3431 priv->resize_idle_id =
3432 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3436 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3441 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3443 gtk_combo_box_list_popup_resize (combo_box);
3448 find_menu_by_path (GtkWidget *menu,
3450 gboolean skip_first)
3456 GtkTreeRowReference *mref;
3460 list = gtk_container_get_children (GTK_CONTAINER (menu));
3463 for (i = list; i; i = i->next)
3465 child = gtk_bin_get_child (i->data);
3466 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3468 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3471 else if (!gtk_tree_row_reference_valid (mref))
3474 mpath = gtk_tree_row_reference_get_path (mref);
3476 else if (GTK_IS_CELL_VIEW (child))
3484 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (child));
3489 /* this case is necessary, since the row reference of
3490 * the cell view may already be updated after a deletion
3497 if (gtk_tree_path_compare (mpath, path) == 0)
3499 gtk_tree_path_free (mpath);
3503 if (gtk_tree_path_is_ancestor (mpath, path))
3505 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3506 if (submenu != NULL)
3508 gtk_tree_path_free (mpath);
3509 item = find_menu_by_path (submenu, path, TRUE);
3513 gtk_tree_path_free (mpath);
3523 dump_menu_tree (GtkWidget *menu,
3530 list = gtk_container_get_children (GTK_CONTAINER (menu));
3531 for (i = list; i; i = i->next)
3533 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3535 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3536 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3537 gtk_tree_path_free (path);
3539 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3540 if (submenu != NULL)
3541 dump_menu_tree (submenu, level + 1);
3550 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3555 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3556 GtkComboBoxPrivate *priv = combo_box->priv;
3558 GtkWidget *item, *menu, *separator;
3562 gboolean is_separator;
3564 if (!priv->popup_widget)
3567 depth = gtk_tree_path_get_depth (path);
3568 pos = gtk_tree_path_get_indices (path)[depth - 1];
3571 ppath = gtk_tree_path_copy (path);
3572 gtk_tree_path_up (ppath);
3573 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3574 gtk_tree_path_free (ppath);
3576 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3579 menu = gtk_menu_new ();
3580 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3581 gtk_widget_show (menu);
3582 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3584 /* Ugly - since menus can only activate leaves, we have to
3585 * duplicate the item inside the submenu.
3587 gtk_tree_model_iter_parent (model, &piter, iter);
3588 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3589 separator = gtk_separator_menu_item_new ();
3590 g_signal_connect (item, "activate",
3591 G_CALLBACK (gtk_combo_box_menu_item_activate),
3593 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3594 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3595 if (cell_view_is_sensitive (GTK_CELL_VIEW (gtk_bin_get_child (GTK_BIN (item)))))
3597 gtk_widget_show (item);
3598 gtk_widget_show (separator);
3605 menu = priv->popup_widget;
3606 if (priv->add_tearoffs)
3610 if (priv->row_separator_func)
3611 is_separator = priv->row_separator_func (model, iter,
3612 priv->row_separator_data);
3614 is_separator = FALSE;
3618 item = gtk_separator_menu_item_new ();
3619 g_object_set_data_full (G_OBJECT (item),
3620 I_("gtk-combo-box-item-path"),
3621 gtk_tree_row_reference_new (model, path),
3622 (GDestroyNotify)gtk_tree_row_reference_free);
3626 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3628 g_signal_connect (item, "activate",
3629 G_CALLBACK (gtk_combo_box_menu_item_activate),
3633 gtk_widget_show (item);
3634 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3638 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3642 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3643 GtkComboBoxPrivate *priv = combo_box->priv;
3647 if (!priv->popup_widget)
3650 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3651 menu = gtk_widget_get_parent (item);
3652 gtk_container_remove (GTK_CONTAINER (menu), item);
3654 if (gtk_tree_path_get_depth (path) > 1)
3656 GtkTreePath *parent_path;
3660 parent_path = gtk_tree_path_copy (path);
3661 gtk_tree_path_up (parent_path);
3662 gtk_tree_model_get_iter (model, &iter, parent_path);
3664 if (!gtk_tree_model_iter_has_child (model, &iter))
3666 parent = find_menu_by_path (priv->popup_widget,
3667 parent_path, FALSE);
3668 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3674 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3680 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3682 gtk_combo_box_relayout (combo_box);
3686 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3691 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3692 GtkComboBoxPrivate *priv = combo_box->priv;
3695 gboolean is_separator;
3697 if (!priv->popup_widget)
3700 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3702 if (priv->row_separator_func)
3703 is_separator = priv->row_separator_func (model, iter,
3704 priv->row_separator_data);
3706 is_separator = FALSE;
3708 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3710 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3711 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3714 if (priv->wrap_width && item->parent == priv->popup_widget)
3716 GtkWidget *pitem = NULL;
3719 prev = gtk_tree_path_copy (path);
3721 if (gtk_tree_path_prev (prev))
3722 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3724 gtk_tree_path_free (prev);
3726 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3728 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3733 "bottom-attach", -1,
3736 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3739 width = gtk_combo_box_calc_requested_width (combo_box, path);
3741 if (width > priv->width)
3743 if (priv->cell_view)
3745 gtk_widget_set_size_request (priv->cell_view, width, -1);
3746 gtk_widget_queue_resize (priv->cell_view);
3748 priv->width = width;
3757 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3759 GtkComboBoxPrivate *priv = combo_box->priv;
3760 GtkTreeSelection *sel;
3763 GtkWidget *widget = GTK_WIDGET (combo_box);
3765 priv->button = gtk_toggle_button_new ();
3766 child = gtk_bin_get_child (GTK_BIN (combo_box));
3767 gtk_widget_set_parent (priv->button,
3768 gtk_widget_get_parent (child));
3769 g_signal_connect (priv->button, "button-press-event",
3770 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3771 g_signal_connect (priv->button, "toggled",
3772 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3774 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3775 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3776 priv->separator = NULL;
3777 gtk_widget_show_all (priv->button);
3779 if (priv->cell_view)
3781 style = gtk_widget_get_style (widget);
3782 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3783 &style->base[gtk_widget_get_state (widget)]);
3785 priv->box = gtk_event_box_new ();
3786 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3789 if (priv->has_frame)
3791 priv->cell_view_frame = gtk_frame_new (NULL);
3792 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3797 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3798 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3802 gtk_widget_set_parent (priv->cell_view_frame,
3803 gtk_widget_get_parent (child));
3804 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3805 gtk_widget_show_all (priv->cell_view_frame);
3807 g_signal_connect (priv->box, "button-press-event",
3808 G_CALLBACK (gtk_combo_box_list_button_pressed),
3812 priv->tree_view = gtk_tree_view_new ();
3813 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3814 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3815 gtk_tree_selection_set_select_function (sel,
3816 gtk_combo_box_list_select_func,
3818 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3820 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3822 if (priv->row_separator_func)
3823 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3824 priv->row_separator_func,
3825 priv->row_separator_data,
3828 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3830 priv->column = gtk_tree_view_column_new ();
3831 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3834 gtk_combo_box_sync_cells (combo_box,
3835 GTK_CELL_LAYOUT (priv->column));
3837 if (gtk_tree_row_reference_valid (priv->active_row))
3841 path = gtk_tree_row_reference_get_path (priv->active_row);
3842 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3844 gtk_tree_path_free (path);
3847 /* set sample/popup widgets */
3848 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3850 g_signal_connect (priv->tree_view, "key-press-event",
3851 G_CALLBACK (gtk_combo_box_list_key_press),
3853 g_signal_connect (priv->tree_view, "enter-notify-event",
3854 G_CALLBACK (gtk_combo_box_list_enter_notify),
3856 g_signal_connect (priv->tree_view, "row-expanded",
3857 G_CALLBACK (gtk_combo_box_model_row_expanded),
3859 g_signal_connect (priv->tree_view, "row-collapsed",
3860 G_CALLBACK (gtk_combo_box_model_row_expanded),
3862 g_signal_connect (priv->popup_window, "button-press-event",
3863 G_CALLBACK (gtk_combo_box_list_button_pressed),
3865 g_signal_connect (priv->popup_window, "button-release-event",
3866 G_CALLBACK (gtk_combo_box_list_button_released),
3869 gtk_widget_show (priv->tree_view);
3871 gtk_combo_box_update_sensitivity (combo_box);
3875 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3877 GtkComboBoxPrivate *priv = combo_box->priv;
3879 /* disconnect signals */
3880 g_signal_handlers_disconnect_matched (priv->tree_view,
3881 G_SIGNAL_MATCH_DATA,
3882 0, 0, NULL, NULL, combo_box);
3883 g_signal_handlers_disconnect_matched (priv->button,
3884 G_SIGNAL_MATCH_DATA,
3886 gtk_combo_box_list_button_pressed,
3888 g_signal_handlers_disconnect_matched (priv->popup_window,
3889 G_SIGNAL_MATCH_DATA,
3891 gtk_combo_box_list_button_pressed,
3893 g_signal_handlers_disconnect_matched (priv->popup_window,
3894 G_SIGNAL_MATCH_DATA,
3896 gtk_combo_box_list_button_released,
3899 g_signal_handlers_disconnect_matched (priv->popup_window,
3900 G_SIGNAL_MATCH_DATA,
3902 gtk_combo_box_child_show,
3905 g_signal_handlers_disconnect_matched (priv->popup_window,
3906 G_SIGNAL_MATCH_DATA,
3908 gtk_combo_box_child_hide,
3912 g_signal_handlers_disconnect_matched (priv->box,
3913 G_SIGNAL_MATCH_DATA,
3915 gtk_combo_box_list_button_pressed,
3918 /* destroy things (unparent will kill the latest ref from us)
3919 * last unref on button will destroy the arrow
3921 gtk_widget_unparent (priv->button);
3922 priv->button = NULL;
3925 if (priv->cell_view)
3927 g_object_set (priv->cell_view,
3928 "background-set", FALSE,
3932 if (priv->cell_view_frame)
3934 gtk_widget_unparent (priv->cell_view_frame);
3935 priv->cell_view_frame = NULL;
3939 if (priv->scroll_timer)
3941 g_source_remove (priv->scroll_timer);
3942 priv->scroll_timer = 0;
3945 if (priv->resize_idle_id)
3947 g_source_remove (priv->resize_idle_id);
3948 priv->resize_idle_id = 0;
3951 gtk_widget_destroy (priv->tree_view);
3953 priv->tree_view = NULL;
3954 if (priv->popup_widget)
3956 g_object_unref (priv->popup_widget);
3957 priv->popup_widget = NULL;
3964 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3965 GdkEventButton *event,
3968 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3969 GtkComboBoxPrivate *priv = combo_box->priv;
3971 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3973 if (ewidget == priv->popup_window)
3976 if ((ewidget != priv->button && ewidget != priv->box) ||
3977 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3980 if (priv->focus_on_click &&
3981 !gtk_widget_has_focus (priv->button))
3982 gtk_widget_grab_focus (priv->button);
3984 gtk_combo_box_popup_for_device (combo_box, event->device);
3986 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3988 priv->auto_scroll = FALSE;
3989 if (priv->scroll_timer == 0)
3990 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3991 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3994 priv->popup_in_progress = TRUE;
4000 gtk_combo_box_list_button_released (GtkWidget *widget,
4001 GdkEventButton *event,
4005 GtkTreePath *path = NULL;
4008 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4009 GtkComboBoxPrivate *priv = combo_box->priv;
4011 gboolean popup_in_progress = FALSE;
4013 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
4015 if (priv->popup_in_progress)
4017 popup_in_progress = TRUE;
4018 priv->popup_in_progress = FALSE;
4021 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
4023 if (priv->scroll_timer)
4025 g_source_remove (priv->scroll_timer);
4026 priv->scroll_timer = 0;
4029 if (ewidget != priv->tree_view)
4031 if ((ewidget == priv->button ||
4032 ewidget == priv->box) &&
4033 !popup_in_progress &&
4034 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
4036 gtk_combo_box_popdown (combo_box);
4040 /* released outside treeview */
4041 if (ewidget != priv->button && ewidget != priv->box)
4043 gtk_combo_box_popdown (combo_box);
4051 /* select something cool */
4052 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
4058 return TRUE; /* clicked outside window? */
4060 gtk_tree_model_get_iter (priv->model, &iter, path);
4061 gtk_tree_path_free (path);
4063 gtk_combo_box_popdown (combo_box);
4065 if (tree_column_row_is_sensitive (combo_box, &iter))
4066 gtk_combo_box_set_active_iter (combo_box, &iter);
4072 gtk_combo_box_menu_key_press (GtkWidget *widget,
4076 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4078 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4080 /* The menu hasn't managed the
4081 * event, forward it to the combobox
4083 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4090 gtk_combo_box_list_key_press (GtkWidget *widget,
4094 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4097 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
4098 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
4100 GtkTreeModel *model = NULL;
4102 gtk_combo_box_popdown (combo_box);
4104 if (combo_box->priv->model)
4106 GtkTreeSelection *sel;
4108 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
4110 if (gtk_tree_selection_get_selected (sel, &model, &iter))
4111 gtk_combo_box_set_active_iter (combo_box, &iter);
4117 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4119 /* The list hasn't managed the
4120 * event, forward it to the combobox
4122 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4129 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4133 GtkWidget *tree_view = combo_box->priv->tree_view;
4137 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4138 if (adj && adj->upper - adj->lower > adj->page_size)
4140 if (x <= tree_view->allocation.x &&
4141 adj->lower < adj->value)
4143 value = adj->value - (tree_view->allocation.x - x + 1);
4144 gtk_adjustment_set_value (adj, value);
4146 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4147 adj->upper - adj->page_size > adj->value)
4149 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4150 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4154 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4155 if (adj && adj->upper - adj->lower > adj->page_size)
4157 if (y <= tree_view->allocation.y &&
4158 adj->lower < adj->value)
4160 value = adj->value - (tree_view->allocation.y - y + 1);
4161 gtk_adjustment_set_value (adj, value);
4163 else if (y >= tree_view->allocation.height &&
4164 adj->upper - adj->page_size > adj->value)
4166 value = adj->value + (y - tree_view->allocation.height + 1);
4167 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4173 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4175 GtkComboBoxPrivate *priv = combo_box->priv;
4178 if (priv->auto_scroll)
4180 gdk_window_get_device_position (priv->tree_view->window,
4183 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4190 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4191 GdkEventCrossing *event,
4194 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4196 combo_box->priv->auto_scroll = TRUE;
4202 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4203 GtkTreeModel *model,
4205 gboolean path_currently_selected,
4209 gboolean sensitive = FALSE;
4211 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4213 GList *cells, *cell;
4214 gboolean cell_sensitive, cell_visible;
4216 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4218 if (!column->visible)
4221 gtk_tree_model_get_iter (model, &iter, path);
4222 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4225 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4228 g_object_get (cell->data,
4229 "sensitive", &cell_sensitive,
4230 "visible", &cell_visible,
4233 if (cell_visible && cell_sensitive)
4238 g_list_free (cells);
4240 sensitive = cell_sensitive;
4247 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4252 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4253 GtkComboBoxPrivate *priv = combo_box->priv;
4256 width = gtk_combo_box_calc_requested_width (combo_box, path);
4258 if (width > priv->width)
4260 if (priv->cell_view)
4262 gtk_widget_set_size_request (priv->cell_view, width, -1);
4263 gtk_widget_queue_resize (priv->cell_view);
4265 priv->width = width;
4270 * GtkCellLayout implementation
4274 pack_start_recurse (GtkWidget *menu,
4275 GtkCellRenderer *cell,
4282 list = gtk_container_get_children (GTK_CONTAINER (menu));
4283 for (i = list; i; i = i->next)
4285 child = gtk_bin_get_child (GTK_BIN (i->data));
4286 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (child),
4289 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4290 if (submenu != NULL)
4291 pack_start_recurse (submenu, cell, expand);
4298 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4299 GtkCellRenderer *cell,
4302 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4303 ComboCellInfo *info;
4304 GtkComboBoxPrivate *priv;
4306 priv = combo_box->priv;
4308 g_object_ref_sink (cell);
4310 info = g_slice_new0 (ComboCellInfo);
4312 info->expand = expand;
4313 info->pack = GTK_PACK_START;
4315 priv->cells = g_slist_append (priv->cells, info);
4317 if (priv->cell_view)
4318 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4322 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4324 if (GTK_IS_MENU (priv->popup_widget))
4325 pack_start_recurse (priv->popup_widget, cell, expand);
4329 pack_end_recurse (GtkWidget *menu,
4330 GtkCellRenderer *cell,
4337 list = gtk_container_get_children (GTK_CONTAINER (menu));
4338 for (i = list; i; i = i->next)
4340 child = gtk_bin_get_child (GTK_BIN (i->data));
4341 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (child),
4344 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4345 if (submenu != NULL)
4346 pack_end_recurse (submenu, cell, expand);
4353 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4354 GtkCellRenderer *cell,
4357 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4358 ComboCellInfo *info;
4359 GtkComboBoxPrivate *priv;
4361 priv = combo_box->priv;
4363 g_object_ref_sink (cell);
4365 info = g_slice_new0 (ComboCellInfo);
4367 info->expand = expand;
4368 info->pack = GTK_PACK_END;
4370 priv->cells = g_slist_append (priv->cells, info);
4372 if (priv->cell_view)
4373 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4377 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4379 if (GTK_IS_MENU (priv->popup_widget))
4380 pack_end_recurse (priv->popup_widget, cell, expand);
4384 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4386 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4388 GList *retval = NULL;
4390 for (list = combo_box->priv->cells; list; list = list->next)
4392 ComboCellInfo *info = (ComboCellInfo *)list->data;
4394 retval = g_list_prepend (retval, info->cell);
4397 return g_list_reverse (retval);
4401 clear_recurse (GtkWidget *menu)
4407 list = gtk_container_get_children (GTK_CONTAINER (menu));
4408 for (i = list; i; i = i->next)
4410 child = gtk_bin_get_child (GTK_BIN (i->data));
4411 gtk_cell_layout_clear (GTK_CELL_LAYOUT (child));
4413 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4414 if (submenu != NULL)
4415 clear_recurse (submenu);
4422 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4424 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4425 GtkComboBoxPrivate *priv = combo_box->priv;
4428 if (priv->cell_view)
4429 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4432 gtk_tree_view_column_clear (priv->column);
4434 for (i = priv->cells; i; i = i->next)
4436 ComboCellInfo *info = (ComboCellInfo *)i->data;
4438 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4439 g_object_unref (info->cell);
4440 g_slice_free (ComboCellInfo, info);
4443 g_slist_free (priv->cells);
4446 if (GTK_IS_MENU (priv->popup_widget))
4447 clear_recurse (priv->popup_widget);
4451 add_attribute_recurse (GtkWidget *menu,
4452 GtkCellRenderer *cell,
4453 const gchar *attribute,
4460 list = gtk_container_get_children (GTK_CONTAINER (menu));
4461 for (i = list; i; i = i->next)
4463 child = gtk_bin_get_child (GTK_BIN (i->data));
4464 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (child),
4465 cell, attribute, column);
4467 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4468 if (submenu != NULL)
4469 add_attribute_recurse (submenu, cell, attribute, column);
4476 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4477 GtkCellRenderer *cell,
4478 const gchar *attribute,
4481 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4482 ComboCellInfo *info;
4484 info = gtk_combo_box_get_cell_info (combo_box, cell);
4485 g_return_if_fail (info != NULL);
4487 info->attributes = g_slist_prepend (info->attributes,
4488 GINT_TO_POINTER (column));
4489 info->attributes = g_slist_prepend (info->attributes,
4490 g_strdup (attribute));
4492 if (combo_box->priv->cell_view)
4493 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4494 cell, attribute, column);
4496 if (combo_box->priv->column)
4497 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4498 cell, attribute, column);
4500 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4501 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4502 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4506 combo_cell_data_func (GtkCellLayout *cell_layout,
4507 GtkCellRenderer *cell,
4508 GtkTreeModel *tree_model,
4512 ComboCellInfo *info = (ComboCellInfo *)data;
4513 GtkWidget *parent = NULL;
4518 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4520 if (GTK_IS_WIDGET (cell_layout))
4521 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4523 if (GTK_IS_MENU_ITEM (parent) &&
4524 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4525 g_object_set (cell, "sensitive", TRUE, NULL);
4530 set_cell_data_func_recurse (GtkWidget *menu,
4531 GtkCellRenderer *cell,
4532 ComboCellInfo *info)
4536 GtkWidget *cell_view;
4538 list = gtk_container_get_children (GTK_CONTAINER (menu));
4539 for (i = list; i; i = i->next)
4541 cell_view = gtk_bin_get_child (GTK_BIN (i->data));
4542 if (GTK_IS_CELL_LAYOUT (cell_view))
4544 /* Override sensitivity for inner nodes; we don't
4545 * want menuitems with submenus to appear insensitive
4547 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4549 combo_cell_data_func,
4551 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4552 if (submenu != NULL)
4553 set_cell_data_func_recurse (submenu, cell, info);
4561 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4562 GtkCellRenderer *cell,
4563 GtkCellLayoutDataFunc func,
4565 GDestroyNotify destroy)
4567 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4568 GtkComboBoxPrivate *priv = combo_box->priv;
4569 ComboCellInfo *info;
4571 info = gtk_combo_box_get_cell_info (combo_box, cell);
4572 g_return_if_fail (info != NULL);
4576 GDestroyNotify d = info->destroy;
4578 info->destroy = NULL;
4579 d (info->func_data);
4583 info->func_data = func_data;
4584 info->destroy = destroy;
4586 if (priv->cell_view)
4587 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4590 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4592 if (GTK_IS_MENU (priv->popup_widget))
4593 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4595 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4599 clear_attributes_recurse (GtkWidget *menu,
4600 GtkCellRenderer *cell)
4606 list = gtk_container_get_children (GTK_CONTAINER (menu));
4607 for (i = list; i; i = i->next)
4609 child = gtk_bin_get_child (GTK_BIN (i->data));
4610 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (child), cell);
4612 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4613 if (submenu != NULL)
4614 clear_attributes_recurse (submenu, cell);
4621 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4622 GtkCellRenderer *cell)
4624 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4625 GtkComboBoxPrivate *priv;
4626 ComboCellInfo *info;
4629 priv = combo_box->priv;
4631 info = gtk_combo_box_get_cell_info (combo_box, cell);
4632 g_return_if_fail (info != NULL);
4634 list = info->attributes;
4635 while (list && list->next)
4637 g_free (list->data);
4638 list = list->next->next;
4640 g_slist_free (info->attributes);
4641 info->attributes = NULL;
4643 if (priv->cell_view)
4644 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4647 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4649 if (GTK_IS_MENU (priv->popup_widget))
4650 clear_attributes_recurse (priv->popup_widget, cell);
4652 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4656 reorder_recurse (GtkWidget *menu,
4657 GtkCellRenderer *cell,
4664 list = gtk_container_get_children (GTK_CONTAINER (menu));
4665 for (i = list; i; i = i->next)
4667 child = gtk_bin_get_child (GTK_BIN (i->data));
4668 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (child),
4671 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4672 if (submenu != NULL)
4673 reorder_recurse (submenu, cell, position);
4680 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4681 GtkCellRenderer *cell,
4684 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4685 GtkComboBoxPrivate *priv;
4686 ComboCellInfo *info;
4689 priv = combo_box->priv;
4691 info = gtk_combo_box_get_cell_info (combo_box, cell);
4693 g_return_if_fail (info != NULL);
4694 g_return_if_fail (position >= 0);
4696 link = g_slist_find (priv->cells, info);
4698 g_return_if_fail (link != NULL);
4700 priv->cells = g_slist_delete_link (priv->cells, link);
4701 priv->cells = g_slist_insert (priv->cells, info, position);
4703 if (priv->cell_view)
4704 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4708 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4711 if (GTK_IS_MENU (priv->popup_widget))
4712 reorder_recurse (priv->popup_widget, cell, position);
4714 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4722 * gtk_combo_box_new:
4724 * Creates a new empty #GtkComboBox.
4726 * Return value: A new #GtkComboBox.
4731 gtk_combo_box_new (void)
4733 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4737 * gtk_combo_box_new_with_model:
4738 * @model: A #GtkTreeModel.
4740 * Creates a new #GtkComboBox with the model initialized to @model.
4742 * Return value: A new #GtkComboBox.
4747 gtk_combo_box_new_with_model (GtkTreeModel *model)
4749 GtkComboBox *combo_box;
4751 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4753 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4755 return GTK_WIDGET (combo_box);
4759 * gtk_combo_box_get_wrap_width:
4760 * @combo_box: A #GtkComboBox
4762 * Returns the wrap width which is used to determine the number of columns
4763 * for the popup menu. If the wrap width is larger than 1, the combo box
4766 * Returns: the wrap width.
4771 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4773 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4775 return combo_box->priv->wrap_width;
4779 * gtk_combo_box_set_wrap_width:
4780 * @combo_box: A #GtkComboBox
4781 * @width: Preferred number of columns
4783 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4784 * the preferred number of columns when you want the popup to be layed out
4790 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4793 GtkComboBoxPrivate *priv;
4795 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4796 g_return_if_fail (width >= 0);
4798 priv = combo_box->priv;
4800 if (width != priv->wrap_width)
4802 priv->wrap_width = width;
4804 gtk_combo_box_check_appearance (combo_box);
4805 gtk_combo_box_relayout (combo_box);
4807 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4812 * gtk_combo_box_get_row_span_column:
4813 * @combo_box: A #GtkComboBox
4815 * Returns the column with row span information for @combo_box.
4817 * Returns: the row span column.
4822 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4824 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4826 return combo_box->priv->row_column;
4830 * gtk_combo_box_set_row_span_column:
4831 * @combo_box: A #GtkComboBox.
4832 * @row_span: A column in the model passed during construction.
4834 * Sets the column with row span information for @combo_box to be @row_span.
4835 * The row span column contains integers which indicate how many rows
4836 * an item should span.
4841 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4844 GtkComboBoxPrivate *priv;
4847 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4849 priv = combo_box->priv;
4851 col = gtk_tree_model_get_n_columns (priv->model);
4852 g_return_if_fail (row_span >= -1 && row_span < col);
4854 if (row_span != priv->row_column)
4856 priv->row_column = row_span;
4858 gtk_combo_box_relayout (combo_box);
4860 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4865 * gtk_combo_box_get_column_span_column:
4866 * @combo_box: A #GtkComboBox
4868 * Returns the column with column span information for @combo_box.
4870 * Returns: the column span column.
4875 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4877 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4879 return combo_box->priv->col_column;
4883 * gtk_combo_box_set_column_span_column:
4884 * @combo_box: A #GtkComboBox
4885 * @column_span: A column in the model passed during construction
4887 * Sets the column with column span information for @combo_box to be
4888 * @column_span. The column span column contains integers which indicate
4889 * how many columns an item should span.
4894 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4897 GtkComboBoxPrivate *priv;
4900 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4902 priv = combo_box->priv;
4904 col = gtk_tree_model_get_n_columns (priv->model);
4905 g_return_if_fail (column_span >= -1 && column_span < col);
4907 if (column_span != priv->col_column)
4909 priv->col_column = column_span;
4911 gtk_combo_box_relayout (combo_box);
4913 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4918 * gtk_combo_box_get_active:
4919 * @combo_box: A #GtkComboBox
4921 * Returns the index of the currently active item, or -1 if there's no
4922 * active item. If the model is a non-flat treemodel, and the active item
4923 * is not an immediate child of the root of the tree, this function returns
4924 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4925 * <literal>path</literal> is the #GtkTreePath of the active item.
4927 * Return value: An integer which is the index of the currently active item,
4928 * or -1 if there's no active item.
4933 gtk_combo_box_get_active (GtkComboBox *combo_box)
4935 GtkComboBoxPrivate *priv;
4938 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4940 priv = combo_box->priv;
4942 if (gtk_tree_row_reference_valid (priv->active_row))
4946 path = gtk_tree_row_reference_get_path (priv->active_row);
4947 result = gtk_tree_path_get_indices (path)[0];
4948 gtk_tree_path_free (path);
4957 * gtk_combo_box_set_active:
4958 * @combo_box: A #GtkComboBox
4959 * @index_: An index in the model passed during construction, or -1 to have
4962 * Sets the active item of @combo_box to be the item at @index.
4967 gtk_combo_box_set_active (GtkComboBox *combo_box,
4970 GtkTreePath *path = NULL;
4971 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4972 g_return_if_fail (index_ >= -1);
4974 if (combo_box->priv->model == NULL)
4976 /* Save index, in case the model is set after the index */
4977 combo_box->priv->active = index_;
4983 path = gtk_tree_path_new_from_indices (index_, -1);
4985 gtk_combo_box_set_active_internal (combo_box, path);
4988 gtk_tree_path_free (path);
4992 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4995 GtkComboBoxPrivate *priv = combo_box->priv;
4996 GtkTreePath *active_path;
4999 /* Remember whether the initially active row is valid. */
5000 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
5002 if (path && is_valid_row_reference)
5004 active_path = gtk_tree_row_reference_get_path (priv->active_row);
5005 path_cmp = gtk_tree_path_compare (path, active_path);
5006 gtk_tree_path_free (active_path);
5011 if (priv->active_row)
5013 gtk_tree_row_reference_free (priv->active_row);
5014 priv->active_row = NULL;
5019 if (priv->tree_view)
5020 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
5023 GtkMenu *menu = GTK_MENU (priv->popup_widget);
5025 if (GTK_IS_MENU (menu))
5026 gtk_menu_set_active (menu, -1);
5029 if (priv->cell_view)
5030 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
5033 * Do not emit a "changed" signal when an already invalid selection was
5034 * now set to invalid.
5036 if (!is_valid_row_reference)
5042 gtk_tree_row_reference_new (priv->model, path);
5044 if (priv->tree_view)
5046 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
5049 else if (GTK_IS_MENU (priv->popup_widget))
5051 /* FIXME handle nested menus better */
5052 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
5053 gtk_tree_path_get_indices (path)[0]);
5056 if (priv->cell_view)
5057 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
5061 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
5062 g_object_notify (G_OBJECT (combo_box), "active");
5067 * gtk_combo_box_get_active_iter:
5068 * @combo_box: A #GtkComboBox
5069 * @iter: (out): The uninitialized #GtkTreeIter
5071 * Sets @iter to point to the current active item, if it exists.
5073 * Return value: %TRUE, if @iter was set
5078 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
5084 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5086 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
5089 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
5090 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
5091 gtk_tree_path_free (path);
5097 * gtk_combo_box_set_active_iter:
5098 * @combo_box: A #GtkComboBox
5099 * @iter: (allow-none): The #GtkTreeIter, or %NULL
5101 * Sets the current active item to be the one referenced by @iter, or
5102 * unsets the active item if @iter is %NULL.
5104 * @iter must correspond to a path of depth one, or be %NULL.
5109 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
5112 GtkTreePath *path = NULL;
5114 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5117 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
5119 gtk_combo_box_set_active_internal (combo_box, path);
5120 gtk_tree_path_free (path);
5124 * gtk_combo_box_set_model:
5125 * @combo_box: A #GtkComboBox
5126 * @model: (allow-none): A #GtkTreeModel
5128 * Sets the model used by @combo_box to be @model. Will unset a previously set
5129 * model (if applicable). If model is %NULL, then it will unset the model.
5131 * Note that this function does not clear the cell renderers, you have to
5132 * call gtk_cell_layout_clear() yourself if you need to set up different
5133 * cell renderers for the new model.
5138 gtk_combo_box_set_model (GtkComboBox *combo_box,
5139 GtkTreeModel *model)
5141 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5142 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5144 if (model == combo_box->priv->model)
5147 gtk_combo_box_unset_model (combo_box);
5152 combo_box->priv->model = model;
5153 g_object_ref (combo_box->priv->model);
5155 combo_box->priv->inserted_id =
5156 g_signal_connect (combo_box->priv->model, "row-inserted",
5157 G_CALLBACK (gtk_combo_box_model_row_inserted),
5159 combo_box->priv->deleted_id =
5160 g_signal_connect (combo_box->priv->model, "row-deleted",
5161 G_CALLBACK (gtk_combo_box_model_row_deleted),
5163 combo_box->priv->reordered_id =
5164 g_signal_connect (combo_box->priv->model, "rows-reordered",
5165 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5167 combo_box->priv->changed_id =
5168 g_signal_connect (combo_box->priv->model, "row-changed",
5169 G_CALLBACK (gtk_combo_box_model_row_changed),
5172 if (combo_box->priv->tree_view)
5175 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5176 combo_box->priv->model);
5177 gtk_combo_box_list_popup_resize (combo_box);
5182 if (combo_box->priv->popup_widget)
5183 gtk_combo_box_menu_fill (combo_box);
5187 if (combo_box->priv->cell_view)
5188 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5189 combo_box->priv->model);
5191 if (combo_box->priv->active != -1)
5193 /* If an index was set in advance, apply it now */
5194 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5195 combo_box->priv->active = -1;
5199 gtk_combo_box_update_sensitivity (combo_box);
5201 g_object_notify (G_OBJECT (combo_box), "model");
5205 * gtk_combo_box_get_model
5206 * @combo_box: A #GtkComboBox
5208 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5210 * Return value: (transfer none): A #GtkTreeModel which was passed during construction.
5215 gtk_combo_box_get_model (GtkComboBox *combo_box)
5217 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5219 return combo_box->priv->model;
5223 /* convenience API for simple text combos */
5226 * gtk_combo_box_new_text:
5228 * Convenience function which constructs a new text combo box, which is a
5229 * #GtkComboBox just displaying strings. If you use this function to create
5230 * a text combo box, you should only manipulate its data source with the
5231 * following convenience functions: gtk_combo_box_append_text(),
5232 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5233 * gtk_combo_box_remove_text().
5235 * Return value: (transfer none): A new text combo box.
5240 gtk_combo_box_new_text (void)
5242 GtkWidget *combo_box;
5243 GtkCellRenderer *cell;
5244 GtkListStore *store;
5246 store = gtk_list_store_new (1, G_TYPE_STRING);
5247 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5248 g_object_unref (store);
5250 cell = gtk_cell_renderer_text_new ();
5251 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5252 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5260 * gtk_combo_box_append_text:
5261 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5264 * Appends @string to the list of strings stored in @combo_box. Note that
5265 * you can only use this function with combo boxes constructed with
5266 * gtk_combo_box_new_text().
5271 gtk_combo_box_append_text (GtkComboBox *combo_box,
5275 GtkListStore *store;
5277 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5278 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5279 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5281 g_return_if_fail (text != NULL);
5283 store = GTK_LIST_STORE (combo_box->priv->model);
5285 gtk_list_store_append (store, &iter);
5286 gtk_list_store_set (store, &iter, 0, text, -1);
5290 * gtk_combo_box_insert_text:
5291 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5292 * @position: An index to insert @text
5295 * Inserts @string at @position in the list of strings stored in @combo_box.
5296 * Note that you can only use this function with combo boxes constructed
5297 * with gtk_combo_box_new_text().
5302 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5307 GtkListStore *store;
5309 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5310 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5311 g_return_if_fail (position >= 0);
5312 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5314 g_return_if_fail (text != NULL);
5316 store = GTK_LIST_STORE (combo_box->priv->model);
5318 gtk_list_store_insert (store, &iter, position);
5319 gtk_list_store_set (store, &iter, 0, text, -1);
5323 * gtk_combo_box_prepend_text:
5324 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5327 * Prepends @string to the list of strings stored in @combo_box. Note that
5328 * you can only use this function with combo boxes constructed with
5329 * gtk_combo_box_new_text().
5334 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5338 GtkListStore *store;
5340 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5341 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5342 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5344 g_return_if_fail (text != NULL);
5346 store = GTK_LIST_STORE (combo_box->priv->model);
5348 gtk_list_store_prepend (store, &iter);
5349 gtk_list_store_set (store, &iter, 0, text, -1);
5353 * gtk_combo_box_remove_text:
5354 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5355 * @position: Index of the item to remove
5357 * Removes the string at @position from @combo_box. Note that you can only use
5358 * this function with combo boxes constructed with gtk_combo_box_new_text().
5363 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5367 GtkListStore *store;
5369 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5370 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5371 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5373 g_return_if_fail (position >= 0);
5375 store = GTK_LIST_STORE (combo_box->priv->model);
5377 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5379 gtk_list_store_remove (store, &iter);
5383 * gtk_combo_box_get_active_text:
5384 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5386 * Returns the currently active string in @combo_box or %NULL if none
5387 * is selected. Note that you can only use this function with combo
5388 * boxes constructed with gtk_combo_box_new_text() and with
5389 * #GtkComboBoxEntry<!-- -->s.
5391 * Returns: a newly allocated string containing the currently active text.
5392 * Must be freed with g_free().
5397 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5399 GtkComboBoxClass *class;
5401 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5403 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5405 if (class->get_active_text)
5406 return class->get_active_text (combo_box);
5412 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5417 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5418 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5419 == G_TYPE_STRING, NULL);
5421 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5422 gtk_tree_model_get (combo_box->priv->model, &iter,
5429 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5430 GtkScrollType scroll)
5433 GtkTreeIter new_iter;
5434 gboolean active_iter;
5437 if (!combo_box->priv->model)
5439 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5443 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5447 case GTK_SCROLL_STEP_BACKWARD:
5448 case GTK_SCROLL_STEP_UP:
5449 case GTK_SCROLL_STEP_LEFT:
5452 found = tree_prev (combo_box, combo_box->priv->model,
5453 &iter, &new_iter, FALSE);
5456 /* else fall through */
5458 case GTK_SCROLL_PAGE_FORWARD:
5459 case GTK_SCROLL_PAGE_DOWN:
5460 case GTK_SCROLL_PAGE_RIGHT:
5461 case GTK_SCROLL_END:
5462 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5465 case GTK_SCROLL_STEP_FORWARD:
5466 case GTK_SCROLL_STEP_DOWN:
5467 case GTK_SCROLL_STEP_RIGHT:
5470 found = tree_next (combo_box, combo_box->priv->model,
5471 &iter, &new_iter, FALSE);
5474 /* else fall through */
5476 case GTK_SCROLL_PAGE_BACKWARD:
5477 case GTK_SCROLL_PAGE_UP:
5478 case GTK_SCROLL_PAGE_LEFT:
5479 case GTK_SCROLL_START:
5480 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5487 if (found && active_iter)
5489 GtkTreePath *old_path;
5490 GtkTreePath *new_path;
5492 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5493 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5495 if (gtk_tree_path_compare (old_path, new_path) == 0)
5498 gtk_tree_path_free (old_path);
5499 gtk_tree_path_free (new_path);
5504 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5508 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5513 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5514 gboolean group_cycling)
5516 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5518 gtk_widget_grab_focus (combo_box->priv->button);
5524 gtk_combo_box_grab_focus (GtkWidget *widget)
5526 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5528 gtk_widget_grab_focus (combo_box->priv->button);
5532 gtk_combo_box_destroy (GtkObject *object)
5534 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5536 if (combo_box->priv->popup_idle_id > 0)
5538 g_source_remove (combo_box->priv->popup_idle_id);
5539 combo_box->priv->popup_idle_id = 0;
5542 gtk_combo_box_popdown (combo_box);
5544 if (combo_box->priv->row_separator_destroy)
5545 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5547 combo_box->priv->row_separator_func = NULL;
5548 combo_box->priv->row_separator_data = NULL;
5549 combo_box->priv->row_separator_destroy = NULL;
5551 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5552 combo_box->priv->cell_view = NULL;
5556 gtk_combo_box_dispose(GObject* object)
5558 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5560 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5562 gtk_combo_box_menu_destroy (combo_box);
5563 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5564 combo_box->priv->popup_widget = NULL;
5567 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5571 gtk_combo_box_finalize (GObject *object)
5573 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5576 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5577 gtk_combo_box_list_destroy (combo_box);
5579 if (combo_box->priv->popup_window)
5580 gtk_widget_destroy (combo_box->priv->popup_window);
5582 gtk_combo_box_unset_model (combo_box);
5584 for (i = combo_box->priv->cells; i; i = i->next)
5586 ComboCellInfo *info = (ComboCellInfo *)i->data;
5587 GSList *list = info->attributes;
5590 info->destroy (info->func_data);
5592 while (list && list->next)
5594 g_free (list->data);
5595 list = list->next->next;
5597 g_slist_free (info->attributes);
5599 g_object_unref (info->cell);
5600 g_slice_free (ComboCellInfo, info);
5602 g_slist_free (combo_box->priv->cells);
5604 g_free (combo_box->priv->tearoff_title);
5606 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5610 gtk_cell_editable_key_press (GtkWidget *widget,
5614 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5616 if (event->keyval == GDK_Escape)
5618 g_object_set (combo_box,
5619 "editing-canceled", TRUE,
5621 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5622 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5626 else if (event->keyval == GDK_Return ||
5627 event->keyval == GDK_ISO_Enter ||
5628 event->keyval == GDK_KP_Enter)
5630 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5631 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5640 popdown_idle (gpointer data)
5642 GtkComboBox *combo_box;
5644 combo_box = GTK_COMBO_BOX (data);
5646 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5647 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5649 g_object_unref (combo_box);
5655 popdown_handler (GtkWidget *widget,
5658 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5662 popup_idle (gpointer data)
5664 GtkComboBox *combo_box;
5666 combo_box = GTK_COMBO_BOX (data);
5668 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5669 combo_box->priv->cell_view)
5670 g_signal_connect_object (combo_box->priv->popup_widget,
5671 "unmap", G_CALLBACK (popdown_handler),
5674 /* we unset this if a menu item is activated */
5675 g_object_set (combo_box,
5676 "editing-canceled", TRUE,
5678 gtk_combo_box_popup (combo_box);
5680 combo_box->priv->popup_idle_id = 0;
5681 combo_box->priv->activate_button = 0;
5682 combo_box->priv->activate_time = 0;
5688 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5691 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5694 combo_box->priv->is_cell_renderer = TRUE;
5696 if (combo_box->priv->cell_view)
5698 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5699 G_CALLBACK (gtk_cell_editable_key_press),
5702 gtk_widget_grab_focus (combo_box->priv->button);
5706 child = gtk_bin_get_child (GTK_BIN (combo_box));
5708 g_signal_connect_object (child, "key-press-event",
5709 G_CALLBACK (gtk_cell_editable_key_press),
5712 gtk_widget_grab_focus (child);
5713 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5716 /* we do the immediate popup only for the optionmenu-like
5719 if (combo_box->priv->is_cell_renderer &&
5720 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5722 if (event && event->type == GDK_BUTTON_PRESS)
5724 GdkEventButton *event_button = (GdkEventButton *)event;
5726 combo_box->priv->activate_button = event_button->button;
5727 combo_box->priv->activate_time = event_button->time;
5730 combo_box->priv->popup_idle_id =
5731 gdk_threads_add_idle (popup_idle, combo_box);
5737 * gtk_combo_box_get_add_tearoffs:
5738 * @combo_box: a #GtkComboBox
5740 * Gets the current value of the :add-tearoffs property.
5742 * Return value: the current value of the :add-tearoffs property.
5745 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5747 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5749 return combo_box->priv->add_tearoffs;
5753 * gtk_combo_box_set_add_tearoffs:
5754 * @combo_box: a #GtkComboBox
5755 * @add_tearoffs: %TRUE to add tearoff menu items
5757 * Sets whether the popup menu should have a tearoff
5763 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5764 gboolean add_tearoffs)
5766 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5768 add_tearoffs = add_tearoffs != FALSE;
5770 if (combo_box->priv->add_tearoffs != add_tearoffs)
5772 combo_box->priv->add_tearoffs = add_tearoffs;
5773 gtk_combo_box_check_appearance (combo_box);
5774 gtk_combo_box_relayout (combo_box);
5775 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5780 * gtk_combo_box_get_title:
5781 * @combo_box: a #GtkComboBox
5783 * Gets the current title of the menu in tearoff mode. See
5784 * gtk_combo_box_set_add_tearoffs().
5786 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5787 * string which must not be freed.
5791 G_CONST_RETURN gchar*
5792 gtk_combo_box_get_title (GtkComboBox *combo_box)
5794 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5796 return combo_box->priv->tearoff_title;
5800 gtk_combo_box_update_title (GtkComboBox *combo_box)
5802 gtk_combo_box_check_appearance (combo_box);
5804 if (combo_box->priv->popup_widget &&
5805 GTK_IS_MENU (combo_box->priv->popup_widget))
5806 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5807 combo_box->priv->tearoff_title);
5811 * gtk_combo_box_set_title:
5812 * @combo_box: a #GtkComboBox
5813 * @title: a title for the menu in tearoff mode
5815 * Sets the menu's title in tearoff mode.
5820 gtk_combo_box_set_title (GtkComboBox *combo_box,
5823 GtkComboBoxPrivate *priv;
5825 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5827 priv = combo_box->priv;
5829 if (strcmp (title ? title : "",
5830 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5832 g_free (priv->tearoff_title);
5833 priv->tearoff_title = g_strdup (title);
5835 gtk_combo_box_update_title (combo_box);
5837 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5842 * gtk_combo_box_get_popup_accessible:
5843 * @combo_box: a #GtkComboBox
5845 * Gets the accessible object corresponding to the combo box's popup.
5847 * This function is mostly intended for use by accessibility technologies;
5848 * applications should have little use for it.
5850 * Returns: the accessible object corresponding to the combo box's popup.
5855 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5859 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5861 if (combo_box->priv->popup_widget)
5863 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5871 * gtk_combo_box_get_row_separator_func:
5872 * @combo_box: a #GtkComboBox
5874 * Returns the current row separator function.
5876 * Return value: the current row separator function.
5880 GtkTreeViewRowSeparatorFunc
5881 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5883 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5885 return combo_box->priv->row_separator_func;
5889 * gtk_combo_box_set_row_separator_func:
5890 * @combo_box: a #GtkComboBox
5891 * @func: a #GtkTreeViewRowSeparatorFunc
5892 * @data: (allow-none): user data to pass to @func, or %NULL
5893 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5895 * Sets the row separator function, which is used to determine
5896 * whether a row should be drawn as a separator. If the row separator
5897 * function is %NULL, no separators are drawn. This is the default value.
5902 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5903 GtkTreeViewRowSeparatorFunc func,
5905 GDestroyNotify destroy)
5907 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5909 if (combo_box->priv->row_separator_destroy)
5910 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5912 combo_box->priv->row_separator_func = func;
5913 combo_box->priv->row_separator_data = data;
5914 combo_box->priv->row_separator_destroy = destroy;
5916 if (combo_box->priv->tree_view)
5917 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5920 gtk_combo_box_relayout (combo_box);
5922 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5926 * gtk_combo_box_set_button_sensitivity:
5927 * @combo_box: a #GtkComboBox
5928 * @sensitivity: specify the sensitivity of the dropdown button
5930 * Sets whether the dropdown button of the combo box should be
5931 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5932 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5937 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5938 GtkSensitivityType sensitivity)
5940 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5942 if (combo_box->priv->button_sensitivity != sensitivity)
5944 combo_box->priv->button_sensitivity = sensitivity;
5945 gtk_combo_box_update_sensitivity (combo_box);
5947 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5952 * gtk_combo_box_get_button_sensitivity:
5953 * @combo_box: a #GtkComboBox
5955 * Returns whether the combo box sets the dropdown button
5956 * sensitive or not when there are no items in the model.
5958 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5959 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5960 * if the button is always insensitive or
5961 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5962 * the model has one item to be selected.
5967 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5969 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5971 return combo_box->priv->button_sensitivity;
5976 * gtk_combo_box_set_focus_on_click:
5977 * @combo: a #GtkComboBox
5978 * @focus_on_click: whether the combo box grabs focus when clicked
5981 * Sets whether the combo box will grab focus when it is clicked with
5982 * the mouse. Making mouse clicks not grab focus is useful in places
5983 * like toolbars where you don't want the keyboard focus removed from
5984 * the main area of the application.
5989 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5990 gboolean focus_on_click)
5992 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5994 focus_on_click = focus_on_click != FALSE;
5996 if (combo_box->priv->focus_on_click != focus_on_click)
5998 combo_box->priv->focus_on_click = focus_on_click;
6000 if (combo_box->priv->button)
6001 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
6004 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
6009 * gtk_combo_box_get_focus_on_click:
6010 * @combo: a #GtkComboBox
6012 * Returns whether the combo box grabs focus when it is clicked
6013 * with the mouse. See gtk_combo_box_set_focus_on_click().
6015 * Return value: %TRUE if the combo box grabs focus when it is
6016 * clicked with the mouse.
6021 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
6023 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
6025 return combo_box->priv->focus_on_click;
6030 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
6031 GtkBuilder *builder,
6033 const gchar *tagname,
6034 GMarkupParser *parser,
6037 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6038 tagname, parser, data))
6041 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6042 tagname, parser, data);
6046 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
6047 GtkBuilder *builder,
6049 const gchar *tagname,
6052 if (strcmp (tagname, "attributes") == 0)
6053 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
6056 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,