]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Don't unref model if it is NULL. (#139770)
[~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   if (combo_box->priv->model)
1487     {
1488       g_object_unref (G_OBJECT (combo_box->priv->model));
1489       combo_box->priv->model = NULL;
1490     }
1491 }
1492
1493 static void
1494 gtk_combo_box_forall (GtkContainer *container,
1495                       gboolean      include_internals,
1496                       GtkCallback   callback,
1497                       gpointer      callback_data)
1498 {
1499   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1500
1501   if (include_internals)
1502     {
1503       if (combo_box->priv->button)
1504         (* callback) (combo_box->priv->button, callback_data);
1505       if (combo_box->priv->separator)
1506         (* callback) (combo_box->priv->separator, callback_data);
1507       if (combo_box->priv->arrow)
1508         (* callback) (combo_box->priv->arrow, callback_data);
1509       if (combo_box->priv->cell_view_frame)
1510         (* callback) (combo_box->priv->cell_view_frame, callback_data);
1511     }
1512
1513   if (GTK_BIN (container)->child)
1514     (* callback) (GTK_BIN (container)->child, callback_data);
1515 }
1516
1517 static gboolean
1518 gtk_combo_box_expose_event (GtkWidget      *widget,
1519                             GdkEventExpose *event)
1520 {
1521   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1522
1523   if (!combo_box->priv->tree_view)
1524     {
1525       gtk_container_propagate_expose (GTK_CONTAINER (widget),
1526                                       combo_box->priv->button, event);
1527
1528       if (combo_box->priv->separator)
1529         {
1530           gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1531                                           combo_box->priv->separator, event);
1532
1533           /* if not in this case, arrow gets its expose event from button */
1534           gtk_container_propagate_expose (GTK_CONTAINER (combo_box->priv->button),
1535                                           combo_box->priv->arrow, event);
1536         }
1537     }
1538   else
1539     {
1540       gtk_container_propagate_expose (GTK_CONTAINER (widget),
1541                                       combo_box->priv->button, event);
1542
1543       if (combo_box->priv->cell_view_frame)
1544         gtk_container_propagate_expose (GTK_CONTAINER (widget),
1545                                         combo_box->priv->cell_view_frame, event);
1546     }
1547
1548   gtk_container_propagate_expose (GTK_CONTAINER (widget),
1549                                   GTK_BIN (widget)->child, event);
1550
1551   return FALSE;
1552 }
1553
1554 static gboolean
1555 gtk_combo_box_scroll_event (GtkWidget          *widget,
1556                             GdkEventScroll     *event)
1557 {
1558   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1559   gint index;
1560   gint items;
1561     
1562   index = gtk_combo_box_get_active (combo_box);
1563
1564   if (index != -1)
1565     {
1566       items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1567       
1568       if (event->direction == GDK_SCROLL_UP)
1569         index--;
1570       else 
1571         index++;
1572
1573       gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1574     }
1575
1576   return TRUE;
1577 }
1578
1579 /*
1580  * menu style
1581  */
1582
1583 static void
1584 cell_view_sync_cells (GtkComboBox *combo_box,
1585                       GtkCellView *cell_view)
1586 {
1587   GSList *k;
1588
1589   for (k = combo_box->priv->cells; k; k = k->next)
1590     {
1591       GSList *j;
1592       ComboCellInfo *info = (ComboCellInfo *)k->data;
1593
1594       if (info->pack == GTK_PACK_START)
1595         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1596                                     info->cell, info->expand);
1597       else if (info->pack == GTK_PACK_END)
1598         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1599                                   info->cell, info->expand);
1600
1601       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1602                                           info->cell,
1603                                           info->func, info->func_data, NULL);
1604
1605       for (j = info->attributes; j; j = j->next->next)
1606         {
1607           gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1608                                          info->cell,
1609                                          j->data,
1610                                          GPOINTER_TO_INT (j->next->data));
1611         }
1612     }
1613 }
1614
1615 static void
1616 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1617                           gboolean     add_children)
1618 {
1619   GtkWidget *box;
1620
1621   if (combo_box->priv->cell_view)
1622     {
1623       combo_box->priv->button = gtk_toggle_button_new ();
1624       g_signal_connect (combo_box->priv->button, "toggled",
1625                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1626       g_signal_connect_after (combo_box->priv->button, "key_press_event",
1627                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
1628       gtk_widget_set_parent (combo_box->priv->button,
1629                              GTK_BIN (combo_box)->child->parent);
1630
1631       combo_box->priv->separator = gtk_vseparator_new ();
1632       gtk_widget_set_parent (combo_box->priv->separator,
1633                              combo_box->priv->button);
1634       gtk_widget_show (combo_box->priv->separator);
1635
1636       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1637       gtk_widget_set_parent (combo_box->priv->arrow, combo_box->priv->button);
1638       gtk_widget_show (combo_box->priv->arrow);
1639
1640       gtk_widget_show_all (combo_box->priv->button);
1641
1642       if (GTK_WIDGET_MAPPED (GTK_BIN (combo_box)->child))
1643         {
1644           /* I have no clue why, but we need to manually map in this case. */
1645           gtk_widget_map (combo_box->priv->separator);
1646           gtk_widget_map (combo_box->priv->arrow);
1647         }
1648     }
1649   else
1650     {
1651       combo_box->priv->button = gtk_toggle_button_new ();
1652       g_signal_connect (combo_box->priv->button, "toggled",
1653                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1654       g_signal_connect_after (combo_box, "key_press_event",
1655                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
1656       gtk_widget_set_parent (combo_box->priv->button,
1657                              GTK_BIN (combo_box)->child->parent);
1658
1659       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1660       gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1661                          combo_box->priv->arrow);
1662       gtk_widget_show_all (combo_box->priv->button);
1663     }
1664
1665   g_signal_connect (combo_box->priv->button, "button_press_event",
1666                     G_CALLBACK (gtk_combo_box_menu_button_press),
1667                     combo_box);
1668
1669   /* create our funky menu */
1670   box = gtk_menu_new ();
1671   g_signal_connect (box, "key_press_event",
1672                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1673   gtk_combo_box_set_popup_widget (combo_box, box);
1674
1675   /* add items */
1676   if (add_children)
1677     gtk_combo_box_menu_fill (combo_box);
1678
1679 }
1680
1681 static void
1682 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1683 {
1684   gint i, items;
1685   GtkWidget *menu;
1686   GtkWidget *tmp;
1687
1688   if (!combo_box->priv->model)
1689     return;
1690
1691   items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1692   menu = combo_box->priv->popup_widget;
1693
1694   for (i = 0; i < items; i++)
1695     {
1696       GtkTreePath *path;
1697
1698       path = gtk_tree_path_new_from_indices (i, -1);
1699       tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1700                                                     path);
1701       g_signal_connect (tmp, "activate",
1702                         G_CALLBACK (gtk_combo_box_menu_item_activate),
1703                         combo_box);
1704
1705       cell_view_sync_cells (combo_box,
1706                             GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1707
1708       gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1709
1710       if (combo_box->priv->wrap_width)
1711         gtk_combo_box_relayout_item (combo_box, i);
1712
1713       gtk_widget_show (tmp);
1714
1715       gtk_tree_path_free (path);
1716     }
1717 }
1718
1719 static void
1720 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1721 {
1722   g_signal_handlers_disconnect_matched (combo_box->priv->button,
1723                                         G_SIGNAL_MATCH_DATA,
1724                                         0, 0, NULL,
1725                                         gtk_combo_box_menu_button_press, NULL);
1726
1727   /* unparent will remove our latest ref */
1728   if (combo_box->priv->cell_view)
1729     {
1730       gtk_widget_unparent (combo_box->priv->arrow);
1731       combo_box->priv->arrow = NULL;
1732
1733       gtk_widget_unparent (combo_box->priv->separator);
1734       combo_box->priv->separator = NULL;
1735
1736       gtk_widget_unparent (combo_box->priv->button);
1737       combo_box->priv->button = NULL;
1738     }
1739   else
1740     {
1741       /* will destroy the arrow too */
1742       gtk_widget_unparent (combo_box->priv->button);
1743
1744       combo_box->priv->button = NULL;
1745       combo_box->priv->arrow = NULL;
1746     }
1747
1748   /* changing the popup window will unref the menu and the children */
1749 }
1750
1751 /*
1752  * grid
1753  */
1754
1755 static void
1756 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1757                              gint         index,
1758                              gint        *cols,
1759                              gint        *rows)
1760 {
1761   GtkTreeIter iter;
1762
1763   gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1764
1765   if (cols)
1766     {
1767       if (combo_box->priv->col_column == -1)
1768         *cols = 1;
1769       else
1770         gtk_tree_model_get (combo_box->priv->model, &iter,
1771                             combo_box->priv->col_column, cols,
1772                             -1);
1773     }
1774
1775   if (rows)
1776     {
1777       if (combo_box->priv->row_column == -1)
1778         *rows = 1;
1779       else
1780         gtk_tree_model_get (combo_box->priv->model, &iter,
1781                             combo_box->priv->row_column, rows,
1782                             -1);
1783     }
1784 }
1785
1786 static gboolean
1787 menu_occupied (GtkMenu *menu,
1788                guint    left_attach,
1789                guint    right_attach,
1790                guint    top_attach,
1791                guint    bottom_attach)
1792 {
1793   GList *i;
1794
1795   g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1796   g_return_val_if_fail (left_attach < right_attach, TRUE);
1797   g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1798
1799   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1800     {
1801       guint l, r, b, t;
1802       gboolean h_intersect = FALSE;
1803       gboolean v_intersect = FALSE;
1804
1805       gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1806                                "left_attach", &l,
1807                                "right_attach", &r,
1808                                "bottom_attach", &b,
1809                                "top_attach", &t,
1810                                NULL);
1811
1812       /* look if this item intersects with the given coordinates */
1813       h_intersect  = left_attach <= l && l <= right_attach;
1814       h_intersect &= left_attach <= r && r <= right_attach;
1815
1816       v_intersect  = top_attach <= t && t <= bottom_attach;
1817       v_intersect &= top_attach <= b && b <= bottom_attach;
1818
1819       if (h_intersect && v_intersect)
1820         return TRUE;
1821     }
1822
1823   return FALSE;
1824 }
1825
1826 static void
1827 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1828                              gint         index)
1829 {
1830   gint current_col = 0, current_row = 0;
1831   gint rows, cols;
1832   GList *list;
1833   GtkWidget *item;
1834   GtkWidget *menu;
1835
1836   menu = combo_box->priv->popup_widget;
1837   if (!GTK_IS_MENU_SHELL (menu))
1838     return;
1839
1840   list = gtk_container_get_children (GTK_CONTAINER (menu));
1841   item = g_list_nth_data (list, index);
1842   g_list_free (list);
1843
1844   gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1845
1846   /* look for a good spot */
1847   while (1)
1848     {
1849       if (current_col + cols > combo_box->priv->wrap_width)
1850         {
1851           current_col = 0;
1852           current_row++;
1853         }
1854
1855       if (!menu_occupied (GTK_MENU (menu),
1856                           current_col, current_col + cols,
1857                           current_row, current_row + rows))
1858         break;
1859
1860       current_col++;
1861     }
1862
1863   /* set attach props */
1864   gtk_menu_attach (GTK_MENU (menu), item,
1865                    current_col, current_col + cols,
1866                    current_row, current_row + rows);
1867 }
1868
1869 static void
1870 gtk_combo_box_relayout (GtkComboBox *combo_box)
1871 {
1872   GList *list, *j;
1873   GtkWidget *menu;
1874
1875   /* do nothing unless we are in menu style */
1876   if (combo_box->priv->tree_view)
1877     return;
1878
1879   menu = combo_box->priv->popup_widget;
1880
1881   /* get rid of all children */
1882   g_return_if_fail (GTK_IS_MENU_SHELL (menu));
1883
1884   list = gtk_container_get_children (GTK_CONTAINER (menu));
1885
1886   for (j = g_list_last (list); j; j = j->prev)
1887     gtk_container_remove (GTK_CONTAINER (menu), j->data);
1888
1889   g_list_free (list);
1890
1891   /* and relayout */
1892   gtk_combo_box_menu_fill (combo_box);
1893 }
1894
1895 /* callbacks */
1896 static gboolean
1897 gtk_combo_box_menu_button_press (GtkWidget      *widget,
1898                                  GdkEventButton *event,
1899                                  gpointer        user_data)
1900 {
1901   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1902
1903   if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1904     return FALSE;
1905
1906   if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1907     {
1908       combo_box->priv->popup_in_progress = TRUE;
1909       
1910       gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1911                            combo_box->priv->active_item);
1912
1913       gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1914                       NULL, NULL,
1915                       gtk_combo_box_menu_position, combo_box,
1916                       event->button, event->time);
1917
1918       return TRUE;
1919     }
1920
1921   return FALSE;
1922 }
1923
1924 static void
1925 gtk_combo_box_menu_item_activate (GtkWidget *item,
1926                                   gpointer   user_data)
1927 {
1928   gint index;
1929   GtkWidget *menu;
1930   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1931
1932   menu = combo_box->priv->popup_widget;
1933   g_return_if_fail (GTK_IS_MENU (menu));
1934
1935   index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1936
1937   gtk_combo_box_set_active (combo_box, index);
1938 }
1939
1940 static void
1941 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
1942                                   GtkTreePath      *path,
1943                                   GtkTreeIter      *iter,
1944                                   gpointer          user_data)
1945 {
1946   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1947   gint index = gtk_tree_path_get_indices (path)[0];
1948
1949   if (combo_box->priv->active_item >= index)
1950     combo_box->priv->active_item++;
1951
1952   if (!combo_box->priv->tree_view)
1953     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1954 }
1955
1956 static void
1957 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
1958                                  GtkTreePath      *path,
1959                                  gpointer          user_data)
1960 {
1961   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1962   gint index = gtk_tree_path_get_indices (path)[0];
1963
1964   if (!combo_box->priv->tree_view)
1965     gtk_combo_box_menu_row_deleted (model, path, user_data);
1966   
1967   if (index == combo_box->priv->active_item)
1968     {
1969       gint items = gtk_tree_model_iter_n_children (model, NULL);
1970
1971       if (items == 0)
1972         gtk_combo_box_set_active_internal (combo_box, -1);
1973       else if (index == items)
1974         gtk_combo_box_set_active_internal (combo_box, index - 1);
1975       else
1976         gtk_combo_box_set_active_internal (combo_box, index);
1977     }
1978   else if (combo_box->priv->active_item > index)
1979     combo_box->priv->active_item--;
1980 }
1981
1982 static void
1983 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
1984                                     GtkTreePath     *path,
1985                                     GtkTreeIter     *iter,
1986                                     gint            *new_order,
1987                                     gpointer         user_data)
1988 {
1989   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1990   gint items = gtk_tree_model_iter_n_children (model, NULL);
1991   gint i;
1992
1993   for (i = 0; i < items; i++)
1994     if (new_order[i] == combo_box->priv->active_item)
1995       {
1996         combo_box->priv->active_item = i;
1997         break;
1998       }
1999
2000   if (!combo_box->priv->tree_view)
2001     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2002 }
2003                                                     
2004 static void
2005 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
2006                                  GtkTreePath      *path,
2007                                  GtkTreeIter      *iter,
2008                                  gpointer          user_data)
2009 {
2010   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2011   gint index = gtk_tree_path_get_indices (path)[0];
2012
2013   if (index == combo_box->priv->active_item &&
2014       combo_box->priv->cell_view)
2015     gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2016   
2017   if (combo_box->priv->tree_view)
2018     gtk_combo_box_list_row_changed (model, path, iter, user_data);
2019   else
2020     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2021 }
2022
2023
2024 static void
2025 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2026                                  GtkTreePath  *path,
2027                                  GtkTreeIter  *iter,
2028                                  gpointer      user_data)
2029 {
2030   GtkWidget *menu;
2031   GtkWidget *item;
2032   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2033
2034   if (!combo_box->priv->popup_widget)
2035     return;
2036
2037   menu = combo_box->priv->popup_widget;
2038   g_return_if_fail (GTK_IS_MENU (menu));
2039
2040   item = gtk_cell_view_menu_item_new_from_model (model, path);
2041   g_signal_connect (item, "activate",
2042                     G_CALLBACK (gtk_combo_box_menu_item_activate),
2043                     combo_box);
2044
2045   cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2046
2047   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2048                          gtk_tree_path_get_indices (path)[0]);
2049   gtk_widget_show (item);
2050 }
2051
2052 static void
2053 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2054                                 GtkTreePath  *path,
2055                                 gpointer      user_data)
2056 {
2057   gint index;
2058   GtkWidget *menu;
2059   GtkWidget *item;
2060   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2061
2062   if (!combo_box->priv->popup_widget)
2063     return;
2064
2065   index = gtk_tree_path_get_indices (path)[0];
2066
2067   menu = combo_box->priv->popup_widget;
2068   g_return_if_fail (GTK_IS_MENU (menu));
2069
2070   item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2071   g_return_if_fail (GTK_IS_MENU_ITEM (item));
2072
2073   gtk_container_remove (GTK_CONTAINER (menu), item);
2074 }
2075
2076 static void
2077 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
2078                                     GtkTreePath      *path,
2079                                     GtkTreeIter      *iter,
2080                                     gint             *new_order,
2081                                     gpointer          user_data)
2082 {
2083   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2084
2085   gtk_combo_box_relayout (combo_box);
2086 }
2087                                     
2088 static void
2089 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2090                                 GtkTreePath  *path,
2091                                 GtkTreeIter  *iter,
2092                                 gpointer      user_data)
2093 {
2094   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2095   gint width;
2096
2097   if (!combo_box->priv->popup_widget)
2098     return;
2099
2100   if (combo_box->priv->wrap_width)
2101     gtk_combo_box_relayout_item (combo_box,
2102                                  gtk_tree_path_get_indices (path)[0]);
2103
2104   width = gtk_combo_box_calc_requested_width (combo_box, path);
2105
2106   if (width > combo_box->priv->width)
2107     {
2108       if (combo_box->priv->cell_view)
2109         {
2110           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2111           gtk_widget_queue_resize (combo_box->priv->cell_view);
2112         }
2113       combo_box->priv->width = width;
2114     }
2115 }
2116
2117 /*
2118  * list style
2119  */
2120
2121 static void
2122 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2123 {
2124   GSList *i;
2125   GtkTreeSelection *sel;
2126
2127   combo_box->priv->button = gtk_toggle_button_new ();
2128   gtk_widget_set_parent (combo_box->priv->button,
2129                          GTK_BIN (combo_box)->child->parent);
2130   g_signal_connect (combo_box->priv->button, "button_press_event",
2131                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2132   g_signal_connect (combo_box->priv->button, "toggled",
2133                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2134   g_signal_connect_after (combo_box, "key_press_event",
2135                           G_CALLBACK (gtk_combo_box_key_press), combo_box);
2136
2137   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2138   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2139                      combo_box->priv->arrow);
2140   combo_box->priv->separator = NULL;
2141   gtk_widget_show_all (combo_box->priv->button);
2142
2143   if (combo_box->priv->cell_view)
2144     {
2145       combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2146       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2147                              GTK_BIN (combo_box)->child->parent);
2148       gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2149                                  GTK_SHADOW_IN);
2150
2151       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
2152                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2153
2154       gtk_widget_show (combo_box->priv->cell_view_frame);
2155     }
2156
2157   combo_box->priv->tree_view = gtk_tree_view_new ();
2158   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2159   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2160   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2161                                      FALSE);
2162   if (combo_box->priv->model)
2163     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2164                              combo_box->priv->model);
2165     
2166   g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2167                     G_CALLBACK (gtk_combo_box_list_button_pressed),
2168                     combo_box);
2169   g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2170                     G_CALLBACK (gtk_combo_box_list_button_released),
2171                     combo_box);
2172   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2173                     G_CALLBACK (gtk_combo_box_list_key_press),
2174                     combo_box);
2175
2176   combo_box->priv->column = gtk_tree_view_column_new ();
2177   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2178                                combo_box->priv->column);
2179
2180   /* sync up */
2181   for (i = combo_box->priv->cells; i; i = i->next)
2182     {
2183       GSList *j;
2184       ComboCellInfo *info = (ComboCellInfo *)i->data;
2185
2186       if (info->pack == GTK_PACK_START)
2187         gtk_tree_view_column_pack_start (combo_box->priv->column,
2188                                          info->cell, info->expand);
2189       else if (info->pack == GTK_PACK_END)
2190         gtk_tree_view_column_pack_end (combo_box->priv->column,
2191                                        info->cell, info->expand);
2192
2193       for (j = info->attributes; j; j = j->next->next)
2194         {
2195           gtk_tree_view_column_add_attribute (combo_box->priv->column,
2196                                               info->cell,
2197                                               j->data,
2198                                               GPOINTER_TO_INT (j->next->data));
2199         }
2200     }
2201
2202   if (combo_box->priv->active_item != -1)
2203     {
2204       GtkTreePath *path;
2205
2206       path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2207       if (path)
2208         {
2209           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2210                                     path, NULL, FALSE);
2211           gtk_tree_path_free (path);
2212         }
2213     }
2214
2215   /* set sample/popup widgets */
2216   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2217
2218   gtk_widget_show (combo_box->priv->tree_view);
2219 }
2220
2221 static void
2222 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2223 {
2224   /* disconnect signals */
2225   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2226                                         G_SIGNAL_MATCH_DATA,
2227                                         0, 0, NULL, NULL, combo_box);
2228   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2229                                         G_SIGNAL_MATCH_DATA,
2230                                         0, 0, NULL,
2231                                         gtk_combo_box_list_button_pressed,
2232                                         NULL);
2233
2234   /* destroy things (unparent will kill the latest ref from us)
2235    * last unref on button will destroy the arrow
2236    */
2237   gtk_widget_unparent (combo_box->priv->button);
2238   combo_box->priv->button = NULL;
2239   combo_box->priv->arrow = NULL;
2240
2241   if (combo_box->priv->cell_view)
2242     {
2243       g_object_set (G_OBJECT (combo_box->priv->cell_view),
2244                     "background_set", FALSE,
2245                     NULL);
2246
2247       gtk_widget_unparent (combo_box->priv->cell_view_frame);
2248       combo_box->priv->cell_view_frame = NULL;
2249     }
2250
2251   gtk_widget_destroy (combo_box->priv->tree_view);
2252
2253   combo_box->priv->tree_view = NULL;
2254   combo_box->priv->popup_widget = NULL;
2255 }
2256
2257 /* callbacks */
2258 static void
2259 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2260 {
2261   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2262     gtk_grab_remove (combo_box->priv->tree_view);
2263
2264   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2265     {
2266       gtk_grab_remove (combo_box->priv->popup_window);
2267       gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2268       gdk_pointer_ungrab (GDK_CURRENT_TIME);
2269     }
2270 }
2271
2272 static gboolean
2273 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
2274                                    GdkEventButton *event,
2275                                    gpointer        data)
2276 {
2277   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2278
2279   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2280
2281   if (ewidget == combo_box->priv->tree_view)
2282     return TRUE;
2283
2284   if ((ewidget != combo_box->priv->button) ||
2285       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2286     return FALSE;
2287
2288   gtk_combo_box_popup (combo_box);
2289
2290   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2291                                 TRUE);
2292
2293   combo_box->priv->popup_in_progress = TRUE;
2294
2295   return TRUE;
2296 }
2297
2298 static gboolean
2299 gtk_combo_box_list_button_released (GtkWidget      *widget,
2300                                     GdkEventButton *event,
2301                                     gpointer        data)
2302 {
2303   gboolean ret;
2304   GtkTreePath *path = NULL;
2305
2306   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2307
2308   gboolean popup_in_progress = FALSE;
2309
2310   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2311
2312   if (combo_box->priv->popup_in_progress)
2313     {
2314       popup_in_progress = TRUE;
2315       combo_box->priv->popup_in_progress = FALSE;
2316     }
2317
2318   if (ewidget != combo_box->priv->tree_view)
2319     {
2320       if (ewidget == combo_box->priv->button &&
2321           !popup_in_progress &&
2322           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2323         {
2324           gtk_combo_box_popdown (combo_box);
2325           return TRUE;
2326         }
2327
2328       /* released outside treeview */
2329       if (ewidget != combo_box->priv->button)
2330         {
2331           gtk_combo_box_popdown (combo_box);
2332
2333           return TRUE;
2334         }
2335
2336       return FALSE;
2337     }
2338
2339   /* select something cool */
2340   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2341                                        event->x, event->y,
2342                                        &path,
2343                                        NULL, NULL, NULL);
2344
2345   if (!ret)
2346     return TRUE; /* clicked outside window? */
2347
2348   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2349   gtk_combo_box_popdown (combo_box);
2350
2351   gtk_tree_path_free (path);
2352
2353   return TRUE;
2354 }
2355
2356 static gboolean
2357 gtk_combo_box_key_press (GtkWidget   *widget,
2358                          GdkEventKey *event,
2359                          gpointer     data)
2360 {
2361   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2362   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2363   gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2364   gint index = gtk_combo_box_get_active (combo_box);
2365   gint new_index;
2366
2367   if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) && 
2368       state == GDK_MOD1_MASK)
2369     {
2370       gtk_combo_box_popup (combo_box);
2371
2372       return TRUE;
2373     }
2374
2375   switch (event->keyval) 
2376     {
2377     case GDK_Down:
2378     case GDK_KP_Down:
2379       new_index = index + 1;
2380       break;
2381     case GDK_Up:
2382     case GDK_KP_Up:
2383       new_index = index - 1;
2384       break;
2385     case GDK_Page_Up:
2386     case GDK_KP_Page_Up:
2387     case GDK_Home: 
2388     case GDK_KP_Home:
2389       new_index = 0;
2390       break;
2391     case GDK_Page_Down:
2392     case GDK_KP_Page_Down:
2393     case GDK_End: 
2394     case GDK_KP_End:
2395       new_index = items - 1;
2396       break;
2397     default:
2398       return FALSE;
2399     }
2400   
2401   gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2402
2403   return TRUE;
2404 }
2405
2406 static gboolean
2407 gtk_combo_box_menu_key_press (GtkWidget   *widget,
2408                               GdkEventKey *event,
2409                               gpointer     data)
2410 {
2411   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2412   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2413
2414   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
2415       state == GDK_MOD1_MASK)
2416     {
2417       gtk_combo_box_popdown (combo_box);
2418
2419       return TRUE;
2420     }
2421   
2422   return FALSE;
2423 }
2424
2425 static gboolean
2426 gtk_combo_box_list_key_press (GtkWidget   *widget,
2427                               GdkEventKey *event,
2428                               gpointer     data)
2429 {
2430   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2431   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2432
2433   if (event->keyval == GDK_Escape ||
2434       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
2435        state == GDK_MOD1_MASK))
2436     {
2437       /* reset active item -- this is incredibly lame and ugly */
2438       gtk_combo_box_set_active (combo_box,
2439                                 gtk_combo_box_get_active (combo_box));
2440       
2441       gtk_combo_box_popdown (combo_box);
2442       
2443       return TRUE;
2444     }
2445     
2446   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2447       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
2448   {
2449     gboolean ret;
2450     GtkTreeIter iter;
2451     GtkTreeModel *model = NULL;
2452     GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2453     
2454     ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2455     if (ret)
2456       {
2457         GtkTreePath *path;
2458         
2459         path = gtk_tree_model_get_path (model, &iter);
2460         if (path)
2461           {
2462             gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2463             gtk_tree_path_free (path);
2464           }
2465       }
2466
2467     gtk_combo_box_popdown (combo_box);
2468     
2469     return TRUE;
2470   }
2471
2472   return FALSE;
2473 }
2474
2475 static void
2476 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2477                                 GtkTreePath  *path,
2478                                 GtkTreeIter  *iter,
2479                                 gpointer      data)
2480 {
2481   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2482   gint width;
2483
2484   width = gtk_combo_box_calc_requested_width (combo_box, path);
2485
2486   if (width > combo_box->priv->width)
2487     {
2488       if (combo_box->priv->cell_view) 
2489         {
2490           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2491           gtk_widget_queue_resize (combo_box->priv->cell_view);
2492         }
2493       combo_box->priv->width = width;
2494     }
2495 }
2496
2497 /*
2498  * GtkCellLayout implementation
2499  */
2500 static void
2501 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
2502                                       GtkCellRenderer *cell,
2503                                       gboolean         expand)
2504 {
2505   ComboCellInfo *info;
2506   GtkComboBox *combo_box;
2507   GtkWidget *menu;
2508
2509   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2510   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2511
2512   combo_box = GTK_COMBO_BOX (layout);
2513
2514   g_object_ref (G_OBJECT (cell));
2515   gtk_object_sink (GTK_OBJECT (cell));
2516
2517   info = g_new0 (ComboCellInfo, 1);
2518   info->cell = cell;
2519   info->expand = expand;
2520   info->pack = GTK_PACK_START;
2521
2522   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2523
2524   if (combo_box->priv->cell_view)
2525     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2526                                 cell, expand);
2527
2528   if (combo_box->priv->column)
2529     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2530
2531   menu = combo_box->priv->popup_widget;
2532   if (GTK_IS_MENU (menu))
2533     {
2534       GList *i, *list;
2535
2536       list = gtk_container_get_children (GTK_CONTAINER (menu));
2537       for (i = list; i; i = i->next)
2538         {
2539           GtkCellView *view;
2540
2541           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2542             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2543           else
2544             view = GTK_CELL_VIEW (i->data);
2545
2546           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2547         }
2548       g_list_free (list);
2549     }
2550 }
2551
2552 static void
2553 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
2554                                     GtkCellRenderer *cell,
2555                                     gboolean         expand)
2556 {
2557   ComboCellInfo *info;
2558   GtkComboBox *combo_box;
2559   GtkWidget *menu;
2560
2561   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2562   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2563
2564   combo_box = GTK_COMBO_BOX (layout);
2565
2566   g_object_ref (G_OBJECT (cell));
2567   gtk_object_sink (GTK_OBJECT (cell));
2568
2569   info = g_new0 (ComboCellInfo, 1);
2570   info->cell = cell;
2571   info->expand = expand;
2572   info->pack = GTK_PACK_END;
2573
2574   if (combo_box->priv->cell_view)
2575     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2576                               cell, expand);
2577
2578   if (combo_box->priv->column)
2579     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2580
2581   menu = combo_box->priv->popup_widget;
2582   if (GTK_IS_MENU (menu))
2583     {
2584       GList *i, *list;
2585
2586       list = gtk_container_get_children (GTK_CONTAINER (menu));
2587       for (i = list; i; i = i->next)
2588         {
2589           GtkCellView *view;
2590
2591           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2592             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2593           else
2594             view = GTK_CELL_VIEW (i->data);
2595
2596           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2597         }
2598       g_list_free (list);
2599     }
2600 }
2601
2602 static void
2603 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2604 {
2605   GtkWidget *menu;
2606   GtkComboBox *combo_box;
2607   GSList *i;
2608   
2609   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2610
2611   combo_box = GTK_COMBO_BOX (layout);
2612  
2613   if (combo_box->priv->cell_view)
2614     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2615
2616   if (combo_box->priv->column)
2617     gtk_tree_view_column_clear (combo_box->priv->column);
2618
2619   for (i = combo_box->priv->cells; i; i = i->next)
2620     {
2621      ComboCellInfo *info = (ComboCellInfo *)i->data;
2622
2623       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2624       g_object_unref (G_OBJECT (info->cell));
2625       g_free (info);
2626       i->data = NULL;
2627     }
2628   g_slist_free (combo_box->priv->cells);
2629   combo_box->priv->cells = NULL;
2630
2631   menu = combo_box->priv->popup_widget;
2632   if (GTK_IS_MENU (menu))
2633     {
2634       GList *i, *list;
2635
2636       list = gtk_container_get_children (GTK_CONTAINER (menu));
2637       for (i = list; i; i = i->next)
2638         {
2639           GtkCellView *view;
2640
2641           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2642             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2643           else
2644             view = GTK_CELL_VIEW (i->data);
2645
2646           gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2647         }
2648       g_list_free (list);
2649     }
2650 }
2651
2652 static void
2653 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
2654                                          GtkCellRenderer *cell,
2655                                          const gchar     *attribute,
2656                                          gint             column)
2657 {
2658   ComboCellInfo *info;
2659   GtkComboBox *combo_box;
2660   GtkWidget *menu;
2661
2662   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2663   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2664
2665   combo_box = GTK_COMBO_BOX (layout);
2666
2667   info = gtk_combo_box_get_cell_info (combo_box, cell);
2668
2669   info->attributes = g_slist_prepend (info->attributes,
2670                                       GINT_TO_POINTER (column));
2671   info->attributes = g_slist_prepend (info->attributes,
2672                                       g_strdup (attribute));
2673
2674   if (combo_box->priv->cell_view)
2675     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2676                                    cell, attribute, column);
2677
2678   if (combo_box->priv->column)
2679     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2680                                    cell, attribute, column);
2681
2682   menu = combo_box->priv->popup_widget;
2683   if (GTK_IS_MENU (menu))
2684     {
2685       GList *i, *list;
2686
2687       list = gtk_container_get_children (GTK_CONTAINER (menu));
2688       for (i = list; i; i = i->next)
2689         {
2690           GtkCellView *view;
2691
2692           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2693             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2694           else
2695             view = GTK_CELL_VIEW (i->data);
2696
2697           gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2698                                          attribute, column);
2699         }
2700       g_list_free (list);
2701     }
2702
2703   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2704 }
2705
2706 static void
2707 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
2708                                               GtkCellRenderer       *cell,
2709                                               GtkCellLayoutDataFunc  func,
2710                                               gpointer               func_data,
2711                                               GDestroyNotify         destroy)
2712 {
2713   ComboCellInfo *info;
2714   GtkComboBox *combo_box;
2715   GtkWidget *menu;
2716
2717   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2718
2719   combo_box = GTK_COMBO_BOX (layout);
2720
2721   info = gtk_combo_box_get_cell_info (combo_box, cell);
2722   g_return_if_fail (info != NULL);
2723
2724   if (info->destroy)
2725     {
2726       GDestroyNotify d = info->destroy;
2727
2728       info->destroy = NULL;
2729       d (info->func_data);
2730     }
2731
2732   info->func = func;
2733   info->func_data = func_data;
2734   info->destroy = destroy;
2735
2736   if (combo_box->priv->cell_view)
2737     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2738
2739   if (combo_box->priv->column)
2740     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2741
2742   menu = combo_box->priv->popup_widget;
2743   if (GTK_IS_MENU (menu))
2744     {
2745       GList *i, *list;
2746
2747       list = gtk_container_get_children (GTK_CONTAINER (menu));
2748       for (i = list; i; i = i->next)
2749         {
2750           GtkCellView *view;
2751
2752           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2753             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2754           else
2755             view = GTK_CELL_VIEW (i->data);
2756
2757           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2758                                               func, func_data, NULL);
2759         }
2760       g_list_free (list);
2761     }
2762
2763   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2764 }
2765
2766 static void
2767 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
2768                                             GtkCellRenderer *cell)
2769 {
2770   ComboCellInfo *info;
2771   GtkComboBox *combo_box;
2772   GtkWidget *menu;
2773   GSList *list;
2774
2775   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2776   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2777
2778   combo_box = GTK_COMBO_BOX (layout);
2779
2780   info = gtk_combo_box_get_cell_info (combo_box, cell);
2781   g_return_if_fail (info != NULL);
2782
2783   list = info->attributes;
2784   while (list && list->next)
2785     {
2786       g_free (list->data);
2787       list = list->next->next;
2788     }
2789   g_slist_free (info->attributes);
2790   info->attributes = NULL;
2791
2792   if (combo_box->priv->cell_view)
2793     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2794
2795   if (combo_box->priv->column)
2796     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2797
2798   menu = combo_box->priv->popup_widget;
2799   if (GTK_IS_MENU (menu))
2800     {
2801       GList *i, *list;
2802
2803       list = gtk_container_get_children (GTK_CONTAINER (menu));
2804       for (i = list; i; i = i->next)
2805         {
2806           GtkCellView *view;
2807
2808           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2809             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2810           else
2811             view = GTK_CELL_VIEW (i->data);
2812
2813           gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2814         }
2815       g_list_free (list);
2816     }
2817
2818   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2819 }
2820
2821 static void
2822 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
2823                                    GtkCellRenderer *cell,
2824                                    gint             position)
2825 {
2826   ComboCellInfo *info;
2827   GtkComboBox *combo_box;
2828   GtkWidget *menu;
2829   GSList *link;
2830
2831   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2832   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2833
2834   combo_box = GTK_COMBO_BOX (layout);
2835
2836   info = gtk_combo_box_get_cell_info (combo_box, cell);
2837
2838   g_return_if_fail (info != NULL);
2839   g_return_if_fail (position >= 0);
2840
2841   link = g_slist_find (combo_box->priv->cells, info);
2842
2843   g_return_if_fail (link != NULL);
2844
2845   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2846   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2847                                            position);
2848
2849   if (combo_box->priv->cell_view)
2850     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2851                              cell, position);
2852
2853   if (combo_box->priv->column)
2854     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2855                              cell, position);
2856
2857   menu = combo_box->priv->popup_widget;
2858   if (GTK_IS_MENU (menu))
2859     {
2860       GList *i, *list;
2861
2862       list = gtk_container_get_children (GTK_CONTAINER (menu));
2863       for (i = list; i; i = i->next)
2864         {
2865           GtkCellView *view;
2866
2867           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2868             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2869           else
2870             view = GTK_CELL_VIEW (i->data);
2871
2872           gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2873         }
2874       g_list_free (list);
2875     }
2876
2877   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2878 }
2879
2880 /*
2881  * public API
2882  */
2883
2884 /**
2885  * gtk_combo_box_new:
2886  *
2887  * Creates a new empty #GtkComboBox.
2888  *
2889  * Return value: A new #GtkComboBox.
2890  *
2891  * Since: 2.4
2892  */
2893 GtkWidget *
2894 gtk_combo_box_new (void)
2895 {
2896   return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2897 }
2898
2899 /**
2900  * gtk_combo_box_new_with_model:
2901  * @model: A #GtkTreeModel.
2902  *
2903  * Creates a new #GtkComboBox with the model initialized to @model.
2904  *
2905  * Return value: A new #GtkComboBox.
2906  *
2907  * Since: 2.4
2908  */
2909 GtkWidget *
2910 gtk_combo_box_new_with_model (GtkTreeModel *model)
2911 {
2912   GtkComboBox *combo_box;
2913
2914   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2915
2916   combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2917                                            "model", model,
2918                                            NULL));
2919
2920   return GTK_WIDGET (combo_box);
2921 }
2922
2923 /**
2924  * gtk_combo_box_set_wrap_width:
2925  * @combo_box: A #GtkComboBox.
2926  * @width: Preferred number of columns.
2927  *
2928  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2929  * the preferred number of columns when you want to the popup to be layed out
2930  * in a table.
2931  *
2932  * Since: 2.4
2933  */
2934 void
2935 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2936                               gint         width)
2937 {
2938   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2939   g_return_if_fail (width >= 0);
2940
2941   if (width != combo_box->priv->wrap_width)
2942     {
2943       combo_box->priv->wrap_width = width;
2944
2945       gtk_combo_box_check_appearance (combo_box);
2946       gtk_combo_box_relayout (combo_box);
2947       
2948       g_object_notify (G_OBJECT (combo_box), "wrap_width");
2949     }
2950 }
2951
2952 /**
2953  * gtk_combo_box_set_row_span_column:
2954  * @combo_box: A #GtkComboBox.
2955  * @row_span: A column in the model passed during construction.
2956  *
2957  * Sets the column with row span information for @combo_box to be @row_span.
2958  * The row span column contains integers which indicate how many rows
2959  * an item should span.
2960  *
2961  * Since: 2.4
2962  */
2963 void
2964 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2965                                    gint         row_span)
2966 {
2967   gint col;
2968
2969   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2970
2971   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2972   g_return_if_fail (row_span >= 0 && row_span < col);
2973
2974   if (row_span != combo_box->priv->row_column)
2975     {
2976       combo_box->priv->row_column = row_span;
2977       
2978       gtk_combo_box_relayout (combo_box);
2979  
2980       g_object_notify (G_OBJECT (combo_box), "row_span_column");
2981     }
2982 }
2983
2984 /**
2985  * gtk_combo_box_set_column_span_column:
2986  * @combo_box: A #GtkComboBox.
2987  * @column_span: A column in the model passed during construction.
2988  *
2989  * Sets the column with column span information for @combo_box to be
2990  * @column_span. The column span column contains integers which indicate
2991  * how many columns an item should span.
2992  *
2993  * Since: 2.4
2994  */
2995 void
2996 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2997                                       gint         column_span)
2998 {
2999   gint col;
3000
3001   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3002
3003   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3004   g_return_if_fail (column_span >= 0 && column_span < col);
3005
3006   if (column_span != combo_box->priv->col_column)
3007     {
3008       combo_box->priv->col_column = column_span;
3009       
3010       gtk_combo_box_relayout (combo_box);
3011
3012       g_object_notify (G_OBJECT (combo_box), "column_span_column");
3013     }
3014 }
3015
3016 /**
3017  * gtk_combo_box_get_active:
3018  * @combo_box: A #GtkComboBox.
3019  *
3020  * Returns the index of the currently active item, or -1 if there's no
3021  * active item.
3022  *
3023  * Return value: An integer which is the index of the currently active item, or
3024  * -1 if there's no active item.
3025  *
3026  * Since: 2.4
3027  */
3028 gint
3029 gtk_combo_box_get_active (GtkComboBox *combo_box)
3030 {
3031   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3032
3033   return combo_box->priv->active_item;
3034 }
3035
3036 /**
3037  * gtk_combo_box_set_active:
3038  * @combo_box: A #GtkComboBox.
3039  * @index: An index in the model passed during construction, or -1 to have
3040  * no active item.
3041  *
3042  * Sets the active item of @combo_box to be the item at @index.
3043  *
3044  * Since: 2.4
3045  */
3046 void
3047 gtk_combo_box_set_active (GtkComboBox *combo_box,
3048                           gint         index)
3049 {
3050   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3051   /* -1 means "no item selected" */
3052   g_return_if_fail (index >= -1);
3053
3054   if (combo_box->priv->active_item == index)
3055     return;
3056   
3057   gtk_combo_box_set_active_internal (combo_box, index);
3058 }
3059
3060 static void
3061 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3062                                    gint         index)
3063 {
3064   GtkTreePath *path;
3065
3066   combo_box->priv->active_item = index;
3067
3068   if (index == -1)
3069     {
3070       if (combo_box->priv->tree_view)
3071         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3072       else
3073         {
3074           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3075
3076           if (GTK_IS_MENU (menu))
3077             gtk_menu_set_active (menu, -1);
3078         }
3079
3080       if (combo_box->priv->cell_view)
3081         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3082     }
3083   else
3084     {
3085       path = gtk_tree_path_new_from_indices (index, -1);
3086
3087       if (combo_box->priv->tree_view)
3088         gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3089       else
3090         {
3091           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3092
3093           if (GTK_IS_MENU (menu))
3094             gtk_menu_set_active (GTK_MENU (menu), index);
3095         }
3096
3097       if (combo_box->priv->cell_view)
3098         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3099
3100       gtk_tree_path_free (path);
3101     }
3102
3103   g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3104 }
3105
3106
3107 /**
3108  * gtk_combo_box_get_active_iter:
3109  * @combo_box: A #GtkComboBox
3110  * @iter: The uninitialized #GtkTreeIter.
3111  * 
3112  * Sets @iter to point to the current active item, if it exists.
3113  * 
3114  * Return value: %TRUE, if @iter was set
3115  *
3116  * Since: 2.4
3117  **/
3118 gboolean
3119 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
3120                                GtkTreeIter     *iter)
3121 {
3122   GtkTreePath *path;
3123   gint active;
3124   gboolean retval;
3125
3126   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3127
3128   active = gtk_combo_box_get_active (combo_box);
3129   if (active < 0)
3130     return FALSE;
3131
3132   path = gtk_tree_path_new_from_indices (active, -1);
3133   retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3134                                     iter, path);
3135   gtk_tree_path_free (path);
3136
3137   return retval;
3138 }
3139
3140 /**
3141  * gtk_combo_box_set_active_iter:
3142  * @combo_box: A #GtkComboBox
3143  * @iter: The #GtkTreeIter.
3144  * 
3145  * Sets the current active item to be the one referenced by @iter. 
3146  * @iter must correspond to a path of depth one.
3147  * 
3148  * Since: 2.4
3149  **/
3150 void
3151 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
3152                                GtkTreeIter     *iter)
3153 {
3154   GtkTreePath *path;
3155
3156   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3157
3158   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3159   g_return_if_fail (path != NULL);
3160   g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3161   
3162   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3163   gtk_tree_path_free (path);
3164 }
3165
3166 /**
3167  * gtk_combo_box_set_model:
3168  * @combo_box: A #GtkComboBox.
3169  * @model: A #GtkTreeModel.
3170  *
3171  * Sets the model used by @combo_box to be @model. Will unset a
3172  * previously set model (if applicable).
3173  *
3174  * Since: 2.4
3175  */
3176 void
3177 gtk_combo_box_set_model (GtkComboBox  *combo_box,
3178                          GtkTreeModel *model)
3179 {
3180   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3181   g_return_if_fail (GTK_IS_TREE_MODEL (model));
3182
3183   if (model == combo_box->priv->model)
3184     return;
3185   
3186   if (combo_box->priv->model)
3187     gtk_combo_box_unset_model (combo_box);
3188
3189   combo_box->priv->model = model;
3190   g_object_ref (G_OBJECT (combo_box->priv->model));
3191
3192   combo_box->priv->inserted_id =
3193     g_signal_connect (combo_box->priv->model, "row_inserted",
3194                       G_CALLBACK (gtk_combo_box_model_row_inserted),
3195                       combo_box);
3196   combo_box->priv->deleted_id =
3197     g_signal_connect (combo_box->priv->model, "row_deleted",
3198                       G_CALLBACK (gtk_combo_box_model_row_deleted),
3199                       combo_box);
3200   combo_box->priv->reordered_id =
3201     g_signal_connect (combo_box->priv->model, "rows_reordered",
3202                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
3203                       combo_box);
3204   combo_box->priv->changed_id =
3205     g_signal_connect (combo_box->priv->model, "row_changed",
3206                       G_CALLBACK (gtk_combo_box_model_row_changed),
3207                       combo_box);
3208       
3209   if (combo_box->priv->tree_view)
3210     {
3211       /* list mode */
3212       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3213                                combo_box->priv->model);
3214     }
3215   else
3216     {
3217       /* menu mode */
3218       if (combo_box->priv->popup_widget)
3219         gtk_combo_box_menu_fill (combo_box);
3220
3221     }
3222
3223   if (combo_box->priv->cell_view)
3224     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3225                              combo_box->priv->model);
3226 }
3227
3228 /**
3229  * gtk_combo_box_get_model
3230  * @combo_box: A #GtkComboBox.
3231  *
3232  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3233  *
3234  * Return value: A #GtkTreeModel which was passed during construction.
3235  *
3236  * Since: 2.4
3237  */
3238 GtkTreeModel *
3239 gtk_combo_box_get_model (GtkComboBox *combo_box)
3240 {
3241   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3242
3243   return combo_box->priv->model;
3244 }
3245
3246
3247 /* convenience API for simple text combos */
3248
3249 /**
3250  * gtk_combo_box_new_text:
3251  *
3252  * Convenience function which constructs a new text combo box, which is a
3253  * #GtkComboBox just displaying strings. If you use this function to create
3254  * a text combo box, you should only manipulate its data source with the
3255  * following convenience functions: gtk_combo_box_append_text(),
3256  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3257  * gtk_combo_box_remove_text().
3258  *
3259  * Return value: A new text combo box.
3260  *
3261  * Since: 2.4
3262  */
3263 GtkWidget *
3264 gtk_combo_box_new_text (void)
3265 {
3266   GtkWidget *combo_box;
3267   GtkCellRenderer *cell;
3268   GtkListStore *store;
3269
3270   store = gtk_list_store_new (1, G_TYPE_STRING);
3271
3272   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3273
3274   cell = gtk_cell_renderer_text_new ();
3275   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3276   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3277                                   "text", 0,
3278                                   NULL);
3279
3280   return combo_box;
3281 }
3282
3283 /**
3284  * gtk_combo_box_append_text:
3285  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3286  * @text: A string.
3287  *
3288  * Appends @string to the list of strings stored in @combo_box. Note that
3289  * you can only use this function with combo boxes constructed with
3290  * gtk_combo_box_new_text().
3291  *
3292  * Since: 2.4
3293  */
3294 void
3295 gtk_combo_box_append_text (GtkComboBox *combo_box,
3296                            const gchar *text)
3297 {
3298   GtkTreeIter iter;
3299   GtkListStore *store;
3300
3301   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3302   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3303   g_return_if_fail (text != NULL);
3304
3305   store = GTK_LIST_STORE (combo_box->priv->model);
3306
3307   gtk_list_store_append (store, &iter);
3308   gtk_list_store_set (store, &iter, 0, text, -1);
3309 }
3310
3311 /**
3312  * gtk_combo_box_insert_text:
3313  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3314  * @position: An index to insert @text.
3315  * @text: A string.
3316  *
3317  * Inserts @string at @position in the list of strings stored in @combo_box.
3318  * Note that you can only use this function with combo boxes constructed
3319  * with gtk_combo_box_new_text().
3320  *
3321  * Since: 2.4
3322  */
3323 void
3324 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3325                            gint         position,
3326                            const gchar *text)
3327 {
3328   GtkTreeIter iter;
3329   GtkListStore *store;
3330
3331   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3332   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3333   g_return_if_fail (position >= 0);
3334   g_return_if_fail (text != NULL);
3335
3336   store = GTK_LIST_STORE (combo_box->priv->model);
3337
3338   gtk_list_store_insert (store, &iter, position);
3339   gtk_list_store_set (store, &iter, 0, text, -1);
3340 }
3341
3342 /**
3343  * gtk_combo_box_prepend_text:
3344  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3345  * @text: A string.
3346  *
3347  * Prepends @string to the list of strings stored in @combo_box. Note that
3348  * you can only use this function with combo boxes constructed with
3349  * gtk_combo_box_new_text().
3350  *
3351  * Since: 2.4
3352  */
3353 void
3354 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3355                             const gchar *text)
3356 {
3357   GtkTreeIter iter;
3358   GtkListStore *store;
3359
3360   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3361   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3362   g_return_if_fail (text != NULL);
3363
3364   store = GTK_LIST_STORE (combo_box->priv->model);
3365
3366   gtk_list_store_prepend (store, &iter);
3367   gtk_list_store_set (store, &iter, 0, text, -1);
3368 }
3369
3370 /**
3371  * gtk_combo_box_remove_text:
3372  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3373  * @position: Index of the item to remove.
3374  *
3375  * Removes the string at @position from @combo_box. Note that you can only use
3376  * this function with combo boxes constructed with gtk_combo_box_new_text().
3377  *
3378  * Since: 2.4
3379  */
3380 void
3381 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3382                            gint         position)
3383 {
3384   GtkTreeIter iter;
3385   GtkListStore *store;
3386
3387   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3388   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3389   g_return_if_fail (position >= 0);
3390
3391   store = GTK_LIST_STORE (combo_box->priv->model);
3392
3393   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3394                                      NULL, position))
3395     gtk_list_store_remove (store, &iter);
3396 }
3397
3398
3399 static gboolean
3400 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3401                                  gboolean   group_cycling)
3402 {
3403   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3404
3405   gtk_widget_grab_focus (combo_box->priv->button);
3406
3407   return TRUE;
3408 }
3409
3410 static void
3411 gtk_combo_box_destroy (GtkObject *object)
3412 {
3413   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3414
3415   combo_box->priv->destroying = 1;
3416
3417   GTK_OBJECT_CLASS (parent_class)->destroy (object);
3418
3419   combo_box->priv->cell_view = NULL;
3420
3421   combo_box->priv->destroying = 0;
3422 }
3423
3424 static void
3425 gtk_combo_box_finalize (GObject *object)
3426 {
3427   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3428   GSList *i;
3429   
3430   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3431     gtk_combo_box_menu_destroy (combo_box);
3432   
3433   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3434     gtk_combo_box_list_destroy (combo_box);
3435
3436   if (combo_box->priv->popup_window)
3437     gtk_widget_destroy (combo_box->priv->popup_window);
3438
3439   gtk_combo_box_unset_model (combo_box);
3440
3441   if (combo_box->priv->model)
3442     g_object_unref (combo_box->priv->model);
3443
3444   for (i = combo_box->priv->cells; i; i = i->next)
3445     {
3446       ComboCellInfo *info = (ComboCellInfo *)i->data;
3447       GSList *list = info->attributes;
3448
3449       if (info->destroy)
3450         info->destroy (info->func_data);
3451
3452       while (list && list->next)
3453         {
3454           g_free (list->data);
3455           list = list->next->next;
3456         }
3457       g_slist_free (info->attributes);
3458
3459       g_object_unref (G_OBJECT (info->cell));
3460       g_free (info);
3461     }
3462    g_slist_free (combo_box->priv->cells);
3463
3464    G_OBJECT_CLASS (parent_class)->finalize (object);
3465 }