]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Move wholly deprecated files to a subdirectory
[~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   widget_class = (GtkWidgetClass *)klass;
496   widget_class->size_allocate = gtk_combo_box_size_allocate;
497   widget_class->draw = gtk_combo_box_draw;
498   widget_class->scroll_event = gtk_combo_box_scroll_event;
499   widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
500   widget_class->grab_focus = gtk_combo_box_grab_focus;
501   widget_class->style_updated = gtk_combo_box_style_updated;
502   widget_class->state_flags_changed = gtk_combo_box_state_flags_changed;
503   widget_class->get_preferred_width = gtk_combo_box_get_preferred_width;
504   widget_class->get_preferred_height = gtk_combo_box_get_preferred_height;
505   widget_class->get_preferred_height_for_width = gtk_combo_box_get_preferred_height_for_width;
506   widget_class->get_preferred_width_for_height = gtk_combo_box_get_preferred_width_for_height;
507   widget_class->destroy = gtk_combo_box_destroy;
508   widget_class->direction_changed = gtk_combo_box_direction_changed;
509
510   object_class = (GObjectClass *)klass;
511   object_class->constructor = gtk_combo_box_constructor;
512   object_class->dispose = gtk_combo_box_dispose;
513   object_class->finalize = gtk_combo_box_finalize;
514   object_class->set_property = gtk_combo_box_set_property;
515   object_class->get_property = gtk_combo_box_get_property;
516
517   klass->format_entry_text = gtk_combo_box_format_entry_text;
518
519   /* signals */
520   /**
521    * GtkComboBox::changed:
522    * @widget: the object which received the signal
523    * 
524    * The changed signal is emitted when the active
525    * item is changed. The can be due to the user selecting
526    * a different item from the list, or due to a
527    * call to gtk_combo_box_set_active_iter().
528    * It will also be emitted while typing into the entry of a combo box
529    * with an entry.
530    *
531    * Since: 2.4
532    */
533   combo_box_signals[CHANGED] =
534     g_signal_new (I_("changed"),
535                   G_OBJECT_CLASS_TYPE (klass),
536                   G_SIGNAL_RUN_LAST,
537                   G_STRUCT_OFFSET (GtkComboBoxClass, changed),
538                   NULL, NULL,
539                   g_cclosure_marshal_VOID__VOID,
540                   G_TYPE_NONE, 0);
541   /**
542    * GtkComboBox::move-active:
543    * @widget: the object that received the signal
544    * @scroll_type: a #GtkScrollType
545    *
546    * The ::move-active signal is a
547    * <link linkend="keybinding-signals">keybinding signal</link>
548    * which gets emitted to move the active selection.
549    *
550    * Since: 2.12
551    */
552   combo_box_signals[MOVE_ACTIVE] =
553     g_signal_new_class_handler (I_("move-active"),
554                                 G_OBJECT_CLASS_TYPE (klass),
555                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
556                                 G_CALLBACK (gtk_combo_box_real_move_active),
557                                 NULL, NULL,
558                                 g_cclosure_marshal_VOID__ENUM,
559                                 G_TYPE_NONE, 1,
560                                 GTK_TYPE_SCROLL_TYPE);
561
562   /**
563    * GtkComboBox::popup:
564    * @widget: the object that received the signal
565    *
566    * The ::popup signal is a
567    * <link linkend="keybinding-signals">keybinding signal</link>
568    * which gets emitted to popup the combo box list.
569    *
570    * The default binding for this signal is Alt+Down.
571    *
572    * Since: 2.12
573    */
574   combo_box_signals[POPUP] =
575     g_signal_new_class_handler (I_("popup"),
576                                 G_OBJECT_CLASS_TYPE (klass),
577                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
578                                 G_CALLBACK (gtk_combo_box_real_popup),
579                                 NULL, NULL,
580                                 g_cclosure_marshal_VOID__VOID,
581                                 G_TYPE_NONE, 0);
582   /**
583    * GtkComboBox::popdown:
584    * @button: the object which received the signal
585    *
586    * The ::popdown signal is a
587    * <link linkend="keybinding-signals">keybinding signal</link>
588    * which gets emitted to popdown the combo box list.
589    *
590    * The default bindings for this signal are Alt+Up and Escape.
591    *
592    * Since: 2.12
593    */
594   combo_box_signals[POPDOWN] =
595     g_signal_new_class_handler (I_("popdown"),
596                                 G_OBJECT_CLASS_TYPE (klass),
597                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
598                                 G_CALLBACK (gtk_combo_box_real_popdown),
599                                 NULL, NULL,
600                                 _gtk_marshal_BOOLEAN__VOID,
601                                 G_TYPE_BOOLEAN, 0);
602
603   /**
604    * GtkComboBox::format-entry-text:
605    * @combo: the object which received the signal
606    * @path: the GtkTreePath string from the combo box's current model to format text for
607    *
608    * For combo boxes that are created with an entry (See GtkComboBox:has-entry).
609    *
610    * A signal which allows you to change how the text displayed in a combo box's
611    * entry is displayed.
612    *
613    * Connect a signal handler which returns an allocated string representing
614    * @path. That string will then be used to set the text in the combo box's entry.
615    * The default signal handler uses the text from the GtkComboBox::entry-text-column 
616    * model column.
617    *
618    * Here's an example signal handler which fetches data from the model and
619    * displays it in the entry.
620    * |[
621    * static gchar*
622    * format_entry_text_callback (GtkComboBox *combo,
623    *                             const gchar *path,
624    *                             gpointer     user_data)
625    * {
626    *   GtkTreeIter iter;
627    *   GtkTreeModel model;
628    *   gdouble      value;
629    *   
630    *   model = gtk_combo_box_get_model (combo);
631    *
632    *   gtk_tree_model_get_iter_from_string (model, &iter, path);
633    *   gtk_tree_model_get (model, &iter, 
634    *                       THE_DOUBLE_VALUE_COLUMN, &value,
635    *                       -1);
636    *
637    *   return g_strdup_printf ("&percnt;g", value);
638    * }
639    * ]|
640    *
641    * Return value: (transfer full): a newly allocated string representing @path 
642    * for the current GtkComboBox model.
643    *
644    * Since: 3.4
645    */
646   combo_box_signals[FORMAT_ENTRY_TEXT] =
647     g_signal_new (I_("format-entry-text"),
648                   G_TYPE_FROM_CLASS (klass),
649                   G_SIGNAL_RUN_LAST,
650                   G_STRUCT_OFFSET (GtkComboBoxClass, format_entry_text),
651                   _gtk_single_string_accumulator, NULL,
652                   _gtk_marshal_STRING__STRING,
653                   G_TYPE_STRING, 1, G_TYPE_STRING);
654
655   /* key bindings */
656   binding_set = gtk_binding_set_by_class (widget_class);
657
658   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_MOD1_MASK,
659                                 "popup", 0);
660   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_MOD1_MASK,
661                                 "popup", 0);
662
663   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_MOD1_MASK,
664                                 "popdown", 0);
665   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_MOD1_MASK,
666                                 "popdown", 0);
667   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
668                                 "popdown", 0);
669
670   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
671                                 "move-active", 1,
672                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
673   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
674                                 "move-active", 1,
675                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_UP);
676   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
677                                 "move-active", 1,
678                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
679   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
680                                 "move-active", 1,
681                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_UP);
682   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
683                                 "move-active", 1,
684                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
685   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
686                                 "move-active", 1,
687                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
688
689   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
690                                 "move-active", 1,
691                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
692   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
693                                 "move-active", 1,
694                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_STEP_DOWN);
695   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
696                                 "move-active", 1,
697                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
698   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
699                                 "move-active", 1,
700                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_PAGE_DOWN);
701   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
702                                 "move-active", 1,
703                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
704   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
705                                 "move-active", 1,
706                                 GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
707
708   /* properties */
709   g_object_class_override_property (object_class,
710                                     PROP_EDITING_CANCELED,
711                                     "editing-canceled");
712
713   /**
714    * GtkComboBox:model:
715    *
716    * The model from which the combo box takes the values shown
717    * in the list.
718    *
719    * Since: 2.4
720    */
721   g_object_class_install_property (object_class,
722                                    PROP_MODEL,
723                                    g_param_spec_object ("model",
724                                                         P_("ComboBox model"),
725                                                         P_("The model for the combo box"),
726                                                         GTK_TYPE_TREE_MODEL,
727                                                         GTK_PARAM_READWRITE));
728
729   /**
730    * GtkComboBox:wrap-width:
731    *
732    * If wrap-width is set to a positive value, the list will be
733    * displayed in multiple columns, the number of columns is
734    * determined by wrap-width.
735    *
736    * Since: 2.4
737    */
738   g_object_class_install_property (object_class,
739                                    PROP_WRAP_WIDTH,
740                                    g_param_spec_int ("wrap-width",
741                                                      P_("Wrap width"),
742                                                      P_("Wrap width for laying out the items in a grid"),
743                                                      0,
744                                                      G_MAXINT,
745                                                      0,
746                                                      GTK_PARAM_READWRITE));
747
748
749   /**
750    * GtkComboBox:row-span-column:
751    *
752    * If this is set to a non-negative value, it must be the index of a column
753    * of type %G_TYPE_INT in the model.
754    *
755    * The values of that column are used to determine how many rows a value in
756    * the list will span. Therefore, the values in the model column pointed to
757    * by this property must be greater than zero and not larger than wrap-width.
758    *
759    * Since: 2.4
760    */
761   g_object_class_install_property (object_class,
762                                    PROP_ROW_SPAN_COLUMN,
763                                    g_param_spec_int ("row-span-column",
764                                                      P_("Row span column"),
765                                                      P_("TreeModel column containing the row span values"),
766                                                      -1,
767                                                      G_MAXINT,
768                                                      -1,
769                                                      GTK_PARAM_READWRITE));
770
771
772   /**
773    * GtkComboBox:column-span-column:
774    *
775    * If this is set to a non-negative value, it must be the index of a column
776    * of type %G_TYPE_INT in the model.
777    *
778    * The values of that column are used to determine how many columns a value
779    * in the list will span.
780    *
781    * Since: 2.4
782    */
783   g_object_class_install_property (object_class,
784                                    PROP_COLUMN_SPAN_COLUMN,
785                                    g_param_spec_int ("column-span-column",
786                                                      P_("Column span column"),
787                                                      P_("TreeModel column containing the column span values"),
788                                                      -1,
789                                                      G_MAXINT,
790                                                      -1,
791                                                      GTK_PARAM_READWRITE));
792
793
794   /**
795    * GtkComboBox:active:
796    *
797    * The item which is currently active. If the model is a non-flat treemodel,
798    * and the active item is not an immediate child of the root of the tree,
799    * this property has the value
800    * <literal>gtk_tree_path_get_indices (path)[0]</literal>,
801    * where <literal>path</literal> is the #GtkTreePath of the active item.
802    *
803    * Since: 2.4
804    */
805   g_object_class_install_property (object_class,
806                                    PROP_ACTIVE,
807                                    g_param_spec_int ("active",
808                                                      P_("Active item"),
809                                                      P_("The item which is currently active"),
810                                                      -1,
811                                                      G_MAXINT,
812                                                      -1,
813                                                      GTK_PARAM_READWRITE));
814
815   /**
816    * GtkComboBox:add-tearoffs:
817    *
818    * The add-tearoffs property controls whether generated menus
819    * have tearoff menu items.
820    *
821    * Note that this only affects menu style combo boxes.
822    *
823    * Since: 2.6
824    */
825   g_object_class_install_property (object_class,
826                                    PROP_ADD_TEAROFFS,
827                                    g_param_spec_boolean ("add-tearoffs",
828                                                          P_("Add tearoffs to menus"),
829                                                          P_("Whether dropdowns should have a tearoff menu item"),
830                                                          FALSE,
831                                                          GTK_PARAM_READWRITE));
832
833   /**
834    * GtkComboBox:has-frame:
835    *
836    * The has-frame property controls whether a frame
837    * is drawn around the entry.
838    *
839    * Since: 2.6
840    */
841   g_object_class_install_property (object_class,
842                                    PROP_HAS_FRAME,
843                                    g_param_spec_boolean ("has-frame",
844                                                          P_("Has Frame"),
845                                                          P_("Whether the combo box draws a frame around the child"),
846                                                          TRUE,
847                                                          GTK_PARAM_READWRITE));
848
849   g_object_class_install_property (object_class,
850                                    PROP_FOCUS_ON_CLICK,
851                                    g_param_spec_boolean ("focus-on-click",
852                                                          P_("Focus on click"),
853                                                          P_("Whether the combo box grabs focus when it is clicked with the mouse"),
854                                                          TRUE,
855                                                          GTK_PARAM_READWRITE));
856
857   /**
858    * GtkComboBox:tearoff-title:
859    *
860    * A title that may be displayed by the window manager
861    * when the popup is torn-off.
862    *
863    * Since: 2.10
864    */
865   g_object_class_install_property (object_class,
866                                    PROP_TEAROFF_TITLE,
867                                    g_param_spec_string ("tearoff-title",
868                                                         P_("Tearoff Title"),
869                                                         P_("A title that may be displayed by the window manager when the popup is torn-off"),
870                                                         NULL,
871                                                         GTK_PARAM_READWRITE));
872
873
874   /**
875    * GtkComboBox:popup-shown:
876    *
877    * Whether the combo boxes dropdown is popped up.
878    * Note that this property is mainly useful, because
879    * it allows you to connect to notify::popup-shown.
880    *
881    * Since: 2.10
882    */
883   g_object_class_install_property (object_class,
884                                    PROP_POPUP_SHOWN,
885                                    g_param_spec_boolean ("popup-shown",
886                                                          P_("Popup shown"),
887                                                          P_("Whether the combo's dropdown is shown"),
888                                                          FALSE,
889                                                          GTK_PARAM_READABLE));
890
891
892    /**
893     * GtkComboBox:button-sensitivity:
894     *
895     * Whether the dropdown button is sensitive when
896     * the model is empty.
897     *
898     * Since: 2.14
899     */
900    g_object_class_install_property (object_class,
901                                     PROP_BUTTON_SENSITIVITY,
902                                     g_param_spec_enum ("button-sensitivity",
903                                                        P_("Button Sensitivity"),
904                                                        P_("Whether the dropdown button is sensitive when the model is empty"),
905                                                        GTK_TYPE_SENSITIVITY_TYPE,
906                                                        GTK_SENSITIVITY_AUTO,
907                                                        GTK_PARAM_READWRITE));
908
909    /**
910     * GtkComboBox:has-entry:
911     *
912     * Whether the combo box has an entry.
913     *
914     * Since: 2.24
915     */
916    g_object_class_install_property (object_class,
917                                     PROP_HAS_ENTRY,
918                                     g_param_spec_boolean ("has-entry",
919                                                           P_("Has Entry"),
920                                                           P_("Whether combo box has an entry"),
921                                                           FALSE,
922                                                           GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
923
924    /**
925     * GtkComboBox:entry-text-column:
926     *
927     * The column in the combo box's model to associate with strings from the entry
928     * if the combo was created with #GtkComboBox:has-entry = %TRUE.
929     *
930     * Since: 2.24
931     */
932    g_object_class_install_property (object_class,
933                                     PROP_ENTRY_TEXT_COLUMN,
934                                     g_param_spec_int ("entry-text-column",
935                                                       P_("Entry Text Column"),
936                                                       P_("The column in the combo box's model to associate "
937                                                          "with strings from the entry if the combo was "
938                                                          "created with #GtkComboBox:has-entry = %TRUE"),
939                                                       -1, G_MAXINT, -1,
940                                                       GTK_PARAM_READWRITE));
941
942    /**
943     * GtkComboBox:id-column:
944     *
945     * The column in the combo box's model that provides string
946     * IDs for the values in the model, if != -1.
947     *
948     * Since: 3.0
949     */
950    g_object_class_install_property (object_class,
951                                     PROP_ID_COLUMN,
952                                     g_param_spec_int ("id-column",
953                                                       P_("ID Column"),
954                                                       P_("The column in the combo box's model that provides "
955                                                       "string IDs for the values in the model"),
956                                                       -1, G_MAXINT, -1,
957                                                       GTK_PARAM_READWRITE));
958
959    /**
960     * GtkComboBox:active-id:
961     *
962     * The value of the ID column of the active row.
963     *
964     * Since: 3.0
965     */
966    g_object_class_install_property (object_class,
967                                     PROP_ACTIVE_ID,
968                                     g_param_spec_string ("active-id",
969                                                          P_("Active id"),
970                                                          P_("The value of the id column "
971                                                          "for the active row"),
972                                                          NULL, GTK_PARAM_READWRITE));
973
974    /**
975     * GtkComboBox:popup-fixed-width:
976     *
977     * Whether the popup's width should be a fixed width matching the
978     * allocated width of the combo box.
979     *
980     * Since: 3.0
981     */
982    g_object_class_install_property (object_class,
983                                     PROP_POPUP_FIXED_WIDTH,
984                                     g_param_spec_boolean ("popup-fixed-width",
985                                                           P_("Popup Fixed Width"),
986                                                           P_("Whether the popup's width should be a "
987                                                              "fixed width matching the allocated width "
988                                                              "of the combo box"),
989                                                           TRUE,
990                                                           GTK_PARAM_READWRITE));
991
992    /**
993     * GtkComboBox:cell-area:
994     *
995     * The #GtkCellArea used to layout cell renderers for this combo box.
996     *
997     * If no area is specified when creating the combo box with gtk_combo_box_new_with_area() 
998     * a horizontally oriented #GtkCellAreaBox will be used.
999     *
1000     * Since: 3.0
1001     */
1002    g_object_class_install_property (object_class,
1003                                     PROP_CELL_AREA,
1004                                     g_param_spec_object ("cell-area",
1005                                                          P_("Cell Area"),
1006                                                          P_("The GtkCellArea used to layout cells"),
1007                                                          GTK_TYPE_CELL_AREA,
1008                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1009
1010   gtk_widget_class_install_style_property (widget_class,
1011                                            g_param_spec_boolean ("appears-as-list",
1012                                                                  P_("Appears as list"),
1013                                                                  P_("Whether dropdowns should look like lists rather than menus"),
1014                                                                  FALSE,
1015                                                                  GTK_PARAM_READABLE));
1016
1017   /**
1018    * GtkComboBox:arrow-size:
1019    *
1020    * Sets the minimum size of the arrow in the combo box.  Note
1021    * that the arrow size is coupled to the font size, so in case
1022    * a larger font is used, the arrow will be larger than set
1023    * by arrow size.
1024    *
1025    * Since: 2.12
1026    */
1027   gtk_widget_class_install_style_property (widget_class,
1028                                            g_param_spec_int ("arrow-size",
1029                                                              P_("Arrow Size"),
1030                                                              P_("The minimum size of the arrow in the combo box"),
1031                                                              0,
1032                                                              G_MAXINT,
1033                                                              15,
1034                                                              GTK_PARAM_READABLE));
1035
1036   /**
1037    * GtkComboBox:arrow-scaling:
1038    *
1039    * Sets the amount of space used up by the combobox arrow,
1040    * proportional to the font size.
1041    *
1042    * Since: 3.2
1043    */
1044   gtk_widget_class_install_style_property (widget_class,
1045                                            g_param_spec_float ("arrow-scaling",
1046                                                                P_("Arrow Scaling"),
1047                                                                P_("The amount of space used by the arrow"),
1048                                                              0,
1049                                                              2.0,
1050                                                              1.0,
1051                                                              GTK_PARAM_READABLE));
1052
1053   /**
1054    * GtkComboBox:shadow-type:
1055    *
1056    * Which kind of shadow to draw around the combo box.
1057    *
1058    * Since: 2.12
1059    */
1060   gtk_widget_class_install_style_property (widget_class,
1061                                            g_param_spec_enum ("shadow-type",
1062                                                               P_("Shadow type"),
1063                                                               P_("Which kind of shadow to draw around the combo box"),
1064                                                               GTK_TYPE_SHADOW_TYPE,
1065                                                               GTK_SHADOW_NONE,
1066                                                               GTK_PARAM_READABLE));
1067
1068   g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
1069
1070   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_COMBO_BOX_ACCESSIBLE);
1071 }
1072
1073 static void
1074 gtk_combo_box_buildable_init (GtkBuildableIface *iface)
1075 {
1076   parent_buildable_iface = g_type_interface_peek_parent (iface);
1077   iface->add_child = _gtk_cell_layout_buildable_add_child;
1078   iface->custom_tag_start = gtk_combo_box_buildable_custom_tag_start;
1079   iface->custom_tag_end = gtk_combo_box_buildable_custom_tag_end;
1080   iface->get_internal_child = gtk_combo_box_buildable_get_internal_child;
1081 }
1082
1083 static void
1084 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
1085 {
1086   iface->get_area = gtk_combo_box_cell_layout_get_area;
1087 }
1088
1089 static void
1090 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
1091 {
1092   iface->start_editing = gtk_combo_box_start_editing;
1093 }
1094
1095 static void
1096 gtk_combo_box_init (GtkComboBox *combo_box)
1097 {
1098   GtkComboBoxPrivate *priv;
1099
1100   combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
1101                                                  GTK_TYPE_COMBO_BOX,
1102                                                  GtkComboBoxPrivate);
1103   priv = combo_box->priv;
1104
1105   priv->wrap_width = 0;
1106
1107   priv->active = -1;
1108   priv->active_row = NULL;
1109   priv->col_column = -1;
1110   priv->row_column = -1;
1111
1112   priv->popup_shown = FALSE;
1113   priv->add_tearoffs = FALSE;
1114   priv->has_frame = TRUE;
1115   priv->is_cell_renderer = FALSE;
1116   priv->editing_canceled = FALSE;
1117   priv->auto_scroll = FALSE;
1118   priv->focus_on_click = TRUE;
1119   priv->button_sensitivity = GTK_SENSITIVITY_AUTO;
1120   priv->has_entry = FALSE;
1121   priv->popup_fixed_width = TRUE;
1122
1123   priv->text_column = -1;
1124   priv->text_renderer = NULL;
1125   priv->id_column = -1;
1126 }
1127
1128 static void
1129 gtk_combo_box_set_property (GObject      *object,
1130                             guint         prop_id,
1131                             const GValue *value,
1132                             GParamSpec   *pspec)
1133 {
1134   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1135   GtkComboBoxPrivate *priv = combo_box->priv;
1136   GtkCellArea *area;
1137
1138   switch (prop_id)
1139     {
1140     case PROP_MODEL:
1141       gtk_combo_box_set_model (combo_box, g_value_get_object (value));
1142       break;
1143
1144     case PROP_WRAP_WIDTH:
1145       gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
1146       break;
1147
1148     case PROP_ROW_SPAN_COLUMN:
1149       gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
1150       break;
1151
1152     case PROP_COLUMN_SPAN_COLUMN:
1153       gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
1154       break;
1155
1156     case PROP_ACTIVE:
1157       gtk_combo_box_set_active (combo_box, g_value_get_int (value));
1158       break;
1159
1160     case PROP_ADD_TEAROFFS:
1161       gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
1162       break;
1163
1164     case PROP_HAS_FRAME:
1165       priv->has_frame = g_value_get_boolean (value);
1166
1167       if (priv->has_entry)
1168         {
1169           GtkWidget *child;
1170
1171           child = gtk_bin_get_child (GTK_BIN (combo_box));
1172
1173           gtk_entry_set_has_frame (GTK_ENTRY (child), priv->has_frame);
1174         }
1175
1176       break;
1177
1178     case PROP_FOCUS_ON_CLICK:
1179       gtk_combo_box_set_focus_on_click (combo_box,
1180                                         g_value_get_boolean (value));
1181       break;
1182
1183     case PROP_TEAROFF_TITLE:
1184       gtk_combo_box_set_title (combo_box, g_value_get_string (value));
1185       break;
1186
1187     case PROP_POPUP_SHOWN:
1188       if (g_value_get_boolean (value))
1189         gtk_combo_box_popup (combo_box);
1190       else
1191         gtk_combo_box_popdown (combo_box);
1192       break;
1193
1194     case PROP_BUTTON_SENSITIVITY:
1195       gtk_combo_box_set_button_sensitivity (combo_box,
1196                                             g_value_get_enum (value));
1197       break;
1198
1199     case PROP_POPUP_FIXED_WIDTH:
1200       gtk_combo_box_set_popup_fixed_width (combo_box,
1201                                            g_value_get_boolean (value));
1202       break;
1203
1204     case PROP_EDITING_CANCELED:
1205       priv->editing_canceled = g_value_get_boolean (value);
1206       break;
1207
1208     case PROP_HAS_ENTRY:
1209       priv->has_entry = g_value_get_boolean (value);
1210       break;
1211
1212     case PROP_ENTRY_TEXT_COLUMN:
1213       gtk_combo_box_set_entry_text_column (combo_box, g_value_get_int (value));
1214       break;
1215
1216     case PROP_ID_COLUMN:
1217       gtk_combo_box_set_id_column (combo_box, g_value_get_int (value));
1218       break;
1219
1220     case PROP_ACTIVE_ID:
1221       gtk_combo_box_set_active_id (combo_box, g_value_get_string (value));
1222       break;
1223
1224     case PROP_CELL_AREA:
1225       /* Construct-only, can only be assigned once */
1226       area = g_value_get_object (value);
1227       if (area)
1228         {
1229           if (priv->area != NULL)
1230             {
1231               g_warning ("cell-area has already been set, ignoring construct property");
1232               g_object_ref_sink (area);
1233               g_object_unref (area);
1234             }
1235           else
1236             priv->area = g_object_ref_sink (area);
1237         }
1238       break;
1239
1240     default:
1241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1242       break;
1243     }
1244 }
1245
1246 static void
1247 gtk_combo_box_get_property (GObject    *object,
1248                             guint       prop_id,
1249                             GValue     *value,
1250                             GParamSpec *pspec)
1251 {
1252   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
1253   GtkComboBoxPrivate *priv = combo_box->priv;
1254
1255   switch (prop_id)
1256     {
1257       case PROP_MODEL:
1258         g_value_set_object (value, combo_box->priv->model);
1259         break;
1260
1261       case PROP_WRAP_WIDTH:
1262         g_value_set_int (value, combo_box->priv->wrap_width);
1263         break;
1264
1265       case PROP_ROW_SPAN_COLUMN:
1266         g_value_set_int (value, combo_box->priv->row_column);
1267         break;
1268
1269       case PROP_COLUMN_SPAN_COLUMN:
1270         g_value_set_int (value, combo_box->priv->col_column);
1271         break;
1272
1273       case PROP_ACTIVE:
1274         g_value_set_int (value, gtk_combo_box_get_active (combo_box));
1275         break;
1276
1277       case PROP_ADD_TEAROFFS:
1278         g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
1279         break;
1280
1281       case PROP_HAS_FRAME:
1282         g_value_set_boolean (value, combo_box->priv->has_frame);
1283         break;
1284
1285       case PROP_FOCUS_ON_CLICK:
1286         g_value_set_boolean (value, combo_box->priv->focus_on_click);
1287         break;
1288
1289       case PROP_TEAROFF_TITLE:
1290         g_value_set_string (value, gtk_combo_box_get_title (combo_box));
1291         break;
1292
1293       case PROP_POPUP_SHOWN:
1294         g_value_set_boolean (value, combo_box->priv->popup_shown);
1295         break;
1296
1297       case PROP_BUTTON_SENSITIVITY:
1298         g_value_set_enum (value, combo_box->priv->button_sensitivity);
1299         break;
1300
1301       case PROP_POPUP_FIXED_WIDTH:
1302         g_value_set_boolean (value, combo_box->priv->popup_fixed_width);
1303         break;
1304
1305       case PROP_EDITING_CANCELED:
1306         g_value_set_boolean (value, priv->editing_canceled);
1307         break;
1308
1309       case PROP_HAS_ENTRY:
1310         g_value_set_boolean (value, priv->has_entry);
1311         break;
1312
1313       case PROP_ENTRY_TEXT_COLUMN:
1314         g_value_set_int (value, priv->text_column);
1315         break;
1316
1317       case PROP_ID_COLUMN:
1318         g_value_set_int (value, priv->id_column);
1319         break;
1320
1321       case PROP_ACTIVE_ID:
1322         g_value_set_string (value, gtk_combo_box_get_active_id (combo_box));
1323         break;
1324
1325       case PROP_CELL_AREA:
1326         g_value_set_object (value, priv->area);
1327         break;
1328
1329       default:
1330         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1331         break;
1332     }
1333 }
1334
1335 static void
1336 gtk_combo_box_state_flags_changed (GtkWidget     *widget,
1337                                    GtkStateFlags  previous)
1338 {
1339   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1340   GtkComboBoxPrivate *priv = combo_box->priv;
1341
1342   if (gtk_widget_get_realized (widget))
1343     {
1344       if (priv->tree_view && priv->cell_view)
1345         {
1346           GtkStyleContext *context;
1347           GtkStateFlags state;
1348           GdkRGBA color;
1349
1350           context  = gtk_widget_get_style_context (widget);
1351           state = gtk_widget_get_state_flags (widget);
1352           gtk_style_context_get_background_color (context, state, &color);
1353
1354           gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1355         }
1356     }
1357
1358   gtk_widget_queue_draw (widget);
1359 }
1360
1361 static void
1362 gtk_combo_box_button_state_flags_changed (GtkWidget     *widget,
1363                                           GtkStateFlags  previous,
1364                                           gpointer       data)
1365 {
1366   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1367   GtkComboBoxPrivate *priv = combo_box->priv;
1368
1369   if (gtk_widget_get_realized (widget))
1370     {
1371       if (!priv->tree_view && priv->cell_view)
1372         gtk_widget_set_state_flags (priv->cell_view,
1373                                     gtk_widget_get_state_flags (widget),
1374                                     TRUE);
1375     }
1376
1377   gtk_widget_queue_draw (widget);
1378 }
1379
1380 static void
1381 gtk_combo_box_invalidate_order (GtkComboBox *combo_box)
1382 {
1383   gtk_container_forall (GTK_CONTAINER (combo_box),
1384                         (GtkCallback) gtk_widget_reset_style,
1385                         NULL);
1386 }
1387
1388 static void
1389 gtk_combo_box_direction_changed (GtkWidget        *widget,
1390                                  GtkTextDirection  previous_direction)
1391 {
1392   gtk_combo_box_invalidate_order (GTK_COMBO_BOX (widget));
1393 }
1394
1395 static GtkWidgetPath *
1396 gtk_combo_box_get_path_for_child (GtkContainer *container,
1397                                   GtkWidget    *child)
1398 {
1399   GtkComboBoxPrivate *priv = GTK_COMBO_BOX (container)->priv;
1400   GtkWidgetPath *path;
1401   GtkWidget *widget;
1402   gboolean found = FALSE;
1403   GList *visible_children, *l;
1404   GtkWidgetPath *sibling_path;
1405   int pos;
1406
1407   path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
1408
1409   if (gtk_widget_get_visible (child))
1410     {
1411       visible_children = NULL;
1412
1413       if (priv->button && gtk_widget_get_visible (priv->button))
1414         visible_children = g_list_prepend (visible_children, priv->button);
1415
1416       if (priv->cell_view_frame && gtk_widget_get_visible (priv->cell_view_frame))
1417         visible_children = g_list_prepend (visible_children, priv->cell_view_frame);
1418
1419       widget = gtk_bin_get_child (GTK_BIN (container));
1420       if (widget && gtk_widget_get_visible (widget))
1421         visible_children = g_list_prepend (visible_children, widget);
1422
1423       if (gtk_widget_get_direction (GTK_WIDGET (container)) == GTK_TEXT_DIR_RTL)
1424         visible_children = g_list_reverse (visible_children);
1425
1426       pos = 0;
1427
1428       for (l = visible_children; l; l = l->next)
1429         {
1430           widget = l->data;
1431
1432           if (widget == child)
1433             {
1434               found = TRUE;
1435               break;
1436             }
1437
1438           pos++;
1439         }
1440     }
1441
1442   if (found)
1443     {
1444       sibling_path = gtk_widget_path_new ();
1445
1446       for (l = visible_children; l; l = l->next)
1447         gtk_widget_path_append_for_widget (sibling_path, l->data);
1448
1449       gtk_widget_path_append_with_siblings (path, sibling_path, pos);
1450
1451       g_list_free (visible_children);
1452       gtk_widget_path_unref (sibling_path);
1453     }
1454   else
1455     {
1456       gtk_widget_path_append_for_widget (path, child);
1457     }
1458
1459   return path;
1460 }
1461
1462 static void
1463 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
1464 {
1465   GtkComboBoxPrivate *priv = combo_box->priv;
1466   gboolean appears_as_list;
1467
1468   /* if wrap_width > 0, then we are in grid-mode and forced to use
1469    * unix style
1470    */
1471   if (priv->wrap_width)
1472     appears_as_list = FALSE;
1473   else
1474     gtk_widget_style_get (GTK_WIDGET (combo_box),
1475                           "appears-as-list", &appears_as_list,
1476                           NULL);
1477   
1478   if (appears_as_list)
1479     {
1480       /* Destroy all the menu mode widgets, if they exist. */
1481       if (GTK_IS_MENU (priv->popup_widget))
1482         gtk_combo_box_menu_destroy (combo_box);
1483
1484       /* Create the list mode widgets, if they don't already exist. */
1485       if (!GTK_IS_TREE_VIEW (priv->tree_view))
1486         gtk_combo_box_list_setup (combo_box);
1487     }
1488   else
1489     {
1490       /* Destroy all the list mode widgets, if they exist. */
1491       if (GTK_IS_TREE_VIEW (priv->tree_view))
1492         gtk_combo_box_list_destroy (combo_box);
1493
1494       /* Create the menu mode widgets, if they don't already exist. */
1495       if (!GTK_IS_MENU (priv->popup_widget))
1496         gtk_combo_box_menu_setup (combo_box, TRUE);
1497     }
1498
1499   gtk_widget_style_get (GTK_WIDGET (combo_box),
1500                         "shadow-type", &priv->shadow_type,
1501                         NULL);
1502 }
1503
1504 static void
1505 gtk_combo_box_style_updated (GtkWidget *widget)
1506 {
1507   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1508   GtkComboBoxPrivate *priv = combo_box->priv;
1509   GtkWidget *child;
1510
1511   GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->style_updated (widget);
1512
1513   gtk_combo_box_check_appearance (combo_box);
1514
1515   if (priv->tree_view && priv->cell_view)
1516     {
1517       GtkStyleContext *context;
1518       GtkStateFlags state;
1519       GdkRGBA color;
1520
1521       context  = gtk_widget_get_style_context (widget);
1522       state = gtk_widget_get_state_flags (widget);
1523       gtk_style_context_get_background_color (context, state, &color);
1524
1525       gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
1526     }
1527
1528   child = gtk_bin_get_child (GTK_BIN (combo_box));
1529   if (GTK_IS_ENTRY (child))
1530     g_object_set (child, "shadow-type",
1531                   GTK_SHADOW_NONE == priv->shadow_type ?
1532                   GTK_SHADOW_IN : GTK_SHADOW_NONE, NULL);
1533 }
1534
1535 static void
1536 gtk_combo_box_button_toggled (GtkWidget *widget,
1537                               gpointer   data)
1538 {
1539   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
1540
1541   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1542     {
1543       if (!combo_box->priv->popup_in_progress)
1544         gtk_combo_box_popup (combo_box);
1545     }
1546   else
1547     gtk_combo_box_popdown (combo_box);
1548 }
1549
1550 static void
1551 gtk_combo_box_add (GtkContainer *container,
1552                    GtkWidget    *widget)
1553 {
1554   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1555   GtkComboBoxPrivate *priv = combo_box->priv;
1556
1557   if (priv->has_entry && !GTK_IS_ENTRY (widget))
1558     {
1559       g_warning ("Attempting to add a widget with type %s to a GtkComboBox that needs an entry "
1560                  "(need an instance of GtkEntry or of a subclass)",
1561                  G_OBJECT_TYPE_NAME (widget));
1562       return;
1563     }
1564
1565   if (priv->cell_view &&
1566       gtk_widget_get_parent (priv->cell_view))
1567     {
1568       gtk_widget_unparent (priv->cell_view);
1569       _gtk_bin_set_child (GTK_BIN (container), NULL);
1570       gtk_widget_queue_resize (GTK_WIDGET (container));
1571     }
1572   
1573   gtk_widget_set_parent (widget, GTK_WIDGET (container));
1574   _gtk_bin_set_child (GTK_BIN (container), widget);
1575
1576   if (priv->cell_view &&
1577       widget != priv->cell_view)
1578     {
1579       /* since the cell_view was unparented, it's gone now */
1580       priv->cell_view = NULL;
1581
1582       if (!priv->tree_view && priv->separator)
1583         {
1584           gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (priv->separator)),
1585                                 priv->separator);
1586           priv->separator = NULL;
1587
1588           gtk_widget_queue_resize (GTK_WIDGET (container));
1589         }
1590       else if (priv->cell_view_frame)
1591         {
1592           gtk_widget_unparent (priv->cell_view_frame);
1593           priv->cell_view_frame = NULL;
1594           priv->box = NULL;
1595         }
1596     }
1597
1598   if (priv->has_entry)
1599     {
1600       /* this flag is a hack to tell the entry to fill its allocation.
1601        */
1602       _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), TRUE);
1603
1604       g_signal_connect (widget, "changed",
1605                         G_CALLBACK (gtk_combo_box_entry_contents_changed),
1606                         combo_box);
1607
1608       gtk_entry_set_has_frame (GTK_ENTRY (widget), priv->has_frame);
1609     }
1610 }
1611
1612 static void
1613 gtk_combo_box_remove (GtkContainer *container,
1614                       GtkWidget    *widget)
1615 {
1616   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1617   GtkComboBoxPrivate *priv = combo_box->priv;
1618   GtkTreePath *path;
1619   gboolean appears_as_list;
1620
1621   if (priv->has_entry)
1622     {
1623       GtkWidget *child_widget;
1624
1625       child_widget = gtk_bin_get_child (GTK_BIN (container));
1626       if (widget && widget == child_widget)
1627         {
1628           g_signal_handlers_disconnect_by_func (widget,
1629                                                 gtk_combo_box_entry_contents_changed,
1630                                                 container);
1631           _gtk_entry_set_is_cell_renderer (GTK_ENTRY (widget), FALSE);
1632         }
1633     }
1634
1635   if (widget == priv->cell_view)
1636     priv->cell_view = NULL;
1637
1638   gtk_widget_unparent (widget);
1639   _gtk_bin_set_child (GTK_BIN (container), NULL);
1640
1641   if (gtk_widget_in_destruction (GTK_WIDGET (combo_box)))
1642     return;
1643
1644   gtk_widget_queue_resize (GTK_WIDGET (container));
1645
1646   if (!priv->tree_view)
1647     appears_as_list = FALSE;
1648   else
1649     appears_as_list = TRUE;
1650   
1651   if (appears_as_list)
1652     gtk_combo_box_list_destroy (combo_box);
1653   else if (GTK_IS_MENU (priv->popup_widget))
1654     {
1655       gtk_combo_box_menu_destroy (combo_box);
1656       gtk_menu_detach (GTK_MENU (priv->popup_widget));
1657       priv->popup_widget = NULL;
1658     }
1659
1660   if (!priv->cell_view)
1661     {
1662       priv->cell_view = gtk_cell_view_new ();
1663       gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (container));
1664       _gtk_bin_set_child (GTK_BIN (container), priv->cell_view);
1665
1666       gtk_widget_show (priv->cell_view);
1667       gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view),
1668                                priv->model);
1669     }
1670
1671
1672   if (appears_as_list)
1673     gtk_combo_box_list_setup (combo_box);
1674   else
1675     gtk_combo_box_menu_setup (combo_box, TRUE);
1676
1677   if (gtk_tree_row_reference_valid (priv->active_row))
1678     {
1679       path = gtk_tree_row_reference_get_path (priv->active_row);
1680       gtk_combo_box_set_active_internal (combo_box, path);
1681       gtk_tree_path_free (path);
1682     }
1683   else
1684     gtk_combo_box_set_active_internal (combo_box, NULL);
1685 }
1686
1687 static void
1688 gtk_combo_box_menu_show (GtkWidget *menu,
1689                          gpointer   user_data)
1690 {
1691   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1692   GtkComboBoxPrivate *priv = combo_box->priv;
1693
1694   gtk_combo_box_child_show (menu, user_data);
1695
1696   priv->popup_in_progress = TRUE;
1697   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
1698                                 TRUE);
1699   priv->popup_in_progress = FALSE;
1700 }
1701
1702 static void
1703 gtk_combo_box_menu_hide (GtkWidget *menu,
1704                          gpointer   user_data)
1705 {
1706   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1707
1708   gtk_combo_box_child_hide (menu,user_data);
1709
1710   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1711                                 FALSE);
1712 }
1713
1714 static void
1715 gtk_combo_box_detacher (GtkWidget *widget,
1716                         GtkMenu          *menu)
1717 {
1718   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1719   GtkComboBoxPrivate *priv = combo_box->priv;
1720
1721   g_return_if_fail (priv->popup_widget == (GtkWidget *) menu);
1722
1723   g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1724                                         gtk_combo_box_menu_show,
1725                                         combo_box);
1726   g_signal_handlers_disconnect_by_func (menu->priv->toplevel,
1727                                         gtk_combo_box_menu_hide,
1728                                         combo_box);
1729   
1730   priv->popup_widget = NULL;
1731 }
1732
1733 static void
1734 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1735                                 GtkWidget   *popup)
1736 {
1737   GtkComboBoxPrivate *priv = combo_box->priv;
1738
1739   if (GTK_IS_MENU (priv->popup_widget))
1740     {
1741       gtk_menu_detach (GTK_MENU (priv->popup_widget));
1742       priv->popup_widget = NULL;
1743     }
1744   else if (priv->popup_widget)
1745     {
1746       gtk_container_remove (GTK_CONTAINER (priv->scrolled_window),
1747                             priv->popup_widget);
1748       g_object_unref (priv->popup_widget);
1749       priv->popup_widget = NULL;
1750     }
1751
1752   if (GTK_IS_MENU (popup))
1753     {
1754       if (priv->popup_window)
1755         {
1756           gtk_widget_destroy (priv->popup_window);
1757           priv->popup_window = NULL;
1758         }
1759
1760       priv->popup_widget = popup;
1761
1762       /*
1763        * Note that we connect to show/hide on the toplevel, not the
1764        * menu itself, since the menu is not shown/hidden when it is
1765        * popped up while torn-off.
1766        */
1767       g_signal_connect (GTK_MENU (popup)->priv->toplevel, "show",
1768                         G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1769       g_signal_connect (GTK_MENU (popup)->priv->toplevel, "hide",
1770                         G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1771
1772       gtk_menu_attach_to_widget (GTK_MENU (popup),
1773                                  GTK_WIDGET (combo_box),
1774                                  gtk_combo_box_detacher);
1775     }
1776   else
1777     {
1778       if (!priv->popup_window)
1779         {
1780           GtkWidget *toplevel;
1781
1782           priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1783           gtk_widget_set_name (priv->popup_window, "gtk-combobox-popup-window");
1784
1785           gtk_window_set_type_hint (GTK_WINDOW (priv->popup_window),
1786                                     GDK_WINDOW_TYPE_HINT_COMBO);
1787
1788           g_signal_connect (GTK_WINDOW (priv->popup_window),"show",
1789                             G_CALLBACK (gtk_combo_box_child_show),
1790                             combo_box);
1791           g_signal_connect (GTK_WINDOW (priv->popup_window),"hide",
1792                             G_CALLBACK (gtk_combo_box_child_hide),
1793                             combo_box);
1794
1795           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1796           if (GTK_IS_WINDOW (toplevel))
1797             {
1798               gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1799                                            GTK_WINDOW (priv->popup_window));
1800               gtk_window_set_transient_for (GTK_WINDOW (priv->popup_window),
1801                                             GTK_WINDOW (toplevel));
1802             }
1803
1804           gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
1805           gtk_window_set_screen (GTK_WINDOW (priv->popup_window),
1806                                  gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1807
1808           priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1809
1810           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1811                                           GTK_POLICY_NEVER,
1812                                           GTK_POLICY_NEVER);
1813           gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
1814                                                GTK_SHADOW_IN);
1815
1816           gtk_widget_show (priv->scrolled_window);
1817
1818           gtk_container_add (GTK_CONTAINER (priv->popup_window),
1819                              priv->scrolled_window);
1820         }
1821
1822       gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
1823                          popup);
1824
1825       gtk_widget_show (popup);
1826       g_object_ref (popup);
1827       priv->popup_widget = popup;
1828     }
1829 }
1830
1831 static void
1832 get_widget_padding (GtkWidget *widget,
1833                     GtkBorder *padding)
1834 {
1835   GtkStyleContext *context;
1836   GtkStateFlags state;
1837
1838   context = gtk_widget_get_style_context (widget);
1839   state = gtk_style_context_get_state (context);
1840
1841   gtk_style_context_get_padding (context, state, padding);
1842 }
1843
1844 static void
1845 gtk_combo_box_menu_position_below (GtkMenu  *menu,
1846                                    gint     *x,
1847                                    gint     *y,
1848                                    gint     *push_in,
1849                                    gpointer  user_data)
1850 {
1851   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1852   GtkAllocation child_allocation;
1853   gint sx, sy;
1854   GtkWidget *child;
1855   GtkRequisition req;
1856   GdkScreen *screen;
1857   gint monitor_num;
1858   GdkRectangle monitor;
1859   GtkBorder padding;
1860
1861   /* FIXME: is using the size request here broken? */
1862   child = gtk_bin_get_child (GTK_BIN (combo_box));
1863
1864   sx = sy = 0;
1865
1866   gtk_widget_get_allocation (child, &child_allocation);
1867
1868   if (!gtk_widget_get_has_window (child))
1869     {
1870       sx += child_allocation.x;
1871       sy += child_allocation.y;
1872     }
1873
1874   gdk_window_get_root_coords (gtk_widget_get_window (child),
1875                               sx, sy, &sx, &sy);
1876   get_widget_padding (GTK_WIDGET (combo_box), &padding);
1877   sx -= padding.left;
1878
1879   if (combo_box->priv->popup_fixed_width)
1880     gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1881   else
1882     gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1883
1884   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1885     *x = sx;
1886   else
1887     *x = sx + child_allocation.width - req.width;
1888   *y = sy;
1889
1890   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1891   monitor_num = gdk_screen_get_monitor_at_window (screen,
1892                                                   gtk_widget_get_window (GTK_WIDGET (combo_box)));
1893   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1894
1895   if (*x < monitor.x)
1896     *x = monitor.x;
1897   else if (*x + req.width > monitor.x + monitor.width)
1898     *x = monitor.x + monitor.width - req.width;
1899
1900   if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1901     *y += child_allocation.height;
1902   else if (*y - monitor.y >= req.height)
1903     *y -= req.height;
1904   else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1905     *y += child_allocation.height;
1906   else
1907     *y -= req.height;
1908
1909    *push_in = FALSE;
1910 }
1911
1912 static void
1913 gtk_combo_box_menu_position_over (GtkMenu  *menu,
1914                                   gint     *x,
1915                                   gint     *y,
1916                                   gboolean *push_in,
1917                                   gpointer  user_data)
1918 {
1919   GtkComboBox    *combo_box;
1920   GtkWidget      *active;
1921   GtkWidget      *child;
1922   GtkWidget      *widget;
1923   GtkAllocation   allocation;
1924   GtkAllocation   child_allocation;
1925   GList          *children;
1926   gint            screen_width;
1927   gint            menu_xpos;
1928   gint            menu_ypos;
1929   gint            menu_width;
1930
1931   combo_box = GTK_COMBO_BOX (user_data);
1932   widget = GTK_WIDGET (combo_box);
1933
1934   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1935
1936   gtk_widget_get_allocation (widget, &allocation);
1937
1938   menu_xpos = allocation.x;
1939   menu_ypos = allocation.y + allocation.height / 2 - 2;
1940
1941   if (combo_box->priv->popup_fixed_width)
1942     gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1943   else
1944     gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1945
1946   if (active != NULL)
1947     {
1948       gtk_widget_get_allocation (active, &child_allocation);
1949       menu_ypos -= child_allocation.height / 2;
1950     }
1951
1952   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1953   while (children)
1954     {
1955       child = children->data;
1956
1957       if (active == child)
1958         break;
1959
1960       if (gtk_widget_get_visible (child))
1961         {
1962           gtk_widget_get_allocation (child, &child_allocation);
1963
1964           menu_ypos -= child_allocation.height;
1965         }
1966
1967       children = children->next;
1968     }
1969
1970   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1971     menu_xpos = menu_xpos + allocation.width - menu_width;
1972
1973   gdk_window_get_root_coords (gtk_widget_get_window (widget),
1974                               menu_xpos, menu_ypos,
1975                               &menu_xpos, &menu_ypos);
1976
1977   /* Clamp the position on screen */
1978   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1979   
1980   if (menu_xpos < 0)
1981     menu_xpos = 0;
1982   else if ((menu_xpos + menu_width) > screen_width)
1983     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1984
1985   *x = menu_xpos;
1986   *y = menu_ypos;
1987
1988   *push_in = TRUE;
1989 }
1990
1991 static void
1992 gtk_combo_box_menu_position (GtkMenu  *menu,
1993                              gint     *x,
1994                              gint     *y,
1995                              gint     *push_in,
1996                              gpointer  user_data)
1997 {
1998   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1999   GtkComboBoxPrivate *priv = combo_box->priv;
2000   GtkWidget *menu_item;
2001
2002   if (priv->wrap_width > 0 || priv->cell_view == NULL)
2003     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2004   else
2005     {
2006       /* FIXME handle nested menus better */
2007       menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2008       if (menu_item)
2009         gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2010                                     menu_item);
2011
2012       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2013     }
2014
2015   if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2016     gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2017                               GDK_WINDOW_TYPE_HINT_COMBO);
2018 }
2019
2020 static void
2021 gtk_combo_box_list_position (GtkComboBox *combo_box,
2022                              gint        *x,
2023                              gint        *y,
2024                              gint        *width,
2025                              gint        *height)
2026 {
2027   GtkComboBoxPrivate *priv = combo_box->priv;
2028   GtkAllocation allocation;
2029   GdkScreen *screen;
2030   gint monitor_num;
2031   GdkRectangle monitor;
2032   GtkRequisition popup_req;
2033   GtkPolicyType hpolicy, vpolicy;
2034   GdkWindow *window;
2035
2036   /* under windows, the drop down list is as wide as the combo box itself.
2037      see bug #340204 */
2038   GtkWidget *widget = GTK_WIDGET (combo_box);
2039
2040   *x = *y = 0;
2041
2042   gtk_widget_get_allocation (widget, &allocation);
2043
2044   if (!gtk_widget_get_has_window (widget))
2045     {
2046       *x += allocation.x;
2047       *y += allocation.y;
2048     }
2049
2050   window = gtk_widget_get_window (widget);
2051
2052   gdk_window_get_root_coords (gtk_widget_get_window (widget),
2053                               *x, *y, x, y);
2054
2055   *width = allocation.width;
2056
2057   hpolicy = vpolicy = GTK_POLICY_NEVER;
2058   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2059                                   hpolicy, vpolicy);
2060
2061   if (combo_box->priv->popup_fixed_width)
2062     {
2063       gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2064
2065       if (popup_req.width > *width)
2066         {
2067           hpolicy = GTK_POLICY_ALWAYS;
2068           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2069                                           hpolicy, vpolicy);
2070         }
2071     }
2072   else
2073     {
2074       /* XXX This code depends on treeviews properly reporting their natural width
2075        * list-mode menus won't fill up to their natural width until then */
2076       gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2077
2078       if (popup_req.width > *width)
2079         {
2080           hpolicy = GTK_POLICY_NEVER;
2081           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2082                                           hpolicy, vpolicy);
2083
2084           *width = popup_req.width;
2085         }
2086     }
2087
2088   *height = popup_req.height;
2089
2090   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2091   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2092   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2093
2094   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2095     *x = *x + allocation.width - *width;
2096
2097   if (*x < monitor.x)
2098     *x = monitor.x;
2099   else if (*x + *width > monitor.x + monitor.width)
2100     *x = monitor.x + monitor.width - *width;
2101
2102   if (*y + allocation.height + *height <= monitor.y + monitor.height)
2103     *y += allocation.height;
2104   else if (*y - *height >= monitor.y)
2105     *y -= *height;
2106   else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2107     {
2108       *y += allocation.height;
2109       *height = monitor.y + monitor.height - *y;
2110     }
2111   else
2112     {
2113       *height = *y - monitor.y;
2114       *y = monitor.y;
2115     }
2116
2117   if (popup_req.height > *height)
2118     {
2119       vpolicy = GTK_POLICY_ALWAYS;
2120
2121       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2122                                       hpolicy, vpolicy);
2123     }
2124 }
2125
2126 static gboolean
2127 cell_layout_is_sensitive (GtkCellLayout *layout)
2128 {
2129   GList *cells, *list;
2130   gboolean sensitive;
2131
2132   cells = gtk_cell_layout_get_cells (layout);
2133
2134   sensitive = FALSE;
2135   for (list = cells; list; list = list->next)
2136     {
2137       g_object_get (list->data, "sensitive", &sensitive, NULL);
2138
2139       if (sensitive)
2140         break;
2141     }
2142   g_list_free (cells);
2143
2144   return sensitive;
2145 }
2146
2147 static gboolean
2148 cell_is_sensitive (GtkCellRenderer *cell,
2149                    gpointer         data)
2150 {
2151   gboolean *sensitive = data;
2152
2153   g_object_get (cell, "sensitive", sensitive, NULL);
2154
2155   return *sensitive;
2156 }
2157
2158 static gboolean
2159 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2160                               GtkTreeIter *iter)
2161 {
2162   GtkComboBoxPrivate *priv = combo_box->priv;
2163
2164   if (priv->row_separator_func)
2165     {
2166       if (priv->row_separator_func (priv->model, iter,
2167                                     priv->row_separator_data))
2168         return FALSE;
2169     }
2170
2171   if (priv->area)
2172     {
2173       gboolean sensitive;
2174
2175       gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2176
2177       sensitive = FALSE;
2178
2179       gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2180
2181       return sensitive;
2182     }
2183
2184   return TRUE;
2185 }
2186
2187 static void
2188 update_menu_sensitivity (GtkComboBox *combo_box,
2189                          GtkWidget   *menu)
2190 {
2191   GtkComboBoxPrivate *priv = combo_box->priv;
2192   GList *children, *child;
2193   GtkWidget *item, *submenu, *separator;
2194   GtkWidget *cell_view;
2195   gboolean sensitive;
2196
2197   if (!priv->model)
2198     return;
2199
2200   children = gtk_container_get_children (GTK_CONTAINER (menu));
2201
2202   for (child = children; child; child = child->next)
2203     {
2204       item = GTK_WIDGET (child->data);
2205       cell_view = gtk_bin_get_child (GTK_BIN (item));
2206
2207       if (!GTK_IS_CELL_VIEW (cell_view))
2208         continue;
2209
2210       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2211       if (submenu != NULL)
2212         {
2213           gtk_widget_set_sensitive (item, TRUE);
2214           update_menu_sensitivity (combo_box, submenu);
2215         }
2216       else
2217         {
2218           sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2219
2220           if (menu != priv->popup_widget && child == children)
2221             {
2222               separator = GTK_WIDGET (child->next->data);
2223               g_object_set (item, "visible", sensitive, NULL);
2224               g_object_set (separator, "visible", sensitive, NULL);
2225             }
2226           else
2227             gtk_widget_set_sensitive (item, sensitive);
2228         }
2229     }
2230
2231   g_list_free (children);
2232 }
2233
2234 static void
2235 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2236                           guint        button,
2237                           guint32      activate_time)
2238 {
2239   GtkComboBoxPrivate *priv = combo_box->priv;
2240   GtkTreePath *path;
2241   gint active_item;
2242   gint width, min_width, nat_width;
2243
2244   update_menu_sensitivity (combo_box, priv->popup_widget);
2245
2246   active_item = -1;
2247   if (gtk_tree_row_reference_valid (priv->active_row))
2248     {
2249       path = gtk_tree_row_reference_get_path (priv->active_row);
2250       active_item = gtk_tree_path_get_indices (path)[0];
2251       gtk_tree_path_free (path);
2252
2253       if (priv->add_tearoffs)
2254         active_item++;
2255     }
2256
2257   /* FIXME handle nested menus better */
2258   gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2259
2260   if (priv->wrap_width == 0)
2261     {
2262       GtkAllocation allocation;
2263
2264       gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2265       width = allocation.width;
2266       gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2267       gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2268
2269       if (combo_box->priv->popup_fixed_width)
2270         width = MAX (width, min_width);
2271       else
2272         width = MAX (width, nat_width);
2273
2274       gtk_widget_set_size_request (priv->popup_widget, width, -1);
2275     }
2276
2277   gtk_menu_popup (GTK_MENU (priv->popup_widget),
2278                   NULL, NULL,
2279                   gtk_combo_box_menu_position, combo_box,
2280                   button, activate_time);
2281 }
2282
2283 static gboolean
2284 popup_grab_on_window (GdkWindow *window,
2285                       GdkDevice *keyboard,
2286                       GdkDevice *pointer,
2287                       guint32    activate_time)
2288 {
2289   if (keyboard &&
2290       gdk_device_grab (keyboard, window,
2291                        GDK_OWNERSHIP_WINDOW, TRUE,
2292                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2293                        NULL, activate_time) != GDK_GRAB_SUCCESS)
2294     return FALSE;
2295
2296   if (pointer &&
2297       gdk_device_grab (pointer, window,
2298                        GDK_OWNERSHIP_WINDOW, TRUE,
2299                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2300                        GDK_POINTER_MOTION_MASK,
2301                        NULL, activate_time) != GDK_GRAB_SUCCESS)
2302     {
2303       if (keyboard)
2304         gdk_device_ungrab (keyboard, activate_time);
2305
2306       return FALSE;
2307     }
2308
2309   return TRUE;
2310 }
2311
2312 /**
2313  * gtk_combo_box_popup:
2314  * @combo_box: a #GtkComboBox
2315  *
2316  * Pops up the menu or dropdown list of @combo_box.
2317  *
2318  * This function is mostly intended for use by accessibility technologies;
2319  * applications should have little use for it.
2320  *
2321  * Since: 2.4
2322  */
2323 void
2324 gtk_combo_box_popup (GtkComboBox *combo_box)
2325 {
2326   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2327
2328   g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2329 }
2330
2331 /**
2332  * gtk_combo_box_popup_for_device:
2333  * @combo_box: a #GtkComboBox
2334  * @device: a #GdkDevice
2335  *
2336  * Pops up the menu or dropdown list of @combo_box, the popup window
2337  * will be grabbed so only @device and its associated pointer/keyboard
2338  * are the only #GdkDevice<!-- -->s able to send events to it.
2339  *
2340  * Since: 3.0
2341  **/
2342 void
2343 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2344                                 GdkDevice   *device)
2345 {
2346   GtkComboBoxPrivate *priv = combo_box->priv;
2347   gint x, y, width, height;
2348   GtkTreePath *path = NULL, *ppath;
2349   GtkWidget *toplevel;
2350   GdkDevice *keyboard, *pointer;
2351   guint32 time;
2352
2353   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2354   g_return_if_fail (GDK_IS_DEVICE (device));
2355
2356   if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2357     return;
2358
2359   if (gtk_widget_get_mapped (priv->popup_widget))
2360     return;
2361
2362   if (priv->grab_pointer && priv->grab_keyboard)
2363     return;
2364
2365   time = gtk_get_current_event_time ();
2366
2367   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2368     {
2369       keyboard = device;
2370       pointer = gdk_device_get_associated_device (device);
2371     }
2372   else
2373     {
2374       pointer = device;
2375       keyboard = gdk_device_get_associated_device (device);
2376     }
2377
2378   if (GTK_IS_MENU (priv->popup_widget))
2379     {
2380       gtk_combo_box_menu_popup (combo_box,
2381                                 priv->activate_button,
2382                                 priv->activate_time);
2383       return;
2384     }
2385
2386   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2387   if (GTK_IS_WINDOW (toplevel))
2388     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2389                                  GTK_WINDOW (priv->popup_window));
2390
2391   gtk_widget_show_all (priv->scrolled_window);
2392   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2393
2394   gtk_widget_set_size_request (priv->popup_window, width, height);
2395   gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2396
2397   if (gtk_tree_row_reference_valid (priv->active_row))
2398     {
2399       path = gtk_tree_row_reference_get_path (priv->active_row);
2400       ppath = gtk_tree_path_copy (path);
2401       if (gtk_tree_path_up (ppath))
2402         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2403                                       ppath);
2404       gtk_tree_path_free (ppath);
2405     }
2406   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2407                                   TRUE);
2408
2409   /* popup */
2410   gtk_widget_show (priv->popup_window);
2411
2412   if (path)
2413     {
2414       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2415                                 path, NULL, FALSE);
2416       gtk_tree_path_free (path);
2417     }
2418
2419   gtk_widget_grab_focus (priv->popup_window);
2420   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2421                                 TRUE);
2422
2423   if (!gtk_widget_has_focus (priv->tree_view))
2424     gtk_widget_grab_focus (priv->tree_view);
2425
2426   if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2427                              keyboard, pointer, time))
2428     {
2429       gtk_widget_hide (priv->popup_window);
2430       return;
2431     }
2432
2433   gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2434   priv->grab_pointer = pointer;
2435   priv->grab_keyboard = keyboard;
2436 }
2437
2438 static void
2439 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2440 {
2441   GdkDevice *device;
2442
2443   device = gtk_get_current_event_device ();
2444
2445   if (!device)
2446     {
2447       GdkDeviceManager *device_manager;
2448       GdkDisplay *display;
2449       GList *devices;
2450
2451       display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2452       device_manager = gdk_display_get_device_manager (display);
2453
2454       /* No device was set, pick the first master device */
2455       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2456       device = devices->data;
2457       g_list_free (devices);
2458     }
2459
2460   gtk_combo_box_popup_for_device (combo_box, device);
2461 }
2462
2463 static gboolean
2464 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2465 {
2466   if (combo_box->priv->popup_shown)
2467     {
2468       gtk_combo_box_popdown (combo_box);
2469       return TRUE;
2470     }
2471
2472   return FALSE;
2473 }
2474
2475 /**
2476  * gtk_combo_box_popdown:
2477  * @combo_box: a #GtkComboBox
2478  *
2479  * Hides the menu or dropdown list of @combo_box.
2480  *
2481  * This function is mostly intended for use by accessibility technologies;
2482  * applications should have little use for it.
2483  *
2484  * Since: 2.4
2485  */
2486 void
2487 gtk_combo_box_popdown (GtkComboBox *combo_box)
2488 {
2489   GtkComboBoxPrivate *priv = combo_box->priv;
2490
2491   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2492
2493   if (GTK_IS_MENU (priv->popup_widget))
2494     {
2495       gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2496       return;
2497     }
2498
2499   if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2500     return;
2501
2502   gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2503   gtk_widget_hide (priv->popup_window);
2504   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2505                                 FALSE);
2506
2507   priv->grab_pointer = NULL;
2508   priv->grab_keyboard = NULL;
2509 }
2510
2511 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON                                         \
2512   gtk_widget_get_preferred_size (combo_box->priv->button,                        \
2513                                  &req, NULL);                                         \
2514                                                                                   \
2515   if (is_rtl)                                                                         \
2516     child.x = allocation->x + padding.right;                                        \
2517   else                                                                                \
2518     child.x = allocation->x + allocation->width - req.width - padding.left;        \
2519                                                                                     \
2520   child.y = allocation->y + padding.top;                                                \
2521   child.width = req.width;                                                        \
2522   child.height = allocation->height - (padding.top + padding.bottom);                \
2523   child.width = MAX (1, child.width);                                                \
2524   child.height = MAX (1, child.height);                                                \
2525                                                                                   \
2526   gtk_widget_size_allocate (combo_box->priv->button, &child);
2527
2528
2529 static void
2530 gtk_combo_box_size_allocate (GtkWidget     *widget,
2531                              GtkAllocation *allocation)
2532 {
2533   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2534   GtkComboBoxPrivate *priv = combo_box->priv;
2535   GtkWidget *child_widget;
2536   gint focus_width, focus_pad;
2537   GtkAllocation child;
2538   GtkRequisition req;
2539   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2540   GtkBorder padding;
2541
2542   gtk_widget_set_allocation (widget, allocation);
2543   child_widget = gtk_bin_get_child (GTK_BIN (widget));
2544   get_widget_padding (widget, &padding);
2545
2546   gtk_widget_style_get (widget,
2547                         "focus-line-width", &focus_width,
2548                         "focus-padding", &focus_pad,
2549                         NULL);
2550
2551   if (!priv->tree_view)
2552     {
2553       if (priv->cell_view)
2554         {
2555           GtkBorder button_padding;
2556           gint width;
2557           guint border_width;
2558
2559           gtk_widget_size_allocate (priv->button, allocation);
2560
2561           /* menu mode */
2562           allocation->x += padding.left;
2563           allocation->y += padding.top;
2564           allocation->width -= padding.left + padding.right;
2565           allocation->height -= padding.top + padding.bottom;
2566
2567           /* set some things ready */
2568           border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2569           get_widget_padding (priv->button, &button_padding);
2570
2571           child.x = allocation->x;
2572           child.y = allocation->y;
2573           width = allocation->width;
2574           child.height = allocation->height;
2575
2576           if (!priv->is_cell_renderer)
2577             {
2578               child.x += border_width + button_padding.left + focus_width + focus_pad;
2579               child.y += border_width + button_padding.top + focus_width + focus_pad;
2580               width -= (2 * (border_width + focus_width + focus_pad)) +
2581                 button_padding.left + button_padding.right;
2582               child.height -= (2 * (border_width + focus_width + focus_pad)) +
2583                 button_padding.top + button_padding.bottom;
2584             }
2585
2586           /* handle the children */
2587           gtk_widget_get_preferred_size (priv->arrow, &req, NULL);
2588           child.width = req.width;
2589           if (!is_rtl)
2590             child.x += width - req.width - button_padding.right;
2591           child.width = MAX (1, child.width);
2592           child.height = MAX (1, child.height);
2593           gtk_widget_size_allocate (priv->arrow, &child);
2594           if (is_rtl)
2595             child.x += req.width;
2596           gtk_widget_get_preferred_size (priv->separator, &req, NULL);
2597           child.width = req.width;
2598           if (!is_rtl)
2599             child.x -= req.width;
2600           child.width = MAX (1, child.width);
2601           child.height = MAX (1, child.height);
2602           gtk_widget_size_allocate (priv->separator, &child);
2603
2604           if (is_rtl)
2605             {
2606               child.x += req.width;
2607               child.width = allocation->x + allocation->width
2608                 - (border_width + focus_width + focus_pad)
2609                 - child.x;
2610             }
2611           else
2612             {
2613               child.width = child.x;
2614               child.x = allocation->x
2615                 + border_width + button_padding.left + focus_width + focus_pad;
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           GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2651
2652           if (is_rtl)
2653             child.x = allocation->x + req.width + padding.right;
2654           else
2655             child.x = allocation->x + padding.left;
2656           child.y = allocation->y + padding.top;
2657           child.width = allocation->width - req.width - (padding.left + padding.right);
2658           child.width = MAX (1, child.width);
2659           child.height = MAX (1, child.height);
2660           gtk_widget_size_allocate (child_widget, &child);
2661         }
2662     }
2663   else
2664     {
2665       /* list mode */
2666       guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2667
2668       /* button */
2669       GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2670
2671       /* frame */
2672       if (is_rtl)
2673         child.x = allocation->x + req.width;
2674       else
2675         child.x = allocation->x;
2676
2677       child.y = allocation->y;
2678       child.width = allocation->width - req.width;
2679       child.height = allocation->height;
2680
2681       if (priv->cell_view_frame)
2682         {
2683           child.x += padding.left + border_width;
2684           child.y += padding.top + border_width;
2685           child.width = MAX (1, child.width - (2 * border_width) - (padding.left + padding.right));
2686           child.height = MAX (1, child.height - (2 * border_width) - (padding.top + padding.bottom));
2687           gtk_widget_size_allocate (priv->cell_view_frame, &child);
2688
2689           /* the sample */
2690           if (priv->has_frame)
2691             {
2692               GtkBorder frame_padding;
2693
2694               border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2695               get_widget_padding (priv->cell_view_frame, &frame_padding);
2696
2697               child.x += border_width + frame_padding.left;
2698               child.y += border_width + frame_padding.right;
2699               child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2700               child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2701             }
2702         }
2703       else
2704         {
2705           child.x += padding.left + border_width;
2706           child.y += padding.top + border_width;
2707           child.width -= (2 * border_width) - (padding.left + padding.right);
2708           child.height -= (2 * border_width) - (padding.top + padding.bottom);
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       child.width = MAX (1, child.width);
2720       child.height = MAX (1, child.height);
2721       gtk_widget_size_allocate (child_widget, &child);
2722     }
2723 }
2724
2725 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2726
2727 static void
2728 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2729 {
2730   GtkComboBoxPrivate *priv = combo_box->priv;
2731
2732   if (priv->model)
2733     {
2734       g_signal_handler_disconnect (priv->model,
2735                                    priv->inserted_id);
2736       g_signal_handler_disconnect (priv->model,
2737                                    priv->deleted_id);
2738       g_signal_handler_disconnect (priv->model,
2739                                    priv->reordered_id);
2740       g_signal_handler_disconnect (priv->model,
2741                                    priv->changed_id);
2742     }
2743
2744   if (priv->model)
2745     {
2746       g_object_unref (priv->model);
2747       priv->model = NULL;
2748     }
2749
2750   if (priv->active_row)
2751     {
2752       gtk_tree_row_reference_free (priv->active_row);
2753       priv->active_row = NULL;
2754     }
2755
2756   if (priv->cell_view)
2757     gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2758 }
2759
2760 static void
2761 gtk_combo_box_forall (GtkContainer *container,
2762                       gboolean      include_internals,
2763                       GtkCallback   callback,
2764                       gpointer      callback_data)
2765 {
2766   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2767   GtkComboBoxPrivate *priv = combo_box->priv;
2768   GtkWidget *child;
2769
2770   if (include_internals)
2771     {
2772       if (priv->button)
2773         (* callback) (priv->button, callback_data);
2774       if (priv->cell_view_frame)
2775         (* callback) (priv->cell_view_frame, callback_data);
2776     }
2777
2778   child = gtk_bin_get_child (GTK_BIN (container));
2779   if (child)
2780     (* callback) (child, callback_data);
2781 }
2782
2783 static void
2784 gtk_combo_box_child_show (GtkWidget *widget,
2785                           GtkComboBox *combo_box)
2786 {
2787   GtkComboBoxPrivate *priv = combo_box->priv;
2788
2789   priv->popup_shown = TRUE;
2790   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2791 }
2792
2793 static void
2794 gtk_combo_box_child_hide (GtkWidget *widget,
2795                           GtkComboBox *combo_box)
2796 {
2797   GtkComboBoxPrivate *priv = combo_box->priv;
2798
2799   priv->popup_shown = FALSE;
2800   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2801 }
2802
2803 static gboolean
2804 gtk_combo_box_draw (GtkWidget *widget,
2805                     cairo_t   *cr)
2806 {
2807   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2808   GtkComboBoxPrivate *priv = combo_box->priv;
2809
2810   if (priv->shadow_type != GTK_SHADOW_NONE)
2811     {
2812       GtkStyleContext *context;
2813       GtkStateFlags state;
2814
2815       context = gtk_widget_get_style_context (widget);
2816       state = gtk_widget_get_state_flags (widget);
2817       gtk_style_context_set_state (context, state);
2818
2819       gtk_render_background (context, cr, 0, 0,
2820                              gtk_widget_get_allocated_width (widget),
2821                              gtk_widget_get_allocated_height (widget));
2822       gtk_render_frame (context, cr, 0, 0,
2823                         gtk_widget_get_allocated_width (widget),
2824                         gtk_widget_get_allocated_height (widget));
2825     }
2826
2827   gtk_container_propagate_draw (GTK_CONTAINER (widget),
2828                                 priv->button, cr);
2829
2830   if (priv->tree_view && priv->cell_view_frame)
2831     {
2832       gtk_container_propagate_draw (GTK_CONTAINER (widget),
2833                                     priv->cell_view_frame, cr);
2834     }
2835
2836   gtk_container_propagate_draw (GTK_CONTAINER (widget),
2837                                 gtk_bin_get_child (GTK_BIN (widget)),
2838                                 cr);
2839
2840   return FALSE;
2841 }
2842
2843 typedef struct {
2844   GtkComboBox *combo;
2845   GtkTreePath *path;
2846   GtkTreeIter iter;
2847   gboolean found;
2848   gboolean set;
2849   gboolean visible;
2850 } SearchData;
2851
2852 static gboolean
2853 path_visible (GtkTreeView *view,
2854               GtkTreePath *path)
2855 {
2856   GtkRBTree *tree;
2857   GtkRBNode *node;
2858
2859   /* Note that we rely on the fact that collapsed rows don't have nodes
2860    */
2861   return _gtk_tree_view_find_node (view, path, &tree, &node);
2862 }
2863
2864 static gboolean
2865 tree_next_func (GtkTreeModel *model,
2866                 GtkTreePath  *path,
2867                 GtkTreeIter  *iter,
2868                 gpointer      data)
2869 {
2870   SearchData *search_data = (SearchData *)data;
2871
2872   if (search_data->found)
2873     {
2874       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2875         return FALSE;
2876
2877       if (search_data->visible &&
2878           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2879         return FALSE;
2880
2881       search_data->set = TRUE;
2882       search_data->iter = *iter;
2883
2884       return TRUE;
2885     }
2886
2887   if (gtk_tree_path_compare (path, search_data->path) == 0)
2888     search_data->found = TRUE;
2889
2890   return FALSE;
2891 }
2892
2893 static gboolean
2894 tree_next (GtkComboBox  *combo,
2895            GtkTreeModel *model,
2896            GtkTreeIter  *iter,
2897            GtkTreeIter  *next,
2898            gboolean      visible)
2899 {
2900   SearchData search_data;
2901
2902   search_data.combo = combo;
2903   search_data.path = gtk_tree_model_get_path (model, iter);
2904   search_data.visible = visible;
2905   search_data.found = FALSE;
2906   search_data.set = FALSE;
2907
2908   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2909
2910   *next = search_data.iter;
2911
2912   gtk_tree_path_free (search_data.path);
2913
2914   return search_data.set;
2915 }
2916
2917 static gboolean
2918 tree_prev_func (GtkTreeModel *model,
2919                 GtkTreePath  *path,
2920                 GtkTreeIter  *iter,
2921                 gpointer      data)
2922 {
2923   SearchData *search_data = (SearchData *)data;
2924
2925   if (gtk_tree_path_compare (path, search_data->path) == 0)
2926     {
2927       search_data->found = TRUE;
2928       return TRUE;
2929     }
2930
2931   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2932     return FALSE;
2933
2934   if (search_data->visible &&
2935       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2936     return FALSE;
2937
2938   search_data->set = TRUE;
2939   search_data->iter = *iter;
2940
2941   return FALSE;
2942 }
2943
2944 static gboolean
2945 tree_prev (GtkComboBox  *combo,
2946            GtkTreeModel *model,
2947            GtkTreeIter  *iter,
2948            GtkTreeIter  *prev,
2949            gboolean      visible)
2950 {
2951   SearchData search_data;
2952
2953   search_data.combo = combo;
2954   search_data.path = gtk_tree_model_get_path (model, iter);
2955   search_data.visible = visible;
2956   search_data.found = FALSE;
2957   search_data.set = FALSE;
2958
2959   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2960
2961   *prev = search_data.iter;
2962
2963   gtk_tree_path_free (search_data.path);
2964
2965   return search_data.set;
2966 }
2967
2968 static gboolean
2969 tree_last_func (GtkTreeModel *model,
2970                 GtkTreePath  *path,
2971                 GtkTreeIter  *iter,
2972                 gpointer      data)
2973 {
2974   SearchData *search_data = (SearchData *)data;
2975
2976   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2977     return FALSE;
2978
2979   /* Note that we rely on the fact that collapsed rows don't have nodes
2980    */
2981   if (search_data->visible &&
2982       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2983     return FALSE;
2984
2985   search_data->set = TRUE;
2986   search_data->iter = *iter;
2987
2988   return FALSE;
2989 }
2990
2991 static gboolean
2992 tree_last (GtkComboBox  *combo,
2993            GtkTreeModel *model,
2994            GtkTreeIter  *last,
2995            gboolean      visible)
2996 {
2997   SearchData search_data;
2998
2999   search_data.combo = combo;
3000   search_data.visible = visible;
3001   search_data.set = FALSE;
3002
3003   gtk_tree_model_foreach (model, tree_last_func, &search_data);
3004
3005   *last = search_data.iter;
3006
3007   return search_data.set;
3008 }
3009
3010
3011 static gboolean
3012 tree_first_func (GtkTreeModel *model,
3013                  GtkTreePath  *path,
3014                  GtkTreeIter  *iter,
3015                  gpointer      data)
3016 {
3017   SearchData *search_data = (SearchData *)data;
3018
3019   if (!tree_column_row_is_sensitive (search_data->combo, iter))
3020     return FALSE;
3021
3022   if (search_data->visible &&
3023       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3024     return FALSE;
3025
3026   search_data->set = TRUE;
3027   search_data->iter = *iter;
3028
3029   return TRUE;
3030 }
3031
3032 static gboolean
3033 tree_first (GtkComboBox  *combo,
3034             GtkTreeModel *model,
3035             GtkTreeIter  *first,
3036             gboolean      visible)
3037 {
3038   SearchData search_data;
3039
3040   search_data.combo = combo;
3041   search_data.visible = visible;
3042   search_data.set = FALSE;
3043
3044   gtk_tree_model_foreach (model, tree_first_func, &search_data);
3045
3046   *first = search_data.iter;
3047
3048   return search_data.set;
3049 }
3050
3051 static gboolean
3052 gtk_combo_box_scroll_event (GtkWidget          *widget,
3053                             GdkEventScroll     *event)
3054 {
3055   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3056   gboolean found;
3057   GtkTreeIter iter;
3058   GtkTreeIter new_iter;
3059
3060   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3061     return TRUE;
3062
3063   if (event->direction == GDK_SCROLL_UP)
3064     found = tree_prev (combo_box, combo_box->priv->model,
3065                        &iter, &new_iter, FALSE);
3066   else
3067     found = tree_next (combo_box, combo_box->priv->model,
3068                        &iter, &new_iter, FALSE);
3069
3070   if (found)
3071     gtk_combo_box_set_active_iter (combo_box, &new_iter);
3072
3073   return TRUE;
3074 }
3075
3076 /*
3077  * menu style
3078  */
3079 static gboolean
3080 gtk_combo_box_row_separator_func (GtkTreeModel      *model,
3081                                   GtkTreeIter       *iter,
3082                                   GtkComboBox       *combo)
3083 {
3084   GtkComboBoxPrivate *priv = combo->priv;
3085
3086   if (priv->row_separator_func)
3087     return priv->row_separator_func (model, iter, priv->row_separator_data);
3088
3089   return FALSE;
3090 }
3091
3092 static gboolean
3093 gtk_combo_box_header_func (GtkTreeModel      *model,
3094                            GtkTreeIter       *iter,
3095                            GtkComboBox       *combo)
3096 {
3097   /* Every submenu has a selectable header, however we
3098    * can expose a method to make that configurable by
3099    * the user (like row_separator_func is done) */
3100   return TRUE;
3101 }
3102
3103 static void
3104 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3105                           gboolean     add_children)
3106 {
3107   GtkComboBoxPrivate *priv = combo_box->priv;
3108   GtkWidget *child;
3109   GtkWidget *menu;
3110
3111   child = gtk_bin_get_child (GTK_BIN (combo_box));
3112
3113   if (priv->cell_view)
3114     {
3115       priv->button = gtk_toggle_button_new ();
3116       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3117                                      priv->focus_on_click);
3118
3119       g_signal_connect (priv->button, "toggled",
3120                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3121       gtk_widget_set_parent (priv->button,
3122                              gtk_widget_get_parent (child));
3123
3124       priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3125       gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3126
3127       priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3128       gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3129
3130       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3131       gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3132
3133       gtk_widget_show_all (priv->button);
3134     }
3135   else
3136     {
3137       priv->button = gtk_toggle_button_new ();
3138       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3139                                      priv->focus_on_click);
3140
3141       g_signal_connect (priv->button, "toggled",
3142                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3143       gtk_widget_set_parent (priv->button,
3144                              gtk_widget_get_parent (child));
3145
3146       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3147       gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3148       gtk_widget_show_all (priv->button);
3149     }
3150
3151   g_signal_connect (priv->button, "button-press-event",
3152                     G_CALLBACK (gtk_combo_box_menu_button_press),
3153                     combo_box);
3154   g_signal_connect (priv->button, "state-flags-changed",
3155                     G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3156                     combo_box);
3157
3158   /* create our funky menu */
3159   menu = _gtk_tree_menu_new_with_area (priv->area);
3160   gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3161
3162   _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3163
3164   _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3165   _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3166   _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3167   _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3168                               combo_box->priv->add_tearoffs);
3169
3170   g_signal_connect (menu, "menu-activate",
3171                     G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3172
3173   /* Chain our row_separator_func through */
3174   _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3175                                          (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3176                                          combo_box, NULL);
3177
3178   _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3179                                   (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3180                                   combo_box, NULL);
3181
3182   g_signal_connect (menu, "key-press-event",
3183                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3184   gtk_combo_box_set_popup_widget (combo_box, menu);
3185
3186   gtk_combo_box_update_title (combo_box);
3187 }
3188
3189 static void
3190 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3191 {
3192   GtkComboBoxPrivate *priv = combo_box->priv;
3193
3194   g_signal_handlers_disconnect_matched (priv->button,
3195                                         G_SIGNAL_MATCH_DATA,
3196                                         0, 0, NULL,
3197                                         gtk_combo_box_menu_button_press, NULL);
3198   g_signal_handlers_disconnect_matched (priv->button,
3199                                         G_SIGNAL_MATCH_DATA,
3200                                         0, 0, NULL,
3201                                         gtk_combo_box_button_state_flags_changed, combo_box);
3202   g_signal_handlers_disconnect_matched (priv->popup_widget,
3203                                         G_SIGNAL_MATCH_DATA,
3204                                         0, 0, NULL,
3205                                         gtk_combo_box_menu_activate, combo_box);
3206
3207   /* unparent will remove our latest ref */
3208   gtk_widget_unparent (priv->button);
3209
3210   priv->box = NULL;
3211   priv->button = NULL;
3212   priv->arrow = NULL;
3213   priv->separator = NULL;
3214
3215   /* changing the popup window will unref the menu and the children */
3216 }
3217
3218 /* callbacks */
3219 static gboolean
3220 gtk_combo_box_menu_button_press (GtkWidget      *widget,
3221                                  GdkEventButton *event,
3222                                  gpointer        user_data)
3223 {
3224   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3225   GtkComboBoxPrivate *priv = combo_box->priv;
3226
3227   if (GTK_IS_MENU (priv->popup_widget) &&
3228       event->type == GDK_BUTTON_PRESS && event->button == 1)
3229     {
3230       if (priv->focus_on_click &&
3231           !gtk_widget_has_focus (priv->button))
3232         gtk_widget_grab_focus (priv->button);
3233
3234       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3235
3236       return TRUE;
3237     }
3238
3239   return FALSE;
3240 }
3241
3242 static void
3243 gtk_combo_box_menu_activate (GtkWidget   *menu,
3244                              const gchar *path,
3245                              GtkComboBox *combo_box)
3246 {
3247   GtkTreeIter iter;
3248
3249   if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3250     gtk_combo_box_set_active_iter (combo_box, &iter);
3251
3252   g_object_set (combo_box,
3253                 "editing-canceled", FALSE,
3254                 NULL);
3255 }
3256
3257
3258 static void
3259 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3260 {
3261   GtkTreeIter iter;
3262   gboolean sensitive = TRUE; /* fool code checkers */
3263
3264   if (!combo_box->priv->button)
3265     return;
3266
3267   switch (combo_box->priv->button_sensitivity)
3268     {
3269       case GTK_SENSITIVITY_ON:
3270         sensitive = TRUE;
3271         break;
3272       case GTK_SENSITIVITY_OFF:
3273         sensitive = FALSE;
3274         break;
3275       case GTK_SENSITIVITY_AUTO:
3276         sensitive = combo_box->priv->model &&
3277                     gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3278         break;
3279       default:
3280         g_assert_not_reached ();
3281         break;
3282     }
3283
3284   gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3285
3286   /* In list-mode, we also need to update sensitivity of the event box */
3287   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3288       && combo_box->priv->cell_view)
3289     gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3290 }
3291
3292 static void
3293 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
3294                                   GtkTreePath      *path,
3295                                   GtkTreeIter      *iter,
3296                                   gpointer          user_data)
3297 {
3298   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3299
3300   if (combo_box->priv->tree_view)
3301     gtk_combo_box_list_popup_resize (combo_box);
3302
3303   gtk_combo_box_update_sensitivity (combo_box);
3304 }
3305
3306 static void
3307 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
3308                                  GtkTreePath      *path,
3309                                  gpointer          user_data)
3310 {
3311   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3312   GtkComboBoxPrivate *priv = combo_box->priv;
3313
3314   if (!gtk_tree_row_reference_valid (priv->active_row))
3315     {
3316       if (priv->cell_view)
3317         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3318       g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3319     }
3320   
3321   if (priv->tree_view)
3322     gtk_combo_box_list_popup_resize (combo_box);
3323
3324   gtk_combo_box_update_sensitivity (combo_box);
3325 }
3326
3327 static void
3328 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
3329                                     GtkTreePath     *path,
3330                                     GtkTreeIter     *iter,
3331                                     gint            *new_order,
3332                                     gpointer         user_data)
3333 {
3334   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3335 }
3336
3337 static void
3338 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
3339                                  GtkTreePath      *path,
3340                                  GtkTreeIter      *iter,
3341                                  gpointer          user_data)
3342 {
3343   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3344   GtkComboBoxPrivate *priv = combo_box->priv;
3345   GtkTreePath *active_path;
3346
3347   /* FIXME this belongs to GtkCellView */
3348   if (gtk_tree_row_reference_valid (priv->active_row))
3349     {
3350       active_path = gtk_tree_row_reference_get_path (priv->active_row);
3351       if (gtk_tree_path_compare (path, active_path) == 0 &&
3352           priv->cell_view)
3353         gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3354       gtk_tree_path_free (active_path);
3355     }
3356
3357   if (priv->tree_view)
3358     gtk_combo_box_list_row_changed (model, path, iter, user_data);
3359 }
3360
3361 static gboolean
3362 list_popup_resize_idle (gpointer user_data)
3363 {
3364   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3365   GtkComboBoxPrivate *priv = combo_box->priv;
3366   gint x, y, width, height;
3367
3368   if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3369     {
3370       gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3371
3372       gtk_widget_set_size_request (priv->popup_window, width, height);
3373       gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3374     }
3375
3376   priv->resize_idle_id = 0;
3377
3378   return FALSE;
3379 }
3380
3381 static void
3382 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3383 {
3384   GtkComboBoxPrivate *priv = combo_box->priv;
3385
3386   if (!priv->resize_idle_id)
3387     priv->resize_idle_id =
3388       gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3389 }
3390
3391 static void
3392 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
3393                                   GtkTreePath      *path,
3394                                   GtkTreeIter      *iter,
3395                                   gpointer          user_data)
3396 {
3397   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3398
3399   gtk_combo_box_list_popup_resize (combo_box);
3400 }
3401
3402
3403 /*
3404  * list style
3405  */
3406
3407 static void
3408 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3409 {
3410   GtkComboBoxPrivate *priv = combo_box->priv;
3411   GtkTreeSelection *sel;
3412   GtkWidget *child;
3413   GtkWidget *widget = GTK_WIDGET (combo_box);
3414
3415   priv->button = gtk_toggle_button_new ();
3416   child = gtk_bin_get_child (GTK_BIN (combo_box));
3417   gtk_widget_set_parent (priv->button,
3418                          gtk_widget_get_parent (child));
3419   g_signal_connect (priv->button, "button-press-event",
3420                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3421   g_signal_connect (priv->button, "toggled",
3422                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3423
3424   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3425   gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3426   priv->separator = NULL;
3427   gtk_widget_show_all (priv->button);
3428
3429   if (priv->cell_view)
3430     {
3431       GtkStyleContext *context;
3432       GtkStateFlags state;
3433       GdkRGBA color;
3434
3435       context  = gtk_widget_get_style_context (widget);
3436       state = gtk_widget_get_state_flags (widget);
3437       gtk_style_context_get_background_color (context, state, &color);
3438
3439       gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3440
3441       priv->box = gtk_event_box_new ();
3442       gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3443                                         FALSE);
3444
3445       if (priv->has_frame)
3446         {
3447           priv->cell_view_frame = gtk_frame_new (NULL);
3448           gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3449                                      GTK_SHADOW_IN);
3450         }
3451       else
3452         {
3453           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3454           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3455                                             FALSE);
3456         }
3457
3458       gtk_widget_set_parent (priv->cell_view_frame,
3459                              gtk_widget_get_parent (child));
3460       gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3461       gtk_widget_show_all (priv->cell_view_frame);
3462
3463       g_signal_connect (priv->box, "button-press-event",
3464                         G_CALLBACK (gtk_combo_box_list_button_pressed),
3465                         combo_box);
3466     }
3467
3468   priv->tree_view = gtk_tree_view_new ();
3469   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3470   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3471   gtk_tree_selection_set_select_function (sel,
3472                                           gtk_combo_box_list_select_func,
3473                                           NULL, NULL);
3474   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3475                                      FALSE);
3476   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3477                                      TRUE);
3478
3479   gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3480                                         (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3481                                         combo_box, NULL);
3482
3483   if (priv->model)
3484     gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3485
3486   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3487                                gtk_tree_view_column_new_with_area (priv->area));
3488
3489   if (gtk_tree_row_reference_valid (priv->active_row))
3490     {
3491       GtkTreePath *path;
3492
3493       path = gtk_tree_row_reference_get_path (priv->active_row);
3494       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3495                                 path, NULL, FALSE);
3496       gtk_tree_path_free (path);
3497     }
3498
3499   /* set sample/popup widgets */
3500   gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3501
3502   g_signal_connect (priv->tree_view, "key-press-event",
3503                     G_CALLBACK (gtk_combo_box_list_key_press),
3504                     combo_box);
3505   g_signal_connect (priv->tree_view, "enter-notify-event",
3506                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3507                     combo_box);
3508   g_signal_connect (priv->tree_view, "row-expanded",
3509                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3510                     combo_box);
3511   g_signal_connect (priv->tree_view, "row-collapsed",
3512                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3513                     combo_box);
3514   g_signal_connect (priv->popup_window, "button-press-event",
3515                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3516                     combo_box);
3517   g_signal_connect (priv->popup_window, "button-release-event",
3518                     G_CALLBACK (gtk_combo_box_list_button_released),
3519                     combo_box);
3520
3521   gtk_widget_show (priv->tree_view);
3522
3523   gtk_combo_box_update_sensitivity (combo_box);
3524 }
3525
3526 static void
3527 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3528 {
3529   GtkComboBoxPrivate *priv = combo_box->priv;
3530
3531   /* disconnect signals */
3532   g_signal_handlers_disconnect_matched (priv->tree_view,
3533                                         G_SIGNAL_MATCH_DATA,
3534                                         0, 0, NULL, NULL, combo_box);
3535   g_signal_handlers_disconnect_matched (priv->button,
3536                                         G_SIGNAL_MATCH_DATA,
3537                                         0, 0, NULL,
3538                                         gtk_combo_box_list_button_pressed,
3539                                         NULL);
3540   g_signal_handlers_disconnect_matched (priv->popup_window,
3541                                         G_SIGNAL_MATCH_DATA,
3542                                         0, 0, NULL,
3543                                         gtk_combo_box_list_button_pressed,
3544                                         NULL);
3545   g_signal_handlers_disconnect_matched (priv->popup_window,
3546                                         G_SIGNAL_MATCH_DATA,
3547                                         0, 0, NULL,
3548                                         gtk_combo_box_list_button_released,
3549                                         NULL);
3550
3551   g_signal_handlers_disconnect_matched (priv->popup_window,
3552                                         G_SIGNAL_MATCH_DATA,
3553                                         0, 0, NULL,
3554                                         gtk_combo_box_child_show,
3555                                         NULL);
3556
3557   g_signal_handlers_disconnect_matched (priv->popup_window,
3558                                         G_SIGNAL_MATCH_DATA,
3559                                         0, 0, NULL,
3560                                         gtk_combo_box_child_hide,
3561                                         NULL);
3562
3563   if (priv->box)
3564     g_signal_handlers_disconnect_matched (priv->box,
3565                                           G_SIGNAL_MATCH_DATA,
3566                                           0, 0, NULL,
3567                                           gtk_combo_box_list_button_pressed,
3568                                           NULL);
3569
3570   /* destroy things (unparent will kill the latest ref from us)
3571    * last unref on button will destroy the arrow
3572    */
3573   gtk_widget_unparent (priv->button);
3574   priv->button = NULL;
3575   priv->arrow = NULL;
3576
3577   if (priv->cell_view)
3578     {
3579       g_object_set (priv->cell_view,
3580                     "background-set", FALSE,
3581                     NULL);
3582     }
3583
3584   if (priv->cell_view_frame)
3585     {
3586       gtk_widget_unparent (priv->cell_view_frame);
3587       priv->cell_view_frame = NULL;
3588       priv->box = NULL;
3589     }
3590
3591   if (priv->scroll_timer)
3592     {
3593       g_source_remove (priv->scroll_timer);
3594       priv->scroll_timer = 0;
3595     }
3596
3597   if (priv->resize_idle_id)
3598     {
3599       g_source_remove (priv->resize_idle_id);
3600       priv->resize_idle_id = 0;
3601     }
3602
3603   gtk_widget_destroy (priv->tree_view);
3604
3605   priv->tree_view = NULL;
3606   if (priv->popup_widget)
3607     {
3608       g_object_unref (priv->popup_widget);
3609       priv->popup_widget = NULL;
3610     }
3611 }
3612
3613 /* callbacks */
3614
3615 static gboolean
3616 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3617                                    GdkEventButton *event,
3618                                    gpointer        data)
3619 {
3620   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3621   GtkComboBoxPrivate *priv = combo_box->priv;
3622
3623   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3624
3625   if (ewidget == priv->popup_window)
3626     return TRUE;
3627
3628   if ((ewidget != priv->button && ewidget != priv->box) ||
3629       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3630     return FALSE;
3631
3632   if (priv->focus_on_click &&
3633       !gtk_widget_has_focus (priv->button))
3634     gtk_widget_grab_focus (priv->button);
3635
3636   gtk_combo_box_popup_for_device (combo_box, event->device);
3637
3638   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3639
3640   priv->auto_scroll = FALSE;
3641   if (priv->scroll_timer == 0)
3642     priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3643                                                   (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3644                                                    combo_box);
3645
3646   priv->popup_in_progress = TRUE;
3647
3648   return TRUE;
3649 }
3650
3651 static gboolean
3652 gtk_combo_box_list_button_released (GtkWidget      *widget,
3653                                     GdkEventButton *event,
3654                                     gpointer        data)
3655 {
3656   gboolean ret;
3657   GtkTreePath *path = NULL;
3658   GtkTreeIter iter;
3659
3660   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3661   GtkComboBoxPrivate *priv = combo_box->priv;
3662
3663   gboolean popup_in_progress = FALSE;
3664
3665   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3666
3667   if (priv->popup_in_progress)
3668     {
3669       popup_in_progress = TRUE;
3670       priv->popup_in_progress = FALSE;
3671     }
3672
3673   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3674                                   FALSE);
3675   if (priv->scroll_timer)
3676     {
3677       g_source_remove (priv->scroll_timer);
3678       priv->scroll_timer = 0;
3679     }
3680
3681   if (ewidget != priv->tree_view)
3682     {
3683       if ((ewidget == priv->button ||
3684            ewidget == priv->box) &&
3685           !popup_in_progress &&
3686           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3687         {
3688           gtk_combo_box_popdown (combo_box);
3689           return TRUE;
3690         }
3691
3692       /* released outside treeview */
3693       if (ewidget != priv->button && ewidget != priv->box)
3694         {
3695           gtk_combo_box_popdown (combo_box);
3696
3697           return TRUE;
3698         }
3699
3700       return FALSE;
3701     }
3702
3703   /* select something cool */
3704   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3705                                        event->x, event->y,
3706                                        &path,
3707                                        NULL, NULL, NULL);
3708
3709   if (!ret)
3710     return TRUE; /* clicked outside window? */
3711
3712   gtk_tree_model_get_iter (priv->model, &iter, path);
3713   gtk_tree_path_free (path);
3714
3715   gtk_combo_box_popdown (combo_box);
3716
3717   if (tree_column_row_is_sensitive (combo_box, &iter))
3718     gtk_combo_box_set_active_iter (combo_box, &iter);
3719
3720   return TRUE;
3721 }
3722
3723 static gboolean
3724 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3725                               GdkEventKey *event,
3726                               gpointer     data)
3727 {
3728   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3729
3730   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3731     {
3732       /* The menu hasn't managed the
3733        * event, forward it to the combobox
3734        */
3735       gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3736     }
3737
3738   return TRUE;
3739 }
3740
3741 static gboolean
3742 gtk_combo_box_list_key_press (GtkWidget   *widget,
3743                               GdkEventKey *event,
3744                               gpointer     data)
3745 {
3746   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3747   GtkTreeIter iter;
3748
3749   if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3750       event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3751   {
3752     GtkTreeModel *model = NULL;
3753
3754     gtk_combo_box_popdown (combo_box);
3755
3756     if (combo_box->priv->model)
3757       {
3758         GtkTreeSelection *sel;
3759
3760         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3761
3762         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3763           gtk_combo_box_set_active_iter (combo_box, &iter);
3764       }
3765
3766     return TRUE;
3767   }
3768
3769   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3770     {
3771       /* The list hasn't managed the
3772        * event, forward it to the combobox
3773        */
3774       gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3775     }
3776
3777   return TRUE;
3778 }
3779
3780 static void
3781 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3782                                 gint         x,
3783                                 gint         y)
3784 {
3785   GtkAdjustment *adj;
3786   GtkAllocation allocation;
3787   GtkWidget *tree_view = combo_box->priv->tree_view;
3788   gdouble value;
3789
3790   gtk_widget_get_allocation (tree_view, &allocation);
3791
3792   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3793   if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3794     {
3795       if (x <= allocation.x &&
3796           gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3797         {
3798           value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3799           gtk_adjustment_set_value (adj, value);
3800         }
3801       else if (x >= allocation.x + allocation.width &&
3802                gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3803         {
3804           value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3805           gtk_adjustment_set_value (adj, MAX (value, 0.0));
3806         }
3807     }
3808
3809   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3810   if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3811     {
3812       if (y <= allocation.y &&
3813           gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3814         {
3815           value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3816           gtk_adjustment_set_value (adj, value);
3817         }
3818       else if (y >= allocation.height &&
3819                gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3820         {
3821           value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3822           gtk_adjustment_set_value (adj, MAX (value, 0.0));
3823         }
3824     }
3825 }
3826
3827 static gboolean
3828 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3829 {
3830   GtkComboBoxPrivate *priv = combo_box->priv;
3831   gint x, y;
3832
3833   if (priv->auto_scroll)
3834     {
3835       gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3836                                       priv->grab_pointer,
3837                                       &x, &y, NULL);
3838       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3839     }
3840
3841   return TRUE;
3842 }
3843
3844 static gboolean
3845 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3846                                  GdkEventCrossing *event,
3847                                  gpointer          data)
3848 {
3849   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3850
3851   combo_box->priv->auto_scroll = TRUE;
3852
3853   return TRUE;
3854 }
3855
3856 static gboolean
3857 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3858                                 GtkTreeModel     *model,
3859                                 GtkTreePath      *path,
3860                                 gboolean          path_currently_selected,
3861                                 gpointer          data)
3862 {
3863   GList *list, *columns;
3864   gboolean sensitive = FALSE;
3865
3866   columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3867
3868   for (list = columns; list && !sensitive; list = list->next)
3869     {
3870       GList *cells, *cell;
3871       gboolean cell_sensitive, cell_visible;
3872       GtkTreeIter iter;
3873       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3874
3875       if (!gtk_tree_view_column_get_visible (column))
3876         continue;
3877
3878       gtk_tree_model_get_iter (model, &iter, path);
3879       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3880                                                FALSE, FALSE);
3881
3882       cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3883       while (cell)
3884         {
3885           g_object_get (cell->data,
3886                         "sensitive", &cell_sensitive,
3887                         "visible", &cell_visible,
3888                         NULL);
3889
3890           if (cell_visible && cell_sensitive)
3891             break;
3892
3893           cell = cell->next;
3894         }
3895       g_list_free (cells);
3896
3897       sensitive = cell_sensitive;
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                   focus_width, focus_pad;
5432   gint                   font_size, arrow_size;
5433   PangoContext          *context;
5434   PangoFontMetrics      *metrics;
5435   const PangoFontDescription *font_desc;
5436   GtkWidget             *child;
5437   gint                   minimum_width = 0, natural_width = 0;
5438   gint                   child_min, child_nat;
5439   GtkStyleContext       *style_context;
5440   GtkStateFlags          state;
5441   GtkBorder              padding;
5442   gfloat                 arrow_scaling;
5443
5444   child = gtk_bin_get_child (GTK_BIN (widget));
5445
5446   /* common */
5447   gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5448
5449   gtk_widget_style_get (GTK_WIDGET (widget),
5450                         "focus-line-width", &focus_width,
5451                         "focus-padding", &focus_pad,
5452                         "arrow-size", &arrow_size,
5453                         "arrow-scaling", &arrow_scaling,
5454                         NULL);
5455
5456   style_context = gtk_widget_get_style_context (widget);
5457   state = gtk_widget_get_state_flags (widget);
5458
5459   get_widget_padding (widget, &padding);
5460   font_desc = gtk_style_context_get_font (style_context, state);
5461
5462   context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5463   metrics = pango_context_get_metrics (context, font_desc,
5464                                        pango_context_get_language (context));
5465   font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5466                             pango_font_metrics_get_descent (metrics));
5467   pango_font_metrics_unref (metrics);
5468
5469   arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5470
5471   gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5472
5473   if (!priv->tree_view)
5474     {
5475       /* menu mode */
5476       if (priv->cell_view)
5477         {
5478           gint sep_width, arrow_width;
5479           gint border_width, xpad;
5480           GtkBorder button_padding;
5481
5482           border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5483           get_widget_padding (priv->button, &button_padding);
5484
5485           gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5486           gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5487
5488           xpad = 2 * (border_width + focus_width + focus_pad) +
5489             button_padding.left + button_padding.right + padding.left + padding.right;
5490
5491           minimum_width  = child_min + sep_width + arrow_width + xpad;
5492           natural_width  = child_nat + sep_width + arrow_width + xpad;
5493         }
5494       else
5495         {
5496           gint but_width, but_nat_width;
5497
5498           gtk_widget_get_preferred_width (priv->button,
5499                                           &but_width, &but_nat_width);
5500
5501           minimum_width  = child_min + but_width;
5502           natural_width  = child_nat + but_nat_width;
5503         }
5504     }
5505   else
5506     {
5507       /* list mode */
5508       gint button_width, button_nat_width;
5509
5510       /* sample + frame */
5511       minimum_width = child_min;
5512       natural_width = child_nat;
5513
5514       minimum_width += 2 * focus_width;
5515       natural_width += 2 * focus_width;
5516
5517       if (priv->cell_view_frame)
5518         {
5519           if (priv->has_frame)
5520             {
5521               gint border_width, xpad;
5522               GtkBorder frame_padding;
5523
5524               border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5525               get_widget_padding (priv->cell_view_frame, &frame_padding);
5526               xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5527
5528               minimum_width  += xpad;
5529               natural_width  += xpad;
5530             }
5531         }
5532
5533       /* the button */
5534       gtk_widget_get_preferred_width (priv->button,
5535                                       &button_width, &button_nat_width);
5536
5537       minimum_width += button_width;
5538       natural_width += button_nat_width;
5539     }
5540
5541   minimum_width += padding.left + padding.right;
5542   natural_width += padding.left + padding.right;
5543
5544   if (minimum_size)
5545     *minimum_size = minimum_width;
5546
5547   if (natural_size)
5548     *natural_size = natural_width;
5549 }
5550
5551 static void
5552 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5553                                     gint      *minimum_size,
5554                                     gint      *natural_size)
5555 {
5556   gint min_width;
5557
5558   /* Combo box is height-for-width only
5559    * (so we always just reserve enough height for the minimum width) */
5560   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5561   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5562 }
5563
5564 static void
5565 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5566                                               gint       avail_size,
5567                                               gint      *minimum_size,
5568                                               gint      *natural_size)
5569 {
5570   /* Combo box is height-for-width only
5571    * (so we assume we always reserved enough height for the minimum width) */
5572   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5573 }
5574
5575
5576 static void
5577 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5578                                               gint       avail_size,
5579                                               gint      *minimum_size,
5580                                               gint      *natural_size)
5581 {
5582   GtkComboBox           *combo_box = GTK_COMBO_BOX (widget);
5583   GtkComboBoxPrivate    *priv = combo_box->priv;
5584   gint                   focus_width, focus_pad;
5585   gint                   min_height = 0, nat_height = 0;
5586   gint                   size;
5587   GtkWidget             *child;
5588   GtkBorder              padding;
5589
5590   gtk_widget_style_get (GTK_WIDGET (widget),
5591                         "focus-line-width", &focus_width,
5592                         "focus-padding", &focus_pad,
5593                         NULL);
5594
5595   child = gtk_bin_get_child (GTK_BIN (widget));
5596
5597   get_widget_padding (widget, &padding);
5598   size = avail_size;
5599
5600   if (!priv->tree_view)
5601     {
5602       /* menu mode */
5603       if (priv->cell_view)
5604         {
5605           /* calculate x/y padding and separator/arrow size */
5606           gint sep_width, arrow_width, sep_height, arrow_height;
5607           gint border_width, xpad, ypad;
5608           GtkBorder button_padding;
5609
5610           border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5611           get_widget_padding (priv->button, &button_padding);
5612
5613           gtk_widget_get_preferred_width (priv->separator, &sep_width, NULL);
5614           gtk_widget_get_preferred_width (priv->arrow, &arrow_width, NULL);
5615           gtk_widget_get_preferred_height_for_width (priv->separator,
5616                                                      sep_width, &sep_height, NULL);
5617           gtk_widget_get_preferred_height_for_width (priv->arrow,
5618                                                      arrow_width, &arrow_height, NULL);
5619
5620           xpad = 2 * (border_width + focus_width + focus_pad) +
5621             button_padding.left + button_padding.right;
5622           ypad = 2 * (border_width + focus_width + focus_pad) +
5623             button_padding.top + button_padding.bottom;
5624
5625           size -= sep_width + arrow_width + xpad;
5626
5627           /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5628            * and fitting the whole treemodel */
5629           gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5630
5631           arrow_height = MAX (arrow_height, sep_height);
5632           min_height = MAX (min_height, arrow_height);
5633           nat_height = MAX (nat_height, arrow_height);
5634
5635           min_height += ypad;
5636           nat_height += ypad;
5637         }
5638       else
5639         {
5640           /* there is a custom child widget inside (no priv->cell_view) */
5641           gint but_width, but_height;
5642
5643           gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5644           gtk_widget_get_preferred_height_for_width (priv->button,
5645                                                      but_width, &but_height, NULL);
5646
5647           size -= but_width;
5648
5649           /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5650            * and fitting the whole treemodel */
5651           gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5652
5653           min_height = MAX (min_height, but_height);
5654           nat_height = MAX (nat_height, but_height);
5655         }
5656     }
5657   else
5658     {
5659       /* list mode */
5660       gint but_width, but_height;
5661       gint xpad = 0, ypad = 0;
5662
5663       gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5664       gtk_widget_get_preferred_height_for_width (priv->button,
5665                                                  but_width, &but_height, NULL);
5666
5667       if (priv->cell_view_frame && priv->has_frame)
5668         {
5669           GtkBorder frame_padding;
5670           gint border_width;
5671
5672           border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5673           get_widget_padding (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5674
5675           xpad = (2 * border_width) + padding.left + frame_padding.right;
5676           ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5677         }
5678
5679       size -= but_width;
5680       size -= 2 * focus_width;
5681       size -= xpad;
5682
5683       /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5684        * and fitting the whole treemodel */
5685       gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5686
5687       min_height = MAX (min_height, but_height);
5688       nat_height = MAX (nat_height, but_height);
5689
5690       min_height += ypad;
5691       nat_height += ypad;
5692     }
5693
5694   min_height += padding.top + padding.bottom;
5695   nat_height += padding.top + padding.bottom;
5696
5697   if (minimum_size)
5698     *minimum_size = min_height;
5699
5700   if (natural_size)
5701     *natural_size = nat_height;
5702 }
5703
5704 /**
5705  * gtk_combo_box_set_id_column:
5706  * @combo_box: A #GtkComboBox
5707  * @id_column: A column in @model to get string IDs for values from
5708  *
5709  * Sets the model column which @combo_box should use to get string IDs
5710  * for values from. The column @id_column in the model of @combo_box
5711  * must be of type %G_TYPE_STRING.
5712  *
5713  * Since: 3.0
5714  */
5715 void
5716 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5717                              gint         id_column)
5718 {
5719   GtkComboBoxPrivate *priv = combo_box->priv;
5720   GtkTreeModel *model;
5721
5722   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5723
5724   if (id_column != priv->id_column)
5725     {
5726       model = gtk_combo_box_get_model (combo_box);
5727
5728       g_return_if_fail (id_column >= 0);
5729       g_return_if_fail (model == NULL ||
5730                         id_column < gtk_tree_model_get_n_columns (model));
5731
5732       priv->id_column = id_column;
5733
5734       g_object_notify (G_OBJECT (combo_box), "id-column");
5735       g_object_notify (G_OBJECT (combo_box), "active-id");
5736     }
5737 }
5738
5739 /**
5740  * gtk_combo_box_get_id_column:
5741  * @combo_box: A #GtkComboBox
5742  *
5743  * Returns the column which @combo_box is using to get string IDs
5744  * for values from.
5745  *
5746  * Return value: A column in the data source model of @combo_box.
5747  *
5748  * Since: 3.0
5749  */
5750 gint
5751 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5752 {
5753   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5754
5755   return combo_box->priv->id_column;
5756 }
5757
5758 /**
5759  * gtk_combo_box_get_active_id:
5760  * @combo_box: a #GtkComboBox
5761  *
5762  * Returns the ID of the active row of @combo_box.  This value is taken
5763  * from the active row and the column specified by the #GtkComboBox:id-column
5764  * property of @combo_box (see gtk_combo_box_set_id_column()).
5765  *
5766  * The returned value is an interned string which means that you can
5767  * compare the pointer by value to other interned strings and that you
5768  * must not free it.
5769  *
5770  * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5771  * no row is active, or if the active row has a %NULL ID value, then %NULL
5772  * is returned.
5773  *
5774  * Return value: the ID of the active row, or %NULL
5775  *
5776  * Since: 3.0
5777  **/
5778 const gchar *
5779 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5780 {
5781   GtkTreeModel *model;
5782   GtkTreeIter iter;
5783   gint column;
5784
5785   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5786
5787   column = combo_box->priv->id_column;
5788
5789   if (column < 0)
5790     return NULL;
5791
5792   model = gtk_combo_box_get_model (combo_box);
5793   g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5794                         G_TYPE_STRING, NULL);
5795
5796   if (gtk_combo_box_get_active_iter (combo_box, &iter))
5797     {
5798       const gchar *interned;
5799       gchar *id;
5800
5801       gtk_tree_model_get (model, &iter, column, &id, -1);
5802       interned = g_intern_string (id);
5803       g_free (id);
5804
5805       return interned;
5806     }
5807
5808   return NULL;
5809 }
5810
5811 /**
5812  * gtk_combo_box_set_active_id:
5813  * @combo_box: a #GtkComboBox
5814  * @active_id: (allow-none): the ID of the row to select, or %NULL
5815  *
5816  * Changes the active row of @combo_box to the one that has an ID equal to
5817  * @active_id, or unsets the active row if @active_id is %NULL.  Rows having
5818  * a %NULL ID string cannot be made active by this function.
5819  *
5820  * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5821  * row has the given ID then the function does nothing and returns %FALSE.
5822  *
5823  * Returns: %TRUE if a row with a matching ID was found.  If a %NULL
5824  *          @active_id was given to unset the active row, the function
5825  *          always returns %TRUE.
5826  *
5827  * Since: 3.0
5828  **/
5829 gboolean
5830 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5831                              const gchar *active_id)
5832 {
5833   GtkTreeModel *model;
5834   GtkTreeIter iter;
5835   gboolean match = FALSE;
5836   gint column;
5837
5838   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5839
5840   if (active_id == NULL)
5841     {
5842       gtk_combo_box_set_active (combo_box, -1);
5843       return TRUE;  /* active row was successfully unset */
5844     }
5845
5846   column = combo_box->priv->id_column;
5847
5848   if (column < 0)
5849     return FALSE;
5850
5851   model = gtk_combo_box_get_model (combo_box);
5852   g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5853                         G_TYPE_STRING, FALSE);
5854
5855   if (gtk_tree_model_get_iter_first (model, &iter))
5856     do {
5857       gchar *id;
5858
5859       gtk_tree_model_get (model, &iter, column, &id, -1);
5860       if (id != NULL)
5861         match = strcmp (id, active_id) == 0;
5862       g_free (id);
5863
5864       if (match)
5865         {
5866           gtk_combo_box_set_active_iter (combo_box, &iter);
5867           break;
5868         }
5869     } while (gtk_tree_model_iter_next (model, &iter));
5870
5871     return match;
5872 }