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