]> Pileus Git - ~andy/gtk/blob - gtk/gtkcombobox.c
Take thickness into account in the size allocation of the child widgets in
[~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.x += shadow_width;
2324       child.y += shadow_height;
2325       child.width -= shadow_width * 2;
2326       child.height -= shadow_height * 2;
2327       
2328       child.width = MAX (1, child.width);
2329       child.height = MAX (1, child.height);
2330       
2331       gtk_widget_size_allocate (GTK_BIN (combo_box)->child, &child);
2332     }
2333 }
2334
2335 #undef GTK_COMBO_BOX_ALLOCATE_BUTTON
2336
2337 static void
2338 gtk_combo_box_unset_model (GtkComboBox *combo_box)
2339 {
2340   GtkComboBoxPrivate *priv = combo_box->priv;
2341
2342   if (priv->model)
2343     {
2344       g_signal_handler_disconnect (priv->model,
2345                                    priv->inserted_id);
2346       g_signal_handler_disconnect (priv->model,
2347                                    priv->deleted_id);
2348       g_signal_handler_disconnect (priv->model,
2349                                    priv->reordered_id);
2350       g_signal_handler_disconnect (priv->model,
2351                                    priv->changed_id);
2352     }
2353
2354   /* menu mode */
2355   if (!priv->tree_view)
2356     {
2357       if (priv->popup_widget)
2358         gtk_container_foreach (GTK_CONTAINER (priv->popup_widget),
2359                                (GtkCallback)gtk_widget_destroy, NULL);
2360     }
2361
2362   if (priv->model)
2363     {
2364       g_object_unref (priv->model);
2365       priv->model = NULL;
2366     }
2367
2368   if (priv->active_row)
2369     {
2370       gtk_tree_row_reference_free (priv->active_row);
2371       priv->active_row = NULL;
2372     }
2373
2374   if (priv->cell_view)
2375     gtk_cell_view_set_model (GTK_CELL_VIEW (priv->cell_view), NULL);
2376 }
2377
2378 static void
2379 gtk_combo_box_forall (GtkContainer *container,
2380                       gboolean      include_internals,
2381                       GtkCallback   callback,
2382                       gpointer      callback_data)
2383 {
2384   GtkComboBox *combo_box = GTK_COMBO_BOX (container);
2385   GtkComboBoxPrivate *priv = combo_box->priv;
2386
2387   if (include_internals)
2388     {
2389       if (priv->button)
2390         (* callback) (priv->button, callback_data);
2391       if (priv->cell_view_frame)
2392         (* callback) (priv->cell_view_frame, callback_data);
2393     }
2394
2395   if (GTK_BIN (container)->child)
2396     (* callback) (GTK_BIN (container)->child, callback_data);
2397 }
2398
2399 static void 
2400 gtk_combo_box_child_show (GtkWidget *widget,
2401                           GtkComboBox *combo_box)
2402 {
2403   GtkComboBoxPrivate *priv = combo_box->priv;
2404
2405   priv->popup_shown = TRUE;
2406   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2407 }
2408
2409 static void 
2410 gtk_combo_box_child_hide (GtkWidget *widget,
2411                           GtkComboBox *combo_box)
2412 {
2413   GtkComboBoxPrivate *priv = combo_box->priv;
2414
2415   priv->popup_shown = FALSE;
2416   g_object_notify (G_OBJECT (combo_box), "popup-shown");
2417 }
2418
2419 static gboolean
2420 gtk_combo_box_expose_event (GtkWidget      *widget,
2421                             GdkEventExpose *event)
2422 {
2423   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2424   GtkComboBoxPrivate *priv = combo_box->priv;
2425
2426   if (GTK_WIDGET_DRAWABLE (widget) &&
2427       GTK_SHADOW_NONE != priv->shadow_type)
2428     {
2429       gtk_paint_shadow (widget->style, widget->window,
2430                         GTK_STATE_NORMAL, priv->shadow_type,
2431                         NULL, widget, "combobox",
2432                         widget->allocation.x, widget->allocation.y,
2433                         widget->allocation.width, widget->allocation.height);
2434     }
2435
2436   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2437                                   priv->button, event);
2438
2439   if (priv->tree_view && priv->cell_view_frame)
2440     {
2441       gtk_container_propagate_expose (GTK_CONTAINER (widget),
2442                                       priv->cell_view_frame, event);
2443     }
2444
2445   gtk_container_propagate_expose (GTK_CONTAINER (widget),
2446                                   GTK_BIN (widget)->child, event);
2447
2448   return FALSE;
2449 }
2450
2451 typedef struct {
2452   GtkComboBox *combo;
2453   GtkTreePath *path;
2454   GtkTreeIter iter;
2455   gboolean found;
2456   gboolean set;
2457   gboolean visible;
2458 } SearchData;
2459
2460 static gboolean
2461 path_visible (GtkTreeView *view,
2462               GtkTreePath *path)
2463 {
2464   GtkRBTree *tree;
2465   GtkRBNode *node;
2466   
2467   /* Note that we rely on the fact that collapsed rows don't have nodes 
2468    */
2469   return _gtk_tree_view_find_node (view, path, &tree, &node);
2470 }
2471
2472 static gboolean
2473 tree_next_func (GtkTreeModel *model,
2474                 GtkTreePath  *path,
2475                 GtkTreeIter  *iter,
2476                 gpointer      data)
2477 {
2478   SearchData *search_data = (SearchData *)data;
2479
2480   if (search_data->found) 
2481     {
2482       if (!tree_column_row_is_sensitive (search_data->combo, iter))
2483         return FALSE;
2484       
2485       if (search_data->visible &&
2486           !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2487         return FALSE;
2488
2489       search_data->set = TRUE;
2490       search_data->iter = *iter;
2491
2492       return TRUE;
2493     }
2494  
2495   if (gtk_tree_path_compare (path, search_data->path) == 0)
2496     search_data->found = TRUE;
2497   
2498   return FALSE;
2499 }
2500
2501 static gboolean
2502 tree_next (GtkComboBox  *combo,
2503            GtkTreeModel *model,
2504            GtkTreeIter  *iter,
2505            GtkTreeIter  *next,
2506            gboolean      visible)
2507 {
2508   SearchData search_data;
2509
2510   search_data.combo = combo;
2511   search_data.path = gtk_tree_model_get_path (model, iter);
2512   search_data.visible = visible;
2513   search_data.found = FALSE;
2514   search_data.set = FALSE;
2515
2516   gtk_tree_model_foreach (model, tree_next_func, &search_data);
2517   
2518   *next = search_data.iter;
2519
2520   gtk_tree_path_free (search_data.path);
2521
2522   return search_data.set;
2523 }
2524
2525 static gboolean
2526 tree_prev_func (GtkTreeModel *model,
2527                 GtkTreePath  *path,
2528                 GtkTreeIter  *iter,
2529                 gpointer      data)
2530 {
2531   SearchData *search_data = (SearchData *)data;
2532
2533   if (gtk_tree_path_compare (path, search_data->path) == 0)
2534     {
2535       search_data->found = TRUE;
2536       return TRUE;
2537     }
2538   
2539   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2540     return FALSE;
2541       
2542   if (search_data->visible &&
2543       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2544     return FALSE; 
2545   
2546   search_data->set = TRUE;
2547   search_data->iter = *iter;
2548   
2549   return FALSE; 
2550 }
2551
2552 static gboolean
2553 tree_prev (GtkComboBox  *combo,
2554            GtkTreeModel *model,
2555            GtkTreeIter  *iter,
2556            GtkTreeIter  *prev,
2557            gboolean      visible)
2558 {
2559   SearchData search_data;
2560
2561   search_data.combo = combo;
2562   search_data.path = gtk_tree_model_get_path (model, iter);
2563   search_data.visible = visible;
2564   search_data.found = FALSE;
2565   search_data.set = FALSE;
2566
2567   gtk_tree_model_foreach (model, tree_prev_func, &search_data);
2568   
2569   *prev = search_data.iter;
2570
2571   gtk_tree_path_free (search_data.path);
2572
2573   return search_data.set;
2574 }
2575
2576 static gboolean
2577 tree_last_func (GtkTreeModel *model,
2578                 GtkTreePath  *path,
2579                 GtkTreeIter  *iter,
2580                 gpointer      data)
2581 {
2582   SearchData *search_data = (SearchData *)data;
2583
2584   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2585     return FALSE;
2586       
2587   /* Note that we rely on the fact that collapsed rows don't have nodes 
2588    */
2589   if (search_data->visible &&
2590       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2591     return FALSE; 
2592   
2593   search_data->set = TRUE;
2594   search_data->iter = *iter;
2595   
2596   return FALSE; 
2597 }
2598
2599 static gboolean
2600 tree_last (GtkComboBox  *combo,
2601            GtkTreeModel *model,
2602            GtkTreeIter  *last,
2603            gboolean      visible)
2604 {
2605   SearchData search_data;
2606
2607   search_data.combo = combo;
2608   search_data.visible = visible;
2609   search_data.set = FALSE;
2610
2611   gtk_tree_model_foreach (model, tree_last_func, &search_data);
2612   
2613   *last = search_data.iter;
2614
2615   return search_data.set;  
2616 }
2617
2618
2619 static gboolean
2620 tree_first_func (GtkTreeModel *model,
2621                  GtkTreePath  *path,
2622                  GtkTreeIter  *iter,
2623                  gpointer      data)
2624 {
2625   SearchData *search_data = (SearchData *)data;
2626
2627   if (!tree_column_row_is_sensitive (search_data->combo, iter))
2628     return FALSE;
2629   
2630   if (search_data->visible &&
2631       !path_visible (GTK_TREE_VIEW (search_data->combo->priv->tree_view), path))
2632     return FALSE;
2633   
2634   search_data->set = TRUE;
2635   search_data->iter = *iter;
2636   
2637   return TRUE;
2638 }
2639
2640 static gboolean
2641 tree_first (GtkComboBox  *combo,
2642             GtkTreeModel *model,
2643             GtkTreeIter  *first,
2644             gboolean      visible)
2645 {
2646   SearchData search_data;
2647   
2648   search_data.combo = combo;
2649   search_data.visible = visible;
2650   search_data.set = FALSE;
2651
2652   gtk_tree_model_foreach (model, tree_first_func, &search_data);
2653   
2654   *first = search_data.iter;
2655
2656   return search_data.set;  
2657 }
2658
2659 static gboolean
2660 gtk_combo_box_scroll_event (GtkWidget          *widget,
2661                             GdkEventScroll     *event)
2662 {
2663   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
2664   gboolean found;
2665   GtkTreeIter iter;
2666   GtkTreeIter new_iter;
2667
2668   if (!gtk_combo_box_get_active_iter (combo_box, &iter))
2669     return TRUE;
2670   
2671   if (event->direction == GDK_SCROLL_UP)
2672     found = tree_prev (combo_box, combo_box->priv->model, 
2673                        &iter, &new_iter, FALSE);
2674   else
2675     found = tree_next (combo_box, combo_box->priv->model, 
2676                        &iter, &new_iter, FALSE);
2677   
2678   if (found)
2679     gtk_combo_box_set_active_iter (combo_box, &new_iter);
2680
2681   return TRUE;
2682 }
2683
2684 /*
2685  * menu style
2686  */
2687
2688 static void
2689 gtk_combo_box_sync_cells (GtkComboBox   *combo_box,
2690                           GtkCellLayout *cell_layout)
2691 {
2692   GtkComboBoxPrivate *priv = combo_box->priv;
2693   GSList *k;
2694
2695   for (k = priv->cells; k; k = k->next)
2696     {
2697       GSList *j;
2698       ComboCellInfo *info = (ComboCellInfo *)k->data;
2699
2700       if (info->pack == GTK_PACK_START)
2701         gtk_cell_layout_pack_start (cell_layout,
2702                                     info->cell, info->expand);
2703       else if (info->pack == GTK_PACK_END)
2704         gtk_cell_layout_pack_end (cell_layout,
2705                                   info->cell, info->expand);
2706
2707       gtk_cell_layout_set_cell_data_func (cell_layout,
2708                                           info->cell,
2709                                           combo_cell_data_func, info, NULL);
2710
2711       for (j = info->attributes; j; j = j->next->next)
2712         {
2713           gtk_cell_layout_add_attribute (cell_layout,
2714                                          info->cell,
2715                                          j->data,
2716                                          GPOINTER_TO_INT (j->next->data));
2717         }
2718     }
2719 }
2720
2721 static void
2722 gtk_combo_box_menu_setup (GtkComboBox *combo_box,
2723                           gboolean     add_children)
2724 {
2725   GtkComboBoxPrivate *priv = combo_box->priv;
2726   GtkWidget *menu;
2727
2728   if (priv->cell_view)
2729     {
2730       priv->button = gtk_toggle_button_new ();
2731       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2732                                      priv->focus_on_click);
2733
2734       g_signal_connect (priv->button, "toggled",
2735                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2736       gtk_widget_set_parent (priv->button,
2737                              GTK_BIN (combo_box)->child->parent);
2738
2739       priv->box = gtk_hbox_new (FALSE, 0);
2740       gtk_container_add (GTK_CONTAINER (priv->button), priv->box);
2741
2742       priv->separator = gtk_vseparator_new ();
2743       gtk_container_add (GTK_CONTAINER (priv->box), priv->separator);
2744
2745       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2746       gtk_container_add (GTK_CONTAINER (priv->box), priv->arrow);
2747
2748       gtk_widget_show_all (priv->button);
2749     }
2750   else
2751     {
2752       priv->button = gtk_toggle_button_new ();
2753       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button),
2754                                      priv->focus_on_click);
2755
2756       g_signal_connect (priv->button, "toggled",
2757                         G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
2758       gtk_widget_set_parent (priv->button,
2759                              GTK_BIN (combo_box)->child->parent);
2760
2761       priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2762       gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
2763       gtk_widget_show_all (priv->button);
2764     }
2765
2766   g_signal_connect (priv->button, "button_press_event",
2767                     G_CALLBACK (gtk_combo_box_menu_button_press),
2768                     combo_box);
2769   g_signal_connect (priv->button, "state_changed",
2770                     G_CALLBACK (gtk_combo_box_button_state_changed), 
2771                     combo_box);
2772
2773   /* create our funky menu */
2774   menu = gtk_menu_new ();
2775   gtk_widget_set_name (menu, "gtk-combobox-popup-menu");
2776   
2777   g_signal_connect (menu, "key_press_event",
2778                     G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
2779   gtk_combo_box_set_popup_widget (combo_box, menu);
2780
2781   /* add items */
2782   if (add_children)
2783     gtk_combo_box_menu_fill (combo_box);
2784
2785   /* the column is needed in tree_column_row_is_sensitive() */
2786   priv->column = gtk_tree_view_column_new ();
2787   g_object_ref_sink (priv->column);
2788   gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (priv->column));
2789
2790   gtk_combo_box_update_title (combo_box);
2791 }
2792
2793 static void
2794 gtk_combo_box_menu_fill (GtkComboBox *combo_box)
2795 {
2796   GtkComboBoxPrivate *priv = combo_box->priv;
2797   GtkWidget *menu;
2798
2799   if (!priv->model)
2800     return;
2801
2802   menu = priv->popup_widget;
2803
2804   if (priv->add_tearoffs)
2805     {
2806       GtkWidget *tearoff = gtk_tearoff_menu_item_new ();
2807
2808       gtk_widget_show (tearoff);
2809       
2810       if (priv->wrap_width)
2811         gtk_menu_attach (GTK_MENU (menu), tearoff, 0, priv->wrap_width, 0, 1);
2812       else
2813         gtk_menu_shell_append (GTK_MENU_SHELL (menu), tearoff);
2814     }
2815   
2816   gtk_combo_box_menu_fill_level (combo_box, menu, NULL);
2817 }
2818
2819 static GtkWidget *
2820 gtk_cell_view_menu_item_new (GtkComboBox  *combo_box,
2821                              GtkTreeModel *model,
2822                              GtkTreeIter  *iter)
2823 {
2824   GtkWidget *cell_view; 
2825   GtkWidget *item;
2826   GtkTreePath *path;
2827   GtkRequisition req;
2828
2829   cell_view = gtk_cell_view_new ();
2830   item = gtk_menu_item_new ();
2831   gtk_container_add (GTK_CONTAINER (item), cell_view);
2832
2833   gtk_cell_view_set_model (GTK_CELL_VIEW (cell_view), model);     
2834   path = gtk_tree_model_get_path (model, iter);
2835   gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (cell_view), path);
2836   gtk_tree_path_free (path);
2837
2838   gtk_combo_box_sync_cells (combo_box, GTK_CELL_LAYOUT (cell_view));
2839   gtk_widget_size_request (cell_view, &req);
2840   gtk_widget_show (cell_view);
2841   
2842   return item;
2843 }
2844  
2845 static void
2846 gtk_combo_box_menu_fill_level (GtkComboBox *combo_box,
2847                                GtkWidget   *menu,
2848                                GtkTreeIter *parent)
2849 {
2850   GtkComboBoxPrivate *priv = combo_box->priv;
2851   GtkTreeModel *model = priv->model;
2852   GtkWidget *item, *submenu, *subitem, *separator;
2853   GtkTreeIter iter;
2854   gboolean is_separator;
2855   gint i, n_children;
2856   GtkWidget *last;
2857   GtkTreePath *path;
2858   
2859   n_children = gtk_tree_model_iter_n_children (model, parent);
2860   
2861   last = NULL;
2862   for (i = 0; i < n_children; i++)
2863     {
2864       gtk_tree_model_iter_nth_child (model, &iter, parent, i);
2865
2866       if (priv->row_separator_func)
2867         is_separator = (*priv->row_separator_func) (priv->model, &iter,
2868                                                     priv->row_separator_data);
2869       else
2870         is_separator = FALSE;
2871       
2872       if (is_separator)
2873         {
2874           item = gtk_separator_menu_item_new ();
2875           path = gtk_tree_model_get_path (model, &iter);
2876           g_object_set_data_full (G_OBJECT (item),
2877                                   I_("gtk-combo-box-item-path"),
2878                                   gtk_tree_row_reference_new (model, path),
2879                                   (GDestroyNotify)gtk_tree_row_reference_free);
2880           gtk_tree_path_free (path);
2881         }
2882       else
2883         {
2884           item = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2885           if (gtk_tree_model_iter_has_child (model, &iter))
2886             {
2887               submenu = gtk_menu_new ();
2888               gtk_widget_show (submenu);
2889               gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
2890               
2891               /* Ugly - since menus can only activate leafs, we have to
2892                * duplicate the item inside the submenu.
2893                */
2894               subitem = gtk_cell_view_menu_item_new (combo_box, model, &iter);
2895               separator = gtk_separator_menu_item_new ();
2896               gtk_widget_show (subitem);
2897               gtk_widget_show (separator);
2898               g_signal_connect (subitem, "activate",
2899                                 G_CALLBACK (gtk_combo_box_menu_item_activate),
2900                                 combo_box);
2901               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), subitem);
2902               gtk_menu_shell_append (GTK_MENU_SHELL (submenu), separator);
2903               
2904               gtk_combo_box_menu_fill_level (combo_box, submenu, &iter);
2905             }
2906           g_signal_connect (item, "activate",
2907                             G_CALLBACK (gtk_combo_box_menu_item_activate),
2908                             combo_box);
2909         }
2910       
2911       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2912       if (priv->wrap_width && menu == priv->popup_widget)
2913         gtk_combo_box_relayout_item (combo_box, item, &iter, last);
2914       gtk_widget_show (item);
2915       
2916       last = item;
2917     }
2918 }
2919
2920 static void
2921 gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
2922 {
2923   GtkComboBoxPrivate *priv = combo_box->priv;
2924
2925   g_signal_handlers_disconnect_matched (priv->button,
2926                                         G_SIGNAL_MATCH_DATA,
2927                                         0, 0, NULL,
2928                                         gtk_combo_box_menu_button_press, NULL);
2929   g_signal_handlers_disconnect_matched (priv->button,
2930                                         G_SIGNAL_MATCH_DATA,
2931                                         0, 0, NULL,
2932                                         gtk_combo_box_button_state_changed, combo_box);
2933
2934   /* unparent will remove our latest ref */
2935   gtk_widget_unparent (priv->button);
2936   
2937   priv->box = NULL;
2938   priv->button = NULL;
2939   priv->arrow = NULL;
2940   priv->separator = NULL;
2941
2942   g_object_unref (priv->column);
2943   priv->column = NULL;
2944
2945   /* changing the popup window will unref the menu and the children */
2946 }
2947
2948 /*
2949  * grid
2950  */
2951
2952 static gboolean
2953 menu_occupied (GtkMenu   *menu,
2954                guint      left_attach,
2955                guint      right_attach,
2956                guint      top_attach,
2957                guint      bottom_attach)
2958 {
2959   GList *i;
2960
2961   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
2962     {
2963       guint l, r, b, t;
2964
2965       gtk_container_child_get (GTK_CONTAINER (menu), 
2966                                i->data,
2967                                "left-attach", &l,
2968                                "right-attach", &r,
2969                                "bottom-attach", &b,
2970                                "top-attach", &t,
2971                                NULL);
2972
2973       /* look if this item intersects with the given coordinates */
2974       if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
2975         return TRUE;
2976     }
2977
2978   return FALSE;
2979 }
2980
2981 static void
2982 gtk_combo_box_relayout_item (GtkComboBox *combo_box,
2983                              GtkWidget   *item,
2984                              GtkTreeIter *iter,
2985                              GtkWidget   *last)
2986 {
2987   GtkComboBoxPrivate *priv = combo_box->priv;
2988   gint current_col = 0, current_row = 0;
2989   gint rows = 1, cols = 1;
2990   GtkWidget *menu = priv->popup_widget;
2991
2992   if (!GTK_IS_MENU_SHELL (menu))
2993     return;
2994   
2995   if (priv->col_column == -1 &&
2996       priv->row_column == -1 &&
2997       last)
2998     {
2999       gtk_container_child_get (GTK_CONTAINER (menu), 
3000                                last,
3001                                "right-attach", &current_col,
3002                                "top-attach", &current_row,
3003                                NULL);
3004       if (current_col + cols > priv->wrap_width)
3005         {
3006           current_col = 0;
3007           current_row++;
3008         }
3009     }
3010   else
3011     {
3012       if (priv->col_column != -1)
3013         gtk_tree_model_get (priv->model, iter,
3014                             priv->col_column, &cols,
3015                             -1);
3016       if (priv->row_column != -1)
3017         gtk_tree_model_get (priv->model, iter,
3018                             priv->row_column, &rows,
3019                             -1);
3020
3021       while (1)
3022         {
3023           if (current_col + cols > priv->wrap_width)
3024             {
3025               current_col = 0;
3026               current_row++;
3027             }
3028           
3029           if (!menu_occupied (GTK_MENU (menu), 
3030                               current_col, current_col + cols,
3031                               current_row, current_row + rows))
3032             break;
3033           
3034           current_col++;
3035         }
3036     }
3037
3038   /* set attach props */
3039   gtk_menu_attach (GTK_MENU (menu), item,
3040                    current_col, current_col + cols,
3041                    current_row, current_row + rows);
3042 }
3043
3044 static void
3045 gtk_combo_box_relayout (GtkComboBox *combo_box)
3046 {
3047   GList *list, *j;
3048   GtkWidget *menu;
3049
3050   menu = combo_box->priv->popup_widget;
3051   
3052   /* do nothing unless we are in menu style and realized */
3053   if (combo_box->priv->tree_view || !GTK_IS_MENU_SHELL (menu))
3054     return;
3055   
3056   list = gtk_container_get_children (GTK_CONTAINER (menu));
3057   
3058   for (j = g_list_last (list); j; j = j->prev)
3059     gtk_container_remove (GTK_CONTAINER (menu), j->data);
3060   
3061   gtk_combo_box_menu_fill (combo_box);
3062
3063   g_list_free (list);
3064 }
3065
3066 /* callbacks */
3067 static gboolean
3068 gtk_combo_box_menu_button_press (GtkWidget      *widget,
3069                                  GdkEventButton *event,
3070                                  gpointer        user_data)
3071 {
3072   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3073   GtkComboBoxPrivate *priv = combo_box->priv;
3074
3075   if (GTK_IS_MENU (priv->popup_widget) &&
3076       event->type == GDK_BUTTON_PRESS && event->button == 1)
3077     {
3078       if (priv->focus_on_click && 
3079           !GTK_WIDGET_HAS_FOCUS (priv->button))
3080         gtk_widget_grab_focus (priv->button);
3081
3082       gtk_combo_box_menu_popup (combo_box, event->button, event->time);
3083
3084       return TRUE;
3085     }
3086
3087   return FALSE;
3088 }
3089
3090 static void
3091 gtk_combo_box_menu_item_activate (GtkWidget *item,
3092                                   gpointer   user_data)
3093 {
3094   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3095   GtkWidget *cell_view;
3096   GtkTreePath *path;
3097   GtkTreeIter iter;
3098
3099   cell_view = GTK_BIN (item)->child;
3100
3101   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
3102
3103   path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (cell_view));
3104
3105   if (gtk_tree_model_get_iter (combo_box->priv->model, &iter, path))
3106   {
3107     if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL)
3108       gtk_combo_box_set_active_iter (combo_box, &iter);
3109   }
3110
3111   gtk_tree_path_free (path);
3112
3113   combo_box->priv->editing_canceled = FALSE;
3114 }
3115
3116 static void
3117 gtk_combo_box_model_row_inserted (GtkTreeModel     *model,
3118                                   GtkTreePath      *path,
3119                                   GtkTreeIter      *iter,
3120                                   gpointer          user_data)
3121 {
3122   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3123
3124   if (combo_box->priv->tree_view)
3125     gtk_combo_box_list_popup_resize (combo_box);
3126   else
3127     gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
3128 }
3129
3130 static void
3131 gtk_combo_box_model_row_deleted (GtkTreeModel     *model,
3132                                  GtkTreePath      *path,
3133                                  gpointer          user_data)
3134 {
3135   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3136   GtkComboBoxPrivate *priv = combo_box->priv;
3137
3138   if (!gtk_tree_row_reference_valid (priv->active_row))
3139     {
3140       if (priv->cell_view)
3141         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
3142       g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
3143     }
3144   
3145   if (priv->tree_view)
3146     gtk_combo_box_list_popup_resize (combo_box);
3147   else
3148     gtk_combo_box_menu_row_deleted (model, path, user_data);  
3149 }
3150
3151 static void
3152 gtk_combo_box_model_rows_reordered (GtkTreeModel    *model,
3153                                     GtkTreePath     *path,
3154                                     GtkTreeIter     *iter,
3155                                     gint            *new_order,
3156                                     gpointer         user_data)
3157 {
3158   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3159
3160   gtk_tree_row_reference_reordered (G_OBJECT (user_data), path, iter, new_order);
3161
3162   if (!combo_box->priv->tree_view)
3163     gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
3164 }
3165                                                     
3166 static void
3167 gtk_combo_box_model_row_changed (GtkTreeModel     *model,
3168                                  GtkTreePath      *path,
3169                                  GtkTreeIter      *iter,
3170                                  gpointer          user_data)
3171 {
3172   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3173   GtkComboBoxPrivate *priv = combo_box->priv;
3174   GtkTreePath *active_path;
3175
3176   /* FIXME this belongs to GtkCellView */
3177   if (gtk_tree_row_reference_valid (priv->active_row))
3178     {
3179       active_path = gtk_tree_row_reference_get_path (priv->active_row);
3180       if (gtk_tree_path_compare (path, active_path) == 0 &&
3181           priv->cell_view)
3182         gtk_widget_queue_resize (GTK_WIDGET (priv->cell_view));
3183       gtk_tree_path_free (active_path);
3184     }
3185       
3186   if (priv->tree_view)
3187     gtk_combo_box_list_row_changed (model, path, iter, user_data);
3188   else
3189     gtk_combo_box_menu_row_changed (model, path, iter, user_data);
3190 }
3191
3192 static gboolean
3193 list_popup_resize_idle (gpointer user_data)
3194 {
3195   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3196   GtkComboBoxPrivate *priv = combo_box->priv;
3197   gint x, y, width, height;
3198
3199   if (priv->tree_view && GTK_WIDGET_MAPPED (priv->popup_window))
3200     {
3201       gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
3202   
3203       gtk_widget_set_size_request (priv->popup_window, width, height);
3204       gtk_window_move (GTK_WINDOW (priv->popup_window), x, y);
3205     }
3206
3207   priv->resize_idle_id = 0;
3208
3209   return FALSE;
3210 }
3211
3212 static void
3213 gtk_combo_box_list_popup_resize (GtkComboBox *combo_box)
3214 {
3215   GtkComboBoxPrivate *priv = combo_box->priv;
3216
3217   if (!priv->resize_idle_id)
3218     priv->resize_idle_id = 
3219       gdk_threads_add_idle (list_popup_resize_idle, combo_box);
3220 }
3221
3222 static void
3223 gtk_combo_box_model_row_expanded (GtkTreeModel     *model,
3224                                   GtkTreePath      *path,
3225                                   GtkTreeIter      *iter,
3226                                   gpointer          user_data)
3227 {
3228   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3229   
3230   gtk_combo_box_list_popup_resize (combo_box);
3231 }
3232
3233
3234 static GtkWidget *
3235 find_menu_by_path (GtkWidget   *menu,
3236                    GtkTreePath *path,
3237                    gboolean     skip_first)
3238 {
3239   GList *i, *list;
3240   GtkWidget *item;
3241   GtkWidget *submenu;    
3242   GtkTreeRowReference *mref;
3243   GtkTreePath *mpath;
3244   gboolean skip;
3245
3246   list = gtk_container_get_children (GTK_CONTAINER (menu));
3247   skip = skip_first;
3248   item = NULL;
3249   for (i = list; i; i = i->next)
3250     {
3251       if (GTK_IS_SEPARATOR_MENU_ITEM (i->data))
3252         {
3253           mref = g_object_get_data (G_OBJECT (i->data), "gtk-combo-box-item-path");
3254           if (!mref)
3255             continue;
3256           else if (!gtk_tree_row_reference_valid (mref))
3257             mpath = NULL;
3258           else
3259             mpath = gtk_tree_row_reference_get_path (mref);
3260         }
3261       else if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3262         {
3263           if (skip)
3264             {
3265               skip = FALSE;
3266               continue;
3267             }
3268
3269           mpath = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3270         }
3271       else 
3272         continue;
3273
3274       /* this case is necessary, since the row reference of
3275        * the cell view may already be updated after a deletion
3276        */
3277       if (!mpath)
3278         {
3279           item = i->data;
3280           break;
3281         }
3282       if (gtk_tree_path_compare (mpath, path) == 0)
3283         {
3284           gtk_tree_path_free (mpath);
3285           item = i->data;
3286           break;
3287         }
3288       if (gtk_tree_path_is_ancestor (mpath, path))
3289         {
3290           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3291           if (submenu != NULL)
3292             {
3293               gtk_tree_path_free (mpath);
3294               item = find_menu_by_path (submenu, path, TRUE);
3295               break;
3296             }
3297         }
3298       gtk_tree_path_free (mpath);
3299     }
3300   
3301   g_list_free (list);  
3302
3303   return item;
3304 }
3305
3306 #if 0
3307 static void
3308 dump_menu_tree (GtkWidget   *menu, 
3309                 gint         level)
3310 {
3311   GList *i, *list;
3312   GtkWidget *submenu;    
3313   GtkTreePath *path;
3314
3315   list = gtk_container_get_children (GTK_CONTAINER (menu));
3316   for (i = list; i; i = i->next)
3317     {
3318       if (GTK_IS_CELL_VIEW (GTK_BIN (i->data)->child))
3319         {
3320           path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (GTK_BIN (i->data)->child));
3321           g_print ("%*s%s\n", 2 * level, " ", gtk_tree_path_to_string (path));
3322           gtk_tree_path_free (path);
3323
3324           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
3325           if (submenu != NULL)
3326             dump_menu_tree (submenu, level + 1);
3327         }
3328     }
3329   
3330   g_list_free (list);  
3331 }
3332 #endif
3333
3334 static void
3335 gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
3336                                  GtkTreePath  *path,
3337                                  GtkTreeIter  *iter,
3338                                  gpointer      user_data)
3339 {
3340   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3341   GtkComboBoxPrivate *priv = combo_box->priv;
3342   GtkWidget *parent;
3343   GtkWidget *item, *menu, *separator;
3344   GtkTreePath *ppath;
3345   GtkTreeIter piter;
3346   gint depth, pos;
3347   gboolean is_separator;
3348
3349   if (!priv->popup_widget)
3350     return;
3351
3352   depth = gtk_tree_path_get_depth (path);
3353   pos = gtk_tree_path_get_indices (path)[depth - 1];
3354   if (depth > 1)
3355     {
3356       ppath = gtk_tree_path_copy (path);
3357       gtk_tree_path_up (ppath);
3358       parent = find_menu_by_path (priv->popup_widget, ppath, FALSE);
3359       gtk_tree_path_free (ppath);
3360
3361       menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent));
3362       if (!menu)
3363         {
3364           menu = gtk_menu_new ();
3365           gtk_widget_show (menu);
3366           gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), menu);
3367           
3368           /* Ugly - since menus can only activate leaves, we have to
3369            * duplicate the item inside the submenu.
3370            */
3371           gtk_tree_model_iter_parent (model, &piter, iter);
3372           item = gtk_cell_view_menu_item_new (combo_box, model, &piter);
3373           separator = gtk_separator_menu_item_new ();
3374           g_signal_connect (item, "activate",
3375                             G_CALLBACK (gtk_combo_box_menu_item_activate),
3376                             combo_box);
3377           gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
3378           gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
3379           if (cell_view_is_sensitive (GTK_CELL_VIEW (GTK_BIN (item)->child)))
3380             {
3381               gtk_widget_show (item);
3382               gtk_widget_show (separator);
3383             }
3384         }
3385       pos += 2;
3386     }
3387   else
3388     {
3389       menu = priv->popup_widget;
3390       if (priv->add_tearoffs)
3391         pos += 1;
3392     }
3393   
3394   if (priv->row_separator_func)
3395     is_separator = (*priv->row_separator_func) (model, iter,
3396                                                 priv->row_separator_data);
3397   else
3398     is_separator = FALSE;
3399
3400   if (is_separator)
3401     {
3402       item = gtk_separator_menu_item_new ();
3403       g_object_set_data_full (G_OBJECT (item),
3404                               I_("gtk-combo-box-item-path"),
3405                               gtk_tree_row_reference_new (model, path),
3406                               (GDestroyNotify)gtk_tree_row_reference_free);
3407     }
3408   else
3409     {
3410       item = gtk_cell_view_menu_item_new (combo_box, model, iter);
3411       
3412       g_signal_connect (item, "activate",
3413                         G_CALLBACK (gtk_combo_box_menu_item_activate),
3414                         combo_box);
3415     }
3416
3417   gtk_widget_show (item);
3418   gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, pos);
3419 }
3420
3421 static void
3422 gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
3423                                 GtkTreePath  *path,
3424                                 gpointer      user_data)
3425 {
3426   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3427   GtkComboBoxPrivate *priv = combo_box->priv;
3428   GtkWidget *menu;
3429   GtkWidget *item;
3430
3431   if (!priv->popup_widget)
3432     return;
3433
3434   item = find_menu_by_path (priv->popup_widget, path, FALSE);
3435   menu = gtk_widget_get_parent (item);
3436   gtk_container_remove (GTK_CONTAINER (menu), item);
3437
3438   if (gtk_tree_path_get_depth (path) > 1)
3439     {
3440       GtkTreePath *parent_path;
3441       GtkTreeIter iter;
3442       GtkWidget *parent;
3443
3444       parent_path = gtk_tree_path_copy (path);
3445       gtk_tree_path_up (parent_path);
3446       gtk_tree_model_get_iter (model, &iter, parent_path);
3447
3448       if (!gtk_tree_model_iter_has_child (model, &iter))
3449         {
3450           parent = find_menu_by_path (priv->popup_widget, 
3451                                       parent_path, FALSE);
3452           gtk_menu_item_set_submenu (GTK_MENU_ITEM (parent), NULL);
3453         }
3454     }
3455 }
3456
3457 static void
3458 gtk_combo_box_menu_rows_reordered  (GtkTreeModel     *model,
3459                                     GtkTreePath      *path,
3460                                     GtkTreeIter      *iter,
3461                                     gint             *new_order,
3462                                     gpointer          user_data)
3463 {
3464   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3465
3466   gtk_combo_box_relayout (combo_box);
3467 }
3468                                     
3469 static void
3470 gtk_combo_box_menu_row_changed (GtkTreeModel *model,
3471                                 GtkTreePath  *path,
3472                                 GtkTreeIter  *iter,
3473                                 gpointer      user_data)
3474 {
3475   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
3476   GtkComboBoxPrivate *priv = combo_box->priv;
3477   GtkWidget *item;
3478   gint width;
3479   gboolean is_separator;
3480
3481   if (!priv->popup_widget)
3482     return;
3483
3484   item = find_menu_by_path (priv->popup_widget, path, FALSE);
3485
3486   if (priv->row_separator_func)
3487     is_separator = (*priv->row_separator_func) (model, iter,
3488                                                 priv->row_separator_data);
3489   else
3490     is_separator = FALSE;
3491
3492   if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
3493     {
3494       gtk_combo_box_menu_row_deleted (model, path, combo_box);
3495       gtk_combo_box_menu_row_inserted (model, path, iter, combo_box);
3496     }
3497
3498   if (priv->wrap_width && item->parent == priv->popup_widget)
3499     {
3500       GtkWidget *pitem = NULL;
3501       GtkTreePath *prev;
3502
3503       prev = gtk_tree_path_copy (path);
3504
3505       if (gtk_tree_path_prev (prev))
3506         pitem = find_menu_by_path (priv->popup_widget, prev, FALSE);
3507
3508       gtk_tree_path_free (prev);
3509
3510       /* unattach item so gtk_combo_box_relayout_item() won't spuriously
3511          move it */
3512       gtk_container_child_set (GTK_CONTAINER (priv->popup_widget),
3513                                item, 
3514                                "left-attach", -1, 
3515                                "right-attach", -1,
3516                                "top-attach", -1, 
3517                                "bottom-attach", -1, 
3518                                NULL);
3519
3520       gtk_combo_box_relayout_item (combo_box, item, iter, pitem);
3521     }
3522
3523   width = gtk_combo_box_calc_requested_width (combo_box, path);
3524
3525   if (width > priv->width)
3526     {
3527       if (priv->cell_view)
3528         {
3529           gtk_widget_set_size_request (priv->cell_view, width, -1);
3530           gtk_widget_queue_resize (priv->cell_view);
3531         }
3532       priv->width = width;
3533     }
3534 }
3535
3536 /*
3537  * list style
3538  */
3539
3540 static void
3541 gtk_combo_box_list_setup (GtkComboBox *combo_box)
3542 {
3543   GtkComboBoxPrivate *priv = combo_box->priv;
3544   GtkTreeSelection *sel;
3545
3546   priv->button = gtk_toggle_button_new ();
3547   gtk_widget_set_parent (priv->button,
3548                          GTK_BIN (combo_box)->child->parent);
3549   g_signal_connect (priv->button, "button_press_event",
3550                     G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
3551   g_signal_connect (priv->button, "toggled",
3552                     G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
3553
3554   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
3555   gtk_container_add (GTK_CONTAINER (priv->button), priv->arrow);
3556   priv->separator = NULL;
3557   gtk_widget_show_all (priv->button);
3558
3559   if (priv->cell_view)
3560     {
3561       gtk_cell_view_set_background_color (GTK_CELL_VIEW (priv->cell_view), 
3562                                           &GTK_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
3563
3564       priv->box = gtk_event_box_new ();
3565       gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->box), 
3566                                         FALSE);
3567
3568       if (priv->has_frame)
3569         {
3570           priv->cell_view_frame = gtk_frame_new (NULL);
3571           gtk_frame_set_shadow_type (GTK_FRAME (priv->cell_view_frame),
3572                                      GTK_SHADOW_IN);
3573         }
3574       else 
3575         {
3576           combo_box->priv->cell_view_frame = gtk_event_box_new ();
3577           gtk_event_box_set_visible_window (GTK_EVENT_BOX (combo_box->priv->cell_view_frame), 
3578                                             FALSE);
3579         }
3580       
3581       gtk_widget_set_parent (priv->cell_view_frame,
3582                              GTK_BIN (combo_box)->child->parent);
3583       gtk_container_add (GTK_CONTAINER (priv->cell_view_frame), priv->box);
3584       gtk_widget_show_all (priv->cell_view_frame);
3585
3586       g_signal_connect (priv->box, "button_press_event",
3587                         G_CALLBACK (gtk_combo_box_list_button_pressed), 
3588                         combo_box);
3589     }
3590
3591   priv->tree_view = gtk_tree_view_new ();
3592   sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
3593   gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
3594   gtk_tree_selection_set_select_function (sel,
3595                                           gtk_combo_box_list_select_func,
3596                                           NULL, NULL);
3597   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view),
3598                                      FALSE);
3599   gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->tree_view),
3600                                      TRUE);
3601   if (priv->row_separator_func)
3602     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (priv->tree_view), 
3603                                           priv->row_separator_func, 
3604                                           priv->row_separator_data, 
3605                                           NULL);
3606   if (priv->model)
3607     gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), priv->model);
3608     
3609   priv->column = gtk_tree_view_column_new ();
3610   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), priv->column);
3611
3612   /* sync up */
3613   gtk_combo_box_sync_cells (combo_box, 
3614                             GTK_CELL_LAYOUT (priv->column));
3615
3616   if (gtk_tree_row_reference_valid (priv->active_row))
3617     {
3618       GtkTreePath *path;
3619
3620       path = gtk_tree_row_reference_get_path (priv->active_row);
3621       gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view),
3622                                 path, NULL, FALSE);
3623       gtk_tree_path_free (path);
3624     }
3625
3626   /* set sample/popup widgets */
3627   gtk_combo_box_set_popup_widget (combo_box, priv->tree_view);
3628
3629   g_signal_connect (priv->tree_view, "key_press_event",
3630                     G_CALLBACK (gtk_combo_box_list_key_press),
3631                     combo_box);
3632   g_signal_connect (priv->tree_view, "enter_notify_event",
3633                     G_CALLBACK (gtk_combo_box_list_enter_notify),
3634                     combo_box);
3635   g_signal_connect (priv->tree_view, "row_expanded",
3636                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3637                     combo_box);
3638   g_signal_connect (priv->tree_view, "row_collapsed",
3639                     G_CALLBACK (gtk_combo_box_model_row_expanded),
3640                     combo_box);
3641   g_signal_connect (priv->popup_window, "button_press_event",
3642                     G_CALLBACK (gtk_combo_box_list_button_pressed),
3643                     combo_box);
3644   g_signal_connect (priv->popup_window, "button_release_event",
3645                     G_CALLBACK (gtk_combo_box_list_button_released),
3646                     combo_box);
3647
3648   gtk_widget_show (priv->tree_view);
3649 }
3650
3651 static void
3652 gtk_combo_box_list_destroy (GtkComboBox *combo_box)
3653 {
3654   GtkComboBoxPrivate *priv = combo_box->priv;
3655
3656   /* disconnect signals */
3657   g_signal_handlers_disconnect_matched (priv->tree_view,
3658                                         G_SIGNAL_MATCH_DATA,
3659                                         0, 0, NULL, NULL, combo_box);
3660   g_signal_handlers_disconnect_matched (priv->button,
3661                                         G_SIGNAL_MATCH_DATA,
3662                                         0, 0, NULL,
3663                                         gtk_combo_box_list_button_pressed,
3664                                         NULL);
3665   g_signal_handlers_disconnect_matched (priv->popup_window,
3666                                         G_SIGNAL_MATCH_DATA,
3667                                         0, 0, NULL,
3668                                         gtk_combo_box_list_button_pressed,
3669                                         NULL);
3670   g_signal_handlers_disconnect_matched (priv->popup_window,
3671                                         G_SIGNAL_MATCH_DATA,
3672                                         0, 0, NULL,
3673                                         gtk_combo_box_list_button_released,
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_show,
3680                                         NULL);
3681
3682   g_signal_handlers_disconnect_matched (priv->popup_window,
3683                                         G_SIGNAL_MATCH_DATA,
3684                                         0, 0, NULL, 
3685                                         gtk_combo_box_child_hide,
3686                                         NULL);
3687   
3688   if (priv->box)
3689     g_signal_handlers_disconnect_matched (priv->box,
3690                                           G_SIGNAL_MATCH_DATA,
3691                                           0, 0, NULL,
3692                                           gtk_combo_box_list_button_pressed,
3693                                           NULL);
3694
3695   /* destroy things (unparent will kill the latest ref from us)
3696    * last unref on button will destroy the arrow
3697    */
3698   gtk_widget_unparent (priv->button);
3699   priv->button = NULL;
3700   priv->arrow = NULL;
3701
3702   if (priv->cell_view)
3703     {
3704       g_object_set (priv->cell_view,
3705                     "background-set", FALSE,
3706                     NULL);
3707     }
3708
3709   if (priv->cell_view_frame)
3710     {
3711       gtk_widget_unparent (priv->cell_view_frame);
3712       priv->cell_view_frame = NULL;
3713       priv->box = NULL;
3714     }
3715
3716   if (priv->scroll_timer)
3717     {
3718       g_source_remove (priv->scroll_timer);
3719       priv->scroll_timer = 0;
3720     }
3721
3722   if (priv->resize_idle_id)
3723     {
3724       g_source_remove (priv->resize_idle_id);
3725       priv->resize_idle_id = 0;
3726     }
3727
3728   gtk_widget_destroy (priv->tree_view);
3729
3730   priv->tree_view = NULL;
3731   if (priv->popup_widget)
3732     {
3733       g_object_unref (priv->popup_widget);
3734       priv->popup_widget = NULL;
3735     }
3736 }
3737
3738 /* callbacks */
3739
3740 static gboolean
3741 gtk_combo_box_list_button_pressed (GtkWidget      *widget,
3742                                    GdkEventButton *event,
3743                                    gpointer        data)
3744 {
3745   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3746   GtkComboBoxPrivate *priv = combo_box->priv;
3747
3748   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3749
3750   if (ewidget == priv->popup_window)
3751     return TRUE;
3752
3753   if ((ewidget != priv->button && ewidget != priv->box) ||
3754       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3755     return FALSE;
3756
3757   if (priv->focus_on_click && 
3758       !GTK_WIDGET_HAS_FOCUS (priv->button))
3759     gtk_widget_grab_focus (priv->button);
3760
3761   gtk_combo_box_popup (combo_box);
3762
3763   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->button), TRUE);
3764
3765   priv->auto_scroll = FALSE;
3766   if (priv->scroll_timer == 0)
3767     priv->scroll_timer = gdk_threads_add_timeout (SCROLL_TIME, 
3768                                                   (GSourceFunc) gtk_combo_box_list_scroll_timeout, 
3769                                                    combo_box);
3770
3771   priv->popup_in_progress = TRUE;
3772
3773   return TRUE;
3774 }
3775
3776 static gboolean
3777 gtk_combo_box_list_button_released (GtkWidget      *widget,
3778                                     GdkEventButton *event,
3779                                     gpointer        data)
3780 {
3781   gboolean ret;
3782   GtkTreePath *path = NULL;
3783   GtkTreeIter iter;
3784
3785   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3786   GtkComboBoxPrivate *priv = combo_box->priv;
3787
3788   gboolean popup_in_progress = FALSE;
3789
3790   GtkWidget *ewidget = gtk_get_event_widget ((GdkEvent *)event);
3791
3792   if (priv->popup_in_progress)
3793     {
3794       popup_in_progress = TRUE;
3795       priv->popup_in_progress = FALSE;
3796     }
3797
3798   gtk_tree_view_set_hover_expand (GTK_TREE_VIEW (priv->tree_view), 
3799                                   FALSE);
3800   if (priv->scroll_timer)
3801     {
3802       g_source_remove (priv->scroll_timer);
3803       priv->scroll_timer = 0;
3804     }
3805
3806   if (ewidget != priv->tree_view)
3807     {
3808       if ((ewidget == priv->button || 
3809            ewidget == priv->box) &&
3810           !popup_in_progress &&
3811           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->button)))
3812         {
3813           gtk_combo_box_popdown (combo_box);
3814           return TRUE;
3815         }
3816
3817       /* released outside treeview */
3818       if (ewidget != priv->button && ewidget != priv->box)
3819         {
3820           gtk_combo_box_popdown (combo_box);
3821
3822           return TRUE;
3823         }
3824
3825       return FALSE;
3826     }
3827
3828   /* select something cool */
3829   ret = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (priv->tree_view),
3830                                        event->x, event->y,
3831                                        &path,
3832                                        NULL, NULL, NULL);
3833
3834   if (!ret)
3835     return TRUE; /* clicked outside window? */
3836
3837   gtk_tree_model_get_iter (priv->model, &iter, path);
3838   gtk_tree_path_free (path);
3839
3840   gtk_combo_box_popdown (combo_box);
3841
3842   if (tree_column_row_is_sensitive (combo_box, &iter))
3843     gtk_combo_box_set_active_iter (combo_box, &iter);
3844
3845   return TRUE;
3846 }
3847
3848 static gboolean
3849 gtk_combo_box_menu_key_press (GtkWidget   *widget,
3850                               GdkEventKey *event,
3851                               gpointer     data)
3852 {
3853   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3854
3855   if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3856     {
3857       /* The menu hasn't managed the
3858        * event, forward it to the combobox
3859        */
3860       gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3861     }
3862
3863   return TRUE;
3864 }
3865
3866 static gboolean
3867 gtk_combo_box_list_key_press (GtkWidget   *widget,
3868                               GdkEventKey *event,
3869                               gpointer     data)
3870 {
3871   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3872   GtkTreeIter iter;
3873
3874   if (event->keyval == GDK_Return || event->keyval == GDK_ISO_Enter || event->keyval == GDK_KP_Enter ||
3875       event->keyval == GDK_space || event->keyval == GDK_KP_Space) 
3876   {
3877     GtkTreeModel *model = NULL;
3878     
3879     gtk_combo_box_popdown (combo_box);
3880     
3881     if (combo_box->priv->model)
3882       {
3883         GtkTreeSelection *sel;
3884
3885         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
3886
3887         if (gtk_tree_selection_get_selected (sel, &model, &iter))
3888           gtk_combo_box_set_active_iter (combo_box, &iter);
3889       }
3890     
3891     return TRUE;
3892   }
3893
3894   if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
3895     {
3896       /* The list hasn't managed the
3897        * event, forward it to the combobox
3898        */
3899       gtk_bindings_activate_event (GTK_OBJECT (combo_box), event);
3900     }
3901
3902   return TRUE;
3903 }
3904
3905 static void
3906 gtk_combo_box_list_auto_scroll (GtkComboBox *combo_box,
3907                                 gint         x, 
3908                                 gint         y)
3909 {
3910   GtkWidget *tree_view = combo_box->priv->tree_view;
3911   GtkAdjustment *adj;
3912   gdouble value;
3913
3914   adj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3915   if (adj && adj->upper - adj->lower > adj->page_size)
3916     {
3917       if (x <= tree_view->allocation.x && 
3918           adj->lower < adj->value)
3919         {
3920           value = adj->value - (tree_view->allocation.x - x + 1);
3921           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3922         }
3923       else if (x >= tree_view->allocation.x + tree_view->allocation.width &&
3924                adj->upper - adj->page_size > adj->value)
3925         {
3926           value = adj->value + (x - tree_view->allocation.x - tree_view->allocation.width + 1);
3927           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3928         }
3929     }
3930
3931   adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (combo_box->priv->scrolled_window));
3932   if (adj && adj->upper - adj->lower > adj->page_size)
3933     {
3934       if (y <= tree_view->allocation.y && 
3935           adj->lower < adj->value)
3936         {
3937           value = adj->value - (tree_view->allocation.y - y + 1);
3938           gtk_adjustment_set_value (adj, CLAMP (value, adj->lower, adj->upper - adj->page_size));
3939         }
3940       else if (y >= tree_view->allocation.height &&
3941                adj->upper - adj->page_size > adj->value)
3942         {
3943           value = adj->value + (y - tree_view->allocation.height + 1);
3944           gtk_adjustment_set_value (adj, CLAMP (value, 0.0, adj->upper - adj->page_size));
3945         }
3946     }
3947 }
3948
3949 static gboolean
3950 gtk_combo_box_list_scroll_timeout (GtkComboBox *combo_box)
3951 {
3952   GtkComboBoxPrivate *priv = combo_box->priv;
3953   gint x, y;
3954
3955   if (priv->auto_scroll)
3956     {
3957       gdk_window_get_pointer (priv->tree_view->window, &x, &y, NULL);
3958       gtk_combo_box_list_auto_scroll (combo_box, x, y);
3959     }
3960
3961   return TRUE;
3962 }
3963
3964 static gboolean 
3965 gtk_combo_box_list_enter_notify (GtkWidget        *widget,
3966                                  GdkEventCrossing *event,
3967                                  gpointer          data)
3968 {
3969   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
3970
3971   combo_box->priv->auto_scroll = TRUE;
3972
3973   return TRUE;
3974 }
3975
3976 static gboolean
3977 gtk_combo_box_list_select_func (GtkTreeSelection *selection,
3978                                 GtkTreeModel     *model,
3979                                 GtkTreePath      *path,
3980                                 gboolean          path_currently_selected,
3981                                 gpointer          data)
3982 {
3983   GList *list;
3984   gboolean sensitive = FALSE;
3985
3986   for (list = selection->tree_view->priv->columns; list && !sensitive; list = list->next)
3987     {
3988       GList *cells, *cell;
3989       gboolean cell_sensitive, cell_visible;
3990       GtkTreeIter iter;
3991       GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN (list->data);
3992
3993       if (!column->visible)
3994         continue;
3995
3996       gtk_tree_model_get_iter (model, &iter, path);
3997       gtk_tree_view_column_cell_set_cell_data (column, model, &iter,
3998                                                FALSE, FALSE);
3999
4000       cell = cells = gtk_tree_view_column_get_cell_renderers (column);
4001       while (cell)
4002         {
4003           g_object_get (cell->data,
4004                         "sensitive", &cell_sensitive,
4005                         "visible", &cell_visible,
4006                         NULL);
4007
4008           if (cell_visible && cell_sensitive)
4009             break;
4010
4011           cell = cell->next;
4012         }
4013       g_list_free (cells);
4014
4015       sensitive = cell_sensitive;
4016     }
4017
4018   return sensitive;
4019 }
4020
4021 static void
4022 gtk_combo_box_list_row_changed (GtkTreeModel *model,
4023                                 GtkTreePath  *path,
4024                                 GtkTreeIter  *iter,
4025                                 gpointer      data)
4026 {
4027   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
4028   GtkComboBoxPrivate *priv = combo_box->priv;
4029   gint width;
4030
4031   width = gtk_combo_box_calc_requested_width (combo_box, path);
4032
4033   if (width > priv->width)
4034     {
4035       if (priv->cell_view) 
4036         {
4037           gtk_widget_set_size_request (priv->cell_view, width, -1);
4038           gtk_widget_queue_resize (priv->cell_view);
4039         }
4040       priv->width = width;
4041     }
4042 }
4043
4044 /*
4045  * GtkCellLayout implementation
4046  */
4047
4048 static void
4049 pack_start_recurse (GtkWidget       *menu,
4050                     GtkCellRenderer *cell,
4051                     gboolean         expand)
4052 {
4053   GList *i, *list;
4054   GtkWidget *submenu;    
4055   
4056   list = gtk_container_get_children (GTK_CONTAINER (menu));
4057   for (i = list; i; i = i->next)
4058     {
4059       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4060         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
4061                                     cell, expand);
4062
4063       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4064       if (submenu != NULL)
4065         pack_start_recurse (submenu, cell, expand);
4066     }
4067
4068   g_list_free (list);
4069 }
4070
4071 static void
4072 gtk_combo_box_cell_layout_pack_start (GtkCellLayout   *layout,
4073                                       GtkCellRenderer *cell,
4074                                       gboolean         expand)
4075 {
4076   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4077   ComboCellInfo *info;
4078   GtkComboBoxPrivate *priv;
4079
4080   priv = combo_box->priv;
4081
4082   g_object_ref_sink (cell);
4083
4084   info = g_new0 (ComboCellInfo, 1);
4085   info->cell = cell;
4086   info->expand = expand;
4087   info->pack = GTK_PACK_START;
4088
4089   priv->cells = g_slist_append (priv->cells, info);
4090
4091   if (priv->cell_view)
4092     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->cell_view),
4093                                 cell, expand);
4094
4095   if (priv->column)
4096     gtk_tree_view_column_pack_start (priv->column, cell, expand);
4097
4098   if (GTK_IS_MENU (priv->popup_widget))
4099     pack_start_recurse (priv->popup_widget, cell, expand);
4100 }
4101
4102 static void
4103 pack_end_recurse (GtkWidget       *menu,
4104                   GtkCellRenderer *cell,
4105                   gboolean         expand)
4106 {
4107   GList *i, *list;
4108   GtkWidget *submenu;    
4109   
4110   list = gtk_container_get_children (GTK_CONTAINER (menu));
4111   for (i = list; i; i = i->next)
4112     {
4113       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4114         gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child), 
4115                                   cell, expand);
4116
4117       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4118       if (submenu != NULL)
4119         pack_end_recurse (submenu, cell, expand);
4120     }
4121
4122   g_list_free (list);
4123 }
4124
4125 static void
4126 gtk_combo_box_cell_layout_pack_end (GtkCellLayout   *layout,
4127                                     GtkCellRenderer *cell,
4128                                     gboolean         expand)
4129 {
4130   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4131   ComboCellInfo *info;
4132   GtkComboBoxPrivate *priv;
4133
4134   priv = combo_box->priv;
4135
4136   g_object_ref_sink (cell);
4137
4138   info = g_new0 (ComboCellInfo, 1);
4139   info->cell = cell;
4140   info->expand = expand;
4141   info->pack = GTK_PACK_END;
4142
4143   priv->cells = g_slist_append (priv->cells, info);
4144
4145   if (priv->cell_view)
4146     gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (priv->cell_view),
4147                               cell, expand);
4148
4149   if (priv->column)
4150     gtk_tree_view_column_pack_end (priv->column, cell, expand);
4151
4152   if (GTK_IS_MENU (priv->popup_widget))
4153     pack_end_recurse (priv->popup_widget, cell, expand);
4154 }
4155
4156 static GList *
4157 gtk_combo_box_cell_layout_get_cells (GtkCellLayout *layout)
4158 {
4159   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4160   GSList *list;
4161   GList *retval = NULL;
4162
4163   for (list = combo_box->priv->cells; list; list = list->next)
4164     {
4165       ComboCellInfo *info = (ComboCellInfo *)list->data;
4166
4167       retval = g_list_prepend (retval, info->cell);
4168     }
4169
4170   return g_list_reverse (retval);
4171 }
4172
4173 static void
4174 clear_recurse (GtkWidget *menu)
4175 {
4176   GList *i, *list;
4177   GtkWidget *submenu;    
4178   
4179   list = gtk_container_get_children (GTK_CONTAINER (menu));
4180   for (i = list; i; i = i->next)
4181     {
4182       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4183         gtk_cell_layout_clear (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child)); 
4184
4185       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4186       if (submenu != NULL)
4187         clear_recurse (submenu);
4188     }
4189
4190   g_list_free (list);
4191 }
4192
4193 static void
4194 gtk_combo_box_cell_layout_clear (GtkCellLayout *layout)
4195 {
4196   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4197   GtkComboBoxPrivate *priv = combo_box->priv;
4198   GSList *i;
4199   
4200   if (priv->cell_view)
4201     gtk_cell_layout_clear (GTK_CELL_LAYOUT (priv->cell_view));
4202
4203   if (priv->column)
4204     gtk_tree_view_column_clear (priv->column);
4205
4206   for (i = priv->cells; i; i = i->next)
4207     {
4208      ComboCellInfo *info = (ComboCellInfo *)i->data;
4209
4210       gtk_combo_box_cell_layout_clear_attributes (layout, info->cell);
4211       g_object_unref (info->cell);
4212       g_free (info);
4213       i->data = NULL;
4214     }
4215   g_slist_free (priv->cells);
4216   priv->cells = NULL;
4217
4218   if (GTK_IS_MENU (priv->popup_widget))
4219     clear_recurse (priv->popup_widget);
4220 }
4221
4222 static void
4223 add_attribute_recurse (GtkWidget       *menu,
4224                        GtkCellRenderer *cell,
4225                        const gchar     *attribute,
4226                        gint             column)
4227 {
4228   GList *i, *list;
4229   GtkWidget *submenu;    
4230   
4231   list = gtk_container_get_children (GTK_CONTAINER (menu));
4232   for (i = list; i; i = i->next)
4233     {
4234       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4235         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4236                                        cell, attribute, column); 
4237
4238       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4239       if (submenu != NULL)
4240         add_attribute_recurse (submenu, cell, attribute, column);
4241     }
4242
4243   g_list_free (list);
4244 }
4245                        
4246 static void
4247 gtk_combo_box_cell_layout_add_attribute (GtkCellLayout   *layout,
4248                                          GtkCellRenderer *cell,
4249                                          const gchar     *attribute,
4250                                          gint             column)
4251 {
4252   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4253   ComboCellInfo *info;
4254
4255   info = gtk_combo_box_get_cell_info (combo_box, cell);
4256   g_return_if_fail (info != NULL);
4257
4258   info->attributes = g_slist_prepend (info->attributes,
4259                                       GINT_TO_POINTER (column));
4260   info->attributes = g_slist_prepend (info->attributes,
4261                                       g_strdup (attribute));
4262
4263   if (combo_box->priv->cell_view)
4264     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->cell_view),
4265                                    cell, attribute, column);
4266
4267   if (combo_box->priv->column)
4268     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo_box->priv->column),
4269                                    cell, attribute, column);
4270
4271   if (GTK_IS_MENU (combo_box->priv->popup_widget))
4272     add_attribute_recurse (combo_box->priv->popup_widget, cell, attribute, column);
4273   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4274 }
4275
4276 static void
4277 combo_cell_data_func (GtkCellLayout   *cell_layout,
4278                       GtkCellRenderer *cell,
4279                       GtkTreeModel    *tree_model,
4280                       GtkTreeIter     *iter,
4281                       gpointer         data)
4282 {
4283   ComboCellInfo *info = (ComboCellInfo *)data;
4284   GtkWidget *parent = NULL;
4285   
4286   if (!info->func)
4287     return;
4288
4289   (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
4290   
4291   if (GTK_IS_WIDGET (cell_layout))
4292     parent = gtk_widget_get_parent (GTK_WIDGET (cell_layout));
4293   
4294   if (GTK_IS_MENU_ITEM (parent) && 
4295       gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)))
4296     g_object_set (cell, "sensitive", TRUE, NULL);
4297 }
4298
4299
4300 static void 
4301 set_cell_data_func_recurse (GtkWidget       *menu,
4302                             GtkCellRenderer *cell,
4303                             ComboCellInfo   *info)
4304 {
4305   GList *i, *list;
4306   GtkWidget *submenu;    
4307   GtkWidget *cell_view;
4308   
4309  list = gtk_container_get_children (GTK_CONTAINER (menu));
4310   for (i = list; i; i = i->next)
4311     {
4312       cell_view = GTK_BIN (i->data)->child;
4313       if (GTK_IS_CELL_LAYOUT (cell_view))
4314         {
4315           /* Override sensitivity for inner nodes; we don't
4316            * want menuitems with submenus to appear insensitive 
4317            */ 
4318           gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (cell_view), 
4319                                               cell, 
4320                                               combo_cell_data_func, 
4321                                               info, NULL); 
4322           submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4323           if (submenu != NULL)
4324             set_cell_data_func_recurse (submenu, cell, info);
4325         }
4326     }
4327
4328   g_list_free (list);
4329 }
4330
4331 static void
4332 gtk_combo_box_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4333                                               GtkCellRenderer       *cell,
4334                                               GtkCellLayoutDataFunc  func,
4335                                               gpointer               func_data,
4336                                               GDestroyNotify         destroy)
4337 {
4338   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4339   GtkComboBoxPrivate *priv = combo_box->priv;
4340   ComboCellInfo *info;
4341
4342   info = gtk_combo_box_get_cell_info (combo_box, cell);
4343   g_return_if_fail (info != NULL);
4344   
4345   if (info->destroy)
4346     {
4347       GDestroyNotify d = info->destroy;
4348
4349       info->destroy = NULL;
4350       d (info->func_data);
4351     }
4352
4353   info->func = func;
4354   info->func_data = func_data;
4355   info->destroy = destroy;
4356
4357   if (priv->cell_view)
4358     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->cell_view), cell, func, func_data, NULL);
4359
4360   if (priv->column)
4361     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column), cell, func, func_data, NULL);
4362
4363   if (GTK_IS_MENU (priv->popup_widget))
4364     set_cell_data_func_recurse (priv->popup_widget, cell, info);
4365
4366   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4367 }
4368
4369 static void 
4370 clear_attributes_recurse (GtkWidget       *menu,
4371                           GtkCellRenderer *cell)
4372 {
4373   GList *i, *list;
4374   GtkWidget *submenu;    
4375   
4376   list = gtk_container_get_children (GTK_CONTAINER (menu));
4377   for (i = list; i; i = i->next)
4378     {
4379       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4380         gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4381                                             cell); 
4382       
4383       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4384       if (submenu != NULL)
4385         clear_attributes_recurse (submenu, cell);
4386     }
4387
4388   g_list_free (list);
4389 }
4390
4391 static void
4392 gtk_combo_box_cell_layout_clear_attributes (GtkCellLayout   *layout,
4393                                             GtkCellRenderer *cell)
4394 {
4395   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4396   GtkComboBoxPrivate *priv;
4397   ComboCellInfo *info;
4398   GSList *list;
4399
4400   priv = combo_box->priv;
4401
4402   info = gtk_combo_box_get_cell_info (combo_box, cell);
4403   g_return_if_fail (info != NULL);
4404
4405   list = info->attributes;
4406   while (list && list->next)
4407     {
4408       g_free (list->data);
4409       list = list->next->next;
4410     }
4411   g_slist_free (info->attributes);
4412   info->attributes = NULL;
4413
4414   if (priv->cell_view)
4415     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->cell_view), cell);
4416
4417   if (priv->column)
4418     gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), cell);
4419
4420   if (GTK_IS_MENU (priv->popup_widget))
4421     clear_attributes_recurse (priv->popup_widget, cell);
4422
4423   gtk_widget_queue_resize (GTK_WIDGET (combo_box));
4424 }
4425
4426 static void 
4427 reorder_recurse (GtkWidget             *menu,
4428                  GtkCellRenderer       *cell,
4429                  gint                   position)
4430 {
4431   GList *i, *list;
4432   GtkWidget *submenu;    
4433   
4434   list = gtk_container_get_children (GTK_CONTAINER (menu));
4435   for (i = list; i; i = i->next)
4436     {
4437       if (GTK_IS_CELL_LAYOUT (GTK_BIN (i->data)->child))
4438         gtk_cell_layout_reorder (GTK_CELL_LAYOUT (GTK_BIN (i->data)->child),
4439                                  cell, position); 
4440       
4441       submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (i->data));
4442       if (submenu != NULL)
4443         reorder_recurse (submenu, cell, position);
4444     }
4445
4446   g_list_free (list);
4447 }
4448
4449 static void
4450 gtk_combo_box_cell_layout_reorder (GtkCellLayout   *layout,
4451                                    GtkCellRenderer *cell,
4452                                    gint             position)
4453 {
4454   GtkComboBox *combo_box = GTK_COMBO_BOX (layout);
4455   GtkComboBoxPrivate *priv;
4456   ComboCellInfo *info;
4457   GSList *link;
4458
4459   priv = combo_box->priv;
4460
4461   info = gtk_combo_box_get_cell_info (combo_box, cell);
4462
4463   g_return_if_fail (info != NULL);
4464   g_return_if_fail (position >= 0);
4465
4466   link = g_slist_find (priv->cells, info);
4467
4468   g_return_if_fail (link != NULL);
4469
4470   priv->cells = g_slist_delete_link (priv->cells, link);
4471   priv->cells = g_slist_insert (priv->cells, info, position);
4472
4473   if (priv->cell_view)
4474     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->cell_view),
4475                              cell, position);
4476
4477   if (priv->column)
4478     gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column),
4479                              cell, position);
4480
4481   if (GTK_IS_MENU (priv->popup_widget))
4482     reorder_recurse (priv->popup_widget, cell, position);
4483
4484   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
4485 }
4486
4487 /*
4488  * public API
4489  */
4490
4491 /**
4492  * gtk_combo_box_new:
4493  *
4494  * Creates a new empty #GtkComboBox.
4495  *
4496  * Return value: A new #GtkComboBox.
4497  *
4498  * Since: 2.4
4499  */
4500 GtkWidget *
4501 gtk_combo_box_new (void)
4502 {
4503   return g_object_new (GTK_TYPE_COMBO_BOX, NULL);
4504 }
4505
4506 /**
4507  * gtk_combo_box_new_with_model:
4508  * @model: A #GtkTreeModel.
4509  *
4510  * Creates a new #GtkComboBox with the model initialized to @model.
4511  *
4512  * Return value: A new #GtkComboBox.
4513  *
4514  * Since: 2.4
4515  */
4516 GtkWidget *
4517 gtk_combo_box_new_with_model (GtkTreeModel *model)
4518 {
4519   GtkComboBox *combo_box;
4520
4521   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
4522
4523   combo_box = g_object_new (GTK_TYPE_COMBO_BOX, "model", model, NULL);
4524
4525   return GTK_WIDGET (combo_box);
4526 }
4527
4528 /**
4529  * gtk_combo_box_get_wrap_width:
4530  * @combo_box: A #GtkComboBox
4531  *
4532  * Returns the wrap width which is used to determine the number of columns 
4533  * for the popup menu. If the wrap width is larger than 1, the combo box 
4534  * is in table mode.
4535  *
4536  * Returns: the wrap width.
4537  *
4538  * Since: 2.6
4539  */
4540 gint
4541 gtk_combo_box_get_wrap_width (GtkComboBox *combo_box)
4542 {
4543   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4544
4545   return combo_box->priv->wrap_width;
4546 }
4547
4548 /**
4549  * gtk_combo_box_set_wrap_width:
4550  * @combo_box: A #GtkComboBox
4551  * @width: Preferred number of columns
4552  *
4553  * Sets the wrap width of @combo_box to be @width. The wrap width is basically
4554  * the preferred number of columns when you want the popup to be layed out
4555  * in a table.
4556  *
4557  * Since: 2.4
4558  */
4559 void
4560 gtk_combo_box_set_wrap_width (GtkComboBox *combo_box,
4561                               gint         width)
4562 {
4563   GtkComboBoxPrivate *priv;
4564
4565   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4566   g_return_if_fail (width >= 0);
4567
4568   priv = combo_box->priv;
4569
4570   if (width != priv->wrap_width)
4571     {
4572       priv->wrap_width = width;
4573
4574       gtk_combo_box_check_appearance (combo_box);
4575       gtk_combo_box_relayout (combo_box);
4576       
4577       g_object_notify (G_OBJECT (combo_box), "wrap-width");
4578     }
4579 }
4580
4581 /**
4582  * gtk_combo_box_get_row_span_column:
4583  * @combo_box: A #GtkComboBox
4584  *
4585  * Returns the column with row span information for @combo_box.
4586  *
4587  * Returns: the row span column.
4588  *
4589  * Since: 2.6
4590  */
4591 gint
4592 gtk_combo_box_get_row_span_column (GtkComboBox *combo_box)
4593 {
4594   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4595
4596   return combo_box->priv->row_column;
4597 }
4598
4599 /**
4600  * gtk_combo_box_set_row_span_column:
4601  * @combo_box: A #GtkComboBox.
4602  * @row_span: A column in the model passed during construction.
4603  *
4604  * Sets the column with row span information for @combo_box to be @row_span.
4605  * The row span column contains integers which indicate how many rows
4606  * an item should span.
4607  *
4608  * Since: 2.4
4609  */
4610 void
4611 gtk_combo_box_set_row_span_column (GtkComboBox *combo_box,
4612                                    gint         row_span)
4613 {
4614   GtkComboBoxPrivate *priv;
4615   gint col;
4616
4617   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4618
4619   priv = combo_box->priv;
4620
4621   col = gtk_tree_model_get_n_columns (priv->model);
4622   g_return_if_fail (row_span >= -1 && row_span < col);
4623
4624   if (row_span != priv->row_column)
4625     {
4626       priv->row_column = row_span;
4627       
4628       gtk_combo_box_relayout (combo_box);
4629  
4630       g_object_notify (G_OBJECT (combo_box), "row-span-column");
4631     }
4632 }
4633
4634 /**
4635  * gtk_combo_box_get_column_span_column:
4636  * @combo_box: A #GtkComboBox
4637  *
4638  * Returns the column with column span information for @combo_box.
4639  *
4640  * Returns: the column span column.
4641  *
4642  * Since: 2.6
4643  */
4644 gint
4645 gtk_combo_box_get_column_span_column (GtkComboBox *combo_box)
4646 {
4647   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), -1);
4648
4649   return combo_box->priv->col_column;
4650 }
4651
4652 /**
4653  * gtk_combo_box_set_column_span_column:
4654  * @combo_box: A #GtkComboBox
4655  * @column_span: A column in the model passed during construction
4656  *
4657  * Sets the column with column span information for @combo_box to be
4658  * @column_span. The column span column contains integers which indicate
4659  * how many columns an item should span.
4660  *
4661  * Since: 2.4
4662  */
4663 void
4664 gtk_combo_box_set_column_span_column (GtkComboBox *combo_box,
4665                                       gint         column_span)
4666 {
4667   GtkComboBoxPrivate *priv;
4668   gint col;
4669
4670   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4671
4672   priv = combo_box->priv;
4673
4674   col = gtk_tree_model_get_n_columns (priv->model);
4675   g_return_if_fail (column_span >= -1 && column_span < col);
4676
4677   if (column_span != priv->col_column)
4678     {
4679       priv->col_column = column_span;
4680       
4681       gtk_combo_box_relayout (combo_box);
4682
4683       g_object_notify (G_OBJECT (combo_box), "column-span-column");
4684     }
4685 }
4686
4687 /**
4688  * gtk_combo_box_get_active:
4689  * @combo_box: A #GtkComboBox
4690  *
4691  * Returns the index of the currently active item, or -1 if there's no
4692  * active item. If the model is a non-flat treemodel, and the active item 
4693  * is not an immediate child of the root of the tree, this function returns 
4694  * <literal>gtk_tree_path_get_indices (path)[0]</literal>, where 
4695  * <literal>path</literal> is the #GtkTreePath of the active item.
4696  *
4697  * Return value: An integer which is the index of the currently active item, 
4698  *     or -1 if there's no active item.
4699  *
4700  * Since: 2.4
4701  */
4702 gint
4703 gtk_combo_box_get_active (GtkComboBox *combo_box)
4704 {
4705   GtkComboBoxPrivate *priv;
4706   gint result;
4707
4708   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), 0);
4709
4710   priv = combo_box->priv;
4711
4712   if (gtk_tree_row_reference_valid (priv->active_row))
4713     {
4714       GtkTreePath *path;
4715
4716       path = gtk_tree_row_reference_get_path (priv->active_row);      
4717       result = gtk_tree_path_get_indices (path)[0];
4718       gtk_tree_path_free (path);
4719     }
4720   else
4721     result = -1;
4722
4723   return result;
4724 }
4725
4726 /**
4727  * gtk_combo_box_set_active:
4728  * @combo_box: A #GtkComboBox
4729  * @index_: An index in the model passed during construction, or -1 to have
4730  * no active item
4731  *
4732  * Sets the active item of @combo_box to be the item at @index.
4733  *
4734  * Since: 2.4
4735  */
4736 void
4737 gtk_combo_box_set_active (GtkComboBox *combo_box,
4738                           gint         index_)
4739 {
4740   GtkTreePath *path = NULL;
4741   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4742   g_return_if_fail (index_ >= -1);
4743
4744   if (index_ != -1)
4745     path = gtk_tree_path_new_from_indices (index_, -1);
4746    
4747   gtk_combo_box_set_active_internal (combo_box, path);
4748
4749   if (path)
4750     gtk_tree_path_free (path);
4751 }
4752
4753 static void
4754 gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
4755                                    GtkTreePath *path)
4756 {
4757   GtkComboBoxPrivate *priv = combo_box->priv;
4758   GtkTreePath *active_path;
4759   gint path_cmp;
4760
4761   if (path && gtk_tree_row_reference_valid (priv->active_row))
4762     {
4763       active_path = gtk_tree_row_reference_get_path (priv->active_row);
4764       path_cmp = gtk_tree_path_compare (path, active_path);
4765       gtk_tree_path_free (active_path);
4766       if (path_cmp == 0)
4767         return;
4768     }
4769
4770   if (priv->active_row)
4771     {
4772       gtk_tree_row_reference_free (priv->active_row);
4773       priv->active_row = NULL;
4774     }
4775   
4776   if (!path)
4777     {
4778       if (priv->tree_view)
4779         gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view)));
4780       else
4781         {
4782           GtkMenu *menu = GTK_MENU (priv->popup_widget);
4783
4784           if (GTK_IS_MENU (menu))
4785             gtk_menu_set_active (menu, -1);
4786         }
4787
4788       if (priv->cell_view)
4789         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), NULL);
4790     }
4791   else
4792     {
4793       priv->active_row = 
4794         gtk_tree_row_reference_new (priv->model, path);
4795
4796       if (priv->tree_view)
4797         {
4798           gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->tree_view), 
4799                                     path, NULL, FALSE);
4800         }
4801       else if (GTK_IS_MENU (priv->popup_widget))
4802         {
4803           /* FIXME handle nested menus better */
4804           gtk_menu_set_active (GTK_MENU (priv->popup_widget), 
4805                                gtk_tree_path_get_indices (path)[0]);
4806         }
4807
4808       if (priv->cell_view)
4809         gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (priv->cell_view), 
4810                                          path);
4811     }
4812
4813   g_signal_emit (combo_box, combo_box_signals[CHANGED], 0);
4814   g_object_notify (G_OBJECT (combo_box), "active");
4815 }
4816
4817
4818 /**
4819  * gtk_combo_box_get_active_iter:
4820  * @combo_box: A #GtkComboBox
4821  * @iter: The uninitialized #GtkTreeIter
4822  * 
4823  * Sets @iter to point to the current active item, if it exists.
4824  * 
4825  * Return value: %TRUE, if @iter was set
4826  *
4827  * Since: 2.4
4828  */
4829 gboolean
4830 gtk_combo_box_get_active_iter (GtkComboBox     *combo_box,
4831                                GtkTreeIter     *iter)
4832 {
4833   GtkTreePath *path;
4834   gboolean result;
4835
4836   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
4837
4838   if (!gtk_tree_row_reference_valid (combo_box->priv->active_row))
4839     return FALSE;
4840
4841   path = gtk_tree_row_reference_get_path (combo_box->priv->active_row);
4842   result = gtk_tree_model_get_iter (combo_box->priv->model, iter, path);
4843   gtk_tree_path_free (path);
4844
4845   return result;
4846 }
4847
4848 /**
4849  * gtk_combo_box_set_active_iter:
4850  * @combo_box: A #GtkComboBox
4851  * @iter: The #GtkTreeIter
4852  * 
4853  * Sets the current active item to be the one referenced by @iter. 
4854  * @iter must correspond to a path of depth one.
4855  * 
4856  * Since: 2.4
4857  */
4858 void
4859 gtk_combo_box_set_active_iter (GtkComboBox     *combo_box,
4860                                GtkTreeIter     *iter)
4861 {
4862   GtkTreePath *path;
4863
4864   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4865
4866   path = gtk_tree_model_get_path (gtk_combo_box_get_model (combo_box), iter);
4867   gtk_combo_box_set_active_internal (combo_box, path);
4868   gtk_tree_path_free (path);
4869 }
4870
4871 /**
4872  * gtk_combo_box_set_model:
4873  * @combo_box: A #GtkComboBox
4874  * @model: A #GtkTreeModel
4875  *
4876  * Sets the model used by @combo_box to be @model. Will unset a previously set 
4877  * model (if applicable). If model is %NULL, then it will unset the model.
4878  *
4879  * Note that this function does not clear the cell renderers, you have to 
4880  * call gtk_cell_layout_clear() yourself if you need to set up different 
4881  * cell renderers for the new model.
4882  *
4883  * Since: 2.4
4884  */
4885 void
4886 gtk_combo_box_set_model (GtkComboBox  *combo_box,
4887                          GtkTreeModel *model)
4888 {
4889   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
4890   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4891
4892   if (model == combo_box->priv->model)
4893     return;
4894   
4895   gtk_combo_box_unset_model (combo_box);
4896
4897   if (model == NULL)
4898     goto out;
4899
4900   combo_box->priv->model = model;
4901   g_object_ref (combo_box->priv->model);
4902
4903   combo_box->priv->inserted_id =
4904     g_signal_connect (combo_box->priv->model, "row_inserted",
4905                       G_CALLBACK (gtk_combo_box_model_row_inserted),
4906                       combo_box);
4907   combo_box->priv->deleted_id =
4908     g_signal_connect (combo_box->priv->model, "row_deleted",
4909                       G_CALLBACK (gtk_combo_box_model_row_deleted),
4910                       combo_box);
4911   combo_box->priv->reordered_id =
4912     g_signal_connect (combo_box->priv->model, "rows_reordered",
4913                       G_CALLBACK (gtk_combo_box_model_rows_reordered),
4914                       combo_box);
4915   combo_box->priv->changed_id =
4916     g_signal_connect (combo_box->priv->model, "row_changed",
4917                       G_CALLBACK (gtk_combo_box_model_row_changed),
4918                       combo_box);
4919       
4920   if (combo_box->priv->tree_view)
4921     {
4922       /* list mode */
4923       gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
4924                                combo_box->priv->model);
4925       gtk_combo_box_list_popup_resize (combo_box);
4926     }
4927   else
4928     {
4929       /* menu mode */
4930       if (combo_box->priv->popup_widget)
4931         gtk_combo_box_menu_fill (combo_box);
4932
4933     }
4934
4935   if (combo_box->priv->cell_view)
4936     gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
4937                              combo_box->priv->model);
4938
4939 out:
4940   g_object_notify (G_OBJECT (combo_box), "model");
4941 }
4942
4943 /**
4944  * gtk_combo_box_get_model
4945  * @combo_box: A #GtkComboBox
4946  *
4947  * Returns the #GtkTreeModel which is acting as data source for @combo_box.
4948  *
4949  * Return value: A #GtkTreeModel which was passed during construction.
4950  *
4951  * Since: 2.4
4952  */
4953 GtkTreeModel *
4954 gtk_combo_box_get_model (GtkComboBox *combo_box)
4955 {
4956   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
4957
4958   return combo_box->priv->model;
4959 }
4960
4961
4962 /* convenience API for simple text combos */
4963
4964 /**
4965  * gtk_combo_box_new_text:
4966  *
4967  * Convenience function which constructs a new text combo box, which is a
4968  * #GtkComboBox just displaying strings. If you use this function to create
4969  * a text combo box, you should only manipulate its data source with the
4970  * following convenience functions: gtk_combo_box_append_text(),
4971  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
4972  * gtk_combo_box_remove_text().
4973  *
4974  * Return value: A new text combo box.
4975  *
4976  * Since: 2.4
4977  */
4978 GtkWidget *
4979 gtk_combo_box_new_text (void)
4980 {
4981   GtkWidget *combo_box;
4982   GtkCellRenderer *cell;
4983   GtkListStore *store;
4984
4985   store = gtk_list_store_new (1, G_TYPE_STRING);
4986   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
4987   g_object_unref (store);
4988
4989   cell = gtk_cell_renderer_text_new ();
4990   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
4991   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
4992                                   "text", 0,
4993                                   NULL);
4994
4995   return combo_box;
4996 }
4997
4998 /**
4999  * gtk_combo_box_append_text:
5000  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5001  * @text: A string
5002  *
5003  * Appends @string to the list of strings stored in @combo_box. Note that
5004  * you can only use this function with combo boxes constructed with
5005  * gtk_combo_box_new_text().
5006  *
5007  * Since: 2.4
5008  */
5009 void
5010 gtk_combo_box_append_text (GtkComboBox *combo_box,
5011                            const gchar *text)
5012 {
5013   GtkTreeIter iter;
5014   GtkListStore *store;
5015
5016   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5017   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5018   g_return_if_fail (text != NULL);
5019
5020   store = GTK_LIST_STORE (combo_box->priv->model);
5021
5022   gtk_list_store_append (store, &iter);
5023   gtk_list_store_set (store, &iter, 0, text, -1);
5024 }
5025
5026 /**
5027  * gtk_combo_box_insert_text:
5028  * @combo_box: A #GtkComboBox constructed using gtk_combo_box_new_text()
5029  * @position: An index to insert @text
5030  * @text: A string
5031  *
5032  * Inserts @string at @position in the list of strings stored in @combo_box.
5033  * Note that you can only use this function with combo boxes constructed
5034  * with gtk_combo_box_new_text().
5035  *
5036  * Since: 2.4
5037  */
5038 void
5039 gtk_combo_box_insert_text (GtkComboBox *combo_box,
5040                            gint         position,
5041                            const gchar *text)
5042 {
5043   GtkTreeIter iter;
5044   GtkListStore *store;
5045
5046   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5047   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5048   g_return_if_fail (position >= 0);
5049   g_return_if_fail (text != NULL);
5050
5051   store = GTK_LIST_STORE (combo_box->priv->model);
5052
5053   gtk_list_store_insert (store, &iter, position);
5054   gtk_list_store_set (store, &iter, 0, text, -1);
5055 }
5056
5057 /**
5058  * gtk_combo_box_prepend_text:
5059  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5060  * @text: A string
5061  *
5062  * Prepends @string to the list of strings stored in @combo_box. Note that
5063  * you can only use this function with combo boxes constructed with
5064  * gtk_combo_box_new_text().
5065  *
5066  * Since: 2.4
5067  */
5068 void
5069 gtk_combo_box_prepend_text (GtkComboBox *combo_box,
5070                             const gchar *text)
5071 {
5072   GtkTreeIter iter;
5073   GtkListStore *store;
5074
5075   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5076   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5077   g_return_if_fail (text != NULL);
5078
5079   store = GTK_LIST_STORE (combo_box->priv->model);
5080
5081   gtk_list_store_prepend (store, &iter);
5082   gtk_list_store_set (store, &iter, 0, text, -1);
5083 }
5084
5085 /**
5086  * gtk_combo_box_remove_text:
5087  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5088  * @position: Index of the item to remove
5089  *
5090  * Removes the string at @position from @combo_box. Note that you can only use
5091  * this function with combo boxes constructed with gtk_combo_box_new_text().
5092  *
5093  * Since: 2.4
5094  */
5095 void
5096 gtk_combo_box_remove_text (GtkComboBox *combo_box,
5097                            gint         position)
5098 {
5099   GtkTreeIter iter;
5100   GtkListStore *store;
5101
5102   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5103   g_return_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model));
5104   g_return_if_fail (position >= 0);
5105
5106   store = GTK_LIST_STORE (combo_box->priv->model);
5107
5108   if (gtk_tree_model_iter_nth_child (combo_box->priv->model, &iter,
5109                                      NULL, position))
5110     gtk_list_store_remove (store, &iter);
5111 }
5112
5113 /**
5114  * gtk_combo_box_get_active_text:
5115  * @combo_box: A #GtkComboBox constructed with gtk_combo_box_new_text()
5116  *
5117  * Returns the currently active string in @combo_box or %NULL if none
5118  * is selected.  Note that you can only use this function with combo
5119  * boxes constructed with gtk_combo_box_new_text() and with 
5120  * #GtkComboBoxEntry<!-- -->s.
5121  *
5122  * Returns: a newly allocated string containing the currently active text.
5123  *
5124  * Since: 2.6
5125  */
5126 gchar *
5127 gtk_combo_box_get_active_text (GtkComboBox *combo_box)
5128 {
5129   GtkComboBoxClass *class;
5130
5131   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5132
5133   class = GTK_COMBO_BOX_GET_CLASS (combo_box);
5134
5135   if (class->get_active_text)
5136     return (* class->get_active_text) (combo_box);
5137
5138   return NULL;
5139 }
5140
5141 static gchar *
5142 gtk_combo_box_real_get_active_text (GtkComboBox *combo_box)
5143 {
5144   GtkTreeIter iter;
5145   gchar *text = NULL;
5146
5147   g_return_val_if_fail (GTK_IS_LIST_STORE (combo_box->priv->model), NULL);
5148
5149   if (gtk_combo_box_get_active_iter (combo_box, &iter))
5150     gtk_tree_model_get (combo_box->priv->model, &iter, 
5151                         0, &text, -1);
5152
5153   return text;
5154 }
5155
5156 static void
5157 gtk_combo_box_real_move_active (GtkComboBox   *combo_box,
5158                                 GtkScrollType  scroll)
5159 {
5160   GtkTreeIter iter;
5161   GtkTreeIter new_iter;
5162   gboolean    active_iter;
5163   gboolean    found;
5164
5165   if (!combo_box->priv->model)
5166     {
5167       gtk_widget_error_bell (GTK_WIDGET (combo_box));
5168       return;
5169     }
5170
5171   active_iter = gtk_combo_box_get_active_iter (combo_box, &iter);
5172
5173   switch (scroll)
5174     {
5175     case GTK_SCROLL_STEP_BACKWARD:
5176     case GTK_SCROLL_STEP_UP:
5177     case GTK_SCROLL_STEP_LEFT:
5178       if (active_iter)
5179         {
5180           found = tree_prev (combo_box, combo_box->priv->model,
5181                              &iter, &new_iter, FALSE);
5182           break;
5183         }
5184       /* else fall through */
5185
5186     case GTK_SCROLL_PAGE_FORWARD:
5187     case GTK_SCROLL_PAGE_DOWN:
5188     case GTK_SCROLL_PAGE_RIGHT:
5189     case GTK_SCROLL_END:
5190       found = tree_last (combo_box, combo_box->priv->model, &new_iter, FALSE);
5191       break;
5192
5193     case GTK_SCROLL_STEP_FORWARD:
5194     case GTK_SCROLL_STEP_DOWN:
5195     case GTK_SCROLL_STEP_RIGHT:
5196       if (active_iter)
5197         {
5198           found = tree_next (combo_box, combo_box->priv->model,
5199                              &iter, &new_iter, FALSE);
5200           break;
5201         }
5202       /* else fall through */
5203
5204     case GTK_SCROLL_PAGE_BACKWARD:
5205     case GTK_SCROLL_PAGE_UP:
5206     case GTK_SCROLL_PAGE_LEFT:
5207     case GTK_SCROLL_START:
5208       found = tree_first (combo_box, combo_box->priv->model, &new_iter, FALSE);
5209       break;
5210
5211     default:
5212       return;
5213     }
5214
5215   if (found && active_iter)
5216     {
5217       GtkTreePath *old_path;
5218       GtkTreePath *new_path;
5219
5220       old_path = gtk_tree_model_get_path (combo_box->priv->model, &iter);
5221       new_path = gtk_tree_model_get_path (combo_box->priv->model, &new_iter);
5222
5223       if (gtk_tree_path_compare (old_path, new_path) == 0)
5224         found = FALSE;
5225
5226       gtk_tree_path_free (old_path);
5227       gtk_tree_path_free (new_path);
5228     }
5229
5230   if (found)
5231     {
5232       gtk_combo_box_set_active_iter (combo_box, &new_iter);
5233     }
5234   else
5235     {
5236       gtk_widget_error_bell (GTK_WIDGET (combo_box));
5237     }
5238 }
5239
5240 static gboolean
5241 gtk_combo_box_mnemonic_activate (GtkWidget *widget,
5242                                  gboolean   group_cycling)
5243 {
5244   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5245
5246   gtk_widget_grab_focus (combo_box->priv->button);
5247
5248   return TRUE;
5249 }
5250
5251 static void
5252 gtk_combo_box_grab_focus (GtkWidget *widget)
5253 {
5254   GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
5255
5256   gtk_widget_grab_focus (combo_box->priv->button);
5257 }
5258
5259 static void
5260 gtk_combo_box_destroy (GtkObject *object)
5261 {
5262   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5263
5264   if (combo_box->priv->popup_idle_id > 0)
5265     {
5266       g_source_remove (combo_box->priv->popup_idle_id);
5267       combo_box->priv->popup_idle_id = 0;
5268     }
5269
5270   gtk_combo_box_popdown (combo_box);
5271
5272   if (combo_box->priv->row_separator_destroy)
5273     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5274
5275   combo_box->priv->row_separator_func = NULL;
5276   combo_box->priv->row_separator_data = NULL;
5277   combo_box->priv->row_separator_destroy = NULL;
5278
5279   GTK_OBJECT_CLASS (gtk_combo_box_parent_class)->destroy (object);
5280   combo_box->priv->cell_view = NULL;
5281 }
5282
5283 static void
5284 gtk_combo_box_dispose(GObject* object)
5285 {
5286   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5287
5288   if (GTK_IS_MENU (combo_box->priv->popup_widget))
5289     {
5290       gtk_combo_box_menu_destroy (combo_box);
5291       gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
5292       combo_box->priv->popup_widget = NULL;
5293     }
5294
5295   G_OBJECT_CLASS (gtk_combo_box_parent_class)->dispose (object);
5296 }
5297
5298 static void
5299 gtk_combo_box_finalize (GObject *object)
5300 {
5301   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
5302   GSList *i;
5303   
5304   if (GTK_IS_TREE_VIEW (combo_box->priv->tree_view))
5305     gtk_combo_box_list_destroy (combo_box);
5306
5307   if (combo_box->priv->popup_window)
5308     gtk_widget_destroy (combo_box->priv->popup_window);
5309
5310   gtk_combo_box_unset_model (combo_box);
5311
5312   for (i = combo_box->priv->cells; i; i = i->next)
5313     {
5314       ComboCellInfo *info = (ComboCellInfo *)i->data;
5315       GSList *list = info->attributes;
5316
5317       if (info->destroy)
5318         info->destroy (info->func_data);
5319
5320       while (list && list->next)
5321         {
5322           g_free (list->data);
5323           list = list->next->next;
5324         }
5325       g_slist_free (info->attributes);
5326
5327       g_object_unref (info->cell);
5328       g_free (info);
5329     }
5330    g_slist_free (combo_box->priv->cells);
5331
5332    g_free (combo_box->priv->tearoff_title);
5333
5334    G_OBJECT_CLASS (gtk_combo_box_parent_class)->finalize (object);
5335 }
5336
5337 static gboolean
5338 gtk_cell_editable_key_press (GtkWidget   *widget,
5339                              GdkEventKey *event,
5340                              gpointer     data)
5341 {
5342   GtkComboBox *combo_box = GTK_COMBO_BOX (data);
5343
5344   if (event->keyval == GDK_Escape)
5345     {
5346       combo_box->priv->editing_canceled = TRUE;
5347
5348       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5349       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5350       
5351       return TRUE;
5352     }
5353   else if (event->keyval == GDK_Return ||
5354            event->keyval == GDK_ISO_Enter ||
5355            event->keyval == GDK_KP_Enter)
5356     {
5357       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5358       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5359       
5360       return TRUE;
5361     }
5362
5363   return FALSE;
5364 }
5365
5366 static gboolean
5367 popdown_idle (gpointer data)
5368 {
5369   GtkComboBox *combo_box;
5370
5371   combo_box = GTK_COMBO_BOX (data);
5372   
5373   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo_box));
5374   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo_box));
5375
5376   g_object_unref (combo_box);
5377
5378   return FALSE;
5379 }
5380
5381 static void
5382 popdown_handler (GtkWidget *widget,
5383                  gpointer   data)
5384 {
5385   gdk_threads_add_idle (popdown_idle, g_object_ref (data));
5386 }
5387
5388 static gboolean
5389 popup_idle (gpointer data)
5390 {
5391   GtkComboBox *combo_box;
5392
5393   combo_box = GTK_COMBO_BOX (data);
5394
5395   if (GTK_IS_MENU (combo_box->priv->popup_widget) &&
5396       combo_box->priv->cell_view)
5397     g_signal_connect_object (combo_box->priv->popup_widget,
5398                              "unmap", G_CALLBACK (popdown_handler),
5399                              combo_box, 0);
5400   
5401   /* we unset this if a menu item is activated */
5402   combo_box->priv->editing_canceled = TRUE;
5403   gtk_combo_box_popup (combo_box);
5404
5405   combo_box->priv->popup_idle_id = 0;
5406   combo_box->priv->activate_button = 0;
5407   combo_box->priv->activate_time = 0;
5408
5409   return FALSE;
5410 }
5411
5412 static void
5413 gtk_combo_box_start_editing (GtkCellEditable *cell_editable,
5414                              GdkEvent        *event)
5415 {
5416   GtkComboBox *combo_box = GTK_COMBO_BOX (cell_editable);
5417
5418   combo_box->priv->is_cell_renderer = TRUE;
5419
5420   if (combo_box->priv->cell_view)
5421     {
5422       g_signal_connect_object (combo_box->priv->button, "key_press_event",
5423                                G_CALLBACK (gtk_cell_editable_key_press), 
5424                                cell_editable, 0);  
5425
5426       gtk_widget_grab_focus (combo_box->priv->button);
5427     }
5428   else
5429     {
5430       g_signal_connect_object (GTK_BIN (combo_box)->child, "key_press_event",
5431                                G_CALLBACK (gtk_cell_editable_key_press), 
5432                                cell_editable, 0);  
5433
5434       gtk_widget_grab_focus (GTK_WIDGET (GTK_BIN (combo_box)->child));
5435       GTK_WIDGET_UNSET_FLAGS (combo_box->priv->button, GTK_CAN_FOCUS);
5436     }
5437
5438   /* we do the immediate popup only for the optionmenu-like 
5439    * appearance 
5440    */  
5441   if (combo_box->priv->is_cell_renderer && 
5442       combo_box->priv->cell_view && !combo_box->priv->tree_view)
5443     {
5444       if (event && event->type == GDK_BUTTON_PRESS)
5445         {
5446           GdkEventButton *event_button = (GdkEventButton *)event;
5447
5448           combo_box->priv->activate_button = event_button->button;
5449           combo_box->priv->activate_time = event_button->time;
5450         }
5451
5452       combo_box->priv->popup_idle_id = 
5453           gdk_threads_add_idle (popup_idle, combo_box);
5454     }
5455 }
5456
5457
5458 /**
5459  * gtk_combo_box_get_add_tearoffs:
5460  * @combo_box: a #GtkComboBox
5461  * 
5462  * Gets the current value of the :add-tearoffs property.
5463  * 
5464  * Return value: the current value of the :add-tearoffs property.
5465  */
5466 gboolean
5467 gtk_combo_box_get_add_tearoffs (GtkComboBox *combo_box)
5468 {
5469   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5470
5471   return combo_box->priv->add_tearoffs;
5472 }
5473
5474 /**
5475  * gtk_combo_box_set_add_tearoffs:
5476  * @combo_box: a #GtkComboBox 
5477  * @add_tearoffs: %TRUE to add tearoff menu items
5478  *  
5479  * Sets whether the popup menu should have a tearoff 
5480  * menu item.
5481  *
5482  * Since: 2.6
5483  */
5484 void
5485 gtk_combo_box_set_add_tearoffs (GtkComboBox *combo_box,
5486                                 gboolean     add_tearoffs)
5487 {
5488   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5489
5490   add_tearoffs = add_tearoffs != FALSE;
5491
5492   if (combo_box->priv->add_tearoffs != add_tearoffs)
5493     {
5494       combo_box->priv->add_tearoffs = add_tearoffs;
5495       gtk_combo_box_check_appearance (combo_box);
5496       gtk_combo_box_relayout (combo_box);
5497       g_object_notify (G_OBJECT (combo_box), "add-tearoffs");
5498     }
5499 }
5500
5501 /**
5502  * gtk_combo_box_get_title:
5503  * @combo_box: a #GtkComboBox
5504  *
5505  * Gets the current title of the menu in tearoff mode. See
5506  * gtk_combo_box_set_add_tearoffs().
5507  *
5508  * Returns: the menu's title in tearoff mode. This is an internal copy of the
5509  * string which must not be freed.
5510  *
5511  * Since: 2.10
5512  */
5513 G_CONST_RETURN gchar*
5514 gtk_combo_box_get_title (GtkComboBox *combo_box)
5515 {
5516   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5517   
5518   return combo_box->priv->tearoff_title;
5519 }
5520
5521 static void
5522 gtk_combo_box_update_title (GtkComboBox *combo_box)
5523 {
5524   gtk_combo_box_check_appearance (combo_box);
5525   
5526   if (combo_box->priv->popup_widget && 
5527       GTK_IS_MENU (combo_box->priv->popup_widget))
5528     gtk_menu_set_title (GTK_MENU (combo_box->priv->popup_widget), 
5529                         combo_box->priv->tearoff_title);
5530 }
5531
5532 /**
5533  * gtk_combo_box_set_title:
5534  * @combo_box: a #GtkComboBox 
5535  * @title: a title for the menu in tearoff mode
5536  *  
5537  * Sets the menu's title in tearoff mode.
5538  *
5539  * Since: 2.10
5540  */
5541 void
5542 gtk_combo_box_set_title (GtkComboBox *combo_box,
5543                          const gchar *title)
5544 {
5545   GtkComboBoxPrivate *priv;
5546
5547   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5548
5549   priv = combo_box->priv;
5550
5551   if (strcmp (title ? title : "", 
5552               priv->tearoff_title ? priv->tearoff_title : "") != 0)
5553     {
5554       g_free (priv->tearoff_title);
5555       priv->tearoff_title = g_strdup (title);
5556
5557       gtk_combo_box_update_title (combo_box);
5558
5559       g_object_notify (G_OBJECT (combo_box), "tearoff-title");
5560     }
5561 }
5562
5563 gboolean
5564 _gtk_combo_box_editing_canceled (GtkComboBox *combo_box)
5565 {
5566   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), TRUE);
5567
5568   return combo_box->priv->editing_canceled;
5569 }
5570
5571 /**
5572  * gtk_combo_box_get_popup_accessible:
5573  * @combo_box: a #GtkComboBox
5574  *
5575  * Gets the accessible object corresponding to the combo box's popup.
5576  *
5577  * This function is mostly intended for use by accessibility technologies;
5578  * applications should have little use for it.
5579  *
5580  * Returns: the accessible object corresponding to the combo box's popup.
5581  *
5582  * Since: 2.6
5583  */
5584 AtkObject*
5585 gtk_combo_box_get_popup_accessible (GtkComboBox *combo_box)
5586 {
5587   AtkObject *atk_obj;
5588
5589   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5590
5591   if (combo_box->priv->popup_widget)
5592     {
5593       atk_obj = gtk_widget_get_accessible (combo_box->priv->popup_widget);
5594       return atk_obj;
5595     }
5596
5597   return NULL;
5598 }
5599
5600 /**
5601  * gtk_combo_box_get_row_separator_func:
5602  * @combo_box: a #GtkComboBox
5603  * 
5604  * Returns the current row separator function.
5605  * 
5606  * Return value: the current row separator function.
5607  *
5608  * Since: 2.6
5609  */
5610 GtkTreeViewRowSeparatorFunc 
5611 gtk_combo_box_get_row_separator_func (GtkComboBox *combo_box)
5612 {
5613   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), NULL);
5614
5615   return combo_box->priv->row_separator_func;
5616 }
5617
5618 /**
5619  * gtk_combo_box_set_row_separator_func:
5620  * @combo_box: a #GtkComboBox
5621  * @func: a #GtkTreeViewRowSeparatorFunc
5622  * @data: user data to pass to @func, or %NULL
5623  * @destroy: destroy notifier for @data, or %NULL
5624  * 
5625  * Sets the row separator function, which is used to determine
5626  * whether a row should be drawn as a separator. If the row separator
5627  * function is %NULL, no separators are drawn. This is the default value.
5628  *
5629  * Since: 2.6
5630  */
5631 void
5632 gtk_combo_box_set_row_separator_func (GtkComboBox                 *combo_box,
5633                                       GtkTreeViewRowSeparatorFunc  func,
5634                                       gpointer                     data,
5635                                       GtkDestroyNotify             destroy)
5636 {
5637   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5638
5639   if (combo_box->priv->row_separator_destroy)
5640     (* combo_box->priv->row_separator_destroy) (combo_box->priv->row_separator_data);
5641
5642   combo_box->priv->row_separator_func = func;
5643   combo_box->priv->row_separator_data = data;
5644   combo_box->priv->row_separator_destroy = destroy;
5645
5646   if (combo_box->priv->tree_view)
5647     gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (combo_box->priv->tree_view), 
5648                                           func, data, NULL);
5649
5650   gtk_combo_box_relayout (combo_box);
5651
5652   gtk_widget_queue_draw (GTK_WIDGET (combo_box));
5653 }
5654
5655
5656 /**
5657  * gtk_combo_box_set_focus_on_click:
5658  * @combo: a #GtkComboBox
5659  * @focus_on_click: whether the combo box grabs focus when clicked 
5660  *    with the mouse
5661  * 
5662  * Sets whether the combo box will grab focus when it is clicked with 
5663  * the mouse. Making mouse clicks not grab focus is useful in places 
5664  * like toolbars where you don't want the keyboard focus removed from 
5665  * the main area of the application.
5666  *
5667  * Since: 2.6
5668  */
5669 void
5670 gtk_combo_box_set_focus_on_click (GtkComboBox *combo_box,
5671                                   gboolean     focus_on_click)
5672 {
5673   g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
5674   
5675   focus_on_click = focus_on_click != FALSE;
5676
5677   if (combo_box->priv->focus_on_click != focus_on_click)
5678     {
5679       combo_box->priv->focus_on_click = focus_on_click;
5680
5681       if (combo_box->priv->button)
5682         gtk_button_set_focus_on_click (GTK_BUTTON (combo_box->priv->button),
5683                                        focus_on_click);
5684       
5685       g_object_notify (G_OBJECT (combo_box), "focus-on-click");
5686     }
5687 }
5688
5689 /**
5690  * gtk_combo_box_get_focus_on_click:
5691  * @combo: a #GtkComboBox
5692  * 
5693  * Returns whether the combo box grabs focus when it is clicked 
5694  * with the mouse. See gtk_combo_box_set_focus_on_click().
5695  *
5696  * Return value: %TRUE if the combo box grabs focus when it is 
5697  *     clicked with the mouse.
5698  *
5699  * Since: 2.6
5700  */
5701 gboolean
5702 gtk_combo_box_get_focus_on_click (GtkComboBox *combo_box)
5703 {
5704   g_return_val_if_fail (GTK_IS_COMBO_BOX (combo_box), FALSE);
5705   
5706   return combo_box->priv->focus_on_click;
5707 }
5708
5709
5710 static gboolean
5711 gtk_combo_box_buildable_custom_tag_start (GtkBuildable  *buildable,
5712                                           GtkBuilder    *builder,
5713                                           GObject       *child,
5714                                           const gchar   *tagname,
5715                                           GMarkupParser *parser,
5716                                           gpointer      *data)
5717 {
5718   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
5719                                                 tagname, parser, data))
5720     return TRUE;
5721
5722   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
5723                                                       tagname, parser, data);
5724 }
5725
5726 static void
5727 gtk_combo_box_buildable_custom_tag_end (GtkBuildable *buildable,
5728                                         GtkBuilder   *builder,
5729                                         GObject      *child,
5730                                         const gchar  *tagname,
5731                                         gpointer     *data)
5732 {
5733   if (strcmp (tagname, "attributes") == 0)
5734     _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
5735                                                data);
5736   else
5737     parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
5738                                             data);
5739 }
5740
5741 #define __GTK_COMBO_BOX_C__
5742 #include "gtkaliasdef.c"