~khurshid-alam/gtk/packaging-xenial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
From 59168f3c1a72f610b693cd1ed4cfa5fac281079a Mon Sep 17 00:00:00 2001
From: Lars Uebernickel <lars.uebernickel@canonical.com>
Date: Wed, 6 Nov 2013 14:48:19 +0100
Subject: [PATCH] Add UbuntuMenuItemFactory

UbuntuMenuItemFactory is an interface for creating widgets for menu
items in a GMenuModel that have an 'x-canonical-type' attribute.

It is needed by unity and should not be considered public API.
---
 gtk/Makefile.am             |  3 ++
 gtk/gtkmenushell.c          | 69 ++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkmenutrackeritem.c    | 16 +++++++++++
 gtk/gtkmenutrackeritem.h    |  4 +++
 gtk/ubuntu-private.h        | 32 +++++++++++++++++++++
 gtk/ubuntumenuitemfactory.c | 70 +++++++++++++++++++++++++++++++++++++++++++++
 gtk/ubuntumenuitemfactory.h | 61 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 255 insertions(+)
 create mode 100644 gtk/ubuntu-private.h
 create mode 100644 gtk/ubuntumenuitemfactory.c
 create mode 100644 gtk/ubuntumenuitemfactory.h

diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 51153e6..3c01638 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -108,6 +108,8 @@ include $(srcdir)/inspector/Makefile.inc
 gtk_public_h_sources = 		\
 	gtk.h			\
 	gtk-autocleanups.h	\
+	ubuntu-private.h	\
+	ubuntumenuitemfactory.h	\
 	gtkx.h			\
 	gtkx-autocleanups.h	\
 	gtk-a11y.h		\
@@ -528,6 +530,7 @@ gtk_base_c_sources = 		\
 	$(a11y_c_sources)	\
 	$(deprecated_c_sources)	\
 	$(inspector_c_sources)	\
+	ubuntumenuitemfactory.c \
 	gtkactionmuxer.c	\
 	gtkactionobserver.c	\
 	gtkactionobservable.c	\
diff --git a/gtk/gtkmenushell.c b/gtk/gtkmenushell.c
index e1ba138..6d7a2f6 100644
--- a/gtk/gtkmenushell.c
+++ b/gtk/gtkmenushell.c
@@ -80,6 +80,8 @@
 
 #include "a11y/gtkmenushellaccessible.h"
 
+#include "ubuntu-private.h"
+
 
 #define MENU_SHELL_TIMEOUT   500
 #define MENU_POPUP_DELAY     225
@@ -2044,6 +2046,58 @@ gtk_menu_shell_tracker_remove_func (gint     position,
   gtk_widget_destroy (child);
 }
 
+static GtkWidget *
+create_custom_menu_item (GMenuItem   *item,
+                         GtkWidget   *parent,
+                         const gchar *action_namespace)
+{
+  gchar *type;
+  GActionGroup *actions;
+  GtkMenuItem *widget = NULL;
+  GList *it;
+
+  g_menu_item_get_attribute (item, "x-canonical-type", "s", &type);
+
+  if (action_namespace)
+    {
+      gchar *action;
+
+      /* Rewrite the menu item to include the fully qualified action
+       * name to make writing widgets easier. This won't break, as
+       * we don't use the tracker item for custom items.
+       */
+      if (g_menu_item_get_attribute (item, "action", "s", &action))
+        {
+          gchar *fullname;
+
+          fullname = g_strconcat (action_namespace, ".", action, NULL);
+          g_menu_item_set_attribute (item, "action", "s", fullname);
+
+          g_free (fullname);
+          g_free (action);
+        }
+    }
+
+  /* Passing the parent muxer is wrong, but we'll only have access
+   * to the menuitem's muxer after the widget has been created.
+   * Thus we'd need some other form of passing the action group to
+   * the widget, which would complicate things for no practical
+   * reason: the panel service is the only consumer of this API and
+   * it will never call gtk_widget_insert_action_group() on the
+   * returned menu item.
+   */
+  actions = G_ACTION_GROUP (_gtk_widget_get_action_muxer (parent, TRUE));
+
+  for (it = ubuntu_menu_item_factory_get_all (); it != NULL && widget == NULL; it = it->next)
+    widget = ubuntu_menu_item_factory_create_menu_item (it->data, type, item, actions);
+
+  if (widget == NULL)
+    g_warning ("Cannot create custom menu item of type '%s'", type);
+
+  g_free (type);
+  return GTK_WIDGET (widget);
+}
+
 static void
 gtk_menu_shell_tracker_insert_func (GtkMenuTrackerItem *item,
                                     gint                position,
@@ -2051,6 +2105,9 @@ gtk_menu_shell_tracker_insert_func (GtkMenuTrackerItem *item,
 {
   GtkMenuShell *menu_shell = user_data;
   GtkWidget *widget;
+  GMenuItem *menuitem;
+
+  menuitem = gtk_menu_tracker_item_get_menu_item (item);
 
   if (gtk_menu_tracker_item_get_is_separator (item))
     {
@@ -2112,6 +2169,18 @@ gtk_menu_shell_tracker_insert_func (GtkMenuTrackerItem *item,
 
       gtk_widget_show (widget);
     }
+  else if (g_menu_item_get_attribute (menuitem, "x-canonical-type", "s", NULL))
+    {
+      const gchar *namespace;
+
+      namespace = gtk_menu_tracker_item_get_action_namespace (item);
+      widget = create_custom_menu_item (menuitem, GTK_WIDGET (menu_shell), namespace);
+
+      if (widget == NULL)
+        return;
+
+      gtk_widget_show (widget);
+    }
   else
     {
       widget = gtk_model_menu_item_new ();
diff --git a/gtk/gtkmenutrackeritem.c b/gtk/gtkmenutrackeritem.c
index c304b66..cd0c796 100644
--- a/gtk/gtkmenutrackeritem.c
+++ b/gtk/gtkmenutrackeritem.c
@@ -979,3 +979,19 @@ gtk_menu_tracker_item_may_disappear (GtkMenuTrackerItem *self)
 {
   return self->hidden_when != HIDDEN_NEVER;
 }
+
+GMenuItem *
+gtk_menu_tracker_item_get_menu_item (GtkMenuTrackerItem *self)
+{
+  g_return_val_if_fail (GTK_IS_MENU_TRACKER_ITEM (self), NULL);
+
+  return self->item;
+}
+
+const gchar *
+gtk_menu_tracker_item_get_action_namespace (GtkMenuTrackerItem *self)
+{
+  g_return_val_if_fail (GTK_IS_MENU_TRACKER_ITEM (self), NULL);
+
+  return self->action_namespace;
+}
diff --git a/gtk/gtkmenutrackeritem.h b/gtk/gtkmenutrackeritem.h
index 6b4fcb5..d74fe92 100644
--- a/gtk/gtkmenutrackeritem.h
+++ b/gtk/gtkmenutrackeritem.h
@@ -92,4 +92,8 @@ void                    gtk_menu_tracker_item_request_submenu_shown     (GtkMenu
 
 gboolean                gtk_menu_tracker_item_get_submenu_shown         (GtkMenuTrackerItem *self);
 
+GMenuItem *             gtk_menu_tracker_item_get_menu_item             (GtkMenuTrackerItem *self);
+
+const gchar *           gtk_menu_tracker_item_get_action_namespace      (GtkMenuTrackerItem *self);
+
 #endif
diff --git a/gtk/ubuntu-private.h b/gtk/ubuntu-private.h
new file mode 100644
index 0000000..3ed366f
--- /dev/null
+++ b/gtk/ubuntu-private.h
@@ -0,0 +1,32 @@
+/*
+* Copyright 2013 Canonical Ltd.
+*
+* This program is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License version 3, as published
+* by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranties of
+* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+* PURPOSE.  See the GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License along
+* with this program.  If not, see <http://www.gnu.org/licenses/>.
+*
+* Authors:
+*     Lars Uebernickel <lars.uebernickel@canonical.com>
+*/
+
+/*
+ * Warning: this file is not part of gtk+, but an Ubuntu-specific extension.
+ * The API provided here is meant to be used only from Unity.
+ *
+ * Applications should not use this.
+ */
+
+#ifndef __UBUNTU_PRIVATE_H__
+#define __UBUNTU_PRIVATE_H__
+
+#include "ubuntumenuitemfactory.h"
+
+#endif
diff --git a/gtk/ubuntumenuitemfactory.c b/gtk/ubuntumenuitemfactory.c
new file mode 100644
index 0000000..24eb214
--- /dev/null
+++ b/gtk/ubuntumenuitemfactory.c
@@ -0,0 +1,70 @@
+/*
+* Copyright 2013 Canonical Ltd.
+*
+* This program is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License version 3, as published
+* by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranties of
+* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+* PURPOSE.  See the GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License along
+* with this program.  If not, see <http://www.gnu.org/licenses/>.
+*
+* Authors:
+*     Lars Uebernickel <lars.uebernickel@canonical.com>
+*/
+
+#include "config.h"
+#include "ubuntumenuitemfactory.h"
+
+G_DEFINE_INTERFACE_WITH_CODE (UbuntuMenuItemFactory, ubuntu_menu_item_factory, G_TYPE_OBJECT,
+  GIOExtensionPoint *ep = g_io_extension_point_register (UBUNTU_MENU_ITEM_FACTORY_EXTENSION_POINT_NAME);
+  g_io_extension_point_set_required_type (ep, g_define_type_id);)
+
+/*
+ * ubuntu_menu_item_factory_get_all:
+ *
+ * Returns a static list of all registered factories.
+ */
+GList *
+ubuntu_menu_item_factory_get_all (void)
+{
+  static GList *factories = NULL;
+
+  if (factories == NULL)
+    {
+      GIOExtensionPoint *ep;
+      GList *it;
+
+      g_type_ensure (UBUNTU_TYPE_MENU_ITEM_FACTORY);
+      ep = g_io_extension_point_lookup (UBUNTU_MENU_ITEM_FACTORY_EXTENSION_POINT_NAME);
+      for (it = g_io_extension_point_get_extensions (ep); it != NULL; it = it->next)
+        {
+          GIOExtension *ext = it->data;
+          UbuntuMenuItemFactory *factory;
+
+          factory = g_object_new (g_io_extension_get_type (ext), NULL);
+          factories = g_list_prepend (factories, factory);
+        }
+      factories = g_list_reverse (factories);
+    }
+
+  return factories;
+}
+
+static void
+ubuntu_menu_item_factory_default_init (UbuntuMenuItemFactoryInterface *iface)
+{
+}
+
+GtkMenuItem *
+ubuntu_menu_item_factory_create_menu_item (UbuntuMenuItemFactory *factory,
+                                           const gchar           *type,
+                                           GMenuItem             *menuitem,
+                                           GActionGroup          *actions)
+{
+  return UBUNTU_MENU_ITEM_FACTORY_GET_IFACE (factory)->create_menu_item (factory, type, menuitem, actions);
+}
diff --git a/gtk/ubuntumenuitemfactory.h b/gtk/ubuntumenuitemfactory.h
new file mode 100644
index 0000000..e97539d
--- /dev/null
+++ b/gtk/ubuntumenuitemfactory.h
@@ -0,0 +1,61 @@
+/*
+* Copyright 2013 Canonical Ltd.
+*
+* This program is free software: you can redistribute it and/or modify it
+* under the terms of the GNU General Public License version 3, as published
+* by the Free Software Foundation.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranties of
+* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+* PURPOSE.  See the GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License along
+* with this program.  If not, see <http://www.gnu.org/licenses/>.
+*
+* Authors:
+*     Lars Uebernickel <lars.uebernickel@canonical.com>
+*/
+
+#ifndef __UBUNTU_MENU_ITEM_FACTORY_H__
+#define __UBUNTU_MENU_ITEM_FACTORY_H__
+
+#include <gtk/gtkmenuitem.h>
+
+G_BEGIN_DECLS
+
+#define UBUNTU_TYPE_MENU_ITEM_FACTORY         (ubuntu_menu_item_factory_get_type ())
+#define UBUNTU_MENU_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), UBUNTU_TYPE_MENU_ITEM_FACTORY, UbuntuMenuItemFactory))
+#define UBUNTU_IS_MENU_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), UBUNTU_TYPE_MENU_ITEM_FACTORY))
+#define UBUNTU_MENU_ITEM_FACTORY_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), UBUNTU_TYPE_MENU_ITEM_FACTORY, UbuntuMenuItemFactoryInterface))
+
+#define UBUNTU_MENU_ITEM_FACTORY_EXTENSION_POINT_NAME "ubuntu-menu-item-factory"
+
+typedef struct _UbuntuMenuItemFactoryInterface UbuntuMenuItemFactoryInterface;
+typedef struct _UbuntuMenuItemFactory          UbuntuMenuItemFactory;
+
+struct _UbuntuMenuItemFactoryInterface
+{
+  GTypeInterface iface;
+
+  GtkMenuItem * (*create_menu_item)  (UbuntuMenuItemFactory *factory,
+                                      const gchar           *type,
+                                      GMenuItem             *menuitem,
+                                      GActionGroup          *actions);
+};
+
+GDK_AVAILABLE_IN_3_10
+GList *                 ubuntu_menu_item_factory_get_all                (void);
+
+GDK_AVAILABLE_IN_3_10
+GType                   ubuntu_menu_item_factory_get_type               (void);
+
+GDK_AVAILABLE_IN_3_10
+GtkMenuItem *           ubuntu_menu_item_factory_create_menu_item       (UbuntuMenuItemFactory *factory,
+                                                                         const gchar           *type,
+                                                                         GMenuItem             *menuitem,
+                                                                         GActionGroup          *actions);
+
+G_END_DECLS
+
+#endif
-- 
2.1.4