2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcombobox.h"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
32 #include "gtkliststore.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkseparator.h"
42 #include "gtkwindow.h"
43 #include "gtktypebuiltins.h"
44 #include "gtkprivate.h"
46 #include <gobject/gvaluecollector.h>
51 #include "gtkmarshalers.h"
54 #include "gtkentryprivate.h"
55 #include "gtktreeprivate.h"
56 #include "a11y/gtkcomboboxaccessible.h"
61 * @Short_description: A widget used to choose from a list of items
63 * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
65 * A GtkComboBox is a widget that allows the user to choose from a list of
66 * valid choices. The GtkComboBox displays the selected choice. When
67 * activated, the GtkComboBox displays a popup which allows the user to
68 * make a new choice. The style in which the selected value is displayed,
69 * and the style of the popup is determined by the current theme. It may
70 * be similar to a Windows-style combo box.
72 * The GtkComboBox uses the model-view pattern; the list of valid choices
73 * is specified in the form of a tree model, and the display of the choices
74 * can be adapted to the data in the model by using cell renderers, as you
75 * would in a tree view. This is possible since GtkComboBox implements the
76 * #GtkCellLayout interface. The tree model holding the valid choices is
77 * not restricted to a flat list, it can be a real tree, and the popup will
78 * reflect the tree structure.
80 * To allow the user to enter values not in the model, the 'has-entry'
81 * property allows the GtkComboBox to contain a #GtkEntry. This entry
82 * can be accessed by calling gtk_bin_get_child() on the combo box.
84 * For a simple list of textual choices, the model-view API of GtkComboBox
85 * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86 * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
91 /* WELCOME, to THE house of evil code */
92 struct _GtkComboBoxPrivate
102 GtkShadowType shadow_type;
104 gint active; /* Only temporary */
105 GtkTreeRowReference *active_row;
107 GtkWidget *tree_view;
109 GtkWidget *cell_view;
110 GtkWidget *cell_view_frame;
115 GtkWidget *separator;
117 GtkWidget *popup_widget;
118 GtkWidget *popup_window;
119 GtkWidget *scrolled_window;
126 guint activate_button;
127 guint32 activate_time;
129 guint resize_idle_id;
131 /* For "has-entry" specific behavior we track
132 * an automated cell renderer and text column
135 GtkCellRenderer *text_renderer;
139 guint popup_in_progress : 1;
140 guint popup_shown : 1;
141 guint add_tearoffs : 1;
143 guint is_cell_renderer : 1;
144 guint editing_canceled : 1;
145 guint auto_scroll : 1;
146 guint focus_on_click : 1;
147 guint button_sensitivity : 2;
149 guint popup_fixed_width : 1;
151 GtkTreeViewRowSeparatorFunc row_separator_func;
152 gpointer row_separator_data;
153 GDestroyNotify row_separator_destroy;
155 GdkDevice *grab_pointer;
156 GdkDevice *grab_keyboard;
158 gchar *tearoff_title;
161 /* While debugging this evil code, I have learned that
162 * there are actually 4 modes to this widget, which can
163 * be characterized as follows
165 * 1) menu mode, no child added
168 * cell_view -> GtkCellView, regular child
169 * cell_view_frame -> NULL
170 * button -> GtkToggleButton set_parent to combo
171 * arrow -> GtkArrow set_parent to button
172 * separator -> GtkVSepator set_parent to button
173 * popup_widget -> GtkMenu
174 * popup_window -> NULL
175 * scrolled_window -> NULL
177 * 2) menu mode, child added
181 * cell_view_frame -> NULL
182 * button -> GtkToggleButton set_parent to combo
183 * arrow -> GtkArrow, child of button
185 * popup_widget -> GtkMenu
186 * popup_window -> NULL
187 * scrolled_window -> NULL
189 * 3) list mode, no child added
191 * tree_view -> GtkTreeView, child of scrolled_window
192 * cell_view -> GtkCellView, regular child
193 * cell_view_frame -> GtkFrame, set parent to combo
194 * button -> GtkToggleButton, set_parent to combo
195 * arrow -> GtkArrow, child of button
197 * popup_widget -> tree_view
198 * popup_window -> GtkWindow
199 * scrolled_window -> GtkScrolledWindow, child of popup_window
201 * 4) list mode, child added
203 * tree_view -> GtkTreeView, child of scrolled_window
205 * cell_view_frame -> NULL
206 * button -> GtkToggleButton, set_parent to combo
207 * arrow -> GtkArrow, child of button
209 * popup_widget -> tree_view
210 * popup_window -> GtkWindow
211 * scrolled_window -> GtkScrolledWindow, child of popup_window
227 PROP_ROW_SPAN_COLUMN,
228 PROP_COLUMN_SPAN_COLUMN,
235 PROP_BUTTON_SENSITIVITY,
236 PROP_EDITING_CANCELED,
238 PROP_ENTRY_TEXT_COLUMN,
239 PROP_POPUP_FIXED_WIDTH,
245 static guint combo_box_signals[LAST_SIGNAL] = {0,};
247 #define BONUS_PADDING 4
248 #define SCROLL_TIME 100
252 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
253 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
254 static GObject *gtk_combo_box_constructor (GType type,
255 guint n_construct_properties,
256 GObjectConstructParam *construct_properties);
257 static void gtk_combo_box_dispose (GObject *object);
258 static void gtk_combo_box_finalize (GObject *object);
259 static void gtk_combo_box_destroy (GtkWidget *widget);
261 static void gtk_combo_box_set_property (GObject *object,
265 static void gtk_combo_box_get_property (GObject *object,
270 static void gtk_combo_box_state_flags_changed (GtkWidget *widget,
271 GtkStateFlags previous);
272 static void gtk_combo_box_grab_focus (GtkWidget *widget);
273 static void gtk_combo_box_style_updated (GtkWidget *widget);
274 static void gtk_combo_box_button_toggled (GtkWidget *widget,
276 static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
277 GtkStateFlags previous,
279 static void gtk_combo_box_add (GtkContainer *container,
281 static void gtk_combo_box_remove (GtkContainer *container,
284 static void gtk_combo_box_menu_show (GtkWidget *menu,
286 static void gtk_combo_box_menu_hide (GtkWidget *menu,
289 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
291 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
296 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
301 static void gtk_combo_box_menu_position (GtkMenu *menu,
307 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
309 static void gtk_combo_box_size_allocate (GtkWidget *widget,
310 GtkAllocation *allocation);
311 static void gtk_combo_box_forall (GtkContainer *container,
312 gboolean include_internals,
313 GtkCallback callback,
314 gpointer callback_data);
315 static gboolean gtk_combo_box_draw (GtkWidget *widget,
317 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
318 GdkEventScroll *event);
319 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
322 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
323 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
324 GtkScrollType scroll);
325 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
326 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
328 /* listening to the model */
329 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
333 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
336 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
341 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
345 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
351 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
356 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
357 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
359 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
360 GdkEventButton *event,
362 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
365 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
366 GdkEventCrossing *event,
368 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
371 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
372 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
373 GdkEventButton *event,
376 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
379 gboolean path_currently_selected,
382 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
386 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
389 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
390 gboolean add_children);
391 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
392 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
395 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
396 GdkEventButton *event,
398 static void gtk_combo_box_menu_activate (GtkWidget *menu,
400 GtkComboBox *combo_box);
401 static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box);
402 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
405 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
407 guint32 activate_time);
410 static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout);
412 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
413 gboolean group_cycling);
415 static void gtk_combo_box_child_show (GtkWidget *widget,
416 GtkComboBox *combo_box);
417 static void gtk_combo_box_child_hide (GtkWidget *widget,
418 GtkComboBox *combo_box);
420 /* GtkComboBox:has-entry callbacks */
421 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
423 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
427 /* GtkBuildable method implementation */
428 static GtkBuildableIface *parent_buildable_iface;
430 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
431 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
434 const gchar *tagname,
435 GMarkupParser *parser,
437 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
440 const gchar *tagname,
442 static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
444 const gchar *childname);
447 /* GtkCellEditable method implementations */
448 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
451 static void gtk_combo_box_get_preferred_width (GtkWidget *widget,
454 static void gtk_combo_box_get_preferred_height (GtkWidget *widget,
457 static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
461 static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
465 static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container,
467 static void gtk_combo_box_direction_changed (GtkWidget *widget,
468 GtkTextDirection previous_direction);
470 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
471 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
472 gtk_combo_box_cell_layout_init)
473 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
474 gtk_combo_box_cell_editable_init)
475 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
476 gtk_combo_box_buildable_init))
481 gtk_combo_box_class_init (GtkComboBoxClass *klass)
483 GObjectClass *object_class;
484 GtkContainerClass *container_class;
485 GtkWidgetClass *widget_class;
486 GtkBindingSet *binding_set;
488 container_class = (GtkContainerClass *)klass;
489 container_class->forall = gtk_combo_box_forall;
490 container_class->add = gtk_combo_box_add;
491 container_class->remove = gtk_combo_box_remove;
492 container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
494 widget_class = (GtkWidgetClass *)klass;
495 widget_class->size_allocate = gtk_combo_box_size_allocate;
496 widget_class->draw = gtk_combo_box_draw;
497 widget_class->scroll_event = gtk_combo_box_scroll_event;
498 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
499 widget_class->grab_focus = gtk_combo_box_grab_focus;
500 widget_class->style_updated = gtk_combo_box_style_updated;
501 widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
502 widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
503 widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
504 widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
505 widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
506 widget_class->destroy = gtk_combo_box_destroy;
507 widget_class->direction_changed = gtk_combo_box_direction_changed;
509 object_class = (GObjectClass *)klass;
510 object_class->constructor = gtk_combo_box_constructor;
511 object_class->dispose = gtk_combo_box_dispose;
512 object_class->finalize = gtk_combo_box_finalize;
513 object_class->set_property = gtk_combo_box_set_property;
514 object_class->get_property = gtk_combo_box_get_property;
518 * GtkComboBox::changed:
519 * @widget: the object which received the signal
521 * The changed signal is emitted when the active
522 * item is changed. The can be due to the user selecting
523 * a different item from the list, or due to a
524 * call to gtk_combo_box_set_active_iter().
525 * It will also be emitted while typing into the entry of a combo box
530 combo_box_signals[CHANGED] =
531 g_signal_new (I_("changed"),
532 G_OBJECT_CLASS_TYPE (klass),
534 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
536 g_cclosure_marshal_VOID__VOID,
539 * GtkComboBox::move-active:
540 * @widget: the object that received the signal
541 * @scroll_type: a #GtkScrollType
543 * The ::move-active signal is a
544 * <link linkend="keybinding-signals">keybinding signal</link>
545 * which gets emitted to move the active selection.
549 combo_box_signals[MOVE_ACTIVE] =
550 g_signal_new_class_handler (I_("move-active"),
551 G_OBJECT_CLASS_TYPE (klass),
552 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
553 G_CALLBACK (gtk_combo_box_real_move_active),
555 g_cclosure_marshal_VOID__ENUM,
557 GTK_TYPE_SCROLL_TYPE);
560 * GtkComboBox::popup:
561 * @widget: the object that received the signal
563 * The ::popup signal is a
564 * <link linkend="keybinding-signals">keybinding signal</link>
565 * which gets emitted to popup the combo box list.
567 * The default binding for this signal is Alt+Down.
571 combo_box_signals[POPUP] =
572 g_signal_new_class_handler (I_("popup"),
573 G_OBJECT_CLASS_TYPE (klass),
574 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
575 G_CALLBACK (gtk_combo_box_real_popup),
577 g_cclosure_marshal_VOID__VOID,
580 * GtkComboBox::popdown:
581 * @button: the object which received the signal
583 * The ::popdown signal is a
584 * <link linkend="keybinding-signals">keybinding signal</link>
585 * which gets emitted to popdown the combo box list.
587 * The default bindings for this signal are Alt+Up and Escape.
591 combo_box_signals[POPDOWN] =
592 g_signal_new_class_handler (I_("popdown"),
593 G_OBJECT_CLASS_TYPE (klass),
594 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
595 G_CALLBACK (gtk_combo_box_real_popdown),
597 _gtk_marshal_BOOLEAN__VOID,
601 binding_set = gtk_binding_set_by_class (widget_class);
603 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
608 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
610 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
612 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
615 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
617 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
618 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
620 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
621 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
623 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
624 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
626 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
627 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
629 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
630 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
632 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
634 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
636 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
637 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
639 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
640 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
642 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
643 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
645 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
646 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
648 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
649 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
651 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
654 g_object_class_override_property (object_class,
655 PROP_EDITING_CANCELED,
661 * The model from which the combo box takes the values shown
666 g_object_class_install_property (object_class,
668 g_param_spec_object ("model",
669 P_("ComboBox model"),
670 P_("The model for the combo box"),
672 GTK_PARAM_READWRITE));
675 * GtkComboBox:wrap-width:
677 * If wrap-width is set to a positive value, the list will be
678 * displayed in multiple columns, the number of columns is
679 * determined by wrap-width.
683 g_object_class_install_property (object_class,
685 g_param_spec_int ("wrap-width",
687 P_("Wrap width for laying out the items in a grid"),
691 GTK_PARAM_READWRITE));
695 * GtkComboBox:row-span-column:
697 * If this is set to a non-negative value, it must be the index of a column
698 * of type %G_TYPE_INT in the model.
700 * The values of that column are used to determine how many rows a value in
701 * the list will span. Therefore, the values in the model column pointed to
702 * by this property must be greater than zero and not larger than wrap-width.
706 g_object_class_install_property (object_class,
707 PROP_ROW_SPAN_COLUMN,
708 g_param_spec_int ("row-span-column",
709 P_("Row span column"),
710 P_("TreeModel column containing the row span values"),
714 GTK_PARAM_READWRITE));
718 * GtkComboBox:column-span-column:
720 * If this is set to a non-negative value, it must be the index of a column
721 * of type %G_TYPE_INT in the model.
723 * The values of that column are used to determine how many columns a value
724 * in the list will span.
728 g_object_class_install_property (object_class,
729 PROP_COLUMN_SPAN_COLUMN,
730 g_param_spec_int ("column-span-column",
731 P_("Column span column"),
732 P_("TreeModel column containing the column span values"),
736 GTK_PARAM_READWRITE));
740 * GtkComboBox:active:
742 * The item which is currently active. If the model is a non-flat treemodel,
743 * and the active item is not an immediate child of the root of the tree,
744 * this property has the value
745 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
746 * where <literal>path</literal> is the #GtkTreePath of the active item.
750 g_object_class_install_property (object_class,
752 g_param_spec_int ("active",
754 P_("The item which is currently active"),
758 GTK_PARAM_READWRITE));
761 * GtkComboBox:add-tearoffs:
763 * The add-tearoffs property controls whether generated menus
764 * have tearoff menu items.
766 * Note that this only affects menu style combo boxes.
770 g_object_class_install_property (object_class,
772 g_param_spec_boolean ("add-tearoffs",
773 P_("Add tearoffs to menus"),
774 P_("Whether dropdowns should have a tearoff menu item"),
776 GTK_PARAM_READWRITE));
779 * GtkComboBox:has-frame:
781 * The has-frame property controls whether a frame
782 * is drawn around the entry.
786 g_object_class_install_property (object_class,
788 g_param_spec_boolean ("has-frame",
790 P_("Whether the combo box draws a frame around the child"),
792 GTK_PARAM_READWRITE));
794 g_object_class_install_property (object_class,
796 g_param_spec_boolean ("focus-on-click",
797 P_("Focus on click"),
798 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
800 GTK_PARAM_READWRITE));
803 * GtkComboBox:tearoff-title:
805 * A title that may be displayed by the window manager
806 * when the popup is torn-off.
810 g_object_class_install_property (object_class,
812 g_param_spec_string ("tearoff-title",
814 P_("A title that may be displayed by the window manager when the popup is torn-off"),
816 GTK_PARAM_READWRITE));
820 * GtkComboBox:popup-shown:
822 * Whether the combo boxes dropdown is popped up.
823 * Note that this property is mainly useful, because
824 * it allows you to connect to notify::popup-shown.
828 g_object_class_install_property (object_class,
830 g_param_spec_boolean ("popup-shown",
832 P_("Whether the combo's dropdown is shown"),
834 GTK_PARAM_READABLE));
838 * GtkComboBox:button-sensitivity:
840 * Whether the dropdown button is sensitive when
841 * the model is empty.
845 g_object_class_install_property (object_class,
846 PROP_BUTTON_SENSITIVITY,
847 g_param_spec_enum ("button-sensitivity",
848 P_("Button Sensitivity"),
849 P_("Whether the dropdown button is sensitive when the model is empty"),
850 GTK_TYPE_SENSITIVITY_TYPE,
851 GTK_SENSITIVITY_AUTO,
852 GTK_PARAM_READWRITE));
855 * GtkComboBox:has-entry:
857 * Whether the combo box has an entry.
861 g_object_class_install_property (object_class,
863 g_param_spec_boolean ("has-entry",
865 P_("Whether combo box has an entry"),
867 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
870 * GtkComboBox:entry-text-column:
872 * The column in the combo box's model to associate with strings from the entry
873 * if the combo was created with #GtkComboBox:has-entry = %TRUE.
877 g_object_class_install_property (object_class,
878 PROP_ENTRY_TEXT_COLUMN,
879 g_param_spec_int ("entry-text-column",
880 P_("Entry Text Column"),
881 P_("The column in the combo box's model to associate "
882 "with strings from the entry if the combo was "
883 "created with #GtkComboBox:has-entry = %TRUE"),
885 GTK_PARAM_READWRITE));
888 * GtkComboBox:id-column:
890 * The column in the combo box's model that provides string
891 * IDs for the values in the model, if != -1.
895 g_object_class_install_property (object_class,
897 g_param_spec_int ("id-column",
899 P_("The column in the combo box's model that provides "
900 "string IDs for the values in the model"),
902 GTK_PARAM_READWRITE));
905 * GtkComboBox:active-id:
907 * The value of the ID column of the active row.
911 g_object_class_install_property (object_class,
913 g_param_spec_string ("active-id",
915 P_("The value of the id column "
916 "for the active row"),
917 NULL, GTK_PARAM_READWRITE));
920 * GtkComboBox:popup-fixed-width:
922 * Whether the popup's width should be a fixed width matching the
923 * allocated width of the combo box.
927 g_object_class_install_property (object_class,
928 PROP_POPUP_FIXED_WIDTH,
929 g_param_spec_boolean ("popup-fixed-width",
930 P_("Popup Fixed Width"),
931 P_("Whether the popup's width should be a "
932 "fixed width matching the allocated width "
935 GTK_PARAM_READWRITE));
938 * GtkComboBox:cell-area:
940 * The #GtkCellArea used to layout cell renderers for this combo box.
942 * If no area is specified when creating the combo box with gtk_combo_box_new_with_area()
943 * a horizontally oriented #GtkCellAreaBox will be used.
947 g_object_class_install_property (object_class,
949 g_param_spec_object ("cell-area",
951 P_("The GtkCellArea used to layout cells"),
953 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
955 gtk_widget_class_install_style_property (widget_class,
956 g_param_spec_boolean ("appears-as-list",
957 P_("Appears as list"),
958 P_("Whether dropdowns should look like lists rather than menus"),
960 GTK_PARAM_READABLE));
963 * GtkComboBox:arrow-size:
965 * Sets the minimum size of the arrow in the combo box. Note
966 * that the arrow size is coupled to the font size, so in case
967 * a larger font is used, the arrow will be larger than set
972 gtk_widget_class_install_style_property (widget_class,
973 g_param_spec_int ("arrow-size",
975 P_("The minimum size of the arrow in the combo box"),
979 GTK_PARAM_READABLE));
982 * GtkComboBox:arrow-scaling:
984 * Sets the amount of space used up by the combobox arrow,
985 * proportional to the font size.
989 gtk_widget_class_install_style_property (widget_class,
990 g_param_spec_float ("arrow-scaling",
992 P_("The amount of space used by the arrow"),
996 GTK_PARAM_READABLE));
999 * GtkComboBox:shadow-type:
1001 * Which kind of shadow to draw around the combo box.
1005 gtk_widget_class_install_style_property (widget_class,
1006 g_param_spec_enum ("shadow-type",
1008 P_("Which kind of shadow to draw around the combo box"),
1009 GTK_TYPE_SHADOW_TYPE,
1011 GTK_PARAM_READABLE));
1013 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1015 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1019 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1021 parent_buildable_iface = g_type_interface_peek_parent (iface);
1022 iface->add_child = _gtk_cell_layout_buildable_add_child;
1023 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1024 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1025 iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1029 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1031 iface->get_area = gtk_combo_box_cell_layout_get_area;
1035 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1037 iface->start_editing = gtk_combo_box_start_editing;
1041 gtk_combo_box_init (GtkComboBox *combo_box)
1043 GtkComboBoxPrivate *priv;
1045 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1047 GtkComboBoxPrivate);
1048 priv = combo_box->priv;
1050 priv->wrap_width = 0;
1053 priv->active_row = NULL;
1054 priv->col_column = -1;
1055 priv->row_column = -1;
1057 priv->popup_shown = FALSE;
1058 priv->add_tearoffs = FALSE;
1059 priv->has_frame = TRUE;
1060 priv->is_cell_renderer = FALSE;
1061 priv->editing_canceled = FALSE;
1062 priv->auto_scroll = FALSE;
1063 priv->focus_on_click = TRUE;
1064 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1065 priv->has_entry = FALSE;
1066 priv->popup_fixed_width = TRUE;
1068 priv->text_column = -1;
1069 priv->text_renderer = NULL;
1070 priv->id_column = -1;
1074 gtk_combo_box_set_property (GObject *object,
1076 const GValue *value,
1079 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1080 GtkComboBoxPrivate *priv = combo_box->priv;
1086 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1089 case PROP_WRAP_WIDTH:
1090 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1093 case PROP_ROW_SPAN_COLUMN:
1094 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1097 case PROP_COLUMN_SPAN_COLUMN:
1098 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1102 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1105 case PROP_ADD_TEAROFFS:
1106 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1109 case PROP_HAS_FRAME:
1110 priv->has_frame = g_value_get_boolean (value);
1112 if (priv->has_entry)
1116 child = gtk_bin_get_child (GTK_BIN (combo_box));
1118 gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1123 case PROP_FOCUS_ON_CLICK:
1124 gtk_combo_box_set_focus_on_click (combo_box,
1125 g_value_get_boolean (value));
1128 case PROP_TEAROFF_TITLE:
1129 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1132 case PROP_POPUP_SHOWN:
1133 if (g_value_get_boolean (value))
1134 gtk_combo_box_popup (combo_box);
1136 gtk_combo_box_popdown (combo_box);
1139 case PROP_BUTTON_SENSITIVITY:
1140 gtk_combo_box_set_button_sensitivity (combo_box,
1141 g_value_get_enum (value));
1144 case PROP_POPUP_FIXED_WIDTH:
1145 gtk_combo_box_set_popup_fixed_width (combo_box,
1146 g_value_get_boolean (value));
1149 case PROP_EDITING_CANCELED:
1150 priv->editing_canceled = g_value_get_boolean (value);
1153 case PROP_HAS_ENTRY:
1154 priv->has_entry = g_value_get_boolean (value);
1157 case PROP_ENTRY_TEXT_COLUMN:
1158 gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1161 case PROP_ID_COLUMN:
1162 gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1165 case PROP_ACTIVE_ID:
1166 gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1169 case PROP_CELL_AREA:
1170 /* Construct-only, can only be assigned once */
1171 area = g_value_get_object (value);
1174 if (priv->area != NULL)
1176 g_warning ("cell-area has already been set, ignoring construct property");
1177 g_object_ref_sink (area);
1178 g_object_unref (area);
1181 priv->area = g_object_ref_sink (area);
1186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1192 gtk_combo_box_get_property (GObject *object,
1197 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1198 GtkComboBoxPrivate *priv = combo_box->priv;
1203 g_value_set_object (value, combo_box->priv->model);
1206 case PROP_WRAP_WIDTH:
1207 g_value_set_int (value, combo_box->priv->wrap_width);
1210 case PROP_ROW_SPAN_COLUMN:
1211 g_value_set_int (value, combo_box->priv->row_column);
1214 case PROP_COLUMN_SPAN_COLUMN:
1215 g_value_set_int (value, combo_box->priv->col_column);
1219 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1222 case PROP_ADD_TEAROFFS:
1223 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1226 case PROP_HAS_FRAME:
1227 g_value_set_boolean (value, combo_box->priv->has_frame);
1230 case PROP_FOCUS_ON_CLICK:
1231 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1234 case PROP_TEAROFF_TITLE:
1235 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1238 case PROP_POPUP_SHOWN:
1239 g_value_set_boolean (value, combo_box->priv->popup_shown);
1242 case PROP_BUTTON_SENSITIVITY:
1243 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1246 case PROP_POPUP_FIXED_WIDTH:
1247 g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1250 case PROP_EDITING_CANCELED:
1251 g_value_set_boolean (value, priv->editing_canceled);
1254 case PROP_HAS_ENTRY:
1255 g_value_set_boolean (value, priv->has_entry);
1258 case PROP_ENTRY_TEXT_COLUMN:
1259 g_value_set_int (value, priv->text_column);
1262 case PROP_ID_COLUMN:
1263 g_value_set_int (value, priv->id_column);
1266 case PROP_ACTIVE_ID:
1267 g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1270 case PROP_CELL_AREA:
1271 g_value_set_object (value, priv->area);
1275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1281 gtk_combo_box_state_flags_changed (GtkWidget *widget,
1282 GtkStateFlags previous)
1284 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1285 GtkComboBoxPrivate *priv = combo_box->priv;
1287 if (gtk_widget_get_realized (widget))
1289 if (priv->tree_view && priv->cell_view)
1291 GtkStyleContext *context;
1292 GtkStateFlags state;
1295 context = gtk_widget_get_style_context (widget);
1296 state = gtk_widget_get_state_flags (widget);
1297 gtk_style_context_get_background_color (context, state, &color);
1299 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1303 gtk_widget_queue_draw (widget);
1307 gtk_combo_box_button_state_flags_changed (GtkWidget *widget,
1308 GtkStateFlags previous,
1311 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1312 GtkComboBoxPrivate *priv = combo_box->priv;
1314 if (gtk_widget_get_realized (widget))
1316 if (!priv->tree_view && priv->cell_view)
1317 gtk_widget_set_state_flags (priv->cell_view,
1318 gtk_widget_get_state_flags (widget),
1322 gtk_widget_queue_draw (widget);
1326 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1328 gtk_container_forall (GTK_CONTAINER (combo_box),
1329 (GtkCallback) gtk_widget_reset_style,
1334 gtk_combo_box_direction_changed (GtkWidget *widget,
1335 GtkTextDirection previous_direction)
1337 gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1340 static GtkWidgetPath *
1341 gtk_combo_box_get_path_for_child (GtkContainer *container,
1344 GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1345 GtkWidgetPath *path;
1347 gboolean found = FALSE;
1348 GList *visible_children, *l;
1349 GtkWidgetPath *sibling_path;
1352 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
1354 if (gtk_widget_get_visible (child))
1356 visible_children = NULL;
1358 if (priv->button && gtk_widget_get_visible (priv->button))
1359 visible_children = g_list_prepend (visible_children, priv->button);
1361 if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1362 visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1364 widget = gtk_bin_get_child (GTK_BIN (container));
1365 if (widget && gtk_widget_get_visible (widget))
1366 visible_children = g_list_prepend (visible_children, widget);
1368 if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1369 visible_children = g_list_reverse (visible_children);
1373 for (l = visible_children; l; l = l->next)
1377 if (widget == child)
1389 sibling_path = gtk_widget_path_new ();
1391 for (l = visible_children; l; l = l->next)
1392 gtk_widget_path_append_for_widget (sibling_path, l->data);
1394 gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1396 g_list_free (visible_children);
1397 gtk_widget_path_unref (sibling_path);
1401 gtk_widget_path_append_for_widget (path, child);
1408 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1410 GtkComboBoxPrivate *priv = combo_box->priv;
1411 gboolean appears_as_list;
1413 /* if wrap_width > 0, then we are in grid-mode and forced to use
1416 if (priv->wrap_width)
1417 appears_as_list = FALSE;
1419 gtk_widget_style_get (GTK_WIDGET (combo_box),
1420 "appears-as-list", &appears_as_list,
1423 if (appears_as_list)
1425 /* Destroy all the menu mode widgets, if they exist. */
1426 if (GTK_IS_MENU (priv->popup_widget))
1427 gtk_combo_box_menu_destroy (combo_box);
1429 /* Create the list mode widgets, if they don't already exist. */
1430 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1431 gtk_combo_box_list_setup (combo_box);
1435 /* Destroy all the list mode widgets, if they exist. */
1436 if (GTK_IS_TREE_VIEW (priv->tree_view))
1437 gtk_combo_box_list_destroy (combo_box);
1439 /* Create the menu mode widgets, if they don't already exist. */
1440 if (!GTK_IS_MENU (priv->popup_widget))
1441 gtk_combo_box_menu_setup (combo_box, TRUE);
1444 gtk_widget_style_get (GTK_WIDGET (combo_box),
1445 "shadow-type", &priv->shadow_type,
1450 gtk_combo_box_style_updated (GtkWidget *widget)
1452 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1453 GtkComboBoxPrivate *priv = combo_box->priv;
1456 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1458 gtk_combo_box_check_appearance (combo_box);
1460 if (priv->tree_view && priv->cell_view)
1462 GtkStyleContext *context;
1463 GtkStateFlags state;
1466 context = gtk_widget_get_style_context (widget);
1467 state = gtk_widget_get_state_flags (widget);
1468 gtk_style_context_get_background_color (context, state, &color);
1470 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1473 child = gtk_bin_get_child (GTK_BIN (combo_box));
1474 if (GTK_IS_ENTRY (child))
1475 g_object_set (child, "shadow-type",
1476 GTK_SHADOW_NONE == priv->shadow_type ?
1477 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1481 gtk_combo_box_button_toggled (GtkWidget *widget,
1484 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1486 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1488 if (!combo_box->priv->popup_in_progress)
1489 gtk_combo_box_popup (combo_box);
1492 gtk_combo_box_popdown (combo_box);
1496 gtk_combo_box_add (GtkContainer *container,
1499 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1500 GtkComboBoxPrivate *priv = combo_box->priv;
1502 if (priv->has_entry && !GTK_IS_ENTRY (widget))
1504 g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1505 "(need an instance of GtkEntry or of a subclass)",
1506 G_OBJECT_TYPE_NAME (widget));
1510 if (priv->cell_view &&
1511 gtk_widget_get_parent (priv->cell_view))
1513 gtk_widget_unparent (priv->cell_view);
1514 _gtk_bin_set_child (GTK_BIN (container), NULL);
1515 gtk_widget_queue_resize (GTK_WIDGET (container));
1518 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1519 _gtk_bin_set_child (GTK_BIN (container), widget);
1521 if (priv->cell_view &&
1522 widget != priv->cell_view)
1524 /* since the cell_view was unparented, it's gone now */
1525 priv->cell_view = NULL;
1527 if (!priv->tree_view && priv->separator)
1529 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1531 priv->separator = NULL;
1533 gtk_widget_queue_resize (GTK_WIDGET (container));
1535 else if (priv->cell_view_frame)
1537 gtk_widget_unparent (priv->cell_view_frame);
1538 priv->cell_view_frame = NULL;
1543 if (priv->has_entry)
1545 /* this flag is a hack to tell the entry to fill its allocation.
1547 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1549 g_signal_connect (widget, "changed",
1550 G_CALLBACK (gtk_combo_box_entry_contents_changed),
1553 gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1558 gtk_combo_box_remove (GtkContainer *container,
1561 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1562 GtkComboBoxPrivate *priv = combo_box->priv;
1564 gboolean appears_as_list;
1566 if (priv->has_entry)
1568 GtkWidget *child_widget;
1570 child_widget = gtk_bin_get_child (GTK_BIN (container));
1571 if (widget && widget == child_widget)
1573 g_signal_handlers_disconnect_by_func (widget,
1574 gtk_combo_box_entry_contents_changed,
1576 _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1580 if (widget == priv->cell_view)
1581 priv->cell_view = NULL;
1583 gtk_widget_unparent (widget);
1584 _gtk_bin_set_child (GTK_BIN (container), NULL);
1586 if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1589 gtk_widget_queue_resize (GTK_WIDGET (container));
1591 if (!priv->tree_view)
1592 appears_as_list = FALSE;
1594 appears_as_list = TRUE;
1596 if (appears_as_list)
1597 gtk_combo_box_list_destroy (combo_box);
1598 else if (GTK_IS_MENU (priv->popup_widget))
1600 gtk_combo_box_menu_destroy (combo_box);
1601 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1602 priv->popup_widget = NULL;
1605 if (!priv->cell_view)
1607 priv->cell_view = gtk_cell_view_new ();
1608 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1609 _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1611 gtk_widget_show (priv->cell_view);
1612 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1617 if (appears_as_list)
1618 gtk_combo_box_list_setup (combo_box);
1620 gtk_combo_box_menu_setup (combo_box, TRUE);
1622 if (gtk_tree_row_reference_valid (priv->active_row))
1624 path = gtk_tree_row_reference_get_path (priv->active_row);
1625 gtk_combo_box_set_active_internal (combo_box, path);
1626 gtk_tree_path_free (path);
1629 gtk_combo_box_set_active_internal (combo_box, NULL);
1633 gtk_combo_box_menu_show (GtkWidget *menu,
1636 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1637 GtkComboBoxPrivate *priv = combo_box->priv;
1639 gtk_combo_box_child_show (menu, user_data);
1641 priv->popup_in_progress = TRUE;
1642 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1644 priv->popup_in_progress = FALSE;
1648 gtk_combo_box_menu_hide (GtkWidget *menu,
1651 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1653 gtk_combo_box_child_hide (menu,user_data);
1655 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1660 gtk_combo_box_detacher (GtkWidget *widget,
1663 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1664 GtkComboBoxPrivate *priv = combo_box->priv;
1666 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1668 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1669 gtk_combo_box_menu_show,
1671 g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1672 gtk_combo_box_menu_hide,
1675 priv->popup_widget = NULL;
1679 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1682 GtkComboBoxPrivate *priv = combo_box->priv;
1684 if (GTK_IS_MENU (priv->popup_widget))
1686 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1687 priv->popup_widget = NULL;
1689 else if (priv->popup_widget)
1691 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1692 priv->popup_widget);
1693 g_object_unref (priv->popup_widget);
1694 priv->popup_widget = NULL;
1697 if (GTK_IS_MENU (popup))
1699 if (priv->popup_window)
1701 gtk_widget_destroy (priv->popup_window);
1702 priv->popup_window = NULL;
1705 priv->popup_widget = popup;
1708 * Note that we connect to show/hide on the toplevel, not the
1709 * menu itself, since the menu is not shown/hidden when it is
1710 * popped up while torn-off.
1712 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1713 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1714 g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1715 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1717 gtk_menu_attach_to_widget (GTK_MENU (popup),
1718 GTK_WIDGET (combo_box),
1719 gtk_combo_box_detacher);
1723 if (!priv->popup_window)
1725 GtkWidget *toplevel;
1727 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1728 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1730 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1731 GDK_WINDOW_TYPE_HINT_COMBO);
1733 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1734 G_CALLBACK (gtk_combo_box_child_show),
1736 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1737 G_CALLBACK (gtk_combo_box_child_hide),
1740 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1741 if (GTK_IS_WINDOW (toplevel))
1743 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1744 GTK_WINDOW (priv->popup_window));
1745 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1746 GTK_WINDOW (toplevel));
1749 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1750 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1751 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1753 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1755 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1758 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1761 gtk_widget_show (priv->scrolled_window);
1763 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1764 priv->scrolled_window);
1767 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1770 gtk_widget_show (popup);
1771 g_object_ref (popup);
1772 priv->popup_widget = popup;
1777 get_widget_padding (GtkWidget *widget,
1780 GtkStyleContext *context;
1781 GtkStateFlags state;
1783 context = gtk_widget_get_style_context (widget);
1784 state = gtk_style_context_get_state (context);
1786 gtk_style_context_get_padding (context, state, padding);
1790 gtk_combo_box_menu_position_below (GtkMenu *menu,
1796 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1797 GtkAllocation child_allocation;
1803 GdkRectangle monitor;
1806 /* FIXME: is using the size request here broken? */
1807 child = gtk_bin_get_child (GTK_BIN (combo_box));
1811 gtk_widget_get_allocation (child, &child_allocation);
1813 if (!gtk_widget_get_has_window (child))
1815 sx += child_allocation.x;
1816 sy += child_allocation.y;
1819 gdk_window_get_root_coords (gtk_widget_get_window (child),
1821 get_widget_padding (GTK_WIDGET (combo_box), &padding);
1824 if (combo_box->priv->popup_fixed_width)
1825 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1827 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1829 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1832 *x = sx + child_allocation.width - req.width;
1835 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1836 monitor_num = gdk_screen_get_monitor_at_window (screen,
1837 gtk_widget_get_window (GTK_WIDGET (combo_box)));
1838 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1842 else if (*x + req.width > monitor.x + monitor.width)
1843 *x = monitor.x + monitor.width - req.width;
1845 if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1846 *y += child_allocation.height;
1847 else if (*y - monitor.y >= req.height)
1849 else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1850 *y += child_allocation.height;
1858 gtk_combo_box_menu_position_over (GtkMenu *menu,
1864 GtkComboBox *combo_box;
1868 GtkAllocation allocation;
1869 GtkAllocation child_allocation;
1876 combo_box = GTK_COMBO_BOX (user_data);
1877 widget = GTK_WIDGET (combo_box);
1879 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1881 gtk_widget_get_allocation (widget, &allocation);
1883 menu_xpos = allocation.x;
1884 menu_ypos = allocation.y + allocation.height / 2 - 2;
1886 if (combo_box->priv->popup_fixed_width)
1887 gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1889 gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1893 gtk_widget_get_allocation (active, &child_allocation);
1894 menu_ypos -= child_allocation.height / 2;
1897 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1900 child = children->data;
1902 if (active == child)
1905 if (gtk_widget_get_visible (child))
1907 gtk_widget_get_allocation (child, &child_allocation);
1909 menu_ypos -= child_allocation.height;
1912 children = children->next;
1915 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1916 menu_xpos = menu_xpos + allocation.width - menu_width;
1918 gdk_window_get_root_coords (gtk_widget_get_window (widget),
1919 menu_xpos, menu_ypos,
1920 &menu_xpos, &menu_ypos);
1922 /* Clamp the position on screen */
1923 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1927 else if ((menu_xpos + menu_width) > screen_width)
1928 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1937 gtk_combo_box_menu_position (GtkMenu *menu,
1943 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1944 GtkComboBoxPrivate *priv = combo_box->priv;
1945 GtkWidget *menu_item;
1947 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1948 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1951 /* FIXME handle nested menus better */
1952 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1954 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1957 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1960 if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
1961 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
1962 GDK_WINDOW_TYPE_HINT_COMBO);
1966 gtk_combo_box_list_position (GtkComboBox *combo_box,
1972 GtkComboBoxPrivate *priv = combo_box->priv;
1973 GtkAllocation allocation;
1976 GdkRectangle monitor;
1977 GtkRequisition popup_req;
1978 GtkPolicyType hpolicy, vpolicy;
1981 /* under windows, the drop down list is as wide as the combo box itself.
1983 GtkWidget *widget = GTK_WIDGET (combo_box);
1987 gtk_widget_get_allocation (widget, &allocation);
1989 if (!gtk_widget_get_has_window (widget))
1995 window = gtk_widget_get_window (widget);
1997 gdk_window_get_root_coords (gtk_widget_get_window (widget),
2000 *width = allocation.width;
2002 hpolicy = vpolicy = GTK_POLICY_NEVER;
2003 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2006 if (combo_box->priv->popup_fixed_width)
2008 gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2010 if (popup_req.width > *width)
2012 hpolicy = GTK_POLICY_ALWAYS;
2013 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2019 /* XXX This code depends on treeviews properly reporting their natural width
2020 * list-mode menus won't fill up to their natural width until then */
2021 gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2023 if (popup_req.width > *width)
2025 hpolicy = GTK_POLICY_NEVER;
2026 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2029 *width = popup_req.width;
2033 *height = popup_req.height;
2035 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2036 monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2037 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2039 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2040 *x = *x + allocation.width - *width;
2044 else if (*x + *width > monitor.x + monitor.width)
2045 *x = monitor.x + monitor.width - *width;
2047 if (*y + allocation.height + *height <= monitor.y + monitor.height)
2048 *y += allocation.height;
2049 else if (*y - *height >= monitor.y)
2051 else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2053 *y += allocation.height;
2054 *height = monitor.y + monitor.height - *y;
2058 *height = *y - monitor.y;
2062 if (popup_req.height > *height)
2064 vpolicy = GTK_POLICY_ALWAYS;
2066 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2072 cell_layout_is_sensitive (GtkCellLayout *layout)
2074 GList *cells, *list;
2077 cells = gtk_cell_layout_get_cells (layout);
2080 for (list = cells; list; list = list->next)
2082 g_object_get (list->data, "sensitive", &sensitive, NULL);
2087 g_list_free (cells);
2093 cell_is_sensitive (GtkCellRenderer *cell,
2096 gboolean *sensitive = data;
2098 g_object_get (cell, "sensitive", sensitive, NULL);
2104 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2107 GtkComboBoxPrivate *priv = combo_box->priv;
2109 if (priv->row_separator_func)
2111 if (priv->row_separator_func (priv->model, iter,
2112 priv->row_separator_data))
2120 gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2124 gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2133 update_menu_sensitivity (GtkComboBox *combo_box,
2136 GtkComboBoxPrivate *priv = combo_box->priv;
2137 GList *children, *child;
2138 GtkWidget *item, *submenu, *separator;
2139 GtkWidget *cell_view;
2145 children = gtk_container_get_children (GTK_CONTAINER (menu));
2147 for (child = children; child; child = child->next)
2149 item = GTK_WIDGET (child->data);
2150 cell_view = gtk_bin_get_child (GTK_BIN (item));
2152 if (!GTK_IS_CELL_VIEW (cell_view))
2155 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2156 if (submenu != NULL)
2158 gtk_widget_set_sensitive (item, TRUE);
2159 update_menu_sensitivity (combo_box, submenu);
2163 sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2165 if (menu != priv->popup_widget && child == children)
2167 separator = GTK_WIDGET (child->next->data);
2168 g_object_set (item, "visible", sensitive, NULL);
2169 g_object_set (separator, "visible", sensitive, NULL);
2172 gtk_widget_set_sensitive (item, sensitive);
2176 g_list_free (children);
2180 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2182 guint32 activate_time)
2184 GtkComboBoxPrivate *priv = combo_box->priv;
2187 gint width, min_width, nat_width;
2189 update_menu_sensitivity (combo_box, priv->popup_widget);
2192 if (gtk_tree_row_reference_valid (priv->active_row))
2194 path = gtk_tree_row_reference_get_path (priv->active_row);
2195 active_item = gtk_tree_path_get_indices (path)[0];
2196 gtk_tree_path_free (path);
2198 if (priv->add_tearoffs)
2202 /* FIXME handle nested menus better */
2203 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2205 if (priv->wrap_width == 0)
2207 GtkAllocation allocation;
2209 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2210 width = allocation.width;
2211 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2212 gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2214 if (combo_box->priv->popup_fixed_width)
2215 width = MAX (width, min_width);
2217 width = MAX (width, nat_width);
2219 gtk_widget_set_size_request (priv->popup_widget, width, -1);
2222 gtk_menu_popup (GTK_MENU (priv->popup_widget),
2224 gtk_combo_box_menu_position, combo_box,
2225 button, activate_time);
2229 popup_grab_on_window (GdkWindow *window,
2230 GdkDevice *keyboard,
2232 guint32 activate_time)
2235 gdk_device_grab (keyboard, window,
2236 GDK_OWNERSHIP_WINDOW, TRUE,
2237 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2238 NULL, activate_time) != GDK_GRAB_SUCCESS)
2242 gdk_device_grab (pointer, window,
2243 GDK_OWNERSHIP_WINDOW, TRUE,
2244 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2245 GDK_POINTER_MOTION_MASK,
2246 NULL, activate_time) != GDK_GRAB_SUCCESS)
2249 gdk_device_ungrab (keyboard, activate_time);
2258 * gtk_combo_box_popup:
2259 * @combo_box: a #GtkComboBox
2261 * Pops up the menu or dropdown list of @combo_box.
2263 * This function is mostly intended for use by accessibility technologies;
2264 * applications should have little use for it.
2269 gtk_combo_box_popup (GtkComboBox *combo_box)
2271 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2273 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2277 * gtk_combo_box_popup_for_device:
2278 * @combo_box: a #GtkComboBox
2279 * @device: a #GdkDevice
2281 * Pops up the menu or dropdown list of @combo_box, the popup window
2282 * will be grabbed so only @device and its associated pointer/keyboard
2283 * are the only #GdkDevice<!-- -->s able to send events to it.
2288 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2291 GtkComboBoxPrivate *priv = combo_box->priv;
2292 gint x, y, width, height;
2293 GtkTreePath *path = NULL, *ppath;
2294 GtkWidget *toplevel;
2295 GdkDevice *keyboard, *pointer;
2298 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2299 g_return_if_fail (GDK_IS_DEVICE (device));
2301 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2304 if (gtk_widget_get_mapped (priv->popup_widget))
2307 if (priv->grab_pointer && priv->grab_keyboard)
2310 time = gtk_get_current_event_time ();
2312 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2315 pointer = gdk_device_get_associated_device (device);
2320 keyboard = gdk_device_get_associated_device (device);
2323 if (GTK_IS_MENU (priv->popup_widget))
2325 gtk_combo_box_menu_popup (combo_box,
2326 priv->activate_button,
2327 priv->activate_time);
2331 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2332 if (GTK_IS_WINDOW (toplevel))
2333 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2334 GTK_WINDOW (priv->popup_window));
2336 gtk_widget_show_all (priv->scrolled_window);
2337 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2339 gtk_widget_set_size_request (priv->popup_window, width, height);
2340 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2342 if (gtk_tree_row_reference_valid (priv->active_row))
2344 path = gtk_tree_row_reference_get_path (priv->active_row);
2345 ppath = gtk_tree_path_copy (path);
2346 if (gtk_tree_path_up (ppath))
2347 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2349 gtk_tree_path_free (ppath);
2351 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2355 gtk_widget_show (priv->popup_window);
2359 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2361 gtk_tree_path_free (path);
2364 gtk_widget_grab_focus (priv->popup_window);
2365 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2368 if (!gtk_widget_has_focus (priv->tree_view))
2369 gtk_widget_grab_focus (priv->tree_view);
2371 if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2372 keyboard, pointer, time))
2374 gtk_widget_hide (priv->popup_window);
2378 gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2379 priv->grab_pointer = pointer;
2380 priv->grab_keyboard = keyboard;
2384 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2388 device = gtk_get_current_event_device ();
2392 GdkDeviceManager *device_manager;
2393 GdkDisplay *display;
2396 display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2397 device_manager = gdk_display_get_device_manager (display);
2399 /* No device was set, pick the first master device */
2400 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2401 device = devices->data;
2402 g_list_free (devices);
2405 gtk_combo_box_popup_for_device (combo_box, device);
2409 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2411 if (combo_box->priv->popup_shown)
2413 gtk_combo_box_popdown (combo_box);
2421 * gtk_combo_box_popdown:
2422 * @combo_box: a #GtkComboBox
2424 * Hides the menu or dropdown list of @combo_box.
2426 * This function is mostly intended for use by accessibility technologies;
2427 * applications should have little use for it.
2432 gtk_combo_box_popdown (GtkComboBox *combo_box)
2434 GtkComboBoxPrivate *priv = combo_box->priv;
2436 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2438 if (GTK_IS_MENU (priv->popup_widget))
2440 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2444 if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2447 gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2448 gtk_widget_hide (priv->popup_window);
2449 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2452 priv->grab_pointer = NULL;
2453 priv->grab_keyboard = NULL;
2456 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2457 gtk_widget_get_preferred_size (combo_box->priv->button, \
2461 child.x = allocation->x + padding.right; \
2463 child.x = allocation->x + allocation->width - req.width - padding.left; \
2465 child.y = allocation->y + padding.top; \
2466 child.width = req.width; \
2467 child.height = allocation->height - (padding.top + padding.bottom); \
2468 child.width = MAX (1, child.width); \
2469 child.height = MAX (1, child.height); \
2471 gtk_widget_size_allocate (combo_box->priv->button, &child);
2475 gtk_combo_box_size_allocate (GtkWidget *widget,
2476 GtkAllocation *allocation)
2478 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2479 GtkComboBoxPrivate *priv = combo_box->priv;
2480 GtkWidget *child_widget;
2481 gint focus_width, focus_pad;
2482 GtkAllocation child;
2484 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2487 gtk_widget_set_allocation (widget, allocation);
2488 child_widget = gtk_bin_get_child (GTK_BIN (widget));
2489 get_widget_padding (widget, &padding);
2491 gtk_widget_style_get (widget,
2492 "focus-line-width", &focus_width,
2493 "focus-padding", &focus_pad,
2496 if (!priv->tree_view)
2498 if (priv->cell_view)
2500 GtkBorder button_padding;
2504 gtk_widget_size_allocate (priv->button, allocation);
2507 allocation->x += padding.left;
2508 allocation->y += padding.top;
2509 allocation->width -= padding.left + padding.right;
2510 allocation->height -= padding.top + padding.bottom;
2512 /* set some things ready */
2513 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2514 get_widget_padding (priv->button, &button_padding);
2516 child.x = allocation->x;
2517 child.y = allocation->y;
2518 width = allocation->width;
2519 child.height = allocation->height;
2521 if (!priv->is_cell_renderer)
2523 child.x += border_width + button_padding.left + focus_width + focus_pad;
2524 child.y += border_width + button_padding.top + focus_width + focus_pad;
2525 width -= (2 * (border_width + focus_width + focus_pad)) +
2526 button_padding.left + button_padding.right;
2527 child.height -= (2 * (border_width + focus_width + focus_pad)) +
2528 button_padding.top + button_padding.bottom;
2531 /* handle the children */
2532 gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2533 child.width = req.width;
2535 child.x += width - req.width - button_padding.right;
2536 child.width = MAX (1, child.width);
2537 child.height = MAX (1, child.height);
2538 gtk_widget_size_allocate (priv->arrow, &child);
2540 child.x += req.width;
2541 gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2542 child.width = req.width;
2544 child.x -= req.width;
2545 child.width = MAX (1, child.width);
2546 child.height = MAX (1, child.height);
2547 gtk_widget_size_allocate (priv->separator, &child);
2551 child.x += req.width;
2552 child.width = allocation->x + allocation->width
2553 - (border_width + focus_width + focus_pad)
2558 child.width = child.x;
2559 child.x = allocation->x
2560 + border_width + button_padding.left + focus_width + focus_pad;
2561 child.width -= child.x;
2564 if (gtk_widget_get_visible (priv->popup_widget))
2566 gint width, menu_width;
2568 if (priv->wrap_width == 0)
2570 GtkAllocation combo_box_allocation;
2572 gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2573 width = combo_box_allocation.width;
2574 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2576 if (combo_box->priv->popup_fixed_width)
2577 gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2579 gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2581 gtk_widget_set_size_request (priv->popup_widget,
2582 MAX (width, menu_width), -1);
2585 /* reposition the menu after giving it a new width */
2586 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2589 child.width = MAX (1, child.width);
2590 child.height = MAX (1, child.height);
2591 gtk_widget_size_allocate (child_widget, &child);
2595 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2598 child.x = allocation->x + req.width + padding.right;
2600 child.x = allocation->x + padding.left;
2601 child.y = allocation->y + padding.top;
2602 child.width = allocation->width - req.width - (padding.left + padding.right);
2603 child.width = MAX (1, child.width);
2604 child.height = MAX (1, child.height);
2605 gtk_widget_size_allocate (child_widget, &child);
2611 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2614 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2618 child.x = allocation->x + req.width;
2620 child.x = allocation->x;
2622 child.y = allocation->y;
2623 child.width = allocation->width - req.width;
2624 child.height = allocation->height;
2626 if (priv->cell_view_frame)
2628 child.x += padding.left + border_width;
2629 child.y += padding.top + border_width;
2630 child.width = MAX (1, child.width - (2 * border_width) - (padding.left + padding.right));
2631 child.height = MAX (1, child.height - (2 * border_width) - (padding.top + padding.bottom));
2632 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2635 if (priv->has_frame)
2637 GtkBorder frame_padding;
2639 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2640 get_widget_padding (priv->cell_view_frame, &frame_padding);
2642 child.x += border_width + frame_padding.left;
2643 child.y += border_width + frame_padding.right;
2644 child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2645 child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2650 child.x += padding.left + border_width;
2651 child.y += padding.top + border_width;
2652 child.width -= (2 * border_width) - (padding.left + padding.right);
2653 child.height -= (2 * border_width) - (padding.top + padding.bottom);
2656 if (gtk_widget_get_visible (priv->popup_window))
2658 gint x, y, width, height;
2659 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2660 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2661 gtk_widget_set_size_request (priv->popup_window, width, height);
2664 child.width = MAX (1, child.width);
2665 child.height = MAX (1, child.height);
2666 gtk_widget_size_allocate (child_widget, &child);
2670 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2673 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2675 GtkComboBoxPrivate *priv = combo_box->priv;
2679 g_signal_handler_disconnect (priv->model,
2681 g_signal_handler_disconnect (priv->model,
2683 g_signal_handler_disconnect (priv->model,
2684 priv->reordered_id);
2685 g_signal_handler_disconnect (priv->model,
2691 g_object_unref (priv->model);
2695 if (priv->active_row)
2697 gtk_tree_row_reference_free (priv->active_row);
2698 priv->active_row = NULL;
2701 if (priv->cell_view)
2702 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2706 gtk_combo_box_forall (GtkContainer *container,
2707 gboolean include_internals,
2708 GtkCallback callback,
2709 gpointer callback_data)
2711 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2712 GtkComboBoxPrivate *priv = combo_box->priv;
2715 if (include_internals)
2718 (* callback) (priv->button, callback_data);
2719 if (priv->cell_view_frame)
2720 (* callback) (priv->cell_view_frame, callback_data);
2723 child = gtk_bin_get_child (GTK_BIN (container));
2725 (* callback) (child, callback_data);
2729 gtk_combo_box_child_show (GtkWidget *widget,
2730 GtkComboBox *combo_box)
2732 GtkComboBoxPrivate *priv = combo_box->priv;
2734 priv->popup_shown = TRUE;
2735 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2739 gtk_combo_box_child_hide (GtkWidget *widget,
2740 GtkComboBox *combo_box)
2742 GtkComboBoxPrivate *priv = combo_box->priv;
2744 priv->popup_shown = FALSE;
2745 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2749 gtk_combo_box_draw (GtkWidget *widget,
2752 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2753 GtkComboBoxPrivate *priv = combo_box->priv;
2755 if (priv->shadow_type != GTK_SHADOW_NONE)
2757 GtkStyleContext *context;
2758 GtkStateFlags state;
2760 context = gtk_widget_get_style_context (widget);
2761 state = gtk_widget_get_state_flags (widget);
2762 gtk_style_context_set_state (context, state);
2764 gtk_render_background (context, cr, 0, 0,
2765 gtk_widget_get_allocated_width (widget),
2766 gtk_widget_get_allocated_height (widget));
2767 gtk_render_frame (context, cr, 0, 0,
2768 gtk_widget_get_allocated_width (widget),
2769 gtk_widget_get_allocated_height (widget));
2772 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2775 if (priv->tree_view && priv->cell_view_frame)
2777 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2778 priv->cell_view_frame, cr);
2781 gtk_container_propagate_draw (GTK_CONTAINER (widget),
2782 gtk_bin_get_child (GTK_BIN (widget)),
2798 path_visible (GtkTreeView *view,
2804 /* Note that we rely on the fact that collapsed rows don't have nodes
2806 return _gtk_tree_view_find_node (view, path, &tree, &node);
2810 tree_next_func (GtkTreeModel *model,
2815 SearchData *search_data = (SearchData *)data;
2817 if (search_data->found)
2819 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2822 if (search_data->visible &&
2823 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2826 search_data->set = TRUE;
2827 search_data->iter = *iter;
2832 if (gtk_tree_path_compare (path, search_data->path) == 0)
2833 search_data->found = TRUE;
2839 tree_next (GtkComboBox *combo,
2840 GtkTreeModel *model,
2845 SearchData search_data;
2847 search_data.combo = combo;
2848 search_data.path = gtk_tree_model_get_path (model, iter);
2849 search_data.visible = visible;
2850 search_data.found = FALSE;
2851 search_data.set = FALSE;
2853 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2855 *next = search_data.iter;
2857 gtk_tree_path_free (search_data.path);
2859 return search_data.set;
2863 tree_prev_func (GtkTreeModel *model,
2868 SearchData *search_data = (SearchData *)data;
2870 if (gtk_tree_path_compare (path, search_data->path) == 0)
2872 search_data->found = TRUE;
2876 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2879 if (search_data->visible &&
2880 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2883 search_data->set = TRUE;
2884 search_data->iter = *iter;
2890 tree_prev (GtkComboBox *combo,
2891 GtkTreeModel *model,
2896 SearchData search_data;
2898 search_data.combo = combo;
2899 search_data.path = gtk_tree_model_get_path (model, iter);
2900 search_data.visible = visible;
2901 search_data.found = FALSE;
2902 search_data.set = FALSE;
2904 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2906 *prev = search_data.iter;
2908 gtk_tree_path_free (search_data.path);
2910 return search_data.set;
2914 tree_last_func (GtkTreeModel *model,
2919 SearchData *search_data = (SearchData *)data;
2921 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2924 /* Note that we rely on the fact that collapsed rows don't have nodes
2926 if (search_data->visible &&
2927 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2930 search_data->set = TRUE;
2931 search_data->iter = *iter;
2937 tree_last (GtkComboBox *combo,
2938 GtkTreeModel *model,
2942 SearchData search_data;
2944 search_data.combo = combo;
2945 search_data.visible = visible;
2946 search_data.set = FALSE;
2948 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2950 *last = search_data.iter;
2952 return search_data.set;
2957 tree_first_func (GtkTreeModel *model,
2962 SearchData *search_data = (SearchData *)data;
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_first (GtkComboBox *combo,
2979 GtkTreeModel *model,
2983 SearchData search_data;
2985 search_data.combo = combo;
2986 search_data.visible = visible;
2987 search_data.set = FALSE;
2989 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2991 *first = search_data.iter;
2993 return search_data.set;
2997 gtk_combo_box_scroll_event (GtkWidget *widget,
2998 GdkEventScroll *event)
3000 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3003 GtkTreeIter new_iter;
3005 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3008 if (event->direction == GDK_SCROLL_UP)
3009 found = tree_prev (combo_box, combo_box->priv->model,
3010 &iter, &new_iter, FALSE);
3012 found = tree_next (combo_box, combo_box->priv->model,
3013 &iter, &new_iter, FALSE);
3016 gtk_combo_box_set_active_iter (combo_box, &new_iter);
3025 gtk_combo_box_row_separator_func (GtkTreeModel *model,
3029 GtkComboBoxPrivate *priv = combo->priv;
3031 if (priv->row_separator_func)
3032 return priv->row_separator_func (model, iter, priv->row_separator_data);
3038 gtk_combo_box_header_func (GtkTreeModel *model,
3042 /* Every submenu has a selectable header, however we
3043 * can expose a method to make that configurable by
3044 * the user (like row_separator_func is done) */
3049 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3050 gboolean add_children)
3052 GtkComboBoxPrivate *priv = combo_box->priv;
3056 child = gtk_bin_get_child (GTK_BIN (combo_box));
3058 if (priv->cell_view)
3060 priv->button = gtk_toggle_button_new ();
3061 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3062 priv->focus_on_click);
3064 g_signal_connect (priv->button, "toggled",
3065 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3066 gtk_widget_set_parent (priv->button,
3067 gtk_widget_get_parent (child));
3069 priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3070 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3072 priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3073 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3075 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3076 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3078 gtk_widget_show_all (priv->button);
3082 priv->button = gtk_toggle_button_new ();
3083 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3084 priv->focus_on_click);
3086 g_signal_connect (priv->button, "toggled",
3087 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3088 gtk_widget_set_parent (priv->button,
3089 gtk_widget_get_parent (child));
3091 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3092 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3093 gtk_widget_show_all (priv->button);
3096 g_signal_connect (priv->button, "button-press-event",
3097 G_CALLBACK (gtk_combo_box_menu_button_press),
3099 g_signal_connect (priv->button, "state-flags-changed",
3100 G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3103 /* create our funky menu */
3104 menu = _gtk_tree_menu_new_with_area (priv->area);
3105 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3107 _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3109 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3110 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3111 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3112 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3113 combo_box->priv->add_tearoffs);
3115 g_signal_connect (menu, "menu-activate",
3116 G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3118 /* Chain our row_separator_func through */
3119 _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3120 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3123 _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3124 (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3127 g_signal_connect (menu, "key-press-event",
3128 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3129 gtk_combo_box_set_popup_widget (combo_box, menu);
3131 gtk_combo_box_update_title (combo_box);
3135 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3137 GtkComboBoxPrivate *priv = combo_box->priv;
3139 g_signal_handlers_disconnect_matched (priv->button,
3140 G_SIGNAL_MATCH_DATA,
3142 gtk_combo_box_menu_button_press, NULL);
3143 g_signal_handlers_disconnect_matched (priv->button,
3144 G_SIGNAL_MATCH_DATA,
3146 gtk_combo_box_button_state_flags_changed, combo_box);
3147 g_signal_handlers_disconnect_matched (priv->popup_widget,
3148 G_SIGNAL_MATCH_DATA,
3150 gtk_combo_box_menu_activate, combo_box);
3152 /* unparent will remove our latest ref */
3153 gtk_widget_unparent (priv->button);
3156 priv->button = NULL;
3158 priv->separator = NULL;
3160 /* changing the popup window will unref the menu and the children */
3165 gtk_combo_box_menu_button_press (GtkWidget *widget,
3166 GdkEventButton *event,
3169 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3170 GtkComboBoxPrivate *priv = combo_box->priv;
3172 if (GTK_IS_MENU (priv->popup_widget) &&
3173 event->type == GDK_BUTTON_PRESS && event->button == 1)
3175 if (priv->focus_on_click &&
3176 !gtk_widget_has_focus (priv->button))
3177 gtk_widget_grab_focus (priv->button);
3179 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3188 gtk_combo_box_menu_activate (GtkWidget *menu,
3190 GtkComboBox *combo_box)
3194 if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3195 gtk_combo_box_set_active_iter (combo_box, &iter);
3197 g_object_set (combo_box,
3198 "editing-canceled", FALSE,
3204 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3207 gboolean sensitive = TRUE; /* fool code checkers */
3209 if (!combo_box->priv->button)
3212 switch (combo_box->priv->button_sensitivity)
3214 case GTK_SENSITIVITY_ON:
3217 case GTK_SENSITIVITY_OFF:
3220 case GTK_SENSITIVITY_AUTO:
3221 sensitive = combo_box->priv->model &&
3222 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3225 g_assert_not_reached ();
3229 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3231 /* In list-mode, we also need to update sensitivity of the event box */
3232 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3233 && combo_box->priv->cell_view)
3234 gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3238 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3243 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3245 if (combo_box->priv->tree_view)
3246 gtk_combo_box_list_popup_resize (combo_box);
3248 gtk_combo_box_update_sensitivity (combo_box);
3252 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3256 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3257 GtkComboBoxPrivate *priv = combo_box->priv;
3259 if (!gtk_tree_row_reference_valid (priv->active_row))
3261 if (priv->cell_view)
3262 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3263 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3266 if (priv->tree_view)
3267 gtk_combo_box_list_popup_resize (combo_box);
3269 gtk_combo_box_update_sensitivity (combo_box);
3273 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3279 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3283 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3288 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3289 GtkComboBoxPrivate *priv = combo_box->priv;
3290 GtkTreePath *active_path;
3292 /* FIXME this belongs to GtkCellView */
3293 if (gtk_tree_row_reference_valid (priv->active_row))
3295 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3296 if (gtk_tree_path_compare (path, active_path) == 0 &&
3298 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3299 gtk_tree_path_free (active_path);
3302 if (priv->tree_view)
3303 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3307 list_popup_resize_idle (gpointer user_data)
3309 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3310 GtkComboBoxPrivate *priv = combo_box->priv;
3311 gint x, y, width, height;
3313 if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3315 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3317 gtk_widget_set_size_request (priv->popup_window, width, height);
3318 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3321 priv->resize_idle_id = 0;
3327 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3329 GtkComboBoxPrivate *priv = combo_box->priv;
3331 if (!priv->resize_idle_id)
3332 priv->resize_idle_id =
3333 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3337 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3342 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3344 gtk_combo_box_list_popup_resize (combo_box);
3353 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3355 GtkComboBoxPrivate *priv = combo_box->priv;
3356 GtkTreeSelection *sel;
3358 GtkWidget *widget = GTK_WIDGET (combo_box);
3360 priv->button = gtk_toggle_button_new ();
3361 child = gtk_bin_get_child (GTK_BIN (combo_box));
3362 gtk_widget_set_parent (priv->button,
3363 gtk_widget_get_parent (child));
3364 g_signal_connect (priv->button, "button-press-event",
3365 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3366 g_signal_connect (priv->button, "toggled",
3367 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3369 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3370 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3371 priv->separator = NULL;
3372 gtk_widget_show_all (priv->button);
3374 if (priv->cell_view)
3376 GtkStyleContext *context;
3377 GtkStateFlags state;
3380 context = gtk_widget_get_style_context (widget);
3381 state = gtk_widget_get_state_flags (widget);
3382 gtk_style_context_get_background_color (context, state, &color);
3384 gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3386 priv->box = gtk_event_box_new ();
3387 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3390 if (priv->has_frame)
3392 priv->cell_view_frame = gtk_frame_new (NULL);
3393 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3398 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3399 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3403 gtk_widget_set_parent (priv->cell_view_frame,
3404 gtk_widget_get_parent (child));
3405 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3406 gtk_widget_show_all (priv->cell_view_frame);
3408 g_signal_connect (priv->box, "button-press-event",
3409 G_CALLBACK (gtk_combo_box_list_button_pressed),
3413 priv->tree_view = gtk_tree_view_new ();
3414 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3415 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3416 gtk_tree_selection_set_select_function (sel,
3417 gtk_combo_box_list_select_func,
3419 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3421 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3424 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3425 (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3429 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3431 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3432 gtk_tree_view_column_new_with_area (priv->area));
3434 if (gtk_tree_row_reference_valid (priv->active_row))
3438 path = gtk_tree_row_reference_get_path (priv->active_row);
3439 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3441 gtk_tree_path_free (path);
3444 /* set sample/popup widgets */
3445 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3447 g_signal_connect (priv->tree_view, "key-press-event",
3448 G_CALLBACK (gtk_combo_box_list_key_press),
3450 g_signal_connect (priv->tree_view, "enter-notify-event",
3451 G_CALLBACK (gtk_combo_box_list_enter_notify),
3453 g_signal_connect (priv->tree_view, "row-expanded",
3454 G_CALLBACK (gtk_combo_box_model_row_expanded),
3456 g_signal_connect (priv->tree_view, "row-collapsed",
3457 G_CALLBACK (gtk_combo_box_model_row_expanded),
3459 g_signal_connect (priv->popup_window, "button-press-event",
3460 G_CALLBACK (gtk_combo_box_list_button_pressed),
3462 g_signal_connect (priv->popup_window, "button-release-event",
3463 G_CALLBACK (gtk_combo_box_list_button_released),
3466 gtk_widget_show (priv->tree_view);
3468 gtk_combo_box_update_sensitivity (combo_box);
3472 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3474 GtkComboBoxPrivate *priv = combo_box->priv;
3476 /* disconnect signals */
3477 g_signal_handlers_disconnect_matched (priv->tree_view,
3478 G_SIGNAL_MATCH_DATA,
3479 0, 0, NULL, NULL, combo_box);
3480 g_signal_handlers_disconnect_matched (priv->button,
3481 G_SIGNAL_MATCH_DATA,
3483 gtk_combo_box_list_button_pressed,
3485 g_signal_handlers_disconnect_matched (priv->popup_window,
3486 G_SIGNAL_MATCH_DATA,
3488 gtk_combo_box_list_button_pressed,
3490 g_signal_handlers_disconnect_matched (priv->popup_window,
3491 G_SIGNAL_MATCH_DATA,
3493 gtk_combo_box_list_button_released,
3496 g_signal_handlers_disconnect_matched (priv->popup_window,
3497 G_SIGNAL_MATCH_DATA,
3499 gtk_combo_box_child_show,
3502 g_signal_handlers_disconnect_matched (priv->popup_window,
3503 G_SIGNAL_MATCH_DATA,
3505 gtk_combo_box_child_hide,
3509 g_signal_handlers_disconnect_matched (priv->box,
3510 G_SIGNAL_MATCH_DATA,
3512 gtk_combo_box_list_button_pressed,
3515 /* destroy things (unparent will kill the latest ref from us)
3516 * last unref on button will destroy the arrow
3518 gtk_widget_unparent (priv->button);
3519 priv->button = NULL;
3522 if (priv->cell_view)
3524 g_object_set (priv->cell_view,
3525 "background-set", FALSE,
3529 if (priv->cell_view_frame)
3531 gtk_widget_unparent (priv->cell_view_frame);
3532 priv->cell_view_frame = NULL;
3536 if (priv->scroll_timer)
3538 g_source_remove (priv->scroll_timer);
3539 priv->scroll_timer = 0;
3542 if (priv->resize_idle_id)
3544 g_source_remove (priv->resize_idle_id);
3545 priv->resize_idle_id = 0;
3548 gtk_widget_destroy (priv->tree_view);
3550 priv->tree_view = NULL;
3551 if (priv->popup_widget)
3553 g_object_unref (priv->popup_widget);
3554 priv->popup_widget = NULL;
3561 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3562 GdkEventButton *event,
3565 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3566 GtkComboBoxPrivate *priv = combo_box->priv;
3568 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3570 if (ewidget == priv->popup_window)
3573 if ((ewidget != priv->button && ewidget != priv->box) ||
3574 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3577 if (priv->focus_on_click &&
3578 !gtk_widget_has_focus (priv->button))
3579 gtk_widget_grab_focus (priv->button);
3581 gtk_combo_box_popup_for_device (combo_box, event->device);
3583 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3585 priv->auto_scroll = FALSE;
3586 if (priv->scroll_timer == 0)
3587 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3588 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3591 priv->popup_in_progress = TRUE;
3597 gtk_combo_box_list_button_released (GtkWidget *widget,
3598 GdkEventButton *event,
3602 GtkTreePath *path = NULL;
3605 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3606 GtkComboBoxPrivate *priv = combo_box->priv;
3608 gboolean popup_in_progress = FALSE;
3610 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3612 if (priv->popup_in_progress)
3614 popup_in_progress = TRUE;
3615 priv->popup_in_progress = FALSE;
3618 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3620 if (priv->scroll_timer)
3622 g_source_remove (priv->scroll_timer);
3623 priv->scroll_timer = 0;
3626 if (ewidget != priv->tree_view)
3628 if ((ewidget == priv->button ||
3629 ewidget == priv->box) &&
3630 !popup_in_progress &&
3631 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3633 gtk_combo_box_popdown (combo_box);
3637 /* released outside treeview */
3638 if (ewidget != priv->button && ewidget != priv->box)
3640 gtk_combo_box_popdown (combo_box);
3648 /* select something cool */
3649 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3655 return TRUE; /* clicked outside window? */
3657 gtk_tree_model_get_iter (priv->model, &iter, path);
3658 gtk_tree_path_free (path);
3660 gtk_combo_box_popdown (combo_box);
3662 if (tree_column_row_is_sensitive (combo_box, &iter))
3663 gtk_combo_box_set_active_iter (combo_box, &iter);
3669 gtk_combo_box_menu_key_press (GtkWidget *widget,
3673 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3675 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3677 /* The menu hasn't managed the
3678 * event, forward it to the combobox
3680 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3687 gtk_combo_box_list_key_press (GtkWidget *widget,
3691 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3694 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3695 event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3697 GtkTreeModel *model = NULL;
3699 gtk_combo_box_popdown (combo_box);
3701 if (combo_box->priv->model)
3703 GtkTreeSelection *sel;
3705 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3707 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3708 gtk_combo_box_set_active_iter (combo_box, &iter);
3714 if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3716 /* The list hasn't managed the
3717 * event, forward it to the combobox
3719 gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3726 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3731 GtkAllocation allocation;
3732 GtkWidget *tree_view = combo_box->priv->tree_view;
3735 gtk_widget_get_allocation (tree_view, &allocation);
3737 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3738 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3740 if (x <= allocation.x &&
3741 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3743 value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3744 gtk_adjustment_set_value (adj, value);
3746 else if (x >= allocation.x + allocation.width &&
3747 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3749 value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3750 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3754 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3755 if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3757 if (y <= allocation.y &&
3758 gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3760 value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3761 gtk_adjustment_set_value (adj, value);
3763 else if (y >= allocation.height &&
3764 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3766 value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3767 gtk_adjustment_set_value (adj, MAX (value, 0.0));
3773 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3775 GtkComboBoxPrivate *priv = combo_box->priv;
3778 if (priv->auto_scroll)
3780 gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3783 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3790 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3791 GdkEventCrossing *event,
3794 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3796 combo_box->priv->auto_scroll = TRUE;
3802 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3803 GtkTreeModel *model,
3805 gboolean path_currently_selected,
3808 GList *list, *columns;
3809 gboolean sensitive = FALSE;
3811 columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3813 for (list = columns; list && !sensitive; list = list->next)
3815 GList *cells, *cell;
3816 gboolean cell_sensitive, cell_visible;
3818 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3820 if (!gtk_tree_view_column_get_visible (column))
3823 gtk_tree_model_get_iter (model, &iter, path);
3824 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3827 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3830 g_object_get (cell->data,
3831 "sensitive", &cell_sensitive,
3832 "visible", &cell_visible,
3835 if (cell_visible && cell_sensitive)
3840 g_list_free (cells);
3842 sensitive = cell_sensitive;
3845 g_list_free (columns);
3851 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3856 /* XXX Do nothing ? */
3860 * GtkCellLayout implementation
3862 static GtkCellArea *
3863 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3865 GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3866 GtkComboBoxPrivate *priv = combo->priv;
3868 if (G_UNLIKELY (!priv->area))
3870 priv->area = gtk_cell_area_box_new ();
3871 g_object_ref_sink (priv->area);
3882 * gtk_combo_box_new:
3884 * Creates a new empty #GtkComboBox.
3886 * Return value: A new #GtkComboBox.
3891 gtk_combo_box_new (void)
3893 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3897 * gtk_combo_box_new_with_area:
3898 * @area: the #GtkCellArea to use to layout cell renderers
3900 * Creates a new empty #GtkComboBox using @area to layout cells.
3902 * Return value: A new #GtkComboBox.
3905 gtk_combo_box_new_with_area (GtkCellArea *area)
3907 return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3911 * gtk_combo_box_new_with_area_and_entry:
3912 * @area: the #GtkCellArea to use to layout cell renderers
3914 * Creates a new empty #GtkComboBox with an entry.
3916 * The new combo box will use @area to layout cells.
3918 * Return value: A new #GtkComboBox.
3921 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3923 return g_object_new (GTK_TYPE_COMBO_BOX,
3931 * gtk_combo_box_new_with_entry:
3933 * Creates a new empty #GtkComboBox with an entry.
3935 * Return value: A new #GtkComboBox.
3938 gtk_combo_box_new_with_entry (void)
3940 return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3944 * gtk_combo_box_new_with_model:
3945 * @model: A #GtkTreeModel.
3947 * Creates a new #GtkComboBox with the model initialized to @model.
3949 * Return value: A new #GtkComboBox.
3954 gtk_combo_box_new_with_model (GtkTreeModel *model)
3956 GtkComboBox *combo_box;
3958 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3960 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
3962 return GTK_WIDGET (combo_box);
3966 * gtk_combo_box_new_with_model_and_entry:
3967 * @model: A #GtkTreeModel
3969 * Creates a new empty #GtkComboBox with an entry
3970 * and with the model initialized to @model.
3972 * Return value: A new #GtkComboBox
3975 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
3977 return g_object_new (GTK_TYPE_COMBO_BOX,
3984 * gtk_combo_box_get_wrap_width:
3985 * @combo_box: A #GtkComboBox
3987 * Returns the wrap width which is used to determine the number of columns
3988 * for the popup menu. If the wrap width is larger than 1, the combo box
3991 * Returns: the wrap width.
3996 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3998 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4000 return combo_box->priv->wrap_width;
4004 * gtk_combo_box_set_wrap_width:
4005 * @combo_box: A #GtkComboBox
4006 * @width: Preferred number of columns
4008 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4009 * the preferred number of columns when you want the popup to be layed out
4015 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4018 GtkComboBoxPrivate *priv;
4020 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4021 g_return_if_fail (width >= 0);
4023 priv = combo_box->priv;
4025 if (width != priv->wrap_width)
4027 priv->wrap_width = width;
4029 gtk_combo_box_check_appearance (combo_box);
4031 if (GTK_IS_TREE_MENU (priv->popup_widget))
4032 _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4034 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4039 * gtk_combo_box_get_row_span_column:
4040 * @combo_box: A #GtkComboBox
4042 * Returns the column with row span information for @combo_box.
4044 * Returns: the row span column.
4049 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4051 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4053 return combo_box->priv->row_column;
4057 * gtk_combo_box_set_row_span_column:
4058 * @combo_box: A #GtkComboBox.
4059 * @row_span: A column in the model passed during construction.
4061 * Sets the column with row span information for @combo_box to be @row_span.
4062 * The row span column contains integers which indicate how many rows
4063 * an item should span.
4068 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4071 GtkComboBoxPrivate *priv;
4074 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4076 priv = combo_box->priv;
4078 col = gtk_tree_model_get_n_columns (priv->model);
4079 g_return_if_fail (row_span >= -1 && row_span < col);
4081 if (row_span != priv->row_column)
4083 priv->row_column = row_span;
4085 if (GTK_IS_TREE_MENU (priv->popup_widget))
4086 _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4088 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4093 * gtk_combo_box_get_column_span_column:
4094 * @combo_box: A #GtkComboBox
4096 * Returns the column with column span information for @combo_box.
4098 * Returns: the column span column.
4103 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4105 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4107 return combo_box->priv->col_column;
4111 * gtk_combo_box_set_column_span_column:
4112 * @combo_box: A #GtkComboBox
4113 * @column_span: A column in the model passed during construction
4115 * Sets the column with column span information for @combo_box to be
4116 * @column_span. The column span column contains integers which indicate
4117 * how many columns an item should span.
4122 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4125 GtkComboBoxPrivate *priv;
4128 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4130 priv = combo_box->priv;
4132 col = gtk_tree_model_get_n_columns (priv->model);
4133 g_return_if_fail (column_span >= -1 && column_span < col);
4135 if (column_span != priv->col_column)
4137 priv->col_column = column_span;
4139 if (GTK_IS_TREE_MENU (priv->popup_widget))
4140 _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4142 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4147 * gtk_combo_box_get_active:
4148 * @combo_box: A #GtkComboBox
4150 * Returns the index of the currently active item, or -1 if there's no
4151 * active item. If the model is a non-flat treemodel, and the active item
4152 * is not an immediate child of the root of the tree, this function returns
4153 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4154 * <literal>path</literal> is the #GtkTreePath of the active item.
4156 * Return value: An integer which is the index of the currently active item,
4157 * or -1 if there's no active item.
4162 gtk_combo_box_get_active (GtkComboBox *combo_box)
4164 GtkComboBoxPrivate *priv;
4167 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4169 priv = combo_box->priv;
4171 if (gtk_tree_row_reference_valid (priv->active_row))
4175 path = gtk_tree_row_reference_get_path (priv->active_row);
4176 result = gtk_tree_path_get_indices (path)[0];
4177 gtk_tree_path_free (path);
4186 * gtk_combo_box_set_active:
4187 * @combo_box: A #GtkComboBox
4188 * @index_: An index in the model passed during construction, or -1 to have
4191 * Sets the active item of @combo_box to be the item at @index.
4196 gtk_combo_box_set_active (GtkComboBox *combo_box,
4199 GtkTreePath *path = NULL;
4200 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4201 g_return_if_fail (index_ >= -1);
4203 if (combo_box->priv->model == NULL)
4205 /* Save index, in case the model is set after the index */
4206 combo_box->priv->active = index_;
4212 path = gtk_tree_path_new_from_indices (index_, -1);
4214 gtk_combo_box_set_active_internal (combo_box, path);
4217 gtk_tree_path_free (path);
4221 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4224 GtkComboBoxPrivate *priv = combo_box->priv;
4225 GtkTreePath *active_path;
4228 /* Remember whether the initially active row is valid. */
4229 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4231 if (path && is_valid_row_reference)
4233 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4234 path_cmp = gtk_tree_path_compare (path, active_path);
4235 gtk_tree_path_free (active_path);
4240 if (priv->active_row)
4242 gtk_tree_row_reference_free (priv->active_row);
4243 priv->active_row = NULL;
4248 if (priv->tree_view)
4249 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4252 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4254 if (GTK_IS_MENU (menu))
4255 gtk_menu_set_active (menu, -1);
4258 if (priv->cell_view)
4259 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4262 * Do not emit a "changed" signal when an already invalid selection was
4263 * now set to invalid.
4265 if (!is_valid_row_reference)
4271 gtk_tree_row_reference_new (priv->model, path);
4273 if (priv->tree_view)
4275 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4278 else if (GTK_IS_MENU (priv->popup_widget))
4280 /* FIXME handle nested menus better */
4281 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4282 gtk_tree_path_get_indices (path)[0]);
4285 if (priv->cell_view)
4286 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4290 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4291 g_object_notify (G_OBJECT (combo_box), "active");
4292 if (combo_box->priv->id_column >= 0)
4293 g_object_notify (G_OBJECT (combo_box), "active-id");
4298 * gtk_combo_box_get_active_iter:
4299 * @combo_box: A #GtkComboBox
4300 * @iter: (out): The uninitialized #GtkTreeIter
4302 * Sets @iter to point to the current active item, if it exists.
4304 * Return value: %TRUE, if @iter was set
4309 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4315 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4317 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4320 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4321 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4322 gtk_tree_path_free (path);
4328 * gtk_combo_box_set_active_iter:
4329 * @combo_box: A #GtkComboBox
4330 * @iter: (allow-none): The #GtkTreeIter, or %NULL
4332 * Sets the current active item to be the one referenced by @iter, or
4333 * unsets the active item if @iter is %NULL.
4338 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4341 GtkTreePath *path = NULL;
4343 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4346 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4348 gtk_combo_box_set_active_internal (combo_box, path);
4349 gtk_tree_path_free (path);
4353 * gtk_combo_box_set_model:
4354 * @combo_box: A #GtkComboBox
4355 * @model: (allow-none): A #GtkTreeModel
4357 * Sets the model used by @combo_box to be @model. Will unset a previously set
4358 * model (if applicable). If model is %NULL, then it will unset the model.
4360 * Note that this function does not clear the cell renderers, you have to
4361 * call gtk_cell_layout_clear() yourself if you need to set up different
4362 * cell renderers for the new model.
4367 gtk_combo_box_set_model (GtkComboBox *combo_box,
4368 GtkTreeModel *model)
4370 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4371 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4373 if (model == combo_box->priv->model)
4376 gtk_combo_box_unset_model (combo_box);
4381 combo_box->priv->model = model;
4382 g_object_ref (combo_box->priv->model);
4384 combo_box->priv->inserted_id =
4385 g_signal_connect (combo_box->priv->model, "row-inserted",
4386 G_CALLBACK (gtk_combo_box_model_row_inserted),
4388 combo_box->priv->deleted_id =
4389 g_signal_connect (combo_box->priv->model, "row-deleted",
4390 G_CALLBACK (gtk_combo_box_model_row_deleted),
4392 combo_box->priv->reordered_id =
4393 g_signal_connect (combo_box->priv->model, "rows-reordered",
4394 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4396 combo_box->priv->changed_id =
4397 g_signal_connect (combo_box->priv->model, "row-changed",
4398 G_CALLBACK (gtk_combo_box_model_row_changed),
4401 if (combo_box->priv->tree_view)
4404 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4405 combo_box->priv->model);
4406 gtk_combo_box_list_popup_resize (combo_box);
4409 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4412 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4413 combo_box->priv->model);
4416 if (combo_box->priv->cell_view)
4417 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4418 combo_box->priv->model);
4420 if (combo_box->priv->active != -1)
4422 /* If an index was set in advance, apply it now */
4423 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4424 combo_box->priv->active = -1;
4428 gtk_combo_box_update_sensitivity (combo_box);
4430 g_object_notify (G_OBJECT (combo_box), "model");
4434 * gtk_combo_box_get_model:
4435 * @combo_box: A #GtkComboBox
4437 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4439 * Return value: (transfer none): A #GtkTreeModel which was passed
4440 * during construction.
4445 gtk_combo_box_get_model (GtkComboBox *combo_box)
4447 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4449 return combo_box->priv->model;
4453 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
4454 GtkScrollType scroll)
4457 GtkTreeIter new_iter;
4458 gboolean active_iter;
4461 if (!combo_box->priv->model)
4463 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4467 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4471 case GTK_SCROLL_STEP_BACKWARD:
4472 case GTK_SCROLL_STEP_UP:
4473 case GTK_SCROLL_STEP_LEFT:
4476 found = tree_prev (combo_box, combo_box->priv->model,
4477 &iter, &new_iter, FALSE);
4480 /* else fall through */
4482 case GTK_SCROLL_PAGE_FORWARD:
4483 case GTK_SCROLL_PAGE_DOWN:
4484 case GTK_SCROLL_PAGE_RIGHT:
4485 case GTK_SCROLL_END:
4486 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4489 case GTK_SCROLL_STEP_FORWARD:
4490 case GTK_SCROLL_STEP_DOWN:
4491 case GTK_SCROLL_STEP_RIGHT:
4494 found = tree_next (combo_box, combo_box->priv->model,
4495 &iter, &new_iter, FALSE);
4498 /* else fall through */
4500 case GTK_SCROLL_PAGE_BACKWARD:
4501 case GTK_SCROLL_PAGE_UP:
4502 case GTK_SCROLL_PAGE_LEFT:
4503 case GTK_SCROLL_START:
4504 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4511 if (found && active_iter)
4513 GtkTreePath *old_path;
4514 GtkTreePath *new_path;
4516 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4517 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4519 if (gtk_tree_path_compare (old_path, new_path) == 0)
4522 gtk_tree_path_free (old_path);
4523 gtk_tree_path_free (new_path);
4528 gtk_combo_box_set_active_iter (combo_box, &new_iter);
4532 gtk_widget_error_bell (GTK_WIDGET (combo_box));
4537 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4538 gboolean group_cycling)
4540 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4542 if (combo_box->priv->has_entry)
4546 child = gtk_bin_get_child (GTK_BIN (combo_box));
4548 gtk_widget_grab_focus (child);
4551 gtk_widget_grab_focus (combo_box->priv->button);
4557 gtk_combo_box_grab_focus (GtkWidget *widget)
4559 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4561 if (combo_box->priv->has_entry)
4565 child = gtk_bin_get_child (GTK_BIN (combo_box));
4567 gtk_widget_grab_focus (child);
4570 gtk_widget_grab_focus (combo_box->priv->button);
4574 gtk_combo_box_destroy (GtkWidget *widget)
4576 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4578 if (combo_box->priv->popup_idle_id > 0)
4580 g_source_remove (combo_box->priv->popup_idle_id);
4581 combo_box->priv->popup_idle_id = 0;
4584 gtk_combo_box_popdown (combo_box);
4586 if (combo_box->priv->row_separator_destroy)
4587 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4589 combo_box->priv->row_separator_func = NULL;
4590 combo_box->priv->row_separator_data = NULL;
4591 combo_box->priv->row_separator_destroy = NULL;
4593 GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4594 combo_box->priv->cell_view = NULL;
4598 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4601 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4604 * Fixes regression reported in bug #574059. The old functionality relied on
4605 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
4606 * when the selection was already set to -1.
4608 if (gtk_combo_box_get_active(combo_box) == -1)
4609 g_signal_emit_by_name (combo_box, "changed");
4611 gtk_combo_box_set_active (combo_box, -1);
4615 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4618 GtkComboBoxPrivate *priv = combo_box->priv;
4619 GtkTreeModel *model;
4622 if (gtk_combo_box_get_active_iter (combo_box, &iter))
4624 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4628 GValue value = {0,};
4630 g_signal_handlers_block_by_func (entry,
4631 gtk_combo_box_entry_contents_changed,
4634 model = gtk_combo_box_get_model (combo_box);
4636 gtk_tree_model_get_value (model, &iter,
4637 priv->text_column, &value);
4638 g_object_set_property (G_OBJECT (entry), "text", &value);
4639 g_value_unset (&value);
4641 g_signal_handlers_unblock_by_func (entry,
4642 gtk_combo_box_entry_contents_changed,
4649 gtk_combo_box_constructor (GType type,
4650 guint n_construct_properties,
4651 GObjectConstructParam *construct_properties)
4654 GtkComboBox *combo_box;
4655 GtkComboBoxPrivate *priv;
4657 object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4658 (type, n_construct_properties, construct_properties);
4660 combo_box = GTK_COMBO_BOX (object);
4661 priv = combo_box->priv;
4665 priv->area = gtk_cell_area_box_new ();
4666 g_object_ref_sink (priv->area);
4669 priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4670 gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4671 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4672 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4673 _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4674 gtk_widget_show (priv->cell_view);
4676 gtk_combo_box_check_appearance (combo_box);
4678 if (priv->has_entry)
4681 GtkStyleContext *context;
4683 entry = gtk_entry_new ();
4684 gtk_widget_show (entry);
4685 gtk_container_add (GTK_CONTAINER (combo_box), entry);
4687 context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4688 gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4690 priv->text_renderer = gtk_cell_renderer_text_new ();
4691 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4692 priv->text_renderer, TRUE);
4694 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4696 g_signal_connect (combo_box, "changed",
4697 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4705 gtk_combo_box_dispose(GObject* object)
4707 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4709 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4711 gtk_combo_box_menu_destroy (combo_box);
4712 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4713 combo_box->priv->popup_widget = NULL;
4716 if (combo_box->priv->area)
4718 g_object_unref (combo_box->priv->area);
4719 combo_box->priv->area = NULL;
4722 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4723 gtk_combo_box_list_destroy (combo_box);
4725 if (combo_box->priv->popup_window)
4727 gtk_widget_destroy (combo_box->priv->popup_window);
4728 combo_box->priv->popup_window = NULL;
4731 gtk_combo_box_unset_model (combo_box);
4733 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4737 gtk_combo_box_finalize (GObject *object)
4739 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4741 g_free (combo_box->priv->tearoff_title);
4743 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4747 gtk_cell_editable_key_press (GtkWidget *widget,
4751 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4753 if (event->keyval == GDK_KEY_Escape)
4755 g_object_set (combo_box,
4756 "editing-canceled", TRUE,
4758 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4759 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4763 else if (event->keyval == GDK_KEY_Return ||
4764 event->keyval == GDK_KEY_ISO_Enter ||
4765 event->keyval == GDK_KEY_KP_Enter)
4767 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4768 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4777 popdown_idle (gpointer data)
4779 GtkComboBox *combo_box;
4781 combo_box = GTK_COMBO_BOX (data);
4783 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4784 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4786 g_object_unref (combo_box);
4792 popdown_handler (GtkWidget *widget,
4795 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4799 popup_idle (gpointer data)
4801 GtkComboBox *combo_box;
4803 combo_box = GTK_COMBO_BOX (data);
4805 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4806 combo_box->priv->cell_view)
4807 g_signal_connect_object (combo_box->priv->popup_widget,
4808 "unmap", G_CALLBACK (popdown_handler),
4811 /* we unset this if a menu item is activated */
4812 g_object_set (combo_box,
4813 "editing-canceled", TRUE,
4815 gtk_combo_box_popup (combo_box);
4817 combo_box->priv->popup_idle_id = 0;
4818 combo_box->priv->activate_button = 0;
4819 combo_box->priv->activate_time = 0;
4825 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4828 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4831 combo_box->priv->is_cell_renderer = TRUE;
4833 if (combo_box->priv->cell_view)
4835 g_signal_connect_object (combo_box->priv->button, "key-press-event",
4836 G_CALLBACK (gtk_cell_editable_key_press),
4839 gtk_widget_grab_focus (combo_box->priv->button);
4843 child = gtk_bin_get_child (GTK_BIN (combo_box));
4845 g_signal_connect_object (child, "key-press-event",
4846 G_CALLBACK (gtk_cell_editable_key_press),
4849 gtk_widget_grab_focus (child);
4850 gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4853 /* we do the immediate popup only for the optionmenu-like
4856 if (combo_box->priv->is_cell_renderer &&
4857 combo_box->priv->cell_view && !combo_box->priv->tree_view)
4859 if (event && event->type == GDK_BUTTON_PRESS)
4861 GdkEventButton *event_button = (GdkEventButton *)event;
4863 combo_box->priv->activate_button = event_button->button;
4864 combo_box->priv->activate_time = event_button->time;
4867 combo_box->priv->popup_idle_id =
4868 gdk_threads_add_idle (popup_idle, combo_box);
4874 * gtk_combo_box_get_add_tearoffs:
4875 * @combo_box: a #GtkComboBox
4877 * Gets the current value of the :add-tearoffs property.
4879 * Return value: the current value of the :add-tearoffs property.
4882 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4884 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4886 return combo_box->priv->add_tearoffs;
4890 * gtk_combo_box_set_add_tearoffs:
4891 * @combo_box: a #GtkComboBox
4892 * @add_tearoffs: %TRUE to add tearoff menu items
4894 * Sets whether the popup menu should have a tearoff
4900 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4901 gboolean add_tearoffs)
4903 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4905 add_tearoffs = add_tearoffs != FALSE;
4907 if (combo_box->priv->add_tearoffs != add_tearoffs)
4909 combo_box->priv->add_tearoffs = add_tearoffs;
4910 gtk_combo_box_check_appearance (combo_box);
4912 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4913 _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4914 combo_box->priv->add_tearoffs);
4916 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
4921 * gtk_combo_box_get_title:
4922 * @combo_box: a #GtkComboBox
4924 * Gets the current title of the menu in tearoff mode. See
4925 * gtk_combo_box_set_add_tearoffs().
4927 * Returns: the menu's title in tearoff mode. This is an internal copy of the
4928 * string which must not be freed.
4933 gtk_combo_box_get_title (GtkComboBox *combo_box)
4935 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4937 return combo_box->priv->tearoff_title;
4941 gtk_combo_box_update_title (GtkComboBox *combo_box)
4943 gtk_combo_box_check_appearance (combo_box);
4945 if (combo_box->priv->popup_widget &&
4946 GTK_IS_MENU (combo_box->priv->popup_widget))
4947 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
4948 combo_box->priv->tearoff_title);
4952 * gtk_combo_box_set_title:
4953 * @combo_box: a #GtkComboBox
4954 * @title: a title for the menu in tearoff mode
4956 * Sets the menu's title in tearoff mode.
4961 gtk_combo_box_set_title (GtkComboBox *combo_box,
4964 GtkComboBoxPrivate *priv;
4966 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4968 priv = combo_box->priv;
4970 if (strcmp (title ? title : "",
4971 priv->tearoff_title ? priv->tearoff_title : "") != 0)
4973 g_free (priv->tearoff_title);
4974 priv->tearoff_title = g_strdup (title);
4976 gtk_combo_box_update_title (combo_box);
4978 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
4984 * gtk_combo_box_set_popup_fixed_width:
4985 * @combo_box: a #GtkComboBox
4986 * @fixed: whether to use a fixed popup width
4988 * Specifies whether the popup's width should be a fixed width
4989 * matching the allocated width of the combo box.
4994 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
4997 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4999 if (combo_box->priv->popup_fixed_width != fixed)
5001 combo_box->priv->popup_fixed_width = fixed;
5003 g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5008 * gtk_combo_box_get_popup_fixed_width:
5009 * @combo_box: a #GtkComboBox
5011 * Gets whether the popup uses a fixed width matching
5012 * the allocated width of the combo box.
5014 * Returns: %TRUE if the popup uses a fixed width
5019 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5021 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5023 return combo_box->priv->popup_fixed_width;
5028 * gtk_combo_box_get_popup_accessible:
5029 * @combo_box: a #GtkComboBox
5031 * Gets the accessible object corresponding to the combo box's popup.
5033 * This function is mostly intended for use by accessibility technologies;
5034 * applications should have little use for it.
5036 * Returns: (transfer none): the accessible object corresponding
5037 * to the combo box's popup.
5042 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5046 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5048 if (combo_box->priv->popup_widget)
5050 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5058 * gtk_combo_box_get_row_separator_func: (skip)
5059 * @combo_box: a #GtkComboBox
5061 * Returns the current row separator function.
5063 * Return value: the current row separator function.
5067 GtkTreeViewRowSeparatorFunc
5068 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5070 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5072 return combo_box->priv->row_separator_func;
5076 * gtk_combo_box_set_row_separator_func:
5077 * @combo_box: a #GtkComboBox
5078 * @func: a #GtkTreeViewRowSeparatorFunc
5079 * @data: (allow-none): user data to pass to @func, or %NULL
5080 * @destroy: (allow-none): destroy notifier for @data, or %NULL
5082 * Sets the row separator function, which is used to determine
5083 * whether a row should be drawn as a separator. If the row separator
5084 * function is %NULL, no separators are drawn. This is the default value.
5089 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5090 GtkTreeViewRowSeparatorFunc func,
5092 GDestroyNotify destroy)
5094 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5096 if (combo_box->priv->row_separator_destroy)
5097 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5099 combo_box->priv->row_separator_func = func;
5100 combo_box->priv->row_separator_data = data;
5101 combo_box->priv->row_separator_destroy = destroy;
5103 /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5104 if (combo_box->priv->tree_view)
5106 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5107 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5110 if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5112 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5113 _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5116 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5120 * gtk_combo_box_set_button_sensitivity:
5121 * @combo_box: a #GtkComboBox
5122 * @sensitivity: specify the sensitivity of the dropdown button
5124 * Sets whether the dropdown button of the combo box should be
5125 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5126 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5131 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5132 GtkSensitivityType sensitivity)
5134 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5136 if (combo_box->priv->button_sensitivity != sensitivity)
5138 combo_box->priv->button_sensitivity = sensitivity;
5139 gtk_combo_box_update_sensitivity (combo_box);
5141 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5146 * gtk_combo_box_get_button_sensitivity:
5147 * @combo_box: a #GtkComboBox
5149 * Returns whether the combo box sets the dropdown button
5150 * sensitive or not when there are no items in the model.
5152 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5153 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5154 * if the button is always insensitive or
5155 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5156 * the model has one item to be selected.
5161 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5163 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5165 return combo_box->priv->button_sensitivity;
5170 * gtk_combo_box_get_has_entry:
5171 * @combo_box: a #GtkComboBox
5173 * Returns whether the combo box has an entry.
5175 * Return Value: whether there is an entry in @combo_box.
5180 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5182 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5184 return combo_box->priv->has_entry;
5188 * gtk_combo_box_set_entry_text_column:
5189 * @combo_box: A #GtkComboBox
5190 * @text_column: A column in @model to get the strings from for
5191 * the internal entry
5193 * Sets the model column which @combo_box should use to get strings from
5194 * to be @text_column. The column @text_column in the model of @combo_box
5195 * must be of type %G_TYPE_STRING.
5197 * This is only relevant if @combo_box has been created with
5198 * #GtkComboBox:has-entry as %TRUE.
5203 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5206 GtkComboBoxPrivate *priv = combo_box->priv;
5207 GtkTreeModel *model;
5209 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5211 model = gtk_combo_box_get_model (combo_box);
5213 g_return_if_fail (text_column >= 0);
5214 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5216 priv->text_column = text_column;
5218 if (priv->text_renderer != NULL)
5219 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5220 priv->text_renderer,
5221 "text", text_column,
5226 * gtk_combo_box_get_entry_text_column:
5227 * @combo_box: A #GtkComboBox.
5229 * Returns the column which @combo_box is using to get the strings
5230 * from to display in the internal entry.
5232 * Return value: A column in the data source model of @combo_box.
5237 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5239 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5241 return combo_box->priv->text_column;
5245 * gtk_combo_box_set_focus_on_click:
5246 * @combo: a #GtkComboBox
5247 * @focus_on_click: whether the combo box grabs focus when clicked
5250 * Sets whether the combo box will grab focus when it is clicked with
5251 * the mouse. Making mouse clicks not grab focus is useful in places
5252 * like toolbars where you don't want the keyboard focus removed from
5253 * the main area of the application.
5258 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5259 gboolean focus_on_click)
5261 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5263 focus_on_click = focus_on_click != FALSE;
5265 if (combo_box->priv->focus_on_click != focus_on_click)
5267 combo_box->priv->focus_on_click = focus_on_click;
5269 if (combo_box->priv->button)
5270 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5273 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5278 * gtk_combo_box_get_focus_on_click:
5279 * @combo: a #GtkComboBox
5281 * Returns whether the combo box grabs focus when it is clicked
5282 * with the mouse. See gtk_combo_box_set_focus_on_click().
5284 * Return value: %TRUE if the combo box grabs focus when it is
5285 * clicked with the mouse.
5290 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5292 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5294 return combo_box->priv->focus_on_click;
5299 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5300 GtkBuilder *builder,
5302 const gchar *tagname,
5303 GMarkupParser *parser,
5306 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5307 tagname, parser, data))
5310 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5311 tagname, parser, data);
5315 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5316 GtkBuilder *builder,
5318 const gchar *tagname,
5321 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5322 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5326 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5327 GtkBuilder *builder,
5328 const gchar *childname)
5330 GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5332 if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5333 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5335 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5339 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5343 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5344 GtkComboBoxPrivate *priv = combo_box->priv;
5345 gint focus_width, focus_pad;
5346 gint font_size, arrow_size;
5347 PangoContext *context;
5348 PangoFontMetrics *metrics;
5349 const PangoFontDescription *font_desc;
5351 gint minimum_width = 0, natural_width = 0;
5352 gint child_min, child_nat;
5353 GtkStyleContext *style_context;
5354 GtkStateFlags state;
5356 gfloat arrow_scaling;
5358 child = gtk_bin_get_child (GTK_BIN (widget));
5361 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5363 gtk_widget_style_get (GTK_WIDGET (widget),
5364 "focus-line-width", &focus_width,
5365 "focus-padding", &focus_pad,
5366 "arrow-size", &arrow_size,
5367 "arrow-scaling", &arrow_scaling,
5370 style_context = gtk_widget_get_style_context (widget);
5371 state = gtk_widget_get_state_flags (widget);
5373 get_widget_padding (widget, &padding);
5374 font_desc = gtk_style_context_get_font (style_context, state);
5376 context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5377 metrics = pango_context_get_metrics (context, font_desc,
5378 pango_context_get_language (context));
5379 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5380 pango_font_metrics_get_descent (metrics));
5381 pango_font_metrics_unref (metrics);
5383 arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5385 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5387 if (!priv->tree_view)
5390 if (priv->cell_view)
5392 gint sep_width, arrow_width;
5393 gint border_width, xpad;
5394 GtkBorder button_padding;
5396 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5397 get_widget_padding (priv->button, &button_padding);
5399 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5400 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5402 xpad = 2 * (border_width + focus_width + focus_pad) +
5403 button_padding.left + button_padding.right + padding.left + padding.right;
5405 minimum_width = child_min + sep_width + arrow_width + xpad;
5406 natural_width = child_nat + sep_width + arrow_width + xpad;
5410 gint but_width, but_nat_width;
5412 gtk_widget_get_preferred_width (priv->button,
5413 &but_width, &but_nat_width);
5415 minimum_width = child_min + but_width;
5416 natural_width = child_nat + but_nat_width;
5422 gint button_width, button_nat_width;
5424 /* sample + frame */
5425 minimum_width = child_min;
5426 natural_width = child_nat;
5428 minimum_width += 2 * focus_width;
5429 natural_width += 2 * focus_width;
5431 if (priv->cell_view_frame)
5433 if (priv->has_frame)
5435 gint border_width, xpad;
5436 GtkBorder frame_padding;
5438 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5439 get_widget_padding (priv->cell_view_frame, &frame_padding);
5440 xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5442 minimum_width += xpad;
5443 natural_width += xpad;
5448 gtk_widget_get_preferred_width (priv->button,
5449 &button_width, &button_nat_width);
5451 minimum_width += button_width;
5452 natural_width += button_nat_width;
5455 minimum_width += padding.left + padding.right;
5456 natural_width += padding.left + padding.right;
5459 *minimum_size = minimum_width;
5462 *natural_size = natural_width;
5466 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5472 /* Combo box is height-for-width only
5473 * (so we always just reserve enough height for the minimum width) */
5474 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5475 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5479 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5484 /* Combo box is height-for-width only
5485 * (so we assume we always reserved enough height for the minimum width) */
5486 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5491 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5496 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5497 GtkComboBoxPrivate *priv = combo_box->priv;
5498 gint focus_width, focus_pad;
5499 gint min_height = 0, nat_height = 0;
5504 gtk_widget_style_get (GTK_WIDGET (widget),
5505 "focus-line-width", &focus_width,
5506 "focus-padding", &focus_pad,
5509 child = gtk_bin_get_child (GTK_BIN (widget));
5511 get_widget_padding (widget, &padding);
5514 if (!priv->tree_view)
5517 if (priv->cell_view)
5519 /* calculate x/y padding and separator/arrow size */
5520 gint sep_width, arrow_width, sep_height, arrow_height;
5521 gint border_width, xpad, ypad;
5522 GtkBorder button_padding;
5524 border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5525 get_widget_padding (priv->button, &button_padding);
5527 gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5528 gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5529 gtk_widget_get_preferred_height_for_width (priv->separator,
5530 sep_width, &sep_height, NULL);
5531 gtk_widget_get_preferred_height_for_width (priv->arrow,
5532 arrow_width, &arrow_height, NULL);
5534 xpad = 2 * (border_width + focus_width + focus_pad) +
5535 button_padding.left + button_padding.right;
5536 ypad = 2 * (border_width + focus_width + focus_pad) +
5537 button_padding.top + button_padding.bottom;
5539 size -= sep_width + arrow_width + xpad;
5541 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5542 * and fitting the whole treemodel */
5543 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5545 arrow_height = MAX (arrow_height, sep_height);
5546 min_height = MAX (min_height, arrow_height);
5547 nat_height = MAX (nat_height, arrow_height);
5554 /* there is a custom child widget inside (no priv->cell_view) */
5555 gint but_width, but_height;
5557 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5558 gtk_widget_get_preferred_height_for_width (priv->button,
5559 but_width, &but_height, NULL);
5563 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5564 * and fitting the whole treemodel */
5565 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5567 min_height = MAX (min_height, but_height);
5568 nat_height = MAX (nat_height, but_height);
5574 gint but_width, but_height;
5575 gint xpad = 0, ypad = 0;
5577 gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5578 gtk_widget_get_preferred_height_for_width (priv->button,
5579 but_width, &but_height, NULL);
5581 if (priv->cell_view_frame && priv->has_frame)
5583 GtkBorder frame_padding;
5586 border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5587 get_widget_padding (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5589 xpad = (2 * border_width) + padding.left + frame_padding.right;
5590 ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5594 size -= 2 * focus_width;
5597 /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5598 * and fitting the whole treemodel */
5599 gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5601 min_height = MAX (min_height, but_height);
5602 nat_height = MAX (nat_height, but_height);
5608 min_height += padding.top + padding.bottom;
5609 nat_height += padding.top + padding.bottom;
5612 *minimum_size = min_height;
5615 *natural_size = nat_height;
5619 * gtk_combo_box_set_id_column:
5620 * @combo_box: A #GtkComboBox
5621 * @id_column: A column in @model to get string IDs for values from
5623 * Sets the model column which @combo_box should use to get string IDs
5624 * for values from. The column @id_column in the model of @combo_box
5625 * must be of type %G_TYPE_STRING.
5630 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5633 GtkComboBoxPrivate *priv = combo_box->priv;
5634 GtkTreeModel *model;
5636 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5638 if (id_column != priv->id_column)
5640 model = gtk_combo_box_get_model (combo_box);
5642 g_return_if_fail (id_column >= 0);
5643 g_return_if_fail (model == NULL ||
5644 id_column < gtk_tree_model_get_n_columns (model));
5646 priv->id_column = id_column;
5648 g_object_notify (G_OBJECT (combo_box), "id-column");
5649 g_object_notify (G_OBJECT (combo_box), "active-id");
5654 * gtk_combo_box_get_id_column:
5655 * @combo_box: A #GtkComboBox
5657 * Returns the column which @combo_box is using to get string IDs
5660 * Return value: A column in the data source model of @combo_box.
5665 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5667 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5669 return combo_box->priv->id_column;
5673 * gtk_combo_box_get_active_id:
5674 * @combo_box: a #GtkComboBox
5676 * Returns the ID of the active row of @combo_box. This value is taken
5677 * from the active row and the column specified by the #GtkComboBox:id-column
5678 * property of @combo_box (see gtk_combo_box_set_id_column()).
5680 * The returned value is an interned string which means that you can
5681 * compare the pointer by value to other interned strings and that you
5684 * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5685 * no row is active, or if the active row has a %NULL ID value, then %NULL
5688 * Return value: the ID of the active row, or %NULL
5693 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5695 GtkTreeModel *model;
5699 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5701 column = combo_box->priv->id_column;
5706 model = gtk_combo_box_get_model (combo_box);
5707 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5708 G_TYPE_STRING, NULL);
5710 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5712 const gchar *interned;
5715 gtk_tree_model_get (model, &iter, column, &id, -1);
5716 interned = g_intern_string (id);
5726 * gtk_combo_box_set_active_id:
5727 * @combo_box: a #GtkComboBox
5728 * @active_id: (allow-none): the ID of the row to select, or %NULL
5730 * Changes the active row of @combo_box to the one that has an ID equal to
5731 * @active_id, or unsets the active row if @active_id is %NULL. Rows having
5732 * a %NULL ID string cannot be made active by this function.
5734 * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5735 * row has the given ID then the function does nothing and returns %FALSE.
5737 * Returns: %TRUE if a row with a matching ID was found. If a %NULL
5738 * @active_id was given to unset the active row, the function
5739 * always returns %TRUE.
5744 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5745 const gchar *active_id)
5747 GtkTreeModel *model;
5749 gboolean match = FALSE;
5752 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5754 if (active_id == NULL)
5756 gtk_combo_box_set_active (combo_box, -1);
5757 return TRUE; /* active row was successfully unset */
5760 column = combo_box->priv->id_column;
5765 model = gtk_combo_box_get_model (combo_box);
5766 g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5767 G_TYPE_STRING, FALSE);
5769 if (gtk_tree_model_get_iter_first (model, &iter))
5773 gtk_tree_model_get (model, &iter, column, &id, -1);
5775 match = strcmp (id, active_id) == 0;
5780 gtk_combo_box_set_active_iter (combo_box, &iter);
5783 } while (gtk_tree_model_iter_next (model, &iter));