]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Fix a crash with combo boxes in RESIZE_IMMEDIATE containers. (#326806,
[~andy/gtk] / gtk / gtkcombobox.c
1 /* gtkcombobox.c
2  * Copyright (C) 2002, 2003  Kristian Rietveld <kris@gtk.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21 #include "gtkcombobox.h"
22
23 #include "gtkarrow.h"
24 #include "gtkbindings.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellview.h"
28 #include "gtkeventbox.h"
29 #include "gtkframe.h"
30 #include "gtkhbox.h"
31 #include "gtkliststore.h"
32 #include "gtkmain.h"
33 #include "gtkmenu.h"
34 #include "gtkscrolledwindow.h"
35 #include "gtkseparatormenuitem.h"
36 #include "gtktearoffmenuitem.h"
37 #include "gtktogglebutton.h"
38 #include "gtktreeselection.h"
39 #include "gtkvseparator.h"
40 #include "gtkwindow.h"
41 #include "gtkprivate.h"
42
43 #include <gdk/gdkkeysyms.h>
44
45 #include <gobject/gvaluecollector.h>
46
47 #include <string.h>
48 #include <stdarg.h>
49
50 #include "gtkmarshalers.h"
51 #include "gtkintl.h"
52
53 #include "gtktreeprivate.h"
54 #include "gtkalias.h"
55
56 /* WELCOME, to THE house of evil code */
57
58 typedef struct _ComboCellInfo ComboCellInfo;
59 struct _ComboCellInfo
60 {
61   GtkCellRenderer *cell;
62   GSList *attributes;
63
64   GtkCellLayoutDataFunc func;
65   gpointer func_data;
66   GDestroyNotify destroy;
67
68   guint expand : 1;
69   guint pack : 1;
70 };
71
72 #define GTK_COMBO_BOX_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX, GtkComboBoxPrivate))
73
74 struct _GtkComboBoxPrivate
75 {
76   GtkTreeModel *model;
77
78   gint col_column;
79   gint row_column;
80
81   gint wrap_width;
82
83   GtkTreeRowReference *active_row;
84
85   GtkWidget *tree_view;
86   GtkTreeViewColumn *column;
87
88   GtkWidget *cell_view;
89   GtkWidget *cell_view_frame;
90
91   GtkWidget *button;
92   GtkWidget *box;
93   GtkWidget *arrow;
94   GtkWidget *separator;
95
96   GtkWidget *popup_widget;
97   GtkWidget *popup_window;
98   GtkWidget *popup_frame;
99   GtkWidget *scrolled_window;
100
101   guint inserted_id;
102   guint deleted_id;
103   guint reordered_id;
104   guint changed_id;
105   guint popup_idle_id;
106   guint scroll_timer;
107   guint resize_idle_id;
108
109   gint width;
110   GSList *cells;
111
112   guint popup_in_progress : 1;
113   guint destroying : 1;
114   guint add_tearoffs : 1;
115   guint has_frame : 1;
116   guint is_cell_renderer : 1;
117   guint editing_canceled : 1;
118   guint auto_scroll : 1;
119   guint focus_on_click : 1;
120
121   GtkTreeViewRowSeparatorFunc row_separator_func;
122   gpointer                    row_separator_data;
123   GtkDestroyNotify            row_separator_destroy;
124
125   gchar *tearoff_title;
126 };
127
128 /* While debugging this evil code, I have learned that
129  * there are actually 4 modes to this widget, which can
130  * be characterized as follows
131  * 
132  * 1) menu mode, no child added
133  *
134  * tree_view -> NULL
135  * cell_view -> GtkCellView, regular child
136  * cell_view_frame -> NULL
137  * button -> GtkToggleButton set_parent to combo
138  * arrow -> GtkArrow set_parent to button
139  * separator -> GtkVSepator set_parent to button
140  * popup_widget -> GtkMenu
141  * popup_window -> NULL
142  * popup_frame -> NULL
143  * scrolled_window -> NULL
144  *
145  * 2) menu mode, child added
146  * 
147  * tree_view -> NULL
148  * cell_view -> NULL 
149  * cell_view_frame -> NULL
150  * button -> GtkToggleButton set_parent to combo
151  * arrow -> GtkArrow, child of button
152  * separator -> NULL
153  * popup_widget -> GtkMenu
154  * popup_window -> NULL
155  * popup_frame -> NULL
156  * scrolled_window -> NULL
157  *
158  * 3) list mode, no child added
159  * 
160  * tree_view -> GtkTreeView, child of popup_frame
161  * cell_view -> GtkCellView, regular child
162  * cell_view_frame -> GtkFrame, set parent to combo
163  * button -> GtkToggleButton, set_parent to combo
164  * arrow -> GtkArrow, child of button
165  * separator -> NULL
166  * popup_widget -> tree_view
167  * popup_window -> GtkWindow
168  * popup_frame -> GtkFrame, child of popup_window
169  * scrolled_window -> GtkScrolledWindow, child of popup_frame
170  *
171  * 4) list mode, child added
172  *
173  * tree_view -> GtkTreeView, child of popup_frame
174  * cell_view -> NULL
175  * cell_view_frame -> NULL
176  * button -> GtkToggleButton, set_parent to combo
177  * arrow -> GtkArrow, child of button
178  * separator -> NULL
179  * popup_widget -> tree_view
180  * popup_window -> GtkWindow
181  * popup_frame -> GtkFrame, child of popup_window
182  * scrolled_window -> GtkScrolledWindow, child of popup_frame
183  * 
184  */
185
186 enum {
187   CHANGED,
188   POPUP_SHOW,
189   POPUP_HIDE,
190   LAST_SIGNAL
191 };
192
193 enum {
194   PROP_0,
195   PROP_MODEL,
196   PROP_WRAP_WIDTH,
197   PROP_ROW_SPAN_COLUMN,
198   PROP_COLUMN_SPAN_COLUMN,
199   PROP_ACTIVE,
200   PROP_ADD_TEAROFFS,
201   PROP_TEAROFF_TITLE,
202   PROP_HAS_FRAME,
203   PROP_FOCUS_ON_CLICK
204 };
205
206 static GtkBinClass *parent_class = NULL;
207 static guint combo_box_signals[LAST_SIGNAL] = {0,};
208
209 #define BONUS_PADDING 4
210 #define SCROLL_TIME  100
211
212 /* common */
213
214 static void     gtk_combo_box_class_init           (GtkComboBoxClass *klass);
215 static void     gtk_combo_box_cell_layout_init     (GtkCellLayoutIface *iface);
216 static void     gtk_combo_box_cell_editable_init   (GtkCellEditableIface *iface);
217 static void     gtk_combo_box_init                 (GtkComboBox      *combo_box);
218 static void     gtk_combo_box_finalize             (GObject          *object);
219 static void     gtk_combo_box_destroy              (GtkObject        *object);
220
221 static void     gtk_combo_box_set_property         (GObject         *object,
222                                                     guint            prop_id,
223                                                     const GValue    *value,
224                                                     GParamSpec      *spec);
225 static void     gtk_combo_box_get_property         (GObject         *object,
226                                                     guint            prop_id,
227                                                     GValue          *value,
228                                                     GParamSpec      *spec);
229
230 static void     gtk_combo_box_state_changed        (GtkWidget        *widget,
231                                                     GtkStateType      previous);
232 static void     gtk_combo_box_grab_focus           (GtkWidget       *widget);
233 static void     gtk_combo_box_style_set            (GtkWidget       *widget,
234                                                     GtkStyle        *previous);
235 static void     gtk_combo_box_button_toggled       (GtkWidget       *widget,
236                                                     gpointer         data);
237 static void     gtk_combo_box_button_state_changed (GtkWidget       *widget,
238                                                     GtkStateType     previous,
239                                                     gpointer         data);
240 static void     gtk_combo_box_add                  (GtkContainer    *container,
241                                                     GtkWidget       *widget);
242 static void     gtk_combo_box_remove               (GtkContainer    *container,
243                                                     GtkWidget       *widget);
244
245 static ComboCellInfo *gtk_combo_box_get_cell_info  (GtkComboBox      *combo_box,
246                                                     GtkCellRenderer  *cell);
247
248 static void     gtk_combo_box_menu_show            (GtkWidget        *menu,
249                                                     gpointer          user_data);
250 static void     gtk_combo_box_menu_hide            (GtkWidget        *menu,
251                                                     gpointer          user_data);
252
253 static void     gtk_combo_box_set_popup_widget     (GtkComboBox      *combo_box,
254                                                     GtkWidget        *popup);
255 static void     gtk_combo_box_menu_position_below  (GtkMenu          *menu,
256                                                     gint             *x,
257                                                     gint             *y,
258                                                     gint             *push_in,
259                                                     gpointer          user_data);
260 static void     gtk_combo_box_menu_position_over   (GtkMenu          *menu,
261                                                     gint             *x,
262                                                     gint             *y,
263                                                     gint             *push_in,
264                                                     gpointer          user_data);
265 static void     gtk_combo_box_menu_position        (GtkMenu          *menu,
266                                                     gint             *x,
267                                                     gint             *y,
268                                                     gint             *push_in,
269                                                     gpointer          user_data);
270
271 static gint     gtk_combo_box_calc_requested_width (GtkComboBox      *combo_box,
272                                                     GtkTreePath      *path);
273 static void     gtk_combo_box_remeasure            (GtkComboBox      *combo_box);
274
275 static void     gtk_combo_box_unset_model          (GtkComboBox      *combo_box);
276
277 static void     gtk_combo_box_size_request         (GtkWidget        *widget,
278                                                     GtkRequisition   *requisition);
279 static void     gtk_combo_box_size_allocate        (GtkWidget        *widget,
280                                                     GtkAllocation    *allocation);
281 static void     gtk_combo_box_forall               (GtkContainer     *container,
282                                                     gboolean          include_internals,
283                                                     GtkCallback       callback,
284                                                     gpointer          callback_data);
285 static gboolean gtk_combo_box_expose_event         (GtkWidget        *widget,
286                                                     GdkEventExpose   *event);
287 static gboolean gtk_combo_box_scroll_event         (GtkWidget        *widget,
288                                                     GdkEventScroll   *event);
289 static void     gtk_combo_box_set_active_internal  (GtkComboBox      *combo_box,
290                                                     GtkTreePath      *path);
291 static gboolean gtk_combo_box_key_press            (GtkWidget        *widget,
292                                                     GdkEventKey      *event,
293                                                     gpointer          data);
294
295 static void     gtk_combo_box_check_appearance     (GtkComboBox      *combo_box);
296 static gchar *  gtk_combo_box_real_get_active_text (GtkComboBox      *combo_box);
297
298 /* listening to the model */
299 static void     gtk_combo_box_model_row_inserted   (GtkTreeModel     *model,
300                                                     GtkTreePath      *path,
301                                                     GtkTreeIter      *iter,
302                                                     gpointer          user_data);
303 static void     gtk_combo_box_model_row_deleted    (GtkTreeModel     *model,
304                                                     GtkTreePath      *path,
305                                                     gpointer          user_data);
306 static void     gtk_combo_box_model_rows_reordered (GtkTreeModel     *model,
307                                                     GtkTreePath      *path,
308                                                     GtkTreeIter      *iter,
309                                                     gint             *new_order,
310                                                     gpointer          user_data);
311 static void     gtk_combo_box_model_row_changed    (GtkTreeModel     *model,
312                                                     GtkTreePath      *path,
313                                                     GtkTreeIter      *iter,
314                                                     gpointer          data);
315 static void     gtk_combo_box_model_row_expanded   (GtkTreeModel     *model,
316                                                     GtkTreePath      *path,
317                                                     GtkTreeIter      *iter,
318                                                     gpointer          data);
319
320 /* list */
321 static void     gtk_combo_box_list_position        (GtkComboBox      *combo_box, 
322                                                     gint             *x, 
323                                                     gint             *y, 
324                                                     gint             *width,
325                                                     gint             *height);
326 static void     gtk_combo_box_list_setup           (GtkComboBox      *combo_box);
327 static void     gtk_combo_box_list_destroy         (GtkComboBox      *combo_box);
328
329 static gboolean gtk_combo_box_list_button_released (GtkWidget        *widget,
330                                                     GdkEventButton   *event,
331                                                     gpointer          data);
332 static gboolean gtk_combo_box_list_key_press       (GtkWidget        *widget,
333                                                     GdkEventKey      *event,
334                                                     gpointer          data);
335 static gboolean gtk_combo_box_list_enter_notify    (GtkWidget        *widget,
336                                                     GdkEventCrossing *event,
337                                                     gpointer          data);
338 static void     gtk_combo_box_list_auto_scroll     (GtkComboBox   *combo,
339                                                     gint           x,
340                                                     gint           y);
341 static gboolean gtk_combo_box_list_scroll_timeout  (GtkComboBox   *combo);
342 static gboolean gtk_combo_box_list_button_pressed  (GtkWidget        *widget,
343                                                     GdkEventButton   *event,
344                                                     gpointer          data);
345
346 static void     gtk_combo_box_list_row_changed     (GtkTreeModel     *model,
347                                                     GtkTreePath      *path,
348                                                     GtkTreeIter      *iter,
349                                                     gpointer          data);
350 static void     gtk_combo_box_list_popup_resize    (GtkComboBox      *combo_box);
351
352 /* menu */
353 static void     gtk_combo_box_menu_setup           (GtkComboBox      *combo_box,
354                                                     gboolean          add_children);
355 static void     gtk_combo_box_menu_fill            (GtkComboBox      *combo_box);
356 static void     gtk_combo_box_menu_fill_level      (GtkComboBox      *combo_box,
357                                                     GtkWidget        *menu,
358                                                     GtkTreeIter      *iter);
359 static void     gtk_combo_box_update_title         (GtkComboBox      *combo_box);
360 static void     gtk_combo_box_menu_destroy         (GtkComboBox      *combo_box);
361
362 static void     gtk_combo_box_relayout_item        (GtkComboBox      *combo_box,
363                                                     GtkWidget        *item,
364                                                     GtkTreeIter      *iter,
365                                                     GtkWidget        *last);
366 static void     gtk_combo_box_relayout             (GtkComboBox      *combo_box);
367
368 static gboolean gtk_combo_box_menu_button_press    (GtkWidget        *widget,
369                                                     GdkEventButton   *event,
370                                                     gpointer          user_data);
371 static void     gtk_combo_box_menu_item_activate   (GtkWidget        *item,
372                                                     gpointer          user_data);
373 static void     gtk_combo_box_menu_row_inserted    (GtkTreeModel     *model,
374                                                     GtkTreePath      *path,
375                                                     GtkTreeIter      *iter,
376                                                     gpointer          user_data);
377 static void     gtk_combo_box_menu_row_deleted     (GtkTreeModel     *model,
378                                                     GtkTreePath      *path,
379                                                     gpointer          user_data);
380 static void     gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
381                                                     GtkTreePath      *path,
382                                                     GtkTreeIter      *iter,
383                                                     gint             *new_order,
384                                                     gpointer          user_data);
385 static void     gtk_combo_box_menu_row_changed     (GtkTreeModel     *model,
386                                                     GtkTreePath      *path,
387                                                     GtkTreeIter      *iter,
388                                                     gpointer          data);
389 static gboolean gtk_combo_box_menu_key_press       (GtkWidget        *widget,
390                                                     GdkEventKey      *event,
391                                                     gpointer          data);
392 static void     gtk_combo_box_menu_popup           (GtkComboBox      *combo_box,
393                                                     guint             button, 
394                                                     guint32           activate_time);
395 static GtkWidget *gtk_cell_view_menu_item_new      (GtkComboBox      *combo_box,
396                                                     GtkTreeModel     *model,
397                                                     GtkTreeIter      *iter);
398
399 /* cell layout */
400 static void     gtk_combo_box_cell_layout_pack_start         (GtkCellLayout         *layout,
401                                                               GtkCellRenderer       *cell,
402                                                               gboolean               expand);
403 static void     gtk_combo_box_cell_layout_pack_end           (GtkCellLayout         *layout,
404                                                               GtkCellRenderer       *cell,
405                                                               gboolean               expand);
406 static void     gtk_combo_box_cell_layout_clear              (GtkCellLayout         *layout);
407 static void     gtk_combo_box_cell_layout_add_attribute      (GtkCellLayout         *layout,
408                                                               GtkCellRenderer       *cell,
409                                                               const gchar           *attribute,
410                                                               gint                   column);
411 static void     gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
412                                                               GtkCellRenderer       *cell,
413                                                               GtkCellLayoutDataFunc  func,
414                                                               gpointer               func_data,
415                                                               GDestroyNotify         destroy);
416 static void     gtk_combo_box_cell_layout_clear_attributes   (GtkCellLayout         *layout,
417                                                               GtkCellRenderer       *cell);
418 static void     gtk_combo_box_cell_layout_reorder            (GtkCellLayout         *layout,
419                                                               GtkCellRenderer       *cell,
420                                                               gint                   position);
421 static gboolean gtk_combo_box_mnemonic_activate              (GtkWidget    *widget,
422                                                               gboolean      group_cycling);
423
424 static void     gtk_combo_box_sync_cells                     (GtkComboBox   *combo_box,
425                                                               GtkCellLayout *cell_layout);
426 static void     combo_cell_data_func                         (GtkCellLayout   *cell_layout,
427                                                               GtkCellRenderer *cell,
428                                                               GtkTreeModel    *tree_model,
429                                                               GtkTreeIter     *iter,
430                                                               gpointer         data);
431 static void     gtk_combo_box_child_show                     (GtkWidget       *widget,
432                                                               gpointer         user_data);
433 static void     gtk_combo_box_child_hide                     (GtkWidget       *widget,
434                                                               gpointer         user_data);
435
436
437 /* GtkCellEditable method implementations */
438 static void gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
439                                          GdkEvent        *event);
440
441
442 GType
443 gtk_combo_box_get_type (void)
444 {
445   static GType combo_box_type = 0;
446
447   if (!combo_box_type)
448     {
449       static const GTypeInfo combo_box_info =
450         {
451           sizeof (GtkComboBoxClass),
452           NULL, /* base_init */
453           NULL, /* base_finalize */
454           (GClassInitFunc) gtk_combo_box_class_init,
455           NULL, /* class_finalize */
456           NULL, /* class_data */
457           sizeof (GtkComboBox),
458           0,
459           (GInstanceInitFunc) gtk_combo_box_init
460         };
461
462       static const GInterfaceInfo cell_layout_info =
463         {
464           (GInterfaceInitFunc) gtk_combo_box_cell_layout_init,
465           NULL,
466           NULL
467         };
468
469       static const GInterfaceInfo cell_editable_info =
470         {
471           (GInterfaceInitFunc) gtk_combo_box_cell_editable_init,
472           NULL,
473           NULL
474        };
475
476       combo_box_type = g_type_register_static (GTK_TYPE_BIN,
477                                                I_("GtkComboBox"),
478                                                &combo_box_info,
479                                                0);
480
481       g_type_add_interface_static (combo_box_type,
482                                    GTK_TYPE_CELL_LAYOUT,
483                                    &cell_layout_info);
484
485
486       g_type_add_interface_static (combo_box_type,
487                                    GTK_TYPE_CELL_EDITABLE,
488                                    &cell_editable_info);
489       
490
491     }
492
493   return combo_box_type;
494 }
495
496 /* common */
497 static void
498 gtk_combo_box_class_init (GtkComboBoxClass *klass)
499 {
500   GObjectClass *object_class;
501   GtkObjectClass *gtk_object_class;
502   GtkContainerClass *container_class;
503   GtkWidgetClass *widget_class;
504
505   klass->get_active_text = gtk_combo_box_real_get_active_text;
506
507   container_class = (GtkContainerClass *)klass;
508   container_class->forall = gtk_combo_box_forall;
509   container_class->add = gtk_combo_box_add;
510   container_class->remove = gtk_combo_box_remove;
511
512   widget_class = (GtkWidgetClass *)klass;
513   widget_class->size_allocate = gtk_combo_box_size_allocate;
514   widget_class->size_request = gtk_combo_box_size_request;
515   widget_class->expose_event = gtk_combo_box_expose_event;
516   widget_class->scroll_event = gtk_combo_box_scroll_event;
517   widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
518   widget_class->grab_focus = gtk_combo_box_grab_focus;
519   widget_class->style_set = gtk_combo_box_style_set;
520   widget_class->state_changed = gtk_combo_box_state_changed;
521
522   gtk_object_class = (GtkObjectClass *)klass;
523   gtk_object_class->destroy = gtk_combo_box_destroy;
524
525   object_class = (GObjectClass *)klass;
526   object_class->finalize = gtk_combo_box_finalize;
527   object_class->set_property = gtk_combo_box_set_property;
528   object_class->get_property = gtk_combo_box_get_property;
529
530   parent_class = g_type_class_peek_parent (klass);
531
532   /* signals */
533   /**
534    * GtkComboBox::changed:
535    * @widget: the object which received the signal
536    * 
537    * The changed signal gets emitted when the active
538    * item is changed. The can be due to the user selecting
539    * a different item from the list, or due to a 
540    * call to gtk_combo_box_set_active_iter().
541    *
542    * Since: 2.4
543    */
544   combo_box_signals[CHANGED] =
545     g_signal_new (I_("changed"),
546                   G_OBJECT_CLASS_TYPE (klass),
547                   G_SIGNAL_RUN_LAST,
548                   G_STRUCT_OFFSET (GtkComboBoxClass, changed),
549                   NULL, NULL,
550                   g_cclosure_marshal_VOID__VOID,
551                   G_TYPE_NONE, 0);
552
553   combo_box_signals[POPUP_SHOW] =
554     g_signal_new ("popup-show",
555                   G_OBJECT_CLASS_TYPE (klass),
556                   G_SIGNAL_RUN_LAST,
557                   0,
558                   NULL, NULL,
559                   g_cclosure_marshal_VOID__VOID,
560                   G_TYPE_NONE, 0);
561
562   combo_box_signals[POPUP_HIDE] =
563     g_signal_new ("popup-hide",
564                   G_OBJECT_CLASS_TYPE (klass),
565                   G_SIGNAL_RUN_LAST,
566                   0,
567                   NULL, NULL,
568                   g_cclosure_marshal_VOID__VOID,
569                   G_TYPE_NONE, 0);
570
571
572   /* properties */
573   /**
574    * GtkComboBox:model:
575    *
576    * The model from which the combo box takes the values shown
577    * in the list. 
578    *
579    * Since: 2.4
580    */
581   g_object_class_install_property (object_class,
582                                    PROP_MODEL,
583                                    g_param_spec_object ("model",
584                                                         P_("ComboBox model"),
585                                                         P_("The model for the combo box"),
586                                                         GTK_TYPE_TREE_MODEL,
587                                                         GTK_PARAM_READWRITE));
588
589   /**
590    * GtkComboBox:wrap-width:
591    *
592    * If wrap-width is set to a positive value, the list will be
593    * displayed in multiple columns, the number of columns is
594    * determined by wrap-width.
595    *
596    * Since: 2.4
597    */
598   g_object_class_install_property (object_class,
599                                    PROP_WRAP_WIDTH,
600                                    g_param_spec_int ("wrap-width",
601                                                      P_("Wrap width"),
602                                                      P_("Wrap width for laying out the items in a grid"),
603                                                      0,
604                                                      G_MAXINT,
605                                                      0,
606                                                      GTK_PARAM_READWRITE));
607
608
609   /**
610    * GtkComboBox:row-span-column:
611    *
612    * If this is set to a non-negative value, it must be the index of a column 
613    * of type %G_TYPE_INT in the model. 
614    *
615    * The values of that column are used to determine how many rows a value 
616    * in the list will span. Therefore, the values in the model column pointed 
617    * to by this property must be greater than zero and not larger than wrap-width.
618    *
619    * Since: 2.4
620    */
621   g_object_class_install_property (object_class,
622                                    PROP_ROW_SPAN_COLUMN,
623                                    g_param_spec_int ("row-span-column",
624                                                      P_("Row span column"),
625                                                      P_("TreeModel column containing the row span values"),
626                                                      -1,
627                                                      G_MAXINT,
628                                                      -1,
629                                                      GTK_PARAM_READWRITE));
630
631
632   /**
633    * GtkComboBox:column-span-column:
634    *
635    * If this is set to a non-negative value, it must be the index of a column 
636    * of type %G_TYPE_INT in the model. 
637    *
638    * The values of that column are used to determine how many columns a value 
639    * in the list will span. 
640    *
641    * Since: 2.4
642    */
643   g_object_class_install_property (object_class,
644                                    PROP_COLUMN_SPAN_COLUMN,
645                                    g_param_spec_int ("column-span-column",
646                                                      P_("Column span column"),
647                                                      P_("TreeModel column containing the column span values"),
648                                                      -1,
649                                                      G_MAXINT,
650                                                      -1,
651                                                      GTK_PARAM_READWRITE));
652
653
654   /**
655    * GtkComboBox:active:
656    *
657    * The item which is currently active. If the model is a non-flat treemodel,
658    * and the active item is not an immediate child of the root of the tree,
659    * this property has the value <literal>gtk_tree_path_get_indices (path)[0]</literal>,
660    * where <literal>path</literal> is the #GtkTreePath of the active item.
661    *
662    * Since: 2.4
663    */
664   g_object_class_install_property (object_class,
665                                    PROP_ACTIVE,
666                                    g_param_spec_int ("active",
667                                                      P_("Active item"),
668                                                      P_("The item which is currently active"),
669                                                      -1,
670                                                      G_MAXINT,
671                                                      -1,
672                                                      GTK_PARAM_READWRITE));
673
674   /**
675    * GtkComboBox:add-tearoffs:
676    *
677    * The add-tearoffs property controls whether generated menus 
678    * have tearoff menu items. 
679    *
680    * Note that this only affects menu style combo boxes.
681    *
682    * Since: 2.6
683    */
684   g_object_class_install_property (object_class,
685                                    PROP_ADD_TEAROFFS,
686                                    g_param_spec_boolean ("add-tearoffs",
687                                                          P_("Add tearoffs to menus"),
688                                                          P_("Whether dropdowns should have a tearoff menu item"),
689                                                          FALSE,
690                                                          GTK_PARAM_READWRITE));
691   
692   /**
693    * GtkComboBox:has-frame:
694    *
695    * The has-frame property controls whether a frame
696    * is drawn around the entry.
697    *
698    * Since: 2.6
699    */
700   g_object_class_install_property (object_class,
701                                    PROP_HAS_FRAME,
702                                    g_param_spec_boolean ("has-frame",
703                                                          P_("Has Frame"),
704                                                          P_("Whether the combo box draws a frame around the child"),
705                                                          TRUE,
706                                                          GTK_PARAM_READWRITE));
707   
708   g_object_class_install_property (object_class,
709                                    PROP_FOCUS_ON_CLICK,
710                                    g_param_spec_boolean ("focus-on-click",
711                                                          P_("Focus on click"),
712                                                          P_("Whether the combo box grabs focus when it is clicked with the mouse"),
713                                                          TRUE,
714                                                          GTK_PARAM_READWRITE));
715
716   /**
717    * GtkComboBox:tearoff-title:
718    *
719    * A title that may be displayed by the window manager 
720    * when the popup is torn-off.
721    *
722    * Since: 2.10
723    */
724   g_object_class_install_property (object_class,
725                                    PROP_TEAROFF_TITLE,
726                                    g_param_spec_string ("tearoff-title",
727                                                         P_("Tearoff Title"),
728                                                         P_("A title that may be displayed by the window manager when the popup is torn-off"),
729                                                         "",
730                                                         GTK_PARAM_READWRITE));
731
732
733   gtk_widget_class_install_style_property (widget_class,
734                                            g_param_spec_boolean ("appears-as-list",
735                                                                  P_("Appears as list"),
736                                                                  P_("Whether dropdowns should look like lists rather than menus"),
737                                                                  FALSE,
738                                                                  GTK_PARAM_READABLE));
739
740   g_type_class_add_private (object_class, sizeof (GtkComboBoxPrivate));
741 }
742
743 static void
744 gtk_combo_box_cell_layout_init (GtkCellLayoutIface *iface)
745 {
746   iface->pack_start = gtk_combo_box_cell_layout_pack_start;
747   iface->pack_end = gtk_combo_box_cell_layout_pack_end;
748   iface->clear = gtk_combo_box_cell_layout_clear;
749   iface->add_attribute = gtk_combo_box_cell_layout_add_attribute;
750   iface->set_cell_data_func = gtk_combo_box_cell_layout_set_cell_data_func;
751   iface->clear_attributes = gtk_combo_box_cell_layout_clear_attributes;
752   iface->reorder = gtk_combo_box_cell_layout_reorder;
753 }
754
755 static void
756 gtk_combo_box_cell_editable_init (GtkCellEditableIface *iface)
757 {
758   iface->start_editing = gtk_combo_box_start_editing;
759 }
760
761 static void
762 gtk_combo_box_init (GtkComboBox *combo_box)
763 {
764   combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
765
766   combo_box->priv->cell_view = gtk_cell_view_new ();
767   gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
768   GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
769   gtk_widget_show (combo_box->priv->cell_view);
770
771   combo_box->priv->width = 0;
772   combo_box->priv->wrap_width = 0;
773
774   combo_box->priv->active_row = NULL;
775   combo_box->priv->col_column = -1;
776   combo_box->priv->row_column = -1;
777
778   combo_box->priv->add_tearoffs = FALSE;
779   combo_box->priv->has_frame = TRUE;
780   combo_box->priv->is_cell_renderer = FALSE;
781   combo_box->priv->editing_canceled = FALSE;
782   combo_box->priv->auto_scroll = FALSE;
783   combo_box->priv->focus_on_click = TRUE;
784
785   gtk_combo_box_check_appearance (combo_box);
786 }
787
788 static void
789 gtk_combo_box_set_property (GObject      *object,
790                             guint         prop_id,
791                             const GValue *value,
792                             GParamSpec   *pspec)
793 {
794   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
795
796   switch (prop_id)
797     {
798       case PROP_MODEL:
799         gtk_combo_box_set_model (combo_box, g_value_get_object (value));
800         break;
801
802       case PROP_WRAP_WIDTH:
803         gtk_combo_box_set_wrap_width (combo_box, g_value_get_int (value));
804         break;
805
806       case PROP_ROW_SPAN_COLUMN:
807         gtk_combo_box_set_row_span_column (combo_box, g_value_get_int (value));
808         break;
809
810       case PROP_COLUMN_SPAN_COLUMN:
811         gtk_combo_box_set_column_span_column (combo_box, g_value_get_int (value));
812         break;
813
814       case PROP_ACTIVE:
815         gtk_combo_box_set_active (combo_box, g_value_get_int (value));
816         break;
817
818       case PROP_ADD_TEAROFFS:
819         gtk_combo_box_set_add_tearoffs (combo_box, g_value_get_boolean (value));
820         break;
821
822       case PROP_HAS_FRAME:
823         combo_box->priv->has_frame = g_value_get_boolean (value);
824         break;
825
826       case PROP_FOCUS_ON_CLICK:
827         gtk_combo_box_set_focus_on_click (combo_box, 
828                                           g_value_get_boolean (value));
829         break;
830
831       case PROP_TEAROFF_TITLE:
832         gtk_combo_box_set_title (combo_box, g_value_get_string (value));
833         break;
834
835       default:
836         break;
837     }
838 }
839
840 static void
841 gtk_combo_box_get_property (GObject    *object,
842                             guint       prop_id,
843                             GValue     *value,
844                             GParamSpec *pspec)
845 {
846   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
847
848   switch (prop_id)
849     {
850       case PROP_MODEL:
851         g_value_set_object (value, combo_box->priv->model);
852         break;
853
854       case PROP_WRAP_WIDTH:
855         g_value_set_int (value, combo_box->priv->wrap_width);
856         break;
857
858       case PROP_ROW_SPAN_COLUMN:
859         g_value_set_int (value, combo_box->priv->row_column);
860         break;
861
862       case PROP_COLUMN_SPAN_COLUMN:
863         g_value_set_int (value, combo_box->priv->col_column);
864         break;
865
866       case PROP_ACTIVE:
867         g_value_set_int (value, gtk_combo_box_get_active (combo_box));
868         break;
869
870       case PROP_ADD_TEAROFFS:
871         g_value_set_boolean (value, gtk_combo_box_get_add_tearoffs (combo_box));
872         break;
873
874       case PROP_HAS_FRAME:
875         g_value_set_boolean (value, combo_box->priv->has_frame);
876         break;
877
878       case PROP_FOCUS_ON_CLICK:
879         g_value_set_boolean (value, combo_box->priv->focus_on_click);
880         break;
881
882       case PROP_TEAROFF_TITLE:
883         g_value_set_string (value, gtk_combo_box_get_title (combo_box));
884         break;
885
886       default:
887         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
888         break;
889     }
890 }
891
892 static void
893 gtk_combo_box_state_changed (GtkWidget    *widget,
894                              GtkStateType  previous)
895 {
896   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
897
898   if (GTK_WIDGET_REALIZED (widget))
899     {
900       if (combo_box->priv->tree_view && combo_box->priv->cell_view)
901         gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
902                                             &widget->style->base[GTK_WIDGET_STATE (widget)]);
903     }
904
905   gtk_widget_queue_draw (widget);
906 }
907
908 static void
909 gtk_combo_box_button_state_changed (GtkWidget    *widget,
910                                     GtkStateType  previous,
911                                     gpointer      data)
912 {
913   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
914
915   if (GTK_WIDGET_REALIZED (widget))
916     {
917       if (!combo_box->priv->tree_view && combo_box->priv->cell_view)
918         {
919           if ((GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE) !=
920               (GTK_WIDGET_STATE (combo_box->priv->cell_view) == GTK_STATE_INSENSITIVE))
921             gtk_widget_set_sensitive (combo_box->priv->cell_view, GTK_WIDGET_SENSITIVE (widget));
922           
923           gtk_widget_set_state (combo_box->priv->cell_view, 
924                                 GTK_WIDGET_STATE (widget));
925           
926         }
927     }
928
929   gtk_widget_queue_draw (widget);
930 }
931
932 static void
933 gtk_combo_box_check_appearance (GtkComboBox *combo_box)
934 {
935   gboolean appears_as_list;
936
937   /* if wrap_width > 0, then we are in grid-mode and forced to use
938    * unix style
939    */
940   if (combo_box->priv->wrap_width)
941     appears_as_list = FALSE;
942   else
943     gtk_widget_style_get (GTK_WIDGET (combo_box),
944                           "appears-as-list", &appears_as_list,
945                           NULL);
946
947   if (appears_as_list)
948     {
949       /* Destroy all the menu mode widgets, if they exist. */
950       if (GTK_IS_MENU (combo_box->priv->popup_widget))
951         gtk_combo_box_menu_destroy (combo_box);
952
953       /* Create the list mode widgets, if they don't already exist. */
954       if (!GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
955         gtk_combo_box_list_setup (combo_box);
956     }
957   else
958     {
959       /* Destroy all the list mode widgets, if they exist. */
960       if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
961         gtk_combo_box_list_destroy (combo_box);
962
963       /* Create the menu mode widgets, if they don't already exist. */
964       if (!GTK_IS_MENU (combo_box->priv->popup_widget))
965         gtk_combo_box_menu_setup (combo_box, TRUE);
966     }
967 }
968
969 static void
970 gtk_combo_box_style_set (GtkWidget *widget,
971                          GtkStyle  *previous)
972 {
973   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
974
975   gtk_combo_box_check_appearance (combo_box);
976
977   if (combo_box->priv->tree_view && combo_box->priv->cell_view)
978     gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
979                                         &widget->style->base[GTK_WIDGET_STATE (widget)]);
980 }
981
982 static void
983 gtk_combo_box_button_toggled (GtkWidget *widget,
984                               gpointer   data)
985 {
986   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
987
988   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
989     {
990       if (!combo_box->priv->popup_in_progress)
991         gtk_combo_box_popup (combo_box);
992     }
993   else
994     gtk_combo_box_popdown (combo_box);
995 }
996
997 static void
998 gtk_combo_box_add (GtkContainer *container,
999                    GtkWidget    *widget)
1000 {
1001   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1002
1003   if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
1004     {
1005       gtk_widget_unparent (combo_box->priv->cell_view);
1006       GTK_BIN (container)->child = NULL;
1007       gtk_widget_queue_resize (GTK_WIDGET (container));
1008     }
1009   
1010   gtk_widget_set_parent (widget, GTK_WIDGET (container));
1011   GTK_BIN (container)->child = widget;
1012
1013   if (combo_box->priv->cell_view &&
1014       widget != combo_box->priv->cell_view)
1015     {
1016       /* since the cell_view was unparented, it's gone now */
1017       combo_box->priv->cell_view = NULL;
1018
1019       if (!combo_box->priv->tree_view && combo_box->priv->separator)
1020         {
1021           gtk_container_remove (GTK_CONTAINER (combo_box->priv->separator->parent),
1022                                 combo_box->priv->separator);
1023           combo_box->priv->separator = NULL;
1024
1025           gtk_widget_queue_resize (GTK_WIDGET (container));
1026         }
1027       else if (combo_box->priv->cell_view_frame)
1028         {
1029           gtk_widget_unparent (combo_box->priv->cell_view_frame);
1030           combo_box->priv->cell_view_frame = NULL;
1031           combo_box->priv->box = NULL;
1032         }
1033     }
1034 }
1035
1036 static void
1037 gtk_combo_box_remove (GtkContainer *container,
1038                       GtkWidget    *widget)
1039 {
1040   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
1041   GtkTreePath *path;
1042   gboolean appears_as_list;
1043
1044   gtk_widget_unparent (widget);
1045   GTK_BIN (container)->child = NULL;
1046
1047   if (combo_box->priv->destroying)
1048     return;
1049
1050   gtk_widget_queue_resize (GTK_WIDGET (container));
1051
1052   if (!combo_box->priv->tree_view)
1053     appears_as_list = FALSE;
1054   else
1055     appears_as_list = TRUE;
1056   
1057   if (appears_as_list)
1058     gtk_combo_box_list_destroy (combo_box);
1059   else if (GTK_IS_MENU (combo_box->priv->popup_widget))
1060     {
1061       gtk_combo_box_menu_destroy (combo_box);
1062       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
1063       combo_box->priv->popup_widget = NULL;
1064     }
1065
1066   if (!combo_box->priv->cell_view)
1067     {
1068       combo_box->priv->cell_view = gtk_cell_view_new ();
1069       gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
1070       GTK_BIN (container)->child = combo_box->priv->cell_view;
1071       
1072       gtk_widget_show (combo_box->priv->cell_view);
1073       gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
1074                                combo_box->priv->model);
1075       gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (combo_box->priv->cell_view));
1076     }
1077
1078
1079   if (appears_as_list)
1080     gtk_combo_box_list_setup (combo_box); 
1081   else
1082     gtk_combo_box_menu_setup (combo_box, TRUE);
1083
1084   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1085     {
1086       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1087       gtk_combo_box_set_active_internal (combo_box, path);
1088       gtk_tree_path_free (path);
1089     }
1090   else
1091     gtk_combo_box_set_active_internal (combo_box, NULL);
1092 }
1093
1094 static ComboCellInfo *
1095 gtk_combo_box_get_cell_info (GtkComboBox     *combo_box,
1096                              GtkCellRenderer *cell)
1097 {
1098   GSList *i;
1099
1100   for (i = combo_box->priv->cells; i; i = i->next)
1101     {
1102       ComboCellInfo *info = (ComboCellInfo *)i->data;
1103
1104       if (info && info->cell == cell)
1105         return info;
1106     }
1107
1108   return NULL;
1109 }
1110
1111 static void
1112 gtk_combo_box_menu_show (GtkWidget *menu,
1113                          gpointer   user_data)
1114 {
1115   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1116
1117   gtk_combo_box_child_show (menu, user_data);
1118
1119   combo_box->priv->popup_in_progress = TRUE;
1120   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1121                                 TRUE);
1122   combo_box->priv->popup_in_progress = FALSE;
1123 }
1124
1125 static void
1126 gtk_combo_box_menu_hide (GtkWidget *menu,
1127                          gpointer   user_data)
1128 {
1129   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1130
1131   gtk_combo_box_child_hide(menu,user_data);
1132
1133   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1134                                 FALSE);
1135 }
1136
1137 static void
1138 gtk_combo_box_detacher (GtkWidget *widget,
1139                         GtkMenu   *menu)
1140 {
1141   GtkComboBox *combo_box;
1142
1143   g_return_if_fail (GTK_IS_COMBO_BOX (widget));
1144
1145   combo_box = GTK_COMBO_BOX (widget);
1146   g_return_if_fail (combo_box->priv->popup_widget == (GtkWidget*) menu);
1147
1148   g_signal_handlers_disconnect_by_func (menu->toplevel,
1149                                         gtk_combo_box_menu_show,
1150                                         combo_box);
1151   g_signal_handlers_disconnect_by_func (menu->toplevel,
1152                                         gtk_combo_box_menu_hide,
1153                                         combo_box);
1154   
1155   combo_box->priv->popup_widget = NULL;
1156 }
1157
1158 static void
1159 gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
1160                                 GtkWidget   *popup)
1161 {
1162   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1163     {
1164       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
1165       combo_box->priv->popup_widget = NULL;
1166     }
1167   else if (combo_box->priv->popup_widget)
1168     {
1169       gtk_container_remove (GTK_CONTAINER (combo_box->priv->popup_frame),
1170                             combo_box->priv->popup_widget);
1171       g_object_unref (combo_box->priv->popup_widget);
1172       combo_box->priv->popup_widget = NULL;
1173     }
1174
1175   if (GTK_IS_MENU (popup))
1176     {
1177       if (combo_box->priv->popup_window)
1178         {
1179           gtk_widget_destroy (combo_box->priv->popup_window);
1180           combo_box->priv->popup_window = NULL;
1181           combo_box->priv->popup_frame = NULL;
1182         }
1183
1184       combo_box->priv->popup_widget = popup;
1185
1186       /* 
1187        * Note that we connect to show/hide on the toplevel, not the
1188        * menu itself, since the menu is not shown/hidden when it is
1189        * popped up while torn-off.
1190        */
1191       g_signal_connect (GTK_MENU (popup)->toplevel, "show",
1192                         G_CALLBACK (gtk_combo_box_menu_show), combo_box);
1193       g_signal_connect (GTK_MENU (popup)->toplevel, "hide",
1194                         G_CALLBACK (gtk_combo_box_menu_hide), combo_box);
1195
1196       gtk_menu_attach_to_widget (GTK_MENU (popup),
1197                                  GTK_WIDGET (combo_box),
1198                                  gtk_combo_box_detacher);
1199     }
1200   else
1201     {
1202       if (!combo_box->priv->popup_window)
1203         {
1204           GtkWidget *toplevel;
1205           
1206           combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
1207
1208           g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"show",
1209                             G_CALLBACK (gtk_combo_box_child_show),
1210                             combo_box);
1211           g_signal_connect (GTK_WINDOW(combo_box->priv->popup_window),"hide",
1212                             G_CALLBACK (gtk_combo_box_child_hide),
1213                             combo_box);
1214           
1215           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1216           if (GTK_IS_WINDOW (toplevel))
1217             gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
1218                                          GTK_WINDOW (combo_box->priv->popup_window));
1219
1220           gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
1221           gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
1222                                  gtk_widget_get_screen (GTK_WIDGET (combo_box)));
1223
1224           combo_box->priv->popup_frame = gtk_frame_new (NULL);
1225           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
1226                                      GTK_SHADOW_ETCHED_IN);
1227           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
1228                              combo_box->priv->popup_frame);
1229
1230           gtk_widget_show (combo_box->priv->popup_frame);
1231
1232           combo_box->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1233           
1234           gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1235                                           GTK_POLICY_NEVER,
1236                                           GTK_POLICY_NEVER);
1237           gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1238                                                GTK_SHADOW_NONE);
1239
1240           gtk_widget_show (combo_box->priv->scrolled_window);
1241           
1242           gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_frame),
1243                              combo_box->priv->scrolled_window);
1244         }
1245
1246       gtk_container_add (GTK_CONTAINER (combo_box->priv->scrolled_window),
1247                          popup);
1248
1249       gtk_widget_show (popup);
1250       g_object_ref (popup);
1251       combo_box->priv->popup_widget = popup;
1252     }
1253 }
1254
1255 static void
1256 gtk_combo_box_menu_position_below (GtkMenu  *menu,
1257                                    gint     *x,
1258                                    gint     *y,
1259                                    gint     *push_in,
1260                                    gpointer  user_data)
1261 {
1262   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
1263   gint sx, sy;
1264   GtkWidget *child;
1265   GtkRequisition req;
1266   GdkScreen *screen;
1267   gint monitor_num;
1268   GdkRectangle monitor;
1269   
1270   /* FIXME: is using the size request here broken? */
1271    child = GTK_BIN (combo_box)->child;
1272    
1273    gdk_window_get_origin (child->window, &sx, &sy);
1274    
1275    if (GTK_WIDGET_NO_WINDOW (child))
1276       {
1277         sx += child->allocation.x;
1278         sy += child->allocation.y;
1279       }
1280
1281    gtk_widget_size_request (GTK_WIDGET (menu), &req);
1282
1283    if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
1284      *x = sx;
1285    else
1286      *x = sx + child->allocation.width - req.width;
1287    *y = sy;
1288
1289   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1290   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1291                                                   GTK_WIDGET (combo_box)->window);
1292   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1293   
1294   if (*x < monitor.x)
1295     *x = monitor.x;
1296   else if (*x + req.width > monitor.x + monitor.width)
1297     *x = monitor.x + monitor.width - req.width;
1298   
1299   if (monitor.y + monitor.height - *y - child->allocation.height >= req.height)
1300     *y += child->allocation.height;
1301   else if (*y - monitor.y >= req.height)
1302     *y -= req.height;
1303   else if (monitor.y + monitor.height - *y - child->allocation.height > *y - monitor.y) 
1304     *y += child->allocation.height;
1305   else
1306     *y -= req.height;
1307
1308    *push_in = FALSE;
1309 }
1310
1311 static void
1312 gtk_combo_box_menu_position_over (GtkMenu  *menu,
1313                                   gint     *x,
1314                                   gint     *y,
1315                                   gboolean *push_in,
1316                                   gpointer  user_data)
1317 {
1318   GtkComboBox *combo_box;
1319   GtkWidget *active;
1320   GtkWidget *child;
1321   GtkWidget *widget;
1322   GtkRequisition requisition;
1323   GList *children;
1324   gint screen_width;
1325   gint menu_xpos;
1326   gint menu_ypos;
1327   gint menu_width;
1328
1329   g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
1330   
1331   combo_box = GTK_COMBO_BOX (user_data);
1332   widget = GTK_WIDGET (combo_box);
1333
1334   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
1335   menu_width = requisition.width;
1336
1337   active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1338   gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
1339
1340   menu_xpos += widget->allocation.x;
1341   menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
1342
1343   if (active != NULL)
1344     {
1345       gtk_widget_get_child_requisition (active, &requisition);
1346       menu_ypos -= requisition.height / 2;
1347     }
1348
1349   children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
1350   while (children)
1351     {
1352       child = children->data;
1353
1354       if (active == child)
1355         break;
1356
1357       if (GTK_WIDGET_VISIBLE (child))
1358         {
1359           gtk_widget_get_child_requisition (child, &requisition);
1360           menu_ypos -= requisition.height;
1361         }
1362
1363       children = children->next;
1364     }
1365
1366   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1367     menu_xpos = menu_xpos + widget->allocation.width - menu_width;
1368
1369   /* Clamp the position on screen */
1370   screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
1371   
1372   if (menu_xpos < 0)
1373     menu_xpos = 0;
1374   else if ((menu_xpos + menu_width) > screen_width)
1375     menu_xpos -= ((menu_xpos + menu_width) - screen_width);
1376
1377   *x = menu_xpos;
1378   *y = menu_ypos;
1379
1380   *push_in = TRUE;
1381 }
1382
1383 static void
1384 gtk_combo_box_menu_position (GtkMenu  *menu,
1385                              gint     *x,
1386                              gint     *y,
1387                              gint     *push_in,
1388                              gpointer  user_data)
1389 {
1390   GtkComboBox *combo_box;
1391   GtkWidget *menu_item;
1392
1393   combo_box = GTK_COMBO_BOX (user_data);
1394
1395   if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)    
1396     gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
1397   else
1398     {
1399       /* FIXME handle nested menus better */
1400       menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
1401       if (menu_item)
1402         gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget), 
1403                                     menu_item);
1404
1405       gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
1406     }
1407
1408 }
1409
1410 static void
1411 gtk_combo_box_list_position (GtkComboBox *combo_box, 
1412                              gint        *x, 
1413                              gint        *y, 
1414                              gint        *width,
1415                              gint        *height)
1416 {
1417   GtkWidget *sample;
1418   GdkScreen *screen;
1419   gint monitor_num;
1420   GdkRectangle monitor;
1421   GtkRequisition popup_req;
1422   GtkPolicyType hpolicy, vpolicy;
1423   
1424   sample = GTK_BIN (combo_box)->child;
1425
1426   gdk_window_get_origin (sample->window, x, y);
1427
1428   if (GTK_WIDGET_NO_WINDOW (sample))
1429     {
1430       *x += sample->allocation.x;
1431       *y += sample->allocation.y;
1432     }
1433   
1434   *width = sample->allocation.width;
1435   
1436   if (combo_box->priv->cell_view_frame && combo_box->priv->has_frame)
1437     {
1438        *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1439              GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
1440        *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1441             GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1442     }
1443
1444   hpolicy = vpolicy = GTK_POLICY_NEVER;
1445   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1446                                   hpolicy, vpolicy);
1447   gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1448
1449   if (popup_req.width > *width)
1450     {
1451       hpolicy = GTK_POLICY_ALWAYS;
1452       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1453                                       hpolicy, vpolicy);
1454       gtk_widget_size_request (combo_box->priv->popup_frame, &popup_req);
1455     }
1456
1457   *height = popup_req.height;
1458
1459   screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
1460   monitor_num = gdk_screen_get_monitor_at_window (screen, 
1461                                                   GTK_WIDGET (combo_box)->window);
1462   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1463
1464   if (*x < monitor.x)
1465     *x = monitor.x;
1466   else if (*x + *width > monitor.x + monitor.width)
1467     *x = monitor.x + monitor.width - *width;
1468   
1469   if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
1470     *y += sample->allocation.height;
1471   else if (*y - *height >= monitor.y)
1472     *y -= *height;
1473   else if (monitor.y + monitor.height - (*y + sample->allocation.height) > *y - monitor.y)
1474     {
1475       *y += sample->allocation.height;
1476       *height = monitor.y + monitor.height - *y;
1477     }
1478   else 
1479     {
1480       *height = *y - monitor.y;
1481       *y = monitor.y;
1482     }
1483
1484   if (popup_req.height > *height)
1485     {
1486       vpolicy = GTK_POLICY_ALWAYS;
1487       
1488       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window),
1489                                       hpolicy, vpolicy);
1490     }
1491
1492
1493 static gboolean
1494 cell_view_is_sensitive (GtkCellView *cell_view)
1495 {
1496   GList *cells, *list;
1497   gboolean sensitive;
1498   
1499   cells = gtk_cell_view_get_cell_renderers (cell_view);
1500
1501   sensitive = FALSE;
1502   list = cells;
1503   while (list)
1504     {
1505       g_object_get (list->data, "sensitive", &sensitive, NULL);
1506       
1507       if (sensitive)
1508         break;
1509
1510       list = list->next;
1511     }
1512   g_list_free (cells);
1513
1514   return sensitive;
1515 }
1516
1517 static gboolean
1518 tree_column_row_is_sensitive (GtkComboBox *combo_box,
1519                               GtkTreeIter *iter)
1520 {
1521   GList *cells, *list;
1522   gboolean sensitive;
1523
1524   if (!combo_box->priv->column)
1525     return TRUE;
1526
1527   if (combo_box->priv->row_separator_func)
1528     {
1529       if ((*combo_box->priv->row_separator_func) (combo_box->priv->model, iter,
1530                                                   combo_box->priv->row_separator_data))
1531         return FALSE;
1532     }
1533
1534   gtk_tree_view_column_cell_set_cell_data (combo_box->priv->column,
1535                                            combo_box->priv->model,
1536                                            iter, FALSE, FALSE);
1537
1538   cells = gtk_tree_view_column_get_cell_renderers (combo_box->priv->column);
1539
1540   sensitive = FALSE;
1541   list = cells;
1542   while (list)
1543     {
1544       g_object_get (list->data, "sensitive", &sensitive, NULL);
1545       
1546       if (sensitive)
1547         break;
1548
1549       list = list->next;
1550     }
1551   g_list_free (cells);
1552
1553   return sensitive;
1554 }
1555
1556 static void
1557 update_menu_sensitivity (GtkComboBox *combo_box,
1558                          GtkWidget   *menu)
1559 {
1560   GList *children, *child;
1561   GtkWidget *item, *submenu, *separator;
1562   GtkWidget *cell_view;
1563   gboolean sensitive;
1564
1565   if (!combo_box->priv->model)
1566     return;
1567
1568   children = gtk_container_get_children (GTK_CONTAINER (menu));
1569
1570   for (child = children; child; child = child->next)
1571     {
1572       item = GTK_WIDGET (child->data);
1573       cell_view = GTK_BIN (item)->child;
1574
1575       if (!GTK_IS_CELL_VIEW (cell_view))
1576         continue;
1577       
1578       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
1579       if (submenu != NULL)
1580         {
1581           gtk_widget_set_sensitive (item, TRUE);
1582           update_menu_sensitivity (combo_box, submenu);
1583         }
1584       else
1585         {
1586           sensitive = cell_view_is_sensitive (GTK_CELL_VIEW (cell_view));
1587
1588           if (menu != combo_box->priv->popup_widget && child == children)
1589             {
1590               separator = GTK_WIDGET (child->next->data);
1591               g_object_set (item, "visible", sensitive, NULL);
1592               g_object_set (separator, "visible", sensitive, NULL);
1593             }
1594           else
1595             gtk_widget_set_sensitive (item, sensitive);
1596         }
1597     }
1598
1599   g_list_free (children);
1600 }
1601
1602 static void 
1603 gtk_combo_box_menu_popup (GtkComboBox *combo_box,
1604                           guint        button, 
1605                           guint32      activate_time)
1606 {
1607   GtkTreePath *path;
1608   gint active_item;
1609   GtkRequisition requisition;
1610   gint width;
1611   
1612   update_menu_sensitivity (combo_box, combo_box->priv->popup_widget);
1613
1614   active_item = -1;
1615   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1616     {
1617       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1618       active_item = gtk_tree_path_get_indices (path)[0];
1619       gtk_tree_path_free (path);
1620       
1621       if (combo_box->priv->add_tearoffs)
1622         active_item++;
1623     }
1624
1625   /* FIXME handle nested menus better */
1626   gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), active_item);
1627   
1628   if (combo_box->priv->wrap_width == 0)
1629     {
1630       width = GTK_WIDGET (combo_box)->allocation.width;
1631       gtk_widget_set_size_request (combo_box->priv->popup_widget, -1, -1);
1632       gtk_widget_size_request (combo_box->priv->popup_widget, &requisition);
1633       
1634       gtk_widget_set_size_request (combo_box->priv->popup_widget,
1635                                    MAX (width, requisition.width), -1);
1636     }
1637   
1638   gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
1639                   NULL, NULL,
1640                   gtk_combo_box_menu_position, combo_box,
1641                   button, activate_time);
1642 }
1643
1644 static gboolean
1645 popup_grab_on_window (GdkWindow *window,
1646                       guint32    activate_time,
1647                       gboolean   grab_keyboard)
1648 {
1649   if ((gdk_pointer_grab (window, TRUE,
1650                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1651                          GDK_POINTER_MOTION_MASK,
1652                          NULL, NULL, activate_time) == 0))
1653     {
1654       if (!grab_keyboard ||
1655           gdk_keyboard_grab (window, TRUE,
1656                              activate_time) == 0)
1657         return TRUE;
1658       else
1659         {
1660           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1661                                       activate_time);
1662           return FALSE;
1663         }
1664     }
1665
1666   return FALSE;
1667 }
1668
1669 /**
1670  * gtk_combo_box_popup:
1671  * @combo_box: a #GtkComboBox
1672  * 
1673  * Pops up the menu or dropdown list of @combo_box. 
1674  *
1675  * This function is mostly intended for use by accessibility technologies;
1676  * applications should have little use for it.
1677  *
1678  * Since: 2.4
1679  **/
1680 void
1681 gtk_combo_box_popup (GtkComboBox *combo_box)
1682 {
1683   gint x, y, width, height;
1684   GtkTreePath *path, *ppath;
1685   GtkWidget *toplevel;
1686
1687   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1688
1689   if (!GTK_WIDGET_REALIZED (combo_box))
1690     return;
1691
1692   if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
1693     return;
1694
1695   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1696     {
1697       gtk_combo_box_menu_popup (combo_box, 0, 0);
1698       return;
1699     }
1700
1701   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo_box));
1702   if (GTK_IS_WINDOW (toplevel))
1703     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
1704                                  GTK_WINDOW (combo_box->priv->popup_window));
1705
1706   gtk_widget_show_all (combo_box->priv->popup_frame);
1707   gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
1708   
1709   gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);  
1710   gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
1711
1712   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
1713     {
1714       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
1715       ppath = gtk_tree_path_copy (path);
1716       if (gtk_tree_path_up (ppath))
1717         gtk_tree_view_expand_to_path (GTK_TREE_VIEW (combo_box->priv->tree_view),
1718                                       ppath);
1719       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
1720                                 path, NULL, FALSE);
1721       gtk_tree_path_free (path);
1722       gtk_tree_path_free (ppath);
1723     }
1724   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
1725                                   TRUE);
1726   
1727   /* popup */
1728   gtk_widget_show (combo_box->priv->popup_window);
1729
1730   gtk_widget_grab_focus (combo_box->priv->popup_window);
1731   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1732                                 TRUE);
1733
1734   if (!GTK_WIDGET_HAS_FOCUS (combo_box->priv->tree_view))
1735     gtk_widget_grab_focus (combo_box->priv->tree_view);
1736
1737   if (!popup_grab_on_window (combo_box->priv->popup_window->window,
1738                              GDK_CURRENT_TIME, TRUE))
1739     {
1740       gtk_widget_hide (combo_box->priv->popup_window);
1741       return;
1742     }
1743
1744   gtk_grab_add (combo_box->priv->popup_window);
1745 }
1746
1747 /**
1748  * gtk_combo_box_popdown:
1749  * @combo_box: a #GtkComboBox
1750  * 
1751  * Hides the menu or dropdown list of @combo_box.
1752  *
1753  * This function is mostly intended for use by accessibility technologies;
1754  * applications should have little use for it.
1755  *
1756  * Since: 2.4
1757  **/
1758 void
1759 gtk_combo_box_popdown (GtkComboBox *combo_box)
1760 {
1761   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
1762
1763   if (GTK_IS_MENU (combo_box->priv->popup_widget))
1764     {
1765       gtk_menu_popdown (GTK_MENU (combo_box->priv->popup_widget));
1766       return;
1767     }
1768
1769   if (!GTK_WIDGET_REALIZED (GTK_WIDGET (combo_box)))
1770     return;
1771
1772   gtk_grab_remove (combo_box->priv->popup_window);
1773   gtk_widget_hide_all (combo_box->priv->popup_window);
1774   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
1775                                 FALSE);
1776 }
1777
1778 static gint
1779 gtk_combo_box_calc_requested_width (GtkComboBox *combo_box,
1780                                     GtkTreePath *path)
1781 {
1782   gint padding;
1783   GtkRequisition req;
1784
1785   if (combo_box->priv->cell_view)
1786     gtk_widget_style_get (combo_box->priv->cell_view,
1787                           "focus-line-width", &padding,
1788                           NULL);
1789   else
1790     padding = 0;
1791
1792   /* add some pixels for good measure */
1793   padding += BONUS_PADDING;
1794
1795   if (combo_box->priv->cell_view)
1796     gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
1797                                    path, &req);
1798   else
1799     req.width = 0;
1800
1801   return req.width + padding;
1802 }
1803
1804 static void
1805 gtk_combo_box_remeasure (GtkComboBox *combo_box)
1806 {
1807   GtkTreeIter iter;
1808   GtkTreePath *path;
1809
1810   if (!combo_box->priv->model ||
1811       !gtk_tree_model_get_iter_first (combo_box->priv->model, &iter))
1812     return;
1813
1814   combo_box->priv->width = 0;
1815
1816   path = gtk_tree_path_new_from_indices (0, -1);
1817
1818   do
1819     {
1820       GtkRequisition req;
1821
1822       if (combo_box->priv->cell_view)
1823         gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
1824                                        path, &req);
1825       else
1826         req.width = 0;
1827
1828       combo_box->priv->width = MAX (combo_box->priv->width, req.width);
1829
1830       gtk_tree_path_next (path);
1831     }
1832   while (gtk_tree_model_iter_next (combo_box->priv->model, &iter));
1833
1834   gtk_tree_path_free (path);
1835 }
1836
1837 static void
1838 gtk_combo_box_size_request (GtkWidget      *widget,
1839                             GtkRequisition *requisition)
1840 {
1841   gint width, height;
1842   gint focus_width, focus_pad;
1843   GtkRequisition bin_req;
1844
1845   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1846  
1847   /* common */
1848   gtk_widget_size_request (GTK_BIN (widget)->child, &bin_req);
1849   gtk_combo_box_remeasure (combo_box);
1850   bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1851
1852   gtk_widget_style_get (GTK_WIDGET (widget),
1853                         "focus-line-width", &focus_width,
1854                         "focus-padding", &focus_pad,
1855                         NULL);
1856
1857   if (!combo_box->priv->tree_view)
1858     {
1859       /* menu mode */
1860
1861       if (combo_box->priv->cell_view)
1862         {
1863           GtkRequisition button_req, sep_req, arrow_req;
1864           gint border_width, xthickness, ythickness;
1865
1866           gtk_widget_size_request (combo_box->priv->button, &button_req);
1867           border_width = GTK_CONTAINER (combo_box)->border_width;
1868           xthickness = combo_box->priv->button->style->xthickness;
1869           ythickness = combo_box->priv->button->style->ythickness;
1870
1871           bin_req.width = MAX (bin_req.width, combo_box->priv->width);
1872
1873           gtk_widget_size_request (combo_box->priv->separator, &sep_req);
1874           gtk_widget_size_request (combo_box->priv->arrow, &arrow_req);
1875
1876           height = MAX (sep_req.height, arrow_req.height);
1877           height = MAX (height, bin_req.height);
1878
1879           width = bin_req.width + sep_req.width + arrow_req.width;
1880
1881           height += 2*(border_width + ythickness + focus_width + focus_pad);
1882           width  += 2*(border_width + xthickness + focus_width + focus_pad);
1883
1884           requisition->width = width;
1885           requisition->height = height;
1886         }
1887       else
1888         {
1889           GtkRequisition but_req;
1890
1891           gtk_widget_size_request (combo_box->priv->button, &but_req);
1892
1893           requisition->width = bin_req.width + but_req.width;
1894           requisition->height = MAX (bin_req.height, but_req.height);
1895         }
1896     }
1897   else
1898     {
1899       /* list mode */
1900       GtkRequisition button_req, frame_req;
1901
1902       /* sample + frame */
1903       *requisition = bin_req;
1904
1905       requisition->width += 2 * focus_width;
1906       
1907       if (combo_box->priv->cell_view_frame)
1908         {
1909           gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
1910           if (combo_box->priv->has_frame)
1911             {
1912               requisition->width += 2 *
1913                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1914                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
1915               requisition->height += 2 *
1916                 (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
1917                  GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
1918             }
1919         }
1920
1921       /* the button */
1922       gtk_widget_size_request (combo_box->priv->button, &button_req);
1923
1924       requisition->height = MAX (requisition->height, button_req.height);
1925       requisition->width += button_req.width;
1926     }
1927 }
1928
1929 static void
1930 gtk_combo_box_size_allocate (GtkWidget     *widget,
1931                              GtkAllocation *allocation)
1932 {
1933   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
1934   gint focus_width, focus_pad;
1935   GtkAllocation child;
1936   GtkRequisition req;
1937   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1938
1939   widget->allocation = *allocation;
1940
1941   gtk_widget_style_get (GTK_WIDGET (widget),
1942                         "focus-line-width", &focus_width,
1943                         "focus-padding", &focus_pad,
1944                         NULL);
1945
1946   if (!combo_box->priv->tree_view)
1947     {
1948       if (combo_box->priv->cell_view)
1949         {
1950           gint border_width, xthickness, ythickness;
1951           gint width;
1952
1953           /* menu mode */
1954           gtk_widget_size_allocate (combo_box->priv->button, allocation);
1955
1956           /* set some things ready */
1957           border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
1958           xthickness = combo_box->priv->button->style->xthickness;
1959           ythickness = combo_box->priv->button->style->ythickness;
1960
1961           child.x = allocation->x;
1962           child.y = allocation->y;
1963           width = allocation->width;
1964           child.height = allocation->height;
1965
1966           if (!combo_box->priv->is_cell_renderer)
1967             {
1968               child.x += border_width + xthickness + focus_width + focus_pad;
1969               child.y += border_width + ythickness + focus_width + focus_pad;
1970               width -= 2 * (child.x - allocation->x);
1971               child.height -= 2 * (child.y - allocation->y);
1972             }
1973
1974
1975           /* handle the children */
1976           gtk_widget_size_request (combo_box->priv->arrow, &req);
1977           child.width = req.width;
1978           if (!is_rtl)
1979             child.x += width - req.width;
1980           child.width = MAX (1, child.width);
1981           child.height = MAX (1, child.height);
1982           gtk_widget_size_allocate (combo_box->priv->arrow, &child);
1983           if (is_rtl)
1984             child.x += req.width;
1985           gtk_widget_size_request (combo_box->priv->separator, &req);
1986           child.width = req.width;
1987           if (!is_rtl)
1988             child.x -= req.width;
1989           child.width = MAX (1, child.width);
1990           child.height = MAX (1, child.height);
1991           gtk_widget_size_allocate (combo_box->priv->separator, &child);
1992
1993           if (is_rtl)
1994             {
1995               child.x += req.width;
1996               child.width = allocation->x + allocation->width 
1997                 - (border_width + xthickness + focus_width + focus_pad) 
1998                 - child.x;
1999             }
2000           else 
2001             {
2002               child.width = child.x;
2003               child.x = allocation->x 
2004                 + border_width + xthickness + focus_width + focus_pad;
2005               child.width -= child.x;
2006             }
2007
2008           child.width = MAX (1, child.width);
2009           child.height = MAX (1, child.height);
2010           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2011         }
2012       else
2013         {
2014           gtk_widget_size_request (combo_box->priv->button, &req);
2015           if (is_rtl)
2016             child.x = allocation->x;
2017           else
2018             child.x = allocation->x + allocation->width - req.width;
2019           child.y = allocation->y;
2020           child.width = req.width;
2021           child.height = allocation->height;
2022           child.width = MAX (1, child.width);
2023           child.height = MAX (1, child.height);
2024           gtk_widget_size_allocate (combo_box->priv->button, &child);
2025
2026           if (is_rtl)
2027             child.x = allocation->x + req.width;
2028           else
2029             child.x = allocation->x;
2030           child.y = allocation->y;
2031           child.width = allocation->width - req.width;
2032           child.width = MAX (1, child.width);
2033           child.height = MAX (1, child.height);
2034           gtk_widget_size_allocate (GTK_BIN (widget)->child, &child);
2035         }
2036     }
2037   else
2038     {
2039       /* list mode */
2040
2041       /* button */
2042       gtk_widget_size_request (combo_box->priv->button, &req);
2043       if (is_rtl)
2044         child.x = allocation->x;
2045       else
2046         child.x = allocation->x + allocation->width - req.width;
2047       child.y = allocation->y;
2048       child.width = req.width;
2049       child.height = allocation->height;
2050       child.width = MAX (1, child.width);
2051       child.height = MAX (1, child.height);
2052       gtk_widget_size_allocate (combo_box->priv->button, &child);
2053
2054       /* frame */
2055       if (is_rtl)
2056         child.x = allocation->x + req.width;
2057       else
2058         child.x = allocation->x;
2059       child.y = allocation->y;
2060       child.width = allocation->width - req.width;
2061       child.height = allocation->height;
2062
2063       if (combo_box->priv->cell_view_frame)
2064         {
2065           child.width = MAX (1, child.width);
2066           child.height = MAX (1, child.height);
2067           gtk_widget_size_allocate (combo_box->priv->cell_view_frame, &child);
2068
2069           /* the sample */
2070           if (combo_box->priv->has_frame)
2071             {
2072               child.x +=
2073                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2074                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
2075               child.y +=
2076                 GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2077                 GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness;
2078               child.width -= 2 * (
2079                                   GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2080                                   GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
2081               child.height -= 2 * (
2082                                    GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
2083                                    GTK_WIDGET (combo_box->priv->cell_view_frame)->style->ythickness);
2084             }
2085         }
2086       
2087       child.width = MAX (1, child.width);
2088       child.height = MAX (1, child.height);
2089       gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2090     }
2091 }
2092
2093 static void
2094 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2095 {
2096   if (combo_box->priv->model)
2097     {
2098       g_signal_handler_disconnect (combo_box->priv->model,
2099                                    combo_box->priv->inserted_id);
2100       g_signal_handler_disconnect (combo_box->priv->model,
2101                                    combo_box->priv->deleted_id);
2102       g_signal_handler_disconnect (combo_box->priv->model,
2103                                    combo_box->priv->reordered_id);
2104       g_signal_handler_disconnect (combo_box->priv->model,
2105                                    combo_box->priv->changed_id);
2106     }
2107
2108   /* menu mode */
2109   if (!combo_box->priv->tree_view)
2110     {
2111       if (combo_box->priv->popup_widget)
2112         gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
2113                                (GtkCallback)gtk_widget_destroy, NULL);
2114     }
2115
2116   if (combo_box->priv->model)
2117     {
2118       g_object_unref (combo_box->priv->model);
2119       combo_box->priv->model = NULL;
2120     }
2121
2122   if (combo_box->priv->active_row)
2123     {
2124       gtk_tree_row_reference_free (combo_box->priv->active_row);
2125       combo_box->priv->active_row = NULL;
2126     }
2127
2128   if (combo_box->priv->cell_view)
2129     gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2130 }
2131
2132 static void
2133 gtk_combo_box_forall (GtkContainer *container,
2134                       gboolean      include_internals,
2135                       GtkCallback   callback,
2136                       gpointer      callback_data)
2137 {
2138   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2139
2140   if (include_internals)
2141     {
2142       if (combo_box->priv->button)
2143         (* callback) (combo_box->priv->button, callback_data);
2144       if (combo_box->priv->cell_view_frame)
2145         (* callback) (combo_box->priv->cell_view_frame, callback_data);
2146     }
2147
2148   if (GTK_BIN (container)->child)
2149     (* callback) (GTK_BIN (container)->child, callback_data);
2150 }
2151
2152 static void 
2153 gtk_combo_box_child_show (GtkWidget *widget,
2154                           gpointer   user_data) 
2155 {
2156   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2157   g_signal_emit (combo_box, combo_box_signals[POPUP_SHOW], 0);
2158 }
2159
2160 static void 
2161 gtk_combo_box_child_hide (GtkWidget *widget,
2162                           gpointer   user_data)
2163 {
2164   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);  
2165   g_signal_emit (combo_box, combo_box_signals[POPUP_HIDE], 0);
2166 }
2167
2168 static gboolean
2169 gtk_combo_box_expose_event (GtkWidget      *widget,
2170                             GdkEventExpose *event)
2171 {
2172   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2173
2174   if (!combo_box->priv->tree_view)
2175     {
2176       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2177                                       combo_box->priv->button, event);
2178     }
2179   else
2180     {
2181       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2182                                       combo_box->priv->button, event);
2183
2184       if (combo_box->priv->cell_view_frame)
2185         gtk_container_propagate_expose (GTK_CONTAINER (widget),
2186                                         combo_box->priv->cell_view_frame, event);
2187     }
2188
2189   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2190                                   GTK_BIN (widget)->child, event);
2191
2192   return FALSE;
2193 }
2194
2195 typedef struct {
2196   GtkComboBox *combo;
2197   GtkTreePath *path;
2198   GtkTreeIter iter;
2199   gboolean found;
2200   gboolean set;
2201   gboolean visible;
2202 } SearchData;
2203
2204 static gboolean
2205 path_visible (GtkTreeView *view,
2206               GtkTreePath *path)
2207 {
2208   GtkRBTree *tree;
2209   GtkRBNode *node;
2210   
2211   /* Note that we rely on the fact that collapsed rows don't have nodes 
2212    */
2213   return _gtk_tree_view_find_node (view, path, &tree, &node);
2214 }
2215
2216 static gboolean
2217 tree_next_func (GtkTreeModel *model,
2218                 GtkTreePath  *path,
2219                 GtkTreeIter  *iter,
2220                 gpointer      data)
2221 {
2222   SearchData *search_data = (SearchData *)data;
2223
2224   if (search_data->found) 
2225     {
2226       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2227         return FALSE;
2228       
2229       if (search_data->visible &&
2230           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2231         return FALSE;
2232
2233       search_data->set = TRUE;
2234       search_data->iter = *iter;
2235
2236       return TRUE;
2237     }
2238  
2239   if (gtk_tree_path_compare (path, search_data->path) == 0)
2240     search_data->found = TRUE;
2241   
2242   return FALSE;
2243 }
2244
2245 static gboolean
2246 tree_next (GtkComboBox  *combo,
2247            GtkTreeModel *model,
2248            GtkTreeIter  *iter,
2249            GtkTreeIter  *next,
2250            gboolean      visible)
2251 {
2252   SearchData search_data;
2253
2254   search_data.combo = combo;
2255   search_data.path = gtk_tree_model_get_path (model, iter);
2256   search_data.visible = visible;
2257   search_data.found = FALSE;
2258   search_data.set = FALSE;
2259
2260   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2261   
2262   *next = search_data.iter;
2263
2264   gtk_tree_path_free (search_data.path);
2265
2266   return search_data.set;
2267 }
2268
2269 static gboolean
2270 tree_prev_func (GtkTreeModel *model,
2271                 GtkTreePath  *path,
2272                 GtkTreeIter  *iter,
2273                 gpointer      data)
2274 {
2275   SearchData *search_data = (SearchData *)data;
2276
2277   if (gtk_tree_path_compare (path, search_data->path) == 0)
2278     {
2279       search_data->found = TRUE;
2280       return TRUE;
2281     }
2282   
2283   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2284     return FALSE;
2285       
2286   if (search_data->visible &&
2287       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2288     return FALSE; 
2289   
2290   search_data->set = TRUE;
2291   search_data->iter = *iter;
2292   
2293   return FALSE; 
2294 }
2295
2296 static gboolean
2297 tree_prev (GtkComboBox  *combo,
2298            GtkTreeModel *model,
2299            GtkTreeIter  *iter,
2300            GtkTreeIter  *prev,
2301            gboolean      visible)
2302 {
2303   SearchData search_data;
2304
2305   search_data.combo = combo;
2306   search_data.path = gtk_tree_model_get_path (model, iter);
2307   search_data.visible = visible;
2308   search_data.found = FALSE;
2309   search_data.set = FALSE;
2310
2311   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2312   
2313   *prev = search_data.iter;
2314
2315   gtk_tree_path_free (search_data.path);
2316
2317   return search_data.set;
2318 }
2319
2320 static gboolean
2321 tree_last_func (GtkTreeModel *model,
2322                 GtkTreePath  *path,
2323                 GtkTreeIter  *iter,
2324                 gpointer      data)
2325 {
2326   SearchData *search_data = (SearchData *)data;
2327
2328   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2329     return FALSE;
2330       
2331   /* Note that we rely on the fact that collapsed rows don't have nodes 
2332    */
2333   if (search_data->visible &&
2334       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2335     return FALSE; 
2336   
2337   search_data->set = TRUE;
2338   search_data->iter = *iter;
2339   
2340   return FALSE; 
2341 }
2342
2343 static gboolean
2344 tree_last (GtkComboBox  *combo,
2345            GtkTreeModel *model,
2346            GtkTreeIter  *last,
2347            gboolean      visible)
2348 {
2349   SearchData search_data;
2350
2351   search_data.combo = combo;
2352   search_data.visible = visible;
2353   search_data.set = FALSE;
2354
2355   gtk_tree_model_foreach (model, tree_last_func, &search_data);
2356   
2357   *last = search_data.iter;
2358
2359   return search_data.set;  
2360 }
2361
2362
2363 static gboolean
2364 tree_first_func (GtkTreeModel *model,
2365                  GtkTreePath  *path,
2366                  GtkTreeIter  *iter,
2367                  gpointer      data)
2368 {
2369   SearchData *search_data = (SearchData *)data;
2370
2371   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2372     return FALSE;
2373   
2374   if (search_data->visible &&
2375       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2376     return FALSE;
2377   
2378   search_data->set = TRUE;
2379   search_data->iter = *iter;
2380   
2381   return TRUE;
2382 }
2383
2384 static gboolean
2385 tree_first (GtkComboBox  *combo,
2386             GtkTreeModel *model,
2387             GtkTreeIter  *first,
2388             gboolean      visible)
2389 {
2390   SearchData search_data;
2391   
2392   search_data.combo = combo;
2393   search_data.visible = visible;
2394   search_data.set = FALSE;
2395
2396   gtk_tree_model_foreach (model, tree_first_func, &search_data);
2397   
2398   *first = search_data.iter;
2399
2400   return search_data.set;  
2401 }
2402
2403 static gboolean
2404 gtk_combo_box_scroll_event (GtkWidget          *widget,
2405                             GdkEventScroll     *event)
2406 {
2407   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2408   gboolean found;
2409   GtkTreeIter iter;
2410   GtkTreeIter new_iter;
2411
2412   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2413     return TRUE;
2414   
2415   if (event->direction == GDK_SCROLL_UP)
2416     found = tree_prev (combo_box, combo_box->priv->model, 
2417                        &iter, &new_iter, FALSE);
2418   else
2419     found = tree_next (combo_box, combo_box->priv->model, 
2420                        &iter, &new_iter, FALSE);
2421   
2422   if (found)
2423     gtk_combo_box_set_active_iter (combo_box, &new_iter);
2424
2425   return TRUE;
2426 }
2427
2428 /*
2429  * menu style
2430  */
2431
2432 static void
2433 gtk_combo_box_sync_cells (GtkComboBox   *combo_box,
2434                           GtkCellLayout *cell_layout)
2435 {
2436   GSList *k;
2437
2438   for (k = combo_box->priv->cells; k; k = k->next)
2439     {
2440       GSList *j;
2441       ComboCellInfo *info = (ComboCellInfo *)k->data;
2442
2443       if (info->pack == GTK_PACK_START)
2444         gtk_cell_layout_pack_start (cell_layout,
2445                                     info->cell, info->expand);
2446       else if (info->pack == GTK_PACK_END)
2447         gtk_cell_layout_pack_end (cell_layout,
2448                                   info->cell, info->expand);
2449
2450       gtk_cell_layout_set_cell_data_func (cell_layout,
2451                                           info->cell,
2452                                           combo_cell_data_func, info, NULL);
2453
2454       for (j = info->attributes; j; j = j->next->next)
2455         {
2456           gtk_cell_layout_add_attribute (cell_layout,
2457                                          info->cell,
2458                                          j->data,
2459                                          GPOINTER_TO_INT (j->next->data));
2460         }
2461     }
2462 }
2463
2464 static void
2465 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2466                           gboolean     add_children)
2467 {
2468   GtkWidget *menu;
2469
2470   if (combo_box->priv->cell_view)
2471     {
2472       combo_box->priv->button = gtk_toggle_button_new ();
2473       gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2474                                      combo_box->priv->focus_on_click);
2475
2476       g_signal_connect (combo_box->priv->button, "toggled",
2477                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2478       g_signal_connect_after (combo_box->priv->button, 
2479                               "key_press_event",
2480                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
2481       gtk_widget_set_parent (combo_box->priv->button,
2482                              GTK_BIN (combo_box)->child->parent);
2483
2484       combo_box->priv->box = gtk_hbox_new (FALSE, 0);
2485       gtk_container_add (GTK_CONTAINER (combo_box->priv->button), 
2486                          combo_box->priv->box);
2487
2488       combo_box->priv->separator = gtk_vseparator_new ();
2489       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
2490                          combo_box->priv->separator);
2491
2492       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2493       gtk_container_add (GTK_CONTAINER (combo_box->priv->box), 
2494                          combo_box->priv->arrow);
2495
2496       gtk_widget_show_all (combo_box->priv->button);
2497     }
2498   else
2499     {
2500       combo_box->priv->button = gtk_toggle_button_new ();
2501       gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
2502                                      combo_box->priv->focus_on_click);
2503
2504       g_signal_connect (combo_box->priv->button, "toggled",
2505                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2506       g_signal_connect_after (combo_box, "key_press_event",
2507                               G_CALLBACK (gtk_combo_box_key_press), combo_box);
2508       gtk_widget_set_parent (combo_box->priv->button,
2509                              GTK_BIN (combo_box)->child->parent);
2510
2511       combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2512       gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
2513                          combo_box->priv->arrow);
2514       gtk_widget_show_all (combo_box->priv->button);
2515     }
2516
2517   g_signal_connect (combo_box->priv->button, "button_press_event",
2518                     G_CALLBACK (gtk_combo_box_menu_button_press),
2519                     combo_box);
2520   g_signal_connect (combo_box->priv->button, "state_changed",
2521                     G_CALLBACK (gtk_combo_box_button_state_changed), 
2522                     combo_box);
2523
2524   /* create our funky menu */
2525   menu = gtk_menu_new ();
2526   g_signal_connect (menu, "key_press_event",
2527                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2528   gtk_combo_box_set_popup_widget (combo_box, menu);
2529
2530   /* add items */
2531   if (add_children)
2532     gtk_combo_box_menu_fill (combo_box);
2533
2534   /* the column is needed in tree_column_row_is_sensitive() */
2535   combo_box->priv->column = gtk_tree_view_column_new ();
2536   g_object_ref_sink (combo_box->priv->column);
2537   gtk_combo_box_sync_cells (combo_box, 
2538                             GTK_CELL_LAYOUT (combo_box->priv->column));
2539
2540   gtk_combo_box_update_title (combo_box);
2541 }
2542
2543 static void
2544 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2545 {
2546   GtkWidget *menu;
2547
2548   if (!combo_box->priv->model)
2549     return;
2550
2551   menu = combo_box->priv->popup_widget;
2552
2553   if (combo_box->priv->add_tearoffs)
2554     {
2555       GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2556
2557       gtk_widget_show (tearoff);
2558       
2559       if (combo_box->priv->wrap_width)
2560         gtk_menu_attach (GTK_MENU (menu), tearoff, 
2561                          0, combo_box->priv->wrap_width, 0, 1);
2562       else
2563         gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2564     }
2565   
2566   gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2567 }
2568
2569 static GtkWidget *
2570 gtk_cell_view_menu_item_new (GtkComboBox  *combo_box,
2571                              GtkTreeModel *model,
2572                              GtkTreeIter  *iter)
2573 {
2574   GtkWidget *cell_view; 
2575   GtkWidget *item;
2576   GtkTreePath *path;
2577   GtkRequisition req;
2578
2579   cell_view = gtk_cell_view_new ();
2580   gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);     
2581   path = gtk_tree_model_get_path (model, iter);
2582   gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2583   gtk_tree_path_free (path);
2584
2585   gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2586   gtk_widget_size_request (cell_view, &req);
2587   gtk_widget_show (cell_view);
2588
2589   item = gtk_menu_item_new ();
2590   gtk_container_add (GTK_CONTAINER (item), cell_view);
2591   
2592   return item;
2593 }
2594  
2595 static void
2596 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2597                                GtkWidget   *menu,
2598                                GtkTreeIter *parent)
2599 {
2600   GtkTreeModel *model = combo_box->priv->model;
2601   GtkWidget *item, *submenu, *subitem, *separator;
2602   GtkTreeIter iter;
2603   gboolean is_separator;
2604   gint i, n_children;
2605   GtkWidget *last;
2606   GtkTreePath *path;
2607   
2608   n_children = gtk_tree_model_iter_n_children (model, parent);
2609   
2610   last = NULL;
2611   for (i = 0; i < n_children; i++)
2612     {
2613       gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2614
2615       if (combo_box->priv->row_separator_func)
2616         is_separator = (*combo_box->priv->row_separator_func) (combo_box->priv->model, &iter,
2617                                                                combo_box->priv->row_separator_data);
2618       else
2619         is_separator = FALSE;
2620       
2621       if (is_separator)
2622         {
2623           item = gtk_separator_menu_item_new ();
2624           path = gtk_tree_model_get_path (model, &iter);
2625           g_object_set_data_full (G_OBJECT (item),
2626                                   I_("gtk-combo-box-item-path"),
2627                                   gtk_tree_row_reference_new (model, path),
2628                                   (GDestroyNotify)gtk_tree_row_reference_free);
2629           gtk_tree_path_free (path);
2630         }
2631       else
2632         {
2633           item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2634           if (gtk_tree_model_iter_has_child (model, &iter))
2635             {
2636               submenu = gtk_menu_new ();
2637               gtk_widget_show (submenu);
2638               gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2639               
2640               /* Ugly - since menus can only activate leafs, we have to
2641                * duplicate the item inside the submenu.
2642                */
2643               subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2644               separator = gtk_separator_menu_item_new ();
2645               gtk_widget_show (subitem);
2646               gtk_widget_show (separator);
2647               g_signal_connect (subitem, "activate",
2648                                 G_CALLBACK (gtk_combo_box_menu_item_activate),
2649                                 combo_box);
2650               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2651               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2652               
2653               gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2654             }
2655           else
2656             g_signal_connect (item, "activate",
2657                               G_CALLBACK (gtk_combo_box_menu_item_activate),
2658                               combo_box);
2659         }
2660       
2661       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2662       if (combo_box->priv->wrap_width && menu == combo_box->priv->popup_widget)
2663         gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2664       gtk_widget_show (item);
2665       
2666       last = item;
2667     }
2668 }
2669
2670 static void
2671 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2672 {
2673   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2674                                         G_SIGNAL_MATCH_DATA,
2675                                         0, 0, NULL,
2676                                         gtk_combo_box_menu_button_press, NULL);
2677   g_signal_handlers_disconnect_matched (combo_box->priv->button,
2678                                         G_SIGNAL_MATCH_DATA,
2679                                         0, 0, NULL,
2680                                         gtk_combo_box_button_state_changed, combo_box);
2681
2682   /* unparent will remove our latest ref */
2683   gtk_widget_unparent (combo_box->priv->button);
2684   
2685   combo_box->priv->box = NULL;
2686   combo_box->priv->button = NULL;
2687   combo_box->priv->arrow = NULL;
2688   combo_box->priv->separator = NULL;
2689
2690   g_object_unref (combo_box->priv->column);
2691   combo_box->priv->column = NULL;
2692
2693   /* changing the popup window will unref the menu and the children */
2694 }
2695
2696 /*
2697  * grid
2698  */
2699
2700 static gboolean
2701 menu_occupied (GtkMenu   *menu,
2702                guint      left_attach,
2703                guint      right_attach,
2704                guint      top_attach,
2705                guint      bottom_attach)
2706 {
2707   GList *i;
2708
2709   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2710     {
2711       guint l, r, b, t;
2712
2713       gtk_container_child_get (GTK_CONTAINER (menu), 
2714                                i->data,
2715                                "left-attach", &l,
2716                                "right-attach", &r,
2717                                "bottom-attach", &b,
2718                                "top-attach", &t,
2719                                NULL);
2720
2721       /* look if this item intersects with the given coordinates */
2722       if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2723         return TRUE;
2724     }
2725
2726   return FALSE;
2727 }
2728
2729 static void
2730 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2731                              GtkWidget   *item,
2732                              GtkTreeIter *iter,
2733                              GtkWidget   *last)
2734 {
2735   gint current_col = 0, current_row = 0;
2736   gint rows = 1, cols = 1;
2737   GtkWidget *menu = combo_box->priv->popup_widget;
2738
2739   if (!GTK_IS_MENU_SHELL (menu))
2740     return;
2741   
2742   if (combo_box->priv->col_column == -1 &&
2743       combo_box->priv->row_column == -1 &&
2744       last)
2745     {
2746       gtk_container_child_get (GTK_CONTAINER (menu), 
2747                                last,
2748                                "right_attach", &current_col,
2749                                "top_attach", &current_row,
2750                                NULL);
2751       if (current_col + cols > combo_box->priv->wrap_width)
2752         {
2753           current_col = 0;
2754           current_row++;
2755         }
2756     }
2757   else
2758     {
2759       if (combo_box->priv->col_column != -1)
2760         gtk_tree_model_get (combo_box->priv->model, iter,
2761                             combo_box->priv->col_column, &cols,
2762                             -1);
2763       if (combo_box->priv->row_column != -1)
2764         gtk_tree_model_get (combo_box->priv->model, iter,
2765                             combo_box->priv->row_column, &rows,
2766                             -1);
2767
2768       while (1)
2769         {
2770           if (current_col + cols > combo_box->priv->wrap_width)
2771             {
2772               current_col = 0;
2773               current_row++;
2774             }
2775           
2776           if (!menu_occupied (GTK_MENU (menu), 
2777                               current_col, current_col + cols,
2778                               current_row, current_row + rows))
2779             break;
2780           
2781           current_col++;
2782         }
2783     }
2784
2785   /* set attach props */
2786   gtk_menu_attach (GTK_MENU (menu), item,
2787                    current_col, current_col + cols,
2788                    current_row, current_row + rows);
2789 }
2790
2791 static void
2792 gtk_combo_box_relayout (GtkComboBox *combo_box)
2793 {
2794   GList *list, *j;
2795   GtkWidget *menu;
2796
2797   menu = combo_box->priv->popup_widget;
2798   
2799   /* do nothing unless we are in menu style and realized */
2800   if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
2801     return;
2802   
2803   list = gtk_container_get_children (GTK_CONTAINER (menu));
2804   
2805   for (j = g_list_last (list); j; j = j->prev)
2806     gtk_container_remove (GTK_CONTAINER (menu), j->data);
2807   
2808   gtk_combo_box_menu_fill (combo_box);
2809
2810   g_list_free (list);
2811 }
2812
2813 /* callbacks */
2814 static gboolean
2815 gtk_combo_box_menu_button_press (GtkWidget      *widget,
2816                                  GdkEventButton *event,
2817                                  gpointer        user_data)
2818 {
2819   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2820
2821   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
2822       event->type == GDK_BUTTON_PRESS && event->button == 1)
2823     {
2824       if (combo_box->priv->focus_on_click && 
2825           !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
2826         gtk_widget_grab_focus (combo_box->priv->button);
2827
2828       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
2829
2830       return TRUE;
2831     }
2832
2833   return FALSE;
2834 }
2835
2836 static void
2837 gtk_combo_box_menu_item_activate (GtkWidget *item,
2838                                   gpointer   user_data)
2839 {
2840   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2841   GtkWidget *cell_view;
2842   GtkTreePath *path;
2843   GtkTreeIter iter;
2844
2845   cell_view = GTK_BIN (item)->child;
2846
2847   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
2848
2849   path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
2850
2851   if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
2852     gtk_combo_box_set_active_iter (combo_box, &iter);
2853
2854   gtk_tree_path_free (path);
2855
2856   combo_box->priv->editing_canceled = FALSE;
2857 }
2858
2859 static void
2860 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
2861                                   GtkTreePath      *path,
2862                                   GtkTreeIter      *iter,
2863                                   gpointer          user_data)
2864 {
2865   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2866
2867   if (combo_box->priv->tree_view)
2868     gtk_combo_box_list_popup_resize (combo_box);
2869   else
2870     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
2871 }
2872
2873 static void
2874 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
2875                                  GtkTreePath      *path,
2876                                  gpointer          user_data)
2877 {
2878   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2879
2880   if (combo_box->priv->cell_view)
2881     {
2882       if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
2883         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
2884     }
2885   
2886   if (combo_box->priv->tree_view)
2887     gtk_combo_box_list_popup_resize (combo_box);
2888   else
2889     gtk_combo_box_menu_row_deleted (model, path, user_data);  
2890 }
2891
2892 static void
2893 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
2894                                     GtkTreePath     *path,
2895                                     GtkTreeIter     *iter,
2896                                     gint            *new_order,
2897                                     gpointer         user_data)
2898 {
2899   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2900
2901   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
2902
2903   if (!combo_box->priv->tree_view)
2904     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
2905 }
2906                                                     
2907 static void
2908 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
2909                                  GtkTreePath      *path,
2910                                  GtkTreeIter      *iter,
2911                                  gpointer          user_data)
2912 {
2913   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2914   GtkTreePath *active_path;
2915
2916   /* FIXME this belongs to GtkCellView */
2917   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
2918     {
2919       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
2920       if (gtk_tree_path_compare (path, active_path) == 0 &&
2921           combo_box->priv->cell_view)
2922         gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
2923       gtk_tree_path_free (active_path);
2924     }
2925       
2926   if (combo_box->priv->tree_view)
2927     gtk_combo_box_list_row_changed (model, path, iter, user_data);
2928   else
2929     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
2930 }
2931
2932 static gboolean
2933 list_popup_resize_idle (gpointer user_data)
2934 {
2935   GtkComboBox *combo_box;
2936   gint x, y, width, height;
2937
2938   GDK_THREADS_ENTER ();
2939
2940   combo_box = GTK_COMBO_BOX (user_data);
2941
2942   if (combo_box->priv->tree_view &&
2943       GTK_WIDGET_MAPPED (combo_box->priv->popup_window))
2944     {
2945       gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
2946   
2947       gtk_widget_set_size_request (combo_box->priv->popup_window, width, height);
2948       gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
2949     }
2950
2951   combo_box->priv->resize_idle_id = 0;
2952
2953   GDK_THREADS_LEAVE ();
2954
2955   return FALSE;
2956 }
2957
2958 static void
2959 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
2960 {
2961   if (!combo_box->priv->resize_idle_id)
2962     combo_box->priv->resize_idle_id = 
2963       g_idle_add (list_popup_resize_idle, combo_box);
2964 }
2965
2966 static void
2967 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
2968                                   GtkTreePath      *path,
2969                                   GtkTreeIter      *iter,
2970                                   gpointer          user_data)
2971 {
2972   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
2973   
2974   gtk_combo_box_list_popup_resize (combo_box);
2975 }
2976
2977
2978 static GtkWidget *
2979 find_menu_by_path (GtkWidget   *menu,
2980                    GtkTreePath *path,
2981                    gboolean     skip_first)
2982 {
2983   GList *i, *list;
2984   GtkWidget *item;
2985   GtkWidget *submenu;    
2986   GtkTreeRowReference *mref;
2987   GtkTreePath *mpath;
2988   gboolean skip;
2989
2990   list = gtk_container_get_children (GTK_CONTAINER (menu));
2991   skip = skip_first;
2992   item = NULL;
2993   for (i = list; i; i = i->next)
2994     {
2995       if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
2996         {
2997           mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
2998           if (!mref)
2999             continue;
3000           else if (!gtk_tree_row_reference_valid (mref))
3001             mpath = NULL;
3002           else
3003             mpath = gtk_tree_row_reference_get_path (mref);
3004         }
3005       else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3006         {
3007           if (skip)
3008             {
3009               skip = FALSE;
3010               continue;
3011             }
3012
3013           mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3014         }
3015       else 
3016         continue;
3017
3018       /* this case is necessary, since the row reference of
3019        * the cell view may already be updated after a deletion
3020        */
3021       if (!mpath)
3022         {
3023           item = i->data;
3024           break;
3025         }
3026       if (gtk_tree_path_compare (mpath, path) == 0)
3027         {
3028           gtk_tree_path_free (mpath);
3029           item = i->data;
3030           break;
3031         }
3032       if (gtk_tree_path_is_ancestor (mpath, path))
3033         {
3034           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3035           if (submenu != NULL)
3036             {
3037               gtk_tree_path_free (mpath);
3038               item = find_menu_by_path (submenu, path, TRUE);
3039               break;
3040             }
3041         }
3042       gtk_tree_path_free (mpath);
3043     }
3044   
3045   g_list_free (list);  
3046
3047   return item;
3048 }
3049
3050 #if 0
3051 static void
3052 dump_menu_tree (GtkWidget   *menu, 
3053                 gint         level)
3054 {
3055   GList *i, *list;
3056   GtkWidget *submenu;    
3057   GtkTreePath *path;
3058
3059   list = gtk_container_get_children (GTK_CONTAINER (menu));
3060   for (i = list; i; i = i->next)
3061     {
3062       if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3063         {
3064           path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3065           g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3066           gtk_tree_path_free (path);
3067
3068           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3069           if (submenu != NULL)
3070             dump_menu_tree (submenu, level + 1);
3071         }
3072     }
3073   
3074   g_list_free (list);  
3075 }
3076 #endif
3077
3078 static void
3079 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3080                                  GtkTreePath  *path,
3081                                  GtkTreeIter  *iter,
3082                                  gpointer      user_data)
3083 {
3084   GtkWidget *parent;
3085   GtkWidget *item, *menu, *separator;
3086   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3087   GtkTreePath *ppath;
3088   GtkTreeIter piter;
3089   gint depth, pos;
3090   gboolean is_separator;
3091
3092   if (!combo_box->priv->popup_widget)
3093     return;
3094
3095   depth = gtk_tree_path_get_depth (path);
3096   pos = gtk_tree_path_get_indices (path)[depth - 1];
3097   if (depth > 1)
3098     {
3099       ppath = gtk_tree_path_copy (path);
3100       gtk_tree_path_up (ppath);
3101       parent = find_menu_by_path (combo_box->priv->popup_widget, ppath, FALSE);
3102       gtk_tree_path_free (ppath);
3103
3104       menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3105       if (!menu)
3106         {
3107           menu = gtk_menu_new ();
3108           gtk_widget_show (menu);
3109           gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3110           
3111           /* Ugly - since menus can only activate leaves, we have to
3112            * duplicate the item inside the submenu.
3113            */
3114           gtk_tree_model_iter_parent (model, &piter, iter);
3115           item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3116           separator = gtk_separator_menu_item_new ();
3117           g_signal_connect (item, "activate",
3118                             G_CALLBACK (gtk_combo_box_menu_item_activate),
3119                             combo_box);
3120           gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3121           gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3122           if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3123             {
3124               gtk_widget_show (item);
3125               gtk_widget_show (separator);
3126             }
3127         }
3128       pos += 2;
3129     }
3130   else
3131     {
3132       menu = combo_box->priv->popup_widget;
3133       if (combo_box->priv->add_tearoffs)
3134         pos += 1;
3135     }
3136   
3137   if (combo_box->priv->row_separator_func)
3138     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3139                                                            combo_box->priv->row_separator_data);
3140   else
3141     is_separator = FALSE;
3142
3143   if (is_separator)
3144     {
3145       item = gtk_separator_menu_item_new ();
3146       g_object_set_data_full (G_OBJECT (item),
3147                               I_("gtk-combo-box-item-path"),
3148                               gtk_tree_row_reference_new (model, path),
3149                               (GDestroyNotify)gtk_tree_row_reference_free);
3150     }
3151   else
3152     {
3153       item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3154       
3155       g_signal_connect (item, "activate",
3156                         G_CALLBACK (gtk_combo_box_menu_item_activate),
3157                         combo_box);
3158     }
3159
3160   gtk_widget_show (item);
3161   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3162 }
3163
3164 static void
3165 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3166                                 GtkTreePath  *path,
3167                                 gpointer      user_data)
3168 {
3169   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3170   GtkWidget *menu;
3171   GtkWidget *item;
3172
3173   if (!combo_box->priv->popup_widget)
3174     return;
3175
3176   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3177   menu = gtk_widget_get_parent (item);
3178   gtk_container_remove (GTK_CONTAINER (menu), item);
3179 }
3180
3181 static void
3182 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
3183                                     GtkTreePath      *path,
3184                                     GtkTreeIter      *iter,
3185                                     gint             *new_order,
3186                                     gpointer          user_data)
3187 {
3188   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3189
3190   gtk_combo_box_relayout (combo_box);
3191 }
3192                                     
3193 static void
3194 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3195                                 GtkTreePath  *path,
3196                                 GtkTreeIter  *iter,
3197                                 gpointer      user_data)
3198 {
3199   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3200   GtkWidget *item;
3201   gint width;
3202   gboolean is_separator;
3203
3204   if (!combo_box->priv->popup_widget)
3205     return;
3206
3207   item = find_menu_by_path (combo_box->priv->popup_widget, path, FALSE);
3208
3209   if (combo_box->priv->row_separator_func)
3210     is_separator = (*combo_box->priv->row_separator_func) (model, iter,
3211                                                            combo_box->priv->row_separator_data);
3212   else
3213     is_separator = FALSE;
3214
3215   if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3216     {
3217       gtk_combo_box_menu_row_deleted (model, path, combo_box);
3218       gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3219     }
3220
3221   if (combo_box->priv->wrap_width
3222       && item->parent == combo_box->priv->popup_widget)
3223     {
3224       GtkWidget *pitem = NULL;
3225       GtkTreePath *prev;
3226
3227       prev = gtk_tree_path_copy (path);
3228
3229       if (gtk_tree_path_prev (prev))
3230         pitem = find_menu_by_path (combo_box->priv->popup_widget, prev, FALSE);
3231
3232       gtk_tree_path_free (prev);
3233
3234       /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3235          move it */
3236       gtk_container_child_set (GTK_CONTAINER (combo_box->priv->popup_widget),
3237                                item, 
3238                                "left-attach", -1, 
3239                                "right-attach", -1,
3240                                "top-attach", -1, 
3241                                "bottom-attach", -1, 
3242                                NULL);
3243
3244       gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3245     }
3246
3247   width = gtk_combo_box_calc_requested_width (combo_box, path);
3248
3249   if (width > combo_box->priv->width)
3250     {
3251       if (combo_box->priv->cell_view)
3252         {
3253           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3254           gtk_widget_queue_resize (combo_box->priv->cell_view);
3255         }
3256       combo_box->priv->width = width;
3257     }
3258 }
3259
3260 /*
3261  * list style
3262  */
3263
3264 static void
3265 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3266 {
3267   GtkTreeSelection *sel;
3268
3269   combo_box->priv->button = gtk_toggle_button_new ();
3270   gtk_widget_set_parent (combo_box->priv->button,
3271                          GTK_BIN (combo_box)->child->parent);
3272   g_signal_connect (combo_box->priv->button, "button_press_event",
3273                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3274   g_signal_connect (combo_box->priv->button, "toggled",
3275                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3276   g_signal_connect_after (combo_box, "key_press_event",
3277                           G_CALLBACK (gtk_combo_box_key_press), combo_box);
3278
3279   combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3280   gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
3281                      combo_box->priv->arrow);
3282   combo_box->priv->separator = NULL;
3283   gtk_widget_show_all (combo_box->priv->button);
3284
3285   if (combo_box->priv->cell_view)
3286     {
3287       gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view), 
3288                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3289
3290       combo_box->priv->box = gtk_event_box_new ();
3291       gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->box), 
3292                                         FALSE);
3293
3294       if (combo_box->priv->has_frame)
3295         {
3296           combo_box->priv->cell_view_frame = gtk_frame_new (NULL);
3297           gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
3298                                      GTK_SHADOW_IN);
3299         }
3300       else 
3301         {
3302           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3303           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), 
3304                                             FALSE);
3305         }
3306       
3307       gtk_widget_set_parent (combo_box->priv->cell_view_frame,
3308                              GTK_BIN (combo_box)->child->parent);
3309       gtk_container_add (GTK_CONTAINER (combo_box->priv->cell_view_frame),
3310                          combo_box->priv->box);
3311       gtk_widget_show_all (combo_box->priv->cell_view_frame);
3312
3313       g_signal_connect (combo_box->priv->box, "button_press_event",
3314                         G_CALLBACK (gtk_combo_box_list_button_pressed), 
3315                         combo_box);
3316     }
3317
3318   combo_box->priv->tree_view = gtk_tree_view_new ();
3319   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3320   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3321   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
3322                                      FALSE);
3323   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (combo_box->priv->tree_view),
3324                                      TRUE);
3325   if (combo_box->priv->row_separator_func)
3326     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3327                                           combo_box->priv->row_separator_func, 
3328                                           combo_box->priv->row_separator_data, 
3329                                           NULL);
3330   if (combo_box->priv->model)
3331     gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
3332                              combo_box->priv->model);
3333     
3334   combo_box->priv->column = gtk_tree_view_column_new ();
3335   gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
3336                                combo_box->priv->column);
3337
3338   /* sync up */
3339   gtk_combo_box_sync_cells (combo_box, 
3340                             GTK_CELL_LAYOUT (combo_box->priv->column));
3341
3342   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
3343     {
3344       GtkTreePath *path;
3345
3346       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
3347       gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view),
3348                                 path, NULL, FALSE);
3349       gtk_tree_path_free (path);
3350     }
3351
3352   /* set sample/popup widgets */
3353   gtk_combo_box_set_popup_widget (combo_box, combo_box->priv->tree_view);
3354
3355   g_signal_connect (combo_box->priv->tree_view, "key_press_event",
3356                     G_CALLBACK (gtk_combo_box_list_key_press),
3357                     combo_box);
3358   g_signal_connect (combo_box->priv->tree_view, "enter_notify_event",
3359                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3360                     combo_box);
3361   g_signal_connect (combo_box->priv->tree_view, "row_expanded",
3362                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3363                     combo_box);
3364   g_signal_connect (combo_box->priv->tree_view, "row_collapsed",
3365                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3366                     combo_box);
3367   g_signal_connect (combo_box->priv->popup_window, "button_press_event",
3368                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3369                     combo_box);
3370   g_signal_connect (combo_box->priv->popup_window, "button_release_event",
3371                     G_CALLBACK (gtk_combo_box_list_button_released),
3372                     combo_box);
3373
3374   gtk_widget_show (combo_box->priv->tree_view);
3375 }
3376
3377 static void
3378 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3379 {
3380   /* disconnect signals */
3381   g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
3382                                         G_SIGNAL_MATCH_DATA,
3383                                         0, 0, NULL, NULL, combo_box);
3384   g_signal_handlers_disconnect_matched (combo_box->priv->button,
3385                                         G_SIGNAL_MATCH_DATA,
3386                                         0, 0, NULL,
3387                                         gtk_combo_box_list_button_pressed,
3388                                         NULL);
3389   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3390                                         G_SIGNAL_MATCH_DATA,
3391                                         0, 0, NULL,
3392                                         gtk_combo_box_list_button_pressed,
3393                                         NULL);
3394   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3395                                         G_SIGNAL_MATCH_DATA,
3396                                         0, 0, NULL,
3397                                         gtk_combo_box_list_button_released,
3398                                         NULL);
3399
3400   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3401                                         G_SIGNAL_MATCH_DATA,
3402                                         0, 0, NULL, 
3403                                         gtk_combo_box_child_show,
3404                                         NULL);
3405
3406   g_signal_handlers_disconnect_matched (combo_box->priv->popup_window,
3407                                         G_SIGNAL_MATCH_DATA,
3408                                         0, 0, NULL, 
3409                                         gtk_combo_box_child_hide,
3410                                         NULL);
3411   
3412   if (combo_box->priv->box)
3413     g_signal_handlers_disconnect_matched (combo_box->priv->box,
3414                                           G_SIGNAL_MATCH_DATA,
3415                                           0, 0, NULL,
3416                                           gtk_combo_box_list_button_pressed,
3417                                           NULL);
3418
3419   /* destroy things (unparent will kill the latest ref from us)
3420    * last unref on button will destroy the arrow
3421    */
3422   gtk_widget_unparent (combo_box->priv->button);
3423   combo_box->priv->button = NULL;
3424   combo_box->priv->arrow = NULL;
3425
3426   if (combo_box->priv->cell_view)
3427     {
3428       g_object_set (combo_box->priv->cell_view,
3429                     "background-set", FALSE,
3430                     NULL);
3431     }
3432
3433   if (combo_box->priv->cell_view_frame)
3434     {
3435       gtk_widget_unparent (combo_box->priv->cell_view_frame);
3436       combo_box->priv->cell_view_frame = NULL;
3437       combo_box->priv->box = NULL;
3438     }
3439
3440   if (combo_box->priv->scroll_timer)
3441     {
3442       g_source_remove (combo_box->priv->scroll_timer);
3443       combo_box->priv->scroll_timer = 0;
3444     }
3445
3446   if (combo_box->priv->resize_idle_id)
3447     {
3448       g_source_remove (combo_box->priv->resize_idle_id);
3449       combo_box->priv->resize_idle_id = 0;
3450     }
3451
3452   gtk_widget_destroy (combo_box->priv->tree_view);
3453
3454   combo_box->priv->tree_view = NULL;
3455   combo_box->priv->popup_widget = NULL;
3456 }
3457
3458 /* callbacks */
3459
3460 static gboolean
3461 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3462                                    GdkEventButton *event,
3463                                    gpointer        data)
3464 {
3465   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3466
3467   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3468
3469   if (ewidget == combo_box->priv->popup_window)
3470     return TRUE;
3471
3472   if ((ewidget != combo_box->priv->button && ewidget != combo_box->priv->box) ||
3473       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3474     return FALSE;
3475
3476   if (combo_box->priv->focus_on_click && 
3477       !GTK_WIDGET_HAS_FOCUS (combo_box->priv->button))
3478     gtk_widget_grab_focus (combo_box->priv->button);
3479
3480   gtk_combo_box_popup (combo_box);
3481
3482   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
3483                                 TRUE);
3484
3485   combo_box->priv->auto_scroll = FALSE;
3486   if (combo_box->priv->scroll_timer == 0)
3487     combo_box->priv->scroll_timer = g_timeout_add (SCROLL_TIME, 
3488                                                    (GSourceFunc) gtk_combo_box_list_scroll_timeout, 
3489                                                    combo_box);
3490
3491   combo_box->priv->popup_in_progress = TRUE;
3492
3493   return TRUE;
3494 }
3495
3496 static gboolean
3497 gtk_combo_box_list_button_released (GtkWidget      *widget,
3498                                     GdkEventButton *event,
3499                                     gpointer        data)
3500 {
3501   gboolean ret;
3502   GtkTreePath *path = NULL;
3503   GtkTreeIter iter;
3504
3505   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3506
3507   gboolean popup_in_progress = FALSE;
3508
3509   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3510
3511   if (combo_box->priv->popup_in_progress)
3512     {
3513       popup_in_progress = TRUE;
3514       combo_box->priv->popup_in_progress = FALSE;
3515     }
3516
3517   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (combo_box->priv->tree_view), 
3518                                   FALSE);
3519   if (combo_box->priv->scroll_timer)
3520     {
3521       g_source_remove (combo_box->priv->scroll_timer);
3522       combo_box->priv->scroll_timer = 0;
3523     }
3524
3525   if (ewidget != combo_box->priv->tree_view)
3526     {
3527       if ((ewidget == combo_box->priv->button || 
3528            ewidget == combo_box->priv->box) &&
3529           !popup_in_progress &&
3530           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (combo_box->priv->button)))
3531         {
3532           gtk_combo_box_popdown (combo_box);
3533           return TRUE;
3534         }
3535
3536       /* released outside treeview */
3537       if (ewidget != combo_box->priv->button && 
3538           ewidget != combo_box->priv->box)
3539         {
3540           gtk_combo_box_popdown (combo_box);
3541
3542           return TRUE;
3543         }
3544
3545       return FALSE;
3546     }
3547
3548   /* select something cool */
3549   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (combo_box->priv->tree_view),
3550                                        event->x, event->y,
3551                                        &path,
3552                                        NULL, NULL, NULL);
3553
3554   if (!ret)
3555     return TRUE; /* clicked outside window? */
3556
3557   gtk_tree_model_get_iter (combo_box->priv->model, &iter, path);
3558   gtk_tree_path_free (path);
3559
3560   if (tree_column_row_is_sensitive (combo_box, &iter))
3561     gtk_combo_box_set_active_iter (combo_box, &iter);
3562
3563   gtk_combo_box_popdown (combo_box);
3564
3565   return TRUE;
3566 }
3567
3568 static gboolean
3569 gtk_combo_box_key_press (GtkWidget   *widget,
3570                          GdkEventKey *event,
3571                          gpointer     data)
3572 {
3573   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3574   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3575   gboolean found;
3576   GtkTreeIter iter;
3577   GtkTreeIter new_iter;
3578
3579   if (combo_box->priv->model == NULL)
3580     return FALSE;
3581
3582   if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) && 
3583       state == GDK_MOD1_MASK)
3584     {
3585       gtk_combo_box_popup (combo_box);
3586
3587       return TRUE;
3588     }
3589
3590   if (state != 0)
3591     return FALSE;
3592
3593   switch (event->keyval) 
3594     {
3595     case GDK_Down:
3596     case GDK_KP_Down:
3597       if (gtk_combo_box_get_active_iter (combo_box, &iter))
3598         {
3599           found = tree_next (combo_box, combo_box->priv->model, 
3600                              &iter, &new_iter, FALSE);
3601           break;
3602         }
3603       /* else fall through */
3604     case GDK_Page_Up:
3605     case GDK_KP_Page_Up:
3606     case GDK_Home: 
3607     case GDK_KP_Home:
3608       found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
3609       break;
3610
3611     case GDK_Up:
3612     case GDK_KP_Up:
3613       if (gtk_combo_box_get_active_iter (combo_box, &iter))
3614         {
3615           found = tree_prev (combo_box, combo_box->priv->model, 
3616                              &iter, &new_iter, FALSE);
3617           break;
3618         }
3619       /* else fall through */      
3620     case GDK_Page_Down:
3621     case GDK_KP_Page_Down:
3622     case GDK_End: 
3623     case GDK_KP_End:
3624       found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
3625       break;
3626     default:
3627       return FALSE;
3628     }
3629       
3630   if (found)
3631     gtk_combo_box_set_active_iter (combo_box, &new_iter);
3632   
3633   return TRUE;
3634 }
3635
3636 static gboolean
3637 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3638                               GdkEventKey *event,
3639                               gpointer     data)
3640 {
3641   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3642   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3643
3644   if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3645       state == GDK_MOD1_MASK)
3646     {
3647       gtk_combo_box_popdown (combo_box);
3648
3649       return TRUE;
3650     }
3651   
3652   return FALSE;
3653 }
3654
3655 static gboolean
3656 gtk_combo_box_list_key_press (GtkWidget   *widget,
3657                               GdkEventKey *event,
3658                               gpointer     data)
3659 {
3660   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3661   GtkTreeIter iter;
3662   guint state = event->state & gtk_accelerator_get_default_mod_mask ();
3663
3664   if (event->keyval == GDK_Escape ||
3665       ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) && 
3666        state == GDK_MOD1_MASK))
3667     {
3668       /* reset active item -- this is incredibly lame and ugly */
3669       if (gtk_combo_box_get_active_iter (combo_box, &iter))
3670         gtk_combo_box_set_active_iter (combo_box, &iter);
3671       
3672       gtk_combo_box_popdown (combo_box);
3673       
3674       return TRUE;
3675     }
3676     
3677   if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
3678       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
3679   {
3680     GtkTreeIter iter;
3681     GtkTreeModel *model = NULL;
3682     
3683     if (combo_box->priv->model)
3684       {
3685         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3686     
3687         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3688           gtk_combo_box_set_active_iter (combo_box, &iter);
3689       }
3690
3691     gtk_combo_box_popdown (combo_box);
3692     
3693     return TRUE;
3694   }
3695
3696   return FALSE;
3697 }
3698
3699 static void
3700 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3701                                 gint         x, 
3702                                 gint         y)
3703 {
3704   GtkWidget *tree_view = combo_box->priv->tree_view;
3705   GtkAdjustment *adj;
3706   gdouble value;
3707
3708   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3709   if (adj && adj->upper - adj->lower > adj->page_size)
3710     {
3711       if (x <= tree_view->allocation.x && 
3712           adj->lower < adj->value)
3713         {
3714           value = adj->value - (tree_view->allocation.x - x + 1);
3715           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3716         }
3717       else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3718                adj->upper - adj->page_size > adj->value)
3719         {
3720           value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3721           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3722         }
3723     }
3724
3725   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3726   if (adj && adj->upper - adj->lower > adj->page_size)
3727     {
3728       if (y <= tree_view->allocation.y && 
3729           adj->lower < adj->value)
3730         {
3731           value = adj->value - (tree_view->allocation.y - y + 1);
3732           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3733         }
3734       else if (y >= tree_view->allocation.height &&
3735                adj->upper - adj->page_size > adj->value)
3736         {
3737           value = adj->value + (y - tree_view->allocation.height + 1);
3738           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3739         }
3740     }
3741 }
3742
3743 static gboolean
3744 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3745 {
3746   gint x, y;
3747
3748   GDK_THREADS_ENTER ();
3749
3750   if (combo_box->priv->auto_scroll)
3751     {
3752       gdk_window_get_pointer (combo_box->priv->tree_view->window, 
3753                               &x, &y, NULL);
3754       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3755     }
3756
3757   GDK_THREADS_LEAVE ();
3758
3759   return TRUE;
3760 }
3761
3762 static gboolean 
3763 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3764                                  GdkEventCrossing *event,
3765                                  gpointer          data)
3766 {
3767   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3768
3769   combo_box->priv->auto_scroll = TRUE;
3770
3771   return TRUE;
3772 }
3773
3774
3775 static void
3776 gtk_combo_box_list_row_changed (GtkTreeModel *model,
3777                                 GtkTreePath  *path,
3778                                 GtkTreeIter  *iter,
3779                                 gpointer      data)
3780 {
3781   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3782   gint width;
3783
3784   width = gtk_combo_box_calc_requested_width (combo_box, path);
3785
3786   if (width > combo_box->priv->width)
3787     {
3788       if (combo_box->priv->cell_view) 
3789         {
3790           gtk_widget_set_size_request (combo_box->priv->cell_view, width, -1);
3791           gtk_widget_queue_resize (combo_box->priv->cell_view);
3792         }
3793       combo_box->priv->width = width;
3794     }
3795 }
3796
3797 /*
3798  * GtkCellLayout implementation
3799  */
3800
3801 static void
3802 pack_start_recurse (GtkWidget       *menu,
3803                     GtkCellRenderer *cell,
3804                     gboolean         expand)
3805 {
3806   GList *i, *list;
3807   GtkWidget *submenu;    
3808   
3809   list = gtk_container_get_children (GTK_CONTAINER (menu));
3810   for (i = list; i; i = i->next)
3811     {
3812       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3813         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3814                                     cell, expand);
3815
3816       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3817       if (submenu != NULL)
3818         pack_start_recurse (submenu, cell, expand);
3819     }
3820
3821   g_list_free (list);
3822 }
3823
3824 static void
3825 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
3826                                       GtkCellRenderer *cell,
3827                                       gboolean         expand)
3828 {
3829   ComboCellInfo *info;
3830   GtkComboBox *combo_box;
3831
3832   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3833   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3834
3835   combo_box = GTK_COMBO_BOX (layout);
3836
3837   g_object_ref_sink (cell);
3838
3839   info = g_new0 (ComboCellInfo, 1);
3840   info->cell = cell;
3841   info->expand = expand;
3842   info->pack = GTK_PACK_START;
3843
3844   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3845
3846   if (combo_box->priv->cell_view)
3847     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3848                                 cell, expand);
3849
3850   if (combo_box->priv->column)
3851     gtk_tree_view_column_pack_start (combo_box->priv->column, cell, expand);
3852
3853   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3854     pack_start_recurse (combo_box->priv->popup_widget, cell, expand);
3855 }
3856
3857 static void
3858 pack_end_recurse (GtkWidget       *menu,
3859                   GtkCellRenderer *cell,
3860                   gboolean         expand)
3861 {
3862   GList *i, *list;
3863   GtkWidget *submenu;    
3864   
3865   list = gtk_container_get_children (GTK_CONTAINER (menu));
3866   for (i = list; i; i = i->next)
3867     {
3868       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3869         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
3870                                   cell, expand);
3871
3872       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3873       if (submenu != NULL)
3874         pack_end_recurse (submenu, cell, expand);
3875     }
3876
3877   g_list_free (list);
3878 }
3879
3880 static void
3881 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
3882                                     GtkCellRenderer *cell,
3883                                     gboolean         expand)
3884 {
3885   ComboCellInfo *info;
3886   GtkComboBox *combo_box;
3887
3888   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3889   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3890
3891   combo_box = GTK_COMBO_BOX (layout);
3892
3893   g_object_ref_sink (cell);
3894
3895   info = g_new0 (ComboCellInfo, 1);
3896   info->cell = cell;
3897   info->expand = expand;
3898   info->pack = GTK_PACK_END;
3899
3900   combo_box->priv->cells = g_slist_append (combo_box->priv->cells, info);
3901
3902   if (combo_box->priv->cell_view)
3903     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
3904                               cell, expand);
3905
3906   if (combo_box->priv->column)
3907     gtk_tree_view_column_pack_end (combo_box->priv->column, cell, expand);
3908
3909   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3910     pack_end_recurse (combo_box->priv->popup_widget, cell, expand);
3911 }
3912
3913 static void
3914 clear_recurse (GtkWidget *menu)
3915 {
3916   GList *i, *list;
3917   GtkWidget *submenu;    
3918   
3919   list = gtk_container_get_children (GTK_CONTAINER (menu));
3920   for (i = list; i; i = i->next)
3921     {
3922       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3923         gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child)); 
3924
3925       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3926       if (submenu != NULL)
3927         clear_recurse (submenu);
3928     }
3929
3930   g_list_free (list);
3931 }
3932
3933 static void
3934 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
3935 {
3936   GtkComboBox *combo_box;
3937   GSList *i;
3938   
3939   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3940
3941   combo_box = GTK_COMBO_BOX (layout);
3942  
3943   if (combo_box->priv->cell_view)
3944     gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box->priv->cell_view));
3945
3946   if (combo_box->priv->column)
3947     gtk_tree_view_column_clear (combo_box->priv->column);
3948
3949   for (i = combo_box->priv->cells; i; i = i->next)
3950     {
3951      ComboCellInfo *info = (ComboCellInfo *)i->data;
3952
3953       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
3954       g_object_unref (info->cell);
3955       g_free (info);
3956       i->data = NULL;
3957     }
3958   g_slist_free (combo_box->priv->cells);
3959   combo_box->priv->cells = NULL;
3960
3961   if (GTK_IS_MENU (combo_box->priv->popup_widget))
3962     clear_recurse (combo_box->priv->popup_widget);
3963 }
3964
3965 static void
3966 add_attribute_recurse (GtkWidget       *menu,
3967                        GtkCellRenderer *cell,
3968                        const gchar     *attribute,
3969                        gint             column)
3970 {
3971   GList *i, *list;
3972   GtkWidget *submenu;    
3973   
3974   list = gtk_container_get_children (GTK_CONTAINER (menu));
3975   for (i = list; i; i = i->next)
3976     {
3977       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
3978         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
3979                                        cell, attribute, column); 
3980
3981       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3982       if (submenu != NULL)
3983         add_attribute_recurse (submenu, cell, attribute, column);
3984     }
3985
3986   g_list_free (list);
3987 }
3988                        
3989 static void
3990 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
3991                                          GtkCellRenderer *cell,
3992                                          const gchar     *attribute,
3993                                          gint             column)
3994 {
3995   ComboCellInfo *info;
3996   GtkComboBox *combo_box;
3997
3998   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
3999   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4000
4001   combo_box = GTK_COMBO_BOX (layout);
4002
4003   info = gtk_combo_box_get_cell_info (combo_box, cell);
4004
4005   info->attributes = g_slist_prepend (info->attributes,
4006                                       GINT_TO_POINTER (column));
4007   info->attributes = g_slist_prepend (info->attributes,
4008                                       g_strdup (attribute));
4009
4010   if (combo_box->priv->cell_view)
4011     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4012                                    cell, attribute, column);
4013
4014   if (combo_box->priv->column)
4015     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4016                                    cell, attribute, column);
4017
4018   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4019     add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4020   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4021 }
4022
4023 static void
4024 combo_cell_data_func (GtkCellLayout   *cell_layout,
4025                       GtkCellRenderer *cell,
4026                       GtkTreeModel    *tree_model,
4027                       GtkTreeIter     *iter,
4028                       gpointer         data)
4029 {
4030   ComboCellInfo *info = (ComboCellInfo *)data;
4031   GtkWidget *parent = NULL;
4032   
4033   if (!info->func)
4034     return;
4035
4036   (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
4037   
4038   if (GTK_IS_WIDGET (cell_layout))
4039     parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4040   
4041   if (GTK_IS_MENU_ITEM (parent) && 
4042       gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4043     g_object_set (cell, "sensitive", TRUE, NULL);
4044 }
4045
4046
4047 static void 
4048 set_cell_data_func_recurse (GtkWidget             *menu,
4049                             GtkCellRenderer       *cell,
4050                             ComboCellInfo         *info)
4051 {
4052   GList *i, *list;
4053   GtkWidget *submenu;    
4054   GtkWidget *cell_view;
4055   
4056  list = gtk_container_get_children (GTK_CONTAINER (menu));
4057   for (i = list; i; i = i->next)
4058     {
4059       cell_view = GTK_BIN (i->data)->child;
4060       if (GTK_IS_CELL_LAYOUT (cell_view))
4061         {
4062           /* Override sensitivity for inner nodes; we don't
4063            * want menuitems with submenus to appear insensitive 
4064            */ 
4065           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view), 
4066                                               cell, 
4067                                               combo_cell_data_func, 
4068                                               info, NULL); 
4069           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4070           if (submenu != NULL)
4071             set_cell_data_func_recurse (submenu, cell, info);
4072         }
4073     }
4074
4075   g_list_free (list);
4076 }
4077
4078 static void
4079 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4080                                               GtkCellRenderer       *cell,
4081                                               GtkCellLayoutDataFunc  func,
4082                                               gpointer               func_data,
4083                                               GDestroyNotify         destroy)
4084 {
4085   ComboCellInfo *info;
4086   GtkComboBox *combo_box;
4087
4088   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4089
4090   combo_box = GTK_COMBO_BOX (layout);
4091
4092   info = gtk_combo_box_get_cell_info (combo_box, cell);
4093   g_return_if_fail (info != NULL);
4094   
4095   if (info->destroy)
4096     {
4097       GDestroyNotify d = info->destroy;
4098
4099       info->destroy = NULL;
4100       d (info->func_data);
4101     }
4102
4103   info->func = func;
4104   info->func_data = func_data;
4105   info->destroy = destroy;
4106
4107   if (combo_box->priv->cell_view)
4108     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell, func, func_data, NULL);
4109
4110   if (combo_box->priv->column)
4111     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo_box->priv->column), cell, func, func_data, NULL);
4112
4113   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4114     set_cell_data_func_recurse (combo_box->priv->popup_widget, cell, info);
4115
4116   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4117 }
4118
4119 static void 
4120 clear_attributes_recurse (GtkWidget             *menu,
4121                           GtkCellRenderer       *cell)
4122 {
4123   GList *i, *list;
4124   GtkWidget *submenu;    
4125   
4126   list = gtk_container_get_children (GTK_CONTAINER (menu));
4127   for (i = list; i; i = i->next)
4128     {
4129       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4130         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4131                                             cell); 
4132       
4133       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4134       if (submenu != NULL)
4135         clear_attributes_recurse (submenu, cell);
4136     }
4137
4138   g_list_free (list);
4139 }
4140
4141 static void
4142 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
4143                                             GtkCellRenderer *cell)
4144 {
4145   ComboCellInfo *info;
4146   GtkComboBox *combo_box;
4147   GSList *list;
4148
4149   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4150   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4151
4152   combo_box = GTK_COMBO_BOX (layout);
4153
4154   info = gtk_combo_box_get_cell_info (combo_box, cell);
4155   g_return_if_fail (info != NULL);
4156
4157   list = info->attributes;
4158   while (list && list->next)
4159     {
4160       g_free (list->data);
4161       list = list->next->next;
4162     }
4163   g_slist_free (info->attributes);
4164   info->attributes = NULL;
4165
4166   if (combo_box->priv->cell_view)
4167     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->cell_view), cell);
4168
4169   if (combo_box->priv->column)
4170     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (combo_box->priv->column), cell);
4171
4172   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4173     clear_attributes_recurse (combo_box->priv->popup_widget, cell);
4174
4175   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4176 }
4177
4178 static void 
4179 reorder_recurse (GtkWidget             *menu,
4180                  GtkCellRenderer       *cell,
4181                  gint                   position)
4182 {
4183   GList *i, *list;
4184   GtkWidget *submenu;    
4185   
4186   list = gtk_container_get_children (GTK_CONTAINER (menu));
4187   for (i = list; i; i = i->next)
4188     {
4189       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4190         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4191                                  cell, position); 
4192       
4193       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4194       if (submenu != NULL)
4195         reorder_recurse (submenu, cell, position);
4196     }
4197
4198   g_list_free (list);
4199 }
4200
4201 static void
4202 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
4203                                    GtkCellRenderer *cell,
4204                                    gint             position)
4205 {
4206   ComboCellInfo *info;
4207   GtkComboBox *combo_box;
4208   GSList *link;
4209
4210   g_return_if_fail (GTK_IS_COMBO_BOX (layout));
4211   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4212
4213   combo_box = GTK_COMBO_BOX (layout);
4214
4215   info = gtk_combo_box_get_cell_info (combo_box, cell);
4216
4217   g_return_if_fail (info != NULL);
4218   g_return_if_fail (position >= 0);
4219
4220   link = g_slist_find (combo_box->priv->cells, info);
4221
4222   g_return_if_fail (link != NULL);
4223
4224   combo_box->priv->cells = g_slist_remove_link (combo_box->priv->cells, link);
4225   combo_box->priv->cells = g_slist_insert (combo_box->priv->cells, info,
4226                                            position);
4227
4228   if (combo_box->priv->cell_view)
4229     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4230                              cell, position);
4231
4232   if (combo_box->priv->column)
4233     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (combo_box->priv->column),
4234                              cell, position);
4235
4236   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4237     reorder_recurse (combo_box->priv->popup_widget, cell, position);
4238
4239   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4240 }
4241
4242 /*
4243  * public API
4244  */
4245
4246 /**
4247  * gtk_combo_box_new:
4248  *
4249  * Creates a new empty #GtkComboBox.
4250  *
4251  * Return value: A new #GtkComboBox.
4252  *
4253  * Since: 2.4
4254  */
4255 GtkWidget *
4256 gtk_combo_box_new (void)
4257 {
4258   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4259 }
4260
4261 /**
4262  * gtk_combo_box_new_with_model:
4263  * @model: A #GtkTreeModel.
4264  *
4265  * Creates a new #GtkComboBox with the model initialized to @model.
4266  *
4267  * Return value: A new #GtkComboBox.
4268  *
4269  * Since: 2.4
4270  */
4271 GtkWidget *
4272 gtk_combo_box_new_with_model (GtkTreeModel *model)
4273 {
4274   GtkComboBox *combo_box;
4275
4276   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4277
4278   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4279
4280   return GTK_WIDGET (combo_box);
4281 }
4282
4283 /**
4284  * gtk_combo_box_get_wrap_width:
4285  * @combo_box: A #GtkComboBox.
4286  *
4287  * Returns the wrap width which is used to determine the number
4288  * of columns for the popup menu. If the wrap width is larger than
4289  * 1, the combo box is in table mode.
4290  *
4291  * Returns: the wrap width.
4292  *
4293  * Since: 2.6
4294  */
4295 gint
4296 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4297 {
4298   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4299
4300   return combo_box->priv->wrap_width;
4301 }
4302
4303 /**
4304  * gtk_combo_box_set_wrap_width:
4305  * @combo_box: A #GtkComboBox.
4306  * @width: Preferred number of columns.
4307  *
4308  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4309  * the preferred number of columns when you want the popup to be layed out
4310  * in a table.
4311  *
4312  * Since: 2.4
4313  */
4314 void
4315 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4316                               gint         width)
4317 {
4318   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4319   g_return_if_fail (width >= 0);
4320
4321   if (width != combo_box->priv->wrap_width)
4322     {
4323       combo_box->priv->wrap_width = width;
4324
4325       gtk_combo_box_check_appearance (combo_box);
4326       gtk_combo_box_relayout (combo_box);
4327       
4328       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4329     }
4330 }
4331
4332 /**
4333  * gtk_combo_box_get_row_span_column:
4334  * @combo_box: A #GtkComboBox.
4335  *
4336  * Returns the column with row span information for @combo_box.
4337  *
4338  * Returns: the row span column.
4339  *
4340  * Since: 2.6
4341  */
4342 gint
4343 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4344 {
4345   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4346
4347   return combo_box->priv->row_column;
4348 }
4349
4350 /**
4351  * gtk_combo_box_set_row_span_column:
4352  * @combo_box: A #GtkComboBox.
4353  * @row_span: A column in the model passed during construction.
4354  *
4355  * Sets the column with row span information for @combo_box to be @row_span.
4356  * The row span column contains integers which indicate how many rows
4357  * an item should span.
4358  *
4359  * Since: 2.4
4360  */
4361 void
4362 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4363                                    gint         row_span)
4364 {
4365   gint col;
4366
4367   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4368
4369   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4370   g_return_if_fail (row_span >= -1 && row_span < col);
4371
4372   if (row_span != combo_box->priv->row_column)
4373     {
4374       combo_box->priv->row_column = row_span;
4375       
4376       gtk_combo_box_relayout (combo_box);
4377  
4378       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4379     }
4380 }
4381
4382 /**
4383  * gtk_combo_box_get_column_span_column:
4384  * @combo_box: A #GtkComboBox.
4385  *
4386  * Returns the column with column span information for @combo_box.
4387  *
4388  * Returns: the column span column.
4389  *
4390  * Since: 2.6
4391  */
4392 gint
4393 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4394 {
4395   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4396
4397   return combo_box->priv->col_column;
4398 }
4399
4400 /**
4401  * gtk_combo_box_set_column_span_column:
4402  * @combo_box: A #GtkComboBox.
4403  * @column_span: A column in the model passed during construction.
4404  *
4405  * Sets the column with column span information for @combo_box to be
4406  * @column_span. The column span column contains integers which indicate
4407  * how many columns an item should span.
4408  *
4409  * Since: 2.4
4410  */
4411 void
4412 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4413                                       gint         column_span)
4414 {
4415   gint col;
4416
4417   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4418
4419   col = gtk_tree_model_get_n_columns (combo_box->priv->model);
4420   g_return_if_fail (column_span >= -1 && column_span < col);
4421
4422   if (column_span != combo_box->priv->col_column)
4423     {
4424       combo_box->priv->col_column = column_span;
4425       
4426       gtk_combo_box_relayout (combo_box);
4427
4428       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4429     }
4430 }
4431
4432 /**
4433  * gtk_combo_box_get_active:
4434  * @combo_box: A #GtkComboBox.
4435  *
4436  * Returns the index of the currently active item, or -1 if there's no
4437  * active item. If the model is a non-flat treemodel, and the active item 
4438  * is not an immediate child of the root of the tree, this function returns 
4439  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where 
4440  * <literal>path</literal> is the #GtkTreePath of the active item.
4441  *
4442  * Return value: An integer which is the index of the currently active item, or
4443  * -1 if there's no active item.
4444  *
4445  * Since: 2.4
4446  */
4447 gint
4448 gtk_combo_box_get_active (GtkComboBox *combo_box)
4449 {
4450   gint result;
4451   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4452
4453   if (gtk_tree_row_reference_valid (combo_box->priv->active_row))
4454     {
4455       GtkTreePath *path;
4456
4457       path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);      
4458       result = gtk_tree_path_get_indices (path)[0];
4459       gtk_tree_path_free (path);
4460     }
4461   else
4462     result = -1;
4463
4464   return result;
4465 }
4466
4467 /**
4468  * gtk_combo_box_set_active:
4469  * @combo_box: A #GtkComboBox.
4470  * @index_: An index in the model passed during construction, or -1 to have
4471  * no active item.
4472  *
4473  * Sets the active item of @combo_box to be the item at @index.
4474  *
4475  * Since: 2.4
4476  */
4477 void
4478 gtk_combo_box_set_active (GtkComboBox *combo_box,
4479                           gint         index_)
4480 {
4481   GtkTreePath *path = NULL;
4482   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4483   g_return_if_fail (index_ >= -1);
4484
4485   if (index_ != -1)
4486     path = gtk_tree_path_new_from_indices (index_, -1);
4487    
4488   gtk_combo_box_set_active_internal (combo_box, path);
4489
4490   if (path)
4491     gtk_tree_path_free (path);
4492 }
4493
4494 static void
4495 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4496                                    GtkTreePath *path)
4497 {
4498   GtkTreePath *active_path;
4499   gint path_cmp;
4500
4501   if (path && gtk_tree_row_reference_valid (combo_box->priv->active_row))
4502     {
4503       active_path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4504       path_cmp = gtk_tree_path_compare (path, active_path);
4505       gtk_tree_path_free (active_path);
4506       if (path_cmp == 0)
4507         return;
4508     }
4509
4510   if (combo_box->priv->active_row)
4511     {
4512       gtk_tree_row_reference_free (combo_box->priv->active_row);
4513       combo_box->priv->active_row = NULL;
4514     }
4515   
4516   if (!path)
4517     {
4518       if (combo_box->priv->tree_view)
4519         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view)));
4520       else
4521         {
4522           GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
4523
4524           if (GTK_IS_MENU (menu))
4525             gtk_menu_set_active (menu, -1);
4526         }
4527
4528       if (combo_box->priv->cell_view)
4529         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), NULL);
4530     }
4531   else
4532     {
4533       combo_box->priv->active_row = 
4534         gtk_tree_row_reference_new (combo_box->priv->model, path);
4535
4536       if (combo_box->priv->tree_view)
4537         {
4538           gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), 
4539                                     path, NULL, FALSE);
4540         }
4541       else if (GTK_IS_MENU (combo_box->priv->popup_widget))
4542         {
4543           /* FIXME handle nested menus better */
4544           gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget), 
4545                                gtk_tree_path_get_indices (path)[0]);
4546         }
4547
4548       if (combo_box->priv->cell_view)
4549         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), 
4550                                          path);
4551     }
4552
4553   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4554   g_object_notify (G_OBJECT (combo_box), "active");
4555 }
4556
4557
4558 /**
4559  * gtk_combo_box_get_active_iter:
4560  * @combo_box: A #GtkComboBox
4561  * @iter: The uninitialized #GtkTreeIter.
4562  * 
4563  * Sets @iter to point to the current active item, if it exists.
4564  * 
4565  * Return value: %TRUE, if @iter was set
4566  *
4567  * Since: 2.4
4568  **/
4569 gboolean
4570 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4571                                GtkTreeIter     *iter)
4572 {
4573   GtkTreePath *path;
4574   gboolean result;
4575
4576   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4577
4578   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4579     return FALSE;
4580
4581   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4582   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4583   gtk_tree_path_free (path);
4584
4585   return result;
4586 }
4587
4588 /**
4589  * gtk_combo_box_set_active_iter:
4590  * @combo_box: A #GtkComboBox
4591  * @iter: The #GtkTreeIter.
4592  * 
4593  * Sets the current active item to be the one referenced by @iter. 
4594  * @iter must correspond to a path of depth one.
4595  * 
4596  * Since: 2.4
4597  **/
4598 void
4599 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4600                                GtkTreeIter     *iter)
4601 {
4602   GtkTreePath *path;
4603
4604   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4605
4606   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4607   gtk_combo_box_set_active_internal (combo_box, path);
4608   gtk_tree_path_free (path);
4609 }
4610
4611 /**
4612  * gtk_combo_box_set_model:
4613  * @combo_box: A #GtkComboBox.
4614  * @model: A #GtkTreeModel.
4615  *
4616  * Sets the model used by @combo_box to be @model. Will unset a previously set 
4617  * model (if applicable). If model is %NULL, then it will unset the model.
4618  *
4619  * Note that this function does not clear the cell renderers, you have to 
4620  * call gtk_combo_box_cell_layout_clear() yourself if you need to set up 
4621  * different cell renderers for the new model.
4622  *
4623  * Since: 2.4
4624  */
4625 void
4626 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4627                          GtkTreeModel *model)
4628 {
4629   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4630
4631   if (!model)
4632     {
4633       gtk_combo_box_unset_model (combo_box);
4634       return;
4635     }
4636
4637   g_return_if_fail (GTK_IS_TREE_MODEL (model));
4638
4639   if (model == combo_box->priv->model)
4640     return;
4641   
4642   if (combo_box->priv->model)
4643     gtk_combo_box_unset_model (combo_box);
4644
4645   combo_box->priv->model = model;
4646   g_object_ref (combo_box->priv->model);
4647
4648   combo_box->priv->inserted_id =
4649     g_signal_connect (combo_box->priv->model, "row_inserted",
4650                       G_CALLBACK (gtk_combo_box_model_row_inserted),
4651                       combo_box);
4652   combo_box->priv->deleted_id =
4653     g_signal_connect (combo_box->priv->model, "row_deleted",
4654                       G_CALLBACK (gtk_combo_box_model_row_deleted),
4655                       combo_box);
4656   combo_box->priv->reordered_id =
4657     g_signal_connect (combo_box->priv->model, "rows_reordered",
4658                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
4659                       combo_box);
4660   combo_box->priv->changed_id =
4661     g_signal_connect (combo_box->priv->model, "row_changed",
4662                       G_CALLBACK (gtk_combo_box_model_row_changed),
4663                       combo_box);
4664       
4665   if (combo_box->priv->tree_view)
4666     {
4667       /* list mode */
4668       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4669                                combo_box->priv->model);
4670       gtk_combo_box_list_popup_resize (combo_box);
4671     }
4672   else
4673     {
4674       /* menu mode */
4675       if (combo_box->priv->popup_widget)
4676         gtk_combo_box_menu_fill (combo_box);
4677
4678     }
4679
4680   if (combo_box->priv->cell_view)
4681     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4682                              combo_box->priv->model);
4683 }
4684
4685 /**
4686  * gtk_combo_box_get_model
4687  * @combo_box: A #GtkComboBox.
4688  *
4689  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4690  *
4691  * Return value: A #GtkTreeModel which was passed during construction.
4692  *
4693  * Since: 2.4
4694  */
4695 GtkTreeModel *
4696 gtk_combo_box_get_model (GtkComboBox *combo_box)
4697 {
4698   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4699
4700   return combo_box->priv->model;
4701 }
4702
4703
4704 /* convenience API for simple text combos */
4705
4706 /**
4707  * gtk_combo_box_new_text:
4708  *
4709  * Convenience function which constructs a new text combo box, which is a
4710  * #GtkComboBox just displaying strings. If you use this function to create
4711  * a text combo box, you should only manipulate its data source with the
4712  * following convenience functions: gtk_combo_box_append_text(),
4713  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
4714  * gtk_combo_box_remove_text().
4715  *
4716  * Return value: A new text combo box.
4717  *
4718  * Since: 2.4
4719  */
4720 GtkWidget *
4721 gtk_combo_box_new_text (void)
4722 {
4723   GtkWidget *combo_box;
4724   GtkCellRenderer *cell;
4725   GtkListStore *store;
4726
4727   store = gtk_list_store_new (1, G_TYPE_STRING);
4728   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
4729   g_object_unref (store);
4730
4731   cell = gtk_cell_renderer_text_new ();
4732   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
4733   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
4734                                   "text", 0,
4735                                   NULL);
4736
4737   return combo_box;
4738 }
4739
4740 /**
4741  * gtk_combo_box_append_text:
4742  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
4743  * @text: A string.
4744  *
4745  * Appends @string to the list of strings stored in @combo_box. Note that
4746  * you can only use this function with combo boxes constructed with
4747  * gtk_combo_box_new_text().
4748  *
4749  * Since: 2.4
4750  */
4751 void
4752 gtk_combo_box_append_text (GtkComboBox *combo_box,
4753                            const gchar *text)
4754 {
4755   GtkTreeIter iter;
4756   GtkListStore *store;
4757
4758   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4759   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4760   g_return_if_fail (text != NULL);
4761
4762   store = GTK_LIST_STORE (combo_box->priv->model);
4763
4764   gtk_list_store_append (store, &iter);
4765   gtk_list_store_set (store, &iter, 0, text, -1);
4766 }
4767
4768 /**
4769  * gtk_combo_box_insert_text:
4770  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text().
4771  * @position: An index to insert @text.
4772  * @text: A string.
4773  *
4774  * Inserts @string at @position in the list of strings stored in @combo_box.
4775  * Note that you can only use this function with combo boxes constructed
4776  * with gtk_combo_box_new_text().
4777  *
4778  * Since: 2.4
4779  */
4780 void
4781 gtk_combo_box_insert_text (GtkComboBox *combo_box,
4782                            gint         position,
4783                            const gchar *text)
4784 {
4785   GtkTreeIter iter;
4786   GtkListStore *store;
4787
4788   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4789   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4790   g_return_if_fail (position >= 0);
4791   g_return_if_fail (text != NULL);
4792
4793   store = GTK_LIST_STORE (combo_box->priv->model);
4794
4795   gtk_list_store_insert (store, &iter, position);
4796   gtk_list_store_set (store, &iter, 0, text, -1);
4797 }
4798
4799 /**
4800  * gtk_combo_box_prepend_text:
4801  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4802  * @text: A string.
4803  *
4804  * Prepends @string to the list of strings stored in @combo_box. Note that
4805  * you can only use this function with combo boxes constructed with
4806  * gtk_combo_box_new_text().
4807  *
4808  * Since: 2.4
4809  */
4810 void
4811 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
4812                             const gchar *text)
4813 {
4814   GtkTreeIter iter;
4815   GtkListStore *store;
4816
4817   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4818   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4819   g_return_if_fail (text != NULL);
4820
4821   store = GTK_LIST_STORE (combo_box->priv->model);
4822
4823   gtk_list_store_prepend (store, &iter);
4824   gtk_list_store_set (store, &iter, 0, text, -1);
4825 }
4826
4827 /**
4828  * gtk_combo_box_remove_text:
4829  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4830  * @position: Index of the item to remove.
4831  *
4832  * Removes the string at @position from @combo_box. Note that you can only use
4833  * this function with combo boxes constructed with gtk_combo_box_new_text().
4834  *
4835  * Since: 2.4
4836  */
4837 void
4838 gtk_combo_box_remove_text (GtkComboBox *combo_box,
4839                            gint         position)
4840 {
4841   GtkTreeIter iter;
4842   GtkListStore *store;
4843
4844   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4845   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
4846   g_return_if_fail (position >= 0);
4847
4848   store = GTK_LIST_STORE (combo_box->priv->model);
4849
4850   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
4851                                      NULL, position))
4852     gtk_list_store_remove (store, &iter);
4853 }
4854
4855 /**
4856  * gtk_combo_box_get_active_text:
4857  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text().
4858  *
4859  * Returns the currently active string in @combo_box or %NULL if none
4860  * is selected.  Note that you can only use this function with combo
4861  * boxes constructed with gtk_combo_box_new_text() and with 
4862  * #GtkComboBoxEntry<!-- -->s.
4863  *
4864  * Returns: a newly allocated string containing the currently active text.
4865  *
4866  * Since: 2.6
4867  */
4868 gchar *
4869 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
4870 {
4871   GtkComboBoxClass *class;
4872
4873   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4874
4875   class = GTK_COMBO_BOX_GET_CLASS (combo_box);
4876
4877   if (class->get_active_text)
4878     return (* class->get_active_text) (combo_box);
4879
4880   return NULL;
4881 }
4882
4883 static gchar *
4884 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
4885 {
4886   GtkTreeIter iter;
4887   gchar *text = NULL;
4888
4889   g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
4890
4891   if (gtk_combo_box_get_active_iter (combo_box, &iter))
4892     gtk_tree_model_get (combo_box->priv->model, &iter, 
4893                         0, &text, -1);
4894
4895   return text;
4896 }
4897
4898 static gboolean
4899 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
4900                                  gboolean   group_cycling)
4901 {
4902   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4903
4904   gtk_widget_grab_focus (combo_box->priv->button);
4905
4906   return TRUE;
4907 }
4908
4909 static void
4910 gtk_combo_box_grab_focus (GtkWidget *widget)
4911 {
4912   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
4913
4914   gtk_widget_grab_focus (combo_box->priv->button);
4915 }
4916
4917 static void
4918 gtk_combo_box_destroy (GtkObject *object)
4919 {
4920   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4921
4922   if (combo_box->priv->popup_idle_id > 0)
4923     {
4924       g_source_remove (combo_box->priv->popup_idle_id);
4925       combo_box->priv->popup_idle_id = 0;
4926     }
4927
4928   gtk_combo_box_popdown (combo_box);
4929
4930   if (combo_box->priv->row_separator_destroy)
4931     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
4932
4933   combo_box->priv->row_separator_func = NULL;
4934   combo_box->priv->row_separator_data = NULL;
4935   combo_box->priv->row_separator_destroy = NULL;
4936
4937   combo_box->priv->destroying = 1;
4938
4939   GTK_OBJECT_CLASS (parent_class)->destroy (object);
4940   combo_box->priv->cell_view = NULL;
4941
4942   combo_box->priv->destroying = 0;
4943 }
4944
4945 static void
4946 gtk_combo_box_finalize (GObject *object)
4947 {
4948   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
4949   GSList *i;
4950   
4951   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4952     {
4953       gtk_combo_box_menu_destroy (combo_box);
4954       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
4955       combo_box->priv->popup_widget = NULL;
4956     }
4957   
4958   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
4959     gtk_combo_box_list_destroy (combo_box);
4960
4961   if (combo_box->priv->popup_window)
4962     gtk_widget_destroy (combo_box->priv->popup_window);
4963
4964   gtk_combo_box_unset_model (combo_box);
4965
4966   for (i = combo_box->priv->cells; i; i = i->next)
4967     {
4968       ComboCellInfo *info = (ComboCellInfo *)i->data;
4969       GSList *list = info->attributes;
4970
4971       if (info->destroy)
4972         info->destroy (info->func_data);
4973
4974       while (list && list->next)
4975         {
4976           g_free (list->data);
4977           list = list->next->next;
4978         }
4979       g_slist_free (info->attributes);
4980
4981       g_object_unref (info->cell);
4982       g_free (info);
4983     }
4984    g_slist_free (combo_box->priv->cells);
4985
4986    G_OBJECT_CLASS (parent_class)->finalize (object);
4987 }
4988
4989 static gboolean
4990 gtk_cell_editable_key_press (GtkWidget   *widget,
4991                              GdkEventKey *event,
4992                              gpointer     data)
4993 {
4994   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4995
4996   if (event->keyval == GDK_Escape)
4997     {
4998       combo_box->priv->editing_canceled = TRUE;
4999
5000       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5001       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5002       
5003       return TRUE;
5004     }
5005   else if (event->keyval == GDK_Return)
5006     {
5007       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5008       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5009       
5010       return TRUE;
5011     }
5012
5013   return FALSE;
5014 }
5015
5016 static gboolean
5017 popdown_idle (gpointer data)
5018 {
5019   GtkComboBox *combo_box;
5020
5021   GDK_THREADS_ENTER ();
5022
5023   combo_box = GTK_COMBO_BOX (data);
5024   
5025   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5026   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5027
5028   g_object_unref (combo_box);
5029
5030   GDK_THREADS_LEAVE ();
5031
5032   return FALSE;
5033 }
5034
5035 static void
5036 popdown_handler (GtkWidget *widget,
5037                  gpointer   data)
5038 {
5039   g_idle_add (popdown_idle, g_object_ref (data));
5040 }
5041
5042 static gboolean
5043 popup_idle (gpointer data)
5044 {
5045   GtkComboBox *combo_box;
5046
5047   GDK_THREADS_ENTER ();
5048
5049   combo_box = GTK_COMBO_BOX (data);
5050
5051   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5052       combo_box->priv->cell_view)
5053     g_signal_connect_object (combo_box->priv->popup_widget,
5054                              "unmap", G_CALLBACK (popdown_handler),
5055                              combo_box, 0);
5056   
5057   /* we unset this if a menu item is activated */
5058   combo_box->priv->editing_canceled = TRUE;
5059   gtk_combo_box_popup (combo_box);
5060
5061   combo_box->priv->popup_idle_id = 0;
5062
5063   GDK_THREADS_LEAVE ();
5064
5065   return FALSE;
5066 }
5067
5068 static void
5069 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5070                              GdkEvent        *event)
5071 {
5072   GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5073
5074   combo_box->priv->is_cell_renderer = TRUE;
5075
5076   if (combo_box->priv->cell_view)
5077     {
5078       g_signal_connect_object (combo_box->priv->button, "key_press_event",
5079                                G_CALLBACK (gtk_cell_editable_key_press), 
5080                                cell_editable, 0);  
5081
5082       gtk_widget_grab_focus (combo_box->priv->button);
5083     }
5084   else
5085     {
5086       g_signal_connect_object (GTK_BIN (combo_box)->child, "key_press_event",
5087                                G_CALLBACK (gtk_cell_editable_key_press), 
5088                                cell_editable, 0);  
5089
5090       gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5091       GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5092     }
5093
5094   /* we do the immediate popup only for the optionmenu-like 
5095    * appearance 
5096    */  
5097   if (combo_box->priv->is_cell_renderer && 
5098       combo_box->priv->cell_view && !combo_box->priv->tree_view)
5099     combo_box->priv->popup_idle_id = g_idle_add (popup_idle, combo_box);
5100 }
5101
5102
5103 /**
5104  * gtk_combo_box_get_add_tearoffs:
5105  * @combo_box: a #GtkComboBox
5106  * 
5107  * Gets the current value of the :add-tearoffs property.
5108  * 
5109  * Return value: the current value of the :add-tearoffs property.
5110  **/
5111 gboolean
5112 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5113 {
5114   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5115
5116   return combo_box->priv->add_tearoffs;
5117 }
5118
5119 /**
5120  * gtk_combo_box_set_add_tearoffs:
5121  * @combo_box: a #GtkComboBox 
5122  * @add_tearoffs: %TRUE to add tearoff menu items
5123  *  
5124  * Sets whether the popup menu should have a tearoff 
5125  * menu item.
5126  *
5127  * Since: 2.6
5128  **/
5129 void
5130 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5131                                 gboolean     add_tearoffs)
5132 {
5133   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5134
5135   add_tearoffs = add_tearoffs != FALSE;
5136
5137   if (combo_box->priv->add_tearoffs != add_tearoffs)
5138     {
5139       combo_box->priv->add_tearoffs = add_tearoffs;
5140       gtk_combo_box_check_appearance (combo_box);
5141       gtk_combo_box_relayout (combo_box);
5142       g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5143     }
5144 }
5145
5146 /**
5147  * gtk_combo_box_get_title:
5148  * @combo_box: a #GtkComboBox
5149  *
5150  * Gets the current title of the menu in tearoff mode. See
5151  * gtk_combo_box_set_add_tearoffs().
5152  *
5153  * Returns: the menu's title in tearoff mode. This is an internal copy of the
5154  * string which must not be freed.
5155  *
5156  * Since: 2.10
5157  */
5158 G_CONST_RETURN gchar*
5159 gtk_combo_box_get_title (GtkComboBox *combo_box)
5160 {
5161   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5162   
5163   return combo_box->priv->tearoff_title;
5164 }
5165
5166 static void
5167 gtk_combo_box_update_title (GtkComboBox *combo_box)
5168 {
5169   gtk_combo_box_check_appearance (combo_box);
5170   
5171   if (combo_box->priv->popup_widget && 
5172       GTK_IS_MENU (combo_box->priv->popup_widget))
5173     gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget), 
5174                         combo_box->priv->tearoff_title);
5175 }
5176
5177 /**
5178  * gtk_combo_box_set_title:
5179  * @combo_box: a #GtkComboBox 
5180  * @title: a title for the menu in tearoff mode.
5181  *  
5182  * Sets the menu's title in tearoff mode.
5183  *
5184  * Since: 2.10
5185  */
5186 void
5187 gtk_combo_box_set_title (GtkComboBox *combo_box,
5188                          const gchar *title)
5189 {
5190   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5191
5192   if (strcmp (title ? title : "", 
5193               combo_box->priv->tearoff_title ? combo_box->priv->tearoff_title : "") != 0)
5194     {
5195       g_free (combo_box->priv->tearoff_title);
5196       combo_box->priv->tearoff_title = g_strdup (title);
5197
5198       gtk_combo_box_update_title (combo_box);
5199
5200       g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5201     }
5202 }
5203
5204 gboolean
5205 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5206 {
5207   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5208
5209   return combo_box->priv->editing_canceled;
5210 }
5211
5212 /**
5213  * gtk_combo_box_get_popup_accessible:
5214  * @combo_box: a #GtkComboBox
5215  *
5216  * Gets the accessible object corresponding to the combo box's popup.
5217  *
5218  * This function is mostly intended for use by accessibility technologies;
5219  * applications should have little use for it.
5220  *
5221  * Returns: the accessible object corresponding to the combo box's popup.
5222  *
5223  * Since: 2.6
5224  **/
5225 AtkObject*
5226 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5227 {
5228   AtkObject *atk_obj;
5229
5230   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5231
5232   if (combo_box->priv->popup_widget)
5233     {
5234       atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5235       return atk_obj;
5236     }
5237
5238   return NULL;
5239 }
5240
5241 /**
5242  * gtk_combo_box_get_row_separator_func:
5243  * @combo_box: a #GtkComboBox
5244  * 
5245  * Returns the current row separator function.
5246  * 
5247  * Return value: the current row separator function.
5248  *
5249  * Since: 2.6
5250  **/
5251 GtkTreeViewRowSeparatorFunc 
5252 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5253 {
5254   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5255
5256   return combo_box->priv->row_separator_func;
5257 }
5258
5259 /**
5260  * gtk_combo_box_set_row_separator_func:
5261  * @combo_box: a #GtkComboBox
5262  * @func: a #GtkTreeViewRowSeparatorFunc
5263  * @data: user data to pass to @func, or %NULL
5264  * @destroy: destroy notifier for @data, or %NULL
5265  * 
5266  * Sets the row separator function, which is used to determine
5267  * whether a row should be drawn as a separator. If the row separator
5268  * function is %NULL, no separators are drawn. This is the default value.
5269  *
5270  * Since: 2.6
5271  **/
5272 void
5273 gtk_combo_box_set_row_separator_func (GtkComboBox                 *combo_box,
5274                                       GtkTreeViewRowSeparatorFunc  func,
5275                                       gpointer                     data,
5276                                       GtkDestroyNotify             destroy)
5277 {
5278   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5279
5280   if (combo_box->priv->row_separator_destroy)
5281     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5282
5283   combo_box->priv->row_separator_func = func;
5284   combo_box->priv->row_separator_data = data;
5285   combo_box->priv->row_separator_destroy = destroy;
5286
5287   if (combo_box->priv->tree_view)
5288     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
5289                                           func, data, NULL);
5290
5291   gtk_combo_box_relayout (combo_box);
5292
5293   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5294 }
5295
5296
5297 /**
5298  * gtk_combo_box_set_focus_on_click:
5299  * @combo: a #GtkComboBox
5300  * @focus_on_click: whether the combo box grabs focus when clicked 
5301  *    with the mouse
5302  * 
5303  * Sets whether the combo box will grab focus when it is clicked with 
5304  * the mouse. Making mouse clicks not grab focus is useful in places 
5305  * like toolbars where you don't want the keyboard focus removed from 
5306  * the main area of the application.
5307  *
5308  * Since: 2.6
5309  **/
5310 void
5311 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5312                                   gboolean     focus_on_click)
5313 {
5314   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5315   
5316   focus_on_click = focus_on_click != FALSE;
5317
5318   if (combo_box->priv->focus_on_click != focus_on_click)
5319     {
5320       combo_box->priv->focus_on_click = focus_on_click;
5321
5322       if (combo_box->priv->button)
5323         gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5324                                        focus_on_click);
5325       
5326       g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5327     }
5328 }
5329
5330 /**
5331  * gtk_combo_box_get_focus_on_click:
5332  * @combo: a #GtkComboBox
5333  * 
5334  * Returns whether the combo box grabs focus when it is clicked 
5335  * with the mouse. See gtk_combo_box_set_focus_on_click().
5336  *
5337  * Return value: %TRUE if the combo box grabs focus when it is 
5338  *     clicked with the mouse.
5339  *
5340  * Since: 2.6
5341  **/
5342 gboolean
5343 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5344 {
5345   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5346   
5347   return combo_box->priv->focus_on_click;
5348 }
5349
5350
5351 #define __GTK_COMBO_BOX_C__
5352 #include "gtkaliasdef.c"