X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkcombobox.c;h=794cd00dd33688d706377bc942035cf8f1863320;hb=3b2182e711ace4a2d6843848208224cd7525d530;hp=5f2edf43aa562b03bd458f82ec23b58738601ac5;hpb=f3de78a023a5509f5defaf0137569cfef0349e19;p=~andy%2Fgtk diff --git a/gtk/gtkcombobox.c b/gtk/gtkcombobox.c index 5f2edf43a..794cd00dd 100644 --- a/gtk/gtkcombobox.c +++ b/gtk/gtkcombobox.c @@ -12,14 +12,16 @@ * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * License along with this library. If not, see . */ #include "config.h" + #include "gtkcombobox.h" +#include "gtkadjustment.h" +#include "gtkcellareabox.h" +#include "gtktreemenu.h" #include "gtkarrow.h" #include "gtkbindings.h" #include "gtkcelllayout.h" @@ -27,24 +29,22 @@ #include "gtkcellview.h" #include "gtkeventbox.h" #include "gtkframe.h" -#include "gtkhbox.h" +#include "gtkbox.h" #include "gtkliststore.h" #include "gtkmain.h" #include "gtkmenuprivate.h" #include "gtkmenushellprivate.h" #include "gtkscrolledwindow.h" #include "gtkseparatormenuitem.h" -#include "gtktearoffmenuitem.h" +#include "deprecated/gtktearoffmenuitem.h" #include "gtktogglebutton.h" #include "gtktreeselection.h" -#include "gtkvseparator.h" +#include "gtkseparator.h" +#include "gtkwidgetpath.h" +#include "gtkwidgetprivate.h" #include "gtkwindow.h" -#include "gtkcellareabox.h" -#include "gtktreemenu.h" +#include "gtktypebuiltins.h" #include "gtkprivate.h" -#include "gtkentryprivate.h" - -#include #include @@ -54,7 +54,9 @@ #include "gtkmarshalers.h" #include "gtkintl.h" +#include "gtkentryprivate.h" #include "gtktreeprivate.h" +#include "a11y/gtkcomboboxaccessible.h" /** @@ -78,13 +80,87 @@ * not restricted to a flat list, it can be a real tree, and the popup will * reflect the tree structure. * + * To allow the user to enter values not in the model, the 'has-entry' + * property allows the GtkComboBox to contain a #GtkEntry. This entry + * can be accessed by calling gtk_bin_get_child() on the combo box. + * * For a simple list of textual choices, the model-view API of GtkComboBox * can be a bit overwhelming. In this case, #GtkComboBoxText offers a - * simple alternative. + * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain + * an entry. */ /* WELCOME, to THE house of evil code */ +struct _GtkComboBoxPrivate +{ + GtkTreeModel *model; + + GtkCellArea *area; + + gint col_column; + gint row_column; + + gint wrap_width; + GtkShadowType shadow_type; + + gint active; /* Only temporary */ + GtkTreeRowReference *active_row; + + GtkWidget *tree_view; + + GtkWidget *cell_view; + GtkWidget *cell_view_frame; + + GtkWidget *button; + GtkWidget *box; + GtkWidget *arrow; + GtkWidget *separator; + + GtkWidget *popup_widget; + GtkWidget *popup_window; + GtkWidget *scrolled_window; + + gulong inserted_id; + gulong deleted_id; + gulong reordered_id; + gulong changed_id; + guint popup_idle_id; + guint activate_button; + guint32 activate_time; + guint scroll_timer; + guint resize_idle_id; + + /* For "has-entry" specific behavior we track + * an automated cell renderer and text column + */ + gint text_column; + GtkCellRenderer *text_renderer; + + gint id_column; + + guint popup_in_progress : 1; + guint popup_shown : 1; + guint add_tearoffs : 1; + guint has_frame : 1; + guint is_cell_renderer : 1; + guint editing_canceled : 1; + guint auto_scroll : 1; + guint focus_on_click : 1; + guint button_sensitivity : 2; + guint has_entry : 1; + guint popup_fixed_width : 1; + + GtkTreeViewRowSeparatorFunc row_separator_func; + gpointer row_separator_data; + GDestroyNotify row_separator_destroy; + + GdkDevice *grab_pointer; + GdkDevice *grab_keyboard; + + gchar *tearoff_title; +}; + /* While debugging this evil code, I have learned that * there are actually 4 modes to this widget, which can * be characterized as follows @@ -139,12 +215,52 @@ * */ -/* GObjectClass */ +enum { + CHANGED, + MOVE_ACTIVE, + POPUP, + POPDOWN, + FORMAT_ENTRY_TEXT, + LAST_SIGNAL +}; + +enum { + PROP_0, + PROP_MODEL, + PROP_WRAP_WIDTH, + PROP_ROW_SPAN_COLUMN, + PROP_COLUMN_SPAN_COLUMN, + PROP_ACTIVE, + PROP_ADD_TEAROFFS, + PROP_TEAROFF_TITLE, + PROP_HAS_FRAME, + PROP_FOCUS_ON_CLICK, + PROP_POPUP_SHOWN, + PROP_BUTTON_SENSITIVITY, + PROP_EDITING_CANCELED, + PROP_HAS_ENTRY, + PROP_ENTRY_TEXT_COLUMN, + PROP_POPUP_FIXED_WIDTH, + PROP_ID_COLUMN, + PROP_ACTIVE_ID, + PROP_CELL_AREA +}; + +static guint combo_box_signals[LAST_SIGNAL] = {0,}; + +#define SCROLL_TIME 100 + +/* common */ + +static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface); +static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface); static GObject *gtk_combo_box_constructor (GType type, - guint n_construct_properties, - GObjectConstructParam *construct_properties); + guint n_construct_properties, + GObjectConstructParam *construct_properties); static void gtk_combo_box_dispose (GObject *object); static void gtk_combo_box_finalize (GObject *object); +static void gtk_combo_box_destroy (GtkWidget *widget); + static void gtk_combo_box_set_property (GObject *object, guint prop_id, const GValue *value, @@ -154,58 +270,19 @@ static void gtk_combo_box_get_property (GObject *object, GValue *value, GParamSpec *spec); -/* GtkWidgetClass */ -static void gtk_combo_box_destroy (GtkWidget *widget); -static void gtk_combo_box_state_changed (GtkWidget *widget, - GtkStateType previous); -static void gtk_combo_box_grab_focus (GtkWidget *widget); -static void gtk_combo_box_style_updated (GtkWidget *widget); -static void gtk_combo_box_size_allocate (GtkWidget *widget, - GtkAllocation *allocation); -static void gtk_combo_box_get_preferred_width (GtkWidget *widget, - gint *minimum_size, - gint *natural_size); -static void gtk_combo_box_get_preferred_height (GtkWidget *widget, - gint *minimum_size, - gint *natural_size); -static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget, - gint avail_size, - gint *minimum_size, - gint *natural_size); -static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget, - gint avail_size, - gint *minimum_size, - gint *natural_size); -static gboolean gtk_combo_box_draw (GtkWidget *widget, - cairo_t *cr); -static gboolean gtk_combo_box_scroll_event (GtkWidget *widget, - GdkEventScroll *event); - -/* GtkContainerClass */ +static void gtk_combo_box_state_flags_changed (GtkWidget *widget, + GtkStateFlags previous); +static void gtk_combo_box_grab_focus (GtkWidget *widget); +static void gtk_combo_box_style_updated (GtkWidget *widget); +static void gtk_combo_box_button_toggled (GtkWidget *widget, + gpointer data); +static void gtk_combo_box_button_state_flags_changed (GtkWidget *widget, + GtkStateFlags previous, + gpointer data); static void gtk_combo_box_add (GtkContainer *container, GtkWidget *widget); static void gtk_combo_box_remove (GtkContainer *container, GtkWidget *widget); -static void gtk_combo_box_forall (GtkContainer *container, - gboolean include_internals, - GtkCallback callback, - gpointer callback_data); - -/* GtkComboBoxClass (binding handlers) */ -static void gtk_combo_box_real_move_active (GtkComboBox *combo_box, - GtkScrollType scroll); -static void gtk_combo_box_real_popup (GtkComboBox *combo_box); -static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box); - - -/* GtkTreeModel signals */ -static void gtk_combo_box_model_row_inserted (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer user_data); -static void gtk_combo_box_model_row_deleted (GtkTreeModel *model, - GtkTreePath *path, - gpointer user_data); static void gtk_combo_box_menu_show (GtkWidget *menu, gpointer user_data); @@ -214,30 +291,71 @@ static void gtk_combo_box_menu_hide (GtkWidget *menu, static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box, GtkWidget *popup); +static void gtk_combo_box_menu_position_below (GtkMenu *menu, + gint *x, + gint *y, + gint *push_in, + gpointer user_data); +static void gtk_combo_box_menu_position_over (GtkMenu *menu, + gint *x, + gint *y, + gint *push_in, + gpointer user_data); +static void gtk_combo_box_menu_position (GtkMenu *menu, + gint *x, + gint *y, + gint *push_in, + gpointer user_data); static void gtk_combo_box_unset_model (GtkComboBox *combo_box); -static void gtk_combo_box_button_toggled (GtkWidget *widget, - gpointer data); -static void gtk_combo_box_button_state_changed (GtkWidget *widget, - GtkStateType previous, - gpointer data); +static void gtk_combo_box_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); +static void gtk_combo_box_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data); +static gboolean gtk_combo_box_draw (GtkWidget *widget, + cairo_t *cr); +static gboolean gtk_combo_box_scroll_event (GtkWidget *widget, + GdkEventScroll *event); static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box, - GtkTreePath *path); + GtkTreePath *path); static void gtk_combo_box_check_appearance (GtkComboBox *combo_box); +static void gtk_combo_box_real_move_active (GtkComboBox *combo_box, + GtkScrollType scroll); +static void gtk_combo_box_real_popup (GtkComboBox *combo_box); +static gboolean gtk_combo_box_real_popdown (GtkComboBox *combo_box); +/* listening to the model */ +static void gtk_combo_box_model_row_inserted (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer user_data); +static void gtk_combo_box_model_row_deleted (GtkTreeModel *model, + GtkTreePath *path, + gpointer user_data); +static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gint *new_order, + gpointer user_data); +static void gtk_combo_box_model_row_changed (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data); static void gtk_combo_box_model_row_expanded (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data); + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data); /* list */ -static void gtk_combo_box_list_position (GtkComboBox *combo_box, - gint *x, - gint *y, - gint *width, - gint *height); +static void gtk_combo_box_list_position (GtkComboBox *combo_box, + gint *x, + gint *y, + gint *width, + gint *height); static void gtk_combo_box_list_setup (GtkComboBox *combo_box); static void gtk_combo_box_list_destroy (GtkComboBox *combo_box); @@ -251,299 +369,206 @@ static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget, GdkEventCrossing *event, gpointer data); static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo, - gint x, - gint y); + gint x, + gint y); static gboolean gtk_combo_box_list_scroll_timeout (GtkComboBox *combo); static gboolean gtk_combo_box_list_button_pressed (GtkWidget *widget, GdkEventButton *event, gpointer data); static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection, - GtkTreeModel *model, - GtkTreePath *path, - gboolean path_currently_selected, - gpointer data); + GtkTreeModel *model, + GtkTreePath *path, + gboolean path_currently_selected, + gpointer data); +static void gtk_combo_box_list_row_changed (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data); static void gtk_combo_box_list_popup_resize (GtkComboBox *combo_box); /* menu */ static void gtk_combo_box_menu_setup (GtkComboBox *combo_box, gboolean add_children); -static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box); static void gtk_combo_box_update_title (GtkComboBox *combo_box); +static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box); + + static gboolean gtk_combo_box_menu_button_press (GtkWidget *widget, GdkEventButton *event, gpointer user_data); static void gtk_combo_box_menu_activate (GtkWidget *menu, - const gchar *path, - GtkComboBox *combo_box); + const gchar *path, + GtkComboBox *combo_box); +static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box); static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget, - GdkEventKey *event, - gpointer data); + GdkEventKey *event, + gpointer data); static void gtk_combo_box_menu_popup (GtkComboBox *combo_box, - guint button, - guint32 activate_time); + guint button, + guint32 activate_time); -static void gtk_combo_box_menu_position_below (GtkMenu *menu, - gint *x, - gint *y, - gint *push_in, - gpointer user_data); -static void gtk_combo_box_menu_position_over (GtkMenu *menu, - gint *x, - gint *y, - gint *push_in, - gpointer user_data); -static void gtk_combo_box_menu_position (GtkMenu *menu, - gint *x, - gint *y, - gint *push_in, - gpointer user_data); +/* cell layout */ +static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout); static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget, - gboolean group_cycling); + gboolean group_cycling); static void gtk_combo_box_child_show (GtkWidget *widget, - GtkComboBox *combo_box); + GtkComboBox *combo_box); static void gtk_combo_box_child_hide (GtkWidget *widget, - GtkComboBox *combo_box); + GtkComboBox *combo_box); /* GtkComboBox:has-entry callbacks */ static void gtk_combo_box_entry_contents_changed (GtkEntry *entry, - gpointer user_data); + gpointer user_data); static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box, - gpointer user_data); -/* GtkBuildableIface */ + gpointer user_data); +static gchar *gtk_combo_box_format_entry_text (GtkComboBox *combo_box, + const gchar *path); + +/* GtkBuildable method implementation */ +static GtkBuildableIface *parent_buildable_iface; + static void gtk_combo_box_buildable_init (GtkBuildableIface *iface); static gboolean gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable, - GtkBuilder *builder, - GObject *child, - const gchar *tagname, - GMarkupParser *parser, - gpointer *data); + GtkBuilder *builder, + GObject *child, + const gchar *tagname, + GMarkupParser *parser, + gpointer *data); static void gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable, - GtkBuilder *builder, - GObject *child, - const gchar *tagname, - gpointer *data); + GtkBuilder *builder, + GObject *child, + const gchar *tagname, + gpointer *data); static GObject *gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable, - GtkBuilder *builder, - const gchar *childname); + GtkBuilder *builder, + const gchar *childname); + -/* GtkCellEditable */ -static void gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface); +/* GtkCellEditable method implementations */ static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable, - GdkEvent *event); + GdkEvent *event); + +static void gtk_combo_box_get_preferred_width (GtkWidget *widget, + gint *minimum_size, + gint *natural_size); +static void gtk_combo_box_get_preferred_height (GtkWidget *widget, + gint *minimum_size, + gint *natural_size); +static void gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget, + gint avail_size, + gint *minimum_size, + gint *natural_size); +static void gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget, + gint avail_size, + gint *minimum_size, + gint *natural_size); +static GtkWidgetPath *gtk_combo_box_get_path_for_child (GtkContainer *container, + GtkWidget *child); +static void gtk_combo_box_direction_changed (GtkWidget *widget, + GtkTextDirection previous_direction); -/* GtkCellLayoutIface */ -static void gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface); -static GtkCellArea *gtk_combo_box_cell_layout_get_area (GtkCellLayout *layout); +G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN, + G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT, + gtk_combo_box_cell_layout_init) + G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE, + gtk_combo_box_cell_editable_init) + G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, + gtk_combo_box_buildable_init)) -struct _GtkComboBoxPrivate +/* common */ +static void +gtk_combo_box_class_init (GtkComboBoxClass *klass) { - GtkTreeModel *model; - - /* The cell area shared with the treeview or treemenu */ - GtkCellArea *area; + GObjectClass *object_class; + GtkContainerClass *container_class; + GtkWidgetClass *widget_class; + GtkBindingSet *binding_set; - gint col_column; - gint row_column; - gint id_column; + container_class = (GtkContainerClass *)klass; + container_class->forall = gtk_combo_box_forall; + container_class->add = gtk_combo_box_add; + container_class->remove = gtk_combo_box_remove; + container_class->get_path_for_child = gtk_combo_box_get_path_for_child; - gint wrap_width; - GtkShadowType shadow_type; + gtk_container_class_handle_border_width (container_class); - gint active; /* Only temporary */ - GtkTreeRowReference *active_row; + widget_class = (GtkWidgetClass *)klass; + widget_class->size_allocate = gtk_combo_box_size_allocate; + widget_class->draw = gtk_combo_box_draw; + widget_class->scroll_event = gtk_combo_box_scroll_event; + widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate; + widget_class->grab_focus = gtk_combo_box_grab_focus; + widget_class->style_updated = gtk_combo_box_style_updated; + widget_class->state_flags_changed = gtk_combo_box_state_flags_changed; + widget_class->get_preferred_width = gtk_combo_box_get_preferred_width; + widget_class->get_preferred_height = gtk_combo_box_get_preferred_height; + widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width; + widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height; + widget_class->destroy = gtk_combo_box_destroy; + widget_class->direction_changed = gtk_combo_box_direction_changed; + object_class = (GObjectClass *)klass; + object_class->constructor = gtk_combo_box_constructor; + object_class->dispose = gtk_combo_box_dispose; + object_class->finalize = gtk_combo_box_finalize; + object_class->set_property = gtk_combo_box_set_property; + object_class->get_property = gtk_combo_box_get_property; - /* The cellview displayed on the button */ - GtkWidget *cell_view; - GtkWidget *cell_view_frame; + klass->format_entry_text = gtk_combo_box_format_entry_text; - /* The treeview & column for list mode */ - GtkWidget *tree_view; - GtkTreeViewColumn *column; - - GtkWidget *button; - GtkWidget *box; - GtkWidget *arrow; - GtkWidget *separator; - - GtkWidget *popup_widget; - GtkWidget *popup_window; - GtkWidget *scrolled_window; - - gulong inserted_id; - gulong deleted_id; - guint popup_idle_id; - guint activate_button; - guint32 activate_time; - guint scroll_timer; - guint resize_idle_id; - - /* For "has-entry" specific behavior we track - * an automated cell renderer and text column */ - gint text_column; - GtkCellRenderer *text_renderer; - - guint in_construction : 1; - guint popup_in_progress : 1; - guint popup_shown : 1; - guint add_tearoffs : 1; - guint has_frame : 1; - guint is_cell_renderer : 1; - guint editing_canceled : 1; - guint auto_scroll : 1; - guint focus_on_click : 1; - guint button_sensitivity : 2; - guint has_entry : 1; - guint popup_fixed_width : 1; - - GtkTreeViewRowSeparatorFunc row_separator_func; - gpointer row_separator_data; - GDestroyNotify row_separator_destroy; - - GdkDevice *grab_pointer; - GdkDevice *grab_keyboard; - - gchar *tearoff_title; -}; - - -enum { - CHANGED, - MOVE_ACTIVE, - POPUP, - POPDOWN, - LAST_SIGNAL -}; - -enum { - PROP_0, - PROP_MODEL, - PROP_WRAP_WIDTH, - PROP_ROW_SPAN_COLUMN, - PROP_COLUMN_SPAN_COLUMN, - PROP_ACTIVE, - PROP_ADD_TEAROFFS, - PROP_TEAROFF_TITLE, - PROP_HAS_FRAME, - PROP_FOCUS_ON_CLICK, - PROP_POPUP_SHOWN, - PROP_BUTTON_SENSITIVITY, - PROP_EDITING_CANCELED, - PROP_HAS_ENTRY, - PROP_ENTRY_TEXT_COLUMN, - PROP_POPUP_FIXED_WIDTH, - PROP_ID_COLUMN, - PROP_ACTIVE_ID, - PROP_CELL_AREA -}; - -#define BONUS_PADDING 4 -#define SCROLL_TIME 100 - -static guint combo_box_signals[LAST_SIGNAL] = {0,}; - - -/* GtkBuildable method implementation */ -static GtkBuildableIface *parent_buildable_iface; - - -G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN, - G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT, - gtk_combo_box_cell_layout_init) - G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE, - gtk_combo_box_cell_editable_init) - G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, - gtk_combo_box_buildable_init)) - - -/* common */ -static void -gtk_combo_box_class_init (GtkComboBoxClass *klass) -{ - GObjectClass *object_class; - GtkContainerClass *container_class; - GtkWidgetClass *widget_class; - GtkBindingSet *binding_set; - - object_class = (GObjectClass *)klass; - object_class->constructor = gtk_combo_box_constructor; - object_class->dispose = gtk_combo_box_dispose; - object_class->finalize = gtk_combo_box_finalize; - object_class->set_property = gtk_combo_box_set_property; - object_class->get_property = gtk_combo_box_get_property; - - widget_class = (GtkWidgetClass *)klass; - widget_class->draw = gtk_combo_box_draw; - widget_class->scroll_event = gtk_combo_box_scroll_event; - widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate; - widget_class->grab_focus = gtk_combo_box_grab_focus; - widget_class->style_updated = gtk_combo_box_style_updated; - widget_class->state_changed = gtk_combo_box_state_changed; - widget_class->size_allocate = gtk_combo_box_size_allocate; - widget_class->get_preferred_width = gtk_combo_box_get_preferred_width; - widget_class->get_preferred_height = gtk_combo_box_get_preferred_height; - widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width; - widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height; - widget_class->destroy = gtk_combo_box_destroy; - - container_class = (GtkContainerClass *)klass; - container_class->forall = gtk_combo_box_forall; - container_class->add = gtk_combo_box_add; - container_class->remove = gtk_combo_box_remove; - - /* signals */ - /** - * GtkComboBox::changed: - * @widget: the object which received the signal - * - * The changed signal is emitted when the active - * item is changed. The can be due to the user selecting - * a different item from the list, or due to a - * call to gtk_combo_box_set_active_iter(). - * It will also be emitted while typing into the entry of a combo box - * with an entry. - * - * Since: 2.4 - */ - combo_box_signals[CHANGED] = - g_signal_new (I_("changed"), - G_OBJECT_CLASS_TYPE (klass), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (GtkComboBoxClass, changed), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); - /** - * GtkComboBox::move-active: - * @widget: the object that received the signal - * @scroll_type: a #GtkScrollType - * - * The ::move-active signal is a - * keybinding signal - * which gets emitted to move the active selection. - * - * Since: 2.12 - */ - combo_box_signals[MOVE_ACTIVE] = - g_signal_new_class_handler (I_("move-active"), - G_OBJECT_CLASS_TYPE (klass), - G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, - G_CALLBACK (gtk_combo_box_real_move_active), - NULL, NULL, - g_cclosure_marshal_VOID__ENUM, - G_TYPE_NONE, 1, - GTK_TYPE_SCROLL_TYPE); + /* signals */ + /** + * GtkComboBox::changed: + * @widget: the object which received the signal + * + * The changed signal is emitted when the active + * item is changed. The can be due to the user selecting + * a different item from the list, or due to a + * call to gtk_combo_box_set_active_iter(). + * It will also be emitted while typing into the entry of a combo box + * with an entry. + * + * Since: 2.4 + */ + combo_box_signals[CHANGED] = + g_signal_new (I_("changed"), + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GtkComboBoxClass, changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + /** + * GtkComboBox::move-active: + * @widget: the object that received the signal + * @scroll_type: a #GtkScrollType + * + * The ::move-active signal is a + * keybinding signal + * which gets emitted to move the active selection. + * + * Since: 2.12 + */ + combo_box_signals[MOVE_ACTIVE] = + g_signal_new_class_handler (I_("move-active"), + G_OBJECT_CLASS_TYPE (klass), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_CALLBACK (gtk_combo_box_real_move_active), + NULL, NULL, + g_cclosure_marshal_VOID__ENUM, + G_TYPE_NONE, 1, + GTK_TYPE_SCROLL_TYPE); /** * GtkComboBox::popup: * @widget: the object that received the signal * - * The ::popup signal is a + * The ::popup signal is a * keybinding signal * which gets emitted to popup the combo box list. * @@ -563,8 +588,8 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) * GtkComboBox::popdown: * @button: the object which received the signal * - * The ::popdown signal is a - * keybinding signal + * The ::popdown signal is a + * keybinding signal * which gets emitted to popdown the combo box list. * * The default bindings for this signal are Alt+Up and Escape. @@ -580,58 +605,110 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) _gtk_marshal_BOOLEAN__VOID, G_TYPE_BOOLEAN, 0); + /** + * GtkComboBox::format-entry-text: + * @combo: the object which received the signal + * @path: the GtkTreePath string from the combo box's current model to format text for + * + * For combo boxes that are created with an entry (See GtkComboBox:has-entry). + * + * A signal which allows you to change how the text displayed in a combo box's + * entry is displayed. + * + * Connect a signal handler which returns an allocated string representing + * @path. That string will then be used to set the text in the combo box's entry. + * The default signal handler uses the text from the GtkComboBox::entry-text-column + * model column. + * + * Here's an example signal handler which fetches data from the model and + * displays it in the entry. + * |[ + * static gchar* + * format_entry_text_callback (GtkComboBox *combo, + * const gchar *path, + * gpointer user_data) + * { + * GtkTreeIter iter; + * GtkTreeModel model; + * gdouble value; + * + * model = gtk_combo_box_get_model (combo); + * + * gtk_tree_model_get_iter_from_string (model, &iter, path); + * gtk_tree_model_get (model, &iter, + * THE_DOUBLE_VALUE_COLUMN, &value, + * -1); + * + * return g_strdup_printf ("%g", value); + * } + * ]| + * + * Return value: (transfer full): a newly allocated string representing @path + * for the current GtkComboBox model. + * + * Since: 3.4 + */ + combo_box_signals[FORMAT_ENTRY_TEXT] = + g_signal_new (I_("format-entry-text"), + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text), + _gtk_single_string_accumulator, NULL, + _gtk_marshal_STRING__STRING, + G_TYPE_STRING, 1, G_TYPE_STRING); + /* key bindings */ binding_set = gtk_binding_set_by_class (widget_class); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK, - "popup", 0); + "popup", 0); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK, - "popup", 0); + "popup", 0); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK, - "popdown", 0); + "popdown", 0); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK, - "popdown", 0); + "popdown", 0); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, - "popdown", 0); + "popdown", 0); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN); gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN); gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END); gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0, - "move-active", 1, - GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END); + "move-active", 1, + GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END); /* properties */ g_object_class_override_property (object_class, @@ -642,7 +719,7 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) * GtkComboBox:model: * * The model from which the combo box takes the values shown - * in the list. + * in the list. * * Since: 2.4 */ @@ -677,11 +754,11 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) /** * GtkComboBox:row-span-column: * - * If this is set to a non-negative value, it must be the index of a column - * of type %G_TYPE_INT in the model. + * If this is set to a non-negative value, it must be the index of a column + * of type %G_TYPE_INT in the model. * - * The values of that column are used to determine how many rows a value in - * the list will span. Therefore, the values in the model column pointed to + * The values of that column are used to determine how many rows a value in + * the list will span. Therefore, the values in the model column pointed to * by this property must be greater than zero and not larger than wrap-width. * * Since: 2.4 @@ -700,11 +777,11 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) /** * GtkComboBox:column-span-column: * - * If this is set to a non-negative value, it must be the index of a column - * of type %G_TYPE_INT in the model. + * If this is set to a non-negative value, it must be the index of a column + * of type %G_TYPE_INT in the model. * - * The values of that column are used to determine how many columns a value - * in the list will span. + * The values of that column are used to determine how many columns a value + * in the list will span. * * Since: 2.4 */ @@ -724,7 +801,7 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) * * The item which is currently active. If the model is a non-flat treemodel, * and the active item is not an immediate child of the root of the tree, - * this property has the value + * this property has the value * gtk_tree_path_get_indices (path)[0], * where path is the #GtkTreePath of the active item. * @@ -743,8 +820,8 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) /** * GtkComboBox:add-tearoffs: * - * The add-tearoffs property controls whether generated menus - * have tearoff menu items. + * The add-tearoffs property controls whether generated menus + * have tearoff menu items. * * Note that this only affects menu style combo boxes. * @@ -752,12 +829,12 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) */ g_object_class_install_property (object_class, PROP_ADD_TEAROFFS, - g_param_spec_boolean ("add-tearoffs", - P_("Add tearoffs to menus"), - P_("Whether dropdowns should have a tearoff menu item"), - FALSE, - GTK_PARAM_READWRITE)); - + g_param_spec_boolean ("add-tearoffs", + P_("Add tearoffs to menus"), + P_("Whether dropdowns should have a tearoff menu item"), + FALSE, + GTK_PARAM_READWRITE)); + /** * GtkComboBox:has-frame: * @@ -768,24 +845,24 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) */ g_object_class_install_property (object_class, PROP_HAS_FRAME, - g_param_spec_boolean ("has-frame", - P_("Has Frame"), - P_("Whether the combo box draws a frame around the child"), - TRUE, - GTK_PARAM_READWRITE)); - + g_param_spec_boolean ("has-frame", + P_("Has Frame"), + P_("Whether the combo box draws a frame around the child"), + TRUE, + GTK_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_FOCUS_ON_CLICK, g_param_spec_boolean ("focus-on-click", - P_("Focus on click"), - P_("Whether the combo box grabs focus when it is clicked with the mouse"), - TRUE, - GTK_PARAM_READWRITE)); + P_("Focus on click"), + P_("Whether the combo box grabs focus when it is clicked with the mouse"), + TRUE, + GTK_PARAM_READWRITE)); /** * GtkComboBox:tearoff-title: * - * A title that may be displayed by the window manager + * A title that may be displayed by the window manager * when the popup is torn-off. * * Since: 2.10 @@ -802,7 +879,7 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) /** * GtkComboBox:popup-shown: * - * Whether the combo boxes dropdown is popped up. + * Whether the combo boxes dropdown is popped up. * Note that this property is mainly useful, because * it allows you to connect to notify::popup-shown. * @@ -844,10 +921,10 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) g_object_class_install_property (object_class, PROP_HAS_ENTRY, g_param_spec_boolean ("has-entry", - P_("Has Entry"), - P_("Whether combo box has an entry"), - FALSE, - GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + P_("Has Entry"), + P_("Whether combo box has an entry"), + FALSE, + GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); /** * GtkComboBox:entry-text-column: @@ -860,12 +937,12 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) g_object_class_install_property (object_class, PROP_ENTRY_TEXT_COLUMN, g_param_spec_int ("entry-text-column", - P_("Entry Text Column"), - P_("The column in the combo box's model to associate " - "with strings from the entry if the combo was " - "created with #GtkComboBox:has-entry = %TRUE"), - -1, G_MAXINT, -1, - GTK_PARAM_READWRITE)); + P_("Entry Text Column"), + P_("The column in the combo box's model to associate " + "with strings from the entry if the combo was " + "created with #GtkComboBox:has-entry = %TRUE"), + -1, G_MAXINT, -1, + GTK_PARAM_READWRITE)); /** * GtkComboBox:id-column: @@ -898,38 +975,42 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) P_("The value of the id column " "for the active row"), NULL, GTK_PARAM_READWRITE)); + /** - * GtkComboBox:cell-area: + * GtkComboBox:popup-fixed-width: * - * The #GtkCellArea used to layout cell renderers for this combo box. + * Whether the popup's width should be a fixed width matching the + * allocated width of the combo box. * * Since: 3.0 */ g_object_class_install_property (object_class, - PROP_CELL_AREA, - g_param_spec_object ("cell-area", - P_("Cell Area"), - P_("The GtkCellArea used to layout cells"), - GTK_TYPE_CELL_AREA, - GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + PROP_POPUP_FIXED_WIDTH, + g_param_spec_boolean ("popup-fixed-width", + P_("Popup Fixed Width"), + P_("Whether the popup's width should be a " + "fixed width matching the allocated width " + "of the combo box"), + TRUE, + GTK_PARAM_READWRITE)); /** - * GtkComboBox:popup-fixed-width: + * GtkComboBox:cell-area: * - * Whether the popup's width should be a fixed width matching the - * allocated width of the combo box. + * The #GtkCellArea used to layout cell renderers for this combo box. + * + * If no area is specified when creating the combo box with gtk_combo_box_new_with_area() + * a horizontally oriented #GtkCellAreaBox will be used. * * Since: 3.0 */ g_object_class_install_property (object_class, - PROP_POPUP_FIXED_WIDTH, - g_param_spec_boolean ("popup-fixed-width", - P_("Popup Fixed Width"), - P_("Whether the popup's width should be a " - "fixed width matching the allocated width " - "of the combo box"), - TRUE, - GTK_PARAM_READWRITE)); + PROP_CELL_AREA, + g_param_spec_object ("cell-area", + P_("Cell Area"), + P_("The GtkCellArea used to layout cells"), + GTK_TYPE_CELL_AREA, + GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); gtk_widget_class_install_style_property (widget_class, g_param_spec_boolean ("appears-as-list", @@ -949,13 +1030,30 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) * Since: 2.12 */ gtk_widget_class_install_style_property (widget_class, - g_param_spec_int ("arrow-size", - P_("Arrow Size"), - P_("The minimum size of the arrow in the combo box"), - 0, - G_MAXINT, - 15, - GTK_PARAM_READABLE)); + g_param_spec_int ("arrow-size", + P_("Arrow Size"), + P_("The minimum size of the arrow in the combo box"), + 0, + G_MAXINT, + 15, + GTK_PARAM_READABLE)); + + /** + * GtkComboBox:arrow-scaling: + * + * Sets the amount of space used up by the combobox arrow, + * proportional to the font size. + * + * Since: 3.2 + */ + gtk_widget_class_install_style_property (widget_class, + g_param_spec_float ("arrow-scaling", + P_("Arrow Scaling"), + P_("The amount of space used by the arrow"), + 0, + 2.0, + 1.0, + GTK_PARAM_READABLE)); /** * GtkComboBox:shadow-type: @@ -973,6 +1071,30 @@ gtk_combo_box_class_init (GtkComboBoxClass *klass) GTK_PARAM_READABLE)); g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate)); + + gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE); +} + +static void +gtk_combo_box_buildable_init (GtkBuildableIface *iface) +{ + parent_buildable_iface = g_type_interface_peek_parent (iface); + iface->add_child = _gtk_cell_layout_buildable_add_child; + iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start; + iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end; + iface->get_internal_child = gtk_combo_box_buildable_get_internal_child; +} + +static void +gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface) +{ + iface->get_area = gtk_combo_box_cell_layout_get_area; +} + +static void +gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface) +{ + iface->start_editing = gtk_combo_box_start_editing; } static void @@ -1008,122 +1130,25 @@ gtk_combo_box_init (GtkComboBox *combo_box) priv->id_column = -1; } -/****************************************************** - * GObjectClass * - ******************************************************/ -static GObject * -gtk_combo_box_constructor (GType type, - guint n_construct_properties, - GObjectConstructParam *construct_properties) +static void +gtk_combo_box_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) { - GObject *object; - GtkComboBox *combo_box; - GtkComboBoxPrivate *priv; - GtkStyleContext *context; - - object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor - (type, n_construct_properties, construct_properties); - - combo_box = GTK_COMBO_BOX (object); - priv = combo_box->priv; + GtkComboBox *combo_box = GTK_COMBO_BOX (object); + GtkComboBoxPrivate *priv = combo_box->priv; + GtkCellArea *area; - if (!priv->area) + switch (prop_id) { - GtkCellArea *area = gtk_cell_area_box_new (); + case PROP_MODEL: + gtk_combo_box_set_model (combo_box, g_value_get_object (value)); + break; - priv->area = g_object_ref_sink (area); - } - - priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL); - gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE); - gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model); - gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box)); - _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view); - gtk_widget_show (priv->cell_view); - - gtk_combo_box_check_appearance (combo_box); - - context = gtk_widget_get_style_context (GTK_WIDGET (combo_box)); - gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON); - - if (priv->has_entry) - { - GtkWidget *entry; - - entry = gtk_entry_new (); - gtk_widget_show (entry); - gtk_container_add (GTK_CONTAINER (combo_box), entry); - - priv->text_renderer = gtk_cell_renderer_text_new (); - gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), - priv->text_renderer, TRUE); - - gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1); - - g_signal_connect (combo_box, "changed", - G_CALLBACK (gtk_combo_box_entry_active_changed), NULL); - } - - return object; -} - -static void -gtk_combo_box_dispose(GObject* object) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (object); - - if (GTK_IS_MENU (combo_box->priv->popup_widget)) - { - gtk_combo_box_menu_destroy (combo_box); - gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget)); - combo_box->priv->popup_widget = NULL; - } - - if (combo_box->priv->area) - { - g_object_unref (combo_box->priv->area); - combo_box->priv->area = NULL; - } - - G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object); -} - -static void -gtk_combo_box_finalize (GObject *object) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (object); - - if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)) - gtk_combo_box_list_destroy (combo_box); - - if (combo_box->priv->popup_window) - gtk_widget_destroy (combo_box->priv->popup_window); - - gtk_combo_box_unset_model (combo_box); - - g_free (combo_box->priv->tearoff_title); - - G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object); -} - -static void -gtk_combo_box_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (object); - GtkCellArea *area; - - switch (prop_id) - { - case PROP_MODEL: - gtk_combo_box_set_model (combo_box, g_value_get_object (value)); - break; - - case PROP_WRAP_WIDTH: - gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value)); - break; + case PROP_WRAP_WIDTH: + gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value)); + break; case PROP_ROW_SPAN_COLUMN: gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value)); @@ -1142,16 +1167,15 @@ gtk_combo_box_set_property (GObject *object, break; case PROP_HAS_FRAME: - combo_box->priv->has_frame = g_value_get_boolean (value); + priv->has_frame = g_value_get_boolean (value); - if (combo_box->priv->has_entry) + if (priv->has_entry) { GtkWidget *child; child = gtk_bin_get_child (GTK_BIN (combo_box)); - gtk_entry_set_has_frame (GTK_ENTRY (child), - combo_box->priv->has_frame); + gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame); } break; @@ -1179,15 +1203,15 @@ gtk_combo_box_set_property (GObject *object, case PROP_POPUP_FIXED_WIDTH: gtk_combo_box_set_popup_fixed_width (combo_box, - g_value_get_boolean (value)); + g_value_get_boolean (value)); break; case PROP_EDITING_CANCELED: - combo_box->priv->editing_canceled = g_value_get_boolean (value); + priv->editing_canceled = g_value_get_boolean (value); break; case PROP_HAS_ENTRY: - combo_box->priv->has_entry = g_value_get_boolean (value); + priv->has_entry = g_value_get_boolean (value); break; case PROP_ENTRY_TEXT_COLUMN: @@ -1205,9 +1229,17 @@ gtk_combo_box_set_property (GObject *object, case PROP_CELL_AREA: /* Construct-only, can only be assigned once */ area = g_value_get_object (value); - if (area) - combo_box->priv->area = g_object_ref_sink (area); + { + if (priv->area != NULL) + { + g_warning ("cell-area has already been set, ignoring construct property"); + g_object_ref_sink (area); + g_object_unref (area); + } + else + priv->area = g_object_ref_sink (area); + } break; default: @@ -1280,12 +1312,12 @@ gtk_combo_box_get_property (GObject *object, break; case PROP_HAS_ENTRY: - g_value_set_boolean (value, priv->has_entry); - break; + g_value_set_boolean (value, priv->has_entry); + break; case PROP_ENTRY_TEXT_COLUMN: - g_value_set_int (value, priv->text_column); - break; + g_value_set_int (value, priv->text_column); + break; case PROP_ID_COLUMN: g_value_set_int (value, priv->id_column); @@ -1296,8 +1328,8 @@ gtk_combo_box_get_property (GObject *object, break; case PROP_CELL_AREA: - g_value_set_object (value, priv->area); - break; + g_value_set_object (value, priv->area); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -1305,1274 +1337,1142 @@ gtk_combo_box_get_property (GObject *object, } } -/****************************************************** - * GtkWidgetClass * - ******************************************************/ -static gboolean -gtk_combo_box_draw (GtkWidget *widget, - cairo_t *cr) +static void +gtk_combo_box_state_flags_changed (GtkWidget *widget, + GtkStateFlags previous) { GtkComboBox *combo_box = GTK_COMBO_BOX (widget); GtkComboBoxPrivate *priv = combo_box->priv; - if (priv->shadow_type != GTK_SHADOW_NONE) + if (gtk_widget_get_realized (widget)) { - GtkStyleContext *context; - GtkStateFlags state; + if (priv->tree_view && priv->cell_view) + { + GtkStyleContext *context; + GtkStateFlags state; + GdkRGBA color; - context = gtk_widget_get_style_context (widget); - state = gtk_widget_get_state_flags (widget); - gtk_style_context_set_state (context, state); + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_background_color (context, state, &color); - gtk_render_background (context, cr, 0, 0, - gtk_widget_get_allocated_width (widget), - gtk_widget_get_allocated_height (widget)); - gtk_render_frame (context, cr, 0, 0, - gtk_widget_get_allocated_width (widget), - gtk_widget_get_allocated_height (widget)); + gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color); + } } - gtk_container_propagate_draw (GTK_CONTAINER (widget), - priv->button, cr); + gtk_widget_queue_draw (widget); +} - if (priv->tree_view && priv->cell_view_frame) +static void +gtk_combo_box_button_state_flags_changed (GtkWidget *widget, + GtkStateFlags previous, + gpointer data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (data); + GtkComboBoxPrivate *priv = combo_box->priv; + + if (gtk_widget_get_realized (widget)) { - gtk_container_propagate_draw (GTK_CONTAINER (widget), - priv->cell_view_frame, cr); + if (!priv->tree_view && priv->cell_view) + gtk_widget_set_state_flags (priv->cell_view, + gtk_widget_get_state_flags (widget), + TRUE); } - gtk_container_propagate_draw (GTK_CONTAINER (widget), - gtk_bin_get_child (GTK_BIN (widget)), - cr); - - return FALSE; + gtk_widget_queue_draw (widget); } +static void +gtk_combo_box_invalidate_order_foreach (GtkWidget *widget) +{ + _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION); +} -typedef struct { - GtkComboBox *combo; - GtkTreePath *path; - GtkTreeIter iter; - gboolean found; - gboolean set; - gboolean visible; -} SearchData; +static void +gtk_combo_box_invalidate_order (GtkComboBox *combo_box) +{ + gtk_container_forall (GTK_CONTAINER (combo_box), + (GtkCallback) gtk_combo_box_invalidate_order_foreach, + NULL); +} -static gboolean -path_visible (GtkTreeView *view, - GtkTreePath *path) +static void +gtk_combo_box_direction_changed (GtkWidget *widget, + GtkTextDirection previous_direction) { - GtkRBTree *tree; - GtkRBNode *node; - - /* Note that we rely on the fact that collapsed rows don't have nodes - */ - return _gtk_tree_view_find_node (view, path, &tree, &node); + gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget)); } -static gboolean -tree_column_row_is_sensitive (GtkComboBox *combo_box, - GtkTreeIter *iter) +static GtkWidgetPath * +gtk_combo_box_get_path_for_child (GtkContainer *container, + GtkWidget *child) { - GtkComboBoxPrivate *priv = combo_box->priv; - GList *cells, *list; - gboolean sensitive; + GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv; + GtkWidgetPath *path; + GtkWidget *widget; + gboolean found = FALSE; + GList *visible_children, *l; + GtkWidgetPath *sibling_path; + int pos; - if (!priv->column) - return TRUE; + path = _gtk_widget_create_path (GTK_WIDGET (container)); - if (priv->row_separator_func) + if (gtk_widget_get_visible (child)) { - if (priv->row_separator_func (priv->model, iter, - priv->row_separator_data)) - return FALSE; + visible_children = NULL; + + if (priv->button && gtk_widget_get_visible (priv->button)) + visible_children = g_list_prepend (visible_children, priv->button); + + if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame)) + visible_children = g_list_prepend (visible_children, priv->cell_view_frame); + + widget = gtk_bin_get_child (GTK_BIN (container)); + if (widget && gtk_widget_get_visible (widget)) + visible_children = g_list_prepend (visible_children, widget); + + if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL) + visible_children = g_list_reverse (visible_children); + + pos = 0; + + for (l = visible_children; l; l = l->next) + { + widget = l->data; + + if (widget == child) + { + found = TRUE; + break; + } + + pos++; + } } - gtk_tree_view_column_cell_set_cell_data (priv->column, - priv->model, - iter, FALSE, FALSE); + if (found) + { + sibling_path = gtk_widget_path_new (); - cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column)); + for (l = visible_children; l; l = l->next) + gtk_widget_path_append_for_widget (sibling_path, l->data); - sensitive = FALSE; - for (list = cells; list; list = list->next) + gtk_widget_path_append_with_siblings (path, sibling_path, pos); + + g_list_free (visible_children); + gtk_widget_path_unref (sibling_path); + } + else { - g_object_get (list->data, "sensitive", &sensitive, NULL); - - if (sensitive) - break; + gtk_widget_path_append_for_widget (path, child); } - g_list_free (cells); - return sensitive; + return path; } -static gboolean -tree_next_func (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data) +static void +gtk_combo_box_check_appearance (GtkComboBox *combo_box) { - SearchData *search_data = (SearchData *)data; + GtkComboBoxPrivate *priv = combo_box->priv; + gboolean appears_as_list; - if (search_data->found) + /* if wrap_width > 0, then we are in grid-mode and forced to use + * unix style + */ + if (priv->wrap_width) + appears_as_list = FALSE; + else + gtk_widget_style_get (GTK_WIDGET (combo_box), + "appears-as-list", &appears_as_list, + NULL); + + if (appears_as_list) { - if (!tree_column_row_is_sensitive (search_data->combo, iter)) - return FALSE; - - if (search_data->visible && - !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) - return FALSE; + /* Destroy all the menu mode widgets, if they exist. */ + if (GTK_IS_MENU (priv->popup_widget)) + gtk_combo_box_menu_destroy (combo_box); - search_data->set = TRUE; - search_data->iter = *iter; + /* Create the list mode widgets, if they don't already exist. */ + if (!GTK_IS_TREE_VIEW (priv->tree_view)) + gtk_combo_box_list_setup (combo_box); + } + else + { + /* Destroy all the list mode widgets, if they exist. */ + if (GTK_IS_TREE_VIEW (priv->tree_view)) + gtk_combo_box_list_destroy (combo_box); - return TRUE; + /* Create the menu mode widgets, if they don't already exist. */ + if (!GTK_IS_MENU (priv->popup_widget)) + gtk_combo_box_menu_setup (combo_box, TRUE); } - - if (gtk_tree_path_compare (path, search_data->path) == 0) - search_data->found = TRUE; - - return FALSE; + + gtk_widget_style_get (GTK_WIDGET (combo_box), + "shadow-type", &priv->shadow_type, + NULL); } -static gboolean -tree_next (GtkComboBox *combo, - GtkTreeModel *model, - GtkTreeIter *iter, - GtkTreeIter *next, - gboolean visible) +static void +gtk_combo_box_style_updated (GtkWidget *widget) { - SearchData search_data; - - search_data.combo = combo; - search_data.path = gtk_tree_model_get_path (model, iter); - search_data.visible = visible; - search_data.found = FALSE; - search_data.set = FALSE; + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + GtkComboBoxPrivate *priv = combo_box->priv; + GtkWidget *child; - gtk_tree_model_foreach (model, tree_next_func, &search_data); - - *next = search_data.iter; + GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget); - gtk_tree_path_free (search_data.path); + gtk_combo_box_check_appearance (combo_box); - return search_data.set; -} + if (priv->tree_view && priv->cell_view) + { + GtkStyleContext *context; + GtkStateFlags state; + GdkRGBA color; -static gboolean -tree_prev_func (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data) -{ - SearchData *search_data = (SearchData *)data; + context = gtk_widget_get_style_context (widget); + state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_background_color (context, state, &color); - if (gtk_tree_path_compare (path, search_data->path) == 0) - { - search_data->found = TRUE; - return TRUE; + gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color); } - - if (!tree_column_row_is_sensitive (search_data->combo, iter)) - return FALSE; - - if (search_data->visible && - !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) - return FALSE; - - search_data->set = TRUE; - search_data->iter = *iter; - - return FALSE; -} - -static gboolean -tree_prev (GtkComboBox *combo, - GtkTreeModel *model, - GtkTreeIter *iter, - GtkTreeIter *prev, - gboolean visible) -{ - SearchData search_data; - - search_data.combo = combo; - search_data.path = gtk_tree_model_get_path (model, iter); - search_data.visible = visible; - search_data.found = FALSE; - search_data.set = FALSE; - - gtk_tree_model_foreach (model, tree_prev_func, &search_data); - - *prev = search_data.iter; - - gtk_tree_path_free (search_data.path); - return search_data.set; + child = gtk_bin_get_child (GTK_BIN (combo_box)); + if (GTK_IS_ENTRY (child)) + g_object_set (child, "shadow-type", + GTK_SHADOW_NONE == priv->shadow_type ? + GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL); } -static gboolean -tree_last_func (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data) +static void +gtk_combo_box_button_toggled (GtkWidget *widget, + gpointer data) { - SearchData *search_data = (SearchData *)data; + GtkComboBox *combo_box = GTK_COMBO_BOX (data); - if (!tree_column_row_is_sensitive (search_data->combo, iter)) - return FALSE; - - /* Note that we rely on the fact that collapsed rows don't have nodes - */ - if (search_data->visible && - !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) - return FALSE; - - search_data->set = TRUE; - search_data->iter = *iter; - - return FALSE; + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) + { + if (!combo_box->priv->popup_in_progress) + gtk_combo_box_popup (combo_box); + } + else + gtk_combo_box_popdown (combo_box); } -static gboolean -tree_last (GtkComboBox *combo, - GtkTreeModel *model, - GtkTreeIter *last, - gboolean visible) +static void +gtk_combo_box_add (GtkContainer *container, + GtkWidget *widget) { - SearchData search_data; + GtkComboBox *combo_box = GTK_COMBO_BOX (container); + GtkComboBoxPrivate *priv = combo_box->priv; - search_data.combo = combo; - search_data.visible = visible; - search_data.set = FALSE; + if (priv->has_entry && !GTK_IS_ENTRY (widget)) + { + g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry " + "(need an instance of GtkEntry or of a subclass)", + G_OBJECT_TYPE_NAME (widget)); + return; + } - gtk_tree_model_foreach (model, tree_last_func, &search_data); + if (priv->cell_view && + gtk_widget_get_parent (priv->cell_view)) + { + gtk_widget_unparent (priv->cell_view); + _gtk_bin_set_child (GTK_BIN (container), NULL); + gtk_widget_queue_resize (GTK_WIDGET (container)); + } - *last = search_data.iter; + gtk_widget_set_parent (widget, GTK_WIDGET (container)); + _gtk_bin_set_child (GTK_BIN (container), widget); - return search_data.set; -} + if (priv->cell_view && + widget != priv->cell_view) + { + /* since the cell_view was unparented, it's gone now */ + priv->cell_view = NULL; + if (!priv->tree_view && priv->separator) + { + gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)), + priv->separator); + priv->separator = NULL; -static gboolean -tree_first_func (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer data) -{ - SearchData *search_data = (SearchData *)data; + gtk_widget_queue_resize (GTK_WIDGET (container)); + } + else if (priv->cell_view_frame) + { + gtk_widget_unparent (priv->cell_view_frame); + priv->cell_view_frame = NULL; + priv->box = NULL; + } + } - if (!tree_column_row_is_sensitive (search_data->combo, iter)) - return FALSE; - - if (search_data->visible && - !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) - return FALSE; - - search_data->set = TRUE; - search_data->iter = *iter; - - return TRUE; + if (priv->has_entry) + { + /* this flag is a hack to tell the entry to fill its allocation. + */ + _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE); + + g_signal_connect (widget, "changed", + G_CALLBACK (gtk_combo_box_entry_contents_changed), + combo_box); + + gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame); + } } -static gboolean -tree_first (GtkComboBox *combo, - GtkTreeModel *model, - GtkTreeIter *first, - gboolean visible) +static void +gtk_combo_box_remove (GtkContainer *container, + GtkWidget *widget) { - SearchData search_data; - - search_data.combo = combo; - search_data.visible = visible; - search_data.set = FALSE; + GtkComboBox *combo_box = GTK_COMBO_BOX (container); + GtkComboBoxPrivate *priv = combo_box->priv; + GtkTreePath *path; + gboolean appears_as_list; - gtk_tree_model_foreach (model, tree_first_func, &search_data); - - *first = search_data.iter; + if (priv->has_entry) + { + GtkWidget *child_widget; - return search_data.set; -} + child_widget = gtk_bin_get_child (GTK_BIN (container)); + if (widget && widget == child_widget) + { + g_signal_handlers_disconnect_by_func (widget, + gtk_combo_box_entry_contents_changed, + container); + _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE); + } + } -static gboolean -gtk_combo_box_scroll_event (GtkWidget *widget, - GdkEventScroll *event) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); - gboolean found; - GtkTreeIter iter; - GtkTreeIter new_iter; + if (widget == priv->cell_view) + priv->cell_view = NULL; - if (!gtk_combo_box_get_active_iter (combo_box, &iter)) - return TRUE; - - if (event->direction == GDK_SCROLL_UP) - found = tree_prev (combo_box, combo_box->priv->model, - &iter, &new_iter, FALSE); - else - found = tree_next (combo_box, combo_box->priv->model, - &iter, &new_iter, FALSE); - - if (found) - gtk_combo_box_set_active_iter (combo_box, &new_iter); + gtk_widget_unparent (widget); + _gtk_bin_set_child (GTK_BIN (container), NULL); - return TRUE; -} + if (gtk_widget_in_destruction (GTK_WIDGET (combo_box))) + return; + gtk_widget_queue_resize (GTK_WIDGET (container)); -static gboolean -gtk_combo_box_mnemonic_activate (GtkWidget *widget, - gboolean group_cycling) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + if (!priv->tree_view) + appears_as_list = FALSE; + else + appears_as_list = TRUE; + + if (appears_as_list) + gtk_combo_box_list_destroy (combo_box); + else if (GTK_IS_MENU (priv->popup_widget)) + { + gtk_combo_box_menu_destroy (combo_box); + gtk_menu_detach (GTK_MENU (priv->popup_widget)); + priv->popup_widget = NULL; + } - if (combo_box->priv->has_entry) + if (!priv->cell_view) { - GtkWidget* child; + priv->cell_view = gtk_cell_view_new (); + gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container)); + _gtk_bin_set_child (GTK_BIN (container), priv->cell_view); - child = gtk_bin_get_child (GTK_BIN (combo_box)); - if (child) - gtk_widget_grab_focus (child); + gtk_widget_show (priv->cell_view); + gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), + priv->model); } - else - gtk_widget_grab_focus (combo_box->priv->button); - return TRUE; -} -static void -gtk_combo_box_grab_focus (GtkWidget *widget) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + if (appears_as_list) + gtk_combo_box_list_setup (combo_box); + else + gtk_combo_box_menu_setup (combo_box, TRUE); - if (combo_box->priv->has_entry) + if (gtk_tree_row_reference_valid (priv->active_row)) { - GtkWidget *child; - - child = gtk_bin_get_child (GTK_BIN (combo_box)); - if (child) - gtk_widget_grab_focus (child); + path = gtk_tree_row_reference_get_path (priv->active_row); + gtk_combo_box_set_active_internal (combo_box, path); + gtk_tree_path_free (path); } else - gtk_widget_grab_focus (combo_box->priv->button); + gtk_combo_box_set_active_internal (combo_box, NULL); } static void -gtk_combo_box_state_changed (GtkWidget *widget, - GtkStateType previous) +gtk_combo_box_menu_show (GtkWidget *menu, + gpointer user_data) { - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); GtkComboBoxPrivate *priv = combo_box->priv; - if (gtk_widget_get_realized (widget)) - { - if (priv->tree_view && priv->cell_view) - { - GtkStyleContext *context; - GtkStateFlags state; - GdkRGBA *color; + gtk_combo_box_child_show (menu, user_data); - context = gtk_widget_get_style_context (widget); - state = gtk_widget_get_state_flags (widget); + priv->popup_in_progress = TRUE; + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), + TRUE); + priv->popup_in_progress = FALSE; +} - gtk_style_context_get (context, state, - "background-color", &color, - NULL); +static void +gtk_combo_box_menu_hide (GtkWidget *menu, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); - gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color); - gdk_rgba_free (color); - } - } + gtk_combo_box_child_hide (menu,user_data); - gtk_widget_queue_draw (widget); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button), + FALSE); } static void -gtk_combo_box_button_state_changed (GtkWidget *widget, - GtkStateType previous, - gpointer data) +gtk_combo_box_detacher (GtkWidget *widget, + GtkMenu *menu) { - GtkComboBox *combo_box = GTK_COMBO_BOX (data); + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); GtkComboBoxPrivate *priv = combo_box->priv; - if (gtk_widget_get_realized (widget)) - { - if (!priv->tree_view && priv->cell_view) - { - if ((gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE) != - (gtk_widget_get_state (priv->cell_view) == GTK_STATE_INSENSITIVE)) - gtk_widget_set_sensitive (priv->cell_view, gtk_widget_get_sensitive (widget)); - - gtk_widget_set_state (priv->cell_view, - gtk_widget_get_state (widget)); - } - } + g_return_if_fail (priv->popup_widget == (GtkWidget *) menu); - gtk_widget_queue_draw (widget); + g_signal_handlers_disconnect_by_func (menu->priv->toplevel, + gtk_combo_box_menu_show, + combo_box); + g_signal_handlers_disconnect_by_func (menu->priv->toplevel, + gtk_combo_box_menu_hide, + combo_box); + + priv->popup_widget = NULL; } static void -gtk_combo_box_check_appearance (GtkComboBox *combo_box) +gtk_combo_box_set_popup_widget (GtkComboBox *combo_box, + GtkWidget *popup) { GtkComboBoxPrivate *priv = combo_box->priv; - gboolean appears_as_list; - - /* if wrap_width > 0, then we are in grid-mode and forced to use - * unix style - */ - if (priv->wrap_width) - appears_as_list = FALSE; - else - gtk_widget_style_get (GTK_WIDGET (combo_box), - "appears-as-list", &appears_as_list, - NULL); - if (appears_as_list) + if (GTK_IS_MENU (priv->popup_widget)) { - /* Destroy all the menu mode widgets, if they exist. */ - if (GTK_IS_MENU (priv->popup_widget)) - gtk_combo_box_menu_destroy (combo_box); - - /* Create the list mode widgets, if they don't already exist. */ - if (!GTK_IS_TREE_VIEW (priv->tree_view)) - gtk_combo_box_list_setup (combo_box); + gtk_menu_detach (GTK_MENU (priv->popup_widget)); + priv->popup_widget = NULL; } - else + else if (priv->popup_widget) { - /* Destroy all the list mode widgets, if they exist. */ - if (GTK_IS_TREE_VIEW (priv->tree_view)) - gtk_combo_box_list_destroy (combo_box); - - /* Create the menu mode widgets, if they don't already exist. */ - if (!GTK_IS_MENU (priv->popup_widget)) - gtk_combo_box_menu_setup (combo_box, TRUE); + gtk_container_remove (GTK_CONTAINER (priv->scrolled_window), + priv->popup_widget); + g_object_unref (priv->popup_widget); + priv->popup_widget = NULL; } - gtk_widget_style_get (GTK_WIDGET (combo_box), - "shadow-type", &priv->shadow_type, - NULL); -} + if (GTK_IS_MENU (popup)) + { + if (priv->popup_window) + { + gtk_widget_destroy (priv->popup_window); + priv->popup_window = NULL; + } -static void -gtk_combo_box_style_updated (GtkWidget *widget) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); - GtkComboBoxPrivate *priv = combo_box->priv; - GtkWidget *child; + priv->popup_widget = popup; - gtk_combo_box_check_appearance (combo_box); + /* + * Note that we connect to show/hide on the toplevel, not the + * menu itself, since the menu is not shown/hidden when it is + * popped up while torn-off. + */ + g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show", + G_CALLBACK (gtk_combo_box_menu_show), combo_box); + g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide", + G_CALLBACK (gtk_combo_box_menu_hide), combo_box); - if (priv->tree_view && priv->cell_view) + gtk_menu_attach_to_widget (GTK_MENU (popup), + GTK_WIDGET (combo_box), + gtk_combo_box_detacher); + } + else { - GtkStyleContext *context; - GdkRGBA *color; + if (!priv->popup_window) + { + GtkWidget *toplevel; - context = gtk_widget_get_style_context (widget); - gtk_style_context_get (context, 0, - "background-color", &color, - NULL); + priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP); + gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window"); - gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color); - gdk_rgba_free (color); - } + gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window), + GDK_WINDOW_TYPE_HINT_COMBO); - child = gtk_bin_get_child (GTK_BIN (combo_box)); - if (GTK_IS_ENTRY (child)) - g_object_set (child, "shadow-type", - GTK_SHADOW_NONE == priv->shadow_type ? - GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL); -} + g_signal_connect (GTK_WINDOW (priv->popup_window),"show", + G_CALLBACK (gtk_combo_box_child_show), + combo_box); + g_signal_connect (GTK_WINDOW (priv->popup_window),"hide", + G_CALLBACK (gtk_combo_box_child_hide), + combo_box); -static void -gtk_combo_box_destroy (GtkWidget *widget) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box)); + if (GTK_IS_WINDOW (toplevel)) + { + gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), + GTK_WINDOW (priv->popup_window)); + gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window), + GTK_WINDOW (toplevel)); + } - if (combo_box->priv->popup_idle_id > 0) - { - g_source_remove (combo_box->priv->popup_idle_id); - combo_box->priv->popup_idle_id = 0; - } + gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE); + gtk_window_set_screen (GTK_WINDOW (priv->popup_window), + gtk_widget_get_screen (GTK_WIDGET (combo_box))); - gtk_combo_box_popdown (combo_box); + priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL); - if (combo_box->priv->row_separator_destroy) - combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), + GTK_POLICY_NEVER, + GTK_POLICY_NEVER); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), + GTK_SHADOW_IN); - combo_box->priv->row_separator_func = NULL; - combo_box->priv->row_separator_data = NULL; - combo_box->priv->row_separator_destroy = NULL; + gtk_widget_show (priv->scrolled_window); - GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget); - combo_box->priv->cell_view = NULL; + gtk_container_add (GTK_CONTAINER (priv->popup_window), + priv->scrolled_window); + } + + gtk_container_add (GTK_CONTAINER (priv->scrolled_window), + popup); + + gtk_widget_show (popup); + g_object_ref (popup); + priv->popup_widget = popup; + } } static void -get_widget_border (GtkWidget *widget, - GtkBorder *border) +get_widget_padding_and_border (GtkWidget *widget, + GtkBorder *padding) { GtkStyleContext *context; - GtkBorder *border_width; + GtkStateFlags state; + GtkBorder tmp; context = gtk_widget_get_style_context (widget); + state = gtk_style_context_get_state (context); - gtk_style_context_get (context, - gtk_widget_get_state_flags (widget), - "border-width", &border_width, - NULL); + gtk_style_context_get_padding (context, state, padding); + gtk_style_context_get_border (context, state, &tmp); - *border = *border_width; - gtk_border_free (border_width); + padding->top += tmp.top; + padding->right += tmp.right; + padding->bottom += tmp.bottom; + padding->left += tmp.left; } -static void -gtk_combo_box_get_preferred_width (GtkWidget *widget, - gint *minimum_size, - gint *natural_size) +static void +gtk_combo_box_menu_position_below (GtkMenu *menu, + gint *x, + gint *y, + gint *push_in, + gpointer user_data) { - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); - GtkComboBoxPrivate *priv = combo_box->priv; - gint focus_width, focus_pad; - gint font_size, arrow_size; - PangoContext *context; - PangoFontMetrics *metrics; - PangoFontDescription *font_desc; - GtkWidget *child; - gint minimum_width = 0, natural_width = 0; - gint child_min, child_nat; - GtkStyleContext *style_context; - GtkStateFlags state; - GtkBorder *border; + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + GtkAllocation child_allocation; + gint sx, sy; + GtkWidget *child; + GtkRequisition req; + GdkScreen *screen; + gint monitor_num; + GdkRectangle monitor; + GtkBorder padding; - child = gtk_bin_get_child (GTK_BIN (widget)); - - /* common */ - gtk_widget_get_preferred_width (child, &child_min, &child_nat); + /* FIXME: is using the size request here broken? */ + child = gtk_bin_get_child (GTK_BIN (combo_box)); - gtk_widget_style_get (GTK_WIDGET (widget), - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - "arrow-size", &arrow_size, - NULL); + sx = sy = 0; - style_context = gtk_widget_get_style_context (widget); - state = gtk_widget_get_state_flags (widget); + gtk_widget_get_allocation (child, &child_allocation); - gtk_style_context_get (style_context, state, - "font", &font_desc, - "border-width", &border, - NULL); + if (!gtk_widget_get_has_window (child)) + { + sx += child_allocation.x; + sy += child_allocation.y; + } - context = gtk_widget_get_pango_context (GTK_WIDGET (widget)); - metrics = pango_context_get_metrics (context, font_desc, - pango_context_get_language (context)); - font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + - pango_font_metrics_get_descent (metrics)); - pango_font_metrics_unref (metrics); - pango_font_description_free (font_desc); + gdk_window_get_root_coords (gtk_widget_get_window (child), + sx, sy, &sx, &sy); + get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding); - arrow_size = MAX (arrow_size, font_size); + if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL) + sx += padding.left; + else + sx -= padding.left; - gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size); + if (combo_box->priv->popup_fixed_width) + gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL); + else + gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req); - if (!priv->tree_view) - { - /* menu mode */ - if (priv->cell_view) - { - gint sep_width, arrow_width; - gint border_width, xpad; - GtkBorder button_border; + if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR) + *x = sx; + else + *x = sx + child_allocation.width - req.width; + *y = sy; - border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box)); - get_widget_border (priv->button, &button_border); + screen = gtk_widget_get_screen (GTK_WIDGET (combo_box)); + monitor_num = gdk_screen_get_monitor_at_window (screen, + gtk_widget_get_window (GTK_WIDGET (combo_box))); + gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor); - gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL); - gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL); + if (*x < monitor.x) + *x = monitor.x; + else if (*x + req.width > monitor.x + monitor.width) + *x = monitor.x + monitor.width - req.width; - xpad = 2 * (border_width + focus_width + focus_pad) + - button_border.left + button_border.right; + if (monitor.y + monitor.height - *y - child_allocation.height >= req.height) + *y += child_allocation.height; + else if (*y - monitor.y >= req.height) + *y -= req.height; + else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y) + *y += child_allocation.height; + else + *y -= req.height; - minimum_width = child_min + sep_width + arrow_width + xpad; - natural_width = child_nat + sep_width + arrow_width + xpad; - } - else - { - gint but_width, but_nat_width; + *push_in = FALSE; +} - gtk_widget_get_preferred_width (priv->button, - &but_width, &but_nat_width); +static void +gtk_combo_box_menu_position_over (GtkMenu *menu, + gint *x, + gint *y, + gboolean *push_in, + gpointer user_data) +{ + GtkComboBox *combo_box; + GtkWidget *active; + GtkWidget *child; + GtkWidget *widget; + GtkAllocation allocation; + GtkAllocation child_allocation; + GList *children; + gint screen_width; + gint menu_xpos; + gint menu_ypos; + gint menu_width; - minimum_width = child_min + but_width; - natural_width = child_nat + but_nat_width; - } - } + combo_box = GTK_COMBO_BOX (user_data); + widget = GTK_WIDGET (combo_box); + + active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget)); + + gtk_widget_get_allocation (widget, &allocation); + + menu_xpos = allocation.x; + menu_ypos = allocation.y + allocation.height / 2 - 2; + + if (combo_box->priv->popup_fixed_width) + gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL); else + gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width); + + if (active != NULL) { - /* list mode */ - gint button_width, button_nat_width; + gtk_widget_get_allocation (active, &child_allocation); + menu_ypos -= child_allocation.height / 2; + } - /* sample + frame */ - minimum_width = child_min; - natural_width = child_nat; + children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children; + while (children) + { + child = children->data; - minimum_width += 2 * focus_width; - natural_width += 2 * focus_width; - - if (priv->cell_view_frame) - { - if (priv->has_frame) - { - gint border_width, xpad; - GtkBorder frame_border; + if (active == child) + break; - border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame)); - get_widget_border (priv->cell_view_frame, &frame_border); - xpad = (2 * border_width) + frame_border.left + frame_border.right; + if (gtk_widget_get_visible (child)) + { + gtk_widget_get_allocation (child, &child_allocation); - minimum_width += xpad; - natural_width += xpad; - } + menu_ypos -= child_allocation.height; } - /* the button */ - gtk_widget_get_preferred_width (priv->button, - &button_width, &button_nat_width); - - minimum_width += button_width; - natural_width += button_nat_width; + children = children->next; } - minimum_width += border->left + border->right; - natural_width += border->left + border->right; - gtk_border_free (border); + if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) + menu_xpos = menu_xpos + allocation.width - menu_width; - if (minimum_size) - *minimum_size = minimum_width; + gdk_window_get_root_coords (gtk_widget_get_window (widget), + menu_xpos, menu_ypos, + &menu_xpos, &menu_ypos); - if (natural_size) - *natural_size = natural_width; + /* Clamp the position on screen */ + screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget)); + + if (menu_xpos < 0) + menu_xpos = 0; + else if ((menu_xpos + menu_width) > screen_width) + menu_xpos -= ((menu_xpos + menu_width) - screen_width); + + *x = menu_xpos; + *y = menu_ypos; + + *push_in = TRUE; } static void -gtk_combo_box_get_preferred_height (GtkWidget *widget, - gint *minimum_size, - gint *natural_size) -{ - gint min_width; +gtk_combo_box_menu_position (GtkMenu *menu, + gint *x, + gint *y, + gint *push_in, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + GtkComboBoxPrivate *priv = combo_box->priv; + GtkWidget *menu_item; - /* Combo box is height-for-width only - * (so we always just reserve enough height for the minimum width) */ - GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL); - GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size); + if (priv->wrap_width > 0 || priv->cell_view == NULL) + gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data); + else + { + /* FIXME handle nested menus better */ + menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget)); + if (menu_item) + gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget), + menu_item); + + gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data); + } + + if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel)) + gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel), + GDK_WINDOW_TYPE_HINT_COMBO); } static void -gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget, - gint avail_size, - gint *minimum_size, - gint *natural_size) +gtk_combo_box_list_position (GtkComboBox *combo_box, + gint *x, + gint *y, + gint *width, + gint *height) { - /* Combo box is height-for-width only - * (so we assume we always reserved enough height for the minimum width) */ - GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size); -} + GtkComboBoxPrivate *priv = combo_box->priv; + GtkAllocation allocation; + GdkScreen *screen; + gint monitor_num; + GdkRectangle monitor; + GtkRequisition popup_req; + GtkPolicyType hpolicy, vpolicy; + GdkWindow *window; + /* under windows, the drop down list is as wide as the combo box itself. + see bug #340204 */ + GtkWidget *widget = GTK_WIDGET (combo_box); -static void -gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget, - gint avail_size, - gint *minimum_size, - gint *natural_size) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); - GtkComboBoxPrivate *priv = combo_box->priv; - gint focus_width, focus_pad; - gint min_height = 0, nat_height = 0; - gint size; - GtkWidget *child; - GtkBorder border; + *x = *y = 0; - gtk_widget_style_get (GTK_WIDGET (widget), - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - NULL); + gtk_widget_get_allocation (widget, &allocation); - child = gtk_bin_get_child (GTK_BIN (widget)); + if (!gtk_widget_get_has_window (widget)) + { + *x += allocation.x; + *y += allocation.y; + } - get_widget_border (widget, &border); - size = avail_size - border.left; + window = gtk_widget_get_window (widget); - if (!priv->tree_view) + gdk_window_get_root_coords (gtk_widget_get_window (widget), + *x, *y, x, y); + + *width = allocation.width; + + hpolicy = vpolicy = GTK_POLICY_NEVER; + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), + hpolicy, vpolicy); + + if (combo_box->priv->popup_fixed_width) { - /* menu mode */ - if (priv->cell_view) + gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL); + + if (popup_req.width > *width) { - /* calculate x/y padding and separator/arrow size */ - gint sep_width, arrow_width, sep_height, arrow_height; - gint border_width, xpad, ypad; - GtkBorder button_border; - - border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box)); - get_widget_border (priv->button, &button_border); - - gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL); - gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL); - gtk_widget_get_preferred_height_for_width (priv->separator, - sep_width, &sep_height, NULL); - gtk_widget_get_preferred_height_for_width (priv->arrow, - arrow_width, &arrow_height, NULL); - - xpad = 2 * (border_width + focus_width + focus_pad) + - button_border.left + button_border.right; - ypad = 2 * (border_width + focus_width + focus_pad) + - button_border.top + button_border.bottom; - - size -= sep_width + arrow_width + xpad; - - /* Get height-for-width of the child widget, usually a GtkCellArea calculating - * and fitting the whole treemodel */ - gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height); - - arrow_height = MAX (arrow_height, sep_height); - min_height = MAX (min_height, arrow_height); - nat_height = MAX (nat_height, arrow_height); - - min_height += ypad; - nat_height += ypad; + hpolicy = GTK_POLICY_ALWAYS; + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), + hpolicy, vpolicy); } - else + } + else + { + /* XXX This code depends on treeviews properly reporting their natural width + * list-mode menus won't fill up to their natural width until then */ + gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req); + + if (popup_req.width > *width) { - /* there is a custom child widget inside (no priv->cell_view) */ - gint but_width, but_height; + hpolicy = GTK_POLICY_NEVER; + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), + hpolicy, vpolicy); - gtk_widget_get_preferred_width (priv->button, &but_width, NULL); - gtk_widget_get_preferred_height_for_width (priv->button, - but_width, &but_height, NULL); + *width = popup_req.width; + } + } - size -= but_width; + *height = popup_req.height; - /* Get height-for-width of the child widget, usually a GtkCellArea calculating - * and fitting the whole treemodel */ - gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height); - - min_height = MAX (min_height, but_height); - nat_height = MAX (nat_height, but_height); - } + screen = gtk_widget_get_screen (GTK_WIDGET (combo_box)); + monitor_num = gdk_screen_get_monitor_at_window (screen, window); + gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor); + + if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL) + *x = *x + allocation.width - *width; + + if (*x < monitor.x) + *x = monitor.x; + else if (*x + *width > monitor.x + monitor.width) + *x = monitor.x + monitor.width - *width; + + if (*y + allocation.height + *height <= monitor.y + monitor.height) + *y += allocation.height; + else if (*y - *height >= monitor.y) + *y -= *height; + else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y) + { + *y += allocation.height; + *height = monitor.y + monitor.height - *y; } else { - /* list mode */ - gint but_width, but_height; - gint xpad = 0, ypad = 0; + *height = *y - monitor.y; + *y = monitor.y; + } - gtk_widget_get_preferred_width (priv->button, &but_width, NULL); - gtk_widget_get_preferred_height_for_width (priv->button, - but_width, &but_height, NULL); - - if (priv->cell_view_frame && priv->has_frame) - { - GtkBorder frame_border; - gint border_width; + if (popup_req.height > *height) + { + vpolicy = GTK_POLICY_ALWAYS; - border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame)); - get_widget_border (GTK_WIDGET (priv->cell_view_frame), &frame_border); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), + hpolicy, vpolicy); + } +} - xpad = (2 * border_width) + border.left + frame_border.right; - ypad = (2 * border_width) + border.top + frame_border.bottom; - } +static gboolean +cell_layout_is_sensitive (GtkCellLayout *layout) +{ + GList *cells, *list; + gboolean sensitive; - size -= but_width; - size -= 2 * focus_width; - size -= xpad; - - /* Get height-for-width of the child widget, usually a GtkCellArea calculating - * and fitting the whole treemodel */ - gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height); + cells = gtk_cell_layout_get_cells (layout); - min_height = MAX (min_height, but_height); - nat_height = MAX (nat_height, but_height); + sensitive = FALSE; + for (list = cells; list; list = list->next) + { + g_object_get (list->data, "sensitive", &sensitive, NULL); - min_height += ypad; - nat_height += ypad; + if (sensitive) + break; } + g_list_free (cells); - min_height += border.top + border.bottom; - nat_height += border.top + border.bottom; - - if (minimum_size) - *minimum_size = min_height; - - if (natural_size) - *natural_size = nat_height; + return sensitive; } -#define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \ - gtk_widget_get_preferred_size (combo_box->priv->button, \ - &req, NULL); \ - \ - if (is_rtl) \ - child.x = allocation->x + border.right; \ - else \ - child.x = allocation->x + allocation->width - req.width - border.left; \ - \ - child.y = allocation->y + border.top; \ - child.width = req.width; \ - child.height = allocation->height - (border.top + border.bottom); \ - child.width = MAX (1, child.width); \ - child.height = MAX (1, child.height); \ - \ - gtk_widget_size_allocate (combo_box->priv->button, &child); +static gboolean +cell_is_sensitive (GtkCellRenderer *cell, + gpointer data) +{ + gboolean *sensitive = data; + g_object_get (cell, "sensitive", sensitive, NULL); -static void -gtk_combo_box_size_allocate (GtkWidget *widget, - GtkAllocation *allocation) + return *sensitive; +} + +static gboolean +tree_column_row_is_sensitive (GtkComboBox *combo_box, + GtkTreeIter *iter) { - GtkComboBox *combo_box = GTK_COMBO_BOX (widget); GtkComboBoxPrivate *priv = combo_box->priv; - GtkWidget *child_widget; - gint focus_width, focus_pad; - GtkAllocation child; - GtkRequisition req; - gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL; - GtkBorder border; - - gtk_widget_set_allocation (widget, allocation); - child_widget = gtk_bin_get_child (GTK_BIN (widget)); - get_widget_border (widget, &border); - gtk_widget_style_get (widget, - "focus-line-width", &focus_width, - "focus-padding", &focus_pad, - NULL); - - if (!priv->tree_view) + if (priv->row_separator_func) { - if (priv->cell_view) - { - GtkBorder button_border; - gint width; - guint border_width; + if (priv->row_separator_func (priv->model, iter, + priv->row_separator_data)) + return FALSE; + } - /* menu mode */ - allocation->x += border.left; - allocation->y += border.top; - allocation->width -= border.left + border.right; - allocation->height -= border.top + border.bottom; + if (priv->area) + { + gboolean sensitive; - gtk_widget_size_allocate (priv->button, allocation); + gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE); - /* set some things ready */ - border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button)); - get_widget_border (priv->button, &button_border); + sensitive = FALSE; - child.x = allocation->x; - child.y = allocation->y; - width = allocation->width; - child.height = allocation->height; - - if (!priv->is_cell_renderer) - { - child.x += border_width + button_border.left + focus_width + focus_pad; - child.y += border_width + button_border.top + focus_width + focus_pad; - width -= (2 * (border_width + focus_width + focus_pad)) + - button_border.left + button_border.right; - child.height -= (2 * (border_width + focus_width + focus_pad)) + - button_border.top + button_border.bottom; - } - - /* handle the children */ - gtk_widget_get_preferred_size (priv->arrow, &req, NULL); - child.width = req.width; - if (!is_rtl) - child.x += width - req.width; - child.width = MAX (1, child.width); - child.height = MAX (1, child.height); - gtk_widget_size_allocate (priv->arrow, &child); - if (is_rtl) - child.x += req.width; - gtk_widget_get_preferred_size (priv->separator, &req, NULL); - child.width = req.width; - if (!is_rtl) - child.x -= req.width; - child.width = MAX (1, child.width); - child.height = MAX (1, child.height); - gtk_widget_size_allocate (priv->separator, &child); + gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive); - if (is_rtl) - { - child.x += req.width; - child.width = allocation->x + allocation->width - - (border_width + button_border.right + focus_width + focus_pad) - - child.x; - } - else - { - child.width = child.x; - child.x = allocation->x - + border_width + button_border.left + focus_width + focus_pad; - child.width -= child.x; - } + return sensitive; + } - if (gtk_widget_get_visible (priv->popup_widget)) - { - gint width, menu_width; + return TRUE; +} - if (priv->wrap_width == 0) - { - GtkAllocation combo_box_allocation; +static void +update_menu_sensitivity (GtkComboBox *combo_box, + GtkWidget *menu) +{ + GtkComboBoxPrivate *priv = combo_box->priv; + GList *children, *child; + GtkWidget *item, *submenu, *separator; + GtkWidget *cell_view; + gboolean sensitive; - gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation); - width = combo_box_allocation.width; - gtk_widget_set_size_request (priv->popup_widget, -1, -1); + if (!priv->model) + return; - if (combo_box->priv->popup_fixed_width) - gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL); - else - gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width); + children = gtk_container_get_children (GTK_CONTAINER (menu)); - gtk_widget_set_size_request (priv->popup_widget, - MAX (width, menu_width), -1); - } + for (child = children; child; child = child->next) + { + item = GTK_WIDGET (child->data); + cell_view = gtk_bin_get_child (GTK_BIN (item)); - /* reposition the menu after giving it a new width */ - gtk_menu_reposition (GTK_MENU (priv->popup_widget)); - } + if (!GTK_IS_CELL_VIEW (cell_view)) + continue; - child.width = MAX (1, child.width); - child.height = MAX (1, child.height); - gtk_widget_size_allocate (child_widget, &child); + submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)); + if (submenu != NULL) + { + gtk_widget_set_sensitive (item, TRUE); + update_menu_sensitivity (combo_box, submenu); } else { - GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON + sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view)); - if (is_rtl) - child.x = allocation->x + req.width + border.right; + if (menu != priv->popup_widget && child == children) + { + separator = GTK_WIDGET (child->next->data); + g_object_set (item, "visible", sensitive, NULL); + g_object_set (separator, "visible", sensitive, NULL); + } else - child.x = allocation->x + border.left; - child.y = allocation->y + border.top; - child.width = allocation->width - req.width - (border.left + border.right); - child.width = MAX (1, child.width); - child.height = MAX (1, child.height); - gtk_widget_size_allocate (child_widget, &child); + gtk_widget_set_sensitive (item, sensitive); } } - else - { - /* list mode */ - guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget)); - /* button */ - GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON + g_list_free (children); +} - /* frame */ - if (is_rtl) - child.x = allocation->x + req.width; - else - child.x = allocation->x; +static void +gtk_combo_box_menu_popup (GtkComboBox *combo_box, + guint button, + guint32 activate_time) +{ + GtkComboBoxPrivate *priv = combo_box->priv; + GtkTreePath *path; + gint active_item; + gint width, min_width, nat_width; - child.y = allocation->y; - child.width = allocation->width - req.width; - child.height = allocation->height; + update_menu_sensitivity (combo_box, priv->popup_widget); - if (priv->cell_view_frame) - { - child.x += border.left + border_width; - child.y += border.top + border_width; - child.width = MAX (1, child.width - (2 * border_width) - (border.left + border.right)); - child.height = MAX (1, child.height - (2 * border_width) - (border.top + border.bottom)); - gtk_widget_size_allocate (priv->cell_view_frame, &child); + active_item = -1; + if (gtk_tree_row_reference_valid (priv->active_row)) + { + path = gtk_tree_row_reference_get_path (priv->active_row); + active_item = gtk_tree_path_get_indices (path)[0]; + gtk_tree_path_free (path); - /* the sample */ - if (priv->has_frame) - { - GtkBorder frame_border; + if (priv->add_tearoffs) + active_item++; + } - border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame)); - get_widget_border (priv->cell_view_frame, &frame_border); + /* FIXME handle nested menus better */ + gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item); - child.x += border_width + frame_border.left; - child.y += border_width + frame_border.right; - child.width -= (2 * border_width) + frame_border.left + frame_border.right; - child.height -= (2 * border_width) + frame_border.top + frame_border.bottom; - } - } - else - { - child.x += border.left + border_width; - child.y += border.top + border_width; - child.width -= (2 * border_width) - (border.left + border.right); - child.height -= (2 * border_width) - (border.top + border.bottom); - } + if (priv->wrap_width == 0) + { + GtkAllocation allocation; - if (gtk_widget_get_visible (priv->popup_window)) - { - gint x, y, width, height; - gtk_combo_box_list_position (combo_box, &x, &y, &width, &height); - gtk_window_move (GTK_WINDOW (priv->popup_window), x, y); - gtk_widget_set_size_request (priv->popup_window, width, height); - } + gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation); + width = allocation.width; + gtk_widget_set_size_request (priv->popup_widget, -1, -1); + gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width); - - child.width = MAX (1, child.width); - child.height = MAX (1, child.height); - - gtk_widget_size_allocate (child_widget, &child); - } -} + if (combo_box->priv->popup_fixed_width) + width = MAX (width, min_width); + else + width = MAX (width, nat_width); -#undef GTK_COMBO_BOX_ALLOCATE_BUTTON + gtk_widget_set_size_request (priv->popup_widget, width, -1); + } -/****************************************************** - * GtkContainerClass * - ******************************************************/ + gtk_menu_popup (GTK_MENU (priv->popup_widget), + NULL, NULL, + gtk_combo_box_menu_position, combo_box, + button, activate_time); +} -static void -gtk_combo_box_add (GtkContainer *container, - GtkWidget *widget) +static gboolean +popup_grab_on_window (GdkWindow *window, + GdkDevice *keyboard, + GdkDevice *pointer, + guint32 activate_time) { - GtkComboBox *combo_box = GTK_COMBO_BOX (container); - GtkComboBoxPrivate *priv = combo_box->priv; + if (keyboard && + gdk_device_grab (keyboard, window, + GDK_OWNERSHIP_WINDOW, TRUE, + GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, + NULL, activate_time) != GDK_GRAB_SUCCESS) + return FALSE; - if (priv->has_entry && !GTK_IS_ENTRY (widget)) + if (pointer && + gdk_device_grab (pointer, window, + GDK_OWNERSHIP_WINDOW, TRUE, + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | + GDK_POINTER_MOTION_MASK, + NULL, activate_time) != GDK_GRAB_SUCCESS) { - g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry " - "(need an instance of GtkEntry or of a subclass)", - G_OBJECT_TYPE_NAME (widget)); - return; - } + if (keyboard) + gdk_device_ungrab (keyboard, activate_time); - if (priv->cell_view && - gtk_widget_get_parent (priv->cell_view)) - { - gtk_container_remove (container, priv->cell_view); - gtk_widget_queue_resize (GTK_WIDGET (container)); + return FALSE; } - gtk_widget_set_parent (widget, GTK_WIDGET (container)); - _gtk_bin_set_child (GTK_BIN (container), widget); - - if (priv->cell_view && - widget != priv->cell_view) - { - /* since the cell_view was unparented, it's gone now */ - priv->cell_view = NULL; + return TRUE; +} - if (!priv->tree_view && priv->separator) - { - gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)), - priv->separator); - priv->separator = NULL; +static gboolean +gtk_combo_box_grab_broken_event (GtkWidget *widget, + GdkEventGrabBroken *event, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); - gtk_widget_queue_resize (GTK_WIDGET (container)); - } - else if (priv->cell_view_frame) - { - gtk_widget_unparent (priv->cell_view_frame); - priv->cell_view_frame = NULL; - priv->box = NULL; - } - } + if (event->grab_window == NULL) + gtk_combo_box_popdown (combo_box); - if (priv->has_entry) - { - /* this flag is a hack to tell the entry to fill its allocation. - */ - _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE); + return TRUE; +} - g_signal_connect (widget, "changed", - G_CALLBACK (gtk_combo_box_entry_contents_changed), - combo_box); +/** + * gtk_combo_box_popup: + * @combo_box: a #GtkComboBox + * + * Pops up the menu or dropdown list of @combo_box. + * + * This function is mostly intended for use by accessibility technologies; + * applications should have little use for it. + * + * Since: 2.4 + */ +void +gtk_combo_box_popup (GtkComboBox *combo_box) +{ + g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); - gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame); - } + g_signal_emit (combo_box, combo_box_signals[POPUP], 0); } -static void -gtk_combo_box_remove (GtkContainer *container, - GtkWidget *widget) +/** + * gtk_combo_box_popup_for_device: + * @combo_box: a #GtkComboBox + * @device: a #GdkDevice + * + * Pops up the menu or dropdown list of @combo_box, the popup window + * will be grabbed so only @device and its associated pointer/keyboard + * are the only #GdkDevices able to send events to it. + * + * Since: 3.0 + **/ +void +gtk_combo_box_popup_for_device (GtkComboBox *combo_box, + GdkDevice *device) { - GtkComboBox *combo_box = GTK_COMBO_BOX (container); GtkComboBoxPrivate *priv = combo_box->priv; - GtkTreePath *path; - gboolean appears_as_list; - - if (priv->has_entry) - { - GtkWidget *child_widget; - - child_widget = gtk_bin_get_child (GTK_BIN (container)); - if (widget && widget == child_widget && GTK_IS_ENTRY (widget)) - { - g_signal_handlers_disconnect_by_func (widget, - gtk_combo_box_entry_contents_changed, - container); + gint x, y, width, height; + GtkTreePath *path = NULL, *ppath; + GtkWidget *toplevel; + GdkDevice *keyboard, *pointer; + guint32 time; - _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE); - } - } + g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); + g_return_if_fail (GDK_IS_DEVICE (device)); - if (widget == priv->cell_view) - priv->cell_view = NULL; + if (!gtk_widget_get_realized (GTK_WIDGET (combo_box))) + return; - GTK_CONTAINER_CLASS (gtk_combo_box_parent_class)->remove (container, widget); + if (gtk_widget_get_mapped (priv->popup_widget)) + return; - if (gtk_widget_in_destruction (GTK_WIDGET (combo_box))) + if (priv->grab_pointer && priv->grab_keyboard) return; - gtk_widget_queue_resize (GTK_WIDGET (container)); + time = gtk_get_current_event_time (); - if (!priv->tree_view) - appears_as_list = FALSE; + if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD) + { + keyboard = device; + pointer = gdk_device_get_associated_device (device); + } else - appears_as_list = TRUE; - - if (appears_as_list) - gtk_combo_box_list_destroy (combo_box); - else if (GTK_IS_MENU (priv->popup_widget)) { - gtk_combo_box_menu_destroy (combo_box); - gtk_menu_detach (GTK_MENU (priv->popup_widget)); - priv->popup_widget = NULL; + pointer = device; + keyboard = gdk_device_get_associated_device (device); } - if (!priv->cell_view) + if (GTK_IS_MENU (priv->popup_widget)) { - priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL); - gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE); - - GTK_CONTAINER_CLASS (gtk_combo_box_parent_class)->add (container, priv->cell_view); - - gtk_widget_show (priv->cell_view); - gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model); + gtk_combo_box_menu_popup (combo_box, + priv->activate_button, + priv->activate_time); + return; } - if (appears_as_list) - gtk_combo_box_list_setup (combo_box); - else - gtk_combo_box_menu_setup (combo_box, TRUE); + toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box)); + if (GTK_IS_WINDOW (toplevel)) + gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), + GTK_WINDOW (priv->popup_window)); + + gtk_widget_show_all (priv->scrolled_window); + gtk_combo_box_list_position (combo_box, &x, &y, &width, &height); + + gtk_widget_set_size_request (priv->popup_window, width, height); + gtk_window_move (GTK_WINDOW (priv->popup_window), x, y); if (gtk_tree_row_reference_valid (priv->active_row)) { path = gtk_tree_row_reference_get_path (priv->active_row); - gtk_combo_box_set_active_internal (combo_box, path); - gtk_tree_path_free (path); + ppath = gtk_tree_path_copy (path); + if (gtk_tree_path_up (ppath)) + gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view), + ppath); + gtk_tree_path_free (ppath); } - else - gtk_combo_box_set_active_internal (combo_box, NULL); -} + gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view), + TRUE); -static void -gtk_combo_box_forall (GtkContainer *container, - gboolean include_internals, - GtkCallback callback, - gpointer callback_data) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (container); - GtkComboBoxPrivate *priv = combo_box->priv; - GtkWidget *child; + /* popup */ + gtk_widget_show (priv->popup_window); - if (include_internals) + if (path) { - if (priv->button) - (* callback) (priv->button, callback_data); - if (priv->cell_view_frame) - (* callback) (priv->cell_view_frame, callback_data); + gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view), + path, NULL, FALSE); + gtk_tree_path_free (path); } - child = gtk_bin_get_child (GTK_BIN (container)); - if (child) - (* callback) (child, callback_data); -} - + gtk_widget_grab_focus (priv->popup_window); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), + TRUE); -/****************************************************** - * GtkComboBoxClass (binding methods) * - ******************************************************/ -static void -gtk_combo_box_real_move_active (GtkComboBox *combo_box, - GtkScrollType scroll) -{ - GtkTreeIter iter; - GtkTreeIter new_iter; - gboolean active_iter; - gboolean found; + if (!gtk_widget_has_focus (priv->tree_view)) + gtk_widget_grab_focus (priv->tree_view); - if (!combo_box->priv->model) + if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window), + keyboard, pointer, time)) { - gtk_widget_error_bell (GTK_WIDGET (combo_box)); + gtk_widget_hide (priv->popup_window); return; } - active_iter = gtk_combo_box_get_active_iter (combo_box, &iter); + gtk_device_grab_add (priv->popup_window, pointer, TRUE); + priv->grab_pointer = pointer; + priv->grab_keyboard = keyboard; - switch (scroll) - { - case GTK_SCROLL_STEP_BACKWARD: - case GTK_SCROLL_STEP_UP: - case GTK_SCROLL_STEP_LEFT: - if (active_iter) - { - found = tree_prev (combo_box, combo_box->priv->model, - &iter, &new_iter, FALSE); - break; - } - /* else fall through */ - - case GTK_SCROLL_PAGE_FORWARD: - case GTK_SCROLL_PAGE_DOWN: - case GTK_SCROLL_PAGE_RIGHT: - case GTK_SCROLL_END: - found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE); - break; - - case GTK_SCROLL_STEP_FORWARD: - case GTK_SCROLL_STEP_DOWN: - case GTK_SCROLL_STEP_RIGHT: - if (active_iter) - { - found = tree_next (combo_box, combo_box->priv->model, - &iter, &new_iter, FALSE); - break; - } - /* else fall through */ - - case GTK_SCROLL_PAGE_BACKWARD: - case GTK_SCROLL_PAGE_UP: - case GTK_SCROLL_PAGE_LEFT: - case GTK_SCROLL_START: - found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE); - break; - - default: - return; - } - - if (found && active_iter) - { - GtkTreePath *old_path; - GtkTreePath *new_path; - - old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter); - new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter); - - if (gtk_tree_path_compare (old_path, new_path) == 0) - found = FALSE; - - gtk_tree_path_free (old_path); - gtk_tree_path_free (new_path); - } - - if (found) - { - gtk_combo_box_set_active_iter (combo_box, &new_iter); - } - else - { - gtk_widget_error_bell (GTK_WIDGET (combo_box)); - } + g_signal_connect (priv->popup_window, + "grab-broken-event", + G_CALLBACK (gtk_combo_box_grab_broken_event), + combo_box); } static void @@ -2612,612 +2512,607 @@ gtk_combo_box_real_popdown (GtkComboBox *combo_box) return FALSE; } -/****************************************************** - * GtkTreeModel callbacks * - ******************************************************/ -static void -gtk_combo_box_model_row_inserted (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer user_data) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); - - if (combo_box->priv->tree_view) - gtk_combo_box_list_popup_resize (combo_box); -} - -static void -gtk_combo_box_model_row_deleted (GtkTreeModel *model, - GtkTreePath *path, - gpointer user_data) +/** + * gtk_combo_box_popdown: + * @combo_box: a #GtkComboBox + * + * Hides the menu or dropdown list of @combo_box. + * + * This function is mostly intended for use by accessibility technologies; + * applications should have little use for it. + * + * Since: 2.4 + */ +void +gtk_combo_box_popdown (GtkComboBox *combo_box) { - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); GtkComboBoxPrivate *priv = combo_box->priv; - if (!gtk_tree_row_reference_valid (priv->active_row)) - { - if (priv->cell_view) - gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL); - g_signal_emit (combo_box, combo_box_signals[CHANGED], 0); - } - - if (priv->tree_view) - gtk_combo_box_list_popup_resize (combo_box); -} - - -static void -gtk_combo_box_button_toggled (GtkWidget *widget, - gpointer data) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (data); + g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); - if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) + if (GTK_IS_MENU (priv->popup_widget)) { - if (!combo_box->priv->popup_in_progress) - gtk_combo_box_popup (combo_box); + gtk_menu_popdown (GTK_MENU (priv->popup_widget)); + return; } - else - gtk_combo_box_popdown (combo_box); -} -static void -gtk_combo_box_menu_show (GtkWidget *menu, - gpointer user_data) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); - GtkComboBoxPrivate *priv = combo_box->priv; + if (!gtk_widget_get_realized (GTK_WIDGET (combo_box))) + return; - gtk_combo_box_child_show (menu, user_data); + if (priv->grab_keyboard) + gdk_device_ungrab (priv->grab_keyboard, GDK_CURRENT_TIME); + gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME); - priv->popup_in_progress = TRUE; + gtk_device_grab_remove (priv->popup_window, priv->grab_pointer); + gtk_widget_hide (priv->popup_window); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), - TRUE); - priv->popup_in_progress = FALSE; -} + FALSE); -static void -gtk_combo_box_menu_hide (GtkWidget *menu, - gpointer user_data) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + priv->grab_pointer = NULL; + priv->grab_keyboard = NULL; +} - gtk_combo_box_child_hide (menu,user_data); +#define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON \ + GtkAllocation button_allocation; \ + gtk_widget_get_preferred_size (combo_box->priv->button, \ + &req, NULL); \ + \ + if (is_rtl) \ + button_allocation.x = allocation->x; \ + else \ + button_allocation.x = allocation->x + allocation->width \ + - req.width; \ + \ + button_allocation.y = allocation->y; \ + button_allocation.width = MAX (1, req.width); \ + button_allocation.height = allocation->height; \ + button_allocation.height = MAX (1, button_allocation.height); \ + \ + gtk_widget_size_allocate (combo_box->priv->button, \ + &button_allocation); - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button), - FALSE); -} static void -gtk_combo_box_detacher (GtkWidget *widget, - GtkMenu *menu) +gtk_combo_box_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) { GtkComboBox *combo_box = GTK_COMBO_BOX (widget); GtkComboBoxPrivate *priv = combo_box->priv; + GtkWidget *child_widget; + GtkAllocation child; + GtkRequisition req; + gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL; + GtkBorder padding; - g_return_if_fail (priv->popup_widget == (GtkWidget *) menu); - - g_signal_handlers_disconnect_by_func (menu->toplevel, - gtk_combo_box_menu_show, - combo_box); - g_signal_handlers_disconnect_by_func (menu->toplevel, - gtk_combo_box_menu_hide, - combo_box); - - priv->popup_widget = NULL; -} - -static void -gtk_combo_box_set_popup_widget (GtkComboBox *combo_box, - GtkWidget *popup) -{ - GtkComboBoxPrivate *priv = combo_box->priv; + gtk_widget_set_allocation (widget, allocation); + child_widget = gtk_bin_get_child (GTK_BIN (widget)); + get_widget_padding_and_border (widget, &padding); - if (GTK_IS_MENU (priv->popup_widget)) - { - gtk_menu_detach (GTK_MENU (priv->popup_widget)); - priv->popup_widget = NULL; - } - else if (priv->popup_widget) - { - gtk_container_remove (GTK_CONTAINER (priv->scrolled_window), - priv->popup_widget); - g_object_unref (priv->popup_widget); - priv->popup_widget = NULL; - } + allocation->x += padding.left; + allocation->y += padding.top; + allocation->width -= padding.left + padding.right; + allocation->height -= padding.top + padding.bottom; - if (GTK_IS_MENU (popup)) + if (!priv->tree_view) { - if (priv->popup_window) + if (priv->cell_view) { - gtk_widget_destroy (priv->popup_window); - priv->popup_window = NULL; - } + GtkBorder button_padding; + gint width; + guint border_width; - priv->popup_widget = popup; + border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button)); + get_widget_padding_and_border (priv->button, &button_padding); - /* - * Note that we connect to show/hide on the toplevel, not the - * menu itself, since the menu is not shown/hidden when it is - * popped up while torn-off. - */ - g_signal_connect (GTK_MENU (popup)->toplevel, "show", - G_CALLBACK (gtk_combo_box_menu_show), combo_box); - g_signal_connect (GTK_MENU (popup)->toplevel, "hide", - G_CALLBACK (gtk_combo_box_menu_hide), combo_box); + /* menu mode; child_widget is priv->cell_view. + * Allocate the button to the full combobox allocation (minus the + * padding). + */ + gtk_widget_size_allocate (priv->button, allocation); - gtk_menu_attach_to_widget (GTK_MENU (popup), - GTK_WIDGET (combo_box), - gtk_combo_box_detacher); - } - else - { - if (!priv->popup_window) - { - GtkWidget *toplevel; - - priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP); - gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window"); + child.x = allocation->x; + child.y = allocation->y; + width = allocation->width; + child.height = allocation->height; - gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window), - GDK_WINDOW_TYPE_HINT_COMBO); - - g_signal_connect (GTK_WINDOW (priv->popup_window),"show", - G_CALLBACK (gtk_combo_box_child_show), - combo_box); - g_signal_connect (GTK_WINDOW (priv->popup_window),"hide", - G_CALLBACK (gtk_combo_box_child_hide), - combo_box); - - toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box)); - if (GTK_IS_WINDOW (toplevel)) - { - gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), - GTK_WINDOW (priv->popup_window)); - gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window), - GTK_WINDOW (toplevel)); - } - - gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE); - gtk_window_set_screen (GTK_WINDOW (priv->popup_window), - gtk_widget_get_screen (GTK_WIDGET (combo_box))); + if (!priv->is_cell_renderer) + { + /* restrict allocation of the child into the button box + * if we're not in cell renderer mode. + */ + child.x += border_width + button_padding.left; + child.y += border_width + button_padding.top; + width -= 2 * border_width + + button_padding.left + button_padding.right; + child.height -= 2 * border_width + + button_padding.top + button_padding.bottom; + } - priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL); - - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), - GTK_POLICY_NEVER, - GTK_POLICY_NEVER); - gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), - GTK_SHADOW_IN); + /* allocate the box containing the separator and the arrow */ + gtk_widget_get_preferred_size (priv->box, &req, NULL); + child.width = req.width; + if (!is_rtl) + child.x += width - req.width; + child.width = MAX (1, child.width); + child.height = MAX (1, child.height); + gtk_widget_size_allocate (priv->box, &child); - gtk_widget_show (priv->scrolled_window); - - gtk_container_add (GTK_CONTAINER (priv->popup_window), - priv->scrolled_window); - } + if (is_rtl) + { + child.x += req.width; + child.width = allocation->x + allocation->width + - border_width - child.x - button_padding.right; + } + else + { + child.width = child.x; + child.x = allocation->x + + border_width + button_padding.left; + child.width -= child.x; + } - gtk_container_add (GTK_CONTAINER (priv->scrolled_window), - popup); + if (gtk_widget_get_visible (priv->popup_widget)) + { + gint width, menu_width; - gtk_widget_show (popup); - g_object_ref (popup); - priv->popup_widget = popup; - } -} + if (priv->wrap_width == 0) + { + GtkAllocation combo_box_allocation; -static void -gtk_combo_box_menu_position_below (GtkMenu *menu, - gint *x, - gint *y, - gint *push_in, - gpointer user_data) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); - GtkAllocation child_allocation; - gint sx, sy; - GtkWidget *child; - GtkRequisition req; - GdkScreen *screen; - gint monitor_num; - GdkRectangle monitor; - GtkBorder border; - - /* FIXME: is using the size request here broken? */ - child = gtk_bin_get_child (GTK_BIN (combo_box)); + gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation); + width = combo_box_allocation.width; + gtk_widget_set_size_request (priv->popup_widget, -1, -1); - sx = sy = 0; + if (combo_box->priv->popup_fixed_width) + gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL); + else + gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width); - gtk_widget_get_allocation (child, &child_allocation); + gtk_widget_set_size_request (priv->popup_widget, + MAX (width, menu_width), -1); + } - if (!gtk_widget_get_has_window (child)) - { - sx += child_allocation.x; - sy += child_allocation.y; - } + /* reposition the menu after giving it a new width */ + gtk_menu_reposition (GTK_MENU (priv->popup_widget)); + } - gdk_window_get_root_coords (gtk_widget_get_window (child), - sx, sy, &sx, &sy); + child.width = MAX (1, child.width); + child.height = MAX (1, child.height); + gtk_widget_size_allocate (child_widget, &child); + } + else + { + /* menu mode; child_widget has been set with gtk_container_add(). + * E.g. it might be a GtkEntry if priv->has_entry is TRUE. + * Allocate the button at the far end, according to the direction + * of the widget. + */ + GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON - get_widget_border (GTK_WIDGET (combo_box), &border); - sx -= border.left; + /* After the macro, button_allocation has the button allocation rect */ - if (combo_box->priv->popup_fixed_width) - gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL); - else - gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req); + if (is_rtl) + child.x = button_allocation.x + button_allocation.width; + else + child.x = allocation->x; - if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR) - *x = sx; - else - *x = sx + child_allocation.width - req.width; - *y = sy; + child.y = allocation->y; + child.width = allocation->width - button_allocation.width; + child.height = button_allocation.height; - screen = gtk_widget_get_screen (GTK_WIDGET (combo_box)); - monitor_num = gdk_screen_get_monitor_at_window (screen, - gtk_widget_get_window (GTK_WIDGET (combo_box))); - gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor); - - if (*x < monitor.x) - *x = monitor.x; - else if (*x + req.width > monitor.x + monitor.width) - *x = monitor.x + monitor.width - req.width; - - if (monitor.y + monitor.height - *y - child_allocation.height >= req.height) - *y += child_allocation.height; - else if (*y - monitor.y >= req.height) - *y -= req.height; - else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y) - *y += child_allocation.height; - else - *y -= req.height; + child.width = MAX (1, child.width); - *push_in = FALSE; -} + gtk_widget_size_allocate (child_widget, &child); + } + } + else + { + /* list mode; child_widget might be either priv->cell_view or a child + * added with gtk_container_add(). + */ -static void -gtk_combo_box_menu_position_over (GtkMenu *menu, - gint *x, - gint *y, - gboolean *push_in, - gpointer user_data) -{ - GtkComboBox *combo_box; - GtkWidget *active; - GtkWidget *child; - GtkWidget *widget; - GtkAllocation allocation; - GtkAllocation child_allocation; - GList *children; - gint screen_width; - gint menu_xpos; - gint menu_ypos; - gint menu_width; + /* After the macro, button_allocation has the button allocation rect */ + GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON - combo_box = GTK_COMBO_BOX (user_data); - widget = GTK_WIDGET (combo_box); + if (is_rtl) + child.x = button_allocation.x + button_allocation.width; + else + child.x = allocation->x; - active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget)); + child.y = allocation->y; + child.width = allocation->width - button_allocation.width; + child.height = button_allocation.height; - gtk_widget_get_allocation (widget, &allocation); + if (priv->cell_view_frame) + { + gtk_widget_size_allocate (priv->cell_view_frame, &child); - menu_xpos = allocation.x; - menu_ypos = allocation.y + allocation.height / 2 - 2; + /* restrict allocation of the child into the frame box if it's present */ + if (priv->has_frame) + { + GtkBorder frame_padding; + guint border_width; - if (combo_box->priv->popup_fixed_width) - gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL); - else - gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width); + border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame)); + get_widget_padding_and_border (priv->cell_view_frame, &frame_padding); - if (active != NULL) - { - gtk_widget_get_allocation (active, &child_allocation); - menu_ypos -= child_allocation.height / 2; - } + child.x += border_width + frame_padding.left; + child.y += border_width + frame_padding.right; + child.width -= (2 * border_width) + frame_padding.left + frame_padding.right; + child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom; + } + } - children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children; - while (children) - { - child = children->data; + if (gtk_widget_get_visible (priv->popup_window)) + { + gint x, y, width, height; + gtk_combo_box_list_position (combo_box, &x, &y, &width, &height); + gtk_window_move (GTK_WINDOW (priv->popup_window), x, y); + gtk_widget_set_size_request (priv->popup_window, width, height); + } - if (active == child) - break; + /* allocate the child */ + child.width = MAX (1, child.width); + child.height = MAX (1, child.height); + gtk_widget_size_allocate (child_widget, &child); + } +} - if (gtk_widget_get_visible (child)) - { - gtk_widget_get_allocation (child, &child_allocation); +#undef GTK_COMBO_BOX_ALLOCATE_BUTTON - menu_ypos -= child_allocation.height; - } +static void +gtk_combo_box_unset_model (GtkComboBox *combo_box) +{ + GtkComboBoxPrivate *priv = combo_box->priv; - children = children->next; + if (priv->model) + { + g_signal_handler_disconnect (priv->model, + priv->inserted_id); + g_signal_handler_disconnect (priv->model, + priv->deleted_id); + g_signal_handler_disconnect (priv->model, + priv->reordered_id); + g_signal_handler_disconnect (priv->model, + priv->changed_id); } - if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) - menu_xpos = menu_xpos + allocation.width - menu_width; - - gdk_window_get_root_coords (gtk_widget_get_window (widget), - menu_xpos, menu_ypos, - &menu_xpos, &menu_ypos); - - /* Clamp the position on screen */ - screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget)); - - if (menu_xpos < 0) - menu_xpos = 0; - else if ((menu_xpos + menu_width) > screen_width) - menu_xpos -= ((menu_xpos + menu_width) - screen_width); + if (priv->model) + { + g_object_unref (priv->model); + priv->model = NULL; + } - *x = menu_xpos; - *y = menu_ypos; + if (priv->active_row) + { + gtk_tree_row_reference_free (priv->active_row); + priv->active_row = NULL; + } - *push_in = TRUE; + if (priv->cell_view) + gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL); } static void -gtk_combo_box_menu_position (GtkMenu *menu, - gint *x, - gint *y, - gint *push_in, - gpointer user_data) +gtk_combo_box_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data) { - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + GtkComboBox *combo_box = GTK_COMBO_BOX (container); GtkComboBoxPrivate *priv = combo_box->priv; - GtkWidget *menu_item; + GtkWidget *child; - if (priv->wrap_width > 0 || priv->cell_view == NULL) - gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data); - else + if (include_internals) { - /* FIXME handle nested menus better */ - menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget)); - if (menu_item) - gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget), - menu_item); - - gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data); + if (priv->button) + (* callback) (priv->button, callback_data); + if (priv->cell_view_frame) + (* callback) (priv->cell_view_frame, callback_data); } - if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->toplevel)) - gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->toplevel), - GDK_WINDOW_TYPE_HINT_COMBO); + child = gtk_bin_get_child (GTK_BIN (container)); + if (child) + (* callback) (child, callback_data); } static void -gtk_combo_box_list_position (GtkComboBox *combo_box, - gint *x, - gint *y, - gint *width, - gint *height) +gtk_combo_box_child_show (GtkWidget *widget, + GtkComboBox *combo_box) { GtkComboBoxPrivate *priv = combo_box->priv; - GtkAllocation allocation; - GdkScreen *screen; - gint monitor_num; - GdkRectangle monitor; - GtkRequisition popup_req; - GtkPolicyType hpolicy, vpolicy; - GdkWindow *window; - /* under windows, the drop down list is as wide as the combo box itself. - see bug #340204 */ - GtkWidget *widget = GTK_WIDGET (combo_box); + priv->popup_shown = TRUE; + g_object_notify (G_OBJECT (combo_box), "popup-shown"); +} - *x = *y = 0; +static void +gtk_combo_box_child_hide (GtkWidget *widget, + GtkComboBox *combo_box) +{ + GtkComboBoxPrivate *priv = combo_box->priv; - gtk_widget_get_allocation (widget, &allocation); + priv->popup_shown = FALSE; + g_object_notify (G_OBJECT (combo_box), "popup-shown"); +} - if (!gtk_widget_get_has_window (widget)) - { - *x += allocation.x; - *y += allocation.y; - } +static gboolean +gtk_combo_box_draw (GtkWidget *widget, + cairo_t *cr) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + GtkComboBoxPrivate *priv = combo_box->priv; - window = gtk_widget_get_window (widget); + if (priv->shadow_type != GTK_SHADOW_NONE) + { + GtkStyleContext *context; - gdk_window_get_root_coords (gtk_widget_get_window (widget), - *x, *y, x, y); + context = gtk_widget_get_style_context (widget); - *width = allocation.width; + gtk_render_background (context, cr, 0, 0, + gtk_widget_get_allocated_width (widget), + gtk_widget_get_allocated_height (widget)); + gtk_render_frame (context, cr, 0, 0, + gtk_widget_get_allocated_width (widget), + gtk_widget_get_allocated_height (widget)); + } - hpolicy = vpolicy = GTK_POLICY_NEVER; - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), - hpolicy, vpolicy); + gtk_container_propagate_draw (GTK_CONTAINER (widget), + priv->button, cr); - if (combo_box->priv->popup_fixed_width) + if (priv->tree_view && priv->cell_view_frame) { - gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL); - - if (popup_req.width > *width) - { - hpolicy = GTK_POLICY_ALWAYS; - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), - hpolicy, vpolicy); - } + gtk_container_propagate_draw (GTK_CONTAINER (widget), + priv->cell_view_frame, cr); } - else - { - /* This code depends on treeviews properly reporting their natural width */ - gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req); - if (popup_req.width > *width) - { - hpolicy = GTK_POLICY_NEVER; - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), - hpolicy, vpolicy); + gtk_container_propagate_draw (GTK_CONTAINER (widget), + gtk_bin_get_child (GTK_BIN (widget)), + cr); - *width = popup_req.width; - } - } + return FALSE; +} - *height = popup_req.height; +typedef struct { + GtkComboBox *combo; + GtkTreePath *path; + GtkTreeIter iter; + gboolean found; + gboolean set; + gboolean visible; +} SearchData; - screen = gtk_widget_get_screen (GTK_WIDGET (combo_box)); - monitor_num = gdk_screen_get_monitor_at_window (screen, window); - gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor); +static gboolean +path_visible (GtkTreeView *view, + GtkTreePath *path) +{ + GtkRBTree *tree; + GtkRBNode *node; - if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL) - *x = *x + allocation.width - *width; + /* Note that we rely on the fact that collapsed rows don't have nodes + */ + return _gtk_tree_view_find_node (view, path, &tree, &node); +} - if (*x < monitor.x) - *x = monitor.x; - else if (*x + *width > monitor.x + monitor.width) - *x = monitor.x + monitor.width - *width; - - if (*y + allocation.height + *height <= monitor.y + monitor.height) - *y += allocation.height; - else if (*y - *height >= monitor.y) - *y -= *height; - else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y) - { - *y += allocation.height; - *height = monitor.y + monitor.height - *y; - } - else - { - *height = *y - monitor.y; - *y = monitor.y; - } +static gboolean +tree_next_func (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) +{ + SearchData *search_data = (SearchData *)data; - if (popup_req.height > *height) + if (search_data->found) { - vpolicy = GTK_POLICY_ALWAYS; - - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), - hpolicy, vpolicy); + if (!tree_column_row_is_sensitive (search_data->combo, iter)) + return FALSE; + + if (search_data->visible && + !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) + return FALSE; + + search_data->set = TRUE; + search_data->iter = *iter; + + return TRUE; } -} -static void -gtk_combo_box_menu_popup (GtkComboBox *combo_box, - guint button, - guint32 activate_time) + if (gtk_tree_path_compare (path, search_data->path) == 0) + search_data->found = TRUE; + + return FALSE; +} + +static gboolean +tree_next (GtkComboBox *combo, + GtkTreeModel *model, + GtkTreeIter *iter, + GtkTreeIter *next, + gboolean visible) { - GtkComboBoxPrivate *priv = combo_box->priv; - GtkTreePath *path; - gint active_item; - gint width, min_width, nat_width; - - active_item = -1; - if (gtk_tree_row_reference_valid (priv->active_row)) + SearchData search_data; + + search_data.combo = combo; + search_data.path = gtk_tree_model_get_path (model, iter); + search_data.visible = visible; + search_data.found = FALSE; + search_data.set = FALSE; + + gtk_tree_model_foreach (model, tree_next_func, &search_data); + + *next = search_data.iter; + + gtk_tree_path_free (search_data.path); + + return search_data.set; +} + +static gboolean +tree_prev_func (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) +{ + SearchData *search_data = (SearchData *)data; + + if (gtk_tree_path_compare (path, search_data->path) == 0) { - path = gtk_tree_row_reference_get_path (priv->active_row); - active_item = gtk_tree_path_get_indices (path)[0]; - gtk_tree_path_free (path); - - if (priv->add_tearoffs) - active_item++; + search_data->found = TRUE; + return TRUE; } - /* FIXME handle nested menus better */ - gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item); - - if (priv->wrap_width == 0) - { - GtkAllocation allocation; + if (!tree_column_row_is_sensitive (search_data->combo, iter)) + return FALSE; - gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation); - width = allocation.width; - gtk_widget_set_size_request (priv->popup_widget, -1, -1); - gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width); + if (search_data->visible && + !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) + return FALSE; - if (combo_box->priv->popup_fixed_width) - width = MAX (width, min_width); - else - width = MAX (width, nat_width); + search_data->set = TRUE; + search_data->iter = *iter; - gtk_widget_set_size_request (priv->popup_widget, width, -1); - } - - gtk_menu_popup (GTK_MENU (priv->popup_widget), - NULL, NULL, - gtk_combo_box_menu_position, combo_box, - button, activate_time); + return FALSE; } static gboolean -popup_grab_on_window (GdkWindow *window, - GdkDevice *keyboard, - GdkDevice *pointer, - guint32 activate_time) +tree_prev (GtkComboBox *combo, + GtkTreeModel *model, + GtkTreeIter *iter, + GtkTreeIter *prev, + gboolean visible) { - if (keyboard && - gdk_device_grab (keyboard, window, - GDK_OWNERSHIP_WINDOW, TRUE, - GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, - NULL, activate_time) != GDK_GRAB_SUCCESS) + SearchData search_data; + + search_data.combo = combo; + search_data.path = gtk_tree_model_get_path (model, iter); + search_data.visible = visible; + search_data.found = FALSE; + search_data.set = FALSE; + + gtk_tree_model_foreach (model, tree_prev_func, &search_data); + + *prev = search_data.iter; + + gtk_tree_path_free (search_data.path); + + return search_data.set; +} + +static gboolean +tree_last_func (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) +{ + SearchData *search_data = (SearchData *)data; + + if (!tree_column_row_is_sensitive (search_data->combo, iter)) return FALSE; - if (pointer && - gdk_device_grab (pointer, window, - GDK_OWNERSHIP_WINDOW, TRUE, - GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | - GDK_POINTER_MOTION_MASK, - NULL, activate_time) != GDK_GRAB_SUCCESS) - { - if (keyboard) - gdk_device_ungrab (keyboard, activate_time); + /* Note that we rely on the fact that collapsed rows don't have nodes + */ + if (search_data->visible && + !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) + return FALSE; - return FALSE; - } + search_data->set = TRUE; + search_data->iter = *iter; - return TRUE; + return FALSE; } -static void -gtk_combo_box_unset_model (GtkComboBox *combo_box) +static gboolean +tree_last (GtkComboBox *combo, + GtkTreeModel *model, + GtkTreeIter *last, + gboolean visible) { - GtkComboBoxPrivate *priv = combo_box->priv; + SearchData search_data; - if (priv->model) - { - g_signal_handler_disconnect (priv->model, - priv->inserted_id); - g_signal_handler_disconnect (priv->model, - priv->deleted_id); + search_data.combo = combo; + search_data.visible = visible; + search_data.set = FALSE; - g_object_unref (priv->model); - priv->model = NULL; - } + gtk_tree_model_foreach (model, tree_last_func, &search_data); - if (priv->active_row) - { - gtk_tree_row_reference_free (priv->active_row); - priv->active_row = NULL; - } + *last = search_data.iter; - if (priv->cell_view) - gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL); + return search_data.set; } -static void -gtk_combo_box_child_show (GtkWidget *widget, - GtkComboBox *combo_box) + +static gboolean +tree_first_func (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) { - GtkComboBoxPrivate *priv = combo_box->priv; + SearchData *search_data = (SearchData *)data; - priv->popup_shown = TRUE; - g_object_notify (G_OBJECT (combo_box), "popup-shown"); + if (!tree_column_row_is_sensitive (search_data->combo, iter)) + return FALSE; + + if (search_data->visible && + !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path)) + return FALSE; + + search_data->set = TRUE; + search_data->iter = *iter; + + return TRUE; } -static void -gtk_combo_box_child_hide (GtkWidget *widget, - GtkComboBox *combo_box) +static gboolean +tree_first (GtkComboBox *combo, + GtkTreeModel *model, + GtkTreeIter *first, + gboolean visible) { - GtkComboBoxPrivate *priv = combo_box->priv; + SearchData search_data; - priv->popup_shown = FALSE; - g_object_notify (G_OBJECT (combo_box), "popup-shown"); + search_data.combo = combo; + search_data.visible = visible; + search_data.set = FALSE; + + gtk_tree_model_foreach (model, tree_first_func, &search_data); + + *first = search_data.iter; + + return search_data.set; +} + +static gboolean +gtk_combo_box_scroll_event (GtkWidget *widget, + GdkEventScroll *event) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + gboolean found; + GtkTreeIter iter; + GtkTreeIter new_iter; + + if (!gtk_combo_box_get_active_iter (combo_box, &iter)) + return TRUE; + + if (event->direction == GDK_SCROLL_UP) + found = tree_prev (combo_box, combo_box->priv->model, + &iter, &new_iter, FALSE); + else + found = tree_next (combo_box, combo_box->priv->model, + &iter, &new_iter, FALSE); + + if (found) + gtk_combo_box_set_active_iter (combo_box, &new_iter); + + return TRUE; } /* * menu style */ - -static gboolean +static gboolean gtk_combo_box_row_separator_func (GtkTreeModel *model, - GtkTreeIter *iter, - GtkComboBox *combo) + GtkTreeIter *iter, + GtkComboBox *combo) { GtkComboBoxPrivate *priv = combo->priv; @@ -3227,32 +3122,17 @@ gtk_combo_box_row_separator_func (GtkTreeModel *model, return FALSE; } -static gboolean +static gboolean gtk_combo_box_header_func (GtkTreeModel *model, - GtkTreeIter *iter, - GtkComboBox *combo) + GtkTreeIter *iter, + GtkComboBox *combo) { /* Every submenu has a selectable header, however we - * can expose a method to make that configurable by + * can expose a method to make that configurable by * the user (like row_separator_func is done) */ return TRUE; } -static void -gtk_combo_box_menu_activate (GtkWidget *menu, - const gchar *path, - GtkComboBox *combo_box) -{ - GtkTreeIter iter; - - if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path)) - gtk_combo_box_set_active_iter (combo_box, &iter); - - g_object_set (combo_box, - "editing-canceled", FALSE, - NULL); -} - static void gtk_combo_box_menu_setup (GtkComboBox *combo_box, gboolean add_children) @@ -3267,7 +3147,7 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box, { priv->button = gtk_toggle_button_new (); gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), - priv->focus_on_click); + priv->focus_on_click); g_signal_connect (priv->button, "toggled", G_CALLBACK (gtk_combo_box_button_toggled), combo_box); @@ -3282,6 +3162,7 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box, priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE); gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow); + gtk_widget_add_events (priv->button, GDK_SCROLL_MASK); gtk_widget_show_all (priv->button); } @@ -3289,7 +3170,7 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box, { priv->button = gtk_toggle_button_new (); gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), - priv->focus_on_click); + priv->focus_on_click); g_signal_connect (priv->button, "toggled", G_CALLBACK (gtk_combo_box_button_toggled), combo_box); @@ -3298,42 +3179,43 @@ gtk_combo_box_menu_setup (GtkComboBox *combo_box, priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE); gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow); + gtk_widget_add_events (priv->button, GDK_SCROLL_MASK); gtk_widget_show_all (priv->button); } g_signal_connect (priv->button, "button-press-event", G_CALLBACK (gtk_combo_box_menu_button_press), combo_box); - g_signal_connect (priv->button, "state-changed", - G_CALLBACK (gtk_combo_box_button_state_changed), - combo_box); + g_signal_connect (priv->button, "state-flags-changed", + G_CALLBACK (gtk_combo_box_button_state_flags_changed), + combo_box); /* create our funky menu */ - menu = gtk_tree_menu_new_with_area (priv->area); - gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model); + menu = _gtk_tree_menu_new_with_area (priv->area); + gtk_widget_set_name (menu, "gtk-combobox-popup-menu"); - gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width); - gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column); - gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column); - gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu), - combo_box->priv->add_tearoffs); + _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model); + + _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width); + _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column); + _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column); + _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu), + combo_box->priv->add_tearoffs); g_signal_connect (menu, "menu-activate", - G_CALLBACK (gtk_combo_box_menu_activate), combo_box); + G_CALLBACK (gtk_combo_box_menu_activate), combo_box); /* Chain our row_separator_func through */ - gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu), - (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func, - combo_box, NULL); + _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu), + (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func, + combo_box, NULL); - gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu), - (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func, - combo_box, NULL); + _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu), + (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func, + combo_box, NULL); - gtk_widget_set_name (menu, "gtk-combobox-popup-menu"); - g_signal_connect (menu, "key-press-event", - G_CALLBACK (gtk_combo_box_menu_key_press), combo_box); + G_CALLBACK (gtk_combo_box_menu_key_press), combo_box); gtk_combo_box_set_popup_widget (combo_box, menu); gtk_combo_box_update_title (combo_box); @@ -3351,7 +3233,7 @@ gtk_combo_box_menu_destroy (GtkComboBox *combo_box) g_signal_handlers_disconnect_matched (priv->button, G_SIGNAL_MATCH_DATA, 0, 0, NULL, - gtk_combo_box_button_state_changed, combo_box); + gtk_combo_box_button_state_flags_changed, combo_box); g_signal_handlers_disconnect_matched (priv->popup_widget, G_SIGNAL_MATCH_DATA, 0, 0, NULL, @@ -3365,8 +3247,6 @@ gtk_combo_box_menu_destroy (GtkComboBox *combo_box) priv->arrow = NULL; priv->separator = NULL; - priv->column = NULL; - /* changing the popup window will unref the menu and the children */ } @@ -3380,11 +3260,11 @@ gtk_combo_box_menu_button_press (GtkWidget *widget, GtkComboBoxPrivate *priv = combo_box->priv; if (GTK_IS_MENU (priv->popup_widget) && - event->type == GDK_BUTTON_PRESS && event->button == 1) + event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) { - if (priv->focus_on_click && - !gtk_widget_has_focus (priv->button)) - gtk_widget_grab_focus (priv->button); + if (priv->focus_on_click && + !gtk_widget_has_focus (priv->button)) + gtk_widget_grab_focus (priv->button); gtk_combo_box_menu_popup (combo_box, event->button, event->time); @@ -3394,6 +3274,22 @@ gtk_combo_box_menu_button_press (GtkWidget *widget, return FALSE; } +static void +gtk_combo_box_menu_activate (GtkWidget *menu, + const gchar *path, + GtkComboBox *combo_box) +{ + GtkTreeIter iter; + + if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path)) + gtk_combo_box_set_active_iter (combo_box, &iter); + + g_object_set (combo_box, + "editing-canceled", FALSE, + NULL); +} + + static void gtk_combo_box_update_sensitivity (GtkComboBox *combo_box) { @@ -3428,6 +3324,75 @@ gtk_combo_box_update_sensitivity (GtkComboBox *combo_box) gtk_widget_set_sensitive (combo_box->priv->box, sensitive); } +static void +gtk_combo_box_model_row_inserted (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + + if (combo_box->priv->tree_view) + gtk_combo_box_list_popup_resize (combo_box); + + gtk_combo_box_update_sensitivity (combo_box); +} + +static void +gtk_combo_box_model_row_deleted (GtkTreeModel *model, + GtkTreePath *path, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + GtkComboBoxPrivate *priv = combo_box->priv; + + if (!gtk_tree_row_reference_valid (priv->active_row)) + { + if (priv->cell_view) + gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL); + g_signal_emit (combo_box, combo_box_signals[CHANGED], 0); + } + + if (priv->tree_view) + gtk_combo_box_list_popup_resize (combo_box); + + gtk_combo_box_update_sensitivity (combo_box); +} + +static void +gtk_combo_box_model_rows_reordered (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gint *new_order, + gpointer user_data) +{ + gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order); +} + +static void +gtk_combo_box_model_row_changed (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + GtkComboBoxPrivate *priv = combo_box->priv; + GtkTreePath *active_path; + + /* FIXME this belongs to GtkCellView */ + if (gtk_tree_row_reference_valid (priv->active_row)) + { + active_path = gtk_tree_row_reference_get_path (priv->active_row); + if (gtk_tree_path_compare (path, active_path) == 0 && + priv->cell_view) + gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view)); + gtk_tree_path_free (active_path); + } + + if (priv->tree_view) + gtk_combo_box_list_row_changed (model, path, iter, user_data); +} + static gboolean list_popup_resize_idle (gpointer user_data) { @@ -3438,7 +3403,7 @@ list_popup_resize_idle (gpointer user_data) if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window)) { gtk_combo_box_list_position (combo_box, &x, &y, &width, &height); - + gtk_widget_set_size_request (priv->popup_window, width, height); gtk_window_move (GTK_WINDOW (priv->popup_window), x, y); } @@ -3454,21 +3419,22 @@ gtk_combo_box_list_popup_resize (GtkComboBox *combo_box) GtkComboBoxPrivate *priv = combo_box->priv; if (!priv->resize_idle_id) - priv->resize_idle_id = + priv->resize_idle_id = gdk_threads_add_idle (list_popup_resize_idle, combo_box); } static void gtk_combo_box_model_row_expanded (GtkTreeModel *model, - GtkTreePath *path, - GtkTreeIter *iter, - gpointer user_data) + GtkTreePath *path, + GtkTreeIter *iter, + gpointer user_data) { GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); - + gtk_combo_box_list_popup_resize (combo_box); } + /* * list style */ @@ -3499,64 +3465,61 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box) { GtkStyleContext *context; GtkStateFlags state; - GdkRGBA *color; + GdkRGBA color; - context = gtk_widget_get_style_context (widget); + context = gtk_widget_get_style_context (widget); state = gtk_widget_get_state_flags (widget); + gtk_style_context_get_background_color (context, state, &color); - gtk_style_context_get (context, state, - "background-color", &color, - NULL); - - gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), color); - gdk_rgba_free (color); + gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color); priv->box = gtk_event_box_new (); - gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box), - FALSE); + gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box), + FALSE); if (priv->has_frame) - { - priv->cell_view_frame = gtk_frame_new (NULL); - gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame), - GTK_SHADOW_IN); - } - else - { - combo_box->priv->cell_view_frame = gtk_event_box_new (); - gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), - FALSE); - } + { + priv->cell_view_frame = gtk_frame_new (NULL); + gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame), + GTK_SHADOW_IN); + } + else + { + combo_box->priv->cell_view_frame = gtk_event_box_new (); + gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), + FALSE); + } gtk_widget_set_parent (priv->cell_view_frame, - gtk_widget_get_parent (child)); + gtk_widget_get_parent (child)); gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box); gtk_widget_show_all (priv->cell_view_frame); g_signal_connect (priv->box, "button-press-event", - G_CALLBACK (gtk_combo_box_list_button_pressed), - combo_box); + G_CALLBACK (gtk_combo_box_list_button_pressed), + combo_box); } priv->tree_view = gtk_tree_view_new (); sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)); gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE); gtk_tree_selection_set_select_function (sel, - gtk_combo_box_list_select_func, - NULL, NULL); + gtk_combo_box_list_select_func, + NULL, NULL); gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE); gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view), - TRUE); + TRUE); + + gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view), + (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func, + combo_box, NULL); - gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view), - (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func, - combo_box, NULL); if (priv->model) gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model); - - priv->column = gtk_tree_view_column_new (); - gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column); + + gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), + gtk_tree_view_column_new_with_area (priv->area)); if (gtk_tree_row_reference_valid (priv->active_row)) { @@ -3564,7 +3527,7 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box) path = gtk_tree_row_reference_get_path (priv->active_row); gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view), - path, NULL, FALSE); + path, NULL, FALSE); gtk_tree_path_free (path); } @@ -3578,11 +3541,11 @@ gtk_combo_box_list_setup (GtkComboBox *combo_box) G_CALLBACK (gtk_combo_box_list_enter_notify), combo_box); g_signal_connect (priv->tree_view, "row-expanded", - G_CALLBACK (gtk_combo_box_model_row_expanded), - combo_box); + G_CALLBACK (gtk_combo_box_model_row_expanded), + combo_box); g_signal_connect (priv->tree_view, "row-collapsed", - G_CALLBACK (gtk_combo_box_model_row_expanded), - combo_box); + G_CALLBACK (gtk_combo_box_model_row_expanded), + combo_box); g_signal_connect (priv->popup_window, "button-press-event", G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box); @@ -3621,23 +3584,23 @@ gtk_combo_box_list_destroy (GtkComboBox *combo_box) NULL); g_signal_handlers_disconnect_matched (priv->popup_window, - G_SIGNAL_MATCH_DATA, - 0, 0, NULL, - gtk_combo_box_child_show, - NULL); + G_SIGNAL_MATCH_DATA, + 0, 0, NULL, + gtk_combo_box_child_show, + NULL); g_signal_handlers_disconnect_matched (priv->popup_window, - G_SIGNAL_MATCH_DATA, - 0, 0, NULL, - gtk_combo_box_child_hide, - NULL); - + G_SIGNAL_MATCH_DATA, + 0, 0, NULL, + gtk_combo_box_child_hide, + NULL); + if (priv->box) g_signal_handlers_disconnect_matched (priv->box, - G_SIGNAL_MATCH_DATA, - 0, 0, NULL, - gtk_combo_box_list_button_pressed, - NULL); + G_SIGNAL_MATCH_DATA, + 0, 0, NULL, + gtk_combo_box_list_button_pressed, + NULL); /* destroy things (unparent will kill the latest ref from us) * last unref on button will destroy the arrow @@ -3701,7 +3664,7 @@ gtk_combo_box_list_button_pressed (GtkWidget *widget, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button))) return FALSE; - if (priv->focus_on_click && + if (priv->focus_on_click && !gtk_widget_has_focus (priv->button)) gtk_widget_grab_focus (priv->button); @@ -3711,9 +3674,9 @@ gtk_combo_box_list_button_pressed (GtkWidget *widget, priv->auto_scroll = FALSE; if (priv->scroll_timer == 0) - priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME, - (GSourceFunc) gtk_combo_box_list_scroll_timeout, - combo_box); + priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME, + (GSourceFunc) gtk_combo_box_list_scroll_timeout, + combo_box); priv->popup_in_progress = TRUE; @@ -3742,8 +3705,8 @@ gtk_combo_box_list_button_released (GtkWidget *widget, priv->popup_in_progress = FALSE; } - gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view), - FALSE); + gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view), + FALSE); if (priv->scroll_timer) { g_source_remove (priv->scroll_timer); @@ -3752,8 +3715,8 @@ gtk_combo_box_list_button_released (GtkWidget *widget, if (ewidget != priv->tree_view) { - if ((ewidget == priv->button || - ewidget == priv->box) && + if ((ewidget == priv->button || + ewidget == priv->box) && !popup_in_progress && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button))) { @@ -3794,8 +3757,8 @@ gtk_combo_box_list_button_released (GtkWidget *widget, static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget, - GdkEventKey *event, - gpointer data) + GdkEventKey *event, + gpointer data) { GtkComboBox *combo_box = GTK_COMBO_BOX (data); @@ -3819,22 +3782,22 @@ gtk_combo_box_list_key_press (GtkWidget *widget, GtkTreeIter iter; if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter || - event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space) + event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space) { GtkTreeModel *model = NULL; - + gtk_combo_box_popdown (combo_box); - + if (combo_box->priv->model) { - GtkTreeSelection *sel; + GtkTreeSelection *sel; - sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)); + sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)); - if (gtk_tree_selection_get_selected (sel, &model, &iter)) - gtk_combo_box_set_active_iter (combo_box, &iter); + if (gtk_tree_selection_get_selected (sel, &model, &iter)) + gtk_combo_box_set_active_iter (combo_box, &iter); } - + return TRUE; } @@ -3851,8 +3814,8 @@ gtk_combo_box_list_key_press (GtkWidget *widget, static void gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box, - gint x, - gint y) + gint x, + gint y) { GtkAdjustment *adj; GtkAllocation allocation; @@ -3862,37 +3825,37 @@ gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box, gtk_widget_get_allocation (tree_view, &allocation); adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window)); - if (adj && adj->upper - adj->lower > adj->page_size) + if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj)) { if (x <= allocation.x && - adj->lower < adj->value) - { - value = adj->value - (allocation.x - x + 1); - gtk_adjustment_set_value (adj, value); - } + gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj)) + { + value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1); + gtk_adjustment_set_value (adj, value); + } else if (x >= allocation.x + allocation.width && - adj->upper - adj->page_size > adj->value) - { - value = adj->value + (x - allocation.x - allocation.width + 1); - gtk_adjustment_set_value (adj, MAX (value, 0.0)); - } + gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj)) + { + value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1); + gtk_adjustment_set_value (adj, MAX (value, 0.0)); + } } adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window)); - if (adj && adj->upper - adj->lower > adj->page_size) + if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj)) { if (y <= allocation.y && - adj->lower < adj->value) - { - value = adj->value - (allocation.y - y + 1); - gtk_adjustment_set_value (adj, value); - } + gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj)) + { + value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1); + gtk_adjustment_set_value (adj, value); + } else if (y >= allocation.height && - adj->upper - adj->page_size > adj->value) - { - value = adj->value + (y - allocation.height + 1); - gtk_adjustment_set_value (adj, MAX (value, 0.0)); - } + gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj)) + { + value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1); + gtk_adjustment_set_value (adj, MAX (value, 0.0)); + } } } @@ -3913,10 +3876,10 @@ gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box) return TRUE; } -static gboolean +static gboolean gtk_combo_box_list_enter_notify (GtkWidget *widget, - GdkEventCrossing *event, - gpointer data) + GdkEventCrossing *event, + gpointer data) { GtkComboBox *combo_box = GTK_COMBO_BOX (data); @@ -3927,10 +3890,10 @@ gtk_combo_box_list_enter_notify (GtkWidget *widget, static gboolean gtk_combo_box_list_select_func (GtkTreeSelection *selection, - GtkTreeModel *model, - GtkTreePath *path, - gboolean path_currently_selected, - gpointer data) + GtkTreeModel *model, + GtkTreePath *path, + gboolean path_currently_selected, + gpointer data) { GList *list, *columns; gboolean sensitive = FALSE; @@ -3945,28 +3908,30 @@ gtk_combo_box_list_select_func (GtkTreeSelection *selection, GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data); if (!gtk_tree_view_column_get_visible (column)) - continue; + continue; gtk_tree_model_get_iter (model, &iter, path); gtk_tree_view_column_cell_set_cell_data (column, model, &iter, - FALSE, FALSE); + FALSE, FALSE); cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column)); while (cell) { - g_object_get (cell->data, - "sensitive", &cell_sensitive, - "visible", &cell_visible, - NULL); + g_object_get (cell->data, + "sensitive", &cell_sensitive, + "visible", &cell_visible, + NULL); - if (cell_visible && cell_sensitive) - break; + if (cell_visible && cell_sensitive) + { + sensitive = TRUE; + break; + } - cell = cell->next; - } - g_list_free (cells); + cell = cell->next; + } - sensitive = cell_sensitive; + g_list_free (cells); } g_list_free (columns); @@ -3974,301 +3939,145 @@ gtk_combo_box_list_select_func (GtkTreeSelection *selection, return sensitive; } - -/* - * GtkCellEditable implementation - */ - static void -gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface) +gtk_combo_box_list_row_changed (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + gpointer data) { - iface->start_editing = gtk_combo_box_start_editing; + /* XXX Do nothing ? */ } -static gboolean -gtk_cell_editable_key_press (GtkWidget *widget, - GdkEventKey *event, - gpointer data) +/* + * GtkCellLayout implementation + */ +static GtkCellArea * +gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout) { - GtkComboBox *combo_box = GTK_COMBO_BOX (data); + GtkComboBox *combo = GTK_COMBO_BOX (cell_layout); + GtkComboBoxPrivate *priv = combo->priv; - if (event->keyval == GDK_KEY_Escape) - { - g_object_set (combo_box, - "editing-canceled", TRUE, - NULL); - gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box)); - gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box)); - - return TRUE; - } - else if (event->keyval == GDK_KEY_Return || - event->keyval == GDK_KEY_ISO_Enter || - event->keyval == GDK_KEY_KP_Enter) + if (G_UNLIKELY (!priv->area)) { - gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box)); - gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box)); - - return TRUE; + priv->area = gtk_cell_area_box_new (); + g_object_ref_sink (priv->area); } - return FALSE; + return priv->area; } -static gboolean -popdown_idle (gpointer data) -{ - GtkComboBox *combo_box; +/* + * public API + */ - combo_box = GTK_COMBO_BOX (data); - - gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box)); - gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box)); +/** + * gtk_combo_box_new: + * + * Creates a new empty #GtkComboBox. + * + * Return value: A new #GtkComboBox. + * + * Since: 2.4 + */ +GtkWidget * +gtk_combo_box_new (void) +{ + return g_object_new (GTK_TYPE_COMBO_BOX, NULL); +} - g_object_unref (combo_box); +/** + * gtk_combo_box_new_with_area: + * @area: the #GtkCellArea to use to layout cell renderers + * + * Creates a new empty #GtkComboBox using @area to layout cells. + * + * Return value: A new #GtkComboBox. + */ +GtkWidget * +gtk_combo_box_new_with_area (GtkCellArea *area) +{ + return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL); +} - return FALSE; +/** + * gtk_combo_box_new_with_area_and_entry: + * @area: the #GtkCellArea to use to layout cell renderers + * + * Creates a new empty #GtkComboBox with an entry. + * + * The new combo box will use @area to layout cells. + * + * Return value: A new #GtkComboBox. + */ +GtkWidget * +gtk_combo_box_new_with_area_and_entry (GtkCellArea *area) +{ + return g_object_new (GTK_TYPE_COMBO_BOX, + "has-entry", TRUE, + "cell-area", area, + NULL); } -static void -popdown_handler (GtkWidget *widget, - gpointer data) + +/** + * gtk_combo_box_new_with_entry: + * + * Creates a new empty #GtkComboBox with an entry. + * + * Return value: A new #GtkComboBox. + */ +GtkWidget * +gtk_combo_box_new_with_entry (void) { - gdk_threads_add_idle (popdown_idle, g_object_ref (data)); + return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL); } -static gboolean -popup_idle (gpointer data) +/** + * gtk_combo_box_new_with_model: + * @model: A #GtkTreeModel. + * + * Creates a new #GtkComboBox with the model initialized to @model. + * + * Return value: A new #GtkComboBox. + * + * Since: 2.4 + */ +GtkWidget * +gtk_combo_box_new_with_model (GtkTreeModel *model) { GtkComboBox *combo_box; - combo_box = GTK_COMBO_BOX (data); - - if (GTK_IS_MENU (combo_box->priv->popup_widget) && - combo_box->priv->cell_view) - g_signal_connect_object (combo_box->priv->popup_widget, - "unmap", G_CALLBACK (popdown_handler), - combo_box, 0); - - /* we unset this if a menu item is activated */ - g_object_set (combo_box, - "editing-canceled", TRUE, - NULL); - gtk_combo_box_popup (combo_box); + g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL); - combo_box->priv->popup_idle_id = 0; - combo_box->priv->activate_button = 0; - combo_box->priv->activate_time = 0; + combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL); - return FALSE; + return GTK_WIDGET (combo_box); } -static void -gtk_combo_box_start_editing (GtkCellEditable *cell_editable, - GdkEvent *event) +/** + * gtk_combo_box_new_with_model_and_entry: + * @model: A #GtkTreeModel + * + * Creates a new empty #GtkComboBox with an entry + * and with the model initialized to @model. + * + * Return value: A new #GtkComboBox + */ +GtkWidget * +gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model) { - GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable); - GtkWidget *child; - - combo_box->priv->is_cell_renderer = TRUE; - - if (combo_box->priv->cell_view) - { - g_signal_connect_object (combo_box->priv->button, "key-press-event", - G_CALLBACK (gtk_cell_editable_key_press), - cell_editable, 0); - - gtk_widget_grab_focus (combo_box->priv->button); - } - else - { - child = gtk_bin_get_child (GTK_BIN (combo_box)); - - g_signal_connect_object (child, "key-press-event", - G_CALLBACK (gtk_cell_editable_key_press), - cell_editable, 0); - - gtk_widget_grab_focus (child); - gtk_widget_set_can_focus (combo_box->priv->button, FALSE); - } - - /* we do the immediate popup only for the optionmenu-like - * appearance - */ - if (combo_box->priv->is_cell_renderer && - combo_box->priv->cell_view && !combo_box->priv->tree_view) - { - if (event && event->type == GDK_BUTTON_PRESS) - { - GdkEventButton *event_button = (GdkEventButton *)event; - - combo_box->priv->activate_button = event_button->button; - combo_box->priv->activate_time = event_button->time; - } - - combo_box->priv->popup_idle_id = - gdk_threads_add_idle (popup_idle, combo_box); - } -} - - -/* - * GtkCellLayout implementation - */ - -static void -gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface) -{ - iface->get_area = gtk_combo_box_cell_layout_get_area; -} - -static GtkCellArea * -gtk_combo_box_cell_layout_get_area (GtkCellLayout *layout) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (layout); - GtkComboBoxPrivate *priv; - - priv = combo_box->priv; - - return priv->area; -} - -/* - * GtkBuildable implementation - */ - -static void -gtk_combo_box_buildable_init (GtkBuildableIface *iface) -{ - parent_buildable_iface = g_type_interface_peek_parent (iface); - iface->add_child = _gtk_cell_layout_buildable_add_child; - iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start; - iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end; - iface->get_internal_child = gtk_combo_box_buildable_get_internal_child; -} - -static gboolean -gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable, - GtkBuilder *builder, - GObject *child, - const gchar *tagname, - GMarkupParser *parser, - gpointer *data) -{ - if (parent_buildable_iface->custom_tag_start (buildable, builder, child, - tagname, parser, data)) - return TRUE; - - return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child, - tagname, parser, data); -} - -static void -gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable, - GtkBuilder *builder, - GObject *child, - const gchar *tagname, - gpointer *data) -{ - if (_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, - data)) - return; - else - parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, - data); -} - -static GObject * -gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable, - GtkBuilder *builder, - const gchar *childname) -{ - GtkComboBox *combo_box = GTK_COMBO_BOX (buildable); - - if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0) - return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable))); - - return parent_buildable_iface->get_internal_child (buildable, builder, childname); -} - -/* - * public API - */ - -/** - * gtk_combo_box_new: - * - * Creates a new empty #GtkComboBox. - * - * Return value: A new #GtkComboBox. - * - * Since: 2.4 - */ -GtkWidget * -gtk_combo_box_new (void) -{ - return g_object_new (GTK_TYPE_COMBO_BOX, NULL); -} - -/** - * gtk_combo_box_new_with_entry: - * - * Creates a new empty #GtkComboBox with an entry. - * - * Return value: A new #GtkComboBox. - */ -GtkWidget * -gtk_combo_box_new_with_entry (void) -{ - return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL); -} - -/** - * gtk_combo_box_new_with_model: - * @model: A #GtkTreeModel. - * - * Creates a new #GtkComboBox with the model initialized to @model. - * - * Return value: A new #GtkComboBox. - * - * Since: 2.4 - */ -GtkWidget * -gtk_combo_box_new_with_model (GtkTreeModel *model) -{ - GtkComboBox *combo_box; - - g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL); - - combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL); - - return GTK_WIDGET (combo_box); -} - -/** - * gtk_combo_box_new_with_model_and_entry: - * @model: A #GtkTreeModel - * - * Creates a new empty #GtkComboBox with an entry - * and with the model initialized to @model. - * - * Return value: A new #GtkComboBox - */ -GtkWidget * -gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model) -{ - return g_object_new (GTK_TYPE_COMBO_BOX, - "has-entry", TRUE, - "model", model, - NULL); -} + return g_object_new (GTK_TYPE_COMBO_BOX, + "has-entry", TRUE, + "model", model, + NULL); +} /** * gtk_combo_box_get_wrap_width: * @combo_box: A #GtkComboBox * - * Returns the wrap width which is used to determine the number of columns - * for the popup menu. If the wrap width is larger than 1, the combo box + * Returns the wrap width which is used to determine the number of columns + * for the popup menu. If the wrap width is larger than 1, the combo box * is in table mode. * * Returns: the wrap width. @@ -4312,8 +4121,8 @@ gtk_combo_box_set_wrap_width (GtkComboBox *combo_box, gtk_combo_box_check_appearance (combo_box); if (GTK_IS_TREE_MENU (priv->popup_widget)) - gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width); - + _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width); + g_object_notify (G_OBJECT (combo_box), "wrap-width"); } } @@ -4366,8 +4175,8 @@ gtk_combo_box_set_row_span_column (GtkComboBox *combo_box, priv->row_column = row_span; if (GTK_IS_TREE_MENU (priv->popup_widget)) - gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column); - + _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column); + g_object_notify (G_OBJECT (combo_box), "row-span-column"); } } @@ -4420,8 +4229,8 @@ gtk_combo_box_set_column_span_column (GtkComboBox *combo_box, priv->col_column = column_span; if (GTK_IS_TREE_MENU (priv->popup_widget)) - gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column); - + _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column); + g_object_notify (G_OBJECT (combo_box), "column-span-column"); } } @@ -4431,12 +4240,12 @@ gtk_combo_box_set_column_span_column (GtkComboBox *combo_box, * @combo_box: A #GtkComboBox * * Returns the index of the currently active item, or -1 if there's no - * active item. If the model is a non-flat treemodel, and the active item - * is not an immediate child of the root of the tree, this function returns - * gtk_tree_path_get_indices (path)[0], where + * active item. If the model is a non-flat treemodel, and the active item + * is not an immediate child of the root of the tree, this function returns + * gtk_tree_path_get_indices (path)[0], where * path is the #GtkTreePath of the active item. * - * Return value: An integer which is the index of the currently active item, + * Return value: An integer which is the index of the currently active item, * or -1 if there's no active item. * * Since: 2.4 @@ -4455,7 +4264,7 @@ gtk_combo_box_get_active (GtkComboBox *combo_box) { GtkTreePath *path; - path = gtk_tree_row_reference_get_path (priv->active_row); + path = gtk_tree_row_reference_get_path (priv->active_row); result = gtk_tree_path_get_indices (path)[0]; gtk_tree_path_free (path); } @@ -4493,7 +4302,7 @@ gtk_combo_box_set_active (GtkComboBox *combo_box, if (index_ != -1) path = gtk_tree_path_new_from_indices (index_, -1); - + gtk_combo_box_set_active_internal (combo_box, path); if (path) @@ -4502,7 +4311,7 @@ gtk_combo_box_set_active (GtkComboBox *combo_box, static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box, - GtkTreePath *path) + GtkTreePath *path) { GtkComboBoxPrivate *priv = combo_box->priv; GtkTreePath *active_path; @@ -4517,7 +4326,7 @@ gtk_combo_box_set_active_internal (GtkComboBox *combo_box, path_cmp = gtk_tree_path_compare (path, active_path); gtk_tree_path_free (active_path); if (path_cmp == 0) - return; + return; } if (priv->active_row) @@ -4525,7 +4334,7 @@ gtk_combo_box_set_active_internal (GtkComboBox *combo_box, gtk_tree_row_reference_free (priv->active_row); priv->active_row = NULL; } - + if (!path) { if (priv->tree_view) @@ -4550,24 +4359,24 @@ gtk_combo_box_set_active_internal (GtkComboBox *combo_box, } else { - priv->active_row = - gtk_tree_row_reference_new (priv->model, path); + priv->active_row = + gtk_tree_row_reference_new (priv->model, path); if (priv->tree_view) - { - gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view), - path, NULL, FALSE); - } + { + gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view), + path, NULL, FALSE); + } else if (GTK_IS_MENU (priv->popup_widget)) { - /* FIXME handle nested menus better */ - gtk_menu_set_active (GTK_MENU (priv->popup_widget), - gtk_tree_path_get_indices (path)[0]); + /* FIXME handle nested menus better */ + gtk_menu_set_active (GTK_MENU (priv->popup_widget), + gtk_tree_path_get_indices (path)[0]); } if (priv->cell_view) - gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), - path); + gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), + path); } g_signal_emit (combo_box, combo_box_signals[CHANGED], 0); @@ -4581,9 +4390,9 @@ gtk_combo_box_set_active_internal (GtkComboBox *combo_box, * gtk_combo_box_get_active_iter: * @combo_box: A #GtkComboBox * @iter: (out): The uninitialized #GtkTreeIter - * + * * Sets @iter to point to the current active item, if it exists. - * + * * Return value: %TRUE, if @iter was set * * Since: 2.4 @@ -4611,10 +4420,10 @@ gtk_combo_box_get_active_iter (GtkComboBox *combo_box, * gtk_combo_box_set_active_iter: * @combo_box: A #GtkComboBox * @iter: (allow-none): The #GtkTreeIter, or %NULL - * + * * Sets the current active item to be the one referenced by @iter, or * unsets the active item if @iter is %NULL. - * + * * Since: 2.4 */ void @@ -4640,8 +4449,8 @@ gtk_combo_box_set_active_iter (GtkComboBox *combo_box, * Sets the model used by @combo_box to be @model. Will unset a previously set * model (if applicable). If model is %NULL, then it will unset the model. * - * Note that this function does not clear the cell renderers, you have to - * call gtk_cell_layout_clear() yourself if you need to set up different + * Note that this function does not clear the cell renderers, you have to + * call gtk_cell_layout_clear() yourself if you need to set up different * cell renderers for the new model. * * Since: 2.4 @@ -4655,7 +4464,7 @@ gtk_combo_box_set_model (GtkComboBox *combo_box, if (model == combo_box->priv->model) return; - + gtk_combo_box_unset_model (combo_box); if (model == NULL) @@ -4666,12 +4475,20 @@ gtk_combo_box_set_model (GtkComboBox *combo_box, combo_box->priv->inserted_id = g_signal_connect (combo_box->priv->model, "row-inserted", - G_CALLBACK (gtk_combo_box_model_row_inserted), - combo_box); + G_CALLBACK (gtk_combo_box_model_row_inserted), + combo_box); combo_box->priv->deleted_id = g_signal_connect (combo_box->priv->model, "row-deleted", - G_CALLBACK (gtk_combo_box_model_row_deleted), - combo_box); + G_CALLBACK (gtk_combo_box_model_row_deleted), + combo_box); + combo_box->priv->reordered_id = + g_signal_connect (combo_box->priv->model, "rows-reordered", + G_CALLBACK (gtk_combo_box_model_rows_reordered), + combo_box); + combo_box->priv->changed_id = + g_signal_connect (combo_box->priv->model, "row-changed", + G_CALLBACK (gtk_combo_box_model_row_changed), + combo_box); if (combo_box->priv->tree_view) { @@ -4684,8 +4501,8 @@ gtk_combo_box_set_model (GtkComboBox *combo_box, if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget)) { /* menu mode */ - gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), - combo_box->priv->model); + _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), + combo_box->priv->model); } if (combo_box->priv->cell_view) @@ -4699,86 +4516,489 @@ gtk_combo_box_set_model (GtkComboBox *combo_box, combo_box->priv->active = -1; } -out: - g_object_notify (G_OBJECT (combo_box), "model"); +out: + gtk_combo_box_update_sensitivity (combo_box); + + g_object_notify (G_OBJECT (combo_box), "model"); +} + +/** + * gtk_combo_box_get_model: + * @combo_box: A #GtkComboBox + * + * Returns the #GtkTreeModel which is acting as data source for @combo_box. + * + * Return value: (transfer none): A #GtkTreeModel which was passed + * during construction. + * + * Since: 2.4 + */ +GtkTreeModel * +gtk_combo_box_get_model (GtkComboBox *combo_box) +{ + g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL); + + return combo_box->priv->model; +} + +static void +gtk_combo_box_real_move_active (GtkComboBox *combo_box, + GtkScrollType scroll) +{ + GtkTreeIter iter; + GtkTreeIter new_iter; + gboolean active_iter; + gboolean found; + + if (!combo_box->priv->model) + { + gtk_widget_error_bell (GTK_WIDGET (combo_box)); + return; + } + + active_iter = gtk_combo_box_get_active_iter (combo_box, &iter); + + switch (scroll) + { + case GTK_SCROLL_STEP_BACKWARD: + case GTK_SCROLL_STEP_UP: + case GTK_SCROLL_STEP_LEFT: + if (active_iter) + { + found = tree_prev (combo_box, combo_box->priv->model, + &iter, &new_iter, FALSE); + break; + } + /* else fall through */ + + case GTK_SCROLL_PAGE_FORWARD: + case GTK_SCROLL_PAGE_DOWN: + case GTK_SCROLL_PAGE_RIGHT: + case GTK_SCROLL_END: + found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE); + break; + + case GTK_SCROLL_STEP_FORWARD: + case GTK_SCROLL_STEP_DOWN: + case GTK_SCROLL_STEP_RIGHT: + if (active_iter) + { + found = tree_next (combo_box, combo_box->priv->model, + &iter, &new_iter, FALSE); + break; + } + /* else fall through */ + + case GTK_SCROLL_PAGE_BACKWARD: + case GTK_SCROLL_PAGE_UP: + case GTK_SCROLL_PAGE_LEFT: + case GTK_SCROLL_START: + found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE); + break; + + default: + return; + } + + if (found && active_iter) + { + GtkTreePath *old_path; + GtkTreePath *new_path; + + old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter); + new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter); + + if (gtk_tree_path_compare (old_path, new_path) == 0) + found = FALSE; + + gtk_tree_path_free (old_path); + gtk_tree_path_free (new_path); + } + + if (found) + { + gtk_combo_box_set_active_iter (combo_box, &new_iter); + } + else + { + gtk_widget_error_bell (GTK_WIDGET (combo_box)); + } +} + +static gboolean +gtk_combo_box_mnemonic_activate (GtkWidget *widget, + gboolean group_cycling) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + + if (combo_box->priv->has_entry) + { + GtkWidget* child; + + child = gtk_bin_get_child (GTK_BIN (combo_box)); + if (child) + gtk_widget_grab_focus (child); + } + else + gtk_widget_grab_focus (combo_box->priv->button); + + return TRUE; +} + +static void +gtk_combo_box_grab_focus (GtkWidget *widget) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + + if (combo_box->priv->has_entry) + { + GtkWidget *child; + + child = gtk_bin_get_child (GTK_BIN (combo_box)); + if (child) + gtk_widget_grab_focus (child); + } + else + gtk_widget_grab_focus (combo_box->priv->button); +} + +static void +gtk_combo_box_destroy (GtkWidget *widget) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + + if (combo_box->priv->popup_idle_id > 0) + { + g_source_remove (combo_box->priv->popup_idle_id); + combo_box->priv->popup_idle_id = 0; + } + + gtk_combo_box_popdown (combo_box); + + if (combo_box->priv->row_separator_destroy) + combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data); + + combo_box->priv->row_separator_func = NULL; + combo_box->priv->row_separator_data = NULL; + combo_box->priv->row_separator_destroy = NULL; + + GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget); + combo_box->priv->cell_view = NULL; +} + +static void +gtk_combo_box_entry_contents_changed (GtkEntry *entry, + gpointer user_data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + + /* + * Fixes regression reported in bug #574059. The old functionality relied on + * bug #572478. As a bugfix, we now emit the "changed" signal ourselves + * when the selection was already set to -1. + */ + if (gtk_combo_box_get_active(combo_box) == -1) + g_signal_emit_by_name (combo_box, "changed"); + else + gtk_combo_box_set_active (combo_box, -1); +} + +static void +gtk_combo_box_entry_active_changed (GtkComboBox *combo_box, + gpointer user_data) +{ + GtkTreeModel *model; + GtkTreeIter iter; + + if (gtk_combo_box_get_active_iter (combo_box, &iter)) + { + GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box))); + + if (entry) + { + GtkTreePath *path; + gchar *path_str; + gchar *text = NULL; + + model = gtk_combo_box_get_model (combo_box); + path = gtk_tree_model_get_path (model, &iter); + path_str = gtk_tree_path_to_string (path); + + g_signal_handlers_block_by_func (entry, + gtk_combo_box_entry_contents_changed, + combo_box); + + + g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0, + path_str, &text); + + gtk_entry_set_text (entry, text); + + g_signal_handlers_unblock_by_func (entry, + gtk_combo_box_entry_contents_changed, + combo_box); + + gtk_tree_path_free (path); + g_free (text); + g_free (path_str); + } + } +} + +static gchar * +gtk_combo_box_format_entry_text (GtkComboBox *combo_box, + const gchar *path) +{ + GtkComboBoxPrivate *priv = combo_box->priv; + GtkTreeModel *model; + GtkTreeIter iter; + gchar *text = NULL; + + if (priv->text_column >= 0) + { + model = gtk_combo_box_get_model (combo_box); + gtk_tree_model_get_iter_from_string (model, &iter, path); + + gtk_tree_model_get (model, &iter, + priv->text_column, &text, + -1); + } + + return text; +} + + +static GObject * +gtk_combo_box_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_properties) +{ + GObject *object; + GtkComboBox *combo_box; + GtkComboBoxPrivate *priv; + + object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor + (type, n_construct_properties, construct_properties); + + combo_box = GTK_COMBO_BOX (object); + priv = combo_box->priv; + + if (!priv->area) + { + priv->area = gtk_cell_area_box_new (); + g_object_ref_sink (priv->area); + } + + priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL); + gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE); + gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model); + gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box)); + _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view); + gtk_widget_show (priv->cell_view); + + gtk_combo_box_check_appearance (combo_box); + + if (priv->has_entry) + { + GtkWidget *entry; + GtkStyleContext *context; + + entry = gtk_entry_new (); + gtk_widget_show (entry); + gtk_container_add (GTK_CONTAINER (combo_box), entry); + + context = gtk_widget_get_style_context (GTK_WIDGET (combo_box)); + gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY); + + priv->text_renderer = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), + priv->text_renderer, TRUE); + + gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1); + + g_signal_connect (combo_box, "changed", + G_CALLBACK (gtk_combo_box_entry_active_changed), NULL); + } + + return object; +} + + +static void +gtk_combo_box_dispose(GObject* object) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (object); + + if (GTK_IS_MENU (combo_box->priv->popup_widget)) + { + gtk_combo_box_menu_destroy (combo_box); + gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget)); + combo_box->priv->popup_widget = NULL; + } + + if (combo_box->priv->area) + { + g_object_unref (combo_box->priv->area); + combo_box->priv->area = NULL; + } + + if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)) + gtk_combo_box_list_destroy (combo_box); + + if (combo_box->priv->popup_window) + { + gtk_widget_destroy (combo_box->priv->popup_window); + combo_box->priv->popup_window = NULL; + } + + gtk_combo_box_unset_model (combo_box); + + G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object); +} + +static void +gtk_combo_box_finalize (GObject *object) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (object); + + g_free (combo_box->priv->tearoff_title); + + G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object); +} + +static gboolean +gtk_cell_editable_key_press (GtkWidget *widget, + GdkEventKey *event, + gpointer data) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (data); + + if (event->keyval == GDK_KEY_Escape) + { + g_object_set (combo_box, + "editing-canceled", TRUE, + NULL); + gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box)); + gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box)); + + return TRUE; + } + else if (event->keyval == GDK_KEY_Return || + event->keyval == GDK_KEY_ISO_Enter || + event->keyval == GDK_KEY_KP_Enter) + { + gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box)); + gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box)); + + return TRUE; + } + + return FALSE; } -/** - * gtk_combo_box_get_model: - * @combo_box: A #GtkComboBox - * - * Returns the #GtkTreeModel which is acting as data source for @combo_box. - * - * Return value: (transfer none): A #GtkTreeModel which was passed - * during construction. - * - * Since: 2.4 - */ -GtkTreeModel * -gtk_combo_box_get_model (GtkComboBox *combo_box) +static gboolean +popdown_idle (gpointer data) { - g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL); + GtkComboBox *combo_box; - return combo_box->priv->model; + combo_box = GTK_COMBO_BOX (data); + + gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box)); + gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box)); + + g_object_unref (combo_box); + + return FALSE; } static void -gtk_combo_box_entry_contents_changed (GtkEntry *entry, - gpointer user_data) +popdown_handler (GtkWidget *widget, + gpointer data) { - GtkComboBox *combo_box = GTK_COMBO_BOX (user_data); + gdk_threads_add_idle (popdown_idle, g_object_ref (data)); +} - /* - * Fixes regression reported in bug #574059. The old functionality relied on - * bug #572478. As a bugfix, we now emit the "changed" signal ourselves - * when the selection was already set to -1. - */ - if (gtk_combo_box_get_active(combo_box) == -1) - g_signal_emit_by_name (combo_box, "changed"); - else - gtk_combo_box_set_active (combo_box, -1); +static gboolean +popup_idle (gpointer data) +{ + GtkComboBox *combo_box; + + combo_box = GTK_COMBO_BOX (data); + + if (GTK_IS_MENU (combo_box->priv->popup_widget) && + combo_box->priv->cell_view) + g_signal_connect_object (combo_box->priv->popup_widget, + "unmap", G_CALLBACK (popdown_handler), + combo_box, 0); + + /* we unset this if a menu item is activated */ + g_object_set (combo_box, + "editing-canceled", TRUE, + NULL); + gtk_combo_box_popup (combo_box); + + combo_box->priv->popup_idle_id = 0; + combo_box->priv->activate_button = 0; + combo_box->priv->activate_time = 0; + + return FALSE; } static void -gtk_combo_box_entry_active_changed (GtkComboBox *combo_box, - gpointer user_data) +gtk_combo_box_start_editing (GtkCellEditable *cell_editable, + GdkEvent *event) { - GtkComboBoxPrivate *priv = combo_box->priv; - GtkTreeModel *model; - GtkTreeIter iter; + GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable); + GtkWidget *child; - if (gtk_combo_box_get_active_iter (combo_box, &iter)) + combo_box->priv->is_cell_renderer = TRUE; + + if (combo_box->priv->cell_view) { - GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box))); + g_signal_connect_object (combo_box->priv->button, "key-press-event", + G_CALLBACK (gtk_cell_editable_key_press), + cell_editable, 0); - if (entry) - { - GValue value = {0,}; + gtk_widget_grab_focus (combo_box->priv->button); + } + else + { + child = gtk_bin_get_child (GTK_BIN (combo_box)); + + g_signal_connect_object (child, "key-press-event", + G_CALLBACK (gtk_cell_editable_key_press), + cell_editable, 0); - g_signal_handlers_block_by_func (entry, - gtk_combo_box_entry_contents_changed, - combo_box); + gtk_widget_grab_focus (child); + gtk_widget_set_can_focus (combo_box->priv->button, FALSE); + } - model = gtk_combo_box_get_model (combo_box); + /* we do the immediate popup only for the optionmenu-like + * appearance + */ + if (combo_box->priv->is_cell_renderer && + combo_box->priv->cell_view && !combo_box->priv->tree_view) + { + if (event && event->type == GDK_BUTTON_PRESS) + { + GdkEventButton *event_button = (GdkEventButton *)event; - gtk_tree_model_get_value (model, &iter, - priv->text_column, &value); - g_object_set_property (G_OBJECT (entry), "text", &value); - g_value_unset (&value); + combo_box->priv->activate_button = event_button->button; + combo_box->priv->activate_time = event_button->time; + } - g_signal_handlers_unblock_by_func (entry, - gtk_combo_box_entry_contents_changed, - combo_box); - } + combo_box->priv->popup_idle_id = + gdk_threads_add_idle (popup_idle, combo_box); } } + /** * gtk_combo_box_get_add_tearoffs: * @combo_box: a #GtkComboBox - * + * * Gets the current value of the :add-tearoffs property. - * + * * Return value: the current value of the :add-tearoffs property. */ gboolean @@ -4791,17 +5011,17 @@ gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box) /** * gtk_combo_box_set_add_tearoffs: - * @combo_box: a #GtkComboBox + * @combo_box: a #GtkComboBox * @add_tearoffs: %TRUE to add tearoff menu items - * - * Sets whether the popup menu should have a tearoff + * + * Sets whether the popup menu should have a tearoff * menu item. * * Since: 2.6 */ void gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box, - gboolean add_tearoffs) + gboolean add_tearoffs) { g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); @@ -4810,14 +5030,11 @@ gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box, if (combo_box->priv->add_tearoffs != add_tearoffs) { combo_box->priv->add_tearoffs = add_tearoffs; + gtk_combo_box_check_appearance (combo_box); if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget)) - { - /* menu mode */ - gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget), - combo_box->priv->add_tearoffs); - /* XXX Resize ?? */ - } + _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget), + combo_box->priv->add_tearoffs); g_object_notify (G_OBJECT (combo_box), "add-tearoffs"); } @@ -4835,11 +5052,11 @@ gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box, * * Since: 2.10 */ -G_CONST_RETURN gchar* +const gchar* gtk_combo_box_get_title (GtkComboBox *combo_box) { g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL); - + return combo_box->priv->tearoff_title; } @@ -4847,25 +5064,25 @@ static void gtk_combo_box_update_title (GtkComboBox *combo_box) { gtk_combo_box_check_appearance (combo_box); - - if (combo_box->priv->popup_widget && + + if (combo_box->priv->popup_widget && GTK_IS_MENU (combo_box->priv->popup_widget)) - gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget), - combo_box->priv->tearoff_title); + gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget), + combo_box->priv->tearoff_title); } /** * gtk_combo_box_set_title: - * @combo_box: a #GtkComboBox + * @combo_box: a #GtkComboBox * @title: a title for the menu in tearoff mode - * + * * Sets the menu's title in tearoff mode. * * Since: 2.10 */ void gtk_combo_box_set_title (GtkComboBox *combo_box, - const gchar *title) + const gchar *title) { GtkComboBoxPrivate *priv; @@ -4873,8 +5090,8 @@ gtk_combo_box_set_title (GtkComboBox *combo_box, priv = combo_box->priv; - if (strcmp (title ? title : "", - priv->tearoff_title ? priv->tearoff_title : "") != 0) + if (strcmp (title ? title : "", + priv->tearoff_title ? priv->tearoff_title : "") != 0) { g_free (priv->tearoff_title); priv->tearoff_title = g_strdup (title); @@ -4961,16 +5178,16 @@ gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box) } /** - * gtk_combo_box_get_row_separator_func: + * gtk_combo_box_get_row_separator_func: (skip) * @combo_box: a #GtkComboBox - * + * * Returns the current row separator function. - * + * * Return value: the current row separator function. * * Since: 2.6 */ -GtkTreeViewRowSeparatorFunc +GtkTreeViewRowSeparatorFunc gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box) { g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL); @@ -4984,7 +5201,7 @@ gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box) * @func: a #GtkTreeViewRowSeparatorFunc * @data: (allow-none): user data to pass to @func, or %NULL * @destroy: (allow-none): destroy notifier for @data, or %NULL - * + * * Sets the row separator function, which is used to determine * whether a row should be drawn as a separator. If the row separator * function is %NULL, no separators are drawn. This is the default value. @@ -4993,9 +5210,9 @@ gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box) */ void gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box, - GtkTreeViewRowSeparatorFunc func, - gpointer data, - GDestroyNotify destroy) + GtkTreeViewRowSeparatorFunc func, + gpointer data, + GDestroyNotify destroy) { g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); @@ -5015,8 +5232,8 @@ gtk_combo_box_set_row_separator_func (GtkComboBox *combo_box, if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget)) { - gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL); - gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model); + _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL); + _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model); } gtk_widget_queue_draw (GTK_WIDGET (combo_box)); @@ -5107,7 +5324,7 @@ gtk_combo_box_get_has_entry (GtkComboBox *combo_box) */ void gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box, - gint text_column) + gint text_column) { GtkComboBoxPrivate *priv = combo_box->priv; GtkTreeModel *model; @@ -5144,223 +5361,350 @@ gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box) { g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0); - return combo_box->priv->text_column; -} + return combo_box->priv->text_column; +} + +/** + * gtk_combo_box_set_focus_on_click: + * @combo: a #GtkComboBox + * @focus_on_click: whether the combo box grabs focus when clicked + * with the mouse + * + * Sets whether the combo box will grab focus when it is clicked with + * the mouse. Making mouse clicks not grab focus is useful in places + * like toolbars where you don't want the keyboard focus removed from + * the main area of the application. + * + * Since: 2.6 + */ +void +gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box, + gboolean focus_on_click) +{ + g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); + + focus_on_click = focus_on_click != FALSE; + + if (combo_box->priv->focus_on_click != focus_on_click) + { + combo_box->priv->focus_on_click = focus_on_click; + + if (combo_box->priv->button) + gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button), + focus_on_click); + + g_object_notify (G_OBJECT (combo_box), "focus-on-click"); + } +} + +/** + * gtk_combo_box_get_focus_on_click: + * @combo: a #GtkComboBox + * + * Returns whether the combo box grabs focus when it is clicked + * with the mouse. See gtk_combo_box_set_focus_on_click(). + * + * Return value: %TRUE if the combo box grabs focus when it is + * clicked with the mouse. + * + * Since: 2.6 + */ +gboolean +gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box) +{ + g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE); + + return combo_box->priv->focus_on_click; +} + + +static gboolean +gtk_combo_box_buildable_custom_tag_start (GtkBuildable *buildable, + GtkBuilder *builder, + GObject *child, + const gchar *tagname, + GMarkupParser *parser, + gpointer *data) +{ + if (parent_buildable_iface->custom_tag_start (buildable, builder, child, + tagname, parser, data)) + return TRUE; + + return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child, + tagname, parser, data); +} + +static void +gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable, + GtkBuilder *builder, + GObject *child, + const gchar *tagname, + gpointer *data) +{ + if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data)) + parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data); +} + +static GObject * +gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable, + GtkBuilder *builder, + const gchar *childname) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (buildable); + + if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0) + return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable))); + + return parent_buildable_iface->get_internal_child (buildable, builder, childname); +} + +static void +gtk_combo_box_get_preferred_width (GtkWidget *widget, + gint *minimum_size, + gint *natural_size) +{ + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + GtkComboBoxPrivate *priv = combo_box->priv; + gint font_size, arrow_size; + PangoContext *context; + PangoFontMetrics *metrics; + GtkWidget *child; + gint minimum_width = 0, natural_width = 0; + gint child_min, child_nat; + GtkBorder padding; + gfloat arrow_scaling; + + child = gtk_bin_get_child (GTK_BIN (widget)); + + /* common */ + gtk_widget_get_preferred_width (child, &child_min, &child_nat); + + gtk_widget_style_get (GTK_WIDGET (widget), + "arrow-size", &arrow_size, + "arrow-scaling", &arrow_scaling, + NULL); + + get_widget_padding_and_border (widget, &padding); + + context = gtk_widget_get_pango_context (GTK_WIDGET (widget)); + metrics = pango_context_get_metrics (context, + pango_context_get_font_description (context), + pango_context_get_language (context)); + font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + + pango_font_metrics_get_descent (metrics)); + pango_font_metrics_unref (metrics); + + arrow_size = MAX (arrow_size, font_size) * arrow_scaling; + + gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size); + + if (!priv->tree_view) + { + /* menu mode */ + if (priv->cell_view) + { + gint box_width, xpad; + GtkBorder button_padding; + + get_widget_padding_and_border (priv->button, &button_padding); + + gtk_widget_get_preferred_width (priv->box, &box_width, NULL); + xpad = button_padding.left + button_padding.right + padding.left + padding.right; + + minimum_width = child_min + box_width + xpad; + natural_width = child_nat + box_width + xpad; + } + else + { + gint but_width, but_nat_width; + + gtk_widget_get_preferred_width (priv->button, + &but_width, &but_nat_width); + + minimum_width = child_min + but_width; + natural_width = child_nat + but_nat_width; + } + } + else + { + /* list mode */ + gint button_width, button_nat_width; + + /* sample + frame */ + minimum_width = child_min; + natural_width = child_nat; + + if (priv->cell_view_frame) + { + if (priv->has_frame) + { + gint border_width, xpad; + GtkBorder frame_padding; + + border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame)); + get_widget_padding_and_border (priv->cell_view_frame, &frame_padding); + xpad = (2 * border_width) + frame_padding.left + frame_padding.right; -/** - * gtk_combo_box_set_focus_on_click: - * @combo: a #GtkComboBox - * @focus_on_click: whether the combo box grabs focus when clicked - * with the mouse - * - * Sets whether the combo box will grab focus when it is clicked with - * the mouse. Making mouse clicks not grab focus is useful in places - * like toolbars where you don't want the keyboard focus removed from - * the main area of the application. - * - * Since: 2.6 - */ -void -gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box, - gboolean focus_on_click) -{ - g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); - - focus_on_click = focus_on_click != FALSE; + minimum_width += xpad; + natural_width += xpad; + } + } - if (combo_box->priv->focus_on_click != focus_on_click) - { - combo_box->priv->focus_on_click = focus_on_click; + /* the button */ + gtk_widget_get_preferred_width (priv->button, + &button_width, &button_nat_width); - if (combo_box->priv->button) - gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button), - focus_on_click); - - g_object_notify (G_OBJECT (combo_box), "focus-on-click"); + minimum_width += button_width; + natural_width += button_nat_width; } + + minimum_width += padding.left + padding.right; + natural_width += padding.left + padding.right; + + if (minimum_size) + *minimum_size = minimum_width; + + if (natural_size) + *natural_size = natural_width; } -/** - * gtk_combo_box_get_focus_on_click: - * @combo: a #GtkComboBox - * - * Returns whether the combo box grabs focus when it is clicked - * with the mouse. See gtk_combo_box_set_focus_on_click(). - * - * Return value: %TRUE if the combo box grabs focus when it is - * clicked with the mouse. - * - * Since: 2.6 - */ -gboolean -gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box) +static void +gtk_combo_box_get_preferred_height (GtkWidget *widget, + gint *minimum_size, + gint *natural_size) { - g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE); - - return combo_box->priv->focus_on_click; -} + gint min_width; + /* Combo box is height-for-width only + * (so we always just reserve enough height for the minimum width) */ + GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL); + GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size); +} -/** - * gtk_combo_box_popup: - * @combo_box: a #GtkComboBox - * - * Pops up the menu or dropdown list of @combo_box. - * - * This function is mostly intended for use by accessibility technologies; - * applications should have little use for it. - * - * Since: 2.4 - */ -void -gtk_combo_box_popup (GtkComboBox *combo_box) +static void +gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget, + gint avail_size, + gint *minimum_size, + gint *natural_size) { - g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); - - g_signal_emit (combo_box, combo_box_signals[POPUP], 0); + /* Combo box is height-for-width only + * (so we assume we always reserved enough height for the minimum width) */ + GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size); } -/** - * gtk_combo_box_popup_for_device: - * @combo_box: a #GtkComboBox - * @device: a #GdkDevice - * - * Pops up the menu or dropdown list of @combo_box, the popup window - * will be grabbed so only @device and its associated pointer/keyboard - * are the only #GdkDevices able to send events to it. - * - * Since: 3.0 - **/ -void -gtk_combo_box_popup_for_device (GtkComboBox *combo_box, - GdkDevice *device) + +static void +gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget, + gint avail_size, + gint *minimum_size, + gint *natural_size) { - GtkComboBoxPrivate *priv = combo_box->priv; - gint x, y, width, height; - GtkTreePath *path = NULL, *ppath; - GtkWidget *toplevel; - GdkDevice *keyboard, *pointer; - guint32 time; + GtkComboBox *combo_box = GTK_COMBO_BOX (widget); + GtkComboBoxPrivate *priv = combo_box->priv; + gint min_height = 0, nat_height = 0; + gint size; + GtkWidget *child; + GtkBorder padding; - g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); - g_return_if_fail (GDK_IS_DEVICE (device)); + child = gtk_bin_get_child (GTK_BIN (widget)); - if (!gtk_widget_get_realized (GTK_WIDGET (combo_box))) - return; + get_widget_padding_and_border (widget, &padding); + size = avail_size; - if (gtk_widget_get_mapped (priv->popup_widget)) - return; + if (!priv->tree_view) + { + /* menu mode */ + if (priv->cell_view) + { + /* calculate x/y padding and separator/arrow size */ + gint box_width, box_height; + gint xpad, ypad; + GtkBorder button_padding; - if (priv->grab_pointer && priv->grab_keyboard) - return; + get_widget_padding_and_border (priv->button, &button_padding); - time = gtk_get_current_event_time (); + gtk_widget_get_preferred_width (priv->box, &box_width, NULL); + gtk_widget_get_preferred_height_for_width (priv->box, + box_width, &box_height, NULL); - if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD) - { - keyboard = device; - pointer = gdk_device_get_associated_device (device); - } - else - { - pointer = device; - keyboard = gdk_device_get_associated_device (device); - } + xpad = button_padding.left + button_padding.right; + ypad = button_padding.top + button_padding.bottom; - if (GTK_IS_MENU (priv->popup_widget)) - { - gtk_combo_box_menu_popup (combo_box, - priv->activate_button, - priv->activate_time); - return; - } + size -= box_width + xpad; - toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box)); - if (GTK_IS_WINDOW (toplevel)) - gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), - GTK_WINDOW (priv->popup_window)); + /* Get height-for-width of the child widget, usually a GtkCellArea calculating + * and fitting the whole treemodel */ + gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height); - gtk_widget_show_all (priv->scrolled_window); - gtk_combo_box_list_position (combo_box, &x, &y, &width, &height); - - gtk_widget_set_size_request (priv->popup_window, width, height); - gtk_window_move (GTK_WINDOW (priv->popup_window), x, y); + min_height = MAX (min_height, box_height); + nat_height = MAX (nat_height, box_height); - if (gtk_tree_row_reference_valid (priv->active_row)) - { - path = gtk_tree_row_reference_get_path (priv->active_row); - ppath = gtk_tree_path_copy (path); - if (gtk_tree_path_up (ppath)) - gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view), - ppath); - gtk_tree_path_free (ppath); - } - gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view), - TRUE); - - /* popup */ - gtk_widget_show (priv->popup_window); + min_height += ypad; + nat_height += ypad; + } + else + { + /* there is a custom child widget inside (no priv->cell_view) */ + gint but_width, but_height; - if (path) - { - gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view), - path, NULL, FALSE); - gtk_tree_path_free (path); - } + gtk_widget_get_preferred_width (priv->button, &but_width, NULL); + gtk_widget_get_preferred_height_for_width (priv->button, + but_width, &but_height, NULL); - gtk_widget_grab_focus (priv->popup_window); - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), - TRUE); + size -= but_width; - if (!gtk_widget_has_focus (priv->tree_view)) - gtk_widget_grab_focus (priv->tree_view); + /* Get height-for-width of the child widget, usually a GtkCellArea calculating + * and fitting the whole treemodel */ + gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height); - if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window), - keyboard, pointer, time)) - { - gtk_widget_hide (priv->popup_window); - return; + min_height = MAX (min_height, but_height); + nat_height = MAX (nat_height, but_height); + } } + else + { + /* list mode */ + gint but_width, but_height; + gint xpad = 0, ypad = 0; - gtk_device_grab_add (priv->popup_window, pointer, TRUE); - priv->grab_pointer = pointer; - priv->grab_keyboard = keyboard; -} + gtk_widget_get_preferred_width (priv->button, &but_width, NULL); + gtk_widget_get_preferred_height_for_width (priv->button, + but_width, &but_height, NULL); -/** - * gtk_combo_box_popdown: - * @combo_box: a #GtkComboBox - * - * Hides the menu or dropdown list of @combo_box. - * - * This function is mostly intended for use by accessibility technologies; - * applications should have little use for it. - * - * Since: 2.4 - */ -void -gtk_combo_box_popdown (GtkComboBox *combo_box) -{ - GtkComboBoxPrivate *priv = combo_box->priv; + if (priv->cell_view_frame && priv->has_frame) + { + GtkBorder frame_padding; + gint border_width; - g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); + border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame)); + get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding); - if (GTK_IS_MENU (priv->popup_widget)) - { - gtk_menu_popdown (GTK_MENU (priv->popup_widget)); - return; + xpad = (2 * border_width) + padding.left + frame_padding.right; + ypad = (2 * border_width) + padding.top + frame_padding.bottom; + } + + size -= but_width; + size -= xpad; + + /* Get height-for-width of the child widget, usually a GtkCellArea calculating + * and fitting the whole treemodel */ + gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height); + + min_height = MAX (min_height, but_height); + nat_height = MAX (nat_height, but_height); + + min_height += ypad; + nat_height += ypad; } - if (!gtk_widget_get_realized (GTK_WIDGET (combo_box))) - return; + min_height += padding.top + padding.bottom; + nat_height += padding.top + padding.bottom; - gtk_device_grab_remove (priv->popup_window, priv->grab_pointer); - gtk_widget_hide (priv->popup_window); - gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), - FALSE); + if (minimum_size) + *minimum_size = min_height; - priv->grab_pointer = NULL; - priv->grab_keyboard = NULL; + if (natural_size) + *natural_size = nat_height; } /** @@ -5422,15 +5766,16 @@ gtk_combo_box_get_id_column (GtkComboBox *combo_box) * @combo_box: a #GtkComboBox * * Returns the ID of the active row of @combo_box. This value is taken - * from the active row and the column specified by the 'id-column' + * from the active row and the column specified by the #GtkComboBox:id-column * property of @combo_box (see gtk_combo_box_set_id_column()). * * The returned value is an interned string which means that you can * compare the pointer by value to other interned strings and that you * must not free it. * - * If the 'id-column' property of @combo_box is not set or if no row is - * selected then %NULL is returned. + * If the #GtkComboBox:id-column property of @combo_box is not set, or if + * no row is active, or if the active row has a %NULL ID value, then %NULL + * is returned. * * Return value: the ID of the active row, or %NULL * @@ -5443,7 +5788,7 @@ gtk_combo_box_get_active_id (GtkComboBox *combo_box) GtkTreeIter iter; gint column; - g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0); + g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL); column = combo_box->priv->id_column; @@ -5472,41 +5817,54 @@ gtk_combo_box_get_active_id (GtkComboBox *combo_box) /** * gtk_combo_box_set_active_id: * @combo_box: a #GtkComboBox - * @active_id: the ID of the row to select + * @active_id: (allow-none): the ID of the row to select, or %NULL * - * Changes the active row of @combo_box to the one that has an ID equal to @id. + * Changes the active row of @combo_box to the one that has an ID equal to + * @active_id, or unsets the active row if @active_id is %NULL. Rows having + * a %NULL ID string cannot be made active by this function. * - * If the 'id-column' property of @combo_box is unset or if no row has - * the given ID then nothing happens. + * If the #GtkComboBox:id-column property of @combo_box is unset or if no + * row has the given ID then the function does nothing and returns %FALSE. + * + * Returns: %TRUE if a row with a matching ID was found. If a %NULL + * @active_id was given to unset the active row, the function + * always returns %TRUE. * * Since: 3.0 **/ -void +gboolean gtk_combo_box_set_active_id (GtkComboBox *combo_box, const gchar *active_id) { GtkTreeModel *model; GtkTreeIter iter; + gboolean match = FALSE; gint column; - g_return_if_fail (GTK_IS_COMBO_BOX (combo_box)); + g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE); + + if (active_id == NULL) + { + gtk_combo_box_set_active (combo_box, -1); + return TRUE; /* active row was successfully unset */ + } column = combo_box->priv->id_column; if (column < 0) - return; + return FALSE; model = gtk_combo_box_get_model (combo_box); - g_return_if_fail (gtk_tree_model_get_column_type (model, column) == - G_TYPE_STRING); + g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) == + G_TYPE_STRING, FALSE); if (gtk_tree_model_get_iter_first (model, &iter)) do { - gboolean match; gchar *id; gtk_tree_model_get (model, &iter, column, &id, -1); - match = strcmp (id, active_id) == 0; + if (id != NULL) + match = strcmp (id, active_id) == 0; g_free (id); if (match) @@ -5515,4 +5873,6 @@ gtk_combo_box_set_active_id (GtkComboBox *combo_box, break; } } while (gtk_tree_model_iter_next (model, &iter)); + + return match; }