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