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