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"
23 #include "gtkbindings.h"
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkcellview.h"
27 #include "gtkcellviewmenuitem.h"
28 #include "gtkeventbox.h"
30 #include "gtkliststore.h"
33 #include "gtktogglebutton.h"
34 #include "gtktreeselection.h"
35 #include "gtkvseparator.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,
195 static void gtk_combo_box_button_toggled (GtkWidget *widget,
197 static void gtk_combo_box_add (GtkContainer *container,
200 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
201 GtkCellRenderer *cell);
203 static void gtk_combo_box_menu_show (GtkWidget *menu,
205 static void gtk_combo_box_menu_hide (GtkWidget *menu,
208 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
210 static void gtk_combo_box_menu_position (GtkMenu *menu,
216 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
218 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
220 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
221 static void gtk_combo_box_set_model_internal (GtkComboBox *combo_box);
223 static void gtk_combo_box_size_request (GtkWidget *widget,
224 GtkRequisition *requisition);
225 static void gtk_combo_box_size_allocate (GtkWidget *widget,
226 GtkAllocation *allocation);
227 static void gtk_combo_box_forall (GtkContainer *container,
228 gboolean include_internals,
229 GtkCallback callback,
230 gpointer callback_data);
231 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
232 GdkEventExpose *event);
233 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
234 GdkEventScroll *event);
237 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
242 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
243 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
245 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
247 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
248 GdkEventButton *event,
250 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
253 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
254 GdkEventButton *event,
257 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
263 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
264 gboolean add_childs);
265 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
266 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
268 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
272 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
274 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
276 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
277 GdkEventButton *event,
279 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
281 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
285 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
288 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
294 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
295 GtkCellRenderer *cell,
297 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
298 GtkCellRenderer *cell,
300 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
301 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
302 GtkCellRenderer *cell,
303 const gchar *attribute,
305 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
306 GtkCellRenderer *cell,
307 GtkCellLayoutDataFunc func,
309 GDestroyNotify destroy);
310 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
311 GtkCellRenderer *cell);
312 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
313 GtkCellRenderer *cell,
315 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
316 gboolean group_cycling);
320 gtk_combo_box_get_type (void)
322 static GType combo_box_type = 0;
326 static const GTypeInfo combo_box_info =
328 sizeof (GtkComboBoxClass),
329 NULL, /* base_init */
330 NULL, /* base_finalize */
331 (GClassInitFunc) gtk_combo_box_class_init,
332 NULL, /* class_finalize */
333 NULL, /* class_data */
334 sizeof (GtkComboBox),
336 (GInstanceInitFunc) gtk_combo_box_init
339 static const GInterfaceInfo cell_layout_info =
341 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
346 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
351 g_type_add_interface_static (combo_box_type,
352 GTK_TYPE_CELL_LAYOUT,
356 return combo_box_type;
361 gtk_combo_box_class_init (GtkComboBoxClass *klass)
363 GObjectClass *object_class;
364 GtkBindingSet *binding_set;
365 GtkObjectClass *gtk_object_class;
366 GtkContainerClass *container_class;
367 GtkWidgetClass *widget_class;
369 binding_set = gtk_binding_set_by_class (klass);
371 container_class = (GtkContainerClass *)klass;
372 container_class->forall = gtk_combo_box_forall;
373 container_class->add = gtk_combo_box_add;
375 widget_class = (GtkWidgetClass *)klass;
376 widget_class->size_allocate = gtk_combo_box_size_allocate;
377 widget_class->size_request = gtk_combo_box_size_request;
378 widget_class->expose_event = gtk_combo_box_expose_event;
379 widget_class->scroll_event = gtk_combo_box_scroll_event;
380 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
381 widget_class->style_set = gtk_combo_box_style_set;
383 gtk_object_class = (GtkObjectClass *)klass;
384 gtk_object_class->destroy = gtk_combo_box_destroy;
386 object_class = (GObjectClass *)klass;
387 object_class->finalize = gtk_combo_box_finalize;
388 object_class->set_property = gtk_combo_box_set_property;
389 object_class->get_property = gtk_combo_box_get_property;
391 parent_class = g_type_class_peek_parent (klass);
394 combo_box_signals[CHANGED] =
395 g_signal_new ("changed",
396 G_OBJECT_CLASS_TYPE (klass),
398 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
400 g_cclosure_marshal_VOID__VOID,
404 g_object_class_install_property (object_class,
406 g_param_spec_object ("model",
407 P_("ComboBox model"),
408 P_("The model for the combo box"),
412 g_object_class_install_property (object_class,
414 g_param_spec_int ("wrap_width",
416 P_("Wrap width for layouting the items in a grid"),
422 g_object_class_install_property (object_class,
423 PROP_ROW_SPAN_COLUMN,
424 g_param_spec_int ("row_span_column",
425 P_("Row span column"),
426 P_("TreeModel column containing the row span values"),
432 g_object_class_install_property (object_class,
433 PROP_COLUMN_SPAN_COLUMN,
434 g_param_spec_int ("column_span_column",
435 P_("Column span column"),
436 P_("TreeModel column containing the column span values"),
442 g_object_class_install_property (object_class,
444 g_param_spec_int ("active",
446 P_("The item which is currently active"),
452 gtk_widget_class_install_style_property (widget_class,
453 g_param_spec_boolean ("appearance",
454 P_("ComboBox appareance"),
455 P_("ComboBox appearance, where TRUE means Windows-style."),
459 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
463 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
465 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
466 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
467 iface->clear = gtk_combo_box_cell_layout_clear;
468 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
469 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
470 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
471 iface->reorder = gtk_combo_box_cell_layout_reorder;
475 gtk_combo_box_init (GtkComboBox *combo_box)
477 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
479 combo_box->priv->cell_view = gtk_cell_view_new ();
480 gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
481 gtk_widget_show (combo_box->priv->cell_view);
483 combo_box->priv->width = 0;
484 combo_box->priv->wrap_width = 0;
486 combo_box->priv->active_item = -1;
487 combo_box->priv->col_column = -1;
488 combo_box->priv->row_column = -1;
492 gtk_combo_box_set_property (GObject *object,
497 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
502 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
505 case PROP_WRAP_WIDTH:
506 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
509 case PROP_ROW_SPAN_COLUMN:
510 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
513 case PROP_COLUMN_SPAN_COLUMN:
514 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
518 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
527 gtk_combo_box_get_property (GObject *object,
532 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
537 g_value_set_object (value, combo_box->priv->model);
540 case PROP_WRAP_WIDTH:
541 g_value_set_int (value, combo_box->priv->wrap_width);
544 case PROP_ROW_SPAN_COLUMN:
545 g_value_set_int (value, combo_box->priv->row_column);
548 case PROP_COLUMN_SPAN_COLUMN:
549 g_value_set_int (value, combo_box->priv->col_column);
553 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
557 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
563 gtk_combo_box_style_set (GtkWidget *widget,
567 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
569 gtk_widget_queue_resize (widget);
571 /* if wrap_width > 0, then we are in grid-mode and forced to use
574 if (combo_box->priv->wrap_width)
577 gtk_widget_style_get (widget,
578 "appearance", &appearance,
581 /* TRUE is windows style */
584 /* Destroy all the menu mode widgets, if they exist. */
585 if (GTK_IS_MENU (combo_box->priv->popup_widget))
586 gtk_combo_box_menu_destroy (combo_box);
588 /* Create the list mode widgets, if they don't already exist. */
589 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
590 gtk_combo_box_list_setup (combo_box);
594 /* Destroy all the list mode widgets, if they exist. */
595 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
596 gtk_combo_box_list_destroy (combo_box);
598 /* Create the menu mode widgets, if they don't already exist. */
599 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
600 gtk_combo_box_menu_setup (combo_box, TRUE);
605 gtk_combo_box_button_toggled (GtkWidget *widget,
608 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
610 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
612 if (!combo_box->priv->popup_in_progress)
613 gtk_combo_box_popup (combo_box);
616 gtk_combo_box_popdown (combo_box);
620 gtk_combo_box_add (GtkContainer *container,
623 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
625 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
626 gtk_container_remove (container, combo_box->priv->cell_view);
628 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
630 if (combo_box->priv->cell_view &&
631 widget != combo_box->priv->cell_view)
633 /* since the cell_view was unparented, it's gone now */
634 combo_box->priv->cell_view = NULL;
636 if (!combo_box->priv->tree_view && combo_box->priv->separator)
638 gtk_widget_unparent (combo_box->priv->separator);
639 combo_box->priv->separator = NULL;
641 g_object_ref (G_OBJECT (combo_box->priv->arrow));
642 gtk_widget_unparent (combo_box->priv->arrow);
643 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
644 combo_box->priv->arrow);
645 g_object_unref (G_OBJECT (combo_box->priv->arrow));
647 gtk_widget_queue_resize (GTK_WIDGET (container));
649 else if (combo_box->priv->cell_view_frame)
651 gtk_widget_unparent (combo_box->priv->cell_view_frame);
652 combo_box->priv->cell_view_frame = NULL;
657 static ComboCellInfo *
658 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
659 GtkCellRenderer *cell)
663 for (i = combo_box->priv->cells; i; i = i->next)
665 ComboCellInfo *info = (ComboCellInfo *)i->data;
667 if (info && info->cell == cell)
675 gtk_combo_box_menu_show (GtkWidget *menu,
678 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
680 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
682 combo_box->priv->popup_in_progress = FALSE;
686 gtk_combo_box_menu_hide (GtkWidget *menu,
689 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
691 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
696 gtk_combo_box_detacher (GtkWidget *widget,
699 GtkComboBox *combo_box;
701 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
703 combo_box = GTK_COMBO_BOX (widget);
704 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
706 g_signal_handlers_disconnect_by_func (menu,
707 gtk_combo_box_menu_show,
709 g_signal_handlers_disconnect_by_func (menu,
710 gtk_combo_box_menu_hide,
713 combo_box->priv->popup_widget = NULL;
717 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
720 if (GTK_IS_MENU (combo_box->priv->popup_widget))
722 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
723 combo_box->priv->popup_widget = NULL;
725 else if (combo_box->priv->popup_widget)
727 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
728 combo_box->priv->popup_widget);
729 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
730 combo_box->priv->popup_widget = NULL;
733 if (GTK_IS_MENU (popup))
735 if (combo_box->priv->popup_window)
737 gtk_widget_destroy (combo_box->priv->popup_window);
738 combo_box->priv->popup_window = NULL;
739 combo_box->priv->popup_frame = NULL;
742 combo_box->priv->popup_widget = popup;
744 g_signal_connect (popup, "show",
745 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
746 g_signal_connect (popup, "hide",
747 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
749 gtk_menu_attach_to_widget (GTK_MENU (popup),
750 GTK_WIDGET (combo_box),
751 gtk_combo_box_detacher);
755 if (!combo_box->priv->popup_window)
757 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
758 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
759 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
760 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
762 combo_box->priv->popup_frame = gtk_frame_new (NULL);
763 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
764 GTK_SHADOW_ETCHED_IN);
765 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
766 combo_box->priv->popup_frame);
768 gtk_widget_show (combo_box->priv->popup_frame);
771 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
773 gtk_widget_show (popup);
774 g_object_ref (G_OBJECT (popup));
775 combo_box->priv->popup_widget = popup;
780 gtk_combo_box_menu_position (GtkMenu *menu,
789 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
791 /* FIXME: is using the size request here broken? */
792 child = GTK_BIN (combo_box)->child;
794 gdk_window_get_origin (child->window, &sx, &sy);
796 gtk_widget_size_request (GTK_WIDGET (menu), &req);
798 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
801 *x = sx + child->allocation.width - req.width;
802 *y = sy + child->allocation.height;
804 if (GTK_WIDGET_NO_WINDOW (child))
806 *x += child->allocation.x;
807 *y += child->allocation.y;
814 gtk_combo_box_list_position (GtkComboBox *combo_box,
823 GdkRectangle monitor;
824 GtkRequisition popup_req;
826 sample = GTK_BIN (combo_box)->child;
828 *width = sample->allocation.width;
829 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
830 *height = popup_req.height;
832 gdk_window_get_origin (sample->window, x, y);
834 if (combo_box->priv->cell_view_frame)
836 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
837 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
838 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
839 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
842 if (GTK_WIDGET_NO_WINDOW (sample))
844 *x += sample->allocation.x;
845 *y += sample->allocation.y;
848 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
849 monitor_num = gdk_screen_get_monitor_at_window (screen,
850 GTK_WIDGET (combo_box)->window);
851 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
855 else if (*x + *width > monitor.x + monitor.width)
856 *x = monitor.x + monitor.width - *width;
858 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
859 *y += sample->allocation.height;
865 * gtk_combo_box_popup:
866 * @combo_box: a #GtkComboBox
868 * Pops up the menu or dropdown list of @combo_box.
870 * This function is mostly intended for use by accessibility technologies;
871 * applications should have little use for it.
876 gtk_combo_box_popup (GtkComboBox *combo_box)
878 gint x, y, width, height;
880 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
882 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
885 if (GTK_IS_MENU (combo_box->priv->popup_widget))
887 if (combo_box->priv->active_item != -1)
891 childs = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
892 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
893 g_list_nth_data (childs, combo_box->priv->active_item));
894 g_list_free (childs);
897 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
899 gtk_combo_box_menu_position, combo_box,
904 gtk_widget_show_all (combo_box->priv->popup_frame);
905 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
907 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
908 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
911 gtk_widget_show (combo_box->priv->popup_window);
913 gtk_widget_grab_focus (combo_box->priv->popup_window);
914 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
917 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
919 gdk_keyboard_grab (combo_box->priv->popup_window->window,
920 FALSE, GDK_CURRENT_TIME);
921 gtk_widget_grab_focus (combo_box->priv->tree_view);
924 gtk_grab_add (combo_box->priv->popup_window);
925 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
926 GDK_BUTTON_PRESS_MASK |
927 GDK_BUTTON_RELEASE_MASK |
928 GDK_POINTER_MOTION_MASK,
929 NULL, NULL, GDK_CURRENT_TIME);
931 gtk_grab_add (combo_box->priv->tree_view);
935 * gtk_combo_box_popdown:
936 * @combo_box: a #GtkComboBox
938 * Hides the menu or dropdown list of @combo_box.
940 * This function is mostly intended for use by accessibility technologies;
941 * applications should have little use for it.
946 gtk_combo_box_popdown (GtkComboBox *combo_box)
948 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
950 if (GTK_IS_MENU (combo_box->priv->popup_widget))
952 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
956 gtk_combo_box_list_remove_grabs (combo_box);
957 gtk_widget_hide_all (combo_box->priv->popup_window);
958 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
963 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
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;
979 if (combo_box->priv->cell_view)
980 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
985 return req.width + padding;
989 gtk_combo_box_remeasure (GtkComboBox *combo_box)
995 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
998 combo_box->priv->width = 0;
1000 path = gtk_tree_path_new_from_indices (0, -1);
1002 if (combo_box->priv->cell_view)
1003 gtk_widget_style_get (combo_box->priv->cell_view,
1004 "focus-line-width", &padding,
1009 /* add some pixels for good measure */
1010 padding += BONUS_PADDING;
1016 if (combo_box->priv->cell_view)
1017 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1022 combo_box->priv->width = MAX (combo_box->priv->width,
1023 req.width + padding);
1025 gtk_tree_path_next (path);
1027 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1029 gtk_tree_path_free (path);
1033 gtk_combo_box_size_request (GtkWidget *widget,
1034 GtkRequisition *requisition)
1037 GtkRequisition bin_req;
1039 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1042 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1043 gtk_combo_box_remeasure (combo_box);
1044 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1046 if (!combo_box->priv->tree_view)
1050 if (combo_box->priv->cell_view)
1052 GtkRequisition sep_req, arrow_req;
1053 gint border_width, xthickness, ythickness;
1055 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1056 xthickness = combo_box->priv->button->style->xthickness;
1057 ythickness = combo_box->priv->button->style->ythickness;
1059 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1061 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1062 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1064 height = MAX (sep_req.height, arrow_req.height);
1065 height = MAX (height, bin_req.height);
1067 width = bin_req.width + sep_req.width + arrow_req.width;
1069 height += border_width + 1 + xthickness * 2 + 4;
1070 width += border_width + 1 + ythickness * 2 + 4;
1072 requisition->width = width;
1073 requisition->height = height;
1077 GtkRequisition but_req;
1079 gtk_widget_size_request (combo_box->priv->button, &but_req);
1081 requisition->width = bin_req.width + but_req.width;
1082 requisition->height = MAX (bin_req.height, but_req.height);
1088 GtkRequisition button_req;
1090 /* sample + frame */
1091 *requisition = bin_req;
1093 if (combo_box->priv->cell_view_frame)
1095 requisition->width += 2 *
1096 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1097 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1098 requisition->height += 2 *
1099 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1100 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1104 gtk_widget_size_request (combo_box->priv->button, &button_req);
1106 requisition->height = MAX (requisition->height, button_req.height);
1107 requisition->width += button_req.width;
1112 gtk_combo_box_size_allocate (GtkWidget *widget,
1113 GtkAllocation *allocation)
1115 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1116 GtkAllocation child;
1118 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1120 widget->allocation = *allocation;
1122 if (!combo_box->priv->tree_view)
1124 if (combo_box->priv->cell_view)
1126 gint border_width, xthickness, ythickness;
1130 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1132 /* set some things ready */
1133 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1134 xthickness = combo_box->priv->button->style->xthickness;
1135 ythickness = combo_box->priv->button->style->ythickness;
1137 child.x = allocation->x + border_width + 1 + xthickness + 2;
1138 child.y = allocation->y + border_width + 1 + ythickness + 2;
1140 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1142 /* handle the childs */
1143 gtk_widget_size_request (combo_box->priv->arrow, &req);
1144 child.width = req.width;
1145 child.height = allocation->height - 2 * (child.y - allocation->y);
1147 child.x += width - req.width;
1148 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1150 child.x += req.width;
1151 gtk_widget_size_request (combo_box->priv->separator, &req);
1152 child.width = req.width;
1154 child.x -= req.width;
1155 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1159 child.x += req.width;
1160 child.width = allocation->x + allocation->width
1161 - (border_width + 1 + xthickness + 2) - child.x;
1165 child.width = child.x;
1166 child.x = allocation->x + border_width + 1 + xthickness + 2;
1167 child.width -= child.x;
1170 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1174 gtk_widget_size_request (combo_box->priv->button, &req);
1176 child.x = allocation->x;
1178 child.x = allocation->x + allocation->width - req.width;
1179 child.y = allocation->y;
1180 child.width = req.width;
1181 child.height = allocation->height;
1182 gtk_widget_size_allocate (combo_box->priv->button, &child);
1185 child.x = allocation->x + req.width;
1187 child.x = allocation->x;
1188 child.y = allocation->y;
1189 child.width = allocation->width - req.width;
1190 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1198 gtk_widget_size_request (combo_box->priv->button, &req);
1200 child.x = allocation->x;
1202 child.x = allocation->x + allocation->width - req.width;
1203 child.y = allocation->y;
1204 child.width = req.width;
1205 child.height = allocation->height;
1206 gtk_widget_size_allocate (combo_box->priv->button, &child);
1210 child.x = allocation->x + req.width;
1212 child.x = allocation->x;
1213 child.y = allocation->y;
1214 child.width = allocation->width - req.width;
1215 child.height = allocation->height;
1217 if (combo_box->priv->cell_view_frame)
1219 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1223 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1224 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1226 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1227 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1228 child.width -= 2 * (
1229 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1230 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1231 child.height -= 2 * (
1232 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1233 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1236 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1241 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1243 if (combo_box->priv->inserted_id != -1)
1245 g_signal_handler_disconnect (combo_box->priv->model,
1246 combo_box->priv->inserted_id);
1247 combo_box->priv->inserted_id = -1;
1249 if (combo_box->priv->deleted_id != -1)
1251 g_signal_handler_disconnect (combo_box->priv->model,
1252 combo_box->priv->deleted_id);
1253 combo_box->priv->deleted_id = -1;
1255 if (combo_box->priv->changed_id != -1)
1257 g_signal_handler_disconnect (combo_box->priv->model,
1258 combo_box->priv->changed_id);
1259 combo_box->priv->changed_id = -1;
1263 if (!combo_box->priv->tree_view)
1265 if (combo_box->priv->popup_widget)
1266 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1267 (GtkCallback)gtk_widget_destroy, NULL);
1272 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1274 if (!combo_box->priv->tree_view)
1277 combo_box->priv->inserted_id =
1278 g_signal_connect (combo_box->priv->model, "row_inserted",
1279 G_CALLBACK (gtk_combo_box_menu_row_inserted),
1281 combo_box->priv->deleted_id =
1282 g_signal_connect (combo_box->priv->model, "row_deleted",
1283 G_CALLBACK (gtk_combo_box_menu_row_deleted),
1285 combo_box->priv->changed_id =
1286 g_signal_connect (combo_box->priv->model, "row_changed",
1287 G_CALLBACK (gtk_combo_box_menu_row_changed),
1293 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1294 combo_box->priv->model);
1296 combo_box->priv->changed_id =
1297 g_signal_connect (combo_box->priv->model, "row_changed",
1298 G_CALLBACK (gtk_combo_box_list_row_changed),
1304 gtk_combo_box_forall (GtkContainer *container,
1305 gboolean include_internals,
1306 GtkCallback callback,
1307 gpointer callback_data)
1309 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1311 if (include_internals)
1313 if (combo_box->priv->button)
1314 (* callback) (combo_box->priv->button, callback_data);
1315 if (combo_box->priv->separator)
1316 (* callback) (combo_box->priv->separator, callback_data);
1317 if (combo_box->priv->arrow)
1318 (* callback) (combo_box->priv->arrow, callback_data);
1319 if (combo_box->priv->cell_view_frame)
1320 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1323 if (GTK_BIN (container)->child)
1324 (* callback) (GTK_BIN (container)->child, callback_data);
1328 gtk_combo_box_expose_event (GtkWidget *widget,
1329 GdkEventExpose *event)
1331 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1333 if (!combo_box->priv->tree_view)
1335 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1336 combo_box->priv->button, event);
1338 if (combo_box->priv->separator)
1340 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1341 combo_box->priv->separator, event);
1343 /* if not in this case, arrow gets its expose event from button */
1344 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1345 combo_box->priv->arrow, event);
1350 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1351 combo_box->priv->button, event);
1353 if (combo_box->priv->cell_view_frame)
1354 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1355 combo_box->priv->cell_view_frame, event);
1358 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1359 GTK_BIN (widget)->child, event);
1365 gtk_combo_box_scroll_event (GtkWidget *widget,
1366 GdkEventScroll *event)
1368 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1372 index = gtk_combo_box_get_active (combo_box);
1376 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1378 if (event->direction == GDK_SCROLL_UP)
1383 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1394 cell_view_sync_cells (GtkComboBox *combo_box,
1395 GtkCellView *cell_view)
1399 for (k = combo_box->priv->cells; k; k = k->next)
1402 ComboCellInfo *info = (ComboCellInfo *)k->data;
1404 if (info->pack == GTK_PACK_START)
1405 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1406 info->cell, info->expand);
1407 else if (info->pack == GTK_PACK_END)
1408 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1409 info->cell, info->expand);
1411 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1413 info->func, info->func_data, NULL);
1415 for (j = info->attributes; j; j = j->next->next)
1417 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1420 GPOINTER_TO_INT (j->next->data));
1426 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1427 gboolean add_childs)
1431 /* Unset any existing model. */
1432 gtk_combo_box_unset_model (combo_box);
1434 if (combo_box->priv->cell_view)
1436 combo_box->priv->button = gtk_toggle_button_new ();
1437 g_signal_connect (combo_box->priv->button, "toggled",
1438 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1439 gtk_widget_set_parent (combo_box->priv->button,
1440 GTK_BIN (combo_box)->child->parent);
1442 combo_box->priv->separator = gtk_vseparator_new ();
1443 gtk_widget_set_parent (combo_box->priv->separator,
1444 combo_box->priv->button);
1445 gtk_widget_show (combo_box->priv->separator);
1447 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1448 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1449 gtk_widget_show (combo_box->priv->arrow);
1451 gtk_widget_show_all (combo_box->priv->button);
1453 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1455 /* I have no clue why, but we need to manually map in this case. */
1456 gtk_widget_map (combo_box->priv->separator);
1457 gtk_widget_map (combo_box->priv->arrow);
1462 combo_box->priv->button = gtk_toggle_button_new ();
1463 g_signal_connect (combo_box->priv->button, "toggled",
1464 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1465 gtk_widget_set_parent (combo_box->priv->button,
1466 GTK_BIN (combo_box)->child->parent);
1468 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1469 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1470 combo_box->priv->arrow);
1471 gtk_widget_show_all (combo_box->priv->button);
1474 g_signal_connect (combo_box->priv->button, "button_press_event",
1475 G_CALLBACK (gtk_combo_box_menu_button_press),
1478 /* create our funky menu */
1479 box = gtk_menu_new ();
1480 gtk_combo_box_set_popup_widget (combo_box, box);
1482 /* set the models */
1483 gtk_combo_box_set_model_internal (combo_box);
1487 gtk_combo_box_menu_fill (combo_box);
1492 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1498 if (!combo_box->priv->model)
1501 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1502 menu = combo_box->priv->popup_widget;
1504 for (i = 0; i < items; i++)
1508 path = gtk_tree_path_new_from_indices (i, -1);
1509 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1511 g_signal_connect (tmp, "activate",
1512 G_CALLBACK (gtk_combo_box_menu_item_activate),
1515 cell_view_sync_cells (combo_box,
1516 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1518 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1519 gtk_widget_show (tmp);
1521 gtk_tree_path_free (path);
1526 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1528 /* disconnect signal handlers */
1529 gtk_combo_box_unset_model (combo_box);
1531 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1532 G_SIGNAL_MATCH_DATA,
1534 gtk_combo_box_menu_button_press, NULL);
1536 /* unparent will remove our latest ref */
1537 if (combo_box->priv->cell_view)
1539 gtk_widget_unparent (combo_box->priv->arrow);
1540 combo_box->priv->arrow = NULL;
1542 gtk_widget_unparent (combo_box->priv->separator);
1543 combo_box->priv->separator = NULL;
1545 gtk_widget_unparent (combo_box->priv->button);
1546 combo_box->priv->button = NULL;
1550 /* will destroy the arrow too */
1551 gtk_widget_unparent (combo_box->priv->button);
1553 combo_box->priv->button = NULL;
1554 combo_box->priv->arrow = NULL;
1557 /* changing the popup window will unref the menu and the childs */
1565 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1572 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1576 if (combo_box->priv->col_column == -1)
1579 gtk_tree_model_get (combo_box->priv->model, &iter,
1580 combo_box->priv->col_column, cols,
1586 if (combo_box->priv->row_column == -1)
1589 gtk_tree_model_get (combo_box->priv->model, &iter,
1590 combo_box->priv->row_column, rows,
1596 menu_occupied (GtkMenu *menu,
1600 guint bottom_attach)
1604 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1605 g_return_val_if_fail (left_attach < right_attach, TRUE);
1606 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1608 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1611 gboolean h_intersect = FALSE;
1612 gboolean v_intersect = FALSE;
1614 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1617 "bottom_attach", &b,
1621 /* look if this item intersects with the given coordinates */
1622 h_intersect = left_attach <= l && l <= right_attach;
1623 h_intersect &= left_attach <= r && r <= right_attach;
1625 v_intersect = top_attach <= t && t <= bottom_attach;
1626 v_intersect &= top_attach <= b && b <= bottom_attach;
1628 if (h_intersect && v_intersect)
1636 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1639 gint current_col = 0, current_row = 0;
1645 menu = combo_box->priv->popup_widget;
1646 if (!GTK_IS_MENU_SHELL (menu))
1649 list = gtk_container_get_children (GTK_CONTAINER (menu));
1650 item = g_list_nth_data (list, index);
1653 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1655 /* look for a good spot */
1658 if (current_col + cols > combo_box->priv->wrap_width)
1664 if (!menu_occupied (GTK_MENU (menu),
1665 current_col, current_col + cols,
1666 current_row, current_row + rows))
1672 /* set attach props */
1673 gtk_menu_attach (GTK_MENU (menu), item,
1674 current_col, current_col + cols,
1675 current_row, current_row + rows);
1679 gtk_combo_box_relayout (GtkComboBox *combo_box)
1685 /* ensure we are in menu style */
1686 if (combo_box->priv->tree_view)
1687 gtk_combo_box_list_destroy (combo_box);
1689 menu = combo_box->priv->popup_widget;
1691 if (!GTK_IS_MENU_SHELL (menu))
1693 gtk_combo_box_menu_setup (combo_box, FALSE);
1694 menu = combo_box->priv->popup_widget;
1697 /* get rid of all children */
1698 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1700 list = gtk_container_get_children (GTK_CONTAINER (menu));
1702 for (j = g_list_last (list); j; j = j->prev)
1703 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1708 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1710 for (i = 0; i < items; i++)
1715 path = gtk_tree_path_new_from_indices (i, -1);
1716 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1719 g_signal_connect (tmp, "activate",
1720 G_CALLBACK (gtk_combo_box_menu_item_activate),
1723 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1725 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1727 if (combo_box->priv->wrap_width)
1728 gtk_combo_box_relayout_item (combo_box, i);
1730 gtk_widget_show (tmp);
1732 gtk_tree_path_free (path);
1738 gtk_combo_box_menu_button_press (GtkWidget *widget,
1739 GdkEventButton *event,
1742 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1744 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1747 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1749 combo_box->priv->popup_in_progress = TRUE;
1750 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1752 gtk_combo_box_menu_position, combo_box,
1753 event->button, event->time);
1762 gtk_combo_box_menu_item_activate (GtkWidget *item,
1767 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1769 menu = combo_box->priv->popup_widget;
1770 g_return_if_fail (GTK_IS_MENU (menu));
1772 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1774 gtk_combo_box_set_active (combo_box, index);
1778 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
1785 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1787 if (!combo_box->priv->popup_widget)
1790 menu = combo_box->priv->popup_widget;
1791 g_return_if_fail (GTK_IS_MENU (menu));
1793 item = gtk_cell_view_menu_item_new_from_model (model, path);
1794 g_signal_connect (item, "activate",
1795 G_CALLBACK (gtk_combo_box_menu_item_activate),
1798 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
1800 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
1801 gtk_tree_path_get_indices (path)[0]);
1802 gtk_widget_show (item);
1806 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
1813 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1815 if (!combo_box->priv->popup_widget)
1818 index = gtk_tree_path_get_indices (path)[0];
1819 items = gtk_tree_model_iter_n_children (model, NULL);
1821 if (gtk_combo_box_get_active (combo_box) == index)
1822 gtk_combo_box_set_active (combo_box, index + 1 % items);
1824 menu = combo_box->priv->popup_widget;
1825 g_return_if_fail (GTK_IS_MENU (menu));
1827 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
1828 g_return_if_fail (GTK_IS_MENU_ITEM (item));
1830 gtk_container_remove (GTK_CONTAINER (menu), item);
1834 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
1839 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1842 if (!combo_box->priv->popup_widget)
1845 if (combo_box->priv->wrap_width)
1846 gtk_combo_box_relayout_item (combo_box,
1847 gtk_tree_path_get_indices (path)[0]);
1849 width = gtk_combo_box_calc_requested_width (combo_box, path);
1851 if (width > combo_box->priv->width)
1853 if (combo_box->priv->cell_view)
1855 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
1856 gtk_widget_queue_resize (combo_box->priv->cell_view);
1858 combo_box->priv->width = width;
1867 gtk_combo_box_list_setup (GtkComboBox *combo_box)
1870 GtkTreeSelection *sel;
1872 /* Unset any existing model. */
1873 gtk_combo_box_unset_model (combo_box);
1875 combo_box->priv->button = gtk_toggle_button_new ();
1876 gtk_widget_set_parent (combo_box->priv->button,
1877 GTK_BIN (combo_box)->child->parent);
1878 g_signal_connect (combo_box->priv->button, "button_press_event",
1879 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
1880 g_signal_connect (combo_box->priv->button, "toggled",
1881 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1883 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1884 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1885 combo_box->priv->arrow);
1886 combo_box->priv->separator = NULL;
1887 gtk_widget_show_all (combo_box->priv->button);
1889 if (combo_box->priv->cell_view)
1891 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
1892 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
1893 GTK_BIN (combo_box)->child->parent);
1894 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
1897 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1898 "background", "white",
1899 "background_set", TRUE,
1902 gtk_widget_show (combo_box->priv->cell_view_frame);
1905 combo_box->priv->tree_view = gtk_tree_view_new ();
1906 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
1907 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
1908 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
1911 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
1912 G_CALLBACK (gtk_combo_box_list_button_pressed),
1914 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
1915 G_CALLBACK (gtk_combo_box_list_button_released),
1917 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
1918 G_CALLBACK (gtk_combo_box_list_key_press),
1921 combo_box->priv->column = gtk_tree_view_column_new ();
1922 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
1923 combo_box->priv->column);
1925 /* set the models */
1926 gtk_combo_box_set_model_internal (combo_box);
1929 for (i = combo_box->priv->cells; i; i = i->next)
1932 ComboCellInfo *info = (ComboCellInfo *)i->data;
1934 if (info->pack == GTK_PACK_START)
1935 gtk_tree_view_column_pack_start (combo_box->priv->column,
1936 info->cell, info->expand);
1937 else if (info->pack == GTK_PACK_END)
1938 gtk_tree_view_column_pack_end (combo_box->priv->column,
1939 info->cell, info->expand);
1941 for (j = info->attributes; j; j = j->next->next)
1943 gtk_tree_view_column_add_attribute (combo_box->priv->column,
1946 GPOINTER_TO_INT (j->next->data));
1950 if (combo_box->priv->active_item != -1)
1954 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
1957 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1959 gtk_tree_path_free (path);
1963 /* set sample/popup widgets */
1964 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
1966 gtk_widget_show (combo_box->priv->tree_view);
1970 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
1972 /* disconnect signals */
1973 gtk_combo_box_unset_model (combo_box);
1975 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
1976 G_SIGNAL_MATCH_DATA,
1977 0, 0, NULL, NULL, combo_box);
1978 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1979 G_SIGNAL_MATCH_DATA,
1981 gtk_combo_box_list_button_pressed,
1984 /* destroy things (unparent will kill the latest ref from us)
1985 * last unref on button will destroy the arrow
1987 gtk_widget_unparent (combo_box->priv->button);
1988 combo_box->priv->button = NULL;
1989 combo_box->priv->arrow = NULL;
1991 if (combo_box->priv->cell_view)
1993 g_object_set (G_OBJECT (combo_box->priv->cell_view),
1994 "background_set", FALSE,
1997 gtk_widget_unparent (combo_box->priv->cell_view_frame);
1998 combo_box->priv->cell_view_frame = NULL;
2001 gtk_widget_destroy (combo_box->priv->tree_view);
2003 combo_box->priv->tree_view = NULL;
2004 combo_box->priv->popup_widget = NULL;
2009 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2011 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2012 gtk_grab_remove (combo_box->priv->tree_view);
2014 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2016 gtk_grab_remove (combo_box->priv->popup_window);
2017 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2018 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2023 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2024 GdkEventButton *event,
2027 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2029 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2031 if (ewidget == combo_box->priv->tree_view)
2034 if ((ewidget != combo_box->priv->button) ||
2035 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2038 gtk_combo_box_popup (combo_box);
2040 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2043 combo_box->priv->popup_in_progress = TRUE;
2049 gtk_combo_box_list_button_released (GtkWidget *widget,
2050 GdkEventButton *event,
2054 GtkTreePath *path = NULL;
2056 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2058 gboolean popup_in_progress = FALSE;
2060 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2062 if (combo_box->priv->popup_in_progress)
2064 popup_in_progress = TRUE;
2065 combo_box->priv->popup_in_progress = FALSE;
2068 if (ewidget != combo_box->priv->tree_view)
2070 if (ewidget == combo_box->priv->button &&
2071 !popup_in_progress &&
2072 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2074 gtk_combo_box_popdown (combo_box);
2078 /* released outside treeview */
2079 if (ewidget != combo_box->priv->button)
2081 gtk_combo_box_popdown (combo_box);
2089 /* select something cool */
2090 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2096 return TRUE; /* clicked outside window? */
2098 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2099 gtk_combo_box_popdown (combo_box);
2101 gtk_tree_path_free (path);
2107 gtk_combo_box_list_key_press (GtkWidget *widget,
2111 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2113 if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2114 event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
2115 event->keyval == GDK_Escape)
2117 if (event->keyval != GDK_Escape)
2121 GtkTreeModel *model = NULL;
2122 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2124 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2129 path = gtk_tree_model_get_path (model, &iter);
2132 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2133 gtk_tree_path_free (path);
2138 /* reset active item -- this is incredibly lame and ugly */
2139 gtk_combo_box_set_active (combo_box,
2140 gtk_combo_box_get_active (combo_box));
2142 gtk_combo_box_popdown (combo_box);
2151 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2156 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2159 width = gtk_combo_box_calc_requested_width (combo_box, path);
2161 if (width > combo_box->priv->width)
2163 if (combo_box->priv->cell_view)
2165 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2166 gtk_widget_queue_resize (combo_box->priv->cell_view);
2168 combo_box->priv->width = width;
2173 * GtkCellLayout implementation
2176 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2177 GtkCellRenderer *cell,
2180 ComboCellInfo *info;
2181 GtkComboBox *combo_box;
2184 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2185 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2187 combo_box = GTK_COMBO_BOX (layout);
2189 g_object_ref (G_OBJECT (cell));
2190 gtk_object_sink (GTK_OBJECT (cell));
2192 info = g_new0 (ComboCellInfo, 1);
2194 info->expand = expand;
2195 info->pack = GTK_PACK_START;
2197 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2199 if (combo_box->priv->cell_view)
2200 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2203 if (combo_box->priv->column)
2204 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2206 menu = combo_box->priv->popup_widget;
2207 if (GTK_IS_MENU (menu))
2211 list = gtk_container_get_children (GTK_CONTAINER (menu));
2212 for (i = list; i; i = i->next)
2216 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2217 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2219 view = GTK_CELL_VIEW (i->data);
2221 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2228 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2229 GtkCellRenderer *cell,
2232 ComboCellInfo *info;
2233 GtkComboBox *combo_box;
2236 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2237 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2239 combo_box = GTK_COMBO_BOX (layout);
2241 g_object_ref (G_OBJECT (cell));
2242 gtk_object_sink (GTK_OBJECT (cell));
2244 info = g_new0 (ComboCellInfo, 1);
2246 info->expand = expand;
2247 info->pack = GTK_PACK_END;
2249 if (combo_box->priv->cell_view)
2250 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2253 if (combo_box->priv->column)
2254 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2256 menu = combo_box->priv->popup_widget;
2257 if (GTK_IS_MENU (menu))
2261 list = gtk_container_get_children (GTK_CONTAINER (menu));
2262 for (i = list; i; i = i->next)
2266 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2267 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2269 view = GTK_CELL_VIEW (i->data);
2271 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2278 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2281 GtkComboBox *combo_box;
2284 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2286 combo_box = GTK_COMBO_BOX (layout);
2288 if (combo_box->priv->cell_view)
2289 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2291 if (combo_box->priv->column)
2292 gtk_tree_view_column_clear (combo_box->priv->column);
2294 for (i = combo_box->priv->cells; i; i = i->next)
2296 ComboCellInfo *info = (ComboCellInfo *)i->data;
2298 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2299 g_object_unref (G_OBJECT (info->cell));
2303 g_slist_free (combo_box->priv->cells);
2304 combo_box->priv->cells = NULL;
2306 menu = combo_box->priv->popup_widget;
2307 if (GTK_IS_MENU (menu))
2311 list = gtk_container_get_children (GTK_CONTAINER (menu));
2312 for (i = list; i; i = i->next)
2316 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2317 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2319 view = GTK_CELL_VIEW (i->data);
2321 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2328 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2329 GtkCellRenderer *cell,
2330 const gchar *attribute,
2333 ComboCellInfo *info;
2334 GtkComboBox *combo_box;
2337 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2338 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2340 combo_box = GTK_COMBO_BOX (layout);
2342 info = gtk_combo_box_get_cell_info (combo_box, cell);
2344 info->attributes = g_slist_prepend (info->attributes,
2345 GINT_TO_POINTER (column));
2346 info->attributes = g_slist_prepend (info->attributes,
2347 g_strdup (attribute));
2349 if (combo_box->priv->cell_view)
2350 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2351 cell, attribute, column);
2353 if (combo_box->priv->column)
2354 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2355 cell, attribute, column);
2357 menu = combo_box->priv->popup_widget;
2358 if (GTK_IS_MENU (menu))
2362 list = gtk_container_get_children (GTK_CONTAINER (menu));
2363 for (i = list; i; i = i->next)
2367 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2368 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2370 view = GTK_CELL_VIEW (i->data);
2372 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2378 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2382 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2383 GtkCellRenderer *cell,
2384 GtkCellLayoutDataFunc func,
2386 GDestroyNotify destroy)
2388 ComboCellInfo *info;
2389 GtkComboBox *combo_box;
2392 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2394 combo_box = GTK_COMBO_BOX (layout);
2396 info = gtk_combo_box_get_cell_info (combo_box, cell);
2397 g_return_if_fail (info != NULL);
2401 GDestroyNotify d = info->destroy;
2403 info->destroy = NULL;
2404 d (info->func_data);
2408 info->func_data = func_data;
2409 info->destroy = destroy;
2411 if (combo_box->priv->cell_view)
2412 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2414 if (combo_box->priv->column)
2415 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2417 menu = combo_box->priv->popup_widget;
2418 if (GTK_IS_MENU (menu))
2422 list = gtk_container_get_children (GTK_CONTAINER (menu));
2423 for (i = list; i; i = i->next)
2427 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2428 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2430 view = GTK_CELL_VIEW (i->data);
2432 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2433 func, func_data, NULL);
2438 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2442 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2443 GtkCellRenderer *cell)
2445 ComboCellInfo *info;
2446 GtkComboBox *combo_box;
2450 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2451 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2453 combo_box = GTK_COMBO_BOX (layout);
2455 info = gtk_combo_box_get_cell_info (combo_box, cell);
2456 g_return_if_fail (info != NULL);
2458 list = info->attributes;
2459 while (list && list->next)
2461 g_free (list->data);
2462 list = list->next->next;
2464 g_slist_free (info->attributes);
2465 info->attributes = NULL;
2467 if (combo_box->priv->cell_view)
2468 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2470 if (combo_box->priv->column)
2471 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2473 menu = combo_box->priv->popup_widget;
2474 if (GTK_IS_MENU (menu))
2478 list = gtk_container_get_children (GTK_CONTAINER (menu));
2479 for (i = list; i; i = i->next)
2483 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2484 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2486 view = GTK_CELL_VIEW (i->data);
2488 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2493 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2497 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2498 GtkCellRenderer *cell,
2501 ComboCellInfo *info;
2502 GtkComboBox *combo_box;
2506 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2507 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2509 combo_box = GTK_COMBO_BOX (layout);
2511 info = gtk_combo_box_get_cell_info (combo_box, cell);
2513 g_return_if_fail (info != NULL);
2514 g_return_if_fail (position >= 0);
2516 link = g_slist_find (combo_box->priv->cells, info);
2518 g_return_if_fail (link != NULL);
2520 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2521 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2524 if (combo_box->priv->cell_view)
2525 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2528 if (combo_box->priv->column)
2529 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2532 menu = combo_box->priv->popup_widget;
2533 if (GTK_IS_MENU (menu))
2537 list = gtk_container_get_children (GTK_CONTAINER (menu));
2538 for (i = list; i; i = i->next)
2542 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2543 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2545 view = GTK_CELL_VIEW (i->data);
2547 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2552 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2560 * gtk_combo_box_new:
2562 * Creates a new empty #GtkComboBox.
2564 * Return value: A new #GtkComboBox.
2569 gtk_combo_box_new (void)
2571 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2575 * gtk_combo_box_new_with_model:
2576 * @model: A #GtkTreeModel.
2578 * Creates a new #GtkComboBox with the model initialized to @model.
2580 * Return value: A new #GtkComboBox.
2585 gtk_combo_box_new_with_model (GtkTreeModel *model)
2587 GtkComboBox *combo_box;
2589 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2591 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2595 return GTK_WIDGET (combo_box);
2599 * gtk_combo_box_set_wrap_width:
2600 * @combo_box: A #GtkComboBox.
2601 * @width: Preferred number of columns.
2603 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2604 * the preferred number of columns when you want to the popup to be layed out
2610 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2613 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2614 g_return_if_fail (width >= 0);
2616 if (width != combo_box->priv->wrap_width)
2618 combo_box->priv->wrap_width = width;
2620 gtk_combo_box_relayout (combo_box);
2621 gtk_combo_box_style_set (GTK_WIDGET (combo_box), NULL);
2623 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2628 * gtk_combo_box_set_row_span_column:
2629 * @combo_box: A #GtkComboBox.
2630 * @row_span: A column in the model passed during construction.
2632 * Sets the column with row span information for @combo_box to be @row_span.
2633 * The row span column contains integers which indicate how many rows
2634 * an item should span.
2639 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2644 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2646 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2647 g_return_if_fail (row_span >= 0 && row_span < col);
2649 if (row_span != combo_box->priv->row_column)
2651 combo_box->priv->row_column = row_span;
2653 gtk_combo_box_relayout (combo_box);
2655 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2660 * gtk_combo_box_set_column_span_column:
2661 * @combo_box: A #GtkComboBox.
2662 * @column_span: A column in the model passed during construction.
2664 * Sets the column with column span information for @combo_box to be
2665 * @column_span. The column span column contains integers which indicate
2666 * how many columns an item should span.
2671 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2676 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2678 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2679 g_return_if_fail (column_span >= 0 && column_span < col);
2681 if (column_span != combo_box->priv->col_column)
2683 combo_box->priv->col_column = column_span;
2685 gtk_combo_box_relayout (combo_box);
2687 g_object_notify (G_OBJECT (combo_box), "column_span_column");
2692 * gtk_combo_box_get_active:
2693 * @combo_box: A #GtkComboBox.
2695 * Returns the index of the currently active item, or -1 if there's no
2698 * Return value: An integer which is the index of the currently active item, or
2699 * -1 if there's no active item.
2704 gtk_combo_box_get_active (GtkComboBox *combo_box)
2706 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
2708 return combo_box->priv->active_item;
2712 * gtk_combo_box_set_active:
2713 * @combo_box: A #GtkComboBox.
2714 * @index: An index in the model passed during construction, or -1 to have
2717 * Sets the active item of @combo_box to be the item at @index.
2722 gtk_combo_box_set_active (GtkComboBox *combo_box,
2727 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2728 /* -1 means "no item selected" */
2729 g_return_if_fail (index >= -1);
2731 if (combo_box->priv->active_item == index)
2734 combo_box->priv->active_item = index;
2738 if (combo_box->priv->tree_view)
2739 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
2742 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2744 if (GTK_IS_MENU (menu))
2745 gtk_menu_set_active (menu, -1);
2748 if (combo_box->priv->cell_view)
2749 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2753 path = gtk_tree_path_new_from_indices (index, -1);
2755 if (combo_box->priv->tree_view)
2756 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
2759 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2761 if (GTK_IS_MENU (menu))
2762 gtk_menu_set_active (GTK_MENU (menu), index);
2765 if (combo_box->priv->cell_view)
2766 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
2768 gtk_tree_path_free (path);
2771 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
2776 * gtk_combo_box_get_active_iter:
2777 * @combo_box: A #GtkComboBox
2778 * @iter: The uninitialized #GtkTreeIter.
2780 * Sets @iter to point to the current active item, if it exists.
2782 * Return value: %TRUE, if @iter was set
2787 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
2794 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
2796 active = gtk_combo_box_get_active (combo_box);
2800 path = gtk_tree_path_new_from_indices (active, -1);
2801 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
2803 gtk_tree_path_free (path);
2809 * gtk_combo_box_set_active_iter:
2810 * @combo_box: A #GtkComboBox
2811 * @iter: The #GtkTreeIter.
2813 * Sets the current active item to be the one referenced by @iter.
2814 * @iter must correspond to a path of depth one.
2819 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
2824 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2826 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
2827 g_return_if_fail (path != NULL);
2828 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
2830 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2831 gtk_tree_path_free (path);
2835 * gtk_combo_box_set_model:
2836 * @combo_box: A #GtkComboBox.
2837 * @model: A #GtkTreeModel.
2839 * Sets the model used by @combo_box to be @model. Will unset a
2840 * previously set model (if applicable).
2845 gtk_combo_box_set_model (GtkComboBox *combo_box,
2846 GtkTreeModel *model)
2848 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2849 g_return_if_fail (GTK_IS_TREE_MODEL (model));
2851 if (combo_box->priv->model)
2853 gtk_combo_box_unset_model (combo_box);
2854 g_object_unref (G_OBJECT (combo_box->priv->model));
2857 combo_box->priv->model = model;
2858 g_object_ref (G_OBJECT (combo_box->priv->model));
2860 gtk_combo_box_set_model_internal (combo_box);
2861 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
2862 gtk_combo_box_menu_fill (combo_box);
2864 if (combo_box->priv->cell_view)
2865 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
2866 combo_box->priv->model);
2870 * gtk_combo_box_get_model
2871 * @combo_box: A #GtkComboBox.
2873 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
2875 * Return value: A #GtkTreeModel which was passed during construction.
2880 gtk_combo_box_get_model (GtkComboBox *combo_box)
2882 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
2884 return combo_box->priv->model;
2888 /* convenience API for simple text combos */
2891 * gtk_combo_box_new_text:
2893 * Convenience function which constructs a new text combo box, which is a
2894 * #GtkComboBox just displaying strings. If you use this function to create
2895 * a text combo box, you should only manipulate its data source with the
2896 * following convenience functions: gtk_combo_box_append_text(),
2897 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
2898 * gtk_combo_box_remove_text().
2900 * Return value: A new text combo box.
2905 gtk_combo_box_new_text (void)
2907 GtkWidget *combo_box;
2908 GtkCellRenderer *cell;
2909 GtkListStore *store;
2911 store = gtk_list_store_new (1, G_TYPE_STRING);
2913 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
2915 cell = gtk_cell_renderer_text_new ();
2916 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
2917 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
2925 * gtk_combo_box_append_text:
2926 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2929 * Appends @string to the list of strings stored in @combo_box. Note that
2930 * you can only use this function with combo boxes constructed with
2931 * gtk_combo_box_new_text().
2936 gtk_combo_box_append_text (GtkComboBox *combo_box,
2940 GtkListStore *store;
2942 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2943 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2944 g_return_if_fail (text != NULL);
2946 store = GTK_LIST_STORE (combo_box->priv->model);
2948 gtk_list_store_append (store, &iter);
2949 gtk_list_store_set (store, &iter, 0, text, -1);
2953 * gtk_combo_box_insert_text:
2954 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
2955 * @position: An index to insert @text.
2958 * Inserts @string at @position in the list of strings stored in @combo_box.
2959 * Note that you can only use this function with combo boxes constructed
2960 * with gtk_combo_box_new_text().
2965 gtk_combo_box_insert_text (GtkComboBox *combo_box,
2970 GtkListStore *store;
2972 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2973 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
2974 g_return_if_fail (position >= 0);
2975 g_return_if_fail (text != NULL);
2977 store = GTK_LIST_STORE (combo_box->priv->model);
2979 gtk_list_store_insert (store, &iter, position);
2980 gtk_list_store_set (store, &iter, 0, text, -1);
2984 * gtk_combo_box_prepend_text:
2985 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
2988 * Prepends @string to the list of strings stored in @combo_box. Note that
2989 * you can only use this function with combo boxes constructed with
2990 * gtk_combo_box_new_text().
2995 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
2999 GtkListStore *store;
3001 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3002 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3003 g_return_if_fail (text != NULL);
3005 store = GTK_LIST_STORE (combo_box->priv->model);
3007 gtk_list_store_prepend (store, &iter);
3008 gtk_list_store_set (store, &iter, 0, text, -1);
3012 * gtk_combo_box_remove_text:
3013 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3014 * @position: Index of the item to remove.
3016 * Removes the string at @position from @combo_box. Note that you can only use
3017 * this function with combo boxes constructed with gtk_combo_box_new_text().
3022 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3026 GtkListStore *store;
3028 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3029 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3030 g_return_if_fail (position >= 0);
3032 store = GTK_LIST_STORE (combo_box->priv->model);
3034 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3036 gtk_list_store_remove (store, &iter);
3041 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3042 gboolean group_cycling)
3044 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3046 gtk_widget_grab_focus (combo_box->priv->button);
3052 gtk_combo_box_destroy (GtkObject *object)
3054 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3056 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3058 combo_box->priv->cell_view = NULL;
3062 gtk_combo_box_finalize (GObject *object)
3064 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3067 gtk_combo_box_unset_model (combo_box);
3069 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3070 gtk_combo_box_menu_destroy (combo_box);
3072 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3073 gtk_combo_box_list_destroy (combo_box);
3075 if (combo_box->priv->popup_window)
3076 gtk_widget_destroy (combo_box->priv->popup_window);
3078 if (combo_box->priv->model)
3079 g_object_unref (combo_box->priv->model);
3081 for (i = combo_box->priv->cells; i; i = i->next)
3083 ComboCellInfo *info = (ComboCellInfo *)i->data;
3084 GSList *list = info->attributes;
3087 info->destroy (info->func_data);
3089 while (list && list->next)
3091 g_free (list->data);
3092 list = list->next->next;
3094 g_slist_free (info->attributes);
3096 g_object_unref (G_OBJECT (info->cell));
3099 g_slist_free (combo_box->priv->cells);
3101 G_OBJECT_CLASS (parent_class)->finalize (object);