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