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;
104 /* While debugging this evil code, I have learned that
105 * there are actually 4 modes to this widget, which can
106 * be characterized as follows
108 * 1) menu mode, no child added
111 * cell_view -> GtkCellView, regular child
112 * cell_view_frame -> NULL
113 * button -> GtkToggleButton set_parent to combo
114 * arrow -> GtkArrow set_parent to button
115 * separator -> GtkVSepator set_parent to button
116 * popup_widget -> GtkMenu
117 * popup_window -> NULL
118 * popup_frame -> NULL
120 * 2) menu mode, child added
124 * cell_view_frame -> NULL
125 * button -> GtkToggleButton set_parent to combo
126 * arrow -> GtkArrow, child of button
128 * popup_widget -> GtkMenu
129 * popup_window -> NULL
130 * popup_frame -> NULL
132 * 3) list mode, no child added
134 * tree_view -> GtkTreeView, child of popup_frame
135 * cell_view -> GtkCellView, regular child
136 * cell_view_frame -> GtkFrame, set parent to combo
137 * button -> GtkToggleButton, set_parent to combo
138 * arrow -> GtkArrow, child of button
140 * popup_widget -> tree_view
141 * popup_window -> GtkWindow
142 * popup_frame -> GtkFrame, child of popup_window
144 * 4) list mode, child added
146 * tree_view -> GtkTreeView, child of popup_frame
148 * cell_view_frame -> NULL
149 * button -> GtkToggleButton, set_parent to combo
150 * arrow -> GtkArrow, child of button
152 * popup_widget -> tree_view
153 * popup_window -> GtkWindow
154 * popup_frame -> GtkFrame, child of popup_window
167 PROP_ROW_SPAN_COLUMN,
168 PROP_COLUMN_SPAN_COLUMN,
172 static GtkBinClass *parent_class = NULL;
173 static guint combo_box_signals[LAST_SIGNAL] = {0,};
175 #define BONUS_PADDING 4
179 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
180 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
181 static void gtk_combo_box_init (GtkComboBox *combo_box);
182 static void gtk_combo_box_finalize (GObject *object);
183 static void gtk_combo_box_destroy (GtkObject *object);
185 static void gtk_combo_box_set_property (GObject *object,
189 static void gtk_combo_box_get_property (GObject *object,
194 static void gtk_combo_box_state_changed (GtkWidget *widget,
195 GtkStateType previous);
196 static void gtk_combo_box_style_set (GtkWidget *widget,
198 static void gtk_combo_box_button_toggled (GtkWidget *widget,
200 static void gtk_combo_box_add (GtkContainer *container,
203 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
204 GtkCellRenderer *cell);
206 static void gtk_combo_box_menu_show (GtkWidget *menu,
208 static void gtk_combo_box_menu_hide (GtkWidget *menu,
211 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
213 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
218 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
223 static void gtk_combo_box_menu_position (GtkMenu *menu,
229 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
231 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
233 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
235 static void gtk_combo_box_size_request (GtkWidget *widget,
236 GtkRequisition *requisition);
237 static void gtk_combo_box_size_allocate (GtkWidget *widget,
238 GtkAllocation *allocation);
239 static void gtk_combo_box_forall (GtkContainer *container,
240 gboolean include_internals,
241 GtkCallback callback,
242 gpointer callback_data);
243 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
244 GdkEventExpose *event);
245 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
246 GdkEventScroll *event);
247 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
249 static gboolean gtk_combo_box_key_press (GtkWidget *widget,
253 /* listening to the model */
254 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
258 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
261 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
266 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
272 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
277 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
278 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
280 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
282 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
283 GdkEventButton *event,
285 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
288 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
289 GdkEventButton *event,
292 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
298 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
299 gboolean add_children);
300 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
301 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
303 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
307 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
309 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
311 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
312 GdkEventButton *event,
314 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
316 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
320 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
323 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
328 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
332 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
337 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
338 GtkCellRenderer *cell,
340 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
341 GtkCellRenderer *cell,
343 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
344 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
345 GtkCellRenderer *cell,
346 const gchar *attribute,
348 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
349 GtkCellRenderer *cell,
350 GtkCellLayoutDataFunc func,
352 GDestroyNotify destroy);
353 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
354 GtkCellRenderer *cell);
355 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
356 GtkCellRenderer *cell,
358 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
359 gboolean group_cycling);
363 gtk_combo_box_get_type (void)
365 static GType combo_box_type = 0;
369 static const GTypeInfo combo_box_info =
371 sizeof (GtkComboBoxClass),
372 NULL, /* base_init */
373 NULL, /* base_finalize */
374 (GClassInitFunc) gtk_combo_box_class_init,
375 NULL, /* class_finalize */
376 NULL, /* class_data */
377 sizeof (GtkComboBox),
379 (GInstanceInitFunc) gtk_combo_box_init
382 static const GInterfaceInfo cell_layout_info =
384 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
389 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
394 g_type_add_interface_static (combo_box_type,
395 GTK_TYPE_CELL_LAYOUT,
399 return combo_box_type;
404 gtk_combo_box_class_init (GtkComboBoxClass *klass)
406 GObjectClass *object_class;
407 GtkBindingSet *binding_set;
408 GtkObjectClass *gtk_object_class;
409 GtkContainerClass *container_class;
410 GtkWidgetClass *widget_class;
412 binding_set = gtk_binding_set_by_class (klass);
414 container_class = (GtkContainerClass *)klass;
415 container_class->forall = gtk_combo_box_forall;
416 container_class->add = gtk_combo_box_add;
418 widget_class = (GtkWidgetClass *)klass;
419 widget_class->size_allocate = gtk_combo_box_size_allocate;
420 widget_class->size_request = gtk_combo_box_size_request;
421 widget_class->expose_event = gtk_combo_box_expose_event;
422 widget_class->scroll_event = gtk_combo_box_scroll_event;
423 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
424 widget_class->style_set = gtk_combo_box_style_set;
425 widget_class->state_changed = gtk_combo_box_state_changed;
427 gtk_object_class = (GtkObjectClass *)klass;
428 gtk_object_class->destroy = gtk_combo_box_destroy;
430 object_class = (GObjectClass *)klass;
431 object_class->finalize = gtk_combo_box_finalize;
432 object_class->set_property = gtk_combo_box_set_property;
433 object_class->get_property = gtk_combo_box_get_property;
435 parent_class = g_type_class_peek_parent (klass);
438 combo_box_signals[CHANGED] =
439 g_signal_new ("changed",
440 G_OBJECT_CLASS_TYPE (klass),
442 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
444 g_cclosure_marshal_VOID__VOID,
448 g_object_class_install_property (object_class,
450 g_param_spec_object ("model",
451 P_("ComboBox model"),
452 P_("The model for the combo box"),
456 g_object_class_install_property (object_class,
458 g_param_spec_int ("wrap_width",
460 P_("Wrap width for layouting the items in a grid"),
466 g_object_class_install_property (object_class,
467 PROP_ROW_SPAN_COLUMN,
468 g_param_spec_int ("row_span_column",
469 P_("Row span column"),
470 P_("TreeModel column containing the row span values"),
476 g_object_class_install_property (object_class,
477 PROP_COLUMN_SPAN_COLUMN,
478 g_param_spec_int ("column_span_column",
479 P_("Column span column"),
480 P_("TreeModel column containing the column span values"),
486 g_object_class_install_property (object_class,
488 g_param_spec_int ("active",
490 P_("The item which is currently active"),
496 gtk_widget_class_install_style_property (widget_class,
497 g_param_spec_boolean ("appears-as-list",
498 P_("Appears as list"),
499 P_("Whether combobox dropdowns should look like lists rather than menus"),
503 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
507 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
509 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
510 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
511 iface->clear = gtk_combo_box_cell_layout_clear;
512 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
513 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
514 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
515 iface->reorder = gtk_combo_box_cell_layout_reorder;
519 gtk_combo_box_init (GtkComboBox *combo_box)
521 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
523 combo_box->priv->cell_view = gtk_cell_view_new ();
524 gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
525 gtk_widget_show (combo_box->priv->cell_view);
527 combo_box->priv->width = 0;
528 combo_box->priv->wrap_width = 0;
530 combo_box->priv->active_item = -1;
531 combo_box->priv->col_column = -1;
532 combo_box->priv->row_column = -1;
536 gtk_combo_box_set_property (GObject *object,
541 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
546 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
549 case PROP_WRAP_WIDTH:
550 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
553 case PROP_ROW_SPAN_COLUMN:
554 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
557 case PROP_COLUMN_SPAN_COLUMN:
558 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
562 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
571 gtk_combo_box_get_property (GObject *object,
576 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
581 g_value_set_object (value, combo_box->priv->model);
584 case PROP_WRAP_WIDTH:
585 g_value_set_int (value, combo_box->priv->wrap_width);
588 case PROP_ROW_SPAN_COLUMN:
589 g_value_set_int (value, combo_box->priv->row_column);
592 case PROP_COLUMN_SPAN_COLUMN:
593 g_value_set_int (value, combo_box->priv->col_column);
597 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
607 gtk_combo_box_state_changed (GtkWidget *widget,
608 GtkStateType previous)
610 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
612 if (GTK_WIDGET_REALIZED (widget))
614 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
615 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
616 &widget->style->base[GTK_WIDGET_STATE (widget)]);
619 gtk_widget_queue_draw (widget);
623 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
625 gboolean appears_as_list;
627 /* if wrap_width > 0, then we are in grid-mode and forced to use
630 if (combo_box->priv->wrap_width)
631 appears_as_list = FALSE;
633 gtk_widget_style_get (GTK_WIDGET (combo_box),
634 "appears-as-list", &appears_as_list,
639 /* Destroy all the menu mode widgets, if they exist. */
640 if (GTK_IS_MENU (combo_box->priv->popup_widget))
641 gtk_combo_box_menu_destroy (combo_box);
643 /* Create the list mode widgets, if they don't already exist. */
644 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
645 gtk_combo_box_list_setup (combo_box);
649 /* Destroy all the list mode widgets, if they exist. */
650 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
651 gtk_combo_box_list_destroy (combo_box);
653 /* Create the menu mode widgets, if they don't already exist. */
654 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
655 gtk_combo_box_menu_setup (combo_box, TRUE);
660 gtk_combo_box_style_set (GtkWidget *widget,
663 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
665 gtk_combo_box_check_appearance (combo_box);
667 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
668 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
669 &widget->style->base[GTK_WIDGET_STATE (widget)]);
673 gtk_combo_box_button_toggled (GtkWidget *widget,
676 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
678 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
680 if (!combo_box->priv->popup_in_progress)
681 gtk_combo_box_popup (combo_box);
684 gtk_combo_box_popdown (combo_box);
688 gtk_combo_box_add (GtkContainer *container,
691 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
693 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
694 gtk_container_remove (container, combo_box->priv->cell_view);
696 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
698 if (combo_box->priv->cell_view &&
699 widget != combo_box->priv->cell_view)
701 /* since the cell_view was unparented, it's gone now */
702 combo_box->priv->cell_view = NULL;
704 if (!combo_box->priv->tree_view && combo_box->priv->separator)
706 gtk_widget_unparent (combo_box->priv->separator);
707 combo_box->priv->separator = NULL;
709 g_object_ref (G_OBJECT (combo_box->priv->arrow));
710 gtk_widget_unparent (combo_box->priv->arrow);
711 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
712 combo_box->priv->arrow);
713 g_object_unref (G_OBJECT (combo_box->priv->arrow));
715 gtk_widget_queue_resize (GTK_WIDGET (container));
717 else if (combo_box->priv->cell_view_frame)
719 gtk_widget_unparent (combo_box->priv->cell_view_frame);
720 combo_box->priv->cell_view_frame = NULL;
725 static ComboCellInfo *
726 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
727 GtkCellRenderer *cell)
731 for (i = combo_box->priv->cells; i; i = i->next)
733 ComboCellInfo *info = (ComboCellInfo *)i->data;
735 if (info && info->cell == cell)
743 gtk_combo_box_menu_show (GtkWidget *menu,
746 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
748 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
750 combo_box->priv->popup_in_progress = FALSE;
754 gtk_combo_box_menu_hide (GtkWidget *menu,
757 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
759 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
764 gtk_combo_box_detacher (GtkWidget *widget,
767 GtkComboBox *combo_box;
769 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
771 combo_box = GTK_COMBO_BOX (widget);
772 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
774 g_signal_handlers_disconnect_by_func (menu,
775 gtk_combo_box_menu_show,
777 g_signal_handlers_disconnect_by_func (menu,
778 gtk_combo_box_menu_hide,
781 combo_box->priv->popup_widget = NULL;
785 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
788 if (GTK_IS_MENU (combo_box->priv->popup_widget))
790 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
791 combo_box->priv->popup_widget = NULL;
793 else if (combo_box->priv->popup_widget)
795 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
796 combo_box->priv->popup_widget);
797 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
798 combo_box->priv->popup_widget = NULL;
801 if (GTK_IS_MENU (popup))
803 if (combo_box->priv->popup_window)
805 gtk_widget_destroy (combo_box->priv->popup_window);
806 combo_box->priv->popup_window = NULL;
807 combo_box->priv->popup_frame = NULL;
810 combo_box->priv->popup_widget = popup;
812 g_signal_connect (popup, "show",
813 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
814 g_signal_connect (popup, "hide",
815 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
817 gtk_menu_attach_to_widget (GTK_MENU (popup),
818 GTK_WIDGET (combo_box),
819 gtk_combo_box_detacher);
823 if (!combo_box->priv->popup_window)
825 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
826 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
827 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
828 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
830 combo_box->priv->popup_frame = gtk_frame_new (NULL);
831 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
832 GTK_SHADOW_ETCHED_IN);
833 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
834 combo_box->priv->popup_frame);
836 gtk_widget_show (combo_box->priv->popup_frame);
839 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
841 gtk_widget_show (popup);
842 g_object_ref (G_OBJECT (popup));
843 combo_box->priv->popup_widget = popup;
848 gtk_combo_box_menu_position_below (GtkMenu *menu,
857 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
859 /* FIXME: is using the size request here broken? */
860 child = GTK_BIN (combo_box)->child;
862 gdk_window_get_origin (child->window, &sx, &sy);
864 gtk_widget_size_request (GTK_WIDGET (menu), &req);
866 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
869 *x = sx + child->allocation.width - req.width;
870 *y = sy + child->allocation.height;
872 if (GTK_WIDGET_NO_WINDOW (child))
874 *x += child->allocation.x;
875 *y += child->allocation.y;
882 gtk_combo_box_menu_position_over (GtkMenu *menu,
888 GtkComboBox *combo_box;
892 GtkRequisition requisition;
899 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
901 combo_box = GTK_COMBO_BOX (user_data);
902 widget = GTK_WIDGET (combo_box);
904 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
905 menu_width = requisition.width;
907 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
908 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
910 menu_xpos += widget->allocation.x;
911 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
915 gtk_widget_get_child_requisition (active, &requisition);
916 menu_ypos -= requisition.height / 2;
919 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
922 child = children->data;
927 if (GTK_WIDGET_VISIBLE (child))
929 gtk_widget_get_child_requisition (child, &requisition);
930 menu_ypos -= requisition.height;
933 children = children->next;
936 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
937 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
939 /* Clamp the position on screen */
940 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
944 else if ((menu_xpos + menu_width) > screen_width)
945 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
954 gtk_combo_box_menu_position (GtkMenu *menu,
960 GtkComboBox *combo_box;
961 GtkWidget *menu_item;
963 combo_box = GTK_COMBO_BOX (user_data);
965 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
966 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
969 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
971 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
974 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
980 gtk_combo_box_list_position (GtkComboBox *combo_box,
989 GdkRectangle monitor;
990 GtkRequisition popup_req;
992 sample = GTK_BIN (combo_box)->child;
994 *width = sample->allocation.width;
995 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
996 *height = popup_req.height;
998 gdk_window_get_origin (sample->window, x, y);
1000 if (combo_box->priv->cell_view_frame)
1002 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1003 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1004 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1005 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1008 if (GTK_WIDGET_NO_WINDOW (sample))
1010 *x += sample->allocation.x;
1011 *y += sample->allocation.y;
1014 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1015 monitor_num = gdk_screen_get_monitor_at_window (screen,
1016 GTK_WIDGET (combo_box)->window);
1017 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1021 else if (*x + *width > monitor.x + monitor.width)
1022 *x = monitor.x + monitor.width - *width;
1024 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1025 *y += sample->allocation.height;
1031 * gtk_combo_box_popup:
1032 * @combo_box: a #GtkComboBox
1034 * Pops up the menu or dropdown list of @combo_box.
1036 * This function is mostly intended for use by accessibility technologies;
1037 * applications should have little use for it.
1042 gtk_combo_box_popup (GtkComboBox *combo_box)
1044 gint x, y, width, height;
1046 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1048 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1051 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1053 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1054 combo_box->priv->active_item);
1056 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1058 gtk_combo_box_menu_position, combo_box,
1063 gtk_widget_show_all (combo_box->priv->popup_frame);
1064 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1066 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1067 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1070 gtk_widget_show (combo_box->priv->popup_window);
1072 gtk_widget_grab_focus (combo_box->priv->popup_window);
1073 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1076 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1078 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1079 FALSE, GDK_CURRENT_TIME);
1080 gtk_widget_grab_focus (combo_box->priv->tree_view);
1083 gtk_grab_add (combo_box->priv->popup_window);
1084 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1085 GDK_BUTTON_PRESS_MASK |
1086 GDK_BUTTON_RELEASE_MASK |
1087 GDK_POINTER_MOTION_MASK,
1088 NULL, NULL, GDK_CURRENT_TIME);
1090 gtk_grab_add (combo_box->priv->tree_view);
1094 * gtk_combo_box_popdown:
1095 * @combo_box: a #GtkComboBox
1097 * Hides the menu or dropdown list of @combo_box.
1099 * This function is mostly intended for use by accessibility technologies;
1100 * applications should have little use for it.
1105 gtk_combo_box_popdown (GtkComboBox *combo_box)
1107 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1109 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1111 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1115 gtk_combo_box_list_remove_grabs (combo_box);
1116 gtk_widget_hide_all (combo_box->priv->popup_window);
1117 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1122 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1128 if (combo_box->priv->cell_view)
1129 gtk_widget_style_get (combo_box->priv->cell_view,
1130 "focus-line-width", &padding,
1135 /* add some pixels for good measure */
1136 padding += BONUS_PADDING;
1138 if (combo_box->priv->cell_view)
1139 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1144 return req.width + padding;
1148 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1154 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1157 combo_box->priv->width = 0;
1159 path = gtk_tree_path_new_from_indices (0, -1);
1161 if (combo_box->priv->cell_view)
1162 gtk_widget_style_get (combo_box->priv->cell_view,
1163 "focus-line-width", &padding,
1168 /* add some pixels for good measure */
1169 padding += BONUS_PADDING;
1175 if (combo_box->priv->cell_view)
1176 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1181 combo_box->priv->width = MAX (combo_box->priv->width,
1182 req.width + padding);
1184 gtk_tree_path_next (path);
1186 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1188 gtk_tree_path_free (path);
1192 gtk_combo_box_size_request (GtkWidget *widget,
1193 GtkRequisition *requisition)
1196 GtkRequisition bin_req;
1198 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1201 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1202 gtk_combo_box_remeasure (combo_box);
1203 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1205 if (!combo_box->priv->tree_view)
1209 if (combo_box->priv->cell_view)
1211 GtkRequisition button_req, sep_req, arrow_req;
1212 gint border_width, xthickness, ythickness;
1214 gtk_widget_size_request (combo_box->priv->button, &button_req);
1215 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1216 xthickness = combo_box->priv->button->style->xthickness;
1217 ythickness = combo_box->priv->button->style->ythickness;
1219 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1221 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1222 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1224 height = MAX (sep_req.height, arrow_req.height);
1225 height = MAX (height, bin_req.height);
1227 width = bin_req.width + sep_req.width + arrow_req.width;
1229 height += border_width + 1 + ythickness * 2 + 4;
1230 width += border_width + 1 + xthickness * 2 + 4;
1232 requisition->width = width;
1233 requisition->height = height;
1237 GtkRequisition but_req;
1239 gtk_widget_size_request (combo_box->priv->button, &but_req);
1241 requisition->width = bin_req.width + but_req.width;
1242 requisition->height = MAX (bin_req.height, but_req.height);
1248 GtkRequisition button_req, frame_req;
1250 /* sample + frame */
1251 *requisition = bin_req;
1253 if (combo_box->priv->cell_view_frame)
1255 gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
1256 requisition->width += 2 *
1257 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1258 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1259 requisition->height += 2 *
1260 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1261 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1265 gtk_widget_size_request (combo_box->priv->button, &button_req);
1267 requisition->height = MAX (requisition->height, button_req.height);
1268 requisition->width += button_req.width;
1273 gtk_combo_box_size_allocate (GtkWidget *widget,
1274 GtkAllocation *allocation)
1276 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1277 GtkAllocation child;
1279 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1281 widget->allocation = *allocation;
1283 if (!combo_box->priv->tree_view)
1285 if (combo_box->priv->cell_view)
1287 gint border_width, xthickness, ythickness;
1291 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1293 /* set some things ready */
1294 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1295 xthickness = combo_box->priv->button->style->xthickness;
1296 ythickness = combo_box->priv->button->style->ythickness;
1298 child.x = allocation->x + border_width + 1 + xthickness + 2;
1299 child.y = allocation->y + border_width + 1 + ythickness + 2;
1301 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1303 /* handle the children */
1304 gtk_widget_size_request (combo_box->priv->arrow, &req);
1305 child.width = req.width;
1306 child.height = allocation->height - 2 * (child.y - allocation->y);
1308 child.x += width - req.width;
1309 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1311 child.x += req.width;
1312 gtk_widget_size_request (combo_box->priv->separator, &req);
1313 child.width = req.width;
1315 child.x -= req.width;
1316 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1320 child.x += req.width;
1321 child.width = allocation->x + allocation->width
1322 - (border_width + 1 + xthickness + 2) - child.x;
1326 child.width = child.x;
1327 child.x = allocation->x + border_width + 1 + xthickness + 2;
1328 child.width -= child.x;
1331 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1335 gtk_widget_size_request (combo_box->priv->button, &req);
1337 child.x = allocation->x;
1339 child.x = allocation->x + allocation->width - req.width;
1340 child.y = allocation->y;
1341 child.width = req.width;
1342 child.height = allocation->height;
1343 gtk_widget_size_allocate (combo_box->priv->button, &child);
1346 child.x = allocation->x + req.width;
1348 child.x = allocation->x;
1349 child.y = allocation->y;
1350 child.width = allocation->width - req.width;
1351 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1359 gtk_widget_size_request (combo_box->priv->button, &req);
1361 child.x = allocation->x;
1363 child.x = allocation->x + allocation->width - req.width;
1364 child.y = allocation->y;
1365 child.width = req.width;
1366 child.height = allocation->height;
1367 gtk_widget_size_allocate (combo_box->priv->button, &child);
1371 child.x = allocation->x + req.width;
1373 child.x = allocation->x;
1374 child.y = allocation->y;
1375 child.width = allocation->width - req.width;
1376 child.height = allocation->height;
1378 if (combo_box->priv->cell_view_frame)
1380 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1384 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1385 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1387 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1388 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1389 child.width -= 2 * (
1390 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1391 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1392 child.height -= 2 * (
1393 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1394 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1397 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1402 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1404 if (combo_box->priv->model)
1406 g_signal_handler_disconnect (combo_box->priv->model,
1407 combo_box->priv->inserted_id);
1408 g_signal_handler_disconnect (combo_box->priv->model,
1409 combo_box->priv->deleted_id);
1410 g_signal_handler_disconnect (combo_box->priv->model,
1411 combo_box->priv->reordered_id);
1412 g_signal_handler_disconnect (combo_box->priv->model,
1413 combo_box->priv->changed_id);
1417 if (!combo_box->priv->tree_view)
1419 if (combo_box->priv->popup_widget)
1420 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1421 (GtkCallback)gtk_widget_destroy, NULL);
1424 g_object_unref (G_OBJECT (combo_box->priv->model));
1425 combo_box->priv->model = NULL;
1429 gtk_combo_box_forall (GtkContainer *container,
1430 gboolean include_internals,
1431 GtkCallback callback,
1432 gpointer callback_data)
1434 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1436 if (include_internals)
1438 if (combo_box->priv->button)
1439 (* callback) (combo_box->priv->button, callback_data);
1440 if (combo_box->priv->separator)
1441 (* callback) (combo_box->priv->separator, callback_data);
1442 if (combo_box->priv->arrow)
1443 (* callback) (combo_box->priv->arrow, callback_data);
1444 if (combo_box->priv->cell_view_frame)
1445 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1448 if (GTK_BIN (container)->child)
1449 (* callback) (GTK_BIN (container)->child, callback_data);
1453 gtk_combo_box_expose_event (GtkWidget *widget,
1454 GdkEventExpose *event)
1456 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1458 if (!combo_box->priv->tree_view)
1460 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1461 combo_box->priv->button, event);
1463 if (combo_box->priv->separator)
1465 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1466 combo_box->priv->separator, event);
1468 /* if not in this case, arrow gets its expose event from button */
1469 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1470 combo_box->priv->arrow, event);
1475 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1476 combo_box->priv->button, event);
1478 if (combo_box->priv->cell_view_frame)
1479 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1480 combo_box->priv->cell_view_frame, event);
1483 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1484 GTK_BIN (widget)->child, event);
1490 gtk_combo_box_scroll_event (GtkWidget *widget,
1491 GdkEventScroll *event)
1493 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1497 index = gtk_combo_box_get_active (combo_box);
1501 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1503 if (event->direction == GDK_SCROLL_UP)
1508 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1519 cell_view_sync_cells (GtkComboBox *combo_box,
1520 GtkCellView *cell_view)
1524 for (k = combo_box->priv->cells; k; k = k->next)
1527 ComboCellInfo *info = (ComboCellInfo *)k->data;
1529 if (info->pack == GTK_PACK_START)
1530 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1531 info->cell, info->expand);
1532 else if (info->pack == GTK_PACK_END)
1533 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1534 info->cell, info->expand);
1536 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1538 info->func, info->func_data, NULL);
1540 for (j = info->attributes; j; j = j->next->next)
1542 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1545 GPOINTER_TO_INT (j->next->data));
1551 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1552 gboolean add_children)
1556 if (combo_box->priv->cell_view)
1558 combo_box->priv->button = gtk_toggle_button_new ();
1559 g_signal_connect (combo_box->priv->button, "toggled",
1560 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1561 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1562 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1563 gtk_widget_set_parent (combo_box->priv->button,
1564 GTK_BIN (combo_box)->child->parent);
1566 combo_box->priv->separator = gtk_vseparator_new ();
1567 gtk_widget_set_parent (combo_box->priv->separator,
1568 combo_box->priv->button);
1569 gtk_widget_show (combo_box->priv->separator);
1571 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1572 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1573 gtk_widget_show (combo_box->priv->arrow);
1575 gtk_widget_show_all (combo_box->priv->button);
1577 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1579 /* I have no clue why, but we need to manually map in this case. */
1580 gtk_widget_map (combo_box->priv->separator);
1581 gtk_widget_map (combo_box->priv->arrow);
1586 combo_box->priv->button = gtk_toggle_button_new ();
1587 g_signal_connect (combo_box->priv->button, "toggled",
1588 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1589 g_signal_connect_after (combo_box, "key_press_event",
1590 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1591 gtk_widget_set_parent (combo_box->priv->button,
1592 GTK_BIN (combo_box)->child->parent);
1594 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1595 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1596 combo_box->priv->arrow);
1597 gtk_widget_show_all (combo_box->priv->button);
1600 g_signal_connect (combo_box->priv->button, "button_press_event",
1601 G_CALLBACK (gtk_combo_box_menu_button_press),
1604 /* create our funky menu */
1605 box = gtk_menu_new ();
1606 g_signal_connect (box, "key_press_event",
1607 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1608 gtk_combo_box_set_popup_widget (combo_box, box);
1612 gtk_combo_box_menu_fill (combo_box);
1617 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1623 if (!combo_box->priv->model)
1626 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1627 menu = combo_box->priv->popup_widget;
1629 for (i = 0; i < items; i++)
1633 path = gtk_tree_path_new_from_indices (i, -1);
1634 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1636 g_signal_connect (tmp, "activate",
1637 G_CALLBACK (gtk_combo_box_menu_item_activate),
1640 cell_view_sync_cells (combo_box,
1641 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1643 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1645 if (combo_box->priv->wrap_width)
1646 gtk_combo_box_relayout_item (combo_box, i);
1648 gtk_widget_show (tmp);
1650 gtk_tree_path_free (path);
1655 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1657 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1658 G_SIGNAL_MATCH_DATA,
1660 gtk_combo_box_menu_button_press, NULL);
1662 /* unparent will remove our latest ref */
1663 if (combo_box->priv->cell_view)
1665 gtk_widget_unparent (combo_box->priv->arrow);
1666 combo_box->priv->arrow = NULL;
1668 gtk_widget_unparent (combo_box->priv->separator);
1669 combo_box->priv->separator = NULL;
1671 gtk_widget_unparent (combo_box->priv->button);
1672 combo_box->priv->button = NULL;
1676 /* will destroy the arrow too */
1677 gtk_widget_unparent (combo_box->priv->button);
1679 combo_box->priv->button = NULL;
1680 combo_box->priv->arrow = NULL;
1683 /* changing the popup window will unref the menu and the children */
1691 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1698 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1702 if (combo_box->priv->col_column == -1)
1705 gtk_tree_model_get (combo_box->priv->model, &iter,
1706 combo_box->priv->col_column, cols,
1712 if (combo_box->priv->row_column == -1)
1715 gtk_tree_model_get (combo_box->priv->model, &iter,
1716 combo_box->priv->row_column, rows,
1722 menu_occupied (GtkMenu *menu,
1726 guint bottom_attach)
1730 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1731 g_return_val_if_fail (left_attach < right_attach, TRUE);
1732 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1734 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1737 gboolean h_intersect = FALSE;
1738 gboolean v_intersect = FALSE;
1740 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1743 "bottom_attach", &b,
1747 /* look if this item intersects with the given coordinates */
1748 h_intersect = left_attach <= l && l <= right_attach;
1749 h_intersect &= left_attach <= r && r <= right_attach;
1751 v_intersect = top_attach <= t && t <= bottom_attach;
1752 v_intersect &= top_attach <= b && b <= bottom_attach;
1754 if (h_intersect && v_intersect)
1762 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1765 gint current_col = 0, current_row = 0;
1771 menu = combo_box->priv->popup_widget;
1772 if (!GTK_IS_MENU_SHELL (menu))
1775 list = gtk_container_get_children (GTK_CONTAINER (menu));
1776 item = g_list_nth_data (list, index);
1779 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1781 /* look for a good spot */
1784 if (current_col + cols > combo_box->priv->wrap_width)
1790 if (!menu_occupied (GTK_MENU (menu),
1791 current_col, current_col + cols,
1792 current_row, current_row + rows))
1798 /* set attach props */
1799 gtk_menu_attach (GTK_MENU (menu), item,
1800 current_col, current_col + cols,
1801 current_row, current_row + rows);
1805 gtk_combo_box_relayout (GtkComboBox *combo_box)
1810 /* do nothing unless we are in menu style */
1811 if (combo_box->priv->tree_view)
1814 menu = combo_box->priv->popup_widget;
1816 /* get rid of all children */
1817 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1819 list = gtk_container_get_children (GTK_CONTAINER (menu));
1821 for (j = g_list_last (list); j; j = j->prev)
1822 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1827 gtk_combo_box_menu_fill (combo_box);
1832 gtk_combo_box_menu_button_press (GtkWidget *widget,
1833 GdkEventButton *event,
1836 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1838 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1841 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1843 combo_box->priv->popup_in_progress = TRUE;
1845 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1846 combo_box->priv->active_item);
1848 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1850 gtk_combo_box_menu_position, combo_box,
1851 event->button, event->time);
1860 gtk_combo_box_menu_item_activate (GtkWidget *item,
1865 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1867 menu = combo_box->priv->popup_widget;
1868 g_return_if_fail (GTK_IS_MENU (menu));
1870 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1872 gtk_combo_box_set_active (combo_box, index);
1876 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1881 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1882 gint index = gtk_tree_path_get_indices (path)[0];
1884 if (combo_box->priv->active_item >= index)
1885 combo_box->priv->active_item++;
1887 if (!combo_box->priv->tree_view)
1888 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1892 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1896 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1897 gint index = gtk_tree_path_get_indices (path)[0];
1899 if (!combo_box->priv->tree_view)
1900 gtk_combo_box_menu_row_deleted (model, path, user_data);
1902 if (index == combo_box->priv->active_item)
1904 gint items = gtk_tree_model_iter_n_children (model, NULL);
1907 gtk_combo_box_set_active_internal (combo_box, -1);
1908 else if (index == items)
1909 gtk_combo_box_set_active_internal (combo_box, index - 1);
1911 gtk_combo_box_set_active_internal (combo_box, index);
1913 else if (combo_box->priv->active_item > index)
1914 combo_box->priv->active_item--;
1918 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1924 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1925 gint items = gtk_tree_model_iter_n_children (model, NULL);
1928 for (i = 0; i < items; i++)
1929 if (new_order[i] == combo_box->priv->active_item)
1931 combo_box->priv->active_item = i;
1935 if (!combo_box->priv->tree_view)
1936 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
1940 gtk_combo_box_model_row_changed (GtkTreeModel *model,
1945 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1946 gint index = gtk_tree_path_get_indices (path)[0];
1948 if (index == combo_box->priv->active_item &&
1949 combo_box->priv->cell_view)
1950 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
1952 if (combo_box->priv->tree_view)
1953 gtk_combo_box_list_row_changed (model, path, iter, user_data);
1955 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
1960 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
1967 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1969 if (!combo_box->priv->popup_widget)
1972 menu = combo_box->priv->popup_widget;
1973 g_return_if_fail (GTK_IS_MENU (menu));
1975 item = gtk_cell_view_menu_item_new_from_model (model, path);
1976 g_signal_connect (item, "activate",
1977 G_CALLBACK (gtk_combo_box_menu_item_activate),
1980 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
1982 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
1983 gtk_tree_path_get_indices (path)[0]);
1984 gtk_widget_show (item);
1988 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
1995 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1997 if (!combo_box->priv->popup_widget)
2000 index = gtk_tree_path_get_indices (path)[0];
2002 menu = combo_box->priv->popup_widget;
2003 g_return_if_fail (GTK_IS_MENU (menu));
2005 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2006 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2008 gtk_container_remove (GTK_CONTAINER (menu), item);
2012 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2018 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2020 gtk_combo_box_relayout (combo_box);
2024 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2029 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2032 if (!combo_box->priv->popup_widget)
2035 if (combo_box->priv->wrap_width)
2036 gtk_combo_box_relayout_item (combo_box,
2037 gtk_tree_path_get_indices (path)[0]);
2039 width = gtk_combo_box_calc_requested_width (combo_box, path);
2041 if (width > combo_box->priv->width)
2043 if (combo_box->priv->cell_view)
2045 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2046 gtk_widget_queue_resize (combo_box->priv->cell_view);
2048 combo_box->priv->width = width;
2057 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2060 GtkTreeSelection *sel;
2062 combo_box->priv->button = gtk_toggle_button_new ();
2063 gtk_widget_set_parent (combo_box->priv->button,
2064 GTK_BIN (combo_box)->child->parent);
2065 g_signal_connect (combo_box->priv->button, "button_press_event",
2066 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2067 g_signal_connect (combo_box->priv->button, "toggled",
2068 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2069 g_signal_connect_after (combo_box, "key_press_event",
2070 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2072 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2073 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2074 combo_box->priv->arrow);
2075 combo_box->priv->separator = NULL;
2076 gtk_widget_show_all (combo_box->priv->button);
2078 if (combo_box->priv->cell_view)
2080 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2081 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2082 GTK_BIN (combo_box)->child->parent);
2083 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2086 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2087 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2089 gtk_widget_show (combo_box->priv->cell_view_frame);
2092 combo_box->priv->tree_view = gtk_tree_view_new ();
2093 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2094 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2095 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2097 if (combo_box->priv->model)
2098 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2099 combo_box->priv->model);
2101 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2102 G_CALLBACK (gtk_combo_box_list_button_pressed),
2104 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2105 G_CALLBACK (gtk_combo_box_list_button_released),
2107 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2108 G_CALLBACK (gtk_combo_box_list_key_press),
2111 combo_box->priv->column = gtk_tree_view_column_new ();
2112 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2113 combo_box->priv->column);
2116 for (i = combo_box->priv->cells; i; i = i->next)
2119 ComboCellInfo *info = (ComboCellInfo *)i->data;
2121 if (info->pack == GTK_PACK_START)
2122 gtk_tree_view_column_pack_start (combo_box->priv->column,
2123 info->cell, info->expand);
2124 else if (info->pack == GTK_PACK_END)
2125 gtk_tree_view_column_pack_end (combo_box->priv->column,
2126 info->cell, info->expand);
2128 for (j = info->attributes; j; j = j->next->next)
2130 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2133 GPOINTER_TO_INT (j->next->data));
2137 if (combo_box->priv->active_item != -1)
2141 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2144 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2146 gtk_tree_path_free (path);
2150 /* set sample/popup widgets */
2151 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2153 gtk_widget_show (combo_box->priv->tree_view);
2157 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2159 /* disconnect signals */
2160 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2161 G_SIGNAL_MATCH_DATA,
2162 0, 0, NULL, NULL, combo_box);
2163 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2164 G_SIGNAL_MATCH_DATA,
2166 gtk_combo_box_list_button_pressed,
2169 /* destroy things (unparent will kill the latest ref from us)
2170 * last unref on button will destroy the arrow
2172 gtk_widget_unparent (combo_box->priv->button);
2173 combo_box->priv->button = NULL;
2174 combo_box->priv->arrow = NULL;
2176 if (combo_box->priv->cell_view)
2178 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2179 "background_set", FALSE,
2182 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2183 combo_box->priv->cell_view_frame = NULL;
2186 gtk_widget_destroy (combo_box->priv->tree_view);
2188 combo_box->priv->tree_view = NULL;
2189 combo_box->priv->popup_widget = NULL;
2194 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2196 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2197 gtk_grab_remove (combo_box->priv->tree_view);
2199 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2201 gtk_grab_remove (combo_box->priv->popup_window);
2202 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2203 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2208 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2209 GdkEventButton *event,
2212 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2214 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2216 if (ewidget == combo_box->priv->tree_view)
2219 if ((ewidget != combo_box->priv->button) ||
2220 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2223 gtk_combo_box_popup (combo_box);
2225 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2228 combo_box->priv->popup_in_progress = TRUE;
2234 gtk_combo_box_list_button_released (GtkWidget *widget,
2235 GdkEventButton *event,
2239 GtkTreePath *path = NULL;
2241 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2243 gboolean popup_in_progress = FALSE;
2245 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2247 if (combo_box->priv->popup_in_progress)
2249 popup_in_progress = TRUE;
2250 combo_box->priv->popup_in_progress = FALSE;
2253 if (ewidget != combo_box->priv->tree_view)
2255 if (ewidget == combo_box->priv->button &&
2256 !popup_in_progress &&
2257 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2259 gtk_combo_box_popdown (combo_box);
2263 /* released outside treeview */
2264 if (ewidget != combo_box->priv->button)
2266 gtk_combo_box_popdown (combo_box);
2274 /* select something cool */
2275 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2281 return TRUE; /* clicked outside window? */
2283 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2284 gtk_combo_box_popdown (combo_box);
2286 gtk_tree_path_free (path);
2292 gtk_combo_box_key_press (GtkWidget *widget,
2296 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2297 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2298 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2299 gint index = gtk_combo_box_get_active (combo_box);
2302 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2303 state == GDK_MOD1_MASK)
2305 gtk_combo_box_popup (combo_box);
2310 switch (event->keyval)
2314 new_index = index + 1;
2318 new_index = index - 1;
2321 case GDK_KP_Page_Up:
2327 case GDK_KP_Page_Down:
2330 new_index = items - 1;
2336 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2342 gtk_combo_box_menu_key_press (GtkWidget *widget,
2346 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2347 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2349 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2350 state == GDK_MOD1_MASK)
2352 gtk_combo_box_popdown (combo_box);
2361 gtk_combo_box_list_key_press (GtkWidget *widget,
2365 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2366 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2368 if (event->keyval == GDK_Escape ||
2369 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2370 state == GDK_MOD1_MASK))
2372 /* reset active item -- this is incredibly lame and ugly */
2373 gtk_combo_box_set_active (combo_box,
2374 gtk_combo_box_get_active (combo_box));
2376 gtk_combo_box_popdown (combo_box);
2381 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2382 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2386 GtkTreeModel *model = NULL;
2387 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2389 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2394 path = gtk_tree_model_get_path (model, &iter);
2397 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2398 gtk_tree_path_free (path);
2407 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2412 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2415 width = gtk_combo_box_calc_requested_width (combo_box, path);
2417 if (width > combo_box->priv->width)
2419 if (combo_box->priv->cell_view)
2421 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2422 gtk_widget_queue_resize (combo_box->priv->cell_view);
2424 combo_box->priv->width = width;
2429 * GtkCellLayout implementation
2432 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2433 GtkCellRenderer *cell,
2436 ComboCellInfo *info;
2437 GtkComboBox *combo_box;
2440 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2441 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2443 combo_box = GTK_COMBO_BOX (layout);
2445 g_object_ref (G_OBJECT (cell));
2446 gtk_object_sink (GTK_OBJECT (cell));
2448 info = g_new0 (ComboCellInfo, 1);
2450 info->expand = expand;
2451 info->pack = GTK_PACK_START;
2453 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2455 if (combo_box->priv->cell_view)
2456 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2459 if (combo_box->priv->column)
2460 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2462 menu = combo_box->priv->popup_widget;
2463 if (GTK_IS_MENU (menu))
2467 list = gtk_container_get_children (GTK_CONTAINER (menu));
2468 for (i = list; i; i = i->next)
2472 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2473 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2475 view = GTK_CELL_VIEW (i->data);
2477 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2484 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2485 GtkCellRenderer *cell,
2488 ComboCellInfo *info;
2489 GtkComboBox *combo_box;
2492 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2493 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2495 combo_box = GTK_COMBO_BOX (layout);
2497 g_object_ref (G_OBJECT (cell));
2498 gtk_object_sink (GTK_OBJECT (cell));
2500 info = g_new0 (ComboCellInfo, 1);
2502 info->expand = expand;
2503 info->pack = GTK_PACK_END;
2505 if (combo_box->priv->cell_view)
2506 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2509 if (combo_box->priv->column)
2510 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2512 menu = combo_box->priv->popup_widget;
2513 if (GTK_IS_MENU (menu))
2517 list = gtk_container_get_children (GTK_CONTAINER (menu));
2518 for (i = list; i; i = i->next)
2522 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2523 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2525 view = GTK_CELL_VIEW (i->data);
2527 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2534 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2537 GtkComboBox *combo_box;
2540 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2542 combo_box = GTK_COMBO_BOX (layout);
2544 if (combo_box->priv->cell_view)
2545 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2547 if (combo_box->priv->column)
2548 gtk_tree_view_column_clear (combo_box->priv->column);
2550 for (i = combo_box->priv->cells; i; i = i->next)
2552 ComboCellInfo *info = (ComboCellInfo *)i->data;
2554 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2555 g_object_unref (G_OBJECT (info->cell));
2559 g_slist_free (combo_box->priv->cells);
2560 combo_box->priv->cells = NULL;
2562 menu = combo_box->priv->popup_widget;
2563 if (GTK_IS_MENU (menu))
2567 list = gtk_container_get_children (GTK_CONTAINER (menu));
2568 for (i = list; i; i = i->next)
2572 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2573 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2575 view = GTK_CELL_VIEW (i->data);
2577 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2584 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2585 GtkCellRenderer *cell,
2586 const gchar *attribute,
2589 ComboCellInfo *info;
2590 GtkComboBox *combo_box;
2593 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2594 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2596 combo_box = GTK_COMBO_BOX (layout);
2598 info = gtk_combo_box_get_cell_info (combo_box, cell);
2600 info->attributes = g_slist_prepend (info->attributes,
2601 GINT_TO_POINTER (column));
2602 info->attributes = g_slist_prepend (info->attributes,
2603 g_strdup (attribute));
2605 if (combo_box->priv->cell_view)
2606 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2607 cell, attribute, column);
2609 if (combo_box->priv->column)
2610 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2611 cell, attribute, column);
2613 menu = combo_box->priv->popup_widget;
2614 if (GTK_IS_MENU (menu))
2618 list = gtk_container_get_children (GTK_CONTAINER (menu));
2619 for (i = list; i; i = i->next)
2623 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2624 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2626 view = GTK_CELL_VIEW (i->data);
2628 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2634 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2638 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2639 GtkCellRenderer *cell,
2640 GtkCellLayoutDataFunc func,
2642 GDestroyNotify destroy)
2644 ComboCellInfo *info;
2645 GtkComboBox *combo_box;
2648 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2650 combo_box = GTK_COMBO_BOX (layout);
2652 info = gtk_combo_box_get_cell_info (combo_box, cell);
2653 g_return_if_fail (info != NULL);
2657 GDestroyNotify d = info->destroy;
2659 info->destroy = NULL;
2660 d (info->func_data);
2664 info->func_data = func_data;
2665 info->destroy = destroy;
2667 if (combo_box->priv->cell_view)
2668 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2670 if (combo_box->priv->column)
2671 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2673 menu = combo_box->priv->popup_widget;
2674 if (GTK_IS_MENU (menu))
2678 list = gtk_container_get_children (GTK_CONTAINER (menu));
2679 for (i = list; i; i = i->next)
2683 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2684 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2686 view = GTK_CELL_VIEW (i->data);
2688 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2689 func, func_data, NULL);
2694 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2698 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2699 GtkCellRenderer *cell)
2701 ComboCellInfo *info;
2702 GtkComboBox *combo_box;
2706 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2707 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2709 combo_box = GTK_COMBO_BOX (layout);
2711 info = gtk_combo_box_get_cell_info (combo_box, cell);
2712 g_return_if_fail (info != NULL);
2714 list = info->attributes;
2715 while (list && list->next)
2717 g_free (list->data);
2718 list = list->next->next;
2720 g_slist_free (info->attributes);
2721 info->attributes = NULL;
2723 if (combo_box->priv->cell_view)
2724 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2726 if (combo_box->priv->column)
2727 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2729 menu = combo_box->priv->popup_widget;
2730 if (GTK_IS_MENU (menu))
2734 list = gtk_container_get_children (GTK_CONTAINER (menu));
2735 for (i = list; i; i = i->next)
2739 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2740 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2742 view = GTK_CELL_VIEW (i->data);
2744 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2749 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2753 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2754 GtkCellRenderer *cell,
2757 ComboCellInfo *info;
2758 GtkComboBox *combo_box;
2762 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2763 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2765 combo_box = GTK_COMBO_BOX (layout);
2767 info = gtk_combo_box_get_cell_info (combo_box, cell);
2769 g_return_if_fail (info != NULL);
2770 g_return_if_fail (position >= 0);
2772 link = g_slist_find (combo_box->priv->cells, info);
2774 g_return_if_fail (link != NULL);
2776 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2777 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2780 if (combo_box->priv->cell_view)
2781 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2784 if (combo_box->priv->column)
2785 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2788 menu = combo_box->priv->popup_widget;
2789 if (GTK_IS_MENU (menu))
2793 list = gtk_container_get_children (GTK_CONTAINER (menu));
2794 for (i = list; i; i = i->next)
2798 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2799 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2801 view = GTK_CELL_VIEW (i->data);
2803 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2808 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2816 * gtk_combo_box_new:
2818 * Creates a new empty #GtkComboBox.
2820 * Return value: A new #GtkComboBox.
2825 gtk_combo_box_new (void)
2827 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2831 * gtk_combo_box_new_with_model:
2832 * @model: A #GtkTreeModel.
2834 * Creates a new #GtkComboBox with the model initialized to @model.
2836 * Return value: A new #GtkComboBox.
2841 gtk_combo_box_new_with_model (GtkTreeModel *model)
2843 GtkComboBox *combo_box;
2845 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2847 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2851 return GTK_WIDGET (combo_box);
2855 * gtk_combo_box_set_wrap_width:
2856 * @combo_box: A #GtkComboBox.
2857 * @width: Preferred number of columns.
2859 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2860 * the preferred number of columns when you want to the popup to be layed out
2866 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2869 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2870 g_return_if_fail (width >= 0);
2872 if (width != combo_box->priv->wrap_width)
2874 combo_box->priv->wrap_width = width;
2876 gtk_combo_box_check_appearance (combo_box);
2877 gtk_combo_box_relayout (combo_box);
2879 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2884 * gtk_combo_box_set_row_span_column:
2885 * @combo_box: A #GtkComboBox.
2886 * @row_span: A column in the model passed during construction.
2888 * Sets the column with row span information for @combo_box to be @row_span.
2889 * The row span column contains integers which indicate how many rows
2890 * an item should span.
2895 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2900 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2902 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2903 g_return_if_fail (row_span >= 0 && row_span < col);
2905 if (row_span != combo_box->priv->row_column)
2907 combo_box->priv->row_column = row_span;
2909 gtk_combo_box_relayout (combo_box);
2911 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2916 * gtk_combo_box_set_column_span_column:
2917 * @combo_box: A #GtkComboBox.
2918 * @column_span: A column in the model passed during construction.
2920 * Sets the column with column span information for @combo_box to be
2921 * @column_span. The column span column contains integers which indicate
2922 * how many columns an item should span.
2927 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2932 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2934 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2935 g_return_if_fail (column_span >= 0 && column_span < col);
2937 if (column_span != combo_box->priv->col_column)
2939 combo_box->priv->col_column = column_span;
2941 gtk_combo_box_relayout (combo_box);
2943 g_object_notify (G_OBJECT (combo_box), "column_span_column");
2948 * gtk_combo_box_get_active:
2949 * @combo_box: A #GtkComboBox.
2951 * Returns the index of the currently active item, or -1 if there's no
2954 * Return value: An integer which is the index of the currently active item, or
2955 * -1 if there's no active item.
2960 gtk_combo_box_get_active (GtkComboBox *combo_box)
2962 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
2964 return combo_box->priv->active_item;
2968 * gtk_combo_box_set_active:
2969 * @combo_box: A #GtkComboBox.
2970 * @index: An index in the model passed during construction, or -1 to have
2973 * Sets the active item of @combo_box to be the item at @index.
2978 gtk_combo_box_set_active (GtkComboBox *combo_box,
2981 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2982 /* -1 means "no item selected" */
2983 g_return_if_fail (index >= -1);
2985 if (combo_box->priv->active_item == index)
2988 gtk_combo_box_set_active_internal (combo_box, index);
2992 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
2997 combo_box->priv->active_item = index;
3001 if (combo_box->priv->tree_view)
3002 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3005 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3007 if (GTK_IS_MENU (menu))
3008 gtk_menu_set_active (menu, -1);
3011 if (combo_box->priv->cell_view)
3012 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3016 path = gtk_tree_path_new_from_indices (index, -1);
3018 if (combo_box->priv->tree_view)
3019 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3022 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3024 if (GTK_IS_MENU (menu))
3025 gtk_menu_set_active (GTK_MENU (menu), index);
3028 if (combo_box->priv->cell_view)
3029 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3031 gtk_tree_path_free (path);
3034 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3039 * gtk_combo_box_get_active_iter:
3040 * @combo_box: A #GtkComboBox
3041 * @iter: The uninitialized #GtkTreeIter.
3043 * Sets @iter to point to the current active item, if it exists.
3045 * Return value: %TRUE, if @iter was set
3050 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3057 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3059 active = gtk_combo_box_get_active (combo_box);
3063 path = gtk_tree_path_new_from_indices (active, -1);
3064 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3066 gtk_tree_path_free (path);
3072 * gtk_combo_box_set_active_iter:
3073 * @combo_box: A #GtkComboBox
3074 * @iter: The #GtkTreeIter.
3076 * Sets the current active item to be the one referenced by @iter.
3077 * @iter must correspond to a path of depth one.
3082 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3087 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3089 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3090 g_return_if_fail (path != NULL);
3091 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3093 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3094 gtk_tree_path_free (path);
3098 * gtk_combo_box_set_model:
3099 * @combo_box: A #GtkComboBox.
3100 * @model: A #GtkTreeModel.
3102 * Sets the model used by @combo_box to be @model. Will unset a
3103 * previously set model (if applicable).
3108 gtk_combo_box_set_model (GtkComboBox *combo_box,
3109 GtkTreeModel *model)
3111 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3112 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3114 if (model == combo_box->priv->model)
3117 if (combo_box->priv->model)
3118 gtk_combo_box_unset_model (combo_box);
3120 combo_box->priv->model = model;
3121 g_object_ref (G_OBJECT (combo_box->priv->model));
3123 combo_box->priv->inserted_id =
3124 g_signal_connect (combo_box->priv->model, "row_inserted",
3125 G_CALLBACK (gtk_combo_box_model_row_inserted),
3127 combo_box->priv->deleted_id =
3128 g_signal_connect (combo_box->priv->model, "row_deleted",
3129 G_CALLBACK (gtk_combo_box_model_row_deleted),
3131 combo_box->priv->reordered_id =
3132 g_signal_connect (combo_box->priv->model, "rows_reordered",
3133 G_CALLBACK (gtk_combo_box_model_rows_reordered),
3135 combo_box->priv->changed_id =
3136 g_signal_connect (combo_box->priv->model, "row_changed",
3137 G_CALLBACK (gtk_combo_box_model_row_changed),
3140 if (combo_box->priv->tree_view)
3143 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3144 combo_box->priv->model);
3149 if (combo_box->priv->popup_widget)
3150 gtk_combo_box_menu_fill (combo_box);
3154 if (combo_box->priv->cell_view)
3155 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3156 combo_box->priv->model);
3160 * gtk_combo_box_get_model
3161 * @combo_box: A #GtkComboBox.
3163 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3165 * Return value: A #GtkTreeModel which was passed during construction.
3170 gtk_combo_box_get_model (GtkComboBox *combo_box)
3172 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3174 return combo_box->priv->model;
3178 /* convenience API for simple text combos */
3181 * gtk_combo_box_new_text:
3183 * Convenience function which constructs a new text combo box, which is a
3184 * #GtkComboBox just displaying strings. If you use this function to create
3185 * a text combo box, you should only manipulate its data source with the
3186 * following convenience functions: gtk_combo_box_append_text(),
3187 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3188 * gtk_combo_box_remove_text().
3190 * Return value: A new text combo box.
3195 gtk_combo_box_new_text (void)
3197 GtkWidget *combo_box;
3198 GtkCellRenderer *cell;
3199 GtkListStore *store;
3201 store = gtk_list_store_new (1, G_TYPE_STRING);
3203 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3205 cell = gtk_cell_renderer_text_new ();
3206 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3207 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3215 * gtk_combo_box_append_text:
3216 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3219 * Appends @string to the list of strings stored in @combo_box. Note that
3220 * you can only use this function with combo boxes constructed with
3221 * gtk_combo_box_new_text().
3226 gtk_combo_box_append_text (GtkComboBox *combo_box,
3230 GtkListStore *store;
3232 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3233 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3234 g_return_if_fail (text != NULL);
3236 store = GTK_LIST_STORE (combo_box->priv->model);
3238 gtk_list_store_append (store, &iter);
3239 gtk_list_store_set (store, &iter, 0, text, -1);
3243 * gtk_combo_box_insert_text:
3244 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3245 * @position: An index to insert @text.
3248 * Inserts @string at @position in the list of strings stored in @combo_box.
3249 * Note that you can only use this function with combo boxes constructed
3250 * with gtk_combo_box_new_text().
3255 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3260 GtkListStore *store;
3262 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3263 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3264 g_return_if_fail (position >= 0);
3265 g_return_if_fail (text != NULL);
3267 store = GTK_LIST_STORE (combo_box->priv->model);
3269 gtk_list_store_insert (store, &iter, position);
3270 gtk_list_store_set (store, &iter, 0, text, -1);
3274 * gtk_combo_box_prepend_text:
3275 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3278 * Prepends @string to the list of strings stored in @combo_box. Note that
3279 * you can only use this function with combo boxes constructed with
3280 * gtk_combo_box_new_text().
3285 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3289 GtkListStore *store;
3291 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3292 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3293 g_return_if_fail (text != NULL);
3295 store = GTK_LIST_STORE (combo_box->priv->model);
3297 gtk_list_store_prepend (store, &iter);
3298 gtk_list_store_set (store, &iter, 0, text, -1);
3302 * gtk_combo_box_remove_text:
3303 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3304 * @position: Index of the item to remove.
3306 * Removes the string at @position from @combo_box. Note that you can only use
3307 * this function with combo boxes constructed with gtk_combo_box_new_text().
3312 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3316 GtkListStore *store;
3318 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3319 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3320 g_return_if_fail (position >= 0);
3322 store = GTK_LIST_STORE (combo_box->priv->model);
3324 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3326 gtk_list_store_remove (store, &iter);
3331 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3332 gboolean group_cycling)
3334 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3336 gtk_widget_grab_focus (combo_box->priv->button);
3342 gtk_combo_box_destroy (GtkObject *object)
3344 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3346 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3348 combo_box->priv->cell_view = NULL;
3352 gtk_combo_box_finalize (GObject *object)
3354 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3357 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3358 gtk_combo_box_menu_destroy (combo_box);
3360 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3361 gtk_combo_box_list_destroy (combo_box);
3363 if (combo_box->priv->popup_window)
3364 gtk_widget_destroy (combo_box->priv->popup_window);
3366 gtk_combo_box_unset_model (combo_box);
3368 if (combo_box->priv->model)
3369 g_object_unref (combo_box->priv->model);
3371 for (i = combo_box->priv->cells; i; i = i->next)
3373 ComboCellInfo *info = (ComboCellInfo *)i->data;
3374 GSList *list = info->attributes;
3377 info->destroy (info->func_data);
3379 while (list && list->next)
3381 g_free (list->data);
3382 list = list->next->next;
3384 g_slist_free (info->attributes);
3386 g_object_unref (G_OBJECT (info->cell));
3389 g_slist_free (combo_box->priv->cells);
3391 G_OBJECT_CLASS (parent_class)->finalize (object);