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.
20 #include "gtkcombobox.h"
21 #include "gtkcelllayout.h"
22 #include "gtkcellview.h"
23 #include "gtkcellviewmenuitem.h"
25 #include "gtktreeselection.h"
27 #include "gtktogglebutton.h"
28 #include "gtkvseparator.h"
32 #include "gtkeventbox.h"
33 #include "gtkcellrenderertext.h"
34 #include "gtkbindings.h"
35 #include "gtkliststore.h"
36 #include "gtkwindow.h"
38 #include <gdk/gdkkeysyms.h>
40 #include <gobject/gvaluecollector.h>
45 #include "gtkmarshalers.h"
49 /* WELCOME, to THE house of evil code */
51 typedef struct _ComboCellInfo ComboCellInfo;
54 GtkCellRenderer *cell;
57 GtkCellLayoutDataFunc func;
59 GDestroyNotify destroy;
65 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
67 struct _GtkComboBoxPrivate
79 GtkTreeViewColumn *column;
82 GtkWidget *cell_view_frame;
88 GtkWidget *popup_widget;
89 GtkWidget *popup_window;
90 GtkWidget *popup_frame;
100 guint popup_in_progress : 1;
103 /* While debugging this evil code, I have learned that
104 * there are actually 4 modes to this widget, which can
105 * be characterized as follows
107 * 1) menu mode, no child added
110 * cell_view -> GtkCellView, regular child
111 * cell_view_frame -> NULL
112 * button -> GtkToggleButton set_parent to combo
113 * arrow -> GtkArrow set_parent to button
114 * separator -> GtkVSepator set_parent to button
115 * popup_widget -> GtkMenu
116 * popup_window -> NULL
117 * popup_frame -> NULL
119 * 2) menu mode, child added
123 * cell_view_frame -> NULL
124 * button -> GtkToggleButton set_parent to combo
125 * arrow -> GtkArrow, child of button
127 * popup_widget -> GtkMenu
128 * popup_window -> NULL
129 * popup_frame -> NULL
131 * 3) list mode, no child added
133 * tree_view -> GtkTreeView, child of popup_frame
134 * cell_view -> GtkCellView, regular child
135 * cell_view_frame -> GtkFrame, set parent to combo
136 * button -> GtkToggleButton, set_parent to combo
137 * arrow -> GtkArrow, child of button
139 * popup_widget -> tree_view
140 * popup_window -> GtkWindow
141 * popup_frame -> GtkFrame, child of popup_window
143 * 4) list mode, child added
145 * tree_view -> GtkTreeView, child of popup_frame
147 * cell_view_frame -> NULL
148 * button -> GtkToggleButton, set_parent to combo
149 * arrow -> GtkArrow, child of button
151 * popup_widget -> tree_view
152 * popup_window -> GtkWindow
153 * popup_frame -> GtkFrame, child of popup_window
166 PROP_ROW_SPAN_COLUMN,
167 PROP_COLUMN_SPAN_COLUMN,
171 static GtkBinClass *parent_class = NULL;
172 static guint combo_box_signals[LAST_SIGNAL] = {0,};
174 #define BONUS_PADDING 4
178 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
179 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
180 static void gtk_combo_box_init (GtkComboBox *combo_box);
181 static void gtk_combo_box_finalize (GObject *object);
182 static void gtk_combo_box_destroy (GtkObject *object);
184 static void gtk_combo_box_set_property (GObject *object,
188 static void gtk_combo_box_get_property (GObject *object,
193 static void gtk_combo_box_style_set (GtkWidget *widget,
194 GtkStyle *previous_style,
196 static void gtk_combo_box_button_toggled (GtkWidget *widget,
198 static void gtk_combo_box_add (GtkContainer *container,
201 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
202 GtkCellRenderer *cell);
204 static void gtk_combo_box_menu_show (GtkWidget *menu,
206 static void gtk_combo_box_menu_hide (GtkWidget *menu,
209 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
211 static void gtk_combo_box_menu_position (GtkMenu *menu,
217 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
219 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
221 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
222 static void gtk_combo_box_set_model_internal (GtkComboBox *combo_box);
224 static void gtk_combo_box_size_request (GtkWidget *widget,
225 GtkRequisition *requisition);
226 static void gtk_combo_box_size_allocate (GtkWidget *widget,
227 GtkAllocation *allocation);
228 static void gtk_combo_box_forall (GtkContainer *container,
229 gboolean include_internals,
230 GtkCallback callback,
231 gpointer callback_data);
232 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
233 GdkEventExpose *event);
234 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
235 GdkEventScroll *event);
238 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
239 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
241 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
242 GdkEventButton *event,
244 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
247 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
248 GdkEventButton *event,
251 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
257 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
258 gboolean add_childs);
259 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
260 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
262 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
266 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
268 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
270 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
271 GdkEventButton *event,
273 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
275 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
279 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
282 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
288 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
289 GtkCellRenderer *cell,
291 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
292 GtkCellRenderer *cell,
294 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
295 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
296 GtkCellRenderer *cell,
297 const gchar *attribute,
299 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
300 GtkCellRenderer *cell,
301 GtkCellLayoutDataFunc func,
303 GDestroyNotify destroy);
304 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
305 GtkCellRenderer *cell);
306 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
307 GtkCellRenderer *cell,
309 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
310 gboolean group_cycling);
314 gtk_combo_box_get_type (void)
316 static GType combo_box_type = 0;
320 static const GTypeInfo combo_box_info =
322 sizeof (GtkComboBoxClass),
323 NULL, /* base_init */
324 NULL, /* base_finalize */
325 (GClassInitFunc) gtk_combo_box_class_init,
326 NULL, /* class_finalize */
327 NULL, /* class_data */
328 sizeof (GtkComboBox),
330 (GInstanceInitFunc) gtk_combo_box_init
333 static const GInterfaceInfo cell_layout_info =
335 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
340 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
345 g_type_add_interface_static (combo_box_type,
346 GTK_TYPE_CELL_LAYOUT,
350 return combo_box_type;
355 gtk_combo_box_class_init (GtkComboBoxClass *klass)
357 GObjectClass *object_class;
358 GtkBindingSet *binding_set;
359 GtkObjectClass *gtk_object_class;
360 GtkContainerClass *container_class;
361 GtkWidgetClass *widget_class;
363 binding_set = gtk_binding_set_by_class (klass);
365 container_class = (GtkContainerClass *)klass;
366 container_class->forall = gtk_combo_box_forall;
367 container_class->add = gtk_combo_box_add;
369 widget_class = (GtkWidgetClass *)klass;
370 widget_class->size_allocate = gtk_combo_box_size_allocate;
371 widget_class->size_request = gtk_combo_box_size_request;
372 widget_class->expose_event = gtk_combo_box_expose_event;
373 widget_class->scroll_event = gtk_combo_box_scroll_event;
374 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
376 gtk_object_class = (GtkObjectClass *)klass;
377 gtk_object_class->destroy = gtk_combo_box_destroy;
379 object_class = (GObjectClass *)klass;
380 object_class->finalize = gtk_combo_box_finalize;
381 object_class->set_property = gtk_combo_box_set_property;
382 object_class->get_property = gtk_combo_box_get_property;
384 parent_class = g_type_class_peek_parent (klass);
387 combo_box_signals[CHANGED] =
388 g_signal_new ("changed",
389 G_OBJECT_CLASS_TYPE (klass),
391 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
393 g_cclosure_marshal_VOID__VOID,
397 g_object_class_install_property (object_class,
399 g_param_spec_object ("model",
400 P_("ComboBox model"),
401 P_("The model for the combo box"),
405 g_object_class_install_property (object_class,
407 g_param_spec_int ("wrap_width",
409 P_("Wrap width for layouting the items in a grid"),
415 g_object_class_install_property (object_class,
416 PROP_ROW_SPAN_COLUMN,
417 g_param_spec_int ("row_span_column",
418 P_("Row span column"),
419 P_("TreeModel column containing the row span values"),
425 g_object_class_install_property (object_class,
426 PROP_COLUMN_SPAN_COLUMN,
427 g_param_spec_int ("column_span_column",
428 P_("Column span column"),
429 P_("TreeModel column containing the column span values"),
435 g_object_class_install_property (object_class,
437 g_param_spec_int ("active",
439 P_("The item which is currently active"),
445 gtk_widget_class_install_style_property (widget_class,
446 g_param_spec_boolean ("appearance",
447 P_("ComboBox appareance"),
448 P_("ComboBox appearance, where TRUE means Windows-style."),
452 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
456 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
458 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
459 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
460 iface->clear = gtk_combo_box_cell_layout_clear;
461 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
462 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
463 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
464 iface->reorder = gtk_combo_box_cell_layout_reorder;
468 gtk_combo_box_init (GtkComboBox *combo_box)
470 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
472 g_signal_connect (combo_box, "style_set",
473 G_CALLBACK (gtk_combo_box_style_set), NULL);
475 combo_box->priv->cell_view = gtk_cell_view_new ();
476 gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
477 gtk_widget_show (combo_box->priv->cell_view);
479 combo_box->priv->width = 0;
480 combo_box->priv->wrap_width = 0;
482 combo_box->priv->active_item = -1;
483 combo_box->priv->col_column = -1;
484 combo_box->priv->row_column = -1;
488 gtk_combo_box_set_property (GObject *object,
493 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
498 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
501 case PROP_WRAP_WIDTH:
502 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
505 case PROP_ROW_SPAN_COLUMN:
506 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
509 case PROP_COLUMN_SPAN_COLUMN:
510 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
514 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
523 gtk_combo_box_get_property (GObject *object,
528 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
533 g_value_set_object (value, combo_box->priv->model);
536 case PROP_WRAP_WIDTH:
537 g_value_set_int (value, combo_box->priv->wrap_width);
540 case PROP_ROW_SPAN_COLUMN:
541 g_value_set_int (value, combo_box->priv->row_column);
544 case PROP_COLUMN_SPAN_COLUMN:
545 g_value_set_int (value, combo_box->priv->col_column);
549 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559 gtk_combo_box_style_set (GtkWidget *widget,
560 GtkStyle *previous_style,
564 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
566 gtk_widget_queue_resize (widget);
568 /* if wrap_width > 0, then we are in grid-mode and forced to use
571 if (combo_box->priv->wrap_width)
574 gtk_widget_style_get (widget,
575 "appearance", &appearance,
578 /* TRUE is windows style */
581 if (GTK_IS_MENU (combo_box->priv->popup_widget))
582 gtk_combo_box_menu_destroy (combo_box);
584 gtk_combo_box_unset_model (combo_box);
586 gtk_combo_box_list_setup (combo_box);
590 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
591 gtk_combo_box_list_destroy (combo_box);
593 gtk_combo_box_unset_model (combo_box);
595 gtk_combo_box_menu_setup (combo_box, TRUE);
600 gtk_combo_box_button_toggled (GtkWidget *widget,
603 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
605 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
607 if (!combo_box->priv->popup_in_progress)
608 gtk_combo_box_popup (combo_box);
611 gtk_combo_box_popdown (combo_box);
615 gtk_combo_box_add (GtkContainer *container,
618 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
620 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
621 gtk_container_remove (container, combo_box->priv->cell_view);
623 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
625 if (combo_box->priv->cell_view &&
626 widget != combo_box->priv->cell_view)
628 /* since the cell_view was unparented, it's gone now */
629 combo_box->priv->cell_view = NULL;
631 if (!combo_box->priv->tree_view && combo_box->priv->separator)
633 gtk_widget_unparent (combo_box->priv->separator);
634 combo_box->priv->separator = NULL;
636 g_object_ref (G_OBJECT (combo_box->priv->arrow));
637 gtk_widget_unparent (combo_box->priv->arrow);
638 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
639 combo_box->priv->arrow);
640 g_object_unref (G_OBJECT (combo_box->priv->arrow));
642 gtk_widget_queue_resize (GTK_WIDGET (container));
644 else if (combo_box->priv->cell_view_frame)
646 gtk_widget_unparent (combo_box->priv->cell_view_frame);
647 combo_box->priv->cell_view_frame = NULL;
652 static ComboCellInfo *
653 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
654 GtkCellRenderer *cell)
658 for (i = combo_box->priv->cells; i; i = i->next)
660 ComboCellInfo *info = (ComboCellInfo *)i->data;
662 if (info->cell == cell)
670 gtk_combo_box_menu_show (GtkWidget *menu,
673 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
675 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
677 combo_box->priv->popup_in_progress = FALSE;
681 gtk_combo_box_menu_hide (GtkWidget *menu,
684 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
686 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
691 gtk_combo_box_detacher (GtkWidget *widget,
694 GtkComboBox *combo_box;
696 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
698 combo_box = GTK_COMBO_BOX (widget);
699 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
701 g_signal_handlers_disconnect_by_func (menu,
702 gtk_combo_box_menu_show,
704 g_signal_handlers_disconnect_by_func (menu,
705 gtk_combo_box_menu_hide,
708 combo_box->priv->popup_widget = NULL;
712 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
715 if (GTK_IS_MENU (combo_box->priv->popup_widget))
717 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
718 combo_box->priv->popup_widget = NULL;
720 else if (combo_box->priv->popup_widget)
722 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
723 combo_box->priv->popup_widget);
724 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
725 combo_box->priv->popup_widget = NULL;
728 if (GTK_IS_MENU (popup))
730 if (combo_box->priv->popup_window)
732 gtk_widget_destroy (combo_box->priv->popup_window);
733 combo_box->priv->popup_window = combo_box->priv->popup_frame = NULL;
736 combo_box->priv->popup_widget = popup;
738 g_signal_connect (popup, "show",
739 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
740 g_signal_connect (popup, "hide",
741 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
743 gtk_menu_attach_to_widget (GTK_MENU (popup),
744 GTK_WIDGET (combo_box),
745 gtk_combo_box_detacher);
749 if (!combo_box->priv->popup_window)
751 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
752 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
753 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
755 combo_box->priv->popup_frame = gtk_frame_new (NULL);
756 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
758 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
759 combo_box->priv->popup_frame);
760 gtk_widget_show (combo_box->priv->popup_frame);
763 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
765 gtk_widget_show (popup);
766 g_object_ref (G_OBJECT (popup));
767 combo_box->priv->popup_widget = popup;
772 gtk_combo_box_menu_position (GtkMenu *menu,
781 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
783 /* FIXME: is using the size request here broken? */
784 child = GTK_BIN (combo_box)->child;
786 gdk_window_get_origin (child->window, &sx, &sy);
788 gtk_widget_size_request (GTK_WIDGET (menu), &req);
790 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
793 *x = sx + child->allocation.width - req.width;
794 *y = sy + child->allocation.height;
796 if (GTK_WIDGET_NO_WINDOW (child))
798 *x += child->allocation.x;
799 *y += child->allocation.y;
806 * gtk_combo_box_popup:
807 * @combo_box: a #GtkComboBox
809 * Pops up the menu or dropdown list of @combo_box.
811 * This function is mostly intended for use by accessibility technologies;
812 * applications should have little use for it.
817 gtk_combo_box_popup (GtkComboBox *combo_box)
819 gint x, y, width, height;
822 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
824 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
827 if (GTK_IS_MENU (combo_box->priv->popup_widget))
829 if (combo_box->priv->active_item != -1)
833 childs = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
834 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
835 g_list_nth_data (childs, combo_box->priv->active_item));
836 g_list_free (childs);
839 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
841 gtk_combo_box_menu_position, combo_box,
847 sample = GTK_BIN (combo_box)->child;
849 width = sample->allocation.width;
850 height = sample->allocation.height;
852 gdk_window_get_origin (sample->window,
855 if (combo_box->priv->cell_view_frame)
857 x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
858 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
859 width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
860 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
863 gtk_widget_set_size_request (combo_box->priv->popup_window,
866 if (GTK_WIDGET_NO_WINDOW (sample))
868 x += sample->allocation.x;
869 y += sample->allocation.y;
872 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window),
876 gtk_widget_show_all (combo_box->priv->popup_window);
878 gtk_widget_grab_focus (combo_box->priv->popup_window);
879 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
882 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
884 gdk_keyboard_grab (combo_box->priv->popup_window->window,
885 FALSE, GDK_CURRENT_TIME);
886 gtk_widget_grab_focus (combo_box->priv->tree_view);
891 * gtk_combo_box_popdown:
892 * @combo_box: a #GtkComboBox
894 * Hides the menu or dropdown list of @combo_box.
896 * This function is mostly intended for use by accessibility technologies;
897 * applications should have little use for it.
902 gtk_combo_box_popdown (GtkComboBox *combo_box)
904 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
906 if (GTK_IS_MENU (combo_box->priv->popup_widget))
908 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
912 gtk_widget_hide_all (combo_box->priv->popup_window);
913 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
918 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
924 if (combo_box->priv->cell_view)
925 gtk_widget_style_get (combo_box->priv->cell_view,
926 "focus-line-width", &padding,
931 /* add some pixels for good measure */
932 padding += BONUS_PADDING;
934 if (combo_box->priv->cell_view)
935 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
940 return req.width + padding;
944 gtk_combo_box_remeasure (GtkComboBox *combo_box)
950 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
953 combo_box->priv->width = 0;
955 path = gtk_tree_path_new_from_indices (0, -1);
957 if (combo_box->priv->cell_view)
958 gtk_widget_style_get (combo_box->priv->cell_view,
959 "focus-line-width", &padding,
964 /* add some pixels for good measure */
965 padding += BONUS_PADDING;
971 if (combo_box->priv->cell_view)
972 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
977 combo_box->priv->width = MAX (combo_box->priv->width,
978 req.width + padding);
980 gtk_tree_path_next (path);
982 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
984 gtk_tree_path_free (path);
988 gtk_combo_box_size_request (GtkWidget *widget,
989 GtkRequisition *requisition)
992 GtkRequisition bin_req;
994 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
997 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
998 gtk_combo_box_remeasure (combo_box);
999 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1001 if (!combo_box->priv->tree_view)
1005 if (combo_box->priv->cell_view)
1007 GtkRequisition sep_req, arrow_req;
1008 gint border_width, xthickness, ythickness;
1010 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1011 xthickness = combo_box->priv->button->style->xthickness;
1012 ythickness = combo_box->priv->button->style->ythickness;
1014 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1016 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1017 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1019 height = MAX (sep_req.height, arrow_req.height);
1020 height = MAX (height, bin_req.height);
1022 width = bin_req.width + sep_req.width + arrow_req.width;
1024 height += border_width + 1 + xthickness * 2 + 4;
1025 width += border_width + 1 + ythickness * 2 + 4;
1027 requisition->width = width;
1028 requisition->height = height;
1032 GtkRequisition but_req;
1034 gtk_widget_size_request (combo_box->priv->button, &but_req);
1036 requisition->width = bin_req.width + but_req.width;
1037 requisition->height = MAX (bin_req.height, but_req.height);
1043 GtkRequisition button_req;
1045 /* sample + frame */
1046 *requisition = bin_req;
1048 if (combo_box->priv->cell_view_frame)
1050 requisition->width += 2 *
1051 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1052 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1053 requisition->height += 2 *
1054 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1055 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1059 gtk_widget_size_request (combo_box->priv->button, &button_req);
1061 requisition->height = MAX (requisition->height, button_req.height);
1062 requisition->width += button_req.width;
1067 gtk_combo_box_size_allocate (GtkWidget *widget,
1068 GtkAllocation *allocation)
1070 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1071 GtkAllocation child;
1073 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1075 widget->allocation = *allocation;
1077 if (!combo_box->priv->tree_view)
1079 if (combo_box->priv->cell_view)
1081 gint border_width, xthickness, ythickness;
1085 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1087 /* set some things ready */
1088 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1089 xthickness = combo_box->priv->button->style->xthickness;
1090 ythickness = combo_box->priv->button->style->ythickness;
1092 child.x = allocation->x + border_width + 1 + xthickness + 2;
1093 child.y = allocation->y + border_width + 1 + ythickness + 2;
1095 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1097 /* handle the childs */
1098 gtk_widget_size_request (combo_box->priv->arrow, &req);
1099 child.width = req.width;
1100 child.height = allocation->height - 2 * (child.y - allocation->y);
1102 child.x += width - req.width;
1103 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1105 child.x += req.width;
1106 gtk_widget_size_request (combo_box->priv->separator, &req);
1107 child.width = req.width;
1109 child.x -= req.width;
1110 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1114 child.x += req.width;
1115 child.width = allocation->x + allocation->width
1116 - (border_width + 1 + xthickness + 2) - child.x;
1120 child.width = child.x;
1121 child.x = allocation->x + border_width + 1 + xthickness + 2;
1122 child.width -= child.x;
1125 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1129 gtk_widget_size_request (combo_box->priv->button, &req);
1131 child.x = allocation->x;
1133 child.x = allocation->x + allocation->width - req.width;
1134 child.y = allocation->y;
1135 child.width = req.width;
1136 child.height = allocation->height;
1137 gtk_widget_size_allocate (combo_box->priv->button, &child);
1140 child.x = allocation->x + req.width;
1142 child.x = allocation->x;
1143 child.y = allocation->y;
1144 child.width = allocation->width - req.width;
1145 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1153 gtk_widget_size_request (combo_box->priv->button, &req);
1155 child.x = allocation->x;
1157 child.x = allocation->x + allocation->width - req.width;
1158 child.y = allocation->y;
1159 child.width = req.width;
1160 child.height = allocation->height;
1161 gtk_widget_size_allocate (combo_box->priv->button, &child);
1165 child.x = allocation->x + req.width;
1167 child.x = allocation->x;
1168 child.y = allocation->y;
1169 child.width = allocation->width - req.width;
1170 child.height = allocation->height;
1172 if (combo_box->priv->cell_view_frame)
1174 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1178 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1179 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1181 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1182 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1183 child.width -= 2 * (
1184 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1185 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1186 child.height -= 2 * (
1187 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1188 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1191 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1196 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1198 if (combo_box->priv->inserted_id != -1)
1200 g_signal_handler_disconnect (combo_box->priv->model,
1201 combo_box->priv->inserted_id);
1202 combo_box->priv->inserted_id = -1;
1204 if (combo_box->priv->deleted_id != -1)
1206 g_signal_handler_disconnect (combo_box->priv->model,
1207 combo_box->priv->deleted_id);
1208 combo_box->priv->deleted_id = -1;
1210 if (combo_box->priv->changed_id != -1)
1212 g_signal_handler_disconnect (combo_box->priv->model,
1213 combo_box->priv->changed_id);
1214 combo_box->priv->changed_id = -1;
1218 if (!combo_box->priv->tree_view)
1220 if (combo_box->priv->popup_widget)
1221 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1222 (GtkCallback)gtk_widget_destroy, NULL);
1227 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1229 if (!combo_box->priv->tree_view)
1232 combo_box->priv->inserted_id =
1233 g_signal_connect (combo_box->priv->model, "row_inserted",
1234 G_CALLBACK (gtk_combo_box_menu_row_inserted),
1236 combo_box->priv->deleted_id =
1237 g_signal_connect (combo_box->priv->model, "row_deleted",
1238 G_CALLBACK (gtk_combo_box_menu_row_deleted),
1240 combo_box->priv->changed_id =
1241 g_signal_connect (combo_box->priv->model, "row_changed",
1242 G_CALLBACK (gtk_combo_box_menu_row_changed),
1248 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1249 combo_box->priv->model);
1251 combo_box->priv->changed_id =
1252 g_signal_connect (combo_box->priv->model, "row_changed",
1253 G_CALLBACK (gtk_combo_box_list_row_changed),
1259 gtk_combo_box_forall (GtkContainer *container,
1260 gboolean include_internals,
1261 GtkCallback callback,
1262 gpointer callback_data)
1264 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1266 if (include_internals)
1268 if (combo_box->priv->button)
1269 (* callback) (combo_box->priv->button, callback_data);
1270 if (combo_box->priv->separator)
1271 (* callback) (combo_box->priv->separator, callback_data);
1272 if (combo_box->priv->arrow)
1273 (* callback) (combo_box->priv->arrow, callback_data);
1274 if (combo_box->priv->cell_view_frame)
1275 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1278 if (GTK_BIN (container)->child)
1279 (* callback) (GTK_BIN (container)->child, callback_data);
1283 gtk_combo_box_expose_event (GtkWidget *widget,
1284 GdkEventExpose *event)
1286 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1288 if (!combo_box->priv->tree_view)
1290 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1291 combo_box->priv->button, event);
1293 if (combo_box->priv->separator)
1295 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1296 combo_box->priv->separator, event);
1298 /* if not in this case, arrow gets its expose event from button */
1299 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1300 combo_box->priv->arrow, event);
1305 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1306 combo_box->priv->button, event);
1308 if (combo_box->priv->cell_view_frame)
1309 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1310 combo_box->priv->cell_view_frame, event);
1313 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1314 GTK_BIN (widget)->child, event);
1320 gtk_combo_box_scroll_event (GtkWidget *widget,
1321 GdkEventScroll *event)
1323 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1327 index = gtk_combo_box_get_active (combo_box);
1331 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1333 if (event->direction == GDK_SCROLL_UP)
1338 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1349 cell_view_sync_cells (GtkComboBox *combo_box,
1350 GtkCellView *cell_view)
1354 for (k = combo_box->priv->cells; k; k = k->next)
1357 ComboCellInfo *info = (ComboCellInfo *)k->data;
1359 if (info->pack == GTK_PACK_START)
1360 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1361 info->cell, info->expand);
1362 else if (info->pack == GTK_PACK_END)
1363 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1364 info->cell, info->expand);
1366 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1368 info->func, info->func_data, NULL);
1370 for (j = info->attributes; j; j = j->next->next)
1372 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1375 GPOINTER_TO_INT (j->next->data));
1381 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1382 gboolean add_childs)
1386 if (combo_box->priv->cell_view)
1388 combo_box->priv->button = gtk_toggle_button_new ();
1389 g_signal_connect (combo_box->priv->button, "toggled",
1390 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1391 gtk_widget_set_parent (combo_box->priv->button,
1392 GTK_BIN (combo_box)->child->parent);
1394 combo_box->priv->separator = gtk_vseparator_new ();
1395 gtk_widget_set_parent (combo_box->priv->separator,
1396 combo_box->priv->button);
1397 gtk_widget_show (combo_box->priv->separator);
1399 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1400 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1401 gtk_widget_show (combo_box->priv->arrow);
1403 gtk_widget_show_all (combo_box->priv->button);
1405 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1407 /* I have no clue why, but we need to manually map in this case. */
1408 gtk_widget_map (combo_box->priv->separator);
1409 gtk_widget_map (combo_box->priv->arrow);
1414 combo_box->priv->button = gtk_toggle_button_new ();
1415 g_signal_connect (combo_box->priv->button, "toggled",
1416 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1417 gtk_widget_set_parent (combo_box->priv->button,
1418 GTK_BIN (combo_box)->child->parent);
1420 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1421 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1422 combo_box->priv->arrow);
1423 gtk_widget_show_all (combo_box->priv->button);
1426 g_signal_connect (combo_box->priv->button, "button_press_event",
1427 G_CALLBACK (gtk_combo_box_menu_button_press),
1430 /* create our funky menu */
1431 box = gtk_menu_new ();
1432 gtk_combo_box_set_popup_widget (combo_box, box);
1434 /* set the models */
1435 gtk_combo_box_set_model_internal (combo_box);
1439 gtk_combo_box_menu_fill (combo_box);
1444 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1450 if (!combo_box->priv->model)
1453 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1454 menu = combo_box->priv->popup_widget;
1456 for (i = 0; i < items; i++)
1460 path = gtk_tree_path_new_from_indices (i, -1);
1461 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1463 g_signal_connect (tmp, "activate",
1464 G_CALLBACK (gtk_combo_box_menu_item_activate),
1467 cell_view_sync_cells (combo_box,
1468 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1470 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1471 gtk_widget_show (tmp);
1473 gtk_tree_path_free (path);
1478 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1480 /* disconnect signal handlers */
1481 gtk_combo_box_unset_model (combo_box);
1483 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1484 G_SIGNAL_MATCH_DATA,
1486 gtk_combo_box_menu_button_press, NULL);
1488 /* unparent will remove our latest ref */
1489 if (combo_box->priv->cell_view)
1491 gtk_widget_unparent (combo_box->priv->arrow);
1492 gtk_widget_unparent (combo_box->priv->separator);
1493 gtk_widget_unparent (combo_box->priv->button);
1497 /* will destroy the arrow too */
1498 gtk_widget_unparent (combo_box->priv->button);
1501 /* changing the popup window will unref the menu and the childs */
1509 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1516 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1520 if (combo_box->priv->col_column == -1)
1523 gtk_tree_model_get (combo_box->priv->model, &iter,
1524 combo_box->priv->col_column, cols,
1530 if (combo_box->priv->row_column == -1)
1533 gtk_tree_model_get (combo_box->priv->model, &iter,
1534 combo_box->priv->row_column, rows,
1540 menu_occupied (GtkMenu *menu,
1544 guint bottom_attach)
1548 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1549 g_return_val_if_fail (left_attach < right_attach, TRUE);
1550 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1552 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1555 gboolean h_intersect = FALSE;
1556 gboolean v_intersect = FALSE;
1558 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1561 "bottom_attach", &b,
1565 /* look if this item intersects with the given coordinates */
1566 h_intersect = left_attach <= l && l <= right_attach;
1567 h_intersect &= left_attach <= r && r <= right_attach;
1569 v_intersect = top_attach <= t && t <= bottom_attach;
1570 v_intersect &= top_attach <= b && b <= bottom_attach;
1572 if (h_intersect && v_intersect)
1580 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1583 gint current_col = 0, current_row = 0;
1589 menu = combo_box->priv->popup_widget;
1590 if (!GTK_IS_MENU_SHELL (menu))
1593 list = gtk_container_get_children (GTK_CONTAINER (menu));
1594 item = g_list_nth_data (list, index);
1597 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1599 /* look for a good spot */
1602 if (current_col + cols > combo_box->priv->wrap_width)
1608 if (!menu_occupied (GTK_MENU (menu),
1609 current_col, current_col + cols,
1610 current_row, current_row + rows))
1616 /* set attach props */
1617 gtk_menu_attach (GTK_MENU (menu), item,
1618 current_col, current_col + cols,
1619 current_row, current_row + rows);
1623 gtk_combo_box_relayout (GtkComboBox *combo_box)
1629 /* ensure we are in menu style */
1630 if (combo_box->priv->tree_view)
1631 gtk_combo_box_list_destroy (combo_box);
1633 gtk_combo_box_unset_model (combo_box);
1635 menu = combo_box->priv->popup_widget;
1637 if (!GTK_IS_MENU_SHELL (menu))
1639 gtk_combo_box_menu_setup (combo_box, FALSE);
1640 menu = combo_box->priv->popup_widget;
1643 /* get rid of all children */
1644 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1646 list = gtk_container_get_children (GTK_CONTAINER (menu));
1648 for (j = g_list_last (list); j; j = j->prev)
1649 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1654 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1656 for (i = 0; i < items; i++)
1661 path = gtk_tree_path_new_from_indices (i, -1);
1662 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1665 g_signal_connect (tmp, "activate",
1666 G_CALLBACK (gtk_combo_box_menu_item_activate),
1669 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1671 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1673 if (combo_box->priv->wrap_width)
1674 gtk_combo_box_relayout_item (combo_box, i);
1676 gtk_widget_show (tmp);
1678 gtk_tree_path_free (path);
1684 gtk_combo_box_menu_button_press (GtkWidget *widget,
1685 GdkEventButton *event,
1688 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1690 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1693 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1695 combo_box->priv->popup_in_progress = TRUE;
1696 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1698 gtk_combo_box_menu_position, combo_box,
1699 event->button, event->time);
1708 gtk_combo_box_menu_item_activate (GtkWidget *item,
1713 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1715 menu = combo_box->priv->popup_widget;
1716 g_return_if_fail (GTK_IS_MENU (menu));
1718 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1720 gtk_combo_box_set_active (combo_box, index);
1724 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
1731 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1733 if (!combo_box->priv->popup_widget)
1736 menu = combo_box->priv->popup_widget;
1737 g_return_if_fail (GTK_IS_MENU (menu));
1739 item = gtk_cell_view_menu_item_new_from_model (model, path);
1740 g_signal_connect (item, "activate",
1741 G_CALLBACK (gtk_combo_box_menu_item_activate),
1744 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
1746 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
1747 gtk_tree_path_get_indices (path)[0]);
1748 gtk_widget_show (item);
1752 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
1759 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1761 if (!combo_box->priv->popup_widget)
1764 index = gtk_tree_path_get_indices (path)[0];
1765 items = gtk_tree_model_iter_n_children (model, NULL);
1767 if (gtk_combo_box_get_active (combo_box) == index)
1768 gtk_combo_box_set_active (combo_box, index + 1 % items);
1770 menu = combo_box->priv->popup_widget;
1771 g_return_if_fail (GTK_IS_MENU (menu));
1773 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
1774 g_return_if_fail (GTK_IS_MENU_ITEM (item));
1776 gtk_container_remove (GTK_CONTAINER (menu), item);
1780 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
1785 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1788 if (!combo_box->priv->popup_widget)
1791 if (combo_box->priv->wrap_width)
1792 gtk_combo_box_relayout_item (combo_box,
1793 gtk_tree_path_get_indices (path)[0]);
1795 width = gtk_combo_box_calc_requested_width (combo_box, path);
1797 if (width > combo_box->priv->width)
1799 if (combo_box->priv->cell_view)
1801 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
1802 gtk_widget_queue_resize (combo_box->priv->cell_view);
1804 combo_box->priv->width = width;
1813 gtk_combo_box_list_setup (GtkComboBox *combo_box)
1816 GtkTreeSelection *sel;
1818 combo_box->priv->button = gtk_toggle_button_new ();
1819 gtk_widget_set_parent (combo_box->priv->button,
1820 GTK_BIN (combo_box)->child->parent);
1821 g_signal_connect (combo_box->priv->button, "button_press_event",
1822 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
1823 g_signal_connect (combo_box->priv->button, "toggled",
1824 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1826 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1827 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1828 combo_box->priv->arrow);
1829 gtk_widget_show_all (combo_box->priv->button);
1831 if (combo_box->priv->cell_view)
1833 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
1834 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
1835 GTK_BIN (combo_box)->child->parent);
1836 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
1839 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1840 "background", "white",
1841 "background_set", TRUE,
1844 gtk_widget_show (combo_box->priv->cell_view_frame);
1847 combo_box->priv->tree_view = gtk_tree_view_new ();
1848 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
1849 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
1850 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
1853 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
1854 G_CALLBACK (gtk_combo_box_list_button_pressed),
1856 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
1857 G_CALLBACK (gtk_combo_box_list_button_released),
1859 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
1860 G_CALLBACK (gtk_combo_box_list_key_press),
1863 combo_box->priv->column = gtk_tree_view_column_new ();
1864 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
1865 combo_box->priv->column);
1867 /* set the models */
1868 gtk_combo_box_set_model_internal (combo_box);
1871 for (i = combo_box->priv->cells; i; i = i->next)
1874 ComboCellInfo *info = (ComboCellInfo *)i->data;
1876 if (info->pack == GTK_PACK_START)
1877 gtk_tree_view_column_pack_start (combo_box->priv->column,
1878 info->cell, info->expand);
1879 else if (info->pack == GTK_PACK_END)
1880 gtk_tree_view_column_pack_end (combo_box->priv->column,
1881 info->cell, info->expand);
1883 for (j = info->attributes; j; j = j->next->next)
1885 gtk_tree_view_column_add_attribute (combo_box->priv->column,
1888 GPOINTER_TO_INT (j->next->data));
1892 if (combo_box->priv->active_item != -1)
1896 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
1899 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1901 gtk_tree_path_free (path);
1905 /* set sample/popup widgets */
1906 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
1908 gtk_widget_show (combo_box->priv->tree_view);
1912 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
1914 /* disconnect signals */
1915 gtk_combo_box_unset_model (combo_box);
1917 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
1918 G_SIGNAL_MATCH_DATA,
1919 0, 0, NULL, NULL, combo_box);
1920 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1921 G_SIGNAL_MATCH_DATA,
1923 gtk_combo_box_list_button_pressed,
1926 /* destroy things (unparent will kill the latest ref from us)
1927 * last unref on button will destroy the arrow
1929 gtk_widget_unparent (combo_box->priv->button);
1931 if (combo_box->priv->cell_view)
1933 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1934 "background_set", FALSE,
1937 gtk_widget_unparent (combo_box->priv->cell_view_frame);
1940 gtk_widget_destroy (combo_box->priv->tree_view);
1941 combo_box->priv->tree_view = NULL;
1942 combo_box->priv->popup_widget = NULL;
1947 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
1949 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
1950 gtk_grab_remove (combo_box->priv->tree_view);
1952 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
1954 gtk_grab_remove (combo_box->priv->popup_window);
1955 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1960 gtk_combo_box_list_button_pressed (GtkWidget *widget,
1961 GdkEventButton *event,
1964 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1966 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
1968 if (ewidget == combo_box->priv->tree_view)
1971 if ((ewidget != combo_box->priv->button) ||
1972 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
1975 gtk_combo_box_popup (combo_box);
1977 gtk_grab_add (combo_box->priv->popup_window);
1978 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1979 GDK_BUTTON_PRESS_MASK |
1980 GDK_BUTTON_RELEASE_MASK |
1981 GDK_POINTER_MOTION_MASK,
1982 NULL, NULL, GDK_CURRENT_TIME);
1984 gtk_grab_add (combo_box->priv->tree_view);
1986 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1989 combo_box->priv->popup_in_progress = TRUE;
1995 gtk_combo_box_list_button_released (GtkWidget *widget,
1996 GdkEventButton *event,
2000 GtkTreePath *path = NULL;
2002 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2004 gboolean popup_in_progress = FALSE;
2006 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2008 if (combo_box->priv->popup_in_progress)
2010 popup_in_progress = TRUE;
2011 combo_box->priv->popup_in_progress = FALSE;
2014 if (ewidget != combo_box->priv->tree_view)
2016 if (ewidget == combo_box->priv->button &&
2017 !popup_in_progress &&
2018 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2020 gtk_combo_box_list_remove_grabs (combo_box);
2021 gtk_combo_box_popdown (combo_box);
2025 /* released outside treeview */
2026 if (ewidget != combo_box->priv->button)
2028 gtk_combo_box_list_remove_grabs (combo_box);
2029 gtk_combo_box_popdown (combo_box);
2038 gtk_combo_box_list_remove_grabs (combo_box);
2040 /* select something cool */
2041 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2047 return TRUE; /* clicked outside window? */
2049 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2050 gtk_combo_box_popdown (combo_box);
2052 gtk_tree_path_free (path);
2058 gtk_combo_box_list_key_press (GtkWidget *widget,
2062 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2064 if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2065 event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
2066 event->keyval == GDK_Escape)
2068 if (event->keyval != GDK_Escape)
2072 GtkTreeModel *model = NULL;
2073 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2075 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2080 path = gtk_tree_model_get_path (model, &iter);
2083 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2084 gtk_tree_path_free (path);
2089 /* reset active item -- this is incredibly lame and ugly */
2090 gtk_combo_box_set_active (combo_box,
2091 gtk_combo_box_get_active (combo_box));
2093 gtk_combo_box_list_remove_grabs (combo_box);
2094 gtk_combo_box_popdown (combo_box);
2103 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2108 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2111 width = gtk_combo_box_calc_requested_width (combo_box, path);
2113 if (width > combo_box->priv->width)
2115 if (combo_box->priv->cell_view)
2117 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2118 gtk_widget_queue_resize (combo_box->priv->cell_view);
2120 combo_box->priv->width = width;
2125 * GtkCellLayout implementation
2128 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2129 GtkCellRenderer *cell,
2132 ComboCellInfo *info;
2133 GtkComboBox *combo_box;
2136 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2137 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2139 combo_box = GTK_COMBO_BOX (layout);
2141 g_object_ref (G_OBJECT (cell));
2142 gtk_object_sink (GTK_OBJECT (cell));
2144 info = g_new0 (ComboCellInfo, 1);
2146 info->expand = expand;
2147 info->pack = GTK_PACK_START;
2149 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2151 if (combo_box->priv->cell_view)
2152 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2155 if (combo_box->priv->column)
2156 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2158 menu = combo_box->priv->popup_widget;
2159 if (GTK_IS_MENU (menu))
2163 list = gtk_container_get_children (GTK_CONTAINER (menu));
2164 for (i = list; i; i = i->next)
2168 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2169 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2171 view = GTK_CELL_VIEW (i->data);
2173 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2180 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2181 GtkCellRenderer *cell,
2184 ComboCellInfo *info;
2185 GtkComboBox *combo_box;
2188 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2189 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2191 combo_box = GTK_COMBO_BOX (layout);
2193 g_object_ref (G_OBJECT (cell));
2194 gtk_object_sink (GTK_OBJECT (cell));
2196 info = g_new0 (ComboCellInfo, 1);
2198 info->expand = expand;
2199 info->pack = GTK_PACK_END;
2201 if (combo_box->priv->cell_view)
2202 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2205 if (combo_box->priv->column)
2206 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2208 menu = combo_box->priv->popup_widget;
2209 if (GTK_IS_MENU (menu))
2213 list = gtk_container_get_children (GTK_CONTAINER (menu));
2214 for (i = list; i; i = i->next)
2218 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2219 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2221 view = GTK_CELL_VIEW (i->data);
2223 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2230 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2233 GtkComboBox *combo_box;
2236 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2238 combo_box = GTK_COMBO_BOX (layout);
2240 if (combo_box->priv->cell_view)
2241 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2243 if (combo_box->priv->column)
2244 gtk_tree_view_column_clear (combo_box->priv->column);
2246 for (i = combo_box->priv->cells; i; i = i->next)
2248 ComboCellInfo *info = (ComboCellInfo *)i->data;
2250 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2251 g_object_unref (G_OBJECT (info->cell));
2254 g_slist_free (combo_box->priv->cells);
2255 combo_box->priv->cells = NULL;
2257 menu = combo_box->priv->popup_widget;
2258 if (GTK_IS_MENU (menu))
2262 list = gtk_container_get_children (GTK_CONTAINER (menu));
2263 for (i = list; i; i = i->next)
2267 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2268 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2270 view = GTK_CELL_VIEW (i->data);
2272 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2279 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2280 GtkCellRenderer *cell,
2281 const gchar *attribute,
2284 ComboCellInfo *info;
2285 GtkComboBox *combo_box;
2288 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2289 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2291 combo_box = GTK_COMBO_BOX (layout);
2293 info = gtk_combo_box_get_cell_info (combo_box, cell);
2295 info->attributes = g_slist_prepend (info->attributes,
2296 GINT_TO_POINTER (column));
2297 info->attributes = g_slist_prepend (info->attributes,
2298 g_strdup (attribute));
2300 if (combo_box->priv->cell_view)
2301 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2302 cell, attribute, column);
2304 if (combo_box->priv->column)
2305 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2306 cell, attribute, column);
2308 menu = combo_box->priv->popup_widget;
2309 if (GTK_IS_MENU (menu))
2313 list = gtk_container_get_children (GTK_CONTAINER (menu));
2314 for (i = list; i; i = i->next)
2318 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2319 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2321 view = GTK_CELL_VIEW (i->data);
2323 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2329 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2333 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2334 GtkCellRenderer *cell,
2335 GtkCellLayoutDataFunc func,
2337 GDestroyNotify destroy)
2339 ComboCellInfo *info;
2340 GtkComboBox *combo_box;
2343 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2345 combo_box = GTK_COMBO_BOX (layout);
2347 info = gtk_combo_box_get_cell_info (combo_box, cell);
2348 g_return_if_fail (info != NULL);
2352 GDestroyNotify d = info->destroy;
2354 info->destroy = NULL;
2355 d (info->func_data);
2359 info->func_data = func_data;
2360 info->destroy = destroy;
2362 if (combo_box->priv->cell_view)
2363 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2365 if (combo_box->priv->column)
2366 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2368 menu = combo_box->priv->popup_widget;
2369 if (GTK_IS_MENU (menu))
2373 list = gtk_container_get_children (GTK_CONTAINER (menu));
2374 for (i = list; i; i = i->next)
2378 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2379 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2381 view = GTK_CELL_VIEW (i->data);
2383 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2384 func, func_data, NULL);
2389 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2393 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2394 GtkCellRenderer *cell)
2396 ComboCellInfo *info;
2397 GtkComboBox *combo_box;
2401 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2402 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2404 combo_box = GTK_COMBO_BOX (layout);
2406 info = gtk_combo_box_get_cell_info (combo_box, cell);
2407 g_return_if_fail (info != NULL);
2409 list = info->attributes;
2410 while (list && list->next)
2412 g_free (list->data);
2413 list = list->next->next;
2415 g_slist_free (info->attributes);
2416 info->attributes = NULL;
2418 if (combo_box->priv->cell_view)
2419 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2421 if (combo_box->priv->column)
2422 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2424 menu = combo_box->priv->popup_widget;
2425 if (GTK_IS_MENU (menu))
2429 list = gtk_container_get_children (GTK_CONTAINER (menu));
2430 for (i = list; i; i = i->next)
2434 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2435 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2437 view = GTK_CELL_VIEW (i->data);
2439 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2444 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2448 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2449 GtkCellRenderer *cell,
2452 ComboCellInfo *info;
2453 GtkComboBox *combo_box;
2457 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2458 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2460 combo_box = GTK_COMBO_BOX (layout);
2462 info = gtk_combo_box_get_cell_info (combo_box, cell);
2464 g_return_if_fail (info != NULL);
2465 g_return_if_fail (position >= 0);
2467 link = g_slist_find (combo_box->priv->cells, info);
2469 g_return_if_fail (link != NULL);
2471 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2472 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2475 if (combo_box->priv->cell_view)
2476 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2479 if (combo_box->priv->column)
2480 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2483 menu = combo_box->priv->popup_widget;
2484 if (GTK_IS_MENU (menu))
2488 list = gtk_container_get_children (GTK_CONTAINER (menu));
2489 for (i = list; i; i = i->next)
2493 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2494 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2496 view = GTK_CELL_VIEW (i->data);
2498 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2503 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2511 * gtk_combo_box_new:
2513 * Creates a new empty #GtkComboBox.
2515 * Return value: A new #GtkComboBox.
2520 gtk_combo_box_new (void)
2522 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2526 * gtk_combo_box_new_with_model:
2527 * @model: A #GtkTreeModel.
2529 * Creates a new #GtkComboBox with the model initialized to @model.
2531 * Return value: A new #GtkComboBox.
2536 gtk_combo_box_new_with_model (GtkTreeModel *model)
2538 GtkComboBox *combo_box;
2540 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2542 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2546 return GTK_WIDGET (combo_box);
2550 * gtk_combo_box_set_wrap_width:
2551 * @combo_box: A #GtkComboBox.
2552 * @width: Preferred number of columns.
2554 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2555 * the preferred number of columns when you want to the popup to be layed out
2561 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2564 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2565 g_return_if_fail (width > 0);
2567 if (width != combo_box->priv->wrap_width)
2569 combo_box->priv->wrap_width = width;
2571 gtk_combo_box_relayout (combo_box);
2573 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2578 * gtk_combo_box_set_row_span_column:
2579 * @combo_box: A #GtkComboBox.
2580 * @row_span: A column in the model passed during construction.
2582 * Sets the column with row span information for @combo_box to be @row_span.
2583 * The row span column contains integers which indicate how many rows
2584 * an item should span.
2589 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2594 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2596 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2597 g_return_if_fail (row_span >= 0 && row_span < col);
2599 if (row_span != combo_box->priv->row_column)
2601 combo_box->priv->row_column = row_span;
2603 gtk_combo_box_relayout (combo_box);
2605 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2610 * gtk_combo_box_set_column_span_column:
2611 * @combo_box: A #GtkComboBox.
2612 * @column_span: A column in the model passed during construction.
2614 * Sets the column with column span information for @combo_box to be
2615 * @column_span. The column span column contains integers which indicate
2616 * how many columns an item should span.
2621 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2626 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2628 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2629 g_return_if_fail (column_span >= 0 && column_span < col);
2631 if (column_span != combo_box->priv->col_column)
2633 combo_box->priv->col_column = column_span;
2635 gtk_combo_box_relayout (combo_box);
2637 g_object_notify (G_OBJECT (combo_box), "column_span_column");
2642 * gtk_combo_box_get_active:
2643 * @combo_box: A #GtkComboBox.
2645 * Returns the index of the currently active item, or -1 if there's no
2648 * Return value: An integer which is the index of the currently active item, or
2649 * -1 if there's no active item.
2654 gtk_combo_box_get_active (GtkComboBox *combo_box)
2656 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
2658 return combo_box->priv->active_item;
2662 * gtk_combo_box_set_active:
2663 * @combo_box: A #GtkComboBox.
2664 * @index: An index in the model passed during construction, or -1 to have
2667 * Sets the active item of @combo_box to be the item at @index.
2672 gtk_combo_box_set_active (GtkComboBox *combo_box,
2677 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2678 /* -1 means "no item selected" */
2679 g_return_if_fail (index >= -1);
2681 if (combo_box->priv->active_item == index)
2684 combo_box->priv->active_item = index;
2688 if (combo_box->priv->tree_view)
2689 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
2692 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2694 if (GTK_IS_MENU (menu))
2695 gtk_menu_set_active (menu, -1);
2698 if (combo_box->priv->cell_view)
2699 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2703 path = gtk_tree_path_new_from_indices (index, -1);
2705 if (combo_box->priv->tree_view)
2706 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
2709 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2711 if (GTK_IS_MENU (menu))
2712 gtk_menu_set_active (GTK_MENU (menu), index);
2715 if (combo_box->priv->cell_view)
2716 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
2718 gtk_tree_path_free (path);
2721 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
2726 * gtk_combo_box_get_active_iter:
2727 * @combo_box: A #GtkComboBox
2728 * @iter: The uninitialized #GtkTreeIter.
2730 * Set @iter to point to the current active item, if it exists.
2732 * Return value: %TRUE, if @iter was set
2737 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
2744 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
2746 active = gtk_combo_box_get_active (combo_box);
2750 path = gtk_tree_path_new_from_indices (active, -1);
2751 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
2753 gtk_tree_path_free (path);
2759 * gtk_combo_box_set_active_iter:
2760 * @combo_box: A #GtkComboBox
2761 * @iter: The #GtkTreeIter.
2763 * Sets the current active item to be the one referenced by @iter.
2764 * @iter must correspond to a path of depth one.
2769 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
2774 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2776 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
2777 g_return_if_fail (path != NULL);
2778 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
2780 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2781 gtk_tree_path_free (path);
2785 * gtk_combo_box_set_model:
2786 * @combo_box: A #GtkComboBox.
2787 * @model: A #GtkTreeModel.
2789 * Sets the model used by @combo_box to be @model. Will unset a
2790 * previously set model (if applicable).
2795 gtk_combo_box_set_model (GtkComboBox *combo_box,
2796 GtkTreeModel *model)
2798 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2799 g_return_if_fail (GTK_IS_TREE_MODEL (model));
2801 if (combo_box->priv->model)
2803 gtk_combo_box_unset_model (combo_box);
2804 g_object_unref (G_OBJECT (combo_box->priv->model));
2807 combo_box->priv->model = model;
2808 g_object_ref (G_OBJECT (combo_box->priv->model));
2810 gtk_combo_box_set_model_internal (combo_box);
2811 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
2812 gtk_combo_box_menu_fill (combo_box);
2814 if (combo_box->priv->cell_view)
2815 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
2816 combo_box->priv->model);
2820 * gtk_combo_box_get_model
2821 * @combo_box: A #GtkComboBox.
2823 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
2825 * Return value: A #GtkTreeModel which was passed during construction.
2830 gtk_combo_box_get_model (GtkComboBox *combo_box)
2832 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
2834 return combo_box->priv->model;
2838 /* convenience API for simple text combos */
2841 * gtk_combo_box_new_text:
2843 * Convenience function which constructs a new text combo box, which is a
2844 * #GtkComboBox just displaying strings. If you use this function to create
2845 * a text combo box, you only want to manipulate it's data source with the
2846 * following convenience functions: gtk_combo_box_append_text(),
2847 * gtk_combo_box_insert_text() and gtk_combo_box_prepend_text().
2849 * Return value: A new text combo box.
2854 gtk_combo_box_new_text (void)
2856 GtkWidget *combo_box;
2857 GtkCellRenderer *cell;
2858 GtkListStore *store;
2860 store = gtk_list_store_new (1, G_TYPE_STRING);
2862 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
2864 cell = gtk_cell_renderer_text_new ();
2865 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
2866 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
2874 * gtk_combo_box_append_text:
2875 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2878 * Appends @string to the list of strings stored in @combo_box. Note that
2879 * you can only use this function with combo boxes constructed with
2880 * gtk_combo_box_new_text().
2885 gtk_combo_box_append_text (GtkComboBox *combo_box,
2889 GtkListStore *store;
2891 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2892 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2893 g_return_if_fail (text != NULL);
2895 store = GTK_LIST_STORE (combo_box->priv->model);
2897 gtk_list_store_append (store, &iter);
2898 gtk_list_store_set (store, &iter, 0, text, -1);
2902 * gtk_combo_box_insert_text:
2903 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2904 * @position: An index to insert @text.
2907 * Inserts @string at @position in the list of strings stored in @combo_box.
2908 * Note that you can only use this function with combo boxes constructed
2909 * with gtk_combo_box_new_text().
2914 gtk_combo_box_insert_text (GtkComboBox *combo_box,
2919 GtkListStore *store;
2921 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2922 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2923 g_return_if_fail (position >= 0);
2924 g_return_if_fail (text != NULL);
2926 store = GTK_LIST_STORE (combo_box->priv->model);
2928 gtk_list_store_insert (store, &iter, position);
2929 gtk_list_store_set (store, &iter, 0, text, -1);
2933 * gtk_combo_box_prepend_text:
2934 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2937 * Prepends @string to the list of strings stored in @combo_box. Note that
2938 * you can only use this function with combo boxes constructed with
2939 * gtk_combo_box_new_text().
2944 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
2948 GtkListStore *store;
2950 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2951 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2952 g_return_if_fail (text != NULL);
2954 store = GTK_LIST_STORE (combo_box->priv->model);
2956 gtk_list_store_prepend (store, &iter);
2957 gtk_list_store_set (store, &iter, 0, text, -1);
2961 * gtk_combo_box_remove_text:
2962 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2963 * @position: Index of the item to remove.
2965 * Removes the string at @position from @combo_box. Note that you can only use
2966 * this function with combo boxes constructed with gtk_combo_box_new_text().
2971 gtk_combo_box_remove_text (GtkComboBox *combo_box,
2975 GtkListStore *store;
2977 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2978 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2979 g_return_if_fail (position >= 0);
2981 store = GTK_LIST_STORE (combo_box->priv->model);
2983 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
2985 gtk_list_store_remove (store, &iter);
2990 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
2991 gboolean group_cycling)
2993 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2995 gtk_widget_grab_focus (combo_box->priv->button);
3001 gtk_combo_box_destroy (GtkObject *object)
3003 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3005 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3007 combo_box->priv->cell_view = NULL;
3011 gtk_combo_box_finalize (GObject *object)
3013 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3016 gtk_combo_box_unset_model (combo_box);
3018 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3019 gtk_combo_box_menu_destroy (combo_box);
3021 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3022 gtk_combo_box_list_destroy (combo_box);
3024 if (combo_box->priv->popup_window)
3025 gtk_widget_destroy (combo_box->priv->popup_window);
3027 if (combo_box->priv->model)
3028 g_object_unref (combo_box->priv->model);
3030 for (i = combo_box->priv->cells; i; i = i->next)
3032 ComboCellInfo *info = (ComboCellInfo *)i->data;
3033 GSList *list = info->attributes;
3036 info->destroy (info->func_data);
3038 while (list && list->next)
3040 g_free (list->data);
3041 list = list->next->next;
3043 g_slist_free (info->attributes);
3045 g_object_unref (G_OBJECT (info->cell));
3048 g_slist_free (combo_box->priv->cells);
3050 G_OBJECT_CLASS (parent_class)->finalize (object);