2 * Copyright © 2011 Canonical Limited
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkmodelmenuitem.h"
26 #include "gtkaccelmap.h"
27 #include "gtkmodelmenu.h"
29 struct _GtkModelMenuItem
31 GtkCheckMenuItem parent_instance;
33 GActionGroup *actions;
34 const gchar *action_name;
35 gboolean has_indicator;
36 gboolean can_activate;
40 typedef GtkCheckMenuItemClass GtkModelMenuItemClass;
42 static void gtk_model_menu_item_observer_iface_init (GActionObserverInterface *iface);
43 G_DEFINE_TYPE_WITH_CODE (GtkModelMenuItem, gtk_model_menu_item, GTK_TYPE_CHECK_MENU_ITEM,
44 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_OBSERVER, gtk_model_menu_item_observer_iface_init))
47 gtk_model_menu_item_activate (GtkMenuItem *menu_item)
49 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
51 if (item->can_activate)
52 g_action_group_activate_action (item->actions, item->action_name, item->target);
56 gtk_model_menu_item_toggle_size_request (GtkMenuItem *menu_item,
59 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
61 if (item->has_indicator)
62 GTK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
63 ->toggle_size_request (menu_item, requisition);
70 gtk_model_menu_item_draw_indicator (GtkCheckMenuItem *check_item,
73 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (check_item);
75 if (item->has_indicator)
76 GTK_CHECK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
77 ->draw_indicator (check_item, cr);
81 gtk_model_menu_item_set_active (GtkModelMenuItem *item,
84 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (item);
86 if (gtk_check_menu_item_get_active (checkitem) != active)
88 _gtk_check_menu_item_set_active (checkitem, active);
89 g_object_notify (G_OBJECT (checkitem), "active");
90 gtk_check_menu_item_toggled (checkitem);
91 gtk_widget_queue_draw (GTK_WIDGET (item));
96 gtk_model_menu_item_action_added (GActionObserver *observer,
97 GActionObservable *observable,
98 const gchar *action_name,
99 const GVariantType *parameter_type,
103 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
105 /* we can only activate the item if we have the correct type of parameter */
106 item->can_activate = (item->target == NULL && parameter_type == NULL) ||
107 (item->target != NULL && parameter_type != NULL &&
108 g_variant_is_of_type (item->target, parameter_type));
110 if (item->can_activate)
112 if (item->target != NULL && state != NULL)
114 /* actions with states and targets are radios */
117 selected = g_variant_equal (state, item->target);
118 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
119 gtk_model_menu_item_set_active (item, selected);
120 item->has_indicator = TRUE;
123 else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
125 /* boolean state actions without target are checks */
126 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), FALSE);
127 gtk_model_menu_item_set_active (item, g_variant_get_boolean (state));
128 item->has_indicator = TRUE;
133 /* stateless items are just plain actions */
134 gtk_model_menu_item_set_active (item, FALSE);
135 item->has_indicator = FALSE;
138 gtk_widget_set_sensitive (GTK_WIDGET (item), enabled);
139 gtk_widget_queue_resize (GTK_WIDGET (item));
144 gtk_model_menu_item_action_enabled_changed (GActionObserver *observer,
145 GActionObservable *observable,
146 const gchar *action_name,
149 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
151 if (!item->can_activate)
154 gtk_widget_set_sensitive (GTK_WIDGET (item), item->can_activate && enabled);
158 gtk_model_menu_item_action_state_changed (GActionObserver *observer,
159 GActionObservable *observable,
160 const gchar *action_name,
163 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
165 if (!item->can_activate)
169 gtk_model_menu_item_set_active (item, g_variant_equal (state, item->target));
171 else if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
172 gtk_model_menu_item_set_active (item, g_variant_get_boolean (state));
176 gtk_model_menu_item_action_removed (GActionObserver *observer,
177 GActionObservable *observable,
178 const gchar *action_name)
180 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
182 if (!item->can_activate)
185 gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
186 gtk_model_menu_item_set_active (item, FALSE);
187 item->has_indicator = FALSE;
189 gtk_widget_queue_resize (GTK_WIDGET (item));
193 get_accel_path (const gchar *action_name,
198 s = g_string_new ("<Actions>/");
199 g_string_append (s, action_name);
202 g_string_append_c (s, '/');
203 g_variant_print_string (parameter, s, FALSE);
205 return g_string_free (s, FALSE);
209 gtk_model_menu_item_setup (GtkModelMenuItem *item,
212 GActionObservable *actions,
213 GtkAccelGroup *accels)
215 GMenuAttributeIter *iter;
220 if ((submenu = g_menu_model_get_item_link (model, item_index, "submenu")))
222 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_model_menu_create_menu (submenu, actions, accels));
223 g_object_unref (submenu);
226 iter = g_menu_model_iterate_item_attributes (model, item_index);
227 while (g_menu_attribute_iter_get_next (iter, &key, &value))
229 if (g_str_equal (key, "label") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
230 gtk_menu_item_set_label (GTK_MENU_ITEM (item), g_variant_get_string (value, NULL));
232 else if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
233 item->action_name = g_variant_get_string (value, NULL);
235 else if (g_str_equal (key, "target"))
236 item->target = g_variant_ref (value);
238 g_variant_unref (value);
240 g_object_unref (iter);
242 gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
244 if (item->action_name)
246 const GVariantType *type;
251 /* observer already causes us to hold a hard ref on the group */
252 item->actions = G_ACTION_GROUP (actions);
254 g_action_observable_register_observer (actions, item->action_name, G_ACTION_OBSERVER (item));
256 if (g_action_group_query_action (G_ACTION_GROUP (actions), item->action_name, &enabled, &type, NULL, NULL, &state))
257 gtk_model_menu_item_action_added (G_ACTION_OBSERVER (item), actions, item->action_name, type, enabled, state);
260 gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
263 g_variant_unref (state);
265 path = get_accel_path (item->action_name, item->target);
266 gtk_menu_item_set_accel_path (GTK_MENU_ITEM (item), path);
272 gtk_model_menu_item_finalize (GObject *object)
274 G_OBJECT_CLASS (gtk_model_menu_item_parent_class)
279 gtk_model_menu_item_init (GtkModelMenuItem *item)
284 gtk_model_menu_item_observer_iface_init (GActionObserverInterface *iface)
286 iface->action_added = gtk_model_menu_item_action_added;
287 iface->action_enabled_changed = gtk_model_menu_item_action_enabled_changed;
288 iface->action_state_changed = gtk_model_menu_item_action_state_changed;
289 iface->action_removed = gtk_model_menu_item_action_removed;
293 gtk_model_menu_item_class_init (GtkModelMenuItemClass *class)
295 GtkCheckMenuItemClass *check_class = GTK_CHECK_MENU_ITEM_CLASS (class);
296 GtkMenuItemClass *item_class = GTK_MENU_ITEM_CLASS (class);
297 GObjectClass *object_class = G_OBJECT_CLASS (class);
299 check_class->draw_indicator = gtk_model_menu_item_draw_indicator;
301 item_class->activate = gtk_model_menu_item_activate;
302 item_class->toggle_size_request = gtk_model_menu_item_toggle_size_request;
304 object_class->finalize = gtk_model_menu_item_finalize;
308 gtk_model_menu_item_new (GMenuModel *model,
310 GActionObservable *actions,
311 GtkAccelGroup *accels)
313 GtkModelMenuItem *item;
315 item = g_object_new (GTK_TYPE_MODEL_MENU_ITEM, NULL);
317 gtk_model_menu_item_setup (item, model, item_index, actions, accels);
319 return GTK_MENU_ITEM (item);