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 void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
243 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
244 GdkEventButton *event,
246 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
249 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
250 GdkEventButton *event,
253 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
259 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
260 gboolean add_childs);
261 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
262 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
264 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
268 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
270 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
272 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
273 GdkEventButton *event,
275 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
277 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
281 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
284 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
290 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
291 GtkCellRenderer *cell,
293 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
294 GtkCellRenderer *cell,
296 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
297 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
298 GtkCellRenderer *cell,
299 const gchar *attribute,
301 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
302 GtkCellRenderer *cell,
303 GtkCellLayoutDataFunc func,
305 GDestroyNotify destroy);
306 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
307 GtkCellRenderer *cell);
308 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
309 GtkCellRenderer *cell,
311 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
312 gboolean group_cycling);
316 gtk_combo_box_get_type (void)
318 static GType combo_box_type = 0;
322 static const GTypeInfo combo_box_info =
324 sizeof (GtkComboBoxClass),
325 NULL, /* base_init */
326 NULL, /* base_finalize */
327 (GClassInitFunc) gtk_combo_box_class_init,
328 NULL, /* class_finalize */
329 NULL, /* class_data */
330 sizeof (GtkComboBox),
332 (GInstanceInitFunc) gtk_combo_box_init
335 static const GInterfaceInfo cell_layout_info =
337 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
342 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
347 g_type_add_interface_static (combo_box_type,
348 GTK_TYPE_CELL_LAYOUT,
352 return combo_box_type;
357 gtk_combo_box_class_init (GtkComboBoxClass *klass)
359 GObjectClass *object_class;
360 GtkBindingSet *binding_set;
361 GtkObjectClass *gtk_object_class;
362 GtkContainerClass *container_class;
363 GtkWidgetClass *widget_class;
365 binding_set = gtk_binding_set_by_class (klass);
367 container_class = (GtkContainerClass *)klass;
368 container_class->forall = gtk_combo_box_forall;
369 container_class->add = gtk_combo_box_add;
371 widget_class = (GtkWidgetClass *)klass;
372 widget_class->size_allocate = gtk_combo_box_size_allocate;
373 widget_class->size_request = gtk_combo_box_size_request;
374 widget_class->expose_event = gtk_combo_box_expose_event;
375 widget_class->scroll_event = gtk_combo_box_scroll_event;
376 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
378 gtk_object_class = (GtkObjectClass *)klass;
379 gtk_object_class->destroy = gtk_combo_box_destroy;
381 object_class = (GObjectClass *)klass;
382 object_class->finalize = gtk_combo_box_finalize;
383 object_class->set_property = gtk_combo_box_set_property;
384 object_class->get_property = gtk_combo_box_get_property;
386 parent_class = g_type_class_peek_parent (klass);
389 combo_box_signals[CHANGED] =
390 g_signal_new ("changed",
391 G_OBJECT_CLASS_TYPE (klass),
393 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
395 g_cclosure_marshal_VOID__VOID,
399 g_object_class_install_property (object_class,
401 g_param_spec_object ("model",
402 P_("ComboBox model"),
403 P_("The model for the combo box"),
407 g_object_class_install_property (object_class,
409 g_param_spec_int ("wrap_width",
411 P_("Wrap width for layouting the items in a grid"),
417 g_object_class_install_property (object_class,
418 PROP_ROW_SPAN_COLUMN,
419 g_param_spec_int ("row_span_column",
420 P_("Row span column"),
421 P_("TreeModel column containing the row span values"),
427 g_object_class_install_property (object_class,
428 PROP_COLUMN_SPAN_COLUMN,
429 g_param_spec_int ("column_span_column",
430 P_("Column span column"),
431 P_("TreeModel column containing the column span values"),
437 g_object_class_install_property (object_class,
439 g_param_spec_int ("active",
441 P_("The item which is currently active"),
447 gtk_widget_class_install_style_property (widget_class,
448 g_param_spec_boolean ("appearance",
449 P_("ComboBox appareance"),
450 P_("ComboBox appearance, where TRUE means Windows-style."),
454 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
458 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
460 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
461 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
462 iface->clear = gtk_combo_box_cell_layout_clear;
463 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
464 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
465 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
466 iface->reorder = gtk_combo_box_cell_layout_reorder;
470 gtk_combo_box_init (GtkComboBox *combo_box)
472 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
474 g_signal_connect (combo_box, "style_set",
475 G_CALLBACK (gtk_combo_box_style_set), NULL);
477 combo_box->priv->cell_view = gtk_cell_view_new ();
478 gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
479 gtk_widget_show (combo_box->priv->cell_view);
481 combo_box->priv->width = 0;
482 combo_box->priv->wrap_width = 0;
484 combo_box->priv->active_item = -1;
485 combo_box->priv->col_column = -1;
486 combo_box->priv->row_column = -1;
490 gtk_combo_box_set_property (GObject *object,
495 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
500 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
503 case PROP_WRAP_WIDTH:
504 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
507 case PROP_ROW_SPAN_COLUMN:
508 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
511 case PROP_COLUMN_SPAN_COLUMN:
512 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
516 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
525 gtk_combo_box_get_property (GObject *object,
530 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
535 g_value_set_object (value, combo_box->priv->model);
538 case PROP_WRAP_WIDTH:
539 g_value_set_int (value, combo_box->priv->wrap_width);
542 case PROP_ROW_SPAN_COLUMN:
543 g_value_set_int (value, combo_box->priv->row_column);
546 case PROP_COLUMN_SPAN_COLUMN:
547 g_value_set_int (value, combo_box->priv->col_column);
551 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
561 gtk_combo_box_style_set (GtkWidget *widget,
562 GtkStyle *previous_style,
566 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
568 gtk_widget_queue_resize (widget);
570 /* if wrap_width > 0, then we are in grid-mode and forced to use
573 if (combo_box->priv->wrap_width)
576 gtk_widget_style_get (widget,
577 "appearance", &appearance,
580 /* TRUE is windows style */
583 if (GTK_IS_MENU (combo_box->priv->popup_widget))
584 gtk_combo_box_menu_destroy (combo_box);
586 gtk_combo_box_unset_model (combo_box);
588 gtk_combo_box_list_setup (combo_box);
592 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
593 gtk_combo_box_list_destroy (combo_box);
595 gtk_combo_box_unset_model (combo_box);
597 gtk_combo_box_menu_setup (combo_box, TRUE);
602 gtk_combo_box_button_toggled (GtkWidget *widget,
605 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
607 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
609 if (!combo_box->priv->popup_in_progress)
610 gtk_combo_box_popup (combo_box);
613 gtk_combo_box_popdown (combo_box);
617 gtk_combo_box_add (GtkContainer *container,
620 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
622 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
623 gtk_container_remove (container, combo_box->priv->cell_view);
625 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
627 if (combo_box->priv->cell_view &&
628 widget != combo_box->priv->cell_view)
630 /* since the cell_view was unparented, it's gone now */
631 combo_box->priv->cell_view = NULL;
633 if (!combo_box->priv->tree_view && combo_box->priv->separator)
635 gtk_widget_unparent (combo_box->priv->separator);
636 combo_box->priv->separator = NULL;
638 g_object_ref (G_OBJECT (combo_box->priv->arrow));
639 gtk_widget_unparent (combo_box->priv->arrow);
640 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
641 combo_box->priv->arrow);
642 g_object_unref (G_OBJECT (combo_box->priv->arrow));
644 gtk_widget_queue_resize (GTK_WIDGET (container));
646 else if (combo_box->priv->cell_view_frame)
648 gtk_widget_unparent (combo_box->priv->cell_view_frame);
649 combo_box->priv->cell_view_frame = NULL;
654 static ComboCellInfo *
655 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
656 GtkCellRenderer *cell)
660 for (i = combo_box->priv->cells; i; i = i->next)
662 ComboCellInfo *info = (ComboCellInfo *)i->data;
664 if (info->cell == cell)
672 gtk_combo_box_menu_show (GtkWidget *menu,
675 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
677 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
679 combo_box->priv->popup_in_progress = FALSE;
683 gtk_combo_box_menu_hide (GtkWidget *menu,
686 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
688 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
693 gtk_combo_box_detacher (GtkWidget *widget,
696 GtkComboBox *combo_box;
698 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
700 combo_box = GTK_COMBO_BOX (widget);
701 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
703 g_signal_handlers_disconnect_by_func (menu,
704 gtk_combo_box_menu_show,
706 g_signal_handlers_disconnect_by_func (menu,
707 gtk_combo_box_menu_hide,
710 combo_box->priv->popup_widget = NULL;
714 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
717 if (GTK_IS_MENU (combo_box->priv->popup_widget))
719 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
720 combo_box->priv->popup_widget = NULL;
722 else if (combo_box->priv->popup_widget)
724 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
725 combo_box->priv->popup_widget);
726 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
727 combo_box->priv->popup_widget = NULL;
730 if (GTK_IS_MENU (popup))
732 if (combo_box->priv->popup_window)
734 gtk_widget_destroy (combo_box->priv->popup_window);
735 combo_box->priv->popup_window = combo_box->priv->popup_frame = NULL;
738 combo_box->priv->popup_widget = popup;
740 g_signal_connect (popup, "show",
741 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
742 g_signal_connect (popup, "hide",
743 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
745 gtk_menu_attach_to_widget (GTK_MENU (popup),
746 GTK_WIDGET (combo_box),
747 gtk_combo_box_detacher);
751 if (!combo_box->priv->popup_window)
753 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
754 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
755 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
757 combo_box->priv->popup_frame = gtk_frame_new (NULL);
758 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
760 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
761 combo_box->priv->popup_frame);
762 gtk_widget_show (combo_box->priv->popup_frame);
765 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
767 gtk_widget_show (popup);
768 g_object_ref (G_OBJECT (popup));
769 combo_box->priv->popup_widget = popup;
774 gtk_combo_box_menu_position (GtkMenu *menu,
783 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
785 /* FIXME: is using the size request here broken? */
786 child = GTK_BIN (combo_box)->child;
788 gdk_window_get_origin (child->window, &sx, &sy);
790 gtk_widget_size_request (GTK_WIDGET (menu), &req);
792 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
795 *x = sx + child->allocation.width - req.width;
796 *y = sy + child->allocation.height;
798 if (GTK_WIDGET_NO_WINDOW (child))
800 *x += child->allocation.x;
801 *y += child->allocation.y;
808 * gtk_combo_box_popup:
809 * @combo_box: a #GtkComboBox
811 * Pops up the menu or dropdown list of @combo_box.
813 * This function is mostly intended for use by accessibility technologies;
814 * applications should have little use for it.
819 gtk_combo_box_popup (GtkComboBox *combo_box)
821 gint x, y, width, height;
824 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
826 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
829 if (GTK_IS_MENU (combo_box->priv->popup_widget))
831 if (combo_box->priv->active_item != -1)
835 childs = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
836 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
837 g_list_nth_data (childs, combo_box->priv->active_item));
838 g_list_free (childs);
841 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
843 gtk_combo_box_menu_position, combo_box,
849 sample = GTK_BIN (combo_box)->child;
851 width = sample->allocation.width;
852 height = sample->allocation.height;
854 gdk_window_get_origin (sample->window,
857 if (combo_box->priv->cell_view_frame)
859 x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
860 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
861 width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
862 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
865 gtk_widget_set_size_request (combo_box->priv->popup_window,
868 if (GTK_WIDGET_NO_WINDOW (sample))
870 x += sample->allocation.x;
871 y += sample->allocation.y;
874 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window),
878 gtk_widget_show_all (combo_box->priv->popup_window);
880 gtk_widget_grab_focus (combo_box->priv->popup_window);
881 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
884 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
886 gdk_keyboard_grab (combo_box->priv->popup_window->window,
887 FALSE, GDK_CURRENT_TIME);
888 gtk_widget_grab_focus (combo_box->priv->tree_view);
891 gtk_grab_add (combo_box->priv->popup_window);
892 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
893 GDK_BUTTON_PRESS_MASK |
894 GDK_BUTTON_RELEASE_MASK |
895 GDK_POINTER_MOTION_MASK,
896 NULL, NULL, GDK_CURRENT_TIME);
898 gtk_grab_add (combo_box->priv->tree_view);
902 * gtk_combo_box_popdown:
903 * @combo_box: a #GtkComboBox
905 * Hides the menu or dropdown list of @combo_box.
907 * This function is mostly intended for use by accessibility technologies;
908 * applications should have little use for it.
913 gtk_combo_box_popdown (GtkComboBox *combo_box)
915 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
917 if (GTK_IS_MENU (combo_box->priv->popup_widget))
919 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
923 gtk_combo_box_list_remove_grabs (combo_box);
924 gtk_widget_hide_all (combo_box->priv->popup_window);
925 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
930 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
936 if (combo_box->priv->cell_view)
937 gtk_widget_style_get (combo_box->priv->cell_view,
938 "focus-line-width", &padding,
943 /* add some pixels for good measure */
944 padding += BONUS_PADDING;
946 if (combo_box->priv->cell_view)
947 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
952 return req.width + padding;
956 gtk_combo_box_remeasure (GtkComboBox *combo_box)
962 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
965 combo_box->priv->width = 0;
967 path = gtk_tree_path_new_from_indices (0, -1);
969 if (combo_box->priv->cell_view)
970 gtk_widget_style_get (combo_box->priv->cell_view,
971 "focus-line-width", &padding,
976 /* add some pixels for good measure */
977 padding += BONUS_PADDING;
983 if (combo_box->priv->cell_view)
984 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
989 combo_box->priv->width = MAX (combo_box->priv->width,
990 req.width + padding);
992 gtk_tree_path_next (path);
994 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
996 gtk_tree_path_free (path);
1000 gtk_combo_box_size_request (GtkWidget *widget,
1001 GtkRequisition *requisition)
1004 GtkRequisition bin_req;
1006 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1009 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1010 gtk_combo_box_remeasure (combo_box);
1011 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1013 if (!combo_box->priv->tree_view)
1017 if (combo_box->priv->cell_view)
1019 GtkRequisition sep_req, arrow_req;
1020 gint border_width, xthickness, ythickness;
1022 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1023 xthickness = combo_box->priv->button->style->xthickness;
1024 ythickness = combo_box->priv->button->style->ythickness;
1026 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1028 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1029 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1031 height = MAX (sep_req.height, arrow_req.height);
1032 height = MAX (height, bin_req.height);
1034 width = bin_req.width + sep_req.width + arrow_req.width;
1036 height += border_width + 1 + xthickness * 2 + 4;
1037 width += border_width + 1 + ythickness * 2 + 4;
1039 requisition->width = width;
1040 requisition->height = height;
1044 GtkRequisition but_req;
1046 gtk_widget_size_request (combo_box->priv->button, &but_req);
1048 requisition->width = bin_req.width + but_req.width;
1049 requisition->height = MAX (bin_req.height, but_req.height);
1055 GtkRequisition button_req;
1057 /* sample + frame */
1058 *requisition = bin_req;
1060 if (combo_box->priv->cell_view_frame)
1062 requisition->width += 2 *
1063 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1064 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1065 requisition->height += 2 *
1066 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1067 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1071 gtk_widget_size_request (combo_box->priv->button, &button_req);
1073 requisition->height = MAX (requisition->height, button_req.height);
1074 requisition->width += button_req.width;
1079 gtk_combo_box_size_allocate (GtkWidget *widget,
1080 GtkAllocation *allocation)
1082 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1083 GtkAllocation child;
1085 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1087 widget->allocation = *allocation;
1089 if (!combo_box->priv->tree_view)
1091 if (combo_box->priv->cell_view)
1093 gint border_width, xthickness, ythickness;
1097 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1099 /* set some things ready */
1100 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1101 xthickness = combo_box->priv->button->style->xthickness;
1102 ythickness = combo_box->priv->button->style->ythickness;
1104 child.x = allocation->x + border_width + 1 + xthickness + 2;
1105 child.y = allocation->y + border_width + 1 + ythickness + 2;
1107 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1109 /* handle the childs */
1110 gtk_widget_size_request (combo_box->priv->arrow, &req);
1111 child.width = req.width;
1112 child.height = allocation->height - 2 * (child.y - allocation->y);
1114 child.x += width - req.width;
1115 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1117 child.x += req.width;
1118 gtk_widget_size_request (combo_box->priv->separator, &req);
1119 child.width = req.width;
1121 child.x -= req.width;
1122 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1126 child.x += req.width;
1127 child.width = allocation->x + allocation->width
1128 - (border_width + 1 + xthickness + 2) - child.x;
1132 child.width = child.x;
1133 child.x = allocation->x + border_width + 1 + xthickness + 2;
1134 child.width -= child.x;
1137 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1141 gtk_widget_size_request (combo_box->priv->button, &req);
1143 child.x = allocation->x;
1145 child.x = allocation->x + allocation->width - req.width;
1146 child.y = allocation->y;
1147 child.width = req.width;
1148 child.height = allocation->height;
1149 gtk_widget_size_allocate (combo_box->priv->button, &child);
1152 child.x = allocation->x + req.width;
1154 child.x = allocation->x;
1155 child.y = allocation->y;
1156 child.width = allocation->width - req.width;
1157 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1165 gtk_widget_size_request (combo_box->priv->button, &req);
1167 child.x = allocation->x;
1169 child.x = allocation->x + allocation->width - req.width;
1170 child.y = allocation->y;
1171 child.width = req.width;
1172 child.height = allocation->height;
1173 gtk_widget_size_allocate (combo_box->priv->button, &child);
1177 child.x = allocation->x + req.width;
1179 child.x = allocation->x;
1180 child.y = allocation->y;
1181 child.width = allocation->width - req.width;
1182 child.height = allocation->height;
1184 if (combo_box->priv->cell_view_frame)
1186 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1190 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1191 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1193 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1194 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1195 child.width -= 2 * (
1196 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1197 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1198 child.height -= 2 * (
1199 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1200 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1203 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1208 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1210 if (combo_box->priv->inserted_id != -1)
1212 g_signal_handler_disconnect (combo_box->priv->model,
1213 combo_box->priv->inserted_id);
1214 combo_box->priv->inserted_id = -1;
1216 if (combo_box->priv->deleted_id != -1)
1218 g_signal_handler_disconnect (combo_box->priv->model,
1219 combo_box->priv->deleted_id);
1220 combo_box->priv->deleted_id = -1;
1222 if (combo_box->priv->changed_id != -1)
1224 g_signal_handler_disconnect (combo_box->priv->model,
1225 combo_box->priv->changed_id);
1226 combo_box->priv->changed_id = -1;
1230 if (!combo_box->priv->tree_view)
1232 if (combo_box->priv->popup_widget)
1233 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1234 (GtkCallback)gtk_widget_destroy, NULL);
1239 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1241 if (!combo_box->priv->tree_view)
1244 combo_box->priv->inserted_id =
1245 g_signal_connect (combo_box->priv->model, "row_inserted",
1246 G_CALLBACK (gtk_combo_box_menu_row_inserted),
1248 combo_box->priv->deleted_id =
1249 g_signal_connect (combo_box->priv->model, "row_deleted",
1250 G_CALLBACK (gtk_combo_box_menu_row_deleted),
1252 combo_box->priv->changed_id =
1253 g_signal_connect (combo_box->priv->model, "row_changed",
1254 G_CALLBACK (gtk_combo_box_menu_row_changed),
1260 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1261 combo_box->priv->model);
1263 combo_box->priv->changed_id =
1264 g_signal_connect (combo_box->priv->model, "row_changed",
1265 G_CALLBACK (gtk_combo_box_list_row_changed),
1271 gtk_combo_box_forall (GtkContainer *container,
1272 gboolean include_internals,
1273 GtkCallback callback,
1274 gpointer callback_data)
1276 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1278 if (include_internals)
1280 if (combo_box->priv->button)
1281 (* callback) (combo_box->priv->button, callback_data);
1282 if (combo_box->priv->separator)
1283 (* callback) (combo_box->priv->separator, callback_data);
1284 if (combo_box->priv->arrow)
1285 (* callback) (combo_box->priv->arrow, callback_data);
1286 if (combo_box->priv->cell_view_frame)
1287 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1290 if (GTK_BIN (container)->child)
1291 (* callback) (GTK_BIN (container)->child, callback_data);
1295 gtk_combo_box_expose_event (GtkWidget *widget,
1296 GdkEventExpose *event)
1298 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1300 if (!combo_box->priv->tree_view)
1302 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1303 combo_box->priv->button, event);
1305 if (combo_box->priv->separator)
1307 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1308 combo_box->priv->separator, event);
1310 /* if not in this case, arrow gets its expose event from button */
1311 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1312 combo_box->priv->arrow, event);
1317 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1318 combo_box->priv->button, event);
1320 if (combo_box->priv->cell_view_frame)
1321 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1322 combo_box->priv->cell_view_frame, event);
1325 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1326 GTK_BIN (widget)->child, event);
1332 gtk_combo_box_scroll_event (GtkWidget *widget,
1333 GdkEventScroll *event)
1335 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1339 index = gtk_combo_box_get_active (combo_box);
1343 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1345 if (event->direction == GDK_SCROLL_UP)
1350 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1361 cell_view_sync_cells (GtkComboBox *combo_box,
1362 GtkCellView *cell_view)
1366 for (k = combo_box->priv->cells; k; k = k->next)
1369 ComboCellInfo *info = (ComboCellInfo *)k->data;
1371 if (info->pack == GTK_PACK_START)
1372 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1373 info->cell, info->expand);
1374 else if (info->pack == GTK_PACK_END)
1375 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1376 info->cell, info->expand);
1378 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1380 info->func, info->func_data, NULL);
1382 for (j = info->attributes; j; j = j->next->next)
1384 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1387 GPOINTER_TO_INT (j->next->data));
1393 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1394 gboolean add_childs)
1398 if (combo_box->priv->cell_view)
1400 combo_box->priv->button = gtk_toggle_button_new ();
1401 g_signal_connect (combo_box->priv->button, "toggled",
1402 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1403 gtk_widget_set_parent (combo_box->priv->button,
1404 GTK_BIN (combo_box)->child->parent);
1406 combo_box->priv->separator = gtk_vseparator_new ();
1407 gtk_widget_set_parent (combo_box->priv->separator,
1408 combo_box->priv->button);
1409 gtk_widget_show (combo_box->priv->separator);
1411 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1412 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1413 gtk_widget_show (combo_box->priv->arrow);
1415 gtk_widget_show_all (combo_box->priv->button);
1417 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1419 /* I have no clue why, but we need to manually map in this case. */
1420 gtk_widget_map (combo_box->priv->separator);
1421 gtk_widget_map (combo_box->priv->arrow);
1426 combo_box->priv->button = gtk_toggle_button_new ();
1427 g_signal_connect (combo_box->priv->button, "toggled",
1428 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1429 gtk_widget_set_parent (combo_box->priv->button,
1430 GTK_BIN (combo_box)->child->parent);
1432 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1433 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1434 combo_box->priv->arrow);
1435 gtk_widget_show_all (combo_box->priv->button);
1438 g_signal_connect (combo_box->priv->button, "button_press_event",
1439 G_CALLBACK (gtk_combo_box_menu_button_press),
1442 /* create our funky menu */
1443 box = gtk_menu_new ();
1444 gtk_combo_box_set_popup_widget (combo_box, box);
1446 /* set the models */
1447 gtk_combo_box_set_model_internal (combo_box);
1451 gtk_combo_box_menu_fill (combo_box);
1456 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1462 if (!combo_box->priv->model)
1465 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1466 menu = combo_box->priv->popup_widget;
1468 for (i = 0; i < items; i++)
1472 path = gtk_tree_path_new_from_indices (i, -1);
1473 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1475 g_signal_connect (tmp, "activate",
1476 G_CALLBACK (gtk_combo_box_menu_item_activate),
1479 cell_view_sync_cells (combo_box,
1480 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1482 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1483 gtk_widget_show (tmp);
1485 gtk_tree_path_free (path);
1490 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1492 /* disconnect signal handlers */
1493 gtk_combo_box_unset_model (combo_box);
1495 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1496 G_SIGNAL_MATCH_DATA,
1498 gtk_combo_box_menu_button_press, NULL);
1500 /* unparent will remove our latest ref */
1501 if (combo_box->priv->cell_view)
1503 gtk_widget_unparent (combo_box->priv->arrow);
1504 combo_box->priv->arrow = NULL;
1506 gtk_widget_unparent (combo_box->priv->separator);
1507 combo_box->priv->separator = NULL;
1509 gtk_widget_unparent (combo_box->priv->button);
1510 combo_box->priv->button = NULL;
1514 /* will destroy the arrow too */
1515 gtk_widget_unparent (combo_box->priv->button);
1517 combo_box->priv->button = NULL;
1518 combo_box->priv->arrow = NULL;
1521 /* changing the popup window will unref the menu and the childs */
1529 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1536 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1540 if (combo_box->priv->col_column == -1)
1543 gtk_tree_model_get (combo_box->priv->model, &iter,
1544 combo_box->priv->col_column, cols,
1550 if (combo_box->priv->row_column == -1)
1553 gtk_tree_model_get (combo_box->priv->model, &iter,
1554 combo_box->priv->row_column, rows,
1560 menu_occupied (GtkMenu *menu,
1564 guint bottom_attach)
1568 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1569 g_return_val_if_fail (left_attach < right_attach, TRUE);
1570 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1572 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1575 gboolean h_intersect = FALSE;
1576 gboolean v_intersect = FALSE;
1578 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1581 "bottom_attach", &b,
1585 /* look if this item intersects with the given coordinates */
1586 h_intersect = left_attach <= l && l <= right_attach;
1587 h_intersect &= left_attach <= r && r <= right_attach;
1589 v_intersect = top_attach <= t && t <= bottom_attach;
1590 v_intersect &= top_attach <= b && b <= bottom_attach;
1592 if (h_intersect && v_intersect)
1600 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1603 gint current_col = 0, current_row = 0;
1609 menu = combo_box->priv->popup_widget;
1610 if (!GTK_IS_MENU_SHELL (menu))
1613 list = gtk_container_get_children (GTK_CONTAINER (menu));
1614 item = g_list_nth_data (list, index);
1617 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1619 /* look for a good spot */
1622 if (current_col + cols > combo_box->priv->wrap_width)
1628 if (!menu_occupied (GTK_MENU (menu),
1629 current_col, current_col + cols,
1630 current_row, current_row + rows))
1636 /* set attach props */
1637 gtk_menu_attach (GTK_MENU (menu), item,
1638 current_col, current_col + cols,
1639 current_row, current_row + rows);
1643 gtk_combo_box_relayout (GtkComboBox *combo_box)
1649 /* ensure we are in menu style */
1650 if (combo_box->priv->tree_view)
1651 gtk_combo_box_list_destroy (combo_box);
1653 gtk_combo_box_unset_model (combo_box);
1655 menu = combo_box->priv->popup_widget;
1657 if (!GTK_IS_MENU_SHELL (menu))
1659 gtk_combo_box_menu_setup (combo_box, FALSE);
1660 menu = combo_box->priv->popup_widget;
1663 /* get rid of all children */
1664 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1666 list = gtk_container_get_children (GTK_CONTAINER (menu));
1668 for (j = g_list_last (list); j; j = j->prev)
1669 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1674 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1676 for (i = 0; i < items; i++)
1681 path = gtk_tree_path_new_from_indices (i, -1);
1682 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1685 g_signal_connect (tmp, "activate",
1686 G_CALLBACK (gtk_combo_box_menu_item_activate),
1689 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1691 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1693 if (combo_box->priv->wrap_width)
1694 gtk_combo_box_relayout_item (combo_box, i);
1696 gtk_widget_show (tmp);
1698 gtk_tree_path_free (path);
1704 gtk_combo_box_menu_button_press (GtkWidget *widget,
1705 GdkEventButton *event,
1708 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1710 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1713 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1715 combo_box->priv->popup_in_progress = TRUE;
1716 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1718 gtk_combo_box_menu_position, combo_box,
1719 event->button, event->time);
1728 gtk_combo_box_menu_item_activate (GtkWidget *item,
1733 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1735 menu = combo_box->priv->popup_widget;
1736 g_return_if_fail (GTK_IS_MENU (menu));
1738 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1740 gtk_combo_box_set_active (combo_box, index);
1744 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
1751 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1753 if (!combo_box->priv->popup_widget)
1756 menu = combo_box->priv->popup_widget;
1757 g_return_if_fail (GTK_IS_MENU (menu));
1759 item = gtk_cell_view_menu_item_new_from_model (model, path);
1760 g_signal_connect (item, "activate",
1761 G_CALLBACK (gtk_combo_box_menu_item_activate),
1764 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
1766 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
1767 gtk_tree_path_get_indices (path)[0]);
1768 gtk_widget_show (item);
1772 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
1779 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1781 if (!combo_box->priv->popup_widget)
1784 index = gtk_tree_path_get_indices (path)[0];
1785 items = gtk_tree_model_iter_n_children (model, NULL);
1787 if (gtk_combo_box_get_active (combo_box) == index)
1788 gtk_combo_box_set_active (combo_box, index + 1 % items);
1790 menu = combo_box->priv->popup_widget;
1791 g_return_if_fail (GTK_IS_MENU (menu));
1793 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
1794 g_return_if_fail (GTK_IS_MENU_ITEM (item));
1796 gtk_container_remove (GTK_CONTAINER (menu), item);
1800 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
1805 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1808 if (!combo_box->priv->popup_widget)
1811 if (combo_box->priv->wrap_width)
1812 gtk_combo_box_relayout_item (combo_box,
1813 gtk_tree_path_get_indices (path)[0]);
1815 width = gtk_combo_box_calc_requested_width (combo_box, path);
1817 if (width > combo_box->priv->width)
1819 if (combo_box->priv->cell_view)
1821 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
1822 gtk_widget_queue_resize (combo_box->priv->cell_view);
1824 combo_box->priv->width = width;
1833 gtk_combo_box_list_setup (GtkComboBox *combo_box)
1836 GtkTreeSelection *sel;
1838 combo_box->priv->button = gtk_toggle_button_new ();
1839 gtk_widget_set_parent (combo_box->priv->button,
1840 GTK_BIN (combo_box)->child->parent);
1841 g_signal_connect (combo_box->priv->button, "button_press_event",
1842 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
1843 g_signal_connect (combo_box->priv->button, "toggled",
1844 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1846 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1847 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1848 combo_box->priv->arrow);
1849 combo_box->priv->separator = NULL;
1850 gtk_widget_show_all (combo_box->priv->button);
1852 if (combo_box->priv->cell_view)
1854 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
1855 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
1856 GTK_BIN (combo_box)->child->parent);
1857 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
1860 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1861 "background", "white",
1862 "background_set", TRUE,
1865 gtk_widget_show (combo_box->priv->cell_view_frame);
1868 combo_box->priv->tree_view = gtk_tree_view_new ();
1869 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
1870 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
1871 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
1874 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
1875 G_CALLBACK (gtk_combo_box_list_button_pressed),
1877 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
1878 G_CALLBACK (gtk_combo_box_list_button_released),
1880 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
1881 G_CALLBACK (gtk_combo_box_list_key_press),
1884 combo_box->priv->column = gtk_tree_view_column_new ();
1885 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
1886 combo_box->priv->column);
1888 /* set the models */
1889 gtk_combo_box_set_model_internal (combo_box);
1892 for (i = combo_box->priv->cells; i; i = i->next)
1895 ComboCellInfo *info = (ComboCellInfo *)i->data;
1897 if (info->pack == GTK_PACK_START)
1898 gtk_tree_view_column_pack_start (combo_box->priv->column,
1899 info->cell, info->expand);
1900 else if (info->pack == GTK_PACK_END)
1901 gtk_tree_view_column_pack_end (combo_box->priv->column,
1902 info->cell, info->expand);
1904 for (j = info->attributes; j; j = j->next->next)
1906 gtk_tree_view_column_add_attribute (combo_box->priv->column,
1909 GPOINTER_TO_INT (j->next->data));
1913 if (combo_box->priv->active_item != -1)
1917 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
1920 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1922 gtk_tree_path_free (path);
1926 /* set sample/popup widgets */
1927 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
1929 gtk_widget_show (combo_box->priv->tree_view);
1933 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
1935 /* disconnect signals */
1936 gtk_combo_box_unset_model (combo_box);
1938 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
1939 G_SIGNAL_MATCH_DATA,
1940 0, 0, NULL, NULL, combo_box);
1941 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1942 G_SIGNAL_MATCH_DATA,
1944 gtk_combo_box_list_button_pressed,
1947 /* destroy things (unparent will kill the latest ref from us)
1948 * last unref on button will destroy the arrow
1950 gtk_widget_unparent (combo_box->priv->button);
1951 combo_box->priv->button = NULL;
1952 combo_box->priv->arrow = NULL;
1954 if (combo_box->priv->cell_view)
1956 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1957 "background_set", FALSE,
1960 gtk_widget_unparent (combo_box->priv->cell_view_frame);
1961 combo_box->priv->cell_view_frame = NULL;
1964 gtk_widget_destroy (combo_box->priv->tree_view);
1966 combo_box->priv->tree_view = NULL;
1967 combo_box->priv->popup_widget = NULL;
1972 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
1974 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
1975 gtk_grab_remove (combo_box->priv->tree_view);
1977 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
1979 gtk_grab_remove (combo_box->priv->popup_window);
1980 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1985 gtk_combo_box_list_button_pressed (GtkWidget *widget,
1986 GdkEventButton *event,
1989 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1991 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
1993 if (ewidget == combo_box->priv->tree_view)
1996 if ((ewidget != combo_box->priv->button) ||
1997 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2000 gtk_combo_box_popup (combo_box);
2002 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2005 combo_box->priv->popup_in_progress = TRUE;
2011 gtk_combo_box_list_button_released (GtkWidget *widget,
2012 GdkEventButton *event,
2016 GtkTreePath *path = NULL;
2018 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2020 gboolean popup_in_progress = FALSE;
2022 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2024 if (combo_box->priv->popup_in_progress)
2026 popup_in_progress = TRUE;
2027 combo_box->priv->popup_in_progress = FALSE;
2030 if (ewidget != combo_box->priv->tree_view)
2032 if (ewidget == combo_box->priv->button &&
2033 !popup_in_progress &&
2034 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2036 gtk_combo_box_popdown (combo_box);
2040 /* released outside treeview */
2041 if (ewidget != combo_box->priv->button)
2043 gtk_combo_box_popdown (combo_box);
2051 /* select something cool */
2052 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2058 return TRUE; /* clicked outside window? */
2060 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2061 gtk_combo_box_popdown (combo_box);
2063 gtk_tree_path_free (path);
2069 gtk_combo_box_list_key_press (GtkWidget *widget,
2073 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2075 if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2076 event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
2077 event->keyval == GDK_Escape)
2079 if (event->keyval != GDK_Escape)
2083 GtkTreeModel *model = NULL;
2084 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2086 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2091 path = gtk_tree_model_get_path (model, &iter);
2094 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2095 gtk_tree_path_free (path);
2100 /* reset active item -- this is incredibly lame and ugly */
2101 gtk_combo_box_set_active (combo_box,
2102 gtk_combo_box_get_active (combo_box));
2104 gtk_combo_box_popdown (combo_box);
2113 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2118 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2121 width = gtk_combo_box_calc_requested_width (combo_box, path);
2123 if (width > combo_box->priv->width)
2125 if (combo_box->priv->cell_view)
2127 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2128 gtk_widget_queue_resize (combo_box->priv->cell_view);
2130 combo_box->priv->width = width;
2135 * GtkCellLayout implementation
2138 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2139 GtkCellRenderer *cell,
2142 ComboCellInfo *info;
2143 GtkComboBox *combo_box;
2146 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2147 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2149 combo_box = GTK_COMBO_BOX (layout);
2151 g_object_ref (G_OBJECT (cell));
2152 gtk_object_sink (GTK_OBJECT (cell));
2154 info = g_new0 (ComboCellInfo, 1);
2156 info->expand = expand;
2157 info->pack = GTK_PACK_START;
2159 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2161 if (combo_box->priv->cell_view)
2162 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2165 if (combo_box->priv->column)
2166 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2168 menu = combo_box->priv->popup_widget;
2169 if (GTK_IS_MENU (menu))
2173 list = gtk_container_get_children (GTK_CONTAINER (menu));
2174 for (i = list; i; i = i->next)
2178 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2179 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2181 view = GTK_CELL_VIEW (i->data);
2183 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2190 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2191 GtkCellRenderer *cell,
2194 ComboCellInfo *info;
2195 GtkComboBox *combo_box;
2198 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2199 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2201 combo_box = GTK_COMBO_BOX (layout);
2203 g_object_ref (G_OBJECT (cell));
2204 gtk_object_sink (GTK_OBJECT (cell));
2206 info = g_new0 (ComboCellInfo, 1);
2208 info->expand = expand;
2209 info->pack = GTK_PACK_END;
2211 if (combo_box->priv->cell_view)
2212 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2215 if (combo_box->priv->column)
2216 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2218 menu = combo_box->priv->popup_widget;
2219 if (GTK_IS_MENU (menu))
2223 list = gtk_container_get_children (GTK_CONTAINER (menu));
2224 for (i = list; i; i = i->next)
2228 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2229 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2231 view = GTK_CELL_VIEW (i->data);
2233 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2240 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2243 GtkComboBox *combo_box;
2246 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2248 combo_box = GTK_COMBO_BOX (layout);
2250 if (combo_box->priv->cell_view)
2251 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2253 if (combo_box->priv->column)
2254 gtk_tree_view_column_clear (combo_box->priv->column);
2256 for (i = combo_box->priv->cells; i; i = i->next)
2258 ComboCellInfo *info = (ComboCellInfo *)i->data;
2260 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2261 g_object_unref (G_OBJECT (info->cell));
2264 g_slist_free (combo_box->priv->cells);
2265 combo_box->priv->cells = NULL;
2267 menu = combo_box->priv->popup_widget;
2268 if (GTK_IS_MENU (menu))
2272 list = gtk_container_get_children (GTK_CONTAINER (menu));
2273 for (i = list; i; i = i->next)
2277 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2278 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2280 view = GTK_CELL_VIEW (i->data);
2282 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2289 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2290 GtkCellRenderer *cell,
2291 const gchar *attribute,
2294 ComboCellInfo *info;
2295 GtkComboBox *combo_box;
2298 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2299 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2301 combo_box = GTK_COMBO_BOX (layout);
2303 info = gtk_combo_box_get_cell_info (combo_box, cell);
2305 info->attributes = g_slist_prepend (info->attributes,
2306 GINT_TO_POINTER (column));
2307 info->attributes = g_slist_prepend (info->attributes,
2308 g_strdup (attribute));
2310 if (combo_box->priv->cell_view)
2311 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2312 cell, attribute, column);
2314 if (combo_box->priv->column)
2315 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2316 cell, attribute, column);
2318 menu = combo_box->priv->popup_widget;
2319 if (GTK_IS_MENU (menu))
2323 list = gtk_container_get_children (GTK_CONTAINER (menu));
2324 for (i = list; i; i = i->next)
2328 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2329 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2331 view = GTK_CELL_VIEW (i->data);
2333 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2339 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2343 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2344 GtkCellRenderer *cell,
2345 GtkCellLayoutDataFunc func,
2347 GDestroyNotify destroy)
2349 ComboCellInfo *info;
2350 GtkComboBox *combo_box;
2353 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2355 combo_box = GTK_COMBO_BOX (layout);
2357 info = gtk_combo_box_get_cell_info (combo_box, cell);
2358 g_return_if_fail (info != NULL);
2362 GDestroyNotify d = info->destroy;
2364 info->destroy = NULL;
2365 d (info->func_data);
2369 info->func_data = func_data;
2370 info->destroy = destroy;
2372 if (combo_box->priv->cell_view)
2373 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2375 if (combo_box->priv->column)
2376 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2378 menu = combo_box->priv->popup_widget;
2379 if (GTK_IS_MENU (menu))
2383 list = gtk_container_get_children (GTK_CONTAINER (menu));
2384 for (i = list; i; i = i->next)
2388 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2389 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2391 view = GTK_CELL_VIEW (i->data);
2393 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2394 func, func_data, NULL);
2399 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2403 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2404 GtkCellRenderer *cell)
2406 ComboCellInfo *info;
2407 GtkComboBox *combo_box;
2411 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2412 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2414 combo_box = GTK_COMBO_BOX (layout);
2416 info = gtk_combo_box_get_cell_info (combo_box, cell);
2417 g_return_if_fail (info != NULL);
2419 list = info->attributes;
2420 while (list && list->next)
2422 g_free (list->data);
2423 list = list->next->next;
2425 g_slist_free (info->attributes);
2426 info->attributes = NULL;
2428 if (combo_box->priv->cell_view)
2429 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2431 if (combo_box->priv->column)
2432 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2434 menu = combo_box->priv->popup_widget;
2435 if (GTK_IS_MENU (menu))
2439 list = gtk_container_get_children (GTK_CONTAINER (menu));
2440 for (i = list; i; i = i->next)
2444 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2445 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2447 view = GTK_CELL_VIEW (i->data);
2449 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2454 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2458 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2459 GtkCellRenderer *cell,
2462 ComboCellInfo *info;
2463 GtkComboBox *combo_box;
2467 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2468 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2470 combo_box = GTK_COMBO_BOX (layout);
2472 info = gtk_combo_box_get_cell_info (combo_box, cell);
2474 g_return_if_fail (info != NULL);
2475 g_return_if_fail (position >= 0);
2477 link = g_slist_find (combo_box->priv->cells, info);
2479 g_return_if_fail (link != NULL);
2481 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2482 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2485 if (combo_box->priv->cell_view)
2486 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2489 if (combo_box->priv->column)
2490 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2493 menu = combo_box->priv->popup_widget;
2494 if (GTK_IS_MENU (menu))
2498 list = gtk_container_get_children (GTK_CONTAINER (menu));
2499 for (i = list; i; i = i->next)
2503 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2504 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2506 view = GTK_CELL_VIEW (i->data);
2508 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2513 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2521 * gtk_combo_box_new:
2523 * Creates a new empty #GtkComboBox.
2525 * Return value: A new #GtkComboBox.
2530 gtk_combo_box_new (void)
2532 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2536 * gtk_combo_box_new_with_model:
2537 * @model: A #GtkTreeModel.
2539 * Creates a new #GtkComboBox with the model initialized to @model.
2541 * Return value: A new #GtkComboBox.
2546 gtk_combo_box_new_with_model (GtkTreeModel *model)
2548 GtkComboBox *combo_box;
2550 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2552 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2556 return GTK_WIDGET (combo_box);
2560 * gtk_combo_box_set_wrap_width:
2561 * @combo_box: A #GtkComboBox.
2562 * @width: Preferred number of columns.
2564 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2565 * the preferred number of columns when you want to the popup to be layed out
2571 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2574 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2575 g_return_if_fail (width > 0);
2577 if (width != combo_box->priv->wrap_width)
2579 combo_box->priv->wrap_width = width;
2581 gtk_combo_box_relayout (combo_box);
2583 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2588 * gtk_combo_box_set_row_span_column:
2589 * @combo_box: A #GtkComboBox.
2590 * @row_span: A column in the model passed during construction.
2592 * Sets the column with row span information for @combo_box to be @row_span.
2593 * The row span column contains integers which indicate how many rows
2594 * an item should span.
2599 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2604 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2606 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2607 g_return_if_fail (row_span >= 0 && row_span < col);
2609 if (row_span != combo_box->priv->row_column)
2611 combo_box->priv->row_column = row_span;
2613 gtk_combo_box_relayout (combo_box);
2615 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2620 * gtk_combo_box_set_column_span_column:
2621 * @combo_box: A #GtkComboBox.
2622 * @column_span: A column in the model passed during construction.
2624 * Sets the column with column span information for @combo_box to be
2625 * @column_span. The column span column contains integers which indicate
2626 * how many columns an item should span.
2631 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2636 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2638 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2639 g_return_if_fail (column_span >= 0 && column_span < col);
2641 if (column_span != combo_box->priv->col_column)
2643 combo_box->priv->col_column = column_span;
2645 gtk_combo_box_relayout (combo_box);
2647 g_object_notify (G_OBJECT (combo_box), "column_span_column");
2652 * gtk_combo_box_get_active:
2653 * @combo_box: A #GtkComboBox.
2655 * Returns the index of the currently active item, or -1 if there's no
2658 * Return value: An integer which is the index of the currently active item, or
2659 * -1 if there's no active item.
2664 gtk_combo_box_get_active (GtkComboBox *combo_box)
2666 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
2668 return combo_box->priv->active_item;
2672 * gtk_combo_box_set_active:
2673 * @combo_box: A #GtkComboBox.
2674 * @index: An index in the model passed during construction, or -1 to have
2677 * Sets the active item of @combo_box to be the item at @index.
2682 gtk_combo_box_set_active (GtkComboBox *combo_box,
2687 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2688 /* -1 means "no item selected" */
2689 g_return_if_fail (index >= -1);
2691 if (combo_box->priv->active_item == index)
2694 combo_box->priv->active_item = index;
2698 if (combo_box->priv->tree_view)
2699 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
2702 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2704 if (GTK_IS_MENU (menu))
2705 gtk_menu_set_active (menu, -1);
2708 if (combo_box->priv->cell_view)
2709 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2713 path = gtk_tree_path_new_from_indices (index, -1);
2715 if (combo_box->priv->tree_view)
2716 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
2719 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2721 if (GTK_IS_MENU (menu))
2722 gtk_menu_set_active (GTK_MENU (menu), index);
2725 if (combo_box->priv->cell_view)
2726 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
2728 gtk_tree_path_free (path);
2731 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
2736 * gtk_combo_box_get_active_iter:
2737 * @combo_box: A #GtkComboBox
2738 * @iter: The uninitialized #GtkTreeIter.
2740 * Set @iter to point to the current active item, if it exists.
2742 * Return value: %TRUE, if @iter was set
2747 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
2754 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
2756 active = gtk_combo_box_get_active (combo_box);
2760 path = gtk_tree_path_new_from_indices (active, -1);
2761 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
2763 gtk_tree_path_free (path);
2769 * gtk_combo_box_set_active_iter:
2770 * @combo_box: A #GtkComboBox
2771 * @iter: The #GtkTreeIter.
2773 * Sets the current active item to be the one referenced by @iter.
2774 * @iter must correspond to a path of depth one.
2779 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
2784 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2786 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
2787 g_return_if_fail (path != NULL);
2788 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
2790 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2791 gtk_tree_path_free (path);
2795 * gtk_combo_box_set_model:
2796 * @combo_box: A #GtkComboBox.
2797 * @model: A #GtkTreeModel.
2799 * Sets the model used by @combo_box to be @model. Will unset a
2800 * previously set model (if applicable).
2805 gtk_combo_box_set_model (GtkComboBox *combo_box,
2806 GtkTreeModel *model)
2808 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2809 g_return_if_fail (GTK_IS_TREE_MODEL (model));
2811 if (combo_box->priv->model)
2813 gtk_combo_box_unset_model (combo_box);
2814 g_object_unref (G_OBJECT (combo_box->priv->model));
2817 combo_box->priv->model = model;
2818 g_object_ref (G_OBJECT (combo_box->priv->model));
2820 gtk_combo_box_set_model_internal (combo_box);
2821 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
2822 gtk_combo_box_menu_fill (combo_box);
2824 if (combo_box->priv->cell_view)
2825 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
2826 combo_box->priv->model);
2830 * gtk_combo_box_get_model
2831 * @combo_box: A #GtkComboBox.
2833 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
2835 * Return value: A #GtkTreeModel which was passed during construction.
2840 gtk_combo_box_get_model (GtkComboBox *combo_box)
2842 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
2844 return combo_box->priv->model;
2848 /* convenience API for simple text combos */
2851 * gtk_combo_box_new_text:
2853 * Convenience function which constructs a new text combo box, which is a
2854 * #GtkComboBox just displaying strings. If you use this function to create
2855 * a text combo box, you should only manipulate its data source with the
2856 * following convenience functions: gtk_combo_box_append_text(),
2857 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
2858 * gtk_combo_box_remove_text().
2860 * Return value: A new text combo box.
2865 gtk_combo_box_new_text (void)
2867 GtkWidget *combo_box;
2868 GtkCellRenderer *cell;
2869 GtkListStore *store;
2871 store = gtk_list_store_new (1, G_TYPE_STRING);
2873 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
2875 cell = gtk_cell_renderer_text_new ();
2876 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
2877 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
2885 * gtk_combo_box_append_text:
2886 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2889 * Appends @string to the list of strings stored in @combo_box. Note that
2890 * you can only use this function with combo boxes constructed with
2891 * gtk_combo_box_new_text().
2896 gtk_combo_box_append_text (GtkComboBox *combo_box,
2900 GtkListStore *store;
2902 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2903 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2904 g_return_if_fail (text != NULL);
2906 store = GTK_LIST_STORE (combo_box->priv->model);
2908 gtk_list_store_append (store, &iter);
2909 gtk_list_store_set (store, &iter, 0, text, -1);
2913 * gtk_combo_box_insert_text:
2914 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2915 * @position: An index to insert @text.
2918 * Inserts @string at @position in the list of strings stored in @combo_box.
2919 * Note that you can only use this function with combo boxes constructed
2920 * with gtk_combo_box_new_text().
2925 gtk_combo_box_insert_text (GtkComboBox *combo_box,
2930 GtkListStore *store;
2932 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2933 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2934 g_return_if_fail (position >= 0);
2935 g_return_if_fail (text != NULL);
2937 store = GTK_LIST_STORE (combo_box->priv->model);
2939 gtk_list_store_insert (store, &iter, position);
2940 gtk_list_store_set (store, &iter, 0, text, -1);
2944 * gtk_combo_box_prepend_text:
2945 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2948 * Prepends @string to the list of strings stored in @combo_box. Note that
2949 * you can only use this function with combo boxes constructed with
2950 * gtk_combo_box_new_text().
2955 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
2959 GtkListStore *store;
2961 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2962 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2963 g_return_if_fail (text != NULL);
2965 store = GTK_LIST_STORE (combo_box->priv->model);
2967 gtk_list_store_prepend (store, &iter);
2968 gtk_list_store_set (store, &iter, 0, text, -1);
2972 * gtk_combo_box_remove_text:
2973 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2974 * @position: Index of the item to remove.
2976 * Removes the string at @position from @combo_box. Note that you can only use
2977 * this function with combo boxes constructed with gtk_combo_box_new_text().
2982 gtk_combo_box_remove_text (GtkComboBox *combo_box,
2986 GtkListStore *store;
2988 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2989 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2990 g_return_if_fail (position >= 0);
2992 store = GTK_LIST_STORE (combo_box->priv->model);
2994 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
2996 gtk_list_store_remove (store, &iter);
3001 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3002 gboolean group_cycling)
3004 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3006 gtk_widget_grab_focus (combo_box->priv->button);
3012 gtk_combo_box_destroy (GtkObject *object)
3014 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3016 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3018 combo_box->priv->cell_view = NULL;
3022 gtk_combo_box_finalize (GObject *object)
3024 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3027 gtk_combo_box_unset_model (combo_box);
3029 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3030 gtk_combo_box_menu_destroy (combo_box);
3032 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3033 gtk_combo_box_list_destroy (combo_box);
3035 if (combo_box->priv->popup_window)
3036 gtk_widget_destroy (combo_box->priv->popup_window);
3038 if (combo_box->priv->model)
3039 g_object_unref (combo_box->priv->model);
3041 for (i = combo_box->priv->cells; i; i = i->next)
3043 ComboCellInfo *info = (ComboCellInfo *)i->data;
3044 GSList *list = info->attributes;
3047 info->destroy (info->func_data);
3049 while (list && list->next)
3051 g_free (list->data);
3052 list = list->next->next;
3054 g_slist_free (info->attributes);
3056 g_object_unref (G_OBJECT (info->cell));
3059 g_slist_free (combo_box->priv->cells);
3061 G_OBJECT_CLASS (parent_class)->finalize (object);