1 /*
2  * Copyright (c) 2014 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 #include <glib/gi18n-lib.h>
20 
21 #include "menu.h"
22 
23 #include "gtktreestore.h"
24 #include "gtkwidgetprivate.h"
25 #include "gtklabel.h"
26 
27 
28 enum
29 {
30   COLUMN_TYPE,
31   COLUMN_LABEL,
32   COLUMN_ACTION,
33   COLUMN_TARGET,
34   COLUMN_ICON
35 };
36 
37 struct _GtkInspectorMenuPrivate
38 {
39   GtkTreeStore *model;
40 };
41 
G_DEFINE_TYPE_WITH_PRIVATE(GtkInspectorMenu,gtk_inspector_menu,GTK_TYPE_BOX)42 G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorMenu, gtk_inspector_menu, GTK_TYPE_BOX)
43 
44 static void
45 gtk_inspector_menu_init (GtkInspectorMenu *sl)
46 {
47   sl->priv = gtk_inspector_menu_get_instance_private (sl);
48   gtk_widget_init_template (GTK_WIDGET (sl));
49 }
50 
51 static void add_menu (GtkInspectorMenu *sl,
52                       GMenuModel       *menu,
53                       GtkTreeIter      *parent);
54 
55 static void
add_item(GtkInspectorMenu * sl,GMenuModel * menu,gint idx,GtkTreeIter * parent)56 add_item (GtkInspectorMenu *sl,
57           GMenuModel       *menu,
58           gint              idx,
59           GtkTreeIter      *parent)
60 {
61   GtkTreeIter iter;
62   GVariant *value;
63   gchar *label = NULL;
64   gchar *action = NULL;
65   gchar *target = NULL;
66   gchar *icon = NULL;
67   GMenuModel *model;
68 
69   g_menu_model_get_item_attribute (menu, idx, G_MENU_ATTRIBUTE_LABEL, "s", &label);
70   g_menu_model_get_item_attribute (menu, idx, G_MENU_ATTRIBUTE_ACTION, "s", &action);
71   value = g_menu_model_get_item_attribute_value (menu, idx, G_MENU_ATTRIBUTE_TARGET, NULL);
72   if (value)
73     {
74       target = g_variant_print (value, FALSE);
75       g_variant_unref (value);
76     }
77 
78   gtk_tree_store_append (sl->priv->model, &iter, parent);
79   gtk_tree_store_set (sl->priv->model, &iter,
80                       COLUMN_TYPE, "item",
81                       COLUMN_LABEL, label,
82                       COLUMN_ACTION, action,
83                       COLUMN_TARGET, target,
84                       COLUMN_ICON, icon,
85                       -1);
86 
87   model = g_menu_model_get_item_link (menu, idx, G_MENU_LINK_SECTION);
88   if (model)
89     {
90       if (label == NULL)
91         gtk_tree_store_set (sl->priv->model, &iter,
92                             COLUMN_LABEL, _("Unnamed section"),
93                             -1);
94       add_menu (sl, model, &iter);
95       g_object_unref (model);
96     }
97 
98   model = g_menu_model_get_item_link (menu, idx, G_MENU_LINK_SUBMENU);
99   if (model)
100     {
101       add_menu (sl, model, &iter);
102       g_object_unref (model);
103     }
104 
105   g_free (label);
106   g_free (action);
107   g_free (target);
108   g_free (icon);
109 }
110 
111 static void
add_menu(GtkInspectorMenu * sl,GMenuModel * menu,GtkTreeIter * parent)112 add_menu (GtkInspectorMenu *sl,
113           GMenuModel       *menu,
114           GtkTreeIter      *parent)
115 {
116   gint n_items;
117   gint i;
118 
119   gtk_widget_show (GTK_WIDGET (sl));
120 
121   n_items = g_menu_model_get_n_items (menu);
122   for (i = 0; i < n_items; i++)
123     add_item (sl, menu, i, parent);
124 }
125 
126 void
gtk_inspector_menu_set_object(GtkInspectorMenu * sl,GObject * object)127 gtk_inspector_menu_set_object (GtkInspectorMenu *sl,
128                                GObject          *object)
129 {
130   gtk_widget_hide (GTK_WIDGET (sl));
131   gtk_tree_store_clear (sl->priv->model);
132 
133   if (G_IS_MENU_MODEL (object))
134     add_menu (sl, G_MENU_MODEL (object), NULL);
135 }
136 
137 static void
gtk_inspector_menu_class_init(GtkInspectorMenuClass * klass)138 gtk_inspector_menu_class_init (GtkInspectorMenuClass *klass)
139 {
140   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
141 
142   gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/inspector/menu.ui");
143   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorMenu, model);
144 }
145 
146 // vim: set et sw=2 ts=2:
147