diff options
author | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-05-31 18:51:10 -0400 |
---|---|---|
committer | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-05-31 18:51:10 -0400 |
commit | 26d057cbae600da0a1e5601187e899707c1b1738 (patch) | |
tree | b413d8404c0bbcdfd7502e7d870e27ff5f511728 | |
parent | 03dc54fbc259422813dc790c1d7f39757cb9b7ec (diff) | |
download | ayatana-ido-26d057cbae600da0a1e5601187e899707c1b1738.tar.gz ayatana-ido-26d057cbae600da0a1e5601187e899707c1b1738.tar.bz2 ayatana-ido-26d057cbae600da0a1e5601187e899707c1b1738.zip |
Add support for creating scale menu items from a menu model
-rw-r--r-- | src/idomenuitemfactory.c | 4 | ||||
-rw-r--r-- | src/idoscalemenuitem.c | 127 | ||||
-rw-r--r-- | src/idoscalemenuitem.h | 3 |
3 files changed, 134 insertions, 0 deletions
diff --git a/src/idomenuitemfactory.c b/src/idomenuitemfactory.c index 59f3630..0ff84a8 100644 --- a/src/idomenuitemfactory.c +++ b/src/idomenuitemfactory.c @@ -21,6 +21,7 @@ #include <gtk/ubuntu-private.h> #include "idousermenuitem.h" +#include "idoscalemenuitem.h" #define IDO_TYPE_MENU_ITEM_FACTORY (ido_menu_item_factory_get_type ()) #define IDO_MENU_ITEM_FACTORY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), IDO_TYPE_MENU_ITEM_FACTORY, IdoMenuItemFactory)) @@ -48,6 +49,9 @@ ido_menu_item_factory_create_menu_item (UbuntuMenuItemFactory *factory, if (g_str_equal (type, "indicator.user-menu-item")) item = ido_user_menu_item_new_from_model (menuitem, actions); + else if (g_str_equal (type, "com.canonical.unity.slider")) + item = ido_scale_menu_item_new_from_model (menuitem, actions); + return item; } diff --git a/src/idoscalemenuitem.c b/src/idoscalemenuitem.c index 9f1c57d..8fca9f6 100644 --- a/src/idoscalemenuitem.c +++ b/src/idoscalemenuitem.c @@ -30,6 +30,7 @@ #include "idorange.h" #include "idoscalemenuitem.h" #include "idotypebuiltins.h" +#include "idoactionhelper.h" static void ido_scale_menu_item_set_property (GObject *object, guint prop_id, @@ -803,6 +804,37 @@ ido_scale_menu_item_get_secondary_image (IdoScaleMenuItem *menuitem) } /** + * ido_scale_menu_item_set_icons: + * @item: a #IdoScaleMenuItem + * @primary-icon: (allow-none): the primary icon, or %NULL + * @secondary-icon: (allow-non): the secondary icon, %NULL + * + * Sets the icons of @item to @primary_icon and @secondary_icon. + * Pass %NULL for either of them to unset the icon. + */ +static void +ido_scale_menu_item_set_icons (IdoScaleMenuItem *item, + GIcon *primary_icon, + GIcon *secondary_icon) +{ + GtkWidget *primary; + GtkWidget *secondary; + + primary = ido_scale_menu_item_get_primary_image (item); + secondary = ido_scale_menu_item_get_secondary_image (item); + + if (primary_icon) + gtk_image_set_from_gicon (GTK_IMAGE (primary), primary_icon, GTK_ICON_SIZE_MENU); + else + gtk_image_clear (GTK_IMAGE (primary)); + + if (secondary_icon) + gtk_image_set_from_gicon (GTK_IMAGE (secondary), secondary_icon, GTK_ICON_SIZE_MENU); + else + gtk_image_clear (GTK_IMAGE (secondary)); +} + +/** * ido_scale_menu_item_get_primary_label: * @menuitem: The #IdoScaleMenuItem * @@ -942,4 +974,99 @@ default_secondary_clicked_handler (IdoScaleMenuItem * item) gtk_adjustment_set_value (adj, gtk_adjustment_get_upper (adj)); } +/** + * ido_scale_menu_item_state_changed: + * + * Updates a IdoScaleMenuItem from @state. State must be a double which + * contains the current value of the slider. + */ +static void +ido_scale_menu_item_state_changed (IdoActionHelper *helper, + GVariant *state, + gpointer user_data) +{ + GtkWidget *scale; + + scale = ido_scale_menu_item_get_scale (IDO_SCALE_MENU_ITEM (ido_action_helper_get_widget (helper))); + gtk_range_set_value (GTK_RANGE (scale), g_variant_get_double (state)); +} + +static void +ido_scale_menu_item_value_changed (GtkScale *scale, + gpointer user_data) +{ + IdoActionHelper *helper = user_data; + gdouble value; + + value = gtk_range_get_value (GTK_RANGE (scale)); + ido_action_helper_change_action_state (helper, g_variant_new_double (value)); +} + +static GIcon * +menu_item_get_icon (GMenuItem *menuitem, + const gchar *attribute) +{ + GVariant *value; + + value = g_menu_item_get_attribute_value (menuitem, attribute, NULL); + + return value ? g_icon_deserialize (value) : NULL; +} + +/** + * ido_scale_menu_item_new_from_model: + * + * Creates a new #IdoScaleMenuItem. If @menuitem contains an action, it + * will be bound to that action in @actions. + * + * Returns: (transfer full): a new #IdoScaleMenuItem + */ +GtkMenuItem * +ido_scale_menu_item_new_from_model (GMenuItem *menuitem, + GActionGroup *actions) +{ + GtkWidget *item; + gchar *action; + gdouble min = 0.0; + gdouble max = 100.0; + gdouble step = 1.0; + GIcon *primary_icon; + GIcon *secondary_icon; + + g_menu_item_get_attribute (menuitem, "min-value", "d", &min); + g_menu_item_get_attribute (menuitem, "max-value", "d", &max); + g_menu_item_get_attribute (menuitem, "step", "d", &step); + + item = ido_scale_menu_item_new_with_range ("Volume", IDO_RANGE_STYLE_DEFAULT, 0.0, min, max, step); + ido_scale_menu_item_set_style (IDO_SCALE_MENU_ITEM (item), IDO_SCALE_MENU_ITEM_STYLE_IMAGE); + + if (g_menu_item_get_attribute (menuitem, "action", "s", &action)) + { + IdoActionHelper *helper; + GtkWidget *scale; + + helper = ido_action_helper_new (item, actions, action, NULL); + g_signal_connect (helper, "action-state-changed", + G_CALLBACK (ido_scale_menu_item_state_changed), NULL); + + scale = ido_scale_menu_item_get_scale (IDO_SCALE_MENU_ITEM (item)); + g_signal_connect (scale, "value-changed", G_CALLBACK (ido_scale_menu_item_value_changed), helper); + + g_signal_connect_swapped (item, "destroy", G_CALLBACK (g_object_unref), helper); + + g_free (action); + } + + primary_icon = menu_item_get_icon (menuitem, "primary-icon"); + secondary_icon = menu_item_get_icon (menuitem, "secondary-icon"); + ido_scale_menu_item_set_icons (IDO_SCALE_MENU_ITEM (item), primary_icon, secondary_icon); + + if (primary_icon) + g_object_unref (primary_icon); + if (secondary_icon) + g_object_unref (secondary_icon); + + return GTK_MENU_ITEM (item); +} + #define __IDO_SCALE_MENU_ITEM_C__ diff --git a/src/idoscalemenuitem.h b/src/idoscalemenuitem.h index 2d63f3c..2c32a49 100644 --- a/src/idoscalemenuitem.h +++ b/src/idoscalemenuitem.h @@ -92,6 +92,9 @@ void ido_scale_menu_item_set_secondary_label (IdoScaleMenuItem void ido_scale_menu_item_primary_clicked (IdoScaleMenuItem *menuitem); void ido_scale_menu_item_secondary_clicked (IdoScaleMenuItem *menuitem); +GtkMenuItem * ido_scale_menu_item_new_from_model (GMenuItem *menuitem, + GActionGroup *actions); + G_END_DECLS #endif /* __IDO_SCALE_MENU_ITEM_H__ */ |