]> Pileus Git - ~andy/gtk/blob - gtk/gtktreemenu.c
Change FSF Address
[~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  * Based on some GtkComboBox menu code by Kristian Rietveld <kris@gtk.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
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 "gtkseparatormenuitem.h"
40 #include "gtkcellareabox.h"
41 #include "gtkcellareacontext.h"
42 #include "gtkcelllayout.h"
43 #include "gtkcellview.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtkprivate.h"
46
47 #include "deprecated/gtktearoffmenuitem.h"
48
49 /* GObjectClass */
50 static GObject  *gtk_tree_menu_constructor                    (GType                  type,
51                                                                guint                  n_construct_properties,
52                                                                GObjectConstructParam *construct_properties);
53 static void      gtk_tree_menu_dispose                        (GObject            *object);
54 static void      gtk_tree_menu_finalize                       (GObject            *object);
55 static void      gtk_tree_menu_set_property                   (GObject            *object,
56                                                                guint               prop_id,
57                                                                const GValue       *value,
58                                                                GParamSpec         *pspec);
59 static void      gtk_tree_menu_get_property                   (GObject            *object,
60                                                                guint               prop_id,
61                                                                GValue             *value,
62                                                                GParamSpec         *pspec);
63
64 /* GtkWidgetClass */
65 static void      gtk_tree_menu_get_preferred_width            (GtkWidget           *widget,
66                                                                gint                *minimum_size,
67                                                                gint                *natural_size);
68 static void      gtk_tree_menu_get_preferred_height           (GtkWidget           *widget,
69                                                                gint                *minimum_size,
70                                                                gint                *natural_size);
71
72 /* GtkCellLayoutIface */
73 static void      gtk_tree_menu_cell_layout_init               (GtkCellLayoutIface  *iface);
74 static GtkCellArea *gtk_tree_menu_cell_layout_get_area        (GtkCellLayout        *layout);
75
76
77 /* TreeModel/DrawingArea callbacks and building menus/submenus */
78 static inline void rebuild_menu                               (GtkTreeMenu          *menu);
79 static gboolean   menu_occupied                               (GtkTreeMenu          *menu,
80                                                                guint                 left_attach,
81                                                                guint                 right_attach,
82                                                                guint                 top_attach,
83                                                                guint                 bottom_attach);
84 static void       relayout_item                               (GtkTreeMenu          *menu,
85                                                                GtkWidget            *item,
86                                                                GtkTreeIter          *iter,
87                                                                GtkWidget            *prev);
88 static void       gtk_tree_menu_populate                      (GtkTreeMenu          *menu);
89 static GtkWidget *gtk_tree_menu_create_item                   (GtkTreeMenu          *menu,
90                                                                GtkTreeIter          *iter,
91                                                                gboolean              header_item);
92 static void       gtk_tree_menu_create_submenu                (GtkTreeMenu          *menu,
93                                                                GtkWidget            *item,
94                                                                GtkTreePath          *path);
95 static void       gtk_tree_menu_set_area                      (GtkTreeMenu          *menu,
96                                                                GtkCellArea          *area);
97 static GtkWidget *gtk_tree_menu_get_path_item                 (GtkTreeMenu          *menu,
98                                                                GtkTreePath          *path);
99 static gboolean   gtk_tree_menu_path_in_menu                  (GtkTreeMenu          *menu,
100                                                                GtkTreePath          *path,
101                                                                gboolean             *header_item);
102 static void       row_inserted_cb                             (GtkTreeModel         *model,
103                                                                GtkTreePath          *path,
104                                                                GtkTreeIter          *iter,
105                                                                GtkTreeMenu          *menu);
106 static void       row_deleted_cb                              (GtkTreeModel         *model,
107                                                                GtkTreePath          *path,
108                                                                GtkTreeMenu          *menu);
109 static void       row_reordered_cb                            (GtkTreeModel         *model,
110                                                                GtkTreePath          *path,
111                                                                GtkTreeIter          *iter,
112                                                                gint                 *new_order,
113                                                                GtkTreeMenu          *menu);
114 static void       row_changed_cb                              (GtkTreeModel         *model,
115                                                                GtkTreePath          *path,
116                                                                GtkTreeIter          *iter,
117                                                                GtkTreeMenu          *menu);
118 static void       context_size_changed_cb                     (GtkCellAreaContext   *context,
119                                                                GParamSpec           *pspec,
120                                                                GtkWidget            *menu);
121 static void       area_apply_attributes_cb                    (GtkCellArea          *area,
122                                                                GtkTreeModel         *tree_model,
123                                                                GtkTreeIter          *iter,
124                                                                gboolean              is_expander,
125                                                                gboolean              is_expanded,
126                                                                GtkTreeMenu          *menu);
127 static void       item_activated_cb                           (GtkMenuItem          *item,
128                                                                GtkTreeMenu          *menu);
129 static void       submenu_activated_cb                        (GtkTreeMenu          *submenu,
130                                                                const gchar          *path,
131                                                                GtkTreeMenu          *menu);
132 static void       gtk_tree_menu_set_model_internal            (GtkTreeMenu          *menu,
133                                                                GtkTreeModel         *model);
134
135
136
137 struct _GtkTreeMenuPrivate
138 {
139   /* TreeModel and parent for this menu */
140   GtkTreeModel        *model;
141   GtkTreeRowReference *root;
142
143   /* CellArea and context for this menu */
144   GtkCellArea         *area;
145   GtkCellAreaContext  *context;
146
147   /* Signals */
148   gulong               size_changed_id;
149   gulong               apply_attributes_id;
150   gulong               row_inserted_id;
151   gulong               row_deleted_id;
152   gulong               row_reordered_id;
153   gulong               row_changed_id;
154
155   /* Grid menu mode */
156   gint                 wrap_width;
157   gint                 row_span_col;
158   gint                 col_span_col;
159
160   /* Flags */
161   guint32              menu_with_header : 1;
162   guint32              tearoff     : 1;
163
164   /* Row separators */
165   GtkTreeViewRowSeparatorFunc row_separator_func;
166   gpointer                    row_separator_data;
167   GDestroyNotify              row_separator_destroy;
168
169   /* Submenu headers */
170   GtkTreeMenuHeaderFunc header_func;
171   gpointer              header_data;
172   GDestroyNotify        header_destroy;
173 };
174
175 enum {
176   PROP_0,
177   PROP_MODEL,
178   PROP_ROOT,
179   PROP_CELL_AREA,
180   PROP_TEAROFF,
181   PROP_WRAP_WIDTH,
182   PROP_ROW_SPAN_COL,
183   PROP_COL_SPAN_COL
184 };
185
186 enum {
187   SIGNAL_MENU_ACTIVATE,
188   N_SIGNALS
189 };
190
191 static guint   tree_menu_signals[N_SIGNALS] = { 0 };
192 static GQuark  tree_menu_path_quark = 0;
193
194 G_DEFINE_TYPE_WITH_CODE (GtkTreeMenu, _gtk_tree_menu, GTK_TYPE_MENU,
195                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
196                                                 gtk_tree_menu_cell_layout_init));
197
198 static void
199 _gtk_tree_menu_init (GtkTreeMenu *menu)
200 {
201   GtkTreeMenuPrivate *priv;
202
203   menu->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu,
204                                             GTK_TYPE_TREE_MENU,
205                                             GtkTreeMenuPrivate);
206   priv = menu->priv;
207
208   priv->model     = NULL;
209   priv->root      = NULL;
210   priv->area      = NULL;
211   priv->context   = NULL;
212
213   priv->size_changed_id  = 0;
214   priv->row_inserted_id  = 0;
215   priv->row_deleted_id   = 0;
216   priv->row_reordered_id = 0;
217   priv->row_changed_id   = 0;
218
219   priv->wrap_width   = 0;
220   priv->row_span_col = -1;
221   priv->col_span_col = -1;
222
223   priv->menu_with_header = FALSE;
224   priv->tearoff          = FALSE;
225
226   priv->row_separator_func    = NULL;
227   priv->row_separator_data    = NULL;
228   priv->row_separator_destroy = NULL;
229
230   priv->header_func    = NULL;
231   priv->header_data    = NULL;
232   priv->header_destroy = NULL;
233
234   gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
235 }
236
237 static void
238 _gtk_tree_menu_class_init (GtkTreeMenuClass *class)
239 {
240   GObjectClass   *object_class = G_OBJECT_CLASS (class);
241   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
242
243   tree_menu_path_quark = g_quark_from_static_string ("gtk-tree-menu-path");
244
245   object_class->constructor  = gtk_tree_menu_constructor;
246   object_class->dispose      = gtk_tree_menu_dispose;
247   object_class->finalize     = gtk_tree_menu_finalize;
248   object_class->set_property = gtk_tree_menu_set_property;
249   object_class->get_property = gtk_tree_menu_get_property;
250
251   widget_class->get_preferred_width  = gtk_tree_menu_get_preferred_width;
252   widget_class->get_preferred_height = gtk_tree_menu_get_preferred_height;
253
254   /*
255    * GtkTreeMenu::menu-activate:
256    * @menu: a #GtkTreeMenu
257    * @path: the #GtkTreePath string for the item which was activated
258    * @user_data: the user data
259    *
260    * This signal is emitted to notify that a menu item in the #GtkTreeMenu
261    * was activated and provides the path string from the #GtkTreeModel
262    * to specify which row was selected.
263    *
264    * Since: 3.0
265    */
266   tree_menu_signals[SIGNAL_MENU_ACTIVATE] =
267     g_signal_new (I_("menu-activate"),
268                   G_OBJECT_CLASS_TYPE (object_class),
269                   G_SIGNAL_RUN_FIRST,
270                   0, /* No class closure here */
271                   NULL, NULL,
272                   _gtk_marshal_VOID__STRING,
273                   G_TYPE_NONE, 1, G_TYPE_STRING);
274
275   /*
276    * GtkTreeMenu:model:
277    *
278    * The #GtkTreeModel from which the menu is constructed.
279    *
280    * Since: 3.0
281    */
282   g_object_class_install_property (object_class,
283                                    PROP_MODEL,
284                                    g_param_spec_object ("model",
285                                                         P_("TreeMenu model"),
286                                                         P_("The model for the tree menu"),
287                                                         GTK_TYPE_TREE_MODEL,
288                                                         GTK_PARAM_READWRITE));
289
290   /*
291    * GtkTreeMenu:root:
292    *
293    * The #GtkTreePath that is the root for this menu, or %NULL.
294    *
295    * The #GtkTreeMenu recursively creates submenus for #GtkTreeModel
296    * rows that have children and the "root" for each menu is provided
297    * by the parent menu.
298    *
299    * If you dont provide a root for the #GtkTreeMenu then the whole
300    * model will be added to the menu. Specifying a root allows you
301    * to build a menu for a given #GtkTreePath and its children.
302    * 
303    * Since: 3.0
304    */
305   g_object_class_install_property (object_class,
306                                    PROP_ROOT,
307                                    g_param_spec_boxed ("root",
308                                                        P_("TreeMenu root row"),
309                                                        P_("The TreeMenu will display children of the "
310                                                           "specified root"),
311                                                        GTK_TYPE_TREE_PATH,
312                                                        GTK_PARAM_READWRITE));
313
314   /*
315    * GtkTreeMenu:cell-area:
316    *
317    * The #GtkCellArea used to render cells in the menu items.
318    *
319    * You can provide a different cell area at object construction
320    * time, otherwise the #GtkTreeMenu will use a #GtkCellAreaBox.
321    *
322    * Since: 3.0
323    */
324   g_object_class_install_property (object_class,
325                                    PROP_CELL_AREA,
326                                    g_param_spec_object ("cell-area",
327                                                         P_("Cell Area"),
328                                                         P_("The GtkCellArea used to layout cells"),
329                                                         GTK_TYPE_CELL_AREA,
330                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
331
332   /*
333    * GtkTreeMenu:tearoff:
334    *
335    * Specifies whether this menu comes with a leading tearoff menu item
336    *
337    * Since: 3.0
338    */
339   g_object_class_install_property (object_class,
340                                    PROP_TEAROFF,
341                                    g_param_spec_boolean ("tearoff",
342                                                          P_("Tearoff"),
343                                                          P_("Whether the menu has a tearoff item"),
344                                                          FALSE,
345                                                          GTK_PARAM_READWRITE));
346
347   /*
348    * GtkTreeMenu:wrap-width:
349    *
350    * If wrap-width is set to a positive value, the list will be
351    * displayed in multiple columns, the number of columns is
352    * determined by wrap-width.
353    *
354    * Since: 3.0
355    */
356   g_object_class_install_property (object_class,
357                                    PROP_WRAP_WIDTH,
358                                    g_param_spec_int ("wrap-width",
359                                                      P_("Wrap Width"),
360                                                      P_("Wrap width for laying out items in a grid"),
361                                                      0,
362                                                      G_MAXINT,
363                                                      0,
364                                                      GTK_PARAM_READWRITE));
365
366   /*
367    * GtkTreeMenu:row-span-column:
368    *
369    * If this is set to a non-negative value, it must be the index of a column
370    * of type %G_TYPE_INT in the model.
371    *
372    * The values of that column are used to determine how many rows a value in
373    * the list will span. Therefore, the values in the model column pointed to
374    * by this property must be greater than zero and not larger than wrap-width.
375    *
376    * Since: 3.0
377    */
378   g_object_class_install_property (object_class,
379                                    PROP_ROW_SPAN_COL,
380                                    g_param_spec_int ("row-span-column",
381                                                      P_("Row span column"),
382                                                      P_("TreeModel column containing the row span values"),
383                                                      -1,
384                                                      G_MAXINT,
385                                                      -1,
386                                                      GTK_PARAM_READWRITE));
387
388   /*
389    * GtkTreeMenu:column-span-column:
390    *
391    * If this is set to a non-negative value, it must be the index of a column
392    * of type %G_TYPE_INT in the model.
393    *
394    * The values of that column are used to determine how many columns a value
395    * in the list will span.
396    *
397    * Since: 3.0
398    */
399   g_object_class_install_property (object_class,
400                                    PROP_COL_SPAN_COL,
401                                    g_param_spec_int ("column-span-column",
402                                                      P_("Column span column"),
403                                                      P_("TreeModel column containing the column span values"),
404                                                      -1,
405                                                      G_MAXINT,
406                                                      -1,
407                                                      GTK_PARAM_READWRITE));
408
409   g_type_class_add_private (object_class, sizeof (GtkTreeMenuPrivate));
410 }
411
412 /****************************************************************
413  *                         GObjectClass                         *
414  ****************************************************************/
415 static GObject  *
416 gtk_tree_menu_constructor (GType                  type,
417                            guint                  n_construct_properties,
418                            GObjectConstructParam *construct_properties)
419 {
420   GObject            *object;
421   GtkTreeMenu        *menu;
422   GtkTreeMenuPrivate *priv;
423
424   object = G_OBJECT_CLASS (_gtk_tree_menu_parent_class)->constructor
425     (type, n_construct_properties, construct_properties);
426
427   menu = GTK_TREE_MENU (object);
428   priv = menu->priv;
429
430   if (!priv->area)
431     {
432       GtkCellArea *area = gtk_cell_area_box_new ();
433
434       gtk_tree_menu_set_area (menu, area);
435     }
436
437   priv->context = gtk_cell_area_create_context (priv->area);
438
439   priv->size_changed_id =
440     g_signal_connect (priv->context, "notify",
441                       G_CALLBACK (context_size_changed_cb), menu);
442
443   return object;
444 }
445
446 static void
447 gtk_tree_menu_dispose (GObject *object)
448 {
449   GtkTreeMenu        *menu;
450   GtkTreeMenuPrivate *priv;
451
452   menu = GTK_TREE_MENU (object);
453   priv = menu->priv;
454
455   _gtk_tree_menu_set_model (menu, NULL);
456   gtk_tree_menu_set_area (menu, NULL);
457
458   if (priv->context)
459     {
460       /* Disconnect signals */
461       g_signal_handler_disconnect (priv->context, priv->size_changed_id);
462
463       g_object_unref (priv->context);
464       priv->context = NULL;
465       priv->size_changed_id = 0;
466     }
467
468   G_OBJECT_CLASS (_gtk_tree_menu_parent_class)->dispose (object);
469 }
470
471 static void
472 gtk_tree_menu_finalize (GObject *object)
473 {
474   GtkTreeMenu        *menu;
475   GtkTreeMenuPrivate *priv;
476
477   menu = GTK_TREE_MENU (object);
478   priv = menu->priv;
479
480   _gtk_tree_menu_set_row_separator_func (menu, NULL, NULL, NULL);
481   _gtk_tree_menu_set_header_func (menu, NULL, NULL, NULL);
482
483   if (priv->root)
484     gtk_tree_row_reference_free (priv->root);
485
486   G_OBJECT_CLASS (_gtk_tree_menu_parent_class)->finalize (object);
487 }
488
489 static void
490 gtk_tree_menu_set_property (GObject            *object,
491                             guint               prop_id,
492                             const GValue       *value,
493                             GParamSpec         *pspec)
494 {
495   GtkTreeMenu *menu = GTK_TREE_MENU (object);
496
497   switch (prop_id)
498     {
499     case PROP_MODEL:
500       _gtk_tree_menu_set_model (menu, g_value_get_object (value));
501       break;
502
503     case PROP_ROOT:
504       _gtk_tree_menu_set_root (menu, g_value_get_boxed (value));
505       break;
506
507     case PROP_CELL_AREA:
508       /* Construct-only, can only be assigned once */
509       gtk_tree_menu_set_area (menu, (GtkCellArea *)g_value_get_object (value));
510       break;
511
512     case PROP_TEAROFF:
513       _gtk_tree_menu_set_tearoff (menu, g_value_get_boolean (value));
514       break;
515
516     case PROP_WRAP_WIDTH:
517       _gtk_tree_menu_set_wrap_width (menu, g_value_get_int (value));
518       break;
519
520      case PROP_ROW_SPAN_COL:
521       _gtk_tree_menu_set_row_span_column (menu, g_value_get_int (value));
522       break;
523
524      case PROP_COL_SPAN_COL:
525       _gtk_tree_menu_set_column_span_column (menu, g_value_get_int (value));
526       break;
527
528     default:
529       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
530       break;
531     }
532 }
533
534 static void
535 gtk_tree_menu_get_property (GObject            *object,
536                             guint               prop_id,
537                             GValue             *value,
538                             GParamSpec         *pspec)
539 {
540   GtkTreeMenu        *menu = GTK_TREE_MENU (object);
541   GtkTreeMenuPrivate *priv = menu->priv;
542
543   switch (prop_id)
544     {
545     case PROP_MODEL:
546       g_value_set_object (value, priv->model);
547       break;
548
549     case PROP_ROOT:
550       g_value_set_boxed (value, priv->root);
551       break;
552
553     case PROP_CELL_AREA:
554       g_value_set_object (value, priv->area);
555       break;
556
557     case PROP_TEAROFF:
558       g_value_set_boolean (value, priv->tearoff);
559       break;
560
561     default:
562       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
563       break;
564     }
565 }
566
567 /****************************************************************
568  *                         GtkWidgetClass                       *
569  ****************************************************************/
570
571 /* We tell all the menu items to reserve space for the submenu
572  * indicator if there is at least one submenu, this way we ensure
573  * that every internal cell area gets allocated the
574  * same width (and requested height for the same appropriate width).
575  */
576 static void
577 sync_reserve_submenu_size (GtkTreeMenu *menu)
578 {
579   GList              *children, *l;
580   gboolean            has_submenu = FALSE;
581
582   children = gtk_container_get_children (GTK_CONTAINER (menu));
583   for (l = children; l; l = l->next)
584     {
585       GtkMenuItem *item = l->data;
586
587       if (gtk_menu_item_get_submenu (item) != NULL)
588         {
589           has_submenu = TRUE;
590           break;
591         }
592     }
593
594   for (l = children; l; l = l->next)
595     {
596       GtkMenuItem *item = l->data;
597
598       gtk_menu_item_set_reserve_indicator (item, has_submenu);
599     }
600
601   g_list_free (children);
602 }
603
604 static void
605 gtk_tree_menu_get_preferred_width (GtkWidget           *widget,
606                                    gint                *minimum_size,
607                                    gint                *natural_size)
608 {
609   GtkTreeMenu        *menu = GTK_TREE_MENU (widget);
610   GtkTreeMenuPrivate *priv = menu->priv;
611
612   /* We leave the requesting work up to the cellviews which operate in the same
613    * context, reserving space for the submenu indicator if any of the items have
614    * submenus ensures that every cellview will receive the same allocated width.
615    *
616    * Since GtkMenu does hieght-for-width correctly, we know that the width of
617    * every cell will be requested before the height-for-widths are requested.
618    */
619   g_signal_handler_block (priv->context, priv->size_changed_id);
620
621   sync_reserve_submenu_size (menu);
622
623   GTK_WIDGET_CLASS (_gtk_tree_menu_parent_class)->get_preferred_width (widget, minimum_size, natural_size);
624
625   g_signal_handler_unblock (priv->context, priv->size_changed_id);
626 }
627
628 static void
629 gtk_tree_menu_get_preferred_height (GtkWidget           *widget,
630                                     gint                *minimum_size,
631                                     gint                *natural_size)
632 {
633   GtkTreeMenu        *menu = GTK_TREE_MENU (widget);
634   GtkTreeMenuPrivate *priv = menu->priv;
635
636   g_signal_handler_block (priv->context, priv->size_changed_id);
637
638   sync_reserve_submenu_size (menu);
639
640   GTK_WIDGET_CLASS (_gtk_tree_menu_parent_class)->get_preferred_height (widget, minimum_size, natural_size);
641
642   g_signal_handler_unblock (priv->context, priv->size_changed_id);
643 }
644
645 /****************************************************************
646  *                      GtkCellLayoutIface                      *
647  ****************************************************************/
648 static void
649 gtk_tree_menu_cell_layout_init (GtkCellLayoutIface  *iface)
650 {
651   iface->get_area = gtk_tree_menu_cell_layout_get_area;
652 }
653
654 static GtkCellArea *
655 gtk_tree_menu_cell_layout_get_area (GtkCellLayout *layout)
656 {
657   GtkTreeMenu        *menu = GTK_TREE_MENU (layout);
658   GtkTreeMenuPrivate *priv = menu->priv;
659
660   return priv->area;
661 }
662
663
664 /****************************************************************
665  *             TreeModel callbacks/populating menus             *
666  ****************************************************************/
667 static GtkWidget *
668 gtk_tree_menu_get_path_item (GtkTreeMenu          *menu,
669                              GtkTreePath          *search)
670 {
671   GtkWidget *item = NULL;
672   GList     *children, *l;
673
674   children = gtk_container_get_children (GTK_CONTAINER (menu));
675
676   for (l = children; item == NULL && l != NULL; l = l->next)
677     {
678       GtkWidget   *child = l->data;
679       GtkTreePath *path  = NULL;
680
681       if (GTK_IS_SEPARATOR_MENU_ITEM (child))
682         {
683           GtkTreeRowReference *row =
684             g_object_get_qdata (G_OBJECT (child), tree_menu_path_quark);
685
686           if (row)
687             {
688               path = gtk_tree_row_reference_get_path (row);
689
690               if (!path)
691                 /* Return any first child where its row-reference became invalid,
692                  * this is because row-references get null paths before we recieve
693                  * the "row-deleted" signal.
694                  */
695                 item = child;
696             }
697         }
698       else if (!GTK_IS_TEAROFF_MENU_ITEM (child))
699         {
700           GtkWidget *view = gtk_bin_get_child (GTK_BIN (child));
701
702           /* It's always a cellview */
703           if (GTK_IS_CELL_VIEW (view))
704             path = gtk_cell_view_get_displayed_row (GTK_CELL_VIEW (view));
705
706           if (!path)
707             /* Return any first child where its row-reference became invalid,
708              * this is because row-references get null paths before we recieve
709              * the "row-deleted" signal.
710              */
711             item = child;
712         }
713
714       if (path)
715         {
716           if (gtk_tree_path_compare (search, path) == 0)
717             item = child;
718
719           gtk_tree_path_free (path);
720         }
721     }
722
723   g_list_free (children);
724
725   return item;
726 }
727
728 static gboolean
729 gtk_tree_menu_path_in_menu (GtkTreeMenu  *menu,
730                             GtkTreePath  *path,
731                             gboolean     *header_item)
732 {
733   GtkTreeMenuPrivate *priv = menu->priv;
734   gboolean            in_menu = FALSE;
735   gboolean            is_header = FALSE;
736
737   /* Check if the is in root of the model */
738   if (gtk_tree_path_get_depth (path) == 1 && !priv->root)
739     in_menu = TRUE;
740   /* If we are a submenu, compare the parent path */
741   else if (priv->root)
742     {
743       GtkTreePath *root_path   = gtk_tree_row_reference_get_path (priv->root);
744       GtkTreePath *search_path = gtk_tree_path_copy (path);
745
746       if (root_path)
747         {
748           if (priv->menu_with_header &&
749               gtk_tree_path_compare (root_path, search_path) == 0)
750             {
751               in_menu   = TRUE;
752               is_header = TRUE;
753             }
754           else if (gtk_tree_path_get_depth (search_path) > 1)
755             {
756               gtk_tree_path_up (search_path);
757
758               if (gtk_tree_path_compare (root_path, search_path) == 0)
759                 in_menu = TRUE;
760             }
761         }
762       gtk_tree_path_free (root_path);
763       gtk_tree_path_free (search_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 its 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 (gtk_tree_path_get_depth (path) == 0 && !priv->root)
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)->priv->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)->priv->children &&
1142                   GTK_MENU_SHELL (menu)->priv->children->next)
1143                 {
1144                   GtkWidget *separator =
1145                     GTK_MENU_SHELL (menu)->priv->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)->priv->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 its 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 its 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  * @path: (allow-none): the #GtkTreePath which is the root of @menu, or %NULL.
1639  *
1640  * Sets the root of a @menu's hierarchy to be @path. @menu must already
1641  * have a model set and @path 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 }