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 /* Destroy all the menu mode widgets, if they exist. */
584 if (GTK_IS_MENU (combo_box->priv->popup_widget))
585 gtk_combo_box_menu_destroy (combo_box);
587 /* Create the list mode widgets, if they don't already exist. */
588 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
589 gtk_combo_box_list_setup (combo_box);
593 /* Destroy all the list mode widgets, if they exist. */
594 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
595 gtk_combo_box_list_destroy (combo_box);
597 /* Create the menu mode widgets, if they don't already exist. */
598 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
599 gtk_combo_box_menu_setup (combo_box, TRUE);
604 gtk_combo_box_button_toggled (GtkWidget *widget,
607 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
609 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
611 if (!combo_box->priv->popup_in_progress)
612 gtk_combo_box_popup (combo_box);
615 gtk_combo_box_popdown (combo_box);
619 gtk_combo_box_add (GtkContainer *container,
622 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
624 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
625 gtk_container_remove (container, combo_box->priv->cell_view);
627 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
629 if (combo_box->priv->cell_view &&
630 widget != combo_box->priv->cell_view)
632 /* since the cell_view was unparented, it's gone now */
633 combo_box->priv->cell_view = NULL;
635 if (!combo_box->priv->tree_view && combo_box->priv->separator)
637 gtk_widget_unparent (combo_box->priv->separator);
638 combo_box->priv->separator = NULL;
640 g_object_ref (G_OBJECT (combo_box->priv->arrow));
641 gtk_widget_unparent (combo_box->priv->arrow);
642 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
643 combo_box->priv->arrow);
644 g_object_unref (G_OBJECT (combo_box->priv->arrow));
646 gtk_widget_queue_resize (GTK_WIDGET (container));
648 else if (combo_box->priv->cell_view_frame)
650 gtk_widget_unparent (combo_box->priv->cell_view_frame);
651 combo_box->priv->cell_view_frame = NULL;
656 static ComboCellInfo *
657 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
658 GtkCellRenderer *cell)
662 for (i = combo_box->priv->cells; i; i = i->next)
664 ComboCellInfo *info = (ComboCellInfo *)i->data;
666 if (info && info->cell == cell)
674 gtk_combo_box_menu_show (GtkWidget *menu,
677 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
679 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
681 combo_box->priv->popup_in_progress = FALSE;
685 gtk_combo_box_menu_hide (GtkWidget *menu,
688 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
690 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
695 gtk_combo_box_detacher (GtkWidget *widget,
698 GtkComboBox *combo_box;
700 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
702 combo_box = GTK_COMBO_BOX (widget);
703 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
705 g_signal_handlers_disconnect_by_func (menu,
706 gtk_combo_box_menu_show,
708 g_signal_handlers_disconnect_by_func (menu,
709 gtk_combo_box_menu_hide,
712 combo_box->priv->popup_widget = NULL;
716 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
719 if (GTK_IS_MENU (combo_box->priv->popup_widget))
721 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
722 combo_box->priv->popup_widget = NULL;
724 else if (combo_box->priv->popup_widget)
726 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
727 combo_box->priv->popup_widget);
728 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
729 combo_box->priv->popup_widget = NULL;
732 if (GTK_IS_MENU (popup))
734 if (combo_box->priv->popup_window)
736 gtk_widget_destroy (combo_box->priv->popup_window);
737 combo_box->priv->popup_window = combo_box->priv->popup_frame = NULL;
740 combo_box->priv->popup_widget = popup;
742 g_signal_connect (popup, "show",
743 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
744 g_signal_connect (popup, "hide",
745 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
747 gtk_menu_attach_to_widget (GTK_MENU (popup),
748 GTK_WIDGET (combo_box),
749 gtk_combo_box_detacher);
753 if (!combo_box->priv->popup_window)
755 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
756 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
757 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
759 combo_box->priv->popup_frame = gtk_frame_new (NULL);
760 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
762 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
763 combo_box->priv->popup_frame);
764 gtk_widget_show (combo_box->priv->popup_frame);
767 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
769 gtk_widget_show (popup);
770 g_object_ref (G_OBJECT (popup));
771 combo_box->priv->popup_widget = popup;
776 gtk_combo_box_menu_position (GtkMenu *menu,
785 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
787 /* FIXME: is using the size request here broken? */
788 child = GTK_BIN (combo_box)->child;
790 gdk_window_get_origin (child->window, &sx, &sy);
792 gtk_widget_size_request (GTK_WIDGET (menu), &req);
794 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
797 *x = sx + child->allocation.width - req.width;
798 *y = sy + child->allocation.height;
800 if (GTK_WIDGET_NO_WINDOW (child))
802 *x += child->allocation.x;
803 *y += child->allocation.y;
810 * gtk_combo_box_popup:
811 * @combo_box: a #GtkComboBox
813 * Pops up the menu or dropdown list of @combo_box.
815 * This function is mostly intended for use by accessibility technologies;
816 * applications should have little use for it.
821 gtk_combo_box_popup (GtkComboBox *combo_box)
823 gint x, y, width, height;
826 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
828 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
831 if (GTK_IS_MENU (combo_box->priv->popup_widget))
833 if (combo_box->priv->active_item != -1)
837 childs = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
838 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
839 g_list_nth_data (childs, combo_box->priv->active_item));
840 g_list_free (childs);
843 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
845 gtk_combo_box_menu_position, combo_box,
851 sample = GTK_BIN (combo_box)->child;
853 width = sample->allocation.width;
854 height = sample->allocation.height;
856 gdk_window_get_origin (sample->window,
859 if (combo_box->priv->cell_view_frame)
861 x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
862 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
863 width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
864 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
867 gtk_widget_set_size_request (combo_box->priv->popup_window,
870 if (GTK_WIDGET_NO_WINDOW (sample))
872 x += sample->allocation.x;
873 y += sample->allocation.y;
876 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window),
880 gtk_widget_show_all (combo_box->priv->popup_window);
882 gtk_widget_grab_focus (combo_box->priv->popup_window);
883 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
886 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
888 gdk_keyboard_grab (combo_box->priv->popup_window->window,
889 FALSE, GDK_CURRENT_TIME);
890 gtk_widget_grab_focus (combo_box->priv->tree_view);
893 gtk_grab_add (combo_box->priv->popup_window);
894 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
895 GDK_BUTTON_PRESS_MASK |
896 GDK_BUTTON_RELEASE_MASK |
897 GDK_POINTER_MOTION_MASK,
898 NULL, NULL, GDK_CURRENT_TIME);
900 gtk_grab_add (combo_box->priv->tree_view);
904 * gtk_combo_box_popdown:
905 * @combo_box: a #GtkComboBox
907 * Hides the menu or dropdown list of @combo_box.
909 * This function is mostly intended for use by accessibility technologies;
910 * applications should have little use for it.
915 gtk_combo_box_popdown (GtkComboBox *combo_box)
917 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
919 if (GTK_IS_MENU (combo_box->priv->popup_widget))
921 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
925 gtk_combo_box_list_remove_grabs (combo_box);
926 gtk_widget_hide_all (combo_box->priv->popup_window);
927 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
932 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
938 if (combo_box->priv->cell_view)
939 gtk_widget_style_get (combo_box->priv->cell_view,
940 "focus-line-width", &padding,
945 /* add some pixels for good measure */
946 padding += BONUS_PADDING;
948 if (combo_box->priv->cell_view)
949 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
954 return req.width + padding;
958 gtk_combo_box_remeasure (GtkComboBox *combo_box)
964 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
967 combo_box->priv->width = 0;
969 path = gtk_tree_path_new_from_indices (0, -1);
971 if (combo_box->priv->cell_view)
972 gtk_widget_style_get (combo_box->priv->cell_view,
973 "focus-line-width", &padding,
978 /* add some pixels for good measure */
979 padding += BONUS_PADDING;
985 if (combo_box->priv->cell_view)
986 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
991 combo_box->priv->width = MAX (combo_box->priv->width,
992 req.width + padding);
994 gtk_tree_path_next (path);
996 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
998 gtk_tree_path_free (path);
1002 gtk_combo_box_size_request (GtkWidget *widget,
1003 GtkRequisition *requisition)
1006 GtkRequisition bin_req;
1008 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1011 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1012 gtk_combo_box_remeasure (combo_box);
1013 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1015 if (!combo_box->priv->tree_view)
1019 if (combo_box->priv->cell_view)
1021 GtkRequisition sep_req, arrow_req;
1022 gint border_width, xthickness, ythickness;
1024 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1025 xthickness = combo_box->priv->button->style->xthickness;
1026 ythickness = combo_box->priv->button->style->ythickness;
1028 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1030 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1031 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1033 height = MAX (sep_req.height, arrow_req.height);
1034 height = MAX (height, bin_req.height);
1036 width = bin_req.width + sep_req.width + arrow_req.width;
1038 height += border_width + 1 + xthickness * 2 + 4;
1039 width += border_width + 1 + ythickness * 2 + 4;
1041 requisition->width = width;
1042 requisition->height = height;
1046 GtkRequisition but_req;
1048 gtk_widget_size_request (combo_box->priv->button, &but_req);
1050 requisition->width = bin_req.width + but_req.width;
1051 requisition->height = MAX (bin_req.height, but_req.height);
1057 GtkRequisition button_req;
1059 /* sample + frame */
1060 *requisition = bin_req;
1062 if (combo_box->priv->cell_view_frame)
1064 requisition->width += 2 *
1065 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1066 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1067 requisition->height += 2 *
1068 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1069 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1073 gtk_widget_size_request (combo_box->priv->button, &button_req);
1075 requisition->height = MAX (requisition->height, button_req.height);
1076 requisition->width += button_req.width;
1081 gtk_combo_box_size_allocate (GtkWidget *widget,
1082 GtkAllocation *allocation)
1084 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1085 GtkAllocation child;
1087 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1089 widget->allocation = *allocation;
1091 if (!combo_box->priv->tree_view)
1093 if (combo_box->priv->cell_view)
1095 gint border_width, xthickness, ythickness;
1099 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1101 /* set some things ready */
1102 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1103 xthickness = combo_box->priv->button->style->xthickness;
1104 ythickness = combo_box->priv->button->style->ythickness;
1106 child.x = allocation->x + border_width + 1 + xthickness + 2;
1107 child.y = allocation->y + border_width + 1 + ythickness + 2;
1109 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1111 /* handle the childs */
1112 gtk_widget_size_request (combo_box->priv->arrow, &req);
1113 child.width = req.width;
1114 child.height = allocation->height - 2 * (child.y - allocation->y);
1116 child.x += width - req.width;
1117 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1119 child.x += req.width;
1120 gtk_widget_size_request (combo_box->priv->separator, &req);
1121 child.width = req.width;
1123 child.x -= req.width;
1124 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1128 child.x += req.width;
1129 child.width = allocation->x + allocation->width
1130 - (border_width + 1 + xthickness + 2) - child.x;
1134 child.width = child.x;
1135 child.x = allocation->x + border_width + 1 + xthickness + 2;
1136 child.width -= child.x;
1139 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1143 gtk_widget_size_request (combo_box->priv->button, &req);
1145 child.x = allocation->x;
1147 child.x = allocation->x + allocation->width - req.width;
1148 child.y = allocation->y;
1149 child.width = req.width;
1150 child.height = allocation->height;
1151 gtk_widget_size_allocate (combo_box->priv->button, &child);
1154 child.x = allocation->x + req.width;
1156 child.x = allocation->x;
1157 child.y = allocation->y;
1158 child.width = allocation->width - req.width;
1159 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1167 gtk_widget_size_request (combo_box->priv->button, &req);
1169 child.x = allocation->x;
1171 child.x = allocation->x + allocation->width - req.width;
1172 child.y = allocation->y;
1173 child.width = req.width;
1174 child.height = allocation->height;
1175 gtk_widget_size_allocate (combo_box->priv->button, &child);
1179 child.x = allocation->x + req.width;
1181 child.x = allocation->x;
1182 child.y = allocation->y;
1183 child.width = allocation->width - req.width;
1184 child.height = allocation->height;
1186 if (combo_box->priv->cell_view_frame)
1188 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1192 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1193 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1195 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1196 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1197 child.width -= 2 * (
1198 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1199 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1200 child.height -= 2 * (
1201 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1202 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1205 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1210 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1212 if (combo_box->priv->inserted_id != -1)
1214 g_signal_handler_disconnect (combo_box->priv->model,
1215 combo_box->priv->inserted_id);
1216 combo_box->priv->inserted_id = -1;
1218 if (combo_box->priv->deleted_id != -1)
1220 g_signal_handler_disconnect (combo_box->priv->model,
1221 combo_box->priv->deleted_id);
1222 combo_box->priv->deleted_id = -1;
1224 if (combo_box->priv->changed_id != -1)
1226 g_signal_handler_disconnect (combo_box->priv->model,
1227 combo_box->priv->changed_id);
1228 combo_box->priv->changed_id = -1;
1232 if (!combo_box->priv->tree_view)
1234 if (combo_box->priv->popup_widget)
1235 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1236 (GtkCallback)gtk_widget_destroy, NULL);
1241 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1243 if (!combo_box->priv->tree_view)
1246 combo_box->priv->inserted_id =
1247 g_signal_connect (combo_box->priv->model, "row_inserted",
1248 G_CALLBACK (gtk_combo_box_menu_row_inserted),
1250 combo_box->priv->deleted_id =
1251 g_signal_connect (combo_box->priv->model, "row_deleted",
1252 G_CALLBACK (gtk_combo_box_menu_row_deleted),
1254 combo_box->priv->changed_id =
1255 g_signal_connect (combo_box->priv->model, "row_changed",
1256 G_CALLBACK (gtk_combo_box_menu_row_changed),
1262 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1263 combo_box->priv->model);
1265 combo_box->priv->changed_id =
1266 g_signal_connect (combo_box->priv->model, "row_changed",
1267 G_CALLBACK (gtk_combo_box_list_row_changed),
1273 gtk_combo_box_forall (GtkContainer *container,
1274 gboolean include_internals,
1275 GtkCallback callback,
1276 gpointer callback_data)
1278 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1280 if (include_internals)
1282 if (combo_box->priv->button)
1283 (* callback) (combo_box->priv->button, callback_data);
1284 if (combo_box->priv->separator)
1285 (* callback) (combo_box->priv->separator, callback_data);
1286 if (combo_box->priv->arrow)
1287 (* callback) (combo_box->priv->arrow, callback_data);
1288 if (combo_box->priv->cell_view_frame)
1289 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1292 if (GTK_BIN (container)->child)
1293 (* callback) (GTK_BIN (container)->child, callback_data);
1297 gtk_combo_box_expose_event (GtkWidget *widget,
1298 GdkEventExpose *event)
1300 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1302 if (!combo_box->priv->tree_view)
1304 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1305 combo_box->priv->button, event);
1307 if (combo_box->priv->separator)
1309 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1310 combo_box->priv->separator, event);
1312 /* if not in this case, arrow gets its expose event from button */
1313 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1314 combo_box->priv->arrow, event);
1319 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1320 combo_box->priv->button, event);
1322 if (combo_box->priv->cell_view_frame)
1323 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1324 combo_box->priv->cell_view_frame, event);
1327 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1328 GTK_BIN (widget)->child, event);
1334 gtk_combo_box_scroll_event (GtkWidget *widget,
1335 GdkEventScroll *event)
1337 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1341 index = gtk_combo_box_get_active (combo_box);
1345 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1347 if (event->direction == GDK_SCROLL_UP)
1352 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1363 cell_view_sync_cells (GtkComboBox *combo_box,
1364 GtkCellView *cell_view)
1368 for (k = combo_box->priv->cells; k; k = k->next)
1371 ComboCellInfo *info = (ComboCellInfo *)k->data;
1373 if (info->pack == GTK_PACK_START)
1374 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1375 info->cell, info->expand);
1376 else if (info->pack == GTK_PACK_END)
1377 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1378 info->cell, info->expand);
1380 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1382 info->func, info->func_data, NULL);
1384 for (j = info->attributes; j; j = j->next->next)
1386 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1389 GPOINTER_TO_INT (j->next->data));
1395 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1396 gboolean add_childs)
1400 /* Unset any existing model. */
1401 gtk_combo_box_unset_model (combo_box);
1403 if (combo_box->priv->cell_view)
1405 combo_box->priv->button = gtk_toggle_button_new ();
1406 g_signal_connect (combo_box->priv->button, "toggled",
1407 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1408 gtk_widget_set_parent (combo_box->priv->button,
1409 GTK_BIN (combo_box)->child->parent);
1411 combo_box->priv->separator = gtk_vseparator_new ();
1412 gtk_widget_set_parent (combo_box->priv->separator,
1413 combo_box->priv->button);
1414 gtk_widget_show (combo_box->priv->separator);
1416 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1417 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1418 gtk_widget_show (combo_box->priv->arrow);
1420 gtk_widget_show_all (combo_box->priv->button);
1422 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1424 /* I have no clue why, but we need to manually map in this case. */
1425 gtk_widget_map (combo_box->priv->separator);
1426 gtk_widget_map (combo_box->priv->arrow);
1431 combo_box->priv->button = gtk_toggle_button_new ();
1432 g_signal_connect (combo_box->priv->button, "toggled",
1433 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1434 gtk_widget_set_parent (combo_box->priv->button,
1435 GTK_BIN (combo_box)->child->parent);
1437 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1438 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1439 combo_box->priv->arrow);
1440 gtk_widget_show_all (combo_box->priv->button);
1443 g_signal_connect (combo_box->priv->button, "button_press_event",
1444 G_CALLBACK (gtk_combo_box_menu_button_press),
1447 /* create our funky menu */
1448 box = gtk_menu_new ();
1449 gtk_combo_box_set_popup_widget (combo_box, box);
1451 /* set the models */
1452 gtk_combo_box_set_model_internal (combo_box);
1456 gtk_combo_box_menu_fill (combo_box);
1461 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1467 if (!combo_box->priv->model)
1470 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1471 menu = combo_box->priv->popup_widget;
1473 for (i = 0; i < items; i++)
1477 path = gtk_tree_path_new_from_indices (i, -1);
1478 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1480 g_signal_connect (tmp, "activate",
1481 G_CALLBACK (gtk_combo_box_menu_item_activate),
1484 cell_view_sync_cells (combo_box,
1485 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1487 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1488 gtk_widget_show (tmp);
1490 gtk_tree_path_free (path);
1495 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1497 /* disconnect signal handlers */
1498 gtk_combo_box_unset_model (combo_box);
1500 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1501 G_SIGNAL_MATCH_DATA,
1503 gtk_combo_box_menu_button_press, NULL);
1505 /* unparent will remove our latest ref */
1506 if (combo_box->priv->cell_view)
1508 gtk_widget_unparent (combo_box->priv->arrow);
1509 combo_box->priv->arrow = NULL;
1511 gtk_widget_unparent (combo_box->priv->separator);
1512 combo_box->priv->separator = NULL;
1514 gtk_widget_unparent (combo_box->priv->button);
1515 combo_box->priv->button = NULL;
1519 /* will destroy the arrow too */
1520 gtk_widget_unparent (combo_box->priv->button);
1522 combo_box->priv->button = NULL;
1523 combo_box->priv->arrow = NULL;
1526 /* changing the popup window will unref the menu and the childs */
1534 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1541 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1545 if (combo_box->priv->col_column == -1)
1548 gtk_tree_model_get (combo_box->priv->model, &iter,
1549 combo_box->priv->col_column, cols,
1555 if (combo_box->priv->row_column == -1)
1558 gtk_tree_model_get (combo_box->priv->model, &iter,
1559 combo_box->priv->row_column, rows,
1565 menu_occupied (GtkMenu *menu,
1569 guint bottom_attach)
1573 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1574 g_return_val_if_fail (left_attach < right_attach, TRUE);
1575 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1577 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1580 gboolean h_intersect = FALSE;
1581 gboolean v_intersect = FALSE;
1583 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1586 "bottom_attach", &b,
1590 /* look if this item intersects with the given coordinates */
1591 h_intersect = left_attach <= l && l <= right_attach;
1592 h_intersect &= left_attach <= r && r <= right_attach;
1594 v_intersect = top_attach <= t && t <= bottom_attach;
1595 v_intersect &= top_attach <= b && b <= bottom_attach;
1597 if (h_intersect && v_intersect)
1605 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1608 gint current_col = 0, current_row = 0;
1614 menu = combo_box->priv->popup_widget;
1615 if (!GTK_IS_MENU_SHELL (menu))
1618 list = gtk_container_get_children (GTK_CONTAINER (menu));
1619 item = g_list_nth_data (list, index);
1622 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1624 /* look for a good spot */
1627 if (current_col + cols > combo_box->priv->wrap_width)
1633 if (!menu_occupied (GTK_MENU (menu),
1634 current_col, current_col + cols,
1635 current_row, current_row + rows))
1641 /* set attach props */
1642 gtk_menu_attach (GTK_MENU (menu), item,
1643 current_col, current_col + cols,
1644 current_row, current_row + rows);
1648 gtk_combo_box_relayout (GtkComboBox *combo_box)
1654 /* ensure we are in menu style */
1655 if (combo_box->priv->tree_view)
1656 gtk_combo_box_list_destroy (combo_box);
1658 menu = combo_box->priv->popup_widget;
1660 if (!GTK_IS_MENU_SHELL (menu))
1662 gtk_combo_box_menu_setup (combo_box, FALSE);
1663 menu = combo_box->priv->popup_widget;
1666 /* get rid of all children */
1667 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1669 list = gtk_container_get_children (GTK_CONTAINER (menu));
1671 for (j = g_list_last (list); j; j = j->prev)
1672 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1677 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1679 for (i = 0; i < items; i++)
1684 path = gtk_tree_path_new_from_indices (i, -1);
1685 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1688 g_signal_connect (tmp, "activate",
1689 G_CALLBACK (gtk_combo_box_menu_item_activate),
1692 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1694 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1696 if (combo_box->priv->wrap_width)
1697 gtk_combo_box_relayout_item (combo_box, i);
1699 gtk_widget_show (tmp);
1701 gtk_tree_path_free (path);
1707 gtk_combo_box_menu_button_press (GtkWidget *widget,
1708 GdkEventButton *event,
1711 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1713 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1716 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1718 combo_box->priv->popup_in_progress = TRUE;
1719 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1721 gtk_combo_box_menu_position, combo_box,
1722 event->button, event->time);
1731 gtk_combo_box_menu_item_activate (GtkWidget *item,
1736 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1738 menu = combo_box->priv->popup_widget;
1739 g_return_if_fail (GTK_IS_MENU (menu));
1741 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1743 gtk_combo_box_set_active (combo_box, index);
1747 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
1754 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1756 if (!combo_box->priv->popup_widget)
1759 menu = combo_box->priv->popup_widget;
1760 g_return_if_fail (GTK_IS_MENU (menu));
1762 item = gtk_cell_view_menu_item_new_from_model (model, path);
1763 g_signal_connect (item, "activate",
1764 G_CALLBACK (gtk_combo_box_menu_item_activate),
1767 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
1769 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
1770 gtk_tree_path_get_indices (path)[0]);
1771 gtk_widget_show (item);
1775 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
1782 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1784 if (!combo_box->priv->popup_widget)
1787 index = gtk_tree_path_get_indices (path)[0];
1788 items = gtk_tree_model_iter_n_children (model, NULL);
1790 if (gtk_combo_box_get_active (combo_box) == index)
1791 gtk_combo_box_set_active (combo_box, index + 1 % items);
1793 menu = combo_box->priv->popup_widget;
1794 g_return_if_fail (GTK_IS_MENU (menu));
1796 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
1797 g_return_if_fail (GTK_IS_MENU_ITEM (item));
1799 gtk_container_remove (GTK_CONTAINER (menu), item);
1803 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
1808 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1811 if (!combo_box->priv->popup_widget)
1814 if (combo_box->priv->wrap_width)
1815 gtk_combo_box_relayout_item (combo_box,
1816 gtk_tree_path_get_indices (path)[0]);
1818 width = gtk_combo_box_calc_requested_width (combo_box, path);
1820 if (width > combo_box->priv->width)
1822 if (combo_box->priv->cell_view)
1824 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
1825 gtk_widget_queue_resize (combo_box->priv->cell_view);
1827 combo_box->priv->width = width;
1836 gtk_combo_box_list_setup (GtkComboBox *combo_box)
1839 GtkTreeSelection *sel;
1841 /* Unset any existing model. */
1842 gtk_combo_box_unset_model (combo_box);
1844 combo_box->priv->button = gtk_toggle_button_new ();
1845 gtk_widget_set_parent (combo_box->priv->button,
1846 GTK_BIN (combo_box)->child->parent);
1847 g_signal_connect (combo_box->priv->button, "button_press_event",
1848 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
1849 g_signal_connect (combo_box->priv->button, "toggled",
1850 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1852 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1853 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1854 combo_box->priv->arrow);
1855 combo_box->priv->separator = NULL;
1856 gtk_widget_show_all (combo_box->priv->button);
1858 if (combo_box->priv->cell_view)
1860 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
1861 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
1862 GTK_BIN (combo_box)->child->parent);
1863 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
1866 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1867 "background", "white",
1868 "background_set", TRUE,
1871 gtk_widget_show (combo_box->priv->cell_view_frame);
1874 combo_box->priv->tree_view = gtk_tree_view_new ();
1875 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
1876 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
1877 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
1880 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
1881 G_CALLBACK (gtk_combo_box_list_button_pressed),
1883 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
1884 G_CALLBACK (gtk_combo_box_list_button_released),
1886 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
1887 G_CALLBACK (gtk_combo_box_list_key_press),
1890 combo_box->priv->column = gtk_tree_view_column_new ();
1891 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
1892 combo_box->priv->column);
1894 /* set the models */
1895 gtk_combo_box_set_model_internal (combo_box);
1898 for (i = combo_box->priv->cells; i; i = i->next)
1901 ComboCellInfo *info = (ComboCellInfo *)i->data;
1903 if (info->pack == GTK_PACK_START)
1904 gtk_tree_view_column_pack_start (combo_box->priv->column,
1905 info->cell, info->expand);
1906 else if (info->pack == GTK_PACK_END)
1907 gtk_tree_view_column_pack_end (combo_box->priv->column,
1908 info->cell, info->expand);
1910 for (j = info->attributes; j; j = j->next->next)
1912 gtk_tree_view_column_add_attribute (combo_box->priv->column,
1915 GPOINTER_TO_INT (j->next->data));
1919 if (combo_box->priv->active_item != -1)
1923 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
1926 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1928 gtk_tree_path_free (path);
1932 /* set sample/popup widgets */
1933 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
1935 gtk_widget_show (combo_box->priv->tree_view);
1939 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
1941 /* disconnect signals */
1942 gtk_combo_box_unset_model (combo_box);
1944 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
1945 G_SIGNAL_MATCH_DATA,
1946 0, 0, NULL, NULL, combo_box);
1947 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1948 G_SIGNAL_MATCH_DATA,
1950 gtk_combo_box_list_button_pressed,
1953 /* destroy things (unparent will kill the latest ref from us)
1954 * last unref on button will destroy the arrow
1956 gtk_widget_unparent (combo_box->priv->button);
1957 combo_box->priv->button = NULL;
1958 combo_box->priv->arrow = NULL;
1960 if (combo_box->priv->cell_view)
1962 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1963 "background_set", FALSE,
1966 gtk_widget_unparent (combo_box->priv->cell_view_frame);
1967 combo_box->priv->cell_view_frame = NULL;
1970 gtk_widget_destroy (combo_box->priv->tree_view);
1972 combo_box->priv->tree_view = NULL;
1973 combo_box->priv->popup_widget = NULL;
1978 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
1980 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
1981 gtk_grab_remove (combo_box->priv->tree_view);
1983 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
1985 gtk_grab_remove (combo_box->priv->popup_window);
1986 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1991 gtk_combo_box_list_button_pressed (GtkWidget *widget,
1992 GdkEventButton *event,
1995 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1997 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
1999 if (ewidget == combo_box->priv->tree_view)
2002 if ((ewidget != combo_box->priv->button) ||
2003 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2006 gtk_combo_box_popup (combo_box);
2008 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2011 combo_box->priv->popup_in_progress = TRUE;
2017 gtk_combo_box_list_button_released (GtkWidget *widget,
2018 GdkEventButton *event,
2022 GtkTreePath *path = NULL;
2024 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2026 gboolean popup_in_progress = FALSE;
2028 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2030 if (combo_box->priv->popup_in_progress)
2032 popup_in_progress = TRUE;
2033 combo_box->priv->popup_in_progress = FALSE;
2036 if (ewidget != combo_box->priv->tree_view)
2038 if (ewidget == combo_box->priv->button &&
2039 !popup_in_progress &&
2040 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2042 gtk_combo_box_popdown (combo_box);
2046 /* released outside treeview */
2047 if (ewidget != combo_box->priv->button)
2049 gtk_combo_box_popdown (combo_box);
2057 /* select something cool */
2058 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2064 return TRUE; /* clicked outside window? */
2066 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2067 gtk_combo_box_popdown (combo_box);
2069 gtk_tree_path_free (path);
2075 gtk_combo_box_list_key_press (GtkWidget *widget,
2079 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2081 if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2082 event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
2083 event->keyval == GDK_Escape)
2085 if (event->keyval != GDK_Escape)
2089 GtkTreeModel *model = NULL;
2090 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2092 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2097 path = gtk_tree_model_get_path (model, &iter);
2100 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2101 gtk_tree_path_free (path);
2106 /* reset active item -- this is incredibly lame and ugly */
2107 gtk_combo_box_set_active (combo_box,
2108 gtk_combo_box_get_active (combo_box));
2110 gtk_combo_box_popdown (combo_box);
2119 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2124 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2127 width = gtk_combo_box_calc_requested_width (combo_box, path);
2129 if (width > combo_box->priv->width)
2131 if (combo_box->priv->cell_view)
2133 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2134 gtk_widget_queue_resize (combo_box->priv->cell_view);
2136 combo_box->priv->width = width;
2141 * GtkCellLayout implementation
2144 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2145 GtkCellRenderer *cell,
2148 ComboCellInfo *info;
2149 GtkComboBox *combo_box;
2152 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2153 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2155 combo_box = GTK_COMBO_BOX (layout);
2157 g_object_ref (G_OBJECT (cell));
2158 gtk_object_sink (GTK_OBJECT (cell));
2160 info = g_new0 (ComboCellInfo, 1);
2162 info->expand = expand;
2163 info->pack = GTK_PACK_START;
2165 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2167 if (combo_box->priv->cell_view)
2168 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2171 if (combo_box->priv->column)
2172 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2174 menu = combo_box->priv->popup_widget;
2175 if (GTK_IS_MENU (menu))
2179 list = gtk_container_get_children (GTK_CONTAINER (menu));
2180 for (i = list; i; i = i->next)
2184 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2185 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2187 view = GTK_CELL_VIEW (i->data);
2189 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2196 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2197 GtkCellRenderer *cell,
2200 ComboCellInfo *info;
2201 GtkComboBox *combo_box;
2204 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2205 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2207 combo_box = GTK_COMBO_BOX (layout);
2209 g_object_ref (G_OBJECT (cell));
2210 gtk_object_sink (GTK_OBJECT (cell));
2212 info = g_new0 (ComboCellInfo, 1);
2214 info->expand = expand;
2215 info->pack = GTK_PACK_END;
2217 if (combo_box->priv->cell_view)
2218 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2221 if (combo_box->priv->column)
2222 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2224 menu = combo_box->priv->popup_widget;
2225 if (GTK_IS_MENU (menu))
2229 list = gtk_container_get_children (GTK_CONTAINER (menu));
2230 for (i = list; i; i = i->next)
2234 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2235 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2237 view = GTK_CELL_VIEW (i->data);
2239 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2246 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2249 GtkComboBox *combo_box;
2252 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2254 combo_box = GTK_COMBO_BOX (layout);
2256 if (combo_box->priv->cell_view)
2257 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2259 if (combo_box->priv->column)
2260 gtk_tree_view_column_clear (combo_box->priv->column);
2262 for (i = combo_box->priv->cells; i; i = i->next)
2264 ComboCellInfo *info = (ComboCellInfo *)i->data;
2266 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2267 g_object_unref (G_OBJECT (info->cell));
2271 g_slist_free (combo_box->priv->cells);
2272 combo_box->priv->cells = NULL;
2274 menu = combo_box->priv->popup_widget;
2275 if (GTK_IS_MENU (menu))
2279 list = gtk_container_get_children (GTK_CONTAINER (menu));
2280 for (i = list; i; i = i->next)
2284 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2285 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2287 view = GTK_CELL_VIEW (i->data);
2289 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2296 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2297 GtkCellRenderer *cell,
2298 const gchar *attribute,
2301 ComboCellInfo *info;
2302 GtkComboBox *combo_box;
2305 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2306 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2308 combo_box = GTK_COMBO_BOX (layout);
2310 info = gtk_combo_box_get_cell_info (combo_box, cell);
2312 info->attributes = g_slist_prepend (info->attributes,
2313 GINT_TO_POINTER (column));
2314 info->attributes = g_slist_prepend (info->attributes,
2315 g_strdup (attribute));
2317 if (combo_box->priv->cell_view)
2318 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2319 cell, attribute, column);
2321 if (combo_box->priv->column)
2322 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2323 cell, attribute, column);
2325 menu = combo_box->priv->popup_widget;
2326 if (GTK_IS_MENU (menu))
2330 list = gtk_container_get_children (GTK_CONTAINER (menu));
2331 for (i = list; i; i = i->next)
2335 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2336 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2338 view = GTK_CELL_VIEW (i->data);
2340 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2346 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2350 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2351 GtkCellRenderer *cell,
2352 GtkCellLayoutDataFunc func,
2354 GDestroyNotify destroy)
2356 ComboCellInfo *info;
2357 GtkComboBox *combo_box;
2360 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2362 combo_box = GTK_COMBO_BOX (layout);
2364 info = gtk_combo_box_get_cell_info (combo_box, cell);
2365 g_return_if_fail (info != NULL);
2369 GDestroyNotify d = info->destroy;
2371 info->destroy = NULL;
2372 d (info->func_data);
2376 info->func_data = func_data;
2377 info->destroy = destroy;
2379 if (combo_box->priv->cell_view)
2380 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2382 if (combo_box->priv->column)
2383 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2385 menu = combo_box->priv->popup_widget;
2386 if (GTK_IS_MENU (menu))
2390 list = gtk_container_get_children (GTK_CONTAINER (menu));
2391 for (i = list; i; i = i->next)
2395 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2396 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2398 view = GTK_CELL_VIEW (i->data);
2400 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2401 func, func_data, NULL);
2406 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2410 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2411 GtkCellRenderer *cell)
2413 ComboCellInfo *info;
2414 GtkComboBox *combo_box;
2418 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2419 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2421 combo_box = GTK_COMBO_BOX (layout);
2423 info = gtk_combo_box_get_cell_info (combo_box, cell);
2424 g_return_if_fail (info != NULL);
2426 list = info->attributes;
2427 while (list && list->next)
2429 g_free (list->data);
2430 list = list->next->next;
2432 g_slist_free (info->attributes);
2433 info->attributes = NULL;
2435 if (combo_box->priv->cell_view)
2436 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2438 if (combo_box->priv->column)
2439 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2441 menu = combo_box->priv->popup_widget;
2442 if (GTK_IS_MENU (menu))
2446 list = gtk_container_get_children (GTK_CONTAINER (menu));
2447 for (i = list; i; i = i->next)
2451 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2452 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2454 view = GTK_CELL_VIEW (i->data);
2456 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2461 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2465 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2466 GtkCellRenderer *cell,
2469 ComboCellInfo *info;
2470 GtkComboBox *combo_box;
2474 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2475 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2477 combo_box = GTK_COMBO_BOX (layout);
2479 info = gtk_combo_box_get_cell_info (combo_box, cell);
2481 g_return_if_fail (info != NULL);
2482 g_return_if_fail (position >= 0);
2484 link = g_slist_find (combo_box->priv->cells, info);
2486 g_return_if_fail (link != NULL);
2488 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2489 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2492 if (combo_box->priv->cell_view)
2493 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2496 if (combo_box->priv->column)
2497 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2500 menu = combo_box->priv->popup_widget;
2501 if (GTK_IS_MENU (menu))
2505 list = gtk_container_get_children (GTK_CONTAINER (menu));
2506 for (i = list; i; i = i->next)
2510 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2511 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2513 view = GTK_CELL_VIEW (i->data);
2515 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2520 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2528 * gtk_combo_box_new:
2530 * Creates a new empty #GtkComboBox.
2532 * Return value: A new #GtkComboBox.
2537 gtk_combo_box_new (void)
2539 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2543 * gtk_combo_box_new_with_model:
2544 * @model: A #GtkTreeModel.
2546 * Creates a new #GtkComboBox with the model initialized to @model.
2548 * Return value: A new #GtkComboBox.
2553 gtk_combo_box_new_with_model (GtkTreeModel *model)
2555 GtkComboBox *combo_box;
2557 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2559 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2563 return GTK_WIDGET (combo_box);
2567 * gtk_combo_box_set_wrap_width:
2568 * @combo_box: A #GtkComboBox.
2569 * @width: Preferred number of columns.
2571 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2572 * the preferred number of columns when you want to the popup to be layed out
2578 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2581 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2582 g_return_if_fail (width > 0);
2584 if (width != combo_box->priv->wrap_width)
2586 combo_box->priv->wrap_width = width;
2588 gtk_combo_box_relayout (combo_box);
2590 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2595 * gtk_combo_box_set_row_span_column:
2596 * @combo_box: A #GtkComboBox.
2597 * @row_span: A column in the model passed during construction.
2599 * Sets the column with row span information for @combo_box to be @row_span.
2600 * The row span column contains integers which indicate how many rows
2601 * an item should span.
2606 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2611 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2613 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2614 g_return_if_fail (row_span >= 0 && row_span < col);
2616 if (row_span != combo_box->priv->row_column)
2618 combo_box->priv->row_column = row_span;
2620 gtk_combo_box_relayout (combo_box);
2622 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2627 * gtk_combo_box_set_column_span_column:
2628 * @combo_box: A #GtkComboBox.
2629 * @column_span: A column in the model passed during construction.
2631 * Sets the column with column span information for @combo_box to be
2632 * @column_span. The column span column contains integers which indicate
2633 * how many columns an item should span.
2638 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2643 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2645 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2646 g_return_if_fail (column_span >= 0 && column_span < col);
2648 if (column_span != combo_box->priv->col_column)
2650 combo_box->priv->col_column = column_span;
2652 gtk_combo_box_relayout (combo_box);
2654 g_object_notify (G_OBJECT (combo_box), "column_span_column");
2659 * gtk_combo_box_get_active:
2660 * @combo_box: A #GtkComboBox.
2662 * Returns the index of the currently active item, or -1 if there's no
2665 * Return value: An integer which is the index of the currently active item, or
2666 * -1 if there's no active item.
2671 gtk_combo_box_get_active (GtkComboBox *combo_box)
2673 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
2675 return combo_box->priv->active_item;
2679 * gtk_combo_box_set_active:
2680 * @combo_box: A #GtkComboBox.
2681 * @index: An index in the model passed during construction, or -1 to have
2684 * Sets the active item of @combo_box to be the item at @index.
2689 gtk_combo_box_set_active (GtkComboBox *combo_box,
2694 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2695 /* -1 means "no item selected" */
2696 g_return_if_fail (index >= -1);
2698 if (combo_box->priv->active_item == index)
2701 combo_box->priv->active_item = index;
2705 if (combo_box->priv->tree_view)
2706 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
2709 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2711 if (GTK_IS_MENU (menu))
2712 gtk_menu_set_active (menu, -1);
2715 if (combo_box->priv->cell_view)
2716 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2720 path = gtk_tree_path_new_from_indices (index, -1);
2722 if (combo_box->priv->tree_view)
2723 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
2726 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2728 if (GTK_IS_MENU (menu))
2729 gtk_menu_set_active (GTK_MENU (menu), index);
2732 if (combo_box->priv->cell_view)
2733 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
2735 gtk_tree_path_free (path);
2738 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
2743 * gtk_combo_box_get_active_iter:
2744 * @combo_box: A #GtkComboBox
2745 * @iter: The uninitialized #GtkTreeIter.
2747 * Set @iter to point to the current active item, if it exists.
2749 * Return value: %TRUE, if @iter was set
2754 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
2761 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
2763 active = gtk_combo_box_get_active (combo_box);
2767 path = gtk_tree_path_new_from_indices (active, -1);
2768 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
2770 gtk_tree_path_free (path);
2776 * gtk_combo_box_set_active_iter:
2777 * @combo_box: A #GtkComboBox
2778 * @iter: The #GtkTreeIter.
2780 * Sets the current active item to be the one referenced by @iter.
2781 * @iter must correspond to a path of depth one.
2786 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
2791 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2793 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
2794 g_return_if_fail (path != NULL);
2795 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
2797 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2798 gtk_tree_path_free (path);
2802 * gtk_combo_box_set_model:
2803 * @combo_box: A #GtkComboBox.
2804 * @model: A #GtkTreeModel.
2806 * Sets the model used by @combo_box to be @model. Will unset a
2807 * previously set model (if applicable).
2812 gtk_combo_box_set_model (GtkComboBox *combo_box,
2813 GtkTreeModel *model)
2815 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2816 g_return_if_fail (GTK_IS_TREE_MODEL (model));
2818 if (combo_box->priv->model)
2820 gtk_combo_box_unset_model (combo_box);
2821 g_object_unref (G_OBJECT (combo_box->priv->model));
2824 combo_box->priv->model = model;
2825 g_object_ref (G_OBJECT (combo_box->priv->model));
2827 gtk_combo_box_set_model_internal (combo_box);
2828 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
2829 gtk_combo_box_menu_fill (combo_box);
2831 if (combo_box->priv->cell_view)
2832 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
2833 combo_box->priv->model);
2837 * gtk_combo_box_get_model
2838 * @combo_box: A #GtkComboBox.
2840 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
2842 * Return value: A #GtkTreeModel which was passed during construction.
2847 gtk_combo_box_get_model (GtkComboBox *combo_box)
2849 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
2851 return combo_box->priv->model;
2855 /* convenience API for simple text combos */
2858 * gtk_combo_box_new_text:
2860 * Convenience function which constructs a new text combo box, which is a
2861 * #GtkComboBox just displaying strings. If you use this function to create
2862 * a text combo box, you should only manipulate its data source with the
2863 * following convenience functions: gtk_combo_box_append_text(),
2864 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
2865 * gtk_combo_box_remove_text().
2867 * Return value: A new text combo box.
2872 gtk_combo_box_new_text (void)
2874 GtkWidget *combo_box;
2875 GtkCellRenderer *cell;
2876 GtkListStore *store;
2878 store = gtk_list_store_new (1, G_TYPE_STRING);
2880 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
2882 cell = gtk_cell_renderer_text_new ();
2883 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
2884 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
2892 * gtk_combo_box_append_text:
2893 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2896 * Appends @string to the list of strings stored in @combo_box. Note that
2897 * you can only use this function with combo boxes constructed with
2898 * gtk_combo_box_new_text().
2903 gtk_combo_box_append_text (GtkComboBox *combo_box,
2907 GtkListStore *store;
2909 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2910 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2911 g_return_if_fail (text != NULL);
2913 store = GTK_LIST_STORE (combo_box->priv->model);
2915 gtk_list_store_append (store, &iter);
2916 gtk_list_store_set (store, &iter, 0, text, -1);
2920 * gtk_combo_box_insert_text:
2921 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2922 * @position: An index to insert @text.
2925 * Inserts @string at @position in the list of strings stored in @combo_box.
2926 * Note that you can only use this function with combo boxes constructed
2927 * with gtk_combo_box_new_text().
2932 gtk_combo_box_insert_text (GtkComboBox *combo_box,
2937 GtkListStore *store;
2939 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2940 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2941 g_return_if_fail (position >= 0);
2942 g_return_if_fail (text != NULL);
2944 store = GTK_LIST_STORE (combo_box->priv->model);
2946 gtk_list_store_insert (store, &iter, position);
2947 gtk_list_store_set (store, &iter, 0, text, -1);
2951 * gtk_combo_box_prepend_text:
2952 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2955 * Prepends @string to the list of strings stored in @combo_box. Note that
2956 * you can only use this function with combo boxes constructed with
2957 * gtk_combo_box_new_text().
2962 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
2966 GtkListStore *store;
2968 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2969 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2970 g_return_if_fail (text != NULL);
2972 store = GTK_LIST_STORE (combo_box->priv->model);
2974 gtk_list_store_prepend (store, &iter);
2975 gtk_list_store_set (store, &iter, 0, text, -1);
2979 * gtk_combo_box_remove_text:
2980 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2981 * @position: Index of the item to remove.
2983 * Removes the string at @position from @combo_box. Note that you can only use
2984 * this function with combo boxes constructed with gtk_combo_box_new_text().
2989 gtk_combo_box_remove_text (GtkComboBox *combo_box,
2993 GtkListStore *store;
2995 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2996 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2997 g_return_if_fail (position >= 0);
2999 store = GTK_LIST_STORE (combo_box->priv->model);
3001 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3003 gtk_list_store_remove (store, &iter);
3008 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3009 gboolean group_cycling)
3011 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3013 gtk_widget_grab_focus (combo_box->priv->button);
3019 gtk_combo_box_destroy (GtkObject *object)
3021 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3023 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3025 combo_box->priv->cell_view = NULL;
3029 gtk_combo_box_finalize (GObject *object)
3031 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3034 gtk_combo_box_unset_model (combo_box);
3036 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3037 gtk_combo_box_menu_destroy (combo_box);
3039 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3040 gtk_combo_box_list_destroy (combo_box);
3042 if (combo_box->priv->popup_window)
3043 gtk_widget_destroy (combo_box->priv->popup_window);
3045 if (combo_box->priv->model)
3046 g_object_unref (combo_box->priv->model);
3048 for (i = combo_box->priv->cells; i; i = i->next)
3050 ComboCellInfo *info = (ComboCellInfo *)i->data;
3051 GSList *list = info->attributes;
3054 info->destroy (info->func_data);
3056 while (list && list->next)
3058 g_free (list->data);
3059 list = list->next->next;
3061 g_slist_free (info->attributes);
3063 g_object_unref (G_OBJECT (info->cell));
3066 g_slist_free (combo_box->priv->cells);
3068 G_OBJECT_CLASS (parent_class)->finalize (object);