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"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellview.h"
28 #include "gtkeventbox.h"
31 #include "gtkliststore.h"
34 #include "gtkscrolledwindow.h"
35 #include "gtkseparatormenuitem.h"
36 #include "gtktearoffmenuitem.h"
37 #include "gtktogglebutton.h"
38 #include "gtktreeselection.h"
39 #include "gtkvseparator.h"
40 #include "gtkwindow.h"
41 #include "gtkprivate.h"
43 #include <gdk/gdkkeysyms.h>
45 #include <gobject/gvaluecollector.h>
50 #include "gtkmarshalers.h"
53 #include "gtktreeprivate.h"
56 /* WELCOME, to THE house of evil code */
58 typedef struct _ComboCellInfo ComboCellInfo;
61 GtkCellRenderer *cell;
64 GtkCellLayoutDataFunc func;
66 GDestroyNotify destroy;
72 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
74 struct _GtkComboBoxPrivate
82 GtkShadowType shadow_type;
84 gint active; /* Only temporary */
85 GtkTreeRowReference *active_row;
88 GtkTreeViewColumn *column;
91 GtkWidget *cell_view_frame;
98 GtkWidget *popup_widget;
99 GtkWidget *popup_window;
100 GtkWidget *scrolled_window;
107 guint activate_button;
108 guint32 activate_time;
110 guint resize_idle_id;
116 guint popup_in_progress : 1;
117 guint popup_shown : 1;
118 guint add_tearoffs : 1;
120 guint is_cell_renderer : 1;
121 guint editing_canceled : 1;
122 guint auto_scroll : 1;
123 guint focus_on_click : 1;
124 guint button_sensitivity : 2;
126 GtkTreeViewRowSeparatorFunc row_separator_func;
127 gpointer row_separator_data;
128 GDestroyNotify row_separator_destroy;
130 gchar *tearoff_title;
133 /* While debugging this evil code, I have learned that
134 * there are actually 4 modes to this widget, which can
135 * be characterized as follows
137 * 1) menu mode, no child added
140 * cell_view -> GtkCellView, regular child
141 * cell_view_frame -> NULL
142 * button -> GtkToggleButton set_parent to combo
143 * arrow -> GtkArrow set_parent to button
144 * separator -> GtkVSepator set_parent to button
145 * popup_widget -> GtkMenu
146 * popup_window -> NULL
147 * scrolled_window -> NULL
149 * 2) menu mode, child added
153 * cell_view_frame -> NULL
154 * button -> GtkToggleButton set_parent to combo
155 * arrow -> GtkArrow, child of button
157 * popup_widget -> GtkMenu
158 * popup_window -> NULL
159 * scrolled_window -> NULL
161 * 3) list mode, no child added
163 * tree_view -> GtkTreeView, child of scrolled_window
164 * cell_view -> GtkCellView, regular child
165 * cell_view_frame -> GtkFrame, set parent to combo
166 * button -> GtkToggleButton, set_parent to combo
167 * arrow -> GtkArrow, child of button
169 * popup_widget -> tree_view
170 * popup_window -> GtkWindow
171 * scrolled_window -> GtkScrolledWindow, child of popup_window
173 * 4) list mode, child added
175 * tree_view -> GtkTreeView, child of scrolled_window
177 * cell_view_frame -> NULL
178 * button -> GtkToggleButton, set_parent to combo
179 * arrow -> GtkArrow, child of button
181 * popup_widget -> tree_view
182 * popup_window -> GtkWindow
183 * scrolled_window -> GtkScrolledWindow, child of popup_window
199 PROP_ROW_SPAN_COLUMN,
200 PROP_COLUMN_SPAN_COLUMN,
207 PROP_BUTTON_SENSITIVITY
210 static guint combo_box_signals[LAST_SIGNAL] = {0,};
212 #define BONUS_PADDING 4
213 #define SCROLL_TIME 100
217 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
218 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
219 static void gtk_combo_box_dispose (GObject *object);
220 static void gtk_combo_box_finalize (GObject *object);
221 static void gtk_combo_box_destroy (GtkObject *object);
223 static void gtk_combo_box_set_property (GObject *object,
227 static void gtk_combo_box_get_property (GObject *object,
232 static void gtk_combo_box_state_changed (GtkWidget *widget,
233 GtkStateType previous);
234 static void gtk_combo_box_grab_focus (GtkWidget *widget);
235 static void gtk_combo_box_style_set (GtkWidget *widget,
237 static void gtk_combo_box_button_toggled (GtkWidget *widget,
239 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
240 GtkStateType previous,
242 static void gtk_combo_box_add (GtkContainer *container,
244 static void gtk_combo_box_remove (GtkContainer *container,
247 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
248 GtkCellRenderer *cell);
250 static void gtk_combo_box_menu_show (GtkWidget *menu,
252 static void gtk_combo_box_menu_hide (GtkWidget *menu,
255 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
257 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
262 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
267 static void gtk_combo_box_menu_position (GtkMenu *menu,
273 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
275 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
277 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
279 static void gtk_combo_box_size_request (GtkWidget *widget,
280 GtkRequisition *requisition);
281 static void gtk_combo_box_size_allocate (GtkWidget *widget,
282 GtkAllocation *allocation);
283 static void gtk_combo_box_forall (GtkContainer *container,
284 gboolean include_internals,
285 GtkCallback callback,
286 gpointer callback_data);
287 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
288 GdkEventExpose *event);
289 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
290 GdkEventScroll *event);
291 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
294 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
295 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
296 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
297 GtkScrollType scroll);
298 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
299 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
301 /* listening to the model */
302 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
306 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
309 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
314 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
318 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
324 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
329 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
330 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
332 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
333 GdkEventButton *event,
335 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
338 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
339 GdkEventCrossing *event,
341 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
344 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
345 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
346 GdkEventButton *event,
349 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
352 gboolean path_currently_selected,
355 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
359 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
362 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
363 gboolean add_children);
364 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
365 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
368 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
369 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
371 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
375 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
377 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
378 GdkEventButton *event,
380 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
382 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
386 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
389 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
394 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
398 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
401 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
403 guint32 activate_time);
404 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
409 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
410 GtkCellRenderer *cell,
412 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
413 GtkCellRenderer *cell,
415 static GList *gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout);
416 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
417 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
418 GtkCellRenderer *cell,
419 const gchar *attribute,
421 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
422 GtkCellRenderer *cell,
423 GtkCellLayoutDataFunc func,
425 GDestroyNotify destroy);
426 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
427 GtkCellRenderer *cell);
428 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
429 GtkCellRenderer *cell,
431 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
432 gboolean group_cycling);
434 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
435 GtkCellLayout *cell_layout);
436 static void combo_cell_data_func (GtkCellLayout *cell_layout,
437 GtkCellRenderer *cell,
438 GtkTreeModel *tree_model,
441 static void gtk_combo_box_child_show (GtkWidget *widget,
442 GtkComboBox *combo_box);
443 static void gtk_combo_box_child_hide (GtkWidget *widget,
444 GtkComboBox *combo_box);
446 /* GtkBuildable method implementation */
447 static GtkBuildableIface *parent_buildable_iface;
449 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
450 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
453 const gchar *tagname,
454 GMarkupParser *parser,
456 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
459 const gchar *tagname,
462 /* GtkCellEditable method implementations */
463 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
467 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
468 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
469 gtk_combo_box_cell_layout_init)
470 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
471 gtk_combo_box_cell_editable_init)
472 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
473 gtk_combo_box_buildable_init))
478 gtk_combo_box_class_init (GtkComboBoxClass *klass)
480 GObjectClass *object_class;
481 GtkObjectClass *gtk_object_class;
482 GtkContainerClass *container_class;
483 GtkWidgetClass *widget_class;
484 GtkBindingSet *binding_set;
486 klass->get_active_text = gtk_combo_box_real_get_active_text;
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;
493 widget_class = (GtkWidgetClass *)klass;
494 widget_class->size_allocate = gtk_combo_box_size_allocate;
495 widget_class->size_request = gtk_combo_box_size_request;
496 widget_class->expose_event = gtk_combo_box_expose_event;
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_set = gtk_combo_box_style_set;
501 widget_class->state_changed = gtk_combo_box_state_changed;
503 gtk_object_class = (GtkObjectClass *)klass;
504 gtk_object_class->destroy = gtk_combo_box_destroy;
506 object_class = (GObjectClass *)klass;
507 object_class->dispose = gtk_combo_box_dispose;
508 object_class->finalize = gtk_combo_box_finalize;
509 object_class->set_property = gtk_combo_box_set_property;
510 object_class->get_property = gtk_combo_box_get_property;
514 * GtkComboBox::changed:
515 * @widget: the object which received the signal
517 * The changed signal is emitted when the active
518 * item is changed. The can be due to the user selecting
519 * a different item from the list, or due to a
520 * call to gtk_combo_box_set_active_iter().
521 * It will also be emitted while typing into a GtkComboBoxEntry,
522 * as well as when selecting an item from the GtkComboBoxEntry's list.
526 combo_box_signals[CHANGED] =
527 g_signal_new (I_("changed"),
528 G_OBJECT_CLASS_TYPE (klass),
530 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
532 g_cclosure_marshal_VOID__VOID,
535 * GtkComboBox::move-active:
536 * @widget: the object that received the signal
537 * @scroll_type: a #GtkScrollType
539 * The ::move-active signal is a
540 * <link linkend="keybinding-signals">keybinding signal</link>
541 * which gets emitted to move the active selection.
545 combo_box_signals[MOVE_ACTIVE] =
546 g_signal_new_class_handler (I_("move-active"),
547 G_OBJECT_CLASS_TYPE (klass),
548 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
549 G_CALLBACK (gtk_combo_box_real_move_active),
551 g_cclosure_marshal_VOID__ENUM,
553 GTK_TYPE_SCROLL_TYPE);
556 * GtkComboBox::popup:
557 * @widget: the object that received the signal
559 * The ::popup signal is a
560 * <link linkend="keybinding-signals">keybinding signal</link>
561 * which gets emitted to popup the combo box list.
563 * The default binding for this signal is Alt+Down.
567 combo_box_signals[POPUP] =
568 g_signal_new_class_handler (I_("popup"),
569 G_OBJECT_CLASS_TYPE (klass),
570 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
571 G_CALLBACK (gtk_combo_box_real_popup),
573 g_cclosure_marshal_VOID__VOID,
576 * GtkComboBox::popdown:
577 * @button: the object which received the signal
579 * The ::popdown signal is a
580 * <link linkend="keybinding-signals">keybinding signal</link>
581 * which gets emitted to popdown the combo box list.
583 * The default bindings for this signal are Alt+Up and Escape.
587 combo_box_signals[POPDOWN] =
588 g_signal_new_class_handler (I_("popdown"),
589 G_OBJECT_CLASS_TYPE (klass),
590 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
591 G_CALLBACK (gtk_combo_box_real_popdown),
593 _gtk_marshal_BOOLEAN__VOID,
597 binding_set = gtk_binding_set_by_class (widget_class);
599 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
601 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
604 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
606 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
608 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
611 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
613 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
614 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
616 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
617 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
619 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
620 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
622 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
623 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
625 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
626 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
628 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
630 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
632 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
633 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
635 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
636 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
638 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
639 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
641 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
642 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
644 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
645 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
647 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
653 * The model from which the combo box takes the values shown
658 g_object_class_install_property (object_class,
660 g_param_spec_object ("model",
661 P_("ComboBox model"),
662 P_("The model for the combo box"),
664 GTK_PARAM_READWRITE));
667 * GtkComboBox:wrap-width:
669 * If wrap-width is set to a positive value, the list will be
670 * displayed in multiple columns, the number of columns is
671 * determined by wrap-width.
675 g_object_class_install_property (object_class,
677 g_param_spec_int ("wrap-width",
679 P_("Wrap width for laying out the items in a grid"),
683 GTK_PARAM_READWRITE));
687 * GtkComboBox:row-span-column:
689 * If this is set to a non-negative value, it must be the index of a column
690 * of type %G_TYPE_INT in the model.
692 * The values of that column are used to determine how many rows a value in
693 * the list will span. Therefore, the values in the model column pointed to
694 * by this property must be greater than zero and not larger than wrap-width.
698 g_object_class_install_property (object_class,
699 PROP_ROW_SPAN_COLUMN,
700 g_param_spec_int ("row-span-column",
701 P_("Row span column"),
702 P_("TreeModel column containing the row span values"),
706 GTK_PARAM_READWRITE));
710 * GtkComboBox:column-span-column:
712 * If this is set to a non-negative value, it must be the index of a column
713 * of type %G_TYPE_INT in the model.
715 * The values of that column are used to determine how many columns a value
716 * in the list will span.
720 g_object_class_install_property (object_class,
721 PROP_COLUMN_SPAN_COLUMN,
722 g_param_spec_int ("column-span-column",
723 P_("Column span column"),
724 P_("TreeModel column containing the column span values"),
728 GTK_PARAM_READWRITE));
732 * GtkComboBox:active:
734 * The item which is currently active. If the model is a non-flat treemodel,
735 * and the active item is not an immediate child of the root of the tree,
736 * this property has the value
737 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
738 * where <literal>path</literal> is the #GtkTreePath of the active item.
742 g_object_class_install_property (object_class,
744 g_param_spec_int ("active",
746 P_("The item which is currently active"),
750 GTK_PARAM_READWRITE));
753 * GtkComboBox:add-tearoffs:
755 * The add-tearoffs property controls whether generated menus
756 * have tearoff menu items.
758 * Note that this only affects menu style combo boxes.
762 g_object_class_install_property (object_class,
764 g_param_spec_boolean ("add-tearoffs",
765 P_("Add tearoffs to menus"),
766 P_("Whether dropdowns should have a tearoff menu item"),
768 GTK_PARAM_READWRITE));
771 * GtkComboBox:has-frame:
773 * The has-frame property controls whether a frame
774 * is drawn around the entry.
778 g_object_class_install_property (object_class,
780 g_param_spec_boolean ("has-frame",
782 P_("Whether the combo box draws a frame around the child"),
784 GTK_PARAM_READWRITE));
786 g_object_class_install_property (object_class,
788 g_param_spec_boolean ("focus-on-click",
789 P_("Focus on click"),
790 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
792 GTK_PARAM_READWRITE));
795 * GtkComboBox:tearoff-title:
797 * A title that may be displayed by the window manager
798 * when the popup is torn-off.
802 g_object_class_install_property (object_class,
804 g_param_spec_string ("tearoff-title",
806 P_("A title that may be displayed by the window manager when the popup is torn-off"),
808 GTK_PARAM_READWRITE));
812 * GtkComboBox:popup-shown:
814 * Whether the combo boxes dropdown is popped up.
815 * Note that this property is mainly useful, because
816 * it allows you to connect to notify::popup-shown.
820 g_object_class_install_property (object_class,
822 g_param_spec_boolean ("popup-shown",
824 P_("Whether the combo's dropdown is shown"),
826 GTK_PARAM_READABLE));
830 * GtkComboBox:button-sensitivity:
832 * Whether the dropdown button is sensitive when
833 * the model is empty.
837 g_object_class_install_property (object_class,
838 PROP_BUTTON_SENSITIVITY,
839 g_param_spec_enum ("button-sensitivity",
840 P_("Button Sensitivity"),
841 P_("Whether the dropdown button is sensitive when the model is empty"),
842 GTK_TYPE_SENSITIVITY_TYPE,
843 GTK_SENSITIVITY_AUTO,
844 GTK_PARAM_READWRITE));
846 gtk_widget_class_install_style_property (widget_class,
847 g_param_spec_boolean ("appears-as-list",
848 P_("Appears as list"),
849 P_("Whether dropdowns should look like lists rather than menus"),
851 GTK_PARAM_READABLE));
854 * GtkComboBox:arrow-size:
856 * Sets the minimum size of the arrow in the combo box. Note
857 * that the arrow size is coupled to the font size, so in case
858 * a larger font is used, the arrow will be larger than set
863 gtk_widget_class_install_style_property (widget_class,
864 g_param_spec_int ("arrow-size",
866 P_("The minimum size of the arrow in the combo box"),
870 GTK_PARAM_READABLE));
873 * GtkComboBox:shadow-type:
875 * Which kind of shadow to draw around the combo box.
879 gtk_widget_class_install_style_property (widget_class,
880 g_param_spec_enum ("shadow-type",
882 P_("Which kind of shadow to draw around the combo box"),
883 GTK_TYPE_SHADOW_TYPE,
885 GTK_PARAM_READABLE));
887 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
891 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
893 parent_buildable_iface = g_type_interface_peek_parent (iface);
894 iface->add_child = _gtk_cell_layout_buildable_add_child;
895 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
896 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
900 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
902 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
903 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
904 iface->get_cells = gtk_combo_box_cell_layout_get_cells;
905 iface->clear = gtk_combo_box_cell_layout_clear;
906 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
907 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
908 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
909 iface->reorder = gtk_combo_box_cell_layout_reorder;
913 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
915 iface->start_editing = gtk_combo_box_start_editing;
919 gtk_combo_box_init (GtkComboBox *combo_box)
921 GtkComboBoxPrivate *priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
923 priv->cell_view = gtk_cell_view_new ();
924 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
925 GTK_BIN (combo_box)->child = priv->cell_view;
926 gtk_widget_show (priv->cell_view);
930 priv->wrap_width = 0;
933 priv->active_row = NULL;
934 priv->col_column = -1;
935 priv->row_column = -1;
937 priv->popup_shown = FALSE;
938 priv->add_tearoffs = FALSE;
939 priv->has_frame = TRUE;
940 priv->is_cell_renderer = FALSE;
941 priv->editing_canceled = FALSE;
942 priv->auto_scroll = FALSE;
943 priv->focus_on_click = TRUE;
944 priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
946 combo_box->priv = priv;
948 gtk_combo_box_check_appearance (combo_box);
952 gtk_combo_box_set_property (GObject *object,
957 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
962 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
965 case PROP_WRAP_WIDTH:
966 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
969 case PROP_ROW_SPAN_COLUMN:
970 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
973 case PROP_COLUMN_SPAN_COLUMN:
974 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
978 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
981 case PROP_ADD_TEAROFFS:
982 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
986 combo_box->priv->has_frame = g_value_get_boolean (value);
989 case PROP_FOCUS_ON_CLICK:
990 gtk_combo_box_set_focus_on_click (combo_box,
991 g_value_get_boolean (value));
994 case PROP_TEAROFF_TITLE:
995 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
998 case PROP_POPUP_SHOWN:
999 if (g_value_get_boolean (value))
1000 gtk_combo_box_popup (combo_box);
1002 gtk_combo_box_popdown (combo_box);
1005 case PROP_BUTTON_SENSITIVITY:
1006 gtk_combo_box_set_button_sensitivity (combo_box,
1007 g_value_get_enum (value));
1011 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1017 gtk_combo_box_get_property (GObject *object,
1022 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1027 g_value_set_object (value, combo_box->priv->model);
1030 case PROP_WRAP_WIDTH:
1031 g_value_set_int (value, combo_box->priv->wrap_width);
1034 case PROP_ROW_SPAN_COLUMN:
1035 g_value_set_int (value, combo_box->priv->row_column);
1038 case PROP_COLUMN_SPAN_COLUMN:
1039 g_value_set_int (value, combo_box->priv->col_column);
1043 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1046 case PROP_ADD_TEAROFFS:
1047 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1050 case PROP_HAS_FRAME:
1051 g_value_set_boolean (value, combo_box->priv->has_frame);
1054 case PROP_FOCUS_ON_CLICK:
1055 g_value_set_boolean (value, combo_box->priv->focus_on_click);
1058 case PROP_TEAROFF_TITLE:
1059 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1062 case PROP_POPUP_SHOWN:
1063 g_value_set_boolean (value, combo_box->priv->popup_shown);
1066 case PROP_BUTTON_SENSITIVITY:
1067 g_value_set_enum (value, combo_box->priv->button_sensitivity);
1071 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1077 gtk_combo_box_state_changed (GtkWidget *widget,
1078 GtkStateType previous)
1080 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1081 GtkComboBoxPrivate *priv = combo_box->priv;
1083 if (GTK_WIDGET_REALIZED (widget))
1085 if (priv->tree_view && priv->cell_view)
1086 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1087 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1090 gtk_widget_queue_draw (widget);
1094 gtk_combo_box_button_state_changed (GtkWidget *widget,
1095 GtkStateType previous,
1098 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1099 GtkComboBoxPrivate *priv = combo_box->priv;
1101 if (GTK_WIDGET_REALIZED (widget))
1103 if (!priv->tree_view && priv->cell_view)
1105 if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
1106 (GTK_WIDGET_STATE (priv->cell_view) == GTK_STATE_INSENSITIVE))
1107 gtk_widget_set_sensitive (priv->cell_view, GTK_WIDGET_SENSITIVE (widget));
1109 gtk_widget_set_state (priv->cell_view,
1110 GTK_WIDGET_STATE (widget));
1114 gtk_widget_queue_draw (widget);
1118 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1120 GtkComboBoxPrivate *priv = combo_box->priv;
1121 gboolean appears_as_list;
1123 /* if wrap_width > 0, then we are in grid-mode and forced to use
1126 if (priv->wrap_width)
1127 appears_as_list = FALSE;
1129 gtk_widget_style_get (GTK_WIDGET (combo_box),
1130 "appears-as-list", &appears_as_list,
1133 if (appears_as_list)
1135 /* Destroy all the menu mode widgets, if they exist. */
1136 if (GTK_IS_MENU (priv->popup_widget))
1137 gtk_combo_box_menu_destroy (combo_box);
1139 /* Create the list mode widgets, if they don't already exist. */
1140 if (!GTK_IS_TREE_VIEW (priv->tree_view))
1141 gtk_combo_box_list_setup (combo_box);
1145 /* Destroy all the list mode widgets, if they exist. */
1146 if (GTK_IS_TREE_VIEW (priv->tree_view))
1147 gtk_combo_box_list_destroy (combo_box);
1149 /* Create the menu mode widgets, if they don't already exist. */
1150 if (!GTK_IS_MENU (priv->popup_widget))
1151 gtk_combo_box_menu_setup (combo_box, TRUE);
1154 gtk_widget_style_get (GTK_WIDGET (combo_box),
1155 "shadow-type", &priv->shadow_type,
1160 gtk_combo_box_style_set (GtkWidget *widget,
1163 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1164 GtkComboBoxPrivate *priv = combo_box->priv;
1166 gtk_combo_box_check_appearance (combo_box);
1168 if (priv->tree_view && priv->cell_view)
1169 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
1170 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1172 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1173 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1174 GTK_SHADOW_NONE == priv->shadow_type ?
1175 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1179 gtk_combo_box_button_toggled (GtkWidget *widget,
1182 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1184 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1186 if (!combo_box->priv->popup_in_progress)
1187 gtk_combo_box_popup (combo_box);
1190 gtk_combo_box_popdown (combo_box);
1194 gtk_combo_box_add (GtkContainer *container,
1197 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1198 GtkComboBoxPrivate *priv = combo_box->priv;
1200 if (priv->cell_view && priv->cell_view->parent)
1202 gtk_widget_unparent (priv->cell_view);
1203 GTK_BIN (container)->child = NULL;
1204 gtk_widget_queue_resize (GTK_WIDGET (container));
1207 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1208 GTK_BIN (container)->child = widget;
1210 if (priv->cell_view &&
1211 widget != priv->cell_view)
1213 /* since the cell_view was unparented, it's gone now */
1214 priv->cell_view = NULL;
1216 if (!priv->tree_view && priv->separator)
1218 gtk_container_remove (GTK_CONTAINER (priv->separator->parent),
1220 priv->separator = NULL;
1222 gtk_widget_queue_resize (GTK_WIDGET (container));
1224 else if (priv->cell_view_frame)
1226 gtk_widget_unparent (priv->cell_view_frame);
1227 priv->cell_view_frame = NULL;
1234 gtk_combo_box_remove (GtkContainer *container,
1237 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1238 GtkComboBoxPrivate *priv = combo_box->priv;
1240 gboolean appears_as_list;
1242 if (widget == priv->cell_view)
1243 priv->cell_view = NULL;
1245 gtk_widget_unparent (widget);
1246 GTK_BIN (container)->child = NULL;
1248 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1251 gtk_widget_queue_resize (GTK_WIDGET (container));
1253 if (!priv->tree_view)
1254 appears_as_list = FALSE;
1256 appears_as_list = TRUE;
1258 if (appears_as_list)
1259 gtk_combo_box_list_destroy (combo_box);
1260 else if (GTK_IS_MENU (priv->popup_widget))
1262 gtk_combo_box_menu_destroy (combo_box);
1263 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1264 priv->popup_widget = NULL;
1267 if (!priv->cell_view)
1269 priv->cell_view = gtk_cell_view_new ();
1270 gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1271 GTK_BIN (container)->child = priv->cell_view;
1273 gtk_widget_show (priv->cell_view);
1274 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1276 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->cell_view));
1280 if (appears_as_list)
1281 gtk_combo_box_list_setup (combo_box);
1283 gtk_combo_box_menu_setup (combo_box, TRUE);
1285 if (gtk_tree_row_reference_valid (priv->active_row))
1287 path = gtk_tree_row_reference_get_path (priv->active_row);
1288 gtk_combo_box_set_active_internal (combo_box, path);
1289 gtk_tree_path_free (path);
1292 gtk_combo_box_set_active_internal (combo_box, NULL);
1295 static ComboCellInfo *
1296 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1297 GtkCellRenderer *cell)
1301 for (i = combo_box->priv->cells; i; i = i->next)
1303 ComboCellInfo *info = (ComboCellInfo *)i->data;
1305 if (info && info->cell == cell)
1313 gtk_combo_box_menu_show (GtkWidget *menu,
1316 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1317 GtkComboBoxPrivate *priv = combo_box->priv;
1319 gtk_combo_box_child_show (menu, user_data);
1321 priv->popup_in_progress = TRUE;
1322 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1324 priv->popup_in_progress = FALSE;
1328 gtk_combo_box_menu_hide (GtkWidget *menu,
1331 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1333 gtk_combo_box_child_hide (menu,user_data);
1335 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1340 gtk_combo_box_detacher (GtkWidget *widget,
1343 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1344 GtkComboBoxPrivate *priv = combo_box->priv;
1346 g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1348 g_signal_handlers_disconnect_by_func (menu->toplevel,
1349 gtk_combo_box_menu_show,
1351 g_signal_handlers_disconnect_by_func (menu->toplevel,
1352 gtk_combo_box_menu_hide,
1355 priv->popup_widget = NULL;
1359 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1362 GtkComboBoxPrivate *priv = combo_box->priv;
1364 if (GTK_IS_MENU (priv->popup_widget))
1366 gtk_menu_detach (GTK_MENU (priv->popup_widget));
1367 priv->popup_widget = NULL;
1369 else if (priv->popup_widget)
1371 gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1372 priv->popup_widget);
1373 g_object_unref (priv->popup_widget);
1374 priv->popup_widget = NULL;
1377 if (GTK_IS_MENU (popup))
1379 if (priv->popup_window)
1381 gtk_widget_destroy (priv->popup_window);
1382 priv->popup_window = NULL;
1385 priv->popup_widget = popup;
1388 * Note that we connect to show/hide on the toplevel, not the
1389 * menu itself, since the menu is not shown/hidden when it is
1390 * popped up while torn-off.
1392 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1393 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1394 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1395 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1397 gtk_menu_attach_to_widget (GTK_MENU (popup),
1398 GTK_WIDGET (combo_box),
1399 gtk_combo_box_detacher);
1403 if (!priv->popup_window)
1405 GtkWidget *toplevel;
1407 priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1408 gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1410 gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1411 GDK_WINDOW_TYPE_HINT_COMBO);
1413 g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1414 G_CALLBACK (gtk_combo_box_child_show),
1416 g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1417 G_CALLBACK (gtk_combo_box_child_hide),
1420 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1421 if (GTK_IS_WINDOW (toplevel))
1423 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1424 GTK_WINDOW (priv->popup_window));
1425 gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1426 GTK_WINDOW (toplevel));
1429 gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1430 gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1431 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1433 priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1435 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1438 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1441 gtk_widget_show (priv->scrolled_window);
1443 gtk_container_add (GTK_CONTAINER (priv->popup_window),
1444 priv->scrolled_window);
1447 gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1450 gtk_widget_show (popup);
1451 g_object_ref (popup);
1452 priv->popup_widget = popup;
1457 gtk_combo_box_menu_position_below (GtkMenu *menu,
1463 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1469 GdkRectangle monitor;
1471 /* FIXME: is using the size request here broken? */
1472 child = GTK_BIN (combo_box)->child;
1476 if (GTK_WIDGET_NO_WINDOW (child))
1478 sx += child->allocation.x;
1479 sy += child->allocation.y;
1482 gdk_window_get_root_coords (child->window, sx, sy, &sx, &sy);
1484 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1485 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1487 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1489 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1492 *x = sx + child->allocation.width - req.width;
1495 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1496 monitor_num = gdk_screen_get_monitor_at_window (screen,
1497 GTK_WIDGET (combo_box)->window);
1498 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1502 else if (*x + req.width > monitor.x + monitor.width)
1503 *x = monitor.x + monitor.width - req.width;
1505 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1506 *y += child->allocation.height;
1507 else if (*y - monitor.y >= req.height)
1509 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1510 *y += child->allocation.height;
1518 gtk_combo_box_menu_position_over (GtkMenu *menu,
1524 GtkComboBox *combo_box;
1528 GtkRequisition requisition;
1535 combo_box = GTK_COMBO_BOX (user_data);
1536 widget = GTK_WIDGET (combo_box);
1538 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1539 menu_width = requisition.width;
1541 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1543 menu_xpos = widget->allocation.x;
1544 menu_ypos = widget->allocation.y + widget->allocation.height / 2 - 2;
1548 gtk_widget_get_child_requisition (active, &requisition);
1549 menu_ypos -= requisition.height / 2;
1552 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1555 child = children->data;
1557 if (active == child)
1560 if (GTK_WIDGET_VISIBLE (child))
1562 gtk_widget_get_child_requisition (child, &requisition);
1563 menu_ypos -= requisition.height;
1566 children = children->next;
1569 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1570 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1572 gdk_window_get_root_coords (widget->window, menu_xpos, menu_ypos,
1573 &menu_xpos, &menu_ypos);
1575 /* Clamp the position on screen */
1576 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1580 else if ((menu_xpos + menu_width) > screen_width)
1581 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1590 gtk_combo_box_menu_position (GtkMenu *menu,
1596 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1597 GtkComboBoxPrivate *priv = combo_box->priv;
1598 GtkWidget *menu_item;
1601 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1602 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1605 /* FIXME handle nested menus better */
1606 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1608 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1611 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1614 if (!GTK_WIDGET_VISIBLE (GTK_MENU (priv->popup_widget)->toplevel))
1615 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1616 GDK_WINDOW_TYPE_HINT_COMBO);
1620 gtk_combo_box_list_position (GtkComboBox *combo_box,
1626 GtkComboBoxPrivate *priv = combo_box->priv;
1629 GdkRectangle monitor;
1630 GtkRequisition popup_req;
1631 GtkPolicyType hpolicy, vpolicy;
1633 /* under windows, the drop down list is as wide as the combo box itself.
1635 GtkWidget *sample = GTK_WIDGET (combo_box);
1639 if (GTK_WIDGET_NO_WINDOW (sample))
1641 *x += sample->allocation.x;
1642 *y += sample->allocation.y;
1645 gdk_window_get_root_coords (sample->window, *x, *y, x, y);
1647 *width = sample->allocation.width;
1649 hpolicy = vpolicy = GTK_POLICY_NEVER;
1650 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1652 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1654 if (popup_req.width > *width)
1656 hpolicy = GTK_POLICY_ALWAYS;
1657 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1659 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1662 *height = popup_req.height;
1664 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1665 monitor_num = gdk_screen_get_monitor_at_window (screen,
1666 GTK_WIDGET (combo_box)->window);
1667 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1671 else if (*x + *width > monitor.x + monitor.width)
1672 *x = monitor.x + monitor.width - *width;
1674 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1675 *y += sample->allocation.height;
1676 else if (*y - *height >= monitor.y)
1678 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1680 *y += sample->allocation.height;
1681 *height = monitor.y + monitor.height - *y;
1685 *height = *y - monitor.y;
1689 if (popup_req.height > *height)
1691 vpolicy = GTK_POLICY_ALWAYS;
1693 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1699 cell_view_is_sensitive (GtkCellView *cell_view)
1701 GList *cells, *list;
1704 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view));
1707 for (list = cells; list; list = list->next)
1709 g_object_get (list->data, "sensitive", &sensitive, NULL);
1714 g_list_free (cells);
1720 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1723 GtkComboBoxPrivate *priv = combo_box->priv;
1724 GList *cells, *list;
1730 if (priv->row_separator_func)
1732 if (priv->row_separator_func (priv->model, iter,
1733 priv->row_separator_data))
1737 gtk_tree_view_column_cell_set_cell_data (priv->column,
1739 iter, FALSE, FALSE);
1741 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
1744 for (list = cells; list; list = list->next)
1746 g_object_get (list->data, "sensitive", &sensitive, NULL);
1751 g_list_free (cells);
1757 update_menu_sensitivity (GtkComboBox *combo_box,
1760 GtkComboBoxPrivate *priv = combo_box->priv;
1761 GList *children, *child;
1762 GtkWidget *item, *submenu, *separator;
1763 GtkWidget *cell_view;
1769 children = gtk_container_get_children (GTK_CONTAINER (menu));
1771 for (child = children; child; child = child->next)
1773 item = GTK_WIDGET (child->data);
1774 cell_view = GTK_BIN (item)->child;
1776 if (!GTK_IS_CELL_VIEW (cell_view))
1779 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1780 if (submenu != NULL)
1782 gtk_widget_set_sensitive (item, TRUE);
1783 update_menu_sensitivity (combo_box, submenu);
1787 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1789 if (menu != priv->popup_widget && child == children)
1791 separator = GTK_WIDGET (child->next->data);
1792 g_object_set (item, "visible", sensitive, NULL);
1793 g_object_set (separator, "visible", sensitive, NULL);
1796 gtk_widget_set_sensitive (item, sensitive);
1800 g_list_free (children);
1804 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1806 guint32 activate_time)
1808 GtkComboBoxPrivate *priv = combo_box->priv;
1811 GtkRequisition requisition;
1814 update_menu_sensitivity (combo_box, priv->popup_widget);
1817 if (gtk_tree_row_reference_valid (priv->active_row))
1819 path = gtk_tree_row_reference_get_path (priv->active_row);
1820 active_item = gtk_tree_path_get_indices (path)[0];
1821 gtk_tree_path_free (path);
1823 if (priv->add_tearoffs)
1827 /* FIXME handle nested menus better */
1828 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1830 if (priv->wrap_width == 0)
1832 width = GTK_WIDGET (combo_box)->allocation.width;
1833 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1834 gtk_widget_size_request (priv->popup_widget, &requisition);
1836 gtk_widget_set_size_request (priv->popup_widget,
1837 MAX (width, requisition.width), -1);
1840 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1842 gtk_combo_box_menu_position, combo_box,
1843 button, activate_time);
1847 popup_grab_on_window (GdkWindow *window,
1848 guint32 activate_time,
1849 gboolean grab_keyboard)
1851 if ((gdk_pointer_grab (window, TRUE,
1852 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1853 GDK_POINTER_MOTION_MASK,
1854 NULL, NULL, activate_time) == 0))
1856 if (!grab_keyboard ||
1857 gdk_keyboard_grab (window, TRUE,
1858 activate_time) == 0)
1862 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1872 * gtk_combo_box_popup:
1873 * @combo_box: a #GtkComboBox
1875 * Pops up the menu or dropdown list of @combo_box.
1877 * This function is mostly intended for use by accessibility technologies;
1878 * applications should have little use for it.
1883 gtk_combo_box_popup (GtkComboBox *combo_box)
1885 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1887 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1891 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1893 GtkComboBoxPrivate *priv = combo_box->priv;
1894 gint x, y, width, height;
1895 GtkTreePath *path = NULL, *ppath;
1896 GtkWidget *toplevel;
1898 if (!GTK_WIDGET_REALIZED (combo_box))
1901 if (GTK_WIDGET_MAPPED (priv->popup_widget))
1904 if (GTK_IS_MENU (priv->popup_widget))
1906 gtk_combo_box_menu_popup (combo_box,
1907 priv->activate_button,
1908 priv->activate_time);
1912 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1913 if (GTK_IS_WINDOW (toplevel))
1914 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1915 GTK_WINDOW (priv->popup_window));
1917 gtk_widget_show_all (priv->scrolled_window);
1918 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1920 gtk_widget_set_size_request (priv->popup_window, width, height);
1921 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1923 if (gtk_tree_row_reference_valid (priv->active_row))
1925 path = gtk_tree_row_reference_get_path (priv->active_row);
1926 ppath = gtk_tree_path_copy (path);
1927 if (gtk_tree_path_up (ppath))
1928 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1930 gtk_tree_path_free (ppath);
1932 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1936 gtk_widget_show (priv->popup_window);
1940 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1942 gtk_tree_path_free (path);
1945 gtk_widget_grab_focus (priv->popup_window);
1946 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1949 if (!GTK_WIDGET_HAS_FOCUS (priv->tree_view))
1950 gtk_widget_grab_focus (priv->tree_view);
1952 if (!popup_grab_on_window (priv->popup_window->window,
1953 GDK_CURRENT_TIME, TRUE))
1955 gtk_widget_hide (priv->popup_window);
1959 gtk_grab_add (priv->popup_window);
1963 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1965 if (combo_box->priv->popup_shown)
1967 gtk_combo_box_popdown (combo_box);
1975 * gtk_combo_box_popdown:
1976 * @combo_box: a #GtkComboBox
1978 * Hides the menu or dropdown list of @combo_box.
1980 * This function is mostly intended for use by accessibility technologies;
1981 * applications should have little use for it.
1986 gtk_combo_box_popdown (GtkComboBox *combo_box)
1988 GtkComboBoxPrivate *priv = combo_box->priv;
1990 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1992 if (GTK_IS_MENU (priv->popup_widget))
1994 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
1998 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
2001 gtk_grab_remove (priv->popup_window);
2002 gtk_widget_hide_all (priv->popup_window);
2003 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2008 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2011 GtkComboBoxPrivate *priv = combo_box->priv;
2015 if (priv->cell_view)
2016 gtk_widget_style_get (priv->cell_view,
2017 "focus-line-width", &padding,
2022 /* add some pixels for good measure */
2023 padding += BONUS_PADDING;
2025 if (priv->cell_view)
2026 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2031 return req.width + padding;
2035 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2037 GtkComboBoxPrivate *priv = combo_box->priv;
2042 !gtk_tree_model_get_iter_first (priv->model, &iter))
2048 path = gtk_tree_path_new_from_indices (0, -1);
2054 if (priv->cell_view)
2055 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2063 priv->width = MAX (priv->width, req.width);
2064 priv->height = MAX (priv->height, req.height);
2066 gtk_tree_path_next (path);
2068 while (gtk_tree_model_iter_next (priv->model, &iter));
2070 gtk_tree_path_free (path);
2074 gtk_combo_box_size_request (GtkWidget *widget,
2075 GtkRequisition *requisition)
2078 gint focus_width, focus_pad;
2081 GtkRequisition bin_req;
2082 PangoContext *context;
2083 PangoFontMetrics *metrics;
2084 PangoFontDescription *font_desc;
2086 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2087 GtkComboBoxPrivate *priv = combo_box->priv;
2090 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2091 gtk_combo_box_remeasure (combo_box);
2092 bin_req.width = MAX (bin_req.width, priv->width);
2093 bin_req.height = MAX (bin_req.height, priv->height);
2095 gtk_widget_style_get (GTK_WIDGET (widget),
2096 "focus-line-width", &focus_width,
2097 "focus-padding", &focus_pad,
2098 "arrow-size", &arrow_size,
2101 font_desc = GTK_BIN (widget)->child->style->font_desc;
2102 context = gtk_widget_get_pango_context (widget);
2103 metrics = pango_context_get_metrics (context, font_desc,
2104 pango_context_get_language (context));
2105 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2106 pango_font_metrics_get_descent (metrics));
2107 pango_font_metrics_unref (metrics);
2109 arrow_size = MAX (arrow_size, font_size);
2111 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2113 if (!priv->tree_view)
2117 if (priv->cell_view)
2119 GtkRequisition button_req, sep_req, arrow_req;
2120 gint border_width, xthickness, ythickness;
2122 gtk_widget_size_request (priv->button, &button_req);
2123 border_width = GTK_CONTAINER (combo_box)->border_width;
2124 xthickness = priv->button->style->xthickness;
2125 ythickness = priv->button->style->ythickness;
2127 bin_req.width = MAX (bin_req.width, priv->width);
2128 bin_req.height = MAX (bin_req.height, priv->height);
2130 gtk_widget_size_request (priv->separator, &sep_req);
2131 gtk_widget_size_request (priv->arrow, &arrow_req);
2133 height = MAX (sep_req.height, arrow_req.height);
2134 height = MAX (height, bin_req.height);
2136 width = bin_req.width + sep_req.width + arrow_req.width;
2138 height += 2*(border_width + ythickness + focus_width + focus_pad);
2139 width += 2*(border_width + xthickness + focus_width + focus_pad);
2141 requisition->width = width;
2142 requisition->height = height;
2146 GtkRequisition but_req;
2148 gtk_widget_size_request (priv->button, &but_req);
2150 requisition->width = bin_req.width + but_req.width;
2151 requisition->height = MAX (bin_req.height, but_req.height);
2157 GtkRequisition button_req, frame_req;
2159 /* sample + frame */
2160 *requisition = bin_req;
2162 requisition->width += 2 * focus_width;
2164 if (priv->cell_view_frame)
2166 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2167 if (priv->has_frame)
2169 requisition->width += 2 *
2170 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2171 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2172 requisition->height += 2 *
2173 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2174 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2179 gtk_widget_size_request (priv->button, &button_req);
2181 requisition->height = MAX (requisition->height, button_req.height);
2182 requisition->width += button_req.width;
2185 if (GTK_SHADOW_NONE != priv->shadow_type)
2187 requisition->height += 2 * widget->style->ythickness;
2188 requisition->width += 2 * widget->style->xthickness;
2192 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2193 gtk_widget_size_request (combo_box->priv->button, &req); \
2196 child.x = allocation->x + shadow_width; \
2198 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2200 child.y = allocation->y + shadow_height; \
2201 child.width = req.width; \
2202 child.height = allocation->height - 2 * shadow_height; \
2203 child.width = MAX (1, child.width); \
2204 child.height = MAX (1, child.height); \
2206 gtk_widget_size_allocate (combo_box->priv->button, &child);
2209 gtk_combo_box_size_allocate (GtkWidget *widget,
2210 GtkAllocation *allocation)
2212 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2213 GtkComboBoxPrivate *priv = combo_box->priv;
2214 gint shadow_width, shadow_height;
2215 gint focus_width, focus_pad;
2216 GtkAllocation child;
2218 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2220 widget->allocation = *allocation;
2222 gtk_widget_style_get (GTK_WIDGET (widget),
2223 "focus-line-width", &focus_width,
2224 "focus-padding", &focus_pad,
2227 if (GTK_SHADOW_NONE != priv->shadow_type)
2229 shadow_width = widget->style->xthickness;
2230 shadow_height = widget->style->ythickness;
2238 if (!priv->tree_view)
2240 if (priv->cell_view)
2242 gint border_width, xthickness, ythickness;
2246 allocation->x += shadow_width;
2247 allocation->y += shadow_height;
2248 allocation->width -= 2 * shadow_width;
2249 allocation->height -= 2 * shadow_height;
2251 gtk_widget_size_allocate (priv->button, allocation);
2253 /* set some things ready */
2254 border_width = GTK_CONTAINER (priv->button)->border_width;
2255 xthickness = priv->button->style->xthickness;
2256 ythickness = priv->button->style->ythickness;
2258 child.x = allocation->x;
2259 child.y = allocation->y;
2260 width = allocation->width;
2261 child.height = allocation->height;
2263 if (!priv->is_cell_renderer)
2265 child.x += border_width + xthickness + focus_width + focus_pad;
2266 child.y += border_width + ythickness + focus_width + focus_pad;
2267 width -= 2 * (child.x - allocation->x);
2268 child.height -= 2 * (child.y - allocation->y);
2272 /* handle the children */
2273 gtk_widget_size_request (priv->arrow, &req);
2274 child.width = req.width;
2276 child.x += width - req.width;
2277 child.width = MAX (1, child.width);
2278 child.height = MAX (1, child.height);
2279 gtk_widget_size_allocate (priv->arrow, &child);
2281 child.x += req.width;
2282 gtk_widget_size_request (priv->separator, &req);
2283 child.width = req.width;
2285 child.x -= req.width;
2286 child.width = MAX (1, child.width);
2287 child.height = MAX (1, child.height);
2288 gtk_widget_size_allocate (priv->separator, &child);
2292 child.x += req.width;
2293 child.width = allocation->x + allocation->width
2294 - (border_width + xthickness + focus_width + focus_pad)
2299 child.width = child.x;
2300 child.x = allocation->x
2301 + border_width + xthickness + focus_width + focus_pad;
2302 child.width -= child.x;
2305 if (GTK_WIDGET_VISIBLE (priv->popup_widget))
2308 GtkRequisition requisition;
2310 /* Warning here, without the check in the position func */
2311 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2312 if (priv->wrap_width == 0)
2314 width = GTK_WIDGET (combo_box)->allocation.width;
2315 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2316 gtk_widget_size_request (priv->popup_widget, &requisition);
2317 gtk_widget_set_size_request (priv->popup_widget,
2318 MAX (width, requisition.width), -1);
2322 child.width = MAX (1, child.width);
2323 child.height = MAX (1, child.height);
2324 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2328 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2331 child.x = allocation->x + req.width + shadow_width;
2333 child.x = allocation->x + shadow_width;
2334 child.y = allocation->y + shadow_height;
2335 child.width = allocation->width - req.width - 2 * shadow_width;
2336 child.width = MAX (1, child.width);
2337 child.height = MAX (1, child.height);
2338 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2345 /* Combobox thickness + border-width */
2346 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2347 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2350 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2354 child.x = allocation->x + req.width;
2356 child.x = allocation->x;
2358 child.y = allocation->y;
2359 child.width = allocation->width - req.width;
2360 child.height = allocation->height;
2362 if (priv->cell_view_frame)
2366 child.width = MAX (1, child.width - delta_x * 2);
2367 child.height = MAX (1, child.height - delta_y * 2);
2368 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2371 if (priv->has_frame)
2373 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2374 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2375 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2376 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2380 child.width -= delta_x * 2;
2381 child.height -= delta_y * 2;
2388 child.width -= delta_x * 2;
2389 child.height -= delta_y * 2;
2392 if (GTK_WIDGET_VISIBLE (priv->popup_window))
2394 gint x, y, width, height;
2395 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2396 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2397 gtk_widget_set_size_request (priv->popup_window, width, height);
2401 child.width = MAX (1, child.width);
2402 child.height = MAX (1, child.height);
2404 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2408 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2411 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2413 GtkComboBoxPrivate *priv = combo_box->priv;
2417 g_signal_handler_disconnect (priv->model,
2419 g_signal_handler_disconnect (priv->model,
2421 g_signal_handler_disconnect (priv->model,
2422 priv->reordered_id);
2423 g_signal_handler_disconnect (priv->model,
2428 if (!priv->tree_view)
2430 if (priv->popup_widget)
2431 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2432 (GtkCallback)gtk_widget_destroy, NULL);
2437 g_object_unref (priv->model);
2441 if (priv->active_row)
2443 gtk_tree_row_reference_free (priv->active_row);
2444 priv->active_row = NULL;
2447 if (priv->cell_view)
2448 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2452 gtk_combo_box_forall (GtkContainer *container,
2453 gboolean include_internals,
2454 GtkCallback callback,
2455 gpointer callback_data)
2457 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2458 GtkComboBoxPrivate *priv = combo_box->priv;
2460 if (include_internals)
2463 (* callback) (priv->button, callback_data);
2464 if (priv->cell_view_frame)
2465 (* callback) (priv->cell_view_frame, callback_data);
2468 if (GTK_BIN (container)->child)
2469 (* callback) (GTK_BIN (container)->child, callback_data);
2473 gtk_combo_box_child_show (GtkWidget *widget,
2474 GtkComboBox *combo_box)
2476 GtkComboBoxPrivate *priv = combo_box->priv;
2478 priv->popup_shown = TRUE;
2479 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2483 gtk_combo_box_child_hide (GtkWidget *widget,
2484 GtkComboBox *combo_box)
2486 GtkComboBoxPrivate *priv = combo_box->priv;
2488 priv->popup_shown = FALSE;
2489 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2493 gtk_combo_box_expose_event (GtkWidget *widget,
2494 GdkEventExpose *event)
2496 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2497 GtkComboBoxPrivate *priv = combo_box->priv;
2499 if (GTK_WIDGET_DRAWABLE (widget) &&
2500 GTK_SHADOW_NONE != priv->shadow_type)
2502 gtk_paint_shadow (widget->style, widget->window,
2503 GTK_STATE_NORMAL, priv->shadow_type,
2504 NULL, widget, "combobox",
2505 widget->allocation.x, widget->allocation.y,
2506 widget->allocation.width, widget->allocation.height);
2509 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2510 priv->button, event);
2512 if (priv->tree_view && priv->cell_view_frame)
2514 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2515 priv->cell_view_frame, event);
2518 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2519 GTK_BIN (widget)->child, event);
2534 path_visible (GtkTreeView *view,
2540 /* Note that we rely on the fact that collapsed rows don't have nodes
2542 return _gtk_tree_view_find_node (view, path, &tree, &node);
2546 tree_next_func (GtkTreeModel *model,
2551 SearchData *search_data = (SearchData *)data;
2553 if (search_data->found)
2555 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2558 if (search_data->visible &&
2559 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2562 search_data->set = TRUE;
2563 search_data->iter = *iter;
2568 if (gtk_tree_path_compare (path, search_data->path) == 0)
2569 search_data->found = TRUE;
2575 tree_next (GtkComboBox *combo,
2576 GtkTreeModel *model,
2581 SearchData search_data;
2583 search_data.combo = combo;
2584 search_data.path = gtk_tree_model_get_path (model, iter);
2585 search_data.visible = visible;
2586 search_data.found = FALSE;
2587 search_data.set = FALSE;
2589 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2591 *next = search_data.iter;
2593 gtk_tree_path_free (search_data.path);
2595 return search_data.set;
2599 tree_prev_func (GtkTreeModel *model,
2604 SearchData *search_data = (SearchData *)data;
2606 if (gtk_tree_path_compare (path, search_data->path) == 0)
2608 search_data->found = TRUE;
2612 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2615 if (search_data->visible &&
2616 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2619 search_data->set = TRUE;
2620 search_data->iter = *iter;
2626 tree_prev (GtkComboBox *combo,
2627 GtkTreeModel *model,
2632 SearchData search_data;
2634 search_data.combo = combo;
2635 search_data.path = gtk_tree_model_get_path (model, iter);
2636 search_data.visible = visible;
2637 search_data.found = FALSE;
2638 search_data.set = FALSE;
2640 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2642 *prev = search_data.iter;
2644 gtk_tree_path_free (search_data.path);
2646 return search_data.set;
2650 tree_last_func (GtkTreeModel *model,
2655 SearchData *search_data = (SearchData *)data;
2657 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2660 /* Note that we rely on the fact that collapsed rows don't have nodes
2662 if (search_data->visible &&
2663 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2666 search_data->set = TRUE;
2667 search_data->iter = *iter;
2673 tree_last (GtkComboBox *combo,
2674 GtkTreeModel *model,
2678 SearchData search_data;
2680 search_data.combo = combo;
2681 search_data.visible = visible;
2682 search_data.set = FALSE;
2684 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2686 *last = search_data.iter;
2688 return search_data.set;
2693 tree_first_func (GtkTreeModel *model,
2698 SearchData *search_data = (SearchData *)data;
2700 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2703 if (search_data->visible &&
2704 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2707 search_data->set = TRUE;
2708 search_data->iter = *iter;
2714 tree_first (GtkComboBox *combo,
2715 GtkTreeModel *model,
2719 SearchData search_data;
2721 search_data.combo = combo;
2722 search_data.visible = visible;
2723 search_data.set = FALSE;
2725 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2727 *first = search_data.iter;
2729 return search_data.set;
2733 gtk_combo_box_scroll_event (GtkWidget *widget,
2734 GdkEventScroll *event)
2736 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2739 GtkTreeIter new_iter;
2741 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2744 if (event->direction == GDK_SCROLL_UP)
2745 found = tree_prev (combo_box, combo_box->priv->model,
2746 &iter, &new_iter, FALSE);
2748 found = tree_next (combo_box, combo_box->priv->model,
2749 &iter, &new_iter, FALSE);
2752 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2762 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2763 GtkCellLayout *cell_layout)
2765 GtkComboBoxPrivate *priv = combo_box->priv;
2768 for (k = priv->cells; k; k = k->next)
2771 ComboCellInfo *info = (ComboCellInfo *)k->data;
2773 if (info->pack == GTK_PACK_START)
2774 gtk_cell_layout_pack_start (cell_layout,
2775 info->cell, info->expand);
2776 else if (info->pack == GTK_PACK_END)
2777 gtk_cell_layout_pack_end (cell_layout,
2778 info->cell, info->expand);
2780 gtk_cell_layout_set_cell_data_func (cell_layout,
2782 combo_cell_data_func, info, NULL);
2784 for (j = info->attributes; j; j = j->next->next)
2786 gtk_cell_layout_add_attribute (cell_layout,
2789 GPOINTER_TO_INT (j->next->data));
2795 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2796 gboolean add_children)
2798 GtkComboBoxPrivate *priv = combo_box->priv;
2801 if (priv->cell_view)
2803 priv->button = gtk_toggle_button_new ();
2804 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2805 priv->focus_on_click);
2807 g_signal_connect (priv->button, "toggled",
2808 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2809 gtk_widget_set_parent (priv->button,
2810 GTK_BIN (combo_box)->child->parent);
2812 priv->box = gtk_hbox_new (FALSE, 0);
2813 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2815 priv->separator = gtk_vseparator_new ();
2816 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2818 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2819 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2821 gtk_widget_show_all (priv->button);
2825 priv->button = gtk_toggle_button_new ();
2826 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2827 priv->focus_on_click);
2829 g_signal_connect (priv->button, "toggled",
2830 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2831 gtk_widget_set_parent (priv->button,
2832 GTK_BIN (combo_box)->child->parent);
2834 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2835 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2836 gtk_widget_show_all (priv->button);
2839 g_signal_connect (priv->button, "button-press-event",
2840 G_CALLBACK (gtk_combo_box_menu_button_press),
2842 g_signal_connect (priv->button, "state-changed",
2843 G_CALLBACK (gtk_combo_box_button_state_changed),
2846 /* create our funky menu */
2847 menu = gtk_menu_new ();
2848 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2849 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
2851 g_signal_connect (menu, "key-press-event",
2852 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2853 gtk_combo_box_set_popup_widget (combo_box, menu);
2857 gtk_combo_box_menu_fill (combo_box);
2859 /* the column is needed in tree_column_row_is_sensitive() */
2860 priv->column = gtk_tree_view_column_new ();
2861 g_object_ref_sink (priv->column);
2862 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2864 gtk_combo_box_update_title (combo_box);
2868 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2870 GtkComboBoxPrivate *priv = combo_box->priv;
2876 menu = priv->popup_widget;
2878 if (priv->add_tearoffs)
2880 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2882 gtk_widget_show (tearoff);
2884 if (priv->wrap_width)
2885 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2887 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2890 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2894 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2895 GtkTreeModel *model,
2898 GtkWidget *cell_view;
2903 cell_view = gtk_cell_view_new ();
2904 item = gtk_menu_item_new ();
2905 gtk_container_add (GTK_CONTAINER (item), cell_view);
2907 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2908 path = gtk_tree_model_get_path (model, iter);
2909 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2910 gtk_tree_path_free (path);
2912 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2913 gtk_widget_size_request (cell_view, &req);
2914 gtk_widget_show (cell_view);
2920 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2922 GtkTreeIter *parent)
2924 GtkComboBoxPrivate *priv = combo_box->priv;
2925 GtkTreeModel *model = priv->model;
2926 GtkWidget *item, *submenu, *subitem, *separator;
2928 gboolean is_separator;
2933 n_children = gtk_tree_model_iter_n_children (model, parent);
2936 for (i = 0; i < n_children; i++)
2938 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2940 if (priv->row_separator_func)
2941 is_separator = priv->row_separator_func (priv->model, &iter,
2942 priv->row_separator_data);
2944 is_separator = FALSE;
2948 item = gtk_separator_menu_item_new ();
2949 path = gtk_tree_model_get_path (model, &iter);
2950 g_object_set_data_full (G_OBJECT (item),
2951 I_("gtk-combo-box-item-path"),
2952 gtk_tree_row_reference_new (model, path),
2953 (GDestroyNotify)gtk_tree_row_reference_free);
2954 gtk_tree_path_free (path);
2958 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2959 if (gtk_tree_model_iter_has_child (model, &iter))
2961 submenu = gtk_menu_new ();
2962 gtk_menu_set_reserve_toggle_size (GTK_MENU (submenu), FALSE);
2963 gtk_widget_show (submenu);
2964 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2966 /* Ugly - since menus can only activate leafs, we have to
2967 * duplicate the item inside the submenu.
2969 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2970 separator = gtk_separator_menu_item_new ();
2971 gtk_widget_show (subitem);
2972 gtk_widget_show (separator);
2973 g_signal_connect (subitem, "activate",
2974 G_CALLBACK (gtk_combo_box_menu_item_activate),
2976 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2977 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2979 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2981 g_signal_connect (item, "activate",
2982 G_CALLBACK (gtk_combo_box_menu_item_activate),
2986 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2987 if (priv->wrap_width && menu == priv->popup_widget)
2988 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2989 gtk_widget_show (item);
2996 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2998 GtkComboBoxPrivate *priv = combo_box->priv;
3000 g_signal_handlers_disconnect_matched (priv->button,
3001 G_SIGNAL_MATCH_DATA,
3003 gtk_combo_box_menu_button_press, NULL);
3004 g_signal_handlers_disconnect_matched (priv->button,
3005 G_SIGNAL_MATCH_DATA,
3007 gtk_combo_box_button_state_changed, combo_box);
3009 /* unparent will remove our latest ref */
3010 gtk_widget_unparent (priv->button);
3013 priv->button = NULL;
3015 priv->separator = NULL;
3017 g_object_unref (priv->column);
3018 priv->column = NULL;
3020 /* changing the popup window will unref the menu and the children */
3028 menu_occupied (GtkMenu *menu,
3032 guint bottom_attach)
3036 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3040 gtk_container_child_get (GTK_CONTAINER (menu),
3044 "bottom-attach", &b,
3048 /* look if this item intersects with the given coordinates */
3049 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3057 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3062 GtkComboBoxPrivate *priv = combo_box->priv;
3063 gint current_col = 0, current_row = 0;
3064 gint rows = 1, cols = 1;
3065 GtkWidget *menu = priv->popup_widget;
3067 if (!GTK_IS_MENU_SHELL (menu))
3070 if (priv->col_column == -1 &&
3071 priv->row_column == -1 &&
3074 gtk_container_child_get (GTK_CONTAINER (menu),
3076 "right-attach", ¤t_col,
3077 "top-attach", ¤t_row,
3079 if (current_col + cols > priv->wrap_width)
3087 if (priv->col_column != -1)
3088 gtk_tree_model_get (priv->model, iter,
3089 priv->col_column, &cols,
3091 if (priv->row_column != -1)
3092 gtk_tree_model_get (priv->model, iter,
3093 priv->row_column, &rows,
3098 if (current_col + cols > priv->wrap_width)
3104 if (!menu_occupied (GTK_MENU (menu),
3105 current_col, current_col + cols,
3106 current_row, current_row + rows))
3113 /* set attach props */
3114 gtk_menu_attach (GTK_MENU (menu), item,
3115 current_col, current_col + cols,
3116 current_row, current_row + rows);
3120 gtk_combo_box_relayout (GtkComboBox *combo_box)
3125 menu = combo_box->priv->popup_widget;
3127 /* do nothing unless we are in menu style and realized */
3128 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3131 list = gtk_container_get_children (GTK_CONTAINER (menu));
3133 for (j = g_list_last (list); j; j = j->prev)
3134 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3136 gtk_combo_box_menu_fill (combo_box);
3143 gtk_combo_box_menu_button_press (GtkWidget *widget,
3144 GdkEventButton *event,
3147 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3148 GtkComboBoxPrivate *priv = combo_box->priv;
3150 if (GTK_IS_MENU (priv->popup_widget) &&
3151 event->type == GDK_BUTTON_PRESS && event->button == 1)
3153 if (priv->focus_on_click &&
3154 !GTK_WIDGET_HAS_FOCUS (priv->button))
3155 gtk_widget_grab_focus (priv->button);
3157 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3166 gtk_combo_box_menu_item_activate (GtkWidget *item,
3169 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3170 GtkWidget *cell_view;
3174 cell_view = GTK_BIN (item)->child;
3176 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3178 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3180 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3182 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3183 gtk_combo_box_set_active_iter (combo_box, &iter);
3186 gtk_tree_path_free (path);
3188 combo_box->priv->editing_canceled = FALSE;
3192 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3195 gboolean sensitive = TRUE; /* fool code checkers */
3197 if (!combo_box->priv->button)
3200 switch (combo_box->priv->button_sensitivity)
3202 case GTK_SENSITIVITY_ON:
3205 case GTK_SENSITIVITY_OFF:
3208 case GTK_SENSITIVITY_AUTO:
3209 sensitive = combo_box->priv->model &&
3210 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3213 g_assert_not_reached ();
3217 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3221 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3226 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3228 if (combo_box->priv->tree_view)
3229 gtk_combo_box_list_popup_resize (combo_box);
3231 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3233 gtk_combo_box_update_sensitivity (combo_box);
3237 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3241 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3242 GtkComboBoxPrivate *priv = combo_box->priv;
3244 if (!gtk_tree_row_reference_valid (priv->active_row))
3246 if (priv->cell_view)
3247 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3248 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3251 if (priv->tree_view)
3252 gtk_combo_box_list_popup_resize (combo_box);
3254 gtk_combo_box_menu_row_deleted (model, path, user_data);
3256 gtk_combo_box_update_sensitivity (combo_box);
3260 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3266 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3268 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3270 if (!combo_box->priv->tree_view)
3271 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3275 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3280 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3281 GtkComboBoxPrivate *priv = combo_box->priv;
3282 GtkTreePath *active_path;
3284 /* FIXME this belongs to GtkCellView */
3285 if (gtk_tree_row_reference_valid (priv->active_row))
3287 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3288 if (gtk_tree_path_compare (path, active_path) == 0 &&
3290 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3291 gtk_tree_path_free (active_path);
3294 if (priv->tree_view)
3295 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3297 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3301 list_popup_resize_idle (gpointer user_data)
3303 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3304 GtkComboBoxPrivate *priv = combo_box->priv;
3305 gint x, y, width, height;
3307 if (priv->tree_view && GTK_WIDGET_MAPPED (priv->popup_window))
3309 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3311 gtk_widget_set_size_request (priv->popup_window, width, height);
3312 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3315 priv->resize_idle_id = 0;
3321 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3323 GtkComboBoxPrivate *priv = combo_box->priv;
3325 if (!priv->resize_idle_id)
3326 priv->resize_idle_id =
3327 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3331 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3336 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3338 gtk_combo_box_list_popup_resize (combo_box);
3343 find_menu_by_path (GtkWidget *menu,
3345 gboolean skip_first)
3350 GtkTreeRowReference *mref;
3354 list = gtk_container_get_children (GTK_CONTAINER (menu));
3357 for (i = list; i; i = i->next)
3359 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3361 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3364 else if (!gtk_tree_row_reference_valid (mref))
3367 mpath = gtk_tree_row_reference_get_path (mref);
3369 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3377 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3382 /* this case is necessary, since the row reference of
3383 * the cell view may already be updated after a deletion
3390 if (gtk_tree_path_compare (mpath, path) == 0)
3392 gtk_tree_path_free (mpath);
3396 if (gtk_tree_path_is_ancestor (mpath, path))
3398 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3399 if (submenu != NULL)
3401 gtk_tree_path_free (mpath);
3402 item = find_menu_by_path (submenu, path, TRUE);
3406 gtk_tree_path_free (mpath);
3416 dump_menu_tree (GtkWidget *menu,
3423 list = gtk_container_get_children (GTK_CONTAINER (menu));
3424 for (i = list; i; i = i->next)
3426 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3428 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3429 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3430 gtk_tree_path_free (path);
3432 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3433 if (submenu != NULL)
3434 dump_menu_tree (submenu, level + 1);
3443 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3448 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3449 GtkComboBoxPrivate *priv = combo_box->priv;
3451 GtkWidget *item, *menu, *separator;
3455 gboolean is_separator;
3457 if (!priv->popup_widget)
3460 depth = gtk_tree_path_get_depth (path);
3461 pos = gtk_tree_path_get_indices (path)[depth - 1];
3464 ppath = gtk_tree_path_copy (path);
3465 gtk_tree_path_up (ppath);
3466 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3467 gtk_tree_path_free (ppath);
3469 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3472 menu = gtk_menu_new ();
3473 gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
3474 gtk_widget_show (menu);
3475 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3477 /* Ugly - since menus can only activate leaves, we have to
3478 * duplicate the item inside the submenu.
3480 gtk_tree_model_iter_parent (model, &piter, iter);
3481 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3482 separator = gtk_separator_menu_item_new ();
3483 g_signal_connect (item, "activate",
3484 G_CALLBACK (gtk_combo_box_menu_item_activate),
3486 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3487 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3488 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3490 gtk_widget_show (item);
3491 gtk_widget_show (separator);
3498 menu = priv->popup_widget;
3499 if (priv->add_tearoffs)
3503 if (priv->row_separator_func)
3504 is_separator = priv->row_separator_func (model, iter,
3505 priv->row_separator_data);
3507 is_separator = FALSE;
3511 item = gtk_separator_menu_item_new ();
3512 g_object_set_data_full (G_OBJECT (item),
3513 I_("gtk-combo-box-item-path"),
3514 gtk_tree_row_reference_new (model, path),
3515 (GDestroyNotify)gtk_tree_row_reference_free);
3519 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3521 g_signal_connect (item, "activate",
3522 G_CALLBACK (gtk_combo_box_menu_item_activate),
3526 gtk_widget_show (item);
3527 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3531 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3535 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3536 GtkComboBoxPrivate *priv = combo_box->priv;
3540 if (!priv->popup_widget)
3543 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3544 menu = gtk_widget_get_parent (item);
3545 gtk_container_remove (GTK_CONTAINER (menu), item);
3547 if (gtk_tree_path_get_depth (path) > 1)
3549 GtkTreePath *parent_path;
3553 parent_path = gtk_tree_path_copy (path);
3554 gtk_tree_path_up (parent_path);
3555 gtk_tree_model_get_iter (model, &iter, parent_path);
3557 if (!gtk_tree_model_iter_has_child (model, &iter))
3559 parent = find_menu_by_path (priv->popup_widget,
3560 parent_path, FALSE);
3561 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3567 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3573 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3575 gtk_combo_box_relayout (combo_box);
3579 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3584 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3585 GtkComboBoxPrivate *priv = combo_box->priv;
3588 gboolean is_separator;
3590 if (!priv->popup_widget)
3593 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3595 if (priv->row_separator_func)
3596 is_separator = priv->row_separator_func (model, iter,
3597 priv->row_separator_data);
3599 is_separator = FALSE;
3601 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3603 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3604 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3607 if (priv->wrap_width && item->parent == priv->popup_widget)
3609 GtkWidget *pitem = NULL;
3612 prev = gtk_tree_path_copy (path);
3614 if (gtk_tree_path_prev (prev))
3615 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3617 gtk_tree_path_free (prev);
3619 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3621 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3626 "bottom-attach", -1,
3629 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3632 width = gtk_combo_box_calc_requested_width (combo_box, path);
3634 if (width > priv->width)
3636 if (priv->cell_view)
3638 gtk_widget_set_size_request (priv->cell_view, width, -1);
3639 gtk_widget_queue_resize (priv->cell_view);
3641 priv->width = width;
3650 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3652 GtkComboBoxPrivate *priv = combo_box->priv;
3653 GtkTreeSelection *sel;
3655 priv->button = gtk_toggle_button_new ();
3656 gtk_widget_set_parent (priv->button,
3657 GTK_BIN (combo_box)->child->parent);
3658 g_signal_connect (priv->button, "button-press-event",
3659 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3660 g_signal_connect (priv->button, "toggled",
3661 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3663 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3664 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3665 priv->separator = NULL;
3666 gtk_widget_show_all (priv->button);
3668 if (priv->cell_view)
3670 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3671 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3673 priv->box = gtk_event_box_new ();
3674 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3677 if (priv->has_frame)
3679 priv->cell_view_frame = gtk_frame_new (NULL);
3680 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3685 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3686 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3690 gtk_widget_set_parent (priv->cell_view_frame,
3691 GTK_BIN (combo_box)->child->parent);
3692 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3693 gtk_widget_show_all (priv->cell_view_frame);
3695 g_signal_connect (priv->box, "button-press-event",
3696 G_CALLBACK (gtk_combo_box_list_button_pressed),
3700 priv->tree_view = gtk_tree_view_new ();
3701 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3702 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3703 gtk_tree_selection_set_select_function (sel,
3704 gtk_combo_box_list_select_func,
3706 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3708 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3710 if (priv->row_separator_func)
3711 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3712 priv->row_separator_func,
3713 priv->row_separator_data,
3716 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3718 priv->column = gtk_tree_view_column_new ();
3719 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3722 gtk_combo_box_sync_cells (combo_box,
3723 GTK_CELL_LAYOUT (priv->column));
3725 if (gtk_tree_row_reference_valid (priv->active_row))
3729 path = gtk_tree_row_reference_get_path (priv->active_row);
3730 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3732 gtk_tree_path_free (path);
3735 /* set sample/popup widgets */
3736 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3738 g_signal_connect (priv->tree_view, "key-press-event",
3739 G_CALLBACK (gtk_combo_box_list_key_press),
3741 g_signal_connect (priv->tree_view, "enter-notify-event",
3742 G_CALLBACK (gtk_combo_box_list_enter_notify),
3744 g_signal_connect (priv->tree_view, "row-expanded",
3745 G_CALLBACK (gtk_combo_box_model_row_expanded),
3747 g_signal_connect (priv->tree_view, "row-collapsed",
3748 G_CALLBACK (gtk_combo_box_model_row_expanded),
3750 g_signal_connect (priv->popup_window, "button-press-event",
3751 G_CALLBACK (gtk_combo_box_list_button_pressed),
3753 g_signal_connect (priv->popup_window, "button-release-event",
3754 G_CALLBACK (gtk_combo_box_list_button_released),
3757 gtk_widget_show (priv->tree_view);
3761 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3763 GtkComboBoxPrivate *priv = combo_box->priv;
3765 /* disconnect signals */
3766 g_signal_handlers_disconnect_matched (priv->tree_view,
3767 G_SIGNAL_MATCH_DATA,
3768 0, 0, NULL, NULL, combo_box);
3769 g_signal_handlers_disconnect_matched (priv->button,
3770 G_SIGNAL_MATCH_DATA,
3772 gtk_combo_box_list_button_pressed,
3774 g_signal_handlers_disconnect_matched (priv->popup_window,
3775 G_SIGNAL_MATCH_DATA,
3777 gtk_combo_box_list_button_pressed,
3779 g_signal_handlers_disconnect_matched (priv->popup_window,
3780 G_SIGNAL_MATCH_DATA,
3782 gtk_combo_box_list_button_released,
3785 g_signal_handlers_disconnect_matched (priv->popup_window,
3786 G_SIGNAL_MATCH_DATA,
3788 gtk_combo_box_child_show,
3791 g_signal_handlers_disconnect_matched (priv->popup_window,
3792 G_SIGNAL_MATCH_DATA,
3794 gtk_combo_box_child_hide,
3798 g_signal_handlers_disconnect_matched (priv->box,
3799 G_SIGNAL_MATCH_DATA,
3801 gtk_combo_box_list_button_pressed,
3804 /* destroy things (unparent will kill the latest ref from us)
3805 * last unref on button will destroy the arrow
3807 gtk_widget_unparent (priv->button);
3808 priv->button = NULL;
3811 if (priv->cell_view)
3813 g_object_set (priv->cell_view,
3814 "background-set", FALSE,
3818 if (priv->cell_view_frame)
3820 gtk_widget_unparent (priv->cell_view_frame);
3821 priv->cell_view_frame = NULL;
3825 if (priv->scroll_timer)
3827 g_source_remove (priv->scroll_timer);
3828 priv->scroll_timer = 0;
3831 if (priv->resize_idle_id)
3833 g_source_remove (priv->resize_idle_id);
3834 priv->resize_idle_id = 0;
3837 gtk_widget_destroy (priv->tree_view);
3839 priv->tree_view = NULL;
3840 if (priv->popup_widget)
3842 g_object_unref (priv->popup_widget);
3843 priv->popup_widget = NULL;
3850 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3851 GdkEventButton *event,
3854 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3855 GtkComboBoxPrivate *priv = combo_box->priv;
3857 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3859 if (ewidget == priv->popup_window)
3862 if ((ewidget != priv->button && ewidget != priv->box) ||
3863 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3866 if (priv->focus_on_click &&
3867 !GTK_WIDGET_HAS_FOCUS (priv->button))
3868 gtk_widget_grab_focus (priv->button);
3870 gtk_combo_box_popup (combo_box);
3872 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3874 priv->auto_scroll = FALSE;
3875 if (priv->scroll_timer == 0)
3876 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3877 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3880 priv->popup_in_progress = TRUE;
3886 gtk_combo_box_list_button_released (GtkWidget *widget,
3887 GdkEventButton *event,
3891 GtkTreePath *path = NULL;
3894 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3895 GtkComboBoxPrivate *priv = combo_box->priv;
3897 gboolean popup_in_progress = FALSE;
3899 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3901 if (priv->popup_in_progress)
3903 popup_in_progress = TRUE;
3904 priv->popup_in_progress = FALSE;
3907 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3909 if (priv->scroll_timer)
3911 g_source_remove (priv->scroll_timer);
3912 priv->scroll_timer = 0;
3915 if (ewidget != priv->tree_view)
3917 if ((ewidget == priv->button ||
3918 ewidget == priv->box) &&
3919 !popup_in_progress &&
3920 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3922 gtk_combo_box_popdown (combo_box);
3926 /* released outside treeview */
3927 if (ewidget != priv->button && ewidget != priv->box)
3929 gtk_combo_box_popdown (combo_box);
3937 /* select something cool */
3938 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3944 return TRUE; /* clicked outside window? */
3946 gtk_tree_model_get_iter (priv->model, &iter, path);
3947 gtk_tree_path_free (path);
3949 gtk_combo_box_popdown (combo_box);
3951 if (tree_column_row_is_sensitive (combo_box, &iter))
3952 gtk_combo_box_set_active_iter (combo_box, &iter);
3958 gtk_combo_box_menu_key_press (GtkWidget *widget,
3962 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3964 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3966 /* The menu hasn't managed the
3967 * event, forward it to the combobox
3969 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3976 gtk_combo_box_list_key_press (GtkWidget *widget,
3980 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3983 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
3984 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
3986 GtkTreeModel *model = NULL;
3988 gtk_combo_box_popdown (combo_box);
3990 if (combo_box->priv->model)
3992 GtkTreeSelection *sel;
3994 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3996 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3997 gtk_combo_box_set_active_iter (combo_box, &iter);
4003 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
4005 /* The list hasn't managed the
4006 * event, forward it to the combobox
4008 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4015 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4019 GtkWidget *tree_view = combo_box->priv->tree_view;
4023 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4024 if (adj && adj->upper - adj->lower > adj->page_size)
4026 if (x <= tree_view->allocation.x &&
4027 adj->lower < adj->value)
4029 value = adj->value - (tree_view->allocation.x - x + 1);
4030 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
4032 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4033 adj->upper - adj->page_size > adj->value)
4035 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4036 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
4040 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4041 if (adj && adj->upper - adj->lower > adj->page_size)
4043 if (y <= tree_view->allocation.y &&
4044 adj->lower < adj->value)
4046 value = adj->value - (tree_view->allocation.y - y + 1);
4047 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
4049 else if (y >= tree_view->allocation.height &&
4050 adj->upper - adj->page_size > adj->value)
4052 value = adj->value + (y - tree_view->allocation.height + 1);
4053 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
4059 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4061 GtkComboBoxPrivate *priv = combo_box->priv;
4064 if (priv->auto_scroll)
4066 gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
4067 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4074 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4075 GdkEventCrossing *event,
4078 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4080 combo_box->priv->auto_scroll = TRUE;
4086 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4087 GtkTreeModel *model,
4089 gboolean path_currently_selected,
4093 gboolean sensitive = FALSE;
4095 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4097 GList *cells, *cell;
4098 gboolean cell_sensitive, cell_visible;
4100 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4102 if (!column->visible)
4105 gtk_tree_model_get_iter (model, &iter, path);
4106 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4109 cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
4112 g_object_get (cell->data,
4113 "sensitive", &cell_sensitive,
4114 "visible", &cell_visible,
4117 if (cell_visible && cell_sensitive)
4122 g_list_free (cells);
4124 sensitive = cell_sensitive;
4131 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4136 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4137 GtkComboBoxPrivate *priv = combo_box->priv;
4140 width = gtk_combo_box_calc_requested_width (combo_box, path);
4142 if (width > priv->width)
4144 if (priv->cell_view)
4146 gtk_widget_set_size_request (priv->cell_view, width, -1);
4147 gtk_widget_queue_resize (priv->cell_view);
4149 priv->width = width;
4154 * GtkCellLayout implementation
4158 pack_start_recurse (GtkWidget *menu,
4159 GtkCellRenderer *cell,
4165 list = gtk_container_get_children (GTK_CONTAINER (menu));
4166 for (i = list; i; i = i->next)
4168 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4169 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4172 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4173 if (submenu != NULL)
4174 pack_start_recurse (submenu, cell, expand);
4181 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4182 GtkCellRenderer *cell,
4185 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4186 ComboCellInfo *info;
4187 GtkComboBoxPrivate *priv;
4189 priv = combo_box->priv;
4191 g_object_ref_sink (cell);
4193 info = g_slice_new0 (ComboCellInfo);
4195 info->expand = expand;
4196 info->pack = GTK_PACK_START;
4198 priv->cells = g_slist_append (priv->cells, info);
4200 if (priv->cell_view)
4201 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4205 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4207 if (GTK_IS_MENU (priv->popup_widget))
4208 pack_start_recurse (priv->popup_widget, cell, expand);
4212 pack_end_recurse (GtkWidget *menu,
4213 GtkCellRenderer *cell,
4219 list = gtk_container_get_children (GTK_CONTAINER (menu));
4220 for (i = list; i; i = i->next)
4222 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4223 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4226 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4227 if (submenu != NULL)
4228 pack_end_recurse (submenu, cell, expand);
4235 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4236 GtkCellRenderer *cell,
4239 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4240 ComboCellInfo *info;
4241 GtkComboBoxPrivate *priv;
4243 priv = combo_box->priv;
4245 g_object_ref_sink (cell);
4247 info = g_slice_new0 (ComboCellInfo);
4249 info->expand = expand;
4250 info->pack = GTK_PACK_END;
4252 priv->cells = g_slist_append (priv->cells, info);
4254 if (priv->cell_view)
4255 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4259 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4261 if (GTK_IS_MENU (priv->popup_widget))
4262 pack_end_recurse (priv->popup_widget, cell, expand);
4266 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4268 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4270 GList *retval = NULL;
4272 for (list = combo_box->priv->cells; list; list = list->next)
4274 ComboCellInfo *info = (ComboCellInfo *)list->data;
4276 retval = g_list_prepend (retval, info->cell);
4279 return g_list_reverse (retval);
4283 clear_recurse (GtkWidget *menu)
4288 list = gtk_container_get_children (GTK_CONTAINER (menu));
4289 for (i = list; i; i = i->next)
4291 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4292 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4294 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4295 if (submenu != NULL)
4296 clear_recurse (submenu);
4303 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4305 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4306 GtkComboBoxPrivate *priv = combo_box->priv;
4309 if (priv->cell_view)
4310 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4313 gtk_tree_view_column_clear (priv->column);
4315 for (i = priv->cells; i; i = i->next)
4317 ComboCellInfo *info = (ComboCellInfo *)i->data;
4319 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4320 g_object_unref (info->cell);
4321 g_slice_free (ComboCellInfo, info);
4324 g_slist_free (priv->cells);
4327 if (GTK_IS_MENU (priv->popup_widget))
4328 clear_recurse (priv->popup_widget);
4332 add_attribute_recurse (GtkWidget *menu,
4333 GtkCellRenderer *cell,
4334 const gchar *attribute,
4340 list = gtk_container_get_children (GTK_CONTAINER (menu));
4341 for (i = list; i; i = i->next)
4343 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4344 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4345 cell, attribute, column);
4347 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4348 if (submenu != NULL)
4349 add_attribute_recurse (submenu, cell, attribute, column);
4356 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4357 GtkCellRenderer *cell,
4358 const gchar *attribute,
4361 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4362 ComboCellInfo *info;
4364 info = gtk_combo_box_get_cell_info (combo_box, cell);
4365 g_return_if_fail (info != NULL);
4367 info->attributes = g_slist_prepend (info->attributes,
4368 GINT_TO_POINTER (column));
4369 info->attributes = g_slist_prepend (info->attributes,
4370 g_strdup (attribute));
4372 if (combo_box->priv->cell_view)
4373 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4374 cell, attribute, column);
4376 if (combo_box->priv->column)
4377 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4378 cell, attribute, column);
4380 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4381 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4382 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4386 combo_cell_data_func (GtkCellLayout *cell_layout,
4387 GtkCellRenderer *cell,
4388 GtkTreeModel *tree_model,
4392 ComboCellInfo *info = (ComboCellInfo *)data;
4393 GtkWidget *parent = NULL;
4398 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4400 if (GTK_IS_WIDGET (cell_layout))
4401 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4403 if (GTK_IS_MENU_ITEM (parent) &&
4404 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4405 g_object_set (cell, "sensitive", TRUE, NULL);
4410 set_cell_data_func_recurse (GtkWidget *menu,
4411 GtkCellRenderer *cell,
4412 ComboCellInfo *info)
4416 GtkWidget *cell_view;
4418 list = gtk_container_get_children (GTK_CONTAINER (menu));
4419 for (i = list; i; i = i->next)
4421 cell_view = GTK_BIN (i->data)->child;
4422 if (GTK_IS_CELL_LAYOUT (cell_view))
4424 /* Override sensitivity for inner nodes; we don't
4425 * want menuitems with submenus to appear insensitive
4427 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4429 combo_cell_data_func,
4431 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4432 if (submenu != NULL)
4433 set_cell_data_func_recurse (submenu, cell, info);
4441 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4442 GtkCellRenderer *cell,
4443 GtkCellLayoutDataFunc func,
4445 GDestroyNotify destroy)
4447 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4448 GtkComboBoxPrivate *priv = combo_box->priv;
4449 ComboCellInfo *info;
4451 info = gtk_combo_box_get_cell_info (combo_box, cell);
4452 g_return_if_fail (info != NULL);
4456 GDestroyNotify d = info->destroy;
4458 info->destroy = NULL;
4459 d (info->func_data);
4463 info->func_data = func_data;
4464 info->destroy = destroy;
4466 if (priv->cell_view)
4467 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4470 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4472 if (GTK_IS_MENU (priv->popup_widget))
4473 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4475 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4479 clear_attributes_recurse (GtkWidget *menu,
4480 GtkCellRenderer *cell)
4485 list = gtk_container_get_children (GTK_CONTAINER (menu));
4486 for (i = list; i; i = i->next)
4488 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4489 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4492 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4493 if (submenu != NULL)
4494 clear_attributes_recurse (submenu, cell);
4501 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4502 GtkCellRenderer *cell)
4504 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4505 GtkComboBoxPrivate *priv;
4506 ComboCellInfo *info;
4509 priv = combo_box->priv;
4511 info = gtk_combo_box_get_cell_info (combo_box, cell);
4512 g_return_if_fail (info != NULL);
4514 list = info->attributes;
4515 while (list && list->next)
4517 g_free (list->data);
4518 list = list->next->next;
4520 g_slist_free (info->attributes);
4521 info->attributes = NULL;
4523 if (priv->cell_view)
4524 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4527 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4529 if (GTK_IS_MENU (priv->popup_widget))
4530 clear_attributes_recurse (priv->popup_widget, cell);
4532 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4536 reorder_recurse (GtkWidget *menu,
4537 GtkCellRenderer *cell,
4543 list = gtk_container_get_children (GTK_CONTAINER (menu));
4544 for (i = list; i; i = i->next)
4546 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4547 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4550 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4551 if (submenu != NULL)
4552 reorder_recurse (submenu, cell, position);
4559 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4560 GtkCellRenderer *cell,
4563 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4564 GtkComboBoxPrivate *priv;
4565 ComboCellInfo *info;
4568 priv = combo_box->priv;
4570 info = gtk_combo_box_get_cell_info (combo_box, cell);
4572 g_return_if_fail (info != NULL);
4573 g_return_if_fail (position >= 0);
4575 link = g_slist_find (priv->cells, info);
4577 g_return_if_fail (link != NULL);
4579 priv->cells = g_slist_delete_link (priv->cells, link);
4580 priv->cells = g_slist_insert (priv->cells, info, position);
4582 if (priv->cell_view)
4583 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4587 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4590 if (GTK_IS_MENU (priv->popup_widget))
4591 reorder_recurse (priv->popup_widget, cell, position);
4593 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4601 * gtk_combo_box_new:
4603 * Creates a new empty #GtkComboBox.
4605 * Return value: A new #GtkComboBox.
4610 gtk_combo_box_new (void)
4612 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4616 * gtk_combo_box_new_with_model:
4617 * @model: A #GtkTreeModel.
4619 * Creates a new #GtkComboBox with the model initialized to @model.
4621 * Return value: A new #GtkComboBox.
4626 gtk_combo_box_new_with_model (GtkTreeModel *model)
4628 GtkComboBox *combo_box;
4630 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4632 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4634 return GTK_WIDGET (combo_box);
4638 * gtk_combo_box_get_wrap_width:
4639 * @combo_box: A #GtkComboBox
4641 * Returns the wrap width which is used to determine the number of columns
4642 * for the popup menu. If the wrap width is larger than 1, the combo box
4645 * Returns: the wrap width.
4650 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4652 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4654 return combo_box->priv->wrap_width;
4658 * gtk_combo_box_set_wrap_width:
4659 * @combo_box: A #GtkComboBox
4660 * @width: Preferred number of columns
4662 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4663 * the preferred number of columns when you want the popup to be layed out
4669 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4672 GtkComboBoxPrivate *priv;
4674 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4675 g_return_if_fail (width >= 0);
4677 priv = combo_box->priv;
4679 if (width != priv->wrap_width)
4681 priv->wrap_width = width;
4683 gtk_combo_box_check_appearance (combo_box);
4684 gtk_combo_box_relayout (combo_box);
4686 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4691 * gtk_combo_box_get_row_span_column:
4692 * @combo_box: A #GtkComboBox
4694 * Returns the column with row span information for @combo_box.
4696 * Returns: the row span column.
4701 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4703 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4705 return combo_box->priv->row_column;
4709 * gtk_combo_box_set_row_span_column:
4710 * @combo_box: A #GtkComboBox.
4711 * @row_span: A column in the model passed during construction.
4713 * Sets the column with row span information for @combo_box to be @row_span.
4714 * The row span column contains integers which indicate how many rows
4715 * an item should span.
4720 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4723 GtkComboBoxPrivate *priv;
4726 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4728 priv = combo_box->priv;
4730 col = gtk_tree_model_get_n_columns (priv->model);
4731 g_return_if_fail (row_span >= -1 && row_span < col);
4733 if (row_span != priv->row_column)
4735 priv->row_column = row_span;
4737 gtk_combo_box_relayout (combo_box);
4739 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4744 * gtk_combo_box_get_column_span_column:
4745 * @combo_box: A #GtkComboBox
4747 * Returns the column with column span information for @combo_box.
4749 * Returns: the column span column.
4754 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4756 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4758 return combo_box->priv->col_column;
4762 * gtk_combo_box_set_column_span_column:
4763 * @combo_box: A #GtkComboBox
4764 * @column_span: A column in the model passed during construction
4766 * Sets the column with column span information for @combo_box to be
4767 * @column_span. The column span column contains integers which indicate
4768 * how many columns an item should span.
4773 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4776 GtkComboBoxPrivate *priv;
4779 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4781 priv = combo_box->priv;
4783 col = gtk_tree_model_get_n_columns (priv->model);
4784 g_return_if_fail (column_span >= -1 && column_span < col);
4786 if (column_span != priv->col_column)
4788 priv->col_column = column_span;
4790 gtk_combo_box_relayout (combo_box);
4792 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4797 * gtk_combo_box_get_active:
4798 * @combo_box: A #GtkComboBox
4800 * Returns the index of the currently active item, or -1 if there's no
4801 * active item. If the model is a non-flat treemodel, and the active item
4802 * is not an immediate child of the root of the tree, this function returns
4803 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4804 * <literal>path</literal> is the #GtkTreePath of the active item.
4806 * Return value: An integer which is the index of the currently active item,
4807 * or -1 if there's no active item.
4812 gtk_combo_box_get_active (GtkComboBox *combo_box)
4814 GtkComboBoxPrivate *priv;
4817 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4819 priv = combo_box->priv;
4821 if (gtk_tree_row_reference_valid (priv->active_row))
4825 path = gtk_tree_row_reference_get_path (priv->active_row);
4826 result = gtk_tree_path_get_indices (path)[0];
4827 gtk_tree_path_free (path);
4836 * gtk_combo_box_set_active:
4837 * @combo_box: A #GtkComboBox
4838 * @index_: An index in the model passed during construction, or -1 to have
4841 * Sets the active item of @combo_box to be the item at @index.
4846 gtk_combo_box_set_active (GtkComboBox *combo_box,
4849 GtkTreePath *path = NULL;
4850 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4851 g_return_if_fail (index_ >= -1);
4853 if (combo_box->priv->model == NULL)
4855 /* Save index, in case the model is set after the index */
4856 combo_box->priv->active = index_;
4862 path = gtk_tree_path_new_from_indices (index_, -1);
4864 gtk_combo_box_set_active_internal (combo_box, path);
4867 gtk_tree_path_free (path);
4871 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4874 GtkComboBoxPrivate *priv = combo_box->priv;
4875 GtkTreePath *active_path;
4878 /* Remember whether the initially active row is valid. */
4879 gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4881 if (path && is_valid_row_reference)
4883 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4884 path_cmp = gtk_tree_path_compare (path, active_path);
4885 gtk_tree_path_free (active_path);
4890 if (priv->active_row)
4892 gtk_tree_row_reference_free (priv->active_row);
4893 priv->active_row = NULL;
4898 if (priv->tree_view)
4899 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4902 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4904 if (GTK_IS_MENU (menu))
4905 gtk_menu_set_active (menu, -1);
4908 if (priv->cell_view)
4909 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4912 * Do not emit a "changed" signal when an already invalid selection was
4913 * now set to invalid.
4915 if (!is_valid_row_reference)
4921 gtk_tree_row_reference_new (priv->model, path);
4923 if (priv->tree_view)
4925 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4928 else if (GTK_IS_MENU (priv->popup_widget))
4930 /* FIXME handle nested menus better */
4931 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4932 gtk_tree_path_get_indices (path)[0]);
4935 if (priv->cell_view)
4936 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4940 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4941 g_object_notify (G_OBJECT (combo_box), "active");
4946 * gtk_combo_box_get_active_iter:
4947 * @combo_box: A #GtkComboBox
4948 * @iter: The uninitialized #GtkTreeIter
4950 * Sets @iter to point to the current active item, if it exists.
4952 * Return value: %TRUE, if @iter was set
4957 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4963 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4965 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4968 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4969 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4970 gtk_tree_path_free (path);
4976 * gtk_combo_box_set_active_iter:
4977 * @combo_box: A #GtkComboBox
4978 * @iter: The #GtkTreeIter
4980 * Sets the current active item to be the one referenced by @iter.
4981 * @iter must correspond to a path of depth one.
4986 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4991 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4993 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4994 gtk_combo_box_set_active_internal (combo_box, path);
4995 gtk_tree_path_free (path);
4999 * gtk_combo_box_set_model:
5000 * @combo_box: A #GtkComboBox
5001 * @model: A #GtkTreeModel
5003 * Sets the model used by @combo_box to be @model. Will unset a previously set
5004 * model (if applicable). If model is %NULL, then it will unset the model.
5006 * Note that this function does not clear the cell renderers, you have to
5007 * call gtk_cell_layout_clear() yourself if you need to set up different
5008 * cell renderers for the new model.
5013 gtk_combo_box_set_model (GtkComboBox *combo_box,
5014 GtkTreeModel *model)
5016 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5017 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5019 if (model == combo_box->priv->model)
5022 gtk_combo_box_unset_model (combo_box);
5027 combo_box->priv->model = model;
5028 g_object_ref (combo_box->priv->model);
5030 combo_box->priv->inserted_id =
5031 g_signal_connect (combo_box->priv->model, "row-inserted",
5032 G_CALLBACK (gtk_combo_box_model_row_inserted),
5034 combo_box->priv->deleted_id =
5035 g_signal_connect (combo_box->priv->model, "row-deleted",
5036 G_CALLBACK (gtk_combo_box_model_row_deleted),
5038 combo_box->priv->reordered_id =
5039 g_signal_connect (combo_box->priv->model, "rows-reordered",
5040 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5042 combo_box->priv->changed_id =
5043 g_signal_connect (combo_box->priv->model, "row-changed",
5044 G_CALLBACK (gtk_combo_box_model_row_changed),
5047 if (combo_box->priv->tree_view)
5050 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5051 combo_box->priv->model);
5052 gtk_combo_box_list_popup_resize (combo_box);
5057 if (combo_box->priv->popup_widget)
5058 gtk_combo_box_menu_fill (combo_box);
5062 if (combo_box->priv->cell_view)
5063 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5064 combo_box->priv->model);
5066 if (combo_box->priv->active != -1)
5068 /* If an index was set in advance, apply it now */
5069 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5070 combo_box->priv->active = -1;
5074 gtk_combo_box_update_sensitivity (combo_box);
5076 g_object_notify (G_OBJECT (combo_box), "model");
5080 * gtk_combo_box_get_model
5081 * @combo_box: A #GtkComboBox
5083 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5085 * Return value: A #GtkTreeModel which was passed during construction.
5090 gtk_combo_box_get_model (GtkComboBox *combo_box)
5092 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5094 return combo_box->priv->model;
5098 /* convenience API for simple text combos */
5101 * gtk_combo_box_new_text:
5103 * Convenience function which constructs a new text combo box, which is a
5104 * #GtkComboBox just displaying strings. If you use this function to create
5105 * a text combo box, you should only manipulate its data source with the
5106 * following convenience functions: gtk_combo_box_append_text(),
5107 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5108 * gtk_combo_box_remove_text().
5110 * Return value: A new text combo box.
5115 gtk_combo_box_new_text (void)
5117 GtkWidget *combo_box;
5118 GtkCellRenderer *cell;
5119 GtkListStore *store;
5121 store = gtk_list_store_new (1, G_TYPE_STRING);
5122 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5123 g_object_unref (store);
5125 cell = gtk_cell_renderer_text_new ();
5126 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5127 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5135 * gtk_combo_box_append_text:
5136 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5139 * Appends @string to the list of strings stored in @combo_box. Note that
5140 * you can only use this function with combo boxes constructed with
5141 * gtk_combo_box_new_text().
5146 gtk_combo_box_append_text (GtkComboBox *combo_box,
5150 GtkListStore *store;
5152 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5153 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5154 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5156 g_return_if_fail (text != NULL);
5158 store = GTK_LIST_STORE (combo_box->priv->model);
5160 gtk_list_store_append (store, &iter);
5161 gtk_list_store_set (store, &iter, 0, text, -1);
5165 * gtk_combo_box_insert_text:
5166 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5167 * @position: An index to insert @text
5170 * Inserts @string at @position in the list of strings stored in @combo_box.
5171 * Note that you can only use this function with combo boxes constructed
5172 * with gtk_combo_box_new_text().
5177 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5182 GtkListStore *store;
5184 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5185 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5186 g_return_if_fail (position >= 0);
5187 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5189 g_return_if_fail (text != NULL);
5191 store = GTK_LIST_STORE (combo_box->priv->model);
5193 gtk_list_store_insert (store, &iter, position);
5194 gtk_list_store_set (store, &iter, 0, text, -1);
5198 * gtk_combo_box_prepend_text:
5199 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5202 * Prepends @string to the list of strings stored in @combo_box. Note that
5203 * you can only use this function with combo boxes constructed with
5204 * gtk_combo_box_new_text().
5209 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5213 GtkListStore *store;
5215 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5216 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5217 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5219 g_return_if_fail (text != NULL);
5221 store = GTK_LIST_STORE (combo_box->priv->model);
5223 gtk_list_store_prepend (store, &iter);
5224 gtk_list_store_set (store, &iter, 0, text, -1);
5228 * gtk_combo_box_remove_text:
5229 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5230 * @position: Index of the item to remove
5232 * Removes the string at @position from @combo_box. Note that you can only use
5233 * this function with combo boxes constructed with gtk_combo_box_new_text().
5238 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5242 GtkListStore *store;
5244 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5245 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5246 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5248 g_return_if_fail (position >= 0);
5250 store = GTK_LIST_STORE (combo_box->priv->model);
5252 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5254 gtk_list_store_remove (store, &iter);
5258 * gtk_combo_box_get_active_text:
5259 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5261 * Returns the currently active string in @combo_box or %NULL if none
5262 * is selected. Note that you can only use this function with combo
5263 * boxes constructed with gtk_combo_box_new_text() and with
5264 * #GtkComboBoxEntry<!-- -->s.
5266 * Returns: a newly allocated string containing the currently active text.
5267 * Must be freed with g_free().
5272 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5274 GtkComboBoxClass *class;
5276 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5278 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5280 if (class->get_active_text)
5281 return class->get_active_text (combo_box);
5287 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5292 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5293 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5294 == G_TYPE_STRING, NULL);
5296 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5297 gtk_tree_model_get (combo_box->priv->model, &iter,
5304 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5305 GtkScrollType scroll)
5308 GtkTreeIter new_iter;
5309 gboolean active_iter;
5312 if (!combo_box->priv->model)
5314 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5318 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5322 case GTK_SCROLL_STEP_BACKWARD:
5323 case GTK_SCROLL_STEP_UP:
5324 case GTK_SCROLL_STEP_LEFT:
5327 found = tree_prev (combo_box, combo_box->priv->model,
5328 &iter, &new_iter, FALSE);
5331 /* else fall through */
5333 case GTK_SCROLL_PAGE_FORWARD:
5334 case GTK_SCROLL_PAGE_DOWN:
5335 case GTK_SCROLL_PAGE_RIGHT:
5336 case GTK_SCROLL_END:
5337 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5340 case GTK_SCROLL_STEP_FORWARD:
5341 case GTK_SCROLL_STEP_DOWN:
5342 case GTK_SCROLL_STEP_RIGHT:
5345 found = tree_next (combo_box, combo_box->priv->model,
5346 &iter, &new_iter, FALSE);
5349 /* else fall through */
5351 case GTK_SCROLL_PAGE_BACKWARD:
5352 case GTK_SCROLL_PAGE_UP:
5353 case GTK_SCROLL_PAGE_LEFT:
5354 case GTK_SCROLL_START:
5355 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5362 if (found && active_iter)
5364 GtkTreePath *old_path;
5365 GtkTreePath *new_path;
5367 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5368 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5370 if (gtk_tree_path_compare (old_path, new_path) == 0)
5373 gtk_tree_path_free (old_path);
5374 gtk_tree_path_free (new_path);
5379 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5383 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5388 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5389 gboolean group_cycling)
5391 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5393 gtk_widget_grab_focus (combo_box->priv->button);
5399 gtk_combo_box_grab_focus (GtkWidget *widget)
5401 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5403 gtk_widget_grab_focus (combo_box->priv->button);
5407 gtk_combo_box_destroy (GtkObject *object)
5409 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5411 if (combo_box->priv->popup_idle_id > 0)
5413 g_source_remove (combo_box->priv->popup_idle_id);
5414 combo_box->priv->popup_idle_id = 0;
5417 gtk_combo_box_popdown (combo_box);
5419 if (combo_box->priv->row_separator_destroy)
5420 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5422 combo_box->priv->row_separator_func = NULL;
5423 combo_box->priv->row_separator_data = NULL;
5424 combo_box->priv->row_separator_destroy = NULL;
5426 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5427 combo_box->priv->cell_view = NULL;
5431 gtk_combo_box_dispose(GObject* object)
5433 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5435 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5437 gtk_combo_box_menu_destroy (combo_box);
5438 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5439 combo_box->priv->popup_widget = NULL;
5442 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5446 gtk_combo_box_finalize (GObject *object)
5448 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5451 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5452 gtk_combo_box_list_destroy (combo_box);
5454 if (combo_box->priv->popup_window)
5455 gtk_widget_destroy (combo_box->priv->popup_window);
5457 gtk_combo_box_unset_model (combo_box);
5459 for (i = combo_box->priv->cells; i; i = i->next)
5461 ComboCellInfo *info = (ComboCellInfo *)i->data;
5462 GSList *list = info->attributes;
5465 info->destroy (info->func_data);
5467 while (list && list->next)
5469 g_free (list->data);
5470 list = list->next->next;
5472 g_slist_free (info->attributes);
5474 g_object_unref (info->cell);
5475 g_slice_free (ComboCellInfo, info);
5477 g_slist_free (combo_box->priv->cells);
5479 g_free (combo_box->priv->tearoff_title);
5481 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5485 gtk_cell_editable_key_press (GtkWidget *widget,
5489 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5491 if (event->keyval == GDK_Escape)
5493 combo_box->priv->editing_canceled = TRUE;
5495 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5496 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5500 else if (event->keyval == GDK_Return ||
5501 event->keyval == GDK_ISO_Enter ||
5502 event->keyval == GDK_KP_Enter)
5504 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5505 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5514 popdown_idle (gpointer data)
5516 GtkComboBox *combo_box;
5518 combo_box = GTK_COMBO_BOX (data);
5520 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5521 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5523 g_object_unref (combo_box);
5529 popdown_handler (GtkWidget *widget,
5532 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5536 popup_idle (gpointer data)
5538 GtkComboBox *combo_box;
5540 combo_box = GTK_COMBO_BOX (data);
5542 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5543 combo_box->priv->cell_view)
5544 g_signal_connect_object (combo_box->priv->popup_widget,
5545 "unmap", G_CALLBACK (popdown_handler),
5548 /* we unset this if a menu item is activated */
5549 combo_box->priv->editing_canceled = TRUE;
5550 gtk_combo_box_popup (combo_box);
5552 combo_box->priv->popup_idle_id = 0;
5553 combo_box->priv->activate_button = 0;
5554 combo_box->priv->activate_time = 0;
5560 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5563 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5565 combo_box->priv->is_cell_renderer = TRUE;
5567 if (combo_box->priv->cell_view)
5569 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5570 G_CALLBACK (gtk_cell_editable_key_press),
5573 gtk_widget_grab_focus (combo_box->priv->button);
5577 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5578 G_CALLBACK (gtk_cell_editable_key_press),
5581 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5582 GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5585 /* we do the immediate popup only for the optionmenu-like
5588 if (combo_box->priv->is_cell_renderer &&
5589 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5591 if (event && event->type == GDK_BUTTON_PRESS)
5593 GdkEventButton *event_button = (GdkEventButton *)event;
5595 combo_box->priv->activate_button = event_button->button;
5596 combo_box->priv->activate_time = event_button->time;
5599 combo_box->priv->popup_idle_id =
5600 gdk_threads_add_idle (popup_idle, combo_box);
5606 * gtk_combo_box_get_add_tearoffs:
5607 * @combo_box: a #GtkComboBox
5609 * Gets the current value of the :add-tearoffs property.
5611 * Return value: the current value of the :add-tearoffs property.
5614 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5616 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5618 return combo_box->priv->add_tearoffs;
5622 * gtk_combo_box_set_add_tearoffs:
5623 * @combo_box: a #GtkComboBox
5624 * @add_tearoffs: %TRUE to add tearoff menu items
5626 * Sets whether the popup menu should have a tearoff
5632 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5633 gboolean add_tearoffs)
5635 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5637 add_tearoffs = add_tearoffs != FALSE;
5639 if (combo_box->priv->add_tearoffs != add_tearoffs)
5641 combo_box->priv->add_tearoffs = add_tearoffs;
5642 gtk_combo_box_check_appearance (combo_box);
5643 gtk_combo_box_relayout (combo_box);
5644 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5649 * gtk_combo_box_get_title:
5650 * @combo_box: a #GtkComboBox
5652 * Gets the current title of the menu in tearoff mode. See
5653 * gtk_combo_box_set_add_tearoffs().
5655 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5656 * string which must not be freed.
5660 G_CONST_RETURN gchar*
5661 gtk_combo_box_get_title (GtkComboBox *combo_box)
5663 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5665 return combo_box->priv->tearoff_title;
5669 gtk_combo_box_update_title (GtkComboBox *combo_box)
5671 gtk_combo_box_check_appearance (combo_box);
5673 if (combo_box->priv->popup_widget &&
5674 GTK_IS_MENU (combo_box->priv->popup_widget))
5675 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5676 combo_box->priv->tearoff_title);
5680 * gtk_combo_box_set_title:
5681 * @combo_box: a #GtkComboBox
5682 * @title: a title for the menu in tearoff mode
5684 * Sets the menu's title in tearoff mode.
5689 gtk_combo_box_set_title (GtkComboBox *combo_box,
5692 GtkComboBoxPrivate *priv;
5694 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5696 priv = combo_box->priv;
5698 if (strcmp (title ? title : "",
5699 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5701 g_free (priv->tearoff_title);
5702 priv->tearoff_title = g_strdup (title);
5704 gtk_combo_box_update_title (combo_box);
5706 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5711 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5713 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5715 return combo_box->priv->editing_canceled;
5719 * gtk_combo_box_get_popup_accessible:
5720 * @combo_box: a #GtkComboBox
5722 * Gets the accessible object corresponding to the combo box's popup.
5724 * This function is mostly intended for use by accessibility technologies;
5725 * applications should have little use for it.
5727 * Returns: the accessible object corresponding to the combo box's popup.
5732 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5736 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5738 if (combo_box->priv->popup_widget)
5740 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5748 * gtk_combo_box_get_row_separator_func:
5749 * @combo_box: a #GtkComboBox
5751 * Returns the current row separator function.
5753 * Return value: the current row separator function.
5757 GtkTreeViewRowSeparatorFunc
5758 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5760 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5762 return combo_box->priv->row_separator_func;
5766 * gtk_combo_box_set_row_separator_func:
5767 * @combo_box: a #GtkComboBox
5768 * @func: a #GtkTreeViewRowSeparatorFunc
5769 * @data: user data to pass to @func, or %NULL
5770 * @destroy: destroy notifier for @data, or %NULL
5772 * Sets the row separator function, which is used to determine
5773 * whether a row should be drawn as a separator. If the row separator
5774 * function is %NULL, no separators are drawn. This is the default value.
5779 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5780 GtkTreeViewRowSeparatorFunc func,
5782 GDestroyNotify destroy)
5784 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5786 if (combo_box->priv->row_separator_destroy)
5787 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5789 combo_box->priv->row_separator_func = func;
5790 combo_box->priv->row_separator_data = data;
5791 combo_box->priv->row_separator_destroy = destroy;
5793 if (combo_box->priv->tree_view)
5794 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5797 gtk_combo_box_relayout (combo_box);
5799 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5803 * gtk_combo_box_set_button_sensitivity:
5804 * @combo_box: a #GtkComboBox
5805 * @sensitivity: specify the sensitivity of the dropdown button
5807 * Sets whether the dropdown button of the combo box should be
5808 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5809 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5814 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5815 GtkSensitivityType sensitivity)
5817 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5819 if (combo_box->priv->button_sensitivity != sensitivity)
5821 combo_box->priv->button_sensitivity = sensitivity;
5822 gtk_combo_box_update_sensitivity (combo_box);
5824 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5829 * gtk_combo_box_get_button_sensitivity:
5830 * @combo_box: a #GtkComboBox
5832 * Returns whether the combo box sets the dropdown button
5833 * sensitive or not when there are no items in the model.
5835 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5836 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5837 * if the button is always insensitive or
5838 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5839 * the model has one item to be selected.
5844 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5846 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5848 return combo_box->priv->button_sensitivity;
5853 * gtk_combo_box_set_focus_on_click:
5854 * @combo: a #GtkComboBox
5855 * @focus_on_click: whether the combo box grabs focus when clicked
5858 * Sets whether the combo box will grab focus when it is clicked with
5859 * the mouse. Making mouse clicks not grab focus is useful in places
5860 * like toolbars where you don't want the keyboard focus removed from
5861 * the main area of the application.
5866 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5867 gboolean focus_on_click)
5869 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5871 focus_on_click = focus_on_click != FALSE;
5873 if (combo_box->priv->focus_on_click != focus_on_click)
5875 combo_box->priv->focus_on_click = focus_on_click;
5877 if (combo_box->priv->button)
5878 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5881 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5886 * gtk_combo_box_get_focus_on_click:
5887 * @combo: a #GtkComboBox
5889 * Returns whether the combo box grabs focus when it is clicked
5890 * with the mouse. See gtk_combo_box_set_focus_on_click().
5892 * Return value: %TRUE if the combo box grabs focus when it is
5893 * clicked with the mouse.
5898 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5900 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5902 return combo_box->priv->focus_on_click;
5907 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5908 GtkBuilder *builder,
5910 const gchar *tagname,
5911 GMarkupParser *parser,
5914 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5915 tagname, parser, data))
5918 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5919 tagname, parser, data);
5923 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5924 GtkBuilder *builder,
5926 const gchar *tagname,
5929 if (strcmp (tagname, "attributes") == 0)
5930 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5933 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5937 #define __GTK_COMBO_BOX_C__
5938 #include "gtkaliasdef.c"