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"
32 #include "gtkliststore.h"
35 #include "gtktogglebutton.h"
36 #include "gtktreeselection.h"
37 #include "gtkvseparator.h"
38 #include "gtkwindow.h"
40 #include <gdk/gdkkeysyms.h>
42 #include <gobject/gvaluecollector.h>
47 #include "gtkmarshalers.h"
51 /* WELCOME, to THE house of evil code */
53 typedef struct _ComboCellInfo ComboCellInfo;
56 GtkCellRenderer *cell;
59 GtkCellLayoutDataFunc func;
61 GDestroyNotify destroy;
67 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
69 struct _GtkComboBoxPrivate
81 GtkTreeViewColumn *column;
84 GtkWidget *cell_view_frame;
91 GtkWidget *popup_widget;
92 GtkWidget *popup_window;
93 GtkWidget *popup_frame;
103 guint popup_in_progress : 1;
104 guint destroying : 1;
107 /* While debugging this evil code, I have learned that
108 * there are actually 4 modes to this widget, which can
109 * be characterized as follows
111 * 1) menu mode, no child added
114 * cell_view -> GtkCellView, regular child
115 * cell_view_frame -> NULL
116 * button -> GtkToggleButton set_parent to combo
117 * arrow -> GtkArrow set_parent to button
118 * separator -> GtkVSepator set_parent to button
119 * popup_widget -> GtkMenu
120 * popup_window -> NULL
121 * popup_frame -> NULL
123 * 2) menu mode, child added
127 * cell_view_frame -> NULL
128 * button -> GtkToggleButton set_parent to combo
129 * arrow -> GtkArrow, child of button
131 * popup_widget -> GtkMenu
132 * popup_window -> NULL
133 * popup_frame -> NULL
135 * 3) list mode, no child added
137 * tree_view -> GtkTreeView, child of popup_frame
138 * cell_view -> GtkCellView, regular child
139 * cell_view_frame -> GtkFrame, set parent to combo
140 * button -> GtkToggleButton, set_parent to combo
141 * arrow -> GtkArrow, child of button
143 * popup_widget -> tree_view
144 * popup_window -> GtkWindow
145 * popup_frame -> GtkFrame, child of popup_window
147 * 4) list mode, child added
149 * tree_view -> GtkTreeView, child of popup_frame
151 * cell_view_frame -> NULL
152 * button -> GtkToggleButton, set_parent to combo
153 * arrow -> GtkArrow, child of button
155 * popup_widget -> tree_view
156 * popup_window -> GtkWindow
157 * popup_frame -> GtkFrame, child of popup_window
170 PROP_ROW_SPAN_COLUMN,
171 PROP_COLUMN_SPAN_COLUMN,
175 static GtkBinClass *parent_class = NULL;
176 static guint combo_box_signals[LAST_SIGNAL] = {0,};
178 #define BONUS_PADDING 4
182 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
183 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
184 static void gtk_combo_box_init (GtkComboBox *combo_box);
185 static void gtk_combo_box_finalize (GObject *object);
186 static void gtk_combo_box_destroy (GtkObject *object);
188 static void gtk_combo_box_set_property (GObject *object,
192 static void gtk_combo_box_get_property (GObject *object,
197 static void gtk_combo_box_state_changed (GtkWidget *widget,
198 GtkStateType previous);
199 static void gtk_combo_box_style_set (GtkWidget *widget,
201 static void gtk_combo_box_button_toggled (GtkWidget *widget,
203 static void gtk_combo_box_add (GtkContainer *container,
205 static void gtk_combo_box_remove (GtkContainer *container,
208 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
209 GtkCellRenderer *cell);
211 static void gtk_combo_box_menu_show (GtkWidget *menu,
213 static void gtk_combo_box_menu_hide (GtkWidget *menu,
216 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
218 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
223 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
228 static void gtk_combo_box_menu_position (GtkMenu *menu,
234 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
236 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
238 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
240 static void gtk_combo_box_size_request (GtkWidget *widget,
241 GtkRequisition *requisition);
242 static void gtk_combo_box_size_allocate (GtkWidget *widget,
243 GtkAllocation *allocation);
244 static void gtk_combo_box_forall (GtkContainer *container,
245 gboolean include_internals,
246 GtkCallback callback,
247 gpointer callback_data);
248 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
249 GdkEventExpose *event);
250 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
251 GdkEventScroll *event);
252 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
254 static gboolean gtk_combo_box_key_press (GtkWidget *widget,
258 /* listening to the model */
259 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
263 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
266 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
271 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
277 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
282 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
283 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
285 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
287 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
288 GdkEventButton *event,
290 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
293 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
294 GdkEventButton *event,
297 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
303 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
304 gboolean add_children);
305 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
306 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
308 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
312 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
314 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
316 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
317 GdkEventButton *event,
319 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
321 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
325 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
328 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
333 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
337 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
340 static void gtk_combo_box_menu_state_changed (GtkWidget *widget,
341 GtkStateType previous,
345 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
346 GtkCellRenderer *cell,
348 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
349 GtkCellRenderer *cell,
351 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
352 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
353 GtkCellRenderer *cell,
354 const gchar *attribute,
356 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
357 GtkCellRenderer *cell,
358 GtkCellLayoutDataFunc func,
360 GDestroyNotify destroy);
361 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
362 GtkCellRenderer *cell);
363 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
364 GtkCellRenderer *cell,
366 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
367 gboolean group_cycling);
369 static void cell_view_sync_cells (GtkComboBox *combo_box,
370 GtkCellView *cell_view);
373 gtk_combo_box_get_type (void)
375 static GType combo_box_type = 0;
379 static const GTypeInfo combo_box_info =
381 sizeof (GtkComboBoxClass),
382 NULL, /* base_init */
383 NULL, /* base_finalize */
384 (GClassInitFunc) gtk_combo_box_class_init,
385 NULL, /* class_finalize */
386 NULL, /* class_data */
387 sizeof (GtkComboBox),
389 (GInstanceInitFunc) gtk_combo_box_init
392 static const GInterfaceInfo cell_layout_info =
394 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
399 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
404 g_type_add_interface_static (combo_box_type,
405 GTK_TYPE_CELL_LAYOUT,
409 return combo_box_type;
414 gtk_combo_box_class_init (GtkComboBoxClass *klass)
416 GObjectClass *object_class;
417 GtkBindingSet *binding_set;
418 GtkObjectClass *gtk_object_class;
419 GtkContainerClass *container_class;
420 GtkWidgetClass *widget_class;
422 binding_set = gtk_binding_set_by_class (klass);
424 container_class = (GtkContainerClass *)klass;
425 container_class->forall = gtk_combo_box_forall;
426 container_class->add = gtk_combo_box_add;
427 container_class->remove = gtk_combo_box_remove;
429 widget_class = (GtkWidgetClass *)klass;
430 widget_class->size_allocate = gtk_combo_box_size_allocate;
431 widget_class->size_request = gtk_combo_box_size_request;
432 widget_class->expose_event = gtk_combo_box_expose_event;
433 widget_class->scroll_event = gtk_combo_box_scroll_event;
434 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
435 widget_class->style_set = gtk_combo_box_style_set;
436 widget_class->state_changed = gtk_combo_box_state_changed;
438 gtk_object_class = (GtkObjectClass *)klass;
439 gtk_object_class->destroy = gtk_combo_box_destroy;
441 object_class = (GObjectClass *)klass;
442 object_class->finalize = gtk_combo_box_finalize;
443 object_class->set_property = gtk_combo_box_set_property;
444 object_class->get_property = gtk_combo_box_get_property;
446 parent_class = g_type_class_peek_parent (klass);
449 combo_box_signals[CHANGED] =
450 g_signal_new ("changed",
451 G_OBJECT_CLASS_TYPE (klass),
453 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
455 g_cclosure_marshal_VOID__VOID,
459 g_object_class_install_property (object_class,
461 g_param_spec_object ("model",
462 P_("ComboBox model"),
463 P_("The model for the combo box"),
467 g_object_class_install_property (object_class,
469 g_param_spec_int ("wrap_width",
471 P_("Wrap width for layouting the items in a grid"),
477 g_object_class_install_property (object_class,
478 PROP_ROW_SPAN_COLUMN,
479 g_param_spec_int ("row_span_column",
480 P_("Row span column"),
481 P_("TreeModel column containing the row span values"),
487 g_object_class_install_property (object_class,
488 PROP_COLUMN_SPAN_COLUMN,
489 g_param_spec_int ("column_span_column",
490 P_("Column span column"),
491 P_("TreeModel column containing the column span values"),
497 g_object_class_install_property (object_class,
499 g_param_spec_int ("active",
501 P_("The item which is currently active"),
507 gtk_widget_class_install_style_property (widget_class,
508 g_param_spec_boolean ("appears-as-list",
509 P_("Appears as list"),
510 P_("Whether combobox dropdowns should look like lists rather than menus"),
514 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
518 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
520 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
521 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
522 iface->clear = gtk_combo_box_cell_layout_clear;
523 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
524 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
525 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
526 iface->reorder = gtk_combo_box_cell_layout_reorder;
530 gtk_combo_box_init (GtkComboBox *combo_box)
532 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
534 combo_box->priv->cell_view = gtk_cell_view_new ();
535 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
536 GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
537 gtk_widget_show (combo_box->priv->cell_view);
539 combo_box->priv->width = 0;
540 combo_box->priv->wrap_width = 0;
542 combo_box->priv->active_item = -1;
543 combo_box->priv->col_column = -1;
544 combo_box->priv->row_column = -1;
548 gtk_combo_box_set_property (GObject *object,
553 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
558 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
561 case PROP_WRAP_WIDTH:
562 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
565 case PROP_ROW_SPAN_COLUMN:
566 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
569 case PROP_COLUMN_SPAN_COLUMN:
570 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
574 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
583 gtk_combo_box_get_property (GObject *object,
588 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
593 g_value_set_object (value, combo_box->priv->model);
596 case PROP_WRAP_WIDTH:
597 g_value_set_int (value, combo_box->priv->wrap_width);
600 case PROP_ROW_SPAN_COLUMN:
601 g_value_set_int (value, combo_box->priv->row_column);
604 case PROP_COLUMN_SPAN_COLUMN:
605 g_value_set_int (value, combo_box->priv->col_column);
609 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
613 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619 gtk_combo_box_state_changed (GtkWidget *widget,
620 GtkStateType previous)
622 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
624 if (GTK_WIDGET_REALIZED (widget))
626 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
627 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
628 &widget->style->base[GTK_WIDGET_STATE (widget)]);
631 gtk_widget_queue_draw (widget);
635 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
637 gboolean appears_as_list;
639 /* if wrap_width > 0, then we are in grid-mode and forced to use
642 if (combo_box->priv->wrap_width)
643 appears_as_list = FALSE;
645 gtk_widget_style_get (GTK_WIDGET (combo_box),
646 "appears-as-list", &appears_as_list,
651 /* Destroy all the menu mode widgets, if they exist. */
652 if (GTK_IS_MENU (combo_box->priv->popup_widget))
653 gtk_combo_box_menu_destroy (combo_box);
655 /* Create the list mode widgets, if they don't already exist. */
656 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
657 gtk_combo_box_list_setup (combo_box);
661 /* Destroy all the list mode widgets, if they exist. */
662 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
663 gtk_combo_box_list_destroy (combo_box);
665 /* Create the menu mode widgets, if they don't already exist. */
666 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
667 gtk_combo_box_menu_setup (combo_box, TRUE);
672 gtk_combo_box_style_set (GtkWidget *widget,
675 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
677 gtk_combo_box_check_appearance (combo_box);
679 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
680 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
681 &widget->style->base[GTK_WIDGET_STATE (widget)]);
685 gtk_combo_box_button_toggled (GtkWidget *widget,
688 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
690 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
692 if (!combo_box->priv->popup_in_progress)
693 gtk_combo_box_popup (combo_box);
696 gtk_combo_box_popdown (combo_box);
700 gtk_combo_box_add (GtkContainer *container,
703 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
705 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
707 gtk_widget_unparent (combo_box->priv->cell_view);
708 GTK_BIN (container)->child = NULL;
709 gtk_widget_queue_resize (GTK_WIDGET (container));
712 gtk_widget_set_parent (widget, GTK_WIDGET (container));
713 GTK_BIN (container)->child = widget;
715 if (combo_box->priv->cell_view &&
716 widget != combo_box->priv->cell_view)
718 /* since the cell_view was unparented, it's gone now */
719 combo_box->priv->cell_view = NULL;
721 if (!combo_box->priv->tree_view && combo_box->priv->separator)
723 gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
724 combo_box->priv->separator);
725 combo_box->priv->separator = NULL;
727 gtk_widget_queue_resize (GTK_WIDGET (container));
729 else if (combo_box->priv->cell_view_frame)
731 gtk_widget_unparent (combo_box->priv->cell_view_frame);
732 combo_box->priv->cell_view_frame = NULL;
738 gtk_combo_box_remove (GtkContainer *container,
741 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
742 gboolean appears_as_list;
744 gtk_widget_unparent (widget);
745 GTK_BIN (container)->child = NULL;
747 if (combo_box->priv->destroying)
750 gtk_widget_queue_resize (GTK_WIDGET (container));
752 if (!combo_box->priv->tree_view)
753 appears_as_list = FALSE;
755 appears_as_list = TRUE;
758 gtk_combo_box_list_destroy (combo_box);
759 else if (GTK_IS_MENU (combo_box->priv->popup_widget))
761 gtk_combo_box_menu_destroy (combo_box);
762 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
763 combo_box->priv->popup_widget = NULL;
766 if (!combo_box->priv->cell_view)
768 combo_box->priv->cell_view = gtk_cell_view_new ();
769 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
770 GTK_BIN (container)->child = combo_box->priv->cell_view;
772 gtk_widget_show (combo_box->priv->cell_view);
773 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
774 combo_box->priv->model);
775 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
780 gtk_combo_box_list_setup (combo_box);
782 gtk_combo_box_menu_setup (combo_box, TRUE);
784 gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
787 static ComboCellInfo *
788 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
789 GtkCellRenderer *cell)
793 for (i = combo_box->priv->cells; i; i = i->next)
795 ComboCellInfo *info = (ComboCellInfo *)i->data;
797 if (info && info->cell == cell)
805 gtk_combo_box_menu_show (GtkWidget *menu,
808 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
810 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
812 combo_box->priv->popup_in_progress = FALSE;
816 gtk_combo_box_menu_hide (GtkWidget *menu,
819 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
821 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
826 gtk_combo_box_detacher (GtkWidget *widget,
829 GtkComboBox *combo_box;
831 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
833 combo_box = GTK_COMBO_BOX (widget);
834 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
836 g_signal_handlers_disconnect_by_func (menu,
837 gtk_combo_box_menu_show,
839 g_signal_handlers_disconnect_by_func (menu,
840 gtk_combo_box_menu_hide,
843 combo_box->priv->popup_widget = NULL;
847 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
850 if (GTK_IS_MENU (combo_box->priv->popup_widget))
852 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
853 combo_box->priv->popup_widget = NULL;
855 else if (combo_box->priv->popup_widget)
857 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
858 combo_box->priv->popup_widget);
859 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
860 combo_box->priv->popup_widget = NULL;
863 if (GTK_IS_MENU (popup))
865 if (combo_box->priv->popup_window)
867 gtk_widget_destroy (combo_box->priv->popup_window);
868 combo_box->priv->popup_window = NULL;
869 combo_box->priv->popup_frame = NULL;
872 combo_box->priv->popup_widget = popup;
874 g_signal_connect (popup, "show",
875 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
876 g_signal_connect (popup, "hide",
877 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
879 gtk_menu_attach_to_widget (GTK_MENU (popup),
880 GTK_WIDGET (combo_box),
881 gtk_combo_box_detacher);
885 if (!combo_box->priv->popup_window)
887 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
888 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
889 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
890 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
892 combo_box->priv->popup_frame = gtk_frame_new (NULL);
893 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
894 GTK_SHADOW_ETCHED_IN);
895 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
896 combo_box->priv->popup_frame);
898 gtk_widget_show (combo_box->priv->popup_frame);
901 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
903 gtk_widget_show (popup);
904 g_object_ref (G_OBJECT (popup));
905 combo_box->priv->popup_widget = popup;
910 gtk_combo_box_menu_position_below (GtkMenu *menu,
919 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
921 /* FIXME: is using the size request here broken? */
922 child = GTK_BIN (combo_box)->child;
924 gdk_window_get_origin (child->window, &sx, &sy);
926 gtk_widget_size_request (GTK_WIDGET (menu), &req);
928 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
931 *x = sx + child->allocation.width - req.width;
932 *y = sy + child->allocation.height;
934 if (GTK_WIDGET_NO_WINDOW (child))
936 *x += child->allocation.x;
937 *y += child->allocation.y;
944 gtk_combo_box_menu_position_over (GtkMenu *menu,
950 GtkComboBox *combo_box;
954 GtkRequisition requisition;
961 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
963 combo_box = GTK_COMBO_BOX (user_data);
964 widget = GTK_WIDGET (combo_box);
966 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
967 menu_width = requisition.width;
969 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
970 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
972 menu_xpos += widget->allocation.x;
973 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
977 gtk_widget_get_child_requisition (active, &requisition);
978 menu_ypos -= requisition.height / 2;
981 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
984 child = children->data;
989 if (GTK_WIDGET_VISIBLE (child))
991 gtk_widget_get_child_requisition (child, &requisition);
992 menu_ypos -= requisition.height;
995 children = children->next;
998 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
999 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1001 /* Clamp the position on screen */
1002 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1006 else if ((menu_xpos + menu_width) > screen_width)
1007 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1016 gtk_combo_box_menu_position (GtkMenu *menu,
1022 GtkComboBox *combo_box;
1023 GtkWidget *menu_item;
1025 combo_box = GTK_COMBO_BOX (user_data);
1027 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
1028 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1031 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1033 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
1036 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1042 gtk_combo_box_list_position (GtkComboBox *combo_box,
1051 GdkRectangle monitor;
1052 GtkRequisition popup_req;
1054 sample = GTK_BIN (combo_box)->child;
1056 *width = sample->allocation.width;
1057 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
1058 *height = popup_req.height;
1060 gdk_window_get_origin (sample->window, x, y);
1062 if (combo_box->priv->cell_view_frame)
1064 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1065 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1066 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1067 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1070 if (GTK_WIDGET_NO_WINDOW (sample))
1072 *x += sample->allocation.x;
1073 *y += sample->allocation.y;
1076 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1077 monitor_num = gdk_screen_get_monitor_at_window (screen,
1078 GTK_WIDGET (combo_box)->window);
1079 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1083 else if (*x + *width > monitor.x + monitor.width)
1084 *x = monitor.x + monitor.width - *width;
1086 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1087 *y += sample->allocation.height;
1093 * gtk_combo_box_popup:
1094 * @combo_box: a #GtkComboBox
1096 * Pops up the menu or dropdown list of @combo_box.
1098 * This function is mostly intended for use by accessibility technologies;
1099 * applications should have little use for it.
1104 gtk_combo_box_popup (GtkComboBox *combo_box)
1106 gint x, y, width, height;
1108 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1110 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1113 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1115 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1116 combo_box->priv->active_item);
1118 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1120 gtk_combo_box_menu_position, combo_box,
1125 gtk_widget_show_all (combo_box->priv->popup_frame);
1126 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1128 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1129 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1132 gtk_widget_show (combo_box->priv->popup_window);
1134 gtk_widget_grab_focus (combo_box->priv->popup_window);
1135 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1138 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1140 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1141 FALSE, GDK_CURRENT_TIME);
1142 gtk_widget_grab_focus (combo_box->priv->tree_view);
1145 gtk_grab_add (combo_box->priv->popup_window);
1146 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1147 GDK_BUTTON_PRESS_MASK |
1148 GDK_BUTTON_RELEASE_MASK |
1149 GDK_POINTER_MOTION_MASK,
1150 NULL, NULL, GDK_CURRENT_TIME);
1152 gtk_grab_add (combo_box->priv->tree_view);
1156 * gtk_combo_box_popdown:
1157 * @combo_box: a #GtkComboBox
1159 * Hides the menu or dropdown list of @combo_box.
1161 * This function is mostly intended for use by accessibility technologies;
1162 * applications should have little use for it.
1167 gtk_combo_box_popdown (GtkComboBox *combo_box)
1169 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1171 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1173 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1177 gtk_combo_box_list_remove_grabs (combo_box);
1178 gtk_widget_hide_all (combo_box->priv->popup_window);
1179 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1184 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1190 if (combo_box->priv->cell_view)
1191 gtk_widget_style_get (combo_box->priv->cell_view,
1192 "focus-line-width", &padding,
1197 /* add some pixels for good measure */
1198 padding += BONUS_PADDING;
1200 if (combo_box->priv->cell_view)
1201 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1206 return req.width + padding;
1210 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1216 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1219 combo_box->priv->width = 0;
1221 path = gtk_tree_path_new_from_indices (0, -1);
1223 if (combo_box->priv->cell_view)
1224 gtk_widget_style_get (combo_box->priv->cell_view,
1225 "focus-line-width", &padding,
1230 /* add some pixels for good measure */
1231 padding += BONUS_PADDING;
1237 if (combo_box->priv->cell_view)
1238 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1243 combo_box->priv->width = MAX (combo_box->priv->width,
1244 req.width + padding);
1246 gtk_tree_path_next (path);
1248 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1250 gtk_tree_path_free (path);
1254 gtk_combo_box_size_request (GtkWidget *widget,
1255 GtkRequisition *requisition)
1258 GtkRequisition bin_req;
1260 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1263 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1264 gtk_combo_box_remeasure (combo_box);
1265 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1267 if (!combo_box->priv->tree_view)
1271 if (combo_box->priv->cell_view)
1273 GtkRequisition button_req, sep_req, arrow_req;
1274 gint border_width, xthickness, ythickness;
1276 gtk_widget_size_request (combo_box->priv->button, &button_req);
1277 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1278 xthickness = combo_box->priv->button->style->xthickness;
1279 ythickness = combo_box->priv->button->style->ythickness;
1281 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1283 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1284 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1286 height = MAX (sep_req.height, arrow_req.height);
1287 height = MAX (height, bin_req.height);
1289 width = bin_req.width + sep_req.width + arrow_req.width;
1291 height += border_width + 1 + ythickness * 2 + 4;
1292 width += border_width + 1 + xthickness * 2 + 4;
1294 requisition->width = width;
1295 requisition->height = height;
1299 GtkRequisition but_req;
1301 gtk_widget_size_request (combo_box->priv->button, &but_req);
1303 requisition->width = bin_req.width + but_req.width;
1304 requisition->height = MAX (bin_req.height, but_req.height);
1310 GtkRequisition button_req, frame_req;
1312 /* sample + frame */
1313 *requisition = bin_req;
1315 if (combo_box->priv->cell_view_frame)
1317 gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
1318 requisition->width += 2 *
1319 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1320 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1321 requisition->height += 2 *
1322 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1323 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1327 gtk_widget_size_request (combo_box->priv->button, &button_req);
1329 requisition->height = MAX (requisition->height, button_req.height);
1330 requisition->width += button_req.width;
1335 gtk_combo_box_size_allocate (GtkWidget *widget,
1336 GtkAllocation *allocation)
1338 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1339 GtkAllocation child;
1341 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1343 widget->allocation = *allocation;
1345 if (!combo_box->priv->tree_view)
1347 if (combo_box->priv->cell_view)
1349 gint border_width, xthickness, ythickness;
1353 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1355 /* set some things ready */
1356 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1357 xthickness = combo_box->priv->button->style->xthickness;
1358 ythickness = combo_box->priv->button->style->ythickness;
1360 child.x = allocation->x + border_width + 1 + xthickness + 2;
1361 child.y = allocation->y + border_width + 1 + ythickness + 2;
1363 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1365 /* handle the children */
1366 gtk_widget_size_request (combo_box->priv->arrow, &req);
1367 child.width = req.width;
1368 child.height = allocation->height - 2 * (child.y - allocation->y);
1370 child.x += width - req.width;
1371 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1373 child.x += req.width;
1374 gtk_widget_size_request (combo_box->priv->separator, &req);
1375 child.width = req.width;
1377 child.x -= req.width;
1378 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1382 child.x += req.width;
1383 child.width = allocation->x + allocation->width
1384 - (border_width + 1 + xthickness + 2) - child.x;
1388 child.width = child.x;
1389 child.x = allocation->x + border_width + 1 + xthickness + 2;
1390 child.width -= child.x;
1393 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1397 gtk_widget_size_request (combo_box->priv->button, &req);
1399 child.x = allocation->x;
1401 child.x = allocation->x + allocation->width - req.width;
1402 child.y = allocation->y;
1403 child.width = req.width;
1404 child.height = allocation->height;
1405 gtk_widget_size_allocate (combo_box->priv->button, &child);
1408 child.x = allocation->x + req.width;
1410 child.x = allocation->x;
1411 child.y = allocation->y;
1412 child.width = allocation->width - req.width;
1413 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1421 gtk_widget_size_request (combo_box->priv->button, &req);
1423 child.x = allocation->x;
1425 child.x = allocation->x + allocation->width - req.width;
1426 child.y = allocation->y;
1427 child.width = req.width;
1428 child.height = allocation->height;
1429 gtk_widget_size_allocate (combo_box->priv->button, &child);
1433 child.x = allocation->x + req.width;
1435 child.x = allocation->x;
1436 child.y = allocation->y;
1437 child.width = allocation->width - req.width;
1438 child.height = allocation->height;
1440 if (combo_box->priv->cell_view_frame)
1442 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1446 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1447 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1449 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1450 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1451 child.width -= 2 * (
1452 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1453 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1454 child.height -= 2 * (
1455 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1456 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1459 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1464 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1466 if (combo_box->priv->model)
1468 g_signal_handler_disconnect (combo_box->priv->model,
1469 combo_box->priv->inserted_id);
1470 g_signal_handler_disconnect (combo_box->priv->model,
1471 combo_box->priv->deleted_id);
1472 g_signal_handler_disconnect (combo_box->priv->model,
1473 combo_box->priv->reordered_id);
1474 g_signal_handler_disconnect (combo_box->priv->model,
1475 combo_box->priv->changed_id);
1479 if (!combo_box->priv->tree_view)
1481 if (combo_box->priv->popup_widget)
1482 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1483 (GtkCallback)gtk_widget_destroy, NULL);
1486 if (combo_box->priv->model)
1488 g_object_unref (G_OBJECT (combo_box->priv->model));
1489 combo_box->priv->model = NULL;
1494 gtk_combo_box_forall (GtkContainer *container,
1495 gboolean include_internals,
1496 GtkCallback callback,
1497 gpointer callback_data)
1499 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1501 if (include_internals)
1503 if (combo_box->priv->button)
1504 (* callback) (combo_box->priv->button, callback_data);
1505 if (combo_box->priv->box)
1506 (* callback) (combo_box->priv->box, callback_data);
1507 if (combo_box->priv->separator)
1508 (* callback) (combo_box->priv->separator, callback_data);
1509 if (combo_box->priv->arrow)
1510 (* callback) (combo_box->priv->arrow, callback_data);
1511 if (combo_box->priv->cell_view_frame)
1512 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1515 if (GTK_BIN (container)->child)
1516 (* callback) (GTK_BIN (container)->child, callback_data);
1520 gtk_combo_box_expose_event (GtkWidget *widget,
1521 GdkEventExpose *event)
1523 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1525 if (!combo_box->priv->tree_view)
1527 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1528 combo_box->priv->button, event);
1532 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1533 combo_box->priv->button, event);
1535 if (combo_box->priv->cell_view_frame)
1536 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1537 combo_box->priv->cell_view_frame, event);
1540 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1541 GTK_BIN (widget)->child, event);
1547 gtk_combo_box_scroll_event (GtkWidget *widget,
1548 GdkEventScroll *event)
1550 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1554 index = gtk_combo_box_get_active (combo_box);
1558 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1560 if (event->direction == GDK_SCROLL_UP)
1565 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1576 cell_view_sync_cells (GtkComboBox *combo_box,
1577 GtkCellView *cell_view)
1581 for (k = combo_box->priv->cells; k; k = k->next)
1584 ComboCellInfo *info = (ComboCellInfo *)k->data;
1586 if (info->pack == GTK_PACK_START)
1587 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1588 info->cell, info->expand);
1589 else if (info->pack == GTK_PACK_END)
1590 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1591 info->cell, info->expand);
1593 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1595 info->func, info->func_data, NULL);
1597 for (j = info->attributes; j; j = j->next->next)
1599 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1602 GPOINTER_TO_INT (j->next->data));
1608 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1609 gboolean add_children)
1613 if (combo_box->priv->cell_view)
1615 combo_box->priv->button = gtk_toggle_button_new ();
1616 g_signal_connect (combo_box->priv->button, "toggled",
1617 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1618 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1619 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1620 gtk_widget_set_parent (combo_box->priv->button,
1621 GTK_BIN (combo_box)->child->parent);
1623 combo_box->priv->box = gtk_hbox_new (FALSE, 0);
1624 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1625 combo_box->priv->box);
1627 combo_box->priv->separator = gtk_vseparator_new ();
1628 gtk_container_add (GTK_CONTAINER (combo_box->priv->box),
1629 combo_box->priv->separator);
1631 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1632 gtk_container_add (GTK_CONTAINER (combo_box->priv->box),
1633 combo_box->priv->arrow);
1635 gtk_widget_show_all (combo_box->priv->button);
1639 combo_box->priv->button = gtk_toggle_button_new ();
1640 g_signal_connect (combo_box->priv->button, "toggled",
1641 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1642 g_signal_connect_after (combo_box, "key_press_event",
1643 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1644 gtk_widget_set_parent (combo_box->priv->button,
1645 GTK_BIN (combo_box)->child->parent);
1647 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1648 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1649 combo_box->priv->arrow);
1650 gtk_widget_show_all (combo_box->priv->button);
1653 g_signal_connect (combo_box->priv->button, "button_press_event",
1654 G_CALLBACK (gtk_combo_box_menu_button_press),
1656 g_signal_connect (combo_box->priv->button, "state_changed",
1657 G_CALLBACK (gtk_combo_box_menu_state_changed),
1660 /* create our funky menu */
1661 menu = gtk_menu_new ();
1662 g_signal_connect (menu, "key_press_event",
1663 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1664 gtk_combo_box_set_popup_widget (combo_box, menu);
1668 gtk_combo_box_menu_fill (combo_box);
1673 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1679 if (!combo_box->priv->model)
1682 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1683 menu = combo_box->priv->popup_widget;
1685 for (i = 0; i < items; i++)
1689 path = gtk_tree_path_new_from_indices (i, -1);
1690 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1692 g_signal_connect (tmp, "activate",
1693 G_CALLBACK (gtk_combo_box_menu_item_activate),
1696 cell_view_sync_cells (combo_box,
1697 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1699 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1701 if (combo_box->priv->wrap_width)
1702 gtk_combo_box_relayout_item (combo_box, i);
1704 gtk_widget_show (tmp);
1706 gtk_tree_path_free (path);
1711 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1713 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1714 G_SIGNAL_MATCH_DATA,
1716 gtk_combo_box_menu_button_press, NULL);
1717 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1718 G_SIGNAL_MATCH_DATA,
1720 gtk_combo_box_menu_state_changed, NULL);
1722 /* unparent will remove our latest ref */
1723 gtk_widget_unparent (combo_box->priv->button);
1725 combo_box->priv->box = NULL;
1726 combo_box->priv->button = NULL;
1727 combo_box->priv->arrow = NULL;
1728 combo_box->priv->separator = NULL;
1730 /* changing the popup window will unref the menu and the children */
1738 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1745 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1749 if (combo_box->priv->col_column == -1)
1752 gtk_tree_model_get (combo_box->priv->model, &iter,
1753 combo_box->priv->col_column, cols,
1759 if (combo_box->priv->row_column == -1)
1762 gtk_tree_model_get (combo_box->priv->model, &iter,
1763 combo_box->priv->row_column, rows,
1769 menu_occupied (GtkMenu *menu,
1773 guint bottom_attach)
1777 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1778 g_return_val_if_fail (left_attach < right_attach, TRUE);
1779 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1781 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1784 gboolean h_intersect = FALSE;
1785 gboolean v_intersect = FALSE;
1787 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1790 "bottom_attach", &b,
1794 /* look if this item intersects with the given coordinates */
1795 h_intersect = left_attach <= l && l <= right_attach;
1796 h_intersect &= left_attach <= r && r <= right_attach;
1798 v_intersect = top_attach <= t && t <= bottom_attach;
1799 v_intersect &= top_attach <= b && b <= bottom_attach;
1801 if (h_intersect && v_intersect)
1809 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1812 gint current_col = 0, current_row = 0;
1818 menu = combo_box->priv->popup_widget;
1819 if (!GTK_IS_MENU_SHELL (menu))
1822 list = gtk_container_get_children (GTK_CONTAINER (menu));
1823 item = g_list_nth_data (list, index);
1826 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1828 /* look for a good spot */
1831 if (current_col + cols > combo_box->priv->wrap_width)
1837 if (!menu_occupied (GTK_MENU (menu),
1838 current_col, current_col + cols,
1839 current_row, current_row + rows))
1845 /* set attach props */
1846 gtk_menu_attach (GTK_MENU (menu), item,
1847 current_col, current_col + cols,
1848 current_row, current_row + rows);
1852 gtk_combo_box_relayout (GtkComboBox *combo_box)
1857 menu = combo_box->priv->popup_widget;
1859 /* do nothing unless we are in menu style and realized */
1860 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
1863 /* get rid of all children */
1864 list = gtk_container_get_children (GTK_CONTAINER (menu));
1866 for (j = g_list_last (list); j; j = j->prev)
1867 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1872 gtk_combo_box_menu_fill (combo_box);
1877 gtk_combo_box_menu_button_press (GtkWidget *widget,
1878 GdkEventButton *event,
1881 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1883 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1886 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1888 combo_box->priv->popup_in_progress = TRUE;
1890 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1891 combo_box->priv->active_item);
1893 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1895 gtk_combo_box_menu_position, combo_box,
1896 event->button, event->time);
1905 gtk_combo_box_menu_state_changed (GtkWidget *widget,
1906 GtkStateType previous,
1909 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1911 if (combo_box->priv->cell_view)
1913 gtk_widget_set_state (combo_box->priv->cell_view,
1914 GTK_WIDGET_STATE (widget));
1916 gtk_widget_queue_draw (combo_box->priv->cell_view);
1921 gtk_combo_box_menu_item_activate (GtkWidget *item,
1926 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1928 menu = combo_box->priv->popup_widget;
1929 g_return_if_fail (GTK_IS_MENU (menu));
1931 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1933 gtk_combo_box_set_active (combo_box, index);
1937 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1942 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1943 gint index = gtk_tree_path_get_indices (path)[0];
1945 if (combo_box->priv->active_item >= index)
1946 combo_box->priv->active_item++;
1948 if (!combo_box->priv->tree_view)
1949 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1953 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1957 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1958 gint index = gtk_tree_path_get_indices (path)[0];
1960 if (!combo_box->priv->tree_view)
1961 gtk_combo_box_menu_row_deleted (model, path, user_data);
1963 if (index == combo_box->priv->active_item)
1965 gint items = gtk_tree_model_iter_n_children (model, NULL);
1968 gtk_combo_box_set_active_internal (combo_box, -1);
1969 else if (index == items)
1970 gtk_combo_box_set_active_internal (combo_box, index - 1);
1972 gtk_combo_box_set_active_internal (combo_box, index);
1974 else if (combo_box->priv->active_item > index)
1975 combo_box->priv->active_item--;
1979 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1985 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1986 gint items = gtk_tree_model_iter_n_children (model, NULL);
1989 for (i = 0; i < items; i++)
1990 if (new_order[i] == combo_box->priv->active_item)
1992 combo_box->priv->active_item = i;
1996 if (!combo_box->priv->tree_view)
1997 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2001 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2006 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2007 gint index = gtk_tree_path_get_indices (path)[0];
2009 if (index == combo_box->priv->active_item &&
2010 combo_box->priv->cell_view)
2011 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2013 if (combo_box->priv->tree_view)
2014 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2016 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2021 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2028 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2030 if (!combo_box->priv->popup_widget)
2033 menu = combo_box->priv->popup_widget;
2034 g_return_if_fail (GTK_IS_MENU (menu));
2036 item = gtk_cell_view_menu_item_new_from_model (model, path);
2037 g_signal_connect (item, "activate",
2038 G_CALLBACK (gtk_combo_box_menu_item_activate),
2041 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2043 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2044 gtk_tree_path_get_indices (path)[0]);
2045 gtk_widget_show (item);
2049 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2056 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2058 if (!combo_box->priv->popup_widget)
2061 index = gtk_tree_path_get_indices (path)[0];
2063 menu = combo_box->priv->popup_widget;
2064 g_return_if_fail (GTK_IS_MENU (menu));
2066 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2067 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2069 gtk_container_remove (GTK_CONTAINER (menu), item);
2073 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2079 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2081 gtk_combo_box_relayout (combo_box);
2085 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2090 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2093 if (!combo_box->priv->popup_widget)
2096 if (combo_box->priv->wrap_width)
2097 gtk_combo_box_relayout_item (combo_box,
2098 gtk_tree_path_get_indices (path)[0]);
2100 width = gtk_combo_box_calc_requested_width (combo_box, path);
2102 if (width > combo_box->priv->width)
2104 if (combo_box->priv->cell_view)
2106 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2107 gtk_widget_queue_resize (combo_box->priv->cell_view);
2109 combo_box->priv->width = width;
2118 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2121 GtkTreeSelection *sel;
2123 combo_box->priv->button = gtk_toggle_button_new ();
2124 gtk_widget_set_parent (combo_box->priv->button,
2125 GTK_BIN (combo_box)->child->parent);
2126 g_signal_connect (combo_box->priv->button, "button_press_event",
2127 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2128 g_signal_connect (combo_box->priv->button, "toggled",
2129 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2130 g_signal_connect_after (combo_box, "key_press_event",
2131 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2133 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2134 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2135 combo_box->priv->arrow);
2136 combo_box->priv->separator = NULL;
2137 gtk_widget_show_all (combo_box->priv->button);
2139 if (combo_box->priv->cell_view)
2141 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2142 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2143 GTK_BIN (combo_box)->child->parent);
2144 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2147 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2148 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2150 gtk_widget_show (combo_box->priv->cell_view_frame);
2153 combo_box->priv->tree_view = gtk_tree_view_new ();
2154 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2155 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2156 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2158 if (combo_box->priv->model)
2159 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2160 combo_box->priv->model);
2162 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2163 G_CALLBACK (gtk_combo_box_list_button_pressed),
2165 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2166 G_CALLBACK (gtk_combo_box_list_button_released),
2168 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2169 G_CALLBACK (gtk_combo_box_list_key_press),
2172 combo_box->priv->column = gtk_tree_view_column_new ();
2173 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2174 combo_box->priv->column);
2177 for (i = combo_box->priv->cells; i; i = i->next)
2180 ComboCellInfo *info = (ComboCellInfo *)i->data;
2182 if (info->pack == GTK_PACK_START)
2183 gtk_tree_view_column_pack_start (combo_box->priv->column,
2184 info->cell, info->expand);
2185 else if (info->pack == GTK_PACK_END)
2186 gtk_tree_view_column_pack_end (combo_box->priv->column,
2187 info->cell, info->expand);
2189 for (j = info->attributes; j; j = j->next->next)
2191 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2194 GPOINTER_TO_INT (j->next->data));
2198 if (combo_box->priv->active_item != -1)
2202 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2205 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2207 gtk_tree_path_free (path);
2211 /* set sample/popup widgets */
2212 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2214 gtk_widget_show (combo_box->priv->tree_view);
2218 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2220 /* disconnect signals */
2221 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2222 G_SIGNAL_MATCH_DATA,
2223 0, 0, NULL, NULL, combo_box);
2224 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2225 G_SIGNAL_MATCH_DATA,
2227 gtk_combo_box_list_button_pressed,
2230 /* destroy things (unparent will kill the latest ref from us)
2231 * last unref on button will destroy the arrow
2233 gtk_widget_unparent (combo_box->priv->button);
2234 combo_box->priv->button = NULL;
2235 combo_box->priv->arrow = NULL;
2237 if (combo_box->priv->cell_view)
2239 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2240 "background_set", FALSE,
2244 if (combo_box->priv->cell_view_frame)
2246 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2247 combo_box->priv->cell_view_frame = NULL;
2250 gtk_widget_destroy (combo_box->priv->tree_view);
2252 combo_box->priv->tree_view = NULL;
2253 combo_box->priv->popup_widget = NULL;
2258 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2260 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2261 gtk_grab_remove (combo_box->priv->tree_view);
2263 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2265 gtk_grab_remove (combo_box->priv->popup_window);
2266 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2267 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2272 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2273 GdkEventButton *event,
2276 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2278 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2280 if (ewidget == combo_box->priv->tree_view)
2283 if ((ewidget != combo_box->priv->button) ||
2284 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2287 gtk_combo_box_popup (combo_box);
2289 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2292 combo_box->priv->popup_in_progress = TRUE;
2298 gtk_combo_box_list_button_released (GtkWidget *widget,
2299 GdkEventButton *event,
2303 GtkTreePath *path = NULL;
2305 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2307 gboolean popup_in_progress = FALSE;
2309 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2311 if (combo_box->priv->popup_in_progress)
2313 popup_in_progress = TRUE;
2314 combo_box->priv->popup_in_progress = FALSE;
2317 if (ewidget != combo_box->priv->tree_view)
2319 if (ewidget == combo_box->priv->button &&
2320 !popup_in_progress &&
2321 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2323 gtk_combo_box_popdown (combo_box);
2327 /* released outside treeview */
2328 if (ewidget != combo_box->priv->button)
2330 gtk_combo_box_popdown (combo_box);
2338 /* select something cool */
2339 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2345 return TRUE; /* clicked outside window? */
2347 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2348 gtk_combo_box_popdown (combo_box);
2350 gtk_tree_path_free (path);
2356 gtk_combo_box_key_press (GtkWidget *widget,
2360 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2361 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2362 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2363 gint index = gtk_combo_box_get_active (combo_box);
2366 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2367 state == GDK_MOD1_MASK)
2369 gtk_combo_box_popup (combo_box);
2374 switch (event->keyval)
2378 new_index = index + 1;
2382 new_index = index - 1;
2385 case GDK_KP_Page_Up:
2391 case GDK_KP_Page_Down:
2394 new_index = items - 1;
2400 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2406 gtk_combo_box_menu_key_press (GtkWidget *widget,
2410 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2411 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2413 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2414 state == GDK_MOD1_MASK)
2416 gtk_combo_box_popdown (combo_box);
2425 gtk_combo_box_list_key_press (GtkWidget *widget,
2429 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2430 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2432 if (event->keyval == GDK_Escape ||
2433 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2434 state == GDK_MOD1_MASK))
2436 /* reset active item -- this is incredibly lame and ugly */
2437 gtk_combo_box_set_active (combo_box,
2438 gtk_combo_box_get_active (combo_box));
2440 gtk_combo_box_popdown (combo_box);
2445 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2446 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2450 GtkTreeModel *model = NULL;
2451 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2453 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2458 path = gtk_tree_model_get_path (model, &iter);
2461 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2462 gtk_tree_path_free (path);
2466 gtk_combo_box_popdown (combo_box);
2475 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2480 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2483 width = gtk_combo_box_calc_requested_width (combo_box, path);
2485 if (width > combo_box->priv->width)
2487 if (combo_box->priv->cell_view)
2489 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2490 gtk_widget_queue_resize (combo_box->priv->cell_view);
2492 combo_box->priv->width = width;
2497 * GtkCellLayout implementation
2500 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2501 GtkCellRenderer *cell,
2504 ComboCellInfo *info;
2505 GtkComboBox *combo_box;
2508 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2509 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2511 combo_box = GTK_COMBO_BOX (layout);
2513 g_object_ref (G_OBJECT (cell));
2514 gtk_object_sink (GTK_OBJECT (cell));
2516 info = g_new0 (ComboCellInfo, 1);
2518 info->expand = expand;
2519 info->pack = GTK_PACK_START;
2521 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2523 if (combo_box->priv->cell_view)
2524 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2527 if (combo_box->priv->column)
2528 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2530 menu = combo_box->priv->popup_widget;
2531 if (GTK_IS_MENU (menu))
2535 list = gtk_container_get_children (GTK_CONTAINER (menu));
2536 for (i = list; i; i = i->next)
2540 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2541 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2543 view = GTK_CELL_VIEW (i->data);
2545 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2552 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2553 GtkCellRenderer *cell,
2556 ComboCellInfo *info;
2557 GtkComboBox *combo_box;
2560 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2561 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2563 combo_box = GTK_COMBO_BOX (layout);
2565 g_object_ref (G_OBJECT (cell));
2566 gtk_object_sink (GTK_OBJECT (cell));
2568 info = g_new0 (ComboCellInfo, 1);
2570 info->expand = expand;
2571 info->pack = GTK_PACK_END;
2573 if (combo_box->priv->cell_view)
2574 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2577 if (combo_box->priv->column)
2578 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2580 menu = combo_box->priv->popup_widget;
2581 if (GTK_IS_MENU (menu))
2585 list = gtk_container_get_children (GTK_CONTAINER (menu));
2586 for (i = list; i; i = i->next)
2590 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2591 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2593 view = GTK_CELL_VIEW (i->data);
2595 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2602 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2605 GtkComboBox *combo_box;
2608 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2610 combo_box = GTK_COMBO_BOX (layout);
2612 if (combo_box->priv->cell_view)
2613 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2615 if (combo_box->priv->column)
2616 gtk_tree_view_column_clear (combo_box->priv->column);
2618 for (i = combo_box->priv->cells; i; i = i->next)
2620 ComboCellInfo *info = (ComboCellInfo *)i->data;
2622 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2623 g_object_unref (G_OBJECT (info->cell));
2627 g_slist_free (combo_box->priv->cells);
2628 combo_box->priv->cells = NULL;
2630 menu = combo_box->priv->popup_widget;
2631 if (GTK_IS_MENU (menu))
2635 list = gtk_container_get_children (GTK_CONTAINER (menu));
2636 for (i = list; i; i = i->next)
2640 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2641 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2643 view = GTK_CELL_VIEW (i->data);
2645 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2652 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2653 GtkCellRenderer *cell,
2654 const gchar *attribute,
2657 ComboCellInfo *info;
2658 GtkComboBox *combo_box;
2661 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2662 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2664 combo_box = GTK_COMBO_BOX (layout);
2666 info = gtk_combo_box_get_cell_info (combo_box, cell);
2668 info->attributes = g_slist_prepend (info->attributes,
2669 GINT_TO_POINTER (column));
2670 info->attributes = g_slist_prepend (info->attributes,
2671 g_strdup (attribute));
2673 if (combo_box->priv->cell_view)
2674 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2675 cell, attribute, column);
2677 if (combo_box->priv->column)
2678 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2679 cell, attribute, column);
2681 menu = combo_box->priv->popup_widget;
2682 if (GTK_IS_MENU (menu))
2686 list = gtk_container_get_children (GTK_CONTAINER (menu));
2687 for (i = list; i; i = i->next)
2691 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2692 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2694 view = GTK_CELL_VIEW (i->data);
2696 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2702 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2706 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2707 GtkCellRenderer *cell,
2708 GtkCellLayoutDataFunc func,
2710 GDestroyNotify destroy)
2712 ComboCellInfo *info;
2713 GtkComboBox *combo_box;
2716 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2718 combo_box = GTK_COMBO_BOX (layout);
2720 info = gtk_combo_box_get_cell_info (combo_box, cell);
2721 g_return_if_fail (info != NULL);
2725 GDestroyNotify d = info->destroy;
2727 info->destroy = NULL;
2728 d (info->func_data);
2732 info->func_data = func_data;
2733 info->destroy = destroy;
2735 if (combo_box->priv->cell_view)
2736 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2738 if (combo_box->priv->column)
2739 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2741 menu = combo_box->priv->popup_widget;
2742 if (GTK_IS_MENU (menu))
2746 list = gtk_container_get_children (GTK_CONTAINER (menu));
2747 for (i = list; i; i = i->next)
2751 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2752 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2754 view = GTK_CELL_VIEW (i->data);
2756 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2757 func, func_data, NULL);
2762 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2766 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2767 GtkCellRenderer *cell)
2769 ComboCellInfo *info;
2770 GtkComboBox *combo_box;
2774 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2775 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2777 combo_box = GTK_COMBO_BOX (layout);
2779 info = gtk_combo_box_get_cell_info (combo_box, cell);
2780 g_return_if_fail (info != NULL);
2782 list = info->attributes;
2783 while (list && list->next)
2785 g_free (list->data);
2786 list = list->next->next;
2788 g_slist_free (info->attributes);
2789 info->attributes = NULL;
2791 if (combo_box->priv->cell_view)
2792 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2794 if (combo_box->priv->column)
2795 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2797 menu = combo_box->priv->popup_widget;
2798 if (GTK_IS_MENU (menu))
2802 list = gtk_container_get_children (GTK_CONTAINER (menu));
2803 for (i = list; i; i = i->next)
2807 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2808 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2810 view = GTK_CELL_VIEW (i->data);
2812 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2817 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2821 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2822 GtkCellRenderer *cell,
2825 ComboCellInfo *info;
2826 GtkComboBox *combo_box;
2830 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2831 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2833 combo_box = GTK_COMBO_BOX (layout);
2835 info = gtk_combo_box_get_cell_info (combo_box, cell);
2837 g_return_if_fail (info != NULL);
2838 g_return_if_fail (position >= 0);
2840 link = g_slist_find (combo_box->priv->cells, info);
2842 g_return_if_fail (link != NULL);
2844 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2845 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2848 if (combo_box->priv->cell_view)
2849 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2852 if (combo_box->priv->column)
2853 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2856 menu = combo_box->priv->popup_widget;
2857 if (GTK_IS_MENU (menu))
2861 list = gtk_container_get_children (GTK_CONTAINER (menu));
2862 for (i = list; i; i = i->next)
2866 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2867 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2869 view = GTK_CELL_VIEW (i->data);
2871 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2876 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2884 * gtk_combo_box_new:
2886 * Creates a new empty #GtkComboBox.
2888 * Return value: A new #GtkComboBox.
2893 gtk_combo_box_new (void)
2895 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2899 * gtk_combo_box_new_with_model:
2900 * @model: A #GtkTreeModel.
2902 * Creates a new #GtkComboBox with the model initialized to @model.
2904 * Return value: A new #GtkComboBox.
2909 gtk_combo_box_new_with_model (GtkTreeModel *model)
2911 GtkComboBox *combo_box;
2913 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2915 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2919 return GTK_WIDGET (combo_box);
2923 * gtk_combo_box_set_wrap_width:
2924 * @combo_box: A #GtkComboBox.
2925 * @width: Preferred number of columns.
2927 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2928 * the preferred number of columns when you want to the popup to be layed out
2934 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2937 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2938 g_return_if_fail (width >= 0);
2940 if (width != combo_box->priv->wrap_width)
2942 combo_box->priv->wrap_width = width;
2944 gtk_combo_box_check_appearance (combo_box);
2945 gtk_combo_box_relayout (combo_box);
2947 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2952 * gtk_combo_box_set_row_span_column:
2953 * @combo_box: A #GtkComboBox.
2954 * @row_span: A column in the model passed during construction.
2956 * Sets the column with row span information for @combo_box to be @row_span.
2957 * The row span column contains integers which indicate how many rows
2958 * an item should span.
2963 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2968 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2970 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2971 g_return_if_fail (row_span >= 0 && row_span < col);
2973 if (row_span != combo_box->priv->row_column)
2975 combo_box->priv->row_column = row_span;
2977 gtk_combo_box_relayout (combo_box);
2979 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2984 * gtk_combo_box_set_column_span_column:
2985 * @combo_box: A #GtkComboBox.
2986 * @column_span: A column in the model passed during construction.
2988 * Sets the column with column span information for @combo_box to be
2989 * @column_span. The column span column contains integers which indicate
2990 * how many columns an item should span.
2995 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
3000 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3002 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3003 g_return_if_fail (column_span >= 0 && column_span < col);
3005 if (column_span != combo_box->priv->col_column)
3007 combo_box->priv->col_column = column_span;
3009 gtk_combo_box_relayout (combo_box);
3011 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3016 * gtk_combo_box_get_active:
3017 * @combo_box: A #GtkComboBox.
3019 * Returns the index of the currently active item, or -1 if there's no
3022 * Return value: An integer which is the index of the currently active item, or
3023 * -1 if there's no active item.
3028 gtk_combo_box_get_active (GtkComboBox *combo_box)
3030 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3032 return combo_box->priv->active_item;
3036 * gtk_combo_box_set_active:
3037 * @combo_box: A #GtkComboBox.
3038 * @index: An index in the model passed during construction, or -1 to have
3041 * Sets the active item of @combo_box to be the item at @index.
3046 gtk_combo_box_set_active (GtkComboBox *combo_box,
3049 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3050 /* -1 means "no item selected" */
3051 g_return_if_fail (index >= -1);
3053 if (combo_box->priv->active_item == index)
3056 gtk_combo_box_set_active_internal (combo_box, index);
3060 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3065 combo_box->priv->active_item = index;
3069 if (combo_box->priv->tree_view)
3070 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3073 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3075 if (GTK_IS_MENU (menu))
3076 gtk_menu_set_active (menu, -1);
3079 if (combo_box->priv->cell_view)
3080 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3084 path = gtk_tree_path_new_from_indices (index, -1);
3086 if (combo_box->priv->tree_view)
3087 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3090 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3092 if (GTK_IS_MENU (menu))
3093 gtk_menu_set_active (GTK_MENU (menu), index);
3096 if (combo_box->priv->cell_view)
3097 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3099 gtk_tree_path_free (path);
3102 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3107 * gtk_combo_box_get_active_iter:
3108 * @combo_box: A #GtkComboBox
3109 * @iter: The uninitialized #GtkTreeIter.
3111 * Sets @iter to point to the current active item, if it exists.
3113 * Return value: %TRUE, if @iter was set
3118 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3125 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3127 active = gtk_combo_box_get_active (combo_box);
3131 path = gtk_tree_path_new_from_indices (active, -1);
3132 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3134 gtk_tree_path_free (path);
3140 * gtk_combo_box_set_active_iter:
3141 * @combo_box: A #GtkComboBox
3142 * @iter: The #GtkTreeIter.
3144 * Sets the current active item to be the one referenced by @iter.
3145 * @iter must correspond to a path of depth one.
3150 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3155 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3157 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3158 g_return_if_fail (path != NULL);
3159 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3161 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3162 gtk_tree_path_free (path);
3166 * gtk_combo_box_set_model:
3167 * @combo_box: A #GtkComboBox.
3168 * @model: A #GtkTreeModel.
3170 * Sets the model used by @combo_box to be @model. Will unset a
3171 * previously set model (if applicable).
3176 gtk_combo_box_set_model (GtkComboBox *combo_box,
3177 GtkTreeModel *model)
3179 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3180 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3182 if (model == combo_box->priv->model)
3185 if (combo_box->priv->model)
3186 gtk_combo_box_unset_model (combo_box);
3188 combo_box->priv->model = model;
3189 g_object_ref (G_OBJECT (combo_box->priv->model));
3191 combo_box->priv->inserted_id =
3192 g_signal_connect (combo_box->priv->model, "row_inserted",
3193 G_CALLBACK (gtk_combo_box_model_row_inserted),
3195 combo_box->priv->deleted_id =
3196 g_signal_connect (combo_box->priv->model, "row_deleted",
3197 G_CALLBACK (gtk_combo_box_model_row_deleted),
3199 combo_box->priv->reordered_id =
3200 g_signal_connect (combo_box->priv->model, "rows_reordered",
3201 G_CALLBACK (gtk_combo_box_model_rows_reordered),
3203 combo_box->priv->changed_id =
3204 g_signal_connect (combo_box->priv->model, "row_changed",
3205 G_CALLBACK (gtk_combo_box_model_row_changed),
3208 if (combo_box->priv->tree_view)
3211 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3212 combo_box->priv->model);
3217 if (combo_box->priv->popup_widget)
3218 gtk_combo_box_menu_fill (combo_box);
3222 if (combo_box->priv->cell_view)
3223 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3224 combo_box->priv->model);
3228 * gtk_combo_box_get_model
3229 * @combo_box: A #GtkComboBox.
3231 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3233 * Return value: A #GtkTreeModel which was passed during construction.
3238 gtk_combo_box_get_model (GtkComboBox *combo_box)
3240 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3242 return combo_box->priv->model;
3246 /* convenience API for simple text combos */
3249 * gtk_combo_box_new_text:
3251 * Convenience function which constructs a new text combo box, which is a
3252 * #GtkComboBox just displaying strings. If you use this function to create
3253 * a text combo box, you should only manipulate its data source with the
3254 * following convenience functions: gtk_combo_box_append_text(),
3255 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3256 * gtk_combo_box_remove_text().
3258 * Return value: A new text combo box.
3263 gtk_combo_box_new_text (void)
3265 GtkWidget *combo_box;
3266 GtkCellRenderer *cell;
3267 GtkListStore *store;
3269 store = gtk_list_store_new (1, G_TYPE_STRING);
3271 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3273 cell = gtk_cell_renderer_text_new ();
3274 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3275 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3283 * gtk_combo_box_append_text:
3284 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3287 * Appends @string to the list of strings stored in @combo_box. Note that
3288 * you can only use this function with combo boxes constructed with
3289 * gtk_combo_box_new_text().
3294 gtk_combo_box_append_text (GtkComboBox *combo_box,
3298 GtkListStore *store;
3300 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3301 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3302 g_return_if_fail (text != NULL);
3304 store = GTK_LIST_STORE (combo_box->priv->model);
3306 gtk_list_store_append (store, &iter);
3307 gtk_list_store_set (store, &iter, 0, text, -1);
3311 * gtk_combo_box_insert_text:
3312 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3313 * @position: An index to insert @text.
3316 * Inserts @string at @position in the list of strings stored in @combo_box.
3317 * Note that you can only use this function with combo boxes constructed
3318 * with gtk_combo_box_new_text().
3323 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3328 GtkListStore *store;
3330 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3331 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3332 g_return_if_fail (position >= 0);
3333 g_return_if_fail (text != NULL);
3335 store = GTK_LIST_STORE (combo_box->priv->model);
3337 gtk_list_store_insert (store, &iter, position);
3338 gtk_list_store_set (store, &iter, 0, text, -1);
3342 * gtk_combo_box_prepend_text:
3343 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3346 * Prepends @string to the list of strings stored in @combo_box. Note that
3347 * you can only use this function with combo boxes constructed with
3348 * gtk_combo_box_new_text().
3353 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3357 GtkListStore *store;
3359 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3360 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3361 g_return_if_fail (text != NULL);
3363 store = GTK_LIST_STORE (combo_box->priv->model);
3365 gtk_list_store_prepend (store, &iter);
3366 gtk_list_store_set (store, &iter, 0, text, -1);
3370 * gtk_combo_box_remove_text:
3371 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3372 * @position: Index of the item to remove.
3374 * Removes the string at @position from @combo_box. Note that you can only use
3375 * this function with combo boxes constructed with gtk_combo_box_new_text().
3380 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3384 GtkListStore *store;
3386 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3387 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3388 g_return_if_fail (position >= 0);
3390 store = GTK_LIST_STORE (combo_box->priv->model);
3392 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3394 gtk_list_store_remove (store, &iter);
3399 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3400 gboolean group_cycling)
3402 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3404 gtk_widget_grab_focus (combo_box->priv->button);
3410 gtk_combo_box_destroy (GtkObject *object)
3412 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3414 combo_box->priv->destroying = 1;
3416 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3418 combo_box->priv->cell_view = NULL;
3420 combo_box->priv->destroying = 0;
3424 gtk_combo_box_finalize (GObject *object)
3426 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3429 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3430 gtk_combo_box_menu_destroy (combo_box);
3432 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3433 gtk_combo_box_list_destroy (combo_box);
3435 if (combo_box->priv->popup_window)
3436 gtk_widget_destroy (combo_box->priv->popup_window);
3438 gtk_combo_box_unset_model (combo_box);
3440 if (combo_box->priv->model)
3441 g_object_unref (combo_box->priv->model);
3443 for (i = combo_box->priv->cells; i; i = i->next)
3445 ComboCellInfo *info = (ComboCellInfo *)i->data;
3446 GSList *list = info->attributes;
3449 info->destroy (info->func_data);
3451 while (list && list->next)
3453 g_free (list->data);
3454 list = list->next->next;
3456 g_slist_free (info->attributes);
3458 g_object_unref (G_OBJECT (info->cell));
3461 g_slist_free (combo_box->priv->cells);
3463 G_OBJECT_CLASS (parent_class)->finalize (object);