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);
364 static void cell_view_sync_cells (GtkComboBox *combo_box,
365 GtkCellView *cell_view);
368 gtk_combo_box_get_type (void)
370 static GType combo_box_type = 0;
374 static const GTypeInfo combo_box_info =
376 sizeof (GtkComboBoxClass),
377 NULL, /* base_init */
378 NULL, /* base_finalize */
379 (GClassInitFunc) gtk_combo_box_class_init,
380 NULL, /* class_finalize */
381 NULL, /* class_data */
382 sizeof (GtkComboBox),
384 (GInstanceInitFunc) gtk_combo_box_init
387 static const GInterfaceInfo cell_layout_info =
389 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
394 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
399 g_type_add_interface_static (combo_box_type,
400 GTK_TYPE_CELL_LAYOUT,
404 return combo_box_type;
409 gtk_combo_box_class_init (GtkComboBoxClass *klass)
411 GObjectClass *object_class;
412 GtkBindingSet *binding_set;
413 GtkObjectClass *gtk_object_class;
414 GtkContainerClass *container_class;
415 GtkWidgetClass *widget_class;
417 binding_set = gtk_binding_set_by_class (klass);
419 container_class = (GtkContainerClass *)klass;
420 container_class->forall = gtk_combo_box_forall;
421 container_class->add = gtk_combo_box_add;
422 container_class->remove = gtk_combo_box_remove;
424 widget_class = (GtkWidgetClass *)klass;
425 widget_class->size_allocate = gtk_combo_box_size_allocate;
426 widget_class->size_request = gtk_combo_box_size_request;
427 widget_class->expose_event = gtk_combo_box_expose_event;
428 widget_class->scroll_event = gtk_combo_box_scroll_event;
429 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
430 widget_class->style_set = gtk_combo_box_style_set;
431 widget_class->state_changed = gtk_combo_box_state_changed;
433 gtk_object_class = (GtkObjectClass *)klass;
434 gtk_object_class->destroy = gtk_combo_box_destroy;
436 object_class = (GObjectClass *)klass;
437 object_class->finalize = gtk_combo_box_finalize;
438 object_class->set_property = gtk_combo_box_set_property;
439 object_class->get_property = gtk_combo_box_get_property;
441 parent_class = g_type_class_peek_parent (klass);
444 combo_box_signals[CHANGED] =
445 g_signal_new ("changed",
446 G_OBJECT_CLASS_TYPE (klass),
448 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
450 g_cclosure_marshal_VOID__VOID,
454 g_object_class_install_property (object_class,
456 g_param_spec_object ("model",
457 P_("ComboBox model"),
458 P_("The model for the combo box"),
462 g_object_class_install_property (object_class,
464 g_param_spec_int ("wrap_width",
466 P_("Wrap width for layouting the items in a grid"),
472 g_object_class_install_property (object_class,
473 PROP_ROW_SPAN_COLUMN,
474 g_param_spec_int ("row_span_column",
475 P_("Row span column"),
476 P_("TreeModel column containing the row span values"),
482 g_object_class_install_property (object_class,
483 PROP_COLUMN_SPAN_COLUMN,
484 g_param_spec_int ("column_span_column",
485 P_("Column span column"),
486 P_("TreeModel column containing the column span values"),
492 g_object_class_install_property (object_class,
494 g_param_spec_int ("active",
496 P_("The item which is currently active"),
502 gtk_widget_class_install_style_property (widget_class,
503 g_param_spec_boolean ("appears-as-list",
504 P_("Appears as list"),
505 P_("Whether combobox dropdowns should look like lists rather than menus"),
509 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
513 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
515 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
516 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
517 iface->clear = gtk_combo_box_cell_layout_clear;
518 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
519 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
520 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
521 iface->reorder = gtk_combo_box_cell_layout_reorder;
525 gtk_combo_box_init (GtkComboBox *combo_box)
527 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
529 combo_box->priv->cell_view = gtk_cell_view_new ();
530 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
531 GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
532 gtk_widget_show (combo_box->priv->cell_view);
534 combo_box->priv->width = 0;
535 combo_box->priv->wrap_width = 0;
537 combo_box->priv->active_item = -1;
538 combo_box->priv->col_column = -1;
539 combo_box->priv->row_column = -1;
543 gtk_combo_box_set_property (GObject *object,
548 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
553 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
556 case PROP_WRAP_WIDTH:
557 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
560 case PROP_ROW_SPAN_COLUMN:
561 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
564 case PROP_COLUMN_SPAN_COLUMN:
565 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
569 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
578 gtk_combo_box_get_property (GObject *object,
583 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
588 g_value_set_object (value, combo_box->priv->model);
591 case PROP_WRAP_WIDTH:
592 g_value_set_int (value, combo_box->priv->wrap_width);
595 case PROP_ROW_SPAN_COLUMN:
596 g_value_set_int (value, combo_box->priv->row_column);
599 case PROP_COLUMN_SPAN_COLUMN:
600 g_value_set_int (value, combo_box->priv->col_column);
604 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
608 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
614 gtk_combo_box_state_changed (GtkWidget *widget,
615 GtkStateType previous)
617 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
619 if (GTK_WIDGET_REALIZED (widget))
621 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
622 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
623 &widget->style->base[GTK_WIDGET_STATE (widget)]);
626 gtk_widget_queue_draw (widget);
630 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
632 gboolean appears_as_list;
634 /* if wrap_width > 0, then we are in grid-mode and forced to use
637 if (combo_box->priv->wrap_width)
638 appears_as_list = FALSE;
640 gtk_widget_style_get (GTK_WIDGET (combo_box),
641 "appears-as-list", &appears_as_list,
646 /* Destroy all the menu mode widgets, if they exist. */
647 if (GTK_IS_MENU (combo_box->priv->popup_widget))
648 gtk_combo_box_menu_destroy (combo_box);
650 /* Create the list mode widgets, if they don't already exist. */
651 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
652 gtk_combo_box_list_setup (combo_box);
656 /* Destroy all the list mode widgets, if they exist. */
657 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
658 gtk_combo_box_list_destroy (combo_box);
660 /* Create the menu mode widgets, if they don't already exist. */
661 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
662 gtk_combo_box_menu_setup (combo_box, TRUE);
667 gtk_combo_box_style_set (GtkWidget *widget,
670 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
672 gtk_combo_box_check_appearance (combo_box);
674 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
675 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
676 &widget->style->base[GTK_WIDGET_STATE (widget)]);
680 gtk_combo_box_button_toggled (GtkWidget *widget,
683 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
685 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
687 if (!combo_box->priv->popup_in_progress)
688 gtk_combo_box_popup (combo_box);
691 gtk_combo_box_popdown (combo_box);
695 gtk_combo_box_add (GtkContainer *container,
698 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
700 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
702 gtk_widget_unparent (combo_box->priv->cell_view);
703 GTK_BIN (container)->child = NULL;
704 gtk_widget_queue_resize (GTK_WIDGET (container));
707 gtk_widget_set_parent (widget, GTK_WIDGET (container));
708 GTK_BIN (container)->child = widget;
710 if (combo_box->priv->cell_view &&
711 widget != combo_box->priv->cell_view)
713 /* since the cell_view was unparented, it's gone now */
714 combo_box->priv->cell_view = NULL;
716 if (!combo_box->priv->tree_view && combo_box->priv->separator)
718 gtk_widget_unparent (combo_box->priv->separator);
719 combo_box->priv->separator = NULL;
721 g_object_ref (G_OBJECT (combo_box->priv->arrow));
722 gtk_widget_unparent (combo_box->priv->arrow);
723 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
724 combo_box->priv->arrow);
725 g_object_unref (G_OBJECT (combo_box->priv->arrow));
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->separator)
1506 (* callback) (combo_box->priv->separator, callback_data);
1507 if (combo_box->priv->arrow)
1508 (* callback) (combo_box->priv->arrow, callback_data);
1509 if (combo_box->priv->cell_view_frame)
1510 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1513 if (GTK_BIN (container)->child)
1514 (* callback) (GTK_BIN (container)->child, callback_data);
1518 gtk_combo_box_expose_event (GtkWidget *widget,
1519 GdkEventExpose *event)
1521 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1523 if (!combo_box->priv->tree_view)
1525 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1526 combo_box->priv->button, event);
1528 if (combo_box->priv->separator)
1530 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1531 combo_box->priv->separator, event);
1533 /* if not in this case, arrow gets its expose event from button */
1534 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1535 combo_box->priv->arrow, event);
1540 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1541 combo_box->priv->button, event);
1543 if (combo_box->priv->cell_view_frame)
1544 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1545 combo_box->priv->cell_view_frame, event);
1548 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1549 GTK_BIN (widget)->child, event);
1555 gtk_combo_box_scroll_event (GtkWidget *widget,
1556 GdkEventScroll *event)
1558 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1562 index = gtk_combo_box_get_active (combo_box);
1566 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1568 if (event->direction == GDK_SCROLL_UP)
1573 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1584 cell_view_sync_cells (GtkComboBox *combo_box,
1585 GtkCellView *cell_view)
1589 for (k = combo_box->priv->cells; k; k = k->next)
1592 ComboCellInfo *info = (ComboCellInfo *)k->data;
1594 if (info->pack == GTK_PACK_START)
1595 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1596 info->cell, info->expand);
1597 else if (info->pack == GTK_PACK_END)
1598 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1599 info->cell, info->expand);
1601 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1603 info->func, info->func_data, NULL);
1605 for (j = info->attributes; j; j = j->next->next)
1607 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1610 GPOINTER_TO_INT (j->next->data));
1616 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1617 gboolean add_children)
1621 if (combo_box->priv->cell_view)
1623 combo_box->priv->button = gtk_toggle_button_new ();
1624 g_signal_connect (combo_box->priv->button, "toggled",
1625 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1626 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1627 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1628 gtk_widget_set_parent (combo_box->priv->button,
1629 GTK_BIN (combo_box)->child->parent);
1631 combo_box->priv->separator = gtk_vseparator_new ();
1632 gtk_widget_set_parent (combo_box->priv->separator,
1633 combo_box->priv->button);
1634 gtk_widget_show (combo_box->priv->separator);
1636 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1637 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1638 gtk_widget_show (combo_box->priv->arrow);
1640 gtk_widget_show_all (combo_box->priv->button);
1642 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1644 /* I have no clue why, but we need to manually map in this case. */
1645 gtk_widget_map (combo_box->priv->separator);
1646 gtk_widget_map (combo_box->priv->arrow);
1651 combo_box->priv->button = gtk_toggle_button_new ();
1652 g_signal_connect (combo_box->priv->button, "toggled",
1653 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1654 g_signal_connect_after (combo_box, "key_press_event",
1655 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1656 gtk_widget_set_parent (combo_box->priv->button,
1657 GTK_BIN (combo_box)->child->parent);
1659 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1660 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1661 combo_box->priv->arrow);
1662 gtk_widget_show_all (combo_box->priv->button);
1665 g_signal_connect (combo_box->priv->button, "button_press_event",
1666 G_CALLBACK (gtk_combo_box_menu_button_press),
1669 /* create our funky menu */
1670 box = gtk_menu_new ();
1671 g_signal_connect (box, "key_press_event",
1672 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1673 gtk_combo_box_set_popup_widget (combo_box, box);
1677 gtk_combo_box_menu_fill (combo_box);
1682 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1688 if (!combo_box->priv->model)
1691 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1692 menu = combo_box->priv->popup_widget;
1694 for (i = 0; i < items; i++)
1698 path = gtk_tree_path_new_from_indices (i, -1);
1699 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1701 g_signal_connect (tmp, "activate",
1702 G_CALLBACK (gtk_combo_box_menu_item_activate),
1705 cell_view_sync_cells (combo_box,
1706 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1708 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1710 if (combo_box->priv->wrap_width)
1711 gtk_combo_box_relayout_item (combo_box, i);
1713 gtk_widget_show (tmp);
1715 gtk_tree_path_free (path);
1720 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1722 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1723 G_SIGNAL_MATCH_DATA,
1725 gtk_combo_box_menu_button_press, NULL);
1727 /* unparent will remove our latest ref */
1728 if (combo_box->priv->cell_view)
1730 gtk_widget_unparent (combo_box->priv->arrow);
1731 combo_box->priv->arrow = NULL;
1733 gtk_widget_unparent (combo_box->priv->separator);
1734 combo_box->priv->separator = NULL;
1736 gtk_widget_unparent (combo_box->priv->button);
1737 combo_box->priv->button = NULL;
1741 /* will destroy the arrow too */
1742 gtk_widget_unparent (combo_box->priv->button);
1744 combo_box->priv->button = NULL;
1745 combo_box->priv->arrow = NULL;
1748 /* changing the popup window will unref the menu and the children */
1756 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1763 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1767 if (combo_box->priv->col_column == -1)
1770 gtk_tree_model_get (combo_box->priv->model, &iter,
1771 combo_box->priv->col_column, cols,
1777 if (combo_box->priv->row_column == -1)
1780 gtk_tree_model_get (combo_box->priv->model, &iter,
1781 combo_box->priv->row_column, rows,
1787 menu_occupied (GtkMenu *menu,
1791 guint bottom_attach)
1795 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1796 g_return_val_if_fail (left_attach < right_attach, TRUE);
1797 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1799 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1802 gboolean h_intersect = FALSE;
1803 gboolean v_intersect = FALSE;
1805 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1808 "bottom_attach", &b,
1812 /* look if this item intersects with the given coordinates */
1813 h_intersect = left_attach <= l && l <= right_attach;
1814 h_intersect &= left_attach <= r && r <= right_attach;
1816 v_intersect = top_attach <= t && t <= bottom_attach;
1817 v_intersect &= top_attach <= b && b <= bottom_attach;
1819 if (h_intersect && v_intersect)
1827 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1830 gint current_col = 0, current_row = 0;
1836 menu = combo_box->priv->popup_widget;
1837 if (!GTK_IS_MENU_SHELL (menu))
1840 list = gtk_container_get_children (GTK_CONTAINER (menu));
1841 item = g_list_nth_data (list, index);
1844 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1846 /* look for a good spot */
1849 if (current_col + cols > combo_box->priv->wrap_width)
1855 if (!menu_occupied (GTK_MENU (menu),
1856 current_col, current_col + cols,
1857 current_row, current_row + rows))
1863 /* set attach props */
1864 gtk_menu_attach (GTK_MENU (menu), item,
1865 current_col, current_col + cols,
1866 current_row, current_row + rows);
1870 gtk_combo_box_relayout (GtkComboBox *combo_box)
1875 /* do nothing unless we are in menu style */
1876 if (combo_box->priv->tree_view)
1879 menu = combo_box->priv->popup_widget;
1881 /* get rid of all children */
1882 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1884 list = gtk_container_get_children (GTK_CONTAINER (menu));
1886 for (j = g_list_last (list); j; j = j->prev)
1887 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1892 gtk_combo_box_menu_fill (combo_box);
1897 gtk_combo_box_menu_button_press (GtkWidget *widget,
1898 GdkEventButton *event,
1901 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1903 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1906 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1908 combo_box->priv->popup_in_progress = TRUE;
1910 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1911 combo_box->priv->active_item);
1913 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1915 gtk_combo_box_menu_position, combo_box,
1916 event->button, event->time);
1925 gtk_combo_box_menu_item_activate (GtkWidget *item,
1930 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1932 menu = combo_box->priv->popup_widget;
1933 g_return_if_fail (GTK_IS_MENU (menu));
1935 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1937 gtk_combo_box_set_active (combo_box, index);
1941 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1946 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1947 gint index = gtk_tree_path_get_indices (path)[0];
1949 if (combo_box->priv->active_item >= index)
1950 combo_box->priv->active_item++;
1952 if (!combo_box->priv->tree_view)
1953 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1957 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1961 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1962 gint index = gtk_tree_path_get_indices (path)[0];
1964 if (!combo_box->priv->tree_view)
1965 gtk_combo_box_menu_row_deleted (model, path, user_data);
1967 if (index == combo_box->priv->active_item)
1969 gint items = gtk_tree_model_iter_n_children (model, NULL);
1972 gtk_combo_box_set_active_internal (combo_box, -1);
1973 else if (index == items)
1974 gtk_combo_box_set_active_internal (combo_box, index - 1);
1976 gtk_combo_box_set_active_internal (combo_box, index);
1978 else if (combo_box->priv->active_item > index)
1979 combo_box->priv->active_item--;
1983 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1989 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1990 gint items = gtk_tree_model_iter_n_children (model, NULL);
1993 for (i = 0; i < items; i++)
1994 if (new_order[i] == combo_box->priv->active_item)
1996 combo_box->priv->active_item = i;
2000 if (!combo_box->priv->tree_view)
2001 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2005 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2010 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2011 gint index = gtk_tree_path_get_indices (path)[0];
2013 if (index == combo_box->priv->active_item &&
2014 combo_box->priv->cell_view)
2015 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2017 if (combo_box->priv->tree_view)
2018 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2020 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2025 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2032 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2034 if (!combo_box->priv->popup_widget)
2037 menu = combo_box->priv->popup_widget;
2038 g_return_if_fail (GTK_IS_MENU (menu));
2040 item = gtk_cell_view_menu_item_new_from_model (model, path);
2041 g_signal_connect (item, "activate",
2042 G_CALLBACK (gtk_combo_box_menu_item_activate),
2045 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2047 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2048 gtk_tree_path_get_indices (path)[0]);
2049 gtk_widget_show (item);
2053 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2060 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2062 if (!combo_box->priv->popup_widget)
2065 index = gtk_tree_path_get_indices (path)[0];
2067 menu = combo_box->priv->popup_widget;
2068 g_return_if_fail (GTK_IS_MENU (menu));
2070 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2071 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2073 gtk_container_remove (GTK_CONTAINER (menu), item);
2077 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2083 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2085 gtk_combo_box_relayout (combo_box);
2089 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2094 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2097 if (!combo_box->priv->popup_widget)
2100 if (combo_box->priv->wrap_width)
2101 gtk_combo_box_relayout_item (combo_box,
2102 gtk_tree_path_get_indices (path)[0]);
2104 width = gtk_combo_box_calc_requested_width (combo_box, path);
2106 if (width > combo_box->priv->width)
2108 if (combo_box->priv->cell_view)
2110 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2111 gtk_widget_queue_resize (combo_box->priv->cell_view);
2113 combo_box->priv->width = width;
2122 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2125 GtkTreeSelection *sel;
2127 combo_box->priv->button = gtk_toggle_button_new ();
2128 gtk_widget_set_parent (combo_box->priv->button,
2129 GTK_BIN (combo_box)->child->parent);
2130 g_signal_connect (combo_box->priv->button, "button_press_event",
2131 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2132 g_signal_connect (combo_box->priv->button, "toggled",
2133 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2134 g_signal_connect_after (combo_box, "key_press_event",
2135 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2137 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2138 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2139 combo_box->priv->arrow);
2140 combo_box->priv->separator = NULL;
2141 gtk_widget_show_all (combo_box->priv->button);
2143 if (combo_box->priv->cell_view)
2145 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2146 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2147 GTK_BIN (combo_box)->child->parent);
2148 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2151 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2152 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2154 gtk_widget_show (combo_box->priv->cell_view_frame);
2157 combo_box->priv->tree_view = gtk_tree_view_new ();
2158 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2159 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2160 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2162 if (combo_box->priv->model)
2163 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2164 combo_box->priv->model);
2166 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2167 G_CALLBACK (gtk_combo_box_list_button_pressed),
2169 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2170 G_CALLBACK (gtk_combo_box_list_button_released),
2172 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2173 G_CALLBACK (gtk_combo_box_list_key_press),
2176 combo_box->priv->column = gtk_tree_view_column_new ();
2177 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2178 combo_box->priv->column);
2181 for (i = combo_box->priv->cells; i; i = i->next)
2184 ComboCellInfo *info = (ComboCellInfo *)i->data;
2186 if (info->pack == GTK_PACK_START)
2187 gtk_tree_view_column_pack_start (combo_box->priv->column,
2188 info->cell, info->expand);
2189 else if (info->pack == GTK_PACK_END)
2190 gtk_tree_view_column_pack_end (combo_box->priv->column,
2191 info->cell, info->expand);
2193 for (j = info->attributes; j; j = j->next->next)
2195 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2198 GPOINTER_TO_INT (j->next->data));
2202 if (combo_box->priv->active_item != -1)
2206 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2209 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2211 gtk_tree_path_free (path);
2215 /* set sample/popup widgets */
2216 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2218 gtk_widget_show (combo_box->priv->tree_view);
2222 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2224 /* disconnect signals */
2225 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2226 G_SIGNAL_MATCH_DATA,
2227 0, 0, NULL, NULL, combo_box);
2228 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2229 G_SIGNAL_MATCH_DATA,
2231 gtk_combo_box_list_button_pressed,
2234 /* destroy things (unparent will kill the latest ref from us)
2235 * last unref on button will destroy the arrow
2237 gtk_widget_unparent (combo_box->priv->button);
2238 combo_box->priv->button = NULL;
2239 combo_box->priv->arrow = NULL;
2241 if (combo_box->priv->cell_view)
2243 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2244 "background_set", FALSE,
2247 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2248 combo_box->priv->cell_view_frame = NULL;
2251 gtk_widget_destroy (combo_box->priv->tree_view);
2253 combo_box->priv->tree_view = NULL;
2254 combo_box->priv->popup_widget = NULL;
2259 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2261 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2262 gtk_grab_remove (combo_box->priv->tree_view);
2264 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2266 gtk_grab_remove (combo_box->priv->popup_window);
2267 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2268 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2273 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2274 GdkEventButton *event,
2277 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2279 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2281 if (ewidget == combo_box->priv->tree_view)
2284 if ((ewidget != combo_box->priv->button) ||
2285 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2288 gtk_combo_box_popup (combo_box);
2290 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2293 combo_box->priv->popup_in_progress = TRUE;
2299 gtk_combo_box_list_button_released (GtkWidget *widget,
2300 GdkEventButton *event,
2304 GtkTreePath *path = NULL;
2306 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2308 gboolean popup_in_progress = FALSE;
2310 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2312 if (combo_box->priv->popup_in_progress)
2314 popup_in_progress = TRUE;
2315 combo_box->priv->popup_in_progress = FALSE;
2318 if (ewidget != combo_box->priv->tree_view)
2320 if (ewidget == combo_box->priv->button &&
2321 !popup_in_progress &&
2322 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2324 gtk_combo_box_popdown (combo_box);
2328 /* released outside treeview */
2329 if (ewidget != combo_box->priv->button)
2331 gtk_combo_box_popdown (combo_box);
2339 /* select something cool */
2340 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2346 return TRUE; /* clicked outside window? */
2348 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2349 gtk_combo_box_popdown (combo_box);
2351 gtk_tree_path_free (path);
2357 gtk_combo_box_key_press (GtkWidget *widget,
2361 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2362 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2363 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2364 gint index = gtk_combo_box_get_active (combo_box);
2367 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2368 state == GDK_MOD1_MASK)
2370 gtk_combo_box_popup (combo_box);
2375 switch (event->keyval)
2379 new_index = index + 1;
2383 new_index = index - 1;
2386 case GDK_KP_Page_Up:
2392 case GDK_KP_Page_Down:
2395 new_index = items - 1;
2401 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2407 gtk_combo_box_menu_key_press (GtkWidget *widget,
2411 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2412 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2414 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2415 state == GDK_MOD1_MASK)
2417 gtk_combo_box_popdown (combo_box);
2426 gtk_combo_box_list_key_press (GtkWidget *widget,
2430 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2431 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2433 if (event->keyval == GDK_Escape ||
2434 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2435 state == GDK_MOD1_MASK))
2437 /* reset active item -- this is incredibly lame and ugly */
2438 gtk_combo_box_set_active (combo_box,
2439 gtk_combo_box_get_active (combo_box));
2441 gtk_combo_box_popdown (combo_box);
2446 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2447 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2451 GtkTreeModel *model = NULL;
2452 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2454 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2459 path = gtk_tree_model_get_path (model, &iter);
2462 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2463 gtk_tree_path_free (path);
2467 gtk_combo_box_popdown (combo_box);
2476 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2481 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2484 width = gtk_combo_box_calc_requested_width (combo_box, path);
2486 if (width > combo_box->priv->width)
2488 if (combo_box->priv->cell_view)
2490 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2491 gtk_widget_queue_resize (combo_box->priv->cell_view);
2493 combo_box->priv->width = width;
2498 * GtkCellLayout implementation
2501 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2502 GtkCellRenderer *cell,
2505 ComboCellInfo *info;
2506 GtkComboBox *combo_box;
2509 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2510 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2512 combo_box = GTK_COMBO_BOX (layout);
2514 g_object_ref (G_OBJECT (cell));
2515 gtk_object_sink (GTK_OBJECT (cell));
2517 info = g_new0 (ComboCellInfo, 1);
2519 info->expand = expand;
2520 info->pack = GTK_PACK_START;
2522 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2524 if (combo_box->priv->cell_view)
2525 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2528 if (combo_box->priv->column)
2529 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2531 menu = combo_box->priv->popup_widget;
2532 if (GTK_IS_MENU (menu))
2536 list = gtk_container_get_children (GTK_CONTAINER (menu));
2537 for (i = list; i; i = i->next)
2541 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2542 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2544 view = GTK_CELL_VIEW (i->data);
2546 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2553 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2554 GtkCellRenderer *cell,
2557 ComboCellInfo *info;
2558 GtkComboBox *combo_box;
2561 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2562 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2564 combo_box = GTK_COMBO_BOX (layout);
2566 g_object_ref (G_OBJECT (cell));
2567 gtk_object_sink (GTK_OBJECT (cell));
2569 info = g_new0 (ComboCellInfo, 1);
2571 info->expand = expand;
2572 info->pack = GTK_PACK_END;
2574 if (combo_box->priv->cell_view)
2575 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2578 if (combo_box->priv->column)
2579 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2581 menu = combo_box->priv->popup_widget;
2582 if (GTK_IS_MENU (menu))
2586 list = gtk_container_get_children (GTK_CONTAINER (menu));
2587 for (i = list; i; i = i->next)
2591 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2592 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2594 view = GTK_CELL_VIEW (i->data);
2596 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2603 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2606 GtkComboBox *combo_box;
2609 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2611 combo_box = GTK_COMBO_BOX (layout);
2613 if (combo_box->priv->cell_view)
2614 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2616 if (combo_box->priv->column)
2617 gtk_tree_view_column_clear (combo_box->priv->column);
2619 for (i = combo_box->priv->cells; i; i = i->next)
2621 ComboCellInfo *info = (ComboCellInfo *)i->data;
2623 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2624 g_object_unref (G_OBJECT (info->cell));
2628 g_slist_free (combo_box->priv->cells);
2629 combo_box->priv->cells = NULL;
2631 menu = combo_box->priv->popup_widget;
2632 if (GTK_IS_MENU (menu))
2636 list = gtk_container_get_children (GTK_CONTAINER (menu));
2637 for (i = list; i; i = i->next)
2641 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2642 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2644 view = GTK_CELL_VIEW (i->data);
2646 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2653 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2654 GtkCellRenderer *cell,
2655 const gchar *attribute,
2658 ComboCellInfo *info;
2659 GtkComboBox *combo_box;
2662 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2663 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2665 combo_box = GTK_COMBO_BOX (layout);
2667 info = gtk_combo_box_get_cell_info (combo_box, cell);
2669 info->attributes = g_slist_prepend (info->attributes,
2670 GINT_TO_POINTER (column));
2671 info->attributes = g_slist_prepend (info->attributes,
2672 g_strdup (attribute));
2674 if (combo_box->priv->cell_view)
2675 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2676 cell, attribute, column);
2678 if (combo_box->priv->column)
2679 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2680 cell, attribute, column);
2682 menu = combo_box->priv->popup_widget;
2683 if (GTK_IS_MENU (menu))
2687 list = gtk_container_get_children (GTK_CONTAINER (menu));
2688 for (i = list; i; i = i->next)
2692 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2693 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2695 view = GTK_CELL_VIEW (i->data);
2697 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2703 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2707 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2708 GtkCellRenderer *cell,
2709 GtkCellLayoutDataFunc func,
2711 GDestroyNotify destroy)
2713 ComboCellInfo *info;
2714 GtkComboBox *combo_box;
2717 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2719 combo_box = GTK_COMBO_BOX (layout);
2721 info = gtk_combo_box_get_cell_info (combo_box, cell);
2722 g_return_if_fail (info != NULL);
2726 GDestroyNotify d = info->destroy;
2728 info->destroy = NULL;
2729 d (info->func_data);
2733 info->func_data = func_data;
2734 info->destroy = destroy;
2736 if (combo_box->priv->cell_view)
2737 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2739 if (combo_box->priv->column)
2740 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2742 menu = combo_box->priv->popup_widget;
2743 if (GTK_IS_MENU (menu))
2747 list = gtk_container_get_children (GTK_CONTAINER (menu));
2748 for (i = list; i; i = i->next)
2752 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2753 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2755 view = GTK_CELL_VIEW (i->data);
2757 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2758 func, func_data, NULL);
2763 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2767 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2768 GtkCellRenderer *cell)
2770 ComboCellInfo *info;
2771 GtkComboBox *combo_box;
2775 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2776 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2778 combo_box = GTK_COMBO_BOX (layout);
2780 info = gtk_combo_box_get_cell_info (combo_box, cell);
2781 g_return_if_fail (info != NULL);
2783 list = info->attributes;
2784 while (list && list->next)
2786 g_free (list->data);
2787 list = list->next->next;
2789 g_slist_free (info->attributes);
2790 info->attributes = NULL;
2792 if (combo_box->priv->cell_view)
2793 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2795 if (combo_box->priv->column)
2796 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2798 menu = combo_box->priv->popup_widget;
2799 if (GTK_IS_MENU (menu))
2803 list = gtk_container_get_children (GTK_CONTAINER (menu));
2804 for (i = list; i; i = i->next)
2808 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2809 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2811 view = GTK_CELL_VIEW (i->data);
2813 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2818 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2822 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2823 GtkCellRenderer *cell,
2826 ComboCellInfo *info;
2827 GtkComboBox *combo_box;
2831 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2832 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2834 combo_box = GTK_COMBO_BOX (layout);
2836 info = gtk_combo_box_get_cell_info (combo_box, cell);
2838 g_return_if_fail (info != NULL);
2839 g_return_if_fail (position >= 0);
2841 link = g_slist_find (combo_box->priv->cells, info);
2843 g_return_if_fail (link != NULL);
2845 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2846 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2849 if (combo_box->priv->cell_view)
2850 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2853 if (combo_box->priv->column)
2854 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2857 menu = combo_box->priv->popup_widget;
2858 if (GTK_IS_MENU (menu))
2862 list = gtk_container_get_children (GTK_CONTAINER (menu));
2863 for (i = list; i; i = i->next)
2867 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2868 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2870 view = GTK_CELL_VIEW (i->data);
2872 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2877 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2885 * gtk_combo_box_new:
2887 * Creates a new empty #GtkComboBox.
2889 * Return value: A new #GtkComboBox.
2894 gtk_combo_box_new (void)
2896 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2900 * gtk_combo_box_new_with_model:
2901 * @model: A #GtkTreeModel.
2903 * Creates a new #GtkComboBox with the model initialized to @model.
2905 * Return value: A new #GtkComboBox.
2910 gtk_combo_box_new_with_model (GtkTreeModel *model)
2912 GtkComboBox *combo_box;
2914 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2916 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2920 return GTK_WIDGET (combo_box);
2924 * gtk_combo_box_set_wrap_width:
2925 * @combo_box: A #GtkComboBox.
2926 * @width: Preferred number of columns.
2928 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2929 * the preferred number of columns when you want to the popup to be layed out
2935 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2938 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2939 g_return_if_fail (width >= 0);
2941 if (width != combo_box->priv->wrap_width)
2943 combo_box->priv->wrap_width = width;
2945 gtk_combo_box_check_appearance (combo_box);
2946 gtk_combo_box_relayout (combo_box);
2948 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2953 * gtk_combo_box_set_row_span_column:
2954 * @combo_box: A #GtkComboBox.
2955 * @row_span: A column in the model passed during construction.
2957 * Sets the column with row span information for @combo_box to be @row_span.
2958 * The row span column contains integers which indicate how many rows
2959 * an item should span.
2964 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2969 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2971 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2972 g_return_if_fail (row_span >= 0 && row_span < col);
2974 if (row_span != combo_box->priv->row_column)
2976 combo_box->priv->row_column = row_span;
2978 gtk_combo_box_relayout (combo_box);
2980 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2985 * gtk_combo_box_set_column_span_column:
2986 * @combo_box: A #GtkComboBox.
2987 * @column_span: A column in the model passed during construction.
2989 * Sets the column with column span information for @combo_box to be
2990 * @column_span. The column span column contains integers which indicate
2991 * how many columns an item should span.
2996 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
3001 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3003 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3004 g_return_if_fail (column_span >= 0 && column_span < col);
3006 if (column_span != combo_box->priv->col_column)
3008 combo_box->priv->col_column = column_span;
3010 gtk_combo_box_relayout (combo_box);
3012 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3017 * gtk_combo_box_get_active:
3018 * @combo_box: A #GtkComboBox.
3020 * Returns the index of the currently active item, or -1 if there's no
3023 * Return value: An integer which is the index of the currently active item, or
3024 * -1 if there's no active item.
3029 gtk_combo_box_get_active (GtkComboBox *combo_box)
3031 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3033 return combo_box->priv->active_item;
3037 * gtk_combo_box_set_active:
3038 * @combo_box: A #GtkComboBox.
3039 * @index: An index in the model passed during construction, or -1 to have
3042 * Sets the active item of @combo_box to be the item at @index.
3047 gtk_combo_box_set_active (GtkComboBox *combo_box,
3050 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3051 /* -1 means "no item selected" */
3052 g_return_if_fail (index >= -1);
3054 if (combo_box->priv->active_item == index)
3057 gtk_combo_box_set_active_internal (combo_box, index);
3061 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3066 combo_box->priv->active_item = index;
3070 if (combo_box->priv->tree_view)
3071 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3074 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3076 if (GTK_IS_MENU (menu))
3077 gtk_menu_set_active (menu, -1);
3080 if (combo_box->priv->cell_view)
3081 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3085 path = gtk_tree_path_new_from_indices (index, -1);
3087 if (combo_box->priv->tree_view)
3088 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3091 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3093 if (GTK_IS_MENU (menu))
3094 gtk_menu_set_active (GTK_MENU (menu), index);
3097 if (combo_box->priv->cell_view)
3098 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3100 gtk_tree_path_free (path);
3103 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3108 * gtk_combo_box_get_active_iter:
3109 * @combo_box: A #GtkComboBox
3110 * @iter: The uninitialized #GtkTreeIter.
3112 * Sets @iter to point to the current active item, if it exists.
3114 * Return value: %TRUE, if @iter was set
3119 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3126 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3128 active = gtk_combo_box_get_active (combo_box);
3132 path = gtk_tree_path_new_from_indices (active, -1);
3133 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3135 gtk_tree_path_free (path);
3141 * gtk_combo_box_set_active_iter:
3142 * @combo_box: A #GtkComboBox
3143 * @iter: The #GtkTreeIter.
3145 * Sets the current active item to be the one referenced by @iter.
3146 * @iter must correspond to a path of depth one.
3151 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3156 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3158 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3159 g_return_if_fail (path != NULL);
3160 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3162 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3163 gtk_tree_path_free (path);
3167 * gtk_combo_box_set_model:
3168 * @combo_box: A #GtkComboBox.
3169 * @model: A #GtkTreeModel.
3171 * Sets the model used by @combo_box to be @model. Will unset a
3172 * previously set model (if applicable).
3177 gtk_combo_box_set_model (GtkComboBox *combo_box,
3178 GtkTreeModel *model)
3180 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3181 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3183 if (model == combo_box->priv->model)
3186 if (combo_box->priv->model)
3187 gtk_combo_box_unset_model (combo_box);
3189 combo_box->priv->model = model;
3190 g_object_ref (G_OBJECT (combo_box->priv->model));
3192 combo_box->priv->inserted_id =
3193 g_signal_connect (combo_box->priv->model, "row_inserted",
3194 G_CALLBACK (gtk_combo_box_model_row_inserted),
3196 combo_box->priv->deleted_id =
3197 g_signal_connect (combo_box->priv->model, "row_deleted",
3198 G_CALLBACK (gtk_combo_box_model_row_deleted),
3200 combo_box->priv->reordered_id =
3201 g_signal_connect (combo_box->priv->model, "rows_reordered",
3202 G_CALLBACK (gtk_combo_box_model_rows_reordered),
3204 combo_box->priv->changed_id =
3205 g_signal_connect (combo_box->priv->model, "row_changed",
3206 G_CALLBACK (gtk_combo_box_model_row_changed),
3209 if (combo_box->priv->tree_view)
3212 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3213 combo_box->priv->model);
3218 if (combo_box->priv->popup_widget)
3219 gtk_combo_box_menu_fill (combo_box);
3223 if (combo_box->priv->cell_view)
3224 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3225 combo_box->priv->model);
3229 * gtk_combo_box_get_model
3230 * @combo_box: A #GtkComboBox.
3232 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3234 * Return value: A #GtkTreeModel which was passed during construction.
3239 gtk_combo_box_get_model (GtkComboBox *combo_box)
3241 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3243 return combo_box->priv->model;
3247 /* convenience API for simple text combos */
3250 * gtk_combo_box_new_text:
3252 * Convenience function which constructs a new text combo box, which is a
3253 * #GtkComboBox just displaying strings. If you use this function to create
3254 * a text combo box, you should only manipulate its data source with the
3255 * following convenience functions: gtk_combo_box_append_text(),
3256 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3257 * gtk_combo_box_remove_text().
3259 * Return value: A new text combo box.
3264 gtk_combo_box_new_text (void)
3266 GtkWidget *combo_box;
3267 GtkCellRenderer *cell;
3268 GtkListStore *store;
3270 store = gtk_list_store_new (1, G_TYPE_STRING);
3272 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3274 cell = gtk_cell_renderer_text_new ();
3275 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3276 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3284 * gtk_combo_box_append_text:
3285 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3288 * Appends @string to the list of strings stored in @combo_box. Note that
3289 * you can only use this function with combo boxes constructed with
3290 * gtk_combo_box_new_text().
3295 gtk_combo_box_append_text (GtkComboBox *combo_box,
3299 GtkListStore *store;
3301 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3302 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3303 g_return_if_fail (text != NULL);
3305 store = GTK_LIST_STORE (combo_box->priv->model);
3307 gtk_list_store_append (store, &iter);
3308 gtk_list_store_set (store, &iter, 0, text, -1);
3312 * gtk_combo_box_insert_text:
3313 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3314 * @position: An index to insert @text.
3317 * Inserts @string at @position in the list of strings stored in @combo_box.
3318 * Note that you can only use this function with combo boxes constructed
3319 * with gtk_combo_box_new_text().
3324 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3329 GtkListStore *store;
3331 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3332 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3333 g_return_if_fail (position >= 0);
3334 g_return_if_fail (text != NULL);
3336 store = GTK_LIST_STORE (combo_box->priv->model);
3338 gtk_list_store_insert (store, &iter, position);
3339 gtk_list_store_set (store, &iter, 0, text, -1);
3343 * gtk_combo_box_prepend_text:
3344 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3347 * Prepends @string to the list of strings stored in @combo_box. Note that
3348 * you can only use this function with combo boxes constructed with
3349 * gtk_combo_box_new_text().
3354 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3358 GtkListStore *store;
3360 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3361 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3362 g_return_if_fail (text != NULL);
3364 store = GTK_LIST_STORE (combo_box->priv->model);
3366 gtk_list_store_prepend (store, &iter);
3367 gtk_list_store_set (store, &iter, 0, text, -1);
3371 * gtk_combo_box_remove_text:
3372 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3373 * @position: Index of the item to remove.
3375 * Removes the string at @position from @combo_box. Note that you can only use
3376 * this function with combo boxes constructed with gtk_combo_box_new_text().
3381 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3385 GtkListStore *store;
3387 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3388 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3389 g_return_if_fail (position >= 0);
3391 store = GTK_LIST_STORE (combo_box->priv->model);
3393 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3395 gtk_list_store_remove (store, &iter);
3400 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3401 gboolean group_cycling)
3403 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3405 gtk_widget_grab_focus (combo_box->priv->button);
3411 gtk_combo_box_destroy (GtkObject *object)
3413 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3415 combo_box->priv->destroying = 1;
3417 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3419 combo_box->priv->cell_view = NULL;
3421 combo_box->priv->destroying = 0;
3425 gtk_combo_box_finalize (GObject *object)
3427 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3430 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3431 gtk_combo_box_menu_destroy (combo_box);
3433 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3434 gtk_combo_box_list_destroy (combo_box);
3436 if (combo_box->priv->popup_window)
3437 gtk_widget_destroy (combo_box->priv->popup_window);
3439 gtk_combo_box_unset_model (combo_box);
3441 if (combo_box->priv->model)
3442 g_object_unref (combo_box->priv->model);
3444 for (i = combo_box->priv->cells; i; i = i->next)
3446 ComboCellInfo *info = (ComboCellInfo *)i->data;
3447 GSList *list = info->attributes;
3450 info->destroy (info->func_data);
3452 while (list && list->next)
3454 g_free (list->data);
3455 list = list->next->next;
3457 g_slist_free (info->attributes);
3459 g_object_unref (G_OBJECT (info->cell));
3462 g_slist_free (combo_box->priv->cells);
3464 G_OBJECT_CLASS (parent_class)->finalize (object);