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"
32 #include "gtkliststore.h"
35 #include "gtktearoffmenuitem.h"
36 #include "gtktogglebutton.h"
37 #include "gtktreeselection.h"
38 #include "gtkvseparator.h"
39 #include "gtkwindow.h"
41 #include <gdk/gdkkeysyms.h>
43 #include <gobject/gvaluecollector.h>
48 #include "gtkmarshalers.h"
52 /* WELCOME, to THE house of evil code */
54 typedef struct _ComboCellInfo ComboCellInfo;
57 GtkCellRenderer *cell;
60 GtkCellLayoutDataFunc func;
62 GDestroyNotify destroy;
68 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
70 struct _GtkComboBoxPrivate
82 GtkTreeViewColumn *column;
85 GtkWidget *cell_view_frame;
92 GtkWidget *popup_widget;
93 GtkWidget *popup_window;
94 GtkWidget *popup_frame;
104 guint popup_in_progress : 1;
105 guint destroying : 1;
106 guint add_tearoffs : 1;
109 /* While debugging this evil code, I have learned that
110 * there are actually 4 modes to this widget, which can
111 * be characterized as follows
113 * 1) menu mode, no child added
116 * cell_view -> GtkCellView, regular child
117 * cell_view_frame -> NULL
118 * button -> GtkToggleButton set_parent to combo
119 * arrow -> GtkArrow set_parent to button
120 * separator -> GtkVSepator set_parent to button
121 * popup_widget -> GtkMenu
122 * popup_window -> NULL
123 * popup_frame -> NULL
125 * 2) menu mode, child added
129 * cell_view_frame -> NULL
130 * button -> GtkToggleButton set_parent to combo
131 * arrow -> GtkArrow, child of button
133 * popup_widget -> GtkMenu
134 * popup_window -> NULL
135 * popup_frame -> NULL
137 * 3) list mode, no child added
139 * tree_view -> GtkTreeView, child of popup_frame
140 * cell_view -> GtkCellView, regular child
141 * cell_view_frame -> GtkFrame, set parent to combo
142 * button -> GtkToggleButton, set_parent to combo
143 * arrow -> GtkArrow, child of button
145 * popup_widget -> tree_view
146 * popup_window -> GtkWindow
147 * popup_frame -> GtkFrame, child of popup_window
149 * 4) list mode, child added
151 * tree_view -> GtkTreeView, child of popup_frame
153 * cell_view_frame -> NULL
154 * button -> GtkToggleButton, set_parent to combo
155 * arrow -> GtkArrow, child of button
157 * popup_widget -> tree_view
158 * popup_window -> GtkWindow
159 * popup_frame -> GtkFrame, child of popup_window
172 PROP_ROW_SPAN_COLUMN,
173 PROP_COLUMN_SPAN_COLUMN,
178 static GtkBinClass *parent_class = NULL;
179 static guint combo_box_signals[LAST_SIGNAL] = {0,};
181 #define BONUS_PADDING 4
185 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
186 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
187 static void gtk_combo_box_init (GtkComboBox *combo_box);
188 static void gtk_combo_box_finalize (GObject *object);
189 static void gtk_combo_box_destroy (GtkObject *object);
191 static void gtk_combo_box_set_property (GObject *object,
195 static void gtk_combo_box_get_property (GObject *object,
200 static void gtk_combo_box_state_changed (GtkWidget *widget,
201 GtkStateType previous);
202 static void gtk_combo_box_style_set (GtkWidget *widget,
204 static void gtk_combo_box_button_toggled (GtkWidget *widget,
206 static void gtk_combo_box_add (GtkContainer *container,
208 static void gtk_combo_box_remove (GtkContainer *container,
211 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
212 GtkCellRenderer *cell);
214 static void gtk_combo_box_menu_show (GtkWidget *menu,
216 static void gtk_combo_box_menu_hide (GtkWidget *menu,
219 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
221 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
226 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
231 static void gtk_combo_box_menu_position (GtkMenu *menu,
237 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
239 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
241 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
243 static void gtk_combo_box_size_request (GtkWidget *widget,
244 GtkRequisition *requisition);
245 static void gtk_combo_box_size_allocate (GtkWidget *widget,
246 GtkAllocation *allocation);
247 static void gtk_combo_box_forall (GtkContainer *container,
248 gboolean include_internals,
249 GtkCallback callback,
250 gpointer callback_data);
251 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
252 GdkEventExpose *event);
253 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
254 GdkEventScroll *event);
255 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
257 static gboolean gtk_combo_box_key_press (GtkWidget *widget,
261 /* listening to the model */
262 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
266 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
269 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
274 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
280 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
285 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
286 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
288 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
290 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
291 GdkEventButton *event,
293 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
296 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
297 GdkEventButton *event,
300 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
306 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
307 gboolean add_children);
308 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
309 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
311 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
315 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
317 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
319 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
320 GdkEventButton *event,
322 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
324 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
328 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
331 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
336 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
340 static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
343 static void gtk_combo_box_menu_state_changed (GtkWidget *widget,
344 GtkStateType previous,
348 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
349 GtkCellRenderer *cell,
351 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
352 GtkCellRenderer *cell,
354 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
355 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
356 GtkCellRenderer *cell,
357 const gchar *attribute,
359 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
360 GtkCellRenderer *cell,
361 GtkCellLayoutDataFunc func,
363 GDestroyNotify destroy);
364 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
365 GtkCellRenderer *cell);
366 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
367 GtkCellRenderer *cell,
369 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
370 gboolean group_cycling);
372 static void cell_view_sync_cells (GtkComboBox *combo_box,
373 GtkCellView *cell_view);
376 gtk_combo_box_get_type (void)
378 static GType combo_box_type = 0;
382 static const GTypeInfo combo_box_info =
384 sizeof (GtkComboBoxClass),
385 NULL, /* base_init */
386 NULL, /* base_finalize */
387 (GClassInitFunc) gtk_combo_box_class_init,
388 NULL, /* class_finalize */
389 NULL, /* class_data */
390 sizeof (GtkComboBox),
392 (GInstanceInitFunc) gtk_combo_box_init
395 static const GInterfaceInfo cell_layout_info =
397 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
402 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
407 g_type_add_interface_static (combo_box_type,
408 GTK_TYPE_CELL_LAYOUT,
412 return combo_box_type;
417 gtk_combo_box_class_init (GtkComboBoxClass *klass)
419 GObjectClass *object_class;
420 GtkBindingSet *binding_set;
421 GtkObjectClass *gtk_object_class;
422 GtkContainerClass *container_class;
423 GtkWidgetClass *widget_class;
425 binding_set = gtk_binding_set_by_class (klass);
427 container_class = (GtkContainerClass *)klass;
428 container_class->forall = gtk_combo_box_forall;
429 container_class->add = gtk_combo_box_add;
430 container_class->remove = gtk_combo_box_remove;
432 widget_class = (GtkWidgetClass *)klass;
433 widget_class->size_allocate = gtk_combo_box_size_allocate;
434 widget_class->size_request = gtk_combo_box_size_request;
435 widget_class->expose_event = gtk_combo_box_expose_event;
436 widget_class->scroll_event = gtk_combo_box_scroll_event;
437 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
438 widget_class->style_set = gtk_combo_box_style_set;
439 widget_class->state_changed = gtk_combo_box_state_changed;
441 gtk_object_class = (GtkObjectClass *)klass;
442 gtk_object_class->destroy = gtk_combo_box_destroy;
444 object_class = (GObjectClass *)klass;
445 object_class->finalize = gtk_combo_box_finalize;
446 object_class->set_property = gtk_combo_box_set_property;
447 object_class->get_property = gtk_combo_box_get_property;
449 parent_class = g_type_class_peek_parent (klass);
452 combo_box_signals[CHANGED] =
453 g_signal_new ("changed",
454 G_OBJECT_CLASS_TYPE (klass),
456 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
458 g_cclosure_marshal_VOID__VOID,
462 g_object_class_install_property (object_class,
464 g_param_spec_object ("model",
465 P_("ComboBox model"),
466 P_("The model for the combo box"),
470 g_object_class_install_property (object_class,
472 g_param_spec_int ("wrap_width",
474 P_("Wrap width for layouting the items in a grid"),
480 g_object_class_install_property (object_class,
481 PROP_ROW_SPAN_COLUMN,
482 g_param_spec_int ("row_span_column",
483 P_("Row span column"),
484 P_("TreeModel column containing the row span values"),
490 g_object_class_install_property (object_class,
491 PROP_COLUMN_SPAN_COLUMN,
492 g_param_spec_int ("column_span_column",
493 P_("Column span column"),
494 P_("TreeModel column containing the column span values"),
500 g_object_class_install_property (object_class,
502 g_param_spec_int ("active",
504 P_("The item which is currently active"),
511 * GtkComboBox:add-tearoffs:
513 * The "add-tearoffs" property controls whether generated menus
514 * have tearoff menu items.
516 * Note that this only affects menu style combo boxes.
520 g_object_class_install_property (object_class,
522 g_param_spec_boolean ("add-tearoffs",
523 P_("Add tearoffs to menus"),
524 P_("Whether combobox dropdowns should have a tearoff menu item"),
528 gtk_widget_class_install_style_property (widget_class,
529 g_param_spec_boolean ("appears-as-list",
530 P_("Appears as list"),
531 P_("Whether combobox dropdowns should look like lists rather than menus"),
535 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
539 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
541 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
542 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
543 iface->clear = gtk_combo_box_cell_layout_clear;
544 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
545 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
546 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
547 iface->reorder = gtk_combo_box_cell_layout_reorder;
551 gtk_combo_box_init (GtkComboBox *combo_box)
553 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
555 combo_box->priv->cell_view = gtk_cell_view_new ();
556 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
557 GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
558 gtk_widget_show (combo_box->priv->cell_view);
560 combo_box->priv->width = 0;
561 combo_box->priv->wrap_width = 0;
563 combo_box->priv->active_item = -1;
564 combo_box->priv->col_column = -1;
565 combo_box->priv->row_column = -1;
569 gtk_combo_box_set_property (GObject *object,
574 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
579 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
582 case PROP_WRAP_WIDTH:
583 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
586 case PROP_ROW_SPAN_COLUMN:
587 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
590 case PROP_COLUMN_SPAN_COLUMN:
591 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
595 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
598 case PROP_ADD_TEAROFFS:
599 gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
608 gtk_combo_box_get_property (GObject *object,
613 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
618 g_value_set_object (value, combo_box->priv->model);
621 case PROP_WRAP_WIDTH:
622 g_value_set_int (value, combo_box->priv->wrap_width);
625 case PROP_ROW_SPAN_COLUMN:
626 g_value_set_int (value, combo_box->priv->row_column);
629 case PROP_COLUMN_SPAN_COLUMN:
630 g_value_set_int (value, combo_box->priv->col_column);
634 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
637 case PROP_ADD_TEAROFFS:
638 g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
642 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
648 gtk_combo_box_state_changed (GtkWidget *widget,
649 GtkStateType previous)
651 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
653 if (GTK_WIDGET_REALIZED (widget))
655 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
656 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
657 &widget->style->base[GTK_WIDGET_STATE (widget)]);
660 gtk_widget_queue_draw (widget);
664 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
666 gboolean appears_as_list;
668 /* if wrap_width > 0, then we are in grid-mode and forced to use
671 if (combo_box->priv->wrap_width)
672 appears_as_list = FALSE;
674 gtk_widget_style_get (GTK_WIDGET (combo_box),
675 "appears-as-list", &appears_as_list,
680 /* Destroy all the menu mode widgets, if they exist. */
681 if (GTK_IS_MENU (combo_box->priv->popup_widget))
682 gtk_combo_box_menu_destroy (combo_box);
684 /* Create the list mode widgets, if they don't already exist. */
685 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
686 gtk_combo_box_list_setup (combo_box);
690 /* Destroy all the list mode widgets, if they exist. */
691 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
692 gtk_combo_box_list_destroy (combo_box);
694 /* Create the menu mode widgets, if they don't already exist. */
695 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
696 gtk_combo_box_menu_setup (combo_box, TRUE);
701 gtk_combo_box_style_set (GtkWidget *widget,
704 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
706 gtk_combo_box_check_appearance (combo_box);
708 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
709 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
710 &widget->style->base[GTK_WIDGET_STATE (widget)]);
714 gtk_combo_box_button_toggled (GtkWidget *widget,
717 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
719 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
721 if (!combo_box->priv->popup_in_progress)
722 gtk_combo_box_popup (combo_box);
725 gtk_combo_box_popdown (combo_box);
729 gtk_combo_box_add (GtkContainer *container,
732 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
734 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
736 gtk_widget_unparent (combo_box->priv->cell_view);
737 GTK_BIN (container)->child = NULL;
738 gtk_widget_queue_resize (GTK_WIDGET (container));
741 gtk_widget_set_parent (widget, GTK_WIDGET (container));
742 GTK_BIN (container)->child = widget;
744 if (combo_box->priv->cell_view &&
745 widget != combo_box->priv->cell_view)
747 /* since the cell_view was unparented, it's gone now */
748 combo_box->priv->cell_view = NULL;
750 if (!combo_box->priv->tree_view && combo_box->priv->separator)
752 gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
753 combo_box->priv->separator);
754 combo_box->priv->separator = NULL;
756 gtk_widget_queue_resize (GTK_WIDGET (container));
758 else if (combo_box->priv->cell_view_frame)
760 gtk_widget_unparent (combo_box->priv->cell_view_frame);
761 combo_box->priv->cell_view_frame = NULL;
767 gtk_combo_box_remove (GtkContainer *container,
770 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
771 gboolean appears_as_list;
773 gtk_widget_unparent (widget);
774 GTK_BIN (container)->child = NULL;
776 if (combo_box->priv->destroying)
779 gtk_widget_queue_resize (GTK_WIDGET (container));
781 if (!combo_box->priv->tree_view)
782 appears_as_list = FALSE;
784 appears_as_list = TRUE;
787 gtk_combo_box_list_destroy (combo_box);
788 else if (GTK_IS_MENU (combo_box->priv->popup_widget))
790 gtk_combo_box_menu_destroy (combo_box);
791 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
792 combo_box->priv->popup_widget = NULL;
795 if (!combo_box->priv->cell_view)
797 combo_box->priv->cell_view = gtk_cell_view_new ();
798 gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
799 GTK_BIN (container)->child = combo_box->priv->cell_view;
801 gtk_widget_show (combo_box->priv->cell_view);
802 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
803 combo_box->priv->model);
804 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
809 gtk_combo_box_list_setup (combo_box);
811 gtk_combo_box_menu_setup (combo_box, TRUE);
813 gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
816 static ComboCellInfo *
817 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
818 GtkCellRenderer *cell)
822 for (i = combo_box->priv->cells; i; i = i->next)
824 ComboCellInfo *info = (ComboCellInfo *)i->data;
826 if (info && info->cell == cell)
834 gtk_combo_box_menu_show (GtkWidget *menu,
837 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
839 combo_box->priv->popup_in_progress = TRUE;
840 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
842 combo_box->priv->popup_in_progress = FALSE;
846 gtk_combo_box_menu_hide (GtkWidget *menu,
849 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
851 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
856 gtk_combo_box_detacher (GtkWidget *widget,
859 GtkComboBox *combo_box;
861 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
863 combo_box = GTK_COMBO_BOX (widget);
864 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
866 g_signal_handlers_disconnect_by_func (menu->toplevel,
867 gtk_combo_box_menu_show,
869 g_signal_handlers_disconnect_by_func (menu->toplevel,
870 gtk_combo_box_menu_hide,
873 combo_box->priv->popup_widget = NULL;
877 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
880 if (GTK_IS_MENU (combo_box->priv->popup_widget))
882 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
883 combo_box->priv->popup_widget = NULL;
885 else if (combo_box->priv->popup_widget)
887 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
888 combo_box->priv->popup_widget);
889 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
890 combo_box->priv->popup_widget = NULL;
893 if (GTK_IS_MENU (popup))
895 if (combo_box->priv->popup_window)
897 gtk_widget_destroy (combo_box->priv->popup_window);
898 combo_box->priv->popup_window = NULL;
899 combo_box->priv->popup_frame = NULL;
902 combo_box->priv->popup_widget = popup;
905 * Note that we connect to show/hide on the toplevel, not the
906 * menu itself, since the menu is not shown/hidden when it is
907 * popped up while torn-off.
909 g_signal_connect (GTK_MENU (popup)->toplevel, "show",
910 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
911 g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
912 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
914 gtk_menu_attach_to_widget (GTK_MENU (popup),
915 GTK_WIDGET (combo_box),
916 gtk_combo_box_detacher);
920 if (!combo_box->priv->popup_window)
922 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
923 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
924 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
925 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
927 combo_box->priv->popup_frame = gtk_frame_new (NULL);
928 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
929 GTK_SHADOW_ETCHED_IN);
930 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
931 combo_box->priv->popup_frame);
933 gtk_widget_show (combo_box->priv->popup_frame);
936 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
938 gtk_widget_show (popup);
939 g_object_ref (G_OBJECT (popup));
940 combo_box->priv->popup_widget = popup;
945 gtk_combo_box_menu_position_below (GtkMenu *menu,
954 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
956 /* FIXME: is using the size request here broken? */
957 child = GTK_BIN (combo_box)->child;
959 gdk_window_get_origin (child->window, &sx, &sy);
961 gtk_widget_size_request (GTK_WIDGET (menu), &req);
963 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
966 *x = sx + child->allocation.width - req.width;
967 *y = sy + child->allocation.height;
969 if (GTK_WIDGET_NO_WINDOW (child))
971 *x += child->allocation.x;
972 *y += child->allocation.y;
979 gtk_combo_box_menu_position_over (GtkMenu *menu,
985 GtkComboBox *combo_box;
989 GtkRequisition requisition;
996 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
998 combo_box = GTK_COMBO_BOX (user_data);
999 widget = GTK_WIDGET (combo_box);
1001 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1002 menu_width = requisition.width;
1004 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1005 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1007 menu_xpos += widget->allocation.x;
1008 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1012 gtk_widget_get_child_requisition (active, &requisition);
1013 menu_ypos -= requisition.height / 2;
1016 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1019 child = children->data;
1021 if (active == child)
1024 if (GTK_WIDGET_VISIBLE (child))
1026 gtk_widget_get_child_requisition (child, &requisition);
1027 menu_ypos -= requisition.height;
1030 children = children->next;
1033 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1034 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1036 /* Clamp the position on screen */
1037 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1041 else if ((menu_xpos + menu_width) > screen_width)
1042 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1051 gtk_combo_box_menu_position (GtkMenu *menu,
1057 GtkComboBox *combo_box;
1058 GtkWidget *menu_item;
1060 combo_box = GTK_COMBO_BOX (user_data);
1062 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
1063 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1066 menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1068 gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
1071 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1077 gtk_combo_box_list_position (GtkComboBox *combo_box,
1086 GdkRectangle monitor;
1087 GtkRequisition popup_req;
1089 sample = GTK_BIN (combo_box)->child;
1091 *width = sample->allocation.width;
1092 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
1093 *height = popup_req.height;
1095 gdk_window_get_origin (sample->window, x, y);
1097 if (combo_box->priv->cell_view_frame)
1099 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1100 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1101 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1102 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1105 if (GTK_WIDGET_NO_WINDOW (sample))
1107 *x += sample->allocation.x;
1108 *y += sample->allocation.y;
1111 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1112 monitor_num = gdk_screen_get_monitor_at_window (screen,
1113 GTK_WIDGET (combo_box)->window);
1114 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1118 else if (*x + *width > monitor.x + monitor.width)
1119 *x = monitor.x + monitor.width - *width;
1121 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1122 *y += sample->allocation.height;
1128 * gtk_combo_box_popup:
1129 * @combo_box: a #GtkComboBox
1131 * Pops up the menu or dropdown list of @combo_box.
1133 * This function is mostly intended for use by accessibility technologies;
1134 * applications should have little use for it.
1139 gtk_combo_box_popup (GtkComboBox *combo_box)
1141 gint x, y, width, height;
1143 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1145 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1148 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1150 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1151 combo_box->priv->active_item);
1153 if (combo_box->priv->wrap_width == 0)
1155 width = GTK_WIDGET (combo_box)->allocation.width;
1156 gtk_widget_set_size_request (combo_box->priv->popup_widget, width, -1);
1159 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1161 gtk_combo_box_menu_position, combo_box,
1166 gtk_widget_show_all (combo_box->priv->popup_frame);
1167 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1169 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1170 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1173 gtk_widget_show (combo_box->priv->popup_window);
1175 gtk_widget_grab_focus (combo_box->priv->popup_window);
1176 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1179 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1181 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1182 FALSE, GDK_CURRENT_TIME);
1183 gtk_widget_grab_focus (combo_box->priv->tree_view);
1186 gtk_grab_add (combo_box->priv->popup_window);
1187 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1188 GDK_BUTTON_PRESS_MASK |
1189 GDK_BUTTON_RELEASE_MASK |
1190 GDK_POINTER_MOTION_MASK,
1191 NULL, NULL, GDK_CURRENT_TIME);
1193 gtk_grab_add (combo_box->priv->tree_view);
1197 * gtk_combo_box_popdown:
1198 * @combo_box: a #GtkComboBox
1200 * Hides the menu or dropdown list of @combo_box.
1202 * This function is mostly intended for use by accessibility technologies;
1203 * applications should have little use for it.
1208 gtk_combo_box_popdown (GtkComboBox *combo_box)
1210 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1212 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1214 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1218 gtk_combo_box_list_remove_grabs (combo_box);
1219 gtk_widget_hide_all (combo_box->priv->popup_window);
1220 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1225 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1231 if (combo_box->priv->cell_view)
1232 gtk_widget_style_get (combo_box->priv->cell_view,
1233 "focus-line-width", &padding,
1238 /* add some pixels for good measure */
1239 padding += BONUS_PADDING;
1241 if (combo_box->priv->cell_view)
1242 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1247 return req.width + padding;
1251 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1257 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1260 combo_box->priv->width = 0;
1262 path = gtk_tree_path_new_from_indices (0, -1);
1264 if (combo_box->priv->cell_view)
1265 gtk_widget_style_get (combo_box->priv->cell_view,
1266 "focus-line-width", &padding,
1271 /* add some pixels for good measure */
1272 padding += BONUS_PADDING;
1278 if (combo_box->priv->cell_view)
1279 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1284 combo_box->priv->width = MAX (combo_box->priv->width,
1285 req.width + padding);
1287 gtk_tree_path_next (path);
1289 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1291 gtk_tree_path_free (path);
1295 gtk_combo_box_size_request (GtkWidget *widget,
1296 GtkRequisition *requisition)
1299 GtkRequisition bin_req;
1301 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1304 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1305 gtk_combo_box_remeasure (combo_box);
1306 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1308 if (!combo_box->priv->tree_view)
1312 if (combo_box->priv->cell_view)
1314 GtkRequisition button_req, sep_req, arrow_req;
1315 gint border_width, xthickness, ythickness;
1317 gtk_widget_size_request (combo_box->priv->button, &button_req);
1318 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1319 xthickness = combo_box->priv->button->style->xthickness;
1320 ythickness = combo_box->priv->button->style->ythickness;
1322 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1324 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1325 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1327 height = MAX (sep_req.height, arrow_req.height);
1328 height = MAX (height, bin_req.height);
1330 width = bin_req.width + sep_req.width + arrow_req.width;
1332 height += border_width + 1 + ythickness * 2 + 4;
1333 width += border_width + 1 + xthickness * 2 + 4;
1335 requisition->width = width;
1336 requisition->height = height;
1340 GtkRequisition but_req;
1342 gtk_widget_size_request (combo_box->priv->button, &but_req);
1344 requisition->width = bin_req.width + but_req.width;
1345 requisition->height = MAX (bin_req.height, but_req.height);
1351 GtkRequisition button_req, frame_req;
1353 /* sample + frame */
1354 *requisition = bin_req;
1356 if (combo_box->priv->cell_view_frame)
1358 gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
1359 requisition->width += 2 *
1360 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1361 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1362 requisition->height += 2 *
1363 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1364 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1368 gtk_widget_size_request (combo_box->priv->button, &button_req);
1370 requisition->height = MAX (requisition->height, button_req.height);
1371 requisition->width += button_req.width;
1376 gtk_combo_box_size_allocate (GtkWidget *widget,
1377 GtkAllocation *allocation)
1379 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1380 GtkAllocation child;
1382 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1384 widget->allocation = *allocation;
1386 if (!combo_box->priv->tree_view)
1388 if (combo_box->priv->cell_view)
1390 gint border_width, xthickness, ythickness;
1394 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1396 /* set some things ready */
1397 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1398 xthickness = combo_box->priv->button->style->xthickness;
1399 ythickness = combo_box->priv->button->style->ythickness;
1401 child.x = allocation->x + border_width + 1 + xthickness + 2;
1402 child.y = allocation->y + border_width + 1 + ythickness + 2;
1404 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1406 /* handle the children */
1407 gtk_widget_size_request (combo_box->priv->arrow, &req);
1408 child.width = req.width;
1409 child.height = allocation->height - 2 * (child.y - allocation->y);
1411 child.x += width - req.width;
1412 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1414 child.x += req.width;
1415 gtk_widget_size_request (combo_box->priv->separator, &req);
1416 child.width = req.width;
1418 child.x -= req.width;
1419 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1423 child.x += req.width;
1424 child.width = allocation->x + allocation->width
1425 - (border_width + 1 + xthickness + 2) - child.x;
1429 child.width = child.x;
1430 child.x = allocation->x + border_width + 1 + xthickness + 2;
1431 child.width -= child.x;
1434 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1438 gtk_widget_size_request (combo_box->priv->button, &req);
1440 child.x = allocation->x;
1442 child.x = allocation->x + allocation->width - req.width;
1443 child.y = allocation->y;
1444 child.width = req.width;
1445 child.height = allocation->height;
1446 gtk_widget_size_allocate (combo_box->priv->button, &child);
1449 child.x = allocation->x + req.width;
1451 child.x = allocation->x;
1452 child.y = allocation->y;
1453 child.width = allocation->width - req.width;
1454 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1462 gtk_widget_size_request (combo_box->priv->button, &req);
1464 child.x = allocation->x;
1466 child.x = allocation->x + allocation->width - req.width;
1467 child.y = allocation->y;
1468 child.width = req.width;
1469 child.height = allocation->height;
1470 gtk_widget_size_allocate (combo_box->priv->button, &child);
1474 child.x = allocation->x + req.width;
1476 child.x = allocation->x;
1477 child.y = allocation->y;
1478 child.width = allocation->width - req.width;
1479 child.height = allocation->height;
1481 if (combo_box->priv->cell_view_frame)
1483 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1487 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1488 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1490 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1491 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1492 child.width -= 2 * (
1493 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1494 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1495 child.height -= 2 * (
1496 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1497 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1500 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1505 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1507 if (combo_box->priv->model)
1509 g_signal_handler_disconnect (combo_box->priv->model,
1510 combo_box->priv->inserted_id);
1511 g_signal_handler_disconnect (combo_box->priv->model,
1512 combo_box->priv->deleted_id);
1513 g_signal_handler_disconnect (combo_box->priv->model,
1514 combo_box->priv->reordered_id);
1515 g_signal_handler_disconnect (combo_box->priv->model,
1516 combo_box->priv->changed_id);
1520 if (!combo_box->priv->tree_view)
1522 if (combo_box->priv->popup_widget)
1523 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1524 (GtkCallback)gtk_widget_destroy, NULL);
1527 if (combo_box->priv->model)
1529 g_object_unref (G_OBJECT (combo_box->priv->model));
1530 combo_box->priv->model = NULL;
1535 gtk_combo_box_forall (GtkContainer *container,
1536 gboolean include_internals,
1537 GtkCallback callback,
1538 gpointer callback_data)
1540 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1542 if (include_internals)
1544 if (combo_box->priv->button)
1545 (* callback) (combo_box->priv->button, callback_data);
1546 if (combo_box->priv->box)
1547 (* callback) (combo_box->priv->box, callback_data);
1548 if (combo_box->priv->separator)
1549 (* callback) (combo_box->priv->separator, callback_data);
1550 if (combo_box->priv->arrow)
1551 (* callback) (combo_box->priv->arrow, callback_data);
1552 if (combo_box->priv->cell_view_frame)
1553 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1556 if (GTK_BIN (container)->child)
1557 (* callback) (GTK_BIN (container)->child, callback_data);
1561 gtk_combo_box_expose_event (GtkWidget *widget,
1562 GdkEventExpose *event)
1564 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1566 if (!combo_box->priv->tree_view)
1568 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1569 combo_box->priv->button, event);
1573 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1574 combo_box->priv->button, event);
1576 if (combo_box->priv->cell_view_frame)
1577 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1578 combo_box->priv->cell_view_frame, event);
1581 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1582 GTK_BIN (widget)->child, event);
1588 gtk_combo_box_scroll_event (GtkWidget *widget,
1589 GdkEventScroll *event)
1591 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1595 index = gtk_combo_box_get_active (combo_box);
1599 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1601 if (event->direction == GDK_SCROLL_UP)
1606 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1617 cell_view_sync_cells (GtkComboBox *combo_box,
1618 GtkCellView *cell_view)
1622 for (k = combo_box->priv->cells; k; k = k->next)
1625 ComboCellInfo *info = (ComboCellInfo *)k->data;
1627 if (info->pack == GTK_PACK_START)
1628 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1629 info->cell, info->expand);
1630 else if (info->pack == GTK_PACK_END)
1631 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1632 info->cell, info->expand);
1634 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1636 info->func, info->func_data, NULL);
1638 for (j = info->attributes; j; j = j->next->next)
1640 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1643 GPOINTER_TO_INT (j->next->data));
1649 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1650 gboolean add_children)
1654 if (combo_box->priv->cell_view)
1656 combo_box->priv->button = gtk_toggle_button_new ();
1657 g_signal_connect (combo_box->priv->button, "toggled",
1658 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1659 g_signal_connect_after (combo_box->priv->button, "key_press_event",
1660 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1661 gtk_widget_set_parent (combo_box->priv->button,
1662 GTK_BIN (combo_box)->child->parent);
1664 combo_box->priv->box = gtk_hbox_new (FALSE, 0);
1665 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1666 combo_box->priv->box);
1668 combo_box->priv->separator = gtk_vseparator_new ();
1669 gtk_container_add (GTK_CONTAINER (combo_box->priv->box),
1670 combo_box->priv->separator);
1672 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1673 gtk_container_add (GTK_CONTAINER (combo_box->priv->box),
1674 combo_box->priv->arrow);
1676 gtk_widget_show_all (combo_box->priv->button);
1680 combo_box->priv->button = gtk_toggle_button_new ();
1681 g_signal_connect (combo_box->priv->button, "toggled",
1682 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1683 g_signal_connect_after (combo_box, "key_press_event",
1684 G_CALLBACK (gtk_combo_box_key_press), combo_box);
1685 gtk_widget_set_parent (combo_box->priv->button,
1686 GTK_BIN (combo_box)->child->parent);
1688 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1689 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1690 combo_box->priv->arrow);
1691 gtk_widget_show_all (combo_box->priv->button);
1694 g_signal_connect (combo_box->priv->button, "button_press_event",
1695 G_CALLBACK (gtk_combo_box_menu_button_press),
1697 g_signal_connect (combo_box->priv->button, "state_changed",
1698 G_CALLBACK (gtk_combo_box_menu_state_changed),
1701 /* create our funky menu */
1702 menu = gtk_menu_new ();
1703 g_signal_connect (menu, "key_press_event",
1704 G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1705 gtk_combo_box_set_popup_widget (combo_box, menu);
1709 gtk_combo_box_menu_fill (combo_box);
1714 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1720 if (!combo_box->priv->model)
1723 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1724 menu = combo_box->priv->popup_widget;
1726 if (combo_box->priv->add_tearoffs)
1728 GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
1730 gtk_widget_show (tearoff);
1732 if (combo_box->priv->wrap_width)
1733 gtk_menu_attach (GTK_MENU (menu), tearoff,
1734 0, combo_box->priv->wrap_width, 0, 1);
1736 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
1739 for (i = 0; i < items; i++)
1743 path = gtk_tree_path_new_from_indices (i, -1);
1744 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1746 g_signal_connect (tmp, "activate",
1747 G_CALLBACK (gtk_combo_box_menu_item_activate),
1750 cell_view_sync_cells (combo_box,
1751 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1753 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1755 if (combo_box->priv->wrap_width)
1756 gtk_combo_box_relayout_item (combo_box, i);
1758 gtk_widget_show (tmp);
1760 gtk_tree_path_free (path);
1765 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1767 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1768 G_SIGNAL_MATCH_DATA,
1770 gtk_combo_box_menu_button_press, NULL);
1771 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1772 G_SIGNAL_MATCH_DATA,
1774 gtk_combo_box_menu_state_changed, NULL);
1776 /* unparent will remove our latest ref */
1777 gtk_widget_unparent (combo_box->priv->button);
1779 combo_box->priv->box = NULL;
1780 combo_box->priv->button = NULL;
1781 combo_box->priv->arrow = NULL;
1782 combo_box->priv->separator = NULL;
1784 /* changing the popup window will unref the menu and the children */
1792 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1799 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1803 if (combo_box->priv->col_column == -1)
1806 gtk_tree_model_get (combo_box->priv->model, &iter,
1807 combo_box->priv->col_column, cols,
1813 if (combo_box->priv->row_column == -1)
1816 gtk_tree_model_get (combo_box->priv->model, &iter,
1817 combo_box->priv->row_column, rows,
1823 menu_occupied (GtkMenu *menu,
1827 guint bottom_attach)
1831 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1832 g_return_val_if_fail (left_attach < right_attach, TRUE);
1833 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1835 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1839 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1842 "bottom_attach", &b,
1846 /* look if this item intersects with the given coordinates */
1847 if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
1855 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1858 gint current_col = 0, current_row = 0;
1864 menu = combo_box->priv->popup_widget;
1865 if (!GTK_IS_MENU_SHELL (menu))
1868 list = gtk_container_get_children (GTK_CONTAINER (menu));
1869 if (combo_box->priv->add_tearoffs)
1871 item = g_list_nth_data (list, index);
1874 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1876 /* look for a good spot */
1879 if (current_col + cols > combo_box->priv->wrap_width)
1885 if (!menu_occupied (GTK_MENU (menu),
1886 current_col, current_col + cols,
1887 current_row, current_row + rows))
1893 /* set attach props */
1894 gtk_menu_attach (GTK_MENU (menu), item,
1895 current_col, current_col + cols,
1896 current_row, current_row + rows);
1900 gtk_combo_box_relayout (GtkComboBox *combo_box)
1905 menu = combo_box->priv->popup_widget;
1907 /* do nothing unless we are in menu style and realized */
1908 if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
1911 /* get rid of all children */
1912 list = gtk_container_get_children (GTK_CONTAINER (menu));
1914 for (j = g_list_last (list); j; j = j->prev)
1915 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1920 gtk_combo_box_menu_fill (combo_box);
1925 gtk_combo_box_menu_button_press (GtkWidget *widget,
1926 GdkEventButton *event,
1929 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1932 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1935 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1937 combo_box->priv->popup_in_progress = TRUE;
1939 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1940 combo_box->priv->active_item);
1942 if (combo_box->priv->wrap_width == 0)
1944 width = GTK_WIDGET (combo_box)->allocation.width;
1945 gtk_widget_set_size_request (combo_box->priv->popup_widget, width, -1);
1948 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1950 gtk_combo_box_menu_position, combo_box,
1951 event->button, event->time);
1960 gtk_combo_box_menu_state_changed (GtkWidget *widget,
1961 GtkStateType previous,
1964 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1966 if (combo_box->priv->cell_view)
1968 gtk_widget_set_state (combo_box->priv->cell_view,
1969 GTK_WIDGET_STATE (widget));
1971 gtk_widget_queue_draw (combo_box->priv->cell_view);
1976 gtk_combo_box_menu_item_activate (GtkWidget *item,
1982 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1984 menu = combo_box->priv->popup_widget;
1985 g_return_if_fail (GTK_IS_MENU (menu));
1987 children = GTK_MENU_SHELL (menu)->children;
1988 if (combo_box->priv->add_tearoffs)
1989 children = children->next;
1991 index = g_list_index (children, item);
1993 gtk_combo_box_set_active (combo_box, index);
1997 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
2002 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2003 gint index = gtk_tree_path_get_indices (path)[0];
2005 if (combo_box->priv->active_item >= index)
2006 combo_box->priv->active_item++;
2008 if (!combo_box->priv->tree_view)
2009 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
2013 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
2017 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2018 gint index = gtk_tree_path_get_indices (path)[0];
2020 if (!combo_box->priv->tree_view)
2021 gtk_combo_box_menu_row_deleted (model, path, user_data);
2023 if (index == combo_box->priv->active_item)
2025 gint items = gtk_tree_model_iter_n_children (model, NULL);
2028 gtk_combo_box_set_active_internal (combo_box, -1);
2029 else if (index == items)
2030 gtk_combo_box_set_active_internal (combo_box, index - 1);
2032 gtk_combo_box_set_active_internal (combo_box, index);
2034 else if (combo_box->priv->active_item > index)
2035 combo_box->priv->active_item--;
2039 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
2045 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2046 gint items = gtk_tree_model_iter_n_children (model, NULL);
2049 for (i = 0; i < items; i++)
2050 if (new_order[i] == combo_box->priv->active_item)
2052 combo_box->priv->active_item = i;
2056 if (!combo_box->priv->tree_view)
2057 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2061 gtk_combo_box_model_row_changed (GtkTreeModel *model,
2066 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2067 gint index = gtk_tree_path_get_indices (path)[0];
2069 if (index == combo_box->priv->active_item &&
2070 combo_box->priv->cell_view)
2071 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2073 if (combo_box->priv->tree_view)
2074 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2076 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2081 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2088 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2090 if (!combo_box->priv->popup_widget)
2093 menu = combo_box->priv->popup_widget;
2094 g_return_if_fail (GTK_IS_MENU (menu));
2096 item = gtk_cell_view_menu_item_new_from_model (model, path);
2097 g_signal_connect (item, "activate",
2098 G_CALLBACK (gtk_combo_box_menu_item_activate),
2101 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2103 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2104 gtk_tree_path_get_indices (path)[0]);
2105 gtk_widget_show (item);
2109 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2116 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2118 if (!combo_box->priv->popup_widget)
2121 index = gtk_tree_path_get_indices (path)[0];
2123 menu = combo_box->priv->popup_widget;
2124 g_return_if_fail (GTK_IS_MENU (menu));
2126 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2127 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2129 gtk_container_remove (GTK_CONTAINER (menu), item);
2133 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2139 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2141 gtk_combo_box_relayout (combo_box);
2145 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2150 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2153 if (!combo_box->priv->popup_widget)
2156 if (combo_box->priv->wrap_width)
2157 gtk_combo_box_relayout_item (combo_box,
2158 gtk_tree_path_get_indices (path)[0]);
2160 width = gtk_combo_box_calc_requested_width (combo_box, path);
2162 if (width > combo_box->priv->width)
2164 if (combo_box->priv->cell_view)
2166 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2167 gtk_widget_queue_resize (combo_box->priv->cell_view);
2169 combo_box->priv->width = width;
2178 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2181 GtkTreeSelection *sel;
2183 combo_box->priv->button = gtk_toggle_button_new ();
2184 gtk_widget_set_parent (combo_box->priv->button,
2185 GTK_BIN (combo_box)->child->parent);
2186 g_signal_connect (combo_box->priv->button, "button_press_event",
2187 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2188 g_signal_connect (combo_box->priv->button, "toggled",
2189 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2190 g_signal_connect_after (combo_box, "key_press_event",
2191 G_CALLBACK (gtk_combo_box_key_press), combo_box);
2193 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2194 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2195 combo_box->priv->arrow);
2196 combo_box->priv->separator = NULL;
2197 gtk_widget_show_all (combo_box->priv->button);
2199 if (combo_box->priv->cell_view)
2201 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2202 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2203 GTK_BIN (combo_box)->child->parent);
2204 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2207 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2208 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2210 combo_box->priv->box = gtk_event_box_new ();
2211 gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->box),
2214 gtk_container_add (GTK_CONTAINER (combo_box->priv->cell_view_frame),
2215 combo_box->priv->box);
2217 gtk_widget_show_all (combo_box->priv->cell_view_frame);
2219 g_signal_connect (combo_box->priv->box, "button_press_event",
2220 G_CALLBACK (gtk_combo_box_list_button_pressed),
2224 combo_box->priv->tree_view = gtk_tree_view_new ();
2225 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2226 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2227 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2229 g_object_set (combo_box->priv->tree_view,
2230 "hover_selection", TRUE,
2233 if (combo_box->priv->model)
2234 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2235 combo_box->priv->model);
2237 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2238 G_CALLBACK (gtk_combo_box_list_button_pressed),
2240 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2241 G_CALLBACK (gtk_combo_box_list_button_released),
2243 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2244 G_CALLBACK (gtk_combo_box_list_key_press),
2247 combo_box->priv->column = gtk_tree_view_column_new ();
2248 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2249 combo_box->priv->column);
2252 for (i = combo_box->priv->cells; i; i = i->next)
2255 ComboCellInfo *info = (ComboCellInfo *)i->data;
2257 if (info->pack == GTK_PACK_START)
2258 gtk_tree_view_column_pack_start (combo_box->priv->column,
2259 info->cell, info->expand);
2260 else if (info->pack == GTK_PACK_END)
2261 gtk_tree_view_column_pack_end (combo_box->priv->column,
2262 info->cell, info->expand);
2264 for (j = info->attributes; j; j = j->next->next)
2266 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2269 GPOINTER_TO_INT (j->next->data));
2273 if (combo_box->priv->active_item != -1)
2277 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2280 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2282 gtk_tree_path_free (path);
2286 /* set sample/popup widgets */
2287 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2289 gtk_widget_show (combo_box->priv->tree_view);
2293 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2295 /* disconnect signals */
2296 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2297 G_SIGNAL_MATCH_DATA,
2298 0, 0, NULL, NULL, combo_box);
2299 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2300 G_SIGNAL_MATCH_DATA,
2302 gtk_combo_box_list_button_pressed,
2304 g_signal_handlers_disconnect_matched (combo_box->priv->box,
2305 G_SIGNAL_MATCH_DATA,
2307 gtk_combo_box_list_button_pressed,
2310 /* destroy things (unparent will kill the latest ref from us)
2311 * last unref on button will destroy the arrow
2313 gtk_widget_unparent (combo_box->priv->button);
2314 combo_box->priv->button = NULL;
2315 combo_box->priv->arrow = NULL;
2317 if (combo_box->priv->cell_view)
2319 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2320 "background_set", FALSE,
2324 if (combo_box->priv->cell_view_frame)
2326 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2327 combo_box->priv->cell_view_frame = NULL;
2328 combo_box->priv->box = NULL;
2331 gtk_widget_destroy (combo_box->priv->tree_view);
2333 combo_box->priv->tree_view = NULL;
2334 combo_box->priv->popup_widget = NULL;
2339 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2341 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2342 gtk_grab_remove (combo_box->priv->tree_view);
2344 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2346 gtk_grab_remove (combo_box->priv->popup_window);
2347 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2348 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2353 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2354 GdkEventButton *event,
2357 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2359 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2361 if (ewidget == combo_box->priv->tree_view)
2364 if ((ewidget != combo_box->priv->button && ewidget != combo_box->priv->box) ||
2365 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2368 gtk_combo_box_popup (combo_box);
2370 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2373 combo_box->priv->popup_in_progress = TRUE;
2379 gtk_combo_box_list_button_released (GtkWidget *widget,
2380 GdkEventButton *event,
2384 GtkTreePath *path = NULL;
2386 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2388 gboolean popup_in_progress = FALSE;
2390 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2392 if (combo_box->priv->popup_in_progress)
2394 popup_in_progress = TRUE;
2395 combo_box->priv->popup_in_progress = FALSE;
2398 if (ewidget != combo_box->priv->tree_view)
2400 if (ewidget == combo_box->priv->button &&
2401 !popup_in_progress &&
2402 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2404 gtk_combo_box_popdown (combo_box);
2408 /* released outside treeview */
2409 if (ewidget != combo_box->priv->button)
2411 gtk_combo_box_popdown (combo_box);
2419 /* select something cool */
2420 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2426 return TRUE; /* clicked outside window? */
2428 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2429 gtk_combo_box_popdown (combo_box);
2431 gtk_tree_path_free (path);
2437 gtk_combo_box_key_press (GtkWidget *widget,
2441 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2442 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2443 gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2444 gint index = gtk_combo_box_get_active (combo_box);
2447 if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
2448 state == GDK_MOD1_MASK)
2450 gtk_combo_box_popup (combo_box);
2455 switch (event->keyval)
2459 new_index = index + 1;
2463 new_index = index - 1;
2466 case GDK_KP_Page_Up:
2472 case GDK_KP_Page_Down:
2475 new_index = items - 1;
2481 gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2487 gtk_combo_box_menu_key_press (GtkWidget *widget,
2491 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2492 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2494 if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2495 state == GDK_MOD1_MASK)
2497 gtk_combo_box_popdown (combo_box);
2506 gtk_combo_box_list_key_press (GtkWidget *widget,
2510 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2511 guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2513 if (event->keyval == GDK_Escape ||
2514 ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
2515 state == GDK_MOD1_MASK))
2517 /* reset active item -- this is incredibly lame and ugly */
2518 gtk_combo_box_set_active (combo_box,
2519 gtk_combo_box_get_active (combo_box));
2521 gtk_combo_box_popdown (combo_box);
2526 if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2527 event->keyval == GDK_space || event->keyval == GDK_KP_Space)
2531 GtkTreeModel *model = NULL;
2532 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2534 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2539 path = gtk_tree_model_get_path (model, &iter);
2542 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2543 gtk_tree_path_free (path);
2547 gtk_combo_box_popdown (combo_box);
2556 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2561 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2564 width = gtk_combo_box_calc_requested_width (combo_box, path);
2566 if (width > combo_box->priv->width)
2568 if (combo_box->priv->cell_view)
2570 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2571 gtk_widget_queue_resize (combo_box->priv->cell_view);
2573 combo_box->priv->width = width;
2578 * GtkCellLayout implementation
2581 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2582 GtkCellRenderer *cell,
2585 ComboCellInfo *info;
2586 GtkComboBox *combo_box;
2589 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2590 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2592 combo_box = GTK_COMBO_BOX (layout);
2594 g_object_ref (G_OBJECT (cell));
2595 gtk_object_sink (GTK_OBJECT (cell));
2597 info = g_new0 (ComboCellInfo, 1);
2599 info->expand = expand;
2600 info->pack = GTK_PACK_START;
2602 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2604 if (combo_box->priv->cell_view)
2605 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2608 if (combo_box->priv->column)
2609 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2611 menu = combo_box->priv->popup_widget;
2612 if (GTK_IS_MENU (menu))
2616 list = gtk_container_get_children (GTK_CONTAINER (menu));
2617 for (i = list; i; i = i->next)
2621 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2624 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2625 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2627 view = GTK_CELL_VIEW (i->data);
2629 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2636 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2637 GtkCellRenderer *cell,
2640 ComboCellInfo *info;
2641 GtkComboBox *combo_box;
2644 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2645 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2647 combo_box = GTK_COMBO_BOX (layout);
2649 g_object_ref (G_OBJECT (cell));
2650 gtk_object_sink (GTK_OBJECT (cell));
2652 info = g_new0 (ComboCellInfo, 1);
2654 info->expand = expand;
2655 info->pack = GTK_PACK_END;
2657 if (combo_box->priv->cell_view)
2658 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2661 if (combo_box->priv->column)
2662 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2664 menu = combo_box->priv->popup_widget;
2665 if (GTK_IS_MENU (menu))
2669 list = gtk_container_get_children (GTK_CONTAINER (menu));
2670 for (i = list; i; i = i->next)
2674 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2677 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2678 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2680 view = GTK_CELL_VIEW (i->data);
2682 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2689 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2692 GtkComboBox *combo_box;
2695 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2697 combo_box = GTK_COMBO_BOX (layout);
2699 if (combo_box->priv->cell_view)
2700 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2702 if (combo_box->priv->column)
2703 gtk_tree_view_column_clear (combo_box->priv->column);
2705 for (i = combo_box->priv->cells; i; i = i->next)
2707 ComboCellInfo *info = (ComboCellInfo *)i->data;
2709 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2710 g_object_unref (G_OBJECT (info->cell));
2714 g_slist_free (combo_box->priv->cells);
2715 combo_box->priv->cells = NULL;
2717 menu = combo_box->priv->popup_widget;
2718 if (GTK_IS_MENU (menu))
2722 list = gtk_container_get_children (GTK_CONTAINER (menu));
2723 for (i = list; i; i = i->next)
2727 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2730 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2731 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2733 view = GTK_CELL_VIEW (i->data);
2735 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2742 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2743 GtkCellRenderer *cell,
2744 const gchar *attribute,
2747 ComboCellInfo *info;
2748 GtkComboBox *combo_box;
2751 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2752 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2754 combo_box = GTK_COMBO_BOX (layout);
2756 info = gtk_combo_box_get_cell_info (combo_box, cell);
2758 info->attributes = g_slist_prepend (info->attributes,
2759 GINT_TO_POINTER (column));
2760 info->attributes = g_slist_prepend (info->attributes,
2761 g_strdup (attribute));
2763 if (combo_box->priv->cell_view)
2764 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2765 cell, attribute, column);
2767 if (combo_box->priv->column)
2768 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2769 cell, attribute, column);
2771 menu = combo_box->priv->popup_widget;
2772 if (GTK_IS_MENU (menu))
2776 list = gtk_container_get_children (GTK_CONTAINER (menu));
2777 for (i = list; i; i = i->next)
2781 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2784 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2785 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2787 view = GTK_CELL_VIEW (i->data);
2789 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2795 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2799 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2800 GtkCellRenderer *cell,
2801 GtkCellLayoutDataFunc func,
2803 GDestroyNotify destroy)
2805 ComboCellInfo *info;
2806 GtkComboBox *combo_box;
2809 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2811 combo_box = GTK_COMBO_BOX (layout);
2813 info = gtk_combo_box_get_cell_info (combo_box, cell);
2814 g_return_if_fail (info != NULL);
2818 GDestroyNotify d = info->destroy;
2820 info->destroy = NULL;
2821 d (info->func_data);
2825 info->func_data = func_data;
2826 info->destroy = destroy;
2828 if (combo_box->priv->cell_view)
2829 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2831 if (combo_box->priv->column)
2832 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2834 menu = combo_box->priv->popup_widget;
2835 if (GTK_IS_MENU (menu))
2839 list = gtk_container_get_children (GTK_CONTAINER (menu));
2840 for (i = list; i; i = i->next)
2844 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2847 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2848 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2850 view = GTK_CELL_VIEW (i->data);
2852 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2853 func, func_data, NULL);
2858 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2862 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2863 GtkCellRenderer *cell)
2865 ComboCellInfo *info;
2866 GtkComboBox *combo_box;
2870 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2871 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2873 combo_box = GTK_COMBO_BOX (layout);
2875 info = gtk_combo_box_get_cell_info (combo_box, cell);
2876 g_return_if_fail (info != NULL);
2878 list = info->attributes;
2879 while (list && list->next)
2881 g_free (list->data);
2882 list = list->next->next;
2884 g_slist_free (info->attributes);
2885 info->attributes = NULL;
2887 if (combo_box->priv->cell_view)
2888 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2890 if (combo_box->priv->column)
2891 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2893 menu = combo_box->priv->popup_widget;
2894 if (GTK_IS_MENU (menu))
2898 list = gtk_container_get_children (GTK_CONTAINER (menu));
2899 for (i = list; i; i = i->next)
2903 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2906 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2907 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2909 view = GTK_CELL_VIEW (i->data);
2911 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2916 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2920 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2921 GtkCellRenderer *cell,
2924 ComboCellInfo *info;
2925 GtkComboBox *combo_box;
2929 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2930 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2932 combo_box = GTK_COMBO_BOX (layout);
2934 info = gtk_combo_box_get_cell_info (combo_box, cell);
2936 g_return_if_fail (info != NULL);
2937 g_return_if_fail (position >= 0);
2939 link = g_slist_find (combo_box->priv->cells, info);
2941 g_return_if_fail (link != NULL);
2943 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2944 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2947 if (combo_box->priv->cell_view)
2948 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2951 if (combo_box->priv->column)
2952 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2955 menu = combo_box->priv->popup_widget;
2956 if (GTK_IS_MENU (menu))
2960 list = gtk_container_get_children (GTK_CONTAINER (menu));
2961 for (i = list; i; i = i->next)
2965 if (GTK_IS_TEAROFF_MENU_ITEM (i->data))
2968 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2969 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2971 view = GTK_CELL_VIEW (i->data);
2973 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2978 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2986 * gtk_combo_box_new:
2988 * Creates a new empty #GtkComboBox.
2990 * Return value: A new #GtkComboBox.
2995 gtk_combo_box_new (void)
2997 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
3001 * gtk_combo_box_new_with_model:
3002 * @model: A #GtkTreeModel.
3004 * Creates a new #GtkComboBox with the model initialized to @model.
3006 * Return value: A new #GtkComboBox.
3011 gtk_combo_box_new_with_model (GtkTreeModel *model)
3013 GtkComboBox *combo_box;
3015 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
3017 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
3021 return GTK_WIDGET (combo_box);
3025 * gtk_combo_box_get_wrap_width:
3026 * @combo_box: A #GtkComboBox.
3028 * Returns the wrap width which is used to determine the number
3029 * of columns for the popup menu. If the wrap width is larger than
3030 * 1, the combo box is in table mode.
3032 * Returns: the wrap width.
3037 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
3039 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3041 return combo_box->priv->wrap_width;
3045 * gtk_combo_box_set_wrap_width:
3046 * @combo_box: A #GtkComboBox.
3047 * @width: Preferred number of columns.
3049 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
3050 * the preferred number of columns when you want to the popup to be layed out
3056 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
3059 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3060 g_return_if_fail (width >= 0);
3062 if (width != combo_box->priv->wrap_width)
3064 combo_box->priv->wrap_width = width;
3066 gtk_combo_box_check_appearance (combo_box);
3067 gtk_combo_box_relayout (combo_box);
3069 g_object_notify (G_OBJECT (combo_box), "wrap_width");
3074 * gtk_combo_box_get_row_span_column:
3075 * @combo_box: A #GtkComboBox.
3077 * Returns the column with row span information for @combo_box.
3079 * Returns: the row span column.
3084 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
3086 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3088 return combo_box->priv->row_column;
3092 * gtk_combo_box_set_row_span_column:
3093 * @combo_box: A #GtkComboBox.
3094 * @row_span: A column in the model passed during construction.
3096 * Sets the column with row span information for @combo_box to be @row_span.
3097 * The row span column contains integers which indicate how many rows
3098 * an item should span.
3103 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
3108 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3110 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3111 g_return_if_fail (row_span >= 0 && row_span < col);
3113 if (row_span != combo_box->priv->row_column)
3115 combo_box->priv->row_column = row_span;
3117 gtk_combo_box_relayout (combo_box);
3119 g_object_notify (G_OBJECT (combo_box), "row_span_column");
3124 * gtk_combo_box_get_column_span_column:
3125 * @combo_box: A #GtkComboBox.
3127 * Returns the column with column span information for @combo_box.
3129 * Returns: the column span column.
3134 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
3136 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
3138 return combo_box->priv->col_column;
3142 * gtk_combo_box_set_column_span_column:
3143 * @combo_box: A #GtkComboBox.
3144 * @column_span: A column in the model passed during construction.
3146 * Sets the column with column span information for @combo_box to be
3147 * @column_span. The column span column contains integers which indicate
3148 * how many columns an item should span.
3153 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
3158 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3160 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3161 g_return_if_fail (column_span >= 0 && column_span < col);
3163 if (column_span != combo_box->priv->col_column)
3165 combo_box->priv->col_column = column_span;
3167 gtk_combo_box_relayout (combo_box);
3169 g_object_notify (G_OBJECT (combo_box), "column_span_column");
3174 * gtk_combo_box_get_active:
3175 * @combo_box: A #GtkComboBox.
3177 * Returns the index of the currently active item, or -1 if there's no
3180 * Return value: An integer which is the index of the currently active item, or
3181 * -1 if there's no active item.
3186 gtk_combo_box_get_active (GtkComboBox *combo_box)
3188 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3190 return combo_box->priv->active_item;
3194 * gtk_combo_box_set_active:
3195 * @combo_box: A #GtkComboBox.
3196 * @index: An index in the model passed during construction, or -1 to have
3199 * Sets the active item of @combo_box to be the item at @index.
3204 gtk_combo_box_set_active (GtkComboBox *combo_box,
3207 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3208 /* -1 means "no item selected" */
3209 g_return_if_fail (index >= -1);
3211 if (combo_box->priv->active_item == index)
3214 gtk_combo_box_set_active_internal (combo_box, index);
3218 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3223 combo_box->priv->active_item = index;
3227 if (combo_box->priv->tree_view)
3228 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3231 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3233 if (GTK_IS_MENU (menu))
3234 gtk_menu_set_active (menu, -1);
3237 if (combo_box->priv->cell_view)
3238 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3242 path = gtk_tree_path_new_from_indices (index, -1);
3244 if (combo_box->priv->tree_view)
3245 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3248 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3250 if (GTK_IS_MENU (menu))
3251 gtk_menu_set_active (GTK_MENU (menu), index);
3254 if (combo_box->priv->cell_view)
3255 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3257 gtk_tree_path_free (path);
3260 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3265 * gtk_combo_box_get_active_iter:
3266 * @combo_box: A #GtkComboBox
3267 * @iter: The uninitialized #GtkTreeIter.
3269 * Sets @iter to point to the current active item, if it exists.
3271 * Return value: %TRUE, if @iter was set
3276 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3283 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3285 active = gtk_combo_box_get_active (combo_box);
3289 path = gtk_tree_path_new_from_indices (active, -1);
3290 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3292 gtk_tree_path_free (path);
3298 * gtk_combo_box_set_active_iter:
3299 * @combo_box: A #GtkComboBox
3300 * @iter: The #GtkTreeIter.
3302 * Sets the current active item to be the one referenced by @iter.
3303 * @iter must correspond to a path of depth one.
3308 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3313 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3315 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3316 g_return_if_fail (path != NULL);
3317 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3319 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3320 gtk_tree_path_free (path);
3324 * gtk_combo_box_set_model:
3325 * @combo_box: A #GtkComboBox.
3326 * @model: A #GtkTreeModel.
3328 * Sets the model used by @combo_box to be @model. Will unset a
3329 * previously set model (if applicable).
3334 gtk_combo_box_set_model (GtkComboBox *combo_box,
3335 GtkTreeModel *model)
3337 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3338 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3340 if (model == combo_box->priv->model)
3343 if (combo_box->priv->model)
3344 gtk_combo_box_unset_model (combo_box);
3346 combo_box->priv->model = model;
3347 g_object_ref (G_OBJECT (combo_box->priv->model));
3349 combo_box->priv->inserted_id =
3350 g_signal_connect (combo_box->priv->model, "row_inserted",
3351 G_CALLBACK (gtk_combo_box_model_row_inserted),
3353 combo_box->priv->deleted_id =
3354 g_signal_connect (combo_box->priv->model, "row_deleted",
3355 G_CALLBACK (gtk_combo_box_model_row_deleted),
3357 combo_box->priv->reordered_id =
3358 g_signal_connect (combo_box->priv->model, "rows_reordered",
3359 G_CALLBACK (gtk_combo_box_model_rows_reordered),
3361 combo_box->priv->changed_id =
3362 g_signal_connect (combo_box->priv->model, "row_changed",
3363 G_CALLBACK (gtk_combo_box_model_row_changed),
3366 if (combo_box->priv->tree_view)
3369 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3370 combo_box->priv->model);
3375 if (combo_box->priv->popup_widget)
3376 gtk_combo_box_menu_fill (combo_box);
3380 if (combo_box->priv->cell_view)
3381 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3382 combo_box->priv->model);
3386 * gtk_combo_box_get_model
3387 * @combo_box: A #GtkComboBox.
3389 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3391 * Return value: A #GtkTreeModel which was passed during construction.
3396 gtk_combo_box_get_model (GtkComboBox *combo_box)
3398 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3400 return combo_box->priv->model;
3404 /* convenience API for simple text combos */
3407 * gtk_combo_box_new_text:
3409 * Convenience function which constructs a new text combo box, which is a
3410 * #GtkComboBox just displaying strings. If you use this function to create
3411 * a text combo box, you should only manipulate its data source with the
3412 * following convenience functions: gtk_combo_box_append_text(),
3413 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3414 * gtk_combo_box_remove_text().
3416 * Return value: A new text combo box.
3421 gtk_combo_box_new_text (void)
3423 GtkWidget *combo_box;
3424 GtkCellRenderer *cell;
3425 GtkListStore *store;
3427 store = gtk_list_store_new (1, G_TYPE_STRING);
3429 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3431 cell = gtk_cell_renderer_text_new ();
3432 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3433 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3441 * gtk_combo_box_append_text:
3442 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3445 * Appends @string to the list of strings stored in @combo_box. Note that
3446 * you can only use this function with combo boxes constructed with
3447 * gtk_combo_box_new_text().
3452 gtk_combo_box_append_text (GtkComboBox *combo_box,
3456 GtkListStore *store;
3458 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3459 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3460 g_return_if_fail (text != NULL);
3462 store = GTK_LIST_STORE (combo_box->priv->model);
3464 gtk_list_store_append (store, &iter);
3465 gtk_list_store_set (store, &iter, 0, text, -1);
3469 * gtk_combo_box_insert_text:
3470 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3471 * @position: An index to insert @text.
3474 * Inserts @string at @position in the list of strings stored in @combo_box.
3475 * Note that you can only use this function with combo boxes constructed
3476 * with gtk_combo_box_new_text().
3481 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3486 GtkListStore *store;
3488 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3489 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3490 g_return_if_fail (position >= 0);
3491 g_return_if_fail (text != NULL);
3493 store = GTK_LIST_STORE (combo_box->priv->model);
3495 gtk_list_store_insert (store, &iter, position);
3496 gtk_list_store_set (store, &iter, 0, text, -1);
3500 * gtk_combo_box_prepend_text:
3501 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3504 * Prepends @string to the list of strings stored in @combo_box. Note that
3505 * you can only use this function with combo boxes constructed with
3506 * gtk_combo_box_new_text().
3511 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3515 GtkListStore *store;
3517 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3518 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3519 g_return_if_fail (text != NULL);
3521 store = GTK_LIST_STORE (combo_box->priv->model);
3523 gtk_list_store_prepend (store, &iter);
3524 gtk_list_store_set (store, &iter, 0, text, -1);
3528 * gtk_combo_box_remove_text:
3529 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3530 * @position: Index of the item to remove.
3532 * Removes the string at @position from @combo_box. Note that you can only use
3533 * this function with combo boxes constructed with gtk_combo_box_new_text().
3538 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3542 GtkListStore *store;
3544 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3545 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3546 g_return_if_fail (position >= 0);
3548 store = GTK_LIST_STORE (combo_box->priv->model);
3550 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3552 gtk_list_store_remove (store, &iter);
3556 * gtk_combo_box_get_active_text:
3557 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3559 * Returns the currently active string in @combo_box or %NULL if none
3560 * is selected. Note that you can only use this function with combo
3561 * boxes constructed with gtk_combo_box_new_text().
3563 * Returns: a newly allocated string containing the currently active text.
3568 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
3573 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3574 g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
3576 if (gtk_combo_box_get_active_iter (combo_box, &iter))
3577 gtk_tree_model_get (combo_box->priv->model, &iter,
3584 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3585 gboolean group_cycling)
3587 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3589 gtk_widget_grab_focus (combo_box->priv->button);
3595 gtk_combo_box_destroy (GtkObject *object)
3597 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3599 combo_box->priv->destroying = 1;
3601 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3603 combo_box->priv->cell_view = NULL;
3605 combo_box->priv->destroying = 0;
3609 gtk_combo_box_finalize (GObject *object)
3611 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3614 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3615 gtk_combo_box_menu_destroy (combo_box);
3617 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3618 gtk_combo_box_list_destroy (combo_box);
3620 if (combo_box->priv->popup_window)
3621 gtk_widget_destroy (combo_box->priv->popup_window);
3623 gtk_combo_box_unset_model (combo_box);
3625 if (combo_box->priv->model)
3626 g_object_unref (combo_box->priv->model);
3628 for (i = combo_box->priv->cells; i; i = i->next)
3630 ComboCellInfo *info = (ComboCellInfo *)i->data;
3631 GSList *list = info->attributes;
3634 info->destroy (info->func_data);
3636 while (list && list->next)
3638 g_free (list->data);
3639 list = list->next->next;
3641 g_slist_free (info->attributes);
3643 g_object_unref (G_OBJECT (info->cell));
3646 g_slist_free (combo_box->priv->cells);
3648 G_OBJECT_CLASS (parent_class)->finalize (object);
3653 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
3655 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3657 return combo_box->priv->add_tearoffs;
3661 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
3662 gboolean add_tearoffs)
3664 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3666 add_tearoffs = add_tearoffs != FALSE;
3668 if (combo_box->priv->add_tearoffs != add_tearoffs)
3670 combo_box->priv->add_tearoffs = add_tearoffs;
3671 gtk_combo_box_check_appearance (combo_box);
3672 gtk_combo_box_relayout (combo_box);
3673 g_object_notify (G_OBJECT (combo_box), "add_tearoffs");