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