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