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