]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Undo bogus commit.
[~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_ETCHED_IN);
1292           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
1293                              combo_box->priv->popup_frame);
1294
1295           gtk_widget_show (combo_box->priv->popup_frame);
1296
1297           combo_box->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1298           
1299           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1300                                           GTK_POLICY_NEVER,
1301                                           GTK_POLICY_NEVER);
1302           gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1303                                                GTK_SHADOW_NONE);
1304
1305           gtk_widget_show (combo_box->priv->scrolled_window);
1306           
1307           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
1308                              combo_box->priv->scrolled_window);
1309         }
1310
1311       gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1312                          popup);
1313
1314       gtk_widget_show (popup);
1315       g_object_ref (popup);
1316       combo_box->priv->popup_widget = popup;
1317     }
1318 }
1319
1320 static void
1321 gtk_combo_box_menu_position_below (GtkMenu  *menu,
1322                                    gint     *x,
1323                                    gint     *y,
1324                                    gint     *push_in,
1325                                    gpointer  user_data)
1326 {
1327   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1328   gint sx, sy;
1329   GtkWidget *child;
1330   GtkRequisition req;
1331   GdkScreen *screen;
1332   gint monitor_num;
1333   GdkRectangle monitor;
1334   
1335   /* FIXME: is using the size request here broken? */
1336    child = GTK_BIN (combo_box)->child;
1337    
1338    gdk_window_get_origin (child->window, &sx, &sy);
1339    
1340    if (GTK_WIDGET_NO_WINDOW (child))
1341       {
1342         sx += child->allocation.x;
1343         sy += child->allocation.y;
1344       }
1345
1346    gtk_widget_size_request (GTK_WIDGET (menu), &req);
1347
1348    if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1349      *x = sx;
1350    else
1351      *x = sx + child->allocation.width - req.width;
1352    *y = sy;
1353
1354   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1355   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1356                                                   GTK_WIDGET (combo_box)->window);
1357   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1358   
1359   if (*x < monitor.x)
1360     *x = monitor.x;
1361   else if (*x + req.width > monitor.x + monitor.width)
1362     *x = monitor.x + monitor.width - req.width;
1363   
1364   if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1365     *y += child->allocation.height;
1366   else if (*y - monitor.y >= req.height)
1367     *y -= req.height;
1368   else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y) 
1369     *y += child->allocation.height;
1370   else
1371     *y -= req.height;
1372
1373    *push_in = FALSE;
1374 }
1375
1376 static void
1377 gtk_combo_box_menu_position_over (GtkMenu  *menu,
1378                                   gint     *x,
1379                                   gint     *y,
1380                                   gboolean *push_in,
1381                                   gpointer  user_data)
1382 {
1383   GtkComboBox *combo_box;
1384   GtkWidget *active;
1385   GtkWidget *child;
1386   GtkWidget *widget;
1387   GtkRequisition requisition;
1388   GList *children;
1389   gint screen_width;
1390   gint menu_xpos;
1391   gint menu_ypos;
1392   gint menu_width;
1393
1394   g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
1395   
1396   combo_box = GTK_COMBO_BOX (user_data);
1397   widget = GTK_WIDGET (combo_box);
1398
1399   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1400   menu_width = requisition.width;
1401
1402   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1403   gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1404
1405   menu_xpos += widget->allocation.x;
1406   menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1407
1408   if (active != NULL)
1409     {
1410       gtk_widget_get_child_requisition (active, &requisition);
1411       menu_ypos -= requisition.height / 2;
1412     }
1413
1414   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1415   while (children)
1416     {
1417       child = children->data;
1418
1419       if (active == child)
1420         break;
1421
1422       if (GTK_WIDGET_VISIBLE (child))
1423         {
1424           gtk_widget_get_child_requisition (child, &requisition);
1425           menu_ypos -= requisition.height;
1426         }
1427
1428       children = children->next;
1429     }
1430
1431   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1432     menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1433
1434   /* Clamp the position on screen */
1435   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1436   
1437   if (menu_xpos < 0)
1438     menu_xpos = 0;
1439   else if ((menu_xpos + menu_width) > screen_width)
1440     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1441
1442   *x = menu_xpos;
1443   *y = menu_ypos;
1444
1445   *push_in = TRUE;
1446 }
1447
1448 static void
1449 gtk_combo_box_menu_position (GtkMenu  *menu,
1450                              gint     *x,
1451                              gint     *y,
1452                              gint     *push_in,
1453                              gpointer  user_data)
1454 {
1455   GtkComboBox *combo_box;
1456   GtkWidget *menu_item;
1457
1458   combo_box = GTK_COMBO_BOX (user_data);
1459
1460   if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)    
1461     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1462   else
1463     {
1464       /* FIXME handle nested menus better */
1465       menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1466       if (menu_item)
1467         gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget), 
1468                                     menu_item);
1469
1470       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1471     }
1472
1473 }
1474
1475 static void
1476 gtk_combo_box_list_position (GtkComboBox *combo_box, 
1477                              gint        *x, 
1478                              gint        *y, 
1479                              gint        *width,
1480                              gint        *height)
1481 {
1482   GtkWidget *sample;
1483   GdkScreen *screen;
1484   gint monitor_num;
1485   GdkRectangle monitor;
1486   GtkRequisition popup_req;
1487   GtkPolicyType hpolicy, vpolicy;
1488   
1489   sample = GTK_BIN (combo_box)->child;
1490
1491   gdk_window_get_origin (sample->window, x, y);
1492
1493   if (GTK_WIDGET_NO_WINDOW (sample))
1494     {
1495       *x += sample->allocation.x;
1496       *y += sample->allocation.y;
1497     }
1498   
1499   *width = sample->allocation.width;
1500   
1501   if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1502     {
1503        *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1504              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1505        *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1506             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1507     }
1508
1509   hpolicy = vpolicy = GTK_POLICY_NEVER;
1510   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1511                                   hpolicy, vpolicy);
1512   gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1513
1514   if (popup_req.width > *width)
1515     {
1516       hpolicy = GTK_POLICY_ALWAYS;
1517       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1518                                       hpolicy, vpolicy);
1519       gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1520     }
1521
1522   *height = popup_req.height;
1523
1524   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1525   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1526                                                   GTK_WIDGET (combo_box)->window);
1527   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1528
1529   if (*x < monitor.x)
1530     *x = monitor.x;
1531   else if (*x + *width > monitor.x + monitor.width)
1532     *x = monitor.x + monitor.width - *width;
1533   
1534   if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1535     *y += sample->allocation.height;
1536   else if (*y - *height >= monitor.y)
1537     *y -= *height;
1538   else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1539     {
1540       *y += sample->allocation.height;
1541       *height = monitor.y + monitor.height - *y;
1542     }
1543   else 
1544     {
1545       *height = *y - monitor.y;
1546       *y = monitor.y;
1547     }
1548
1549   if (popup_req.height > *height)
1550     {
1551       vpolicy = GTK_POLICY_ALWAYS;
1552       
1553       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1554                                       hpolicy, vpolicy);
1555     }
1556
1557
1558 static gboolean
1559 cell_view_is_sensitive (GtkCellView *cell_view)
1560 {
1561   GList *cells, *list;
1562   gboolean sensitive;
1563   
1564   cells = gtk_cell_view_get_cell_renderers (cell_view);
1565
1566   sensitive = FALSE;
1567   list = cells;
1568   while (list)
1569     {
1570       g_object_get (list->data, "sensitive", &sensitive, NULL);
1571       
1572       if (sensitive)
1573         break;
1574
1575       list = list->next;
1576     }
1577   g_list_free (cells);
1578
1579   return sensitive;
1580 }
1581
1582 static gboolean
1583 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1584                               GtkTreeIter *iter)
1585 {
1586   GList *cells, *list;
1587   gboolean sensitive;
1588
1589   if (!combo_box->priv->column)
1590     return TRUE;
1591
1592   if (combo_box->priv->row_separator_func)
1593     {
1594       if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
1595                                                   combo_box->priv->row_separator_data))
1596         return FALSE;
1597     }
1598
1599   gtk_tree_view_column_cell_set_cell_data (combo_box->priv->column,
1600                                            combo_box->priv->model,
1601                                            iter, FALSE, FALSE);
1602
1603   cells = gtk_tree_view_column_get_cell_renderers (combo_box->priv->column);
1604
1605   sensitive = FALSE;
1606   list = cells;
1607   while (list)
1608     {
1609       g_object_get (list->data, "sensitive", &sensitive, NULL);
1610       
1611       if (sensitive)
1612         break;
1613
1614       list = list->next;
1615     }
1616   g_list_free (cells);
1617
1618   return sensitive;
1619 }
1620
1621 static void
1622 update_menu_sensitivity (GtkComboBox *combo_box,
1623                          GtkWidget   *menu)
1624 {
1625   GList *children, *child;
1626   GtkWidget *item, *submenu, *separator;
1627   GtkWidget *cell_view;
1628   gboolean sensitive;
1629
1630   if (!combo_box->priv->model)
1631     return;
1632
1633   children = gtk_container_get_children (GTK_CONTAINER (menu));
1634
1635   for (child = children; child; child = child->next)
1636     {
1637       item = GTK_WIDGET (child->data);
1638       cell_view = GTK_BIN (item)->child;
1639
1640       if (!GTK_IS_CELL_VIEW (cell_view))
1641         continue;
1642       
1643       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1644       if (submenu != NULL)
1645         {
1646           gtk_widget_set_sensitive (item, TRUE);
1647           update_menu_sensitivity (combo_box, submenu);
1648         }
1649       else
1650         {
1651           sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1652
1653           if (menu != combo_box->priv->popup_widget && child == children)
1654             {
1655               separator = GTK_WIDGET (child->next->data);
1656               g_object_set (item, "visible", sensitive, NULL);
1657               g_object_set (separator, "visible", sensitive, NULL);
1658             }
1659           else
1660             gtk_widget_set_sensitive (item, sensitive);
1661         }
1662     }
1663
1664   g_list_free (children);
1665 }
1666
1667 static void 
1668 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1669                           guint        button, 
1670                           guint32      activate_time)
1671 {
1672   GtkTreePath *path;
1673   gint active_item;
1674   GtkRequisition requisition;
1675   gint width;
1676   
1677   update_menu_sensitivity (combo_box, combo_box->priv->popup_widget);
1678
1679   active_item = -1;
1680   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1681     {
1682       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1683       active_item = gtk_tree_path_get_indices (path)[0];
1684       gtk_tree_path_free (path);
1685       
1686       if (combo_box->priv->add_tearoffs)
1687         active_item++;
1688     }
1689
1690   /* FIXME handle nested menus better */
1691   gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), active_item);
1692   
1693   if (combo_box->priv->wrap_width == 0)
1694     {
1695       width = GTK_WIDGET (combo_box)->allocation.width;
1696       gtk_widget_set_size_request (combo_box->priv->popup_widget, -1, -1);
1697       gtk_widget_size_request (combo_box->priv->popup_widget, &requisition);
1698       
1699       gtk_widget_set_size_request (combo_box->priv->popup_widget,
1700                                    MAX (width, requisition.width), -1);
1701     }
1702   
1703   gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1704                   NULL, NULL,
1705                   gtk_combo_box_menu_position, combo_box,
1706                   button, activate_time);
1707 }
1708
1709 static gboolean
1710 popup_grab_on_window (GdkWindow *window,
1711                       guint32    activate_time,
1712                       gboolean   grab_keyboard)
1713 {
1714   if ((gdk_pointer_grab (window, TRUE,
1715                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1716                          GDK_POINTER_MOTION_MASK,
1717                          NULL, NULL, activate_time) == 0))
1718     {
1719       if (!grab_keyboard ||
1720           gdk_keyboard_grab (window, TRUE,
1721                              activate_time) == 0)
1722         return TRUE;
1723       else
1724         {
1725           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1726                                       activate_time);
1727           return FALSE;
1728         }
1729     }
1730
1731   return FALSE;
1732 }
1733
1734 /**
1735  * gtk_combo_box_popup:
1736  * @combo_box: a #GtkComboBox
1737  * 
1738  * Pops up the menu or dropdown list of @combo_box. 
1739  *
1740  * This function is mostly intended for use by accessibility technologies;
1741  * applications should have little use for it.
1742  *
1743  * Since: 2.4
1744  **/
1745 void
1746 gtk_combo_box_popup (GtkComboBox *combo_box)
1747 {
1748   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1749
1750   g_signal_emit (combo_box, combo_box_signals[POPUP], 0);
1751 }
1752
1753 static void
1754 gtk_combo_box_real_popup (GtkComboBox *combo_box)
1755 {
1756   gint x, y, width, height;
1757   GtkTreePath *path, *ppath;
1758   GtkWidget *toplevel;
1759
1760   if (!GTK_WIDGET_REALIZED (combo_box))
1761     return;
1762
1763   if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1764     return;
1765
1766   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1767     {
1768       gtk_combo_box_menu_popup (combo_box, 0, 0);
1769       return;
1770     }
1771
1772   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1773   if (GTK_IS_WINDOW (toplevel))
1774     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
1775                                  GTK_WINDOW (combo_box->priv->popup_window));
1776
1777   gtk_widget_show_all (combo_box->priv->popup_frame);
1778   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1779   
1780   gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);  
1781   gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1782
1783   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1784     {
1785       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1786       ppath = gtk_tree_path_copy (path);
1787       if (gtk_tree_path_up (ppath))
1788         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (combo_box->priv->tree_view),
1789                                       ppath);
1790       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1791                                 path, NULL, FALSE);
1792       gtk_tree_path_free (path);
1793       gtk_tree_path_free (ppath);
1794     }
1795   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
1796                                   TRUE);
1797   
1798   /* popup */
1799   gtk_widget_show (combo_box->priv->popup_window);
1800
1801   gtk_widget_grab_focus (combo_box->priv->popup_window);
1802   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1803                                 TRUE);
1804
1805   if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1806     gtk_widget_grab_focus (combo_box->priv->tree_view);
1807
1808   if (!popup_grab_on_window (combo_box->priv->popup_window->window,
1809                              GDK_CURRENT_TIME, TRUE))
1810     {
1811       gtk_widget_hide (combo_box->priv->popup_window);
1812       return;
1813     }
1814
1815   gtk_grab_add (combo_box->priv->popup_window);
1816 }
1817
1818 /**
1819  * gtk_combo_box_popdown:
1820  * @combo_box: a #GtkComboBox
1821  * 
1822  * Hides the menu or dropdown list of @combo_box.
1823  *
1824  * This function is mostly intended for use by accessibility technologies;
1825  * applications should have little use for it.
1826  *
1827  * Since: 2.4
1828  **/
1829 void
1830 gtk_combo_box_popdown (GtkComboBox *combo_box)
1831 {
1832   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1833
1834   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1835     {
1836       gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1837       return;
1838     }
1839
1840   if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1841     return;
1842
1843   gtk_grab_remove (combo_box->priv->popup_window);
1844   gtk_widget_hide_all (combo_box->priv->popup_window);
1845   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1846                                 FALSE);
1847 }
1848
1849 static gint
1850 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1851                                     GtkTreePath *path)
1852 {
1853   gint padding;
1854   GtkRequisition req;
1855
1856   if (combo_box->priv->cell_view)
1857     gtk_widget_style_get (combo_box->priv->cell_view,
1858                           "focus-line-width", &padding,
1859                           NULL);
1860   else
1861     padding = 0;
1862
1863   /* add some pixels for good measure */
1864   padding += BONUS_PADDING;
1865
1866   if (combo_box->priv->cell_view)
1867     gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1868                                    path, &req);
1869   else
1870     req.width = 0;
1871
1872   return req.width + padding;
1873 }
1874
1875 static void
1876 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1877 {
1878   GtkTreeIter iter;
1879   GtkTreePath *path;
1880
1881   if (!combo_box->priv->model ||
1882       !gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1883     return;
1884
1885   combo_box->priv->width = 0;
1886   combo_box->priv->height = 0;
1887
1888   path = gtk_tree_path_new_from_indices (0, -1);
1889
1890   do
1891     {
1892       GtkRequisition req;
1893
1894       if (combo_box->priv->cell_view)
1895         gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
1896                                        path, &req);
1897       else
1898         {
1899           req.width = 0;
1900           req.height = 0;
1901         }
1902
1903       combo_box->priv->width = MAX (combo_box->priv->width, req.width);
1904       combo_box->priv->height = MAX (combo_box->priv->height, req.height);
1905
1906       gtk_tree_path_next (path);
1907     }
1908   while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1909
1910   gtk_tree_path_free (path);
1911 }
1912
1913 static void
1914 gtk_combo_box_size_request (GtkWidget      *widget,
1915                             GtkRequisition *requisition)
1916 {
1917   gint width, height;
1918   gint focus_width, focus_pad;
1919   gint font_size;
1920   gint arrow_size;
1921   GtkRequisition bin_req;
1922   PangoContext *context;
1923   PangoFontMetrics *metrics;
1924   PangoFontDescription *font_desc;
1925
1926   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1927  
1928   /* common */
1929   gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1930   gtk_combo_box_remeasure (combo_box);
1931   bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1932   bin_req.height = MAX (bin_req.height, combo_box->priv->height);
1933
1934   gtk_widget_style_get (GTK_WIDGET (widget),
1935                         "focus-line-width", &focus_width,
1936                         "focus-padding", &focus_pad,
1937                         "arrow-size", &arrow_size,
1938                         NULL);
1939
1940   font_desc = GTK_BIN (widget)->child->style->font_desc;
1941   context = gtk_widget_get_pango_context (widget);
1942   metrics = pango_context_get_metrics (context, font_desc,
1943                                        pango_context_get_language (context));
1944   font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
1945                             pango_font_metrics_get_descent (metrics));
1946   pango_font_metrics_unref (metrics);
1947
1948   arrow_size = MAX (arrow_size, font_size);
1949
1950   gtk_widget_set_size_request (combo_box->priv->arrow, arrow_size, arrow_size);
1951
1952   if (!combo_box->priv->tree_view)
1953     {
1954       /* menu mode */
1955
1956       if (combo_box->priv->cell_view)
1957         {
1958           GtkRequisition button_req, sep_req, arrow_req;
1959           gint border_width, xthickness, ythickness;
1960
1961           gtk_widget_size_request (combo_box->priv->button, &button_req);
1962           border_width = GTK_CONTAINER (combo_box)->border_width;
1963           xthickness = combo_box->priv->button->style->xthickness;
1964           ythickness = combo_box->priv->button->style->ythickness;
1965
1966           bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1967           bin_req.height = MAX (bin_req.height, combo_box->priv->height);
1968
1969           gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1970           gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1971
1972           height = MAX (sep_req.height, arrow_req.height);
1973           height = MAX (height, bin_req.height);
1974
1975           width = bin_req.width + sep_req.width + arrow_req.width;
1976
1977           height += 2*(border_width + ythickness + focus_width + focus_pad);
1978           width  += 2*(border_width + xthickness + focus_width + focus_pad);
1979
1980           requisition->width = width;
1981           requisition->height = height;
1982         }
1983       else
1984         {
1985           GtkRequisition but_req;
1986
1987           gtk_widget_size_request (combo_box->priv->button, &but_req);
1988
1989           requisition->width = bin_req.width + but_req.width;
1990           requisition->height = MAX (bin_req.height, but_req.height);
1991         }
1992     }
1993   else
1994     {
1995       /* list mode */
1996       GtkRequisition button_req, frame_req;
1997
1998       /* sample + frame */
1999       *requisition = bin_req;
2000
2001       requisition->width += 2 * focus_width;
2002       
2003       if (combo_box->priv->cell_view_frame)
2004         {
2005           gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
2006           if (combo_box->priv->has_frame)
2007             {
2008               requisition->width += 2 *
2009                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2010                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2011               requisition->height += 2 *
2012                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2013                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2014             }
2015         }
2016
2017       /* the button */
2018       gtk_widget_size_request (combo_box->priv->button, &button_req);
2019
2020       requisition->height = MAX (requisition->height, button_req.height);
2021       requisition->width += button_req.width;
2022     }
2023 }
2024
2025 static void
2026 gtk_combo_box_size_allocate (GtkWidget     *widget,
2027                              GtkAllocation *allocation)
2028 {
2029   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2030   gint focus_width, focus_pad;
2031   GtkAllocation child;
2032   GtkRequisition req;
2033   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2034
2035   widget->allocation = *allocation;
2036
2037   gtk_widget_style_get (GTK_WIDGET (widget),
2038                         "focus-line-width", &focus_width,
2039                         "focus-padding", &focus_pad,
2040                         NULL);
2041
2042   if (!combo_box->priv->tree_view)
2043     {
2044       if (combo_box->priv->cell_view)
2045         {
2046           gint border_width, xthickness, ythickness;
2047           gint width;
2048
2049           /* menu mode */
2050           gtk_widget_size_allocate (combo_box->priv->button, allocation);
2051
2052           /* set some things ready */
2053           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
2054           xthickness = combo_box->priv->button->style->xthickness;
2055           ythickness = combo_box->priv->button->style->ythickness;
2056
2057           child.x = allocation->x;
2058           child.y = allocation->y;
2059           width = allocation->width;
2060           child.height = allocation->height;
2061
2062           if (!combo_box->priv->is_cell_renderer)
2063             {
2064               child.x += border_width + xthickness + focus_width + focus_pad;
2065               child.y += border_width + ythickness + focus_width + focus_pad;
2066               width -= 2 * (child.x - allocation->x);
2067               child.height -= 2 * (child.y - allocation->y);
2068             }
2069
2070
2071           /* handle the children */
2072           gtk_widget_size_request (combo_box->priv->arrow, &req);
2073           child.width = req.width;
2074           if (!is_rtl)
2075             child.x += width - req.width;
2076           child.width = MAX (1, child.width);
2077           child.height = MAX (1, child.height);
2078           gtk_widget_size_allocate (combo_box->priv->arrow, &child);
2079           if (is_rtl)
2080             child.x += req.width;
2081           gtk_widget_size_request (combo_box->priv->separator, &req);
2082           child.width = req.width;
2083           if (!is_rtl)
2084             child.x -= req.width;
2085           child.width = MAX (1, child.width);
2086           child.height = MAX (1, child.height);
2087           gtk_widget_size_allocate (combo_box->priv->separator, &child);
2088
2089           if (is_rtl)
2090             {
2091               child.x += req.width;
2092               child.width = allocation->x + allocation->width 
2093                 - (border_width + xthickness + focus_width + focus_pad) 
2094                 - child.x;
2095             }
2096           else 
2097             {
2098               child.width = child.x;
2099               child.x = allocation->x 
2100                 + border_width + xthickness + focus_width + focus_pad;
2101               child.width -= child.x;
2102             }
2103
2104           child.width = MAX (1, child.width);
2105           child.height = MAX (1, child.height);
2106           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2107         }
2108       else
2109         {
2110           gtk_widget_size_request (combo_box->priv->button, &req);
2111           if (is_rtl)
2112             child.x = allocation->x;
2113           else
2114             child.x = allocation->x + allocation->width - req.width;
2115           child.y = allocation->y;
2116           child.width = req.width;
2117           child.height = allocation->height;
2118           child.width = MAX (1, child.width);
2119           child.height = MAX (1, child.height);
2120           gtk_widget_size_allocate (combo_box->priv->button, &child);
2121
2122           if (is_rtl)
2123             child.x = allocation->x + req.width;
2124           else
2125             child.x = allocation->x;
2126           child.y = allocation->y;
2127           child.width = allocation->width - req.width;
2128           child.width = MAX (1, child.width);
2129           child.height = MAX (1, child.height);
2130           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2131         }
2132     }
2133   else
2134     {
2135       /* list mode */
2136
2137       /* button */
2138       gtk_widget_size_request (combo_box->priv->button, &req);
2139       if (is_rtl)
2140         child.x = allocation->x;
2141       else
2142         child.x = allocation->x + allocation->width - req.width;
2143       child.y = allocation->y;
2144       child.width = req.width;
2145       child.height = allocation->height;
2146       child.width = MAX (1, child.width);
2147       child.height = MAX (1, child.height);
2148       gtk_widget_size_allocate (combo_box->priv->button, &child);
2149
2150       /* frame */
2151       if (is_rtl)
2152         child.x = allocation->x + req.width;
2153       else
2154         child.x = allocation->x;
2155       child.y = allocation->y;
2156       child.width = allocation->width - req.width;
2157       child.height = allocation->height;
2158
2159       if (combo_box->priv->cell_view_frame)
2160         {
2161           child.width = MAX (1, child.width);
2162           child.height = MAX (1, child.height);
2163           gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
2164
2165           /* the sample */
2166           if (combo_box->priv->has_frame)
2167             {
2168               child.x +=
2169                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2170                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
2171               child.y +=
2172                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2173                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
2174               child.width -= 2 * (
2175                                   GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2176                                   GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2177               child.height -= 2 * (
2178                                    GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2179                                    GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2180             }
2181         }
2182       
2183       child.width = MAX (1, child.width);
2184       child.height = MAX (1, child.height);
2185       gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2186     }
2187 }
2188
2189 static void
2190 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2191 {
2192   if (combo_box->priv->model)
2193     {
2194       g_signal_handler_disconnect (combo_box->priv->model,
2195                                    combo_box->priv->inserted_id);
2196       g_signal_handler_disconnect (combo_box->priv->model,
2197                                    combo_box->priv->deleted_id);
2198       g_signal_handler_disconnect (combo_box->priv->model,
2199                                    combo_box->priv->reordered_id);
2200       g_signal_handler_disconnect (combo_box->priv->model,
2201                                    combo_box->priv->changed_id);
2202     }
2203
2204   /* menu mode */
2205   if (!combo_box->priv->tree_view)
2206     {
2207       if (combo_box->priv->popup_widget)
2208         gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
2209                                (GtkCallback)gtk_widget_destroy, NULL);
2210     }
2211
2212   if (combo_box->priv->model)
2213     {
2214       g_object_unref (combo_box->priv->model);
2215       combo_box->priv->model = NULL;
2216     }
2217
2218   if (combo_box->priv->active_row)
2219     {
2220       gtk_tree_row_reference_free (combo_box->priv->active_row);
2221       combo_box->priv->active_row = NULL;
2222     }
2223
2224   if (combo_box->priv->cell_view)
2225     gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2226 }
2227
2228 static void
2229 gtk_combo_box_forall (GtkContainer *container,
2230                       gboolean      include_internals,
2231                       GtkCallback   callback,
2232                       gpointer      callback_data)
2233 {
2234   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2235
2236   if (include_internals)
2237     {
2238       if (combo_box->priv->button)
2239         (* callback) (combo_box->priv->button, callback_data);
2240       if (combo_box->priv->cell_view_frame)
2241         (* callback) (combo_box->priv->cell_view_frame, callback_data);
2242     }
2243
2244   if (GTK_BIN (container)->child)
2245     (* callback) (GTK_BIN (container)->child, callback_data);
2246 }
2247
2248 static void 
2249 gtk_combo_box_child_show (GtkWidget *widget,
2250                           GtkComboBox *combo_box)
2251 {
2252   GtkComboBoxPrivate *priv = combo_box->priv;
2253
2254   priv->popup_shown = TRUE;
2255   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2256 }
2257
2258 static void 
2259 gtk_combo_box_child_hide (GtkWidget *widget,
2260                           GtkComboBox *combo_box)
2261 {
2262   GtkComboBoxPrivate *priv = combo_box->priv;
2263
2264   priv->popup_shown = FALSE;
2265   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2266 }
2267
2268 static gboolean
2269 gtk_combo_box_expose_event (GtkWidget      *widget,
2270                             GdkEventExpose *event)
2271 {
2272   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2273
2274   if (!combo_box->priv->tree_view)
2275     {
2276       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2277                                       combo_box->priv->button, event);
2278     }
2279   else
2280     {
2281       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2282                                       combo_box->priv->button, event);
2283
2284       if (combo_box->priv->cell_view_frame)
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           else
2751             g_signal_connect (item, "activate",
2752                               G_CALLBACK (gtk_combo_box_menu_item_activate),
2753                               combo_box);
2754         }
2755       
2756       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2757       if (combo_box->priv->wrap_width && menu == combo_box->priv->popup_widget)
2758         gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2759       gtk_widget_show (item);
2760       
2761       last = item;
2762     }
2763 }
2764
2765 static void
2766 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2767 {
2768   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2769                                         G_SIGNAL_MATCH_DATA,
2770                                         0, 0, NULL,
2771                                         gtk_combo_box_menu_button_press, NULL);
2772   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2773                                         G_SIGNAL_MATCH_DATA,
2774                                         0, 0, NULL,
2775                                         gtk_combo_box_button_state_changed, combo_box);
2776
2777   /* unparent will remove our latest ref */
2778   gtk_widget_unparent (combo_box->priv->button);
2779   
2780   combo_box->priv->box = NULL;
2781   combo_box->priv->button = NULL;
2782   combo_box->priv->arrow = NULL;
2783   combo_box->priv->separator = NULL;
2784
2785   g_object_unref (combo_box->priv->column);
2786   combo_box->priv->column = NULL;
2787
2788   /* changing the popup window will unref the menu and the children */
2789 }
2790
2791 /*
2792  * grid
2793  */
2794
2795 static gboolean
2796 menu_occupied (GtkMenu   *menu,
2797                guint      left_attach,
2798                guint      right_attach,
2799                guint      top_attach,
2800                guint      bottom_attach)
2801 {
2802   GList *i;
2803
2804   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2805     {
2806       guint l, r, b, t;
2807
2808       gtk_container_child_get (GTK_CONTAINER (menu), 
2809                                i->data,
2810                                "left-attach", &l,
2811                                "right-attach", &r,
2812                                "bottom-attach", &b,
2813                                "top-attach", &t,
2814                                NULL);
2815
2816       /* look if this item intersects with the given coordinates */
2817       if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2818         return TRUE;
2819     }
2820
2821   return FALSE;
2822 }
2823
2824 static void
2825 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2826                              GtkWidget   *item,
2827                              GtkTreeIter *iter,
2828                              GtkWidget   *last)
2829 {
2830   gint current_col = 0, current_row = 0;
2831   gint rows = 1, cols = 1;
2832   GtkWidget *menu = combo_box->priv->popup_widget;
2833
2834   if (!GTK_IS_MENU_SHELL (menu))
2835     return;
2836   
2837   if (combo_box->priv->col_column == -1 &&
2838       combo_box->priv->row_column == -1 &&
2839       last)
2840     {
2841       gtk_container_child_get (GTK_CONTAINER (menu), 
2842                                last,
2843                                "right-attach", &current_col,
2844                                "top-attach", &current_row,
2845                                NULL);
2846       if (current_col + cols > combo_box->priv->wrap_width)
2847         {
2848           current_col = 0;
2849           current_row++;
2850         }
2851     }
2852   else
2853     {
2854       if (combo_box->priv->col_column != -1)
2855         gtk_tree_model_get (combo_box->priv->model, iter,
2856                             combo_box->priv->col_column, &cols,
2857                             -1);
2858       if (combo_box->priv->row_column != -1)
2859         gtk_tree_model_get (combo_box->priv->model, iter,
2860                             combo_box->priv->row_column, &rows,
2861                             -1);
2862
2863       while (1)
2864         {
2865           if (current_col + cols > combo_box->priv->wrap_width)
2866             {
2867               current_col = 0;
2868               current_row++;
2869             }
2870           
2871           if (!menu_occupied (GTK_MENU (menu), 
2872                               current_col, current_col + cols,
2873                               current_row, current_row + rows))
2874             break;
2875           
2876           current_col++;
2877         }
2878     }
2879
2880   /* set attach props */
2881   gtk_menu_attach (GTK_MENU (menu), item,
2882                    current_col, current_col + cols,
2883                    current_row, current_row + rows);
2884 }
2885
2886 static void
2887 gtk_combo_box_relayout (GtkComboBox *combo_box)
2888 {
2889   GList *list, *j;
2890   GtkWidget *menu;
2891
2892   menu = combo_box->priv->popup_widget;
2893   
2894   /* do nothing unless we are in menu style and realized */
2895   if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
2896     return;
2897   
2898   list = gtk_container_get_children (GTK_CONTAINER (menu));
2899   
2900   for (j = g_list_last (list); j; j = j->prev)
2901     gtk_container_remove (GTK_CONTAINER (menu), j->data);
2902   
2903   gtk_combo_box_menu_fill (combo_box);
2904
2905   g_list_free (list);
2906 }
2907
2908 /* callbacks */
2909 static gboolean
2910 gtk_combo_box_menu_button_press (GtkWidget      *widget,
2911                                  GdkEventButton *event,
2912                                  gpointer        user_data)
2913 {
2914   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2915
2916   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
2917       event->type == GDK_BUTTON_PRESS && event->button == 1)
2918     {
2919       if (combo_box->priv->focus_on_click && 
2920           !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
2921         gtk_widget_grab_focus (combo_box->priv->button);
2922
2923       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
2924
2925       return TRUE;
2926     }
2927
2928   return FALSE;
2929 }
2930
2931 static void
2932 gtk_combo_box_menu_item_activate (GtkWidget *item,
2933                                   gpointer   user_data)
2934 {
2935   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2936   GtkWidget *cell_view;
2937   GtkTreePath *path;
2938   GtkTreeIter iter;
2939
2940   cell_view = GTK_BIN (item)->child;
2941
2942   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
2943
2944   path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
2945
2946   if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
2947     gtk_combo_box_set_active_iter (combo_box, &iter);
2948
2949   gtk_tree_path_free (path);
2950
2951   combo_box->priv->editing_canceled = FALSE;
2952 }
2953
2954 static void
2955 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
2956                                   GtkTreePath      *path,
2957                                   GtkTreeIter      *iter,
2958                                   gpointer          user_data)
2959 {
2960   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2961
2962   if (combo_box->priv->tree_view)
2963     gtk_combo_box_list_popup_resize (combo_box);
2964   else
2965     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
2966 }
2967
2968 static void
2969 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
2970                                  GtkTreePath      *path,
2971                                  gpointer          user_data)
2972 {
2973   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2974
2975   if (combo_box->priv->cell_view)
2976     {
2977       if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
2978         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2979     }
2980   
2981   if (combo_box->priv->tree_view)
2982     gtk_combo_box_list_popup_resize (combo_box);
2983   else
2984     gtk_combo_box_menu_row_deleted (model, path, user_data);  
2985 }
2986
2987 static void
2988 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
2989                                     GtkTreePath     *path,
2990                                     GtkTreeIter     *iter,
2991                                     gint            *new_order,
2992                                     gpointer         user_data)
2993 {
2994   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2995
2996   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
2997
2998   if (!combo_box->priv->tree_view)
2999     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3000 }
3001                                                     
3002 static void
3003 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
3004                                  GtkTreePath      *path,
3005                                  GtkTreeIter      *iter,
3006                                  gpointer          user_data)
3007 {
3008   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3009   GtkTreePath *active_path;
3010
3011   /* FIXME this belongs to GtkCellView */
3012   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3013     {
3014       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3015       if (gtk_tree_path_compare (path, active_path) == 0 &&
3016           combo_box->priv->cell_view)
3017         gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
3018       gtk_tree_path_free (active_path);
3019     }
3020       
3021   if (combo_box->priv->tree_view)
3022     gtk_combo_box_list_row_changed (model, path, iter, user_data);
3023   else
3024     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3025 }
3026
3027 static gboolean
3028 list_popup_resize_idle (gpointer user_data)
3029 {
3030   GtkComboBox *combo_box;
3031   gint x, y, width, height;
3032
3033   GDK_THREADS_ENTER ();
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   GDK_THREADS_LEAVE ();
3049
3050   return FALSE;
3051 }
3052
3053 static void
3054 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3055 {
3056   if (!combo_box->priv->resize_idle_id)
3057     combo_box->priv->resize_idle_id = 
3058       g_idle_add (list_popup_resize_idle, combo_box);
3059 }
3060
3061 static void
3062 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
3063                                   GtkTreePath      *path,
3064                                   GtkTreeIter      *iter,
3065                                   gpointer          user_data)
3066 {
3067   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3068   
3069   gtk_combo_box_list_popup_resize (combo_box);
3070 }
3071
3072
3073 static GtkWidget *
3074 find_menu_by_path (GtkWidget   *menu,
3075                    GtkTreePath *path,
3076                    gboolean     skip_first)
3077 {
3078   GList *i, *list;
3079   GtkWidget *item;
3080   GtkWidget *submenu;    
3081   GtkTreeRowReference *mref;
3082   GtkTreePath *mpath;
3083   gboolean skip;
3084
3085   list = gtk_container_get_children (GTK_CONTAINER (menu));
3086   skip = skip_first;
3087   item = NULL;
3088   for (i = list; i; i = i->next)
3089     {
3090       if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3091         {
3092           mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3093           if (!mref)
3094             continue;
3095           else if (!gtk_tree_row_reference_valid (mref))
3096             mpath = NULL;
3097           else
3098             mpath = gtk_tree_row_reference_get_path (mref);
3099         }
3100       else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3101         {
3102           if (skip)
3103             {
3104               skip = FALSE;
3105               continue;
3106             }
3107
3108           mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3109         }
3110       else 
3111         continue;
3112
3113       /* this case is necessary, since the row reference of
3114        * the cell view may already be updated after a deletion
3115        */
3116       if (!mpath)
3117         {
3118           item = i->data;
3119           break;
3120         }
3121       if (gtk_tree_path_compare (mpath, path) == 0)
3122         {
3123           gtk_tree_path_free (mpath);
3124           item = i->data;
3125           break;
3126         }
3127       if (gtk_tree_path_is_ancestor (mpath, path))
3128         {
3129           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3130           if (submenu != NULL)
3131             {
3132               gtk_tree_path_free (mpath);
3133               item = find_menu_by_path (submenu, path, TRUE);
3134               break;
3135             }
3136         }
3137       gtk_tree_path_free (mpath);
3138     }
3139   
3140   g_list_free (list);  
3141
3142   return item;
3143 }
3144
3145 #if 0
3146 static void
3147 dump_menu_tree (GtkWidget   *menu, 
3148                 gint         level)
3149 {
3150   GList *i, *list;
3151   GtkWidget *submenu;    
3152   GtkTreePath *path;
3153
3154   list = gtk_container_get_children (GTK_CONTAINER (menu));
3155   for (i = list; i; i = i->next)
3156     {
3157       if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3158         {
3159           path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3160           g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3161           gtk_tree_path_free (path);
3162
3163           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3164           if (submenu != NULL)
3165             dump_menu_tree (submenu, level + 1);
3166         }
3167     }
3168   
3169   g_list_free (list);  
3170 }
3171 #endif
3172
3173 static void
3174 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3175                                  GtkTreePath  *path,
3176                                  GtkTreeIter  *iter,
3177                                  gpointer      user_data)
3178 {
3179   GtkWidget *parent;
3180   GtkWidget *item, *menu, *separator;
3181   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3182   GtkTreePath *ppath;
3183   GtkTreeIter piter;
3184   gint depth, pos;
3185   gboolean is_separator;
3186
3187   if (!combo_box->priv->popup_widget)
3188     return;
3189
3190   depth = gtk_tree_path_get_depth (path);
3191   pos = gtk_tree_path_get_indices (path)[depth - 1];
3192   if (depth > 1)
3193     {
3194       ppath = gtk_tree_path_copy (path);
3195       gtk_tree_path_up (ppath);
3196       parent = find_menu_by_path (combo_box->priv->popup_widget, ppath, FALSE);
3197       gtk_tree_path_free (ppath);
3198
3199       menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3200       if (!menu)
3201         {
3202           menu = gtk_menu_new ();
3203           gtk_widget_show (menu);
3204           gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3205           
3206           /* Ugly - since menus can only activate leaves, we have to
3207            * duplicate the item inside the submenu.
3208            */
3209           gtk_tree_model_iter_parent (model, &piter, iter);
3210           item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3211           separator = gtk_separator_menu_item_new ();
3212           g_signal_connect (item, "activate",
3213                             G_CALLBACK (gtk_combo_box_menu_item_activate),
3214                             combo_box);
3215           gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3216           gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3217           if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3218             {
3219               gtk_widget_show (item);
3220               gtk_widget_show (separator);
3221             }
3222         }
3223       pos += 2;
3224     }
3225   else
3226     {
3227       menu = combo_box->priv->popup_widget;
3228       if (combo_box->priv->add_tearoffs)
3229         pos += 1;
3230     }
3231   
3232   if (combo_box->priv->row_separator_func)
3233     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3234                                                            combo_box->priv->row_separator_data);
3235   else
3236     is_separator = FALSE;
3237
3238   if (is_separator)
3239     {
3240       item = gtk_separator_menu_item_new ();
3241       g_object_set_data_full (G_OBJECT (item),
3242                               I_("gtk-combo-box-item-path"),
3243                               gtk_tree_row_reference_new (model, path),
3244                               (GDestroyNotify)gtk_tree_row_reference_free);
3245     }
3246   else
3247     {
3248       item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3249       
3250       g_signal_connect (item, "activate",
3251                         G_CALLBACK (gtk_combo_box_menu_item_activate),
3252                         combo_box);
3253     }
3254
3255   gtk_widget_show (item);
3256   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3257 }
3258
3259 static void
3260 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3261                                 GtkTreePath  *path,
3262                                 gpointer      user_data)
3263 {
3264   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3265   GtkWidget *menu;
3266   GtkWidget *item;
3267
3268   if (!combo_box->priv->popup_widget)
3269     return;
3270
3271   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3272   menu = gtk_widget_get_parent (item);
3273   gtk_container_remove (GTK_CONTAINER (menu), item);
3274 }
3275
3276 static void
3277 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
3278                                     GtkTreePath      *path,
3279                                     GtkTreeIter      *iter,
3280                                     gint             *new_order,
3281                                     gpointer          user_data)
3282 {
3283   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3284
3285   gtk_combo_box_relayout (combo_box);
3286 }
3287                                     
3288 static void
3289 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3290                                 GtkTreePath  *path,
3291                                 GtkTreeIter  *iter,
3292                                 gpointer      user_data)
3293 {
3294   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3295   GtkWidget *item;
3296   gint width;
3297   gboolean is_separator;
3298
3299   if (!combo_box->priv->popup_widget)
3300     return;
3301
3302   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3303
3304   if (combo_box->priv->row_separator_func)
3305     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3306                                                            combo_box->priv->row_separator_data);
3307   else
3308     is_separator = FALSE;
3309
3310   if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3311     {
3312       gtk_combo_box_menu_row_deleted (model, path, combo_box);
3313       gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3314     }
3315
3316   if (combo_box->priv->wrap_width
3317       && item->parent == combo_box->priv->popup_widget)
3318     {
3319       GtkWidget *pitem = NULL;
3320       GtkTreePath *prev;
3321
3322       prev = gtk_tree_path_copy (path);
3323
3324       if (gtk_tree_path_prev (prev))
3325         pitem = find_menu_by_path (combo_box->priv->popup_widget, prev, FALSE);
3326
3327       gtk_tree_path_free (prev);
3328
3329       /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3330          move it */
3331       gtk_container_child_set (GTK_CONTAINER (combo_box->priv->popup_widget),
3332                                item, 
3333                                "left-attach", -1, 
3334                                "right-attach", -1,
3335                                "top-attach", -1, 
3336                                "bottom-attach", -1, 
3337                                NULL);
3338
3339       gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3340     }
3341
3342   width = gtk_combo_box_calc_requested_width (combo_box, path);
3343
3344   if (width > combo_box->priv->width)
3345     {
3346       if (combo_box->priv->cell_view)
3347         {
3348           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3349           gtk_widget_queue_resize (combo_box->priv->cell_view);
3350         }
3351       combo_box->priv->width = width;
3352     }
3353 }
3354
3355 /*
3356  * list style
3357  */
3358
3359 static void
3360 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3361 {
3362   GtkTreeSelection *sel;
3363
3364   combo_box->priv->button = gtk_toggle_button_new ();
3365   gtk_widget_set_parent (combo_box->priv->button,
3366                          GTK_BIN (combo_box)->child->parent);
3367   g_signal_connect (combo_box->priv->button, "button_press_event",
3368                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3369   g_signal_connect (combo_box->priv->button, "toggled",
3370                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3371
3372   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3373   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
3374                      combo_box->priv->arrow);
3375   combo_box->priv->separator = NULL;
3376   gtk_widget_show_all (combo_box->priv->button);
3377
3378   if (combo_box->priv->cell_view)
3379     {
3380       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
3381                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3382
3383       combo_box->priv->box = gtk_event_box_new ();
3384       gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->box), 
3385                                         FALSE);
3386
3387       if (combo_box->priv->has_frame)
3388         {
3389           combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
3390           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
3391                                      GTK_SHADOW_IN);
3392         }
3393       else 
3394         {
3395           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3396           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), 
3397                                             FALSE);
3398         }
3399       
3400       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
3401                              GTK_BIN (combo_box)->child->parent);
3402       gtk_container_add (GTK_CONTAINER (combo_box->priv->cell_view_frame),
3403                          combo_box->priv->box);
3404       gtk_widget_show_all (combo_box->priv->cell_view_frame);
3405
3406       g_signal_connect (combo_box->priv->box, "button_press_event",
3407                         G_CALLBACK (gtk_combo_box_list_button_pressed), 
3408                         combo_box);
3409     }
3410
3411   combo_box->priv->tree_view = gtk_tree_view_new ();
3412   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3413   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3414   gtk_tree_selection_set_select_function (sel,
3415                                           gtk_combo_box_list_select_func,
3416                                           NULL, NULL);
3417   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
3418                                      FALSE);
3419   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (combo_box->priv->tree_view),
3420                                      TRUE);
3421   if (combo_box->priv->row_separator_func)
3422     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3423                                           combo_box->priv->row_separator_func, 
3424                                           combo_box->priv->row_separator_data, 
3425                                           NULL);
3426   if (combo_box->priv->model)
3427     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3428                              combo_box->priv->model);
3429     
3430   combo_box->priv->column = gtk_tree_view_column_new ();
3431   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
3432                                combo_box->priv->column);
3433
3434   /* sync up */
3435   gtk_combo_box_sync_cells (combo_box, 
3436                             GTK_CELL_LAYOUT (combo_box->priv->column));
3437
3438   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3439     {
3440       GtkTreePath *path;
3441
3442       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3443       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
3444                                 path, NULL, FALSE);
3445       gtk_tree_path_free (path);
3446     }
3447
3448   /* set sample/popup widgets */
3449   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
3450
3451   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
3452                     G_CALLBACK (gtk_combo_box_list_key_press),
3453                     combo_box);
3454   g_signal_connect (combo_box->priv->tree_view, "enter_notify_event",
3455                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3456                     combo_box);
3457   g_signal_connect (combo_box->priv->tree_view, "row_expanded",
3458                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3459                     combo_box);
3460   g_signal_connect (combo_box->priv->tree_view, "row_collapsed",
3461                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3462                     combo_box);
3463   g_signal_connect (combo_box->priv->popup_window, "button_press_event",
3464                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3465                     combo_box);
3466   g_signal_connect (combo_box->priv->popup_window, "button_release_event",
3467                     G_CALLBACK (gtk_combo_box_list_button_released),
3468                     combo_box);
3469
3470   gtk_widget_show (combo_box->priv->tree_view);
3471 }
3472
3473 static void
3474 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3475 {
3476   /* disconnect signals */
3477   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
3478                                         G_SIGNAL_MATCH_DATA,
3479                                         0, 0, NULL, NULL, combo_box);
3480   g_signal_handlers_disconnect_matched (combo_box->priv->button,
3481                                         G_SIGNAL_MATCH_DATA,
3482                                         0, 0, NULL,
3483                                         gtk_combo_box_list_button_pressed,
3484                                         NULL);
3485   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3486                                         G_SIGNAL_MATCH_DATA,
3487                                         0, 0, NULL,
3488                                         gtk_combo_box_list_button_pressed,
3489                                         NULL);
3490   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3491                                         G_SIGNAL_MATCH_DATA,
3492                                         0, 0, NULL,
3493                                         gtk_combo_box_list_button_released,
3494                                         NULL);
3495
3496   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3497                                         G_SIGNAL_MATCH_DATA,
3498                                         0, 0, NULL, 
3499                                         gtk_combo_box_child_show,
3500                                         NULL);
3501
3502   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3503                                         G_SIGNAL_MATCH_DATA,
3504                                         0, 0, NULL, 
3505                                         gtk_combo_box_child_hide,
3506                                         NULL);
3507   
3508   if (combo_box->priv->box)
3509     g_signal_handlers_disconnect_matched (combo_box->priv->box,
3510                                           G_SIGNAL_MATCH_DATA,
3511                                           0, 0, NULL,
3512                                           gtk_combo_box_list_button_pressed,
3513                                           NULL);
3514
3515   /* destroy things (unparent will kill the latest ref from us)
3516    * last unref on button will destroy the arrow
3517    */
3518   gtk_widget_unparent (combo_box->priv->button);
3519   combo_box->priv->button = NULL;
3520   combo_box->priv->arrow = NULL;
3521
3522   if (combo_box->priv->cell_view)
3523     {
3524       g_object_set (combo_box->priv->cell_view,
3525                     "background-set", FALSE,
3526                     NULL);
3527     }
3528
3529   if (combo_box->priv->cell_view_frame)
3530     {
3531       gtk_widget_unparent (combo_box->priv->cell_view_frame);
3532       combo_box->priv->cell_view_frame = NULL;
3533       combo_box->priv->box = NULL;
3534     }
3535
3536   if (combo_box->priv->scroll_timer)
3537     {
3538       g_source_remove (combo_box->priv->scroll_timer);
3539       combo_box->priv->scroll_timer = 0;
3540     }
3541
3542   if (combo_box->priv->resize_idle_id)
3543     {
3544       g_source_remove (combo_box->priv->resize_idle_id);
3545       combo_box->priv->resize_idle_id = 0;
3546     }
3547
3548   gtk_widget_destroy (combo_box->priv->tree_view);
3549
3550   combo_box->priv->tree_view = NULL;
3551   combo_box->priv->popup_widget = NULL;
3552 }
3553
3554 /* callbacks */
3555
3556 static gboolean
3557 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3558                                    GdkEventButton *event,
3559                                    gpointer        data)
3560 {
3561   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3562
3563   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3564
3565   if (ewidget == combo_box->priv->popup_window)
3566     return TRUE;
3567
3568   if ((ewidget != combo_box->priv->button && ewidget != combo_box->priv->box) ||
3569       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3570     return FALSE;
3571
3572   if (combo_box->priv->focus_on_click && 
3573       !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
3574     gtk_widget_grab_focus (combo_box->priv->button);
3575
3576   gtk_combo_box_popup (combo_box);
3577
3578   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
3579                                 TRUE);
3580
3581   combo_box->priv->auto_scroll = FALSE;
3582   if (combo_box->priv->scroll_timer == 0)
3583     combo_box->priv->scroll_timer = g_timeout_add (SCROLL_TIME, 
3584                                                    (GSourceFunc) gtk_combo_box_list_scroll_timeout, 
3585                                                    combo_box);
3586
3587   combo_box->priv->popup_in_progress = TRUE;
3588
3589   return TRUE;
3590 }
3591
3592 static gboolean
3593 gtk_combo_box_list_button_released (GtkWidget      *widget,
3594                                     GdkEventButton *event,
3595                                     gpointer        data)
3596 {
3597   gboolean ret;
3598   GtkTreePath *path = NULL;
3599   GtkTreeIter iter;
3600
3601   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3602
3603   gboolean popup_in_progress = FALSE;
3604
3605   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3606
3607   if (combo_box->priv->popup_in_progress)
3608     {
3609       popup_in_progress = TRUE;
3610       combo_box->priv->popup_in_progress = FALSE;
3611     }
3612
3613   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3614                                   FALSE);
3615   if (combo_box->priv->scroll_timer)
3616     {
3617       g_source_remove (combo_box->priv->scroll_timer);
3618       combo_box->priv->scroll_timer = 0;
3619     }
3620
3621   if (ewidget != combo_box->priv->tree_view)
3622     {
3623       if ((ewidget == combo_box->priv->button || 
3624            ewidget == combo_box->priv->box) &&
3625           !popup_in_progress &&
3626           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3627         {
3628           gtk_combo_box_popdown (combo_box);
3629           return TRUE;
3630         }
3631
3632       /* released outside treeview */
3633       if (ewidget != combo_box->priv->button && 
3634           ewidget != combo_box->priv->box)
3635         {
3636           gtk_combo_box_popdown (combo_box);
3637
3638           return TRUE;
3639         }
3640
3641       return FALSE;
3642     }
3643
3644   /* select something cool */
3645   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (combo_box->priv->tree_view),
3646                                        event->x, event->y,
3647                                        &path,
3648                                        NULL, NULL, NULL);
3649
3650   if (!ret)
3651     return TRUE; /* clicked outside window? */
3652
3653   gtk_tree_model_get_iter (combo_box->priv->model, &iter, path);
3654   gtk_tree_path_free (path);
3655
3656   gtk_combo_box_popdown (combo_box);
3657
3658   if (tree_column_row_is_sensitive (combo_box, &iter))
3659     gtk_combo_box_set_active_iter (combo_box, &iter);
3660
3661   return TRUE;
3662 }
3663
3664 static gboolean
3665 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3666                               GdkEventKey *event,
3667                               gpointer     data)
3668 {
3669   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3670   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3671
3672   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3673       state == GDK_MOD1_MASK)
3674     {
3675       gtk_combo_box_popdown (combo_box);
3676
3677       return TRUE;
3678     }
3679   
3680   return FALSE;
3681 }
3682
3683 static gboolean
3684 gtk_combo_box_list_key_press (GtkWidget   *widget,
3685                               GdkEventKey *event,
3686                               gpointer     data)
3687 {
3688   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3689   GtkTreeIter iter;
3690   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3691
3692   if (event->keyval == GDK_Escape ||
3693       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3694        state == GDK_MOD1_MASK))
3695     {
3696       gtk_combo_box_popdown (combo_box);
3697       
3698       /* reset active item -- this is incredibly lame and ugly */
3699       if (gtk_combo_box_get_active_iter (combo_box, &iter))
3700         gtk_combo_box_set_active_iter (combo_box, &iter);
3701       
3702       return TRUE;
3703     }
3704     
3705   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
3706       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
3707   {
3708     GtkTreeModel *model = NULL;
3709     
3710     gtk_combo_box_popdown (combo_box);
3711     
3712     if (combo_box->priv->model)
3713       {
3714         GtkTreeSelection *sel;
3715
3716         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3717
3718         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3719           gtk_combo_box_set_active_iter (combo_box, &iter);
3720       }
3721     
3722     return TRUE;
3723   }
3724
3725   return FALSE;
3726 }
3727
3728 static void
3729 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3730                                 gint         x, 
3731                                 gint         y)
3732 {
3733   GtkWidget *tree_view = combo_box->priv->tree_view;
3734   GtkAdjustment *adj;
3735   gdouble value;
3736
3737   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3738   if (adj && adj->upper - adj->lower > adj->page_size)
3739     {
3740       if (x <= tree_view->allocation.x && 
3741           adj->lower < adj->value)
3742         {
3743           value = adj->value - (tree_view->allocation.x - x + 1);
3744           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3745         }
3746       else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3747                adj->upper - adj->page_size > adj->value)
3748         {
3749           value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3750           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3751         }
3752     }
3753
3754   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3755   if (adj && adj->upper - adj->lower > adj->page_size)
3756     {
3757       if (y <= tree_view->allocation.y && 
3758           adj->lower < adj->value)
3759         {
3760           value = adj->value - (tree_view->allocation.y - y + 1);
3761           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3762         }
3763       else if (y >= tree_view->allocation.height &&
3764                adj->upper - adj->page_size > adj->value)
3765         {
3766           value = adj->value + (y - tree_view->allocation.height + 1);
3767           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3768         }
3769     }
3770 }
3771
3772 static gboolean
3773 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3774 {
3775   gint x, y;
3776
3777   GDK_THREADS_ENTER ();
3778
3779   if (combo_box->priv->auto_scroll)
3780     {
3781       gdk_window_get_pointer (combo_box->priv->tree_view->window, 
3782                               &x, &y, NULL);
3783       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3784     }
3785
3786   GDK_THREADS_LEAVE ();
3787
3788   return TRUE;
3789 }
3790
3791 static gboolean 
3792 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3793                                  GdkEventCrossing *event,
3794                                  gpointer          data)
3795 {
3796   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3797
3798   combo_box->priv->auto_scroll = TRUE;
3799
3800   return TRUE;
3801 }
3802
3803 static gboolean
3804 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3805                                 GtkTreeModel     *model,
3806                                 GtkTreePath      *path,
3807                                 gboolean          path_currently_selected,
3808                                 gpointer          data)
3809 {
3810   GList *list;
3811   gboolean sensitive = FALSE;
3812
3813   for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
3814     {
3815       GList *cells, *cell;
3816       gboolean cell_sensitive, cell_visible;
3817       GtkTreeIter iter;
3818       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3819
3820       if (!column->visible)
3821         continue;
3822
3823       gtk_tree_model_get_iter (model, &iter, path);
3824       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3825                                                FALSE, FALSE);
3826
3827       cell = cells = gtk_tree_view_column_get_cell_renderers (column);
3828       while (cell)
3829         {
3830           g_object_get (cell->data,
3831                         "sensitive", &cell_sensitive,
3832                         "visible", &cell_visible,
3833                         NULL);
3834
3835           if (cell_visible && cell_sensitive)
3836             break;
3837
3838           cell = cell->next;
3839         }
3840       g_list_free (cells);
3841
3842       sensitive = cell_sensitive;
3843     }
3844
3845   return sensitive;
3846 }
3847
3848 static void
3849 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3850                                 GtkTreePath  *path,
3851                                 GtkTreeIter  *iter,
3852                                 gpointer      data)
3853 {
3854   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3855   gint width;
3856
3857   width = gtk_combo_box_calc_requested_width (combo_box, path);
3858
3859   if (width > combo_box->priv->width)
3860     {
3861       if (combo_box->priv->cell_view) 
3862         {
3863           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3864           gtk_widget_queue_resize (combo_box->priv->cell_view);
3865         }
3866       combo_box->priv->width = width;
3867     }
3868 }
3869
3870 /*
3871  * GtkCellLayout implementation
3872  */
3873
3874 static void
3875 pack_start_recurse (GtkWidget       *menu,
3876                     GtkCellRenderer *cell,
3877                     gboolean         expand)
3878 {
3879   GList *i, *list;
3880   GtkWidget *submenu;    
3881   
3882   list = gtk_container_get_children (GTK_CONTAINER (menu));
3883   for (i = list; i; i = i->next)
3884     {
3885       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3886         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3887                                     cell, expand);
3888
3889       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3890       if (submenu != NULL)
3891         pack_start_recurse (submenu, cell, expand);
3892     }
3893
3894   g_list_free (list);
3895 }
3896
3897 static void
3898 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
3899                                       GtkCellRenderer *cell,
3900                                       gboolean         expand)
3901 {
3902   ComboCellInfo *info;
3903   GtkComboBox *combo_box;
3904
3905   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3906   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3907
3908   combo_box = GTK_COMBO_BOX (layout);
3909
3910   g_object_ref_sink (cell);
3911
3912   info = g_new0 (ComboCellInfo, 1);
3913   info->cell = cell;
3914   info->expand = expand;
3915   info->pack = GTK_PACK_START;
3916
3917   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3918
3919   if (combo_box->priv->cell_view)
3920     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3921                                 cell, expand);
3922
3923   if (combo_box->priv->column)
3924     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
3925
3926   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3927     pack_start_recurse (combo_box->priv->popup_widget, cell, expand);
3928 }
3929
3930 static void
3931 pack_end_recurse (GtkWidget       *menu,
3932                   GtkCellRenderer *cell,
3933                   gboolean         expand)
3934 {
3935   GList *i, *list;
3936   GtkWidget *submenu;    
3937   
3938   list = gtk_container_get_children (GTK_CONTAINER (menu));
3939   for (i = list; i; i = i->next)
3940     {
3941       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3942         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3943                                   cell, expand);
3944
3945       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3946       if (submenu != NULL)
3947         pack_end_recurse (submenu, cell, expand);
3948     }
3949
3950   g_list_free (list);
3951 }
3952
3953 static void
3954 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
3955                                     GtkCellRenderer *cell,
3956                                     gboolean         expand)
3957 {
3958   ComboCellInfo *info;
3959   GtkComboBox *combo_box;
3960
3961   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3962   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3963
3964   combo_box = GTK_COMBO_BOX (layout);
3965
3966   g_object_ref_sink (cell);
3967
3968   info = g_new0 (ComboCellInfo, 1);
3969   info->cell = cell;
3970   info->expand = expand;
3971   info->pack = GTK_PACK_END;
3972
3973   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3974
3975   if (combo_box->priv->cell_view)
3976     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3977                               cell, expand);
3978
3979   if (combo_box->priv->column)
3980     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
3981
3982   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3983     pack_end_recurse (combo_box->priv->popup_widget, cell, expand);
3984 }
3985
3986 static void
3987 clear_recurse (GtkWidget *menu)
3988 {
3989   GList *i, *list;
3990   GtkWidget *submenu;    
3991   
3992   list = gtk_container_get_children (GTK_CONTAINER (menu));
3993   for (i = list; i; i = i->next)
3994     {
3995       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3996         gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child)); 
3997
3998       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3999       if (submenu != NULL)
4000         clear_recurse (submenu);
4001     }
4002
4003   g_list_free (list);
4004 }
4005
4006 static void
4007 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4008 {
4009   GtkComboBox *combo_box;
4010   GSList *i;
4011   
4012   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4013
4014   combo_box = GTK_COMBO_BOX (layout);
4015  
4016   if (combo_box->priv->cell_view)
4017     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
4018
4019   if (combo_box->priv->column)
4020     gtk_tree_view_column_clear (combo_box->priv->column);
4021
4022   for (i = combo_box->priv->cells; i; i = i->next)
4023     {
4024      ComboCellInfo *info = (ComboCellInfo *)i->data;
4025
4026       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4027       g_object_unref (info->cell);
4028       g_free (info);
4029       i->data = NULL;
4030     }
4031   g_slist_free (combo_box->priv->cells);
4032   combo_box->priv->cells = NULL;
4033
4034   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4035     clear_recurse (combo_box->priv->popup_widget);
4036 }
4037
4038 static void
4039 add_attribute_recurse (GtkWidget       *menu,
4040                        GtkCellRenderer *cell,
4041                        const gchar     *attribute,
4042                        gint             column)
4043 {
4044   GList *i, *list;
4045   GtkWidget *submenu;    
4046   
4047   list = gtk_container_get_children (GTK_CONTAINER (menu));
4048   for (i = list; i; i = i->next)
4049     {
4050       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4051         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4052                                        cell, attribute, column); 
4053
4054       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4055       if (submenu != NULL)
4056         add_attribute_recurse (submenu, cell, attribute, column);
4057     }
4058
4059   g_list_free (list);
4060 }
4061                        
4062 static void
4063 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
4064                                          GtkCellRenderer *cell,
4065                                          const gchar     *attribute,
4066                                          gint             column)
4067 {
4068   ComboCellInfo *info;
4069   GtkComboBox *combo_box;
4070
4071   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4072   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4073
4074   combo_box = GTK_COMBO_BOX (layout);
4075
4076   info = gtk_combo_box_get_cell_info (combo_box, cell);
4077
4078   info->attributes = g_slist_prepend (info->attributes,
4079                                       GINT_TO_POINTER (column));
4080   info->attributes = g_slist_prepend (info->attributes,
4081                                       g_strdup (attribute));
4082
4083   if (combo_box->priv->cell_view)
4084     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4085                                    cell, attribute, column);
4086
4087   if (combo_box->priv->column)
4088     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4089                                    cell, attribute, column);
4090
4091   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4092     add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4093   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4094 }
4095
4096 static void
4097 combo_cell_data_func (GtkCellLayout   *cell_layout,
4098                       GtkCellRenderer *cell,
4099                       GtkTreeModel    *tree_model,
4100                       GtkTreeIter     *iter,
4101                       gpointer         data)
4102 {
4103   ComboCellInfo *info = (ComboCellInfo *)data;
4104   GtkWidget *parent = NULL;
4105   
4106   if (!info->func)
4107     return;
4108
4109   (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
4110   
4111   if (GTK_IS_WIDGET (cell_layout))
4112     parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4113   
4114   if (GTK_IS_MENU_ITEM (parent) && 
4115       gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4116     g_object_set (cell, "sensitive", TRUE, NULL);
4117 }
4118
4119
4120 static void 
4121 set_cell_data_func_recurse (GtkWidget             *menu,
4122                             GtkCellRenderer       *cell,
4123                             ComboCellInfo         *info)
4124 {
4125   GList *i, *list;
4126   GtkWidget *submenu;    
4127   GtkWidget *cell_view;
4128   
4129  list = gtk_container_get_children (GTK_CONTAINER (menu));
4130   for (i = list; i; i = i->next)
4131     {
4132       cell_view = GTK_BIN (i->data)->child;
4133       if (GTK_IS_CELL_LAYOUT (cell_view))
4134         {
4135           /* Override sensitivity for inner nodes; we don't
4136            * want menuitems with submenus to appear insensitive 
4137            */ 
4138           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view), 
4139                                               cell, 
4140                                               combo_cell_data_func, 
4141                                               info, NULL); 
4142           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4143           if (submenu != NULL)
4144             set_cell_data_func_recurse (submenu, cell, info);
4145         }
4146     }
4147
4148   g_list_free (list);
4149 }
4150
4151 static void
4152 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4153                                               GtkCellRenderer       *cell,
4154                                               GtkCellLayoutDataFunc  func,
4155                                               gpointer               func_data,
4156                                               GDestroyNotify         destroy)
4157 {
4158   ComboCellInfo *info;
4159   GtkComboBox *combo_box;
4160
4161   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4162
4163   combo_box = GTK_COMBO_BOX (layout);
4164
4165   info = gtk_combo_box_get_cell_info (combo_box, cell);
4166   g_return_if_fail (info != NULL);
4167   
4168   if (info->destroy)
4169     {
4170       GDestroyNotify d = info->destroy;
4171
4172       info->destroy = NULL;
4173       d (info->func_data);
4174     }
4175
4176   info->func = func;
4177   info->func_data = func_data;
4178   info->destroy = destroy;
4179
4180   if (combo_box->priv->cell_view)
4181     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
4182
4183   if (combo_box->priv->column)
4184     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
4185
4186   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4187     set_cell_data_func_recurse (combo_box->priv->popup_widget, cell, info);
4188
4189   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4190 }
4191
4192 static void 
4193 clear_attributes_recurse (GtkWidget             *menu,
4194                           GtkCellRenderer       *cell)
4195 {
4196   GList *i, *list;
4197   GtkWidget *submenu;    
4198   
4199   list = gtk_container_get_children (GTK_CONTAINER (menu));
4200   for (i = list; i; i = i->next)
4201     {
4202       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4203         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4204                                             cell); 
4205       
4206       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4207       if (submenu != NULL)
4208         clear_attributes_recurse (submenu, cell);
4209     }
4210
4211   g_list_free (list);
4212 }
4213
4214 static void
4215 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
4216                                             GtkCellRenderer *cell)
4217 {
4218   ComboCellInfo *info;
4219   GtkComboBox *combo_box;
4220   GSList *list;
4221
4222   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4223   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4224
4225   combo_box = GTK_COMBO_BOX (layout);
4226
4227   info = gtk_combo_box_get_cell_info (combo_box, cell);
4228   g_return_if_fail (info != NULL);
4229
4230   list = info->attributes;
4231   while (list && list->next)
4232     {
4233       g_free (list->data);
4234       list = list->next->next;
4235     }
4236   g_slist_free (info->attributes);
4237   info->attributes = NULL;
4238
4239   if (combo_box->priv->cell_view)
4240     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
4241
4242   if (combo_box->priv->column)
4243     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
4244
4245   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4246     clear_attributes_recurse (combo_box->priv->popup_widget, cell);
4247
4248   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4249 }
4250
4251 static void 
4252 reorder_recurse (GtkWidget             *menu,
4253                  GtkCellRenderer       *cell,
4254                  gint                   position)
4255 {
4256   GList *i, *list;
4257   GtkWidget *submenu;    
4258   
4259   list = gtk_container_get_children (GTK_CONTAINER (menu));
4260   for (i = list; i; i = i->next)
4261     {
4262       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4263         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4264                                  cell, position); 
4265       
4266       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4267       if (submenu != NULL)
4268         reorder_recurse (submenu, cell, position);
4269     }
4270
4271   g_list_free (list);
4272 }
4273
4274 static void
4275 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
4276                                    GtkCellRenderer *cell,
4277                                    gint             position)
4278 {
4279   ComboCellInfo *info;
4280   GtkComboBox *combo_box;
4281   GSList *link;
4282
4283   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4284   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4285
4286   combo_box = GTK_COMBO_BOX (layout);
4287
4288   info = gtk_combo_box_get_cell_info (combo_box, cell);
4289
4290   g_return_if_fail (info != NULL);
4291   g_return_if_fail (position >= 0);
4292
4293   link = g_slist_find (combo_box->priv->cells, info);
4294
4295   g_return_if_fail (link != NULL);
4296
4297   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
4298   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
4299                                            position);
4300
4301   if (combo_box->priv->cell_view)
4302     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4303                              cell, position);
4304
4305   if (combo_box->priv->column)
4306     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
4307                              cell, position);
4308
4309   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4310     reorder_recurse (combo_box->priv->popup_widget, cell, position);
4311
4312   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4313 }
4314
4315 /*
4316  * public API
4317  */
4318
4319 /**
4320  * gtk_combo_box_new:
4321  *
4322  * Creates a new empty #GtkComboBox.
4323  *
4324  * Return value: A new #GtkComboBox.
4325  *
4326  * Since: 2.4
4327  */
4328 GtkWidget *
4329 gtk_combo_box_new (void)
4330 {
4331   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4332 }
4333
4334 /**
4335  * gtk_combo_box_new_with_model:
4336  * @model: A #GtkTreeModel.
4337  *
4338  * Creates a new #GtkComboBox with the model initialized to @model.
4339  *
4340  * Return value: A new #GtkComboBox.
4341  *
4342  * Since: 2.4
4343  */
4344 GtkWidget *
4345 gtk_combo_box_new_with_model (GtkTreeModel *model)
4346 {
4347   GtkComboBox *combo_box;
4348
4349   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4350
4351   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4352
4353   return GTK_WIDGET (combo_box);
4354 }
4355
4356 /**
4357  * gtk_combo_box_get_wrap_width:
4358  * @combo_box: A #GtkComboBox.
4359  *
4360  * Returns the wrap width which is used to determine the number
4361  * of columns for the popup menu. If the wrap width is larger than
4362  * 1, the combo box is in table mode.
4363  *
4364  * Returns: the wrap width.
4365  *
4366  * Since: 2.6
4367  */
4368 gint
4369 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4370 {
4371   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4372
4373   return combo_box->priv->wrap_width;
4374 }
4375
4376 /**
4377  * gtk_combo_box_set_wrap_width:
4378  * @combo_box: A #GtkComboBox.
4379  * @width: Preferred number of columns.
4380  *
4381  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4382  * the preferred number of columns when you want the popup to be layed out
4383  * in a table.
4384  *
4385  * Since: 2.4
4386  */
4387 void
4388 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4389                               gint         width)
4390 {
4391   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4392   g_return_if_fail (width >= 0);
4393
4394   if (width != combo_box->priv->wrap_width)
4395     {
4396       combo_box->priv->wrap_width = width;
4397
4398       gtk_combo_box_check_appearance (combo_box);
4399       gtk_combo_box_relayout (combo_box);
4400       
4401       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4402     }
4403 }
4404
4405 /**
4406  * gtk_combo_box_get_row_span_column:
4407  * @combo_box: A #GtkComboBox.
4408  *
4409  * Returns the column with row span information for @combo_box.
4410  *
4411  * Returns: the row span column.
4412  *
4413  * Since: 2.6
4414  */
4415 gint
4416 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4417 {
4418   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4419
4420   return combo_box->priv->row_column;
4421 }
4422
4423 /**
4424  * gtk_combo_box_set_row_span_column:
4425  * @combo_box: A #GtkComboBox.
4426  * @row_span: A column in the model passed during construction.
4427  *
4428  * Sets the column with row span information for @combo_box to be @row_span.
4429  * The row span column contains integers which indicate how many rows
4430  * an item should span.
4431  *
4432  * Since: 2.4
4433  */
4434 void
4435 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4436                                    gint         row_span)
4437 {
4438   gint col;
4439
4440   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4441
4442   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4443   g_return_if_fail (row_span >= -1 && row_span < col);
4444
4445   if (row_span != combo_box->priv->row_column)
4446     {
4447       combo_box->priv->row_column = row_span;
4448       
4449       gtk_combo_box_relayout (combo_box);
4450  
4451       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4452     }
4453 }
4454
4455 /**
4456  * gtk_combo_box_get_column_span_column:
4457  * @combo_box: A #GtkComboBox.
4458  *
4459  * Returns the column with column span information for @combo_box.
4460  *
4461  * Returns: the column span column.
4462  *
4463  * Since: 2.6
4464  */
4465 gint
4466 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4467 {
4468   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4469
4470   return combo_box->priv->col_column;
4471 }
4472
4473 /**
4474  * gtk_combo_box_set_column_span_column:
4475  * @combo_box: A #GtkComboBox.
4476  * @column_span: A column in the model passed during construction.
4477  *
4478  * Sets the column with column span information for @combo_box to be
4479  * @column_span. The column span column contains integers which indicate
4480  * how many columns an item should span.
4481  *
4482  * Since: 2.4
4483  */
4484 void
4485 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4486                                       gint         column_span)
4487 {
4488   gint col;
4489
4490   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4491
4492   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4493   g_return_if_fail (column_span >= -1 && column_span < col);
4494
4495   if (column_span != combo_box->priv->col_column)
4496     {
4497       combo_box->priv->col_column = column_span;
4498       
4499       gtk_combo_box_relayout (combo_box);
4500
4501       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4502     }
4503 }
4504
4505 /**
4506  * gtk_combo_box_get_active:
4507  * @combo_box: A #GtkComboBox.
4508  *
4509  * Returns the index of the currently active item, or -1 if there's no
4510  * active item. If the model is a non-flat treemodel, and the active item 
4511  * is not an immediate child of the root of the tree, this function returns 
4512  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where 
4513  * <literal>path</literal> is the #GtkTreePath of the active item.
4514  *
4515  * Return value: An integer which is the index of the currently active item, or
4516  * -1 if there's no active item.
4517  *
4518  * Since: 2.4
4519  */
4520 gint
4521 gtk_combo_box_get_active (GtkComboBox *combo_box)
4522 {
4523   gint result;
4524   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4525
4526   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
4527     {
4528       GtkTreePath *path;
4529
4530       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);      
4531       result = gtk_tree_path_get_indices (path)[0];
4532       gtk_tree_path_free (path);
4533     }
4534   else
4535     result = -1;
4536
4537   return result;
4538 }
4539
4540 /**
4541  * gtk_combo_box_set_active:
4542  * @combo_box: A #GtkComboBox.
4543  * @index_: An index in the model passed during construction, or -1 to have
4544  * no active item.
4545  *
4546  * Sets the active item of @combo_box to be the item at @index.
4547  *
4548  * Since: 2.4
4549  */
4550 void
4551 gtk_combo_box_set_active (GtkComboBox *combo_box,
4552                           gint         index_)
4553 {
4554   GtkTreePath *path = NULL;
4555   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4556   g_return_if_fail (index_ >= -1);
4557
4558   if (index_ != -1)
4559     path = gtk_tree_path_new_from_indices (index_, -1);
4560    
4561   gtk_combo_box_set_active_internal (combo_box, path);
4562
4563   if (path)
4564     gtk_tree_path_free (path);
4565 }
4566
4567 static void
4568 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4569                                    GtkTreePath *path)
4570 {
4571   GtkTreePath *active_path;
4572   gint path_cmp;
4573
4574   if (path && gtk_tree_row_reference_valid (combo_box->priv->active_row))
4575     {
4576       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4577       path_cmp = gtk_tree_path_compare (path, active_path);
4578       gtk_tree_path_free (active_path);
4579       if (path_cmp == 0)
4580         return;
4581     }
4582
4583   if (combo_box->priv->active_row)
4584     {
4585       gtk_tree_row_reference_free (combo_box->priv->active_row);
4586       combo_box->priv->active_row = NULL;
4587     }
4588   
4589   if (!path)
4590     {
4591       if (combo_box->priv->tree_view)
4592         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
4593       else
4594         {
4595           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
4596
4597           if (GTK_IS_MENU (menu))
4598             gtk_menu_set_active (menu, -1);
4599         }
4600
4601       if (combo_box->priv->cell_view)
4602         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
4603     }
4604   else
4605     {
4606       combo_box->priv->active_row = 
4607         gtk_tree_row_reference_new (combo_box->priv->model, path);
4608
4609       if (combo_box->priv->tree_view)
4610         {
4611           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), 
4612                                     path, NULL, FALSE);
4613         }
4614       else if (GTK_IS_MENU (combo_box->priv->popup_widget))
4615         {
4616           /* FIXME handle nested menus better */
4617           gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), 
4618                                gtk_tree_path_get_indices (path)[0]);
4619         }
4620
4621       if (combo_box->priv->cell_view)
4622         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
4623                                          path);
4624     }
4625
4626   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4627   g_object_notify (G_OBJECT (combo_box), "active");
4628 }
4629
4630
4631 /**
4632  * gtk_combo_box_get_active_iter:
4633  * @combo_box: A #GtkComboBox
4634  * @iter: The uninitialized #GtkTreeIter.
4635  * 
4636  * Sets @iter to point to the current active item, if it exists.
4637  * 
4638  * Return value: %TRUE, if @iter was set
4639  *
4640  * Since: 2.4
4641  **/
4642 gboolean
4643 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4644                                GtkTreeIter     *iter)
4645 {
4646   GtkTreePath *path;
4647   gboolean result;
4648
4649   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4650
4651   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4652     return FALSE;
4653
4654   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4655   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4656   gtk_tree_path_free (path);
4657
4658   return result;
4659 }
4660
4661 /**
4662  * gtk_combo_box_set_active_iter:
4663  * @combo_box: A #GtkComboBox
4664  * @iter: The #GtkTreeIter.
4665  * 
4666  * Sets the current active item to be the one referenced by @iter. 
4667  * @iter must correspond to a path of depth one.
4668  * 
4669  * Since: 2.4
4670  **/
4671 void
4672 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4673                                GtkTreeIter     *iter)
4674 {
4675   GtkTreePath *path;
4676
4677   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4678
4679   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4680   gtk_combo_box_set_active_internal (combo_box, path);
4681   gtk_tree_path_free (path);
4682 }
4683
4684 /**
4685  * gtk_combo_box_set_model:
4686  * @combo_box: A #GtkComboBox.
4687  * @model: A #GtkTreeModel.
4688  *
4689  * Sets the model used by @combo_box to be @model. Will unset a previously set 
4690  * model (if applicable). If model is %NULL, then it will unset the model.
4691  *
4692  * Note that this function does not clear the cell renderers, you have to 
4693  * call gtk_combo_box_cell_layout_clear() yourself if you need to set up 
4694  * different cell renderers for the new model.
4695  *
4696  * Since: 2.4
4697  */
4698 void
4699 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4700                          GtkTreeModel *model)
4701 {
4702   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4703
4704   if (!model)
4705     {
4706       gtk_combo_box_unset_model (combo_box);
4707       return;
4708     }
4709
4710   g_return_if_fail (GTK_IS_TREE_MODEL (model));
4711
4712   if (model == combo_box->priv->model)
4713     return;
4714   
4715   if (combo_box->priv->model)
4716     gtk_combo_box_unset_model (combo_box);
4717
4718   combo_box->priv->model = model;
4719   g_object_ref (combo_box->priv->model);
4720
4721   combo_box->priv->inserted_id =
4722     g_signal_connect (combo_box->priv->model, "row_inserted",
4723                       G_CALLBACK (gtk_combo_box_model_row_inserted),
4724                       combo_box);
4725   combo_box->priv->deleted_id =
4726     g_signal_connect (combo_box->priv->model, "row_deleted",
4727                       G_CALLBACK (gtk_combo_box_model_row_deleted),
4728                       combo_box);
4729   combo_box->priv->reordered_id =
4730     g_signal_connect (combo_box->priv->model, "rows_reordered",
4731                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
4732                       combo_box);
4733   combo_box->priv->changed_id =
4734     g_signal_connect (combo_box->priv->model, "row_changed",
4735                       G_CALLBACK (gtk_combo_box_model_row_changed),
4736                       combo_box);
4737       
4738   if (combo_box->priv->tree_view)
4739     {
4740       /* list mode */
4741       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4742                                combo_box->priv->model);
4743       gtk_combo_box_list_popup_resize (combo_box);
4744     }
4745   else
4746     {
4747       /* menu mode */
4748       if (combo_box->priv->popup_widget)
4749         gtk_combo_box_menu_fill (combo_box);
4750
4751     }
4752
4753   if (combo_box->priv->cell_view)
4754     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4755                              combo_box->priv->model);
4756 }
4757
4758 /**
4759  * gtk_combo_box_get_model
4760  * @combo_box: A #GtkComboBox.
4761  *
4762  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4763  *
4764  * Return value: A #GtkTreeModel which was passed during construction.
4765  *
4766  * Since: 2.4
4767  */
4768 GtkTreeModel *
4769 gtk_combo_box_get_model (GtkComboBox *combo_box)
4770 {
4771   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4772
4773   return combo_box->priv->model;
4774 }
4775
4776
4777 /* convenience API for simple text combos */
4778
4779 /**
4780  * gtk_combo_box_new_text:
4781  *
4782  * Convenience function which constructs a new text combo box, which is a
4783  * #GtkComboBox just displaying strings. If you use this function to create
4784  * a text combo box, you should only manipulate its data source with the
4785  * following convenience functions: gtk_combo_box_append_text(),
4786  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
4787  * gtk_combo_box_remove_text().
4788  *
4789  * Return value: A new text combo box.
4790  *
4791  * Since: 2.4
4792  */
4793 GtkWidget *
4794 gtk_combo_box_new_text (void)
4795 {
4796   GtkWidget *combo_box;
4797   GtkCellRenderer *cell;
4798   GtkListStore *store;
4799
4800   store = gtk_list_store_new (1, G_TYPE_STRING);
4801   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
4802   g_object_unref (store);
4803
4804   cell = gtk_cell_renderer_text_new ();
4805   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
4806   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
4807                                   "text", 0,
4808                                   NULL);
4809
4810   return combo_box;
4811 }
4812
4813 /**
4814  * gtk_combo_box_append_text:
4815  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
4816  * @text: A string.
4817  *
4818  * Appends @string to the list of strings stored in @combo_box. Note that
4819  * you can only use this function with combo boxes constructed with
4820  * gtk_combo_box_new_text().
4821  *
4822  * Since: 2.4
4823  */
4824 void
4825 gtk_combo_box_append_text (GtkComboBox *combo_box,
4826                            const gchar *text)
4827 {
4828   GtkTreeIter iter;
4829   GtkListStore *store;
4830
4831   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4832   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4833   g_return_if_fail (text != NULL);
4834
4835   store = GTK_LIST_STORE (combo_box->priv->model);
4836
4837   gtk_list_store_append (store, &iter);
4838   gtk_list_store_set (store, &iter, 0, text, -1);
4839 }
4840
4841 /**
4842  * gtk_combo_box_insert_text:
4843  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
4844  * @position: An index to insert @text.
4845  * @text: A string.
4846  *
4847  * Inserts @string at @position in the list of strings stored in @combo_box.
4848  * Note that you can only use this function with combo boxes constructed
4849  * with gtk_combo_box_new_text().
4850  *
4851  * Since: 2.4
4852  */
4853 void
4854 gtk_combo_box_insert_text (GtkComboBox *combo_box,
4855                            gint         position,
4856                            const gchar *text)
4857 {
4858   GtkTreeIter iter;
4859   GtkListStore *store;
4860
4861   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4862   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4863   g_return_if_fail (position >= 0);
4864   g_return_if_fail (text != NULL);
4865
4866   store = GTK_LIST_STORE (combo_box->priv->model);
4867
4868   gtk_list_store_insert (store, &iter, position);
4869   gtk_list_store_set (store, &iter, 0, text, -1);
4870 }
4871
4872 /**
4873  * gtk_combo_box_prepend_text:
4874  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4875  * @text: A string.
4876  *
4877  * Prepends @string to the list of strings stored in @combo_box. Note that
4878  * you can only use this function with combo boxes constructed with
4879  * gtk_combo_box_new_text().
4880  *
4881  * Since: 2.4
4882  */
4883 void
4884 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
4885                             const gchar *text)
4886 {
4887   GtkTreeIter iter;
4888   GtkListStore *store;
4889
4890   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4891   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4892   g_return_if_fail (text != NULL);
4893
4894   store = GTK_LIST_STORE (combo_box->priv->model);
4895
4896   gtk_list_store_prepend (store, &iter);
4897   gtk_list_store_set (store, &iter, 0, text, -1);
4898 }
4899
4900 /**
4901  * gtk_combo_box_remove_text:
4902  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4903  * @position: Index of the item to remove.
4904  *
4905  * Removes the string at @position from @combo_box. Note that you can only use
4906  * this function with combo boxes constructed with gtk_combo_box_new_text().
4907  *
4908  * Since: 2.4
4909  */
4910 void
4911 gtk_combo_box_remove_text (GtkComboBox *combo_box,
4912                            gint         position)
4913 {
4914   GtkTreeIter iter;
4915   GtkListStore *store;
4916
4917   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4918   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4919   g_return_if_fail (position >= 0);
4920
4921   store = GTK_LIST_STORE (combo_box->priv->model);
4922
4923   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
4924                                      NULL, position))
4925     gtk_list_store_remove (store, &iter);
4926 }
4927
4928 /**
4929  * gtk_combo_box_get_active_text:
4930  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4931  *
4932  * Returns the currently active string in @combo_box or %NULL if none
4933  * is selected.  Note that you can only use this function with combo
4934  * boxes constructed with gtk_combo_box_new_text() and with 
4935  * #GtkComboBoxEntry<!-- -->s.
4936  *
4937  * Returns: a newly allocated string containing the currently active text.
4938  *
4939  * Since: 2.6
4940  */
4941 gchar *
4942 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
4943 {
4944   GtkComboBoxClass *class;
4945
4946   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4947
4948   class = GTK_COMBO_BOX_GET_CLASS (combo_box);
4949
4950   if (class->get_active_text)
4951     return (* class->get_active_text) (combo_box);
4952
4953   return NULL;
4954 }
4955
4956 static gchar *
4957 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
4958 {
4959   GtkTreeIter iter;
4960   gchar *text = NULL;
4961
4962   g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
4963
4964   if (gtk_combo_box_get_active_iter (combo_box, &iter))
4965     gtk_tree_model_get (combo_box->priv->model, &iter, 
4966                         0, &text, -1);
4967
4968   return text;
4969 }
4970
4971 static void
4972 gtk_combo_box_real_move_active (GtkComboBox   *combo_box,
4973                                 GtkScrollType  scroll)
4974 {
4975   GtkTreeIter iter;
4976   GtkTreeIter new_iter;
4977   gboolean    active_iter;
4978   gboolean    found;
4979
4980   if (!combo_box->priv->model)
4981     {
4982       gtk_widget_error_bell (GTK_WIDGET (combo_box));
4983       return;
4984     }
4985
4986   active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
4987
4988   switch (scroll)
4989     {
4990     case GTK_SCROLL_STEP_BACKWARD:
4991     case GTK_SCROLL_STEP_UP:
4992     case GTK_SCROLL_STEP_LEFT:
4993       if (active_iter)
4994         {
4995           found = tree_prev (combo_box, combo_box->priv->model,
4996                              &iter, &new_iter, FALSE);
4997           break;
4998         }
4999       /* else fall through */
5000
5001     case GTK_SCROLL_PAGE_FORWARD:
5002     case GTK_SCROLL_PAGE_DOWN:
5003     case GTK_SCROLL_PAGE_RIGHT:
5004     case GTK_SCROLL_END:
5005       found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5006       break;
5007
5008     case GTK_SCROLL_STEP_FORWARD:
5009     case GTK_SCROLL_STEP_DOWN:
5010     case GTK_SCROLL_STEP_RIGHT:
5011       if (active_iter)
5012         {
5013           found = tree_next (combo_box, combo_box->priv->model,
5014                              &iter, &new_iter, FALSE);
5015           break;
5016         }
5017       /* else fall through */
5018
5019     case GTK_SCROLL_PAGE_BACKWARD:
5020     case GTK_SCROLL_PAGE_UP:
5021     case GTK_SCROLL_PAGE_LEFT:
5022     case GTK_SCROLL_START:
5023       found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5024       break;
5025
5026     default:
5027       return;
5028     }
5029
5030   if (found && active_iter)
5031     {
5032       GtkTreePath *old_path;
5033       GtkTreePath *new_path;
5034
5035       old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5036       new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5037
5038       if (gtk_tree_path_compare (old_path, new_path) == 0)
5039         found = FALSE;
5040
5041       gtk_tree_path_free (old_path);
5042       gtk_tree_path_free (new_path);
5043     }
5044
5045   if (found)
5046     {
5047       gtk_combo_box_set_active_iter (combo_box, &new_iter);
5048     }
5049   else
5050     {
5051       gtk_widget_error_bell (GTK_WIDGET (combo_box));
5052     }
5053 }
5054
5055 static gboolean
5056 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5057                                  gboolean   group_cycling)
5058 {
5059   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5060
5061   gtk_widget_grab_focus (combo_box->priv->button);
5062
5063   return TRUE;
5064 }
5065
5066 static void
5067 gtk_combo_box_grab_focus (GtkWidget *widget)
5068 {
5069   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5070
5071   gtk_widget_grab_focus (combo_box->priv->button);
5072 }
5073
5074 static void
5075 gtk_combo_box_destroy (GtkObject *object)
5076 {
5077   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5078
5079   if (combo_box->priv->popup_idle_id > 0)
5080     {
5081       g_source_remove (combo_box->priv->popup_idle_id);
5082       combo_box->priv->popup_idle_id = 0;
5083     }
5084
5085   gtk_combo_box_popdown (combo_box);
5086
5087   if (combo_box->priv->row_separator_destroy)
5088     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5089
5090   combo_box->priv->row_separator_func = NULL;
5091   combo_box->priv->row_separator_data = NULL;
5092   combo_box->priv->row_separator_destroy = NULL;
5093
5094   GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5095   combo_box->priv->cell_view = NULL;
5096 }
5097
5098 static void
5099 gtk_combo_box_finalize (GObject *object)
5100 {
5101   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5102   GSList *i;
5103   
5104   if (GTK_IS_MENU (combo_box->priv->popup_widget))
5105     {
5106       gtk_combo_box_menu_destroy (combo_box);
5107       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5108       combo_box->priv->popup_widget = NULL;
5109     }
5110   
5111   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5112     gtk_combo_box_list_destroy (combo_box);
5113
5114   if (combo_box->priv->popup_window)
5115     gtk_widget_destroy (combo_box->priv->popup_window);
5116
5117   gtk_combo_box_unset_model (combo_box);
5118
5119   for (i = combo_box->priv->cells; i; i = i->next)
5120     {
5121       ComboCellInfo *info = (ComboCellInfo *)i->data;
5122       GSList *list = info->attributes;
5123
5124       if (info->destroy)
5125         info->destroy (info->func_data);
5126
5127       while (list && list->next)
5128         {
5129           g_free (list->data);
5130           list = list->next->next;
5131         }
5132       g_slist_free (info->attributes);
5133
5134       g_object_unref (info->cell);
5135       g_free (info);
5136     }
5137    g_slist_free (combo_box->priv->cells);
5138
5139    g_free (combo_box->priv->tearoff_title);
5140
5141    G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5142 }
5143
5144 static gboolean
5145 gtk_cell_editable_key_press (GtkWidget   *widget,
5146                              GdkEventKey *event,
5147                              gpointer     data)
5148 {
5149   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5150
5151   if (event->keyval == GDK_Escape)
5152     {
5153       combo_box->priv->editing_canceled = TRUE;
5154
5155       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5156       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5157       
5158       return TRUE;
5159     }
5160   else if (event->keyval == GDK_Return)
5161     {
5162       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5163       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5164       
5165       return TRUE;
5166     }
5167
5168   return FALSE;
5169 }
5170
5171 static gboolean
5172 popdown_idle (gpointer data)
5173 {
5174   GtkComboBox *combo_box;
5175
5176   GDK_THREADS_ENTER ();
5177
5178   combo_box = GTK_COMBO_BOX (data);
5179   
5180   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5181   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5182
5183   g_object_unref (combo_box);
5184
5185   GDK_THREADS_LEAVE ();
5186
5187   return FALSE;
5188 }
5189
5190 static void
5191 popdown_handler (GtkWidget *widget,
5192                  gpointer   data)
5193 {
5194   g_idle_add (popdown_idle, g_object_ref (data));
5195 }
5196
5197 static gboolean
5198 popup_idle (gpointer data)
5199 {
5200   GtkComboBox *combo_box;
5201
5202   GDK_THREADS_ENTER ();
5203
5204   combo_box = GTK_COMBO_BOX (data);
5205
5206   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5207       combo_box->priv->cell_view)
5208     g_signal_connect_object (combo_box->priv->popup_widget,
5209                              "unmap", G_CALLBACK (popdown_handler),
5210                              combo_box, 0);
5211   
5212   /* we unset this if a menu item is activated */
5213   combo_box->priv->editing_canceled = TRUE;
5214   gtk_combo_box_popup (combo_box);
5215
5216   combo_box->priv->popup_idle_id = 0;
5217
5218   GDK_THREADS_LEAVE ();
5219
5220   return FALSE;
5221 }
5222
5223 static void
5224 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5225                              GdkEvent        *event)
5226 {
5227   GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5228
5229   combo_box->priv->is_cell_renderer = TRUE;
5230
5231   if (combo_box->priv->cell_view)
5232     {
5233       g_signal_connect_object (combo_box->priv->button, "key_press_event",
5234                                G_CALLBACK (gtk_cell_editable_key_press), 
5235                                cell_editable, 0);  
5236
5237       gtk_widget_grab_focus (combo_box->priv->button);
5238     }
5239   else
5240     {
5241       g_signal_connect_object (GTK_BIN (combo_box)->child, "key_press_event",
5242                                G_CALLBACK (gtk_cell_editable_key_press), 
5243                                cell_editable, 0);  
5244
5245       gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5246       GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5247     }
5248
5249   /* we do the immediate popup only for the optionmenu-like 
5250    * appearance 
5251    */  
5252   if (combo_box->priv->is_cell_renderer && 
5253       combo_box->priv->cell_view && !combo_box->priv->tree_view)
5254     combo_box->priv->popup_idle_id = g_idle_add (popup_idle, combo_box);
5255 }
5256
5257
5258 /**
5259  * gtk_combo_box_get_add_tearoffs:
5260  * @combo_box: a #GtkComboBox
5261  * 
5262  * Gets the current value of the :add-tearoffs property.
5263  * 
5264  * Return value: the current value of the :add-tearoffs property.
5265  **/
5266 gboolean
5267 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5268 {
5269   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5270
5271   return combo_box->priv->add_tearoffs;
5272 }
5273
5274 /**
5275  * gtk_combo_box_set_add_tearoffs:
5276  * @combo_box: a #GtkComboBox 
5277  * @add_tearoffs: %TRUE to add tearoff menu items
5278  *  
5279  * Sets whether the popup menu should have a tearoff 
5280  * menu item.
5281  *
5282  * Since: 2.6
5283  **/
5284 void
5285 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5286                                 gboolean     add_tearoffs)
5287 {
5288   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5289
5290   add_tearoffs = add_tearoffs != FALSE;
5291
5292   if (combo_box->priv->add_tearoffs != add_tearoffs)
5293     {
5294       combo_box->priv->add_tearoffs = add_tearoffs;
5295       gtk_combo_box_check_appearance (combo_box);
5296       gtk_combo_box_relayout (combo_box);
5297       g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5298     }
5299 }
5300
5301 /**
5302  * gtk_combo_box_get_title:
5303  * @combo_box: a #GtkComboBox
5304  *
5305  * Gets the current title of the menu in tearoff mode. See
5306  * gtk_combo_box_set_add_tearoffs().
5307  *
5308  * Returns: the menu's title in tearoff mode. This is an internal copy of the
5309  * string which must not be freed.
5310  *
5311  * Since: 2.10
5312  */
5313 G_CONST_RETURN gchar*
5314 gtk_combo_box_get_title (GtkComboBox *combo_box)
5315 {
5316   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5317   
5318   return combo_box->priv->tearoff_title;
5319 }
5320
5321 static void
5322 gtk_combo_box_update_title (GtkComboBox *combo_box)
5323 {
5324   gtk_combo_box_check_appearance (combo_box);
5325   
5326   if (combo_box->priv->popup_widget && 
5327       GTK_IS_MENU (combo_box->priv->popup_widget))
5328     gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget), 
5329                         combo_box->priv->tearoff_title);
5330 }
5331
5332 /**
5333  * gtk_combo_box_set_title:
5334  * @combo_box: a #GtkComboBox 
5335  * @title: a title for the menu in tearoff mode.
5336  *  
5337  * Sets the menu's title in tearoff mode.
5338  *
5339  * Since: 2.10
5340  */
5341 void
5342 gtk_combo_box_set_title (GtkComboBox *combo_box,
5343                          const gchar *title)
5344 {
5345   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5346
5347   if (strcmp (title ? title : "", 
5348               combo_box->priv->tearoff_title ? combo_box->priv->tearoff_title : "") != 0)
5349     {
5350       g_free (combo_box->priv->tearoff_title);
5351       combo_box->priv->tearoff_title = g_strdup (title);
5352
5353       gtk_combo_box_update_title (combo_box);
5354
5355       g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5356     }
5357 }
5358
5359 gboolean
5360 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5361 {
5362   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5363
5364   return combo_box->priv->editing_canceled;
5365 }
5366
5367 /**
5368  * gtk_combo_box_get_popup_accessible:
5369  * @combo_box: a #GtkComboBox
5370  *
5371  * Gets the accessible object corresponding to the combo box's popup.
5372  *
5373  * This function is mostly intended for use by accessibility technologies;
5374  * applications should have little use for it.
5375  *
5376  * Returns: the accessible object corresponding to the combo box's popup.
5377  *
5378  * Since: 2.6
5379  **/
5380 AtkObject*
5381 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5382 {
5383   AtkObject *atk_obj;
5384
5385   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5386
5387   if (combo_box->priv->popup_widget)
5388     {
5389       atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5390       return atk_obj;
5391     }
5392
5393   return NULL;
5394 }
5395
5396 /**
5397  * gtk_combo_box_get_row_separator_func:
5398  * @combo_box: a #GtkComboBox
5399  * 
5400  * Returns the current row separator function.
5401  * 
5402  * Return value: the current row separator function.
5403  *
5404  * Since: 2.6
5405  **/
5406 GtkTreeViewRowSeparatorFunc 
5407 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5408 {
5409   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5410
5411   return combo_box->priv->row_separator_func;
5412 }
5413
5414 /**
5415  * gtk_combo_box_set_row_separator_func:
5416  * @combo_box: a #GtkComboBox
5417  * @func: a #GtkTreeViewRowSeparatorFunc
5418  * @data: user data to pass to @func, or %NULL
5419  * @destroy: destroy notifier for @data, or %NULL
5420  * 
5421  * Sets the row separator function, which is used to determine
5422  * whether a row should be drawn as a separator. If the row separator
5423  * function is %NULL, no separators are drawn. This is the default value.
5424  *
5425  * Since: 2.6
5426  **/
5427 void
5428 gtk_combo_box_set_row_separator_func (GtkComboBox                 *combo_box,
5429                                       GtkTreeViewRowSeparatorFunc  func,
5430                                       gpointer                     data,
5431                                       GtkDestroyNotify             destroy)
5432 {
5433   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5434
5435   if (combo_box->priv->row_separator_destroy)
5436     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5437
5438   combo_box->priv->row_separator_func = func;
5439   combo_box->priv->row_separator_data = data;
5440   combo_box->priv->row_separator_destroy = destroy;
5441
5442   if (combo_box->priv->tree_view)
5443     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
5444                                           func, data, NULL);
5445
5446   gtk_combo_box_relayout (combo_box);
5447
5448   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5449 }
5450
5451
5452 /**
5453  * gtk_combo_box_set_focus_on_click:
5454  * @combo: a #GtkComboBox
5455  * @focus_on_click: whether the combo box grabs focus when clicked 
5456  *    with the mouse
5457  * 
5458  * Sets whether the combo box will grab focus when it is clicked with 
5459  * the mouse. Making mouse clicks not grab focus is useful in places 
5460  * like toolbars where you don't want the keyboard focus removed from 
5461  * the main area of the application.
5462  *
5463  * Since: 2.6
5464  **/
5465 void
5466 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5467                                   gboolean     focus_on_click)
5468 {
5469   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5470   
5471   focus_on_click = focus_on_click != FALSE;
5472
5473   if (combo_box->priv->focus_on_click != focus_on_click)
5474     {
5475       combo_box->priv->focus_on_click = focus_on_click;
5476
5477       if (combo_box->priv->button)
5478         gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5479                                        focus_on_click);
5480       
5481       g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5482     }
5483 }
5484
5485 /**
5486  * gtk_combo_box_get_focus_on_click:
5487  * @combo: a #GtkComboBox
5488  * 
5489  * Returns whether the combo box grabs focus when it is clicked 
5490  * with the mouse. See gtk_combo_box_set_focus_on_click().
5491  *
5492  * Return value: %TRUE if the combo box grabs focus when it is 
5493  *     clicked with the mouse.
5494  *
5495  * Since: 2.6
5496  **/
5497 gboolean
5498 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5499 {
5500   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5501   
5502   return combo_box->priv->focus_on_click;
5503 }
5504
5505
5506 #define __GTK_COMBO_BOX_C__
5507 #include "gtkaliasdef.c"