]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Set names on the popup widgets to make them themable. (#414975, Benjamin
[~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           gtk_widget_set_name (combo_box->priv->popup_window, "gtk-combobox-popup-window");
1266
1267           gtk_window_set_type_hint (GTK_WINDOW (combo_box->priv->popup_window),
1268                                     GDK_WINDOW_TYPE_HINT_COMBO);
1269
1270           g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"show",
1271                             G_CALLBACK (gtk_combo_box_child_show),
1272                             combo_box);
1273           g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"hide",
1274                             G_CALLBACK (gtk_combo_box_child_hide),
1275                             combo_box);
1276           
1277           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1278           if (GTK_IS_WINDOW (toplevel))
1279             {
1280               gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
1281                                            GTK_WINDOW (combo_box->priv->popup_window));
1282               gtk_window_set_transient_for (GTK_WINDOW (combo_box->priv->popup_window),
1283                                             GTK_WINDOW (toplevel));
1284             }
1285
1286           gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1287           gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
1288                                  gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1289
1290           combo_box->priv->popup_frame = gtk_frame_new (NULL);
1291           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
1292                                      GTK_SHADOW_NONE);
1293           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
1294                              combo_box->priv->popup_frame);
1295
1296           gtk_widget_show (combo_box->priv->popup_frame);
1297
1298           combo_box->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1299           
1300           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1301                                           GTK_POLICY_NEVER,
1302                                           GTK_POLICY_NEVER);
1303           gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1304                                                GTK_SHADOW_NONE);
1305
1306           gtk_widget_show (combo_box->priv->scrolled_window);
1307           
1308           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
1309                              combo_box->priv->scrolled_window);
1310         }
1311
1312       gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1313                          popup);
1314
1315       gtk_widget_show (popup);
1316       g_object_ref (popup);
1317       combo_box->priv->popup_widget = popup;
1318     }
1319 }
1320
1321 static void
1322 gtk_combo_box_menu_position_below (GtkMenu  *menu,
1323                                    gint     *x,
1324                                    gint     *y,
1325                                    gint     *push_in,
1326                                    gpointer  user_data)
1327 {
1328   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1329   gint sx, sy;
1330   GtkWidget *child;
1331   GtkRequisition req;
1332   GdkScreen *screen;
1333   gint monitor_num;
1334   GdkRectangle monitor;
1335   
1336   /* FIXME: is using the size request here broken? */
1337    child = GTK_BIN (combo_box)->child;
1338    
1339    gdk_window_get_origin (child->window, &sx, &sy);
1340    
1341    if (GTK_WIDGET_NO_WINDOW (child))
1342       {
1343         sx += child->allocation.x;
1344         sy += child->allocation.y;
1345       }
1346
1347    gtk_widget_size_request (GTK_WIDGET (menu), &req);
1348
1349    if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1350      *x = sx;
1351    else
1352      *x = sx + child->allocation.width - req.width;
1353    *y = sy;
1354
1355   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1356   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1357                                                   GTK_WIDGET (combo_box)->window);
1358   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1359   
1360   if (*x < monitor.x)
1361     *x = monitor.x;
1362   else if (*x + req.width > monitor.x + monitor.width)
1363     *x = monitor.x + monitor.width - req.width;
1364   
1365   if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1366     *y += child->allocation.height;
1367   else if (*y - monitor.y >= req.height)
1368     *y -= req.height;
1369   else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y) 
1370     *y += child->allocation.height;
1371   else
1372     *y -= req.height;
1373
1374    *push_in = FALSE;
1375 }
1376
1377 static void
1378 gtk_combo_box_menu_position_over (GtkMenu  *menu,
1379                                   gint     *x,
1380                                   gint     *y,
1381                                   gboolean *push_in,
1382                                   gpointer  user_data)
1383 {
1384   GtkComboBox *combo_box;
1385   GtkWidget *active;
1386   GtkWidget *child;
1387   GtkWidget *widget;
1388   GtkRequisition requisition;
1389   GList *children;
1390   gint screen_width;
1391   gint menu_xpos;
1392   gint menu_ypos;
1393   gint menu_width;
1394
1395   g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
1396   
1397   combo_box = GTK_COMBO_BOX (user_data);
1398   widget = GTK_WIDGET (combo_box);
1399
1400   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1401   menu_width = requisition.width;
1402
1403   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1404   gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1405
1406   menu_xpos += widget->allocation.x;
1407   menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1408
1409   if (active != NULL)
1410     {
1411       gtk_widget_get_child_requisition (active, &requisition);
1412       menu_ypos -= requisition.height / 2;
1413     }
1414
1415   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1416   while (children)
1417     {
1418       child = children->data;
1419
1420       if (active == child)
1421         break;
1422
1423       if (GTK_WIDGET_VISIBLE (child))
1424         {
1425           gtk_widget_get_child_requisition (child, &requisition);
1426           menu_ypos -= requisition.height;
1427         }
1428
1429       children = children->next;
1430     }
1431
1432   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1433     menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1434
1435   /* Clamp the position on screen */
1436   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1437   
1438   if (menu_xpos < 0)
1439     menu_xpos = 0;
1440   else if ((menu_xpos + menu_width) > screen_width)
1441     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1442
1443   *x = menu_xpos;
1444   *y = menu_ypos;
1445
1446   *push_in = TRUE;
1447 }
1448
1449 static void
1450 gtk_combo_box_menu_position (GtkMenu  *menu,
1451                              gint     *x,
1452                              gint     *y,
1453                              gint     *push_in,
1454                              gpointer  user_data)
1455 {
1456   GtkComboBox *combo_box;
1457   GtkWidget *menu_item;
1458
1459   combo_box = GTK_COMBO_BOX (user_data);
1460
1461   if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)    
1462     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1463   else
1464     {
1465       /* FIXME handle nested menus better */
1466       menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1467       if (menu_item)
1468         gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget), 
1469                                     menu_item);
1470
1471       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1472     }
1473
1474 }
1475
1476 static void
1477 gtk_combo_box_list_position (GtkComboBox *combo_box, 
1478                              gint        *x, 
1479                              gint        *y, 
1480                              gint        *width,
1481                              gint        *height)
1482 {
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   GtkWidget *sample = GTK_WIDGET (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 = NULL, *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_path_free (ppath);
1793     }
1794   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
1795                                   TRUE);
1796   
1797   /* popup */
1798   gtk_widget_show (combo_box->priv->popup_window);
1799
1800   if (path)
1801     {
1802       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1803                                 path, NULL, FALSE);
1804       gtk_tree_path_free (path);
1805     }
1806
1807   gtk_widget_grab_focus (combo_box->priv->popup_window);
1808   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1809                                 TRUE);
1810
1811   if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1812     gtk_widget_grab_focus (combo_box->priv->tree_view);
1813
1814   if (!popup_grab_on_window (combo_box->priv->popup_window->window,
1815                              GDK_CURRENT_TIME, TRUE))
1816     {
1817       gtk_widget_hide (combo_box->priv->popup_window);
1818       return;
1819     }
1820
1821   gtk_grab_add (combo_box->priv->popup_window);
1822 }
1823
1824 /**
1825  * gtk_combo_box_popdown:
1826  * @combo_box: a #GtkComboBox
1827  * 
1828  * Hides the menu or dropdown list of @combo_box.
1829  *
1830  * This function is mostly intended for use by accessibility technologies;
1831  * applications should have little use for it.
1832  *
1833  * Since: 2.4
1834  **/
1835 void
1836 gtk_combo_box_popdown (GtkComboBox *combo_box)
1837 {
1838   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1839
1840   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1841     {
1842       gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1843       return;
1844     }
1845
1846   if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1847     return;
1848
1849   gtk_grab_remove (combo_box->priv->popup_window);
1850   gtk_widget_hide_all (combo_box->priv->popup_window);
1851   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1852                                 FALSE);
1853 }
1854
1855 static gint
1856 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1857                                     GtkTreePath *path)
1858 {
1859   gint padding;
1860   GtkRequisition req;
1861
1862   if (combo_box->priv->cell_view)
1863     gtk_widget_style_get (combo_box->priv->cell_view,
1864                           "focus-line-width", &padding,
1865                           NULL);
1866   else
1867     padding = 0;
1868
1869   /* add some pixels for good measure */
1870   padding += BONUS_PADDING;
1871
1872   if (combo_box->priv->cell_view)
1873     gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1874                                    path, &req);
1875   else
1876     req.width = 0;
1877
1878   return req.width + padding;
1879 }
1880
1881 static void
1882 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1883 {
1884   GtkTreeIter iter;
1885   GtkTreePath *path;
1886
1887   if (!combo_box->priv->model ||
1888       !gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1889     return;
1890
1891   combo_box->priv->width = 0;
1892   combo_box->priv->height = 0;
1893
1894   path = gtk_tree_path_new_from_indices (0, -1);
1895
1896   do
1897     {
1898       GtkRequisition req;
1899
1900       if (combo_box->priv->cell_view)
1901         gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
1902                                        path, &req);
1903       else
1904         {
1905           req.width = 0;
1906           req.height = 0;
1907         }
1908
1909       combo_box->priv->width = MAX (combo_box->priv->width, req.width);
1910       combo_box->priv->height = MAX (combo_box->priv->height, req.height);
1911
1912       gtk_tree_path_next (path);
1913     }
1914   while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1915
1916   gtk_tree_path_free (path);
1917 }
1918
1919 static void
1920 gtk_combo_box_size_request (GtkWidget      *widget,
1921                             GtkRequisition *requisition)
1922 {
1923   gint width, height;
1924   gint focus_width, focus_pad;
1925   gint font_size;
1926   gint arrow_size;
1927   GtkRequisition bin_req;
1928   PangoContext *context;
1929   PangoFontMetrics *metrics;
1930   PangoFontDescription *font_desc;
1931
1932   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1933  
1934   /* common */
1935   gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1936   gtk_combo_box_remeasure (combo_box);
1937   bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1938   bin_req.height = MAX (bin_req.height, combo_box->priv->height);
1939
1940   gtk_widget_style_get (GTK_WIDGET (widget),
1941                         "focus-line-width", &focus_width,
1942                         "focus-padding", &focus_pad,
1943                         "arrow-size", &arrow_size,
1944                         NULL);
1945
1946   font_desc = GTK_BIN (widget)->child->style->font_desc;
1947   context = gtk_widget_get_pango_context (widget);
1948   metrics = pango_context_get_metrics (context, font_desc,
1949                                        pango_context_get_language (context));
1950   font_size = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
1951                             pango_font_metrics_get_descent (metrics));
1952   pango_font_metrics_unref (metrics);
1953
1954   arrow_size = MAX (arrow_size, font_size);
1955
1956   gtk_widget_set_size_request (combo_box->priv->arrow, arrow_size, arrow_size);
1957
1958   if (!combo_box->priv->tree_view)
1959     {
1960       /* menu mode */
1961
1962       if (combo_box->priv->cell_view)
1963         {
1964           GtkRequisition button_req, sep_req, arrow_req;
1965           gint border_width, xthickness, ythickness;
1966
1967           gtk_widget_size_request (combo_box->priv->button, &button_req);
1968           border_width = GTK_CONTAINER (combo_box)->border_width;
1969           xthickness = combo_box->priv->button->style->xthickness;
1970           ythickness = combo_box->priv->button->style->ythickness;
1971
1972           bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1973           bin_req.height = MAX (bin_req.height, combo_box->priv->height);
1974
1975           gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1976           gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1977
1978           height = MAX (sep_req.height, arrow_req.height);
1979           height = MAX (height, bin_req.height);
1980
1981           width = bin_req.width + sep_req.width + arrow_req.width;
1982
1983           height += 2*(border_width + ythickness + focus_width + focus_pad);
1984           width  += 2*(border_width + xthickness + focus_width + focus_pad);
1985
1986           requisition->width = width;
1987           requisition->height = height;
1988         }
1989       else
1990         {
1991           GtkRequisition but_req;
1992
1993           gtk_widget_size_request (combo_box->priv->button, &but_req);
1994
1995           requisition->width = bin_req.width + but_req.width;
1996           requisition->height = MAX (bin_req.height, but_req.height);
1997         }
1998     }
1999   else
2000     {
2001       /* list mode */
2002       GtkRequisition button_req, frame_req;
2003
2004       /* sample + frame */
2005       *requisition = bin_req;
2006
2007       requisition->width += 2 * focus_width;
2008       
2009       if (combo_box->priv->cell_view_frame)
2010         {
2011           gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
2012           if (combo_box->priv->has_frame)
2013             {
2014               requisition->width += 2 *
2015                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2016                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2017               requisition->height += 2 *
2018                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2019                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2020             }
2021         }
2022
2023       /* the button */
2024       gtk_widget_size_request (combo_box->priv->button, &button_req);
2025
2026       requisition->height = MAX (requisition->height, button_req.height);
2027       requisition->width += button_req.width;
2028     }
2029 }
2030
2031 static void
2032 gtk_combo_box_size_allocate (GtkWidget     *widget,
2033                              GtkAllocation *allocation)
2034 {
2035   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2036   gint focus_width, focus_pad;
2037   GtkAllocation child;
2038   GtkRequisition req;
2039   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2040
2041   widget->allocation = *allocation;
2042
2043   gtk_widget_style_get (GTK_WIDGET (widget),
2044                         "focus-line-width", &focus_width,
2045                         "focus-padding", &focus_pad,
2046                         NULL);
2047
2048   if (!combo_box->priv->tree_view)
2049     {
2050       if (combo_box->priv->cell_view)
2051         {
2052           gint border_width, xthickness, ythickness;
2053           gint width;
2054
2055           /* menu mode */
2056           gtk_widget_size_allocate (combo_box->priv->button, allocation);
2057
2058           /* set some things ready */
2059           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
2060           xthickness = combo_box->priv->button->style->xthickness;
2061           ythickness = combo_box->priv->button->style->ythickness;
2062
2063           child.x = allocation->x;
2064           child.y = allocation->y;
2065           width = allocation->width;
2066           child.height = allocation->height;
2067
2068           if (!combo_box->priv->is_cell_renderer)
2069             {
2070               child.x += border_width + xthickness + focus_width + focus_pad;
2071               child.y += border_width + ythickness + focus_width + focus_pad;
2072               width -= 2 * (child.x - allocation->x);
2073               child.height -= 2 * (child.y - allocation->y);
2074             }
2075
2076
2077           /* handle the children */
2078           gtk_widget_size_request (combo_box->priv->arrow, &req);
2079           child.width = req.width;
2080           if (!is_rtl)
2081             child.x += width - req.width;
2082           child.width = MAX (1, child.width);
2083           child.height = MAX (1, child.height);
2084           gtk_widget_size_allocate (combo_box->priv->arrow, &child);
2085           if (is_rtl)
2086             child.x += req.width;
2087           gtk_widget_size_request (combo_box->priv->separator, &req);
2088           child.width = req.width;
2089           if (!is_rtl)
2090             child.x -= req.width;
2091           child.width = MAX (1, child.width);
2092           child.height = MAX (1, child.height);
2093           gtk_widget_size_allocate (combo_box->priv->separator, &child);
2094
2095           if (is_rtl)
2096             {
2097               child.x += req.width;
2098               child.width = allocation->x + allocation->width 
2099                 - (border_width + xthickness + focus_width + focus_pad) 
2100                 - child.x;
2101             }
2102           else 
2103             {
2104               child.width = child.x;
2105               child.x = allocation->x 
2106                 + border_width + xthickness + focus_width + focus_pad;
2107               child.width -= child.x;
2108             }
2109
2110           child.width = MAX (1, child.width);
2111           child.height = MAX (1, child.height);
2112           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2113         }
2114       else
2115         {
2116           gtk_widget_size_request (combo_box->priv->button, &req);
2117           if (is_rtl)
2118             child.x = allocation->x;
2119           else
2120             child.x = allocation->x + allocation->width - req.width;
2121           child.y = allocation->y;
2122           child.width = req.width;
2123           child.height = allocation->height;
2124           child.width = MAX (1, child.width);
2125           child.height = MAX (1, child.height);
2126           gtk_widget_size_allocate (combo_box->priv->button, &child);
2127
2128           if (is_rtl)
2129             child.x = allocation->x + req.width;
2130           else
2131             child.x = allocation->x;
2132           child.y = allocation->y;
2133           child.width = allocation->width - req.width;
2134           child.width = MAX (1, child.width);
2135           child.height = MAX (1, child.height);
2136           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2137         }
2138     }
2139   else
2140     {
2141       /* list mode */
2142
2143       /* button */
2144       gtk_widget_size_request (combo_box->priv->button, &req);
2145       if (is_rtl)
2146         child.x = allocation->x;
2147       else
2148         child.x = allocation->x + allocation->width - req.width;
2149       child.y = allocation->y;
2150       child.width = req.width;
2151       child.height = allocation->height;
2152       child.width = MAX (1, child.width);
2153       child.height = MAX (1, child.height);
2154       gtk_widget_size_allocate (combo_box->priv->button, &child);
2155
2156       /* frame */
2157       if (is_rtl)
2158         child.x = allocation->x + req.width;
2159       else
2160         child.x = allocation->x;
2161       child.y = allocation->y;
2162       child.width = allocation->width - req.width;
2163       child.height = allocation->height;
2164
2165       if (combo_box->priv->cell_view_frame)
2166         {
2167           child.width = MAX (1, child.width);
2168           child.height = MAX (1, child.height);
2169           gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
2170
2171           /* the sample */
2172           if (combo_box->priv->has_frame)
2173             {
2174               child.x +=
2175                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2176                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
2177               child.y +=
2178                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2179                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
2180               child.width -= 2 * (
2181                                   GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2182                                   GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2183               child.height -= 2 * (
2184                                    GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2185                                    GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2186             }
2187         }
2188       
2189       child.width = MAX (1, child.width);
2190       child.height = MAX (1, child.height);
2191       gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2192     }
2193 }
2194
2195 static void
2196 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2197 {
2198   if (combo_box->priv->model)
2199     {
2200       g_signal_handler_disconnect (combo_box->priv->model,
2201                                    combo_box->priv->inserted_id);
2202       g_signal_handler_disconnect (combo_box->priv->model,
2203                                    combo_box->priv->deleted_id);
2204       g_signal_handler_disconnect (combo_box->priv->model,
2205                                    combo_box->priv->reordered_id);
2206       g_signal_handler_disconnect (combo_box->priv->model,
2207                                    combo_box->priv->changed_id);
2208     }
2209
2210   /* menu mode */
2211   if (!combo_box->priv->tree_view)
2212     {
2213       if (combo_box->priv->popup_widget)
2214         gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
2215                                (GtkCallback)gtk_widget_destroy, NULL);
2216     }
2217
2218   if (combo_box->priv->model)
2219     {
2220       g_object_unref (combo_box->priv->model);
2221       combo_box->priv->model = NULL;
2222     }
2223
2224   if (combo_box->priv->active_row)
2225     {
2226       gtk_tree_row_reference_free (combo_box->priv->active_row);
2227       combo_box->priv->active_row = NULL;
2228     }
2229
2230   if (combo_box->priv->cell_view)
2231     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2232 }
2233
2234 static void
2235 gtk_combo_box_forall (GtkContainer *container,
2236                       gboolean      include_internals,
2237                       GtkCallback   callback,
2238                       gpointer      callback_data)
2239 {
2240   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2241
2242   if (include_internals)
2243     {
2244       if (combo_box->priv->button)
2245         (* callback) (combo_box->priv->button, callback_data);
2246       if (combo_box->priv->cell_view_frame)
2247         (* callback) (combo_box->priv->cell_view_frame, callback_data);
2248     }
2249
2250   if (GTK_BIN (container)->child)
2251     (* callback) (GTK_BIN (container)->child, callback_data);
2252 }
2253
2254 static void 
2255 gtk_combo_box_child_show (GtkWidget *widget,
2256                           GtkComboBox *combo_box)
2257 {
2258   GtkComboBoxPrivate *priv = combo_box->priv;
2259
2260   priv->popup_shown = TRUE;
2261   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2262 }
2263
2264 static void 
2265 gtk_combo_box_child_hide (GtkWidget *widget,
2266                           GtkComboBox *combo_box)
2267 {
2268   GtkComboBoxPrivate *priv = combo_box->priv;
2269
2270   priv->popup_shown = FALSE;
2271   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2272 }
2273
2274 static gboolean
2275 gtk_combo_box_expose_event (GtkWidget      *widget,
2276                             GdkEventExpose *event)
2277 {
2278   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2279
2280   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2281                                   combo_box->priv->button, event);
2282
2283   if (combo_box->priv->tree_view &&
2284       combo_box->priv->cell_view_frame)
2285     {
2286       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2287                                       combo_box->priv->cell_view_frame, event);
2288     }
2289
2290   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2291                                   GTK_BIN (widget)->child, event);
2292
2293   return FALSE;
2294 }
2295
2296 typedef struct {
2297   GtkComboBox *combo;
2298   GtkTreePath *path;
2299   GtkTreeIter iter;
2300   gboolean found;
2301   gboolean set;
2302   gboolean visible;
2303 } SearchData;
2304
2305 static gboolean
2306 path_visible (GtkTreeView *view,
2307               GtkTreePath *path)
2308 {
2309   GtkRBTree *tree;
2310   GtkRBNode *node;
2311   
2312   /* Note that we rely on the fact that collapsed rows don't have nodes 
2313    */
2314   return _gtk_tree_view_find_node (view, path, &tree, &node);
2315 }
2316
2317 static gboolean
2318 tree_next_func (GtkTreeModel *model,
2319                 GtkTreePath  *path,
2320                 GtkTreeIter  *iter,
2321                 gpointer      data)
2322 {
2323   SearchData *search_data = (SearchData *)data;
2324
2325   if (search_data->found) 
2326     {
2327       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2328         return FALSE;
2329       
2330       if (search_data->visible &&
2331           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2332         return FALSE;
2333
2334       search_data->set = TRUE;
2335       search_data->iter = *iter;
2336
2337       return TRUE;
2338     }
2339  
2340   if (gtk_tree_path_compare (path, search_data->path) == 0)
2341     search_data->found = TRUE;
2342   
2343   return FALSE;
2344 }
2345
2346 static gboolean
2347 tree_next (GtkComboBox  *combo,
2348            GtkTreeModel *model,
2349            GtkTreeIter  *iter,
2350            GtkTreeIter  *next,
2351            gboolean      visible)
2352 {
2353   SearchData search_data;
2354
2355   search_data.combo = combo;
2356   search_data.path = gtk_tree_model_get_path (model, iter);
2357   search_data.visible = visible;
2358   search_data.found = FALSE;
2359   search_data.set = FALSE;
2360
2361   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2362   
2363   *next = search_data.iter;
2364
2365   gtk_tree_path_free (search_data.path);
2366
2367   return search_data.set;
2368 }
2369
2370 static gboolean
2371 tree_prev_func (GtkTreeModel *model,
2372                 GtkTreePath  *path,
2373                 GtkTreeIter  *iter,
2374                 gpointer      data)
2375 {
2376   SearchData *search_data = (SearchData *)data;
2377
2378   if (gtk_tree_path_compare (path, search_data->path) == 0)
2379     {
2380       search_data->found = TRUE;
2381       return TRUE;
2382     }
2383   
2384   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2385     return FALSE;
2386       
2387   if (search_data->visible &&
2388       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2389     return FALSE; 
2390   
2391   search_data->set = TRUE;
2392   search_data->iter = *iter;
2393   
2394   return FALSE; 
2395 }
2396
2397 static gboolean
2398 tree_prev (GtkComboBox  *combo,
2399            GtkTreeModel *model,
2400            GtkTreeIter  *iter,
2401            GtkTreeIter  *prev,
2402            gboolean      visible)
2403 {
2404   SearchData search_data;
2405
2406   search_data.combo = combo;
2407   search_data.path = gtk_tree_model_get_path (model, iter);
2408   search_data.visible = visible;
2409   search_data.found = FALSE;
2410   search_data.set = FALSE;
2411
2412   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2413   
2414   *prev = search_data.iter;
2415
2416   gtk_tree_path_free (search_data.path);
2417
2418   return search_data.set;
2419 }
2420
2421 static gboolean
2422 tree_last_func (GtkTreeModel *model,
2423                 GtkTreePath  *path,
2424                 GtkTreeIter  *iter,
2425                 gpointer      data)
2426 {
2427   SearchData *search_data = (SearchData *)data;
2428
2429   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2430     return FALSE;
2431       
2432   /* Note that we rely on the fact that collapsed rows don't have nodes 
2433    */
2434   if (search_data->visible &&
2435       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2436     return FALSE; 
2437   
2438   search_data->set = TRUE;
2439   search_data->iter = *iter;
2440   
2441   return FALSE; 
2442 }
2443
2444 static gboolean
2445 tree_last (GtkComboBox  *combo,
2446            GtkTreeModel *model,
2447            GtkTreeIter  *last,
2448            gboolean      visible)
2449 {
2450   SearchData search_data;
2451
2452   search_data.combo = combo;
2453   search_data.visible = visible;
2454   search_data.set = FALSE;
2455
2456   gtk_tree_model_foreach (model, tree_last_func, &search_data);
2457   
2458   *last = search_data.iter;
2459
2460   return search_data.set;  
2461 }
2462
2463
2464 static gboolean
2465 tree_first_func (GtkTreeModel *model,
2466                  GtkTreePath  *path,
2467                  GtkTreeIter  *iter,
2468                  gpointer      data)
2469 {
2470   SearchData *search_data = (SearchData *)data;
2471
2472   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2473     return FALSE;
2474   
2475   if (search_data->visible &&
2476       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2477     return FALSE;
2478   
2479   search_data->set = TRUE;
2480   search_data->iter = *iter;
2481   
2482   return TRUE;
2483 }
2484
2485 static gboolean
2486 tree_first (GtkComboBox  *combo,
2487             GtkTreeModel *model,
2488             GtkTreeIter  *first,
2489             gboolean      visible)
2490 {
2491   SearchData search_data;
2492   
2493   search_data.combo = combo;
2494   search_data.visible = visible;
2495   search_data.set = FALSE;
2496
2497   gtk_tree_model_foreach (model, tree_first_func, &search_data);
2498   
2499   *first = search_data.iter;
2500
2501   return search_data.set;  
2502 }
2503
2504 static gboolean
2505 gtk_combo_box_scroll_event (GtkWidget          *widget,
2506                             GdkEventScroll     *event)
2507 {
2508   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2509   gboolean found;
2510   GtkTreeIter iter;
2511   GtkTreeIter new_iter;
2512
2513   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2514     return TRUE;
2515   
2516   if (event->direction == GDK_SCROLL_UP)
2517     found = tree_prev (combo_box, combo_box->priv->model, 
2518                        &iter, &new_iter, FALSE);
2519   else
2520     found = tree_next (combo_box, combo_box->priv->model, 
2521                        &iter, &new_iter, FALSE);
2522   
2523   if (found)
2524     gtk_combo_box_set_active_iter (combo_box, &new_iter);
2525
2526   return TRUE;
2527 }
2528
2529 /*
2530  * menu style
2531  */
2532
2533 static void
2534 gtk_combo_box_sync_cells (GtkComboBox   *combo_box,
2535                           GtkCellLayout *cell_layout)
2536 {
2537   GSList *k;
2538
2539   for (k = combo_box->priv->cells; k; k = k->next)
2540     {
2541       GSList *j;
2542       ComboCellInfo *info = (ComboCellInfo *)k->data;
2543
2544       if (info->pack == GTK_PACK_START)
2545         gtk_cell_layout_pack_start (cell_layout,
2546                                     info->cell, info->expand);
2547       else if (info->pack == GTK_PACK_END)
2548         gtk_cell_layout_pack_end (cell_layout,
2549                                   info->cell, info->expand);
2550
2551       gtk_cell_layout_set_cell_data_func (cell_layout,
2552                                           info->cell,
2553                                           combo_cell_data_func, info, NULL);
2554
2555       for (j = info->attributes; j; j = j->next->next)
2556         {
2557           gtk_cell_layout_add_attribute (cell_layout,
2558                                          info->cell,
2559                                          j->data,
2560                                          GPOINTER_TO_INT (j->next->data));
2561         }
2562     }
2563 }
2564
2565 static void
2566 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2567                           gboolean     add_children)
2568 {
2569   GtkWidget *menu;
2570
2571   if (combo_box->priv->cell_view)
2572     {
2573       combo_box->priv->button = gtk_toggle_button_new ();
2574       gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2575                                      combo_box->priv->focus_on_click);
2576
2577       g_signal_connect (combo_box->priv->button, "toggled",
2578                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2579       gtk_widget_set_parent (combo_box->priv->button,
2580                              GTK_BIN (combo_box)->child->parent);
2581
2582       combo_box->priv->box = gtk_hbox_new (FALSE, 0);
2583       gtk_container_add (GTK_CONTAINER (combo_box->priv->button), 
2584                          combo_box->priv->box);
2585
2586       combo_box->priv->separator = gtk_vseparator_new ();
2587       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
2588                          combo_box->priv->separator);
2589
2590       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2591       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
2592                          combo_box->priv->arrow);
2593
2594       gtk_widget_show_all (combo_box->priv->button);
2595     }
2596   else
2597     {
2598       combo_box->priv->button = gtk_toggle_button_new ();
2599       gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2600                                      combo_box->priv->focus_on_click);
2601
2602       g_signal_connect (combo_box->priv->button, "toggled",
2603                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2604       gtk_widget_set_parent (combo_box->priv->button,
2605                              GTK_BIN (combo_box)->child->parent);
2606
2607       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2608       gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2609                          combo_box->priv->arrow);
2610       gtk_widget_show_all (combo_box->priv->button);
2611     }
2612
2613   g_signal_connect (combo_box->priv->button, "button_press_event",
2614                     G_CALLBACK (gtk_combo_box_menu_button_press),
2615                     combo_box);
2616   g_signal_connect (combo_box->priv->button, "state_changed",
2617                     G_CALLBACK (gtk_combo_box_button_state_changed), 
2618                     combo_box);
2619
2620   /* create our funky menu */
2621   menu = gtk_menu_new ();
2622   gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2623   
2624   g_signal_connect (menu, "key_press_event",
2625                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2626   gtk_combo_box_set_popup_widget (combo_box, menu);
2627
2628   /* add items */
2629   if (add_children)
2630     gtk_combo_box_menu_fill (combo_box);
2631
2632   /* the column is needed in tree_column_row_is_sensitive() */
2633   combo_box->priv->column = gtk_tree_view_column_new ();
2634   g_object_ref_sink (combo_box->priv->column);
2635   gtk_combo_box_sync_cells (combo_box, 
2636                             GTK_CELL_LAYOUT (combo_box->priv->column));
2637
2638   gtk_combo_box_update_title (combo_box);
2639 }
2640
2641 static void
2642 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2643 {
2644   GtkWidget *menu;
2645
2646   if (!combo_box->priv->model)
2647     return;
2648
2649   menu = combo_box->priv->popup_widget;
2650
2651   if (combo_box->priv->add_tearoffs)
2652     {
2653       GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2654
2655       gtk_widget_show (tearoff);
2656       
2657       if (combo_box->priv->wrap_width)
2658         gtk_menu_attach (GTK_MENU (menu), tearoff, 
2659                          0, combo_box->priv->wrap_width, 0, 1);
2660       else
2661         gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2662     }
2663   
2664   gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2665 }
2666
2667 static GtkWidget *
2668 gtk_cell_view_menu_item_new (GtkComboBox  *combo_box,
2669                              GtkTreeModel *model,
2670                              GtkTreeIter  *iter)
2671 {
2672   GtkWidget *cell_view; 
2673   GtkWidget *item;
2674   GtkTreePath *path;
2675   GtkRequisition req;
2676
2677   cell_view = gtk_cell_view_new ();
2678   item = gtk_menu_item_new ();
2679   gtk_container_add (GTK_CONTAINER (item), cell_view);
2680
2681   gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);     
2682   path = gtk_tree_model_get_path (model, iter);
2683   gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2684   gtk_tree_path_free (path);
2685
2686   gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2687   gtk_widget_size_request (cell_view, &req);
2688   gtk_widget_show (cell_view);
2689   
2690   return item;
2691 }
2692  
2693 static void
2694 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2695                                GtkWidget   *menu,
2696                                GtkTreeIter *parent)
2697 {
2698   GtkTreeModel *model = combo_box->priv->model;
2699   GtkWidget *item, *submenu, *subitem, *separator;
2700   GtkTreeIter iter;
2701   gboolean is_separator;
2702   gint i, n_children;
2703   GtkWidget *last;
2704   GtkTreePath *path;
2705   
2706   n_children = gtk_tree_model_iter_n_children (model, parent);
2707   
2708   last = NULL;
2709   for (i = 0; i < n_children; i++)
2710     {
2711       gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2712
2713       if (combo_box->priv->row_separator_func)
2714         is_separator = (*combo_box->priv->row_separator_func) (combo_box->priv->model, &iter,
2715                                                                combo_box->priv->row_separator_data);
2716       else
2717         is_separator = FALSE;
2718       
2719       if (is_separator)
2720         {
2721           item = gtk_separator_menu_item_new ();
2722           path = gtk_tree_model_get_path (model, &iter);
2723           g_object_set_data_full (G_OBJECT (item),
2724                                   I_("gtk-combo-box-item-path"),
2725                                   gtk_tree_row_reference_new (model, path),
2726                                   (GDestroyNotify)gtk_tree_row_reference_free);
2727           gtk_tree_path_free (path);
2728         }
2729       else
2730         {
2731           item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2732           if (gtk_tree_model_iter_has_child (model, &iter))
2733             {
2734               submenu = gtk_menu_new ();
2735               gtk_widget_show (submenu);
2736               gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2737               
2738               /* Ugly - since menus can only activate leafs, we have to
2739                * duplicate the item inside the submenu.
2740                */
2741               subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2742               separator = gtk_separator_menu_item_new ();
2743               gtk_widget_show (subitem);
2744               gtk_widget_show (separator);
2745               g_signal_connect (subitem, "activate",
2746                                 G_CALLBACK (gtk_combo_box_menu_item_activate),
2747                                 combo_box);
2748               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2749               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2750               
2751               gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2752             }
2753           g_signal_connect (item, "activate",
2754                             G_CALLBACK (gtk_combo_box_menu_item_activate),
2755                             combo_box);
2756         }
2757       
2758       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2759       if (combo_box->priv->wrap_width && menu == combo_box->priv->popup_widget)
2760         gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2761       gtk_widget_show (item);
2762       
2763       last = item;
2764     }
2765 }
2766
2767 static void
2768 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2769 {
2770   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2771                                         G_SIGNAL_MATCH_DATA,
2772                                         0, 0, NULL,
2773                                         gtk_combo_box_menu_button_press, NULL);
2774   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2775                                         G_SIGNAL_MATCH_DATA,
2776                                         0, 0, NULL,
2777                                         gtk_combo_box_button_state_changed, combo_box);
2778
2779   /* unparent will remove our latest ref */
2780   gtk_widget_unparent (combo_box->priv->button);
2781   
2782   combo_box->priv->box = NULL;
2783   combo_box->priv->button = NULL;
2784   combo_box->priv->arrow = NULL;
2785   combo_box->priv->separator = NULL;
2786
2787   g_object_unref (combo_box->priv->column);
2788   combo_box->priv->column = NULL;
2789
2790   /* changing the popup window will unref the menu and the children */
2791 }
2792
2793 /*
2794  * grid
2795  */
2796
2797 static gboolean
2798 menu_occupied (GtkMenu   *menu,
2799                guint      left_attach,
2800                guint      right_attach,
2801                guint      top_attach,
2802                guint      bottom_attach)
2803 {
2804   GList *i;
2805
2806   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2807     {
2808       guint l, r, b, t;
2809
2810       gtk_container_child_get (GTK_CONTAINER (menu), 
2811                                i->data,
2812                                "left-attach", &l,
2813                                "right-attach", &r,
2814                                "bottom-attach", &b,
2815                                "top-attach", &t,
2816                                NULL);
2817
2818       /* look if this item intersects with the given coordinates */
2819       if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2820         return TRUE;
2821     }
2822
2823   return FALSE;
2824 }
2825
2826 static void
2827 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2828                              GtkWidget   *item,
2829                              GtkTreeIter *iter,
2830                              GtkWidget   *last)
2831 {
2832   gint current_col = 0, current_row = 0;
2833   gint rows = 1, cols = 1;
2834   GtkWidget *menu = combo_box->priv->popup_widget;
2835
2836   if (!GTK_IS_MENU_SHELL (menu))
2837     return;
2838   
2839   if (combo_box->priv->col_column == -1 &&
2840       combo_box->priv->row_column == -1 &&
2841       last)
2842     {
2843       gtk_container_child_get (GTK_CONTAINER (menu), 
2844                                last,
2845                                "right-attach", &current_col,
2846                                "top-attach", &current_row,
2847                                NULL);
2848       if (current_col + cols > combo_box->priv->wrap_width)
2849         {
2850           current_col = 0;
2851           current_row++;
2852         }
2853     }
2854   else
2855     {
2856       if (combo_box->priv->col_column != -1)
2857         gtk_tree_model_get (combo_box->priv->model, iter,
2858                             combo_box->priv->col_column, &cols,
2859                             -1);
2860       if (combo_box->priv->row_column != -1)
2861         gtk_tree_model_get (combo_box->priv->model, iter,
2862                             combo_box->priv->row_column, &rows,
2863                             -1);
2864
2865       while (1)
2866         {
2867           if (current_col + cols > combo_box->priv->wrap_width)
2868             {
2869               current_col = 0;
2870               current_row++;
2871             }
2872           
2873           if (!menu_occupied (GTK_MENU (menu), 
2874                               current_col, current_col + cols,
2875                               current_row, current_row + rows))
2876             break;
2877           
2878           current_col++;
2879         }
2880     }
2881
2882   /* set attach props */
2883   gtk_menu_attach (GTK_MENU (menu), item,
2884                    current_col, current_col + cols,
2885                    current_row, current_row + rows);
2886 }
2887
2888 static void
2889 gtk_combo_box_relayout (GtkComboBox *combo_box)
2890 {
2891   GList *list, *j;
2892   GtkWidget *menu;
2893
2894   menu = combo_box->priv->popup_widget;
2895   
2896   /* do nothing unless we are in menu style and realized */
2897   if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
2898     return;
2899   
2900   list = gtk_container_get_children (GTK_CONTAINER (menu));
2901   
2902   for (j = g_list_last (list); j; j = j->prev)
2903     gtk_container_remove (GTK_CONTAINER (menu), j->data);
2904   
2905   gtk_combo_box_menu_fill (combo_box);
2906
2907   g_list_free (list);
2908 }
2909
2910 /* callbacks */
2911 static gboolean
2912 gtk_combo_box_menu_button_press (GtkWidget      *widget,
2913                                  GdkEventButton *event,
2914                                  gpointer        user_data)
2915 {
2916   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2917
2918   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
2919       event->type == GDK_BUTTON_PRESS && event->button == 1)
2920     {
2921       if (combo_box->priv->focus_on_click && 
2922           !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
2923         gtk_widget_grab_focus (combo_box->priv->button);
2924
2925       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
2926
2927       return TRUE;
2928     }
2929
2930   return FALSE;
2931 }
2932
2933 static void
2934 gtk_combo_box_menu_item_activate (GtkWidget *item,
2935                                   gpointer   user_data)
2936 {
2937   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2938   GtkWidget *cell_view;
2939   GtkTreePath *path;
2940   GtkTreeIter iter;
2941
2942   cell_view = GTK_BIN (item)->child;
2943
2944   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
2945
2946   path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
2947
2948   if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
2949   {
2950     if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
2951       gtk_combo_box_set_active_iter (combo_box, &iter);
2952   }
2953
2954   gtk_tree_path_free (path);
2955
2956   combo_box->priv->editing_canceled = FALSE;
2957 }
2958
2959 static void
2960 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
2961                                   GtkTreePath      *path,
2962                                   GtkTreeIter      *iter,
2963                                   gpointer          user_data)
2964 {
2965   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2966
2967   if (combo_box->priv->tree_view)
2968     gtk_combo_box_list_popup_resize (combo_box);
2969   else
2970     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
2971 }
2972
2973 static void
2974 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
2975                                  GtkTreePath      *path,
2976                                  gpointer          user_data)
2977 {
2978   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2979
2980   if (combo_box->priv->cell_view)
2981     {
2982       if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
2983         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2984     }
2985   
2986   if (combo_box->priv->tree_view)
2987     gtk_combo_box_list_popup_resize (combo_box);
2988   else
2989     gtk_combo_box_menu_row_deleted (model, path, user_data);  
2990 }
2991
2992 static void
2993 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
2994                                     GtkTreePath     *path,
2995                                     GtkTreeIter     *iter,
2996                                     gint            *new_order,
2997                                     gpointer         user_data)
2998 {
2999   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3000
3001   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3002
3003   if (!combo_box->priv->tree_view)
3004     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3005 }
3006                                                     
3007 static void
3008 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
3009                                  GtkTreePath      *path,
3010                                  GtkTreeIter      *iter,
3011                                  gpointer          user_data)
3012 {
3013   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3014   GtkTreePath *active_path;
3015
3016   /* FIXME this belongs to GtkCellView */
3017   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3018     {
3019       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3020       if (gtk_tree_path_compare (path, active_path) == 0 &&
3021           combo_box->priv->cell_view)
3022         gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
3023       gtk_tree_path_free (active_path);
3024     }
3025       
3026   if (combo_box->priv->tree_view)
3027     gtk_combo_box_list_row_changed (model, path, iter, user_data);
3028   else
3029     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3030 }
3031
3032 static gboolean
3033 list_popup_resize_idle (gpointer user_data)
3034 {
3035   GtkComboBox *combo_box;
3036   gint x, y, width, height;
3037
3038   combo_box = GTK_COMBO_BOX (user_data);
3039
3040   if (combo_box->priv->tree_view &&
3041       GTK_WIDGET_MAPPED (combo_box->priv->popup_window))
3042     {
3043       gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3044   
3045       gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);
3046       gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
3047     }
3048
3049   combo_box->priv->resize_idle_id = 0;
3050
3051   return FALSE;
3052 }
3053
3054 static void
3055 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3056 {
3057   if (!combo_box->priv->resize_idle_id)
3058     combo_box->priv->resize_idle_id = 
3059       gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3060 }
3061
3062 static void
3063 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
3064                                   GtkTreePath      *path,
3065                                   GtkTreeIter      *iter,
3066                                   gpointer          user_data)
3067 {
3068   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3069   
3070   gtk_combo_box_list_popup_resize (combo_box);
3071 }
3072
3073
3074 static GtkWidget *
3075 find_menu_by_path (GtkWidget   *menu,
3076                    GtkTreePath *path,
3077                    gboolean     skip_first)
3078 {
3079   GList *i, *list;
3080   GtkWidget *item;
3081   GtkWidget *submenu;    
3082   GtkTreeRowReference *mref;
3083   GtkTreePath *mpath;
3084   gboolean skip;
3085
3086   list = gtk_container_get_children (GTK_CONTAINER (menu));
3087   skip = skip_first;
3088   item = NULL;
3089   for (i = list; i; i = i->next)
3090     {
3091       if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3092         {
3093           mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3094           if (!mref)
3095             continue;
3096           else if (!gtk_tree_row_reference_valid (mref))
3097             mpath = NULL;
3098           else
3099             mpath = gtk_tree_row_reference_get_path (mref);
3100         }
3101       else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3102         {
3103           if (skip)
3104             {
3105               skip = FALSE;
3106               continue;
3107             }
3108
3109           mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3110         }
3111       else 
3112         continue;
3113
3114       /* this case is necessary, since the row reference of
3115        * the cell view may already be updated after a deletion
3116        */
3117       if (!mpath)
3118         {
3119           item = i->data;
3120           break;
3121         }
3122       if (gtk_tree_path_compare (mpath, path) == 0)
3123         {
3124           gtk_tree_path_free (mpath);
3125           item = i->data;
3126           break;
3127         }
3128       if (gtk_tree_path_is_ancestor (mpath, path))
3129         {
3130           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3131           if (submenu != NULL)
3132             {
3133               gtk_tree_path_free (mpath);
3134               item = find_menu_by_path (submenu, path, TRUE);
3135               break;
3136             }
3137         }
3138       gtk_tree_path_free (mpath);
3139     }
3140   
3141   g_list_free (list);  
3142
3143   return item;
3144 }
3145
3146 #if 0
3147 static void
3148 dump_menu_tree (GtkWidget   *menu, 
3149                 gint         level)
3150 {
3151   GList *i, *list;
3152   GtkWidget *submenu;    
3153   GtkTreePath *path;
3154
3155   list = gtk_container_get_children (GTK_CONTAINER (menu));
3156   for (i = list; i; i = i->next)
3157     {
3158       if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3159         {
3160           path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3161           g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3162           gtk_tree_path_free (path);
3163
3164           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3165           if (submenu != NULL)
3166             dump_menu_tree (submenu, level + 1);
3167         }
3168     }
3169   
3170   g_list_free (list);  
3171 }
3172 #endif
3173
3174 static void
3175 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3176                                  GtkTreePath  *path,
3177                                  GtkTreeIter  *iter,
3178                                  gpointer      user_data)
3179 {
3180   GtkWidget *parent;
3181   GtkWidget *item, *menu, *separator;
3182   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3183   GtkTreePath *ppath;
3184   GtkTreeIter piter;
3185   gint depth, pos;
3186   gboolean is_separator;
3187
3188   if (!combo_box->priv->popup_widget)
3189     return;
3190
3191   depth = gtk_tree_path_get_depth (path);
3192   pos = gtk_tree_path_get_indices (path)[depth - 1];
3193   if (depth > 1)
3194     {
3195       ppath = gtk_tree_path_copy (path);
3196       gtk_tree_path_up (ppath);
3197       parent = find_menu_by_path (combo_box->priv->popup_widget, ppath, FALSE);
3198       gtk_tree_path_free (ppath);
3199
3200       menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3201       if (!menu)
3202         {
3203           menu = gtk_menu_new ();
3204           gtk_widget_show (menu);
3205           gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3206           
3207           /* Ugly - since menus can only activate leaves, we have to
3208            * duplicate the item inside the submenu.
3209            */
3210           gtk_tree_model_iter_parent (model, &piter, iter);
3211           item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3212           separator = gtk_separator_menu_item_new ();
3213           g_signal_connect (item, "activate",
3214                             G_CALLBACK (gtk_combo_box_menu_item_activate),
3215                             combo_box);
3216           gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3217           gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3218           if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3219             {
3220               gtk_widget_show (item);
3221               gtk_widget_show (separator);
3222             }
3223         }
3224       pos += 2;
3225     }
3226   else
3227     {
3228       menu = combo_box->priv->popup_widget;
3229       if (combo_box->priv->add_tearoffs)
3230         pos += 1;
3231     }
3232   
3233   if (combo_box->priv->row_separator_func)
3234     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3235                                                            combo_box->priv->row_separator_data);
3236   else
3237     is_separator = FALSE;
3238
3239   if (is_separator)
3240     {
3241       item = gtk_separator_menu_item_new ();
3242       g_object_set_data_full (G_OBJECT (item),
3243                               I_("gtk-combo-box-item-path"),
3244                               gtk_tree_row_reference_new (model, path),
3245                               (GDestroyNotify)gtk_tree_row_reference_free);
3246     }
3247   else
3248     {
3249       item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3250       
3251       g_signal_connect (item, "activate",
3252                         G_CALLBACK (gtk_combo_box_menu_item_activate),
3253                         combo_box);
3254     }
3255
3256   gtk_widget_show (item);
3257   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3258 }
3259
3260 static void
3261 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3262                                 GtkTreePath  *path,
3263                                 gpointer      user_data)
3264 {
3265   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3266   GtkWidget *menu;
3267   GtkWidget *item;
3268
3269   if (!combo_box->priv->popup_widget)
3270     return;
3271
3272   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3273   menu = gtk_widget_get_parent (item);
3274   gtk_container_remove (GTK_CONTAINER (menu), item);
3275
3276   if (gtk_tree_path_get_depth (path) > 1)
3277     {
3278       GtkTreePath *parent_path;
3279       GtkTreeIter iter;
3280       GtkWidget *parent;
3281
3282       parent_path = gtk_tree_path_copy (path);
3283       gtk_tree_path_up (parent_path);
3284       gtk_tree_model_get_iter (model, &iter, parent_path);
3285
3286       if (!gtk_tree_model_iter_has_child (model, &iter))
3287         {
3288           parent = find_menu_by_path (combo_box->priv->popup_widget, 
3289                                       parent_path, FALSE);
3290           gtk_menu_item_remove_submenu (GTK_MENU_ITEM (parent));
3291         }
3292     }
3293 }
3294
3295 static void
3296 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
3297                                     GtkTreePath      *path,
3298                                     GtkTreeIter      *iter,
3299                                     gint             *new_order,
3300                                     gpointer          user_data)
3301 {
3302   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3303
3304   gtk_combo_box_relayout (combo_box);
3305 }
3306                                     
3307 static void
3308 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3309                                 GtkTreePath  *path,
3310                                 GtkTreeIter  *iter,
3311                                 gpointer      user_data)
3312 {
3313   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3314   GtkWidget *item;
3315   gint width;
3316   gboolean is_separator;
3317
3318   if (!combo_box->priv->popup_widget)
3319     return;
3320
3321   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3322
3323   if (combo_box->priv->row_separator_func)
3324     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3325                                                            combo_box->priv->row_separator_data);
3326   else
3327     is_separator = FALSE;
3328
3329   if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3330     {
3331       gtk_combo_box_menu_row_deleted (model, path, combo_box);
3332       gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3333     }
3334
3335   if (combo_box->priv->wrap_width
3336       && item->parent == combo_box->priv->popup_widget)
3337     {
3338       GtkWidget *pitem = NULL;
3339       GtkTreePath *prev;
3340
3341       prev = gtk_tree_path_copy (path);
3342
3343       if (gtk_tree_path_prev (prev))
3344         pitem = find_menu_by_path (combo_box->priv->popup_widget, prev, FALSE);
3345
3346       gtk_tree_path_free (prev);
3347
3348       /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3349          move it */
3350       gtk_container_child_set (GTK_CONTAINER (combo_box->priv->popup_widget),
3351                                item, 
3352                                "left-attach", -1, 
3353                                "right-attach", -1,
3354                                "top-attach", -1, 
3355                                "bottom-attach", -1, 
3356                                NULL);
3357
3358       gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3359     }
3360
3361   width = gtk_combo_box_calc_requested_width (combo_box, path);
3362
3363   if (width > combo_box->priv->width)
3364     {
3365       if (combo_box->priv->cell_view)
3366         {
3367           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3368           gtk_widget_queue_resize (combo_box->priv->cell_view);
3369         }
3370       combo_box->priv->width = width;
3371     }
3372 }
3373
3374 /*
3375  * list style
3376  */
3377
3378 static void
3379 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3380 {
3381   GtkTreeSelection *sel;
3382
3383   combo_box->priv->button = gtk_toggle_button_new ();
3384   gtk_widget_set_parent (combo_box->priv->button,
3385                          GTK_BIN (combo_box)->child->parent);
3386   g_signal_connect (combo_box->priv->button, "button_press_event",
3387                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3388   g_signal_connect (combo_box->priv->button, "toggled",
3389                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3390
3391   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3392   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
3393                      combo_box->priv->arrow);
3394   combo_box->priv->separator = NULL;
3395   gtk_widget_show_all (combo_box->priv->button);
3396
3397   if (combo_box->priv->cell_view)
3398     {
3399       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
3400                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3401
3402       combo_box->priv->box = gtk_event_box_new ();
3403       gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->box), 
3404                                         FALSE);
3405
3406       if (combo_box->priv->has_frame)
3407         {
3408           combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
3409           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
3410                                      GTK_SHADOW_IN);
3411         }
3412       else 
3413         {
3414           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3415           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), 
3416                                             FALSE);
3417         }
3418       
3419       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
3420                              GTK_BIN (combo_box)->child->parent);
3421       gtk_container_add (GTK_CONTAINER (combo_box->priv->cell_view_frame),
3422                          combo_box->priv->box);
3423       gtk_widget_show_all (combo_box->priv->cell_view_frame);
3424
3425       g_signal_connect (combo_box->priv->box, "button_press_event",
3426                         G_CALLBACK (gtk_combo_box_list_button_pressed), 
3427                         combo_box);
3428     }
3429
3430   combo_box->priv->tree_view = gtk_tree_view_new ();
3431   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3432   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3433   gtk_tree_selection_set_select_function (sel,
3434                                           gtk_combo_box_list_select_func,
3435                                           NULL, NULL);
3436   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
3437                                      FALSE);
3438   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (combo_box->priv->tree_view),
3439                                      TRUE);
3440   if (combo_box->priv->row_separator_func)
3441     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3442                                           combo_box->priv->row_separator_func, 
3443                                           combo_box->priv->row_separator_data, 
3444                                           NULL);
3445   if (combo_box->priv->model)
3446     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3447                              combo_box->priv->model);
3448     
3449   combo_box->priv->column = gtk_tree_view_column_new ();
3450   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
3451                                combo_box->priv->column);
3452
3453   /* sync up */
3454   gtk_combo_box_sync_cells (combo_box, 
3455                             GTK_CELL_LAYOUT (combo_box->priv->column));
3456
3457   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3458     {
3459       GtkTreePath *path;
3460
3461       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3462       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
3463                                 path, NULL, FALSE);
3464       gtk_tree_path_free (path);
3465     }
3466
3467   /* set sample/popup widgets */
3468   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
3469
3470   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
3471                     G_CALLBACK (gtk_combo_box_list_key_press),
3472                     combo_box);
3473   g_signal_connect (combo_box->priv->tree_view, "enter_notify_event",
3474                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3475                     combo_box);
3476   g_signal_connect (combo_box->priv->tree_view, "row_expanded",
3477                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3478                     combo_box);
3479   g_signal_connect (combo_box->priv->tree_view, "row_collapsed",
3480                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3481                     combo_box);
3482   g_signal_connect (combo_box->priv->popup_window, "button_press_event",
3483                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3484                     combo_box);
3485   g_signal_connect (combo_box->priv->popup_window, "button_release_event",
3486                     G_CALLBACK (gtk_combo_box_list_button_released),
3487                     combo_box);
3488
3489   gtk_widget_show (combo_box->priv->tree_view);
3490 }
3491
3492 static void
3493 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3494 {
3495   /* disconnect signals */
3496   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
3497                                         G_SIGNAL_MATCH_DATA,
3498                                         0, 0, NULL, NULL, combo_box);
3499   g_signal_handlers_disconnect_matched (combo_box->priv->button,
3500                                         G_SIGNAL_MATCH_DATA,
3501                                         0, 0, NULL,
3502                                         gtk_combo_box_list_button_pressed,
3503                                         NULL);
3504   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3505                                         G_SIGNAL_MATCH_DATA,
3506                                         0, 0, NULL,
3507                                         gtk_combo_box_list_button_pressed,
3508                                         NULL);
3509   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3510                                         G_SIGNAL_MATCH_DATA,
3511                                         0, 0, NULL,
3512                                         gtk_combo_box_list_button_released,
3513                                         NULL);
3514
3515   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3516                                         G_SIGNAL_MATCH_DATA,
3517                                         0, 0, NULL, 
3518                                         gtk_combo_box_child_show,
3519                                         NULL);
3520
3521   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3522                                         G_SIGNAL_MATCH_DATA,
3523                                         0, 0, NULL, 
3524                                         gtk_combo_box_child_hide,
3525                                         NULL);
3526   
3527   if (combo_box->priv->box)
3528     g_signal_handlers_disconnect_matched (combo_box->priv->box,
3529                                           G_SIGNAL_MATCH_DATA,
3530                                           0, 0, NULL,
3531                                           gtk_combo_box_list_button_pressed,
3532                                           NULL);
3533
3534   /* destroy things (unparent will kill the latest ref from us)
3535    * last unref on button will destroy the arrow
3536    */
3537   gtk_widget_unparent (combo_box->priv->button);
3538   combo_box->priv->button = NULL;
3539   combo_box->priv->arrow = NULL;
3540
3541   if (combo_box->priv->cell_view)
3542     {
3543       g_object_set (combo_box->priv->cell_view,
3544                     "background-set", FALSE,
3545                     NULL);
3546     }
3547
3548   if (combo_box->priv->cell_view_frame)
3549     {
3550       gtk_widget_unparent (combo_box->priv->cell_view_frame);
3551       combo_box->priv->cell_view_frame = NULL;
3552       combo_box->priv->box = NULL;
3553     }
3554
3555   if (combo_box->priv->scroll_timer)
3556     {
3557       g_source_remove (combo_box->priv->scroll_timer);
3558       combo_box->priv->scroll_timer = 0;
3559     }
3560
3561   if (combo_box->priv->resize_idle_id)
3562     {
3563       g_source_remove (combo_box->priv->resize_idle_id);
3564       combo_box->priv->resize_idle_id = 0;
3565     }
3566
3567   gtk_widget_destroy (combo_box->priv->tree_view);
3568
3569   combo_box->priv->tree_view = NULL;
3570   if (combo_box->priv->popup_widget)
3571     {
3572       g_object_unref (combo_box->priv->popup_widget);
3573       combo_box->priv->popup_widget = NULL;
3574     }
3575 }
3576
3577 /* callbacks */
3578
3579 static gboolean
3580 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3581                                    GdkEventButton *event,
3582                                    gpointer        data)
3583 {
3584   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3585
3586   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3587
3588   if (ewidget == combo_box->priv->popup_window)
3589     return TRUE;
3590
3591   if ((ewidget != combo_box->priv->button && ewidget != combo_box->priv->box) ||
3592       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3593     return FALSE;
3594
3595   if (combo_box->priv->focus_on_click && 
3596       !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
3597     gtk_widget_grab_focus (combo_box->priv->button);
3598
3599   gtk_combo_box_popup (combo_box);
3600
3601   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
3602                                 TRUE);
3603
3604   combo_box->priv->auto_scroll = FALSE;
3605   if (combo_box->priv->scroll_timer == 0)
3606     combo_box->priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME, 
3607                                                    (GSourceFunc) gtk_combo_box_list_scroll_timeout, 
3608                                                    combo_box);
3609
3610   combo_box->priv->popup_in_progress = TRUE;
3611
3612   return TRUE;
3613 }
3614
3615 static gboolean
3616 gtk_combo_box_list_button_released (GtkWidget      *widget,
3617                                     GdkEventButton *event,
3618                                     gpointer        data)
3619 {
3620   gboolean ret;
3621   GtkTreePath *path = NULL;
3622   GtkTreeIter iter;
3623
3624   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3625
3626   gboolean popup_in_progress = FALSE;
3627
3628   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3629
3630   if (combo_box->priv->popup_in_progress)
3631     {
3632       popup_in_progress = TRUE;
3633       combo_box->priv->popup_in_progress = FALSE;
3634     }
3635
3636   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3637                                   FALSE);
3638   if (combo_box->priv->scroll_timer)
3639     {
3640       g_source_remove (combo_box->priv->scroll_timer);
3641       combo_box->priv->scroll_timer = 0;
3642     }
3643
3644   if (ewidget != combo_box->priv->tree_view)
3645     {
3646       if ((ewidget == combo_box->priv->button || 
3647            ewidget == combo_box->priv->box) &&
3648           !popup_in_progress &&
3649           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3650         {
3651           gtk_combo_box_popdown (combo_box);
3652           return TRUE;
3653         }
3654
3655       /* released outside treeview */
3656       if (ewidget != combo_box->priv->button && 
3657           ewidget != combo_box->priv->box)
3658         {
3659           gtk_combo_box_popdown (combo_box);
3660
3661           return TRUE;
3662         }
3663
3664       return FALSE;
3665     }
3666
3667   /* select something cool */
3668   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (combo_box->priv->tree_view),
3669                                        event->x, event->y,
3670                                        &path,
3671                                        NULL, NULL, NULL);
3672
3673   if (!ret)
3674     return TRUE; /* clicked outside window? */
3675
3676   gtk_tree_model_get_iter (combo_box->priv->model, &iter, path);
3677   gtk_tree_path_free (path);
3678
3679   gtk_combo_box_popdown (combo_box);
3680
3681   if (tree_column_row_is_sensitive (combo_box, &iter))
3682     gtk_combo_box_set_active_iter (combo_box, &iter);
3683
3684   return TRUE;
3685 }
3686
3687 static gboolean
3688 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3689                               GdkEventKey *event,
3690                               gpointer     data)
3691 {
3692   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3693   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3694
3695   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3696       state == GDK_MOD1_MASK)
3697     {
3698       gtk_combo_box_popdown (combo_box);
3699
3700       return TRUE;
3701     }
3702   
3703   return FALSE;
3704 }
3705
3706 static gboolean
3707 gtk_combo_box_list_key_press (GtkWidget   *widget,
3708                               GdkEventKey *event,
3709                               gpointer     data)
3710 {
3711   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3712   GtkTreeIter iter;
3713   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3714
3715   if (event->keyval == GDK_Escape ||
3716       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3717        state == GDK_MOD1_MASK))
3718     {
3719       gtk_combo_box_popdown (combo_box);
3720       
3721       /* reset active item -- this is incredibly lame and ugly */
3722       if (gtk_combo_box_get_active_iter (combo_box, &iter))
3723         gtk_combo_box_set_active_iter (combo_box, &iter);
3724       
3725       return TRUE;
3726     }
3727     
3728   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
3729       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
3730   {
3731     GtkTreeModel *model = NULL;
3732     
3733     gtk_combo_box_popdown (combo_box);
3734     
3735     if (combo_box->priv->model)
3736       {
3737         GtkTreeSelection *sel;
3738
3739         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3740
3741         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3742           gtk_combo_box_set_active_iter (combo_box, &iter);
3743       }
3744     
3745     return TRUE;
3746   }
3747
3748   return FALSE;
3749 }
3750
3751 static void
3752 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3753                                 gint         x, 
3754                                 gint         y)
3755 {
3756   GtkWidget *tree_view = combo_box->priv->tree_view;
3757   GtkAdjustment *adj;
3758   gdouble value;
3759
3760   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3761   if (adj && adj->upper - adj->lower > adj->page_size)
3762     {
3763       if (x <= tree_view->allocation.x && 
3764           adj->lower < adj->value)
3765         {
3766           value = adj->value - (tree_view->allocation.x - x + 1);
3767           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3768         }
3769       else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3770                adj->upper - adj->page_size > adj->value)
3771         {
3772           value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3773           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3774         }
3775     }
3776
3777   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3778   if (adj && adj->upper - adj->lower > adj->page_size)
3779     {
3780       if (y <= tree_view->allocation.y && 
3781           adj->lower < adj->value)
3782         {
3783           value = adj->value - (tree_view->allocation.y - y + 1);
3784           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3785         }
3786       else if (y >= tree_view->allocation.height &&
3787                adj->upper - adj->page_size > adj->value)
3788         {
3789           value = adj->value + (y - tree_view->allocation.height + 1);
3790           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3791         }
3792     }
3793 }
3794
3795 static gboolean
3796 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3797 {
3798   gint x, y;
3799
3800   if (combo_box->priv->auto_scroll)
3801     {
3802       gdk_window_get_pointer (combo_box->priv->tree_view->window, 
3803                               &x, &y, NULL);
3804       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3805     }
3806
3807   return TRUE;
3808 }
3809
3810 static gboolean 
3811 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3812                                  GdkEventCrossing *event,
3813                                  gpointer          data)
3814 {
3815   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3816
3817   combo_box->priv->auto_scroll = TRUE;
3818
3819   return TRUE;
3820 }
3821
3822 static gboolean
3823 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3824                                 GtkTreeModel     *model,
3825                                 GtkTreePath      *path,
3826                                 gboolean          path_currently_selected,
3827                                 gpointer          data)
3828 {
3829   GList *list;
3830   gboolean sensitive = FALSE;
3831
3832   for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
3833     {
3834       GList *cells, *cell;
3835       gboolean cell_sensitive, cell_visible;
3836       GtkTreeIter iter;
3837       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3838
3839       if (!column->visible)
3840         continue;
3841
3842       gtk_tree_model_get_iter (model, &iter, path);
3843       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3844                                                FALSE, FALSE);
3845
3846       cell = cells = gtk_tree_view_column_get_cell_renderers (column);
3847       while (cell)
3848         {
3849           g_object_get (cell->data,
3850                         "sensitive", &cell_sensitive,
3851                         "visible", &cell_visible,
3852                         NULL);
3853
3854           if (cell_visible && cell_sensitive)
3855             break;
3856
3857           cell = cell->next;
3858         }
3859       g_list_free (cells);
3860
3861       sensitive = cell_sensitive;
3862     }
3863
3864   return sensitive;
3865 }
3866
3867 static void
3868 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3869                                 GtkTreePath  *path,
3870                                 GtkTreeIter  *iter,
3871                                 gpointer      data)
3872 {
3873   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3874   gint width;
3875
3876   width = gtk_combo_box_calc_requested_width (combo_box, path);
3877
3878   if (width > combo_box->priv->width)
3879     {
3880       if (combo_box->priv->cell_view) 
3881         {
3882           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3883           gtk_widget_queue_resize (combo_box->priv->cell_view);
3884         }
3885       combo_box->priv->width = width;
3886     }
3887 }
3888
3889 /*
3890  * GtkCellLayout implementation
3891  */
3892
3893 static void
3894 pack_start_recurse (GtkWidget       *menu,
3895                     GtkCellRenderer *cell,
3896                     gboolean         expand)
3897 {
3898   GList *i, *list;
3899   GtkWidget *submenu;    
3900   
3901   list = gtk_container_get_children (GTK_CONTAINER (menu));
3902   for (i = list; i; i = i->next)
3903     {
3904       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3905         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3906                                     cell, expand);
3907
3908       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3909       if (submenu != NULL)
3910         pack_start_recurse (submenu, cell, expand);
3911     }
3912
3913   g_list_free (list);
3914 }
3915
3916 static void
3917 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
3918                                       GtkCellRenderer *cell,
3919                                       gboolean         expand)
3920 {
3921   ComboCellInfo *info;
3922   GtkComboBox *combo_box;
3923
3924   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3925   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3926
3927   combo_box = GTK_COMBO_BOX (layout);
3928
3929   g_object_ref_sink (cell);
3930
3931   info = g_new0 (ComboCellInfo, 1);
3932   info->cell = cell;
3933   info->expand = expand;
3934   info->pack = GTK_PACK_START;
3935
3936   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3937
3938   if (combo_box->priv->cell_view)
3939     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3940                                 cell, expand);
3941
3942   if (combo_box->priv->column)
3943     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
3944
3945   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3946     pack_start_recurse (combo_box->priv->popup_widget, cell, expand);
3947 }
3948
3949 static void
3950 pack_end_recurse (GtkWidget       *menu,
3951                   GtkCellRenderer *cell,
3952                   gboolean         expand)
3953 {
3954   GList *i, *list;
3955   GtkWidget *submenu;    
3956   
3957   list = gtk_container_get_children (GTK_CONTAINER (menu));
3958   for (i = list; i; i = i->next)
3959     {
3960       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3961         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3962                                   cell, expand);
3963
3964       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3965       if (submenu != NULL)
3966         pack_end_recurse (submenu, cell, expand);
3967     }
3968
3969   g_list_free (list);
3970 }
3971
3972 static void
3973 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
3974                                     GtkCellRenderer *cell,
3975                                     gboolean         expand)
3976 {
3977   ComboCellInfo *info;
3978   GtkComboBox *combo_box;
3979
3980   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3981   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3982
3983   combo_box = GTK_COMBO_BOX (layout);
3984
3985   g_object_ref_sink (cell);
3986
3987   info = g_new0 (ComboCellInfo, 1);
3988   info->cell = cell;
3989   info->expand = expand;
3990   info->pack = GTK_PACK_END;
3991
3992   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3993
3994   if (combo_box->priv->cell_view)
3995     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3996                               cell, expand);
3997
3998   if (combo_box->priv->column)
3999     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
4000
4001   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4002     pack_end_recurse (combo_box->priv->popup_widget, cell, expand);
4003 }
4004
4005 static void
4006 clear_recurse (GtkWidget *menu)
4007 {
4008   GList *i, *list;
4009   GtkWidget *submenu;    
4010   
4011   list = gtk_container_get_children (GTK_CONTAINER (menu));
4012   for (i = list; i; i = i->next)
4013     {
4014       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4015         gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child)); 
4016
4017       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4018       if (submenu != NULL)
4019         clear_recurse (submenu);
4020     }
4021
4022   g_list_free (list);
4023 }
4024
4025 static void
4026 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4027 {
4028   GtkComboBox *combo_box;
4029   GSList *i;
4030   
4031   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4032
4033   combo_box = GTK_COMBO_BOX (layout);
4034  
4035   if (combo_box->priv->cell_view)
4036     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
4037
4038   if (combo_box->priv->column)
4039     gtk_tree_view_column_clear (combo_box->priv->column);
4040
4041   for (i = combo_box->priv->cells; i; i = i->next)
4042     {
4043      ComboCellInfo *info = (ComboCellInfo *)i->data;
4044
4045       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4046       g_object_unref (info->cell);
4047       g_free (info);
4048       i->data = NULL;
4049     }
4050   g_slist_free (combo_box->priv->cells);
4051   combo_box->priv->cells = NULL;
4052
4053   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4054     clear_recurse (combo_box->priv->popup_widget);
4055 }
4056
4057 static void
4058 add_attribute_recurse (GtkWidget       *menu,
4059                        GtkCellRenderer *cell,
4060                        const gchar     *attribute,
4061                        gint             column)
4062 {
4063   GList *i, *list;
4064   GtkWidget *submenu;    
4065   
4066   list = gtk_container_get_children (GTK_CONTAINER (menu));
4067   for (i = list; i; i = i->next)
4068     {
4069       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4070         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4071                                        cell, attribute, column); 
4072
4073       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4074       if (submenu != NULL)
4075         add_attribute_recurse (submenu, cell, attribute, column);
4076     }
4077
4078   g_list_free (list);
4079 }
4080                        
4081 static void
4082 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
4083                                          GtkCellRenderer *cell,
4084                                          const gchar     *attribute,
4085                                          gint             column)
4086 {
4087   ComboCellInfo *info;
4088   GtkComboBox *combo_box;
4089
4090   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4091   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4092
4093   combo_box = GTK_COMBO_BOX (layout);
4094
4095   info = gtk_combo_box_get_cell_info (combo_box, cell);
4096
4097   info->attributes = g_slist_prepend (info->attributes,
4098                                       GINT_TO_POINTER (column));
4099   info->attributes = g_slist_prepend (info->attributes,
4100                                       g_strdup (attribute));
4101
4102   if (combo_box->priv->cell_view)
4103     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4104                                    cell, attribute, column);
4105
4106   if (combo_box->priv->column)
4107     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4108                                    cell, attribute, column);
4109
4110   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4111     add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4112   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4113 }
4114
4115 static void
4116 combo_cell_data_func (GtkCellLayout   *cell_layout,
4117                       GtkCellRenderer *cell,
4118                       GtkTreeModel    *tree_model,
4119                       GtkTreeIter     *iter,
4120                       gpointer         data)
4121 {
4122   ComboCellInfo *info = (ComboCellInfo *)data;
4123   GtkWidget *parent = NULL;
4124   
4125   if (!info->func)
4126     return;
4127
4128   (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
4129   
4130   if (GTK_IS_WIDGET (cell_layout))
4131     parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4132   
4133   if (GTK_IS_MENU_ITEM (parent) && 
4134       gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4135     g_object_set (cell, "sensitive", TRUE, NULL);
4136 }
4137
4138
4139 static void 
4140 set_cell_data_func_recurse (GtkWidget             *menu,
4141                             GtkCellRenderer       *cell,
4142                             ComboCellInfo         *info)
4143 {
4144   GList *i, *list;
4145   GtkWidget *submenu;    
4146   GtkWidget *cell_view;
4147   
4148  list = gtk_container_get_children (GTK_CONTAINER (menu));
4149   for (i = list; i; i = i->next)
4150     {
4151       cell_view = GTK_BIN (i->data)->child;
4152       if (GTK_IS_CELL_LAYOUT (cell_view))
4153         {
4154           /* Override sensitivity for inner nodes; we don't
4155            * want menuitems with submenus to appear insensitive 
4156            */ 
4157           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view), 
4158                                               cell, 
4159                                               combo_cell_data_func, 
4160                                               info, NULL); 
4161           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4162           if (submenu != NULL)
4163             set_cell_data_func_recurse (submenu, cell, info);
4164         }
4165     }
4166
4167   g_list_free (list);
4168 }
4169
4170 static void
4171 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4172                                               GtkCellRenderer       *cell,
4173                                               GtkCellLayoutDataFunc  func,
4174                                               gpointer               func_data,
4175                                               GDestroyNotify         destroy)
4176 {
4177   ComboCellInfo *info;
4178   GtkComboBox *combo_box;
4179
4180   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4181
4182   combo_box = GTK_COMBO_BOX (layout);
4183
4184   info = gtk_combo_box_get_cell_info (combo_box, cell);
4185   g_return_if_fail (info != NULL);
4186   
4187   if (info->destroy)
4188     {
4189       GDestroyNotify d = info->destroy;
4190
4191       info->destroy = NULL;
4192       d (info->func_data);
4193     }
4194
4195   info->func = func;
4196   info->func_data = func_data;
4197   info->destroy = destroy;
4198
4199   if (combo_box->priv->cell_view)
4200     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
4201
4202   if (combo_box->priv->column)
4203     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
4204
4205   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4206     set_cell_data_func_recurse (combo_box->priv->popup_widget, cell, info);
4207
4208   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4209 }
4210
4211 static void 
4212 clear_attributes_recurse (GtkWidget             *menu,
4213                           GtkCellRenderer       *cell)
4214 {
4215   GList *i, *list;
4216   GtkWidget *submenu;    
4217   
4218   list = gtk_container_get_children (GTK_CONTAINER (menu));
4219   for (i = list; i; i = i->next)
4220     {
4221       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4222         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4223                                             cell); 
4224       
4225       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4226       if (submenu != NULL)
4227         clear_attributes_recurse (submenu, cell);
4228     }
4229
4230   g_list_free (list);
4231 }
4232
4233 static void
4234 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
4235                                             GtkCellRenderer *cell)
4236 {
4237   ComboCellInfo *info;
4238   GtkComboBox *combo_box;
4239   GSList *list;
4240
4241   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4242   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4243
4244   combo_box = GTK_COMBO_BOX (layout);
4245
4246   info = gtk_combo_box_get_cell_info (combo_box, cell);
4247   g_return_if_fail (info != NULL);
4248
4249   list = info->attributes;
4250   while (list && list->next)
4251     {
4252       g_free (list->data);
4253       list = list->next->next;
4254     }
4255   g_slist_free (info->attributes);
4256   info->attributes = NULL;
4257
4258   if (combo_box->priv->cell_view)
4259     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
4260
4261   if (combo_box->priv->column)
4262     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
4263
4264   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4265     clear_attributes_recurse (combo_box->priv->popup_widget, cell);
4266
4267   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4268 }
4269
4270 static void 
4271 reorder_recurse (GtkWidget             *menu,
4272                  GtkCellRenderer       *cell,
4273                  gint                   position)
4274 {
4275   GList *i, *list;
4276   GtkWidget *submenu;    
4277   
4278   list = gtk_container_get_children (GTK_CONTAINER (menu));
4279   for (i = list; i; i = i->next)
4280     {
4281       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4282         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4283                                  cell, position); 
4284       
4285       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4286       if (submenu != NULL)
4287         reorder_recurse (submenu, cell, position);
4288     }
4289
4290   g_list_free (list);
4291 }
4292
4293 static void
4294 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
4295                                    GtkCellRenderer *cell,
4296                                    gint             position)
4297 {
4298   ComboCellInfo *info;
4299   GtkComboBox *combo_box;
4300   GSList *link;
4301
4302   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4303   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4304
4305   combo_box = GTK_COMBO_BOX (layout);
4306
4307   info = gtk_combo_box_get_cell_info (combo_box, cell);
4308
4309   g_return_if_fail (info != NULL);
4310   g_return_if_fail (position >= 0);
4311
4312   link = g_slist_find (combo_box->priv->cells, info);
4313
4314   g_return_if_fail (link != NULL);
4315
4316   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
4317   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
4318                                            position);
4319
4320   if (combo_box->priv->cell_view)
4321     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4322                              cell, position);
4323
4324   if (combo_box->priv->column)
4325     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
4326                              cell, position);
4327
4328   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4329     reorder_recurse (combo_box->priv->popup_widget, cell, position);
4330
4331   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4332 }
4333
4334 /*
4335  * public API
4336  */
4337
4338 /**
4339  * gtk_combo_box_new:
4340  *
4341  * Creates a new empty #GtkComboBox.
4342  *
4343  * Return value: A new #GtkComboBox.
4344  *
4345  * Since: 2.4
4346  */
4347 GtkWidget *
4348 gtk_combo_box_new (void)
4349 {
4350   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4351 }
4352
4353 /**
4354  * gtk_combo_box_new_with_model:
4355  * @model: A #GtkTreeModel.
4356  *
4357  * Creates a new #GtkComboBox with the model initialized to @model.
4358  *
4359  * Return value: A new #GtkComboBox.
4360  *
4361  * Since: 2.4
4362  */
4363 GtkWidget *
4364 gtk_combo_box_new_with_model (GtkTreeModel *model)
4365 {
4366   GtkComboBox *combo_box;
4367
4368   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4369
4370   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4371
4372   return GTK_WIDGET (combo_box);
4373 }
4374
4375 /**
4376  * gtk_combo_box_get_wrap_width:
4377  * @combo_box: A #GtkComboBox.
4378  *
4379  * Returns the wrap width which is used to determine the number
4380  * of columns for the popup menu. If the wrap width is larger than
4381  * 1, the combo box is in table mode.
4382  *
4383  * Returns: the wrap width.
4384  *
4385  * Since: 2.6
4386  */
4387 gint
4388 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4389 {
4390   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4391
4392   return combo_box->priv->wrap_width;
4393 }
4394
4395 /**
4396  * gtk_combo_box_set_wrap_width:
4397  * @combo_box: A #GtkComboBox.
4398  * @width: Preferred number of columns.
4399  *
4400  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4401  * the preferred number of columns when you want the popup to be layed out
4402  * in a table.
4403  *
4404  * Since: 2.4
4405  */
4406 void
4407 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4408                               gint         width)
4409 {
4410   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4411   g_return_if_fail (width >= 0);
4412
4413   if (width != combo_box->priv->wrap_width)
4414     {
4415       combo_box->priv->wrap_width = width;
4416
4417       gtk_combo_box_check_appearance (combo_box);
4418       gtk_combo_box_relayout (combo_box);
4419       
4420       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4421     }
4422 }
4423
4424 /**
4425  * gtk_combo_box_get_row_span_column:
4426  * @combo_box: A #GtkComboBox.
4427  *
4428  * Returns the column with row span information for @combo_box.
4429  *
4430  * Returns: the row span column.
4431  *
4432  * Since: 2.6
4433  */
4434 gint
4435 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4436 {
4437   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4438
4439   return combo_box->priv->row_column;
4440 }
4441
4442 /**
4443  * gtk_combo_box_set_row_span_column:
4444  * @combo_box: A #GtkComboBox.
4445  * @row_span: A column in the model passed during construction.
4446  *
4447  * Sets the column with row span information for @combo_box to be @row_span.
4448  * The row span column contains integers which indicate how many rows
4449  * an item should span.
4450  *
4451  * Since: 2.4
4452  */
4453 void
4454 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4455                                    gint         row_span)
4456 {
4457   gint col;
4458
4459   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4460
4461   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4462   g_return_if_fail (row_span >= -1 && row_span < col);
4463
4464   if (row_span != combo_box->priv->row_column)
4465     {
4466       combo_box->priv->row_column = row_span;
4467       
4468       gtk_combo_box_relayout (combo_box);
4469  
4470       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4471     }
4472 }
4473
4474 /**
4475  * gtk_combo_box_get_column_span_column:
4476  * @combo_box: A #GtkComboBox.
4477  *
4478  * Returns the column with column span information for @combo_box.
4479  *
4480  * Returns: the column span column.
4481  *
4482  * Since: 2.6
4483  */
4484 gint
4485 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4486 {
4487   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4488
4489   return combo_box->priv->col_column;
4490 }
4491
4492 /**
4493  * gtk_combo_box_set_column_span_column:
4494  * @combo_box: A #GtkComboBox.
4495  * @column_span: A column in the model passed during construction.
4496  *
4497  * Sets the column with column span information for @combo_box to be
4498  * @column_span. The column span column contains integers which indicate
4499  * how many columns an item should span.
4500  *
4501  * Since: 2.4
4502  */
4503 void
4504 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4505                                       gint         column_span)
4506 {
4507   gint col;
4508
4509   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4510
4511   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4512   g_return_if_fail (column_span >= -1 && column_span < col);
4513
4514   if (column_span != combo_box->priv->col_column)
4515     {
4516       combo_box->priv->col_column = column_span;
4517       
4518       gtk_combo_box_relayout (combo_box);
4519
4520       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4521     }
4522 }
4523
4524 /**
4525  * gtk_combo_box_get_active:
4526  * @combo_box: A #GtkComboBox.
4527  *
4528  * Returns the index of the currently active item, or -1 if there's no
4529  * active item. If the model is a non-flat treemodel, and the active item 
4530  * is not an immediate child of the root of the tree, this function returns 
4531  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where 
4532  * <literal>path</literal> is the #GtkTreePath of the active item.
4533  *
4534  * Return value: An integer which is the index of the currently active item, or
4535  * -1 if there's no active item.
4536  *
4537  * Since: 2.4
4538  */
4539 gint
4540 gtk_combo_box_get_active (GtkComboBox *combo_box)
4541 {
4542   gint result;
4543   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4544
4545   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
4546     {
4547       GtkTreePath *path;
4548
4549       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);      
4550       result = gtk_tree_path_get_indices (path)[0];
4551       gtk_tree_path_free (path);
4552     }
4553   else
4554     result = -1;
4555
4556   return result;
4557 }
4558
4559 /**
4560  * gtk_combo_box_set_active:
4561  * @combo_box: A #GtkComboBox.
4562  * @index_: An index in the model passed during construction, or -1 to have
4563  * no active item.
4564  *
4565  * Sets the active item of @combo_box to be the item at @index.
4566  *
4567  * Since: 2.4
4568  */
4569 void
4570 gtk_combo_box_set_active (GtkComboBox *combo_box,
4571                           gint         index_)
4572 {
4573   GtkTreePath *path = NULL;
4574   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4575   g_return_if_fail (index_ >= -1);
4576
4577   if (index_ != -1)
4578     path = gtk_tree_path_new_from_indices (index_, -1);
4579    
4580   gtk_combo_box_set_active_internal (combo_box, path);
4581
4582   if (path)
4583     gtk_tree_path_free (path);
4584 }
4585
4586 static void
4587 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4588                                    GtkTreePath *path)
4589 {
4590   GtkTreePath *active_path;
4591   gint path_cmp;
4592
4593   if (path && gtk_tree_row_reference_valid (combo_box->priv->active_row))
4594     {
4595       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4596       path_cmp = gtk_tree_path_compare (path, active_path);
4597       gtk_tree_path_free (active_path);
4598       if (path_cmp == 0)
4599         return;
4600     }
4601
4602   if (combo_box->priv->active_row)
4603     {
4604       gtk_tree_row_reference_free (combo_box->priv->active_row);
4605       combo_box->priv->active_row = NULL;
4606     }
4607   
4608   if (!path)
4609     {
4610       if (combo_box->priv->tree_view)
4611         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
4612       else
4613         {
4614           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
4615
4616           if (GTK_IS_MENU (menu))
4617             gtk_menu_set_active (menu, -1);
4618         }
4619
4620       if (combo_box->priv->cell_view)
4621         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
4622     }
4623   else
4624     {
4625       combo_box->priv->active_row = 
4626         gtk_tree_row_reference_new (combo_box->priv->model, path);
4627
4628       if (combo_box->priv->tree_view)
4629         {
4630           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), 
4631                                     path, NULL, FALSE);
4632         }
4633       else if (GTK_IS_MENU (combo_box->priv->popup_widget))
4634         {
4635           /* FIXME handle nested menus better */
4636           gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), 
4637                                gtk_tree_path_get_indices (path)[0]);
4638         }
4639
4640       if (combo_box->priv->cell_view)
4641         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
4642                                          path);
4643     }
4644
4645   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4646   g_object_notify (G_OBJECT (combo_box), "active");
4647 }
4648
4649
4650 /**
4651  * gtk_combo_box_get_active_iter:
4652  * @combo_box: A #GtkComboBox
4653  * @iter: The uninitialized #GtkTreeIter.
4654  * 
4655  * Sets @iter to point to the current active item, if it exists.
4656  * 
4657  * Return value: %TRUE, if @iter was set
4658  *
4659  * Since: 2.4
4660  **/
4661 gboolean
4662 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4663                                GtkTreeIter     *iter)
4664 {
4665   GtkTreePath *path;
4666   gboolean result;
4667
4668   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4669
4670   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4671     return FALSE;
4672
4673   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4674   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4675   gtk_tree_path_free (path);
4676
4677   return result;
4678 }
4679
4680 /**
4681  * gtk_combo_box_set_active_iter:
4682  * @combo_box: A #GtkComboBox
4683  * @iter: The #GtkTreeIter.
4684  * 
4685  * Sets the current active item to be the one referenced by @iter. 
4686  * @iter must correspond to a path of depth one.
4687  * 
4688  * Since: 2.4
4689  **/
4690 void
4691 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4692                                GtkTreeIter     *iter)
4693 {
4694   GtkTreePath *path;
4695
4696   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4697
4698   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4699   gtk_combo_box_set_active_internal (combo_box, path);
4700   gtk_tree_path_free (path);
4701 }
4702
4703 /**
4704  * gtk_combo_box_set_model:
4705  * @combo_box: A #GtkComboBox.
4706  * @model: A #GtkTreeModel.
4707  *
4708  * Sets the model used by @combo_box to be @model. Will unset a previously set 
4709  * model (if applicable). If model is %NULL, then it will unset the model.
4710  *
4711  * Note that this function does not clear the cell renderers, you have to 
4712  * call gtk_combo_box_cell_layout_clear() yourself if you need to set up 
4713  * different cell renderers for the new model.
4714  *
4715  * Since: 2.4
4716  */
4717 void
4718 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4719                          GtkTreeModel *model)
4720 {
4721   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4722   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4723
4724   if (model == combo_box->priv->model)
4725     return;
4726   
4727   gtk_combo_box_unset_model (combo_box);
4728
4729   if (model == NULL)
4730     return;
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"