2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcombobox.h"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellview.h"
28 #include "gtkeventbox.h"
31 #include "gtkliststore.h"
34 #include "gtkscrolledwindow.h"
35 #include "gtkseparatormenuitem.h"
36 #include "gtktearoffmenuitem.h"
37 #include "gtktogglebutton.h"
38 #include "gtktreeselection.h"
39 #include "gtkvseparator.h"
40 #include "gtkwindow.h"
41 #include "gtkprivate.h"
43 #include <gdk/gdkkeysyms.h>
45 #include <gobject/gvaluecollector.h>
50 #include "gtkmarshalers.h"
53 #include "gtktreeprivate.h"
56 /* WELCOME, to THE house of evil code */
58 typedef struct _ComboCellInfo ComboCellInfo;
61 GtkCellRenderer *cell;
64 GtkCellLayoutDataFunc func;
66 GDestroyNotify destroy;
72 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
74 struct _GtkComboBoxPrivate
82 GtkShadowType shadow_type;
84 GtkTreeRowReference *active_row;
87 GtkTreeViewColumn *column;
90 GtkWidget *cell_view_frame;
97 GtkWidget *popup_widget;
98 GtkWidget *popup_window;
99 GtkWidget *scrolled_window;
106 guint activate_button;
107 guint32 activate_time;
109 guint resize_idle_id;
115 guint popup_in_progress : 1;
116 guint popup_shown : 1;
117 guint add_tearoffs : 1;
119 guint is_cell_renderer : 1;
120 guint editing_canceled : 1;
121 guint auto_scroll : 1;
122 guint focus_on_click : 1;
123 guint button_sensitivity : 2;
125 GtkTreeViewRowSeparatorFunc row_separator_func;
126 gpointer row_separator_data;
127 GDestroyNotify row_separator_destroy;
129 gchar *tearoff_title;
132 /* While debugging this evil code, I have learned that
133 * there are actually 4 modes to this widget, which can
134 * be characterized as follows
136 * 1) menu mode, no child added
139 * cell_view -> GtkCellView, regular child
140 * cell_view_frame -> NULL
141 * button -> GtkToggleButton set_parent to combo
142 * arrow -> GtkArrow set_parent to button
143 * separator -> GtkVSepator set_parent to button
144 * popup_widget -> GtkMenu
145 * popup_window -> NULL
146 * scrolled_window -> NULL
148 * 2) menu mode, child added
152 * cell_view_frame -> NULL
153 * button -> GtkToggleButton set_parent to combo
154 * arrow -> GtkArrow, child of button
156 * popup_widget -> GtkMenu
157 * popup_window -> NULL
158 * scrolled_window -> NULL
160 * 3) list mode, no child added
162 * tree_view -> GtkTreeView, child of scrolled_window
163 * cell_view -> GtkCellView, regular child
164 * cell_view_frame -> GtkFrame, set parent to combo
165 * button -> GtkToggleButton, set_parent to combo
166 * arrow -> GtkArrow, child of button
168 * popup_widget -> tree_view
169 * popup_window -> GtkWindow
170 * scrolled_window -> GtkScrolledWindow, child of popup_window
172 * 4) list mode, child added
174 * tree_view -> GtkTreeView, child of scrolled_window
176 * cell_view_frame -> NULL
177 * button -> GtkToggleButton, set_parent to combo
178 * arrow -> GtkArrow, child of button
180 * popup_widget -> tree_view
181 * popup_window -> GtkWindow
182 * scrolled_window -> GtkScrolledWindow, child of popup_window
198 PROP_ROW_SPAN_COLUMN,
199 PROP_COLUMN_SPAN_COLUMN,
206 PROP_BUTTON_SENSITIVITY
209 static guint combo_box_signals[LAST_SIGNAL] = {0,};
211 #define BONUS_PADDING 4
212 #define SCROLL_TIME 100
216 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
217 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
218 static void gtk_combo_box_dispose (GObject *object);
219 static void gtk_combo_box_finalize (GObject *object);
220 static void gtk_combo_box_destroy (GtkObject *object);
222 static void gtk_combo_box_set_property (GObject *object,
226 static void gtk_combo_box_get_property (GObject *object,
231 static void gtk_combo_box_state_changed (GtkWidget *widget,
232 GtkStateType previous);
233 static void gtk_combo_box_grab_focus (GtkWidget *widget);
234 static void gtk_combo_box_style_set (GtkWidget *widget,
236 static void gtk_combo_box_button_toggled (GtkWidget *widget,
238 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
239 GtkStateType previous,
241 static void gtk_combo_box_add (GtkContainer *container,
243 static void gtk_combo_box_remove (GtkContainer *container,
246 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
247 GtkCellRenderer *cell);
249 static void gtk_combo_box_menu_show (GtkWidget *menu,
251 static void gtk_combo_box_menu_hide (GtkWidget *menu,
254 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
256 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
261 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
266 static void gtk_combo_box_menu_position (GtkMenu *menu,
272 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
274 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
276 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
278 static void gtk_combo_box_size_request (GtkWidget *widget,
279 GtkRequisition *requisition);
280 static void gtk_combo_box_size_allocate (GtkWidget *widget,
281 GtkAllocation *allocation);
282 static void gtk_combo_box_forall (GtkContainer *container,
283 gboolean include_internals,
284 GtkCallback callback,
285 gpointer callback_data);
286 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
287 GdkEventExpose *event);
288 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
289 GdkEventScroll *event);
290 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
293 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
294 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
295 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
296 GtkScrollType scroll);
297 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
298 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
300 /* listening to the model */
301 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
305 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
308 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
313 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
317 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
323 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
328 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
329 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
331 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
332 GdkEventButton *event,
334 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
337 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
338 GdkEventCrossing *event,
340 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
343 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
344 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
345 GdkEventButton *event,
348 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
351 gboolean path_currently_selected,
354 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
358 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
361 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
362 gboolean add_children);
363 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
364 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
367 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
368 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
370 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
374 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
376 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
377 GdkEventButton *event,
379 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
381 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
385 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
388 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
393 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
397 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
400 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
402 guint32 activate_time);
403 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
408 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
409 GtkCellRenderer *cell,
411 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
412 GtkCellRenderer *cell,
414 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
415 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
416 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
417 GtkCellRenderer *cell,
418 const gchar *attribute,
420 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
421 GtkCellRenderer *cell,
422 GtkCellLayoutDataFunc func,
424 GDestroyNotify destroy);
425 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
426 GtkCellRenderer *cell);
427 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
428 GtkCellRenderer *cell,
430 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
431 gboolean group_cycling);
433 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
434 GtkCellLayout *cell_layout);
435 static void combo_cell_data_func (GtkCellLayout *cell_layout,
436 GtkCellRenderer *cell,
437 GtkTreeModel *tree_model,
440 static void gtk_combo_box_child_show (GtkWidget *widget,
441 GtkComboBox *combo_box);
442 static void gtk_combo_box_child_hide (GtkWidget *widget,
443 GtkComboBox *combo_box);
445 /* GtkBuildable method implementation */
446 static GtkBuildableIface *parent_buildable_iface;
448 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
449 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
452 const gchar *tagname,
453 GMarkupParser *parser,
455 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
458 const gchar *tagname,
461 /* GtkCellEditable method implementations */
462 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
466 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
467 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
468 gtk_combo_box_cell_layout_init)
469 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
470 gtk_combo_box_cell_editable_init)
471 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
472 gtk_combo_box_buildable_init))
477 gtk_combo_box_class_init (GtkComboBoxClass *klass)
479 GObjectClass *object_class;
480 GtkObjectClass *gtk_object_class;
481 GtkContainerClass *container_class;
482 GtkWidgetClass *widget_class;
483 GtkBindingSet *binding_set;
485 klass->get_active_text = gtk_combo_box_real_get_active_text;
487 container_class = (GtkContainerClass *)klass;
488 container_class->forall = gtk_combo_box_forall;
489 container_class->add = gtk_combo_box_add;
490 container_class->remove = gtk_combo_box_remove;
492 widget_class = (GtkWidgetClass *)klass;
493 widget_class->size_allocate = gtk_combo_box_size_allocate;
494 widget_class->size_request = gtk_combo_box_size_request;
495 widget_class->expose_event = gtk_combo_box_expose_event;
496 widget_class->scroll_event = gtk_combo_box_scroll_event;
497 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
498 widget_class->grab_focus = gtk_combo_box_grab_focus;
499 widget_class->style_set = gtk_combo_box_style_set;
500 widget_class->state_changed = gtk_combo_box_state_changed;
502 gtk_object_class = (GtkObjectClass *)klass;
503 gtk_object_class->destroy = gtk_combo_box_destroy;
505 object_class = (GObjectClass *)klass;
506 object_class->dispose = gtk_combo_box_dispose;
507 object_class->finalize = gtk_combo_box_finalize;
508 object_class->set_property = gtk_combo_box_set_property;
509 object_class->get_property = gtk_combo_box_get_property;
513 * GtkComboBox::changed:
514 * @widget: the object which received the signal
516 * The changed signal is emitted when the active
517 * item is changed. The can be due to the user selecting
518 * a different item from the list, or due to a
519 * call to gtk_combo_box_set_active_iter().
520 * It will also be emitted while typing into a GtkComboBoxEntry,
521 * as well as when selecting an item from the GtkComboBoxEntry's list.
525 combo_box_signals[CHANGED] =
526 g_signal_new (I_("changed"),
527 G_OBJECT_CLASS_TYPE (klass),
529 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
531 g_cclosure_marshal_VOID__VOID,
534 * GtkComboBox::move-active:
535 * @widget: the object that received the signal
536 * @scroll_type: a #GtkScrollType
538 * The ::move-active signal is a
539 * <link linkend="keybinding-signals">keybinding signal</link>
540 * which gets emitted to move the active selection.
544 combo_box_signals[MOVE_ACTIVE] =
545 g_signal_new_class_handler (I_("move-active"),
546 G_OBJECT_CLASS_TYPE (klass),
547 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
548 G_CALLBACK (gtk_combo_box_real_move_active),
550 g_cclosure_marshal_VOID__ENUM,
552 GTK_TYPE_SCROLL_TYPE);
555 * GtkComboBox::popup:
556 * @widget: the object that received the signal
558 * The ::popup signal is a
559 * <link linkend="keybinding-signals">keybinding signal</link>
560 * which gets emitted to popup the combo box list.
562 * The default binding for this signal is Alt+Down.
566 combo_box_signals[POPUP] =
567 g_signal_new_class_handler (I_("popup"),
568 G_OBJECT_CLASS_TYPE (klass),
569 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
570 G_CALLBACK (gtk_combo_box_real_popup),
572 g_cclosure_marshal_VOID__VOID,
575 * GtkComboBox::popdown:
576 * @button: the object which received the signal
578 * The ::popdown signal is a
579 * <link linkend="keybinding-signals">keybinding signal</link>
580 * which gets emitted to popdown the combo box list.
582 * The default bindings for this signal are Alt+Up and Escape.
586 combo_box_signals[POPDOWN] =
587 g_signal_new_class_handler (I_("popdown"),
588 G_OBJECT_CLASS_TYPE (klass),
589 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
590 G_CALLBACK (gtk_combo_box_real_popdown),
592 _gtk_marshal_BOOLEAN__VOID,
596 binding_set = gtk_binding_set_by_class (widget_class);
598 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
600 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
603 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
607 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
610 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
612 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
613 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
615 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
616 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
618 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
619 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
621 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
622 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
624 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
625 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
627 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
629 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
631 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
632 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
634 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
635 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
637 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
638 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
640 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
641 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
643 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
644 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
646 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
652 * The model from which the combo box takes the values shown
657 g_object_class_install_property (object_class,
659 g_param_spec_object ("model",
660 P_("ComboBox model"),
661 P_("The model for the combo box"),
663 GTK_PARAM_READWRITE));
666 * GtkComboBox:wrap-width:
668 * If wrap-width is set to a positive value, the list will be
669 * displayed in multiple columns, the number of columns is
670 * determined by wrap-width.
674 g_object_class_install_property (object_class,
676 g_param_spec_int ("wrap-width",
678 P_("Wrap width for laying out the items in a grid"),
682 GTK_PARAM_READWRITE));
686 * GtkComboBox:row-span-column:
688 * If this is set to a non-negative value, it must be the index of a column
689 * of type %G_TYPE_INT in the model.
691 * The values of that column are used to determine how many rows a value in
692 * the list will span. Therefore, the values in the model column pointed to
693 * by this property must be greater than zero and not larger than wrap-width.
697 g_object_class_install_property (object_class,
698 PROP_ROW_SPAN_COLUMN,
699 g_param_spec_int ("row-span-column",
700 P_("Row span column"),
701 P_("TreeModel column containing the row span values"),
705 GTK_PARAM_READWRITE));
709 * GtkComboBox:column-span-column:
711 * If this is set to a non-negative value, it must be the index of a column
712 * of type %G_TYPE_INT in the model.
714 * The values of that column are used to determine how many columns a value
715 * in the list will span.
719 g_object_class_install_property (object_class,
720 PROP_COLUMN_SPAN_COLUMN,
721 g_param_spec_int ("column-span-column",
722 P_("Column span column"),
723 P_("TreeModel column containing the column span values"),
727 GTK_PARAM_READWRITE));
731 * GtkComboBox:active:
733 * The item which is currently active. If the model is a non-flat treemodel,
734 * and the active item is not an immediate child of the root of the tree,
735 * this property has the value
736 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
737 * where <literal>path</literal> is the #GtkTreePath of the active item.
741 g_object_class_install_property (object_class,
743 g_param_spec_int ("active",
745 P_("The item which is currently active"),
749 GTK_PARAM_READWRITE));
752 * GtkComboBox:add-tearoffs:
754 * The add-tearoffs property controls whether generated menus
755 * have tearoff menu items.
757 * Note that this only affects menu style combo boxes.
761 g_object_class_install_property (object_class,
763 g_param_spec_boolean ("add-tearoffs",
764 P_("Add tearoffs to menus"),
765 P_("Whether dropdowns should have a tearoff menu item"),
767 GTK_PARAM_READWRITE));
770 * GtkComboBox:has-frame:
772 * The has-frame property controls whether a frame
773 * is drawn around the entry.
777 g_object_class_install_property (object_class,
779 g_param_spec_boolean ("has-frame",
781 P_("Whether the combo box draws a frame around the child"),
783 GTK_PARAM_READWRITE));
785 g_object_class_install_property (object_class,
787 g_param_spec_boolean ("focus-on-click",
788 P_("Focus on click"),
789 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
791 GTK_PARAM_READWRITE));
794 * GtkComboBox:tearoff-title:
796 * A title that may be displayed by the window manager
797 * when the popup is torn-off.
801 g_object_class_install_property (object_class,
803 g_param_spec_string ("tearoff-title",
805 P_("A title that may be displayed by the window manager when the popup is torn-off"),
807 GTK_PARAM_READWRITE));
811 * GtkComboBox:popup-shown:
813 * Whether the combo boxes dropdown is popped up.
814 * Note that this property is mainly useful, because
815 * it allows you to connect to notify::popup-shown.
819 g_object_class_install_property (object_class,
821 g_param_spec_boolean ("popup-shown",
823 P_("Whether the combo's dropdown is shown"),
825 GTK_PARAM_READABLE));
829 * GtkComboBox:button-sensitivity:
831 * Whether the dropdown button is sensitive when
832 * the model is empty.
836 g_object_class_install_property (object_class,
837 PROP_BUTTON_SENSITIVITY,
838 g_param_spec_enum ("button-sensitivity",
839 P_("Button Sensitivity"),
840 P_("Whether the dropdown button is sensitive when the model is empty"),
841 GTK_TYPE_SENSITIVITY_TYPE,
842 GTK_SENSITIVITY_AUTO,
843 GTK_PARAM_READWRITE));
845 gtk_widget_class_install_style_property (widget_class,
846 g_param_spec_boolean ("appears-as-list",
847 P_("Appears as list"),
848 P_("Whether dropdowns should look like lists rather than menus"),
850 GTK_PARAM_READABLE));
853 * GtkComboBox:arrow-size:
855 * Sets the minimum size of the arrow in the combo box. Note
856 * that the arrow size is coupled to the font size, so in case
857 * a larger font is used, the arrow will be larger than set
862 gtk_widget_class_install_style_property (widget_class,
863 g_param_spec_int ("arrow-size",
865 P_("The minimum size of the arrow in the combo box"),
869 GTK_PARAM_READABLE));
872 * GtkComboBox:shadow-type:
874 * Which kind of shadow to draw around the combo box.
878 gtk_widget_class_install_style_property (widget_class,
879 g_param_spec_enum ("shadow-type",
881 P_("Which kind of shadow to draw around the combo box"),
882 GTK_TYPE_SHADOW_TYPE,
884 GTK_PARAM_READABLE));
886 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
890 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
892 parent_buildable_iface = g_type_interface_peek_parent (iface);
893 iface->add_child = _gtk_cell_layout_buildable_add_child;
894 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
895 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
899 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
901 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
902 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
903 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
904 iface->clear = gtk_combo_box_cell_layout_clear;
905 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
906 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
907 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
908 iface->reorder = gtk_combo_box_cell_layout_reorder;
912 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
914 iface->start_editing = gtk_combo_box_start_editing;
918 gtk_combo_box_init (GtkComboBox *combo_box)
920 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
922 priv->cell_view = gtk_cell_view_new ();
923 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
924 GTK_BIN (combo_box)->child = priv->cell_view;
925 gtk_widget_show (priv->cell_view);
929 priv->wrap_width = 0;
931 priv->active_row = NULL;
932 priv->col_column = -1;
933 priv->row_column = -1;
935 priv->popup_shown = FALSE;
936 priv->add_tearoffs = FALSE;
937 priv->has_frame = TRUE;
938 priv->is_cell_renderer = FALSE;
939 priv->editing_canceled = FALSE;
940 priv->auto_scroll = FALSE;
941 priv->focus_on_click = TRUE;
942 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
944 combo_box->priv = priv;
946 gtk_combo_box_check_appearance (combo_box);
950 gtk_combo_box_set_property (GObject *object,
955 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
960 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
963 case PROP_WRAP_WIDTH:
964 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
967 case PROP_ROW_SPAN_COLUMN:
968 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
971 case PROP_COLUMN_SPAN_COLUMN:
972 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
976 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
979 case PROP_ADD_TEAROFFS:
980 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
984 combo_box->priv->has_frame = g_value_get_boolean (value);
987 case PROP_FOCUS_ON_CLICK:
988 gtk_combo_box_set_focus_on_click (combo_box,
989 g_value_get_boolean (value));
992 case PROP_TEAROFF_TITLE:
993 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
996 case PROP_POPUP_SHOWN:
997 if (g_value_get_boolean (value))
998 gtk_combo_box_popup (combo_box);
1000 gtk_combo_box_popdown (combo_box);
1003 case PROP_BUTTON_SENSITIVITY:
1004 gtk_combo_box_set_button_sensitivity (combo_box,
1005 g_value_get_enum (value));
1009 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1015 gtk_combo_box_get_property (GObject *object,
1020 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1025 g_value_set_object (value, combo_box->priv->model);
1028 case PROP_WRAP_WIDTH:
1029 g_value_set_int (value, combo_box->priv->wrap_width);
1032 case PROP_ROW_SPAN_COLUMN:
1033 g_value_set_int (value, combo_box->priv->row_column);
1036 case PROP_COLUMN_SPAN_COLUMN:
1037 g_value_set_int (value, combo_box->priv->col_column);
1041 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1044 case PROP_ADD_TEAROFFS:
1045 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1048 case PROP_HAS_FRAME:
1049 g_value_set_boolean (value, combo_box->priv->has_frame);
1052 case PROP_FOCUS_ON_CLICK:
1053 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1056 case PROP_TEAROFF_TITLE:
1057 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1060 case PROP_POPUP_SHOWN:
1061 g_value_set_boolean (value, combo_box->priv->popup_shown);
1064 case PROP_BUTTON_SENSITIVITY:
1065 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1075 gtk_combo_box_state_changed (GtkWidget *widget,
1076 GtkStateType previous)
1078 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1079 GtkComboBoxPrivate *priv = combo_box->priv;
1081 if (GTK_WIDGET_REALIZED (widget))
1083 if (priv->tree_view && priv->cell_view)
1084 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1085 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1088 gtk_widget_queue_draw (widget);
1092 gtk_combo_box_button_state_changed (GtkWidget *widget,
1093 GtkStateType previous,
1096 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1097 GtkComboBoxPrivate *priv = combo_box->priv;
1099 if (GTK_WIDGET_REALIZED (widget))
1101 if (!priv->tree_view && priv->cell_view)
1103 if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
1104 (GTK_WIDGET_STATE (priv->cell_view) == GTK_STATE_INSENSITIVE))
1105 gtk_widget_set_sensitive (priv->cell_view, GTK_WIDGET_SENSITIVE (widget));
1107 gtk_widget_set_state (priv->cell_view,
1108 GTK_WIDGET_STATE (widget));
1112 gtk_widget_queue_draw (widget);
1116 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1118 GtkComboBoxPrivate *priv = combo_box->priv;
1119 gboolean appears_as_list;
1121 /* if wrap_width > 0, then we are in grid-mode and forced to use
1124 if (priv->wrap_width)
1125 appears_as_list = FALSE;
1127 gtk_widget_style_get (GTK_WIDGET (combo_box),
1128 "appears-as-list", &appears_as_list,
1131 if (appears_as_list)
1133 /* Destroy all the menu mode widgets, if they exist. */
1134 if (GTK_IS_MENU (priv->popup_widget))
1135 gtk_combo_box_menu_destroy (combo_box);
1137 /* Create the list mode widgets, if they don't already exist. */
1138 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1139 gtk_combo_box_list_setup (combo_box);
1143 /* Destroy all the list mode widgets, if they exist. */
1144 if (GTK_IS_TREE_VIEW (priv->tree_view))
1145 gtk_combo_box_list_destroy (combo_box);
1147 /* Create the menu mode widgets, if they don't already exist. */
1148 if (!GTK_IS_MENU (priv->popup_widget))
1149 gtk_combo_box_menu_setup (combo_box, TRUE);
1152 gtk_widget_style_get (GTK_WIDGET (combo_box),
1153 "shadow-type", &priv->shadow_type,
1158 gtk_combo_box_style_set (GtkWidget *widget,
1161 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1162 GtkComboBoxPrivate *priv = combo_box->priv;
1164 gtk_combo_box_check_appearance (combo_box);
1166 if (priv->tree_view && priv->cell_view)
1167 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1168 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1170 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1171 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1172 GTK_SHADOW_NONE == priv->shadow_type ?
1173 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1177 gtk_combo_box_button_toggled (GtkWidget *widget,
1180 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1182 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1184 if (!combo_box->priv->popup_in_progress)
1185 gtk_combo_box_popup (combo_box);
1188 gtk_combo_box_popdown (combo_box);
1192 gtk_combo_box_add (GtkContainer *container,
1195 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1196 GtkComboBoxPrivate *priv = combo_box->priv;
1198 if (priv->cell_view && priv->cell_view->parent)
1200 gtk_widget_unparent (priv->cell_view);
1201 GTK_BIN (container)->child = NULL;
1202 gtk_widget_queue_resize (GTK_WIDGET (container));
1205 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1206 GTK_BIN (container)->child = widget;
1208 if (priv->cell_view &&
1209 widget != priv->cell_view)
1211 /* since the cell_view was unparented, it's gone now */
1212 priv->cell_view = NULL;
1214 if (!priv->tree_view && priv->separator)
1216 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1218 priv->separator = NULL;
1220 gtk_widget_queue_resize (GTK_WIDGET (container));
1222 else if (priv->cell_view_frame)
1224 gtk_widget_unparent (priv->cell_view_frame);
1225 priv->cell_view_frame = NULL;
1232 gtk_combo_box_remove (GtkContainer *container,
1235 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1236 GtkComboBoxPrivate *priv = combo_box->priv;
1238 gboolean appears_as_list;
1240 if (widget == priv->cell_view)
1241 priv->cell_view = NULL;
1243 gtk_widget_unparent (widget);
1244 GTK_BIN (container)->child = NULL;
1246 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1249 gtk_widget_queue_resize (GTK_WIDGET (container));
1251 if (!priv->tree_view)
1252 appears_as_list = FALSE;
1254 appears_as_list = TRUE;
1256 if (appears_as_list)
1257 gtk_combo_box_list_destroy (combo_box);
1258 else if (GTK_IS_MENU (priv->popup_widget))
1260 gtk_combo_box_menu_destroy (combo_box);
1261 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1262 priv->popup_widget = NULL;
1265 if (!priv->cell_view)
1267 priv->cell_view = gtk_cell_view_new ();
1268 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1269 GTK_BIN (container)->child = priv->cell_view;
1271 gtk_widget_show (priv->cell_view);
1272 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1274 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1278 if (appears_as_list)
1279 gtk_combo_box_list_setup (combo_box);
1281 gtk_combo_box_menu_setup (combo_box, TRUE);
1283 if (gtk_tree_row_reference_valid (priv->active_row))
1285 path = gtk_tree_row_reference_get_path (priv->active_row);
1286 gtk_combo_box_set_active_internal (combo_box, path);
1287 gtk_tree_path_free (path);
1290 gtk_combo_box_set_active_internal (combo_box, NULL);
1293 static ComboCellInfo *
1294 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1295 GtkCellRenderer *cell)
1299 for (i = combo_box->priv->cells; i; i = i->next)
1301 ComboCellInfo *info = (ComboCellInfo *)i->data;
1303 if (info && info->cell == cell)
1311 gtk_combo_box_menu_show (GtkWidget *menu,
1314 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1315 GtkComboBoxPrivate *priv = combo_box->priv;
1317 gtk_combo_box_child_show (menu, user_data);
1319 priv->popup_in_progress = TRUE;
1320 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1322 priv->popup_in_progress = FALSE;
1326 gtk_combo_box_menu_hide (GtkWidget *menu,
1329 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1331 gtk_combo_box_child_hide (menu,user_data);
1333 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1338 gtk_combo_box_detacher (GtkWidget *widget,
1341 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1342 GtkComboBoxPrivate *priv = combo_box->priv;
1344 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1346 g_signal_handlers_disconnect_by_func (menu->toplevel,
1347 gtk_combo_box_menu_show,
1349 g_signal_handlers_disconnect_by_func (menu->toplevel,
1350 gtk_combo_box_menu_hide,
1353 priv->popup_widget = NULL;
1357 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1360 GtkComboBoxPrivate *priv = combo_box->priv;
1362 if (GTK_IS_MENU (priv->popup_widget))
1364 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1365 priv->popup_widget = NULL;
1367 else if (priv->popup_widget)
1369 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1370 priv->popup_widget);
1371 g_object_unref (priv->popup_widget);
1372 priv->popup_widget = NULL;
1375 if (GTK_IS_MENU (popup))
1377 if (priv->popup_window)
1379 gtk_widget_destroy (priv->popup_window);
1380 priv->popup_window = NULL;
1383 priv->popup_widget = popup;
1386 * Note that we connect to show/hide on the toplevel, not the
1387 * menu itself, since the menu is not shown/hidden when it is
1388 * popped up while torn-off.
1390 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1391 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1392 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1393 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1395 gtk_menu_attach_to_widget (GTK_MENU (popup),
1396 GTK_WIDGET (combo_box),
1397 gtk_combo_box_detacher);
1401 if (!priv->popup_window)
1403 GtkWidget *toplevel;
1405 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1406 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1408 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1409 GDK_WINDOW_TYPE_HINT_COMBO);
1411 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1412 G_CALLBACK (gtk_combo_box_child_show),
1414 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1415 G_CALLBACK (gtk_combo_box_child_hide),
1418 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1419 if (GTK_IS_WINDOW (toplevel))
1421 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1422 GTK_WINDOW (priv->popup_window));
1423 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1424 GTK_WINDOW (toplevel));
1427 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1428 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1429 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1431 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1433 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1436 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1439 gtk_widget_show (priv->scrolled_window);
1441 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1442 priv->scrolled_window);
1445 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1448 gtk_widget_show (popup);
1449 g_object_ref (popup);
1450 priv->popup_widget = popup;
1455 gtk_combo_box_menu_position_below (GtkMenu *menu,
1461 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1467 GdkRectangle monitor;
1469 /* FIXME: is using the size request here broken? */
1470 child = GTK_BIN (combo_box)->child;
1472 gdk_window_get_origin (child->window, &sx, &sy);
1474 if (GTK_WIDGET_NO_WINDOW (child))
1476 sx += child->allocation.x;
1477 sy += child->allocation.y;
1480 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1481 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1483 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1485 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1488 *x = sx + child->allocation.width - req.width;
1491 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1492 monitor_num = gdk_screen_get_monitor_at_window (screen,
1493 GTK_WIDGET (combo_box)->window);
1494 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1498 else if (*x + req.width > monitor.x + monitor.width)
1499 *x = monitor.x + monitor.width - req.width;
1501 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1502 *y += child->allocation.height;
1503 else if (*y - monitor.y >= req.height)
1505 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1506 *y += child->allocation.height;
1514 gtk_combo_box_menu_position_over (GtkMenu *menu,
1520 GtkComboBox *combo_box;
1524 GtkRequisition requisition;
1531 combo_box = GTK_COMBO_BOX (user_data);
1532 widget = GTK_WIDGET (combo_box);
1534 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1535 menu_width = requisition.width;
1537 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1538 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1540 menu_xpos += widget->allocation.x;
1541 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1545 gtk_widget_get_child_requisition (active, &requisition);
1546 menu_ypos -= requisition.height / 2;
1549 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1552 child = children->data;
1554 if (active == child)
1557 if (GTK_WIDGET_VISIBLE (child))
1559 gtk_widget_get_child_requisition (child, &requisition);
1560 menu_ypos -= requisition.height;
1563 children = children->next;
1566 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1567 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1569 /* Clamp the position on screen */
1570 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1574 else if ((menu_xpos + menu_width) > screen_width)
1575 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1584 gtk_combo_box_menu_position (GtkMenu *menu,
1590 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1591 GtkComboBoxPrivate *priv = combo_box->priv;
1592 GtkWidget *menu_item;
1595 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1596 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1599 /* FIXME handle nested menus better */
1600 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1602 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1605 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1608 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1609 GDK_WINDOW_TYPE_HINT_COMBO);
1613 gtk_combo_box_list_position (GtkComboBox *combo_box,
1619 GtkComboBoxPrivate *priv = combo_box->priv;
1622 GdkRectangle monitor;
1623 GtkRequisition popup_req;
1624 GtkPolicyType hpolicy, vpolicy;
1626 /* under windows, the drop down list is as wide as the combo box itself.
1628 GtkWidget *sample = GTK_WIDGET (combo_box);
1630 gdk_window_get_origin (sample->window, x, y);
1632 if (GTK_WIDGET_NO_WINDOW (sample))
1634 *x += sample->allocation.x;
1635 *y += sample->allocation.y;
1638 *width = sample->allocation.width;
1640 hpolicy = vpolicy = GTK_POLICY_NEVER;
1641 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1643 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1645 if (popup_req.width > *width)
1647 hpolicy = GTK_POLICY_ALWAYS;
1648 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1650 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1653 *height = popup_req.height;
1655 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1656 monitor_num = gdk_screen_get_monitor_at_window (screen,
1657 GTK_WIDGET (combo_box)->window);
1658 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1662 else if (*x + *width > monitor.x + monitor.width)
1663 *x = monitor.x + monitor.width - *width;
1665 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1666 *y += sample->allocation.height;
1667 else if (*y - *height >= monitor.y)
1669 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1671 *y += sample->allocation.height;
1672 *height = monitor.y + monitor.height - *y;
1676 *height = *y - monitor.y;
1680 if (popup_req.height > *height)
1682 vpolicy = GTK_POLICY_ALWAYS;
1684 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1690 cell_view_is_sensitive (GtkCellView *cell_view)
1692 GList *cells, *list;
1695 cells = gtk_cell_view_get_cell_renderers (cell_view);
1698 for (list = cells; list; list = list->next)
1700 g_object_get (list->data, "sensitive", &sensitive, NULL);
1705 g_list_free (cells);
1711 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1714 GtkComboBoxPrivate *priv = combo_box->priv;
1715 GList *cells, *list;
1721 if (priv->row_separator_func)
1723 if (priv->row_separator_func (priv->model, iter,
1724 priv->row_separator_data))
1728 gtk_tree_view_column_cell_set_cell_data (priv->column,
1730 iter, FALSE, FALSE);
1732 cells = gtk_tree_view_column_get_cell_renderers (priv->column);
1735 for (list = cells; list; list = list->next)
1737 g_object_get (list->data, "sensitive", &sensitive, NULL);
1742 g_list_free (cells);
1748 update_menu_sensitivity (GtkComboBox *combo_box,
1751 GtkComboBoxPrivate *priv = combo_box->priv;
1752 GList *children, *child;
1753 GtkWidget *item, *submenu, *separator;
1754 GtkWidget *cell_view;
1760 children = gtk_container_get_children (GTK_CONTAINER (menu));
1762 for (child = children; child; child = child->next)
1764 item = GTK_WIDGET (child->data);
1765 cell_view = GTK_BIN (item)->child;
1767 if (!GTK_IS_CELL_VIEW (cell_view))
1770 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1771 if (submenu != NULL)
1773 gtk_widget_set_sensitive (item, TRUE);
1774 update_menu_sensitivity (combo_box, submenu);
1778 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1780 if (menu != priv->popup_widget && child == children)
1782 separator = GTK_WIDGET (child->next->data);
1783 g_object_set (item, "visible", sensitive, NULL);
1784 g_object_set (separator, "visible", sensitive, NULL);
1787 gtk_widget_set_sensitive (item, sensitive);
1791 g_list_free (children);
1795 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1797 guint32 activate_time)
1799 GtkComboBoxPrivate *priv = combo_box->priv;
1802 GtkRequisition requisition;
1805 update_menu_sensitivity (combo_box, priv->popup_widget);
1808 if (gtk_tree_row_reference_valid (priv->active_row))
1810 path = gtk_tree_row_reference_get_path (priv->active_row);
1811 active_item = gtk_tree_path_get_indices (path)[0];
1812 gtk_tree_path_free (path);
1814 if (priv->add_tearoffs)
1818 /* FIXME handle nested menus better */
1819 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1821 if (priv->wrap_width == 0)
1823 width = GTK_WIDGET (combo_box)->allocation.width;
1824 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1825 gtk_widget_size_request (priv->popup_widget, &requisition);
1827 gtk_widget_set_size_request (priv->popup_widget,
1828 MAX (width, requisition.width), -1);
1831 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1833 gtk_combo_box_menu_position, combo_box,
1834 button, activate_time);
1838 popup_grab_on_window (GdkWindow *window,
1839 guint32 activate_time,
1840 gboolean grab_keyboard)
1842 if ((gdk_pointer_grab (window, TRUE,
1843 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1844 GDK_POINTER_MOTION_MASK,
1845 NULL, NULL, activate_time) == 0))
1847 if (!grab_keyboard ||
1848 gdk_keyboard_grab (window, TRUE,
1849 activate_time) == 0)
1853 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1863 * gtk_combo_box_popup:
1864 * @combo_box: a #GtkComboBox
1866 * Pops up the menu or dropdown list of @combo_box.
1868 * This function is mostly intended for use by accessibility technologies;
1869 * applications should have little use for it.
1874 gtk_combo_box_popup (GtkComboBox *combo_box)
1876 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1878 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1882 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1884 GtkComboBoxPrivate *priv = combo_box->priv;
1885 gint x, y, width, height;
1886 GtkTreePath *path = NULL, *ppath;
1887 GtkWidget *toplevel;
1889 if (!GTK_WIDGET_REALIZED (combo_box))
1892 if (GTK_WIDGET_MAPPED (priv->popup_widget))
1895 if (GTK_IS_MENU (priv->popup_widget))
1897 gtk_combo_box_menu_popup (combo_box,
1898 priv->activate_button,
1899 priv->activate_time);
1903 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1904 if (GTK_IS_WINDOW (toplevel))
1905 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1906 GTK_WINDOW (priv->popup_window));
1908 gtk_widget_show_all (priv->scrolled_window);
1909 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1911 gtk_widget_set_size_request (priv->popup_window, width, height);
1912 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1914 if (gtk_tree_row_reference_valid (priv->active_row))
1916 path = gtk_tree_row_reference_get_path (priv->active_row);
1917 ppath = gtk_tree_path_copy (path);
1918 if (gtk_tree_path_up (ppath))
1919 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1921 gtk_tree_path_free (ppath);
1923 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1927 gtk_widget_show (priv->popup_window);
1931 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1933 gtk_tree_path_free (path);
1936 gtk_widget_grab_focus (priv->popup_window);
1937 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1940 if (!GTK_WIDGET_HAS_FOCUS (priv->tree_view))
1941 gtk_widget_grab_focus (priv->tree_view);
1943 if (!popup_grab_on_window (priv->popup_window->window,
1944 GDK_CURRENT_TIME, TRUE))
1946 gtk_widget_hide (priv->popup_window);
1950 gtk_grab_add (priv->popup_window);
1954 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1956 if (combo_box->priv->popup_shown)
1958 gtk_combo_box_popdown (combo_box);
1966 * gtk_combo_box_popdown:
1967 * @combo_box: a #GtkComboBox
1969 * Hides the menu or dropdown list of @combo_box.
1971 * This function is mostly intended for use by accessibility technologies;
1972 * applications should have little use for it.
1977 gtk_combo_box_popdown (GtkComboBox *combo_box)
1979 GtkComboBoxPrivate *priv = combo_box->priv;
1981 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1983 if (GTK_IS_MENU (priv->popup_widget))
1985 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
1989 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1992 gtk_grab_remove (priv->popup_window);
1993 gtk_widget_hide_all (priv->popup_window);
1994 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1999 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2002 GtkComboBoxPrivate *priv = combo_box->priv;
2006 if (priv->cell_view)
2007 gtk_widget_style_get (priv->cell_view,
2008 "focus-line-width", &padding,
2013 /* add some pixels for good measure */
2014 padding += BONUS_PADDING;
2016 if (priv->cell_view)
2017 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2022 return req.width + padding;
2026 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2028 GtkComboBoxPrivate *priv = combo_box->priv;
2033 !gtk_tree_model_get_iter_first (priv->model, &iter))
2039 path = gtk_tree_path_new_from_indices (0, -1);
2045 if (priv->cell_view)
2046 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2054 priv->width = MAX (priv->width, req.width);
2055 priv->height = MAX (priv->height, req.height);
2057 gtk_tree_path_next (path);
2059 while (gtk_tree_model_iter_next (priv->model, &iter));
2061 gtk_tree_path_free (path);
2065 gtk_combo_box_size_request (GtkWidget *widget,
2066 GtkRequisition *requisition)
2069 gint focus_width, focus_pad;
2072 GtkRequisition bin_req;
2073 PangoContext *context;
2074 PangoFontMetrics *metrics;
2075 PangoFontDescription *font_desc;
2077 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2078 GtkComboBoxPrivate *priv = combo_box->priv;
2081 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2082 gtk_combo_box_remeasure (combo_box);
2083 bin_req.width = MAX (bin_req.width, priv->width);
2084 bin_req.height = MAX (bin_req.height, priv->height);
2086 gtk_widget_style_get (GTK_WIDGET (widget),
2087 "focus-line-width", &focus_width,
2088 "focus-padding", &focus_pad,
2089 "arrow-size", &arrow_size,
2092 font_desc = GTK_BIN (widget)->child->style->font_desc;
2093 context = gtk_widget_get_pango_context (widget);
2094 metrics = pango_context_get_metrics (context, font_desc,
2095 pango_context_get_language (context));
2096 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2097 pango_font_metrics_get_descent (metrics));
2098 pango_font_metrics_unref (metrics);
2100 arrow_size = MAX (arrow_size, font_size);
2102 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2104 if (!priv->tree_view)
2108 if (priv->cell_view)
2110 GtkRequisition button_req, sep_req, arrow_req;
2111 gint border_width, xthickness, ythickness;
2113 gtk_widget_size_request (priv->button, &button_req);
2114 border_width = GTK_CONTAINER (combo_box)->border_width;
2115 xthickness = priv->button->style->xthickness;
2116 ythickness = priv->button->style->ythickness;
2118 bin_req.width = MAX (bin_req.width, priv->width);
2119 bin_req.height = MAX (bin_req.height, priv->height);
2121 gtk_widget_size_request (priv->separator, &sep_req);
2122 gtk_widget_size_request (priv->arrow, &arrow_req);
2124 height = MAX (sep_req.height, arrow_req.height);
2125 height = MAX (height, bin_req.height);
2127 width = bin_req.width + sep_req.width + arrow_req.width;
2129 height += 2*(border_width + ythickness + focus_width + focus_pad);
2130 width += 2*(border_width + xthickness + focus_width + focus_pad);
2132 requisition->width = width;
2133 requisition->height = height;
2137 GtkRequisition but_req;
2139 gtk_widget_size_request (priv->button, &but_req);
2141 requisition->width = bin_req.width + but_req.width;
2142 requisition->height = MAX (bin_req.height, but_req.height);
2148 GtkRequisition button_req, frame_req;
2150 /* sample + frame */
2151 *requisition = bin_req;
2153 requisition->width += 2 * focus_width;
2155 if (priv->cell_view_frame)
2157 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2158 if (priv->has_frame)
2160 requisition->width += 2 *
2161 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2162 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2163 requisition->height += 2 *
2164 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2165 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2170 gtk_widget_size_request (priv->button, &button_req);
2172 requisition->height = MAX (requisition->height, button_req.height);
2173 requisition->width += button_req.width;
2176 if (GTK_SHADOW_NONE != priv->shadow_type)
2178 requisition->height += 2 * widget->style->ythickness;
2179 requisition->width += 2 * widget->style->xthickness;
2183 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2184 gtk_widget_size_request (combo_box->priv->button, &req); \
2187 child.x = allocation->x + shadow_width; \
2189 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2191 child.y = allocation->y + shadow_height; \
2192 child.width = req.width; \
2193 child.height = allocation->height - 2 * shadow_height; \
2194 child.width = MAX (1, child.width); \
2195 child.height = MAX (1, child.height); \
2197 gtk_widget_size_allocate (combo_box->priv->button, &child);
2200 gtk_combo_box_size_allocate (GtkWidget *widget,
2201 GtkAllocation *allocation)
2203 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2204 GtkComboBoxPrivate *priv = combo_box->priv;
2205 gint shadow_width, shadow_height;
2206 gint focus_width, focus_pad;
2207 GtkAllocation child;
2209 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2211 widget->allocation = *allocation;
2213 gtk_widget_style_get (GTK_WIDGET (widget),
2214 "focus-line-width", &focus_width,
2215 "focus-padding", &focus_pad,
2218 if (GTK_SHADOW_NONE != priv->shadow_type)
2220 shadow_width = widget->style->xthickness;
2221 shadow_height = widget->style->ythickness;
2229 if (!priv->tree_view)
2231 if (priv->cell_view)
2233 gint border_width, xthickness, ythickness;
2237 allocation->x += shadow_width;
2238 allocation->y += shadow_height;
2239 allocation->width -= 2 * shadow_width;
2240 allocation->height -= 2 * shadow_height;
2242 gtk_widget_size_allocate (priv->button, allocation);
2244 /* set some things ready */
2245 border_width = GTK_CONTAINER (priv->button)->border_width;
2246 xthickness = priv->button->style->xthickness;
2247 ythickness = priv->button->style->ythickness;
2249 child.x = allocation->x;
2250 child.y = allocation->y;
2251 width = allocation->width;
2252 child.height = allocation->height;
2254 if (!priv->is_cell_renderer)
2256 child.x += border_width + xthickness + focus_width + focus_pad;
2257 child.y += border_width + ythickness + focus_width + focus_pad;
2258 width -= 2 * (child.x - allocation->x);
2259 child.height -= 2 * (child.y - allocation->y);
2263 /* handle the children */
2264 gtk_widget_size_request (priv->arrow, &req);
2265 child.width = req.width;
2267 child.x += width - req.width;
2268 child.width = MAX (1, child.width);
2269 child.height = MAX (1, child.height);
2270 gtk_widget_size_allocate (priv->arrow, &child);
2272 child.x += req.width;
2273 gtk_widget_size_request (priv->separator, &req);
2274 child.width = req.width;
2276 child.x -= req.width;
2277 child.width = MAX (1, child.width);
2278 child.height = MAX (1, child.height);
2279 gtk_widget_size_allocate (priv->separator, &child);
2283 child.x += req.width;
2284 child.width = allocation->x + allocation->width
2285 - (border_width + xthickness + focus_width + focus_pad)
2290 child.width = child.x;
2291 child.x = allocation->x
2292 + border_width + xthickness + focus_width + focus_pad;
2293 child.width -= child.x;
2296 child.width = MAX (1, child.width);
2297 child.height = MAX (1, child.height);
2298 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2302 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2305 child.x = allocation->x + req.width + shadow_width;
2307 child.x = allocation->x + shadow_width;
2308 child.y = allocation->y + shadow_height;
2309 child.width = allocation->width - req.width - 2 * shadow_width;
2310 child.width = MAX (1, child.width);
2311 child.height = MAX (1, child.height);
2312 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2319 /* Combobox thickness + border-width */
2320 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2321 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2324 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2328 child.x = allocation->x + req.width;
2330 child.x = allocation->x;
2332 child.y = allocation->y;
2333 child.width = allocation->width - req.width;
2334 child.height = allocation->height;
2336 if (priv->cell_view_frame)
2340 child.width = MAX (1, child.width - delta_x * 2);
2341 child.height = MAX (1, child.height - delta_y * 2);
2342 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2345 if (priv->has_frame)
2347 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2348 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2349 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2350 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2354 child.width -= delta_x * 2;
2355 child.height -= delta_y * 2;
2362 child.width -= delta_x * 2;
2363 child.height -= delta_y * 2;
2366 child.width = MAX (1, child.width);
2367 child.height = MAX (1, child.height);
2369 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2373 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2376 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2378 GtkComboBoxPrivate *priv = combo_box->priv;
2382 g_signal_handler_disconnect (priv->model,
2384 g_signal_handler_disconnect (priv->model,
2386 g_signal_handler_disconnect (priv->model,
2387 priv->reordered_id);
2388 g_signal_handler_disconnect (priv->model,
2393 if (!priv->tree_view)
2395 if (priv->popup_widget)
2396 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2397 (GtkCallback)gtk_widget_destroy, NULL);
2402 g_object_unref (priv->model);
2406 if (priv->active_row)
2408 gtk_tree_row_reference_free (priv->active_row);
2409 priv->active_row = NULL;
2412 if (priv->cell_view)
2413 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2417 gtk_combo_box_forall (GtkContainer *container,
2418 gboolean include_internals,
2419 GtkCallback callback,
2420 gpointer callback_data)
2422 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2423 GtkComboBoxPrivate *priv = combo_box->priv;
2425 if (include_internals)
2428 (* callback) (priv->button, callback_data);
2429 if (priv->cell_view_frame)
2430 (* callback) (priv->cell_view_frame, callback_data);
2433 if (GTK_BIN (container)->child)
2434 (* callback) (GTK_BIN (container)->child, callback_data);
2438 gtk_combo_box_child_show (GtkWidget *widget,
2439 GtkComboBox *combo_box)
2441 GtkComboBoxPrivate *priv = combo_box->priv;
2443 priv->popup_shown = TRUE;
2444 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2448 gtk_combo_box_child_hide (GtkWidget *widget,
2449 GtkComboBox *combo_box)
2451 GtkComboBoxPrivate *priv = combo_box->priv;
2453 priv->popup_shown = FALSE;
2454 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2458 gtk_combo_box_expose_event (GtkWidget *widget,
2459 GdkEventExpose *event)
2461 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2462 GtkComboBoxPrivate *priv = combo_box->priv;
2464 if (GTK_WIDGET_DRAWABLE (widget) &&
2465 GTK_SHADOW_NONE != priv->shadow_type)
2467 gtk_paint_shadow (widget->style, widget->window,
2468 GTK_STATE_NORMAL, priv->shadow_type,
2469 NULL, widget, "combobox",
2470 widget->allocation.x, widget->allocation.y,
2471 widget->allocation.width, widget->allocation.height);
2474 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2475 priv->button, event);
2477 if (priv->tree_view && priv->cell_view_frame)
2479 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2480 priv->cell_view_frame, event);
2483 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2484 GTK_BIN (widget)->child, event);
2499 path_visible (GtkTreeView *view,
2505 /* Note that we rely on the fact that collapsed rows don't have nodes
2507 return _gtk_tree_view_find_node (view, path, &tree, &node);
2511 tree_next_func (GtkTreeModel *model,
2516 SearchData *search_data = (SearchData *)data;
2518 if (search_data->found)
2520 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2523 if (search_data->visible &&
2524 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2527 search_data->set = TRUE;
2528 search_data->iter = *iter;
2533 if (gtk_tree_path_compare (path, search_data->path) == 0)
2534 search_data->found = TRUE;
2540 tree_next (GtkComboBox *combo,
2541 GtkTreeModel *model,
2546 SearchData search_data;
2548 search_data.combo = combo;
2549 search_data.path = gtk_tree_model_get_path (model, iter);
2550 search_data.visible = visible;
2551 search_data.found = FALSE;
2552 search_data.set = FALSE;
2554 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2556 *next = search_data.iter;
2558 gtk_tree_path_free (search_data.path);
2560 return search_data.set;
2564 tree_prev_func (GtkTreeModel *model,
2569 SearchData *search_data = (SearchData *)data;
2571 if (gtk_tree_path_compare (path, search_data->path) == 0)
2573 search_data->found = TRUE;
2577 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2580 if (search_data->visible &&
2581 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2584 search_data->set = TRUE;
2585 search_data->iter = *iter;
2591 tree_prev (GtkComboBox *combo,
2592 GtkTreeModel *model,
2597 SearchData search_data;
2599 search_data.combo = combo;
2600 search_data.path = gtk_tree_model_get_path (model, iter);
2601 search_data.visible = visible;
2602 search_data.found = FALSE;
2603 search_data.set = FALSE;
2605 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2607 *prev = search_data.iter;
2609 gtk_tree_path_free (search_data.path);
2611 return search_data.set;
2615 tree_last_func (GtkTreeModel *model,
2620 SearchData *search_data = (SearchData *)data;
2622 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2625 /* Note that we rely on the fact that collapsed rows don't have nodes
2627 if (search_data->visible &&
2628 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2631 search_data->set = TRUE;
2632 search_data->iter = *iter;
2638 tree_last (GtkComboBox *combo,
2639 GtkTreeModel *model,
2643 SearchData search_data;
2645 search_data.combo = combo;
2646 search_data.visible = visible;
2647 search_data.set = FALSE;
2649 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2651 *last = search_data.iter;
2653 return search_data.set;
2658 tree_first_func (GtkTreeModel *model,
2663 SearchData *search_data = (SearchData *)data;
2665 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2668 if (search_data->visible &&
2669 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2672 search_data->set = TRUE;
2673 search_data->iter = *iter;
2679 tree_first (GtkComboBox *combo,
2680 GtkTreeModel *model,
2684 SearchData search_data;
2686 search_data.combo = combo;
2687 search_data.visible = visible;
2688 search_data.set = FALSE;
2690 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2692 *first = search_data.iter;
2694 return search_data.set;
2698 gtk_combo_box_scroll_event (GtkWidget *widget,
2699 GdkEventScroll *event)
2701 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2704 GtkTreeIter new_iter;
2706 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2709 if (event->direction == GDK_SCROLL_UP)
2710 found = tree_prev (combo_box, combo_box->priv->model,
2711 &iter, &new_iter, FALSE);
2713 found = tree_next (combo_box, combo_box->priv->model,
2714 &iter, &new_iter, FALSE);
2717 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2727 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2728 GtkCellLayout *cell_layout)
2730 GtkComboBoxPrivate *priv = combo_box->priv;
2733 for (k = priv->cells; k; k = k->next)
2736 ComboCellInfo *info = (ComboCellInfo *)k->data;
2738 if (info->pack == GTK_PACK_START)
2739 gtk_cell_layout_pack_start (cell_layout,
2740 info->cell, info->expand);
2741 else if (info->pack == GTK_PACK_END)
2742 gtk_cell_layout_pack_end (cell_layout,
2743 info->cell, info->expand);
2745 gtk_cell_layout_set_cell_data_func (cell_layout,
2747 combo_cell_data_func, info, NULL);
2749 for (j = info->attributes; j; j = j->next->next)
2751 gtk_cell_layout_add_attribute (cell_layout,
2754 GPOINTER_TO_INT (j->next->data));
2760 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2761 gboolean add_children)
2763 GtkComboBoxPrivate *priv = combo_box->priv;
2766 if (priv->cell_view)
2768 priv->button = gtk_toggle_button_new ();
2769 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2770 priv->focus_on_click);
2772 g_signal_connect (priv->button, "toggled",
2773 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2774 gtk_widget_set_parent (priv->button,
2775 GTK_BIN (combo_box)->child->parent);
2777 priv->box = gtk_hbox_new (FALSE, 0);
2778 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2780 priv->separator = gtk_vseparator_new ();
2781 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2783 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2784 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2786 gtk_widget_show_all (priv->button);
2790 priv->button = gtk_toggle_button_new ();
2791 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2792 priv->focus_on_click);
2794 g_signal_connect (priv->button, "toggled",
2795 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2796 gtk_widget_set_parent (priv->button,
2797 GTK_BIN (combo_box)->child->parent);
2799 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2800 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2801 gtk_widget_show_all (priv->button);
2804 g_signal_connect (priv->button, "button-press-event",
2805 G_CALLBACK (gtk_combo_box_menu_button_press),
2807 g_signal_connect (priv->button, "state-changed",
2808 G_CALLBACK (gtk_combo_box_button_state_changed),
2811 /* create our funky menu */
2812 menu = gtk_menu_new ();
2813 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2815 g_signal_connect (menu, "key-press-event",
2816 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2817 gtk_combo_box_set_popup_widget (combo_box, menu);
2821 gtk_combo_box_menu_fill (combo_box);
2823 /* the column is needed in tree_column_row_is_sensitive() */
2824 priv->column = gtk_tree_view_column_new ();
2825 g_object_ref_sink (priv->column);
2826 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2828 gtk_combo_box_update_title (combo_box);
2832 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2834 GtkComboBoxPrivate *priv = combo_box->priv;
2840 menu = priv->popup_widget;
2842 if (priv->add_tearoffs)
2844 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2846 gtk_widget_show (tearoff);
2848 if (priv->wrap_width)
2849 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2851 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2854 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2858 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2859 GtkTreeModel *model,
2862 GtkWidget *cell_view;
2867 cell_view = gtk_cell_view_new ();
2868 item = gtk_menu_item_new ();
2869 gtk_container_add (GTK_CONTAINER (item), cell_view);
2871 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2872 path = gtk_tree_model_get_path (model, iter);
2873 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2874 gtk_tree_path_free (path);
2876 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2877 gtk_widget_size_request (cell_view, &req);
2878 gtk_widget_show (cell_view);
2884 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2886 GtkTreeIter *parent)
2888 GtkComboBoxPrivate *priv = combo_box->priv;
2889 GtkTreeModel *model = priv->model;
2890 GtkWidget *item, *submenu, *subitem, *separator;
2892 gboolean is_separator;
2897 n_children = gtk_tree_model_iter_n_children (model, parent);
2900 for (i = 0; i < n_children; i++)
2902 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2904 if (priv->row_separator_func)
2905 is_separator = priv->row_separator_func (priv->model, &iter,
2906 priv->row_separator_data);
2908 is_separator = FALSE;
2912 item = gtk_separator_menu_item_new ();
2913 path = gtk_tree_model_get_path (model, &iter);
2914 g_object_set_data_full (G_OBJECT (item),
2915 I_("gtk-combo-box-item-path"),
2916 gtk_tree_row_reference_new (model, path),
2917 (GDestroyNotify)gtk_tree_row_reference_free);
2918 gtk_tree_path_free (path);
2922 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2923 if (gtk_tree_model_iter_has_child (model, &iter))
2925 submenu = gtk_menu_new ();
2926 gtk_widget_show (submenu);
2927 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2929 /* Ugly - since menus can only activate leafs, we have to
2930 * duplicate the item inside the submenu.
2932 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2933 separator = gtk_separator_menu_item_new ();
2934 gtk_widget_show (subitem);
2935 gtk_widget_show (separator);
2936 g_signal_connect (subitem, "activate",
2937 G_CALLBACK (gtk_combo_box_menu_item_activate),
2939 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2940 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2942 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2944 g_signal_connect (item, "activate",
2945 G_CALLBACK (gtk_combo_box_menu_item_activate),
2949 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2950 if (priv->wrap_width && menu == priv->popup_widget)
2951 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2952 gtk_widget_show (item);
2959 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2961 GtkComboBoxPrivate *priv = combo_box->priv;
2963 g_signal_handlers_disconnect_matched (priv->button,
2964 G_SIGNAL_MATCH_DATA,
2966 gtk_combo_box_menu_button_press, NULL);
2967 g_signal_handlers_disconnect_matched (priv->button,
2968 G_SIGNAL_MATCH_DATA,
2970 gtk_combo_box_button_state_changed, combo_box);
2972 /* unparent will remove our latest ref */
2973 gtk_widget_unparent (priv->button);
2976 priv->button = NULL;
2978 priv->separator = NULL;
2980 g_object_unref (priv->column);
2981 priv->column = NULL;
2983 /* changing the popup window will unref the menu and the children */
2991 menu_occupied (GtkMenu *menu,
2995 guint bottom_attach)
2999 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3003 gtk_container_child_get (GTK_CONTAINER (menu),
3007 "bottom-attach", &b,
3011 /* look if this item intersects with the given coordinates */
3012 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3020 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3025 GtkComboBoxPrivate *priv = combo_box->priv;
3026 gint current_col = 0, current_row = 0;
3027 gint rows = 1, cols = 1;
3028 GtkWidget *menu = priv->popup_widget;
3030 if (!GTK_IS_MENU_SHELL (menu))
3033 if (priv->col_column == -1 &&
3034 priv->row_column == -1 &&
3037 gtk_container_child_get (GTK_CONTAINER (menu),
3039 "right-attach", ¤t_col,
3040 "top-attach", ¤t_row,
3042 if (current_col + cols > priv->wrap_width)
3050 if (priv->col_column != -1)
3051 gtk_tree_model_get (priv->model, iter,
3052 priv->col_column, &cols,
3054 if (priv->row_column != -1)
3055 gtk_tree_model_get (priv->model, iter,
3056 priv->row_column, &rows,
3061 if (current_col + cols > priv->wrap_width)
3067 if (!menu_occupied (GTK_MENU (menu),
3068 current_col, current_col + cols,
3069 current_row, current_row + rows))
3076 /* set attach props */
3077 gtk_menu_attach (GTK_MENU (menu), item,
3078 current_col, current_col + cols,
3079 current_row, current_row + rows);
3083 gtk_combo_box_relayout (GtkComboBox *combo_box)
3088 menu = combo_box->priv->popup_widget;
3090 /* do nothing unless we are in menu style and realized */
3091 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3094 list = gtk_container_get_children (GTK_CONTAINER (menu));
3096 for (j = g_list_last (list); j; j = j->prev)
3097 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3099 gtk_combo_box_menu_fill (combo_box);
3106 gtk_combo_box_menu_button_press (GtkWidget *widget,
3107 GdkEventButton *event,
3110 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3111 GtkComboBoxPrivate *priv = combo_box->priv;
3113 if (GTK_IS_MENU (priv->popup_widget) &&
3114 event->type == GDK_BUTTON_PRESS && event->button == 1)
3116 if (priv->focus_on_click &&
3117 !GTK_WIDGET_HAS_FOCUS (priv->button))
3118 gtk_widget_grab_focus (priv->button);
3120 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3129 gtk_combo_box_menu_item_activate (GtkWidget *item,
3132 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3133 GtkWidget *cell_view;
3137 cell_view = GTK_BIN (item)->child;
3139 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3141 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3143 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3145 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3146 gtk_combo_box_set_active_iter (combo_box, &iter);
3149 gtk_tree_path_free (path);
3151 combo_box->priv->editing_canceled = FALSE;
3155 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3158 gboolean sensitive = TRUE; /* fool code checkers */
3160 if (!combo_box->priv->button)
3163 switch (combo_box->priv->button_sensitivity)
3165 case GTK_SENSITIVITY_ON:
3168 case GTK_SENSITIVITY_OFF:
3171 case GTK_SENSITIVITY_AUTO:
3172 sensitive = combo_box->priv->model &&
3173 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3176 g_assert_not_reached ();
3180 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3184 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3189 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3191 if (combo_box->priv->tree_view)
3192 gtk_combo_box_list_popup_resize (combo_box);
3194 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3196 gtk_combo_box_update_sensitivity (combo_box);
3200 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3204 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3205 GtkComboBoxPrivate *priv = combo_box->priv;
3207 if (!gtk_tree_row_reference_valid (priv->active_row))
3209 if (priv->cell_view)
3210 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3211 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3214 if (priv->tree_view)
3215 gtk_combo_box_list_popup_resize (combo_box);
3217 gtk_combo_box_menu_row_deleted (model, path, user_data);
3219 gtk_combo_box_update_sensitivity (combo_box);
3223 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3229 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3231 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3233 if (!combo_box->priv->tree_view)
3234 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3238 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3243 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3244 GtkComboBoxPrivate *priv = combo_box->priv;
3245 GtkTreePath *active_path;
3247 /* FIXME this belongs to GtkCellView */
3248 if (gtk_tree_row_reference_valid (priv->active_row))
3250 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3251 if (gtk_tree_path_compare (path, active_path) == 0 &&
3253 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3254 gtk_tree_path_free (active_path);
3257 if (priv->tree_view)
3258 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3260 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3264 list_popup_resize_idle (gpointer user_data)
3266 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3267 GtkComboBoxPrivate *priv = combo_box->priv;
3268 gint x, y, width, height;
3270 if (priv->tree_view && GTK_WIDGET_MAPPED (priv->popup_window))
3272 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3274 gtk_widget_set_size_request (priv->popup_window, width, height);
3275 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3278 priv->resize_idle_id = 0;
3284 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3286 GtkComboBoxPrivate *priv = combo_box->priv;
3288 if (!priv->resize_idle_id)
3289 priv->resize_idle_id =
3290 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3294 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3299 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3301 gtk_combo_box_list_popup_resize (combo_box);
3306 find_menu_by_path (GtkWidget *menu,
3308 gboolean skip_first)
3313 GtkTreeRowReference *mref;
3317 list = gtk_container_get_children (GTK_CONTAINER (menu));
3320 for (i = list; i; i = i->next)
3322 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3324 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3327 else if (!gtk_tree_row_reference_valid (mref))
3330 mpath = gtk_tree_row_reference_get_path (mref);
3332 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3340 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3345 /* this case is necessary, since the row reference of
3346 * the cell view may already be updated after a deletion
3353 if (gtk_tree_path_compare (mpath, path) == 0)
3355 gtk_tree_path_free (mpath);
3359 if (gtk_tree_path_is_ancestor (mpath, path))
3361 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3362 if (submenu != NULL)
3364 gtk_tree_path_free (mpath);
3365 item = find_menu_by_path (submenu, path, TRUE);
3369 gtk_tree_path_free (mpath);
3379 dump_menu_tree (GtkWidget *menu,
3386 list = gtk_container_get_children (GTK_CONTAINER (menu));
3387 for (i = list; i; i = i->next)
3389 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3391 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3392 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3393 gtk_tree_path_free (path);
3395 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3396 if (submenu != NULL)
3397 dump_menu_tree (submenu, level + 1);
3406 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3411 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3412 GtkComboBoxPrivate *priv = combo_box->priv;
3414 GtkWidget *item, *menu, *separator;
3418 gboolean is_separator;
3420 if (!priv->popup_widget)
3423 depth = gtk_tree_path_get_depth (path);
3424 pos = gtk_tree_path_get_indices (path)[depth - 1];
3427 ppath = gtk_tree_path_copy (path);
3428 gtk_tree_path_up (ppath);
3429 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3430 gtk_tree_path_free (ppath);
3432 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3435 menu = gtk_menu_new ();
3436 gtk_widget_show (menu);
3437 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3439 /* Ugly - since menus can only activate leaves, we have to
3440 * duplicate the item inside the submenu.
3442 gtk_tree_model_iter_parent (model, &piter, iter);
3443 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3444 separator = gtk_separator_menu_item_new ();
3445 g_signal_connect (item, "activate",
3446 G_CALLBACK (gtk_combo_box_menu_item_activate),
3448 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3449 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3450 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3452 gtk_widget_show (item);
3453 gtk_widget_show (separator);
3460 menu = priv->popup_widget;
3461 if (priv->add_tearoffs)
3465 if (priv->row_separator_func)
3466 is_separator = priv->row_separator_func (model, iter,
3467 priv->row_separator_data);
3469 is_separator = FALSE;
3473 item = gtk_separator_menu_item_new ();
3474 g_object_set_data_full (G_OBJECT (item),
3475 I_("gtk-combo-box-item-path"),
3476 gtk_tree_row_reference_new (model, path),
3477 (GDestroyNotify)gtk_tree_row_reference_free);
3481 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3483 g_signal_connect (item, "activate",
3484 G_CALLBACK (gtk_combo_box_menu_item_activate),
3488 gtk_widget_show (item);
3489 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3493 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3497 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3498 GtkComboBoxPrivate *priv = combo_box->priv;
3502 if (!priv->popup_widget)
3505 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3506 menu = gtk_widget_get_parent (item);
3507 gtk_container_remove (GTK_CONTAINER (menu), item);
3509 if (gtk_tree_path_get_depth (path) > 1)
3511 GtkTreePath *parent_path;
3515 parent_path = gtk_tree_path_copy (path);
3516 gtk_tree_path_up (parent_path);
3517 gtk_tree_model_get_iter (model, &iter, parent_path);
3519 if (!gtk_tree_model_iter_has_child (model, &iter))
3521 parent = find_menu_by_path (priv->popup_widget,
3522 parent_path, FALSE);
3523 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3529 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3535 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3537 gtk_combo_box_relayout (combo_box);
3541 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3546 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3547 GtkComboBoxPrivate *priv = combo_box->priv;
3550 gboolean is_separator;
3552 if (!priv->popup_widget)
3555 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3557 if (priv->row_separator_func)
3558 is_separator = priv->row_separator_func (model, iter,
3559 priv->row_separator_data);
3561 is_separator = FALSE;
3563 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3565 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3566 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3569 if (priv->wrap_width && item->parent == priv->popup_widget)
3571 GtkWidget *pitem = NULL;
3574 prev = gtk_tree_path_copy (path);
3576 if (gtk_tree_path_prev (prev))
3577 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3579 gtk_tree_path_free (prev);
3581 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3583 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3588 "bottom-attach", -1,
3591 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3594 width = gtk_combo_box_calc_requested_width (combo_box, path);
3596 if (width > priv->width)
3598 if (priv->cell_view)
3600 gtk_widget_set_size_request (priv->cell_view, width, -1);
3601 gtk_widget_queue_resize (priv->cell_view);
3603 priv->width = width;
3612 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3614 GtkComboBoxPrivate *priv = combo_box->priv;
3615 GtkTreeSelection *sel;
3617 priv->button = gtk_toggle_button_new ();
3618 gtk_widget_set_parent (priv->button,
3619 GTK_BIN (combo_box)->child->parent);
3620 g_signal_connect (priv->button, "button-press-event",
3621 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3622 g_signal_connect (priv->button, "toggled",
3623 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3625 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3626 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3627 priv->separator = NULL;
3628 gtk_widget_show_all (priv->button);
3630 if (priv->cell_view)
3632 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3633 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3635 priv->box = gtk_event_box_new ();
3636 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3639 if (priv->has_frame)
3641 priv->cell_view_frame = gtk_frame_new (NULL);
3642 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3647 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3648 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3652 gtk_widget_set_parent (priv->cell_view_frame,
3653 GTK_BIN (combo_box)->child->parent);
3654 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3655 gtk_widget_show_all (priv->cell_view_frame);
3657 g_signal_connect (priv->box, "button-press-event",
3658 G_CALLBACK (gtk_combo_box_list_button_pressed),
3662 priv->tree_view = gtk_tree_view_new ();
3663 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3664 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3665 gtk_tree_selection_set_select_function (sel,
3666 gtk_combo_box_list_select_func,
3668 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3670 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3672 if (priv->row_separator_func)
3673 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3674 priv->row_separator_func,
3675 priv->row_separator_data,
3678 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3680 priv->column = gtk_tree_view_column_new ();
3681 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3684 gtk_combo_box_sync_cells (combo_box,
3685 GTK_CELL_LAYOUT (priv->column));
3687 if (gtk_tree_row_reference_valid (priv->active_row))
3691 path = gtk_tree_row_reference_get_path (priv->active_row);
3692 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3694 gtk_tree_path_free (path);
3697 /* set sample/popup widgets */
3698 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3700 g_signal_connect (priv->tree_view, "key-press-event",
3701 G_CALLBACK (gtk_combo_box_list_key_press),
3703 g_signal_connect (priv->tree_view, "enter-notify-event",
3704 G_CALLBACK (gtk_combo_box_list_enter_notify),
3706 g_signal_connect (priv->tree_view, "row-expanded",
3707 G_CALLBACK (gtk_combo_box_model_row_expanded),
3709 g_signal_connect (priv->tree_view, "row-collapsed",
3710 G_CALLBACK (gtk_combo_box_model_row_expanded),
3712 g_signal_connect (priv->popup_window, "button-press-event",
3713 G_CALLBACK (gtk_combo_box_list_button_pressed),
3715 g_signal_connect (priv->popup_window, "button-release-event",
3716 G_CALLBACK (gtk_combo_box_list_button_released),
3719 gtk_widget_show (priv->tree_view);
3723 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3725 GtkComboBoxPrivate *priv = combo_box->priv;
3727 /* disconnect signals */
3728 g_signal_handlers_disconnect_matched (priv->tree_view,
3729 G_SIGNAL_MATCH_DATA,
3730 0, 0, NULL, NULL, combo_box);
3731 g_signal_handlers_disconnect_matched (priv->button,
3732 G_SIGNAL_MATCH_DATA,
3734 gtk_combo_box_list_button_pressed,
3736 g_signal_handlers_disconnect_matched (priv->popup_window,
3737 G_SIGNAL_MATCH_DATA,
3739 gtk_combo_box_list_button_pressed,
3741 g_signal_handlers_disconnect_matched (priv->popup_window,
3742 G_SIGNAL_MATCH_DATA,
3744 gtk_combo_box_list_button_released,
3747 g_signal_handlers_disconnect_matched (priv->popup_window,
3748 G_SIGNAL_MATCH_DATA,
3750 gtk_combo_box_child_show,
3753 g_signal_handlers_disconnect_matched (priv->popup_window,
3754 G_SIGNAL_MATCH_DATA,
3756 gtk_combo_box_child_hide,
3760 g_signal_handlers_disconnect_matched (priv->box,
3761 G_SIGNAL_MATCH_DATA,
3763 gtk_combo_box_list_button_pressed,
3766 /* destroy things (unparent will kill the latest ref from us)
3767 * last unref on button will destroy the arrow
3769 gtk_widget_unparent (priv->button);
3770 priv->button = NULL;
3773 if (priv->cell_view)
3775 g_object_set (priv->cell_view,
3776 "background-set", FALSE,
3780 if (priv->cell_view_frame)
3782 gtk_widget_unparent (priv->cell_view_frame);
3783 priv->cell_view_frame = NULL;
3787 if (priv->scroll_timer)
3789 g_source_remove (priv->scroll_timer);
3790 priv->scroll_timer = 0;
3793 if (priv->resize_idle_id)
3795 g_source_remove (priv->resize_idle_id);
3796 priv->resize_idle_id = 0;
3799 gtk_widget_destroy (priv->tree_view);
3801 priv->tree_view = NULL;
3802 if (priv->popup_widget)
3804 g_object_unref (priv->popup_widget);
3805 priv->popup_widget = NULL;
3812 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3813 GdkEventButton *event,
3816 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3817 GtkComboBoxPrivate *priv = combo_box->priv;
3819 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3821 if (ewidget == priv->popup_window)
3824 if ((ewidget != priv->button && ewidget != priv->box) ||
3825 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3828 if (priv->focus_on_click &&
3829 !GTK_WIDGET_HAS_FOCUS (priv->button))
3830 gtk_widget_grab_focus (priv->button);
3832 gtk_combo_box_popup (combo_box);
3834 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3836 priv->auto_scroll = FALSE;
3837 if (priv->scroll_timer == 0)
3838 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3839 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3842 priv->popup_in_progress = TRUE;
3848 gtk_combo_box_list_button_released (GtkWidget *widget,
3849 GdkEventButton *event,
3853 GtkTreePath *path = NULL;
3856 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3857 GtkComboBoxPrivate *priv = combo_box->priv;
3859 gboolean popup_in_progress = FALSE;
3861 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3863 if (priv->popup_in_progress)
3865 popup_in_progress = TRUE;
3866 priv->popup_in_progress = FALSE;
3869 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3871 if (priv->scroll_timer)
3873 g_source_remove (priv->scroll_timer);
3874 priv->scroll_timer = 0;
3877 if (ewidget != priv->tree_view)
3879 if ((ewidget == priv->button ||
3880 ewidget == priv->box) &&
3881 !popup_in_progress &&
3882 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3884 gtk_combo_box_popdown (combo_box);
3888 /* released outside treeview */
3889 if (ewidget != priv->button && ewidget != priv->box)
3891 gtk_combo_box_popdown (combo_box);
3899 /* select something cool */
3900 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3906 return TRUE; /* clicked outside window? */
3908 gtk_tree_model_get_iter (priv->model, &iter, path);
3909 gtk_tree_path_free (path);
3911 gtk_combo_box_popdown (combo_box);
3913 if (tree_column_row_is_sensitive (combo_box, &iter))
3914 gtk_combo_box_set_active_iter (combo_box, &iter);
3920 gtk_combo_box_menu_key_press (GtkWidget *widget,
3924 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3926 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3928 /* The menu hasn't managed the
3929 * event, forward it to the combobox
3931 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3938 gtk_combo_box_list_key_press (GtkWidget *widget,
3942 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3945 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
3946 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
3948 GtkTreeModel *model = NULL;
3950 gtk_combo_box_popdown (combo_box);
3952 if (combo_box->priv->model)
3954 GtkTreeSelection *sel;
3956 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3958 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3959 gtk_combo_box_set_active_iter (combo_box, &iter);
3965 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3967 /* The list hasn't managed the
3968 * event, forward it to the combobox
3970 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3977 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3981 GtkWidget *tree_view = combo_box->priv->tree_view;
3985 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3986 if (adj && adj->upper - adj->lower > adj->page_size)
3988 if (x <= tree_view->allocation.x &&
3989 adj->lower < adj->value)
3991 value = adj->value - (tree_view->allocation.x - x + 1);
3992 gtk_adjustment_set_value (adj, value);
3994 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3995 adj->upper - adj->page_size > adj->value)
3997 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3998 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4002 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4003 if (adj && adj->upper - adj->lower > adj->page_size)
4005 if (y <= tree_view->allocation.y &&
4006 adj->lower < adj->value)
4008 value = adj->value - (tree_view->allocation.y - y + 1);
4009 gtk_adjustment_set_value (adj, value);
4011 else if (y >= tree_view->allocation.height &&
4012 adj->upper - adj->page_size > adj->value)
4014 value = adj->value + (y - tree_view->allocation.height + 1);
4015 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4021 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4023 GtkComboBoxPrivate *priv = combo_box->priv;
4026 if (priv->auto_scroll)
4028 gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
4029 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4036 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4037 GdkEventCrossing *event,
4040 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4042 combo_box->priv->auto_scroll = TRUE;
4048 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4049 GtkTreeModel *model,
4051 gboolean path_currently_selected,
4055 gboolean sensitive = FALSE;
4057 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4059 GList *cells, *cell;
4060 gboolean cell_sensitive, cell_visible;
4062 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4064 if (!column->visible)
4067 gtk_tree_model_get_iter (model, &iter, path);
4068 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4071 cell = cells = gtk_tree_view_column_get_cell_renderers (column);
4074 g_object_get (cell->data,
4075 "sensitive", &cell_sensitive,
4076 "visible", &cell_visible,
4079 if (cell_visible && cell_sensitive)
4084 g_list_free (cells);
4086 sensitive = cell_sensitive;
4093 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4098 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4099 GtkComboBoxPrivate *priv = combo_box->priv;
4102 width = gtk_combo_box_calc_requested_width (combo_box, path);
4104 if (width > priv->width)
4106 if (priv->cell_view)
4108 gtk_widget_set_size_request (priv->cell_view, width, -1);
4109 gtk_widget_queue_resize (priv->cell_view);
4111 priv->width = width;
4116 * GtkCellLayout implementation
4120 pack_start_recurse (GtkWidget *menu,
4121 GtkCellRenderer *cell,
4127 list = gtk_container_get_children (GTK_CONTAINER (menu));
4128 for (i = list; i; i = i->next)
4130 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4131 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4134 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4135 if (submenu != NULL)
4136 pack_start_recurse (submenu, cell, expand);
4143 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4144 GtkCellRenderer *cell,
4147 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4148 ComboCellInfo *info;
4149 GtkComboBoxPrivate *priv;
4151 priv = combo_box->priv;
4153 g_object_ref_sink (cell);
4155 info = g_slice_new0 (ComboCellInfo);
4157 info->expand = expand;
4158 info->pack = GTK_PACK_START;
4160 priv->cells = g_slist_append (priv->cells, info);
4162 if (priv->cell_view)
4163 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4167 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4169 if (GTK_IS_MENU (priv->popup_widget))
4170 pack_start_recurse (priv->popup_widget, cell, expand);
4174 pack_end_recurse (GtkWidget *menu,
4175 GtkCellRenderer *cell,
4181 list = gtk_container_get_children (GTK_CONTAINER (menu));
4182 for (i = list; i; i = i->next)
4184 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4185 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4188 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4189 if (submenu != NULL)
4190 pack_end_recurse (submenu, cell, expand);
4197 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4198 GtkCellRenderer *cell,
4201 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4202 ComboCellInfo *info;
4203 GtkComboBoxPrivate *priv;
4205 priv = combo_box->priv;
4207 g_object_ref_sink (cell);
4209 info = g_slice_new0 (ComboCellInfo);
4211 info->expand = expand;
4212 info->pack = GTK_PACK_END;
4214 priv->cells = g_slist_append (priv->cells, info);
4216 if (priv->cell_view)
4217 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4221 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4223 if (GTK_IS_MENU (priv->popup_widget))
4224 pack_end_recurse (priv->popup_widget, cell, expand);
4228 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4230 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4232 GList *retval = NULL;
4234 for (list = combo_box->priv->cells; list; list = list->next)
4236 ComboCellInfo *info = (ComboCellInfo *)list->data;
4238 retval = g_list_prepend (retval, info->cell);
4241 return g_list_reverse (retval);
4245 clear_recurse (GtkWidget *menu)
4250 list = gtk_container_get_children (GTK_CONTAINER (menu));
4251 for (i = list; i; i = i->next)
4253 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4254 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4256 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4257 if (submenu != NULL)
4258 clear_recurse (submenu);
4265 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4267 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4268 GtkComboBoxPrivate *priv = combo_box->priv;
4271 if (priv->cell_view)
4272 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4275 gtk_tree_view_column_clear (priv->column);
4277 for (i = priv->cells; i; i = i->next)
4279 ComboCellInfo *info = (ComboCellInfo *)i->data;
4281 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4282 g_object_unref (info->cell);
4283 g_slice_free (ComboCellInfo, info);
4286 g_slist_free (priv->cells);
4289 if (GTK_IS_MENU (priv->popup_widget))
4290 clear_recurse (priv->popup_widget);
4294 add_attribute_recurse (GtkWidget *menu,
4295 GtkCellRenderer *cell,
4296 const gchar *attribute,
4302 list = gtk_container_get_children (GTK_CONTAINER (menu));
4303 for (i = list; i; i = i->next)
4305 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4306 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4307 cell, attribute, column);
4309 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4310 if (submenu != NULL)
4311 add_attribute_recurse (submenu, cell, attribute, column);
4318 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4319 GtkCellRenderer *cell,
4320 const gchar *attribute,
4323 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4324 ComboCellInfo *info;
4326 info = gtk_combo_box_get_cell_info (combo_box, cell);
4327 g_return_if_fail (info != NULL);
4329 info->attributes = g_slist_prepend (info->attributes,
4330 GINT_TO_POINTER (column));
4331 info->attributes = g_slist_prepend (info->attributes,
4332 g_strdup (attribute));
4334 if (combo_box->priv->cell_view)
4335 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4336 cell, attribute, column);
4338 if (combo_box->priv->column)
4339 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4340 cell, attribute, column);
4342 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4343 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4344 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4348 combo_cell_data_func (GtkCellLayout *cell_layout,
4349 GtkCellRenderer *cell,
4350 GtkTreeModel *tree_model,
4354 ComboCellInfo *info = (ComboCellInfo *)data;
4355 GtkWidget *parent = NULL;
4360 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4362 if (GTK_IS_WIDGET (cell_layout))
4363 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4365 if (GTK_IS_MENU_ITEM (parent) &&
4366 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4367 g_object_set (cell, "sensitive", TRUE, NULL);
4372 set_cell_data_func_recurse (GtkWidget *menu,
4373 GtkCellRenderer *cell,
4374 ComboCellInfo *info)
4378 GtkWidget *cell_view;
4380 list = gtk_container_get_children (GTK_CONTAINER (menu));
4381 for (i = list; i; i = i->next)
4383 cell_view = GTK_BIN (i->data)->child;
4384 if (GTK_IS_CELL_LAYOUT (cell_view))
4386 /* Override sensitivity for inner nodes; we don't
4387 * want menuitems with submenus to appear insensitive
4389 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4391 combo_cell_data_func,
4393 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4394 if (submenu != NULL)
4395 set_cell_data_func_recurse (submenu, cell, info);
4403 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4404 GtkCellRenderer *cell,
4405 GtkCellLayoutDataFunc func,
4407 GDestroyNotify destroy)
4409 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4410 GtkComboBoxPrivate *priv = combo_box->priv;
4411 ComboCellInfo *info;
4413 info = gtk_combo_box_get_cell_info (combo_box, cell);
4414 g_return_if_fail (info != NULL);
4418 GDestroyNotify d = info->destroy;
4420 info->destroy = NULL;
4421 d (info->func_data);
4425 info->func_data = func_data;
4426 info->destroy = destroy;
4428 if (priv->cell_view)
4429 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4432 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4434 if (GTK_IS_MENU (priv->popup_widget))
4435 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4437 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4441 clear_attributes_recurse (GtkWidget *menu,
4442 GtkCellRenderer *cell)
4447 list = gtk_container_get_children (GTK_CONTAINER (menu));
4448 for (i = list; i; i = i->next)
4450 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4451 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4454 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4455 if (submenu != NULL)
4456 clear_attributes_recurse (submenu, cell);
4463 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4464 GtkCellRenderer *cell)
4466 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4467 GtkComboBoxPrivate *priv;
4468 ComboCellInfo *info;
4471 priv = combo_box->priv;
4473 info = gtk_combo_box_get_cell_info (combo_box, cell);
4474 g_return_if_fail (info != NULL);
4476 list = info->attributes;
4477 while (list && list->next)
4479 g_free (list->data);
4480 list = list->next->next;
4482 g_slist_free (info->attributes);
4483 info->attributes = NULL;
4485 if (priv->cell_view)
4486 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4489 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4491 if (GTK_IS_MENU (priv->popup_widget))
4492 clear_attributes_recurse (priv->popup_widget, cell);
4494 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4498 reorder_recurse (GtkWidget *menu,
4499 GtkCellRenderer *cell,
4505 list = gtk_container_get_children (GTK_CONTAINER (menu));
4506 for (i = list; i; i = i->next)
4508 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4509 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4512 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4513 if (submenu != NULL)
4514 reorder_recurse (submenu, cell, position);
4521 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4522 GtkCellRenderer *cell,
4525 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4526 GtkComboBoxPrivate *priv;
4527 ComboCellInfo *info;
4530 priv = combo_box->priv;
4532 info = gtk_combo_box_get_cell_info (combo_box, cell);
4534 g_return_if_fail (info != NULL);
4535 g_return_if_fail (position >= 0);
4537 link = g_slist_find (priv->cells, info);
4539 g_return_if_fail (link != NULL);
4541 priv->cells = g_slist_delete_link (priv->cells, link);
4542 priv->cells = g_slist_insert (priv->cells, info, position);
4544 if (priv->cell_view)
4545 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4549 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4552 if (GTK_IS_MENU (priv->popup_widget))
4553 reorder_recurse (priv->popup_widget, cell, position);
4555 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4563 * gtk_combo_box_new:
4565 * Creates a new empty #GtkComboBox.
4567 * Return value: A new #GtkComboBox.
4572 gtk_combo_box_new (void)
4574 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4578 * gtk_combo_box_new_with_model:
4579 * @model: A #GtkTreeModel.
4581 * Creates a new #GtkComboBox with the model initialized to @model.
4583 * Return value: A new #GtkComboBox.
4588 gtk_combo_box_new_with_model (GtkTreeModel *model)
4590 GtkComboBox *combo_box;
4592 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4594 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4596 return GTK_WIDGET (combo_box);
4600 * gtk_combo_box_get_wrap_width:
4601 * @combo_box: A #GtkComboBox
4603 * Returns the wrap width which is used to determine the number of columns
4604 * for the popup menu. If the wrap width is larger than 1, the combo box
4607 * Returns: the wrap width.
4612 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4614 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4616 return combo_box->priv->wrap_width;
4620 * gtk_combo_box_set_wrap_width:
4621 * @combo_box: A #GtkComboBox
4622 * @width: Preferred number of columns
4624 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4625 * the preferred number of columns when you want the popup to be layed out
4631 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4634 GtkComboBoxPrivate *priv;
4636 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4637 g_return_if_fail (width >= 0);
4639 priv = combo_box->priv;
4641 if (width != priv->wrap_width)
4643 priv->wrap_width = width;
4645 gtk_combo_box_check_appearance (combo_box);
4646 gtk_combo_box_relayout (combo_box);
4648 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4653 * gtk_combo_box_get_row_span_column:
4654 * @combo_box: A #GtkComboBox
4656 * Returns the column with row span information for @combo_box.
4658 * Returns: the row span column.
4663 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4665 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4667 return combo_box->priv->row_column;
4671 * gtk_combo_box_set_row_span_column:
4672 * @combo_box: A #GtkComboBox.
4673 * @row_span: A column in the model passed during construction.
4675 * Sets the column with row span information for @combo_box to be @row_span.
4676 * The row span column contains integers which indicate how many rows
4677 * an item should span.
4682 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4685 GtkComboBoxPrivate *priv;
4688 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4690 priv = combo_box->priv;
4692 col = gtk_tree_model_get_n_columns (priv->model);
4693 g_return_if_fail (row_span >= -1 && row_span < col);
4695 if (row_span != priv->row_column)
4697 priv->row_column = row_span;
4699 gtk_combo_box_relayout (combo_box);
4701 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4706 * gtk_combo_box_get_column_span_column:
4707 * @combo_box: A #GtkComboBox
4709 * Returns the column with column span information for @combo_box.
4711 * Returns: the column span column.
4716 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4718 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4720 return combo_box->priv->col_column;
4724 * gtk_combo_box_set_column_span_column:
4725 * @combo_box: A #GtkComboBox
4726 * @column_span: A column in the model passed during construction
4728 * Sets the column with column span information for @combo_box to be
4729 * @column_span. The column span column contains integers which indicate
4730 * how many columns an item should span.
4735 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4738 GtkComboBoxPrivate *priv;
4741 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4743 priv = combo_box->priv;
4745 col = gtk_tree_model_get_n_columns (priv->model);
4746 g_return_if_fail (column_span >= -1 && column_span < col);
4748 if (column_span != priv->col_column)
4750 priv->col_column = column_span;
4752 gtk_combo_box_relayout (combo_box);
4754 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4759 * gtk_combo_box_get_active:
4760 * @combo_box: A #GtkComboBox
4762 * Returns the index of the currently active item, or -1 if there's no
4763 * active item. If the model is a non-flat treemodel, and the active item
4764 * is not an immediate child of the root of the tree, this function returns
4765 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4766 * <literal>path</literal> is the #GtkTreePath of the active item.
4768 * Return value: An integer which is the index of the currently active item,
4769 * or -1 if there's no active item.
4774 gtk_combo_box_get_active (GtkComboBox *combo_box)
4776 GtkComboBoxPrivate *priv;
4779 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4781 priv = combo_box->priv;
4783 if (gtk_tree_row_reference_valid (priv->active_row))
4787 path = gtk_tree_row_reference_get_path (priv->active_row);
4788 result = gtk_tree_path_get_indices (path)[0];
4789 gtk_tree_path_free (path);
4798 * gtk_combo_box_set_active:
4799 * @combo_box: A #GtkComboBox
4800 * @index_: An index in the model passed during construction, or -1 to have
4803 * Sets the active item of @combo_box to be the item at @index.
4808 gtk_combo_box_set_active (GtkComboBox *combo_box,
4811 GtkTreePath *path = NULL;
4812 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4813 g_return_if_fail (index_ >= -1);
4816 path = gtk_tree_path_new_from_indices (index_, -1);
4818 gtk_combo_box_set_active_internal (combo_box, path);
4821 gtk_tree_path_free (path);
4825 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4828 GtkComboBoxPrivate *priv = combo_box->priv;
4829 GtkTreePath *active_path;
4832 if (path && gtk_tree_row_reference_valid (priv->active_row))
4834 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4835 path_cmp = gtk_tree_path_compare (path, active_path);
4836 gtk_tree_path_free (active_path);
4841 if (priv->active_row)
4843 gtk_tree_row_reference_free (priv->active_row);
4844 priv->active_row = NULL;
4849 if (priv->tree_view)
4850 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4853 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4855 if (GTK_IS_MENU (menu))
4856 gtk_menu_set_active (menu, -1);
4859 if (priv->cell_view)
4860 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4865 gtk_tree_row_reference_new (priv->model, path);
4867 if (priv->tree_view)
4869 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4872 else if (GTK_IS_MENU (priv->popup_widget))
4874 /* FIXME handle nested menus better */
4875 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4876 gtk_tree_path_get_indices (path)[0]);
4879 if (priv->cell_view)
4880 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4884 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4885 g_object_notify (G_OBJECT (combo_box), "active");
4890 * gtk_combo_box_get_active_iter:
4891 * @combo_box: A #GtkComboBox
4892 * @iter: The uninitialized #GtkTreeIter
4894 * Sets @iter to point to the current active item, if it exists.
4896 * Return value: %TRUE, if @iter was set
4901 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4907 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4909 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4912 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4913 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4914 gtk_tree_path_free (path);
4920 * gtk_combo_box_set_active_iter:
4921 * @combo_box: A #GtkComboBox
4922 * @iter: The #GtkTreeIter
4924 * Sets the current active item to be the one referenced by @iter.
4925 * @iter must correspond to a path of depth one.
4930 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4935 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4937 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4938 gtk_combo_box_set_active_internal (combo_box, path);
4939 gtk_tree_path_free (path);
4943 * gtk_combo_box_set_model:
4944 * @combo_box: A #GtkComboBox
4945 * @model: A #GtkTreeModel
4947 * Sets the model used by @combo_box to be @model. Will unset a previously set
4948 * model (if applicable). If model is %NULL, then it will unset the model.
4950 * Note that this function does not clear the cell renderers, you have to
4951 * call gtk_cell_layout_clear() yourself if you need to set up different
4952 * cell renderers for the new model.
4957 gtk_combo_box_set_model (GtkComboBox *combo_box,
4958 GtkTreeModel *model)
4960 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4961 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4963 if (model == combo_box->priv->model)
4966 gtk_combo_box_unset_model (combo_box);
4971 combo_box->priv->model = model;
4972 g_object_ref (combo_box->priv->model);
4974 combo_box->priv->inserted_id =
4975 g_signal_connect (combo_box->priv->model, "row-inserted",
4976 G_CALLBACK (gtk_combo_box_model_row_inserted),
4978 combo_box->priv->deleted_id =
4979 g_signal_connect (combo_box->priv->model, "row-deleted",
4980 G_CALLBACK (gtk_combo_box_model_row_deleted),
4982 combo_box->priv->reordered_id =
4983 g_signal_connect (combo_box->priv->model, "rows-reordered",
4984 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4986 combo_box->priv->changed_id =
4987 g_signal_connect (combo_box->priv->model, "row-changed",
4988 G_CALLBACK (gtk_combo_box_model_row_changed),
4991 if (combo_box->priv->tree_view)
4994 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4995 combo_box->priv->model);
4996 gtk_combo_box_list_popup_resize (combo_box);
5001 if (combo_box->priv->popup_widget)
5002 gtk_combo_box_menu_fill (combo_box);
5006 if (combo_box->priv->cell_view)
5007 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5008 combo_box->priv->model);
5011 gtk_combo_box_update_sensitivity (combo_box);
5013 g_object_notify (G_OBJECT (combo_box), "model");
5017 * gtk_combo_box_get_model
5018 * @combo_box: A #GtkComboBox
5020 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5022 * Return value: A #GtkTreeModel which was passed during construction.
5027 gtk_combo_box_get_model (GtkComboBox *combo_box)
5029 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5031 return combo_box->priv->model;
5035 /* convenience API for simple text combos */
5038 * gtk_combo_box_new_text:
5040 * Convenience function which constructs a new text combo box, which is a
5041 * #GtkComboBox just displaying strings. If you use this function to create
5042 * a text combo box, you should only manipulate its data source with the
5043 * following convenience functions: gtk_combo_box_append_text(),
5044 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5045 * gtk_combo_box_remove_text().
5047 * Return value: A new text combo box.
5052 gtk_combo_box_new_text (void)
5054 GtkWidget *combo_box;
5055 GtkCellRenderer *cell;
5056 GtkListStore *store;
5058 store = gtk_list_store_new (1, G_TYPE_STRING);
5059 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5060 g_object_unref (store);
5062 cell = gtk_cell_renderer_text_new ();
5063 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5064 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5072 * gtk_combo_box_append_text:
5073 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5076 * Appends @string to the list of strings stored in @combo_box. Note that
5077 * you can only use this function with combo boxes constructed with
5078 * gtk_combo_box_new_text().
5083 gtk_combo_box_append_text (GtkComboBox *combo_box,
5087 GtkListStore *store;
5089 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5090 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5091 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5093 g_return_if_fail (text != NULL);
5095 store = GTK_LIST_STORE (combo_box->priv->model);
5097 gtk_list_store_append (store, &iter);
5098 gtk_list_store_set (store, &iter, 0, text, -1);
5102 * gtk_combo_box_insert_text:
5103 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5104 * @position: An index to insert @text
5107 * Inserts @string at @position in the list of strings stored in @combo_box.
5108 * Note that you can only use this function with combo boxes constructed
5109 * with gtk_combo_box_new_text().
5114 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5119 GtkListStore *store;
5121 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5122 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5123 g_return_if_fail (position >= 0);
5124 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5126 g_return_if_fail (text != NULL);
5128 store = GTK_LIST_STORE (combo_box->priv->model);
5130 gtk_list_store_insert (store, &iter, position);
5131 gtk_list_store_set (store, &iter, 0, text, -1);
5135 * gtk_combo_box_prepend_text:
5136 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5139 * Prepends @string to the list of strings stored in @combo_box. Note that
5140 * you can only use this function with combo boxes constructed with
5141 * gtk_combo_box_new_text().
5146 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5150 GtkListStore *store;
5152 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5153 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5154 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5156 g_return_if_fail (text != NULL);
5158 store = GTK_LIST_STORE (combo_box->priv->model);
5160 gtk_list_store_prepend (store, &iter);
5161 gtk_list_store_set (store, &iter, 0, text, -1);
5165 * gtk_combo_box_remove_text:
5166 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5167 * @position: Index of the item to remove
5169 * Removes the string at @position from @combo_box. Note that you can only use
5170 * this function with combo boxes constructed with gtk_combo_box_new_text().
5175 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5179 GtkListStore *store;
5181 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5182 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5183 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5185 g_return_if_fail (position >= 0);
5187 store = GTK_LIST_STORE (combo_box->priv->model);
5189 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5191 gtk_list_store_remove (store, &iter);
5195 * gtk_combo_box_get_active_text:
5196 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5198 * Returns the currently active string in @combo_box or %NULL if none
5199 * is selected. Note that you can only use this function with combo
5200 * boxes constructed with gtk_combo_box_new_text() and with
5201 * #GtkComboBoxEntry<!-- -->s.
5203 * Returns: a newly allocated string containing the currently active text.
5208 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5210 GtkComboBoxClass *class;
5212 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5214 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5216 if (class->get_active_text)
5217 return class->get_active_text (combo_box);
5223 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5228 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5229 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5230 == G_TYPE_STRING, NULL);
5232 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5233 gtk_tree_model_get (combo_box->priv->model, &iter,
5240 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5241 GtkScrollType scroll)
5244 GtkTreeIter new_iter;
5245 gboolean active_iter;
5248 if (!combo_box->priv->model)
5250 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5254 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5258 case GTK_SCROLL_STEP_BACKWARD:
5259 case GTK_SCROLL_STEP_UP:
5260 case GTK_SCROLL_STEP_LEFT:
5263 found = tree_prev (combo_box, combo_box->priv->model,
5264 &iter, &new_iter, FALSE);
5267 /* else fall through */
5269 case GTK_SCROLL_PAGE_FORWARD:
5270 case GTK_SCROLL_PAGE_DOWN:
5271 case GTK_SCROLL_PAGE_RIGHT:
5272 case GTK_SCROLL_END:
5273 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5276 case GTK_SCROLL_STEP_FORWARD:
5277 case GTK_SCROLL_STEP_DOWN:
5278 case GTK_SCROLL_STEP_RIGHT:
5281 found = tree_next (combo_box, combo_box->priv->model,
5282 &iter, &new_iter, FALSE);
5285 /* else fall through */
5287 case GTK_SCROLL_PAGE_BACKWARD:
5288 case GTK_SCROLL_PAGE_UP:
5289 case GTK_SCROLL_PAGE_LEFT:
5290 case GTK_SCROLL_START:
5291 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5298 if (found && active_iter)
5300 GtkTreePath *old_path;
5301 GtkTreePath *new_path;
5303 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5304 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5306 if (gtk_tree_path_compare (old_path, new_path) == 0)
5309 gtk_tree_path_free (old_path);
5310 gtk_tree_path_free (new_path);
5315 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5319 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5324 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5325 gboolean group_cycling)
5327 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5329 gtk_widget_grab_focus (combo_box->priv->button);
5335 gtk_combo_box_grab_focus (GtkWidget *widget)
5337 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5339 gtk_widget_grab_focus (combo_box->priv->button);
5343 gtk_combo_box_destroy (GtkObject *object)
5345 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5347 if (combo_box->priv->popup_idle_id > 0)
5349 g_source_remove (combo_box->priv->popup_idle_id);
5350 combo_box->priv->popup_idle_id = 0;
5353 gtk_combo_box_popdown (combo_box);
5355 if (combo_box->priv->row_separator_destroy)
5356 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5358 combo_box->priv->row_separator_func = NULL;
5359 combo_box->priv->row_separator_data = NULL;
5360 combo_box->priv->row_separator_destroy = NULL;
5362 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5363 combo_box->priv->cell_view = NULL;
5367 gtk_combo_box_dispose(GObject* object)
5369 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5371 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5373 gtk_combo_box_menu_destroy (combo_box);
5374 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5375 combo_box->priv->popup_widget = NULL;
5378 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5382 gtk_combo_box_finalize (GObject *object)
5384 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5387 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5388 gtk_combo_box_list_destroy (combo_box);
5390 if (combo_box->priv->popup_window)
5391 gtk_widget_destroy (combo_box->priv->popup_window);
5393 gtk_combo_box_unset_model (combo_box);
5395 for (i = combo_box->priv->cells; i; i = i->next)
5397 ComboCellInfo *info = (ComboCellInfo *)i->data;
5398 GSList *list = info->attributes;
5401 info->destroy (info->func_data);
5403 while (list && list->next)
5405 g_free (list->data);
5406 list = list->next->next;
5408 g_slist_free (info->attributes);
5410 g_object_unref (info->cell);
5411 g_slice_free (ComboCellInfo, info);
5413 g_slist_free (combo_box->priv->cells);
5415 g_free (combo_box->priv->tearoff_title);
5417 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5421 gtk_cell_editable_key_press (GtkWidget *widget,
5425 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5427 if (event->keyval == GDK_Escape)
5429 combo_box->priv->editing_canceled = TRUE;
5431 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5432 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5436 else if (event->keyval == GDK_Return ||
5437 event->keyval == GDK_ISO_Enter ||
5438 event->keyval == GDK_KP_Enter)
5440 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5441 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5450 popdown_idle (gpointer data)
5452 GtkComboBox *combo_box;
5454 combo_box = GTK_COMBO_BOX (data);
5456 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5457 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5459 g_object_unref (combo_box);
5465 popdown_handler (GtkWidget *widget,
5468 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5472 popup_idle (gpointer data)
5474 GtkComboBox *combo_box;
5476 combo_box = GTK_COMBO_BOX (data);
5478 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5479 combo_box->priv->cell_view)
5480 g_signal_connect_object (combo_box->priv->popup_widget,
5481 "unmap", G_CALLBACK (popdown_handler),
5484 /* we unset this if a menu item is activated */
5485 combo_box->priv->editing_canceled = TRUE;
5486 gtk_combo_box_popup (combo_box);
5488 combo_box->priv->popup_idle_id = 0;
5489 combo_box->priv->activate_button = 0;
5490 combo_box->priv->activate_time = 0;
5496 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5499 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5501 combo_box->priv->is_cell_renderer = TRUE;
5503 if (combo_box->priv->cell_view)
5505 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5506 G_CALLBACK (gtk_cell_editable_key_press),
5509 gtk_widget_grab_focus (combo_box->priv->button);
5513 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5514 G_CALLBACK (gtk_cell_editable_key_press),
5517 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5518 GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5521 /* we do the immediate popup only for the optionmenu-like
5524 if (combo_box->priv->is_cell_renderer &&
5525 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5527 if (event && event->type == GDK_BUTTON_PRESS)
5529 GdkEventButton *event_button = (GdkEventButton *)event;
5531 combo_box->priv->activate_button = event_button->button;
5532 combo_box->priv->activate_time = event_button->time;
5535 combo_box->priv->popup_idle_id =
5536 gdk_threads_add_idle (popup_idle, combo_box);
5542 * gtk_combo_box_get_add_tearoffs:
5543 * @combo_box: a #GtkComboBox
5545 * Gets the current value of the :add-tearoffs property.
5547 * Return value: the current value of the :add-tearoffs property.
5550 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5552 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5554 return combo_box->priv->add_tearoffs;
5558 * gtk_combo_box_set_add_tearoffs:
5559 * @combo_box: a #GtkComboBox
5560 * @add_tearoffs: %TRUE to add tearoff menu items
5562 * Sets whether the popup menu should have a tearoff
5568 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5569 gboolean add_tearoffs)
5571 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5573 add_tearoffs = add_tearoffs != FALSE;
5575 if (combo_box->priv->add_tearoffs != add_tearoffs)
5577 combo_box->priv->add_tearoffs = add_tearoffs;
5578 gtk_combo_box_check_appearance (combo_box);
5579 gtk_combo_box_relayout (combo_box);
5580 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5585 * gtk_combo_box_get_title:
5586 * @combo_box: a #GtkComboBox
5588 * Gets the current title of the menu in tearoff mode. See
5589 * gtk_combo_box_set_add_tearoffs().
5591 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5592 * string which must not be freed.
5596 G_CONST_RETURN gchar*
5597 gtk_combo_box_get_title (GtkComboBox *combo_box)
5599 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5601 return combo_box->priv->tearoff_title;
5605 gtk_combo_box_update_title (GtkComboBox *combo_box)
5607 gtk_combo_box_check_appearance (combo_box);
5609 if (combo_box->priv->popup_widget &&
5610 GTK_IS_MENU (combo_box->priv->popup_widget))
5611 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5612 combo_box->priv->tearoff_title);
5616 * gtk_combo_box_set_title:
5617 * @combo_box: a #GtkComboBox
5618 * @title: a title for the menu in tearoff mode
5620 * Sets the menu's title in tearoff mode.
5625 gtk_combo_box_set_title (GtkComboBox *combo_box,
5628 GtkComboBoxPrivate *priv;
5630 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5632 priv = combo_box->priv;
5634 if (strcmp (title ? title : "",
5635 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5637 g_free (priv->tearoff_title);
5638 priv->tearoff_title = g_strdup (title);
5640 gtk_combo_box_update_title (combo_box);
5642 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5647 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5649 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5651 return combo_box->priv->editing_canceled;
5655 * gtk_combo_box_get_popup_accessible:
5656 * @combo_box: a #GtkComboBox
5658 * Gets the accessible object corresponding to the combo box's popup.
5660 * This function is mostly intended for use by accessibility technologies;
5661 * applications should have little use for it.
5663 * Returns: the accessible object corresponding to the combo box's popup.
5668 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5672 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5674 if (combo_box->priv->popup_widget)
5676 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5684 * gtk_combo_box_get_row_separator_func:
5685 * @combo_box: a #GtkComboBox
5687 * Returns the current row separator function.
5689 * Return value: the current row separator function.
5693 GtkTreeViewRowSeparatorFunc
5694 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5696 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5698 return combo_box->priv->row_separator_func;
5702 * gtk_combo_box_set_row_separator_func:
5703 * @combo_box: a #GtkComboBox
5704 * @func: a #GtkTreeViewRowSeparatorFunc
5705 * @data: user data to pass to @func, or %NULL
5706 * @destroy: destroy notifier for @data, or %NULL
5708 * Sets the row separator function, which is used to determine
5709 * whether a row should be drawn as a separator. If the row separator
5710 * function is %NULL, no separators are drawn. This is the default value.
5715 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5716 GtkTreeViewRowSeparatorFunc func,
5718 GDestroyNotify destroy)
5720 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5722 if (combo_box->priv->row_separator_destroy)
5723 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5725 combo_box->priv->row_separator_func = func;
5726 combo_box->priv->row_separator_data = data;
5727 combo_box->priv->row_separator_destroy = destroy;
5729 if (combo_box->priv->tree_view)
5730 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5733 gtk_combo_box_relayout (combo_box);
5735 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5739 * gtk_combo_box_set_button_sensitivity:
5740 * @combo_box: a #GtkComboBox
5741 * @sensitivity: specify the sensitivity of the dropdown button
5743 * Sets whether the dropdown button of the combo box should be
5744 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5745 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5750 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5751 GtkSensitivityType sensitivity)
5753 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5755 if (combo_box->priv->button_sensitivity != sensitivity)
5757 combo_box->priv->button_sensitivity = sensitivity;
5758 gtk_combo_box_update_sensitivity (combo_box);
5760 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5765 * gtk_combo_box_get_button_sensitivity:
5766 * @combo_box: a #GtkComboBox
5768 * Returns whether the combo box sets the dropdown button
5769 * sensitive or not when there are no items in the model.
5771 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5772 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5773 * if the button is always insensitive or
5774 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5775 * the model has one item to be selected.
5780 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5782 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5784 return combo_box->priv->button_sensitivity;
5789 * gtk_combo_box_set_focus_on_click:
5790 * @combo: a #GtkComboBox
5791 * @focus_on_click: whether the combo box grabs focus when clicked
5794 * Sets whether the combo box will grab focus when it is clicked with
5795 * the mouse. Making mouse clicks not grab focus is useful in places
5796 * like toolbars where you don't want the keyboard focus removed from
5797 * the main area of the application.
5802 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5803 gboolean focus_on_click)
5805 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5807 focus_on_click = focus_on_click != FALSE;
5809 if (combo_box->priv->focus_on_click != focus_on_click)
5811 combo_box->priv->focus_on_click = focus_on_click;
5813 if (combo_box->priv->button)
5814 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5817 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5822 * gtk_combo_box_get_focus_on_click:
5823 * @combo: a #GtkComboBox
5825 * Returns whether the combo box grabs focus when it is clicked
5826 * with the mouse. See gtk_combo_box_set_focus_on_click().
5828 * Return value: %TRUE if the combo box grabs focus when it is
5829 * clicked with the mouse.
5834 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5836 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5838 return combo_box->priv->focus_on_click;
5843 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5844 GtkBuilder *builder,
5846 const gchar *tagname,
5847 GMarkupParser *parser,
5850 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5851 tagname, parser, data))
5854 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5855 tagname, parser, data);
5859 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5860 GtkBuilder *builder,
5862 const gchar *tagname,
5865 if (strcmp (tagname, "attributes") == 0)
5866 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5869 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5873 #define __GTK_COMBO_BOX_C__
5874 #include "gtkaliasdef.c"