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_padding (GtkWidget *widget,
1697 GtkStyleContext *context;
1698 GtkStateFlags state;
1700 context = gtk_widget_get_style_context (widget);
1701 state = gtk_style_context_get_state (context);
1703 gtk_style_context_get_padding (context, state, padding);
1707 gtk_combo_box_menu_position_below (GtkMenu *menu,
1713 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1714 GtkAllocation child_allocation;
1720 GdkRectangle monitor;
1723 /* FIXME: is using the size request here broken? */
1724 child = gtk_bin_get_child (GTK_BIN (combo_box));
1728 gtk_widget_get_allocation (child, &child_allocation);
1730 if (!gtk_widget_get_has_window (child))
1732 sx += child_allocation.x;
1733 sy += child_allocation.y;
1736 gdk_window_get_root_coords (gtk_widget_get_window (child),
1738 get_widget_padding (GTK_WIDGET (combo_box), &border);
1741 if (combo_box->priv->popup_fixed_width)
1742 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1744 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1746 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1749 *x = sx + child_allocation.width - req.width;
1752 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1753 monitor_num = gdk_screen_get_monitor_at_window (screen,
1754 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1755 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1759 else if (*x + req.width > monitor.x + monitor.width)
1760 *x = monitor.x + monitor.width - req.width;
1762 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1763 *y += child_allocation.height;
1764 else if (*y - monitor.y >= req.height)
1766 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1767 *y += child_allocation.height;
1775 gtk_combo_box_menu_position_over (GtkMenu *menu,
1781 GtkComboBox *combo_box;
1785 GtkAllocation allocation;
1786 GtkAllocation child_allocation;
1793 combo_box = GTK_COMBO_BOX (user_data);
1794 widget = GTK_WIDGET (combo_box);
1796 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1798 gtk_widget_get_allocation (widget, &allocation);
1800 menu_xpos = allocation.x;
1801 menu_ypos = allocation.y + allocation.height / 2 - 2;
1803 if (combo_box->priv->popup_fixed_width)
1804 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1806 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1810 gtk_widget_get_allocation (active, &child_allocation);
1811 menu_ypos -= child_allocation.height / 2;
1814 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1817 child = children->data;
1819 if (active == child)
1822 if (gtk_widget_get_visible (child))
1824 gtk_widget_get_allocation (child, &child_allocation);
1826 menu_ypos -= child_allocation.height;
1829 children = children->next;
1832 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1833 menu_xpos = menu_xpos + allocation.width - menu_width;
1835 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1836 menu_xpos, menu_ypos,
1837 &menu_xpos, &menu_ypos);
1839 /* Clamp the position on screen */
1840 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1844 else if ((menu_xpos + menu_width) > screen_width)
1845 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1854 gtk_combo_box_menu_position (GtkMenu *menu,
1860 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1861 GtkComboBoxPrivate *priv = combo_box->priv;
1862 GtkWidget *menu_item;
1864 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1865 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1868 /* FIXME handle nested menus better */
1869 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1871 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1874 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1877 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1878 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1879 GDK_WINDOW_TYPE_HINT_COMBO);
1883 gtk_combo_box_list_position (GtkComboBox *combo_box,
1889 GtkComboBoxPrivate *priv = combo_box->priv;
1890 GtkAllocation allocation;
1893 GdkRectangle monitor;
1894 GtkRequisition popup_req;
1895 GtkPolicyType hpolicy, vpolicy;
1898 /* under windows, the drop down list is as wide as the combo box itself.
1900 GtkWidget *widget = GTK_WIDGET (combo_box);
1904 gtk_widget_get_allocation (widget, &allocation);
1906 if (!gtk_widget_get_has_window (widget))
1912 window = gtk_widget_get_window (widget);
1914 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1917 *width = allocation.width;
1919 hpolicy = vpolicy = GTK_POLICY_NEVER;
1920 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1923 if (combo_box->priv->popup_fixed_width)
1925 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
1927 if (popup_req.width > *width)
1929 hpolicy = GTK_POLICY_ALWAYS;
1930 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1936 /* XXX This code depends on treeviews properly reporting their natural width
1937 * list-mode menus won't fill up to their natural width until then */
1938 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
1940 if (popup_req.width > *width)
1942 hpolicy = GTK_POLICY_NEVER;
1943 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1946 *width = popup_req.width;
1950 *height = popup_req.height;
1952 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1953 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
1954 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1956 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1957 *x = *x + allocation.width - *width;
1961 else if (*x + *width > monitor.x + monitor.width)
1962 *x = monitor.x + monitor.width - *width;
1964 if (*y + allocation.height + *height <= monitor.y + monitor.height)
1965 *y += allocation.height;
1966 else if (*y - *height >= monitor.y)
1968 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
1970 *y += allocation.height;
1971 *height = monitor.y + monitor.height - *y;
1975 *height = *y - monitor.y;
1979 if (popup_req.height > *height)
1981 vpolicy = GTK_POLICY_ALWAYS;
1983 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1989 cell_layout_is_sensitive (GtkCellLayout *layout)
1991 GList *cells, *list;
1994 cells = gtk_cell_layout_get_cells (layout);
1997 for (list = cells; list; list = list->next)
1999 g_object_get (list->data, "sensitive", &sensitive, NULL);
2004 g_list_free (cells);
2010 cell_is_sensitive (GtkCellRenderer *cell,
2013 gboolean *sensitive = data;
2015 g_object_get (cell, "sensitive", sensitive, NULL);
2021 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2024 GtkComboBoxPrivate *priv = combo_box->priv;
2026 if (priv->row_separator_func)
2028 if (priv->row_separator_func (priv->model, iter,
2029 priv->row_separator_data))
2037 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2041 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2050 update_menu_sensitivity (GtkComboBox *combo_box,
2053 GtkComboBoxPrivate *priv = combo_box->priv;
2054 GList *children, *child;
2055 GtkWidget *item, *submenu, *separator;
2056 GtkWidget *cell_view;
2062 children = gtk_container_get_children (GTK_CONTAINER (menu));
2064 for (child = children; child; child = child->next)
2066 item = GTK_WIDGET (child->data);
2067 cell_view = gtk_bin_get_child (GTK_BIN (item));
2069 if (!GTK_IS_CELL_VIEW (cell_view))
2072 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2073 if (submenu != NULL)
2075 gtk_widget_set_sensitive (item, TRUE);
2076 update_menu_sensitivity (combo_box, submenu);
2080 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2082 if (menu != priv->popup_widget && child == children)
2084 separator = GTK_WIDGET (child->next->data);
2085 g_object_set (item, "visible", sensitive, NULL);
2086 g_object_set (separator, "visible", sensitive, NULL);
2089 gtk_widget_set_sensitive (item, sensitive);
2093 g_list_free (children);
2097 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2099 guint32 activate_time)
2101 GtkComboBoxPrivate *priv = combo_box->priv;
2104 gint width, min_width, nat_width;
2106 update_menu_sensitivity (combo_box, priv->popup_widget);
2109 if (gtk_tree_row_reference_valid (priv->active_row))
2111 path = gtk_tree_row_reference_get_path (priv->active_row);
2112 active_item = gtk_tree_path_get_indices (path)[0];
2113 gtk_tree_path_free (path);
2115 if (priv->add_tearoffs)
2119 /* FIXME handle nested menus better */
2120 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2122 if (priv->wrap_width == 0)
2124 GtkAllocation allocation;
2126 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2127 width = allocation.width;
2128 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2129 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2131 if (combo_box->priv->popup_fixed_width)
2132 width = MAX (width, min_width);
2134 width = MAX (width, nat_width);
2136 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2139 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2141 gtk_combo_box_menu_position, combo_box,
2142 button, activate_time);
2146 popup_grab_on_window (GdkWindow *window,
2147 GdkDevice *keyboard,
2149 guint32 activate_time)
2152 gdk_device_grab (keyboard, window,
2153 GDK_OWNERSHIP_WINDOW, TRUE,
2154 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2155 NULL, activate_time) != GDK_GRAB_SUCCESS)
2159 gdk_device_grab (pointer, window,
2160 GDK_OWNERSHIP_WINDOW, TRUE,
2161 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2162 GDK_POINTER_MOTION_MASK,
2163 NULL, activate_time) != GDK_GRAB_SUCCESS)
2166 gdk_device_ungrab (keyboard, activate_time);
2175 * gtk_combo_box_popup:
2176 * @combo_box: a #GtkComboBox
2178 * Pops up the menu or dropdown list of @combo_box.
2180 * This function is mostly intended for use by accessibility technologies;
2181 * applications should have little use for it.
2186 gtk_combo_box_popup (GtkComboBox *combo_box)
2188 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2190 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2194 * gtk_combo_box_popup_for_device:
2195 * @combo_box: a #GtkComboBox
2196 * @device: a #GdkDevice
2198 * Pops up the menu or dropdown list of @combo_box, the popup window
2199 * will be grabbed so only @device and its associated pointer/keyboard
2200 * are the only #GdkDevice<!-- -->s able to send events to it.
2205 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2208 GtkComboBoxPrivate *priv = combo_box->priv;
2209 gint x, y, width, height;
2210 GtkTreePath *path = NULL, *ppath;
2211 GtkWidget *toplevel;
2212 GdkDevice *keyboard, *pointer;
2215 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2216 g_return_if_fail (GDK_IS_DEVICE (device));
2218 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2221 if (gtk_widget_get_mapped (priv->popup_widget))
2224 if (priv->grab_pointer && priv->grab_keyboard)
2227 time = gtk_get_current_event_time ();
2229 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2232 pointer = gdk_device_get_associated_device (device);
2237 keyboard = gdk_device_get_associated_device (device);
2240 if (GTK_IS_MENU (priv->popup_widget))
2242 gtk_combo_box_menu_popup (combo_box,
2243 priv->activate_button,
2244 priv->activate_time);
2248 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2249 if (GTK_IS_WINDOW (toplevel))
2250 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2251 GTK_WINDOW (priv->popup_window));
2253 gtk_widget_show_all (priv->scrolled_window);
2254 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2256 gtk_widget_set_size_request (priv->popup_window, width, height);
2257 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2259 if (gtk_tree_row_reference_valid (priv->active_row))
2261 path = gtk_tree_row_reference_get_path (priv->active_row);
2262 ppath = gtk_tree_path_copy (path);
2263 if (gtk_tree_path_up (ppath))
2264 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2266 gtk_tree_path_free (ppath);
2268 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2272 gtk_widget_show (priv->popup_window);
2276 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2278 gtk_tree_path_free (path);
2281 gtk_widget_grab_focus (priv->popup_window);
2282 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2285 if (!gtk_widget_has_focus (priv->tree_view))
2286 gtk_widget_grab_focus (priv->tree_view);
2288 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2289 keyboard, pointer, time))
2291 gtk_widget_hide (priv->popup_window);
2295 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2296 priv->grab_pointer = pointer;
2297 priv->grab_keyboard = keyboard;
2301 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2305 device = gtk_get_current_event_device ();
2309 GdkDeviceManager *device_manager;
2310 GdkDisplay *display;
2313 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2314 device_manager = gdk_display_get_device_manager (display);
2316 /* No device was set, pick the first master device */
2317 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2318 device = devices->data;
2319 g_list_free (devices);
2322 gtk_combo_box_popup_for_device (combo_box, device);
2326 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2328 if (combo_box->priv->popup_shown)
2330 gtk_combo_box_popdown (combo_box);
2338 * gtk_combo_box_popdown:
2339 * @combo_box: a #GtkComboBox
2341 * Hides the menu or dropdown list of @combo_box.
2343 * This function is mostly intended for use by accessibility technologies;
2344 * applications should have little use for it.
2349 gtk_combo_box_popdown (GtkComboBox *combo_box)
2351 GtkComboBoxPrivate *priv = combo_box->priv;
2353 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2355 if (GTK_IS_MENU (priv->popup_widget))
2357 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2361 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2364 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2365 gtk_widget_hide (priv->popup_window);
2366 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2369 priv->grab_pointer = NULL;
2370 priv->grab_keyboard = NULL;
2373 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2374 gtk_widget_get_preferred_size (combo_box->priv->button, \
2378 child.x = allocation->x + border.right; \
2380 child.x = allocation->x + allocation->width - req.width - border.left; \
2382 child.y = allocation->y + border.top; \
2383 child.width = req.width; \
2384 child.height = allocation->height - (border.top + border.bottom); \
2385 child.width = MAX (1, child.width); \
2386 child.height = MAX (1, child.height); \
2388 gtk_widget_size_allocate (combo_box->priv->button, &child);
2392 gtk_combo_box_size_allocate (GtkWidget *widget,
2393 GtkAllocation *allocation)
2395 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2396 GtkComboBoxPrivate *priv = combo_box->priv;
2397 GtkWidget *child_widget;
2398 gint focus_width, focus_pad;
2399 GtkAllocation child;
2401 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2404 gtk_widget_set_allocation (widget, allocation);
2405 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2406 get_widget_padding (widget, &border);
2408 gtk_widget_style_get (widget,
2409 "focus-line-width", &focus_width,
2410 "focus-padding", &focus_pad,
2413 if (!priv->tree_view)
2415 if (priv->cell_view)
2417 GtkBorder button_border;
2422 allocation->x += border.left;
2423 allocation->y += border.top;
2424 allocation->width -= border.left + border.right;
2425 allocation->height -= border.top + border.bottom;
2427 gtk_widget_size_allocate (priv->button, allocation);
2429 /* set some things ready */
2430 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2431 get_widget_padding (priv->button, &button_border);
2433 child.x = allocation->x;
2434 child.y = allocation->y;
2435 width = allocation->width;
2436 child.height = allocation->height;
2438 if (!priv->is_cell_renderer)
2440 child.x += border_width + button_border.left + focus_width + focus_pad;
2441 child.y += border_width + button_border.top + focus_width + focus_pad;
2442 width -= (2 * (border_width + focus_width + focus_pad)) +
2443 button_border.left + button_border.right;
2444 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2445 button_border.top + button_border.bottom;
2448 /* handle the children */
2449 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2450 child.width = req.width;
2452 child.x += width - req.width;
2453 child.width = MAX (1, child.width);
2454 child.height = MAX (1, child.height);
2455 gtk_widget_size_allocate (priv->arrow, &child);
2457 child.x += req.width;
2458 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2459 child.width = req.width;
2461 child.x -= req.width;
2462 child.width = MAX (1, child.width);
2463 child.height = MAX (1, child.height);
2464 gtk_widget_size_allocate (priv->separator, &child);
2468 child.x += req.width;
2469 child.width = allocation->x + allocation->width
2470 - (border_width + button_border.right + focus_width + focus_pad)
2475 child.width = child.x;
2476 child.x = allocation->x
2477 + border_width + button_border.left + focus_width + focus_pad;
2478 child.width -= child.x;
2481 if (gtk_widget_get_visible (priv->popup_widget))
2483 gint width, menu_width;
2485 if (priv->wrap_width == 0)
2487 GtkAllocation combo_box_allocation;
2489 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2490 width = combo_box_allocation.width;
2491 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2493 if (combo_box->priv->popup_fixed_width)
2494 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2496 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2498 gtk_widget_set_size_request (priv->popup_widget,
2499 MAX (width, menu_width), -1);
2502 /* reposition the menu after giving it a new width */
2503 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2506 child.width = MAX (1, child.width);
2507 child.height = MAX (1, child.height);
2508 gtk_widget_size_allocate (child_widget, &child);
2512 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2515 child.x = allocation->x + req.width + border.right;
2517 child.x = allocation->x + border.left;
2518 child.y = allocation->y + border.top;
2519 child.width = allocation->width - req.width - (border.left + border.right);
2520 child.width = MAX (1, child.width);
2521 child.height = MAX (1, child.height);
2522 gtk_widget_size_allocate (child_widget, &child);
2528 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2531 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2535 child.x = allocation->x + req.width;
2537 child.x = allocation->x;
2539 child.y = allocation->y;
2540 child.width = allocation->width - req.width;
2541 child.height = allocation->height;
2543 if (priv->cell_view_frame)
2545 child.x += border.left + border_width;
2546 child.y += border.top + border_width;
2547 child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right));
2548 child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom));
2549 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2552 if (priv->has_frame)
2554 GtkBorder frame_border;
2556 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2557 get_widget_padding (priv->cell_view_frame, &frame_border);
2559 child.x += border_width + frame_border.left;
2560 child.y += border_width + frame_border.right;
2561 child.width -= (2 * border_width) + frame_border.left + frame_border.right;
2562 child.height -= (2 * border_width) + frame_border.top + frame_border.bottom;
2567 child.x += border.left + border_width;
2568 child.y += border.top + border_width;
2569 child.width -= (2 * border_width) - (border.left + border.right);
2570 child.height -= (2 * border_width) - (border.top + border.bottom);
2573 if (gtk_widget_get_visible (priv->popup_window))
2575 gint x, y, width, height;
2576 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2577 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2578 gtk_widget_set_size_request (priv->popup_window, width, height);
2581 child.width = MAX (1, child.width);
2582 child.height = MAX (1, child.height);
2583 gtk_widget_size_allocate (child_widget, &child);
2587 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2590 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2592 GtkComboBoxPrivate *priv = combo_box->priv;
2596 g_signal_handler_disconnect (priv->model,
2598 g_signal_handler_disconnect (priv->model,
2600 g_signal_handler_disconnect (priv->model,
2601 priv->reordered_id);
2602 g_signal_handler_disconnect (priv->model,
2608 g_object_unref (priv->model);
2612 if (priv->active_row)
2614 gtk_tree_row_reference_free (priv->active_row);
2615 priv->active_row = NULL;
2618 if (priv->cell_view)
2619 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2623 gtk_combo_box_forall (GtkContainer *container,
2624 gboolean include_internals,
2625 GtkCallback callback,
2626 gpointer callback_data)
2628 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2629 GtkComboBoxPrivate *priv = combo_box->priv;
2632 if (include_internals)
2635 (* callback) (priv->button, callback_data);
2636 if (priv->cell_view_frame)
2637 (* callback) (priv->cell_view_frame, callback_data);
2640 child = gtk_bin_get_child (GTK_BIN (container));
2642 (* callback) (child, callback_data);
2646 gtk_combo_box_child_show (GtkWidget *widget,
2647 GtkComboBox *combo_box)
2649 GtkComboBoxPrivate *priv = combo_box->priv;
2651 priv->popup_shown = TRUE;
2652 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2656 gtk_combo_box_child_hide (GtkWidget *widget,
2657 GtkComboBox *combo_box)
2659 GtkComboBoxPrivate *priv = combo_box->priv;
2661 priv->popup_shown = FALSE;
2662 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2666 gtk_combo_box_draw (GtkWidget *widget,
2669 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2670 GtkComboBoxPrivate *priv = combo_box->priv;
2672 if (priv->shadow_type != GTK_SHADOW_NONE)
2674 GtkStyleContext *context;
2675 GtkStateFlags state;
2677 context = gtk_widget_get_style_context (widget);
2678 state = gtk_widget_get_state_flags (widget);
2679 gtk_style_context_set_state (context, state);
2681 gtk_render_background (context, cr, 0, 0,
2682 gtk_widget_get_allocated_width (widget),
2683 gtk_widget_get_allocated_height (widget));
2684 gtk_render_frame (context, cr, 0, 0,
2685 gtk_widget_get_allocated_width (widget),
2686 gtk_widget_get_allocated_height (widget));
2689 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2692 if (priv->tree_view && priv->cell_view_frame)
2694 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2695 priv->cell_view_frame, cr);
2698 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2699 gtk_bin_get_child (GTK_BIN (widget)),
2715 path_visible (GtkTreeView *view,
2721 /* Note that we rely on the fact that collapsed rows don't have nodes
2723 return _gtk_tree_view_find_node (view, path, &tree, &node);
2727 tree_next_func (GtkTreeModel *model,
2732 SearchData *search_data = (SearchData *)data;
2734 if (search_data->found)
2736 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2739 if (search_data->visible &&
2740 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2743 search_data->set = TRUE;
2744 search_data->iter = *iter;
2749 if (gtk_tree_path_compare (path, search_data->path) == 0)
2750 search_data->found = TRUE;
2756 tree_next (GtkComboBox *combo,
2757 GtkTreeModel *model,
2762 SearchData search_data;
2764 search_data.combo = combo;
2765 search_data.path = gtk_tree_model_get_path (model, iter);
2766 search_data.visible = visible;
2767 search_data.found = FALSE;
2768 search_data.set = FALSE;
2770 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2772 *next = search_data.iter;
2774 gtk_tree_path_free (search_data.path);
2776 return search_data.set;
2780 tree_prev_func (GtkTreeModel *model,
2785 SearchData *search_data = (SearchData *)data;
2787 if (gtk_tree_path_compare (path, search_data->path) == 0)
2789 search_data->found = TRUE;
2793 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2796 if (search_data->visible &&
2797 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2800 search_data->set = TRUE;
2801 search_data->iter = *iter;
2807 tree_prev (GtkComboBox *combo,
2808 GtkTreeModel *model,
2813 SearchData search_data;
2815 search_data.combo = combo;
2816 search_data.path = gtk_tree_model_get_path (model, iter);
2817 search_data.visible = visible;
2818 search_data.found = FALSE;
2819 search_data.set = FALSE;
2821 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2823 *prev = search_data.iter;
2825 gtk_tree_path_free (search_data.path);
2827 return search_data.set;
2831 tree_last_func (GtkTreeModel *model,
2836 SearchData *search_data = (SearchData *)data;
2838 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2841 /* Note that we rely on the fact that collapsed rows don't have nodes
2843 if (search_data->visible &&
2844 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2847 search_data->set = TRUE;
2848 search_data->iter = *iter;
2854 tree_last (GtkComboBox *combo,
2855 GtkTreeModel *model,
2859 SearchData search_data;
2861 search_data.combo = combo;
2862 search_data.visible = visible;
2863 search_data.set = FALSE;
2865 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2867 *last = search_data.iter;
2869 return search_data.set;
2874 tree_first_func (GtkTreeModel *model,
2879 SearchData *search_data = (SearchData *)data;
2881 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2884 if (search_data->visible &&
2885 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2888 search_data->set = TRUE;
2889 search_data->iter = *iter;
2895 tree_first (GtkComboBox *combo,
2896 GtkTreeModel *model,
2900 SearchData search_data;
2902 search_data.combo = combo;
2903 search_data.visible = visible;
2904 search_data.set = FALSE;
2906 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2908 *first = search_data.iter;
2910 return search_data.set;
2914 gtk_combo_box_scroll_event (GtkWidget *widget,
2915 GdkEventScroll *event)
2917 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2920 GtkTreeIter new_iter;
2922 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2925 if (event->direction == GDK_SCROLL_UP)
2926 found = tree_prev (combo_box, combo_box->priv->model,
2927 &iter, &new_iter, FALSE);
2929 found = tree_next (combo_box, combo_box->priv->model,
2930 &iter, &new_iter, FALSE);
2933 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2942 gtk_combo_box_row_separator_func (GtkTreeModel *model,
2946 GtkComboBoxPrivate *priv = combo->priv;
2948 if (priv->row_separator_func)
2949 return priv->row_separator_func (model, iter, priv->row_separator_data);
2955 gtk_combo_box_header_func (GtkTreeModel *model,
2959 /* Every submenu has a selectable header, however we
2960 * can expose a method to make that configurable by
2961 * the user (like row_separator_func is done) */
2966 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2967 gboolean add_children)
2969 GtkComboBoxPrivate *priv = combo_box->priv;
2973 child = gtk_bin_get_child (GTK_BIN (combo_box));
2975 if (priv->cell_view)
2977 priv->button = gtk_toggle_button_new ();
2978 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2979 priv->focus_on_click);
2981 g_signal_connect (priv->button, "toggled",
2982 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2983 gtk_widget_set_parent (priv->button,
2984 gtk_widget_get_parent (child));
2986 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2987 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2989 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
2990 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2992 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2993 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2995 gtk_widget_show_all (priv->button);
2999 priv->button = gtk_toggle_button_new ();
3000 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3001 priv->focus_on_click);
3003 g_signal_connect (priv->button, "toggled",
3004 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3005 gtk_widget_set_parent (priv->button,
3006 gtk_widget_get_parent (child));
3008 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3009 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3010 gtk_widget_show_all (priv->button);
3013 g_signal_connect (priv->button, "button-press-event",
3014 G_CALLBACK (gtk_combo_box_menu_button_press),
3016 g_signal_connect (priv->button, "state-flags-changed",
3017 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3020 /* create our funky menu */
3021 menu = _gtk_tree_menu_new_with_area (priv->area);
3022 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3024 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3026 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3027 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3028 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3029 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3030 combo_box->priv->add_tearoffs);
3032 g_signal_connect (menu, "menu-activate",
3033 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3035 /* Chain our row_separator_func through */
3036 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3037 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3040 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3041 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3044 g_signal_connect (menu, "key-press-event",
3045 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3046 gtk_combo_box_set_popup_widget (combo_box, menu);
3048 gtk_combo_box_update_title (combo_box);
3052 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3054 GtkComboBoxPrivate *priv = combo_box->priv;
3056 g_signal_handlers_disconnect_matched (priv->button,
3057 G_SIGNAL_MATCH_DATA,
3059 gtk_combo_box_menu_button_press, NULL);
3060 g_signal_handlers_disconnect_matched (priv->button,
3061 G_SIGNAL_MATCH_DATA,
3063 gtk_combo_box_button_state_flags_changed, combo_box);
3064 g_signal_handlers_disconnect_matched (priv->popup_widget,
3065 G_SIGNAL_MATCH_DATA,
3067 gtk_combo_box_menu_activate, combo_box);
3069 /* unparent will remove our latest ref */
3070 gtk_widget_unparent (priv->button);
3073 priv->button = NULL;
3075 priv->separator = NULL;
3077 /* changing the popup window will unref the menu and the children */
3082 gtk_combo_box_menu_button_press (GtkWidget *widget,
3083 GdkEventButton *event,
3086 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3087 GtkComboBoxPrivate *priv = combo_box->priv;
3089 if (GTK_IS_MENU (priv->popup_widget) &&
3090 event->type == GDK_BUTTON_PRESS && event->button == 1)
3092 if (priv->focus_on_click &&
3093 !gtk_widget_has_focus (priv->button))
3094 gtk_widget_grab_focus (priv->button);
3096 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3105 gtk_combo_box_menu_activate (GtkWidget *menu,
3107 GtkComboBox *combo_box)
3111 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3112 gtk_combo_box_set_active_iter (combo_box, &iter);
3114 g_object_set (combo_box,
3115 "editing-canceled", FALSE,
3121 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3124 gboolean sensitive = TRUE; /* fool code checkers */
3126 if (!combo_box->priv->button)
3129 switch (combo_box->priv->button_sensitivity)
3131 case GTK_SENSITIVITY_ON:
3134 case GTK_SENSITIVITY_OFF:
3137 case GTK_SENSITIVITY_AUTO:
3138 sensitive = combo_box->priv->model &&
3139 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3142 g_assert_not_reached ();
3146 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3148 /* In list-mode, we also need to update sensitivity of the event box */
3149 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3150 && combo_box->priv->cell_view)
3151 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3155 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3160 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3162 if (combo_box->priv->tree_view)
3163 gtk_combo_box_list_popup_resize (combo_box);
3165 gtk_combo_box_update_sensitivity (combo_box);
3169 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3173 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3174 GtkComboBoxPrivate *priv = combo_box->priv;
3176 if (!gtk_tree_row_reference_valid (priv->active_row))
3178 if (priv->cell_view)
3179 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3180 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3183 if (priv->tree_view)
3184 gtk_combo_box_list_popup_resize (combo_box);
3186 gtk_combo_box_update_sensitivity (combo_box);
3190 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3196 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3200 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3205 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3206 GtkComboBoxPrivate *priv = combo_box->priv;
3207 GtkTreePath *active_path;
3209 /* FIXME this belongs to GtkCellView */
3210 if (gtk_tree_row_reference_valid (priv->active_row))
3212 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3213 if (gtk_tree_path_compare (path, active_path) == 0 &&
3215 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3216 gtk_tree_path_free (active_path);
3219 if (priv->tree_view)
3220 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3224 list_popup_resize_idle (gpointer user_data)
3226 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3227 GtkComboBoxPrivate *priv = combo_box->priv;
3228 gint x, y, width, height;
3230 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3232 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3234 gtk_widget_set_size_request (priv->popup_window, width, height);
3235 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3238 priv->resize_idle_id = 0;
3244 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3246 GtkComboBoxPrivate *priv = combo_box->priv;
3248 if (!priv->resize_idle_id)
3249 priv->resize_idle_id =
3250 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3254 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3259 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3261 gtk_combo_box_list_popup_resize (combo_box);
3270 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3272 GtkComboBoxPrivate *priv = combo_box->priv;
3273 GtkTreeSelection *sel;
3275 GtkWidget *widget = GTK_WIDGET (combo_box);
3277 priv->button = gtk_toggle_button_new ();
3278 child = gtk_bin_get_child (GTK_BIN (combo_box));
3279 gtk_widget_set_parent (priv->button,
3280 gtk_widget_get_parent (child));
3281 g_signal_connect (priv->button, "button-press-event",
3282 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3283 g_signal_connect (priv->button, "toggled",
3284 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3286 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3287 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3288 priv->separator = NULL;
3289 gtk_widget_show_all (priv->button);
3291 if (priv->cell_view)
3293 GtkStyleContext *context;
3294 GtkStateFlags state;
3297 context = gtk_widget_get_style_context (widget);
3298 state = gtk_widget_get_state_flags (widget);
3300 gtk_style_context_get (context, state,
3301 "background-color", &color,
3304 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color);
3305 gdk_rgba_free (color);
3307 priv->box = gtk_event_box_new ();
3308 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3311 if (priv->has_frame)
3313 priv->cell_view_frame = gtk_frame_new (NULL);
3314 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3319 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3320 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3324 gtk_widget_set_parent (priv->cell_view_frame,
3325 gtk_widget_get_parent (child));
3326 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3327 gtk_widget_show_all (priv->cell_view_frame);
3329 g_signal_connect (priv->box, "button-press-event",
3330 G_CALLBACK (gtk_combo_box_list_button_pressed),
3334 priv->tree_view = gtk_tree_view_new ();
3335 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3336 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3337 gtk_tree_selection_set_select_function (sel,
3338 gtk_combo_box_list_select_func,
3340 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3342 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3345 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3346 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3350 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3352 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3353 gtk_tree_view_column_new_with_area (priv->area));
3355 if (gtk_tree_row_reference_valid (priv->active_row))
3359 path = gtk_tree_row_reference_get_path (priv->active_row);
3360 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3362 gtk_tree_path_free (path);
3365 /* set sample/popup widgets */
3366 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3368 g_signal_connect (priv->tree_view, "key-press-event",
3369 G_CALLBACK (gtk_combo_box_list_key_press),
3371 g_signal_connect (priv->tree_view, "enter-notify-event",
3372 G_CALLBACK (gtk_combo_box_list_enter_notify),
3374 g_signal_connect (priv->tree_view, "row-expanded",
3375 G_CALLBACK (gtk_combo_box_model_row_expanded),
3377 g_signal_connect (priv->tree_view, "row-collapsed",
3378 G_CALLBACK (gtk_combo_box_model_row_expanded),
3380 g_signal_connect (priv->popup_window, "button-press-event",
3381 G_CALLBACK (gtk_combo_box_list_button_pressed),
3383 g_signal_connect (priv->popup_window, "button-release-event",
3384 G_CALLBACK (gtk_combo_box_list_button_released),
3387 gtk_widget_show (priv->tree_view);
3389 gtk_combo_box_update_sensitivity (combo_box);
3393 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3395 GtkComboBoxPrivate *priv = combo_box->priv;
3397 /* disconnect signals */
3398 g_signal_handlers_disconnect_matched (priv->tree_view,
3399 G_SIGNAL_MATCH_DATA,
3400 0, 0, NULL, NULL, combo_box);
3401 g_signal_handlers_disconnect_matched (priv->button,
3402 G_SIGNAL_MATCH_DATA,
3404 gtk_combo_box_list_button_pressed,
3406 g_signal_handlers_disconnect_matched (priv->popup_window,
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_released,
3417 g_signal_handlers_disconnect_matched (priv->popup_window,
3418 G_SIGNAL_MATCH_DATA,
3420 gtk_combo_box_child_show,
3423 g_signal_handlers_disconnect_matched (priv->popup_window,
3424 G_SIGNAL_MATCH_DATA,
3426 gtk_combo_box_child_hide,
3430 g_signal_handlers_disconnect_matched (priv->box,
3431 G_SIGNAL_MATCH_DATA,
3433 gtk_combo_box_list_button_pressed,
3436 /* destroy things (unparent will kill the latest ref from us)
3437 * last unref on button will destroy the arrow
3439 gtk_widget_unparent (priv->button);
3440 priv->button = NULL;
3443 if (priv->cell_view)
3445 g_object_set (priv->cell_view,
3446 "background-set", FALSE,
3450 if (priv->cell_view_frame)
3452 gtk_widget_unparent (priv->cell_view_frame);
3453 priv->cell_view_frame = NULL;
3457 if (priv->scroll_timer)
3459 g_source_remove (priv->scroll_timer);
3460 priv->scroll_timer = 0;
3463 if (priv->resize_idle_id)
3465 g_source_remove (priv->resize_idle_id);
3466 priv->resize_idle_id = 0;
3469 gtk_widget_destroy (priv->tree_view);
3471 priv->tree_view = NULL;
3472 if (priv->popup_widget)
3474 g_object_unref (priv->popup_widget);
3475 priv->popup_widget = NULL;
3482 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3483 GdkEventButton *event,
3486 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3487 GtkComboBoxPrivate *priv = combo_box->priv;
3489 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3491 if (ewidget == priv->popup_window)
3494 if ((ewidget != priv->button && ewidget != priv->box) ||
3495 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3498 if (priv->focus_on_click &&
3499 !gtk_widget_has_focus (priv->button))
3500 gtk_widget_grab_focus (priv->button);
3502 gtk_combo_box_popup_for_device (combo_box, event->device);
3504 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3506 priv->auto_scroll = FALSE;
3507 if (priv->scroll_timer == 0)
3508 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3509 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3512 priv->popup_in_progress = TRUE;
3518 gtk_combo_box_list_button_released (GtkWidget *widget,
3519 GdkEventButton *event,
3523 GtkTreePath *path = NULL;
3526 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3527 GtkComboBoxPrivate *priv = combo_box->priv;
3529 gboolean popup_in_progress = FALSE;
3531 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3533 if (priv->popup_in_progress)
3535 popup_in_progress = TRUE;
3536 priv->popup_in_progress = FALSE;
3539 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3541 if (priv->scroll_timer)
3543 g_source_remove (priv->scroll_timer);
3544 priv->scroll_timer = 0;
3547 if (ewidget != priv->tree_view)
3549 if ((ewidget == priv->button ||
3550 ewidget == priv->box) &&
3551 !popup_in_progress &&
3552 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3554 gtk_combo_box_popdown (combo_box);
3558 /* released outside treeview */
3559 if (ewidget != priv->button && ewidget != priv->box)
3561 gtk_combo_box_popdown (combo_box);
3569 /* select something cool */
3570 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3576 return TRUE; /* clicked outside window? */
3578 gtk_tree_model_get_iter (priv->model, &iter, path);
3579 gtk_tree_path_free (path);
3581 gtk_combo_box_popdown (combo_box);
3583 if (tree_column_row_is_sensitive (combo_box, &iter))
3584 gtk_combo_box_set_active_iter (combo_box, &iter);
3590 gtk_combo_box_menu_key_press (GtkWidget *widget,
3594 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3596 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3598 /* The menu hasn't managed the
3599 * event, forward it to the combobox
3601 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3608 gtk_combo_box_list_key_press (GtkWidget *widget,
3612 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3615 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3616 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3618 GtkTreeModel *model = NULL;
3620 gtk_combo_box_popdown (combo_box);
3622 if (combo_box->priv->model)
3624 GtkTreeSelection *sel;
3626 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3628 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3629 gtk_combo_box_set_active_iter (combo_box, &iter);
3635 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3637 /* The list hasn't managed the
3638 * event, forward it to the combobox
3640 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3647 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3652 GtkAllocation allocation;
3653 GtkWidget *tree_view = combo_box->priv->tree_view;
3656 gtk_widget_get_allocation (tree_view, &allocation);
3658 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3659 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3661 if (x <= allocation.x &&
3662 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3664 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3665 gtk_adjustment_set_value (adj, value);
3667 else if (x >= allocation.x + allocation.width &&
3668 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3670 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3671 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3675 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3676 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3678 if (y <= allocation.y &&
3679 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3681 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3682 gtk_adjustment_set_value (adj, value);
3684 else if (y >= allocation.height &&
3685 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3687 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3688 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3694 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3696 GtkComboBoxPrivate *priv = combo_box->priv;
3699 if (priv->auto_scroll)
3701 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3704 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3711 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3712 GdkEventCrossing *event,
3715 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3717 combo_box->priv->auto_scroll = TRUE;
3723 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3724 GtkTreeModel *model,
3726 gboolean path_currently_selected,
3729 GList *list, *columns;
3730 gboolean sensitive = FALSE;
3732 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3734 for (list = columns; list && !sensitive; list = list->next)
3736 GList *cells, *cell;
3737 gboolean cell_sensitive, cell_visible;
3739 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3741 if (!gtk_tree_view_column_get_visible (column))
3744 gtk_tree_model_get_iter (model, &iter, path);
3745 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3748 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3751 g_object_get (cell->data,
3752 "sensitive", &cell_sensitive,
3753 "visible", &cell_visible,
3756 if (cell_visible && cell_sensitive)
3761 g_list_free (cells);
3763 sensitive = cell_sensitive;
3766 g_list_free (columns);
3772 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3777 /* XXX Do nothing ? */
3781 * GtkCellLayout implementation
3783 static GtkCellArea *
3784 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3786 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3787 GtkComboBoxPrivate *priv = combo->priv;
3789 if (G_UNLIKELY (!priv->area))
3791 priv->area = gtk_cell_area_box_new ();
3792 g_object_ref_sink (priv->area);
3803 * gtk_combo_box_new:
3805 * Creates a new empty #GtkComboBox.
3807 * Return value: A new #GtkComboBox.
3812 gtk_combo_box_new (void)
3814 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3818 * gtk_combo_box_new_with_area:
3819 * @area: the #GtkCellArea to use to layout cell renderers
3821 * Creates a new empty #GtkComboBox using @area to layout cells.
3823 * Return value: A new #GtkComboBox.
3826 gtk_combo_box_new_with_area (GtkCellArea *area)
3828 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3832 * gtk_combo_box_new_with_area_and_entry:
3833 * @area: the #GtkCellArea to use to layout cell renderers
3835 * Creates a new empty #GtkComboBox with an entry.
3837 * The new combo box will use @area to layout cells.
3839 * Return value: A new #GtkComboBox.
3842 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3844 return g_object_new (GTK_TYPE_COMBO_BOX,
3852 * gtk_combo_box_new_with_entry:
3854 * Creates a new empty #GtkComboBox with an entry.
3856 * Return value: A new #GtkComboBox.
3859 gtk_combo_box_new_with_entry (void)
3861 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3865 * gtk_combo_box_new_with_model:
3866 * @model: A #GtkTreeModel.
3868 * Creates a new #GtkComboBox with the model initialized to @model.
3870 * Return value: A new #GtkComboBox.
3875 gtk_combo_box_new_with_model (GtkTreeModel *model)
3877 GtkComboBox *combo_box;
3879 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3881 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3883 return GTK_WIDGET (combo_box);
3887 * gtk_combo_box_new_with_model_and_entry:
3888 * @model: A #GtkTreeModel
3890 * Creates a new empty #GtkComboBox with an entry
3891 * and with the model initialized to @model.
3893 * Return value: A new #GtkComboBox
3896 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3898 return g_object_new (GTK_TYPE_COMBO_BOX,
3905 * gtk_combo_box_get_wrap_width:
3906 * @combo_box: A #GtkComboBox
3908 * Returns the wrap width which is used to determine the number of columns
3909 * for the popup menu. If the wrap width is larger than 1, the combo box
3912 * Returns: the wrap width.
3917 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3919 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3921 return combo_box->priv->wrap_width;
3925 * gtk_combo_box_set_wrap_width:
3926 * @combo_box: A #GtkComboBox
3927 * @width: Preferred number of columns
3929 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3930 * the preferred number of columns when you want the popup to be layed out
3936 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3939 GtkComboBoxPrivate *priv;
3941 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3942 g_return_if_fail (width >= 0);
3944 priv = combo_box->priv;
3946 if (width != priv->wrap_width)
3948 priv->wrap_width = width;
3950 gtk_combo_box_check_appearance (combo_box);
3952 if (GTK_IS_TREE_MENU (priv->popup_widget))
3953 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
3955 g_object_notify (G_OBJECT (combo_box), "wrap-width");
3960 * gtk_combo_box_get_row_span_column:
3961 * @combo_box: A #GtkComboBox
3963 * Returns the column with row span information for @combo_box.
3965 * Returns: the row span column.
3970 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3972 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3974 return combo_box->priv->row_column;
3978 * gtk_combo_box_set_row_span_column:
3979 * @combo_box: A #GtkComboBox.
3980 * @row_span: A column in the model passed during construction.
3982 * Sets the column with row span information for @combo_box to be @row_span.
3983 * The row span column contains integers which indicate how many rows
3984 * an item should span.
3989 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3992 GtkComboBoxPrivate *priv;
3995 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3997 priv = combo_box->priv;
3999 col = gtk_tree_model_get_n_columns (priv->model);
4000 g_return_if_fail (row_span >= -1 && row_span < col);
4002 if (row_span != priv->row_column)
4004 priv->row_column = row_span;
4006 if (GTK_IS_TREE_MENU (priv->popup_widget))
4007 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4009 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4014 * gtk_combo_box_get_column_span_column:
4015 * @combo_box: A #GtkComboBox
4017 * Returns the column with column span information for @combo_box.
4019 * Returns: the column span column.
4024 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4026 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4028 return combo_box->priv->col_column;
4032 * gtk_combo_box_set_column_span_column:
4033 * @combo_box: A #GtkComboBox
4034 * @column_span: A column in the model passed during construction
4036 * Sets the column with column span information for @combo_box to be
4037 * @column_span. The column span column contains integers which indicate
4038 * how many columns an item should span.
4043 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4046 GtkComboBoxPrivate *priv;
4049 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4051 priv = combo_box->priv;
4053 col = gtk_tree_model_get_n_columns (priv->model);
4054 g_return_if_fail (column_span >= -1 && column_span < col);
4056 if (column_span != priv->col_column)
4058 priv->col_column = column_span;
4060 if (GTK_IS_TREE_MENU (priv->popup_widget))
4061 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4063 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4068 * gtk_combo_box_get_active:
4069 * @combo_box: A #GtkComboBox
4071 * Returns the index of the currently active item, or -1 if there's no
4072 * active item. If the model is a non-flat treemodel, and the active item
4073 * is not an immediate child of the root of the tree, this function returns
4074 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4075 * <literal>path</literal> is the #GtkTreePath of the active item.
4077 * Return value: An integer which is the index of the currently active item,
4078 * or -1 if there's no active item.
4083 gtk_combo_box_get_active (GtkComboBox *combo_box)
4085 GtkComboBoxPrivate *priv;
4088 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4090 priv = combo_box->priv;
4092 if (gtk_tree_row_reference_valid (priv->active_row))
4096 path = gtk_tree_row_reference_get_path (priv->active_row);
4097 result = gtk_tree_path_get_indices (path)[0];
4098 gtk_tree_path_free (path);
4107 * gtk_combo_box_set_active:
4108 * @combo_box: A #GtkComboBox
4109 * @index_: An index in the model passed during construction, or -1 to have
4112 * Sets the active item of @combo_box to be the item at @index.
4117 gtk_combo_box_set_active (GtkComboBox *combo_box,
4120 GtkTreePath *path = NULL;
4121 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4122 g_return_if_fail (index_ >= -1);
4124 if (combo_box->priv->model == NULL)
4126 /* Save index, in case the model is set after the index */
4127 combo_box->priv->active = index_;
4133 path = gtk_tree_path_new_from_indices (index_, -1);
4135 gtk_combo_box_set_active_internal (combo_box, path);
4138 gtk_tree_path_free (path);
4142 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4145 GtkComboBoxPrivate *priv = combo_box->priv;
4146 GtkTreePath *active_path;
4149 /* Remember whether the initially active row is valid. */
4150 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4152 if (path && is_valid_row_reference)
4154 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4155 path_cmp = gtk_tree_path_compare (path, active_path);
4156 gtk_tree_path_free (active_path);
4161 if (priv->active_row)
4163 gtk_tree_row_reference_free (priv->active_row);
4164 priv->active_row = NULL;
4169 if (priv->tree_view)
4170 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4173 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4175 if (GTK_IS_MENU (menu))
4176 gtk_menu_set_active (menu, -1);
4179 if (priv->cell_view)
4180 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4183 * Do not emit a "changed" signal when an already invalid selection was
4184 * now set to invalid.
4186 if (!is_valid_row_reference)
4192 gtk_tree_row_reference_new (priv->model, path);
4194 if (priv->tree_view)
4196 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4199 else if (GTK_IS_MENU (priv->popup_widget))
4201 /* FIXME handle nested menus better */
4202 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4203 gtk_tree_path_get_indices (path)[0]);
4206 if (priv->cell_view)
4207 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4211 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4212 g_object_notify (G_OBJECT (combo_box), "active");
4213 if (combo_box->priv->id_column >= 0)
4214 g_object_notify (G_OBJECT (combo_box), "active-id");
4219 * gtk_combo_box_get_active_iter:
4220 * @combo_box: A #GtkComboBox
4221 * @iter: (out): The uninitialized #GtkTreeIter
4223 * Sets @iter to point to the current active item, if it exists.
4225 * Return value: %TRUE, if @iter was set
4230 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4236 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4238 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4241 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4242 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4243 gtk_tree_path_free (path);
4249 * gtk_combo_box_set_active_iter:
4250 * @combo_box: A #GtkComboBox
4251 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4253 * Sets the current active item to be the one referenced by @iter, or
4254 * unsets the active item if @iter is %NULL.
4259 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4262 GtkTreePath *path = NULL;
4264 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4267 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4269 gtk_combo_box_set_active_internal (combo_box, path);
4270 gtk_tree_path_free (path);
4274 * gtk_combo_box_set_model:
4275 * @combo_box: A #GtkComboBox
4276 * @model: (allow-none): A #GtkTreeModel
4278 * Sets the model used by @combo_box to be @model. Will unset a previously set
4279 * model (if applicable). If model is %NULL, then it will unset the model.
4281 * Note that this function does not clear the cell renderers, you have to
4282 * call gtk_cell_layout_clear() yourself if you need to set up different
4283 * cell renderers for the new model.
4288 gtk_combo_box_set_model (GtkComboBox *combo_box,
4289 GtkTreeModel *model)
4291 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4292 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4294 if (model == combo_box->priv->model)
4297 gtk_combo_box_unset_model (combo_box);
4302 combo_box->priv->model = model;
4303 g_object_ref (combo_box->priv->model);
4305 combo_box->priv->inserted_id =
4306 g_signal_connect (combo_box->priv->model, "row-inserted",
4307 G_CALLBACK (gtk_combo_box_model_row_inserted),
4309 combo_box->priv->deleted_id =
4310 g_signal_connect (combo_box->priv->model, "row-deleted",
4311 G_CALLBACK (gtk_combo_box_model_row_deleted),
4313 combo_box->priv->reordered_id =
4314 g_signal_connect (combo_box->priv->model, "rows-reordered",
4315 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4317 combo_box->priv->changed_id =
4318 g_signal_connect (combo_box->priv->model, "row-changed",
4319 G_CALLBACK (gtk_combo_box_model_row_changed),
4322 if (combo_box->priv->tree_view)
4325 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4326 combo_box->priv->model);
4327 gtk_combo_box_list_popup_resize (combo_box);
4330 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4333 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4334 combo_box->priv->model);
4337 if (combo_box->priv->cell_view)
4338 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4339 combo_box->priv->model);
4341 if (combo_box->priv->active != -1)
4343 /* If an index was set in advance, apply it now */
4344 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4345 combo_box->priv->active = -1;
4349 gtk_combo_box_update_sensitivity (combo_box);
4351 g_object_notify (G_OBJECT (combo_box), "model");
4355 * gtk_combo_box_get_model:
4356 * @combo_box: A #GtkComboBox
4358 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4360 * Return value: (transfer none): A #GtkTreeModel which was passed
4361 * during construction.
4366 gtk_combo_box_get_model (GtkComboBox *combo_box)
4368 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4370 return combo_box->priv->model;
4374 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4375 GtkScrollType scroll)
4378 GtkTreeIter new_iter;
4379 gboolean active_iter;
4382 if (!combo_box->priv->model)
4384 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4388 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4392 case GTK_SCROLL_STEP_BACKWARD:
4393 case GTK_SCROLL_STEP_UP:
4394 case GTK_SCROLL_STEP_LEFT:
4397 found = tree_prev (combo_box, combo_box->priv->model,
4398 &iter, &new_iter, FALSE);
4401 /* else fall through */
4403 case GTK_SCROLL_PAGE_FORWARD:
4404 case GTK_SCROLL_PAGE_DOWN:
4405 case GTK_SCROLL_PAGE_RIGHT:
4406 case GTK_SCROLL_END:
4407 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4410 case GTK_SCROLL_STEP_FORWARD:
4411 case GTK_SCROLL_STEP_DOWN:
4412 case GTK_SCROLL_STEP_RIGHT:
4415 found = tree_next (combo_box, combo_box->priv->model,
4416 &iter, &new_iter, FALSE);
4419 /* else fall through */
4421 case GTK_SCROLL_PAGE_BACKWARD:
4422 case GTK_SCROLL_PAGE_UP:
4423 case GTK_SCROLL_PAGE_LEFT:
4424 case GTK_SCROLL_START:
4425 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4432 if (found && active_iter)
4434 GtkTreePath *old_path;
4435 GtkTreePath *new_path;
4437 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4438 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4440 if (gtk_tree_path_compare (old_path, new_path) == 0)
4443 gtk_tree_path_free (old_path);
4444 gtk_tree_path_free (new_path);
4449 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4453 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4458 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4459 gboolean group_cycling)
4461 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4463 if (combo_box->priv->has_entry)
4467 child = gtk_bin_get_child (GTK_BIN (combo_box));
4469 gtk_widget_grab_focus (child);
4472 gtk_widget_grab_focus (combo_box->priv->button);
4478 gtk_combo_box_grab_focus (GtkWidget *widget)
4480 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4482 if (combo_box->priv->has_entry)
4486 child = gtk_bin_get_child (GTK_BIN (combo_box));
4488 gtk_widget_grab_focus (child);
4491 gtk_widget_grab_focus (combo_box->priv->button);
4495 gtk_combo_box_destroy (GtkWidget *widget)
4497 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4499 if (combo_box->priv->popup_idle_id > 0)
4501 g_source_remove (combo_box->priv->popup_idle_id);
4502 combo_box->priv->popup_idle_id = 0;
4505 gtk_combo_box_popdown (combo_box);
4507 if (combo_box->priv->row_separator_destroy)
4508 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4510 combo_box->priv->row_separator_func = NULL;
4511 combo_box->priv->row_separator_data = NULL;
4512 combo_box->priv->row_separator_destroy = NULL;
4514 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4515 combo_box->priv->cell_view = NULL;
4519 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4522 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4525 * Fixes regression reported in bug #574059. The old functionality relied on
4526 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4527 * when the selection was already set to -1.
4529 if (gtk_combo_box_get_active(combo_box) == -1)
4530 g_signal_emit_by_name (combo_box, "changed");
4532 gtk_combo_box_set_active (combo_box, -1);
4536 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4539 GtkComboBoxPrivate *priv = combo_box->priv;
4540 GtkTreeModel *model;
4543 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4545 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4549 GValue value = {0,};
4551 g_signal_handlers_block_by_func (entry,
4552 gtk_combo_box_entry_contents_changed,
4555 model = gtk_combo_box_get_model (combo_box);
4557 gtk_tree_model_get_value (model, &iter,
4558 priv->text_column, &value);
4559 g_object_set_property (G_OBJECT (entry), "text", &value);
4560 g_value_unset (&value);
4562 g_signal_handlers_unblock_by_func (entry,
4563 gtk_combo_box_entry_contents_changed,
4570 gtk_combo_box_constructor (GType type,
4571 guint n_construct_properties,
4572 GObjectConstructParam *construct_properties)
4575 GtkComboBox *combo_box;
4576 GtkComboBoxPrivate *priv;
4578 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4579 (type, n_construct_properties, construct_properties);
4581 combo_box = GTK_COMBO_BOX (object);
4582 priv = combo_box->priv;
4586 priv->area = gtk_cell_area_box_new ();
4587 g_object_ref_sink (priv->area);
4590 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4591 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4592 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4593 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4594 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4595 gtk_widget_show (priv->cell_view);
4597 gtk_combo_box_check_appearance (combo_box);
4599 if (priv->has_entry)
4603 entry = gtk_entry_new ();
4604 gtk_widget_show (entry);
4605 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4607 priv->text_renderer = gtk_cell_renderer_text_new ();
4608 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4609 priv->text_renderer, TRUE);
4611 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4613 g_signal_connect (combo_box, "changed",
4614 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4622 gtk_combo_box_dispose(GObject* object)
4624 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4626 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4628 gtk_combo_box_menu_destroy (combo_box);
4629 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4630 combo_box->priv->popup_widget = NULL;
4633 if (combo_box->priv->area)
4635 g_object_unref (combo_box->priv->area);
4636 combo_box->priv->area = NULL;
4639 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4640 gtk_combo_box_list_destroy (combo_box);
4642 if (combo_box->priv->popup_window)
4644 gtk_widget_destroy (combo_box->priv->popup_window);
4645 combo_box->priv->popup_window = NULL;
4648 gtk_combo_box_unset_model (combo_box);
4650 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4654 gtk_combo_box_finalize (GObject *object)
4656 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4658 g_free (combo_box->priv->tearoff_title);
4660 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4664 gtk_cell_editable_key_press (GtkWidget *widget,
4668 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4670 if (event->keyval == GDK_KEY_Escape)
4672 g_object_set (combo_box,
4673 "editing-canceled", TRUE,
4675 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4676 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4680 else if (event->keyval == GDK_KEY_Return ||
4681 event->keyval == GDK_KEY_ISO_Enter ||
4682 event->keyval == GDK_KEY_KP_Enter)
4684 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4685 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4694 popdown_idle (gpointer data)
4696 GtkComboBox *combo_box;
4698 combo_box = GTK_COMBO_BOX (data);
4700 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4701 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4703 g_object_unref (combo_box);
4709 popdown_handler (GtkWidget *widget,
4712 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4716 popup_idle (gpointer data)
4718 GtkComboBox *combo_box;
4720 combo_box = GTK_COMBO_BOX (data);
4722 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4723 combo_box->priv->cell_view)
4724 g_signal_connect_object (combo_box->priv->popup_widget,
4725 "unmap", G_CALLBACK (popdown_handler),
4728 /* we unset this if a menu item is activated */
4729 g_object_set (combo_box,
4730 "editing-canceled", TRUE,
4732 gtk_combo_box_popup (combo_box);
4734 combo_box->priv->popup_idle_id = 0;
4735 combo_box->priv->activate_button = 0;
4736 combo_box->priv->activate_time = 0;
4742 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4745 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4748 combo_box->priv->is_cell_renderer = TRUE;
4750 if (combo_box->priv->cell_view)
4752 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4753 G_CALLBACK (gtk_cell_editable_key_press),
4756 gtk_widget_grab_focus (combo_box->priv->button);
4760 child = gtk_bin_get_child (GTK_BIN (combo_box));
4762 g_signal_connect_object (child, "key-press-event",
4763 G_CALLBACK (gtk_cell_editable_key_press),
4766 gtk_widget_grab_focus (child);
4767 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4770 /* we do the immediate popup only for the optionmenu-like
4773 if (combo_box->priv->is_cell_renderer &&
4774 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4776 if (event && event->type == GDK_BUTTON_PRESS)
4778 GdkEventButton *event_button = (GdkEventButton *)event;
4780 combo_box->priv->activate_button = event_button->button;
4781 combo_box->priv->activate_time = event_button->time;
4784 combo_box->priv->popup_idle_id =
4785 gdk_threads_add_idle (popup_idle, combo_box);
4791 * gtk_combo_box_get_add_tearoffs:
4792 * @combo_box: a #GtkComboBox
4794 * Gets the current value of the :add-tearoffs property.
4796 * Return value: the current value of the :add-tearoffs property.
4799 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4801 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4803 return combo_box->priv->add_tearoffs;
4807 * gtk_combo_box_set_add_tearoffs:
4808 * @combo_box: a #GtkComboBox
4809 * @add_tearoffs: %TRUE to add tearoff menu items
4811 * Sets whether the popup menu should have a tearoff
4817 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4818 gboolean add_tearoffs)
4820 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4822 add_tearoffs = add_tearoffs != FALSE;
4824 if (combo_box->priv->add_tearoffs != add_tearoffs)
4826 combo_box->priv->add_tearoffs = add_tearoffs;
4827 gtk_combo_box_check_appearance (combo_box);
4829 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4830 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4831 combo_box->priv->add_tearoffs);
4833 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4838 * gtk_combo_box_get_title:
4839 * @combo_box: a #GtkComboBox
4841 * Gets the current title of the menu in tearoff mode. See
4842 * gtk_combo_box_set_add_tearoffs().
4844 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4845 * string which must not be freed.
4849 G_CONST_RETURN gchar*
4850 gtk_combo_box_get_title (GtkComboBox *combo_box)
4852 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4854 return combo_box->priv->tearoff_title;
4858 gtk_combo_box_update_title (GtkComboBox *combo_box)
4860 gtk_combo_box_check_appearance (combo_box);
4862 if (combo_box->priv->popup_widget &&
4863 GTK_IS_MENU (combo_box->priv->popup_widget))
4864 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4865 combo_box->priv->tearoff_title);
4869 * gtk_combo_box_set_title:
4870 * @combo_box: a #GtkComboBox
4871 * @title: a title for the menu in tearoff mode
4873 * Sets the menu's title in tearoff mode.
4878 gtk_combo_box_set_title (GtkComboBox *combo_box,
4881 GtkComboBoxPrivate *priv;
4883 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4885 priv = combo_box->priv;
4887 if (strcmp (title ? title : "",
4888 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4890 g_free (priv->tearoff_title);
4891 priv->tearoff_title = g_strdup (title);
4893 gtk_combo_box_update_title (combo_box);
4895 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4901 * gtk_combo_box_set_popup_fixed_width:
4902 * @combo_box: a #GtkComboBox
4903 * @fixed: whether to use a fixed popup width
4905 * Specifies whether the popup's width should be a fixed width
4906 * matching the allocated width of the combo box.
4911 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4914 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4916 if (combo_box->priv->popup_fixed_width != fixed)
4918 combo_box->priv->popup_fixed_width = fixed;
4920 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
4925 * gtk_combo_box_get_popup_fixed_width:
4926 * @combo_box: a #GtkComboBox
4928 * Gets whether the popup uses a fixed width matching
4929 * the allocated width of the combo box.
4931 * Returns: %TRUE if the popup uses a fixed width
4936 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
4938 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4940 return combo_box->priv->popup_fixed_width;
4945 * gtk_combo_box_get_popup_accessible:
4946 * @combo_box: a #GtkComboBox
4948 * Gets the accessible object corresponding to the combo box's popup.
4950 * This function is mostly intended for use by accessibility technologies;
4951 * applications should have little use for it.
4953 * Returns: (transfer none): the accessible object corresponding
4954 * to the combo box's popup.
4959 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
4963 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4965 if (combo_box->priv->popup_widget)
4967 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
4975 * gtk_combo_box_get_row_separator_func: (skip)
4976 * @combo_box: a #GtkComboBox
4978 * Returns the current row separator function.
4980 * Return value: the current row separator function.
4984 GtkTreeViewRowSeparatorFunc
4985 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
4987 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4989 return combo_box->priv->row_separator_func;
4993 * gtk_combo_box_set_row_separator_func:
4994 * @combo_box: a #GtkComboBox
4995 * @func: a #GtkTreeViewRowSeparatorFunc
4996 * @data: (allow-none): user data to pass to @func, or %NULL
4997 * @destroy: (allow-none): destroy notifier for @data, or %NULL
4999 * Sets the row separator function, which is used to determine
5000 * whether a row should be drawn as a separator. If the row separator
5001 * function is %NULL, no separators are drawn. This is the default value.
5006 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5007 GtkTreeViewRowSeparatorFunc func,
5009 GDestroyNotify destroy)
5011 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5013 if (combo_box->priv->row_separator_destroy)
5014 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5016 combo_box->priv->row_separator_func = func;
5017 combo_box->priv->row_separator_data = data;
5018 combo_box->priv->row_separator_destroy = destroy;
5020 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5021 if (combo_box->priv->tree_view)
5023 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5024 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5027 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5029 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5030 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5033 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5037 * gtk_combo_box_set_button_sensitivity:
5038 * @combo_box: a #GtkComboBox
5039 * @sensitivity: specify the sensitivity of the dropdown button
5041 * Sets whether the dropdown button of the combo box should be
5042 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5043 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5048 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5049 GtkSensitivityType sensitivity)
5051 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5053 if (combo_box->priv->button_sensitivity != sensitivity)
5055 combo_box->priv->button_sensitivity = sensitivity;
5056 gtk_combo_box_update_sensitivity (combo_box);
5058 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5063 * gtk_combo_box_get_button_sensitivity:
5064 * @combo_box: a #GtkComboBox
5066 * Returns whether the combo box sets the dropdown button
5067 * sensitive or not when there are no items in the model.
5069 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5070 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5071 * if the button is always insensitive or
5072 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5073 * the model has one item to be selected.
5078 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5080 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5082 return combo_box->priv->button_sensitivity;
5087 * gtk_combo_box_get_has_entry:
5088 * @combo_box: a #GtkComboBox
5090 * Returns whether the combo box has an entry.
5092 * Return Value: whether there is an entry in @combo_box.
5097 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5099 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5101 return combo_box->priv->has_entry;
5105 * gtk_combo_box_set_entry_text_column:
5106 * @combo_box: A #GtkComboBox
5107 * @text_column: A column in @model to get the strings from for
5108 * the internal entry
5110 * Sets the model column which @combo_box should use to get strings from
5111 * to be @text_column. The column @text_column in the model of @combo_box
5112 * must be of type %G_TYPE_STRING.
5114 * This is only relevant if @combo_box has been created with
5115 * #GtkComboBox:has-entry as %TRUE.
5120 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5123 GtkComboBoxPrivate *priv = combo_box->priv;
5124 GtkTreeModel *model;
5126 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5128 model = gtk_combo_box_get_model (combo_box);
5130 g_return_if_fail (text_column >= 0);
5131 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5133 priv->text_column = text_column;
5135 if (priv->text_renderer != NULL)
5136 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5137 priv->text_renderer,
5138 "text", text_column,
5143 * gtk_combo_box_get_entry_text_column:
5144 * @combo_box: A #GtkComboBox.
5146 * Returns the column which @combo_box is using to get the strings
5147 * from to display in the internal entry.
5149 * Return value: A column in the data source model of @combo_box.
5154 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5156 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5158 return combo_box->priv->text_column;
5162 * gtk_combo_box_set_focus_on_click:
5163 * @combo: a #GtkComboBox
5164 * @focus_on_click: whether the combo box grabs focus when clicked
5167 * Sets whether the combo box will grab focus when it is clicked with
5168 * the mouse. Making mouse clicks not grab focus is useful in places
5169 * like toolbars where you don't want the keyboard focus removed from
5170 * the main area of the application.
5175 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5176 gboolean focus_on_click)
5178 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5180 focus_on_click = focus_on_click != FALSE;
5182 if (combo_box->priv->focus_on_click != focus_on_click)
5184 combo_box->priv->focus_on_click = focus_on_click;
5186 if (combo_box->priv->button)
5187 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5190 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5195 * gtk_combo_box_get_focus_on_click:
5196 * @combo: a #GtkComboBox
5198 * Returns whether the combo box grabs focus when it is clicked
5199 * with the mouse. See gtk_combo_box_set_focus_on_click().
5201 * Return value: %TRUE if the combo box grabs focus when it is
5202 * clicked with the mouse.
5207 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5209 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5211 return combo_box->priv->focus_on_click;
5216 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5217 GtkBuilder *builder,
5219 const gchar *tagname,
5220 GMarkupParser *parser,
5223 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5224 tagname, parser, data))
5227 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5228 tagname, parser, data);
5232 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5233 GtkBuilder *builder,
5235 const gchar *tagname,
5238 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5239 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5243 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5244 GtkBuilder *builder,
5245 const gchar *childname)
5247 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5249 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5250 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5252 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5256 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5260 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5261 GtkComboBoxPrivate *priv = combo_box->priv;
5262 gint focus_width, focus_pad;
5263 gint font_size, arrow_size;
5264 PangoContext *context;
5265 PangoFontMetrics *metrics;
5266 PangoFontDescription *font_desc;
5268 gint minimum_width = 0, natural_width = 0;
5269 gint child_min, child_nat;
5270 GtkStyleContext *style_context;
5271 GtkStateFlags state;
5273 gfloat arrow_scaling;
5275 child = gtk_bin_get_child (GTK_BIN (widget));
5278 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5280 gtk_widget_style_get (GTK_WIDGET (widget),
5281 "focus-line-width", &focus_width,
5282 "focus-padding", &focus_pad,
5283 "arrow-size", &arrow_size,
5284 "arrow-scaling", &arrow_scaling,
5287 style_context = gtk_widget_get_style_context (widget);
5288 state = gtk_widget_get_state_flags (widget);
5290 get_widget_padding (widget, &border);
5291 gtk_style_context_get (style_context, state,
5295 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5296 metrics = pango_context_get_metrics (context, font_desc,
5297 pango_context_get_language (context));
5298 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5299 pango_font_metrics_get_descent (metrics));
5300 pango_font_metrics_unref (metrics);
5301 pango_font_description_free (font_desc);
5303 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5305 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5307 if (!priv->tree_view)
5310 if (priv->cell_view)
5312 gint sep_width, arrow_width;
5313 gint border_width, xpad;
5314 GtkBorder button_border;
5316 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5317 get_widget_padding (priv->button, &button_border);
5319 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5320 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5322 xpad = 2 * (border_width + focus_width + focus_pad) +
5323 button_border.left + button_border.right + border.left + border.right;
5325 minimum_width = child_min + sep_width + arrow_width + xpad;
5326 natural_width = child_nat + sep_width + arrow_width + xpad;
5330 gint but_width, but_nat_width;
5332 gtk_widget_get_preferred_width (priv->button,
5333 &but_width, &but_nat_width);
5335 minimum_width = child_min + but_width;
5336 natural_width = child_nat + but_nat_width;
5342 gint button_width, button_nat_width;
5344 /* sample + frame */
5345 minimum_width = child_min;
5346 natural_width = child_nat;
5348 minimum_width += 2 * focus_width;
5349 natural_width += 2 * focus_width;
5351 if (priv->cell_view_frame)
5353 if (priv->has_frame)
5355 gint border_width, xpad;
5356 GtkBorder frame_border;
5358 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5359 get_widget_padding (priv->cell_view_frame, &frame_border);
5360 xpad = (2 * border_width) + frame_border.left + frame_border.right;
5362 minimum_width += xpad;
5363 natural_width += xpad;
5368 gtk_widget_get_preferred_width (priv->button,
5369 &button_width, &button_nat_width);
5371 minimum_width += button_width;
5372 natural_width += button_nat_width;
5375 minimum_width += border.left + border.right;
5376 natural_width += border.left + border.right;
5379 *minimum_size = minimum_width;
5382 *natural_size = natural_width;
5386 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5392 /* Combo box is height-for-width only
5393 * (so we always just reserve enough height for the minimum width) */
5394 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5395 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5399 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5404 /* Combo box is height-for-width only
5405 * (so we assume we always reserved enough height for the minimum width) */
5406 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5411 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5416 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5417 GtkComboBoxPrivate *priv = combo_box->priv;
5418 gint focus_width, focus_pad;
5419 gint min_height = 0, nat_height = 0;
5424 gtk_widget_style_get (GTK_WIDGET (widget),
5425 "focus-line-width", &focus_width,
5426 "focus-padding", &focus_pad,
5429 child = gtk_bin_get_child (GTK_BIN (widget));
5431 get_widget_padding (widget, &border);
5432 size = avail_size - border.left;
5434 if (!priv->tree_view)
5437 if (priv->cell_view)
5439 /* calculate x/y padding and separator/arrow size */
5440 gint sep_width, arrow_width, sep_height, arrow_height;
5441 gint border_width, xpad, ypad;
5442 GtkBorder button_border;
5444 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5445 get_widget_padding (priv->button, &button_border);
5447 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5448 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5449 gtk_widget_get_preferred_height_for_width (priv->separator,
5450 sep_width, &sep_height, NULL);
5451 gtk_widget_get_preferred_height_for_width (priv->arrow,
5452 arrow_width, &arrow_height, NULL);
5454 xpad = 2 * (border_width + focus_width + focus_pad) +
5455 button_border.left + button_border.right;
5456 ypad = 2 * (border_width + focus_width + focus_pad) +
5457 button_border.top + button_border.bottom;
5459 size -= sep_width + arrow_width + xpad;
5461 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5462 * and fitting the whole treemodel */
5463 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5465 arrow_height = MAX (arrow_height, sep_height);
5466 min_height = MAX (min_height, arrow_height);
5467 nat_height = MAX (nat_height, arrow_height);
5474 /* there is a custom child widget inside (no priv->cell_view) */
5475 gint but_width, but_height;
5477 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5478 gtk_widget_get_preferred_height_for_width (priv->button,
5479 but_width, &but_height, NULL);
5483 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5484 * and fitting the whole treemodel */
5485 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5487 min_height = MAX (min_height, but_height);
5488 nat_height = MAX (nat_height, but_height);
5494 gint but_width, but_height;
5495 gint xpad = 0, ypad = 0;
5497 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5498 gtk_widget_get_preferred_height_for_width (priv->button,
5499 but_width, &but_height, NULL);
5501 if (priv->cell_view_frame && priv->has_frame)
5503 GtkBorder frame_border;
5506 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5507 get_widget_padding (GTK_WIDGET (priv->cell_view_frame), &frame_border);
5509 xpad = (2 * border_width) + border.left + frame_border.right;
5510 ypad = (2 * border_width) + border.top + frame_border.bottom;
5514 size -= 2 * focus_width;
5517 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5518 * and fitting the whole treemodel */
5519 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5521 min_height = MAX (min_height, but_height);
5522 nat_height = MAX (nat_height, but_height);
5528 min_height += border.top + border.bottom;
5529 nat_height += border.top + border.bottom;
5532 *minimum_size = min_height;
5535 *natural_size = nat_height;
5539 * gtk_combo_box_set_id_column:
5540 * @combo_box: A #GtkComboBox
5541 * @id_column: A column in @model to get string IDs for values from
5543 * Sets the model column which @combo_box should use to get string IDs
5544 * for values from. The column @id_column in the model of @combo_box
5545 * must be of type %G_TYPE_STRING.
5550 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5553 GtkComboBoxPrivate *priv = combo_box->priv;
5554 GtkTreeModel *model;
5556 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5558 if (id_column != priv->id_column)
5560 model = gtk_combo_box_get_model (combo_box);
5562 g_return_if_fail (id_column >= 0);
5563 g_return_if_fail (model == NULL ||
5564 id_column < gtk_tree_model_get_n_columns (model));
5566 priv->id_column = id_column;
5568 g_object_notify (G_OBJECT (combo_box), "id-column");
5569 g_object_notify (G_OBJECT (combo_box), "active-id");
5574 * gtk_combo_box_get_id_column:
5575 * @combo_box: A #GtkComboBox
5577 * Returns the column which @combo_box is using to get string IDs
5580 * Return value: A column in the data source model of @combo_box.
5585 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5587 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5589 return combo_box->priv->id_column;
5593 * gtk_combo_box_get_active_id:
5594 * @combo_box: a #GtkComboBox
5596 * Returns the ID of the active row of @combo_box. This value is taken
5597 * from the active row and the column specified by the 'id-column'
5598 * property of @combo_box (see gtk_combo_box_set_id_column()).
5600 * The returned value is an interned string which means that you can
5601 * compare the pointer by value to other interned strings and that you
5604 * If the 'id-column' property of @combo_box is not set or if no row is
5605 * selected then %NULL is returned.
5607 * Return value: the ID of the active row, or %NULL
5612 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5614 GtkTreeModel *model;
5618 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5620 column = combo_box->priv->id_column;
5625 model = gtk_combo_box_get_model (combo_box);
5626 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5627 G_TYPE_STRING, NULL);
5629 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5631 const gchar *interned;
5634 gtk_tree_model_get (model, &iter, column, &id, -1);
5635 interned = g_intern_string (id);
5645 * gtk_combo_box_set_active_id:
5646 * @combo_box: a #GtkComboBox
5647 * @active_id: the ID of the row to select
5649 * Changes the active row of @combo_box to the one that has an ID equal to @id.
5651 * If the 'id-column' property of @combo_box is unset or if no row has
5652 * the given ID then nothing happens.
5657 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5658 const gchar *active_id)
5660 GtkTreeModel *model;
5664 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5666 column = combo_box->priv->id_column;
5671 model = gtk_combo_box_get_model (combo_box);
5672 g_return_if_fail (gtk_tree_model_get_column_type (model, column) ==
5675 if (gtk_tree_model_get_iter_first (model, &iter))
5680 gtk_tree_model_get (model, &iter, column, &id, -1);
5681 match = strcmp (id, active_id) == 0;
5686 gtk_combo_box_set_active_iter (combo_box, &iter);
5689 } while (gtk_tree_model_iter_next (model, &iter));