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"
42 #include "gtkmarshal.h"
44 #include <gdk/gdkkeysyms.h>
46 #include <gobject/gvaluecollector.h>
51 #include "gtkmarshalers.h"
54 #include "gtktreeprivate.h"
57 /* WELCOME, to THE house of evil code */
59 typedef struct _ComboCellInfo ComboCellInfo;
62 GtkCellRenderer *cell;
65 GtkCellLayoutDataFunc func;
67 GDestroyNotify destroy;
73 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
75 struct _GtkComboBoxPrivate
83 GtkShadowType shadow_type;
85 GtkTreeRowReference *active_row;
88 GtkTreeViewColumn *column;
91 GtkWidget *cell_view_frame;
98 GtkWidget *popup_widget;
99 GtkWidget *popup_window;
100 GtkWidget *scrolled_window;
108 guint resize_idle_id;
114 guint popup_in_progress : 1;
115 guint popup_shown : 1;
116 guint add_tearoffs : 1;
118 guint is_cell_renderer : 1;
119 guint editing_canceled : 1;
120 guint auto_scroll : 1;
121 guint focus_on_click : 1;
123 GtkTreeViewRowSeparatorFunc row_separator_func;
124 gpointer row_separator_data;
125 GtkDestroyNotify row_separator_destroy;
127 gchar *tearoff_title;
130 /* While debugging this evil code, I have learned that
131 * there are actually 4 modes to this widget, which can
132 * be characterized as follows
134 * 1) menu mode, no child added
137 * cell_view -> GtkCellView, regular child
138 * cell_view_frame -> NULL
139 * button -> GtkToggleButton set_parent to combo
140 * arrow -> GtkArrow set_parent to button
141 * separator -> GtkVSepator set_parent to button
142 * popup_widget -> GtkMenu
143 * popup_window -> NULL
144 * scrolled_window -> NULL
146 * 2) menu mode, child added
150 * cell_view_frame -> NULL
151 * button -> GtkToggleButton set_parent to combo
152 * arrow -> GtkArrow, child of button
154 * popup_widget -> GtkMenu
155 * popup_window -> NULL
156 * scrolled_window -> NULL
158 * 3) list mode, no child added
160 * tree_view -> GtkTreeView, child of scrolled_window
161 * cell_view -> GtkCellView, regular child
162 * cell_view_frame -> GtkFrame, set parent to combo
163 * button -> GtkToggleButton, set_parent to combo
164 * arrow -> GtkArrow, child of button
166 * popup_widget -> tree_view
167 * popup_window -> GtkWindow
168 * scrolled_window -> GtkScrolledWindow, child of popup_window
170 * 4) list mode, child added
172 * tree_view -> GtkTreeView, child of scrolled_window
174 * cell_view_frame -> NULL
175 * button -> GtkToggleButton, set_parent to combo
176 * arrow -> GtkArrow, child of button
178 * popup_widget -> tree_view
179 * popup_window -> GtkWindow
180 * scrolled_window -> GtkScrolledWindow, child of popup_window
196 PROP_ROW_SPAN_COLUMN,
197 PROP_COLUMN_SPAN_COLUMN,
206 static guint combo_box_signals[LAST_SIGNAL] = {0,};
208 #define BONUS_PADDING 4
209 #define SCROLL_TIME 100
213 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
214 static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface);
215 static void gtk_combo_box_dispose (GObject *object);
216 static void gtk_combo_box_finalize (GObject *object);
217 static void gtk_combo_box_destroy (GtkObject *object);
219 static void gtk_combo_box_set_property (GObject *object,
223 static void gtk_combo_box_get_property (GObject *object,
228 static void gtk_combo_box_state_changed (GtkWidget *widget,
229 GtkStateType previous);
230 static void gtk_combo_box_grab_focus (GtkWidget *widget);
231 static void gtk_combo_box_style_set (GtkWidget *widget,
233 static void gtk_combo_box_button_toggled (GtkWidget *widget,
235 static void gtk_combo_box_button_state_changed (GtkWidget *widget,
236 GtkStateType previous,
238 static void gtk_combo_box_add (GtkContainer *container,
240 static void gtk_combo_box_remove (GtkContainer *container,
243 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
244 GtkCellRenderer *cell);
246 static void gtk_combo_box_menu_show (GtkWidget *menu,
248 static void gtk_combo_box_menu_hide (GtkWidget *menu,
251 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
253 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
258 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
263 static void gtk_combo_box_menu_position (GtkMenu *menu,
269 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
271 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
273 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
275 static void gtk_combo_box_size_request (GtkWidget *widget,
276 GtkRequisition *requisition);
277 static void gtk_combo_box_size_allocate (GtkWidget *widget,
278 GtkAllocation *allocation);
279 static void gtk_combo_box_forall (GtkContainer *container,
280 gboolean include_internals,
281 GtkCallback callback,
282 gpointer callback_data);
283 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
284 GdkEventExpose *event);
285 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
286 GdkEventScroll *event);
287 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
290 static void gtk_combo_box_check_appearance (GtkComboBox *combo_box);
291 static gchar * gtk_combo_box_real_get_active_text (GtkComboBox *combo_box);
292 static void gtk_combo_box_real_move_active (GtkComboBox *combo_box,
293 GtkScrollType scroll);
294 static void gtk_combo_box_real_popup (GtkComboBox *combo_box);
295 static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box);
297 /* listening to the model */
298 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
302 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
305 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
310 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
314 static void gtk_combo_box_model_row_expanded (GtkTreeModel *model,
320 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
325 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
326 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
328 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
329 GdkEventButton *event,
331 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
334 static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget,
335 GdkEventCrossing *event,
337 static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo,
340 static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo);
341 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
342 GdkEventButton *event,
345 static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection,
348 gboolean path_currently_selected,
351 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
355 static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box);
358 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
359 gboolean add_children);
360 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
361 static void gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
364 static void gtk_combo_box_update_title (GtkComboBox *combo_box);
365 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
367 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
371 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
373 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
374 GdkEventButton *event,
376 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
378 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
382 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
385 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
390 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
394 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
397 static void gtk_combo_box_menu_popup (GtkComboBox *combo_box,
399 guint32 activate_time);
400 static GtkWidget *gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
405 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
406 GtkCellRenderer *cell,
408 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
409 GtkCellRenderer *cell,
411 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
412 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
413 GtkCellRenderer *cell,
414 const gchar *attribute,
416 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
417 GtkCellRenderer *cell,
418 GtkCellLayoutDataFunc func,
420 GDestroyNotify destroy);
421 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
422 GtkCellRenderer *cell);
423 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
424 GtkCellRenderer *cell,
426 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
427 gboolean group_cycling);
429 static void gtk_combo_box_sync_cells (GtkComboBox *combo_box,
430 GtkCellLayout *cell_layout);
431 static void combo_cell_data_func (GtkCellLayout *cell_layout,
432 GtkCellRenderer *cell,
433 GtkTreeModel *tree_model,
436 static void gtk_combo_box_child_show (GtkWidget *widget,
437 GtkComboBox *combo_box);
438 static void gtk_combo_box_child_hide (GtkWidget *widget,
439 GtkComboBox *combo_box);
441 /* GtkBuildable method implementation */
442 static GtkBuildableIface *parent_buildable_iface;
444 static void gtk_combo_box_buildable_init (GtkBuildableIface *iface);
445 static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
448 const gchar *tagname,
449 GMarkupParser *parser,
451 static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
454 const gchar *tagname,
457 /* GtkCellEditable method implementations */
458 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
462 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
463 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
464 gtk_combo_box_cell_layout_init)
465 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
466 gtk_combo_box_cell_editable_init)
467 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
468 gtk_combo_box_buildable_init))
473 gtk_combo_box_class_init (GtkComboBoxClass *klass)
475 GObjectClass *object_class;
476 GtkObjectClass *gtk_object_class;
477 GtkContainerClass *container_class;
478 GtkWidgetClass *widget_class;
479 GtkBindingSet *binding_set;
481 klass->get_active_text = gtk_combo_box_real_get_active_text;
483 container_class = (GtkContainerClass *)klass;
484 container_class->forall = gtk_combo_box_forall;
485 container_class->add = gtk_combo_box_add;
486 container_class->remove = gtk_combo_box_remove;
488 widget_class = (GtkWidgetClass *)klass;
489 widget_class->size_allocate = gtk_combo_box_size_allocate;
490 widget_class->size_request = gtk_combo_box_size_request;
491 widget_class->expose_event = gtk_combo_box_expose_event;
492 widget_class->scroll_event = gtk_combo_box_scroll_event;
493 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
494 widget_class->grab_focus = gtk_combo_box_grab_focus;
495 widget_class->style_set = gtk_combo_box_style_set;
496 widget_class->state_changed = gtk_combo_box_state_changed;
498 gtk_object_class = (GtkObjectClass *)klass;
499 gtk_object_class->destroy = gtk_combo_box_destroy;
501 object_class = (GObjectClass *)klass;
502 object_class->dispose = gtk_combo_box_dispose;
503 object_class->finalize = gtk_combo_box_finalize;
504 object_class->set_property = gtk_combo_box_set_property;
505 object_class->get_property = gtk_combo_box_get_property;
509 * GtkComboBox::changed:
510 * @widget: the object which received the signal
512 * The changed signal is emitted when the active
513 * item is changed. The can be due to the user selecting
514 * a different item from the list, or due to a
515 * call to gtk_combo_box_set_active_iter().
516 * It will also be emitted while typing into a GtkComboBoxEntry,
517 * as well as when selecting an item from the GtkComboBoxEntry's list.
521 combo_box_signals[CHANGED] =
522 g_signal_new (I_("changed"),
523 G_OBJECT_CLASS_TYPE (klass),
525 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
527 g_cclosure_marshal_VOID__VOID,
530 combo_box_signals[MOVE_ACTIVE] =
531 _gtk_binding_signal_new (I_("move-active"),
532 G_OBJECT_CLASS_TYPE (klass),
533 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
534 G_CALLBACK (gtk_combo_box_real_move_active),
536 g_cclosure_marshal_VOID__ENUM,
538 GTK_TYPE_SCROLL_TYPE);
540 combo_box_signals[POPUP] =
541 _gtk_binding_signal_new (I_("popup"),
542 G_OBJECT_CLASS_TYPE (klass),
543 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
544 G_CALLBACK (gtk_combo_box_real_popup),
546 g_cclosure_marshal_VOID__VOID,
549 combo_box_signals[POPDOWN] =
550 _gtk_binding_signal_new (I_("popdown"),
551 G_OBJECT_CLASS_TYPE (klass),
552 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
553 G_CALLBACK (gtk_combo_box_real_popdown),
555 gtk_marshal_BOOLEAN__VOID,
559 binding_set = gtk_binding_set_by_class (widget_class);
561 gtk_binding_entry_add_signal (binding_set, GDK_Down, GDK_MOD1_MASK,
563 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, GDK_MOD1_MASK,
566 gtk_binding_entry_add_signal (binding_set, GDK_Up, GDK_MOD1_MASK,
568 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, GDK_MOD1_MASK,
570 gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
573 gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
575 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
576 gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
578 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
579 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, 0,
581 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
582 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, 0,
584 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
585 gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
587 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
588 gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
590 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
592 gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
594 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
595 gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
597 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
598 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, 0,
600 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
601 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, 0,
603 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
604 gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
606 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
607 gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
609 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
615 * The model from which the combo box takes the values shown
620 g_object_class_install_property (object_class,
622 g_param_spec_object ("model",
623 P_("ComboBox model"),
624 P_("The model for the combo box"),
626 GTK_PARAM_READWRITE));
629 * GtkComboBox:wrap-width:
631 * If wrap-width is set to a positive value, the list will be
632 * displayed in multiple columns, the number of columns is
633 * determined by wrap-width.
637 g_object_class_install_property (object_class,
639 g_param_spec_int ("wrap-width",
641 P_("Wrap width for laying out the items in a grid"),
645 GTK_PARAM_READWRITE));
649 * GtkComboBox:row-span-column:
651 * If this is set to a non-negative value, it must be the index of a column
652 * of type %G_TYPE_INT in the model.
654 * The values of that column are used to determine how many rows a value in
655 * the list will span. Therefore, the values in the model column pointed to
656 * by this property must be greater than zero and not larger than wrap-width.
660 g_object_class_install_property (object_class,
661 PROP_ROW_SPAN_COLUMN,
662 g_param_spec_int ("row-span-column",
663 P_("Row span column"),
664 P_("TreeModel column containing the row span values"),
668 GTK_PARAM_READWRITE));
672 * GtkComboBox:column-span-column:
674 * If this is set to a non-negative value, it must be the index of a column
675 * of type %G_TYPE_INT in the model.
677 * The values of that column are used to determine how many columns a value
678 * in the list will span.
682 g_object_class_install_property (object_class,
683 PROP_COLUMN_SPAN_COLUMN,
684 g_param_spec_int ("column-span-column",
685 P_("Column span column"),
686 P_("TreeModel column containing the column span values"),
690 GTK_PARAM_READWRITE));
694 * GtkComboBox:active:
696 * The item which is currently active. If the model is a non-flat treemodel,
697 * and the active item is not an immediate child of the root of the tree,
698 * this property has the value
699 * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
700 * where <literal>path</literal> is the #GtkTreePath of the active item.
704 g_object_class_install_property (object_class,
706 g_param_spec_int ("active",
708 P_("The item which is currently active"),
712 GTK_PARAM_READWRITE));
715 * GtkComboBox:add-tearoffs:
717 * The add-tearoffs property controls whether generated menus
718 * have tearoff menu items.
720 * Note that this only affects menu style combo boxes.
724 g_object_class_install_property (object_class,
726 g_param_spec_boolean ("add-tearoffs",
727 P_("Add tearoffs to menus"),
728 P_("Whether dropdowns should have a tearoff menu item"),
730 GTK_PARAM_READWRITE));
733 * GtkComboBox:has-frame:
735 * The has-frame property controls whether a frame
736 * is drawn around the entry.
740 g_object_class_install_property (object_class,
742 g_param_spec_boolean ("has-frame",
744 P_("Whether the combo box draws a frame around the child"),
746 GTK_PARAM_READWRITE));
748 g_object_class_install_property (object_class,
750 g_param_spec_boolean ("focus-on-click",
751 P_("Focus on click"),
752 P_("Whether the combo box grabs focus when it is clicked with the mouse"),
754 GTK_PARAM_READWRITE));
757 * GtkComboBox:tearoff-title:
759 * A title that may be displayed by the window manager
760 * when the popup is torn-off.
764 g_object_class_install_property (object_class,
766 g_param_spec_string ("tearoff-title",
768 P_("A title that may be displayed by the window manager when the popup is torn-off"),
770 GTK_PARAM_READWRITE));
774 * GtkComboBox:popup-shown:
776 * Whether the combo boxes dropdown is popped up.
777 * Note that this property is mainly useful, because
778 * it allows you to connect to notify::popup-shown.
782 g_object_class_install_property (object_class,
784 g_param_spec_boolean ("popup-shown",
786 P_("Whether the combo's dropdown is shown"),
788 GTK_PARAM_READABLE));
790 gtk_widget_class_install_style_property (widget_class,
791 g_param_spec_boolean ("appears-as-list",
792 P_("Appears as list"),
793 P_("Whether dropdowns should look like lists rather than menus"),
795 GTK_PARAM_READABLE));
798 * GtkComboBox:arrow-size:
800 * Sets the minimum size of the arrow in the combo box. Note
801 * that the arrow size is coupled to the font size, so in case
802 * a larger font is used, the arrow will be larger than set
807 gtk_widget_class_install_style_property (widget_class,
808 g_param_spec_int ("arrow-size",
810 P_("The minimum size of the arrow in the combo box"),
814 GTK_PARAM_READABLE));
817 * GtkComboBox:shadow-type:
819 * Which kind of shadow to draw around the combo box.
823 gtk_widget_class_install_style_property (widget_class,
824 g_param_spec_enum ("shadow-type",
826 P_("Which kind of shadow to draw around the combo box"),
827 GTK_TYPE_SHADOW_TYPE,
829 GTK_PARAM_READABLE));
831 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
835 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
837 parent_buildable_iface = g_type_interface_peek_parent (iface);
838 iface->add = _gtk_cell_layout_buildable_add;
839 iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
840 iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
844 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
846 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
847 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
848 iface->clear = gtk_combo_box_cell_layout_clear;
849 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
850 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
851 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
852 iface->reorder = gtk_combo_box_cell_layout_reorder;
856 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
858 iface->start_editing = gtk_combo_box_start_editing;
862 gtk_combo_box_init (GtkComboBox *combo_box)
864 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
866 combo_box->priv->cell_view = gtk_cell_view_new ();
867 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
868 GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
869 gtk_widget_show (combo_box->priv->cell_view);
871 combo_box->priv->width = 0;
872 combo_box->priv->height = 0;
873 combo_box->priv->wrap_width = 0;
875 combo_box->priv->active_row = NULL;
876 combo_box->priv->col_column = -1;
877 combo_box->priv->row_column = -1;
879 combo_box->priv->popup_shown = FALSE;
880 combo_box->priv->add_tearoffs = FALSE;
881 combo_box->priv->has_frame = TRUE;
882 combo_box->priv->is_cell_renderer = FALSE;
883 combo_box->priv->editing_canceled = FALSE;
884 combo_box->priv->auto_scroll = FALSE;
885 combo_box->priv->focus_on_click = TRUE;
887 gtk_combo_box_check_appearance (combo_box);
891 gtk_combo_box_set_property (GObject *object,
896 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
901 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
904 case PROP_WRAP_WIDTH:
905 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
908 case PROP_ROW_SPAN_COLUMN:
909 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
912 case PROP_COLUMN_SPAN_COLUMN:
913 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
917 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
920 case PROP_ADD_TEAROFFS:
921 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
925 combo_box->priv->has_frame = g_value_get_boolean (value);
928 case PROP_FOCUS_ON_CLICK:
929 gtk_combo_box_set_focus_on_click (combo_box,
930 g_value_get_boolean (value));
933 case PROP_TEAROFF_TITLE:
934 gtk_combo_box_set_title (combo_box, g_value_get_string (value));
937 case PROP_POPUP_SHOWN:
938 if (g_value_get_boolean (value))
940 gtk_combo_box_popup (combo_box);
944 gtk_combo_box_popdown (combo_box);
954 gtk_combo_box_get_property (GObject *object,
959 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
964 g_value_set_object (value, combo_box->priv->model);
967 case PROP_WRAP_WIDTH:
968 g_value_set_int (value, combo_box->priv->wrap_width);
971 case PROP_ROW_SPAN_COLUMN:
972 g_value_set_int (value, combo_box->priv->row_column);
975 case PROP_COLUMN_SPAN_COLUMN:
976 g_value_set_int (value, combo_box->priv->col_column);
980 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
983 case PROP_ADD_TEAROFFS:
984 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
988 g_value_set_boolean (value, combo_box->priv->has_frame);
991 case PROP_FOCUS_ON_CLICK:
992 g_value_set_boolean (value, combo_box->priv->focus_on_click);
995 case PROP_TEAROFF_TITLE:
996 g_value_set_string (value, gtk_combo_box_get_title (combo_box));
999 case PROP_POPUP_SHOWN:
1000 g_value_set_boolean (value, combo_box->priv->popup_shown);
1004 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1010 gtk_combo_box_state_changed (GtkWidget *widget,
1011 GtkStateType previous)
1013 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1015 if (GTK_WIDGET_REALIZED (widget))
1017 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
1018 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
1019 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1022 gtk_widget_queue_draw (widget);
1026 gtk_combo_box_button_state_changed (GtkWidget *widget,
1027 GtkStateType previous,
1030 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1032 if (GTK_WIDGET_REALIZED (widget))
1034 if (!combo_box->priv->tree_view && combo_box->priv->cell_view)
1036 if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
1037 (GTK_WIDGET_STATE (combo_box->priv->cell_view) == GTK_STATE_INSENSITIVE))
1038 gtk_widget_set_sensitive (combo_box->priv->cell_view, GTK_WIDGET_SENSITIVE (widget));
1040 gtk_widget_set_state (combo_box->priv->cell_view,
1041 GTK_WIDGET_STATE (widget));
1046 gtk_widget_queue_draw (widget);
1050 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1052 gboolean appears_as_list;
1054 /* if wrap_width > 0, then we are in grid-mode and forced to use
1057 if (combo_box->priv->wrap_width)
1058 appears_as_list = FALSE;
1060 gtk_widget_style_get (GTK_WIDGET (combo_box),
1061 "appears-as-list", &appears_as_list,
1064 if (appears_as_list)
1066 /* Destroy all the menu mode widgets, if they exist. */
1067 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1068 gtk_combo_box_menu_destroy (combo_box);
1070 /* Create the list mode widgets, if they don't already exist. */
1071 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
1072 gtk_combo_box_list_setup (combo_box);
1076 /* Destroy all the list mode widgets, if they exist. */
1077 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
1078 gtk_combo_box_list_destroy (combo_box);
1080 /* Create the menu mode widgets, if they don't already exist. */
1081 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
1082 gtk_combo_box_menu_setup (combo_box, TRUE);
1085 gtk_widget_style_get (GTK_WIDGET (combo_box),
1086 "shadow-type", &combo_box->priv->shadow_type,
1091 gtk_combo_box_style_set (GtkWidget *widget,
1094 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1096 gtk_combo_box_check_appearance (combo_box);
1098 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
1099 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
1100 &widget->style->base[GTK_WIDGET_STATE (widget)]);
1102 if (GTK_IS_ENTRY (GTK_BIN (combo_box)->child))
1103 g_object_set (GTK_BIN (combo_box)->child, "shadow-type",
1104 GTK_SHADOW_NONE == combo_box->priv->shadow_type ?
1105 GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1109 gtk_combo_box_button_toggled (GtkWidget *widget,
1112 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1114 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1116 if (!combo_box->priv->popup_in_progress)
1117 gtk_combo_box_popup (combo_box);
1120 gtk_combo_box_popdown (combo_box);
1124 gtk_combo_box_add (GtkContainer *container,
1127 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1129 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
1131 gtk_widget_unparent (combo_box->priv->cell_view);
1132 GTK_BIN (container)->child = NULL;
1133 gtk_widget_queue_resize (GTK_WIDGET (container));
1136 gtk_widget_set_parent (widget, GTK_WIDGET (container));
1137 GTK_BIN (container)->child = widget;
1139 if (combo_box->priv->cell_view &&
1140 widget != combo_box->priv->cell_view)
1142 /* since the cell_view was unparented, it's gone now */
1143 combo_box->priv->cell_view = NULL;
1145 if (!combo_box->priv->tree_view && combo_box->priv->separator)
1147 gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
1148 combo_box->priv->separator);
1149 combo_box->priv->separator = NULL;
1151 gtk_widget_queue_resize (GTK_WIDGET (container));
1153 else if (combo_box->priv->cell_view_frame)
1155 gtk_widget_unparent (combo_box->priv->cell_view_frame);
1156 combo_box->priv->cell_view_frame = NULL;
1157 combo_box->priv->box = NULL;
1163 gtk_combo_box_remove (GtkContainer *container,
1166 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1168 gboolean appears_as_list;
1170 if (widget == combo_box->priv->cell_view)
1171 combo_box->priv->cell_view = NULL;
1173 gtk_widget_unparent (widget);
1174 GTK_BIN (container)->child = NULL;
1176 if (GTK_OBJECT_FLAGS (combo_box) & GTK_IN_DESTRUCTION)
1179 gtk_widget_queue_resize (GTK_WIDGET (container));
1181 if (!combo_box->priv->tree_view)
1182 appears_as_list = FALSE;
1184 appears_as_list = TRUE;
1186 if (appears_as_list)
1187 gtk_combo_box_list_destroy (combo_box);
1188 else if (GTK_IS_MENU (combo_box->priv->popup_widget))
1190 gtk_combo_box_menu_destroy (combo_box);
1191 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
1192 combo_box->priv->popup_widget = NULL;
1195 if (!combo_box->priv->cell_view)
1197 combo_box->priv->cell_view = gtk_cell_view_new ();
1198 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
1199 GTK_BIN (container)->child = combo_box->priv->cell_view;
1201 gtk_widget_show (combo_box->priv->cell_view);
1202 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
1203 combo_box->priv->model);
1204 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (combo_box->priv->cell_view));
1208 if (appears_as_list)
1209 gtk_combo_box_list_setup (combo_box);
1211 gtk_combo_box_menu_setup (combo_box, TRUE);
1213 if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1215 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1216 gtk_combo_box_set_active_internal (combo_box, path);
1217 gtk_tree_path_free (path);
1220 gtk_combo_box_set_active_internal (combo_box, NULL);
1223 static ComboCellInfo *
1224 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
1225 GtkCellRenderer *cell)
1229 for (i = combo_box->priv->cells; i; i = i->next)
1231 ComboCellInfo *info = (ComboCellInfo *)i->data;
1233 if (info && info->cell == cell)
1241 gtk_combo_box_menu_show (GtkWidget *menu,
1244 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1246 gtk_combo_box_child_show (menu, user_data);
1248 combo_box->priv->popup_in_progress = TRUE;
1249 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1251 combo_box->priv->popup_in_progress = FALSE;
1255 gtk_combo_box_menu_hide (GtkWidget *menu,
1258 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1260 gtk_combo_box_child_hide(menu,user_data);
1262 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1267 gtk_combo_box_detacher (GtkWidget *widget,
1270 GtkComboBox *combo_box;
1272 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
1274 combo_box = GTK_COMBO_BOX (widget);
1275 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
1277 g_signal_handlers_disconnect_by_func (menu->toplevel,
1278 gtk_combo_box_menu_show,
1280 g_signal_handlers_disconnect_by_func (menu->toplevel,
1281 gtk_combo_box_menu_hide,
1284 combo_box->priv->popup_widget = NULL;
1288 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1291 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1293 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
1294 combo_box->priv->popup_widget = NULL;
1296 else if (combo_box->priv->popup_widget)
1298 gtk_container_remove (GTK_CONTAINER (combo_box->priv->scrolled_window),
1299 combo_box->priv->popup_widget);
1300 g_object_unref (combo_box->priv->popup_widget);
1301 combo_box->priv->popup_widget = NULL;
1304 if (GTK_IS_MENU (popup))
1306 if (combo_box->priv->popup_window)
1308 gtk_widget_destroy (combo_box->priv->popup_window);
1309 combo_box->priv->popup_window = NULL;
1312 combo_box->priv->popup_widget = popup;
1315 * Note that we connect to show/hide on the toplevel, not the
1316 * menu itself, since the menu is not shown/hidden when it is
1317 * popped up while torn-off.
1319 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1320 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1321 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1322 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1324 gtk_menu_attach_to_widget (GTK_MENU (popup),
1325 GTK_WIDGET (combo_box),
1326 gtk_combo_box_detacher);
1330 if (!combo_box->priv->popup_window)
1332 GtkWidget *toplevel;
1334 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1335 gtk_widget_set_name (combo_box->priv->popup_window, "gtk-combobox-popup-window");
1337 gtk_window_set_type_hint (GTK_WINDOW (combo_box->priv->popup_window),
1338 GDK_WINDOW_TYPE_HINT_COMBO);
1340 g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"show",
1341 G_CALLBACK (gtk_combo_box_child_show),
1343 g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"hide",
1344 G_CALLBACK (gtk_combo_box_child_hide),
1347 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1348 if (GTK_IS_WINDOW (toplevel))
1350 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1351 GTK_WINDOW (combo_box->priv->popup_window));
1352 gtk_window_set_transient_for (GTK_WINDOW (combo_box->priv->popup_window),
1353 GTK_WINDOW (toplevel));
1356 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1357 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
1358 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1360 combo_box->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1362 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1365 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1368 gtk_widget_show (combo_box->priv->scrolled_window);
1370 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
1371 combo_box->priv->scrolled_window);
1374 gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1377 gtk_widget_show (popup);
1378 g_object_ref (popup);
1379 combo_box->priv->popup_widget = popup;
1384 gtk_combo_box_menu_position_below (GtkMenu *menu,
1390 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1396 GdkRectangle monitor;
1398 /* FIXME: is using the size request here broken? */
1399 child = GTK_BIN (combo_box)->child;
1401 gdk_window_get_origin (child->window, &sx, &sy);
1403 if (GTK_WIDGET_NO_WINDOW (child))
1405 sx += child->allocation.x;
1406 sy += child->allocation.y;
1409 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
1410 sx -= GTK_WIDGET (combo_box)->style->xthickness;
1412 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1414 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1417 *x = sx + child->allocation.width - req.width;
1420 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1421 monitor_num = gdk_screen_get_monitor_at_window (screen,
1422 GTK_WIDGET (combo_box)->window);
1423 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1427 else if (*x + req.width > monitor.x + monitor.width)
1428 *x = monitor.x + monitor.width - req.width;
1430 if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1431 *y += child->allocation.height;
1432 else if (*y - monitor.y >= req.height)
1434 else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y)
1435 *y += child->allocation.height;
1443 gtk_combo_box_menu_position_over (GtkMenu *menu,
1449 GtkComboBox *combo_box;
1453 GtkRequisition requisition;
1460 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
1462 combo_box = GTK_COMBO_BOX (user_data);
1463 widget = GTK_WIDGET (combo_box);
1465 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1466 menu_width = requisition.width;
1468 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1469 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1471 menu_xpos += widget->allocation.x;
1472 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1476 gtk_widget_get_child_requisition (active, &requisition);
1477 menu_ypos -= requisition.height / 2;
1480 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1483 child = children->data;
1485 if (active == child)
1488 if (GTK_WIDGET_VISIBLE (child))
1490 gtk_widget_get_child_requisition (child, &requisition);
1491 menu_ypos -= requisition.height;
1494 children = children->next;
1497 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1498 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1500 /* Clamp the position on screen */
1501 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1505 else if ((menu_xpos + menu_width) > screen_width)
1506 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1515 gtk_combo_box_menu_position (GtkMenu *menu,
1521 GtkComboBox *combo_box;
1522 GtkWidget *menu_item;
1524 combo_box = GTK_COMBO_BOX (user_data);
1526 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
1527 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1530 /* FIXME handle nested menus better */
1531 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1533 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
1536 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1542 gtk_combo_box_list_position (GtkComboBox *combo_box,
1550 GdkRectangle monitor;
1551 GtkRequisition popup_req;
1552 GtkPolicyType hpolicy, vpolicy;
1554 /* under windows, the drop down list is as wide as the combo box itself.
1556 GtkWidget *sample = GTK_WIDGET (combo_box);
1558 gdk_window_get_origin (sample->window, x, y);
1560 if (GTK_WIDGET_NO_WINDOW (sample))
1562 *x += sample->allocation.x;
1563 *y += sample->allocation.y;
1566 *width = sample->allocation.width;
1568 hpolicy = vpolicy = GTK_POLICY_NEVER;
1569 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1571 gtk_widget_size_request (combo_box->priv->scrolled_window, &popup_req);
1573 if (popup_req.width > *width)
1575 hpolicy = GTK_POLICY_ALWAYS;
1576 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1578 gtk_widget_size_request (combo_box->priv->scrolled_window, &popup_req);
1581 *height = popup_req.height;
1583 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1584 monitor_num = gdk_screen_get_monitor_at_window (screen,
1585 GTK_WIDGET (combo_box)->window);
1586 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1590 else if (*x + *width > monitor.x + monitor.width)
1591 *x = monitor.x + monitor.width - *width;
1593 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1594 *y += sample->allocation.height;
1595 else if (*y - *height >= monitor.y)
1597 else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1599 *y += sample->allocation.height;
1600 *height = monitor.y + monitor.height - *y;
1604 *height = *y - monitor.y;
1608 if (popup_req.height > *height)
1610 vpolicy = GTK_POLICY_ALWAYS;
1612 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1618 cell_view_is_sensitive (GtkCellView *cell_view)
1620 GList *cells, *list;
1623 cells = gtk_cell_view_get_cell_renderers (cell_view);
1629 g_object_get (list->data, "sensitive", &sensitive, NULL);
1636 g_list_free (cells);
1642 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1645 GList *cells, *list;
1648 if (!combo_box->priv->column)
1651 if (combo_box->priv->row_separator_func)
1653 if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
1654 combo_box->priv->row_separator_data))
1658 gtk_tree_view_column_cell_set_cell_data (combo_box->priv->column,
1659 combo_box->priv->model,
1660 iter, FALSE, FALSE);
1662 cells = gtk_tree_view_column_get_cell_renderers (combo_box->priv->column);
1668 g_object_get (list->data, "sensitive", &sensitive, NULL);
1675 g_list_free (cells);
1681 update_menu_sensitivity (GtkComboBox *combo_box,
1684 GList *children, *child;
1685 GtkWidget *item, *submenu, *separator;
1686 GtkWidget *cell_view;
1689 if (!combo_box->priv->model)
1692 children = gtk_container_get_children (GTK_CONTAINER (menu));
1694 for (child = children; child; child = child->next)
1696 item = GTK_WIDGET (child->data);
1697 cell_view = GTK_BIN (item)->child;
1699 if (!GTK_IS_CELL_VIEW (cell_view))
1702 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1703 if (submenu != NULL)
1705 gtk_widget_set_sensitive (item, TRUE);
1706 update_menu_sensitivity (combo_box, submenu);
1710 sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1712 if (menu != combo_box->priv->popup_widget && child == children)
1714 separator = GTK_WIDGET (child->next->data);
1715 g_object_set (item, "visible", sensitive, NULL);
1716 g_object_set (separator, "visible", sensitive, NULL);
1719 gtk_widget_set_sensitive (item, sensitive);
1723 g_list_free (children);
1727 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1729 guint32 activate_time)
1733 GtkRequisition requisition;
1736 update_menu_sensitivity (combo_box, combo_box->priv->popup_widget);
1739 if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1741 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1742 active_item = gtk_tree_path_get_indices (path)[0];
1743 gtk_tree_path_free (path);
1745 if (combo_box->priv->add_tearoffs)
1749 /* FIXME handle nested menus better */
1750 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), active_item);
1752 if (combo_box->priv->wrap_width == 0)
1754 width = GTK_WIDGET (combo_box)->allocation.width;
1755 gtk_widget_set_size_request (combo_box->priv->popup_widget, -1, -1);
1756 gtk_widget_size_request (combo_box->priv->popup_widget, &requisition);
1758 gtk_widget_set_size_request (combo_box->priv->popup_widget,
1759 MAX (width, requisition.width), -1);
1762 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1764 gtk_combo_box_menu_position, combo_box,
1765 button, activate_time);
1769 popup_grab_on_window (GdkWindow *window,
1770 guint32 activate_time,
1771 gboolean grab_keyboard)
1773 if ((gdk_pointer_grab (window, TRUE,
1774 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1775 GDK_POINTER_MOTION_MASK,
1776 NULL, NULL, activate_time) == 0))
1778 if (!grab_keyboard ||
1779 gdk_keyboard_grab (window, TRUE,
1780 activate_time) == 0)
1784 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1794 * gtk_combo_box_popup:
1795 * @combo_box: a #GtkComboBox
1797 * Pops up the menu or dropdown list of @combo_box.
1799 * This function is mostly intended for use by accessibility technologies;
1800 * applications should have little use for it.
1805 gtk_combo_box_popup (GtkComboBox *combo_box)
1807 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1809 g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1813 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1815 gint x, y, width, height;
1816 GtkTreePath *path = NULL, *ppath;
1817 GtkWidget *toplevel;
1819 if (!GTK_WIDGET_REALIZED (combo_box))
1822 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1825 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1827 gtk_combo_box_menu_popup (combo_box, 0, 0);
1831 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1832 if (GTK_IS_WINDOW (toplevel))
1833 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1834 GTK_WINDOW (combo_box->priv->popup_window));
1836 gtk_widget_show_all (combo_box->priv->scrolled_window);
1837 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1839 gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);
1840 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1842 if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1844 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1845 ppath = gtk_tree_path_copy (path);
1846 if (gtk_tree_path_up (ppath))
1847 gtk_tree_view_expand_to_path (GTK_TREE_VIEW (combo_box->priv->tree_view),
1849 gtk_tree_path_free (ppath);
1851 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view),
1855 gtk_widget_show (combo_box->priv->popup_window);
1859 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1861 gtk_tree_path_free (path);
1864 gtk_widget_grab_focus (combo_box->priv->popup_window);
1865 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1868 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1869 gtk_widget_grab_focus (combo_box->priv->tree_view);
1871 if (!popup_grab_on_window (combo_box->priv->popup_window->window,
1872 GDK_CURRENT_TIME, TRUE))
1874 gtk_widget_hide (combo_box->priv->popup_window);
1878 gtk_grab_add (combo_box->priv->popup_window);
1882 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
1884 if (combo_box->priv->popup_shown)
1886 gtk_combo_box_popdown (combo_box);
1894 * gtk_combo_box_popdown:
1895 * @combo_box: a #GtkComboBox
1897 * Hides the menu or dropdown list of @combo_box.
1899 * This function is mostly intended for use by accessibility technologies;
1900 * applications should have little use for it.
1905 gtk_combo_box_popdown (GtkComboBox *combo_box)
1907 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1909 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1911 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1915 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1918 gtk_grab_remove (combo_box->priv->popup_window);
1919 gtk_widget_hide_all (combo_box->priv->popup_window);
1920 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1925 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1931 if (combo_box->priv->cell_view)
1932 gtk_widget_style_get (combo_box->priv->cell_view,
1933 "focus-line-width", &padding,
1938 /* add some pixels for good measure */
1939 padding += BONUS_PADDING;
1941 if (combo_box->priv->cell_view)
1942 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1947 return req.width + padding;
1951 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1956 if (!combo_box->priv->model ||
1957 !gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1960 combo_box->priv->width = 0;
1961 combo_box->priv->height = 0;
1963 path = gtk_tree_path_new_from_indices (0, -1);
1969 if (combo_box->priv->cell_view)
1970 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1978 combo_box->priv->width = MAX (combo_box->priv->width, req.width);
1979 combo_box->priv->height = MAX (combo_box->priv->height, req.height);
1981 gtk_tree_path_next (path);
1983 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1985 gtk_tree_path_free (path);
1989 gtk_combo_box_size_request (GtkWidget *widget,
1990 GtkRequisition *requisition)
1993 gint focus_width, focus_pad;
1996 GtkRequisition bin_req;
1997 PangoContext *context;
1998 PangoFontMetrics *metrics;
1999 PangoFontDescription *font_desc;
2001 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2004 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
2005 gtk_combo_box_remeasure (combo_box);
2006 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
2007 bin_req.height = MAX (bin_req.height, combo_box->priv->height);
2009 gtk_widget_style_get (GTK_WIDGET (widget),
2010 "focus-line-width", &focus_width,
2011 "focus-padding", &focus_pad,
2012 "arrow-size", &arrow_size,
2015 font_desc = GTK_BIN (widget)->child->style->font_desc;
2016 context = gtk_widget_get_pango_context (widget);
2017 metrics = pango_context_get_metrics (context, font_desc,
2018 pango_context_get_language (context));
2019 font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
2020 pango_font_metrics_get_descent (metrics));
2021 pango_font_metrics_unref (metrics);
2023 arrow_size = MAX (arrow_size, font_size);
2025 gtk_widget_set_size_request (combo_box->priv->arrow, arrow_size, arrow_size);
2027 if (!combo_box->priv->tree_view)
2031 if (combo_box->priv->cell_view)
2033 GtkRequisition button_req, sep_req, arrow_req;
2034 gint border_width, xthickness, ythickness;
2036 gtk_widget_size_request (combo_box->priv->button, &button_req);
2037 border_width = GTK_CONTAINER (combo_box)->border_width;
2038 xthickness = combo_box->priv->button->style->xthickness;
2039 ythickness = combo_box->priv->button->style->ythickness;
2041 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
2042 bin_req.height = MAX (bin_req.height, combo_box->priv->height);
2044 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
2045 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
2047 height = MAX (sep_req.height, arrow_req.height);
2048 height = MAX (height, bin_req.height);
2050 width = bin_req.width + sep_req.width + arrow_req.width;
2052 height += 2*(border_width + ythickness + focus_width + focus_pad);
2053 width += 2*(border_width + xthickness + focus_width + focus_pad);
2055 requisition->width = width;
2056 requisition->height = height;
2060 GtkRequisition but_req;
2062 gtk_widget_size_request (combo_box->priv->button, &but_req);
2064 requisition->width = bin_req.width + but_req.width;
2065 requisition->height = MAX (bin_req.height, but_req.height);
2071 GtkRequisition button_req, frame_req;
2073 /* sample + frame */
2074 *requisition = bin_req;
2076 requisition->width += 2 * focus_width;
2078 if (combo_box->priv->cell_view_frame)
2080 gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
2081 if (combo_box->priv->has_frame)
2083 requisition->width += 2 *
2084 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2085 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2086 requisition->height += 2 *
2087 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2088 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2093 gtk_widget_size_request (combo_box->priv->button, &button_req);
2095 requisition->height = MAX (requisition->height, button_req.height);
2096 requisition->width += button_req.width;
2099 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
2101 requisition->height += 2 * widget->style->ythickness;
2102 requisition->width += 2 * widget->style->xthickness;
2106 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \
2107 gtk_widget_size_request (combo_box->priv->button, &req); \
2110 child.x = allocation->x + shadow_width; \
2112 child.x = allocation->x + allocation->width - req.width - shadow_width; \
2114 child.y = allocation->y + shadow_height; \
2115 child.width = req.width; \
2116 child.height = allocation->height - 2 * shadow_height; \
2117 child.width = MAX (1, child.width); \
2118 child.height = MAX (1, child.height); \
2120 gtk_widget_size_allocate (combo_box->priv->button, &child);
2123 gtk_combo_box_size_allocate (GtkWidget *widget,
2124 GtkAllocation *allocation)
2126 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2127 gint shadow_width, shadow_height;
2128 gint focus_width, focus_pad;
2129 GtkAllocation child;
2131 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2133 widget->allocation = *allocation;
2135 gtk_widget_style_get (GTK_WIDGET (widget),
2136 "focus-line-width", &focus_width,
2137 "focus-padding", &focus_pad,
2140 if (GTK_SHADOW_NONE != combo_box->priv->shadow_type)
2142 shadow_width = widget->style->xthickness;
2143 shadow_height = widget->style->ythickness;
2151 if (!combo_box->priv->tree_view)
2153 if (combo_box->priv->cell_view)
2155 gint border_width, xthickness, ythickness;
2159 allocation->x += shadow_width;
2160 allocation->y += shadow_height;
2161 allocation->width -= 2 * shadow_width;
2162 allocation->height -= 2 * shadow_height;
2164 gtk_widget_size_allocate (combo_box->priv->button, allocation);
2166 /* set some things ready */
2167 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
2168 xthickness = combo_box->priv->button->style->xthickness;
2169 ythickness = combo_box->priv->button->style->ythickness;
2171 child.x = allocation->x;
2172 child.y = allocation->y;
2173 width = allocation->width;
2174 child.height = allocation->height;
2176 if (!combo_box->priv->is_cell_renderer)
2178 child.x += border_width + xthickness + focus_width + focus_pad;
2179 child.y += border_width + ythickness + focus_width + focus_pad;
2180 width -= 2 * (child.x - allocation->x);
2181 child.height -= 2 * (child.y - allocation->y);
2185 /* handle the children */
2186 gtk_widget_size_request (combo_box->priv->arrow, &req);
2187 child.width = req.width;
2189 child.x += width - req.width;
2190 child.width = MAX (1, child.width);
2191 child.height = MAX (1, child.height);
2192 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
2194 child.x += req.width;
2195 gtk_widget_size_request (combo_box->priv->separator, &req);
2196 child.width = req.width;
2198 child.x -= req.width;
2199 child.width = MAX (1, child.width);
2200 child.height = MAX (1, child.height);
2201 gtk_widget_size_allocate (combo_box->priv->separator, &child);
2205 child.x += req.width;
2206 child.width = allocation->x + allocation->width
2207 - (border_width + xthickness + focus_width + focus_pad)
2212 child.width = child.x;
2213 child.x = allocation->x
2214 + border_width + xthickness + focus_width + focus_pad;
2215 child.width -= child.x;
2218 child.width = MAX (1, child.width);
2219 child.height = MAX (1, child.height);
2220 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2224 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2227 child.x = allocation->x + req.width + shadow_width;
2229 child.x = allocation->x + shadow_width;
2230 child.y = allocation->y + shadow_height;
2231 child.width = allocation->width - req.width - 2 * shadow_width;
2232 child.width = MAX (1, child.width);
2233 child.height = MAX (1, child.height);
2234 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2242 GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2246 child.x = allocation->x + req.width;
2248 child.x = allocation->x;
2249 child.y = allocation->y;
2250 child.width = allocation->width - req.width;
2251 child.height = allocation->height;
2253 if (combo_box->priv->cell_view_frame)
2255 child.width = MAX (1, child.width);
2256 child.height = MAX (1, child.height);
2257 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
2260 if (combo_box->priv->has_frame)
2263 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2264 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
2266 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2267 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
2268 child.width -= 2 * (
2269 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2270 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2271 child.height -= 2 * (
2272 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2273 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2277 child.width = MAX (1, child.width);
2278 child.height = MAX (1, child.height);
2279 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2283 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2286 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2288 if (combo_box->priv->model)
2290 g_signal_handler_disconnect (combo_box->priv->model,
2291 combo_box->priv->inserted_id);
2292 g_signal_handler_disconnect (combo_box->priv->model,
2293 combo_box->priv->deleted_id);
2294 g_signal_handler_disconnect (combo_box->priv->model,
2295 combo_box->priv->reordered_id);
2296 g_signal_handler_disconnect (combo_box->priv->model,
2297 combo_box->priv->changed_id);
2301 if (!combo_box->priv->tree_view)
2303 if (combo_box->priv->popup_widget)
2304 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
2305 (GtkCallback)gtk_widget_destroy, NULL);
2308 if (combo_box->priv->model)
2310 g_object_unref (combo_box->priv->model);
2311 combo_box->priv->model = NULL;
2314 if (combo_box->priv->active_row)
2316 gtk_tree_row_reference_free (combo_box->priv->active_row);
2317 combo_box->priv->active_row = NULL;
2320 if (combo_box->priv->cell_view)
2321 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2325 gtk_combo_box_forall (GtkContainer *container,
2326 gboolean include_internals,
2327 GtkCallback callback,
2328 gpointer callback_data)
2330 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2332 if (include_internals)
2334 if (combo_box->priv->button)
2335 (* callback) (combo_box->priv->button, callback_data);
2336 if (combo_box->priv->cell_view_frame)
2337 (* callback) (combo_box->priv->cell_view_frame, callback_data);
2340 if (GTK_BIN (container)->child)
2341 (* callback) (GTK_BIN (container)->child, callback_data);
2345 gtk_combo_box_child_show (GtkWidget *widget,
2346 GtkComboBox *combo_box)
2348 GtkComboBoxPrivate *priv = combo_box->priv;
2350 priv->popup_shown = TRUE;
2351 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2355 gtk_combo_box_child_hide (GtkWidget *widget,
2356 GtkComboBox *combo_box)
2358 GtkComboBoxPrivate *priv = combo_box->priv;
2360 priv->popup_shown = FALSE;
2361 g_object_notify (G_OBJECT (combo_box), "popup-shown");
2365 gtk_combo_box_expose_event (GtkWidget *widget,
2366 GdkEventExpose *event)
2368 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2370 if (GTK_WIDGET_DRAWABLE (widget) &&
2371 GTK_SHADOW_NONE != combo_box->priv->shadow_type)
2373 gtk_paint_shadow (widget->style, widget->window,
2374 GTK_STATE_NORMAL, combo_box->priv->shadow_type,
2375 NULL, widget, "combobox",
2376 widget->allocation.x, widget->allocation.y,
2377 widget->allocation.width, widget->allocation.height);
2380 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2381 combo_box->priv->button, event);
2383 if (combo_box->priv->tree_view &&
2384 combo_box->priv->cell_view_frame)
2386 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2387 combo_box->priv->cell_view_frame, event);
2390 gtk_container_propagate_expose (GTK_CONTAINER (widget),
2391 GTK_BIN (widget)->child, event);
2406 path_visible (GtkTreeView *view,
2412 /* Note that we rely on the fact that collapsed rows don't have nodes
2414 return _gtk_tree_view_find_node (view, path, &tree, &node);
2418 tree_next_func (GtkTreeModel *model,
2423 SearchData *search_data = (SearchData *)data;
2425 if (search_data->found)
2427 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2430 if (search_data->visible &&
2431 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2434 search_data->set = TRUE;
2435 search_data->iter = *iter;
2440 if (gtk_tree_path_compare (path, search_data->path) == 0)
2441 search_data->found = TRUE;
2447 tree_next (GtkComboBox *combo,
2448 GtkTreeModel *model,
2453 SearchData search_data;
2455 search_data.combo = combo;
2456 search_data.path = gtk_tree_model_get_path (model, iter);
2457 search_data.visible = visible;
2458 search_data.found = FALSE;
2459 search_data.set = FALSE;
2461 gtk_tree_model_foreach (model, tree_next_func, &search_data);
2463 *next = search_data.iter;
2465 gtk_tree_path_free (search_data.path);
2467 return search_data.set;
2471 tree_prev_func (GtkTreeModel *model,
2476 SearchData *search_data = (SearchData *)data;
2478 if (gtk_tree_path_compare (path, search_data->path) == 0)
2480 search_data->found = TRUE;
2484 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2487 if (search_data->visible &&
2488 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2491 search_data->set = TRUE;
2492 search_data->iter = *iter;
2498 tree_prev (GtkComboBox *combo,
2499 GtkTreeModel *model,
2504 SearchData search_data;
2506 search_data.combo = combo;
2507 search_data.path = gtk_tree_model_get_path (model, iter);
2508 search_data.visible = visible;
2509 search_data.found = FALSE;
2510 search_data.set = FALSE;
2512 gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2514 *prev = search_data.iter;
2516 gtk_tree_path_free (search_data.path);
2518 return search_data.set;
2522 tree_last_func (GtkTreeModel *model,
2527 SearchData *search_data = (SearchData *)data;
2529 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2532 /* Note that we rely on the fact that collapsed rows don't have nodes
2534 if (search_data->visible &&
2535 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2538 search_data->set = TRUE;
2539 search_data->iter = *iter;
2545 tree_last (GtkComboBox *combo,
2546 GtkTreeModel *model,
2550 SearchData search_data;
2552 search_data.combo = combo;
2553 search_data.visible = visible;
2554 search_data.set = FALSE;
2556 gtk_tree_model_foreach (model, tree_last_func, &search_data);
2558 *last = search_data.iter;
2560 return search_data.set;
2565 tree_first_func (GtkTreeModel *model,
2570 SearchData *search_data = (SearchData *)data;
2572 if (!tree_column_row_is_sensitive (search_data->combo, iter))
2575 if (search_data->visible &&
2576 !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2579 search_data->set = TRUE;
2580 search_data->iter = *iter;
2586 tree_first (GtkComboBox *combo,
2587 GtkTreeModel *model,
2591 SearchData search_data;
2593 search_data.combo = combo;
2594 search_data.visible = visible;
2595 search_data.set = FALSE;
2597 gtk_tree_model_foreach (model, tree_first_func, &search_data);
2599 *first = search_data.iter;
2601 return search_data.set;
2605 gtk_combo_box_scroll_event (GtkWidget *widget,
2606 GdkEventScroll *event)
2608 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2611 GtkTreeIter new_iter;
2613 if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2616 if (event->direction == GDK_SCROLL_UP)
2617 found = tree_prev (combo_box, combo_box->priv->model,
2618 &iter, &new_iter, FALSE);
2620 found = tree_next (combo_box, combo_box->priv->model,
2621 &iter, &new_iter, FALSE);
2624 gtk_combo_box_set_active_iter (combo_box, &new_iter);
2634 gtk_combo_box_sync_cells (GtkComboBox *combo_box,
2635 GtkCellLayout *cell_layout)
2639 for (k = combo_box->priv->cells; k; k = k->next)
2642 ComboCellInfo *info = (ComboCellInfo *)k->data;
2644 if (info->pack == GTK_PACK_START)
2645 gtk_cell_layout_pack_start (cell_layout,
2646 info->cell, info->expand);
2647 else if (info->pack == GTK_PACK_END)
2648 gtk_cell_layout_pack_end (cell_layout,
2649 info->cell, info->expand);
2651 gtk_cell_layout_set_cell_data_func (cell_layout,
2653 combo_cell_data_func, info, NULL);
2655 for (j = info->attributes; j; j = j->next->next)
2657 gtk_cell_layout_add_attribute (cell_layout,
2660 GPOINTER_TO_INT (j->next->data));
2666 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2667 gboolean add_children)
2671 if (combo_box->priv->cell_view)
2673 combo_box->priv->button = gtk_toggle_button_new ();
2674 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2675 combo_box->priv->focus_on_click);
2677 g_signal_connect (combo_box->priv->button, "toggled",
2678 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2679 gtk_widget_set_parent (combo_box->priv->button,
2680 GTK_BIN (combo_box)->child->parent);
2682 combo_box->priv->box = gtk_hbox_new (FALSE, 0);
2683 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2684 combo_box->priv->box);
2686 combo_box->priv->separator = gtk_vseparator_new ();
2687 gtk_container_add (GTK_CONTAINER (combo_box->priv->box),
2688 combo_box->priv->separator);
2690 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2691 gtk_container_add (GTK_CONTAINER (combo_box->priv->box),
2692 combo_box->priv->arrow);
2694 gtk_widget_show_all (combo_box->priv->button);
2698 combo_box->priv->button = gtk_toggle_button_new ();
2699 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2700 combo_box->priv->focus_on_click);
2702 g_signal_connect (combo_box->priv->button, "toggled",
2703 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2704 gtk_widget_set_parent (combo_box->priv->button,
2705 GTK_BIN (combo_box)->child->parent);
2707 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2708 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2709 combo_box->priv->arrow);
2710 gtk_widget_show_all (combo_box->priv->button);
2713 g_signal_connect (combo_box->priv->button, "button_press_event",
2714 G_CALLBACK (gtk_combo_box_menu_button_press),
2716 g_signal_connect (combo_box->priv->button, "state_changed",
2717 G_CALLBACK (gtk_combo_box_button_state_changed),
2720 /* create our funky menu */
2721 menu = gtk_menu_new ();
2722 gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2724 g_signal_connect (menu, "key_press_event",
2725 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2726 gtk_combo_box_set_popup_widget (combo_box, menu);
2730 gtk_combo_box_menu_fill (combo_box);
2732 /* the column is needed in tree_column_row_is_sensitive() */
2733 combo_box->priv->column = gtk_tree_view_column_new ();
2734 g_object_ref_sink (combo_box->priv->column);
2735 gtk_combo_box_sync_cells (combo_box,
2736 GTK_CELL_LAYOUT (combo_box->priv->column));
2738 gtk_combo_box_update_title (combo_box);
2742 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2746 if (!combo_box->priv->model)
2749 menu = combo_box->priv->popup_widget;
2751 if (combo_box->priv->add_tearoffs)
2753 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2755 gtk_widget_show (tearoff);
2757 if (combo_box->priv->wrap_width)
2758 gtk_menu_attach (GTK_MENU (menu), tearoff,
2759 0, combo_box->priv->wrap_width, 0, 1);
2761 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2764 gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2768 gtk_cell_view_menu_item_new (GtkComboBox *combo_box,
2769 GtkTreeModel *model,
2772 GtkWidget *cell_view;
2777 cell_view = gtk_cell_view_new ();
2778 item = gtk_menu_item_new ();
2779 gtk_container_add (GTK_CONTAINER (item), cell_view);
2781 gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);
2782 path = gtk_tree_model_get_path (model, iter);
2783 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2784 gtk_tree_path_free (path);
2786 gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2787 gtk_widget_size_request (cell_view, &req);
2788 gtk_widget_show (cell_view);
2794 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2796 GtkTreeIter *parent)
2798 GtkTreeModel *model = combo_box->priv->model;
2799 GtkWidget *item, *submenu, *subitem, *separator;
2801 gboolean is_separator;
2806 n_children = gtk_tree_model_iter_n_children (model, parent);
2809 for (i = 0; i < n_children; i++)
2811 gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2813 if (combo_box->priv->row_separator_func)
2814 is_separator = (*combo_box->priv->row_separator_func) (combo_box->priv->model, &iter,
2815 combo_box->priv->row_separator_data);
2817 is_separator = FALSE;
2821 item = gtk_separator_menu_item_new ();
2822 path = gtk_tree_model_get_path (model, &iter);
2823 g_object_set_data_full (G_OBJECT (item),
2824 I_("gtk-combo-box-item-path"),
2825 gtk_tree_row_reference_new (model, path),
2826 (GDestroyNotify)gtk_tree_row_reference_free);
2827 gtk_tree_path_free (path);
2831 item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2832 if (gtk_tree_model_iter_has_child (model, &iter))
2834 submenu = gtk_menu_new ();
2835 gtk_widget_show (submenu);
2836 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2838 /* Ugly - since menus can only activate leafs, we have to
2839 * duplicate the item inside the submenu.
2841 subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2842 separator = gtk_separator_menu_item_new ();
2843 gtk_widget_show (subitem);
2844 gtk_widget_show (separator);
2845 g_signal_connect (subitem, "activate",
2846 G_CALLBACK (gtk_combo_box_menu_item_activate),
2848 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2849 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2851 gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2853 g_signal_connect (item, "activate",
2854 G_CALLBACK (gtk_combo_box_menu_item_activate),
2858 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2859 if (combo_box->priv->wrap_width && menu == combo_box->priv->popup_widget)
2860 gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2861 gtk_widget_show (item);
2868 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2870 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2871 G_SIGNAL_MATCH_DATA,
2873 gtk_combo_box_menu_button_press, NULL);
2874 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2875 G_SIGNAL_MATCH_DATA,
2877 gtk_combo_box_button_state_changed, combo_box);
2879 /* unparent will remove our latest ref */
2880 gtk_widget_unparent (combo_box->priv->button);
2882 combo_box->priv->box = NULL;
2883 combo_box->priv->button = NULL;
2884 combo_box->priv->arrow = NULL;
2885 combo_box->priv->separator = NULL;
2887 g_object_unref (combo_box->priv->column);
2888 combo_box->priv->column = NULL;
2890 /* changing the popup window will unref the menu and the children */
2898 menu_occupied (GtkMenu *menu,
2902 guint bottom_attach)
2906 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2910 gtk_container_child_get (GTK_CONTAINER (menu),
2914 "bottom-attach", &b,
2918 /* look if this item intersects with the given coordinates */
2919 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2927 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2932 gint current_col = 0, current_row = 0;
2933 gint rows = 1, cols = 1;
2934 GtkWidget *menu = combo_box->priv->popup_widget;
2936 if (!GTK_IS_MENU_SHELL (menu))
2939 if (combo_box->priv->col_column == -1 &&
2940 combo_box->priv->row_column == -1 &&
2943 gtk_container_child_get (GTK_CONTAINER (menu),
2945 "right-attach", ¤t_col,
2946 "top-attach", ¤t_row,
2948 if (current_col + cols > combo_box->priv->wrap_width)
2956 if (combo_box->priv->col_column != -1)
2957 gtk_tree_model_get (combo_box->priv->model, iter,
2958 combo_box->priv->col_column, &cols,
2960 if (combo_box->priv->row_column != -1)
2961 gtk_tree_model_get (combo_box->priv->model, iter,
2962 combo_box->priv->row_column, &rows,
2967 if (current_col + cols > combo_box->priv->wrap_width)
2973 if (!menu_occupied (GTK_MENU (menu),
2974 current_col, current_col + cols,
2975 current_row, current_row + rows))
2982 /* set attach props */
2983 gtk_menu_attach (GTK_MENU (menu), item,
2984 current_col, current_col + cols,
2985 current_row, current_row + rows);
2989 gtk_combo_box_relayout (GtkComboBox *combo_box)
2994 menu = combo_box->priv->popup_widget;
2996 /* do nothing unless we are in menu style and realized */
2997 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3000 list = gtk_container_get_children (GTK_CONTAINER (menu));
3002 for (j = g_list_last (list); j; j = j->prev)
3003 gtk_container_remove (GTK_CONTAINER (menu), j->data);
3005 gtk_combo_box_menu_fill (combo_box);
3012 gtk_combo_box_menu_button_press (GtkWidget *widget,
3013 GdkEventButton *event,
3016 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3018 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
3019 event->type == GDK_BUTTON_PRESS && event->button == 1)
3021 if (combo_box->priv->focus_on_click &&
3022 !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
3023 gtk_widget_grab_focus (combo_box->priv->button);
3025 gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3034 gtk_combo_box_menu_item_activate (GtkWidget *item,
3037 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3038 GtkWidget *cell_view;
3042 cell_view = GTK_BIN (item)->child;
3044 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3046 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3048 if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3050 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3051 gtk_combo_box_set_active_iter (combo_box, &iter);
3054 gtk_tree_path_free (path);
3056 combo_box->priv->editing_canceled = FALSE;
3060 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
3065 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3067 if (combo_box->priv->tree_view)
3068 gtk_combo_box_list_popup_resize (combo_box);
3070 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3074 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
3078 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3080 if (combo_box->priv->cell_view)
3082 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
3083 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3086 if (combo_box->priv->tree_view)
3087 gtk_combo_box_list_popup_resize (combo_box);
3089 gtk_combo_box_menu_row_deleted (model, path, user_data);
3093 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
3099 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3101 gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3103 if (!combo_box->priv->tree_view)
3104 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3108 gtk_combo_box_model_row_changed (GtkTreeModel *model,
3113 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3114 GtkTreePath *active_path;
3116 /* FIXME this belongs to GtkCellView */
3117 if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3119 active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3120 if (gtk_tree_path_compare (path, active_path) == 0 &&
3121 combo_box->priv->cell_view)
3122 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
3123 gtk_tree_path_free (active_path);
3126 if (combo_box->priv->tree_view)
3127 gtk_combo_box_list_row_changed (model, path, iter, user_data);
3129 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3133 list_popup_resize_idle (gpointer user_data)
3135 GtkComboBox *combo_box;
3136 gint x, y, width, height;
3138 combo_box = GTK_COMBO_BOX (user_data);
3140 if (combo_box->priv->tree_view &&
3141 GTK_WIDGET_MAPPED (combo_box->priv->popup_window))
3143 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3145 gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);
3146 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
3149 combo_box->priv->resize_idle_id = 0;
3155 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3157 if (!combo_box->priv->resize_idle_id)
3158 combo_box->priv->resize_idle_id =
3159 gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3163 gtk_combo_box_model_row_expanded (GtkTreeModel *model,
3168 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3170 gtk_combo_box_list_popup_resize (combo_box);
3175 find_menu_by_path (GtkWidget *menu,
3177 gboolean skip_first)
3182 GtkTreeRowReference *mref;
3186 list = gtk_container_get_children (GTK_CONTAINER (menu));
3189 for (i = list; i; i = i->next)
3191 if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3193 mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3196 else if (!gtk_tree_row_reference_valid (mref))
3199 mpath = gtk_tree_row_reference_get_path (mref);
3201 else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3209 mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3214 /* this case is necessary, since the row reference of
3215 * the cell view may already be updated after a deletion
3222 if (gtk_tree_path_compare (mpath, path) == 0)
3224 gtk_tree_path_free (mpath);
3228 if (gtk_tree_path_is_ancestor (mpath, path))
3230 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3231 if (submenu != NULL)
3233 gtk_tree_path_free (mpath);
3234 item = find_menu_by_path (submenu, path, TRUE);
3238 gtk_tree_path_free (mpath);
3248 dump_menu_tree (GtkWidget *menu,
3255 list = gtk_container_get_children (GTK_CONTAINER (menu));
3256 for (i = list; i; i = i->next)
3258 if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3260 path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3261 g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3262 gtk_tree_path_free (path);
3264 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3265 if (submenu != NULL)
3266 dump_menu_tree (submenu, level + 1);
3275 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3281 GtkWidget *item, *menu, *separator;
3282 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3286 gboolean is_separator;
3288 if (!combo_box->priv->popup_widget)
3291 depth = gtk_tree_path_get_depth (path);
3292 pos = gtk_tree_path_get_indices (path)[depth - 1];
3295 ppath = gtk_tree_path_copy (path);
3296 gtk_tree_path_up (ppath);
3297 parent = find_menu_by_path (combo_box->priv->popup_widget, ppath, FALSE);
3298 gtk_tree_path_free (ppath);
3300 menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3303 menu = gtk_menu_new ();
3304 gtk_widget_show (menu);
3305 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3307 /* Ugly - since menus can only activate leaves, we have to
3308 * duplicate the item inside the submenu.
3310 gtk_tree_model_iter_parent (model, &piter, iter);
3311 item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3312 separator = gtk_separator_menu_item_new ();
3313 g_signal_connect (item, "activate",
3314 G_CALLBACK (gtk_combo_box_menu_item_activate),
3316 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3317 gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3318 if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3320 gtk_widget_show (item);
3321 gtk_widget_show (separator);
3328 menu = combo_box->priv->popup_widget;
3329 if (combo_box->priv->add_tearoffs)
3333 if (combo_box->priv->row_separator_func)
3334 is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3335 combo_box->priv->row_separator_data);
3337 is_separator = FALSE;
3341 item = gtk_separator_menu_item_new ();
3342 g_object_set_data_full (G_OBJECT (item),
3343 I_("gtk-combo-box-item-path"),
3344 gtk_tree_row_reference_new (model, path),
3345 (GDestroyNotify)gtk_tree_row_reference_free);
3349 item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3351 g_signal_connect (item, "activate",
3352 G_CALLBACK (gtk_combo_box_menu_item_activate),
3356 gtk_widget_show (item);
3357 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3361 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3365 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3369 if (!combo_box->priv->popup_widget)
3372 item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3373 menu = gtk_widget_get_parent (item);
3374 gtk_container_remove (GTK_CONTAINER (menu), item);
3376 if (gtk_tree_path_get_depth (path) > 1)
3378 GtkTreePath *parent_path;
3382 parent_path = gtk_tree_path_copy (path);
3383 gtk_tree_path_up (parent_path);
3384 gtk_tree_model_get_iter (model, &iter, parent_path);
3386 if (!gtk_tree_model_iter_has_child (model, &iter))
3388 parent = find_menu_by_path (combo_box->priv->popup_widget,
3389 parent_path, FALSE);
3390 gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3396 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
3402 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3404 gtk_combo_box_relayout (combo_box);
3408 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3413 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3416 gboolean is_separator;
3418 if (!combo_box->priv->popup_widget)
3421 item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3423 if (combo_box->priv->row_separator_func)
3424 is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3425 combo_box->priv->row_separator_data);
3427 is_separator = FALSE;
3429 if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3431 gtk_combo_box_menu_row_deleted (model, path, combo_box);
3432 gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3435 if (combo_box->priv->wrap_width
3436 && item->parent == combo_box->priv->popup_widget)
3438 GtkWidget *pitem = NULL;
3441 prev = gtk_tree_path_copy (path);
3443 if (gtk_tree_path_prev (prev))
3444 pitem = find_menu_by_path (combo_box->priv->popup_widget, prev, FALSE);
3446 gtk_tree_path_free (prev);
3448 /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3450 gtk_container_child_set (GTK_CONTAINER (combo_box->priv->popup_widget),
3455 "bottom-attach", -1,
3458 gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3461 width = gtk_combo_box_calc_requested_width (combo_box, path);
3463 if (width > combo_box->priv->width)
3465 if (combo_box->priv->cell_view)
3467 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3468 gtk_widget_queue_resize (combo_box->priv->cell_view);
3470 combo_box->priv->width = width;
3479 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3481 GtkTreeSelection *sel;
3483 combo_box->priv->button = gtk_toggle_button_new ();
3484 gtk_widget_set_parent (combo_box->priv->button,
3485 GTK_BIN (combo_box)->child->parent);
3486 g_signal_connect (combo_box->priv->button, "button_press_event",
3487 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3488 g_signal_connect (combo_box->priv->button, "toggled",
3489 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3491 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3492 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
3493 combo_box->priv->arrow);
3494 combo_box->priv->separator = NULL;
3495 gtk_widget_show_all (combo_box->priv->button);
3497 if (combo_box->priv->cell_view)
3499 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
3500 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3502 combo_box->priv->box = gtk_event_box_new ();
3503 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->box),
3506 if (combo_box->priv->has_frame)
3508 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
3509 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
3514 combo_box->priv->cell_view_frame = gtk_event_box_new ();
3515 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3519 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
3520 GTK_BIN (combo_box)->child->parent);
3521 gtk_container_add (GTK_CONTAINER (combo_box->priv->cell_view_frame),
3522 combo_box->priv->box);
3523 gtk_widget_show_all (combo_box->priv->cell_view_frame);
3525 g_signal_connect (combo_box->priv->box, "button_press_event",
3526 G_CALLBACK (gtk_combo_box_list_button_pressed),
3530 combo_box->priv->tree_view = gtk_tree_view_new ();
3531 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3532 gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3533 gtk_tree_selection_set_select_function (sel,
3534 gtk_combo_box_list_select_func,
3536 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
3538 gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (combo_box->priv->tree_view),
3540 if (combo_box->priv->row_separator_func)
3541 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
3542 combo_box->priv->row_separator_func,
3543 combo_box->priv->row_separator_data,
3545 if (combo_box->priv->model)
3546 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3547 combo_box->priv->model);
3549 combo_box->priv->column = gtk_tree_view_column_new ();
3550 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
3551 combo_box->priv->column);
3554 gtk_combo_box_sync_cells (combo_box,
3555 GTK_CELL_LAYOUT (combo_box->priv->column));
3557 if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3561 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3562 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
3564 gtk_tree_path_free (path);
3567 /* set sample/popup widgets */
3568 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
3570 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
3571 G_CALLBACK (gtk_combo_box_list_key_press),
3573 g_signal_connect (combo_box->priv->tree_view, "enter_notify_event",
3574 G_CALLBACK (gtk_combo_box_list_enter_notify),
3576 g_signal_connect (combo_box->priv->tree_view, "row_expanded",
3577 G_CALLBACK (gtk_combo_box_model_row_expanded),
3579 g_signal_connect (combo_box->priv->tree_view, "row_collapsed",
3580 G_CALLBACK (gtk_combo_box_model_row_expanded),
3582 g_signal_connect (combo_box->priv->popup_window, "button_press_event",
3583 G_CALLBACK (gtk_combo_box_list_button_pressed),
3585 g_signal_connect (combo_box->priv->popup_window, "button_release_event",
3586 G_CALLBACK (gtk_combo_box_list_button_released),
3589 gtk_widget_show (combo_box->priv->tree_view);
3593 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3595 /* disconnect signals */
3596 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
3597 G_SIGNAL_MATCH_DATA,
3598 0, 0, NULL, NULL, combo_box);
3599 g_signal_handlers_disconnect_matched (combo_box->priv->button,
3600 G_SIGNAL_MATCH_DATA,
3602 gtk_combo_box_list_button_pressed,
3604 g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3605 G_SIGNAL_MATCH_DATA,
3607 gtk_combo_box_list_button_pressed,
3609 g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3610 G_SIGNAL_MATCH_DATA,
3612 gtk_combo_box_list_button_released,
3615 g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3616 G_SIGNAL_MATCH_DATA,
3618 gtk_combo_box_child_show,
3621 g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3622 G_SIGNAL_MATCH_DATA,
3624 gtk_combo_box_child_hide,
3627 if (combo_box->priv->box)
3628 g_signal_handlers_disconnect_matched (combo_box->priv->box,
3629 G_SIGNAL_MATCH_DATA,
3631 gtk_combo_box_list_button_pressed,
3634 /* destroy things (unparent will kill the latest ref from us)
3635 * last unref on button will destroy the arrow
3637 gtk_widget_unparent (combo_box->priv->button);
3638 combo_box->priv->button = NULL;
3639 combo_box->priv->arrow = NULL;
3641 if (combo_box->priv->cell_view)
3643 g_object_set (combo_box->priv->cell_view,
3644 "background-set", FALSE,
3648 if (combo_box->priv->cell_view_frame)
3650 gtk_widget_unparent (combo_box->priv->cell_view_frame);
3651 combo_box->priv->cell_view_frame = NULL;
3652 combo_box->priv->box = NULL;
3655 if (combo_box->priv->scroll_timer)
3657 g_source_remove (combo_box->priv->scroll_timer);
3658 combo_box->priv->scroll_timer = 0;
3661 if (combo_box->priv->resize_idle_id)
3663 g_source_remove (combo_box->priv->resize_idle_id);
3664 combo_box->priv->resize_idle_id = 0;
3667 gtk_widget_destroy (combo_box->priv->tree_view);
3669 combo_box->priv->tree_view = NULL;
3670 if (combo_box->priv->popup_widget)
3672 g_object_unref (combo_box->priv->popup_widget);
3673 combo_box->priv->popup_widget = NULL;
3680 gtk_combo_box_list_button_pressed (GtkWidget *widget,
3681 GdkEventButton *event,
3684 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3686 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3688 if (ewidget == combo_box->priv->popup_window)
3691 if ((ewidget != combo_box->priv->button && ewidget != combo_box->priv->box) ||
3692 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3695 if (combo_box->priv->focus_on_click &&
3696 !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
3697 gtk_widget_grab_focus (combo_box->priv->button);
3699 gtk_combo_box_popup (combo_box);
3701 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
3704 combo_box->priv->auto_scroll = FALSE;
3705 if (combo_box->priv->scroll_timer == 0)
3706 combo_box->priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3707 (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3710 combo_box->priv->popup_in_progress = TRUE;
3716 gtk_combo_box_list_button_released (GtkWidget *widget,
3717 GdkEventButton *event,
3721 GtkTreePath *path = NULL;
3724 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3726 gboolean popup_in_progress = FALSE;
3728 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3730 if (combo_box->priv->popup_in_progress)
3732 popup_in_progress = TRUE;
3733 combo_box->priv->popup_in_progress = FALSE;
3736 gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view),
3738 if (combo_box->priv->scroll_timer)
3740 g_source_remove (combo_box->priv->scroll_timer);
3741 combo_box->priv->scroll_timer = 0;
3744 if (ewidget != combo_box->priv->tree_view)
3746 if ((ewidget == combo_box->priv->button ||
3747 ewidget == combo_box->priv->box) &&
3748 !popup_in_progress &&
3749 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3751 gtk_combo_box_popdown (combo_box);
3755 /* released outside treeview */
3756 if (ewidget != combo_box->priv->button &&
3757 ewidget != combo_box->priv->box)
3759 gtk_combo_box_popdown (combo_box);
3767 /* select something cool */
3768 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (combo_box->priv->tree_view),
3774 return TRUE; /* clicked outside window? */
3776 gtk_tree_model_get_iter (combo_box->priv->model, &iter, path);
3777 gtk_tree_path_free (path);
3779 gtk_combo_box_popdown (combo_box);
3781 if (tree_column_row_is_sensitive (combo_box, &iter))
3782 gtk_combo_box_set_active_iter (combo_box, &iter);
3788 gtk_combo_box_menu_key_press (GtkWidget *widget,
3792 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3794 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3796 /* The menu hasn't managed the
3797 * event, forward it to the combobox
3799 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3806 gtk_combo_box_list_key_press (GtkWidget *widget,
3810 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3813 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
3814 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
3816 GtkTreeModel *model = NULL;
3818 gtk_combo_box_popdown (combo_box);
3820 if (combo_box->priv->model)
3822 GtkTreeSelection *sel;
3824 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3826 if (gtk_tree_selection_get_selected (sel, &model, &iter))
3827 gtk_combo_box_set_active_iter (combo_box, &iter);
3833 if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3835 /* The list hasn't managed the
3836 * event, forward it to the combobox
3838 gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3845 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3849 GtkWidget *tree_view = combo_box->priv->tree_view;
3853 adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3854 if (adj && adj->upper - adj->lower > adj->page_size)
3856 if (x <= tree_view->allocation.x &&
3857 adj->lower < adj->value)
3859 value = adj->value - (tree_view->allocation.x - x + 1);
3860 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3862 else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3863 adj->upper - adj->page_size > adj->value)
3865 value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3866 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3870 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3871 if (adj && adj->upper - adj->lower > adj->page_size)
3873 if (y <= tree_view->allocation.y &&
3874 adj->lower < adj->value)
3876 value = adj->value - (tree_view->allocation.y - y + 1);
3877 gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3879 else if (y >= tree_view->allocation.height &&
3880 adj->upper - adj->page_size > adj->value)
3882 value = adj->value + (y - tree_view->allocation.height + 1);
3883 gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3889 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3893 if (combo_box->priv->auto_scroll)
3895 gdk_window_get_pointer (combo_box->priv->tree_view->window,
3897 gtk_combo_box_list_auto_scroll (combo_box, x, y);
3904 gtk_combo_box_list_enter_notify (GtkWidget *widget,
3905 GdkEventCrossing *event,
3908 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3910 combo_box->priv->auto_scroll = TRUE;
3916 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3917 GtkTreeModel *model,
3919 gboolean path_currently_selected,
3923 gboolean sensitive = FALSE;
3925 for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
3927 GList *cells, *cell;
3928 gboolean cell_sensitive, cell_visible;
3930 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3932 if (!column->visible)
3935 gtk_tree_model_get_iter (model, &iter, path);
3936 gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3939 cell = cells = gtk_tree_view_column_get_cell_renderers (column);
3942 g_object_get (cell->data,
3943 "sensitive", &cell_sensitive,
3944 "visible", &cell_visible,
3947 if (cell_visible && cell_sensitive)
3952 g_list_free (cells);
3954 sensitive = cell_sensitive;
3961 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3966 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3969 width = gtk_combo_box_calc_requested_width (combo_box, path);
3971 if (width > combo_box->priv->width)
3973 if (combo_box->priv->cell_view)
3975 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3976 gtk_widget_queue_resize (combo_box->priv->cell_view);
3978 combo_box->priv->width = width;
3983 * GtkCellLayout implementation
3987 pack_start_recurse (GtkWidget *menu,
3988 GtkCellRenderer *cell,
3994 list = gtk_container_get_children (GTK_CONTAINER (menu));
3995 for (i = list; i; i = i->next)
3997 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3998 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4001 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4002 if (submenu != NULL)
4003 pack_start_recurse (submenu, cell, expand);
4010 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
4011 GtkCellRenderer *cell,
4014 ComboCellInfo *info;
4015 GtkComboBox *combo_box;
4017 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4018 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4020 combo_box = GTK_COMBO_BOX (layout);
4022 g_object_ref_sink (cell);
4024 info = g_new0 (ComboCellInfo, 1);
4026 info->expand = expand;
4027 info->pack = GTK_PACK_START;
4029 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
4031 if (combo_box->priv->cell_view)
4032 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4035 if (combo_box->priv->column)
4036 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
4038 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4039 pack_start_recurse (combo_box->priv->popup_widget, cell, expand);
4043 pack_end_recurse (GtkWidget *menu,
4044 GtkCellRenderer *cell,
4050 list = gtk_container_get_children (GTK_CONTAINER (menu));
4051 for (i = list; i; i = i->next)
4053 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4054 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4057 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4058 if (submenu != NULL)
4059 pack_end_recurse (submenu, cell, expand);
4066 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
4067 GtkCellRenderer *cell,
4070 ComboCellInfo *info;
4071 GtkComboBox *combo_box;
4073 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4074 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4076 combo_box = GTK_COMBO_BOX (layout);
4078 g_object_ref_sink (cell);
4080 info = g_new0 (ComboCellInfo, 1);
4082 info->expand = expand;
4083 info->pack = GTK_PACK_END;
4085 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
4087 if (combo_box->priv->cell_view)
4088 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4091 if (combo_box->priv->column)
4092 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
4094 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4095 pack_end_recurse (combo_box->priv->popup_widget, cell, expand);
4099 clear_recurse (GtkWidget *menu)
4104 list = gtk_container_get_children (GTK_CONTAINER (menu));
4105 for (i = list; i; i = i->next)
4107 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4108 gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child));
4110 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4111 if (submenu != NULL)
4112 clear_recurse (submenu);
4119 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4121 GtkComboBox *combo_box;
4124 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4126 combo_box = GTK_COMBO_BOX (layout);
4128 if (combo_box->priv->cell_view)
4129 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
4131 if (combo_box->priv->column)
4132 gtk_tree_view_column_clear (combo_box->priv->column);
4134 for (i = combo_box->priv->cells; i; i = i->next)
4136 ComboCellInfo *info = (ComboCellInfo *)i->data;
4138 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4139 g_object_unref (info->cell);
4143 g_slist_free (combo_box->priv->cells);
4144 combo_box->priv->cells = NULL;
4146 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4147 clear_recurse (combo_box->priv->popup_widget);
4151 add_attribute_recurse (GtkWidget *menu,
4152 GtkCellRenderer *cell,
4153 const gchar *attribute,
4159 list = gtk_container_get_children (GTK_CONTAINER (menu));
4160 for (i = list; i; i = i->next)
4162 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4163 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4164 cell, attribute, column);
4166 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4167 if (submenu != NULL)
4168 add_attribute_recurse (submenu, cell, attribute, column);
4175 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
4176 GtkCellRenderer *cell,
4177 const gchar *attribute,
4180 ComboCellInfo *info;
4181 GtkComboBox *combo_box;
4183 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4184 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4186 combo_box = GTK_COMBO_BOX (layout);
4188 info = gtk_combo_box_get_cell_info (combo_box, cell);
4190 info->attributes = g_slist_prepend (info->attributes,
4191 GINT_TO_POINTER (column));
4192 info->attributes = g_slist_prepend (info->attributes,
4193 g_strdup (attribute));
4195 if (combo_box->priv->cell_view)
4196 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4197 cell, attribute, column);
4199 if (combo_box->priv->column)
4200 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4201 cell, attribute, column);
4203 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4204 add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4205 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4209 combo_cell_data_func (GtkCellLayout *cell_layout,
4210 GtkCellRenderer *cell,
4211 GtkTreeModel *tree_model,
4215 ComboCellInfo *info = (ComboCellInfo *)data;
4216 GtkWidget *parent = NULL;
4221 (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
4223 if (GTK_IS_WIDGET (cell_layout))
4224 parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4226 if (GTK_IS_MENU_ITEM (parent) &&
4227 gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4228 g_object_set (cell, "sensitive", TRUE, NULL);
4233 set_cell_data_func_recurse (GtkWidget *menu,
4234 GtkCellRenderer *cell,
4235 ComboCellInfo *info)
4239 GtkWidget *cell_view;
4241 list = gtk_container_get_children (GTK_CONTAINER (menu));
4242 for (i = list; i; i = i->next)
4244 cell_view = GTK_BIN (i->data)->child;
4245 if (GTK_IS_CELL_LAYOUT (cell_view))
4247 /* Override sensitivity for inner nodes; we don't
4248 * want menuitems with submenus to appear insensitive
4250 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
4252 combo_cell_data_func,
4254 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4255 if (submenu != NULL)
4256 set_cell_data_func_recurse (submenu, cell, info);
4264 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
4265 GtkCellRenderer *cell,
4266 GtkCellLayoutDataFunc func,
4268 GDestroyNotify destroy)
4270 ComboCellInfo *info;
4271 GtkComboBox *combo_box;
4273 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4275 combo_box = GTK_COMBO_BOX (layout);
4277 info = gtk_combo_box_get_cell_info (combo_box, cell);
4278 g_return_if_fail (info != NULL);
4282 GDestroyNotify d = info->destroy;
4284 info->destroy = NULL;
4285 d (info->func_data);
4289 info->func_data = func_data;
4290 info->destroy = destroy;
4292 if (combo_box->priv->cell_view)
4293 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
4295 if (combo_box->priv->column)
4296 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
4298 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4299 set_cell_data_func_recurse (combo_box->priv->popup_widget, cell, info);
4301 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4305 clear_attributes_recurse (GtkWidget *menu,
4306 GtkCellRenderer *cell)
4311 list = gtk_container_get_children (GTK_CONTAINER (menu));
4312 for (i = list; i; i = i->next)
4314 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4315 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4318 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4319 if (submenu != NULL)
4320 clear_attributes_recurse (submenu, cell);
4327 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
4328 GtkCellRenderer *cell)
4330 ComboCellInfo *info;
4331 GtkComboBox *combo_box;
4334 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4335 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4337 combo_box = GTK_COMBO_BOX (layout);
4339 info = gtk_combo_box_get_cell_info (combo_box, cell);
4340 g_return_if_fail (info != NULL);
4342 list = info->attributes;
4343 while (list && list->next)
4345 g_free (list->data);
4346 list = list->next->next;
4348 g_slist_free (info->attributes);
4349 info->attributes = NULL;
4351 if (combo_box->priv->cell_view)
4352 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
4354 if (combo_box->priv->column)
4355 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
4357 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4358 clear_attributes_recurse (combo_box->priv->popup_widget, cell);
4360 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4364 reorder_recurse (GtkWidget *menu,
4365 GtkCellRenderer *cell,
4371 list = gtk_container_get_children (GTK_CONTAINER (menu));
4372 for (i = list; i; i = i->next)
4374 if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4375 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4378 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4379 if (submenu != NULL)
4380 reorder_recurse (submenu, cell, position);
4387 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
4388 GtkCellRenderer *cell,
4391 ComboCellInfo *info;
4392 GtkComboBox *combo_box;
4395 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4396 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4398 combo_box = GTK_COMBO_BOX (layout);
4400 info = gtk_combo_box_get_cell_info (combo_box, cell);
4402 g_return_if_fail (info != NULL);
4403 g_return_if_fail (position >= 0);
4405 link = g_slist_find (combo_box->priv->cells, info);
4407 g_return_if_fail (link != NULL);
4409 combo_box->priv->cells = g_slist_delete_link (combo_box->priv->cells, link);
4410 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
4413 if (combo_box->priv->cell_view)
4414 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4417 if (combo_box->priv->column)
4418 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
4421 if (GTK_IS_MENU (combo_box->priv->popup_widget))
4422 reorder_recurse (combo_box->priv->popup_widget, cell, position);
4424 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4432 * gtk_combo_box_new:
4434 * Creates a new empty #GtkComboBox.
4436 * Return value: A new #GtkComboBox.
4441 gtk_combo_box_new (void)
4443 return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4447 * gtk_combo_box_new_with_model:
4448 * @model: A #GtkTreeModel.
4450 * Creates a new #GtkComboBox with the model initialized to @model.
4452 * Return value: A new #GtkComboBox.
4457 gtk_combo_box_new_with_model (GtkTreeModel *model)
4459 GtkComboBox *combo_box;
4461 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4463 combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4465 return GTK_WIDGET (combo_box);
4469 * gtk_combo_box_get_wrap_width:
4470 * @combo_box: A #GtkComboBox
4472 * Returns the wrap width which is used to determine the number of columns
4473 * for the popup menu. If the wrap width is larger than 1, the combo box
4476 * Returns: the wrap width.
4481 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4483 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4485 return combo_box->priv->wrap_width;
4489 * gtk_combo_box_set_wrap_width:
4490 * @combo_box: A #GtkComboBox
4491 * @width: Preferred number of columns
4493 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4494 * the preferred number of columns when you want the popup to be layed out
4500 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4503 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4504 g_return_if_fail (width >= 0);
4506 if (width != combo_box->priv->wrap_width)
4508 combo_box->priv->wrap_width = width;
4510 gtk_combo_box_check_appearance (combo_box);
4511 gtk_combo_box_relayout (combo_box);
4513 g_object_notify (G_OBJECT (combo_box), "wrap-width");
4518 * gtk_combo_box_get_row_span_column:
4519 * @combo_box: A #GtkComboBox
4521 * Returns the column with row span information for @combo_box.
4523 * Returns: the row span column.
4528 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4530 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4532 return combo_box->priv->row_column;
4536 * gtk_combo_box_set_row_span_column:
4537 * @combo_box: A #GtkComboBox.
4538 * @row_span: A column in the model passed during construction.
4540 * Sets the column with row span information for @combo_box to be @row_span.
4541 * The row span column contains integers which indicate how many rows
4542 * an item should span.
4547 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4552 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4554 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4555 g_return_if_fail (row_span >= -1 && row_span < col);
4557 if (row_span != combo_box->priv->row_column)
4559 combo_box->priv->row_column = row_span;
4561 gtk_combo_box_relayout (combo_box);
4563 g_object_notify (G_OBJECT (combo_box), "row-span-column");
4568 * gtk_combo_box_get_column_span_column:
4569 * @combo_box: A #GtkComboBox
4571 * Returns the column with column span information for @combo_box.
4573 * Returns: the column span column.
4578 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4580 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4582 return combo_box->priv->col_column;
4586 * gtk_combo_box_set_column_span_column:
4587 * @combo_box: A #GtkComboBox
4588 * @column_span: A column in the model passed during construction
4590 * Sets the column with column span information for @combo_box to be
4591 * @column_span. The column span column contains integers which indicate
4592 * how many columns an item should span.
4597 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4602 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4604 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4605 g_return_if_fail (column_span >= -1 && column_span < col);
4607 if (column_span != combo_box->priv->col_column)
4609 combo_box->priv->col_column = column_span;
4611 gtk_combo_box_relayout (combo_box);
4613 g_object_notify (G_OBJECT (combo_box), "column-span-column");
4618 * gtk_combo_box_get_active:
4619 * @combo_box: A #GtkComboBox
4621 * Returns the index of the currently active item, or -1 if there's no
4622 * active item. If the model is a non-flat treemodel, and the active item
4623 * is not an immediate child of the root of the tree, this function returns
4624 * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4625 * <literal>path</literal> is the #GtkTreePath of the active item.
4627 * Return value: An integer which is the index of the currently active item,
4628 * or -1 if there's no active item.
4633 gtk_combo_box_get_active (GtkComboBox *combo_box)
4636 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4638 if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
4642 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4643 result = gtk_tree_path_get_indices (path)[0];
4644 gtk_tree_path_free (path);
4653 * gtk_combo_box_set_active:
4654 * @combo_box: A #GtkComboBox
4655 * @index_: An index in the model passed during construction, or -1 to have
4658 * Sets the active item of @combo_box to be the item at @index.
4663 gtk_combo_box_set_active (GtkComboBox *combo_box,
4666 GtkTreePath *path = NULL;
4667 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4668 g_return_if_fail (index_ >= -1);
4671 path = gtk_tree_path_new_from_indices (index_, -1);
4673 gtk_combo_box_set_active_internal (combo_box, path);
4676 gtk_tree_path_free (path);
4680 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4683 GtkTreePath *active_path;
4686 if (path && gtk_tree_row_reference_valid (combo_box->priv->active_row))
4688 active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4689 path_cmp = gtk_tree_path_compare (path, active_path);
4690 gtk_tree_path_free (active_path);
4695 if (combo_box->priv->active_row)
4697 gtk_tree_row_reference_free (combo_box->priv->active_row);
4698 combo_box->priv->active_row = NULL;
4703 if (combo_box->priv->tree_view)
4704 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
4707 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
4709 if (GTK_IS_MENU (menu))
4710 gtk_menu_set_active (menu, -1);
4713 if (combo_box->priv->cell_view)
4714 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
4718 combo_box->priv->active_row =
4719 gtk_tree_row_reference_new (combo_box->priv->model, path);
4721 if (combo_box->priv->tree_view)
4723 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
4726 else if (GTK_IS_MENU (combo_box->priv->popup_widget))
4728 /* FIXME handle nested menus better */
4729 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
4730 gtk_tree_path_get_indices (path)[0]);
4733 if (combo_box->priv->cell_view)
4734 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
4738 g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4739 g_object_notify (G_OBJECT (combo_box), "active");
4744 * gtk_combo_box_get_active_iter:
4745 * @combo_box: A #GtkComboBox
4746 * @iter: The uninitialized #GtkTreeIter
4748 * Sets @iter to point to the current active item, if it exists.
4750 * Return value: %TRUE, if @iter was set
4755 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
4761 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4763 if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4766 path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4767 result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4768 gtk_tree_path_free (path);
4774 * gtk_combo_box_set_active_iter:
4775 * @combo_box: A #GtkComboBox
4776 * @iter: The #GtkTreeIter
4778 * Sets the current active item to be the one referenced by @iter.
4779 * @iter must correspond to a path of depth one.
4784 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
4789 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4791 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4792 gtk_combo_box_set_active_internal (combo_box, path);
4793 gtk_tree_path_free (path);
4797 * gtk_combo_box_set_model:
4798 * @combo_box: A #GtkComboBox
4799 * @model: A #GtkTreeModel
4801 * Sets the model used by @combo_box to be @model. Will unset a previously set
4802 * model (if applicable). If model is %NULL, then it will unset the model.
4804 * Note that this function does not clear the cell renderers, you have to
4805 * call gtk_cell_layout_clear() yourself if you need to set up different
4806 * cell renderers for the new model.
4811 gtk_combo_box_set_model (GtkComboBox *combo_box,
4812 GtkTreeModel *model)
4814 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4815 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4817 if (model == combo_box->priv->model)
4820 gtk_combo_box_unset_model (combo_box);
4825 combo_box->priv->model = model;
4826 g_object_ref (combo_box->priv->model);
4828 combo_box->priv->inserted_id =
4829 g_signal_connect (combo_box->priv->model, "row_inserted",
4830 G_CALLBACK (gtk_combo_box_model_row_inserted),
4832 combo_box->priv->deleted_id =
4833 g_signal_connect (combo_box->priv->model, "row_deleted",
4834 G_CALLBACK (gtk_combo_box_model_row_deleted),
4836 combo_box->priv->reordered_id =
4837 g_signal_connect (combo_box->priv->model, "rows_reordered",
4838 G_CALLBACK (gtk_combo_box_model_rows_reordered),
4840 combo_box->priv->changed_id =
4841 g_signal_connect (combo_box->priv->model, "row_changed",
4842 G_CALLBACK (gtk_combo_box_model_row_changed),
4845 if (combo_box->priv->tree_view)
4848 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4849 combo_box->priv->model);
4850 gtk_combo_box_list_popup_resize (combo_box);
4855 if (combo_box->priv->popup_widget)
4856 gtk_combo_box_menu_fill (combo_box);
4860 if (combo_box->priv->cell_view)
4861 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4862 combo_box->priv->model);
4866 * gtk_combo_box_get_model
4867 * @combo_box: A #GtkComboBox
4869 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4871 * Return value: A #GtkTreeModel which was passed during construction.
4876 gtk_combo_box_get_model (GtkComboBox *combo_box)
4878 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4880 return combo_box->priv->model;
4884 /* convenience API for simple text combos */
4887 * gtk_combo_box_new_text:
4889 * Convenience function which constructs a new text combo box, which is a
4890 * #GtkComboBox just displaying strings. If you use this function to create
4891 * a text combo box, you should only manipulate its data source with the
4892 * following convenience functions: gtk_combo_box_append_text(),
4893 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
4894 * gtk_combo_box_remove_text().
4896 * Return value: A new text combo box.
4901 gtk_combo_box_new_text (void)
4903 GtkWidget *combo_box;
4904 GtkCellRenderer *cell;
4905 GtkListStore *store;
4907 store = gtk_list_store_new (1, G_TYPE_STRING);
4908 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
4909 g_object_unref (store);
4911 cell = gtk_cell_renderer_text_new ();
4912 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
4913 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
4921 * gtk_combo_box_append_text:
4922 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
4925 * Appends @string to the list of strings stored in @combo_box. Note that
4926 * you can only use this function with combo boxes constructed with
4927 * gtk_combo_box_new_text().
4932 gtk_combo_box_append_text (GtkComboBox *combo_box,
4936 GtkListStore *store;
4938 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4939 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4940 g_return_if_fail (text != NULL);
4942 store = GTK_LIST_STORE (combo_box->priv->model);
4944 gtk_list_store_append (store, &iter);
4945 gtk_list_store_set (store, &iter, 0, text, -1);
4949 * gtk_combo_box_insert_text:
4950 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
4951 * @position: An index to insert @text
4954 * Inserts @string at @position in the list of strings stored in @combo_box.
4955 * Note that you can only use this function with combo boxes constructed
4956 * with gtk_combo_box_new_text().
4961 gtk_combo_box_insert_text (GtkComboBox *combo_box,
4966 GtkListStore *store;
4968 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4969 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4970 g_return_if_fail (position >= 0);
4971 g_return_if_fail (text != NULL);
4973 store = GTK_LIST_STORE (combo_box->priv->model);
4975 gtk_list_store_insert (store, &iter, position);
4976 gtk_list_store_set (store, &iter, 0, text, -1);
4980 * gtk_combo_box_prepend_text:
4981 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
4984 * Prepends @string to the list of strings stored in @combo_box. Note that
4985 * you can only use this function with combo boxes constructed with
4986 * gtk_combo_box_new_text().
4991 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
4995 GtkListStore *store;
4997 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4998 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4999 g_return_if_fail (text != NULL);
5001 store = GTK_LIST_STORE (combo_box->priv->model);
5003 gtk_list_store_prepend (store, &iter);
5004 gtk_list_store_set (store, &iter, 0, text, -1);
5008 * gtk_combo_box_remove_text:
5009 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5010 * @position: Index of the item to remove
5012 * Removes the string at @position from @combo_box. Note that you can only use
5013 * this function with combo boxes constructed with gtk_combo_box_new_text().
5018 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5022 GtkListStore *store;
5024 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5025 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5026 g_return_if_fail (position >= 0);
5028 store = GTK_LIST_STORE (combo_box->priv->model);
5030 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5032 gtk_list_store_remove (store, &iter);
5036 * gtk_combo_box_get_active_text:
5037 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5039 * Returns the currently active string in @combo_box or %NULL if none
5040 * is selected. Note that you can only use this function with combo
5041 * boxes constructed with gtk_combo_box_new_text() and with
5042 * #GtkComboBoxEntry<!-- -->s.
5044 * Returns: a newly allocated string containing the currently active text.
5049 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5051 GtkComboBoxClass *class;
5053 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5055 class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5057 if (class->get_active_text)
5058 return (* class->get_active_text) (combo_box);
5064 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5069 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5071 if (gtk_combo_box_get_active_iter (combo_box, &iter))
5072 gtk_tree_model_get (combo_box->priv->model, &iter,
5079 gtk_combo_box_real_move_active (GtkComboBox *combo_box,
5080 GtkScrollType scroll)
5083 GtkTreeIter new_iter;
5084 gboolean active_iter;
5087 if (!combo_box->priv->model)
5089 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5093 active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5097 case GTK_SCROLL_STEP_BACKWARD:
5098 case GTK_SCROLL_STEP_UP:
5099 case GTK_SCROLL_STEP_LEFT:
5102 found = tree_prev (combo_box, combo_box->priv->model,
5103 &iter, &new_iter, FALSE);
5106 /* else fall through */
5108 case GTK_SCROLL_PAGE_FORWARD:
5109 case GTK_SCROLL_PAGE_DOWN:
5110 case GTK_SCROLL_PAGE_RIGHT:
5111 case GTK_SCROLL_END:
5112 found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5115 case GTK_SCROLL_STEP_FORWARD:
5116 case GTK_SCROLL_STEP_DOWN:
5117 case GTK_SCROLL_STEP_RIGHT:
5120 found = tree_next (combo_box, combo_box->priv->model,
5121 &iter, &new_iter, FALSE);
5124 /* else fall through */
5126 case GTK_SCROLL_PAGE_BACKWARD:
5127 case GTK_SCROLL_PAGE_UP:
5128 case GTK_SCROLL_PAGE_LEFT:
5129 case GTK_SCROLL_START:
5130 found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5137 if (found && active_iter)
5139 GtkTreePath *old_path;
5140 GtkTreePath *new_path;
5142 old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5143 new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5145 if (gtk_tree_path_compare (old_path, new_path) == 0)
5148 gtk_tree_path_free (old_path);
5149 gtk_tree_path_free (new_path);
5154 gtk_combo_box_set_active_iter (combo_box, &new_iter);
5158 gtk_widget_error_bell (GTK_WIDGET (combo_box));
5163 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5164 gboolean group_cycling)
5166 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5168 gtk_widget_grab_focus (combo_box->priv->button);
5174 gtk_combo_box_grab_focus (GtkWidget *widget)
5176 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5178 gtk_widget_grab_focus (combo_box->priv->button);
5182 gtk_combo_box_destroy (GtkObject *object)
5184 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5186 if (combo_box->priv->popup_idle_id > 0)
5188 g_source_remove (combo_box->priv->popup_idle_id);
5189 combo_box->priv->popup_idle_id = 0;
5192 gtk_combo_box_popdown (combo_box);
5194 if (combo_box->priv->row_separator_destroy)
5195 (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5197 combo_box->priv->row_separator_func = NULL;
5198 combo_box->priv->row_separator_data = NULL;
5199 combo_box->priv->row_separator_destroy = NULL;
5201 GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5202 combo_box->priv->cell_view = NULL;
5206 gtk_combo_box_dispose(GObject* object)
5208 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5210 if (GTK_IS_MENU (combo_box->priv->popup_widget))
5212 gtk_combo_box_menu_destroy (combo_box);
5213 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5214 combo_box->priv->popup_widget = NULL;
5217 G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5221 gtk_combo_box_finalize (GObject *object)
5223 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5226 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5227 gtk_combo_box_list_destroy (combo_box);
5229 if (combo_box->priv->popup_window)
5230 gtk_widget_destroy (combo_box->priv->popup_window);
5232 gtk_combo_box_unset_model (combo_box);
5234 for (i = combo_box->priv->cells; i; i = i->next)
5236 ComboCellInfo *info = (ComboCellInfo *)i->data;
5237 GSList *list = info->attributes;
5240 info->destroy (info->func_data);
5242 while (list && list->next)
5244 g_free (list->data);
5245 list = list->next->next;
5247 g_slist_free (info->attributes);
5249 g_object_unref (info->cell);
5252 g_slist_free (combo_box->priv->cells);
5254 g_free (combo_box->priv->tearoff_title);
5256 G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5260 gtk_cell_editable_key_press (GtkWidget *widget,
5264 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5266 if (event->keyval == GDK_Escape)
5268 combo_box->priv->editing_canceled = TRUE;
5270 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5271 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5275 else if (event->keyval == GDK_Return)
5277 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5278 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5287 popdown_idle (gpointer data)
5289 GtkComboBox *combo_box;
5291 combo_box = GTK_COMBO_BOX (data);
5293 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5294 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5296 g_object_unref (combo_box);
5302 popdown_handler (GtkWidget *widget,
5305 gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5309 popup_idle (gpointer data)
5311 GtkComboBox *combo_box;
5313 combo_box = GTK_COMBO_BOX (data);
5315 if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5316 combo_box->priv->cell_view)
5317 g_signal_connect_object (combo_box->priv->popup_widget,
5318 "unmap", G_CALLBACK (popdown_handler),
5321 /* we unset this if a menu item is activated */
5322 combo_box->priv->editing_canceled = TRUE;
5323 gtk_combo_box_popup (combo_box);
5325 combo_box->priv->popup_idle_id = 0;
5331 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5334 GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5336 combo_box->priv->is_cell_renderer = TRUE;
5338 if (combo_box->priv->cell_view)
5340 g_signal_connect_object (combo_box->priv->button, "key_press_event",
5341 G_CALLBACK (gtk_cell_editable_key_press),
5344 gtk_widget_grab_focus (combo_box->priv->button);
5348 g_signal_connect_object (GTK_BIN (combo_box)->child, "key_press_event",
5349 G_CALLBACK (gtk_cell_editable_key_press),
5352 gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5353 GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5356 /* we do the immediate popup only for the optionmenu-like
5359 if (combo_box->priv->is_cell_renderer &&
5360 combo_box->priv->cell_view && !combo_box->priv->tree_view)
5361 combo_box->priv->popup_idle_id = gdk_threads_add_idle (popup_idle, combo_box);
5366 * gtk_combo_box_get_add_tearoffs:
5367 * @combo_box: a #GtkComboBox
5369 * Gets the current value of the :add-tearoffs property.
5371 * Return value: the current value of the :add-tearoffs property.
5374 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5376 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5378 return combo_box->priv->add_tearoffs;
5382 * gtk_combo_box_set_add_tearoffs:
5383 * @combo_box: a #GtkComboBox
5384 * @add_tearoffs: %TRUE to add tearoff menu items
5386 * Sets whether the popup menu should have a tearoff
5392 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5393 gboolean add_tearoffs)
5395 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5397 add_tearoffs = add_tearoffs != FALSE;
5399 if (combo_box->priv->add_tearoffs != add_tearoffs)
5401 combo_box->priv->add_tearoffs = add_tearoffs;
5402 gtk_combo_box_check_appearance (combo_box);
5403 gtk_combo_box_relayout (combo_box);
5404 g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5409 * gtk_combo_box_get_title:
5410 * @combo_box: a #GtkComboBox
5412 * Gets the current title of the menu in tearoff mode. See
5413 * gtk_combo_box_set_add_tearoffs().
5415 * Returns: the menu's title in tearoff mode. This is an internal copy of the
5416 * string which must not be freed.
5420 G_CONST_RETURN gchar*
5421 gtk_combo_box_get_title (GtkComboBox *combo_box)
5423 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5425 return combo_box->priv->tearoff_title;
5429 gtk_combo_box_update_title (GtkComboBox *combo_box)
5431 gtk_combo_box_check_appearance (combo_box);
5433 if (combo_box->priv->popup_widget &&
5434 GTK_IS_MENU (combo_box->priv->popup_widget))
5435 gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5436 combo_box->priv->tearoff_title);
5440 * gtk_combo_box_set_title:
5441 * @combo_box: a #GtkComboBox
5442 * @title: a title for the menu in tearoff mode
5444 * Sets the menu's title in tearoff mode.
5449 gtk_combo_box_set_title (GtkComboBox *combo_box,
5452 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5454 if (strcmp (title ? title : "",
5455 combo_box->priv->tearoff_title ? combo_box->priv->tearoff_title : "") != 0)
5457 g_free (combo_box->priv->tearoff_title);
5458 combo_box->priv->tearoff_title = g_strdup (title);
5460 gtk_combo_box_update_title (combo_box);
5462 g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5467 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5469 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5471 return combo_box->priv->editing_canceled;
5475 * gtk_combo_box_get_popup_accessible:
5476 * @combo_box: a #GtkComboBox
5478 * Gets the accessible object corresponding to the combo box's popup.
5480 * This function is mostly intended for use by accessibility technologies;
5481 * applications should have little use for it.
5483 * Returns: the accessible object corresponding to the combo box's popup.
5488 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5492 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5494 if (combo_box->priv->popup_widget)
5496 atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5504 * gtk_combo_box_get_row_separator_func:
5505 * @combo_box: a #GtkComboBox
5507 * Returns the current row separator function.
5509 * Return value: the current row separator function.
5513 GtkTreeViewRowSeparatorFunc
5514 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5516 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5518 return combo_box->priv->row_separator_func;
5522 * gtk_combo_box_set_row_separator_func:
5523 * @combo_box: a #GtkComboBox
5524 * @func: a #GtkTreeViewRowSeparatorFunc
5525 * @data: user data to pass to @func, or %NULL
5526 * @destroy: destroy notifier for @data, or %NULL
5528 * Sets the row separator function, which is used to determine
5529 * whether a row should be drawn as a separator. If the row separator
5530 * function is %NULL, no separators are drawn. This is the default value.
5535 gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box,
5536 GtkTreeViewRowSeparatorFunc func,
5538 GtkDestroyNotify destroy)
5540 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5542 if (combo_box->priv->row_separator_destroy)
5543 (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5545 combo_box->priv->row_separator_func = func;
5546 combo_box->priv->row_separator_data = data;
5547 combo_box->priv->row_separator_destroy = destroy;
5549 if (combo_box->priv->tree_view)
5550 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view),
5553 gtk_combo_box_relayout (combo_box);
5555 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5560 * gtk_combo_box_set_focus_on_click:
5561 * @combo: a #GtkComboBox
5562 * @focus_on_click: whether the combo box grabs focus when clicked
5565 * Sets whether the combo box will grab focus when it is clicked with
5566 * the mouse. Making mouse clicks not grab focus is useful in places
5567 * like toolbars where you don't want the keyboard focus removed from
5568 * the main area of the application.
5573 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5574 gboolean focus_on_click)
5576 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5578 focus_on_click = focus_on_click != FALSE;
5580 if (combo_box->priv->focus_on_click != focus_on_click)
5582 combo_box->priv->focus_on_click = focus_on_click;
5584 if (combo_box->priv->button)
5585 gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5588 g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5593 * gtk_combo_box_get_focus_on_click:
5594 * @combo: a #GtkComboBox
5596 * Returns whether the combo box grabs focus when it is clicked
5597 * with the mouse. See gtk_combo_box_set_focus_on_click().
5599 * Return value: %TRUE if the combo box grabs focus when it is
5600 * clicked with the mouse.
5605 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5607 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5609 return combo_box->priv->focus_on_click;
5614 gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable,
5615 GtkBuilder *builder,
5617 const gchar *tagname,
5618 GMarkupParser *parser,
5621 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5622 tagname, parser, data))
5625 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5626 tagname, parser, data);
5630 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5631 GtkBuilder *builder,
5633 const gchar *tagname,
5636 if (strcmp (tagname, "attributes") == 0)
5637 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5640 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5644 #define __GTK_COMBO_BOX_C__
5645 #include "gtkaliasdef.c"