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, see <http://www.gnu.org/licenses/>.
20 #include "gtkcombobox.h"
22 #include "gtkadjustment.h"
23 #include "gtkcellareabox.h"
24 #include "gtktreemenu.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcellview.h"
30 #include "gtkeventbox.h"
33 #include "gtkliststore.h"
35 #include "gtkmenuprivate.h"
36 #include "gtkmenushellprivate.h"
37 #include "gtkscrolledwindow.h"
38 #include "gtkseparatormenuitem.h"
39 #include "deprecated/gtktearoffmenuitem.h"
40 #include "gtktogglebutton.h"
41 #include "gtktreeselection.h"
42 #include "gtkseparator.h"
43 #include "gtkwidgetpath.h"
44 #include "gtkwidgetprivate.h"
45 #include "gtkwindow.h"
46 #include "gtktypebuiltins.h"
47 #include "gtkprivate.h"
49 #include <gobject/gvaluecollector.h>
54 #include "gtkmarshalers.h"
57 #include "gtkentryprivate.h"
58 #include "gtktreeprivate.h"
59 #include "a11y/gtkcomboboxaccessible.h"
64 * @Short_description: A widget used to choose from a list of items
66 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
68 * A GtkComboBox is a widget that allows the user to choose from a list of
69 * valid choices. The GtkComboBox displays the selected choice. When
70 * activated, the GtkComboBox displays a popup which allows the user to
71 * make a new choice. The style in which the selected value is displayed,
72 * and the style of the popup is determined by the current theme. It may
73 * be similar to a Windows-style combo box.
75 * The GtkComboBox uses the model-view pattern; the list of valid choices
76 * is specified in the form of a tree model, and the display of the choices
77 * can be adapted to the data in the model by using cell renderers, as you
78 * would in a tree view. This is possible since GtkComboBox implements the
79 * #GtkCellLayout interface. The tree model holding the valid choices is
80 * not restricted to a flat list, it can be a real tree, and the popup will
81 * reflect the tree structure.
83 * To allow the user to enter values not in the model, the 'has-entry'
84 * property allows the GtkComboBox to contain a #GtkEntry. This entry
85 * can be accessed by calling gtk_bin_get_child() on the combo box.
87 * For a simple list of textual choices, the model-view API of GtkComboBox
88 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
89 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
94 /* WELCOME, to THE house of evil code */
95 struct _GtkComboBoxPrivate
105 GtkShadowType shadow_type;
107 gint active; /* Only temporary */
108 GtkTreeRowReference *active_row;
110 GtkWidget *tree_view;
112 GtkWidget *cell_view;
113 GtkWidget *cell_view_frame;
118 GtkWidget *separator;
120 GtkWidget *popup_widget;
121 GtkWidget *popup_window;
122 GtkWidget *scrolled_window;
129 guint activate_button;
130 guint32 activate_time;
132 guint resize_idle_id;
134 /* For "has-entry" specific behavior we track
135 * an automated cell renderer and text column
138 GtkCellRenderer *text_renderer;
142 guint popup_in_progress : 1;
143 guint popup_shown : 1;
144 guint add_tearoffs : 1;
146 guint is_cell_renderer : 1;
147 guint editing_canceled : 1;
148 guint auto_scroll : 1;
149 guint focus_on_click : 1;
150 guint button_sensitivity : 2;
152 guint popup_fixed_width : 1;
154 GtkTreeViewRowSeparatorFunc row_separator_func;
155 gpointer row_separator_data;
156 GDestroyNotify row_separator_destroy;
158 GdkDevice *grab_pointer;
159 GdkDevice *grab_keyboard;
161 gchar *tearoff_title;
164 /* While debugging this evil code, I have learned that
165 * there are actually 4 modes to this widget, which can
166 * be characterized as follows
168 * 1) menu mode, no child added
171 * cell_view -> GtkCellView, regular child
172 * cell_view_frame -> NULL
173 * button -> GtkToggleButton set_parent to combo
174 * arrow -> GtkArrow set_parent to button
175 * separator -> GtkVSepator set_parent to button
176 * popup_widget -> GtkMenu
177 * popup_window -> NULL
178 * scrolled_window -> NULL
180 * 2) menu mode, child added
184 * cell_view_frame -> NULL
185 * button -> GtkToggleButton set_parent to combo
186 * arrow -> GtkArrow, child of button
188 * popup_widget -> GtkMenu
189 * popup_window -> NULL
190 * scrolled_window -> NULL
192 * 3) list mode, no child added
194 * tree_view -> GtkTreeView, child of scrolled_window
195 * cell_view -> GtkCellView, regular child
196 * cell_view_frame -> GtkFrame, set parent to combo
197 * button -> GtkToggleButton, set_parent to combo
198 * arrow -> GtkArrow, child of button
200 * popup_widget -> tree_view
201 * popup_window -> GtkWindow
202 * scrolled_window -> GtkScrolledWindow, child of popup_window
204 * 4) list mode, child added
206 * tree_view -> GtkTreeView, child of scrolled_window
208 * cell_view_frame -> NULL
209 * button -> GtkToggleButton, set_parent to combo
210 * arrow -> GtkArrow, child of button
212 * popup_widget -> tree_view
213 * popup_window -> GtkWindow
214 * scrolled_window -> GtkScrolledWindow, child of popup_window
231 PROP_ROW_SPAN_COLUMN,
232 PROP_COLUMN_SPAN_COLUMN,
239 PROP_BUTTON_SENSITIVITY,
240 PROP_EDITING_CANCELED,
242 PROP_ENTRY_TEXT_COLUMN,
243 PROP_POPUP_FIXED_WIDTH,
249 static guint combo_box_signals[LAST_SIGNAL] = {0,};
251 #define SCROLL_TIME 100
255 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
256 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
257 static GObject *gtk_combo_box_constructor (GType type,
258 guint n_construct_properties,
259 GObjectConstructParam *construct_properties);
260 static void gtk_combo_box_dispose (GObject *object);
261 static void gtk_combo_box_finalize (GObject *object);
262 static void gtk_combo_box_destroy (GtkWidget *widget);
264 static void gtk_combo_box_set_property (GObject *object,
268 static void gtk_combo_box_get_property (GObject *object,
273 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
274 GtkStateFlags previous);
275 static void gtk_combo_box_grab_focus (GtkWidget *widget);
276 static void gtk_combo_box_style_updated (GtkWidget *widget);
277 static void gtk_combo_box_button_toggled (GtkWidget *widget,
279 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
280 GtkStateFlags previous,
282 static void gtk_combo_box_add (GtkContainer *container,
284 static void gtk_combo_box_remove (GtkContainer *container,
287 static void gtk_combo_box_menu_show (GtkWidget *menu,
289 static void gtk_combo_box_menu_hide (GtkWidget *menu,
292 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
294 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
299 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
304 static void gtk_combo_box_menu_position (GtkMenu *menu,
310 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
312 static void gtk_combo_box_size_allocate (GtkWidget *widget,
313 GtkAllocation *allocation);
314 static void gtk_combo_box_forall (GtkContainer *container,
315 gboolean include_internals,
316 GtkCallback callback,
317 gpointer callback_data);
318 static gboolean gtk_combo_box_draw (GtkWidget *widget,
320 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
321 GdkEventScroll *event);
322 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
325 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
326 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
327 GtkScrollType scroll);
328 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
329 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
331 /* listening to the model */
332 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
336 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
339 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
344 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
348 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
354 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
359 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
360 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
362 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
363 GdkEventButton *event,
365 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
368 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
369 GdkEventCrossing *event,
371 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
374 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
375 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
376 GdkEventButton *event,
379 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
382 gboolean path_currently_selected,
385 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
389 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
392 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
393 gboolean add_children);
394 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
395 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
398 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
399 GdkEventButton *event,
401 static void gtk_combo_box_menu_activate (GtkWidget *menu,
403 GtkComboBox *combo_box);
404 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
405 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
408 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
410 guint32 activate_time);
413 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
415 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
416 gboolean group_cycling);
418 static void gtk_combo_box_child_show (GtkWidget *widget,
419 GtkComboBox *combo_box);
420 static void gtk_combo_box_child_hide (GtkWidget *widget,
421 GtkComboBox *combo_box);
423 /* GtkComboBox:has-entry callbacks */
424 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
426 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
428 static gchar *gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
431 /* GtkBuildable method implementation */
432 static GtkBuildableIface *parent_buildable_iface;
434 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
435 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
438 const gchar *tagname,
439 GMarkupParser *parser,
441 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
444 const gchar *tagname,
446 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
448 const gchar *childname);
451 /* GtkCellEditable method implementations */
452 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
455 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
458 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
461 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
465 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
469 static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container,
471 static void gtk_combo_box_direction_changed (GtkWidget *widget,
472 GtkTextDirection previous_direction);
474 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
475 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
476 gtk_combo_box_cell_layout_init)
477 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
478 gtk_combo_box_cell_editable_init)
479 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
480 gtk_combo_box_buildable_init))
485 gtk_combo_box_class_init (GtkComboBoxClass *klass)
487 GObjectClass *object_class;
488 GtkContainerClass *container_class;
489 GtkWidgetClass *widget_class;
490 GtkBindingSet *binding_set;
492 container_class = (GtkContainerClass *)klass;
493 container_class->forall = gtk_combo_box_forall;
494 container_class->add = gtk_combo_box_add;
495 container_class->remove = gtk_combo_box_remove;
496 container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
498 gtk_container_class_handle_border_width (container_class);
500 widget_class = (GtkWidgetClass *)klass;
501 widget_class->size_allocate = gtk_combo_box_size_allocate;
502 widget_class->draw = gtk_combo_box_draw;
503 widget_class->scroll_event = gtk_combo_box_scroll_event;
504 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
505 widget_class->grab_focus = gtk_combo_box_grab_focus;
506 widget_class->style_updated = gtk_combo_box_style_updated;
507 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
508 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
509 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
510 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
511 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
512 widget_class->destroy = gtk_combo_box_destroy;
513 widget_class->direction_changed = gtk_combo_box_direction_changed;
515 object_class = (GObjectClass *)klass;
516 object_class->constructor = gtk_combo_box_constructor;
517 object_class->dispose = gtk_combo_box_dispose;
518 object_class->finalize = gtk_combo_box_finalize;
519 object_class->set_property = gtk_combo_box_set_property;
520 object_class->get_property = gtk_combo_box_get_property;
522 klass->format_entry_text = gtk_combo_box_format_entry_text;
526 * GtkComboBox::changed:
527 * @widget: the object which received the signal
529 * The changed signal is emitted when the active
530 * item is changed. The can be due to the user selecting
531 * a different item from the list, or due to a
532 * call to gtk_combo_box_set_active_iter().
533 * It will also be emitted while typing into the entry of a combo box
538 combo_box_signals[CHANGED] =
539 g_signal_new (I_("changed"),
540 G_OBJECT_CLASS_TYPE (klass),
542 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
544 g_cclosure_marshal_VOID__VOID,
547 * GtkComboBox::move-active:
548 * @widget: the object that received the signal
549 * @scroll_type: a #GtkScrollType
551 * The ::move-active signal is a
552 * <link linkend="keybinding-signals">keybinding signal</link>
553 * which gets emitted to move the active selection.
557 combo_box_signals[MOVE_ACTIVE] =
558 g_signal_new_class_handler (I_("move-active"),
559 G_OBJECT_CLASS_TYPE (klass),
560 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
561 G_CALLBACK (gtk_combo_box_real_move_active),
563 g_cclosure_marshal_VOID__ENUM,
565 GTK_TYPE_SCROLL_TYPE);
568 * GtkComboBox::popup:
569 * @widget: the object that received the signal
571 * The ::popup signal is a
572 * <link linkend="keybinding-signals">keybinding signal</link>
573 * which gets emitted to popup the combo box list.
575 * The default binding for this signal is Alt+Down.
579 combo_box_signals[POPUP] =
580 g_signal_new_class_handler (I_("popup"),
581 G_OBJECT_CLASS_TYPE (klass),
582 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
583 G_CALLBACK (gtk_combo_box_real_popup),
585 g_cclosure_marshal_VOID__VOID,
588 * GtkComboBox::popdown:
589 * @button: the object which received the signal
591 * The ::popdown signal is a
592 * <link linkend="keybinding-signals">keybinding signal</link>
593 * which gets emitted to popdown the combo box list.
595 * The default bindings for this signal are Alt+Up and Escape.
599 combo_box_signals[POPDOWN] =
600 g_signal_new_class_handler (I_("popdown"),
601 G_OBJECT_CLASS_TYPE (klass),
602 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
603 G_CALLBACK (gtk_combo_box_real_popdown),
605 _gtk_marshal_BOOLEAN__VOID,
609 * GtkComboBox::format-entry-text:
610 * @combo: the object which received the signal
611 * @path: the GtkTreePath string from the combo box's current model to format text for
613 * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
615 * A signal which allows you to change how the text displayed in a combo box's
616 * entry is displayed.
618 * Connect a signal handler which returns an allocated string representing
619 * @path. That string will then be used to set the text in the combo box's entry.
620 * The default signal handler uses the text from the GtkComboBox::entry-text-column
623 * Here's an example signal handler which fetches data from the model and
624 * displays it in the entry.
627 * format_entry_text_callback (GtkComboBox *combo,
629 * gpointer user_data)
632 * GtkTreeModel model;
635 * model = gtk_combo_box_get_model (combo);
637 * gtk_tree_model_get_iter_from_string (model, &iter, path);
638 * gtk_tree_model_get (model, &iter,
639 * THE_DOUBLE_VALUE_COLUMN, &value,
642 * return g_strdup_printf ("%g", value);
646 * Return value: (transfer full): a newly allocated string representing @path
647 * for the current GtkComboBox model.
651 combo_box_signals[FORMAT_ENTRY_TEXT] =
652 g_signal_new (I_("format-entry-text"),
653 G_TYPE_FROM_CLASS (klass),
655 G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text),
656 _gtk_single_string_accumulator, NULL,
657 _gtk_marshal_STRING__STRING,
658 G_TYPE_STRING, 1, G_TYPE_STRING);
661 binding_set = gtk_binding_set_by_class (widget_class);
663 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
665 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
668 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
670 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
672 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
675 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
677 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
678 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
680 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
681 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
683 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
684 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
686 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
687 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
689 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
690 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
692 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
694 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
696 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
697 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
699 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
700 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
702 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
703 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
705 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
706 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
708 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
709 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
711 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
714 g_object_class_override_property (object_class,
715 PROP_EDITING_CANCELED,
721 * The model from which the combo box takes the values shown
726 g_object_class_install_property (object_class,
728 g_param_spec_object ("model",
729 P_("ComboBox model"),
730 P_("The model for the combo box"),
732 GTK_PARAM_READWRITE));
735 * GtkComboBox:wrap-width:
737 * If wrap-width is set to a positive value, the list will be
738 * displayed in multiple columns, the number of columns is
739 * determined by wrap-width.
743 g_object_class_install_property (object_class,
745 g_param_spec_int ("wrap-width",
747 P_("Wrap width for laying out the items in a grid"),
751 GTK_PARAM_READWRITE));
755 * GtkComboBox:row-span-column:
757 * If this is set to a non-negative value, it must be the index of a column
758 * of type %G_TYPE_INT in the model.
760 * The values of that column are used to determine how many rows a value in
761 * the list will span. Therefore, the values in the model column pointed to
762 * by this property must be greater than zero and not larger than wrap-width.
766 g_object_class_install_property (object_class,
767 PROP_ROW_SPAN_COLUMN,
768 g_param_spec_int ("row-span-column",
769 P_("Row span column"),
770 P_("TreeModel column containing the row span values"),
774 GTK_PARAM_READWRITE));
778 * GtkComboBox:column-span-column:
780 * If this is set to a non-negative value, it must be the index of a column
781 * of type %G_TYPE_INT in the model.
783 * The values of that column are used to determine how many columns a value
784 * in the list will span.
788 g_object_class_install_property (object_class,
789 PROP_COLUMN_SPAN_COLUMN,
790 g_param_spec_int ("column-span-column",
791 P_("Column span column"),
792 P_("TreeModel column containing the column span values"),
796 GTK_PARAM_READWRITE));
800 * GtkComboBox:active:
802 * The item which is currently active. If the model is a non-flat treemodel,
803 * and the active item is not an immediate child of the root of the tree,
804 * this property has the value
805 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
806 * where <literal>path</literal> is the #GtkTreePath of the active item.
810 g_object_class_install_property (object_class,
812 g_param_spec_int ("active",
814 P_("The item which is currently active"),
818 GTK_PARAM_READWRITE));
821 * GtkComboBox:add-tearoffs:
823 * The add-tearoffs property controls whether generated menus
824 * have tearoff menu items.
826 * Note that this only affects menu style combo boxes.
830 g_object_class_install_property (object_class,
832 g_param_spec_boolean ("add-tearoffs",
833 P_("Add tearoffs to menus"),
834 P_("Whether dropdowns should have a tearoff menu item"),
836 GTK_PARAM_READWRITE));
839 * GtkComboBox:has-frame:
841 * The has-frame property controls whether a frame
842 * is drawn around the entry.
846 g_object_class_install_property (object_class,
848 g_param_spec_boolean ("has-frame",
850 P_("Whether the combo box draws a frame around the child"),
852 GTK_PARAM_READWRITE));
854 g_object_class_install_property (object_class,
856 g_param_spec_boolean ("focus-on-click",
857 P_("Focus on click"),
858 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
860 GTK_PARAM_READWRITE));
863 * GtkComboBox:tearoff-title:
865 * A title that may be displayed by the window manager
866 * when the popup is torn-off.
870 g_object_class_install_property (object_class,
872 g_param_spec_string ("tearoff-title",
874 P_("A title that may be displayed by the window manager when the popup is torn-off"),
876 GTK_PARAM_READWRITE));
880 * GtkComboBox:popup-shown:
882 * Whether the combo boxes dropdown is popped up.
883 * Note that this property is mainly useful, because
884 * it allows you to connect to notify::popup-shown.
888 g_object_class_install_property (object_class,
890 g_param_spec_boolean ("popup-shown",
892 P_("Whether the combo's dropdown is shown"),
894 GTK_PARAM_READABLE));
898 * GtkComboBox:button-sensitivity:
900 * Whether the dropdown button is sensitive when
901 * the model is empty.
905 g_object_class_install_property (object_class,
906 PROP_BUTTON_SENSITIVITY,
907 g_param_spec_enum ("button-sensitivity",
908 P_("Button Sensitivity"),
909 P_("Whether the dropdown button is sensitive when the model is empty"),
910 GTK_TYPE_SENSITIVITY_TYPE,
911 GTK_SENSITIVITY_AUTO,
912 GTK_PARAM_READWRITE));
915 * GtkComboBox:has-entry:
917 * Whether the combo box has an entry.
921 g_object_class_install_property (object_class,
923 g_param_spec_boolean ("has-entry",
925 P_("Whether combo box has an entry"),
927 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
930 * GtkComboBox:entry-text-column:
932 * The column in the combo box's model to associate with strings from the entry
933 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
937 g_object_class_install_property (object_class,
938 PROP_ENTRY_TEXT_COLUMN,
939 g_param_spec_int ("entry-text-column",
940 P_("Entry Text Column"),
941 P_("The column in the combo box's model to associate "
942 "with strings from the entry if the combo was "
943 "created with #GtkComboBox:has-entry = %TRUE"),
945 GTK_PARAM_READWRITE));
948 * GtkComboBox:id-column:
950 * The column in the combo box's model that provides string
951 * IDs for the values in the model, if != -1.
955 g_object_class_install_property (object_class,
957 g_param_spec_int ("id-column",
959 P_("The column in the combo box's model that provides "
960 "string IDs for the values in the model"),
962 GTK_PARAM_READWRITE));
965 * GtkComboBox:active-id:
967 * The value of the ID column of the active row.
971 g_object_class_install_property (object_class,
973 g_param_spec_string ("active-id",
975 P_("The value of the id column "
976 "for the active row"),
977 NULL, GTK_PARAM_READWRITE));
980 * GtkComboBox:popup-fixed-width:
982 * Whether the popup's width should be a fixed width matching the
983 * allocated width of the combo box.
987 g_object_class_install_property (object_class,
988 PROP_POPUP_FIXED_WIDTH,
989 g_param_spec_boolean ("popup-fixed-width",
990 P_("Popup Fixed Width"),
991 P_("Whether the popup's width should be a "
992 "fixed width matching the allocated width "
995 GTK_PARAM_READWRITE));
998 * GtkComboBox:cell-area:
1000 * The #GtkCellArea used to layout cell renderers for this combo box.
1002 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
1003 * a horizontally oriented #GtkCellAreaBox will be used.
1007 g_object_class_install_property (object_class,
1009 g_param_spec_object ("cell-area",
1011 P_("The GtkCellArea used to layout cells"),
1013 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1015 gtk_widget_class_install_style_property (widget_class,
1016 g_param_spec_boolean ("appears-as-list",
1017 P_("Appears as list"),
1018 P_("Whether dropdowns should look like lists rather than menus"),
1020 GTK_PARAM_READABLE));
1023 * GtkComboBox:arrow-size:
1025 * Sets the minimum size of the arrow in the combo box. Note
1026 * that the arrow size is coupled to the font size, so in case
1027 * a larger font is used, the arrow will be larger than set
1032 gtk_widget_class_install_style_property (widget_class,
1033 g_param_spec_int ("arrow-size",
1035 P_("The minimum size of the arrow in the combo box"),
1039 GTK_PARAM_READABLE));
1042 * GtkComboBox:arrow-scaling:
1044 * Sets the amount of space used up by the combobox arrow,
1045 * proportional to the font size.
1049 gtk_widget_class_install_style_property (widget_class,
1050 g_param_spec_float ("arrow-scaling",
1051 P_("Arrow Scaling"),
1052 P_("The amount of space used by the arrow"),
1056 GTK_PARAM_READABLE));
1059 * GtkComboBox:shadow-type:
1061 * Which kind of shadow to draw around the combo box.
1065 gtk_widget_class_install_style_property (widget_class,
1066 g_param_spec_enum ("shadow-type",
1068 P_("Which kind of shadow to draw around the combo box"),
1069 GTK_TYPE_SHADOW_TYPE,
1071 GTK_PARAM_READABLE));
1073 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1075 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1079 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1081 parent_buildable_iface = g_type_interface_peek_parent (iface);
1082 iface->add_child = _gtk_cell_layout_buildable_add_child;
1083 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1084 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1085 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1089 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1091 iface->get_area = gtk_combo_box_cell_layout_get_area;
1095 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1097 iface->start_editing = gtk_combo_box_start_editing;
1101 gtk_combo_box_init (GtkComboBox *combo_box)
1103 GtkComboBoxPrivate *priv;
1105 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1107 GtkComboBoxPrivate);
1108 priv = combo_box->priv;
1110 priv->wrap_width = 0;
1113 priv->active_row = NULL;
1114 priv->col_column = -1;
1115 priv->row_column = -1;
1117 priv->popup_shown = FALSE;
1118 priv->add_tearoffs = FALSE;
1119 priv->has_frame = TRUE;
1120 priv->is_cell_renderer = FALSE;
1121 priv->editing_canceled = FALSE;
1122 priv->auto_scroll = FALSE;
1123 priv->focus_on_click = TRUE;
1124 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1125 priv->has_entry = FALSE;
1126 priv->popup_fixed_width = TRUE;
1128 priv->text_column = -1;
1129 priv->text_renderer = NULL;
1130 priv->id_column = -1;
1134 gtk_combo_box_set_property (GObject *object,
1136 const GValue *value,
1139 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1140 GtkComboBoxPrivate *priv = combo_box->priv;
1146 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1149 case PROP_WRAP_WIDTH:
1150 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1153 case PROP_ROW_SPAN_COLUMN:
1154 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1157 case PROP_COLUMN_SPAN_COLUMN:
1158 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1162 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1165 case PROP_ADD_TEAROFFS:
1166 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1169 case PROP_HAS_FRAME:
1170 priv->has_frame = g_value_get_boolean (value);
1172 if (priv->has_entry)
1176 child = gtk_bin_get_child (GTK_BIN (combo_box));
1178 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1183 case PROP_FOCUS_ON_CLICK:
1184 gtk_combo_box_set_focus_on_click (combo_box,
1185 g_value_get_boolean (value));
1188 case PROP_TEAROFF_TITLE:
1189 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1192 case PROP_POPUP_SHOWN:
1193 if (g_value_get_boolean (value))
1194 gtk_combo_box_popup (combo_box);
1196 gtk_combo_box_popdown (combo_box);
1199 case PROP_BUTTON_SENSITIVITY:
1200 gtk_combo_box_set_button_sensitivity (combo_box,
1201 g_value_get_enum (value));
1204 case PROP_POPUP_FIXED_WIDTH:
1205 gtk_combo_box_set_popup_fixed_width (combo_box,
1206 g_value_get_boolean (value));
1209 case PROP_EDITING_CANCELED:
1210 priv->editing_canceled = g_value_get_boolean (value);
1213 case PROP_HAS_ENTRY:
1214 priv->has_entry = g_value_get_boolean (value);
1217 case PROP_ENTRY_TEXT_COLUMN:
1218 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1221 case PROP_ID_COLUMN:
1222 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1225 case PROP_ACTIVE_ID:
1226 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1229 case PROP_CELL_AREA:
1230 /* Construct-only, can only be assigned once */
1231 area = g_value_get_object (value);
1234 if (priv->area != NULL)
1236 g_warning ("cell-area has already been set, ignoring construct property");
1237 g_object_ref_sink (area);
1238 g_object_unref (area);
1241 priv->area = g_object_ref_sink (area);
1246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1252 gtk_combo_box_get_property (GObject *object,
1257 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1258 GtkComboBoxPrivate *priv = combo_box->priv;
1263 g_value_set_object (value, combo_box->priv->model);
1266 case PROP_WRAP_WIDTH:
1267 g_value_set_int (value, combo_box->priv->wrap_width);
1270 case PROP_ROW_SPAN_COLUMN:
1271 g_value_set_int (value, combo_box->priv->row_column);
1274 case PROP_COLUMN_SPAN_COLUMN:
1275 g_value_set_int (value, combo_box->priv->col_column);
1279 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1282 case PROP_ADD_TEAROFFS:
1283 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1286 case PROP_HAS_FRAME:
1287 g_value_set_boolean (value, combo_box->priv->has_frame);
1290 case PROP_FOCUS_ON_CLICK:
1291 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1294 case PROP_TEAROFF_TITLE:
1295 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1298 case PROP_POPUP_SHOWN:
1299 g_value_set_boolean (value, combo_box->priv->popup_shown);
1302 case PROP_BUTTON_SENSITIVITY:
1303 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1306 case PROP_POPUP_FIXED_WIDTH:
1307 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1310 case PROP_EDITING_CANCELED:
1311 g_value_set_boolean (value, priv->editing_canceled);
1314 case PROP_HAS_ENTRY:
1315 g_value_set_boolean (value, priv->has_entry);
1318 case PROP_ENTRY_TEXT_COLUMN:
1319 g_value_set_int (value, priv->text_column);
1322 case PROP_ID_COLUMN:
1323 g_value_set_int (value, priv->id_column);
1326 case PROP_ACTIVE_ID:
1327 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1330 case PROP_CELL_AREA:
1331 g_value_set_object (value, priv->area);
1335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1341 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1342 GtkStateFlags previous)
1344 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1345 GtkComboBoxPrivate *priv = combo_box->priv;
1347 if (gtk_widget_get_realized (widget))
1349 if (priv->tree_view && priv->cell_view)
1351 GtkStyleContext *context;
1352 GtkStateFlags state;
1355 context = gtk_widget_get_style_context (widget);
1356 state = gtk_widget_get_state_flags (widget);
1357 gtk_style_context_get_background_color (context, state, &color);
1359 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1363 gtk_widget_queue_draw (widget);
1367 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1368 GtkStateFlags previous,
1371 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1372 GtkComboBoxPrivate *priv = combo_box->priv;
1374 if (gtk_widget_get_realized (widget))
1376 if (!priv->tree_view && priv->cell_view)
1377 gtk_widget_set_state_flags (priv->cell_view,
1378 gtk_widget_get_state_flags (widget),
1382 gtk_widget_queue_draw (widget);
1386 gtk_combo_box_invalidate_order_foreach (GtkWidget *widget)
1388 _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
1392 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1394 gtk_container_forall (GTK_CONTAINER (combo_box),
1395 (GtkCallback) gtk_combo_box_invalidate_order_foreach,
1400 gtk_combo_box_direction_changed (GtkWidget *widget,
1401 GtkTextDirection previous_direction)
1403 gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1406 static GtkWidgetPath *
1407 gtk_combo_box_get_path_for_child (GtkContainer *container,
1410 GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1411 GtkWidgetPath *path;
1413 gboolean found = FALSE;
1414 GList *visible_children, *l;
1415 GtkWidgetPath *sibling_path;
1418 path = _gtk_widget_create_path (GTK_WIDGET (container));
1420 if (gtk_widget_get_visible (child))
1422 visible_children = NULL;
1424 if (priv->button && gtk_widget_get_visible (priv->button))
1425 visible_children = g_list_prepend (visible_children, priv->button);
1427 if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1428 visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1430 widget = gtk_bin_get_child (GTK_BIN (container));
1431 if (widget && gtk_widget_get_visible (widget))
1432 visible_children = g_list_prepend (visible_children, widget);
1434 if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1435 visible_children = g_list_reverse (visible_children);
1439 for (l = visible_children; l; l = l->next)
1443 if (widget == child)
1455 sibling_path = gtk_widget_path_new ();
1457 for (l = visible_children; l; l = l->next)
1458 gtk_widget_path_append_for_widget (sibling_path, l->data);
1460 gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1462 g_list_free (visible_children);
1463 gtk_widget_path_unref (sibling_path);
1467 gtk_widget_path_append_for_widget (path, child);
1474 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1476 GtkComboBoxPrivate *priv = combo_box->priv;
1477 gboolean appears_as_list;
1479 /* if wrap_width > 0, then we are in grid-mode and forced to use
1482 if (priv->wrap_width)
1483 appears_as_list = FALSE;
1485 gtk_widget_style_get (GTK_WIDGET (combo_box),
1486 "appears-as-list", &appears_as_list,
1489 if (appears_as_list)
1491 /* Destroy all the menu mode widgets, if they exist. */
1492 if (GTK_IS_MENU (priv->popup_widget))
1493 gtk_combo_box_menu_destroy (combo_box);
1495 /* Create the list mode widgets, if they don't already exist. */
1496 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1497 gtk_combo_box_list_setup (combo_box);
1501 /* Destroy all the list mode widgets, if they exist. */
1502 if (GTK_IS_TREE_VIEW (priv->tree_view))
1503 gtk_combo_box_list_destroy (combo_box);
1505 /* Create the menu mode widgets, if they don't already exist. */
1506 if (!GTK_IS_MENU (priv->popup_widget))
1507 gtk_combo_box_menu_setup (combo_box, TRUE);
1510 gtk_widget_style_get (GTK_WIDGET (combo_box),
1511 "shadow-type", &priv->shadow_type,
1516 gtk_combo_box_style_updated (GtkWidget *widget)
1518 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1519 GtkComboBoxPrivate *priv = combo_box->priv;
1522 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1524 gtk_combo_box_check_appearance (combo_box);
1526 if (priv->tree_view && priv->cell_view)
1528 GtkStyleContext *context;
1529 GtkStateFlags state;
1532 context = gtk_widget_get_style_context (widget);
1533 state = gtk_widget_get_state_flags (widget);
1534 gtk_style_context_get_background_color (context, state, &color);
1536 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1539 child = gtk_bin_get_child (GTK_BIN (combo_box));
1540 if (GTK_IS_ENTRY (child))
1541 g_object_set (child, "shadow-type",
1542 GTK_SHADOW_NONE == priv->shadow_type ?
1543 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1547 gtk_combo_box_button_toggled (GtkWidget *widget,
1550 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1552 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1554 if (!combo_box->priv->popup_in_progress)
1555 gtk_combo_box_popup (combo_box);
1558 gtk_combo_box_popdown (combo_box);
1562 gtk_combo_box_add (GtkContainer *container,
1565 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1566 GtkComboBoxPrivate *priv = combo_box->priv;
1568 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1570 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1571 "(need an instance of GtkEntry or of a subclass)",
1572 G_OBJECT_TYPE_NAME (widget));
1576 if (priv->cell_view &&
1577 gtk_widget_get_parent (priv->cell_view))
1579 gtk_widget_unparent (priv->cell_view);
1580 _gtk_bin_set_child (GTK_BIN (container), NULL);
1581 gtk_widget_queue_resize (GTK_WIDGET (container));
1584 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1585 _gtk_bin_set_child (GTK_BIN (container), widget);
1587 if (priv->cell_view &&
1588 widget != priv->cell_view)
1590 /* since the cell_view was unparented, it's gone now */
1591 priv->cell_view = NULL;
1593 if (!priv->tree_view && priv->separator)
1595 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1597 priv->separator = NULL;
1599 gtk_widget_queue_resize (GTK_WIDGET (container));
1601 else if (priv->cell_view_frame)
1603 gtk_widget_unparent (priv->cell_view_frame);
1604 priv->cell_view_frame = NULL;
1609 if (priv->has_entry)
1611 /* this flag is a hack to tell the entry to fill its allocation.
1613 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1615 g_signal_connect (widget, "changed",
1616 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1619 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1624 gtk_combo_box_remove (GtkContainer *container,
1627 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1628 GtkComboBoxPrivate *priv = combo_box->priv;
1630 gboolean appears_as_list;
1632 if (priv->has_entry)
1634 GtkWidget *child_widget;
1636 child_widget = gtk_bin_get_child (GTK_BIN (container));
1637 if (widget && widget == child_widget)
1639 g_signal_handlers_disconnect_by_func (widget,
1640 gtk_combo_box_entry_contents_changed,
1642 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1646 if (widget == priv->cell_view)
1647 priv->cell_view = NULL;
1649 gtk_widget_unparent (widget);
1650 _gtk_bin_set_child (GTK_BIN (container), NULL);
1652 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1655 gtk_widget_queue_resize (GTK_WIDGET (container));
1657 if (!priv->tree_view)
1658 appears_as_list = FALSE;
1660 appears_as_list = TRUE;
1662 if (appears_as_list)
1663 gtk_combo_box_list_destroy (combo_box);
1664 else if (GTK_IS_MENU (priv->popup_widget))
1666 gtk_combo_box_menu_destroy (combo_box);
1667 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1668 priv->popup_widget = NULL;
1671 if (!priv->cell_view)
1673 priv->cell_view = gtk_cell_view_new ();
1674 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1675 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1677 gtk_widget_show (priv->cell_view);
1678 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1683 if (appears_as_list)
1684 gtk_combo_box_list_setup (combo_box);
1686 gtk_combo_box_menu_setup (combo_box, TRUE);
1688 if (gtk_tree_row_reference_valid (priv->active_row))
1690 path = gtk_tree_row_reference_get_path (priv->active_row);
1691 gtk_combo_box_set_active_internal (combo_box, path);
1692 gtk_tree_path_free (path);
1695 gtk_combo_box_set_active_internal (combo_box, NULL);
1699 gtk_combo_box_menu_show (GtkWidget *menu,
1702 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1703 GtkComboBoxPrivate *priv = combo_box->priv;
1705 gtk_combo_box_child_show (menu, user_data);
1707 priv->popup_in_progress = TRUE;
1708 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1710 priv->popup_in_progress = FALSE;
1714 gtk_combo_box_menu_hide (GtkWidget *menu,
1717 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1719 gtk_combo_box_child_hide (menu,user_data);
1721 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1726 gtk_combo_box_detacher (GtkWidget *widget,
1729 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1730 GtkComboBoxPrivate *priv = combo_box->priv;
1732 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1734 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1735 gtk_combo_box_menu_show,
1737 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1738 gtk_combo_box_menu_hide,
1741 priv->popup_widget = NULL;
1745 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1748 GtkComboBoxPrivate *priv = combo_box->priv;
1750 if (GTK_IS_MENU (priv->popup_widget))
1752 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1753 priv->popup_widget = NULL;
1755 else if (priv->popup_widget)
1757 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1758 priv->popup_widget);
1759 g_object_unref (priv->popup_widget);
1760 priv->popup_widget = NULL;
1763 if (GTK_IS_MENU (popup))
1765 if (priv->popup_window)
1767 gtk_widget_destroy (priv->popup_window);
1768 priv->popup_window = NULL;
1771 priv->popup_widget = popup;
1774 * Note that we connect to show/hide on the toplevel, not the
1775 * menu itself, since the menu is not shown/hidden when it is
1776 * popped up while torn-off.
1778 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1779 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1780 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1781 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1783 gtk_menu_attach_to_widget (GTK_MENU (popup),
1784 GTK_WIDGET (combo_box),
1785 gtk_combo_box_detacher);
1789 if (!priv->popup_window)
1791 GtkWidget *toplevel;
1793 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1794 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1796 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1797 GDK_WINDOW_TYPE_HINT_COMBO);
1799 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1800 G_CALLBACK (gtk_combo_box_child_show),
1802 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1803 G_CALLBACK (gtk_combo_box_child_hide),
1806 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1807 if (GTK_IS_WINDOW (toplevel))
1809 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1810 GTK_WINDOW (priv->popup_window));
1811 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1812 GTK_WINDOW (toplevel));
1815 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1816 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1817 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1819 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1821 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1824 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1827 gtk_widget_show (priv->scrolled_window);
1829 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1830 priv->scrolled_window);
1833 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1836 gtk_widget_show (popup);
1837 g_object_ref (popup);
1838 priv->popup_widget = popup;
1843 get_widget_padding_and_border (GtkWidget *widget,
1846 GtkStyleContext *context;
1847 GtkStateFlags state;
1850 context = gtk_widget_get_style_context (widget);
1851 state = gtk_style_context_get_state (context);
1853 gtk_style_context_get_padding (context, state, padding);
1854 gtk_style_context_get_border (context, state, &tmp);
1856 padding->top += tmp.top;
1857 padding->right += tmp.right;
1858 padding->bottom += tmp.bottom;
1859 padding->left += tmp.left;
1863 gtk_combo_box_menu_position_below (GtkMenu *menu,
1869 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1870 GtkAllocation child_allocation;
1876 GdkRectangle monitor;
1879 /* FIXME: is using the size request here broken? */
1880 child = gtk_bin_get_child (GTK_BIN (combo_box));
1884 gtk_widget_get_allocation (child, &child_allocation);
1886 if (!gtk_widget_get_has_window (child))
1888 sx += child_allocation.x;
1889 sy += child_allocation.y;
1892 gdk_window_get_root_coords (gtk_widget_get_window (child),
1894 get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding);
1896 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
1901 if (combo_box->priv->popup_fixed_width)
1902 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1904 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1906 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1909 *x = sx + child_allocation.width - req.width;
1912 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1913 monitor_num = gdk_screen_get_monitor_at_window (screen,
1914 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1915 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1919 else if (*x + req.width > monitor.x + monitor.width)
1920 *x = monitor.x + monitor.width - req.width;
1922 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1923 *y += child_allocation.height;
1924 else if (*y - monitor.y >= req.height)
1926 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1927 *y += child_allocation.height;
1935 gtk_combo_box_menu_position_over (GtkMenu *menu,
1941 GtkComboBox *combo_box;
1945 GtkAllocation allocation;
1946 GtkAllocation child_allocation;
1953 combo_box = GTK_COMBO_BOX (user_data);
1954 widget = GTK_WIDGET (combo_box);
1956 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1958 gtk_widget_get_allocation (widget, &allocation);
1960 menu_xpos = allocation.x;
1961 menu_ypos = allocation.y + allocation.height / 2 - 2;
1963 if (combo_box->priv->popup_fixed_width)
1964 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1966 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1970 gtk_widget_get_allocation (active, &child_allocation);
1971 menu_ypos -= child_allocation.height / 2;
1974 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1977 child = children->data;
1979 if (active == child)
1982 if (gtk_widget_get_visible (child))
1984 gtk_widget_get_allocation (child, &child_allocation);
1986 menu_ypos -= child_allocation.height;
1989 children = children->next;
1992 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1993 menu_xpos = menu_xpos + allocation.width - menu_width;
1995 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1996 menu_xpos, menu_ypos,
1997 &menu_xpos, &menu_ypos);
1999 /* Clamp the position on screen */
2000 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
2004 else if ((menu_xpos + menu_width) > screen_width)
2005 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
2014 gtk_combo_box_menu_position (GtkMenu *menu,
2020 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2021 GtkComboBoxPrivate *priv = combo_box->priv;
2022 GtkWidget *menu_item;
2024 if (priv->wrap_width > 0 || priv->cell_view == NULL)
2025 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2028 /* FIXME handle nested menus better */
2029 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2031 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2034 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2037 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2038 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2039 GDK_WINDOW_TYPE_HINT_COMBO);
2043 gtk_combo_box_list_position (GtkComboBox *combo_box,
2049 GtkComboBoxPrivate *priv = combo_box->priv;
2050 GtkAllocation allocation;
2053 GdkRectangle monitor;
2054 GtkRequisition popup_req;
2055 GtkPolicyType hpolicy, vpolicy;
2058 /* under windows, the drop down list is as wide as the combo box itself.
2060 GtkWidget *widget = GTK_WIDGET (combo_box);
2064 gtk_widget_get_allocation (widget, &allocation);
2066 if (!gtk_widget_get_has_window (widget))
2072 window = gtk_widget_get_window (widget);
2074 gdk_window_get_root_coords (gtk_widget_get_window (widget),
2077 *width = allocation.width;
2079 hpolicy = vpolicy = GTK_POLICY_NEVER;
2080 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2083 if (combo_box->priv->popup_fixed_width)
2085 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2087 if (popup_req.width > *width)
2089 hpolicy = GTK_POLICY_ALWAYS;
2090 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2096 /* XXX This code depends on treeviews properly reporting their natural width
2097 * list-mode menus won't fill up to their natural width until then */
2098 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2100 if (popup_req.width > *width)
2102 hpolicy = GTK_POLICY_NEVER;
2103 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2106 *width = popup_req.width;
2110 *height = popup_req.height;
2112 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2113 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2114 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2116 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2117 *x = *x + allocation.width - *width;
2121 else if (*x + *width > monitor.x + monitor.width)
2122 *x = monitor.x + monitor.width - *width;
2124 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2125 *y += allocation.height;
2126 else if (*y - *height >= monitor.y)
2128 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2130 *y += allocation.height;
2131 *height = monitor.y + monitor.height - *y;
2135 *height = *y - monitor.y;
2139 if (popup_req.height > *height)
2141 vpolicy = GTK_POLICY_ALWAYS;
2143 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2149 cell_layout_is_sensitive (GtkCellLayout *layout)
2151 GList *cells, *list;
2154 cells = gtk_cell_layout_get_cells (layout);
2157 for (list = cells; list; list = list->next)
2159 g_object_get (list->data, "sensitive", &sensitive, NULL);
2164 g_list_free (cells);
2170 cell_is_sensitive (GtkCellRenderer *cell,
2173 gboolean *sensitive = data;
2175 g_object_get (cell, "sensitive", sensitive, NULL);
2181 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2184 GtkComboBoxPrivate *priv = combo_box->priv;
2186 if (priv->row_separator_func)
2188 if (priv->row_separator_func (priv->model, iter,
2189 priv->row_separator_data))
2197 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2201 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2210 update_menu_sensitivity (GtkComboBox *combo_box,
2213 GtkComboBoxPrivate *priv = combo_box->priv;
2214 GList *children, *child;
2215 GtkWidget *item, *submenu, *separator;
2216 GtkWidget *cell_view;
2222 children = gtk_container_get_children (GTK_CONTAINER (menu));
2224 for (child = children; child; child = child->next)
2226 item = GTK_WIDGET (child->data);
2227 cell_view = gtk_bin_get_child (GTK_BIN (item));
2229 if (!GTK_IS_CELL_VIEW (cell_view))
2232 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2233 if (submenu != NULL)
2235 gtk_widget_set_sensitive (item, TRUE);
2236 update_menu_sensitivity (combo_box, submenu);
2240 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2242 if (menu != priv->popup_widget && child == children)
2244 separator = GTK_WIDGET (child->next->data);
2245 g_object_set (item, "visible", sensitive, NULL);
2246 g_object_set (separator, "visible", sensitive, NULL);
2249 gtk_widget_set_sensitive (item, sensitive);
2253 g_list_free (children);
2257 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2259 guint32 activate_time)
2261 GtkComboBoxPrivate *priv = combo_box->priv;
2264 gint width, min_width, nat_width;
2266 update_menu_sensitivity (combo_box, priv->popup_widget);
2269 if (gtk_tree_row_reference_valid (priv->active_row))
2271 path = gtk_tree_row_reference_get_path (priv->active_row);
2272 active_item = gtk_tree_path_get_indices (path)[0];
2273 gtk_tree_path_free (path);
2275 if (priv->add_tearoffs)
2279 /* FIXME handle nested menus better */
2280 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2282 if (priv->wrap_width == 0)
2284 GtkAllocation allocation;
2286 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2287 width = allocation.width;
2288 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2289 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2291 if (combo_box->priv->popup_fixed_width)
2292 width = MAX (width, min_width);
2294 width = MAX (width, nat_width);
2296 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2299 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2301 gtk_combo_box_menu_position, combo_box,
2302 button, activate_time);
2306 popup_grab_on_window (GdkWindow *window,
2307 GdkDevice *keyboard,
2309 guint32 activate_time)
2312 gdk_device_grab (keyboard, window,
2313 GDK_OWNERSHIP_WINDOW, TRUE,
2314 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2315 NULL, activate_time) != GDK_GRAB_SUCCESS)
2319 gdk_device_grab (pointer, window,
2320 GDK_OWNERSHIP_WINDOW, TRUE,
2321 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2322 GDK_POINTER_MOTION_MASK,
2323 NULL, activate_time) != GDK_GRAB_SUCCESS)
2326 gdk_device_ungrab (keyboard, activate_time);
2335 * gtk_combo_box_popup:
2336 * @combo_box: a #GtkComboBox
2338 * Pops up the menu or dropdown list of @combo_box.
2340 * This function is mostly intended for use by accessibility technologies;
2341 * applications should have little use for it.
2346 gtk_combo_box_popup (GtkComboBox *combo_box)
2348 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2350 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2354 * gtk_combo_box_popup_for_device:
2355 * @combo_box: a #GtkComboBox
2356 * @device: a #GdkDevice
2358 * Pops up the menu or dropdown list of @combo_box, the popup window
2359 * will be grabbed so only @device and its associated pointer/keyboard
2360 * are the only #GdkDevice<!-- -->s able to send events to it.
2365 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2368 GtkComboBoxPrivate *priv = combo_box->priv;
2369 gint x, y, width, height;
2370 GtkTreePath *path = NULL, *ppath;
2371 GtkWidget *toplevel;
2372 GdkDevice *keyboard, *pointer;
2375 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2376 g_return_if_fail (GDK_IS_DEVICE (device));
2378 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2381 if (gtk_widget_get_mapped (priv->popup_widget))
2384 if (priv->grab_pointer && priv->grab_keyboard)
2387 time = gtk_get_current_event_time ();
2389 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2392 pointer = gdk_device_get_associated_device (device);
2397 keyboard = gdk_device_get_associated_device (device);
2400 if (GTK_IS_MENU (priv->popup_widget))
2402 gtk_combo_box_menu_popup (combo_box,
2403 priv->activate_button,
2404 priv->activate_time);
2408 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2409 if (GTK_IS_WINDOW (toplevel))
2410 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2411 GTK_WINDOW (priv->popup_window));
2413 gtk_widget_show_all (priv->scrolled_window);
2414 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2416 gtk_widget_set_size_request (priv->popup_window, width, height);
2417 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2419 if (gtk_tree_row_reference_valid (priv->active_row))
2421 path = gtk_tree_row_reference_get_path (priv->active_row);
2422 ppath = gtk_tree_path_copy (path);
2423 if (gtk_tree_path_up (ppath))
2424 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2426 gtk_tree_path_free (ppath);
2428 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2432 gtk_widget_show (priv->popup_window);
2436 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2438 gtk_tree_path_free (path);
2441 gtk_widget_grab_focus (priv->popup_window);
2442 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2445 if (!gtk_widget_has_focus (priv->tree_view))
2446 gtk_widget_grab_focus (priv->tree_view);
2448 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2449 keyboard, pointer, time))
2451 gtk_widget_hide (priv->popup_window);
2455 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2456 priv->grab_pointer = pointer;
2457 priv->grab_keyboard = keyboard;
2461 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2465 device = gtk_get_current_event_device ();
2469 GdkDeviceManager *device_manager;
2470 GdkDisplay *display;
2473 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2474 device_manager = gdk_display_get_device_manager (display);
2476 /* No device was set, pick the first master device */
2477 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2478 device = devices->data;
2479 g_list_free (devices);
2482 gtk_combo_box_popup_for_device (combo_box, device);
2486 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2488 if (combo_box->priv->popup_shown)
2490 gtk_combo_box_popdown (combo_box);
2498 * gtk_combo_box_popdown:
2499 * @combo_box: a #GtkComboBox
2501 * Hides the menu or dropdown list of @combo_box.
2503 * This function is mostly intended for use by accessibility technologies;
2504 * applications should have little use for it.
2509 gtk_combo_box_popdown (GtkComboBox *combo_box)
2511 GtkComboBoxPrivate *priv = combo_box->priv;
2513 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2515 if (GTK_IS_MENU (priv->popup_widget))
2517 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2521 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2524 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2525 gtk_widget_hide (priv->popup_window);
2526 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2529 priv->grab_pointer = NULL;
2530 priv->grab_keyboard = NULL;
2533 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2534 GtkAllocation button_allocation; \
2535 gtk_widget_get_preferred_size (combo_box->priv->button, \
2539 button_allocation.x = allocation->x; \
2541 button_allocation.x = allocation->x + allocation->width \
2544 button_allocation.y = allocation->y; \
2545 button_allocation.width = MAX (1, req.width); \
2546 button_allocation.height = allocation->height; \
2547 button_allocation.height = MAX (1, button_allocation.height); \
2549 gtk_widget_size_allocate (combo_box->priv->button, \
2550 &button_allocation);
2554 gtk_combo_box_size_allocate (GtkWidget *widget,
2555 GtkAllocation *allocation)
2557 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2558 GtkComboBoxPrivate *priv = combo_box->priv;
2559 GtkWidget *child_widget;
2560 GtkAllocation child;
2562 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2565 gtk_widget_set_allocation (widget, allocation);
2566 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2567 get_widget_padding_and_border (widget, &padding);
2569 allocation->x += padding.left;
2570 allocation->y += padding.top;
2571 allocation->width -= padding.left + padding.right;
2572 allocation->height -= padding.top + padding.bottom;
2574 if (!priv->tree_view)
2576 if (priv->cell_view)
2578 GtkBorder button_padding;
2582 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2583 get_widget_padding_and_border (priv->button, &button_padding);
2585 /* menu mode; child_widget is priv->cell_view.
2586 * Allocate the button to the full combobox allocation (minus the
2589 gtk_widget_size_allocate (priv->button, allocation);
2591 child.x = allocation->x;
2592 child.y = allocation->y;
2593 width = allocation->width;
2594 child.height = allocation->height;
2596 if (!priv->is_cell_renderer)
2598 /* restrict allocation of the child into the button box
2599 * if we're not in cell renderer mode.
2601 child.x += border_width + button_padding.left;
2602 child.y += border_width + button_padding.top;
2603 width -= 2 * border_width +
2604 button_padding.left + button_padding.right;
2605 child.height -= 2 * border_width +
2606 button_padding.top + button_padding.bottom;
2609 /* allocate the box containing the separator and the arrow */
2610 gtk_widget_get_preferred_size (priv->box, &req, NULL);
2611 child.width = req.width;
2613 child.x += width - req.width;
2614 child.width = MAX (1, child.width);
2615 child.height = MAX (1, child.height);
2616 gtk_widget_size_allocate (priv->box, &child);
2620 child.x += req.width;
2621 child.width = allocation->x + allocation->width
2622 - border_width - child.x - button_padding.right;
2626 child.width = child.x;
2627 child.x = allocation->x
2628 + border_width + button_padding.left;
2629 child.width -= child.x;
2632 if (gtk_widget_get_visible (priv->popup_widget))
2634 gint width, menu_width;
2636 if (priv->wrap_width == 0)
2638 GtkAllocation combo_box_allocation;
2640 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2641 width = combo_box_allocation.width;
2642 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2644 if (combo_box->priv->popup_fixed_width)
2645 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2647 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2649 gtk_widget_set_size_request (priv->popup_widget,
2650 MAX (width, menu_width), -1);
2653 /* reposition the menu after giving it a new width */
2654 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2657 child.width = MAX (1, child.width);
2658 child.height = MAX (1, child.height);
2659 gtk_widget_size_allocate (child_widget, &child);
2663 /* menu mode; child_widget has been set with gtk_container_add().
2664 * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2665 * Allocate the button at the far end, according to the direction
2668 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2670 /* After the macro, button_allocation has the button allocation rect */
2673 child.x = button_allocation.x + button_allocation.width;
2675 child.x = allocation->x;
2677 child.y = allocation->y;
2678 child.width = allocation->width - button_allocation.width;
2679 child.height = button_allocation.height;
2681 child.width = MAX (1, child.width);
2683 gtk_widget_size_allocate (child_widget, &child);
2688 /* list mode; child_widget might be either priv->cell_view or a child
2689 * added with gtk_container_add().
2692 /* After the macro, button_allocation has the button allocation rect */
2693 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2696 child.x = button_allocation.x + button_allocation.width;
2698 child.x = allocation->x;
2700 child.y = allocation->y;
2701 child.width = allocation->width - button_allocation.width;
2702 child.height = button_allocation.height;
2704 if (priv->cell_view_frame)
2706 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2708 /* restrict allocation of the child into the frame box if it's present */
2709 if (priv->has_frame)
2711 GtkBorder frame_padding;
2714 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2715 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2717 child.x += border_width + frame_padding.left;
2718 child.y += border_width + frame_padding.right;
2719 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2720 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2724 if (gtk_widget_get_visible (priv->popup_window))
2726 gint x, y, width, height;
2727 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2728 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2729 gtk_widget_set_size_request (priv->popup_window, width, height);
2732 /* allocate the child */
2733 child.width = MAX (1, child.width);
2734 child.height = MAX (1, child.height);
2735 gtk_widget_size_allocate (child_widget, &child);
2739 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2742 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2744 GtkComboBoxPrivate *priv = combo_box->priv;
2748 g_signal_handler_disconnect (priv->model,
2750 g_signal_handler_disconnect (priv->model,
2752 g_signal_handler_disconnect (priv->model,
2753 priv->reordered_id);
2754 g_signal_handler_disconnect (priv->model,
2760 g_object_unref (priv->model);
2764 if (priv->active_row)
2766 gtk_tree_row_reference_free (priv->active_row);
2767 priv->active_row = NULL;
2770 if (priv->cell_view)
2771 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2775 gtk_combo_box_forall (GtkContainer *container,
2776 gboolean include_internals,
2777 GtkCallback callback,
2778 gpointer callback_data)
2780 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2781 GtkComboBoxPrivate *priv = combo_box->priv;
2784 if (include_internals)
2787 (* callback) (priv->button, callback_data);
2788 if (priv->cell_view_frame)
2789 (* callback) (priv->cell_view_frame, callback_data);
2792 child = gtk_bin_get_child (GTK_BIN (container));
2794 (* callback) (child, callback_data);
2798 gtk_combo_box_child_show (GtkWidget *widget,
2799 GtkComboBox *combo_box)
2801 GtkComboBoxPrivate *priv = combo_box->priv;
2803 priv->popup_shown = TRUE;
2804 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2808 gtk_combo_box_child_hide (GtkWidget *widget,
2809 GtkComboBox *combo_box)
2811 GtkComboBoxPrivate *priv = combo_box->priv;
2813 priv->popup_shown = FALSE;
2814 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2818 gtk_combo_box_draw (GtkWidget *widget,
2821 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2822 GtkComboBoxPrivate *priv = combo_box->priv;
2824 if (priv->shadow_type != GTK_SHADOW_NONE)
2826 GtkStyleContext *context;
2828 context = gtk_widget_get_style_context (widget);
2830 gtk_render_background (context, cr, 0, 0,
2831 gtk_widget_get_allocated_width (widget),
2832 gtk_widget_get_allocated_height (widget));
2833 gtk_render_frame (context, cr, 0, 0,
2834 gtk_widget_get_allocated_width (widget),
2835 gtk_widget_get_allocated_height (widget));
2838 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2841 if (priv->tree_view && priv->cell_view_frame)
2843 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2844 priv->cell_view_frame, cr);
2847 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2848 gtk_bin_get_child (GTK_BIN (widget)),
2864 path_visible (GtkTreeView *view,
2870 /* Note that we rely on the fact that collapsed rows don't have nodes
2872 return _gtk_tree_view_find_node (view, path, &tree, &node);
2876 tree_next_func (GtkTreeModel *model,
2881 SearchData *search_data = (SearchData *)data;
2883 if (search_data->found)
2885 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2888 if (search_data->visible &&
2889 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2892 search_data->set = TRUE;
2893 search_data->iter = *iter;
2898 if (gtk_tree_path_compare (path, search_data->path) == 0)
2899 search_data->found = TRUE;
2905 tree_next (GtkComboBox *combo,
2906 GtkTreeModel *model,
2911 SearchData search_data;
2913 search_data.combo = combo;
2914 search_data.path = gtk_tree_model_get_path (model, iter);
2915 search_data.visible = visible;
2916 search_data.found = FALSE;
2917 search_data.set = FALSE;
2919 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2921 *next = search_data.iter;
2923 gtk_tree_path_free (search_data.path);
2925 return search_data.set;
2929 tree_prev_func (GtkTreeModel *model,
2934 SearchData *search_data = (SearchData *)data;
2936 if (gtk_tree_path_compare (path, search_data->path) == 0)
2938 search_data->found = TRUE;
2942 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2945 if (search_data->visible &&
2946 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2949 search_data->set = TRUE;
2950 search_data->iter = *iter;
2956 tree_prev (GtkComboBox *combo,
2957 GtkTreeModel *model,
2962 SearchData search_data;
2964 search_data.combo = combo;
2965 search_data.path = gtk_tree_model_get_path (model, iter);
2966 search_data.visible = visible;
2967 search_data.found = FALSE;
2968 search_data.set = FALSE;
2970 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2972 *prev = search_data.iter;
2974 gtk_tree_path_free (search_data.path);
2976 return search_data.set;
2980 tree_last_func (GtkTreeModel *model,
2985 SearchData *search_data = (SearchData *)data;
2987 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2990 /* Note that we rely on the fact that collapsed rows don't have nodes
2992 if (search_data->visible &&
2993 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2996 search_data->set = TRUE;
2997 search_data->iter = *iter;
3003 tree_last (GtkComboBox *combo,
3004 GtkTreeModel *model,
3008 SearchData search_data;
3010 search_data.combo = combo;
3011 search_data.visible = visible;
3012 search_data.set = FALSE;
3014 gtk_tree_model_foreach (model, tree_last_func, &search_data);
3016 *last = search_data.iter;
3018 return search_data.set;
3023 tree_first_func (GtkTreeModel *model,
3028 SearchData *search_data = (SearchData *)data;
3030 if (!tree_column_row_is_sensitive (search_data->combo, iter))
3033 if (search_data->visible &&
3034 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3037 search_data->set = TRUE;
3038 search_data->iter = *iter;
3044 tree_first (GtkComboBox *combo,
3045 GtkTreeModel *model,
3049 SearchData search_data;
3051 search_data.combo = combo;
3052 search_data.visible = visible;
3053 search_data.set = FALSE;
3055 gtk_tree_model_foreach (model, tree_first_func, &search_data);
3057 *first = search_data.iter;
3059 return search_data.set;
3063 gtk_combo_box_scroll_event (GtkWidget *widget,
3064 GdkEventScroll *event)
3066 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3069 GtkTreeIter new_iter;
3071 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3074 if (event->direction == GDK_SCROLL_UP)
3075 found = tree_prev (combo_box, combo_box->priv->model,
3076 &iter, &new_iter, FALSE);
3078 found = tree_next (combo_box, combo_box->priv->model,
3079 &iter, &new_iter, FALSE);
3082 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3091 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3095 GtkComboBoxPrivate *priv = combo->priv;
3097 if (priv->row_separator_func)
3098 return priv->row_separator_func (model, iter, priv->row_separator_data);
3104 gtk_combo_box_header_func (GtkTreeModel *model,
3108 /* Every submenu has a selectable header, however we
3109 * can expose a method to make that configurable by
3110 * the user (like row_separator_func is done) */
3115 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3116 gboolean add_children)
3118 GtkComboBoxPrivate *priv = combo_box->priv;
3122 child = gtk_bin_get_child (GTK_BIN (combo_box));
3124 if (priv->cell_view)
3126 priv->button = gtk_toggle_button_new ();
3127 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3128 priv->focus_on_click);
3130 g_signal_connect (priv->button, "toggled",
3131 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3132 gtk_widget_set_parent (priv->button,
3133 gtk_widget_get_parent (child));
3135 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3136 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3138 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3139 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3141 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3142 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3143 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3145 gtk_widget_show_all (priv->button);
3149 priv->button = gtk_toggle_button_new ();
3150 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3151 priv->focus_on_click);
3153 g_signal_connect (priv->button, "toggled",
3154 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3155 gtk_widget_set_parent (priv->button,
3156 gtk_widget_get_parent (child));
3158 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3159 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3160 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3161 gtk_widget_show_all (priv->button);
3164 g_signal_connect (priv->button, "button-press-event",
3165 G_CALLBACK (gtk_combo_box_menu_button_press),
3167 g_signal_connect (priv->button, "state-flags-changed",
3168 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3171 /* create our funky menu */
3172 menu = _gtk_tree_menu_new_with_area (priv->area);
3173 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3175 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3177 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3178 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3179 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3180 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3181 combo_box->priv->add_tearoffs);
3183 g_signal_connect (menu, "menu-activate",
3184 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3186 /* Chain our row_separator_func through */
3187 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3188 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3191 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3192 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3195 g_signal_connect (menu, "key-press-event",
3196 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3197 gtk_combo_box_set_popup_widget (combo_box, menu);
3199 gtk_combo_box_update_title (combo_box);
3203 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3205 GtkComboBoxPrivate *priv = combo_box->priv;
3207 g_signal_handlers_disconnect_matched (priv->button,
3208 G_SIGNAL_MATCH_DATA,
3210 gtk_combo_box_menu_button_press, NULL);
3211 g_signal_handlers_disconnect_matched (priv->button,
3212 G_SIGNAL_MATCH_DATA,
3214 gtk_combo_box_button_state_flags_changed, combo_box);
3215 g_signal_handlers_disconnect_matched (priv->popup_widget,
3216 G_SIGNAL_MATCH_DATA,
3218 gtk_combo_box_menu_activate, combo_box);
3220 /* unparent will remove our latest ref */
3221 gtk_widget_unparent (priv->button);
3224 priv->button = NULL;
3226 priv->separator = NULL;
3228 /* changing the popup window will unref the menu and the children */
3233 gtk_combo_box_menu_button_press (GtkWidget *widget,
3234 GdkEventButton *event,
3237 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3238 GtkComboBoxPrivate *priv = combo_box->priv;
3240 if (GTK_IS_MENU (priv->popup_widget) &&
3241 event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3243 if (priv->focus_on_click &&
3244 !gtk_widget_has_focus (priv->button))
3245 gtk_widget_grab_focus (priv->button);
3247 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3256 gtk_combo_box_menu_activate (GtkWidget *menu,
3258 GtkComboBox *combo_box)
3262 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3263 gtk_combo_box_set_active_iter (combo_box, &iter);
3265 g_object_set (combo_box,
3266 "editing-canceled", FALSE,
3272 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3275 gboolean sensitive = TRUE; /* fool code checkers */
3277 if (!combo_box->priv->button)
3280 switch (combo_box->priv->button_sensitivity)
3282 case GTK_SENSITIVITY_ON:
3285 case GTK_SENSITIVITY_OFF:
3288 case GTK_SENSITIVITY_AUTO:
3289 sensitive = combo_box->priv->model &&
3290 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3293 g_assert_not_reached ();
3297 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3299 /* In list-mode, we also need to update sensitivity of the event box */
3300 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3301 && combo_box->priv->cell_view)
3302 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3306 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3311 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3313 if (combo_box->priv->tree_view)
3314 gtk_combo_box_list_popup_resize (combo_box);
3316 gtk_combo_box_update_sensitivity (combo_box);
3320 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3324 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3325 GtkComboBoxPrivate *priv = combo_box->priv;
3327 if (!gtk_tree_row_reference_valid (priv->active_row))
3329 if (priv->cell_view)
3330 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3331 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3334 if (priv->tree_view)
3335 gtk_combo_box_list_popup_resize (combo_box);
3337 gtk_combo_box_update_sensitivity (combo_box);
3341 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3347 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3351 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3356 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3357 GtkComboBoxPrivate *priv = combo_box->priv;
3358 GtkTreePath *active_path;
3360 /* FIXME this belongs to GtkCellView */
3361 if (gtk_tree_row_reference_valid (priv->active_row))
3363 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3364 if (gtk_tree_path_compare (path, active_path) == 0 &&
3366 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3367 gtk_tree_path_free (active_path);
3370 if (priv->tree_view)
3371 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3375 list_popup_resize_idle (gpointer user_data)
3377 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3378 GtkComboBoxPrivate *priv = combo_box->priv;
3379 gint x, y, width, height;
3381 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3383 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3385 gtk_widget_set_size_request (priv->popup_window, width, height);
3386 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3389 priv->resize_idle_id = 0;
3395 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3397 GtkComboBoxPrivate *priv = combo_box->priv;
3399 if (!priv->resize_idle_id)
3400 priv->resize_idle_id =
3401 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3405 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3410 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3412 gtk_combo_box_list_popup_resize (combo_box);
3421 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3423 GtkComboBoxPrivate *priv = combo_box->priv;
3424 GtkTreeSelection *sel;
3426 GtkWidget *widget = GTK_WIDGET (combo_box);
3428 priv->button = gtk_toggle_button_new ();
3429 child = gtk_bin_get_child (GTK_BIN (combo_box));
3430 gtk_widget_set_parent (priv->button,
3431 gtk_widget_get_parent (child));
3432 g_signal_connect (priv->button, "button-press-event",
3433 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3434 g_signal_connect (priv->button, "toggled",
3435 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3437 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3438 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3439 priv->separator = NULL;
3440 gtk_widget_show_all (priv->button);
3442 if (priv->cell_view)
3444 GtkStyleContext *context;
3445 GtkStateFlags state;
3448 context = gtk_widget_get_style_context (widget);
3449 state = gtk_widget_get_state_flags (widget);
3450 gtk_style_context_get_background_color (context, state, &color);
3452 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3454 priv->box = gtk_event_box_new ();
3455 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3458 if (priv->has_frame)
3460 priv->cell_view_frame = gtk_frame_new (NULL);
3461 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3466 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3467 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3471 gtk_widget_set_parent (priv->cell_view_frame,
3472 gtk_widget_get_parent (child));
3473 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3474 gtk_widget_show_all (priv->cell_view_frame);
3476 g_signal_connect (priv->box, "button-press-event",
3477 G_CALLBACK (gtk_combo_box_list_button_pressed),
3481 priv->tree_view = gtk_tree_view_new ();
3482 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3483 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3484 gtk_tree_selection_set_select_function (sel,
3485 gtk_combo_box_list_select_func,
3487 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3489 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3492 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3493 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3497 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3499 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3500 gtk_tree_view_column_new_with_area (priv->area));
3502 if (gtk_tree_row_reference_valid (priv->active_row))
3506 path = gtk_tree_row_reference_get_path (priv->active_row);
3507 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3509 gtk_tree_path_free (path);
3512 /* set sample/popup widgets */
3513 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3515 g_signal_connect (priv->tree_view, "key-press-event",
3516 G_CALLBACK (gtk_combo_box_list_key_press),
3518 g_signal_connect (priv->tree_view, "enter-notify-event",
3519 G_CALLBACK (gtk_combo_box_list_enter_notify),
3521 g_signal_connect (priv->tree_view, "row-expanded",
3522 G_CALLBACK (gtk_combo_box_model_row_expanded),
3524 g_signal_connect (priv->tree_view, "row-collapsed",
3525 G_CALLBACK (gtk_combo_box_model_row_expanded),
3527 g_signal_connect (priv->popup_window, "button-press-event",
3528 G_CALLBACK (gtk_combo_box_list_button_pressed),
3530 g_signal_connect (priv->popup_window, "button-release-event",
3531 G_CALLBACK (gtk_combo_box_list_button_released),
3534 gtk_widget_show (priv->tree_view);
3536 gtk_combo_box_update_sensitivity (combo_box);
3540 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3542 GtkComboBoxPrivate *priv = combo_box->priv;
3544 /* disconnect signals */
3545 g_signal_handlers_disconnect_matched (priv->tree_view,
3546 G_SIGNAL_MATCH_DATA,
3547 0, 0, NULL, NULL, combo_box);
3548 g_signal_handlers_disconnect_matched (priv->button,
3549 G_SIGNAL_MATCH_DATA,
3551 gtk_combo_box_list_button_pressed,
3553 g_signal_handlers_disconnect_matched (priv->popup_window,
3554 G_SIGNAL_MATCH_DATA,
3556 gtk_combo_box_list_button_pressed,
3558 g_signal_handlers_disconnect_matched (priv->popup_window,
3559 G_SIGNAL_MATCH_DATA,
3561 gtk_combo_box_list_button_released,
3564 g_signal_handlers_disconnect_matched (priv->popup_window,
3565 G_SIGNAL_MATCH_DATA,
3567 gtk_combo_box_child_show,
3570 g_signal_handlers_disconnect_matched (priv->popup_window,
3571 G_SIGNAL_MATCH_DATA,
3573 gtk_combo_box_child_hide,
3577 g_signal_handlers_disconnect_matched (priv->box,
3578 G_SIGNAL_MATCH_DATA,
3580 gtk_combo_box_list_button_pressed,
3583 /* destroy things (unparent will kill the latest ref from us)
3584 * last unref on button will destroy the arrow
3586 gtk_widget_unparent (priv->button);
3587 priv->button = NULL;
3590 if (priv->cell_view)
3592 g_object_set (priv->cell_view,
3593 "background-set", FALSE,
3597 if (priv->cell_view_frame)
3599 gtk_widget_unparent (priv->cell_view_frame);
3600 priv->cell_view_frame = NULL;
3604 if (priv->scroll_timer)
3606 g_source_remove (priv->scroll_timer);
3607 priv->scroll_timer = 0;
3610 if (priv->resize_idle_id)
3612 g_source_remove (priv->resize_idle_id);
3613 priv->resize_idle_id = 0;
3616 gtk_widget_destroy (priv->tree_view);
3618 priv->tree_view = NULL;
3619 if (priv->popup_widget)
3621 g_object_unref (priv->popup_widget);
3622 priv->popup_widget = NULL;
3629 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3630 GdkEventButton *event,
3633 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3634 GtkComboBoxPrivate *priv = combo_box->priv;
3636 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3638 if (ewidget == priv->popup_window)
3641 if ((ewidget != priv->button && ewidget != priv->box) ||
3642 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3645 if (priv->focus_on_click &&
3646 !gtk_widget_has_focus (priv->button))
3647 gtk_widget_grab_focus (priv->button);
3649 gtk_combo_box_popup_for_device (combo_box, event->device);
3651 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3653 priv->auto_scroll = FALSE;
3654 if (priv->scroll_timer == 0)
3655 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3656 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3659 priv->popup_in_progress = TRUE;
3665 gtk_combo_box_list_button_released (GtkWidget *widget,
3666 GdkEventButton *event,
3670 GtkTreePath *path = NULL;
3673 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3674 GtkComboBoxPrivate *priv = combo_box->priv;
3676 gboolean popup_in_progress = FALSE;
3678 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3680 if (priv->popup_in_progress)
3682 popup_in_progress = TRUE;
3683 priv->popup_in_progress = FALSE;
3686 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3688 if (priv->scroll_timer)
3690 g_source_remove (priv->scroll_timer);
3691 priv->scroll_timer = 0;
3694 if (ewidget != priv->tree_view)
3696 if ((ewidget == priv->button ||
3697 ewidget == priv->box) &&
3698 !popup_in_progress &&
3699 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3701 gtk_combo_box_popdown (combo_box);
3705 /* released outside treeview */
3706 if (ewidget != priv->button && ewidget != priv->box)
3708 gtk_combo_box_popdown (combo_box);
3716 /* select something cool */
3717 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3723 return TRUE; /* clicked outside window? */
3725 gtk_tree_model_get_iter (priv->model, &iter, path);
3726 gtk_tree_path_free (path);
3728 gtk_combo_box_popdown (combo_box);
3730 if (tree_column_row_is_sensitive (combo_box, &iter))
3731 gtk_combo_box_set_active_iter (combo_box, &iter);
3737 gtk_combo_box_menu_key_press (GtkWidget *widget,
3741 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3743 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3745 /* The menu hasn't managed the
3746 * event, forward it to the combobox
3748 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3755 gtk_combo_box_list_key_press (GtkWidget *widget,
3759 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3762 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3763 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3765 GtkTreeModel *model = NULL;
3767 gtk_combo_box_popdown (combo_box);
3769 if (combo_box->priv->model)
3771 GtkTreeSelection *sel;
3773 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3775 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3776 gtk_combo_box_set_active_iter (combo_box, &iter);
3782 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3784 /* The list hasn't managed the
3785 * event, forward it to the combobox
3787 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3794 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3799 GtkAllocation allocation;
3800 GtkWidget *tree_view = combo_box->priv->tree_view;
3803 gtk_widget_get_allocation (tree_view, &allocation);
3805 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3806 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3808 if (x <= allocation.x &&
3809 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3811 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3812 gtk_adjustment_set_value (adj, value);
3814 else if (x >= allocation.x + allocation.width &&
3815 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3817 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3818 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3822 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3823 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3825 if (y <= allocation.y &&
3826 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3828 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3829 gtk_adjustment_set_value (adj, value);
3831 else if (y >= allocation.height &&
3832 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3834 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3835 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3841 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3843 GtkComboBoxPrivate *priv = combo_box->priv;
3846 if (priv->auto_scroll)
3848 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3851 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3858 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3859 GdkEventCrossing *event,
3862 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3864 combo_box->priv->auto_scroll = TRUE;
3870 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3871 GtkTreeModel *model,
3873 gboolean path_currently_selected,
3876 GList *list, *columns;
3877 gboolean sensitive = FALSE;
3879 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3881 for (list = columns; list && !sensitive; list = list->next)
3883 GList *cells, *cell;
3884 gboolean cell_sensitive, cell_visible;
3886 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3888 if (!gtk_tree_view_column_get_visible (column))
3891 gtk_tree_model_get_iter (model, &iter, path);
3892 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3895 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3898 g_object_get (cell->data,
3899 "sensitive", &cell_sensitive,
3900 "visible", &cell_visible,
3903 if (cell_visible && cell_sensitive)
3912 g_list_free (cells);
3915 g_list_free (columns);
3921 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3926 /* XXX Do nothing ? */
3930 * GtkCellLayout implementation
3932 static GtkCellArea *
3933 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3935 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3936 GtkComboBoxPrivate *priv = combo->priv;
3938 if (G_UNLIKELY (!priv->area))
3940 priv->area = gtk_cell_area_box_new ();
3941 g_object_ref_sink (priv->area);
3952 * gtk_combo_box_new:
3954 * Creates a new empty #GtkComboBox.
3956 * Return value: A new #GtkComboBox.
3961 gtk_combo_box_new (void)
3963 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3967 * gtk_combo_box_new_with_area:
3968 * @area: the #GtkCellArea to use to layout cell renderers
3970 * Creates a new empty #GtkComboBox using @area to layout cells.
3972 * Return value: A new #GtkComboBox.
3975 gtk_combo_box_new_with_area (GtkCellArea *area)
3977 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3981 * gtk_combo_box_new_with_area_and_entry:
3982 * @area: the #GtkCellArea to use to layout cell renderers
3984 * Creates a new empty #GtkComboBox with an entry.
3986 * The new combo box will use @area to layout cells.
3988 * Return value: A new #GtkComboBox.
3991 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3993 return g_object_new (GTK_TYPE_COMBO_BOX,
4001 * gtk_combo_box_new_with_entry:
4003 * Creates a new empty #GtkComboBox with an entry.
4005 * Return value: A new #GtkComboBox.
4008 gtk_combo_box_new_with_entry (void)
4010 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
4014 * gtk_combo_box_new_with_model:
4015 * @model: A #GtkTreeModel.
4017 * Creates a new #GtkComboBox with the model initialized to @model.
4019 * Return value: A new #GtkComboBox.
4024 gtk_combo_box_new_with_model (GtkTreeModel *model)
4026 GtkComboBox *combo_box;
4028 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4030 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4032 return GTK_WIDGET (combo_box);
4036 * gtk_combo_box_new_with_model_and_entry:
4037 * @model: A #GtkTreeModel
4039 * Creates a new empty #GtkComboBox with an entry
4040 * and with the model initialized to @model.
4042 * Return value: A new #GtkComboBox
4045 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4047 return g_object_new (GTK_TYPE_COMBO_BOX,
4054 * gtk_combo_box_get_wrap_width:
4055 * @combo_box: A #GtkComboBox
4057 * Returns the wrap width which is used to determine the number of columns
4058 * for the popup menu. If the wrap width is larger than 1, the combo box
4061 * Returns: the wrap width.
4066 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4068 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4070 return combo_box->priv->wrap_width;
4074 * gtk_combo_box_set_wrap_width:
4075 * @combo_box: A #GtkComboBox
4076 * @width: Preferred number of columns
4078 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4079 * the preferred number of columns when you want the popup to be layed out
4085 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4088 GtkComboBoxPrivate *priv;
4090 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4091 g_return_if_fail (width >= 0);
4093 priv = combo_box->priv;
4095 if (width != priv->wrap_width)
4097 priv->wrap_width = width;
4099 gtk_combo_box_check_appearance (combo_box);
4101 if (GTK_IS_TREE_MENU (priv->popup_widget))
4102 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4104 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4109 * gtk_combo_box_get_row_span_column:
4110 * @combo_box: A #GtkComboBox
4112 * Returns the column with row span information for @combo_box.
4114 * Returns: the row span column.
4119 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4121 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4123 return combo_box->priv->row_column;
4127 * gtk_combo_box_set_row_span_column:
4128 * @combo_box: A #GtkComboBox.
4129 * @row_span: A column in the model passed during construction.
4131 * Sets the column with row span information for @combo_box to be @row_span.
4132 * The row span column contains integers which indicate how many rows
4133 * an item should span.
4138 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4141 GtkComboBoxPrivate *priv;
4144 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4146 priv = combo_box->priv;
4148 col = gtk_tree_model_get_n_columns (priv->model);
4149 g_return_if_fail (row_span >= -1 && row_span < col);
4151 if (row_span != priv->row_column)
4153 priv->row_column = row_span;
4155 if (GTK_IS_TREE_MENU (priv->popup_widget))
4156 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4158 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4163 * gtk_combo_box_get_column_span_column:
4164 * @combo_box: A #GtkComboBox
4166 * Returns the column with column span information for @combo_box.
4168 * Returns: the column span column.
4173 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4175 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4177 return combo_box->priv->col_column;
4181 * gtk_combo_box_set_column_span_column:
4182 * @combo_box: A #GtkComboBox
4183 * @column_span: A column in the model passed during construction
4185 * Sets the column with column span information for @combo_box to be
4186 * @column_span. The column span column contains integers which indicate
4187 * how many columns an item should span.
4192 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4195 GtkComboBoxPrivate *priv;
4198 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4200 priv = combo_box->priv;
4202 col = gtk_tree_model_get_n_columns (priv->model);
4203 g_return_if_fail (column_span >= -1 && column_span < col);
4205 if (column_span != priv->col_column)
4207 priv->col_column = column_span;
4209 if (GTK_IS_TREE_MENU (priv->popup_widget))
4210 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4212 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4217 * gtk_combo_box_get_active:
4218 * @combo_box: A #GtkComboBox
4220 * Returns the index of the currently active item, or -1 if there's no
4221 * active item. If the model is a non-flat treemodel, and the active item
4222 * is not an immediate child of the root of the tree, this function returns
4223 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4224 * <literal>path</literal> is the #GtkTreePath of the active item.
4226 * Return value: An integer which is the index of the currently active item,
4227 * or -1 if there's no active item.
4232 gtk_combo_box_get_active (GtkComboBox *combo_box)
4234 GtkComboBoxPrivate *priv;
4237 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4239 priv = combo_box->priv;
4241 if (gtk_tree_row_reference_valid (priv->active_row))
4245 path = gtk_tree_row_reference_get_path (priv->active_row);
4246 result = gtk_tree_path_get_indices (path)[0];
4247 gtk_tree_path_free (path);
4256 * gtk_combo_box_set_active:
4257 * @combo_box: A #GtkComboBox
4258 * @index_: An index in the model passed during construction, or -1 to have
4261 * Sets the active item of @combo_box to be the item at @index.
4266 gtk_combo_box_set_active (GtkComboBox *combo_box,
4269 GtkTreePath *path = NULL;
4270 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4271 g_return_if_fail (index_ >= -1);
4273 if (combo_box->priv->model == NULL)
4275 /* Save index, in case the model is set after the index */
4276 combo_box->priv->active = index_;
4282 path = gtk_tree_path_new_from_indices (index_, -1);
4284 gtk_combo_box_set_active_internal (combo_box, path);
4287 gtk_tree_path_free (path);
4291 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4294 GtkComboBoxPrivate *priv = combo_box->priv;
4295 GtkTreePath *active_path;
4298 /* Remember whether the initially active row is valid. */
4299 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4301 if (path && is_valid_row_reference)
4303 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4304 path_cmp = gtk_tree_path_compare (path, active_path);
4305 gtk_tree_path_free (active_path);
4310 if (priv->active_row)
4312 gtk_tree_row_reference_free (priv->active_row);
4313 priv->active_row = NULL;
4318 if (priv->tree_view)
4319 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4322 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4324 if (GTK_IS_MENU (menu))
4325 gtk_menu_set_active (menu, -1);
4328 if (priv->cell_view)
4329 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4332 * Do not emit a "changed" signal when an already invalid selection was
4333 * now set to invalid.
4335 if (!is_valid_row_reference)
4341 gtk_tree_row_reference_new (priv->model, path);
4343 if (priv->tree_view)
4345 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4348 else if (GTK_IS_MENU (priv->popup_widget))
4350 /* FIXME handle nested menus better */
4351 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4352 gtk_tree_path_get_indices (path)[0]);
4355 if (priv->cell_view)
4356 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4360 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4361 g_object_notify (G_OBJECT (combo_box), "active");
4362 if (combo_box->priv->id_column >= 0)
4363 g_object_notify (G_OBJECT (combo_box), "active-id");
4368 * gtk_combo_box_get_active_iter:
4369 * @combo_box: A #GtkComboBox
4370 * @iter: (out): The uninitialized #GtkTreeIter
4372 * Sets @iter to point to the current active item, if it exists.
4374 * Return value: %TRUE, if @iter was set
4379 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4385 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4387 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4390 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4391 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4392 gtk_tree_path_free (path);
4398 * gtk_combo_box_set_active_iter:
4399 * @combo_box: A #GtkComboBox
4400 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4402 * Sets the current active item to be the one referenced by @iter, or
4403 * unsets the active item if @iter is %NULL.
4408 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4411 GtkTreePath *path = NULL;
4413 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4416 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4418 gtk_combo_box_set_active_internal (combo_box, path);
4419 gtk_tree_path_free (path);
4423 * gtk_combo_box_set_model:
4424 * @combo_box: A #GtkComboBox
4425 * @model: (allow-none): A #GtkTreeModel
4427 * Sets the model used by @combo_box to be @model. Will unset a previously set
4428 * model (if applicable). If model is %NULL, then it will unset the model.
4430 * Note that this function does not clear the cell renderers, you have to
4431 * call gtk_cell_layout_clear() yourself if you need to set up different
4432 * cell renderers for the new model.
4437 gtk_combo_box_set_model (GtkComboBox *combo_box,
4438 GtkTreeModel *model)
4440 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4441 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4443 if (model == combo_box->priv->model)
4446 gtk_combo_box_unset_model (combo_box);
4451 combo_box->priv->model = model;
4452 g_object_ref (combo_box->priv->model);
4454 combo_box->priv->inserted_id =
4455 g_signal_connect (combo_box->priv->model, "row-inserted",
4456 G_CALLBACK (gtk_combo_box_model_row_inserted),
4458 combo_box->priv->deleted_id =
4459 g_signal_connect (combo_box->priv->model, "row-deleted",
4460 G_CALLBACK (gtk_combo_box_model_row_deleted),
4462 combo_box->priv->reordered_id =
4463 g_signal_connect (combo_box->priv->model, "rows-reordered",
4464 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4466 combo_box->priv->changed_id =
4467 g_signal_connect (combo_box->priv->model, "row-changed",
4468 G_CALLBACK (gtk_combo_box_model_row_changed),
4471 if (combo_box->priv->tree_view)
4474 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4475 combo_box->priv->model);
4476 gtk_combo_box_list_popup_resize (combo_box);
4479 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4482 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4483 combo_box->priv->model);
4486 if (combo_box->priv->cell_view)
4487 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4488 combo_box->priv->model);
4490 if (combo_box->priv->active != -1)
4492 /* If an index was set in advance, apply it now */
4493 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4494 combo_box->priv->active = -1;
4498 gtk_combo_box_update_sensitivity (combo_box);
4500 g_object_notify (G_OBJECT (combo_box), "model");
4504 * gtk_combo_box_get_model:
4505 * @combo_box: A #GtkComboBox
4507 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4509 * Return value: (transfer none): A #GtkTreeModel which was passed
4510 * during construction.
4515 gtk_combo_box_get_model (GtkComboBox *combo_box)
4517 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4519 return combo_box->priv->model;
4523 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4524 GtkScrollType scroll)
4527 GtkTreeIter new_iter;
4528 gboolean active_iter;
4531 if (!combo_box->priv->model)
4533 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4537 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4541 case GTK_SCROLL_STEP_BACKWARD:
4542 case GTK_SCROLL_STEP_UP:
4543 case GTK_SCROLL_STEP_LEFT:
4546 found = tree_prev (combo_box, combo_box->priv->model,
4547 &iter, &new_iter, FALSE);
4550 /* else fall through */
4552 case GTK_SCROLL_PAGE_FORWARD:
4553 case GTK_SCROLL_PAGE_DOWN:
4554 case GTK_SCROLL_PAGE_RIGHT:
4555 case GTK_SCROLL_END:
4556 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4559 case GTK_SCROLL_STEP_FORWARD:
4560 case GTK_SCROLL_STEP_DOWN:
4561 case GTK_SCROLL_STEP_RIGHT:
4564 found = tree_next (combo_box, combo_box->priv->model,
4565 &iter, &new_iter, FALSE);
4568 /* else fall through */
4570 case GTK_SCROLL_PAGE_BACKWARD:
4571 case GTK_SCROLL_PAGE_UP:
4572 case GTK_SCROLL_PAGE_LEFT:
4573 case GTK_SCROLL_START:
4574 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4581 if (found && active_iter)
4583 GtkTreePath *old_path;
4584 GtkTreePath *new_path;
4586 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4587 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4589 if (gtk_tree_path_compare (old_path, new_path) == 0)
4592 gtk_tree_path_free (old_path);
4593 gtk_tree_path_free (new_path);
4598 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4602 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4607 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4608 gboolean group_cycling)
4610 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4612 if (combo_box->priv->has_entry)
4616 child = gtk_bin_get_child (GTK_BIN (combo_box));
4618 gtk_widget_grab_focus (child);
4621 gtk_widget_grab_focus (combo_box->priv->button);
4627 gtk_combo_box_grab_focus (GtkWidget *widget)
4629 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4631 if (combo_box->priv->has_entry)
4635 child = gtk_bin_get_child (GTK_BIN (combo_box));
4637 gtk_widget_grab_focus (child);
4640 gtk_widget_grab_focus (combo_box->priv->button);
4644 gtk_combo_box_destroy (GtkWidget *widget)
4646 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4648 if (combo_box->priv->popup_idle_id > 0)
4650 g_source_remove (combo_box->priv->popup_idle_id);
4651 combo_box->priv->popup_idle_id = 0;
4654 gtk_combo_box_popdown (combo_box);
4656 if (combo_box->priv->row_separator_destroy)
4657 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4659 combo_box->priv->row_separator_func = NULL;
4660 combo_box->priv->row_separator_data = NULL;
4661 combo_box->priv->row_separator_destroy = NULL;
4663 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4664 combo_box->priv->cell_view = NULL;
4668 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4671 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4674 * Fixes regression reported in bug #574059. The old functionality relied on
4675 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4676 * when the selection was already set to -1.
4678 if (gtk_combo_box_get_active(combo_box) == -1)
4679 g_signal_emit_by_name (combo_box, "changed");
4681 gtk_combo_box_set_active (combo_box, -1);
4685 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4688 GtkTreeModel *model;
4691 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4693 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4701 model = gtk_combo_box_get_model (combo_box);
4702 path = gtk_tree_model_get_path (model, &iter);
4703 path_str = gtk_tree_path_to_string (path);
4705 g_signal_handlers_block_by_func (entry,
4706 gtk_combo_box_entry_contents_changed,
4710 g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0,
4713 gtk_entry_set_text (entry, text);
4715 g_signal_handlers_unblock_by_func (entry,
4716 gtk_combo_box_entry_contents_changed,
4719 gtk_tree_path_free (path);
4727 gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
4730 GtkComboBoxPrivate *priv = combo_box->priv;
4731 GtkTreeModel *model;
4735 if (priv->text_column >= 0)
4737 model = gtk_combo_box_get_model (combo_box);
4738 gtk_tree_model_get_iter_from_string (model, &iter, path);
4740 gtk_tree_model_get (model, &iter,
4741 priv->text_column, &text,
4750 gtk_combo_box_constructor (GType type,
4751 guint n_construct_properties,
4752 GObjectConstructParam *construct_properties)
4755 GtkComboBox *combo_box;
4756 GtkComboBoxPrivate *priv;
4758 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4759 (type, n_construct_properties, construct_properties);
4761 combo_box = GTK_COMBO_BOX (object);
4762 priv = combo_box->priv;
4766 priv->area = gtk_cell_area_box_new ();
4767 g_object_ref_sink (priv->area);
4770 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4771 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4772 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4773 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4774 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4775 gtk_widget_show (priv->cell_view);
4777 gtk_combo_box_check_appearance (combo_box);
4779 if (priv->has_entry)
4782 GtkStyleContext *context;
4784 entry = gtk_entry_new ();
4785 gtk_widget_show (entry);
4786 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4788 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4789 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4791 priv->text_renderer = gtk_cell_renderer_text_new ();
4792 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4793 priv->text_renderer, TRUE);
4795 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4797 g_signal_connect (combo_box, "changed",
4798 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4806 gtk_combo_box_dispose(GObject* object)
4808 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4810 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4812 gtk_combo_box_menu_destroy (combo_box);
4813 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4814 combo_box->priv->popup_widget = NULL;
4817 if (combo_box->priv->area)
4819 g_object_unref (combo_box->priv->area);
4820 combo_box->priv->area = NULL;
4823 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4824 gtk_combo_box_list_destroy (combo_box);
4826 if (combo_box->priv->popup_window)
4828 gtk_widget_destroy (combo_box->priv->popup_window);
4829 combo_box->priv->popup_window = NULL;
4832 gtk_combo_box_unset_model (combo_box);
4834 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4838 gtk_combo_box_finalize (GObject *object)
4840 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4842 g_free (combo_box->priv->tearoff_title);
4844 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4848 gtk_cell_editable_key_press (GtkWidget *widget,
4852 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4854 if (event->keyval == GDK_KEY_Escape)
4856 g_object_set (combo_box,
4857 "editing-canceled", TRUE,
4859 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4860 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4864 else if (event->keyval == GDK_KEY_Return ||
4865 event->keyval == GDK_KEY_ISO_Enter ||
4866 event->keyval == GDK_KEY_KP_Enter)
4868 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4869 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4878 popdown_idle (gpointer data)
4880 GtkComboBox *combo_box;
4882 combo_box = GTK_COMBO_BOX (data);
4884 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4885 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4887 g_object_unref (combo_box);
4893 popdown_handler (GtkWidget *widget,
4896 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4900 popup_idle (gpointer data)
4902 GtkComboBox *combo_box;
4904 combo_box = GTK_COMBO_BOX (data);
4906 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4907 combo_box->priv->cell_view)
4908 g_signal_connect_object (combo_box->priv->popup_widget,
4909 "unmap", G_CALLBACK (popdown_handler),
4912 /* we unset this if a menu item is activated */
4913 g_object_set (combo_box,
4914 "editing-canceled", TRUE,
4916 gtk_combo_box_popup (combo_box);
4918 combo_box->priv->popup_idle_id = 0;
4919 combo_box->priv->activate_button = 0;
4920 combo_box->priv->activate_time = 0;
4926 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4929 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4932 combo_box->priv->is_cell_renderer = TRUE;
4934 if (combo_box->priv->cell_view)
4936 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4937 G_CALLBACK (gtk_cell_editable_key_press),
4940 gtk_widget_grab_focus (combo_box->priv->button);
4944 child = gtk_bin_get_child (GTK_BIN (combo_box));
4946 g_signal_connect_object (child, "key-press-event",
4947 G_CALLBACK (gtk_cell_editable_key_press),
4950 gtk_widget_grab_focus (child);
4951 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4954 /* we do the immediate popup only for the optionmenu-like
4957 if (combo_box->priv->is_cell_renderer &&
4958 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4960 if (event && event->type == GDK_BUTTON_PRESS)
4962 GdkEventButton *event_button = (GdkEventButton *)event;
4964 combo_box->priv->activate_button = event_button->button;
4965 combo_box->priv->activate_time = event_button->time;
4968 combo_box->priv->popup_idle_id =
4969 gdk_threads_add_idle (popup_idle, combo_box);
4975 * gtk_combo_box_get_add_tearoffs:
4976 * @combo_box: a #GtkComboBox
4978 * Gets the current value of the :add-tearoffs property.
4980 * Return value: the current value of the :add-tearoffs property.
4983 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4985 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4987 return combo_box->priv->add_tearoffs;
4991 * gtk_combo_box_set_add_tearoffs:
4992 * @combo_box: a #GtkComboBox
4993 * @add_tearoffs: %TRUE to add tearoff menu items
4995 * Sets whether the popup menu should have a tearoff
5001 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5002 gboolean add_tearoffs)
5004 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5006 add_tearoffs = add_tearoffs != FALSE;
5008 if (combo_box->priv->add_tearoffs != add_tearoffs)
5010 combo_box->priv->add_tearoffs = add_tearoffs;
5011 gtk_combo_box_check_appearance (combo_box);
5013 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5014 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
5015 combo_box->priv->add_tearoffs);
5017 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5022 * gtk_combo_box_get_title:
5023 * @combo_box: a #GtkComboBox
5025 * Gets the current title of the menu in tearoff mode. See
5026 * gtk_combo_box_set_add_tearoffs().
5028 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5029 * string which must not be freed.
5034 gtk_combo_box_get_title (GtkComboBox *combo_box)
5036 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5038 return combo_box->priv->tearoff_title;
5042 gtk_combo_box_update_title (GtkComboBox *combo_box)
5044 gtk_combo_box_check_appearance (combo_box);
5046 if (combo_box->priv->popup_widget &&
5047 GTK_IS_MENU (combo_box->priv->popup_widget))
5048 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5049 combo_box->priv->tearoff_title);
5053 * gtk_combo_box_set_title:
5054 * @combo_box: a #GtkComboBox
5055 * @title: a title for the menu in tearoff mode
5057 * Sets the menu's title in tearoff mode.
5062 gtk_combo_box_set_title (GtkComboBox *combo_box,
5065 GtkComboBoxPrivate *priv;
5067 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5069 priv = combo_box->priv;
5071 if (strcmp (title ? title : "",
5072 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5074 g_free (priv->tearoff_title);
5075 priv->tearoff_title = g_strdup (title);
5077 gtk_combo_box_update_title (combo_box);
5079 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5085 * gtk_combo_box_set_popup_fixed_width:
5086 * @combo_box: a #GtkComboBox
5087 * @fixed: whether to use a fixed popup width
5089 * Specifies whether the popup's width should be a fixed width
5090 * matching the allocated width of the combo box.
5095 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5098 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5100 if (combo_box->priv->popup_fixed_width != fixed)
5102 combo_box->priv->popup_fixed_width = fixed;
5104 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5109 * gtk_combo_box_get_popup_fixed_width:
5110 * @combo_box: a #GtkComboBox
5112 * Gets whether the popup uses a fixed width matching
5113 * the allocated width of the combo box.
5115 * Returns: %TRUE if the popup uses a fixed width
5120 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5122 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5124 return combo_box->priv->popup_fixed_width;
5129 * gtk_combo_box_get_popup_accessible:
5130 * @combo_box: a #GtkComboBox
5132 * Gets the accessible object corresponding to the combo box's popup.
5134 * This function is mostly intended for use by accessibility technologies;
5135 * applications should have little use for it.
5137 * Returns: (transfer none): the accessible object corresponding
5138 * to the combo box's popup.
5143 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5147 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5149 if (combo_box->priv->popup_widget)
5151 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5159 * gtk_combo_box_get_row_separator_func: (skip)
5160 * @combo_box: a #GtkComboBox
5162 * Returns the current row separator function.
5164 * Return value: the current row separator function.
5168 GtkTreeViewRowSeparatorFunc
5169 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5171 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5173 return combo_box->priv->row_separator_func;
5177 * gtk_combo_box_set_row_separator_func:
5178 * @combo_box: a #GtkComboBox
5179 * @func: a #GtkTreeViewRowSeparatorFunc
5180 * @data: (allow-none): user data to pass to @func, or %NULL
5181 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5183 * Sets the row separator function, which is used to determine
5184 * whether a row should be drawn as a separator. If the row separator
5185 * function is %NULL, no separators are drawn. This is the default value.
5190 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5191 GtkTreeViewRowSeparatorFunc func,
5193 GDestroyNotify destroy)
5195 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5197 if (combo_box->priv->row_separator_destroy)
5198 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5200 combo_box->priv->row_separator_func = func;
5201 combo_box->priv->row_separator_data = data;
5202 combo_box->priv->row_separator_destroy = destroy;
5204 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5205 if (combo_box->priv->tree_view)
5207 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5208 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5211 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5213 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5214 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5217 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5221 * gtk_combo_box_set_button_sensitivity:
5222 * @combo_box: a #GtkComboBox
5223 * @sensitivity: specify the sensitivity of the dropdown button
5225 * Sets whether the dropdown button of the combo box should be
5226 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5227 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5232 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5233 GtkSensitivityType sensitivity)
5235 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5237 if (combo_box->priv->button_sensitivity != sensitivity)
5239 combo_box->priv->button_sensitivity = sensitivity;
5240 gtk_combo_box_update_sensitivity (combo_box);
5242 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5247 * gtk_combo_box_get_button_sensitivity:
5248 * @combo_box: a #GtkComboBox
5250 * Returns whether the combo box sets the dropdown button
5251 * sensitive or not when there are no items in the model.
5253 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5254 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5255 * if the button is always insensitive or
5256 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5257 * the model has one item to be selected.
5262 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5264 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5266 return combo_box->priv->button_sensitivity;
5271 * gtk_combo_box_get_has_entry:
5272 * @combo_box: a #GtkComboBox
5274 * Returns whether the combo box has an entry.
5276 * Return Value: whether there is an entry in @combo_box.
5281 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5283 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5285 return combo_box->priv->has_entry;
5289 * gtk_combo_box_set_entry_text_column:
5290 * @combo_box: A #GtkComboBox
5291 * @text_column: A column in @model to get the strings from for
5292 * the internal entry
5294 * Sets the model column which @combo_box should use to get strings from
5295 * to be @text_column. The column @text_column in the model of @combo_box
5296 * must be of type %G_TYPE_STRING.
5298 * This is only relevant if @combo_box has been created with
5299 * #GtkComboBox:has-entry as %TRUE.
5304 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5307 GtkComboBoxPrivate *priv = combo_box->priv;
5308 GtkTreeModel *model;
5310 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5312 model = gtk_combo_box_get_model (combo_box);
5314 g_return_if_fail (text_column >= 0);
5315 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5317 priv->text_column = text_column;
5319 if (priv->text_renderer != NULL)
5320 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5321 priv->text_renderer,
5322 "text", text_column,
5327 * gtk_combo_box_get_entry_text_column:
5328 * @combo_box: A #GtkComboBox.
5330 * Returns the column which @combo_box is using to get the strings
5331 * from to display in the internal entry.
5333 * Return value: A column in the data source model of @combo_box.
5338 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5340 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5342 return combo_box->priv->text_column;
5346 * gtk_combo_box_set_focus_on_click:
5347 * @combo: a #GtkComboBox
5348 * @focus_on_click: whether the combo box grabs focus when clicked
5351 * Sets whether the combo box will grab focus when it is clicked with
5352 * the mouse. Making mouse clicks not grab focus is useful in places
5353 * like toolbars where you don't want the keyboard focus removed from
5354 * the main area of the application.
5359 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5360 gboolean focus_on_click)
5362 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5364 focus_on_click = focus_on_click != FALSE;
5366 if (combo_box->priv->focus_on_click != focus_on_click)
5368 combo_box->priv->focus_on_click = focus_on_click;
5370 if (combo_box->priv->button)
5371 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5374 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5379 * gtk_combo_box_get_focus_on_click:
5380 * @combo: a #GtkComboBox
5382 * Returns whether the combo box grabs focus when it is clicked
5383 * with the mouse. See gtk_combo_box_set_focus_on_click().
5385 * Return value: %TRUE if the combo box grabs focus when it is
5386 * clicked with the mouse.
5391 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5393 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5395 return combo_box->priv->focus_on_click;
5400 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5401 GtkBuilder *builder,
5403 const gchar *tagname,
5404 GMarkupParser *parser,
5407 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5408 tagname, parser, data))
5411 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5412 tagname, parser, data);
5416 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5417 GtkBuilder *builder,
5419 const gchar *tagname,
5422 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5423 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5427 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5428 GtkBuilder *builder,
5429 const gchar *childname)
5431 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5433 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5434 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5436 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5440 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5444 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5445 GtkComboBoxPrivate *priv = combo_box->priv;
5446 gint font_size, arrow_size;
5447 PangoContext *context;
5448 PangoFontMetrics *metrics;
5450 gint minimum_width = 0, natural_width = 0;
5451 gint child_min, child_nat;
5453 gfloat arrow_scaling;
5455 child = gtk_bin_get_child (GTK_BIN (widget));
5458 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5460 gtk_widget_style_get (GTK_WIDGET (widget),
5461 "arrow-size", &arrow_size,
5462 "arrow-scaling", &arrow_scaling,
5465 get_widget_padding_and_border (widget, &padding);
5467 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5468 metrics = pango_context_get_metrics (context,
5469 pango_context_get_font_description (context),
5470 pango_context_get_language (context));
5471 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5472 pango_font_metrics_get_descent (metrics));
5473 pango_font_metrics_unref (metrics);
5475 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5477 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5479 if (!priv->tree_view)
5482 if (priv->cell_view)
5484 gint box_width, xpad;
5485 GtkBorder button_padding;
5487 get_widget_padding_and_border (priv->button, &button_padding);
5489 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5490 xpad = button_padding.left + button_padding.right + padding.left + padding.right;
5492 minimum_width = child_min + box_width + xpad;
5493 natural_width = child_nat + box_width + xpad;
5497 gint but_width, but_nat_width;
5499 gtk_widget_get_preferred_width (priv->button,
5500 &but_width, &but_nat_width);
5502 minimum_width = child_min + but_width;
5503 natural_width = child_nat + but_nat_width;
5509 gint button_width, button_nat_width;
5511 /* sample + frame */
5512 minimum_width = child_min;
5513 natural_width = child_nat;
5515 if (priv->cell_view_frame)
5517 if (priv->has_frame)
5519 gint border_width, xpad;
5520 GtkBorder frame_padding;
5522 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5523 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5524 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5526 minimum_width += xpad;
5527 natural_width += xpad;
5532 gtk_widget_get_preferred_width (priv->button,
5533 &button_width, &button_nat_width);
5535 minimum_width += button_width;
5536 natural_width += button_nat_width;
5539 minimum_width += padding.left + padding.right;
5540 natural_width += padding.left + padding.right;
5543 *minimum_size = minimum_width;
5546 *natural_size = natural_width;
5550 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5556 /* Combo box is height-for-width only
5557 * (so we always just reserve enough height for the minimum width) */
5558 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5559 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5563 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5568 /* Combo box is height-for-width only
5569 * (so we assume we always reserved enough height for the minimum width) */
5570 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5575 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5580 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5581 GtkComboBoxPrivate *priv = combo_box->priv;
5582 gint min_height = 0, nat_height = 0;
5587 child = gtk_bin_get_child (GTK_BIN (widget));
5589 get_widget_padding_and_border (widget, &padding);
5592 if (!priv->tree_view)
5595 if (priv->cell_view)
5597 /* calculate x/y padding and separator/arrow size */
5598 gint box_width, box_height;
5600 GtkBorder button_padding;
5602 get_widget_padding_and_border (priv->button, &button_padding);
5604 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5605 gtk_widget_get_preferred_height_for_width (priv->box,
5606 box_width, &box_height, NULL);
5608 xpad = button_padding.left + button_padding.right;
5609 ypad = button_padding.top + button_padding.bottom;
5611 size -= box_width + xpad;
5613 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5614 * and fitting the whole treemodel */
5615 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5617 min_height = MAX (min_height, box_height);
5618 nat_height = MAX (nat_height, box_height);
5625 /* there is a custom child widget inside (no priv->cell_view) */
5626 gint but_width, but_height;
5628 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5629 gtk_widget_get_preferred_height_for_width (priv->button,
5630 but_width, &but_height, NULL);
5634 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5635 * and fitting the whole treemodel */
5636 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5638 min_height = MAX (min_height, but_height);
5639 nat_height = MAX (nat_height, but_height);
5645 gint but_width, but_height;
5646 gint xpad = 0, ypad = 0;
5648 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5649 gtk_widget_get_preferred_height_for_width (priv->button,
5650 but_width, &but_height, NULL);
5652 if (priv->cell_view_frame && priv->has_frame)
5654 GtkBorder frame_padding;
5657 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5658 get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5660 xpad = (2 * border_width) + padding.left + frame_padding.right;
5661 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5667 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5668 * and fitting the whole treemodel */
5669 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5671 min_height = MAX (min_height, but_height);
5672 nat_height = MAX (nat_height, but_height);
5678 min_height += padding.top + padding.bottom;
5679 nat_height += padding.top + padding.bottom;
5682 *minimum_size = min_height;
5685 *natural_size = nat_height;
5689 * gtk_combo_box_set_id_column:
5690 * @combo_box: A #GtkComboBox
5691 * @id_column: A column in @model to get string IDs for values from
5693 * Sets the model column which @combo_box should use to get string IDs
5694 * for values from. The column @id_column in the model of @combo_box
5695 * must be of type %G_TYPE_STRING.
5700 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5703 GtkComboBoxPrivate *priv = combo_box->priv;
5704 GtkTreeModel *model;
5706 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5708 if (id_column != priv->id_column)
5710 model = gtk_combo_box_get_model (combo_box);
5712 g_return_if_fail (id_column >= 0);
5713 g_return_if_fail (model == NULL ||
5714 id_column < gtk_tree_model_get_n_columns (model));
5716 priv->id_column = id_column;
5718 g_object_notify (G_OBJECT (combo_box), "id-column");
5719 g_object_notify (G_OBJECT (combo_box), "active-id");
5724 * gtk_combo_box_get_id_column:
5725 * @combo_box: A #GtkComboBox
5727 * Returns the column which @combo_box is using to get string IDs
5730 * Return value: A column in the data source model of @combo_box.
5735 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5737 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5739 return combo_box->priv->id_column;
5743 * gtk_combo_box_get_active_id:
5744 * @combo_box: a #GtkComboBox
5746 * Returns the ID of the active row of @combo_box. This value is taken
5747 * from the active row and the column specified by the #GtkComboBox:id-column
5748 * property of @combo_box (see gtk_combo_box_set_id_column()).
5750 * The returned value is an interned string which means that you can
5751 * compare the pointer by value to other interned strings and that you
5754 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5755 * no row is active, or if the active row has a %NULL ID value, then %NULL
5758 * Return value: the ID of the active row, or %NULL
5763 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5765 GtkTreeModel *model;
5769 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5771 column = combo_box->priv->id_column;
5776 model = gtk_combo_box_get_model (combo_box);
5777 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5778 G_TYPE_STRING, NULL);
5780 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5782 const gchar *interned;
5785 gtk_tree_model_get (model, &iter, column, &id, -1);
5786 interned = g_intern_string (id);
5796 * gtk_combo_box_set_active_id:
5797 * @combo_box: a #GtkComboBox
5798 * @active_id: (allow-none): the ID of the row to select, or %NULL
5800 * Changes the active row of @combo_box to the one that has an ID equal to
5801 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5802 * a %NULL ID string cannot be made active by this function.
5804 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5805 * row has the given ID then the function does nothing and returns %FALSE.
5807 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5808 * @active_id was given to unset the active row, the function
5809 * always returns %TRUE.
5814 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5815 const gchar *active_id)
5817 GtkTreeModel *model;
5819 gboolean match = FALSE;
5822 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5824 if (active_id == NULL)
5826 gtk_combo_box_set_active (combo_box, -1);
5827 return TRUE; /* active row was successfully unset */
5830 column = combo_box->priv->id_column;
5835 model = gtk_combo_box_get_model (combo_box);
5836 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5837 G_TYPE_STRING, FALSE);
5839 if (gtk_tree_model_get_iter_first (model, &iter))
5843 gtk_tree_model_get (model, &iter, column, &id, -1);
5845 match = strcmp (id, active_id) == 0;
5850 gtk_combo_box_set_active_iter (combo_box, &iter);
5853 } while (gtk_tree_model_iter_next (model, &iter));