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