]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
gtk/gtkcombobox.c : prototype cell_view_sync_cells() before first usage
[~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 "gtkcellviewmenuitem.h"
29 #include "gtkeventbox.h"
30 #include "gtkframe.h"
31 #include "gtkliststore.h"
32 #include "gtkmain.h"
33 #include "gtkmenu.h"
34 #include "gtktogglebutton.h"
35 #include "gtktreeselection.h"
36 #include "gtkvseparator.h"
37 #include "gtkwindow.h"
38
39 #include <gdk/gdkkeysyms.h>
40
41 #include <gobject/gvaluecollector.h>
42
43 #include <string.h>
44 #include <stdarg.h>
45
46 #include "gtkmarshalers.h"
47 #include "gtkintl.h"
48
49
50 /* WELCOME, to THE house of evil code */
51
52 typedef struct _ComboCellInfo ComboCellInfo;
53 struct _ComboCellInfo
54 {
55   GtkCellRenderer *cell;
56   GSList *attributes;
57
58   GtkCellLayoutDataFunc func;
59   gpointer func_data;
60   GDestroyNotify destroy;
61
62   guint expand : 1;
63   guint pack : 1;
64 };
65
66 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
67
68 struct _GtkComboBoxPrivate
69 {
70   GtkTreeModel *model;
71
72   gint col_column;
73   gint row_column;
74
75   gint wrap_width;
76
77   gint active_item;
78
79   GtkWidget *tree_view;
80   GtkTreeViewColumn *column;
81
82   GtkWidget *cell_view;
83   GtkWidget *cell_view_frame;
84
85   GtkWidget *button;
86   GtkWidget *arrow;
87   GtkWidget *separator;
88
89   GtkWidget *popup_widget;
90   GtkWidget *popup_window;
91   GtkWidget *popup_frame;
92
93   guint inserted_id;
94   guint deleted_id;
95   guint reordered_id;
96   guint changed_id;
97
98   gint width;
99   GSList *cells;
100
101   guint popup_in_progress : 1;
102   guint destroying : 1;
103 };
104
105 /* While debugging this evil code, I have learned that
106  * there are actually 4 modes to this widget, which can
107  * be characterized as follows
108  * 
109  * 1) menu mode, no child added
110  *
111  * tree_view -> NULL
112  * cell_view -> GtkCellView, regular child
113  * cell_view_frame -> NULL
114  * button -> GtkToggleButton set_parent to combo
115  * arrow -> GtkArrow set_parent to button
116  * separator -> GtkVSepator set_parent to button
117  * popup_widget -> GtkMenu
118  * popup_window -> NULL
119  * popup_frame -> NULL
120  *
121  * 2) menu mode, child added
122  * 
123  * tree_view -> NULL
124  * cell_view -> NULL 
125  * cell_view_frame -> NULL
126  * button -> GtkToggleButton set_parent to combo
127  * arrow -> GtkArrow, child of button
128  * separator -> NULL
129  * popup_widget -> GtkMenu
130  * popup_window -> NULL
131  * popup_frame -> NULL
132  *
133  * 3) list mode, no child added
134  * 
135  * tree_view -> GtkTreeView, child of popup_frame
136  * cell_view -> GtkCellView, regular child
137  * cell_view_frame -> GtkFrame, set parent to combo
138  * button -> GtkToggleButton, set_parent to combo
139  * arrow -> GtkArrow, child of button
140  * separator -> NULL
141  * popup_widget -> tree_view
142  * popup_window -> GtkWindow
143  * popup_frame -> GtkFrame, child of popup_window
144  *
145  * 4) list mode, child added
146  *
147  * tree_view -> GtkTreeView, child of popup_frame
148  * cell_view -> NULL
149  * cell_view_frame -> NULL
150  * button -> GtkToggleButton, set_parent to combo
151  * arrow -> GtkArrow, child of button
152  * separator -> NULL
153  * popup_widget -> tree_view
154  * popup_window -> GtkWindow
155  * popup_frame -> GtkFrame, child of popup_window
156  * 
157  */
158
159 enum {
160   CHANGED,
161   LAST_SIGNAL
162 };
163
164 enum {
165   PROP_0,
166   PROP_MODEL,
167   PROP_WRAP_WIDTH,
168   PROP_ROW_SPAN_COLUMN,
169   PROP_COLUMN_SPAN_COLUMN,
170   PROP_ACTIVE
171 };
172
173 static GtkBinClass *parent_class = NULL;
174 static guint combo_box_signals[LAST_SIGNAL] = {0,};
175
176 #define BONUS_PADDING 4
177
178
179 /* common */
180 static void     gtk_combo_box_class_init           (GtkComboBoxClass *klass);
181 static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
182 static void     gtk_combo_box_init                 (GtkComboBox      *combo_box);
183 static void     gtk_combo_box_finalize             (GObject          *object);
184 static void     gtk_combo_box_destroy              (GtkObject        *object);
185
186 static void     gtk_combo_box_set_property         (GObject         *object,
187                                                     guint            prop_id,
188                                                     const GValue    *value,
189                                                     GParamSpec      *spec);
190 static void     gtk_combo_box_get_property         (GObject         *object,
191                                                     guint            prop_id,
192                                                     GValue          *value,
193                                                     GParamSpec      *spec);
194
195 static void     gtk_combo_box_state_changed        (GtkWidget        *widget,
196                                                     GtkStateType      previous);
197 static void     gtk_combo_box_style_set            (GtkWidget       *widget,
198                                                     GtkStyle        *previous);
199 static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
200                                                     gpointer         data);
201 static void     gtk_combo_box_add                  (GtkContainer    *container,
202                                                     GtkWidget       *widget);
203 static void     gtk_combo_box_remove               (GtkContainer    *container,
204                                                     GtkWidget       *widget);
205
206 static ComboCellInfo *gtk_combo_box_get_cell_info  (GtkComboBox      *combo_box,
207                                                     GtkCellRenderer  *cell);
208
209 static void     gtk_combo_box_menu_show            (GtkWidget        *menu,
210                                                     gpointer          user_data);
211 static void     gtk_combo_box_menu_hide            (GtkWidget        *menu,
212                                                     gpointer          user_data);
213
214 static void     gtk_combo_box_set_popup_widget     (GtkComboBox      *combo_box,
215                                                     GtkWidget        *popup);
216 static void     gtk_combo_box_menu_position_below  (GtkMenu          *menu,
217                                                     gint             *x,
218                                                     gint             *y,
219                                                     gint             *push_in,
220                                                     gpointer          user_data);
221 static void     gtk_combo_box_menu_position_over   (GtkMenu          *menu,
222                                                     gint             *x,
223                                                     gint             *y,
224                                                     gint             *push_in,
225                                                     gpointer          user_data);
226 static void     gtk_combo_box_menu_position        (GtkMenu          *menu,
227                                                     gint             *x,
228                                                     gint             *y,
229                                                     gint             *push_in,
230                                                     gpointer          user_data);
231
232 static gint     gtk_combo_box_calc_requested_width (GtkComboBox      *combo_box,
233                                                     GtkTreePath      *path);
234 static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
235
236 static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);
237
238 static void     gtk_combo_box_size_request         (GtkWidget        *widget,
239                                                     GtkRequisition   *requisition);
240 static void     gtk_combo_box_size_allocate        (GtkWidget        *widget,
241                                                     GtkAllocation    *allocation);
242 static void     gtk_combo_box_forall               (GtkContainer     *container,
243                                                     gboolean          include_internals,
244                                                     GtkCallback       callback,
245                                                     gpointer          callback_data);
246 static gboolean gtk_combo_box_expose_event         (GtkWidget        *widget,
247                                                     GdkEventExpose   *event);
248 static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
249                                                     GdkEventScroll   *event);
250 static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
251                                                     gint              index);
252 static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
253                                                     GdkEventKey      *event,
254                                                     gpointer          data);
255
256 /* listening to the model */
257 static void     gtk_combo_box_model_row_inserted   (GtkTreeModel     *model,
258                                                     GtkTreePath      *path,
259                                                     GtkTreeIter      *iter,
260                                                     gpointer          user_data);
261 static void     gtk_combo_box_model_row_deleted    (GtkTreeModel     *model,
262                                                     GtkTreePath      *path,
263                                                     gpointer          user_data);
264 static void     gtk_combo_box_model_rows_reordered (GtkTreeModel     *model,
265                                                     GtkTreePath      *path,
266                                                     GtkTreeIter      *iter,
267                                                     gint             *new_order,
268                                                     gpointer          user_data);
269 static void     gtk_combo_box_model_row_changed    (GtkTreeModel     *model,
270                                                     GtkTreePath      *path,
271                                                     GtkTreeIter      *iter,
272                                                     gpointer          data);
273
274 /* list */
275 static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
276                                                     gint             *x, 
277                                                     gint             *y, 
278                                                     gint             *width,
279                                                     gint             *height);
280 static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
281 static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);
282
283 static void     gtk_combo_box_list_remove_grabs    (GtkComboBox      *combo_box);
284
285 static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
286                                                     GdkEventButton   *event,
287                                                     gpointer          data);
288 static gboolean gtk_combo_box_list_key_press       (GtkWidget        *widget,
289                                                     GdkEventKey      *event,
290                                                     gpointer          data);
291 static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
292                                                     GdkEventButton   *event,
293                                                     gpointer          data);
294
295 static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
296                                                     GtkTreePath      *path,
297                                                     GtkTreeIter      *iter,
298                                                     gpointer          data);
299
300 /* menu */
301 static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
302                                                     gboolean          add_children);
303 static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
304 static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);
305
306 static void     gtk_combo_box_item_get_size        (GtkComboBox      *combo_box,
307                                                     gint              index,
308                                                     gint             *cols,
309                                                     gint             *rows);
310 static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
311                                                     gint              index);
312 static void     gtk_combo_box_relayout             (GtkComboBox      *combo_box);
313
314 static gboolean gtk_combo_box_menu_button_press    (GtkWidget        *widget,
315                                                     GdkEventButton   *event,
316                                                     gpointer          user_data);
317 static void     gtk_combo_box_menu_item_activate   (GtkWidget        *item,
318                                                     gpointer          user_data);
319 static void     gtk_combo_box_menu_row_inserted    (GtkTreeModel     *model,
320                                                     GtkTreePath      *path,
321                                                     GtkTreeIter      *iter,
322                                                     gpointer          user_data);
323 static void     gtk_combo_box_menu_row_deleted     (GtkTreeModel     *model,
324                                                     GtkTreePath      *path,
325                                                     gpointer          user_data);
326 static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
327                                                     GtkTreePath      *path,
328                                                     GtkTreeIter      *iter,
329                                                     gint             *new_order,
330                                                     gpointer          user_data);
331 static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
332                                                     GtkTreePath      *path,
333                                                     GtkTreeIter      *iter,
334                                                     gpointer          data);
335 static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
336                                                     GdkEventKey      *event,
337                                                     gpointer          data);
338
339 /* cell layout */
340 static void     gtk_combo_box_cell_layout_pack_start         (GtkCellLayout         *layout,
341                                                               GtkCellRenderer       *cell,
342                                                               gboolean               expand);
343 static void     gtk_combo_box_cell_layout_pack_end           (GtkCellLayout         *layout,
344                                                               GtkCellRenderer       *cell,
345                                                               gboolean               expand);
346 static void     gtk_combo_box_cell_layout_clear              (GtkCellLayout         *layout);
347 static void     gtk_combo_box_cell_layout_add_attribute      (GtkCellLayout         *layout,
348                                                               GtkCellRenderer       *cell,
349                                                               const gchar           *attribute,
350                                                               gint                   column);
351 static void     gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
352                                                               GtkCellRenderer       *cell,
353                                                               GtkCellLayoutDataFunc  func,
354                                                               gpointer               func_data,
355                                                               GDestroyNotify         destroy);
356 static void     gtk_combo_box_cell_layout_clear_attributes   (GtkCellLayout         *layout,
357                                                               GtkCellRenderer       *cell);
358 static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
359                                                               GtkCellRenderer       *cell,
360                                                               gint                   position);
361 static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
362                                                               gboolean      group_cycling);
363
364 static void     cell_view_sync_cells (GtkComboBox *combo_box,
365                                       GtkCellView *cell_view);
366
367 GType
368 gtk_combo_box_get_type (void)
369 {
370   static GType combo_box_type = 0;
371
372   if (!combo_box_type)
373     {
374       static const GTypeInfo combo_box_info =
375         {
376           sizeof (GtkComboBoxClass),
377           NULL, /* base_init */
378           NULL, /* base_finalize */
379           (GClassInitFunc) gtk_combo_box_class_init,
380           NULL, /* class_finalize */
381           NULL, /* class_data */
382           sizeof (GtkComboBox),
383           0,
384           (GInstanceInitFunc) gtk_combo_box_init
385         };
386
387       static const GInterfaceInfo cell_layout_info =
388         {
389           (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
390           NULL,
391           NULL
392         };
393
394       combo_box_type = g_type_register_static (GTK_TYPE_BIN,
395                                                "GtkComboBox",
396                                                &combo_box_info,
397                                                0);
398
399       g_type_add_interface_static (combo_box_type,
400                                    GTK_TYPE_CELL_LAYOUT,
401                                    &cell_layout_info);
402     }
403
404   return combo_box_type;
405 }
406
407 /* common */
408 static void
409 gtk_combo_box_class_init (GtkComboBoxClass *klass)
410 {
411   GObjectClass *object_class;
412   GtkBindingSet *binding_set;
413   GtkObjectClass *gtk_object_class;
414   GtkContainerClass *container_class;
415   GtkWidgetClass *widget_class;
416
417   binding_set = gtk_binding_set_by_class (klass);
418
419   container_class = (GtkContainerClass *)klass;
420   container_class->forall = gtk_combo_box_forall;
421   container_class->add = gtk_combo_box_add;
422   container_class->remove = gtk_combo_box_remove;
423
424   widget_class = (GtkWidgetClass *)klass;
425   widget_class->size_allocate = gtk_combo_box_size_allocate;
426   widget_class->size_request = gtk_combo_box_size_request;
427   widget_class->expose_event = gtk_combo_box_expose_event;
428   widget_class->scroll_event = gtk_combo_box_scroll_event;
429   widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
430   widget_class->style_set = gtk_combo_box_style_set;
431   widget_class->state_changed = gtk_combo_box_state_changed;
432
433   gtk_object_class = (GtkObjectClass *)klass;
434   gtk_object_class->destroy = gtk_combo_box_destroy;
435
436   object_class = (GObjectClass *)klass;
437   object_class->finalize = gtk_combo_box_finalize;
438   object_class->set_property = gtk_combo_box_set_property;
439   object_class->get_property = gtk_combo_box_get_property;
440
441   parent_class = g_type_class_peek_parent (klass);
442
443   /* signals */
444   combo_box_signals[CHANGED] =
445     g_signal_new ("changed",
446                   G_OBJECT_CLASS_TYPE (klass),
447                   G_SIGNAL_RUN_LAST,
448                   G_STRUCT_OFFSET (GtkComboBoxClass, changed),
449                   NULL, NULL,
450                   g_cclosure_marshal_VOID__VOID,
451                   G_TYPE_NONE, 0);
452
453   /* properties */
454   g_object_class_install_property (object_class,
455                                    PROP_MODEL,
456                                    g_param_spec_object ("model",
457                                                         P_("ComboBox model"),
458                                                         P_("The model for the combo box"),
459                                                         GTK_TYPE_TREE_MODEL,
460                                                         G_PARAM_READWRITE));
461
462   g_object_class_install_property (object_class,
463                                    PROP_WRAP_WIDTH,
464                                    g_param_spec_int ("wrap_width",
465                                                      P_("Wrap width"),
466                                                      P_("Wrap width for layouting the items in a grid"),
467                                                      0,
468                                                      G_MAXINT,
469                                                      0,
470                                                      G_PARAM_READWRITE));
471
472   g_object_class_install_property (object_class,
473                                    PROP_ROW_SPAN_COLUMN,
474                                    g_param_spec_int ("row_span_column",
475                                                      P_("Row span column"),
476                                                      P_("TreeModel column containing the row span values"),
477                                                      0,
478                                                      G_MAXINT,
479                                                      0,
480                                                      G_PARAM_READWRITE));
481
482   g_object_class_install_property (object_class,
483                                    PROP_COLUMN_SPAN_COLUMN,
484                                    g_param_spec_int ("column_span_column",
485                                                      P_("Column span column"),
486                                                      P_("TreeModel column containing the column span values"),
487                                                      0,
488                                                      G_MAXINT,
489                                                      0,
490                                                      G_PARAM_READWRITE));
491
492   g_object_class_install_property (object_class,
493                                    PROP_ACTIVE,
494                                    g_param_spec_int ("active",
495                                                      P_("Active item"),
496                                                      P_("The item which is currently active"),
497                                                      0,
498                                                      G_MAXINT,
499                                                      0,
500                                                      G_PARAM_READWRITE));
501
502   gtk_widget_class_install_style_property (widget_class,
503                                            g_param_spec_boolean ("appears-as-list",
504                                                                  P_("Appears as list"),
505                                                                  P_("Whether combobox dropdowns should look like lists rather than menus"),
506                                                                  FALSE,
507                                                                  G_PARAM_READWRITE));
508
509   g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
510 }
511
512 static void
513 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
514 {
515   iface->pack_start = gtk_combo_box_cell_layout_pack_start;
516   iface->pack_end = gtk_combo_box_cell_layout_pack_end;
517   iface->clear = gtk_combo_box_cell_layout_clear;
518   iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
519   iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
520   iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
521   iface->reorder = gtk_combo_box_cell_layout_reorder;
522 }
523
524 static void
525 gtk_combo_box_init (GtkComboBox *combo_box)
526 {
527   combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
528
529   combo_box->priv->cell_view = gtk_cell_view_new ();
530   gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
531   GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
532   gtk_widget_show (combo_box->priv->cell_view);
533
534   combo_box->priv->width = 0;
535   combo_box->priv->wrap_width = 0;
536
537   combo_box->priv->active_item = -1;
538   combo_box->priv->col_column = -1;
539   combo_box->priv->row_column = -1;
540 }
541
542 static void
543 gtk_combo_box_set_property (GObject      *object,
544                             guint         prop_id,
545                             const GValue *value,
546                             GParamSpec   *pspec)
547 {
548   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
549
550   switch (prop_id)
551     {
552       case PROP_MODEL:
553         gtk_combo_box_set_model (combo_box, g_value_get_object (value));
554         break;
555
556       case PROP_WRAP_WIDTH:
557         gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
558         break;
559
560       case PROP_ROW_SPAN_COLUMN:
561         gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
562         break;
563
564       case PROP_COLUMN_SPAN_COLUMN:
565         gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
566         break;
567
568       case PROP_ACTIVE:
569         gtk_combo_box_set_active (combo_box, g_value_get_int (value));
570         break;
571
572       default:
573         break;
574     }
575 }
576
577 static void
578 gtk_combo_box_get_property (GObject    *object,
579                             guint       prop_id,
580                             GValue     *value,
581                             GParamSpec *pspec)
582 {
583   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
584
585   switch (prop_id)
586     {
587       case PROP_MODEL:
588         g_value_set_object (value, combo_box->priv->model);
589         break;
590
591       case PROP_WRAP_WIDTH:
592         g_value_set_int (value, combo_box->priv->wrap_width);
593         break;
594
595       case PROP_ROW_SPAN_COLUMN:
596         g_value_set_int (value, combo_box->priv->row_column);
597         break;
598
599       case PROP_COLUMN_SPAN_COLUMN:
600         g_value_set_int (value, combo_box->priv->col_column);
601         break;
602
603       case PROP_ACTIVE:
604         g_value_set_int (value, gtk_combo_box_get_active (combo_box));
605         break;
606
607       default:
608         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
609         break;
610     }
611 }
612
613 static void
614 gtk_combo_box_state_changed (GtkWidget    *widget,
615                              GtkStateType  previous)
616 {
617   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
618
619   if (GTK_WIDGET_REALIZED (widget))
620     {
621       if (combo_box->priv->tree_view && combo_box->priv->cell_view)
622         gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
623                                             &widget->style->base[GTK_WIDGET_STATE (widget)]);
624     }
625
626   gtk_widget_queue_draw (widget);
627 }
628
629 static void
630 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
631 {
632   gboolean appears_as_list;
633
634   /* if wrap_width > 0, then we are in grid-mode and forced to use
635    * unix style
636    */
637   if (combo_box->priv->wrap_width)
638     appears_as_list = FALSE;
639   else
640     gtk_widget_style_get (GTK_WIDGET (combo_box),
641                           "appears-as-list", &appears_as_list,
642                           NULL);
643
644   if (appears_as_list)
645     {
646       /* Destroy all the menu mode widgets, if they exist. */
647       if (GTK_IS_MENU (combo_box->priv->popup_widget))
648         gtk_combo_box_menu_destroy (combo_box);
649
650       /* Create the list mode widgets, if they don't already exist. */
651       if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
652         gtk_combo_box_list_setup (combo_box);
653     }
654   else
655     {
656       /* Destroy all the list mode widgets, if they exist. */
657       if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
658         gtk_combo_box_list_destroy (combo_box);
659
660       /* Create the menu mode widgets, if they don't already exist. */
661       if (!GTK_IS_MENU (combo_box->priv->popup_widget))
662         gtk_combo_box_menu_setup (combo_box, TRUE);
663     }
664 }
665
666 static void
667 gtk_combo_box_style_set (GtkWidget *widget,
668                          GtkStyle  *previous)
669 {
670   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
671
672   gtk_combo_box_check_appearance (combo_box);
673
674   if (combo_box->priv->tree_view && combo_box->priv->cell_view)
675     gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
676                                         &widget->style->base[GTK_WIDGET_STATE (widget)]);
677 }
678
679 static void
680 gtk_combo_box_button_toggled (GtkWidget *widget,
681                               gpointer   data)
682 {
683   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
684
685   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
686     {
687       if (!combo_box->priv->popup_in_progress)
688         gtk_combo_box_popup (combo_box);
689     }
690   else
691     gtk_combo_box_popdown (combo_box);
692 }
693
694 static void
695 gtk_combo_box_add (GtkContainer *container,
696                    GtkWidget    *widget)
697 {
698   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
699
700   if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
701     {
702       gtk_widget_unparent (combo_box->priv->cell_view);
703       GTK_BIN (container)->child = NULL;
704       gtk_widget_queue_resize (GTK_WIDGET (container));
705     }
706   
707   gtk_widget_set_parent (widget, GTK_WIDGET (container));
708   GTK_BIN (container)->child = widget;
709
710   if (combo_box->priv->cell_view &&
711       widget != combo_box->priv->cell_view)
712     {
713       /* since the cell_view was unparented, it's gone now */
714       combo_box->priv->cell_view = NULL;
715
716       if (!combo_box->priv->tree_view && combo_box->priv->separator)
717         {
718           gtk_widget_unparent (combo_box->priv->separator);
719           combo_box->priv->separator = NULL;
720
721           g_object_ref (G_OBJECT (combo_box->priv->arrow));
722           gtk_widget_unparent (combo_box->priv->arrow);
723           gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
724                              combo_box->priv->arrow);
725           g_object_unref (G_OBJECT (combo_box->priv->arrow));
726
727           gtk_widget_queue_resize (GTK_WIDGET (container));
728         }
729       else if (combo_box->priv->cell_view_frame)
730         {
731           gtk_widget_unparent (combo_box->priv->cell_view_frame);
732           combo_box->priv->cell_view_frame = NULL;
733         }
734     }
735 }
736
737 static void
738 gtk_combo_box_remove (GtkContainer *container,
739                       GtkWidget    *widget)
740 {
741   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
742   gboolean appears_as_list;
743
744   gtk_widget_unparent (widget);
745   GTK_BIN (container)->child = NULL;
746
747   if (combo_box->priv->destroying)
748     return;
749
750   gtk_widget_queue_resize (GTK_WIDGET (container));
751
752   if (!combo_box->priv->tree_view)
753     appears_as_list = FALSE;
754   else
755     appears_as_list = TRUE;
756   
757   if (appears_as_list)
758     gtk_combo_box_list_destroy (combo_box);
759   else if (GTK_IS_MENU (combo_box->priv->popup_widget))
760     {
761       gtk_combo_box_menu_destroy (combo_box);
762       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
763       combo_box->priv->popup_widget = NULL;
764     }
765
766   if (!combo_box->priv->cell_view)
767     {
768       combo_box->priv->cell_view = gtk_cell_view_new ();
769       gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
770       GTK_BIN (container)->child = combo_box->priv->cell_view;
771       
772       gtk_widget_show (combo_box->priv->cell_view);
773       gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
774                                combo_box->priv->model);
775       cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
776     }
777
778
779   if (appears_as_list)
780     gtk_combo_box_list_setup (combo_box);
781   else
782     gtk_combo_box_menu_setup (combo_box, TRUE);
783
784   gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
785 }
786
787 static ComboCellInfo *
788 gtk_combo_box_get_cell_info (GtkComboBox     *combo_box,
789                              GtkCellRenderer *cell)
790 {
791   GSList *i;
792
793   for (i = combo_box->priv->cells; i; i = i->next)
794     {
795       ComboCellInfo *info = (ComboCellInfo *)i->data;
796
797       if (info && info->cell == cell)
798         return info;
799     }
800
801   return NULL;
802 }
803
804 static void
805 gtk_combo_box_menu_show (GtkWidget *menu,
806                          gpointer   user_data)
807 {
808   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
809
810   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
811                                 TRUE);
812   combo_box->priv->popup_in_progress = FALSE;
813 }
814
815 static void
816 gtk_combo_box_menu_hide (GtkWidget *menu,
817                          gpointer   user_data)
818 {
819   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
820
821   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
822                                 FALSE);
823 }
824
825 static void
826 gtk_combo_box_detacher (GtkWidget *widget,
827                         GtkMenu   *menu)
828 {
829   GtkComboBox *combo_box;
830
831   g_return_if_fail (GTK_IS_COMBO_BOX (widget));
832
833   combo_box = GTK_COMBO_BOX (widget);
834   g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
835
836   g_signal_handlers_disconnect_by_func (menu,
837                                         gtk_combo_box_menu_show,
838                                         combo_box);
839   g_signal_handlers_disconnect_by_func (menu,
840                                         gtk_combo_box_menu_hide,
841                                         combo_box);
842   
843   combo_box->priv->popup_widget = NULL;
844 }
845
846 static void
847 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
848                                 GtkWidget   *popup)
849 {
850   if (GTK_IS_MENU (combo_box->priv->popup_widget))
851     {
852       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
853       combo_box->priv->popup_widget = NULL;
854     }
855   else if (combo_box->priv->popup_widget)
856     {
857       gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
858                             combo_box->priv->popup_widget);
859       g_object_unref (G_OBJECT (combo_box->priv->popup_widget));
860       combo_box->priv->popup_widget = NULL;
861     }
862
863   if (GTK_IS_MENU (popup))
864     {
865       if (combo_box->priv->popup_window)
866         {
867           gtk_widget_destroy (combo_box->priv->popup_window);
868           combo_box->priv->popup_window = NULL;
869           combo_box->priv->popup_frame = NULL;
870         }
871
872       combo_box->priv->popup_widget = popup;
873
874       g_signal_connect (popup, "show",
875                         G_CALLBACK (gtk_combo_box_menu_show), combo_box);
876       g_signal_connect (popup, "hide",
877                         G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
878
879       gtk_menu_attach_to_widget (GTK_MENU (popup),
880                                  GTK_WIDGET (combo_box),
881                                  gtk_combo_box_detacher);
882     }
883   else
884     {
885       if (!combo_box->priv->popup_window)
886         {
887           combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
888           gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
889           gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
890                                  gtk_widget_get_screen (GTK_WIDGET (combo_box)));
891
892           combo_box->priv->popup_frame = gtk_frame_new (NULL);
893           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
894                                      GTK_SHADOW_ETCHED_IN);
895           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
896                              combo_box->priv->popup_frame);
897
898           gtk_widget_show (combo_box->priv->popup_frame);
899         }
900
901       gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
902                          popup);
903       gtk_widget_show (popup);
904       g_object_ref (G_OBJECT (popup));
905       combo_box->priv->popup_widget = popup;
906     }
907 }
908
909 static void
910 gtk_combo_box_menu_position_below (GtkMenu  *menu,
911                                    gint     *x,
912                                    gint     *y,
913                                    gint     *push_in,
914                                    gpointer  user_data)
915 {
916   gint sx, sy;
917   GtkWidget *child;
918   GtkRequisition req;
919   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
920   
921   /* FIXME: is using the size request here broken? */
922    child = GTK_BIN (combo_box)->child;
923    
924    gdk_window_get_origin (child->window, &sx, &sy);
925    
926    gtk_widget_size_request (GTK_WIDGET (menu), &req);
927    
928    if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
929      *x = sx;
930    else
931      *x = sx + child->allocation.width - req.width;
932    *y = sy + child->allocation.height;
933    
934    if (GTK_WIDGET_NO_WINDOW (child))
935       {
936         *x += child->allocation.x;
937         *y += child->allocation.y;
938       }
939    
940    *push_in = TRUE;
941 }
942
943 static void
944 gtk_combo_box_menu_position_over (GtkMenu  *menu,
945                                   gint     *x,
946                                   gint     *y,
947                                   gboolean *push_in,
948                                   gpointer  user_data)
949 {
950   GtkComboBox *combo_box;
951   GtkWidget *active;
952   GtkWidget *child;
953   GtkWidget *widget;
954   GtkRequisition requisition;
955   GList *children;
956   gint screen_width;
957   gint menu_xpos;
958   gint menu_ypos;
959   gint menu_width;
960
961   g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
962   
963   combo_box = GTK_COMBO_BOX (user_data);
964   widget = GTK_WIDGET (combo_box);
965
966   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
967   menu_width = requisition.width;
968
969   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
970   gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
971
972   menu_xpos += widget->allocation.x;
973   menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
974
975   if (active != NULL)
976     {
977       gtk_widget_get_child_requisition (active, &requisition);
978       menu_ypos -= requisition.height / 2;
979     }
980
981   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
982   while (children)
983     {
984       child = children->data;
985
986       if (active == child)
987         break;
988
989       if (GTK_WIDGET_VISIBLE (child))
990         {
991           gtk_widget_get_child_requisition (child, &requisition);
992           menu_ypos -= requisition.height;
993         }
994
995       children = children->next;
996     }
997
998   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
999     menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1000
1001   /* Clamp the position on screen */
1002   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1003   
1004   if (menu_xpos < 0)
1005     menu_xpos = 0;
1006   else if ((menu_xpos + menu_width) > screen_width)
1007     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1008
1009   *x = menu_xpos;
1010   *y = menu_ypos;
1011
1012   *push_in = TRUE;
1013 }
1014
1015 static void
1016 gtk_combo_box_menu_position (GtkMenu  *menu,
1017                              gint     *x,
1018                              gint     *y,
1019                              gint     *push_in,
1020                              gpointer  user_data)
1021 {
1022   GtkComboBox *combo_box;
1023   GtkWidget *menu_item;
1024
1025   combo_box = GTK_COMBO_BOX (user_data);
1026
1027   if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)    
1028     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1029   else
1030     {
1031       menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1032       if (menu_item)
1033         gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget), 
1034                                     menu_item);
1035
1036       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1037     }
1038
1039 }
1040
1041 static void
1042 gtk_combo_box_list_position (GtkComboBox *combo_box, 
1043                              gint        *x, 
1044                              gint        *y, 
1045                              gint        *width,
1046                              gint        *height)
1047 {
1048   GtkWidget *sample;
1049   GdkScreen *screen;
1050   gint monitor_num;
1051   GdkRectangle monitor;
1052   GtkRequisition popup_req;
1053   
1054   sample = GTK_BIN (combo_box)->child;
1055
1056   *width = sample->allocation.width;
1057   gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
1058   *height = popup_req.height;
1059
1060   gdk_window_get_origin (sample->window, x, y);
1061
1062   if (combo_box->priv->cell_view_frame)
1063     {
1064        *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1065              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1066        *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1067             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1068     }
1069
1070   if (GTK_WIDGET_NO_WINDOW (sample))
1071     {
1072       *x += sample->allocation.x;
1073       *y += sample->allocation.y;
1074     }
1075   
1076   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1077   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1078                                                   GTK_WIDGET (combo_box)->window);
1079   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1080   
1081   if (*x < monitor.x)
1082     *x = monitor.x;
1083   else if (*x + *width > monitor.x + monitor.width)
1084     *x = monitor.x + monitor.width - *width;
1085   
1086   if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1087     *y += sample->allocation.height;
1088   else
1089     *y -= *height;
1090
1091
1092 /**
1093  * gtk_combo_box_popup:
1094  * @combo_box: a #GtkComboBox
1095  * 
1096  * Pops up the menu or dropdown list of @combo_box. 
1097  *
1098  * This function is mostly intended for use by accessibility technologies;
1099  * applications should have little use for it.
1100  *
1101  * Since: 2.4
1102  **/
1103 void
1104 gtk_combo_box_popup (GtkComboBox *combo_box)
1105 {
1106   gint x, y, width, height;
1107   
1108   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1109
1110   if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1111     return;
1112
1113   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1114     {
1115       gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1116                            combo_box->priv->active_item);
1117
1118       gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1119                       NULL, NULL,
1120                       gtk_combo_box_menu_position, combo_box,
1121                       0, 0);
1122       return;
1123     }
1124
1125   gtk_widget_show_all (combo_box->priv->popup_frame);
1126   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1127
1128   gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);  
1129   gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1130
1131   /* popup */
1132   gtk_widget_show (combo_box->priv->popup_window);
1133
1134   gtk_widget_grab_focus (combo_box->priv->popup_window);
1135   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1136                                 TRUE);
1137
1138   if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1139     {
1140       gdk_keyboard_grab (combo_box->priv->popup_window->window,
1141                          FALSE, GDK_CURRENT_TIME);
1142       gtk_widget_grab_focus (combo_box->priv->tree_view);
1143     }
1144
1145   gtk_grab_add (combo_box->priv->popup_window);
1146   gdk_pointer_grab (combo_box->priv->popup_window->window, TRUE,
1147                     GDK_BUTTON_PRESS_MASK |
1148                     GDK_BUTTON_RELEASE_MASK |
1149                     GDK_POINTER_MOTION_MASK,
1150                     NULL, NULL, GDK_CURRENT_TIME);
1151
1152   gtk_grab_add (combo_box->priv->tree_view);
1153 }
1154
1155 /**
1156  * gtk_combo_box_popdown:
1157  * @combo_box: a #GtkComboBox
1158  * 
1159  * Hides the menu or dropdown list of @combo_box.
1160  *
1161  * This function is mostly intended for use by accessibility technologies;
1162  * applications should have little use for it.
1163  *
1164  * Since: 2.4
1165  **/
1166 void
1167 gtk_combo_box_popdown (GtkComboBox *combo_box)
1168 {
1169   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1170
1171   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1172     {
1173       gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1174       return;
1175     }
1176
1177   gtk_combo_box_list_remove_grabs (combo_box);
1178   gtk_widget_hide_all (combo_box->priv->popup_window);
1179   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1180                                 FALSE);
1181 }
1182
1183 static gint
1184 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1185                                     GtkTreePath *path)
1186 {
1187   gint padding;
1188   GtkRequisition req;
1189
1190   if (combo_box->priv->cell_view)
1191     gtk_widget_style_get (combo_box->priv->cell_view,
1192                           "focus-line-width", &padding,
1193                           NULL);
1194   else
1195     padding = 0;
1196
1197   /* add some pixels for good measure */
1198   padding += BONUS_PADDING;
1199
1200   if (combo_box->priv->cell_view)
1201     gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1202                                    path, &req);
1203   else
1204     req.width = 0;
1205
1206   return req.width + padding;
1207 }
1208
1209 static void
1210 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1211 {
1212   GtkTreeIter iter;
1213   GtkTreePath *path;
1214   gint padding = 0;
1215
1216   if (!gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1217     return;
1218
1219   combo_box->priv->width = 0;
1220
1221   path = gtk_tree_path_new_from_indices (0, -1);
1222
1223   if (combo_box->priv->cell_view)
1224     gtk_widget_style_get (combo_box->priv->cell_view,
1225                           "focus-line-width", &padding,
1226                           NULL);
1227   else
1228     padding = 0;
1229
1230   /* add some pixels for good measure */
1231   padding += BONUS_PADDING;
1232
1233   do
1234     {
1235       GtkRequisition req;
1236
1237       if (combo_box->priv->cell_view)
1238         gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
1239                                        path, &req);
1240       else
1241         req.width = 0;
1242
1243       combo_box->priv->width = MAX (combo_box->priv->width,
1244                                     req.width + padding);
1245
1246       gtk_tree_path_next (path);
1247     }
1248   while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1249
1250   gtk_tree_path_free (path);
1251 }
1252
1253 static void
1254 gtk_combo_box_size_request (GtkWidget      *widget,
1255                             GtkRequisition *requisition)
1256 {
1257   gint width, height;
1258   GtkRequisition bin_req;
1259
1260   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1261
1262   /* common */
1263   gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1264   gtk_combo_box_remeasure (combo_box);
1265   bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1266
1267   if (!combo_box->priv->tree_view)
1268     {
1269       /* menu mode */
1270
1271       if (combo_box->priv->cell_view)
1272         {
1273           GtkRequisition button_req, sep_req, arrow_req;
1274           gint border_width, xthickness, ythickness;
1275
1276           gtk_widget_size_request (combo_box->priv->button, &button_req);
1277           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1278           xthickness = combo_box->priv->button->style->xthickness;
1279           ythickness = combo_box->priv->button->style->ythickness;
1280
1281           bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1282
1283           gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1284           gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1285
1286           height = MAX (sep_req.height, arrow_req.height);
1287           height = MAX (height, bin_req.height);
1288
1289           width = bin_req.width + sep_req.width + arrow_req.width;
1290
1291           height += border_width + 1 + ythickness * 2 + 4;
1292           width += border_width + 1 + xthickness * 2 + 4;
1293
1294           requisition->width = width;
1295           requisition->height = height;
1296         }
1297       else
1298         {
1299           GtkRequisition but_req;
1300
1301           gtk_widget_size_request (combo_box->priv->button, &but_req);
1302
1303           requisition->width = bin_req.width + but_req.width;
1304           requisition->height = MAX (bin_req.height, but_req.height);
1305         }
1306     }
1307   else
1308     {
1309       /* list mode */
1310       GtkRequisition button_req, frame_req;
1311
1312       /* sample + frame */
1313       *requisition = bin_req;
1314
1315       if (combo_box->priv->cell_view_frame)
1316         {
1317           gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
1318           requisition->width += 2 *
1319             (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1320              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1321           requisition->height += 2 *
1322             (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1323              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1324         }
1325
1326       /* the button */
1327       gtk_widget_size_request (combo_box->priv->button, &button_req);
1328
1329       requisition->height = MAX (requisition->height, button_req.height);
1330       requisition->width += button_req.width;
1331     }
1332 }
1333
1334 static void
1335 gtk_combo_box_size_allocate (GtkWidget     *widget,
1336                              GtkAllocation *allocation)
1337 {
1338   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1339   GtkAllocation child;
1340   GtkRequisition req;
1341   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1342
1343   widget->allocation = *allocation;
1344
1345   if (!combo_box->priv->tree_view)
1346     {
1347       if (combo_box->priv->cell_view)
1348         {
1349           gint border_width, xthickness, ythickness;
1350           gint width;
1351
1352           /* menu mode */
1353           gtk_widget_size_allocate (combo_box->priv->button, allocation);
1354
1355           /* set some things ready */
1356           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1357           xthickness = combo_box->priv->button->style->xthickness;
1358           ythickness = combo_box->priv->button->style->ythickness;
1359
1360           child.x = allocation->x + border_width + 1 + xthickness + 2;
1361           child.y = allocation->y + border_width + 1 + ythickness + 2;
1362
1363           width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
1364
1365           /* handle the children */
1366           gtk_widget_size_request (combo_box->priv->arrow, &req);
1367           child.width = req.width;
1368           child.height = allocation->height - 2 * (child.y - allocation->y);
1369           if (!is_rtl)
1370             child.x += width - req.width;
1371           gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1372           if (is_rtl)
1373             child.x += req.width;
1374           gtk_widget_size_request (combo_box->priv->separator, &req);
1375           child.width = req.width;
1376           if (!is_rtl)
1377             child.x -= req.width;
1378           gtk_widget_size_allocate (combo_box->priv->separator, &child);
1379
1380           if (is_rtl)
1381             {
1382               child.x += req.width;
1383               child.width = allocation->x + allocation->width 
1384                 - (border_width + 1 + xthickness + 2) - child.x;
1385             }
1386           else 
1387             {
1388               child.width = child.x;
1389               child.x = allocation->x + border_width + 1 + xthickness + 2;
1390               child.width -= child.x;
1391             }
1392
1393           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1394         }
1395       else
1396         {
1397           gtk_widget_size_request (combo_box->priv->button, &req);
1398           if (is_rtl)
1399             child.x = allocation->x;
1400           else
1401             child.x = allocation->x + allocation->width - req.width;
1402           child.y = allocation->y;
1403           child.width = req.width;
1404           child.height = allocation->height;
1405           gtk_widget_size_allocate (combo_box->priv->button, &child);
1406
1407           if (is_rtl)
1408             child.x = allocation->x + req.width;
1409           else
1410             child.x = allocation->x;
1411           child.y = allocation->y;
1412           child.width = allocation->width - req.width;
1413           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
1414         }
1415     }
1416   else
1417     {
1418       /* list mode */
1419
1420       /* button */
1421       gtk_widget_size_request (combo_box->priv->button, &req);
1422       if (is_rtl)
1423         child.x = allocation->x;
1424       else
1425         child.x = allocation->x + allocation->width - req.width;
1426       child.y = allocation->y;
1427       child.width = req.width;
1428       child.height = allocation->height;
1429       gtk_widget_size_allocate (combo_box->priv->button, &child);
1430
1431       /* frame */
1432       if (is_rtl)
1433         child.x = allocation->x + req.width;
1434       else
1435         child.x = allocation->x;
1436       child.y = allocation->y;
1437       child.width = allocation->width - req.width;
1438       child.height = allocation->height;
1439
1440       if (combo_box->priv->cell_view_frame)
1441         {
1442           gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
1443
1444           /* the sample */
1445           child.x +=
1446             GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1447             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1448           child.y +=
1449             GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1450             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
1451           child.width -= 2 * (
1452             GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1453             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1454           child.height -= 2 * (
1455             GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1456             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1457         }
1458
1459       gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
1460     }
1461 }
1462
1463 static void
1464 gtk_combo_box_unset_model (GtkComboBox *combo_box)
1465 {
1466   if (combo_box->priv->model)
1467     {
1468       g_signal_handler_disconnect (combo_box->priv->model,
1469                                    combo_box->priv->inserted_id);
1470       g_signal_handler_disconnect (combo_box->priv->model,
1471                                    combo_box->priv->deleted_id);
1472       g_signal_handler_disconnect (combo_box->priv->model,
1473                                    combo_box->priv->reordered_id);
1474       g_signal_handler_disconnect (combo_box->priv->model,
1475                                    combo_box->priv->changed_id);
1476     }
1477
1478   /* menu mode */
1479   if (!combo_box->priv->tree_view)
1480     {
1481       if (combo_box->priv->popup_widget)
1482         gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
1483                                (GtkCallback)gtk_widget_destroy, NULL);
1484     }
1485
1486   g_object_unref (G_OBJECT (combo_box->priv->model));
1487   combo_box->priv->model = NULL;
1488 }
1489
1490 static void
1491 gtk_combo_box_forall (GtkContainer *container,
1492                       gboolean      include_internals,
1493                       GtkCallback   callback,
1494                       gpointer      callback_data)
1495 {
1496   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1497
1498   if (include_internals)
1499     {
1500       if (combo_box->priv->button)
1501         (* callback) (combo_box->priv->button, callback_data);
1502       if (combo_box->priv->separator)
1503         (* callback) (combo_box->priv->separator, callback_data);
1504       if (combo_box->priv->arrow)
1505         (* callback) (combo_box->priv->arrow, callback_data);
1506       if (combo_box->priv->cell_view_frame)
1507         (* callback) (combo_box->priv->cell_view_frame, callback_data);
1508     }
1509
1510   if (GTK_BIN (container)->child)
1511     (* callback) (GTK_BIN (container)->child, callback_data);
1512 }
1513
1514 static gboolean
1515 gtk_combo_box_expose_event (GtkWidget      *widget,
1516                             GdkEventExpose *event)
1517 {
1518   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1519
1520   if (!combo_box->priv->tree_view)
1521     {
1522       gtk_container_propagate_expose (GTK_CONTAINER (widget),
1523                                       combo_box->priv->button, event);
1524
1525       if (combo_box->priv->separator)
1526         {
1527           gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1528                                           combo_box->priv->separator, event);
1529
1530           /* if not in this case, arrow gets its expose event from button */
1531           gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1532                                           combo_box->priv->arrow, event);
1533         }
1534     }
1535   else
1536     {
1537       gtk_container_propagate_expose (GTK_CONTAINER (widget),
1538                                       combo_box->priv->button, event);
1539
1540       if (combo_box->priv->cell_view_frame)
1541         gtk_container_propagate_expose (GTK_CONTAINER (widget),
1542                                         combo_box->priv->cell_view_frame, event);
1543     }
1544
1545   gtk_container_propagate_expose (GTK_CONTAINER (widget),
1546                                   GTK_BIN (widget)->child, event);
1547
1548   return FALSE;
1549 }
1550
1551 static gboolean
1552 gtk_combo_box_scroll_event (GtkWidget          *widget,
1553                             GdkEventScroll     *event)
1554 {
1555   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1556   gint index;
1557   gint items;
1558     
1559   index = gtk_combo_box_get_active (combo_box);
1560
1561   if (index != -1)
1562     {
1563       items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1564       
1565       if (event->direction == GDK_SCROLL_UP)
1566         index--;
1567       else 
1568         index++;
1569
1570       gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1571     }
1572
1573   return TRUE;
1574 }
1575
1576 /*
1577  * menu style
1578  */
1579
1580 static void
1581 cell_view_sync_cells (GtkComboBox *combo_box,
1582                       GtkCellView *cell_view)
1583 {
1584   GSList *k;
1585
1586   for (k = combo_box->priv->cells; k; k = k->next)
1587     {
1588       GSList *j;
1589       ComboCellInfo *info = (ComboCellInfo *)k->data;
1590
1591       if (info->pack == GTK_PACK_START)
1592         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1593                                     info->cell, info->expand);
1594       else if (info->pack == GTK_PACK_END)
1595         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1596                                   info->cell, info->expand);
1597
1598       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1599                                           info->cell,
1600                                           info->func, info->func_data, NULL);
1601
1602       for (j = info->attributes; j; j = j->next->next)
1603         {
1604           gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1605                                          info->cell,
1606                                          j->data,
1607                                          GPOINTER_TO_INT (j->next->data));
1608         }
1609     }
1610 }
1611
1612 static void
1613 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1614                           gboolean     add_children)
1615 {
1616   GtkWidget *box;
1617
1618   if (combo_box->priv->cell_view)
1619     {
1620       combo_box->priv->button = gtk_toggle_button_new ();
1621       g_signal_connect (combo_box->priv->button, "toggled",
1622                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1623       g_signal_connect_after (combo_box->priv->button, "key_press_event",
1624                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
1625       gtk_widget_set_parent (combo_box->priv->button,
1626                              GTK_BIN (combo_box)->child->parent);
1627
1628       combo_box->priv->separator = gtk_vseparator_new ();
1629       gtk_widget_set_parent (combo_box->priv->separator,
1630                              combo_box->priv->button);
1631       gtk_widget_show (combo_box->priv->separator);
1632
1633       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1634       gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1635       gtk_widget_show (combo_box->priv->arrow);
1636
1637       gtk_widget_show_all (combo_box->priv->button);
1638
1639       if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1640         {
1641           /* I have no clue why, but we need to manually map in this case. */
1642           gtk_widget_map (combo_box->priv->separator);
1643           gtk_widget_map (combo_box->priv->arrow);
1644         }
1645     }
1646   else
1647     {
1648       combo_box->priv->button = gtk_toggle_button_new ();
1649       g_signal_connect (combo_box->priv->button, "toggled",
1650                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1651       g_signal_connect_after (combo_box, "key_press_event",
1652                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
1653       gtk_widget_set_parent (combo_box->priv->button,
1654                              GTK_BIN (combo_box)->child->parent);
1655
1656       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1657       gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1658                          combo_box->priv->arrow);
1659       gtk_widget_show_all (combo_box->priv->button);
1660     }
1661
1662   g_signal_connect (combo_box->priv->button, "button_press_event",
1663                     G_CALLBACK (gtk_combo_box_menu_button_press),
1664                     combo_box);
1665
1666   /* create our funky menu */
1667   box = gtk_menu_new ();
1668   g_signal_connect (box, "key_press_event",
1669                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1670   gtk_combo_box_set_popup_widget (combo_box, box);
1671
1672   /* add items */
1673   if (add_children)
1674     gtk_combo_box_menu_fill (combo_box);
1675
1676 }
1677
1678 static void
1679 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1680 {
1681   gint i, items;
1682   GtkWidget *menu;
1683   GtkWidget *tmp;
1684
1685   if (!combo_box->priv->model)
1686     return;
1687
1688   items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1689   menu = combo_box->priv->popup_widget;
1690
1691   for (i = 0; i < items; i++)
1692     {
1693       GtkTreePath *path;
1694
1695       path = gtk_tree_path_new_from_indices (i, -1);
1696       tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1697                                                     path);
1698       g_signal_connect (tmp, "activate",
1699                         G_CALLBACK (gtk_combo_box_menu_item_activate),
1700                         combo_box);
1701
1702       cell_view_sync_cells (combo_box,
1703                             GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1704
1705       gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1706
1707       if (combo_box->priv->wrap_width)
1708         gtk_combo_box_relayout_item (combo_box, i);
1709
1710       gtk_widget_show (tmp);
1711
1712       gtk_tree_path_free (path);
1713     }
1714 }
1715
1716 static void
1717 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1718 {
1719   g_signal_handlers_disconnect_matched (combo_box->priv->button,
1720                                         G_SIGNAL_MATCH_DATA,
1721                                         0, 0, NULL,
1722                                         gtk_combo_box_menu_button_press, NULL);
1723
1724   /* unparent will remove our latest ref */
1725   if (combo_box->priv->cell_view)
1726     {
1727       gtk_widget_unparent (combo_box->priv->arrow);
1728       combo_box->priv->arrow = NULL;
1729
1730       gtk_widget_unparent (combo_box->priv->separator);
1731       combo_box->priv->separator = NULL;
1732
1733       gtk_widget_unparent (combo_box->priv->button);
1734       combo_box->priv->button = NULL;
1735     }
1736   else
1737     {
1738       /* will destroy the arrow too */
1739       gtk_widget_unparent (combo_box->priv->button);
1740
1741       combo_box->priv->button = NULL;
1742       combo_box->priv->arrow = NULL;
1743     }
1744
1745   /* changing the popup window will unref the menu and the children */
1746 }
1747
1748 /*
1749  * grid
1750  */
1751
1752 static void
1753 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1754                              gint         index,
1755                              gint        *cols,
1756                              gint        *rows)
1757 {
1758   GtkTreeIter iter;
1759
1760   gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1761
1762   if (cols)
1763     {
1764       if (combo_box->priv->col_column == -1)
1765         *cols = 1;
1766       else
1767         gtk_tree_model_get (combo_box->priv->model, &iter,
1768                             combo_box->priv->col_column, cols,
1769                             -1);
1770     }
1771
1772   if (rows)
1773     {
1774       if (combo_box->priv->row_column == -1)
1775         *rows = 1;
1776       else
1777         gtk_tree_model_get (combo_box->priv->model, &iter,
1778                             combo_box->priv->row_column, rows,
1779                             -1);
1780     }
1781 }
1782
1783 static gboolean
1784 menu_occupied (GtkMenu *menu,
1785                guint    left_attach,
1786                guint    right_attach,
1787                guint    top_attach,
1788                guint    bottom_attach)
1789 {
1790   GList *i;
1791
1792   g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1793   g_return_val_if_fail (left_attach < right_attach, TRUE);
1794   g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1795
1796   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1797     {
1798       guint l, r, b, t;
1799       gboolean h_intersect = FALSE;
1800       gboolean v_intersect = FALSE;
1801
1802       gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1803                                "left_attach", &l,
1804                                "right_attach", &r,
1805                                "bottom_attach", &b,
1806                                "top_attach", &t,
1807                                NULL);
1808
1809       /* look if this item intersects with the given coordinates */
1810       h_intersect  = left_attach <= l && l <= right_attach;
1811       h_intersect &= left_attach <= r && r <= right_attach;
1812
1813       v_intersect  = top_attach <= t && t <= bottom_attach;
1814       v_intersect &= top_attach <= b && b <= bottom_attach;
1815
1816       if (h_intersect && v_intersect)
1817         return TRUE;
1818     }
1819
1820   return FALSE;
1821 }
1822
1823 static void
1824 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1825                              gint         index)
1826 {
1827   gint current_col = 0, current_row = 0;
1828   gint rows, cols;
1829   GList *list;
1830   GtkWidget *item;
1831   GtkWidget *menu;
1832
1833   menu = combo_box->priv->popup_widget;
1834   if (!GTK_IS_MENU_SHELL (menu))
1835     return;
1836
1837   list = gtk_container_get_children (GTK_CONTAINER (menu));
1838   item = g_list_nth_data (list, index);
1839   g_list_free (list);
1840
1841   gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1842
1843   /* look for a good spot */
1844   while (1)
1845     {
1846       if (current_col + cols > combo_box->priv->wrap_width)
1847         {
1848           current_col = 0;
1849           current_row++;
1850         }
1851
1852       if (!menu_occupied (GTK_MENU (menu),
1853                           current_col, current_col + cols,
1854                           current_row, current_row + rows))
1855         break;
1856
1857       current_col++;
1858     }
1859
1860   /* set attach props */
1861   gtk_menu_attach (GTK_MENU (menu), item,
1862                    current_col, current_col + cols,
1863                    current_row, current_row + rows);
1864 }
1865
1866 static void
1867 gtk_combo_box_relayout (GtkComboBox *combo_box)
1868 {
1869   GList *list, *j;
1870   GtkWidget *menu;
1871
1872   /* do nothing unless we are in menu style */
1873   if (combo_box->priv->tree_view)
1874     return;
1875
1876   menu = combo_box->priv->popup_widget;
1877
1878   /* get rid of all children */
1879   g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1880
1881   list = gtk_container_get_children (GTK_CONTAINER (menu));
1882
1883   for (j = g_list_last (list); j; j = j->prev)
1884     gtk_container_remove (GTK_CONTAINER (menu), j->data);
1885
1886   g_list_free (list);
1887
1888   /* and relayout */
1889   gtk_combo_box_menu_fill (combo_box);
1890 }
1891
1892 /* callbacks */
1893 static gboolean
1894 gtk_combo_box_menu_button_press (GtkWidget      *widget,
1895                                  GdkEventButton *event,
1896                                  gpointer        user_data)
1897 {
1898   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1899
1900   if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1901     return FALSE;
1902
1903   if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1904     {
1905       combo_box->priv->popup_in_progress = TRUE;
1906       
1907       gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1908                            combo_box->priv->active_item);
1909
1910       gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1911                       NULL, NULL,
1912                       gtk_combo_box_menu_position, combo_box,
1913                       event->button, event->time);
1914
1915       return TRUE;
1916     }
1917
1918   return FALSE;
1919 }
1920
1921 static void
1922 gtk_combo_box_menu_item_activate (GtkWidget *item,
1923                                   gpointer   user_data)
1924 {
1925   gint index;
1926   GtkWidget *menu;
1927   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1928
1929   menu = combo_box->priv->popup_widget;
1930   g_return_if_fail (GTK_IS_MENU (menu));
1931
1932   index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1933
1934   gtk_combo_box_set_active (combo_box, index);
1935 }
1936
1937 static void
1938 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
1939                                   GtkTreePath      *path,
1940                                   GtkTreeIter      *iter,
1941                                   gpointer          user_data)
1942 {
1943   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1944   gint index = gtk_tree_path_get_indices (path)[0];
1945
1946   if (combo_box->priv->active_item >= index)
1947     combo_box->priv->active_item++;
1948
1949   if (!combo_box->priv->tree_view)
1950     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1951 }
1952
1953 static void
1954 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
1955                                  GtkTreePath      *path,
1956                                  gpointer          user_data)
1957 {
1958   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1959   gint index = gtk_tree_path_get_indices (path)[0];
1960
1961   if (!combo_box->priv->tree_view)
1962     gtk_combo_box_menu_row_deleted (model, path, user_data);
1963   
1964   if (index == combo_box->priv->active_item)
1965     {
1966       gint items = gtk_tree_model_iter_n_children (model, NULL);
1967
1968       if (items == 0)
1969         gtk_combo_box_set_active_internal (combo_box, -1);
1970       else if (index == items)
1971         gtk_combo_box_set_active_internal (combo_box, index - 1);
1972       else
1973         gtk_combo_box_set_active_internal (combo_box, index);
1974     }
1975   else if (combo_box->priv->active_item > index)
1976     combo_box->priv->active_item--;
1977 }
1978
1979 static void
1980 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
1981                                     GtkTreePath     *path,
1982                                     GtkTreeIter     *iter,
1983                                     gint            *new_order,
1984                                     gpointer         user_data)
1985 {
1986   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1987   gint items = gtk_tree_model_iter_n_children (model, NULL);
1988   gint i;
1989
1990   for (i = 0; i < items; i++)
1991     if (new_order[i] == combo_box->priv->active_item)
1992       {
1993         combo_box->priv->active_item = i;
1994         break;
1995       }
1996
1997   if (!combo_box->priv->tree_view)
1998     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
1999 }
2000                                                     
2001 static void
2002 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
2003                                  GtkTreePath      *path,
2004                                  GtkTreeIter      *iter,
2005                                  gpointer          user_data)
2006 {
2007   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2008   gint index = gtk_tree_path_get_indices (path)[0];
2009
2010   if (index == combo_box->priv->active_item &&
2011       combo_box->priv->cell_view)
2012     gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2013   
2014   if (combo_box->priv->tree_view)
2015     gtk_combo_box_list_row_changed (model, path, iter, user_data);
2016   else
2017     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2018 }
2019
2020
2021 static void
2022 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2023                                  GtkTreePath  *path,
2024                                  GtkTreeIter  *iter,
2025                                  gpointer      user_data)
2026 {
2027   GtkWidget *menu;
2028   GtkWidget *item;
2029   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2030
2031   if (!combo_box->priv->popup_widget)
2032     return;
2033
2034   menu = combo_box->priv->popup_widget;
2035   g_return_if_fail (GTK_IS_MENU (menu));
2036
2037   item = gtk_cell_view_menu_item_new_from_model (model, path);
2038   g_signal_connect (item, "activate",
2039                     G_CALLBACK (gtk_combo_box_menu_item_activate),
2040                     combo_box);
2041
2042   cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2043
2044   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2045                          gtk_tree_path_get_indices (path)[0]);
2046   gtk_widget_show (item);
2047 }
2048
2049 static void
2050 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2051                                 GtkTreePath  *path,
2052                                 gpointer      user_data)
2053 {
2054   gint index;
2055   GtkWidget *menu;
2056   GtkWidget *item;
2057   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2058
2059   if (!combo_box->priv->popup_widget)
2060     return;
2061
2062   index = gtk_tree_path_get_indices (path)[0];
2063
2064   menu = combo_box->priv->popup_widget;
2065   g_return_if_fail (GTK_IS_MENU (menu));
2066
2067   item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2068   g_return_if_fail (GTK_IS_MENU_ITEM (item));
2069
2070   gtk_container_remove (GTK_CONTAINER (menu), item);
2071 }
2072
2073 static void
2074 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
2075                                     GtkTreePath      *path,
2076                                     GtkTreeIter      *iter,
2077                                     gint             *new_order,
2078                                     gpointer          user_data)
2079 {
2080   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2081
2082   gtk_combo_box_relayout (combo_box);
2083 }
2084                                     
2085 static void
2086 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2087                                 GtkTreePath  *path,
2088                                 GtkTreeIter  *iter,
2089                                 gpointer      user_data)
2090 {
2091   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2092   gint width;
2093
2094   if (!combo_box->priv->popup_widget)
2095     return;
2096
2097   if (combo_box->priv->wrap_width)
2098     gtk_combo_box_relayout_item (combo_box,
2099                                  gtk_tree_path_get_indices (path)[0]);
2100
2101   width = gtk_combo_box_calc_requested_width (combo_box, path);
2102
2103   if (width > combo_box->priv->width)
2104     {
2105       if (combo_box->priv->cell_view)
2106         {
2107           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2108           gtk_widget_queue_resize (combo_box->priv->cell_view);
2109         }
2110       combo_box->priv->width = width;
2111     }
2112 }
2113
2114 /*
2115  * list style
2116  */
2117
2118 static void
2119 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2120 {
2121   GSList *i;
2122   GtkTreeSelection *sel;
2123
2124   combo_box->priv->button = gtk_toggle_button_new ();
2125   gtk_widget_set_parent (combo_box->priv->button,
2126                          GTK_BIN (combo_box)->child->parent);
2127   g_signal_connect (combo_box->priv->button, "button_press_event",
2128                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2129   g_signal_connect (combo_box->priv->button, "toggled",
2130                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2131   g_signal_connect_after (combo_box, "key_press_event",
2132                           G_CALLBACK (gtk_combo_box_key_press), combo_box);
2133
2134   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2135   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2136                      combo_box->priv->arrow);
2137   combo_box->priv->separator = NULL;
2138   gtk_widget_show_all (combo_box->priv->button);
2139
2140   if (combo_box->priv->cell_view)
2141     {
2142       combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2143       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2144                              GTK_BIN (combo_box)->child->parent);
2145       gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2146                                  GTK_SHADOW_IN);
2147
2148       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
2149                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2150
2151       gtk_widget_show (combo_box->priv->cell_view_frame);
2152     }
2153
2154   combo_box->priv->tree_view = gtk_tree_view_new ();
2155   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2156   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2157   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2158                                      FALSE);
2159   if (combo_box->priv->model)
2160     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2161                              combo_box->priv->model);
2162     
2163   g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2164                     G_CALLBACK (gtk_combo_box_list_button_pressed),
2165                     combo_box);
2166   g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2167                     G_CALLBACK (gtk_combo_box_list_button_released),
2168                     combo_box);
2169   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2170                     G_CALLBACK (gtk_combo_box_list_key_press),
2171                     combo_box);
2172
2173   combo_box->priv->column = gtk_tree_view_column_new ();
2174   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2175                                combo_box->priv->column);
2176
2177   /* sync up */
2178   for (i = combo_box->priv->cells; i; i = i->next)
2179     {
2180       GSList *j;
2181       ComboCellInfo *info = (ComboCellInfo *)i->data;
2182
2183       if (info->pack == GTK_PACK_START)
2184         gtk_tree_view_column_pack_start (combo_box->priv->column,
2185                                          info->cell, info->expand);
2186       else if (info->pack == GTK_PACK_END)
2187         gtk_tree_view_column_pack_end (combo_box->priv->column,
2188                                        info->cell, info->expand);
2189
2190       for (j = info->attributes; j; j = j->next->next)
2191         {
2192           gtk_tree_view_column_add_attribute (combo_box->priv->column,
2193                                               info->cell,
2194                                               j->data,
2195                                               GPOINTER_TO_INT (j->next->data));
2196         }
2197     }
2198
2199   if (combo_box->priv->active_item != -1)
2200     {
2201       GtkTreePath *path;
2202
2203       path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2204       if (path)
2205         {
2206           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2207                                     path, NULL, FALSE);
2208           gtk_tree_path_free (path);
2209         }
2210     }
2211
2212   /* set sample/popup widgets */
2213   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2214
2215   gtk_widget_show (combo_box->priv->tree_view);
2216 }
2217
2218 static void
2219 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2220 {
2221   /* disconnect signals */
2222   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2223                                         G_SIGNAL_MATCH_DATA,
2224                                         0, 0, NULL, NULL, combo_box);
2225   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2226                                         G_SIGNAL_MATCH_DATA,
2227                                         0, 0, NULL,
2228                                         gtk_combo_box_list_button_pressed,
2229                                         NULL);
2230
2231   /* destroy things (unparent will kill the latest ref from us)
2232    * last unref on button will destroy the arrow
2233    */
2234   gtk_widget_unparent (combo_box->priv->button);
2235   combo_box->priv->button = NULL;
2236   combo_box->priv->arrow = NULL;
2237
2238   if (combo_box->priv->cell_view)
2239     {
2240       g_object_set (G_OBJECT (combo_box->priv->cell_view),
2241                     "background_set", FALSE,
2242                     NULL);
2243
2244       gtk_widget_unparent (combo_box->priv->cell_view_frame);
2245       combo_box->priv->cell_view_frame = NULL;
2246     }
2247
2248   gtk_widget_destroy (combo_box->priv->tree_view);
2249
2250   combo_box->priv->tree_view = NULL;
2251   combo_box->priv->popup_widget = NULL;
2252 }
2253
2254 /* callbacks */
2255 static void
2256 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2257 {
2258   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2259     gtk_grab_remove (combo_box->priv->tree_view);
2260
2261   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2262     {
2263       gtk_grab_remove (combo_box->priv->popup_window);
2264       gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2265       gdk_pointer_ungrab (GDK_CURRENT_TIME);
2266     }
2267 }
2268
2269 static gboolean
2270 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
2271                                    GdkEventButton *event,
2272                                    gpointer        data)
2273 {
2274   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2275
2276   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2277
2278   if (ewidget == combo_box->priv->tree_view)
2279     return TRUE;
2280
2281   if ((ewidget != combo_box->priv->button) ||
2282       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2283     return FALSE;
2284
2285   gtk_combo_box_popup (combo_box);
2286
2287   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2288                                 TRUE);
2289
2290   combo_box->priv->popup_in_progress = TRUE;
2291
2292   return TRUE;
2293 }
2294
2295 static gboolean
2296 gtk_combo_box_list_button_released (GtkWidget      *widget,
2297                                     GdkEventButton *event,
2298                                     gpointer        data)
2299 {
2300   gboolean ret;
2301   GtkTreePath *path = NULL;
2302
2303   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2304
2305   gboolean popup_in_progress = FALSE;
2306
2307   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2308
2309   if (combo_box->priv->popup_in_progress)
2310     {
2311       popup_in_progress = TRUE;
2312       combo_box->priv->popup_in_progress = FALSE;
2313     }
2314
2315   if (ewidget != combo_box->priv->tree_view)
2316     {
2317       if (ewidget == combo_box->priv->button &&
2318           !popup_in_progress &&
2319           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2320         {
2321           gtk_combo_box_popdown (combo_box);
2322           return TRUE;
2323         }
2324
2325       /* released outside treeview */
2326       if (ewidget != combo_box->priv->button)
2327         {
2328           gtk_combo_box_popdown (combo_box);
2329
2330           return TRUE;
2331         }
2332
2333       return FALSE;
2334     }
2335
2336   /* select something cool */
2337   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2338                                        event->x, event->y,
2339                                        &path,
2340                                        NULL, NULL, NULL);
2341
2342   if (!ret)
2343     return TRUE; /* clicked outside window? */
2344
2345   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2346   gtk_combo_box_popdown (combo_box);
2347
2348   gtk_tree_path_free (path);
2349
2350   return TRUE;
2351 }
2352
2353 static gboolean
2354 gtk_combo_box_key_press (GtkWidget   *widget,
2355                          GdkEventKey *event,
2356                          gpointer     data)
2357 {
2358   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2359   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2360   gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2361   gint index = gtk_combo_box_get_active (combo_box);
2362   gint new_index;
2363
2364   if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) && 
2365       state == GDK_MOD1_MASK)
2366     {
2367       gtk_combo_box_popup (combo_box);
2368
2369       return TRUE;
2370     }
2371
2372   switch (event->keyval) 
2373     {
2374     case GDK_Down:
2375     case GDK_KP_Down:
2376       new_index = index + 1;
2377       break;
2378     case GDK_Up:
2379     case GDK_KP_Up:
2380       new_index = index - 1;
2381       break;
2382     case GDK_Page_Up:
2383     case GDK_KP_Page_Up:
2384     case GDK_Home: 
2385     case GDK_KP_Home:
2386       new_index = 0;
2387       break;
2388     case GDK_Page_Down:
2389     case GDK_KP_Page_Down:
2390     case GDK_End: 
2391     case GDK_KP_End:
2392       new_index = items - 1;
2393       break;
2394     default:
2395       return FALSE;
2396     }
2397   
2398   gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2399
2400   return TRUE;
2401 }
2402
2403 static gboolean
2404 gtk_combo_box_menu_key_press (GtkWidget   *widget,
2405                               GdkEventKey *event,
2406                               gpointer     data)
2407 {
2408   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2409   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2410
2411   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
2412       state == GDK_MOD1_MASK)
2413     {
2414       gtk_combo_box_popdown (combo_box);
2415
2416       return TRUE;
2417     }
2418   
2419   return FALSE;
2420 }
2421
2422 static gboolean
2423 gtk_combo_box_list_key_press (GtkWidget   *widget,
2424                               GdkEventKey *event,
2425                               gpointer     data)
2426 {
2427   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2428   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2429
2430   if (event->keyval == GDK_Escape ||
2431       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
2432        state == GDK_MOD1_MASK))
2433     {
2434       /* reset active item -- this is incredibly lame and ugly */
2435       gtk_combo_box_set_active (combo_box,
2436                                 gtk_combo_box_get_active (combo_box));
2437       
2438       gtk_combo_box_popdown (combo_box);
2439       
2440       return TRUE;
2441     }
2442     
2443   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2444       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
2445   {
2446     gboolean ret;
2447     GtkTreeIter iter;
2448     GtkTreeModel *model = NULL;
2449     GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2450     
2451     ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2452     if (ret)
2453       {
2454         GtkTreePath *path;
2455         
2456         path = gtk_tree_model_get_path (model, &iter);
2457         if (path)
2458           {
2459             gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2460             gtk_tree_path_free (path);
2461           }
2462       }
2463
2464     gtk_combo_box_popdown (combo_box);
2465     
2466     return TRUE;
2467   }
2468
2469   return FALSE;
2470 }
2471
2472 static void
2473 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2474                                 GtkTreePath  *path,
2475                                 GtkTreeIter  *iter,
2476                                 gpointer      data)
2477 {
2478   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2479   gint width;
2480
2481   width = gtk_combo_box_calc_requested_width (combo_box, path);
2482
2483   if (width > combo_box->priv->width)
2484     {
2485       if (combo_box->priv->cell_view) 
2486         {
2487           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2488           gtk_widget_queue_resize (combo_box->priv->cell_view);
2489         }
2490       combo_box->priv->width = width;
2491     }
2492 }
2493
2494 /*
2495  * GtkCellLayout implementation
2496  */
2497 static void
2498 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
2499                                       GtkCellRenderer *cell,
2500                                       gboolean         expand)
2501 {
2502   ComboCellInfo *info;
2503   GtkComboBox *combo_box;
2504   GtkWidget *menu;
2505
2506   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2507   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2508
2509   combo_box = GTK_COMBO_BOX (layout);
2510
2511   g_object_ref (G_OBJECT (cell));
2512   gtk_object_sink (GTK_OBJECT (cell));
2513
2514   info = g_new0 (ComboCellInfo, 1);
2515   info->cell = cell;
2516   info->expand = expand;
2517   info->pack = GTK_PACK_START;
2518
2519   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2520
2521   if (combo_box->priv->cell_view)
2522     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2523                                 cell, expand);
2524
2525   if (combo_box->priv->column)
2526     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2527
2528   menu = combo_box->priv->popup_widget;
2529   if (GTK_IS_MENU (menu))
2530     {
2531       GList *i, *list;
2532
2533       list = gtk_container_get_children (GTK_CONTAINER (menu));
2534       for (i = list; i; i = i->next)
2535         {
2536           GtkCellView *view;
2537
2538           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2539             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2540           else
2541             view = GTK_CELL_VIEW (i->data);
2542
2543           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2544         }
2545       g_list_free (list);
2546     }
2547 }
2548
2549 static void
2550 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
2551                                     GtkCellRenderer *cell,
2552                                     gboolean         expand)
2553 {
2554   ComboCellInfo *info;
2555   GtkComboBox *combo_box;
2556   GtkWidget *menu;
2557
2558   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2559   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2560
2561   combo_box = GTK_COMBO_BOX (layout);
2562
2563   g_object_ref (G_OBJECT (cell));
2564   gtk_object_sink (GTK_OBJECT (cell));
2565
2566   info = g_new0 (ComboCellInfo, 1);
2567   info->cell = cell;
2568   info->expand = expand;
2569   info->pack = GTK_PACK_END;
2570
2571   if (combo_box->priv->cell_view)
2572     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2573                               cell, expand);
2574
2575   if (combo_box->priv->column)
2576     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2577
2578   menu = combo_box->priv->popup_widget;
2579   if (GTK_IS_MENU (menu))
2580     {
2581       GList *i, *list;
2582
2583       list = gtk_container_get_children (GTK_CONTAINER (menu));
2584       for (i = list; i; i = i->next)
2585         {
2586           GtkCellView *view;
2587
2588           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2589             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2590           else
2591             view = GTK_CELL_VIEW (i->data);
2592
2593           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2594         }
2595       g_list_free (list);
2596     }
2597 }
2598
2599 static void
2600 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2601 {
2602   GtkWidget *menu;
2603   GtkComboBox *combo_box;
2604   GSList *i;
2605   
2606   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2607
2608   combo_box = GTK_COMBO_BOX (layout);
2609  
2610   if (combo_box->priv->cell_view)
2611     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2612
2613   if (combo_box->priv->column)
2614     gtk_tree_view_column_clear (combo_box->priv->column);
2615
2616   for (i = combo_box->priv->cells; i; i = i->next)
2617     {
2618      ComboCellInfo *info = (ComboCellInfo *)i->data;
2619
2620       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2621       g_object_unref (G_OBJECT (info->cell));
2622       g_free (info);
2623       i->data = NULL;
2624     }
2625   g_slist_free (combo_box->priv->cells);
2626   combo_box->priv->cells = NULL;
2627
2628   menu = combo_box->priv->popup_widget;
2629   if (GTK_IS_MENU (menu))
2630     {
2631       GList *i, *list;
2632
2633       list = gtk_container_get_children (GTK_CONTAINER (menu));
2634       for (i = list; i; i = i->next)
2635         {
2636           GtkCellView *view;
2637
2638           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2639             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2640           else
2641             view = GTK_CELL_VIEW (i->data);
2642
2643           gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2644         }
2645       g_list_free (list);
2646     }
2647 }
2648
2649 static void
2650 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
2651                                          GtkCellRenderer *cell,
2652                                          const gchar     *attribute,
2653                                          gint             column)
2654 {
2655   ComboCellInfo *info;
2656   GtkComboBox *combo_box;
2657   GtkWidget *menu;
2658
2659   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2660   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2661
2662   combo_box = GTK_COMBO_BOX (layout);
2663
2664   info = gtk_combo_box_get_cell_info (combo_box, cell);
2665
2666   info->attributes = g_slist_prepend (info->attributes,
2667                                       GINT_TO_POINTER (column));
2668   info->attributes = g_slist_prepend (info->attributes,
2669                                       g_strdup (attribute));
2670
2671   if (combo_box->priv->cell_view)
2672     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2673                                    cell, attribute, column);
2674
2675   if (combo_box->priv->column)
2676     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2677                                    cell, attribute, column);
2678
2679   menu = combo_box->priv->popup_widget;
2680   if (GTK_IS_MENU (menu))
2681     {
2682       GList *i, *list;
2683
2684       list = gtk_container_get_children (GTK_CONTAINER (menu));
2685       for (i = list; i; i = i->next)
2686         {
2687           GtkCellView *view;
2688
2689           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2690             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2691           else
2692             view = GTK_CELL_VIEW (i->data);
2693
2694           gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2695                                          attribute, column);
2696         }
2697       g_list_free (list);
2698     }
2699
2700   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2701 }
2702
2703 static void
2704 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
2705                                               GtkCellRenderer       *cell,
2706                                               GtkCellLayoutDataFunc  func,
2707                                               gpointer               func_data,
2708                                               GDestroyNotify         destroy)
2709 {
2710   ComboCellInfo *info;
2711   GtkComboBox *combo_box;
2712   GtkWidget *menu;
2713
2714   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2715
2716   combo_box = GTK_COMBO_BOX (layout);
2717
2718   info = gtk_combo_box_get_cell_info (combo_box, cell);
2719   g_return_if_fail (info != NULL);
2720
2721   if (info->destroy)
2722     {
2723       GDestroyNotify d = info->destroy;
2724
2725       info->destroy = NULL;
2726       d (info->func_data);
2727     }
2728
2729   info->func = func;
2730   info->func_data = func_data;
2731   info->destroy = destroy;
2732
2733   if (combo_box->priv->cell_view)
2734     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2735
2736   if (combo_box->priv->column)
2737     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2738
2739   menu = combo_box->priv->popup_widget;
2740   if (GTK_IS_MENU (menu))
2741     {
2742       GList *i, *list;
2743
2744       list = gtk_container_get_children (GTK_CONTAINER (menu));
2745       for (i = list; i; i = i->next)
2746         {
2747           GtkCellView *view;
2748
2749           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2750             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2751           else
2752             view = GTK_CELL_VIEW (i->data);
2753
2754           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2755                                               func, func_data, NULL);
2756         }
2757       g_list_free (list);
2758     }
2759
2760   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2761 }
2762
2763 static void
2764 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
2765                                             GtkCellRenderer *cell)
2766 {
2767   ComboCellInfo *info;
2768   GtkComboBox *combo_box;
2769   GtkWidget *menu;
2770   GSList *list;
2771
2772   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2773   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2774
2775   combo_box = GTK_COMBO_BOX (layout);
2776
2777   info = gtk_combo_box_get_cell_info (combo_box, cell);
2778   g_return_if_fail (info != NULL);
2779
2780   list = info->attributes;
2781   while (list && list->next)
2782     {
2783       g_free (list->data);
2784       list = list->next->next;
2785     }
2786   g_slist_free (info->attributes);
2787   info->attributes = NULL;
2788
2789   if (combo_box->priv->cell_view)
2790     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2791
2792   if (combo_box->priv->column)
2793     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2794
2795   menu = combo_box->priv->popup_widget;
2796   if (GTK_IS_MENU (menu))
2797     {
2798       GList *i, *list;
2799
2800       list = gtk_container_get_children (GTK_CONTAINER (menu));
2801       for (i = list; i; i = i->next)
2802         {
2803           GtkCellView *view;
2804
2805           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2806             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2807           else
2808             view = GTK_CELL_VIEW (i->data);
2809
2810           gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2811         }
2812       g_list_free (list);
2813     }
2814
2815   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2816 }
2817
2818 static void
2819 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
2820                                    GtkCellRenderer *cell,
2821                                    gint             position)
2822 {
2823   ComboCellInfo *info;
2824   GtkComboBox *combo_box;
2825   GtkWidget *menu;
2826   GSList *link;
2827
2828   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2829   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2830
2831   combo_box = GTK_COMBO_BOX (layout);
2832
2833   info = gtk_combo_box_get_cell_info (combo_box, cell);
2834
2835   g_return_if_fail (info != NULL);
2836   g_return_if_fail (position >= 0);
2837
2838   link = g_slist_find (combo_box->priv->cells, info);
2839
2840   g_return_if_fail (link != NULL);
2841
2842   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2843   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2844                                            position);
2845
2846   if (combo_box->priv->cell_view)
2847     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2848                              cell, position);
2849
2850   if (combo_box->priv->column)
2851     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2852                              cell, position);
2853
2854   menu = combo_box->priv->popup_widget;
2855   if (GTK_IS_MENU (menu))
2856     {
2857       GList *i, *list;
2858
2859       list = gtk_container_get_children (GTK_CONTAINER (menu));
2860       for (i = list; i; i = i->next)
2861         {
2862           GtkCellView *view;
2863
2864           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2865             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2866           else
2867             view = GTK_CELL_VIEW (i->data);
2868
2869           gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2870         }
2871       g_list_free (list);
2872     }
2873
2874   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2875 }
2876
2877 /*
2878  * public API
2879  */
2880
2881 /**
2882  * gtk_combo_box_new:
2883  *
2884  * Creates a new empty #GtkComboBox.
2885  *
2886  * Return value: A new #GtkComboBox.
2887  *
2888  * Since: 2.4
2889  */
2890 GtkWidget *
2891 gtk_combo_box_new (void)
2892 {
2893   return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2894 }
2895
2896 /**
2897  * gtk_combo_box_new_with_model:
2898  * @model: A #GtkTreeModel.
2899  *
2900  * Creates a new #GtkComboBox with the model initialized to @model.
2901  *
2902  * Return value: A new #GtkComboBox.
2903  *
2904  * Since: 2.4
2905  */
2906 GtkWidget *
2907 gtk_combo_box_new_with_model (GtkTreeModel *model)
2908 {
2909   GtkComboBox *combo_box;
2910
2911   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2912
2913   combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2914                                            "model", model,
2915                                            NULL));
2916
2917   return GTK_WIDGET (combo_box);
2918 }
2919
2920 /**
2921  * gtk_combo_box_set_wrap_width:
2922  * @combo_box: A #GtkComboBox.
2923  * @width: Preferred number of columns.
2924  *
2925  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2926  * the preferred number of columns when you want to the popup to be layed out
2927  * in a table.
2928  *
2929  * Since: 2.4
2930  */
2931 void
2932 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2933                               gint         width)
2934 {
2935   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2936   g_return_if_fail (width >= 0);
2937
2938   if (width != combo_box->priv->wrap_width)
2939     {
2940       combo_box->priv->wrap_width = width;
2941
2942       gtk_combo_box_check_appearance (combo_box);
2943       gtk_combo_box_relayout (combo_box);
2944       
2945       g_object_notify (G_OBJECT (combo_box), "wrap_width");
2946     }
2947 }
2948
2949 /**
2950  * gtk_combo_box_set_row_span_column:
2951  * @combo_box: A #GtkComboBox.
2952  * @row_span: A column in the model passed during construction.
2953  *
2954  * Sets the column with row span information for @combo_box to be @row_span.
2955  * The row span column contains integers which indicate how many rows
2956  * an item should span.
2957  *
2958  * Since: 2.4
2959  */
2960 void
2961 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2962                                    gint         row_span)
2963 {
2964   gint col;
2965
2966   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2967
2968   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2969   g_return_if_fail (row_span >= 0 && row_span < col);
2970
2971   if (row_span != combo_box->priv->row_column)
2972     {
2973       combo_box->priv->row_column = row_span;
2974       
2975       gtk_combo_box_relayout (combo_box);
2976  
2977       g_object_notify (G_OBJECT (combo_box), "row_span_column");
2978     }
2979 }
2980
2981 /**
2982  * gtk_combo_box_set_column_span_column:
2983  * @combo_box: A #GtkComboBox.
2984  * @column_span: A column in the model passed during construction.
2985  *
2986  * Sets the column with column span information for @combo_box to be
2987  * @column_span. The column span column contains integers which indicate
2988  * how many columns an item should span.
2989  *
2990  * Since: 2.4
2991  */
2992 void
2993 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2994                                       gint         column_span)
2995 {
2996   gint col;
2997
2998   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2999
3000   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3001   g_return_if_fail (column_span >= 0 && column_span < col);
3002
3003   if (column_span != combo_box->priv->col_column)
3004     {
3005       combo_box->priv->col_column = column_span;
3006       
3007       gtk_combo_box_relayout (combo_box);
3008
3009       g_object_notify (G_OBJECT (combo_box), "column_span_column");
3010     }
3011 }
3012
3013 /**
3014  * gtk_combo_box_get_active:
3015  * @combo_box: A #GtkComboBox.
3016  *
3017  * Returns the index of the currently active item, or -1 if there's no
3018  * active item.
3019  *
3020  * Return value: An integer which is the index of the currently active item, or
3021  * -1 if there's no active item.
3022  *
3023  * Since: 2.4
3024  */
3025 gint
3026 gtk_combo_box_get_active (GtkComboBox *combo_box)
3027 {
3028   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3029
3030   return combo_box->priv->active_item;
3031 }
3032
3033 /**
3034  * gtk_combo_box_set_active:
3035  * @combo_box: A #GtkComboBox.
3036  * @index: An index in the model passed during construction, or -1 to have
3037  * no active item.
3038  *
3039  * Sets the active item of @combo_box to be the item at @index.
3040  *
3041  * Since: 2.4
3042  */
3043 void
3044 gtk_combo_box_set_active (GtkComboBox *combo_box,
3045                           gint         index)
3046 {
3047   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3048   /* -1 means "no item selected" */
3049   g_return_if_fail (index >= -1);
3050
3051   if (combo_box->priv->active_item == index)
3052     return;
3053   
3054   gtk_combo_box_set_active_internal (combo_box, index);
3055 }
3056
3057 static void
3058 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3059                                    gint         index)
3060 {
3061   GtkTreePath *path;
3062
3063   combo_box->priv->active_item = index;
3064
3065   if (index == -1)
3066     {
3067       if (combo_box->priv->tree_view)
3068         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3069       else
3070         {
3071           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3072
3073           if (GTK_IS_MENU (menu))
3074             gtk_menu_set_active (menu, -1);
3075         }
3076
3077       if (combo_box->priv->cell_view)
3078         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3079     }
3080   else
3081     {
3082       path = gtk_tree_path_new_from_indices (index, -1);
3083
3084       if (combo_box->priv->tree_view)
3085         gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3086       else
3087         {
3088           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3089
3090           if (GTK_IS_MENU (menu))
3091             gtk_menu_set_active (GTK_MENU (menu), index);
3092         }
3093
3094       if (combo_box->priv->cell_view)
3095         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3096
3097       gtk_tree_path_free (path);
3098     }
3099
3100   g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3101 }
3102
3103
3104 /**
3105  * gtk_combo_box_get_active_iter:
3106  * @combo_box: A #GtkComboBox
3107  * @iter: The uninitialized #GtkTreeIter.
3108  * 
3109  * Sets @iter to point to the current active item, if it exists.
3110  * 
3111  * Return value: %TRUE, if @iter was set
3112  *
3113  * Since: 2.4
3114  **/
3115 gboolean
3116 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
3117                                GtkTreeIter     *iter)
3118 {
3119   GtkTreePath *path;
3120   gint active;
3121   gboolean retval;
3122
3123   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3124
3125   active = gtk_combo_box_get_active (combo_box);
3126   if (active < 0)
3127     return FALSE;
3128
3129   path = gtk_tree_path_new_from_indices (active, -1);
3130   retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3131                                     iter, path);
3132   gtk_tree_path_free (path);
3133
3134   return retval;
3135 }
3136
3137 /**
3138  * gtk_combo_box_set_active_iter:
3139  * @combo_box: A #GtkComboBox
3140  * @iter: The #GtkTreeIter.
3141  * 
3142  * Sets the current active item to be the one referenced by @iter. 
3143  * @iter must correspond to a path of depth one.
3144  * 
3145  * Since: 2.4
3146  **/
3147 void
3148 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
3149                                GtkTreeIter     *iter)
3150 {
3151   GtkTreePath *path;
3152
3153   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3154
3155   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3156   g_return_if_fail (path != NULL);
3157   g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3158   
3159   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3160   gtk_tree_path_free (path);
3161 }
3162
3163 /**
3164  * gtk_combo_box_set_model:
3165  * @combo_box: A #GtkComboBox.
3166  * @model: A #GtkTreeModel.
3167  *
3168  * Sets the model used by @combo_box to be @model. Will unset a
3169  * previously set model (if applicable).
3170  *
3171  * Since: 2.4
3172  */
3173 void
3174 gtk_combo_box_set_model (GtkComboBox  *combo_box,
3175                          GtkTreeModel *model)
3176 {
3177   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3178   g_return_if_fail (GTK_IS_TREE_MODEL (model));
3179
3180   if (model == combo_box->priv->model)
3181     return;
3182   
3183   if (combo_box->priv->model)
3184     gtk_combo_box_unset_model (combo_box);
3185
3186   combo_box->priv->model = model;
3187   g_object_ref (G_OBJECT (combo_box->priv->model));
3188
3189   combo_box->priv->inserted_id =
3190     g_signal_connect (combo_box->priv->model, "row_inserted",
3191                       G_CALLBACK (gtk_combo_box_model_row_inserted),
3192                       combo_box);
3193   combo_box->priv->deleted_id =
3194     g_signal_connect (combo_box->priv->model, "row_deleted",
3195                       G_CALLBACK (gtk_combo_box_model_row_deleted),
3196                       combo_box);
3197   combo_box->priv->reordered_id =
3198     g_signal_connect (combo_box->priv->model, "rows_reordered",
3199                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
3200                       combo_box);
3201   combo_box->priv->changed_id =
3202     g_signal_connect (combo_box->priv->model, "row_changed",
3203                       G_CALLBACK (gtk_combo_box_model_row_changed),
3204                       combo_box);
3205       
3206   if (combo_box->priv->tree_view)
3207     {
3208       /* list mode */
3209       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3210                                combo_box->priv->model);
3211     }
3212   else
3213     {
3214       /* menu mode */
3215       if (combo_box->priv->popup_widget)
3216         gtk_combo_box_menu_fill (combo_box);
3217
3218     }
3219
3220   if (combo_box->priv->cell_view)
3221     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3222                              combo_box->priv->model);
3223 }
3224
3225 /**
3226  * gtk_combo_box_get_model
3227  * @combo_box: A #GtkComboBox.
3228  *
3229  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3230  *
3231  * Return value: A #GtkTreeModel which was passed during construction.
3232  *
3233  * Since: 2.4
3234  */
3235 GtkTreeModel *
3236 gtk_combo_box_get_model (GtkComboBox *combo_box)
3237 {
3238   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3239
3240   return combo_box->priv->model;
3241 }
3242
3243
3244 /* convenience API for simple text combos */
3245
3246 /**
3247  * gtk_combo_box_new_text:
3248  *
3249  * Convenience function which constructs a new text combo box, which is a
3250  * #GtkComboBox just displaying strings. If you use this function to create
3251  * a text combo box, you should only manipulate its data source with the
3252  * following convenience functions: gtk_combo_box_append_text(),
3253  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3254  * gtk_combo_box_remove_text().
3255  *
3256  * Return value: A new text combo box.
3257  *
3258  * Since: 2.4
3259  */
3260 GtkWidget *
3261 gtk_combo_box_new_text (void)
3262 {
3263   GtkWidget *combo_box;
3264   GtkCellRenderer *cell;
3265   GtkListStore *store;
3266
3267   store = gtk_list_store_new (1, G_TYPE_STRING);
3268
3269   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3270
3271   cell = gtk_cell_renderer_text_new ();
3272   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3273   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3274                                   "text", 0,
3275                                   NULL);
3276
3277   return combo_box;
3278 }
3279
3280 /**
3281  * gtk_combo_box_append_text:
3282  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3283  * @text: A string.
3284  *
3285  * Appends @string to the list of strings stored in @combo_box. Note that
3286  * you can only use this function with combo boxes constructed with
3287  * gtk_combo_box_new_text().
3288  *
3289  * Since: 2.4
3290  */
3291 void
3292 gtk_combo_box_append_text (GtkComboBox *combo_box,
3293                            const gchar *text)
3294 {
3295   GtkTreeIter iter;
3296   GtkListStore *store;
3297
3298   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3299   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3300   g_return_if_fail (text != NULL);
3301
3302   store = GTK_LIST_STORE (combo_box->priv->model);
3303
3304   gtk_list_store_append (store, &iter);
3305   gtk_list_store_set (store, &iter, 0, text, -1);
3306 }
3307
3308 /**
3309  * gtk_combo_box_insert_text:
3310  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3311  * @position: An index to insert @text.
3312  * @text: A string.
3313  *
3314  * Inserts @string at @position in the list of strings stored in @combo_box.
3315  * Note that you can only use this function with combo boxes constructed
3316  * with gtk_combo_box_new_text().
3317  *
3318  * Since: 2.4
3319  */
3320 void
3321 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3322                            gint         position,
3323                            const gchar *text)
3324 {
3325   GtkTreeIter iter;
3326   GtkListStore *store;
3327
3328   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3329   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3330   g_return_if_fail (position >= 0);
3331   g_return_if_fail (text != NULL);
3332
3333   store = GTK_LIST_STORE (combo_box->priv->model);
3334
3335   gtk_list_store_insert (store, &iter, position);
3336   gtk_list_store_set (store, &iter, 0, text, -1);
3337 }
3338
3339 /**
3340  * gtk_combo_box_prepend_text:
3341  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3342  * @text: A string.
3343  *
3344  * Prepends @string to the list of strings stored in @combo_box. Note that
3345  * you can only use this function with combo boxes constructed with
3346  * gtk_combo_box_new_text().
3347  *
3348  * Since: 2.4
3349  */
3350 void
3351 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3352                             const gchar *text)
3353 {
3354   GtkTreeIter iter;
3355   GtkListStore *store;
3356
3357   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3358   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3359   g_return_if_fail (text != NULL);
3360
3361   store = GTK_LIST_STORE (combo_box->priv->model);
3362
3363   gtk_list_store_prepend (store, &iter);
3364   gtk_list_store_set (store, &iter, 0, text, -1);
3365 }
3366
3367 /**
3368  * gtk_combo_box_remove_text:
3369  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3370  * @position: Index of the item to remove.
3371  *
3372  * Removes the string at @position from @combo_box. Note that you can only use
3373  * this function with combo boxes constructed with gtk_combo_box_new_text().
3374  *
3375  * Since: 2.4
3376  */
3377 void
3378 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3379                            gint         position)
3380 {
3381   GtkTreeIter iter;
3382   GtkListStore *store;
3383
3384   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3385   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3386   g_return_if_fail (position >= 0);
3387
3388   store = GTK_LIST_STORE (combo_box->priv->model);
3389
3390   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3391                                      NULL, position))
3392     gtk_list_store_remove (store, &iter);
3393 }
3394
3395
3396 static gboolean
3397 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3398                                  gboolean   group_cycling)
3399 {
3400   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3401
3402   gtk_widget_grab_focus (combo_box->priv->button);
3403
3404   return TRUE;
3405 }
3406
3407 static void
3408 gtk_combo_box_destroy (GtkObject *object)
3409 {
3410   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3411
3412   combo_box->priv->destroying = 1;
3413
3414   GTK_OBJECT_CLASS (parent_class)->destroy (object);
3415
3416   combo_box->priv->cell_view = NULL;
3417
3418   combo_box->priv->destroying = 0;
3419 }
3420
3421 static void
3422 gtk_combo_box_finalize (GObject *object)
3423 {
3424   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3425   GSList *i;
3426   
3427   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3428     gtk_combo_box_menu_destroy (combo_box);
3429   
3430   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3431     gtk_combo_box_list_destroy (combo_box);
3432
3433   if (combo_box->priv->popup_window)
3434     gtk_widget_destroy (combo_box->priv->popup_window);
3435
3436   gtk_combo_box_unset_model (combo_box);
3437
3438   if (combo_box->priv->model)
3439     g_object_unref (combo_box->priv->model);
3440
3441   for (i = combo_box->priv->cells; i; i = i->next)
3442     {
3443       ComboCellInfo *info = (ComboCellInfo *)i->data;
3444       GSList *list = info->attributes;
3445
3446       if (info->destroy)
3447         info->destroy (info->func_data);
3448
3449       while (list && list->next)
3450         {
3451           g_free (list->data);
3452           list = list->next->next;
3453         }
3454       g_slist_free (info->attributes);
3455
3456       g_object_unref (G_OBJECT (info->cell));
3457       g_free (info);
3458     }
3459    g_slist_free (combo_box->priv->cells);
3460
3461    G_OBJECT_CLASS (parent_class)->finalize (object);
3462 }