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 "gtkvseparator.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"
60 * @Short_description: A widget used to choose from a list of items
62 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
64 * A GtkComboBox is a widget that allows the user to choose from a list of
65 * valid choices. The GtkComboBox displays the selected choice. When
66 * activated, the GtkComboBox displays a popup which allows the user to
67 * make a new choice. The style in which the selected value is displayed,
68 * and the style of the popup is determined by the current theme. It may
69 * be similar to a Windows-style combo box.
71 * The GtkComboBox uses the model-view pattern; the list of valid choices
72 * is specified in the form of a tree model, and the display of the choices
73 * can be adapted to the data in the model by using cell renderers, as you
74 * would in a tree view. This is possible since GtkComboBox implements the
75 * #GtkCellLayout interface. The tree model holding the valid choices is
76 * not restricted to a flat list, it can be a real tree, and the popup will
77 * reflect the tree structure.
79 * To allow the user to enter values not in the model, the 'has-entry'
80 * property allows the GtkComboBox to contain a #GtkEntry. This entry
81 * can be accessed by calling gtk_bin_get_child() on the combo box.
83 * For a simple list of textual choices, the model-view API of GtkComboBox
84 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
85 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
90 /* WELCOME, to THE house of evil code */
91 struct _GtkComboBoxPrivate
101 GtkShadowType shadow_type;
103 gint active; /* Only temporary */
104 GtkTreeRowReference *active_row;
106 GtkWidget *tree_view;
108 GtkWidget *cell_view;
109 GtkWidget *cell_view_frame;
114 GtkWidget *separator;
116 GtkWidget *popup_widget;
117 GtkWidget *popup_window;
118 GtkWidget *scrolled_window;
125 guint activate_button;
126 guint32 activate_time;
128 guint resize_idle_id;
130 /* For "has-entry" specific behavior we track
131 * an automated cell renderer and text column
134 GtkCellRenderer *text_renderer;
138 guint popup_in_progress : 1;
139 guint popup_shown : 1;
140 guint add_tearoffs : 1;
142 guint is_cell_renderer : 1;
143 guint editing_canceled : 1;
144 guint auto_scroll : 1;
145 guint focus_on_click : 1;
146 guint button_sensitivity : 2;
148 guint popup_fixed_width : 1;
150 GtkTreeViewRowSeparatorFunc row_separator_func;
151 gpointer row_separator_data;
152 GDestroyNotify row_separator_destroy;
154 GdkDevice *grab_pointer;
155 GdkDevice *grab_keyboard;
157 gchar *tearoff_title;
160 /* While debugging this evil code, I have learned that
161 * there are actually 4 modes to this widget, which can
162 * be characterized as follows
164 * 1) menu mode, no child added
167 * cell_view -> GtkCellView, regular child
168 * cell_view_frame -> NULL
169 * button -> GtkToggleButton set_parent to combo
170 * arrow -> GtkArrow set_parent to button
171 * separator -> GtkVSepator set_parent to button
172 * popup_widget -> GtkMenu
173 * popup_window -> NULL
174 * scrolled_window -> NULL
176 * 2) menu mode, child added
180 * cell_view_frame -> NULL
181 * button -> GtkToggleButton set_parent to combo
182 * arrow -> GtkArrow, child of button
184 * popup_widget -> GtkMenu
185 * popup_window -> NULL
186 * scrolled_window -> NULL
188 * 3) list mode, no child added
190 * tree_view -> GtkTreeView, child of scrolled_window
191 * cell_view -> GtkCellView, regular child
192 * cell_view_frame -> GtkFrame, set parent to combo
193 * button -> GtkToggleButton, set_parent to combo
194 * arrow -> GtkArrow, child of button
196 * popup_widget -> tree_view
197 * popup_window -> GtkWindow
198 * scrolled_window -> GtkScrolledWindow, child of popup_window
200 * 4) list mode, child added
202 * tree_view -> GtkTreeView, child of scrolled_window
204 * cell_view_frame -> NULL
205 * button -> GtkToggleButton, set_parent to combo
206 * arrow -> GtkArrow, child of button
208 * popup_widget -> tree_view
209 * popup_window -> GtkWindow
210 * scrolled_window -> GtkScrolledWindow, child of popup_window
226 PROP_ROW_SPAN_COLUMN,
227 PROP_COLUMN_SPAN_COLUMN,
234 PROP_BUTTON_SENSITIVITY,
235 PROP_EDITING_CANCELED,
237 PROP_ENTRY_TEXT_COLUMN,
238 PROP_POPUP_FIXED_WIDTH,
244 static guint combo_box_signals[LAST_SIGNAL] = {0,};
246 #define BONUS_PADDING 4
247 #define SCROLL_TIME 100
251 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
252 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
253 static GObject *gtk_combo_box_constructor (GType type,
254 guint n_construct_properties,
255 GObjectConstructParam *construct_properties);
256 static void gtk_combo_box_dispose (GObject *object);
257 static void gtk_combo_box_finalize (GObject *object);
258 static void gtk_combo_box_destroy (GtkWidget *widget);
260 static void gtk_combo_box_set_property (GObject *object,
264 static void gtk_combo_box_get_property (GObject *object,
269 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
270 GtkStateFlags previous);
271 static void gtk_combo_box_grab_focus (GtkWidget *widget);
272 static void gtk_combo_box_style_updated (GtkWidget *widget);
273 static void gtk_combo_box_button_toggled (GtkWidget *widget,
275 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
276 GtkStateFlags previous,
278 static void gtk_combo_box_add (GtkContainer *container,
280 static void gtk_combo_box_remove (GtkContainer *container,
283 static void gtk_combo_box_menu_show (GtkWidget *menu,
285 static void gtk_combo_box_menu_hide (GtkWidget *menu,
288 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
290 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
295 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
300 static void gtk_combo_box_menu_position (GtkMenu *menu,
306 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
308 static void gtk_combo_box_size_allocate (GtkWidget *widget,
309 GtkAllocation *allocation);
310 static void gtk_combo_box_forall (GtkContainer *container,
311 gboolean include_internals,
312 GtkCallback callback,
313 gpointer callback_data);
314 static gboolean gtk_combo_box_draw (GtkWidget *widget,
316 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
317 GdkEventScroll *event);
318 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
321 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
322 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
323 GtkScrollType scroll);
324 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
325 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
327 /* listening to the model */
328 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
332 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
335 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
340 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
344 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
350 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
355 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
356 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
358 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
359 GdkEventButton *event,
361 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
364 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
365 GdkEventCrossing *event,
367 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
370 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
371 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
372 GdkEventButton *event,
375 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
378 gboolean path_currently_selected,
381 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
385 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
388 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
389 gboolean add_children);
390 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
391 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
394 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
395 GdkEventButton *event,
397 static void gtk_combo_box_menu_activate (GtkWidget *menu,
399 GtkComboBox *combo_box);
400 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
401 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
404 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
406 guint32 activate_time);
409 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
411 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
412 gboolean group_cycling);
414 static void gtk_combo_box_child_show (GtkWidget *widget,
415 GtkComboBox *combo_box);
416 static void gtk_combo_box_child_hide (GtkWidget *widget,
417 GtkComboBox *combo_box);
419 /* GtkComboBox:has-entry callbacks */
420 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
422 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
426 /* GtkBuildable method implementation */
427 static GtkBuildableIface *parent_buildable_iface;
429 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
430 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
433 const gchar *tagname,
434 GMarkupParser *parser,
436 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
439 const gchar *tagname,
441 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
443 const gchar *childname);
446 /* GtkCellEditable method implementations */
447 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
450 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
453 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
456 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
460 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
466 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
467 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
468 gtk_combo_box_cell_layout_init)
469 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
470 gtk_combo_box_cell_editable_init)
471 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
472 gtk_combo_box_buildable_init))
477 gtk_combo_box_class_init (GtkComboBoxClass *klass)
479 GObjectClass *object_class;
480 GtkContainerClass *container_class;
481 GtkWidgetClass *widget_class;
482 GtkBindingSet *binding_set;
484 container_class = (GtkContainerClass *)klass;
485 container_class->forall = gtk_combo_box_forall;
486 container_class->add = gtk_combo_box_add;
487 container_class->remove = gtk_combo_box_remove;
489 widget_class = (GtkWidgetClass *)klass;
490 widget_class->size_allocate = gtk_combo_box_size_allocate;
491 widget_class->draw = gtk_combo_box_draw;
492 widget_class->scroll_event = gtk_combo_box_scroll_event;
493 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
494 widget_class->grab_focus = gtk_combo_box_grab_focus;
495 widget_class->style_updated = gtk_combo_box_style_updated;
496 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
497 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
498 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
499 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
500 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
501 widget_class->destroy = gtk_combo_box_destroy;
503 object_class = (GObjectClass *)klass;
504 object_class->constructor = gtk_combo_box_constructor;
505 object_class->dispose = gtk_combo_box_dispose;
506 object_class->finalize = gtk_combo_box_finalize;
507 object_class->set_property = gtk_combo_box_set_property;
508 object_class->get_property = gtk_combo_box_get_property;
512 * GtkComboBox::changed:
513 * @widget: the object which received the signal
515 * The changed signal is emitted when the active
516 * item is changed. The can be due to the user selecting
517 * a different item from the list, or due to a
518 * call to gtk_combo_box_set_active_iter().
519 * It will also be emitted while typing into the entry of a combo box
524 combo_box_signals[CHANGED] =
525 g_signal_new (I_("changed"),
526 G_OBJECT_CLASS_TYPE (klass),
528 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
530 g_cclosure_marshal_VOID__VOID,
533 * GtkComboBox::move-active:
534 * @widget: the object that received the signal
535 * @scroll_type: a #GtkScrollType
537 * The ::move-active signal is a
538 * <link linkend="keybinding-signals">keybinding signal</link>
539 * which gets emitted to move the active selection.
543 combo_box_signals[MOVE_ACTIVE] =
544 g_signal_new_class_handler (I_("move-active"),
545 G_OBJECT_CLASS_TYPE (klass),
546 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
547 G_CALLBACK (gtk_combo_box_real_move_active),
549 g_cclosure_marshal_VOID__ENUM,
551 GTK_TYPE_SCROLL_TYPE);
554 * GtkComboBox::popup:
555 * @widget: the object that received the signal
557 * The ::popup signal is a
558 * <link linkend="keybinding-signals">keybinding signal</link>
559 * which gets emitted to popup the combo box list.
561 * The default binding for this signal is Alt+Down.
565 combo_box_signals[POPUP] =
566 g_signal_new_class_handler (I_("popup"),
567 G_OBJECT_CLASS_TYPE (klass),
568 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
569 G_CALLBACK (gtk_combo_box_real_popup),
571 g_cclosure_marshal_VOID__VOID,
574 * GtkComboBox::popdown:
575 * @button: the object which received the signal
577 * The ::popdown signal is a
578 * <link linkend="keybinding-signals">keybinding signal</link>
579 * which gets emitted to popdown the combo box list.
581 * The default bindings for this signal are Alt+Up and Escape.
585 combo_box_signals[POPDOWN] =
586 g_signal_new_class_handler (I_("popdown"),
587 G_OBJECT_CLASS_TYPE (klass),
588 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
589 G_CALLBACK (gtk_combo_box_real_popdown),
591 _gtk_marshal_BOOLEAN__VOID,
595 binding_set = gtk_binding_set_by_class (widget_class);
597 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
599 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
602 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
604 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
606 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
609 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
611 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
612 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
614 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
615 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
617 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
618 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
620 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
621 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
623 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
624 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
626 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
628 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
630 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
631 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
633 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
634 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
636 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
637 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
639 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
640 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
642 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
643 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
645 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
648 g_object_class_override_property (object_class,
649 PROP_EDITING_CANCELED,
655 * The model from which the combo box takes the values shown
660 g_object_class_install_property (object_class,
662 g_param_spec_object ("model",
663 P_("ComboBox model"),
664 P_("The model for the combo box"),
666 GTK_PARAM_READWRITE));
669 * GtkComboBox:wrap-width:
671 * If wrap-width is set to a positive value, the list will be
672 * displayed in multiple columns, the number of columns is
673 * determined by wrap-width.
677 g_object_class_install_property (object_class,
679 g_param_spec_int ("wrap-width",
681 P_("Wrap width for laying out the items in a grid"),
685 GTK_PARAM_READWRITE));
689 * GtkComboBox:row-span-column:
691 * If this is set to a non-negative value, it must be the index of a column
692 * of type %G_TYPE_INT in the model.
694 * The values of that column are used to determine how many rows a value in
695 * the list will span. Therefore, the values in the model column pointed to
696 * by this property must be greater than zero and not larger than wrap-width.
700 g_object_class_install_property (object_class,
701 PROP_ROW_SPAN_COLUMN,
702 g_param_spec_int ("row-span-column",
703 P_("Row span column"),
704 P_("TreeModel column containing the row span values"),
708 GTK_PARAM_READWRITE));
712 * GtkComboBox:column-span-column:
714 * If this is set to a non-negative value, it must be the index of a column
715 * of type %G_TYPE_INT in the model.
717 * The values of that column are used to determine how many columns a value
718 * in the list will span.
722 g_object_class_install_property (object_class,
723 PROP_COLUMN_SPAN_COLUMN,
724 g_param_spec_int ("column-span-column",
725 P_("Column span column"),
726 P_("TreeModel column containing the column span values"),
730 GTK_PARAM_READWRITE));
734 * GtkComboBox:active:
736 * The item which is currently active. If the model is a non-flat treemodel,
737 * and the active item is not an immediate child of the root of the tree,
738 * this property has the value
739 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
740 * where <literal>path</literal> is the #GtkTreePath of the active item.
744 g_object_class_install_property (object_class,
746 g_param_spec_int ("active",
748 P_("The item which is currently active"),
752 GTK_PARAM_READWRITE));
755 * GtkComboBox:add-tearoffs:
757 * The add-tearoffs property controls whether generated menus
758 * have tearoff menu items.
760 * Note that this only affects menu style combo boxes.
764 g_object_class_install_property (object_class,
766 g_param_spec_boolean ("add-tearoffs",
767 P_("Add tearoffs to menus"),
768 P_("Whether dropdowns should have a tearoff menu item"),
770 GTK_PARAM_READWRITE));
773 * GtkComboBox:has-frame:
775 * The has-frame property controls whether a frame
776 * is drawn around the entry.
780 g_object_class_install_property (object_class,
782 g_param_spec_boolean ("has-frame",
784 P_("Whether the combo box draws a frame around the child"),
786 GTK_PARAM_READWRITE));
788 g_object_class_install_property (object_class,
790 g_param_spec_boolean ("focus-on-click",
791 P_("Focus on click"),
792 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
794 GTK_PARAM_READWRITE));
797 * GtkComboBox:tearoff-title:
799 * A title that may be displayed by the window manager
800 * when the popup is torn-off.
804 g_object_class_install_property (object_class,
806 g_param_spec_string ("tearoff-title",
808 P_("A title that may be displayed by the window manager when the popup is torn-off"),
810 GTK_PARAM_READWRITE));
814 * GtkComboBox:popup-shown:
816 * Whether the combo boxes dropdown is popped up.
817 * Note that this property is mainly useful, because
818 * it allows you to connect to notify::popup-shown.
822 g_object_class_install_property (object_class,
824 g_param_spec_boolean ("popup-shown",
826 P_("Whether the combo's dropdown is shown"),
828 GTK_PARAM_READABLE));
832 * GtkComboBox:button-sensitivity:
834 * Whether the dropdown button is sensitive when
835 * the model is empty.
839 g_object_class_install_property (object_class,
840 PROP_BUTTON_SENSITIVITY,
841 g_param_spec_enum ("button-sensitivity",
842 P_("Button Sensitivity"),
843 P_("Whether the dropdown button is sensitive when the model is empty"),
844 GTK_TYPE_SENSITIVITY_TYPE,
845 GTK_SENSITIVITY_AUTO,
846 GTK_PARAM_READWRITE));
849 * GtkComboBox:has-entry:
851 * Whether the combo box has an entry.
855 g_object_class_install_property (object_class,
857 g_param_spec_boolean ("has-entry",
859 P_("Whether combo box has an entry"),
861 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
864 * GtkComboBox:entry-text-column:
866 * The column in the combo box's model to associate with strings from the entry
867 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
871 g_object_class_install_property (object_class,
872 PROP_ENTRY_TEXT_COLUMN,
873 g_param_spec_int ("entry-text-column",
874 P_("Entry Text Column"),
875 P_("The column in the combo box's model to associate "
876 "with strings from the entry if the combo was "
877 "created with #GtkComboBox:has-entry = %TRUE"),
879 GTK_PARAM_READWRITE));
882 * GtkComboBox:id-column:
884 * The column in the combo box's model that provides string
885 * IDs for the values in the model, if != -1.
889 g_object_class_install_property (object_class,
891 g_param_spec_int ("id-column",
893 P_("The column in the combo box's model that provides "
894 "string IDs for the values in the model"),
896 GTK_PARAM_READWRITE));
899 * GtkComboBox:active-id:
901 * The value of the ID column of the active row.
905 g_object_class_install_property (object_class,
907 g_param_spec_string ("active-id",
909 P_("The value of the id column "
910 "for the active row"),
911 NULL, GTK_PARAM_READWRITE));
914 * GtkComboBox:popup-fixed-width:
916 * Whether the popup's width should be a fixed width matching the
917 * allocated width of the combo box.
921 g_object_class_install_property (object_class,
922 PROP_POPUP_FIXED_WIDTH,
923 g_param_spec_boolean ("popup-fixed-width",
924 P_("Popup Fixed Width"),
925 P_("Whether the popup's width should be a "
926 "fixed width matching the allocated width "
929 GTK_PARAM_READWRITE));
932 * GtkComboBox:cell-area:
934 * The #GtkCellArea used to layout cell renderers for this combo box.
936 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
937 * a horizontally oriented #GtkCellAreaBox will be used.
941 g_object_class_install_property (object_class,
943 g_param_spec_object ("cell-area",
945 P_("The GtkCellArea used to layout cells"),
947 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
949 gtk_widget_class_install_style_property (widget_class,
950 g_param_spec_boolean ("appears-as-list",
951 P_("Appears as list"),
952 P_("Whether dropdowns should look like lists rather than menus"),
954 GTK_PARAM_READABLE));
957 * GtkComboBox:arrow-size:
959 * Sets the minimum size of the arrow in the combo box. Note
960 * that the arrow size is coupled to the font size, so in case
961 * a larger font is used, the arrow will be larger than set
966 gtk_widget_class_install_style_property (widget_class,
967 g_param_spec_int ("arrow-size",
969 P_("The minimum size of the arrow in the combo box"),
973 GTK_PARAM_READABLE));
976 * GtkComboBox:arrow-scaling:
978 * Sets the amount of space used up by the combobox arrow,
979 * proportional to the font size.
983 gtk_widget_class_install_style_property (widget_class,
984 g_param_spec_float ("arrow-scaling",
986 P_("The amount of space used by the arrow"),
990 GTK_PARAM_READABLE));
993 * GtkComboBox:shadow-type:
995 * Which kind of shadow to draw around the combo box.
999 gtk_widget_class_install_style_property (widget_class,
1000 g_param_spec_enum ("shadow-type",
1002 P_("Which kind of shadow to draw around the combo box"),
1003 GTK_TYPE_SHADOW_TYPE,
1005 GTK_PARAM_READABLE));
1007 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1011 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1013 parent_buildable_iface = g_type_interface_peek_parent (iface);
1014 iface->add_child = _gtk_cell_layout_buildable_add_child;
1015 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1016 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1017 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1021 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1023 iface->get_area = gtk_combo_box_cell_layout_get_area;
1027 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1029 iface->start_editing = gtk_combo_box_start_editing;
1033 gtk_combo_box_init (GtkComboBox *combo_box)
1035 GtkComboBoxPrivate *priv;
1037 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1039 GtkComboBoxPrivate);
1040 priv = combo_box->priv;
1042 priv->wrap_width = 0;
1045 priv->active_row = NULL;
1046 priv->col_column = -1;
1047 priv->row_column = -1;
1049 priv->popup_shown = FALSE;
1050 priv->add_tearoffs = FALSE;
1051 priv->has_frame = TRUE;
1052 priv->is_cell_renderer = FALSE;
1053 priv->editing_canceled = FALSE;
1054 priv->auto_scroll = FALSE;
1055 priv->focus_on_click = TRUE;
1056 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1057 priv->has_entry = FALSE;
1058 priv->popup_fixed_width = TRUE;
1060 priv->text_column = -1;
1061 priv->text_renderer = NULL;
1062 priv->id_column = -1;
1066 gtk_combo_box_set_property (GObject *object,
1068 const GValue *value,
1071 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1072 GtkComboBoxPrivate *priv = combo_box->priv;
1078 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1081 case PROP_WRAP_WIDTH:
1082 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1085 case PROP_ROW_SPAN_COLUMN:
1086 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1089 case PROP_COLUMN_SPAN_COLUMN:
1090 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1094 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1097 case PROP_ADD_TEAROFFS:
1098 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1101 case PROP_HAS_FRAME:
1102 priv->has_frame = g_value_get_boolean (value);
1104 if (priv->has_entry)
1108 child = gtk_bin_get_child (GTK_BIN (combo_box));
1110 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1115 case PROP_FOCUS_ON_CLICK:
1116 gtk_combo_box_set_focus_on_click (combo_box,
1117 g_value_get_boolean (value));
1120 case PROP_TEAROFF_TITLE:
1121 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1124 case PROP_POPUP_SHOWN:
1125 if (g_value_get_boolean (value))
1126 gtk_combo_box_popup (combo_box);
1128 gtk_combo_box_popdown (combo_box);
1131 case PROP_BUTTON_SENSITIVITY:
1132 gtk_combo_box_set_button_sensitivity (combo_box,
1133 g_value_get_enum (value));
1136 case PROP_POPUP_FIXED_WIDTH:
1137 gtk_combo_box_set_popup_fixed_width (combo_box,
1138 g_value_get_boolean (value));
1141 case PROP_EDITING_CANCELED:
1142 priv->editing_canceled = g_value_get_boolean (value);
1145 case PROP_HAS_ENTRY:
1146 priv->has_entry = g_value_get_boolean (value);
1149 case PROP_ENTRY_TEXT_COLUMN:
1150 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1153 case PROP_ID_COLUMN:
1154 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1157 case PROP_ACTIVE_ID:
1158 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1161 case PROP_CELL_AREA:
1162 /* Construct-only, can only be assigned once */
1163 area = g_value_get_object (value);
1166 if (priv->area != NULL)
1168 g_warning ("cell-area has already been set, ignoring construct property");
1169 g_object_ref_sink (area);
1170 g_object_unref (area);
1173 priv->area = g_object_ref_sink (area);
1178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1184 gtk_combo_box_get_property (GObject *object,
1189 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1190 GtkComboBoxPrivate *priv = combo_box->priv;
1195 g_value_set_object (value, combo_box->priv->model);
1198 case PROP_WRAP_WIDTH:
1199 g_value_set_int (value, combo_box->priv->wrap_width);
1202 case PROP_ROW_SPAN_COLUMN:
1203 g_value_set_int (value, combo_box->priv->row_column);
1206 case PROP_COLUMN_SPAN_COLUMN:
1207 g_value_set_int (value, combo_box->priv->col_column);
1211 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1214 case PROP_ADD_TEAROFFS:
1215 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1218 case PROP_HAS_FRAME:
1219 g_value_set_boolean (value, combo_box->priv->has_frame);
1222 case PROP_FOCUS_ON_CLICK:
1223 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1226 case PROP_TEAROFF_TITLE:
1227 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1230 case PROP_POPUP_SHOWN:
1231 g_value_set_boolean (value, combo_box->priv->popup_shown);
1234 case PROP_BUTTON_SENSITIVITY:
1235 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1238 case PROP_POPUP_FIXED_WIDTH:
1239 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1242 case PROP_EDITING_CANCELED:
1243 g_value_set_boolean (value, priv->editing_canceled);
1246 case PROP_HAS_ENTRY:
1247 g_value_set_boolean (value, priv->has_entry);
1250 case PROP_ENTRY_TEXT_COLUMN:
1251 g_value_set_int (value, priv->text_column);
1254 case PROP_ID_COLUMN:
1255 g_value_set_int (value, priv->id_column);
1258 case PROP_ACTIVE_ID:
1259 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1262 case PROP_CELL_AREA:
1263 g_value_set_object (value, priv->area);
1267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1273 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1274 GtkStateFlags previous)
1276 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1277 GtkComboBoxPrivate *priv = combo_box->priv;
1279 if (gtk_widget_get_realized (widget))
1281 if (priv->tree_view && priv->cell_view)
1283 GtkStyleContext *context;
1284 GtkStateFlags state;
1287 context = gtk_widget_get_style_context (widget);
1288 state = gtk_widget_get_state_flags (widget);
1290 gtk_style_context_get (context, state,
1291 "background-color", &color,
1294 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
1295 gdk_rgba_free (color);
1299 gtk_widget_queue_draw (widget);
1303 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1304 GtkStateFlags previous,
1307 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1308 GtkComboBoxPrivate *priv = combo_box->priv;
1310 if (gtk_widget_get_realized (widget))
1312 if (!priv->tree_view && priv->cell_view)
1313 gtk_widget_set_state_flags (priv->cell_view,
1314 gtk_widget_get_state_flags (widget),
1318 gtk_widget_queue_draw (widget);
1322 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1324 GtkComboBoxPrivate *priv = combo_box->priv;
1325 gboolean appears_as_list;
1327 /* if wrap_width > 0, then we are in grid-mode and forced to use
1330 if (priv->wrap_width)
1331 appears_as_list = FALSE;
1333 gtk_widget_style_get (GTK_WIDGET (combo_box),
1334 "appears-as-list", &appears_as_list,
1337 if (appears_as_list)
1339 /* Destroy all the menu mode widgets, if they exist. */
1340 if (GTK_IS_MENU (priv->popup_widget))
1341 gtk_combo_box_menu_destroy (combo_box);
1343 /* Create the list mode widgets, if they don't already exist. */
1344 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1345 gtk_combo_box_list_setup (combo_box);
1349 /* Destroy all the list mode widgets, if they exist. */
1350 if (GTK_IS_TREE_VIEW (priv->tree_view))
1351 gtk_combo_box_list_destroy (combo_box);
1353 /* Create the menu mode widgets, if they don't already exist. */
1354 if (!GTK_IS_MENU (priv->popup_widget))
1355 gtk_combo_box_menu_setup (combo_box, TRUE);
1358 gtk_widget_style_get (GTK_WIDGET (combo_box),
1359 "shadow-type", &priv->shadow_type,
1364 gtk_combo_box_style_updated (GtkWidget *widget)
1366 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1367 GtkComboBoxPrivate *priv = combo_box->priv;
1370 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1372 gtk_combo_box_check_appearance (combo_box);
1374 if (priv->tree_view && priv->cell_view)
1376 GtkStyleContext *context;
1379 context = gtk_widget_get_style_context (widget);
1380 gtk_style_context_get (context, 0,
1381 "background-color", &color,
1384 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view),
1387 gdk_rgba_free (color);
1390 child = gtk_bin_get_child (GTK_BIN (combo_box));
1391 if (GTK_IS_ENTRY (child))
1392 g_object_set (child, "shadow-type",
1393 GTK_SHADOW_NONE == priv->shadow_type ?
1394 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1398 gtk_combo_box_button_toggled (GtkWidget *widget,
1401 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1403 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1405 if (!combo_box->priv->popup_in_progress)
1406 gtk_combo_box_popup (combo_box);
1409 gtk_combo_box_popdown (combo_box);
1413 gtk_combo_box_add (GtkContainer *container,
1416 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1417 GtkComboBoxPrivate *priv = combo_box->priv;
1419 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1421 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1422 "(need an instance of GtkEntry or of a subclass)",
1423 G_OBJECT_TYPE_NAME (widget));
1427 if (priv->cell_view &&
1428 gtk_widget_get_parent (priv->cell_view))
1430 gtk_widget_unparent (priv->cell_view);
1431 _gtk_bin_set_child (GTK_BIN (container), NULL);
1432 gtk_widget_queue_resize (GTK_WIDGET (container));
1435 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1436 _gtk_bin_set_child (GTK_BIN (container), widget);
1438 if (priv->cell_view &&
1439 widget != priv->cell_view)
1441 /* since the cell_view was unparented, it's gone now */
1442 priv->cell_view = NULL;
1444 if (!priv->tree_view && priv->separator)
1446 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1448 priv->separator = NULL;
1450 gtk_widget_queue_resize (GTK_WIDGET (container));
1452 else if (priv->cell_view_frame)
1454 gtk_widget_unparent (priv->cell_view_frame);
1455 priv->cell_view_frame = NULL;
1460 if (priv->has_entry)
1462 /* this flag is a hack to tell the entry to fill its allocation.
1464 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1466 g_signal_connect (widget, "changed",
1467 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1470 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1475 gtk_combo_box_remove (GtkContainer *container,
1478 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1479 GtkComboBoxPrivate *priv = combo_box->priv;
1481 gboolean appears_as_list;
1483 if (priv->has_entry)
1485 GtkWidget *child_widget;
1487 child_widget = gtk_bin_get_child (GTK_BIN (container));
1488 if (widget && widget == child_widget)
1490 g_signal_handlers_disconnect_by_func (widget,
1491 gtk_combo_box_entry_contents_changed,
1493 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1497 if (widget == priv->cell_view)
1498 priv->cell_view = NULL;
1500 gtk_widget_unparent (widget);
1501 _gtk_bin_set_child (GTK_BIN (container), NULL);
1503 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1506 gtk_widget_queue_resize (GTK_WIDGET (container));
1508 if (!priv->tree_view)
1509 appears_as_list = FALSE;
1511 appears_as_list = TRUE;
1513 if (appears_as_list)
1514 gtk_combo_box_list_destroy (combo_box);
1515 else if (GTK_IS_MENU (priv->popup_widget))
1517 gtk_combo_box_menu_destroy (combo_box);
1518 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1519 priv->popup_widget = NULL;
1522 if (!priv->cell_view)
1524 priv->cell_view = gtk_cell_view_new ();
1525 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1526 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1528 gtk_widget_show (priv->cell_view);
1529 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1534 if (appears_as_list)
1535 gtk_combo_box_list_setup (combo_box);
1537 gtk_combo_box_menu_setup (combo_box, TRUE);
1539 if (gtk_tree_row_reference_valid (priv->active_row))
1541 path = gtk_tree_row_reference_get_path (priv->active_row);
1542 gtk_combo_box_set_active_internal (combo_box, path);
1543 gtk_tree_path_free (path);
1546 gtk_combo_box_set_active_internal (combo_box, NULL);
1550 gtk_combo_box_menu_show (GtkWidget *menu,
1553 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1554 GtkComboBoxPrivate *priv = combo_box->priv;
1556 gtk_combo_box_child_show (menu, user_data);
1558 priv->popup_in_progress = TRUE;
1559 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1561 priv->popup_in_progress = FALSE;
1565 gtk_combo_box_menu_hide (GtkWidget *menu,
1568 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1570 gtk_combo_box_child_hide (menu,user_data);
1572 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1577 gtk_combo_box_detacher (GtkWidget *widget,
1580 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1581 GtkComboBoxPrivate *priv = combo_box->priv;
1583 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1585 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1586 gtk_combo_box_menu_show,
1588 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1589 gtk_combo_box_menu_hide,
1592 priv->popup_widget = NULL;
1596 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1599 GtkComboBoxPrivate *priv = combo_box->priv;
1601 if (GTK_IS_MENU (priv->popup_widget))
1603 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1604 priv->popup_widget = NULL;
1606 else if (priv->popup_widget)
1608 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1609 priv->popup_widget);
1610 g_object_unref (priv->popup_widget);
1611 priv->popup_widget = NULL;
1614 if (GTK_IS_MENU (popup))
1616 if (priv->popup_window)
1618 gtk_widget_destroy (priv->popup_window);
1619 priv->popup_window = NULL;
1622 priv->popup_widget = popup;
1625 * Note that we connect to show/hide on the toplevel, not the
1626 * menu itself, since the menu is not shown/hidden when it is
1627 * popped up while torn-off.
1629 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1630 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1631 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1632 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1634 gtk_menu_attach_to_widget (GTK_MENU (popup),
1635 GTK_WIDGET (combo_box),
1636 gtk_combo_box_detacher);
1640 if (!priv->popup_window)
1642 GtkWidget *toplevel;
1644 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1645 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1647 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1648 GDK_WINDOW_TYPE_HINT_COMBO);
1650 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1651 G_CALLBACK (gtk_combo_box_child_show),
1653 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1654 G_CALLBACK (gtk_combo_box_child_hide),
1657 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1658 if (GTK_IS_WINDOW (toplevel))
1660 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1661 GTK_WINDOW (priv->popup_window));
1662 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1663 GTK_WINDOW (toplevel));
1666 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1667 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1668 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1670 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1672 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1675 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1678 gtk_widget_show (priv->scrolled_window);
1680 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1681 priv->scrolled_window);
1684 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1687 gtk_widget_show (popup);
1688 g_object_ref (popup);
1689 priv->popup_widget = popup;
1694 get_widget_border (GtkWidget *widget,
1697 GtkStyleContext *context;
1698 GtkBorder *border_width;
1700 context = gtk_widget_get_style_context (widget);
1702 gtk_style_context_get (context,
1703 gtk_widget_get_state_flags (widget),
1704 "border-width", &border_width,
1707 *border = *border_width;
1708 gtk_border_free (border_width);
1712 gtk_combo_box_menu_position_below (GtkMenu *menu,
1718 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1719 GtkAllocation child_allocation;
1725 GdkRectangle monitor;
1728 /* FIXME: is using the size request here broken? */
1729 child = gtk_bin_get_child (GTK_BIN (combo_box));
1733 gtk_widget_get_allocation (child, &child_allocation);
1735 if (!gtk_widget_get_has_window (child))
1737 sx += child_allocation.x;
1738 sy += child_allocation.y;
1741 gdk_window_get_root_coords (gtk_widget_get_window (child),
1743 get_widget_border (GTK_WIDGET (combo_box), &border);
1746 if (combo_box->priv->popup_fixed_width)
1747 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1749 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1751 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1754 *x = sx + child_allocation.width - req.width;
1757 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1758 monitor_num = gdk_screen_get_monitor_at_window (screen,
1759 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1760 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1764 else if (*x + req.width > monitor.x + monitor.width)
1765 *x = monitor.x + monitor.width - req.width;
1767 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1768 *y += child_allocation.height;
1769 else if (*y - monitor.y >= req.height)
1771 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1772 *y += child_allocation.height;
1780 gtk_combo_box_menu_position_over (GtkMenu *menu,
1786 GtkComboBox *combo_box;
1790 GtkAllocation allocation;
1791 GtkAllocation child_allocation;
1798 combo_box = GTK_COMBO_BOX (user_data);
1799 widget = GTK_WIDGET (combo_box);
1801 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1803 gtk_widget_get_allocation (widget, &allocation);
1805 menu_xpos = allocation.x;
1806 menu_ypos = allocation.y + allocation.height / 2 - 2;
1808 if (combo_box->priv->popup_fixed_width)
1809 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1811 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1815 gtk_widget_get_allocation (active, &child_allocation);
1816 menu_ypos -= child_allocation.height / 2;
1819 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1822 child = children->data;
1824 if (active == child)
1827 if (gtk_widget_get_visible (child))
1829 gtk_widget_get_allocation (child, &child_allocation);
1831 menu_ypos -= child_allocation.height;
1834 children = children->next;
1837 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1838 menu_xpos = menu_xpos + allocation.width - menu_width;
1840 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1841 menu_xpos, menu_ypos,
1842 &menu_xpos, &menu_ypos);
1844 /* Clamp the position on screen */
1845 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1849 else if ((menu_xpos + menu_width) > screen_width)
1850 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1859 gtk_combo_box_menu_position (GtkMenu *menu,
1865 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1866 GtkComboBoxPrivate *priv = combo_box->priv;
1867 GtkWidget *menu_item;
1869 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1870 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1873 /* FIXME handle nested menus better */
1874 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1876 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1879 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1882 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1883 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1884 GDK_WINDOW_TYPE_HINT_COMBO);
1888 gtk_combo_box_list_position (GtkComboBox *combo_box,
1894 GtkComboBoxPrivate *priv = combo_box->priv;
1895 GtkAllocation allocation;
1898 GdkRectangle monitor;
1899 GtkRequisition popup_req;
1900 GtkPolicyType hpolicy, vpolicy;
1903 /* under windows, the drop down list is as wide as the combo box itself.
1905 GtkWidget *widget = GTK_WIDGET (combo_box);
1909 gtk_widget_get_allocation (widget, &allocation);
1911 if (!gtk_widget_get_has_window (widget))
1917 window = gtk_widget_get_window (widget);
1919 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1922 *width = allocation.width;
1924 hpolicy = vpolicy = GTK_POLICY_NEVER;
1925 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1928 if (combo_box->priv->popup_fixed_width)
1930 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1932 if (popup_req.width > *width)
1934 hpolicy = GTK_POLICY_ALWAYS;
1935 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1941 /* XXX This code depends on treeviews properly reporting their natural width
1942 * list-mode menus won't fill up to their natural width until then */
1943 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1945 if (popup_req.width > *width)
1947 hpolicy = GTK_POLICY_NEVER;
1948 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1951 *width = popup_req.width;
1955 *height = popup_req.height;
1957 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1958 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1959 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1961 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1962 *x = *x + allocation.width - *width;
1966 else if (*x + *width > monitor.x + monitor.width)
1967 *x = monitor.x + monitor.width - *width;
1969 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1970 *y += allocation.height;
1971 else if (*y - *height >= monitor.y)
1973 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1975 *y += allocation.height;
1976 *height = monitor.y + monitor.height - *y;
1980 *height = *y - monitor.y;
1984 if (popup_req.height > *height)
1986 vpolicy = GTK_POLICY_ALWAYS;
1988 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1994 cell_layout_is_sensitive (GtkCellLayout *layout)
1996 GList *cells, *list;
1999 cells = gtk_cell_layout_get_cells (layout);
2002 for (list = cells; list; list = list->next)
2004 g_object_get (list->data, "sensitive", &sensitive, NULL);
2009 g_list_free (cells);
2015 cell_is_sensitive (GtkCellRenderer *cell,
2018 gboolean *sensitive = data;
2020 g_object_get (cell, "sensitive", sensitive, NULL);
2026 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2029 GtkComboBoxPrivate *priv = combo_box->priv;
2031 if (priv->row_separator_func)
2033 if (priv->row_separator_func (priv->model, iter,
2034 priv->row_separator_data))
2042 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2046 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2055 update_menu_sensitivity (GtkComboBox *combo_box,
2058 GtkComboBoxPrivate *priv = combo_box->priv;
2059 GList *children, *child;
2060 GtkWidget *item, *submenu, *separator;
2061 GtkWidget *cell_view;
2067 children = gtk_container_get_children (GTK_CONTAINER (menu));
2069 for (child = children; child; child = child->next)
2071 item = GTK_WIDGET (child->data);
2072 cell_view = gtk_bin_get_child (GTK_BIN (item));
2074 if (!GTK_IS_CELL_VIEW (cell_view))
2077 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2078 if (submenu != NULL)
2080 gtk_widget_set_sensitive (item, TRUE);
2081 update_menu_sensitivity (combo_box, submenu);
2085 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2087 if (menu != priv->popup_widget && child == children)
2089 separator = GTK_WIDGET (child->next->data);
2090 g_object_set (item, "visible", sensitive, NULL);
2091 g_object_set (separator, "visible", sensitive, NULL);
2094 gtk_widget_set_sensitive (item, sensitive);
2098 g_list_free (children);
2102 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2104 guint32 activate_time)
2106 GtkComboBoxPrivate *priv = combo_box->priv;
2109 gint width, min_width, nat_width;
2111 update_menu_sensitivity (combo_box, priv->popup_widget);
2114 if (gtk_tree_row_reference_valid (priv->active_row))
2116 path = gtk_tree_row_reference_get_path (priv->active_row);
2117 active_item = gtk_tree_path_get_indices (path)[0];
2118 gtk_tree_path_free (path);
2120 if (priv->add_tearoffs)
2124 /* FIXME handle nested menus better */
2125 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2127 if (priv->wrap_width == 0)
2129 GtkAllocation allocation;
2131 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2132 width = allocation.width;
2133 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2134 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2136 if (combo_box->priv->popup_fixed_width)
2137 width = MAX (width, min_width);
2139 width = MAX (width, nat_width);
2141 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2144 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2146 gtk_combo_box_menu_position, combo_box,
2147 button, activate_time);
2151 popup_grab_on_window (GdkWindow *window,
2152 GdkDevice *keyboard,
2154 guint32 activate_time)
2157 gdk_device_grab (keyboard, window,
2158 GDK_OWNERSHIP_WINDOW, TRUE,
2159 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2160 NULL, activate_time) != GDK_GRAB_SUCCESS)
2164 gdk_device_grab (pointer, window,
2165 GDK_OWNERSHIP_WINDOW, TRUE,
2166 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2167 GDK_POINTER_MOTION_MASK,
2168 NULL, activate_time) != GDK_GRAB_SUCCESS)
2171 gdk_device_ungrab (keyboard, activate_time);
2180 * gtk_combo_box_popup:
2181 * @combo_box: a #GtkComboBox
2183 * Pops up the menu or dropdown list of @combo_box.
2185 * This function is mostly intended for use by accessibility technologies;
2186 * applications should have little use for it.
2191 gtk_combo_box_popup (GtkComboBox *combo_box)
2193 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2195 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2199 * gtk_combo_box_popup_for_device:
2200 * @combo_box: a #GtkComboBox
2201 * @device: a #GdkDevice
2203 * Pops up the menu or dropdown list of @combo_box, the popup window
2204 * will be grabbed so only @device and its associated pointer/keyboard
2205 * are the only #GdkDevice<!-- -->s able to send events to it.
2210 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2213 GtkComboBoxPrivate *priv = combo_box->priv;
2214 gint x, y, width, height;
2215 GtkTreePath *path = NULL, *ppath;
2216 GtkWidget *toplevel;
2217 GdkDevice *keyboard, *pointer;
2220 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2221 g_return_if_fail (GDK_IS_DEVICE (device));
2223 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2226 if (gtk_widget_get_mapped (priv->popup_widget))
2229 if (priv->grab_pointer && priv->grab_keyboard)
2232 time = gtk_get_current_event_time ();
2234 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2237 pointer = gdk_device_get_associated_device (device);
2242 keyboard = gdk_device_get_associated_device (device);
2245 if (GTK_IS_MENU (priv->popup_widget))
2247 gtk_combo_box_menu_popup (combo_box,
2248 priv->activate_button,
2249 priv->activate_time);
2253 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2254 if (GTK_IS_WINDOW (toplevel))
2255 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2256 GTK_WINDOW (priv->popup_window));
2258 gtk_widget_show_all (priv->scrolled_window);
2259 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2261 gtk_widget_set_size_request (priv->popup_window, width, height);
2262 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2264 if (gtk_tree_row_reference_valid (priv->active_row))
2266 path = gtk_tree_row_reference_get_path (priv->active_row);
2267 ppath = gtk_tree_path_copy (path);
2268 if (gtk_tree_path_up (ppath))
2269 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2271 gtk_tree_path_free (ppath);
2273 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2277 gtk_widget_show (priv->popup_window);
2281 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2283 gtk_tree_path_free (path);
2286 gtk_widget_grab_focus (priv->popup_window);
2287 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2290 if (!gtk_widget_has_focus (priv->tree_view))
2291 gtk_widget_grab_focus (priv->tree_view);
2293 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2294 keyboard, pointer, time))
2296 gtk_widget_hide (priv->popup_window);
2300 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2301 priv->grab_pointer = pointer;
2302 priv->grab_keyboard = keyboard;
2306 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2310 device = gtk_get_current_event_device ();
2314 GdkDeviceManager *device_manager;
2315 GdkDisplay *display;
2318 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2319 device_manager = gdk_display_get_device_manager (display);
2321 /* No device was set, pick the first master device */
2322 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2323 device = devices->data;
2324 g_list_free (devices);
2327 gtk_combo_box_popup_for_device (combo_box, device);
2331 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2333 if (combo_box->priv->popup_shown)
2335 gtk_combo_box_popdown (combo_box);
2343 * gtk_combo_box_popdown:
2344 * @combo_box: a #GtkComboBox
2346 * Hides the menu or dropdown list of @combo_box.
2348 * This function is mostly intended for use by accessibility technologies;
2349 * applications should have little use for it.
2354 gtk_combo_box_popdown (GtkComboBox *combo_box)
2356 GtkComboBoxPrivate *priv = combo_box->priv;
2358 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2360 if (GTK_IS_MENU (priv->popup_widget))
2362 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2366 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2369 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2370 gtk_widget_hide (priv->popup_window);
2371 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2374 priv->grab_pointer = NULL;
2375 priv->grab_keyboard = NULL;
2378 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2379 gtk_widget_get_preferred_size (combo_box->priv->button, \
2383 child.x = allocation->x + border.right; \
2385 child.x = allocation->x + allocation->width - req.width - border.left; \
2387 child.y = allocation->y + border.top; \
2388 child.width = req.width; \
2389 child.height = allocation->height - (border.top + border.bottom); \
2390 child.width = MAX (1, child.width); \
2391 child.height = MAX (1, child.height); \
2393 gtk_widget_size_allocate (combo_box->priv->button, &child);
2397 gtk_combo_box_size_allocate (GtkWidget *widget,
2398 GtkAllocation *allocation)
2400 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2401 GtkComboBoxPrivate *priv = combo_box->priv;
2402 GtkWidget *child_widget;
2403 gint focus_width, focus_pad;
2404 GtkAllocation child;
2406 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2409 gtk_widget_set_allocation (widget, allocation);
2410 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2411 get_widget_border (widget, &border);
2413 gtk_widget_style_get (widget,
2414 "focus-line-width", &focus_width,
2415 "focus-padding", &focus_pad,
2418 if (!priv->tree_view)
2420 if (priv->cell_view)
2422 GtkBorder button_border;
2427 allocation->x += border.left;
2428 allocation->y += border.top;
2429 allocation->width -= border.left + border.right;
2430 allocation->height -= border.top + border.bottom;
2432 gtk_widget_size_allocate (priv->button, allocation);
2434 /* set some things ready */
2435 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2436 get_widget_border (priv->button, &button_border);
2438 child.x = allocation->x;
2439 child.y = allocation->y;
2440 width = allocation->width;
2441 child.height = allocation->height;
2443 if (!priv->is_cell_renderer)
2445 child.x += border_width + button_border.left + focus_width + focus_pad;
2446 child.y += border_width + button_border.top + focus_width + focus_pad;
2447 width -= (2 * (border_width + focus_width + focus_pad)) +
2448 button_border.left + button_border.right;
2449 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2450 button_border.top + button_border.bottom;
2453 /* handle the children */
2454 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2455 child.width = req.width;
2457 child.x += width - req.width;
2458 child.width = MAX (1, child.width);
2459 child.height = MAX (1, child.height);
2460 gtk_widget_size_allocate (priv->arrow, &child);
2462 child.x += req.width;
2463 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2464 child.width = req.width;
2466 child.x -= req.width;
2467 child.width = MAX (1, child.width);
2468 child.height = MAX (1, child.height);
2469 gtk_widget_size_allocate (priv->separator, &child);
2473 child.x += req.width;
2474 child.width = allocation->x + allocation->width
2475 - (border_width + button_border.right + focus_width + focus_pad)
2480 child.width = child.x;
2481 child.x = allocation->x
2482 + border_width + button_border.left + focus_width + focus_pad;
2483 child.width -= child.x;
2486 if (gtk_widget_get_visible (priv->popup_widget))
2488 gint width, menu_width;
2490 if (priv->wrap_width == 0)
2492 GtkAllocation combo_box_allocation;
2494 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2495 width = combo_box_allocation.width;
2496 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2498 if (combo_box->priv->popup_fixed_width)
2499 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2501 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2503 gtk_widget_set_size_request (priv->popup_widget,
2504 MAX (width, menu_width), -1);
2507 /* reposition the menu after giving it a new width */
2508 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2511 child.width = MAX (1, child.width);
2512 child.height = MAX (1, child.height);
2513 gtk_widget_size_allocate (child_widget, &child);
2517 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2520 child.x = allocation->x + req.width + border.right;
2522 child.x = allocation->x + border.left;
2523 child.y = allocation->y + border.top;
2524 child.width = allocation->width - req.width - (border.left + border.right);
2525 child.width = MAX (1, child.width);
2526 child.height = MAX (1, child.height);
2527 gtk_widget_size_allocate (child_widget, &child);
2533 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2536 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2540 child.x = allocation->x + req.width;
2542 child.x = allocation->x;
2544 child.y = allocation->y;
2545 child.width = allocation->width - req.width;
2546 child.height = allocation->height;
2548 if (priv->cell_view_frame)
2550 child.x += border.left + border_width;
2551 child.y += border.top + border_width;
2552 child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right));
2553 child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom));
2554 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2557 if (priv->has_frame)
2559 GtkBorder frame_border;
2561 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2562 get_widget_border (priv->cell_view_frame, &frame_border);
2564 child.x += border_width + frame_border.left;
2565 child.y += border_width + frame_border.right;
2566 child.width -= (2 * border_width) + frame_border.left + frame_border.right;
2567 child.height -= (2 * border_width) + frame_border.top + frame_border.bottom;
2572 child.x += border.left + border_width;
2573 child.y += border.top + border_width;
2574 child.width -= (2 * border_width) - (border.left + border.right);
2575 child.height -= (2 * border_width) - (border.top + border.bottom);
2578 if (gtk_widget_get_visible (priv->popup_window))
2580 gint x, y, width, height;
2581 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2582 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2583 gtk_widget_set_size_request (priv->popup_window, width, height);
2586 child.width = MAX (1, child.width);
2587 child.height = MAX (1, child.height);
2588 gtk_widget_size_allocate (child_widget, &child);
2592 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2595 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2597 GtkComboBoxPrivate *priv = combo_box->priv;
2601 g_signal_handler_disconnect (priv->model,
2603 g_signal_handler_disconnect (priv->model,
2605 g_signal_handler_disconnect (priv->model,
2606 priv->reordered_id);
2607 g_signal_handler_disconnect (priv->model,
2613 g_object_unref (priv->model);
2617 if (priv->active_row)
2619 gtk_tree_row_reference_free (priv->active_row);
2620 priv->active_row = NULL;
2623 if (priv->cell_view)
2624 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2628 gtk_combo_box_forall (GtkContainer *container,
2629 gboolean include_internals,
2630 GtkCallback callback,
2631 gpointer callback_data)
2633 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2634 GtkComboBoxPrivate *priv = combo_box->priv;
2637 if (include_internals)
2640 (* callback) (priv->button, callback_data);
2641 if (priv->cell_view_frame)
2642 (* callback) (priv->cell_view_frame, callback_data);
2645 child = gtk_bin_get_child (GTK_BIN (container));
2647 (* callback) (child, callback_data);
2651 gtk_combo_box_child_show (GtkWidget *widget,
2652 GtkComboBox *combo_box)
2654 GtkComboBoxPrivate *priv = combo_box->priv;
2656 priv->popup_shown = TRUE;
2657 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2661 gtk_combo_box_child_hide (GtkWidget *widget,
2662 GtkComboBox *combo_box)
2664 GtkComboBoxPrivate *priv = combo_box->priv;
2666 priv->popup_shown = FALSE;
2667 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2671 gtk_combo_box_draw (GtkWidget *widget,
2674 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2675 GtkComboBoxPrivate *priv = combo_box->priv;
2677 if (priv->shadow_type != GTK_SHADOW_NONE)
2679 GtkStyleContext *context;
2680 GtkStateFlags state;
2682 context = gtk_widget_get_style_context (widget);
2683 state = gtk_widget_get_state_flags (widget);
2684 gtk_style_context_set_state (context, state);
2686 gtk_render_background (context, cr, 0, 0,
2687 gtk_widget_get_allocated_width (widget),
2688 gtk_widget_get_allocated_height (widget));
2689 gtk_render_frame (context, cr, 0, 0,
2690 gtk_widget_get_allocated_width (widget),
2691 gtk_widget_get_allocated_height (widget));
2694 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2697 if (priv->tree_view && priv->cell_view_frame)
2699 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2700 priv->cell_view_frame, cr);
2703 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2704 gtk_bin_get_child (GTK_BIN (widget)),
2720 path_visible (GtkTreeView *view,
2726 /* Note that we rely on the fact that collapsed rows don't have nodes
2728 return _gtk_tree_view_find_node (view, path, &tree, &node);
2732 tree_next_func (GtkTreeModel *model,
2737 SearchData *search_data = (SearchData *)data;
2739 if (search_data->found)
2741 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2744 if (search_data->visible &&
2745 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2748 search_data->set = TRUE;
2749 search_data->iter = *iter;
2754 if (gtk_tree_path_compare (path, search_data->path) == 0)
2755 search_data->found = TRUE;
2761 tree_next (GtkComboBox *combo,
2762 GtkTreeModel *model,
2767 SearchData search_data;
2769 search_data.combo = combo;
2770 search_data.path = gtk_tree_model_get_path (model, iter);
2771 search_data.visible = visible;
2772 search_data.found = FALSE;
2773 search_data.set = FALSE;
2775 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2777 *next = search_data.iter;
2779 gtk_tree_path_free (search_data.path);
2781 return search_data.set;
2785 tree_prev_func (GtkTreeModel *model,
2790 SearchData *search_data = (SearchData *)data;
2792 if (gtk_tree_path_compare (path, search_data->path) == 0)
2794 search_data->found = TRUE;
2798 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2801 if (search_data->visible &&
2802 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2805 search_data->set = TRUE;
2806 search_data->iter = *iter;
2812 tree_prev (GtkComboBox *combo,
2813 GtkTreeModel *model,
2818 SearchData search_data;
2820 search_data.combo = combo;
2821 search_data.path = gtk_tree_model_get_path (model, iter);
2822 search_data.visible = visible;
2823 search_data.found = FALSE;
2824 search_data.set = FALSE;
2826 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2828 *prev = search_data.iter;
2830 gtk_tree_path_free (search_data.path);
2832 return search_data.set;
2836 tree_last_func (GtkTreeModel *model,
2841 SearchData *search_data = (SearchData *)data;
2843 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2846 /* Note that we rely on the fact that collapsed rows don't have nodes
2848 if (search_data->visible &&
2849 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2852 search_data->set = TRUE;
2853 search_data->iter = *iter;
2859 tree_last (GtkComboBox *combo,
2860 GtkTreeModel *model,
2864 SearchData search_data;
2866 search_data.combo = combo;
2867 search_data.visible = visible;
2868 search_data.set = FALSE;
2870 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2872 *last = search_data.iter;
2874 return search_data.set;
2879 tree_first_func (GtkTreeModel *model,
2884 SearchData *search_data = (SearchData *)data;
2886 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2889 if (search_data->visible &&
2890 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2893 search_data->set = TRUE;
2894 search_data->iter = *iter;
2900 tree_first (GtkComboBox *combo,
2901 GtkTreeModel *model,
2905 SearchData search_data;
2907 search_data.combo = combo;
2908 search_data.visible = visible;
2909 search_data.set = FALSE;
2911 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2913 *first = search_data.iter;
2915 return search_data.set;
2919 gtk_combo_box_scroll_event (GtkWidget *widget,
2920 GdkEventScroll *event)
2922 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2925 GtkTreeIter new_iter;
2927 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2930 if (event->direction == GDK_SCROLL_UP)
2931 found = tree_prev (combo_box, combo_box->priv->model,
2932 &iter, &new_iter, FALSE);
2934 found = tree_next (combo_box, combo_box->priv->model,
2935 &iter, &new_iter, FALSE);
2938 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2947 gtk_combo_box_row_separator_func (GtkTreeModel *model,
2951 GtkComboBoxPrivate *priv = combo->priv;
2953 if (priv->row_separator_func)
2954 return priv->row_separator_func (model, iter, priv->row_separator_data);
2960 gtk_combo_box_header_func (GtkTreeModel *model,
2964 /* Every submenu has a selectable header, however we
2965 * can expose a method to make that configurable by
2966 * the user (like row_separator_func is done) */
2971 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2972 gboolean add_children)
2974 GtkComboBoxPrivate *priv = combo_box->priv;
2978 child = gtk_bin_get_child (GTK_BIN (combo_box));
2980 if (priv->cell_view)
2982 priv->button = gtk_toggle_button_new ();
2983 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2984 priv->focus_on_click);
2986 g_signal_connect (priv->button, "toggled",
2987 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2988 gtk_widget_set_parent (priv->button,
2989 gtk_widget_get_parent (child));
2991 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2992 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2994 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
2995 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2997 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2998 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3000 gtk_widget_show_all (priv->button);
3004 priv->button = gtk_toggle_button_new ();
3005 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3006 priv->focus_on_click);
3008 g_signal_connect (priv->button, "toggled",
3009 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3010 gtk_widget_set_parent (priv->button,
3011 gtk_widget_get_parent (child));
3013 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3014 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3015 gtk_widget_show_all (priv->button);
3018 g_signal_connect (priv->button, "button-press-event",
3019 G_CALLBACK (gtk_combo_box_menu_button_press),
3021 g_signal_connect (priv->button, "state-flags-changed",
3022 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3025 /* create our funky menu */
3026 menu = _gtk_tree_menu_new_with_area (priv->area);
3027 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3029 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3031 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3032 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3033 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3034 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3035 combo_box->priv->add_tearoffs);
3037 g_signal_connect (menu, "menu-activate",
3038 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3040 /* Chain our row_separator_func through */
3041 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3042 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3045 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3046 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3049 g_signal_connect (menu, "key-press-event",
3050 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3051 gtk_combo_box_set_popup_widget (combo_box, menu);
3053 gtk_combo_box_update_title (combo_box);
3057 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3059 GtkComboBoxPrivate *priv = combo_box->priv;
3061 g_signal_handlers_disconnect_matched (priv->button,
3062 G_SIGNAL_MATCH_DATA,
3064 gtk_combo_box_menu_button_press, NULL);
3065 g_signal_handlers_disconnect_matched (priv->button,
3066 G_SIGNAL_MATCH_DATA,
3068 gtk_combo_box_button_state_flags_changed, combo_box);
3069 g_signal_handlers_disconnect_matched (priv->popup_widget,
3070 G_SIGNAL_MATCH_DATA,
3072 gtk_combo_box_menu_activate, combo_box);
3074 /* unparent will remove our latest ref */
3075 gtk_widget_unparent (priv->button);
3078 priv->button = NULL;
3080 priv->separator = NULL;
3082 /* changing the popup window will unref the menu and the children */
3087 gtk_combo_box_menu_button_press (GtkWidget *widget,
3088 GdkEventButton *event,
3091 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3092 GtkComboBoxPrivate *priv = combo_box->priv;
3094 if (GTK_IS_MENU (priv->popup_widget) &&
3095 event->type == GDK_BUTTON_PRESS && event->button == 1)
3097 if (priv->focus_on_click &&
3098 !gtk_widget_has_focus (priv->button))
3099 gtk_widget_grab_focus (priv->button);
3101 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3110 gtk_combo_box_menu_activate (GtkWidget *menu,
3112 GtkComboBox *combo_box)
3116 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3117 gtk_combo_box_set_active_iter (combo_box, &iter);
3119 g_object_set (combo_box,
3120 "editing-canceled", FALSE,
3126 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3129 gboolean sensitive = TRUE; /* fool code checkers */
3131 if (!combo_box->priv->button)
3134 switch (combo_box->priv->button_sensitivity)
3136 case GTK_SENSITIVITY_ON:
3139 case GTK_SENSITIVITY_OFF:
3142 case GTK_SENSITIVITY_AUTO:
3143 sensitive = combo_box->priv->model &&
3144 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3147 g_assert_not_reached ();
3151 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3153 /* In list-mode, we also need to update sensitivity of the event box */
3154 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3155 && combo_box->priv->cell_view)
3156 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3160 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3165 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3167 if (combo_box->priv->tree_view)
3168 gtk_combo_box_list_popup_resize (combo_box);
3170 gtk_combo_box_update_sensitivity (combo_box);
3174 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3178 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3179 GtkComboBoxPrivate *priv = combo_box->priv;
3181 if (!gtk_tree_row_reference_valid (priv->active_row))
3183 if (priv->cell_view)
3184 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3185 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3188 if (priv->tree_view)
3189 gtk_combo_box_list_popup_resize (combo_box);
3191 gtk_combo_box_update_sensitivity (combo_box);
3195 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3201 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3205 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3210 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3211 GtkComboBoxPrivate *priv = combo_box->priv;
3212 GtkTreePath *active_path;
3214 /* FIXME this belongs to GtkCellView */
3215 if (gtk_tree_row_reference_valid (priv->active_row))
3217 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3218 if (gtk_tree_path_compare (path, active_path) == 0 &&
3220 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3221 gtk_tree_path_free (active_path);
3224 if (priv->tree_view)
3225 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3229 list_popup_resize_idle (gpointer user_data)
3231 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3232 GtkComboBoxPrivate *priv = combo_box->priv;
3233 gint x, y, width, height;
3235 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3237 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3239 gtk_widget_set_size_request (priv->popup_window, width, height);
3240 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3243 priv->resize_idle_id = 0;
3249 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3251 GtkComboBoxPrivate *priv = combo_box->priv;
3253 if (!priv->resize_idle_id)
3254 priv->resize_idle_id =
3255 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3259 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3264 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3266 gtk_combo_box_list_popup_resize (combo_box);
3275 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3277 GtkComboBoxPrivate *priv = combo_box->priv;
3278 GtkTreeSelection *sel;
3280 GtkWidget *widget = GTK_WIDGET (combo_box);
3282 priv->button = gtk_toggle_button_new ();
3283 child = gtk_bin_get_child (GTK_BIN (combo_box));
3284 gtk_widget_set_parent (priv->button,
3285 gtk_widget_get_parent (child));
3286 g_signal_connect (priv->button, "button-press-event",
3287 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3288 g_signal_connect (priv->button, "toggled",
3289 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3291 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3292 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3293 priv->separator = NULL;
3294 gtk_widget_show_all (priv->button);
3296 if (priv->cell_view)
3298 GtkStyleContext *context;
3299 GtkStateFlags state;
3302 context = gtk_widget_get_style_context (widget);
3303 state = gtk_widget_get_state_flags (widget);
3305 gtk_style_context_get (context, state,
3306 "background-color", &color,
3309 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
3310 gdk_rgba_free (color);
3312 priv->box = gtk_event_box_new ();
3313 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3316 if (priv->has_frame)
3318 priv->cell_view_frame = gtk_frame_new (NULL);
3319 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3324 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3325 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3329 gtk_widget_set_parent (priv->cell_view_frame,
3330 gtk_widget_get_parent (child));
3331 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3332 gtk_widget_show_all (priv->cell_view_frame);
3334 g_signal_connect (priv->box, "button-press-event",
3335 G_CALLBACK (gtk_combo_box_list_button_pressed),
3339 priv->tree_view = gtk_tree_view_new ();
3340 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3341 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3342 gtk_tree_selection_set_select_function (sel,
3343 gtk_combo_box_list_select_func,
3345 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3347 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3350 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3351 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3355 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3357 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3358 gtk_tree_view_column_new_with_area (priv->area));
3360 if (gtk_tree_row_reference_valid (priv->active_row))
3364 path = gtk_tree_row_reference_get_path (priv->active_row);
3365 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3367 gtk_tree_path_free (path);
3370 /* set sample/popup widgets */
3371 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3373 g_signal_connect (priv->tree_view, "key-press-event",
3374 G_CALLBACK (gtk_combo_box_list_key_press),
3376 g_signal_connect (priv->tree_view, "enter-notify-event",
3377 G_CALLBACK (gtk_combo_box_list_enter_notify),
3379 g_signal_connect (priv->tree_view, "row-expanded",
3380 G_CALLBACK (gtk_combo_box_model_row_expanded),
3382 g_signal_connect (priv->tree_view, "row-collapsed",
3383 G_CALLBACK (gtk_combo_box_model_row_expanded),
3385 g_signal_connect (priv->popup_window, "button-press-event",
3386 G_CALLBACK (gtk_combo_box_list_button_pressed),
3388 g_signal_connect (priv->popup_window, "button-release-event",
3389 G_CALLBACK (gtk_combo_box_list_button_released),
3392 gtk_widget_show (priv->tree_view);
3394 gtk_combo_box_update_sensitivity (combo_box);
3398 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3400 GtkComboBoxPrivate *priv = combo_box->priv;
3402 /* disconnect signals */
3403 g_signal_handlers_disconnect_matched (priv->tree_view,
3404 G_SIGNAL_MATCH_DATA,
3405 0, 0, NULL, NULL, combo_box);
3406 g_signal_handlers_disconnect_matched (priv->button,
3407 G_SIGNAL_MATCH_DATA,
3409 gtk_combo_box_list_button_pressed,
3411 g_signal_handlers_disconnect_matched (priv->popup_window,
3412 G_SIGNAL_MATCH_DATA,
3414 gtk_combo_box_list_button_pressed,
3416 g_signal_handlers_disconnect_matched (priv->popup_window,
3417 G_SIGNAL_MATCH_DATA,
3419 gtk_combo_box_list_button_released,
3422 g_signal_handlers_disconnect_matched (priv->popup_window,
3423 G_SIGNAL_MATCH_DATA,
3425 gtk_combo_box_child_show,
3428 g_signal_handlers_disconnect_matched (priv->popup_window,
3429 G_SIGNAL_MATCH_DATA,
3431 gtk_combo_box_child_hide,
3435 g_signal_handlers_disconnect_matched (priv->box,
3436 G_SIGNAL_MATCH_DATA,
3438 gtk_combo_box_list_button_pressed,
3441 /* destroy things (unparent will kill the latest ref from us)
3442 * last unref on button will destroy the arrow
3444 gtk_widget_unparent (priv->button);
3445 priv->button = NULL;
3448 if (priv->cell_view)
3450 g_object_set (priv->cell_view,
3451 "background-set", FALSE,
3455 if (priv->cell_view_frame)
3457 gtk_widget_unparent (priv->cell_view_frame);
3458 priv->cell_view_frame = NULL;
3462 if (priv->scroll_timer)
3464 g_source_remove (priv->scroll_timer);
3465 priv->scroll_timer = 0;
3468 if (priv->resize_idle_id)
3470 g_source_remove (priv->resize_idle_id);
3471 priv->resize_idle_id = 0;
3474 gtk_widget_destroy (priv->tree_view);
3476 priv->tree_view = NULL;
3477 if (priv->popup_widget)
3479 g_object_unref (priv->popup_widget);
3480 priv->popup_widget = NULL;
3487 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3488 GdkEventButton *event,
3491 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3492 GtkComboBoxPrivate *priv = combo_box->priv;
3494 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3496 if (ewidget == priv->popup_window)
3499 if ((ewidget != priv->button && ewidget != priv->box) ||
3500 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3503 if (priv->focus_on_click &&
3504 !gtk_widget_has_focus (priv->button))
3505 gtk_widget_grab_focus (priv->button);
3507 gtk_combo_box_popup_for_device (combo_box, event->device);
3509 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3511 priv->auto_scroll = FALSE;
3512 if (priv->scroll_timer == 0)
3513 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3514 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3517 priv->popup_in_progress = TRUE;
3523 gtk_combo_box_list_button_released (GtkWidget *widget,
3524 GdkEventButton *event,
3528 GtkTreePath *path = NULL;
3531 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3532 GtkComboBoxPrivate *priv = combo_box->priv;
3534 gboolean popup_in_progress = FALSE;
3536 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3538 if (priv->popup_in_progress)
3540 popup_in_progress = TRUE;
3541 priv->popup_in_progress = FALSE;
3544 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3546 if (priv->scroll_timer)
3548 g_source_remove (priv->scroll_timer);
3549 priv->scroll_timer = 0;
3552 if (ewidget != priv->tree_view)
3554 if ((ewidget == priv->button ||
3555 ewidget == priv->box) &&
3556 !popup_in_progress &&
3557 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3559 gtk_combo_box_popdown (combo_box);
3563 /* released outside treeview */
3564 if (ewidget != priv->button && ewidget != priv->box)
3566 gtk_combo_box_popdown (combo_box);
3574 /* select something cool */
3575 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3581 return TRUE; /* clicked outside window? */
3583 gtk_tree_model_get_iter (priv->model, &iter, path);
3584 gtk_tree_path_free (path);
3586 gtk_combo_box_popdown (combo_box);
3588 if (tree_column_row_is_sensitive (combo_box, &iter))
3589 gtk_combo_box_set_active_iter (combo_box, &iter);
3595 gtk_combo_box_menu_key_press (GtkWidget *widget,
3599 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3601 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3603 /* The menu hasn't managed the
3604 * event, forward it to the combobox
3606 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3613 gtk_combo_box_list_key_press (GtkWidget *widget,
3617 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3620 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3621 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3623 GtkTreeModel *model = NULL;
3625 gtk_combo_box_popdown (combo_box);
3627 if (combo_box->priv->model)
3629 GtkTreeSelection *sel;
3631 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3633 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3634 gtk_combo_box_set_active_iter (combo_box, &iter);
3640 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3642 /* The list hasn't managed the
3643 * event, forward it to the combobox
3645 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3652 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3657 GtkAllocation allocation;
3658 GtkWidget *tree_view = combo_box->priv->tree_view;
3661 gtk_widget_get_allocation (tree_view, &allocation);
3663 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3664 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3666 if (x <= allocation.x &&
3667 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3669 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3670 gtk_adjustment_set_value (adj, value);
3672 else if (x >= allocation.x + allocation.width &&
3673 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3675 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3676 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3680 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3681 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3683 if (y <= allocation.y &&
3684 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3686 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3687 gtk_adjustment_set_value (adj, value);
3689 else if (y >= allocation.height &&
3690 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3692 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3693 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3699 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3701 GtkComboBoxPrivate *priv = combo_box->priv;
3704 if (priv->auto_scroll)
3706 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3709 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3716 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3717 GdkEventCrossing *event,
3720 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3722 combo_box->priv->auto_scroll = TRUE;
3728 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3729 GtkTreeModel *model,
3731 gboolean path_currently_selected,
3734 GList *list, *columns;
3735 gboolean sensitive = FALSE;
3737 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3739 for (list = columns; list && !sensitive; list = list->next)
3741 GList *cells, *cell;
3742 gboolean cell_sensitive, cell_visible;
3744 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3746 if (!gtk_tree_view_column_get_visible (column))
3749 gtk_tree_model_get_iter (model, &iter, path);
3750 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3753 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3756 g_object_get (cell->data,
3757 "sensitive", &cell_sensitive,
3758 "visible", &cell_visible,
3761 if (cell_visible && cell_sensitive)
3766 g_list_free (cells);
3768 sensitive = cell_sensitive;
3771 g_list_free (columns);
3777 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3782 /* XXX Do nothing ? */
3786 * GtkCellLayout implementation
3788 static GtkCellArea *
3789 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3791 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3792 GtkComboBoxPrivate *priv = combo->priv;
3794 if (G_UNLIKELY (!priv->area))
3796 priv->area = gtk_cell_area_box_new ();
3797 g_object_ref_sink (priv->area);
3808 * gtk_combo_box_new:
3810 * Creates a new empty #GtkComboBox.
3812 * Return value: A new #GtkComboBox.
3817 gtk_combo_box_new (void)
3819 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3823 * gtk_combo_box_new_with_area:
3824 * @area: the #GtkCellArea to use to layout cell renderers
3826 * Creates a new empty #GtkComboBox using @area to layout cells.
3828 * Return value: A new #GtkComboBox.
3831 gtk_combo_box_new_with_area (GtkCellArea *area)
3833 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3837 * gtk_combo_box_new_with_area_and_entry:
3838 * @area: the #GtkCellArea to use to layout cell renderers
3840 * Creates a new empty #GtkComboBox with an entry.
3842 * The new combo box will use @area to layout cells.
3844 * Return value: A new #GtkComboBox.
3847 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3849 return g_object_new (GTK_TYPE_COMBO_BOX,
3857 * gtk_combo_box_new_with_entry:
3859 * Creates a new empty #GtkComboBox with an entry.
3861 * Return value: A new #GtkComboBox.
3864 gtk_combo_box_new_with_entry (void)
3866 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3870 * gtk_combo_box_new_with_model:
3871 * @model: A #GtkTreeModel.
3873 * Creates a new #GtkComboBox with the model initialized to @model.
3875 * Return value: A new #GtkComboBox.
3880 gtk_combo_box_new_with_model (GtkTreeModel *model)
3882 GtkComboBox *combo_box;
3884 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3886 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3888 return GTK_WIDGET (combo_box);
3892 * gtk_combo_box_new_with_model_and_entry:
3893 * @model: A #GtkTreeModel
3895 * Creates a new empty #GtkComboBox with an entry
3896 * and with the model initialized to @model.
3898 * Return value: A new #GtkComboBox
3901 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3903 return g_object_new (GTK_TYPE_COMBO_BOX,
3910 * gtk_combo_box_get_wrap_width:
3911 * @combo_box: A #GtkComboBox
3913 * Returns the wrap width which is used to determine the number of columns
3914 * for the popup menu. If the wrap width is larger than 1, the combo box
3917 * Returns: the wrap width.
3922 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3924 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3926 return combo_box->priv->wrap_width;
3930 * gtk_combo_box_set_wrap_width:
3931 * @combo_box: A #GtkComboBox
3932 * @width: Preferred number of columns
3934 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3935 * the preferred number of columns when you want the popup to be layed out
3941 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3944 GtkComboBoxPrivate *priv;
3946 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3947 g_return_if_fail (width >= 0);
3949 priv = combo_box->priv;
3951 if (width != priv->wrap_width)
3953 priv->wrap_width = width;
3955 gtk_combo_box_check_appearance (combo_box);
3957 if (GTK_IS_TREE_MENU (priv->popup_widget))
3958 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
3960 g_object_notify (G_OBJECT (combo_box), "wrap-width");
3965 * gtk_combo_box_get_row_span_column:
3966 * @combo_box: A #GtkComboBox
3968 * Returns the column with row span information for @combo_box.
3970 * Returns: the row span column.
3975 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3977 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3979 return combo_box->priv->row_column;
3983 * gtk_combo_box_set_row_span_column:
3984 * @combo_box: A #GtkComboBox.
3985 * @row_span: A column in the model passed during construction.
3987 * Sets the column with row span information for @combo_box to be @row_span.
3988 * The row span column contains integers which indicate how many rows
3989 * an item should span.
3994 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3997 GtkComboBoxPrivate *priv;
4000 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4002 priv = combo_box->priv;
4004 col = gtk_tree_model_get_n_columns (priv->model);
4005 g_return_if_fail (row_span >= -1 && row_span < col);
4007 if (row_span != priv->row_column)
4009 priv->row_column = row_span;
4011 if (GTK_IS_TREE_MENU (priv->popup_widget))
4012 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4014 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4019 * gtk_combo_box_get_column_span_column:
4020 * @combo_box: A #GtkComboBox
4022 * Returns the column with column span information for @combo_box.
4024 * Returns: the column span column.
4029 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4031 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4033 return combo_box->priv->col_column;
4037 * gtk_combo_box_set_column_span_column:
4038 * @combo_box: A #GtkComboBox
4039 * @column_span: A column in the model passed during construction
4041 * Sets the column with column span information for @combo_box to be
4042 * @column_span. The column span column contains integers which indicate
4043 * how many columns an item should span.
4048 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4051 GtkComboBoxPrivate *priv;
4054 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4056 priv = combo_box->priv;
4058 col = gtk_tree_model_get_n_columns (priv->model);
4059 g_return_if_fail (column_span >= -1 && column_span < col);
4061 if (column_span != priv->col_column)
4063 priv->col_column = column_span;
4065 if (GTK_IS_TREE_MENU (priv->popup_widget))
4066 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4068 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4073 * gtk_combo_box_get_active:
4074 * @combo_box: A #GtkComboBox
4076 * Returns the index of the currently active item, or -1 if there's no
4077 * active item. If the model is a non-flat treemodel, and the active item
4078 * is not an immediate child of the root of the tree, this function returns
4079 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4080 * <literal>path</literal> is the #GtkTreePath of the active item.
4082 * Return value: An integer which is the index of the currently active item,
4083 * or -1 if there's no active item.
4088 gtk_combo_box_get_active (GtkComboBox *combo_box)
4090 GtkComboBoxPrivate *priv;
4093 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4095 priv = combo_box->priv;
4097 if (gtk_tree_row_reference_valid (priv->active_row))
4101 path = gtk_tree_row_reference_get_path (priv->active_row);
4102 result = gtk_tree_path_get_indices (path)[0];
4103 gtk_tree_path_free (path);
4112 * gtk_combo_box_set_active:
4113 * @combo_box: A #GtkComboBox
4114 * @index_: An index in the model passed during construction, or -1 to have
4117 * Sets the active item of @combo_box to be the item at @index.
4122 gtk_combo_box_set_active (GtkComboBox *combo_box,
4125 GtkTreePath *path = NULL;
4126 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4127 g_return_if_fail (index_ >= -1);
4129 if (combo_box->priv->model == NULL)
4131 /* Save index, in case the model is set after the index */
4132 combo_box->priv->active = index_;
4138 path = gtk_tree_path_new_from_indices (index_, -1);
4140 gtk_combo_box_set_active_internal (combo_box, path);
4143 gtk_tree_path_free (path);
4147 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4150 GtkComboBoxPrivate *priv = combo_box->priv;
4151 GtkTreePath *active_path;
4154 /* Remember whether the initially active row is valid. */
4155 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4157 if (path && is_valid_row_reference)
4159 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4160 path_cmp = gtk_tree_path_compare (path, active_path);
4161 gtk_tree_path_free (active_path);
4166 if (priv->active_row)
4168 gtk_tree_row_reference_free (priv->active_row);
4169 priv->active_row = NULL;
4174 if (priv->tree_view)
4175 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4178 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4180 if (GTK_IS_MENU (menu))
4181 gtk_menu_set_active (menu, -1);
4184 if (priv->cell_view)
4185 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4188 * Do not emit a "changed" signal when an already invalid selection was
4189 * now set to invalid.
4191 if (!is_valid_row_reference)
4197 gtk_tree_row_reference_new (priv->model, path);
4199 if (priv->tree_view)
4201 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4204 else if (GTK_IS_MENU (priv->popup_widget))
4206 /* FIXME handle nested menus better */
4207 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4208 gtk_tree_path_get_indices (path)[0]);
4211 if (priv->cell_view)
4212 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4216 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4217 g_object_notify (G_OBJECT (combo_box), "active");
4218 if (combo_box->priv->id_column >= 0)
4219 g_object_notify (G_OBJECT (combo_box), "active-id");
4224 * gtk_combo_box_get_active_iter:
4225 * @combo_box: A #GtkComboBox
4226 * @iter: (out): The uninitialized #GtkTreeIter
4228 * Sets @iter to point to the current active item, if it exists.
4230 * Return value: %TRUE, if @iter was set
4235 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4241 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4243 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4246 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4247 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4248 gtk_tree_path_free (path);
4254 * gtk_combo_box_set_active_iter:
4255 * @combo_box: A #GtkComboBox
4256 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4258 * Sets the current active item to be the one referenced by @iter, or
4259 * unsets the active item if @iter is %NULL.
4264 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4267 GtkTreePath *path = NULL;
4269 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4272 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4274 gtk_combo_box_set_active_internal (combo_box, path);
4275 gtk_tree_path_free (path);
4279 * gtk_combo_box_set_model:
4280 * @combo_box: A #GtkComboBox
4281 * @model: (allow-none): A #GtkTreeModel
4283 * Sets the model used by @combo_box to be @model. Will unset a previously set
4284 * model (if applicable). If model is %NULL, then it will unset the model.
4286 * Note that this function does not clear the cell renderers, you have to
4287 * call gtk_cell_layout_clear() yourself if you need to set up different
4288 * cell renderers for the new model.
4293 gtk_combo_box_set_model (GtkComboBox *combo_box,
4294 GtkTreeModel *model)
4296 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4297 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4299 if (model == combo_box->priv->model)
4302 gtk_combo_box_unset_model (combo_box);
4307 combo_box->priv->model = model;
4308 g_object_ref (combo_box->priv->model);
4310 combo_box->priv->inserted_id =
4311 g_signal_connect (combo_box->priv->model, "row-inserted",
4312 G_CALLBACK (gtk_combo_box_model_row_inserted),
4314 combo_box->priv->deleted_id =
4315 g_signal_connect (combo_box->priv->model, "row-deleted",
4316 G_CALLBACK (gtk_combo_box_model_row_deleted),
4318 combo_box->priv->reordered_id =
4319 g_signal_connect (combo_box->priv->model, "rows-reordered",
4320 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4322 combo_box->priv->changed_id =
4323 g_signal_connect (combo_box->priv->model, "row-changed",
4324 G_CALLBACK (gtk_combo_box_model_row_changed),
4327 if (combo_box->priv->tree_view)
4330 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4331 combo_box->priv->model);
4332 gtk_combo_box_list_popup_resize (combo_box);
4335 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4338 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4339 combo_box->priv->model);
4342 if (combo_box->priv->cell_view)
4343 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4344 combo_box->priv->model);
4346 if (combo_box->priv->active != -1)
4348 /* If an index was set in advance, apply it now */
4349 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4350 combo_box->priv->active = -1;
4354 gtk_combo_box_update_sensitivity (combo_box);
4356 g_object_notify (G_OBJECT (combo_box), "model");
4360 * gtk_combo_box_get_model:
4361 * @combo_box: A #GtkComboBox
4363 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4365 * Return value: (transfer none): A #GtkTreeModel which was passed
4366 * during construction.
4371 gtk_combo_box_get_model (GtkComboBox *combo_box)
4373 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4375 return combo_box->priv->model;
4379 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4380 GtkScrollType scroll)
4383 GtkTreeIter new_iter;
4384 gboolean active_iter;
4387 if (!combo_box->priv->model)
4389 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4393 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4397 case GTK_SCROLL_STEP_BACKWARD:
4398 case GTK_SCROLL_STEP_UP:
4399 case GTK_SCROLL_STEP_LEFT:
4402 found = tree_prev (combo_box, combo_box->priv->model,
4403 &iter, &new_iter, FALSE);
4406 /* else fall through */
4408 case GTK_SCROLL_PAGE_FORWARD:
4409 case GTK_SCROLL_PAGE_DOWN:
4410 case GTK_SCROLL_PAGE_RIGHT:
4411 case GTK_SCROLL_END:
4412 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4415 case GTK_SCROLL_STEP_FORWARD:
4416 case GTK_SCROLL_STEP_DOWN:
4417 case GTK_SCROLL_STEP_RIGHT:
4420 found = tree_next (combo_box, combo_box->priv->model,
4421 &iter, &new_iter, FALSE);
4424 /* else fall through */
4426 case GTK_SCROLL_PAGE_BACKWARD:
4427 case GTK_SCROLL_PAGE_UP:
4428 case GTK_SCROLL_PAGE_LEFT:
4429 case GTK_SCROLL_START:
4430 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4437 if (found && active_iter)
4439 GtkTreePath *old_path;
4440 GtkTreePath *new_path;
4442 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4443 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4445 if (gtk_tree_path_compare (old_path, new_path) == 0)
4448 gtk_tree_path_free (old_path);
4449 gtk_tree_path_free (new_path);
4454 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4458 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4463 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4464 gboolean group_cycling)
4466 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4468 if (combo_box->priv->has_entry)
4472 child = gtk_bin_get_child (GTK_BIN (combo_box));
4474 gtk_widget_grab_focus (child);
4477 gtk_widget_grab_focus (combo_box->priv->button);
4483 gtk_combo_box_grab_focus (GtkWidget *widget)
4485 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4487 if (combo_box->priv->has_entry)
4491 child = gtk_bin_get_child (GTK_BIN (combo_box));
4493 gtk_widget_grab_focus (child);
4496 gtk_widget_grab_focus (combo_box->priv->button);
4500 gtk_combo_box_destroy (GtkWidget *widget)
4502 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4504 if (combo_box->priv->popup_idle_id > 0)
4506 g_source_remove (combo_box->priv->popup_idle_id);
4507 combo_box->priv->popup_idle_id = 0;
4510 gtk_combo_box_popdown (combo_box);
4512 if (combo_box->priv->row_separator_destroy)
4513 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4515 combo_box->priv->row_separator_func = NULL;
4516 combo_box->priv->row_separator_data = NULL;
4517 combo_box->priv->row_separator_destroy = NULL;
4519 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4520 combo_box->priv->cell_view = NULL;
4524 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4527 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4530 * Fixes regression reported in bug #574059. The old functionality relied on
4531 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4532 * when the selection was already set to -1.
4534 if (gtk_combo_box_get_active(combo_box) == -1)
4535 g_signal_emit_by_name (combo_box, "changed");
4537 gtk_combo_box_set_active (combo_box, -1);
4541 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4544 GtkComboBoxPrivate *priv = combo_box->priv;
4545 GtkTreeModel *model;
4548 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4550 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4554 GValue value = {0,};
4556 g_signal_handlers_block_by_func (entry,
4557 gtk_combo_box_entry_contents_changed,
4560 model = gtk_combo_box_get_model (combo_box);
4562 gtk_tree_model_get_value (model, &iter,
4563 priv->text_column, &value);
4564 g_object_set_property (G_OBJECT (entry), "text", &value);
4565 g_value_unset (&value);
4567 g_signal_handlers_unblock_by_func (entry,
4568 gtk_combo_box_entry_contents_changed,
4575 gtk_combo_box_constructor (GType type,
4576 guint n_construct_properties,
4577 GObjectConstructParam *construct_properties)
4580 GtkComboBox *combo_box;
4581 GtkComboBoxPrivate *priv;
4583 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4584 (type, n_construct_properties, construct_properties);
4586 combo_box = GTK_COMBO_BOX (object);
4587 priv = combo_box->priv;
4591 priv->area = gtk_cell_area_box_new ();
4592 g_object_ref_sink (priv->area);
4595 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4596 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4597 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4598 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4599 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4600 gtk_widget_show (priv->cell_view);
4602 gtk_combo_box_check_appearance (combo_box);
4604 if (priv->has_entry)
4608 entry = gtk_entry_new ();
4609 gtk_widget_show (entry);
4610 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4612 priv->text_renderer = gtk_cell_renderer_text_new ();
4613 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4614 priv->text_renderer, TRUE);
4616 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4618 g_signal_connect (combo_box, "changed",
4619 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4627 gtk_combo_box_dispose(GObject* object)
4629 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4631 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4633 gtk_combo_box_menu_destroy (combo_box);
4634 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4635 combo_box->priv->popup_widget = NULL;
4638 if (combo_box->priv->area)
4640 g_object_unref (combo_box->priv->area);
4641 combo_box->priv->area = NULL;
4644 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4645 gtk_combo_box_list_destroy (combo_box);
4647 if (combo_box->priv->popup_window)
4649 gtk_widget_destroy (combo_box->priv->popup_window);
4650 combo_box->priv->popup_window = NULL;
4653 gtk_combo_box_unset_model (combo_box);
4655 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4659 gtk_combo_box_finalize (GObject *object)
4661 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4663 g_free (combo_box->priv->tearoff_title);
4665 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4669 gtk_cell_editable_key_press (GtkWidget *widget,
4673 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4675 if (event->keyval == GDK_KEY_Escape)
4677 g_object_set (combo_box,
4678 "editing-canceled", TRUE,
4680 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4681 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4685 else if (event->keyval == GDK_KEY_Return ||
4686 event->keyval == GDK_KEY_ISO_Enter ||
4687 event->keyval == GDK_KEY_KP_Enter)
4689 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4690 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4699 popdown_idle (gpointer data)
4701 GtkComboBox *combo_box;
4703 combo_box = GTK_COMBO_BOX (data);
4705 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4706 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4708 g_object_unref (combo_box);
4714 popdown_handler (GtkWidget *widget,
4717 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4721 popup_idle (gpointer data)
4723 GtkComboBox *combo_box;
4725 combo_box = GTK_COMBO_BOX (data);
4727 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4728 combo_box->priv->cell_view)
4729 g_signal_connect_object (combo_box->priv->popup_widget,
4730 "unmap", G_CALLBACK (popdown_handler),
4733 /* we unset this if a menu item is activated */
4734 g_object_set (combo_box,
4735 "editing-canceled", TRUE,
4737 gtk_combo_box_popup (combo_box);
4739 combo_box->priv->popup_idle_id = 0;
4740 combo_box->priv->activate_button = 0;
4741 combo_box->priv->activate_time = 0;
4747 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4750 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4753 combo_box->priv->is_cell_renderer = TRUE;
4755 if (combo_box->priv->cell_view)
4757 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4758 G_CALLBACK (gtk_cell_editable_key_press),
4761 gtk_widget_grab_focus (combo_box->priv->button);
4765 child = gtk_bin_get_child (GTK_BIN (combo_box));
4767 g_signal_connect_object (child, "key-press-event",
4768 G_CALLBACK (gtk_cell_editable_key_press),
4771 gtk_widget_grab_focus (child);
4772 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4775 /* we do the immediate popup only for the optionmenu-like
4778 if (combo_box->priv->is_cell_renderer &&
4779 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4781 if (event && event->type == GDK_BUTTON_PRESS)
4783 GdkEventButton *event_button = (GdkEventButton *)event;
4785 combo_box->priv->activate_button = event_button->button;
4786 combo_box->priv->activate_time = event_button->time;
4789 combo_box->priv->popup_idle_id =
4790 gdk_threads_add_idle (popup_idle, combo_box);
4796 * gtk_combo_box_get_add_tearoffs:
4797 * @combo_box: a #GtkComboBox
4799 * Gets the current value of the :add-tearoffs property.
4801 * Return value: the current value of the :add-tearoffs property.
4804 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4806 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4808 return combo_box->priv->add_tearoffs;
4812 * gtk_combo_box_set_add_tearoffs:
4813 * @combo_box: a #GtkComboBox
4814 * @add_tearoffs: %TRUE to add tearoff menu items
4816 * Sets whether the popup menu should have a tearoff
4822 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4823 gboolean add_tearoffs)
4825 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4827 add_tearoffs = add_tearoffs != FALSE;
4829 if (combo_box->priv->add_tearoffs != add_tearoffs)
4831 combo_box->priv->add_tearoffs = add_tearoffs;
4832 gtk_combo_box_check_appearance (combo_box);
4834 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4835 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4836 combo_box->priv->add_tearoffs);
4838 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4843 * gtk_combo_box_get_title:
4844 * @combo_box: a #GtkComboBox
4846 * Gets the current title of the menu in tearoff mode. See
4847 * gtk_combo_box_set_add_tearoffs().
4849 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4850 * string which must not be freed.
4854 G_CONST_RETURN gchar*
4855 gtk_combo_box_get_title (GtkComboBox *combo_box)
4857 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4859 return combo_box->priv->tearoff_title;
4863 gtk_combo_box_update_title (GtkComboBox *combo_box)
4865 gtk_combo_box_check_appearance (combo_box);
4867 if (combo_box->priv->popup_widget &&
4868 GTK_IS_MENU (combo_box->priv->popup_widget))
4869 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4870 combo_box->priv->tearoff_title);
4874 * gtk_combo_box_set_title:
4875 * @combo_box: a #GtkComboBox
4876 * @title: a title for the menu in tearoff mode
4878 * Sets the menu's title in tearoff mode.
4883 gtk_combo_box_set_title (GtkComboBox *combo_box,
4886 GtkComboBoxPrivate *priv;
4888 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4890 priv = combo_box->priv;
4892 if (strcmp (title ? title : "",
4893 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4895 g_free (priv->tearoff_title);
4896 priv->tearoff_title = g_strdup (title);
4898 gtk_combo_box_update_title (combo_box);
4900 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4906 * gtk_combo_box_set_popup_fixed_width:
4907 * @combo_box: a #GtkComboBox
4908 * @fixed: whether to use a fixed popup width
4910 * Specifies whether the popup's width should be a fixed width
4911 * matching the allocated width of the combo box.
4916 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4919 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4921 if (combo_box->priv->popup_fixed_width != fixed)
4923 combo_box->priv->popup_fixed_width = fixed;
4925 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
4930 * gtk_combo_box_get_popup_fixed_width:
4931 * @combo_box: a #GtkComboBox
4933 * Gets whether the popup uses a fixed width matching
4934 * the allocated width of the combo box.
4936 * Returns: %TRUE if the popup uses a fixed width
4941 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
4943 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4945 return combo_box->priv->popup_fixed_width;
4950 * gtk_combo_box_get_popup_accessible:
4951 * @combo_box: a #GtkComboBox
4953 * Gets the accessible object corresponding to the combo box's popup.
4955 * This function is mostly intended for use by accessibility technologies;
4956 * applications should have little use for it.
4958 * Returns: (transfer none): the accessible object corresponding
4959 * to the combo box's popup.
4964 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
4968 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4970 if (combo_box->priv->popup_widget)
4972 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
4980 * gtk_combo_box_get_row_separator_func: (skip)
4981 * @combo_box: a #GtkComboBox
4983 * Returns the current row separator function.
4985 * Return value: the current row separator function.
4989 GtkTreeViewRowSeparatorFunc
4990 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
4992 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4994 return combo_box->priv->row_separator_func;
4998 * gtk_combo_box_set_row_separator_func:
4999 * @combo_box: a #GtkComboBox
5000 * @func: a #GtkTreeViewRowSeparatorFunc
5001 * @data: (allow-none): user data to pass to @func, or %NULL
5002 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5004 * Sets the row separator function, which is used to determine
5005 * whether a row should be drawn as a separator. If the row separator
5006 * function is %NULL, no separators are drawn. This is the default value.
5011 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5012 GtkTreeViewRowSeparatorFunc func,
5014 GDestroyNotify destroy)
5016 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5018 if (combo_box->priv->row_separator_destroy)
5019 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5021 combo_box->priv->row_separator_func = func;
5022 combo_box->priv->row_separator_data = data;
5023 combo_box->priv->row_separator_destroy = destroy;
5025 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5026 if (combo_box->priv->tree_view)
5028 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5029 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5032 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5034 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5035 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5038 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5042 * gtk_combo_box_set_button_sensitivity:
5043 * @combo_box: a #GtkComboBox
5044 * @sensitivity: specify the sensitivity of the dropdown button
5046 * Sets whether the dropdown button of the combo box should be
5047 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5048 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5053 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5054 GtkSensitivityType sensitivity)
5056 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5058 if (combo_box->priv->button_sensitivity != sensitivity)
5060 combo_box->priv->button_sensitivity = sensitivity;
5061 gtk_combo_box_update_sensitivity (combo_box);
5063 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5068 * gtk_combo_box_get_button_sensitivity:
5069 * @combo_box: a #GtkComboBox
5071 * Returns whether the combo box sets the dropdown button
5072 * sensitive or not when there are no items in the model.
5074 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5075 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5076 * if the button is always insensitive or
5077 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5078 * the model has one item to be selected.
5083 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5085 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5087 return combo_box->priv->button_sensitivity;
5092 * gtk_combo_box_get_has_entry:
5093 * @combo_box: a #GtkComboBox
5095 * Returns whether the combo box has an entry.
5097 * Return Value: whether there is an entry in @combo_box.
5102 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5104 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5106 return combo_box->priv->has_entry;
5110 * gtk_combo_box_set_entry_text_column:
5111 * @combo_box: A #GtkComboBox
5112 * @text_column: A column in @model to get the strings from for
5113 * the internal entry
5115 * Sets the model column which @combo_box should use to get strings from
5116 * to be @text_column. The column @text_column in the model of @combo_box
5117 * must be of type %G_TYPE_STRING.
5119 * This is only relevant if @combo_box has been created with
5120 * #GtkComboBox:has-entry as %TRUE.
5125 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5128 GtkComboBoxPrivate *priv = combo_box->priv;
5129 GtkTreeModel *model;
5131 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5133 model = gtk_combo_box_get_model (combo_box);
5135 g_return_if_fail (text_column >= 0);
5136 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5138 priv->text_column = text_column;
5140 if (priv->text_renderer != NULL)
5141 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5142 priv->text_renderer,
5143 "text", text_column,
5148 * gtk_combo_box_get_entry_text_column:
5149 * @combo_box: A #GtkComboBox.
5151 * Returns the column which @combo_box is using to get the strings
5152 * from to display in the internal entry.
5154 * Return value: A column in the data source model of @combo_box.
5159 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5161 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5163 return combo_box->priv->text_column;
5167 * gtk_combo_box_set_focus_on_click:
5168 * @combo: a #GtkComboBox
5169 * @focus_on_click: whether the combo box grabs focus when clicked
5172 * Sets whether the combo box will grab focus when it is clicked with
5173 * the mouse. Making mouse clicks not grab focus is useful in places
5174 * like toolbars where you don't want the keyboard focus removed from
5175 * the main area of the application.
5180 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5181 gboolean focus_on_click)
5183 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5185 focus_on_click = focus_on_click != FALSE;
5187 if (combo_box->priv->focus_on_click != focus_on_click)
5189 combo_box->priv->focus_on_click = focus_on_click;
5191 if (combo_box->priv->button)
5192 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5195 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5200 * gtk_combo_box_get_focus_on_click:
5201 * @combo: a #GtkComboBox
5203 * Returns whether the combo box grabs focus when it is clicked
5204 * with the mouse. See gtk_combo_box_set_focus_on_click().
5206 * Return value: %TRUE if the combo box grabs focus when it is
5207 * clicked with the mouse.
5212 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5214 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5216 return combo_box->priv->focus_on_click;
5221 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5222 GtkBuilder *builder,
5224 const gchar *tagname,
5225 GMarkupParser *parser,
5228 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5229 tagname, parser, data))
5232 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5233 tagname, parser, data);
5237 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5238 GtkBuilder *builder,
5240 const gchar *tagname,
5243 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5244 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5248 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5249 GtkBuilder *builder,
5250 const gchar *childname)
5252 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5254 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5255 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5257 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5261 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5265 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5266 GtkComboBoxPrivate *priv = combo_box->priv;
5267 gint focus_width, focus_pad;
5268 gint font_size, arrow_size;
5269 PangoContext *context;
5270 PangoFontMetrics *metrics;
5271 PangoFontDescription *font_desc;
5273 gint minimum_width = 0, natural_width = 0;
5274 gint child_min, child_nat;
5275 GtkStyleContext *style_context;
5276 GtkStateFlags state;
5278 gfloat arrow_scaling;
5280 child = gtk_bin_get_child (GTK_BIN (widget));
5283 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5285 gtk_widget_style_get (GTK_WIDGET (widget),
5286 "focus-line-width", &focus_width,
5287 "focus-padding", &focus_pad,
5288 "arrow-size", &arrow_size,
5289 "arrow-scaling", &arrow_scaling,
5292 style_context = gtk_widget_get_style_context (widget);
5293 state = gtk_widget_get_state_flags (widget);
5295 gtk_style_context_get (style_context, state,
5297 "border-width", &border,
5300 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5301 metrics = pango_context_get_metrics (context, font_desc,
5302 pango_context_get_language (context));
5303 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5304 pango_font_metrics_get_descent (metrics));
5305 pango_font_metrics_unref (metrics);
5306 pango_font_description_free (font_desc);
5308 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5310 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5312 if (!priv->tree_view)
5315 if (priv->cell_view)
5317 gint sep_width, arrow_width;
5318 gint border_width, xpad;
5319 GtkBorder button_border;
5321 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5322 get_widget_border (priv->button, &button_border);
5324 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5325 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5327 xpad = 2 * (border_width + focus_width + focus_pad) +
5328 button_border.left + button_border.right;
5330 minimum_width = child_min + sep_width + arrow_width + xpad;
5331 natural_width = child_nat + sep_width + arrow_width + xpad;
5335 gint but_width, but_nat_width;
5337 gtk_widget_get_preferred_width (priv->button,
5338 &but_width, &but_nat_width);
5340 minimum_width = child_min + but_width;
5341 natural_width = child_nat + but_nat_width;
5347 gint button_width, button_nat_width;
5349 /* sample + frame */
5350 minimum_width = child_min;
5351 natural_width = child_nat;
5353 minimum_width += 2 * focus_width;
5354 natural_width += 2 * focus_width;
5356 if (priv->cell_view_frame)
5358 if (priv->has_frame)
5360 gint border_width, xpad;
5361 GtkBorder frame_border;
5363 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5364 get_widget_border (priv->cell_view_frame, &frame_border);
5365 xpad = (2 * border_width) + frame_border.left + frame_border.right;
5367 minimum_width += xpad;
5368 natural_width += xpad;
5373 gtk_widget_get_preferred_width (priv->button,
5374 &button_width, &button_nat_width);
5376 minimum_width += button_width;
5377 natural_width += button_nat_width;
5380 minimum_width += border->left + border->right;
5381 natural_width += border->left + border->right;
5382 gtk_border_free (border);
5385 *minimum_size = minimum_width;
5388 *natural_size = natural_width;
5392 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5398 /* Combo box is height-for-width only
5399 * (so we always just reserve enough height for the minimum width) */
5400 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5401 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5405 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5410 /* Combo box is height-for-width only
5411 * (so we assume we always reserved enough height for the minimum width) */
5412 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5417 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5422 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5423 GtkComboBoxPrivate *priv = combo_box->priv;
5424 gint focus_width, focus_pad;
5425 gint min_height = 0, nat_height = 0;
5430 gtk_widget_style_get (GTK_WIDGET (widget),
5431 "focus-line-width", &focus_width,
5432 "focus-padding", &focus_pad,
5435 child = gtk_bin_get_child (GTK_BIN (widget));
5437 get_widget_border (widget, &border);
5438 size = avail_size - border.left;
5440 if (!priv->tree_view)
5443 if (priv->cell_view)
5445 /* calculate x/y padding and separator/arrow size */
5446 gint sep_width, arrow_width, sep_height, arrow_height;
5447 gint border_width, xpad, ypad;
5448 GtkBorder button_border;
5450 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5451 get_widget_border (priv->button, &button_border);
5453 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5454 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5455 gtk_widget_get_preferred_height_for_width (priv->separator,
5456 sep_width, &sep_height, NULL);
5457 gtk_widget_get_preferred_height_for_width (priv->arrow,
5458 arrow_width, &arrow_height, NULL);
5460 xpad = 2 * (border_width + focus_width + focus_pad) +
5461 button_border.left + button_border.right;
5462 ypad = 2 * (border_width + focus_width + focus_pad) +
5463 button_border.top + button_border.bottom;
5465 size -= sep_width + arrow_width + xpad;
5467 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5468 * and fitting the whole treemodel */
5469 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5471 arrow_height = MAX (arrow_height, sep_height);
5472 min_height = MAX (min_height, arrow_height);
5473 nat_height = MAX (nat_height, arrow_height);
5480 /* there is a custom child widget inside (no priv->cell_view) */
5481 gint but_width, but_height;
5483 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5484 gtk_widget_get_preferred_height_for_width (priv->button,
5485 but_width, &but_height, NULL);
5489 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5490 * and fitting the whole treemodel */
5491 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5493 min_height = MAX (min_height, but_height);
5494 nat_height = MAX (nat_height, but_height);
5500 gint but_width, but_height;
5501 gint xpad = 0, ypad = 0;
5503 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5504 gtk_widget_get_preferred_height_for_width (priv->button,
5505 but_width, &but_height, NULL);
5507 if (priv->cell_view_frame && priv->has_frame)
5509 GtkBorder frame_border;
5512 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5513 get_widget_border (GTK_WIDGET (priv->cell_view_frame), &frame_border);
5515 xpad = (2 * border_width) + border.left + frame_border.right;
5516 ypad = (2 * border_width) + border.top + frame_border.bottom;
5520 size -= 2 * focus_width;
5523 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5524 * and fitting the whole treemodel */
5525 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5527 min_height = MAX (min_height, but_height);
5528 nat_height = MAX (nat_height, but_height);
5534 min_height += border.top + border.bottom;
5535 nat_height += border.top + border.bottom;
5538 *minimum_size = min_height;
5541 *natural_size = nat_height;
5545 * gtk_combo_box_set_id_column:
5546 * @combo_box: A #GtkComboBox
5547 * @id_column: A column in @model to get string IDs for values from
5549 * Sets the model column which @combo_box should use to get string IDs
5550 * for values from. The column @id_column in the model of @combo_box
5551 * must be of type %G_TYPE_STRING.
5556 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5559 GtkComboBoxPrivate *priv = combo_box->priv;
5560 GtkTreeModel *model;
5562 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5564 if (id_column != priv->id_column)
5566 model = gtk_combo_box_get_model (combo_box);
5568 g_return_if_fail (id_column >= 0);
5569 g_return_if_fail (model == NULL ||
5570 id_column < gtk_tree_model_get_n_columns (model));
5572 priv->id_column = id_column;
5574 g_object_notify (G_OBJECT (combo_box), "id-column");
5575 g_object_notify (G_OBJECT (combo_box), "active-id");
5580 * gtk_combo_box_get_id_column:
5581 * @combo_box: A #GtkComboBox
5583 * Returns the column which @combo_box is using to get string IDs
5586 * Return value: A column in the data source model of @combo_box.
5591 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5593 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5595 return combo_box->priv->id_column;
5599 * gtk_combo_box_get_active_id:
5600 * @combo_box: a #GtkComboBox
5602 * Returns the ID of the active row of @combo_box. This value is taken
5603 * from the active row and the column specified by the 'id-column'
5604 * property of @combo_box (see gtk_combo_box_set_id_column()).
5606 * The returned value is an interned string which means that you can
5607 * compare the pointer by value to other interned strings and that you
5610 * If the 'id-column' property of @combo_box is not set or if no row is
5611 * selected then %NULL is returned.
5613 * Return value: the ID of the active row, or %NULL
5618 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5620 GtkTreeModel *model;
5624 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5626 column = combo_box->priv->id_column;
5631 model = gtk_combo_box_get_model (combo_box);
5632 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5633 G_TYPE_STRING, NULL);
5635 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5637 const gchar *interned;
5640 gtk_tree_model_get (model, &iter, column, &id, -1);
5641 interned = g_intern_string (id);
5651 * gtk_combo_box_set_active_id:
5652 * @combo_box: a #GtkComboBox
5653 * @active_id: the ID of the row to select
5655 * Changes the active row of @combo_box to the one that has an ID equal to @id.
5657 * If the 'id-column' property of @combo_box is unset or if no row has
5658 * the given ID then nothing happens.
5663 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5664 const gchar *active_id)
5666 GtkTreeModel *model;
5670 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5672 column = combo_box->priv->id_column;
5677 model = gtk_combo_box_get_model (combo_box);
5678 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
5681 if (gtk_tree_model_get_iter_first (model, &iter))
5686 gtk_tree_model_get (model, &iter, column, &id, -1);
5687 match = strcmp (id, active_id) == 0;
5692 gtk_combo_box_set_active_iter (combo_box, &iter);
5695 } while (gtk_tree_model_iter_next (model, &iter));