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