]> Pileus Git - ~andy/gtk/blob - gtk/gtkmodelmenuitem.c
Change FSF Address
[~andy/gtk] / gtk / gtkmodelmenuitem.c
1 /*
2  * Copyright © 2011 Canonical Limited
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 licence, 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  * Author: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gtkmodelmenuitem.h"
23
24 #include "gtkaccelmapprivate.h"
25 #include "gtkmodelmenu.h"
26
27 struct _GtkModelMenuItem
28 {
29   GtkCheckMenuItem parent_instance;
30
31   GActionGroup *actions;
32   const gchar *action_name;
33   gboolean has_indicator;
34   gboolean can_activate;
35   GVariant *target;
36 };
37
38 typedef GtkCheckMenuItemClass GtkModelMenuItemClass;
39
40 static void gtk_model_menu_item_observer_iface_init (GActionObserverInterface *iface);
41 G_DEFINE_TYPE_WITH_CODE (GtkModelMenuItem, gtk_model_menu_item, GTK_TYPE_CHECK_MENU_ITEM,
42                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_OBSERVER, gtk_model_menu_item_observer_iface_init))
43
44 static void
45 gtk_model_menu_item_activate (GtkMenuItem *menu_item)
46 {
47   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
48
49   if (item->can_activate)
50     g_action_group_activate_action (item->actions, item->action_name, item->target);
51 }
52
53 static void
54 gtk_model_menu_item_toggle_size_request (GtkMenuItem *menu_item,
55                                          gint        *requisition)
56 {
57   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
58
59   if (item->has_indicator)
60     GTK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
61       ->toggle_size_request (menu_item, requisition);
62
63   else
64     *requisition = 0;
65 }
66
67 static void
68 gtk_model_menu_item_draw_indicator (GtkCheckMenuItem *check_item,
69                                     cairo_t          *cr)
70 {
71   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (check_item);
72
73   if (item->has_indicator)
74     GTK_CHECK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
75       ->draw_indicator (check_item, cr);
76 }
77
78 static void
79 gtk_model_menu_item_set_active (GtkModelMenuItem *item,
80                                 gboolean          active)
81 {
82   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (item);
83
84   if (gtk_check_menu_item_get_active (checkitem) != active)
85     {
86       _gtk_check_menu_item_set_active (checkitem, active);
87       g_object_notify (G_OBJECT (checkitem), "active");
88       gtk_check_menu_item_toggled (checkitem);
89       gtk_widget_queue_draw (GTK_WIDGET (item));
90     }
91 }
92
93 static void
94 gtk_model_menu_item_action_added (GActionObserver    *observer,
95                                   GActionObservable  *observable,
96                                   const gchar        *action_name,
97                                   const GVariantType *parameter_type,
98                                   gboolean            enabled,
99                                   GVariant           *state)
100 {
101   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
102
103   /* we can only activate the item if we have the correct type of parameter */
104   item->can_activate = (item->target == NULL && parameter_type == NULL) ||
105                        (item->target != NULL && parameter_type != NULL &&
106                         g_variant_is_of_type (item->target, parameter_type));
107
108   if (item->can_activate)
109     {
110       if (item->target != NULL && state != NULL)
111         {
112           /* actions with states and targets are radios */
113           gboolean selected;
114
115           selected = g_variant_equal (state, item->target);
116           gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
117           gtk_model_menu_item_set_active (item, selected);
118           item->has_indicator = TRUE;
119         }
120
121       else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
122         {
123           /* boolean state actions without target are checks */
124           gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), FALSE);
125           gtk_model_menu_item_set_active (item, g_variant_get_boolean (state));
126           item->has_indicator = TRUE;
127         }
128
129       else
130         {
131           /* stateless items are just plain actions */
132           gtk_model_menu_item_set_active (item, FALSE);
133           item->has_indicator = FALSE;
134         }
135
136       gtk_widget_set_sensitive (GTK_WIDGET (item), enabled);
137       gtk_widget_queue_resize (GTK_WIDGET (item));
138     }
139 }
140
141 static void
142 gtk_model_menu_item_action_enabled_changed (GActionObserver   *observer,
143                                             GActionObservable *observable,
144                                             const gchar       *action_name,
145                                             gboolean           enabled)
146 {
147   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
148
149   if (!item->can_activate)
150     return;
151
152   gtk_widget_set_sensitive (GTK_WIDGET (item), item->can_activate && enabled);
153 }
154
155 static void
156 gtk_model_menu_item_action_state_changed (GActionObserver   *observer,
157                                           GActionObservable *observable,
158                                           const gchar       *action_name,
159                                           GVariant          *state)
160 {
161   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
162
163   if (!item->can_activate)
164     return;
165
166   if (item->target)
167     gtk_model_menu_item_set_active (item, g_variant_equal (state, item->target));
168
169   else if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
170     gtk_model_menu_item_set_active (item, g_variant_get_boolean (state));
171 }
172
173 static void
174 gtk_model_menu_item_action_removed (GActionObserver   *observer,
175                                     GActionObservable *observable,
176                                     const gchar       *action_name)
177 {
178   GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
179
180   if (!item->can_activate)
181     return;
182
183   gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
184   gtk_model_menu_item_set_active (item, FALSE);
185   item->has_indicator = FALSE;
186
187   gtk_widget_queue_resize (GTK_WIDGET (item));
188 }
189
190 static void
191 gtk_model_menu_item_setup (GtkModelMenuItem  *item,
192                            GMenuModel        *model,
193                            gint               item_index,
194                            GActionObservable *actions,
195                            GtkAccelGroup     *accels)
196 {
197   GMenuAttributeIter *iter;
198   GMenuModel *submenu;
199   const gchar *key;
200   GVariant *value;
201
202   if ((submenu = g_menu_model_get_item_link (model, item_index, "submenu")))
203     {
204       gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_model_menu_create_menu (submenu, actions, accels));
205       g_object_unref (submenu);
206     }
207
208   iter = g_menu_model_iterate_item_attributes (model, item_index);
209   while (g_menu_attribute_iter_get_next (iter, &key, &value))
210     {
211       if (g_str_equal (key, "label") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
212         gtk_menu_item_set_label (GTK_MENU_ITEM (item), g_variant_get_string (value, NULL));
213
214       else if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
215         item->action_name = g_variant_get_string (value, NULL);
216
217       else if (g_str_equal (key, "target"))
218         item->target = g_variant_ref (value);
219
220       g_variant_unref (value);
221     }
222   g_object_unref (iter);
223
224   gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
225
226   if (item->action_name)
227     {
228       const GVariantType *type;
229       gboolean enabled;
230       GVariant *state;
231       gchar *path;
232
233       /* observer already causes us to hold a hard ref on the group */
234       item->actions = G_ACTION_GROUP (actions);
235
236       if (actions)
237         {
238           g_action_observable_register_observer (actions, item->action_name, G_ACTION_OBSERVER (item));
239
240           if (g_action_group_query_action (G_ACTION_GROUP (actions), item->action_name, &enabled, &type, NULL, NULL, &state))
241             {
242               gtk_model_menu_item_action_added (G_ACTION_OBSERVER (item), actions, item->action_name, type, enabled, state);
243               if (state != NULL)
244                 g_variant_unref (state);
245             }
246           else
247             gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
248         }
249       else
250         gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
251
252       path = _gtk_accel_path_for_action (item->action_name, item->target);
253       gtk_menu_item_set_accel_path (GTK_MENU_ITEM (item), path);
254       g_free (path);
255     }
256 }
257
258 static void
259 gtk_model_menu_item_finalize (GObject *object)
260 {
261   G_OBJECT_CLASS (gtk_model_menu_item_parent_class)
262     ->finalize (object);
263 }
264
265 static void
266 gtk_model_menu_item_init (GtkModelMenuItem *item)
267 {
268 }
269
270 static void
271 gtk_model_menu_item_observer_iface_init (GActionObserverInterface *iface)
272 {
273   iface->action_added = gtk_model_menu_item_action_added;
274   iface->action_enabled_changed = gtk_model_menu_item_action_enabled_changed;
275   iface->action_state_changed = gtk_model_menu_item_action_state_changed;
276   iface->action_removed = gtk_model_menu_item_action_removed;
277 }
278
279 static void
280 gtk_model_menu_item_class_init (GtkModelMenuItemClass *class)
281 {
282   GtkCheckMenuItemClass *check_class = GTK_CHECK_MENU_ITEM_CLASS (class);
283   GtkMenuItemClass *item_class = GTK_MENU_ITEM_CLASS (class);
284   GObjectClass *object_class = G_OBJECT_CLASS (class);
285
286   check_class->draw_indicator = gtk_model_menu_item_draw_indicator;
287
288   item_class->activate = gtk_model_menu_item_activate;
289   item_class->toggle_size_request = gtk_model_menu_item_toggle_size_request;
290
291   object_class->finalize = gtk_model_menu_item_finalize;
292 }
293
294 GtkMenuItem *
295 gtk_model_menu_item_new (GMenuModel        *model,
296                          gint               item_index,
297                          GActionObservable *actions,
298                          GtkAccelGroup     *accels)
299 {
300   GtkModelMenuItem *item;
301
302   item = g_object_new (GTK_TYPE_MODEL_MENU_ITEM, NULL);
303
304   gtk_model_menu_item_setup (item, model, item_index, actions, accels);
305
306   return GTK_MENU_ITEM (item);
307 }