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