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