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_grab_broken_event (GtkWidget *widget,
2336 GdkEventGrabBroken *event,
2339 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2341 if (event->grab_window == NULL)
2342 gtk_combo_box_popdown (combo_box);
2348 * gtk_combo_box_popup:
2349 * @combo_box: a #GtkComboBox
2351 * Pops up the menu or dropdown list of @combo_box.
2353 * This function is mostly intended for use by accessibility technologies;
2354 * applications should have little use for it.
2359 gtk_combo_box_popup (GtkComboBox *combo_box)
2361 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2363 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2367 * gtk_combo_box_popup_for_device:
2368 * @combo_box: a #GtkComboBox
2369 * @device: a #GdkDevice
2371 * Pops up the menu or dropdown list of @combo_box, the popup window
2372 * will be grabbed so only @device and its associated pointer/keyboard
2373 * are the only #GdkDevice<!-- -->s able to send events to it.
2378 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2381 GtkComboBoxPrivate *priv = combo_box->priv;
2382 gint x, y, width, height;
2383 GtkTreePath *path = NULL, *ppath;
2384 GtkWidget *toplevel;
2385 GdkDevice *keyboard, *pointer;
2388 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2389 g_return_if_fail (GDK_IS_DEVICE (device));
2391 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2394 if (gtk_widget_get_mapped (priv->popup_widget))
2397 if (priv->grab_pointer && priv->grab_keyboard)
2400 time = gtk_get_current_event_time ();
2402 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2405 pointer = gdk_device_get_associated_device (device);
2410 keyboard = gdk_device_get_associated_device (device);
2413 if (GTK_IS_MENU (priv->popup_widget))
2415 gtk_combo_box_menu_popup (combo_box,
2416 priv->activate_button,
2417 priv->activate_time);
2421 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2422 if (GTK_IS_WINDOW (toplevel))
2423 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2424 GTK_WINDOW (priv->popup_window));
2426 gtk_widget_show_all (priv->scrolled_window);
2427 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2429 gtk_widget_set_size_request (priv->popup_window, width, height);
2430 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2432 if (gtk_tree_row_reference_valid (priv->active_row))
2434 path = gtk_tree_row_reference_get_path (priv->active_row);
2435 ppath = gtk_tree_path_copy (path);
2436 if (gtk_tree_path_up (ppath))
2437 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2439 gtk_tree_path_free (ppath);
2441 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2445 gtk_widget_show (priv->popup_window);
2449 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2451 gtk_tree_path_free (path);
2454 gtk_widget_grab_focus (priv->popup_window);
2455 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2458 if (!gtk_widget_has_focus (priv->tree_view))
2459 gtk_widget_grab_focus (priv->tree_view);
2461 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2462 keyboard, pointer, time))
2464 gtk_widget_hide (priv->popup_window);
2468 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2469 priv->grab_pointer = pointer;
2470 priv->grab_keyboard = keyboard;
2472 g_signal_connect (priv->popup_window,
2473 "grab-broken-event",
2474 G_CALLBACK (gtk_combo_box_grab_broken_event),
2479 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2483 device = gtk_get_current_event_device ();
2487 GdkDeviceManager *device_manager;
2488 GdkDisplay *display;
2491 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2492 device_manager = gdk_display_get_device_manager (display);
2494 /* No device was set, pick the first master device */
2495 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2496 device = devices->data;
2497 g_list_free (devices);
2500 gtk_combo_box_popup_for_device (combo_box, device);
2504 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2506 if (combo_box->priv->popup_shown)
2508 gtk_combo_box_popdown (combo_box);
2516 * gtk_combo_box_popdown:
2517 * @combo_box: a #GtkComboBox
2519 * Hides the menu or dropdown list of @combo_box.
2521 * This function is mostly intended for use by accessibility technologies;
2522 * applications should have little use for it.
2527 gtk_combo_box_popdown (GtkComboBox *combo_box)
2529 GtkComboBoxPrivate *priv = combo_box->priv;
2531 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2533 if (GTK_IS_MENU (priv->popup_widget))
2535 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2539 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2542 if (priv->grab_keyboard)
2543 gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME);
2544 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
2546 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2547 gtk_widget_hide (priv->popup_window);
2548 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2551 priv->grab_pointer = NULL;
2552 priv->grab_keyboard = NULL;
2555 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2556 GtkAllocation button_allocation; \
2557 gtk_widget_get_preferred_size (combo_box->priv->button, \
2561 button_allocation.x = allocation->x; \
2563 button_allocation.x = allocation->x + allocation->width \
2566 button_allocation.y = allocation->y; \
2567 button_allocation.width = MAX (1, req.width); \
2568 button_allocation.height = allocation->height; \
2569 button_allocation.height = MAX (1, button_allocation.height); \
2571 gtk_widget_size_allocate (combo_box->priv->button, \
2572 &button_allocation);
2576 gtk_combo_box_size_allocate (GtkWidget *widget,
2577 GtkAllocation *allocation)
2579 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2580 GtkComboBoxPrivate *priv = combo_box->priv;
2581 GtkWidget *child_widget;
2582 GtkAllocation child;
2584 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2587 gtk_widget_set_allocation (widget, allocation);
2588 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2589 get_widget_padding_and_border (widget, &padding);
2591 allocation->x += padding.left;
2592 allocation->y += padding.top;
2593 allocation->width -= padding.left + padding.right;
2594 allocation->height -= padding.top + padding.bottom;
2596 if (!priv->tree_view)
2598 if (priv->cell_view)
2600 GtkBorder button_padding;
2604 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2605 get_widget_padding_and_border (priv->button, &button_padding);
2607 /* menu mode; child_widget is priv->cell_view.
2608 * Allocate the button to the full combobox allocation (minus the
2611 gtk_widget_size_allocate (priv->button, allocation);
2613 child.x = allocation->x;
2614 child.y = allocation->y;
2615 width = allocation->width;
2616 child.height = allocation->height;
2618 if (!priv->is_cell_renderer)
2620 /* restrict allocation of the child into the button box
2621 * if we're not in cell renderer mode.
2623 child.x += border_width + button_padding.left;
2624 child.y += border_width + button_padding.top;
2625 width -= 2 * border_width +
2626 button_padding.left + button_padding.right;
2627 child.height -= 2 * border_width +
2628 button_padding.top + button_padding.bottom;
2631 /* allocate the box containing the separator and the arrow */
2632 gtk_widget_get_preferred_size (priv->box, &req, NULL);
2633 child.width = req.width;
2635 child.x += width - req.width;
2636 child.width = MAX (1, child.width);
2637 child.height = MAX (1, child.height);
2638 gtk_widget_size_allocate (priv->box, &child);
2642 child.x += req.width;
2643 child.width = allocation->x + allocation->width
2644 - border_width - child.x - button_padding.right;
2648 child.width = child.x;
2649 child.x = allocation->x
2650 + border_width + button_padding.left;
2651 child.width -= child.x;
2654 if (gtk_widget_get_visible (priv->popup_widget))
2656 gint width, menu_width;
2658 if (priv->wrap_width == 0)
2660 GtkAllocation combo_box_allocation;
2662 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2663 width = combo_box_allocation.width;
2664 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2666 if (combo_box->priv->popup_fixed_width)
2667 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2669 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2671 gtk_widget_set_size_request (priv->popup_widget,
2672 MAX (width, menu_width), -1);
2675 /* reposition the menu after giving it a new width */
2676 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2679 child.width = MAX (1, child.width);
2680 child.height = MAX (1, child.height);
2681 gtk_widget_size_allocate (child_widget, &child);
2685 /* menu mode; child_widget has been set with gtk_container_add().
2686 * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2687 * Allocate the button at the far end, according to the direction
2690 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2692 /* After the macro, button_allocation has the button allocation rect */
2695 child.x = button_allocation.x + button_allocation.width;
2697 child.x = allocation->x;
2699 child.y = allocation->y;
2700 child.width = allocation->width - button_allocation.width;
2701 child.height = button_allocation.height;
2703 child.width = MAX (1, child.width);
2705 gtk_widget_size_allocate (child_widget, &child);
2710 /* list mode; child_widget might be either priv->cell_view or a child
2711 * added with gtk_container_add().
2714 /* After the macro, button_allocation has the button allocation rect */
2715 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2718 child.x = button_allocation.x + button_allocation.width;
2720 child.x = allocation->x;
2722 child.y = allocation->y;
2723 child.width = allocation->width - button_allocation.width;
2724 child.height = button_allocation.height;
2726 if (priv->cell_view_frame)
2728 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2730 /* restrict allocation of the child into the frame box if it's present */
2731 if (priv->has_frame)
2733 GtkBorder frame_padding;
2736 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2737 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2739 child.x += border_width + frame_padding.left;
2740 child.y += border_width + frame_padding.right;
2741 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2742 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2746 if (gtk_widget_get_visible (priv->popup_window))
2748 gint x, y, width, height;
2749 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2750 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2751 gtk_widget_set_size_request (priv->popup_window, width, height);
2754 /* allocate the child */
2755 child.width = MAX (1, child.width);
2756 child.height = MAX (1, child.height);
2757 gtk_widget_size_allocate (child_widget, &child);
2761 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2764 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2766 GtkComboBoxPrivate *priv = combo_box->priv;
2770 g_signal_handler_disconnect (priv->model,
2772 g_signal_handler_disconnect (priv->model,
2774 g_signal_handler_disconnect (priv->model,
2775 priv->reordered_id);
2776 g_signal_handler_disconnect (priv->model,
2782 g_object_unref (priv->model);
2786 if (priv->active_row)
2788 gtk_tree_row_reference_free (priv->active_row);
2789 priv->active_row = NULL;
2792 if (priv->cell_view)
2793 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2797 gtk_combo_box_forall (GtkContainer *container,
2798 gboolean include_internals,
2799 GtkCallback callback,
2800 gpointer callback_data)
2802 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2803 GtkComboBoxPrivate *priv = combo_box->priv;
2806 if (include_internals)
2809 (* callback) (priv->button, callback_data);
2810 if (priv->cell_view_frame)
2811 (* callback) (priv->cell_view_frame, callback_data);
2814 child = gtk_bin_get_child (GTK_BIN (container));
2816 (* callback) (child, callback_data);
2820 gtk_combo_box_child_show (GtkWidget *widget,
2821 GtkComboBox *combo_box)
2823 GtkComboBoxPrivate *priv = combo_box->priv;
2825 priv->popup_shown = TRUE;
2826 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2830 gtk_combo_box_child_hide (GtkWidget *widget,
2831 GtkComboBox *combo_box)
2833 GtkComboBoxPrivate *priv = combo_box->priv;
2835 priv->popup_shown = FALSE;
2836 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2840 gtk_combo_box_draw (GtkWidget *widget,
2843 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2844 GtkComboBoxPrivate *priv = combo_box->priv;
2846 if (priv->shadow_type != GTK_SHADOW_NONE)
2848 GtkStyleContext *context;
2850 context = gtk_widget_get_style_context (widget);
2852 gtk_render_background (context, cr, 0, 0,
2853 gtk_widget_get_allocated_width (widget),
2854 gtk_widget_get_allocated_height (widget));
2855 gtk_render_frame (context, cr, 0, 0,
2856 gtk_widget_get_allocated_width (widget),
2857 gtk_widget_get_allocated_height (widget));
2860 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2863 if (priv->tree_view && priv->cell_view_frame)
2865 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2866 priv->cell_view_frame, cr);
2869 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2870 gtk_bin_get_child (GTK_BIN (widget)),
2886 path_visible (GtkTreeView *view,
2892 /* Note that we rely on the fact that collapsed rows don't have nodes
2894 return _gtk_tree_view_find_node (view, path, &tree, &node);
2898 tree_next_func (GtkTreeModel *model,
2903 SearchData *search_data = (SearchData *)data;
2905 if (search_data->found)
2907 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2910 if (search_data->visible &&
2911 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2914 search_data->set = TRUE;
2915 search_data->iter = *iter;
2920 if (gtk_tree_path_compare (path, search_data->path) == 0)
2921 search_data->found = TRUE;
2927 tree_next (GtkComboBox *combo,
2928 GtkTreeModel *model,
2933 SearchData search_data;
2935 search_data.combo = combo;
2936 search_data.path = gtk_tree_model_get_path (model, iter);
2937 search_data.visible = visible;
2938 search_data.found = FALSE;
2939 search_data.set = FALSE;
2941 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2943 *next = search_data.iter;
2945 gtk_tree_path_free (search_data.path);
2947 return search_data.set;
2951 tree_prev_func (GtkTreeModel *model,
2956 SearchData *search_data = (SearchData *)data;
2958 if (gtk_tree_path_compare (path, search_data->path) == 0)
2960 search_data->found = TRUE;
2964 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2967 if (search_data->visible &&
2968 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2971 search_data->set = TRUE;
2972 search_data->iter = *iter;
2978 tree_prev (GtkComboBox *combo,
2979 GtkTreeModel *model,
2984 SearchData search_data;
2986 search_data.combo = combo;
2987 search_data.path = gtk_tree_model_get_path (model, iter);
2988 search_data.visible = visible;
2989 search_data.found = FALSE;
2990 search_data.set = FALSE;
2992 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2994 *prev = search_data.iter;
2996 gtk_tree_path_free (search_data.path);
2998 return search_data.set;
3002 tree_last_func (GtkTreeModel *model,
3007 SearchData *search_data = (SearchData *)data;
3009 if (!tree_column_row_is_sensitive (search_data->combo, iter))
3012 /* Note that we rely on the fact that collapsed rows don't have nodes
3014 if (search_data->visible &&
3015 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3018 search_data->set = TRUE;
3019 search_data->iter = *iter;
3025 tree_last (GtkComboBox *combo,
3026 GtkTreeModel *model,
3030 SearchData search_data;
3032 search_data.combo = combo;
3033 search_data.visible = visible;
3034 search_data.set = FALSE;
3036 gtk_tree_model_foreach (model, tree_last_func, &search_data);
3038 *last = search_data.iter;
3040 return search_data.set;
3045 tree_first_func (GtkTreeModel *model,
3050 SearchData *search_data = (SearchData *)data;
3052 if (!tree_column_row_is_sensitive (search_data->combo, iter))
3055 if (search_data->visible &&
3056 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3059 search_data->set = TRUE;
3060 search_data->iter = *iter;
3066 tree_first (GtkComboBox *combo,
3067 GtkTreeModel *model,
3071 SearchData search_data;
3073 search_data.combo = combo;
3074 search_data.visible = visible;
3075 search_data.set = FALSE;
3077 gtk_tree_model_foreach (model, tree_first_func, &search_data);
3079 *first = search_data.iter;
3081 return search_data.set;
3085 gtk_combo_box_scroll_event (GtkWidget *widget,
3086 GdkEventScroll *event)
3088 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3091 GtkTreeIter new_iter;
3093 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3096 if (event->direction == GDK_SCROLL_UP)
3097 found = tree_prev (combo_box, combo_box->priv->model,
3098 &iter, &new_iter, FALSE);
3100 found = tree_next (combo_box, combo_box->priv->model,
3101 &iter, &new_iter, FALSE);
3104 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3113 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3117 GtkComboBoxPrivate *priv = combo->priv;
3119 if (priv->row_separator_func)
3120 return priv->row_separator_func (model, iter, priv->row_separator_data);
3126 gtk_combo_box_header_func (GtkTreeModel *model,
3130 /* Every submenu has a selectable header, however we
3131 * can expose a method to make that configurable by
3132 * the user (like row_separator_func is done) */
3137 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3138 gboolean add_children)
3140 GtkComboBoxPrivate *priv = combo_box->priv;
3144 child = gtk_bin_get_child (GTK_BIN (combo_box));
3146 if (priv->cell_view)
3148 priv->button = gtk_toggle_button_new ();
3149 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3150 priv->focus_on_click);
3152 g_signal_connect (priv->button, "toggled",
3153 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3154 gtk_widget_set_parent (priv->button,
3155 gtk_widget_get_parent (child));
3157 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3158 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3160 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3161 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3163 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3164 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3165 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3167 gtk_widget_show_all (priv->button);
3171 priv->button = gtk_toggle_button_new ();
3172 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3173 priv->focus_on_click);
3175 g_signal_connect (priv->button, "toggled",
3176 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3177 gtk_widget_set_parent (priv->button,
3178 gtk_widget_get_parent (child));
3180 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3181 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3182 gtk_widget_add_events (priv->button, GDK_SCROLL_MASK);
3183 gtk_widget_show_all (priv->button);
3186 g_signal_connect (priv->button, "button-press-event",
3187 G_CALLBACK (gtk_combo_box_menu_button_press),
3189 g_signal_connect (priv->button, "state-flags-changed",
3190 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3193 /* create our funky menu */
3194 menu = _gtk_tree_menu_new_with_area (priv->area);
3195 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3197 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3199 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3200 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3201 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3202 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3203 combo_box->priv->add_tearoffs);
3205 g_signal_connect (menu, "menu-activate",
3206 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3208 /* Chain our row_separator_func through */
3209 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3210 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3213 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3214 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3217 g_signal_connect (menu, "key-press-event",
3218 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3219 gtk_combo_box_set_popup_widget (combo_box, menu);
3221 gtk_combo_box_update_title (combo_box);
3225 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3227 GtkComboBoxPrivate *priv = combo_box->priv;
3229 g_signal_handlers_disconnect_matched (priv->button,
3230 G_SIGNAL_MATCH_DATA,
3232 gtk_combo_box_menu_button_press, NULL);
3233 g_signal_handlers_disconnect_matched (priv->button,
3234 G_SIGNAL_MATCH_DATA,
3236 gtk_combo_box_button_state_flags_changed, combo_box);
3237 g_signal_handlers_disconnect_matched (priv->popup_widget,
3238 G_SIGNAL_MATCH_DATA,
3240 gtk_combo_box_menu_activate, combo_box);
3242 /* unparent will remove our latest ref */
3243 gtk_widget_unparent (priv->button);
3246 priv->button = NULL;
3248 priv->separator = NULL;
3250 /* changing the popup window will unref the menu and the children */
3255 gtk_combo_box_menu_button_press (GtkWidget *widget,
3256 GdkEventButton *event,
3259 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3260 GtkComboBoxPrivate *priv = combo_box->priv;
3262 if (GTK_IS_MENU (priv->popup_widget) &&
3263 event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3265 if (priv->focus_on_click &&
3266 !gtk_widget_has_focus (priv->button))
3267 gtk_widget_grab_focus (priv->button);
3269 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3278 gtk_combo_box_menu_activate (GtkWidget *menu,
3280 GtkComboBox *combo_box)
3284 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3285 gtk_combo_box_set_active_iter (combo_box, &iter);
3287 g_object_set (combo_box,
3288 "editing-canceled", FALSE,
3294 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3297 gboolean sensitive = TRUE; /* fool code checkers */
3299 if (!combo_box->priv->button)
3302 switch (combo_box->priv->button_sensitivity)
3304 case GTK_SENSITIVITY_ON:
3307 case GTK_SENSITIVITY_OFF:
3310 case GTK_SENSITIVITY_AUTO:
3311 sensitive = combo_box->priv->model &&
3312 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3315 g_assert_not_reached ();
3319 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3321 /* In list-mode, we also need to update sensitivity of the event box */
3322 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3323 && combo_box->priv->cell_view)
3324 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3328 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3333 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3335 if (combo_box->priv->tree_view)
3336 gtk_combo_box_list_popup_resize (combo_box);
3338 gtk_combo_box_update_sensitivity (combo_box);
3342 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3346 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3347 GtkComboBoxPrivate *priv = combo_box->priv;
3349 if (!gtk_tree_row_reference_valid (priv->active_row))
3351 if (priv->cell_view)
3352 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3353 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3356 if (priv->tree_view)
3357 gtk_combo_box_list_popup_resize (combo_box);
3359 gtk_combo_box_update_sensitivity (combo_box);
3363 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3369 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3373 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3378 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3379 GtkComboBoxPrivate *priv = combo_box->priv;
3380 GtkTreePath *active_path;
3382 /* FIXME this belongs to GtkCellView */
3383 if (gtk_tree_row_reference_valid (priv->active_row))
3385 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3386 if (gtk_tree_path_compare (path, active_path) == 0 &&
3388 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3389 gtk_tree_path_free (active_path);
3392 if (priv->tree_view)
3393 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3397 list_popup_resize_idle (gpointer user_data)
3399 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3400 GtkComboBoxPrivate *priv = combo_box->priv;
3401 gint x, y, width, height;
3403 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3405 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3407 gtk_widget_set_size_request (priv->popup_window, width, height);
3408 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3411 priv->resize_idle_id = 0;
3417 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3419 GtkComboBoxPrivate *priv = combo_box->priv;
3421 if (!priv->resize_idle_id)
3422 priv->resize_idle_id =
3423 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3427 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3432 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3434 gtk_combo_box_list_popup_resize (combo_box);
3443 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3445 GtkComboBoxPrivate *priv = combo_box->priv;
3446 GtkTreeSelection *sel;
3448 GtkWidget *widget = GTK_WIDGET (combo_box);
3450 priv->button = gtk_toggle_button_new ();
3451 child = gtk_bin_get_child (GTK_BIN (combo_box));
3452 gtk_widget_set_parent (priv->button,
3453 gtk_widget_get_parent (child));
3454 g_signal_connect (priv->button, "button-press-event",
3455 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3456 g_signal_connect (priv->button, "toggled",
3457 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3459 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3460 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3461 priv->separator = NULL;
3462 gtk_widget_show_all (priv->button);
3464 if (priv->cell_view)
3466 GtkStyleContext *context;
3467 GtkStateFlags state;
3470 context = gtk_widget_get_style_context (widget);
3471 state = gtk_widget_get_state_flags (widget);
3472 gtk_style_context_get_background_color (context, state, &color);
3474 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3476 priv->box = gtk_event_box_new ();
3477 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3480 if (priv->has_frame)
3482 priv->cell_view_frame = gtk_frame_new (NULL);
3483 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3488 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3489 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3493 gtk_widget_set_parent (priv->cell_view_frame,
3494 gtk_widget_get_parent (child));
3495 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3496 gtk_widget_show_all (priv->cell_view_frame);
3498 g_signal_connect (priv->box, "button-press-event",
3499 G_CALLBACK (gtk_combo_box_list_button_pressed),
3503 priv->tree_view = gtk_tree_view_new ();
3504 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3505 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3506 gtk_tree_selection_set_select_function (sel,
3507 gtk_combo_box_list_select_func,
3509 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3511 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3514 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3515 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3519 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3521 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3522 gtk_tree_view_column_new_with_area (priv->area));
3524 if (gtk_tree_row_reference_valid (priv->active_row))
3528 path = gtk_tree_row_reference_get_path (priv->active_row);
3529 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3531 gtk_tree_path_free (path);
3534 /* set sample/popup widgets */
3535 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3537 g_signal_connect (priv->tree_view, "key-press-event",
3538 G_CALLBACK (gtk_combo_box_list_key_press),
3540 g_signal_connect (priv->tree_view, "enter-notify-event",
3541 G_CALLBACK (gtk_combo_box_list_enter_notify),
3543 g_signal_connect (priv->tree_view, "row-expanded",
3544 G_CALLBACK (gtk_combo_box_model_row_expanded),
3546 g_signal_connect (priv->tree_view, "row-collapsed",
3547 G_CALLBACK (gtk_combo_box_model_row_expanded),
3549 g_signal_connect (priv->popup_window, "button-press-event",
3550 G_CALLBACK (gtk_combo_box_list_button_pressed),
3552 g_signal_connect (priv->popup_window, "button-release-event",
3553 G_CALLBACK (gtk_combo_box_list_button_released),
3556 gtk_widget_show (priv->tree_view);
3558 gtk_combo_box_update_sensitivity (combo_box);
3562 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3564 GtkComboBoxPrivate *priv = combo_box->priv;
3566 /* disconnect signals */
3567 g_signal_handlers_disconnect_matched (priv->tree_view,
3568 G_SIGNAL_MATCH_DATA,
3569 0, 0, NULL, NULL, combo_box);
3570 g_signal_handlers_disconnect_matched (priv->button,
3571 G_SIGNAL_MATCH_DATA,
3573 gtk_combo_box_list_button_pressed,
3575 g_signal_handlers_disconnect_matched (priv->popup_window,
3576 G_SIGNAL_MATCH_DATA,
3578 gtk_combo_box_list_button_pressed,
3580 g_signal_handlers_disconnect_matched (priv->popup_window,
3581 G_SIGNAL_MATCH_DATA,
3583 gtk_combo_box_list_button_released,
3586 g_signal_handlers_disconnect_matched (priv->popup_window,
3587 G_SIGNAL_MATCH_DATA,
3589 gtk_combo_box_child_show,
3592 g_signal_handlers_disconnect_matched (priv->popup_window,
3593 G_SIGNAL_MATCH_DATA,
3595 gtk_combo_box_child_hide,
3599 g_signal_handlers_disconnect_matched (priv->box,
3600 G_SIGNAL_MATCH_DATA,
3602 gtk_combo_box_list_button_pressed,
3605 /* destroy things (unparent will kill the latest ref from us)
3606 * last unref on button will destroy the arrow
3608 gtk_widget_unparent (priv->button);
3609 priv->button = NULL;
3612 if (priv->cell_view)
3614 g_object_set (priv->cell_view,
3615 "background-set", FALSE,
3619 if (priv->cell_view_frame)
3621 gtk_widget_unparent (priv->cell_view_frame);
3622 priv->cell_view_frame = NULL;
3626 if (priv->scroll_timer)
3628 g_source_remove (priv->scroll_timer);
3629 priv->scroll_timer = 0;
3632 if (priv->resize_idle_id)
3634 g_source_remove (priv->resize_idle_id);
3635 priv->resize_idle_id = 0;
3638 gtk_widget_destroy (priv->tree_view);
3640 priv->tree_view = NULL;
3641 if (priv->popup_widget)
3643 g_object_unref (priv->popup_widget);
3644 priv->popup_widget = NULL;
3651 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3652 GdkEventButton *event,
3655 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3656 GtkComboBoxPrivate *priv = combo_box->priv;
3658 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3660 if (ewidget == priv->popup_window)
3663 if ((ewidget != priv->button && ewidget != priv->box) ||
3664 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3667 if (priv->focus_on_click &&
3668 !gtk_widget_has_focus (priv->button))
3669 gtk_widget_grab_focus (priv->button);
3671 gtk_combo_box_popup_for_device (combo_box, event->device);
3673 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3675 priv->auto_scroll = FALSE;
3676 if (priv->scroll_timer == 0)
3677 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3678 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3681 priv->popup_in_progress = TRUE;
3687 gtk_combo_box_list_button_released (GtkWidget *widget,
3688 GdkEventButton *event,
3692 GtkTreePath *path = NULL;
3695 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3696 GtkComboBoxPrivate *priv = combo_box->priv;
3698 gboolean popup_in_progress = FALSE;
3700 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3702 if (priv->popup_in_progress)
3704 popup_in_progress = TRUE;
3705 priv->popup_in_progress = FALSE;
3708 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3710 if (priv->scroll_timer)
3712 g_source_remove (priv->scroll_timer);
3713 priv->scroll_timer = 0;
3716 if (ewidget != priv->tree_view)
3718 if ((ewidget == priv->button ||
3719 ewidget == priv->box) &&
3720 !popup_in_progress &&
3721 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3723 gtk_combo_box_popdown (combo_box);
3727 /* released outside treeview */
3728 if (ewidget != priv->button && ewidget != priv->box)
3730 gtk_combo_box_popdown (combo_box);
3738 /* select something cool */
3739 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3745 return TRUE; /* clicked outside window? */
3747 gtk_tree_model_get_iter (priv->model, &iter, path);
3748 gtk_tree_path_free (path);
3750 gtk_combo_box_popdown (combo_box);
3752 if (tree_column_row_is_sensitive (combo_box, &iter))
3753 gtk_combo_box_set_active_iter (combo_box, &iter);
3759 gtk_combo_box_menu_key_press (GtkWidget *widget,
3763 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3765 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3767 /* The menu hasn't managed the
3768 * event, forward it to the combobox
3770 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3777 gtk_combo_box_list_key_press (GtkWidget *widget,
3781 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3784 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3785 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3787 GtkTreeModel *model = NULL;
3789 gtk_combo_box_popdown (combo_box);
3791 if (combo_box->priv->model)
3793 GtkTreeSelection *sel;
3795 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3797 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3798 gtk_combo_box_set_active_iter (combo_box, &iter);
3804 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3806 /* The list hasn't managed the
3807 * event, forward it to the combobox
3809 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3816 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3821 GtkAllocation allocation;
3822 GtkWidget *tree_view = combo_box->priv->tree_view;
3825 gtk_widget_get_allocation (tree_view, &allocation);
3827 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3828 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3830 if (x <= allocation.x &&
3831 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3833 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3834 gtk_adjustment_set_value (adj, value);
3836 else if (x >= allocation.x + allocation.width &&
3837 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3839 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3840 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3844 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3845 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3847 if (y <= allocation.y &&
3848 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3850 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3851 gtk_adjustment_set_value (adj, value);
3853 else if (y >= allocation.height &&
3854 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3856 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3857 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3863 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3865 GtkComboBoxPrivate *priv = combo_box->priv;
3868 if (priv->auto_scroll)
3870 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3873 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3880 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3881 GdkEventCrossing *event,
3884 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3886 combo_box->priv->auto_scroll = TRUE;
3892 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3893 GtkTreeModel *model,
3895 gboolean path_currently_selected,
3898 GList *list, *columns;
3899 gboolean sensitive = FALSE;
3901 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3903 for (list = columns; list && !sensitive; list = list->next)
3905 GList *cells, *cell;
3906 gboolean cell_sensitive, cell_visible;
3908 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3910 if (!gtk_tree_view_column_get_visible (column))
3913 gtk_tree_model_get_iter (model, &iter, path);
3914 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3917 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3920 g_object_get (cell->data,
3921 "sensitive", &cell_sensitive,
3922 "visible", &cell_visible,
3925 if (cell_visible && cell_sensitive)
3934 g_list_free (cells);
3937 g_list_free (columns);
3943 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3948 /* XXX Do nothing ? */
3952 * GtkCellLayout implementation
3954 static GtkCellArea *
3955 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3957 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3958 GtkComboBoxPrivate *priv = combo->priv;
3960 if (G_UNLIKELY (!priv->area))
3962 priv->area = gtk_cell_area_box_new ();
3963 g_object_ref_sink (priv->area);
3974 * gtk_combo_box_new:
3976 * Creates a new empty #GtkComboBox.
3978 * Return value: A new #GtkComboBox.
3983 gtk_combo_box_new (void)
3985 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3989 * gtk_combo_box_new_with_area:
3990 * @area: the #GtkCellArea to use to layout cell renderers
3992 * Creates a new empty #GtkComboBox using @area to layout cells.
3994 * Return value: A new #GtkComboBox.
3997 gtk_combo_box_new_with_area (GtkCellArea *area)
3999 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
4003 * gtk_combo_box_new_with_area_and_entry:
4004 * @area: the #GtkCellArea to use to layout cell renderers
4006 * Creates a new empty #GtkComboBox with an entry.
4008 * The new combo box will use @area to layout cells.
4010 * Return value: A new #GtkComboBox.
4013 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
4015 return g_object_new (GTK_TYPE_COMBO_BOX,
4023 * gtk_combo_box_new_with_entry:
4025 * Creates a new empty #GtkComboBox with an entry.
4027 * Return value: A new #GtkComboBox.
4030 gtk_combo_box_new_with_entry (void)
4032 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
4036 * gtk_combo_box_new_with_model:
4037 * @model: A #GtkTreeModel.
4039 * Creates a new #GtkComboBox with the model initialized to @model.
4041 * Return value: A new #GtkComboBox.
4046 gtk_combo_box_new_with_model (GtkTreeModel *model)
4048 GtkComboBox *combo_box;
4050 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4052 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4054 return GTK_WIDGET (combo_box);
4058 * gtk_combo_box_new_with_model_and_entry:
4059 * @model: A #GtkTreeModel
4061 * Creates a new empty #GtkComboBox with an entry
4062 * and with the model initialized to @model.
4064 * Return value: A new #GtkComboBox
4067 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4069 return g_object_new (GTK_TYPE_COMBO_BOX,
4076 * gtk_combo_box_get_wrap_width:
4077 * @combo_box: A #GtkComboBox
4079 * Returns the wrap width which is used to determine the number of columns
4080 * for the popup menu. If the wrap width is larger than 1, the combo box
4083 * Returns: the wrap width.
4088 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4090 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4092 return combo_box->priv->wrap_width;
4096 * gtk_combo_box_set_wrap_width:
4097 * @combo_box: A #GtkComboBox
4098 * @width: Preferred number of columns
4100 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4101 * the preferred number of columns when you want the popup to be layed out
4107 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4110 GtkComboBoxPrivate *priv;
4112 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4113 g_return_if_fail (width >= 0);
4115 priv = combo_box->priv;
4117 if (width != priv->wrap_width)
4119 priv->wrap_width = width;
4121 gtk_combo_box_check_appearance (combo_box);
4123 if (GTK_IS_TREE_MENU (priv->popup_widget))
4124 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4126 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4131 * gtk_combo_box_get_row_span_column:
4132 * @combo_box: A #GtkComboBox
4134 * Returns the column with row span information for @combo_box.
4136 * Returns: the row span column.
4141 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4143 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4145 return combo_box->priv->row_column;
4149 * gtk_combo_box_set_row_span_column:
4150 * @combo_box: A #GtkComboBox.
4151 * @row_span: A column in the model passed during construction.
4153 * Sets the column with row span information for @combo_box to be @row_span.
4154 * The row span column contains integers which indicate how many rows
4155 * an item should span.
4160 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4163 GtkComboBoxPrivate *priv;
4166 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4168 priv = combo_box->priv;
4170 col = gtk_tree_model_get_n_columns (priv->model);
4171 g_return_if_fail (row_span >= -1 && row_span < col);
4173 if (row_span != priv->row_column)
4175 priv->row_column = row_span;
4177 if (GTK_IS_TREE_MENU (priv->popup_widget))
4178 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4180 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4185 * gtk_combo_box_get_column_span_column:
4186 * @combo_box: A #GtkComboBox
4188 * Returns the column with column span information for @combo_box.
4190 * Returns: the column span column.
4195 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4197 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4199 return combo_box->priv->col_column;
4203 * gtk_combo_box_set_column_span_column:
4204 * @combo_box: A #GtkComboBox
4205 * @column_span: A column in the model passed during construction
4207 * Sets the column with column span information for @combo_box to be
4208 * @column_span. The column span column contains integers which indicate
4209 * how many columns an item should span.
4214 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4217 GtkComboBoxPrivate *priv;
4220 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4222 priv = combo_box->priv;
4224 col = gtk_tree_model_get_n_columns (priv->model);
4225 g_return_if_fail (column_span >= -1 && column_span < col);
4227 if (column_span != priv->col_column)
4229 priv->col_column = column_span;
4231 if (GTK_IS_TREE_MENU (priv->popup_widget))
4232 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4234 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4239 * gtk_combo_box_get_active:
4240 * @combo_box: A #GtkComboBox
4242 * Returns the index of the currently active item, or -1 if there's no
4243 * active item. If the model is a non-flat treemodel, and the active item
4244 * is not an immediate child of the root of the tree, this function returns
4245 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4246 * <literal>path</literal> is the #GtkTreePath of the active item.
4248 * Return value: An integer which is the index of the currently active item,
4249 * or -1 if there's no active item.
4254 gtk_combo_box_get_active (GtkComboBox *combo_box)
4256 GtkComboBoxPrivate *priv;
4259 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4261 priv = combo_box->priv;
4263 if (gtk_tree_row_reference_valid (priv->active_row))
4267 path = gtk_tree_row_reference_get_path (priv->active_row);
4268 result = gtk_tree_path_get_indices (path)[0];
4269 gtk_tree_path_free (path);
4278 * gtk_combo_box_set_active:
4279 * @combo_box: A #GtkComboBox
4280 * @index_: An index in the model passed during construction, or -1 to have
4283 * Sets the active item of @combo_box to be the item at @index.
4288 gtk_combo_box_set_active (GtkComboBox *combo_box,
4291 GtkTreePath *path = NULL;
4292 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4293 g_return_if_fail (index_ >= -1);
4295 if (combo_box->priv->model == NULL)
4297 /* Save index, in case the model is set after the index */
4298 combo_box->priv->active = index_;
4304 path = gtk_tree_path_new_from_indices (index_, -1);
4306 gtk_combo_box_set_active_internal (combo_box, path);
4309 gtk_tree_path_free (path);
4313 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4316 GtkComboBoxPrivate *priv = combo_box->priv;
4317 GtkTreePath *active_path;
4320 /* Remember whether the initially active row is valid. */
4321 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4323 if (path && is_valid_row_reference)
4325 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4326 path_cmp = gtk_tree_path_compare (path, active_path);
4327 gtk_tree_path_free (active_path);
4332 if (priv->active_row)
4334 gtk_tree_row_reference_free (priv->active_row);
4335 priv->active_row = NULL;
4340 if (priv->tree_view)
4341 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4344 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4346 if (GTK_IS_MENU (menu))
4347 gtk_menu_set_active (menu, -1);
4350 if (priv->cell_view)
4351 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4354 * Do not emit a "changed" signal when an already invalid selection was
4355 * now set to invalid.
4357 if (!is_valid_row_reference)
4363 gtk_tree_row_reference_new (priv->model, path);
4365 if (priv->tree_view)
4367 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4370 else if (GTK_IS_MENU (priv->popup_widget))
4372 /* FIXME handle nested menus better */
4373 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4374 gtk_tree_path_get_indices (path)[0]);
4377 if (priv->cell_view)
4378 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4382 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4383 g_object_notify (G_OBJECT (combo_box), "active");
4384 if (combo_box->priv->id_column >= 0)
4385 g_object_notify (G_OBJECT (combo_box), "active-id");
4390 * gtk_combo_box_get_active_iter:
4391 * @combo_box: A #GtkComboBox
4392 * @iter: (out): The uninitialized #GtkTreeIter
4394 * Sets @iter to point to the current active item, if it exists.
4396 * Return value: %TRUE, if @iter was set
4401 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4407 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4409 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4412 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4413 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4414 gtk_tree_path_free (path);
4420 * gtk_combo_box_set_active_iter:
4421 * @combo_box: A #GtkComboBox
4422 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4424 * Sets the current active item to be the one referenced by @iter, or
4425 * unsets the active item if @iter is %NULL.
4430 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4433 GtkTreePath *path = NULL;
4435 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4438 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4440 gtk_combo_box_set_active_internal (combo_box, path);
4441 gtk_tree_path_free (path);
4445 * gtk_combo_box_set_model:
4446 * @combo_box: A #GtkComboBox
4447 * @model: (allow-none): A #GtkTreeModel
4449 * Sets the model used by @combo_box to be @model. Will unset a previously set
4450 * model (if applicable). If model is %NULL, then it will unset the model.
4452 * Note that this function does not clear the cell renderers, you have to
4453 * call gtk_cell_layout_clear() yourself if you need to set up different
4454 * cell renderers for the new model.
4459 gtk_combo_box_set_model (GtkComboBox *combo_box,
4460 GtkTreeModel *model)
4462 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4463 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4465 if (model == combo_box->priv->model)
4468 gtk_combo_box_unset_model (combo_box);
4473 combo_box->priv->model = model;
4474 g_object_ref (combo_box->priv->model);
4476 combo_box->priv->inserted_id =
4477 g_signal_connect (combo_box->priv->model, "row-inserted",
4478 G_CALLBACK (gtk_combo_box_model_row_inserted),
4480 combo_box->priv->deleted_id =
4481 g_signal_connect (combo_box->priv->model, "row-deleted",
4482 G_CALLBACK (gtk_combo_box_model_row_deleted),
4484 combo_box->priv->reordered_id =
4485 g_signal_connect (combo_box->priv->model, "rows-reordered",
4486 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4488 combo_box->priv->changed_id =
4489 g_signal_connect (combo_box->priv->model, "row-changed",
4490 G_CALLBACK (gtk_combo_box_model_row_changed),
4493 if (combo_box->priv->tree_view)
4496 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4497 combo_box->priv->model);
4498 gtk_combo_box_list_popup_resize (combo_box);
4501 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4504 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4505 combo_box->priv->model);
4508 if (combo_box->priv->cell_view)
4509 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4510 combo_box->priv->model);
4512 if (combo_box->priv->active != -1)
4514 /* If an index was set in advance, apply it now */
4515 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4516 combo_box->priv->active = -1;
4520 gtk_combo_box_update_sensitivity (combo_box);
4522 g_object_notify (G_OBJECT (combo_box), "model");
4526 * gtk_combo_box_get_model:
4527 * @combo_box: A #GtkComboBox
4529 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4531 * Return value: (transfer none): A #GtkTreeModel which was passed
4532 * during construction.
4537 gtk_combo_box_get_model (GtkComboBox *combo_box)
4539 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4541 return combo_box->priv->model;
4545 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4546 GtkScrollType scroll)
4549 GtkTreeIter new_iter;
4550 gboolean active_iter;
4553 if (!combo_box->priv->model)
4555 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4559 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4563 case GTK_SCROLL_STEP_BACKWARD:
4564 case GTK_SCROLL_STEP_UP:
4565 case GTK_SCROLL_STEP_LEFT:
4568 found = tree_prev (combo_box, combo_box->priv->model,
4569 &iter, &new_iter, FALSE);
4572 /* else fall through */
4574 case GTK_SCROLL_PAGE_FORWARD:
4575 case GTK_SCROLL_PAGE_DOWN:
4576 case GTK_SCROLL_PAGE_RIGHT:
4577 case GTK_SCROLL_END:
4578 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4581 case GTK_SCROLL_STEP_FORWARD:
4582 case GTK_SCROLL_STEP_DOWN:
4583 case GTK_SCROLL_STEP_RIGHT:
4586 found = tree_next (combo_box, combo_box->priv->model,
4587 &iter, &new_iter, FALSE);
4590 /* else fall through */
4592 case GTK_SCROLL_PAGE_BACKWARD:
4593 case GTK_SCROLL_PAGE_UP:
4594 case GTK_SCROLL_PAGE_LEFT:
4595 case GTK_SCROLL_START:
4596 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4603 if (found && active_iter)
4605 GtkTreePath *old_path;
4606 GtkTreePath *new_path;
4608 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4609 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4611 if (gtk_tree_path_compare (old_path, new_path) == 0)
4614 gtk_tree_path_free (old_path);
4615 gtk_tree_path_free (new_path);
4620 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4624 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4629 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4630 gboolean group_cycling)
4632 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4634 if (combo_box->priv->has_entry)
4638 child = gtk_bin_get_child (GTK_BIN (combo_box));
4640 gtk_widget_grab_focus (child);
4643 gtk_widget_grab_focus (combo_box->priv->button);
4649 gtk_combo_box_grab_focus (GtkWidget *widget)
4651 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4653 if (combo_box->priv->has_entry)
4657 child = gtk_bin_get_child (GTK_BIN (combo_box));
4659 gtk_widget_grab_focus (child);
4662 gtk_widget_grab_focus (combo_box->priv->button);
4666 gtk_combo_box_destroy (GtkWidget *widget)
4668 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4670 if (combo_box->priv->popup_idle_id > 0)
4672 g_source_remove (combo_box->priv->popup_idle_id);
4673 combo_box->priv->popup_idle_id = 0;
4676 gtk_combo_box_popdown (combo_box);
4678 if (combo_box->priv->row_separator_destroy)
4679 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4681 combo_box->priv->row_separator_func = NULL;
4682 combo_box->priv->row_separator_data = NULL;
4683 combo_box->priv->row_separator_destroy = NULL;
4685 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4686 combo_box->priv->cell_view = NULL;
4690 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4693 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4696 * Fixes regression reported in bug #574059. The old functionality relied on
4697 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4698 * when the selection was already set to -1.
4700 if (gtk_combo_box_get_active(combo_box) == -1)
4701 g_signal_emit_by_name (combo_box, "changed");
4703 gtk_combo_box_set_active (combo_box, -1);
4707 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4710 GtkTreeModel *model;
4713 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4715 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4723 model = gtk_combo_box_get_model (combo_box);
4724 path = gtk_tree_model_get_path (model, &iter);
4725 path_str = gtk_tree_path_to_string (path);
4727 g_signal_handlers_block_by_func (entry,
4728 gtk_combo_box_entry_contents_changed,
4732 g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0,
4735 gtk_entry_set_text (entry, text);
4737 g_signal_handlers_unblock_by_func (entry,
4738 gtk_combo_box_entry_contents_changed,
4741 gtk_tree_path_free (path);
4749 gtk_combo_box_format_entry_text (GtkComboBox *combo_box,
4752 GtkComboBoxPrivate *priv = combo_box->priv;
4753 GtkTreeModel *model;
4757 if (priv->text_column >= 0)
4759 model = gtk_combo_box_get_model (combo_box);
4760 gtk_tree_model_get_iter_from_string (model, &iter, path);
4762 gtk_tree_model_get (model, &iter,
4763 priv->text_column, &text,
4772 gtk_combo_box_constructor (GType type,
4773 guint n_construct_properties,
4774 GObjectConstructParam *construct_properties)
4777 GtkComboBox *combo_box;
4778 GtkComboBoxPrivate *priv;
4780 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4781 (type, n_construct_properties, construct_properties);
4783 combo_box = GTK_COMBO_BOX (object);
4784 priv = combo_box->priv;
4788 priv->area = gtk_cell_area_box_new ();
4789 g_object_ref_sink (priv->area);
4792 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4793 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4794 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4795 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4796 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4797 gtk_widget_show (priv->cell_view);
4799 gtk_combo_box_check_appearance (combo_box);
4801 if (priv->has_entry)
4804 GtkStyleContext *context;
4806 entry = gtk_entry_new ();
4807 gtk_widget_show (entry);
4808 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4810 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4811 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4813 priv->text_renderer = gtk_cell_renderer_text_new ();
4814 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4815 priv->text_renderer, TRUE);
4817 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4819 g_signal_connect (combo_box, "changed",
4820 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4828 gtk_combo_box_dispose(GObject* object)
4830 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4832 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4834 gtk_combo_box_menu_destroy (combo_box);
4835 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4836 combo_box->priv->popup_widget = NULL;
4839 if (combo_box->priv->area)
4841 g_object_unref (combo_box->priv->area);
4842 combo_box->priv->area = NULL;
4845 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4846 gtk_combo_box_list_destroy (combo_box);
4848 if (combo_box->priv->popup_window)
4850 gtk_widget_destroy (combo_box->priv->popup_window);
4851 combo_box->priv->popup_window = NULL;
4854 gtk_combo_box_unset_model (combo_box);
4856 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4860 gtk_combo_box_finalize (GObject *object)
4862 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4864 g_free (combo_box->priv->tearoff_title);
4866 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4870 gtk_cell_editable_key_press (GtkWidget *widget,
4874 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4876 if (event->keyval == GDK_KEY_Escape)
4878 g_object_set (combo_box,
4879 "editing-canceled", TRUE,
4881 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4882 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4886 else if (event->keyval == GDK_KEY_Return ||
4887 event->keyval == GDK_KEY_ISO_Enter ||
4888 event->keyval == GDK_KEY_KP_Enter)
4890 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4891 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4900 popdown_idle (gpointer data)
4902 GtkComboBox *combo_box;
4904 combo_box = GTK_COMBO_BOX (data);
4906 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4907 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4909 g_object_unref (combo_box);
4915 popdown_handler (GtkWidget *widget,
4918 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4922 popup_idle (gpointer data)
4924 GtkComboBox *combo_box;
4926 combo_box = GTK_COMBO_BOX (data);
4928 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4929 combo_box->priv->cell_view)
4930 g_signal_connect_object (combo_box->priv->popup_widget,
4931 "unmap", G_CALLBACK (popdown_handler),
4934 /* we unset this if a menu item is activated */
4935 g_object_set (combo_box,
4936 "editing-canceled", TRUE,
4938 gtk_combo_box_popup (combo_box);
4940 combo_box->priv->popup_idle_id = 0;
4941 combo_box->priv->activate_button = 0;
4942 combo_box->priv->activate_time = 0;
4948 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4951 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4954 combo_box->priv->is_cell_renderer = TRUE;
4956 if (combo_box->priv->cell_view)
4958 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4959 G_CALLBACK (gtk_cell_editable_key_press),
4962 gtk_widget_grab_focus (combo_box->priv->button);
4966 child = gtk_bin_get_child (GTK_BIN (combo_box));
4968 g_signal_connect_object (child, "key-press-event",
4969 G_CALLBACK (gtk_cell_editable_key_press),
4972 gtk_widget_grab_focus (child);
4973 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4976 /* we do the immediate popup only for the optionmenu-like
4979 if (combo_box->priv->is_cell_renderer &&
4980 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4982 if (event && event->type == GDK_BUTTON_PRESS)
4984 GdkEventButton *event_button = (GdkEventButton *)event;
4986 combo_box->priv->activate_button = event_button->button;
4987 combo_box->priv->activate_time = event_button->time;
4990 combo_box->priv->popup_idle_id =
4991 gdk_threads_add_idle (popup_idle, combo_box);
4997 * gtk_combo_box_get_add_tearoffs:
4998 * @combo_box: a #GtkComboBox
5000 * Gets the current value of the :add-tearoffs property.
5002 * Return value: the current value of the :add-tearoffs property.
5005 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5007 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5009 return combo_box->priv->add_tearoffs;
5013 * gtk_combo_box_set_add_tearoffs:
5014 * @combo_box: a #GtkComboBox
5015 * @add_tearoffs: %TRUE to add tearoff menu items
5017 * Sets whether the popup menu should have a tearoff
5023 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5024 gboolean add_tearoffs)
5026 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5028 add_tearoffs = add_tearoffs != FALSE;
5030 if (combo_box->priv->add_tearoffs != add_tearoffs)
5032 combo_box->priv->add_tearoffs = add_tearoffs;
5033 gtk_combo_box_check_appearance (combo_box);
5035 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5036 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
5037 combo_box->priv->add_tearoffs);
5039 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5044 * gtk_combo_box_get_title:
5045 * @combo_box: a #GtkComboBox
5047 * Gets the current title of the menu in tearoff mode. See
5048 * gtk_combo_box_set_add_tearoffs().
5050 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5051 * string which must not be freed.
5056 gtk_combo_box_get_title (GtkComboBox *combo_box)
5058 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5060 return combo_box->priv->tearoff_title;
5064 gtk_combo_box_update_title (GtkComboBox *combo_box)
5066 gtk_combo_box_check_appearance (combo_box);
5068 if (combo_box->priv->popup_widget &&
5069 GTK_IS_MENU (combo_box->priv->popup_widget))
5070 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5071 combo_box->priv->tearoff_title);
5075 * gtk_combo_box_set_title:
5076 * @combo_box: a #GtkComboBox
5077 * @title: a title for the menu in tearoff mode
5079 * Sets the menu's title in tearoff mode.
5084 gtk_combo_box_set_title (GtkComboBox *combo_box,
5087 GtkComboBoxPrivate *priv;
5089 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5091 priv = combo_box->priv;
5093 if (strcmp (title ? title : "",
5094 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5096 g_free (priv->tearoff_title);
5097 priv->tearoff_title = g_strdup (title);
5099 gtk_combo_box_update_title (combo_box);
5101 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5107 * gtk_combo_box_set_popup_fixed_width:
5108 * @combo_box: a #GtkComboBox
5109 * @fixed: whether to use a fixed popup width
5111 * Specifies whether the popup's width should be a fixed width
5112 * matching the allocated width of the combo box.
5117 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5120 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5122 if (combo_box->priv->popup_fixed_width != fixed)
5124 combo_box->priv->popup_fixed_width = fixed;
5126 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5131 * gtk_combo_box_get_popup_fixed_width:
5132 * @combo_box: a #GtkComboBox
5134 * Gets whether the popup uses a fixed width matching
5135 * the allocated width of the combo box.
5137 * Returns: %TRUE if the popup uses a fixed width
5142 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5144 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5146 return combo_box->priv->popup_fixed_width;
5151 * gtk_combo_box_get_popup_accessible:
5152 * @combo_box: a #GtkComboBox
5154 * Gets the accessible object corresponding to the combo box's popup.
5156 * This function is mostly intended for use by accessibility technologies;
5157 * applications should have little use for it.
5159 * Returns: (transfer none): the accessible object corresponding
5160 * to the combo box's popup.
5165 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5169 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5171 if (combo_box->priv->popup_widget)
5173 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5181 * gtk_combo_box_get_row_separator_func: (skip)
5182 * @combo_box: a #GtkComboBox
5184 * Returns the current row separator function.
5186 * Return value: the current row separator function.
5190 GtkTreeViewRowSeparatorFunc
5191 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5193 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5195 return combo_box->priv->row_separator_func;
5199 * gtk_combo_box_set_row_separator_func:
5200 * @combo_box: a #GtkComboBox
5201 * @func: a #GtkTreeViewRowSeparatorFunc
5202 * @data: (allow-none): user data to pass to @func, or %NULL
5203 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5205 * Sets the row separator function, which is used to determine
5206 * whether a row should be drawn as a separator. If the row separator
5207 * function is %NULL, no separators are drawn. This is the default value.
5212 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5213 GtkTreeViewRowSeparatorFunc func,
5215 GDestroyNotify destroy)
5217 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5219 if (combo_box->priv->row_separator_destroy)
5220 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5222 combo_box->priv->row_separator_func = func;
5223 combo_box->priv->row_separator_data = data;
5224 combo_box->priv->row_separator_destroy = destroy;
5226 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5227 if (combo_box->priv->tree_view)
5229 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5230 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5233 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5235 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5236 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5239 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5243 * gtk_combo_box_set_button_sensitivity:
5244 * @combo_box: a #GtkComboBox
5245 * @sensitivity: specify the sensitivity of the dropdown button
5247 * Sets whether the dropdown button of the combo box should be
5248 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5249 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5254 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5255 GtkSensitivityType sensitivity)
5257 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5259 if (combo_box->priv->button_sensitivity != sensitivity)
5261 combo_box->priv->button_sensitivity = sensitivity;
5262 gtk_combo_box_update_sensitivity (combo_box);
5264 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5269 * gtk_combo_box_get_button_sensitivity:
5270 * @combo_box: a #GtkComboBox
5272 * Returns whether the combo box sets the dropdown button
5273 * sensitive or not when there are no items in the model.
5275 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5276 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5277 * if the button is always insensitive or
5278 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5279 * the model has one item to be selected.
5284 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5286 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5288 return combo_box->priv->button_sensitivity;
5293 * gtk_combo_box_get_has_entry:
5294 * @combo_box: a #GtkComboBox
5296 * Returns whether the combo box has an entry.
5298 * Return Value: whether there is an entry in @combo_box.
5303 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5305 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5307 return combo_box->priv->has_entry;
5311 * gtk_combo_box_set_entry_text_column:
5312 * @combo_box: A #GtkComboBox
5313 * @text_column: A column in @model to get the strings from for
5314 * the internal entry
5316 * Sets the model column which @combo_box should use to get strings from
5317 * to be @text_column. The column @text_column in the model of @combo_box
5318 * must be of type %G_TYPE_STRING.
5320 * This is only relevant if @combo_box has been created with
5321 * #GtkComboBox:has-entry as %TRUE.
5326 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5329 GtkComboBoxPrivate *priv = combo_box->priv;
5330 GtkTreeModel *model;
5332 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5334 model = gtk_combo_box_get_model (combo_box);
5336 g_return_if_fail (text_column >= 0);
5337 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5339 priv->text_column = text_column;
5341 if (priv->text_renderer != NULL)
5342 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5343 priv->text_renderer,
5344 "text", text_column,
5349 * gtk_combo_box_get_entry_text_column:
5350 * @combo_box: A #GtkComboBox.
5352 * Returns the column which @combo_box is using to get the strings
5353 * from to display in the internal entry.
5355 * Return value: A column in the data source model of @combo_box.
5360 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5362 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5364 return combo_box->priv->text_column;
5368 * gtk_combo_box_set_focus_on_click:
5369 * @combo: a #GtkComboBox
5370 * @focus_on_click: whether the combo box grabs focus when clicked
5373 * Sets whether the combo box will grab focus when it is clicked with
5374 * the mouse. Making mouse clicks not grab focus is useful in places
5375 * like toolbars where you don't want the keyboard focus removed from
5376 * the main area of the application.
5381 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5382 gboolean focus_on_click)
5384 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5386 focus_on_click = focus_on_click != FALSE;
5388 if (combo_box->priv->focus_on_click != focus_on_click)
5390 combo_box->priv->focus_on_click = focus_on_click;
5392 if (combo_box->priv->button)
5393 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5396 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5401 * gtk_combo_box_get_focus_on_click:
5402 * @combo: a #GtkComboBox
5404 * Returns whether the combo box grabs focus when it is clicked
5405 * with the mouse. See gtk_combo_box_set_focus_on_click().
5407 * Return value: %TRUE if the combo box grabs focus when it is
5408 * clicked with the mouse.
5413 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5415 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5417 return combo_box->priv->focus_on_click;
5422 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5423 GtkBuilder *builder,
5425 const gchar *tagname,
5426 GMarkupParser *parser,
5429 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5430 tagname, parser, data))
5433 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5434 tagname, parser, data);
5438 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5439 GtkBuilder *builder,
5441 const gchar *tagname,
5444 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5445 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5449 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5450 GtkBuilder *builder,
5451 const gchar *childname)
5453 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5455 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5456 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5458 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5462 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5466 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5467 GtkComboBoxPrivate *priv = combo_box->priv;
5468 gint font_size, arrow_size;
5469 PangoContext *context;
5470 PangoFontMetrics *metrics;
5472 gint minimum_width = 0, natural_width = 0;
5473 gint child_min, child_nat;
5475 gfloat arrow_scaling;
5477 child = gtk_bin_get_child (GTK_BIN (widget));
5480 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5482 gtk_widget_style_get (GTK_WIDGET (widget),
5483 "arrow-size", &arrow_size,
5484 "arrow-scaling", &arrow_scaling,
5487 get_widget_padding_and_border (widget, &padding);
5489 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5490 metrics = pango_context_get_metrics (context,
5491 pango_context_get_font_description (context),
5492 pango_context_get_language (context));
5493 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5494 pango_font_metrics_get_descent (metrics));
5495 pango_font_metrics_unref (metrics);
5497 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5499 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5501 if (!priv->tree_view)
5504 if (priv->cell_view)
5506 gint box_width, xpad;
5507 GtkBorder button_padding;
5509 get_widget_padding_and_border (priv->button, &button_padding);
5511 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5512 xpad = button_padding.left + button_padding.right + padding.left + padding.right;
5514 minimum_width = child_min + box_width + xpad;
5515 natural_width = child_nat + box_width + xpad;
5519 gint but_width, but_nat_width;
5521 gtk_widget_get_preferred_width (priv->button,
5522 &but_width, &but_nat_width);
5524 minimum_width = child_min + but_width;
5525 natural_width = child_nat + but_nat_width;
5531 gint button_width, button_nat_width;
5533 /* sample + frame */
5534 minimum_width = child_min;
5535 natural_width = child_nat;
5537 if (priv->cell_view_frame)
5539 if (priv->has_frame)
5541 gint border_width, xpad;
5542 GtkBorder frame_padding;
5544 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5545 get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5546 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5548 minimum_width += xpad;
5549 natural_width += xpad;
5554 gtk_widget_get_preferred_width (priv->button,
5555 &button_width, &button_nat_width);
5557 minimum_width += button_width;
5558 natural_width += button_nat_width;
5561 minimum_width += padding.left + padding.right;
5562 natural_width += padding.left + padding.right;
5565 *minimum_size = minimum_width;
5568 *natural_size = natural_width;
5572 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5578 /* Combo box is height-for-width only
5579 * (so we always just reserve enough height for the minimum width) */
5580 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5581 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5585 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5590 /* Combo box is height-for-width only
5591 * (so we assume we always reserved enough height for the minimum width) */
5592 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5597 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5602 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5603 GtkComboBoxPrivate *priv = combo_box->priv;
5604 gint min_height = 0, nat_height = 0;
5609 child = gtk_bin_get_child (GTK_BIN (widget));
5611 get_widget_padding_and_border (widget, &padding);
5614 if (!priv->tree_view)
5617 if (priv->cell_view)
5619 /* calculate x/y padding and separator/arrow size */
5620 gint box_width, box_height;
5622 GtkBorder button_padding;
5624 get_widget_padding_and_border (priv->button, &button_padding);
5626 gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5627 gtk_widget_get_preferred_height_for_width (priv->box,
5628 box_width, &box_height, NULL);
5630 xpad = button_padding.left + button_padding.right;
5631 ypad = button_padding.top + button_padding.bottom;
5633 size -= box_width + xpad;
5635 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5636 * and fitting the whole treemodel */
5637 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5639 min_height = MAX (min_height, box_height);
5640 nat_height = MAX (nat_height, box_height);
5647 /* there is a custom child widget inside (no priv->cell_view) */
5648 gint but_width, but_height;
5650 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5651 gtk_widget_get_preferred_height_for_width (priv->button,
5652 but_width, &but_height, NULL);
5656 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5657 * and fitting the whole treemodel */
5658 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5660 min_height = MAX (min_height, but_height);
5661 nat_height = MAX (nat_height, but_height);
5667 gint but_width, but_height;
5668 gint xpad = 0, ypad = 0;
5670 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5671 gtk_widget_get_preferred_height_for_width (priv->button,
5672 but_width, &but_height, NULL);
5674 if (priv->cell_view_frame && priv->has_frame)
5676 GtkBorder frame_padding;
5679 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5680 get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5682 xpad = (2 * border_width) + padding.left + frame_padding.right;
5683 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5689 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5690 * and fitting the whole treemodel */
5691 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5693 min_height = MAX (min_height, but_height);
5694 nat_height = MAX (nat_height, but_height);
5700 min_height += padding.top + padding.bottom;
5701 nat_height += padding.top + padding.bottom;
5704 *minimum_size = min_height;
5707 *natural_size = nat_height;
5711 * gtk_combo_box_set_id_column:
5712 * @combo_box: A #GtkComboBox
5713 * @id_column: A column in @model to get string IDs for values from
5715 * Sets the model column which @combo_box should use to get string IDs
5716 * for values from. The column @id_column in the model of @combo_box
5717 * must be of type %G_TYPE_STRING.
5722 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5725 GtkComboBoxPrivate *priv = combo_box->priv;
5726 GtkTreeModel *model;
5728 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5730 if (id_column != priv->id_column)
5732 model = gtk_combo_box_get_model (combo_box);
5734 g_return_if_fail (id_column >= 0);
5735 g_return_if_fail (model == NULL ||
5736 id_column < gtk_tree_model_get_n_columns (model));
5738 priv->id_column = id_column;
5740 g_object_notify (G_OBJECT (combo_box), "id-column");
5741 g_object_notify (G_OBJECT (combo_box), "active-id");
5746 * gtk_combo_box_get_id_column:
5747 * @combo_box: A #GtkComboBox
5749 * Returns the column which @combo_box is using to get string IDs
5752 * Return value: A column in the data source model of @combo_box.
5757 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5759 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5761 return combo_box->priv->id_column;
5765 * gtk_combo_box_get_active_id:
5766 * @combo_box: a #GtkComboBox
5768 * Returns the ID of the active row of @combo_box. This value is taken
5769 * from the active row and the column specified by the #GtkComboBox:id-column
5770 * property of @combo_box (see gtk_combo_box_set_id_column()).
5772 * The returned value is an interned string which means that you can
5773 * compare the pointer by value to other interned strings and that you
5776 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5777 * no row is active, or if the active row has a %NULL ID value, then %NULL
5780 * Return value: the ID of the active row, or %NULL
5785 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5787 GtkTreeModel *model;
5791 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5793 column = combo_box->priv->id_column;
5798 model = gtk_combo_box_get_model (combo_box);
5799 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5800 G_TYPE_STRING, NULL);
5802 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5804 const gchar *interned;
5807 gtk_tree_model_get (model, &iter, column, &id, -1);
5808 interned = g_intern_string (id);
5818 * gtk_combo_box_set_active_id:
5819 * @combo_box: a #GtkComboBox
5820 * @active_id: (allow-none): the ID of the row to select, or %NULL
5822 * Changes the active row of @combo_box to the one that has an ID equal to
5823 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5824 * a %NULL ID string cannot be made active by this function.
5826 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5827 * row has the given ID then the function does nothing and returns %FALSE.
5829 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5830 * @active_id was given to unset the active row, the function
5831 * always returns %TRUE.
5836 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5837 const gchar *active_id)
5839 GtkTreeModel *model;
5841 gboolean match = FALSE;
5844 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5846 if (active_id == NULL)
5848 gtk_combo_box_set_active (combo_box, -1);
5849 return TRUE; /* active row was successfully unset */
5852 column = combo_box->priv->id_column;
5857 model = gtk_combo_box_get_model (combo_box);
5858 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5859 G_TYPE_STRING, FALSE);
5861 if (gtk_tree_model_get_iter_first (model, &iter))
5865 gtk_tree_model_get (model, &iter, column, &id, -1);
5867 match = strcmp (id, active_id) == 0;
5872 gtk_combo_box_set_active_iter (combo_box, &iter);
5875 } while (gtk_tree_model_iter_next (model, &iter));