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