2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcombobox.h"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellview.h"
28 #include "gtkcellviewmenuitem.h"
29 #include "gtkeventbox.h"
31 #include "gtkliststore.h"
34 #include "gtktogglebutton.h"
35 #include "gtktreeselection.h"
36 #include "gtkvseparator.h"
37 #include "gtkwindow.h"
39 #include <gdk/gdkkeysyms.h>
41 #include <gobject/gvaluecollector.h>
46 #include "gtkmarshalers.h"
50 /* WELCOME, to THE house of evil code */
52 typedef struct _ComboCellInfo ComboCellInfo;
55 GtkCellRenderer *cell;
58 GtkCellLayoutDataFunc func;
60 GDestroyNotify destroy;
66 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
68 struct _GtkComboBoxPrivate
80 GtkTreeViewColumn *column;
83 GtkWidget *cell_view_frame;
89 GtkWidget *popup_widget;
90 GtkWidget *popup_window;
91 GtkWidget *popup_frame;
101 guint popup_in_progress : 1;
102 guint destroying : 1;
105 /* While debugging this evil code, I have learned that
106 * there are actually 4 modes to this widget, which can
107 * be characterized as follows
109 * 1) menu mode, no child added
112 * cell_view -> GtkCellView, regular child
113 * cell_view_frame -> NULL
114 * button -> GtkToggleButton set_parent to combo
115 * arrow -> GtkArrow set_parent to button
116 * separator -> GtkVSepator set_parent to button
117 * popup_widget -> GtkMenu
118 * popup_window -> NULL
119 * popup_frame -> NULL
121 * 2) menu mode, child added
125 * cell_view_frame -> NULL
126 * button -> GtkToggleButton set_parent to combo
127 * arrow -> GtkArrow, child of button
129 * popup_widget -> GtkMenu
130 * popup_window -> NULL
131 * popup_frame -> NULL
133 * 3) list mode, no child added
135 * tree_view -> GtkTreeView, child of popup_frame
136 * cell_view -> GtkCellView, regular child
137 * cell_view_frame -> GtkFrame, set parent to combo
138 * button -> GtkToggleButton, set_parent to combo
139 * arrow -> GtkArrow, child of button
141 * popup_widget -> tree_view
142 * popup_window -> GtkWindow
143 * popup_frame -> GtkFrame, child of popup_window
145 * 4) list mode, child added
147 * tree_view -> GtkTreeView, child of popup_frame
149 * cell_view_frame -> NULL
150 * button -> GtkToggleButton, set_parent to combo
151 * arrow -> GtkArrow, child of button
153 * popup_widget -> tree_view
154 * popup_window -> GtkWindow
155 * popup_frame -> GtkFrame, child of popup_window
168 PROP_ROW_SPAN_COLUMN,
169 PROP_COLUMN_SPAN_COLUMN,
173 static GtkBinClass *parent_class = NULL;
174 static guint combo_box_signals[LAST_SIGNAL] = {0,};
176 #define BONUS_PADDING 4
180 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
181 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
182 static void gtk_combo_box_init (GtkComboBox *combo_box);
183 static void gtk_combo_box_finalize (GObject *object);
184 static void gtk_combo_box_destroy (GtkObject *object);
186 static void gtk_combo_box_set_property (GObject *object,
190 static void gtk_combo_box_get_property (GObject *object,
195 static void gtk_combo_box_state_changed (GtkWidget *widget,
196 GtkStateType previous);
197 static void gtk_combo_box_style_set (GtkWidget *widget,
199 static void gtk_combo_box_button_toggled (GtkWidget *widget,
201 static void gtk_combo_box_add (GtkContainer *container,
203 static void gtk_combo_box_remove (GtkContainer *container,
206 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
207 GtkCellRenderer *cell);
209 static void gtk_combo_box_menu_show (GtkWidget *menu,
211 static void gtk_combo_box_menu_hide (GtkWidget *menu,
214 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
216 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
221 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
226 static void gtk_combo_box_menu_position (GtkMenu *menu,
232 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
234 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
236 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
238 static void gtk_combo_box_size_request (GtkWidget *widget,
239 GtkRequisition *requisition);
240 static void gtk_combo_box_size_allocate (GtkWidget *widget,
241 GtkAllocation *allocation);
242 static void gtk_combo_box_forall (GtkContainer *container,
243 gboolean include_internals,
244 GtkCallback callback,
245 gpointer callback_data);
246 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
247 GdkEventExpose *event);
248 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
249 GdkEventScroll *event);
250 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
252 static gboolean gtk_combo_box_key_press (GtkWidget *widget,
256 /* listening to the model */
257 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
261 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
264 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
269 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
275 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
280 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
281 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
283 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
285 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
286 GdkEventButton *event,
288 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
291 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
292 GdkEventButton *event,
295 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
301 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
302 gboolean add_children);
303 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
304 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
306 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
310 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
312 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
314 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
315 GdkEventButton *event,
317 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
319 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
323 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
326 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
331 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
335 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
340 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
341 GtkCellRenderer *cell,
343 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
344 GtkCellRenderer *cell,
346 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
347 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
348 GtkCellRenderer *cell,
349 const gchar *attribute,
351 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
352 GtkCellRenderer *cell,
353 GtkCellLayoutDataFunc func,
355 GDestroyNotify destroy);
356 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
357 GtkCellRenderer *cell);
358 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
359 GtkCellRenderer *cell,
361 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
362 gboolean group_cycling);
366 gtk_combo_box_get_type (void)
368 static GType combo_box_type = 0;
372 static const GTypeInfo combo_box_info =
374 sizeof (GtkComboBoxClass),
375 NULL, /* base_init */
376 NULL, /* base_finalize */
377 (GClassInitFunc) gtk_combo_box_class_init,
378 NULL, /* class_finalize */
379 NULL, /* class_data */
380 sizeof (GtkComboBox),
382 (GInstanceInitFunc) gtk_combo_box_init
385 static const GInterfaceInfo cell_layout_info =
387 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
392 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
397 g_type_add_interface_static (combo_box_type,
398 GTK_TYPE_CELL_LAYOUT,
402 return combo_box_type;
407 gtk_combo_box_class_init (GtkComboBoxClass *klass)
409 GObjectClass *object_class;
410 GtkBindingSet *binding_set;
411 GtkObjectClass *gtk_object_class;
412 GtkContainerClass *container_class;
413 GtkWidgetClass *widget_class;
415 binding_set = gtk_binding_set_by_class (klass);
417 container_class = (GtkContainerClass *)klass;
418 container_class->forall = gtk_combo_box_forall;
419 container_class->add = gtk_combo_box_add;
420 container_class->remove = gtk_combo_box_remove;
422 widget_class = (GtkWidgetClass *)klass;
423 widget_class->size_allocate = gtk_combo_box_size_allocate;
424 widget_class->size_request = gtk_combo_box_size_request;
425 widget_class->expose_event = gtk_combo_box_expose_event;
426 widget_class->scroll_event = gtk_combo_box_scroll_event;
427 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
428 widget_class->style_set = gtk_combo_box_style_set;
429 widget_class->state_changed = gtk_combo_box_state_changed;
431 gtk_object_class = (GtkObjectClass *)klass;
432 gtk_object_class->destroy = gtk_combo_box_destroy;
434 object_class = (GObjectClass *)klass;
435 object_class->finalize = gtk_combo_box_finalize;
436 object_class->set_property = gtk_combo_box_set_property;
437 object_class->get_property = gtk_combo_box_get_property;
439 parent_class = g_type_class_peek_parent (klass);
442 combo_box_signals[CHANGED] =
443 g_signal_new ("changed",
444 G_OBJECT_CLASS_TYPE (klass),
446 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
448 g_cclosure_marshal_VOID__VOID,
452 g_object_class_install_property (object_class,
454 g_param_spec_object ("model",
455 P_("ComboBox model"),
456 P_("The model for the combo box"),
460 g_object_class_install_property (object_class,
462 g_param_spec_int ("wrap_width",
464 P_("Wrap width for layouting the items in a grid"),
470 g_object_class_install_property (object_class,
471 PROP_ROW_SPAN_COLUMN,
472 g_param_spec_int ("row_span_column",
473 P_("Row span column"),
474 P_("TreeModel column containing the row span values"),
480 g_object_class_install_property (object_class,
481 PROP_COLUMN_SPAN_COLUMN,
482 g_param_spec_int ("column_span_column",
483 P_("Column span column"),
484 P_("TreeModel column containing the column span values"),
490 g_object_class_install_property (object_class,
492 g_param_spec_int ("active",
494 P_("The item which is currently active"),
500 gtk_widget_class_install_style_property (widget_class,
501 g_param_spec_boolean ("appears-as-list",
502 P_("Appears as list"),
503 P_("Whether combobox dropdowns should look like lists rather than menus"),
507 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
511 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
513 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
514 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
515 iface->clear = gtk_combo_box_cell_layout_clear;
516 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
517 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
518 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
519 iface->reorder = gtk_combo_box_cell_layout_reorder;
523 gtk_combo_box_init (GtkComboBox *combo_box)
525 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
527 combo_box->priv->cell_view = gtk_cell_view_new ();
528 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
529 GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
530 gtk_widget_show (combo_box->priv->cell_view);
532 combo_box->priv->width = 0;
533 combo_box->priv->wrap_width = 0;
535 combo_box->priv->active_item = -1;
536 combo_box->priv->col_column = -1;
537 combo_box->priv->row_column = -1;
541 gtk_combo_box_set_property (GObject *object,
546 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
551 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
554 case PROP_WRAP_WIDTH:
555 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
558 case PROP_ROW_SPAN_COLUMN:
559 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
562 case PROP_COLUMN_SPAN_COLUMN:
563 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
567 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
576 gtk_combo_box_get_property (GObject *object,
581 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
586 g_value_set_object (value, combo_box->priv->model);
589 case PROP_WRAP_WIDTH:
590 g_value_set_int (value, combo_box->priv->wrap_width);
593 case PROP_ROW_SPAN_COLUMN:
594 g_value_set_int (value, combo_box->priv->row_column);
597 case PROP_COLUMN_SPAN_COLUMN:
598 g_value_set_int (value, combo_box->priv->col_column);
602 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 gtk_combo_box_state_changed (GtkWidget *widget,
613 GtkStateType previous)
615 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
617 if (GTK_WIDGET_REALIZED (widget))
619 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
620 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
621 &widget->style->base[GTK_WIDGET_STATE (widget)]);
624 gtk_widget_queue_draw (widget);
628 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
630 gboolean appears_as_list;
632 /* if wrap_width > 0, then we are in grid-mode and forced to use
635 if (combo_box->priv->wrap_width)
636 appears_as_list = FALSE;
638 gtk_widget_style_get (GTK_WIDGET (combo_box),
639 "appears-as-list", &appears_as_list,
644 /* Destroy all the menu mode widgets, if they exist. */
645 if (GTK_IS_MENU (combo_box->priv->popup_widget))
646 gtk_combo_box_menu_destroy (combo_box);
648 /* Create the list mode widgets, if they don't already exist. */
649 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
650 gtk_combo_box_list_setup (combo_box);
654 /* Destroy all the list mode widgets, if they exist. */
655 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
656 gtk_combo_box_list_destroy (combo_box);
658 /* Create the menu mode widgets, if they don't already exist. */
659 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
660 gtk_combo_box_menu_setup (combo_box, TRUE);
665 gtk_combo_box_style_set (GtkWidget *widget,
668 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
670 gtk_combo_box_check_appearance (combo_box);
672 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
673 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
674 &widget->style->base[GTK_WIDGET_STATE (widget)]);
678 gtk_combo_box_button_toggled (GtkWidget *widget,
681 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
683 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
685 if (!combo_box->priv->popup_in_progress)
686 gtk_combo_box_popup (combo_box);
689 gtk_combo_box_popdown (combo_box);
693 gtk_combo_box_add (GtkContainer *container,
696 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
698 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
700 gtk_widget_unparent (combo_box->priv->cell_view);
701 GTK_BIN (container)->child = NULL;
702 gtk_widget_queue_resize (GTK_WIDGET (container));
705 gtk_widget_set_parent (widget, GTK_WIDGET (container));
706 GTK_BIN (container)->child = widget;
708 if (combo_box->priv->cell_view &&
709 widget != combo_box->priv->cell_view)
711 /* since the cell_view was unparented, it's gone now */
712 combo_box->priv->cell_view = NULL;
714 if (!combo_box->priv->tree_view && combo_box->priv->separator)
716 gtk_widget_unparent (combo_box->priv->separator);
717 combo_box->priv->separator = NULL;
719 g_object_ref (G_OBJECT (combo_box->priv->arrow));
720 gtk_widget_unparent (combo_box->priv->arrow);
721 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
722 combo_box->priv->arrow);
723 g_object_unref (G_OBJECT (combo_box->priv->arrow));
725 gtk_widget_queue_resize (GTK_WIDGET (container));
727 else if (combo_box->priv->cell_view_frame)
729 gtk_widget_unparent (combo_box->priv->cell_view_frame);
730 combo_box->priv->cell_view_frame = NULL;
736 gtk_combo_box_remove (GtkContainer *container,
739 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
740 gboolean appears_as_list;
742 gtk_widget_unparent (widget);
743 GTK_BIN (container)->child = NULL;
745 if (combo_box->priv->destroying)
748 gtk_widget_queue_resize (GTK_WIDGET (container));
750 if (!combo_box->priv->tree_view)
751 appears_as_list = FALSE;
753 appears_as_list = TRUE;
756 gtk_combo_box_list_destroy (combo_box);
757 else if (GTK_IS_MENU (combo_box->priv->popup_widget))
759 gtk_combo_box_menu_destroy (combo_box);
760 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
761 combo_box->priv->popup_widget = NULL;
764 if (!combo_box->priv->cell_view)
766 combo_box->priv->cell_view = gtk_cell_view_new ();
767 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
768 GTK_BIN (container)->child = combo_box->priv->cell_view;
770 gtk_widget_show (combo_box->priv->cell_view);
771 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
772 combo_box->priv->model);
773 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
778 gtk_combo_box_list_setup (combo_box);
780 gtk_combo_box_menu_setup (combo_box, TRUE);
782 gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
785 static ComboCellInfo *
786 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
787 GtkCellRenderer *cell)
791 for (i = combo_box->priv->cells; i; i = i->next)
793 ComboCellInfo *info = (ComboCellInfo *)i->data;
795 if (info && info->cell == cell)
803 gtk_combo_box_menu_show (GtkWidget *menu,
806 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
808 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
810 combo_box->priv->popup_in_progress = FALSE;
814 gtk_combo_box_menu_hide (GtkWidget *menu,
817 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
819 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
824 gtk_combo_box_detacher (GtkWidget *widget,
827 GtkComboBox *combo_box;
829 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
831 combo_box = GTK_COMBO_BOX (widget);
832 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
834 g_signal_handlers_disconnect_by_func (menu,
835 gtk_combo_box_menu_show,
837 g_signal_handlers_disconnect_by_func (menu,
838 gtk_combo_box_menu_hide,
841 combo_box->priv->popup_widget = NULL;
845 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
848 if (GTK_IS_MENU (combo_box->priv->popup_widget))
850 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
851 combo_box->priv->popup_widget = NULL;
853 else if (combo_box->priv->popup_widget)
855 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
856 combo_box->priv->popup_widget);
857 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
858 combo_box->priv->popup_widget = NULL;
861 if (GTK_IS_MENU (popup))
863 if (combo_box->priv->popup_window)
865 gtk_widget_destroy (combo_box->priv->popup_window);
866 combo_box->priv->popup_window = NULL;
867 combo_box->priv->popup_frame = NULL;
870 combo_box->priv->popup_widget = popup;
872 g_signal_connect (popup, "show",
873 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
874 g_signal_connect (popup, "hide",
875 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
877 gtk_menu_attach_to_widget (GTK_MENU (popup),
878 GTK_WIDGET (combo_box),
879 gtk_combo_box_detacher);
883 if (!combo_box->priv->popup_window)
885 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
886 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
887 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
888 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
890 combo_box->priv->popup_frame = gtk_frame_new (NULL);
891 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
892 GTK_SHADOW_ETCHED_IN);
893 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
894 combo_box->priv->popup_frame);
896 gtk_widget_show (combo_box->priv->popup_frame);
899 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
901 gtk_widget_show (popup);
902 g_object_ref (G_OBJECT (popup));
903 combo_box->priv->popup_widget = popup;
908 gtk_combo_box_menu_position_below (GtkMenu *menu,
917 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
919 /* FIXME: is using the size request here broken? */
920 child = GTK_BIN (combo_box)->child;
922 gdk_window_get_origin (child->window, &sx, &sy);
924 gtk_widget_size_request (GTK_WIDGET (menu), &req);
926 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
929 *x = sx + child->allocation.width - req.width;
930 *y = sy + child->allocation.height;
932 if (GTK_WIDGET_NO_WINDOW (child))
934 *x += child->allocation.x;
935 *y += child->allocation.y;
942 gtk_combo_box_menu_position_over (GtkMenu *menu,
948 GtkComboBox *combo_box;
952 GtkRequisition requisition;
959 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
961 combo_box = GTK_COMBO_BOX (user_data);
962 widget = GTK_WIDGET (combo_box);
964 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
965 menu_width = requisition.width;
967 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
968 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
970 menu_xpos += widget->allocation.x;
971 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
975 gtk_widget_get_child_requisition (active, &requisition);
976 menu_ypos -= requisition.height / 2;
979 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
982 child = children->data;
987 if (GTK_WIDGET_VISIBLE (child))
989 gtk_widget_get_child_requisition (child, &requisition);
990 menu_ypos -= requisition.height;
993 children = children->next;
996 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
997 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
999 /* Clamp the position on screen */
1000 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1004 else if ((menu_xpos + menu_width) > screen_width)
1005 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1014 gtk_combo_box_menu_position (GtkMenu *menu,
1020 GtkComboBox *combo_box;
1021 GtkWidget *menu_item;
1023 combo_box = GTK_COMBO_BOX (user_data);
1025 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
1026 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1029 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1031 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
1034 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1040 gtk_combo_box_list_position (GtkComboBox *combo_box,
1049 GdkRectangle monitor;
1050 GtkRequisition popup_req;
1052 sample = GTK_BIN (combo_box)->child;
1054 *width = sample->allocation.width;
1055 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
1056 *height = popup_req.height;
1058 gdk_window_get_origin (sample->window, x, y);
1060 if (combo_box->priv->cell_view_frame)
1062 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1063 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1064 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1065 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1068 if (GTK_WIDGET_NO_WINDOW (sample))
1070 *x += sample->allocation.x;
1071 *y += sample->allocation.y;
1074 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1075 monitor_num = gdk_screen_get_monitor_at_window (screen,
1076 GTK_WIDGET (combo_box)->window);
1077 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1081 else if (*x + *width > monitor.x + monitor.width)
1082 *x = monitor.x + monitor.width - *width;
1084 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1085 *y += sample->allocation.height;
1091 * gtk_combo_box_popup:
1092 * @combo_box: a #GtkComboBox
1094 * Pops up the menu or dropdown list of @combo_box.
1096 * This function is mostly intended for use by accessibility technologies;
1097 * applications should have little use for it.
1102 gtk_combo_box_popup (GtkComboBox *combo_box)
1104 gint x, y, width, height;
1106 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1108 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1111 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1113 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1114 combo_box->priv->active_item);
1116 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1118 gtk_combo_box_menu_position, combo_box,
1123 gtk_widget_show_all (combo_box->priv->popup_frame);
1124 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1126 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1127 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1130 gtk_widget_show (combo_box->priv->popup_window);
1132 gtk_widget_grab_focus (combo_box->priv->popup_window);
1133 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1136 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1138 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1139 FALSE, GDK_CURRENT_TIME);
1140 gtk_widget_grab_focus (combo_box->priv->tree_view);
1143 gtk_grab_add (combo_box->priv->popup_window);
1144 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1145 GDK_BUTTON_PRESS_MASK |
1146 GDK_BUTTON_RELEASE_MASK |
1147 GDK_POINTER_MOTION_MASK,
1148 NULL, NULL, GDK_CURRENT_TIME);
1150 gtk_grab_add (combo_box->priv->tree_view);
1154 * gtk_combo_box_popdown:
1155 * @combo_box: a #GtkComboBox
1157 * Hides the menu or dropdown list of @combo_box.
1159 * This function is mostly intended for use by accessibility technologies;
1160 * applications should have little use for it.
1165 gtk_combo_box_popdown (GtkComboBox *combo_box)
1167 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1169 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1171 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1175 gtk_combo_box_list_remove_grabs (combo_box);
1176 gtk_widget_hide_all (combo_box->priv->popup_window);
1177 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1182 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1188 if (combo_box->priv->cell_view)
1189 gtk_widget_style_get (combo_box->priv->cell_view,
1190 "focus-line-width", &padding,
1195 /* add some pixels for good measure */
1196 padding += BONUS_PADDING;
1198 if (combo_box->priv->cell_view)
1199 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1204 return req.width + padding;
1208 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1214 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1217 combo_box->priv->width = 0;
1219 path = gtk_tree_path_new_from_indices (0, -1);
1221 if (combo_box->priv->cell_view)
1222 gtk_widget_style_get (combo_box->priv->cell_view,
1223 "focus-line-width", &padding,
1228 /* add some pixels for good measure */
1229 padding += BONUS_PADDING;
1235 if (combo_box->priv->cell_view)
1236 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1241 combo_box->priv->width = MAX (combo_box->priv->width,
1242 req.width + padding);
1244 gtk_tree_path_next (path);
1246 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1248 gtk_tree_path_free (path);
1252 gtk_combo_box_size_request (GtkWidget *widget,
1253 GtkRequisition *requisition)
1256 GtkRequisition bin_req;
1258 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1261 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1262 gtk_combo_box_remeasure (combo_box);
1263 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1265 if (!combo_box->priv->tree_view)
1269 if (combo_box->priv->cell_view)
1271 GtkRequisition button_req, sep_req, arrow_req;
1272 gint border_width, xthickness, ythickness;
1274 gtk_widget_size_request (combo_box->priv->button, &button_req);
1275 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1276 xthickness = combo_box->priv->button->style->xthickness;
1277 ythickness = combo_box->priv->button->style->ythickness;
1279 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1281 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1282 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1284 height = MAX (sep_req.height, arrow_req.height);
1285 height = MAX (height, bin_req.height);
1287 width = bin_req.width + sep_req.width + arrow_req.width;
1289 height += border_width + 1 + ythickness * 2 + 4;
1290 width += border_width + 1 + xthickness * 2 + 4;
1292 requisition->width = width;
1293 requisition->height = height;
1297 GtkRequisition but_req;
1299 gtk_widget_size_request (combo_box->priv->button, &but_req);
1301 requisition->width = bin_req.width + but_req.width;
1302 requisition->height = MAX (bin_req.height, but_req.height);
1308 GtkRequisition button_req, frame_req;
1310 /* sample + frame */
1311 *requisition = bin_req;
1313 if (combo_box->priv->cell_view_frame)
1315 gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
1316 requisition->width += 2 *
1317 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1318 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1319 requisition->height += 2 *
1320 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1321 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1325 gtk_widget_size_request (combo_box->priv->button, &button_req);
1327 requisition->height = MAX (requisition->height, button_req.height);
1328 requisition->width += button_req.width;
1333 gtk_combo_box_size_allocate (GtkWidget *widget,
1334 GtkAllocation *allocation)
1336 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1337 GtkAllocation child;
1339 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1341 widget->allocation = *allocation;
1343 if (!combo_box->priv->tree_view)
1345 if (combo_box->priv->cell_view)
1347 gint border_width, xthickness, ythickness;
1351 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1353 /* set some things ready */
1354 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1355 xthickness = combo_box->priv->button->style->xthickness;
1356 ythickness = combo_box->priv->button->style->ythickness;
1358 child.x = allocation->x + border_width + 1 + xthickness + 2;
1359 child.y = allocation->y + border_width + 1 + ythickness + 2;
1361 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1363 /* handle the children */
1364 gtk_widget_size_request (combo_box->priv->arrow, &req);
1365 child.width = req.width;
1366 child.height = allocation->height - 2 * (child.y - allocation->y);
1368 child.x += width - req.width;
1369 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1371 child.x += req.width;
1372 gtk_widget_size_request (combo_box->priv->separator, &req);
1373 child.width = req.width;
1375 child.x -= req.width;
1376 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1380 child.x += req.width;
1381 child.width = allocation->x + allocation->width
1382 - (border_width + 1 + xthickness + 2) - child.x;
1386 child.width = child.x;
1387 child.x = allocation->x + border_width + 1 + xthickness + 2;
1388 child.width -= child.x;
1391 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1395 gtk_widget_size_request (combo_box->priv->button, &req);
1397 child.x = allocation->x;
1399 child.x = allocation->x + allocation->width - req.width;
1400 child.y = allocation->y;
1401 child.width = req.width;
1402 child.height = allocation->height;
1403 gtk_widget_size_allocate (combo_box->priv->button, &child);
1406 child.x = allocation->x + req.width;
1408 child.x = allocation->x;
1409 child.y = allocation->y;
1410 child.width = allocation->width - req.width;
1411 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1419 gtk_widget_size_request (combo_box->priv->button, &req);
1421 child.x = allocation->x;
1423 child.x = allocation->x + allocation->width - req.width;
1424 child.y = allocation->y;
1425 child.width = req.width;
1426 child.height = allocation->height;
1427 gtk_widget_size_allocate (combo_box->priv->button, &child);
1431 child.x = allocation->x + req.width;
1433 child.x = allocation->x;
1434 child.y = allocation->y;
1435 child.width = allocation->width - req.width;
1436 child.height = allocation->height;
1438 if (combo_box->priv->cell_view_frame)
1440 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1444 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1445 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1447 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1448 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1449 child.width -= 2 * (
1450 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1451 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1452 child.height -= 2 * (
1453 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1454 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1457 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1462 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1464 if (combo_box->priv->model)
1466 g_signal_handler_disconnect (combo_box->priv->model,
1467 combo_box->priv->inserted_id);
1468 g_signal_handler_disconnect (combo_box->priv->model,
1469 combo_box->priv->deleted_id);
1470 g_signal_handler_disconnect (combo_box->priv->model,
1471 combo_box->priv->reordered_id);
1472 g_signal_handler_disconnect (combo_box->priv->model,
1473 combo_box->priv->changed_id);
1477 if (!combo_box->priv->tree_view)
1479 if (combo_box->priv->popup_widget)
1480 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1481 (GtkCallback)gtk_widget_destroy, NULL);
1484 g_object_unref (G_OBJECT (combo_box->priv->model));
1485 combo_box->priv->model = NULL;
1489 gtk_combo_box_forall (GtkContainer *container,
1490 gboolean include_internals,
1491 GtkCallback callback,
1492 gpointer callback_data)
1494 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1496 if (include_internals)
1498 if (combo_box->priv->button)
1499 (* callback) (combo_box->priv->button, callback_data);
1500 if (combo_box->priv->separator)
1501 (* callback) (combo_box->priv->separator, callback_data);
1502 if (combo_box->priv->arrow)
1503 (* callback) (combo_box->priv->arrow, callback_data);
1504 if (combo_box->priv->cell_view_frame)
1505 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1508 if (GTK_BIN (container)->child)
1509 (* callback) (GTK_BIN (container)->child, callback_data);
1513 gtk_combo_box_expose_event (GtkWidget *widget,
1514 GdkEventExpose *event)
1516 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1518 if (!combo_box->priv->tree_view)
1520 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1521 combo_box->priv->button, event);
1523 if (combo_box->priv->separator)
1525 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1526 combo_box->priv->separator, event);
1528 /* if not in this case, arrow gets its expose event from button */
1529 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1530 combo_box->priv->arrow, event);
1535 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1536 combo_box->priv->button, event);
1538 if (combo_box->priv->cell_view_frame)
1539 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1540 combo_box->priv->cell_view_frame, event);
1543 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1544 GTK_BIN (widget)->child, event);
1550 gtk_combo_box_scroll_event (GtkWidget *widget,
1551 GdkEventScroll *event)
1553 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1557 index = gtk_combo_box_get_active (combo_box);
1561 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1563 if (event->direction == GDK_SCROLL_UP)
1568 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1579 cell_view_sync_cells (GtkComboBox *combo_box,
1580 GtkCellView *cell_view)
1584 for (k = combo_box->priv->cells; k; k = k->next)
1587 ComboCellInfo *info = (ComboCellInfo *)k->data;
1589 if (info->pack == GTK_PACK_START)
1590 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1591 info->cell, info->expand);
1592 else if (info->pack == GTK_PACK_END)
1593 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1594 info->cell, info->expand);
1596 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1598 info->func, info->func_data, NULL);
1600 for (j = info->attributes; j; j = j->next->next)
1602 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1605 GPOINTER_TO_INT (j->next->data));
1611 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1612 gboolean add_children)
1616 if (combo_box->priv->cell_view)
1618 combo_box->priv->button = gtk_toggle_button_new ();
1619 g_signal_connect (combo_box->priv->button, "toggled",
1620 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1621 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1622 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1623 gtk_widget_set_parent (combo_box->priv->button,
1624 GTK_BIN (combo_box)->child->parent);
1626 combo_box->priv->separator = gtk_vseparator_new ();
1627 gtk_widget_set_parent (combo_box->priv->separator,
1628 combo_box->priv->button);
1629 gtk_widget_show (combo_box->priv->separator);
1631 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1632 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1633 gtk_widget_show (combo_box->priv->arrow);
1635 gtk_widget_show_all (combo_box->priv->button);
1637 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1639 /* I have no clue why, but we need to manually map in this case. */
1640 gtk_widget_map (combo_box->priv->separator);
1641 gtk_widget_map (combo_box->priv->arrow);
1646 combo_box->priv->button = gtk_toggle_button_new ();
1647 g_signal_connect (combo_box->priv->button, "toggled",
1648 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1649 g_signal_connect_after (combo_box, "key_press_event",
1650 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1651 gtk_widget_set_parent (combo_box->priv->button,
1652 GTK_BIN (combo_box)->child->parent);
1654 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1655 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1656 combo_box->priv->arrow);
1657 gtk_widget_show_all (combo_box->priv->button);
1660 g_signal_connect (combo_box->priv->button, "button_press_event",
1661 G_CALLBACK (gtk_combo_box_menu_button_press),
1664 /* create our funky menu */
1665 box = gtk_menu_new ();
1666 g_signal_connect (box, "key_press_event",
1667 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1668 gtk_combo_box_set_popup_widget (combo_box, box);
1672 gtk_combo_box_menu_fill (combo_box);
1677 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1683 if (!combo_box->priv->model)
1686 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1687 menu = combo_box->priv->popup_widget;
1689 for (i = 0; i < items; i++)
1693 path = gtk_tree_path_new_from_indices (i, -1);
1694 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1696 g_signal_connect (tmp, "activate",
1697 G_CALLBACK (gtk_combo_box_menu_item_activate),
1700 cell_view_sync_cells (combo_box,
1701 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1703 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1705 if (combo_box->priv->wrap_width)
1706 gtk_combo_box_relayout_item (combo_box, i);
1708 gtk_widget_show (tmp);
1710 gtk_tree_path_free (path);
1715 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1717 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1718 G_SIGNAL_MATCH_DATA,
1720 gtk_combo_box_menu_button_press, NULL);
1722 /* unparent will remove our latest ref */
1723 if (combo_box->priv->cell_view)
1725 gtk_widget_unparent (combo_box->priv->arrow);
1726 combo_box->priv->arrow = NULL;
1728 gtk_widget_unparent (combo_box->priv->separator);
1729 combo_box->priv->separator = NULL;
1731 gtk_widget_unparent (combo_box->priv->button);
1732 combo_box->priv->button = NULL;
1736 /* will destroy the arrow too */
1737 gtk_widget_unparent (combo_box->priv->button);
1739 combo_box->priv->button = NULL;
1740 combo_box->priv->arrow = NULL;
1743 /* changing the popup window will unref the menu and the children */
1751 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1758 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1762 if (combo_box->priv->col_column == -1)
1765 gtk_tree_model_get (combo_box->priv->model, &iter,
1766 combo_box->priv->col_column, cols,
1772 if (combo_box->priv->row_column == -1)
1775 gtk_tree_model_get (combo_box->priv->model, &iter,
1776 combo_box->priv->row_column, rows,
1782 menu_occupied (GtkMenu *menu,
1786 guint bottom_attach)
1790 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1791 g_return_val_if_fail (left_attach < right_attach, TRUE);
1792 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1794 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1797 gboolean h_intersect = FALSE;
1798 gboolean v_intersect = FALSE;
1800 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1803 "bottom_attach", &b,
1807 /* look if this item intersects with the given coordinates */
1808 h_intersect = left_attach <= l && l <= right_attach;
1809 h_intersect &= left_attach <= r && r <= right_attach;
1811 v_intersect = top_attach <= t && t <= bottom_attach;
1812 v_intersect &= top_attach <= b && b <= bottom_attach;
1814 if (h_intersect && v_intersect)
1822 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1825 gint current_col = 0, current_row = 0;
1831 menu = combo_box->priv->popup_widget;
1832 if (!GTK_IS_MENU_SHELL (menu))
1835 list = gtk_container_get_children (GTK_CONTAINER (menu));
1836 item = g_list_nth_data (list, index);
1839 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1841 /* look for a good spot */
1844 if (current_col + cols > combo_box->priv->wrap_width)
1850 if (!menu_occupied (GTK_MENU (menu),
1851 current_col, current_col + cols,
1852 current_row, current_row + rows))
1858 /* set attach props */
1859 gtk_menu_attach (GTK_MENU (menu), item,
1860 current_col, current_col + cols,
1861 current_row, current_row + rows);
1865 gtk_combo_box_relayout (GtkComboBox *combo_box)
1870 /* do nothing unless we are in menu style */
1871 if (combo_box->priv->tree_view)
1874 menu = combo_box->priv->popup_widget;
1876 /* get rid of all children */
1877 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1879 list = gtk_container_get_children (GTK_CONTAINER (menu));
1881 for (j = g_list_last (list); j; j = j->prev)
1882 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1887 gtk_combo_box_menu_fill (combo_box);
1892 gtk_combo_box_menu_button_press (GtkWidget *widget,
1893 GdkEventButton *event,
1896 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1898 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1901 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1903 combo_box->priv->popup_in_progress = TRUE;
1905 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1906 combo_box->priv->active_item);
1908 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1910 gtk_combo_box_menu_position, combo_box,
1911 event->button, event->time);
1920 gtk_combo_box_menu_item_activate (GtkWidget *item,
1925 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1927 menu = combo_box->priv->popup_widget;
1928 g_return_if_fail (GTK_IS_MENU (menu));
1930 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1932 gtk_combo_box_set_active (combo_box, index);
1936 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1941 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1942 gint index = gtk_tree_path_get_indices (path)[0];
1944 if (combo_box->priv->active_item >= index)
1945 combo_box->priv->active_item++;
1947 if (!combo_box->priv->tree_view)
1948 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1952 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1956 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1957 gint index = gtk_tree_path_get_indices (path)[0];
1959 if (!combo_box->priv->tree_view)
1960 gtk_combo_box_menu_row_deleted (model, path, user_data);
1962 if (index == combo_box->priv->active_item)
1964 gint items = gtk_tree_model_iter_n_children (model, NULL);
1967 gtk_combo_box_set_active_internal (combo_box, -1);
1968 else if (index == items)
1969 gtk_combo_box_set_active_internal (combo_box, index - 1);
1971 gtk_combo_box_set_active_internal (combo_box, index);
1973 else if (combo_box->priv->active_item > index)
1974 combo_box->priv->active_item--;
1978 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1984 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1985 gint items = gtk_tree_model_iter_n_children (model, NULL);
1988 for (i = 0; i < items; i++)
1989 if (new_order[i] == combo_box->priv->active_item)
1991 combo_box->priv->active_item = i;
1995 if (!combo_box->priv->tree_view)
1996 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2000 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2005 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2006 gint index = gtk_tree_path_get_indices (path)[0];
2008 if (index == combo_box->priv->active_item &&
2009 combo_box->priv->cell_view)
2010 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2012 if (combo_box->priv->tree_view)
2013 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2015 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2020 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2027 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2029 if (!combo_box->priv->popup_widget)
2032 menu = combo_box->priv->popup_widget;
2033 g_return_if_fail (GTK_IS_MENU (menu));
2035 item = gtk_cell_view_menu_item_new_from_model (model, path);
2036 g_signal_connect (item, "activate",
2037 G_CALLBACK (gtk_combo_box_menu_item_activate),
2040 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2042 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2043 gtk_tree_path_get_indices (path)[0]);
2044 gtk_widget_show (item);
2048 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2055 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2057 if (!combo_box->priv->popup_widget)
2060 index = gtk_tree_path_get_indices (path)[0];
2062 menu = combo_box->priv->popup_widget;
2063 g_return_if_fail (GTK_IS_MENU (menu));
2065 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2066 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2068 gtk_container_remove (GTK_CONTAINER (menu), item);
2072 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2078 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2080 gtk_combo_box_relayout (combo_box);
2084 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2089 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2092 if (!combo_box->priv->popup_widget)
2095 if (combo_box->priv->wrap_width)
2096 gtk_combo_box_relayout_item (combo_box,
2097 gtk_tree_path_get_indices (path)[0]);
2099 width = gtk_combo_box_calc_requested_width (combo_box, path);
2101 if (width > combo_box->priv->width)
2103 if (combo_box->priv->cell_view)
2105 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2106 gtk_widget_queue_resize (combo_box->priv->cell_view);
2108 combo_box->priv->width = width;
2117 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2120 GtkTreeSelection *sel;
2122 combo_box->priv->button = gtk_toggle_button_new ();
2123 gtk_widget_set_parent (combo_box->priv->button,
2124 GTK_BIN (combo_box)->child->parent);
2125 g_signal_connect (combo_box->priv->button, "button_press_event",
2126 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2127 g_signal_connect (combo_box->priv->button, "toggled",
2128 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2129 g_signal_connect_after (combo_box, "key_press_event",
2130 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2132 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2133 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2134 combo_box->priv->arrow);
2135 combo_box->priv->separator = NULL;
2136 gtk_widget_show_all (combo_box->priv->button);
2138 if (combo_box->priv->cell_view)
2140 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2141 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2142 GTK_BIN (combo_box)->child->parent);
2143 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2146 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2147 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2149 gtk_widget_show (combo_box->priv->cell_view_frame);
2152 combo_box->priv->tree_view = gtk_tree_view_new ();
2153 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2154 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2155 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2157 if (combo_box->priv->model)
2158 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2159 combo_box->priv->model);
2161 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2162 G_CALLBACK (gtk_combo_box_list_button_pressed),
2164 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2165 G_CALLBACK (gtk_combo_box_list_button_released),
2167 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2168 G_CALLBACK (gtk_combo_box_list_key_press),
2171 combo_box->priv->column = gtk_tree_view_column_new ();
2172 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2173 combo_box->priv->column);
2176 for (i = combo_box->priv->cells; i; i = i->next)
2179 ComboCellInfo *info = (ComboCellInfo *)i->data;
2181 if (info->pack == GTK_PACK_START)
2182 gtk_tree_view_column_pack_start (combo_box->priv->column,
2183 info->cell, info->expand);
2184 else if (info->pack == GTK_PACK_END)
2185 gtk_tree_view_column_pack_end (combo_box->priv->column,
2186 info->cell, info->expand);
2188 for (j = info->attributes; j; j = j->next->next)
2190 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2193 GPOINTER_TO_INT (j->next->data));
2197 if (combo_box->priv->active_item != -1)
2201 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2204 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2206 gtk_tree_path_free (path);
2210 /* set sample/popup widgets */
2211 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2213 gtk_widget_show (combo_box->priv->tree_view);
2217 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2219 /* disconnect signals */
2220 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2221 G_SIGNAL_MATCH_DATA,
2222 0, 0, NULL, NULL, combo_box);
2223 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2224 G_SIGNAL_MATCH_DATA,
2226 gtk_combo_box_list_button_pressed,
2229 /* destroy things (unparent will kill the latest ref from us)
2230 * last unref on button will destroy the arrow
2232 gtk_widget_unparent (combo_box->priv->button);
2233 combo_box->priv->button = NULL;
2234 combo_box->priv->arrow = NULL;
2236 if (combo_box->priv->cell_view)
2238 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2239 "background_set", FALSE,
2242 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2243 combo_box->priv->cell_view_frame = NULL;
2246 gtk_widget_destroy (combo_box->priv->tree_view);
2248 combo_box->priv->tree_view = NULL;
2249 combo_box->priv->popup_widget = NULL;
2254 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2256 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2257 gtk_grab_remove (combo_box->priv->tree_view);
2259 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2261 gtk_grab_remove (combo_box->priv->popup_window);
2262 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2263 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2268 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2269 GdkEventButton *event,
2272 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2274 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2276 if (ewidget == combo_box->priv->tree_view)
2279 if ((ewidget != combo_box->priv->button) ||
2280 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2283 gtk_combo_box_popup (combo_box);
2285 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2288 combo_box->priv->popup_in_progress = TRUE;
2294 gtk_combo_box_list_button_released (GtkWidget *widget,
2295 GdkEventButton *event,
2299 GtkTreePath *path = NULL;
2301 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2303 gboolean popup_in_progress = FALSE;
2305 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2307 if (combo_box->priv->popup_in_progress)
2309 popup_in_progress = TRUE;
2310 combo_box->priv->popup_in_progress = FALSE;
2313 if (ewidget != combo_box->priv->tree_view)
2315 if (ewidget == combo_box->priv->button &&
2316 !popup_in_progress &&
2317 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2319 gtk_combo_box_popdown (combo_box);
2323 /* released outside treeview */
2324 if (ewidget != combo_box->priv->button)
2326 gtk_combo_box_popdown (combo_box);
2334 /* select something cool */
2335 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2341 return TRUE; /* clicked outside window? */
2343 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2344 gtk_combo_box_popdown (combo_box);
2346 gtk_tree_path_free (path);
2352 gtk_combo_box_key_press (GtkWidget *widget,
2356 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2357 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2358 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2359 gint index = gtk_combo_box_get_active (combo_box);
2362 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2363 state == GDK_MOD1_MASK)
2365 gtk_combo_box_popup (combo_box);
2370 switch (event->keyval)
2374 new_index = index + 1;
2378 new_index = index - 1;
2381 case GDK_KP_Page_Up:
2387 case GDK_KP_Page_Down:
2390 new_index = items - 1;
2396 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2402 gtk_combo_box_menu_key_press (GtkWidget *widget,
2406 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2407 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2409 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2410 state == GDK_MOD1_MASK)
2412 gtk_combo_box_popdown (combo_box);
2421 gtk_combo_box_list_key_press (GtkWidget *widget,
2425 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2426 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2428 if (event->keyval == GDK_Escape ||
2429 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2430 state == GDK_MOD1_MASK))
2432 /* reset active item -- this is incredibly lame and ugly */
2433 gtk_combo_box_set_active (combo_box,
2434 gtk_combo_box_get_active (combo_box));
2436 gtk_combo_box_popdown (combo_box);
2441 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2442 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2446 GtkTreeModel *model = NULL;
2447 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2449 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2454 path = gtk_tree_model_get_path (model, &iter);
2457 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2458 gtk_tree_path_free (path);
2467 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2472 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2475 width = gtk_combo_box_calc_requested_width (combo_box, path);
2477 if (width > combo_box->priv->width)
2479 if (combo_box->priv->cell_view)
2481 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2482 gtk_widget_queue_resize (combo_box->priv->cell_view);
2484 combo_box->priv->width = width;
2489 * GtkCellLayout implementation
2492 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2493 GtkCellRenderer *cell,
2496 ComboCellInfo *info;
2497 GtkComboBox *combo_box;
2500 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2501 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2503 combo_box = GTK_COMBO_BOX (layout);
2505 g_object_ref (G_OBJECT (cell));
2506 gtk_object_sink (GTK_OBJECT (cell));
2508 info = g_new0 (ComboCellInfo, 1);
2510 info->expand = expand;
2511 info->pack = GTK_PACK_START;
2513 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2515 if (combo_box->priv->cell_view)
2516 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2519 if (combo_box->priv->column)
2520 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2522 menu = combo_box->priv->popup_widget;
2523 if (GTK_IS_MENU (menu))
2527 list = gtk_container_get_children (GTK_CONTAINER (menu));
2528 for (i = list; i; i = i->next)
2532 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2533 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2535 view = GTK_CELL_VIEW (i->data);
2537 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2544 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2545 GtkCellRenderer *cell,
2548 ComboCellInfo *info;
2549 GtkComboBox *combo_box;
2552 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2553 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2555 combo_box = GTK_COMBO_BOX (layout);
2557 g_object_ref (G_OBJECT (cell));
2558 gtk_object_sink (GTK_OBJECT (cell));
2560 info = g_new0 (ComboCellInfo, 1);
2562 info->expand = expand;
2563 info->pack = GTK_PACK_END;
2565 if (combo_box->priv->cell_view)
2566 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2569 if (combo_box->priv->column)
2570 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2572 menu = combo_box->priv->popup_widget;
2573 if (GTK_IS_MENU (menu))
2577 list = gtk_container_get_children (GTK_CONTAINER (menu));
2578 for (i = list; i; i = i->next)
2582 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2583 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2585 view = GTK_CELL_VIEW (i->data);
2587 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2594 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2597 GtkComboBox *combo_box;
2600 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2602 combo_box = GTK_COMBO_BOX (layout);
2604 if (combo_box->priv->cell_view)
2605 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2607 if (combo_box->priv->column)
2608 gtk_tree_view_column_clear (combo_box->priv->column);
2610 for (i = combo_box->priv->cells; i; i = i->next)
2612 ComboCellInfo *info = (ComboCellInfo *)i->data;
2614 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2615 g_object_unref (G_OBJECT (info->cell));
2619 g_slist_free (combo_box->priv->cells);
2620 combo_box->priv->cells = NULL;
2622 menu = combo_box->priv->popup_widget;
2623 if (GTK_IS_MENU (menu))
2627 list = gtk_container_get_children (GTK_CONTAINER (menu));
2628 for (i = list; i; i = i->next)
2632 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2633 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2635 view = GTK_CELL_VIEW (i->data);
2637 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2644 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2645 GtkCellRenderer *cell,
2646 const gchar *attribute,
2649 ComboCellInfo *info;
2650 GtkComboBox *combo_box;
2653 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2654 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2656 combo_box = GTK_COMBO_BOX (layout);
2658 info = gtk_combo_box_get_cell_info (combo_box, cell);
2660 info->attributes = g_slist_prepend (info->attributes,
2661 GINT_TO_POINTER (column));
2662 info->attributes = g_slist_prepend (info->attributes,
2663 g_strdup (attribute));
2665 if (combo_box->priv->cell_view)
2666 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2667 cell, attribute, column);
2669 if (combo_box->priv->column)
2670 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2671 cell, attribute, column);
2673 menu = combo_box->priv->popup_widget;
2674 if (GTK_IS_MENU (menu))
2678 list = gtk_container_get_children (GTK_CONTAINER (menu));
2679 for (i = list; i; i = i->next)
2683 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2684 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2686 view = GTK_CELL_VIEW (i->data);
2688 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2694 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2698 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2699 GtkCellRenderer *cell,
2700 GtkCellLayoutDataFunc func,
2702 GDestroyNotify destroy)
2704 ComboCellInfo *info;
2705 GtkComboBox *combo_box;
2708 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2710 combo_box = GTK_COMBO_BOX (layout);
2712 info = gtk_combo_box_get_cell_info (combo_box, cell);
2713 g_return_if_fail (info != NULL);
2717 GDestroyNotify d = info->destroy;
2719 info->destroy = NULL;
2720 d (info->func_data);
2724 info->func_data = func_data;
2725 info->destroy = destroy;
2727 if (combo_box->priv->cell_view)
2728 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2730 if (combo_box->priv->column)
2731 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2733 menu = combo_box->priv->popup_widget;
2734 if (GTK_IS_MENU (menu))
2738 list = gtk_container_get_children (GTK_CONTAINER (menu));
2739 for (i = list; i; i = i->next)
2743 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2744 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2746 view = GTK_CELL_VIEW (i->data);
2748 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2749 func, func_data, NULL);
2754 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2758 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2759 GtkCellRenderer *cell)
2761 ComboCellInfo *info;
2762 GtkComboBox *combo_box;
2766 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2767 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2769 combo_box = GTK_COMBO_BOX (layout);
2771 info = gtk_combo_box_get_cell_info (combo_box, cell);
2772 g_return_if_fail (info != NULL);
2774 list = info->attributes;
2775 while (list && list->next)
2777 g_free (list->data);
2778 list = list->next->next;
2780 g_slist_free (info->attributes);
2781 info->attributes = NULL;
2783 if (combo_box->priv->cell_view)
2784 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2786 if (combo_box->priv->column)
2787 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2789 menu = combo_box->priv->popup_widget;
2790 if (GTK_IS_MENU (menu))
2794 list = gtk_container_get_children (GTK_CONTAINER (menu));
2795 for (i = list; i; i = i->next)
2799 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2800 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2802 view = GTK_CELL_VIEW (i->data);
2804 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2809 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2813 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2814 GtkCellRenderer *cell,
2817 ComboCellInfo *info;
2818 GtkComboBox *combo_box;
2822 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2823 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2825 combo_box = GTK_COMBO_BOX (layout);
2827 info = gtk_combo_box_get_cell_info (combo_box, cell);
2829 g_return_if_fail (info != NULL);
2830 g_return_if_fail (position >= 0);
2832 link = g_slist_find (combo_box->priv->cells, info);
2834 g_return_if_fail (link != NULL);
2836 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2837 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2840 if (combo_box->priv->cell_view)
2841 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2844 if (combo_box->priv->column)
2845 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2848 menu = combo_box->priv->popup_widget;
2849 if (GTK_IS_MENU (menu))
2853 list = gtk_container_get_children (GTK_CONTAINER (menu));
2854 for (i = list; i; i = i->next)
2858 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2859 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2861 view = GTK_CELL_VIEW (i->data);
2863 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2868 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2876 * gtk_combo_box_new:
2878 * Creates a new empty #GtkComboBox.
2880 * Return value: A new #GtkComboBox.
2885 gtk_combo_box_new (void)
2887 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2891 * gtk_combo_box_new_with_model:
2892 * @model: A #GtkTreeModel.
2894 * Creates a new #GtkComboBox with the model initialized to @model.
2896 * Return value: A new #GtkComboBox.
2901 gtk_combo_box_new_with_model (GtkTreeModel *model)
2903 GtkComboBox *combo_box;
2905 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2907 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2911 return GTK_WIDGET (combo_box);
2915 * gtk_combo_box_set_wrap_width:
2916 * @combo_box: A #GtkComboBox.
2917 * @width: Preferred number of columns.
2919 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2920 * the preferred number of columns when you want to the popup to be layed out
2926 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2929 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2930 g_return_if_fail (width >= 0);
2932 if (width != combo_box->priv->wrap_width)
2934 combo_box->priv->wrap_width = width;
2936 gtk_combo_box_check_appearance (combo_box);
2937 gtk_combo_box_relayout (combo_box);
2939 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2944 * gtk_combo_box_set_row_span_column:
2945 * @combo_box: A #GtkComboBox.
2946 * @row_span: A column in the model passed during construction.
2948 * Sets the column with row span information for @combo_box to be @row_span.
2949 * The row span column contains integers which indicate how many rows
2950 * an item should span.
2955 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2960 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2962 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2963 g_return_if_fail (row_span >= 0 && row_span < col);
2965 if (row_span != combo_box->priv->row_column)
2967 combo_box->priv->row_column = row_span;
2969 gtk_combo_box_relayout (combo_box);
2971 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2976 * gtk_combo_box_set_column_span_column:
2977 * @combo_box: A #GtkComboBox.
2978 * @column_span: A column in the model passed during construction.
2980 * Sets the column with column span information for @combo_box to be
2981 * @column_span. The column span column contains integers which indicate
2982 * how many columns an item should span.
2987 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2992 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2994 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2995 g_return_if_fail (column_span >= 0 && column_span < col);
2997 if (column_span != combo_box->priv->col_column)
2999 combo_box->priv->col_column = column_span;
3001 gtk_combo_box_relayout (combo_box);
3003 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3008 * gtk_combo_box_get_active:
3009 * @combo_box: A #GtkComboBox.
3011 * Returns the index of the currently active item, or -1 if there's no
3014 * Return value: An integer which is the index of the currently active item, or
3015 * -1 if there's no active item.
3020 gtk_combo_box_get_active (GtkComboBox *combo_box)
3022 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3024 return combo_box->priv->active_item;
3028 * gtk_combo_box_set_active:
3029 * @combo_box: A #GtkComboBox.
3030 * @index: An index in the model passed during construction, or -1 to have
3033 * Sets the active item of @combo_box to be the item at @index.
3038 gtk_combo_box_set_active (GtkComboBox *combo_box,
3041 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3042 /* -1 means "no item selected" */
3043 g_return_if_fail (index >= -1);
3045 if (combo_box->priv->active_item == index)
3048 gtk_combo_box_set_active_internal (combo_box, index);
3052 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3057 combo_box->priv->active_item = index;
3061 if (combo_box->priv->tree_view)
3062 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3065 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3067 if (GTK_IS_MENU (menu))
3068 gtk_menu_set_active (menu, -1);
3071 if (combo_box->priv->cell_view)
3072 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3076 path = gtk_tree_path_new_from_indices (index, -1);
3078 if (combo_box->priv->tree_view)
3079 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3082 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3084 if (GTK_IS_MENU (menu))
3085 gtk_menu_set_active (GTK_MENU (menu), index);
3088 if (combo_box->priv->cell_view)
3089 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3091 gtk_tree_path_free (path);
3094 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3099 * gtk_combo_box_get_active_iter:
3100 * @combo_box: A #GtkComboBox
3101 * @iter: The uninitialized #GtkTreeIter.
3103 * Sets @iter to point to the current active item, if it exists.
3105 * Return value: %TRUE, if @iter was set
3110 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3117 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3119 active = gtk_combo_box_get_active (combo_box);
3123 path = gtk_tree_path_new_from_indices (active, -1);
3124 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3126 gtk_tree_path_free (path);
3132 * gtk_combo_box_set_active_iter:
3133 * @combo_box: A #GtkComboBox
3134 * @iter: The #GtkTreeIter.
3136 * Sets the current active item to be the one referenced by @iter.
3137 * @iter must correspond to a path of depth one.
3142 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3147 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3149 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3150 g_return_if_fail (path != NULL);
3151 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3153 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3154 gtk_tree_path_free (path);
3158 * gtk_combo_box_set_model:
3159 * @combo_box: A #GtkComboBox.
3160 * @model: A #GtkTreeModel.
3162 * Sets the model used by @combo_box to be @model. Will unset a
3163 * previously set model (if applicable).
3168 gtk_combo_box_set_model (GtkComboBox *combo_box,
3169 GtkTreeModel *model)
3171 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3172 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3174 if (model == combo_box->priv->model)
3177 if (combo_box->priv->model)
3178 gtk_combo_box_unset_model (combo_box);
3180 combo_box->priv->model = model;
3181 g_object_ref (G_OBJECT (combo_box->priv->model));
3183 combo_box->priv->inserted_id =
3184 g_signal_connect (combo_box->priv->model, "row_inserted",
3185 G_CALLBACK (gtk_combo_box_model_row_inserted),
3187 combo_box->priv->deleted_id =
3188 g_signal_connect (combo_box->priv->model, "row_deleted",
3189 G_CALLBACK (gtk_combo_box_model_row_deleted),
3191 combo_box->priv->reordered_id =
3192 g_signal_connect (combo_box->priv->model, "rows_reordered",
3193 G_CALLBACK (gtk_combo_box_model_rows_reordered),
3195 combo_box->priv->changed_id =
3196 g_signal_connect (combo_box->priv->model, "row_changed",
3197 G_CALLBACK (gtk_combo_box_model_row_changed),
3200 if (combo_box->priv->tree_view)
3203 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3204 combo_box->priv->model);
3209 if (combo_box->priv->popup_widget)
3210 gtk_combo_box_menu_fill (combo_box);
3214 if (combo_box->priv->cell_view)
3215 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3216 combo_box->priv->model);
3220 * gtk_combo_box_get_model
3221 * @combo_box: A #GtkComboBox.
3223 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3225 * Return value: A #GtkTreeModel which was passed during construction.
3230 gtk_combo_box_get_model (GtkComboBox *combo_box)
3232 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3234 return combo_box->priv->model;
3238 /* convenience API for simple text combos */
3241 * gtk_combo_box_new_text:
3243 * Convenience function which constructs a new text combo box, which is a
3244 * #GtkComboBox just displaying strings. If you use this function to create
3245 * a text combo box, you should only manipulate its data source with the
3246 * following convenience functions: gtk_combo_box_append_text(),
3247 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3248 * gtk_combo_box_remove_text().
3250 * Return value: A new text combo box.
3255 gtk_combo_box_new_text (void)
3257 GtkWidget *combo_box;
3258 GtkCellRenderer *cell;
3259 GtkListStore *store;
3261 store = gtk_list_store_new (1, G_TYPE_STRING);
3263 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3265 cell = gtk_cell_renderer_text_new ();
3266 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3267 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3275 * gtk_combo_box_append_text:
3276 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3279 * Appends @string to the list of strings stored in @combo_box. Note that
3280 * you can only use this function with combo boxes constructed with
3281 * gtk_combo_box_new_text().
3286 gtk_combo_box_append_text (GtkComboBox *combo_box,
3290 GtkListStore *store;
3292 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3293 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3294 g_return_if_fail (text != NULL);
3296 store = GTK_LIST_STORE (combo_box->priv->model);
3298 gtk_list_store_append (store, &iter);
3299 gtk_list_store_set (store, &iter, 0, text, -1);
3303 * gtk_combo_box_insert_text:
3304 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3305 * @position: An index to insert @text.
3308 * Inserts @string at @position in the list of strings stored in @combo_box.
3309 * Note that you can only use this function with combo boxes constructed
3310 * with gtk_combo_box_new_text().
3315 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3320 GtkListStore *store;
3322 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3323 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3324 g_return_if_fail (position >= 0);
3325 g_return_if_fail (text != NULL);
3327 store = GTK_LIST_STORE (combo_box->priv->model);
3329 gtk_list_store_insert (store, &iter, position);
3330 gtk_list_store_set (store, &iter, 0, text, -1);
3334 * gtk_combo_box_prepend_text:
3335 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3338 * Prepends @string to the list of strings stored in @combo_box. Note that
3339 * you can only use this function with combo boxes constructed with
3340 * gtk_combo_box_new_text().
3345 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3349 GtkListStore *store;
3351 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3352 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3353 g_return_if_fail (text != NULL);
3355 store = GTK_LIST_STORE (combo_box->priv->model);
3357 gtk_list_store_prepend (store, &iter);
3358 gtk_list_store_set (store, &iter, 0, text, -1);
3362 * gtk_combo_box_remove_text:
3363 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3364 * @position: Index of the item to remove.
3366 * Removes the string at @position from @combo_box. Note that you can only use
3367 * this function with combo boxes constructed with gtk_combo_box_new_text().
3372 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3376 GtkListStore *store;
3378 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3379 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3380 g_return_if_fail (position >= 0);
3382 store = GTK_LIST_STORE (combo_box->priv->model);
3384 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3386 gtk_list_store_remove (store, &iter);
3391 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3392 gboolean group_cycling)
3394 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3396 gtk_widget_grab_focus (combo_box->priv->button);
3402 gtk_combo_box_destroy (GtkObject *object)
3404 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3406 combo_box->priv->destroying = 1;
3408 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3410 combo_box->priv->cell_view = NULL;
3412 combo_box->priv->destroying = 0;
3416 gtk_combo_box_finalize (GObject *object)
3418 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3421 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3422 gtk_combo_box_menu_destroy (combo_box);
3424 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3425 gtk_combo_box_list_destroy (combo_box);
3427 if (combo_box->priv->popup_window)
3428 gtk_widget_destroy (combo_box->priv->popup_window);
3430 gtk_combo_box_unset_model (combo_box);
3432 if (combo_box->priv->model)
3433 g_object_unref (combo_box->priv->model);
3435 for (i = combo_box->priv->cells; i; i = i->next)
3437 ComboCellInfo *info = (ComboCellInfo *)i->data;
3438 GSList *list = info->attributes;
3441 info->destroy (info->func_data);
3443 while (list && list->next)
3445 g_free (list->data);
3446 list = list->next->next;
3448 g_slist_free (info->attributes);
3450 g_object_unref (G_OBJECT (info->cell));
3453 g_slist_free (combo_box->priv->cells);
3455 G_OBJECT_CLASS (parent_class)->finalize (object);