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"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
32 #include "gtkliststore.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkseparator.h"
42 #include "gtkwindow.h"
43 #include "gtktypebuiltins.h"
44 #include "gtkprivate.h"
46 #include <gobject/gvaluecollector.h>
51 #include "gtkmarshalers.h"
54 #include "gtkentryprivate.h"
55 #include "gtktreeprivate.h"
56 #include "a11y/gtkcomboboxaccessible.h"
61 * @Short_description: A widget used to choose from a list of items
63 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
65 * A GtkComboBox is a widget that allows the user to choose from a list of
66 * valid choices. The GtkComboBox displays the selected choice. When
67 * activated, the GtkComboBox displays a popup which allows the user to
68 * make a new choice. The style in which the selected value is displayed,
69 * and the style of the popup is determined by the current theme. It may
70 * be similar to a Windows-style combo box.
72 * The GtkComboBox uses the model-view pattern; the list of valid choices
73 * is specified in the form of a tree model, and the display of the choices
74 * can be adapted to the data in the model by using cell renderers, as you
75 * would in a tree view. This is possible since GtkComboBox implements the
76 * #GtkCellLayout interface. The tree model holding the valid choices is
77 * not restricted to a flat list, it can be a real tree, and the popup will
78 * reflect the tree structure.
80 * To allow the user to enter values not in the model, the 'has-entry'
81 * property allows the GtkComboBox to contain a #GtkEntry. This entry
82 * can be accessed by calling gtk_bin_get_child() on the combo box.
84 * For a simple list of textual choices, the model-view API of GtkComboBox
85 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
91 /* WELCOME, to THE house of evil code */
92 struct _GtkComboBoxPrivate
102 GtkShadowType shadow_type;
104 gint active; /* Only temporary */
105 GtkTreeRowReference *active_row;
107 GtkWidget *tree_view;
109 GtkWidget *cell_view;
110 GtkWidget *cell_view_frame;
115 GtkWidget *separator;
117 GtkWidget *popup_widget;
118 GtkWidget *popup_window;
119 GtkWidget *scrolled_window;
126 guint activate_button;
127 guint32 activate_time;
129 guint resize_idle_id;
131 /* For "has-entry" specific behavior we track
132 * an automated cell renderer and text column
135 GtkCellRenderer *text_renderer;
139 guint popup_in_progress : 1;
140 guint popup_shown : 1;
141 guint add_tearoffs : 1;
143 guint is_cell_renderer : 1;
144 guint editing_canceled : 1;
145 guint auto_scroll : 1;
146 guint focus_on_click : 1;
147 guint button_sensitivity : 2;
149 guint popup_fixed_width : 1;
151 GtkTreeViewRowSeparatorFunc row_separator_func;
152 gpointer row_separator_data;
153 GDestroyNotify row_separator_destroy;
155 GdkDevice *grab_pointer;
156 GdkDevice *grab_keyboard;
158 gchar *tearoff_title;
161 /* While debugging this evil code, I have learned that
162 * there are actually 4 modes to this widget, which can
163 * be characterized as follows
165 * 1) menu mode, no child added
168 * cell_view -> GtkCellView, regular child
169 * cell_view_frame -> NULL
170 * button -> GtkToggleButton set_parent to combo
171 * arrow -> GtkArrow set_parent to button
172 * separator -> GtkVSepator set_parent to button
173 * popup_widget -> GtkMenu
174 * popup_window -> NULL
175 * scrolled_window -> NULL
177 * 2) menu mode, child added
181 * cell_view_frame -> NULL
182 * button -> GtkToggleButton set_parent to combo
183 * arrow -> GtkArrow, child of button
185 * popup_widget -> GtkMenu
186 * popup_window -> NULL
187 * scrolled_window -> NULL
189 * 3) list mode, no child added
191 * tree_view -> GtkTreeView, child of scrolled_window
192 * cell_view -> GtkCellView, regular child
193 * cell_view_frame -> GtkFrame, set parent to combo
194 * button -> GtkToggleButton, set_parent to combo
195 * arrow -> GtkArrow, child of button
197 * popup_widget -> tree_view
198 * popup_window -> GtkWindow
199 * scrolled_window -> GtkScrolledWindow, child of popup_window
201 * 4) list mode, child added
203 * tree_view -> GtkTreeView, child of scrolled_window
205 * cell_view_frame -> NULL
206 * button -> GtkToggleButton, set_parent to combo
207 * arrow -> GtkArrow, child of button
209 * popup_widget -> tree_view
210 * popup_window -> GtkWindow
211 * scrolled_window -> GtkScrolledWindow, child of popup_window
227 PROP_ROW_SPAN_COLUMN,
228 PROP_COLUMN_SPAN_COLUMN,
235 PROP_BUTTON_SENSITIVITY,
236 PROP_EDITING_CANCELED,
238 PROP_ENTRY_TEXT_COLUMN,
239 PROP_POPUP_FIXED_WIDTH,
245 static guint combo_box_signals[LAST_SIGNAL] = {0,};
247 #define BONUS_PADDING 4
248 #define SCROLL_TIME 100
252 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
253 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
254 static GObject *gtk_combo_box_constructor (GType type,
255 guint n_construct_properties,
256 GObjectConstructParam *construct_properties);
257 static void gtk_combo_box_dispose (GObject *object);
258 static void gtk_combo_box_finalize (GObject *object);
259 static void gtk_combo_box_destroy (GtkWidget *widget);
261 static void gtk_combo_box_set_property (GObject *object,
265 static void gtk_combo_box_get_property (GObject *object,
270 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
271 GtkStateFlags previous);
272 static void gtk_combo_box_grab_focus (GtkWidget *widget);
273 static void gtk_combo_box_style_updated (GtkWidget *widget);
274 static void gtk_combo_box_button_toggled (GtkWidget *widget,
276 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
277 GtkStateFlags previous,
279 static void gtk_combo_box_add (GtkContainer *container,
281 static void gtk_combo_box_remove (GtkContainer *container,
284 static void gtk_combo_box_menu_show (GtkWidget *menu,
286 static void gtk_combo_box_menu_hide (GtkWidget *menu,
289 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
291 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
296 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
301 static void gtk_combo_box_menu_position (GtkMenu *menu,
307 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
309 static void gtk_combo_box_size_allocate (GtkWidget *widget,
310 GtkAllocation *allocation);
311 static void gtk_combo_box_forall (GtkContainer *container,
312 gboolean include_internals,
313 GtkCallback callback,
314 gpointer callback_data);
315 static gboolean gtk_combo_box_draw (GtkWidget *widget,
317 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
318 GdkEventScroll *event);
319 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
322 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
323 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
324 GtkScrollType scroll);
325 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
326 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
328 /* listening to the model */
329 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
333 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
336 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
341 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
345 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
351 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
356 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
357 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
359 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
360 GdkEventButton *event,
362 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
365 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
366 GdkEventCrossing *event,
368 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
371 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
372 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
373 GdkEventButton *event,
376 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
379 gboolean path_currently_selected,
382 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
386 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
389 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
390 gboolean add_children);
391 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
392 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
395 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
396 GdkEventButton *event,
398 static void gtk_combo_box_menu_activate (GtkWidget *menu,
400 GtkComboBox *combo_box);
401 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
402 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
405 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
407 guint32 activate_time);
410 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
412 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
413 gboolean group_cycling);
415 static void gtk_combo_box_child_show (GtkWidget *widget,
416 GtkComboBox *combo_box);
417 static void gtk_combo_box_child_hide (GtkWidget *widget,
418 GtkComboBox *combo_box);
420 /* GtkComboBox:has-entry callbacks */
421 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
423 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
427 /* GtkBuildable method implementation */
428 static GtkBuildableIface *parent_buildable_iface;
430 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
431 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
434 const gchar *tagname,
435 GMarkupParser *parser,
437 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
440 const gchar *tagname,
442 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
444 const gchar *childname);
447 /* GtkCellEditable method implementations */
448 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
451 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
454 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
457 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
461 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
467 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
468 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
469 gtk_combo_box_cell_layout_init)
470 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
471 gtk_combo_box_cell_editable_init)
472 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
473 gtk_combo_box_buildable_init))
478 gtk_combo_box_class_init (GtkComboBoxClass *klass)
480 GObjectClass *object_class;
481 GtkContainerClass *container_class;
482 GtkWidgetClass *widget_class;
483 GtkBindingSet *binding_set;
485 container_class = (GtkContainerClass *)klass;
486 container_class->forall = gtk_combo_box_forall;
487 container_class->add = gtk_combo_box_add;
488 container_class->remove = gtk_combo_box_remove;
490 widget_class = (GtkWidgetClass *)klass;
491 widget_class->size_allocate = gtk_combo_box_size_allocate;
492 widget_class->draw = gtk_combo_box_draw;
493 widget_class->scroll_event = gtk_combo_box_scroll_event;
494 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
495 widget_class->grab_focus = gtk_combo_box_grab_focus;
496 widget_class->style_updated = gtk_combo_box_style_updated;
497 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
498 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
499 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
500 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
501 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
502 widget_class->destroy = gtk_combo_box_destroy;
504 object_class = (GObjectClass *)klass;
505 object_class->constructor = gtk_combo_box_constructor;
506 object_class->dispose = gtk_combo_box_dispose;
507 object_class->finalize = gtk_combo_box_finalize;
508 object_class->set_property = gtk_combo_box_set_property;
509 object_class->get_property = gtk_combo_box_get_property;
513 * GtkComboBox::changed:
514 * @widget: the object which received the signal
516 * The changed signal is emitted when the active
517 * item is changed. The can be due to the user selecting
518 * a different item from the list, or due to a
519 * call to gtk_combo_box_set_active_iter().
520 * It will also be emitted while typing into the entry of a combo box
525 combo_box_signals[CHANGED] =
526 g_signal_new (I_("changed"),
527 G_OBJECT_CLASS_TYPE (klass),
529 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
531 g_cclosure_marshal_VOID__VOID,
534 * GtkComboBox::move-active:
535 * @widget: the object that received the signal
536 * @scroll_type: a #GtkScrollType
538 * The ::move-active signal is a
539 * <link linkend="keybinding-signals">keybinding signal</link>
540 * which gets emitted to move the active selection.
544 combo_box_signals[MOVE_ACTIVE] =
545 g_signal_new_class_handler (I_("move-active"),
546 G_OBJECT_CLASS_TYPE (klass),
547 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
548 G_CALLBACK (gtk_combo_box_real_move_active),
550 g_cclosure_marshal_VOID__ENUM,
552 GTK_TYPE_SCROLL_TYPE);
555 * GtkComboBox::popup:
556 * @widget: the object that received the signal
558 * The ::popup signal is a
559 * <link linkend="keybinding-signals">keybinding signal</link>
560 * which gets emitted to popup the combo box list.
562 * The default binding for this signal is Alt+Down.
566 combo_box_signals[POPUP] =
567 g_signal_new_class_handler (I_("popup"),
568 G_OBJECT_CLASS_TYPE (klass),
569 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
570 G_CALLBACK (gtk_combo_box_real_popup),
572 g_cclosure_marshal_VOID__VOID,
575 * GtkComboBox::popdown:
576 * @button: the object which received the signal
578 * The ::popdown signal is a
579 * <link linkend="keybinding-signals">keybinding signal</link>
580 * which gets emitted to popdown the combo box list.
582 * The default bindings for this signal are Alt+Up and Escape.
586 combo_box_signals[POPDOWN] =
587 g_signal_new_class_handler (I_("popdown"),
588 G_OBJECT_CLASS_TYPE (klass),
589 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
590 G_CALLBACK (gtk_combo_box_real_popdown),
592 _gtk_marshal_BOOLEAN__VOID,
596 binding_set = gtk_binding_set_by_class (widget_class);
598 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
600 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
603 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
607 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
610 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
612 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
613 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
615 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
616 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
618 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
619 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
621 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
622 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
624 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
625 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
627 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
629 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
631 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
632 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
634 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
635 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
637 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
638 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
640 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
641 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
643 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
644 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
646 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
649 g_object_class_override_property (object_class,
650 PROP_EDITING_CANCELED,
656 * The model from which the combo box takes the values shown
661 g_object_class_install_property (object_class,
663 g_param_spec_object ("model",
664 P_("ComboBox model"),
665 P_("The model for the combo box"),
667 GTK_PARAM_READWRITE));
670 * GtkComboBox:wrap-width:
672 * If wrap-width is set to a positive value, the list will be
673 * displayed in multiple columns, the number of columns is
674 * determined by wrap-width.
678 g_object_class_install_property (object_class,
680 g_param_spec_int ("wrap-width",
682 P_("Wrap width for laying out the items in a grid"),
686 GTK_PARAM_READWRITE));
690 * GtkComboBox:row-span-column:
692 * If this is set to a non-negative value, it must be the index of a column
693 * of type %G_TYPE_INT in the model.
695 * The values of that column are used to determine how many rows a value in
696 * the list will span. Therefore, the values in the model column pointed to
697 * by this property must be greater than zero and not larger than wrap-width.
701 g_object_class_install_property (object_class,
702 PROP_ROW_SPAN_COLUMN,
703 g_param_spec_int ("row-span-column",
704 P_("Row span column"),
705 P_("TreeModel column containing the row span values"),
709 GTK_PARAM_READWRITE));
713 * GtkComboBox:column-span-column:
715 * If this is set to a non-negative value, it must be the index of a column
716 * of type %G_TYPE_INT in the model.
718 * The values of that column are used to determine how many columns a value
719 * in the list will span.
723 g_object_class_install_property (object_class,
724 PROP_COLUMN_SPAN_COLUMN,
725 g_param_spec_int ("column-span-column",
726 P_("Column span column"),
727 P_("TreeModel column containing the column span values"),
731 GTK_PARAM_READWRITE));
735 * GtkComboBox:active:
737 * The item which is currently active. If the model is a non-flat treemodel,
738 * and the active item is not an immediate child of the root of the tree,
739 * this property has the value
740 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
741 * where <literal>path</literal> is the #GtkTreePath of the active item.
745 g_object_class_install_property (object_class,
747 g_param_spec_int ("active",
749 P_("The item which is currently active"),
753 GTK_PARAM_READWRITE));
756 * GtkComboBox:add-tearoffs:
758 * The add-tearoffs property controls whether generated menus
759 * have tearoff menu items.
761 * Note that this only affects menu style combo boxes.
765 g_object_class_install_property (object_class,
767 g_param_spec_boolean ("add-tearoffs",
768 P_("Add tearoffs to menus"),
769 P_("Whether dropdowns should have a tearoff menu item"),
771 GTK_PARAM_READWRITE));
774 * GtkComboBox:has-frame:
776 * The has-frame property controls whether a frame
777 * is drawn around the entry.
781 g_object_class_install_property (object_class,
783 g_param_spec_boolean ("has-frame",
785 P_("Whether the combo box draws a frame around the child"),
787 GTK_PARAM_READWRITE));
789 g_object_class_install_property (object_class,
791 g_param_spec_boolean ("focus-on-click",
792 P_("Focus on click"),
793 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
795 GTK_PARAM_READWRITE));
798 * GtkComboBox:tearoff-title:
800 * A title that may be displayed by the window manager
801 * when the popup is torn-off.
805 g_object_class_install_property (object_class,
807 g_param_spec_string ("tearoff-title",
809 P_("A title that may be displayed by the window manager when the popup is torn-off"),
811 GTK_PARAM_READWRITE));
815 * GtkComboBox:popup-shown:
817 * Whether the combo boxes dropdown is popped up.
818 * Note that this property is mainly useful, because
819 * it allows you to connect to notify::popup-shown.
823 g_object_class_install_property (object_class,
825 g_param_spec_boolean ("popup-shown",
827 P_("Whether the combo's dropdown is shown"),
829 GTK_PARAM_READABLE));
833 * GtkComboBox:button-sensitivity:
835 * Whether the dropdown button is sensitive when
836 * the model is empty.
840 g_object_class_install_property (object_class,
841 PROP_BUTTON_SENSITIVITY,
842 g_param_spec_enum ("button-sensitivity",
843 P_("Button Sensitivity"),
844 P_("Whether the dropdown button is sensitive when the model is empty"),
845 GTK_TYPE_SENSITIVITY_TYPE,
846 GTK_SENSITIVITY_AUTO,
847 GTK_PARAM_READWRITE));
850 * GtkComboBox:has-entry:
852 * Whether the combo box has an entry.
856 g_object_class_install_property (object_class,
858 g_param_spec_boolean ("has-entry",
860 P_("Whether combo box has an entry"),
862 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
865 * GtkComboBox:entry-text-column:
867 * The column in the combo box's model to associate with strings from the entry
868 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
872 g_object_class_install_property (object_class,
873 PROP_ENTRY_TEXT_COLUMN,
874 g_param_spec_int ("entry-text-column",
875 P_("Entry Text Column"),
876 P_("The column in the combo box's model to associate "
877 "with strings from the entry if the combo was "
878 "created with #GtkComboBox:has-entry = %TRUE"),
880 GTK_PARAM_READWRITE));
883 * GtkComboBox:id-column:
885 * The column in the combo box's model that provides string
886 * IDs for the values in the model, if != -1.
890 g_object_class_install_property (object_class,
892 g_param_spec_int ("id-column",
894 P_("The column in the combo box's model that provides "
895 "string IDs for the values in the model"),
897 GTK_PARAM_READWRITE));
900 * GtkComboBox:active-id:
902 * The value of the ID column of the active row.
906 g_object_class_install_property (object_class,
908 g_param_spec_string ("active-id",
910 P_("The value of the id column "
911 "for the active row"),
912 NULL, GTK_PARAM_READWRITE));
915 * GtkComboBox:popup-fixed-width:
917 * Whether the popup's width should be a fixed width matching the
918 * allocated width of the combo box.
922 g_object_class_install_property (object_class,
923 PROP_POPUP_FIXED_WIDTH,
924 g_param_spec_boolean ("popup-fixed-width",
925 P_("Popup Fixed Width"),
926 P_("Whether the popup's width should be a "
927 "fixed width matching the allocated width "
930 GTK_PARAM_READWRITE));
933 * GtkComboBox:cell-area:
935 * The #GtkCellArea used to layout cell renderers for this combo box.
937 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
938 * a horizontally oriented #GtkCellAreaBox will be used.
942 g_object_class_install_property (object_class,
944 g_param_spec_object ("cell-area",
946 P_("The GtkCellArea used to layout cells"),
948 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
950 gtk_widget_class_install_style_property (widget_class,
951 g_param_spec_boolean ("appears-as-list",
952 P_("Appears as list"),
953 P_("Whether dropdowns should look like lists rather than menus"),
955 GTK_PARAM_READABLE));
958 * GtkComboBox:arrow-size:
960 * Sets the minimum size of the arrow in the combo box. Note
961 * that the arrow size is coupled to the font size, so in case
962 * a larger font is used, the arrow will be larger than set
967 gtk_widget_class_install_style_property (widget_class,
968 g_param_spec_int ("arrow-size",
970 P_("The minimum size of the arrow in the combo box"),
974 GTK_PARAM_READABLE));
977 * GtkComboBox:arrow-scaling:
979 * Sets the amount of space used up by the combobox arrow,
980 * proportional to the font size.
984 gtk_widget_class_install_style_property (widget_class,
985 g_param_spec_float ("arrow-scaling",
987 P_("The amount of space used by the arrow"),
991 GTK_PARAM_READABLE));
994 * GtkComboBox:shadow-type:
996 * Which kind of shadow to draw around the combo box.
1000 gtk_widget_class_install_style_property (widget_class,
1001 g_param_spec_enum ("shadow-type",
1003 P_("Which kind of shadow to draw around the combo box"),
1004 GTK_TYPE_SHADOW_TYPE,
1006 GTK_PARAM_READABLE));
1008 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1010 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1014 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1016 parent_buildable_iface = g_type_interface_peek_parent (iface);
1017 iface->add_child = _gtk_cell_layout_buildable_add_child;
1018 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1019 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1020 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1024 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1026 iface->get_area = gtk_combo_box_cell_layout_get_area;
1030 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1032 iface->start_editing = gtk_combo_box_start_editing;
1036 gtk_combo_box_init (GtkComboBox *combo_box)
1038 GtkComboBoxPrivate *priv;
1040 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1042 GtkComboBoxPrivate);
1043 priv = combo_box->priv;
1045 priv->wrap_width = 0;
1048 priv->active_row = NULL;
1049 priv->col_column = -1;
1050 priv->row_column = -1;
1052 priv->popup_shown = FALSE;
1053 priv->add_tearoffs = FALSE;
1054 priv->has_frame = TRUE;
1055 priv->is_cell_renderer = FALSE;
1056 priv->editing_canceled = FALSE;
1057 priv->auto_scroll = FALSE;
1058 priv->focus_on_click = TRUE;
1059 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1060 priv->has_entry = FALSE;
1061 priv->popup_fixed_width = TRUE;
1063 priv->text_column = -1;
1064 priv->text_renderer = NULL;
1065 priv->id_column = -1;
1069 gtk_combo_box_set_property (GObject *object,
1071 const GValue *value,
1074 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1075 GtkComboBoxPrivate *priv = combo_box->priv;
1081 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1084 case PROP_WRAP_WIDTH:
1085 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1088 case PROP_ROW_SPAN_COLUMN:
1089 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1092 case PROP_COLUMN_SPAN_COLUMN:
1093 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1097 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1100 case PROP_ADD_TEAROFFS:
1101 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1104 case PROP_HAS_FRAME:
1105 priv->has_frame = g_value_get_boolean (value);
1107 if (priv->has_entry)
1111 child = gtk_bin_get_child (GTK_BIN (combo_box));
1113 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1118 case PROP_FOCUS_ON_CLICK:
1119 gtk_combo_box_set_focus_on_click (combo_box,
1120 g_value_get_boolean (value));
1123 case PROP_TEAROFF_TITLE:
1124 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1127 case PROP_POPUP_SHOWN:
1128 if (g_value_get_boolean (value))
1129 gtk_combo_box_popup (combo_box);
1131 gtk_combo_box_popdown (combo_box);
1134 case PROP_BUTTON_SENSITIVITY:
1135 gtk_combo_box_set_button_sensitivity (combo_box,
1136 g_value_get_enum (value));
1139 case PROP_POPUP_FIXED_WIDTH:
1140 gtk_combo_box_set_popup_fixed_width (combo_box,
1141 g_value_get_boolean (value));
1144 case PROP_EDITING_CANCELED:
1145 priv->editing_canceled = g_value_get_boolean (value);
1148 case PROP_HAS_ENTRY:
1149 priv->has_entry = g_value_get_boolean (value);
1152 case PROP_ENTRY_TEXT_COLUMN:
1153 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1156 case PROP_ID_COLUMN:
1157 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1160 case PROP_ACTIVE_ID:
1161 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1164 case PROP_CELL_AREA:
1165 /* Construct-only, can only be assigned once */
1166 area = g_value_get_object (value);
1169 if (priv->area != NULL)
1171 g_warning ("cell-area has already been set, ignoring construct property");
1172 g_object_ref_sink (area);
1173 g_object_unref (area);
1176 priv->area = g_object_ref_sink (area);
1181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1187 gtk_combo_box_get_property (GObject *object,
1192 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1193 GtkComboBoxPrivate *priv = combo_box->priv;
1198 g_value_set_object (value, combo_box->priv->model);
1201 case PROP_WRAP_WIDTH:
1202 g_value_set_int (value, combo_box->priv->wrap_width);
1205 case PROP_ROW_SPAN_COLUMN:
1206 g_value_set_int (value, combo_box->priv->row_column);
1209 case PROP_COLUMN_SPAN_COLUMN:
1210 g_value_set_int (value, combo_box->priv->col_column);
1214 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1217 case PROP_ADD_TEAROFFS:
1218 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1221 case PROP_HAS_FRAME:
1222 g_value_set_boolean (value, combo_box->priv->has_frame);
1225 case PROP_FOCUS_ON_CLICK:
1226 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1229 case PROP_TEAROFF_TITLE:
1230 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1233 case PROP_POPUP_SHOWN:
1234 g_value_set_boolean (value, combo_box->priv->popup_shown);
1237 case PROP_BUTTON_SENSITIVITY:
1238 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1241 case PROP_POPUP_FIXED_WIDTH:
1242 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1245 case PROP_EDITING_CANCELED:
1246 g_value_set_boolean (value, priv->editing_canceled);
1249 case PROP_HAS_ENTRY:
1250 g_value_set_boolean (value, priv->has_entry);
1253 case PROP_ENTRY_TEXT_COLUMN:
1254 g_value_set_int (value, priv->text_column);
1257 case PROP_ID_COLUMN:
1258 g_value_set_int (value, priv->id_column);
1261 case PROP_ACTIVE_ID:
1262 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1265 case PROP_CELL_AREA:
1266 g_value_set_object (value, priv->area);
1270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1276 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1277 GtkStateFlags previous)
1279 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1280 GtkComboBoxPrivate *priv = combo_box->priv;
1282 if (gtk_widget_get_realized (widget))
1284 if (priv->tree_view && priv->cell_view)
1286 GtkStyleContext *context;
1287 GtkStateFlags state;
1290 context = gtk_widget_get_style_context (widget);
1291 state = gtk_widget_get_state_flags (widget);
1292 gtk_style_context_get_background_color (context, state, &color);
1294 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1298 gtk_widget_queue_draw (widget);
1302 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1303 GtkStateFlags previous,
1306 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1307 GtkComboBoxPrivate *priv = combo_box->priv;
1309 if (gtk_widget_get_realized (widget))
1311 if (!priv->tree_view && priv->cell_view)
1312 gtk_widget_set_state_flags (priv->cell_view,
1313 gtk_widget_get_state_flags (widget),
1317 gtk_widget_queue_draw (widget);
1321 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1323 GtkComboBoxPrivate *priv = combo_box->priv;
1324 gboolean appears_as_list;
1326 /* if wrap_width > 0, then we are in grid-mode and forced to use
1329 if (priv->wrap_width)
1330 appears_as_list = FALSE;
1332 gtk_widget_style_get (GTK_WIDGET (combo_box),
1333 "appears-as-list", &appears_as_list,
1336 if (appears_as_list)
1338 /* Destroy all the menu mode widgets, if they exist. */
1339 if (GTK_IS_MENU (priv->popup_widget))
1340 gtk_combo_box_menu_destroy (combo_box);
1342 /* Create the list mode widgets, if they don't already exist. */
1343 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1344 gtk_combo_box_list_setup (combo_box);
1348 /* Destroy all the list mode widgets, if they exist. */
1349 if (GTK_IS_TREE_VIEW (priv->tree_view))
1350 gtk_combo_box_list_destroy (combo_box);
1352 /* Create the menu mode widgets, if they don't already exist. */
1353 if (!GTK_IS_MENU (priv->popup_widget))
1354 gtk_combo_box_menu_setup (combo_box, TRUE);
1357 gtk_widget_style_get (GTK_WIDGET (combo_box),
1358 "shadow-type", &priv->shadow_type,
1363 gtk_combo_box_style_updated (GtkWidget *widget)
1365 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1366 GtkComboBoxPrivate *priv = combo_box->priv;
1369 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1371 gtk_combo_box_check_appearance (combo_box);
1373 if (priv->tree_view && priv->cell_view)
1375 GtkStyleContext *context;
1376 GtkStateFlags state;
1379 context = gtk_widget_get_style_context (widget);
1380 state = gtk_widget_get_state_flags (widget);
1381 gtk_style_context_get_background_color (context, state, &color);
1383 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1386 child = gtk_bin_get_child (GTK_BIN (combo_box));
1387 if (GTK_IS_ENTRY (child))
1388 g_object_set (child, "shadow-type",
1389 GTK_SHADOW_NONE == priv->shadow_type ?
1390 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1394 gtk_combo_box_button_toggled (GtkWidget *widget,
1397 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1399 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1401 if (!combo_box->priv->popup_in_progress)
1402 gtk_combo_box_popup (combo_box);
1405 gtk_combo_box_popdown (combo_box);
1409 gtk_combo_box_add (GtkContainer *container,
1412 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1413 GtkComboBoxPrivate *priv = combo_box->priv;
1415 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1417 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1418 "(need an instance of GtkEntry or of a subclass)",
1419 G_OBJECT_TYPE_NAME (widget));
1423 if (priv->cell_view &&
1424 gtk_widget_get_parent (priv->cell_view))
1426 gtk_widget_unparent (priv->cell_view);
1427 _gtk_bin_set_child (GTK_BIN (container), NULL);
1428 gtk_widget_queue_resize (GTK_WIDGET (container));
1431 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1432 _gtk_bin_set_child (GTK_BIN (container), widget);
1434 if (priv->cell_view &&
1435 widget != priv->cell_view)
1437 /* since the cell_view was unparented, it's gone now */
1438 priv->cell_view = NULL;
1440 if (!priv->tree_view && priv->separator)
1442 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1444 priv->separator = NULL;
1446 gtk_widget_queue_resize (GTK_WIDGET (container));
1448 else if (priv->cell_view_frame)
1450 gtk_widget_unparent (priv->cell_view_frame);
1451 priv->cell_view_frame = NULL;
1456 if (priv->has_entry)
1458 /* this flag is a hack to tell the entry to fill its allocation.
1460 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1462 g_signal_connect (widget, "changed",
1463 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1466 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1471 gtk_combo_box_remove (GtkContainer *container,
1474 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1475 GtkComboBoxPrivate *priv = combo_box->priv;
1477 gboolean appears_as_list;
1479 if (priv->has_entry)
1481 GtkWidget *child_widget;
1483 child_widget = gtk_bin_get_child (GTK_BIN (container));
1484 if (widget && widget == child_widget)
1486 g_signal_handlers_disconnect_by_func (widget,
1487 gtk_combo_box_entry_contents_changed,
1489 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1493 if (widget == priv->cell_view)
1494 priv->cell_view = NULL;
1496 gtk_widget_unparent (widget);
1497 _gtk_bin_set_child (GTK_BIN (container), NULL);
1499 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1502 gtk_widget_queue_resize (GTK_WIDGET (container));
1504 if (!priv->tree_view)
1505 appears_as_list = FALSE;
1507 appears_as_list = TRUE;
1509 if (appears_as_list)
1510 gtk_combo_box_list_destroy (combo_box);
1511 else if (GTK_IS_MENU (priv->popup_widget))
1513 gtk_combo_box_menu_destroy (combo_box);
1514 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1515 priv->popup_widget = NULL;
1518 if (!priv->cell_view)
1520 priv->cell_view = gtk_cell_view_new ();
1521 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1522 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1524 gtk_widget_show (priv->cell_view);
1525 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1530 if (appears_as_list)
1531 gtk_combo_box_list_setup (combo_box);
1533 gtk_combo_box_menu_setup (combo_box, TRUE);
1535 if (gtk_tree_row_reference_valid (priv->active_row))
1537 path = gtk_tree_row_reference_get_path (priv->active_row);
1538 gtk_combo_box_set_active_internal (combo_box, path);
1539 gtk_tree_path_free (path);
1542 gtk_combo_box_set_active_internal (combo_box, NULL);
1546 gtk_combo_box_menu_show (GtkWidget *menu,
1549 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1550 GtkComboBoxPrivate *priv = combo_box->priv;
1552 gtk_combo_box_child_show (menu, user_data);
1554 priv->popup_in_progress = TRUE;
1555 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1557 priv->popup_in_progress = FALSE;
1561 gtk_combo_box_menu_hide (GtkWidget *menu,
1564 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1566 gtk_combo_box_child_hide (menu,user_data);
1568 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1573 gtk_combo_box_detacher (GtkWidget *widget,
1576 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1577 GtkComboBoxPrivate *priv = combo_box->priv;
1579 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1581 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1582 gtk_combo_box_menu_show,
1584 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1585 gtk_combo_box_menu_hide,
1588 priv->popup_widget = NULL;
1592 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1595 GtkComboBoxPrivate *priv = combo_box->priv;
1597 if (GTK_IS_MENU (priv->popup_widget))
1599 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1600 priv->popup_widget = NULL;
1602 else if (priv->popup_widget)
1604 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1605 priv->popup_widget);
1606 g_object_unref (priv->popup_widget);
1607 priv->popup_widget = NULL;
1610 if (GTK_IS_MENU (popup))
1612 if (priv->popup_window)
1614 gtk_widget_destroy (priv->popup_window);
1615 priv->popup_window = NULL;
1618 priv->popup_widget = popup;
1621 * Note that we connect to show/hide on the toplevel, not the
1622 * menu itself, since the menu is not shown/hidden when it is
1623 * popped up while torn-off.
1625 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1626 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1627 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1628 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1630 gtk_menu_attach_to_widget (GTK_MENU (popup),
1631 GTK_WIDGET (combo_box),
1632 gtk_combo_box_detacher);
1636 if (!priv->popup_window)
1638 GtkWidget *toplevel;
1640 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1641 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1643 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1644 GDK_WINDOW_TYPE_HINT_COMBO);
1646 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1647 G_CALLBACK (gtk_combo_box_child_show),
1649 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1650 G_CALLBACK (gtk_combo_box_child_hide),
1653 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1654 if (GTK_IS_WINDOW (toplevel))
1656 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1657 GTK_WINDOW (priv->popup_window));
1658 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1659 GTK_WINDOW (toplevel));
1662 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1663 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1664 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1666 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1668 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1671 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1674 gtk_widget_show (priv->scrolled_window);
1676 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1677 priv->scrolled_window);
1680 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1683 gtk_widget_show (popup);
1684 g_object_ref (popup);
1685 priv->popup_widget = popup;
1690 get_widget_padding (GtkWidget *widget,
1693 GtkStyleContext *context;
1694 GtkStateFlags state;
1696 context = gtk_widget_get_style_context (widget);
1697 state = gtk_style_context_get_state (context);
1699 gtk_style_context_get_padding (context, state, padding);
1703 gtk_combo_box_menu_position_below (GtkMenu *menu,
1709 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1710 GtkAllocation child_allocation;
1716 GdkRectangle monitor;
1719 /* FIXME: is using the size request here broken? */
1720 child = gtk_bin_get_child (GTK_BIN (combo_box));
1724 gtk_widget_get_allocation (child, &child_allocation);
1726 if (!gtk_widget_get_has_window (child))
1728 sx += child_allocation.x;
1729 sy += child_allocation.y;
1732 gdk_window_get_root_coords (gtk_widget_get_window (child),
1734 get_widget_padding (GTK_WIDGET (combo_box), &padding);
1737 if (combo_box->priv->popup_fixed_width)
1738 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1740 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1742 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1745 *x = sx + child_allocation.width - req.width;
1748 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1749 monitor_num = gdk_screen_get_monitor_at_window (screen,
1750 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1751 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1755 else if (*x + req.width > monitor.x + monitor.width)
1756 *x = monitor.x + monitor.width - req.width;
1758 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1759 *y += child_allocation.height;
1760 else if (*y - monitor.y >= req.height)
1762 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1763 *y += child_allocation.height;
1771 gtk_combo_box_menu_position_over (GtkMenu *menu,
1777 GtkComboBox *combo_box;
1781 GtkAllocation allocation;
1782 GtkAllocation child_allocation;
1789 combo_box = GTK_COMBO_BOX (user_data);
1790 widget = GTK_WIDGET (combo_box);
1792 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1794 gtk_widget_get_allocation (widget, &allocation);
1796 menu_xpos = allocation.x;
1797 menu_ypos = allocation.y + allocation.height / 2 - 2;
1799 if (combo_box->priv->popup_fixed_width)
1800 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1802 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1806 gtk_widget_get_allocation (active, &child_allocation);
1807 menu_ypos -= child_allocation.height / 2;
1810 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1813 child = children->data;
1815 if (active == child)
1818 if (gtk_widget_get_visible (child))
1820 gtk_widget_get_allocation (child, &child_allocation);
1822 menu_ypos -= child_allocation.height;
1825 children = children->next;
1828 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1829 menu_xpos = menu_xpos + allocation.width - menu_width;
1831 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1832 menu_xpos, menu_ypos,
1833 &menu_xpos, &menu_ypos);
1835 /* Clamp the position on screen */
1836 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1840 else if ((menu_xpos + menu_width) > screen_width)
1841 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1850 gtk_combo_box_menu_position (GtkMenu *menu,
1856 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1857 GtkComboBoxPrivate *priv = combo_box->priv;
1858 GtkWidget *menu_item;
1860 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1861 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1864 /* FIXME handle nested menus better */
1865 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1867 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1870 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1873 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1874 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1875 GDK_WINDOW_TYPE_HINT_COMBO);
1879 gtk_combo_box_list_position (GtkComboBox *combo_box,
1885 GtkComboBoxPrivate *priv = combo_box->priv;
1886 GtkAllocation allocation;
1889 GdkRectangle monitor;
1890 GtkRequisition popup_req;
1891 GtkPolicyType hpolicy, vpolicy;
1894 /* under windows, the drop down list is as wide as the combo box itself.
1896 GtkWidget *widget = GTK_WIDGET (combo_box);
1900 gtk_widget_get_allocation (widget, &allocation);
1902 if (!gtk_widget_get_has_window (widget))
1908 window = gtk_widget_get_window (widget);
1910 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1913 *width = allocation.width;
1915 hpolicy = vpolicy = GTK_POLICY_NEVER;
1916 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1919 if (combo_box->priv->popup_fixed_width)
1921 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1923 if (popup_req.width > *width)
1925 hpolicy = GTK_POLICY_ALWAYS;
1926 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1932 /* XXX This code depends on treeviews properly reporting their natural width
1933 * list-mode menus won't fill up to their natural width until then */
1934 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1936 if (popup_req.width > *width)
1938 hpolicy = GTK_POLICY_NEVER;
1939 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1942 *width = popup_req.width;
1946 *height = popup_req.height;
1948 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1949 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1950 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1952 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1953 *x = *x + allocation.width - *width;
1957 else if (*x + *width > monitor.x + monitor.width)
1958 *x = monitor.x + monitor.width - *width;
1960 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1961 *y += allocation.height;
1962 else if (*y - *height >= monitor.y)
1964 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1966 *y += allocation.height;
1967 *height = monitor.y + monitor.height - *y;
1971 *height = *y - monitor.y;
1975 if (popup_req.height > *height)
1977 vpolicy = GTK_POLICY_ALWAYS;
1979 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1985 cell_layout_is_sensitive (GtkCellLayout *layout)
1987 GList *cells, *list;
1990 cells = gtk_cell_layout_get_cells (layout);
1993 for (list = cells; list; list = list->next)
1995 g_object_get (list->data, "sensitive", &sensitive, NULL);
2000 g_list_free (cells);
2006 cell_is_sensitive (GtkCellRenderer *cell,
2009 gboolean *sensitive = data;
2011 g_object_get (cell, "sensitive", sensitive, NULL);
2017 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2020 GtkComboBoxPrivate *priv = combo_box->priv;
2022 if (priv->row_separator_func)
2024 if (priv->row_separator_func (priv->model, iter,
2025 priv->row_separator_data))
2033 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2037 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2046 update_menu_sensitivity (GtkComboBox *combo_box,
2049 GtkComboBoxPrivate *priv = combo_box->priv;
2050 GList *children, *child;
2051 GtkWidget *item, *submenu, *separator;
2052 GtkWidget *cell_view;
2058 children = gtk_container_get_children (GTK_CONTAINER (menu));
2060 for (child = children; child; child = child->next)
2062 item = GTK_WIDGET (child->data);
2063 cell_view = gtk_bin_get_child (GTK_BIN (item));
2065 if (!GTK_IS_CELL_VIEW (cell_view))
2068 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2069 if (submenu != NULL)
2071 gtk_widget_set_sensitive (item, TRUE);
2072 update_menu_sensitivity (combo_box, submenu);
2076 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2078 if (menu != priv->popup_widget && child == children)
2080 separator = GTK_WIDGET (child->next->data);
2081 g_object_set (item, "visible", sensitive, NULL);
2082 g_object_set (separator, "visible", sensitive, NULL);
2085 gtk_widget_set_sensitive (item, sensitive);
2089 g_list_free (children);
2093 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2095 guint32 activate_time)
2097 GtkComboBoxPrivate *priv = combo_box->priv;
2100 gint width, min_width, nat_width;
2102 update_menu_sensitivity (combo_box, priv->popup_widget);
2105 if (gtk_tree_row_reference_valid (priv->active_row))
2107 path = gtk_tree_row_reference_get_path (priv->active_row);
2108 active_item = gtk_tree_path_get_indices (path)[0];
2109 gtk_tree_path_free (path);
2111 if (priv->add_tearoffs)
2115 /* FIXME handle nested menus better */
2116 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2118 if (priv->wrap_width == 0)
2120 GtkAllocation allocation;
2122 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2123 width = allocation.width;
2124 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2125 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2127 if (combo_box->priv->popup_fixed_width)
2128 width = MAX (width, min_width);
2130 width = MAX (width, nat_width);
2132 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2135 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2137 gtk_combo_box_menu_position, combo_box,
2138 button, activate_time);
2142 popup_grab_on_window (GdkWindow *window,
2143 GdkDevice *keyboard,
2145 guint32 activate_time)
2148 gdk_device_grab (keyboard, window,
2149 GDK_OWNERSHIP_WINDOW, TRUE,
2150 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2151 NULL, activate_time) != GDK_GRAB_SUCCESS)
2155 gdk_device_grab (pointer, window,
2156 GDK_OWNERSHIP_WINDOW, TRUE,
2157 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2158 GDK_POINTER_MOTION_MASK,
2159 NULL, activate_time) != GDK_GRAB_SUCCESS)
2162 gdk_device_ungrab (keyboard, activate_time);
2171 * gtk_combo_box_popup:
2172 * @combo_box: a #GtkComboBox
2174 * Pops up the menu or dropdown list of @combo_box.
2176 * This function is mostly intended for use by accessibility technologies;
2177 * applications should have little use for it.
2182 gtk_combo_box_popup (GtkComboBox *combo_box)
2184 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2186 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2190 * gtk_combo_box_popup_for_device:
2191 * @combo_box: a #GtkComboBox
2192 * @device: a #GdkDevice
2194 * Pops up the menu or dropdown list of @combo_box, the popup window
2195 * will be grabbed so only @device and its associated pointer/keyboard
2196 * are the only #GdkDevice<!-- -->s able to send events to it.
2201 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2204 GtkComboBoxPrivate *priv = combo_box->priv;
2205 gint x, y, width, height;
2206 GtkTreePath *path = NULL, *ppath;
2207 GtkWidget *toplevel;
2208 GdkDevice *keyboard, *pointer;
2211 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2212 g_return_if_fail (GDK_IS_DEVICE (device));
2214 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2217 if (gtk_widget_get_mapped (priv->popup_widget))
2220 if (priv->grab_pointer && priv->grab_keyboard)
2223 time = gtk_get_current_event_time ();
2225 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2228 pointer = gdk_device_get_associated_device (device);
2233 keyboard = gdk_device_get_associated_device (device);
2236 if (GTK_IS_MENU (priv->popup_widget))
2238 gtk_combo_box_menu_popup (combo_box,
2239 priv->activate_button,
2240 priv->activate_time);
2244 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2245 if (GTK_IS_WINDOW (toplevel))
2246 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2247 GTK_WINDOW (priv->popup_window));
2249 gtk_widget_show_all (priv->scrolled_window);
2250 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2252 gtk_widget_set_size_request (priv->popup_window, width, height);
2253 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2255 if (gtk_tree_row_reference_valid (priv->active_row))
2257 path = gtk_tree_row_reference_get_path (priv->active_row);
2258 ppath = gtk_tree_path_copy (path);
2259 if (gtk_tree_path_up (ppath))
2260 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2262 gtk_tree_path_free (ppath);
2264 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2268 gtk_widget_show (priv->popup_window);
2272 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2274 gtk_tree_path_free (path);
2277 gtk_widget_grab_focus (priv->popup_window);
2278 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2281 if (!gtk_widget_has_focus (priv->tree_view))
2282 gtk_widget_grab_focus (priv->tree_view);
2284 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2285 keyboard, pointer, time))
2287 gtk_widget_hide (priv->popup_window);
2291 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2292 priv->grab_pointer = pointer;
2293 priv->grab_keyboard = keyboard;
2297 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2301 device = gtk_get_current_event_device ();
2305 GdkDeviceManager *device_manager;
2306 GdkDisplay *display;
2309 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2310 device_manager = gdk_display_get_device_manager (display);
2312 /* No device was set, pick the first master device */
2313 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2314 device = devices->data;
2315 g_list_free (devices);
2318 gtk_combo_box_popup_for_device (combo_box, device);
2322 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2324 if (combo_box->priv->popup_shown)
2326 gtk_combo_box_popdown (combo_box);
2334 * gtk_combo_box_popdown:
2335 * @combo_box: a #GtkComboBox
2337 * Hides the menu or dropdown list of @combo_box.
2339 * This function is mostly intended for use by accessibility technologies;
2340 * applications should have little use for it.
2345 gtk_combo_box_popdown (GtkComboBox *combo_box)
2347 GtkComboBoxPrivate *priv = combo_box->priv;
2349 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2351 if (GTK_IS_MENU (priv->popup_widget))
2353 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2357 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2360 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2361 gtk_widget_hide (priv->popup_window);
2362 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2365 priv->grab_pointer = NULL;
2366 priv->grab_keyboard = NULL;
2369 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2370 gtk_widget_get_preferred_size (combo_box->priv->button, \
2374 child.x = allocation->x + padding.right; \
2376 child.x = allocation->x + allocation->width - req.width - padding.left; \
2378 child.y = allocation->y + padding.top; \
2379 child.width = req.width; \
2380 child.height = allocation->height - (padding.top + padding.bottom); \
2381 child.width = MAX (1, child.width); \
2382 child.height = MAX (1, child.height); \
2384 gtk_widget_size_allocate (combo_box->priv->button, &child);
2388 gtk_combo_box_size_allocate (GtkWidget *widget,
2389 GtkAllocation *allocation)
2391 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2392 GtkComboBoxPrivate *priv = combo_box->priv;
2393 GtkWidget *child_widget;
2394 gint focus_width, focus_pad;
2395 GtkAllocation child;
2397 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2400 gtk_widget_set_allocation (widget, allocation);
2401 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2402 get_widget_padding (widget, &padding);
2404 gtk_widget_style_get (widget,
2405 "focus-line-width", &focus_width,
2406 "focus-padding", &focus_pad,
2409 if (!priv->tree_view)
2411 if (priv->cell_view)
2413 GtkBorder button_padding;
2417 gtk_widget_size_allocate (priv->button, allocation);
2420 allocation->x += padding.left;
2421 allocation->y += padding.top;
2422 allocation->width -= padding.left + padding.right;
2423 allocation->height -= padding.top + padding.bottom;
2425 /* set some things ready */
2426 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2427 get_widget_padding (priv->button, &button_padding);
2429 child.x = allocation->x;
2430 child.y = allocation->y;
2431 width = allocation->width;
2432 child.height = allocation->height;
2434 if (!priv->is_cell_renderer)
2436 child.x += border_width + button_padding.left + focus_width + focus_pad;
2437 child.y += border_width + button_padding.top + focus_width + focus_pad;
2438 width -= (2 * (border_width + focus_width + focus_pad)) +
2439 button_padding.left + button_padding.right;
2440 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2441 button_padding.top + button_padding.bottom;
2444 /* handle the children */
2445 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2446 child.width = req.width;
2448 child.x += width - req.width - button_padding.right;
2449 child.width = MAX (1, child.width);
2450 child.height = MAX (1, child.height);
2451 gtk_widget_size_allocate (priv->arrow, &child);
2453 child.x += req.width;
2454 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2455 child.width = req.width;
2457 child.x -= req.width;
2458 child.width = MAX (1, child.width);
2459 child.height = MAX (1, child.height);
2460 gtk_widget_size_allocate (priv->separator, &child);
2464 child.x += req.width;
2465 child.width = allocation->x + allocation->width
2466 - (border_width + focus_width + focus_pad)
2471 child.width = child.x;
2472 child.x = allocation->x
2473 + border_width + button_padding.left + focus_width + focus_pad;
2474 child.width -= child.x;
2477 if (gtk_widget_get_visible (priv->popup_widget))
2479 gint width, menu_width;
2481 if (priv->wrap_width == 0)
2483 GtkAllocation combo_box_allocation;
2485 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2486 width = combo_box_allocation.width;
2487 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2489 if (combo_box->priv->popup_fixed_width)
2490 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2492 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2494 gtk_widget_set_size_request (priv->popup_widget,
2495 MAX (width, menu_width), -1);
2498 /* reposition the menu after giving it a new width */
2499 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2502 child.width = MAX (1, child.width);
2503 child.height = MAX (1, child.height);
2504 gtk_widget_size_allocate (child_widget, &child);
2508 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2511 child.x = allocation->x + req.width + padding.right;
2513 child.x = allocation->x + padding.left;
2514 child.y = allocation->y + padding.top;
2515 child.width = allocation->width - req.width - (padding.left + padding.right);
2516 child.width = MAX (1, child.width);
2517 child.height = MAX (1, child.height);
2518 gtk_widget_size_allocate (child_widget, &child);
2524 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2527 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2531 child.x = allocation->x + req.width;
2533 child.x = allocation->x;
2535 child.y = allocation->y;
2536 child.width = allocation->width - req.width;
2537 child.height = allocation->height;
2539 if (priv->cell_view_frame)
2541 child.x += padding.left + border_width;
2542 child.y += padding.top + border_width;
2543 child.width = MAX (1, child.width - (2 * border_width) - (padding.left + padding.right));
2544 child.height = MAX (1, child.height - (2 * border_width) - (padding.top + padding.bottom));
2545 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2548 if (priv->has_frame)
2550 GtkBorder frame_padding;
2552 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2553 get_widget_padding (priv->cell_view_frame, &frame_padding);
2555 child.x += border_width + frame_padding.left;
2556 child.y += border_width + frame_padding.right;
2557 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2558 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2563 child.x += padding.left + border_width;
2564 child.y += padding.top + border_width;
2565 child.width -= (2 * border_width) - (padding.left + padding.right);
2566 child.height -= (2 * border_width) - (padding.top + padding.bottom);
2569 if (gtk_widget_get_visible (priv->popup_window))
2571 gint x, y, width, height;
2572 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2573 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2574 gtk_widget_set_size_request (priv->popup_window, width, height);
2577 child.width = MAX (1, child.width);
2578 child.height = MAX (1, child.height);
2579 gtk_widget_size_allocate (child_widget, &child);
2583 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2586 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2588 GtkComboBoxPrivate *priv = combo_box->priv;
2592 g_signal_handler_disconnect (priv->model,
2594 g_signal_handler_disconnect (priv->model,
2596 g_signal_handler_disconnect (priv->model,
2597 priv->reordered_id);
2598 g_signal_handler_disconnect (priv->model,
2604 g_object_unref (priv->model);
2608 if (priv->active_row)
2610 gtk_tree_row_reference_free (priv->active_row);
2611 priv->active_row = NULL;
2614 if (priv->cell_view)
2615 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2619 gtk_combo_box_forall (GtkContainer *container,
2620 gboolean include_internals,
2621 GtkCallback callback,
2622 gpointer callback_data)
2624 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2625 GtkComboBoxPrivate *priv = combo_box->priv;
2628 if (include_internals)
2631 (* callback) (priv->button, callback_data);
2632 if (priv->cell_view_frame)
2633 (* callback) (priv->cell_view_frame, callback_data);
2636 child = gtk_bin_get_child (GTK_BIN (container));
2638 (* callback) (child, callback_data);
2642 gtk_combo_box_child_show (GtkWidget *widget,
2643 GtkComboBox *combo_box)
2645 GtkComboBoxPrivate *priv = combo_box->priv;
2647 priv->popup_shown = TRUE;
2648 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2652 gtk_combo_box_child_hide (GtkWidget *widget,
2653 GtkComboBox *combo_box)
2655 GtkComboBoxPrivate *priv = combo_box->priv;
2657 priv->popup_shown = FALSE;
2658 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2662 gtk_combo_box_draw (GtkWidget *widget,
2665 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2666 GtkComboBoxPrivate *priv = combo_box->priv;
2668 if (priv->shadow_type != GTK_SHADOW_NONE)
2670 GtkStyleContext *context;
2671 GtkStateFlags state;
2673 context = gtk_widget_get_style_context (widget);
2674 state = gtk_widget_get_state_flags (widget);
2675 gtk_style_context_set_state (context, state);
2677 gtk_render_background (context, cr, 0, 0,
2678 gtk_widget_get_allocated_width (widget),
2679 gtk_widget_get_allocated_height (widget));
2680 gtk_render_frame (context, cr, 0, 0,
2681 gtk_widget_get_allocated_width (widget),
2682 gtk_widget_get_allocated_height (widget));
2685 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2688 if (priv->tree_view && priv->cell_view_frame)
2690 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2691 priv->cell_view_frame, cr);
2694 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2695 gtk_bin_get_child (GTK_BIN (widget)),
2711 path_visible (GtkTreeView *view,
2717 /* Note that we rely on the fact that collapsed rows don't have nodes
2719 return _gtk_tree_view_find_node (view, path, &tree, &node);
2723 tree_next_func (GtkTreeModel *model,
2728 SearchData *search_data = (SearchData *)data;
2730 if (search_data->found)
2732 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2735 if (search_data->visible &&
2736 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2739 search_data->set = TRUE;
2740 search_data->iter = *iter;
2745 if (gtk_tree_path_compare (path, search_data->path) == 0)
2746 search_data->found = TRUE;
2752 tree_next (GtkComboBox *combo,
2753 GtkTreeModel *model,
2758 SearchData search_data;
2760 search_data.combo = combo;
2761 search_data.path = gtk_tree_model_get_path (model, iter);
2762 search_data.visible = visible;
2763 search_data.found = FALSE;
2764 search_data.set = FALSE;
2766 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2768 *next = search_data.iter;
2770 gtk_tree_path_free (search_data.path);
2772 return search_data.set;
2776 tree_prev_func (GtkTreeModel *model,
2781 SearchData *search_data = (SearchData *)data;
2783 if (gtk_tree_path_compare (path, search_data->path) == 0)
2785 search_data->found = TRUE;
2789 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2792 if (search_data->visible &&
2793 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2796 search_data->set = TRUE;
2797 search_data->iter = *iter;
2803 tree_prev (GtkComboBox *combo,
2804 GtkTreeModel *model,
2809 SearchData search_data;
2811 search_data.combo = combo;
2812 search_data.path = gtk_tree_model_get_path (model, iter);
2813 search_data.visible = visible;
2814 search_data.found = FALSE;
2815 search_data.set = FALSE;
2817 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2819 *prev = search_data.iter;
2821 gtk_tree_path_free (search_data.path);
2823 return search_data.set;
2827 tree_last_func (GtkTreeModel *model,
2832 SearchData *search_data = (SearchData *)data;
2834 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2837 /* Note that we rely on the fact that collapsed rows don't have nodes
2839 if (search_data->visible &&
2840 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2843 search_data->set = TRUE;
2844 search_data->iter = *iter;
2850 tree_last (GtkComboBox *combo,
2851 GtkTreeModel *model,
2855 SearchData search_data;
2857 search_data.combo = combo;
2858 search_data.visible = visible;
2859 search_data.set = FALSE;
2861 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2863 *last = search_data.iter;
2865 return search_data.set;
2870 tree_first_func (GtkTreeModel *model,
2875 SearchData *search_data = (SearchData *)data;
2877 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2880 if (search_data->visible &&
2881 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2884 search_data->set = TRUE;
2885 search_data->iter = *iter;
2891 tree_first (GtkComboBox *combo,
2892 GtkTreeModel *model,
2896 SearchData search_data;
2898 search_data.combo = combo;
2899 search_data.visible = visible;
2900 search_data.set = FALSE;
2902 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2904 *first = search_data.iter;
2906 return search_data.set;
2910 gtk_combo_box_scroll_event (GtkWidget *widget,
2911 GdkEventScroll *event)
2913 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2916 GtkTreeIter new_iter;
2918 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2921 if (event->direction == GDK_SCROLL_UP)
2922 found = tree_prev (combo_box, combo_box->priv->model,
2923 &iter, &new_iter, FALSE);
2925 found = tree_next (combo_box, combo_box->priv->model,
2926 &iter, &new_iter, FALSE);
2929 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2938 gtk_combo_box_row_separator_func (GtkTreeModel *model,
2942 GtkComboBoxPrivate *priv = combo->priv;
2944 if (priv->row_separator_func)
2945 return priv->row_separator_func (model, iter, priv->row_separator_data);
2951 gtk_combo_box_header_func (GtkTreeModel *model,
2955 /* Every submenu has a selectable header, however we
2956 * can expose a method to make that configurable by
2957 * the user (like row_separator_func is done) */
2962 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2963 gboolean add_children)
2965 GtkComboBoxPrivate *priv = combo_box->priv;
2969 child = gtk_bin_get_child (GTK_BIN (combo_box));
2971 if (priv->cell_view)
2973 priv->button = gtk_toggle_button_new ();
2974 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2975 priv->focus_on_click);
2977 g_signal_connect (priv->button, "toggled",
2978 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2979 gtk_widget_set_parent (priv->button,
2980 gtk_widget_get_parent (child));
2982 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2983 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2985 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
2986 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2988 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2989 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2991 gtk_widget_show_all (priv->button);
2995 priv->button = gtk_toggle_button_new ();
2996 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2997 priv->focus_on_click);
2999 g_signal_connect (priv->button, "toggled",
3000 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3001 gtk_widget_set_parent (priv->button,
3002 gtk_widget_get_parent (child));
3004 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3005 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3006 gtk_widget_show_all (priv->button);
3009 g_signal_connect (priv->button, "button-press-event",
3010 G_CALLBACK (gtk_combo_box_menu_button_press),
3012 g_signal_connect (priv->button, "state-flags-changed",
3013 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3016 /* create our funky menu */
3017 menu = _gtk_tree_menu_new_with_area (priv->area);
3018 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3020 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3022 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3023 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3024 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3025 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3026 combo_box->priv->add_tearoffs);
3028 g_signal_connect (menu, "menu-activate",
3029 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3031 /* Chain our row_separator_func through */
3032 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3033 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3036 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3037 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3040 g_signal_connect (menu, "key-press-event",
3041 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3042 gtk_combo_box_set_popup_widget (combo_box, menu);
3044 gtk_combo_box_update_title (combo_box);
3048 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3050 GtkComboBoxPrivate *priv = combo_box->priv;
3052 g_signal_handlers_disconnect_matched (priv->button,
3053 G_SIGNAL_MATCH_DATA,
3055 gtk_combo_box_menu_button_press, NULL);
3056 g_signal_handlers_disconnect_matched (priv->button,
3057 G_SIGNAL_MATCH_DATA,
3059 gtk_combo_box_button_state_flags_changed, combo_box);
3060 g_signal_handlers_disconnect_matched (priv->popup_widget,
3061 G_SIGNAL_MATCH_DATA,
3063 gtk_combo_box_menu_activate, combo_box);
3065 /* unparent will remove our latest ref */
3066 gtk_widget_unparent (priv->button);
3069 priv->button = NULL;
3071 priv->separator = NULL;
3073 /* changing the popup window will unref the menu and the children */
3078 gtk_combo_box_menu_button_press (GtkWidget *widget,
3079 GdkEventButton *event,
3082 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3083 GtkComboBoxPrivate *priv = combo_box->priv;
3085 if (GTK_IS_MENU (priv->popup_widget) &&
3086 event->type == GDK_BUTTON_PRESS && event->button == 1)
3088 if (priv->focus_on_click &&
3089 !gtk_widget_has_focus (priv->button))
3090 gtk_widget_grab_focus (priv->button);
3092 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3101 gtk_combo_box_menu_activate (GtkWidget *menu,
3103 GtkComboBox *combo_box)
3107 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3108 gtk_combo_box_set_active_iter (combo_box, &iter);
3110 g_object_set (combo_box,
3111 "editing-canceled", FALSE,
3117 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3120 gboolean sensitive = TRUE; /* fool code checkers */
3122 if (!combo_box->priv->button)
3125 switch (combo_box->priv->button_sensitivity)
3127 case GTK_SENSITIVITY_ON:
3130 case GTK_SENSITIVITY_OFF:
3133 case GTK_SENSITIVITY_AUTO:
3134 sensitive = combo_box->priv->model &&
3135 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3138 g_assert_not_reached ();
3142 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3144 /* In list-mode, we also need to update sensitivity of the event box */
3145 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3146 && combo_box->priv->cell_view)
3147 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3151 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3156 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3158 if (combo_box->priv->tree_view)
3159 gtk_combo_box_list_popup_resize (combo_box);
3161 gtk_combo_box_update_sensitivity (combo_box);
3165 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3169 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3170 GtkComboBoxPrivate *priv = combo_box->priv;
3172 if (!gtk_tree_row_reference_valid (priv->active_row))
3174 if (priv->cell_view)
3175 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3176 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3179 if (priv->tree_view)
3180 gtk_combo_box_list_popup_resize (combo_box);
3182 gtk_combo_box_update_sensitivity (combo_box);
3186 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3192 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3196 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3201 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3202 GtkComboBoxPrivate *priv = combo_box->priv;
3203 GtkTreePath *active_path;
3205 /* FIXME this belongs to GtkCellView */
3206 if (gtk_tree_row_reference_valid (priv->active_row))
3208 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3209 if (gtk_tree_path_compare (path, active_path) == 0 &&
3211 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3212 gtk_tree_path_free (active_path);
3215 if (priv->tree_view)
3216 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3220 list_popup_resize_idle (gpointer user_data)
3222 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3223 GtkComboBoxPrivate *priv = combo_box->priv;
3224 gint x, y, width, height;
3226 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3228 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3230 gtk_widget_set_size_request (priv->popup_window, width, height);
3231 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3234 priv->resize_idle_id = 0;
3240 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3242 GtkComboBoxPrivate *priv = combo_box->priv;
3244 if (!priv->resize_idle_id)
3245 priv->resize_idle_id =
3246 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3250 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3255 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3257 gtk_combo_box_list_popup_resize (combo_box);
3266 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3268 GtkComboBoxPrivate *priv = combo_box->priv;
3269 GtkTreeSelection *sel;
3271 GtkWidget *widget = GTK_WIDGET (combo_box);
3273 priv->button = gtk_toggle_button_new ();
3274 child = gtk_bin_get_child (GTK_BIN (combo_box));
3275 gtk_widget_set_parent (priv->button,
3276 gtk_widget_get_parent (child));
3277 g_signal_connect (priv->button, "button-press-event",
3278 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3279 g_signal_connect (priv->button, "toggled",
3280 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3282 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3283 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3284 priv->separator = NULL;
3285 gtk_widget_show_all (priv->button);
3287 if (priv->cell_view)
3289 GtkStyleContext *context;
3290 GtkStateFlags state;
3293 context = gtk_widget_get_style_context (widget);
3294 state = gtk_widget_get_state_flags (widget);
3295 gtk_style_context_get_background_color (context, state, &color);
3297 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3299 priv->box = gtk_event_box_new ();
3300 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3303 if (priv->has_frame)
3305 priv->cell_view_frame = gtk_frame_new (NULL);
3306 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3311 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3312 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3316 gtk_widget_set_parent (priv->cell_view_frame,
3317 gtk_widget_get_parent (child));
3318 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3319 gtk_widget_show_all (priv->cell_view_frame);
3321 g_signal_connect (priv->box, "button-press-event",
3322 G_CALLBACK (gtk_combo_box_list_button_pressed),
3326 priv->tree_view = gtk_tree_view_new ();
3327 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3328 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3329 gtk_tree_selection_set_select_function (sel,
3330 gtk_combo_box_list_select_func,
3332 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3334 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3337 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3338 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3342 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3344 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3345 gtk_tree_view_column_new_with_area (priv->area));
3347 if (gtk_tree_row_reference_valid (priv->active_row))
3351 path = gtk_tree_row_reference_get_path (priv->active_row);
3352 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3354 gtk_tree_path_free (path);
3357 /* set sample/popup widgets */
3358 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3360 g_signal_connect (priv->tree_view, "key-press-event",
3361 G_CALLBACK (gtk_combo_box_list_key_press),
3363 g_signal_connect (priv->tree_view, "enter-notify-event",
3364 G_CALLBACK (gtk_combo_box_list_enter_notify),
3366 g_signal_connect (priv->tree_view, "row-expanded",
3367 G_CALLBACK (gtk_combo_box_model_row_expanded),
3369 g_signal_connect (priv->tree_view, "row-collapsed",
3370 G_CALLBACK (gtk_combo_box_model_row_expanded),
3372 g_signal_connect (priv->popup_window, "button-press-event",
3373 G_CALLBACK (gtk_combo_box_list_button_pressed),
3375 g_signal_connect (priv->popup_window, "button-release-event",
3376 G_CALLBACK (gtk_combo_box_list_button_released),
3379 gtk_widget_show (priv->tree_view);
3381 gtk_combo_box_update_sensitivity (combo_box);
3385 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3387 GtkComboBoxPrivate *priv = combo_box->priv;
3389 /* disconnect signals */
3390 g_signal_handlers_disconnect_matched (priv->tree_view,
3391 G_SIGNAL_MATCH_DATA,
3392 0, 0, NULL, NULL, combo_box);
3393 g_signal_handlers_disconnect_matched (priv->button,
3394 G_SIGNAL_MATCH_DATA,
3396 gtk_combo_box_list_button_pressed,
3398 g_signal_handlers_disconnect_matched (priv->popup_window,
3399 G_SIGNAL_MATCH_DATA,
3401 gtk_combo_box_list_button_pressed,
3403 g_signal_handlers_disconnect_matched (priv->popup_window,
3404 G_SIGNAL_MATCH_DATA,
3406 gtk_combo_box_list_button_released,
3409 g_signal_handlers_disconnect_matched (priv->popup_window,
3410 G_SIGNAL_MATCH_DATA,
3412 gtk_combo_box_child_show,
3415 g_signal_handlers_disconnect_matched (priv->popup_window,
3416 G_SIGNAL_MATCH_DATA,
3418 gtk_combo_box_child_hide,
3422 g_signal_handlers_disconnect_matched (priv->box,
3423 G_SIGNAL_MATCH_DATA,
3425 gtk_combo_box_list_button_pressed,
3428 /* destroy things (unparent will kill the latest ref from us)
3429 * last unref on button will destroy the arrow
3431 gtk_widget_unparent (priv->button);
3432 priv->button = NULL;
3435 if (priv->cell_view)
3437 g_object_set (priv->cell_view,
3438 "background-set", FALSE,
3442 if (priv->cell_view_frame)
3444 gtk_widget_unparent (priv->cell_view_frame);
3445 priv->cell_view_frame = NULL;
3449 if (priv->scroll_timer)
3451 g_source_remove (priv->scroll_timer);
3452 priv->scroll_timer = 0;
3455 if (priv->resize_idle_id)
3457 g_source_remove (priv->resize_idle_id);
3458 priv->resize_idle_id = 0;
3461 gtk_widget_destroy (priv->tree_view);
3463 priv->tree_view = NULL;
3464 if (priv->popup_widget)
3466 g_object_unref (priv->popup_widget);
3467 priv->popup_widget = NULL;
3474 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3475 GdkEventButton *event,
3478 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3479 GtkComboBoxPrivate *priv = combo_box->priv;
3481 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3483 if (ewidget == priv->popup_window)
3486 if ((ewidget != priv->button && ewidget != priv->box) ||
3487 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3490 if (priv->focus_on_click &&
3491 !gtk_widget_has_focus (priv->button))
3492 gtk_widget_grab_focus (priv->button);
3494 gtk_combo_box_popup_for_device (combo_box, event->device);
3496 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3498 priv->auto_scroll = FALSE;
3499 if (priv->scroll_timer == 0)
3500 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3501 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3504 priv->popup_in_progress = TRUE;
3510 gtk_combo_box_list_button_released (GtkWidget *widget,
3511 GdkEventButton *event,
3515 GtkTreePath *path = NULL;
3518 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3519 GtkComboBoxPrivate *priv = combo_box->priv;
3521 gboolean popup_in_progress = FALSE;
3523 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3525 if (priv->popup_in_progress)
3527 popup_in_progress = TRUE;
3528 priv->popup_in_progress = FALSE;
3531 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3533 if (priv->scroll_timer)
3535 g_source_remove (priv->scroll_timer);
3536 priv->scroll_timer = 0;
3539 if (ewidget != priv->tree_view)
3541 if ((ewidget == priv->button ||
3542 ewidget == priv->box) &&
3543 !popup_in_progress &&
3544 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3546 gtk_combo_box_popdown (combo_box);
3550 /* released outside treeview */
3551 if (ewidget != priv->button && ewidget != priv->box)
3553 gtk_combo_box_popdown (combo_box);
3561 /* select something cool */
3562 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3568 return TRUE; /* clicked outside window? */
3570 gtk_tree_model_get_iter (priv->model, &iter, path);
3571 gtk_tree_path_free (path);
3573 gtk_combo_box_popdown (combo_box);
3575 if (tree_column_row_is_sensitive (combo_box, &iter))
3576 gtk_combo_box_set_active_iter (combo_box, &iter);
3582 gtk_combo_box_menu_key_press (GtkWidget *widget,
3586 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3588 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3590 /* The menu hasn't managed the
3591 * event, forward it to the combobox
3593 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3600 gtk_combo_box_list_key_press (GtkWidget *widget,
3604 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3607 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3608 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3610 GtkTreeModel *model = NULL;
3612 gtk_combo_box_popdown (combo_box);
3614 if (combo_box->priv->model)
3616 GtkTreeSelection *sel;
3618 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3620 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3621 gtk_combo_box_set_active_iter (combo_box, &iter);
3627 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3629 /* The list hasn't managed the
3630 * event, forward it to the combobox
3632 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3639 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3644 GtkAllocation allocation;
3645 GtkWidget *tree_view = combo_box->priv->tree_view;
3648 gtk_widget_get_allocation (tree_view, &allocation);
3650 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3651 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3653 if (x <= allocation.x &&
3654 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3656 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3657 gtk_adjustment_set_value (adj, value);
3659 else if (x >= allocation.x + allocation.width &&
3660 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3662 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3663 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3667 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3668 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3670 if (y <= allocation.y &&
3671 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3673 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3674 gtk_adjustment_set_value (adj, value);
3676 else if (y >= allocation.height &&
3677 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3679 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3680 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3686 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3688 GtkComboBoxPrivate *priv = combo_box->priv;
3691 if (priv->auto_scroll)
3693 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3696 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3703 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3704 GdkEventCrossing *event,
3707 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3709 combo_box->priv->auto_scroll = TRUE;
3715 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3716 GtkTreeModel *model,
3718 gboolean path_currently_selected,
3721 GList *list, *columns;
3722 gboolean sensitive = FALSE;
3724 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3726 for (list = columns; list && !sensitive; list = list->next)
3728 GList *cells, *cell;
3729 gboolean cell_sensitive, cell_visible;
3731 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3733 if (!gtk_tree_view_column_get_visible (column))
3736 gtk_tree_model_get_iter (model, &iter, path);
3737 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3740 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3743 g_object_get (cell->data,
3744 "sensitive", &cell_sensitive,
3745 "visible", &cell_visible,
3748 if (cell_visible && cell_sensitive)
3753 g_list_free (cells);
3755 sensitive = cell_sensitive;
3758 g_list_free (columns);
3764 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3769 /* XXX Do nothing ? */
3773 * GtkCellLayout implementation
3775 static GtkCellArea *
3776 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3778 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3779 GtkComboBoxPrivate *priv = combo->priv;
3781 if (G_UNLIKELY (!priv->area))
3783 priv->area = gtk_cell_area_box_new ();
3784 g_object_ref_sink (priv->area);
3795 * gtk_combo_box_new:
3797 * Creates a new empty #GtkComboBox.
3799 * Return value: A new #GtkComboBox.
3804 gtk_combo_box_new (void)
3806 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3810 * gtk_combo_box_new_with_area:
3811 * @area: the #GtkCellArea to use to layout cell renderers
3813 * Creates a new empty #GtkComboBox using @area to layout cells.
3815 * Return value: A new #GtkComboBox.
3818 gtk_combo_box_new_with_area (GtkCellArea *area)
3820 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3824 * gtk_combo_box_new_with_area_and_entry:
3825 * @area: the #GtkCellArea to use to layout cell renderers
3827 * Creates a new empty #GtkComboBox with an entry.
3829 * The new combo box will use @area to layout cells.
3831 * Return value: A new #GtkComboBox.
3834 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3836 return g_object_new (GTK_TYPE_COMBO_BOX,
3844 * gtk_combo_box_new_with_entry:
3846 * Creates a new empty #GtkComboBox with an entry.
3848 * Return value: A new #GtkComboBox.
3851 gtk_combo_box_new_with_entry (void)
3853 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3857 * gtk_combo_box_new_with_model:
3858 * @model: A #GtkTreeModel.
3860 * Creates a new #GtkComboBox with the model initialized to @model.
3862 * Return value: A new #GtkComboBox.
3867 gtk_combo_box_new_with_model (GtkTreeModel *model)
3869 GtkComboBox *combo_box;
3871 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3873 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3875 return GTK_WIDGET (combo_box);
3879 * gtk_combo_box_new_with_model_and_entry:
3880 * @model: A #GtkTreeModel
3882 * Creates a new empty #GtkComboBox with an entry
3883 * and with the model initialized to @model.
3885 * Return value: A new #GtkComboBox
3888 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3890 return g_object_new (GTK_TYPE_COMBO_BOX,
3897 * gtk_combo_box_get_wrap_width:
3898 * @combo_box: A #GtkComboBox
3900 * Returns the wrap width which is used to determine the number of columns
3901 * for the popup menu. If the wrap width is larger than 1, the combo box
3904 * Returns: the wrap width.
3909 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3911 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3913 return combo_box->priv->wrap_width;
3917 * gtk_combo_box_set_wrap_width:
3918 * @combo_box: A #GtkComboBox
3919 * @width: Preferred number of columns
3921 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3922 * the preferred number of columns when you want the popup to be layed out
3928 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3931 GtkComboBoxPrivate *priv;
3933 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3934 g_return_if_fail (width >= 0);
3936 priv = combo_box->priv;
3938 if (width != priv->wrap_width)
3940 priv->wrap_width = width;
3942 gtk_combo_box_check_appearance (combo_box);
3944 if (GTK_IS_TREE_MENU (priv->popup_widget))
3945 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
3947 g_object_notify (G_OBJECT (combo_box), "wrap-width");
3952 * gtk_combo_box_get_row_span_column:
3953 * @combo_box: A #GtkComboBox
3955 * Returns the column with row span information for @combo_box.
3957 * Returns: the row span column.
3962 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3964 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3966 return combo_box->priv->row_column;
3970 * gtk_combo_box_set_row_span_column:
3971 * @combo_box: A #GtkComboBox.
3972 * @row_span: A column in the model passed during construction.
3974 * Sets the column with row span information for @combo_box to be @row_span.
3975 * The row span column contains integers which indicate how many rows
3976 * an item should span.
3981 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3984 GtkComboBoxPrivate *priv;
3987 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3989 priv = combo_box->priv;
3991 col = gtk_tree_model_get_n_columns (priv->model);
3992 g_return_if_fail (row_span >= -1 && row_span < col);
3994 if (row_span != priv->row_column)
3996 priv->row_column = row_span;
3998 if (GTK_IS_TREE_MENU (priv->popup_widget))
3999 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4001 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4006 * gtk_combo_box_get_column_span_column:
4007 * @combo_box: A #GtkComboBox
4009 * Returns the column with column span information for @combo_box.
4011 * Returns: the column span column.
4016 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4018 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4020 return combo_box->priv->col_column;
4024 * gtk_combo_box_set_column_span_column:
4025 * @combo_box: A #GtkComboBox
4026 * @column_span: A column in the model passed during construction
4028 * Sets the column with column span information for @combo_box to be
4029 * @column_span. The column span column contains integers which indicate
4030 * how many columns an item should span.
4035 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4038 GtkComboBoxPrivate *priv;
4041 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4043 priv = combo_box->priv;
4045 col = gtk_tree_model_get_n_columns (priv->model);
4046 g_return_if_fail (column_span >= -1 && column_span < col);
4048 if (column_span != priv->col_column)
4050 priv->col_column = column_span;
4052 if (GTK_IS_TREE_MENU (priv->popup_widget))
4053 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4055 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4060 * gtk_combo_box_get_active:
4061 * @combo_box: A #GtkComboBox
4063 * Returns the index of the currently active item, or -1 if there's no
4064 * active item. If the model is a non-flat treemodel, and the active item
4065 * is not an immediate child of the root of the tree, this function returns
4066 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4067 * <literal>path</literal> is the #GtkTreePath of the active item.
4069 * Return value: An integer which is the index of the currently active item,
4070 * or -1 if there's no active item.
4075 gtk_combo_box_get_active (GtkComboBox *combo_box)
4077 GtkComboBoxPrivate *priv;
4080 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4082 priv = combo_box->priv;
4084 if (gtk_tree_row_reference_valid (priv->active_row))
4088 path = gtk_tree_row_reference_get_path (priv->active_row);
4089 result = gtk_tree_path_get_indices (path)[0];
4090 gtk_tree_path_free (path);
4099 * gtk_combo_box_set_active:
4100 * @combo_box: A #GtkComboBox
4101 * @index_: An index in the model passed during construction, or -1 to have
4104 * Sets the active item of @combo_box to be the item at @index.
4109 gtk_combo_box_set_active (GtkComboBox *combo_box,
4112 GtkTreePath *path = NULL;
4113 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4114 g_return_if_fail (index_ >= -1);
4116 if (combo_box->priv->model == NULL)
4118 /* Save index, in case the model is set after the index */
4119 combo_box->priv->active = index_;
4125 path = gtk_tree_path_new_from_indices (index_, -1);
4127 gtk_combo_box_set_active_internal (combo_box, path);
4130 gtk_tree_path_free (path);
4134 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4137 GtkComboBoxPrivate *priv = combo_box->priv;
4138 GtkTreePath *active_path;
4141 /* Remember whether the initially active row is valid. */
4142 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4144 if (path && is_valid_row_reference)
4146 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4147 path_cmp = gtk_tree_path_compare (path, active_path);
4148 gtk_tree_path_free (active_path);
4153 if (priv->active_row)
4155 gtk_tree_row_reference_free (priv->active_row);
4156 priv->active_row = NULL;
4161 if (priv->tree_view)
4162 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4165 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4167 if (GTK_IS_MENU (menu))
4168 gtk_menu_set_active (menu, -1);
4171 if (priv->cell_view)
4172 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4175 * Do not emit a "changed" signal when an already invalid selection was
4176 * now set to invalid.
4178 if (!is_valid_row_reference)
4184 gtk_tree_row_reference_new (priv->model, path);
4186 if (priv->tree_view)
4188 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4191 else if (GTK_IS_MENU (priv->popup_widget))
4193 /* FIXME handle nested menus better */
4194 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4195 gtk_tree_path_get_indices (path)[0]);
4198 if (priv->cell_view)
4199 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4203 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4204 g_object_notify (G_OBJECT (combo_box), "active");
4205 if (combo_box->priv->id_column >= 0)
4206 g_object_notify (G_OBJECT (combo_box), "active-id");
4211 * gtk_combo_box_get_active_iter:
4212 * @combo_box: A #GtkComboBox
4213 * @iter: (out): The uninitialized #GtkTreeIter
4215 * Sets @iter to point to the current active item, if it exists.
4217 * Return value: %TRUE, if @iter was set
4222 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4228 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4230 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4233 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4234 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4235 gtk_tree_path_free (path);
4241 * gtk_combo_box_set_active_iter:
4242 * @combo_box: A #GtkComboBox
4243 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4245 * Sets the current active item to be the one referenced by @iter, or
4246 * unsets the active item if @iter is %NULL.
4251 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4254 GtkTreePath *path = NULL;
4256 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4259 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4261 gtk_combo_box_set_active_internal (combo_box, path);
4262 gtk_tree_path_free (path);
4266 * gtk_combo_box_set_model:
4267 * @combo_box: A #GtkComboBox
4268 * @model: (allow-none): A #GtkTreeModel
4270 * Sets the model used by @combo_box to be @model. Will unset a previously set
4271 * model (if applicable). If model is %NULL, then it will unset the model.
4273 * Note that this function does not clear the cell renderers, you have to
4274 * call gtk_cell_layout_clear() yourself if you need to set up different
4275 * cell renderers for the new model.
4280 gtk_combo_box_set_model (GtkComboBox *combo_box,
4281 GtkTreeModel *model)
4283 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4284 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4286 if (model == combo_box->priv->model)
4289 gtk_combo_box_unset_model (combo_box);
4294 combo_box->priv->model = model;
4295 g_object_ref (combo_box->priv->model);
4297 combo_box->priv->inserted_id =
4298 g_signal_connect (combo_box->priv->model, "row-inserted",
4299 G_CALLBACK (gtk_combo_box_model_row_inserted),
4301 combo_box->priv->deleted_id =
4302 g_signal_connect (combo_box->priv->model, "row-deleted",
4303 G_CALLBACK (gtk_combo_box_model_row_deleted),
4305 combo_box->priv->reordered_id =
4306 g_signal_connect (combo_box->priv->model, "rows-reordered",
4307 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4309 combo_box->priv->changed_id =
4310 g_signal_connect (combo_box->priv->model, "row-changed",
4311 G_CALLBACK (gtk_combo_box_model_row_changed),
4314 if (combo_box->priv->tree_view)
4317 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4318 combo_box->priv->model);
4319 gtk_combo_box_list_popup_resize (combo_box);
4322 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4325 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4326 combo_box->priv->model);
4329 if (combo_box->priv->cell_view)
4330 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4331 combo_box->priv->model);
4333 if (combo_box->priv->active != -1)
4335 /* If an index was set in advance, apply it now */
4336 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4337 combo_box->priv->active = -1;
4341 gtk_combo_box_update_sensitivity (combo_box);
4343 g_object_notify (G_OBJECT (combo_box), "model");
4347 * gtk_combo_box_get_model:
4348 * @combo_box: A #GtkComboBox
4350 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4352 * Return value: (transfer none): A #GtkTreeModel which was passed
4353 * during construction.
4358 gtk_combo_box_get_model (GtkComboBox *combo_box)
4360 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4362 return combo_box->priv->model;
4366 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4367 GtkScrollType scroll)
4370 GtkTreeIter new_iter;
4371 gboolean active_iter;
4374 if (!combo_box->priv->model)
4376 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4380 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4384 case GTK_SCROLL_STEP_BACKWARD:
4385 case GTK_SCROLL_STEP_UP:
4386 case GTK_SCROLL_STEP_LEFT:
4389 found = tree_prev (combo_box, combo_box->priv->model,
4390 &iter, &new_iter, FALSE);
4393 /* else fall through */
4395 case GTK_SCROLL_PAGE_FORWARD:
4396 case GTK_SCROLL_PAGE_DOWN:
4397 case GTK_SCROLL_PAGE_RIGHT:
4398 case GTK_SCROLL_END:
4399 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4402 case GTK_SCROLL_STEP_FORWARD:
4403 case GTK_SCROLL_STEP_DOWN:
4404 case GTK_SCROLL_STEP_RIGHT:
4407 found = tree_next (combo_box, combo_box->priv->model,
4408 &iter, &new_iter, FALSE);
4411 /* else fall through */
4413 case GTK_SCROLL_PAGE_BACKWARD:
4414 case GTK_SCROLL_PAGE_UP:
4415 case GTK_SCROLL_PAGE_LEFT:
4416 case GTK_SCROLL_START:
4417 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4424 if (found && active_iter)
4426 GtkTreePath *old_path;
4427 GtkTreePath *new_path;
4429 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4430 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4432 if (gtk_tree_path_compare (old_path, new_path) == 0)
4435 gtk_tree_path_free (old_path);
4436 gtk_tree_path_free (new_path);
4441 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4445 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4450 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4451 gboolean group_cycling)
4453 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4455 if (combo_box->priv->has_entry)
4459 child = gtk_bin_get_child (GTK_BIN (combo_box));
4461 gtk_widget_grab_focus (child);
4464 gtk_widget_grab_focus (combo_box->priv->button);
4470 gtk_combo_box_grab_focus (GtkWidget *widget)
4472 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4474 if (combo_box->priv->has_entry)
4478 child = gtk_bin_get_child (GTK_BIN (combo_box));
4480 gtk_widget_grab_focus (child);
4483 gtk_widget_grab_focus (combo_box->priv->button);
4487 gtk_combo_box_destroy (GtkWidget *widget)
4489 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4491 if (combo_box->priv->popup_idle_id > 0)
4493 g_source_remove (combo_box->priv->popup_idle_id);
4494 combo_box->priv->popup_idle_id = 0;
4497 gtk_combo_box_popdown (combo_box);
4499 if (combo_box->priv->row_separator_destroy)
4500 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4502 combo_box->priv->row_separator_func = NULL;
4503 combo_box->priv->row_separator_data = NULL;
4504 combo_box->priv->row_separator_destroy = NULL;
4506 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4507 combo_box->priv->cell_view = NULL;
4511 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4514 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4517 * Fixes regression reported in bug #574059. The old functionality relied on
4518 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4519 * when the selection was already set to -1.
4521 if (gtk_combo_box_get_active(combo_box) == -1)
4522 g_signal_emit_by_name (combo_box, "changed");
4524 gtk_combo_box_set_active (combo_box, -1);
4528 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4531 GtkComboBoxPrivate *priv = combo_box->priv;
4532 GtkTreeModel *model;
4535 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4537 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4541 GValue value = {0,};
4543 g_signal_handlers_block_by_func (entry,
4544 gtk_combo_box_entry_contents_changed,
4547 model = gtk_combo_box_get_model (combo_box);
4549 gtk_tree_model_get_value (model, &iter,
4550 priv->text_column, &value);
4551 g_object_set_property (G_OBJECT (entry), "text", &value);
4552 g_value_unset (&value);
4554 g_signal_handlers_unblock_by_func (entry,
4555 gtk_combo_box_entry_contents_changed,
4562 gtk_combo_box_constructor (GType type,
4563 guint n_construct_properties,
4564 GObjectConstructParam *construct_properties)
4567 GtkComboBox *combo_box;
4568 GtkComboBoxPrivate *priv;
4570 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4571 (type, n_construct_properties, construct_properties);
4573 combo_box = GTK_COMBO_BOX (object);
4574 priv = combo_box->priv;
4578 priv->area = gtk_cell_area_box_new ();
4579 g_object_ref_sink (priv->area);
4582 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4583 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4584 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4585 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4586 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4587 gtk_widget_show (priv->cell_view);
4589 gtk_combo_box_check_appearance (combo_box);
4591 if (priv->has_entry)
4594 GtkStyleContext *context;
4596 entry = gtk_entry_new ();
4597 gtk_widget_show (entry);
4598 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4600 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4601 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4603 priv->text_renderer = gtk_cell_renderer_text_new ();
4604 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4605 priv->text_renderer, TRUE);
4607 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4609 g_signal_connect (combo_box, "changed",
4610 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4618 gtk_combo_box_dispose(GObject* object)
4620 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4622 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4624 gtk_combo_box_menu_destroy (combo_box);
4625 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4626 combo_box->priv->popup_widget = NULL;
4629 if (combo_box->priv->area)
4631 g_object_unref (combo_box->priv->area);
4632 combo_box->priv->area = NULL;
4635 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4636 gtk_combo_box_list_destroy (combo_box);
4638 if (combo_box->priv->popup_window)
4640 gtk_widget_destroy (combo_box->priv->popup_window);
4641 combo_box->priv->popup_window = NULL;
4644 gtk_combo_box_unset_model (combo_box);
4646 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4650 gtk_combo_box_finalize (GObject *object)
4652 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4654 g_free (combo_box->priv->tearoff_title);
4656 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4660 gtk_cell_editable_key_press (GtkWidget *widget,
4664 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4666 if (event->keyval == GDK_KEY_Escape)
4668 g_object_set (combo_box,
4669 "editing-canceled", TRUE,
4671 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4672 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4676 else if (event->keyval == GDK_KEY_Return ||
4677 event->keyval == GDK_KEY_ISO_Enter ||
4678 event->keyval == GDK_KEY_KP_Enter)
4680 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4681 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4690 popdown_idle (gpointer data)
4692 GtkComboBox *combo_box;
4694 combo_box = GTK_COMBO_BOX (data);
4696 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4697 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4699 g_object_unref (combo_box);
4705 popdown_handler (GtkWidget *widget,
4708 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4712 popup_idle (gpointer data)
4714 GtkComboBox *combo_box;
4716 combo_box = GTK_COMBO_BOX (data);
4718 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4719 combo_box->priv->cell_view)
4720 g_signal_connect_object (combo_box->priv->popup_widget,
4721 "unmap", G_CALLBACK (popdown_handler),
4724 /* we unset this if a menu item is activated */
4725 g_object_set (combo_box,
4726 "editing-canceled", TRUE,
4728 gtk_combo_box_popup (combo_box);
4730 combo_box->priv->popup_idle_id = 0;
4731 combo_box->priv->activate_button = 0;
4732 combo_box->priv->activate_time = 0;
4738 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4741 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4744 combo_box->priv->is_cell_renderer = TRUE;
4746 if (combo_box->priv->cell_view)
4748 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4749 G_CALLBACK (gtk_cell_editable_key_press),
4752 gtk_widget_grab_focus (combo_box->priv->button);
4756 child = gtk_bin_get_child (GTK_BIN (combo_box));
4758 g_signal_connect_object (child, "key-press-event",
4759 G_CALLBACK (gtk_cell_editable_key_press),
4762 gtk_widget_grab_focus (child);
4763 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4766 /* we do the immediate popup only for the optionmenu-like
4769 if (combo_box->priv->is_cell_renderer &&
4770 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4772 if (event && event->type == GDK_BUTTON_PRESS)
4774 GdkEventButton *event_button = (GdkEventButton *)event;
4776 combo_box->priv->activate_button = event_button->button;
4777 combo_box->priv->activate_time = event_button->time;
4780 combo_box->priv->popup_idle_id =
4781 gdk_threads_add_idle (popup_idle, combo_box);
4787 * gtk_combo_box_get_add_tearoffs:
4788 * @combo_box: a #GtkComboBox
4790 * Gets the current value of the :add-tearoffs property.
4792 * Return value: the current value of the :add-tearoffs property.
4795 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4797 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4799 return combo_box->priv->add_tearoffs;
4803 * gtk_combo_box_set_add_tearoffs:
4804 * @combo_box: a #GtkComboBox
4805 * @add_tearoffs: %TRUE to add tearoff menu items
4807 * Sets whether the popup menu should have a tearoff
4813 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4814 gboolean add_tearoffs)
4816 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4818 add_tearoffs = add_tearoffs != FALSE;
4820 if (combo_box->priv->add_tearoffs != add_tearoffs)
4822 combo_box->priv->add_tearoffs = add_tearoffs;
4823 gtk_combo_box_check_appearance (combo_box);
4825 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4826 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4827 combo_box->priv->add_tearoffs);
4829 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4834 * gtk_combo_box_get_title:
4835 * @combo_box: a #GtkComboBox
4837 * Gets the current title of the menu in tearoff mode. See
4838 * gtk_combo_box_set_add_tearoffs().
4840 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4841 * string which must not be freed.
4846 gtk_combo_box_get_title (GtkComboBox *combo_box)
4848 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4850 return combo_box->priv->tearoff_title;
4854 gtk_combo_box_update_title (GtkComboBox *combo_box)
4856 gtk_combo_box_check_appearance (combo_box);
4858 if (combo_box->priv->popup_widget &&
4859 GTK_IS_MENU (combo_box->priv->popup_widget))
4860 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4861 combo_box->priv->tearoff_title);
4865 * gtk_combo_box_set_title:
4866 * @combo_box: a #GtkComboBox
4867 * @title: a title for the menu in tearoff mode
4869 * Sets the menu's title in tearoff mode.
4874 gtk_combo_box_set_title (GtkComboBox *combo_box,
4877 GtkComboBoxPrivate *priv;
4879 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4881 priv = combo_box->priv;
4883 if (strcmp (title ? title : "",
4884 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4886 g_free (priv->tearoff_title);
4887 priv->tearoff_title = g_strdup (title);
4889 gtk_combo_box_update_title (combo_box);
4891 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4897 * gtk_combo_box_set_popup_fixed_width:
4898 * @combo_box: a #GtkComboBox
4899 * @fixed: whether to use a fixed popup width
4901 * Specifies whether the popup's width should be a fixed width
4902 * matching the allocated width of the combo box.
4907 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4910 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4912 if (combo_box->priv->popup_fixed_width != fixed)
4914 combo_box->priv->popup_fixed_width = fixed;
4916 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
4921 * gtk_combo_box_get_popup_fixed_width:
4922 * @combo_box: a #GtkComboBox
4924 * Gets whether the popup uses a fixed width matching
4925 * the allocated width of the combo box.
4927 * Returns: %TRUE if the popup uses a fixed width
4932 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
4934 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4936 return combo_box->priv->popup_fixed_width;
4941 * gtk_combo_box_get_popup_accessible:
4942 * @combo_box: a #GtkComboBox
4944 * Gets the accessible object corresponding to the combo box's popup.
4946 * This function is mostly intended for use by accessibility technologies;
4947 * applications should have little use for it.
4949 * Returns: (transfer none): the accessible object corresponding
4950 * to the combo box's popup.
4955 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
4959 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4961 if (combo_box->priv->popup_widget)
4963 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
4971 * gtk_combo_box_get_row_separator_func: (skip)
4972 * @combo_box: a #GtkComboBox
4974 * Returns the current row separator function.
4976 * Return value: the current row separator function.
4980 GtkTreeViewRowSeparatorFunc
4981 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
4983 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4985 return combo_box->priv->row_separator_func;
4989 * gtk_combo_box_set_row_separator_func:
4990 * @combo_box: a #GtkComboBox
4991 * @func: a #GtkTreeViewRowSeparatorFunc
4992 * @data: (allow-none): user data to pass to @func, or %NULL
4993 * @destroy: (allow-none): destroy notifier for @data, or %NULL
4995 * Sets the row separator function, which is used to determine
4996 * whether a row should be drawn as a separator. If the row separator
4997 * function is %NULL, no separators are drawn. This is the default value.
5002 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5003 GtkTreeViewRowSeparatorFunc func,
5005 GDestroyNotify destroy)
5007 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5009 if (combo_box->priv->row_separator_destroy)
5010 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5012 combo_box->priv->row_separator_func = func;
5013 combo_box->priv->row_separator_data = data;
5014 combo_box->priv->row_separator_destroy = destroy;
5016 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5017 if (combo_box->priv->tree_view)
5019 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5020 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5023 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5025 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5026 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5029 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5033 * gtk_combo_box_set_button_sensitivity:
5034 * @combo_box: a #GtkComboBox
5035 * @sensitivity: specify the sensitivity of the dropdown button
5037 * Sets whether the dropdown button of the combo box should be
5038 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5039 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5044 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5045 GtkSensitivityType sensitivity)
5047 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5049 if (combo_box->priv->button_sensitivity != sensitivity)
5051 combo_box->priv->button_sensitivity = sensitivity;
5052 gtk_combo_box_update_sensitivity (combo_box);
5054 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5059 * gtk_combo_box_get_button_sensitivity:
5060 * @combo_box: a #GtkComboBox
5062 * Returns whether the combo box sets the dropdown button
5063 * sensitive or not when there are no items in the model.
5065 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5066 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5067 * if the button is always insensitive or
5068 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5069 * the model has one item to be selected.
5074 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5076 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5078 return combo_box->priv->button_sensitivity;
5083 * gtk_combo_box_get_has_entry:
5084 * @combo_box: a #GtkComboBox
5086 * Returns whether the combo box has an entry.
5088 * Return Value: whether there is an entry in @combo_box.
5093 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5095 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5097 return combo_box->priv->has_entry;
5101 * gtk_combo_box_set_entry_text_column:
5102 * @combo_box: A #GtkComboBox
5103 * @text_column: A column in @model to get the strings from for
5104 * the internal entry
5106 * Sets the model column which @combo_box should use to get strings from
5107 * to be @text_column. The column @text_column in the model of @combo_box
5108 * must be of type %G_TYPE_STRING.
5110 * This is only relevant if @combo_box has been created with
5111 * #GtkComboBox:has-entry as %TRUE.
5116 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5119 GtkComboBoxPrivate *priv = combo_box->priv;
5120 GtkTreeModel *model;
5122 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5124 model = gtk_combo_box_get_model (combo_box);
5126 g_return_if_fail (text_column >= 0);
5127 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5129 priv->text_column = text_column;
5131 if (priv->text_renderer != NULL)
5132 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5133 priv->text_renderer,
5134 "text", text_column,
5139 * gtk_combo_box_get_entry_text_column:
5140 * @combo_box: A #GtkComboBox.
5142 * Returns the column which @combo_box is using to get the strings
5143 * from to display in the internal entry.
5145 * Return value: A column in the data source model of @combo_box.
5150 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5152 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5154 return combo_box->priv->text_column;
5158 * gtk_combo_box_set_focus_on_click:
5159 * @combo: a #GtkComboBox
5160 * @focus_on_click: whether the combo box grabs focus when clicked
5163 * Sets whether the combo box will grab focus when it is clicked with
5164 * the mouse. Making mouse clicks not grab focus is useful in places
5165 * like toolbars where you don't want the keyboard focus removed from
5166 * the main area of the application.
5171 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5172 gboolean focus_on_click)
5174 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5176 focus_on_click = focus_on_click != FALSE;
5178 if (combo_box->priv->focus_on_click != focus_on_click)
5180 combo_box->priv->focus_on_click = focus_on_click;
5182 if (combo_box->priv->button)
5183 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5186 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5191 * gtk_combo_box_get_focus_on_click:
5192 * @combo: a #GtkComboBox
5194 * Returns whether the combo box grabs focus when it is clicked
5195 * with the mouse. See gtk_combo_box_set_focus_on_click().
5197 * Return value: %TRUE if the combo box grabs focus when it is
5198 * clicked with the mouse.
5203 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5205 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5207 return combo_box->priv->focus_on_click;
5212 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5213 GtkBuilder *builder,
5215 const gchar *tagname,
5216 GMarkupParser *parser,
5219 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5220 tagname, parser, data))
5223 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5224 tagname, parser, data);
5228 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5229 GtkBuilder *builder,
5231 const gchar *tagname,
5234 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5235 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5239 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5240 GtkBuilder *builder,
5241 const gchar *childname)
5243 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5245 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5246 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5248 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5252 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5256 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5257 GtkComboBoxPrivate *priv = combo_box->priv;
5258 gint focus_width, focus_pad;
5259 gint font_size, arrow_size;
5260 PangoContext *context;
5261 PangoFontMetrics *metrics;
5262 const PangoFontDescription *font_desc;
5264 gint minimum_width = 0, natural_width = 0;
5265 gint child_min, child_nat;
5266 GtkStyleContext *style_context;
5267 GtkStateFlags state;
5269 gfloat arrow_scaling;
5271 child = gtk_bin_get_child (GTK_BIN (widget));
5274 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5276 gtk_widget_style_get (GTK_WIDGET (widget),
5277 "focus-line-width", &focus_width,
5278 "focus-padding", &focus_pad,
5279 "arrow-size", &arrow_size,
5280 "arrow-scaling", &arrow_scaling,
5283 style_context = gtk_widget_get_style_context (widget);
5284 state = gtk_widget_get_state_flags (widget);
5286 get_widget_padding (widget, &padding);
5287 font_desc = gtk_style_context_get_font (style_context, state);
5289 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5290 metrics = pango_context_get_metrics (context, font_desc,
5291 pango_context_get_language (context));
5292 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5293 pango_font_metrics_get_descent (metrics));
5294 pango_font_metrics_unref (metrics);
5296 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5298 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5300 if (!priv->tree_view)
5303 if (priv->cell_view)
5305 gint sep_width, arrow_width;
5306 gint border_width, xpad;
5307 GtkBorder button_padding;
5309 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5310 get_widget_padding (priv->button, &button_padding);
5312 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5313 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5315 xpad = 2 * (border_width + focus_width + focus_pad) +
5316 button_padding.left + button_padding.right + padding.left + padding.right;
5318 minimum_width = child_min + sep_width + arrow_width + xpad;
5319 natural_width = child_nat + sep_width + arrow_width + xpad;
5323 gint but_width, but_nat_width;
5325 gtk_widget_get_preferred_width (priv->button,
5326 &but_width, &but_nat_width);
5328 minimum_width = child_min + but_width;
5329 natural_width = child_nat + but_nat_width;
5335 gint button_width, button_nat_width;
5337 /* sample + frame */
5338 minimum_width = child_min;
5339 natural_width = child_nat;
5341 minimum_width += 2 * focus_width;
5342 natural_width += 2 * focus_width;
5344 if (priv->cell_view_frame)
5346 if (priv->has_frame)
5348 gint border_width, xpad;
5349 GtkBorder frame_padding;
5351 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5352 get_widget_padding (priv->cell_view_frame, &frame_padding);
5353 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5355 minimum_width += xpad;
5356 natural_width += xpad;
5361 gtk_widget_get_preferred_width (priv->button,
5362 &button_width, &button_nat_width);
5364 minimum_width += button_width;
5365 natural_width += button_nat_width;
5368 minimum_width += padding.left + padding.right;
5369 natural_width += padding.left + padding.right;
5372 *minimum_size = minimum_width;
5375 *natural_size = natural_width;
5379 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5385 /* Combo box is height-for-width only
5386 * (so we always just reserve enough height for the minimum width) */
5387 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5388 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5392 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5397 /* Combo box is height-for-width only
5398 * (so we assume we always reserved enough height for the minimum width) */
5399 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5404 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5409 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5410 GtkComboBoxPrivate *priv = combo_box->priv;
5411 gint focus_width, focus_pad;
5412 gint min_height = 0, nat_height = 0;
5417 gtk_widget_style_get (GTK_WIDGET (widget),
5418 "focus-line-width", &focus_width,
5419 "focus-padding", &focus_pad,
5422 child = gtk_bin_get_child (GTK_BIN (widget));
5424 get_widget_padding (widget, &padding);
5427 if (!priv->tree_view)
5430 if (priv->cell_view)
5432 /* calculate x/y padding and separator/arrow size */
5433 gint sep_width, arrow_width, sep_height, arrow_height;
5434 gint border_width, xpad, ypad;
5435 GtkBorder button_padding;
5437 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5438 get_widget_padding (priv->button, &button_padding);
5440 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5441 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5442 gtk_widget_get_preferred_height_for_width (priv->separator,
5443 sep_width, &sep_height, NULL);
5444 gtk_widget_get_preferred_height_for_width (priv->arrow,
5445 arrow_width, &arrow_height, NULL);
5447 xpad = 2 * (border_width + focus_width + focus_pad) +
5448 button_padding.left + button_padding.right;
5449 ypad = 2 * (border_width + focus_width + focus_pad) +
5450 button_padding.top + button_padding.bottom;
5452 size -= sep_width + arrow_width + xpad;
5454 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5455 * and fitting the whole treemodel */
5456 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5458 arrow_height = MAX (arrow_height, sep_height);
5459 min_height = MAX (min_height, arrow_height);
5460 nat_height = MAX (nat_height, arrow_height);
5467 /* there is a custom child widget inside (no priv->cell_view) */
5468 gint but_width, but_height;
5470 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5471 gtk_widget_get_preferred_height_for_width (priv->button,
5472 but_width, &but_height, NULL);
5476 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5477 * and fitting the whole treemodel */
5478 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5480 min_height = MAX (min_height, but_height);
5481 nat_height = MAX (nat_height, but_height);
5487 gint but_width, but_height;
5488 gint xpad = 0, ypad = 0;
5490 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5491 gtk_widget_get_preferred_height_for_width (priv->button,
5492 but_width, &but_height, NULL);
5494 if (priv->cell_view_frame && priv->has_frame)
5496 GtkBorder frame_padding;
5499 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5500 get_widget_padding (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5502 xpad = (2 * border_width) + padding.left + frame_padding.right;
5503 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5507 size -= 2 * focus_width;
5510 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5511 * and fitting the whole treemodel */
5512 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5514 min_height = MAX (min_height, but_height);
5515 nat_height = MAX (nat_height, but_height);
5521 min_height += padding.top + padding.bottom;
5522 nat_height += padding.top + padding.bottom;
5525 *minimum_size = min_height;
5528 *natural_size = nat_height;
5532 * gtk_combo_box_set_id_column:
5533 * @combo_box: A #GtkComboBox
5534 * @id_column: A column in @model to get string IDs for values from
5536 * Sets the model column which @combo_box should use to get string IDs
5537 * for values from. The column @id_column in the model of @combo_box
5538 * must be of type %G_TYPE_STRING.
5543 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5546 GtkComboBoxPrivate *priv = combo_box->priv;
5547 GtkTreeModel *model;
5549 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5551 if (id_column != priv->id_column)
5553 model = gtk_combo_box_get_model (combo_box);
5555 g_return_if_fail (id_column >= 0);
5556 g_return_if_fail (model == NULL ||
5557 id_column < gtk_tree_model_get_n_columns (model));
5559 priv->id_column = id_column;
5561 g_object_notify (G_OBJECT (combo_box), "id-column");
5562 g_object_notify (G_OBJECT (combo_box), "active-id");
5567 * gtk_combo_box_get_id_column:
5568 * @combo_box: A #GtkComboBox
5570 * Returns the column which @combo_box is using to get string IDs
5573 * Return value: A column in the data source model of @combo_box.
5578 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5580 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5582 return combo_box->priv->id_column;
5586 * gtk_combo_box_get_active_id:
5587 * @combo_box: a #GtkComboBox
5589 * Returns the ID of the active row of @combo_box. This value is taken
5590 * from the active row and the column specified by the #GtkComboBox:id-column
5591 * property of @combo_box (see gtk_combo_box_set_id_column()).
5593 * The returned value is an interned string which means that you can
5594 * compare the pointer by value to other interned strings and that you
5597 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5598 * no row is active, or if the active row has a %NULL ID value, then %NULL
5601 * Return value: the ID of the active row, or %NULL
5606 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5608 GtkTreeModel *model;
5612 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5614 column = combo_box->priv->id_column;
5619 model = gtk_combo_box_get_model (combo_box);
5620 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5621 G_TYPE_STRING, NULL);
5623 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5625 const gchar *interned;
5628 gtk_tree_model_get (model, &iter, column, &id, -1);
5629 interned = g_intern_string (id);
5639 * gtk_combo_box_set_active_id:
5640 * @combo_box: a #GtkComboBox
5641 * @active_id: (allow-none): the ID of the row to select, or %NULL
5643 * Changes the active row of @combo_box to the one that has an ID equal to
5644 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5645 * a %NULL ID string cannot be made active by this function.
5647 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5648 * row has the given ID then the function does nothing and returns %FALSE.
5650 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5651 * @active_id was given to unset the active row, the function
5652 * always returns %TRUE.
5657 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5658 const gchar *active_id)
5660 GtkTreeModel *model;
5662 gboolean match = FALSE;
5665 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5667 if (active_id == NULL)
5669 gtk_combo_box_set_active (combo_box, -1);
5670 return TRUE; /* active row was successfully unset */
5673 column = combo_box->priv->id_column;
5678 model = gtk_combo_box_get_model (combo_box);
5679 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5680 G_TYPE_STRING, FALSE);
5682 if (gtk_tree_model_get_iter_first (model, &iter))
5686 gtk_tree_model_get (model, &iter, column, &id, -1);
5688 match = strcmp (id, active_id) == 0;
5693 gtk_combo_box_set_active_iter (combo_box, &iter);
5696 } while (gtk_tree_model_iter_next (model, &iter));