]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
combobox: always trim the allocated area by padding and border for child
[~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_and_border (GtkWidget *widget,
1833                                GtkBorder *padding)
1834 {
1835   GtkStyleContext *context;
1836   GtkStateFlags state;
1837   GtkBorder tmp;
1838
1839   context = gtk_widget_get_style_context (widget);
1840   state = gtk_style_context_get_state (context);
1841
1842   gtk_style_context_get_padding (context, state, padding);
1843   gtk_style_context_get_border (context, state, &tmp);
1844
1845   padding->top += tmp.top;
1846   padding->right += tmp.right;
1847   padding->bottom += tmp.bottom;
1848   padding->left += tmp.left;
1849 }
1850
1851 static void
1852 gtk_combo_box_menu_position_below (GtkMenu  *menu,
1853                                    gint     *x,
1854                                    gint     *y,
1855                                    gint     *push_in,
1856                                    gpointer  user_data)
1857 {
1858   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1859   GtkAllocation child_allocation;
1860   gint sx, sy;
1861   GtkWidget *child;
1862   GtkRequisition req;
1863   GdkScreen *screen;
1864   gint monitor_num;
1865   GdkRectangle monitor;
1866   GtkBorder padding;
1867
1868   /* FIXME: is using the size request here broken? */
1869   child = gtk_bin_get_child (GTK_BIN (combo_box));
1870
1871   sx = sy = 0;
1872
1873   gtk_widget_get_allocation (child, &child_allocation);
1874
1875   if (!gtk_widget_get_has_window (child))
1876     {
1877       sx += child_allocation.x;
1878       sy += child_allocation.y;
1879     }
1880
1881   gdk_window_get_root_coords (gtk_widget_get_window (child),
1882                               sx, sy, &sx, &sy);
1883   get_widget_padding_and_border (GTK_WIDGET (combo_box), &padding);
1884   sx -= padding.left;
1885
1886   if (combo_box->priv->popup_fixed_width)
1887     gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
1888   else
1889     gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1890
1891   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1892     *x = sx;
1893   else
1894     *x = sx + child_allocation.width - req.width;
1895   *y = sy;
1896
1897   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1898   monitor_num = gdk_screen_get_monitor_at_window (screen,
1899                                                   gtk_widget_get_window (GTK_WIDGET (combo_box)));
1900   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
1901
1902   if (*x < monitor.x)
1903     *x = monitor.x;
1904   else if (*x + req.width > monitor.x + monitor.width)
1905     *x = monitor.x + monitor.width - req.width;
1906
1907   if (monitor.y + monitor.height - *y - child_allocation.height >= req.height)
1908     *y += child_allocation.height;
1909   else if (*y - monitor.y >= req.height)
1910     *y -= req.height;
1911   else if (monitor.y + monitor.height - *y - child_allocation.height > *y - monitor.y)
1912     *y += child_allocation.height;
1913   else
1914     *y -= req.height;
1915
1916    *push_in = FALSE;
1917 }
1918
1919 static void
1920 gtk_combo_box_menu_position_over (GtkMenu  *menu,
1921                                   gint     *x,
1922                                   gint     *y,
1923                                   gboolean *push_in,
1924                                   gpointer  user_data)
1925 {
1926   GtkComboBox    *combo_box;
1927   GtkWidget      *active;
1928   GtkWidget      *child;
1929   GtkWidget      *widget;
1930   GtkAllocation   allocation;
1931   GtkAllocation   child_allocation;
1932   GList          *children;
1933   gint            screen_width;
1934   gint            menu_xpos;
1935   gint            menu_ypos;
1936   gint            menu_width;
1937
1938   combo_box = GTK_COMBO_BOX (user_data);
1939   widget = GTK_WIDGET (combo_box);
1940
1941   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1942
1943   gtk_widget_get_allocation (widget, &allocation);
1944
1945   menu_xpos = allocation.x;
1946   menu_ypos = allocation.y + allocation.height / 2 - 2;
1947
1948   if (combo_box->priv->popup_fixed_width)
1949     gtk_widget_get_preferred_width (GTK_WIDGET (menu), &menu_width, NULL);
1950   else
1951     gtk_widget_get_preferred_width (GTK_WIDGET (menu), NULL, &menu_width);
1952
1953   if (active != NULL)
1954     {
1955       gtk_widget_get_allocation (active, &child_allocation);
1956       menu_ypos -= child_allocation.height / 2;
1957     }
1958
1959   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->priv->children;
1960   while (children)
1961     {
1962       child = children->data;
1963
1964       if (active == child)
1965         break;
1966
1967       if (gtk_widget_get_visible (child))
1968         {
1969           gtk_widget_get_allocation (child, &child_allocation);
1970
1971           menu_ypos -= child_allocation.height;
1972         }
1973
1974       children = children->next;
1975     }
1976
1977   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1978     menu_xpos = menu_xpos + allocation.width - menu_width;
1979
1980   gdk_window_get_root_coords (gtk_widget_get_window (widget),
1981                               menu_xpos, menu_ypos,
1982                               &menu_xpos, &menu_ypos);
1983
1984   /* Clamp the position on screen */
1985   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1986   
1987   if (menu_xpos < 0)
1988     menu_xpos = 0;
1989   else if ((menu_xpos + menu_width) > screen_width)
1990     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1991
1992   *x = menu_xpos;
1993   *y = menu_ypos;
1994
1995   *push_in = TRUE;
1996 }
1997
1998 static void
1999 gtk_combo_box_menu_position (GtkMenu  *menu,
2000                              gint     *x,
2001                              gint     *y,
2002                              gint     *push_in,
2003                              gpointer  user_data)
2004 {
2005   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2006   GtkComboBoxPrivate *priv = combo_box->priv;
2007   GtkWidget *menu_item;
2008
2009   if (priv->wrap_width > 0 || priv->cell_view == NULL)
2010     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
2011   else
2012     {
2013       /* FIXME handle nested menus better */
2014       menu_item = gtk_menu_get_active (GTK_MENU (priv->popup_widget));
2015       if (menu_item)
2016         gtk_menu_shell_select_item (GTK_MENU_SHELL (priv->popup_widget),
2017                                     menu_item);
2018
2019       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
2020     }
2021
2022   if (!gtk_widget_get_visible (GTK_MENU (priv->popup_widget)->priv->toplevel))
2023     gtk_window_set_type_hint (GTK_WINDOW (GTK_MENU (priv->popup_widget)->priv->toplevel),
2024                               GDK_WINDOW_TYPE_HINT_COMBO);
2025 }
2026
2027 static void
2028 gtk_combo_box_list_position (GtkComboBox *combo_box,
2029                              gint        *x,
2030                              gint        *y,
2031                              gint        *width,
2032                              gint        *height)
2033 {
2034   GtkComboBoxPrivate *priv = combo_box->priv;
2035   GtkAllocation allocation;
2036   GdkScreen *screen;
2037   gint monitor_num;
2038   GdkRectangle monitor;
2039   GtkRequisition popup_req;
2040   GtkPolicyType hpolicy, vpolicy;
2041   GdkWindow *window;
2042
2043   /* under windows, the drop down list is as wide as the combo box itself.
2044      see bug #340204 */
2045   GtkWidget *widget = GTK_WIDGET (combo_box);
2046
2047   *x = *y = 0;
2048
2049   gtk_widget_get_allocation (widget, &allocation);
2050
2051   if (!gtk_widget_get_has_window (widget))
2052     {
2053       *x += allocation.x;
2054       *y += allocation.y;
2055     }
2056
2057   window = gtk_widget_get_window (widget);
2058
2059   gdk_window_get_root_coords (gtk_widget_get_window (widget),
2060                               *x, *y, x, y);
2061
2062   *width = allocation.width;
2063
2064   hpolicy = vpolicy = GTK_POLICY_NEVER;
2065   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2066                                   hpolicy, vpolicy);
2067
2068   if (combo_box->priv->popup_fixed_width)
2069     {
2070       gtk_widget_get_preferred_size (priv->scrolled_window, &popup_req, NULL);
2071
2072       if (popup_req.width > *width)
2073         {
2074           hpolicy = GTK_POLICY_ALWAYS;
2075           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2076                                           hpolicy, vpolicy);
2077         }
2078     }
2079   else
2080     {
2081       /* XXX This code depends on treeviews properly reporting their natural width
2082        * list-mode menus won't fill up to their natural width until then */
2083       gtk_widget_get_preferred_size (priv->scrolled_window, NULL, &popup_req);
2084
2085       if (popup_req.width > *width)
2086         {
2087           hpolicy = GTK_POLICY_NEVER;
2088           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2089                                           hpolicy, vpolicy);
2090
2091           *width = popup_req.width;
2092         }
2093     }
2094
2095   *height = popup_req.height;
2096
2097   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
2098   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
2099   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2100
2101   if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
2102     *x = *x + allocation.width - *width;
2103
2104   if (*x < monitor.x)
2105     *x = monitor.x;
2106   else if (*x + *width > monitor.x + monitor.width)
2107     *x = monitor.x + monitor.width - *width;
2108
2109   if (*y + allocation.height + *height <= monitor.y + monitor.height)
2110     *y += allocation.height;
2111   else if (*y - *height >= monitor.y)
2112     *y -= *height;
2113   else if (monitor.y + monitor.height - (*y + allocation.height) > *y - monitor.y)
2114     {
2115       *y += allocation.height;
2116       *height = monitor.y + monitor.height - *y;
2117     }
2118   else
2119     {
2120       *height = *y - monitor.y;
2121       *y = monitor.y;
2122     }
2123
2124   if (popup_req.height > *height)
2125     {
2126       vpolicy = GTK_POLICY_ALWAYS;
2127
2128       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
2129                                       hpolicy, vpolicy);
2130     }
2131 }
2132
2133 static gboolean
2134 cell_layout_is_sensitive (GtkCellLayout *layout)
2135 {
2136   GList *cells, *list;
2137   gboolean sensitive;
2138
2139   cells = gtk_cell_layout_get_cells (layout);
2140
2141   sensitive = FALSE;
2142   for (list = cells; list; list = list->next)
2143     {
2144       g_object_get (list->data, "sensitive", &sensitive, NULL);
2145
2146       if (sensitive)
2147         break;
2148     }
2149   g_list_free (cells);
2150
2151   return sensitive;
2152 }
2153
2154 static gboolean
2155 cell_is_sensitive (GtkCellRenderer *cell,
2156                    gpointer         data)
2157 {
2158   gboolean *sensitive = data;
2159
2160   g_object_get (cell, "sensitive", sensitive, NULL);
2161
2162   return *sensitive;
2163 }
2164
2165 static gboolean
2166 tree_column_row_is_sensitive (GtkComboBox *combo_box,
2167                               GtkTreeIter *iter)
2168 {
2169   GtkComboBoxPrivate *priv = combo_box->priv;
2170
2171   if (priv->row_separator_func)
2172     {
2173       if (priv->row_separator_func (priv->model, iter,
2174                                     priv->row_separator_data))
2175         return FALSE;
2176     }
2177
2178   if (priv->area)
2179     {
2180       gboolean sensitive;
2181
2182       gtk_cell_area_apply_attributes (priv->area, priv->model, iter, FALSE, FALSE);
2183
2184       sensitive = FALSE;
2185
2186       gtk_cell_area_foreach (priv->area, cell_is_sensitive, &sensitive);
2187
2188       return sensitive;
2189     }
2190
2191   return TRUE;
2192 }
2193
2194 static void
2195 update_menu_sensitivity (GtkComboBox *combo_box,
2196                          GtkWidget   *menu)
2197 {
2198   GtkComboBoxPrivate *priv = combo_box->priv;
2199   GList *children, *child;
2200   GtkWidget *item, *submenu, *separator;
2201   GtkWidget *cell_view;
2202   gboolean sensitive;
2203
2204   if (!priv->model)
2205     return;
2206
2207   children = gtk_container_get_children (GTK_CONTAINER (menu));
2208
2209   for (child = children; child; child = child->next)
2210     {
2211       item = GTK_WIDGET (child->data);
2212       cell_view = gtk_bin_get_child (GTK_BIN (item));
2213
2214       if (!GTK_IS_CELL_VIEW (cell_view))
2215         continue;
2216
2217       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
2218       if (submenu != NULL)
2219         {
2220           gtk_widget_set_sensitive (item, TRUE);
2221           update_menu_sensitivity (combo_box, submenu);
2222         }
2223       else
2224         {
2225           sensitive = cell_layout_is_sensitive (GTK_CELL_LAYOUT (cell_view));
2226
2227           if (menu != priv->popup_widget && child == children)
2228             {
2229               separator = GTK_WIDGET (child->next->data);
2230               g_object_set (item, "visible", sensitive, NULL);
2231               g_object_set (separator, "visible", sensitive, NULL);
2232             }
2233           else
2234             gtk_widget_set_sensitive (item, sensitive);
2235         }
2236     }
2237
2238   g_list_free (children);
2239 }
2240
2241 static void
2242 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
2243                           guint        button,
2244                           guint32      activate_time)
2245 {
2246   GtkComboBoxPrivate *priv = combo_box->priv;
2247   GtkTreePath *path;
2248   gint active_item;
2249   gint width, min_width, nat_width;
2250
2251   update_menu_sensitivity (combo_box, priv->popup_widget);
2252
2253   active_item = -1;
2254   if (gtk_tree_row_reference_valid (priv->active_row))
2255     {
2256       path = gtk_tree_row_reference_get_path (priv->active_row);
2257       active_item = gtk_tree_path_get_indices (path)[0];
2258       gtk_tree_path_free (path);
2259
2260       if (priv->add_tearoffs)
2261         active_item++;
2262     }
2263
2264   /* FIXME handle nested menus better */
2265   gtk_menu_set_active (GTK_MENU (priv->popup_widget), active_item);
2266
2267   if (priv->wrap_width == 0)
2268     {
2269       GtkAllocation allocation;
2270
2271       gtk_widget_get_allocation (GTK_WIDGET (combo_box), &allocation);
2272       width = allocation.width;
2273       gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2274       gtk_widget_get_preferred_width (priv->popup_widget, &min_width, &nat_width);
2275
2276       if (combo_box->priv->popup_fixed_width)
2277         width = MAX (width, min_width);
2278       else
2279         width = MAX (width, nat_width);
2280
2281       gtk_widget_set_size_request (priv->popup_widget, width, -1);
2282     }
2283
2284   gtk_menu_popup (GTK_MENU (priv->popup_widget),
2285                   NULL, NULL,
2286                   gtk_combo_box_menu_position, combo_box,
2287                   button, activate_time);
2288 }
2289
2290 static gboolean
2291 popup_grab_on_window (GdkWindow *window,
2292                       GdkDevice *keyboard,
2293                       GdkDevice *pointer,
2294                       guint32    activate_time)
2295 {
2296   if (keyboard &&
2297       gdk_device_grab (keyboard, window,
2298                        GDK_OWNERSHIP_WINDOW, TRUE,
2299                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2300                        NULL, activate_time) != GDK_GRAB_SUCCESS)
2301     return FALSE;
2302
2303   if (pointer &&
2304       gdk_device_grab (pointer, window,
2305                        GDK_OWNERSHIP_WINDOW, TRUE,
2306                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2307                        GDK_POINTER_MOTION_MASK,
2308                        NULL, activate_time) != GDK_GRAB_SUCCESS)
2309     {
2310       if (keyboard)
2311         gdk_device_ungrab (keyboard, activate_time);
2312
2313       return FALSE;
2314     }
2315
2316   return TRUE;
2317 }
2318
2319 /**
2320  * gtk_combo_box_popup:
2321  * @combo_box: a #GtkComboBox
2322  *
2323  * Pops up the menu or dropdown list of @combo_box.
2324  *
2325  * This function is mostly intended for use by accessibility technologies;
2326  * applications should have little use for it.
2327  *
2328  * Since: 2.4
2329  */
2330 void
2331 gtk_combo_box_popup (GtkComboBox *combo_box)
2332 {
2333   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2334
2335   g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
2336 }
2337
2338 /**
2339  * gtk_combo_box_popup_for_device:
2340  * @combo_box: a #GtkComboBox
2341  * @device: a #GdkDevice
2342  *
2343  * Pops up the menu or dropdown list of @combo_box, the popup window
2344  * will be grabbed so only @device and its associated pointer/keyboard
2345  * are the only #GdkDevice<!-- -->s able to send events to it.
2346  *
2347  * Since: 3.0
2348  **/
2349 void
2350 gtk_combo_box_popup_for_device (GtkComboBox *combo_box,
2351                                 GdkDevice   *device)
2352 {
2353   GtkComboBoxPrivate *priv = combo_box->priv;
2354   gint x, y, width, height;
2355   GtkTreePath *path = NULL, *ppath;
2356   GtkWidget *toplevel;
2357   GdkDevice *keyboard, *pointer;
2358   guint32 time;
2359
2360   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2361   g_return_if_fail (GDK_IS_DEVICE (device));
2362
2363   if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2364     return;
2365
2366   if (gtk_widget_get_mapped (priv->popup_widget))
2367     return;
2368
2369   if (priv->grab_pointer && priv->grab_keyboard)
2370     return;
2371
2372   time = gtk_get_current_event_time ();
2373
2374   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2375     {
2376       keyboard = device;
2377       pointer = gdk_device_get_associated_device (device);
2378     }
2379   else
2380     {
2381       pointer = device;
2382       keyboard = gdk_device_get_associated_device (device);
2383     }
2384
2385   if (GTK_IS_MENU (priv->popup_widget))
2386     {
2387       gtk_combo_box_menu_popup (combo_box,
2388                                 priv->activate_button,
2389                                 priv->activate_time);
2390       return;
2391     }
2392
2393   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
2394   if (GTK_IS_WINDOW (toplevel))
2395     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
2396                                  GTK_WINDOW (priv->popup_window));
2397
2398   gtk_widget_show_all (priv->scrolled_window);
2399   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2400
2401   gtk_widget_set_size_request (priv->popup_window, width, height);
2402   gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2403
2404   if (gtk_tree_row_reference_valid (priv->active_row))
2405     {
2406       path = gtk_tree_row_reference_get_path (priv->active_row);
2407       ppath = gtk_tree_path_copy (path);
2408       if (gtk_tree_path_up (ppath))
2409         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (priv->tree_view),
2410                                       ppath);
2411       gtk_tree_path_free (ppath);
2412     }
2413   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
2414                                   TRUE);
2415
2416   /* popup */
2417   gtk_widget_show (priv->popup_window);
2418
2419   if (path)
2420     {
2421       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
2422                                 path, NULL, FALSE);
2423       gtk_tree_path_free (path);
2424     }
2425
2426   gtk_widget_grab_focus (priv->popup_window);
2427   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2428                                 TRUE);
2429
2430   if (!gtk_widget_has_focus (priv->tree_view))
2431     gtk_widget_grab_focus (priv->tree_view);
2432
2433   if (!popup_grab_on_window (gtk_widget_get_window (priv->popup_window),
2434                              keyboard, pointer, time))
2435     {
2436       gtk_widget_hide (priv->popup_window);
2437       return;
2438     }
2439
2440   gtk_device_grab_add (priv->popup_window, pointer, TRUE);
2441   priv->grab_pointer = pointer;
2442   priv->grab_keyboard = keyboard;
2443 }
2444
2445 static void
2446 gtk_combo_box_real_popup (GtkComboBox *combo_box)
2447 {
2448   GdkDevice *device;
2449
2450   device = gtk_get_current_event_device ();
2451
2452   if (!device)
2453     {
2454       GdkDeviceManager *device_manager;
2455       GdkDisplay *display;
2456       GList *devices;
2457
2458       display = gtk_widget_get_display (GTK_WIDGET (combo_box));
2459       device_manager = gdk_display_get_device_manager (display);
2460
2461       /* No device was set, pick the first master device */
2462       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
2463       device = devices->data;
2464       g_list_free (devices);
2465     }
2466
2467   gtk_combo_box_popup_for_device (combo_box, device);
2468 }
2469
2470 static gboolean
2471 gtk_combo_box_real_popdown (GtkComboBox *combo_box)
2472 {
2473   if (combo_box->priv->popup_shown)
2474     {
2475       gtk_combo_box_popdown (combo_box);
2476       return TRUE;
2477     }
2478
2479   return FALSE;
2480 }
2481
2482 /**
2483  * gtk_combo_box_popdown:
2484  * @combo_box: a #GtkComboBox
2485  *
2486  * Hides the menu or dropdown list of @combo_box.
2487  *
2488  * This function is mostly intended for use by accessibility technologies;
2489  * applications should have little use for it.
2490  *
2491  * Since: 2.4
2492  */
2493 void
2494 gtk_combo_box_popdown (GtkComboBox *combo_box)
2495 {
2496   GtkComboBoxPrivate *priv = combo_box->priv;
2497
2498   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2499
2500   if (GTK_IS_MENU (priv->popup_widget))
2501     {
2502       gtk_menu_popdown (GTK_MENU (priv->popup_widget));
2503       return;
2504     }
2505
2506   if (!gtk_widget_get_realized (GTK_WIDGET (combo_box)))
2507     return;
2508
2509   gtk_device_grab_remove (priv->popup_window, priv->grab_pointer);
2510   gtk_widget_hide (priv->popup_window);
2511   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button),
2512                                 FALSE);
2513
2514   priv->grab_pointer = NULL;
2515   priv->grab_keyboard = NULL;
2516 }
2517
2518 #define GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON                      \
2519   GtkAllocation button_allocation;                              \
2520   gtk_widget_get_preferred_size (combo_box->priv->button,       \
2521                                  &req, NULL);                   \
2522                                                                 \
2523   if (is_rtl)                                                   \
2524     button_allocation.x = allocation->x;                        \
2525   else                                                          \
2526     button_allocation.x = allocation->x + allocation->width     \
2527      - req.width;                                               \
2528                                                                 \
2529   button_allocation.y = allocation->y;                          \
2530   button_allocation.width = MAX (1, req.width);                 \
2531   button_allocation.height = allocation->height;                \
2532   button_allocation.height = MAX (1, button_allocation.height); \
2533                                                                 \
2534   gtk_widget_size_allocate (combo_box->priv->button,            \
2535                             &button_allocation);
2536
2537
2538 static void
2539 gtk_combo_box_size_allocate (GtkWidget     *widget,
2540                              GtkAllocation *allocation)
2541 {
2542   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2543   GtkComboBoxPrivate *priv = combo_box->priv;
2544   GtkWidget *child_widget;
2545   GtkAllocation child;
2546   GtkRequisition req;
2547   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2548   GtkBorder padding;
2549
2550   gtk_widget_set_allocation (widget, allocation);
2551   child_widget = gtk_bin_get_child (GTK_BIN (widget));
2552   get_widget_padding_and_border (widget, &padding);
2553
2554   allocation->x += padding.left;
2555   allocation->y += padding.top;
2556   allocation->width -= padding.left + padding.right;
2557   allocation->height -= padding.top + padding.bottom;
2558
2559   if (!priv->tree_view)
2560     {
2561       if (priv->cell_view)
2562         {
2563           GtkBorder button_padding;
2564           gint width;
2565           guint border_width;
2566
2567           border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->button));
2568           get_widget_padding_and_border (priv->button, &button_padding);
2569
2570           /* menu mode; child_widget is priv->cell_view.
2571            * Allocate the button to the full combobox allocation (minus the
2572            * padding).
2573            */
2574           gtk_widget_size_allocate (priv->button, allocation);
2575
2576           child.x = allocation->x;
2577           child.y = allocation->y;
2578           width = allocation->width;
2579           child.height = allocation->height;
2580
2581           if (!priv->is_cell_renderer)
2582             {
2583               /* restrict allocation of the child into the button box
2584                * if we're not in cell renderer mode.
2585                */
2586               child.x += border_width + button_padding.left;
2587               child.y += border_width + button_padding.top;
2588               width -= 2 * border_width +
2589                 button_padding.left + button_padding.right;
2590               child.height -= 2 * border_width +
2591                 button_padding.top + button_padding.bottom;
2592             }
2593
2594           /* allocate the box containing the separator and the arrow */
2595           gtk_widget_get_preferred_size (priv->box, &req, NULL);
2596           child.width = req.width;
2597           if (!is_rtl)
2598             child.x += width - req.width;
2599           child.width = MAX (1, child.width);
2600           child.height = MAX (1, child.height);
2601           gtk_widget_size_allocate (priv->box, &child);
2602
2603           if (is_rtl)
2604             {
2605               child.x += req.width;
2606               child.width = allocation->x + allocation->width
2607                 - border_width - child.x;
2608             }
2609           else
2610             {
2611               child.width = child.x;
2612               child.x = allocation->x
2613                 + border_width + button_padding.left;
2614               child.width -= child.x;
2615             }
2616
2617           if (gtk_widget_get_visible (priv->popup_widget))
2618             {
2619               gint width, menu_width;
2620
2621               if (priv->wrap_width == 0)
2622                 {
2623                   GtkAllocation combo_box_allocation;
2624
2625                   gtk_widget_get_allocation (GTK_WIDGET (combo_box), &combo_box_allocation);
2626                   width = combo_box_allocation.width;
2627                   gtk_widget_set_size_request (priv->popup_widget, -1, -1);
2628
2629                   if (combo_box->priv->popup_fixed_width)
2630                     gtk_widget_get_preferred_width (priv->popup_widget, &menu_width, NULL);
2631                   else
2632                     gtk_widget_get_preferred_width (priv->popup_widget, NULL, &menu_width);
2633
2634                   gtk_widget_set_size_request (priv->popup_widget,
2635                                                MAX (width, menu_width), -1);
2636                }
2637
2638               /* reposition the menu after giving it a new width */
2639               gtk_menu_reposition (GTK_MENU (priv->popup_widget));
2640             }
2641
2642           child.width = MAX (1, child.width);
2643           child.height = MAX (1, child.height);
2644           gtk_widget_size_allocate (child_widget, &child);
2645         }
2646       else
2647         {
2648           /* menu mode; child_widget has been set with gtk_container_add().
2649            * E.g. it might be a GtkEntry if priv->has_entry is TRUE.
2650            * Allocate the button at the far end, according to the direction
2651            * of the widget.
2652            */
2653           GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2654
2655             /* After the macro, button_allocation has the button allocation rect */
2656
2657           if (is_rtl)
2658             child.x = button_allocation.x + button_allocation.width;
2659           else
2660             child.x = allocation->x;
2661
2662           child.y = allocation->y;
2663           child.width = allocation->width - button_allocation.width;
2664           child.height = button_allocation.height;
2665
2666           child.width = MAX (1, child.width);
2667
2668           gtk_widget_size_allocate (child_widget, &child);
2669         }
2670     }
2671   else
2672     {
2673       /* list mode; child_widget might be either priv->cell_view or a child
2674        * added with gtk_container_add().
2675        */
2676       guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2677
2678       /* After the macro, button_allocation has the button allocation rect */
2679       GTK_COMBO_BOX_SIZE_ALLOCATE_BUTTON
2680
2681       if (is_rtl)
2682         child.x = button_allocation.x + button_allocation.width;
2683       else
2684         child.x = allocation->x + border_width;
2685
2686       child.y = allocation->y + border_width;
2687       child.width = allocation->width - button_allocation.width - (2 * border_width);
2688       child.height = button_allocation.height - 2 * border_width;
2689
2690       if (priv->cell_view_frame)
2691         {
2692           gtk_widget_size_allocate (priv->cell_view_frame, &child);
2693
2694           /* restrict allocation of the child into the frame box if it's present */
2695           if (priv->has_frame)
2696             {
2697               GtkBorder frame_padding;
2698
2699               border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
2700               get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
2701
2702               child.x += border_width + frame_padding.left;
2703               child.y += border_width + frame_padding.right;
2704               child.width -= (2 * border_width) + frame_padding.left + frame_padding.right;
2705               child.height -= (2 * border_width) + frame_padding.top + frame_padding.bottom;
2706             }
2707         }
2708
2709       if (gtk_widget_get_visible (priv->popup_window))
2710         {
2711           gint x, y, width, height;
2712           gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2713           gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
2714           gtk_widget_set_size_request (priv->popup_window, width, height);
2715         }
2716
2717       /* allocate the child */
2718       child.width = MAX (1, child.width);
2719       child.height = MAX (1, child.height);
2720       gtk_widget_size_allocate (child_widget, &child);
2721     }
2722 }
2723
2724 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2725
2726 static void
2727 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2728 {
2729   GtkComboBoxPrivate *priv = combo_box->priv;
2730
2731   if (priv->model)
2732     {
2733       g_signal_handler_disconnect (priv->model,
2734                                    priv->inserted_id);
2735       g_signal_handler_disconnect (priv->model,
2736                                    priv->deleted_id);
2737       g_signal_handler_disconnect (priv->model,
2738                                    priv->reordered_id);
2739       g_signal_handler_disconnect (priv->model,
2740                                    priv->changed_id);
2741     }
2742
2743   if (priv->model)
2744     {
2745       g_object_unref (priv->model);
2746       priv->model = NULL;
2747     }
2748
2749   if (priv->active_row)
2750     {
2751       gtk_tree_row_reference_free (priv->active_row);
2752       priv->active_row = NULL;
2753     }
2754
2755   if (priv->cell_view)
2756     gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2757 }
2758
2759 static void
2760 gtk_combo_box_forall (GtkContainer *container,
2761                       gboolean      include_internals,
2762                       GtkCallback   callback,
2763                       gpointer      callback_data)
2764 {
2765   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2766   GtkComboBoxPrivate *priv = combo_box->priv;
2767   GtkWidget *child;
2768
2769   if (include_internals)
2770     {
2771       if (priv->button)
2772         (* callback) (priv->button, callback_data);
2773       if (priv->cell_view_frame)
2774         (* callback) (priv->cell_view_frame, callback_data);
2775     }
2776
2777   child = gtk_bin_get_child (GTK_BIN (container));
2778   if (child)
2779     (* callback) (child, callback_data);
2780 }
2781
2782 static void
2783 gtk_combo_box_child_show (GtkWidget *widget,
2784                           GtkComboBox *combo_box)
2785 {
2786   GtkComboBoxPrivate *priv = combo_box->priv;
2787
2788   priv->popup_shown = TRUE;
2789   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2790 }
2791
2792 static void
2793 gtk_combo_box_child_hide (GtkWidget *widget,
2794                           GtkComboBox *combo_box)
2795 {
2796   GtkComboBoxPrivate *priv = combo_box->priv;
2797
2798   priv->popup_shown = FALSE;
2799   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2800 }
2801
2802 static gboolean
2803 gtk_combo_box_draw (GtkWidget *widget,
2804                     cairo_t   *cr)
2805 {
2806   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2807   GtkComboBoxPrivate *priv = combo_box->priv;
2808
2809   if (priv->shadow_type != GTK_SHADOW_NONE)
2810     {
2811       GtkStyleContext *context;
2812
2813       context = gtk_widget_get_style_context (widget);
2814
2815       gtk_render_background (context, cr, 0, 0,
2816                              gtk_widget_get_allocated_width (widget),
2817                              gtk_widget_get_allocated_height (widget));
2818       gtk_render_frame (context, cr, 0, 0,
2819                         gtk_widget_get_allocated_width (widget),
2820                         gtk_widget_get_allocated_height (widget));
2821     }
2822
2823   gtk_container_propagate_draw (GTK_CONTAINER (widget),
2824                                 priv->button, cr);
2825
2826   if (priv->tree_view && priv->cell_view_frame)
2827     {
2828       gtk_container_propagate_draw (GTK_CONTAINER (widget),
2829                                     priv->cell_view_frame, cr);
2830     }
2831
2832   gtk_container_propagate_draw (GTK_CONTAINER (widget),
2833                                 gtk_bin_get_child (GTK_BIN (widget)),
2834                                 cr);
2835
2836   return FALSE;
2837 }
2838
2839 typedef struct {
2840   GtkComboBox *combo;
2841   GtkTreePath *path;
2842   GtkTreeIter iter;
2843   gboolean found;
2844   gboolean set;
2845   gboolean visible;
2846 } SearchData;
2847
2848 static gboolean
2849 path_visible (GtkTreeView *view,
2850               GtkTreePath *path)
2851 {
2852   GtkRBTree *tree;
2853   GtkRBNode *node;
2854
2855   /* Note that we rely on the fact that collapsed rows don't have nodes
2856    */
2857   return _gtk_tree_view_find_node (view, path, &tree, &node);
2858 }
2859
2860 static gboolean
2861 tree_next_func (GtkTreeModel *model,
2862                 GtkTreePath  *path,
2863                 GtkTreeIter  *iter,
2864                 gpointer      data)
2865 {
2866   SearchData *search_data = (SearchData *)data;
2867
2868   if (search_data->found)
2869     {
2870       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2871         return FALSE;
2872
2873       if (search_data->visible &&
2874           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2875         return FALSE;
2876
2877       search_data->set = TRUE;
2878       search_data->iter = *iter;
2879
2880       return TRUE;
2881     }
2882
2883   if (gtk_tree_path_compare (path, search_data->path) == 0)
2884     search_data->found = TRUE;
2885
2886   return FALSE;
2887 }
2888
2889 static gboolean
2890 tree_next (GtkComboBox  *combo,
2891            GtkTreeModel *model,
2892            GtkTreeIter  *iter,
2893            GtkTreeIter  *next,
2894            gboolean      visible)
2895 {
2896   SearchData search_data;
2897
2898   search_data.combo = combo;
2899   search_data.path = gtk_tree_model_get_path (model, iter);
2900   search_data.visible = visible;
2901   search_data.found = FALSE;
2902   search_data.set = FALSE;
2903
2904   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2905
2906   *next = search_data.iter;
2907
2908   gtk_tree_path_free (search_data.path);
2909
2910   return search_data.set;
2911 }
2912
2913 static gboolean
2914 tree_prev_func (GtkTreeModel *model,
2915                 GtkTreePath  *path,
2916                 GtkTreeIter  *iter,
2917                 gpointer      data)
2918 {
2919   SearchData *search_data = (SearchData *)data;
2920
2921   if (gtk_tree_path_compare (path, search_data->path) == 0)
2922     {
2923       search_data->found = TRUE;
2924       return TRUE;
2925     }
2926
2927   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2928     return FALSE;
2929
2930   if (search_data->visible &&
2931       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2932     return FALSE;
2933
2934   search_data->set = TRUE;
2935   search_data->iter = *iter;
2936
2937   return FALSE;
2938 }
2939
2940 static gboolean
2941 tree_prev (GtkComboBox  *combo,
2942            GtkTreeModel *model,
2943            GtkTreeIter  *iter,
2944            GtkTreeIter  *prev,
2945            gboolean      visible)
2946 {
2947   SearchData search_data;
2948
2949   search_data.combo = combo;
2950   search_data.path = gtk_tree_model_get_path (model, iter);
2951   search_data.visible = visible;
2952   search_data.found = FALSE;
2953   search_data.set = FALSE;
2954
2955   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2956
2957   *prev = search_data.iter;
2958
2959   gtk_tree_path_free (search_data.path);
2960
2961   return search_data.set;
2962 }
2963
2964 static gboolean
2965 tree_last_func (GtkTreeModel *model,
2966                 GtkTreePath  *path,
2967                 GtkTreeIter  *iter,
2968                 gpointer      data)
2969 {
2970   SearchData *search_data = (SearchData *)data;
2971
2972   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2973     return FALSE;
2974
2975   /* Note that we rely on the fact that collapsed rows don't have nodes
2976    */
2977   if (search_data->visible &&
2978       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2979     return FALSE;
2980
2981   search_data->set = TRUE;
2982   search_data->iter = *iter;
2983
2984   return FALSE;
2985 }
2986
2987 static gboolean
2988 tree_last (GtkComboBox  *combo,
2989            GtkTreeModel *model,
2990            GtkTreeIter  *last,
2991            gboolean      visible)
2992 {
2993   SearchData search_data;
2994
2995   search_data.combo = combo;
2996   search_data.visible = visible;
2997   search_data.set = FALSE;
2998
2999   gtk_tree_model_foreach (model, tree_last_func, &search_data);
3000
3001   *last = search_data.iter;
3002
3003   return search_data.set;
3004 }
3005
3006
3007 static gboolean
3008 tree_first_func (GtkTreeModel *model,
3009                  GtkTreePath  *path,
3010                  GtkTreeIter  *iter,
3011                  gpointer      data)
3012 {
3013   SearchData *search_data = (SearchData *)data;
3014
3015   if (!tree_column_row_is_sensitive (search_data->combo, iter))
3016     return FALSE;
3017
3018   if (search_data->visible &&
3019       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
3020     return FALSE;
3021
3022   search_data->set = TRUE;
3023   search_data->iter = *iter;
3024
3025   return TRUE;
3026 }
3027
3028 static gboolean
3029 tree_first (GtkComboBox  *combo,
3030             GtkTreeModel *model,
3031             GtkTreeIter  *first,
3032             gboolean      visible)
3033 {
3034   SearchData search_data;
3035
3036   search_data.combo = combo;
3037   search_data.visible = visible;
3038   search_data.set = FALSE;
3039
3040   gtk_tree_model_foreach (model, tree_first_func, &search_data);
3041
3042   *first = search_data.iter;
3043
3044   return search_data.set;
3045 }
3046
3047 static gboolean
3048 gtk_combo_box_scroll_event (GtkWidget          *widget,
3049                             GdkEventScroll     *event)
3050 {
3051   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3052   gboolean found;
3053   GtkTreeIter iter;
3054   GtkTreeIter new_iter;
3055
3056   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
3057     return TRUE;
3058
3059   if (event->direction == GDK_SCROLL_UP)
3060     found = tree_prev (combo_box, combo_box->priv->model,
3061                        &iter, &new_iter, FALSE);
3062   else
3063     found = tree_next (combo_box, combo_box->priv->model,
3064                        &iter, &new_iter, FALSE);
3065
3066   if (found)
3067     gtk_combo_box_set_active_iter (combo_box, &new_iter);
3068
3069   return TRUE;
3070 }
3071
3072 /*
3073  * menu style
3074  */
3075 static gboolean
3076 gtk_combo_box_row_separator_func (GtkTreeModel      *model,
3077                                   GtkTreeIter       *iter,
3078                                   GtkComboBox       *combo)
3079 {
3080   GtkComboBoxPrivate *priv = combo->priv;
3081
3082   if (priv->row_separator_func)
3083     return priv->row_separator_func (model, iter, priv->row_separator_data);
3084
3085   return FALSE;
3086 }
3087
3088 static gboolean
3089 gtk_combo_box_header_func (GtkTreeModel      *model,
3090                            GtkTreeIter       *iter,
3091                            GtkComboBox       *combo)
3092 {
3093   /* Every submenu has a selectable header, however we
3094    * can expose a method to make that configurable by
3095    * the user (like row_separator_func is done) */
3096   return TRUE;
3097 }
3098
3099 static void
3100 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
3101                           gboolean     add_children)
3102 {
3103   GtkComboBoxPrivate *priv = combo_box->priv;
3104   GtkWidget *child;
3105   GtkWidget *menu;
3106
3107   child = gtk_bin_get_child (GTK_BIN (combo_box));
3108
3109   if (priv->cell_view)
3110     {
3111       priv->button = gtk_toggle_button_new ();
3112       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3113                                      priv->focus_on_click);
3114
3115       g_signal_connect (priv->button, "toggled",
3116                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3117       gtk_widget_set_parent (priv->button,
3118                              gtk_widget_get_parent (child));
3119
3120       priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
3121       gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
3122
3123       priv->separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
3124       gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
3125
3126       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3127       gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
3128
3129       gtk_widget_show_all (priv->button);
3130     }
3131   else
3132     {
3133       priv->button = gtk_toggle_button_new ();
3134       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
3135                                      priv->focus_on_click);
3136
3137       g_signal_connect (priv->button, "toggled",
3138                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3139       gtk_widget_set_parent (priv->button,
3140                              gtk_widget_get_parent (child));
3141
3142       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3143       gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3144       gtk_widget_show_all (priv->button);
3145     }
3146
3147   g_signal_connect (priv->button, "button-press-event",
3148                     G_CALLBACK (gtk_combo_box_menu_button_press),
3149                     combo_box);
3150   g_signal_connect (priv->button, "state-flags-changed",
3151                     G_CALLBACK (gtk_combo_box_button_state_flags_changed),
3152                     combo_box);
3153
3154   /* create our funky menu */
3155   menu = _gtk_tree_menu_new_with_area (priv->area);
3156   gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
3157
3158   _gtk_tree_menu_set_model (GTK_TREE_MENU (menu), priv->model);
3159
3160   _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (menu), priv->wrap_width);
3161   _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (menu), priv->row_column);
3162   _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (menu), priv->col_column);
3163   _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (menu),
3164                               combo_box->priv->add_tearoffs);
3165
3166   g_signal_connect (menu, "menu-activate",
3167                     G_CALLBACK (gtk_combo_box_menu_activate), combo_box);
3168
3169   /* Chain our row_separator_func through */
3170   _gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (menu),
3171                                          (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3172                                          combo_box, NULL);
3173
3174   _gtk_tree_menu_set_header_func (GTK_TREE_MENU (menu),
3175                                   (GtkTreeMenuHeaderFunc)gtk_combo_box_header_func,
3176                                   combo_box, NULL);
3177
3178   g_signal_connect (menu, "key-press-event",
3179                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
3180   gtk_combo_box_set_popup_widget (combo_box, menu);
3181
3182   gtk_combo_box_update_title (combo_box);
3183 }
3184
3185 static void
3186 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
3187 {
3188   GtkComboBoxPrivate *priv = combo_box->priv;
3189
3190   g_signal_handlers_disconnect_matched (priv->button,
3191                                         G_SIGNAL_MATCH_DATA,
3192                                         0, 0, NULL,
3193                                         gtk_combo_box_menu_button_press, NULL);
3194   g_signal_handlers_disconnect_matched (priv->button,
3195                                         G_SIGNAL_MATCH_DATA,
3196                                         0, 0, NULL,
3197                                         gtk_combo_box_button_state_flags_changed, combo_box);
3198   g_signal_handlers_disconnect_matched (priv->popup_widget,
3199                                         G_SIGNAL_MATCH_DATA,
3200                                         0, 0, NULL,
3201                                         gtk_combo_box_menu_activate, combo_box);
3202
3203   /* unparent will remove our latest ref */
3204   gtk_widget_unparent (priv->button);
3205
3206   priv->box = NULL;
3207   priv->button = NULL;
3208   priv->arrow = NULL;
3209   priv->separator = NULL;
3210
3211   /* changing the popup window will unref the menu and the children */
3212 }
3213
3214 /* callbacks */
3215 static gboolean
3216 gtk_combo_box_menu_button_press (GtkWidget      *widget,
3217                                  GdkEventButton *event,
3218                                  gpointer        user_data)
3219 {
3220   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3221   GtkComboBoxPrivate *priv = combo_box->priv;
3222
3223   if (GTK_IS_MENU (priv->popup_widget) &&
3224       event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
3225     {
3226       if (priv->focus_on_click &&
3227           !gtk_widget_has_focus (priv->button))
3228         gtk_widget_grab_focus (priv->button);
3229
3230       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3231
3232       return TRUE;
3233     }
3234
3235   return FALSE;
3236 }
3237
3238 static void
3239 gtk_combo_box_menu_activate (GtkWidget   *menu,
3240                              const gchar *path,
3241                              GtkComboBox *combo_box)
3242 {
3243   GtkTreeIter iter;
3244
3245   if (gtk_tree_model_get_iter_from_string (combo_box->priv->model, &iter, path))
3246     gtk_combo_box_set_active_iter (combo_box, &iter);
3247
3248   g_object_set (combo_box,
3249                 "editing-canceled", FALSE,
3250                 NULL);
3251 }
3252
3253
3254 static void
3255 gtk_combo_box_update_sensitivity (GtkComboBox *combo_box)
3256 {
3257   GtkTreeIter iter;
3258   gboolean sensitive = TRUE; /* fool code checkers */
3259
3260   if (!combo_box->priv->button)
3261     return;
3262
3263   switch (combo_box->priv->button_sensitivity)
3264     {
3265       case GTK_SENSITIVITY_ON:
3266         sensitive = TRUE;
3267         break;
3268       case GTK_SENSITIVITY_OFF:
3269         sensitive = FALSE;
3270         break;
3271       case GTK_SENSITIVITY_AUTO:
3272         sensitive = combo_box->priv->model &&
3273                     gtk_tree_model_get_iter_first (combo_box->priv->model, &iter);
3274         break;
3275       default:
3276         g_assert_not_reached ();
3277         break;
3278     }
3279
3280   gtk_widget_set_sensitive (combo_box->priv->button, sensitive);
3281
3282   /* In list-mode, we also need to update sensitivity of the event box */
3283   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view)
3284       && combo_box->priv->cell_view)
3285     gtk_widget_set_sensitive (combo_box->priv->box, sensitive);
3286 }
3287
3288 static void
3289 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
3290                                   GtkTreePath      *path,
3291                                   GtkTreeIter      *iter,
3292                                   gpointer          user_data)
3293 {
3294   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3295
3296   if (combo_box->priv->tree_view)
3297     gtk_combo_box_list_popup_resize (combo_box);
3298
3299   gtk_combo_box_update_sensitivity (combo_box);
3300 }
3301
3302 static void
3303 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
3304                                  GtkTreePath      *path,
3305                                  gpointer          user_data)
3306 {
3307   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3308   GtkComboBoxPrivate *priv = combo_box->priv;
3309
3310   if (!gtk_tree_row_reference_valid (priv->active_row))
3311     {
3312       if (priv->cell_view)
3313         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3314       g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3315     }
3316   
3317   if (priv->tree_view)
3318     gtk_combo_box_list_popup_resize (combo_box);
3319
3320   gtk_combo_box_update_sensitivity (combo_box);
3321 }
3322
3323 static void
3324 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
3325                                     GtkTreePath     *path,
3326                                     GtkTreeIter     *iter,
3327                                     gint            *new_order,
3328                                     gpointer         user_data)
3329 {
3330   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3331 }
3332
3333 static void
3334 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
3335                                  GtkTreePath      *path,
3336                                  GtkTreeIter      *iter,
3337                                  gpointer          user_data)
3338 {
3339   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3340   GtkComboBoxPrivate *priv = combo_box->priv;
3341   GtkTreePath *active_path;
3342
3343   /* FIXME this belongs to GtkCellView */
3344   if (gtk_tree_row_reference_valid (priv->active_row))
3345     {
3346       active_path = gtk_tree_row_reference_get_path (priv->active_row);
3347       if (gtk_tree_path_compare (path, active_path) == 0 &&
3348           priv->cell_view)
3349         gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3350       gtk_tree_path_free (active_path);
3351     }
3352
3353   if (priv->tree_view)
3354     gtk_combo_box_list_row_changed (model, path, iter, user_data);
3355 }
3356
3357 static gboolean
3358 list_popup_resize_idle (gpointer user_data)
3359 {
3360   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3361   GtkComboBoxPrivate *priv = combo_box->priv;
3362   gint x, y, width, height;
3363
3364   if (priv->tree_view && gtk_widget_get_mapped (priv->popup_window))
3365     {
3366       gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3367
3368       gtk_widget_set_size_request (priv->popup_window, width, height);
3369       gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3370     }
3371
3372   priv->resize_idle_id = 0;
3373
3374   return FALSE;
3375 }
3376
3377 static void
3378 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3379 {
3380   GtkComboBoxPrivate *priv = combo_box->priv;
3381
3382   if (!priv->resize_idle_id)
3383     priv->resize_idle_id =
3384       gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3385 }
3386
3387 static void
3388 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
3389                                   GtkTreePath      *path,
3390                                   GtkTreeIter      *iter,
3391                                   gpointer          user_data)
3392 {
3393   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3394
3395   gtk_combo_box_list_popup_resize (combo_box);
3396 }
3397
3398
3399 /*
3400  * list style
3401  */
3402
3403 static void
3404 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3405 {
3406   GtkComboBoxPrivate *priv = combo_box->priv;
3407   GtkTreeSelection *sel;
3408   GtkWidget *child;
3409   GtkWidget *widget = GTK_WIDGET (combo_box);
3410
3411   priv->button = gtk_toggle_button_new ();
3412   child = gtk_bin_get_child (GTK_BIN (combo_box));
3413   gtk_widget_set_parent (priv->button,
3414                          gtk_widget_get_parent (child));
3415   g_signal_connect (priv->button, "button-press-event",
3416                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3417   g_signal_connect (priv->button, "toggled",
3418                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3419
3420   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3421   gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3422   priv->separator = NULL;
3423   gtk_widget_show_all (priv->button);
3424
3425   if (priv->cell_view)
3426     {
3427       GtkStyleContext *context;
3428       GtkStateFlags state;
3429       GdkRGBA color;
3430
3431       context  = gtk_widget_get_style_context (widget);
3432       state = gtk_widget_get_state_flags (widget);
3433       gtk_style_context_get_background_color (context, state, &color);
3434
3435       gtk_cell_view_set_background_rgba (GTK_CELL_VIEW (priv->cell_view), &color);
3436
3437       priv->box = gtk_event_box_new ();
3438       gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box),
3439                                         FALSE);
3440
3441       if (priv->has_frame)
3442         {
3443           priv->cell_view_frame = gtk_frame_new (NULL);
3444           gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3445                                      GTK_SHADOW_IN);
3446         }
3447       else
3448         {
3449           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3450           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame),
3451                                             FALSE);
3452         }
3453
3454       gtk_widget_set_parent (priv->cell_view_frame,
3455                              gtk_widget_get_parent (child));
3456       gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3457       gtk_widget_show_all (priv->cell_view_frame);
3458
3459       g_signal_connect (priv->box, "button-press-event",
3460                         G_CALLBACK (gtk_combo_box_list_button_pressed),
3461                         combo_box);
3462     }
3463
3464   priv->tree_view = gtk_tree_view_new ();
3465   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3466   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3467   gtk_tree_selection_set_select_function (sel,
3468                                           gtk_combo_box_list_select_func,
3469                                           NULL, NULL);
3470   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3471                                      FALSE);
3472   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3473                                      TRUE);
3474
3475   gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view),
3476                                         (GtkTreeViewRowSeparatorFunc)gtk_combo_box_row_separator_func,
3477                                         combo_box, NULL);
3478
3479   if (priv->model)
3480     gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3481
3482   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view),
3483                                gtk_tree_view_column_new_with_area (priv->area));
3484
3485   if (gtk_tree_row_reference_valid (priv->active_row))
3486     {
3487       GtkTreePath *path;
3488
3489       path = gtk_tree_row_reference_get_path (priv->active_row);
3490       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3491                                 path, NULL, FALSE);
3492       gtk_tree_path_free (path);
3493     }
3494
3495   /* set sample/popup widgets */
3496   gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3497
3498   g_signal_connect (priv->tree_view, "key-press-event",
3499                     G_CALLBACK (gtk_combo_box_list_key_press),
3500                     combo_box);
3501   g_signal_connect (priv->tree_view, "enter-notify-event",
3502                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3503                     combo_box);
3504   g_signal_connect (priv->tree_view, "row-expanded",
3505                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3506                     combo_box);
3507   g_signal_connect (priv->tree_view, "row-collapsed",
3508                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3509                     combo_box);
3510   g_signal_connect (priv->popup_window, "button-press-event",
3511                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3512                     combo_box);
3513   g_signal_connect (priv->popup_window, "button-release-event",
3514                     G_CALLBACK (gtk_combo_box_list_button_released),
3515                     combo_box);
3516
3517   gtk_widget_show (priv->tree_view);
3518
3519   gtk_combo_box_update_sensitivity (combo_box);
3520 }
3521
3522 static void
3523 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3524 {
3525   GtkComboBoxPrivate *priv = combo_box->priv;
3526
3527   /* disconnect signals */
3528   g_signal_handlers_disconnect_matched (priv->tree_view,
3529                                         G_SIGNAL_MATCH_DATA,
3530                                         0, 0, NULL, NULL, combo_box);
3531   g_signal_handlers_disconnect_matched (priv->button,
3532                                         G_SIGNAL_MATCH_DATA,
3533                                         0, 0, NULL,
3534                                         gtk_combo_box_list_button_pressed,
3535                                         NULL);
3536   g_signal_handlers_disconnect_matched (priv->popup_window,
3537                                         G_SIGNAL_MATCH_DATA,
3538                                         0, 0, NULL,
3539                                         gtk_combo_box_list_button_pressed,
3540                                         NULL);
3541   g_signal_handlers_disconnect_matched (priv->popup_window,
3542                                         G_SIGNAL_MATCH_DATA,
3543                                         0, 0, NULL,
3544                                         gtk_combo_box_list_button_released,
3545                                         NULL);
3546
3547   g_signal_handlers_disconnect_matched (priv->popup_window,
3548                                         G_SIGNAL_MATCH_DATA,
3549                                         0, 0, NULL,
3550                                         gtk_combo_box_child_show,
3551                                         NULL);
3552
3553   g_signal_handlers_disconnect_matched (priv->popup_window,
3554                                         G_SIGNAL_MATCH_DATA,
3555                                         0, 0, NULL,
3556                                         gtk_combo_box_child_hide,
3557                                         NULL);
3558
3559   if (priv->box)
3560     g_signal_handlers_disconnect_matched (priv->box,
3561                                           G_SIGNAL_MATCH_DATA,
3562                                           0, 0, NULL,
3563                                           gtk_combo_box_list_button_pressed,
3564                                           NULL);
3565
3566   /* destroy things (unparent will kill the latest ref from us)
3567    * last unref on button will destroy the arrow
3568    */
3569   gtk_widget_unparent (priv->button);
3570   priv->button = NULL;
3571   priv->arrow = NULL;
3572
3573   if (priv->cell_view)
3574     {
3575       g_object_set (priv->cell_view,
3576                     "background-set", FALSE,
3577                     NULL);
3578     }
3579
3580   if (priv->cell_view_frame)
3581     {
3582       gtk_widget_unparent (priv->cell_view_frame);
3583       priv->cell_view_frame = NULL;
3584       priv->box = NULL;
3585     }
3586
3587   if (priv->scroll_timer)
3588     {
3589       g_source_remove (priv->scroll_timer);
3590       priv->scroll_timer = 0;
3591     }
3592
3593   if (priv->resize_idle_id)
3594     {
3595       g_source_remove (priv->resize_idle_id);
3596       priv->resize_idle_id = 0;
3597     }
3598
3599   gtk_widget_destroy (priv->tree_view);
3600
3601   priv->tree_view = NULL;
3602   if (priv->popup_widget)
3603     {
3604       g_object_unref (priv->popup_widget);
3605       priv->popup_widget = NULL;
3606     }
3607 }
3608
3609 /* callbacks */
3610
3611 static gboolean
3612 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3613                                    GdkEventButton *event,
3614                                    gpointer        data)
3615 {
3616   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3617   GtkComboBoxPrivate *priv = combo_box->priv;
3618
3619   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3620
3621   if (ewidget == priv->popup_window)
3622     return TRUE;
3623
3624   if ((ewidget != priv->button && ewidget != priv->box) ||
3625       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3626     return FALSE;
3627
3628   if (priv->focus_on_click &&
3629       !gtk_widget_has_focus (priv->button))
3630     gtk_widget_grab_focus (priv->button);
3631
3632   gtk_combo_box_popup_for_device (combo_box, event->device);
3633
3634   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3635
3636   priv->auto_scroll = FALSE;
3637   if (priv->scroll_timer == 0)
3638     priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME,
3639                                                   (GSourceFunc) gtk_combo_box_list_scroll_timeout,
3640                                                    combo_box);
3641
3642   priv->popup_in_progress = TRUE;
3643
3644   return TRUE;
3645 }
3646
3647 static gboolean
3648 gtk_combo_box_list_button_released (GtkWidget      *widget,
3649                                     GdkEventButton *event,
3650                                     gpointer        data)
3651 {
3652   gboolean ret;
3653   GtkTreePath *path = NULL;
3654   GtkTreeIter iter;
3655
3656   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3657   GtkComboBoxPrivate *priv = combo_box->priv;
3658
3659   gboolean popup_in_progress = FALSE;
3660
3661   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3662
3663   if (priv->popup_in_progress)
3664     {
3665       popup_in_progress = TRUE;
3666       priv->popup_in_progress = FALSE;
3667     }
3668
3669   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view),
3670                                   FALSE);
3671   if (priv->scroll_timer)
3672     {
3673       g_source_remove (priv->scroll_timer);
3674       priv->scroll_timer = 0;
3675     }
3676
3677   if (ewidget != priv->tree_view)
3678     {
3679       if ((ewidget == priv->button ||
3680            ewidget == priv->box) &&
3681           !popup_in_progress &&
3682           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3683         {
3684           gtk_combo_box_popdown (combo_box);
3685           return TRUE;
3686         }
3687
3688       /* released outside treeview */
3689       if (ewidget != priv->button && ewidget != priv->box)
3690         {
3691           gtk_combo_box_popdown (combo_box);
3692
3693           return TRUE;
3694         }
3695
3696       return FALSE;
3697     }
3698
3699   /* select something cool */
3700   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3701                                        event->x, event->y,
3702                                        &path,
3703                                        NULL, NULL, NULL);
3704
3705   if (!ret)
3706     return TRUE; /* clicked outside window? */
3707
3708   gtk_tree_model_get_iter (priv->model, &iter, path);
3709   gtk_tree_path_free (path);
3710
3711   gtk_combo_box_popdown (combo_box);
3712
3713   if (tree_column_row_is_sensitive (combo_box, &iter))
3714     gtk_combo_box_set_active_iter (combo_box, &iter);
3715
3716   return TRUE;
3717 }
3718
3719 static gboolean
3720 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3721                               GdkEventKey *event,
3722                               gpointer     data)
3723 {
3724   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3725
3726   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3727     {
3728       /* The menu hasn't managed the
3729        * event, forward it to the combobox
3730        */
3731       gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3732     }
3733
3734   return TRUE;
3735 }
3736
3737 static gboolean
3738 gtk_combo_box_list_key_press (GtkWidget   *widget,
3739                               GdkEventKey *event,
3740                               gpointer     data)
3741 {
3742   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3743   GtkTreeIter iter;
3744
3745   if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter ||
3746       event->keyval == GDK_KEY_space || event->keyval == GDK_KEY_KP_Space)
3747   {
3748     GtkTreeModel *model = NULL;
3749
3750     gtk_combo_box_popdown (combo_box);
3751
3752     if (combo_box->priv->model)
3753       {
3754         GtkTreeSelection *sel;
3755
3756         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3757
3758         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3759           gtk_combo_box_set_active_iter (combo_box, &iter);
3760       }
3761
3762     return TRUE;
3763   }
3764
3765   if (!gtk_bindings_activate_event (G_OBJECT (widget), event))
3766     {
3767       /* The list hasn't managed the
3768        * event, forward it to the combobox
3769        */
3770       gtk_bindings_activate_event (G_OBJECT (combo_box), event);
3771     }
3772
3773   return TRUE;
3774 }
3775
3776 static void
3777 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3778                                 gint         x,
3779                                 gint         y)
3780 {
3781   GtkAdjustment *adj;
3782   GtkAllocation allocation;
3783   GtkWidget *tree_view = combo_box->priv->tree_view;
3784   gdouble value;
3785
3786   gtk_widget_get_allocation (tree_view, &allocation);
3787
3788   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3789   if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3790     {
3791       if (x <= allocation.x &&
3792           gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3793         {
3794           value = gtk_adjustment_get_value (adj) - (allocation.x - x + 1);
3795           gtk_adjustment_set_value (adj, value);
3796         }
3797       else if (x >= allocation.x + allocation.width &&
3798                gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3799         {
3800           value = gtk_adjustment_get_value (adj) + (x - allocation.x - allocation.width + 1);
3801           gtk_adjustment_set_value (adj, MAX (value, 0.0));
3802         }
3803     }
3804
3805   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3806   if (adj && gtk_adjustment_get_upper (adj) - gtk_adjustment_get_lower (adj) > gtk_adjustment_get_page_size (adj))
3807     {
3808       if (y <= allocation.y &&
3809           gtk_adjustment_get_lower (adj) < gtk_adjustment_get_value (adj))
3810         {
3811           value = gtk_adjustment_get_value (adj) - (allocation.y - y + 1);
3812           gtk_adjustment_set_value (adj, value);
3813         }
3814       else if (y >= allocation.height &&
3815                gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj) > gtk_adjustment_get_value (adj))
3816         {
3817           value = gtk_adjustment_get_value (adj) + (y - allocation.height + 1);
3818           gtk_adjustment_set_value (adj, MAX (value, 0.0));
3819         }
3820     }
3821 }
3822
3823 static gboolean
3824 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3825 {
3826   GtkComboBoxPrivate *priv = combo_box->priv;
3827   gint x, y;
3828
3829   if (priv->auto_scroll)
3830     {
3831       gdk_window_get_device_position (gtk_widget_get_window (priv->tree_view),
3832                                       priv->grab_pointer,
3833                                       &x, &y, NULL);
3834       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3835     }
3836
3837   return TRUE;
3838 }
3839
3840 static gboolean
3841 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3842                                  GdkEventCrossing *event,
3843                                  gpointer          data)
3844 {
3845   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3846
3847   combo_box->priv->auto_scroll = TRUE;
3848
3849   return TRUE;
3850 }
3851
3852 static gboolean
3853 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3854                                 GtkTreeModel     *model,
3855                                 GtkTreePath      *path,
3856                                 gboolean          path_currently_selected,
3857                                 gpointer          data)
3858 {
3859   GList *list, *columns;
3860   gboolean sensitive = FALSE;
3861
3862   columns = gtk_tree_view_get_columns (gtk_tree_selection_get_tree_view (selection));
3863
3864   for (list = columns; list && !sensitive; list = list->next)
3865     {
3866       GList *cells, *cell;
3867       gboolean cell_sensitive, cell_visible;
3868       GtkTreeIter iter;
3869       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3870
3871       if (!gtk_tree_view_column_get_visible (column))
3872         continue;
3873
3874       gtk_tree_model_get_iter (model, &iter, path);
3875       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3876                                                FALSE, FALSE);
3877
3878       cell = cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3879       while (cell)
3880         {
3881           g_object_get (cell->data,
3882                         "sensitive", &cell_sensitive,
3883                         "visible", &cell_visible,
3884                         NULL);
3885
3886           if (cell_visible && cell_sensitive)
3887             {
3888               sensitive = TRUE;
3889               break;
3890             }
3891
3892           cell = cell->next;
3893         }
3894
3895       g_list_free (cells);
3896     }
3897
3898   g_list_free (columns);
3899
3900   return sensitive;
3901 }
3902
3903 static void
3904 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3905                                 GtkTreePath  *path,
3906                                 GtkTreeIter  *iter,
3907                                 gpointer      data)
3908 {
3909   /* XXX Do nothing ? */
3910 }
3911
3912 /*
3913  * GtkCellLayout implementation
3914  */
3915 static GtkCellArea *
3916 gtk_combo_box_cell_layout_get_area (GtkCellLayout *cell_layout)
3917 {
3918   GtkComboBox *combo = GTK_COMBO_BOX (cell_layout);
3919   GtkComboBoxPrivate *priv = combo->priv;
3920
3921   if (G_UNLIKELY (!priv->area))
3922     {
3923       priv->area = gtk_cell_area_box_new ();
3924       g_object_ref_sink (priv->area);
3925     }
3926
3927   return priv->area;
3928 }
3929
3930 /*
3931  * public API
3932  */
3933
3934 /**
3935  * gtk_combo_box_new:
3936  *
3937  * Creates a new empty #GtkComboBox.
3938  *
3939  * Return value: A new #GtkComboBox.
3940  *
3941  * Since: 2.4
3942  */
3943 GtkWidget *
3944 gtk_combo_box_new (void)
3945 {
3946   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
3947 }
3948
3949 /**
3950  * gtk_combo_box_new_with_area:
3951  * @area: the #GtkCellArea to use to layout cell renderers
3952  *
3953  * Creates a new empty #GtkComboBox using @area to layout cells.
3954  *
3955  * Return value: A new #GtkComboBox.
3956  */
3957 GtkWidget *
3958 gtk_combo_box_new_with_area (GtkCellArea  *area)
3959 {
3960   return g_object_new (GTK_TYPE_COMBO_BOX, "cell-area", area, NULL);
3961 }
3962
3963 /**
3964  * gtk_combo_box_new_with_area_and_entry:
3965  * @area: the #GtkCellArea to use to layout cell renderers
3966  *
3967  * Creates a new empty #GtkComboBox with an entry.
3968  *
3969  * The new combo box will use @area to layout cells.
3970  *
3971  * Return value: A new #GtkComboBox.
3972  */
3973 GtkWidget *
3974 gtk_combo_box_new_with_area_and_entry (GtkCellArea *area)
3975 {
3976   return g_object_new (GTK_TYPE_COMBO_BOX,
3977                        "has-entry", TRUE,
3978                        "cell-area", area,
3979                        NULL);
3980 }
3981
3982
3983 /**
3984  * gtk_combo_box_new_with_entry:
3985  *
3986  * Creates a new empty #GtkComboBox with an entry.
3987  *
3988  * Return value: A new #GtkComboBox.
3989  */
3990 GtkWidget *
3991 gtk_combo_box_new_with_entry (void)
3992 {
3993   return g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
3994 }
3995
3996 /**
3997  * gtk_combo_box_new_with_model:
3998  * @model: A #GtkTreeModel.
3999  *
4000  * Creates a new #GtkComboBox with the model initialized to @model.
4001  *
4002  * Return value: A new #GtkComboBox.
4003  *
4004  * Since: 2.4
4005  */
4006 GtkWidget *
4007 gtk_combo_box_new_with_model (GtkTreeModel *model)
4008 {
4009   GtkComboBox *combo_box;
4010
4011   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4012
4013   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4014
4015   return GTK_WIDGET (combo_box);
4016 }
4017
4018 /**
4019  * gtk_combo_box_new_with_model_and_entry:
4020  * @model: A #GtkTreeModel
4021  *
4022  * Creates a new empty #GtkComboBox with an entry
4023  * and with the model initialized to @model.
4024  *
4025  * Return value: A new #GtkComboBox
4026  */
4027 GtkWidget *
4028 gtk_combo_box_new_with_model_and_entry (GtkTreeModel *model)
4029 {
4030   return g_object_new (GTK_TYPE_COMBO_BOX,
4031                        "has-entry", TRUE,
4032                        "model", model,
4033                        NULL);
4034 }
4035
4036 /**
4037  * gtk_combo_box_get_wrap_width:
4038  * @combo_box: A #GtkComboBox
4039  *
4040  * Returns the wrap width which is used to determine the number of columns
4041  * for the popup menu. If the wrap width is larger than 1, the combo box
4042  * is in table mode.
4043  *
4044  * Returns: the wrap width.
4045  *
4046  * Since: 2.6
4047  */
4048 gint
4049 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4050 {
4051   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4052
4053   return combo_box->priv->wrap_width;
4054 }
4055
4056 /**
4057  * gtk_combo_box_set_wrap_width:
4058  * @combo_box: A #GtkComboBox
4059  * @width: Preferred number of columns
4060  *
4061  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4062  * the preferred number of columns when you want the popup to be layed out
4063  * in a table.
4064  *
4065  * Since: 2.4
4066  */
4067 void
4068 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4069                               gint         width)
4070 {
4071   GtkComboBoxPrivate *priv;
4072
4073   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4074   g_return_if_fail (width >= 0);
4075
4076   priv = combo_box->priv;
4077
4078   if (width != priv->wrap_width)
4079     {
4080       priv->wrap_width = width;
4081
4082       gtk_combo_box_check_appearance (combo_box);
4083
4084       if (GTK_IS_TREE_MENU (priv->popup_widget))
4085         _gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (priv->popup_widget), priv->wrap_width);
4086
4087       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4088     }
4089 }
4090
4091 /**
4092  * gtk_combo_box_get_row_span_column:
4093  * @combo_box: A #GtkComboBox
4094  *
4095  * Returns the column with row span information for @combo_box.
4096  *
4097  * Returns: the row span column.
4098  *
4099  * Since: 2.6
4100  */
4101 gint
4102 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4103 {
4104   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4105
4106   return combo_box->priv->row_column;
4107 }
4108
4109 /**
4110  * gtk_combo_box_set_row_span_column:
4111  * @combo_box: A #GtkComboBox.
4112  * @row_span: A column in the model passed during construction.
4113  *
4114  * Sets the column with row span information for @combo_box to be @row_span.
4115  * The row span column contains integers which indicate how many rows
4116  * an item should span.
4117  *
4118  * Since: 2.4
4119  */
4120 void
4121 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4122                                    gint         row_span)
4123 {
4124   GtkComboBoxPrivate *priv;
4125   gint col;
4126
4127   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4128
4129   priv = combo_box->priv;
4130
4131   col = gtk_tree_model_get_n_columns (priv->model);
4132   g_return_if_fail (row_span >= -1 && row_span < col);
4133
4134   if (row_span != priv->row_column)
4135     {
4136       priv->row_column = row_span;
4137
4138       if (GTK_IS_TREE_MENU (priv->popup_widget))
4139         _gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (priv->popup_widget), priv->row_column);
4140
4141       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4142     }
4143 }
4144
4145 /**
4146  * gtk_combo_box_get_column_span_column:
4147  * @combo_box: A #GtkComboBox
4148  *
4149  * Returns the column with column span information for @combo_box.
4150  *
4151  * Returns: the column span column.
4152  *
4153  * Since: 2.6
4154  */
4155 gint
4156 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4157 {
4158   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4159
4160   return combo_box->priv->col_column;
4161 }
4162
4163 /**
4164  * gtk_combo_box_set_column_span_column:
4165  * @combo_box: A #GtkComboBox
4166  * @column_span: A column in the model passed during construction
4167  *
4168  * Sets the column with column span information for @combo_box to be
4169  * @column_span. The column span column contains integers which indicate
4170  * how many columns an item should span.
4171  *
4172  * Since: 2.4
4173  */
4174 void
4175 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4176                                       gint         column_span)
4177 {
4178   GtkComboBoxPrivate *priv;
4179   gint col;
4180
4181   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4182
4183   priv = combo_box->priv;
4184
4185   col = gtk_tree_model_get_n_columns (priv->model);
4186   g_return_if_fail (column_span >= -1 && column_span < col);
4187
4188   if (column_span != priv->col_column)
4189     {
4190       priv->col_column = column_span;
4191
4192       if (GTK_IS_TREE_MENU (priv->popup_widget))
4193         _gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (priv->popup_widget), priv->col_column);
4194
4195       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4196     }
4197 }
4198
4199 /**
4200  * gtk_combo_box_get_active:
4201  * @combo_box: A #GtkComboBox
4202  *
4203  * Returns the index of the currently active item, or -1 if there's no
4204  * active item. If the model is a non-flat treemodel, and the active item
4205  * is not an immediate child of the root of the tree, this function returns
4206  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where
4207  * <literal>path</literal> is the #GtkTreePath of the active item.
4208  *
4209  * Return value: An integer which is the index of the currently active item,
4210  *     or -1 if there's no active item.
4211  *
4212  * Since: 2.4
4213  */
4214 gint
4215 gtk_combo_box_get_active (GtkComboBox *combo_box)
4216 {
4217   GtkComboBoxPrivate *priv;
4218   gint result;
4219
4220   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4221
4222   priv = combo_box->priv;
4223
4224   if (gtk_tree_row_reference_valid (priv->active_row))
4225     {
4226       GtkTreePath *path;
4227
4228       path = gtk_tree_row_reference_get_path (priv->active_row);
4229       result = gtk_tree_path_get_indices (path)[0];
4230       gtk_tree_path_free (path);
4231     }
4232   else
4233     result = -1;
4234
4235   return result;
4236 }
4237
4238 /**
4239  * gtk_combo_box_set_active:
4240  * @combo_box: A #GtkComboBox
4241  * @index_: An index in the model passed during construction, or -1 to have
4242  * no active item
4243  *
4244  * Sets the active item of @combo_box to be the item at @index.
4245  *
4246  * Since: 2.4
4247  */
4248 void
4249 gtk_combo_box_set_active (GtkComboBox *combo_box,
4250                           gint         index_)
4251 {
4252   GtkTreePath *path = NULL;
4253   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4254   g_return_if_fail (index_ >= -1);
4255
4256   if (combo_box->priv->model == NULL)
4257     {
4258       /* Save index, in case the model is set after the index */
4259       combo_box->priv->active = index_;
4260       if (index_ != -1)
4261         return;
4262     }
4263
4264   if (index_ != -1)
4265     path = gtk_tree_path_new_from_indices (index_, -1);
4266
4267   gtk_combo_box_set_active_internal (combo_box, path);
4268
4269   if (path)
4270     gtk_tree_path_free (path);
4271 }
4272
4273 static void
4274 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4275                                    GtkTreePath *path)
4276 {
4277   GtkComboBoxPrivate *priv = combo_box->priv;
4278   GtkTreePath *active_path;
4279   gint path_cmp;
4280
4281   /* Remember whether the initially active row is valid. */
4282   gboolean is_valid_row_reference = gtk_tree_row_reference_valid (priv->active_row);
4283
4284   if (path && is_valid_row_reference)
4285     {
4286       active_path = gtk_tree_row_reference_get_path (priv->active_row);
4287       path_cmp = gtk_tree_path_compare (path, active_path);
4288       gtk_tree_path_free (active_path);
4289       if (path_cmp == 0)
4290         return;
4291     }
4292
4293   if (priv->active_row)
4294     {
4295       gtk_tree_row_reference_free (priv->active_row);
4296       priv->active_row = NULL;
4297     }
4298
4299   if (!path)
4300     {
4301       if (priv->tree_view)
4302         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4303       else
4304         {
4305           GtkMenu *menu = GTK_MENU (priv->popup_widget);
4306
4307           if (GTK_IS_MENU (menu))
4308             gtk_menu_set_active (menu, -1);
4309         }
4310
4311       if (priv->cell_view)
4312         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4313
4314       /*
4315        *  Do not emit a "changed" signal when an already invalid selection was
4316        *  now set to invalid.
4317        */
4318       if (!is_valid_row_reference)
4319         return;
4320     }
4321   else
4322     {
4323       priv->active_row =
4324         gtk_tree_row_reference_new (priv->model, path);
4325
4326       if (priv->tree_view)
4327         {
4328           gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
4329                                     path, NULL, FALSE);
4330         }
4331       else if (GTK_IS_MENU (priv->popup_widget))
4332         {
4333           /* FIXME handle nested menus better */
4334           gtk_menu_set_active (GTK_MENU (priv->popup_widget),
4335                                gtk_tree_path_get_indices (path)[0]);
4336         }
4337
4338       if (priv->cell_view)
4339         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view),
4340                                          path);
4341     }
4342
4343   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4344   g_object_notify (G_OBJECT (combo_box), "active");
4345   if (combo_box->priv->id_column >= 0)
4346     g_object_notify (G_OBJECT (combo_box), "active-id");
4347 }
4348
4349
4350 /**
4351  * gtk_combo_box_get_active_iter:
4352  * @combo_box: A #GtkComboBox
4353  * @iter: (out): The uninitialized #GtkTreeIter
4354  *
4355  * Sets @iter to point to the current active item, if it exists.
4356  *
4357  * Return value: %TRUE, if @iter was set
4358  *
4359  * Since: 2.4
4360  */
4361 gboolean
4362 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4363                                GtkTreeIter     *iter)
4364 {
4365   GtkTreePath *path;
4366   gboolean result;
4367
4368   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4369
4370   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4371     return FALSE;
4372
4373   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4374   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4375   gtk_tree_path_free (path);
4376
4377   return result;
4378 }
4379
4380 /**
4381  * gtk_combo_box_set_active_iter:
4382  * @combo_box: A #GtkComboBox
4383  * @iter: (allow-none): The #GtkTreeIter, or %NULL
4384  *
4385  * Sets the current active item to be the one referenced by @iter, or
4386  * unsets the active item if @iter is %NULL.
4387  *
4388  * Since: 2.4
4389  */
4390 void
4391 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4392                                GtkTreeIter     *iter)
4393 {
4394   GtkTreePath *path = NULL;
4395
4396   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4397
4398   if (iter)
4399     path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4400
4401   gtk_combo_box_set_active_internal (combo_box, path);
4402   gtk_tree_path_free (path);
4403 }
4404
4405 /**
4406  * gtk_combo_box_set_model:
4407  * @combo_box: A #GtkComboBox
4408  * @model: (allow-none): A #GtkTreeModel
4409  *
4410  * Sets the model used by @combo_box to be @model. Will unset a previously set
4411  * model (if applicable). If model is %NULL, then it will unset the model.
4412  *
4413  * Note that this function does not clear the cell renderers, you have to
4414  * call gtk_cell_layout_clear() yourself if you need to set up different
4415  * cell renderers for the new model.
4416  *
4417  * Since: 2.4
4418  */
4419 void
4420 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4421                          GtkTreeModel *model)
4422 {
4423   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4424   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4425
4426   if (model == combo_box->priv->model)
4427     return;
4428
4429   gtk_combo_box_unset_model (combo_box);
4430
4431   if (model == NULL)
4432     goto out;
4433
4434   combo_box->priv->model = model;
4435   g_object_ref (combo_box->priv->model);
4436
4437   combo_box->priv->inserted_id =
4438     g_signal_connect (combo_box->priv->model, "row-inserted",
4439                       G_CALLBACK (gtk_combo_box_model_row_inserted),
4440                       combo_box);
4441   combo_box->priv->deleted_id =
4442     g_signal_connect (combo_box->priv->model, "row-deleted",
4443                       G_CALLBACK (gtk_combo_box_model_row_deleted),
4444                       combo_box);
4445   combo_box->priv->reordered_id =
4446     g_signal_connect (combo_box->priv->model, "rows-reordered",
4447                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
4448                       combo_box);
4449   combo_box->priv->changed_id =
4450     g_signal_connect (combo_box->priv->model, "row-changed",
4451                       G_CALLBACK (gtk_combo_box_model_row_changed),
4452                       combo_box);
4453
4454   if (combo_box->priv->tree_view)
4455     {
4456       /* list mode */
4457       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4458                                combo_box->priv->model);
4459       gtk_combo_box_list_popup_resize (combo_box);
4460     }
4461
4462   if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4463     {
4464       /* menu mode */
4465       _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget),
4466                                 combo_box->priv->model);
4467     }
4468
4469   if (combo_box->priv->cell_view)
4470     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4471                              combo_box->priv->model);
4472
4473   if (combo_box->priv->active != -1)
4474     {
4475       /* If an index was set in advance, apply it now */
4476       gtk_combo_box_set_active (combo_box, combo_box->priv->active);
4477       combo_box->priv->active = -1;
4478     }
4479
4480 out:
4481   gtk_combo_box_update_sensitivity (combo_box);
4482
4483   g_object_notify (G_OBJECT (combo_box), "model");
4484 }
4485
4486 /**
4487  * gtk_combo_box_get_model:
4488  * @combo_box: A #GtkComboBox
4489  *
4490  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4491  *
4492  * Return value: (transfer none): A #GtkTreeModel which was passed
4493  *     during construction.
4494  *
4495  * Since: 2.4
4496  */
4497 GtkTreeModel *
4498 gtk_combo_box_get_model (GtkComboBox *combo_box)
4499 {
4500   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4501
4502   return combo_box->priv->model;
4503 }
4504
4505 static void
4506 gtk_combo_box_real_move_active (GtkComboBox   *combo_box,
4507                                 GtkScrollType  scroll)
4508 {
4509   GtkTreeIter iter;
4510   GtkTreeIter new_iter;
4511   gboolean    active_iter;
4512   gboolean    found;
4513
4514   if (!combo_box->priv->model)
4515     {
4516       gtk_widget_error_bell (GTK_WIDGET (combo_box));
4517       return;
4518     }
4519
4520   active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4521
4522   switch (scroll)
4523     {
4524     case GTK_SCROLL_STEP_BACKWARD:
4525     case GTK_SCROLL_STEP_UP:
4526     case GTK_SCROLL_STEP_LEFT:
4527       if (active_iter)
4528         {
4529           found = tree_prev (combo_box, combo_box->priv->model,
4530                              &iter, &new_iter, FALSE);
4531           break;
4532         }
4533       /* else fall through */
4534
4535     case GTK_SCROLL_PAGE_FORWARD:
4536     case GTK_SCROLL_PAGE_DOWN:
4537     case GTK_SCROLL_PAGE_RIGHT:
4538     case GTK_SCROLL_END:
4539       found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
4540       break;
4541
4542     case GTK_SCROLL_STEP_FORWARD:
4543     case GTK_SCROLL_STEP_DOWN:
4544     case GTK_SCROLL_STEP_RIGHT:
4545       if (active_iter)
4546         {
4547           found = tree_next (combo_box, combo_box->priv->model,
4548                              &iter, &new_iter, FALSE);
4549           break;
4550         }
4551       /* else fall through */
4552
4553     case GTK_SCROLL_PAGE_BACKWARD:
4554     case GTK_SCROLL_PAGE_UP:
4555     case GTK_SCROLL_PAGE_LEFT:
4556     case GTK_SCROLL_START:
4557       found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
4558       break;
4559
4560     default:
4561       return;
4562     }
4563
4564   if (found && active_iter)
4565     {
4566       GtkTreePath *old_path;
4567       GtkTreePath *new_path;
4568
4569       old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
4570       new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
4571
4572       if (gtk_tree_path_compare (old_path, new_path) == 0)
4573         found = FALSE;
4574
4575       gtk_tree_path_free (old_path);
4576       gtk_tree_path_free (new_path);
4577     }
4578
4579   if (found)
4580     {
4581       gtk_combo_box_set_active_iter (combo_box, &new_iter);
4582     }
4583   else
4584     {
4585       gtk_widget_error_bell (GTK_WIDGET (combo_box));
4586     }
4587 }
4588
4589 static gboolean
4590 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4591                                  gboolean   group_cycling)
4592 {
4593   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4594
4595   if (combo_box->priv->has_entry)
4596     {
4597       GtkWidget* child;
4598
4599       child = gtk_bin_get_child (GTK_BIN (combo_box));
4600       if (child)
4601         gtk_widget_grab_focus (child);
4602     }
4603   else
4604     gtk_widget_grab_focus (combo_box->priv->button);
4605
4606   return TRUE;
4607 }
4608
4609 static void
4610 gtk_combo_box_grab_focus (GtkWidget *widget)
4611 {
4612   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4613
4614   if (combo_box->priv->has_entry)
4615     {
4616       GtkWidget *child;
4617
4618       child = gtk_bin_get_child (GTK_BIN (combo_box));
4619       if (child)
4620         gtk_widget_grab_focus (child);
4621     }
4622   else
4623     gtk_widget_grab_focus (combo_box->priv->button);
4624 }
4625
4626 static void
4627 gtk_combo_box_destroy (GtkWidget *widget)
4628 {
4629   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4630
4631   if (combo_box->priv->popup_idle_id > 0)
4632     {
4633       g_source_remove (combo_box->priv->popup_idle_id);
4634       combo_box->priv->popup_idle_id = 0;
4635     }
4636
4637   gtk_combo_box_popdown (combo_box);
4638
4639   if (combo_box->priv->row_separator_destroy)
4640     combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
4641
4642   combo_box->priv->row_separator_func = NULL;
4643   combo_box->priv->row_separator_data = NULL;
4644   combo_box->priv->row_separator_destroy = NULL;
4645
4646   GTK_WIDGET_CLASS (gtk_combo_box_parent_class)->destroy (widget);
4647   combo_box->priv->cell_view = NULL;
4648 }
4649
4650 static void
4651 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
4652                                       gpointer  user_data)
4653 {
4654   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
4655
4656   /*
4657    *  Fixes regression reported in bug #574059. The old functionality relied on
4658    *  bug #572478.  As a bugfix, we now emit the "changed" signal ourselves
4659    *  when the selection was already set to -1.
4660    */
4661   if (gtk_combo_box_get_active(combo_box) == -1)
4662     g_signal_emit_by_name (combo_box, "changed");
4663   else
4664     gtk_combo_box_set_active (combo_box, -1);
4665 }
4666
4667 static void
4668 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
4669                                     gpointer     user_data)
4670 {
4671   GtkTreeModel *model;
4672   GtkTreeIter iter;
4673
4674   if (gtk_combo_box_get_active_iter (combo_box, &iter))
4675     {
4676       GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
4677
4678       if (entry)
4679         {
4680           GtkTreePath *path;
4681           gchar       *path_str;
4682           gchar       *text = NULL;
4683
4684           model    = gtk_combo_box_get_model (combo_box);
4685           path     = gtk_tree_model_get_path (model, &iter);
4686           path_str = gtk_tree_path_to_string (path);
4687
4688           g_signal_handlers_block_by_func (entry,
4689                                            gtk_combo_box_entry_contents_changed,
4690                                            combo_box);
4691
4692
4693           g_signal_emit (combo_box, combo_box_signals[FORMAT_ENTRY_TEXT], 0, 
4694                          path_str, &text);
4695
4696           gtk_entry_set_text (entry, text);
4697
4698           g_signal_handlers_unblock_by_func (entry,
4699                                              gtk_combo_box_entry_contents_changed,
4700                                              combo_box);
4701
4702           gtk_tree_path_free (path);
4703           g_free (text);
4704           g_free (path_str);
4705         }
4706     }
4707 }
4708
4709 static gchar *
4710 gtk_combo_box_format_entry_text (GtkComboBox     *combo_box,
4711                                  const gchar     *path)
4712 {
4713   GtkComboBoxPrivate *priv = combo_box->priv;
4714   GtkTreeModel       *model;
4715   GtkTreeIter         iter;
4716   gchar              *text = NULL;
4717
4718   if (priv->text_column >= 0)
4719     {
4720       model = gtk_combo_box_get_model (combo_box);
4721       gtk_tree_model_get_iter_from_string (model, &iter, path);
4722
4723       gtk_tree_model_get (model, &iter,
4724                           priv->text_column, &text,
4725                           -1);
4726     }
4727
4728   return text;
4729 }
4730
4731
4732 static GObject *
4733 gtk_combo_box_constructor (GType                  type,
4734                            guint                  n_construct_properties,
4735                            GObjectConstructParam *construct_properties)
4736 {
4737   GObject            *object;
4738   GtkComboBox        *combo_box;
4739   GtkComboBoxPrivate *priv;
4740
4741   object = G_OBJECT_CLASS (gtk_combo_box_parent_class)->constructor
4742     (type, n_construct_properties, construct_properties);
4743
4744   combo_box = GTK_COMBO_BOX (object);
4745   priv      = combo_box->priv;
4746
4747   if (!priv->area)
4748     {
4749       priv->area = gtk_cell_area_box_new ();
4750       g_object_ref_sink (priv->area);
4751     }
4752
4753   priv->cell_view = gtk_cell_view_new_with_context (priv->area, NULL);
4754   gtk_cell_view_set_fit_model (GTK_CELL_VIEW (priv->cell_view), TRUE);
4755   gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), priv->model);
4756   gtk_widget_set_parent (priv->cell_view, GTK_WIDGET (combo_box));
4757   _gtk_bin_set_child (GTK_BIN (combo_box), priv->cell_view);
4758   gtk_widget_show (priv->cell_view);
4759
4760   gtk_combo_box_check_appearance (combo_box);
4761
4762   if (priv->has_entry)
4763     {
4764       GtkWidget *entry;
4765       GtkStyleContext *context;
4766
4767       entry = gtk_entry_new ();
4768       gtk_widget_show (entry);
4769       gtk_container_add (GTK_CONTAINER (combo_box), entry);
4770
4771       context = gtk_widget_get_style_context (GTK_WIDGET (combo_box));
4772       gtk_style_context_add_class (context, GTK_STYLE_CLASS_COMBOBOX_ENTRY);
4773
4774       priv->text_renderer = gtk_cell_renderer_text_new ();
4775       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
4776                                   priv->text_renderer, TRUE);
4777
4778       gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
4779
4780       g_signal_connect (combo_box, "changed",
4781                         G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
4782     }
4783
4784   return object;
4785 }
4786
4787
4788 static void
4789 gtk_combo_box_dispose(GObject* object)
4790 {
4791   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4792
4793   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4794     {
4795       gtk_combo_box_menu_destroy (combo_box);
4796       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4797       combo_box->priv->popup_widget = NULL;
4798     }
4799
4800   if (combo_box->priv->area)
4801     {
4802       g_object_unref (combo_box->priv->area);
4803       combo_box->priv->area = NULL;
4804     }
4805
4806   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4807     gtk_combo_box_list_destroy (combo_box);
4808
4809   if (combo_box->priv->popup_window)
4810     {
4811       gtk_widget_destroy (combo_box->priv->popup_window);
4812       combo_box->priv->popup_window = NULL;
4813     }
4814
4815   gtk_combo_box_unset_model (combo_box);
4816
4817   G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
4818 }
4819
4820 static void
4821 gtk_combo_box_finalize (GObject *object)
4822 {
4823   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4824
4825   g_free (combo_box->priv->tearoff_title);
4826
4827   G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
4828 }
4829
4830 static gboolean
4831 gtk_cell_editable_key_press (GtkWidget   *widget,
4832                              GdkEventKey *event,
4833                              gpointer     data)
4834 {
4835   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4836
4837   if (event->keyval == GDK_KEY_Escape)
4838     {
4839       g_object_set (combo_box,
4840                     "editing-canceled", TRUE,
4841                     NULL);
4842       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4843       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4844
4845       return TRUE;
4846     }
4847   else if (event->keyval == GDK_KEY_Return ||
4848            event->keyval == GDK_KEY_ISO_Enter ||
4849            event->keyval == GDK_KEY_KP_Enter)
4850     {
4851       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4852       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4853
4854       return TRUE;
4855     }
4856
4857   return FALSE;
4858 }
4859
4860 static gboolean
4861 popdown_idle (gpointer data)
4862 {
4863   GtkComboBox *combo_box;
4864
4865   combo_box = GTK_COMBO_BOX (data);
4866
4867   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
4868   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
4869
4870   g_object_unref (combo_box);
4871
4872   return FALSE;
4873 }
4874
4875 static void
4876 popdown_handler (GtkWidget *widget,
4877                  gpointer   data)
4878 {
4879   gdk_threads_add_idle (popdown_idle, g_object_ref (data));
4880 }
4881
4882 static gboolean
4883 popup_idle (gpointer data)
4884 {
4885   GtkComboBox *combo_box;
4886
4887   combo_box = GTK_COMBO_BOX (data);
4888
4889   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
4890       combo_box->priv->cell_view)
4891     g_signal_connect_object (combo_box->priv->popup_widget,
4892                              "unmap", G_CALLBACK (popdown_handler),
4893                              combo_box, 0);
4894
4895   /* we unset this if a menu item is activated */
4896   g_object_set (combo_box,
4897                 "editing-canceled", TRUE,
4898                 NULL);
4899   gtk_combo_box_popup (combo_box);
4900
4901   combo_box->priv->popup_idle_id = 0;
4902   combo_box->priv->activate_button = 0;
4903   combo_box->priv->activate_time = 0;
4904
4905   return FALSE;
4906 }
4907
4908 static void
4909 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
4910                              GdkEvent        *event)
4911 {
4912   GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
4913   GtkWidget *child;
4914
4915   combo_box->priv->is_cell_renderer = TRUE;
4916
4917   if (combo_box->priv->cell_view)
4918     {
4919       g_signal_connect_object (combo_box->priv->button, "key-press-event",
4920                                G_CALLBACK (gtk_cell_editable_key_press),
4921                                cell_editable, 0);
4922
4923       gtk_widget_grab_focus (combo_box->priv->button);
4924     }
4925   else
4926     {
4927       child = gtk_bin_get_child (GTK_BIN (combo_box));
4928
4929       g_signal_connect_object (child, "key-press-event",
4930                                G_CALLBACK (gtk_cell_editable_key_press),
4931                                cell_editable, 0);
4932
4933       gtk_widget_grab_focus (child);
4934       gtk_widget_set_can_focus (combo_box->priv->button, FALSE);
4935     }
4936
4937   /* we do the immediate popup only for the optionmenu-like
4938    * appearance
4939    */
4940   if (combo_box->priv->is_cell_renderer &&
4941       combo_box->priv->cell_view && !combo_box->priv->tree_view)
4942     {
4943       if (event && event->type == GDK_BUTTON_PRESS)
4944         {
4945           GdkEventButton *event_button = (GdkEventButton *)event;
4946
4947           combo_box->priv->activate_button = event_button->button;
4948           combo_box->priv->activate_time = event_button->time;
4949         }
4950
4951       combo_box->priv->popup_idle_id =
4952           gdk_threads_add_idle (popup_idle, combo_box);
4953     }
4954 }
4955
4956
4957 /**
4958  * gtk_combo_box_get_add_tearoffs:
4959  * @combo_box: a #GtkComboBox
4960  *
4961  * Gets the current value of the :add-tearoffs property.
4962  *
4963  * Return value: the current value of the :add-tearoffs property.
4964  */
4965 gboolean
4966 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
4967 {
4968   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4969
4970   return combo_box->priv->add_tearoffs;
4971 }
4972
4973 /**
4974  * gtk_combo_box_set_add_tearoffs:
4975  * @combo_box: a #GtkComboBox
4976  * @add_tearoffs: %TRUE to add tearoff menu items
4977  *
4978  * Sets whether the popup menu should have a tearoff
4979  * menu item.
4980  *
4981  * Since: 2.6
4982  */
4983 void
4984 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
4985                                 gboolean     add_tearoffs)
4986 {
4987   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4988
4989   add_tearoffs = add_tearoffs != FALSE;
4990
4991   if (combo_box->priv->add_tearoffs != add_tearoffs)
4992     {
4993       combo_box->priv->add_tearoffs = add_tearoffs;
4994       gtk_combo_box_check_appearance (combo_box);
4995
4996       if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
4997         _gtk_tree_menu_set_tearoff (GTK_TREE_MENU (combo_box->priv->popup_widget),
4998                                     combo_box->priv->add_tearoffs);
4999
5000       g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5001     }
5002 }
5003
5004 /**
5005  * gtk_combo_box_get_title:
5006  * @combo_box: a #GtkComboBox
5007  *
5008  * Gets the current title of the menu in tearoff mode. See
5009  * gtk_combo_box_set_add_tearoffs().
5010  *
5011  * Returns: the menu's title in tearoff mode. This is an internal copy of the
5012  * string which must not be freed.
5013  *
5014  * Since: 2.10
5015  */
5016 const gchar*
5017 gtk_combo_box_get_title (GtkComboBox *combo_box)
5018 {
5019   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5020
5021   return combo_box->priv->tearoff_title;
5022 }
5023
5024 static void
5025 gtk_combo_box_update_title (GtkComboBox *combo_box)
5026 {
5027   gtk_combo_box_check_appearance (combo_box);
5028
5029   if (combo_box->priv->popup_widget &&
5030       GTK_IS_MENU (combo_box->priv->popup_widget))
5031     gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget),
5032                         combo_box->priv->tearoff_title);
5033 }
5034
5035 /**
5036  * gtk_combo_box_set_title:
5037  * @combo_box: a #GtkComboBox
5038  * @title: a title for the menu in tearoff mode
5039  *
5040  * Sets the menu's title in tearoff mode.
5041  *
5042  * Since: 2.10
5043  */
5044 void
5045 gtk_combo_box_set_title (GtkComboBox *combo_box,
5046                          const gchar *title)
5047 {
5048   GtkComboBoxPrivate *priv;
5049
5050   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5051
5052   priv = combo_box->priv;
5053
5054   if (strcmp (title ? title : "",
5055               priv->tearoff_title ? priv->tearoff_title : "") != 0)
5056     {
5057       g_free (priv->tearoff_title);
5058       priv->tearoff_title = g_strdup (title);
5059
5060       gtk_combo_box_update_title (combo_box);
5061
5062       g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5063     }
5064 }
5065
5066
5067 /**
5068  * gtk_combo_box_set_popup_fixed_width:
5069  * @combo_box: a #GtkComboBox
5070  * @fixed: whether to use a fixed popup width
5071  *
5072  * Specifies whether the popup's width should be a fixed width
5073  * matching the allocated width of the combo box.
5074  *
5075  * Since: 3.0
5076  **/
5077 void
5078 gtk_combo_box_set_popup_fixed_width (GtkComboBox *combo_box,
5079                                      gboolean     fixed)
5080 {
5081   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5082
5083   if (combo_box->priv->popup_fixed_width != fixed)
5084     {
5085       combo_box->priv->popup_fixed_width = fixed;
5086
5087       g_object_notify (G_OBJECT (combo_box), "popup-fixed-width");
5088     }
5089 }
5090
5091 /**
5092  * gtk_combo_box_get_popup_fixed_width:
5093  * @combo_box: a #GtkComboBox
5094  *
5095  * Gets whether the popup uses a fixed width matching
5096  * the allocated width of the combo box.
5097  *
5098  * Returns: %TRUE if the popup uses a fixed width
5099  *
5100  * Since: 3.0
5101  **/
5102 gboolean
5103 gtk_combo_box_get_popup_fixed_width (GtkComboBox *combo_box)
5104 {
5105   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5106
5107   return combo_box->priv->popup_fixed_width;
5108 }
5109
5110
5111 /**
5112  * gtk_combo_box_get_popup_accessible:
5113  * @combo_box: a #GtkComboBox
5114  *
5115  * Gets the accessible object corresponding to the combo box's popup.
5116  *
5117  * This function is mostly intended for use by accessibility technologies;
5118  * applications should have little use for it.
5119  *
5120  * Returns: (transfer none): the accessible object corresponding
5121  *     to the combo box's popup.
5122  *
5123  * Since: 2.6
5124  */
5125 AtkObject*
5126 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5127 {
5128   AtkObject *atk_obj;
5129
5130   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5131
5132   if (combo_box->priv->popup_widget)
5133     {
5134       atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5135       return atk_obj;
5136     }
5137
5138   return NULL;
5139 }
5140
5141 /**
5142  * gtk_combo_box_get_row_separator_func: (skip)
5143  * @combo_box: a #GtkComboBox
5144  *
5145  * Returns the current row separator function.
5146  *
5147  * Return value: the current row separator function.
5148  *
5149  * Since: 2.6
5150  */
5151 GtkTreeViewRowSeparatorFunc
5152 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5153 {
5154   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5155
5156   return combo_box->priv->row_separator_func;
5157 }
5158
5159 /**
5160  * gtk_combo_box_set_row_separator_func:
5161  * @combo_box: a #GtkComboBox
5162  * @func: a #GtkTreeViewRowSeparatorFunc
5163  * @data: (allow-none): user data to pass to @func, or %NULL
5164  * @destroy: (allow-none): destroy notifier for @data, or %NULL
5165  *
5166  * Sets the row separator function, which is used to determine
5167  * whether a row should be drawn as a separator. If the row separator
5168  * function is %NULL, no separators are drawn. This is the default value.
5169  *
5170  * Since: 2.6
5171  */
5172 void
5173 gtk_combo_box_set_row_separator_func (GtkComboBox                 *combo_box,
5174                                       GtkTreeViewRowSeparatorFunc  func,
5175                                       gpointer                     data,
5176                                       GDestroyNotify               destroy)
5177 {
5178   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5179
5180   if (combo_box->priv->row_separator_destroy)
5181     combo_box->priv->row_separator_destroy (combo_box->priv->row_separator_data);
5182
5183   combo_box->priv->row_separator_func = func;
5184   combo_box->priv->row_separator_data = data;
5185   combo_box->priv->row_separator_destroy = destroy;
5186
5187   /* Provoke the underlying treeview/menu to rebuild themselves with the new separator func */
5188   if (combo_box->priv->tree_view)
5189     {
5190       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), NULL);
5191       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view), combo_box->priv->model);
5192     }
5193
5194   if (GTK_IS_TREE_MENU (combo_box->priv->popup_widget))
5195     {
5196       _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), NULL);
5197       _gtk_tree_menu_set_model (GTK_TREE_MENU (combo_box->priv->popup_widget), combo_box->priv->model);
5198     }
5199
5200   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5201 }
5202
5203 /**
5204  * gtk_combo_box_set_button_sensitivity:
5205  * @combo_box: a #GtkComboBox
5206  * @sensitivity: specify the sensitivity of the dropdown button
5207  *
5208  * Sets whether the dropdown button of the combo box should be
5209  * always sensitive (%GTK_SENSITIVITY_ON), never sensitive (%GTK_SENSITIVITY_OFF)
5210  * or only if there is at least one item to display (%GTK_SENSITIVITY_AUTO).
5211  *
5212  * Since: 2.14
5213  **/
5214 void
5215 gtk_combo_box_set_button_sensitivity (GtkComboBox        *combo_box,
5216                                       GtkSensitivityType  sensitivity)
5217 {
5218   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5219
5220   if (combo_box->priv->button_sensitivity != sensitivity)
5221     {
5222       combo_box->priv->button_sensitivity = sensitivity;
5223       gtk_combo_box_update_sensitivity (combo_box);
5224
5225       g_object_notify (G_OBJECT (combo_box), "button-sensitivity");
5226     }
5227 }
5228
5229 /**
5230  * gtk_combo_box_get_button_sensitivity:
5231  * @combo_box: a #GtkComboBox
5232  *
5233  * Returns whether the combo box sets the dropdown button
5234  * sensitive or not when there are no items in the model.
5235  *
5236  * Return Value: %GTK_SENSITIVITY_ON if the dropdown button
5237  *    is sensitive when the model is empty, %GTK_SENSITIVITY_OFF
5238  *    if the button is always insensitive or
5239  *    %GTK_SENSITIVITY_AUTO if it is only sensitive as long as
5240  *    the model has one item to be selected.
5241  *
5242  * Since: 2.14
5243  **/
5244 GtkSensitivityType
5245 gtk_combo_box_get_button_sensitivity (GtkComboBox *combo_box)
5246 {
5247   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5248
5249   return combo_box->priv->button_sensitivity;
5250 }
5251
5252
5253 /**
5254  * gtk_combo_box_get_has_entry:
5255  * @combo_box: a #GtkComboBox
5256  *
5257  * Returns whether the combo box has an entry.
5258  *
5259  * Return Value: whether there is an entry in @combo_box.
5260  *
5261  * Since: 2.24
5262  **/
5263 gboolean
5264 gtk_combo_box_get_has_entry (GtkComboBox *combo_box)
5265 {
5266   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5267
5268   return combo_box->priv->has_entry;
5269 }
5270
5271 /**
5272  * gtk_combo_box_set_entry_text_column:
5273  * @combo_box: A #GtkComboBox
5274  * @text_column: A column in @model to get the strings from for
5275  *     the internal entry
5276  *
5277  * Sets the model column which @combo_box should use to get strings from
5278  * to be @text_column. The column @text_column in the model of @combo_box
5279  * must be of type %G_TYPE_STRING.
5280  *
5281  * This is only relevant if @combo_box has been created with
5282  * #GtkComboBox:has-entry as %TRUE.
5283  *
5284  * Since: 2.24
5285  */
5286 void
5287 gtk_combo_box_set_entry_text_column (GtkComboBox *combo_box,
5288                                      gint         text_column)
5289 {
5290   GtkComboBoxPrivate *priv = combo_box->priv;
5291   GtkTreeModel *model;
5292
5293   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5294
5295   model = gtk_combo_box_get_model (combo_box);
5296
5297   g_return_if_fail (text_column >= 0);
5298   g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
5299
5300   priv->text_column = text_column;
5301
5302   if (priv->text_renderer != NULL)
5303     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
5304                                     priv->text_renderer,
5305                                     "text", text_column,
5306                                     NULL);
5307 }
5308
5309 /**
5310  * gtk_combo_box_get_entry_text_column:
5311  * @combo_box: A #GtkComboBox.
5312  *
5313  * Returns the column which @combo_box is using to get the strings
5314  * from to display in the internal entry.
5315  *
5316  * Return value: A column in the data source model of @combo_box.
5317  *
5318  * Since: 2.24
5319  */
5320 gint
5321 gtk_combo_box_get_entry_text_column (GtkComboBox *combo_box)
5322 {
5323   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5324
5325   return combo_box->priv->text_column;
5326 }
5327
5328 /**
5329  * gtk_combo_box_set_focus_on_click:
5330  * @combo: a #GtkComboBox
5331  * @focus_on_click: whether the combo box grabs focus when clicked
5332  *    with the mouse
5333  *
5334  * Sets whether the combo box will grab focus when it is clicked with
5335  * the mouse. Making mouse clicks not grab focus is useful in places
5336  * like toolbars where you don't want the keyboard focus removed from
5337  * the main area of the application.
5338  *
5339  * Since: 2.6
5340  */
5341 void
5342 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5343                                   gboolean     focus_on_click)
5344 {
5345   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5346
5347   focus_on_click = focus_on_click != FALSE;
5348
5349   if (combo_box->priv->focus_on_click != focus_on_click)
5350     {
5351       combo_box->priv->focus_on_click = focus_on_click;
5352
5353       if (combo_box->priv->button)
5354         gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5355                                        focus_on_click);
5356
5357       g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5358     }
5359 }
5360
5361 /**
5362  * gtk_combo_box_get_focus_on_click:
5363  * @combo: a #GtkComboBox
5364  *
5365  * Returns whether the combo box grabs focus when it is clicked
5366  * with the mouse. See gtk_combo_box_set_focus_on_click().
5367  *
5368  * Return value: %TRUE if the combo box grabs focus when it is
5369  *     clicked with the mouse.
5370  *
5371  * Since: 2.6
5372  */
5373 gboolean
5374 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5375 {
5376   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5377   
5378   return combo_box->priv->focus_on_click;
5379 }
5380
5381
5382 static gboolean
5383 gtk_combo_box_buildable_custom_tag_start (GtkBuildable  *buildable,
5384                                           GtkBuilder    *builder,
5385                                           GObject       *child,
5386                                           const gchar   *tagname,
5387                                           GMarkupParser *parser,
5388                                           gpointer      *data)
5389 {
5390   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5391                                                 tagname, parser, data))
5392     return TRUE;
5393
5394   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5395                                                       tagname, parser, data);
5396 }
5397
5398 static void
5399 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5400                                         GtkBuilder   *builder,
5401                                         GObject      *child,
5402                                         const gchar  *tagname,
5403                                         gpointer     *data)
5404 {
5405   if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data))
5406     parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname, data);
5407 }
5408
5409 static GObject *
5410 gtk_combo_box_buildable_get_internal_child (GtkBuildable *buildable,
5411                                             GtkBuilder   *builder,
5412                                             const gchar  *childname)
5413 {
5414   GtkComboBox *combo_box = GTK_COMBO_BOX (buildable);
5415
5416   if (combo_box->priv->has_entry && strcmp (childname, "entry") == 0)
5417     return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
5418
5419   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
5420 }
5421
5422 static void
5423 gtk_combo_box_get_preferred_width (GtkWidget *widget,
5424                                    gint      *minimum_size,
5425                                    gint      *natural_size)
5426 {
5427   GtkComboBox           *combo_box = GTK_COMBO_BOX (widget);
5428   GtkComboBoxPrivate    *priv = combo_box->priv;
5429   gint                   font_size, arrow_size;
5430   PangoContext          *context;
5431   PangoFontMetrics      *metrics;
5432   const PangoFontDescription *font_desc;
5433   GtkWidget             *child;
5434   gint                   minimum_width = 0, natural_width = 0;
5435   gint                   child_min, child_nat;
5436   GtkStyleContext       *style_context;
5437   GtkStateFlags          state;
5438   GtkBorder              padding;
5439   gfloat                 arrow_scaling;
5440
5441   child = gtk_bin_get_child (GTK_BIN (widget));
5442
5443   /* common */
5444   gtk_widget_get_preferred_width (child, &child_min, &child_nat);
5445
5446   gtk_widget_style_get (GTK_WIDGET (widget),
5447                         "arrow-size", &arrow_size,
5448                         "arrow-scaling", &arrow_scaling,
5449                         NULL);
5450
5451   style_context = gtk_widget_get_style_context (widget);
5452   state = gtk_widget_get_state_flags (widget);
5453
5454   get_widget_padding_and_border (widget, &padding);
5455   font_desc = gtk_style_context_get_font (style_context, state);
5456
5457   context = gtk_widget_get_pango_context (GTK_WIDGET (widget));
5458   metrics = pango_context_get_metrics (context, font_desc,
5459                                        pango_context_get_language (context));
5460   font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
5461                             pango_font_metrics_get_descent (metrics));
5462   pango_font_metrics_unref (metrics);
5463
5464   arrow_size = MAX (arrow_size, font_size) * arrow_scaling;
5465
5466   gtk_widget_set_size_request (priv->arrow, arrow_size, arrow_size);
5467
5468   if (!priv->tree_view)
5469     {
5470       /* menu mode */
5471       if (priv->cell_view)
5472         {
5473           gint box_width;
5474           gint border_width, xpad;
5475           GtkBorder button_padding;
5476
5477           border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5478           get_widget_padding_and_border (priv->button, &button_padding);
5479
5480           gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5481           xpad = 2 * border_width +
5482             button_padding.left + button_padding.right + padding.left + padding.right;
5483
5484           minimum_width  = child_min + box_width + xpad;
5485           natural_width  = child_nat + box_width + xpad;
5486         }
5487       else
5488         {
5489           gint but_width, but_nat_width;
5490
5491           gtk_widget_get_preferred_width (priv->button,
5492                                           &but_width, &but_nat_width);
5493
5494           minimum_width  = child_min + but_width;
5495           natural_width  = child_nat + but_nat_width;
5496         }
5497     }
5498   else
5499     {
5500       /* list mode */
5501       gint button_width, button_nat_width;
5502
5503       /* sample + frame */
5504       minimum_width = child_min;
5505       natural_width = child_nat;
5506
5507       if (priv->cell_view_frame)
5508         {
5509           if (priv->has_frame)
5510             {
5511               gint border_width, xpad;
5512               GtkBorder frame_padding;
5513
5514               border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5515               get_widget_padding_and_border (priv->cell_view_frame, &frame_padding);
5516               xpad = (2 * border_width) + frame_padding.left + frame_padding.right;
5517
5518               minimum_width  += xpad;
5519               natural_width  += xpad;
5520             }
5521         }
5522
5523       /* the button */
5524       gtk_widget_get_preferred_width (priv->button,
5525                                       &button_width, &button_nat_width);
5526
5527       minimum_width += button_width;
5528       natural_width += button_nat_width;
5529     }
5530
5531   minimum_width += padding.left + padding.right;
5532   natural_width += padding.left + padding.right;
5533
5534   if (minimum_size)
5535     *minimum_size = minimum_width;
5536
5537   if (natural_size)
5538     *natural_size = natural_width;
5539 }
5540
5541 static void
5542 gtk_combo_box_get_preferred_height (GtkWidget *widget,
5543                                     gint      *minimum_size,
5544                                     gint      *natural_size)
5545 {
5546   gint min_width;
5547
5548   /* Combo box is height-for-width only
5549    * (so we always just reserve enough height for the minimum width) */
5550   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
5551   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
5552 }
5553
5554 static void
5555 gtk_combo_box_get_preferred_width_for_height (GtkWidget *widget,
5556                                               gint       avail_size,
5557                                               gint      *minimum_size,
5558                                               gint      *natural_size)
5559 {
5560   /* Combo box is height-for-width only
5561    * (so we assume we always reserved enough height for the minimum width) */
5562   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_size, natural_size);
5563 }
5564
5565
5566 static void
5567 gtk_combo_box_get_preferred_height_for_width (GtkWidget *widget,
5568                                               gint       avail_size,
5569                                               gint      *minimum_size,
5570                                               gint      *natural_size)
5571 {
5572   GtkComboBox           *combo_box = GTK_COMBO_BOX (widget);
5573   GtkComboBoxPrivate    *priv = combo_box->priv;
5574   gint                   min_height = 0, nat_height = 0;
5575   gint                   size;
5576   GtkWidget             *child;
5577   GtkBorder              padding;
5578
5579   child = gtk_bin_get_child (GTK_BIN (widget));
5580
5581   get_widget_padding_and_border (widget, &padding);
5582   size = avail_size;
5583
5584   if (!priv->tree_view)
5585     {
5586       /* menu mode */
5587       if (priv->cell_view)
5588         {
5589           /* calculate x/y padding and separator/arrow size */
5590           gint box_width, box_height;
5591           gint border_width, xpad, ypad;
5592           GtkBorder button_padding;
5593
5594           border_width = gtk_container_get_border_width (GTK_CONTAINER (combo_box));
5595           get_widget_padding_and_border (priv->button, &button_padding);
5596
5597           gtk_widget_get_preferred_width (priv->box, &box_width, NULL);
5598           gtk_widget_get_preferred_height_for_width (priv->box,
5599                                                      box_width, &box_height, NULL);
5600
5601           xpad = 2 * border_width +
5602             button_padding.left + button_padding.right;
5603           ypad = 2 * border_width +
5604             button_padding.top + button_padding.bottom;
5605
5606           size -= box_width + xpad;
5607
5608           /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5609            * and fitting the whole treemodel */
5610           gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5611
5612           min_height = MAX (min_height, box_height);
5613           nat_height = MAX (nat_height, box_height);
5614
5615           min_height += ypad;
5616           nat_height += ypad;
5617         }
5618       else
5619         {
5620           /* there is a custom child widget inside (no priv->cell_view) */
5621           gint but_width, but_height;
5622
5623           gtk_widget_get_preferred_width (priv->button, &but_width, NULL);
5624           gtk_widget_get_preferred_height_for_width (priv->button,
5625                                                      but_width, &but_height, NULL);
5626
5627           size -= but_width;
5628
5629           /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5630            * and fitting the whole treemodel */
5631           gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5632
5633           min_height = MAX (min_height, but_height);
5634           nat_height = MAX (nat_height, but_height);
5635         }
5636     }
5637   else
5638     {
5639       /* list mode */
5640       gint but_width, but_height;
5641       gint xpad = 0, ypad = 0;
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       if (priv->cell_view_frame && priv->has_frame)
5648         {
5649           GtkBorder frame_padding;
5650           gint border_width;
5651
5652           border_width = gtk_container_get_border_width (GTK_CONTAINER (priv->cell_view_frame));
5653           get_widget_padding_and_border (GTK_WIDGET (priv->cell_view_frame), &frame_padding);
5654
5655           xpad = (2 * border_width) + padding.left + frame_padding.right;
5656           ypad = (2 * border_width) + padding.top + frame_padding.bottom;
5657         }
5658
5659       size -= but_width;
5660       size -= xpad;
5661
5662       /* Get height-for-width of the child widget, usually a GtkCellArea calculating
5663        * and fitting the whole treemodel */
5664       gtk_widget_get_preferred_height_for_width (child, size, &min_height, &nat_height);
5665
5666       min_height = MAX (min_height, but_height);
5667       nat_height = MAX (nat_height, but_height);
5668
5669       min_height += ypad;
5670       nat_height += ypad;
5671     }
5672
5673   min_height += padding.top + padding.bottom;
5674   nat_height += padding.top + padding.bottom;
5675
5676   if (minimum_size)
5677     *minimum_size = min_height;
5678
5679   if (natural_size)
5680     *natural_size = nat_height;
5681 }
5682
5683 /**
5684  * gtk_combo_box_set_id_column:
5685  * @combo_box: A #GtkComboBox
5686  * @id_column: A column in @model to get string IDs for values from
5687  *
5688  * Sets the model column which @combo_box should use to get string IDs
5689  * for values from. The column @id_column in the model of @combo_box
5690  * must be of type %G_TYPE_STRING.
5691  *
5692  * Since: 3.0
5693  */
5694 void
5695 gtk_combo_box_set_id_column (GtkComboBox *combo_box,
5696                              gint         id_column)
5697 {
5698   GtkComboBoxPrivate *priv = combo_box->priv;
5699   GtkTreeModel *model;
5700
5701   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5702
5703   if (id_column != priv->id_column)
5704     {
5705       model = gtk_combo_box_get_model (combo_box);
5706
5707       g_return_if_fail (id_column >= 0);
5708       g_return_if_fail (model == NULL ||
5709                         id_column < gtk_tree_model_get_n_columns (model));
5710
5711       priv->id_column = id_column;
5712
5713       g_object_notify (G_OBJECT (combo_box), "id-column");
5714       g_object_notify (G_OBJECT (combo_box), "active-id");
5715     }
5716 }
5717
5718 /**
5719  * gtk_combo_box_get_id_column:
5720  * @combo_box: A #GtkComboBox
5721  *
5722  * Returns the column which @combo_box is using to get string IDs
5723  * for values from.
5724  *
5725  * Return value: A column in the data source model of @combo_box.
5726  *
5727  * Since: 3.0
5728  */
5729 gint
5730 gtk_combo_box_get_id_column (GtkComboBox *combo_box)
5731 {
5732   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
5733
5734   return combo_box->priv->id_column;
5735 }
5736
5737 /**
5738  * gtk_combo_box_get_active_id:
5739  * @combo_box: a #GtkComboBox
5740  *
5741  * Returns the ID of the active row of @combo_box.  This value is taken
5742  * from the active row and the column specified by the #GtkComboBox:id-column
5743  * property of @combo_box (see gtk_combo_box_set_id_column()).
5744  *
5745  * The returned value is an interned string which means that you can
5746  * compare the pointer by value to other interned strings and that you
5747  * must not free it.
5748  *
5749  * If the #GtkComboBox:id-column property of @combo_box is not set, or if
5750  * no row is active, or if the active row has a %NULL ID value, then %NULL
5751  * is returned.
5752  *
5753  * Return value: the ID of the active row, or %NULL
5754  *
5755  * Since: 3.0
5756  **/
5757 const gchar *
5758 gtk_combo_box_get_active_id (GtkComboBox *combo_box)
5759 {
5760   GtkTreeModel *model;
5761   GtkTreeIter iter;
5762   gint column;
5763
5764   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5765
5766   column = combo_box->priv->id_column;
5767
5768   if (column < 0)
5769     return NULL;
5770
5771   model = gtk_combo_box_get_model (combo_box);
5772   g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5773                         G_TYPE_STRING, NULL);
5774
5775   if (gtk_combo_box_get_active_iter (combo_box, &iter))
5776     {
5777       const gchar *interned;
5778       gchar *id;
5779
5780       gtk_tree_model_get (model, &iter, column, &id, -1);
5781       interned = g_intern_string (id);
5782       g_free (id);
5783
5784       return interned;
5785     }
5786
5787   return NULL;
5788 }
5789
5790 /**
5791  * gtk_combo_box_set_active_id:
5792  * @combo_box: a #GtkComboBox
5793  * @active_id: (allow-none): the ID of the row to select, or %NULL
5794  *
5795  * Changes the active row of @combo_box to the one that has an ID equal to
5796  * @active_id, or unsets the active row if @active_id is %NULL.  Rows having
5797  * a %NULL ID string cannot be made active by this function.
5798  *
5799  * If the #GtkComboBox:id-column property of @combo_box is unset or if no
5800  * row has the given ID then the function does nothing and returns %FALSE.
5801  *
5802  * Returns: %TRUE if a row with a matching ID was found.  If a %NULL
5803  *          @active_id was given to unset the active row, the function
5804  *          always returns %TRUE.
5805  *
5806  * Since: 3.0
5807  **/
5808 gboolean
5809 gtk_combo_box_set_active_id (GtkComboBox *combo_box,
5810                              const gchar *active_id)
5811 {
5812   GtkTreeModel *model;
5813   GtkTreeIter iter;
5814   gboolean match = FALSE;
5815   gint column;
5816
5817   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5818
5819   if (active_id == NULL)
5820     {
5821       gtk_combo_box_set_active (combo_box, -1);
5822       return TRUE;  /* active row was successfully unset */
5823     }
5824
5825   column = combo_box->priv->id_column;
5826
5827   if (column < 0)
5828     return FALSE;
5829
5830   model = gtk_combo_box_get_model (combo_box);
5831   g_return_val_if_fail (gtk_tree_model_get_column_type (model, column) ==
5832                         G_TYPE_STRING, FALSE);
5833
5834   if (gtk_tree_model_get_iter_first (model, &iter))
5835     do {
5836       gchar *id;
5837
5838       gtk_tree_model_get (model, &iter, column, &id, -1);
5839       if (id != NULL)
5840         match = strcmp (id, active_id) == 0;
5841       g_free (id);
5842
5843       if (match)
5844         {
5845           gtk_combo_box_set_active_iter (combo_box, &iter);
5846           break;
5847         }
5848     } while (gtk_tree_model_iter_next (model, &iter));
5849
5850     return match;
5851 }