2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtkcombobox.h"
23 #include "gtkbindings.h"
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkcellview.h"
27 #include "gtkcellviewmenuitem.h"
28 #include "gtkeventbox.h"
30 #include "gtkliststore.h"
33 #include "gtktogglebutton.h"
34 #include "gtktreeselection.h"
35 #include "gtkvseparator.h"
36 #include "gtkwindow.h"
38 #include <gdk/gdkkeysyms.h>
40 #include <gobject/gvaluecollector.h>
45 #include "gtkmarshalers.h"
49 /* WELCOME, to THE house of evil code */
51 typedef struct _ComboCellInfo ComboCellInfo;
54 GtkCellRenderer *cell;
57 GtkCellLayoutDataFunc func;
59 GDestroyNotify destroy;
65 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
67 struct _GtkComboBoxPrivate
79 GtkTreeViewColumn *column;
82 GtkWidget *cell_view_frame;
88 GtkWidget *popup_widget;
89 GtkWidget *popup_window;
90 GtkWidget *popup_frame;
100 guint popup_in_progress : 1;
103 /* While debugging this evil code, I have learned that
104 * there are actually 4 modes to this widget, which can
105 * be characterized as follows
107 * 1) menu mode, no child added
110 * cell_view -> GtkCellView, regular child
111 * cell_view_frame -> NULL
112 * button -> GtkToggleButton set_parent to combo
113 * arrow -> GtkArrow set_parent to button
114 * separator -> GtkVSepator set_parent to button
115 * popup_widget -> GtkMenu
116 * popup_window -> NULL
117 * popup_frame -> NULL
119 * 2) menu mode, child added
123 * cell_view_frame -> NULL
124 * button -> GtkToggleButton set_parent to combo
125 * arrow -> GtkArrow, child of button
127 * popup_widget -> GtkMenu
128 * popup_window -> NULL
129 * popup_frame -> NULL
131 * 3) list mode, no child added
133 * tree_view -> GtkTreeView, child of popup_frame
134 * cell_view -> GtkCellView, regular child
135 * cell_view_frame -> GtkFrame, set parent to combo
136 * button -> GtkToggleButton, set_parent to combo
137 * arrow -> GtkArrow, child of button
139 * popup_widget -> tree_view
140 * popup_window -> GtkWindow
141 * popup_frame -> GtkFrame, child of popup_window
143 * 4) list mode, child added
145 * tree_view -> GtkTreeView, child of popup_frame
147 * cell_view_frame -> NULL
148 * button -> GtkToggleButton, set_parent to combo
149 * arrow -> GtkArrow, child of button
151 * popup_widget -> tree_view
152 * popup_window -> GtkWindow
153 * popup_frame -> GtkFrame, child of popup_window
166 PROP_ROW_SPAN_COLUMN,
167 PROP_COLUMN_SPAN_COLUMN,
171 static GtkBinClass *parent_class = NULL;
172 static guint combo_box_signals[LAST_SIGNAL] = {0,};
174 #define BONUS_PADDING 4
178 static void gtk_combo_box_class_init (GtkComboBoxClass *klass);
179 static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface);
180 static void gtk_combo_box_init (GtkComboBox *combo_box);
181 static void gtk_combo_box_finalize (GObject *object);
182 static void gtk_combo_box_destroy (GtkObject *object);
184 static void gtk_combo_box_set_property (GObject *object,
188 static void gtk_combo_box_get_property (GObject *object,
193 static void gtk_combo_box_state_changed (GtkWidget *widget,
194 GtkStateType previous);
195 static void gtk_combo_box_style_set (GtkWidget *widget,
197 static void gtk_combo_box_button_toggled (GtkWidget *widget,
199 static void gtk_combo_box_add (GtkContainer *container,
202 static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
203 GtkCellRenderer *cell);
205 static void gtk_combo_box_menu_show (GtkWidget *menu,
207 static void gtk_combo_box_menu_hide (GtkWidget *menu,
210 static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
212 static void gtk_combo_box_menu_position_below (GtkMenu *menu,
217 static void gtk_combo_box_menu_position_over (GtkMenu *menu,
222 static void gtk_combo_box_menu_position (GtkMenu *menu,
228 static gint gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
230 static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
232 static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
233 static void gtk_combo_box_set_model_internal (GtkComboBox *combo_box);
235 static void gtk_combo_box_size_request (GtkWidget *widget,
236 GtkRequisition *requisition);
237 static void gtk_combo_box_size_allocate (GtkWidget *widget,
238 GtkAllocation *allocation);
239 static void gtk_combo_box_forall (GtkContainer *container,
240 gboolean include_internals,
241 GtkCallback callback,
242 gpointer callback_data);
243 static gboolean gtk_combo_box_expose_event (GtkWidget *widget,
244 GdkEventExpose *event);
245 static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
246 GdkEventScroll *event);
247 static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
250 /* listening to the model */
251 static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
255 static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
258 static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
263 static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
269 static void gtk_combo_box_list_position (GtkComboBox *combo_box,
274 static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
275 static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
277 static void gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box);
279 static gboolean gtk_combo_box_list_button_released (GtkWidget *widget,
280 GdkEventButton *event,
282 static gboolean gtk_combo_box_list_key_press (GtkWidget *widget,
285 static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget,
286 GdkEventButton *event,
289 static void gtk_combo_box_list_row_changed (GtkTreeModel *model,
295 static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
296 gboolean add_childs);
297 static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
298 static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
300 static void gtk_combo_box_item_get_size (GtkComboBox *combo_box,
304 static void gtk_combo_box_relayout_item (GtkComboBox *combo_box,
306 static void gtk_combo_box_relayout (GtkComboBox *combo_box);
308 static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget,
309 GdkEventButton *event,
311 static void gtk_combo_box_menu_item_activate (GtkWidget *item,
313 static void gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
317 static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
320 static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
325 static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
331 static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
332 GtkCellRenderer *cell,
334 static void gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
335 GtkCellRenderer *cell,
337 static void gtk_combo_box_cell_layout_clear (GtkCellLayout *layout);
338 static void gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
339 GtkCellRenderer *cell,
340 const gchar *attribute,
342 static void gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
343 GtkCellRenderer *cell,
344 GtkCellLayoutDataFunc func,
346 GDestroyNotify destroy);
347 static void gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
348 GtkCellRenderer *cell);
349 static void gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
350 GtkCellRenderer *cell,
352 static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
353 gboolean group_cycling);
357 gtk_combo_box_get_type (void)
359 static GType combo_box_type = 0;
363 static const GTypeInfo combo_box_info =
365 sizeof (GtkComboBoxClass),
366 NULL, /* base_init */
367 NULL, /* base_finalize */
368 (GClassInitFunc) gtk_combo_box_class_init,
369 NULL, /* class_finalize */
370 NULL, /* class_data */
371 sizeof (GtkComboBox),
373 (GInstanceInitFunc) gtk_combo_box_init
376 static const GInterfaceInfo cell_layout_info =
378 (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
383 combo_box_type = g_type_register_static (GTK_TYPE_BIN,
388 g_type_add_interface_static (combo_box_type,
389 GTK_TYPE_CELL_LAYOUT,
393 return combo_box_type;
398 gtk_combo_box_class_init (GtkComboBoxClass *klass)
400 GObjectClass *object_class;
401 GtkBindingSet *binding_set;
402 GtkObjectClass *gtk_object_class;
403 GtkContainerClass *container_class;
404 GtkWidgetClass *widget_class;
406 binding_set = gtk_binding_set_by_class (klass);
408 container_class = (GtkContainerClass *)klass;
409 container_class->forall = gtk_combo_box_forall;
410 container_class->add = gtk_combo_box_add;
412 widget_class = (GtkWidgetClass *)klass;
413 widget_class->size_allocate = gtk_combo_box_size_allocate;
414 widget_class->size_request = gtk_combo_box_size_request;
415 widget_class->expose_event = gtk_combo_box_expose_event;
416 widget_class->scroll_event = gtk_combo_box_scroll_event;
417 widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
418 widget_class->style_set = gtk_combo_box_style_set;
419 widget_class->state_changed = gtk_combo_box_state_changed;
421 gtk_object_class = (GtkObjectClass *)klass;
422 gtk_object_class->destroy = gtk_combo_box_destroy;
424 object_class = (GObjectClass *)klass;
425 object_class->finalize = gtk_combo_box_finalize;
426 object_class->set_property = gtk_combo_box_set_property;
427 object_class->get_property = gtk_combo_box_get_property;
429 parent_class = g_type_class_peek_parent (klass);
432 combo_box_signals[CHANGED] =
433 g_signal_new ("changed",
434 G_OBJECT_CLASS_TYPE (klass),
436 G_STRUCT_OFFSET (GtkComboBoxClass, changed),
438 g_cclosure_marshal_VOID__VOID,
442 g_object_class_install_property (object_class,
444 g_param_spec_object ("model",
445 P_("ComboBox model"),
446 P_("The model for the combo box"),
450 g_object_class_install_property (object_class,
452 g_param_spec_int ("wrap_width",
454 P_("Wrap width for layouting the items in a grid"),
460 g_object_class_install_property (object_class,
461 PROP_ROW_SPAN_COLUMN,
462 g_param_spec_int ("row_span_column",
463 P_("Row span column"),
464 P_("TreeModel column containing the row span values"),
470 g_object_class_install_property (object_class,
471 PROP_COLUMN_SPAN_COLUMN,
472 g_param_spec_int ("column_span_column",
473 P_("Column span column"),
474 P_("TreeModel column containing the column span values"),
480 g_object_class_install_property (object_class,
482 g_param_spec_int ("active",
484 P_("The item which is currently active"),
490 gtk_widget_class_install_style_property (widget_class,
491 g_param_spec_boolean ("appears-as-list",
492 P_("Appears as list"),
493 P_("Whether combobox dropdowns should look like lists rather than menus"),
497 g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
501 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
503 iface->pack_start = gtk_combo_box_cell_layout_pack_start;
504 iface->pack_end = gtk_combo_box_cell_layout_pack_end;
505 iface->clear = gtk_combo_box_cell_layout_clear;
506 iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
507 iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
508 iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
509 iface->reorder = gtk_combo_box_cell_layout_reorder;
513 gtk_combo_box_init (GtkComboBox *combo_box)
515 combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
517 combo_box->priv->cell_view = gtk_cell_view_new ();
518 gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
519 gtk_widget_show (combo_box->priv->cell_view);
521 combo_box->priv->width = 0;
522 combo_box->priv->wrap_width = 0;
524 combo_box->priv->active_item = -1;
525 combo_box->priv->col_column = -1;
526 combo_box->priv->row_column = -1;
530 gtk_combo_box_set_property (GObject *object,
535 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
540 gtk_combo_box_set_model (combo_box, g_value_get_object (value));
543 case PROP_WRAP_WIDTH:
544 gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
547 case PROP_ROW_SPAN_COLUMN:
548 gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
551 case PROP_COLUMN_SPAN_COLUMN:
552 gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
556 gtk_combo_box_set_active (combo_box, g_value_get_int (value));
565 gtk_combo_box_get_property (GObject *object,
570 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
575 g_value_set_object (value, combo_box->priv->model);
578 case PROP_WRAP_WIDTH:
579 g_value_set_int (value, combo_box->priv->wrap_width);
582 case PROP_ROW_SPAN_COLUMN:
583 g_value_set_int (value, combo_box->priv->row_column);
586 case PROP_COLUMN_SPAN_COLUMN:
587 g_value_set_int (value, combo_box->priv->col_column);
591 g_value_set_int (value, gtk_combo_box_get_active (combo_box));
595 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
601 gtk_combo_box_state_changed (GtkWidget *widget,
602 GtkStateType previous)
604 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
606 if (GTK_WIDGET_REALIZED (widget))
608 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
609 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
610 &widget->style->base[GTK_WIDGET_STATE (widget)]);
613 gtk_widget_queue_draw (widget);
617 gtk_combo_box_style_set (GtkWidget *widget,
621 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
623 gtk_widget_queue_resize (widget);
625 /* if wrap_width > 0, then we are in grid-mode and forced to use
628 if (combo_box->priv->wrap_width)
631 gtk_widget_style_get (widget,
632 "appears-as-list", &appearance,
635 /* TRUE is windows style */
638 /* Destroy all the menu mode widgets, if they exist. */
639 if (GTK_IS_MENU (combo_box->priv->popup_widget))
640 gtk_combo_box_menu_destroy (combo_box);
642 /* Create the list mode widgets, if they don't already exist. */
643 if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
644 gtk_combo_box_list_setup (combo_box);
648 /* Destroy all the list mode widgets, if they exist. */
649 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
650 gtk_combo_box_list_destroy (combo_box);
652 /* Create the menu mode widgets, if they don't already exist. */
653 if (!GTK_IS_MENU (combo_box->priv->popup_widget))
654 gtk_combo_box_menu_setup (combo_box, TRUE);
657 if (combo_box->priv->tree_view && combo_box->priv->cell_view)
658 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
659 &widget->style->base[GTK_WIDGET_STATE (widget)]);
664 gtk_combo_box_button_toggled (GtkWidget *widget,
667 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
669 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
671 if (!combo_box->priv->popup_in_progress)
672 gtk_combo_box_popup (combo_box);
675 gtk_combo_box_popdown (combo_box);
679 gtk_combo_box_add (GtkContainer *container,
682 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
684 if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
685 gtk_container_remove (container, combo_box->priv->cell_view);
687 (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
689 if (combo_box->priv->cell_view &&
690 widget != combo_box->priv->cell_view)
692 /* since the cell_view was unparented, it's gone now */
693 combo_box->priv->cell_view = NULL;
695 if (!combo_box->priv->tree_view && combo_box->priv->separator)
697 gtk_widget_unparent (combo_box->priv->separator);
698 combo_box->priv->separator = NULL;
700 g_object_ref (G_OBJECT (combo_box->priv->arrow));
701 gtk_widget_unparent (combo_box->priv->arrow);
702 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
703 combo_box->priv->arrow);
704 g_object_unref (G_OBJECT (combo_box->priv->arrow));
706 gtk_widget_queue_resize (GTK_WIDGET (container));
708 else if (combo_box->priv->cell_view_frame)
710 gtk_widget_unparent (combo_box->priv->cell_view_frame);
711 combo_box->priv->cell_view_frame = NULL;
716 static ComboCellInfo *
717 gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
718 GtkCellRenderer *cell)
722 for (i = combo_box->priv->cells; i; i = i->next)
724 ComboCellInfo *info = (ComboCellInfo *)i->data;
726 if (info && info->cell == cell)
734 gtk_combo_box_menu_show (GtkWidget *menu,
737 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
739 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
741 combo_box->priv->popup_in_progress = FALSE;
745 gtk_combo_box_menu_hide (GtkWidget *menu,
748 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
750 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
755 gtk_combo_box_detacher (GtkWidget *widget,
758 GtkComboBox *combo_box;
760 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
762 combo_box = GTK_COMBO_BOX (widget);
763 g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
765 g_signal_handlers_disconnect_by_func (menu,
766 gtk_combo_box_menu_show,
768 g_signal_handlers_disconnect_by_func (menu,
769 gtk_combo_box_menu_hide,
772 combo_box->priv->popup_widget = NULL;
776 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
779 if (GTK_IS_MENU (combo_box->priv->popup_widget))
781 gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
782 combo_box->priv->popup_widget = NULL;
784 else if (combo_box->priv->popup_widget)
786 gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
787 combo_box->priv->popup_widget);
788 g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
789 combo_box->priv->popup_widget = NULL;
792 if (GTK_IS_MENU (popup))
794 if (combo_box->priv->popup_window)
796 gtk_widget_destroy (combo_box->priv->popup_window);
797 combo_box->priv->popup_window = NULL;
798 combo_box->priv->popup_frame = NULL;
801 combo_box->priv->popup_widget = popup;
803 g_signal_connect (popup, "show",
804 G_CALLBACK (gtk_combo_box_menu_show), combo_box);
805 g_signal_connect (popup, "hide",
806 G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
808 gtk_menu_attach_to_widget (GTK_MENU (popup),
809 GTK_WIDGET (combo_box),
810 gtk_combo_box_detacher);
814 if (!combo_box->priv->popup_window)
816 combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
817 gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
818 gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
819 gtk_widget_get_screen (GTK_WIDGET (combo_box)));
821 combo_box->priv->popup_frame = gtk_frame_new (NULL);
822 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
823 GTK_SHADOW_ETCHED_IN);
824 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
825 combo_box->priv->popup_frame);
827 gtk_widget_show (combo_box->priv->popup_frame);
830 gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
832 gtk_widget_show (popup);
833 g_object_ref (G_OBJECT (popup));
834 combo_box->priv->popup_widget = popup;
839 gtk_combo_box_menu_position_below (GtkMenu *menu,
848 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
850 /* FIXME: is using the size request here broken? */
851 child = GTK_BIN (combo_box)->child;
853 gdk_window_get_origin (child->window, &sx, &sy);
855 gtk_widget_size_request (GTK_WIDGET (menu), &req);
857 if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
860 *x = sx + child->allocation.width - req.width;
861 *y = sy + child->allocation.height;
863 if (GTK_WIDGET_NO_WINDOW (child))
865 *x += child->allocation.x;
866 *y += child->allocation.y;
873 gtk_combo_box_menu_position_over (GtkMenu *menu,
879 GtkComboBox *combo_box;
883 GtkRequisition requisition;
890 g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
892 combo_box = GTK_COMBO_BOX (user_data);
893 widget = GTK_WIDGET (combo_box);
895 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
896 menu_width = requisition.width;
898 active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
899 gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
901 menu_xpos += widget->allocation.x;
902 menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
906 gtk_widget_get_child_requisition (active, &requisition);
907 menu_ypos -= requisition.height / 2;
910 children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
913 child = children->data;
918 if (GTK_WIDGET_VISIBLE (child))
920 gtk_widget_get_child_requisition (child, &requisition);
921 menu_ypos -= requisition.height;
924 children = children->next;
927 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
928 menu_xpos = menu_xpos + widget->allocation.width - menu_width;
930 /* Clamp the position on screen */
931 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
935 else if ((menu_xpos + menu_width) > screen_width)
936 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
945 gtk_combo_box_menu_position (GtkMenu *menu,
951 GtkComboBox *combo_box;
953 combo_box = GTK_COMBO_BOX (user_data);
955 if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
956 gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
958 gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
963 gtk_combo_box_list_position (GtkComboBox *combo_box,
972 GdkRectangle monitor;
973 GtkRequisition popup_req;
975 sample = GTK_BIN (combo_box)->child;
977 *width = sample->allocation.width;
978 gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
979 *height = popup_req.height;
981 gdk_window_get_origin (sample->window, x, y);
983 if (combo_box->priv->cell_view_frame)
985 *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
986 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
987 *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
988 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
991 if (GTK_WIDGET_NO_WINDOW (sample))
993 *x += sample->allocation.x;
994 *y += sample->allocation.y;
997 screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
998 monitor_num = gdk_screen_get_monitor_at_window (screen,
999 GTK_WIDGET (combo_box)->window);
1000 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1004 else if (*x + *width > monitor.x + monitor.width)
1005 *x = monitor.x + monitor.width - *width;
1007 if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1008 *y += sample->allocation.height;
1014 * gtk_combo_box_popup:
1015 * @combo_box: a #GtkComboBox
1017 * Pops up the menu or dropdown list of @combo_box.
1019 * This function is mostly intended for use by accessibility technologies;
1020 * applications should have little use for it.
1025 gtk_combo_box_popup (GtkComboBox *combo_box)
1027 gint x, y, width, height;
1029 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1031 if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1034 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1036 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1037 combo_box->priv->active_item);
1039 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1041 gtk_combo_box_menu_position, combo_box,
1046 gtk_widget_show_all (combo_box->priv->popup_frame);
1047 gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1049 gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
1050 gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1053 gtk_widget_show (combo_box->priv->popup_window);
1055 gtk_widget_grab_focus (combo_box->priv->popup_window);
1056 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1059 if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1061 gdk_keyboard_grab (combo_box->priv->popup_window->window,
1062 FALSE, GDK_CURRENT_TIME);
1063 gtk_widget_grab_focus (combo_box->priv->tree_view);
1066 gtk_grab_add (combo_box->priv->popup_window);
1067 gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1068 GDK_BUTTON_PRESS_MASK |
1069 GDK_BUTTON_RELEASE_MASK |
1070 GDK_POINTER_MOTION_MASK,
1071 NULL, NULL, GDK_CURRENT_TIME);
1073 gtk_grab_add (combo_box->priv->tree_view);
1077 * gtk_combo_box_popdown:
1078 * @combo_box: a #GtkComboBox
1080 * Hides the menu or dropdown list of @combo_box.
1082 * This function is mostly intended for use by accessibility technologies;
1083 * applications should have little use for it.
1088 gtk_combo_box_popdown (GtkComboBox *combo_box)
1090 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1092 if (GTK_IS_MENU (combo_box->priv->popup_widget))
1094 gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1098 gtk_combo_box_list_remove_grabs (combo_box);
1099 gtk_widget_hide_all (combo_box->priv->popup_window);
1100 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1105 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1111 if (combo_box->priv->cell_view)
1112 gtk_widget_style_get (combo_box->priv->cell_view,
1113 "focus-line-width", &padding,
1118 /* add some pixels for good measure */
1119 padding += BONUS_PADDING;
1121 if (combo_box->priv->cell_view)
1122 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1127 return req.width + padding;
1131 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1137 if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1140 combo_box->priv->width = 0;
1142 path = gtk_tree_path_new_from_indices (0, -1);
1144 if (combo_box->priv->cell_view)
1145 gtk_widget_style_get (combo_box->priv->cell_view,
1146 "focus-line-width", &padding,
1151 /* add some pixels for good measure */
1152 padding += BONUS_PADDING;
1158 if (combo_box->priv->cell_view)
1159 gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1164 combo_box->priv->width = MAX (combo_box->priv->width,
1165 req.width + padding);
1167 gtk_tree_path_next (path);
1169 while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1171 gtk_tree_path_free (path);
1175 gtk_combo_box_size_request (GtkWidget *widget,
1176 GtkRequisition *requisition)
1179 GtkRequisition bin_req;
1181 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1184 gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1185 gtk_combo_box_remeasure (combo_box);
1186 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1188 if (!combo_box->priv->tree_view)
1192 if (combo_box->priv->cell_view)
1194 GtkRequisition sep_req, arrow_req;
1195 gint border_width, xthickness, ythickness;
1197 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1198 xthickness = combo_box->priv->button->style->xthickness;
1199 ythickness = combo_box->priv->button->style->ythickness;
1201 bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1203 gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1204 gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1206 height = MAX (sep_req.height, arrow_req.height);
1207 height = MAX (height, bin_req.height);
1209 width = bin_req.width + sep_req.width + arrow_req.width;
1211 height += border_width + 1 + xthickness * 2 + 4;
1212 width += border_width + 1 + ythickness * 2 + 4;
1214 requisition->width = width;
1215 requisition->height = height;
1219 GtkRequisition but_req;
1221 gtk_widget_size_request (combo_box->priv->button, &but_req);
1223 requisition->width = bin_req.width + but_req.width;
1224 requisition->height = MAX (bin_req.height, but_req.height);
1230 GtkRequisition button_req;
1232 /* sample + frame */
1233 *requisition = bin_req;
1235 if (combo_box->priv->cell_view_frame)
1237 requisition->width += 2 *
1238 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1239 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1240 requisition->height += 2 *
1241 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1242 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1246 gtk_widget_size_request (combo_box->priv->button, &button_req);
1248 requisition->height = MAX (requisition->height, button_req.height);
1249 requisition->width += button_req.width;
1254 gtk_combo_box_size_allocate (GtkWidget *widget,
1255 GtkAllocation *allocation)
1257 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1258 GtkAllocation child;
1260 gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1262 widget->allocation = *allocation;
1264 if (!combo_box->priv->tree_view)
1266 if (combo_box->priv->cell_view)
1268 gint border_width, xthickness, ythickness;
1272 gtk_widget_size_allocate (combo_box->priv->button, allocation);
1274 /* set some things ready */
1275 border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1276 xthickness = combo_box->priv->button->style->xthickness;
1277 ythickness = combo_box->priv->button->style->ythickness;
1279 child.x = allocation->x + border_width + 1 + xthickness + 2;
1280 child.y = allocation->y + border_width + 1 + ythickness + 2;
1282 width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1284 /* handle the childs */
1285 gtk_widget_size_request (combo_box->priv->arrow, &req);
1286 child.width = req.width;
1287 child.height = allocation->height - 2 * (child.y - allocation->y);
1289 child.x += width - req.width;
1290 gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1292 child.x += req.width;
1293 gtk_widget_size_request (combo_box->priv->separator, &req);
1294 child.width = req.width;
1296 child.x -= req.width;
1297 gtk_widget_size_allocate (combo_box->priv->separator, &child);
1301 child.x += req.width;
1302 child.width = allocation->x + allocation->width
1303 - (border_width + 1 + xthickness + 2) - child.x;
1307 child.width = child.x;
1308 child.x = allocation->x + border_width + 1 + xthickness + 2;
1309 child.width -= child.x;
1312 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1316 gtk_widget_size_request (combo_box->priv->button, &req);
1318 child.x = allocation->x;
1320 child.x = allocation->x + allocation->width - req.width;
1321 child.y = allocation->y;
1322 child.width = req.width;
1323 child.height = allocation->height;
1324 gtk_widget_size_allocate (combo_box->priv->button, &child);
1327 child.x = allocation->x + req.width;
1329 child.x = allocation->x;
1330 child.y = allocation->y;
1331 child.width = allocation->width - req.width;
1332 gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1340 gtk_widget_size_request (combo_box->priv->button, &req);
1342 child.x = allocation->x;
1344 child.x = allocation->x + allocation->width - req.width;
1345 child.y = allocation->y;
1346 child.width = req.width;
1347 child.height = allocation->height;
1348 gtk_widget_size_allocate (combo_box->priv->button, &child);
1352 child.x = allocation->x + req.width;
1354 child.x = allocation->x;
1355 child.y = allocation->y;
1356 child.width = allocation->width - req.width;
1357 child.height = allocation->height;
1359 if (combo_box->priv->cell_view_frame)
1361 gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1365 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1366 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1368 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1369 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1370 child.width -= 2 * (
1371 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1372 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1373 child.height -= 2 * (
1374 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1375 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1378 gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1383 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1385 if (combo_box->priv->inserted_id != -1)
1387 g_signal_handler_disconnect (combo_box->priv->model,
1388 combo_box->priv->inserted_id);
1389 combo_box->priv->inserted_id = -1;
1391 if (combo_box->priv->deleted_id != -1)
1393 g_signal_handler_disconnect (combo_box->priv->model,
1394 combo_box->priv->deleted_id);
1395 combo_box->priv->deleted_id = -1;
1397 if (combo_box->priv->reordered_id != -1)
1399 g_signal_handler_disconnect (combo_box->priv->model,
1400 combo_box->priv->reordered_id);
1401 combo_box->priv->reordered_id = -1;
1403 if (combo_box->priv->changed_id != -1)
1405 g_signal_handler_disconnect (combo_box->priv->model,
1406 combo_box->priv->changed_id);
1407 combo_box->priv->changed_id = -1;
1411 if (!combo_box->priv->tree_view)
1413 if (combo_box->priv->popup_widget)
1414 gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1415 (GtkCallback)gtk_widget_destroy, NULL);
1420 gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
1422 combo_box->priv->inserted_id =
1423 g_signal_connect (combo_box->priv->model, "row_inserted",
1424 G_CALLBACK (gtk_combo_box_model_row_inserted),
1426 combo_box->priv->deleted_id =
1427 g_signal_connect (combo_box->priv->model, "row_deleted",
1428 G_CALLBACK (gtk_combo_box_model_row_deleted),
1430 combo_box->priv->reordered_id =
1431 g_signal_connect (combo_box->priv->model, "rows_reordered",
1432 G_CALLBACK (gtk_combo_box_model_rows_reordered),
1434 combo_box->priv->changed_id =
1435 g_signal_connect (combo_box->priv->model, "row_changed",
1436 G_CALLBACK (gtk_combo_box_model_row_changed),
1439 if (combo_box->priv->tree_view)
1442 gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
1443 combo_box->priv->model);
1448 gtk_combo_box_forall (GtkContainer *container,
1449 gboolean include_internals,
1450 GtkCallback callback,
1451 gpointer callback_data)
1453 GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1455 if (include_internals)
1457 if (combo_box->priv->button)
1458 (* callback) (combo_box->priv->button, callback_data);
1459 if (combo_box->priv->separator)
1460 (* callback) (combo_box->priv->separator, callback_data);
1461 if (combo_box->priv->arrow)
1462 (* callback) (combo_box->priv->arrow, callback_data);
1463 if (combo_box->priv->cell_view_frame)
1464 (* callback) (combo_box->priv->cell_view_frame, callback_data);
1467 if (GTK_BIN (container)->child)
1468 (* callback) (GTK_BIN (container)->child, callback_data);
1472 gtk_combo_box_expose_event (GtkWidget *widget,
1473 GdkEventExpose *event)
1475 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1477 if (!combo_box->priv->tree_view)
1479 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1480 combo_box->priv->button, event);
1482 if (combo_box->priv->separator)
1484 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1485 combo_box->priv->separator, event);
1487 /* if not in this case, arrow gets its expose event from button */
1488 gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1489 combo_box->priv->arrow, event);
1494 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1495 combo_box->priv->button, event);
1497 if (combo_box->priv->cell_view_frame)
1498 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1499 combo_box->priv->cell_view_frame, event);
1502 gtk_container_propagate_expose (GTK_CONTAINER (widget),
1503 GTK_BIN (widget)->child, event);
1509 gtk_combo_box_scroll_event (GtkWidget *widget,
1510 GdkEventScroll *event)
1512 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1516 index = gtk_combo_box_get_active (combo_box);
1520 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1522 if (event->direction == GDK_SCROLL_UP)
1527 gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1538 cell_view_sync_cells (GtkComboBox *combo_box,
1539 GtkCellView *cell_view)
1543 for (k = combo_box->priv->cells; k; k = k->next)
1546 ComboCellInfo *info = (ComboCellInfo *)k->data;
1548 if (info->pack == GTK_PACK_START)
1549 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1550 info->cell, info->expand);
1551 else if (info->pack == GTK_PACK_END)
1552 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1553 info->cell, info->expand);
1555 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1557 info->func, info->func_data, NULL);
1559 for (j = info->attributes; j; j = j->next->next)
1561 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1564 GPOINTER_TO_INT (j->next->data));
1570 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1571 gboolean add_childs)
1575 /* Unset any existing model. */
1576 gtk_combo_box_unset_model (combo_box);
1578 if (combo_box->priv->cell_view)
1580 combo_box->priv->button = gtk_toggle_button_new ();
1581 g_signal_connect (combo_box->priv->button, "toggled",
1582 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1583 gtk_widget_set_parent (combo_box->priv->button,
1584 GTK_BIN (combo_box)->child->parent);
1586 combo_box->priv->separator = gtk_vseparator_new ();
1587 gtk_widget_set_parent (combo_box->priv->separator,
1588 combo_box->priv->button);
1589 gtk_widget_show (combo_box->priv->separator);
1591 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1592 gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1593 gtk_widget_show (combo_box->priv->arrow);
1595 gtk_widget_show_all (combo_box->priv->button);
1597 if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1599 /* I have no clue why, but we need to manually map in this case. */
1600 gtk_widget_map (combo_box->priv->separator);
1601 gtk_widget_map (combo_box->priv->arrow);
1606 combo_box->priv->button = gtk_toggle_button_new ();
1607 g_signal_connect (combo_box->priv->button, "toggled",
1608 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1609 gtk_widget_set_parent (combo_box->priv->button,
1610 GTK_BIN (combo_box)->child->parent);
1612 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1613 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1614 combo_box->priv->arrow);
1615 gtk_widget_show_all (combo_box->priv->button);
1618 g_signal_connect (combo_box->priv->button, "button_press_event",
1619 G_CALLBACK (gtk_combo_box_menu_button_press),
1622 /* create our funky menu */
1623 box = gtk_menu_new ();
1624 gtk_combo_box_set_popup_widget (combo_box, box);
1626 /* set the models */
1627 gtk_combo_box_set_model_internal (combo_box);
1631 gtk_combo_box_menu_fill (combo_box);
1636 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1642 if (!combo_box->priv->model)
1645 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1646 menu = combo_box->priv->popup_widget;
1648 for (i = 0; i < items; i++)
1652 path = gtk_tree_path_new_from_indices (i, -1);
1653 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1655 g_signal_connect (tmp, "activate",
1656 G_CALLBACK (gtk_combo_box_menu_item_activate),
1659 cell_view_sync_cells (combo_box,
1660 GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1662 gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1663 gtk_widget_show (tmp);
1665 gtk_tree_path_free (path);
1670 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1672 /* disconnect signal handlers */
1673 gtk_combo_box_unset_model (combo_box);
1675 g_signal_handlers_disconnect_matched (combo_box->priv->button,
1676 G_SIGNAL_MATCH_DATA,
1678 gtk_combo_box_menu_button_press, NULL);
1680 /* unparent will remove our latest ref */
1681 if (combo_box->priv->cell_view)
1683 gtk_widget_unparent (combo_box->priv->arrow);
1684 combo_box->priv->arrow = NULL;
1686 gtk_widget_unparent (combo_box->priv->separator);
1687 combo_box->priv->separator = NULL;
1689 gtk_widget_unparent (combo_box->priv->button);
1690 combo_box->priv->button = NULL;
1694 /* will destroy the arrow too */
1695 gtk_widget_unparent (combo_box->priv->button);
1697 combo_box->priv->button = NULL;
1698 combo_box->priv->arrow = NULL;
1701 /* changing the popup window will unref the menu and the childs */
1709 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1716 gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1720 if (combo_box->priv->col_column == -1)
1723 gtk_tree_model_get (combo_box->priv->model, &iter,
1724 combo_box->priv->col_column, cols,
1730 if (combo_box->priv->row_column == -1)
1733 gtk_tree_model_get (combo_box->priv->model, &iter,
1734 combo_box->priv->row_column, rows,
1740 menu_occupied (GtkMenu *menu,
1744 guint bottom_attach)
1748 g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1749 g_return_val_if_fail (left_attach < right_attach, TRUE);
1750 g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1752 for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1755 gboolean h_intersect = FALSE;
1756 gboolean v_intersect = FALSE;
1758 gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1761 "bottom_attach", &b,
1765 /* look if this item intersects with the given coordinates */
1766 h_intersect = left_attach <= l && l <= right_attach;
1767 h_intersect &= left_attach <= r && r <= right_attach;
1769 v_intersect = top_attach <= t && t <= bottom_attach;
1770 v_intersect &= top_attach <= b && b <= bottom_attach;
1772 if (h_intersect && v_intersect)
1780 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1783 gint current_col = 0, current_row = 0;
1789 menu = combo_box->priv->popup_widget;
1790 if (!GTK_IS_MENU_SHELL (menu))
1793 list = gtk_container_get_children (GTK_CONTAINER (menu));
1794 item = g_list_nth_data (list, index);
1797 gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1799 /* look for a good spot */
1802 if (current_col + cols > combo_box->priv->wrap_width)
1808 if (!menu_occupied (GTK_MENU (menu),
1809 current_col, current_col + cols,
1810 current_row, current_row + rows))
1816 /* set attach props */
1817 gtk_menu_attach (GTK_MENU (menu), item,
1818 current_col, current_col + cols,
1819 current_row, current_row + rows);
1823 gtk_combo_box_relayout (GtkComboBox *combo_box)
1829 /* ensure we are in menu style */
1830 if (combo_box->priv->tree_view)
1831 gtk_combo_box_list_destroy (combo_box);
1833 menu = combo_box->priv->popup_widget;
1835 if (!GTK_IS_MENU_SHELL (menu))
1837 gtk_combo_box_menu_setup (combo_box, FALSE);
1838 menu = combo_box->priv->popup_widget;
1841 /* get rid of all children */
1842 g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1844 list = gtk_container_get_children (GTK_CONTAINER (menu));
1846 for (j = g_list_last (list); j; j = j->prev)
1847 gtk_container_remove (GTK_CONTAINER (menu), j->data);
1852 items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1854 for (i = 0; i < items; i++)
1859 path = gtk_tree_path_new_from_indices (i, -1);
1860 tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1863 g_signal_connect (tmp, "activate",
1864 G_CALLBACK (gtk_combo_box_menu_item_activate),
1867 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1869 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
1871 if (combo_box->priv->wrap_width)
1872 gtk_combo_box_relayout_item (combo_box, i);
1874 gtk_widget_show (tmp);
1876 gtk_tree_path_free (path);
1882 gtk_combo_box_menu_button_press (GtkWidget *widget,
1883 GdkEventButton *event,
1886 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1888 if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1891 if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1893 combo_box->priv->popup_in_progress = TRUE;
1895 gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1896 combo_box->priv->active_item);
1897 gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1899 gtk_combo_box_menu_position, combo_box,
1900 event->button, event->time);
1909 gtk_combo_box_menu_item_activate (GtkWidget *item,
1914 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1916 menu = combo_box->priv->popup_widget;
1917 g_return_if_fail (GTK_IS_MENU (menu));
1919 index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1921 gtk_combo_box_set_active (combo_box, index);
1925 gtk_combo_box_model_row_inserted (GtkTreeModel *model,
1930 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1931 gint index = gtk_tree_path_get_indices (path)[0];
1932 gint items = gtk_tree_model_iter_n_children (model, NULL);
1934 if (combo_box->priv->active_item >= index)
1935 combo_box->priv->active_item++;
1937 if (!combo_box->priv->tree_view)
1938 gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1941 gtk_combo_box_set_active (combo_box, 0);
1945 gtk_combo_box_model_row_deleted (GtkTreeModel *model,
1949 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1950 gint index = gtk_tree_path_get_indices (path)[0];
1952 if (!combo_box->priv->tree_view)
1953 gtk_combo_box_menu_row_deleted (model, path, user_data);
1955 if (index == combo_box->priv->active_item)
1957 gint items = gtk_tree_model_iter_n_children (model, NULL);
1960 gtk_combo_box_set_active_internal (combo_box, -1);
1961 else if (index == items)
1962 gtk_combo_box_set_active_internal (combo_box, index - 1);
1964 gtk_combo_box_set_active_internal (combo_box, index);
1966 else if (combo_box->priv->active_item > index)
1967 combo_box->priv->active_item--;
1971 gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
1977 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1978 gint items = gtk_tree_model_iter_n_children (model, NULL);
1981 for (i = 0; i < items; i++)
1982 if (new_order[i] == combo_box->priv->active_item)
1984 combo_box->priv->active_item = i;
1988 if (!combo_box->priv->tree_view)
1989 gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
1993 gtk_combo_box_model_row_changed (GtkTreeModel *model,
1998 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1999 gint index = gtk_tree_path_get_indices (path)[0];
2001 if (index == combo_box->priv->active_item &&
2002 combo_box->priv->cell_view)
2003 gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2005 if (combo_box->priv->tree_view)
2006 gtk_combo_box_list_row_changed (model, path, iter, user_data);
2008 gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2013 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2020 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2022 if (!combo_box->priv->popup_widget)
2025 menu = combo_box->priv->popup_widget;
2026 g_return_if_fail (GTK_IS_MENU (menu));
2028 item = gtk_cell_view_menu_item_new_from_model (model, path);
2029 g_signal_connect (item, "activate",
2030 G_CALLBACK (gtk_combo_box_menu_item_activate),
2033 cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2035 gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2036 gtk_tree_path_get_indices (path)[0]);
2037 gtk_widget_show (item);
2041 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2048 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2050 if (!combo_box->priv->popup_widget)
2053 index = gtk_tree_path_get_indices (path)[0];
2055 menu = combo_box->priv->popup_widget;
2056 g_return_if_fail (GTK_IS_MENU (menu));
2058 item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2059 g_return_if_fail (GTK_IS_MENU_ITEM (item));
2061 gtk_container_remove (GTK_CONTAINER (menu), item);
2065 gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
2071 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2073 gtk_combo_box_relayout (combo_box);
2077 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2082 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2085 if (!combo_box->priv->popup_widget)
2088 if (combo_box->priv->wrap_width)
2089 gtk_combo_box_relayout_item (combo_box,
2090 gtk_tree_path_get_indices (path)[0]);
2092 width = gtk_combo_box_calc_requested_width (combo_box, path);
2094 if (width > combo_box->priv->width)
2096 if (combo_box->priv->cell_view)
2098 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2099 gtk_widget_queue_resize (combo_box->priv->cell_view);
2101 combo_box->priv->width = width;
2110 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2113 GtkTreeSelection *sel;
2115 /* Unset any existing model. */
2116 gtk_combo_box_unset_model (combo_box);
2118 combo_box->priv->button = gtk_toggle_button_new ();
2119 gtk_widget_set_parent (combo_box->priv->button,
2120 GTK_BIN (combo_box)->child->parent);
2121 g_signal_connect (combo_box->priv->button, "button_press_event",
2122 G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2123 g_signal_connect (combo_box->priv->button, "toggled",
2124 G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2126 combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2127 gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2128 combo_box->priv->arrow);
2129 combo_box->priv->separator = NULL;
2130 gtk_widget_show_all (combo_box->priv->button);
2132 if (combo_box->priv->cell_view)
2134 combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2135 gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2136 GTK_BIN (combo_box)->child->parent);
2137 gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2140 gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
2141 >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2143 gtk_widget_show (combo_box->priv->cell_view_frame);
2146 combo_box->priv->tree_view = gtk_tree_view_new ();
2147 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2148 gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2149 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2152 g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2153 G_CALLBACK (gtk_combo_box_list_button_pressed),
2155 g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2156 G_CALLBACK (gtk_combo_box_list_button_released),
2158 g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2159 G_CALLBACK (gtk_combo_box_list_key_press),
2162 combo_box->priv->column = gtk_tree_view_column_new ();
2163 gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2164 combo_box->priv->column);
2166 /* set the models */
2167 gtk_combo_box_set_model_internal (combo_box);
2170 for (i = combo_box->priv->cells; i; i = i->next)
2173 ComboCellInfo *info = (ComboCellInfo *)i->data;
2175 if (info->pack == GTK_PACK_START)
2176 gtk_tree_view_column_pack_start (combo_box->priv->column,
2177 info->cell, info->expand);
2178 else if (info->pack == GTK_PACK_END)
2179 gtk_tree_view_column_pack_end (combo_box->priv->column,
2180 info->cell, info->expand);
2182 for (j = info->attributes; j; j = j->next->next)
2184 gtk_tree_view_column_add_attribute (combo_box->priv->column,
2187 GPOINTER_TO_INT (j->next->data));
2191 if (combo_box->priv->active_item != -1)
2195 path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2198 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2200 gtk_tree_path_free (path);
2204 /* set sample/popup widgets */
2205 gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2207 gtk_widget_show (combo_box->priv->tree_view);
2211 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2213 /* disconnect signals */
2214 gtk_combo_box_unset_model (combo_box);
2216 g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2217 G_SIGNAL_MATCH_DATA,
2218 0, 0, NULL, NULL, combo_box);
2219 g_signal_handlers_disconnect_matched (combo_box->priv->button,
2220 G_SIGNAL_MATCH_DATA,
2222 gtk_combo_box_list_button_pressed,
2225 /* destroy things (unparent will kill the latest ref from us)
2226 * last unref on button will destroy the arrow
2228 gtk_widget_unparent (combo_box->priv->button);
2229 combo_box->priv->button = NULL;
2230 combo_box->priv->arrow = NULL;
2232 if (combo_box->priv->cell_view)
2234 g_object_set (G_OBJECT (combo_box->priv->cell_view),
2235 "background_set", FALSE,
2238 gtk_widget_unparent (combo_box->priv->cell_view_frame);
2239 combo_box->priv->cell_view_frame = NULL;
2242 gtk_widget_destroy (combo_box->priv->tree_view);
2244 combo_box->priv->tree_view = NULL;
2245 combo_box->priv->popup_widget = NULL;
2250 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2252 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2253 gtk_grab_remove (combo_box->priv->tree_view);
2255 if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2257 gtk_grab_remove (combo_box->priv->popup_window);
2258 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2259 gdk_pointer_ungrab (GDK_CURRENT_TIME);
2264 gtk_combo_box_list_button_pressed (GtkWidget *widget,
2265 GdkEventButton *event,
2268 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2270 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2272 if (ewidget == combo_box->priv->tree_view)
2275 if ((ewidget != combo_box->priv->button) ||
2276 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2279 gtk_combo_box_popup (combo_box);
2281 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2284 combo_box->priv->popup_in_progress = TRUE;
2290 gtk_combo_box_list_button_released (GtkWidget *widget,
2291 GdkEventButton *event,
2295 GtkTreePath *path = NULL;
2297 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2299 gboolean popup_in_progress = FALSE;
2301 GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2303 if (combo_box->priv->popup_in_progress)
2305 popup_in_progress = TRUE;
2306 combo_box->priv->popup_in_progress = FALSE;
2309 if (ewidget != combo_box->priv->tree_view)
2311 if (ewidget == combo_box->priv->button &&
2312 !popup_in_progress &&
2313 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2315 gtk_combo_box_popdown (combo_box);
2319 /* released outside treeview */
2320 if (ewidget != combo_box->priv->button)
2322 gtk_combo_box_popdown (combo_box);
2330 /* select something cool */
2331 ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2337 return TRUE; /* clicked outside window? */
2339 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2340 gtk_combo_box_popdown (combo_box);
2342 gtk_tree_path_free (path);
2348 gtk_combo_box_list_key_press (GtkWidget *widget,
2352 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2354 if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2355 event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
2356 event->keyval == GDK_Escape)
2358 if (event->keyval != GDK_Escape)
2362 GtkTreeModel *model = NULL;
2363 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2365 ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2370 path = gtk_tree_model_get_path (model, &iter);
2373 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2374 gtk_tree_path_free (path);
2379 /* reset active item -- this is incredibly lame and ugly */
2380 gtk_combo_box_set_active (combo_box,
2381 gtk_combo_box_get_active (combo_box));
2383 gtk_combo_box_popdown (combo_box);
2392 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2397 GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2400 width = gtk_combo_box_calc_requested_width (combo_box, path);
2402 if (width > combo_box->priv->width)
2404 if (combo_box->priv->cell_view)
2406 gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2407 gtk_widget_queue_resize (combo_box->priv->cell_view);
2409 combo_box->priv->width = width;
2414 * GtkCellLayout implementation
2417 gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
2418 GtkCellRenderer *cell,
2421 ComboCellInfo *info;
2422 GtkComboBox *combo_box;
2425 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2426 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2428 combo_box = GTK_COMBO_BOX (layout);
2430 g_object_ref (G_OBJECT (cell));
2431 gtk_object_sink (GTK_OBJECT (cell));
2433 info = g_new0 (ComboCellInfo, 1);
2435 info->expand = expand;
2436 info->pack = GTK_PACK_START;
2438 combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2440 if (combo_box->priv->cell_view)
2441 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2444 if (combo_box->priv->column)
2445 gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2447 menu = combo_box->priv->popup_widget;
2448 if (GTK_IS_MENU (menu))
2452 list = gtk_container_get_children (GTK_CONTAINER (menu));
2453 for (i = list; i; i = i->next)
2457 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2458 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2460 view = GTK_CELL_VIEW (i->data);
2462 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2469 gtk_combo_box_cell_layout_pack_end (GtkCellLayout *layout,
2470 GtkCellRenderer *cell,
2473 ComboCellInfo *info;
2474 GtkComboBox *combo_box;
2477 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2478 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2480 combo_box = GTK_COMBO_BOX (layout);
2482 g_object_ref (G_OBJECT (cell));
2483 gtk_object_sink (GTK_OBJECT (cell));
2485 info = g_new0 (ComboCellInfo, 1);
2487 info->expand = expand;
2488 info->pack = GTK_PACK_END;
2490 if (combo_box->priv->cell_view)
2491 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2494 if (combo_box->priv->column)
2495 gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2497 menu = combo_box->priv->popup_widget;
2498 if (GTK_IS_MENU (menu))
2502 list = gtk_container_get_children (GTK_CONTAINER (menu));
2503 for (i = list; i; i = i->next)
2507 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2508 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2510 view = GTK_CELL_VIEW (i->data);
2512 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2519 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2522 GtkComboBox *combo_box;
2525 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2527 combo_box = GTK_COMBO_BOX (layout);
2529 if (combo_box->priv->cell_view)
2530 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2532 if (combo_box->priv->column)
2533 gtk_tree_view_column_clear (combo_box->priv->column);
2535 for (i = combo_box->priv->cells; i; i = i->next)
2537 ComboCellInfo *info = (ComboCellInfo *)i->data;
2539 gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2540 g_object_unref (G_OBJECT (info->cell));
2544 g_slist_free (combo_box->priv->cells);
2545 combo_box->priv->cells = NULL;
2547 menu = combo_box->priv->popup_widget;
2548 if (GTK_IS_MENU (menu))
2552 list = gtk_container_get_children (GTK_CONTAINER (menu));
2553 for (i = list; i; i = i->next)
2557 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2558 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2560 view = GTK_CELL_VIEW (i->data);
2562 gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2569 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout *layout,
2570 GtkCellRenderer *cell,
2571 const gchar *attribute,
2574 ComboCellInfo *info;
2575 GtkComboBox *combo_box;
2578 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2579 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2581 combo_box = GTK_COMBO_BOX (layout);
2583 info = gtk_combo_box_get_cell_info (combo_box, cell);
2585 info->attributes = g_slist_prepend (info->attributes,
2586 GINT_TO_POINTER (column));
2587 info->attributes = g_slist_prepend (info->attributes,
2588 g_strdup (attribute));
2590 if (combo_box->priv->cell_view)
2591 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2592 cell, attribute, column);
2594 if (combo_box->priv->column)
2595 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2596 cell, attribute, column);
2598 menu = combo_box->priv->popup_widget;
2599 if (GTK_IS_MENU (menu))
2603 list = gtk_container_get_children (GTK_CONTAINER (menu));
2604 for (i = list; i; i = i->next)
2608 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2609 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2611 view = GTK_CELL_VIEW (i->data);
2613 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2619 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2623 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout *layout,
2624 GtkCellRenderer *cell,
2625 GtkCellLayoutDataFunc func,
2627 GDestroyNotify destroy)
2629 ComboCellInfo *info;
2630 GtkComboBox *combo_box;
2633 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2635 combo_box = GTK_COMBO_BOX (layout);
2637 info = gtk_combo_box_get_cell_info (combo_box, cell);
2638 g_return_if_fail (info != NULL);
2642 GDestroyNotify d = info->destroy;
2644 info->destroy = NULL;
2645 d (info->func_data);
2649 info->func_data = func_data;
2650 info->destroy = destroy;
2652 if (combo_box->priv->cell_view)
2653 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2655 if (combo_box->priv->column)
2656 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2658 menu = combo_box->priv->popup_widget;
2659 if (GTK_IS_MENU (menu))
2663 list = gtk_container_get_children (GTK_CONTAINER (menu));
2664 for (i = list; i; i = i->next)
2668 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2669 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2671 view = GTK_CELL_VIEW (i->data);
2673 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2674 func, func_data, NULL);
2679 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2683 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout *layout,
2684 GtkCellRenderer *cell)
2686 ComboCellInfo *info;
2687 GtkComboBox *combo_box;
2691 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2692 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2694 combo_box = GTK_COMBO_BOX (layout);
2696 info = gtk_combo_box_get_cell_info (combo_box, cell);
2697 g_return_if_fail (info != NULL);
2699 list = info->attributes;
2700 while (list && list->next)
2702 g_free (list->data);
2703 list = list->next->next;
2705 g_slist_free (info->attributes);
2706 info->attributes = NULL;
2708 if (combo_box->priv->cell_view)
2709 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2711 if (combo_box->priv->column)
2712 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2714 menu = combo_box->priv->popup_widget;
2715 if (GTK_IS_MENU (menu))
2719 list = gtk_container_get_children (GTK_CONTAINER (menu));
2720 for (i = list; i; i = i->next)
2724 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2725 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2727 view = GTK_CELL_VIEW (i->data);
2729 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2734 gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2738 gtk_combo_box_cell_layout_reorder (GtkCellLayout *layout,
2739 GtkCellRenderer *cell,
2742 ComboCellInfo *info;
2743 GtkComboBox *combo_box;
2747 g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2748 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2750 combo_box = GTK_COMBO_BOX (layout);
2752 info = gtk_combo_box_get_cell_info (combo_box, cell);
2754 g_return_if_fail (info != NULL);
2755 g_return_if_fail (position >= 0);
2757 link = g_slist_find (combo_box->priv->cells, info);
2759 g_return_if_fail (link != NULL);
2761 combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2762 combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2765 if (combo_box->priv->cell_view)
2766 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2769 if (combo_box->priv->column)
2770 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2773 menu = combo_box->priv->popup_widget;
2774 if (GTK_IS_MENU (menu))
2778 list = gtk_container_get_children (GTK_CONTAINER (menu));
2779 for (i = list; i; i = i->next)
2783 if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2784 view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2786 view = GTK_CELL_VIEW (i->data);
2788 gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2793 gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2801 * gtk_combo_box_new:
2803 * Creates a new empty #GtkComboBox.
2805 * Return value: A new #GtkComboBox.
2810 gtk_combo_box_new (void)
2812 return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2816 * gtk_combo_box_new_with_model:
2817 * @model: A #GtkTreeModel.
2819 * Creates a new #GtkComboBox with the model initialized to @model.
2821 * Return value: A new #GtkComboBox.
2826 gtk_combo_box_new_with_model (GtkTreeModel *model)
2828 GtkComboBox *combo_box;
2830 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2832 combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2836 return GTK_WIDGET (combo_box);
2840 * gtk_combo_box_set_wrap_width:
2841 * @combo_box: A #GtkComboBox.
2842 * @width: Preferred number of columns.
2844 * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2845 * the preferred number of columns when you want to the popup to be layed out
2851 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2854 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2855 g_return_if_fail (width >= 0);
2857 if (width != combo_box->priv->wrap_width)
2859 combo_box->priv->wrap_width = width;
2861 gtk_combo_box_relayout (combo_box);
2862 gtk_combo_box_style_set (GTK_WIDGET (combo_box), NULL);
2864 g_object_notify (G_OBJECT (combo_box), "wrap_width");
2869 * gtk_combo_box_set_row_span_column:
2870 * @combo_box: A #GtkComboBox.
2871 * @row_span: A column in the model passed during construction.
2873 * Sets the column with row span information for @combo_box to be @row_span.
2874 * The row span column contains integers which indicate how many rows
2875 * an item should span.
2880 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2885 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2887 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2888 g_return_if_fail (row_span >= 0 && row_span < col);
2890 if (row_span != combo_box->priv->row_column)
2892 combo_box->priv->row_column = row_span;
2894 gtk_combo_box_relayout (combo_box);
2896 g_object_notify (G_OBJECT (combo_box), "row_span_column");
2901 * gtk_combo_box_set_column_span_column:
2902 * @combo_box: A #GtkComboBox.
2903 * @column_span: A column in the model passed during construction.
2905 * Sets the column with column span information for @combo_box to be
2906 * @column_span. The column span column contains integers which indicate
2907 * how many columns an item should span.
2912 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2917 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2919 col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2920 g_return_if_fail (column_span >= 0 && column_span < col);
2922 if (column_span != combo_box->priv->col_column)
2924 combo_box->priv->col_column = column_span;
2926 gtk_combo_box_relayout (combo_box);
2928 g_object_notify (G_OBJECT (combo_box), "column_span_column");
2933 * gtk_combo_box_get_active:
2934 * @combo_box: A #GtkComboBox.
2936 * Returns the index of the currently active item, or -1 if there's no
2939 * Return value: An integer which is the index of the currently active item, or
2940 * -1 if there's no active item.
2945 gtk_combo_box_get_active (GtkComboBox *combo_box)
2947 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
2949 return combo_box->priv->active_item;
2953 * gtk_combo_box_set_active:
2954 * @combo_box: A #GtkComboBox.
2955 * @index: An index in the model passed during construction, or -1 to have
2958 * Sets the active item of @combo_box to be the item at @index.
2963 gtk_combo_box_set_active (GtkComboBox *combo_box,
2966 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2967 /* -1 means "no item selected" */
2968 g_return_if_fail (index >= -1);
2970 if (combo_box->priv->active_item == index)
2973 gtk_combo_box_set_active_internal (combo_box, index);
2977 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
2982 combo_box->priv->active_item = index;
2986 if (combo_box->priv->tree_view)
2987 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
2990 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
2992 if (GTK_IS_MENU (menu))
2993 gtk_menu_set_active (menu, -1);
2996 if (combo_box->priv->cell_view)
2997 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3001 path = gtk_tree_path_new_from_indices (index, -1);
3003 if (combo_box->priv->tree_view)
3004 gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3007 GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3009 if (GTK_IS_MENU (menu))
3010 gtk_menu_set_active (GTK_MENU (menu), index);
3013 if (combo_box->priv->cell_view)
3014 gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3016 gtk_tree_path_free (path);
3019 g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3024 * gtk_combo_box_get_active_iter:
3025 * @combo_box: A #GtkComboBox
3026 * @iter: The uninitialized #GtkTreeIter.
3028 * Sets @iter to point to the current active item, if it exists.
3030 * Return value: %TRUE, if @iter was set
3035 gtk_combo_box_get_active_iter (GtkComboBox *combo_box,
3042 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3044 active = gtk_combo_box_get_active (combo_box);
3048 path = gtk_tree_path_new_from_indices (active, -1);
3049 retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3051 gtk_tree_path_free (path);
3057 * gtk_combo_box_set_active_iter:
3058 * @combo_box: A #GtkComboBox
3059 * @iter: The #GtkTreeIter.
3061 * Sets the current active item to be the one referenced by @iter.
3062 * @iter must correspond to a path of depth one.
3067 gtk_combo_box_set_active_iter (GtkComboBox *combo_box,
3072 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3074 path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3075 g_return_if_fail (path != NULL);
3076 g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3078 gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3079 gtk_tree_path_free (path);
3083 * gtk_combo_box_set_model:
3084 * @combo_box: A #GtkComboBox.
3085 * @model: A #GtkTreeModel.
3087 * Sets the model used by @combo_box to be @model. Will unset a
3088 * previously set model (if applicable).
3093 gtk_combo_box_set_model (GtkComboBox *combo_box,
3094 GtkTreeModel *model)
3096 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3097 g_return_if_fail (GTK_IS_TREE_MODEL (model));
3099 if (combo_box->priv->model)
3101 gtk_combo_box_unset_model (combo_box);
3102 g_object_unref (G_OBJECT (combo_box->priv->model));
3105 combo_box->priv->model = model;
3106 g_object_ref (G_OBJECT (combo_box->priv->model));
3108 gtk_combo_box_set_model_internal (combo_box);
3109 if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
3110 gtk_combo_box_menu_fill (combo_box);
3112 if (combo_box->priv->cell_view)
3113 gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3114 combo_box->priv->model);
3118 * gtk_combo_box_get_model
3119 * @combo_box: A #GtkComboBox.
3121 * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3123 * Return value: A #GtkTreeModel which was passed during construction.
3128 gtk_combo_box_get_model (GtkComboBox *combo_box)
3130 g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3132 return combo_box->priv->model;
3136 /* convenience API for simple text combos */
3139 * gtk_combo_box_new_text:
3141 * Convenience function which constructs a new text combo box, which is a
3142 * #GtkComboBox just displaying strings. If you use this function to create
3143 * a text combo box, you should only manipulate its data source with the
3144 * following convenience functions: gtk_combo_box_append_text(),
3145 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3146 * gtk_combo_box_remove_text().
3148 * Return value: A new text combo box.
3153 gtk_combo_box_new_text (void)
3155 GtkWidget *combo_box;
3156 GtkCellRenderer *cell;
3157 GtkListStore *store;
3159 store = gtk_list_store_new (1, G_TYPE_STRING);
3161 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3163 cell = gtk_cell_renderer_text_new ();
3164 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3165 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3173 * gtk_combo_box_append_text:
3174 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3177 * Appends @string to the list of strings stored in @combo_box. Note that
3178 * you can only use this function with combo boxes constructed with
3179 * gtk_combo_box_new_text().
3184 gtk_combo_box_append_text (GtkComboBox *combo_box,
3188 GtkListStore *store;
3190 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3191 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3192 g_return_if_fail (text != NULL);
3194 store = GTK_LIST_STORE (combo_box->priv->model);
3196 gtk_list_store_append (store, &iter);
3197 gtk_list_store_set (store, &iter, 0, text, -1);
3201 * gtk_combo_box_insert_text:
3202 * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3203 * @position: An index to insert @text.
3206 * Inserts @string at @position in the list of strings stored in @combo_box.
3207 * Note that you can only use this function with combo boxes constructed
3208 * with gtk_combo_box_new_text().
3213 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3218 GtkListStore *store;
3220 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3221 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3222 g_return_if_fail (position >= 0);
3223 g_return_if_fail (text != NULL);
3225 store = GTK_LIST_STORE (combo_box->priv->model);
3227 gtk_list_store_insert (store, &iter, position);
3228 gtk_list_store_set (store, &iter, 0, text, -1);
3232 * gtk_combo_box_prepend_text:
3233 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3236 * Prepends @string to the list of strings stored in @combo_box. Note that
3237 * you can only use this function with combo boxes constructed with
3238 * gtk_combo_box_new_text().
3243 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3247 GtkListStore *store;
3249 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3250 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3251 g_return_if_fail (text != NULL);
3253 store = GTK_LIST_STORE (combo_box->priv->model);
3255 gtk_list_store_prepend (store, &iter);
3256 gtk_list_store_set (store, &iter, 0, text, -1);
3260 * gtk_combo_box_remove_text:
3261 * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3262 * @position: Index of the item to remove.
3264 * Removes the string at @position from @combo_box. Note that you can only use
3265 * this function with combo boxes constructed with gtk_combo_box_new_text().
3270 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3274 GtkListStore *store;
3276 g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3277 g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3278 g_return_if_fail (position >= 0);
3280 store = GTK_LIST_STORE (combo_box->priv->model);
3282 if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3284 gtk_list_store_remove (store, &iter);
3289 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3290 gboolean group_cycling)
3292 GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3294 gtk_widget_grab_focus (combo_box->priv->button);
3300 gtk_combo_box_destroy (GtkObject *object)
3302 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3304 GTK_OBJECT_CLASS (parent_class)->destroy (object);
3306 combo_box->priv->cell_view = NULL;
3310 gtk_combo_box_finalize (GObject *object)
3312 GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3315 gtk_combo_box_unset_model (combo_box);
3317 if (GTK_IS_MENU (combo_box->priv->popup_widget))
3318 gtk_combo_box_menu_destroy (combo_box);
3320 if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3321 gtk_combo_box_list_destroy (combo_box);
3323 if (combo_box->priv->popup_window)
3324 gtk_widget_destroy (combo_box->priv->popup_window);
3326 if (combo_box->priv->model)
3327 g_object_unref (combo_box->priv->model);
3329 for (i = combo_box->priv->cells; i; i = i->next)
3331 ComboCellInfo *info = (ComboCellInfo *)i->data;
3332 GSList *list = info->attributes;
3335 info->destroy (info->func_data);
3337 while (list && list->next)
3339 g_free (list->data);
3340 list = list->next->next;
3342 g_slist_free (info->attributes);
3344 g_object_unref (G_OBJECT (info->cell));
3347 g_slist_free (combo_box->priv->cells);
3349 G_OBJECT_CLASS (parent_class)->finalize (object);