]> Pileus Git - ~andy/gtk/blob - gtk/gtktreemenu.c
169af2735ed19cd28161178355e69b84044d2df7
[~andy/gtk] / gtk / gtktreemenu.c
1 /* gtktreemenu.c
2  *
3  * Copyright (C) 2010 Openismus GmbH
4  *
5  * Authors:
6  *      Tristan Van Berkom <tristanvb@openismus.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gtktreemenu
26  * @Short_Description: A GtkMenu automatically created from a #GtkTreeModel
27  * @Title: GtkTreeMenu
28  *
29  * The #GtkTreeMenu is used to display a drop-down menu allowing selection 
30  * of every row in the model and is used by the #GtkComboBox for its drop-down
31  * menu.
32  */
33
34 #include "config.h"
35 #include "gtkintl.h"
36 #include "gtktreemenu.h"
37 #include "gtkmarshalers.h"
38 #include "gtkmenuitem.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtkcellareabox.h"
42 #include "gtkcellareacontext.h"
43 #include "gtkcelllayout.h"
44 #include "gtkcellview.h"
45 #include "gtkprivate.h"
46
47
48 /* GObjectClass */
49 static GObject  *gtk_tree_menu_constructor                    (GType                  type,
50                                                                guint                  n_construct_properties,
51                                                                GObjectConstructParam *construct_properties);
52 static void      gtk_tree_menu_dispose                        (GObject            *object);
53 static void      gtk_tree_menu_finalize                       (GObject            *object);
54 static void      gtk_tree_menu_set_property                   (GObject            *object,
55                                                                guint               prop_id,
56                                                                const GValue       *value,
57                                                                GParamSpec         *pspec);
58 static void      gtk_tree_menu_get_property                   (GObject            *object,
59                                                                guint               prop_id,
60                                                                GValue             *value,
61                                                                GParamSpec         *pspec);
62
63 /* GtkWidgetClass */
64 static void      gtk_tree_menu_get_preferred_width            (GtkWidget           *widget,
65                                                                gint                *minimum_size,
66                                                                gint                *natural_size);
67 static void      gtk_tree_menu_get_preferred_height           (GtkWidget           *widget,
68                                                                gint                *minimum_size,
69                                                                gint                *natural_size);
70
71 /* GtkCellLayoutIface */
72 static void      gtk_tree_menu_cell_layout_init               (GtkCellLayoutIface  *iface);
73 static GtkCellArea *gtk_tree_menu_cell_layout_get_area        (GtkCellLayout        *layout);
74
75
76 /* TreeModel/DrawingArea callbacks and building menus/submenus */
77 static inline void rebuild_menu                               (GtkTreeMenu          *menu);
78 static gboolean   menu_occupied                               (GtkTreeMenu          *menu,
79                                                                guint                 left_attach,
80                                                                guint                 right_attach,
81                                                                guint                 top_attach,
82                                                                guint                 bottom_attach);
83 static void       relayout_item                               (GtkTreeMenu          *menu,
84                                                                GtkWidget            *item,
85                                                                GtkTreeIter          *iter,
86                                                                GtkWidget            *prev);
87 static void       gtk_tree_menu_populate                      (GtkTreeMenu          *menu);
88 static GtkWidget *gtk_tree_menu_create_item                   (GtkTreeMenu          *menu,
89                                                                GtkTreeIter          *iter,
90                                                                gboolean              header_item);
91 static void       gtk_tree_menu_create_submenu                (GtkTreeMenu          *menu,
92                                                                GtkWidget            *item,
93                                                                GtkTreePath          *path);
94 static void       gtk_tree_menu_set_area                      (GtkTreeMenu          *menu,
95                                                                GtkCellArea          *area);
96 static GtkWidget *gtk_tree_menu_get_path_item                 (GtkTreeMenu          *menu,
97                                                                GtkTreePath          *path);
98 static gboolean   gtk_tree_menu_path_in_menu                  (GtkTreeMenu          *menu,
99                                                                GtkTreePath          *path,
100                                                                gboolean             *header_item);
101 static void       row_inserted_cb                             (GtkTreeModel         *model,
102                                                                GtkTreePath          *path,
103                                                                GtkTreeIter          *iter,
104                                                                GtkTreeMenu          *menu);
105 static void       row_deleted_cb                              (GtkTreeModel         *model,
106                                                                GtkTreePath          *path,
107                                                                GtkTreeMenu          *menu);
108 static void       row_reordered_cb                            (GtkTreeModel         *model,
109                                                                GtkTreePath          *path,
110                                                                GtkTreeIter          *iter,
111                                                                gint                 *new_order,
112                                                                GtkTreeMenu          *menu);
113 static void       row_changed_cb                              (GtkTreeModel         *model,
114                                                                GtkTreePath          *path,
115                                                                GtkTreeIter          *iter,
116                                                                GtkTreeMenu          *menu);
117 static void       context_size_changed_cb                     (GtkCellAreaContext   *context,
118                                                                GParamSpec           *pspec,
119                                                                GtkWidget            *menu);
120 static void       area_apply_attributes_cb                    (GtkCellArea          *area,
121                                                                GtkTreeModel         *tree_model,
122                                                                GtkTreeIter          *iter,
123                                                                gboolean              is_expander,
124                                                                gboolean              is_expanded,
125                                                                GtkTreeMenu          *menu);
126 static void       item_activated_cb                           (GtkMenuItem          *item,
127                                                                GtkTreeMenu          *menu);
128 static void       submenu_activated_cb                        (GtkTreeMenu          *submenu,
129                                                                const gchar          *path,
130                                                                GtkTreeMenu          *menu);
131 static void       gtk_tree_menu_set_model_internal            (GtkTreeMenu          *menu,
132                                                                GtkTreeModel         *model);
133
134
135
136 struct _GtkTreeMenuPrivate
137 {
138   /* TreeModel and parent for this menu */
139   GtkTreeModel        *model;
140   GtkTreeRowReference *root;
141
142   /* CellArea and context for this menu */
143   GtkCellArea         *area;
144   GtkCellAreaContext  *context;
145   
146   /* Signals */
147   gulong               size_changed_id;
148   gulong               apply_attributes_id;
149   gulong               row_inserted_id;
150   gulong               row_deleted_id;
151   gulong               row_reordered_id;
152   gulong               row_changed_id;
153
154   /* Grid menu mode */
155   gint                 wrap_width;
156   gint                 row_span_col;
157   gint                 col_span_col;
158
159   /* Flags */
160   guint32              menu_with_header : 1;
161   guint32              tearoff     : 1;
162
163   /* Row separators */
164   GtkTreeViewRowSeparatorFunc row_separator_func;
165   gpointer                    row_separator_data;
166   GDestroyNotify              row_separator_destroy;
167
168   /* Submenu headers */
169   GtkTreeMenuHeaderFunc header_func;
170   gpointer              header_data;
171   GDestroyNotify        header_destroy;
172 };
173
174 enum {
175   PROP_0,
176   PROP_MODEL,
177   PROP_ROOT,
178   PROP_CELL_AREA,
179   PROP_TEAROFF,
180   PROP_WRAP_WIDTH,
181   PROP_ROW_SPAN_COL,
182   PROP_COL_SPAN_COL
183 };
184
185 enum {
186   SIGNAL_MENU_ACTIVATE,
187   N_SIGNALS
188 };
189
190 static guint   tree_menu_signals[N_SIGNALS] = { 0 };
191 static GQuark  tree_menu_path_quark = 0;
192
193 G_DEFINE_TYPE_WITH_CODE (GtkTreeMenu, gtk_tree_menu, GTK_TYPE_MENU,
194                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
195                                                 gtk_tree_menu_cell_layout_init));
196
197 static void
198 gtk_tree_menu_init (GtkTreeMenu *menu)
199 {
200   GtkTreeMenuPrivate *priv;
201
202   menu->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu,
203                                             GTK_TYPE_TREE_MENU,
204                                             GtkTreeMenuPrivate);
205   priv = menu->priv;
206
207   priv->model     = NULL;
208   priv->root      = NULL;
209   priv->area      = NULL;
210   priv->context   = NULL;
211
212   priv->size_changed_id  = 0;
213   priv->row_inserted_id  = 0;
214   priv->row_deleted_id   = 0;
215   priv->row_reordered_id = 0;
216   priv->row_changed_id   = 0;
217
218   priv->wrap_width   = 0;
219   priv->row_span_col = -1;
220   priv->col_span_col = -1;
221
222   priv->menu_with_header = FALSE;
223   priv->tearoff          = FALSE;
224
225   priv->row_separator_func    = NULL;
226   priv->row_separator_data    = NULL;
227   priv->row_separator_destroy = NULL;
228
229   priv->header_func    = NULL;
230   priv->header_data    = NULL;
231   priv->header_destroy = NULL;
232
233   gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
234 }
235
236 static void 
237 gtk_tree_menu_class_init (GtkTreeMenuClass *class)
238 {
239   GObjectClass   *object_class = G_OBJECT_CLASS (class);
240   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
241
242   tree_menu_path_quark = g_quark_from_static_string ("gtk-tree-menu-path");
243
244   object_class->constructor  = gtk_tree_menu_constructor;
245   object_class->dispose      = gtk_tree_menu_dispose;
246   object_class->finalize     = gtk_tree_menu_finalize;
247   object_class->set_property = gtk_tree_menu_set_property;
248   object_class->get_property = gtk_tree_menu_get_property;
249
250   widget_class->get_preferred_width  = gtk_tree_menu_get_preferred_width;
251   widget_class->get_preferred_height = gtk_tree_menu_get_preferred_height;
252
253   /**
254    * GtkTreeMenu::menu-activate:
255    * @menu: a #GtkTreeMenu
256    * @path: the #GtkTreePath string for the item which was activated
257    * @user_data: the user data
258    *
259    * This signal is emitted to notify that a menu item in the #GtkTreeMenu
260    * was activated and provides the path string from the #GtkTreeModel
261    * to specify which row was selected.
262    *
263    * Since: 3.0
264    */
265   tree_menu_signals[SIGNAL_MENU_ACTIVATE] =
266     g_signal_new (I_("menu-activate"),
267                   G_OBJECT_CLASS_TYPE (object_class),
268                   G_SIGNAL_RUN_FIRST,
269                   0, /* No class closure here */
270                   NULL, NULL,
271                   _gtk_marshal_VOID__STRING,
272                   G_TYPE_NONE, 1, G_TYPE_STRING);
273
274   /**
275    * GtkTreeMenu:model:
276    *
277    * The #GtkTreeModel from which the menu is constructed.
278    * 
279    * Since: 3.0
280    */
281   g_object_class_install_property (object_class,
282                                    PROP_MODEL,
283                                    g_param_spec_object ("model",
284                                                         P_("TreeMenu model"),
285                                                         P_("The model for the tree menu"),
286                                                         GTK_TYPE_TREE_MODEL,
287                                                         GTK_PARAM_READWRITE));
288
289   /**
290    * GtkTreeMenu:root:
291    *
292    * The #GtkTreePath that is the root for this menu, or %NULL.
293    *
294    * The #GtkTreeMenu recursively creates submenus for #GtkTreeModel
295    * rows that have children and the "root" for each menu is provided
296    * by the parent menu.
297    *
298    * If you dont provide a root for the #GtkTreeMenu then the whole
299    * model will be added to the menu. Specifying a root allows you
300    * to build a menu for a given #GtkTreePath and it's children.
301    * 
302    * Since: 3.0
303    */
304   g_object_class_install_property (object_class,
305                                    PROP_ROOT,
306                                    g_param_spec_boxed ("root",
307                                                        P_("TreeMenu root row"),
308                                                        P_("The TreeMenu will display children of the "
309                                                           "specified root"),
310                                                        GTK_TYPE_TREE_PATH,
311                                                        GTK_PARAM_READWRITE));
312
313   /**
314    * GtkTreeMenu:cell-area:
315    *
316    * The #GtkCellArea used to render cells in the menu items.
317    *
318    * You can provide a different cell area at object construction
319    * time, otherwise the #GtkTreeMenu will use a #GtkCellAreaBox.
320    *
321    * Since: 3.0
322    */
323   g_object_class_install_property (object_class,
324                                    PROP_CELL_AREA,
325                                    g_param_spec_object ("cell-area",
326                                                         P_("Cell Area"),
327                                                         P_("The GtkCellArea used to layout cells"),
328                                                         GTK_TYPE_CELL_AREA,
329                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
330
331   /**
332    * GtkTreeMenu:tearoff:
333    *
334    * Specifies whether this menu comes with a leading tearoff menu item
335    *
336    * Since: 3.0
337    */
338   g_object_class_install_property (object_class,
339                                    PROP_TEAROFF,
340                                    g_param_spec_boolean ("tearoff",
341                                                          P_("Tearoff"),
342                                                          P_("Whether the menu has a tearoff item"),
343                                                          FALSE,
344                                                          GTK_PARAM_READWRITE));
345
346   /**
347    * GtkTreeMenu:wrap-width:
348    *
349    * If wrap-width is set to a positive value, the list will be
350    * displayed in multiple columns, the number of columns is
351    * determined by wrap-width.
352    *
353    * Since: 3.0
354    */
355   g_object_class_install_property (object_class,
356                                    PROP_WRAP_WIDTH,
357                                    g_param_spec_int ("wrap-width",
358                                                      P_("Wrap Width"),
359                                                      P_("Wrap width for laying out items in a grid"),
360                                                      0,
361                                                      G_MAXINT,
362                                                      0,
363                                                      GTK_PARAM_READWRITE));
364
365   /**
366    * GtkTreeMenu:row-span-column:
367    *
368    * If this is set to a non-negative value, it must be the index of a column 
369    * of type %G_TYPE_INT in the model. 
370    *
371    * The values of that column are used to determine how many rows a value in 
372    * the list will span. Therefore, the values in the model column pointed to 
373    * by this property must be greater than zero and not larger than wrap-width.
374    *
375    * Since: 3.0
376    */
377   g_object_class_install_property (object_class,
378                                    PROP_ROW_SPAN_COL,
379                                    g_param_spec_int ("row-span-column",
380                                                      P_("Row span column"),
381                                                      P_("TreeModel column containing the row span values"),
382                                                      -1,
383                                                      G_MAXINT,
384                                                      -1,
385                                                      GTK_PARAM_READWRITE));
386
387   /**
388    * GtkTreeMenu:column-span-column:
389    *
390    * If this is set to a non-negative value, it must be the index of a column 
391    * of type %G_TYPE_INT in the model. 
392    *
393    * The values of that column are used to determine how many columns a value 
394    * in the list will span. 
395    *
396    * Since: 3.0
397    */
398   g_object_class_install_property (object_class,
399                                    PROP_COL_SPAN_COL,
400                                    g_param_spec_int ("column-span-column",
401                                                      P_("Column span column"),
402                                                      P_("TreeModel column containing the column span values"),
403                                                      -1,
404                                                      G_MAXINT,
405                                                      -1,
406                                                      GTK_PARAM_READWRITE));
407
408   g_type_class_add_private (object_class, sizeof (GtkTreeMenuPrivate));
409 }
410
411 /****************************************************************
412  *                         GObjectClass                         *
413  ****************************************************************/
414 static GObject  *
415 gtk_tree_menu_constructor (GType                  type,
416                            guint                  n_construct_properties,
417                            GObjectConstructParam *construct_properties)
418 {
419   GObject            *object;
420   GtkTreeMenu        *menu;
421   GtkTreeMenuPrivate *priv;
422
423   object = G_OBJECT_CLASS (gtk_tree_menu_parent_class)->constructor
424     (type, n_construct_properties, construct_properties);
425
426   menu = GTK_TREE_MENU (object);
427   priv = menu->priv;
428
429   if (!priv->area)
430     {
431       GtkCellArea *area = gtk_cell_area_box_new ();
432
433       gtk_tree_menu_set_area (menu, area);
434     }
435
436   priv->context = gtk_cell_area_create_context (priv->area);
437
438   priv->size_changed_id = 
439     g_signal_connect (priv->context, "notify",
440                       G_CALLBACK (context_size_changed_cb), menu);
441
442   return object;
443 }
444
445 static void
446 gtk_tree_menu_dispose (GObject *object)
447 {
448   GtkTreeMenu        *menu;
449   GtkTreeMenuPrivate *priv;
450
451   menu = GTK_TREE_MENU (object);
452   priv = menu->priv;
453
454   gtk_tree_menu_set_model (menu, NULL);
455   gtk_tree_menu_set_area (menu, NULL);
456
457   if (priv->context)
458     {
459       /* Disconnect signals */
460       g_signal_handler_disconnect (priv->context, priv->size_changed_id);
461
462       g_object_unref (priv->context);
463       priv->context = NULL;
464       priv->size_changed_id = 0;
465     }
466
467   G_OBJECT_CLASS (gtk_tree_menu_parent_class)->dispose (object);
468 }
469
470 static void
471 gtk_tree_menu_finalize (GObject *object)
472 {
473   GtkTreeMenu        *menu;
474   GtkTreeMenuPrivate *priv;
475
476   menu = GTK_TREE_MENU (object);
477   priv = menu->priv;
478
479   gtk_tree_menu_set_row_separator_func (menu, NULL, NULL, NULL);
480   gtk_tree_menu_set_header_func (menu, NULL, NULL, NULL);
481
482   if (priv->root) 
483     gtk_tree_row_reference_free (priv->root);
484
485   G_OBJECT_CLASS (gtk_tree_menu_parent_class)->finalize (object);
486 }
487
488 static void
489 gtk_tree_menu_set_property (GObject            *object,
490                             guint               prop_id,
491                             const GValue       *value,
492                             GParamSpec         *pspec)
493 {
494   GtkTreeMenu *menu = GTK_TREE_MENU (object);
495
496   switch (prop_id)
497     {
498     case PROP_MODEL:
499       gtk_tree_menu_set_model (menu, g_value_get_object (value));
500       break;
501
502     case PROP_ROOT:
503       gtk_tree_menu_set_root (menu, g_value_get_boxed (value));
504       break;
505
506     case PROP_CELL_AREA:
507       /* Construct-only, can only be assigned once */
508       gtk_tree_menu_set_area (menu, (GtkCellArea *)g_value_get_object (value));
509       break;
510
511     case PROP_TEAROFF:
512       gtk_tree_menu_set_tearoff (menu, g_value_get_boolean (value));
513       break;
514
515     case PROP_WRAP_WIDTH:
516       gtk_tree_menu_set_wrap_width (menu, g_value_get_int (value));
517       break;
518
519      case PROP_ROW_SPAN_COL:
520       gtk_tree_menu_set_row_span_column (menu, g_value_get_int (value));
521       break;
522
523      case PROP_COL_SPAN_COL:
524       gtk_tree_menu_set_column_span_column (menu, g_value_get_int (value));
525       break;
526
527     default:
528       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529       break;
530     }
531 }
532
533 static void
534 gtk_tree_menu_get_property (GObject            *object,
535                             guint               prop_id,
536                             GValue             *value,
537                             GParamSpec         *pspec)
538 {
539   GtkTreeMenu        *menu = GTK_TREE_MENU (object);
540   GtkTreeMenuPrivate *priv = menu->priv;
541
542   switch (prop_id)
543     {
544     case PROP_MODEL:
545       g_value_set_object (value, priv->model);
546       break;
547       
548     case PROP_ROOT:
549       g_value_set_boxed (value, priv->root);
550       break;
551       
552     case PROP_CELL_AREA:
553       g_value_set_object (value, priv->area);
554       break;
555
556     case PROP_TEAROFF:
557       g_value_set_boolean (value, priv->tearoff);
558       break;
559
560     default:
561       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
562       break;
563     }
564 }
565
566 /****************************************************************
567  *                         GtkWidgetClass                       *
568  ****************************************************************/
569
570 /* We tell all the menu items to reserve space for the submenu
571  * indicator if there is at least one submenu, this way we ensure
572  * that every internal cell area gets allocated the
573  * same width (and requested height for the same appropriate width).
574  */
575 static void
576 sync_reserve_submenu_size (GtkTreeMenu *menu)
577 {
578   GList              *children, *l;
579   gboolean            has_submenu = FALSE;
580
581   children = gtk_container_get_children (GTK_CONTAINER (menu));
582   for (l = children; l; l = l->next)
583     {
584       GtkMenuItem *item = l->data;
585
586       if (gtk_menu_item_get_submenu (item) != NULL)
587         {
588           has_submenu = TRUE;
589           break;
590         }
591     }
592
593   for (l = children; l; l = l->next)
594     {
595       GtkMenuItem *item = l->data;
596
597       gtk_menu_item_set_reserve_indicator (item, has_submenu);
598     }
599
600   g_list_free (children);
601 }
602
603 static void
604 gtk_tree_menu_get_preferred_width (GtkWidget           *widget,
605                                    gint                *minimum_size,
606                                    gint                *natural_size)
607 {
608   GtkTreeMenu        *menu = GTK_TREE_MENU (widget);
609   GtkTreeMenuPrivate *priv = menu->priv;
610
611   /* We leave the requesting work up to the cellviews which operate in the same
612    * context, reserving space for the submenu indicator if any of the items have
613    * submenus ensures that every cellview will receive the same allocated width.
614    *
615    * Since GtkMenu does hieght-for-width correctly, we know that the width of
616    * every cell will be requested before the height-for-widths are requested.
617    */
618   g_signal_handler_block (priv->context, priv->size_changed_id);
619
620   sync_reserve_submenu_size (menu);
621
622   GTK_WIDGET_CLASS (gtk_tree_menu_parent_class)->get_preferred_width (widget, minimum_size, natural_size);
623
624   g_signal_handler_unblock (priv->context, priv->size_changed_id);
625 }
626
627 static void
628 gtk_tree_menu_get_preferred_height (GtkWidget           *widget,
629                                     gint                *minimum_size,
630                                     gint                *natural_size)
631 {
632   GtkTreeMenu        *menu = GTK_TREE_MENU (widget);
633   GtkTreeMenuPrivate *priv = menu->priv;
634
635   g_signal_handler_block (priv->context, priv->size_changed_id);
636
637   sync_reserve_submenu_size (menu);
638
639   GTK_WIDGET_CLASS (gtk_tree_menu_parent_class)->get_preferred_height (widget, minimum_size, natural_size);
640
641   g_signal_handler_unblock (priv->context, priv->size_changed_id);
642 }
643
644 /****************************************************************
645  *                      GtkCellLayoutIface                      *
646  ****************************************************************/
647 static void
648 gtk_tree_menu_cell_layout_init (GtkCellLayoutIface  *iface)
649 {
650   iface->get_area = gtk_tree_menu_cell_layout_get_area;
651 }
652
653 static GtkCellArea *
654 gtk_tree_menu_cell_layout_get_area (GtkCellLayout *layout)
655 {
656   GtkTreeMenu        *menu = GTK_TREE_MENU (layout);
657   GtkTreeMenuPrivate *priv = menu->priv;
658
659   return priv->area;
660 }
661
662
663 /****************************************************************
664  *             TreeModel callbacks/populating menus             *
665  ****************************************************************/
666 static GtkWidget *
667 gtk_tree_menu_get_path_item (GtkTreeMenu          *menu,
668                              GtkTreePath          *search)
669 {
670   GtkWidget *item = NULL;
671   GList     *children, *l;
672
673   children = gtk_container_get_children (GTK_CONTAINER (menu));
674
675   for (l = children; item == NULL && l != NULL; l = l->next)
676     {
677       GtkWidget   *child = l->data;
678       GtkTreePath *path  = NULL;
679
680       if (GTK_IS_SEPARATOR_MENU_ITEM (child))
681         {
682           GtkTreeRowReference *row =
683             g_object_get_qdata (G_OBJECT (child), tree_menu_path_quark);
684
685           if (row)
686             {
687               path = gtk_tree_row_reference_get_path (row);
688               
689               if (!path)
690                 /* Return any first child where it's row-reference became invalid,
691                  * this is because row-references get null paths before we recieve
692                  * the "row-deleted" signal.
693                  */
694                 item = child;
695             }
696         }
697       else if (!GTK_IS_TEAROFF_MENU_ITEM (child))
698         {
699           GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
700
701           /* It's always a cellview */
702           if (GTK_IS_CELL_VIEW (view))
703             path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
704
705           if (!path)
706             /* Return any first child where it's row-reference became invalid,
707              * this is because row-references get null paths before we recieve
708              * the "row-deleted" signal.
709              */
710             item = child;
711         }
712
713       if (path)
714         {
715           if (gtk_tree_path_compare (search, path) == 0)
716             item = child;
717
718           gtk_tree_path_free (path);
719         }
720     }
721
722   g_list_free (children);
723
724   return item;
725 }
726
727 static gboolean
728 gtk_tree_menu_path_in_menu (GtkTreeMenu  *menu,
729                             GtkTreePath  *path,
730                             gboolean     *header_item)
731 {
732   GtkTreeMenuPrivate *priv = menu->priv;
733   gboolean            in_menu = FALSE;
734   gboolean            is_header = FALSE;
735
736   /* Check if the is in root of the model */
737   if (gtk_tree_path_get_depth (path) == 1)
738     {
739       if (!priv->root)
740         in_menu = TRUE;
741     }
742   /* If we are a submenu, compare the parent path */
743   else if (priv->root && gtk_tree_path_get_depth (path) > 1)
744     {
745       GtkTreePath *root_path   = gtk_tree_row_reference_get_path (priv->root);
746       GtkTreePath *parent_path = gtk_tree_path_copy (path);
747
748       gtk_tree_path_up (parent_path);
749
750       if (root_path)
751         {
752           if (gtk_tree_path_compare (root_path, parent_path) == 0)
753             in_menu = TRUE;
754           
755           if (!in_menu && priv->menu_with_header && 
756               gtk_tree_path_compare (root_path, path) == 0)
757             {
758               in_menu   = TRUE;
759               is_header = TRUE;
760             }
761         }
762       gtk_tree_path_free (root_path);
763       gtk_tree_path_free (parent_path);
764     }
765
766   if (header_item)
767     *header_item = is_header;
768
769   return in_menu;
770 }
771
772 static GtkWidget *
773 gtk_tree_menu_path_needs_submenu (GtkTreeMenu *menu, 
774                                   GtkTreePath *search)
775 {
776   GtkWidget   *item = NULL;
777   GList       *children, *l;
778   GtkTreePath *parent_path;
779
780   if (gtk_tree_path_get_depth (search) <= 1)
781     return NULL;
782
783   parent_path = gtk_tree_path_copy (search);
784   gtk_tree_path_up (parent_path);
785
786   children    = gtk_container_get_children (GTK_CONTAINER (menu));
787
788   for (l = children; item == NULL && l != NULL; l = l->next)
789     {
790       GtkWidget   *child = l->data;
791       GtkTreePath *path  = NULL;
792
793       /* Separators dont get submenus, if it already has a submenu then let
794        * the submenu handle inserted rows */
795       if (!GTK_IS_SEPARATOR_MENU_ITEM (child) && 
796           !gtk_menu_item_get_submenu (GTK_MENU_ITEM (child)))
797         {
798           GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
799
800           /* It's always a cellview */
801           if (GTK_IS_CELL_VIEW (view))
802             path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
803         }
804
805       if (path)
806         {
807           if (gtk_tree_path_compare (parent_path, path) == 0)
808             item = child;
809
810           gtk_tree_path_free (path);
811         }
812     }
813
814   g_list_free (children);
815   gtk_tree_path_free (parent_path);
816
817   return item;
818 }
819
820 static GtkWidget *
821 find_empty_submenu (GtkTreeMenu  *menu)
822 {
823   GtkTreeMenuPrivate *priv = menu->priv;
824   GList              *children, *l;
825   GtkWidget          *submenu = NULL;
826
827   children = gtk_container_get_children (GTK_CONTAINER (menu));
828
829   for (l = children; submenu == NULL && l != NULL; l = l->next)
830     {
831       GtkWidget   *child = l->data;
832       GtkTreePath *path  = NULL;
833       GtkTreeIter  iter;
834
835       /* Separators dont get submenus, if it already has a submenu then let
836        * the submenu handle inserted rows */
837       if (!GTK_IS_SEPARATOR_MENU_ITEM (child) && !GTK_IS_TEAROFF_MENU_ITEM (child))
838         {
839           GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
840
841           /* It's always a cellview */
842           if (GTK_IS_CELL_VIEW (view))
843             path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
844         }
845
846       if (path)
847         {
848           if (gtk_tree_model_get_iter (priv->model, &iter, path) &&
849               !gtk_tree_model_iter_has_child (priv->model, &iter))
850             submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (child));
851
852           gtk_tree_path_free (path);
853         }
854     }
855
856   g_list_free (children);
857
858   return submenu;
859 }
860
861 static void
862 row_inserted_cb (GtkTreeModel     *model,
863                  GtkTreePath      *path,
864                  GtkTreeIter      *iter,
865                  GtkTreeMenu      *menu)
866 {
867   GtkTreeMenuPrivate *priv = menu->priv;
868   gint               *indices, index, depth;
869   GtkWidget          *item;
870
871   /* If the iter should be in this menu then go ahead and insert it */
872   if (gtk_tree_menu_path_in_menu (menu, path, NULL))
873     {
874       if (priv->wrap_width > 0)
875         rebuild_menu (menu);
876       else
877         {
878           /* Get the index of the path for this depth */
879           indices = gtk_tree_path_get_indices (path);
880           depth   = gtk_tree_path_get_depth (path);
881           index   = indices[depth -1];
882           
883           /* Menus with a header include a menuitem for it's root node
884            * and a separator menu item */
885           if (priv->menu_with_header)
886             index += 2;
887           
888           /* Index after the tearoff item for the root menu if
889            * there is a tearoff item
890            */
891           if (priv->root == NULL && priv->tearoff)
892             index += 1;
893           
894           item = gtk_tree_menu_create_item (menu, iter, FALSE);
895           gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, index);
896           
897           /* Resize everything */
898           gtk_cell_area_context_reset (menu->priv->context);
899         }
900     }
901   else
902     {
903       /* Create submenus for iters if we need to */
904       item = gtk_tree_menu_path_needs_submenu (menu, path);
905       if (item)
906         {
907           GtkTreePath *item_path = gtk_tree_path_copy (path);
908
909           gtk_tree_path_up (item_path);
910           gtk_tree_menu_create_submenu (menu, item, item_path);
911           gtk_tree_path_free (item_path);
912         }
913     }
914 }
915
916 static void
917 row_deleted_cb (GtkTreeModel     *model,
918                 GtkTreePath      *path,
919                 GtkTreeMenu      *menu)
920 {
921   GtkTreeMenuPrivate *priv = menu->priv;
922   GtkWidget          *item;
923
924   /* If it's the header item we leave it to the parent menu 
925    * to remove us from its menu
926    */
927   item = gtk_tree_menu_get_path_item (menu, path);
928
929   if (item)
930     {
931       if (priv->wrap_width > 0)
932         rebuild_menu (menu);
933       else
934         {
935           /* Get rid of the deleted item */
936           gtk_widget_destroy (item);
937           
938           /* Resize everything */
939           gtk_cell_area_context_reset (menu->priv->context);
940         }
941     }
942   else 
943     {
944       /* It's up to the parent menu to destroy a child menu that becomes empty
945        * since the topmost menu belongs to the user and is allowed to have no contents */
946       GtkWidget *submenu = find_empty_submenu (menu);
947       if (submenu)
948         gtk_widget_destroy (submenu);
949     }
950 }
951
952 static void
953 row_reordered_cb (GtkTreeModel    *model,
954                   GtkTreePath     *path,
955                   GtkTreeIter     *iter,
956                   gint            *new_order,
957                   GtkTreeMenu     *menu)
958 {
959   GtkTreeMenuPrivate *priv = menu->priv;
960   gboolean            this_menu = FALSE;
961
962   if (path == NULL && priv->root == NULL)
963     this_menu = TRUE;
964   else if (priv->root)
965     {
966       GtkTreePath *root_path =
967         gtk_tree_row_reference_get_path (priv->root);
968
969       if (gtk_tree_path_compare (root_path, path) == 0)
970         this_menu = TRUE;
971
972       gtk_tree_path_free (root_path);
973     }
974
975   if (this_menu)
976     rebuild_menu (menu);
977 }
978
979 static gint 
980 menu_item_position (GtkTreeMenu *menu,
981                     GtkWidget   *item)
982 {
983   GList *children, *l;
984   gint   position;
985
986   children = gtk_container_get_children (GTK_CONTAINER (menu));
987   for (position = 0, l = children; l; position++, l = l->next)
988     {
989       GtkWidget *iitem = l->data;
990
991       if (item == iitem)
992         break;
993     }
994
995   g_list_free (children);
996
997   return position;
998 }
999
1000 static void
1001 row_changed_cb (GtkTreeModel         *model,
1002                 GtkTreePath          *path,
1003                 GtkTreeIter          *iter,
1004                 GtkTreeMenu          *menu)
1005 {
1006   GtkTreeMenuPrivate *priv = menu->priv;
1007   gboolean            is_separator = FALSE;
1008   gboolean            has_header = FALSE;
1009   GtkWidget          *item;
1010
1011   item = gtk_tree_menu_get_path_item (menu, path);
1012
1013   if (priv->root)
1014     {
1015       GtkTreePath *root_path =
1016         gtk_tree_row_reference_get_path (priv->root);
1017       
1018       if (root_path && gtk_tree_path_compare (root_path, path) == 0)
1019         {
1020           if (priv->header_func)
1021             has_header = 
1022               priv->header_func (priv->model, iter, priv->header_data);
1023           
1024           if (has_header && !item)
1025             {
1026               item = gtk_separator_menu_item_new ();
1027               gtk_widget_show (item);
1028               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1029
1030               item = gtk_tree_menu_create_item (menu, iter, TRUE);
1031               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1032
1033               priv->menu_with_header = TRUE;
1034             }
1035           else if (!has_header && item)
1036             {
1037               /* Destroy the header item and then the following separator */
1038               gtk_widget_destroy (item);
1039               gtk_widget_destroy (GTK_MENU_SHELL (menu)->children->data);
1040
1041               priv->menu_with_header = FALSE;
1042             }
1043
1044           gtk_tree_path_free (root_path);
1045         }
1046     }
1047   
1048   if (item)
1049     {
1050       if (priv->wrap_width > 0)
1051         /* Ugly, we need to rebuild the menu here if
1052          * the row-span/row-column values change 
1053          */
1054         rebuild_menu (menu);
1055       else
1056         {
1057           if (priv->row_separator_func)
1058             is_separator = 
1059               priv->row_separator_func (model, iter,
1060                                         priv->row_separator_data);
1061
1062
1063           if (is_separator != GTK_IS_SEPARATOR_MENU_ITEM (item))
1064             {
1065               gint position = menu_item_position (menu, item);
1066
1067               gtk_widget_destroy (item);
1068               item = gtk_tree_menu_create_item (menu, iter, FALSE);
1069               gtk_menu_shell_insert (GTK_MENU_SHELL (menu), item, position);
1070             }
1071         }
1072     }
1073 }
1074
1075 static void
1076 context_size_changed_cb (GtkCellAreaContext  *context,
1077                          GParamSpec          *pspec,
1078                          GtkWidget           *menu)
1079 {
1080   if (!strcmp (pspec->name, "minimum-width") ||
1081       !strcmp (pspec->name, "natural-width") ||
1082       !strcmp (pspec->name, "minimum-height") ||
1083       !strcmp (pspec->name, "natural-height"))
1084     gtk_widget_queue_resize (menu);
1085 }
1086
1087 static gboolean
1088 area_is_sensitive (GtkCellArea *area)
1089 {
1090   GList    *cells, *list;
1091   gboolean  sensitive = FALSE;
1092   
1093   cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
1094
1095   for (list = cells; list; list = list->next)
1096     {
1097       g_object_get (list->data, "sensitive", &sensitive, NULL);
1098       
1099       if (sensitive)
1100         break;
1101     }
1102   g_list_free (cells);
1103
1104   return sensitive;
1105 }
1106
1107 static void
1108 area_apply_attributes_cb (GtkCellArea          *area,
1109                           GtkTreeModel         *tree_model,
1110                           GtkTreeIter          *iter,
1111                           gboolean              is_expander,
1112                           gboolean              is_expanded,
1113                           GtkTreeMenu          *menu)
1114 {
1115   /* If the menu for this iter has a submenu */
1116   GtkTreeMenuPrivate *priv = menu->priv;
1117   GtkTreePath        *path;
1118   GtkWidget          *item;
1119   gboolean            is_header;
1120   gboolean            sensitive;
1121
1122   path = gtk_tree_model_get_path (tree_model, iter);
1123
1124   if (gtk_tree_menu_path_in_menu (menu, path, &is_header))
1125     {
1126       item = gtk_tree_menu_get_path_item (menu, path);
1127
1128       /* If there is no submenu, go ahead and update item sensitivity,
1129        * items with submenus are always sensitive */
1130       if (item && !gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)))
1131         {
1132           sensitive = area_is_sensitive (priv->area);
1133
1134           gtk_widget_set_sensitive (item, sensitive);
1135
1136           if (is_header)
1137             {
1138               /* For header items we need to set the sensitivity
1139                * of the following separator item 
1140                */
1141               if (GTK_MENU_SHELL (menu)->children && 
1142                   GTK_MENU_SHELL (menu)->children->next)
1143                 {
1144                   GtkWidget *separator = 
1145                     GTK_MENU_SHELL (menu)->children->next->data;
1146
1147                   gtk_widget_set_sensitive (separator, sensitive);
1148                 }
1149             }
1150         }
1151     }
1152
1153   gtk_tree_path_free (path);
1154 }
1155
1156 static void
1157 gtk_tree_menu_set_area (GtkTreeMenu *menu,
1158                         GtkCellArea *area)
1159 {
1160   GtkTreeMenuPrivate *priv = menu->priv;
1161
1162   if (priv->area)
1163     {
1164       g_signal_handler_disconnect (priv->area,
1165                                    priv->apply_attributes_id);
1166       priv->apply_attributes_id = 0;
1167
1168       g_object_unref (priv->area);
1169     }
1170
1171   priv->area = area;
1172
1173   if (priv->area)
1174     {
1175       g_object_ref_sink (priv->area);
1176
1177       priv->apply_attributes_id =
1178         g_signal_connect (priv->area, "apply-attributes",
1179                           G_CALLBACK (area_apply_attributes_cb), menu);
1180     }
1181 }
1182
1183 static gboolean
1184 menu_occupied (GtkTreeMenu *menu,
1185                guint        left_attach,
1186                guint        right_attach,
1187                guint        top_attach,
1188                guint        bottom_attach)
1189 {
1190   GList *i;
1191
1192   for (i = GTK_MENU_SHELL (menu)->children; i; i = i->next)
1193     {
1194       guint l, r, b, t;
1195
1196       gtk_container_child_get (GTK_CONTAINER (menu), 
1197                                i->data,
1198                                "left-attach", &l,
1199                                "right-attach", &r,
1200                                "bottom-attach", &b,
1201                                "top-attach", &t,
1202                                NULL);
1203
1204       /* look if this item intersects with the given coordinates */
1205       if (right_attach > l && left_attach < r && bottom_attach > t && top_attach < b)
1206         return TRUE;
1207     }
1208
1209   return FALSE;
1210 }
1211
1212 static void
1213 relayout_item (GtkTreeMenu *menu,
1214                GtkWidget   *item,
1215                GtkTreeIter *iter,
1216                GtkWidget   *prev)
1217 {
1218   GtkTreeMenuPrivate *priv = menu->priv;
1219   gint                current_col = 0, current_row = 0;
1220   gint                rows = 1, cols = 1;
1221   
1222   if (priv->col_span_col == -1 &&
1223       priv->row_span_col == -1 &&
1224       prev)
1225     {
1226       gtk_container_child_get (GTK_CONTAINER (menu), prev,
1227                                "right-attach", &current_col,
1228                                "top-attach", &current_row,
1229                                NULL);
1230       if (current_col + cols > priv->wrap_width)
1231         {
1232           current_col = 0;
1233           current_row++;
1234         }
1235     }
1236   else
1237     {
1238       if (priv->col_span_col != -1)
1239         gtk_tree_model_get (priv->model, iter,
1240                             priv->col_span_col, &cols,
1241                             -1);
1242       if (priv->row_span_col != -1)
1243         gtk_tree_model_get (priv->model, iter,
1244                             priv->row_span_col, &rows,
1245                             -1);
1246
1247       while (1)
1248         {
1249           if (current_col + cols > priv->wrap_width)
1250             {
1251               current_col = 0;
1252               current_row++;
1253             }
1254           
1255           if (!menu_occupied (menu, 
1256                               current_col, current_col + cols,
1257                               current_row, current_row + rows))
1258             break;
1259
1260           current_col++;
1261         }
1262     }
1263
1264   /* set attach props */
1265   gtk_menu_attach (GTK_MENU (menu), item,
1266                    current_col, current_col + cols,
1267                    current_row, current_row + rows);
1268 }
1269
1270 static void
1271 gtk_tree_menu_create_submenu (GtkTreeMenu *menu,
1272                               GtkWidget   *item,
1273                               GtkTreePath *path)
1274 {
1275   GtkTreeMenuPrivate *priv = menu->priv;
1276   GtkWidget          *view;
1277   GtkWidget          *submenu;
1278
1279   view = gtk_bin_get_child (GTK_BIN (item));
1280   gtk_cell_view_set_draw_sensitive (GTK_CELL_VIEW (view), TRUE);
1281
1282   submenu = gtk_tree_menu_new_with_area (priv->area);
1283
1284   gtk_tree_menu_set_row_separator_func (GTK_TREE_MENU (submenu), 
1285                                         priv->row_separator_func,
1286                                         priv->row_separator_data,
1287                                         priv->row_separator_destroy);
1288   gtk_tree_menu_set_header_func (GTK_TREE_MENU (submenu), 
1289                                  priv->header_func,
1290                                  priv->header_data,
1291                                  priv->header_destroy);
1292
1293   gtk_tree_menu_set_wrap_width (GTK_TREE_MENU (submenu), priv->wrap_width);
1294   gtk_tree_menu_set_row_span_column (GTK_TREE_MENU (submenu), priv->row_span_col);
1295   gtk_tree_menu_set_column_span_column (GTK_TREE_MENU (submenu), priv->col_span_col);
1296   
1297   gtk_tree_menu_set_model_internal (GTK_TREE_MENU (submenu), priv->model);
1298   gtk_tree_menu_set_root (GTK_TREE_MENU (submenu), path);
1299   gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
1300   
1301   g_signal_connect (submenu, "menu-activate", 
1302                     G_CALLBACK (submenu_activated_cb), menu);
1303 }
1304
1305 static GtkWidget *
1306 gtk_tree_menu_create_item (GtkTreeMenu *menu,
1307                            GtkTreeIter *iter,
1308                            gboolean     header_item)
1309 {
1310   GtkTreeMenuPrivate *priv = menu->priv;
1311   GtkWidget          *item, *view;
1312   GtkTreePath        *path;
1313   gboolean            is_separator = FALSE;
1314
1315   path = gtk_tree_model_get_path (priv->model, iter);
1316
1317   if (priv->row_separator_func)
1318     is_separator = 
1319       priv->row_separator_func (priv->model, iter,
1320                                 priv->row_separator_data);
1321
1322   if (is_separator)
1323     {
1324       item = gtk_separator_menu_item_new ();
1325       gtk_widget_show (item);
1326
1327       g_object_set_qdata_full (G_OBJECT (item),
1328                                tree_menu_path_quark,
1329                                gtk_tree_row_reference_new (priv->model, path),
1330                                (GDestroyNotify)gtk_tree_row_reference_free);
1331     }
1332   else
1333     {
1334       view = gtk_cell_view_new_with_context (priv->area, priv->context);
1335       item = gtk_menu_item_new ();
1336       gtk_widget_show (view);
1337       gtk_widget_show (item);
1338       
1339       gtk_cell_view_set_model (GTK_CELL_VIEW (view), priv->model);
1340       gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (view), path);
1341       
1342       gtk_widget_show (view);
1343       gtk_container_add (GTK_CONTAINER (item), view);
1344       
1345       g_signal_connect (item, "activate", G_CALLBACK (item_activated_cb), menu);
1346
1347       /* Add a GtkTreeMenu submenu to render the children of this row */
1348       if (header_item == FALSE &&
1349           gtk_tree_model_iter_has_child (priv->model, iter))
1350         gtk_tree_menu_create_submenu (menu, item, path);
1351     }
1352
1353   gtk_tree_path_free (path);
1354
1355   return item;
1356 }
1357
1358 static inline void 
1359 rebuild_menu (GtkTreeMenu *menu)
1360 {
1361   GtkTreeMenuPrivate *priv = menu->priv;
1362
1363   /* Destroy all the menu items */
1364   gtk_container_foreach (GTK_CONTAINER (menu), 
1365                          (GtkCallback) gtk_widget_destroy, NULL);
1366   
1367   /* Populate */
1368   if (priv->model)
1369     gtk_tree_menu_populate (menu);
1370 }
1371
1372
1373 static void
1374 gtk_tree_menu_populate (GtkTreeMenu *menu)
1375 {
1376   GtkTreeMenuPrivate *priv = menu->priv;
1377   GtkTreePath        *path = NULL;
1378   GtkTreeIter         parent;
1379   GtkTreeIter         iter;
1380   gboolean            valid = FALSE;
1381   GtkWidget          *menu_item, *prev = NULL;
1382
1383   if (!priv->model)
1384     return;
1385
1386   if (priv->root)
1387     path = gtk_tree_row_reference_get_path (priv->root);
1388
1389   if (path)
1390     {
1391       if (gtk_tree_model_get_iter (priv->model, &parent, path))
1392         {
1393           valid = gtk_tree_model_iter_children (priv->model, &iter, &parent);
1394
1395           if (priv->header_func && 
1396               priv->header_func (priv->model, &parent, priv->header_data))
1397             {
1398               /* Add a submenu header for rows which desire one, used for
1399                * combo boxes to allow all rows to be activatable/selectable 
1400                */
1401               menu_item = gtk_tree_menu_create_item (menu, &parent, TRUE);
1402               gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1403               
1404               menu_item = gtk_separator_menu_item_new ();
1405               gtk_widget_show (menu_item);
1406               gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1407
1408               prev = menu_item;
1409               priv->menu_with_header = TRUE;
1410             }
1411         }
1412       gtk_tree_path_free (path);
1413     }
1414   else
1415     {
1416       /* Tearoff menu items only go in the root menu */
1417       if (priv->tearoff)
1418         {
1419           menu_item = gtk_tearoff_menu_item_new ();
1420           gtk_widget_show (menu_item);
1421
1422           if (priv->wrap_width > 0)
1423             gtk_menu_attach (GTK_MENU (menu), menu_item, 0, priv->wrap_width, 0, 1);
1424           else
1425             gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1426
1427           prev = menu_item;
1428         }
1429
1430       valid = gtk_tree_model_iter_children (priv->model, &iter, NULL);
1431     }
1432
1433   /* Create a menu item for every row at the current depth, add a GtkTreeMenu
1434    * submenu for iters/items that have children */
1435   while (valid)
1436     {
1437       menu_item = gtk_tree_menu_create_item (menu, &iter, FALSE);
1438
1439       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1440
1441       if (priv->wrap_width > 0)
1442         relayout_item (menu, menu_item, &iter, prev);
1443
1444       prev  = menu_item;
1445       valid = gtk_tree_model_iter_next (priv->model, &iter);
1446     }
1447 }
1448
1449 static void
1450 item_activated_cb (GtkMenuItem          *item,
1451                    GtkTreeMenu          *menu)
1452 {
1453   GtkCellView *view;
1454   GtkTreePath *path;
1455   gchar       *path_str;
1456
1457   /* Only activate leafs, not parents */
1458   if (!gtk_menu_item_get_submenu (item))
1459     {
1460       view     = GTK_CELL_VIEW (gtk_bin_get_child (GTK_BIN (item)));
1461       path     = gtk_cell_view_get_displayed_row (view);
1462       path_str = gtk_tree_path_to_string (path);
1463       
1464       g_signal_emit (menu, tree_menu_signals[SIGNAL_MENU_ACTIVATE], 0, path_str);
1465       
1466       g_free (path_str);
1467       gtk_tree_path_free (path);
1468     }
1469 }
1470
1471 static void
1472 submenu_activated_cb (GtkTreeMenu          *submenu,
1473                       const gchar          *path,
1474                       GtkTreeMenu          *menu)
1475 {
1476   g_signal_emit (menu, tree_menu_signals[SIGNAL_MENU_ACTIVATE], 0, path);
1477 }
1478
1479 /* Sets the model without rebuilding the menu, prevents
1480  * infinite recursion while building submenus (we wait
1481  * until the root is set and then build the menu) */
1482 static void
1483 gtk_tree_menu_set_model_internal (GtkTreeMenu  *menu,
1484                                   GtkTreeModel *model)
1485 {
1486   GtkTreeMenuPrivate *priv;
1487
1488   priv = menu->priv;
1489
1490   if (priv->model != model)
1491     {
1492       if (priv->model)
1493         {
1494           /* Disconnect signals */
1495           g_signal_handler_disconnect (priv->model,
1496                                        priv->row_inserted_id);
1497           g_signal_handler_disconnect (priv->model,
1498                                        priv->row_deleted_id);
1499           g_signal_handler_disconnect (priv->model,
1500                                        priv->row_reordered_id);
1501           g_signal_handler_disconnect (priv->model,
1502                                        priv->row_changed_id);
1503           priv->row_inserted_id  = 0;
1504           priv->row_deleted_id   = 0;
1505           priv->row_reordered_id = 0;
1506           priv->row_changed_id = 0;
1507
1508           g_object_unref (priv->model);
1509         }
1510
1511       priv->model = model;
1512
1513       if (priv->model)
1514         {
1515           g_object_ref (priv->model);
1516
1517           /* Connect signals */
1518           priv->row_inserted_id  = g_signal_connect (priv->model, "row-inserted",
1519                                                      G_CALLBACK (row_inserted_cb), menu);
1520           priv->row_deleted_id   = g_signal_connect (priv->model, "row-deleted",
1521                                                      G_CALLBACK (row_deleted_cb), menu);
1522           priv->row_reordered_id = g_signal_connect (priv->model, "rows-reordered",
1523                                                      G_CALLBACK (row_reordered_cb), menu);
1524           priv->row_changed_id   = g_signal_connect (priv->model, "row-changed",
1525                                                      G_CALLBACK (row_changed_cb), menu);
1526         }
1527     }
1528 }
1529
1530 /****************************************************************
1531  *                            API                               *
1532  ****************************************************************/
1533
1534 /**
1535  * gtk_tree_menu_new:
1536  *
1537  * Creates a new #GtkTreeMenu.
1538  *
1539  * Return value: A newly created #GtkTreeMenu with no model or root.
1540  *
1541  * Since: 3.0
1542  */
1543 GtkWidget *
1544 gtk_tree_menu_new (void)
1545 {
1546   return (GtkWidget *)g_object_new (GTK_TYPE_TREE_MENU, NULL);
1547 }
1548
1549 /**
1550  * gtk_tree_menu_new_with_area:
1551  * @area: the #GtkCellArea to use to render cells in the menu
1552  *
1553  * Creates a new #GtkTreeMenu using @area to render it's cells.
1554  *
1555  * Return value: A newly created #GtkTreeMenu with no model or root.
1556  *
1557  * Since: 3.0
1558  */
1559 GtkWidget *
1560 gtk_tree_menu_new_with_area (GtkCellArea    *area)
1561 {
1562   return (GtkWidget *)g_object_new (GTK_TYPE_TREE_MENU, 
1563                                     "cell-area", area, 
1564                                     NULL);
1565 }
1566
1567 /**
1568  * gtk_tree_menu_new_full:
1569  * @area: (allow-none): the #GtkCellArea to use to render cells in the menu, or %NULL.
1570  * @model: (allow-none): the #GtkTreeModel to build the menu heirarchy from, or %NULL.
1571  * @root: (allow-none): the #GtkTreePath indicating the root row for this menu, or %NULL.
1572  *
1573  * Creates a new #GtkTreeMenu hierarchy from the provided @model and @root using @area to render it's cells.
1574  *
1575  * Return value: A newly created #GtkTreeMenu.
1576  *
1577  * Since: 3.0
1578  */
1579 GtkWidget *
1580 gtk_tree_menu_new_full (GtkCellArea         *area,
1581                         GtkTreeModel        *model,
1582                         GtkTreePath         *root)
1583 {
1584   return (GtkWidget *)g_object_new (GTK_TYPE_TREE_MENU, 
1585                                     "cell-area", area, 
1586                                     "model", model,
1587                                     "root", root,
1588                                     NULL);
1589 }
1590
1591 /**
1592  * gtk_tree_menu_set_model:
1593  * @menu: a #GtkTreeMenu
1594  * @model: (allow-none): the #GtkTreeModel to build the menu hierarchy from, or %NULL.
1595  *
1596  * Sets @model to be used to build the menu heirarhcy.
1597  *
1598  * Since: 3.0
1599  */
1600 void
1601 gtk_tree_menu_set_model (GtkTreeMenu  *menu,
1602                          GtkTreeModel *model)
1603 {
1604   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1605   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1606
1607   gtk_tree_menu_set_model_internal (menu, model);
1608
1609   rebuild_menu (menu);
1610 }
1611
1612 /**
1613  * gtk_tree_menu_get_model:
1614  * @menu: a #GtkTreeMenu
1615  *
1616  * Gets the @model currently used for the menu heirarhcy.
1617  *
1618  * Return value: (transfer none): the #GtkTreeModel which is used
1619  * for @menu's hierarchy.
1620  *
1621  * Since: 3.0
1622  */
1623 GtkTreeModel *
1624 gtk_tree_menu_get_model (GtkTreeMenu *menu)
1625 {
1626   GtkTreeMenuPrivate *priv;
1627
1628   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), NULL);
1629
1630   priv = menu->priv;
1631
1632   return priv->model;
1633 }
1634
1635 /**
1636  * gtk_tree_menu_set_root:
1637  * @menu: a #GtkTreeMenu
1638  * @root: (allow-none): the #GtkTreePath which is the root of @menu, or %NULL.
1639  *
1640  * Sets the @root path for @menu's hierarchy. @menu must already
1641  * have a model set and @root must point to a valid path inside the model.
1642  *
1643  * Since: 3.0
1644  */
1645 void
1646 gtk_tree_menu_set_root (GtkTreeMenu         *menu,
1647                         GtkTreePath         *path)
1648 {
1649   GtkTreeMenuPrivate *priv;
1650
1651   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1652   g_return_if_fail (menu->priv->model != NULL || path == NULL);
1653
1654   priv = menu->priv;
1655
1656   if (priv->root) 
1657     gtk_tree_row_reference_free (priv->root);
1658
1659   if (path)
1660     priv->root = gtk_tree_row_reference_new (priv->model, path);
1661   else
1662     priv->root = NULL;
1663
1664   rebuild_menu (menu);
1665 }
1666
1667 /**
1668  * gtk_tree_menu_get_root:
1669  * @menu: a #GtkTreeMenu
1670  *
1671  * Gets the @root path for @menu's hierarchy, or returns %NULL if @menu
1672  * has no model or is building a heirarchy for the entire model. *
1673  *
1674  * Return value: (transfer full) (allow-none): A newly created #GtkTreePath
1675  * pointing to the root of @menu which must be freed with gtk_tree_path_free().
1676  *
1677  * Since: 3.0
1678  */
1679 GtkTreePath *
1680 gtk_tree_menu_get_root (GtkTreeMenu *menu)
1681 {
1682   GtkTreeMenuPrivate *priv;
1683
1684   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), NULL);
1685
1686   priv = menu->priv;
1687
1688   if (priv->root)
1689     return gtk_tree_row_reference_get_path (priv->root);
1690
1691   return NULL;
1692 }
1693
1694 /**
1695  * gtk_tree_menu_get_tearoff:
1696  * @menu: a #GtkTreeMenu
1697  *
1698  * Gets whether this menu is build with a leading tearoff menu item.
1699  *
1700  * Return value: %TRUE if the menu has a tearoff item.
1701  *
1702  * Since: 3.0
1703  */
1704 gboolean
1705 gtk_tree_menu_get_tearoff (GtkTreeMenu *menu)
1706 {
1707   GtkTreeMenuPrivate *priv;
1708
1709   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), FALSE);
1710
1711   priv = menu->priv;
1712
1713   return priv->tearoff;
1714 }
1715
1716 /**
1717  * gtk_tree_menu_set_tearoff:
1718  * @menu: a #GtkTreeMenu
1719  * @tearoff: whether the menu should have a leading tearoff menu item.
1720  *
1721  * Sets whether this menu has a leading tearoff menu item.
1722  *
1723  * Since: 3.0
1724  */
1725 void
1726 gtk_tree_menu_set_tearoff (GtkTreeMenu *menu,
1727                            gboolean     tearoff)
1728 {
1729   GtkTreeMenuPrivate *priv;
1730
1731   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1732
1733   priv = menu->priv;
1734
1735   if (priv->tearoff != tearoff)
1736     {
1737       priv->tearoff = tearoff;
1738
1739       rebuild_menu (menu);
1740
1741       g_object_notify (G_OBJECT (menu), "tearoff");
1742     }
1743 }
1744
1745 /**
1746  * gtk_tree_menu_get_wrap_width:
1747  * @menu: a #GtkTreeMenu
1748  *
1749  * Gets the wrap width which is used to determine the number of columns 
1750  * for @menu. If the wrap width is larger than 1, @menu is in table mode.
1751  *
1752  * Return value: the wrap width.
1753  *
1754  * Since: 3.0
1755  */
1756 gint
1757 gtk_tree_menu_get_wrap_width (GtkTreeMenu *menu)
1758 {
1759   GtkTreeMenuPrivate *priv;
1760
1761   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), FALSE);
1762
1763   priv = menu->priv;
1764
1765   return priv->wrap_width;
1766 }
1767
1768 /**
1769  * gtk_tree_menu_set_wrap_width:
1770  * @menu: a #GtkTreeMenu
1771  * @width: the wrap width
1772  *
1773  * Sets the wrap width which is used to determine the number of columns 
1774  * for @menu. If the wrap width is larger than 1, @menu is in table mode.
1775  *
1776  * Since: 3.0
1777  */
1778 void
1779 gtk_tree_menu_set_wrap_width (GtkTreeMenu *menu,
1780                               gint         width)
1781 {
1782   GtkTreeMenuPrivate *priv;
1783
1784   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1785   g_return_if_fail (width >= 0);
1786
1787   priv = menu->priv;
1788
1789   if (priv->wrap_width != width)
1790     {
1791       priv->wrap_width = width;
1792
1793       rebuild_menu (menu);
1794
1795       g_object_notify (G_OBJECT (menu), "wrap-width");
1796     }
1797 }
1798
1799 /**
1800  * gtk_tree_menu_get_row_span_column:
1801  * @menu: a #GtkTreeMenu
1802  *
1803  * Gets the column with row span information for @menu.
1804  * The row span column contains integers which indicate how many rows
1805  * a menu item should span.
1806  *
1807  * Return value: the column in @menu's model containing row span information, or -1.
1808  *
1809  * Since: 3.0
1810  */
1811 gint
1812 gtk_tree_menu_get_row_span_column (GtkTreeMenu *menu)
1813 {
1814   GtkTreeMenuPrivate *priv;
1815
1816   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), FALSE);
1817
1818   priv = menu->priv;
1819
1820   return priv->row_span_col;
1821 }
1822
1823 /**
1824  * gtk_tree_menu_set_row_span_column:
1825  * @menu: a #GtkTreeMenu
1826  * @row_span: the column in the model to fetch the row span for a given menu item.
1827  *
1828  * Sets the column with row span information for @menu to be @row_span.
1829  * The row span column contains integers which indicate how many rows
1830  * a menu item should span.
1831  *
1832  * Since: 3.0
1833  */
1834 void
1835 gtk_tree_menu_set_row_span_column (GtkTreeMenu *menu,
1836                                    gint         row_span)
1837 {
1838   GtkTreeMenuPrivate *priv;
1839
1840   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1841
1842   priv = menu->priv;
1843
1844   if (priv->row_span_col != row_span)
1845     {
1846       priv->row_span_col = row_span;
1847
1848       if (priv->wrap_width > 0)
1849         rebuild_menu (menu);
1850
1851       g_object_notify (G_OBJECT (menu), "row-span-column");
1852     }
1853 }
1854
1855 /**
1856  * gtk_tree_menu_get_column_span_column:
1857  * @menu: a #GtkTreeMenu
1858  *
1859  * Gets the column with column span information for @menu.
1860  * The column span column contains integers which indicate how many columns
1861  * a menu item should span.
1862  *
1863  * Return value: the column in @menu's model containing column span information, or -1.
1864  *
1865  * Since: 3.0
1866  */
1867 gint
1868 gtk_tree_menu_get_column_span_column (GtkTreeMenu *menu)
1869 {
1870   GtkTreeMenuPrivate *priv;
1871
1872   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), FALSE);
1873
1874   priv = menu->priv;
1875
1876   return priv->col_span_col;
1877 }
1878
1879 /**
1880  * gtk_tree_menu_set_column_span_column:
1881  * @menu: a #GtkTreeMenu
1882  * @column_span: the column in the model to fetch the column span for a given menu item.
1883  *
1884  * Sets the column with column span information for @menu to be @column_span.
1885  * The column span column contains integers which indicate how many columns
1886  * a menu item should span.
1887  *
1888  * Since: 3.0
1889  */
1890 void
1891 gtk_tree_menu_set_column_span_column (GtkTreeMenu *menu,
1892                                       gint         column_span)
1893 {
1894   GtkTreeMenuPrivate *priv;
1895
1896   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1897
1898   priv = menu->priv;
1899
1900   if (priv->col_span_col != column_span)
1901     {
1902       priv->col_span_col = column_span;
1903
1904       if (priv->wrap_width > 0)
1905         rebuild_menu (menu);
1906
1907       g_object_notify (G_OBJECT (menu), "column-span-column");
1908     }
1909 }
1910
1911 /**
1912  * gtk_tree_menu_get_row_separator_func:
1913  * @menu: a #GtkTreeMenu
1914  * 
1915  * Gets the current #GtkTreeViewRowSeparatorFunc separator function.
1916  * 
1917  * Return value: the current row separator function.
1918  *
1919  * Since: 3.0
1920  */
1921 GtkTreeViewRowSeparatorFunc
1922 gtk_tree_menu_get_row_separator_func (GtkTreeMenu *menu)
1923 {
1924   GtkTreeMenuPrivate *priv;
1925
1926   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), NULL);
1927
1928   priv = menu->priv;
1929
1930   return priv->row_separator_func;
1931 }
1932
1933 /**
1934  * gtk_tree_menu_set_row_separator_func:
1935  * @menu: a #GtkTreeMenu
1936  * @func: (allow-none): a #GtkTreeViewRowSeparatorFunc, or %NULL to unset the separator function.
1937  * @data: (allow-none): user data to pass to @func, or %NULL
1938  * @destroy: (allow-none): destroy notifier for @data, or %NULL
1939  * 
1940  * Sets the row separator function, which is used to determine
1941  * whether a row should be drawn as a separator. If the row separator
1942  * function is %NULL, no separators are drawn. This is the default value.
1943  *
1944  * Since: 3.0
1945  */
1946 void
1947 gtk_tree_menu_set_row_separator_func (GtkTreeMenu          *menu,
1948                                       GtkTreeViewRowSeparatorFunc func,
1949                                       gpointer              data,
1950                                       GDestroyNotify        destroy)
1951 {
1952   GtkTreeMenuPrivate *priv;
1953
1954   g_return_if_fail (GTK_IS_TREE_MENU (menu));
1955
1956   priv = menu->priv;
1957
1958   if (priv->row_separator_destroy)
1959     priv->row_separator_destroy (priv->row_separator_data);
1960
1961   priv->row_separator_func    = func;
1962   priv->row_separator_data    = data;
1963   priv->row_separator_destroy = destroy;
1964
1965   rebuild_menu (menu);
1966 }
1967
1968 /**
1969  * gtk_tree_menu_get_header_func:
1970  * @menu: a #GtkTreeMenu
1971  * 
1972  * Gets the current #GtkTreeMenuHeaderFunc header function.
1973  * 
1974  * Return value: the current header function.
1975  *
1976  * Since: 3.0
1977  */
1978 GtkTreeMenuHeaderFunc
1979 gtk_tree_menu_get_header_func (GtkTreeMenu *menu)
1980 {
1981   GtkTreeMenuPrivate *priv;
1982
1983   g_return_val_if_fail (GTK_IS_TREE_MENU (menu), NULL);
1984
1985   priv = menu->priv;
1986
1987   return priv->header_func;
1988 }
1989
1990 /**
1991  * gtk_tree_menu_set_header_func:
1992  * @menu: a #GtkTreeMenu
1993  * @func: (allow-none): a #GtkTreeMenuHeaderFunc, or %NULL to unset the header function.
1994  * @data: (allow-none): user data to pass to @func, or %NULL
1995  * @destroy: (allow-none): destroy notifier for @data, or %NULL
1996  * 
1997  * Sets the header function, which is used to determine
1998  * whether a row width children should contain a leading header
1999  * menu item to allow that row to be selectable as an independant
2000  * menu item. If the header function is %NULL, no rows with children
2001  * have menu items which can be activated as leafs. 
2002  * This is the default value.
2003  *
2004  * Since: 3.0
2005  */
2006 void
2007 gtk_tree_menu_set_header_func (GtkTreeMenu          *menu,
2008                                GtkTreeMenuHeaderFunc func,
2009                                gpointer              data,
2010                                GDestroyNotify        destroy)
2011 {
2012   GtkTreeMenuPrivate *priv;
2013
2014   g_return_if_fail (GTK_IS_TREE_MENU (menu));
2015
2016   priv = menu->priv;
2017
2018   if (priv->header_destroy)
2019     priv->header_destroy (priv->header_data);
2020
2021   priv->header_func    = func;
2022   priv->header_data    = data;
2023   priv->header_destroy = destroy;
2024
2025   rebuild_menu (menu);
2026 }