]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Make all style properties readonly.
[~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 "gtkhbox.h"
32 #include "gtkliststore.h"
33 #include "gtkmain.h"
34 #include "gtkmenu.h"
35 #include "gtktogglebutton.h"
36 #include "gtktreeselection.h"
37 #include "gtkvseparator.h"
38 #include "gtkwindow.h"
39
40 #include <gdk/gdkkeysyms.h>
41
42 #include <gobject/gvaluecollector.h>
43
44 #include <string.h>
45 #include <stdarg.h>
46
47 #include "gtkmarshalers.h"
48 #include "gtkintl.h"
49
50
51 /* WELCOME, to THE house of evil code */
52
53 typedef struct _ComboCellInfo ComboCellInfo;
54 struct _ComboCellInfo
55 {
56   GtkCellRenderer *cell;
57   GSList *attributes;
58
59   GtkCellLayoutDataFunc func;
60   gpointer func_data;
61   GDestroyNotify destroy;
62
63   guint expand : 1;
64   guint pack : 1;
65 };
66
67 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
68
69 struct _GtkComboBoxPrivate
70 {
71   GtkTreeModel *model;
72
73   gint col_column;
74   gint row_column;
75
76   gint wrap_width;
77
78   gint active_item;
79
80   GtkWidget *tree_view;
81   GtkTreeViewColumn *column;
82
83   GtkWidget *cell_view;
84   GtkWidget *cell_view_frame;
85
86   GtkWidget *button;
87   GtkWidget *box;
88   GtkWidget *arrow;
89   GtkWidget *separator;
90
91   GtkWidget *popup_widget;
92   GtkWidget *popup_window;
93   GtkWidget *popup_frame;
94
95   guint inserted_id;
96   guint deleted_id;
97   guint reordered_id;
98   guint changed_id;
99
100   gint width;
101   GSList *cells;
102
103   guint popup_in_progress : 1;
104   guint destroying : 1;
105 };
106
107 /* While debugging this evil code, I have learned that
108  * there are actually 4 modes to this widget, which can
109  * be characterized as follows
110  * 
111  * 1) menu mode, no child added
112  *
113  * tree_view -> NULL
114  * cell_view -> GtkCellView, regular child
115  * cell_view_frame -> NULL
116  * button -> GtkToggleButton set_parent to combo
117  * arrow -> GtkArrow set_parent to button
118  * separator -> GtkVSepator set_parent to button
119  * popup_widget -> GtkMenu
120  * popup_window -> NULL
121  * popup_frame -> NULL
122  *
123  * 2) menu mode, child added
124  * 
125  * tree_view -> NULL
126  * cell_view -> NULL 
127  * cell_view_frame -> NULL
128  * button -> GtkToggleButton set_parent to combo
129  * arrow -> GtkArrow, child of button
130  * separator -> NULL
131  * popup_widget -> GtkMenu
132  * popup_window -> NULL
133  * popup_frame -> NULL
134  *
135  * 3) list mode, no child added
136  * 
137  * tree_view -> GtkTreeView, child of popup_frame
138  * cell_view -> GtkCellView, regular child
139  * cell_view_frame -> GtkFrame, set parent to combo
140  * button -> GtkToggleButton, set_parent to combo
141  * arrow -> GtkArrow, child of button
142  * separator -> NULL
143  * popup_widget -> tree_view
144  * popup_window -> GtkWindow
145  * popup_frame -> GtkFrame, child of popup_window
146  *
147  * 4) list mode, child added
148  *
149  * tree_view -> GtkTreeView, child of popup_frame
150  * cell_view -> NULL
151  * cell_view_frame -> NULL
152  * button -> GtkToggleButton, set_parent to combo
153  * arrow -> GtkArrow, child of button
154  * separator -> NULL
155  * popup_widget -> tree_view
156  * popup_window -> GtkWindow
157  * popup_frame -> GtkFrame, child of popup_window
158  * 
159  */
160
161 enum {
162   CHANGED,
163   LAST_SIGNAL
164 };
165
166 enum {
167   PROP_0,
168   PROP_MODEL,
169   PROP_WRAP_WIDTH,
170   PROP_ROW_SPAN_COLUMN,
171   PROP_COLUMN_SPAN_COLUMN,
172   PROP_ACTIVE
173 };
174
175 static GtkBinClass *parent_class = NULL;
176 static guint combo_box_signals[LAST_SIGNAL] = {0,};
177
178 #define BONUS_PADDING 4
179
180
181 /* common */
182 static void     gtk_combo_box_class_init           (GtkComboBoxClass *klass);
183 static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
184 static void     gtk_combo_box_init                 (GtkComboBox      *combo_box);
185 static void     gtk_combo_box_finalize             (GObject          *object);
186 static void     gtk_combo_box_destroy              (GtkObject        *object);
187
188 static void     gtk_combo_box_set_property         (GObject         *object,
189                                                     guint            prop_id,
190                                                     const GValue    *value,
191                                                     GParamSpec      *spec);
192 static void     gtk_combo_box_get_property         (GObject         *object,
193                                                     guint            prop_id,
194                                                     GValue          *value,
195                                                     GParamSpec      *spec);
196
197 static void     gtk_combo_box_state_changed        (GtkWidget        *widget,
198                                                     GtkStateType      previous);
199 static void     gtk_combo_box_style_set            (GtkWidget       *widget,
200                                                     GtkStyle        *previous);
201 static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
202                                                     gpointer         data);
203 static void     gtk_combo_box_add                  (GtkContainer    *container,
204                                                     GtkWidget       *widget);
205 static void     gtk_combo_box_remove               (GtkContainer    *container,
206                                                     GtkWidget       *widget);
207
208 static ComboCellInfo *gtk_combo_box_get_cell_info  (GtkComboBox      *combo_box,
209                                                     GtkCellRenderer  *cell);
210
211 static void     gtk_combo_box_menu_show            (GtkWidget        *menu,
212                                                     gpointer          user_data);
213 static void     gtk_combo_box_menu_hide            (GtkWidget        *menu,
214                                                     gpointer          user_data);
215
216 static void     gtk_combo_box_set_popup_widget     (GtkComboBox      *combo_box,
217                                                     GtkWidget        *popup);
218 static void     gtk_combo_box_menu_position_below  (GtkMenu          *menu,
219                                                     gint             *x,
220                                                     gint             *y,
221                                                     gint             *push_in,
222                                                     gpointer          user_data);
223 static void     gtk_combo_box_menu_position_over   (GtkMenu          *menu,
224                                                     gint             *x,
225                                                     gint             *y,
226                                                     gint             *push_in,
227                                                     gpointer          user_data);
228 static void     gtk_combo_box_menu_position        (GtkMenu          *menu,
229                                                     gint             *x,
230                                                     gint             *y,
231                                                     gint             *push_in,
232                                                     gpointer          user_data);
233
234 static gint     gtk_combo_box_calc_requested_width (GtkComboBox      *combo_box,
235                                                     GtkTreePath      *path);
236 static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
237
238 static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);
239
240 static void     gtk_combo_box_size_request         (GtkWidget        *widget,
241                                                     GtkRequisition   *requisition);
242 static void     gtk_combo_box_size_allocate        (GtkWidget        *widget,
243                                                     GtkAllocation    *allocation);
244 static void     gtk_combo_box_forall               (GtkContainer     *container,
245                                                     gboolean          include_internals,
246                                                     GtkCallback       callback,
247                                                     gpointer          callback_data);
248 static gboolean gtk_combo_box_expose_event         (GtkWidget        *widget,
249                                                     GdkEventExpose   *event);
250 static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
251                                                     GdkEventScroll   *event);
252 static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
253                                                     gint              index);
254 static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
255                                                     GdkEventKey      *event,
256                                                     gpointer          data);
257
258 /* listening to the model */
259 static void     gtk_combo_box_model_row_inserted   (GtkTreeModel     *model,
260                                                     GtkTreePath      *path,
261                                                     GtkTreeIter      *iter,
262                                                     gpointer          user_data);
263 static void     gtk_combo_box_model_row_deleted    (GtkTreeModel     *model,
264                                                     GtkTreePath      *path,
265                                                     gpointer          user_data);
266 static void     gtk_combo_box_model_rows_reordered (GtkTreeModel     *model,
267                                                     GtkTreePath      *path,
268                                                     GtkTreeIter      *iter,
269                                                     gint             *new_order,
270                                                     gpointer          user_data);
271 static void     gtk_combo_box_model_row_changed    (GtkTreeModel     *model,
272                                                     GtkTreePath      *path,
273                                                     GtkTreeIter      *iter,
274                                                     gpointer          data);
275
276 /* list */
277 static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
278                                                     gint             *x, 
279                                                     gint             *y, 
280                                                     gint             *width,
281                                                     gint             *height);
282 static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
283 static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);
284
285 static void     gtk_combo_box_list_remove_grabs    (GtkComboBox      *combo_box);
286
287 static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
288                                                     GdkEventButton   *event,
289                                                     gpointer          data);
290 static gboolean gtk_combo_box_list_key_press       (GtkWidget        *widget,
291                                                     GdkEventKey      *event,
292                                                     gpointer          data);
293 static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
294                                                     GdkEventButton   *event,
295                                                     gpointer          data);
296
297 static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
298                                                     GtkTreePath      *path,
299                                                     GtkTreeIter      *iter,
300                                                     gpointer          data);
301
302 /* menu */
303 static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
304                                                     gboolean          add_children);
305 static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
306 static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);
307
308 static void     gtk_combo_box_item_get_size        (GtkComboBox      *combo_box,
309                                                     gint              index,
310                                                     gint             *cols,
311                                                     gint             *rows);
312 static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
313                                                     gint              index);
314 static void     gtk_combo_box_relayout             (GtkComboBox      *combo_box);
315
316 static gboolean gtk_combo_box_menu_button_press    (GtkWidget        *widget,
317                                                     GdkEventButton   *event,
318                                                     gpointer          user_data);
319 static void     gtk_combo_box_menu_item_activate   (GtkWidget        *item,
320                                                     gpointer          user_data);
321 static void     gtk_combo_box_menu_row_inserted    (GtkTreeModel     *model,
322                                                     GtkTreePath      *path,
323                                                     GtkTreeIter      *iter,
324                                                     gpointer          user_data);
325 static void     gtk_combo_box_menu_row_deleted     (GtkTreeModel     *model,
326                                                     GtkTreePath      *path,
327                                                     gpointer          user_data);
328 static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
329                                                     GtkTreePath      *path,
330                                                     GtkTreeIter      *iter,
331                                                     gint             *new_order,
332                                                     gpointer          user_data);
333 static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
334                                                     GtkTreePath      *path,
335                                                     GtkTreeIter      *iter,
336                                                     gpointer          data);
337 static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
338                                                     GdkEventKey      *event,
339                                                     gpointer          data);
340 static void     gtk_combo_box_menu_state_changed   (GtkWidget        *widget,
341                                                     GtkStateType      previous,
342                                                     gpointer          data);
343
344 /* cell layout */
345 static void     gtk_combo_box_cell_layout_pack_start         (GtkCellLayout         *layout,
346                                                               GtkCellRenderer       *cell,
347                                                               gboolean               expand);
348 static void     gtk_combo_box_cell_layout_pack_end           (GtkCellLayout         *layout,
349                                                               GtkCellRenderer       *cell,
350                                                               gboolean               expand);
351 static void     gtk_combo_box_cell_layout_clear              (GtkCellLayout         *layout);
352 static void     gtk_combo_box_cell_layout_add_attribute      (GtkCellLayout         *layout,
353                                                               GtkCellRenderer       *cell,
354                                                               const gchar           *attribute,
355                                                               gint                   column);
356 static void     gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
357                                                               GtkCellRenderer       *cell,
358                                                               GtkCellLayoutDataFunc  func,
359                                                               gpointer               func_data,
360                                                               GDestroyNotify         destroy);
361 static void     gtk_combo_box_cell_layout_clear_attributes   (GtkCellLayout         *layout,
362                                                               GtkCellRenderer       *cell);
363 static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
364                                                               GtkCellRenderer       *cell,
365                                                               gint                   position);
366 static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
367                                                               gboolean      group_cycling);
368
369 static void     cell_view_sync_cells (GtkComboBox *combo_box,
370                                       GtkCellView *cell_view);
371
372 GType
373 gtk_combo_box_get_type (void)
374 {
375   static GType combo_box_type = 0;
376
377   if (!combo_box_type)
378     {
379       static const GTypeInfo combo_box_info =
380         {
381           sizeof (GtkComboBoxClass),
382           NULL, /* base_init */
383           NULL, /* base_finalize */
384           (GClassInitFunc) gtk_combo_box_class_init,
385           NULL, /* class_finalize */
386           NULL, /* class_data */
387           sizeof (GtkComboBox),
388           0,
389           (GInstanceInitFunc) gtk_combo_box_init
390         };
391
392       static const GInterfaceInfo cell_layout_info =
393         {
394           (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
395           NULL,
396           NULL
397         };
398
399       combo_box_type = g_type_register_static (GTK_TYPE_BIN,
400                                                "GtkComboBox",
401                                                &combo_box_info,
402                                                0);
403
404       g_type_add_interface_static (combo_box_type,
405                                    GTK_TYPE_CELL_LAYOUT,
406                                    &cell_layout_info);
407     }
408
409   return combo_box_type;
410 }
411
412 /* common */
413 static void
414 gtk_combo_box_class_init (GtkComboBoxClass *klass)
415 {
416   GObjectClass *object_class;
417   GtkBindingSet *binding_set;
418   GtkObjectClass *gtk_object_class;
419   GtkContainerClass *container_class;
420   GtkWidgetClass *widget_class;
421
422   binding_set = gtk_binding_set_by_class (klass);
423
424   container_class = (GtkContainerClass *)klass;
425   container_class->forall = gtk_combo_box_forall;
426   container_class->add = gtk_combo_box_add;
427   container_class->remove = gtk_combo_box_remove;
428
429   widget_class = (GtkWidgetClass *)klass;
430   widget_class->size_allocate = gtk_combo_box_size_allocate;
431   widget_class->size_request = gtk_combo_box_size_request;
432   widget_class->expose_event = gtk_combo_box_expose_event;
433   widget_class->scroll_event = gtk_combo_box_scroll_event;
434   widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
435   widget_class->style_set = gtk_combo_box_style_set;
436   widget_class->state_changed = gtk_combo_box_state_changed;
437
438   gtk_object_class = (GtkObjectClass *)klass;
439   gtk_object_class->destroy = gtk_combo_box_destroy;
440
441   object_class = (GObjectClass *)klass;
442   object_class->finalize = gtk_combo_box_finalize;
443   object_class->set_property = gtk_combo_box_set_property;
444   object_class->get_property = gtk_combo_box_get_property;
445
446   parent_class = g_type_class_peek_parent (klass);
447
448   /* signals */
449   combo_box_signals[CHANGED] =
450     g_signal_new ("changed",
451                   G_OBJECT_CLASS_TYPE (klass),
452                   G_SIGNAL_RUN_LAST,
453                   G_STRUCT_OFFSET (GtkComboBoxClass, changed),
454                   NULL, NULL,
455                   g_cclosure_marshal_VOID__VOID,
456                   G_TYPE_NONE, 0);
457
458   /* properties */
459   g_object_class_install_property (object_class,
460                                    PROP_MODEL,
461                                    g_param_spec_object ("model",
462                                                         P_("ComboBox model"),
463                                                         P_("The model for the combo box"),
464                                                         GTK_TYPE_TREE_MODEL,
465                                                         G_PARAM_READWRITE));
466
467   g_object_class_install_property (object_class,
468                                    PROP_WRAP_WIDTH,
469                                    g_param_spec_int ("wrap_width",
470                                                      P_("Wrap width"),
471                                                      P_("Wrap width for layouting the items in a grid"),
472                                                      0,
473                                                      G_MAXINT,
474                                                      0,
475                                                      G_PARAM_READWRITE));
476
477   g_object_class_install_property (object_class,
478                                    PROP_ROW_SPAN_COLUMN,
479                                    g_param_spec_int ("row_span_column",
480                                                      P_("Row span column"),
481                                                      P_("TreeModel column containing the row span values"),
482                                                      0,
483                                                      G_MAXINT,
484                                                      0,
485                                                      G_PARAM_READWRITE));
486
487   g_object_class_install_property (object_class,
488                                    PROP_COLUMN_SPAN_COLUMN,
489                                    g_param_spec_int ("column_span_column",
490                                                      P_("Column span column"),
491                                                      P_("TreeModel column containing the column span values"),
492                                                      0,
493                                                      G_MAXINT,
494                                                      0,
495                                                      G_PARAM_READWRITE));
496
497   g_object_class_install_property (object_class,
498                                    PROP_ACTIVE,
499                                    g_param_spec_int ("active",
500                                                      P_("Active item"),
501                                                      P_("The item which is currently active"),
502                                                      0,
503                                                      G_MAXINT,
504                                                      0,
505                                                      G_PARAM_READWRITE));
506
507   gtk_widget_class_install_style_property (widget_class,
508                                            g_param_spec_boolean ("appears-as-list",
509                                                                  P_("Appears as list"),
510                                                                  P_("Whether combobox dropdowns should look like lists rather than menus"),
511                                                                  FALSE,
512                                                                  G_PARAM_READABLE));
513
514   g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
515 }
516
517 static void
518 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
519 {
520   iface->pack_start = gtk_combo_box_cell_layout_pack_start;
521   iface->pack_end = gtk_combo_box_cell_layout_pack_end;
522   iface->clear = gtk_combo_box_cell_layout_clear;
523   iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
524   iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
525   iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
526   iface->reorder = gtk_combo_box_cell_layout_reorder;
527 }
528
529 static void
530 gtk_combo_box_init (GtkComboBox *combo_box)
531 {
532   combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
533
534   combo_box->priv->cell_view = gtk_cell_view_new ();
535   gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
536   GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
537   gtk_widget_show (combo_box->priv->cell_view);
538
539   combo_box->priv->width = 0;
540   combo_box->priv->wrap_width = 0;
541
542   combo_box->priv->active_item = -1;
543   combo_box->priv->col_column = -1;
544   combo_box->priv->row_column = -1;
545 }
546
547 static void
548 gtk_combo_box_set_property (GObject      *object,
549                             guint         prop_id,
550                             const GValue *value,
551                             GParamSpec   *pspec)
552 {
553   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
554
555   switch (prop_id)
556     {
557       case PROP_MODEL:
558         gtk_combo_box_set_model (combo_box, g_value_get_object (value));
559         break;
560
561       case PROP_WRAP_WIDTH:
562         gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
563         break;
564
565       case PROP_ROW_SPAN_COLUMN:
566         gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
567         break;
568
569       case PROP_COLUMN_SPAN_COLUMN:
570         gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
571         break;
572
573       case PROP_ACTIVE:
574         gtk_combo_box_set_active (combo_box, g_value_get_int (value));
575         break;
576
577       default:
578         break;
579     }
580 }
581
582 static void
583 gtk_combo_box_get_property (GObject    *object,
584                             guint       prop_id,
585                             GValue     *value,
586                             GParamSpec *pspec)
587 {
588   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
589
590   switch (prop_id)
591     {
592       case PROP_MODEL:
593         g_value_set_object (value, combo_box->priv->model);
594         break;
595
596       case PROP_WRAP_WIDTH:
597         g_value_set_int (value, combo_box->priv->wrap_width);
598         break;
599
600       case PROP_ROW_SPAN_COLUMN:
601         g_value_set_int (value, combo_box->priv->row_column);
602         break;
603
604       case PROP_COLUMN_SPAN_COLUMN:
605         g_value_set_int (value, combo_box->priv->col_column);
606         break;
607
608       case PROP_ACTIVE:
609         g_value_set_int (value, gtk_combo_box_get_active (combo_box));
610         break;
611
612       default:
613         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
614         break;
615     }
616 }
617
618 static void
619 gtk_combo_box_state_changed (GtkWidget    *widget,
620                              GtkStateType  previous)
621 {
622   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
623
624   if (GTK_WIDGET_REALIZED (widget))
625     {
626       if (combo_box->priv->tree_view && combo_box->priv->cell_view)
627         gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
628                                             &widget->style->base[GTK_WIDGET_STATE (widget)]);
629     }
630
631   gtk_widget_queue_draw (widget);
632 }
633
634 static void
635 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
636 {
637   gboolean appears_as_list;
638
639   /* if wrap_width > 0, then we are in grid-mode and forced to use
640    * unix style
641    */
642   if (combo_box->priv->wrap_width)
643     appears_as_list = FALSE;
644   else
645     gtk_widget_style_get (GTK_WIDGET (combo_box),
646                           "appears-as-list", &appears_as_list,
647                           NULL);
648
649   if (appears_as_list)
650     {
651       /* Destroy all the menu mode widgets, if they exist. */
652       if (GTK_IS_MENU (combo_box->priv->popup_widget))
653         gtk_combo_box_menu_destroy (combo_box);
654
655       /* Create the list mode widgets, if they don't already exist. */
656       if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
657         gtk_combo_box_list_setup (combo_box);
658     }
659   else
660     {
661       /* Destroy all the list mode widgets, if they exist. */
662       if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
663         gtk_combo_box_list_destroy (combo_box);
664
665       /* Create the menu mode widgets, if they don't already exist. */
666       if (!GTK_IS_MENU (combo_box->priv->popup_widget))
667         gtk_combo_box_menu_setup (combo_box, TRUE);
668     }
669 }
670
671 static void
672 gtk_combo_box_style_set (GtkWidget *widget,
673                          GtkStyle  *previous)
674 {
675   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
676
677   gtk_combo_box_check_appearance (combo_box);
678
679   if (combo_box->priv->tree_view && combo_box->priv->cell_view)
680     gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
681                                         &widget->style->base[GTK_WIDGET_STATE (widget)]);
682 }
683
684 static void
685 gtk_combo_box_button_toggled (GtkWidget *widget,
686                               gpointer   data)
687 {
688   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
689
690   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
691     {
692       if (!combo_box->priv->popup_in_progress)
693         gtk_combo_box_popup (combo_box);
694     }
695   else
696     gtk_combo_box_popdown (combo_box);
697 }
698
699 static void
700 gtk_combo_box_add (GtkContainer *container,
701                    GtkWidget    *widget)
702 {
703   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
704
705   if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
706     {
707       gtk_widget_unparent (combo_box->priv->cell_view);
708       GTK_BIN (container)->child = NULL;
709       gtk_widget_queue_resize (GTK_WIDGET (container));
710     }
711   
712   gtk_widget_set_parent (widget, GTK_WIDGET (container));
713   GTK_BIN (container)->child = widget;
714
715   if (combo_box->priv->cell_view &&
716       widget != combo_box->priv->cell_view)
717     {
718       /* since the cell_view was unparented, it's gone now */
719       combo_box->priv->cell_view = NULL;
720
721       if (!combo_box->priv->tree_view && combo_box->priv->separator)
722         {
723           gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
724                                 combo_box->priv->separator);
725           combo_box->priv->separator = NULL;
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->box)
1506         (* callback) (combo_box->priv->box, callback_data);
1507       if (combo_box->priv->separator)
1508         (* callback) (combo_box->priv->separator, callback_data);
1509       if (combo_box->priv->arrow)
1510         (* callback) (combo_box->priv->arrow, callback_data);
1511       if (combo_box->priv->cell_view_frame)
1512         (* callback) (combo_box->priv->cell_view_frame, callback_data);
1513     }
1514
1515   if (GTK_BIN (container)->child)
1516     (* callback) (GTK_BIN (container)->child, callback_data);
1517 }
1518
1519 static gboolean
1520 gtk_combo_box_expose_event (GtkWidget      *widget,
1521                             GdkEventExpose *event)
1522 {
1523   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1524
1525   if (!combo_box->priv->tree_view)
1526     {
1527       gtk_container_propagate_expose (GTK_CONTAINER (widget),
1528                                       combo_box->priv->button, event);
1529     }
1530   else
1531     {
1532       gtk_container_propagate_expose (GTK_CONTAINER (widget),
1533                                       combo_box->priv->button, event);
1534
1535       if (combo_box->priv->cell_view_frame)
1536         gtk_container_propagate_expose (GTK_CONTAINER (widget),
1537                                         combo_box->priv->cell_view_frame, event);
1538     }
1539
1540   gtk_container_propagate_expose (GTK_CONTAINER (widget),
1541                                   GTK_BIN (widget)->child, event);
1542
1543   return FALSE;
1544 }
1545
1546 static gboolean
1547 gtk_combo_box_scroll_event (GtkWidget          *widget,
1548                             GdkEventScroll     *event)
1549 {
1550   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1551   gint index;
1552   gint items;
1553     
1554   index = gtk_combo_box_get_active (combo_box);
1555
1556   if (index != -1)
1557     {
1558       items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1559       
1560       if (event->direction == GDK_SCROLL_UP)
1561         index--;
1562       else 
1563         index++;
1564
1565       gtk_combo_box_set_active (combo_box, CLAMP (index, 0, items - 1));
1566     }
1567
1568   return TRUE;
1569 }
1570
1571 /*
1572  * menu style
1573  */
1574
1575 static void
1576 cell_view_sync_cells (GtkComboBox *combo_box,
1577                       GtkCellView *cell_view)
1578 {
1579   GSList *k;
1580
1581   for (k = combo_box->priv->cells; k; k = k->next)
1582     {
1583       GSList *j;
1584       ComboCellInfo *info = (ComboCellInfo *)k->data;
1585
1586       if (info->pack == GTK_PACK_START)
1587         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
1588                                     info->cell, info->expand);
1589       else if (info->pack == GTK_PACK_END)
1590         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (cell_view),
1591                                   info->cell, info->expand);
1592
1593       gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view),
1594                                           info->cell,
1595                                           info->func, info->func_data, NULL);
1596
1597       for (j = info->attributes; j; j = j->next->next)
1598         {
1599           gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cell_view),
1600                                          info->cell,
1601                                          j->data,
1602                                          GPOINTER_TO_INT (j->next->data));
1603         }
1604     }
1605 }
1606
1607 static void
1608 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
1609                           gboolean     add_children)
1610 {
1611   GtkWidget *menu;
1612
1613   if (combo_box->priv->cell_view)
1614     {
1615       combo_box->priv->button = gtk_toggle_button_new ();
1616       g_signal_connect (combo_box->priv->button, "toggled",
1617                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1618       g_signal_connect_after (combo_box->priv->button, "key_press_event",
1619                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
1620       gtk_widget_set_parent (combo_box->priv->button,
1621                              GTK_BIN (combo_box)->child->parent);
1622
1623       combo_box->priv->box = gtk_hbox_new (FALSE, 0);
1624       gtk_container_add (GTK_CONTAINER (combo_box->priv->button), 
1625                          combo_box->priv->box);
1626
1627       combo_box->priv->separator = gtk_vseparator_new ();
1628       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
1629                          combo_box->priv->separator);
1630
1631       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1632       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
1633                          combo_box->priv->arrow);
1634
1635       gtk_widget_show_all (combo_box->priv->button);
1636     }
1637   else
1638     {
1639       combo_box->priv->button = gtk_toggle_button_new ();
1640       g_signal_connect (combo_box->priv->button, "toggled",
1641                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
1642       g_signal_connect_after (combo_box, "key_press_event",
1643                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
1644       gtk_widget_set_parent (combo_box->priv->button,
1645                              GTK_BIN (combo_box)->child->parent);
1646
1647       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1648       gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
1649                          combo_box->priv->arrow);
1650       gtk_widget_show_all (combo_box->priv->button);
1651     }
1652
1653   g_signal_connect (combo_box->priv->button, "button_press_event",
1654                     G_CALLBACK (gtk_combo_box_menu_button_press),
1655                     combo_box);
1656   g_signal_connect (combo_box->priv->button, "state_changed",
1657                     G_CALLBACK (gtk_combo_box_menu_state_changed), 
1658                     combo_box);
1659
1660   /* create our funky menu */
1661   menu = gtk_menu_new ();
1662   g_signal_connect (menu, "key_press_event",
1663                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
1664   gtk_combo_box_set_popup_widget (combo_box, menu);
1665
1666   /* add items */
1667   if (add_children)
1668     gtk_combo_box_menu_fill (combo_box);
1669
1670 }
1671
1672 static void
1673 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
1674 {
1675   gint i, items;
1676   GtkWidget *menu;
1677   GtkWidget *tmp;
1678
1679   if (!combo_box->priv->model)
1680     return;
1681
1682   items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
1683   menu = combo_box->priv->popup_widget;
1684
1685   for (i = 0; i < items; i++)
1686     {
1687       GtkTreePath *path;
1688
1689       path = gtk_tree_path_new_from_indices (i, -1);
1690       tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
1691                                                     path);
1692       g_signal_connect (tmp, "activate",
1693                         G_CALLBACK (gtk_combo_box_menu_item_activate),
1694                         combo_box);
1695
1696       cell_view_sync_cells (combo_box,
1697                             GTK_CELL_VIEW (GTK_BIN (tmp)->child));
1698
1699       gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
1700
1701       if (combo_box->priv->wrap_width)
1702         gtk_combo_box_relayout_item (combo_box, i);
1703
1704       gtk_widget_show (tmp);
1705
1706       gtk_tree_path_free (path);
1707     }
1708 }
1709
1710 static void
1711 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
1712 {
1713   g_signal_handlers_disconnect_matched (combo_box->priv->button,
1714                                         G_SIGNAL_MATCH_DATA,
1715                                         0, 0, NULL,
1716                                         gtk_combo_box_menu_button_press, NULL);
1717   g_signal_handlers_disconnect_matched (combo_box->priv->button,
1718                                         G_SIGNAL_MATCH_DATA,
1719                                         0, 0, NULL,
1720                                         gtk_combo_box_menu_state_changed, NULL);
1721
1722   /* unparent will remove our latest ref */
1723   gtk_widget_unparent (combo_box->priv->button);
1724   
1725   combo_box->priv->box = NULL;
1726   combo_box->priv->button = NULL;
1727   combo_box->priv->arrow = NULL;
1728   combo_box->priv->separator = NULL;
1729
1730   /* changing the popup window will unref the menu and the children */
1731 }
1732
1733 /*
1734  * grid
1735  */
1736
1737 static void
1738 gtk_combo_box_item_get_size (GtkComboBox *combo_box,
1739                              gint         index,
1740                              gint        *cols,
1741                              gint        *rows)
1742 {
1743   GtkTreeIter iter;
1744
1745   gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter, NULL, index);
1746
1747   if (cols)
1748     {
1749       if (combo_box->priv->col_column == -1)
1750         *cols = 1;
1751       else
1752         gtk_tree_model_get (combo_box->priv->model, &iter,
1753                             combo_box->priv->col_column, cols,
1754                             -1);
1755     }
1756
1757   if (rows)
1758     {
1759       if (combo_box->priv->row_column == -1)
1760         *rows = 1;
1761       else
1762         gtk_tree_model_get (combo_box->priv->model, &iter,
1763                             combo_box->priv->row_column, rows,
1764                             -1);
1765     }
1766 }
1767
1768 static gboolean
1769 menu_occupied (GtkMenu *menu,
1770                guint    left_attach,
1771                guint    right_attach,
1772                guint    top_attach,
1773                guint    bottom_attach)
1774 {
1775   GList *i;
1776
1777   g_return_val_if_fail (GTK_IS_MENU (menu), TRUE);
1778   g_return_val_if_fail (left_attach < right_attach, TRUE);
1779   g_return_val_if_fail (top_attach < bottom_attach, TRUE);
1780
1781   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1782     {
1783       guint l, r, b, t;
1784       gboolean h_intersect = FALSE;
1785       gboolean v_intersect = FALSE;
1786
1787       gtk_container_child_get (GTK_CONTAINER (menu), i->data,
1788                                "left_attach", &l,
1789                                "right_attach", &r,
1790                                "bottom_attach", &b,
1791                                "top_attach", &t,
1792                                NULL);
1793
1794       /* look if this item intersects with the given coordinates */
1795       h_intersect  = left_attach <= l && l <= right_attach;
1796       h_intersect &= left_attach <= r && r <= right_attach;
1797
1798       v_intersect  = top_attach <= t && t <= bottom_attach;
1799       v_intersect &= top_attach <= b && b <= bottom_attach;
1800
1801       if (h_intersect && v_intersect)
1802         return TRUE;
1803     }
1804
1805   return FALSE;
1806 }
1807
1808 static void
1809 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
1810                              gint         index)
1811 {
1812   gint current_col = 0, current_row = 0;
1813   gint rows, cols;
1814   GList *list;
1815   GtkWidget *item;
1816   GtkWidget *menu;
1817
1818   menu = combo_box->priv->popup_widget;
1819   if (!GTK_IS_MENU_SHELL (menu))
1820     return;
1821
1822   list = gtk_container_get_children (GTK_CONTAINER (menu));
1823   item = g_list_nth_data (list, index);
1824   g_list_free (list);
1825
1826   gtk_combo_box_item_get_size (combo_box, index, &cols, &rows);
1827
1828   /* look for a good spot */
1829   while (1)
1830     {
1831       if (current_col + cols > combo_box->priv->wrap_width)
1832         {
1833           current_col = 0;
1834           current_row++;
1835         }
1836
1837       if (!menu_occupied (GTK_MENU (menu),
1838                           current_col, current_col + cols,
1839                           current_row, current_row + rows))
1840         break;
1841
1842       current_col++;
1843     }
1844
1845   /* set attach props */
1846   gtk_menu_attach (GTK_MENU (menu), item,
1847                    current_col, current_col + cols,
1848                    current_row, current_row + rows);
1849 }
1850
1851 static void
1852 gtk_combo_box_relayout (GtkComboBox *combo_box)
1853 {
1854   GList *list, *j;
1855   GtkWidget *menu;
1856
1857   menu = combo_box->priv->popup_widget;
1858   
1859   /* do nothing unless we are in menu style and realized */
1860   if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
1861     return;
1862   
1863   /* get rid of all children */
1864   list = gtk_container_get_children (GTK_CONTAINER (menu));
1865   
1866   for (j = g_list_last (list); j; j = j->prev)
1867     gtk_container_remove (GTK_CONTAINER (menu), j->data);
1868   
1869   g_list_free (list);
1870       
1871   /* and relayout */
1872   gtk_combo_box_menu_fill (combo_box);
1873 }
1874
1875 /* callbacks */
1876 static gboolean
1877 gtk_combo_box_menu_button_press (GtkWidget      *widget,
1878                                  GdkEventButton *event,
1879                                  gpointer        user_data)
1880 {
1881   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1882
1883   if (! GTK_IS_MENU (combo_box->priv->popup_widget))
1884     return FALSE;
1885
1886   if (event->type == GDK_BUTTON_PRESS && event->button == 1)
1887     {
1888       combo_box->priv->popup_in_progress = TRUE;
1889       
1890       gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
1891                            combo_box->priv->active_item);
1892
1893       gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1894                       NULL, NULL,
1895                       gtk_combo_box_menu_position, combo_box,
1896                       event->button, event->time);
1897
1898       return TRUE;
1899     }
1900
1901   return FALSE;
1902 }
1903
1904 static void
1905 gtk_combo_box_menu_state_changed (GtkWidget    *widget,
1906                                   GtkStateType  previous,
1907                                   gpointer      user_data)
1908 {
1909   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1910
1911   if (combo_box->priv->cell_view)
1912     {
1913       gtk_widget_set_state (combo_box->priv->cell_view, 
1914                             GTK_WIDGET_STATE (widget));
1915       
1916       gtk_widget_queue_draw (combo_box->priv->cell_view);
1917     }
1918 }
1919
1920 static void
1921 gtk_combo_box_menu_item_activate (GtkWidget *item,
1922                                   gpointer   user_data)
1923 {
1924   gint index;
1925   GtkWidget *menu;
1926   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1927
1928   menu = combo_box->priv->popup_widget;
1929   g_return_if_fail (GTK_IS_MENU (menu));
1930
1931   index = g_list_index (GTK_MENU_SHELL (menu)->children, item);
1932
1933   gtk_combo_box_set_active (combo_box, index);
1934 }
1935
1936 static void
1937 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
1938                                   GtkTreePath      *path,
1939                                   GtkTreeIter      *iter,
1940                                   gpointer          user_data)
1941 {
1942   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1943   gint index = gtk_tree_path_get_indices (path)[0];
1944
1945   if (combo_box->priv->active_item >= index)
1946     combo_box->priv->active_item++;
1947
1948   if (!combo_box->priv->tree_view)
1949     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
1950 }
1951
1952 static void
1953 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
1954                                  GtkTreePath      *path,
1955                                  gpointer          user_data)
1956 {
1957   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1958   gint index = gtk_tree_path_get_indices (path)[0];
1959
1960   if (!combo_box->priv->tree_view)
1961     gtk_combo_box_menu_row_deleted (model, path, user_data);
1962   
1963   if (index == combo_box->priv->active_item)
1964     {
1965       gint items = gtk_tree_model_iter_n_children (model, NULL);
1966
1967       if (items == 0)
1968         gtk_combo_box_set_active_internal (combo_box, -1);
1969       else if (index == items)
1970         gtk_combo_box_set_active_internal (combo_box, index - 1);
1971       else
1972         gtk_combo_box_set_active_internal (combo_box, index);
1973     }
1974   else if (combo_box->priv->active_item > index)
1975     combo_box->priv->active_item--;
1976 }
1977
1978 static void
1979 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
1980                                     GtkTreePath     *path,
1981                                     GtkTreeIter     *iter,
1982                                     gint            *new_order,
1983                                     gpointer         user_data)
1984 {
1985   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1986   gint items = gtk_tree_model_iter_n_children (model, NULL);
1987   gint i;
1988
1989   for (i = 0; i < items; i++)
1990     if (new_order[i] == combo_box->priv->active_item)
1991       {
1992         combo_box->priv->active_item = i;
1993         break;
1994       }
1995
1996   if (!combo_box->priv->tree_view)
1997     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
1998 }
1999                                                     
2000 static void
2001 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
2002                                  GtkTreePath      *path,
2003                                  GtkTreeIter      *iter,
2004                                  gpointer          user_data)
2005 {
2006   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2007   gint index = gtk_tree_path_get_indices (path)[0];
2008
2009   if (index == combo_box->priv->active_item &&
2010       combo_box->priv->cell_view)
2011     gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2012   
2013   if (combo_box->priv->tree_view)
2014     gtk_combo_box_list_row_changed (model, path, iter, user_data);
2015   else
2016     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2017 }
2018
2019
2020 static void
2021 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
2022                                  GtkTreePath  *path,
2023                                  GtkTreeIter  *iter,
2024                                  gpointer      user_data)
2025 {
2026   GtkWidget *menu;
2027   GtkWidget *item;
2028   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2029
2030   if (!combo_box->priv->popup_widget)
2031     return;
2032
2033   menu = combo_box->priv->popup_widget;
2034   g_return_if_fail (GTK_IS_MENU (menu));
2035
2036   item = gtk_cell_view_menu_item_new_from_model (model, path);
2037   g_signal_connect (item, "activate",
2038                     G_CALLBACK (gtk_combo_box_menu_item_activate),
2039                     combo_box);
2040
2041   cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (item)->child));
2042
2043   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item,
2044                          gtk_tree_path_get_indices (path)[0]);
2045   gtk_widget_show (item);
2046 }
2047
2048 static void
2049 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
2050                                 GtkTreePath  *path,
2051                                 gpointer      user_data)
2052 {
2053   gint index;
2054   GtkWidget *menu;
2055   GtkWidget *item;
2056   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2057
2058   if (!combo_box->priv->popup_widget)
2059     return;
2060
2061   index = gtk_tree_path_get_indices (path)[0];
2062
2063   menu = combo_box->priv->popup_widget;
2064   g_return_if_fail (GTK_IS_MENU (menu));
2065
2066   item = g_list_nth_data (GTK_MENU_SHELL (menu)->children, index);
2067   g_return_if_fail (GTK_IS_MENU_ITEM (item));
2068
2069   gtk_container_remove (GTK_CONTAINER (menu), item);
2070 }
2071
2072 static void
2073 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
2074                                     GtkTreePath      *path,
2075                                     GtkTreeIter      *iter,
2076                                     gint             *new_order,
2077                                     gpointer          user_data)
2078 {
2079   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2080
2081   gtk_combo_box_relayout (combo_box);
2082 }
2083                                     
2084 static void
2085 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
2086                                 GtkTreePath  *path,
2087                                 GtkTreeIter  *iter,
2088                                 gpointer      user_data)
2089 {
2090   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2091   gint width;
2092
2093   if (!combo_box->priv->popup_widget)
2094     return;
2095
2096   if (combo_box->priv->wrap_width)
2097     gtk_combo_box_relayout_item (combo_box,
2098                                  gtk_tree_path_get_indices (path)[0]);
2099
2100   width = gtk_combo_box_calc_requested_width (combo_box, path);
2101
2102   if (width > combo_box->priv->width)
2103     {
2104       if (combo_box->priv->cell_view)
2105         {
2106           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2107           gtk_widget_queue_resize (combo_box->priv->cell_view);
2108         }
2109       combo_box->priv->width = width;
2110     }
2111 }
2112
2113 /*
2114  * list style
2115  */
2116
2117 static void
2118 gtk_combo_box_list_setup (GtkComboBox *combo_box)
2119 {
2120   GSList *i;
2121   GtkTreeSelection *sel;
2122
2123   combo_box->priv->button = gtk_toggle_button_new ();
2124   gtk_widget_set_parent (combo_box->priv->button,
2125                          GTK_BIN (combo_box)->child->parent);
2126   g_signal_connect (combo_box->priv->button, "button_press_event",
2127                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
2128   g_signal_connect (combo_box->priv->button, "toggled",
2129                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2130   g_signal_connect_after (combo_box, "key_press_event",
2131                           G_CALLBACK (gtk_combo_box_key_press), combo_box);
2132
2133   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2134   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2135                      combo_box->priv->arrow);
2136   combo_box->priv->separator = NULL;
2137   gtk_widget_show_all (combo_box->priv->button);
2138
2139   if (combo_box->priv->cell_view)
2140     {
2141       combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
2142       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
2143                              GTK_BIN (combo_box)->child->parent);
2144       gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
2145                                  GTK_SHADOW_IN);
2146
2147       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
2148                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
2149
2150       gtk_widget_show (combo_box->priv->cell_view_frame);
2151     }
2152
2153   combo_box->priv->tree_view = gtk_tree_view_new ();
2154   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2155   gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
2156   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
2157                                      FALSE);
2158   if (combo_box->priv->model)
2159     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
2160                              combo_box->priv->model);
2161     
2162   g_signal_connect (combo_box->priv->tree_view, "button_press_event",
2163                     G_CALLBACK (gtk_combo_box_list_button_pressed),
2164                     combo_box);
2165   g_signal_connect (combo_box->priv->tree_view, "button_release_event",
2166                     G_CALLBACK (gtk_combo_box_list_button_released),
2167                     combo_box);
2168   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
2169                     G_CALLBACK (gtk_combo_box_list_key_press),
2170                     combo_box);
2171
2172   combo_box->priv->column = gtk_tree_view_column_new ();
2173   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
2174                                combo_box->priv->column);
2175
2176   /* sync up */
2177   for (i = combo_box->priv->cells; i; i = i->next)
2178     {
2179       GSList *j;
2180       ComboCellInfo *info = (ComboCellInfo *)i->data;
2181
2182       if (info->pack == GTK_PACK_START)
2183         gtk_tree_view_column_pack_start (combo_box->priv->column,
2184                                          info->cell, info->expand);
2185       else if (info->pack == GTK_PACK_END)
2186         gtk_tree_view_column_pack_end (combo_box->priv->column,
2187                                        info->cell, info->expand);
2188
2189       for (j = info->attributes; j; j = j->next->next)
2190         {
2191           gtk_tree_view_column_add_attribute (combo_box->priv->column,
2192                                               info->cell,
2193                                               j->data,
2194                                               GPOINTER_TO_INT (j->next->data));
2195         }
2196     }
2197
2198   if (combo_box->priv->active_item != -1)
2199     {
2200       GtkTreePath *path;
2201
2202       path = gtk_tree_path_new_from_indices (combo_box->priv->active_item, -1);
2203       if (path)
2204         {
2205           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
2206                                     path, NULL, FALSE);
2207           gtk_tree_path_free (path);
2208         }
2209     }
2210
2211   /* set sample/popup widgets */
2212   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
2213
2214   gtk_widget_show (combo_box->priv->tree_view);
2215 }
2216
2217 static void
2218 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
2219 {
2220   /* disconnect signals */
2221   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
2222                                         G_SIGNAL_MATCH_DATA,
2223                                         0, 0, NULL, NULL, combo_box);
2224   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2225                                         G_SIGNAL_MATCH_DATA,
2226                                         0, 0, NULL,
2227                                         gtk_combo_box_list_button_pressed,
2228                                         NULL);
2229
2230   /* destroy things (unparent will kill the latest ref from us)
2231    * last unref on button will destroy the arrow
2232    */
2233   gtk_widget_unparent (combo_box->priv->button);
2234   combo_box->priv->button = NULL;
2235   combo_box->priv->arrow = NULL;
2236
2237   if (combo_box->priv->cell_view)
2238     {
2239       g_object_set (G_OBJECT (combo_box->priv->cell_view),
2240                     "background_set", FALSE,
2241                     NULL);
2242     }
2243
2244   if (combo_box->priv->cell_view_frame)
2245     {
2246       gtk_widget_unparent (combo_box->priv->cell_view_frame);
2247       combo_box->priv->cell_view_frame = NULL;
2248     }
2249
2250   gtk_widget_destroy (combo_box->priv->tree_view);
2251
2252   combo_box->priv->tree_view = NULL;
2253   combo_box->priv->popup_widget = NULL;
2254 }
2255
2256 /* callbacks */
2257 static void
2258 gtk_combo_box_list_remove_grabs (GtkComboBox *combo_box)
2259 {
2260   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->tree_view))
2261     gtk_grab_remove (combo_box->priv->tree_view);
2262
2263   if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
2264     {
2265       gtk_grab_remove (combo_box->priv->popup_window);
2266       gdk_keyboard_ungrab (GDK_CURRENT_TIME);
2267       gdk_pointer_ungrab (GDK_CURRENT_TIME);
2268     }
2269 }
2270
2271 static gboolean
2272 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
2273                                    GdkEventButton *event,
2274                                    gpointer        data)
2275 {
2276   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2277
2278   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2279
2280   if (ewidget == combo_box->priv->tree_view)
2281     return TRUE;
2282
2283   if ((ewidget != combo_box->priv->button) ||
2284       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2285     return FALSE;
2286
2287   gtk_combo_box_popup (combo_box);
2288
2289   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
2290                                 TRUE);
2291
2292   combo_box->priv->popup_in_progress = TRUE;
2293
2294   return TRUE;
2295 }
2296
2297 static gboolean
2298 gtk_combo_box_list_button_released (GtkWidget      *widget,
2299                                     GdkEventButton *event,
2300                                     gpointer        data)
2301 {
2302   gboolean ret;
2303   GtkTreePath *path = NULL;
2304
2305   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2306
2307   gboolean popup_in_progress = FALSE;
2308
2309   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
2310
2311   if (combo_box->priv->popup_in_progress)
2312     {
2313       popup_in_progress = TRUE;
2314       combo_box->priv->popup_in_progress = FALSE;
2315     }
2316
2317   if (ewidget != combo_box->priv->tree_view)
2318     {
2319       if (ewidget == combo_box->priv->button &&
2320           !popup_in_progress &&
2321           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
2322         {
2323           gtk_combo_box_popdown (combo_box);
2324           return TRUE;
2325         }
2326
2327       /* released outside treeview */
2328       if (ewidget != combo_box->priv->button)
2329         {
2330           gtk_combo_box_popdown (combo_box);
2331
2332           return TRUE;
2333         }
2334
2335       return FALSE;
2336     }
2337
2338   /* select something cool */
2339   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
2340                                        event->x, event->y,
2341                                        &path,
2342                                        NULL, NULL, NULL);
2343
2344   if (!ret)
2345     return TRUE; /* clicked outside window? */
2346
2347   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2348   gtk_combo_box_popdown (combo_box);
2349
2350   gtk_tree_path_free (path);
2351
2352   return TRUE;
2353 }
2354
2355 static gboolean
2356 gtk_combo_box_key_press (GtkWidget   *widget,
2357                          GdkEventKey *event,
2358                          gpointer     data)
2359 {
2360   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2361   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2362   gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
2363   gint index = gtk_combo_box_get_active (combo_box);
2364   gint new_index;
2365
2366   if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) && 
2367       state == GDK_MOD1_MASK)
2368     {
2369       gtk_combo_box_popup (combo_box);
2370
2371       return TRUE;
2372     }
2373
2374   switch (event->keyval) 
2375     {
2376     case GDK_Down:
2377     case GDK_KP_Down:
2378       new_index = index + 1;
2379       break;
2380     case GDK_Up:
2381     case GDK_KP_Up:
2382       new_index = index - 1;
2383       break;
2384     case GDK_Page_Up:
2385     case GDK_KP_Page_Up:
2386     case GDK_Home: 
2387     case GDK_KP_Home:
2388       new_index = 0;
2389       break;
2390     case GDK_Page_Down:
2391     case GDK_KP_Page_Down:
2392     case GDK_End: 
2393     case GDK_KP_End:
2394       new_index = items - 1;
2395       break;
2396     default:
2397       return FALSE;
2398     }
2399   
2400   gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
2401
2402   return TRUE;
2403 }
2404
2405 static gboolean
2406 gtk_combo_box_menu_key_press (GtkWidget   *widget,
2407                               GdkEventKey *event,
2408                               gpointer     data)
2409 {
2410   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2411   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2412
2413   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
2414       state == GDK_MOD1_MASK)
2415     {
2416       gtk_combo_box_popdown (combo_box);
2417
2418       return TRUE;
2419     }
2420   
2421   return FALSE;
2422 }
2423
2424 static gboolean
2425 gtk_combo_box_list_key_press (GtkWidget   *widget,
2426                               GdkEventKey *event,
2427                               gpointer     data)
2428 {
2429   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2430   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
2431
2432   if (event->keyval == GDK_Escape ||
2433       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
2434        state == GDK_MOD1_MASK))
2435     {
2436       /* reset active item -- this is incredibly lame and ugly */
2437       gtk_combo_box_set_active (combo_box,
2438                                 gtk_combo_box_get_active (combo_box));
2439       
2440       gtk_combo_box_popdown (combo_box);
2441       
2442       return TRUE;
2443     }
2444     
2445   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
2446       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
2447   {
2448     gboolean ret;
2449     GtkTreeIter iter;
2450     GtkTreeModel *model = NULL;
2451     GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
2452     
2453     ret = gtk_tree_selection_get_selected (sel, &model, &iter);
2454     if (ret)
2455       {
2456         GtkTreePath *path;
2457         
2458         path = gtk_tree_model_get_path (model, &iter);
2459         if (path)
2460           {
2461             gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
2462             gtk_tree_path_free (path);
2463           }
2464       }
2465
2466     gtk_combo_box_popdown (combo_box);
2467     
2468     return TRUE;
2469   }
2470
2471   return FALSE;
2472 }
2473
2474 static void
2475 gtk_combo_box_list_row_changed (GtkTreeModel *model,
2476                                 GtkTreePath  *path,
2477                                 GtkTreeIter  *iter,
2478                                 gpointer      data)
2479 {
2480   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
2481   gint width;
2482
2483   width = gtk_combo_box_calc_requested_width (combo_box, path);
2484
2485   if (width > combo_box->priv->width)
2486     {
2487       if (combo_box->priv->cell_view) 
2488         {
2489           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
2490           gtk_widget_queue_resize (combo_box->priv->cell_view);
2491         }
2492       combo_box->priv->width = width;
2493     }
2494 }
2495
2496 /*
2497  * GtkCellLayout implementation
2498  */
2499 static void
2500 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
2501                                       GtkCellRenderer *cell,
2502                                       gboolean         expand)
2503 {
2504   ComboCellInfo *info;
2505   GtkComboBox *combo_box;
2506   GtkWidget *menu;
2507
2508   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2509   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2510
2511   combo_box = GTK_COMBO_BOX (layout);
2512
2513   g_object_ref (G_OBJECT (cell));
2514   gtk_object_sink (GTK_OBJECT (cell));
2515
2516   info = g_new0 (ComboCellInfo, 1);
2517   info->cell = cell;
2518   info->expand = expand;
2519   info->pack = GTK_PACK_START;
2520
2521   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
2522
2523   if (combo_box->priv->cell_view)
2524     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2525                                 cell, expand);
2526
2527   if (combo_box->priv->column)
2528     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
2529
2530   menu = combo_box->priv->popup_widget;
2531   if (GTK_IS_MENU (menu))
2532     {
2533       GList *i, *list;
2534
2535       list = gtk_container_get_children (GTK_CONTAINER (menu));
2536       for (i = list; i; i = i->next)
2537         {
2538           GtkCellView *view;
2539
2540           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2541             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2542           else
2543             view = GTK_CELL_VIEW (i->data);
2544
2545           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (view), cell, expand);
2546         }
2547       g_list_free (list);
2548     }
2549 }
2550
2551 static void
2552 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
2553                                     GtkCellRenderer *cell,
2554                                     gboolean         expand)
2555 {
2556   ComboCellInfo *info;
2557   GtkComboBox *combo_box;
2558   GtkWidget *menu;
2559
2560   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2561   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2562
2563   combo_box = GTK_COMBO_BOX (layout);
2564
2565   g_object_ref (G_OBJECT (cell));
2566   gtk_object_sink (GTK_OBJECT (cell));
2567
2568   info = g_new0 (ComboCellInfo, 1);
2569   info->cell = cell;
2570   info->expand = expand;
2571   info->pack = GTK_PACK_END;
2572
2573   if (combo_box->priv->cell_view)
2574     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2575                               cell, expand);
2576
2577   if (combo_box->priv->column)
2578     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
2579
2580   menu = combo_box->priv->popup_widget;
2581   if (GTK_IS_MENU (menu))
2582     {
2583       GList *i, *list;
2584
2585       list = gtk_container_get_children (GTK_CONTAINER (menu));
2586       for (i = list; i; i = i->next)
2587         {
2588           GtkCellView *view;
2589
2590           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2591             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2592           else
2593             view = GTK_CELL_VIEW (i->data);
2594
2595           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (view), cell, expand);
2596         }
2597       g_list_free (list);
2598     }
2599 }
2600
2601 static void
2602 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
2603 {
2604   GtkWidget *menu;
2605   GtkComboBox *combo_box;
2606   GSList *i;
2607   
2608   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2609
2610   combo_box = GTK_COMBO_BOX (layout);
2611  
2612   if (combo_box->priv->cell_view)
2613     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
2614
2615   if (combo_box->priv->column)
2616     gtk_tree_view_column_clear (combo_box->priv->column);
2617
2618   for (i = combo_box->priv->cells; i; i = i->next)
2619     {
2620      ComboCellInfo *info = (ComboCellInfo *)i->data;
2621
2622       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
2623       g_object_unref (G_OBJECT (info->cell));
2624       g_free (info);
2625       i->data = NULL;
2626     }
2627   g_slist_free (combo_box->priv->cells);
2628   combo_box->priv->cells = NULL;
2629
2630   menu = combo_box->priv->popup_widget;
2631   if (GTK_IS_MENU (menu))
2632     {
2633       GList *i, *list;
2634
2635       list = gtk_container_get_children (GTK_CONTAINER (menu));
2636       for (i = list; i; i = i->next)
2637         {
2638           GtkCellView *view;
2639
2640           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2641             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2642           else
2643             view = GTK_CELL_VIEW (i->data);
2644
2645           gtk_cell_layout_clear (GTK_CELL_LAYOUT (view));
2646         }
2647       g_list_free (list);
2648     }
2649 }
2650
2651 static void
2652 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
2653                                          GtkCellRenderer *cell,
2654                                          const gchar     *attribute,
2655                                          gint             column)
2656 {
2657   ComboCellInfo *info;
2658   GtkComboBox *combo_box;
2659   GtkWidget *menu;
2660
2661   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2662   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2663
2664   combo_box = GTK_COMBO_BOX (layout);
2665
2666   info = gtk_combo_box_get_cell_info (combo_box, cell);
2667
2668   info->attributes = g_slist_prepend (info->attributes,
2669                                       GINT_TO_POINTER (column));
2670   info->attributes = g_slist_prepend (info->attributes,
2671                                       g_strdup (attribute));
2672
2673   if (combo_box->priv->cell_view)
2674     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2675                                    cell, attribute, column);
2676
2677   if (combo_box->priv->column)
2678     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
2679                                    cell, attribute, column);
2680
2681   menu = combo_box->priv->popup_widget;
2682   if (GTK_IS_MENU (menu))
2683     {
2684       GList *i, *list;
2685
2686       list = gtk_container_get_children (GTK_CONTAINER (menu));
2687       for (i = list; i; i = i->next)
2688         {
2689           GtkCellView *view;
2690
2691           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2692             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2693           else
2694             view = GTK_CELL_VIEW (i->data);
2695
2696           gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (view), cell,
2697                                          attribute, column);
2698         }
2699       g_list_free (list);
2700     }
2701
2702   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2703 }
2704
2705 static void
2706 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
2707                                               GtkCellRenderer       *cell,
2708                                               GtkCellLayoutDataFunc  func,
2709                                               gpointer               func_data,
2710                                               GDestroyNotify         destroy)
2711 {
2712   ComboCellInfo *info;
2713   GtkComboBox *combo_box;
2714   GtkWidget *menu;
2715
2716   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2717
2718   combo_box = GTK_COMBO_BOX (layout);
2719
2720   info = gtk_combo_box_get_cell_info (combo_box, cell);
2721   g_return_if_fail (info != NULL);
2722
2723   if (info->destroy)
2724     {
2725       GDestroyNotify d = info->destroy;
2726
2727       info->destroy = NULL;
2728       d (info->func_data);
2729     }
2730
2731   info->func = func;
2732   info->func_data = func_data;
2733   info->destroy = destroy;
2734
2735   if (combo_box->priv->cell_view)
2736     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
2737
2738   if (combo_box->priv->column)
2739     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
2740
2741   menu = combo_box->priv->popup_widget;
2742   if (GTK_IS_MENU (menu))
2743     {
2744       GList *i, *list;
2745
2746       list = gtk_container_get_children (GTK_CONTAINER (menu));
2747       for (i = list; i; i = i->next)
2748         {
2749           GtkCellView *view;
2750
2751           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2752             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2753           else
2754             view = GTK_CELL_VIEW (i->data);
2755
2756           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (view), cell,
2757                                               func, func_data, NULL);
2758         }
2759       g_list_free (list);
2760     }
2761
2762   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2763 }
2764
2765 static void
2766 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
2767                                             GtkCellRenderer *cell)
2768 {
2769   ComboCellInfo *info;
2770   GtkComboBox *combo_box;
2771   GtkWidget *menu;
2772   GSList *list;
2773
2774   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2775   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2776
2777   combo_box = GTK_COMBO_BOX (layout);
2778
2779   info = gtk_combo_box_get_cell_info (combo_box, cell);
2780   g_return_if_fail (info != NULL);
2781
2782   list = info->attributes;
2783   while (list && list->next)
2784     {
2785       g_free (list->data);
2786       list = list->next->next;
2787     }
2788   g_slist_free (info->attributes);
2789   info->attributes = NULL;
2790
2791   if (combo_box->priv->cell_view)
2792     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
2793
2794   if (combo_box->priv->column)
2795     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
2796
2797   menu = combo_box->priv->popup_widget;
2798   if (GTK_IS_MENU (menu))
2799     {
2800       GList *i, *list;
2801
2802       list = gtk_container_get_children (GTK_CONTAINER (menu));
2803       for (i = list; i; i = i->next)
2804         {
2805           GtkCellView *view;
2806
2807           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2808             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2809           else
2810             view = GTK_CELL_VIEW (i->data);
2811
2812           gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view), cell);
2813         }
2814       g_list_free (list);
2815     }
2816
2817   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
2818 }
2819
2820 static void
2821 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
2822                                    GtkCellRenderer *cell,
2823                                    gint             position)
2824 {
2825   ComboCellInfo *info;
2826   GtkComboBox *combo_box;
2827   GtkWidget *menu;
2828   GSList *link;
2829
2830   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
2831   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
2832
2833   combo_box = GTK_COMBO_BOX (layout);
2834
2835   info = gtk_combo_box_get_cell_info (combo_box, cell);
2836
2837   g_return_if_fail (info != NULL);
2838   g_return_if_fail (position >= 0);
2839
2840   link = g_slist_find (combo_box->priv->cells, info);
2841
2842   g_return_if_fail (link != NULL);
2843
2844   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
2845   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
2846                                            position);
2847
2848   if (combo_box->priv->cell_view)
2849     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
2850                              cell, position);
2851
2852   if (combo_box->priv->column)
2853     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
2854                              cell, position);
2855
2856   menu = combo_box->priv->popup_widget;
2857   if (GTK_IS_MENU (menu))
2858     {
2859       GList *i, *list;
2860
2861       list = gtk_container_get_children (GTK_CONTAINER (menu));
2862       for (i = list; i; i = i->next)
2863         {
2864           GtkCellView *view;
2865
2866           if (GTK_IS_CELL_VIEW_MENU_ITEM (i->data))
2867             view = GTK_CELL_VIEW (GTK_BIN (i->data)->child);
2868           else
2869             view = GTK_CELL_VIEW (i->data);
2870
2871           gtk_cell_layout_reorder (GTK_CELL_LAYOUT (view), cell, position);
2872         }
2873       g_list_free (list);
2874     }
2875
2876   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
2877 }
2878
2879 /*
2880  * public API
2881  */
2882
2883 /**
2884  * gtk_combo_box_new:
2885  *
2886  * Creates a new empty #GtkComboBox.
2887  *
2888  * Return value: A new #GtkComboBox.
2889  *
2890  * Since: 2.4
2891  */
2892 GtkWidget *
2893 gtk_combo_box_new (void)
2894 {
2895   return GTK_WIDGET (g_object_new (GTK_TYPE_COMBO_BOX, NULL));
2896 }
2897
2898 /**
2899  * gtk_combo_box_new_with_model:
2900  * @model: A #GtkTreeModel.
2901  *
2902  * Creates a new #GtkComboBox with the model initialized to @model.
2903  *
2904  * Return value: A new #GtkComboBox.
2905  *
2906  * Since: 2.4
2907  */
2908 GtkWidget *
2909 gtk_combo_box_new_with_model (GtkTreeModel *model)
2910 {
2911   GtkComboBox *combo_box;
2912
2913   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
2914
2915   combo_box = GTK_COMBO_BOX (g_object_new (GTK_TYPE_COMBO_BOX,
2916                                            "model", model,
2917                                            NULL));
2918
2919   return GTK_WIDGET (combo_box);
2920 }
2921
2922 /**
2923  * gtk_combo_box_set_wrap_width:
2924  * @combo_box: A #GtkComboBox.
2925  * @width: Preferred number of columns.
2926  *
2927  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
2928  * the preferred number of columns when you want to the popup to be layed out
2929  * in a table.
2930  *
2931  * Since: 2.4
2932  */
2933 void
2934 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
2935                               gint         width)
2936 {
2937   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2938   g_return_if_fail (width >= 0);
2939
2940   if (width != combo_box->priv->wrap_width)
2941     {
2942       combo_box->priv->wrap_width = width;
2943
2944       gtk_combo_box_check_appearance (combo_box);
2945       gtk_combo_box_relayout (combo_box);
2946       
2947       g_object_notify (G_OBJECT (combo_box), "wrap_width");
2948     }
2949 }
2950
2951 /**
2952  * gtk_combo_box_set_row_span_column:
2953  * @combo_box: A #GtkComboBox.
2954  * @row_span: A column in the model passed during construction.
2955  *
2956  * Sets the column with row span information for @combo_box to be @row_span.
2957  * The row span column contains integers which indicate how many rows
2958  * an item should span.
2959  *
2960  * Since: 2.4
2961  */
2962 void
2963 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
2964                                    gint         row_span)
2965 {
2966   gint col;
2967
2968   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
2969
2970   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
2971   g_return_if_fail (row_span >= 0 && row_span < col);
2972
2973   if (row_span != combo_box->priv->row_column)
2974     {
2975       combo_box->priv->row_column = row_span;
2976       
2977       gtk_combo_box_relayout (combo_box);
2978  
2979       g_object_notify (G_OBJECT (combo_box), "row_span_column");
2980     }
2981 }
2982
2983 /**
2984  * gtk_combo_box_set_column_span_column:
2985  * @combo_box: A #GtkComboBox.
2986  * @column_span: A column in the model passed during construction.
2987  *
2988  * Sets the column with column span information for @combo_box to be
2989  * @column_span. The column span column contains integers which indicate
2990  * how many columns an item should span.
2991  *
2992  * Since: 2.4
2993  */
2994 void
2995 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
2996                                       gint         column_span)
2997 {
2998   gint col;
2999
3000   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3001
3002   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
3003   g_return_if_fail (column_span >= 0 && column_span < col);
3004
3005   if (column_span != combo_box->priv->col_column)
3006     {
3007       combo_box->priv->col_column = column_span;
3008       
3009       gtk_combo_box_relayout (combo_box);
3010
3011       g_object_notify (G_OBJECT (combo_box), "column_span_column");
3012     }
3013 }
3014
3015 /**
3016  * gtk_combo_box_get_active:
3017  * @combo_box: A #GtkComboBox.
3018  *
3019  * Returns the index of the currently active item, or -1 if there's no
3020  * active item.
3021  *
3022  * Return value: An integer which is the index of the currently active item, or
3023  * -1 if there's no active item.
3024  *
3025  * Since: 2.4
3026  */
3027 gint
3028 gtk_combo_box_get_active (GtkComboBox *combo_box)
3029 {
3030   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
3031
3032   return combo_box->priv->active_item;
3033 }
3034
3035 /**
3036  * gtk_combo_box_set_active:
3037  * @combo_box: A #GtkComboBox.
3038  * @index: An index in the model passed during construction, or -1 to have
3039  * no active item.
3040  *
3041  * Sets the active item of @combo_box to be the item at @index.
3042  *
3043  * Since: 2.4
3044  */
3045 void
3046 gtk_combo_box_set_active (GtkComboBox *combo_box,
3047                           gint         index)
3048 {
3049   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3050   /* -1 means "no item selected" */
3051   g_return_if_fail (index >= -1);
3052
3053   if (combo_box->priv->active_item == index)
3054     return;
3055   
3056   gtk_combo_box_set_active_internal (combo_box, index);
3057 }
3058
3059 static void
3060 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
3061                                    gint         index)
3062 {
3063   GtkTreePath *path;
3064
3065   combo_box->priv->active_item = index;
3066
3067   if (index == -1)
3068     {
3069       if (combo_box->priv->tree_view)
3070         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
3071       else
3072         {
3073           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3074
3075           if (GTK_IS_MENU (menu))
3076             gtk_menu_set_active (menu, -1);
3077         }
3078
3079       if (combo_box->priv->cell_view)
3080         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
3081     }
3082   else
3083     {
3084       path = gtk_tree_path_new_from_indices (index, -1);
3085
3086       if (combo_box->priv->tree_view)
3087         gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
3088       else
3089         {
3090           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
3091
3092           if (GTK_IS_MENU (menu))
3093             gtk_menu_set_active (GTK_MENU (menu), index);
3094         }
3095
3096       if (combo_box->priv->cell_view)
3097         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
3098
3099       gtk_tree_path_free (path);
3100     }
3101
3102   g_signal_emit_by_name (combo_box, "changed", NULL, NULL);
3103 }
3104
3105
3106 /**
3107  * gtk_combo_box_get_active_iter:
3108  * @combo_box: A #GtkComboBox
3109  * @iter: The uninitialized #GtkTreeIter.
3110  * 
3111  * Sets @iter to point to the current active item, if it exists.
3112  * 
3113  * Return value: %TRUE, if @iter was set
3114  *
3115  * Since: 2.4
3116  **/
3117 gboolean
3118 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
3119                                GtkTreeIter     *iter)
3120 {
3121   GtkTreePath *path;
3122   gint active;
3123   gboolean retval;
3124
3125   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
3126
3127   active = gtk_combo_box_get_active (combo_box);
3128   if (active < 0)
3129     return FALSE;
3130
3131   path = gtk_tree_path_new_from_indices (active, -1);
3132   retval = gtk_tree_model_get_iter (gtk_combo_box_get_model (combo_box),
3133                                     iter, path);
3134   gtk_tree_path_free (path);
3135
3136   return retval;
3137 }
3138
3139 /**
3140  * gtk_combo_box_set_active_iter:
3141  * @combo_box: A #GtkComboBox
3142  * @iter: The #GtkTreeIter.
3143  * 
3144  * Sets the current active item to be the one referenced by @iter. 
3145  * @iter must correspond to a path of depth one.
3146  * 
3147  * Since: 2.4
3148  **/
3149 void
3150 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
3151                                GtkTreeIter     *iter)
3152 {
3153   GtkTreePath *path;
3154
3155   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3156
3157   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
3158   g_return_if_fail (path != NULL);
3159   g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
3160   
3161   gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
3162   gtk_tree_path_free (path);
3163 }
3164
3165 /**
3166  * gtk_combo_box_set_model:
3167  * @combo_box: A #GtkComboBox.
3168  * @model: A #GtkTreeModel.
3169  *
3170  * Sets the model used by @combo_box to be @model. Will unset a
3171  * previously set model (if applicable).
3172  *
3173  * Since: 2.4
3174  */
3175 void
3176 gtk_combo_box_set_model (GtkComboBox  *combo_box,
3177                          GtkTreeModel *model)
3178 {
3179   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3180   g_return_if_fail (GTK_IS_TREE_MODEL (model));
3181
3182   if (model == combo_box->priv->model)
3183     return;
3184   
3185   if (combo_box->priv->model)
3186     gtk_combo_box_unset_model (combo_box);
3187
3188   combo_box->priv->model = model;
3189   g_object_ref (G_OBJECT (combo_box->priv->model));
3190
3191   combo_box->priv->inserted_id =
3192     g_signal_connect (combo_box->priv->model, "row_inserted",
3193                       G_CALLBACK (gtk_combo_box_model_row_inserted),
3194                       combo_box);
3195   combo_box->priv->deleted_id =
3196     g_signal_connect (combo_box->priv->model, "row_deleted",
3197                       G_CALLBACK (gtk_combo_box_model_row_deleted),
3198                       combo_box);
3199   combo_box->priv->reordered_id =
3200     g_signal_connect (combo_box->priv->model, "rows_reordered",
3201                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
3202                       combo_box);
3203   combo_box->priv->changed_id =
3204     g_signal_connect (combo_box->priv->model, "row_changed",
3205                       G_CALLBACK (gtk_combo_box_model_row_changed),
3206                       combo_box);
3207       
3208   if (combo_box->priv->tree_view)
3209     {
3210       /* list mode */
3211       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3212                                combo_box->priv->model);
3213     }
3214   else
3215     {
3216       /* menu mode */
3217       if (combo_box->priv->popup_widget)
3218         gtk_combo_box_menu_fill (combo_box);
3219
3220     }
3221
3222   if (combo_box->priv->cell_view)
3223     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
3224                              combo_box->priv->model);
3225 }
3226
3227 /**
3228  * gtk_combo_box_get_model
3229  * @combo_box: A #GtkComboBox.
3230  *
3231  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
3232  *
3233  * Return value: A #GtkTreeModel which was passed during construction.
3234  *
3235  * Since: 2.4
3236  */
3237 GtkTreeModel *
3238 gtk_combo_box_get_model (GtkComboBox *combo_box)
3239 {
3240   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
3241
3242   return combo_box->priv->model;
3243 }
3244
3245
3246 /* convenience API for simple text combos */
3247
3248 /**
3249  * gtk_combo_box_new_text:
3250  *
3251  * Convenience function which constructs a new text combo box, which is a
3252  * #GtkComboBox just displaying strings. If you use this function to create
3253  * a text combo box, you should only manipulate its data source with the
3254  * following convenience functions: gtk_combo_box_append_text(),
3255  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
3256  * gtk_combo_box_remove_text().
3257  *
3258  * Return value: A new text combo box.
3259  *
3260  * Since: 2.4
3261  */
3262 GtkWidget *
3263 gtk_combo_box_new_text (void)
3264 {
3265   GtkWidget *combo_box;
3266   GtkCellRenderer *cell;
3267   GtkListStore *store;
3268
3269   store = gtk_list_store_new (1, G_TYPE_STRING);
3270
3271   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
3272
3273   cell = gtk_cell_renderer_text_new ();
3274   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
3275   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
3276                                   "text", 0,
3277                                   NULL);
3278
3279   return combo_box;
3280 }
3281
3282 /**
3283  * gtk_combo_box_append_text:
3284  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3285  * @text: A string.
3286  *
3287  * Appends @string to the list of strings stored in @combo_box. Note that
3288  * you can only use this function with combo boxes constructed with
3289  * gtk_combo_box_new_text().
3290  *
3291  * Since: 2.4
3292  */
3293 void
3294 gtk_combo_box_append_text (GtkComboBox *combo_box,
3295                            const gchar *text)
3296 {
3297   GtkTreeIter iter;
3298   GtkListStore *store;
3299
3300   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3301   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3302   g_return_if_fail (text != NULL);
3303
3304   store = GTK_LIST_STORE (combo_box->priv->model);
3305
3306   gtk_list_store_append (store, &iter);
3307   gtk_list_store_set (store, &iter, 0, text, -1);
3308 }
3309
3310 /**
3311  * gtk_combo_box_insert_text:
3312  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
3313  * @position: An index to insert @text.
3314  * @text: A string.
3315  *
3316  * Inserts @string at @position in the list of strings stored in @combo_box.
3317  * Note that you can only use this function with combo boxes constructed
3318  * with gtk_combo_box_new_text().
3319  *
3320  * Since: 2.4
3321  */
3322 void
3323 gtk_combo_box_insert_text (GtkComboBox *combo_box,
3324                            gint         position,
3325                            const gchar *text)
3326 {
3327   GtkTreeIter iter;
3328   GtkListStore *store;
3329
3330   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3331   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3332   g_return_if_fail (position >= 0);
3333   g_return_if_fail (text != NULL);
3334
3335   store = GTK_LIST_STORE (combo_box->priv->model);
3336
3337   gtk_list_store_insert (store, &iter, position);
3338   gtk_list_store_set (store, &iter, 0, text, -1);
3339 }
3340
3341 /**
3342  * gtk_combo_box_prepend_text:
3343  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3344  * @text: A string.
3345  *
3346  * Prepends @string to the list of strings stored in @combo_box. Note that
3347  * you can only use this function with combo boxes constructed with
3348  * gtk_combo_box_new_text().
3349  *
3350  * Since: 2.4
3351  */
3352 void
3353 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
3354                             const gchar *text)
3355 {
3356   GtkTreeIter iter;
3357   GtkListStore *store;
3358
3359   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3360   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3361   g_return_if_fail (text != NULL);
3362
3363   store = GTK_LIST_STORE (combo_box->priv->model);
3364
3365   gtk_list_store_prepend (store, &iter);
3366   gtk_list_store_set (store, &iter, 0, text, -1);
3367 }
3368
3369 /**
3370  * gtk_combo_box_remove_text:
3371  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
3372  * @position: Index of the item to remove.
3373  *
3374  * Removes the string at @position from @combo_box. Note that you can only use
3375  * this function with combo boxes constructed with gtk_combo_box_new_text().
3376  *
3377  * Since: 2.4
3378  */
3379 void
3380 gtk_combo_box_remove_text (GtkComboBox *combo_box,
3381                            gint         position)
3382 {
3383   GtkTreeIter iter;
3384   GtkListStore *store;
3385
3386   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
3387   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
3388   g_return_if_fail (position >= 0);
3389
3390   store = GTK_LIST_STORE (combo_box->priv->model);
3391
3392   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
3393                                      NULL, position))
3394     gtk_list_store_remove (store, &iter);
3395 }
3396
3397
3398 static gboolean
3399 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
3400                                  gboolean   group_cycling)
3401 {
3402   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
3403
3404   gtk_widget_grab_focus (combo_box->priv->button);
3405
3406   return TRUE;
3407 }
3408
3409 static void
3410 gtk_combo_box_destroy (GtkObject *object)
3411 {
3412   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3413
3414   combo_box->priv->destroying = 1;
3415
3416   GTK_OBJECT_CLASS (parent_class)->destroy (object);
3417
3418   combo_box->priv->cell_view = NULL;
3419
3420   combo_box->priv->destroying = 0;
3421 }
3422
3423 static void
3424 gtk_combo_box_finalize (GObject *object)
3425 {
3426   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
3427   GSList *i;
3428   
3429   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3430     gtk_combo_box_menu_destroy (combo_box);
3431   
3432   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
3433     gtk_combo_box_list_destroy (combo_box);
3434
3435   if (combo_box->priv->popup_window)
3436     gtk_widget_destroy (combo_box->priv->popup_window);
3437
3438   gtk_combo_box_unset_model (combo_box);
3439
3440   if (combo_box->priv->model)
3441     g_object_unref (combo_box->priv->model);
3442
3443   for (i = combo_box->priv->cells; i; i = i->next)
3444     {
3445       ComboCellInfo *info = (ComboCellInfo *)i->data;
3446       GSList *list = info->attributes;
3447
3448       if (info->destroy)
3449         info->destroy (info->func_data);
3450
3451       while (list && list->next)
3452         {
3453           g_free (list->data);
3454           list = list->next->next;
3455         }
3456       g_slist_free (info->attributes);
3457
3458       g_object_unref (G_OBJECT (info->cell));
3459       g_free (info);
3460     }
3461    g_slist_free (combo_box->priv->cells);
3462
3463    G_OBJECT_CLASS (parent_class)->finalize (object);
3464 }