]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Cleanup the logic in expose event handler. Reviewed by Matthias Clasen,
[~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   GdkScreen *screen;
1483   gint monitor_num;
1484   GdkRectangle monitor;
1485   GtkRequisition popup_req;
1486   GtkPolicyType hpolicy, vpolicy;
1487   
1488   /* under windows, the drop down list is as wide as the combo box itself.
1489      see bug #340204 */
1490   GtkWidget *sample = GTK_WIDGET (combo_box);
1491
1492   gdk_window_get_origin (sample->window, x, y);
1493
1494   if (GTK_WIDGET_NO_WINDOW (sample))
1495     {
1496       *x += sample->allocation.x;
1497       *y += sample->allocation.y;
1498     }
1499   
1500   *width = sample->allocation.width;
1501   
1502   if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1503     {
1504        *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1505              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1506        *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1507             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1508     }
1509
1510   hpolicy = vpolicy = GTK_POLICY_NEVER;
1511   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1512                                   hpolicy, vpolicy);
1513   gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1514
1515   if (popup_req.width > *width)
1516     {
1517       hpolicy = GTK_POLICY_ALWAYS;
1518       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1519                                       hpolicy, vpolicy);
1520       gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1521     }
1522
1523   *height = popup_req.height;
1524
1525   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1526   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1527                                                   GTK_WIDGET (combo_box)->window);
1528   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1529
1530   if (*x < monitor.x)
1531     *x = monitor.x;
1532   else if (*x + *width > monitor.x + monitor.width)
1533     *x = monitor.x + monitor.width - *width;
1534   
1535   if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1536     *y += sample->allocation.height;
1537   else if (*y - *height >= monitor.y)
1538     *y -= *height;
1539   else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1540     {
1541       *y += sample->allocation.height;
1542       *height = monitor.y + monitor.height - *y;
1543     }
1544   else 
1545     {
1546       *height = *y - monitor.y;
1547       *y = monitor.y;
1548     }
1549
1550   if (popup_req.height > *height)
1551     {
1552       vpolicy = GTK_POLICY_ALWAYS;
1553       
1554       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1555                                       hpolicy, vpolicy);
1556     }
1557
1558
1559 static gboolean
1560 cell_view_is_sensitive (GtkCellView *cell_view)
1561 {
1562   GList *cells, *list;
1563   gboolean sensitive;
1564   
1565   cells = gtk_cell_view_get_cell_renderers (cell_view);
1566
1567   sensitive = FALSE;
1568   list = cells;
1569   while (list)
1570     {
1571       g_object_get (list->data, "sensitive", &sensitive, NULL);
1572       
1573       if (sensitive)
1574         break;
1575
1576       list = list->next;
1577     }
1578   g_list_free (cells);
1579
1580   return sensitive;
1581 }
1582
1583 static gboolean
1584 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1585                               GtkTreeIter *iter)
1586 {
1587   GList *cells, *list;
1588   gboolean sensitive;
1589
1590   if (!combo_box->priv->column)
1591     return TRUE;
1592
1593   if (combo_box->priv->row_separator_func)
1594     {
1595       if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
1596                                                   combo_box->priv->row_separator_data))
1597         return FALSE;
1598     }
1599
1600   gtk_tree_view_column_cell_set_cell_data (combo_box->priv->column,
1601                                            combo_box->priv->model,
1602                                            iter, FALSE, FALSE);
1603
1604   cells = gtk_tree_view_column_get_cell_renderers (combo_box->priv->column);
1605
1606   sensitive = FALSE;
1607   list = cells;
1608   while (list)
1609     {
1610       g_object_get (list->data, "sensitive", &sensitive, NULL);
1611       
1612       if (sensitive)
1613         break;
1614
1615       list = list->next;
1616     }
1617   g_list_free (cells);
1618
1619   return sensitive;
1620 }
1621
1622 static void
1623 update_menu_sensitivity (GtkComboBox *combo_box,
1624                          GtkWidget   *menu)
1625 {
1626   GList *children, *child;
1627   GtkWidget *item, *submenu, *separator;
1628   GtkWidget *cell_view;
1629   gboolean sensitive;
1630
1631   if (!combo_box->priv->model)
1632     return;
1633
1634   children = gtk_container_get_children (GTK_CONTAINER (menu));
1635
1636   for (child = children; child; child = child->next)
1637     {
1638       item = GTK_WIDGET (child->data);
1639       cell_view = GTK_BIN (item)->child;
1640
1641       if (!GTK_IS_CELL_VIEW (cell_view))
1642         continue;
1643       
1644       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1645       if (submenu != NULL)
1646         {
1647           gtk_widget_set_sensitive (item, TRUE);
1648           update_menu_sensitivity (combo_box, submenu);
1649         }
1650       else
1651         {
1652           sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1653
1654           if (menu != combo_box->priv->popup_widget && child == children)
1655             {
1656               separator = GTK_WIDGET (child->next->data);
1657               g_object_set (item, "visible", sensitive, NULL);
1658               g_object_set (separator, "visible", sensitive, NULL);
1659             }
1660           else
1661             gtk_widget_set_sensitive (item, sensitive);
1662         }
1663     }
1664
1665   g_list_free (children);
1666 }
1667
1668 static void 
1669 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1670                           guint        button, 
1671                           guint32      activate_time)
1672 {
1673   GtkTreePath *path;
1674   gint active_item;
1675   GtkRequisition requisition;
1676   gint width;
1677   
1678   update_menu_sensitivity (combo_box, combo_box->priv->popup_widget);
1679
1680   active_item = -1;
1681   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1682     {
1683       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1684       active_item = gtk_tree_path_get_indices (path)[0];
1685       gtk_tree_path_free (path);
1686       
1687       if (combo_box->priv->add_tearoffs)
1688         active_item++;
1689     }
1690
1691   /* FIXME handle nested menus better */
1692   gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), active_item);
1693   
1694   if (combo_box->priv->wrap_width == 0)
1695     {
1696       width = GTK_WIDGET (combo_box)->allocation.width;
1697       gtk_widget_set_size_request (combo_box->priv->popup_widget, -1, -1);
1698       gtk_widget_size_request (combo_box->priv->popup_widget, &requisition);
1699       
1700       gtk_widget_set_size_request (combo_box->priv->popup_widget,
1701                                    MAX (width, requisition.width), -1);
1702     }
1703   
1704   gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1705                   NULL, NULL,
1706                   gtk_combo_box_menu_position, combo_box,
1707                   button, activate_time);
1708 }
1709
1710 static gboolean
1711 popup_grab_on_window (GdkWindow *window,
1712                       guint32    activate_time,
1713                       gboolean   grab_keyboard)
1714 {
1715   if ((gdk_pointer_grab (window, TRUE,
1716                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1717                          GDK_POINTER_MOTION_MASK,
1718                          NULL, NULL, activate_time) == 0))
1719     {
1720       if (!grab_keyboard ||
1721           gdk_keyboard_grab (window, TRUE,
1722                              activate_time) == 0)
1723         return TRUE;
1724       else
1725         {
1726           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1727                                       activate_time);
1728           return FALSE;
1729         }
1730     }
1731
1732   return FALSE;
1733 }
1734
1735 /**
1736  * gtk_combo_box_popup:
1737  * @combo_box: a #GtkComboBox
1738  * 
1739  * Pops up the menu or dropdown list of @combo_box. 
1740  *
1741  * This function is mostly intended for use by accessibility technologies;
1742  * applications should have little use for it.
1743  *
1744  * Since: 2.4
1745  **/
1746 void
1747 gtk_combo_box_popup (GtkComboBox *combo_box)
1748 {
1749   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1750
1751   g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1752 }
1753
1754 static void
1755 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1756 {
1757   gint x, y, width, height;
1758   GtkTreePath *path = NULL, *ppath;
1759   GtkWidget *toplevel;
1760
1761   if (!GTK_WIDGET_REALIZED (combo_box))
1762     return;
1763
1764   if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1765     return;
1766
1767   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1768     {
1769       gtk_combo_box_menu_popup (combo_box, 0, 0);
1770       return;
1771     }
1772
1773   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1774   if (GTK_IS_WINDOW (toplevel))
1775     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
1776                                  GTK_WINDOW (combo_box->priv->popup_window));
1777
1778   gtk_widget_show_all (combo_box->priv->popup_frame);
1779   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1780   
1781   gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);  
1782   gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1783
1784   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1785     {
1786       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1787       ppath = gtk_tree_path_copy (path);
1788       if (gtk_tree_path_up (ppath))
1789         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (combo_box->priv->tree_view),
1790                                       ppath);
1791       gtk_tree_path_free (ppath);
1792     }
1793   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
1794                                   TRUE);
1795   
1796   /* popup */
1797   gtk_widget_show (combo_box->priv->popup_window);
1798
1799   if (path)
1800     {
1801       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1802                                 path, NULL, FALSE);
1803       gtk_tree_path_free (path);
1804     }
1805
1806   gtk_widget_grab_focus (combo_box->priv->popup_window);
1807   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1808                                 TRUE);
1809
1810   if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1811     gtk_widget_grab_focus (combo_box->priv->tree_view);
1812
1813   if (!popup_grab_on_window (combo_box->priv->popup_window->window,
1814                              GDK_CURRENT_TIME, TRUE))
1815     {
1816       gtk_widget_hide (combo_box->priv->popup_window);
1817       return;
1818     }
1819
1820   gtk_grab_add (combo_box->priv->popup_window);
1821 }
1822
1823 /**
1824  * gtk_combo_box_popdown:
1825  * @combo_box: a #GtkComboBox
1826  * 
1827  * Hides the menu or dropdown list of @combo_box.
1828  *
1829  * This function is mostly intended for use by accessibility technologies;
1830  * applications should have little use for it.
1831  *
1832  * Since: 2.4
1833  **/
1834 void
1835 gtk_combo_box_popdown (GtkComboBox *combo_box)
1836 {
1837   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1838
1839   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1840     {
1841       gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1842       return;
1843     }
1844
1845   if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1846     return;
1847
1848   gtk_grab_remove (combo_box->priv->popup_window);
1849   gtk_widget_hide_all (combo_box->priv->popup_window);
1850   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1851                                 FALSE);
1852 }
1853
1854 static gint
1855 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1856                                     GtkTreePath *path)
1857 {
1858   gint padding;
1859   GtkRequisition req;
1860
1861   if (combo_box->priv->cell_view)
1862     gtk_widget_style_get (combo_box->priv->cell_view,
1863                           "focus-line-width", &padding,
1864                           NULL);
1865   else
1866     padding = 0;
1867
1868   /* add some pixels for good measure */
1869   padding += BONUS_PADDING;
1870
1871   if (combo_box->priv->cell_view)
1872     gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1873                                    path, &req);
1874   else
1875     req.width = 0;
1876
1877   return req.width + padding;
1878 }
1879
1880 static void
1881 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1882 {
1883   GtkTreeIter iter;
1884   GtkTreePath *path;
1885
1886   if (!combo_box->priv->model ||
1887       !gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1888     return;
1889
1890   combo_box->priv->width = 0;
1891   combo_box->priv->height = 0;
1892
1893   path = gtk_tree_path_new_from_indices (0, -1);
1894
1895   do
1896     {
1897       GtkRequisition req;
1898
1899       if (combo_box->priv->cell_view)
1900         gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
1901                                        path, &req);
1902       else
1903         {
1904           req.width = 0;
1905           req.height = 0;
1906         }
1907
1908       combo_box->priv->width = MAX (combo_box->priv->width, req.width);
1909       combo_box->priv->height = MAX (combo_box->priv->height, req.height);
1910
1911       gtk_tree_path_next (path);
1912     }
1913   while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1914
1915   gtk_tree_path_free (path);
1916 }
1917
1918 static void
1919 gtk_combo_box_size_request (GtkWidget      *widget,
1920                             GtkRequisition *requisition)
1921 {
1922   gint width, height;
1923   gint focus_width, focus_pad;
1924   gint font_size;
1925   gint arrow_size;
1926   GtkRequisition bin_req;
1927   PangoContext *context;
1928   PangoFontMetrics *metrics;
1929   PangoFontDescription *font_desc;
1930
1931   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1932  
1933   /* common */
1934   gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1935   gtk_combo_box_remeasure (combo_box);
1936   bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1937   bin_req.height = MAX (bin_req.height, combo_box->priv->height);
1938
1939   gtk_widget_style_get (GTK_WIDGET (widget),
1940                         "focus-line-width", &focus_width,
1941                         "focus-padding", &focus_pad,
1942                         "arrow-size", &arrow_size,
1943                         NULL);
1944
1945   font_desc = GTK_BIN (widget)->child->style->font_desc;
1946   context = gtk_widget_get_pango_context (widget);
1947   metrics = pango_context_get_metrics (context, font_desc,
1948                                        pango_context_get_language (context));
1949   font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
1950                             pango_font_metrics_get_descent (metrics));
1951   pango_font_metrics_unref (metrics);
1952
1953   arrow_size = MAX (arrow_size, font_size);
1954
1955   gtk_widget_set_size_request (combo_box->priv->arrow, arrow_size, arrow_size);
1956
1957   if (!combo_box->priv->tree_view)
1958     {
1959       /* menu mode */
1960
1961       if (combo_box->priv->cell_view)
1962         {
1963           GtkRequisition button_req, sep_req, arrow_req;
1964           gint border_width, xthickness, ythickness;
1965
1966           gtk_widget_size_request (combo_box->priv->button, &button_req);
1967           border_width = GTK_CONTAINER (combo_box)->border_width;
1968           xthickness = combo_box->priv->button->style->xthickness;
1969           ythickness = combo_box->priv->button->style->ythickness;
1970
1971           bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1972           bin_req.height = MAX (bin_req.height, combo_box->priv->height);
1973
1974           gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1975           gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1976
1977           height = MAX (sep_req.height, arrow_req.height);
1978           height = MAX (height, bin_req.height);
1979
1980           width = bin_req.width + sep_req.width + arrow_req.width;
1981
1982           height += 2*(border_width + ythickness + focus_width + focus_pad);
1983           width  += 2*(border_width + xthickness + focus_width + focus_pad);
1984
1985           requisition->width = width;
1986           requisition->height = height;
1987         }
1988       else
1989         {
1990           GtkRequisition but_req;
1991
1992           gtk_widget_size_request (combo_box->priv->button, &but_req);
1993
1994           requisition->width = bin_req.width + but_req.width;
1995           requisition->height = MAX (bin_req.height, but_req.height);
1996         }
1997     }
1998   else
1999     {
2000       /* list mode */
2001       GtkRequisition button_req, frame_req;
2002
2003       /* sample + frame */
2004       *requisition = bin_req;
2005
2006       requisition->width += 2 * focus_width;
2007       
2008       if (combo_box->priv->cell_view_frame)
2009         {
2010           gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
2011           if (combo_box->priv->has_frame)
2012             {
2013               requisition->width += 2 *
2014                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2015                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2016               requisition->height += 2 *
2017                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2018                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2019             }
2020         }
2021
2022       /* the button */
2023       gtk_widget_size_request (combo_box->priv->button, &button_req);
2024
2025       requisition->height = MAX (requisition->height, button_req.height);
2026       requisition->width += button_req.width;
2027     }
2028 }
2029
2030 static void
2031 gtk_combo_box_size_allocate (GtkWidget     *widget,
2032                              GtkAllocation *allocation)
2033 {
2034   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2035   gint focus_width, focus_pad;
2036   GtkAllocation child;
2037   GtkRequisition req;
2038   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2039
2040   widget->allocation = *allocation;
2041
2042   gtk_widget_style_get (GTK_WIDGET (widget),
2043                         "focus-line-width", &focus_width,
2044                         "focus-padding", &focus_pad,
2045                         NULL);
2046
2047   if (!combo_box->priv->tree_view)
2048     {
2049       if (combo_box->priv->cell_view)
2050         {
2051           gint border_width, xthickness, ythickness;
2052           gint width;
2053
2054           /* menu mode */
2055           gtk_widget_size_allocate (combo_box->priv->button, allocation);
2056
2057           /* set some things ready */
2058           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
2059           xthickness = combo_box->priv->button->style->xthickness;
2060           ythickness = combo_box->priv->button->style->ythickness;
2061
2062           child.x = allocation->x;
2063           child.y = allocation->y;
2064           width = allocation->width;
2065           child.height = allocation->height;
2066
2067           if (!combo_box->priv->is_cell_renderer)
2068             {
2069               child.x += border_width + xthickness + focus_width + focus_pad;
2070               child.y += border_width + ythickness + focus_width + focus_pad;
2071               width -= 2 * (child.x - allocation->x);
2072               child.height -= 2 * (child.y - allocation->y);
2073             }
2074
2075
2076           /* handle the children */
2077           gtk_widget_size_request (combo_box->priv->arrow, &req);
2078           child.width = req.width;
2079           if (!is_rtl)
2080             child.x += width - req.width;
2081           child.width = MAX (1, child.width);
2082           child.height = MAX (1, child.height);
2083           gtk_widget_size_allocate (combo_box->priv->arrow, &child);
2084           if (is_rtl)
2085             child.x += req.width;
2086           gtk_widget_size_request (combo_box->priv->separator, &req);
2087           child.width = req.width;
2088           if (!is_rtl)
2089             child.x -= req.width;
2090           child.width = MAX (1, child.width);
2091           child.height = MAX (1, child.height);
2092           gtk_widget_size_allocate (combo_box->priv->separator, &child);
2093
2094           if (is_rtl)
2095             {
2096               child.x += req.width;
2097               child.width = allocation->x + allocation->width 
2098                 - (border_width + xthickness + focus_width + focus_pad) 
2099                 - child.x;
2100             }
2101           else 
2102             {
2103               child.width = child.x;
2104               child.x = allocation->x 
2105                 + border_width + xthickness + focus_width + focus_pad;
2106               child.width -= child.x;
2107             }
2108
2109           child.width = MAX (1, child.width);
2110           child.height = MAX (1, child.height);
2111           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2112         }
2113       else
2114         {
2115           gtk_widget_size_request (combo_box->priv->button, &req);
2116           if (is_rtl)
2117             child.x = allocation->x;
2118           else
2119             child.x = allocation->x + allocation->width - req.width;
2120           child.y = allocation->y;
2121           child.width = req.width;
2122           child.height = allocation->height;
2123           child.width = MAX (1, child.width);
2124           child.height = MAX (1, child.height);
2125           gtk_widget_size_allocate (combo_box->priv->button, &child);
2126
2127           if (is_rtl)
2128             child.x = allocation->x + req.width;
2129           else
2130             child.x = allocation->x;
2131           child.y = allocation->y;
2132           child.width = allocation->width - req.width;
2133           child.width = MAX (1, child.width);
2134           child.height = MAX (1, child.height);
2135           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2136         }
2137     }
2138   else
2139     {
2140       /* list mode */
2141
2142       /* button */
2143       gtk_widget_size_request (combo_box->priv->button, &req);
2144       if (is_rtl)
2145         child.x = allocation->x;
2146       else
2147         child.x = allocation->x + allocation->width - req.width;
2148       child.y = allocation->y;
2149       child.width = req.width;
2150       child.height = allocation->height;
2151       child.width = MAX (1, child.width);
2152       child.height = MAX (1, child.height);
2153       gtk_widget_size_allocate (combo_box->priv->button, &child);
2154
2155       /* frame */
2156       if (is_rtl)
2157         child.x = allocation->x + req.width;
2158       else
2159         child.x = allocation->x;
2160       child.y = allocation->y;
2161       child.width = allocation->width - req.width;
2162       child.height = allocation->height;
2163
2164       if (combo_box->priv->cell_view_frame)
2165         {
2166           child.width = MAX (1, child.width);
2167           child.height = MAX (1, child.height);
2168           gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
2169
2170           /* the sample */
2171           if (combo_box->priv->has_frame)
2172             {
2173               child.x +=
2174                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2175                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
2176               child.y +=
2177                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2178                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
2179               child.width -= 2 * (
2180                                   GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2181                                   GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2182               child.height -= 2 * (
2183                                    GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2184                                    GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2185             }
2186         }
2187       
2188       child.width = MAX (1, child.width);
2189       child.height = MAX (1, child.height);
2190       gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2191     }
2192 }
2193
2194 static void
2195 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2196 {
2197   if (combo_box->priv->model)
2198     {
2199       g_signal_handler_disconnect (combo_box->priv->model,
2200                                    combo_box->priv->inserted_id);
2201       g_signal_handler_disconnect (combo_box->priv->model,
2202                                    combo_box->priv->deleted_id);
2203       g_signal_handler_disconnect (combo_box->priv->model,
2204                                    combo_box->priv->reordered_id);
2205       g_signal_handler_disconnect (combo_box->priv->model,
2206                                    combo_box->priv->changed_id);
2207     }
2208
2209   /* menu mode */
2210   if (!combo_box->priv->tree_view)
2211     {
2212       if (combo_box->priv->popup_widget)
2213         gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
2214                                (GtkCallback)gtk_widget_destroy, NULL);
2215     }
2216
2217   if (combo_box->priv->model)
2218     {
2219       g_object_unref (combo_box->priv->model);
2220       combo_box->priv->model = NULL;
2221     }
2222
2223   if (combo_box->priv->active_row)
2224     {
2225       gtk_tree_row_reference_free (combo_box->priv->active_row);
2226       combo_box->priv->active_row = NULL;
2227     }
2228
2229   if (combo_box->priv->cell_view)
2230     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2231 }
2232
2233 static void
2234 gtk_combo_box_forall (GtkContainer *container,
2235                       gboolean      include_internals,
2236                       GtkCallback   callback,
2237                       gpointer      callback_data)
2238 {
2239   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2240
2241   if (include_internals)
2242     {
2243       if (combo_box->priv->button)
2244         (* callback) (combo_box->priv->button, callback_data);
2245       if (combo_box->priv->cell_view_frame)
2246         (* callback) (combo_box->priv->cell_view_frame, callback_data);
2247     }
2248
2249   if (GTK_BIN (container)->child)
2250     (* callback) (GTK_BIN (container)->child, callback_data);
2251 }
2252
2253 static void 
2254 gtk_combo_box_child_show (GtkWidget *widget,
2255                           GtkComboBox *combo_box)
2256 {
2257   GtkComboBoxPrivate *priv = combo_box->priv;
2258
2259   priv->popup_shown = TRUE;
2260   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2261 }
2262
2263 static void 
2264 gtk_combo_box_child_hide (GtkWidget *widget,
2265                           GtkComboBox *combo_box)
2266 {
2267   GtkComboBoxPrivate *priv = combo_box->priv;
2268
2269   priv->popup_shown = FALSE;
2270   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2271 }
2272
2273 static gboolean
2274 gtk_combo_box_expose_event (GtkWidget      *widget,
2275                             GdkEventExpose *event)
2276 {
2277   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2278
2279   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2280                                   combo_box->priv->button, event);
2281
2282   if (combo_box->priv->tree_view &&
2283       combo_box->priv->cell_view_frame)
2284     {
2285       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2286                                       combo_box->priv->cell_view_frame, event);
2287     }
2288
2289   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2290                                   GTK_BIN (widget)->child, event);
2291
2292   return FALSE;
2293 }
2294
2295 typedef struct {
2296   GtkComboBox *combo;
2297   GtkTreePath *path;
2298   GtkTreeIter iter;
2299   gboolean found;
2300   gboolean set;
2301   gboolean visible;
2302 } SearchData;
2303
2304 static gboolean
2305 path_visible (GtkTreeView *view,
2306               GtkTreePath *path)
2307 {
2308   GtkRBTree *tree;
2309   GtkRBNode *node;
2310   
2311   /* Note that we rely on the fact that collapsed rows don't have nodes 
2312    */
2313   return _gtk_tree_view_find_node (view, path, &tree, &node);
2314 }
2315
2316 static gboolean
2317 tree_next_func (GtkTreeModel *model,
2318                 GtkTreePath  *path,
2319                 GtkTreeIter  *iter,
2320                 gpointer      data)
2321 {
2322   SearchData *search_data = (SearchData *)data;
2323
2324   if (search_data->found) 
2325     {
2326       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2327         return FALSE;
2328       
2329       if (search_data->visible &&
2330           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2331         return FALSE;
2332
2333       search_data->set = TRUE;
2334       search_data->iter = *iter;
2335
2336       return TRUE;
2337     }
2338  
2339   if (gtk_tree_path_compare (path, search_data->path) == 0)
2340     search_data->found = TRUE;
2341   
2342   return FALSE;
2343 }
2344
2345 static gboolean
2346 tree_next (GtkComboBox  *combo,
2347            GtkTreeModel *model,
2348            GtkTreeIter  *iter,
2349            GtkTreeIter  *next,
2350            gboolean      visible)
2351 {
2352   SearchData search_data;
2353
2354   search_data.combo = combo;
2355   search_data.path = gtk_tree_model_get_path (model, iter);
2356   search_data.visible = visible;
2357   search_data.found = FALSE;
2358   search_data.set = FALSE;
2359
2360   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2361   
2362   *next = search_data.iter;
2363
2364   gtk_tree_path_free (search_data.path);
2365
2366   return search_data.set;
2367 }
2368
2369 static gboolean
2370 tree_prev_func (GtkTreeModel *model,
2371                 GtkTreePath  *path,
2372                 GtkTreeIter  *iter,
2373                 gpointer      data)
2374 {
2375   SearchData *search_data = (SearchData *)data;
2376
2377   if (gtk_tree_path_compare (path, search_data->path) == 0)
2378     {
2379       search_data->found = TRUE;
2380       return TRUE;
2381     }
2382   
2383   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2384     return FALSE;
2385       
2386   if (search_data->visible &&
2387       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2388     return FALSE; 
2389   
2390   search_data->set = TRUE;
2391   search_data->iter = *iter;
2392   
2393   return FALSE; 
2394 }
2395
2396 static gboolean
2397 tree_prev (GtkComboBox  *combo,
2398            GtkTreeModel *model,
2399            GtkTreeIter  *iter,
2400            GtkTreeIter  *prev,
2401            gboolean      visible)
2402 {
2403   SearchData search_data;
2404
2405   search_data.combo = combo;
2406   search_data.path = gtk_tree_model_get_path (model, iter);
2407   search_data.visible = visible;
2408   search_data.found = FALSE;
2409   search_data.set = FALSE;
2410
2411   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2412   
2413   *prev = search_data.iter;
2414
2415   gtk_tree_path_free (search_data.path);
2416
2417   return search_data.set;
2418 }
2419
2420 static gboolean
2421 tree_last_func (GtkTreeModel *model,
2422                 GtkTreePath  *path,
2423                 GtkTreeIter  *iter,
2424                 gpointer      data)
2425 {
2426   SearchData *search_data = (SearchData *)data;
2427
2428   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2429     return FALSE;
2430       
2431   /* Note that we rely on the fact that collapsed rows don't have nodes 
2432    */
2433   if (search_data->visible &&
2434       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2435     return FALSE; 
2436   
2437   search_data->set = TRUE;
2438   search_data->iter = *iter;
2439   
2440   return FALSE; 
2441 }
2442
2443 static gboolean
2444 tree_last (GtkComboBox  *combo,
2445            GtkTreeModel *model,
2446            GtkTreeIter  *last,
2447            gboolean      visible)
2448 {
2449   SearchData search_data;
2450
2451   search_data.combo = combo;
2452   search_data.visible = visible;
2453   search_data.set = FALSE;
2454
2455   gtk_tree_model_foreach (model, tree_last_func, &search_data);
2456   
2457   *last = search_data.iter;
2458
2459   return search_data.set;  
2460 }
2461
2462
2463 static gboolean
2464 tree_first_func (GtkTreeModel *model,
2465                  GtkTreePath  *path,
2466                  GtkTreeIter  *iter,
2467                  gpointer      data)
2468 {
2469   SearchData *search_data = (SearchData *)data;
2470
2471   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2472     return FALSE;
2473   
2474   if (search_data->visible &&
2475       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2476     return FALSE;
2477   
2478   search_data->set = TRUE;
2479   search_data->iter = *iter;
2480   
2481   return TRUE;
2482 }
2483
2484 static gboolean
2485 tree_first (GtkComboBox  *combo,
2486             GtkTreeModel *model,
2487             GtkTreeIter  *first,
2488             gboolean      visible)
2489 {
2490   SearchData search_data;
2491   
2492   search_data.combo = combo;
2493   search_data.visible = visible;
2494   search_data.set = FALSE;
2495
2496   gtk_tree_model_foreach (model, tree_first_func, &search_data);
2497   
2498   *first = search_data.iter;
2499
2500   return search_data.set;  
2501 }
2502
2503 static gboolean
2504 gtk_combo_box_scroll_event (GtkWidget          *widget,
2505                             GdkEventScroll     *event)
2506 {
2507   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2508   gboolean found;
2509   GtkTreeIter iter;
2510   GtkTreeIter new_iter;
2511
2512   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2513     return TRUE;
2514   
2515   if (event->direction == GDK_SCROLL_UP)
2516     found = tree_prev (combo_box, combo_box->priv->model, 
2517                        &iter, &new_iter, FALSE);
2518   else
2519     found = tree_next (combo_box, combo_box->priv->model, 
2520                        &iter, &new_iter, FALSE);
2521   
2522   if (found)
2523     gtk_combo_box_set_active_iter (combo_box, &new_iter);
2524
2525   return TRUE;
2526 }
2527
2528 /*
2529  * menu style
2530  */
2531
2532 static void
2533 gtk_combo_box_sync_cells (GtkComboBox   *combo_box,
2534                           GtkCellLayout *cell_layout)
2535 {
2536   GSList *k;
2537
2538   for (k = combo_box->priv->cells; k; k = k->next)
2539     {
2540       GSList *j;
2541       ComboCellInfo *info = (ComboCellInfo *)k->data;
2542
2543       if (info->pack == GTK_PACK_START)
2544         gtk_cell_layout_pack_start (cell_layout,
2545                                     info->cell, info->expand);
2546       else if (info->pack == GTK_PACK_END)
2547         gtk_cell_layout_pack_end (cell_layout,
2548                                   info->cell, info->expand);
2549
2550       gtk_cell_layout_set_cell_data_func (cell_layout,
2551                                           info->cell,
2552                                           combo_cell_data_func, info, NULL);
2553
2554       for (j = info->attributes; j; j = j->next->next)
2555         {
2556           gtk_cell_layout_add_attribute (cell_layout,
2557                                          info->cell,
2558                                          j->data,
2559                                          GPOINTER_TO_INT (j->next->data));
2560         }
2561     }
2562 }
2563
2564 static void
2565 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2566                           gboolean     add_children)
2567 {
2568   GtkWidget *menu;
2569
2570   if (combo_box->priv->cell_view)
2571     {
2572       combo_box->priv->button = gtk_toggle_button_new ();
2573       gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2574                                      combo_box->priv->focus_on_click);
2575
2576       g_signal_connect (combo_box->priv->button, "toggled",
2577                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2578       gtk_widget_set_parent (combo_box->priv->button,
2579                              GTK_BIN (combo_box)->child->parent);
2580
2581       combo_box->priv->box = gtk_hbox_new (FALSE, 0);
2582       gtk_container_add (GTK_CONTAINER (combo_box->priv->button), 
2583                          combo_box->priv->box);
2584
2585       combo_box->priv->separator = gtk_vseparator_new ();
2586       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
2587                          combo_box->priv->separator);
2588
2589       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2590       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
2591                          combo_box->priv->arrow);
2592
2593       gtk_widget_show_all (combo_box->priv->button);
2594     }
2595   else
2596     {
2597       combo_box->priv->button = gtk_toggle_button_new ();
2598       gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2599                                      combo_box->priv->focus_on_click);
2600
2601       g_signal_connect (combo_box->priv->button, "toggled",
2602                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2603       gtk_widget_set_parent (combo_box->priv->button,
2604                              GTK_BIN (combo_box)->child->parent);
2605
2606       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2607       gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2608                          combo_box->priv->arrow);
2609       gtk_widget_show_all (combo_box->priv->button);
2610     }
2611
2612   g_signal_connect (combo_box->priv->button, "button_press_event",
2613                     G_CALLBACK (gtk_combo_box_menu_button_press),
2614                     combo_box);
2615   g_signal_connect (combo_box->priv->button, "state_changed",
2616                     G_CALLBACK (gtk_combo_box_button_state_changed), 
2617                     combo_box);
2618
2619   /* create our funky menu */
2620   menu = gtk_menu_new ();
2621   g_signal_connect (menu, "key_press_event",
2622                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2623   gtk_combo_box_set_popup_widget (combo_box, menu);
2624
2625   /* add items */
2626   if (add_children)
2627     gtk_combo_box_menu_fill (combo_box);
2628
2629   /* the column is needed in tree_column_row_is_sensitive() */
2630   combo_box->priv->column = gtk_tree_view_column_new ();
2631   g_object_ref_sink (combo_box->priv->column);
2632   gtk_combo_box_sync_cells (combo_box, 
2633                             GTK_CELL_LAYOUT (combo_box->priv->column));
2634
2635   gtk_combo_box_update_title (combo_box);
2636 }
2637
2638 static void
2639 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2640 {
2641   GtkWidget *menu;
2642
2643   if (!combo_box->priv->model)
2644     return;
2645
2646   menu = combo_box->priv->popup_widget;
2647
2648   if (combo_box->priv->add_tearoffs)
2649     {
2650       GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2651
2652       gtk_widget_show (tearoff);
2653       
2654       if (combo_box->priv->wrap_width)
2655         gtk_menu_attach (GTK_MENU (menu), tearoff, 
2656                          0, combo_box->priv->wrap_width, 0, 1);
2657       else
2658         gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2659     }
2660   
2661   gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2662 }
2663
2664 static GtkWidget *
2665 gtk_cell_view_menu_item_new (GtkComboBox  *combo_box,
2666                              GtkTreeModel *model,
2667                              GtkTreeIter  *iter)
2668 {
2669   GtkWidget *cell_view; 
2670   GtkWidget *item;
2671   GtkTreePath *path;
2672   GtkRequisition req;
2673
2674   cell_view = gtk_cell_view_new ();
2675   item = gtk_menu_item_new ();
2676   gtk_container_add (GTK_CONTAINER (item), cell_view);
2677
2678   gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);     
2679   path = gtk_tree_model_get_path (model, iter);
2680   gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2681   gtk_tree_path_free (path);
2682
2683   gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2684   gtk_widget_size_request (cell_view, &req);
2685   gtk_widget_show (cell_view);
2686   
2687   return item;
2688 }
2689  
2690 static void
2691 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2692                                GtkWidget   *menu,
2693                                GtkTreeIter *parent)
2694 {
2695   GtkTreeModel *model = combo_box->priv->model;
2696   GtkWidget *item, *submenu, *subitem, *separator;
2697   GtkTreeIter iter;
2698   gboolean is_separator;
2699   gint i, n_children;
2700   GtkWidget *last;
2701   GtkTreePath *path;
2702   
2703   n_children = gtk_tree_model_iter_n_children (model, parent);
2704   
2705   last = NULL;
2706   for (i = 0; i < n_children; i++)
2707     {
2708       gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2709
2710       if (combo_box->priv->row_separator_func)
2711         is_separator = (*combo_box->priv->row_separator_func) (combo_box->priv->model, &iter,
2712                                                                combo_box->priv->row_separator_data);
2713       else
2714         is_separator = FALSE;
2715       
2716       if (is_separator)
2717         {
2718           item = gtk_separator_menu_item_new ();
2719           path = gtk_tree_model_get_path (model, &iter);
2720           g_object_set_data_full (G_OBJECT (item),
2721                                   I_("gtk-combo-box-item-path"),
2722                                   gtk_tree_row_reference_new (model, path),
2723                                   (GDestroyNotify)gtk_tree_row_reference_free);
2724           gtk_tree_path_free (path);
2725         }
2726       else
2727         {
2728           item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2729           if (gtk_tree_model_iter_has_child (model, &iter))
2730             {
2731               submenu = gtk_menu_new ();
2732               gtk_widget_show (submenu);
2733               gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2734               
2735               /* Ugly - since menus can only activate leafs, we have to
2736                * duplicate the item inside the submenu.
2737                */
2738               subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2739               separator = gtk_separator_menu_item_new ();
2740               gtk_widget_show (subitem);
2741               gtk_widget_show (separator);
2742               g_signal_connect (subitem, "activate",
2743                                 G_CALLBACK (gtk_combo_box_menu_item_activate),
2744                                 combo_box);
2745               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2746               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2747               
2748               gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2749             }
2750           g_signal_connect (item, "activate",
2751                             G_CALLBACK (gtk_combo_box_menu_item_activate),
2752                             combo_box);
2753         }
2754       
2755       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2756       if (combo_box->priv->wrap_width && menu == combo_box->priv->popup_widget)
2757         gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2758       gtk_widget_show (item);
2759       
2760       last = item;
2761     }
2762 }
2763
2764 static void
2765 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2766 {
2767   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2768                                         G_SIGNAL_MATCH_DATA,
2769                                         0, 0, NULL,
2770                                         gtk_combo_box_menu_button_press, NULL);
2771   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2772                                         G_SIGNAL_MATCH_DATA,
2773                                         0, 0, NULL,
2774                                         gtk_combo_box_button_state_changed, combo_box);
2775
2776   /* unparent will remove our latest ref */
2777   gtk_widget_unparent (combo_box->priv->button);
2778   
2779   combo_box->priv->box = NULL;
2780   combo_box->priv->button = NULL;
2781   combo_box->priv->arrow = NULL;
2782   combo_box->priv->separator = NULL;
2783
2784   g_object_unref (combo_box->priv->column);
2785   combo_box->priv->column = NULL;
2786
2787   /* changing the popup window will unref the menu and the children */
2788 }
2789
2790 /*
2791  * grid
2792  */
2793
2794 static gboolean
2795 menu_occupied (GtkMenu   *menu,
2796                guint      left_attach,
2797                guint      right_attach,
2798                guint      top_attach,
2799                guint      bottom_attach)
2800 {
2801   GList *i;
2802
2803   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2804     {
2805       guint l, r, b, t;
2806
2807       gtk_container_child_get (GTK_CONTAINER (menu), 
2808                                i->data,
2809                                "left-attach", &l,
2810                                "right-attach", &r,
2811                                "bottom-attach", &b,
2812                                "top-attach", &t,
2813                                NULL);
2814
2815       /* look if this item intersects with the given coordinates */
2816       if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2817         return TRUE;
2818     }
2819
2820   return FALSE;
2821 }
2822
2823 static void
2824 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2825                              GtkWidget   *item,
2826                              GtkTreeIter *iter,
2827                              GtkWidget   *last)
2828 {
2829   gint current_col = 0, current_row = 0;
2830   gint rows = 1, cols = 1;
2831   GtkWidget *menu = combo_box->priv->popup_widget;
2832
2833   if (!GTK_IS_MENU_SHELL (menu))
2834     return;
2835   
2836   if (combo_box->priv->col_column == -1 &&
2837       combo_box->priv->row_column == -1 &&
2838       last)
2839     {
2840       gtk_container_child_get (GTK_CONTAINER (menu), 
2841                                last,
2842                                "right-attach", &current_col,
2843                                "top-attach", &current_row,
2844                                NULL);
2845       if (current_col + cols > combo_box->priv->wrap_width)
2846         {
2847           current_col = 0;
2848           current_row++;
2849         }
2850     }
2851   else
2852     {
2853       if (combo_box->priv->col_column != -1)
2854         gtk_tree_model_get (combo_box->priv->model, iter,
2855                             combo_box->priv->col_column, &cols,
2856                             -1);
2857       if (combo_box->priv->row_column != -1)
2858         gtk_tree_model_get (combo_box->priv->model, iter,
2859                             combo_box->priv->row_column, &rows,
2860                             -1);
2861
2862       while (1)
2863         {
2864           if (current_col + cols > combo_box->priv->wrap_width)
2865             {
2866               current_col = 0;
2867               current_row++;
2868             }
2869           
2870           if (!menu_occupied (GTK_MENU (menu), 
2871                               current_col, current_col + cols,
2872                               current_row, current_row + rows))
2873             break;
2874           
2875           current_col++;
2876         }
2877     }
2878
2879   /* set attach props */
2880   gtk_menu_attach (GTK_MENU (menu), item,
2881                    current_col, current_col + cols,
2882                    current_row, current_row + rows);
2883 }
2884
2885 static void
2886 gtk_combo_box_relayout (GtkComboBox *combo_box)
2887 {
2888   GList *list, *j;
2889   GtkWidget *menu;
2890
2891   menu = combo_box->priv->popup_widget;
2892   
2893   /* do nothing unless we are in menu style and realized */
2894   if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
2895     return;
2896   
2897   list = gtk_container_get_children (GTK_CONTAINER (menu));
2898   
2899   for (j = g_list_last (list); j; j = j->prev)
2900     gtk_container_remove (GTK_CONTAINER (menu), j->data);
2901   
2902   gtk_combo_box_menu_fill (combo_box);
2903
2904   g_list_free (list);
2905 }
2906
2907 /* callbacks */
2908 static gboolean
2909 gtk_combo_box_menu_button_press (GtkWidget      *widget,
2910                                  GdkEventButton *event,
2911                                  gpointer        user_data)
2912 {
2913   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2914
2915   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
2916       event->type == GDK_BUTTON_PRESS && event->button == 1)
2917     {
2918       if (combo_box->priv->focus_on_click && 
2919           !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
2920         gtk_widget_grab_focus (combo_box->priv->button);
2921
2922       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
2923
2924       return TRUE;
2925     }
2926
2927   return FALSE;
2928 }
2929
2930 static void
2931 gtk_combo_box_menu_item_activate (GtkWidget *item,
2932                                   gpointer   user_data)
2933 {
2934   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2935   GtkWidget *cell_view;
2936   GtkTreePath *path;
2937   GtkTreeIter iter;
2938
2939   cell_view = GTK_BIN (item)->child;
2940
2941   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
2942
2943   path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
2944
2945   if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
2946   {
2947     if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
2948       gtk_combo_box_set_active_iter (combo_box, &iter);
2949   }
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   if (gtk_tree_path_get_depth (path) > 1)
3274     {
3275       GtkTreePath *parent_path;
3276       GtkTreeIter iter;
3277       GtkWidget *parent;
3278
3279       parent_path = gtk_tree_path_copy (path);
3280       gtk_tree_path_up (parent_path);
3281       gtk_tree_model_get_iter (model, &iter, parent_path);
3282
3283       if (!gtk_tree_model_iter_has_child (model, &iter))
3284         {
3285           parent = find_menu_by_path (combo_box->priv->popup_widget, 
3286                                       parent_path, FALSE);
3287           gtk_menu_item_remove_submenu (GTK_MENU_ITEM (parent));
3288         }
3289     }
3290 }
3291
3292 static void
3293 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
3294                                     GtkTreePath      *path,
3295                                     GtkTreeIter      *iter,
3296                                     gint             *new_order,
3297                                     gpointer          user_data)
3298 {
3299   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3300
3301   gtk_combo_box_relayout (combo_box);
3302 }
3303                                     
3304 static void
3305 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3306                                 GtkTreePath  *path,
3307                                 GtkTreeIter  *iter,
3308                                 gpointer      user_data)
3309 {
3310   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3311   GtkWidget *item;
3312   gint width;
3313   gboolean is_separator;
3314
3315   if (!combo_box->priv->popup_widget)
3316     return;
3317
3318   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3319
3320   if (combo_box->priv->row_separator_func)
3321     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3322                                                            combo_box->priv->row_separator_data);
3323   else
3324     is_separator = FALSE;
3325
3326   if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3327     {
3328       gtk_combo_box_menu_row_deleted (model, path, combo_box);
3329       gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3330     }
3331
3332   if (combo_box->priv->wrap_width
3333       && item->parent == combo_box->priv->popup_widget)
3334     {
3335       GtkWidget *pitem = NULL;
3336       GtkTreePath *prev;
3337
3338       prev = gtk_tree_path_copy (path);
3339
3340       if (gtk_tree_path_prev (prev))
3341         pitem = find_menu_by_path (combo_box->priv->popup_widget, prev, FALSE);
3342
3343       gtk_tree_path_free (prev);
3344
3345       /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3346          move it */
3347       gtk_container_child_set (GTK_CONTAINER (combo_box->priv->popup_widget),
3348                                item, 
3349                                "left-attach", -1, 
3350                                "right-attach", -1,
3351                                "top-attach", -1, 
3352                                "bottom-attach", -1, 
3353                                NULL);
3354
3355       gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3356     }
3357
3358   width = gtk_combo_box_calc_requested_width (combo_box, path);
3359
3360   if (width > combo_box->priv->width)
3361     {
3362       if (combo_box->priv->cell_view)
3363         {
3364           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3365           gtk_widget_queue_resize (combo_box->priv->cell_view);
3366         }
3367       combo_box->priv->width = width;
3368     }
3369 }
3370
3371 /*
3372  * list style
3373  */
3374
3375 static void
3376 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3377 {
3378   GtkTreeSelection *sel;
3379
3380   combo_box->priv->button = gtk_toggle_button_new ();
3381   gtk_widget_set_parent (combo_box->priv->button,
3382                          GTK_BIN (combo_box)->child->parent);
3383   g_signal_connect (combo_box->priv->button, "button_press_event",
3384                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3385   g_signal_connect (combo_box->priv->button, "toggled",
3386                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3387
3388   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3389   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
3390                      combo_box->priv->arrow);
3391   combo_box->priv->separator = NULL;
3392   gtk_widget_show_all (combo_box->priv->button);
3393
3394   if (combo_box->priv->cell_view)
3395     {
3396       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
3397                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3398
3399       combo_box->priv->box = gtk_event_box_new ();
3400       gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->box), 
3401                                         FALSE);
3402
3403       if (combo_box->priv->has_frame)
3404         {
3405           combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
3406           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
3407                                      GTK_SHADOW_IN);
3408         }
3409       else 
3410         {
3411           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3412           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), 
3413                                             FALSE);
3414         }
3415       
3416       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
3417                              GTK_BIN (combo_box)->child->parent);
3418       gtk_container_add (GTK_CONTAINER (combo_box->priv->cell_view_frame),
3419                          combo_box->priv->box);
3420       gtk_widget_show_all (combo_box->priv->cell_view_frame);
3421
3422       g_signal_connect (combo_box->priv->box, "button_press_event",
3423                         G_CALLBACK (gtk_combo_box_list_button_pressed), 
3424                         combo_box);
3425     }
3426
3427   combo_box->priv->tree_view = gtk_tree_view_new ();
3428   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3429   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3430   gtk_tree_selection_set_select_function (sel,
3431                                           gtk_combo_box_list_select_func,
3432                                           NULL, NULL);
3433   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
3434                                      FALSE);
3435   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (combo_box->priv->tree_view),
3436                                      TRUE);
3437   if (combo_box->priv->row_separator_func)
3438     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3439                                           combo_box->priv->row_separator_func, 
3440                                           combo_box->priv->row_separator_data, 
3441                                           NULL);
3442   if (combo_box->priv->model)
3443     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3444                              combo_box->priv->model);
3445     
3446   combo_box->priv->column = gtk_tree_view_column_new ();
3447   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
3448                                combo_box->priv->column);
3449
3450   /* sync up */
3451   gtk_combo_box_sync_cells (combo_box, 
3452                             GTK_CELL_LAYOUT (combo_box->priv->column));
3453
3454   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3455     {
3456       GtkTreePath *path;
3457
3458       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3459       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
3460                                 path, NULL, FALSE);
3461       gtk_tree_path_free (path);
3462     }
3463
3464   /* set sample/popup widgets */
3465   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
3466
3467   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
3468                     G_CALLBACK (gtk_combo_box_list_key_press),
3469                     combo_box);
3470   g_signal_connect (combo_box->priv->tree_view, "enter_notify_event",
3471                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3472                     combo_box);
3473   g_signal_connect (combo_box->priv->tree_view, "row_expanded",
3474                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3475                     combo_box);
3476   g_signal_connect (combo_box->priv->tree_view, "row_collapsed",
3477                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3478                     combo_box);
3479   g_signal_connect (combo_box->priv->popup_window, "button_press_event",
3480                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3481                     combo_box);
3482   g_signal_connect (combo_box->priv->popup_window, "button_release_event",
3483                     G_CALLBACK (gtk_combo_box_list_button_released),
3484                     combo_box);
3485
3486   gtk_widget_show (combo_box->priv->tree_view);
3487 }
3488
3489 static void
3490 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3491 {
3492   /* disconnect signals */
3493   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
3494                                         G_SIGNAL_MATCH_DATA,
3495                                         0, 0, NULL, NULL, combo_box);
3496   g_signal_handlers_disconnect_matched (combo_box->priv->button,
3497                                         G_SIGNAL_MATCH_DATA,
3498                                         0, 0, NULL,
3499                                         gtk_combo_box_list_button_pressed,
3500                                         NULL);
3501   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3502                                         G_SIGNAL_MATCH_DATA,
3503                                         0, 0, NULL,
3504                                         gtk_combo_box_list_button_pressed,
3505                                         NULL);
3506   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3507                                         G_SIGNAL_MATCH_DATA,
3508                                         0, 0, NULL,
3509                                         gtk_combo_box_list_button_released,
3510                                         NULL);
3511
3512   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3513                                         G_SIGNAL_MATCH_DATA,
3514                                         0, 0, NULL, 
3515                                         gtk_combo_box_child_show,
3516                                         NULL);
3517
3518   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3519                                         G_SIGNAL_MATCH_DATA,
3520                                         0, 0, NULL, 
3521                                         gtk_combo_box_child_hide,
3522                                         NULL);
3523   
3524   if (combo_box->priv->box)
3525     g_signal_handlers_disconnect_matched (combo_box->priv->box,
3526                                           G_SIGNAL_MATCH_DATA,
3527                                           0, 0, NULL,
3528                                           gtk_combo_box_list_button_pressed,
3529                                           NULL);
3530
3531   /* destroy things (unparent will kill the latest ref from us)
3532    * last unref on button will destroy the arrow
3533    */
3534   gtk_widget_unparent (combo_box->priv->button);
3535   combo_box->priv->button = NULL;
3536   combo_box->priv->arrow = NULL;
3537
3538   if (combo_box->priv->cell_view)
3539     {
3540       g_object_set (combo_box->priv->cell_view,
3541                     "background-set", FALSE,
3542                     NULL);
3543     }
3544
3545   if (combo_box->priv->cell_view_frame)
3546     {
3547       gtk_widget_unparent (combo_box->priv->cell_view_frame);
3548       combo_box->priv->cell_view_frame = NULL;
3549       combo_box->priv->box = NULL;
3550     }
3551
3552   if (combo_box->priv->scroll_timer)
3553     {
3554       g_source_remove (combo_box->priv->scroll_timer);
3555       combo_box->priv->scroll_timer = 0;
3556     }
3557
3558   if (combo_box->priv->resize_idle_id)
3559     {
3560       g_source_remove (combo_box->priv->resize_idle_id);
3561       combo_box->priv->resize_idle_id = 0;
3562     }
3563
3564   gtk_widget_destroy (combo_box->priv->tree_view);
3565
3566   combo_box->priv->tree_view = NULL;
3567   if (combo_box->priv->popup_widget)
3568     {
3569       g_object_unref (combo_box->priv->popup_widget);
3570       combo_box->priv->popup_widget = NULL;
3571     }
3572 }
3573
3574 /* callbacks */
3575
3576 static gboolean
3577 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3578                                    GdkEventButton *event,
3579                                    gpointer        data)
3580 {
3581   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3582
3583   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3584
3585   if (ewidget == combo_box->priv->popup_window)
3586     return TRUE;
3587
3588   if ((ewidget != combo_box->priv->button && ewidget != combo_box->priv->box) ||
3589       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3590     return FALSE;
3591
3592   if (combo_box->priv->focus_on_click && 
3593       !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
3594     gtk_widget_grab_focus (combo_box->priv->button);
3595
3596   gtk_combo_box_popup (combo_box);
3597
3598   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
3599                                 TRUE);
3600
3601   combo_box->priv->auto_scroll = FALSE;
3602   if (combo_box->priv->scroll_timer == 0)
3603     combo_box->priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME, 
3604                                                    (GSourceFunc) gtk_combo_box_list_scroll_timeout, 
3605                                                    combo_box);
3606
3607   combo_box->priv->popup_in_progress = TRUE;
3608
3609   return TRUE;
3610 }
3611
3612 static gboolean
3613 gtk_combo_box_list_button_released (GtkWidget      *widget,
3614                                     GdkEventButton *event,
3615                                     gpointer        data)
3616 {
3617   gboolean ret;
3618   GtkTreePath *path = NULL;
3619   GtkTreeIter iter;
3620
3621   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3622
3623   gboolean popup_in_progress = FALSE;
3624
3625   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3626
3627   if (combo_box->priv->popup_in_progress)
3628     {
3629       popup_in_progress = TRUE;
3630       combo_box->priv->popup_in_progress = FALSE;
3631     }
3632
3633   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3634                                   FALSE);
3635   if (combo_box->priv->scroll_timer)
3636     {
3637       g_source_remove (combo_box->priv->scroll_timer);
3638       combo_box->priv->scroll_timer = 0;
3639     }
3640
3641   if (ewidget != combo_box->priv->tree_view)
3642     {
3643       if ((ewidget == combo_box->priv->button || 
3644            ewidget == combo_box->priv->box) &&
3645           !popup_in_progress &&
3646           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3647         {
3648           gtk_combo_box_popdown (combo_box);
3649           return TRUE;
3650         }
3651
3652       /* released outside treeview */
3653       if (ewidget != combo_box->priv->button && 
3654           ewidget != combo_box->priv->box)
3655         {
3656           gtk_combo_box_popdown (combo_box);
3657
3658           return TRUE;
3659         }
3660
3661       return FALSE;
3662     }
3663
3664   /* select something cool */
3665   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (combo_box->priv->tree_view),
3666                                        event->x, event->y,
3667                                        &path,
3668                                        NULL, NULL, NULL);
3669
3670   if (!ret)
3671     return TRUE; /* clicked outside window? */
3672
3673   gtk_tree_model_get_iter (combo_box->priv->model, &iter, path);
3674   gtk_tree_path_free (path);
3675
3676   gtk_combo_box_popdown (combo_box);
3677
3678   if (tree_column_row_is_sensitive (combo_box, &iter))
3679     gtk_combo_box_set_active_iter (combo_box, &iter);
3680
3681   return TRUE;
3682 }
3683
3684 static gboolean
3685 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3686                               GdkEventKey *event,
3687                               gpointer     data)
3688 {
3689   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3690   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3691
3692   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3693       state == GDK_MOD1_MASK)
3694     {
3695       gtk_combo_box_popdown (combo_box);
3696
3697       return TRUE;
3698     }
3699   
3700   return FALSE;
3701 }
3702
3703 static gboolean
3704 gtk_combo_box_list_key_press (GtkWidget   *widget,
3705                               GdkEventKey *event,
3706                               gpointer     data)
3707 {
3708   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3709   GtkTreeIter iter;
3710   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3711
3712   if (event->keyval == GDK_Escape ||
3713       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3714        state == GDK_MOD1_MASK))
3715     {
3716       gtk_combo_box_popdown (combo_box);
3717       
3718       /* reset active item -- this is incredibly lame and ugly */
3719       if (gtk_combo_box_get_active_iter (combo_box, &iter))
3720         gtk_combo_box_set_active_iter (combo_box, &iter);
3721       
3722       return TRUE;
3723     }
3724     
3725   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
3726       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
3727   {
3728     GtkTreeModel *model = NULL;
3729     
3730     gtk_combo_box_popdown (combo_box);
3731     
3732     if (combo_box->priv->model)
3733       {
3734         GtkTreeSelection *sel;
3735
3736         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3737
3738         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3739           gtk_combo_box_set_active_iter (combo_box, &iter);
3740       }
3741     
3742     return TRUE;
3743   }
3744
3745   return FALSE;
3746 }
3747
3748 static void
3749 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3750                                 gint         x, 
3751                                 gint         y)
3752 {
3753   GtkWidget *tree_view = combo_box->priv->tree_view;
3754   GtkAdjustment *adj;
3755   gdouble value;
3756
3757   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3758   if (adj && adj->upper - adj->lower > adj->page_size)
3759     {
3760       if (x <= tree_view->allocation.x && 
3761           adj->lower < adj->value)
3762         {
3763           value = adj->value - (tree_view->allocation.x - x + 1);
3764           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3765         }
3766       else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3767                adj->upper - adj->page_size > adj->value)
3768         {
3769           value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3770           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3771         }
3772     }
3773
3774   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3775   if (adj && adj->upper - adj->lower > adj->page_size)
3776     {
3777       if (y <= tree_view->allocation.y && 
3778           adj->lower < adj->value)
3779         {
3780           value = adj->value - (tree_view->allocation.y - y + 1);
3781           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3782         }
3783       else if (y >= tree_view->allocation.height &&
3784                adj->upper - adj->page_size > adj->value)
3785         {
3786           value = adj->value + (y - tree_view->allocation.height + 1);
3787           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3788         }
3789     }
3790 }
3791
3792 static gboolean
3793 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3794 {
3795   gint x, y;
3796
3797   if (combo_box->priv->auto_scroll)
3798     {
3799       gdk_window_get_pointer (combo_box->priv->tree_view->window, 
3800                               &x, &y, NULL);
3801       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3802     }
3803
3804   return TRUE;
3805 }
3806
3807 static gboolean 
3808 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3809                                  GdkEventCrossing *event,
3810                                  gpointer          data)
3811 {
3812   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3813
3814   combo_box->priv->auto_scroll = TRUE;
3815
3816   return TRUE;
3817 }
3818
3819 static gboolean
3820 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3821                                 GtkTreeModel     *model,
3822                                 GtkTreePath      *path,
3823                                 gboolean          path_currently_selected,
3824                                 gpointer          data)
3825 {
3826   GList *list;
3827   gboolean sensitive = FALSE;
3828
3829   for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
3830     {
3831       GList *cells, *cell;
3832       gboolean cell_sensitive, cell_visible;
3833       GtkTreeIter iter;
3834       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3835
3836       if (!column->visible)
3837         continue;
3838
3839       gtk_tree_model_get_iter (model, &iter, path);
3840       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3841                                                FALSE, FALSE);
3842
3843       cell = cells = gtk_tree_view_column_get_cell_renderers (column);
3844       while (cell)
3845         {
3846           g_object_get (cell->data,
3847                         "sensitive", &cell_sensitive,
3848                         "visible", &cell_visible,
3849                         NULL);
3850
3851           if (cell_visible && cell_sensitive)
3852             break;
3853
3854           cell = cell->next;
3855         }
3856       g_list_free (cells);
3857
3858       sensitive = cell_sensitive;
3859     }
3860
3861   return sensitive;
3862 }
3863
3864 static void
3865 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3866                                 GtkTreePath  *path,
3867                                 GtkTreeIter  *iter,
3868                                 gpointer      data)
3869 {
3870   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3871   gint width;
3872
3873   width = gtk_combo_box_calc_requested_width (combo_box, path);
3874
3875   if (width > combo_box->priv->width)
3876     {
3877       if (combo_box->priv->cell_view) 
3878         {
3879           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3880           gtk_widget_queue_resize (combo_box->priv->cell_view);
3881         }
3882       combo_box->priv->width = width;
3883     }
3884 }
3885
3886 /*
3887  * GtkCellLayout implementation
3888  */
3889
3890 static void
3891 pack_start_recurse (GtkWidget       *menu,
3892                     GtkCellRenderer *cell,
3893                     gboolean         expand)
3894 {
3895   GList *i, *list;
3896   GtkWidget *submenu;    
3897   
3898   list = gtk_container_get_children (GTK_CONTAINER (menu));
3899   for (i = list; i; i = i->next)
3900     {
3901       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3902         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3903                                     cell, expand);
3904
3905       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3906       if (submenu != NULL)
3907         pack_start_recurse (submenu, cell, expand);
3908     }
3909
3910   g_list_free (list);
3911 }
3912
3913 static void
3914 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
3915                                       GtkCellRenderer *cell,
3916                                       gboolean         expand)
3917 {
3918   ComboCellInfo *info;
3919   GtkComboBox *combo_box;
3920
3921   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3922   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3923
3924   combo_box = GTK_COMBO_BOX (layout);
3925
3926   g_object_ref_sink (cell);
3927
3928   info = g_new0 (ComboCellInfo, 1);
3929   info->cell = cell;
3930   info->expand = expand;
3931   info->pack = GTK_PACK_START;
3932
3933   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3934
3935   if (combo_box->priv->cell_view)
3936     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3937                                 cell, expand);
3938
3939   if (combo_box->priv->column)
3940     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
3941
3942   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3943     pack_start_recurse (combo_box->priv->popup_widget, cell, expand);
3944 }
3945
3946 static void
3947 pack_end_recurse (GtkWidget       *menu,
3948                   GtkCellRenderer *cell,
3949                   gboolean         expand)
3950 {
3951   GList *i, *list;
3952   GtkWidget *submenu;    
3953   
3954   list = gtk_container_get_children (GTK_CONTAINER (menu));
3955   for (i = list; i; i = i->next)
3956     {
3957       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3958         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3959                                   cell, expand);
3960
3961       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3962       if (submenu != NULL)
3963         pack_end_recurse (submenu, cell, expand);
3964     }
3965
3966   g_list_free (list);
3967 }
3968
3969 static void
3970 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
3971                                     GtkCellRenderer *cell,
3972                                     gboolean         expand)
3973 {
3974   ComboCellInfo *info;
3975   GtkComboBox *combo_box;
3976
3977   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3978   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3979
3980   combo_box = GTK_COMBO_BOX (layout);
3981
3982   g_object_ref_sink (cell);
3983
3984   info = g_new0 (ComboCellInfo, 1);
3985   info->cell = cell;
3986   info->expand = expand;
3987   info->pack = GTK_PACK_END;
3988
3989   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3990
3991   if (combo_box->priv->cell_view)
3992     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3993                               cell, expand);
3994
3995   if (combo_box->priv->column)
3996     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
3997
3998   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3999     pack_end_recurse (combo_box->priv->popup_widget, cell, expand);
4000 }
4001
4002 static void
4003 clear_recurse (GtkWidget *menu)
4004 {
4005   GList *i, *list;
4006   GtkWidget *submenu;    
4007   
4008   list = gtk_container_get_children (GTK_CONTAINER (menu));
4009   for (i = list; i; i = i->next)
4010     {
4011       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4012         gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child)); 
4013
4014       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4015       if (submenu != NULL)
4016         clear_recurse (submenu);
4017     }
4018
4019   g_list_free (list);
4020 }
4021
4022 static void
4023 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4024 {
4025   GtkComboBox *combo_box;
4026   GSList *i;
4027   
4028   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4029
4030   combo_box = GTK_COMBO_BOX (layout);
4031  
4032   if (combo_box->priv->cell_view)
4033     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
4034
4035   if (combo_box->priv->column)
4036     gtk_tree_view_column_clear (combo_box->priv->column);
4037
4038   for (i = combo_box->priv->cells; i; i = i->next)
4039     {
4040      ComboCellInfo *info = (ComboCellInfo *)i->data;
4041
4042       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4043       g_object_unref (info->cell);
4044       g_free (info);
4045       i->data = NULL;
4046     }
4047   g_slist_free (combo_box->priv->cells);
4048   combo_box->priv->cells = NULL;
4049
4050   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4051     clear_recurse (combo_box->priv->popup_widget);
4052 }
4053
4054 static void
4055 add_attribute_recurse (GtkWidget       *menu,
4056                        GtkCellRenderer *cell,
4057                        const gchar     *attribute,
4058                        gint             column)
4059 {
4060   GList *i, *list;
4061   GtkWidget *submenu;    
4062   
4063   list = gtk_container_get_children (GTK_CONTAINER (menu));
4064   for (i = list; i; i = i->next)
4065     {
4066       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4067         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4068                                        cell, attribute, column); 
4069
4070       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4071       if (submenu != NULL)
4072         add_attribute_recurse (submenu, cell, attribute, column);
4073     }
4074
4075   g_list_free (list);
4076 }
4077                        
4078 static void
4079 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
4080                                          GtkCellRenderer *cell,
4081                                          const gchar     *attribute,
4082                                          gint             column)
4083 {
4084   ComboCellInfo *info;
4085   GtkComboBox *combo_box;
4086
4087   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4088   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4089
4090   combo_box = GTK_COMBO_BOX (layout);
4091
4092   info = gtk_combo_box_get_cell_info (combo_box, cell);
4093
4094   info->attributes = g_slist_prepend (info->attributes,
4095                                       GINT_TO_POINTER (column));
4096   info->attributes = g_slist_prepend (info->attributes,
4097                                       g_strdup (attribute));
4098
4099   if (combo_box->priv->cell_view)
4100     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4101                                    cell, attribute, column);
4102
4103   if (combo_box->priv->column)
4104     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4105                                    cell, attribute, column);
4106
4107   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4108     add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4109   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4110 }
4111
4112 static void
4113 combo_cell_data_func (GtkCellLayout   *cell_layout,
4114                       GtkCellRenderer *cell,
4115                       GtkTreeModel    *tree_model,
4116                       GtkTreeIter     *iter,
4117                       gpointer         data)
4118 {
4119   ComboCellInfo *info = (ComboCellInfo *)data;
4120   GtkWidget *parent = NULL;
4121   
4122   if (!info->func)
4123     return;
4124
4125   (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
4126   
4127   if (GTK_IS_WIDGET (cell_layout))
4128     parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4129   
4130   if (GTK_IS_MENU_ITEM (parent) && 
4131       gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4132     g_object_set (cell, "sensitive", TRUE, NULL);
4133 }
4134
4135
4136 static void 
4137 set_cell_data_func_recurse (GtkWidget             *menu,
4138                             GtkCellRenderer       *cell,
4139                             ComboCellInfo         *info)
4140 {
4141   GList *i, *list;
4142   GtkWidget *submenu;    
4143   GtkWidget *cell_view;
4144   
4145  list = gtk_container_get_children (GTK_CONTAINER (menu));
4146   for (i = list; i; i = i->next)
4147     {
4148       cell_view = GTK_BIN (i->data)->child;
4149       if (GTK_IS_CELL_LAYOUT (cell_view))
4150         {
4151           /* Override sensitivity for inner nodes; we don't
4152            * want menuitems with submenus to appear insensitive 
4153            */ 
4154           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view), 
4155                                               cell, 
4156                                               combo_cell_data_func, 
4157                                               info, NULL); 
4158           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4159           if (submenu != NULL)
4160             set_cell_data_func_recurse (submenu, cell, info);
4161         }
4162     }
4163
4164   g_list_free (list);
4165 }
4166
4167 static void
4168 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4169                                               GtkCellRenderer       *cell,
4170                                               GtkCellLayoutDataFunc  func,
4171                                               gpointer               func_data,
4172                                               GDestroyNotify         destroy)
4173 {
4174   ComboCellInfo *info;
4175   GtkComboBox *combo_box;
4176
4177   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4178
4179   combo_box = GTK_COMBO_BOX (layout);
4180
4181   info = gtk_combo_box_get_cell_info (combo_box, cell);
4182   g_return_if_fail (info != NULL);
4183   
4184   if (info->destroy)
4185     {
4186       GDestroyNotify d = info->destroy;
4187
4188       info->destroy = NULL;
4189       d (info->func_data);
4190     }
4191
4192   info->func = func;
4193   info->func_data = func_data;
4194   info->destroy = destroy;
4195
4196   if (combo_box->priv->cell_view)
4197     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
4198
4199   if (combo_box->priv->column)
4200     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
4201
4202   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4203     set_cell_data_func_recurse (combo_box->priv->popup_widget, cell, info);
4204
4205   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4206 }
4207
4208 static void 
4209 clear_attributes_recurse (GtkWidget             *menu,
4210                           GtkCellRenderer       *cell)
4211 {
4212   GList *i, *list;
4213   GtkWidget *submenu;    
4214   
4215   list = gtk_container_get_children (GTK_CONTAINER (menu));
4216   for (i = list; i; i = i->next)
4217     {
4218       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4219         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4220                                             cell); 
4221       
4222       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4223       if (submenu != NULL)
4224         clear_attributes_recurse (submenu, cell);
4225     }
4226
4227   g_list_free (list);
4228 }
4229
4230 static void
4231 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
4232                                             GtkCellRenderer *cell)
4233 {
4234   ComboCellInfo *info;
4235   GtkComboBox *combo_box;
4236   GSList *list;
4237
4238   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4239   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4240
4241   combo_box = GTK_COMBO_BOX (layout);
4242
4243   info = gtk_combo_box_get_cell_info (combo_box, cell);
4244   g_return_if_fail (info != NULL);
4245
4246   list = info->attributes;
4247   while (list && list->next)
4248     {
4249       g_free (list->data);
4250       list = list->next->next;
4251     }
4252   g_slist_free (info->attributes);
4253   info->attributes = NULL;
4254
4255   if (combo_box->priv->cell_view)
4256     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
4257
4258   if (combo_box->priv->column)
4259     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
4260
4261   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4262     clear_attributes_recurse (combo_box->priv->popup_widget, cell);
4263
4264   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4265 }
4266
4267 static void 
4268 reorder_recurse (GtkWidget             *menu,
4269                  GtkCellRenderer       *cell,
4270                  gint                   position)
4271 {
4272   GList *i, *list;
4273   GtkWidget *submenu;    
4274   
4275   list = gtk_container_get_children (GTK_CONTAINER (menu));
4276   for (i = list; i; i = i->next)
4277     {
4278       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4279         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4280                                  cell, position); 
4281       
4282       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4283       if (submenu != NULL)
4284         reorder_recurse (submenu, cell, position);
4285     }
4286
4287   g_list_free (list);
4288 }
4289
4290 static void
4291 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
4292                                    GtkCellRenderer *cell,
4293                                    gint             position)
4294 {
4295   ComboCellInfo *info;
4296   GtkComboBox *combo_box;
4297   GSList *link;
4298
4299   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4300   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4301
4302   combo_box = GTK_COMBO_BOX (layout);
4303
4304   info = gtk_combo_box_get_cell_info (combo_box, cell);
4305
4306   g_return_if_fail (info != NULL);
4307   g_return_if_fail (position >= 0);
4308
4309   link = g_slist_find (combo_box->priv->cells, info);
4310
4311   g_return_if_fail (link != NULL);
4312
4313   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
4314   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
4315                                            position);
4316
4317   if (combo_box->priv->cell_view)
4318     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4319                              cell, position);
4320
4321   if (combo_box->priv->column)
4322     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
4323                              cell, position);
4324
4325   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4326     reorder_recurse (combo_box->priv->popup_widget, cell, position);
4327
4328   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4329 }
4330
4331 /*
4332  * public API
4333  */
4334
4335 /**
4336  * gtk_combo_box_new:
4337  *
4338  * Creates a new empty #GtkComboBox.
4339  *
4340  * Return value: A new #GtkComboBox.
4341  *
4342  * Since: 2.4
4343  */
4344 GtkWidget *
4345 gtk_combo_box_new (void)
4346 {
4347   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4348 }
4349
4350 /**
4351  * gtk_combo_box_new_with_model:
4352  * @model: A #GtkTreeModel.
4353  *
4354  * Creates a new #GtkComboBox with the model initialized to @model.
4355  *
4356  * Return value: A new #GtkComboBox.
4357  *
4358  * Since: 2.4
4359  */
4360 GtkWidget *
4361 gtk_combo_box_new_with_model (GtkTreeModel *model)
4362 {
4363   GtkComboBox *combo_box;
4364
4365   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4366
4367   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4368
4369   return GTK_WIDGET (combo_box);
4370 }
4371
4372 /**
4373  * gtk_combo_box_get_wrap_width:
4374  * @combo_box: A #GtkComboBox.
4375  *
4376  * Returns the wrap width which is used to determine the number
4377  * of columns for the popup menu. If the wrap width is larger than
4378  * 1, the combo box is in table mode.
4379  *
4380  * Returns: the wrap width.
4381  *
4382  * Since: 2.6
4383  */
4384 gint
4385 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4386 {
4387   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4388
4389   return combo_box->priv->wrap_width;
4390 }
4391
4392 /**
4393  * gtk_combo_box_set_wrap_width:
4394  * @combo_box: A #GtkComboBox.
4395  * @width: Preferred number of columns.
4396  *
4397  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4398  * the preferred number of columns when you want the popup to be layed out
4399  * in a table.
4400  *
4401  * Since: 2.4
4402  */
4403 void
4404 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4405                               gint         width)
4406 {
4407   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4408   g_return_if_fail (width >= 0);
4409
4410   if (width != combo_box->priv->wrap_width)
4411     {
4412       combo_box->priv->wrap_width = width;
4413
4414       gtk_combo_box_check_appearance (combo_box);
4415       gtk_combo_box_relayout (combo_box);
4416       
4417       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4418     }
4419 }
4420
4421 /**
4422  * gtk_combo_box_get_row_span_column:
4423  * @combo_box: A #GtkComboBox.
4424  *
4425  * Returns the column with row span information for @combo_box.
4426  *
4427  * Returns: the row span column.
4428  *
4429  * Since: 2.6
4430  */
4431 gint
4432 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4433 {
4434   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4435
4436   return combo_box->priv->row_column;
4437 }
4438
4439 /**
4440  * gtk_combo_box_set_row_span_column:
4441  * @combo_box: A #GtkComboBox.
4442  * @row_span: A column in the model passed during construction.
4443  *
4444  * Sets the column with row span information for @combo_box to be @row_span.
4445  * The row span column contains integers which indicate how many rows
4446  * an item should span.
4447  *
4448  * Since: 2.4
4449  */
4450 void
4451 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4452                                    gint         row_span)
4453 {
4454   gint col;
4455
4456   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4457
4458   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4459   g_return_if_fail (row_span >= -1 && row_span < col);
4460
4461   if (row_span != combo_box->priv->row_column)
4462     {
4463       combo_box->priv->row_column = row_span;
4464       
4465       gtk_combo_box_relayout (combo_box);
4466  
4467       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4468     }
4469 }
4470
4471 /**
4472  * gtk_combo_box_get_column_span_column:
4473  * @combo_box: A #GtkComboBox.
4474  *
4475  * Returns the column with column span information for @combo_box.
4476  *
4477  * Returns: the column span column.
4478  *
4479  * Since: 2.6
4480  */
4481 gint
4482 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4483 {
4484   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4485
4486   return combo_box->priv->col_column;
4487 }
4488
4489 /**
4490  * gtk_combo_box_set_column_span_column:
4491  * @combo_box: A #GtkComboBox.
4492  * @column_span: A column in the model passed during construction.
4493  *
4494  * Sets the column with column span information for @combo_box to be
4495  * @column_span. The column span column contains integers which indicate
4496  * how many columns an item should span.
4497  *
4498  * Since: 2.4
4499  */
4500 void
4501 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4502                                       gint         column_span)
4503 {
4504   gint col;
4505
4506   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4507
4508   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4509   g_return_if_fail (column_span >= -1 && column_span < col);
4510
4511   if (column_span != combo_box->priv->col_column)
4512     {
4513       combo_box->priv->col_column = column_span;
4514       
4515       gtk_combo_box_relayout (combo_box);
4516
4517       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4518     }
4519 }
4520
4521 /**
4522  * gtk_combo_box_get_active:
4523  * @combo_box: A #GtkComboBox.
4524  *
4525  * Returns the index of the currently active item, or -1 if there's no
4526  * active item. If the model is a non-flat treemodel, and the active item 
4527  * is not an immediate child of the root of the tree, this function returns 
4528  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where 
4529  * <literal>path</literal> is the #GtkTreePath of the active item.
4530  *
4531  * Return value: An integer which is the index of the currently active item, or
4532  * -1 if there's no active item.
4533  *
4534  * Since: 2.4
4535  */
4536 gint
4537 gtk_combo_box_get_active (GtkComboBox *combo_box)
4538 {
4539   gint result;
4540   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4541
4542   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
4543     {
4544       GtkTreePath *path;
4545
4546       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);      
4547       result = gtk_tree_path_get_indices (path)[0];
4548       gtk_tree_path_free (path);
4549     }
4550   else
4551     result = -1;
4552
4553   return result;
4554 }
4555
4556 /**
4557  * gtk_combo_box_set_active:
4558  * @combo_box: A #GtkComboBox.
4559  * @index_: An index in the model passed during construction, or -1 to have
4560  * no active item.
4561  *
4562  * Sets the active item of @combo_box to be the item at @index.
4563  *
4564  * Since: 2.4
4565  */
4566 void
4567 gtk_combo_box_set_active (GtkComboBox *combo_box,
4568                           gint         index_)
4569 {
4570   GtkTreePath *path = NULL;
4571   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4572   g_return_if_fail (index_ >= -1);
4573
4574   if (index_ != -1)
4575     path = gtk_tree_path_new_from_indices (index_, -1);
4576    
4577   gtk_combo_box_set_active_internal (combo_box, path);
4578
4579   if (path)
4580     gtk_tree_path_free (path);
4581 }
4582
4583 static void
4584 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4585                                    GtkTreePath *path)
4586 {
4587   GtkTreePath *active_path;
4588   gint path_cmp;
4589
4590   if (path && gtk_tree_row_reference_valid (combo_box->priv->active_row))
4591     {
4592       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4593       path_cmp = gtk_tree_path_compare (path, active_path);
4594       gtk_tree_path_free (active_path);
4595       if (path_cmp == 0)
4596         return;
4597     }
4598
4599   if (combo_box->priv->active_row)
4600     {
4601       gtk_tree_row_reference_free (combo_box->priv->active_row);
4602       combo_box->priv->active_row = NULL;
4603     }
4604   
4605   if (!path)
4606     {
4607       if (combo_box->priv->tree_view)
4608         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
4609       else
4610         {
4611           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
4612
4613           if (GTK_IS_MENU (menu))
4614             gtk_menu_set_active (menu, -1);
4615         }
4616
4617       if (combo_box->priv->cell_view)
4618         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
4619     }
4620   else
4621     {
4622       combo_box->priv->active_row = 
4623         gtk_tree_row_reference_new (combo_box->priv->model, path);
4624
4625       if (combo_box->priv->tree_view)
4626         {
4627           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), 
4628                                     path, NULL, FALSE);
4629         }
4630       else if (GTK_IS_MENU (combo_box->priv->popup_widget))
4631         {
4632           /* FIXME handle nested menus better */
4633           gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), 
4634                                gtk_tree_path_get_indices (path)[0]);
4635         }
4636
4637       if (combo_box->priv->cell_view)
4638         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
4639                                          path);
4640     }
4641
4642   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4643   g_object_notify (G_OBJECT (combo_box), "active");
4644 }
4645
4646
4647 /**
4648  * gtk_combo_box_get_active_iter:
4649  * @combo_box: A #GtkComboBox
4650  * @iter: The uninitialized #GtkTreeIter.
4651  * 
4652  * Sets @iter to point to the current active item, if it exists.
4653  * 
4654  * Return value: %TRUE, if @iter was set
4655  *
4656  * Since: 2.4
4657  **/
4658 gboolean
4659 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4660                                GtkTreeIter     *iter)
4661 {
4662   GtkTreePath *path;
4663   gboolean result;
4664
4665   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4666
4667   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4668     return FALSE;
4669
4670   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4671   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4672   gtk_tree_path_free (path);
4673
4674   return result;
4675 }
4676
4677 /**
4678  * gtk_combo_box_set_active_iter:
4679  * @combo_box: A #GtkComboBox
4680  * @iter: The #GtkTreeIter.
4681  * 
4682  * Sets the current active item to be the one referenced by @iter. 
4683  * @iter must correspond to a path of depth one.
4684  * 
4685  * Since: 2.4
4686  **/
4687 void
4688 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4689                                GtkTreeIter     *iter)
4690 {
4691   GtkTreePath *path;
4692
4693   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4694
4695   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4696   gtk_combo_box_set_active_internal (combo_box, path);
4697   gtk_tree_path_free (path);
4698 }
4699
4700 /**
4701  * gtk_combo_box_set_model:
4702  * @combo_box: A #GtkComboBox.
4703  * @model: A #GtkTreeModel.
4704  *
4705  * Sets the model used by @combo_box to be @model. Will unset a previously set 
4706  * model (if applicable). If model is %NULL, then it will unset the model.
4707  *
4708  * Note that this function does not clear the cell renderers, you have to 
4709  * call gtk_combo_box_cell_layout_clear() yourself if you need to set up 
4710  * different cell renderers for the new model.
4711  *
4712  * Since: 2.4
4713  */
4714 void
4715 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4716                          GtkTreeModel *model)
4717 {
4718   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4719   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4720
4721   if (model == combo_box->priv->model)
4722     return;
4723   
4724   gtk_combo_box_unset_model (combo_box);
4725
4726   if (model == NULL)
4727     return;
4728
4729   combo_box->priv->model = model;
4730   g_object_ref (combo_box->priv->model);
4731
4732   combo_box->priv->inserted_id =
4733     g_signal_connect (combo_box->priv->model, "row_inserted",
4734                       G_CALLBACK (gtk_combo_box_model_row_inserted),
4735                       combo_box);
4736   combo_box->priv->deleted_id =
4737     g_signal_connect (combo_box->priv->model, "row_deleted",
4738                       G_CALLBACK (gtk_combo_box_model_row_deleted),
4739                       combo_box);
4740   combo_box->priv->reordered_id =
4741     g_signal_connect (combo_box->priv->model, "rows_reordered",
4742                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
4743                       combo_box);
4744   combo_box->priv->changed_id =
4745     g_signal_connect (combo_box->priv->model, "row_changed",
4746                       G_CALLBACK (gtk_combo_box_model_row_changed),
4747                       combo_box);
4748       
4749   if (combo_box->priv->tree_view)
4750     {
4751       /* list mode */
4752       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4753                                combo_box->priv->model);
4754       gtk_combo_box_list_popup_resize (combo_box);
4755     }
4756   else
4757     {
4758       /* menu mode */
4759       if (combo_box->priv->popup_widget)
4760         gtk_combo_box_menu_fill (combo_box);
4761
4762     }
4763
4764   if (combo_box->priv->cell_view)
4765     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4766                              combo_box->priv->model);
4767 }
4768
4769 /**
4770  * gtk_combo_box_get_model
4771  * @combo_box: A #GtkComboBox.
4772  *
4773  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4774  *
4775  * Return value: A #GtkTreeModel which was passed during construction.
4776  *
4777  * Since: 2.4
4778  */
4779 GtkTreeModel *
4780 gtk_combo_box_get_model (GtkComboBox *combo_box)
4781 {
4782   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4783
4784   return combo_box->priv->model;
4785 }
4786
4787
4788 /* convenience API for simple text combos */
4789
4790 /**
4791  * gtk_combo_box_new_text:
4792  *
4793  * Convenience function which constructs a new text combo box, which is a
4794  * #GtkComboBox just displaying strings. If you use this function to create
4795  * a text combo box, you should only manipulate its data source with the
4796  * following convenience functions: gtk_combo_box_append_text(),
4797  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
4798  * gtk_combo_box_remove_text().
4799  *
4800  * Return value: A new text combo box.
4801  *
4802  * Since: 2.4
4803  */
4804 GtkWidget *
4805 gtk_combo_box_new_text (void)
4806 {
4807   GtkWidget *combo_box;
4808   GtkCellRenderer *cell;
4809   GtkListStore *store;
4810
4811   store = gtk_list_store_new (1, G_TYPE_STRING);
4812   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
4813   g_object_unref (store);
4814
4815   cell = gtk_cell_renderer_text_new ();
4816   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
4817   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
4818                                   "text", 0,
4819                                   NULL);
4820
4821   return combo_box;
4822 }
4823
4824 /**
4825  * gtk_combo_box_append_text:
4826  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
4827  * @text: A string.
4828  *
4829  * Appends @string to the list of strings stored in @combo_box. Note that
4830  * you can only use this function with combo boxes constructed with
4831  * gtk_combo_box_new_text().
4832  *
4833  * Since: 2.4
4834  */
4835 void
4836 gtk_combo_box_append_text (GtkComboBox *combo_box,
4837                            const gchar *text)
4838 {
4839   GtkTreeIter iter;
4840   GtkListStore *store;
4841
4842   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4843   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4844   g_return_if_fail (text != NULL);
4845
4846   store = GTK_LIST_STORE (combo_box->priv->model);
4847
4848   gtk_list_store_append (store, &iter);
4849   gtk_list_store_set (store, &iter, 0, text, -1);
4850 }
4851
4852 /**
4853  * gtk_combo_box_insert_text:
4854  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
4855  * @position: An index to insert @text.
4856  * @text: A string.
4857  *
4858  * Inserts @string at @position in the list of strings stored in @combo_box.
4859  * Note that you can only use this function with combo boxes constructed
4860  * with gtk_combo_box_new_text().
4861  *
4862  * Since: 2.4
4863  */
4864 void
4865 gtk_combo_box_insert_text (GtkComboBox *combo_box,
4866                            gint         position,
4867                            const gchar *text)
4868 {
4869   GtkTreeIter iter;
4870   GtkListStore *store;
4871
4872   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4873   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4874   g_return_if_fail (position >= 0);
4875   g_return_if_fail (text != NULL);
4876
4877   store = GTK_LIST_STORE (combo_box->priv->model);
4878
4879   gtk_list_store_insert (store, &iter, position);
4880   gtk_list_store_set (store, &iter, 0, text, -1);
4881 }
4882
4883 /**
4884  * gtk_combo_box_prepend_text:
4885  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4886  * @text: A string.
4887  *
4888  * Prepends @string to the list of strings stored in @combo_box. Note that
4889  * you can only use this function with combo boxes constructed with
4890  * gtk_combo_box_new_text().
4891  *
4892  * Since: 2.4
4893  */
4894 void
4895 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
4896                             const gchar *text)
4897 {
4898   GtkTreeIter iter;
4899   GtkListStore *store;
4900
4901   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4902   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4903   g_return_if_fail (text != NULL);
4904
4905   store = GTK_LIST_STORE (combo_box->priv->model);
4906
4907   gtk_list_store_prepend (store, &iter);
4908   gtk_list_store_set (store, &iter, 0, text, -1);
4909 }
4910
4911 /**
4912  * gtk_combo_box_remove_text:
4913  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4914  * @position: Index of the item to remove.
4915  *
4916  * Removes the string at @position from @combo_box. Note that you can only use
4917  * this function with combo boxes constructed with gtk_combo_box_new_text().
4918  *
4919  * Since: 2.4
4920  */
4921 void
4922 gtk_combo_box_remove_text (GtkComboBox *combo_box,
4923                            gint         position)
4924 {
4925   GtkTreeIter iter;
4926   GtkListStore *store;
4927
4928   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4929   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4930   g_return_if_fail (position >= 0);
4931
4932   store = GTK_LIST_STORE (combo_box->priv->model);
4933
4934   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
4935                                      NULL, position))
4936     gtk_list_store_remove (store, &iter);
4937 }
4938
4939 /**
4940  * gtk_combo_box_get_active_text:
4941  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4942  *
4943  * Returns the currently active string in @combo_box or %NULL if none
4944  * is selected.  Note that you can only use this function with combo
4945  * boxes constructed with gtk_combo_box_new_text() and with 
4946  * #GtkComboBoxEntry<!-- -->s.
4947  *
4948  * Returns: a newly allocated string containing the currently active text.
4949  *
4950  * Since: 2.6
4951  */
4952 gchar *
4953 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
4954 {
4955   GtkComboBoxClass *class;
4956
4957   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4958
4959   class = GTK_COMBO_BOX_GET_CLASS (combo_box);
4960
4961   if (class->get_active_text)
4962     return (* class->get_active_text) (combo_box);
4963
4964   return NULL;
4965 }
4966
4967 static gchar *
4968 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
4969 {
4970   GtkTreeIter iter;
4971   gchar *text = NULL;
4972
4973   g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
4974
4975   if (gtk_combo_box_get_active_iter (combo_box, &iter))
4976     gtk_tree_model_get (combo_box->priv->model, &iter, 
4977                         0, &text, -1);
4978
4979   return text;
4980 }
4981
4982 static void
4983 gtk_combo_box_real_move_active (GtkComboBox   *combo_box,
4984                                 GtkScrollType  scroll)
4985 {
4986   GtkTreeIter iter;
4987   GtkTreeIter new_iter;
4988   gboolean    active_iter;
4989   gboolean    found;
4990
4991   if (!combo_box->priv->model)
4992     {
4993       gtk_widget_error_bell (GTK_WIDGET (combo_box));
4994       return;
4995     }
4996
4997   active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4998
4999   switch (scroll)
5000     {
5001     case GTK_SCROLL_STEP_BACKWARD:
5002     case GTK_SCROLL_STEP_UP:
5003     case GTK_SCROLL_STEP_LEFT:
5004       if (active_iter)
5005         {
5006           found = tree_prev (combo_box, combo_box->priv->model,
5007                              &iter, &new_iter, FALSE);
5008           break;
5009         }
5010       /* else fall through */
5011
5012     case GTK_SCROLL_PAGE_FORWARD:
5013     case GTK_SCROLL_PAGE_DOWN:
5014     case GTK_SCROLL_PAGE_RIGHT:
5015     case GTK_SCROLL_END:
5016       found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5017       break;
5018
5019     case GTK_SCROLL_STEP_FORWARD:
5020     case GTK_SCROLL_STEP_DOWN:
5021     case GTK_SCROLL_STEP_RIGHT:
5022       if (active_iter)
5023         {
5024           found = tree_next (combo_box, combo_box->priv->model,
5025                              &iter, &new_iter, FALSE);
5026           break;
5027         }
5028       /* else fall through */
5029
5030     case GTK_SCROLL_PAGE_BACKWARD:
5031     case GTK_SCROLL_PAGE_UP:
5032     case GTK_SCROLL_PAGE_LEFT:
5033     case GTK_SCROLL_START:
5034       found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5035       break;
5036
5037     default:
5038       return;
5039     }
5040
5041   if (found && active_iter)
5042     {
5043       GtkTreePath *old_path;
5044       GtkTreePath *new_path;
5045
5046       old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5047       new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5048
5049       if (gtk_tree_path_compare (old_path, new_path) == 0)
5050         found = FALSE;
5051
5052       gtk_tree_path_free (old_path);
5053       gtk_tree_path_free (new_path);
5054     }
5055
5056   if (found)
5057     {
5058       gtk_combo_box_set_active_iter (combo_box, &new_iter);
5059     }
5060   else
5061     {
5062       gtk_widget_error_bell (GTK_WIDGET (combo_box));
5063     }
5064 }
5065
5066 static gboolean
5067 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5068                                  gboolean   group_cycling)
5069 {
5070   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5071
5072   gtk_widget_grab_focus (combo_box->priv->button);
5073
5074   return TRUE;
5075 }
5076
5077 static void
5078 gtk_combo_box_grab_focus (GtkWidget *widget)
5079 {
5080   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5081
5082   gtk_widget_grab_focus (combo_box->priv->button);
5083 }
5084
5085 static void
5086 gtk_combo_box_destroy (GtkObject *object)
5087 {
5088   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5089
5090   if (combo_box->priv->popup_idle_id > 0)
5091     {
5092       g_source_remove (combo_box->priv->popup_idle_id);
5093       combo_box->priv->popup_idle_id = 0;
5094     }
5095
5096   gtk_combo_box_popdown (combo_box);
5097
5098   if (combo_box->priv->row_separator_destroy)
5099     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5100
5101   combo_box->priv->row_separator_func = NULL;
5102   combo_box->priv->row_separator_data = NULL;
5103   combo_box->priv->row_separator_destroy = NULL;
5104
5105   GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5106   combo_box->priv->cell_view = NULL;
5107 }
5108
5109 static void
5110 gtk_combo_box_finalize (GObject *object)
5111 {
5112   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5113   GSList *i;
5114   
5115   if (GTK_IS_MENU (combo_box->priv->popup_widget))
5116     {
5117       gtk_combo_box_menu_destroy (combo_box);
5118       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5119       combo_box->priv->popup_widget = NULL;
5120     }
5121   
5122   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5123     gtk_combo_box_list_destroy (combo_box);
5124
5125   if (combo_box->priv->popup_window)
5126     gtk_widget_destroy (combo_box->priv->popup_window);
5127
5128   gtk_combo_box_unset_model (combo_box);
5129
5130   for (i = combo_box->priv->cells; i; i = i->next)
5131     {
5132       ComboCellInfo *info = (ComboCellInfo *)i->data;
5133       GSList *list = info->attributes;
5134
5135       if (info->destroy)
5136         info->destroy (info->func_data);
5137
5138       while (list && list->next)
5139         {
5140           g_free (list->data);
5141           list = list->next->next;
5142         }
5143       g_slist_free (info->attributes);
5144
5145       g_object_unref (info->cell);
5146       g_free (info);
5147     }
5148    g_slist_free (combo_box->priv->cells);
5149
5150    g_free (combo_box->priv->tearoff_title);
5151
5152    G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5153 }
5154
5155 static gboolean
5156 gtk_cell_editable_key_press (GtkWidget   *widget,
5157                              GdkEventKey *event,
5158                              gpointer     data)
5159 {
5160   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5161
5162   if (event->keyval == GDK_Escape)
5163     {
5164       combo_box->priv->editing_canceled = TRUE;
5165
5166       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5167       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5168       
5169       return TRUE;
5170     }
5171   else if (event->keyval == GDK_Return)
5172     {
5173       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5174       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5175       
5176       return TRUE;
5177     }
5178
5179   return FALSE;
5180 }
5181
5182 static gboolean
5183 popdown_idle (gpointer data)
5184 {
5185   GtkComboBox *combo_box;
5186
5187   combo_box = GTK_COMBO_BOX (data);
5188   
5189   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5190   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5191
5192   g_object_unref (combo_box);
5193
5194   return FALSE;
5195 }
5196
5197 static void
5198 popdown_handler (GtkWidget *widget,
5199                  gpointer   data)
5200 {
5201   gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5202 }
5203
5204 static gboolean
5205 popup_idle (gpointer data)
5206 {
5207   GtkComboBox *combo_box;
5208
5209   combo_box = GTK_COMBO_BOX (data);
5210
5211   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5212       combo_box->priv->cell_view)
5213     g_signal_connect_object (combo_box->priv->popup_widget,
5214                              "unmap", G_CALLBACK (popdown_handler),
5215                              combo_box, 0);
5216   
5217   /* we unset this if a menu item is activated */
5218   combo_box->priv->editing_canceled = TRUE;
5219   gtk_combo_box_popup (combo_box);
5220
5221   combo_box->priv->popup_idle_id = 0;
5222
5223   return FALSE;
5224 }
5225
5226 static void
5227 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5228                              GdkEvent        *event)
5229 {
5230   GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5231
5232   combo_box->priv->is_cell_renderer = TRUE;
5233
5234   if (combo_box->priv->cell_view)
5235     {
5236       g_signal_connect_object (combo_box->priv->button, "key_press_event",
5237                                G_CALLBACK (gtk_cell_editable_key_press), 
5238                                cell_editable, 0);  
5239
5240       gtk_widget_grab_focus (combo_box->priv->button);
5241     }
5242   else
5243     {
5244       g_signal_connect_object (GTK_BIN (combo_box)->child, "key_press_event",
5245                                G_CALLBACK (gtk_cell_editable_key_press), 
5246                                cell_editable, 0);  
5247
5248       gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5249       GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5250     }
5251
5252   /* we do the immediate popup only for the optionmenu-like 
5253    * appearance 
5254    */  
5255   if (combo_box->priv->is_cell_renderer && 
5256       combo_box->priv->cell_view && !combo_box->priv->tree_view)
5257     combo_box->priv->popup_idle_id = gdk_threads_add_idle (popup_idle, combo_box);
5258 }
5259
5260
5261 /**
5262  * gtk_combo_box_get_add_tearoffs:
5263  * @combo_box: a #GtkComboBox
5264  * 
5265  * Gets the current value of the :add-tearoffs property.
5266  * 
5267  * Return value: the current value of the :add-tearoffs property.
5268  **/
5269 gboolean
5270 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5271 {
5272   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5273
5274   return combo_box->priv->add_tearoffs;
5275 }
5276
5277 /**
5278  * gtk_combo_box_set_add_tearoffs:
5279  * @combo_box: a #GtkComboBox 
5280  * @add_tearoffs: %TRUE to add tearoff menu items
5281  *  
5282  * Sets whether the popup menu should have a tearoff 
5283  * menu item.
5284  *
5285  * Since: 2.6
5286  **/
5287 void
5288 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5289                                 gboolean     add_tearoffs)
5290 {
5291   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5292
5293   add_tearoffs = add_tearoffs != FALSE;
5294
5295   if (combo_box->priv->add_tearoffs != add_tearoffs)
5296     {
5297       combo_box->priv->add_tearoffs = add_tearoffs;
5298       gtk_combo_box_check_appearance (combo_box);
5299       gtk_combo_box_relayout (combo_box);
5300       g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5301     }
5302 }
5303
5304 /**
5305  * gtk_combo_box_get_title:
5306  * @combo_box: a #GtkComboBox
5307  *
5308  * Gets the current title of the menu in tearoff mode. See
5309  * gtk_combo_box_set_add_tearoffs().
5310  *
5311  * Returns: the menu's title in tearoff mode. This is an internal copy of the
5312  * string which must not be freed.
5313  *
5314  * Since: 2.10
5315  */
5316 G_CONST_RETURN gchar*
5317 gtk_combo_box_get_title (GtkComboBox *combo_box)
5318 {
5319   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5320   
5321   return combo_box->priv->tearoff_title;
5322 }
5323
5324 static void
5325 gtk_combo_box_update_title (GtkComboBox *combo_box)
5326 {
5327   gtk_combo_box_check_appearance (combo_box);
5328   
5329   if (combo_box->priv->popup_widget && 
5330       GTK_IS_MENU (combo_box->priv->popup_widget))
5331     gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget), 
5332                         combo_box->priv->tearoff_title);
5333 }
5334
5335 /**
5336  * gtk_combo_box_set_title:
5337  * @combo_box: a #GtkComboBox 
5338  * @title: a title for the menu in tearoff mode.
5339  *  
5340  * Sets the menu's title in tearoff mode.
5341  *
5342  * Since: 2.10
5343  */
5344 void
5345 gtk_combo_box_set_title (GtkComboBox *combo_box,
5346                          const gchar *title)
5347 {
5348   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5349
5350   if (strcmp (title ? title : "", 
5351               combo_box->priv->tearoff_title ? combo_box->priv->tearoff_title : "") != 0)
5352     {
5353       g_free (combo_box->priv->tearoff_title);
5354       combo_box->priv->tearoff_title = g_strdup (title);
5355
5356       gtk_combo_box_update_title (combo_box);
5357
5358       g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5359     }
5360 }
5361
5362 gboolean
5363 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5364 {
5365   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5366
5367   return combo_box->priv->editing_canceled;
5368 }
5369
5370 /**
5371  * gtk_combo_box_get_popup_accessible:
5372  * @combo_box: a #GtkComboBox
5373  *
5374  * Gets the accessible object corresponding to the combo box's popup.
5375  *
5376  * This function is mostly intended for use by accessibility technologies;
5377  * applications should have little use for it.
5378  *
5379  * Returns: the accessible object corresponding to the combo box's popup.
5380  *
5381  * Since: 2.6
5382  **/
5383 AtkObject*
5384 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5385 {
5386   AtkObject *atk_obj;
5387
5388   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5389
5390   if (combo_box->priv->popup_widget)
5391     {
5392       atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5393       return atk_obj;
5394     }
5395
5396   return NULL;
5397 }
5398
5399 /**
5400  * gtk_combo_box_get_row_separator_func:
5401  * @combo_box: a #GtkComboBox
5402  * 
5403  * Returns the current row separator function.
5404  * 
5405  * Return value: the current row separator function.
5406  *
5407  * Since: 2.6
5408  **/
5409 GtkTreeViewRowSeparatorFunc 
5410 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5411 {
5412   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5413
5414   return combo_box->priv->row_separator_func;
5415 }
5416
5417 /**
5418  * gtk_combo_box_set_row_separator_func:
5419  * @combo_box: a #GtkComboBox
5420  * @func: a #GtkTreeViewRowSeparatorFunc
5421  * @data: user data to pass to @func, or %NULL
5422  * @destroy: destroy notifier for @data, or %NULL
5423  * 
5424  * Sets the row separator function, which is used to determine
5425  * whether a row should be drawn as a separator. If the row separator
5426  * function is %NULL, no separators are drawn. This is the default value.
5427  *
5428  * Since: 2.6
5429  **/
5430 void
5431 gtk_combo_box_set_row_separator_func (GtkComboBox                 *combo_box,
5432                                       GtkTreeViewRowSeparatorFunc  func,
5433                                       gpointer                     data,
5434                                       GtkDestroyNotify             destroy)
5435 {
5436   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5437
5438   if (combo_box->priv->row_separator_destroy)
5439     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5440
5441   combo_box->priv->row_separator_func = func;
5442   combo_box->priv->row_separator_data = data;
5443   combo_box->priv->row_separator_destroy = destroy;
5444
5445   if (combo_box->priv->tree_view)
5446     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
5447                                           func, data, NULL);
5448
5449   gtk_combo_box_relayout (combo_box);
5450
5451   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5452 }
5453
5454
5455 /**
5456  * gtk_combo_box_set_focus_on_click:
5457  * @combo: a #GtkComboBox
5458  * @focus_on_click: whether the combo box grabs focus when clicked 
5459  *    with the mouse
5460  * 
5461  * Sets whether the combo box will grab focus when it is clicked with 
5462  * the mouse. Making mouse clicks not grab focus is useful in places 
5463  * like toolbars where you don't want the keyboard focus removed from 
5464  * the main area of the application.
5465  *
5466  * Since: 2.6
5467  **/
5468 void
5469 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5470                                   gboolean     focus_on_click)
5471 {
5472   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5473   
5474   focus_on_click = focus_on_click != FALSE;
5475
5476   if (combo_box->priv->focus_on_click != focus_on_click)
5477     {
5478       combo_box->priv->focus_on_click = focus_on_click;
5479
5480       if (combo_box->priv->button)
5481         gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5482                                        focus_on_click);
5483       
5484       g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5485     }
5486 }
5487
5488 /**
5489  * gtk_combo_box_get_focus_on_click:
5490  * @combo: a #GtkComboBox
5491  * 
5492  * Returns whether the combo box grabs focus when it is clicked 
5493  * with the mouse. See gtk_combo_box_set_focus_on_click().
5494  *
5495  * Return value: %TRUE if the combo box grabs focus when it is 
5496  *     clicked with the mouse.
5497  *
5498  * Since: 2.6
5499  **/
5500 gboolean
5501 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5502 {
5503   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5504   
5505   return combo_box->priv->focus_on_click;
5506 }
5507
5508
5509 #define __GTK_COMBO_BOX_C__
5510 #include "gtkaliasdef.c"