2011-12-02 01:39:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Canonical Limited
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the licence, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-12-02 01:39:11 +00:00
|
|
|
*
|
|
|
|
* Author: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkmodelmenuitem.h"
|
|
|
|
|
2011-12-05 22:37:04 +00:00
|
|
|
#include "gtkaccelmapprivate.h"
|
2012-08-18 03:13:42 +00:00
|
|
|
#include "gtkactionhelper.h"
|
2012-08-24 18:11:37 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2012-09-10 18:36:23 +00:00
|
|
|
#include "gtkaccellabel.h"
|
2011-12-03 23:45:32 +00:00
|
|
|
|
2011-12-02 01:39:11 +00:00
|
|
|
struct _GtkModelMenuItem
|
|
|
|
{
|
|
|
|
GtkCheckMenuItem parent_instance;
|
2012-08-18 03:13:42 +00:00
|
|
|
GtkActionHelperRole role;
|
2011-12-02 03:33:30 +00:00
|
|
|
gboolean has_indicator;
|
2011-12-02 01:39:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef GtkCheckMenuItemClass GtkModelMenuItemClass;
|
|
|
|
|
2012-08-18 03:13:42 +00:00
|
|
|
G_DEFINE_TYPE (GtkModelMenuItem, gtk_model_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
|
2011-12-02 01:39:11 +00:00
|
|
|
|
2012-08-18 03:13:42 +00:00
|
|
|
#define PROP_ACTION_ROLE 1
|
2011-12-02 01:39:11 +00:00
|
|
|
|
2011-12-02 03:33:30 +00:00
|
|
|
static void
|
|
|
|
gtk_model_menu_item_toggle_size_request (GtkMenuItem *menu_item,
|
|
|
|
gint *requisition)
|
|
|
|
{
|
|
|
|
GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
|
|
|
|
|
|
|
|
if (item->has_indicator)
|
|
|
|
GTK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
|
|
|
|
->toggle_size_request (menu_item, requisition);
|
|
|
|
|
|
|
|
else
|
|
|
|
*requisition = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_menu_item_draw_indicator (GtkCheckMenuItem *check_item,
|
|
|
|
cairo_t *cr)
|
|
|
|
{
|
|
|
|
GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (check_item);
|
|
|
|
|
|
|
|
if (item->has_indicator)
|
|
|
|
GTK_CHECK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
|
|
|
|
->draw_indicator (check_item, cr);
|
|
|
|
}
|
|
|
|
|
2012-06-20 14:56:56 +00:00
|
|
|
static void
|
|
|
|
gtk_actionable_set_namespaced_action_name (GtkActionable *actionable,
|
|
|
|
const gchar *namespace,
|
|
|
|
const gchar *action_name)
|
|
|
|
{
|
|
|
|
if (namespace)
|
|
|
|
{
|
|
|
|
gchar *name = g_strdup_printf ("%s.%s", namespace, action_name);
|
|
|
|
gtk_actionable_set_action_name (actionable, name);
|
|
|
|
g_free (name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_actionable_set_action_name (actionable, action_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 18:11:37 +00:00
|
|
|
static void
|
|
|
|
gtk_model_menu_item_submenu_shown (GtkWidget *widget,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
const gchar *action_name = user_data;
|
|
|
|
GActionMuxer *muxer;
|
|
|
|
|
|
|
|
muxer = _gtk_widget_get_action_muxer (widget);
|
|
|
|
g_action_group_change_action_state (G_ACTION_GROUP (muxer), action_name, g_variant_new_boolean (TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_menu_item_submenu_hidden (GtkWidget *widget,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
const gchar *action_name = user_data;
|
|
|
|
GActionMuxer *muxer;
|
|
|
|
|
|
|
|
muxer = _gtk_widget_get_action_muxer (widget);
|
|
|
|
g_action_group_change_action_state (G_ACTION_GROUP (muxer), action_name, g_variant_new_boolean (FALSE));
|
|
|
|
}
|
|
|
|
|
2011-12-02 01:39:11 +00:00
|
|
|
static void
|
|
|
|
gtk_model_menu_item_setup (GtkModelMenuItem *item,
|
|
|
|
GMenuModel *model,
|
|
|
|
gint item_index,
|
2012-09-10 18:36:23 +00:00
|
|
|
const gchar *action_namespace)
|
2011-12-02 01:39:11 +00:00
|
|
|
{
|
|
|
|
GMenuAttributeIter *iter;
|
2011-12-03 23:45:32 +00:00
|
|
|
GMenuModel *submenu;
|
2011-12-02 01:39:11 +00:00
|
|
|
const gchar *key;
|
|
|
|
GVariant *value;
|
|
|
|
|
2011-12-03 23:45:32 +00:00
|
|
|
if ((submenu = g_menu_model_get_item_link (model, item_index, "submenu")))
|
|
|
|
{
|
2012-06-20 14:56:56 +00:00
|
|
|
gchar *section_namespace = NULL;
|
|
|
|
GtkWidget *menu;
|
|
|
|
|
|
|
|
g_menu_model_get_item_attribute (model, item_index, "action-namespace", "s", §ion_namespace);
|
2012-09-11 15:38:06 +00:00
|
|
|
menu = gtk_menu_new ();
|
2012-06-20 14:56:56 +00:00
|
|
|
|
|
|
|
if (action_namespace)
|
|
|
|
{
|
|
|
|
gchar *namespace = g_strjoin (".", action_namespace, section_namespace, NULL);
|
2012-09-11 15:38:06 +00:00
|
|
|
gtk_menu_shell_bind_model (GTK_MENU_SHELL (menu), submenu, namespace, TRUE);
|
2012-06-20 14:56:56 +00:00
|
|
|
g_free (namespace);
|
|
|
|
}
|
|
|
|
else
|
2012-09-11 15:38:06 +00:00
|
|
|
gtk_menu_shell_bind_model (GTK_MENU_SHELL (menu), submenu, section_namespace, TRUE);
|
2012-06-20 14:56:56 +00:00
|
|
|
|
|
|
|
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
|
|
|
|
|
|
|
|
g_free (section_namespace);
|
2011-12-03 23:45:32 +00:00
|
|
|
g_object_unref (submenu);
|
|
|
|
}
|
|
|
|
|
2011-12-02 01:39:11 +00:00
|
|
|
iter = g_menu_model_iterate_item_attributes (model, item_index);
|
|
|
|
while (g_menu_attribute_iter_get_next (iter, &key, &value))
|
|
|
|
{
|
|
|
|
if (g_str_equal (key, "label") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
|
|
|
|
gtk_menu_item_set_label (GTK_MENU_ITEM (item), g_variant_get_string (value, NULL));
|
|
|
|
|
2012-09-10 18:36:23 +00:00
|
|
|
else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
|
|
|
|
{
|
|
|
|
GdkModifierType modifiers;
|
|
|
|
guint key;
|
|
|
|
|
|
|
|
gtk_accelerator_parse (g_variant_get_string (value, NULL), &key, &modifiers);
|
|
|
|
|
|
|
|
if (key)
|
|
|
|
{
|
|
|
|
GtkAccelLabel *accel_label;
|
|
|
|
|
|
|
|
/* Ensure that the GtkAccelLabel has been created... */
|
|
|
|
(void) gtk_menu_item_get_label (GTK_MENU_ITEM (item));
|
|
|
|
accel_label = GTK_ACCEL_LABEL (gtk_bin_get_child (GTK_BIN (item)));
|
|
|
|
g_assert (accel_label);
|
|
|
|
|
|
|
|
gtk_accel_label_set_accel (accel_label, key, modifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 01:39:11 +00:00
|
|
|
else if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
|
2012-06-20 14:56:56 +00:00
|
|
|
gtk_actionable_set_namespaced_action_name (GTK_ACTIONABLE (item), action_namespace,
|
|
|
|
g_variant_get_string (value, NULL));
|
2011-12-02 01:39:11 +00:00
|
|
|
|
|
|
|
else if (g_str_equal (key, "target"))
|
2012-08-18 03:13:42 +00:00
|
|
|
gtk_actionable_set_action_target_value (GTK_ACTIONABLE (item), value);
|
2011-12-02 01:39:11 +00:00
|
|
|
|
2012-08-24 18:11:37 +00:00
|
|
|
else if (g_str_equal (key, "submenu-action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
|
|
|
|
{
|
|
|
|
GtkWidget *submenu;
|
|
|
|
|
|
|
|
submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (item));
|
|
|
|
|
|
|
|
if (submenu != NULL)
|
|
|
|
{
|
|
|
|
const gchar *action = g_variant_get_string (value, NULL);
|
|
|
|
gchar *full_action;
|
|
|
|
|
|
|
|
if (action_namespace)
|
|
|
|
full_action = g_strjoin (".", action_namespace, action, NULL);
|
|
|
|
else
|
|
|
|
full_action = g_strdup (action);
|
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (submenu), "gtkmodelmenu-visibility-action", full_action, g_free);
|
|
|
|
g_signal_connect (submenu, "show", G_CALLBACK (gtk_model_menu_item_submenu_shown), full_action);
|
|
|
|
g_signal_connect (submenu, "hide", G_CALLBACK (gtk_model_menu_item_submenu_hidden), full_action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 01:39:11 +00:00
|
|
|
g_variant_unref (value);
|
|
|
|
}
|
|
|
|
g_object_unref (iter);
|
|
|
|
|
|
|
|
gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-18 03:13:42 +00:00
|
|
|
gtk_model_menu_item_set_has_indicator (GtkModelMenuItem *item,
|
|
|
|
gboolean has_indicator)
|
2011-12-02 01:39:11 +00:00
|
|
|
{
|
2012-08-18 03:13:42 +00:00
|
|
|
if (has_indicator == item->has_indicator)
|
|
|
|
return;
|
|
|
|
|
|
|
|
item->has_indicator = has_indicator;
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (item));
|
2011-12-02 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-18 03:13:42 +00:00
|
|
|
gtk_model_menu_item_set_property (GObject *object, guint prop_id,
|
|
|
|
const GValue *value, GParamSpec *pspec)
|
2011-12-02 01:39:11 +00:00
|
|
|
{
|
2012-08-18 03:13:42 +00:00
|
|
|
GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (object);
|
|
|
|
GtkActionHelperRole role;
|
|
|
|
AtkObject *accessible;
|
|
|
|
AtkRole a11y_role;
|
|
|
|
|
|
|
|
g_assert (prop_id == PROP_ACTION_ROLE);
|
|
|
|
|
|
|
|
role = g_value_get_uint (value);
|
|
|
|
|
|
|
|
if (role == item->role)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), role == GTK_ACTION_HELPER_ROLE_RADIO);
|
|
|
|
gtk_model_menu_item_set_has_indicator (item, role != GTK_ACTION_HELPER_ROLE_NORMAL);
|
|
|
|
|
|
|
|
accessible = gtk_widget_get_accessible (GTK_WIDGET (item));
|
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case GTK_ACTION_HELPER_ROLE_NORMAL:
|
|
|
|
a11y_role = ATK_ROLE_MENU_ITEM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_ACTION_HELPER_ROLE_TOGGLE:
|
|
|
|
a11y_role = ATK_ROLE_CHECK_MENU_ITEM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_ACTION_HELPER_ROLE_RADIO:
|
|
|
|
a11y_role = ATK_ROLE_RADIO_MENU_ITEM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
atk_object_set_role (accessible, a11y_role);
|
2011-12-02 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-18 03:13:42 +00:00
|
|
|
gtk_model_menu_item_init (GtkModelMenuItem *item)
|
2011-12-02 01:39:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_menu_item_class_init (GtkModelMenuItemClass *class)
|
|
|
|
{
|
2011-12-02 03:33:30 +00:00
|
|
|
GtkCheckMenuItemClass *check_class = GTK_CHECK_MENU_ITEM_CLASS (class);
|
2011-12-02 01:39:11 +00:00
|
|
|
GtkMenuItemClass *item_class = GTK_MENU_ITEM_CLASS (class);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
2011-12-02 03:33:30 +00:00
|
|
|
check_class->draw_indicator = gtk_model_menu_item_draw_indicator;
|
|
|
|
|
|
|
|
item_class->toggle_size_request = gtk_model_menu_item_toggle_size_request;
|
2011-12-02 01:39:11 +00:00
|
|
|
|
2012-08-18 03:13:42 +00:00
|
|
|
object_class->set_property = gtk_model_menu_item_set_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_ACTION_ROLE,
|
|
|
|
g_param_spec_uint ("action-role", "action role", "action role",
|
|
|
|
0, 2, 0, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
|
2011-12-02 01:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkMenuItem *
|
|
|
|
gtk_model_menu_item_new (GMenuModel *model,
|
|
|
|
gint item_index,
|
2012-09-10 18:36:23 +00:00
|
|
|
const gchar *action_namespace)
|
2011-12-02 01:39:11 +00:00
|
|
|
{
|
|
|
|
GtkModelMenuItem *item;
|
|
|
|
|
|
|
|
item = g_object_new (GTK_TYPE_MODEL_MENU_ITEM, NULL);
|
|
|
|
|
2012-09-10 18:36:23 +00:00
|
|
|
gtk_model_menu_item_setup (item, model, item_index, action_namespace);
|
2011-12-02 01:39:11 +00:00
|
|
|
|
|
|
|
return GTK_MENU_ITEM (item);
|
|
|
|
}
|