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