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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gtkmodelmenuitem.h"
24 #include "gtkaccelmapprivate.h"
25 #include "gtkactionhelper.h"
26 #include "gtkwidgetprivate.h"
27 #include "gtkaccellabel.h"
29 struct _GtkModelMenuItem
31 GtkCheckMenuItem parent_instance;
32 GtkActionHelperRole role;
33 gboolean has_indicator;
36 typedef GtkCheckMenuItemClass GtkModelMenuItemClass;
38 G_DEFINE_TYPE (GtkModelMenuItem, gtk_model_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
40 #define PROP_ACTION_ROLE 1
43 gtk_model_menu_item_toggle_size_request (GtkMenuItem *menu_item,
46 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
48 if (item->has_indicator)
49 GTK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
50 ->toggle_size_request (menu_item, requisition);
57 gtk_model_menu_item_draw_indicator (GtkCheckMenuItem *check_item,
60 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (check_item);
62 if (item->has_indicator)
63 GTK_CHECK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
64 ->draw_indicator (check_item, cr);
68 gtk_actionable_set_namespaced_action_name (GtkActionable *actionable,
69 const gchar *namespace,
70 const gchar *action_name)
74 gchar *name = g_strdup_printf ("%s.%s", namespace, action_name);
75 gtk_actionable_set_action_name (actionable, name);
80 gtk_actionable_set_action_name (actionable, action_name);
85 gtk_model_menu_item_submenu_shown (GtkWidget *widget,
88 const gchar *action_name = user_data;
91 muxer = _gtk_widget_get_action_muxer (widget);
92 g_action_group_change_action_state (G_ACTION_GROUP (muxer), action_name, g_variant_new_boolean (TRUE));
96 gtk_model_menu_item_submenu_hidden (GtkWidget *widget,
99 const gchar *action_name = user_data;
102 muxer = _gtk_widget_get_action_muxer (widget);
103 g_action_group_change_action_state (G_ACTION_GROUP (muxer), action_name, g_variant_new_boolean (FALSE));
107 gtk_model_menu_item_setup (GtkModelMenuItem *item,
110 const gchar *action_namespace)
112 GMenuAttributeIter *iter;
117 if ((submenu = g_menu_model_get_item_link (model, item_index, "submenu")))
119 gchar *section_namespace = NULL;
122 g_menu_model_get_item_attribute (model, item_index, "action-namespace", "s", §ion_namespace);
123 menu = gtk_menu_new ();
125 if (action_namespace)
127 gchar *namespace = g_strjoin (".", action_namespace, section_namespace, NULL);
128 gtk_menu_shell_bind_model (GTK_MENU_SHELL (menu), submenu, namespace, TRUE);
132 gtk_menu_shell_bind_model (GTK_MENU_SHELL (menu), submenu, section_namespace, TRUE);
134 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
136 g_free (section_namespace);
137 g_object_unref (submenu);
140 iter = g_menu_model_iterate_item_attributes (model, item_index);
141 while (g_menu_attribute_iter_get_next (iter, &key, &value))
143 if (g_str_equal (key, "label") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
144 gtk_menu_item_set_label (GTK_MENU_ITEM (item), g_variant_get_string (value, NULL));
146 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
148 GdkModifierType modifiers;
151 gtk_accelerator_parse (g_variant_get_string (value, NULL), &key, &modifiers);
155 GtkAccelLabel *accel_label;
157 /* Ensure that the GtkAccelLabel has been created... */
158 (void) gtk_menu_item_get_label (GTK_MENU_ITEM (item));
159 accel_label = GTK_ACCEL_LABEL (gtk_bin_get_child (GTK_BIN (item)));
160 g_assert (accel_label);
162 gtk_accel_label_set_accel (accel_label, key, modifiers);
166 else if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
167 gtk_actionable_set_namespaced_action_name (GTK_ACTIONABLE (item), action_namespace,
168 g_variant_get_string (value, NULL));
170 else if (g_str_equal (key, "target"))
171 gtk_actionable_set_action_target_value (GTK_ACTIONABLE (item), value);
173 else if (g_str_equal (key, "submenu-action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
177 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
181 const gchar *action = g_variant_get_string (value, NULL);
184 if (action_namespace)
185 full_action = g_strjoin (".", action_namespace, action, NULL);
187 full_action = g_strdup (action);
189 g_object_set_data_full (G_OBJECT (submenu), "gtkmodelmenu-visibility-action", full_action, g_free);
190 g_signal_connect (submenu, "show", G_CALLBACK (gtk_model_menu_item_submenu_shown), full_action);
191 g_signal_connect (submenu, "hide", G_CALLBACK (gtk_model_menu_item_submenu_hidden), full_action);
195 g_variant_unref (value);
197 g_object_unref (iter);
199 gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
203 gtk_model_menu_item_set_has_indicator (GtkModelMenuItem *item,
204 gboolean has_indicator)
206 if (has_indicator == item->has_indicator)
209 item->has_indicator = has_indicator;
211 gtk_widget_queue_resize (GTK_WIDGET (item));
215 gtk_model_menu_item_set_property (GObject *object, guint prop_id,
216 const GValue *value, GParamSpec *pspec)
218 GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (object);
219 GtkActionHelperRole role;
220 AtkObject *accessible;
223 g_assert (prop_id == PROP_ACTION_ROLE);
225 role = g_value_get_uint (value);
227 if (role == item->role)
230 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), role == GTK_ACTION_HELPER_ROLE_RADIO);
231 gtk_model_menu_item_set_has_indicator (item, role != GTK_ACTION_HELPER_ROLE_NORMAL);
233 accessible = gtk_widget_get_accessible (GTK_WIDGET (item));
236 case GTK_ACTION_HELPER_ROLE_NORMAL:
237 a11y_role = ATK_ROLE_MENU_ITEM;
240 case GTK_ACTION_HELPER_ROLE_TOGGLE:
241 a11y_role = ATK_ROLE_CHECK_MENU_ITEM;
244 case GTK_ACTION_HELPER_ROLE_RADIO:
245 a11y_role = ATK_ROLE_RADIO_MENU_ITEM;
249 g_assert_not_reached ();
252 atk_object_set_role (accessible, a11y_role);
256 gtk_model_menu_item_init (GtkModelMenuItem *item)
261 gtk_model_menu_item_class_init (GtkModelMenuItemClass *class)
263 GtkCheckMenuItemClass *check_class = GTK_CHECK_MENU_ITEM_CLASS (class);
264 GtkMenuItemClass *item_class = GTK_MENU_ITEM_CLASS (class);
265 GObjectClass *object_class = G_OBJECT_CLASS (class);
267 check_class->draw_indicator = gtk_model_menu_item_draw_indicator;
269 item_class->toggle_size_request = gtk_model_menu_item_toggle_size_request;
271 object_class->set_property = gtk_model_menu_item_set_property;
273 g_object_class_install_property (object_class, PROP_ACTION_ROLE,
274 g_param_spec_uint ("action-role", "action role", "action role",
275 0, 2, 0, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
279 gtk_model_menu_item_new (GMenuModel *model,
281 const gchar *action_namespace)
283 GtkModelMenuItem *item;
285 item = g_object_new (GTK_TYPE_MODEL_MENU_ITEM, NULL);
287 gtk_model_menu_item_setup (item, model, item_index, action_namespace);
289 return GTK_MENU_ITEM (item);