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;
1474 gdk_window_get_origin (child->window, &sx, &sy);
1476 if (GTK_WIDGET_NO_WINDOW (child))
1478 sx += child->allocation.x;
1479 sy += child->allocation.y;
1482 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1483 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1485 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1487 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1490 *x = sx + child->allocation.width - req.width;
1493 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1494 monitor_num = gdk_screen_get_monitor_at_window (screen,
1495 GTK_WIDGET (combo_box)->window);
1496 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1500 else if (*x + req.width > monitor.x + monitor.width)
1501 *x = monitor.x + monitor.width - req.width;
1503 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1504 *y += child->allocation.height;
1505 else if (*y - monitor.y >= req.height)
1507 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1508 *y += child->allocation.height;
1516 gtk_combo_box_menu_position_over (GtkMenu *menu,
1522 GtkComboBox *combo_box;
1526 GtkRequisition requisition;
1533 combo_box = GTK_COMBO_BOX (user_data);
1534 widget = GTK_WIDGET (combo_box);
1536 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1537 menu_width = requisition.width;
1539 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1540 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1542 menu_xpos += widget->allocation.x;
1543 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1547 gtk_widget_get_child_requisition (active, &requisition);
1548 menu_ypos -= requisition.height / 2;
1551 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1554 child = children->data;
1556 if (active == child)
1559 if (GTK_WIDGET_VISIBLE (child))
1561 gtk_widget_get_child_requisition (child, &requisition);
1562 menu_ypos -= requisition.height;
1565 children = children->next;
1568 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1569 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1571 /* Clamp the position on screen */
1572 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1576 else if ((menu_xpos + menu_width) > screen_width)
1577 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1586 gtk_combo_box_menu_position (GtkMenu *menu,
1592 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1593 GtkComboBoxPrivate *priv = combo_box->priv;
1594 GtkWidget *menu_item;
1597 if (priv->wrap_width > 0 || priv->cell_view == NULL)
1598 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1601 /* FIXME handle nested menus better */
1602 menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
1604 gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
1607 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1610 if (!GTK_WIDGET_VISIBLE (GTK_MENU (priv->popup_widget)->toplevel))
1611 gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel),
1612 GDK_WINDOW_TYPE_HINT_COMBO);
1616 gtk_combo_box_list_position (GtkComboBox *combo_box,
1622 GtkComboBoxPrivate *priv = combo_box->priv;
1625 GdkRectangle monitor;
1626 GtkRequisition popup_req;
1627 GtkPolicyType hpolicy, vpolicy;
1629 /* under windows, the drop down list is as wide as the combo box itself.
1631 GtkWidget *sample = GTK_WIDGET (combo_box);
1633 gdk_window_get_origin (sample->window, x, y);
1635 if (GTK_WIDGET_NO_WINDOW (sample))
1637 *x += sample->allocation.x;
1638 *y += sample->allocation.y;
1641 *width = sample->allocation.width;
1643 hpolicy = vpolicy = GTK_POLICY_NEVER;
1644 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1646 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1648 if (popup_req.width > *width)
1650 hpolicy = GTK_POLICY_ALWAYS;
1651 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1653 gtk_widget_size_request (priv->scrolled_window, &popup_req);
1656 *height = popup_req.height;
1658 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1659 monitor_num = gdk_screen_get_monitor_at_window (screen,
1660 GTK_WIDGET (combo_box)->window);
1661 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1665 else if (*x + *width > monitor.x + monitor.width)
1666 *x = monitor.x + monitor.width - *width;
1668 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1669 *y += sample->allocation.height;
1670 else if (*y - *height >= monitor.y)
1672 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1674 *y += sample->allocation.height;
1675 *height = monitor.y + monitor.height - *y;
1679 *height = *y - monitor.y;
1683 if (popup_req.height > *height)
1685 vpolicy = GTK_POLICY_ALWAYS;
1687 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1693 cell_view_is_sensitive (GtkCellView *cell_view)
1695 GList *cells, *list;
1698 cells = gtk_cell_view_get_cell_renderers (cell_view);
1701 for (list = cells; list; list = list->next)
1703 g_object_get (list->data, "sensitive", &sensitive, NULL);
1708 g_list_free (cells);
1714 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1717 GtkComboBoxPrivate *priv = combo_box->priv;
1718 GList *cells, *list;
1724 if (priv->row_separator_func)
1726 if (priv->row_separator_func (priv->model, iter,
1727 priv->row_separator_data))
1731 gtk_tree_view_column_cell_set_cell_data (priv->column,
1733 iter, FALSE, FALSE);
1735 cells = gtk_tree_view_column_get_cell_renderers (priv->column);
1738 for (list = cells; list; list = list->next)
1740 g_object_get (list->data, "sensitive", &sensitive, NULL);
1745 g_list_free (cells);
1751 update_menu_sensitivity (GtkComboBox *combo_box,
1754 GtkComboBoxPrivate *priv = combo_box->priv;
1755 GList *children, *child;
1756 GtkWidget *item, *submenu, *separator;
1757 GtkWidget *cell_view;
1763 children = gtk_container_get_children (GTK_CONTAINER (menu));
1765 for (child = children; child; child = child->next)
1767 item = GTK_WIDGET (child->data);
1768 cell_view = GTK_BIN (item)->child;
1770 if (!GTK_IS_CELL_VIEW (cell_view))
1773 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1774 if (submenu != NULL)
1776 gtk_widget_set_sensitive (item, TRUE);
1777 update_menu_sensitivity (combo_box, submenu);
1781 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1783 if (menu != priv->popup_widget && child == children)
1785 separator = GTK_WIDGET (child->next->data);
1786 g_object_set (item, "visible", sensitive, NULL);
1787 g_object_set (separator, "visible", sensitive, NULL);
1790 gtk_widget_set_sensitive (item, sensitive);
1794 g_list_free (children);
1798 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1800 guint32 activate_time)
1802 GtkComboBoxPrivate *priv = combo_box->priv;
1805 GtkRequisition requisition;
1808 update_menu_sensitivity (combo_box, priv->popup_widget);
1811 if (gtk_tree_row_reference_valid (priv->active_row))
1813 path = gtk_tree_row_reference_get_path (priv->active_row);
1814 active_item = gtk_tree_path_get_indices (path)[0];
1815 gtk_tree_path_free (path);
1817 if (priv->add_tearoffs)
1821 /* FIXME handle nested menus better */
1822 gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
1824 if (priv->wrap_width == 0)
1826 width = GTK_WIDGET (combo_box)->allocation.width;
1827 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
1828 gtk_widget_size_request (priv->popup_widget, &requisition);
1830 gtk_widget_set_size_request (priv->popup_widget,
1831 MAX (width, requisition.width), -1);
1834 gtk_menu_popup (GTK_MENU (priv->popup_widget),
1836 gtk_combo_box_menu_position, combo_box,
1837 button, activate_time);
1841 popup_grab_on_window (GdkWindow *window,
1842 guint32 activate_time,
1843 gboolean grab_keyboard)
1845 if ((gdk_pointer_grab (window, TRUE,
1846 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1847 GDK_POINTER_MOTION_MASK,
1848 NULL, NULL, activate_time) == 0))
1850 if (!grab_keyboard ||
1851 gdk_keyboard_grab (window, TRUE,
1852 activate_time) == 0)
1856 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1866 * gtk_combo_box_popup:
1867 * @combo_box: a #GtkComboBox
1869 * Pops up the menu or dropdown list of @combo_box.
1871 * This function is mostly intended for use by accessibility technologies;
1872 * applications should have little use for it.
1877 gtk_combo_box_popup (GtkComboBox *combo_box)
1879 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1881 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1885 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1887 GtkComboBoxPrivate *priv = combo_box->priv;
1888 gint x, y, width, height;
1889 GtkTreePath *path = NULL, *ppath;
1890 GtkWidget *toplevel;
1892 if (!GTK_WIDGET_REALIZED (combo_box))
1895 if (GTK_WIDGET_MAPPED (priv->popup_widget))
1898 if (GTK_IS_MENU (priv->popup_widget))
1900 gtk_combo_box_menu_popup (combo_box,
1901 priv->activate_button,
1902 priv->activate_time);
1906 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1907 if (GTK_IS_WINDOW (toplevel))
1908 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1909 GTK_WINDOW (priv->popup_window));
1911 gtk_widget_show_all (priv->scrolled_window);
1912 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1914 gtk_widget_set_size_request (priv->popup_window, width, height);
1915 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
1917 if (gtk_tree_row_reference_valid (priv->active_row))
1919 path = gtk_tree_row_reference_get_path (priv->active_row);
1920 ppath = gtk_tree_path_copy (path);
1921 if (gtk_tree_path_up (ppath))
1922 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
1924 gtk_tree_path_free (ppath);
1926 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
1930 gtk_widget_show (priv->popup_window);
1934 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
1936 gtk_tree_path_free (path);
1939 gtk_widget_grab_focus (priv->popup_window);
1940 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1943 if (!GTK_WIDGET_HAS_FOCUS (priv->tree_view))
1944 gtk_widget_grab_focus (priv->tree_view);
1946 if (!popup_grab_on_window (priv->popup_window->window,
1947 GDK_CURRENT_TIME, TRUE))
1949 gtk_widget_hide (priv->popup_window);
1953 gtk_grab_add (priv->popup_window);
1957 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1959 if (combo_box->priv->popup_shown)
1961 gtk_combo_box_popdown (combo_box);
1969 * gtk_combo_box_popdown:
1970 * @combo_box: a #GtkComboBox
1972 * Hides the menu or dropdown list of @combo_box.
1974 * This function is mostly intended for use by accessibility technologies;
1975 * applications should have little use for it.
1980 gtk_combo_box_popdown (GtkComboBox *combo_box)
1982 GtkComboBoxPrivate *priv = combo_box->priv;
1984 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1986 if (GTK_IS_MENU (priv->popup_widget))
1988 gtk_menu_popdown (GTK_MENU (priv->popup_widget));
1992 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1995 gtk_grab_remove (priv->popup_window);
1996 gtk_widget_hide_all (priv->popup_window);
1997 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2002 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
2005 GtkComboBoxPrivate *priv = combo_box->priv;
2009 if (priv->cell_view)
2010 gtk_widget_style_get (priv->cell_view,
2011 "focus-line-width", &padding,
2016 /* add some pixels for good measure */
2017 padding += BONUS_PADDING;
2019 if (priv->cell_view)
2020 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2025 return req.width + padding;
2029 gtk_combo_box_remeasure (GtkComboBox *combo_box)
2031 GtkComboBoxPrivate *priv = combo_box->priv;
2036 !gtk_tree_model_get_iter_first (priv->model, &iter))
2042 path = gtk_tree_path_new_from_indices (0, -1);
2048 if (priv->cell_view)
2049 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (priv->cell_view),
2057 priv->width = MAX (priv->width, req.width);
2058 priv->height = MAX (priv->height, req.height);
2060 gtk_tree_path_next (path);
2062 while (gtk_tree_model_iter_next (priv->model, &iter));
2064 gtk_tree_path_free (path);
2068 gtk_combo_box_size_request (GtkWidget *widget,
2069 GtkRequisition *requisition)
2072 gint focus_width, focus_pad;
2075 GtkRequisition bin_req;
2076 PangoContext *context;
2077 PangoFontMetrics *metrics;
2078 PangoFontDescription *font_desc;
2080 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2081 GtkComboBoxPrivate *priv = combo_box->priv;
2084 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2085 gtk_combo_box_remeasure (combo_box);
2086 bin_req.width = MAX (bin_req.width, priv->width);
2087 bin_req.height = MAX (bin_req.height, priv->height);
2089 gtk_widget_style_get (GTK_WIDGET (widget),
2090 "focus-line-width", &focus_width,
2091 "focus-padding", &focus_pad,
2092 "arrow-size", &arrow_size,
2095 font_desc = GTK_BIN (widget)->child->style->font_desc;
2096 context = gtk_widget_get_pango_context (widget);
2097 metrics = pango_context_get_metrics (context, font_desc,
2098 pango_context_get_language (context));
2099 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2100 pango_font_metrics_get_descent (metrics));
2101 pango_font_metrics_unref (metrics);
2103 arrow_size = MAX (arrow_size, font_size);
2105 gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
2107 if (!priv->tree_view)
2111 if (priv->cell_view)
2113 GtkRequisition button_req, sep_req, arrow_req;
2114 gint border_width, xthickness, ythickness;
2116 gtk_widget_size_request (priv->button, &button_req);
2117 border_width = GTK_CONTAINER (combo_box)->border_width;
2118 xthickness = priv->button->style->xthickness;
2119 ythickness = priv->button->style->ythickness;
2121 bin_req.width = MAX (bin_req.width, priv->width);
2122 bin_req.height = MAX (bin_req.height, priv->height);
2124 gtk_widget_size_request (priv->separator, &sep_req);
2125 gtk_widget_size_request (priv->arrow, &arrow_req);
2127 height = MAX (sep_req.height, arrow_req.height);
2128 height = MAX (height, bin_req.height);
2130 width = bin_req.width + sep_req.width + arrow_req.width;
2132 height += 2*(border_width + ythickness + focus_width + focus_pad);
2133 width += 2*(border_width + xthickness + focus_width + focus_pad);
2135 requisition->width = width;
2136 requisition->height = height;
2140 GtkRequisition but_req;
2142 gtk_widget_size_request (priv->button, &but_req);
2144 requisition->width = bin_req.width + but_req.width;
2145 requisition->height = MAX (bin_req.height, but_req.height);
2151 GtkRequisition button_req, frame_req;
2153 /* sample + frame */
2154 *requisition = bin_req;
2156 requisition->width += 2 * focus_width;
2158 if (priv->cell_view_frame)
2160 gtk_widget_size_request (priv->cell_view_frame, &frame_req);
2161 if (priv->has_frame)
2163 requisition->width += 2 *
2164 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2165 GTK_WIDGET (priv->cell_view_frame)->style->xthickness);
2166 requisition->height += 2 *
2167 (GTK_CONTAINER (priv->cell_view_frame)->border_width +
2168 GTK_WIDGET (priv->cell_view_frame)->style->ythickness);
2173 gtk_widget_size_request (priv->button, &button_req);
2175 requisition->height = MAX (requisition->height, button_req.height);
2176 requisition->width += button_req.width;
2179 if (GTK_SHADOW_NONE != priv->shadow_type)
2181 requisition->height += 2 * widget->style->ythickness;
2182 requisition->width += 2 * widget->style->xthickness;
2186 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2187 gtk_widget_size_request (combo_box->priv->button, &req); \
2190 child.x = allocation->x + shadow_width; \
2192 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2194 child.y = allocation->y + shadow_height; \
2195 child.width = req.width; \
2196 child.height = allocation->height - 2 * shadow_height; \
2197 child.width = MAX (1, child.width); \
2198 child.height = MAX (1, child.height); \
2200 gtk_widget_size_allocate (combo_box->priv->button, &child);
2203 gtk_combo_box_size_allocate (GtkWidget *widget,
2204 GtkAllocation *allocation)
2206 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2207 GtkComboBoxPrivate *priv = combo_box->priv;
2208 gint shadow_width, shadow_height;
2209 gint focus_width, focus_pad;
2210 GtkAllocation child;
2212 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2214 widget->allocation = *allocation;
2216 gtk_widget_style_get (GTK_WIDGET (widget),
2217 "focus-line-width", &focus_width,
2218 "focus-padding", &focus_pad,
2221 if (GTK_SHADOW_NONE != priv->shadow_type)
2223 shadow_width = widget->style->xthickness;
2224 shadow_height = widget->style->ythickness;
2232 if (!priv->tree_view)
2234 if (priv->cell_view)
2236 gint border_width, xthickness, ythickness;
2240 allocation->x += shadow_width;
2241 allocation->y += shadow_height;
2242 allocation->width -= 2 * shadow_width;
2243 allocation->height -= 2 * shadow_height;
2245 gtk_widget_size_allocate (priv->button, allocation);
2247 /* set some things ready */
2248 border_width = GTK_CONTAINER (priv->button)->border_width;
2249 xthickness = priv->button->style->xthickness;
2250 ythickness = priv->button->style->ythickness;
2252 child.x = allocation->x;
2253 child.y = allocation->y;
2254 width = allocation->width;
2255 child.height = allocation->height;
2257 if (!priv->is_cell_renderer)
2259 child.x += border_width + xthickness + focus_width + focus_pad;
2260 child.y += border_width + ythickness + focus_width + focus_pad;
2261 width -= 2 * (child.x - allocation->x);
2262 child.height -= 2 * (child.y - allocation->y);
2266 /* handle the children */
2267 gtk_widget_size_request (priv->arrow, &req);
2268 child.width = req.width;
2270 child.x += width - req.width;
2271 child.width = MAX (1, child.width);
2272 child.height = MAX (1, child.height);
2273 gtk_widget_size_allocate (priv->arrow, &child);
2275 child.x += req.width;
2276 gtk_widget_size_request (priv->separator, &req);
2277 child.width = req.width;
2279 child.x -= req.width;
2280 child.width = MAX (1, child.width);
2281 child.height = MAX (1, child.height);
2282 gtk_widget_size_allocate (priv->separator, &child);
2286 child.x += req.width;
2287 child.width = allocation->x + allocation->width
2288 - (border_width + xthickness + focus_width + focus_pad)
2293 child.width = child.x;
2294 child.x = allocation->x
2295 + border_width + xthickness + focus_width + focus_pad;
2296 child.width -= child.x;
2299 if (GTK_WIDGET_VISIBLE (priv->popup_widget))
2302 GtkRequisition requisition;
2304 /* Warning here, without the check in the position func */
2305 gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2306 if (priv->wrap_width == 0)
2308 width = GTK_WIDGET (combo_box)->allocation.width;
2309 gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2310 gtk_widget_size_request (priv->popup_widget, &requisition);
2311 gtk_widget_set_size_request (priv->popup_widget,
2312 MAX (width, requisition.width), -1);
2316 child.width = MAX (1, child.width);
2317 child.height = MAX (1, child.height);
2318 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2322 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2325 child.x = allocation->x + req.width + shadow_width;
2327 child.x = allocation->x + shadow_width;
2328 child.y = allocation->y + shadow_height;
2329 child.width = allocation->width - req.width - 2 * shadow_width;
2330 child.width = MAX (1, child.width);
2331 child.height = MAX (1, child.height);
2332 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2339 /* Combobox thickness + border-width */
2340 int delta_x = shadow_width + GTK_CONTAINER (widget)->border_width;
2341 int delta_y = shadow_height + GTK_CONTAINER (widget)->border_width;
2344 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2348 child.x = allocation->x + req.width;
2350 child.x = allocation->x;
2352 child.y = allocation->y;
2353 child.width = allocation->width - req.width;
2354 child.height = allocation->height;
2356 if (priv->cell_view_frame)
2360 child.width = MAX (1, child.width - delta_x * 2);
2361 child.height = MAX (1, child.height - delta_y * 2);
2362 gtk_widget_size_allocate (priv->cell_view_frame, &child);
2365 if (priv->has_frame)
2367 delta_x = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2368 GTK_WIDGET (priv->cell_view_frame)->style->xthickness;
2369 delta_y = GTK_CONTAINER (priv->cell_view_frame)->border_width +
2370 GTK_WIDGET (priv->cell_view_frame)->style->ythickness;
2374 child.width -= delta_x * 2;
2375 child.height -= delta_y * 2;
2382 child.width -= delta_x * 2;
2383 child.height -= delta_y * 2;
2386 if (GTK_WIDGET_VISIBLE (priv->popup_window))
2388 gint x, y, width, height;
2389 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2390 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2391 gtk_widget_set_size_request (priv->popup_window, width, height);
2395 child.width = MAX (1, child.width);
2396 child.height = MAX (1, child.height);
2398 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2402 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2405 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2407 GtkComboBoxPrivate *priv = combo_box->priv;
2411 g_signal_handler_disconnect (priv->model,
2413 g_signal_handler_disconnect (priv->model,
2415 g_signal_handler_disconnect (priv->model,
2416 priv->reordered_id);
2417 g_signal_handler_disconnect (priv->model,
2422 if (!priv->tree_view)
2424 if (priv->popup_widget)
2425 gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2426 (GtkCallback)gtk_widget_destroy, NULL);
2431 g_object_unref (priv->model);
2435 if (priv->active_row)
2437 gtk_tree_row_reference_free (priv->active_row);
2438 priv->active_row = NULL;
2441 if (priv->cell_view)
2442 gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2446 gtk_combo_box_forall (GtkContainer *container,
2447 gboolean include_internals,
2448 GtkCallback callback,
2449 gpointer callback_data)
2451 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2452 GtkComboBoxPrivate *priv = combo_box->priv;
2454 if (include_internals)
2457 (* callback) (priv->button, callback_data);
2458 if (priv->cell_view_frame)
2459 (* callback) (priv->cell_view_frame, callback_data);
2462 if (GTK_BIN (container)->child)
2463 (* callback) (GTK_BIN (container)->child, callback_data);
2467 gtk_combo_box_child_show (GtkWidget *widget,
2468 GtkComboBox *combo_box)
2470 GtkComboBoxPrivate *priv = combo_box->priv;
2472 priv->popup_shown = TRUE;
2473 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2477 gtk_combo_box_child_hide (GtkWidget *widget,
2478 GtkComboBox *combo_box)
2480 GtkComboBoxPrivate *priv = combo_box->priv;
2482 priv->popup_shown = FALSE;
2483 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2487 gtk_combo_box_expose_event (GtkWidget *widget,
2488 GdkEventExpose *event)
2490 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2491 GtkComboBoxPrivate *priv = combo_box->priv;
2493 if (GTK_WIDGET_DRAWABLE (widget) &&
2494 GTK_SHADOW_NONE != priv->shadow_type)
2496 gtk_paint_shadow (widget->style, widget->window,
2497 GTK_STATE_NORMAL, priv->shadow_type,
2498 NULL, widget, "combobox",
2499 widget->allocation.x, widget->allocation.y,
2500 widget->allocation.width, widget->allocation.height);
2503 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2504 priv->button, event);
2506 if (priv->tree_view && priv->cell_view_frame)
2508 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2509 priv->cell_view_frame, event);
2512 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2513 GTK_BIN (widget)->child, event);
2528 path_visible (GtkTreeView *view,
2534 /* Note that we rely on the fact that collapsed rows don't have nodes
2536 return _gtk_tree_view_find_node (view, path, &tree, &node);
2540 tree_next_func (GtkTreeModel *model,
2545 SearchData *search_data = (SearchData *)data;
2547 if (search_data->found)
2549 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2552 if (search_data->visible &&
2553 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2556 search_data->set = TRUE;
2557 search_data->iter = *iter;
2562 if (gtk_tree_path_compare (path, search_data->path) == 0)
2563 search_data->found = TRUE;
2569 tree_next (GtkComboBox *combo,
2570 GtkTreeModel *model,
2575 SearchData search_data;
2577 search_data.combo = combo;
2578 search_data.path = gtk_tree_model_get_path (model, iter);
2579 search_data.visible = visible;
2580 search_data.found = FALSE;
2581 search_data.set = FALSE;
2583 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2585 *next = search_data.iter;
2587 gtk_tree_path_free (search_data.path);
2589 return search_data.set;
2593 tree_prev_func (GtkTreeModel *model,
2598 SearchData *search_data = (SearchData *)data;
2600 if (gtk_tree_path_compare (path, search_data->path) == 0)
2602 search_data->found = TRUE;
2606 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2609 if (search_data->visible &&
2610 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2613 search_data->set = TRUE;
2614 search_data->iter = *iter;
2620 tree_prev (GtkComboBox *combo,
2621 GtkTreeModel *model,
2626 SearchData search_data;
2628 search_data.combo = combo;
2629 search_data.path = gtk_tree_model_get_path (model, iter);
2630 search_data.visible = visible;
2631 search_data.found = FALSE;
2632 search_data.set = FALSE;
2634 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2636 *prev = search_data.iter;
2638 gtk_tree_path_free (search_data.path);
2640 return search_data.set;
2644 tree_last_func (GtkTreeModel *model,
2649 SearchData *search_data = (SearchData *)data;
2651 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2654 /* Note that we rely on the fact that collapsed rows don't have nodes
2656 if (search_data->visible &&
2657 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2660 search_data->set = TRUE;
2661 search_data->iter = *iter;
2667 tree_last (GtkComboBox *combo,
2668 GtkTreeModel *model,
2672 SearchData search_data;
2674 search_data.combo = combo;
2675 search_data.visible = visible;
2676 search_data.set = FALSE;
2678 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2680 *last = search_data.iter;
2682 return search_data.set;
2687 tree_first_func (GtkTreeModel *model,
2692 SearchData *search_data = (SearchData *)data;
2694 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2697 if (search_data->visible &&
2698 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2701 search_data->set = TRUE;
2702 search_data->iter = *iter;
2708 tree_first (GtkComboBox *combo,
2709 GtkTreeModel *model,
2713 SearchData search_data;
2715 search_data.combo = combo;
2716 search_data.visible = visible;
2717 search_data.set = FALSE;
2719 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2721 *first = search_data.iter;
2723 return search_data.set;
2727 gtk_combo_box_scroll_event (GtkWidget *widget,
2728 GdkEventScroll *event)
2730 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2733 GtkTreeIter new_iter;
2735 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2738 if (event->direction == GDK_SCROLL_UP)
2739 found = tree_prev (combo_box, combo_box->priv->model,
2740 &iter, &new_iter, FALSE);
2742 found = tree_next (combo_box, combo_box->priv->model,
2743 &iter, &new_iter, FALSE);
2746 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2756 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2757 GtkCellLayout *cell_layout)
2759 GtkComboBoxPrivate *priv = combo_box->priv;
2762 for (k = priv->cells; k; k = k->next)
2765 ComboCellInfo *info = (ComboCellInfo *)k->data;
2767 if (info->pack == GTK_PACK_START)
2768 gtk_cell_layout_pack_start (cell_layout,
2769 info->cell, info->expand);
2770 else if (info->pack == GTK_PACK_END)
2771 gtk_cell_layout_pack_end (cell_layout,
2772 info->cell, info->expand);
2774 gtk_cell_layout_set_cell_data_func (cell_layout,
2776 combo_cell_data_func, info, NULL);
2778 for (j = info->attributes; j; j = j->next->next)
2780 gtk_cell_layout_add_attribute (cell_layout,
2783 GPOINTER_TO_INT (j->next->data));
2789 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2790 gboolean add_children)
2792 GtkComboBoxPrivate *priv = combo_box->priv;
2795 if (priv->cell_view)
2797 priv->button = gtk_toggle_button_new ();
2798 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2799 priv->focus_on_click);
2801 g_signal_connect (priv->button, "toggled",
2802 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2803 gtk_widget_set_parent (priv->button,
2804 GTK_BIN (combo_box)->child->parent);
2806 priv->box = gtk_hbox_new (FALSE, 0);
2807 gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2809 priv->separator = gtk_vseparator_new ();
2810 gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2812 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2813 gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2815 gtk_widget_show_all (priv->button);
2819 priv->button = gtk_toggle_button_new ();
2820 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2821 priv->focus_on_click);
2823 g_signal_connect (priv->button, "toggled",
2824 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2825 gtk_widget_set_parent (priv->button,
2826 GTK_BIN (combo_box)->child->parent);
2828 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2829 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2830 gtk_widget_show_all (priv->button);
2833 g_signal_connect (priv->button, "button-press-event",
2834 G_CALLBACK (gtk_combo_box_menu_button_press),
2836 g_signal_connect (priv->button, "state-changed",
2837 G_CALLBACK (gtk_combo_box_button_state_changed),
2840 /* create our funky menu */
2841 menu = gtk_menu_new ();
2842 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2844 g_signal_connect (menu, "key-press-event",
2845 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2846 gtk_combo_box_set_popup_widget (combo_box, menu);
2850 gtk_combo_box_menu_fill (combo_box);
2852 /* the column is needed in tree_column_row_is_sensitive() */
2853 priv->column = gtk_tree_view_column_new ();
2854 g_object_ref_sink (priv->column);
2855 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2857 gtk_combo_box_update_title (combo_box);
2861 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2863 GtkComboBoxPrivate *priv = combo_box->priv;
2869 menu = priv->popup_widget;
2871 if (priv->add_tearoffs)
2873 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2875 gtk_widget_show (tearoff);
2877 if (priv->wrap_width)
2878 gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2880 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2883 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2887 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2888 GtkTreeModel *model,
2891 GtkWidget *cell_view;
2896 cell_view = gtk_cell_view_new ();
2897 item = gtk_menu_item_new ();
2898 gtk_container_add (GTK_CONTAINER (item), cell_view);
2900 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2901 path = gtk_tree_model_get_path (model, iter);
2902 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2903 gtk_tree_path_free (path);
2905 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2906 gtk_widget_size_request (cell_view, &req);
2907 gtk_widget_show (cell_view);
2913 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2915 GtkTreeIter *parent)
2917 GtkComboBoxPrivate *priv = combo_box->priv;
2918 GtkTreeModel *model = priv->model;
2919 GtkWidget *item, *submenu, *subitem, *separator;
2921 gboolean is_separator;
2926 n_children = gtk_tree_model_iter_n_children (model, parent);
2929 for (i = 0; i < n_children; i++)
2931 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2933 if (priv->row_separator_func)
2934 is_separator = priv->row_separator_func (priv->model, &iter,
2935 priv->row_separator_data);
2937 is_separator = FALSE;
2941 item = gtk_separator_menu_item_new ();
2942 path = gtk_tree_model_get_path (model, &iter);
2943 g_object_set_data_full (G_OBJECT (item),
2944 I_("gtk-combo-box-item-path"),
2945 gtk_tree_row_reference_new (model, path),
2946 (GDestroyNotify)gtk_tree_row_reference_free);
2947 gtk_tree_path_free (path);
2951 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2952 if (gtk_tree_model_iter_has_child (model, &iter))
2954 submenu = gtk_menu_new ();
2955 gtk_widget_show (submenu);
2956 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2958 /* Ugly - since menus can only activate leafs, we have to
2959 * duplicate the item inside the submenu.
2961 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2962 separator = gtk_separator_menu_item_new ();
2963 gtk_widget_show (subitem);
2964 gtk_widget_show (separator);
2965 g_signal_connect (subitem, "activate",
2966 G_CALLBACK (gtk_combo_box_menu_item_activate),
2968 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2969 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2971 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2973 g_signal_connect (item, "activate",
2974 G_CALLBACK (gtk_combo_box_menu_item_activate),
2978 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2979 if (priv->wrap_width && menu == priv->popup_widget)
2980 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2981 gtk_widget_show (item);
2988 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2990 GtkComboBoxPrivate *priv = combo_box->priv;
2992 g_signal_handlers_disconnect_matched (priv->button,
2993 G_SIGNAL_MATCH_DATA,
2995 gtk_combo_box_menu_button_press, NULL);
2996 g_signal_handlers_disconnect_matched (priv->button,
2997 G_SIGNAL_MATCH_DATA,
2999 gtk_combo_box_button_state_changed, combo_box);
3001 /* unparent will remove our latest ref */
3002 gtk_widget_unparent (priv->button);
3005 priv->button = NULL;
3007 priv->separator = NULL;
3009 g_object_unref (priv->column);
3010 priv->column = NULL;
3012 /* changing the popup window will unref the menu and the children */
3020 menu_occupied (GtkMenu *menu,
3024 guint bottom_attach)
3028 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
3032 gtk_container_child_get (GTK_CONTAINER (menu),
3036 "bottom-attach", &b,
3040 /* look if this item intersects with the given coordinates */
3041 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
3049 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
3054 GtkComboBoxPrivate *priv = combo_box->priv;
3055 gint current_col = 0, current_row = 0;
3056 gint rows = 1, cols = 1;
3057 GtkWidget *menu = priv->popup_widget;
3059 if (!GTK_IS_MENU_SHELL (menu))
3062 if (priv->col_column == -1 &&
3063 priv->row_column == -1 &&
3066 gtk_container_child_get (GTK_CONTAINER (menu),
3068 "right-attach", ¤t_col,
3069 "top-attach", ¤t_row,
3071 if (current_col + cols > priv->wrap_width)
3079 if (priv->col_column != -1)
3080 gtk_tree_model_get (priv->model, iter,
3081 priv->col_column, &cols,
3083 if (priv->row_column != -1)
3084 gtk_tree_model_get (priv->model, iter,
3085 priv->row_column, &rows,
3090 if (current_col + cols > priv->wrap_width)
3096 if (!menu_occupied (GTK_MENU (menu),
3097 current_col, current_col + cols,
3098 current_row, current_row + rows))
3105 /* set attach props */
3106 gtk_menu_attach (GTK_MENU (menu), item,
3107 current_col, current_col + cols,
3108 current_row, current_row + rows);
3112 gtk_combo_box_relayout (GtkComboBox *combo_box)
3117 menu = combo_box->priv->popup_widget;
3119 /* do nothing unless we are in menu style and realized */
3120 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3123 list = gtk_container_get_children (GTK_CONTAINER (menu));
3125 for (j = g_list_last (list); j; j = j->prev)
3126 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3128 gtk_combo_box_menu_fill (combo_box);
3135 gtk_combo_box_menu_button_press (GtkWidget *widget,
3136 GdkEventButton *event,
3139 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3140 GtkComboBoxPrivate *priv = combo_box->priv;
3142 if (GTK_IS_MENU (priv->popup_widget) &&
3143 event->type == GDK_BUTTON_PRESS && event->button == 1)
3145 if (priv->focus_on_click &&
3146 !GTK_WIDGET_HAS_FOCUS (priv->button))
3147 gtk_widget_grab_focus (priv->button);
3149 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3158 gtk_combo_box_menu_item_activate (GtkWidget *item,
3161 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3162 GtkWidget *cell_view;
3166 cell_view = GTK_BIN (item)->child;
3168 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3170 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3172 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3174 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3175 gtk_combo_box_set_active_iter (combo_box, &iter);
3178 gtk_tree_path_free (path);
3180 combo_box->priv->editing_canceled = FALSE;
3184 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3187 gboolean sensitive = TRUE; /* fool code checkers */
3189 if (!combo_box->priv->button)
3192 switch (combo_box->priv->button_sensitivity)
3194 case GTK_SENSITIVITY_ON:
3197 case GTK_SENSITIVITY_OFF:
3200 case GTK_SENSITIVITY_AUTO:
3201 sensitive = combo_box->priv->model &&
3202 gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3205 g_assert_not_reached ();
3209 gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3213 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3218 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3220 if (combo_box->priv->tree_view)
3221 gtk_combo_box_list_popup_resize (combo_box);
3223 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3225 gtk_combo_box_update_sensitivity (combo_box);
3229 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3233 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3234 GtkComboBoxPrivate *priv = combo_box->priv;
3236 if (!gtk_tree_row_reference_valid (priv->active_row))
3238 if (priv->cell_view)
3239 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3240 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3243 if (priv->tree_view)
3244 gtk_combo_box_list_popup_resize (combo_box);
3246 gtk_combo_box_menu_row_deleted (model, path, user_data);
3248 gtk_combo_box_update_sensitivity (combo_box);
3252 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3258 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3260 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3262 if (!combo_box->priv->tree_view)
3263 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3267 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3272 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3273 GtkComboBoxPrivate *priv = combo_box->priv;
3274 GtkTreePath *active_path;
3276 /* FIXME this belongs to GtkCellView */
3277 if (gtk_tree_row_reference_valid (priv->active_row))
3279 active_path = gtk_tree_row_reference_get_path (priv->active_row);
3280 if (gtk_tree_path_compare (path, active_path) == 0 &&
3282 gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3283 gtk_tree_path_free (active_path);
3286 if (priv->tree_view)
3287 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3289 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3293 list_popup_resize_idle (gpointer user_data)
3295 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3296 GtkComboBoxPrivate *priv = combo_box->priv;
3297 gint x, y, width, height;
3299 if (priv->tree_view && GTK_WIDGET_MAPPED (priv->popup_window))
3301 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3303 gtk_widget_set_size_request (priv->popup_window, width, height);
3304 gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3307 priv->resize_idle_id = 0;
3313 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3315 GtkComboBoxPrivate *priv = combo_box->priv;
3317 if (!priv->resize_idle_id)
3318 priv->resize_idle_id =
3319 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3323 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3328 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3330 gtk_combo_box_list_popup_resize (combo_box);
3335 find_menu_by_path (GtkWidget *menu,
3337 gboolean skip_first)
3342 GtkTreeRowReference *mref;
3346 list = gtk_container_get_children (GTK_CONTAINER (menu));
3349 for (i = list; i; i = i->next)
3351 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3353 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3356 else if (!gtk_tree_row_reference_valid (mref))
3359 mpath = gtk_tree_row_reference_get_path (mref);
3361 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3369 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3374 /* this case is necessary, since the row reference of
3375 * the cell view may already be updated after a deletion
3382 if (gtk_tree_path_compare (mpath, path) == 0)
3384 gtk_tree_path_free (mpath);
3388 if (gtk_tree_path_is_ancestor (mpath, path))
3390 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3391 if (submenu != NULL)
3393 gtk_tree_path_free (mpath);
3394 item = find_menu_by_path (submenu, path, TRUE);
3398 gtk_tree_path_free (mpath);
3408 dump_menu_tree (GtkWidget *menu,
3415 list = gtk_container_get_children (GTK_CONTAINER (menu));
3416 for (i = list; i; i = i->next)
3418 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3420 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3421 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3422 gtk_tree_path_free (path);
3424 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3425 if (submenu != NULL)
3426 dump_menu_tree (submenu, level + 1);
3435 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3440 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3441 GtkComboBoxPrivate *priv = combo_box->priv;
3443 GtkWidget *item, *menu, *separator;
3447 gboolean is_separator;
3449 if (!priv->popup_widget)
3452 depth = gtk_tree_path_get_depth (path);
3453 pos = gtk_tree_path_get_indices (path)[depth - 1];
3456 ppath = gtk_tree_path_copy (path);
3457 gtk_tree_path_up (ppath);
3458 parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3459 gtk_tree_path_free (ppath);
3461 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3464 menu = gtk_menu_new ();
3465 gtk_widget_show (menu);
3466 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3468 /* Ugly - since menus can only activate leaves, we have to
3469 * duplicate the item inside the submenu.
3471 gtk_tree_model_iter_parent (model, &piter, iter);
3472 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3473 separator = gtk_separator_menu_item_new ();
3474 g_signal_connect (item, "activate",
3475 G_CALLBACK (gtk_combo_box_menu_item_activate),
3477 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3478 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3479 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3481 gtk_widget_show (item);
3482 gtk_widget_show (separator);
3489 menu = priv->popup_widget;
3490 if (priv->add_tearoffs)
3494 if (priv->row_separator_func)
3495 is_separator = priv->row_separator_func (model, iter,
3496 priv->row_separator_data);
3498 is_separator = FALSE;
3502 item = gtk_separator_menu_item_new ();
3503 g_object_set_data_full (G_OBJECT (item),
3504 I_("gtk-combo-box-item-path"),
3505 gtk_tree_row_reference_new (model, path),
3506 (GDestroyNotify)gtk_tree_row_reference_free);
3510 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3512 g_signal_connect (item, "activate",
3513 G_CALLBACK (gtk_combo_box_menu_item_activate),
3517 gtk_widget_show (item);
3518 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3522 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3526 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3527 GtkComboBoxPrivate *priv = combo_box->priv;
3531 if (!priv->popup_widget)
3534 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3535 menu = gtk_widget_get_parent (item);
3536 gtk_container_remove (GTK_CONTAINER (menu), item);
3538 if (gtk_tree_path_get_depth (path) > 1)
3540 GtkTreePath *parent_path;
3544 parent_path = gtk_tree_path_copy (path);
3545 gtk_tree_path_up (parent_path);
3546 gtk_tree_model_get_iter (model, &iter, parent_path);
3548 if (!gtk_tree_model_iter_has_child (model, &iter))
3550 parent = find_menu_by_path (priv->popup_widget,
3551 parent_path, FALSE);
3552 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3558 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3564 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3566 gtk_combo_box_relayout (combo_box);
3570 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3575 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3576 GtkComboBoxPrivate *priv = combo_box->priv;
3579 gboolean is_separator;
3581 if (!priv->popup_widget)
3584 item = find_menu_by_path (priv->popup_widget, path, FALSE);
3586 if (priv->row_separator_func)
3587 is_separator = priv->row_separator_func (model, iter,
3588 priv->row_separator_data);
3590 is_separator = FALSE;
3592 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3594 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3595 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3598 if (priv->wrap_width && item->parent == priv->popup_widget)
3600 GtkWidget *pitem = NULL;
3603 prev = gtk_tree_path_copy (path);
3605 if (gtk_tree_path_prev (prev))
3606 pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3608 gtk_tree_path_free (prev);
3610 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3612 gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3617 "bottom-attach", -1,
3620 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3623 width = gtk_combo_box_calc_requested_width (combo_box, path);
3625 if (width > priv->width)
3627 if (priv->cell_view)
3629 gtk_widget_set_size_request (priv->cell_view, width, -1);
3630 gtk_widget_queue_resize (priv->cell_view);
3632 priv->width = width;
3641 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3643 GtkComboBoxPrivate *priv = combo_box->priv;
3644 GtkTreeSelection *sel;
3646 priv->button = gtk_toggle_button_new ();
3647 gtk_widget_set_parent (priv->button,
3648 GTK_BIN (combo_box)->child->parent);
3649 g_signal_connect (priv->button, "button-press-event",
3650 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3651 g_signal_connect (priv->button, "toggled",
3652 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3654 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3655 gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3656 priv->separator = NULL;
3657 gtk_widget_show_all (priv->button);
3659 if (priv->cell_view)
3661 gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view),
3662 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3664 priv->box = gtk_event_box_new ();
3665 gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3668 if (priv->has_frame)
3670 priv->cell_view_frame = gtk_frame_new (NULL);
3671 gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3676 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3677 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3681 gtk_widget_set_parent (priv->cell_view_frame,
3682 GTK_BIN (combo_box)->child->parent);
3683 gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3684 gtk_widget_show_all (priv->cell_view_frame);
3686 g_signal_connect (priv->box, "button-press-event",
3687 G_CALLBACK (gtk_combo_box_list_button_pressed),
3691 priv->tree_view = gtk_tree_view_new ();
3692 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3693 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3694 gtk_tree_selection_set_select_function (sel,
3695 gtk_combo_box_list_select_func,
3697 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3699 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3701 if (priv->row_separator_func)
3702 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3703 priv->row_separator_func,
3704 priv->row_separator_data,
3707 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3709 priv->column = gtk_tree_view_column_new ();
3710 gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3713 gtk_combo_box_sync_cells (combo_box,
3714 GTK_CELL_LAYOUT (priv->column));
3716 if (gtk_tree_row_reference_valid (priv->active_row))
3720 path = gtk_tree_row_reference_get_path (priv->active_row);
3721 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3723 gtk_tree_path_free (path);
3726 /* set sample/popup widgets */
3727 gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3729 g_signal_connect (priv->tree_view, "key-press-event",
3730 G_CALLBACK (gtk_combo_box_list_key_press),
3732 g_signal_connect (priv->tree_view, "enter-notify-event",
3733 G_CALLBACK (gtk_combo_box_list_enter_notify),
3735 g_signal_connect (priv->tree_view, "row-expanded",
3736 G_CALLBACK (gtk_combo_box_model_row_expanded),
3738 g_signal_connect (priv->tree_view, "row-collapsed",
3739 G_CALLBACK (gtk_combo_box_model_row_expanded),
3741 g_signal_connect (priv->popup_window, "button-press-event",
3742 G_CALLBACK (gtk_combo_box_list_button_pressed),
3744 g_signal_connect (priv->popup_window, "button-release-event",
3745 G_CALLBACK (gtk_combo_box_list_button_released),
3748 gtk_widget_show (priv->tree_view);
3752 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3754 GtkComboBoxPrivate *priv = combo_box->priv;
3756 /* disconnect signals */
3757 g_signal_handlers_disconnect_matched (priv->tree_view,
3758 G_SIGNAL_MATCH_DATA,
3759 0, 0, NULL, NULL, combo_box);
3760 g_signal_handlers_disconnect_matched (priv->button,
3761 G_SIGNAL_MATCH_DATA,
3763 gtk_combo_box_list_button_pressed,
3765 g_signal_handlers_disconnect_matched (priv->popup_window,
3766 G_SIGNAL_MATCH_DATA,
3768 gtk_combo_box_list_button_pressed,
3770 g_signal_handlers_disconnect_matched (priv->popup_window,
3771 G_SIGNAL_MATCH_DATA,
3773 gtk_combo_box_list_button_released,
3776 g_signal_handlers_disconnect_matched (priv->popup_window,
3777 G_SIGNAL_MATCH_DATA,
3779 gtk_combo_box_child_show,
3782 g_signal_handlers_disconnect_matched (priv->popup_window,
3783 G_SIGNAL_MATCH_DATA,
3785 gtk_combo_box_child_hide,
3789 g_signal_handlers_disconnect_matched (priv->box,
3790 G_SIGNAL_MATCH_DATA,
3792 gtk_combo_box_list_button_pressed,
3795 /* destroy things (unparent will kill the latest ref from us)
3796 * last unref on button will destroy the arrow
3798 gtk_widget_unparent (priv->button);
3799 priv->button = NULL;
3802 if (priv->cell_view)
3804 g_object_set (priv->cell_view,
3805 "background-set", FALSE,
3809 if (priv->cell_view_frame)
3811 gtk_widget_unparent (priv->cell_view_frame);
3812 priv->cell_view_frame = NULL;
3816 if (priv->scroll_timer)
3818 g_source_remove (priv->scroll_timer);
3819 priv->scroll_timer = 0;
3822 if (priv->resize_idle_id)
3824 g_source_remove (priv->resize_idle_id);
3825 priv->resize_idle_id = 0;
3828 gtk_widget_destroy (priv->tree_view);
3830 priv->tree_view = NULL;
3831 if (priv->popup_widget)
3833 g_object_unref (priv->popup_widget);
3834 priv->popup_widget = NULL;
3841 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3842 GdkEventButton *event,
3845 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3846 GtkComboBoxPrivate *priv = combo_box->priv;
3848 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3850 if (ewidget == priv->popup_window)
3853 if ((ewidget != priv->button && ewidget != priv->box) ||
3854 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3857 if (priv->focus_on_click &&
3858 !GTK_WIDGET_HAS_FOCUS (priv->button))
3859 gtk_widget_grab_focus (priv->button);
3861 gtk_combo_box_popup (combo_box);
3863 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3865 priv->auto_scroll = FALSE;
3866 if (priv->scroll_timer == 0)
3867 priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3868 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3871 priv->popup_in_progress = TRUE;
3877 gtk_combo_box_list_button_released (GtkWidget *widget,
3878 GdkEventButton *event,
3882 GtkTreePath *path = NULL;
3885 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3886 GtkComboBoxPrivate *priv = combo_box->priv;
3888 gboolean popup_in_progress = FALSE;
3890 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3892 if (priv->popup_in_progress)
3894 popup_in_progress = TRUE;
3895 priv->popup_in_progress = FALSE;
3898 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3900 if (priv->scroll_timer)
3902 g_source_remove (priv->scroll_timer);
3903 priv->scroll_timer = 0;
3906 if (ewidget != priv->tree_view)
3908 if ((ewidget == priv->button ||
3909 ewidget == priv->box) &&
3910 !popup_in_progress &&
3911 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3913 gtk_combo_box_popdown (combo_box);
3917 /* released outside treeview */
3918 if (ewidget != priv->button && ewidget != priv->box)
3920 gtk_combo_box_popdown (combo_box);
3928 /* select something cool */
3929 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3935 return TRUE; /* clicked outside window? */
3937 gtk_tree_model_get_iter (priv->model, &iter, path);
3938 gtk_tree_path_free (path);
3940 gtk_combo_box_popdown (combo_box);
3942 if (tree_column_row_is_sensitive (combo_box, &iter))
3943 gtk_combo_box_set_active_iter (combo_box, &iter);
3949 gtk_combo_box_menu_key_press (GtkWidget *widget,
3953 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3955 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3957 /* The menu hasn't managed the
3958 * event, forward it to the combobox
3960 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3967 gtk_combo_box_list_key_press (GtkWidget *widget,
3971 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3974 if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
3975 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
3977 GtkTreeModel *model = NULL;
3979 gtk_combo_box_popdown (combo_box);
3981 if (combo_box->priv->model)
3983 GtkTreeSelection *sel;
3985 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3987 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3988 gtk_combo_box_set_active_iter (combo_box, &iter);
3994 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3996 /* The list hasn't managed the
3997 * event, forward it to the combobox
3999 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
4006 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
4010 GtkWidget *tree_view = combo_box->priv->tree_view;
4014 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4015 if (adj && adj->upper - adj->lower > adj->page_size)
4017 if (x <= tree_view->allocation.x &&
4018 adj->lower < adj->value)
4020 value = adj->value - (tree_view->allocation.x - x + 1);
4021 gtk_adjustment_set_value (adj, value);
4023 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
4024 adj->upper - adj->page_size > adj->value)
4026 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
4027 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4031 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
4032 if (adj && adj->upper - adj->lower > adj->page_size)
4034 if (y <= tree_view->allocation.y &&
4035 adj->lower < adj->value)
4037 value = adj->value - (tree_view->allocation.y - y + 1);
4038 gtk_adjustment_set_value (adj, value);
4040 else if (y >= tree_view->allocation.height &&
4041 adj->upper - adj->page_size > adj->value)
4043 value = adj->value + (y - tree_view->allocation.height + 1);
4044 gtk_adjustment_set_value (adj, MAX (value, 0.0));
4050 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
4052 GtkComboBoxPrivate *priv = combo_box->priv;
4055 if (priv->auto_scroll)
4057 gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
4058 gtk_combo_box_list_auto_scroll (combo_box, x, y);
4065 gtk_combo_box_list_enter_notify (GtkWidget *widget,
4066 GdkEventCrossing *event,
4069 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4071 combo_box->priv->auto_scroll = TRUE;
4077 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
4078 GtkTreeModel *model,
4080 gboolean path_currently_selected,
4084 gboolean sensitive = FALSE;
4086 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
4088 GList *cells, *cell;
4089 gboolean cell_sensitive, cell_visible;
4091 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
4093 if (!column->visible)
4096 gtk_tree_model_get_iter (model, &iter, path);
4097 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
4100 cell = cells = gtk_tree_view_column_get_cell_renderers (column);
4103 g_object_get (cell->data,
4104 "sensitive", &cell_sensitive,
4105 "visible", &cell_visible,
4108 if (cell_visible && cell_sensitive)
4113 g_list_free (cells);
4115 sensitive = cell_sensitive;
4122 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4127 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4128 GtkComboBoxPrivate *priv = combo_box->priv;
4131 width = gtk_combo_box_calc_requested_width (combo_box, path);
4133 if (width > priv->width)
4135 if (priv->cell_view)
4137 gtk_widget_set_size_request (priv->cell_view, width, -1);
4138 gtk_widget_queue_resize (priv->cell_view);
4140 priv->width = width;
4145 * GtkCellLayout implementation
4149 pack_start_recurse (GtkWidget *menu,
4150 GtkCellRenderer *cell,
4156 list = gtk_container_get_children (GTK_CONTAINER (menu));
4157 for (i = list; i; i = i->next)
4159 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4160 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4163 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4164 if (submenu != NULL)
4165 pack_start_recurse (submenu, cell, expand);
4172 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4173 GtkCellRenderer *cell,
4176 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4177 ComboCellInfo *info;
4178 GtkComboBoxPrivate *priv;
4180 priv = combo_box->priv;
4182 g_object_ref_sink (cell);
4184 info = g_slice_new0 (ComboCellInfo);
4186 info->expand = expand;
4187 info->pack = GTK_PACK_START;
4189 priv->cells = g_slist_append (priv->cells, info);
4191 if (priv->cell_view)
4192 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4196 gtk_tree_view_column_pack_start (priv->column, cell, expand);
4198 if (GTK_IS_MENU (priv->popup_widget))
4199 pack_start_recurse (priv->popup_widget, cell, expand);
4203 pack_end_recurse (GtkWidget *menu,
4204 GtkCellRenderer *cell,
4210 list = gtk_container_get_children (GTK_CONTAINER (menu));
4211 for (i = list; i; i = i->next)
4213 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4214 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4217 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4218 if (submenu != NULL)
4219 pack_end_recurse (submenu, cell, expand);
4226 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4227 GtkCellRenderer *cell,
4230 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4231 ComboCellInfo *info;
4232 GtkComboBoxPrivate *priv;
4234 priv = combo_box->priv;
4236 g_object_ref_sink (cell);
4238 info = g_slice_new0 (ComboCellInfo);
4240 info->expand = expand;
4241 info->pack = GTK_PACK_END;
4243 priv->cells = g_slist_append (priv->cells, info);
4245 if (priv->cell_view)
4246 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4250 gtk_tree_view_column_pack_end (priv->column, cell, expand);
4252 if (GTK_IS_MENU (priv->popup_widget))
4253 pack_end_recurse (priv->popup_widget, cell, expand);
4257 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4259 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4261 GList *retval = NULL;
4263 for (list = combo_box->priv->cells; list; list = list->next)
4265 ComboCellInfo *info = (ComboCellInfo *)list->data;
4267 retval = g_list_prepend (retval, info->cell);
4270 return g_list_reverse (retval);
4274 clear_recurse (GtkWidget *menu)
4279 list = gtk_container_get_children (GTK_CONTAINER (menu));
4280 for (i = list; i; i = i->next)
4282 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4283 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4285 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4286 if (submenu != NULL)
4287 clear_recurse (submenu);
4294 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4296 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4297 GtkComboBoxPrivate *priv = combo_box->priv;
4300 if (priv->cell_view)
4301 gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4304 gtk_tree_view_column_clear (priv->column);
4306 for (i = priv->cells; i; i = i->next)
4308 ComboCellInfo *info = (ComboCellInfo *)i->data;
4310 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4311 g_object_unref (info->cell);
4312 g_slice_free (ComboCellInfo, info);
4315 g_slist_free (priv->cells);
4318 if (GTK_IS_MENU (priv->popup_widget))
4319 clear_recurse (priv->popup_widget);
4323 add_attribute_recurse (GtkWidget *menu,
4324 GtkCellRenderer *cell,
4325 const gchar *attribute,
4331 list = gtk_container_get_children (GTK_CONTAINER (menu));
4332 for (i = list; i; i = i->next)
4334 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4335 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4336 cell, attribute, column);
4338 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4339 if (submenu != NULL)
4340 add_attribute_recurse (submenu, cell, attribute, column);
4347 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4348 GtkCellRenderer *cell,
4349 const gchar *attribute,
4352 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4353 ComboCellInfo *info;
4355 info = gtk_combo_box_get_cell_info (combo_box, cell);
4356 g_return_if_fail (info != NULL);
4358 info->attributes = g_slist_prepend (info->attributes,
4359 GINT_TO_POINTER (column));
4360 info->attributes = g_slist_prepend (info->attributes,
4361 g_strdup (attribute));
4363 if (combo_box->priv->cell_view)
4364 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4365 cell, attribute, column);
4367 if (combo_box->priv->column)
4368 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4369 cell, attribute, column);
4371 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4372 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4373 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4377 combo_cell_data_func (GtkCellLayout *cell_layout,
4378 GtkCellRenderer *cell,
4379 GtkTreeModel *tree_model,
4383 ComboCellInfo *info = (ComboCellInfo *)data;
4384 GtkWidget *parent = NULL;
4389 info->func (cell_layout, cell, tree_model, iter, info->func_data);
4391 if (GTK_IS_WIDGET (cell_layout))
4392 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4394 if (GTK_IS_MENU_ITEM (parent) &&
4395 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4396 g_object_set (cell, "sensitive", TRUE, NULL);
4401 set_cell_data_func_recurse (GtkWidget *menu,
4402 GtkCellRenderer *cell,
4403 ComboCellInfo *info)
4407 GtkWidget *cell_view;
4409 list = gtk_container_get_children (GTK_CONTAINER (menu));
4410 for (i = list; i; i = i->next)
4412 cell_view = GTK_BIN (i->data)->child;
4413 if (GTK_IS_CELL_LAYOUT (cell_view))
4415 /* Override sensitivity for inner nodes; we don't
4416 * want menuitems with submenus to appear insensitive
4418 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4420 combo_cell_data_func,
4422 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4423 if (submenu != NULL)
4424 set_cell_data_func_recurse (submenu, cell, info);
4432 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4433 GtkCellRenderer *cell,
4434 GtkCellLayoutDataFunc func,
4436 GDestroyNotify destroy)
4438 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4439 GtkComboBoxPrivate *priv = combo_box->priv;
4440 ComboCellInfo *info;
4442 info = gtk_combo_box_get_cell_info (combo_box, cell);
4443 g_return_if_fail (info != NULL);
4447 GDestroyNotify d = info->destroy;
4449 info->destroy = NULL;
4450 d (info->func_data);
4454 info->func_data = func_data;
4455 info->destroy = destroy;
4457 if (priv->cell_view)
4458 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4461 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4463 if (GTK_IS_MENU (priv->popup_widget))
4464 set_cell_data_func_recurse (priv->popup_widget, cell, info);
4466 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4470 clear_attributes_recurse (GtkWidget *menu,
4471 GtkCellRenderer *cell)
4476 list = gtk_container_get_children (GTK_CONTAINER (menu));
4477 for (i = list; i; i = i->next)
4479 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4480 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4483 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4484 if (submenu != NULL)
4485 clear_attributes_recurse (submenu, cell);
4492 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4493 GtkCellRenderer *cell)
4495 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4496 GtkComboBoxPrivate *priv;
4497 ComboCellInfo *info;
4500 priv = combo_box->priv;
4502 info = gtk_combo_box_get_cell_info (combo_box, cell);
4503 g_return_if_fail (info != NULL);
4505 list = info->attributes;
4506 while (list && list->next)
4508 g_free (list->data);
4509 list = list->next->next;
4511 g_slist_free (info->attributes);
4512 info->attributes = NULL;
4514 if (priv->cell_view)
4515 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4518 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4520 if (GTK_IS_MENU (priv->popup_widget))
4521 clear_attributes_recurse (priv->popup_widget, cell);
4523 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4527 reorder_recurse (GtkWidget *menu,
4528 GtkCellRenderer *cell,
4534 list = gtk_container_get_children (GTK_CONTAINER (menu));
4535 for (i = list; i; i = i->next)
4537 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4538 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4541 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4542 if (submenu != NULL)
4543 reorder_recurse (submenu, cell, position);
4550 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4551 GtkCellRenderer *cell,
4554 GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4555 GtkComboBoxPrivate *priv;
4556 ComboCellInfo *info;
4559 priv = combo_box->priv;
4561 info = gtk_combo_box_get_cell_info (combo_box, cell);
4563 g_return_if_fail (info != NULL);
4564 g_return_if_fail (position >= 0);
4566 link = g_slist_find (priv->cells, info);
4568 g_return_if_fail (link != NULL);
4570 priv->cells = g_slist_delete_link (priv->cells, link);
4571 priv->cells = g_slist_insert (priv->cells, info, position);
4573 if (priv->cell_view)
4574 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4578 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4581 if (GTK_IS_MENU (priv->popup_widget))
4582 reorder_recurse (priv->popup_widget, cell, position);
4584 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4592 * gtk_combo_box_new:
4594 * Creates a new empty #GtkComboBox.
4596 * Return value: A new #GtkComboBox.
4601 gtk_combo_box_new (void)
4603 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4607 * gtk_combo_box_new_with_model:
4608 * @model: A #GtkTreeModel.
4610 * Creates a new #GtkComboBox with the model initialized to @model.
4612 * Return value: A new #GtkComboBox.
4617 gtk_combo_box_new_with_model (GtkTreeModel *model)
4619 GtkComboBox *combo_box;
4621 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4623 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4625 return GTK_WIDGET (combo_box);
4629 * gtk_combo_box_get_wrap_width:
4630 * @combo_box: A #GtkComboBox
4632 * Returns the wrap width which is used to determine the number of columns
4633 * for the popup menu. If the wrap width is larger than 1, the combo box
4636 * Returns: the wrap width.
4641 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4643 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4645 return combo_box->priv->wrap_width;
4649 * gtk_combo_box_set_wrap_width:
4650 * @combo_box: A #GtkComboBox
4651 * @width: Preferred number of columns
4653 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4654 * the preferred number of columns when you want the popup to be layed out
4660 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4663 GtkComboBoxPrivate *priv;
4665 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4666 g_return_if_fail (width >= 0);
4668 priv = combo_box->priv;
4670 if (width != priv->wrap_width)
4672 priv->wrap_width = width;
4674 gtk_combo_box_check_appearance (combo_box);
4675 gtk_combo_box_relayout (combo_box);
4677 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4682 * gtk_combo_box_get_row_span_column:
4683 * @combo_box: A #GtkComboBox
4685 * Returns the column with row span information for @combo_box.
4687 * Returns: the row span column.
4692 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4694 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4696 return combo_box->priv->row_column;
4700 * gtk_combo_box_set_row_span_column:
4701 * @combo_box: A #GtkComboBox.
4702 * @row_span: A column in the model passed during construction.
4704 * Sets the column with row span information for @combo_box to be @row_span.
4705 * The row span column contains integers which indicate how many rows
4706 * an item should span.
4711 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4714 GtkComboBoxPrivate *priv;
4717 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4719 priv = combo_box->priv;
4721 col = gtk_tree_model_get_n_columns (priv->model);
4722 g_return_if_fail (row_span >= -1 && row_span < col);
4724 if (row_span != priv->row_column)
4726 priv->row_column = row_span;
4728 gtk_combo_box_relayout (combo_box);
4730 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4735 * gtk_combo_box_get_column_span_column:
4736 * @combo_box: A #GtkComboBox
4738 * Returns the column with column span information for @combo_box.
4740 * Returns: the column span column.
4745 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4747 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4749 return combo_box->priv->col_column;
4753 * gtk_combo_box_set_column_span_column:
4754 * @combo_box: A #GtkComboBox
4755 * @column_span: A column in the model passed during construction
4757 * Sets the column with column span information for @combo_box to be
4758 * @column_span. The column span column contains integers which indicate
4759 * how many columns an item should span.
4764 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4767 GtkComboBoxPrivate *priv;
4770 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4772 priv = combo_box->priv;
4774 col = gtk_tree_model_get_n_columns (priv->model);
4775 g_return_if_fail (column_span >= -1 && column_span < col);
4777 if (column_span != priv->col_column)
4779 priv->col_column = column_span;
4781 gtk_combo_box_relayout (combo_box);
4783 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4788 * gtk_combo_box_get_active:
4789 * @combo_box: A #GtkComboBox
4791 * Returns the index of the currently active item, or -1 if there's no
4792 * active item. If the model is a non-flat treemodel, and the active item
4793 * is not an immediate child of the root of the tree, this function returns
4794 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4795 * <literal>path</literal> is the #GtkTreePath of the active item.
4797 * Return value: An integer which is the index of the currently active item,
4798 * or -1 if there's no active item.
4803 gtk_combo_box_get_active (GtkComboBox *combo_box)
4805 GtkComboBoxPrivate *priv;
4808 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4810 priv = combo_box->priv;
4812 if (gtk_tree_row_reference_valid (priv->active_row))
4816 path = gtk_tree_row_reference_get_path (priv->active_row);
4817 result = gtk_tree_path_get_indices (path)[0];
4818 gtk_tree_path_free (path);
4827 * gtk_combo_box_set_active:
4828 * @combo_box: A #GtkComboBox
4829 * @index_: An index in the model passed during construction, or -1 to have
4832 * Sets the active item of @combo_box to be the item at @index.
4837 gtk_combo_box_set_active (GtkComboBox *combo_box,
4840 GtkTreePath *path = NULL;
4841 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4842 g_return_if_fail (index_ >= -1);
4844 if (combo_box->priv->model == NULL)
4846 /* Save index, in case the model is set after the index */
4847 combo_box->priv->active = index_;
4852 path = gtk_tree_path_new_from_indices (index_, -1);
4854 gtk_combo_box_set_active_internal (combo_box, path);
4857 gtk_tree_path_free (path);
4861 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4864 GtkComboBoxPrivate *priv = combo_box->priv;
4865 GtkTreePath *active_path;
4868 if (path && gtk_tree_row_reference_valid (priv->active_row))
4870 active_path = gtk_tree_row_reference_get_path (priv->active_row);
4871 path_cmp = gtk_tree_path_compare (path, active_path);
4872 gtk_tree_path_free (active_path);
4877 if (priv->active_row)
4879 gtk_tree_row_reference_free (priv->active_row);
4880 priv->active_row = NULL;
4885 if (priv->tree_view)
4886 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4889 GtkMenu *menu = GTK_MENU (priv->popup_widget);
4891 if (GTK_IS_MENU (menu))
4892 gtk_menu_set_active (menu, -1);
4895 if (priv->cell_view)
4896 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4901 gtk_tree_row_reference_new (priv->model, path);
4903 if (priv->tree_view)
4905 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4908 else if (GTK_IS_MENU (priv->popup_widget))
4910 /* FIXME handle nested menus better */
4911 gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4912 gtk_tree_path_get_indices (path)[0]);
4915 if (priv->cell_view)
4916 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4920 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4921 g_object_notify (G_OBJECT (combo_box), "active");
4926 * gtk_combo_box_get_active_iter:
4927 * @combo_box: A #GtkComboBox
4928 * @iter: The uninitialized #GtkTreeIter
4930 * Sets @iter to point to the current active item, if it exists.
4932 * Return value: %TRUE, if @iter was set
4937 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4943 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4945 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4948 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4949 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4950 gtk_tree_path_free (path);
4956 * gtk_combo_box_set_active_iter:
4957 * @combo_box: A #GtkComboBox
4958 * @iter: The #GtkTreeIter
4960 * Sets the current active item to be the one referenced by @iter.
4961 * @iter must correspond to a path of depth one.
4966 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4971 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4973 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4974 gtk_combo_box_set_active_internal (combo_box, path);
4975 gtk_tree_path_free (path);
4979 * gtk_combo_box_set_model:
4980 * @combo_box: A #GtkComboBox
4981 * @model: A #GtkTreeModel
4983 * Sets the model used by @combo_box to be @model. Will unset a previously set
4984 * model (if applicable). If model is %NULL, then it will unset the model.
4986 * Note that this function does not clear the cell renderers, you have to
4987 * call gtk_cell_layout_clear() yourself if you need to set up different
4988 * cell renderers for the new model.
4993 gtk_combo_box_set_model (GtkComboBox *combo_box,
4994 GtkTreeModel *model)
4996 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4997 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4999 if (model == combo_box->priv->model)
5002 gtk_combo_box_unset_model (combo_box);
5007 combo_box->priv->model = model;
5008 g_object_ref (combo_box->priv->model);
5010 combo_box->priv->inserted_id =
5011 g_signal_connect (combo_box->priv->model, "row-inserted",
5012 G_CALLBACK (gtk_combo_box_model_row_inserted),
5014 combo_box->priv->deleted_id =
5015 g_signal_connect (combo_box->priv->model, "row-deleted",
5016 G_CALLBACK (gtk_combo_box_model_row_deleted),
5018 combo_box->priv->reordered_id =
5019 g_signal_connect (combo_box->priv->model, "rows-reordered",
5020 G_CALLBACK (gtk_combo_box_model_rows_reordered),
5022 combo_box->priv->changed_id =
5023 g_signal_connect (combo_box->priv->model, "row-changed",
5024 G_CALLBACK (gtk_combo_box_model_row_changed),
5027 if (combo_box->priv->tree_view)
5030 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
5031 combo_box->priv->model);
5032 gtk_combo_box_list_popup_resize (combo_box);
5037 if (combo_box->priv->popup_widget)
5038 gtk_combo_box_menu_fill (combo_box);
5042 if (combo_box->priv->cell_view)
5043 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
5044 combo_box->priv->model);
5046 if (combo_box->priv->active != -1)
5048 /* If an index was set in advance, apply it now */
5049 gtk_combo_box_set_active (combo_box, combo_box->priv->active);
5050 combo_box->priv->active = -1;
5054 gtk_combo_box_update_sensitivity (combo_box);
5056 g_object_notify (G_OBJECT (combo_box), "model");
5060 * gtk_combo_box_get_model
5061 * @combo_box: A #GtkComboBox
5063 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
5065 * Return value: A #GtkTreeModel which was passed during construction.
5070 gtk_combo_box_get_model (GtkComboBox *combo_box)
5072 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5074 return combo_box->priv->model;
5078 /* convenience API for simple text combos */
5081 * gtk_combo_box_new_text:
5083 * Convenience function which constructs a new text combo box, which is a
5084 * #GtkComboBox just displaying strings. If you use this function to create
5085 * a text combo box, you should only manipulate its data source with the
5086 * following convenience functions: gtk_combo_box_append_text(),
5087 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
5088 * gtk_combo_box_remove_text().
5090 * Return value: A new text combo box.
5095 gtk_combo_box_new_text (void)
5097 GtkWidget *combo_box;
5098 GtkCellRenderer *cell;
5099 GtkListStore *store;
5101 store = gtk_list_store_new (1, G_TYPE_STRING);
5102 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
5103 g_object_unref (store);
5105 cell = gtk_cell_renderer_text_new ();
5106 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
5107 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
5115 * gtk_combo_box_append_text:
5116 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5119 * Appends @string to the list of strings stored in @combo_box. Note that
5120 * you can only use this function with combo boxes constructed with
5121 * gtk_combo_box_new_text().
5126 gtk_combo_box_append_text (GtkComboBox *combo_box,
5130 GtkListStore *store;
5132 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5133 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5134 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5136 g_return_if_fail (text != NULL);
5138 store = GTK_LIST_STORE (combo_box->priv->model);
5140 gtk_list_store_append (store, &iter);
5141 gtk_list_store_set (store, &iter, 0, text, -1);
5145 * gtk_combo_box_insert_text:
5146 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5147 * @position: An index to insert @text
5150 * Inserts @string at @position in the list of strings stored in @combo_box.
5151 * Note that you can only use this function with combo boxes constructed
5152 * with gtk_combo_box_new_text().
5157 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5162 GtkListStore *store;
5164 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5165 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5166 g_return_if_fail (position >= 0);
5167 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5169 g_return_if_fail (text != NULL);
5171 store = GTK_LIST_STORE (combo_box->priv->model);
5173 gtk_list_store_insert (store, &iter, position);
5174 gtk_list_store_set (store, &iter, 0, text, -1);
5178 * gtk_combo_box_prepend_text:
5179 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5182 * Prepends @string to the list of strings stored in @combo_box. Note that
5183 * you can only use this function with combo boxes constructed with
5184 * gtk_combo_box_new_text().
5189 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5193 GtkListStore *store;
5195 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5196 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5197 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5199 g_return_if_fail (text != NULL);
5201 store = GTK_LIST_STORE (combo_box->priv->model);
5203 gtk_list_store_prepend (store, &iter);
5204 gtk_list_store_set (store, &iter, 0, text, -1);
5208 * gtk_combo_box_remove_text:
5209 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5210 * @position: Index of the item to remove
5212 * Removes the string at @position from @combo_box. Note that you can only use
5213 * this function with combo boxes constructed with gtk_combo_box_new_text().
5218 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5222 GtkListStore *store;
5224 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5225 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5226 g_return_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5228 g_return_if_fail (position >= 0);
5230 store = GTK_LIST_STORE (combo_box->priv->model);
5232 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5234 gtk_list_store_remove (store, &iter);
5238 * gtk_combo_box_get_active_text:
5239 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5241 * Returns the currently active string in @combo_box or %NULL if none
5242 * is selected. Note that you can only use this function with combo
5243 * boxes constructed with gtk_combo_box_new_text() and with
5244 * #GtkComboBoxEntry<!-- -->s.
5246 * Returns: a newly allocated string containing the currently active text.
5251 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5253 GtkComboBoxClass *class;
5255 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5257 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5259 if (class->get_active_text)
5260 return class->get_active_text (combo_box);
5266 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5271 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5272 g_return_val_if_fail (gtk_tree_model_get_column_type (combo_box->priv->model, 0)
5273 == G_TYPE_STRING, NULL);
5275 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5276 gtk_tree_model_get (combo_box->priv->model, &iter,
5283 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5284 GtkScrollType scroll)
5287 GtkTreeIter new_iter;
5288 gboolean active_iter;
5291 if (!combo_box->priv->model)
5293 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5297 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5301 case GTK_SCROLL_STEP_BACKWARD:
5302 case GTK_SCROLL_STEP_UP:
5303 case GTK_SCROLL_STEP_LEFT:
5306 found = tree_prev (combo_box, combo_box->priv->model,
5307 &iter, &new_iter, FALSE);
5310 /* else fall through */
5312 case GTK_SCROLL_PAGE_FORWARD:
5313 case GTK_SCROLL_PAGE_DOWN:
5314 case GTK_SCROLL_PAGE_RIGHT:
5315 case GTK_SCROLL_END:
5316 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5319 case GTK_SCROLL_STEP_FORWARD:
5320 case GTK_SCROLL_STEP_DOWN:
5321 case GTK_SCROLL_STEP_RIGHT:
5324 found = tree_next (combo_box, combo_box->priv->model,
5325 &iter, &new_iter, FALSE);
5328 /* else fall through */
5330 case GTK_SCROLL_PAGE_BACKWARD:
5331 case GTK_SCROLL_PAGE_UP:
5332 case GTK_SCROLL_PAGE_LEFT:
5333 case GTK_SCROLL_START:
5334 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5341 if (found && active_iter)
5343 GtkTreePath *old_path;
5344 GtkTreePath *new_path;
5346 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5347 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5349 if (gtk_tree_path_compare (old_path, new_path) == 0)
5352 gtk_tree_path_free (old_path);
5353 gtk_tree_path_free (new_path);
5358 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5362 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5367 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5368 gboolean group_cycling)
5370 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5372 gtk_widget_grab_focus (combo_box->priv->button);
5378 gtk_combo_box_grab_focus (GtkWidget *widget)
5380 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5382 gtk_widget_grab_focus (combo_box->priv->button);
5386 gtk_combo_box_destroy (GtkObject *object)
5388 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5390 if (combo_box->priv->popup_idle_id > 0)
5392 g_source_remove (combo_box->priv->popup_idle_id);
5393 combo_box->priv->popup_idle_id = 0;
5396 gtk_combo_box_popdown (combo_box);
5398 if (combo_box->priv->row_separator_destroy)
5399 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5401 combo_box->priv->row_separator_func = NULL;
5402 combo_box->priv->row_separator_data = NULL;
5403 combo_box->priv->row_separator_destroy = NULL;
5405 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5406 combo_box->priv->cell_view = NULL;
5410 gtk_combo_box_dispose(GObject* object)
5412 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5414 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5416 gtk_combo_box_menu_destroy (combo_box);
5417 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5418 combo_box->priv->popup_widget = NULL;
5421 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5425 gtk_combo_box_finalize (GObject *object)
5427 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5430 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5431 gtk_combo_box_list_destroy (combo_box);
5433 if (combo_box->priv->popup_window)
5434 gtk_widget_destroy (combo_box->priv->popup_window);
5436 gtk_combo_box_unset_model (combo_box);
5438 for (i = combo_box->priv->cells; i; i = i->next)
5440 ComboCellInfo *info = (ComboCellInfo *)i->data;
5441 GSList *list = info->attributes;
5444 info->destroy (info->func_data);
5446 while (list && list->next)
5448 g_free (list->data);
5449 list = list->next->next;
5451 g_slist_free (info->attributes);
5453 g_object_unref (info->cell);
5454 g_slice_free (ComboCellInfo, info);
5456 g_slist_free (combo_box->priv->cells);
5458 g_free (combo_box->priv->tearoff_title);
5460 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5464 gtk_cell_editable_key_press (GtkWidget *widget,
5468 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5470 if (event->keyval == GDK_Escape)
5472 combo_box->priv->editing_canceled = TRUE;
5474 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5475 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5479 else if (event->keyval == GDK_Return ||
5480 event->keyval == GDK_ISO_Enter ||
5481 event->keyval == GDK_KP_Enter)
5483 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5484 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5493 popdown_idle (gpointer data)
5495 GtkComboBox *combo_box;
5497 combo_box = GTK_COMBO_BOX (data);
5499 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5500 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5502 g_object_unref (combo_box);
5508 popdown_handler (GtkWidget *widget,
5511 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5515 popup_idle (gpointer data)
5517 GtkComboBox *combo_box;
5519 combo_box = GTK_COMBO_BOX (data);
5521 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5522 combo_box->priv->cell_view)
5523 g_signal_connect_object (combo_box->priv->popup_widget,
5524 "unmap", G_CALLBACK (popdown_handler),
5527 /* we unset this if a menu item is activated */
5528 combo_box->priv->editing_canceled = TRUE;
5529 gtk_combo_box_popup (combo_box);
5531 combo_box->priv->popup_idle_id = 0;
5532 combo_box->priv->activate_button = 0;
5533 combo_box->priv->activate_time = 0;
5539 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5542 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5544 combo_box->priv->is_cell_renderer = TRUE;
5546 if (combo_box->priv->cell_view)
5548 g_signal_connect_object (combo_box->priv->button, "key-press-event",
5549 G_CALLBACK (gtk_cell_editable_key_press),
5552 gtk_widget_grab_focus (combo_box->priv->button);
5556 g_signal_connect_object (GTK_BIN (combo_box)->child, "key-press-event",
5557 G_CALLBACK (gtk_cell_editable_key_press),
5560 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5561 GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5564 /* we do the immediate popup only for the optionmenu-like
5567 if (combo_box->priv->is_cell_renderer &&
5568 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5570 if (event && event->type == GDK_BUTTON_PRESS)
5572 GdkEventButton *event_button = (GdkEventButton *)event;
5574 combo_box->priv->activate_button = event_button->button;
5575 combo_box->priv->activate_time = event_button->time;
5578 combo_box->priv->popup_idle_id =
5579 gdk_threads_add_idle (popup_idle, combo_box);
5585 * gtk_combo_box_get_add_tearoffs:
5586 * @combo_box: a #GtkComboBox
5588 * Gets the current value of the :add-tearoffs property.
5590 * Return value: the current value of the :add-tearoffs property.
5593 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5595 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5597 return combo_box->priv->add_tearoffs;
5601 * gtk_combo_box_set_add_tearoffs:
5602 * @combo_box: a #GtkComboBox
5603 * @add_tearoffs: %TRUE to add tearoff menu items
5605 * Sets whether the popup menu should have a tearoff
5611 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5612 gboolean add_tearoffs)
5614 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5616 add_tearoffs = add_tearoffs != FALSE;
5618 if (combo_box->priv->add_tearoffs != add_tearoffs)
5620 combo_box->priv->add_tearoffs = add_tearoffs;
5621 gtk_combo_box_check_appearance (combo_box);
5622 gtk_combo_box_relayout (combo_box);
5623 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5628 * gtk_combo_box_get_title:
5629 * @combo_box: a #GtkComboBox
5631 * Gets the current title of the menu in tearoff mode. See
5632 * gtk_combo_box_set_add_tearoffs().
5634 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5635 * string which must not be freed.
5639 G_CONST_RETURN gchar*
5640 gtk_combo_box_get_title (GtkComboBox *combo_box)
5642 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5644 return combo_box->priv->tearoff_title;
5648 gtk_combo_box_update_title (GtkComboBox *combo_box)
5650 gtk_combo_box_check_appearance (combo_box);
5652 if (combo_box->priv->popup_widget &&
5653 GTK_IS_MENU (combo_box->priv->popup_widget))
5654 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5655 combo_box->priv->tearoff_title);
5659 * gtk_combo_box_set_title:
5660 * @combo_box: a #GtkComboBox
5661 * @title: a title for the menu in tearoff mode
5663 * Sets the menu's title in tearoff mode.
5668 gtk_combo_box_set_title (GtkComboBox *combo_box,
5671 GtkComboBoxPrivate *priv;
5673 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5675 priv = combo_box->priv;
5677 if (strcmp (title ? title : "",
5678 priv->tearoff_title ? priv->tearoff_title : "") != 0)
5680 g_free (priv->tearoff_title);
5681 priv->tearoff_title = g_strdup (title);
5683 gtk_combo_box_update_title (combo_box);
5685 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5690 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5692 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5694 return combo_box->priv->editing_canceled;
5698 * gtk_combo_box_get_popup_accessible:
5699 * @combo_box: a #GtkComboBox
5701 * Gets the accessible object corresponding to the combo box's popup.
5703 * This function is mostly intended for use by accessibility technologies;
5704 * applications should have little use for it.
5706 * Returns: the accessible object corresponding to the combo box's popup.
5711 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5715 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5717 if (combo_box->priv->popup_widget)
5719 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5727 * gtk_combo_box_get_row_separator_func:
5728 * @combo_box: a #GtkComboBox
5730 * Returns the current row separator function.
5732 * Return value: the current row separator function.
5736 GtkTreeViewRowSeparatorFunc
5737 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5739 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5741 return combo_box->priv->row_separator_func;
5745 * gtk_combo_box_set_row_separator_func:
5746 * @combo_box: a #GtkComboBox
5747 * @func: a #GtkTreeViewRowSeparatorFunc
5748 * @data: user data to pass to @func, or %NULL
5749 * @destroy: destroy notifier for @data, or %NULL
5751 * Sets the row separator function, which is used to determine
5752 * whether a row should be drawn as a separator. If the row separator
5753 * function is %NULL, no separators are drawn. This is the default value.
5758 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5759 GtkTreeViewRowSeparatorFunc func,
5761 GDestroyNotify destroy)
5763 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5765 if (combo_box->priv->row_separator_destroy)
5766 combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5768 combo_box->priv->row_separator_func = func;
5769 combo_box->priv->row_separator_data = data;
5770 combo_box->priv->row_separator_destroy = destroy;
5772 if (combo_box->priv->tree_view)
5773 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5776 gtk_combo_box_relayout (combo_box);
5778 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5782 * gtk_combo_box_set_button_sensitivity:
5783 * @combo_box: a #GtkComboBox
5784 * @sensitivity: specify the sensitivity of the dropdown button
5786 * Sets whether the dropdown button of the combo box should be
5787 * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5788 * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5793 gtk_combo_box_set_button_sensitivity (GtkComboBox *combo_box,
5794 GtkSensitivityType sensitivity)
5796 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5798 if (combo_box->priv->button_sensitivity != sensitivity)
5800 combo_box->priv->button_sensitivity = sensitivity;
5801 gtk_combo_box_update_sensitivity (combo_box);
5803 g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5808 * gtk_combo_box_get_button_sensitivity:
5809 * @combo_box: a #GtkComboBox
5811 * Returns whether the combo box sets the dropdown button
5812 * sensitive or not when there are no items in the model.
5814 * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5815 * is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5816 * if the button is always insensitive or
5817 * %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5818 * the model has one item to be selected.
5823 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5825 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5827 return combo_box->priv->button_sensitivity;
5832 * gtk_combo_box_set_focus_on_click:
5833 * @combo: a #GtkComboBox
5834 * @focus_on_click: whether the combo box grabs focus when clicked
5837 * Sets whether the combo box will grab focus when it is clicked with
5838 * the mouse. Making mouse clicks not grab focus is useful in places
5839 * like toolbars where you don't want the keyboard focus removed from
5840 * the main area of the application.
5845 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5846 gboolean focus_on_click)
5848 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5850 focus_on_click = focus_on_click != FALSE;
5852 if (combo_box->priv->focus_on_click != focus_on_click)
5854 combo_box->priv->focus_on_click = focus_on_click;
5856 if (combo_box->priv->button)
5857 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5860 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5865 * gtk_combo_box_get_focus_on_click:
5866 * @combo: a #GtkComboBox
5868 * Returns whether the combo box grabs focus when it is clicked
5869 * with the mouse. See gtk_combo_box_set_focus_on_click().
5871 * Return value: %TRUE if the combo box grabs focus when it is
5872 * clicked with the mouse.
5877 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5879 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5881 return combo_box->priv->focus_on_click;
5886 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5887 GtkBuilder *builder,
5889 const gchar *tagname,
5890 GMarkupParser *parser,
5893 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5894 tagname, parser, data))
5897 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5898 tagname, parser, data);
5902 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5903 GtkBuilder *builder,
5905 const gchar *tagname,
5908 if (strcmp (tagname, "attributes") == 0)
5909 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5912 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5916 #define __GTK_COMBO_BOX_C__
5917 #include "gtkaliasdef.c"