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);
234 static void gtk_combo_box_set_model_internal (GtkComboBox *combo_box);
236 static void gtk_combo_box_size_request (GtkWidget *widget,
237 GtkRequisition *requisition);
238 static void gtk_combo_box_size_allocate (GtkWidget *widget,
239 GtkAllocation *allocation);
240 static void gtk_combo_box_forall (GtkContainer *container,
241 gboolean include_internals,
242 GtkCallback callback,
243 gpointer callback_data);
244 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
245 GdkEventExpose *event);
246 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
247 GdkEventScroll *event);
248 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
250 static gboolean gtk_combo_box_key_press (GtkWidget *widget,
254 /* listening to the model */
255 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
259 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
262 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
267 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
273 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
278 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
279 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
281 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
283 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
284 GdkEventButton *event,
286 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
289 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
290 GdkEventButton *event,
293 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
299 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
300 gboolean add_childs);
301 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
302 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
304 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
308 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
310 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
312 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
313 GdkEventButton *event,
315 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
317 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
321 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
324 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
329 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
333 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
338 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
339 GtkCellRenderer *cell,
341 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
342 GtkCellRenderer *cell,
344 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
345 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
346 GtkCellRenderer *cell,
347 const gchar *attribute,
349 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
350 GtkCellRenderer *cell,
351 GtkCellLayoutDataFunc func,
353 GDestroyNotify destroy);
354 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
355 GtkCellRenderer *cell);
356 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
357 GtkCellRenderer *cell,
359 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
360 gboolean group_cycling);
364 gtk_combo_box_get_type (void)
366 static GType combo_box_type = 0;
370 static const GTypeInfo combo_box_info =
372 sizeof (GtkComboBoxClass),
373 NULL, /* base_init */
374 NULL, /* base_finalize */
375 (GClassInitFunc) gtk_combo_box_class_init,
376 NULL, /* class_finalize */
377 NULL, /* class_data */
378 sizeof (GtkComboBox),
380 (GInstanceInitFunc) gtk_combo_box_init
383 static const GInterfaceInfo cell_layout_info =
385 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
390 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
395 g_type_add_interface_static (combo_box_type,
396 GTK_TYPE_CELL_LAYOUT,
400 return combo_box_type;
405 gtk_combo_box_class_init (GtkComboBoxClass *klass)
407 GObjectClass *object_class;
408 GtkBindingSet *binding_set;
409 GtkObjectClass *gtk_object_class;
410 GtkContainerClass *container_class;
411 GtkWidgetClass *widget_class;
413 binding_set = gtk_binding_set_by_class (klass);
415 container_class = (GtkContainerClass *)klass;
416 container_class->forall = gtk_combo_box_forall;
417 container_class->add = gtk_combo_box_add;
419 widget_class = (GtkWidgetClass *)klass;
420 widget_class->size_allocate = gtk_combo_box_size_allocate;
421 widget_class->size_request = gtk_combo_box_size_request;
422 widget_class->expose_event = gtk_combo_box_expose_event;
423 widget_class->scroll_event = gtk_combo_box_scroll_event;
424 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
425 widget_class->style_set = gtk_combo_box_style_set;
426 widget_class->state_changed = gtk_combo_box_state_changed;
428 gtk_object_class = (GtkObjectClass *)klass;
429 gtk_object_class->destroy = gtk_combo_box_destroy;
431 object_class = (GObjectClass *)klass;
432 object_class->finalize = gtk_combo_box_finalize;
433 object_class->set_property = gtk_combo_box_set_property;
434 object_class->get_property = gtk_combo_box_get_property;
436 parent_class = g_type_class_peek_parent (klass);
439 combo_box_signals[CHANGED] =
440 g_signal_new ("changed",
441 G_OBJECT_CLASS_TYPE (klass),
443 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
445 g_cclosure_marshal_VOID__VOID,
449 g_object_class_install_property (object_class,
451 g_param_spec_object ("model",
452 P_("ComboBox model"),
453 P_("The model for the combo box"),
457 g_object_class_install_property (object_class,
459 g_param_spec_int ("wrap_width",
461 P_("Wrap width for layouting the items in a grid"),
467 g_object_class_install_property (object_class,
468 PROP_ROW_SPAN_COLUMN,
469 g_param_spec_int ("row_span_column",
470 P_("Row span column"),
471 P_("TreeModel column containing the row span values"),
477 g_object_class_install_property (object_class,
478 PROP_COLUMN_SPAN_COLUMN,
479 g_param_spec_int ("column_span_column",
480 P_("Column span column"),
481 P_("TreeModel column containing the column span values"),
487 g_object_class_install_property (object_class,
489 g_param_spec_int ("active",
491 P_("The item which is currently active"),
497 gtk_widget_class_install_style_property (widget_class,
498 g_param_spec_boolean ("appears-as-list",
499 P_("Appears as list"),
500 P_("Whether combobox dropdowns should look like lists rather than menus"),
504 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
508 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
510 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
511 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
512 iface->clear = gtk_combo_box_cell_layout_clear;
513 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
514 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
515 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
516 iface->reorder = gtk_combo_box_cell_layout_reorder;
520 gtk_combo_box_init (GtkComboBox *combo_box)
522 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
524 combo_box->priv->cell_view = gtk_cell_view_new ();
525 gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
526 gtk_widget_show (combo_box->priv->cell_view);
528 combo_box->priv->width = 0;
529 combo_box->priv->wrap_width = 0;
531 combo_box->priv->active_item = -1;
532 combo_box->priv->col_column = -1;
533 combo_box->priv->row_column = -1;
537 gtk_combo_box_set_property (GObject *object,
542 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
547 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
550 case PROP_WRAP_WIDTH:
551 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
554 case PROP_ROW_SPAN_COLUMN:
555 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
558 case PROP_COLUMN_SPAN_COLUMN:
559 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
563 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
572 gtk_combo_box_get_property (GObject *object,
577 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
582 g_value_set_object (value, combo_box->priv->model);
585 case PROP_WRAP_WIDTH:
586 g_value_set_int (value, combo_box->priv->wrap_width);
589 case PROP_ROW_SPAN_COLUMN:
590 g_value_set_int (value, combo_box->priv->row_column);
593 case PROP_COLUMN_SPAN_COLUMN:
594 g_value_set_int (value, combo_box->priv->col_column);
598 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
602 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
608 gtk_combo_box_state_changed (GtkWidget *widget,
609 GtkStateType previous)
611 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
613 if (GTK_WIDGET_REALIZED (widget))
615 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
616 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
617 &widget->style->base[GTK_WIDGET_STATE (widget)]);
620 gtk_widget_queue_draw (widget);
624 gtk_combo_box_style_set (GtkWidget *widget,
628 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
630 gtk_widget_queue_resize (widget);
632 /* if wrap_width > 0, then we are in grid-mode and forced to use
635 if (combo_box->priv->wrap_width)
638 gtk_widget_style_get (widget,
639 "appears-as-list", &appearance,
642 /* TRUE is windows style */
645 /* Destroy all the menu mode widgets, if they exist. */
646 if (GTK_IS_MENU (combo_box->priv->popup_widget))
647 gtk_combo_box_menu_destroy (combo_box);
649 /* Create the list mode widgets, if they don't already exist. */
650 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
651 gtk_combo_box_list_setup (combo_box);
655 /* Destroy all the list mode widgets, if they exist. */
656 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
657 gtk_combo_box_list_destroy (combo_box);
659 /* Create the menu mode widgets, if they don't already exist. */
660 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
661 gtk_combo_box_menu_setup (combo_box, TRUE);
664 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
665 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
666 &widget->style->base[GTK_WIDGET_STATE (widget)]);
671 gtk_combo_box_button_toggled (GtkWidget *widget,
674 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
676 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
678 if (!combo_box->priv->popup_in_progress)
679 gtk_combo_box_popup (combo_box);
682 gtk_combo_box_popdown (combo_box);
686 gtk_combo_box_add (GtkContainer *container,
689 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
691 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
692 gtk_container_remove (container, combo_box->priv->cell_view);
694 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
696 if (combo_box->priv->cell_view &&
697 widget != combo_box->priv->cell_view)
699 /* since the cell_view was unparented, it's gone now */
700 combo_box->priv->cell_view = NULL;
702 if (!combo_box->priv->tree_view && combo_box->priv->separator)
704 gtk_widget_unparent (combo_box->priv->separator);
705 combo_box->priv->separator = NULL;
707 g_object_ref (G_OBJECT (combo_box->priv->arrow));
708 gtk_widget_unparent (combo_box->priv->arrow);
709 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
710 combo_box->priv->arrow);
711 g_object_unref (G_OBJECT (combo_box->priv->arrow));
713 gtk_widget_queue_resize (GTK_WIDGET (container));
715 else if (combo_box->priv->cell_view_frame)
717 gtk_widget_unparent (combo_box->priv->cell_view_frame);
718 combo_box->priv->cell_view_frame = NULL;
723 static ComboCellInfo *
724 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
725 GtkCellRenderer *cell)
729 for (i = combo_box->priv->cells; i; i = i->next)
731 ComboCellInfo *info = (ComboCellInfo *)i->data;
733 if (info && info->cell == cell)
741 gtk_combo_box_menu_show (GtkWidget *menu,
744 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
746 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
748 combo_box->priv->popup_in_progress = FALSE;
752 gtk_combo_box_menu_hide (GtkWidget *menu,
755 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
757 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
762 gtk_combo_box_detacher (GtkWidget *widget,
765 GtkComboBox *combo_box;
767 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
769 combo_box = GTK_COMBO_BOX (widget);
770 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
772 g_signal_handlers_disconnect_by_func (menu,
773 gtk_combo_box_menu_show,
775 g_signal_handlers_disconnect_by_func (menu,
776 gtk_combo_box_menu_hide,
779 combo_box->priv->popup_widget = NULL;
783 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
786 if (GTK_IS_MENU (combo_box->priv->popup_widget))
788 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
789 combo_box->priv->popup_widget = NULL;
791 else if (combo_box->priv->popup_widget)
793 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
794 combo_box->priv->popup_widget);
795 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
796 combo_box->priv->popup_widget = NULL;
799 if (GTK_IS_MENU (popup))
801 if (combo_box->priv->popup_window)
803 gtk_widget_destroy (combo_box->priv->popup_window);
804 combo_box->priv->popup_window = NULL;
805 combo_box->priv->popup_frame = NULL;
808 combo_box->priv->popup_widget = popup;
810 g_signal_connect (popup, "show",
811 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
812 g_signal_connect (popup, "hide",
813 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
815 gtk_menu_attach_to_widget (GTK_MENU (popup),
816 GTK_WIDGET (combo_box),
817 gtk_combo_box_detacher);
821 if (!combo_box->priv->popup_window)
823 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
824 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
825 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
826 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
828 combo_box->priv->popup_frame = gtk_frame_new (NULL);
829 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
830 GTK_SHADOW_ETCHED_IN);
831 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
832 combo_box->priv->popup_frame);
834 gtk_widget_show (combo_box->priv->popup_frame);
837 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
839 gtk_widget_show (popup);
840 g_object_ref (G_OBJECT (popup));
841 combo_box->priv->popup_widget = popup;
846 gtk_combo_box_menu_position_below (GtkMenu *menu,
855 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
857 /* FIXME: is using the size request here broken? */
858 child = GTK_BIN (combo_box)->child;
860 gdk_window_get_origin (child->window, &sx, &sy);
862 gtk_widget_size_request (GTK_WIDGET (menu), &req);
864 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
867 *x = sx + child->allocation.width - req.width;
868 *y = sy + child->allocation.height;
870 if (GTK_WIDGET_NO_WINDOW (child))
872 *x += child->allocation.x;
873 *y += child->allocation.y;
880 gtk_combo_box_menu_position_over (GtkMenu *menu,
886 GtkComboBox *combo_box;
890 GtkRequisition requisition;
897 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
899 combo_box = GTK_COMBO_BOX (user_data);
900 widget = GTK_WIDGET (combo_box);
902 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
903 menu_width = requisition.width;
905 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
906 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
908 menu_xpos += widget->allocation.x;
909 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
913 gtk_widget_get_child_requisition (active, &requisition);
914 menu_ypos -= requisition.height / 2;
917 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
920 child = children->data;
925 if (GTK_WIDGET_VISIBLE (child))
927 gtk_widget_get_child_requisition (child, &requisition);
928 menu_ypos -= requisition.height;
931 children = children->next;
934 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
935 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
937 /* Clamp the position on screen */
938 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
942 else if ((menu_xpos + menu_width) > screen_width)
943 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
952 gtk_combo_box_menu_position (GtkMenu *menu,
958 GtkComboBox *combo_box;
959 GtkWidget *menu_item;
961 combo_box = GTK_COMBO_BOX (user_data);
963 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
964 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
967 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
969 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
972 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
978 gtk_combo_box_list_position (GtkComboBox *combo_box,
987 GdkRectangle monitor;
988 GtkRequisition popup_req;
990 sample = GTK_BIN (combo_box)->child;
992 *width = sample->allocation.width;
993 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
994 *height = popup_req.height;
996 gdk_window_get_origin (sample->window, x, y);
998 if (combo_box->priv->cell_view_frame)
1000 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1001 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1002 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1003 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1006 if (GTK_WIDGET_NO_WINDOW (sample))
1008 *x += sample->allocation.x;
1009 *y += sample->allocation.y;
1012 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1013 monitor_num = gdk_screen_get_monitor_at_window (screen,
1014 GTK_WIDGET (combo_box)->window);
1015 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1019 else if (*x + *width > monitor.x + monitor.width)
1020 *x = monitor.x + monitor.width - *width;
1022 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1023 *y += sample->allocation.height;
1029 * gtk_combo_box_popup:
1030 * @combo_box: a #GtkComboBox
1032 * Pops up the menu or dropdown list of @combo_box.
1034 * This function is mostly intended for use by accessibility technologies;
1035 * applications should have little use for it.
1040 gtk_combo_box_popup (GtkComboBox *combo_box)
1042 gint x, y, width, height;
1044 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1046 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1049 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1051 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1052 combo_box->priv->active_item);
1054 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1056 gtk_combo_box_menu_position, combo_box,
1061 gtk_widget_show_all (combo_box->priv->popup_frame);
1062 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1064 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1065 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1068 gtk_widget_show (combo_box->priv->popup_window);
1070 gtk_widget_grab_focus (combo_box->priv->popup_window);
1071 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1074 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1076 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1077 FALSE, GDK_CURRENT_TIME);
1078 gtk_widget_grab_focus (combo_box->priv->tree_view);
1081 gtk_grab_add (combo_box->priv->popup_window);
1082 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1083 GDK_BUTTON_PRESS_MASK |
1084 GDK_BUTTON_RELEASE_MASK |
1085 GDK_POINTER_MOTION_MASK,
1086 NULL, NULL, GDK_CURRENT_TIME);
1088 gtk_grab_add (combo_box->priv->tree_view);
1092 * gtk_combo_box_popdown:
1093 * @combo_box: a #GtkComboBox
1095 * Hides the menu or dropdown list of @combo_box.
1097 * This function is mostly intended for use by accessibility technologies;
1098 * applications should have little use for it.
1103 gtk_combo_box_popdown (GtkComboBox *combo_box)
1105 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1107 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1109 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1113 gtk_combo_box_list_remove_grabs (combo_box);
1114 gtk_widget_hide_all (combo_box->priv->popup_window);
1115 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1120 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1126 if (combo_box->priv->cell_view)
1127 gtk_widget_style_get (combo_box->priv->cell_view,
1128 "focus-line-width", &padding,
1133 /* add some pixels for good measure */
1134 padding += BONUS_PADDING;
1136 if (combo_box->priv->cell_view)
1137 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1142 return req.width + padding;
1146 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1152 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1155 combo_box->priv->width = 0;
1157 path = gtk_tree_path_new_from_indices (0, -1);
1159 if (combo_box->priv->cell_view)
1160 gtk_widget_style_get (combo_box->priv->cell_view,
1161 "focus-line-width", &padding,
1166 /* add some pixels for good measure */
1167 padding += BONUS_PADDING;
1173 if (combo_box->priv->cell_view)
1174 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1179 combo_box->priv->width = MAX (combo_box->priv->width,
1180 req.width + padding);
1182 gtk_tree_path_next (path);
1184 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1186 gtk_tree_path_free (path);
1190 gtk_combo_box_size_request (GtkWidget *widget,
1191 GtkRequisition *requisition)
1194 GtkRequisition bin_req;
1196 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1199 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1200 gtk_combo_box_remeasure (combo_box);
1201 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1203 if (!combo_box->priv->tree_view)
1207 if (combo_box->priv->cell_view)
1209 GtkRequisition sep_req, arrow_req;
1210 gint border_width, xthickness, ythickness;
1212 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1213 xthickness = combo_box->priv->button->style->xthickness;
1214 ythickness = combo_box->priv->button->style->ythickness;
1216 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1218 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1219 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1221 height = MAX (sep_req.height, arrow_req.height);
1222 height = MAX (height, bin_req.height);
1224 width = bin_req.width + sep_req.width + arrow_req.width;
1226 height += border_width + 1 + xthickness * 2 + 4;
1227 width += border_width + 1 + ythickness * 2 + 4;
1229 requisition->width = width;
1230 requisition->height = height;
1234 GtkRequisition but_req;
1236 gtk_widget_size_request (combo_box->priv->button, &but_req);
1238 requisition->width = bin_req.width + but_req.width;
1239 requisition->height = MAX (bin_req.height, but_req.height);
1245 GtkRequisition button_req;
1247 /* sample + frame */
1248 *requisition = bin_req;
1250 if (combo_box->priv->cell_view_frame)
1252 requisition->width += 2 *
1253 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1254 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1255 requisition->height += 2 *
1256 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1257 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1261 gtk_widget_size_request (combo_box->priv->button, &button_req);
1263 requisition->height = MAX (requisition->height, button_req.height);
1264 requisition->width += button_req.width;
1269 gtk_combo_box_size_allocate (GtkWidget *widget,
1270 GtkAllocation *allocation)
1272 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1273 GtkAllocation child;
1275 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1277 widget->allocation = *allocation;
1279 if (!combo_box->priv->tree_view)
1281 if (combo_box->priv->cell_view)
1283 gint border_width, xthickness, ythickness;
1287 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1289 /* set some things ready */
1290 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1291 xthickness = combo_box->priv->button->style->xthickness;
1292 ythickness = combo_box->priv->button->style->ythickness;
1294 child.x = allocation->x + border_width + 1 + xthickness + 2;
1295 child.y = allocation->y + border_width + 1 + ythickness + 2;
1297 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1299 /* handle the childs */
1300 gtk_widget_size_request (combo_box->priv->arrow, &req);
1301 child.width = req.width;
1302 child.height = allocation->height - 2 * (child.y - allocation->y);
1304 child.x += width - req.width;
1305 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1307 child.x += req.width;
1308 gtk_widget_size_request (combo_box->priv->separator, &req);
1309 child.width = req.width;
1311 child.x -= req.width;
1312 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1316 child.x += req.width;
1317 child.width = allocation->x + allocation->width
1318 - (border_width + 1 + xthickness + 2) - child.x;
1322 child.width = child.x;
1323 child.x = allocation->x + border_width + 1 + xthickness + 2;
1324 child.width -= child.x;
1327 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1331 gtk_widget_size_request (combo_box->priv->button, &req);
1333 child.x = allocation->x;
1335 child.x = allocation->x + allocation->width - req.width;
1336 child.y = allocation->y;
1337 child.width = req.width;
1338 child.height = allocation->height;
1339 gtk_widget_size_allocate (combo_box->priv->button, &child);
1342 child.x = allocation->x + req.width;
1344 child.x = allocation->x;
1345 child.y = allocation->y;
1346 child.width = allocation->width - req.width;
1347 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1355 gtk_widget_size_request (combo_box->priv->button, &req);
1357 child.x = allocation->x;
1359 child.x = allocation->x + allocation->width - req.width;
1360 child.y = allocation->y;
1361 child.width = req.width;
1362 child.height = allocation->height;
1363 gtk_widget_size_allocate (combo_box->priv->button, &child);
1367 child.x = allocation->x + req.width;
1369 child.x = allocation->x;
1370 child.y = allocation->y;
1371 child.width = allocation->width - req.width;
1372 child.height = allocation->height;
1374 if (combo_box->priv->cell_view_frame)
1376 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1380 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1381 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1383 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1384 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1385 child.width -= 2 * (
1386 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1387 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1388 child.height -= 2 * (
1389 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1390 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1393 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1398 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1400 if (combo_box->priv->inserted_id != -1)
1402 g_signal_handler_disconnect (combo_box->priv->model,
1403 combo_box->priv->inserted_id);
1404 combo_box->priv->inserted_id = -1;
1406 if (combo_box->priv->deleted_id != -1)
1408 g_signal_handler_disconnect (combo_box->priv->model,
1409 combo_box->priv->deleted_id);
1410 combo_box->priv->deleted_id = -1;
1412 if (combo_box->priv->reordered_id != -1)
1414 g_signal_handler_disconnect (combo_box->priv->model,
1415 combo_box->priv->reordered_id);
1416 combo_box->priv->reordered_id = -1;
1418 if (combo_box->priv->changed_id != -1)
1420 g_signal_handler_disconnect (combo_box->priv->model,
1421 combo_box->priv->changed_id);
1422 combo_box->priv->changed_id = -1;
1426 if (!combo_box->priv->tree_view)
1428 if (combo_box->priv->popup_widget)
1429 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1430 (GtkCallback)gtk_widget_destroy, NULL);
1435 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1437 combo_box->priv->inserted_id =
1438 g_signal_connect (combo_box->priv->model, "row_inserted",
1439 G_CALLBACK (gtk_combo_box_model_row_inserted),
1441 combo_box->priv->deleted_id =
1442 g_signal_connect (combo_box->priv->model, "row_deleted",
1443 G_CALLBACK (gtk_combo_box_model_row_deleted),
1445 combo_box->priv->reordered_id =
1446 g_signal_connect (combo_box->priv->model, "rows_reordered",
1447 G_CALLBACK (gtk_combo_box_model_rows_reordered),
1449 combo_box->priv->changed_id =
1450 g_signal_connect (combo_box->priv->model, "row_changed",
1451 G_CALLBACK (gtk_combo_box_model_row_changed),
1454 if (combo_box->priv->tree_view)
1457 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1458 combo_box->priv->model);
1463 gtk_combo_box_forall (GtkContainer *container,
1464 gboolean include_internals,
1465 GtkCallback callback,
1466 gpointer callback_data)
1468 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1470 if (include_internals)
1472 if (combo_box->priv->button)
1473 (* callback) (combo_box->priv->button, callback_data);
1474 if (combo_box->priv->separator)
1475 (* callback) (combo_box->priv->separator, callback_data);
1476 if (combo_box->priv->arrow)
1477 (* callback) (combo_box->priv->arrow, callback_data);
1478 if (combo_box->priv->cell_view_frame)
1479 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1482 if (GTK_BIN (container)->child)
1483 (* callback) (GTK_BIN (container)->child, callback_data);
1487 gtk_combo_box_expose_event (GtkWidget *widget,
1488 GdkEventExpose *event)
1490 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1492 if (!combo_box->priv->tree_view)
1494 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1495 combo_box->priv->button, event);
1497 if (combo_box->priv->separator)
1499 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1500 combo_box->priv->separator, event);
1502 /* if not in this case, arrow gets its expose event from button */
1503 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1504 combo_box->priv->arrow, event);
1509 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1510 combo_box->priv->button, event);
1512 if (combo_box->priv->cell_view_frame)
1513 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1514 combo_box->priv->cell_view_frame, event);
1517 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1518 GTK_BIN (widget)->child, event);
1524 gtk_combo_box_scroll_event (GtkWidget *widget,
1525 GdkEventScroll *event)
1527 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1531 index = gtk_combo_box_get_active (combo_box);
1535 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1537 if (event->direction == GDK_SCROLL_UP)
1542 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1553 cell_view_sync_cells (GtkComboBox *combo_box,
1554 GtkCellView *cell_view)
1558 for (k = combo_box->priv->cells; k; k = k->next)
1561 ComboCellInfo *info = (ComboCellInfo *)k->data;
1563 if (info->pack == GTK_PACK_START)
1564 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1565 info->cell, info->expand);
1566 else if (info->pack == GTK_PACK_END)
1567 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1568 info->cell, info->expand);
1570 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1572 info->func, info->func_data, NULL);
1574 for (j = info->attributes; j; j = j->next->next)
1576 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1579 GPOINTER_TO_INT (j->next->data));
1585 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1586 gboolean add_childs)
1590 /* Unset any existing model. */
1591 gtk_combo_box_unset_model (combo_box);
1593 if (combo_box->priv->cell_view)
1595 combo_box->priv->button = gtk_toggle_button_new ();
1596 g_signal_connect (combo_box->priv->button, "toggled",
1597 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1598 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1599 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1600 gtk_widget_set_parent (combo_box->priv->button,
1601 GTK_BIN (combo_box)->child->parent);
1603 combo_box->priv->separator = gtk_vseparator_new ();
1604 gtk_widget_set_parent (combo_box->priv->separator,
1605 combo_box->priv->button);
1606 gtk_widget_show (combo_box->priv->separator);
1608 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1609 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1610 gtk_widget_show (combo_box->priv->arrow);
1612 gtk_widget_show_all (combo_box->priv->button);
1614 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1616 /* I have no clue why, but we need to manually map in this case. */
1617 gtk_widget_map (combo_box->priv->separator);
1618 gtk_widget_map (combo_box->priv->arrow);
1623 combo_box->priv->button = gtk_toggle_button_new ();
1624 g_signal_connect (combo_box->priv->button, "toggled",
1625 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1626 g_signal_connect_after (combo_box, "key_press_event",
1627 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1628 gtk_widget_set_parent (combo_box->priv->button,
1629 GTK_BIN (combo_box)->child->parent);
1631 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1632 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1633 combo_box->priv->arrow);
1634 gtk_widget_show_all (combo_box->priv->button);
1637 g_signal_connect (combo_box->priv->button, "button_press_event",
1638 G_CALLBACK (gtk_combo_box_menu_button_press),
1641 /* create our funky menu */
1642 box = gtk_menu_new ();
1643 g_signal_connect (box, "key_press_event",
1644 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1645 gtk_combo_box_set_popup_widget (combo_box, box);
1647 /* set the models */
1648 gtk_combo_box_set_model_internal (combo_box);
1652 gtk_combo_box_menu_fill (combo_box);
1657 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1663 if (!combo_box->priv->model)
1666 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1667 menu = combo_box->priv->popup_widget;
1669 for (i = 0; i < items; i++)
1673 path = gtk_tree_path_new_from_indices (i, -1);
1674 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1676 g_signal_connect (tmp, "activate",
1677 G_CALLBACK (gtk_combo_box_menu_item_activate),
1680 cell_view_sync_cells (combo_box,
1681 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1683 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1684 gtk_widget_show (tmp);
1686 gtk_tree_path_free (path);
1691 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1693 /* disconnect signal handlers */
1694 gtk_combo_box_unset_model (combo_box);
1696 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1697 G_SIGNAL_MATCH_DATA,
1699 gtk_combo_box_menu_button_press, NULL);
1701 /* unparent will remove our latest ref */
1702 if (combo_box->priv->cell_view)
1704 gtk_widget_unparent (combo_box->priv->arrow);
1705 combo_box->priv->arrow = NULL;
1707 gtk_widget_unparent (combo_box->priv->separator);
1708 combo_box->priv->separator = NULL;
1710 gtk_widget_unparent (combo_box->priv->button);
1711 combo_box->priv->button = NULL;
1715 /* will destroy the arrow too */
1716 gtk_widget_unparent (combo_box->priv->button);
1718 combo_box->priv->button = NULL;
1719 combo_box->priv->arrow = NULL;
1722 /* changing the popup window will unref the menu and the childs */
1730 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1737 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1741 if (combo_box->priv->col_column == -1)
1744 gtk_tree_model_get (combo_box->priv->model, &iter,
1745 combo_box->priv->col_column, cols,
1751 if (combo_box->priv->row_column == -1)
1754 gtk_tree_model_get (combo_box->priv->model, &iter,
1755 combo_box->priv->row_column, rows,
1761 menu_occupied (GtkMenu *menu,
1765 guint bottom_attach)
1769 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1770 g_return_val_if_fail (left_attach < right_attach, TRUE);
1771 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1773 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1776 gboolean h_intersect = FALSE;
1777 gboolean v_intersect = FALSE;
1779 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1782 "bottom_attach", &b,
1786 /* look if this item intersects with the given coordinates */
1787 h_intersect = left_attach <= l && l <= right_attach;
1788 h_intersect &= left_attach <= r && r <= right_attach;
1790 v_intersect = top_attach <= t && t <= bottom_attach;
1791 v_intersect &= top_attach <= b && b <= bottom_attach;
1793 if (h_intersect && v_intersect)
1801 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1804 gint current_col = 0, current_row = 0;
1810 menu = combo_box->priv->popup_widget;
1811 if (!GTK_IS_MENU_SHELL (menu))
1814 list = gtk_container_get_children (GTK_CONTAINER (menu));
1815 item = g_list_nth_data (list, index);
1818 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1820 /* look for a good spot */
1823 if (current_col + cols > combo_box->priv->wrap_width)
1829 if (!menu_occupied (GTK_MENU (menu),
1830 current_col, current_col + cols,
1831 current_row, current_row + rows))
1837 /* set attach props */
1838 gtk_menu_attach (GTK_MENU (menu), item,
1839 current_col, current_col + cols,
1840 current_row, current_row + rows);
1844 gtk_combo_box_relayout (GtkComboBox *combo_box)
1850 /* ensure we are in menu style */
1851 if (combo_box->priv->tree_view)
1852 gtk_combo_box_list_destroy (combo_box);
1854 menu = combo_box->priv->popup_widget;
1856 if (!GTK_IS_MENU_SHELL (menu))
1858 gtk_combo_box_menu_setup (combo_box, FALSE);
1859 menu = combo_box->priv->popup_widget;
1862 /* get rid of all children */
1863 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1865 list = gtk_container_get_children (GTK_CONTAINER (menu));
1867 for (j = g_list_last (list); j; j = j->prev)
1868 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1873 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1875 for (i = 0; i < items; i++)
1880 path = gtk_tree_path_new_from_indices (i, -1);
1881 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1884 g_signal_connect (tmp, "activate",
1885 G_CALLBACK (gtk_combo_box_menu_item_activate),
1888 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1890 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1892 if (combo_box->priv->wrap_width)
1893 gtk_combo_box_relayout_item (combo_box, i);
1895 gtk_widget_show (tmp);
1897 gtk_tree_path_free (path);
1903 gtk_combo_box_menu_button_press (GtkWidget *widget,
1904 GdkEventButton *event,
1907 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1909 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1912 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1914 combo_box->priv->popup_in_progress = TRUE;
1916 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1917 combo_box->priv->active_item);
1919 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1921 gtk_combo_box_menu_position, combo_box,
1922 event->button, event->time);
1931 gtk_combo_box_menu_item_activate (GtkWidget *item,
1936 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1938 menu = combo_box->priv->popup_widget;
1939 g_return_if_fail (GTK_IS_MENU (menu));
1941 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1943 gtk_combo_box_set_active (combo_box, index);
1947 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1952 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1953 gint index = gtk_tree_path_get_indices (path)[0];
1954 gint items = gtk_tree_model_iter_n_children (model, NULL);
1956 if (combo_box->priv->active_item >= index)
1957 combo_box->priv->active_item++;
1959 if (!combo_box->priv->tree_view)
1960 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1963 gtk_combo_box_set_active (combo_box, 0);
1967 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1971 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1972 gint index = gtk_tree_path_get_indices (path)[0];
1974 if (!combo_box->priv->tree_view)
1975 gtk_combo_box_menu_row_deleted (model, path, user_data);
1977 if (index == combo_box->priv->active_item)
1979 gint items = gtk_tree_model_iter_n_children (model, NULL);
1982 gtk_combo_box_set_active_internal (combo_box, -1);
1983 else if (index == items)
1984 gtk_combo_box_set_active_internal (combo_box, index - 1);
1986 gtk_combo_box_set_active_internal (combo_box, index);
1988 else if (combo_box->priv->active_item > index)
1989 combo_box->priv->active_item--;
1993 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1999 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2000 gint items = gtk_tree_model_iter_n_children (model, NULL);
2003 for (i = 0; i < items; i++)
2004 if (new_order[i] == combo_box->priv->active_item)
2006 combo_box->priv->active_item = i;
2010 if (!combo_box->priv->tree_view)
2011 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2015 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2020 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2021 gint index = gtk_tree_path_get_indices (path)[0];
2023 if (index == combo_box->priv->active_item &&
2024 combo_box->priv->cell_view)
2025 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2027 if (combo_box->priv->tree_view)
2028 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2030 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2035 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2042 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2044 if (!combo_box->priv->popup_widget)
2047 menu = combo_box->priv->popup_widget;
2048 g_return_if_fail (GTK_IS_MENU (menu));
2050 item = gtk_cell_view_menu_item_new_from_model (model, path);
2051 g_signal_connect (item, "activate",
2052 G_CALLBACK (gtk_combo_box_menu_item_activate),
2055 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2057 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2058 gtk_tree_path_get_indices (path)[0]);
2059 gtk_widget_show (item);
2063 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2070 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2072 if (!combo_box->priv->popup_widget)
2075 index = gtk_tree_path_get_indices (path)[0];
2077 menu = combo_box->priv->popup_widget;
2078 g_return_if_fail (GTK_IS_MENU (menu));
2080 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2081 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2083 gtk_container_remove (GTK_CONTAINER (menu), item);
2087 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2093 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2095 gtk_combo_box_relayout (combo_box);
2099 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2104 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2107 if (!combo_box->priv->popup_widget)
2110 if (combo_box->priv->wrap_width)
2111 gtk_combo_box_relayout_item (combo_box,
2112 gtk_tree_path_get_indices (path)[0]);
2114 width = gtk_combo_box_calc_requested_width (combo_box, path);
2116 if (width > combo_box->priv->width)
2118 if (combo_box->priv->cell_view)
2120 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2121 gtk_widget_queue_resize (combo_box->priv->cell_view);
2123 combo_box->priv->width = width;
2132 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2135 GtkTreeSelection *sel;
2137 /* Unset any existing model. */
2138 gtk_combo_box_unset_model (combo_box);
2140 combo_box->priv->button = gtk_toggle_button_new ();
2141 gtk_widget_set_parent (combo_box->priv->button,
2142 GTK_BIN (combo_box)->child->parent);
2143 g_signal_connect (combo_box->priv->button, "button_press_event",
2144 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2145 g_signal_connect (combo_box->priv->button, "toggled",
2146 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2147 g_signal_connect_after (combo_box, "key_press_event",
2148 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2150 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2151 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2152 combo_box->priv->arrow);
2153 combo_box->priv->separator = NULL;
2154 gtk_widget_show_all (combo_box->priv->button);
2156 if (combo_box->priv->cell_view)
2158 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2159 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2160 GTK_BIN (combo_box)->child->parent);
2161 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2164 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2165 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2167 gtk_widget_show (combo_box->priv->cell_view_frame);
2170 combo_box->priv->tree_view = gtk_tree_view_new ();
2171 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2172 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2173 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2176 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2177 G_CALLBACK (gtk_combo_box_list_button_pressed),
2179 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2180 G_CALLBACK (gtk_combo_box_list_button_released),
2182 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2183 G_CALLBACK (gtk_combo_box_list_key_press),
2186 combo_box->priv->column = gtk_tree_view_column_new ();
2187 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2188 combo_box->priv->column);
2190 /* set the models */
2191 gtk_combo_box_set_model_internal (combo_box);
2194 for (i = combo_box->priv->cells; i; i = i->next)
2197 ComboCellInfo *info = (ComboCellInfo *)i->data;
2199 if (info->pack == GTK_PACK_START)
2200 gtk_tree_view_column_pack_start (combo_box->priv->column,
2201 info->cell, info->expand);
2202 else if (info->pack == GTK_PACK_END)
2203 gtk_tree_view_column_pack_end (combo_box->priv->column,
2204 info->cell, info->expand);
2206 for (j = info->attributes; j; j = j->next->next)
2208 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2211 GPOINTER_TO_INT (j->next->data));
2215 if (combo_box->priv->active_item != -1)
2219 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2222 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2224 gtk_tree_path_free (path);
2228 /* set sample/popup widgets */
2229 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2231 gtk_widget_show (combo_box->priv->tree_view);
2235 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2237 /* disconnect signals */
2238 gtk_combo_box_unset_model (combo_box);
2240 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2241 G_SIGNAL_MATCH_DATA,
2242 0, 0, NULL, NULL, combo_box);
2243 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2244 G_SIGNAL_MATCH_DATA,
2246 gtk_combo_box_list_button_pressed,
2249 /* destroy things (unparent will kill the latest ref from us)
2250 * last unref on button will destroy the arrow
2252 gtk_widget_unparent (combo_box->priv->button);
2253 combo_box->priv->button = NULL;
2254 combo_box->priv->arrow = NULL;
2256 if (combo_box->priv->cell_view)
2258 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2259 "background_set", FALSE,
2262 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2263 combo_box->priv->cell_view_frame = NULL;
2266 gtk_widget_destroy (combo_box->priv->tree_view);
2268 combo_box->priv->tree_view = NULL;
2269 combo_box->priv->popup_widget = NULL;
2274 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2276 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2277 gtk_grab_remove (combo_box->priv->tree_view);
2279 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2281 gtk_grab_remove (combo_box->priv->popup_window);
2282 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2283 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2288 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2289 GdkEventButton *event,
2292 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2294 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2296 if (ewidget == combo_box->priv->tree_view)
2299 if ((ewidget != combo_box->priv->button) ||
2300 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2303 gtk_combo_box_popup (combo_box);
2305 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2308 combo_box->priv->popup_in_progress = TRUE;
2314 gtk_combo_box_list_button_released (GtkWidget *widget,
2315 GdkEventButton *event,
2319 GtkTreePath *path = NULL;
2321 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2323 gboolean popup_in_progress = FALSE;
2325 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2327 if (combo_box->priv->popup_in_progress)
2329 popup_in_progress = TRUE;
2330 combo_box->priv->popup_in_progress = FALSE;
2333 if (ewidget != combo_box->priv->tree_view)
2335 if (ewidget == combo_box->priv->button &&
2336 !popup_in_progress &&
2337 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2339 gtk_combo_box_popdown (combo_box);
2343 /* released outside treeview */
2344 if (ewidget != combo_box->priv->button)
2346 gtk_combo_box_popdown (combo_box);
2354 /* select something cool */
2355 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2361 return TRUE; /* clicked outside window? */
2363 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2364 gtk_combo_box_popdown (combo_box);
2366 gtk_tree_path_free (path);
2372 gtk_combo_box_key_press (GtkWidget *widget,
2376 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2377 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2378 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2379 gint index = gtk_combo_box_get_active (combo_box);
2382 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2383 state == GDK_MOD1_MASK)
2385 gtk_combo_box_popup (combo_box);
2390 switch (event->keyval)
2394 new_index = index + 1;
2398 new_index = index - 1;
2401 case GDK_KP_Page_Up:
2407 case GDK_KP_Page_Down:
2410 new_index = items - 1;
2416 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2422 gtk_combo_box_menu_key_press (GtkWidget *widget,
2426 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2427 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2429 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2430 state == GDK_MOD1_MASK)
2432 gtk_combo_box_popdown (combo_box);
2441 gtk_combo_box_list_key_press (GtkWidget *widget,
2445 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2446 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2448 if (event->keyval == GDK_Escape ||
2449 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2450 state == GDK_MOD1_MASK))
2452 /* reset active item -- this is incredibly lame and ugly */
2453 gtk_combo_box_set_active (combo_box,
2454 gtk_combo_box_get_active (combo_box));
2456 gtk_combo_box_popdown (combo_box);
2461 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2462 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2466 GtkTreeModel *model = NULL;
2467 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2469 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2474 path = gtk_tree_model_get_path (model, &iter);
2477 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2478 gtk_tree_path_free (path);
2487 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2492 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2495 width = gtk_combo_box_calc_requested_width (combo_box, path);
2497 if (width > combo_box->priv->width)
2499 if (combo_box->priv->cell_view)
2501 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2502 gtk_widget_queue_resize (combo_box->priv->cell_view);
2504 combo_box->priv->width = width;
2509 * GtkCellLayout implementation
2512 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2513 GtkCellRenderer *cell,
2516 ComboCellInfo *info;
2517 GtkComboBox *combo_box;
2520 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2521 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2523 combo_box = GTK_COMBO_BOX (layout);
2525 g_object_ref (G_OBJECT (cell));
2526 gtk_object_sink (GTK_OBJECT (cell));
2528 info = g_new0 (ComboCellInfo, 1);
2530 info->expand = expand;
2531 info->pack = GTK_PACK_START;
2533 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2535 if (combo_box->priv->cell_view)
2536 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2539 if (combo_box->priv->column)
2540 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2542 menu = combo_box->priv->popup_widget;
2543 if (GTK_IS_MENU (menu))
2547 list = gtk_container_get_children (GTK_CONTAINER (menu));
2548 for (i = list; i; i = i->next)
2552 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2553 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2555 view = GTK_CELL_VIEW (i->data);
2557 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2564 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2565 GtkCellRenderer *cell,
2568 ComboCellInfo *info;
2569 GtkComboBox *combo_box;
2572 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2573 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2575 combo_box = GTK_COMBO_BOX (layout);
2577 g_object_ref (G_OBJECT (cell));
2578 gtk_object_sink (GTK_OBJECT (cell));
2580 info = g_new0 (ComboCellInfo, 1);
2582 info->expand = expand;
2583 info->pack = GTK_PACK_END;
2585 if (combo_box->priv->cell_view)
2586 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2589 if (combo_box->priv->column)
2590 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2592 menu = combo_box->priv->popup_widget;
2593 if (GTK_IS_MENU (menu))
2597 list = gtk_container_get_children (GTK_CONTAINER (menu));
2598 for (i = list; i; i = i->next)
2602 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2603 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2605 view = GTK_CELL_VIEW (i->data);
2607 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2614 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2617 GtkComboBox *combo_box;
2620 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2622 combo_box = GTK_COMBO_BOX (layout);
2624 if (combo_box->priv->cell_view)
2625 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2627 if (combo_box->priv->column)
2628 gtk_tree_view_column_clear (combo_box->priv->column);
2630 for (i = combo_box->priv->cells; i; i = i->next)
2632 ComboCellInfo *info = (ComboCellInfo *)i->data;
2634 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2635 g_object_unref (G_OBJECT (info->cell));
2639 g_slist_free (combo_box->priv->cells);
2640 combo_box->priv->cells = NULL;
2642 menu = combo_box->priv->popup_widget;
2643 if (GTK_IS_MENU (menu))
2647 list = gtk_container_get_children (GTK_CONTAINER (menu));
2648 for (i = list; i; i = i->next)
2652 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2653 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2655 view = GTK_CELL_VIEW (i->data);
2657 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2664 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2665 GtkCellRenderer *cell,
2666 const gchar *attribute,
2669 ComboCellInfo *info;
2670 GtkComboBox *combo_box;
2673 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2674 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2676 combo_box = GTK_COMBO_BOX (layout);
2678 info = gtk_combo_box_get_cell_info (combo_box, cell);
2680 info->attributes = g_slist_prepend (info->attributes,
2681 GINT_TO_POINTER (column));
2682 info->attributes = g_slist_prepend (info->attributes,
2683 g_strdup (attribute));
2685 if (combo_box->priv->cell_view)
2686 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2687 cell, attribute, column);
2689 if (combo_box->priv->column)
2690 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2691 cell, attribute, column);
2693 menu = combo_box->priv->popup_widget;
2694 if (GTK_IS_MENU (menu))
2698 list = gtk_container_get_children (GTK_CONTAINER (menu));
2699 for (i = list; i; i = i->next)
2703 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2704 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2706 view = GTK_CELL_VIEW (i->data);
2708 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2714 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2718 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2719 GtkCellRenderer *cell,
2720 GtkCellLayoutDataFunc func,
2722 GDestroyNotify destroy)
2724 ComboCellInfo *info;
2725 GtkComboBox *combo_box;
2728 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2730 combo_box = GTK_COMBO_BOX (layout);
2732 info = gtk_combo_box_get_cell_info (combo_box, cell);
2733 g_return_if_fail (info != NULL);
2737 GDestroyNotify d = info->destroy;
2739 info->destroy = NULL;
2740 d (info->func_data);
2744 info->func_data = func_data;
2745 info->destroy = destroy;
2747 if (combo_box->priv->cell_view)
2748 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2750 if (combo_box->priv->column)
2751 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2753 menu = combo_box->priv->popup_widget;
2754 if (GTK_IS_MENU (menu))
2758 list = gtk_container_get_children (GTK_CONTAINER (menu));
2759 for (i = list; i; i = i->next)
2763 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2764 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2766 view = GTK_CELL_VIEW (i->data);
2768 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2769 func, func_data, NULL);
2774 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2778 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2779 GtkCellRenderer *cell)
2781 ComboCellInfo *info;
2782 GtkComboBox *combo_box;
2786 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2787 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2789 combo_box = GTK_COMBO_BOX (layout);
2791 info = gtk_combo_box_get_cell_info (combo_box, cell);
2792 g_return_if_fail (info != NULL);
2794 list = info->attributes;
2795 while (list && list->next)
2797 g_free (list->data);
2798 list = list->next->next;
2800 g_slist_free (info->attributes);
2801 info->attributes = NULL;
2803 if (combo_box->priv->cell_view)
2804 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2806 if (combo_box->priv->column)
2807 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2809 menu = combo_box->priv->popup_widget;
2810 if (GTK_IS_MENU (menu))
2814 list = gtk_container_get_children (GTK_CONTAINER (menu));
2815 for (i = list; i; i = i->next)
2819 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2820 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2822 view = GTK_CELL_VIEW (i->data);
2824 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2829 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2833 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2834 GtkCellRenderer *cell,
2837 ComboCellInfo *info;
2838 GtkComboBox *combo_box;
2842 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2843 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2845 combo_box = GTK_COMBO_BOX (layout);
2847 info = gtk_combo_box_get_cell_info (combo_box, cell);
2849 g_return_if_fail (info != NULL);
2850 g_return_if_fail (position >= 0);
2852 link = g_slist_find (combo_box->priv->cells, info);
2854 g_return_if_fail (link != NULL);
2856 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2857 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2860 if (combo_box->priv->cell_view)
2861 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2864 if (combo_box->priv->column)
2865 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2868 menu = combo_box->priv->popup_widget;
2869 if (GTK_IS_MENU (menu))
2873 list = gtk_container_get_children (GTK_CONTAINER (menu));
2874 for (i = list; i; i = i->next)
2878 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2879 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2881 view = GTK_CELL_VIEW (i->data);
2883 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2888 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2896 * gtk_combo_box_new:
2898 * Creates a new empty #GtkComboBox.
2900 * Return value: A new #GtkComboBox.
2905 gtk_combo_box_new (void)
2907 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2911 * gtk_combo_box_new_with_model:
2912 * @model: A #GtkTreeModel.
2914 * Creates a new #GtkComboBox with the model initialized to @model.
2916 * Return value: A new #GtkComboBox.
2921 gtk_combo_box_new_with_model (GtkTreeModel *model)
2923 GtkComboBox *combo_box;
2925 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2927 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2931 return GTK_WIDGET (combo_box);
2935 * gtk_combo_box_set_wrap_width:
2936 * @combo_box: A #GtkComboBox.
2937 * @width: Preferred number of columns.
2939 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2940 * the preferred number of columns when you want to the popup to be layed out
2946 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2949 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2950 g_return_if_fail (width >= 0);
2952 if (width != combo_box->priv->wrap_width)
2954 combo_box->priv->wrap_width = width;
2956 gtk_combo_box_relayout (combo_box);
2957 gtk_combo_box_style_set (GTK_WIDGET (combo_box), NULL);
2959 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2964 * gtk_combo_box_set_row_span_column:
2965 * @combo_box: A #GtkComboBox.
2966 * @row_span: A column in the model passed during construction.
2968 * Sets the column with row span information for @combo_box to be @row_span.
2969 * The row span column contains integers which indicate how many rows
2970 * an item should span.
2975 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2980 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2982 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2983 g_return_if_fail (row_span >= 0 && row_span < col);
2985 if (row_span != combo_box->priv->row_column)
2987 combo_box->priv->row_column = row_span;
2989 gtk_combo_box_relayout (combo_box);
2991 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2996 * gtk_combo_box_set_column_span_column:
2997 * @combo_box: A #GtkComboBox.
2998 * @column_span: A column in the model passed during construction.
3000 * Sets the column with column span information for @combo_box to be
3001 * @column_span. The column span column contains integers which indicate
3002 * how many columns an item should span.
3007 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
3012 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3014 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3015 g_return_if_fail (column_span >= 0 && column_span < col);
3017 if (column_span != combo_box->priv->col_column)
3019 combo_box->priv->col_column = column_span;
3021 gtk_combo_box_relayout (combo_box);
3023 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3028 * gtk_combo_box_get_active:
3029 * @combo_box: A #GtkComboBox.
3031 * Returns the index of the currently active item, or -1 if there's no
3034 * Return value: An integer which is the index of the currently active item, or
3035 * -1 if there's no active item.
3040 gtk_combo_box_get_active (GtkComboBox *combo_box)
3042 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3044 return combo_box->priv->active_item;
3048 * gtk_combo_box_set_active:
3049 * @combo_box: A #GtkComboBox.
3050 * @index: An index in the model passed during construction, or -1 to have
3053 * Sets the active item of @combo_box to be the item at @index.
3058 gtk_combo_box_set_active (GtkComboBox *combo_box,
3061 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3062 /* -1 means "no item selected" */
3063 g_return_if_fail (index >= -1);
3065 if (combo_box->priv->active_item == index)
3068 gtk_combo_box_set_active_internal (combo_box, index);
3072 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3077 combo_box->priv->active_item = index;
3081 if (combo_box->priv->tree_view)
3082 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3085 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3087 if (GTK_IS_MENU (menu))
3088 gtk_menu_set_active (menu, -1);
3091 if (combo_box->priv->cell_view)
3092 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3096 path = gtk_tree_path_new_from_indices (index, -1);
3098 if (combo_box->priv->tree_view)
3099 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3102 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3104 if (GTK_IS_MENU (menu))
3105 gtk_menu_set_active (GTK_MENU (menu), index);
3108 if (combo_box->priv->cell_view)
3109 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3111 gtk_tree_path_free (path);
3114 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3119 * gtk_combo_box_get_active_iter:
3120 * @combo_box: A #GtkComboBox
3121 * @iter: The uninitialized #GtkTreeIter.
3123 * Sets @iter to point to the current active item, if it exists.
3125 * Return value: %TRUE, if @iter was set
3130 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3137 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3139 active = gtk_combo_box_get_active (combo_box);
3143 path = gtk_tree_path_new_from_indices (active, -1);
3144 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3146 gtk_tree_path_free (path);
3152 * gtk_combo_box_set_active_iter:
3153 * @combo_box: A #GtkComboBox
3154 * @iter: The #GtkTreeIter.
3156 * Sets the current active item to be the one referenced by @iter.
3157 * @iter must correspond to a path of depth one.
3162 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3167 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3169 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3170 g_return_if_fail (path != NULL);
3171 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3173 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3174 gtk_tree_path_free (path);
3178 * gtk_combo_box_set_model:
3179 * @combo_box: A #GtkComboBox.
3180 * @model: A #GtkTreeModel.
3182 * Sets the model used by @combo_box to be @model. Will unset a
3183 * previously set model (if applicable).
3188 gtk_combo_box_set_model (GtkComboBox *combo_box,
3189 GtkTreeModel *model)
3191 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3192 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3194 if (combo_box->priv->model)
3196 gtk_combo_box_unset_model (combo_box);
3197 g_object_unref (G_OBJECT (combo_box->priv->model));
3200 combo_box->priv->model = model;
3201 g_object_ref (G_OBJECT (combo_box->priv->model));
3203 gtk_combo_box_set_model_internal (combo_box);
3204 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
3205 gtk_combo_box_menu_fill (combo_box);
3207 if (combo_box->priv->cell_view)
3208 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3209 combo_box->priv->model);
3213 * gtk_combo_box_get_model
3214 * @combo_box: A #GtkComboBox.
3216 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3218 * Return value: A #GtkTreeModel which was passed during construction.
3223 gtk_combo_box_get_model (GtkComboBox *combo_box)
3225 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3227 return combo_box->priv->model;
3231 /* convenience API for simple text combos */
3234 * gtk_combo_box_new_text:
3236 * Convenience function which constructs a new text combo box, which is a
3237 * #GtkComboBox just displaying strings. If you use this function to create
3238 * a text combo box, you should only manipulate its data source with the
3239 * following convenience functions: gtk_combo_box_append_text(),
3240 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3241 * gtk_combo_box_remove_text().
3243 * Return value: A new text combo box.
3248 gtk_combo_box_new_text (void)
3250 GtkWidget *combo_box;
3251 GtkCellRenderer *cell;
3252 GtkListStore *store;
3254 store = gtk_list_store_new (1, G_TYPE_STRING);
3256 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3258 cell = gtk_cell_renderer_text_new ();
3259 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3260 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3268 * gtk_combo_box_append_text:
3269 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3272 * Appends @string to the list of strings stored in @combo_box. Note that
3273 * you can only use this function with combo boxes constructed with
3274 * gtk_combo_box_new_text().
3279 gtk_combo_box_append_text (GtkComboBox *combo_box,
3283 GtkListStore *store;
3285 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3286 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3287 g_return_if_fail (text != NULL);
3289 store = GTK_LIST_STORE (combo_box->priv->model);
3291 gtk_list_store_append (store, &iter);
3292 gtk_list_store_set (store, &iter, 0, text, -1);
3296 * gtk_combo_box_insert_text:
3297 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3298 * @position: An index to insert @text.
3301 * Inserts @string at @position in the list of strings stored in @combo_box.
3302 * Note that you can only use this function with combo boxes constructed
3303 * with gtk_combo_box_new_text().
3308 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3313 GtkListStore *store;
3315 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3316 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3317 g_return_if_fail (position >= 0);
3318 g_return_if_fail (text != NULL);
3320 store = GTK_LIST_STORE (combo_box->priv->model);
3322 gtk_list_store_insert (store, &iter, position);
3323 gtk_list_store_set (store, &iter, 0, text, -1);
3327 * gtk_combo_box_prepend_text:
3328 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3331 * Prepends @string to the list of strings stored in @combo_box. Note that
3332 * you can only use this function with combo boxes constructed with
3333 * gtk_combo_box_new_text().
3338 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3342 GtkListStore *store;
3344 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3345 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3346 g_return_if_fail (text != NULL);
3348 store = GTK_LIST_STORE (combo_box->priv->model);
3350 gtk_list_store_prepend (store, &iter);
3351 gtk_list_store_set (store, &iter, 0, text, -1);
3355 * gtk_combo_box_remove_text:
3356 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3357 * @position: Index of the item to remove.
3359 * Removes the string at @position from @combo_box. Note that you can only use
3360 * this function with combo boxes constructed with gtk_combo_box_new_text().
3365 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3369 GtkListStore *store;
3371 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3372 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3373 g_return_if_fail (position >= 0);
3375 store = GTK_LIST_STORE (combo_box->priv->model);
3377 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3379 gtk_list_store_remove (store, &iter);
3384 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3385 gboolean group_cycling)
3387 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3389 gtk_widget_grab_focus (combo_box->priv->button);
3395 gtk_combo_box_destroy (GtkObject *object)
3397 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3399 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3401 combo_box->priv->cell_view = NULL;
3405 gtk_combo_box_finalize (GObject *object)
3407 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3410 gtk_combo_box_unset_model (combo_box);
3412 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3413 gtk_combo_box_menu_destroy (combo_box);
3415 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3416 gtk_combo_box_list_destroy (combo_box);
3418 if (combo_box->priv->popup_window)
3419 gtk_widget_destroy (combo_box->priv->popup_window);
3421 if (combo_box->priv->model)
3422 g_object_unref (combo_box->priv->model);
3424 for (i = combo_box->priv->cells; i; i = i->next)
3426 ComboCellInfo *info = (ComboCellInfo *)i->data;
3427 GSList *list = info->attributes;
3430 info->destroy (info->func_data);
3432 while (list && list->next)
3434 g_free (list->data);
3435 list = list->next->next;
3437 g_slist_free (info->attributes);
3439 g_object_unref (G_OBJECT (info->cell));
3442 g_slist_free (combo_box->priv->cells);
3444 G_OBJECT_CLASS (parent_class)->finalize (object);