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