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