]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
combobox: use gtk_container_class_handle_border_width()
[~andy/gtk] / gtk / gtkcombobox.c
1 /* gtkcombobox.c
2  * Copyright (C) 2002, 2003  Kristian Rietveld <kris@gtk.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21 #include "gtkcombobox.h"
22 #include "gtkcellareabox.h"
23 #include "gtktreemenu.h"
24 #include "gtkarrow.h"
25 #include "gtkbindings.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcellview.h"
29 #include "gtkeventbox.h"
30 #include "gtkframe.h"
31 #include "gtkbox.h"
32 #include "gtkliststore.h"
33 #include "gtkmain.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkscrolledwindow.h"
37 #include "gtkseparatormenuitem.h"
38 #include "deprecated/gtktearoffmenuitem.h"
39 #include "gtktogglebutton.h"
40 #include "gtktreeselection.h"
41 #include "gtkseparator.h"
42 #include "gtkwindow.h"
43 #include "gtktypebuiltins.h"
44 #include "gtkprivate.h"
45
46 #include <gobject/gvaluecollector.h>
47
48 #include <string.h>
49 #include <stdarg.h>
50
51 #include "gtkmarshalers.h"
52 #include "gtkintl.h"
53
54 #include "gtkentryprivate.h"
55 #include "gtktreeprivate.h"
56 #include "a11y/gtkcomboboxaccessible.h"
57
58
59 /**
60  * SECTION:gtkcombobox
61  * @Short_description: A widget used to choose from a list of items
62  * @Title: GtkComboBox
63  * @See_also: #GtkComboBoxText, #GtkTreeModel, #GtkCellRenderer
64  *
65  * A GtkComboBox is a widget that allows the user to choose from a list of
66  * valid choices. The GtkComboBox displays the selected choice. When
67  * activated, the GtkComboBox displays a popup which allows the user to
68  * make a new choice. The style in which the selected value is displayed,
69  * and the style of the popup is determined by the current theme. It may
70  * be similar to a Windows-style combo box.
71  *
72  * The GtkComboBox uses the model-view pattern; the list of valid choices
73  * is specified in the form of a tree model, and the display of the choices
74  * can be adapted to the data in the model by using cell renderers, as you
75  * would in a tree view. This is possible since GtkComboBox implements the
76  * #GtkCellLayout interface. The tree model holding the valid choices is
77  * not restricted to a flat list, it can be a real tree, and the popup will
78  * reflect the tree structure.
79  *
80  * To allow the user to enter values not in the model, the 'has-entry'
81  * property allows the GtkComboBox to contain a #GtkEntry. This entry
82  * can be accessed by calling gtk_bin_get_child() on the combo box.
83  *
84  * For a simple list of textual choices, the model-view API of GtkComboBox
85  * can be a bit overwhelming. In this case, #GtkComboBoxText offers a
86  * simple alternative. Both GtkComboBox and #GtkComboBoxText can contain
87  * an entry.
88  */
89
90
91 /* WELCOME, to THE house of evil code */
92 struct _GtkComboBoxPrivate
93 {
94   GtkTreeModel *model;
95
96   GtkCellArea *area;
97
98   gint col_column;
99   gint row_column;
100
101   gint wrap_width;
102   GtkShadowType shadow_type;
103
104   gint active; /* Only temporary */
105   GtkTreeRowReference *active_row;
106
107   GtkWidget *tree_view;
108
109   GtkWidget *cell_view;
110   GtkWidget *cell_view_frame;
111
112   GtkWidget *button;
113   GtkWidget *box;
114   GtkWidget *arrow;
115   GtkWidget *separator;
116
117   GtkWidget *popup_widget;
118   GtkWidget *popup_window;
119   GtkWidget *scrolled_window;
120
121   gulong inserted_id;
122   gulong deleted_id;
123   gulong reordered_id;
124   gulong changed_id;
125   guint popup_idle_id;
126   guint activate_button;
127   guint32 activate_time;
128   guint scroll_timer;
129   guint resize_idle_id;
130
131   /* For "has-entry" specific behavior we track
132    * an automated cell renderer and text column
133    */
134   gint  text_column;
135   GtkCellRenderer *text_renderer;
136
137   gint id_column;
138
139   guint popup_in_progress : 1;
140   guint popup_shown : 1;
141   guint add_tearoffs : 1;
142   guint has_frame : 1;
143   guint is_cell_renderer : 1;
144   guint editing_canceled : 1;
145   guint auto_scroll : 1;
146   guint focus_on_click : 1;
147   guint button_sensitivity : 2;
148   guint has_entry : 1;
149   guint popup_fixed_width : 1;
150
151   GtkTreeViewRowSeparatorFunc row_separator_func;
152   gpointer                    row_separator_data;
153   GDestroyNotify              row_separator_destroy;
154
155   GdkDevice *grab_pointer;
156   GdkDevice *grab_keyboard;
157
158   gchar *tearoff_title;
159 };
160
161 /* While debugging this evil code, I have learned that
162  * there are actually 4 modes to this widget, which can
163  * be characterized as follows
164  * 
165  * 1) menu mode, no child added
166  *
167  * tree_view -> NULL
168  * cell_view -> GtkCellView, regular child
169  * cell_view_frame -> NULL
170  * button -> GtkToggleButton set_parent to combo
171  * arrow -> GtkArrow set_parent to button
172  * separator -> GtkVSepator set_parent to button
173  * popup_widget -> GtkMenu
174  * popup_window -> NULL
175  * scrolled_window -> NULL
176  *
177  * 2) menu mode, child added
178  * 
179  * tree_view -> NULL
180  * cell_view -> NULL 
181  * cell_view_frame -> NULL
182  * button -> GtkToggleButton set_parent to combo
183  * arrow -> GtkArrow, child of button
184  * separator -> NULL
185  * popup_widget -> GtkMenu
186  * popup_window -> NULL
187  * scrolled_window -> NULL
188  *
189  * 3) list mode, no child added
190  * 
191  * tree_view -> GtkTreeView, child of scrolled_window
192  * cell_view -> GtkCellView, regular child
193  * cell_view_frame -> GtkFrame, set parent to combo
194  * button -> GtkToggleButton, set_parent to combo
195  * arrow -> GtkArrow, child of button
196  * separator -> NULL
197  * popup_widget -> tree_view
198  * popup_window -> GtkWindow
199  * scrolled_window -> GtkScrolledWindow, child of popup_window
200  *
201  * 4) list mode, child added
202  *
203  * tree_view -> GtkTreeView, child of scrolled_window
204  * cell_view -> NULL
205  * cell_view_frame -> NULL
206  * button -> GtkToggleButton, set_parent to combo
207  * arrow -> GtkArrow, child of button
208  * separator -> NULL
209  * popup_widget -> tree_view
210  * popup_window -> GtkWindow
211  * scrolled_window -> GtkScrolledWindow, child of popup_window
212  * 
213  */
214
215 enum {
216   CHANGED,
217   MOVE_ACTIVE,
218   POPUP,
219   POPDOWN,
220   FORMAT_ENTRY_TEXT,
221   LAST_SIGNAL
222 };
223
224 enum {
225   PROP_0,
226   PROP_MODEL,
227   PROP_WRAP_WIDTH,
228   PROP_ROW_SPAN_COLUMN,
229   PROP_COLUMN_SPAN_COLUMN,
230   PROP_ACTIVE,
231   PROP_ADD_TEAROFFS,
232   PROP_TEAROFF_TITLE,
233   PROP_HAS_FRAME,
234   PROP_FOCUS_ON_CLICK,
235   PROP_POPUP_SHOWN,
236   PROP_BUTTON_SENSITIVITY,
237   PROP_EDITING_CANCELED,
238   PROP_HAS_ENTRY,
239   PROP_ENTRY_TEXT_COLUMN,
240   PROP_POPUP_FIXED_WIDTH,
241   PROP_ID_COLUMN,
242   PROP_ACTIVE_ID,
243   PROP_CELL_AREA
244 };
245
246 static guint combo_box_signals[LAST_SIGNAL] = {0,};
247
248 #define SCROLL_TIME  100
249
250 /* common */
251
252 static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
253 static void     gtk_combo_box_cell_editable_init   (GtkCellEditableIface *iface);
254 static GObject *gtk_combo_box_constructor          (GType                  type,
255                                                     guint                  n_construct_properties,
256                                                     GObjectConstructParam *construct_properties);
257 static void     gtk_combo_box_dispose              (GObject          *object);
258 static void     gtk_combo_box_finalize             (GObject          *object);
259 static void     gtk_combo_box_destroy              (GtkWidget        *widget);
260
261 static void     gtk_combo_box_set_property         (GObject         *object,
262                                                     guint            prop_id,
263                                                     const GValue    *value,
264                                                     GParamSpec      *spec);
265 static void     gtk_combo_box_get_property         (GObject         *object,
266                                                     guint            prop_id,
267                                                     GValue          *value,
268                                                     GParamSpec      *spec);
269
270 static void     gtk_combo_box_state_flags_changed  (GtkWidget       *widget,
271                                                     GtkStateFlags    previous);
272 static void     gtk_combo_box_grab_focus           (GtkWidget       *widget);
273 static void     gtk_combo_box_style_updated        (GtkWidget       *widget);
274 static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
275                                                     gpointer         data);
276 static void     gtk_combo_box_button_state_flags_changed (GtkWidget     *widget,
277                                                           GtkStateFlags  previous,
278                                                           gpointer       data);
279 static void     gtk_combo_box_add                  (GtkContainer    *container,
280                                                     GtkWidget       *widget);
281 static void     gtk_combo_box_remove               (GtkContainer    *container,
282                                                     GtkWidget       *widget);
283
284 static void     gtk_combo_box_menu_show            (GtkWidget        *menu,
285                                                     gpointer          user_data);
286 static void     gtk_combo_box_menu_hide            (GtkWidget        *menu,
287                                                     gpointer          user_data);
288
289 static void     gtk_combo_box_set_popup_widget     (GtkComboBox      *combo_box,
290                                                     GtkWidget        *popup);
291 static void     gtk_combo_box_menu_position_below  (GtkMenu          *menu,
292                                                     gint             *x,
293                                                     gint             *y,
294                                                     gint             *push_in,
295                                                     gpointer          user_data);
296 static void     gtk_combo_box_menu_position_over   (GtkMenu          *menu,
297                                                     gint             *x,
298                                                     gint             *y,
299                                                     gint             *push_in,
300                                                     gpointer          user_data);
301 static void     gtk_combo_box_menu_position        (GtkMenu          *menu,
302                                                     gint             *x,
303                                                     gint             *y,
304                                                     gint             *push_in,
305                                                     gpointer          user_data);
306
307 static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);
308
309 static void     gtk_combo_box_size_allocate        (GtkWidget        *widget,
310                                                     GtkAllocation    *allocation);
311 static void     gtk_combo_box_forall               (GtkContainer     *container,
312                                                     gboolean          include_internals,
313                                                     GtkCallback       callback,
314                                                     gpointer          callback_data);
315 static gboolean gtk_combo_box_draw                 (GtkWidget        *widget,
316                                                     cairo_t          *cr);
317 static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
318                                                     GdkEventScroll   *event);
319 static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
320                                                     GtkTreePath      *path);
321
322 static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);
323 static void     gtk_combo_box_real_move_active     (GtkComboBox      *combo_box,
324                                                     GtkScrollType     scroll);
325 static void     gtk_combo_box_real_popup           (GtkComboBox      *combo_box);
326 static gboolean gtk_combo_box_real_popdown         (GtkComboBox      *combo_box);
327
328 /* listening to the model */
329 static void     gtk_combo_box_model_row_inserted   (GtkTreeModel     *model,
330                                                     GtkTreePath      *path,
331                                                     GtkTreeIter      *iter,
332                                                     gpointer          user_data);
333 static void     gtk_combo_box_model_row_deleted    (GtkTreeModel     *model,
334                                                     GtkTreePath      *path,
335                                                     gpointer          user_data);
336 static void     gtk_combo_box_model_rows_reordered (GtkTreeModel     *model,
337                                                     GtkTreePath      *path,
338                                                     GtkTreeIter      *iter,
339                                                     gint             *new_order,
340                                                     gpointer          user_data);
341 static void     gtk_combo_box_model_row_changed    (GtkTreeModel     *model,
342                                                     GtkTreePath      *path,
343                                                     GtkTreeIter      *iter,
344                                                     gpointer          data);
345 static void     gtk_combo_box_model_row_expanded   (GtkTreeModel     *model,
346                                                     GtkTreePath      *path,
347                                                     GtkTreeIter      *iter,
348                                                     gpointer          data);
349
350 /* list */
351 static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box,
352                                                     gint             *x,
353                                                     gint             *y,
354                                                     gint             *width,
355                                                     gint             *height);
356 static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
357 static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);
358
359 static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
360                                                     GdkEventButton   *event,
361                                                     gpointer          data);
362 static gboolean gtk_combo_box_list_key_press       (GtkWidget        *widget,
363                                                     GdkEventKey      *event,
364                                                     gpointer          data);
365 static gboolean gtk_combo_box_list_enter_notify    (GtkWidget        *widget,
366                                                     GdkEventCrossing *event,
367                                                     gpointer          data);
368 static void     gtk_combo_box_list_auto_scroll     (GtkComboBox   *combo,
369                                                     gint           x,
370                                                     gint           y);
371 static gboolean gtk_combo_box_list_scroll_timeout  (GtkComboBox   *combo);
372 static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
373                                                     GdkEventButton   *event,
374                                                     gpointer          data);
375
376 static gboolean gtk_combo_box_list_select_func     (GtkTreeSelection *selection,
377                                                     GtkTreeModel     *model,
378                                                     GtkTreePath      *path,
379                                                     gboolean          path_currently_selected,
380                                                     gpointer          data);
381
382 static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
383                                                     GtkTreePath      *path,
384                                                     GtkTreeIter      *iter,
385                                                     gpointer          data);
386 static void     gtk_combo_box_list_popup_resize    (GtkComboBox      *combo_box);
387
388 /* menu */
389 static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
390                                                     gboolean          add_children);
391 static void     gtk_combo_box_update_title         (GtkComboBox      *combo_box);
392 static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);
393
394
395 static gboolean gtk_combo_box_menu_button_press    (GtkWidget        *widget,
396                                                     GdkEventButton   *event,
397                                                     gpointer          user_data);
398 static void     gtk_combo_box_menu_activate        (GtkWidget        *menu,
399                                                     const gchar      *path,
400                                                     GtkComboBox      *combo_box);
401 static void     gtk_combo_box_update_sensitivity   (GtkComboBox      *combo_box);
402 static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
403                                                     GdkEventKey      *event,
404                                                     gpointer          data);
405 static void     gtk_combo_box_menu_popup           (GtkComboBox      *combo_box,
406                                                     guint             button,
407                                                     guint32           activate_time);
408
409 /* cell layout */
410 static GtkCellArea *gtk_combo_box_cell_layout_get_area       (GtkCellLayout    *cell_layout);
411
412 static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
413                                                               gboolean      group_cycling);
414
415 static void     gtk_combo_box_child_show                     (GtkWidget       *widget,
416                                                               GtkComboBox     *combo_box);
417 static void     gtk_combo_box_child_hide                     (GtkWidget       *widget,
418                                                               GtkComboBox     *combo_box);
419
420 /* GtkComboBox:has-entry callbacks */
421 static void     gtk_combo_box_entry_contents_changed         (GtkEntry        *entry,
422                                                               gpointer         user_data);
423 static void     gtk_combo_box_entry_active_changed           (GtkComboBox     *combo_box,
424                                                               gpointer         user_data);
425 static gchar   *gtk_combo_box_format_entry_text              (GtkComboBox     *combo_box,
426                                                               const gchar     *path);
427
428 /* GtkBuildable method implementation */
429 static GtkBuildableIface *parent_buildable_iface;
430
431 static void     gtk_combo_box_buildable_init                 (GtkBuildableIface *iface);
432 static gboolean gtk_combo_box_buildable_custom_tag_start     (GtkBuildable  *buildable,
433                                                               GtkBuilder    *builder,
434                                                               GObject       *child,
435                                                               const gchar   *tagname,
436                                                               GMarkupParser *parser,
437                                                               gpointer      *data);
438 static void     gtk_combo_box_buildable_custom_tag_end       (GtkBuildable  *buildable,
439                                                               GtkBuilder    *builder,
440                                                               GObject       *child,
441                                                               const gchar   *tagname,
442                                                               gpointer      *data);
443 static GObject *gtk_combo_box_buildable_get_internal_child   (GtkBuildable *buildable,
444                                                               GtkBuilder   *builder,
445                                                               const gchar  *childname);
446
447
448 /* GtkCellEditable method implementations */
449 static void     gtk_combo_box_start_editing                  (GtkCellEditable *cell_editable,
450                                                               GdkEvent        *event);
451
452 static void     gtk_combo_box_get_preferred_width            (GtkWidget    *widget,
453                                                               gint         *minimum_size,
454                                                               gint         *natural_size);
455 static void     gtk_combo_box_get_preferred_height           (GtkWidget    *widget,
456                                                               gint         *minimum_size,
457                                                               gint         *natural_size);
458 static void     gtk_combo_box_get_preferred_width_for_height (GtkWidget    *widget,
459                                                               gint          avail_size,
460                                                               gint         *minimum_size,
461                                                               gint         *natural_size);
462 static void     gtk_combo_box_get_preferred_height_for_width (GtkWidget    *widget,
463                                                               gint          avail_size,
464                                                               gint         *minimum_size,
465                                                               gint         *natural_size);
466 static GtkWidgetPath *gtk_combo_box_get_path_for_child       (GtkContainer *container,
467                                                               GtkWidget    *child);
468 static void     gtk_combo_box_direction_changed              (GtkWidget    *widget,
469                                                               GtkTextDirection  previous_direction);
470
471 G_DEFINE_TYPE_WITH_CODE (GtkComboBox, gtk_combo_box, GTK_TYPE_BIN,
472                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
473                                                 gtk_combo_box_cell_layout_init)
474                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
475                                                 gtk_combo_box_cell_editable_init)
476                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
477                                                 gtk_combo_box_buildable_init))
478
479
480 /* common */
481 static void
482 gtk_combo_box_class_init (GtkComboBoxClass *klass)
483 {
484   GObjectClass *object_class;
485   GtkContainerClass *container_class;
486   GtkWidgetClass *widget_class;
487   GtkBindingSet *binding_set;
488
489   container_class = (GtkContainerClass *)klass;
490   container_class->forall = gtk_combo_box_forall;
491   container_class->add = gtk_combo_box_add;
492   container_class->remove = gtk_combo_box_remove;
493   container_class->get_path_for_child = gtk_combo_box_get_path_for_child;
494
495   gtk_container_class_handle_border_width (container_class);
496
497   widget_class = (GtkWidgetClass *)klass;
498   widget_class->size_allocate = gtk_combo_box_size_allocate;
499   widget_class->draw = gtk_combo_box_draw;
500   widget_class->scroll_event = gtk_combo_box_scroll_event;
501   widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
502   widget_class->grab_focus = gtk_combo_box_grab_focus;
503   widget_class->style_updated = gtk_combo_box_style_updated;
504   widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
505   widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
506   widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
507   widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
508   widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
509   widget_class->destroy = gtk_combo_box_destroy;
510   widget_class->direction_changed = gtk_combo_box_direction_changed;
511
512   object_class = (GObjectClass *)klass;
513   object_class->constructor = gtk_combo_box_constructor;
514   object_class->dispose = gtk_combo_box_dispose;
515   object_class->finalize = gtk_combo_box_finalize;
516   object_class->set_property = gtk_combo_box_set_property;
517   object_class->get_property = gtk_combo_box_get_property;
518
519   klass->format_entry_text = gtk_combo_box_format_entry_text;
520
521   /* signals */
522   /**
523    * GtkComboBox::changed:
524    * @widget: the object which received the signal
525    * 
526    * The changed signal is emitted when the active
527    * item is changed. The can be due to the user selecting
528    * a different item from the list, or due to a
529    * call to gtk_combo_box_set_active_iter().
530    * It will also be emitted while typing into the entry of a combo box
531    * with an entry.
532    *
533    * Since: 2.4
534    */
535   combo_box_signals[CHANGED] =
536     g_signal_new (I_("changed"),
537                   G_OBJECT_CLASS_TYPE (klass),
538                   G_SIGNAL_RUN_LAST,
539                   G_STRUCT_OFFSET (GtkComboBoxClass, changed),
540                   NULL, NULL,
541                   g_cclosure_marshal_VOID__VOID,
542                   G_TYPE_NONE, 0);
543   /**
544    * GtkComboBox::move-active:
545    * @widget: the object that received the signal
546    * @scroll_type: a #GtkScrollType
547    *
548    * The ::move-active signal is a
549    * <link linkend="keybinding-signals">keybinding signal</link>
550    * which gets emitted to move the active selection.
551    *
552    * Since: 2.12
553    */
554   combo_box_signals[MOVE_ACTIVE] =
555     g_signal_new_class_handler (I_("move-active"),
556                                 G_OBJECT_CLASS_TYPE (klass),
557                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
558                                 G_CALLBACK (gtk_combo_box_real_move_active),
559                                 NULL, NULL,
560                                 g_cclosure_marshal_VOID__ENUM,
561                                 G_TYPE_NONE, 1,
562                                 GTK_TYPE_SCROLL_TYPE);
563
564   /**
565    * GtkComboBox::popup:
566    * @widget: the object that received the signal
567    *
568    * The ::popup signal is a
569    * <link linkend="keybinding-signals">keybinding signal</link>
570    * which gets emitted to popup the combo box list.
571    *
572    * The default binding for this signal is Alt+Down.
573    *
574    * Since: 2.12
575    */
576   combo_box_signals[POPUP] =
577     g_signal_new_class_handler (I_("popup"),
578                                 G_OBJECT_CLASS_TYPE (klass),
579                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
580                                 G_CALLBACK (gtk_combo_box_real_popup),
581                                 NULL, NULL,
582                                 g_cclosure_marshal_VOID__VOID,
583                                 G_TYPE_NONE, 0);
584   /**
585    * GtkComboBox::popdown:
586    * @button: the object which received the signal
587    *
588    * The ::popdown signal is a
589    * <link linkend="keybinding-signals">keybinding signal</link>
590    * which gets emitted to popdown the combo box list.
591    *
592    * The default bindings for this signal are Alt+Up and Escape.
593    *
594    * Since: 2.12
595    */
596   combo_box_signals[POPDOWN] =
597     g_signal_new_class_handler (I_("popdown"),
598                                 G_OBJECT_CLASS_TYPE (klass),
599                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
600                                 G_CALLBACK (gtk_combo_box_real_popdown),
601                                 NULL, NULL,
602                                 _gtk_marshal_BOOLEAN__VOID,
603                                 G_TYPE_BOOLEAN, 0);
604
605   /**
606    * GtkComboBox::format-entry-text:
607    * @combo: the object which received the signal
608    * @path: the GtkTreePath string from the combo box's current model to format text for
609    *
610    * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
611    *
612    * A signal which allows you to change how the text displayed in a combo box's
613    * entry is displayed.
614    *
615    * Connect a signal handler which returns an allocated string representing
616    * @path. That string will then be used to set the text in the combo box's entry.
617    * The default signal handler uses the text from the GtkComboBox::entry-text-column 
618    * model column.
619    *
620    * Here's an example signal handler which fetches data from the model and
621    * displays it in the entry.
622    * |[
623    * static gchar*
624    * format_entry_text_callback (GtkComboBox *combo,
625    *                             const gchar *path,
626    *                             gpointer     user_data)
627    * {
628    *   GtkTreeIter iter;
629    *   GtkTreeModel model;
630    *   gdouble      value;
631    *   
632    *   model = gtk_combo_box_get_model (combo);
633    *
634    *   gtk_tree_model_get_iter_from_string (model, &iter, path);
635    *   gtk_tree_model_get (model, &iter, 
636    *                       THE_DOUBLE_VALUE_COLUMN, &value,
637    *                       -1);
638    *
639    *   return g_strdup_printf ("&percnt;g", value);
640    * }
641    * ]|
642    *
643    * Return value: (transfer full): a newly allocated string representing @path 
644    * for the current GtkComboBox model.
645    *
646    * Since: 3.4
647    */
648   combo_box_signals[FORMAT_ENTRY_TEXT] =
649     g_signal_new (I_("format-entry-text"),
650                   G_TYPE_FROM_CLASS (klass),
651                   G_SIGNAL_RUN_LAST,
652                   G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text),
653                   _gtk_single_string_accumulator, NULL,
654                   _gtk_marshal_STRING__STRING,
655                   G_TYPE_STRING, 1, G_TYPE_STRING);
656
657   /* key bindings */
658   binding_set = gtk_binding_set_by_class (widget_class);
659
660   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
661                                 "popup", 0);
662   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
663                                 "popup", 0);
664
665   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
666                                 "popdown", 0);
667   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
668                                 "popdown", 0);
669   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
670                                 "popdown", 0);
671
672   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
673                                 "move-active", 1,
674                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
675   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
676                                 "move-active", 1,
677                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
678   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
679                                 "move-active", 1,
680                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
681   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
682                                 "move-active", 1,
683                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
684   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
685                                 "move-active", 1,
686                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
687   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
688                                 "move-active", 1,
689                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
690
691   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
692                                 "move-active", 1,
693                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
694   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
695                                 "move-active", 1,
696                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
697   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
698                                 "move-active", 1,
699                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
700   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
701                                 "move-active", 1,
702                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
703   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
704                                 "move-active", 1,
705                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
706   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
707                                 "move-active", 1,
708                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
709
710   /* properties */
711   g_object_class_override_property (object_class,
712                                     PROP_EDITING_CANCELED,
713                                     "editing-canceled");
714
715   /**
716    * GtkComboBox:model:
717    *
718    * The model from which the combo box takes the values shown
719    * in the list.
720    *
721    * Since: 2.4
722    */
723   g_object_class_install_property (object_class,
724                                    PROP_MODEL,
725                                    g_param_spec_object ("model",
726                                                         P_("ComboBox model"),
727                                                         P_("The model for the combo box"),
728                                                         GTK_TYPE_TREE_MODEL,
729                                                         GTK_PARAM_READWRITE));
730
731   /**
732    * GtkComboBox:wrap-width:
733    *
734    * If wrap-width is set to a positive value, the list will be
735    * displayed in multiple columns, the number of columns is
736    * determined by wrap-width.
737    *
738    * Since: 2.4
739    */
740   g_object_class_install_property (object_class,
741                                    PROP_WRAP_WIDTH,
742                                    g_param_spec_int ("wrap-width",
743                                                      P_("Wrap width"),
744                                                      P_("Wrap width for laying out the items in a grid"),
745                                                      0,
746                                                      G_MAXINT,
747                                                      0,
748                                                      GTK_PARAM_READWRITE));
749
750
751   /**
752    * GtkComboBox:row-span-column:
753    *
754    * If this is set to a non-negative value, it must be the index of a column
755    * of type %G_TYPE_INT in the model.
756    *
757    * The values of that column are used to determine how many rows a value in
758    * the list will span. Therefore, the values in the model column pointed to
759    * by this property must be greater than zero and not larger than wrap-width.
760    *
761    * Since: 2.4
762    */
763   g_object_class_install_property (object_class,
764                                    PROP_ROW_SPAN_COLUMN,
765                                    g_param_spec_int ("row-span-column",
766                                                      P_("Row span column"),
767                                                      P_("TreeModel column containing the row span values"),
768                                                      -1,
769                                                      G_MAXINT,
770                                                      -1,
771                                                      GTK_PARAM_READWRITE));
772
773
774   /**
775    * GtkComboBox:column-span-column:
776    *
777    * If this is set to a non-negative value, it must be the index of a column
778    * of type %G_TYPE_INT in the model.
779    *
780    * The values of that column are used to determine how many columns a value
781    * in the list will span.
782    *
783    * Since: 2.4
784    */
785   g_object_class_install_property (object_class,
786                                    PROP_COLUMN_SPAN_COLUMN,
787                                    g_param_spec_int ("column-span-column",
788                                                      P_("Column span column"),
789                                                      P_("TreeModel column containing the column span values"),
790                                                      -1,
791                                                      G_MAXINT,
792                                                      -1,
793                                                      GTK_PARAM_READWRITE));
794
795
796   /**
797    * GtkComboBox:active:
798    *
799    * The item which is currently active. If the model is a non-flat treemodel,
800    * and the active item is not an immediate child of the root of the tree,
801    * this property has the value
802    * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
803    * where <literal>path</literal> is the #GtkTreePath of the active item.
804    *
805    * Since: 2.4
806    */
807   g_object_class_install_property (object_class,
808                                    PROP_ACTIVE,
809                                    g_param_spec_int ("active",
810                                                      P_("Active item"),
811                                                      P_("The item which is currently active"),
812                                                      -1,
813                                                      G_MAXINT,
814                                                      -1,
815                                                      GTK_PARAM_READWRITE));
816
817   /**
818    * GtkComboBox:add-tearoffs:
819    *
820    * The add-tearoffs property controls whether generated menus
821    * have tearoff menu items.
822    *
823    * Note that this only affects menu style combo boxes.
824    *
825    * Since: 2.6
826    */
827   g_object_class_install_property (object_class,
828                                    PROP_ADD_TEAROFFS,
829                                    g_param_spec_boolean ("add-tearoffs",
830                                                          P_("Add tearoffs to menus"),
831                                                          P_("Whether dropdowns should have a tearoff menu item"),
832                                                          FALSE,
833                                                          GTK_PARAM_READWRITE));
834
835   /**
836    * GtkComboBox:has-frame:
837    *
838    * The has-frame property controls whether a frame
839    * is drawn around the entry.
840    *
841    * Since: 2.6
842    */
843   g_object_class_install_property (object_class,
844                                    PROP_HAS_FRAME,
845                                    g_param_spec_boolean ("has-frame",
846                                                          P_("Has Frame"),
847                                                          P_("Whether the combo box draws a frame around the child"),
848                                                          TRUE,
849                                                          GTK_PARAM_READWRITE));
850
851   g_object_class_install_property (object_class,
852                                    PROP_FOCUS_ON_CLICK,
853                                    g_param_spec_boolean ("focus-on-click",
854                                                          P_("Focus on click"),
855                                                          P_("Whether the combo box grabs focus when it is clicked with the mouse"),
856                                                          TRUE,
857                                                          GTK_PARAM_READWRITE));
858
859   /**
860    * GtkComboBox:tearoff-title:
861    *
862    * A title that may be displayed by the window manager
863    * when the popup is torn-off.
864    *
865    * Since: 2.10
866    */
867   g_object_class_install_property (object_class,
868                                    PROP_TEAROFF_TITLE,
869                                    g_param_spec_string ("tearoff-title",
870                                                         P_("Tearoff Title"),
871                                                         P_("A title that may be displayed by the window manager when the popup is torn-off"),
872                                                         NULL,
873                                                         GTK_PARAM_READWRITE));
874
875
876   /**
877    * GtkComboBox:popup-shown:
878    *
879    * Whether the combo boxes dropdown is popped up.
880    * Note that this property is mainly useful, because
881    * it allows you to connect to notify::popup-shown.
882    *
883    * Since: 2.10
884    */
885   g_object_class_install_property (object_class,
886                                    PROP_POPUP_SHOWN,
887                                    g_param_spec_boolean ("popup-shown",
888                                                          P_("Popup shown"),
889                                                          P_("Whether the combo's dropdown is shown"),
890                                                          FALSE,
891                                                          GTK_PARAM_READABLE));
892
893
894    /**
895     * GtkComboBox:button-sensitivity:
896     *
897     * Whether the dropdown button is sensitive when
898     * the model is empty.
899     *
900     * Since: 2.14
901     */
902    g_object_class_install_property (object_class,
903                                     PROP_BUTTON_SENSITIVITY,
904                                     g_param_spec_enum ("button-sensitivity",
905                                                        P_("Button Sensitivity"),
906                                                        P_("Whether the dropdown button is sensitive when the model is empty"),
907                                                        GTK_TYPE_SENSITIVITY_TYPE,
908                                                        GTK_SENSITIVITY_AUTO,
909                                                        GTK_PARAM_READWRITE));
910
911    /**
912     * GtkComboBox:has-entry:
913     *
914     * Whether the combo box has an entry.
915     *
916     * Since: 2.24
917     */
918    g_object_class_install_property (object_class,
919                                     PROP_HAS_ENTRY,
920                                     g_param_spec_boolean ("has-entry",
921                                                           P_("Has Entry"),
922                                                           P_("Whether combo box has an entry"),
923                                                           FALSE,
924                                                           GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
925
926    /**
927     * GtkComboBox:entry-text-column:
928     *
929     * The column in the combo box's model to associate with strings from the entry
930     * if the combo was created with #GtkComboBox:has-entry = %TRUE.
931     *
932     * Since: 2.24
933     */
934    g_object_class_install_property (object_class,
935                                     PROP_ENTRY_TEXT_COLUMN,
936                                     g_param_spec_int ("entry-text-column",
937                                                       P_("Entry Text Column"),
938                                                       P_("The column in the combo box's model to associate "
939                                                          "with strings from the entry if the combo was "
940                                                          "created with #GtkComboBox:has-entry = %TRUE"),
941                                                       -1, G_MAXINT, -1,
942                                                       GTK_PARAM_READWRITE));
943
944    /**
945     * GtkComboBox:id-column:
946     *
947     * The column in the combo box's model that provides string
948     * IDs for the values in the model, if != -1.
949     *
950     * Since: 3.0
951     */
952    g_object_class_install_property (object_class,
953                                     PROP_ID_COLUMN,
954                                     g_param_spec_int ("id-column",
955                                                       P_("ID Column"),
956                                                       P_("The column in the combo box's model that provides "
957                                                       "string IDs for the values in the model"),
958                                                       -1, G_MAXINT, -1,
959                                                       GTK_PARAM_READWRITE));
960
961    /**
962     * GtkComboBox:active-id:
963     *
964     * The value of the ID column of the active row.
965     *
966     * Since: 3.0
967     */
968    g_object_class_install_property (object_class,
969                                     PROP_ACTIVE_ID,
970                                     g_param_spec_string ("active-id",
971                                                          P_("Active id"),
972                                                          P_("The value of the id column "
973                                                          "for the active row"),
974                                                          NULL, GTK_PARAM_READWRITE));
975
976    /**
977     * GtkComboBox:popup-fixed-width:
978     *
979     * Whether the popup's width should be a fixed width matching the
980     * allocated width of the combo box.
981     *
982     * Since: 3.0
983     */
984    g_object_class_install_property (object_class,
985                                     PROP_POPUP_FIXED_WIDTH,
986                                     g_param_spec_boolean ("popup-fixed-width",
987                                                           P_("Popup Fixed Width"),
988                                                           P_("Whether the popup's width should be a "
989                                                              "fixed width matching the allocated width "
990                                                              "of the combo box"),
991                                                           TRUE,
992                                                           GTK_PARAM_READWRITE));
993
994    /**
995     * GtkComboBox:cell-area:
996     *
997     * The #GtkCellArea used to layout cell renderers for this combo box.
998     *
999     * If no area is specified when creating the combo box with gtk_combo_box_new_with_area() 
1000     * a horizontally oriented #GtkCellAreaBox will be used.
1001     *
1002     * Since: 3.0
1003     */
1004    g_object_class_install_property (object_class,
1005                                     PROP_CELL_AREA,
1006                                     g_param_spec_object ("cell-area",
1007                                                          P_("Cell Area"),
1008                                                          P_("The GtkCellArea used to layout cells"),
1009                                                          GTK_TYPE_CELL_AREA,
1010                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1011
1012   gtk_widget_class_install_style_property (widget_class,
1013                                            g_param_spec_boolean ("appears-as-list",
1014                                                                  P_("Appears as list"),
1015                                                                  P_("Whether dropdowns should look like lists rather than menus"),
1016                                                                  FALSE,
1017                                                                  GTK_PARAM_READABLE));
1018
1019   /**
1020    * GtkComboBox:arrow-size:
1021    *
1022    * Sets the minimum size of the arrow in the combo box.  Note
1023    * that the arrow size is coupled to the font size, so in case
1024    * a larger font is used, the arrow will be larger than set
1025    * by arrow size.
1026    *
1027    * Since: 2.12
1028    */
1029   gtk_widget_class_install_style_property (widget_class,
1030                                            g_param_spec_int ("arrow-size",
1031                                                              P_("Arrow Size"),
1032                                                              P_("The minimum size of the arrow in the combo box"),
1033                                                              0,
1034                                                              G_MAXINT,
1035                                                              15,
1036                                                              GTK_PARAM_READABLE));
1037
1038   /**
1039    * GtkComboBox:arrow-scaling:
1040    *
1041    * Sets the amount of space used up by the combobox arrow,
1042    * proportional to the font size.
1043    *
1044    * Since: 3.2
1045    */
1046   gtk_widget_class_install_style_property (widget_class,
1047                                            g_param_spec_float ("arrow-scaling",
1048                                                                P_("Arrow Scaling"),
1049                                                                P_("The amount of space used by the arrow"),
1050                                                              0,
1051                                                              2.0,
1052                                                              1.0,
1053                                                              GTK_PARAM_READABLE));
1054
1055   /**
1056    * GtkComboBox:shadow-type:
1057    *
1058    * Which kind of shadow to draw around the combo box.
1059    *
1060    * Since: 2.12
1061    */
1062   gtk_widget_class_install_style_property (widget_class,
1063                                            g_param_spec_enum ("shadow-type",
1064                                                               P_("Shadow type"),
1065                                                               P_("Which kind of shadow to draw around the combo box"),
1066                                                               GTK_TYPE_SHADOW_TYPE,
1067                                                               GTK_SHADOW_NONE,
1068                                                               GTK_PARAM_READABLE));
1069
1070   g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1071
1072   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1073 }
1074
1075 static void
1076 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1077 {
1078   parent_buildable_iface = g_type_interface_peek_parent (iface);
1079   iface->add_child = _gtk_cell_layout_buildable_add_child;
1080   iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1081   iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1082   iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1083 }
1084
1085 static void
1086 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1087 {
1088   iface->get_area = gtk_combo_box_cell_layout_get_area;
1089 }
1090
1091 static void
1092 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1093 {
1094   iface->start_editing = gtk_combo_box_start_editing;
1095 }
1096
1097 static void
1098 gtk_combo_box_init (GtkComboBox *combo_box)
1099 {
1100   GtkComboBoxPrivate *priv;
1101
1102   combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1103                                                  GTK_TYPE_COMBO_BOX,
1104                                                  GtkComboBoxPrivate);
1105   priv = combo_box->priv;
1106
1107   priv->wrap_width = 0;
1108
1109   priv->active = -1;
1110   priv->active_row = NULL;
1111   priv->col_column = -1;
1112   priv->row_column = -1;
1113
1114   priv->popup_shown = FALSE;
1115   priv->add_tearoffs = FALSE;
1116   priv->has_frame = TRUE;
1117   priv->is_cell_renderer = FALSE;
1118   priv->editing_canceled = FALSE;
1119   priv->auto_scroll = FALSE;
1120   priv->focus_on_click = TRUE;
1121   priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1122   priv->has_entry = FALSE;
1123   priv->popup_fixed_width = TRUE;
1124
1125   priv->text_column = -1;
1126   priv->text_renderer = NULL;
1127   priv->id_column = -1;
1128 }
1129
1130 static void
1131 gtk_combo_box_set_property (GObject      *object,
1132                             guint         prop_id,
1133                             const GValue *value,
1134                             GParamSpec   *pspec)
1135 {
1136   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1137   GtkComboBoxPrivate *priv = combo_box->priv;
1138   GtkCellArea *area;
1139
1140   switch (prop_id)
1141     {
1142     case PROP_MODEL:
1143       gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1144       break;
1145
1146     case PROP_WRAP_WIDTH:
1147       gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1148       break;
1149
1150     case PROP_ROW_SPAN_COLUMN:
1151       gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1152       break;
1153
1154     case PROP_COLUMN_SPAN_COLUMN:
1155       gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1156       break;
1157
1158     case PROP_ACTIVE:
1159       gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1160       break;
1161
1162     case PROP_ADD_TEAROFFS:
1163       gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1164       break;
1165
1166     case PROP_HAS_FRAME:
1167       priv->has_frame = g_value_get_boolean (value);
1168
1169       if (priv->has_entry)
1170         {
1171           GtkWidget *child;
1172
1173           child = gtk_bin_get_child (GTK_BIN (combo_box));
1174
1175           gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1176         }
1177
1178       break;
1179
1180     case PROP_FOCUS_ON_CLICK:
1181       gtk_combo_box_set_focus_on_click (combo_box,
1182                                         g_value_get_boolean (value));
1183       break;
1184
1185     case PROP_TEAROFF_TITLE:
1186       gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1187       break;
1188
1189     case PROP_POPUP_SHOWN:
1190       if (g_value_get_boolean (value))
1191         gtk_combo_box_popup (combo_box);
1192       else
1193         gtk_combo_box_popdown (combo_box);
1194       break;
1195
1196     case PROP_BUTTON_SENSITIVITY:
1197       gtk_combo_box_set_button_sensitivity (combo_box,
1198                                             g_value_get_enum (value));
1199       break;
1200
1201     case PROP_POPUP_FIXED_WIDTH:
1202       gtk_combo_box_set_popup_fixed_width (combo_box,
1203                                            g_value_get_boolean (value));
1204       break;
1205
1206     case PROP_EDITING_CANCELED:
1207       priv->editing_canceled = g_value_get_boolean (value);
1208       break;
1209
1210     case PROP_HAS_ENTRY:
1211       priv->has_entry = g_value_get_boolean (value);
1212       break;
1213
1214     case PROP_ENTRY_TEXT_COLUMN:
1215       gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1216       break;
1217
1218     case PROP_ID_COLUMN:
1219       gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1220       break;
1221
1222     case PROP_ACTIVE_ID:
1223       gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1224       break;
1225
1226     case PROP_CELL_AREA:
1227       /* Construct-only, can only be assigned once */
1228       area = g_value_get_object (value);
1229       if (area)
1230         {
1231           if (priv->area != NULL)
1232             {
1233               g_warning ("cell-area has already been set, ignoring construct property");
1234               g_object_ref_sink (area);
1235               g_object_unref (area);
1236             }
1237           else
1238             priv->area = g_object_ref_sink (area);
1239         }
1240       break;
1241
1242     default:
1243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1244       break;
1245     }
1246 }
1247
1248 static void
1249 gtk_combo_box_get_property (GObject    *object,
1250                             guint       prop_id,
1251                             GValue     *value,
1252                             GParamSpec *pspec)
1253 {
1254   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1255   GtkComboBoxPrivate *priv = combo_box->priv;
1256
1257   switch (prop_id)
1258     {
1259       case PROP_MODEL:
1260         g_value_set_object (value, combo_box->priv->model);
1261         break;
1262
1263       case PROP_WRAP_WIDTH:
1264         g_value_set_int (value, combo_box->priv->wrap_width);
1265         break;
1266
1267       case PROP_ROW_SPAN_COLUMN:
1268         g_value_set_int (value, combo_box->priv->row_column);
1269         break;
1270
1271       case PROP_COLUMN_SPAN_COLUMN:
1272         g_value_set_int (value, combo_box->priv->col_column);
1273         break;
1274
1275       case PROP_ACTIVE:
1276         g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1277         break;
1278
1279       case PROP_ADD_TEAROFFS:
1280         g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1281         break;
1282
1283       case PROP_HAS_FRAME:
1284         g_value_set_boolean (value, combo_box->priv->has_frame);
1285         break;
1286
1287       case PROP_FOCUS_ON_CLICK:
1288         g_value_set_boolean (value, combo_box->priv->focus_on_click);
1289         break;
1290
1291       case PROP_TEAROFF_TITLE:
1292         g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1293         break;
1294
1295       case PROP_POPUP_SHOWN:
1296         g_value_set_boolean (value, combo_box->priv->popup_shown);
1297         break;
1298
1299       case PROP_BUTTON_SENSITIVITY:
1300         g_value_set_enum (value, combo_box->priv->button_sensitivity);
1301         break;
1302
1303       case PROP_POPUP_FIXED_WIDTH:
1304         g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1305         break;
1306
1307       case PROP_EDITING_CANCELED:
1308         g_value_set_boolean (value, priv->editing_canceled);
1309         break;
1310
1311       case PROP_HAS_ENTRY:
1312         g_value_set_boolean (value, priv->has_entry);
1313         break;
1314
1315       case PROP_ENTRY_TEXT_COLUMN:
1316         g_value_set_int (value, priv->text_column);
1317         break;
1318
1319       case PROP_ID_COLUMN:
1320         g_value_set_int (value, priv->id_column);
1321         break;
1322
1323       case PROP_ACTIVE_ID:
1324         g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1325         break;
1326
1327       case PROP_CELL_AREA:
1328         g_value_set_object (value, priv->area);
1329         break;
1330
1331       default:
1332         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1333         break;
1334     }
1335 }
1336
1337 static void
1338 gtk_combo_box_state_flags_changed (GtkWidget     *widget,
1339                                    GtkStateFlags  previous)
1340 {
1341   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1342   GtkComboBoxPrivate *priv = combo_box->priv;
1343
1344   if (gtk_widget_get_realized (widget))
1345     {
1346       if (priv->tree_view && priv->cell_view)
1347         {
1348           GtkStyleContext *context;
1349           GtkStateFlags state;
1350           GdkRGBA color;
1351
1352           context  = gtk_widget_get_style_context (widget);
1353           state = gtk_widget_get_state_flags (widget);
1354           gtk_style_context_get_background_color (context, state, &color);
1355
1356           gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1357         }
1358     }
1359
1360   gtk_widget_queue_draw (widget);
1361 }
1362
1363 static void
1364 gtk_combo_box_button_state_flags_changed (GtkWidget     *widget,
1365                                           GtkStateFlags  previous,
1366                                           gpointer       data)
1367 {
1368   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1369   GtkComboBoxPrivate *priv = combo_box->priv;
1370
1371   if (gtk_widget_get_realized (widget))
1372     {
1373       if (!priv->tree_view && priv->cell_view)
1374         gtk_widget_set_state_flags (priv->cell_view,
1375                                     gtk_widget_get_state_flags (widget),
1376                                     TRUE);
1377     }
1378
1379   gtk_widget_queue_draw (widget);
1380 }
1381
1382 static void
1383 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1384 {
1385   gtk_container_forall (GTK_CONTAINER (combo_box),
1386                         (GtkCallback) gtk_widget_reset_style,
1387                         NULL);
1388 }
1389
1390 static void
1391 gtk_combo_box_direction_changed (GtkWidget        *widget,
1392                                  GtkTextDirection  previous_direction)
1393 {
1394   gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1395 }
1396
1397 static GtkWidgetPath *
1398 gtk_combo_box_get_path_for_child (GtkContainer *container,
1399                                   GtkWidget    *child)
1400 {
1401   GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1402   GtkWidgetPath *path;
1403   GtkWidget *widget;
1404   gboolean found = FALSE;
1405   GList *visible_children, *l;
1406   GtkWidgetPath *sibling_path;
1407   int pos;
1408
1409   path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
1410
1411   if (gtk_widget_get_visible (child))
1412     {
1413       visible_children = NULL;
1414
1415       if (priv->button && gtk_widget_get_visible (priv->button))
1416         visible_children = g_list_prepend (visible_children, priv->button);
1417
1418       if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1419         visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1420
1421       widget = gtk_bin_get_child (GTK_BIN (container));
1422       if (widget && gtk_widget_get_visible (widget))
1423         visible_children = g_list_prepend (visible_children, widget);
1424
1425       if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1426         visible_children = g_list_reverse (visible_children);
1427
1428       pos = 0;
1429
1430       for (l = visible_children; l; l = l->next)
1431         {
1432           widget = l->data;
1433
1434           if (widget == child)
1435             {
1436               found = TRUE;
1437               break;
1438             }
1439
1440           pos++;
1441         }
1442     }
1443
1444   if (found)
1445     {
1446       sibling_path = gtk_widget_path_new ();
1447
1448       for (l = visible_children; l; l = l->next)
1449         gtk_widget_path_append_for_widget (sibling_path, l->data);
1450
1451       gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1452
1453       g_list_free (visible_children);
1454       gtk_widget_path_unref (sibling_path);
1455     }
1456   else
1457     {
1458       gtk_widget_path_append_for_widget (path, child);
1459     }
1460
1461   return path;
1462 }
1463
1464 static void
1465 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1466 {
1467   GtkComboBoxPrivate *priv = combo_box->priv;
1468   gboolean appears_as_list;
1469
1470   /* if wrap_width > 0, then we are in grid-mode and forced to use
1471    * unix style
1472    */
1473   if (priv->wrap_width)
1474     appears_as_list = FALSE;
1475   else
1476     gtk_widget_style_get (GTK_WIDGET (combo_box),
1477                           "appears-as-list", &appears_as_list,
1478                           NULL);
1479   
1480   if (appears_as_list)
1481     {
1482       /* Destroy all the menu mode widgets, if they exist. */
1483       if (GTK_IS_MENU (priv->popup_widget))
1484         gtk_combo_box_menu_destroy (combo_box);
1485
1486       /* Create the list mode widgets, if they don't already exist. */
1487       if (!GTK_IS_TREE_VIEW (priv->tree_view))
1488         gtk_combo_box_list_setup (combo_box);
1489     }
1490   else
1491     {
1492       /* Destroy all the list mode widgets, if they exist. */
1493       if (GTK_IS_TREE_VIEW (priv->tree_view))
1494         gtk_combo_box_list_destroy (combo_box);
1495
1496       /* Create the menu mode widgets, if they don't already exist. */
1497       if (!GTK_IS_MENU (priv->popup_widget))
1498         gtk_combo_box_menu_setup (combo_box, TRUE);
1499     }
1500
1501   gtk_widget_style_get (GTK_WIDGET (combo_box),
1502                         "shadow-type", &priv->shadow_type,
1503                         NULL);
1504 }
1505
1506 static void
1507 gtk_combo_box_style_updated (GtkWidget *widget)
1508 {
1509   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1510   GtkComboBoxPrivate *priv = combo_box->priv;
1511   GtkWidget *child;
1512
1513   GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1514
1515   gtk_combo_box_check_appearance (combo_box);
1516
1517   if (priv->tree_view && priv->cell_view)
1518     {
1519       GtkStyleContext *context;
1520       GtkStateFlags state;
1521       GdkRGBA color;
1522
1523       context  = gtk_widget_get_style_context (widget);
1524       state = gtk_widget_get_state_flags (widget);
1525       gtk_style_context_get_background_color (context, state, &color);
1526
1527       gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1528     }
1529
1530   child = gtk_bin_get_child (GTK_BIN (combo_box));
1531   if (GTK_IS_ENTRY (child))
1532     g_object_set (child, "shadow-type",
1533                   GTK_SHADOW_NONE == priv->shadow_type ?
1534                   GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1535 }
1536
1537 static void
1538 gtk_combo_box_button_toggled (GtkWidget *widget,
1539                               gpointer   data)
1540 {
1541   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1542
1543   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1544     {
1545       if (!combo_box->priv->popup_in_progress)
1546         gtk_combo_box_popup (combo_box);
1547     }
1548   else
1549     gtk_combo_box_popdown (combo_box);
1550 }
1551
1552 static void
1553 gtk_combo_box_add (GtkContainer *container,
1554                    GtkWidget    *widget)
1555 {
1556   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1557   GtkComboBoxPrivate *priv = combo_box->priv;
1558
1559   if (priv->has_entry && !GTK_IS_ENTRY (widget))
1560     {
1561       g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1562                  "(need an instance of GtkEntry or of a subclass)",
1563                  G_OBJECT_TYPE_NAME (widget));
1564       return;
1565     }
1566
1567   if (priv->cell_view &&
1568       gtk_widget_get_parent (priv->cell_view))
1569     {
1570       gtk_widget_unparent (priv->cell_view);
1571       _gtk_bin_set_child (GTK_BIN (container), NULL);
1572       gtk_widget_queue_resize (GTK_WIDGET (container));
1573     }
1574   
1575   gtk_widget_set_parent (widget, GTK_WIDGET (container));
1576   _gtk_bin_set_child (GTK_BIN (container), widget);
1577
1578   if (priv->cell_view &&
1579       widget != priv->cell_view)
1580     {
1581       /* since the cell_view was unparented, it's gone now */
1582       priv->cell_view = NULL;
1583
1584       if (!priv->tree_view && priv->separator)
1585         {
1586           gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1587                                 priv->separator);
1588           priv->separator = NULL;
1589
1590           gtk_widget_queue_resize (GTK_WIDGET (container));
1591         }
1592       else if (priv->cell_view_frame)
1593         {
1594           gtk_widget_unparent (priv->cell_view_frame);
1595           priv->cell_view_frame = NULL;
1596           priv->box = NULL;
1597         }
1598     }
1599
1600   if (priv->has_entry)
1601     {
1602       /* this flag is a hack to tell the entry to fill its allocation.
1603        */
1604       _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1605
1606       g_signal_connect (widget, "changed",
1607                         G_CALLBACK (gtk_combo_box_entry_contents_changed),
1608                         combo_box);
1609
1610       gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1611     }
1612 }
1613
1614 static void
1615 gtk_combo_box_remove (GtkContainer *container,
1616                       GtkWidget    *widget)
1617 {
1618   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1619   GtkComboBoxPrivate *priv = combo_box->priv;
1620   GtkTreePath *path;
1621   gboolean appears_as_list;
1622
1623   if (priv->has_entry)
1624     {
1625       GtkWidget *child_widget;
1626
1627       child_widget = gtk_bin_get_child (GTK_BIN (container));
1628       if (widget && widget == child_widget)
1629         {
1630           g_signal_handlers_disconnect_by_func (widget,
1631                                                 gtk_combo_box_entry_contents_changed,
1632                                                 container);
1633           _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1634         }
1635     }
1636
1637   if (widget == priv->cell_view)
1638     priv->cell_view = NULL;
1639
1640   gtk_widget_unparent (widget);
1641   _gtk_bin_set_child (GTK_BIN (container), NULL);
1642
1643   if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1644     return;
1645
1646   gtk_widget_queue_resize (GTK_WIDGET (container));
1647
1648   if (!priv->tree_view)
1649     appears_as_list = FALSE;
1650   else
1651     appears_as_list = TRUE;
1652   
1653   if (appears_as_list)
1654     gtk_combo_box_list_destroy (combo_box);
1655   else if (GTK_IS_MENU (priv->popup_widget))
1656     {
1657       gtk_combo_box_menu_destroy (combo_box);
1658       gtk_menu_detach (GTK_MENU (priv->popup_widget));
1659       priv->popup_widget = NULL;
1660     }
1661
1662   if (!priv->cell_view)
1663     {
1664       priv->cell_view = gtk_cell_view_new ();
1665       gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1666       _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1667
1668       gtk_widget_show (priv->cell_view);
1669       gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1670                                priv->model);
1671     }
1672
1673
1674   if (appears_as_list)
1675     gtk_combo_box_list_setup (combo_box);
1676   else
1677     gtk_combo_box_menu_setup (combo_box, TRUE);
1678
1679   if (gtk_tree_row_reference_valid (priv->active_row))
1680     {
1681       path = gtk_tree_row_reference_get_path (priv->active_row);
1682       gtk_combo_box_set_active_internal (combo_box, path);
1683       gtk_tree_path_free (path);
1684     }
1685   else
1686     gtk_combo_box_set_active_internal (combo_box, NULL);
1687 }
1688
1689 static void
1690 gtk_combo_box_menu_show (GtkWidget *menu,
1691                          gpointer   user_data)
1692 {
1693   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1694   GtkComboBoxPrivate *priv = combo_box->priv;
1695
1696   gtk_combo_box_child_show (menu, user_data);
1697
1698   priv->popup_in_progress = TRUE;
1699   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1700                                 TRUE);
1701   priv->popup_in_progress = FALSE;
1702 }
1703
1704 static void
1705 gtk_combo_box_menu_hide (GtkWidget *menu,
1706                          gpointer   user_data)
1707 {
1708   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1709
1710   gtk_combo_box_child_hide (menu,user_data);
1711
1712   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1713                                 FALSE);
1714 }
1715
1716 static void
1717 gtk_combo_box_detacher (GtkWidget *widget,
1718                         GtkMenu          *menu)
1719 {
1720   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1721   GtkComboBoxPrivate *priv = combo_box->priv;
1722
1723   g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1724
1725   g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1726                                         gtk_combo_box_menu_show,
1727                                         combo_box);
1728   g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1729                                         gtk_combo_box_menu_hide,
1730                                         combo_box);
1731   
1732   priv->popup_widget = NULL;
1733 }
1734
1735 static void
1736 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1737                                 GtkWidget   *popup)
1738 {
1739   GtkComboBoxPrivate *priv = combo_box->priv;
1740
1741   if (GTK_IS_MENU (priv->popup_widget))
1742     {
1743       gtk_menu_detach (GTK_MENU (priv->popup_widget));
1744       priv->popup_widget = NULL;
1745     }
1746   else if (priv->popup_widget)
1747     {
1748       gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1749                             priv->popup_widget);
1750       g_object_unref (priv->popup_widget);
1751       priv->popup_widget = NULL;
1752     }
1753
1754   if (GTK_IS_MENU (popup))
1755     {
1756       if (priv->popup_window)
1757         {
1758           gtk_widget_destroy (priv->popup_window);
1759           priv->popup_window = NULL;
1760         }
1761
1762       priv->popup_widget = popup;
1763
1764       /*
1765        * Note that we connect to show/hide on the toplevel, not the
1766        * menu itself, since the menu is not shown/hidden when it is
1767        * popped up while torn-off.
1768        */
1769       g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1770                         G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1771       g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1772                         G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1773
1774       gtk_menu_attach_to_widget (GTK_MENU (popup),
1775                                  GTK_WIDGET (combo_box),
1776                                  gtk_combo_box_detacher);
1777     }
1778   else
1779     {
1780       if (!priv->popup_window)
1781         {
1782           GtkWidget *toplevel;
1783
1784           priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1785           gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1786
1787           gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1788                                     GDK_WINDOW_TYPE_HINT_COMBO);
1789
1790           g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1791                             G_CALLBACK (gtk_combo_box_child_show),
1792                             combo_box);
1793           g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1794                             G_CALLBACK (gtk_combo_box_child_hide),
1795                             combo_box);
1796
1797           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1798           if (GTK_IS_WINDOW (toplevel))
1799             {
1800               gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1801                                            GTK_WINDOW (priv->popup_window));
1802               gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1803                                             GTK_WINDOW (toplevel));
1804             }
1805
1806           gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1807           gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1808                                  gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1809
1810           priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1811
1812           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1813                                           GTK_POLICY_NEVER,
1814                                           GTK_POLICY_NEVER);
1815           gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1816                                                GTK_SHADOW_IN);
1817
1818           gtk_widget_show (priv->scrolled_window);
1819
1820           gtk_container_add (GTK_CONTAINER (priv->popup_window),
1821                              priv->scrolled_window);
1822         }
1823
1824       gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1825                          popup);
1826
1827       gtk_widget_show (popup);
1828       g_object_ref (popup);
1829       priv->popup_widget = popup;
1830     }
1831 }
1832
1833 static void
1834 get_widget_padding_and_border (GtkWidget *widget,
1835                                GtkBorder *padding)
1836 {
1837   GtkStyleContext *context;
1838   GtkStateFlags state;
1839   GtkBorder tmp;
1840
1841   context = gtk_widget_get_style_context (widget);
1842   state = gtk_style_context_get_state (context);
1843
1844   gtk_style_context_get_padding (context, state, padding);
1845   gtk_style_context_get_border (context, state, &tmp);
1846
1847   padding->top += tmp.top;
1848   padding->right += tmp.right;
1849   padding->bottom += tmp.bottom;
1850   padding->left += tmp.left;
1851 }
1852
1853 static void
1854 gtk_combo_box_menu_position_below (GtkMenu  *menu,
1855                                    gint     *x,
1856                                    gint     *y,
1857                                    gint     *push_in,
1858                                    gpointer  user_data)
1859 {
1860   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1861   GtkAllocation child_allocation;
1862   gint sx, sy;
1863   GtkWidget *child;
1864   GtkRequisition req;
1865   GdkScreen *screen;
1866   gint monitor_num;
1867   GdkRectangle monitor;
1868   GtkBorder padding;
1869
1870   /* FIXME: is using the size request here broken? */
1871   child = gtk_bin_get_child (GTK_BIN (combo_box));
1872
1873   sx = sy = 0;
1874
1875   gtk_widget_get_allocation (child, &child_allocation);
1876
1877   if (!gtk_widget_get_has_window (child))
1878     {
1879       sx += child_allocation.x;
1880       sy += child_allocation.y;
1881     }
1882
1883   gdk_window_get_root_coords (gtk_widget_get_window (child),
1884                               sx, sy, &sx, &sy);
1885   get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding);
1886   sx -= padding.left;
1887
1888   if (combo_box->priv->popup_fixed_width)
1889     gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1890   else
1891     gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1892
1893   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1894     *x = sx;
1895   else
1896     *x = sx + child_allocation.width - req.width;
1897   *y = sy;
1898
1899   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1900   monitor_num = gdk_screen_get_monitor_at_window (screen,
1901                                                   gtk_widget_get_window (GTK_WIDGET (combo_box)));
1902   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1903
1904   if (*x < monitor.x)
1905     *x = monitor.x;
1906   else if (*x + req.width > monitor.x + monitor.width)
1907     *x = monitor.x + monitor.width - req.width;
1908
1909   if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1910     *y += child_allocation.height;
1911   else if (*y - monitor.y >= req.height)
1912     *y -= req.height;
1913   else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1914     *y += child_allocation.height;
1915   else
1916     *y -= req.height;
1917
1918    *push_in = FALSE;
1919 }
1920
1921 static void
1922 gtk_combo_box_menu_position_over (GtkMenu  *menu,
1923                                   gint     *x,
1924                                   gint     *y,
1925                                   gboolean *push_in,
1926                                   gpointer  user_data)
1927 {
1928   GtkComboBox    *combo_box;
1929   GtkWidget      *active;
1930   GtkWidget      *child;
1931   GtkWidget      *widget;
1932   GtkAllocation   allocation;
1933   GtkAllocation   child_allocation;
1934   GList          *children;
1935   gint            screen_width;
1936   gint            menu_xpos;
1937   gint            menu_ypos;
1938   gint            menu_width;
1939
1940   combo_box = GTK_COMBO_BOX (user_data);
1941   widget = GTK_WIDGET (combo_box);
1942
1943   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1944
1945   gtk_widget_get_allocation (widget, &allocation);
1946
1947   menu_xpos = allocation.x;
1948   menu_ypos = allocation.y + allocation.height / 2 - 2;
1949
1950   if (combo_box->priv->popup_fixed_width)
1951     gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1952   else
1953     gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1954
1955   if (active != NULL)
1956     {
1957       gtk_widget_get_allocation (active, &child_allocation);
1958       menu_ypos -= child_allocation.height / 2;
1959     }
1960
1961   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1962   while (children)
1963     {
1964       child = children->data;
1965
1966       if (active == child)
1967         break;
1968
1969       if (gtk_widget_get_visible (child))
1970         {
1971           gtk_widget_get_allocation (child, &child_allocation);
1972
1973           menu_ypos -= child_allocation.height;
1974         }
1975
1976       children = children->next;
1977     }
1978
1979   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1980     menu_xpos = menu_xpos + allocation.width - menu_width;
1981
1982   gdk_window_get_root_coords (gtk_widget_get_window (widget),
1983                               menu_xpos, menu_ypos,
1984                               &menu_xpos, &menu_ypos);
1985
1986   /* Clamp the position on screen */
1987   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1988   
1989   if (menu_xpos < 0)
1990     menu_xpos = 0;
1991   else if ((menu_xpos + menu_width) > screen_width)
1992     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1993
1994   *x = menu_xpos;
1995   *y = menu_ypos;
1996
1997   *push_in = TRUE;
1998 }
1999
2000 static void
2001 gtk_combo_box_menu_position (GtkMenu  *menu,
2002                              gint     *x,
2003                              gint     *y,
2004                              gint     *push_in,
2005                              gpointer  user_data)
2006 {
2007   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2008   GtkComboBoxPrivate *priv = combo_box->priv;
2009   GtkWidget *menu_item;
2010
2011   if (priv->wrap_width > 0 || priv->cell_view == NULL)
2012     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2013   else
2014     {
2015       /* FIXME handle nested menus better */
2016       menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2017       if (menu_item)
2018         gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2019                                     menu_item);
2020
2021       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2022     }
2023
2024   if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2025     gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2026                               GDK_WINDOW_TYPE_HINT_COMBO);
2027 }
2028
2029 static void
2030 gtk_combo_box_list_position (GtkComboBox *combo_box,
2031                              gint        *x,
2032                              gint        *y,
2033                              gint        *width,
2034                              gint        *height)
2035 {
2036   GtkComboBoxPrivate *priv = combo_box->priv;
2037   GtkAllocation allocation;
2038   GdkScreen *screen;
2039   gint monitor_num;
2040   GdkRectangle monitor;
2041   GtkRequisition popup_req;
2042   GtkPolicyType hpolicy, vpolicy;
2043   GdkWindow *window;
2044
2045   /* under windows, the drop down list is as wide as the combo box itself.
2046      see bug #340204 */
2047   GtkWidget *widget = GTK_WIDGET (combo_box);
2048
2049   *x = *y = 0;
2050
2051   gtk_widget_get_allocation (widget, &allocation);
2052
2053   if (!gtk_widget_get_has_window (widget))
2054     {
2055       *x += allocation.x;
2056       *y += allocation.y;
2057     }
2058
2059   window = gtk_widget_get_window (widget);
2060
2061   gdk_window_get_root_coords (gtk_widget_get_window (widget),
2062                               *x, *y, x, y);
2063
2064   *width = allocation.width;
2065
2066   hpolicy = vpolicy = GTK_POLICY_NEVER;
2067   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2068                                   hpolicy, vpolicy);
2069
2070   if (combo_box->priv->popup_fixed_width)
2071     {
2072       gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2073
2074       if (popup_req.width > *width)
2075         {
2076           hpolicy = GTK_POLICY_ALWAYS;
2077           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2078                                           hpolicy, vpolicy);
2079         }
2080     }
2081   else
2082     {
2083       /* XXX This code depends on treeviews properly reporting their natural width
2084        * list-mode menus won't fill up to their natural width until then */
2085       gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2086
2087       if (popup_req.width > *width)
2088         {
2089           hpolicy = GTK_POLICY_NEVER;
2090           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2091                                           hpolicy, vpolicy);
2092
2093           *width = popup_req.width;
2094         }
2095     }
2096
2097   *height = popup_req.height;
2098
2099   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2100   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2101   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2102
2103   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2104     *x = *x + allocation.width - *width;
2105
2106   if (*x < monitor.x)
2107     *x = monitor.x;
2108   else if (*x + *width > monitor.x + monitor.width)
2109     *x = monitor.x + monitor.width - *width;
2110
2111   if (*y + allocation.height + *height <= monitor.y + monitor.height)
2112     *y += allocation.height;
2113   else if (*y - *height >= monitor.y)
2114     *y -= *height;
2115   else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2116     {
2117       *y += allocation.height;
2118       *height = monitor.y + monitor.height - *y;
2119     }
2120   else
2121     {
2122       *height = *y - monitor.y;
2123       *y = monitor.y;
2124     }
2125
2126   if (popup_req.height > *height)
2127     {
2128       vpolicy = GTK_POLICY_ALWAYS;
2129
2130       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2131                                       hpolicy, vpolicy);
2132     }
2133 }
2134
2135 static gboolean
2136 cell_layout_is_sensitive (GtkCellLayout *layout)
2137 {
2138   GList *cells, *list;
2139   gboolean sensitive;
2140
2141   cells = gtk_cell_layout_get_cells (layout);
2142
2143   sensitive = FALSE;
2144   for (list = cells; list; list = list->next)
2145     {
2146       g_object_get (list->data, "sensitive", &sensitive, NULL);
2147
2148       if (sensitive)
2149         break;
2150     }
2151   g_list_free (cells);
2152
2153   return sensitive;
2154 }
2155
2156 static gboolean
2157 cell_is_sensitive (GtkCellRenderer *cell,
2158                    gpointer         data)
2159 {
2160   gboolean *sensitive = data;
2161
2162   g_object_get (cell, "sensitive", sensitive, NULL);
2163
2164   return *sensitive;
2165 }
2166
2167 static gboolean
2168 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2169                               GtkTreeIter *iter)
2170 {
2171   GtkComboBoxPrivate *priv = combo_box->priv;
2172
2173   if (priv->row_separator_func)
2174     {
2175       if (priv->row_separator_func (priv->model, iter,
2176                                     priv->row_separator_data))
2177         return FALSE;
2178     }
2179
2180   if (priv->area)
2181     {
2182       gboolean sensitive;
2183
2184       gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2185
2186       sensitive = FALSE;
2187
2188       gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2189
2190       return sensitive;
2191     }
2192
2193   return TRUE;
2194 }
2195
2196 static void
2197 update_menu_sensitivity (GtkComboBox *combo_box,
2198                          GtkWidget   *menu)
2199 {
2200   GtkComboBoxPrivate *priv = combo_box->priv;
2201   GList *children, *child;
2202   GtkWidget *item, *submenu, *separator;
2203   GtkWidget *cell_view;
2204   gboolean sensitive;
2205
2206   if (!priv->model)
2207     return;
2208
2209   children = gtk_container_get_children (GTK_CONTAINER (menu));
2210
2211   for (child = children; child; child = child->next)
2212     {
2213       item = GTK_WIDGET (child->data);
2214       cell_view = gtk_bin_get_child (GTK_BIN (item));
2215
2216       if (!GTK_IS_CELL_VIEW (cell_view))
2217         continue;
2218
2219       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2220       if (submenu != NULL)
2221         {
2222           gtk_widget_set_sensitive (item, TRUE);
2223           update_menu_sensitivity (combo_box, submenu);
2224         }
2225       else
2226         {
2227           sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2228
2229           if (menu != priv->popup_widget && child == children)
2230             {
2231               separator = GTK_WIDGET (child->next->data);
2232               g_object_set (item, "visible", sensitive, NULL);
2233               g_object_set (separator, "visible", sensitive, NULL);
2234             }
2235           else
2236             gtk_widget_set_sensitive (item, sensitive);
2237         }
2238     }
2239
2240   g_list_free (children);
2241 }
2242
2243 static void
2244 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2245                           guint        button,
2246                           guint32      activate_time)
2247 {
2248   GtkComboBoxPrivate *priv = combo_box->priv;
2249   GtkTreePath *path;
2250   gint active_item;
2251   gint width, min_width, nat_width;
2252
2253   update_menu_sensitivity (combo_box, priv->popup_widget);
2254
2255   active_item = -1;
2256   if (gtk_tree_row_reference_valid (priv->active_row))
2257     {
2258       path = gtk_tree_row_reference_get_path (priv->active_row);
2259       active_item = gtk_tree_path_get_indices (path)[0];
2260       gtk_tree_path_free (path);
2261
2262       if (priv->add_tearoffs)
2263         active_item++;
2264     }
2265
2266   /* FIXME handle nested menus better */
2267   gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2268
2269   if (priv->wrap_width == 0)
2270     {
2271       GtkAllocation allocation;
2272
2273       gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2274       width = allocation.width;
2275       gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2276       gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2277
2278       if (combo_box->priv->popup_fixed_width)
2279         width = MAX (width, min_width);
2280       else
2281         width = MAX (width, nat_width);
2282
2283       gtk_widget_set_size_request (priv->popup_widget, width, -1);
2284     }
2285
2286   gtk_menu_popup (GTK_MENU (priv->popup_widget),
2287                   NULL, NULL,
2288                   gtk_combo_box_menu_position, combo_box,
2289                   button, activate_time);
2290 }
2291
2292 static gboolean
2293 popup_grab_on_window (GdkWindow *window,
2294                       GdkDevice *keyboard,
2295                       GdkDevice *pointer,
2296                       guint32    activate_time)
2297 {
2298   if (keyboard &&
2299       gdk_device_grab (keyboard, window,
2300                        GDK_OWNERSHIP_WINDOW, TRUE,
2301                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2302                        NULL, activate_time) != GDK_GRAB_SUCCESS)
2303     return FALSE;
2304
2305   if (pointer &&
2306       gdk_device_grab (pointer, window,
2307                        GDK_OWNERSHIP_WINDOW, TRUE,
2308                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2309                        GDK_POINTER_MOTION_MASK,
2310                        NULL, activate_time) != GDK_GRAB_SUCCESS)
2311     {
2312       if (keyboard)
2313         gdk_device_ungrab (keyboard, activate_time);
2314
2315       return FALSE;
2316     }
2317
2318   return TRUE;
2319 }
2320
2321 /**
2322  * gtk_combo_box_popup:
2323  * @combo_box: a #GtkComboBox
2324  *
2325  * Pops up the menu or dropdown list of @combo_box.
2326  *
2327  * This function is mostly intended for use by accessibility technologies;
2328  * applications should have little use for it.
2329  *
2330  * Since: 2.4
2331  */
2332 void
2333 gtk_combo_box_popup (GtkComboBox *combo_box)
2334 {
2335   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2336
2337   g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2338 }
2339
2340 /**
2341  * gtk_combo_box_popup_for_device:
2342  * @combo_box: a #GtkComboBox
2343  * @device: a #GdkDevice
2344  *
2345  * Pops up the menu or dropdown list of @combo_box, the popup window
2346  * will be grabbed so only @device and its associated pointer/keyboard
2347  * are the only #GdkDevice<!-- -->s able to send events to it.
2348  *
2349  * Since: 3.0
2350  **/
2351 void
2352 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2353                                 GdkDevice   *device)
2354 {
2355   GtkComboBoxPrivate *priv = combo_box->priv;
2356   gint x, y, width, height;
2357   GtkTreePath *path = NULL, *ppath;
2358   GtkWidget *toplevel;
2359   GdkDevice *keyboard, *pointer;
2360   guint32 time;
2361
2362   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2363   g_return_if_fail (GDK_IS_DEVICE (device));
2364
2365   if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2366     return;
2367
2368   if (gtk_widget_get_mapped (priv->popup_widget))
2369     return;
2370
2371   if (priv->grab_pointer && priv->grab_keyboard)
2372     return;
2373
2374   time = gtk_get_current_event_time ();
2375
2376   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2377     {
2378       keyboard = device;
2379       pointer = gdk_device_get_associated_device (device);
2380     }
2381   else
2382     {
2383       pointer = device;
2384       keyboard = gdk_device_get_associated_device (device);
2385     }
2386
2387   if (GTK_IS_MENU (priv->popup_widget))
2388     {
2389       gtk_combo_box_menu_popup (combo_box,
2390                                 priv->activate_button,
2391                                 priv->activate_time);
2392       return;
2393     }
2394
2395   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2396   if (GTK_IS_WINDOW (toplevel))
2397     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2398                                  GTK_WINDOW (priv->popup_window));
2399
2400   gtk_widget_show_all (priv->scrolled_window);
2401   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2402
2403   gtk_widget_set_size_request (priv->popup_window, width, height);
2404   gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2405
2406   if (gtk_tree_row_reference_valid (priv->active_row))
2407     {
2408       path = gtk_tree_row_reference_get_path (priv->active_row);
2409       ppath = gtk_tree_path_copy (path);
2410       if (gtk_tree_path_up (ppath))
2411         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2412                                       ppath);
2413       gtk_tree_path_free (ppath);
2414     }
2415   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2416                                   TRUE);
2417
2418   /* popup */
2419   gtk_widget_show (priv->popup_window);
2420
2421   if (path)
2422     {
2423       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2424                                 path, NULL, FALSE);
2425       gtk_tree_path_free (path);
2426     }
2427
2428   gtk_widget_grab_focus (priv->popup_window);
2429   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2430                                 TRUE);
2431
2432   if (!gtk_widget_has_focus (priv->tree_view))
2433     gtk_widget_grab_focus (priv->tree_view);
2434
2435   if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2436                              keyboard, pointer, time))
2437     {
2438       gtk_widget_hide (priv->popup_window);
2439       return;
2440     }
2441
2442   gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2443   priv->grab_pointer = pointer;
2444   priv->grab_keyboard = keyboard;
2445 }
2446
2447 static void
2448 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2449 {
2450   GdkDevice *device;
2451
2452   device = gtk_get_current_event_device ();
2453
2454   if (!device)
2455     {
2456       GdkDeviceManager *device_manager;
2457       GdkDisplay *display;
2458       GList *devices;
2459
2460       display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2461       device_manager = gdk_display_get_device_manager (display);
2462
2463       /* No device was set, pick the first master device */
2464       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2465       device = devices->data;
2466       g_list_free (devices);
2467     }
2468
2469   gtk_combo_box_popup_for_device (combo_box, device);
2470 }
2471
2472 static gboolean
2473 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2474 {
2475   if (combo_box->priv->popup_shown)
2476     {
2477       gtk_combo_box_popdown (combo_box);
2478       return TRUE;
2479     }
2480
2481   return FALSE;
2482 }
2483
2484 /**
2485  * gtk_combo_box_popdown:
2486  * @combo_box: a #GtkComboBox
2487  *
2488  * Hides the menu or dropdown list of @combo_box.
2489  *
2490  * This function is mostly intended for use by accessibility technologies;
2491  * applications should have little use for it.
2492  *
2493  * Since: 2.4
2494  */
2495 void
2496 gtk_combo_box_popdown (GtkComboBox *combo_box)
2497 {
2498   GtkComboBoxPrivate *priv = combo_box->priv;
2499
2500   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2501
2502   if (GTK_IS_MENU (priv->popup_widget))
2503     {
2504       gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2505       return;
2506     }
2507
2508   if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2509     return;
2510
2511   gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2512   gtk_widget_hide (priv->popup_window);
2513   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2514                                 FALSE);
2515
2516   priv->grab_pointer = NULL;
2517   priv->grab_keyboard = NULL;
2518 }
2519
2520 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON                      \
2521   GtkAllocation button_allocation;                              \
2522   gtk_widget_get_preferred_size (combo_box->priv->button,       \
2523                                  &req, NULL);                   \
2524                                                                 \
2525   if (is_rtl)                                                   \
2526     button_allocation.x = allocation->x;                        \
2527   else                                                          \
2528     button_allocation.x = allocation->x + allocation->width     \
2529      - req.width;                                               \
2530                                                                 \
2531   button_allocation.y = allocation->y;                          \
2532   button_allocation.width = MAX (1, req.width);                 \
2533   button_allocation.height = allocation->height;                \
2534   button_allocation.height = MAX (1, button_allocation.height); \
2535                                                                 \
2536   gtk_widget_size_allocate (combo_box->priv->button,            \
2537                             &button_allocation);
2538
2539
2540 static void
2541 gtk_combo_box_size_allocate (GtkWidget     *widget,
2542                              GtkAllocation *allocation)
2543 {
2544   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2545   GtkComboBoxPrivate *priv = combo_box->priv;
2546   GtkWidget *child_widget;
2547   GtkAllocation child;
2548   GtkRequisition req;
2549   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2550   GtkBorder padding;
2551
2552   gtk_widget_set_allocation (widget, allocation);
2553   child_widget = gtk_bin_get_child (GTK_BIN (widget));
2554   get_widget_padding_and_border (widget, &padding);
2555
2556   allocation->x += padding.left;
2557   allocation->y += padding.top;
2558   allocation->width -= padding.left + padding.right;
2559   allocation->height -= padding.top + padding.bottom;
2560
2561   if (!priv->tree_view)
2562     {
2563       if (priv->cell_view)
2564         {
2565           GtkBorder button_padding;
2566           gint width;
2567           guint border_width;
2568
2569           border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2570           get_widget_padding_and_border (priv->button, &button_padding);
2571
2572           /* menu mode; child_widget is priv->cell_view.
2573            * Allocate the button to the full combobox allocation (minus the
2574            * padding).
2575            */
2576           gtk_widget_size_allocate (priv->button, allocation);
2577
2578           child.x = allocation->x;
2579           child.y = allocation->y;
2580           width = allocation->width;
2581           child.height = allocation->height;
2582
2583           if (!priv->is_cell_renderer)
2584             {
2585               /* restrict allocation of the child into the button box
2586                * if we're not in cell renderer mode.
2587                */
2588               child.x += border_width + button_padding.left;
2589               child.y += border_width + button_padding.top;
2590               width -= 2 * border_width +
2591                 button_padding.left + button_padding.right;
2592               child.height -= 2 * border_width +
2593                 button_padding.top + button_padding.bottom;
2594             }
2595
2596           /* allocate the box containing the separator and the arrow */
2597           gtk_widget_get_preferred_size (priv->box, &req, NULL);
2598           child.width = req.width;
2599           if (!is_rtl)
2600             child.x += width - req.width;
2601           child.width = MAX (1, child.width);
2602           child.height = MAX (1, child.height);
2603           gtk_widget_size_allocate (priv->box, &child);
2604
2605           if (is_rtl)
2606             {
2607               child.x += req.width;
2608               child.width = allocation->x + allocation->width
2609                 - border_width - child.x;
2610             }
2611           else
2612             {
2613               child.width = child.x;
2614               child.x = allocation->x
2615                 + border_width + button_padding.left;
2616               child.width -= child.x;
2617             }
2618
2619           if (gtk_widget_get_visible (priv->popup_widget))
2620             {
2621               gint width, menu_width;
2622
2623               if (priv->wrap_width == 0)
2624                 {
2625                   GtkAllocation combo_box_allocation;
2626
2627                   gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2628                   width = combo_box_allocation.width;
2629                   gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2630
2631                   if (combo_box->priv->popup_fixed_width)
2632                     gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2633                   else
2634                     gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2635
2636                   gtk_widget_set_size_request (priv->popup_widget,
2637                                                MAX (width, menu_width), -1);
2638                }
2639
2640               /* reposition the menu after giving it a new width */
2641               gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2642             }
2643
2644           child.width = MAX (1, child.width);
2645           child.height = MAX (1, child.height);
2646           gtk_widget_size_allocate (child_widget, &child);
2647         }
2648       else
2649         {
2650           /* menu mode; child_widget has been set with gtk_container_add().
2651            * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2652            * Allocate the button at the far end, according to the direction
2653            * of the widget.
2654            */
2655           GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2656
2657             /* After the macro, button_allocation has the button allocation rect */
2658
2659           if (is_rtl)
2660             child.x = button_allocation.x + button_allocation.width;
2661           else
2662             child.x = allocation->x;
2663
2664           child.y = allocation->y;
2665           child.width = allocation->width - button_allocation.width;
2666           child.height = button_allocation.height;
2667
2668           child.width = MAX (1, child.width);
2669
2670           gtk_widget_size_allocate (child_widget, &child);
2671         }
2672     }
2673   else
2674     {
2675       /* list mode; child_widget might be either priv->cell_view or a child
2676        * added with gtk_container_add().
2677        */
2678
2679       /* After the macro, button_allocation has the button allocation rect */
2680       GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2681
2682       if (is_rtl)
2683         child.x = button_allocation.x + button_allocation.width;
2684       else
2685         child.x = allocation->x;
2686
2687       child.y = allocation->y;
2688       child.width = allocation->width - button_allocation.width;
2689       child.height = button_allocation.height;
2690
2691       if (priv->cell_view_frame)
2692         {
2693           gtk_widget_size_allocate (priv->cell_view_frame, &child);
2694
2695           /* restrict allocation of the child into the frame box if it's present */
2696           if (priv->has_frame)
2697             {
2698               GtkBorder frame_padding;
2699               guint border_width;
2700
2701               border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2702               get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2703
2704               child.x += border_width + frame_padding.left;
2705               child.y += border_width + frame_padding.right;
2706               child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2707               child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2708             }
2709         }
2710
2711       if (gtk_widget_get_visible (priv->popup_window))
2712         {
2713           gint x, y, width, height;
2714           gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2715           gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2716           gtk_widget_set_size_request (priv->popup_window, width, height);
2717         }
2718
2719       /* allocate the child */
2720       child.width = MAX (1, child.width);
2721       child.height = MAX (1, child.height);
2722       gtk_widget_size_allocate (child_widget, &child);
2723     }
2724 }
2725
2726 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2727
2728 static void
2729 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2730 {
2731   GtkComboBoxPrivate *priv = combo_box->priv;
2732
2733   if (priv->model)
2734     {
2735       g_signal_handler_disconnect (priv->model,
2736                                    priv->inserted_id);
2737       g_signal_handler_disconnect (priv->model,
2738                                    priv->deleted_id);
2739       g_signal_handler_disconnect (priv->model,
2740                                    priv->reordered_id);
2741       g_signal_handler_disconnect (priv->model,
2742                                    priv->changed_id);
2743     }
2744
2745   if (priv->model)
2746     {
2747       g_object_unref (priv->model);
2748       priv->model = NULL;
2749     }
2750
2751   if (priv->active_row)
2752     {
2753       gtk_tree_row_reference_free (priv->active_row);
2754       priv->active_row = NULL;
2755     }
2756
2757   if (priv->cell_view)
2758     gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2759 }
2760
2761 static void
2762 gtk_combo_box_forall (GtkContainer *container,
2763                       gboolean      include_internals,
2764                       GtkCallback   callback,
2765                       gpointer      callback_data)
2766 {
2767   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2768   GtkComboBoxPrivate *priv = combo_box->priv;
2769   GtkWidget *child;
2770
2771   if (include_internals)
2772     {
2773       if (priv->button)
2774         (* callback) (priv->button, callback_data);
2775       if (priv->cell_view_frame)
2776         (* callback) (priv->cell_view_frame, callback_data);
2777     }
2778
2779   child = gtk_bin_get_child (GTK_BIN (container));
2780   if (child)
2781     (* callback) (child, callback_data);
2782 }
2783
2784 static void
2785 gtk_combo_box_child_show (GtkWidget *widget,
2786                           GtkComboBox *combo_box)
2787 {
2788   GtkComboBoxPrivate *priv = combo_box->priv;
2789
2790   priv->popup_shown = TRUE;
2791   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2792 }
2793
2794 static void
2795 gtk_combo_box_child_hide (GtkWidget *widget,
2796                           GtkComboBox *combo_box)
2797 {
2798   GtkComboBoxPrivate *priv = combo_box->priv;
2799
2800   priv->popup_shown = FALSE;
2801   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2802 }
2803
2804 static gboolean
2805 gtk_combo_box_draw (GtkWidget *widget,
2806                     cairo_t   *cr)
2807 {
2808   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2809   GtkComboBoxPrivate *priv = combo_box->priv;
2810
2811   if (priv->shadow_type != GTK_SHADOW_NONE)
2812     {
2813       GtkStyleContext *context;
2814
2815       context = gtk_widget_get_style_context (widget);
2816
2817       gtk_render_background (context, cr, 0, 0,
2818                              gtk_widget_get_allocated_width (widget),
2819                              gtk_widget_get_allocated_height (widget));
2820       gtk_render_frame (context, cr, 0, 0,
2821                         gtk_widget_get_allocated_width (widget),
2822                         gtk_widget_get_allocated_height (widget));
2823     }
2824
2825   gtk_container_propagate_draw (GTK_CONTAINER (widget),
2826                                 priv->button, cr);
2827
2828   if (priv->tree_view && priv->cell_view_frame)
2829     {
2830       gtk_container_propagate_draw (GTK_CONTAINER (widget),
2831                                     priv->cell_view_frame, cr);
2832     }
2833
2834   gtk_container_propagate_draw (GTK_CONTAINER (widget),
2835                                 gtk_bin_get_child (GTK_BIN (widget)),
2836                                 cr);
2837
2838   return FALSE;
2839 }
2840
2841 typedef struct {
2842   GtkComboBox *combo;
2843   GtkTreePath *path;
2844   GtkTreeIter iter;
2845   gboolean found;
2846   gboolean set;
2847   gboolean visible;
2848 } SearchData;
2849
2850 static gboolean
2851 path_visible (GtkTreeView *view,
2852               GtkTreePath *path)
2853 {
2854   GtkRBTree *tree;
2855   GtkRBNode *node;
2856
2857   /* Note that we rely on the fact that collapsed rows don't have nodes
2858    */
2859   return _gtk_tree_view_find_node (view, path, &tree, &node);
2860 }
2861
2862 static gboolean
2863 tree_next_func (GtkTreeModel *model,
2864                 GtkTreePath  *path,
2865                 GtkTreeIter  *iter,
2866                 gpointer      data)
2867 {
2868   SearchData *search_data = (SearchData *)data;
2869
2870   if (search_data->found)
2871     {
2872       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2873         return FALSE;
2874
2875       if (search_data->visible &&
2876           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2877         return FALSE;
2878
2879       search_data->set = TRUE;
2880       search_data->iter = *iter;
2881
2882       return TRUE;
2883     }
2884
2885   if (gtk_tree_path_compare (path, search_data->path) == 0)
2886     search_data->found = TRUE;
2887
2888   return FALSE;
2889 }
2890
2891 static gboolean
2892 tree_next (GtkComboBox  *combo,
2893            GtkTreeModel *model,
2894            GtkTreeIter  *iter,
2895            GtkTreeIter  *next,
2896            gboolean      visible)
2897 {
2898   SearchData search_data;
2899
2900   search_data.combo = combo;
2901   search_data.path = gtk_tree_model_get_path (model, iter);
2902   search_data.visible = visible;
2903   search_data.found = FALSE;
2904   search_data.set = FALSE;
2905
2906   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2907
2908   *next = search_data.iter;
2909
2910   gtk_tree_path_free (search_data.path);
2911
2912   return search_data.set;
2913 }
2914
2915 static gboolean
2916 tree_prev_func (GtkTreeModel *model,
2917                 GtkTreePath  *path,
2918                 GtkTreeIter  *iter,
2919                 gpointer      data)
2920 {
2921   SearchData *search_data = (SearchData *)data;
2922
2923   if (gtk_tree_path_compare (path, search_data->path) == 0)
2924     {
2925       search_data->found = TRUE;
2926       return TRUE;
2927     }
2928
2929   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2930     return FALSE;
2931
2932   if (search_data->visible &&
2933       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2934     return FALSE;
2935
2936   search_data->set = TRUE;
2937   search_data->iter = *iter;
2938
2939   return FALSE;
2940 }
2941
2942 static gboolean
2943 tree_prev (GtkComboBox  *combo,
2944            GtkTreeModel *model,
2945            GtkTreeIter  *iter,
2946            GtkTreeIter  *prev,
2947            gboolean      visible)
2948 {
2949   SearchData search_data;
2950
2951   search_data.combo = combo;
2952   search_data.path = gtk_tree_model_get_path (model, iter);
2953   search_data.visible = visible;
2954   search_data.found = FALSE;
2955   search_data.set = FALSE;
2956
2957   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2958
2959   *prev = search_data.iter;
2960
2961   gtk_tree_path_free (search_data.path);
2962
2963   return search_data.set;
2964 }
2965
2966 static gboolean
2967 tree_last_func (GtkTreeModel *model,
2968                 GtkTreePath  *path,
2969                 GtkTreeIter  *iter,
2970                 gpointer      data)
2971 {
2972   SearchData *search_data = (SearchData *)data;
2973
2974   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2975     return FALSE;
2976
2977   /* Note that we rely on the fact that collapsed rows don't have nodes
2978    */
2979   if (search_data->visible &&
2980       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2981     return FALSE;
2982
2983   search_data->set = TRUE;
2984   search_data->iter = *iter;
2985
2986   return FALSE;
2987 }
2988
2989 static gboolean
2990 tree_last (GtkComboBox  *combo,
2991            GtkTreeModel *model,
2992            GtkTreeIter  *last,
2993            gboolean      visible)
2994 {
2995   SearchData search_data;
2996
2997   search_data.combo = combo;
2998   search_data.visible = visible;
2999   search_data.set = FALSE;
3000
3001   gtk_tree_model_foreach (model, tree_last_func, &search_data);
3002
3003   *last = search_data.iter;
3004
3005   return search_data.set;
3006 }
3007
3008
3009 static gboolean
3010 tree_first_func (GtkTreeModel *model,
3011                  GtkTreePath  *path,
3012                  GtkTreeIter  *iter,
3013                  gpointer      data)
3014 {
3015   SearchData *search_data = (SearchData *)data;
3016
3017   if (!tree_column_row_is_sensitive (search_data->combo, iter))
3018     return FALSE;
3019
3020   if (search_data->visible &&
3021       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3022     return FALSE;
3023
3024   search_data->set = TRUE;
3025   search_data->iter = *iter;
3026
3027   return TRUE;
3028 }
3029
3030 static gboolean
3031 tree_first (GtkComboBox  *combo,
3032             GtkTreeModel *model,
3033             GtkTreeIter  *first,
3034             gboolean      visible)
3035 {
3036   SearchData search_data;
3037
3038   search_data.combo = combo;
3039   search_data.visible = visible;
3040   search_data.set = FALSE;
3041
3042   gtk_tree_model_foreach (model, tree_first_func, &search_data);
3043
3044   *first = search_data.iter;
3045
3046   return search_data.set;
3047 }
3048
3049 static gboolean
3050 gtk_combo_box_scroll_event (GtkWidget          *widget,
3051                             GdkEventScroll     *event)
3052 {
3053   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3054   gboolean found;
3055   GtkTreeIter iter;
3056   GtkTreeIter new_iter;
3057
3058   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3059     return TRUE;
3060
3061   if (event->direction == GDK_SCROLL_UP)
3062     found = tree_prev (combo_box, combo_box->priv->model,
3063                        &iter, &new_iter, FALSE);
3064   else
3065     found = tree_next (combo_box, combo_box->priv->model,
3066                        &iter, &new_iter, FALSE);
3067
3068   if (found)
3069     gtk_combo_box_set_active_iter (combo_box, &new_iter);
3070
3071   return TRUE;
3072 }
3073
3074 /*
3075  * menu style
3076  */
3077 static gboolean
3078 gtk_combo_box_row_separator_func (GtkTreeModel      *model,
3079                                   GtkTreeIter       *iter,
3080                                   GtkComboBox       *combo)
3081 {
3082   GtkComboBoxPrivate *priv = combo->priv;
3083
3084   if (priv->row_separator_func)
3085     return priv->row_separator_func (model, iter, priv->row_separator_data);
3086
3087   return FALSE;
3088 }
3089
3090 static gboolean
3091 gtk_combo_box_header_func (GtkTreeModel      *model,
3092                            GtkTreeIter       *iter,
3093                            GtkComboBox       *combo)
3094 {
3095   /* Every submenu has a selectable header, however we
3096    * can expose a method to make that configurable by
3097    * the user (like row_separator_func is done) */
3098   return TRUE;
3099 }
3100
3101 static void
3102 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3103                           gboolean     add_children)
3104 {
3105   GtkComboBoxPrivate *priv = combo_box->priv;
3106   GtkWidget *child;
3107   GtkWidget *menu;
3108
3109   child = gtk_bin_get_child (GTK_BIN (combo_box));
3110
3111   if (priv->cell_view)
3112     {
3113       priv->button = gtk_toggle_button_new ();
3114       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3115                                      priv->focus_on_click);
3116
3117       g_signal_connect (priv->button, "toggled",
3118                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3119       gtk_widget_set_parent (priv->button,
3120                              gtk_widget_get_parent (child));
3121
3122       priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3123       gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3124
3125       priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3126       gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3127
3128       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3129       gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3130
3131       gtk_widget_show_all (priv->button);
3132     }
3133   else
3134     {
3135       priv->button = gtk_toggle_button_new ();
3136       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3137                                      priv->focus_on_click);
3138
3139       g_signal_connect (priv->button, "toggled",
3140                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3141       gtk_widget_set_parent (priv->button,
3142                              gtk_widget_get_parent (child));
3143
3144       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3145       gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3146       gtk_widget_show_all (priv->button);
3147     }
3148
3149   g_signal_connect (priv->button, "button-press-event",
3150                     G_CALLBACK (gtk_combo_box_menu_button_press),
3151                     combo_box);
3152   g_signal_connect (priv->button, "state-flags-changed",
3153                     G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3154                     combo_box);
3155
3156   /* create our funky menu */
3157   menu = _gtk_tree_menu_new_with_area (priv->area);
3158   gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3159
3160   _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3161
3162   _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3163   _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3164   _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3165   _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3166                               combo_box->priv->add_tearoffs);
3167
3168   g_signal_connect (menu, "menu-activate",
3169                     G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3170
3171   /* Chain our row_separator_func through */
3172   _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3173                                          (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3174                                          combo_box, NULL);
3175
3176   _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3177                                   (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3178                                   combo_box, NULL);
3179
3180   g_signal_connect (menu, "key-press-event",
3181                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3182   gtk_combo_box_set_popup_widget (combo_box, menu);
3183
3184   gtk_combo_box_update_title (combo_box);
3185 }
3186
3187 static void
3188 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3189 {
3190   GtkComboBoxPrivate *priv = combo_box->priv;
3191
3192   g_signal_handlers_disconnect_matched (priv->button,
3193                                         G_SIGNAL_MATCH_DATA,
3194                                         0, 0, NULL,
3195                                         gtk_combo_box_menu_button_press, NULL);
3196   g_signal_handlers_disconnect_matched (priv->button,
3197                                         G_SIGNAL_MATCH_DATA,
3198                                         0, 0, NULL,
3199                                         gtk_combo_box_button_state_flags_changed, combo_box);
3200   g_signal_handlers_disconnect_matched (priv->popup_widget,
3201                                         G_SIGNAL_MATCH_DATA,
3202                                         0, 0, NULL,
3203                                         gtk_combo_box_menu_activate, combo_box);
3204
3205   /* unparent will remove our latest ref */
3206   gtk_widget_unparent (priv->button);
3207
3208   priv->box = NULL;
3209   priv->button = NULL;
3210   priv->arrow = NULL;
3211   priv->separator = NULL;
3212
3213   /* changing the popup window will unref the menu and the children */
3214 }
3215
3216 /* callbacks */
3217 static gboolean
3218 gtk_combo_box_menu_button_press (GtkWidget      *widget,
3219                                  GdkEventButton *event,
3220                                  gpointer        user_data)
3221 {
3222   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3223   GtkComboBoxPrivate *priv = combo_box->priv;
3224
3225   if (GTK_IS_MENU (priv->popup_widget) &&
3226       event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3227     {
3228       if (priv->focus_on_click &&
3229           !gtk_widget_has_focus (priv->button))
3230         gtk_widget_grab_focus (priv->button);
3231
3232       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3233
3234       return TRUE;
3235     }
3236
3237   return FALSE;
3238 }
3239
3240 static void
3241 gtk_combo_box_menu_activate (GtkWidget   *menu,
3242                              const gchar *path,
3243                              GtkComboBox *combo_box)
3244 {
3245   GtkTreeIter iter;
3246
3247   if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3248     gtk_combo_box_set_active_iter (combo_box, &iter);
3249
3250   g_object_set (combo_box,
3251                 "editing-canceled", FALSE,
3252                 NULL);
3253 }
3254
3255
3256 static void
3257 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3258 {
3259   GtkTreeIter iter;
3260   gboolean sensitive = TRUE; /* fool code checkers */
3261
3262   if (!combo_box->priv->button)
3263     return;
3264
3265   switch (combo_box->priv->button_sensitivity)
3266     {
3267       case GTK_SENSITIVITY_ON:
3268         sensitive = TRUE;
3269         break;
3270       case GTK_SENSITIVITY_OFF:
3271         sensitive = FALSE;
3272         break;
3273       case GTK_SENSITIVITY_AUTO:
3274         sensitive = combo_box->priv->model &&
3275                     gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3276         break;
3277       default:
3278         g_assert_not_reached ();
3279         break;
3280     }
3281
3282   gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3283
3284   /* In list-mode, we also need to update sensitivity of the event box */
3285   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3286       && combo_box->priv->cell_view)
3287     gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3288 }
3289
3290 static void
3291 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
3292                                   GtkTreePath      *path,
3293                                   GtkTreeIter      *iter,
3294                                   gpointer          user_data)
3295 {
3296   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3297
3298   if (combo_box->priv->tree_view)
3299     gtk_combo_box_list_popup_resize (combo_box);
3300
3301   gtk_combo_box_update_sensitivity (combo_box);
3302 }
3303
3304 static void
3305 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
3306                                  GtkTreePath      *path,
3307                                  gpointer          user_data)
3308 {
3309   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3310   GtkComboBoxPrivate *priv = combo_box->priv;
3311
3312   if (!gtk_tree_row_reference_valid (priv->active_row))
3313     {
3314       if (priv->cell_view)
3315         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3316       g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3317     }
3318   
3319   if (priv->tree_view)
3320     gtk_combo_box_list_popup_resize (combo_box);
3321
3322   gtk_combo_box_update_sensitivity (combo_box);
3323 }
3324
3325 static void
3326 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
3327                                     GtkTreePath     *path,
3328                                     GtkTreeIter     *iter,
3329                                     gint            *new_order,
3330                                     gpointer         user_data)
3331 {
3332   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3333 }
3334
3335 static void
3336 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
3337                                  GtkTreePath      *path,
3338                                  GtkTreeIter      *iter,
3339                                  gpointer          user_data)
3340 {
3341   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3342   GtkComboBoxPrivate *priv = combo_box->priv;
3343   GtkTreePath *active_path;
3344
3345   /* FIXME this belongs to GtkCellView */
3346   if (gtk_tree_row_reference_valid (priv->active_row))
3347     {
3348       active_path = gtk_tree_row_reference_get_path (priv->active_row);
3349       if (gtk_tree_path_compare (path, active_path) == 0 &&
3350           priv->cell_view)
3351         gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3352       gtk_tree_path_free (active_path);
3353     }
3354
3355   if (priv->tree_view)
3356     gtk_combo_box_list_row_changed (model, path, iter, user_data);
3357 }
3358
3359 static gboolean
3360 list_popup_resize_idle (gpointer user_data)
3361 {
3362   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3363   GtkComboBoxPrivate *priv = combo_box->priv;
3364   gint x, y, width, height;
3365
3366   if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3367     {
3368       gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3369
3370       gtk_widget_set_size_request (priv->popup_window, width, height);
3371       gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3372     }
3373
3374   priv->resize_idle_id = 0;
3375
3376   return FALSE;
3377 }
3378
3379 static void
3380 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3381 {
3382   GtkComboBoxPrivate *priv = combo_box->priv;
3383
3384   if (!priv->resize_idle_id)
3385     priv->resize_idle_id =
3386       gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3387 }
3388
3389 static void
3390 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
3391                                   GtkTreePath      *path,
3392                                   GtkTreeIter      *iter,
3393                                   gpointer          user_data)
3394 {
3395   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3396
3397   gtk_combo_box_list_popup_resize (combo_box);
3398 }
3399
3400
3401 /*
3402  * list style
3403  */
3404
3405 static void
3406 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3407 {
3408   GtkComboBoxPrivate *priv = combo_box->priv;
3409   GtkTreeSelection *sel;
3410   GtkWidget *child;
3411   GtkWidget *widget = GTK_WIDGET (combo_box);
3412
3413   priv->button = gtk_toggle_button_new ();
3414   child = gtk_bin_get_child (GTK_BIN (combo_box));
3415   gtk_widget_set_parent (priv->button,
3416                          gtk_widget_get_parent (child));
3417   g_signal_connect (priv->button, "button-press-event",
3418                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3419   g_signal_connect (priv->button, "toggled",
3420                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3421
3422   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3423   gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3424   priv->separator = NULL;
3425   gtk_widget_show_all (priv->button);
3426
3427   if (priv->cell_view)
3428     {
3429       GtkStyleContext *context;
3430       GtkStateFlags state;
3431       GdkRGBA color;
3432
3433       context  = gtk_widget_get_style_context (widget);
3434       state = gtk_widget_get_state_flags (widget);
3435       gtk_style_context_get_background_color (context, state, &color);
3436
3437       gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3438
3439       priv->box = gtk_event_box_new ();
3440       gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3441                                         FALSE);
3442
3443       if (priv->has_frame)
3444         {
3445           priv->cell_view_frame = gtk_frame_new (NULL);
3446           gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3447                                      GTK_SHADOW_IN);
3448         }
3449       else
3450         {
3451           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3452           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3453                                             FALSE);
3454         }
3455
3456       gtk_widget_set_parent (priv->cell_view_frame,
3457                              gtk_widget_get_parent (child));
3458       gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3459       gtk_widget_show_all (priv->cell_view_frame);
3460
3461       g_signal_connect (priv->box, "button-press-event",
3462                         G_CALLBACK (gtk_combo_box_list_button_pressed),
3463                         combo_box);
3464     }
3465
3466   priv->tree_view = gtk_tree_view_new ();
3467   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3468   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3469   gtk_tree_selection_set_select_function (sel,
3470                                           gtk_combo_box_list_select_func,
3471                                           NULL, NULL);
3472   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3473                                      FALSE);
3474   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3475                                      TRUE);
3476
3477   gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3478                                         (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3479                                         combo_box, NULL);
3480
3481   if (priv->model)
3482     gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3483
3484   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3485                                gtk_tree_view_column_new_with_area (priv->area));
3486
3487   if (gtk_tree_row_reference_valid (priv->active_row))
3488     {
3489       GtkTreePath *path;
3490
3491       path = gtk_tree_row_reference_get_path (priv->active_row);
3492       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3493                                 path, NULL, FALSE);
3494       gtk_tree_path_free (path);
3495     }
3496
3497   /* set sample/popup widgets */
3498   gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3499
3500   g_signal_connect (priv->tree_view, "key-press-event",
3501                     G_CALLBACK (gtk_combo_box_list_key_press),
3502                     combo_box);
3503   g_signal_connect (priv->tree_view, "enter-notify-event",
3504                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3505                     combo_box);
3506   g_signal_connect (priv->tree_view, "row-expanded",
3507                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3508                     combo_box);
3509   g_signal_connect (priv->tree_view, "row-collapsed",
3510                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3511                     combo_box);
3512   g_signal_connect (priv->popup_window, "button-press-event",
3513                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3514                     combo_box);
3515   g_signal_connect (priv->popup_window, "button-release-event",
3516                     G_CALLBACK (gtk_combo_box_list_button_released),
3517                     combo_box);
3518
3519   gtk_widget_show (priv->tree_view);
3520
3521   gtk_combo_box_update_sensitivity (combo_box);
3522 }
3523
3524 static void
3525 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3526 {
3527   GtkComboBoxPrivate *priv = combo_box->priv;
3528
3529   /* disconnect signals */
3530   g_signal_handlers_disconnect_matched (priv->tree_view,
3531                                         G_SIGNAL_MATCH_DATA,
3532                                         0, 0, NULL, NULL, combo_box);
3533   g_signal_handlers_disconnect_matched (priv->button,
3534                                         G_SIGNAL_MATCH_DATA,
3535                                         0, 0, NULL,
3536                                         gtk_combo_box_list_button_pressed,
3537                                         NULL);
3538   g_signal_handlers_disconnect_matched (priv->popup_window,
3539                                         G_SIGNAL_MATCH_DATA,
3540                                         0, 0, NULL,
3541                                         gtk_combo_box_list_button_pressed,
3542                                         NULL);
3543   g_signal_handlers_disconnect_matched (priv->popup_window,
3544                                         G_SIGNAL_MATCH_DATA,
3545                                         0, 0, NULL,
3546                                         gtk_combo_box_list_button_released,
3547                                         NULL);
3548
3549   g_signal_handlers_disconnect_matched (priv->popup_window,
3550                                         G_SIGNAL_MATCH_DATA,
3551                                         0, 0, NULL,
3552                                         gtk_combo_box_child_show,
3553                                         NULL);
3554
3555   g_signal_handlers_disconnect_matched (priv->popup_window,
3556                                         G_SIGNAL_MATCH_DATA,
3557                                         0, 0, NULL,
3558                                         gtk_combo_box_child_hide,
3559                                         NULL);
3560
3561   if (priv->box)
3562     g_signal_handlers_disconnect_matched (priv->box,
3563                                           G_SIGNAL_MATCH_DATA,
3564                                           0, 0, NULL,
3565                                           gtk_combo_box_list_button_pressed,
3566                                           NULL);
3567
3568   /* destroy things (unparent will kill the latest ref from us)
3569    * last unref on button will destroy the arrow
3570    */
3571   gtk_widget_unparent (priv->button);
3572   priv->button = NULL;
3573   priv->arrow = NULL;
3574
3575   if (priv->cell_view)
3576     {
3577       g_object_set (priv->cell_view,
3578                     "background-set", FALSE,
3579                     NULL);
3580     }
3581
3582   if (priv->cell_view_frame)
3583     {
3584       gtk_widget_unparent (priv->cell_view_frame);
3585       priv->cell_view_frame = NULL;
3586       priv->box = NULL;
3587     }
3588
3589   if (priv->scroll_timer)
3590     {
3591       g_source_remove (priv->scroll_timer);
3592       priv->scroll_timer = 0;
3593     }
3594
3595   if (priv->resize_idle_id)
3596     {
3597       g_source_remove (priv->resize_idle_id);
3598       priv->resize_idle_id = 0;
3599     }
3600
3601   gtk_widget_destroy (priv->tree_view);
3602
3603   priv->tree_view = NULL;
3604   if (priv->popup_widget)
3605     {
3606       g_object_unref (priv->popup_widget);
3607       priv->popup_widget = NULL;
3608     }
3609 }
3610
3611 /* callbacks */
3612
3613 static gboolean
3614 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3615                                    GdkEventButton *event,
3616                                    gpointer        data)
3617 {
3618   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3619   GtkComboBoxPrivate *priv = combo_box->priv;
3620
3621   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3622
3623   if (ewidget == priv->popup_window)
3624     return TRUE;
3625
3626   if ((ewidget != priv->button && ewidget != priv->box) ||
3627       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3628     return FALSE;
3629
3630   if (priv->focus_on_click &&
3631       !gtk_widget_has_focus (priv->button))
3632     gtk_widget_grab_focus (priv->button);
3633
3634   gtk_combo_box_popup_for_device (combo_box, event->device);
3635
3636   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3637
3638   priv->auto_scroll = FALSE;
3639   if (priv->scroll_timer == 0)
3640     priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3641                                                   (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3642                                                    combo_box);
3643
3644   priv->popup_in_progress = TRUE;
3645
3646   return TRUE;
3647 }
3648
3649 static gboolean
3650 gtk_combo_box_list_button_released (GtkWidget      *widget,
3651                                     GdkEventButton *event,
3652                                     gpointer        data)
3653 {
3654   gboolean ret;
3655   GtkTreePath *path = NULL;
3656   GtkTreeIter iter;
3657
3658   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3659   GtkComboBoxPrivate *priv = combo_box->priv;
3660
3661   gboolean popup_in_progress = FALSE;
3662
3663   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3664
3665   if (priv->popup_in_progress)
3666     {
3667       popup_in_progress = TRUE;
3668       priv->popup_in_progress = FALSE;
3669     }
3670
3671   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3672                                   FALSE);
3673   if (priv->scroll_timer)
3674     {
3675       g_source_remove (priv->scroll_timer);
3676       priv->scroll_timer = 0;
3677     }
3678
3679   if (ewidget != priv->tree_view)
3680     {
3681       if ((ewidget == priv->button ||
3682            ewidget == priv->box) &&
3683           !popup_in_progress &&
3684           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3685         {
3686           gtk_combo_box_popdown (combo_box);
3687           return TRUE;
3688         }
3689
3690       /* released outside treeview */
3691       if (ewidget != priv->button && ewidget != priv->box)
3692         {
3693           gtk_combo_box_popdown (combo_box);
3694
3695           return TRUE;
3696         }
3697
3698       return FALSE;
3699     }
3700
3701   /* select something cool */
3702   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3703                                        event->x, event->y,
3704                                        &path,
3705                                        NULL, NULL, NULL);
3706
3707   if (!ret)
3708     return TRUE; /* clicked outside window? */
3709
3710   gtk_tree_model_get_iter (priv->model, &iter, path);
3711   gtk_tree_path_free (path);
3712
3713   gtk_combo_box_popdown (combo_box);
3714
3715   if (tree_column_row_is_sensitive (combo_box, &iter))
3716     gtk_combo_box_set_active_iter (combo_box, &iter);
3717
3718   return TRUE;
3719 }
3720
3721 static gboolean
3722 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3723                               GdkEventKey *event,
3724                               gpointer     data)
3725 {
3726   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3727
3728   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3729     {
3730       /* The menu hasn't managed the
3731        * event, forward it to the combobox
3732        */
3733       gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3734     }
3735
3736   return TRUE;
3737 }
3738
3739 static gboolean
3740 gtk_combo_box_list_key_press (GtkWidget   *widget,
3741                               GdkEventKey *event,
3742                               gpointer     data)
3743 {
3744   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3745   GtkTreeIter iter;
3746
3747   if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3748       event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3749   {
3750     GtkTreeModel *model = NULL;
3751
3752     gtk_combo_box_popdown (combo_box);
3753
3754     if (combo_box->priv->model)
3755       {
3756         GtkTreeSelection *sel;
3757
3758         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3759
3760         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3761           gtk_combo_box_set_active_iter (combo_box, &iter);
3762       }
3763
3764     return TRUE;
3765   }
3766
3767   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3768     {
3769       /* The list hasn't managed the
3770        * event, forward it to the combobox
3771        */
3772       gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3773     }
3774
3775   return TRUE;
3776 }
3777
3778 static void
3779 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3780                                 gint         x,
3781                                 gint         y)
3782 {
3783   GtkAdjustment *adj;
3784   GtkAllocation allocation;
3785   GtkWidget *tree_view = combo_box->priv->tree_view;
3786   gdouble value;
3787
3788   gtk_widget_get_allocation (tree_view, &allocation);
3789
3790   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3791   if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3792     {
3793       if (x <= allocation.x &&
3794           gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3795         {
3796           value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3797           gtk_adjustment_set_value (adj, value);
3798         }
3799       else if (x >= allocation.x + allocation.width &&
3800                gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3801         {
3802           value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3803           gtk_adjustment_set_value (adj, MAX (value, 0.0));
3804         }
3805     }
3806
3807   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3808   if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3809     {
3810       if (y <= allocation.y &&
3811           gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3812         {
3813           value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3814           gtk_adjustment_set_value (adj, value);
3815         }
3816       else if (y >= allocation.height &&
3817                gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3818         {
3819           value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3820           gtk_adjustment_set_value (adj, MAX (value, 0.0));
3821         }
3822     }
3823 }
3824
3825 static gboolean
3826 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3827 {
3828   GtkComboBoxPrivate *priv = combo_box->priv;
3829   gint x, y;
3830
3831   if (priv->auto_scroll)
3832     {
3833       gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3834                                       priv->grab_pointer,
3835                                       &x, &y, NULL);
3836       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3837     }
3838
3839   return TRUE;
3840 }
3841
3842 static gboolean
3843 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3844                                  GdkEventCrossing *event,
3845                                  gpointer          data)
3846 {
3847   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3848
3849   combo_box->priv->auto_scroll = TRUE;
3850
3851   return TRUE;
3852 }
3853
3854 static gboolean
3855 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3856                                 GtkTreeModel     *model,
3857                                 GtkTreePath      *path,
3858                                 gboolean          path_currently_selected,
3859                                 gpointer          data)
3860 {
3861   GList *list, *columns;
3862   gboolean sensitive = FALSE;
3863
3864   columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3865
3866   for (list = columns; list && !sensitive; list = list->next)
3867     {
3868       GList *cells, *cell;
3869       gboolean cell_sensitive, cell_visible;
3870       GtkTreeIter iter;
3871       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3872
3873       if (!gtk_tree_view_column_get_visible (column))
3874         continue;
3875
3876       gtk_tree_model_get_iter (model, &iter, path);
3877       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3878                                                FALSE, FALSE);
3879
3880       cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3881       while (cell)
3882         {
3883           g_object_get (cell->data,
3884                         "sensitive", &cell_sensitive,
3885                         "visible", &cell_visible,
3886                         NULL);
3887
3888           if (cell_visible && cell_sensitive)
3889             {
3890               sensitive = TRUE;
3891               break;
3892             }
3893
3894           cell = cell->next;
3895         }
3896
3897       g_list_free (cells);
3898     }
3899
3900   g_list_free (columns);
3901
3902   return sensitive;
3903 }
3904
3905 static void
3906 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3907                                 GtkTreePath  *path,
3908                                 GtkTreeIter  *iter,
3909                                 gpointer      data)
3910 {
3911   /* XXX Do nothing ? */
3912 }
3913
3914 /*
3915  * GtkCellLayout implementation
3916  */
3917 static GtkCellArea *
3918 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3919 {
3920   GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3921   GtkComboBoxPrivate *priv = combo->priv;
3922
3923   if (G_UNLIKELY (!priv->area))
3924     {
3925       priv->area = gtk_cell_area_box_new ();
3926       g_object_ref_sink (priv->area);
3927     }
3928
3929   return priv->area;
3930 }
3931
3932 /*
3933  * public API
3934  */
3935
3936 /**
3937  * gtk_combo_box_new:
3938  *
3939  * Creates a new empty #GtkComboBox.
3940  *
3941  * Return value: A new #GtkComboBox.
3942  *
3943  * Since: 2.4
3944  */
3945 GtkWidget *
3946 gtk_combo_box_new (void)
3947 {
3948   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3949 }
3950
3951 /**
3952  * gtk_combo_box_new_with_area:
3953  * @area: the #GtkCellArea to use to layout cell renderers
3954  *
3955  * Creates a new empty #GtkComboBox using @area to layout cells.
3956  *
3957  * Return value: A new #GtkComboBox.
3958  */
3959 GtkWidget *
3960 gtk_combo_box_new_with_area (GtkCellArea  *area)
3961 {
3962   return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3963 }
3964
3965 /**
3966  * gtk_combo_box_new_with_area_and_entry:
3967  * @area: the #GtkCellArea to use to layout cell renderers
3968  *
3969  * Creates a new empty #GtkComboBox with an entry.
3970  *
3971  * The new combo box will use @area to layout cells.
3972  *
3973  * Return value: A new #GtkComboBox.
3974  */
3975 GtkWidget *
3976 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3977 {
3978   return g_object_new (GTK_TYPE_COMBO_BOX,
3979                        "has-entry", TRUE,
3980                        "cell-area", area,
3981                        NULL);
3982 }
3983
3984
3985 /**
3986  * gtk_combo_box_new_with_entry:
3987  *
3988  * Creates a new empty #GtkComboBox with an entry.
3989  *
3990  * Return value: A new #GtkComboBox.
3991  */
3992 GtkWidget *
3993 gtk_combo_box_new_with_entry (void)
3994 {
3995   return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3996 }
3997
3998 /**
3999  * gtk_combo_box_new_with_model:
4000  * @model: A #GtkTreeModel.
4001  *
4002  * Creates a new #GtkComboBox with the model initialized to @model.
4003  *
4004  * Return value: A new #GtkComboBox.
4005  *
4006  * Since: 2.4
4007  */
4008 GtkWidget *
4009 gtk_combo_box_new_with_model (GtkTreeModel *model)
4010 {
4011   GtkComboBox *combo_box;
4012
4013   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4014
4015   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4016
4017   return GTK_WIDGET (combo_box);
4018 }
4019
4020 /**
4021  * gtk_combo_box_new_with_model_and_entry:
4022  * @model: A #GtkTreeModel
4023  *
4024  * Creates a new empty #GtkComboBox with an entry
4025  * and with the model initialized to @model.
4026  *
4027  * Return value: A new #GtkComboBox
4028  */
4029 GtkWidget *
4030 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4031 {
4032   return g_object_new (GTK_TYPE_COMBO_BOX,
4033                        "has-entry", TRUE,
4034                        "model", model,
4035                        NULL);
4036 }
4037
4038 /**
4039  * gtk_combo_box_get_wrap_width:
4040  * @combo_box: A #GtkComboBox
4041  *
4042  * Returns the wrap width which is used to determine the number of columns
4043  * for the popup menu. If the wrap width is larger than 1, the combo box
4044  * is in table mode.
4045  *
4046  * Returns: the wrap width.
4047  *
4048  * Since: 2.6
4049  */
4050 gint
4051 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4052 {
4053   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4054
4055   return combo_box->priv->wrap_width;
4056 }
4057
4058 /**
4059  * gtk_combo_box_set_wrap_width:
4060  * @combo_box: A #GtkComboBox
4061  * @width: Preferred number of columns
4062  *
4063  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4064  * the preferred number of columns when you want the popup to be layed out
4065  * in a table.
4066  *
4067  * Since: 2.4
4068  */
4069 void
4070 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4071                               gint         width)
4072 {
4073   GtkComboBoxPrivate *priv;
4074
4075   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4076   g_return_if_fail (width >= 0);
4077
4078   priv = combo_box->priv;
4079
4080   if (width != priv->wrap_width)
4081     {
4082       priv->wrap_width = width;
4083
4084       gtk_combo_box_check_appearance (combo_box);
4085
4086       if (GTK_IS_TREE_MENU (priv->popup_widget))
4087         _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4088
4089       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4090     }
4091 }
4092
4093 /**
4094  * gtk_combo_box_get_row_span_column:
4095  * @combo_box: A #GtkComboBox
4096  *
4097  * Returns the column with row span information for @combo_box.
4098  *
4099  * Returns: the row span column.
4100  *
4101  * Since: 2.6
4102  */
4103 gint
4104 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4105 {
4106   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4107
4108   return combo_box->priv->row_column;
4109 }
4110
4111 /**
4112  * gtk_combo_box_set_row_span_column:
4113  * @combo_box: A #GtkComboBox.
4114  * @row_span: A column in the model passed during construction.
4115  *
4116  * Sets the column with row span information for @combo_box to be @row_span.
4117  * The row span column contains integers which indicate how many rows
4118  * an item should span.
4119  *
4120  * Since: 2.4
4121  */
4122 void
4123 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4124                                    gint         row_span)
4125 {
4126   GtkComboBoxPrivate *priv;
4127   gint col;
4128
4129   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4130
4131   priv = combo_box->priv;
4132
4133   col = gtk_tree_model_get_n_columns (priv->model);
4134   g_return_if_fail (row_span >= -1 && row_span < col);
4135
4136   if (row_span != priv->row_column)
4137     {
4138       priv->row_column = row_span;
4139
4140       if (GTK_IS_TREE_MENU (priv->popup_widget))
4141         _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4142
4143       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4144     }
4145 }
4146
4147 /**
4148  * gtk_combo_box_get_column_span_column:
4149  * @combo_box: A #GtkComboBox
4150  *
4151  * Returns the column with column span information for @combo_box.
4152  *
4153  * Returns: the column span column.
4154  *
4155  * Since: 2.6
4156  */
4157 gint
4158 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4159 {
4160   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4161
4162   return combo_box->priv->col_column;
4163 }
4164
4165 /**
4166  * gtk_combo_box_set_column_span_column:
4167  * @combo_box: A #GtkComboBox
4168  * @column_span: A column in the model passed during construction
4169  *
4170  * Sets the column with column span information for @combo_box to be
4171  * @column_span. The column span column contains integers which indicate
4172  * how many columns an item should span.
4173  *
4174  * Since: 2.4
4175  */
4176 void
4177 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4178                                       gint         column_span)
4179 {
4180   GtkComboBoxPrivate *priv;
4181   gint col;
4182
4183   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4184
4185   priv = combo_box->priv;
4186
4187   col = gtk_tree_model_get_n_columns (priv->model);
4188   g_return_if_fail (column_span >= -1 && column_span < col);
4189
4190   if (column_span != priv->col_column)
4191     {
4192       priv->col_column = column_span;
4193
4194       if (GTK_IS_TREE_MENU (priv->popup_widget))
4195         _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4196
4197       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4198     }
4199 }
4200
4201 /**
4202  * gtk_combo_box_get_active:
4203  * @combo_box: A #GtkComboBox
4204  *
4205  * Returns the index of the currently active item, or -1 if there's no
4206  * active item. If the model is a non-flat treemodel, and the active item
4207  * is not an immediate child of the root of the tree, this function returns
4208  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4209  * <literal>path</literal> is the #GtkTreePath of the active item.
4210  *
4211  * Return value: An integer which is the index of the currently active item,
4212  *     or -1 if there's no active item.
4213  *
4214  * Since: 2.4
4215  */
4216 gint
4217 gtk_combo_box_get_active (GtkComboBox *combo_box)
4218 {
4219   GtkComboBoxPrivate *priv;
4220   gint result;
4221
4222   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4223
4224   priv = combo_box->priv;
4225
4226   if (gtk_tree_row_reference_valid (priv->active_row))
4227     {
4228       GtkTreePath *path;
4229
4230       path = gtk_tree_row_reference_get_path (priv->active_row);
4231       result = gtk_tree_path_get_indices (path)[0];
4232       gtk_tree_path_free (path);
4233     }
4234   else
4235     result = -1;
4236
4237   return result;
4238 }
4239
4240 /**
4241  * gtk_combo_box_set_active:
4242  * @combo_box: A #GtkComboBox
4243  * @index_: An index in the model passed during construction, or -1 to have
4244  * no active item
4245  *
4246  * Sets the active item of @combo_box to be the item at @index.
4247  *
4248  * Since: 2.4
4249  */
4250 void
4251 gtk_combo_box_set_active (GtkComboBox *combo_box,
4252                           gint         index_)
4253 {
4254   GtkTreePath *path = NULL;
4255   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4256   g_return_if_fail (index_ >= -1);
4257
4258   if (combo_box->priv->model == NULL)
4259     {
4260       /* Save index, in case the model is set after the index */
4261       combo_box->priv->active = index_;
4262       if (index_ != -1)
4263         return;
4264     }
4265
4266   if (index_ != -1)
4267     path = gtk_tree_path_new_from_indices (index_, -1);
4268
4269   gtk_combo_box_set_active_internal (combo_box, path);
4270
4271   if (path)
4272     gtk_tree_path_free (path);
4273 }
4274
4275 static void
4276 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4277                                    GtkTreePath *path)
4278 {
4279   GtkComboBoxPrivate *priv = combo_box->priv;
4280   GtkTreePath *active_path;
4281   gint path_cmp;
4282
4283   /* Remember whether the initially active row is valid. */
4284   gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4285
4286   if (path && is_valid_row_reference)
4287     {
4288       active_path = gtk_tree_row_reference_get_path (priv->active_row);
4289       path_cmp = gtk_tree_path_compare (path, active_path);
4290       gtk_tree_path_free (active_path);
4291       if (path_cmp == 0)
4292         return;
4293     }
4294
4295   if (priv->active_row)
4296     {
4297       gtk_tree_row_reference_free (priv->active_row);
4298       priv->active_row = NULL;
4299     }
4300
4301   if (!path)
4302     {
4303       if (priv->tree_view)
4304         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4305       else
4306         {
4307           GtkMenu *menu = GTK_MENU (priv->popup_widget);
4308
4309           if (GTK_IS_MENU (menu))
4310             gtk_menu_set_active (menu, -1);
4311         }
4312
4313       if (priv->cell_view)
4314         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4315
4316       /*
4317        *  Do not emit a "changed" signal when an already invalid selection was
4318        *  now set to invalid.
4319        */
4320       if (!is_valid_row_reference)
4321         return;
4322     }
4323   else
4324     {
4325       priv->active_row =
4326         gtk_tree_row_reference_new (priv->model, path);
4327
4328       if (priv->tree_view)
4329         {
4330           gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4331                                     path, NULL, FALSE);
4332         }
4333       else if (GTK_IS_MENU (priv->popup_widget))
4334         {
4335           /* FIXME handle nested menus better */
4336           gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4337                                gtk_tree_path_get_indices (path)[0]);
4338         }
4339
4340       if (priv->cell_view)
4341         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4342                                          path);
4343     }
4344
4345   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4346   g_object_notify (G_OBJECT (combo_box), "active");
4347   if (combo_box->priv->id_column >= 0)
4348     g_object_notify (G_OBJECT (combo_box), "active-id");
4349 }
4350
4351
4352 /**
4353  * gtk_combo_box_get_active_iter:
4354  * @combo_box: A #GtkComboBox
4355  * @iter: (out): The uninitialized #GtkTreeIter
4356  *
4357  * Sets @iter to point to the current active item, if it exists.
4358  *
4359  * Return value: %TRUE, if @iter was set
4360  *
4361  * Since: 2.4
4362  */
4363 gboolean
4364 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4365                                GtkTreeIter     *iter)
4366 {
4367   GtkTreePath *path;
4368   gboolean result;
4369
4370   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4371
4372   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4373     return FALSE;
4374
4375   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4376   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4377   gtk_tree_path_free (path);
4378
4379   return result;
4380 }
4381
4382 /**
4383  * gtk_combo_box_set_active_iter:
4384  * @combo_box: A #GtkComboBox
4385  * @iter: (allow-none): The #GtkTreeIter, or %NULL
4386  *
4387  * Sets the current active item to be the one referenced by @iter, or
4388  * unsets the active item if @iter is %NULL.
4389  *
4390  * Since: 2.4
4391  */
4392 void
4393 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4394                                GtkTreeIter     *iter)
4395 {
4396   GtkTreePath *path = NULL;
4397
4398   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4399
4400   if (iter)
4401     path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4402
4403   gtk_combo_box_set_active_internal (combo_box, path);
4404   gtk_tree_path_free (path);
4405 }
4406
4407 /**
4408  * gtk_combo_box_set_model:
4409  * @combo_box: A #GtkComboBox
4410  * @model: (allow-none): A #GtkTreeModel
4411  *
4412  * Sets the model used by @combo_box to be @model. Will unset a previously set
4413  * model (if applicable). If model is %NULL, then it will unset the model.
4414  *
4415  * Note that this function does not clear the cell renderers, you have to
4416  * call gtk_cell_layout_clear() yourself if you need to set up different
4417  * cell renderers for the new model.
4418  *
4419  * Since: 2.4
4420  */
4421 void
4422 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4423                          GtkTreeModel *model)
4424 {
4425   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4426   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4427
4428   if (model == combo_box->priv->model)
4429     return;
4430
4431   gtk_combo_box_unset_model (combo_box);
4432
4433   if (model == NULL)
4434     goto out;
4435
4436   combo_box->priv->model = model;
4437   g_object_ref (combo_box->priv->model);
4438
4439   combo_box->priv->inserted_id =
4440     g_signal_connect (combo_box->priv->model, "row-inserted",
4441                       G_CALLBACK (gtk_combo_box_model_row_inserted),
4442                       combo_box);
4443   combo_box->priv->deleted_id =
4444     g_signal_connect (combo_box->priv->model, "row-deleted",
4445                       G_CALLBACK (gtk_combo_box_model_row_deleted),
4446                       combo_box);
4447   combo_box->priv->reordered_id =
4448     g_signal_connect (combo_box->priv->model, "rows-reordered",
4449                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
4450                       combo_box);
4451   combo_box->priv->changed_id =
4452     g_signal_connect (combo_box->priv->model, "row-changed",
4453                       G_CALLBACK (gtk_combo_box_model_row_changed),
4454                       combo_box);
4455
4456   if (combo_box->priv->tree_view)
4457     {
4458       /* list mode */
4459       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4460                                combo_box->priv->model);
4461       gtk_combo_box_list_popup_resize (combo_box);
4462     }
4463
4464   if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4465     {
4466       /* menu mode */
4467       _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4468                                 combo_box->priv->model);
4469     }
4470
4471   if (combo_box->priv->cell_view)
4472     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4473                              combo_box->priv->model);
4474
4475   if (combo_box->priv->active != -1)
4476     {
4477       /* If an index was set in advance, apply it now */
4478       gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4479       combo_box->priv->active = -1;
4480     }
4481
4482 out:
4483   gtk_combo_box_update_sensitivity (combo_box);
4484
4485   g_object_notify (G_OBJECT (combo_box), "model");
4486 }
4487
4488 /**
4489  * gtk_combo_box_get_model:
4490  * @combo_box: A #GtkComboBox
4491  *
4492  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4493  *
4494  * Return value: (transfer none): A #GtkTreeModel which was passed
4495  *     during construction.
4496  *
4497  * Since: 2.4
4498  */
4499 GtkTreeModel *
4500 gtk_combo_box_get_model (GtkComboBox *combo_box)
4501 {
4502   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4503
4504   return combo_box->priv->model;
4505 }
4506
4507 static void
4508 gtk_combo_box_real_move_active (GtkComboBox   *combo_box,
4509                                 GtkScrollType  scroll)
4510 {
4511   GtkTreeIter iter;
4512   GtkTreeIter new_iter;
4513   gboolean    active_iter;
4514   gboolean    found;
4515
4516   if (!combo_box->priv->model)
4517     {
4518       gtk_widget_error_bell (GTK_WIDGET (combo_box));
4519       return;
4520     }
4521
4522   active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4523
4524   switch (scroll)
4525     {
4526     case GTK_SCROLL_STEP_BACKWARD:
4527     case GTK_SCROLL_STEP_UP:
4528     case GTK_SCROLL_STEP_LEFT:
4529       if (active_iter)
4530         {
4531           found = tree_prev (combo_box, combo_box->priv->model,
4532                              &iter, &new_iter, FALSE);
4533           break;
4534         }
4535       /* else fall through */
4536
4537     case GTK_SCROLL_PAGE_FORWARD:
4538     case GTK_SCROLL_PAGE_DOWN:
4539     case GTK_SCROLL_PAGE_RIGHT:
4540     case GTK_SCROLL_END:
4541       found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4542       break;
4543
4544     case GTK_SCROLL_STEP_FORWARD:
4545     case GTK_SCROLL_STEP_DOWN:
4546     case GTK_SCROLL_STEP_RIGHT:
4547       if (active_iter)
4548         {
4549           found = tree_next (combo_box, combo_box->priv->model,
4550                              &iter, &new_iter, FALSE);
4551           break;
4552         }
4553       /* else fall through */
4554
4555     case GTK_SCROLL_PAGE_BACKWARD:
4556     case GTK_SCROLL_PAGE_UP:
4557     case GTK_SCROLL_PAGE_LEFT:
4558     case GTK_SCROLL_START:
4559       found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4560       break;
4561
4562     default:
4563       return;
4564     }
4565
4566   if (found && active_iter)
4567     {
4568       GtkTreePath *old_path;
4569       GtkTreePath *new_path;
4570
4571       old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4572       new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4573
4574       if (gtk_tree_path_compare (old_path, new_path) == 0)
4575         found = FALSE;
4576
4577       gtk_tree_path_free (old_path);
4578       gtk_tree_path_free (new_path);
4579     }
4580
4581   if (found)
4582     {
4583       gtk_combo_box_set_active_iter (combo_box, &new_iter);
4584     }
4585   else
4586     {
4587       gtk_widget_error_bell (GTK_WIDGET (combo_box));
4588     }
4589 }
4590
4591 static gboolean
4592 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4593                                  gboolean   group_cycling)
4594 {
4595   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4596
4597   if (combo_box->priv->has_entry)
4598     {
4599       GtkWidget* child;
4600
4601       child = gtk_bin_get_child (GTK_BIN (combo_box));
4602       if (child)
4603         gtk_widget_grab_focus (child);
4604     }
4605   else
4606     gtk_widget_grab_focus (combo_box->priv->button);
4607
4608   return TRUE;
4609 }
4610
4611 static void
4612 gtk_combo_box_grab_focus (GtkWidget *widget)
4613 {
4614   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4615
4616   if (combo_box->priv->has_entry)
4617     {
4618       GtkWidget *child;
4619
4620       child = gtk_bin_get_child (GTK_BIN (combo_box));
4621       if (child)
4622         gtk_widget_grab_focus (child);
4623     }
4624   else
4625     gtk_widget_grab_focus (combo_box->priv->button);
4626 }
4627
4628 static void
4629 gtk_combo_box_destroy (GtkWidget *widget)
4630 {
4631   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4632
4633   if (combo_box->priv->popup_idle_id > 0)
4634     {
4635       g_source_remove (combo_box->priv->popup_idle_id);
4636       combo_box->priv->popup_idle_id = 0;
4637     }
4638
4639   gtk_combo_box_popdown (combo_box);
4640
4641   if (combo_box->priv->row_separator_destroy)
4642     combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4643
4644   combo_box->priv->row_separator_func = NULL;
4645   combo_box->priv->row_separator_data = NULL;
4646   combo_box->priv->row_separator_destroy = NULL;
4647
4648   GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4649   combo_box->priv->cell_view = NULL;
4650 }
4651
4652 static void
4653 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4654                                       gpointer  user_data)
4655 {
4656   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4657
4658   /*
4659    *  Fixes regression reported in bug #574059. The old functionality relied on
4660    *  bug #572478.  As a bugfix, we now emit the "changed" signal ourselves
4661    *  when the selection was already set to -1.
4662    */
4663   if (gtk_combo_box_get_active(combo_box) == -1)
4664     g_signal_emit_by_name (combo_box, "changed");
4665   else
4666     gtk_combo_box_set_active (combo_box, -1);
4667 }
4668
4669 static void
4670 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4671                                     gpointer     user_data)
4672 {
4673   GtkTreeModel *model;
4674   GtkTreeIter iter;
4675
4676   if (gtk_combo_box_get_active_iter (combo_box, &iter))
4677     {
4678       GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4679
4680       if (entry)
4681         {
4682           GtkTreePath *path;
4683           gchar       *path_str;
4684           gchar       *text = NULL;
4685
4686           model    = gtk_combo_box_get_model (combo_box);
4687           path     = gtk_tree_model_get_path (model, &iter);
4688           path_str = gtk_tree_path_to_string (path);
4689
4690           g_signal_handlers_block_by_func (entry,
4691                                            gtk_combo_box_entry_contents_changed,
4692                                            combo_box);
4693
4694
4695           g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0, 
4696                          path_str, &text);
4697
4698           gtk_entry_set_text (entry, text);
4699
4700           g_signal_handlers_unblock_by_func (entry,
4701                                              gtk_combo_box_entry_contents_changed,
4702                                              combo_box);
4703
4704           gtk_tree_path_free (path);
4705           g_free (text);
4706           g_free (path_str);
4707         }
4708     }
4709 }
4710
4711 static gchar *
4712 gtk_combo_box_format_entry_text (GtkComboBox     *combo_box,
4713                                  const gchar     *path)
4714 {
4715   GtkComboBoxPrivate *priv = combo_box->priv;
4716   GtkTreeModel       *model;
4717   GtkTreeIter         iter;
4718   gchar              *text = NULL;
4719
4720   if (priv->text_column >= 0)
4721     {
4722       model = gtk_combo_box_get_model (combo_box);
4723       gtk_tree_model_get_iter_from_string (model, &iter, path);
4724
4725       gtk_tree_model_get (model, &iter,
4726                           priv->text_column, &text,
4727                           -1);
4728     }
4729
4730   return text;
4731 }
4732
4733
4734 static GObject *
4735 gtk_combo_box_constructor (GType                  type,
4736                            guint                  n_construct_properties,
4737                            GObjectConstructParam *construct_properties)
4738 {
4739   GObject            *object;
4740   GtkComboBox        *combo_box;
4741   GtkComboBoxPrivate *priv;
4742
4743   object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4744     (type, n_construct_properties, construct_properties);
4745
4746   combo_box = GTK_COMBO_BOX (object);
4747   priv      = combo_box->priv;
4748
4749   if (!priv->area)
4750     {
4751       priv->area = gtk_cell_area_box_new ();
4752       g_object_ref_sink (priv->area);
4753     }
4754
4755   priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4756   gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4757   gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4758   gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4759   _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4760   gtk_widget_show (priv->cell_view);
4761
4762   gtk_combo_box_check_appearance (combo_box);
4763
4764   if (priv->has_entry)
4765     {
4766       GtkWidget *entry;
4767       GtkStyleContext *context;
4768
4769       entry = gtk_entry_new ();
4770       gtk_widget_show (entry);
4771       gtk_container_add (GTK_CONTAINER (combo_box), entry);
4772
4773       context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4774       gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4775
4776       priv->text_renderer = gtk_cell_renderer_text_new ();
4777       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4778                                   priv->text_renderer, TRUE);
4779
4780       gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4781
4782       g_signal_connect (combo_box, "changed",
4783                         G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4784     }
4785
4786   return object;
4787 }
4788
4789
4790 static void
4791 gtk_combo_box_dispose(GObject* object)
4792 {
4793   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4794
4795   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4796     {
4797       gtk_combo_box_menu_destroy (combo_box);
4798       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4799       combo_box->priv->popup_widget = NULL;
4800     }
4801
4802   if (combo_box->priv->area)
4803     {
4804       g_object_unref (combo_box->priv->area);
4805       combo_box->priv->area = NULL;
4806     }
4807
4808   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4809     gtk_combo_box_list_destroy (combo_box);
4810
4811   if (combo_box->priv->popup_window)
4812     {
4813       gtk_widget_destroy (combo_box->priv->popup_window);
4814       combo_box->priv->popup_window = NULL;
4815     }
4816
4817   gtk_combo_box_unset_model (combo_box);
4818
4819   G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4820 }
4821
4822 static void
4823 gtk_combo_box_finalize (GObject *object)
4824 {
4825   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4826
4827   g_free (combo_box->priv->tearoff_title);
4828
4829   G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4830 }
4831
4832 static gboolean
4833 gtk_cell_editable_key_press (GtkWidget   *widget,
4834                              GdkEventKey *event,
4835                              gpointer     data)
4836 {
4837   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4838
4839   if (event->keyval == GDK_KEY_Escape)
4840     {
4841       g_object_set (combo_box,
4842                     "editing-canceled", TRUE,
4843                     NULL);
4844       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4845       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4846
4847       return TRUE;
4848     }
4849   else if (event->keyval == GDK_KEY_Return ||
4850            event->keyval == GDK_KEY_ISO_Enter ||
4851            event->keyval == GDK_KEY_KP_Enter)
4852     {
4853       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4854       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4855
4856       return TRUE;
4857     }
4858
4859   return FALSE;
4860 }
4861
4862 static gboolean
4863 popdown_idle (gpointer data)
4864 {
4865   GtkComboBox *combo_box;
4866
4867   combo_box = GTK_COMBO_BOX (data);
4868
4869   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4870   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4871
4872   g_object_unref (combo_box);
4873
4874   return FALSE;
4875 }
4876
4877 static void
4878 popdown_handler (GtkWidget *widget,
4879                  gpointer   data)
4880 {
4881   gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4882 }
4883
4884 static gboolean
4885 popup_idle (gpointer data)
4886 {
4887   GtkComboBox *combo_box;
4888
4889   combo_box = GTK_COMBO_BOX (data);
4890
4891   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4892       combo_box->priv->cell_view)
4893     g_signal_connect_object (combo_box->priv->popup_widget,
4894                              "unmap", G_CALLBACK (popdown_handler),
4895                              combo_box, 0);
4896
4897   /* we unset this if a menu item is activated */
4898   g_object_set (combo_box,
4899                 "editing-canceled", TRUE,
4900                 NULL);
4901   gtk_combo_box_popup (combo_box);
4902
4903   combo_box->priv->popup_idle_id = 0;
4904   combo_box->priv->activate_button = 0;
4905   combo_box->priv->activate_time = 0;
4906
4907   return FALSE;
4908 }
4909
4910 static void
4911 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4912                              GdkEvent        *event)
4913 {
4914   GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4915   GtkWidget *child;
4916
4917   combo_box->priv->is_cell_renderer = TRUE;
4918
4919   if (combo_box->priv->cell_view)
4920     {
4921       g_signal_connect_object (combo_box->priv->button, "key-press-event",
4922                                G_CALLBACK (gtk_cell_editable_key_press),
4923                                cell_editable, 0);
4924
4925       gtk_widget_grab_focus (combo_box->priv->button);
4926     }
4927   else
4928     {
4929       child = gtk_bin_get_child (GTK_BIN (combo_box));
4930
4931       g_signal_connect_object (child, "key-press-event",
4932                                G_CALLBACK (gtk_cell_editable_key_press),
4933                                cell_editable, 0);
4934
4935       gtk_widget_grab_focus (child);
4936       gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4937     }
4938
4939   /* we do the immediate popup only for the optionmenu-like
4940    * appearance
4941    */
4942   if (combo_box->priv->is_cell_renderer &&
4943       combo_box->priv->cell_view && !combo_box->priv->tree_view)
4944     {
4945       if (event && event->type == GDK_BUTTON_PRESS)
4946         {
4947           GdkEventButton *event_button = (GdkEventButton *)event;
4948
4949           combo_box->priv->activate_button = event_button->button;
4950           combo_box->priv->activate_time = event_button->time;
4951         }
4952
4953       combo_box->priv->popup_idle_id =
4954           gdk_threads_add_idle (popup_idle, combo_box);
4955     }
4956 }
4957
4958
4959 /**
4960  * gtk_combo_box_get_add_tearoffs:
4961  * @combo_box: a #GtkComboBox
4962  *
4963  * Gets the current value of the :add-tearoffs property.
4964  *
4965  * Return value: the current value of the :add-tearoffs property.
4966  */
4967 gboolean
4968 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4969 {
4970   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4971
4972   return combo_box->priv->add_tearoffs;
4973 }
4974
4975 /**
4976  * gtk_combo_box_set_add_tearoffs:
4977  * @combo_box: a #GtkComboBox
4978  * @add_tearoffs: %TRUE to add tearoff menu items
4979  *
4980  * Sets whether the popup menu should have a tearoff
4981  * menu item.
4982  *
4983  * Since: 2.6
4984  */
4985 void
4986 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4987                                 gboolean     add_tearoffs)
4988 {
4989   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4990
4991   add_tearoffs = add_tearoffs != FALSE;
4992
4993   if (combo_box->priv->add_tearoffs != add_tearoffs)
4994     {
4995       combo_box->priv->add_tearoffs = add_tearoffs;
4996       gtk_combo_box_check_appearance (combo_box);
4997
4998       if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4999         _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
5000                                     combo_box->priv->add_tearoffs);
5001
5002       g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5003     }
5004 }
5005
5006 /**
5007  * gtk_combo_box_get_title:
5008  * @combo_box: a #GtkComboBox
5009  *
5010  * Gets the current title of the menu in tearoff mode. See
5011  * gtk_combo_box_set_add_tearoffs().
5012  *
5013  * Returns: the menu's title in tearoff mode. This is an internal copy of the
5014  * string which must not be freed.
5015  *
5016  * Since: 2.10
5017  */
5018 const gchar*
5019 gtk_combo_box_get_title (GtkComboBox *combo_box)
5020 {
5021   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5022
5023   return combo_box->priv->tearoff_title;
5024 }
5025
5026 static void
5027 gtk_combo_box_update_title (GtkComboBox *combo_box)
5028 {
5029   gtk_combo_box_check_appearance (combo_box);
5030
5031   if (combo_box->priv->popup_widget &&
5032       GTK_IS_MENU (combo_box->priv->popup_widget))
5033     gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5034                         combo_box->priv->tearoff_title);
5035 }
5036
5037 /**
5038  * gtk_combo_box_set_title:
5039  * @combo_box: a #GtkComboBox
5040  * @title: a title for the menu in tearoff mode
5041  *
5042  * Sets the menu's title in tearoff mode.
5043  *
5044  * Since: 2.10
5045  */
5046 void
5047 gtk_combo_box_set_title (GtkComboBox *combo_box,
5048                          const gchar *title)
5049 {
5050   GtkComboBoxPrivate *priv;
5051
5052   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5053
5054   priv = combo_box->priv;
5055
5056   if (strcmp (title ? title : "",
5057               priv->tearoff_title ? priv->tearoff_title : "") != 0)
5058     {
5059       g_free (priv->tearoff_title);
5060       priv->tearoff_title = g_strdup (title);
5061
5062       gtk_combo_box_update_title (combo_box);
5063
5064       g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5065     }
5066 }
5067
5068
5069 /**
5070  * gtk_combo_box_set_popup_fixed_width:
5071  * @combo_box: a #GtkComboBox
5072  * @fixed: whether to use a fixed popup width
5073  *
5074  * Specifies whether the popup's width should be a fixed width
5075  * matching the allocated width of the combo box.
5076  *
5077  * Since: 3.0
5078  **/
5079 void
5080 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5081                                      gboolean     fixed)
5082 {
5083   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5084
5085   if (combo_box->priv->popup_fixed_width != fixed)
5086     {
5087       combo_box->priv->popup_fixed_width = fixed;
5088
5089       g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5090     }
5091 }
5092
5093 /**
5094  * gtk_combo_box_get_popup_fixed_width:
5095  * @combo_box: a #GtkComboBox
5096  *
5097  * Gets whether the popup uses a fixed width matching
5098  * the allocated width of the combo box.
5099  *
5100  * Returns: %TRUE if the popup uses a fixed width
5101  *
5102  * Since: 3.0
5103  **/
5104 gboolean
5105 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5106 {
5107   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5108
5109   return combo_box->priv->popup_fixed_width;
5110 }
5111
5112
5113 /**
5114  * gtk_combo_box_get_popup_accessible:
5115  * @combo_box: a #GtkComboBox
5116  *
5117  * Gets the accessible object corresponding to the combo box's popup.
5118  *
5119  * This function is mostly intended for use by accessibility technologies;
5120  * applications should have little use for it.
5121  *
5122  * Returns: (transfer none): the accessible object corresponding
5123  *     to the combo box's popup.
5124  *
5125  * Since: 2.6
5126  */
5127 AtkObject*
5128 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5129 {
5130   AtkObject *atk_obj;
5131
5132   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5133
5134   if (combo_box->priv->popup_widget)
5135     {
5136       atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5137       return atk_obj;
5138     }
5139
5140   return NULL;
5141 }
5142
5143 /**
5144  * gtk_combo_box_get_row_separator_func: (skip)
5145  * @combo_box: a #GtkComboBox
5146  *
5147  * Returns the current row separator function.
5148  *
5149  * Return value: the current row separator function.
5150  *
5151  * Since: 2.6
5152  */
5153 GtkTreeViewRowSeparatorFunc
5154 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5155 {
5156   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5157
5158   return combo_box->priv->row_separator_func;
5159 }
5160
5161 /**
5162  * gtk_combo_box_set_row_separator_func:
5163  * @combo_box: a #GtkComboBox
5164  * @func: a #GtkTreeViewRowSeparatorFunc
5165  * @data: (allow-none): user data to pass to @func, or %NULL
5166  * @destroy: (allow-none): destroy notifier for @data, or %NULL
5167  *
5168  * Sets the row separator function, which is used to determine
5169  * whether a row should be drawn as a separator. If the row separator
5170  * function is %NULL, no separators are drawn. This is the default value.
5171  *
5172  * Since: 2.6
5173  */
5174 void
5175 gtk_combo_box_set_row_separator_func (GtkComboBox                 *combo_box,
5176                                       GtkTreeViewRowSeparatorFunc  func,
5177                                       gpointer                     data,
5178                                       GDestroyNotify               destroy)
5179 {
5180   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5181
5182   if (combo_box->priv->row_separator_destroy)
5183     combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5184
5185   combo_box->priv->row_separator_func = func;
5186   combo_box->priv->row_separator_data = data;
5187   combo_box->priv->row_separator_destroy = destroy;
5188
5189   /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5190   if (combo_box->priv->tree_view)
5191     {
5192       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5193       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5194     }
5195
5196   if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5197     {
5198       _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5199       _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5200     }
5201
5202   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5203 }
5204
5205 /**
5206  * gtk_combo_box_set_button_sensitivity:
5207  * @combo_box: a #GtkComboBox
5208  * @sensitivity: specify the sensitivity of the dropdown button
5209  *
5210  * Sets whether the dropdown button of the combo box should be
5211  * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5212  * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5213  *
5214  * Since: 2.14
5215  **/
5216 void
5217 gtk_combo_box_set_button_sensitivity (GtkComboBox        *combo_box,
5218                                       GtkSensitivityType  sensitivity)
5219 {
5220   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5221
5222   if (combo_box->priv->button_sensitivity != sensitivity)
5223     {
5224       combo_box->priv->button_sensitivity = sensitivity;
5225       gtk_combo_box_update_sensitivity (combo_box);
5226
5227       g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5228     }
5229 }
5230
5231 /**
5232  * gtk_combo_box_get_button_sensitivity:
5233  * @combo_box: a #GtkComboBox
5234  *
5235  * Returns whether the combo box sets the dropdown button
5236  * sensitive or not when there are no items in the model.
5237  *
5238  * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5239  *    is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5240  *    if the button is always insensitive or
5241  *    %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5242  *    the model has one item to be selected.
5243  *
5244  * Since: 2.14
5245  **/
5246 GtkSensitivityType
5247 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5248 {
5249   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5250
5251   return combo_box->priv->button_sensitivity;
5252 }
5253
5254
5255 /**
5256  * gtk_combo_box_get_has_entry:
5257  * @combo_box: a #GtkComboBox
5258  *
5259  * Returns whether the combo box has an entry.
5260  *
5261  * Return Value: whether there is an entry in @combo_box.
5262  *
5263  * Since: 2.24
5264  **/
5265 gboolean
5266 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5267 {
5268   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5269
5270   return combo_box->priv->has_entry;
5271 }
5272
5273 /**
5274  * gtk_combo_box_set_entry_text_column:
5275  * @combo_box: A #GtkComboBox
5276  * @text_column: A column in @model to get the strings from for
5277  *     the internal entry
5278  *
5279  * Sets the model column which @combo_box should use to get strings from
5280  * to be @text_column. The column @text_column in the model of @combo_box
5281  * must be of type %G_TYPE_STRING.
5282  *
5283  * This is only relevant if @combo_box has been created with
5284  * #GtkComboBox:has-entry as %TRUE.
5285  *
5286  * Since: 2.24
5287  */
5288 void
5289 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5290                                      gint         text_column)
5291 {
5292   GtkComboBoxPrivate *priv = combo_box->priv;
5293   GtkTreeModel *model;
5294
5295   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5296
5297   model = gtk_combo_box_get_model (combo_box);
5298
5299   g_return_if_fail (text_column >= 0);
5300   g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5301
5302   priv->text_column = text_column;
5303
5304   if (priv->text_renderer != NULL)
5305     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5306                                     priv->text_renderer,
5307                                     "text", text_column,
5308                                     NULL);
5309 }
5310
5311 /**
5312  * gtk_combo_box_get_entry_text_column:
5313  * @combo_box: A #GtkComboBox.
5314  *
5315  * Returns the column which @combo_box is using to get the strings
5316  * from to display in the internal entry.
5317  *
5318  * Return value: A column in the data source model of @combo_box.
5319  *
5320  * Since: 2.24
5321  */
5322 gint
5323 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5324 {
5325   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5326
5327   return combo_box->priv->text_column;
5328 }
5329
5330 /**
5331  * gtk_combo_box_set_focus_on_click:
5332  * @combo: a #GtkComboBox
5333  * @focus_on_click: whether the combo box grabs focus when clicked
5334  *    with the mouse
5335  *
5336  * Sets whether the combo box will grab focus when it is clicked with
5337  * the mouse. Making mouse clicks not grab focus is useful in places
5338  * like toolbars where you don't want the keyboard focus removed from
5339  * the main area of the application.
5340  *
5341  * Since: 2.6
5342  */
5343 void
5344 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5345                                   gboolean     focus_on_click)
5346 {
5347   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5348
5349   focus_on_click = focus_on_click != FALSE;
5350
5351   if (combo_box->priv->focus_on_click != focus_on_click)
5352     {
5353       combo_box->priv->focus_on_click = focus_on_click;
5354
5355       if (combo_box->priv->button)
5356         gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5357                                        focus_on_click);
5358
5359       g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5360     }
5361 }
5362
5363 /**
5364  * gtk_combo_box_get_focus_on_click:
5365  * @combo: a #GtkComboBox
5366  *
5367  * Returns whether the combo box grabs focus when it is clicked
5368  * with the mouse. See gtk_combo_box_set_focus_on_click().
5369  *
5370  * Return value: %TRUE if the combo box grabs focus when it is
5371  *     clicked with the mouse.
5372  *
5373  * Since: 2.6
5374  */
5375 gboolean
5376 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5377 {
5378   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5379   
5380   return combo_box->priv->focus_on_click;
5381 }
5382
5383
5384 static gboolean
5385 gtk_combo_box_buildable_custom_tag_start (GtkBuildable  *buildable,
5386                                           GtkBuilder    *builder,
5387                                           GObject       *child,
5388                                           const gchar   *tagname,
5389                                           GMarkupParser *parser,
5390                                           gpointer      *data)
5391 {
5392   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5393                                                 tagname, parser, data))
5394     return TRUE;
5395
5396   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5397                                                       tagname, parser, data);
5398 }
5399
5400 static void
5401 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5402                                         GtkBuilder   *builder,
5403                                         GObject      *child,
5404                                         const gchar  *tagname,
5405                                         gpointer     *data)
5406 {
5407   if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5408     parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5409 }
5410
5411 static GObject *
5412 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5413                                             GtkBuilder   *builder,
5414                                             const gchar  *childname)
5415 {
5416   GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5417
5418   if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5419     return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5420
5421   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5422 }
5423
5424 static void
5425 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5426                                    gint      *minimum_size,
5427                                    gint      *natural_size)
5428 {
5429   GtkComboBox           *combo_box = GTK_COMBO_BOX (widget);
5430   GtkComboBoxPrivate    *priv = combo_box->priv;
5431   gint                   font_size, arrow_size;
5432   PangoContext          *context;
5433   PangoFontMetrics      *metrics;
5434   const PangoFontDescription *font_desc;
5435   GtkWidget             *child;
5436   gint                   minimum_width = 0, natural_width = 0;
5437   gint                   child_min, child_nat;
5438   GtkStyleContext       *style_context;
5439   GtkStateFlags          state;
5440   GtkBorder              padding;
5441   gfloat                 arrow_scaling;
5442
5443   child = gtk_bin_get_child (GTK_BIN (widget));
5444
5445   /* common */
5446   gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5447
5448   gtk_widget_style_get (GTK_WIDGET (widget),
5449                         "arrow-size", &arrow_size,
5450                         "arrow-scaling", &arrow_scaling,
5451                         NULL);
5452
5453   style_context = gtk_widget_get_style_context (widget);
5454   state = gtk_widget_get_state_flags (widget);
5455
5456   get_widget_padding_and_border (widget, &padding);
5457   font_desc = gtk_style_context_get_font (style_context, state);
5458
5459   context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5460   metrics = pango_context_get_metrics (context, font_desc,
5461                                        pango_context_get_language (context));
5462   font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5463                             pango_font_metrics_get_descent (metrics));
5464   pango_font_metrics_unref (metrics);
5465
5466   arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5467
5468   gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5469
5470   if (!priv->tree_view)
5471     {
5472       /* menu mode */
5473       if (priv->cell_view)
5474         {
5475           gint box_width, xpad;
5476           GtkBorder button_padding;
5477
5478           get_widget_padding_and_border (priv->button, &button_padding);
5479
5480           gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5481           xpad = button_padding.left + button_padding.right + padding.left + padding.right;
5482
5483           minimum_width  = child_min + box_width + xpad;
5484           natural_width  = child_nat + box_width + xpad;
5485         }
5486       else
5487         {
5488           gint but_width, but_nat_width;
5489
5490           gtk_widget_get_preferred_width (priv->button,
5491                                           &but_width, &but_nat_width);
5492
5493           minimum_width  = child_min + but_width;
5494           natural_width  = child_nat + but_nat_width;
5495         }
5496     }
5497   else
5498     {
5499       /* list mode */
5500       gint button_width, button_nat_width;
5501
5502       /* sample + frame */
5503       minimum_width = child_min;
5504       natural_width = child_nat;
5505
5506       if (priv->cell_view_frame)
5507         {
5508           if (priv->has_frame)
5509             {
5510               gint border_width, xpad;
5511               GtkBorder frame_padding;
5512
5513               border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5514               get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5515               xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5516
5517               minimum_width  += xpad;
5518               natural_width  += xpad;
5519             }
5520         }
5521
5522       /* the button */
5523       gtk_widget_get_preferred_width (priv->button,
5524                                       &button_width, &button_nat_width);
5525
5526       minimum_width += button_width;
5527       natural_width += button_nat_width;
5528     }
5529
5530   minimum_width += padding.left + padding.right;
5531   natural_width += padding.left + padding.right;
5532
5533   if (minimum_size)
5534     *minimum_size = minimum_width;
5535
5536   if (natural_size)
5537     *natural_size = natural_width;
5538 }
5539
5540 static void
5541 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5542                                     gint      *minimum_size,
5543                                     gint      *natural_size)
5544 {
5545   gint min_width;
5546
5547   /* Combo box is height-for-width only
5548    * (so we always just reserve enough height for the minimum width) */
5549   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5550   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5551 }
5552
5553 static void
5554 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5555                                               gint       avail_size,
5556                                               gint      *minimum_size,
5557                                               gint      *natural_size)
5558 {
5559   /* Combo box is height-for-width only
5560    * (so we assume we always reserved enough height for the minimum width) */
5561   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5562 }
5563
5564
5565 static void
5566 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5567                                               gint       avail_size,
5568                                               gint      *minimum_size,
5569                                               gint      *natural_size)
5570 {
5571   GtkComboBox           *combo_box = GTK_COMBO_BOX (widget);
5572   GtkComboBoxPrivate    *priv = combo_box->priv;
5573   gint                   min_height = 0, nat_height = 0;
5574   gint                   size;
5575   GtkWidget             *child;
5576   GtkBorder              padding;
5577
5578   child = gtk_bin_get_child (GTK_BIN (widget));
5579
5580   get_widget_padding_and_border (widget, &padding);
5581   size = avail_size;
5582
5583   if (!priv->tree_view)
5584     {
5585       /* menu mode */
5586       if (priv->cell_view)
5587         {
5588           /* calculate x/y padding and separator/arrow size */
5589           gint box_width, box_height;
5590           gint xpad, ypad;
5591           GtkBorder button_padding;
5592
5593           get_widget_padding_and_border (priv->button, &button_padding);
5594
5595           gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5596           gtk_widget_get_preferred_height_for_width (priv->box,
5597                                                      box_width, &box_height, NULL);
5598
5599           xpad = button_padding.left + button_padding.right;
5600           ypad = button_padding.top + button_padding.bottom;
5601
5602           size -= box_width + xpad;
5603
5604           /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5605            * and fitting the whole treemodel */
5606           gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5607
5608           min_height = MAX (min_height, box_height);
5609           nat_height = MAX (nat_height, box_height);
5610
5611           min_height += ypad;
5612           nat_height += ypad;
5613         }
5614       else
5615         {
5616           /* there is a custom child widget inside (no priv->cell_view) */
5617           gint but_width, but_height;
5618
5619           gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5620           gtk_widget_get_preferred_height_for_width (priv->button,
5621                                                      but_width, &but_height, NULL);
5622
5623           size -= but_width;
5624
5625           /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5626            * and fitting the whole treemodel */
5627           gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5628
5629           min_height = MAX (min_height, but_height);
5630           nat_height = MAX (nat_height, but_height);
5631         }
5632     }
5633   else
5634     {
5635       /* list mode */
5636       gint but_width, but_height;
5637       gint xpad = 0, ypad = 0;
5638
5639       gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5640       gtk_widget_get_preferred_height_for_width (priv->button,
5641                                                  but_width, &but_height, NULL);
5642
5643       if (priv->cell_view_frame && priv->has_frame)
5644         {
5645           GtkBorder frame_padding;
5646           gint border_width;
5647
5648           border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5649           get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5650
5651           xpad = (2 * border_width) + padding.left + frame_padding.right;
5652           ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5653         }
5654
5655       size -= but_width;
5656       size -= xpad;
5657
5658       /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5659        * and fitting the whole treemodel */
5660       gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5661
5662       min_height = MAX (min_height, but_height);
5663       nat_height = MAX (nat_height, but_height);
5664
5665       min_height += ypad;
5666       nat_height += ypad;
5667     }
5668
5669   min_height += padding.top + padding.bottom;
5670   nat_height += padding.top + padding.bottom;
5671
5672   if (minimum_size)
5673     *minimum_size = min_height;
5674
5675   if (natural_size)
5676     *natural_size = nat_height;
5677 }
5678
5679 /**
5680  * gtk_combo_box_set_id_column:
5681  * @combo_box: A #GtkComboBox
5682  * @id_column: A column in @model to get string IDs for values from
5683  *
5684  * Sets the model column which @combo_box should use to get string IDs
5685  * for values from. The column @id_column in the model of @combo_box
5686  * must be of type %G_TYPE_STRING.
5687  *
5688  * Since: 3.0
5689  */
5690 void
5691 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5692                              gint         id_column)
5693 {
5694   GtkComboBoxPrivate *priv = combo_box->priv;
5695   GtkTreeModel *model;
5696
5697   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5698
5699   if (id_column != priv->id_column)
5700     {
5701       model = gtk_combo_box_get_model (combo_box);
5702
5703       g_return_if_fail (id_column >= 0);
5704       g_return_if_fail (model == NULL ||
5705                         id_column < gtk_tree_model_get_n_columns (model));
5706
5707       priv->id_column = id_column;
5708
5709       g_object_notify (G_OBJECT (combo_box), "id-column");
5710       g_object_notify (G_OBJECT (combo_box), "active-id");
5711     }
5712 }
5713
5714 /**
5715  * gtk_combo_box_get_id_column:
5716  * @combo_box: A #GtkComboBox
5717  *
5718  * Returns the column which @combo_box is using to get string IDs
5719  * for values from.
5720  *
5721  * Return value: A column in the data source model of @combo_box.
5722  *
5723  * Since: 3.0
5724  */
5725 gint
5726 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5727 {
5728   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5729
5730   return combo_box->priv->id_column;
5731 }
5732
5733 /**
5734  * gtk_combo_box_get_active_id:
5735  * @combo_box: a #GtkComboBox
5736  *
5737  * Returns the ID of the active row of @combo_box.  This value is taken
5738  * from the active row and the column specified by the #GtkComboBox:id-column
5739  * property of @combo_box (see gtk_combo_box_set_id_column()).
5740  *
5741  * The returned value is an interned string which means that you can
5742  * compare the pointer by value to other interned strings and that you
5743  * must not free it.
5744  *
5745  * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5746  * no row is active, or if the active row has a %NULL ID value, then %NULL
5747  * is returned.
5748  *
5749  * Return value: the ID of the active row, or %NULL
5750  *
5751  * Since: 3.0
5752  **/
5753 const gchar *
5754 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5755 {
5756   GtkTreeModel *model;
5757   GtkTreeIter iter;
5758   gint column;
5759
5760   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5761
5762   column = combo_box->priv->id_column;
5763
5764   if (column < 0)
5765     return NULL;
5766
5767   model = gtk_combo_box_get_model (combo_box);
5768   g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5769                         G_TYPE_STRING, NULL);
5770
5771   if (gtk_combo_box_get_active_iter (combo_box, &iter))
5772     {
5773       const gchar *interned;
5774       gchar *id;
5775
5776       gtk_tree_model_get (model, &iter, column, &id, -1);
5777       interned = g_intern_string (id);
5778       g_free (id);
5779
5780       return interned;
5781     }
5782
5783   return NULL;
5784 }
5785
5786 /**
5787  * gtk_combo_box_set_active_id:
5788  * @combo_box: a #GtkComboBox
5789  * @active_id: (allow-none): the ID of the row to select, or %NULL
5790  *
5791  * Changes the active row of @combo_box to the one that has an ID equal to
5792  * @active_id, or unsets the active row if @active_id is %NULL.  Rows having
5793  * a %NULL ID string cannot be made active by this function.
5794  *
5795  * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5796  * row has the given ID then the function does nothing and returns %FALSE.
5797  *
5798  * Returns: %TRUE if a row with a matching ID was found.  If a %NULL
5799  *          @active_id was given to unset the active row, the function
5800  *          always returns %TRUE.
5801  *
5802  * Since: 3.0
5803  **/
5804 gboolean
5805 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5806                              const gchar *active_id)
5807 {
5808   GtkTreeModel *model;
5809   GtkTreeIter iter;
5810   gboolean match = FALSE;
5811   gint column;
5812
5813   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5814
5815   if (active_id == NULL)
5816     {
5817       gtk_combo_box_set_active (combo_box, -1);
5818       return TRUE;  /* active row was successfully unset */
5819     }
5820
5821   column = combo_box->priv->id_column;
5822
5823   if (column < 0)
5824     return FALSE;
5825
5826   model = gtk_combo_box_get_model (combo_box);
5827   g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5828                         G_TYPE_STRING, FALSE);
5829
5830   if (gtk_tree_model_get_iter_first (model, &iter))
5831     do {
5832       gchar *id;
5833
5834       gtk_tree_model_get (model, &iter, column, &id, -1);
5835       if (id != NULL)
5836         match = strcmp (id, active_id) == 0;
5837       g_free (id);
5838
5839       if (match)
5840         {
5841           gtk_combo_box_set_active_iter (combo_box, &iter);
5842           break;
5843         }
5844     } while (gtk_tree_model_iter_next (model, &iter));
5845
5846     return match;
5847 }