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