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.
20 #include "gtkcombobox.h"
23 #include "gtkbindings.h"
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkcellview.h"
27 #include "gtkcellviewmenuitem.h"
28 #include "gtkeventbox.h"
30 #include "gtkliststore.h"
33 #include "gtktogglebutton.h"
34 #include "gtktreeselection.h"
35 #include "gtkvseparator.h"
36 #include "gtkwindow.h"
38 #include <gdk/gdkkeysyms.h>
40 #include <gobject/gvaluecollector.h>
45 #include "gtkmarshalers.h"
49 /* WELCOME, to THE house of evil code */
51 typedef struct _ComboCellInfo ComboCellInfo;
54 GtkCellRenderer *cell;
57 GtkCellLayoutDataFunc func;
59 GDestroyNotify destroy;
65 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
67 struct _GtkComboBoxPrivate
79 GtkTreeViewColumn *column;
82 GtkWidget *cell_view_frame;
88 GtkWidget *popup_widget;
89 GtkWidget *popup_window;
90 GtkWidget *popup_frame;
100 guint popup_in_progress : 1;
103 /* While debugging this evil code, I have learned that
104 * there are actually 4 modes to this widget, which can
105 * be characterized as follows
107 * 1) menu mode, no child added
110 * cell_view -> GtkCellView, regular child
111 * cell_view_frame -> NULL
112 * button -> GtkToggleButton set_parent to combo
113 * arrow -> GtkArrow set_parent to button
114 * separator -> GtkVSepator set_parent to button
115 * popup_widget -> GtkMenu
116 * popup_window -> NULL
117 * popup_frame -> NULL
119 * 2) menu mode, child added
123 * cell_view_frame -> NULL
124 * button -> GtkToggleButton set_parent to combo
125 * arrow -> GtkArrow, child of button
127 * popup_widget -> GtkMenu
128 * popup_window -> NULL
129 * popup_frame -> NULL
131 * 3) list mode, no child added
133 * tree_view -> GtkTreeView, child of popup_frame
134 * cell_view -> GtkCellView, regular child
135 * cell_view_frame -> GtkFrame, set parent to combo
136 * button -> GtkToggleButton, set_parent to combo
137 * arrow -> GtkArrow, child of button
139 * popup_widget -> tree_view
140 * popup_window -> GtkWindow
141 * popup_frame -> GtkFrame, child of popup_window
143 * 4) list mode, child added
145 * tree_view -> GtkTreeView, child of popup_frame
147 * cell_view_frame -> NULL
148 * button -> GtkToggleButton, set_parent to combo
149 * arrow -> GtkArrow, child of button
151 * popup_widget -> tree_view
152 * popup_window -> GtkWindow
153 * popup_frame -> GtkFrame, child of popup_window
166 PROP_ROW_SPAN_COLUMN,
167 PROP_COLUMN_SPAN_COLUMN,
171 static GtkBinClass *parent_class = NULL;
172 static guint combo_box_signals[LAST_SIGNAL] = {0,};
174 #define BONUS_PADDING 4
178 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
179 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
180 static void gtk_combo_box_init (GtkComboBox *combo_box);
181 static void gtk_combo_box_finalize (GObject *object);
182 static void gtk_combo_box_destroy (GtkObject *object);
184 static void gtk_combo_box_set_property (GObject *object,
188 static void gtk_combo_box_get_property (GObject *object,
193 static void gtk_combo_box_state_changed (GtkWidget *widget,
194 GtkStateType previous);
195 static void gtk_combo_box_style_set (GtkWidget *widget,
197 static void gtk_combo_box_button_toggled (GtkWidget *widget,
199 static void gtk_combo_box_add (GtkContainer *container,
202 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
203 GtkCellRenderer *cell);
205 static void gtk_combo_box_menu_show (GtkWidget *menu,
207 static void gtk_combo_box_menu_hide (GtkWidget *menu,
210 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
212 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
217 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
222 static void gtk_combo_box_menu_position (GtkMenu *menu,
228 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
230 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
232 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
233 static void gtk_combo_box_set_model_internal (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_childs);
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_style_set (GtkWidget *widget,
627 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
629 gtk_widget_queue_resize (widget);
631 /* if wrap_width > 0, then we are in grid-mode and forced to use
634 if (combo_box->priv->wrap_width)
637 gtk_widget_style_get (widget,
638 "appears-as-list", &appearance,
641 /* TRUE is windows style */
644 /* Destroy all the menu mode widgets, if they exist. */
645 if (GTK_IS_MENU (combo_box->priv->popup_widget))
646 gtk_combo_box_menu_destroy (combo_box);
648 /* Create the list mode widgets, if they don't already exist. */
649 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
650 gtk_combo_box_list_setup (combo_box);
654 /* Destroy all the list mode widgets, if they exist. */
655 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
656 gtk_combo_box_list_destroy (combo_box);
658 /* Create the menu mode widgets, if they don't already exist. */
659 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
660 gtk_combo_box_menu_setup (combo_box, TRUE);
663 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
664 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
665 &widget->style->base[GTK_WIDGET_STATE (widget)]);
670 gtk_combo_box_button_toggled (GtkWidget *widget,
673 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
675 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
677 if (!combo_box->priv->popup_in_progress)
678 gtk_combo_box_popup (combo_box);
681 gtk_combo_box_popdown (combo_box);
685 gtk_combo_box_add (GtkContainer *container,
688 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
690 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
691 gtk_container_remove (container, combo_box->priv->cell_view);
693 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
695 if (combo_box->priv->cell_view &&
696 widget != combo_box->priv->cell_view)
698 /* since the cell_view was unparented, it's gone now */
699 combo_box->priv->cell_view = NULL;
701 if (!combo_box->priv->tree_view && combo_box->priv->separator)
703 gtk_widget_unparent (combo_box->priv->separator);
704 combo_box->priv->separator = NULL;
706 g_object_ref (G_OBJECT (combo_box->priv->arrow));
707 gtk_widget_unparent (combo_box->priv->arrow);
708 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
709 combo_box->priv->arrow);
710 g_object_unref (G_OBJECT (combo_box->priv->arrow));
712 gtk_widget_queue_resize (GTK_WIDGET (container));
714 else if (combo_box->priv->cell_view_frame)
716 gtk_widget_unparent (combo_box->priv->cell_view_frame);
717 combo_box->priv->cell_view_frame = NULL;
722 static ComboCellInfo *
723 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
724 GtkCellRenderer *cell)
728 for (i = combo_box->priv->cells; i; i = i->next)
730 ComboCellInfo *info = (ComboCellInfo *)i->data;
732 if (info && info->cell == cell)
740 gtk_combo_box_menu_show (GtkWidget *menu,
743 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
745 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
747 combo_box->priv->popup_in_progress = FALSE;
751 gtk_combo_box_menu_hide (GtkWidget *menu,
754 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
756 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
761 gtk_combo_box_detacher (GtkWidget *widget,
764 GtkComboBox *combo_box;
766 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
768 combo_box = GTK_COMBO_BOX (widget);
769 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
771 g_signal_handlers_disconnect_by_func (menu,
772 gtk_combo_box_menu_show,
774 g_signal_handlers_disconnect_by_func (menu,
775 gtk_combo_box_menu_hide,
778 combo_box->priv->popup_widget = NULL;
782 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
785 if (GTK_IS_MENU (combo_box->priv->popup_widget))
787 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
788 combo_box->priv->popup_widget = NULL;
790 else if (combo_box->priv->popup_widget)
792 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
793 combo_box->priv->popup_widget);
794 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
795 combo_box->priv->popup_widget = NULL;
798 if (GTK_IS_MENU (popup))
800 if (combo_box->priv->popup_window)
802 gtk_widget_destroy (combo_box->priv->popup_window);
803 combo_box->priv->popup_window = NULL;
804 combo_box->priv->popup_frame = NULL;
807 combo_box->priv->popup_widget = popup;
809 g_signal_connect (popup, "show",
810 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
811 g_signal_connect (popup, "hide",
812 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
814 gtk_menu_attach_to_widget (GTK_MENU (popup),
815 GTK_WIDGET (combo_box),
816 gtk_combo_box_detacher);
820 if (!combo_box->priv->popup_window)
822 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
823 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
824 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
825 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
827 combo_box->priv->popup_frame = gtk_frame_new (NULL);
828 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
829 GTK_SHADOW_ETCHED_IN);
830 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
831 combo_box->priv->popup_frame);
833 gtk_widget_show (combo_box->priv->popup_frame);
836 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
838 gtk_widget_show (popup);
839 g_object_ref (G_OBJECT (popup));
840 combo_box->priv->popup_widget = popup;
845 gtk_combo_box_menu_position_below (GtkMenu *menu,
854 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
856 /* FIXME: is using the size request here broken? */
857 child = GTK_BIN (combo_box)->child;
859 gdk_window_get_origin (child->window, &sx, &sy);
861 gtk_widget_size_request (GTK_WIDGET (menu), &req);
863 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
866 *x = sx + child->allocation.width - req.width;
867 *y = sy + child->allocation.height;
869 if (GTK_WIDGET_NO_WINDOW (child))
871 *x += child->allocation.x;
872 *y += child->allocation.y;
879 gtk_combo_box_menu_position_over (GtkMenu *menu,
885 GtkComboBox *combo_box;
889 GtkRequisition requisition;
896 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
898 combo_box = GTK_COMBO_BOX (user_data);
899 widget = GTK_WIDGET (combo_box);
901 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
902 menu_width = requisition.width;
904 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
905 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
907 menu_xpos += widget->allocation.x;
908 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
912 gtk_widget_get_child_requisition (active, &requisition);
913 menu_ypos -= requisition.height / 2;
916 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
919 child = children->data;
924 if (GTK_WIDGET_VISIBLE (child))
926 gtk_widget_get_child_requisition (child, &requisition);
927 menu_ypos -= requisition.height;
930 children = children->next;
933 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
934 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
936 /* Clamp the position on screen */
937 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
941 else if ((menu_xpos + menu_width) > screen_width)
942 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
951 gtk_combo_box_menu_position (GtkMenu *menu,
957 GtkComboBox *combo_box;
958 GtkWidget *menu_item;
960 combo_box = GTK_COMBO_BOX (user_data);
962 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
963 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
966 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
968 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
971 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
977 gtk_combo_box_list_position (GtkComboBox *combo_box,
986 GdkRectangle monitor;
987 GtkRequisition popup_req;
989 sample = GTK_BIN (combo_box)->child;
991 *width = sample->allocation.width;
992 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
993 *height = popup_req.height;
995 gdk_window_get_origin (sample->window, x, y);
997 if (combo_box->priv->cell_view_frame)
999 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1000 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1001 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1002 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1005 if (GTK_WIDGET_NO_WINDOW (sample))
1007 *x += sample->allocation.x;
1008 *y += sample->allocation.y;
1011 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1012 monitor_num = gdk_screen_get_monitor_at_window (screen,
1013 GTK_WIDGET (combo_box)->window);
1014 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1018 else if (*x + *width > monitor.x + monitor.width)
1019 *x = monitor.x + monitor.width - *width;
1021 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1022 *y += sample->allocation.height;
1028 * gtk_combo_box_popup:
1029 * @combo_box: a #GtkComboBox
1031 * Pops up the menu or dropdown list of @combo_box.
1033 * This function is mostly intended for use by accessibility technologies;
1034 * applications should have little use for it.
1039 gtk_combo_box_popup (GtkComboBox *combo_box)
1041 gint x, y, width, height;
1043 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1045 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1048 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1050 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1051 combo_box->priv->active_item);
1053 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1055 gtk_combo_box_menu_position, combo_box,
1060 gtk_widget_show_all (combo_box->priv->popup_frame);
1061 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1063 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1064 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1067 gtk_widget_show (combo_box->priv->popup_window);
1069 gtk_widget_grab_focus (combo_box->priv->popup_window);
1070 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1073 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1075 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1076 FALSE, GDK_CURRENT_TIME);
1077 gtk_widget_grab_focus (combo_box->priv->tree_view);
1080 gtk_grab_add (combo_box->priv->popup_window);
1081 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1082 GDK_BUTTON_PRESS_MASK |
1083 GDK_BUTTON_RELEASE_MASK |
1084 GDK_POINTER_MOTION_MASK,
1085 NULL, NULL, GDK_CURRENT_TIME);
1087 gtk_grab_add (combo_box->priv->tree_view);
1091 * gtk_combo_box_popdown:
1092 * @combo_box: a #GtkComboBox
1094 * Hides the menu or dropdown list of @combo_box.
1096 * This function is mostly intended for use by accessibility technologies;
1097 * applications should have little use for it.
1102 gtk_combo_box_popdown (GtkComboBox *combo_box)
1104 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1106 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1108 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1112 gtk_combo_box_list_remove_grabs (combo_box);
1113 gtk_widget_hide_all (combo_box->priv->popup_window);
1114 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1119 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1125 if (combo_box->priv->cell_view)
1126 gtk_widget_style_get (combo_box->priv->cell_view,
1127 "focus-line-width", &padding,
1132 /* add some pixels for good measure */
1133 padding += BONUS_PADDING;
1135 if (combo_box->priv->cell_view)
1136 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1141 return req.width + padding;
1145 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1151 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1154 combo_box->priv->width = 0;
1156 path = gtk_tree_path_new_from_indices (0, -1);
1158 if (combo_box->priv->cell_view)
1159 gtk_widget_style_get (combo_box->priv->cell_view,
1160 "focus-line-width", &padding,
1165 /* add some pixels for good measure */
1166 padding += BONUS_PADDING;
1172 if (combo_box->priv->cell_view)
1173 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1178 combo_box->priv->width = MAX (combo_box->priv->width,
1179 req.width + padding);
1181 gtk_tree_path_next (path);
1183 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1185 gtk_tree_path_free (path);
1189 gtk_combo_box_size_request (GtkWidget *widget,
1190 GtkRequisition *requisition)
1193 GtkRequisition bin_req;
1195 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1198 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1199 gtk_combo_box_remeasure (combo_box);
1200 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1202 if (!combo_box->priv->tree_view)
1206 if (combo_box->priv->cell_view)
1208 GtkRequisition sep_req, arrow_req;
1209 gint border_width, xthickness, ythickness;
1211 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1212 xthickness = combo_box->priv->button->style->xthickness;
1213 ythickness = combo_box->priv->button->style->ythickness;
1215 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1217 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1218 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1220 height = MAX (sep_req.height, arrow_req.height);
1221 height = MAX (height, bin_req.height);
1223 width = bin_req.width + sep_req.width + arrow_req.width;
1225 height += border_width + 1 + xthickness * 2 + 4;
1226 width += border_width + 1 + ythickness * 2 + 4;
1228 requisition->width = width;
1229 requisition->height = height;
1233 GtkRequisition but_req;
1235 gtk_widget_size_request (combo_box->priv->button, &but_req);
1237 requisition->width = bin_req.width + but_req.width;
1238 requisition->height = MAX (bin_req.height, but_req.height);
1244 GtkRequisition button_req;
1246 /* sample + frame */
1247 *requisition = bin_req;
1249 if (combo_box->priv->cell_view_frame)
1251 requisition->width += 2 *
1252 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1253 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1254 requisition->height += 2 *
1255 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1256 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1260 gtk_widget_size_request (combo_box->priv->button, &button_req);
1262 requisition->height = MAX (requisition->height, button_req.height);
1263 requisition->width += button_req.width;
1268 gtk_combo_box_size_allocate (GtkWidget *widget,
1269 GtkAllocation *allocation)
1271 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1272 GtkAllocation child;
1274 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1276 widget->allocation = *allocation;
1278 if (!combo_box->priv->tree_view)
1280 if (combo_box->priv->cell_view)
1282 gint border_width, xthickness, ythickness;
1286 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1288 /* set some things ready */
1289 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1290 xthickness = combo_box->priv->button->style->xthickness;
1291 ythickness = combo_box->priv->button->style->ythickness;
1293 child.x = allocation->x + border_width + 1 + xthickness + 2;
1294 child.y = allocation->y + border_width + 1 + ythickness + 2;
1296 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1298 /* handle the childs */
1299 gtk_widget_size_request (combo_box->priv->arrow, &req);
1300 child.width = req.width;
1301 child.height = allocation->height - 2 * (child.y - allocation->y);
1303 child.x += width - req.width;
1304 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1306 child.x += req.width;
1307 gtk_widget_size_request (combo_box->priv->separator, &req);
1308 child.width = req.width;
1310 child.x -= req.width;
1311 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1315 child.x += req.width;
1316 child.width = allocation->x + allocation->width
1317 - (border_width + 1 + xthickness + 2) - child.x;
1321 child.width = child.x;
1322 child.x = allocation->x + border_width + 1 + xthickness + 2;
1323 child.width -= child.x;
1326 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1330 gtk_widget_size_request (combo_box->priv->button, &req);
1332 child.x = allocation->x;
1334 child.x = allocation->x + allocation->width - req.width;
1335 child.y = allocation->y;
1336 child.width = req.width;
1337 child.height = allocation->height;
1338 gtk_widget_size_allocate (combo_box->priv->button, &child);
1341 child.x = allocation->x + req.width;
1343 child.x = allocation->x;
1344 child.y = allocation->y;
1345 child.width = allocation->width - req.width;
1346 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1354 gtk_widget_size_request (combo_box->priv->button, &req);
1356 child.x = allocation->x;
1358 child.x = allocation->x + allocation->width - req.width;
1359 child.y = allocation->y;
1360 child.width = req.width;
1361 child.height = allocation->height;
1362 gtk_widget_size_allocate (combo_box->priv->button, &child);
1366 child.x = allocation->x + req.width;
1368 child.x = allocation->x;
1369 child.y = allocation->y;
1370 child.width = allocation->width - req.width;
1371 child.height = allocation->height;
1373 if (combo_box->priv->cell_view_frame)
1375 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1379 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1380 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1382 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1383 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1384 child.width -= 2 * (
1385 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1386 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1387 child.height -= 2 * (
1388 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1389 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1392 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1397 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1399 if (combo_box->priv->inserted_id != -1)
1401 g_signal_handler_disconnect (combo_box->priv->model,
1402 combo_box->priv->inserted_id);
1403 combo_box->priv->inserted_id = -1;
1405 if (combo_box->priv->deleted_id != -1)
1407 g_signal_handler_disconnect (combo_box->priv->model,
1408 combo_box->priv->deleted_id);
1409 combo_box->priv->deleted_id = -1;
1411 if (combo_box->priv->reordered_id != -1)
1413 g_signal_handler_disconnect (combo_box->priv->model,
1414 combo_box->priv->reordered_id);
1415 combo_box->priv->reordered_id = -1;
1417 if (combo_box->priv->changed_id != -1)
1419 g_signal_handler_disconnect (combo_box->priv->model,
1420 combo_box->priv->changed_id);
1421 combo_box->priv->changed_id = -1;
1425 if (!combo_box->priv->tree_view)
1427 if (combo_box->priv->popup_widget)
1428 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1429 (GtkCallback)gtk_widget_destroy, NULL);
1434 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1436 combo_box->priv->inserted_id =
1437 g_signal_connect (combo_box->priv->model, "row_inserted",
1438 G_CALLBACK (gtk_combo_box_model_row_inserted),
1440 combo_box->priv->deleted_id =
1441 g_signal_connect (combo_box->priv->model, "row_deleted",
1442 G_CALLBACK (gtk_combo_box_model_row_deleted),
1444 combo_box->priv->reordered_id =
1445 g_signal_connect (combo_box->priv->model, "rows_reordered",
1446 G_CALLBACK (gtk_combo_box_model_rows_reordered),
1448 combo_box->priv->changed_id =
1449 g_signal_connect (combo_box->priv->model, "row_changed",
1450 G_CALLBACK (gtk_combo_box_model_row_changed),
1453 if (combo_box->priv->tree_view)
1456 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1457 combo_box->priv->model);
1462 gtk_combo_box_forall (GtkContainer *container,
1463 gboolean include_internals,
1464 GtkCallback callback,
1465 gpointer callback_data)
1467 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1469 if (include_internals)
1471 if (combo_box->priv->button)
1472 (* callback) (combo_box->priv->button, callback_data);
1473 if (combo_box->priv->separator)
1474 (* callback) (combo_box->priv->separator, callback_data);
1475 if (combo_box->priv->arrow)
1476 (* callback) (combo_box->priv->arrow, callback_data);
1477 if (combo_box->priv->cell_view_frame)
1478 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1481 if (GTK_BIN (container)->child)
1482 (* callback) (GTK_BIN (container)->child, callback_data);
1486 gtk_combo_box_expose_event (GtkWidget *widget,
1487 GdkEventExpose *event)
1489 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1491 if (!combo_box->priv->tree_view)
1493 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1494 combo_box->priv->button, event);
1496 if (combo_box->priv->separator)
1498 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1499 combo_box->priv->separator, event);
1501 /* if not in this case, arrow gets its expose event from button */
1502 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1503 combo_box->priv->arrow, event);
1508 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1509 combo_box->priv->button, event);
1511 if (combo_box->priv->cell_view_frame)
1512 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1513 combo_box->priv->cell_view_frame, event);
1516 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1517 GTK_BIN (widget)->child, event);
1523 gtk_combo_box_scroll_event (GtkWidget *widget,
1524 GdkEventScroll *event)
1526 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1530 index = gtk_combo_box_get_active (combo_box);
1534 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1536 if (event->direction == GDK_SCROLL_UP)
1541 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1552 cell_view_sync_cells (GtkComboBox *combo_box,
1553 GtkCellView *cell_view)
1557 for (k = combo_box->priv->cells; k; k = k->next)
1560 ComboCellInfo *info = (ComboCellInfo *)k->data;
1562 if (info->pack == GTK_PACK_START)
1563 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1564 info->cell, info->expand);
1565 else if (info->pack == GTK_PACK_END)
1566 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1567 info->cell, info->expand);
1569 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1571 info->func, info->func_data, NULL);
1573 for (j = info->attributes; j; j = j->next->next)
1575 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1578 GPOINTER_TO_INT (j->next->data));
1584 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1585 gboolean add_childs)
1589 /* Unset any existing model. */
1590 gtk_combo_box_unset_model (combo_box);
1592 if (combo_box->priv->cell_view)
1594 combo_box->priv->button = gtk_toggle_button_new ();
1595 g_signal_connect (combo_box->priv->button, "toggled",
1596 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1597 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1598 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1599 gtk_widget_set_parent (combo_box->priv->button,
1600 GTK_BIN (combo_box)->child->parent);
1602 combo_box->priv->separator = gtk_vseparator_new ();
1603 gtk_widget_set_parent (combo_box->priv->separator,
1604 combo_box->priv->button);
1605 gtk_widget_show (combo_box->priv->separator);
1607 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1608 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1609 gtk_widget_show (combo_box->priv->arrow);
1611 gtk_widget_show_all (combo_box->priv->button);
1613 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1615 /* I have no clue why, but we need to manually map in this case. */
1616 gtk_widget_map (combo_box->priv->separator);
1617 gtk_widget_map (combo_box->priv->arrow);
1622 combo_box->priv->button = gtk_toggle_button_new ();
1623 g_signal_connect (combo_box->priv->button, "toggled",
1624 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1625 g_signal_connect_after (combo_box, "key_press_event",
1626 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1627 gtk_widget_set_parent (combo_box->priv->button,
1628 GTK_BIN (combo_box)->child->parent);
1630 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1631 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1632 combo_box->priv->arrow);
1633 gtk_widget_show_all (combo_box->priv->button);
1636 g_signal_connect (combo_box->priv->button, "button_press_event",
1637 G_CALLBACK (gtk_combo_box_menu_button_press),
1640 /* create our funky menu */
1641 box = gtk_menu_new ();
1642 g_signal_connect (box, "key_press_event",
1643 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1644 gtk_combo_box_set_popup_widget (combo_box, box);
1646 /* set the models */
1647 gtk_combo_box_set_model_internal (combo_box);
1651 gtk_combo_box_menu_fill (combo_box);
1656 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1662 if (!combo_box->priv->model)
1665 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1666 menu = combo_box->priv->popup_widget;
1668 for (i = 0; i < items; i++)
1672 path = gtk_tree_path_new_from_indices (i, -1);
1673 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1675 g_signal_connect (tmp, "activate",
1676 G_CALLBACK (gtk_combo_box_menu_item_activate),
1679 cell_view_sync_cells (combo_box,
1680 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1682 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1683 gtk_widget_show (tmp);
1685 gtk_tree_path_free (path);
1690 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1692 /* disconnect signal handlers */
1693 gtk_combo_box_unset_model (combo_box);
1695 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1696 G_SIGNAL_MATCH_DATA,
1698 gtk_combo_box_menu_button_press, NULL);
1700 /* unparent will remove our latest ref */
1701 if (combo_box->priv->cell_view)
1703 gtk_widget_unparent (combo_box->priv->arrow);
1704 combo_box->priv->arrow = NULL;
1706 gtk_widget_unparent (combo_box->priv->separator);
1707 combo_box->priv->separator = NULL;
1709 gtk_widget_unparent (combo_box->priv->button);
1710 combo_box->priv->button = NULL;
1714 /* will destroy the arrow too */
1715 gtk_widget_unparent (combo_box->priv->button);
1717 combo_box->priv->button = NULL;
1718 combo_box->priv->arrow = NULL;
1721 /* changing the popup window will unref the menu and the childs */
1729 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1736 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1740 if (combo_box->priv->col_column == -1)
1743 gtk_tree_model_get (combo_box->priv->model, &iter,
1744 combo_box->priv->col_column, cols,
1750 if (combo_box->priv->row_column == -1)
1753 gtk_tree_model_get (combo_box->priv->model, &iter,
1754 combo_box->priv->row_column, rows,
1760 menu_occupied (GtkMenu *menu,
1764 guint bottom_attach)
1768 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1769 g_return_val_if_fail (left_attach < right_attach, TRUE);
1770 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1772 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1775 gboolean h_intersect = FALSE;
1776 gboolean v_intersect = FALSE;
1778 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1781 "bottom_attach", &b,
1785 /* look if this item intersects with the given coordinates */
1786 h_intersect = left_attach <= l && l <= right_attach;
1787 h_intersect &= left_attach <= r && r <= right_attach;
1789 v_intersect = top_attach <= t && t <= bottom_attach;
1790 v_intersect &= top_attach <= b && b <= bottom_attach;
1792 if (h_intersect && v_intersect)
1800 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1803 gint current_col = 0, current_row = 0;
1809 menu = combo_box->priv->popup_widget;
1810 if (!GTK_IS_MENU_SHELL (menu))
1813 list = gtk_container_get_children (GTK_CONTAINER (menu));
1814 item = g_list_nth_data (list, index);
1817 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1819 /* look for a good spot */
1822 if (current_col + cols > combo_box->priv->wrap_width)
1828 if (!menu_occupied (GTK_MENU (menu),
1829 current_col, current_col + cols,
1830 current_row, current_row + rows))
1836 /* set attach props */
1837 gtk_menu_attach (GTK_MENU (menu), item,
1838 current_col, current_col + cols,
1839 current_row, current_row + rows);
1843 gtk_combo_box_relayout (GtkComboBox *combo_box)
1849 /* ensure we are in menu style */
1850 if (combo_box->priv->tree_view)
1851 gtk_combo_box_list_destroy (combo_box);
1853 menu = combo_box->priv->popup_widget;
1855 if (!GTK_IS_MENU_SHELL (menu))
1857 gtk_combo_box_menu_setup (combo_box, FALSE);
1858 menu = combo_box->priv->popup_widget;
1861 /* get rid of all children */
1862 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1864 list = gtk_container_get_children (GTK_CONTAINER (menu));
1866 for (j = g_list_last (list); j; j = j->prev)
1867 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1872 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1874 for (i = 0; i < items; i++)
1879 path = gtk_tree_path_new_from_indices (i, -1);
1880 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1883 g_signal_connect (tmp, "activate",
1884 G_CALLBACK (gtk_combo_box_menu_item_activate),
1887 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1889 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1891 if (combo_box->priv->wrap_width)
1892 gtk_combo_box_relayout_item (combo_box, i);
1894 gtk_widget_show (tmp);
1896 gtk_tree_path_free (path);
1902 gtk_combo_box_menu_button_press (GtkWidget *widget,
1903 GdkEventButton *event,
1906 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1908 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1911 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1913 combo_box->priv->popup_in_progress = TRUE;
1915 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1916 combo_box->priv->active_item);
1918 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1920 gtk_combo_box_menu_position, combo_box,
1921 event->button, event->time);
1930 gtk_combo_box_menu_item_activate (GtkWidget *item,
1935 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1937 menu = combo_box->priv->popup_widget;
1938 g_return_if_fail (GTK_IS_MENU (menu));
1940 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1942 gtk_combo_box_set_active (combo_box, index);
1946 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1951 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1952 gint index = gtk_tree_path_get_indices (path)[0];
1953 gint items = gtk_tree_model_iter_n_children (model, NULL);
1955 if (combo_box->priv->active_item >= index)
1956 combo_box->priv->active_item++;
1958 if (!combo_box->priv->tree_view)
1959 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1962 gtk_combo_box_set_active (combo_box, 0);
1966 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1970 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1971 gint index = gtk_tree_path_get_indices (path)[0];
1973 if (!combo_box->priv->tree_view)
1974 gtk_combo_box_menu_row_deleted (model, path, user_data);
1976 if (index == combo_box->priv->active_item)
1978 gint items = gtk_tree_model_iter_n_children (model, NULL);
1981 gtk_combo_box_set_active_internal (combo_box, -1);
1982 else if (index == items)
1983 gtk_combo_box_set_active_internal (combo_box, index - 1);
1985 gtk_combo_box_set_active_internal (combo_box, index);
1987 else if (combo_box->priv->active_item > index)
1988 combo_box->priv->active_item--;
1992 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1998 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1999 gint items = gtk_tree_model_iter_n_children (model, NULL);
2002 for (i = 0; i < items; i++)
2003 if (new_order[i] == combo_box->priv->active_item)
2005 combo_box->priv->active_item = i;
2009 if (!combo_box->priv->tree_view)
2010 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2014 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2019 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2020 gint index = gtk_tree_path_get_indices (path)[0];
2022 if (index == combo_box->priv->active_item &&
2023 combo_box->priv->cell_view)
2024 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2026 if (combo_box->priv->tree_view)
2027 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2029 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2034 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2041 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2043 if (!combo_box->priv->popup_widget)
2046 menu = combo_box->priv->popup_widget;
2047 g_return_if_fail (GTK_IS_MENU (menu));
2049 item = gtk_cell_view_menu_item_new_from_model (model, path);
2050 g_signal_connect (item, "activate",
2051 G_CALLBACK (gtk_combo_box_menu_item_activate),
2054 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2056 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2057 gtk_tree_path_get_indices (path)[0]);
2058 gtk_widget_show (item);
2062 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2069 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2071 if (!combo_box->priv->popup_widget)
2074 index = gtk_tree_path_get_indices (path)[0];
2076 menu = combo_box->priv->popup_widget;
2077 g_return_if_fail (GTK_IS_MENU (menu));
2079 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2080 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2082 gtk_container_remove (GTK_CONTAINER (menu), item);
2086 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2092 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2094 gtk_combo_box_relayout (combo_box);
2098 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2103 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2106 if (!combo_box->priv->popup_widget)
2109 if (combo_box->priv->wrap_width)
2110 gtk_combo_box_relayout_item (combo_box,
2111 gtk_tree_path_get_indices (path)[0]);
2113 width = gtk_combo_box_calc_requested_width (combo_box, path);
2115 if (width > combo_box->priv->width)
2117 if (combo_box->priv->cell_view)
2119 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2120 gtk_widget_queue_resize (combo_box->priv->cell_view);
2122 combo_box->priv->width = width;
2131 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2134 GtkTreeSelection *sel;
2136 /* Unset any existing model. */
2137 gtk_combo_box_unset_model (combo_box);
2139 combo_box->priv->button = gtk_toggle_button_new ();
2140 gtk_widget_set_parent (combo_box->priv->button,
2141 GTK_BIN (combo_box)->child->parent);
2142 g_signal_connect (combo_box->priv->button, "button_press_event",
2143 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2144 g_signal_connect (combo_box->priv->button, "toggled",
2145 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2146 g_signal_connect_after (combo_box, "key_press_event",
2147 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2149 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2150 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2151 combo_box->priv->arrow);
2152 combo_box->priv->separator = NULL;
2153 gtk_widget_show_all (combo_box->priv->button);
2155 if (combo_box->priv->cell_view)
2157 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2158 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2159 GTK_BIN (combo_box)->child->parent);
2160 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2163 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2164 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2166 gtk_widget_show (combo_box->priv->cell_view_frame);
2169 combo_box->priv->tree_view = gtk_tree_view_new ();
2170 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2171 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2172 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2175 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2176 G_CALLBACK (gtk_combo_box_list_button_pressed),
2178 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2179 G_CALLBACK (gtk_combo_box_list_button_released),
2181 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2182 G_CALLBACK (gtk_combo_box_list_key_press),
2185 combo_box->priv->column = gtk_tree_view_column_new ();
2186 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2187 combo_box->priv->column);
2189 /* set the models */
2190 gtk_combo_box_set_model_internal (combo_box);
2193 for (i = combo_box->priv->cells; i; i = i->next)
2196 ComboCellInfo *info = (ComboCellInfo *)i->data;
2198 if (info->pack == GTK_PACK_START)
2199 gtk_tree_view_column_pack_start (combo_box->priv->column,
2200 info->cell, info->expand);
2201 else if (info->pack == GTK_PACK_END)
2202 gtk_tree_view_column_pack_end (combo_box->priv->column,
2203 info->cell, info->expand);
2205 for (j = info->attributes; j; j = j->next->next)
2207 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2210 GPOINTER_TO_INT (j->next->data));
2214 if (combo_box->priv->active_item != -1)
2218 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2221 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2223 gtk_tree_path_free (path);
2227 /* set sample/popup widgets */
2228 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2230 gtk_widget_show (combo_box->priv->tree_view);
2234 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2236 /* disconnect signals */
2237 gtk_combo_box_unset_model (combo_box);
2239 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2240 G_SIGNAL_MATCH_DATA,
2241 0, 0, NULL, NULL, combo_box);
2242 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2243 G_SIGNAL_MATCH_DATA,
2245 gtk_combo_box_list_button_pressed,
2248 /* destroy things (unparent will kill the latest ref from us)
2249 * last unref on button will destroy the arrow
2251 gtk_widget_unparent (combo_box->priv->button);
2252 combo_box->priv->button = NULL;
2253 combo_box->priv->arrow = NULL;
2255 if (combo_box->priv->cell_view)
2257 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2258 "background_set", FALSE,
2261 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2262 combo_box->priv->cell_view_frame = NULL;
2265 gtk_widget_destroy (combo_box->priv->tree_view);
2267 combo_box->priv->tree_view = NULL;
2268 combo_box->priv->popup_widget = NULL;
2273 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2275 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2276 gtk_grab_remove (combo_box->priv->tree_view);
2278 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2280 gtk_grab_remove (combo_box->priv->popup_window);
2281 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2282 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2287 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2288 GdkEventButton *event,
2291 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2293 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2295 if (ewidget == combo_box->priv->tree_view)
2298 if ((ewidget != combo_box->priv->button) ||
2299 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2302 gtk_combo_box_popup (combo_box);
2304 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2307 combo_box->priv->popup_in_progress = TRUE;
2313 gtk_combo_box_list_button_released (GtkWidget *widget,
2314 GdkEventButton *event,
2318 GtkTreePath *path = NULL;
2320 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2322 gboolean popup_in_progress = FALSE;
2324 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2326 if (combo_box->priv->popup_in_progress)
2328 popup_in_progress = TRUE;
2329 combo_box->priv->popup_in_progress = FALSE;
2332 if (ewidget != combo_box->priv->tree_view)
2334 if (ewidget == combo_box->priv->button &&
2335 !popup_in_progress &&
2336 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2338 gtk_combo_box_popdown (combo_box);
2342 /* released outside treeview */
2343 if (ewidget != combo_box->priv->button)
2345 gtk_combo_box_popdown (combo_box);
2353 /* select something cool */
2354 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2360 return TRUE; /* clicked outside window? */
2362 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2363 gtk_combo_box_popdown (combo_box);
2365 gtk_tree_path_free (path);
2371 gtk_combo_box_key_press (GtkWidget *widget,
2375 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2376 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2377 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2378 gint index = gtk_combo_box_get_active (combo_box);
2381 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2382 state == GDK_MOD1_MASK)
2384 gtk_combo_box_popup (combo_box);
2389 switch (event->keyval)
2393 new_index = index + 1;
2397 new_index = index - 1;
2400 case GDK_KP_Page_Up:
2406 case GDK_KP_Page_Down:
2409 new_index = items - 1;
2415 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2421 gtk_combo_box_menu_key_press (GtkWidget *widget,
2425 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2426 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2428 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2429 state == GDK_MOD1_MASK)
2431 gtk_combo_box_popdown (combo_box);
2440 gtk_combo_box_list_key_press (GtkWidget *widget,
2444 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2445 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2447 if (event->keyval == GDK_Escape ||
2448 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2449 state == GDK_MOD1_MASK))
2451 /* reset active item -- this is incredibly lame and ugly */
2452 gtk_combo_box_set_active (combo_box,
2453 gtk_combo_box_get_active (combo_box));
2455 gtk_combo_box_popdown (combo_box);
2460 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2461 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2465 GtkTreeModel *model = NULL;
2466 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2468 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2473 path = gtk_tree_model_get_path (model, &iter);
2476 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2477 gtk_tree_path_free (path);
2486 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2491 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2494 width = gtk_combo_box_calc_requested_width (combo_box, path);
2496 if (width > combo_box->priv->width)
2498 if (combo_box->priv->cell_view)
2500 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2501 gtk_widget_queue_resize (combo_box->priv->cell_view);
2503 combo_box->priv->width = width;
2508 * GtkCellLayout implementation
2511 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2512 GtkCellRenderer *cell,
2515 ComboCellInfo *info;
2516 GtkComboBox *combo_box;
2519 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2520 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2522 combo_box = GTK_COMBO_BOX (layout);
2524 g_object_ref (G_OBJECT (cell));
2525 gtk_object_sink (GTK_OBJECT (cell));
2527 info = g_new0 (ComboCellInfo, 1);
2529 info->expand = expand;
2530 info->pack = GTK_PACK_START;
2532 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2534 if (combo_box->priv->cell_view)
2535 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2538 if (combo_box->priv->column)
2539 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2541 menu = combo_box->priv->popup_widget;
2542 if (GTK_IS_MENU (menu))
2546 list = gtk_container_get_children (GTK_CONTAINER (menu));
2547 for (i = list; i; i = i->next)
2551 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2552 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2554 view = GTK_CELL_VIEW (i->data);
2556 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2563 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2564 GtkCellRenderer *cell,
2567 ComboCellInfo *info;
2568 GtkComboBox *combo_box;
2571 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2572 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2574 combo_box = GTK_COMBO_BOX (layout);
2576 g_object_ref (G_OBJECT (cell));
2577 gtk_object_sink (GTK_OBJECT (cell));
2579 info = g_new0 (ComboCellInfo, 1);
2581 info->expand = expand;
2582 info->pack = GTK_PACK_END;
2584 if (combo_box->priv->cell_view)
2585 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2588 if (combo_box->priv->column)
2589 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2591 menu = combo_box->priv->popup_widget;
2592 if (GTK_IS_MENU (menu))
2596 list = gtk_container_get_children (GTK_CONTAINER (menu));
2597 for (i = list; i; i = i->next)
2601 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2602 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2604 view = GTK_CELL_VIEW (i->data);
2606 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2613 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2616 GtkComboBox *combo_box;
2619 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2621 combo_box = GTK_COMBO_BOX (layout);
2623 if (combo_box->priv->cell_view)
2624 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2626 if (combo_box->priv->column)
2627 gtk_tree_view_column_clear (combo_box->priv->column);
2629 for (i = combo_box->priv->cells; i; i = i->next)
2631 ComboCellInfo *info = (ComboCellInfo *)i->data;
2633 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2634 g_object_unref (G_OBJECT (info->cell));
2638 g_slist_free (combo_box->priv->cells);
2639 combo_box->priv->cells = NULL;
2641 menu = combo_box->priv->popup_widget;
2642 if (GTK_IS_MENU (menu))
2646 list = gtk_container_get_children (GTK_CONTAINER (menu));
2647 for (i = list; i; i = i->next)
2651 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2652 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2654 view = GTK_CELL_VIEW (i->data);
2656 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2663 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2664 GtkCellRenderer *cell,
2665 const gchar *attribute,
2668 ComboCellInfo *info;
2669 GtkComboBox *combo_box;
2672 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2673 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2675 combo_box = GTK_COMBO_BOX (layout);
2677 info = gtk_combo_box_get_cell_info (combo_box, cell);
2679 info->attributes = g_slist_prepend (info->attributes,
2680 GINT_TO_POINTER (column));
2681 info->attributes = g_slist_prepend (info->attributes,
2682 g_strdup (attribute));
2684 if (combo_box->priv->cell_view)
2685 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2686 cell, attribute, column);
2688 if (combo_box->priv->column)
2689 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2690 cell, attribute, column);
2692 menu = combo_box->priv->popup_widget;
2693 if (GTK_IS_MENU (menu))
2697 list = gtk_container_get_children (GTK_CONTAINER (menu));
2698 for (i = list; i; i = i->next)
2702 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2703 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2705 view = GTK_CELL_VIEW (i->data);
2707 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2713 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2717 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2718 GtkCellRenderer *cell,
2719 GtkCellLayoutDataFunc func,
2721 GDestroyNotify destroy)
2723 ComboCellInfo *info;
2724 GtkComboBox *combo_box;
2727 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2729 combo_box = GTK_COMBO_BOX (layout);
2731 info = gtk_combo_box_get_cell_info (combo_box, cell);
2732 g_return_if_fail (info != NULL);
2736 GDestroyNotify d = info->destroy;
2738 info->destroy = NULL;
2739 d (info->func_data);
2743 info->func_data = func_data;
2744 info->destroy = destroy;
2746 if (combo_box->priv->cell_view)
2747 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2749 if (combo_box->priv->column)
2750 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2752 menu = combo_box->priv->popup_widget;
2753 if (GTK_IS_MENU (menu))
2757 list = gtk_container_get_children (GTK_CONTAINER (menu));
2758 for (i = list; i; i = i->next)
2762 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2763 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2765 view = GTK_CELL_VIEW (i->data);
2767 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2768 func, func_data, NULL);
2773 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2777 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2778 GtkCellRenderer *cell)
2780 ComboCellInfo *info;
2781 GtkComboBox *combo_box;
2785 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2786 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2788 combo_box = GTK_COMBO_BOX (layout);
2790 info = gtk_combo_box_get_cell_info (combo_box, cell);
2791 g_return_if_fail (info != NULL);
2793 list = info->attributes;
2794 while (list && list->next)
2796 g_free (list->data);
2797 list = list->next->next;
2799 g_slist_free (info->attributes);
2800 info->attributes = NULL;
2802 if (combo_box->priv->cell_view)
2803 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2805 if (combo_box->priv->column)
2806 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2808 menu = combo_box->priv->popup_widget;
2809 if (GTK_IS_MENU (menu))
2813 list = gtk_container_get_children (GTK_CONTAINER (menu));
2814 for (i = list; i; i = i->next)
2818 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2819 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2821 view = GTK_CELL_VIEW (i->data);
2823 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2828 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2832 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2833 GtkCellRenderer *cell,
2836 ComboCellInfo *info;
2837 GtkComboBox *combo_box;
2841 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2842 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2844 combo_box = GTK_COMBO_BOX (layout);
2846 info = gtk_combo_box_get_cell_info (combo_box, cell);
2848 g_return_if_fail (info != NULL);
2849 g_return_if_fail (position >= 0);
2851 link = g_slist_find (combo_box->priv->cells, info);
2853 g_return_if_fail (link != NULL);
2855 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2856 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2859 if (combo_box->priv->cell_view)
2860 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2863 if (combo_box->priv->column)
2864 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2867 menu = combo_box->priv->popup_widget;
2868 if (GTK_IS_MENU (menu))
2872 list = gtk_container_get_children (GTK_CONTAINER (menu));
2873 for (i = list; i; i = i->next)
2877 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2878 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2880 view = GTK_CELL_VIEW (i->data);
2882 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2887 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2895 * gtk_combo_box_new:
2897 * Creates a new empty #GtkComboBox.
2899 * Return value: A new #GtkComboBox.
2904 gtk_combo_box_new (void)
2906 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2910 * gtk_combo_box_new_with_model:
2911 * @model: A #GtkTreeModel.
2913 * Creates a new #GtkComboBox with the model initialized to @model.
2915 * Return value: A new #GtkComboBox.
2920 gtk_combo_box_new_with_model (GtkTreeModel *model)
2922 GtkComboBox *combo_box;
2924 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2926 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2930 return GTK_WIDGET (combo_box);
2934 * gtk_combo_box_set_wrap_width:
2935 * @combo_box: A #GtkComboBox.
2936 * @width: Preferred number of columns.
2938 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2939 * the preferred number of columns when you want to the popup to be layed out
2945 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2948 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2949 g_return_if_fail (width >= 0);
2951 if (width != combo_box->priv->wrap_width)
2953 combo_box->priv->wrap_width = width;
2955 gtk_combo_box_relayout (combo_box);
2956 gtk_combo_box_style_set (GTK_WIDGET (combo_box), NULL);
2958 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2963 * gtk_combo_box_set_row_span_column:
2964 * @combo_box: A #GtkComboBox.
2965 * @row_span: A column in the model passed during construction.
2967 * Sets the column with row span information for @combo_box to be @row_span.
2968 * The row span column contains integers which indicate how many rows
2969 * an item should span.
2974 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2979 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2981 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2982 g_return_if_fail (row_span >= 0 && row_span < col);
2984 if (row_span != combo_box->priv->row_column)
2986 combo_box->priv->row_column = row_span;
2988 gtk_combo_box_relayout (combo_box);
2990 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2995 * gtk_combo_box_set_column_span_column:
2996 * @combo_box: A #GtkComboBox.
2997 * @column_span: A column in the model passed during construction.
2999 * Sets the column with column span information for @combo_box to be
3000 * @column_span. The column span column contains integers which indicate
3001 * how many columns an item should span.
3006 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
3011 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3013 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3014 g_return_if_fail (column_span >= 0 && column_span < col);
3016 if (column_span != combo_box->priv->col_column)
3018 combo_box->priv->col_column = column_span;
3020 gtk_combo_box_relayout (combo_box);
3022 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3027 * gtk_combo_box_get_active:
3028 * @combo_box: A #GtkComboBox.
3030 * Returns the index of the currently active item, or -1 if there's no
3033 * Return value: An integer which is the index of the currently active item, or
3034 * -1 if there's no active item.
3039 gtk_combo_box_get_active (GtkComboBox *combo_box)
3041 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3043 return combo_box->priv->active_item;
3047 * gtk_combo_box_set_active:
3048 * @combo_box: A #GtkComboBox.
3049 * @index: An index in the model passed during construction, or -1 to have
3052 * Sets the active item of @combo_box to be the item at @index.
3057 gtk_combo_box_set_active (GtkComboBox *combo_box,
3060 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3061 /* -1 means "no item selected" */
3062 g_return_if_fail (index >= -1);
3064 if (combo_box->priv->active_item == index)
3067 gtk_combo_box_set_active_internal (combo_box, index);
3071 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3076 combo_box->priv->active_item = index;
3080 if (combo_box->priv->tree_view)
3081 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3084 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3086 if (GTK_IS_MENU (menu))
3087 gtk_menu_set_active (menu, -1);
3090 if (combo_box->priv->cell_view)
3091 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3095 path = gtk_tree_path_new_from_indices (index, -1);
3097 if (combo_box->priv->tree_view)
3098 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3101 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3103 if (GTK_IS_MENU (menu))
3104 gtk_menu_set_active (GTK_MENU (menu), index);
3107 if (combo_box->priv->cell_view)
3108 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3110 gtk_tree_path_free (path);
3113 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3118 * gtk_combo_box_get_active_iter:
3119 * @combo_box: A #GtkComboBox
3120 * @iter: The uninitialized #GtkTreeIter.
3122 * Sets @iter to point to the current active item, if it exists.
3124 * Return value: %TRUE, if @iter was set
3129 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3136 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3138 active = gtk_combo_box_get_active (combo_box);
3142 path = gtk_tree_path_new_from_indices (active, -1);
3143 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3145 gtk_tree_path_free (path);
3151 * gtk_combo_box_set_active_iter:
3152 * @combo_box: A #GtkComboBox
3153 * @iter: The #GtkTreeIter.
3155 * Sets the current active item to be the one referenced by @iter.
3156 * @iter must correspond to a path of depth one.
3161 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3166 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3168 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3169 g_return_if_fail (path != NULL);
3170 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3172 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3173 gtk_tree_path_free (path);
3177 * gtk_combo_box_set_model:
3178 * @combo_box: A #GtkComboBox.
3179 * @model: A #GtkTreeModel.
3181 * Sets the model used by @combo_box to be @model. Will unset a
3182 * previously set model (if applicable).
3187 gtk_combo_box_set_model (GtkComboBox *combo_box,
3188 GtkTreeModel *model)
3190 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3191 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3193 if (combo_box->priv->model)
3195 gtk_combo_box_unset_model (combo_box);
3196 g_object_unref (G_OBJECT (combo_box->priv->model));
3199 combo_box->priv->model = model;
3200 g_object_ref (G_OBJECT (combo_box->priv->model));
3202 gtk_combo_box_set_model_internal (combo_box);
3203 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
3204 gtk_combo_box_menu_fill (combo_box);
3206 if (combo_box->priv->cell_view)
3207 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3208 combo_box->priv->model);
3212 * gtk_combo_box_get_model
3213 * @combo_box: A #GtkComboBox.
3215 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3217 * Return value: A #GtkTreeModel which was passed during construction.
3222 gtk_combo_box_get_model (GtkComboBox *combo_box)
3224 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3226 return combo_box->priv->model;
3230 /* convenience API for simple text combos */
3233 * gtk_combo_box_new_text:
3235 * Convenience function which constructs a new text combo box, which is a
3236 * #GtkComboBox just displaying strings. If you use this function to create
3237 * a text combo box, you should only manipulate its data source with the
3238 * following convenience functions: gtk_combo_box_append_text(),
3239 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3240 * gtk_combo_box_remove_text().
3242 * Return value: A new text combo box.
3247 gtk_combo_box_new_text (void)
3249 GtkWidget *combo_box;
3250 GtkCellRenderer *cell;
3251 GtkListStore *store;
3253 store = gtk_list_store_new (1, G_TYPE_STRING);
3255 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3257 cell = gtk_cell_renderer_text_new ();
3258 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3259 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3267 * gtk_combo_box_append_text:
3268 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3271 * Appends @string to the list of strings stored in @combo_box. Note that
3272 * you can only use this function with combo boxes constructed with
3273 * gtk_combo_box_new_text().
3278 gtk_combo_box_append_text (GtkComboBox *combo_box,
3282 GtkListStore *store;
3284 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3285 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3286 g_return_if_fail (text != NULL);
3288 store = GTK_LIST_STORE (combo_box->priv->model);
3290 gtk_list_store_append (store, &iter);
3291 gtk_list_store_set (store, &iter, 0, text, -1);
3295 * gtk_combo_box_insert_text:
3296 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3297 * @position: An index to insert @text.
3300 * Inserts @string at @position in the list of strings stored in @combo_box.
3301 * Note that you can only use this function with combo boxes constructed
3302 * with gtk_combo_box_new_text().
3307 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3312 GtkListStore *store;
3314 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3315 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3316 g_return_if_fail (position >= 0);
3317 g_return_if_fail (text != NULL);
3319 store = GTK_LIST_STORE (combo_box->priv->model);
3321 gtk_list_store_insert (store, &iter, position);
3322 gtk_list_store_set (store, &iter, 0, text, -1);
3326 * gtk_combo_box_prepend_text:
3327 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3330 * Prepends @string to the list of strings stored in @combo_box. Note that
3331 * you can only use this function with combo boxes constructed with
3332 * gtk_combo_box_new_text().
3337 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3341 GtkListStore *store;
3343 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3344 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3345 g_return_if_fail (text != NULL);
3347 store = GTK_LIST_STORE (combo_box->priv->model);
3349 gtk_list_store_prepend (store, &iter);
3350 gtk_list_store_set (store, &iter, 0, text, -1);
3354 * gtk_combo_box_remove_text:
3355 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3356 * @position: Index of the item to remove.
3358 * Removes the string at @position from @combo_box. Note that you can only use
3359 * this function with combo boxes constructed with gtk_combo_box_new_text().
3364 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3368 GtkListStore *store;
3370 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3371 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3372 g_return_if_fail (position >= 0);
3374 store = GTK_LIST_STORE (combo_box->priv->model);
3376 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3378 gtk_list_store_remove (store, &iter);
3383 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3384 gboolean group_cycling)
3386 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3388 gtk_widget_grab_focus (combo_box->priv->button);
3394 gtk_combo_box_destroy (GtkObject *object)
3396 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3398 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3400 combo_box->priv->cell_view = NULL;
3404 gtk_combo_box_finalize (GObject *object)
3406 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3409 gtk_combo_box_unset_model (combo_box);
3411 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3412 gtk_combo_box_menu_destroy (combo_box);
3414 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3415 gtk_combo_box_list_destroy (combo_box);
3417 if (combo_box->priv->popup_window)
3418 gtk_widget_destroy (combo_box->priv->popup_window);
3420 if (combo_box->priv->model)
3421 g_object_unref (combo_box->priv->model);
3423 for (i = combo_box->priv->cells; i; i = i->next)
3425 ComboCellInfo *info = (ComboCellInfo *)i->data;
3426 GSList *list = info->attributes;
3429 info->destroy (info->func_data);
3431 while (list && list->next)
3433 g_free (list->data);
3434 list = list->next->next;
3436 g_slist_free (info->attributes);
3438 g_object_unref (G_OBJECT (info->cell));
3441 g_slist_free (combo_box->priv->cells);
3443 G_OBJECT_CLASS (parent_class)->finalize (object);