From 7d036b65aac90b646eb7845cfc8e229464f372f0 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Tue, 21 Aug 2012 11:40:47 +0200 Subject: Show icons in application and source menu items Everthing goes through GIcon now, using g_icon_{to,new_for}_string to set a string attribute on the menu item. The attribute is prefixed x-canonical- for now. --- src/app-section.c | 8 ++++---- src/dbus-data.h | 3 --- src/gmenuutils.c | 2 +- src/im-app-menu-item.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/im-source-menu-item.c | 28 +++++++++++++++++++++++++--- src/indicator-messages.c | 25 +++++++++++++++++++------ src/messages-service.c | 11 +++++++++-- 7 files changed, 104 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/app-section.c b/src/app-section.c index 1602ac6..70bf21e 100644 --- a/src/app-section.c +++ b/src/app-section.c @@ -312,7 +312,7 @@ app_section_set_app_info (AppSection *self, GSimpleAction *launch; GFile *keyfile; GMenuItem *item; - gchar *iconname; + gchar *iconstr; g_return_if_fail (priv->appinfo == NULL); @@ -330,9 +330,9 @@ app_section_set_app_info (AppSection *self, item = g_menu_item_new (g_app_info_get_name (G_APP_INFO (priv->appinfo)), "launch"); g_menu_item_set_attribute (item, "x-canonical-type", "s", "ImAppMenuItem"); - iconname = g_icon_to_string (g_app_info_get_icon (G_APP_INFO (priv->appinfo))); - g_menu_item_set_attribute (item, INDICATOR_MENU_ATTRIBUTE_ICON_NAME, "s", iconname); - g_free (iconname); + iconstr = g_icon_to_string (g_app_info_get_icon (G_APP_INFO (priv->appinfo))); + g_menu_item_set_attribute (item, "x-canonical-icon", "s", iconstr); + g_free (iconstr); g_menu_append_item (priv->menu, item); g_object_unref (item); diff --git a/src/dbus-data.h b/src/dbus-data.h index 3c72b81..64747a9 100644 --- a/src/dbus-data.h +++ b/src/dbus-data.h @@ -8,7 +8,4 @@ #define INDICATOR_MESSAGES_DBUS_SERVICE_OBJECT "/com/canonical/indicator/messages/service" #define INDICATOR_MESSAGES_DBUS_SERVICE_INTERFACE "com.canonical.indicator.messages.service" -#define INDICATOR_MENU_ATTRIBUTE_ICON_NAME "indicator-icon-name" -#define INDICATOR_MENU_ATTRIBUTE_ACCESSIBLE_DESCRIPTION "indicator-accessible-description" - #endif /* __DBUS_DATA_H__ */ diff --git a/src/gmenuutils.c b/src/gmenuutils.c index 056e75f..f3ceba7 100644 --- a/src/gmenuutils.c +++ b/src/gmenuutils.c @@ -71,7 +71,7 @@ g_menu_append_with_icon_name (GMenu *menu, GMenuItem *item; item = g_menu_item_new (label, detailed_action); - g_menu_item_set_attribute (item, INDICATOR_MENU_ATTRIBUTE_ICON_NAME, "s", icon_name); + g_menu_item_set_attribute (item, "x-canonical-icon", "s", icon_name); g_menu_append_item (menu, item); diff --git a/src/im-app-menu-item.c b/src/im-app-menu-item.c index f4430be..eddf562 100644 --- a/src/im-app-menu-item.c +++ b/src/im-app-menu-item.c @@ -24,6 +24,9 @@ struct _ImAppMenuItemPrivate GActionGroup *action_group; gchar *action; gboolean is_running; + + GtkWidget *icon; + GtkWidget *label; }; enum @@ -38,6 +41,26 @@ static GParamSpec *properties[NUM_PROPERTIES]; G_DEFINE_TYPE (ImAppMenuItem, im_app_menu_item, GTK_TYPE_MENU_ITEM); +static void +im_app_menu_item_constructed (GObject *object) +{ + ImAppMenuItemPrivate *priv = IM_APP_MENU_ITEM (object)->priv; + GtkWidget *grid; + + priv->icon = g_object_ref (gtk_image_new ()); + + priv->label = g_object_ref (gtk_label_new ("")); + + grid = gtk_grid_new (); + gtk_grid_attach (GTK_GRID (grid), priv->icon, 0, 0, 1, 1); + gtk_grid_attach (GTK_GRID (grid), priv->label, 1, 0, 1, 1); + + gtk_container_add (GTK_CONTAINER (object), grid); + gtk_widget_show_all (grid); + + G_OBJECT_CLASS (im_app_menu_item_parent_class)->constructed (object); +} + static void im_app_menu_item_set_action_name (ImAppMenuItem *self, const gchar *action_name) @@ -156,6 +179,9 @@ im_app_menu_item_dispose (GObject *object) if (self->priv->action_group) im_app_menu_item_set_action_group (self, NULL); + g_clear_object (&self->priv->icon); + g_clear_object (&self->priv->label); + G_OBJECT_CLASS (im_app_menu_item_parent_class)->dispose (object); } @@ -223,6 +249,7 @@ im_app_menu_item_class_init (ImAppMenuItemClass *klass) g_type_class_add_private (klass, sizeof (ImAppMenuItemPrivate)); + object_class->constructed = im_app_menu_item_constructed; object_class->set_property = im_app_menu_set_property; object_class->dispose = im_app_menu_item_dispose; object_class->finalize = im_app_menu_item_finalize; @@ -260,15 +287,33 @@ void im_app_menu_item_set_menu_item (ImAppMenuItem *self, GMenuItem *menuitem) { + gchar *iconstr = NULL; + GIcon *icon = NULL; gchar *label; gchar *action = NULL; + if (g_menu_item_get_attribute (menuitem, "x-canonical-icon", "s", &iconstr)) + { + GError *error; + + icon = g_icon_new_for_string (iconstr, &error); + if (icon == NULL) + { + g_warning ("unable to set icon: %s", error->message); + g_error_free (error); + } + g_free (iconstr); + } + gtk_image_set_from_gicon (GTK_IMAGE (self->priv->icon), icon, GTK_ICON_SIZE_MENU); + g_menu_item_get_attribute (menuitem, "label", "s", &label); - gtk_menu_item_set_label (GTK_MENU_ITEM (self), label ? label : ""); + gtk_label_set_label (GTK_LABEL (self->priv->label), label ? label : ""); g_menu_item_get_attribute (menuitem, "action", "s", &action); im_app_menu_item_set_action_name (self, action); + if (icon) + g_object_unref (icon); g_free (label); g_free (action); } diff --git a/src/im-source-menu-item.c b/src/im-source-menu-item.c index b0c55f4..269c75d 100644 --- a/src/im-source-menu-item.c +++ b/src/im-source-menu-item.c @@ -26,6 +26,7 @@ struct _ImSourceMenuItemPrivate GActionGroup *action_group; gchar *action; + GtkWidget *icon; GtkWidget *label; GtkWidget *detail; }; @@ -51,8 +52,10 @@ im_source_menu_item_constructed (GObject *object) gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &icon_width, NULL); + priv->icon = g_object_ref (gtk_image_new ()); + gtk_widget_set_margin_left (priv->icon, icon_width + 2); + priv->label = g_object_ref (gtk_label_new ("")); - gtk_widget_set_margin_left (priv->label, icon_width + 2); priv->detail = g_object_ref (gtk_label_new ("")); gtk_widget_set_halign (priv->detail, GTK_ALIGN_END); @@ -61,8 +64,9 @@ im_source_menu_item_constructed (GObject *object) gtk_style_context_add_class (gtk_widget_get_style_context (priv->detail), "accelerator"); grid = gtk_grid_new (); - gtk_grid_attach (GTK_GRID (grid), priv->label, 0, 0, 1, 1); - gtk_grid_attach (GTK_GRID (grid), priv->detail, 1, 0, 1, 1); + gtk_grid_attach (GTK_GRID (grid), priv->icon, 0, 0, 1, 1); + gtk_grid_attach (GTK_GRID (grid), priv->label, 1, 0, 1, 1); + gtk_grid_attach (GTK_GRID (grid), priv->detail, 2, 0, 1, 1); gtk_container_add (GTK_CONTAINER (object), grid); gtk_widget_show_all (grid); @@ -274,6 +278,7 @@ im_source_menu_item_dispose (GObject *object) if (self->priv->action_group) im_source_menu_item_set_action_group (self, NULL); + g_clear_object (&self->priv->icon); g_clear_object (&self->priv->label); g_clear_object (&self->priv->detail); @@ -343,15 +348,32 @@ void im_source_menu_item_set_menu_item (ImSourceMenuItem *self, GMenuItem *menuitem) { + gchar *iconstr = NULL; + GIcon *icon = NULL; gchar *label; gchar *action = NULL; + if (g_menu_item_get_attribute (menuitem, "x-canonical-icon", "s", &iconstr)) + { + GError *error; + icon = g_icon_new_for_string (iconstr, &error); + if (icon == NULL) + { + g_warning ("unable to set icon: %s", error->message); + g_error_free (error); + } + g_free (iconstr); + } + gtk_image_set_from_gicon (GTK_IMAGE (self->priv->icon), icon, GTK_ICON_SIZE_MENU); + g_menu_item_get_attribute (menuitem, "label", "s", &label); gtk_label_set_label (GTK_LABEL (self->priv->label), label ? label : ""); g_menu_item_get_attribute (menuitem, "action", "s", &action); im_source_menu_item_set_action_name (self, action); + if (icon) + g_object_unref (icon); g_free (label); g_free (action); } diff --git a/src/indicator-messages.c b/src/indicator-messages.c index d898ad6..2824b0c 100644 --- a/src/indicator-messages.c +++ b/src/indicator-messages.c @@ -232,21 +232,34 @@ indicator_messages_accessible_desc_updated (IndicatorMessages *self) static void update_root_item (IndicatorMessages * self) { - const gchar *icon_name; + gchar *iconstr; if (g_menu_model_get_n_items (self->menu) == 0) return; - g_menu_model_get_item_attribute (self->menu, 0, INDICATOR_MENU_ATTRIBUTE_ICON_NAME, - "&s", &icon_name); + if (g_menu_model_get_item_attribute (self->menu, 0, "x-canonical-icon", "s", &iconstr)) { + GIcon *icon; + GError *error; + + icon = g_icon_new_for_string (iconstr, &error); + if (icon) { + gtk_image_set_from_gicon (GTK_IMAGE (self->image), icon, GTK_ICON_SIZE_MENU); + g_object_unref (icon); + } + else { + g_warning ("unable to load icon: %s", error->message); + g_error_free (error); + } + + g_free (iconstr); + } g_free (self->accessible_desc); + self->accessible_desc = NULL; - g_menu_model_get_item_attribute (self->menu, 0, INDICATOR_MENU_ATTRIBUTE_ACCESSIBLE_DESCRIPTION, + g_menu_model_get_item_attribute (self->menu, 0, "x-canonical-accessible-description", "s", &self->accessible_desc); indicator_messages_accessible_desc_updated (self); - - gtk_image_set_from_icon_name (GTK_IMAGE (self->image), icon_name, GTK_ICON_SIZE_MENU); } static void diff --git a/src/messages-service.c b/src/messages-service.c index aade829..7654e1f 100644 --- a/src/messages-service.c +++ b/src/messages-service.c @@ -464,6 +464,8 @@ main (int argc, char ** argv) GMainLoop * mainloop = NULL; IndicatorService * service = NULL; GMenuItem *header; + GIcon *icon; + gchar *iconstr; /* Glib init */ g_type_init(); @@ -501,11 +503,14 @@ main (int argc, char ** argv) chat_section = create_status_section (); g_menu_append (menu, _("Clear"), "clear"); + icon = g_themed_icon_new ("indicator-messages"); + iconstr = g_icon_to_string (icon); + toplevel_menu = g_menu_new (); header = g_menu_item_new (NULL, "messages"); g_menu_item_set_submenu (header, G_MENU_MODEL (menu)); - g_menu_item_set_attribute (header, INDICATOR_MENU_ATTRIBUTE_ICON_NAME, "s", "indicator-messages"); - g_menu_item_set_attribute (header, INDICATOR_MENU_ATTRIBUTE_ACCESSIBLE_DESCRIPTION, "s", _("Messages")); + g_menu_item_set_attribute (header, "x-canonical-icon", "s", iconstr); + g_menu_item_set_attribute (header, "x-canonical-accessible-description", "s", _("Messages")); g_menu_append_item (toplevel_menu, header); g_object_unref (header); @@ -518,6 +523,8 @@ main (int argc, char ** argv) g_main_loop_run(mainloop); /* Clean up */ + g_free (iconstr); + g_object_unref (icon); g_object_unref (messages_service); g_object_unref (chat_section); g_object_unref (settings); -- cgit v1.2.3