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 g_object_unref (G_OBJECT (combo_box->priv->model));
1487 combo_box->priv->model = NULL;
1491 gtk_combo_box_forall (GtkContainer *container,
1492 gboolean include_internals,
1493 GtkCallback callback,
1494 gpointer callback_data)
1496 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1498 if (include_internals)
1500 if (combo_box->priv->button)
1501 (* callback) (combo_box->priv->button, callback_data);
1502 if (combo_box->priv->separator)
1503 (* callback) (combo_box->priv->separator, callback_data);
1504 if (combo_box->priv->arrow)
1505 (* callback) (combo_box->priv->arrow, callback_data);
1506 if (combo_box->priv->cell_view_frame)
1507 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1510 if (GTK_BIN (container)->child)
1511 (* callback) (GTK_BIN (container)->child, callback_data);
1515 gtk_combo_box_expose_event (GtkWidget *widget,
1516 GdkEventExpose *event)
1518 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1520 if (!combo_box->priv->tree_view)
1522 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1523 combo_box->priv->button, event);
1525 if (combo_box->priv->separator)
1527 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1528 combo_box->priv->separator, event);
1530 /* if not in this case, arrow gets its expose event from button */
1531 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1532 combo_box->priv->arrow, event);
1537 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1538 combo_box->priv->button, event);
1540 if (combo_box->priv->cell_view_frame)
1541 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1542 combo_box->priv->cell_view_frame, event);
1545 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1546 GTK_BIN (widget)->child, event);
1552 gtk_combo_box_scroll_event (GtkWidget *widget,
1553 GdkEventScroll *event)
1555 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1559 index = gtk_combo_box_get_active (combo_box);
1563 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1565 if (event->direction == GDK_SCROLL_UP)
1570 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1581 cell_view_sync_cells (GtkComboBox *combo_box,
1582 GtkCellView *cell_view)
1586 for (k = combo_box->priv->cells; k; k = k->next)
1589 ComboCellInfo *info = (ComboCellInfo *)k->data;
1591 if (info->pack == GTK_PACK_START)
1592 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1593 info->cell, info->expand);
1594 else if (info->pack == GTK_PACK_END)
1595 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1596 info->cell, info->expand);
1598 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1600 info->func, info->func_data, NULL);
1602 for (j = info->attributes; j; j = j->next->next)
1604 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1607 GPOINTER_TO_INT (j->next->data));
1613 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1614 gboolean add_children)
1618 if (combo_box->priv->cell_view)
1620 combo_box->priv->button = gtk_toggle_button_new ();
1621 g_signal_connect (combo_box->priv->button, "toggled",
1622 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1623 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1624 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1625 gtk_widget_set_parent (combo_box->priv->button,
1626 GTK_BIN (combo_box)->child->parent);
1628 combo_box->priv->separator = gtk_vseparator_new ();
1629 gtk_widget_set_parent (combo_box->priv->separator,
1630 combo_box->priv->button);
1631 gtk_widget_show (combo_box->priv->separator);
1633 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1634 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1635 gtk_widget_show (combo_box->priv->arrow);
1637 gtk_widget_show_all (combo_box->priv->button);
1639 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1641 /* I have no clue why, but we need to manually map in this case. */
1642 gtk_widget_map (combo_box->priv->separator);
1643 gtk_widget_map (combo_box->priv->arrow);
1648 combo_box->priv->button = gtk_toggle_button_new ();
1649 g_signal_connect (combo_box->priv->button, "toggled",
1650 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1651 g_signal_connect_after (combo_box, "key_press_event",
1652 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1653 gtk_widget_set_parent (combo_box->priv->button,
1654 GTK_BIN (combo_box)->child->parent);
1656 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1657 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1658 combo_box->priv->arrow);
1659 gtk_widget_show_all (combo_box->priv->button);
1662 g_signal_connect (combo_box->priv->button, "button_press_event",
1663 G_CALLBACK (gtk_combo_box_menu_button_press),
1666 /* create our funky menu */
1667 box = gtk_menu_new ();
1668 g_signal_connect (box, "key_press_event",
1669 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1670 gtk_combo_box_set_popup_widget (combo_box, box);
1674 gtk_combo_box_menu_fill (combo_box);
1679 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1685 if (!combo_box->priv->model)
1688 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1689 menu = combo_box->priv->popup_widget;
1691 for (i = 0; i < items; i++)
1695 path = gtk_tree_path_new_from_indices (i, -1);
1696 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1698 g_signal_connect (tmp, "activate",
1699 G_CALLBACK (gtk_combo_box_menu_item_activate),
1702 cell_view_sync_cells (combo_box,
1703 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1705 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1707 if (combo_box->priv->wrap_width)
1708 gtk_combo_box_relayout_item (combo_box, i);
1710 gtk_widget_show (tmp);
1712 gtk_tree_path_free (path);
1717 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1719 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1720 G_SIGNAL_MATCH_DATA,
1722 gtk_combo_box_menu_button_press, NULL);
1724 /* unparent will remove our latest ref */
1725 if (combo_box->priv->cell_view)
1727 gtk_widget_unparent (combo_box->priv->arrow);
1728 combo_box->priv->arrow = NULL;
1730 gtk_widget_unparent (combo_box->priv->separator);
1731 combo_box->priv->separator = NULL;
1733 gtk_widget_unparent (combo_box->priv->button);
1734 combo_box->priv->button = NULL;
1738 /* will destroy the arrow too */
1739 gtk_widget_unparent (combo_box->priv->button);
1741 combo_box->priv->button = NULL;
1742 combo_box->priv->arrow = NULL;
1745 /* changing the popup window will unref the menu and the children */
1753 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1760 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1764 if (combo_box->priv->col_column == -1)
1767 gtk_tree_model_get (combo_box->priv->model, &iter,
1768 combo_box->priv->col_column, cols,
1774 if (combo_box->priv->row_column == -1)
1777 gtk_tree_model_get (combo_box->priv->model, &iter,
1778 combo_box->priv->row_column, rows,
1784 menu_occupied (GtkMenu *menu,
1788 guint bottom_attach)
1792 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1793 g_return_val_if_fail (left_attach < right_attach, TRUE);
1794 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1796 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1799 gboolean h_intersect = FALSE;
1800 gboolean v_intersect = FALSE;
1802 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1805 "bottom_attach", &b,
1809 /* look if this item intersects with the given coordinates */
1810 h_intersect = left_attach <= l && l <= right_attach;
1811 h_intersect &= left_attach <= r && r <= right_attach;
1813 v_intersect = top_attach <= t && t <= bottom_attach;
1814 v_intersect &= top_attach <= b && b <= bottom_attach;
1816 if (h_intersect && v_intersect)
1824 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1827 gint current_col = 0, current_row = 0;
1833 menu = combo_box->priv->popup_widget;
1834 if (!GTK_IS_MENU_SHELL (menu))
1837 list = gtk_container_get_children (GTK_CONTAINER (menu));
1838 item = g_list_nth_data (list, index);
1841 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1843 /* look for a good spot */
1846 if (current_col + cols > combo_box->priv->wrap_width)
1852 if (!menu_occupied (GTK_MENU (menu),
1853 current_col, current_col + cols,
1854 current_row, current_row + rows))
1860 /* set attach props */
1861 gtk_menu_attach (GTK_MENU (menu), item,
1862 current_col, current_col + cols,
1863 current_row, current_row + rows);
1867 gtk_combo_box_relayout (GtkComboBox *combo_box)
1872 /* do nothing unless we are in menu style */
1873 if (combo_box->priv->tree_view)
1876 menu = combo_box->priv->popup_widget;
1878 /* get rid of all children */
1879 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1881 list = gtk_container_get_children (GTK_CONTAINER (menu));
1883 for (j = g_list_last (list); j; j = j->prev)
1884 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1889 gtk_combo_box_menu_fill (combo_box);
1894 gtk_combo_box_menu_button_press (GtkWidget *widget,
1895 GdkEventButton *event,
1898 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1900 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1903 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1905 combo_box->priv->popup_in_progress = TRUE;
1907 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1908 combo_box->priv->active_item);
1910 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1912 gtk_combo_box_menu_position, combo_box,
1913 event->button, event->time);
1922 gtk_combo_box_menu_item_activate (GtkWidget *item,
1927 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1929 menu = combo_box->priv->popup_widget;
1930 g_return_if_fail (GTK_IS_MENU (menu));
1932 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1934 gtk_combo_box_set_active (combo_box, index);
1938 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1943 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1944 gint index = gtk_tree_path_get_indices (path)[0];
1946 if (combo_box->priv->active_item >= index)
1947 combo_box->priv->active_item++;
1949 if (!combo_box->priv->tree_view)
1950 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1954 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1958 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1959 gint index = gtk_tree_path_get_indices (path)[0];
1961 if (!combo_box->priv->tree_view)
1962 gtk_combo_box_menu_row_deleted (model, path, user_data);
1964 if (index == combo_box->priv->active_item)
1966 gint items = gtk_tree_model_iter_n_children (model, NULL);
1969 gtk_combo_box_set_active_internal (combo_box, -1);
1970 else if (index == items)
1971 gtk_combo_box_set_active_internal (combo_box, index - 1);
1973 gtk_combo_box_set_active_internal (combo_box, index);
1975 else if (combo_box->priv->active_item > index)
1976 combo_box->priv->active_item--;
1980 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1986 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1987 gint items = gtk_tree_model_iter_n_children (model, NULL);
1990 for (i = 0; i < items; i++)
1991 if (new_order[i] == combo_box->priv->active_item)
1993 combo_box->priv->active_item = i;
1997 if (!combo_box->priv->tree_view)
1998 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2002 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2007 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2008 gint index = gtk_tree_path_get_indices (path)[0];
2010 if (index == combo_box->priv->active_item &&
2011 combo_box->priv->cell_view)
2012 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2014 if (combo_box->priv->tree_view)
2015 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2017 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2022 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2029 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2031 if (!combo_box->priv->popup_widget)
2034 menu = combo_box->priv->popup_widget;
2035 g_return_if_fail (GTK_IS_MENU (menu));
2037 item = gtk_cell_view_menu_item_new_from_model (model, path);
2038 g_signal_connect (item, "activate",
2039 G_CALLBACK (gtk_combo_box_menu_item_activate),
2042 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2044 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2045 gtk_tree_path_get_indices (path)[0]);
2046 gtk_widget_show (item);
2050 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2057 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2059 if (!combo_box->priv->popup_widget)
2062 index = gtk_tree_path_get_indices (path)[0];
2064 menu = combo_box->priv->popup_widget;
2065 g_return_if_fail (GTK_IS_MENU (menu));
2067 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2068 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2070 gtk_container_remove (GTK_CONTAINER (menu), item);
2074 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2080 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2082 gtk_combo_box_relayout (combo_box);
2086 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2091 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2094 if (!combo_box->priv->popup_widget)
2097 if (combo_box->priv->wrap_width)
2098 gtk_combo_box_relayout_item (combo_box,
2099 gtk_tree_path_get_indices (path)[0]);
2101 width = gtk_combo_box_calc_requested_width (combo_box, path);
2103 if (width > combo_box->priv->width)
2105 if (combo_box->priv->cell_view)
2107 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2108 gtk_widget_queue_resize (combo_box->priv->cell_view);
2110 combo_box->priv->width = width;
2119 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2122 GtkTreeSelection *sel;
2124 combo_box->priv->button = gtk_toggle_button_new ();
2125 gtk_widget_set_parent (combo_box->priv->button,
2126 GTK_BIN (combo_box)->child->parent);
2127 g_signal_connect (combo_box->priv->button, "button_press_event",
2128 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2129 g_signal_connect (combo_box->priv->button, "toggled",
2130 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2131 g_signal_connect_after (combo_box, "key_press_event",
2132 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2134 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2135 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2136 combo_box->priv->arrow);
2137 combo_box->priv->separator = NULL;
2138 gtk_widget_show_all (combo_box->priv->button);
2140 if (combo_box->priv->cell_view)
2142 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2143 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2144 GTK_BIN (combo_box)->child->parent);
2145 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2148 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2149 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2151 gtk_widget_show (combo_box->priv->cell_view_frame);
2154 combo_box->priv->tree_view = gtk_tree_view_new ();
2155 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2156 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2157 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2159 if (combo_box->priv->model)
2160 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2161 combo_box->priv->model);
2163 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2164 G_CALLBACK (gtk_combo_box_list_button_pressed),
2166 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2167 G_CALLBACK (gtk_combo_box_list_button_released),
2169 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2170 G_CALLBACK (gtk_combo_box_list_key_press),
2173 combo_box->priv->column = gtk_tree_view_column_new ();
2174 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2175 combo_box->priv->column);
2178 for (i = combo_box->priv->cells; i; i = i->next)
2181 ComboCellInfo *info = (ComboCellInfo *)i->data;
2183 if (info->pack == GTK_PACK_START)
2184 gtk_tree_view_column_pack_start (combo_box->priv->column,
2185 info->cell, info->expand);
2186 else if (info->pack == GTK_PACK_END)
2187 gtk_tree_view_column_pack_end (combo_box->priv->column,
2188 info->cell, info->expand);
2190 for (j = info->attributes; j; j = j->next->next)
2192 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2195 GPOINTER_TO_INT (j->next->data));
2199 if (combo_box->priv->active_item != -1)
2203 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2206 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2208 gtk_tree_path_free (path);
2212 /* set sample/popup widgets */
2213 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2215 gtk_widget_show (combo_box->priv->tree_view);
2219 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2221 /* disconnect signals */
2222 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2223 G_SIGNAL_MATCH_DATA,
2224 0, 0, NULL, NULL, combo_box);
2225 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2226 G_SIGNAL_MATCH_DATA,
2228 gtk_combo_box_list_button_pressed,
2231 /* destroy things (unparent will kill the latest ref from us)
2232 * last unref on button will destroy the arrow
2234 gtk_widget_unparent (combo_box->priv->button);
2235 combo_box->priv->button = NULL;
2236 combo_box->priv->arrow = NULL;
2238 if (combo_box->priv->cell_view)
2240 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2241 "background_set", FALSE,
2244 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2245 combo_box->priv->cell_view_frame = NULL;
2248 gtk_widget_destroy (combo_box->priv->tree_view);
2250 combo_box->priv->tree_view = NULL;
2251 combo_box->priv->popup_widget = NULL;
2256 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2258 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2259 gtk_grab_remove (combo_box->priv->tree_view);
2261 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2263 gtk_grab_remove (combo_box->priv->popup_window);
2264 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2265 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2270 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2271 GdkEventButton *event,
2274 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2276 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2278 if (ewidget == combo_box->priv->tree_view)
2281 if ((ewidget != combo_box->priv->button) ||
2282 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2285 gtk_combo_box_popup (combo_box);
2287 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2290 combo_box->priv->popup_in_progress = TRUE;
2296 gtk_combo_box_list_button_released (GtkWidget *widget,
2297 GdkEventButton *event,
2301 GtkTreePath *path = NULL;
2303 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2305 gboolean popup_in_progress = FALSE;
2307 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2309 if (combo_box->priv->popup_in_progress)
2311 popup_in_progress = TRUE;
2312 combo_box->priv->popup_in_progress = FALSE;
2315 if (ewidget != combo_box->priv->tree_view)
2317 if (ewidget == combo_box->priv->button &&
2318 !popup_in_progress &&
2319 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2321 gtk_combo_box_popdown (combo_box);
2325 /* released outside treeview */
2326 if (ewidget != combo_box->priv->button)
2328 gtk_combo_box_popdown (combo_box);
2336 /* select something cool */
2337 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2343 return TRUE; /* clicked outside window? */
2345 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2346 gtk_combo_box_popdown (combo_box);
2348 gtk_tree_path_free (path);
2354 gtk_combo_box_key_press (GtkWidget *widget,
2358 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2359 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2360 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2361 gint index = gtk_combo_box_get_active (combo_box);
2364 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2365 state == GDK_MOD1_MASK)
2367 gtk_combo_box_popup (combo_box);
2372 switch (event->keyval)
2376 new_index = index + 1;
2380 new_index = index - 1;
2383 case GDK_KP_Page_Up:
2389 case GDK_KP_Page_Down:
2392 new_index = items - 1;
2398 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2404 gtk_combo_box_menu_key_press (GtkWidget *widget,
2408 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2409 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2411 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2412 state == GDK_MOD1_MASK)
2414 gtk_combo_box_popdown (combo_box);
2423 gtk_combo_box_list_key_press (GtkWidget *widget,
2427 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2428 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2430 if (event->keyval == GDK_Escape ||
2431 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2432 state == GDK_MOD1_MASK))
2434 /* reset active item -- this is incredibly lame and ugly */
2435 gtk_combo_box_set_active (combo_box,
2436 gtk_combo_box_get_active (combo_box));
2438 gtk_combo_box_popdown (combo_box);
2443 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2444 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2448 GtkTreeModel *model = NULL;
2449 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2451 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2456 path = gtk_tree_model_get_path (model, &iter);
2459 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2460 gtk_tree_path_free (path);
2464 gtk_combo_box_popdown (combo_box);
2473 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2478 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2481 width = gtk_combo_box_calc_requested_width (combo_box, path);
2483 if (width > combo_box->priv->width)
2485 if (combo_box->priv->cell_view)
2487 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2488 gtk_widget_queue_resize (combo_box->priv->cell_view);
2490 combo_box->priv->width = width;
2495 * GtkCellLayout implementation
2498 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2499 GtkCellRenderer *cell,
2502 ComboCellInfo *info;
2503 GtkComboBox *combo_box;
2506 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2507 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2509 combo_box = GTK_COMBO_BOX (layout);
2511 g_object_ref (G_OBJECT (cell));
2512 gtk_object_sink (GTK_OBJECT (cell));
2514 info = g_new0 (ComboCellInfo, 1);
2516 info->expand = expand;
2517 info->pack = GTK_PACK_START;
2519 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2521 if (combo_box->priv->cell_view)
2522 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2525 if (combo_box->priv->column)
2526 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2528 menu = combo_box->priv->popup_widget;
2529 if (GTK_IS_MENU (menu))
2533 list = gtk_container_get_children (GTK_CONTAINER (menu));
2534 for (i = list; i; i = i->next)
2538 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2539 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2541 view = GTK_CELL_VIEW (i->data);
2543 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2550 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2551 GtkCellRenderer *cell,
2554 ComboCellInfo *info;
2555 GtkComboBox *combo_box;
2558 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2559 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2561 combo_box = GTK_COMBO_BOX (layout);
2563 g_object_ref (G_OBJECT (cell));
2564 gtk_object_sink (GTK_OBJECT (cell));
2566 info = g_new0 (ComboCellInfo, 1);
2568 info->expand = expand;
2569 info->pack = GTK_PACK_END;
2571 if (combo_box->priv->cell_view)
2572 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2575 if (combo_box->priv->column)
2576 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2578 menu = combo_box->priv->popup_widget;
2579 if (GTK_IS_MENU (menu))
2583 list = gtk_container_get_children (GTK_CONTAINER (menu));
2584 for (i = list; i; i = i->next)
2588 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2589 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2591 view = GTK_CELL_VIEW (i->data);
2593 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2600 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2603 GtkComboBox *combo_box;
2606 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2608 combo_box = GTK_COMBO_BOX (layout);
2610 if (combo_box->priv->cell_view)
2611 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2613 if (combo_box->priv->column)
2614 gtk_tree_view_column_clear (combo_box->priv->column);
2616 for (i = combo_box->priv->cells; i; i = i->next)
2618 ComboCellInfo *info = (ComboCellInfo *)i->data;
2620 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2621 g_object_unref (G_OBJECT (info->cell));
2625 g_slist_free (combo_box->priv->cells);
2626 combo_box->priv->cells = NULL;
2628 menu = combo_box->priv->popup_widget;
2629 if (GTK_IS_MENU (menu))
2633 list = gtk_container_get_children (GTK_CONTAINER (menu));
2634 for (i = list; i; i = i->next)
2638 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2639 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2641 view = GTK_CELL_VIEW (i->data);
2643 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2650 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2651 GtkCellRenderer *cell,
2652 const gchar *attribute,
2655 ComboCellInfo *info;
2656 GtkComboBox *combo_box;
2659 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2660 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2662 combo_box = GTK_COMBO_BOX (layout);
2664 info = gtk_combo_box_get_cell_info (combo_box, cell);
2666 info->attributes = g_slist_prepend (info->attributes,
2667 GINT_TO_POINTER (column));
2668 info->attributes = g_slist_prepend (info->attributes,
2669 g_strdup (attribute));
2671 if (combo_box->priv->cell_view)
2672 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2673 cell, attribute, column);
2675 if (combo_box->priv->column)
2676 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2677 cell, attribute, column);
2679 menu = combo_box->priv->popup_widget;
2680 if (GTK_IS_MENU (menu))
2684 list = gtk_container_get_children (GTK_CONTAINER (menu));
2685 for (i = list; i; i = i->next)
2689 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2690 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2692 view = GTK_CELL_VIEW (i->data);
2694 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2700 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2704 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2705 GtkCellRenderer *cell,
2706 GtkCellLayoutDataFunc func,
2708 GDestroyNotify destroy)
2710 ComboCellInfo *info;
2711 GtkComboBox *combo_box;
2714 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2716 combo_box = GTK_COMBO_BOX (layout);
2718 info = gtk_combo_box_get_cell_info (combo_box, cell);
2719 g_return_if_fail (info != NULL);
2723 GDestroyNotify d = info->destroy;
2725 info->destroy = NULL;
2726 d (info->func_data);
2730 info->func_data = func_data;
2731 info->destroy = destroy;
2733 if (combo_box->priv->cell_view)
2734 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2736 if (combo_box->priv->column)
2737 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2739 menu = combo_box->priv->popup_widget;
2740 if (GTK_IS_MENU (menu))
2744 list = gtk_container_get_children (GTK_CONTAINER (menu));
2745 for (i = list; i; i = i->next)
2749 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2750 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2752 view = GTK_CELL_VIEW (i->data);
2754 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2755 func, func_data, NULL);
2760 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2764 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2765 GtkCellRenderer *cell)
2767 ComboCellInfo *info;
2768 GtkComboBox *combo_box;
2772 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2773 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2775 combo_box = GTK_COMBO_BOX (layout);
2777 info = gtk_combo_box_get_cell_info (combo_box, cell);
2778 g_return_if_fail (info != NULL);
2780 list = info->attributes;
2781 while (list && list->next)
2783 g_free (list->data);
2784 list = list->next->next;
2786 g_slist_free (info->attributes);
2787 info->attributes = NULL;
2789 if (combo_box->priv->cell_view)
2790 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2792 if (combo_box->priv->column)
2793 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2795 menu = combo_box->priv->popup_widget;
2796 if (GTK_IS_MENU (menu))
2800 list = gtk_container_get_children (GTK_CONTAINER (menu));
2801 for (i = list; i; i = i->next)
2805 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2806 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2808 view = GTK_CELL_VIEW (i->data);
2810 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2815 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2819 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2820 GtkCellRenderer *cell,
2823 ComboCellInfo *info;
2824 GtkComboBox *combo_box;
2828 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2829 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2831 combo_box = GTK_COMBO_BOX (layout);
2833 info = gtk_combo_box_get_cell_info (combo_box, cell);
2835 g_return_if_fail (info != NULL);
2836 g_return_if_fail (position >= 0);
2838 link = g_slist_find (combo_box->priv->cells, info);
2840 g_return_if_fail (link != NULL);
2842 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2843 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2846 if (combo_box->priv->cell_view)
2847 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2850 if (combo_box->priv->column)
2851 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2854 menu = combo_box->priv->popup_widget;
2855 if (GTK_IS_MENU (menu))
2859 list = gtk_container_get_children (GTK_CONTAINER (menu));
2860 for (i = list; i; i = i->next)
2864 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2865 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2867 view = GTK_CELL_VIEW (i->data);
2869 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2874 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2882 * gtk_combo_box_new:
2884 * Creates a new empty #GtkComboBox.
2886 * Return value: A new #GtkComboBox.
2891 gtk_combo_box_new (void)
2893 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2897 * gtk_combo_box_new_with_model:
2898 * @model: A #GtkTreeModel.
2900 * Creates a new #GtkComboBox with the model initialized to @model.
2902 * Return value: A new #GtkComboBox.
2907 gtk_combo_box_new_with_model (GtkTreeModel *model)
2909 GtkComboBox *combo_box;
2911 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2913 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2917 return GTK_WIDGET (combo_box);
2921 * gtk_combo_box_set_wrap_width:
2922 * @combo_box: A #GtkComboBox.
2923 * @width: Preferred number of columns.
2925 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2926 * the preferred number of columns when you want to the popup to be layed out
2932 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2935 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2936 g_return_if_fail (width >= 0);
2938 if (width != combo_box->priv->wrap_width)
2940 combo_box->priv->wrap_width = width;
2942 gtk_combo_box_check_appearance (combo_box);
2943 gtk_combo_box_relayout (combo_box);
2945 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2950 * gtk_combo_box_set_row_span_column:
2951 * @combo_box: A #GtkComboBox.
2952 * @row_span: A column in the model passed during construction.
2954 * Sets the column with row span information for @combo_box to be @row_span.
2955 * The row span column contains integers which indicate how many rows
2956 * an item should span.
2961 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2966 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2968 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2969 g_return_if_fail (row_span >= 0 && row_span < col);
2971 if (row_span != combo_box->priv->row_column)
2973 combo_box->priv->row_column = row_span;
2975 gtk_combo_box_relayout (combo_box);
2977 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2982 * gtk_combo_box_set_column_span_column:
2983 * @combo_box: A #GtkComboBox.
2984 * @column_span: A column in the model passed during construction.
2986 * Sets the column with column span information for @combo_box to be
2987 * @column_span. The column span column contains integers which indicate
2988 * how many columns an item should span.
2993 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2998 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3000 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3001 g_return_if_fail (column_span >= 0 && column_span < col);
3003 if (column_span != combo_box->priv->col_column)
3005 combo_box->priv->col_column = column_span;
3007 gtk_combo_box_relayout (combo_box);
3009 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3014 * gtk_combo_box_get_active:
3015 * @combo_box: A #GtkComboBox.
3017 * Returns the index of the currently active item, or -1 if there's no
3020 * Return value: An integer which is the index of the currently active item, or
3021 * -1 if there's no active item.
3026 gtk_combo_box_get_active (GtkComboBox *combo_box)
3028 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3030 return combo_box->priv->active_item;
3034 * gtk_combo_box_set_active:
3035 * @combo_box: A #GtkComboBox.
3036 * @index: An index in the model passed during construction, or -1 to have
3039 * Sets the active item of @combo_box to be the item at @index.
3044 gtk_combo_box_set_active (GtkComboBox *combo_box,
3047 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3048 /* -1 means "no item selected" */
3049 g_return_if_fail (index >= -1);
3051 if (combo_box->priv->active_item == index)
3054 gtk_combo_box_set_active_internal (combo_box, index);
3058 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3063 combo_box->priv->active_item = index;
3067 if (combo_box->priv->tree_view)
3068 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3071 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3073 if (GTK_IS_MENU (menu))
3074 gtk_menu_set_active (menu, -1);
3077 if (combo_box->priv->cell_view)
3078 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3082 path = gtk_tree_path_new_from_indices (index, -1);
3084 if (combo_box->priv->tree_view)
3085 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3088 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3090 if (GTK_IS_MENU (menu))
3091 gtk_menu_set_active (GTK_MENU (menu), index);
3094 if (combo_box->priv->cell_view)
3095 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3097 gtk_tree_path_free (path);
3100 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3105 * gtk_combo_box_get_active_iter:
3106 * @combo_box: A #GtkComboBox
3107 * @iter: The uninitialized #GtkTreeIter.
3109 * Sets @iter to point to the current active item, if it exists.
3111 * Return value: %TRUE, if @iter was set
3116 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3123 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3125 active = gtk_combo_box_get_active (combo_box);
3129 path = gtk_tree_path_new_from_indices (active, -1);
3130 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3132 gtk_tree_path_free (path);
3138 * gtk_combo_box_set_active_iter:
3139 * @combo_box: A #GtkComboBox
3140 * @iter: The #GtkTreeIter.
3142 * Sets the current active item to be the one referenced by @iter.
3143 * @iter must correspond to a path of depth one.
3148 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3153 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3155 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3156 g_return_if_fail (path != NULL);
3157 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3159 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3160 gtk_tree_path_free (path);
3164 * gtk_combo_box_set_model:
3165 * @combo_box: A #GtkComboBox.
3166 * @model: A #GtkTreeModel.
3168 * Sets the model used by @combo_box to be @model. Will unset a
3169 * previously set model (if applicable).
3174 gtk_combo_box_set_model (GtkComboBox *combo_box,
3175 GtkTreeModel *model)
3177 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3178 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3180 if (model == combo_box->priv->model)
3183 if (combo_box->priv->model)
3184 gtk_combo_box_unset_model (combo_box);
3186 combo_box->priv->model = model;
3187 g_object_ref (G_OBJECT (combo_box->priv->model));
3189 combo_box->priv->inserted_id =
3190 g_signal_connect (combo_box->priv->model, "row_inserted",
3191 G_CALLBACK (gtk_combo_box_model_row_inserted),
3193 combo_box->priv->deleted_id =
3194 g_signal_connect (combo_box->priv->model, "row_deleted",
3195 G_CALLBACK (gtk_combo_box_model_row_deleted),
3197 combo_box->priv->reordered_id =
3198 g_signal_connect (combo_box->priv->model, "rows_reordered",
3199 G_CALLBACK (gtk_combo_box_model_rows_reordered),
3201 combo_box->priv->changed_id =
3202 g_signal_connect (combo_box->priv->model, "row_changed",
3203 G_CALLBACK (gtk_combo_box_model_row_changed),
3206 if (combo_box->priv->tree_view)
3209 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3210 combo_box->priv->model);
3215 if (combo_box->priv->popup_widget)
3216 gtk_combo_box_menu_fill (combo_box);
3220 if (combo_box->priv->cell_view)
3221 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3222 combo_box->priv->model);
3226 * gtk_combo_box_get_model
3227 * @combo_box: A #GtkComboBox.
3229 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3231 * Return value: A #GtkTreeModel which was passed during construction.
3236 gtk_combo_box_get_model (GtkComboBox *combo_box)
3238 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3240 return combo_box->priv->model;
3244 /* convenience API for simple text combos */
3247 * gtk_combo_box_new_text:
3249 * Convenience function which constructs a new text combo box, which is a
3250 * #GtkComboBox just displaying strings. If you use this function to create
3251 * a text combo box, you should only manipulate its data source with the
3252 * following convenience functions: gtk_combo_box_append_text(),
3253 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3254 * gtk_combo_box_remove_text().
3256 * Return value: A new text combo box.
3261 gtk_combo_box_new_text (void)
3263 GtkWidget *combo_box;
3264 GtkCellRenderer *cell;
3265 GtkListStore *store;
3267 store = gtk_list_store_new (1, G_TYPE_STRING);
3269 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3271 cell = gtk_cell_renderer_text_new ();
3272 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3273 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3281 * gtk_combo_box_append_text:
3282 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3285 * Appends @string to the list of strings stored in @combo_box. Note that
3286 * you can only use this function with combo boxes constructed with
3287 * gtk_combo_box_new_text().
3292 gtk_combo_box_append_text (GtkComboBox *combo_box,
3296 GtkListStore *store;
3298 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3299 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3300 g_return_if_fail (text != NULL);
3302 store = GTK_LIST_STORE (combo_box->priv->model);
3304 gtk_list_store_append (store, &iter);
3305 gtk_list_store_set (store, &iter, 0, text, -1);
3309 * gtk_combo_box_insert_text:
3310 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3311 * @position: An index to insert @text.
3314 * Inserts @string at @position in the list of strings stored in @combo_box.
3315 * Note that you can only use this function with combo boxes constructed
3316 * with gtk_combo_box_new_text().
3321 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3326 GtkListStore *store;
3328 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3329 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3330 g_return_if_fail (position >= 0);
3331 g_return_if_fail (text != NULL);
3333 store = GTK_LIST_STORE (combo_box->priv->model);
3335 gtk_list_store_insert (store, &iter, position);
3336 gtk_list_store_set (store, &iter, 0, text, -1);
3340 * gtk_combo_box_prepend_text:
3341 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3344 * Prepends @string to the list of strings stored in @combo_box. Note that
3345 * you can only use this function with combo boxes constructed with
3346 * gtk_combo_box_new_text().
3351 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3355 GtkListStore *store;
3357 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3358 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3359 g_return_if_fail (text != NULL);
3361 store = GTK_LIST_STORE (combo_box->priv->model);
3363 gtk_list_store_prepend (store, &iter);
3364 gtk_list_store_set (store, &iter, 0, text, -1);
3368 * gtk_combo_box_remove_text:
3369 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3370 * @position: Index of the item to remove.
3372 * Removes the string at @position from @combo_box. Note that you can only use
3373 * this function with combo boxes constructed with gtk_combo_box_new_text().
3378 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3382 GtkListStore *store;
3384 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3385 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3386 g_return_if_fail (position >= 0);
3388 store = GTK_LIST_STORE (combo_box->priv->model);
3390 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3392 gtk_list_store_remove (store, &iter);
3397 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3398 gboolean group_cycling)
3400 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3402 gtk_widget_grab_focus (combo_box->priv->button);
3408 gtk_combo_box_destroy (GtkObject *object)
3410 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3412 combo_box->priv->destroying = 1;
3414 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3416 combo_box->priv->cell_view = NULL;
3418 combo_box->priv->destroying = 0;
3422 gtk_combo_box_finalize (GObject *object)
3424 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3427 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3428 gtk_combo_box_menu_destroy (combo_box);
3430 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3431 gtk_combo_box_list_destroy (combo_box);
3433 if (combo_box->priv->popup_window)
3434 gtk_widget_destroy (combo_box->priv->popup_window);
3436 gtk_combo_box_unset_model (combo_box);
3438 if (combo_box->priv->model)
3439 g_object_unref (combo_box->priv->model);
3441 for (i = combo_box->priv->cells; i; i = i->next)
3443 ComboCellInfo *info = (ComboCellInfo *)i->data;
3444 GSList *list = info->attributes;
3447 info->destroy (info->func_data);
3449 while (list && list->next)
3451 g_free (list->data);
3452 list = list->next->next;
3454 g_slist_free (info->attributes);
3456 g_object_unref (G_OBJECT (info->cell));
3459 g_slist_free (combo_box->priv->cells);
3461 G_OBJECT_CLASS (parent_class)->finalize (object);