aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Uebernickel <lars.uebernickel@canonical.com>2013-10-22 20:13:38 +0000
committerTarmac <Unknown>2013-10-22 20:13:38 +0000
commit596e38b65bf82aaea478b27fe2a9adf3021ef26c (patch)
tree0d703d8f02328410970d45e5bb8a2901f6dda85d
parent6e849d59f8ca112a42e4fda71d14ece905038e41 (diff)
parentbccb3fbe7afc9842c0296d662d083285fdbe9706 (diff)
downloadayatana-ido-596e38b65bf82aaea478b27fe2a9adf3021ef26c.tar.gz
ayatana-ido-596e38b65bf82aaea478b27fe2a9adf3021ef26c.tar.bz2
ayatana-ido-596e38b65bf82aaea478b27fe2a9adf3021ef26c.zip
Make IdoSwitchMenuItem accessible from gtk_menu_new_from_model.
Approved by Charles Kerr, PS Jenkins bot.
-rw-r--r--debian/libido3-0.1-0.symbols3
-rw-r--r--example/menus.c5
-rw-r--r--src/idomenuitemfactory.c4
-rw-r--r--src/idoswitchmenuitem.c142
-rw-r--r--src/idoswitchmenuitem.h9
5 files changed, 158 insertions, 5 deletions
diff --git a/debian/libido3-0.1-0.symbols b/debian/libido3-0.1-0.symbols
index 9ce8256..987528b 100644
--- a/debian/libido3-0.1-0.symbols
+++ b/debian/libido3-0.1-0.symbols
@@ -70,6 +70,9 @@ libido3-0.1.so.0 libido3-0.1-0 #MINVER#
ido_switch_menu_item_get_content_area@Base 12.10.0
ido_switch_menu_item_get_type@Base 12.10.0
ido_switch_menu_item_new@Base 12.10.0
+ ido_switch_menu_item_new_from_menu_model@Base 0replaceme
+ ido_switch_menu_item_set_icon@Base 0replaceme
+ ido_switch_menu_item_set_label@Base 0replaceme
ido_timeline_calculate_progress@Base 0.1.8
ido_timeline_direction_get_type@Base 0.1.8
ido_timeline_get_direction@Base 0.1.8
diff --git a/example/menus.c b/example/menus.c
index ab070ac..0ab6066 100644
--- a/example/menus.c
+++ b/example/menus.c
@@ -31,7 +31,6 @@ main (int argc, char *argv[])
GtkWidget *root;
GtkWidget *menubar;
GtkWidget *image;
- GtkWidget *label;
const struct {
const char * username;
@@ -103,9 +102,7 @@ main (int argc, char *argv[])
/* Switch */
menuitem = ido_switch_menu_item_new ();
- label = gtk_label_new ("This is a switch");
- gtk_widget_show(label);
- gtk_container_add (ido_switch_menu_item_get_content_area(IDO_SWITCH_MENU_ITEM(menuitem)), label);
+ ido_switch_menu_item_set_label (IDO_SWITCH_MENU_ITEM (menuitem), "This is a switch.");
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
/* Calendar */
diff --git a/src/idomenuitemfactory.c b/src/idomenuitemfactory.c
index 376134e..11beaa8 100644
--- a/src/idomenuitemfactory.c
+++ b/src/idomenuitemfactory.c
@@ -31,6 +31,7 @@
#include "idoplaybackmenuitem.h"
#include "idoapplicationmenuitem.h"
#include "idosourcemenuitem.h"
+#include "idoswitchmenuitem.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))
@@ -91,6 +92,9 @@ ido_menu_item_factory_create_menu_item (UbuntuMenuItemFactory *factory,
else if (g_str_equal (type, "com.canonical.indicator.messages.source"))
item = ido_source_menu_item_new_from_menu_model (menuitem, actions);
+ else if (g_str_equal (type, "com.canonical.indicator.switch"))
+ item = ido_switch_menu_item_new_from_menu_model (menuitem, actions);
+
return item;
}
diff --git a/src/idoswitchmenuitem.c b/src/idoswitchmenuitem.c
index 10ff1f3..d47392f 100644
--- a/src/idoswitchmenuitem.c
+++ b/src/idoswitchmenuitem.c
@@ -21,6 +21,7 @@
#include "config.h"
#include "idoswitchmenuitem.h"
+#include "idoactionhelper.h"
static gboolean ido_switch_menu_button_release_event (GtkWidget * widget,
GdkEventButton * event);
@@ -30,6 +31,8 @@ struct _IdoSwitchMenuItemPrivate
{
GtkWidget * box;
GtkWidget * content_area;
+ GtkWidget * label;
+ GtkWidget * image;
GtkWidget * switch_w;
};
@@ -63,7 +66,7 @@ ido_switch_menu_item_init (IdoSwitchMenuItem *item)
priv = item->priv = G_TYPE_INSTANCE_GET_PRIVATE (item, IDO_TYPE_SWITCH_MENU_ITEM, IdoSwitchMenuItemPrivate);
priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
- priv->content_area = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+ priv->content_area = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
priv->switch_w = gtk_switch_new ();
gtk_box_pack_start (GTK_BOX (priv->box), priv->content_area, TRUE, TRUE, 0);
@@ -120,12 +123,149 @@ ido_switch_menu_item_new (void)
*
* Get the #GtkContainer to add additional widgets into.
*
+ * This function is dperecated.
+ *
* Return Value: (transfer none): The #GtkContainer to add additional widgets into.
**/
GtkContainer *
ido_switch_menu_item_get_content_area (IdoSwitchMenuItem * item)
{
+ static gboolean warned = FALSE;
+
g_return_val_if_fail (IDO_IS_SWITCH_MENU_ITEM(item), NULL);
+ if (!warned)
+ {
+ g_warning ("%s is deprecated. Please don't use it, especially if you're using"
+ "ido_switch_menu_set_{label,icon}()", G_STRFUNC);
+ warned = TRUE;
+ }
+
return GTK_CONTAINER (item->priv->content_area);
}
+
+/**
+ * ido_switch_menu_item_set_label:
+ * @item: a #IdoSwitchMenuItem.
+ * @label: a string to set as the label of @item
+ *
+ * Set the label of @item to @label.
+ **/
+void
+ido_switch_menu_item_set_label (IdoSwitchMenuItem *item,
+ const gchar *label)
+{
+ IdoSwitchMenuItemPrivate *priv;
+
+ g_return_if_fail (IDO_IS_SWITCH_MENU_ITEM (item));
+ g_return_if_fail (label != NULL);
+
+ priv = item->priv;
+
+ if (priv->label == NULL)
+ {
+ priv->label = gtk_label_new (NULL);
+ gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
+ gtk_widget_show (priv->label);
+ gtk_box_pack_end (GTK_BOX (priv->content_area), priv->label, TRUE, TRUE, 0);
+ }
+
+ gtk_label_set_text (GTK_LABEL (priv->label), label);
+}
+
+/**
+ * ido_switch_menu_item_set_icon:
+ * @item: a #IdoSwitchMenuItem.
+ * @icon: (allow-none): a #GIcon
+ *
+ * Set the icon of @item to @icon.
+ **/
+void
+ido_switch_menu_item_set_icon (IdoSwitchMenuItem *item,
+ GIcon *icon)
+{
+ IdoSwitchMenuItemPrivate *priv;
+
+ g_return_if_fail (IDO_IS_SWITCH_MENU_ITEM (item));
+ g_return_if_fail (icon == NULL || G_IS_ICON (icon));
+
+ priv = item->priv;
+
+ if (icon)
+ {
+ if (priv->image == NULL)
+ {
+ priv->image = gtk_image_new ();
+ gtk_widget_show (priv->image);
+ gtk_box_pack_start (GTK_BOX (priv->content_area), priv->image, FALSE, FALSE, 0);
+ }
+
+ gtk_image_set_from_gicon (GTK_IMAGE (priv->image), icon, GTK_ICON_SIZE_MENU);
+ }
+ else if (priv->image)
+ {
+ gtk_image_clear (GTK_IMAGE (priv->image));
+ }
+}
+
+static void
+ido_source_menu_item_state_changed (IdoActionHelper *helper,
+ GVariant *state,
+ gpointer user_data)
+{
+ IdoSwitchMenuItem *item = user_data;
+
+ if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
+ gtk_switch_set_active (GTK_SWITCH (item->priv->switch_w),
+ g_variant_get_boolean (state));
+}
+
+GtkMenuItem *
+ido_switch_menu_item_new_from_menu_model (GMenuItem *menuitem,
+ GActionGroup *actions)
+{
+ GtkMenuItem *item;
+ gchar *label;
+ GVariant *serialized_icon;
+ gchar *action = NULL;
+
+ item = g_object_new (IDO_TYPE_SWITCH_MENU_ITEM, NULL);
+
+ if (g_menu_item_get_attribute (menuitem, "label", "s", &label))
+ {
+ ido_switch_menu_item_set_label (IDO_SWITCH_MENU_ITEM (item), label);
+ g_free (label);
+ }
+
+ serialized_icon = g_menu_item_get_attribute_value (menuitem, "icon", NULL);
+ if (serialized_icon)
+ {
+ GIcon *icon;
+
+ icon = g_icon_deserialize (serialized_icon);
+ if (icon)
+ {
+ ido_switch_menu_item_set_icon (IDO_SWITCH_MENU_ITEM (item), icon);
+ g_object_unref (icon);
+ }
+
+ g_variant_unref (serialized_icon);
+ }
+
+ if (g_menu_item_get_attribute (menuitem, "action", "s", &action))
+ {
+ IdoActionHelper *helper;
+
+ helper = ido_action_helper_new (GTK_WIDGET (item), actions, action, NULL);
+ g_signal_connect (helper, "action-state-changed",
+ G_CALLBACK (ido_source_menu_item_state_changed), item);
+ g_signal_connect_object (item, "activate",
+ G_CALLBACK (ido_action_helper_activate), helper,
+ G_CONNECT_SWAPPED);
+ g_signal_connect_swapped (item, "destroy", G_CALLBACK (g_object_unref), helper);
+
+ g_free (action);
+ }
+
+ return item;
+}
diff --git a/src/idoswitchmenuitem.h b/src/idoswitchmenuitem.h
index 7e7e2d2..222b473 100644
--- a/src/idoswitchmenuitem.h
+++ b/src/idoswitchmenuitem.h
@@ -52,6 +52,15 @@ GType ido_switch_menu_item_get_type (void) G_GNUC_CONST;
GtkWidget *ido_switch_menu_item_new (void);
GtkContainer *ido_switch_menu_item_get_content_area (IdoSwitchMenuItem * item);
+GtkMenuItem * ido_switch_menu_item_new_from_menu_model (GMenuItem *menuitem,
+ GActionGroup *actions);
+
+void ido_switch_menu_item_set_label (IdoSwitchMenuItem *item,
+ const gchar *label);
+
+void ido_switch_menu_item_set_icon (IdoSwitchMenuItem *item,
+ GIcon *icon);
+
G_END_DECLS
#endif /* __IDO_SWITCH_MENU_ITEM_H__ */