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 "gtkextendedlayout.h"
42 #include "gtkprivate.h"
44 #include <gdk/gdkkeysyms.h>
46 #include <gobject/gvaluecollector.h>
51 #include "gtkmarshalers.h"
54 #include "gtktreeprivate.h"
57 /* WELCOME, to THE house of evil code */
59 typedef struct _ComboCellInfo ComboCellInfo;
62 GtkCellRenderer *cell;
65 GtkCellLayoutDataFunc func;
67 GDestroyNotify destroy;
73 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
75 struct _GtkComboBoxPrivate
83 GtkShadowType shadow_type;
85 gint active; /* Only temporary */
86 GtkTreeRowReference *active_row;
89 GtkTreeViewColumn *column;
92 GtkWidget *cell_view_frame;
99 GtkWidget *popup_widget;
100 GtkWidget *popup_window;
101 GtkWidget *scrolled_window;
108 guint activate_button;
109 guint32 activate_time;
111 guint resize_idle_id;
113 GtkRequisition minimum_size;
114 GtkRequisition natural_size;
118 guint popup_in_progress : 1;
119 guint popup_shown : 1;
120 guint add_tearoffs : 1;
122 guint is_cell_renderer : 1;
123 guint editing_canceled : 1;
124 guint auto_scroll : 1;
125 guint focus_on_click : 1;
126 guint button_sensitivity : 2;
128 GtkTreeViewRowSeparatorFunc row_separator_func;
129 gpointer row_separator_data;
130 GDestroyNotify row_separator_destroy;
132 gchar *tearoff_title;
135 /* While debugging this evil code, I have learned that
136 * there are actually 4 modes to this widget, which can
137 * be characterized as follows
139 * 1) menu mode, no child added
142 * cell_view -> GtkCellView, regular child
143 * cell_view_frame -> NULL
144 * button -> GtkToggleButton set_parent to combo
145 * arrow -> GtkArrow set_parent to button
146 * separator -> GtkVSepator set_parent to button
147 * popup_widget -> GtkMenu
148 * popup_window -> NULL
149 * scrolled_window -> NULL
151 * 2) menu mode, child added
155 * cell_view_frame -> NULL
156 * button -> GtkToggleButton set_parent to combo
157 * arrow -> GtkArrow, child of button
159 * popup_widget -> GtkMenu
160 * popup_window -> NULL
161 * scrolled_window -> NULL
163 * 3) list mode, no child added
165 * tree_view -> GtkTreeView, child of scrolled_window
166 * cell_view -> GtkCellView, regular child
167 * cell_view_frame -> GtkFrame, set parent to combo
168 * button -> GtkToggleButton, set_parent to combo
169 * arrow -> GtkArrow, child of button
171 * popup_widget -> tree_view
172 * popup_window -> GtkWindow
173 * scrolled_window -> GtkScrolledWindow, child of popup_window
175 * 4) list mode, child added
177 * tree_view -> GtkTreeView, child of scrolled_window
179 * cell_view_frame -> NULL
180 * button -> GtkToggleButton, set_parent to combo
181 * arrow -> GtkArrow, child of button
183 * popup_widget -> tree_view
184 * popup_window -> GtkWindow
185 * scrolled_window -> GtkScrolledWindow, child of popup_window
201 PROP_ROW_SPAN_COLUMN,
202 PROP_COLUMN_SPAN_COLUMN,
209 PROP_BUTTON_SENSITIVITY,
210 PROP_EDITING_CANCELED
213 static guint combo_box_signals[LAST_SIGNAL] = {0,};
215 #define BONUS_PADDING 4
216 #define SCROLL_TIME 100
220 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
221 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
222 static void gtk_combo_box_dispose (GObject *object);
223 static void gtk_combo_box_finalize (GObject *object);
224 static void gtk_combo_box_destroy (GtkObject *object);
226 static void gtk_combo_box_set_property (GObject *object,
230 static void gtk_combo_box_get_property (GObject *object,
235 static void gtk_combo_box_state_changed (GtkWidget *widget,
236 GtkStateType previous);
237 static void gtk_combo_box_grab_focus (GtkWidget *widget);
238 static void gtk_combo_box_style_set (GtkWidget *widget,
240 static void gtk_combo_box_button_toggled (GtkWidget *widget,
242 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
243 GtkStateType previous,
245 static void gtk_combo_box_add (GtkContainer *container,
247 static void gtk_combo_box_remove (GtkContainer *container,
250 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
251 GtkCellRenderer *cell);
253 static void gtk_combo_box_menu_show (GtkWidget *menu,
255 static void gtk_combo_box_menu_hide (GtkWidget *menu,
258 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
260 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
265 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
270 static void gtk_combo_box_menu_position (GtkMenu *menu,
276 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
278 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
280 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
282 static void gtk_combo_box_size_allocate (GtkWidget *widget,
283 GtkAllocation *allocation);
284 static void gtk_combo_box_forall (GtkContainer *container,
285 gboolean include_internals,
286 GtkCallback callback,
287 gpointer callback_data);
288 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
289 GdkEventExpose *event);
290 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
291 GdkEventScroll *event);
292 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
295 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
296 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
297 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
298 GtkScrollType scroll);
299 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
300 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
302 /* listening to the model */
303 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
307 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
310 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
315 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
319 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
325 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
330 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
331 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
333 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
334 GdkEventButton *event,
336 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
339 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
340 GdkEventCrossing *event,
342 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
345 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
346 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
347 GdkEventButton *event,
350 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
353 gboolean path_currently_selected,
356 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
360 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
363 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
364 gboolean add_children);
365 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
366 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
369 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
370 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
372 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
376 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
378 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
379 GdkEventButton *event,
381 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
384 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
385 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
389 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
392 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
397 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
401 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
404 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
406 guint32 activate_time);
407 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
412 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
413 GtkCellRenderer *cell,
415 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
416 GtkCellRenderer *cell,
418 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
419 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
420 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
421 GtkCellRenderer *cell,
422 const gchar *attribute,
424 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
425 GtkCellRenderer *cell,
426 GtkCellLayoutDataFunc func,
428 GDestroyNotify destroy);
429 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
430 GtkCellRenderer *cell);
431 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
432 GtkCellRenderer *cell,
434 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
435 gboolean group_cycling);
437 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
438 GtkCellLayout *cell_layout);
439 static void combo_cell_data_func (GtkCellLayout *cell_layout,
440 GtkCellRenderer *cell,
441 GtkTreeModel *tree_model,
444 static void gtk_combo_box_child_show (GtkWidget *widget,
445 GtkComboBox *combo_box);
446 static void gtk_combo_box_child_hide (GtkWidget *widget,
447 GtkComboBox *combo_box);
449 /* GtkBuildable method implementation */
450 static GtkBuildableIface *parent_buildable_iface;
452 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
453 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
456 const gchar *tagname,
457 GMarkupParser *parser,
459 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
462 const gchar *tagname,
465 /* GtkCellEditable method implementations */
466 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
469 static void gtk_combo_box_extended_layout_init (GtkExtendedLayoutIface *iface);
470 static void gtk_combo_box_get_desired_width (GtkExtendedLayout *layout,
473 static void gtk_combo_box_get_desired_height (GtkExtendedLayout *layout,
478 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
479 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
480 gtk_combo_box_cell_layout_init)
481 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
482 gtk_combo_box_cell_editable_init)
483 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
484 gtk_combo_box_buildable_init)
485 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
486 gtk_combo_box_extended_layout_init))
491 gtk_combo_box_class_init (GtkComboBoxClass *klass)
493 GObjectClass *object_class;
494 GtkObjectClass *gtk_object_class;
495 GtkContainerClass *container_class;
496 GtkWidgetClass *widget_class;
497 GtkBindingSet *binding_set;
499 klass->get_active_text = gtk_combo_box_real_get_active_text;
501 container_class = (GtkContainerClass *)klass;
502 container_class->forall = gtk_combo_box_forall;
503 container_class->add = gtk_combo_box_add;
504 container_class->remove = gtk_combo_box_remove;
506 widget_class = (GtkWidgetClass *)klass;
507 widget_class->size_allocate = gtk_combo_box_size_allocate;
508 widget_class->expose_event = gtk_combo_box_expose_event;
509 widget_class->scroll_event = gtk_combo_box_scroll_event;
510 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
511 widget_class->grab_focus = gtk_combo_box_grab_focus;
512 widget_class->style_set = gtk_combo_box_style_set;
513 widget_class->state_changed = gtk_combo_box_state_changed;
515 gtk_object_class = (GtkObjectClass *)klass;
516 gtk_object_class->destroy = gtk_combo_box_destroy;
518 object_class = (GObjectClass *)klass;
519 object_class->dispose = gtk_combo_box_dispose;
520 object_class->finalize = gtk_combo_box_finalize;
521 object_class->set_property = gtk_combo_box_set_property;
522 object_class->get_property = gtk_combo_box_get_property;
526 * GtkComboBox::changed:
527 * @widget: the object which received the signal
529 * The changed signal is emitted when the active
530 * item is changed. The can be due to the user selecting
531 * a different item from the list, or due to a
532 * call to gtk_combo_box_set_active_iter().
533 * It will also be emitted while typing into a GtkComboBoxEntry,
534 * as well as when selecting an item from the GtkComboBoxEntry's list.
538 combo_box_signals[CHANGED] =
539 g_signal_new (I_("changed"),
540 G_OBJECT_CLASS_TYPE (klass),
542 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
544 g_cclosure_marshal_VOID__VOID,
547 * GtkComboBox::move-active:
548 * @widget: the object that received the signal
549 * @scroll_type: a #GtkScrollType
551 * The ::move-active signal is a
552 * <link linkend="keybinding-signals">keybinding signal</link>
553 * which gets emitted to move the active selection.
557 combo_box_signals[MOVE_ACTIVE] =
558 g_signal_new_class_handler (I_("move-active"),
559 G_OBJECT_CLASS_TYPE (klass),
560 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
561 G_CALLBACK (gtk_combo_box_real_move_active),
563 g_cclosure_marshal_VOID__ENUM,
565 GTK_TYPE_SCROLL_TYPE);
568 * GtkComboBox::popup:
569 * @widget: the object that received the signal
571 * The ::popup signal is a
572 * <link linkend="keybinding-signals">keybinding signal</link>
573 * which gets emitted to popup the combo box list.
575 * The default binding for this signal is Alt+Down.
579 combo_box_signals[POPUP] =
580 g_signal_new_class_handler (I_("popup"),
581 G_OBJECT_CLASS_TYPE (klass),
582 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
583 G_CALLBACK (gtk_combo_box_real_popup),
585 g_cclosure_marshal_VOID__VOID,
588 * GtkComboBox::popdown:
589 * @button: the object which received the signal
591 * The ::popdown signal is a
592 * <link linkend="keybinding-signals">keybinding signal</link>
593 * which gets emitted to popdown the combo box list.
595 * The default bindings for this signal are Alt+Up and Escape.
599 combo_box_signals[POPDOWN] =
600 g_signal_new_class_handler (I_("popdown"),
601 G_OBJECT_CLASS_TYPE (klass),
602 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
603 G_CALLBACK (gtk_combo_box_real_popdown),
605 _gtk_marshal_BOOLEAN__VOID,
609 binding_set = gtk_binding_set_by_class (widget_class);
611 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
613 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
616 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
618 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
620 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
623 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
625 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
626 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
628 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
629 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
631 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
632 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
634 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
635 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
637 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
638 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
640 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
642 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
644 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
645 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
647 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
648 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
650 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
651 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
653 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
654 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
656 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
657 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
659 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
662 g_object_class_override_property (object_class,
663 PROP_EDITING_CANCELED,
669 * The model from which the combo box takes the values shown
674 g_object_class_install_property (object_class,
676 g_param_spec_object ("model",
677 P_("ComboBox model"),
678 P_("The model for the combo box"),
680 GTK_PARAM_READWRITE));
683 * GtkComboBox:wrap-width:
685 * If wrap-width is set to a positive value, the list will be
686 * displayed in multiple columns, the number of columns is
687 * determined by wrap-width.
691 g_object_class_install_property (object_class,
693 g_param_spec_int ("wrap-width",
695 P_("Wrap width for laying out the items in a grid"),
699 GTK_PARAM_READWRITE));
703 * GtkComboBox:row-span-column:
705 * If this is set to a non-negative value, it must be the index of a column
706 * of type %G_TYPE_INT in the model.
708 * The values of that column are used to determine how many rows a value in
709 * the list will span. Therefore, the values in the model column pointed to
710 * by this property must be greater than zero and not larger than wrap-width.
714 g_object_class_install_property (object_class,
715 PROP_ROW_SPAN_COLUMN,
716 g_param_spec_int ("row-span-column",
717 P_("Row span column"),
718 P_("TreeModel column containing the row span values"),
722 GTK_PARAM_READWRITE));
726 * GtkComboBox:column-span-column:
728 * If this is set to a non-negative value, it must be the index of a column
729 * of type %G_TYPE_INT in the model.
731 * The values of that column are used to determine how many columns a value
732 * in the list will span.
736 g_object_class_install_property (object_class,
737 PROP_COLUMN_SPAN_COLUMN,
738 g_param_spec_int ("column-span-column",
739 P_("Column span column"),
740 P_("TreeModel column containing the column span values"),
744 GTK_PARAM_READWRITE));
748 * GtkComboBox:active:
750 * The item which is currently active. If the model is a non-flat treemodel,
751 * and the active item is not an immediate child of the root of the tree,
752 * this property has the value
753 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
754 * where <literal>path</literal> is the #GtkTreePath of the active item.
758 g_object_class_install_property (object_class,
760 g_param_spec_int ("active",
762 P_("The item which is currently active"),
766 GTK_PARAM_READWRITE));
769 * GtkComboBox:add-tearoffs:
771 * The add-tearoffs property controls whether generated menus
772 * have tearoff menu items.
774 * Note that this only affects menu style combo boxes.
778 g_object_class_install_property (object_class,
780 g_param_spec_boolean ("add-tearoffs",
781 P_("Add tearoffs to menus"),
782 P_("Whether dropdowns should have a tearoff menu item"),
784 GTK_PARAM_READWRITE));
787 * GtkComboBox:has-frame:
789 * The has-frame property controls whether a frame
790 * is drawn around the entry.
794 g_object_class_install_property (object_class,
796 g_param_spec_boolean ("has-frame",
798 P_("Whether the combo box draws a frame around the child"),
800 GTK_PARAM_READWRITE));
802 g_object_class_install_property (object_class,
804 g_param_spec_boolean ("focus-on-click",
805 P_("Focus on click"),
806 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
808 GTK_PARAM_READWRITE));
811 * GtkComboBox:tearoff-title:
813 * A title that may be displayed by the window manager
814 * when the popup is torn-off.
818 g_object_class_install_property (object_class,
820 g_param_spec_string ("tearoff-title",
822 P_("A title that may be displayed by the window manager when the popup is torn-off"),
824 GTK_PARAM_READWRITE));
828 * GtkComboBox:popup-shown:
830 * Whether the combo boxes dropdown is popped up.
831 * Note that this property is mainly useful, because
832 * it allows you to connect to notify::popup-shown.
836 g_object_class_install_property (object_class,
838 g_param_spec_boolean ("popup-shown",
840 P_("Whether the combo's dropdown is shown"),
842 GTK_PARAM_READABLE));
846 * GtkComboBox:button-sensitivity:
848 * Whether the dropdown button is sensitive when
849 * the model is empty.
853 g_object_class_install_property (object_class,
854 PROP_BUTTON_SENSITIVITY,
855 g_param_spec_enum ("button-sensitivity",
856 P_("Button Sensitivity"),
857 P_("Whether the dropdown button is sensitive when the model is empty"),
858 GTK_TYPE_SENSITIVITY_TYPE,
859 GTK_SENSITIVITY_AUTO,
860 GTK_PARAM_READWRITE));
862 gtk_widget_class_install_style_property (widget_class,
863 g_param_spec_boolean ("appears-as-list",
864 P_("Appears as list"),
865 P_("Whether dropdowns should look like lists rather than menus"),
867 GTK_PARAM_READABLE));
870 * GtkComboBox:arrow-size:
872 * Sets the minimum size of the arrow in the combo box. Note
873 * that the arrow size is coupled to the font size, so in case
874 * a larger font is used, the arrow will be larger than set
879 gtk_widget_class_install_style_property (widget_class,
880 g_param_spec_int ("arrow-size",
882 P_("The minimum size of the arrow in the combo box"),
886 GTK_PARAM_READABLE));
889 * GtkComboBox:shadow-type:
891 * Which kind of shadow to draw around the combo box.
895 gtk_widget_class_install_style_property (widget_class,
896 g_param_spec_enum ("shadow-type",
898 P_("Which kind of shadow to draw around the combo box"),
899 GTK_TYPE_SHADOW_TYPE,
901 GTK_PARAM_READABLE));
903 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
907 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
909 parent_buildable_iface = g_type_interface_peek_parent (iface);
910 iface->add_child = _gtk_cell_layout_buildable_add_child;
911 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
912 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
916 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
918 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
919 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
920 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
921 iface->clear = gtk_combo_box_cell_layout_clear;
922 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
923 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
924 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
925 iface->reorder = gtk_combo_box_cell_layout_reorder;
929 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
931 iface->start_editing = gtk_combo_box_start_editing;
935 gtk_combo_box_init (GtkComboBox *combo_box)
937 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
939 priv->cell_view = gtk_cell_view_new ();
940 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
941 GTK_BIN (combo_box)->child = priv->cell_view;
942 gtk_widget_show (priv->cell_view);
944 memset (&priv->minimum_size, 0x0, sizeof (GtkRequisition));
945 memset (&priv->natural_size, 0x0, sizeof (GtkRequisition));
947 priv->wrap_width = 0;
950 priv->active_row = NULL;
951 priv->col_column = -1;
952 priv->row_column = -1;
954 priv->popup_shown = FALSE;
955 priv->add_tearoffs = FALSE;
956 priv->has_frame = TRUE;
957 priv->is_cell_renderer = FALSE;
958 priv->editing_canceled = FALSE;
959 priv->auto_scroll = FALSE;
960 priv->focus_on_click = TRUE;
961 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
963 combo_box->priv = priv;
965 gtk_combo_box_check_appearance (combo_box);
969 gtk_combo_box_set_property (GObject *object,
974 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
979 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
982 case PROP_WRAP_WIDTH:
983 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
986 case PROP_ROW_SPAN_COLUMN:
987 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
990 case PROP_COLUMN_SPAN_COLUMN:
991 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
995 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
998 case PROP_ADD_TEAROFFS:
999 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1002 case PROP_HAS_FRAME:
1003 combo_box->priv->has_frame = g_value_get_boolean (value);
1006 case PROP_FOCUS_ON_CLICK:
1007 gtk_combo_box_set_focus_on_click (combo_box,
1008 g_value_get_boolean (value));
1011 case PROP_TEAROFF_TITLE:
1012 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1015 case PROP_POPUP_SHOWN:
1016 if (g_value_get_boolean (value))
1017 gtk_combo_box_popup (combo_box);
1019 gtk_combo_box_popdown (combo_box);
1022 case PROP_BUTTON_SENSITIVITY:
1023 gtk_combo_box_set_button_sensitivity (combo_box,
1024 g_value_get_enum (value));
1027 case PROP_EDITING_CANCELED:
1028 combo_box->priv->editing_canceled = g_value_get_boolean (value);
1032 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1038 gtk_combo_box_get_property (GObject *object,
1043 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1044 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
1049 g_value_set_object (value, combo_box->priv->model);
1052 case PROP_WRAP_WIDTH:
1053 g_value_set_int (value, combo_box->priv->wrap_width);
1056 case PROP_ROW_SPAN_COLUMN:
1057 g_value_set_int (value, combo_box->priv->row_column);
1060 case PROP_COLUMN_SPAN_COLUMN:
1061 g_value_set_int (value, combo_box->priv->col_column);
1065 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1068 case PROP_ADD_TEAROFFS:
1069 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1072 case PROP_HAS_FRAME:
1073 g_value_set_boolean (value, combo_box->priv->has_frame);
1076 case PROP_FOCUS_ON_CLICK:
1077 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1080 case PROP_TEAROFF_TITLE:
1081 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1084 case PROP_POPUP_SHOWN:
1085 g_value_set_boolean (value, combo_box->priv->popup_shown);
1088 case PROP_BUTTON_SENSITIVITY:
1089 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1092 case PROP_EDITING_CANCELED:
1093 g_value_set_boolean (value, priv->editing_canceled);
1097 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1103 gtk_combo_box_state_changed (GtkWidget *widget,
1104 GtkStateType previous)
1106 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1107 GtkComboBoxPrivate *priv = combo_box->priv;
1109 if (gtk_widget_get_realized (widget))
1111 if (priv->tree_view && priv->cell_view)
1112 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1113 &widget->style->base[gtk_widget_get_state (widget)]);
1116 gtk_widget_queue_draw (widget);
1120 gtk_combo_box_button_state_changed (GtkWidget *widget,
1121 GtkStateType previous,
1124 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1125 GtkComboBoxPrivate *priv = combo_box->priv;
1127 if (gtk_widget_get_realized (widget))
1129 if (!priv->tree_view && priv->cell_view)
1131 if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) !=
1132 (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE))
1133 gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget));
1135 gtk_widget_set_state (priv->cell_view,
1136 gtk_widget_get_state (widget));
1140 gtk_widget_queue_draw (widget);
1144 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1146 GtkComboBoxPrivate *priv = combo_box->priv;
1147 gboolean appears_as_list;
1149 /* if wrap_width > 0, then we are in grid-mode and forced to use
1152 if (priv->wrap_width)
1153 appears_as_list = FALSE;
1155 gtk_widget_style_get (GTK_WIDGET (combo_box),
1156 "appears-as-list", &appears_as_list,
1159 if (appears_as_list)
1161 /* Destroy all the menu mode widgets, if they exist. */
1162 if (GTK_IS_MENU (priv->popup_widget))
1163 gtk_combo_box_menu_destroy (combo_box);
1165 /* Create the list mode widgets, if they don't already exist. */
1166 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1167 gtk_combo_box_list_setup (combo_box);
1171 /* Destroy all the list mode widgets, if they exist. */
1172 if (GTK_IS_TREE_VIEW (priv->tree_view))
1173 gtk_combo_box_list_destroy (combo_box);
1175 /* Create the menu mode widgets, if they don't already exist. */
1176 if (!GTK_IS_MENU (priv->popup_widget))
1177 gtk_combo_box_menu_setup (combo_box, TRUE);
1180 gtk_widget_style_get (GTK_WIDGET (combo_box),
1181 "shadow-type", &priv->shadow_type,
1186 gtk_combo_box_style_set (GtkWidget *widget,
1189 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1190 GtkComboBoxPrivate *priv = combo_box->priv;
1192 gtk_combo_box_check_appearance (combo_box);
1194 if (priv->tree_view && priv->cell_view)
1195 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1196 &widget->style->base[gtk_widget_get_state (widget)]);
1198 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1199 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1200 GTK_SHADOW_NONE == priv->shadow_type ?
1201 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1205 gtk_combo_box_button_toggled (GtkWidget *widget,
1208 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1210 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1212 if (!combo_box->priv->popup_in_progress)
1213 gtk_combo_box_popup (combo_box);
1216 gtk_combo_box_popdown (combo_box);
1220 gtk_combo_box_add (GtkContainer *container,
1223 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1224 GtkComboBoxPrivate *priv = combo_box->priv;
1226 if (priv->cell_view && priv->cell_view->parent)
1228 gtk_widget_unparent (priv->cell_view);
1229 GTK_BIN (container)->child = NULL;
1230 gtk_widget_queue_resize (GTK_WIDGET (container));
1233 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1234 GTK_BIN (container)->child = widget;
1236 if (priv->cell_view &&
1237 widget != priv->cell_view)
1239 /* since the cell_view was unparented, it's gone now */
1240 priv->cell_view = NULL;
1242 if (!priv->tree_view && priv->separator)
1244 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1246 priv->separator = NULL;
1248 gtk_widget_queue_resize (GTK_WIDGET (container));
1250 else if (priv->cell_view_frame)
1252 gtk_widget_unparent (priv->cell_view_frame);
1253 priv->cell_view_frame = NULL;
1260 gtk_combo_box_remove (GtkContainer *container,
1263 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1264 GtkComboBoxPrivate *priv = combo_box->priv;
1266 gboolean appears_as_list;
1268 if (widget == priv->cell_view)
1269 priv->cell_view = NULL;
1271 gtk_widget_unparent (widget);
1272 GTK_BIN (container)->child = NULL;
1274 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1277 gtk_widget_queue_resize (GTK_WIDGET (container));
1279 if (!priv->tree_view)
1280 appears_as_list = FALSE;
1282 appears_as_list = TRUE;
1284 if (appears_as_list)
1285 gtk_combo_box_list_destroy (combo_box);
1286 else if (GTK_IS_MENU (priv->popup_widget))
1288 gtk_combo_box_menu_destroy (combo_box);
1289 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1290 priv->popup_widget = NULL;
1293 if (!priv->cell_view)
1295 priv->cell_view = gtk_cell_view_new ();
1296 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1297 GTK_BIN (container)->child = priv->cell_view;
1299 gtk_widget_show (priv->cell_view);
1300 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1302 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1306 if (appears_as_list)
1307 gtk_combo_box_list_setup (combo_box);
1309 gtk_combo_box_menu_setup (combo_box, TRUE);
1311 if (gtk_tree_row_reference_valid (priv->active_row))
1313 path = gtk_tree_row_reference_get_path (priv->active_row);
1314 gtk_combo_box_set_active_internal (combo_box, path);
1315 gtk_tree_path_free (path);
1318 gtk_combo_box_set_active_internal (combo_box, NULL);
1321 static ComboCellInfo *
1322 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1323 GtkCellRenderer *cell)
1327 for (i = combo_box->priv->cells; i; i = i->next)
1329 ComboCellInfo *info = (ComboCellInfo *)i->data;
1331 if (info && info->cell == cell)
1339 gtk_combo_box_menu_show (GtkWidget *menu,
1342 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1343 GtkComboBoxPrivate *priv = combo_box->priv;
1345 gtk_combo_box_child_show (menu, user_data);
1347 priv->popup_in_progress = TRUE;
1348 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1350 priv->popup_in_progress = FALSE;
1354 gtk_combo_box_menu_hide (GtkWidget *menu,
1357 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1359 gtk_combo_box_child_hide (menu,user_data);
1361 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1366 gtk_combo_box_detacher (GtkWidget *widget,
1369 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1370 GtkComboBoxPrivate *priv = combo_box->priv;
1372 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1374 g_signal_handlers_disconnect_by_func (menu->toplevel,
1375 gtk_combo_box_menu_show,
1377 g_signal_handlers_disconnect_by_func (menu->toplevel,
1378 gtk_combo_box_menu_hide,
1381 priv->popup_widget = NULL;
1385 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1388 GtkComboBoxPrivate *priv = combo_box->priv;
1390 if (GTK_IS_MENU (priv->popup_widget))
1392 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1393 priv->popup_widget = NULL;
1395 else if (priv->popup_widget)
1397 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1398 priv->popup_widget);
1399 g_object_unref (priv->popup_widget);
1400 priv->popup_widget = NULL;
1403 if (GTK_IS_MENU (popup))
1405 if (priv->popup_window)
1407 gtk_widget_destroy (priv->popup_window);
1408 priv->popup_window = NULL;
1411 priv->popup_widget = popup;
1414 * Note that we connect to show/hide on the toplevel, not the
1415 * menu itself, since the menu is not shown/hidden when it is
1416 * popped up while torn-off.
1418 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1419 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1420 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1421 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1423 gtk_menu_attach_to_widget (GTK_MENU (popup),
1424 GTK_WIDGET (combo_box),
1425 gtk_combo_box_detacher);
1429 if (!priv->popup_window)
1431 GtkWidget *toplevel;
1433 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1434 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1436 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1437 GDK_WINDOW_TYPE_HINT_COMBO);
1439 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1440 G_CALLBACK (gtk_combo_box_child_show),
1442 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1443 G_CALLBACK (gtk_combo_box_child_hide),
1446 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1447 if (GTK_IS_WINDOW (toplevel))
1449 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1450 GTK_WINDOW (priv->popup_window));
1451 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1452 GTK_WINDOW (toplevel));
1455 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1456 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1457 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1459 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1461 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1464 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1467 gtk_widget_show (priv->scrolled_window);
1469 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1470 priv->scrolled_window);
1473 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1476 gtk_widget_show (popup);
1477 g_object_ref (popup);
1478 priv->popup_widget = popup;
1483 gtk_combo_box_menu_position_below (GtkMenu *menu,
1489 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1495 GdkRectangle monitor;
1497 /* FIXME: is using the size request here broken? */
1498 child = GTK_BIN (combo_box)->child;
1502 if (!gtk_widget_get_has_window (child))
1504 sx += child->allocation.x;
1505 sy += child->allocation.y;
1508 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1510 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1511 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1513 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1515 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1518 *x = sx + child->allocation.width - req.width;
1521 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1522 monitor_num = gdk_screen_get_monitor_at_window (screen,
1523 GTK_WIDGET (combo_box)->window);
1524 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1528 else if (*x + req.width > monitor.x + monitor.width)
1529 *x = monitor.x + monitor.width - req.width;
1531 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1532 *y += child->allocation.height;
1533 else if (*y - monitor.y >= req.height)
1535 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1536 *y += child->allocation.height;
1544 gtk_combo_box_menu_position_over (GtkMenu *menu,
1550 GtkComboBox *combo_box;
1554 GtkRequisition requisition;
1561 combo_box = GTK_COMBO_BOX (user_data);
1562 widget = GTK_WIDGET (combo_box);
1564 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1565 menu_width = requisition.width;
1567 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1569 menu_xpos = widget->allocation.x;
1570 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1574 gtk_widget_get_child_requisition (active, &requisition);
1575 menu_ypos -= requisition.height / 2;
1578 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1581 child = children->data;
1583 if (active == child)
1586 if (gtk_widget_get_visible (child))
1588 gtk_widget_get_child_requisition (child, &requisition);
1589 menu_ypos -= requisition.height;
1592 children = children->next;
1595 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1596 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1598 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1599 &menu_xpos, &menu_ypos);
1601 /* Clamp the position on screen */
1602 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1606 else if ((menu_xpos + menu_width) > screen_width)
1607 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1616 gtk_combo_box_menu_position (GtkMenu *menu,
1622 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1623 GtkComboBoxPrivate *priv = combo_box->priv;
1624 GtkWidget *menu_item;
1627 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1628 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1631 /* FIXME handle nested menus better */
1632 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1634 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1637 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1640 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel))
1641 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1642 GDK_WINDOW_TYPE_HINT_COMBO);
1646 gtk_combo_box_list_position (GtkComboBox *combo_box,
1652 GtkComboBoxPrivate *priv = combo_box->priv;
1655 GdkRectangle monitor;
1656 GtkRequisition popup_req;
1657 GtkPolicyType hpolicy, vpolicy;
1659 /* under windows, the drop down list is as wide as the combo box itself.
1661 GtkWidget *sample = GTK_WIDGET (combo_box);
1665 if (!gtk_widget_get_has_window (sample))
1667 *x += sample->allocation.x;
1668 *y += sample->allocation.y;
1671 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1673 *width = sample->allocation.width;
1675 hpolicy = vpolicy = GTK_POLICY_NEVER;
1676 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1678 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1680 if (popup_req.width > *width)
1682 hpolicy = GTK_POLICY_ALWAYS;
1683 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1685 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1688 *height = popup_req.height;
1690 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1691 monitor_num = gdk_screen_get_monitor_at_window (screen,
1692 GTK_WIDGET (combo_box)->window);
1693 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1697 else if (*x + *width > monitor.x + monitor.width)
1698 *x = monitor.x + monitor.width - *width;
1700 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1701 *y += sample->allocation.height;
1702 else if (*y - *height >= monitor.y)
1704 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1706 *y += sample->allocation.height;
1707 *height = monitor.y + monitor.height - *y;
1711 *height = *y - monitor.y;
1715 if (popup_req.height > *height)
1717 vpolicy = GTK_POLICY_ALWAYS;
1719 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1725 cell_view_is_sensitive (GtkCellView *cell_view)
1727 GList *cells, *list;
1730 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1733 for (list = cells; list; list = list->next)
1735 g_object_get (list->data, "sensitive", &sensitive, NULL);
1740 g_list_free (cells);
1746 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1749 GtkComboBoxPrivate *priv = combo_box->priv;
1750 GList *cells, *list;
1756 if (priv->row_separator_func)
1758 if (priv->row_separator_func (priv->model, iter,
1759 priv->row_separator_data))
1763 gtk_tree_view_column_cell_set_cell_data (priv->column,
1765 iter, FALSE, FALSE);
1767 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1770 for (list = cells; list; list = list->next)
1772 g_object_get (list->data, "sensitive", &sensitive, NULL);
1777 g_list_free (cells);
1783 update_menu_sensitivity (GtkComboBox *combo_box,
1786 GtkComboBoxPrivate *priv = combo_box->priv;
1787 GList *children, *child;
1788 GtkWidget *item, *submenu, *separator;
1789 GtkWidget *cell_view;
1795 children = gtk_container_get_children (GTK_CONTAINER (menu));
1797 for (child = children; child; child = child->next)
1799 item = GTK_WIDGET (child->data);
1800 cell_view = GTK_BIN (item)->child;
1802 if (!GTK_IS_CELL_VIEW (cell_view))
1805 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1806 if (submenu != NULL)
1808 gtk_widget_set_sensitive (item, TRUE);
1809 update_menu_sensitivity (combo_box, submenu);
1813 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1815 if (menu != priv->popup_widget && child == children)
1817 separator = GTK_WIDGET (child->next->data);
1818 g_object_set (item, "visible", sensitive, NULL);
1819 g_object_set (separator, "visible", sensitive, NULL);
1822 gtk_widget_set_sensitive (item, sensitive);
1826 g_list_free (children);
1830 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1832 guint32 activate_time)
1834 GtkComboBoxPrivate *priv = combo_box->priv;
1837 GtkRequisition requisition;
1840 update_menu_sensitivity (combo_box, priv->popup_widget);
1843 if (gtk_tree_row_reference_valid (priv->active_row))
1845 path = gtk_tree_row_reference_get_path (priv->active_row);
1846 active_item = gtk_tree_path_get_indices (path)[0];
1847 gtk_tree_path_free (path);
1849 if (priv->add_tearoffs)
1853 /* FIXME handle nested menus better */
1854 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1856 if (priv->wrap_width == 0)
1858 width = GTK_WIDGET (combo_box)->allocation.width;
1859 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1860 gtk_widget_size_request (priv->popup_widget, &requisition);
1862 gtk_widget_set_size_request (priv->popup_widget,
1863 MAX (width, requisition.width), -1);
1866 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1868 gtk_combo_box_menu_position, combo_box,
1869 button, activate_time);
1873 popup_grab_on_window (GdkWindow *window,
1874 guint32 activate_time,
1875 gboolean grab_keyboard)
1877 if ((gdk_pointer_grab (window, TRUE,
1878 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1879 GDK_POINTER_MOTION_MASK,
1880 NULL, NULL, activate_time) == 0))
1882 if (!grab_keyboard ||
1883 gdk_keyboard_grab (window, TRUE,
1884 activate_time) == 0)
1888 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1898 * gtk_combo_box_popup:
1899 * @combo_box: a #GtkComboBox
1901 * Pops up the menu or dropdown list of @combo_box.
1903 * This function is mostly intended for use by accessibility technologies;
1904 * applications should have little use for it.
1909 gtk_combo_box_popup (GtkComboBox *combo_box)
1911 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1913 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1917 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1919 GtkComboBoxPrivate *priv = combo_box->priv;
1920 gint x, y, width, height;
1921 GtkTreePath *path = NULL, *ppath;
1922 GtkWidget *toplevel;
1924 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
1927 if (gtk_widget_get_mapped (priv->popup_widget))
1930 if (GTK_IS_MENU (priv->popup_widget))
1932 gtk_combo_box_menu_popup (combo_box,
1933 priv->activate_button,
1934 priv->activate_time);
1938 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1939 if (GTK_IS_WINDOW (toplevel))
1940 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1941 GTK_WINDOW (priv->popup_window));
1943 gtk_widget_show_all (priv->scrolled_window);
1944 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1946 gtk_widget_set_size_request (priv->popup_window, width, height);
1947 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1949 if (gtk_tree_row_reference_valid (priv->active_row))
1951 path = gtk_tree_row_reference_get_path (priv->active_row);
1952 ppath = gtk_tree_path_copy (path);
1953 if (gtk_tree_path_up (ppath))
1954 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1956 gtk_tree_path_free (ppath);
1958 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1962 gtk_widget_show (priv->popup_window);
1966 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1968 gtk_tree_path_free (path);
1971 gtk_widget_grab_focus (priv->popup_window);
1972 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1975 if (!gtk_widget_has_focus (priv->tree_view))
1976 gtk_widget_grab_focus (priv->tree_view);
1978 if (!popup_grab_on_window (priv->popup_window->window,
1979 GDK_CURRENT_TIME, TRUE))
1981 gtk_widget_hide (priv->popup_window);
1985 gtk_grab_add (priv->popup_window);
1989 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1991 if (combo_box->priv->popup_shown)
1993 gtk_combo_box_popdown (combo_box);
2001 * gtk_combo_box_popdown:
2002 * @combo_box: a #GtkComboBox
2004 * Hides the menu or dropdown list of @combo_box.
2006 * This function is mostly intended for use by accessibility technologies;
2007 * applications should have little use for it.
2012 gtk_combo_box_popdown (GtkComboBox *combo_box)
2014 GtkComboBoxPrivate *priv = combo_box->priv;
2016 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2018 if (GTK_IS_MENU (priv->popup_widget))
2020 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2024 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2027 gtk_grab_remove (priv->popup_window);
2028 gtk_widget_hide_all (priv->popup_window);
2029 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2034 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2037 GtkComboBoxPrivate *priv = combo_box->priv;
2041 if (priv->cell_view)
2042 gtk_widget_style_get (priv->cell_view,
2043 "focus-line-width", &padding,
2048 /* add some pixels for good measure */
2049 padding += BONUS_PADDING;
2051 if (priv->cell_view)
2052 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2057 return req.width + padding;
2060 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2061 gtk_widget_size_request (combo_box->priv->button, &req); \
2064 child.x = allocation->x + shadow_width; \
2066 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2068 child.y = allocation->y + shadow_height; \
2069 child.width = req.width; \
2070 child.height = allocation->height - 2 * shadow_height; \
2071 child.width = MAX (1, child.width); \
2072 child.height = MAX (1, child.height); \
2074 gtk_widget_size_allocate (combo_box->priv->button, &child);
2077 gtk_combo_box_size_allocate (GtkWidget *widget,
2078 GtkAllocation *allocation)
2080 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2081 GtkComboBoxPrivate *priv = combo_box->priv;
2082 gint shadow_width, shadow_height;
2083 gint focus_width, focus_pad;
2084 GtkAllocation child;
2086 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2088 widget->allocation = *allocation;
2090 gtk_widget_style_get (GTK_WIDGET (widget),
2091 "focus-line-width", &focus_width,
2092 "focus-padding", &focus_pad,
2095 if (GTK_SHADOW_NONE != priv->shadow_type)
2097 shadow_width = widget->style->xthickness;
2098 shadow_height = widget->style->ythickness;
2106 if (!priv->tree_view)
2108 if (priv->cell_view)
2110 gint border_width, xthickness, ythickness;
2114 allocation->x += shadow_width;
2115 allocation->y += shadow_height;
2116 allocation->width -= 2 * shadow_width;
2117 allocation->height -= 2 * shadow_height;
2119 gtk_widget_size_allocate (priv->button, allocation);
2121 /* set some things ready */
2122 border_width = GTK_CONTAINER (priv->button)->border_width;
2123 xthickness = priv->button->style->xthickness;
2124 ythickness = priv->button->style->ythickness;
2126 child.x = allocation->x;
2127 child.y = allocation->y;
2128 width = allocation->width;
2129 child.height = allocation->height;
2131 if (!priv->is_cell_renderer)
2133 child.x += border_width + xthickness + focus_width + focus_pad;
2134 child.y += border_width + ythickness + focus_width + focus_pad;
2135 width -= 2 * (child.x - allocation->x);
2136 child.height -= 2 * (child.y - allocation->y);
2140 /* handle the children */
2141 gtk_widget_size_request (priv->arrow, &req);
2142 child.width = req.width;
2144 child.x += width - req.width;
2145 child.width = MAX (1, child.width);
2146 child.height = MAX (1, child.height);
2147 gtk_widget_size_allocate (priv->arrow, &child);
2149 child.x += req.width;
2150 gtk_widget_size_request (priv->separator, &req);
2151 child.width = req.width;
2153 child.x -= req.width;
2154 child.width = MAX (1, child.width);
2155 child.height = MAX (1, child.height);
2156 gtk_widget_size_allocate (priv->separator, &child);
2160 child.x += req.width;
2161 child.width = allocation->x + allocation->width
2162 - (border_width + xthickness + focus_width + focus_pad)
2167 child.width = child.x;
2168 child.x = allocation->x
2169 + border_width + xthickness + focus_width + focus_pad;
2170 child.width -= child.x;
2173 if (gtk_widget_get_visible (priv->popup_widget))
2176 GtkRequisition requisition;
2178 /* Warning here, without the check in the position func */
2179 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2180 if (priv->wrap_width == 0)
2182 width = GTK_WIDGET (combo_box)->allocation.width;
2183 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2184 gtk_widget_size_request (priv->popup_widget, &requisition);
2185 gtk_widget_set_size_request (priv->popup_widget,
2186 MAX (width, requisition.width), -1);
2190 child.width = MAX (1, child.width);
2191 child.height = MAX (1, child.height);
2192 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2196 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2199 child.x = allocation->x + req.width + shadow_width;
2201 child.x = allocation->x + shadow_width;
2202 child.y = allocation->y + shadow_height;
2203 child.width = allocation->width - req.width - 2 * shadow_width;
2204 child.width = MAX (1, child.width);
2205 child.height = MAX (1, child.height);
2206 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2213 /* Combobox thickness + border-width */
2214 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2215 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2218 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2222 child.x = allocation->x + req.width;
2224 child.x = allocation->x;
2226 child.y = allocation->y;
2227 child.width = allocation->width - req.width;
2228 child.height = allocation->height;
2230 if (priv->cell_view_frame)
2234 child.width = MAX (1, child.width - delta_x * 2);
2235 child.height = MAX (1, child.height - delta_y * 2);
2236 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2239 if (priv->has_frame)
2241 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2242 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2243 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2244 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2248 child.width -= delta_x * 2;
2249 child.height -= delta_y * 2;
2256 child.width -= delta_x * 2;
2257 child.height -= delta_y * 2;
2260 if (gtk_widget_get_visible (priv->popup_window))
2262 gint x, y, width, height;
2263 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2264 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2265 gtk_widget_set_size_request (priv->popup_window, width, height);
2269 child.width = MAX (1, child.width);
2270 child.height = MAX (1, child.height);
2272 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2276 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2279 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2281 GtkComboBoxPrivate *priv = combo_box->priv;
2285 g_signal_handler_disconnect (priv->model,
2287 g_signal_handler_disconnect (priv->model,
2289 g_signal_handler_disconnect (priv->model,
2290 priv->reordered_id);
2291 g_signal_handler_disconnect (priv->model,
2296 if (!priv->tree_view)
2298 if (priv->popup_widget)
2299 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2300 (GtkCallback)gtk_widget_destroy, NULL);
2305 g_object_unref (priv->model);
2309 if (priv->active_row)
2311 gtk_tree_row_reference_free (priv->active_row);
2312 priv->active_row = NULL;
2315 if (priv->cell_view)
2316 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2320 gtk_combo_box_forall (GtkContainer *container,
2321 gboolean include_internals,
2322 GtkCallback callback,
2323 gpointer callback_data)
2325 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2326 GtkComboBoxPrivate *priv = combo_box->priv;
2328 if (include_internals)
2331 (* callback) (priv->button, callback_data);
2332 if (priv->cell_view_frame)
2333 (* callback) (priv->cell_view_frame, callback_data);
2336 if (GTK_BIN (container)->child)
2337 (* callback) (GTK_BIN (container)->child, callback_data);
2341 gtk_combo_box_child_show (GtkWidget *widget,
2342 GtkComboBox *combo_box)
2344 GtkComboBoxPrivate *priv = combo_box->priv;
2346 priv->popup_shown = TRUE;
2347 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2351 gtk_combo_box_child_hide (GtkWidget *widget,
2352 GtkComboBox *combo_box)
2354 GtkComboBoxPrivate *priv = combo_box->priv;
2356 priv->popup_shown = FALSE;
2357 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2361 gtk_combo_box_expose_event (GtkWidget *widget,
2362 GdkEventExpose *event)
2364 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2365 GtkComboBoxPrivate *priv = combo_box->priv;
2367 if (gtk_widget_is_drawable (widget) &&
2368 GTK_SHADOW_NONE != priv->shadow_type)
2370 gtk_paint_shadow (widget->style, widget->window,
2371 GTK_STATE_NORMAL, priv->shadow_type,
2372 NULL, widget, "combobox",
2373 widget->allocation.x, widget->allocation.y,
2374 widget->allocation.width, widget->allocation.height);
2377 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2378 priv->button, event);
2380 if (priv->tree_view && priv->cell_view_frame)
2382 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2383 priv->cell_view_frame, event);
2386 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2387 GTK_BIN (widget)->child, event);
2402 path_visible (GtkTreeView *view,
2408 /* Note that we rely on the fact that collapsed rows don't have nodes
2410 return _gtk_tree_view_find_node (view, path, &tree, &node);
2414 tree_next_func (GtkTreeModel *model,
2419 SearchData *search_data = (SearchData *)data;
2421 if (search_data->found)
2423 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2426 if (search_data->visible &&
2427 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2430 search_data->set = TRUE;
2431 search_data->iter = *iter;
2436 if (gtk_tree_path_compare (path, search_data->path) == 0)
2437 search_data->found = TRUE;
2443 tree_next (GtkComboBox *combo,
2444 GtkTreeModel *model,
2449 SearchData search_data;
2451 search_data.combo = combo;
2452 search_data.path = gtk_tree_model_get_path (model, iter);
2453 search_data.visible = visible;
2454 search_data.found = FALSE;
2455 search_data.set = FALSE;
2457 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2459 *next = search_data.iter;
2461 gtk_tree_path_free (search_data.path);
2463 return search_data.set;
2467 tree_prev_func (GtkTreeModel *model,
2472 SearchData *search_data = (SearchData *)data;
2474 if (gtk_tree_path_compare (path, search_data->path) == 0)
2476 search_data->found = TRUE;
2480 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2483 if (search_data->visible &&
2484 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2487 search_data->set = TRUE;
2488 search_data->iter = *iter;
2494 tree_prev (GtkComboBox *combo,
2495 GtkTreeModel *model,
2500 SearchData search_data;
2502 search_data.combo = combo;
2503 search_data.path = gtk_tree_model_get_path (model, iter);
2504 search_data.visible = visible;
2505 search_data.found = FALSE;
2506 search_data.set = FALSE;
2508 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2510 *prev = search_data.iter;
2512 gtk_tree_path_free (search_data.path);
2514 return search_data.set;
2518 tree_last_func (GtkTreeModel *model,
2523 SearchData *search_data = (SearchData *)data;
2525 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2528 /* Note that we rely on the fact that collapsed rows don't have nodes
2530 if (search_data->visible &&
2531 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2534 search_data->set = TRUE;
2535 search_data->iter = *iter;
2541 tree_last (GtkComboBox *combo,
2542 GtkTreeModel *model,
2546 SearchData search_data;
2548 search_data.combo = combo;
2549 search_data.visible = visible;
2550 search_data.set = FALSE;
2552 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2554 *last = search_data.iter;
2556 return search_data.set;
2561 tree_first_func (GtkTreeModel *model,
2566 SearchData *search_data = (SearchData *)data;
2568 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2571 if (search_data->visible &&
2572 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2575 search_data->set = TRUE;
2576 search_data->iter = *iter;
2582 tree_first (GtkComboBox *combo,
2583 GtkTreeModel *model,
2587 SearchData search_data;
2589 search_data.combo = combo;
2590 search_data.visible = visible;
2591 search_data.set = FALSE;
2593 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2595 *first = search_data.iter;
2597 return search_data.set;
2601 gtk_combo_box_scroll_event (GtkWidget *widget,
2602 GdkEventScroll *event)
2604 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2607 GtkTreeIter new_iter;
2609 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2612 if (event->direction == GDK_SCROLL_UP)
2613 found = tree_prev (combo_box, combo_box->priv->model,
2614 &iter, &new_iter, FALSE);
2616 found = tree_next (combo_box, combo_box->priv->model,
2617 &iter, &new_iter, FALSE);
2620 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2630 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2631 GtkCellLayout *cell_layout)
2633 GtkComboBoxPrivate *priv = combo_box->priv;
2636 for (k = priv->cells; k; k = k->next)
2639 ComboCellInfo *info = (ComboCellInfo *)k->data;
2641 if (info->pack == GTK_PACK_START)
2642 gtk_cell_layout_pack_start (cell_layout,
2643 info->cell, info->expand);
2644 else if (info->pack == GTK_PACK_END)
2645 gtk_cell_layout_pack_end (cell_layout,
2646 info->cell, info->expand);
2648 gtk_cell_layout_set_cell_data_func (cell_layout,
2650 combo_cell_data_func, info, NULL);
2652 for (j = info->attributes; j; j = j->next->next)
2654 gtk_cell_layout_add_attribute (cell_layout,
2657 GPOINTER_TO_INT (j->next->data));
2663 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2664 gboolean add_children)
2666 GtkComboBoxPrivate *priv = combo_box->priv;
2669 if (priv->cell_view)
2671 priv->button = gtk_toggle_button_new ();
2672 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2673 priv->focus_on_click);
2675 g_signal_connect (priv->button, "toggled",
2676 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2677 gtk_widget_set_parent (priv->button,
2678 GTK_BIN (combo_box)->child->parent);
2680 priv->box = gtk_hbox_new (FALSE, 0);
2681 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2683 priv->separator = gtk_vseparator_new ();
2684 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2686 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2687 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2689 gtk_widget_show_all (priv->button);
2693 priv->button = gtk_toggle_button_new ();
2694 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2695 priv->focus_on_click);
2697 g_signal_connect (priv->button, "toggled",
2698 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2699 gtk_widget_set_parent (priv->button,
2700 GTK_BIN (combo_box)->child->parent);
2702 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2703 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2704 gtk_widget_show_all (priv->button);
2707 g_signal_connect (priv->button, "button-press-event",
2708 G_CALLBACK (gtk_combo_box_menu_button_press),
2710 g_signal_connect (priv->button, "state-changed",
2711 G_CALLBACK (gtk_combo_box_button_state_changed),
2714 /* create our funky menu */
2715 menu = gtk_menu_new ();
2716 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2717 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2719 g_signal_connect (menu, "key-press-event",
2720 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2721 gtk_combo_box_set_popup_widget (combo_box, menu);
2725 gtk_combo_box_menu_fill (combo_box);
2727 /* the column is needed in tree_column_row_is_sensitive() */
2728 priv->column = gtk_tree_view_column_new ();
2729 g_object_ref_sink (priv->column);
2730 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2732 gtk_combo_box_update_title (combo_box);
2733 gtk_combo_box_update_sensitivity (combo_box);
2737 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2739 GtkComboBoxPrivate *priv = combo_box->priv;
2745 menu = priv->popup_widget;
2747 if (priv->add_tearoffs)
2749 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2751 gtk_widget_show (tearoff);
2753 if (priv->wrap_width)
2754 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2756 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2759 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2763 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2764 GtkTreeModel *model,
2767 GtkWidget *cell_view;
2772 cell_view = gtk_cell_view_new ();
2773 item = gtk_menu_item_new ();
2774 gtk_container_add (GTK_CONTAINER (item), cell_view);
2776 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2777 path = gtk_tree_model_get_path (model, iter);
2778 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2779 gtk_tree_path_free (path);
2781 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2782 gtk_widget_size_request (cell_view, &req);
2783 gtk_widget_show (cell_view);
2789 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2791 GtkTreeIter *parent)
2793 GtkComboBoxPrivate *priv = combo_box->priv;
2794 GtkTreeModel *model = priv->model;
2795 GtkWidget *item, *submenu, *subitem, *separator;
2797 gboolean is_separator;
2802 n_children = gtk_tree_model_iter_n_children (model, parent);
2805 for (i = 0; i < n_children; i++)
2807 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2809 if (priv->row_separator_func)
2810 is_separator = priv->row_separator_func (priv->model, &iter,
2811 priv->row_separator_data);
2813 is_separator = FALSE;
2817 item = gtk_separator_menu_item_new ();
2818 path = gtk_tree_model_get_path (model, &iter);
2819 g_object_set_data_full (G_OBJECT (item),
2820 I_("gtk-combo-box-item-path"),
2821 gtk_tree_row_reference_new (model, path),
2822 (GDestroyNotify)gtk_tree_row_reference_free);
2823 gtk_tree_path_free (path);
2827 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2828 if (gtk_tree_model_iter_has_child (model, &iter))
2830 submenu = gtk_menu_new ();
2831 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
2832 gtk_widget_show (submenu);
2833 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2835 /* Ugly - since menus can only activate leafs, we have to
2836 * duplicate the item inside the submenu.
2838 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2839 separator = gtk_separator_menu_item_new ();
2840 gtk_widget_show (subitem);
2841 gtk_widget_show (separator);
2842 g_signal_connect (subitem, "activate",
2843 G_CALLBACK (gtk_combo_box_menu_item_activate),
2845 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2846 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2848 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2850 g_signal_connect (item, "activate",
2851 G_CALLBACK (gtk_combo_box_menu_item_activate),
2855 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2856 if (priv->wrap_width && menu == priv->popup_widget)
2857 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2858 gtk_widget_show (item);
2865 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2867 GtkComboBoxPrivate *priv = combo_box->priv;
2869 g_signal_handlers_disconnect_matched (priv->button,
2870 G_SIGNAL_MATCH_DATA,
2872 gtk_combo_box_menu_button_press, NULL);
2873 g_signal_handlers_disconnect_matched (priv->button,
2874 G_SIGNAL_MATCH_DATA,
2876 gtk_combo_box_button_state_changed, combo_box);
2878 /* unparent will remove our latest ref */
2879 gtk_widget_unparent (priv->button);
2882 priv->button = NULL;
2884 priv->separator = NULL;
2886 g_object_unref (priv->column);
2887 priv->column = NULL;
2889 /* changing the popup window will unref the menu and the children */
2897 menu_occupied (GtkMenu *menu,
2901 guint bottom_attach)
2905 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2909 gtk_container_child_get (GTK_CONTAINER (menu),
2913 "bottom-attach", &b,
2917 /* look if this item intersects with the given coordinates */
2918 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2926 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2931 GtkComboBoxPrivate *priv = combo_box->priv;
2932 gint current_col = 0, current_row = 0;
2933 gint rows = 1, cols = 1;
2934 GtkWidget *menu = priv->popup_widget;
2936 if (!GTK_IS_MENU_SHELL (menu))
2939 if (priv->col_column == -1 &&
2940 priv->row_column == -1 &&
2943 gtk_container_child_get (GTK_CONTAINER (menu),
2945 "right-attach", ¤t_col,
2946 "top-attach", ¤t_row,
2948 if (current_col + cols > priv->wrap_width)
2956 if (priv->col_column != -1)
2957 gtk_tree_model_get (priv->model, iter,
2958 priv->col_column, &cols,
2960 if (priv->row_column != -1)
2961 gtk_tree_model_get (priv->model, iter,
2962 priv->row_column, &rows,
2967 if (current_col + cols > priv->wrap_width)
2973 if (!menu_occupied (GTK_MENU (menu),
2974 current_col, current_col + cols,
2975 current_row, current_row + rows))
2982 /* set attach props */
2983 gtk_menu_attach (GTK_MENU (menu), item,
2984 current_col, current_col + cols,
2985 current_row, current_row + rows);
2989 gtk_combo_box_relayout (GtkComboBox *combo_box)
2994 menu = combo_box->priv->popup_widget;
2996 /* do nothing unless we are in menu style and realized */
2997 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3000 list = gtk_container_get_children (GTK_CONTAINER (menu));
3002 for (j = g_list_last (list); j; j = j->prev)
3003 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3005 gtk_combo_box_menu_fill (combo_box);
3012 gtk_combo_box_menu_button_press (GtkWidget *widget,
3013 GdkEventButton *event,
3016 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3017 GtkComboBoxPrivate *priv = combo_box->priv;
3019 if (GTK_IS_MENU (priv->popup_widget) &&
3020 event->type == GDK_BUTTON_PRESS && event->button == 1)
3022 if (priv->focus_on_click &&
3023 !gtk_widget_has_focus (priv->button))
3024 gtk_widget_grab_focus (priv->button);
3026 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3035 gtk_combo_box_menu_item_activate (GtkWidget *item,
3038 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3039 GtkWidget *cell_view;
3043 cell_view = GTK_BIN (item)->child;
3045 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3047 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3049 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3051 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3052 gtk_combo_box_set_active_iter (combo_box, &iter);
3055 gtk_tree_path_free (path);
3057 g_object_set (combo_box,
3058 "editing-canceled", FALSE,
3063 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3066 gboolean sensitive = TRUE; /* fool code checkers */
3068 if (!combo_box->priv->button)
3071 switch (combo_box->priv->button_sensitivity)
3073 case GTK_SENSITIVITY_ON:
3076 case GTK_SENSITIVITY_OFF:
3079 case GTK_SENSITIVITY_AUTO:
3080 sensitive = combo_box->priv->model &&
3081 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3084 g_assert_not_reached ();
3088 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3090 /* In list-mode, we also need to update sensitivity of the event box */
3091 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3092 && combo_box->priv->cell_view)
3093 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3097 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3102 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3104 if (combo_box->priv->tree_view)
3105 gtk_combo_box_list_popup_resize (combo_box);
3107 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3109 gtk_combo_box_update_sensitivity (combo_box);
3113 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3117 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3118 GtkComboBoxPrivate *priv = combo_box->priv;
3120 if (!gtk_tree_row_reference_valid (priv->active_row))
3122 if (priv->cell_view)
3123 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3124 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3127 if (priv->tree_view)
3128 gtk_combo_box_list_popup_resize (combo_box);
3130 gtk_combo_box_menu_row_deleted (model, path, user_data);
3132 gtk_combo_box_update_sensitivity (combo_box);
3136 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3142 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3144 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3146 if (!combo_box->priv->tree_view)
3147 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3151 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3156 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3157 GtkComboBoxPrivate *priv = combo_box->priv;
3158 GtkTreePath *active_path;
3160 /* FIXME this belongs to GtkCellView */
3161 if (gtk_tree_row_reference_valid (priv->active_row))
3163 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3164 if (gtk_tree_path_compare (path, active_path) == 0 &&
3166 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3167 gtk_tree_path_free (active_path);
3170 if (priv->tree_view)
3171 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3173 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3177 list_popup_resize_idle (gpointer user_data)
3179 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3180 GtkComboBoxPrivate *priv = combo_box->priv;
3181 gint x, y, width, height;
3183 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3185 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3187 gtk_widget_set_size_request (priv->popup_window, width, height);
3188 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3191 priv->resize_idle_id = 0;
3197 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3199 GtkComboBoxPrivate *priv = combo_box->priv;
3201 if (!priv->resize_idle_id)
3202 priv->resize_idle_id =
3203 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3207 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3212 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3214 gtk_combo_box_list_popup_resize (combo_box);
3219 find_menu_by_path (GtkWidget *menu,
3221 gboolean skip_first)
3226 GtkTreeRowReference *mref;
3230 list = gtk_container_get_children (GTK_CONTAINER (menu));
3233 for (i = list; i; i = i->next)
3235 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3237 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3240 else if (!gtk_tree_row_reference_valid (mref))
3243 mpath = gtk_tree_row_reference_get_path (mref);
3245 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3253 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3258 /* this case is necessary, since the row reference of
3259 * the cell view may already be updated after a deletion
3266 if (gtk_tree_path_compare (mpath, path) == 0)
3268 gtk_tree_path_free (mpath);
3272 if (gtk_tree_path_is_ancestor (mpath, path))
3274 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3275 if (submenu != NULL)
3277 gtk_tree_path_free (mpath);
3278 item = find_menu_by_path (submenu, path, TRUE);
3282 gtk_tree_path_free (mpath);
3292 dump_menu_tree (GtkWidget *menu,
3299 list = gtk_container_get_children (GTK_CONTAINER (menu));
3300 for (i = list; i; i = i->next)
3302 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3304 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3305 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3306 gtk_tree_path_free (path);
3308 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3309 if (submenu != NULL)
3310 dump_menu_tree (submenu, level + 1);
3319 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3324 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3325 GtkComboBoxPrivate *priv = combo_box->priv;
3327 GtkWidget *item, *menu, *separator;
3331 gboolean is_separator;
3333 if (!priv->popup_widget)
3336 depth = gtk_tree_path_get_depth (path);
3337 pos = gtk_tree_path_get_indices (path)[depth - 1];
3340 ppath = gtk_tree_path_copy (path);
3341 gtk_tree_path_up (ppath);
3342 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3343 gtk_tree_path_free (ppath);
3345 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3348 menu = gtk_menu_new ();
3349 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3350 gtk_widget_show (menu);
3351 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3353 /* Ugly - since menus can only activate leaves, we have to
3354 * duplicate the item inside the submenu.
3356 gtk_tree_model_iter_parent (model, &piter, iter);
3357 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3358 separator = gtk_separator_menu_item_new ();
3359 g_signal_connect (item, "activate",
3360 G_CALLBACK (gtk_combo_box_menu_item_activate),
3362 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3363 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3364 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3366 gtk_widget_show (item);
3367 gtk_widget_show (separator);
3374 menu = priv->popup_widget;
3375 if (priv->add_tearoffs)
3379 if (priv->row_separator_func)
3380 is_separator = priv->row_separator_func (model, iter,
3381 priv->row_separator_data);
3383 is_separator = FALSE;
3387 item = gtk_separator_menu_item_new ();
3388 g_object_set_data_full (G_OBJECT (item),
3389 I_("gtk-combo-box-item-path"),
3390 gtk_tree_row_reference_new (model, path),
3391 (GDestroyNotify)gtk_tree_row_reference_free);
3395 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3397 g_signal_connect (item, "activate",
3398 G_CALLBACK (gtk_combo_box_menu_item_activate),
3402 gtk_widget_show (item);
3403 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3407 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3411 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3412 GtkComboBoxPrivate *priv = combo_box->priv;
3416 if (!priv->popup_widget)
3419 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3420 menu = gtk_widget_get_parent (item);
3421 gtk_container_remove (GTK_CONTAINER (menu), item);
3423 if (gtk_tree_path_get_depth (path) > 1)
3425 GtkTreePath *parent_path;
3429 parent_path = gtk_tree_path_copy (path);
3430 gtk_tree_path_up (parent_path);
3431 gtk_tree_model_get_iter (model, &iter, parent_path);
3433 if (!gtk_tree_model_iter_has_child (model, &iter))
3435 parent = find_menu_by_path (priv->popup_widget,
3436 parent_path, FALSE);
3437 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3443 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3449 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3451 gtk_combo_box_relayout (combo_box);
3455 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3460 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3461 GtkComboBoxPrivate *priv = combo_box->priv;
3464 gboolean is_separator;
3466 if (!priv->popup_widget)
3469 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3471 if (priv->row_separator_func)
3472 is_separator = priv->row_separator_func (model, iter,
3473 priv->row_separator_data);
3475 is_separator = FALSE;
3477 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3479 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3480 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3483 if (priv->wrap_width && item->parent == priv->popup_widget)
3485 GtkWidget *pitem = NULL;
3488 prev = gtk_tree_path_copy (path);
3490 if (gtk_tree_path_prev (prev))
3491 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3493 gtk_tree_path_free (prev);
3495 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3497 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3502 "bottom-attach", -1,
3505 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3508 width = gtk_combo_box_calc_requested_width (combo_box, path);
3510 if (width > priv->minimum_size.width)
3512 if (priv->cell_view)
3514 gtk_widget_set_size_request (priv->cell_view, width, -1);
3515 gtk_widget_queue_resize (priv->cell_view);
3517 priv->minimum_size.width = width;
3526 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3528 GtkComboBoxPrivate *priv = combo_box->priv;
3529 GtkTreeSelection *sel;
3531 GtkWidget *widget = GTK_WIDGET (combo_box);
3533 priv->button = gtk_toggle_button_new ();
3534 gtk_widget_set_parent (priv->button,
3535 GTK_BIN (combo_box)->child->parent);
3536 g_signal_connect (priv->button, "button-press-event",
3537 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3538 g_signal_connect (priv->button, "toggled",
3539 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3541 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3542 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3543 priv->separator = NULL;
3544 gtk_widget_show_all (priv->button);
3546 if (priv->cell_view)
3548 style = gtk_widget_get_style (widget);
3549 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3550 &style->base[gtk_widget_get_state (widget)]);
3552 priv->box = gtk_event_box_new ();
3553 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3556 if (priv->has_frame)
3558 priv->cell_view_frame = gtk_frame_new (NULL);
3559 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3564 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3565 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3569 gtk_widget_set_parent (priv->cell_view_frame,
3570 GTK_BIN (combo_box)->child->parent);
3571 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3572 gtk_widget_show_all (priv->cell_view_frame);
3574 g_signal_connect (priv->box, "button-press-event",
3575 G_CALLBACK (gtk_combo_box_list_button_pressed),
3579 priv->tree_view = gtk_tree_view_new ();
3580 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3581 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3582 gtk_tree_selection_set_select_function (sel,
3583 gtk_combo_box_list_select_func,
3585 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3587 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3589 if (priv->row_separator_func)
3590 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3591 priv->row_separator_func,
3592 priv->row_separator_data,
3595 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3597 priv->column = gtk_tree_view_column_new ();
3598 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3601 gtk_combo_box_sync_cells (combo_box,
3602 GTK_CELL_LAYOUT (priv->column));
3604 if (gtk_tree_row_reference_valid (priv->active_row))
3608 path = gtk_tree_row_reference_get_path (priv->active_row);
3609 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3611 gtk_tree_path_free (path);
3614 /* set sample/popup widgets */
3615 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3617 g_signal_connect (priv->tree_view, "key-press-event",
3618 G_CALLBACK (gtk_combo_box_list_key_press),
3620 g_signal_connect (priv->tree_view, "enter-notify-event",
3621 G_CALLBACK (gtk_combo_box_list_enter_notify),
3623 g_signal_connect (priv->tree_view, "row-expanded",
3624 G_CALLBACK (gtk_combo_box_model_row_expanded),
3626 g_signal_connect (priv->tree_view, "row-collapsed",
3627 G_CALLBACK (gtk_combo_box_model_row_expanded),
3629 g_signal_connect (priv->popup_window, "button-press-event",
3630 G_CALLBACK (gtk_combo_box_list_button_pressed),
3632 g_signal_connect (priv->popup_window, "button-release-event",
3633 G_CALLBACK (gtk_combo_box_list_button_released),
3636 gtk_widget_show (priv->tree_view);
3638 gtk_combo_box_update_sensitivity (combo_box);
3642 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3644 GtkComboBoxPrivate *priv = combo_box->priv;
3646 /* disconnect signals */
3647 g_signal_handlers_disconnect_matched (priv->tree_view,
3648 G_SIGNAL_MATCH_DATA,
3649 0, 0, NULL, NULL, combo_box);
3650 g_signal_handlers_disconnect_matched (priv->button,
3651 G_SIGNAL_MATCH_DATA,
3653 gtk_combo_box_list_button_pressed,
3655 g_signal_handlers_disconnect_matched (priv->popup_window,
3656 G_SIGNAL_MATCH_DATA,
3658 gtk_combo_box_list_button_pressed,
3660 g_signal_handlers_disconnect_matched (priv->popup_window,
3661 G_SIGNAL_MATCH_DATA,
3663 gtk_combo_box_list_button_released,
3666 g_signal_handlers_disconnect_matched (priv->popup_window,
3667 G_SIGNAL_MATCH_DATA,
3669 gtk_combo_box_child_show,
3672 g_signal_handlers_disconnect_matched (priv->popup_window,
3673 G_SIGNAL_MATCH_DATA,
3675 gtk_combo_box_child_hide,
3679 g_signal_handlers_disconnect_matched (priv->box,
3680 G_SIGNAL_MATCH_DATA,
3682 gtk_combo_box_list_button_pressed,
3685 /* destroy things (unparent will kill the latest ref from us)
3686 * last unref on button will destroy the arrow
3688 gtk_widget_unparent (priv->button);
3689 priv->button = NULL;
3692 if (priv->cell_view)
3694 g_object_set (priv->cell_view,
3695 "background-set", FALSE,
3699 if (priv->cell_view_frame)
3701 gtk_widget_unparent (priv->cell_view_frame);
3702 priv->cell_view_frame = NULL;
3706 if (priv->scroll_timer)
3708 g_source_remove (priv->scroll_timer);
3709 priv->scroll_timer = 0;
3712 if (priv->resize_idle_id)
3714 g_source_remove (priv->resize_idle_id);
3715 priv->resize_idle_id = 0;
3718 gtk_widget_destroy (priv->tree_view);
3720 priv->tree_view = NULL;
3721 if (priv->popup_widget)
3723 g_object_unref (priv->popup_widget);
3724 priv->popup_widget = NULL;
3731 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3732 GdkEventButton *event,
3735 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3736 GtkComboBoxPrivate *priv = combo_box->priv;
3738 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3740 if (ewidget == priv->popup_window)
3743 if ((ewidget != priv->button && ewidget != priv->box) ||
3744 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3747 if (priv->focus_on_click &&
3748 !gtk_widget_has_focus (priv->button))
3749 gtk_widget_grab_focus (priv->button);
3751 gtk_combo_box_popup (combo_box);
3753 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3755 priv->auto_scroll = FALSE;
3756 if (priv->scroll_timer == 0)
3757 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3758 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3761 priv->popup_in_progress = TRUE;
3767 gtk_combo_box_list_button_released (GtkWidget *widget,
3768 GdkEventButton *event,
3772 GtkTreePath *path = NULL;
3775 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3776 GtkComboBoxPrivate *priv = combo_box->priv;
3778 gboolean popup_in_progress = FALSE;
3780 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3782 if (priv->popup_in_progress)
3784 popup_in_progress = TRUE;
3785 priv->popup_in_progress = FALSE;
3788 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3790 if (priv->scroll_timer)
3792 g_source_remove (priv->scroll_timer);
3793 priv->scroll_timer = 0;
3796 if (ewidget != priv->tree_view)
3798 if ((ewidget == priv->button ||
3799 ewidget == priv->box) &&
3800 !popup_in_progress &&
3801 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3803 gtk_combo_box_popdown (combo_box);
3807 /* released outside treeview */
3808 if (ewidget != priv->button && ewidget != priv->box)
3810 gtk_combo_box_popdown (combo_box);
3818 /* select something cool */
3819 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3825 return TRUE; /* clicked outside window? */
3827 gtk_tree_model_get_iter (priv->model, &iter, path);
3828 gtk_tree_path_free (path);
3830 gtk_combo_box_popdown (combo_box);
3832 if (tree_column_row_is_sensitive (combo_box, &iter))
3833 gtk_combo_box_set_active_iter (combo_box, &iter);
3839 gtk_combo_box_menu_key_press (GtkWidget *widget,
3843 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3845 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3847 /* The menu hasn't managed the
3848 * event, forward it to the combobox
3850 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3857 gtk_combo_box_list_key_press (GtkWidget *widget,
3861 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3864 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
3865 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
3867 GtkTreeModel *model = NULL;
3869 gtk_combo_box_popdown (combo_box);
3871 if (combo_box->priv->model)
3873 GtkTreeSelection *sel;
3875 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3877 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3878 gtk_combo_box_set_active_iter (combo_box, &iter);
3884 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3886 /* The list hasn't managed the
3887 * event, forward it to the combobox
3889 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3896 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3900 GtkWidget *tree_view = combo_box->priv->tree_view;
3904 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3905 if (adj && adj->upper - adj->lower > adj->page_size)
3907 if (x <= tree_view->allocation.x &&
3908 adj->lower < adj->value)
3910 value = adj->value - (tree_view->allocation.x - x + 1);
3911 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3913 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3914 adj->upper - adj->page_size > adj->value)
3916 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3917 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3921 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3922 if (adj && adj->upper - adj->lower > adj->page_size)
3924 if (y <= tree_view->allocation.y &&
3925 adj->lower < adj->value)
3927 value = adj->value - (tree_view->allocation.y - y + 1);
3928 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3930 else if (y >= tree_view->allocation.height &&
3931 adj->upper - adj->page_size > adj->value)
3933 value = adj->value + (y - tree_view->allocation.height + 1);
3934 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3940 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3942 GtkComboBoxPrivate *priv = combo_box->priv;
3945 if (priv->auto_scroll)
3947 gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
3948 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3955 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3956 GdkEventCrossing *event,
3959 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3961 combo_box->priv->auto_scroll = TRUE;
3967 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3968 GtkTreeModel *model,
3970 gboolean path_currently_selected,
3974 gboolean sensitive = FALSE;
3976 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
3978 GList *cells, *cell;
3979 gboolean cell_sensitive, cell_visible;
3981 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3983 if (!column->visible)
3986 gtk_tree_model_get_iter (model, &iter, path);
3987 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3990 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3993 g_object_get (cell->data,
3994 "sensitive", &cell_sensitive,
3995 "visible", &cell_visible,
3998 if (cell_visible && cell_sensitive)
4003 g_list_free (cells);
4005 sensitive = cell_sensitive;
4012 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4017 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4018 GtkComboBoxPrivate *priv = combo_box->priv;
4021 width = gtk_combo_box_calc_requested_width (combo_box, path);
4023 if (width > priv->minimum_size.width)
4025 if (priv->cell_view)
4027 gtk_widget_set_size_request (priv->cell_view, width, -1);
4028 gtk_widget_queue_resize (priv->cell_view);
4030 priv->minimum_size.width = width;
4035 * GtkCellLayout implementation
4039 pack_start_recurse (GtkWidget *menu,
4040 GtkCellRenderer *cell,
4046 list = gtk_container_get_children (GTK_CONTAINER (menu));
4047 for (i = list; i; i = i->next)
4049 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4050 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4053 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4054 if (submenu != NULL)
4055 pack_start_recurse (submenu, cell, expand);
4062 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4063 GtkCellRenderer *cell,
4066 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4067 ComboCellInfo *info;
4068 GtkComboBoxPrivate *priv;
4070 priv = combo_box->priv;
4072 g_object_ref_sink (cell);
4074 info = g_slice_new0 (ComboCellInfo);
4076 info->expand = expand;
4077 info->pack = GTK_PACK_START;
4079 priv->cells = g_slist_append (priv->cells, info);
4081 if (priv->cell_view)
4083 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4089 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4091 if (GTK_IS_MENU (priv->popup_widget))
4092 pack_start_recurse (priv->popup_widget, cell, expand);
4096 pack_end_recurse (GtkWidget *menu,
4097 GtkCellRenderer *cell,
4103 list = gtk_container_get_children (GTK_CONTAINER (menu));
4104 for (i = list; i; i = i->next)
4106 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4107 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4110 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4111 if (submenu != NULL)
4112 pack_end_recurse (submenu, cell, expand);
4119 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4120 GtkCellRenderer *cell,
4123 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4124 ComboCellInfo *info;
4125 GtkComboBoxPrivate *priv;
4127 priv = combo_box->priv;
4129 g_object_ref_sink (cell);
4131 info = g_slice_new0 (ComboCellInfo);
4133 info->expand = expand;
4134 info->pack = GTK_PACK_END;
4136 priv->cells = g_slist_append (priv->cells, info);
4138 if (priv->cell_view)
4139 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4143 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4145 if (GTK_IS_MENU (priv->popup_widget))
4146 pack_end_recurse (priv->popup_widget, cell, expand);
4150 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4152 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4154 GList *retval = NULL;
4156 for (list = combo_box->priv->cells; list; list = list->next)
4158 ComboCellInfo *info = (ComboCellInfo *)list->data;
4160 retval = g_list_prepend (retval, info->cell);
4163 return g_list_reverse (retval);
4167 clear_recurse (GtkWidget *menu)
4172 list = gtk_container_get_children (GTK_CONTAINER (menu));
4173 for (i = list; i; i = i->next)
4175 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4176 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4178 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4179 if (submenu != NULL)
4180 clear_recurse (submenu);
4187 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4189 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4190 GtkComboBoxPrivate *priv = combo_box->priv;
4193 if (priv->cell_view)
4194 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4197 gtk_tree_view_column_clear (priv->column);
4199 for (i = priv->cells; i; i = i->next)
4201 ComboCellInfo *info = (ComboCellInfo *)i->data;
4203 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4204 g_object_unref (info->cell);
4205 g_slice_free (ComboCellInfo, info);
4208 g_slist_free (priv->cells);
4211 if (GTK_IS_MENU (priv->popup_widget))
4212 clear_recurse (priv->popup_widget);
4216 add_attribute_recurse (GtkWidget *menu,
4217 GtkCellRenderer *cell,
4218 const gchar *attribute,
4224 list = gtk_container_get_children (GTK_CONTAINER (menu));
4225 for (i = list; i; i = i->next)
4227 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4228 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4229 cell, attribute, column);
4231 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4232 if (submenu != NULL)
4233 add_attribute_recurse (submenu, cell, attribute, column);
4240 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4241 GtkCellRenderer *cell,
4242 const gchar *attribute,
4245 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4246 ComboCellInfo *info;
4248 info = gtk_combo_box_get_cell_info (combo_box, cell);
4249 g_return_if_fail (info != NULL);
4251 info->attributes = g_slist_prepend (info->attributes,
4252 GINT_TO_POINTER (column));
4253 info->attributes = g_slist_prepend (info->attributes,
4254 g_strdup (attribute));
4256 if (combo_box->priv->cell_view)
4257 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4258 cell, attribute, column);
4260 if (combo_box->priv->column)
4261 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4262 cell, attribute, column);
4264 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4265 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4266 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4270 combo_cell_data_func (GtkCellLayout *cell_layout,
4271 GtkCellRenderer *cell,
4272 GtkTreeModel *tree_model,
4276 ComboCellInfo *info = (ComboCellInfo *)data;
4277 GtkWidget *parent = NULL;
4282 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4284 if (GTK_IS_WIDGET (cell_layout))
4285 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4287 if (GTK_IS_MENU_ITEM (parent) &&
4288 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4289 g_object_set (cell, "sensitive", TRUE, NULL);
4294 set_cell_data_func_recurse (GtkWidget *menu,
4295 GtkCellRenderer *cell,
4296 ComboCellInfo *info)
4300 GtkWidget *cell_view;
4302 list = gtk_container_get_children (GTK_CONTAINER (menu));
4303 for (i = list; i; i = i->next)
4305 cell_view = GTK_BIN (i->data)->child;
4306 if (GTK_IS_CELL_LAYOUT (cell_view))
4308 /* Override sensitivity for inner nodes; we don't
4309 * want menuitems with submenus to appear insensitive
4311 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4313 combo_cell_data_func,
4315 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4316 if (submenu != NULL)
4317 set_cell_data_func_recurse (submenu, cell, info);
4325 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4326 GtkCellRenderer *cell,
4327 GtkCellLayoutDataFunc func,
4329 GDestroyNotify destroy)
4331 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4332 GtkComboBoxPrivate *priv = combo_box->priv;
4333 ComboCellInfo *info;
4335 info = gtk_combo_box_get_cell_info (combo_box, cell);
4336 g_return_if_fail (info != NULL);
4340 GDestroyNotify d = info->destroy;
4342 info->destroy = NULL;
4343 d (info->func_data);
4347 info->func_data = func_data;
4348 info->destroy = destroy;
4350 if (priv->cell_view)
4351 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4354 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4356 if (GTK_IS_MENU (priv->popup_widget))
4357 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4359 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4363 clear_attributes_recurse (GtkWidget *menu,
4364 GtkCellRenderer *cell)
4369 list = gtk_container_get_children (GTK_CONTAINER (menu));
4370 for (i = list; i; i = i->next)
4372 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4373 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4376 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4377 if (submenu != NULL)
4378 clear_attributes_recurse (submenu, cell);
4385 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4386 GtkCellRenderer *cell)
4388 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4389 GtkComboBoxPrivate *priv;
4390 ComboCellInfo *info;
4393 priv = combo_box->priv;
4395 info = gtk_combo_box_get_cell_info (combo_box, cell);
4396 g_return_if_fail (info != NULL);
4398 list = info->attributes;
4399 while (list && list->next)
4401 g_free (list->data);
4402 list = list->next->next;
4404 g_slist_free (info->attributes);
4405 info->attributes = NULL;
4407 if (priv->cell_view)
4408 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4411 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4413 if (GTK_IS_MENU (priv->popup_widget))
4414 clear_attributes_recurse (priv->popup_widget, cell);
4416 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4420 reorder_recurse (GtkWidget *menu,
4421 GtkCellRenderer *cell,
4427 list = gtk_container_get_children (GTK_CONTAINER (menu));
4428 for (i = list; i; i = i->next)
4430 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4431 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4434 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4435 if (submenu != NULL)
4436 reorder_recurse (submenu, cell, position);
4443 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4444 GtkCellRenderer *cell,
4447 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4448 GtkComboBoxPrivate *priv;
4449 ComboCellInfo *info;
4452 priv = combo_box->priv;
4454 info = gtk_combo_box_get_cell_info (combo_box, cell);
4456 g_return_if_fail (info != NULL);
4457 g_return_if_fail (position >= 0);
4459 link = g_slist_find (priv->cells, info);
4461 g_return_if_fail (link != NULL);
4463 priv->cells = g_slist_delete_link (priv->cells, link);
4464 priv->cells = g_slist_insert (priv->cells, info, position);
4466 if (priv->cell_view)
4467 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4471 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4474 if (GTK_IS_MENU (priv->popup_widget))
4475 reorder_recurse (priv->popup_widget, cell, position);
4477 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4485 * gtk_combo_box_new:
4487 * Creates a new empty #GtkComboBox.
4489 * Return value: A new #GtkComboBox.
4494 gtk_combo_box_new (void)
4496 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4500 * gtk_combo_box_new_with_model:
4501 * @model: A #GtkTreeModel.
4503 * Creates a new #GtkComboBox with the model initialized to @model.
4505 * Return value: A new #GtkComboBox.
4510 gtk_combo_box_new_with_model (GtkTreeModel *model)
4512 GtkComboBox *combo_box;
4514 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4516 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4518 return GTK_WIDGET (combo_box);
4522 * gtk_combo_box_get_wrap_width:
4523 * @combo_box: A #GtkComboBox
4525 * Returns the wrap width which is used to determine the number of columns
4526 * for the popup menu. If the wrap width is larger than 1, the combo box
4529 * Returns: the wrap width.
4534 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4536 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4538 return combo_box->priv->wrap_width;
4542 * gtk_combo_box_set_wrap_width:
4543 * @combo_box: A #GtkComboBox
4544 * @width: Preferred number of columns
4546 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4547 * the preferred number of columns when you want the popup to be layed out
4553 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4556 GtkComboBoxPrivate *priv;
4558 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4559 g_return_if_fail (width >= 0);
4561 priv = combo_box->priv;
4563 if (width != priv->wrap_width)
4565 priv->wrap_width = width;
4567 gtk_combo_box_check_appearance (combo_box);
4568 gtk_combo_box_relayout (combo_box);
4570 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4575 * gtk_combo_box_get_row_span_column:
4576 * @combo_box: A #GtkComboBox
4578 * Returns the column with row span information for @combo_box.
4580 * Returns: the row span column.
4585 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4587 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4589 return combo_box->priv->row_column;
4593 * gtk_combo_box_set_row_span_column:
4594 * @combo_box: A #GtkComboBox.
4595 * @row_span: A column in the model passed during construction.
4597 * Sets the column with row span information for @combo_box to be @row_span.
4598 * The row span column contains integers which indicate how many rows
4599 * an item should span.
4604 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4607 GtkComboBoxPrivate *priv;
4610 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4612 priv = combo_box->priv;
4614 col = gtk_tree_model_get_n_columns (priv->model);
4615 g_return_if_fail (row_span >= -1 && row_span < col);
4617 if (row_span != priv->row_column)
4619 priv->row_column = row_span;
4621 gtk_combo_box_relayout (combo_box);
4623 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4628 * gtk_combo_box_get_column_span_column:
4629 * @combo_box: A #GtkComboBox
4631 * Returns the column with column span information for @combo_box.
4633 * Returns: the column span column.
4638 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4640 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4642 return combo_box->priv->col_column;
4646 * gtk_combo_box_set_column_span_column:
4647 * @combo_box: A #GtkComboBox
4648 * @column_span: A column in the model passed during construction
4650 * Sets the column with column span information for @combo_box to be
4651 * @column_span. The column span column contains integers which indicate
4652 * how many columns an item should span.
4657 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4660 GtkComboBoxPrivate *priv;
4663 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4665 priv = combo_box->priv;
4667 col = gtk_tree_model_get_n_columns (priv->model);
4668 g_return_if_fail (column_span >= -1 && column_span < col);
4670 if (column_span != priv->col_column)
4672 priv->col_column = column_span;
4674 gtk_combo_box_relayout (combo_box);
4676 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4681 * gtk_combo_box_get_active:
4682 * @combo_box: A #GtkComboBox
4684 * Returns the index of the currently active item, or -1 if there's no
4685 * active item. If the model is a non-flat treemodel, and the active item
4686 * is not an immediate child of the root of the tree, this function returns
4687 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4688 * <literal>path</literal> is the #GtkTreePath of the active item.
4690 * Return value: An integer which is the index of the currently active item,
4691 * or -1 if there's no active item.
4696 gtk_combo_box_get_active (GtkComboBox *combo_box)
4698 GtkComboBoxPrivate *priv;
4701 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4703 priv = combo_box->priv;
4705 if (gtk_tree_row_reference_valid (priv->active_row))
4709 path = gtk_tree_row_reference_get_path (priv->active_row);
4710 result = gtk_tree_path_get_indices (path)[0];
4711 gtk_tree_path_free (path);
4720 * gtk_combo_box_set_active:
4721 * @combo_box: A #GtkComboBox
4722 * @index_: An index in the model passed during construction, or -1 to have
4725 * Sets the active item of @combo_box to be the item at @index.
4730 gtk_combo_box_set_active (GtkComboBox *combo_box,
4733 GtkTreePath *path = NULL;
4734 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4735 g_return_if_fail (index_ >= -1);
4737 if (combo_box->priv->model == NULL)
4739 /* Save index, in case the model is set after the index */
4740 combo_box->priv->active = index_;
4746 path = gtk_tree_path_new_from_indices (index_, -1);
4748 gtk_combo_box_set_active_internal (combo_box, path);
4751 gtk_tree_path_free (path);
4755 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4758 GtkComboBoxPrivate *priv = combo_box->priv;
4759 GtkTreePath *active_path;
4762 /* Remember whether the initially active row is valid. */
4763 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4765 if (path && is_valid_row_reference)
4767 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4768 path_cmp = gtk_tree_path_compare (path, active_path);
4769 gtk_tree_path_free (active_path);
4774 if (priv->active_row)
4776 gtk_tree_row_reference_free (priv->active_row);
4777 priv->active_row = NULL;
4782 if (priv->tree_view)
4783 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4786 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4788 if (GTK_IS_MENU (menu))
4789 gtk_menu_set_active (menu, -1);
4792 if (priv->cell_view)
4793 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4796 * Do not emit a "changed" signal when an already invalid selection was
4797 * now set to invalid.
4799 if (!is_valid_row_reference)
4805 gtk_tree_row_reference_new (priv->model, path);
4807 if (priv->tree_view)
4809 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4812 else if (GTK_IS_MENU (priv->popup_widget))
4814 /* FIXME handle nested menus better */
4815 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4816 gtk_tree_path_get_indices (path)[0]);
4819 if (priv->cell_view)
4820 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4824 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4825 g_object_notify (G_OBJECT (combo_box), "active");
4830 * gtk_combo_box_get_active_iter:
4831 * @combo_box: A #GtkComboBox
4832 * @iter: The uninitialized #GtkTreeIter
4834 * Sets @iter to point to the current active item, if it exists.
4836 * Return value: %TRUE, if @iter was set
4841 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4847 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4849 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4852 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4853 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4854 gtk_tree_path_free (path);
4860 * gtk_combo_box_set_active_iter:
4861 * @combo_box: A #GtkComboBox
4862 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4864 * Sets the current active item to be the one referenced by @iter, or
4865 * unsets the active item if @iter is %NULL.
4867 * @iter must correspond to a path of depth one, or be %NULL.
4872 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4875 GtkTreePath *path = NULL;
4877 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4880 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4882 gtk_combo_box_set_active_internal (combo_box, path);
4883 gtk_tree_path_free (path);
4887 * gtk_combo_box_set_model:
4888 * @combo_box: A #GtkComboBox
4889 * @model: (allow-none): A #GtkTreeModel
4891 * Sets the model used by @combo_box to be @model. Will unset a previously set
4892 * model (if applicable). If model is %NULL, then it will unset the model.
4894 * Note that this function does not clear the cell renderers, you have to
4895 * call gtk_cell_layout_clear() yourself if you need to set up different
4896 * cell renderers for the new model.
4901 gtk_combo_box_set_model (GtkComboBox *combo_box,
4902 GtkTreeModel *model)
4904 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4905 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4907 if (model == combo_box->priv->model)
4910 gtk_combo_box_unset_model (combo_box);
4915 combo_box->priv->model = model;
4916 g_object_ref (combo_box->priv->model);
4918 combo_box->priv->inserted_id =
4919 g_signal_connect (combo_box->priv->model, "row-inserted",
4920 G_CALLBACK (gtk_combo_box_model_row_inserted),
4922 combo_box->priv->deleted_id =
4923 g_signal_connect (combo_box->priv->model, "row-deleted",
4924 G_CALLBACK (gtk_combo_box_model_row_deleted),
4926 combo_box->priv->reordered_id =
4927 g_signal_connect (combo_box->priv->model, "rows-reordered",
4928 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4930 combo_box->priv->changed_id =
4931 g_signal_connect (combo_box->priv->model, "row-changed",
4932 G_CALLBACK (gtk_combo_box_model_row_changed),
4935 if (combo_box->priv->tree_view)
4938 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4939 combo_box->priv->model);
4940 gtk_combo_box_list_popup_resize (combo_box);
4945 if (combo_box->priv->popup_widget)
4946 gtk_combo_box_menu_fill (combo_box);
4950 if (combo_box->priv->cell_view)
4951 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4952 combo_box->priv->model);
4954 if (combo_box->priv->active != -1)
4956 /* If an index was set in advance, apply it now */
4957 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4958 combo_box->priv->active = -1;
4962 gtk_combo_box_update_sensitivity (combo_box);
4964 g_object_notify (G_OBJECT (combo_box), "model");
4968 * gtk_combo_box_get_model
4969 * @combo_box: A #GtkComboBox
4971 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4973 * Return value: (transfer none): A #GtkTreeModel which was passed during construction.
4978 gtk_combo_box_get_model (GtkComboBox *combo_box)
4980 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4982 return combo_box->priv->model;
4986 /* convenience API for simple text combos */
4989 * gtk_combo_box_new_text:
4991 * Convenience function which constructs a new text combo box, which is a
4992 * #GtkComboBox just displaying strings. If you use this function to create
4993 * a text combo box, you should only manipulate its data source with the
4994 * following convenience functions: gtk_combo_box_append_text(),
4995 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
4996 * gtk_combo_box_remove_text().
4998 * Return value: (transfer none): A new text combo box.
5003 gtk_combo_box_new_text (void)
5005 GtkWidget *combo_box;
5006 GtkCellRenderer *cell;
5007 GtkListStore *store;
5009 store = gtk_list_store_new (1, G_TYPE_STRING);
5010 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5011 g_object_unref (store);
5013 cell = gtk_cell_renderer_text_new ();
5014 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5015 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5023 * gtk_combo_box_append_text:
5024 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5027 * Appends @string to the list of strings stored in @combo_box. Note that
5028 * you can only use this function with combo boxes constructed with
5029 * gtk_combo_box_new_text().
5034 gtk_combo_box_append_text (GtkComboBox *combo_box,
5038 GtkListStore *store;
5040 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5041 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5042 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5044 g_return_if_fail (text != NULL);
5046 store = GTK_LIST_STORE (combo_box->priv->model);
5048 gtk_list_store_append (store, &iter);
5049 gtk_list_store_set (store, &iter, 0, text, -1);
5053 * gtk_combo_box_insert_text:
5054 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5055 * @position: An index to insert @text
5058 * Inserts @string at @position in the list of strings stored in @combo_box.
5059 * Note that you can only use this function with combo boxes constructed
5060 * with gtk_combo_box_new_text().
5065 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5070 GtkListStore *store;
5072 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5073 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5074 g_return_if_fail (position >= 0);
5075 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5077 g_return_if_fail (text != NULL);
5079 store = GTK_LIST_STORE (combo_box->priv->model);
5081 gtk_list_store_insert (store, &iter, position);
5082 gtk_list_store_set (store, &iter, 0, text, -1);
5086 * gtk_combo_box_prepend_text:
5087 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5090 * Prepends @string to the list of strings stored in @combo_box. Note that
5091 * you can only use this function with combo boxes constructed with
5092 * gtk_combo_box_new_text().
5097 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5101 GtkListStore *store;
5103 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5104 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5105 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5107 g_return_if_fail (text != NULL);
5109 store = GTK_LIST_STORE (combo_box->priv->model);
5111 gtk_list_store_prepend (store, &iter);
5112 gtk_list_store_set (store, &iter, 0, text, -1);
5116 * gtk_combo_box_remove_text:
5117 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5118 * @position: Index of the item to remove
5120 * Removes the string at @position from @combo_box. Note that you can only use
5121 * this function with combo boxes constructed with gtk_combo_box_new_text().
5126 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5130 GtkListStore *store;
5132 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5133 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5134 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5136 g_return_if_fail (position >= 0);
5138 store = GTK_LIST_STORE (combo_box->priv->model);
5140 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5142 gtk_list_store_remove (store, &iter);
5146 * gtk_combo_box_get_active_text:
5147 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5149 * Returns the currently active string in @combo_box or %NULL if none
5150 * is selected. Note that you can only use this function with combo
5151 * boxes constructed with gtk_combo_box_new_text() and with
5152 * #GtkComboBoxEntry<!-- -->s.
5154 * Returns: a newly allocated string containing the currently active text.
5155 * Must be freed with g_free().
5160 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5162 GtkComboBoxClass *class;
5164 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5166 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5168 if (class->get_active_text)
5169 return class->get_active_text (combo_box);
5175 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5180 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5181 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5182 == G_TYPE_STRING, NULL);
5184 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5185 gtk_tree_model_get (combo_box->priv->model, &iter,
5192 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5193 GtkScrollType scroll)
5196 GtkTreeIter new_iter;
5197 gboolean active_iter;
5200 if (!combo_box->priv->model)
5202 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5206 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5210 case GTK_SCROLL_STEP_BACKWARD:
5211 case GTK_SCROLL_STEP_UP:
5212 case GTK_SCROLL_STEP_LEFT:
5215 found = tree_prev (combo_box, combo_box->priv->model,
5216 &iter, &new_iter, FALSE);
5219 /* else fall through */
5221 case GTK_SCROLL_PAGE_FORWARD:
5222 case GTK_SCROLL_PAGE_DOWN:
5223 case GTK_SCROLL_PAGE_RIGHT:
5224 case GTK_SCROLL_END:
5225 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5228 case GTK_SCROLL_STEP_FORWARD:
5229 case GTK_SCROLL_STEP_DOWN:
5230 case GTK_SCROLL_STEP_RIGHT:
5233 found = tree_next (combo_box, combo_box->priv->model,
5234 &iter, &new_iter, FALSE);
5237 /* else fall through */
5239 case GTK_SCROLL_PAGE_BACKWARD:
5240 case GTK_SCROLL_PAGE_UP:
5241 case GTK_SCROLL_PAGE_LEFT:
5242 case GTK_SCROLL_START:
5243 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5250 if (found && active_iter)
5252 GtkTreePath *old_path;
5253 GtkTreePath *new_path;
5255 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5256 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5258 if (gtk_tree_path_compare (old_path, new_path) == 0)
5261 gtk_tree_path_free (old_path);
5262 gtk_tree_path_free (new_path);
5267 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5271 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5276 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5277 gboolean group_cycling)
5279 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5281 gtk_widget_grab_focus (combo_box->priv->button);
5287 gtk_combo_box_grab_focus (GtkWidget *widget)
5289 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5291 gtk_widget_grab_focus (combo_box->priv->button);
5295 gtk_combo_box_destroy (GtkObject *object)
5297 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5299 if (combo_box->priv->popup_idle_id > 0)
5301 g_source_remove (combo_box->priv->popup_idle_id);
5302 combo_box->priv->popup_idle_id = 0;
5305 gtk_combo_box_popdown (combo_box);
5307 if (combo_box->priv->row_separator_destroy)
5308 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5310 combo_box->priv->row_separator_func = NULL;
5311 combo_box->priv->row_separator_data = NULL;
5312 combo_box->priv->row_separator_destroy = NULL;
5314 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5315 combo_box->priv->cell_view = NULL;
5319 gtk_combo_box_dispose(GObject* object)
5321 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5323 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5325 gtk_combo_box_menu_destroy (combo_box);
5326 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5327 combo_box->priv->popup_widget = NULL;
5330 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5334 gtk_combo_box_finalize (GObject *object)
5336 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5339 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5340 gtk_combo_box_list_destroy (combo_box);
5342 if (combo_box->priv->popup_window)
5343 gtk_widget_destroy (combo_box->priv->popup_window);
5345 gtk_combo_box_unset_model (combo_box);
5347 for (i = combo_box->priv->cells; i; i = i->next)
5349 ComboCellInfo *info = (ComboCellInfo *)i->data;
5350 GSList *list = info->attributes;
5353 info->destroy (info->func_data);
5355 while (list && list->next)
5357 g_free (list->data);
5358 list = list->next->next;
5360 g_slist_free (info->attributes);
5362 g_object_unref (info->cell);
5363 g_slice_free (ComboCellInfo, info);
5365 g_slist_free (combo_box->priv->cells);
5367 g_free (combo_box->priv->tearoff_title);
5369 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5373 gtk_cell_editable_key_press (GtkWidget *widget,
5377 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5379 if (event->keyval == GDK_Escape)
5381 g_object_set (combo_box,
5382 "editing-canceled", TRUE,
5384 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5385 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5389 else if (event->keyval == GDK_Return ||
5390 event->keyval == GDK_ISO_Enter ||
5391 event->keyval == GDK_KP_Enter)
5393 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5394 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5403 popdown_idle (gpointer data)
5405 GtkComboBox *combo_box;
5407 combo_box = GTK_COMBO_BOX (data);
5409 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5410 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5412 g_object_unref (combo_box);
5418 popdown_handler (GtkWidget *widget,
5421 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5425 popup_idle (gpointer data)
5427 GtkComboBox *combo_box;
5429 combo_box = GTK_COMBO_BOX (data);
5431 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5432 combo_box->priv->cell_view)
5433 g_signal_connect_object (combo_box->priv->popup_widget,
5434 "unmap", G_CALLBACK (popdown_handler),
5437 /* we unset this if a menu item is activated */
5438 g_object_set (combo_box,
5439 "editing-canceled", TRUE,
5441 gtk_combo_box_popup (combo_box);
5443 combo_box->priv->popup_idle_id = 0;
5444 combo_box->priv->activate_button = 0;
5445 combo_box->priv->activate_time = 0;
5451 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5454 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5456 combo_box->priv->is_cell_renderer = TRUE;
5458 if (combo_box->priv->cell_view)
5460 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5461 G_CALLBACK (gtk_cell_editable_key_press),
5464 gtk_widget_grab_focus (combo_box->priv->button);
5468 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5469 G_CALLBACK (gtk_cell_editable_key_press),
5472 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5473 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
5476 /* we do the immediate popup only for the optionmenu-like
5479 if (combo_box->priv->is_cell_renderer &&
5480 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5482 if (event && event->type == GDK_BUTTON_PRESS)
5484 GdkEventButton *event_button = (GdkEventButton *)event;
5486 combo_box->priv->activate_button = event_button->button;
5487 combo_box->priv->activate_time = event_button->time;
5490 combo_box->priv->popup_idle_id =
5491 gdk_threads_add_idle (popup_idle, combo_box);
5497 * gtk_combo_box_get_add_tearoffs:
5498 * @combo_box: a #GtkComboBox
5500 * Gets the current value of the :add-tearoffs property.
5502 * Return value: the current value of the :add-tearoffs property.
5505 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5507 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5509 return combo_box->priv->add_tearoffs;
5513 * gtk_combo_box_set_add_tearoffs:
5514 * @combo_box: a #GtkComboBox
5515 * @add_tearoffs: %TRUE to add tearoff menu items
5517 * Sets whether the popup menu should have a tearoff
5523 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5524 gboolean add_tearoffs)
5526 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5528 add_tearoffs = add_tearoffs != FALSE;
5530 if (combo_box->priv->add_tearoffs != add_tearoffs)
5532 combo_box->priv->add_tearoffs = add_tearoffs;
5533 gtk_combo_box_check_appearance (combo_box);
5534 gtk_combo_box_relayout (combo_box);
5535 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5540 * gtk_combo_box_get_title:
5541 * @combo_box: a #GtkComboBox
5543 * Gets the current title of the menu in tearoff mode. See
5544 * gtk_combo_box_set_add_tearoffs().
5546 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5547 * string which must not be freed.
5551 G_CONST_RETURN gchar*
5552 gtk_combo_box_get_title (GtkComboBox *combo_box)
5554 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5556 return combo_box->priv->tearoff_title;
5560 gtk_combo_box_update_title (GtkComboBox *combo_box)
5562 gtk_combo_box_check_appearance (combo_box);
5564 if (combo_box->priv->popup_widget &&
5565 GTK_IS_MENU (combo_box->priv->popup_widget))
5566 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5567 combo_box->priv->tearoff_title);
5571 * gtk_combo_box_set_title:
5572 * @combo_box: a #GtkComboBox
5573 * @title: a title for the menu in tearoff mode
5575 * Sets the menu's title in tearoff mode.
5580 gtk_combo_box_set_title (GtkComboBox *combo_box,
5583 GtkComboBoxPrivate *priv;
5585 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5587 priv = combo_box->priv;
5589 if (strcmp (title ? title : "",
5590 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5592 g_free (priv->tearoff_title);
5593 priv->tearoff_title = g_strdup (title);
5595 gtk_combo_box_update_title (combo_box);
5597 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5602 * gtk_combo_box_get_popup_accessible:
5603 * @combo_box: a #GtkComboBox
5605 * Gets the accessible object corresponding to the combo box's popup.
5607 * This function is mostly intended for use by accessibility technologies;
5608 * applications should have little use for it.
5610 * Returns: the accessible object corresponding to the combo box's popup.
5615 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5619 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5621 if (combo_box->priv->popup_widget)
5623 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5631 * gtk_combo_box_get_row_separator_func:
5632 * @combo_box: a #GtkComboBox
5634 * Returns the current row separator function.
5636 * Return value: the current row separator function.
5640 GtkTreeViewRowSeparatorFunc
5641 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5643 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5645 return combo_box->priv->row_separator_func;
5649 * gtk_combo_box_set_row_separator_func:
5650 * @combo_box: a #GtkComboBox
5651 * @func: a #GtkTreeViewRowSeparatorFunc
5652 * @data: (allow-none): user data to pass to @func, or %NULL
5653 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5655 * Sets the row separator function, which is used to determine
5656 * whether a row should be drawn as a separator. If the row separator
5657 * function is %NULL, no separators are drawn. This is the default value.
5662 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5663 GtkTreeViewRowSeparatorFunc func,
5665 GDestroyNotify destroy)
5667 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5669 if (combo_box->priv->row_separator_destroy)
5670 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5672 combo_box->priv->row_separator_func = func;
5673 combo_box->priv->row_separator_data = data;
5674 combo_box->priv->row_separator_destroy = destroy;
5676 if (combo_box->priv->tree_view)
5677 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5680 gtk_combo_box_relayout (combo_box);
5682 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5686 * gtk_combo_box_set_button_sensitivity:
5687 * @combo_box: a #GtkComboBox
5688 * @sensitivity: specify the sensitivity of the dropdown button
5690 * Sets whether the dropdown button of the combo box should be
5691 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5692 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5697 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5698 GtkSensitivityType sensitivity)
5700 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5702 if (combo_box->priv->button_sensitivity != sensitivity)
5704 combo_box->priv->button_sensitivity = sensitivity;
5705 gtk_combo_box_update_sensitivity (combo_box);
5707 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5712 * gtk_combo_box_get_button_sensitivity:
5713 * @combo_box: a #GtkComboBox
5715 * Returns whether the combo box sets the dropdown button
5716 * sensitive or not when there are no items in the model.
5718 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5719 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5720 * if the button is always insensitive or
5721 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5722 * the model has one item to be selected.
5727 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5729 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5731 return combo_box->priv->button_sensitivity;
5736 * gtk_combo_box_set_focus_on_click:
5737 * @combo: a #GtkComboBox
5738 * @focus_on_click: whether the combo box grabs focus when clicked
5741 * Sets whether the combo box will grab focus when it is clicked with
5742 * the mouse. Making mouse clicks not grab focus is useful in places
5743 * like toolbars where you don't want the keyboard focus removed from
5744 * the main area of the application.
5749 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5750 gboolean focus_on_click)
5752 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5754 focus_on_click = focus_on_click != FALSE;
5756 if (combo_box->priv->focus_on_click != focus_on_click)
5758 combo_box->priv->focus_on_click = focus_on_click;
5760 if (combo_box->priv->button)
5761 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5764 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5769 * gtk_combo_box_get_focus_on_click:
5770 * @combo: a #GtkComboBox
5772 * Returns whether the combo box grabs focus when it is clicked
5773 * with the mouse. See gtk_combo_box_set_focus_on_click().
5775 * Return value: %TRUE if the combo box grabs focus when it is
5776 * clicked with the mouse.
5781 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5783 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5785 return combo_box->priv->focus_on_click;
5790 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5791 GtkBuilder *builder,
5793 const gchar *tagname,
5794 GMarkupParser *parser,
5797 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5798 tagname, parser, data))
5801 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5802 tagname, parser, data);
5806 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5807 GtkBuilder *builder,
5809 const gchar *tagname,
5812 if (strcmp (tagname, "attributes") == 0)
5813 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5816 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5822 gtk_combo_box_extended_layout_init (GtkExtendedLayoutIface *iface)
5824 iface->get_desired_width = gtk_combo_box_get_desired_width;
5825 iface->get_desired_height = gtk_combo_box_get_desired_height;
5829 gtk_combo_box_remeasure (GtkComboBox *combo_box)
5831 GtkComboBoxPrivate *priv = combo_box->priv;
5836 !gtk_tree_model_get_iter_first (priv->model, &iter))
5839 memset (&priv->minimum_size, 0x0, sizeof (GtkRequisition));
5840 memset (&priv->natural_size, 0x0, sizeof (GtkRequisition));
5842 path = gtk_tree_path_new_from_indices (0, -1);
5846 GtkRequisition req, nat_req;
5848 if (priv->cell_view)
5850 /* XXX FIXME: Currently still not doing height-for-width in cell renderers here */
5851 gtk_cell_view_get_desired_width_of_row (GTK_CELL_VIEW (priv->cell_view),
5852 path, &req.width, &nat_req.width);
5853 gtk_cell_view_get_desired_height_of_row (GTK_CELL_VIEW (priv->cell_view),
5854 path, &req.height, &nat_req.height);
5858 memset (&req, 0x0, sizeof (GtkRequisition));
5859 memset (&nat_req, 0x0, sizeof (GtkRequisition));
5862 priv->minimum_size.width = MAX (priv->minimum_size.width, req.width);
5863 priv->minimum_size.height = MAX (priv->minimum_size.height, req.height);
5865 priv->natural_size.width = MAX (priv->natural_size.width, nat_req.width);
5866 priv->natural_size.height = MAX (priv->natural_size.height, nat_req.height);
5868 gtk_tree_path_next (path);
5870 while (gtk_tree_model_iter_next (priv->model, &iter));
5872 gtk_tree_path_free (path);
5876 /* XXX TODO: Split this up into 2 orientations so as
5877 * to properly support height-for-width/width-for-height here
5881 gtk_combo_box_get_desired_size (GtkExtendedLayout *layout,
5882 GtkRequisition *minimum_size,
5883 GtkRequisition *natural_size)
5885 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
5886 GtkComboBoxPrivate *priv = combo_box->priv;
5887 gint focus_width, focus_pad;
5888 gint font_size, arrow_size;
5889 GtkRequisition bin_req, bin_nat_req;
5890 PangoContext *context;
5891 PangoFontMetrics *metrics;
5892 PangoFontDescription *font_desc;
5895 child = gtk_bin_get_child (GTK_BIN (layout));
5898 gtk_extended_layout_get_desired_size (GTK_EXTENDED_LAYOUT (child), &bin_req, &bin_nat_req);
5899 gtk_combo_box_remeasure (combo_box);
5901 bin_req.width = MAX (bin_req.width, priv->minimum_size.width);
5902 bin_req.height = MAX (bin_req.height, priv->minimum_size.height);
5903 bin_nat_req.width = MAX (bin_nat_req.width, priv->natural_size.width);
5904 bin_nat_req.height = MAX (bin_nat_req.height, priv->natural_size.height);
5906 gtk_widget_style_get (GTK_WIDGET (layout),
5907 "focus-line-width", &focus_width,
5908 "focus-padding", &focus_pad,
5909 "arrow-size", &arrow_size,
5912 font_desc = child->style->font_desc;
5913 context = gtk_widget_get_pango_context (GTK_WIDGET (layout));
5914 metrics = pango_context_get_metrics (context, font_desc,
5915 pango_context_get_language (context));
5916 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5917 pango_font_metrics_get_descent (metrics));
5918 pango_font_metrics_unref (metrics);
5920 arrow_size = MAX (arrow_size, font_size);
5922 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5924 if (!priv->tree_view)
5928 if (priv->cell_view)
5930 GtkRequisition button_req, sep_req, arrow_req;
5931 gint border_width, xthickness, ythickness, xpad, ypad;
5933 gtk_widget_size_request (priv->button, &button_req);
5934 border_width = GTK_CONTAINER (combo_box)->border_width;
5935 xthickness = priv->button->style->xthickness;
5936 ythickness = priv->button->style->ythickness;
5938 xpad = 2*(border_width + xthickness + focus_width + focus_pad);
5939 ypad = 2*(border_width + ythickness + focus_width + focus_pad);
5941 gtk_widget_size_request (priv->separator, &sep_req);
5942 gtk_widget_size_request (priv->arrow, &arrow_req);
5944 minimum_size->width = bin_req.width + sep_req.width + arrow_req.width;
5945 minimum_size->height = MAX (sep_req.height, arrow_req.height);
5946 minimum_size->height = MAX (minimum_size->height, bin_req.height);
5948 natural_size->width = bin_nat_req.width + sep_req.width + arrow_req.width;
5949 natural_size->height = MAX (minimum_size->height, bin_nat_req.height);
5951 minimum_size->width += xpad;
5952 minimum_size->height += ypad;
5953 natural_size->width += xpad;
5954 natural_size->height += ypad;
5958 GtkRequisition but_req, but_nat_req;
5960 gtk_extended_layout_get_desired_size (GTK_EXTENDED_LAYOUT (priv->button),
5961 &but_req, &but_nat_req);
5963 minimum_size->width = bin_req.width + but_req.width;
5964 minimum_size->height = MAX (bin_req.height, but_req.height);
5966 natural_size->width = bin_nat_req.width + but_nat_req.width;
5967 natural_size->height = MAX (bin_nat_req.height, but_nat_req.height);
5973 GtkRequisition button_req, button_nat_req, frame_req;
5975 /* sample + frame */
5976 *minimum_size = bin_req;
5977 *natural_size = bin_nat_req;
5979 minimum_size->width += 2 * focus_width;
5980 natural_size->width += 2 * focus_width;
5982 if (priv->cell_view_frame)
5984 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
5985 if (priv->has_frame)
5987 gint xpad = 2 * (GTK_CONTAINER (priv->cell_view_frame)->border_width +
5988 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
5990 gint ypad = 2 * (GTK_CONTAINER (priv->cell_view_frame)->border_width +
5991 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
5993 minimum_size->width += xpad;
5994 minimum_size->height += ypad;
5995 natural_size->width += xpad;
5996 natural_size->height += ypad;
6001 gtk_extended_layout_get_desired_size (GTK_EXTENDED_LAYOUT (priv->button),
6002 &button_req, &button_nat_req);
6004 minimum_size->width += button_req.width;
6005 minimum_size->height = MAX (minimum_size->height, button_req.height);
6007 natural_size->width += button_nat_req.width;
6008 natural_size->height = MAX (natural_size->height, button_nat_req.height);
6011 if (GTK_SHADOW_NONE != priv->shadow_type)
6013 minimum_size->width += 2 * GTK_WIDGET (layout)->style->xthickness;
6014 minimum_size->height += 2 * GTK_WIDGET (layout)->style->ythickness;
6016 natural_size->width += 2 * GTK_WIDGET (layout)->style->xthickness;
6017 natural_size->height += 2 * GTK_WIDGET (layout)->style->ythickness;
6022 gtk_combo_box_get_desired_width (GtkExtendedLayout *layout,
6026 GtkRequisition minimum, natural;
6028 gtk_combo_box_get_desired_size (layout, &minimum, &natural);
6031 *minimum_size = minimum.width;
6034 *natural_size = natural.width;
6038 gtk_combo_box_get_desired_height (GtkExtendedLayout *layout,
6042 GtkRequisition minimum, natural;
6044 gtk_combo_box_get_desired_size (layout, &minimum, &natural);
6047 *minimum_size = minimum.height;
6050 *natural_size = natural.height;
6054 #define __GTK_COMBO_BOX_C__
6055 #include "gtkaliasdef.c"