From bf3b14f5c5105c63816b9555dffb3bf3e273c602 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 26 Mar 2013 15:05:06 -0500 Subject: add user-widget.[ch] from indicator-session's trunk --- src/user-widget.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/user-widget.h | 55 ++++++++++ 2 files changed, 347 insertions(+) create mode 100644 src/user-widget.c create mode 100644 src/user-widget.h (limited to 'src') diff --git a/src/user-widget.c b/src/user-widget.c new file mode 100644 index 0000000..79b87b0 --- /dev/null +++ b/src/user-widget.c @@ -0,0 +1,292 @@ +/* +Copyright 2011 Canonical Ltd. + +Authors: + Conor Curran + Mirco Müller + Charles Kerr + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include + +#include + +#include "shared-names.h" +#include "user-widget.h" + + +typedef struct _UserWidgetPrivate UserWidgetPrivate; + +struct _UserWidgetPrivate +{ + DbusmenuMenuitem* twin_item; + GtkWidget* user_image; + GtkWidget* user_name; + GtkWidget* container; + GtkWidget* tick_icon; + gboolean logged_in; + gboolean sessions_active; +}; + +#define USER_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), USER_WIDGET_TYPE, UserWidgetPrivate)) + +/* Prototypes */ +static void user_widget_class_init (UserWidgetClass *klass); +static void user_widget_init (UserWidget *self); +static void user_widget_dispose (GObject *object); +static void user_widget_finalize (GObject *object); + +static void user_widget_set_twin_item (UserWidget* self, + DbusmenuMenuitem* twin_item); + +static gboolean user_widget_primitive_draw_cb_gtk_3 (GtkWidget *image, + cairo_t* cr, + gpointer user_data); + +G_DEFINE_TYPE (UserWidget, user_widget, GTK_TYPE_MENU_ITEM); + +static void +user_widget_class_init (UserWidgetClass *klass) +{ + GObjectClass * gobject_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (UserWidgetPrivate)); + + gobject_class->dispose = user_widget_dispose; + gobject_class->finalize = user_widget_finalize; +} + +static void +user_widget_init (UserWidget *self) +{ + self->priv = USER_WIDGET_GET_PRIVATE(self); + + UserWidgetPrivate * priv = self->priv; + + priv->user_image = NULL; + priv->user_name = NULL; + priv->logged_in = FALSE; + priv->sessions_active = FALSE; + priv->container = NULL; + priv->tick_icon = NULL; + + // Create the UI elements. + priv->user_image = gtk_image_new (); + gtk_misc_set_alignment(GTK_MISC(priv->user_image), 0.0, 0.0); + + priv->user_name = gtk_label_new (NULL); + + priv->container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4); + + priv->tick_icon = gtk_image_new_from_icon_name ("account-logged-in", + GTK_ICON_SIZE_MENU); + gtk_misc_set_alignment(GTK_MISC(priv->tick_icon), 1.0, 0.5); + + // Pack it together + gtk_box_pack_start (GTK_BOX (priv->container), + priv->user_image, + FALSE, + FALSE, + 0); + gtk_box_pack_start (GTK_BOX (priv->container), + priv->user_name, + FALSE, + FALSE, + 3); + gtk_box_pack_end (GTK_BOX(priv->container), + priv->tick_icon, + FALSE, + FALSE, 5); + + gtk_widget_show_all (priv->container); + gtk_container_add (GTK_CONTAINER (self), priv->container); + gtk_widget_show_all (priv->tick_icon); + gtk_widget_set_no_show_all (priv->tick_icon, TRUE); + gtk_widget_hide (priv->tick_icon); + + + // Fetch the drawing context. + g_signal_connect_after (GTK_WIDGET(self), "draw", + G_CALLBACK(user_widget_primitive_draw_cb_gtk_3), + GTK_WIDGET(self)); +} + +static void +user_widget_dispose (GObject *object) +{ + G_OBJECT_CLASS (user_widget_parent_class)->dispose (object); +} + +// TODO tidy up image and name +static void +user_widget_finalize (GObject *object) +{ + G_OBJECT_CLASS (user_widget_parent_class)->finalize (object); +} + + +/*****************************************************************/ + +// TODO handle drawing of green check mark +static gboolean +user_widget_primitive_draw_cb_gtk_3 (GtkWidget *widget, + cairo_t* cr, + gpointer user_data) +{ + g_return_val_if_fail(IS_USER_WIDGET(user_data), FALSE); + UserWidget* meta = USER_WIDGET(user_data); + UserWidgetPrivate * priv = USER_WIDGET_GET_PRIVATE(meta); + + // Draw dot only when user is the current user. + if (dbusmenu_menuitem_property_get_bool (priv->twin_item, USER_ITEM_PROP_IS_CURRENT_USER)) + { + gdouble x, y; + GtkStyle * style = gtk_widget_get_style (widget); + + GtkAllocation allocation; + gtk_widget_get_allocation (widget, &allocation); + x = allocation.x + 13; + y = allocation.height / 2; + + cairo_arc (cr, x, y, 3.0, 0.0, 2 * G_PI); + + cairo_set_source_rgb (cr, style->fg[gtk_widget_get_state(widget)].red/65535.0, + style->fg[gtk_widget_get_state(widget)].green/65535.0, + style->fg[gtk_widget_get_state(widget)].blue/65535.0); + cairo_fill (cr); + } + + return FALSE; +} + +/*** +**** +***/ + +static void +update_icon (UserWidget * self, DbusmenuMenuitem * mi) +{ + gboolean updated = FALSE; + GtkImage * image = GTK_IMAGE(self->priv->user_image); + + /* first try the menuitem's icon property */ + const gchar * icon_name = dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_ICON); + if (icon_name != NULL) + { + int width = 18; /* arbitrary default values */ + int height = 18; + GError * err = NULL; + GdkPixbuf * pixbuf = NULL; + + /* load the image */ + gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height); + pixbuf = gdk_pixbuf_new_from_file_at_size (icon_name, width, height, &err); + if (err == NULL) + { + gtk_image_set_from_pixbuf (image, pixbuf); + g_object_unref (pixbuf); + updated = TRUE; + } + else + { + g_warning ("Couldn't load the image \"%s\": %s", icon_name, err->message); + g_clear_error (&err); + } + } + + /* as a fallback, use the default user icon */ + if (!updated) + { + gtk_image_set_from_icon_name (image, + USER_ITEM_ICON_DEFAULT, + GTK_ICON_SIZE_MENU); + } +} + +static void +update_logged_in (UserWidget * self, DbusmenuMenuitem * mi) +{ + const gboolean b = dbusmenu_menuitem_property_get_bool (mi, USER_ITEM_PROP_LOGGED_IN); + + g_debug ("User \"%s\" %s active sessions", + dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME), + b ? "has" : "doesn't have"); + + gtk_widget_set_visible (self->priv->tick_icon, b); +} + +static void +update_name (UserWidget * self, DbusmenuMenuitem * mi) +{ + gtk_label_set_label (GTK_LABEL(self->priv->user_name), + dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME)); +} + +static void +user_widget_property_update (DbusmenuMenuitem * mi, + const gchar * property, + GVariant * value, + UserWidget * self) +{ + g_return_if_fail (IS_USER_WIDGET (self)); + + if (!g_strcmp0 (property, USER_ITEM_PROP_LOGGED_IN)) + { + update_logged_in (self, mi); + } + else if (!g_strcmp0 (property, USER_ITEM_PROP_ICON)) + { + update_icon (self, mi); + } + else if (!g_strcmp0 (property, USER_ITEM_PROP_NAME)) + { + update_name (self, mi); + } + else + { + g_debug ("%s FIXME: unhandled property change %s", G_STRFUNC, property); + } +} + +static void +user_widget_set_twin_item (UserWidget * self, DbusmenuMenuitem * mi) +{ + self->priv->twin_item = mi; + + update_icon (self, mi); + update_name (self, mi); + update_logged_in (self, mi); + + g_signal_connect (G_OBJECT(mi), "property-changed", + G_CALLBACK(user_widget_property_update), self); +} + + /** + * user_widget_new: + * @item: the #DbusmenuMenuitem this widget will render. + * + * Returns: (transfer full): a new #UserWidget. + **/ +GtkWidget* +user_widget_new (DbusmenuMenuitem *item) +{ + GtkWidget* widget = g_object_new(USER_WIDGET_TYPE, NULL); + user_widget_set_twin_item ( USER_WIDGET(widget), item ); + return widget; +} diff --git a/src/user-widget.h b/src/user-widget.h new file mode 100644 index 0000000..0953e6c --- /dev/null +++ b/src/user-widget.h @@ -0,0 +1,55 @@ +/* +Copyright 2011 Canonical Ltd. + +Authors: + Conor Curran + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ +#ifndef __USER_WIDGET_H__ +#define __USER_WIDGET_H__ + +#include +#include + +G_BEGIN_DECLS + +#define USER_WIDGET_TYPE (user_widget_get_type ()) +#define USER_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), USER_WIDGET_TYPE, UserWidget)) +#define USER_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), USER_WIDGET_TYPE, UserWidgetClass)) +#define IS_USER_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), USER_WIDGET_TYPE)) +#define IS_USER_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), USER_WIDGET_TYPE)) +#define USER_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), USER_WIDGET_TYPE, UserWidgetClass)) + +typedef struct _UserWidget UserWidget; +typedef struct _UserWidgetClass UserWidgetClass; +typedef struct _UserWidgetPrivate UserWidgetPrivate; + +struct _UserWidgetClass +{ + GtkMenuItemClass parent_class; +}; + +struct _UserWidget +{ + /*< private >*/ + GtkMenuItem parent; + UserWidgetPrivate * priv; +}; + +GType user_widget_get_type (void) G_GNUC_CONST; +GtkWidget* user_widget_new(DbusmenuMenuitem *twin_item); + +G_END_DECLS + +#endif -- cgit v1.2.3 From 2d8eac6ae37a5615c846953410e1576faf5bc677 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 26 Mar 2013 15:14:33 -0500 Subject: rename user-widget.[ch] to idousermenuitem.[ch]. Not building yet, next step is to decouple from dbusmenu. --- src/Makefile.am | 2 + src/idousermenuitem.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++++ src/idousermenuitem.h | 55 ++++++++++ src/user-widget.c | 292 -------------------------------------------------- src/user-widget.h | 55 ---------- 5 files changed, 346 insertions(+), 347 deletions(-) create mode 100644 src/idousermenuitem.c create mode 100644 src/idousermenuitem.h delete mode 100644 src/user-widget.c delete mode 100644 src/user-widget.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index e14efbf..6e1d813 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,6 +17,7 @@ sources_h = \ idorange.h \ idoscalemenuitem.h \ idoswitchmenuitem.h \ + idousermenuitem.h \ idotimeline.h \ libido.h @@ -66,6 +67,7 @@ libido_0_1_la_SOURCES = \ idorange.c \ idoscalemenuitem.c \ idoswitchmenuitem.c \ + idousermenuitem.c \ idotimeline.c libido3_0_1_la_SOURCES = $(libido_0_1_la_SOURCES) diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c new file mode 100644 index 0000000..620df8d --- /dev/null +++ b/src/idousermenuitem.c @@ -0,0 +1,289 @@ +/* +Copyright 2011 Canonical Ltd. + +Authors: + Conor Curran + Mirco Müller + Charles Kerr + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include + +#include "idousermenuitem.h" + + +typedef struct _IdoUserMenuItemPrivate IdoUserMenuItemPrivate; + +struct _IdoUserMenuItemPrivate +{ + DbusmenuMenuitem* twin_item; + GtkWidget* user_image; + GtkWidget* user_name; + GtkWidget* container; + GtkWidget* tick_icon; + gboolean logged_in; + gboolean sessions_active; +}; + +#define USER_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), USER_WIDGET_TYPE, IdoUserMenuItemPrivate)) + +/* Prototypes */ +static void user_widget_class_init (IdoUserMenuItemClass *klass); +static void user_widget_init (IdoUserMenuItem *self); +static void user_widget_dispose (GObject *object); +static void user_widget_finalize (GObject *object); + +static void user_widget_set_twin_item (IdoUserMenuItem* self, + DbusmenuMenuitem* twin_item); + +static gboolean user_widget_primitive_draw_cb_gtk_3 (GtkWidget *image, + cairo_t* cr, + gpointer user_data); + +G_DEFINE_TYPE (IdoUserMenuItem, user_widget, GTK_TYPE_MENU_ITEM); + +static void +user_widget_class_init (IdoUserMenuItemClass *klass) +{ + GObjectClass * gobject_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IdoUserMenuItemPrivate)); + + gobject_class->dispose = user_widget_dispose; + gobject_class->finalize = user_widget_finalize; +} + +static void +user_widget_init (IdoUserMenuItem *self) +{ + self->priv = USER_WIDGET_GET_PRIVATE(self); + + IdoUserMenuItemPrivate * priv = self->priv; + + priv->user_image = NULL; + priv->user_name = NULL; + priv->logged_in = FALSE; + priv->sessions_active = FALSE; + priv->container = NULL; + priv->tick_icon = NULL; + + // Create the UI elements. + priv->user_image = gtk_image_new (); + gtk_misc_set_alignment(GTK_MISC(priv->user_image), 0.0, 0.0); + + priv->user_name = gtk_label_new (NULL); + + priv->container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4); + + priv->tick_icon = gtk_image_new_from_icon_name ("account-logged-in", + GTK_ICON_SIZE_MENU); + gtk_misc_set_alignment(GTK_MISC(priv->tick_icon), 1.0, 0.5); + + // Pack it together + gtk_box_pack_start (GTK_BOX (priv->container), + priv->user_image, + FALSE, + FALSE, + 0); + gtk_box_pack_start (GTK_BOX (priv->container), + priv->user_name, + FALSE, + FALSE, + 3); + gtk_box_pack_end (GTK_BOX(priv->container), + priv->tick_icon, + FALSE, + FALSE, 5); + + gtk_widget_show_all (priv->container); + gtk_container_add (GTK_CONTAINER (self), priv->container); + gtk_widget_show_all (priv->tick_icon); + gtk_widget_set_no_show_all (priv->tick_icon, TRUE); + gtk_widget_hide (priv->tick_icon); + + + // Fetch the drawing context. + g_signal_connect_after (GTK_WIDGET(self), "draw", + G_CALLBACK(user_widget_primitive_draw_cb_gtk_3), + GTK_WIDGET(self)); +} + +static void +user_widget_dispose (GObject *object) +{ + G_OBJECT_CLASS (user_widget_parent_class)->dispose (object); +} + +// TODO tidy up image and name +static void +user_widget_finalize (GObject *object) +{ + G_OBJECT_CLASS (user_widget_parent_class)->finalize (object); +} + + +/*****************************************************************/ + +// TODO handle drawing of green check mark +static gboolean +user_widget_primitive_draw_cb_gtk_3 (GtkWidget *widget, + cairo_t* cr, + gpointer user_data) +{ + g_return_val_if_fail(IS_USER_WIDGET(user_data), FALSE); + IdoUserMenuItem* meta = USER_WIDGET(user_data); + IdoUserMenuItemPrivate * priv = USER_WIDGET_GET_PRIVATE(meta); + + // Draw dot only when user is the current user. + if (dbusmenu_menuitem_property_get_bool (priv->twin_item, USER_ITEM_PROP_IS_CURRENT_USER)) + { + gdouble x, y; + GtkStyle * style = gtk_widget_get_style (widget); + + GtkAllocation allocation; + gtk_widget_get_allocation (widget, &allocation); + x = allocation.x + 13; + y = allocation.height / 2; + + cairo_arc (cr, x, y, 3.0, 0.0, 2 * G_PI); + + cairo_set_source_rgb (cr, style->fg[gtk_widget_get_state(widget)].red/65535.0, + style->fg[gtk_widget_get_state(widget)].green/65535.0, + style->fg[gtk_widget_get_state(widget)].blue/65535.0); + cairo_fill (cr); + } + + return FALSE; +} + +/*** +**** +***/ + +static void +update_icon (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +{ + gboolean updated = FALSE; + GtkImage * image = GTK_IMAGE(self->priv->user_image); + + /* first try the menuitem's icon property */ + const gchar * icon_name = dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_ICON); + if (icon_name != NULL) + { + int width = 18; /* arbitrary default values */ + int height = 18; + GError * err = NULL; + GdkPixbuf * pixbuf = NULL; + + /* load the image */ + gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height); + pixbuf = gdk_pixbuf_new_from_file_at_size (icon_name, width, height, &err); + if (err == NULL) + { + gtk_image_set_from_pixbuf (image, pixbuf); + g_object_unref (pixbuf); + updated = TRUE; + } + else + { + g_warning ("Couldn't load the image \"%s\": %s", icon_name, err->message); + g_clear_error (&err); + } + } + + /* as a fallback, use the default user icon */ + if (!updated) + { + gtk_image_set_from_icon_name (image, + USER_ITEM_ICON_DEFAULT, + GTK_ICON_SIZE_MENU); + } +} + +static void +update_logged_in (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +{ + const gboolean b = dbusmenu_menuitem_property_get_bool (mi, USER_ITEM_PROP_LOGGED_IN); + + g_debug ("User \"%s\" %s active sessions", + dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME), + b ? "has" : "doesn't have"); + + gtk_widget_set_visible (self->priv->tick_icon, b); +} + +static void +update_name (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +{ + gtk_label_set_label (GTK_LABEL(self->priv->user_name), + dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME)); +} + +static void +user_widget_property_update (DbusmenuMenuitem * mi, + const gchar * property, + GVariant * value, + IdoUserMenuItem * self) +{ + g_return_if_fail (IS_USER_WIDGET (self)); + + if (!g_strcmp0 (property, USER_ITEM_PROP_LOGGED_IN)) + { + update_logged_in (self, mi); + } + else if (!g_strcmp0 (property, USER_ITEM_PROP_ICON)) + { + update_icon (self, mi); + } + else if (!g_strcmp0 (property, USER_ITEM_PROP_NAME)) + { + update_name (self, mi); + } + else + { + g_debug ("%s FIXME: unhandled property change %s", G_STRFUNC, property); + } +} + +static void +user_widget_set_twin_item (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +{ + self->priv->twin_item = mi; + + update_icon (self, mi); + update_name (self, mi); + update_logged_in (self, mi); + + g_signal_connect (G_OBJECT(mi), "property-changed", + G_CALLBACK(user_widget_property_update), self); +} + + /** + * user_widget_new: + * @item: the #DbusmenuMenuitem this widget will render. + * + * Returns: (transfer full): a new #IdoUserMenuItem. + **/ +GtkWidget* +user_widget_new (DbusmenuMenuitem *item) +{ + GtkWidget* widget = g_object_new(USER_WIDGET_TYPE, NULL); + user_widget_set_twin_item ( USER_WIDGET(widget), item ); + return widget; +} diff --git a/src/idousermenuitem.h b/src/idousermenuitem.h new file mode 100644 index 0000000..ac115a8 --- /dev/null +++ b/src/idousermenuitem.h @@ -0,0 +1,55 @@ +/* +Copyright 2011 Canonical Ltd. + +Authors: + Conor Curran + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ +#ifndef __IDO_USER_MENU_ITEM_H__ +#define __IDO_USER_MENU_ITEM_H__ + +#include +#include + +G_BEGIN_DECLS + +#define IDO_USER_MENU_ITEM_TYPE (ido_user_menu_item_get_type ()) +#define IDO_USER_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), IDO_USER_MENU_ITEM_TYPE, IdoUserMenuItem)) +#define IDO_USER_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), IDO_USER_MENU_ITEM_TYPE, IdoUserMenuItemClass)) +#define IS_IDO_USER_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), IDO_USER_MENU_ITEM_TYPE)) +#define IS_IDO_USER_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), IDO_USER_MENU_ITEM_TYPE)) +#define IDO_USER_MENU_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), IDO_USER_MENU_ITEM_TYPE, IdoUserMenuItemClass)) + +typedef struct _IdoUserMenuItem IdoUserMenuItem; +typedef struct _IdoUserMenuItemClass IdoUserMenuItemClass; +typedef struct _IdoUserMenuItemPrivate IdoUserMenuItemPrivate; + +struct _IdoUserMenuItemClass +{ + GtkMenuItemClass parent_class; +}; + +struct _IdoUserMenuItem +{ + /*< private >*/ + GtkMenuItem parent; + IdoUserMenuItemPrivate * priv; +}; + +GType ido_user_menu_item_get_type (void) G_GNUC_CONST; +GtkWidget* ido_user_menu_item_new(DbusmenuMenuitem *twin_item); + +G_END_DECLS + +#endif diff --git a/src/user-widget.c b/src/user-widget.c deleted file mode 100644 index 79b87b0..0000000 --- a/src/user-widget.c +++ /dev/null @@ -1,292 +0,0 @@ -/* -Copyright 2011 Canonical Ltd. - -Authors: - Conor Curran - Mirco Müller - Charles Kerr - -This program is free software: you can redistribute it and/or modify it -under the terms of the GNU General Public License version 3, as published -by the Free Software Foundation. - -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranties of -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR -PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program. If not, see . -*/ - -#ifdef HAVE_CONFIG_H - #include "config.h" -#endif - -#include - -#include - -#include "shared-names.h" -#include "user-widget.h" - - -typedef struct _UserWidgetPrivate UserWidgetPrivate; - -struct _UserWidgetPrivate -{ - DbusmenuMenuitem* twin_item; - GtkWidget* user_image; - GtkWidget* user_name; - GtkWidget* container; - GtkWidget* tick_icon; - gboolean logged_in; - gboolean sessions_active; -}; - -#define USER_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), USER_WIDGET_TYPE, UserWidgetPrivate)) - -/* Prototypes */ -static void user_widget_class_init (UserWidgetClass *klass); -static void user_widget_init (UserWidget *self); -static void user_widget_dispose (GObject *object); -static void user_widget_finalize (GObject *object); - -static void user_widget_set_twin_item (UserWidget* self, - DbusmenuMenuitem* twin_item); - -static gboolean user_widget_primitive_draw_cb_gtk_3 (GtkWidget *image, - cairo_t* cr, - gpointer user_data); - -G_DEFINE_TYPE (UserWidget, user_widget, GTK_TYPE_MENU_ITEM); - -static void -user_widget_class_init (UserWidgetClass *klass) -{ - GObjectClass * gobject_class = G_OBJECT_CLASS (klass); - - g_type_class_add_private (klass, sizeof (UserWidgetPrivate)); - - gobject_class->dispose = user_widget_dispose; - gobject_class->finalize = user_widget_finalize; -} - -static void -user_widget_init (UserWidget *self) -{ - self->priv = USER_WIDGET_GET_PRIVATE(self); - - UserWidgetPrivate * priv = self->priv; - - priv->user_image = NULL; - priv->user_name = NULL; - priv->logged_in = FALSE; - priv->sessions_active = FALSE; - priv->container = NULL; - priv->tick_icon = NULL; - - // Create the UI elements. - priv->user_image = gtk_image_new (); - gtk_misc_set_alignment(GTK_MISC(priv->user_image), 0.0, 0.0); - - priv->user_name = gtk_label_new (NULL); - - priv->container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4); - - priv->tick_icon = gtk_image_new_from_icon_name ("account-logged-in", - GTK_ICON_SIZE_MENU); - gtk_misc_set_alignment(GTK_MISC(priv->tick_icon), 1.0, 0.5); - - // Pack it together - gtk_box_pack_start (GTK_BOX (priv->container), - priv->user_image, - FALSE, - FALSE, - 0); - gtk_box_pack_start (GTK_BOX (priv->container), - priv->user_name, - FALSE, - FALSE, - 3); - gtk_box_pack_end (GTK_BOX(priv->container), - priv->tick_icon, - FALSE, - FALSE, 5); - - gtk_widget_show_all (priv->container); - gtk_container_add (GTK_CONTAINER (self), priv->container); - gtk_widget_show_all (priv->tick_icon); - gtk_widget_set_no_show_all (priv->tick_icon, TRUE); - gtk_widget_hide (priv->tick_icon); - - - // Fetch the drawing context. - g_signal_connect_after (GTK_WIDGET(self), "draw", - G_CALLBACK(user_widget_primitive_draw_cb_gtk_3), - GTK_WIDGET(self)); -} - -static void -user_widget_dispose (GObject *object) -{ - G_OBJECT_CLASS (user_widget_parent_class)->dispose (object); -} - -// TODO tidy up image and name -static void -user_widget_finalize (GObject *object) -{ - G_OBJECT_CLASS (user_widget_parent_class)->finalize (object); -} - - -/*****************************************************************/ - -// TODO handle drawing of green check mark -static gboolean -user_widget_primitive_draw_cb_gtk_3 (GtkWidget *widget, - cairo_t* cr, - gpointer user_data) -{ - g_return_val_if_fail(IS_USER_WIDGET(user_data), FALSE); - UserWidget* meta = USER_WIDGET(user_data); - UserWidgetPrivate * priv = USER_WIDGET_GET_PRIVATE(meta); - - // Draw dot only when user is the current user. - if (dbusmenu_menuitem_property_get_bool (priv->twin_item, USER_ITEM_PROP_IS_CURRENT_USER)) - { - gdouble x, y; - GtkStyle * style = gtk_widget_get_style (widget); - - GtkAllocation allocation; - gtk_widget_get_allocation (widget, &allocation); - x = allocation.x + 13; - y = allocation.height / 2; - - cairo_arc (cr, x, y, 3.0, 0.0, 2 * G_PI); - - cairo_set_source_rgb (cr, style->fg[gtk_widget_get_state(widget)].red/65535.0, - style->fg[gtk_widget_get_state(widget)].green/65535.0, - style->fg[gtk_widget_get_state(widget)].blue/65535.0); - cairo_fill (cr); - } - - return FALSE; -} - -/*** -**** -***/ - -static void -update_icon (UserWidget * self, DbusmenuMenuitem * mi) -{ - gboolean updated = FALSE; - GtkImage * image = GTK_IMAGE(self->priv->user_image); - - /* first try the menuitem's icon property */ - const gchar * icon_name = dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_ICON); - if (icon_name != NULL) - { - int width = 18; /* arbitrary default values */ - int height = 18; - GError * err = NULL; - GdkPixbuf * pixbuf = NULL; - - /* load the image */ - gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height); - pixbuf = gdk_pixbuf_new_from_file_at_size (icon_name, width, height, &err); - if (err == NULL) - { - gtk_image_set_from_pixbuf (image, pixbuf); - g_object_unref (pixbuf); - updated = TRUE; - } - else - { - g_warning ("Couldn't load the image \"%s\": %s", icon_name, err->message); - g_clear_error (&err); - } - } - - /* as a fallback, use the default user icon */ - if (!updated) - { - gtk_image_set_from_icon_name (image, - USER_ITEM_ICON_DEFAULT, - GTK_ICON_SIZE_MENU); - } -} - -static void -update_logged_in (UserWidget * self, DbusmenuMenuitem * mi) -{ - const gboolean b = dbusmenu_menuitem_property_get_bool (mi, USER_ITEM_PROP_LOGGED_IN); - - g_debug ("User \"%s\" %s active sessions", - dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME), - b ? "has" : "doesn't have"); - - gtk_widget_set_visible (self->priv->tick_icon, b); -} - -static void -update_name (UserWidget * self, DbusmenuMenuitem * mi) -{ - gtk_label_set_label (GTK_LABEL(self->priv->user_name), - dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME)); -} - -static void -user_widget_property_update (DbusmenuMenuitem * mi, - const gchar * property, - GVariant * value, - UserWidget * self) -{ - g_return_if_fail (IS_USER_WIDGET (self)); - - if (!g_strcmp0 (property, USER_ITEM_PROP_LOGGED_IN)) - { - update_logged_in (self, mi); - } - else if (!g_strcmp0 (property, USER_ITEM_PROP_ICON)) - { - update_icon (self, mi); - } - else if (!g_strcmp0 (property, USER_ITEM_PROP_NAME)) - { - update_name (self, mi); - } - else - { - g_debug ("%s FIXME: unhandled property change %s", G_STRFUNC, property); - } -} - -static void -user_widget_set_twin_item (UserWidget * self, DbusmenuMenuitem * mi) -{ - self->priv->twin_item = mi; - - update_icon (self, mi); - update_name (self, mi); - update_logged_in (self, mi); - - g_signal_connect (G_OBJECT(mi), "property-changed", - G_CALLBACK(user_widget_property_update), self); -} - - /** - * user_widget_new: - * @item: the #DbusmenuMenuitem this widget will render. - * - * Returns: (transfer full): a new #UserWidget. - **/ -GtkWidget* -user_widget_new (DbusmenuMenuitem *item) -{ - GtkWidget* widget = g_object_new(USER_WIDGET_TYPE, NULL); - user_widget_set_twin_item ( USER_WIDGET(widget), item ); - return widget; -} diff --git a/src/user-widget.h b/src/user-widget.h deleted file mode 100644 index 0953e6c..0000000 --- a/src/user-widget.h +++ /dev/null @@ -1,55 +0,0 @@ -/* -Copyright 2011 Canonical Ltd. - -Authors: - Conor Curran - -This program is free software: you can redistribute it and/or modify it -under the terms of the GNU General Public License version 3, as published -by the Free Software Foundation. - -This program is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranties of -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR -PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program. If not, see . -*/ -#ifndef __USER_WIDGET_H__ -#define __USER_WIDGET_H__ - -#include -#include - -G_BEGIN_DECLS - -#define USER_WIDGET_TYPE (user_widget_get_type ()) -#define USER_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), USER_WIDGET_TYPE, UserWidget)) -#define USER_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), USER_WIDGET_TYPE, UserWidgetClass)) -#define IS_USER_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), USER_WIDGET_TYPE)) -#define IS_USER_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), USER_WIDGET_TYPE)) -#define USER_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), USER_WIDGET_TYPE, UserWidgetClass)) - -typedef struct _UserWidget UserWidget; -typedef struct _UserWidgetClass UserWidgetClass; -typedef struct _UserWidgetPrivate UserWidgetPrivate; - -struct _UserWidgetClass -{ - GtkMenuItemClass parent_class; -}; - -struct _UserWidget -{ - /*< private >*/ - GtkMenuItem parent; - UserWidgetPrivate * priv; -}; - -GType user_widget_get_type (void) G_GNUC_CONST; -GtkWidget* user_widget_new(DbusmenuMenuitem *twin_item); - -G_END_DECLS - -#endif -- cgit v1.2.3 From 259c7ad0334a5c04873e4b62eb04890c76813fd0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 26 Mar 2013 16:24:47 -0500 Subject: add properties to IdoUserMenuItem and remove its dependencies on DbusmenuMenuitem --- src/idousermenuitem.c | 301 ++++++++++++++++++++++++++++++-------------------- src/idousermenuitem.h | 15 ++- 2 files changed, 194 insertions(+), 122 deletions(-) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index 620df8d..a6fd9f0 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -27,61 +27,176 @@ with this program. If not, see . #include "idousermenuitem.h" +#define FALLBACK_ICON_NAME "avatar-default" -typedef struct _IdoUserMenuItemPrivate IdoUserMenuItemPrivate; +enum +{ + PROP_0, + PROP_LABEL, + PROP_ICON, + PROP_IS_LOGGED_IN, + PROP_IS_CURRENT_USER, + PROP_LAST +}; + +static GParamSpec *properties[PROP_LAST]; struct _IdoUserMenuItemPrivate { - DbusmenuMenuitem* twin_item; GtkWidget* user_image; GtkWidget* user_name; GtkWidget* container; GtkWidget* tick_icon; - gboolean logged_in; - gboolean sessions_active; + gboolean is_logged_in; + gboolean is_current_user; + gchar * label; + gchar * icon_name; }; -#define USER_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), USER_WIDGET_TYPE, IdoUserMenuItemPrivate)) +G_DEFINE_TYPE (IdoUserMenuItem, ido_user_menu_item, GTK_TYPE_MENU_ITEM); /* Prototypes */ -static void user_widget_class_init (IdoUserMenuItemClass *klass); -static void user_widget_init (IdoUserMenuItem *self); -static void user_widget_dispose (GObject *object); -static void user_widget_finalize (GObject *object); +static gboolean ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * image, + cairo_t * cr, + gpointer gself); + + +static void +my_get_property (GObject * o, + guint property_id, + GValue * value, + GParamSpec * pspec) +{ + IdoUserMenuItem * self = IDO_USER_MENU_ITEM (o); + + switch (property_id) + { + case PROP_LABEL: + g_value_set_string (value, self->priv->label); + break; + + case PROP_ICON: + g_value_set_string (value, self->priv->icon_name); + break; -static void user_widget_set_twin_item (IdoUserMenuItem* self, - DbusmenuMenuitem* twin_item); + case PROP_IS_LOGGED_IN: + g_value_set_boolean (value, self->priv->is_logged_in); + break; -static gboolean user_widget_primitive_draw_cb_gtk_3 (GtkWidget *image, - cairo_t* cr, - gpointer user_data); + case PROP_IS_CURRENT_USER: + g_value_set_boolean (value, self->priv->is_current_user); + break; -G_DEFINE_TYPE (IdoUserMenuItem, user_widget, GTK_TYPE_MENU_ITEM); + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); + break; + } +} static void -user_widget_class_init (IdoUserMenuItemClass *klass) +my_set_property (GObject * o, + guint property_id, + const GValue * value, + GParamSpec * pspec) { + IdoUserMenuItem * self = IDO_USER_MENU_ITEM (o); + + switch (property_id) + { + case PROP_LABEL: + ido_user_menu_item_set_label (self, g_value_get_string (value)); + break; + + case PROP_ICON: + ido_user_menu_item_set_icon (self, g_value_get_string (value)); + break; + + case PROP_IS_LOGGED_IN: + ido_user_menu_item_set_logged_in (self, g_value_get_boolean (value)); + break; + + case PROP_IS_CURRENT_USER: + self->priv->is_current_user = g_value_get_boolean (value); + gtk_widget_queue_draw (GTK_WIDGET(self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); + break; + } +} + +static void +my_dispose (GObject *object) +{ + G_OBJECT_CLASS (ido_user_menu_item_parent_class)->dispose (object); +} + +static void +my_finalize (GObject *object) +{ + IdoUserMenuItem * self = IDO_USER_MENU_ITEM (object); + + g_free (self->priv->label); + g_free (self->priv->icon_name); + + G_OBJECT_CLASS (ido_user_menu_item_parent_class)->finalize (object); +} + +static void +ido_user_menu_item_class_init (IdoUserMenuItemClass *klass) +{ + GParamFlags prop_flags; GObjectClass * gobject_class = G_OBJECT_CLASS (klass); g_type_class_add_private (klass, sizeof (IdoUserMenuItemPrivate)); - gobject_class->dispose = user_widget_dispose; - gobject_class->finalize = user_widget_finalize; + gobject_class->get_property = my_get_property; + gobject_class->set_property = my_set_property; + gobject_class->dispose = my_dispose; + gobject_class->finalize = my_finalize; + + prop_flags = G_PARAM_CONSTRUCT + | G_PARAM_READWRITE + | G_PARAM_STATIC_STRINGS; + + properties[PROP_LABEL] = g_param_spec_string ("label", + "The user's name", + "The name to display", + "J. Random User", + prop_flags); + + properties[PROP_ICON] = g_param_spec_string ("icon", + "The icon's name", + "The icon to display", + NULL, + prop_flags); + + properties[PROP_IS_LOGGED_IN] = g_param_spec_boolean ("is-logged-in", + "is logged in", + "is user logged in?", + FALSE, + prop_flags); + + properties[PROP_IS_CURRENT_USER] = g_param_spec_boolean ("is-current-user", + "is current user", + "is user current?", + FALSE, + prop_flags); + + g_object_class_install_properties (gobject_class, PROP_LAST, properties); } static void -user_widget_init (IdoUserMenuItem *self) +ido_user_menu_item_init (IdoUserMenuItem *self) { - self->priv = USER_WIDGET_GET_PRIVATE(self); + IdoUserMenuItemPrivate * priv; - IdoUserMenuItemPrivate * priv = self->priv; + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + IDO_USER_MENU_ITEM_TYPE, + IdoUserMenuItemPrivate); - priv->user_image = NULL; - priv->user_name = NULL; - priv->logged_in = FALSE; - priv->sessions_active = FALSE; - priv->container = NULL; - priv->tick_icon = NULL; + priv = self->priv; // Create the UI elements. priv->user_image = gtk_image_new (); @@ -120,41 +235,30 @@ user_widget_init (IdoUserMenuItem *self) // Fetch the drawing context. g_signal_connect_after (GTK_WIDGET(self), "draw", - G_CALLBACK(user_widget_primitive_draw_cb_gtk_3), + G_CALLBACK(ido_user_menu_item_primitive_draw_cb_gtk_3), GTK_WIDGET(self)); } -static void -user_widget_dispose (GObject *object) -{ - G_OBJECT_CLASS (user_widget_parent_class)->dispose (object); -} - -// TODO tidy up image and name -static void -user_widget_finalize (GObject *object) -{ - G_OBJECT_CLASS (user_widget_parent_class)->finalize (object); -} - /*****************************************************************/ -// TODO handle drawing of green check mark static gboolean -user_widget_primitive_draw_cb_gtk_3 (GtkWidget *widget, - cairo_t* cr, - gpointer user_data) +ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * widget, + cairo_t * cr, + gpointer user_data) { - g_return_val_if_fail(IS_USER_WIDGET(user_data), FALSE); - IdoUserMenuItem* meta = USER_WIDGET(user_data); - IdoUserMenuItemPrivate * priv = USER_WIDGET_GET_PRIVATE(meta); + IdoUserMenuItemPrivate * priv; + + g_return_val_if_fail(IS_IDO_USER_MENU_ITEM(user_data), FALSE); + + priv = IDO_USER_MENU_ITEM(user_data)->priv; - // Draw dot only when user is the current user. - if (dbusmenu_menuitem_property_get_bool (priv->twin_item, USER_ITEM_PROP_IS_CURRENT_USER)) + /* Draw dot only when user is the current user. */ + if (priv->is_current_user) { gdouble x, y; GtkStyle * style = gtk_widget_get_style (widget); + const GtkStateType state = gtk_widget_get_state (widget); GtkAllocation allocation; gtk_widget_get_allocation (widget, &allocation); @@ -163,9 +267,9 @@ user_widget_primitive_draw_cb_gtk_3 (GtkWidget *widget, cairo_arc (cr, x, y, 3.0, 0.0, 2 * G_PI); - cairo_set_source_rgb (cr, style->fg[gtk_widget_get_state(widget)].red/65535.0, - style->fg[gtk_widget_get_state(widget)].green/65535.0, - style->fg[gtk_widget_get_state(widget)].blue/65535.0); + cairo_set_source_rgb (cr, style->fg[state].red/65535.0, + style->fg[state].green/65535.0, + style->fg[state].blue/65535.0); cairo_fill (cr); } @@ -173,18 +277,22 @@ user_widget_primitive_draw_cb_gtk_3 (GtkWidget *widget, } /*** -**** +**** PUBLIC API ***/ -static void -update_icon (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +void +ido_user_menu_item_set_icon (IdoUserMenuItem * self, const char * icon_name) { gboolean updated = FALSE; - GtkImage * image = GTK_IMAGE(self->priv->user_image); + IdoUserMenuItemPrivate * p = self->priv; + GtkImage * image = GTK_IMAGE (p->user_image); + + /* make a private copy of the icon name */ + g_free (p->icon_name); + self->priv->icon_name = g_strdup (icon_name); - /* first try the menuitem's icon property */ - const gchar * icon_name = dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_ICON); - if (icon_name != NULL) + /* now try to use it */ + if (icon_name && *icon_name) { int width = 18; /* arbitrary default values */ int height = 18; @@ -211,79 +319,32 @@ update_icon (IdoUserMenuItem * self, DbusmenuMenuitem * mi) if (!updated) { gtk_image_set_from_icon_name (image, - USER_ITEM_ICON_DEFAULT, + FALLBACK_ICON_NAME, GTK_ICON_SIZE_MENU); } } -static void -update_logged_in (IdoUserMenuItem * self, DbusmenuMenuitem * mi) -{ - const gboolean b = dbusmenu_menuitem_property_get_bool (mi, USER_ITEM_PROP_LOGGED_IN); - - g_debug ("User \"%s\" %s active sessions", - dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME), - b ? "has" : "doesn't have"); - - gtk_widget_set_visible (self->priv->tick_icon, b); -} - -static void -update_name (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +void +ido_user_menu_item_set_logged_in (IdoUserMenuItem * self, gboolean is_logged_in) { - gtk_label_set_label (GTK_LABEL(self->priv->user_name), - dbusmenu_menuitem_property_get (mi, USER_ITEM_PROP_NAME)); + gtk_widget_set_visible (self->priv->tick_icon, is_logged_in); } -static void -user_widget_property_update (DbusmenuMenuitem * mi, - const gchar * property, - GVariant * value, - IdoUserMenuItem * self) +void +ido_user_menu_item_set_current_user (IdoUserMenuItem * self, gboolean is_current_user) { - g_return_if_fail (IS_USER_WIDGET (self)); - - if (!g_strcmp0 (property, USER_ITEM_PROP_LOGGED_IN)) - { - update_logged_in (self, mi); - } - else if (!g_strcmp0 (property, USER_ITEM_PROP_ICON)) - { - update_icon (self, mi); - } - else if (!g_strcmp0 (property, USER_ITEM_PROP_NAME)) - { - update_name (self, mi); - } - else - { - g_debug ("%s FIXME: unhandled property change %s", G_STRFUNC, property); - } + self->priv->is_current_user = is_current_user; + gtk_widget_queue_draw (GTK_WIDGET (self)); } -static void -user_widget_set_twin_item (IdoUserMenuItem * self, DbusmenuMenuitem * mi) +void +ido_user_menu_item_set_label (IdoUserMenuItem * self, const char * label) { - self->priv->twin_item = mi; - - update_icon (self, mi); - update_name (self, mi); - update_logged_in (self, mi); - - g_signal_connect (G_OBJECT(mi), "property-changed", - G_CALLBACK(user_widget_property_update), self); + gtk_label_set_label (GTK_LABEL(self->priv->user_name), label); } - /** - * user_widget_new: - * @item: the #DbusmenuMenuitem this widget will render. - * - * Returns: (transfer full): a new #IdoUserMenuItem. - **/ GtkWidget* -user_widget_new (DbusmenuMenuitem *item) +ido_user_menu_item_new (void) { - GtkWidget* widget = g_object_new(USER_WIDGET_TYPE, NULL); - user_widget_set_twin_item ( USER_WIDGET(widget), item ); - return widget; + return GTK_WIDGET (g_object_new (IDO_USER_MENU_ITEM_TYPE, NULL)); } diff --git a/src/idousermenuitem.h b/src/idousermenuitem.h index ac115a8..7826718 100644 --- a/src/idousermenuitem.h +++ b/src/idousermenuitem.h @@ -20,7 +20,6 @@ with this program. If not, see . #define __IDO_USER_MENU_ITEM_H__ #include -#include G_BEGIN_DECLS @@ -35,6 +34,12 @@ typedef struct _IdoUserMenuItem IdoUserMenuItem; typedef struct _IdoUserMenuItemClass IdoUserMenuItemClass; typedef struct _IdoUserMenuItemPrivate IdoUserMenuItemPrivate; +/* property keys */ +#define IDO_USER_MENU_ITEM_PROP_LABEL "label" +#define IDO_USER_MENU_ITEM_PROP_ICON "icon" +#define IDO_USER_MENU_ITEM_PROP_IS_LOGGED_IN "is-logged-in" +#define IDO_USER_MENU_ITEM_PROP_IS_CURRENT_USER "is-current-user" + struct _IdoUserMenuItemClass { GtkMenuItemClass parent_class; @@ -48,7 +53,13 @@ struct _IdoUserMenuItem }; GType ido_user_menu_item_get_type (void) G_GNUC_CONST; -GtkWidget* ido_user_menu_item_new(DbusmenuMenuitem *twin_item); + +GtkWidget* ido_user_menu_item_new(void); + +void ido_user_menu_item_set_icon (IdoUserMenuItem * self, const char * icon_name); +void ido_user_menu_item_set_logged_in (IdoUserMenuItem * self, gboolean is_logged_in); +void ido_user_menu_item_set_current_user (IdoUserMenuItem * self, gboolean is_current_user); +void ido_user_menu_item_set_label (IdoUserMenuItem * self, const char * label); G_END_DECLS -- cgit v1.2.3 From 0430322cec5472c4cbac20e2869f3aab9218c3d7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 26 Mar 2013 16:30:46 -0500 Subject: in ido_user_menu_item_primitive_draw_cb_gtk_3(), remove deprecated use of gtk_widget_get_style(), gtk_widget_get_state() --- src/idousermenuitem.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index a6fd9f0..5677065 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -256,9 +256,15 @@ ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * widget, /* Draw dot only when user is the current user. */ if (priv->is_current_user) { + GtkStyleContext * style_context; + GtkStateFlags state_flags; + GdkRGBA color; gdouble x, y; - GtkStyle * style = gtk_widget_get_style (widget); - const GtkStateType state = gtk_widget_get_state (widget); + + /* get the foreground color */ + style_context = gtk_widget_get_style_context (widget); + state_flags = gtk_widget_get_state_flags (widget); + gtk_style_context_get_color (style_context, state_flags, &color); GtkAllocation allocation; gtk_widget_get_allocation (widget, &allocation); @@ -267,9 +273,8 @@ ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * widget, cairo_arc (cr, x, y, 3.0, 0.0, 2 * G_PI); - cairo_set_source_rgb (cr, style->fg[state].red/65535.0, - style->fg[state].green/65535.0, - style->fg[state].blue/65535.0); + gdk_cairo_set_source_rgba (cr, &color); + cairo_fill (cr); } -- cgit v1.2.3 From e9c82c3904642003b47f3d2bc13343894e6750d8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 27 Mar 2013 08:30:38 -0500 Subject: rename idousermenuitem's "icon" property as "icon-filename" for a little more clarity. --- src/idousermenuitem.c | 36 +++++++++++++++++++----------------- src/idousermenuitem.h | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index 5677065..cb405e8 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -33,7 +33,7 @@ enum { PROP_0, PROP_LABEL, - PROP_ICON, + PROP_ICON_FILENAME, PROP_IS_LOGGED_IN, PROP_IS_CURRENT_USER, PROP_LAST @@ -50,7 +50,7 @@ struct _IdoUserMenuItemPrivate gboolean is_logged_in; gboolean is_current_user; gchar * label; - gchar * icon_name; + gchar * icon_filename; }; G_DEFINE_TYPE (IdoUserMenuItem, ido_user_menu_item, GTK_TYPE_MENU_ITEM); @@ -75,8 +75,8 @@ my_get_property (GObject * o, g_value_set_string (value, self->priv->label); break; - case PROP_ICON: - g_value_set_string (value, self->priv->icon_name); + case PROP_ICON_FILENAME: + g_value_set_string (value, self->priv->icon_filename); break; case PROP_IS_LOGGED_IN: @@ -107,7 +107,7 @@ my_set_property (GObject * o, ido_user_menu_item_set_label (self, g_value_get_string (value)); break; - case PROP_ICON: + case PROP_ICON_FILENAME: ido_user_menu_item_set_icon (self, g_value_get_string (value)); break; @@ -138,7 +138,7 @@ my_finalize (GObject *object) IdoUserMenuItem * self = IDO_USER_MENU_ITEM (object); g_free (self->priv->label); - g_free (self->priv->icon_name); + g_free (self->priv->icon_filename); G_OBJECT_CLASS (ido_user_menu_item_parent_class)->finalize (object); } @@ -166,11 +166,11 @@ ido_user_menu_item_class_init (IdoUserMenuItemClass *klass) "J. Random User", prop_flags); - properties[PROP_ICON] = g_param_spec_string ("icon", - "The icon's name", - "The icon to display", - NULL, - prop_flags); + properties[PROP_ICON_FILENAME] = g_param_spec_string ("icon-filename", + "The icon's filename", + "The icon to display", + NULL, + prop_flags); properties[PROP_IS_LOGGED_IN] = g_param_spec_boolean ("is-logged-in", "is logged in", @@ -286,18 +286,18 @@ ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * widget, ***/ void -ido_user_menu_item_set_icon (IdoUserMenuItem * self, const char * icon_name) +ido_user_menu_item_set_icon (IdoUserMenuItem * self, const char * icon_filename) { gboolean updated = FALSE; IdoUserMenuItemPrivate * p = self->priv; GtkImage * image = GTK_IMAGE (p->user_image); /* make a private copy of the icon name */ - g_free (p->icon_name); - self->priv->icon_name = g_strdup (icon_name); + g_free (p->icon_filename); + self->priv->icon_filename = g_strdup (icon_filename); /* now try to use it */ - if (icon_name && *icon_name) + if (icon_filename && *icon_filename) { int width = 18; /* arbitrary default values */ int height = 18; @@ -306,7 +306,8 @@ ido_user_menu_item_set_icon (IdoUserMenuItem * self, const char * icon_name) /* load the image */ gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height); - pixbuf = gdk_pixbuf_new_from_file_at_size (icon_name, width, height, &err); + pixbuf = gdk_pixbuf_new_from_file_at_size (icon_filename, + width, height, &err); if (err == NULL) { gtk_image_set_from_pixbuf (image, pixbuf); @@ -315,7 +316,8 @@ ido_user_menu_item_set_icon (IdoUserMenuItem * self, const char * icon_name) } else { - g_warning ("Couldn't load the image \"%s\": %s", icon_name, err->message); + g_warning ("Couldn't load the image \"%s\": %s", + icon_filename, err->message); g_clear_error (&err); } } diff --git a/src/idousermenuitem.h b/src/idousermenuitem.h index 7826718..b9a3d00 100644 --- a/src/idousermenuitem.h +++ b/src/idousermenuitem.h @@ -36,7 +36,7 @@ typedef struct _IdoUserMenuItemPrivate IdoUserMenuItemPrivate; /* property keys */ #define IDO_USER_MENU_ITEM_PROP_LABEL "label" -#define IDO_USER_MENU_ITEM_PROP_ICON "icon" +#define IDO_USER_MENU_ITEM_PROP_ICON_FILENAME "icon-filename" #define IDO_USER_MENU_ITEM_PROP_IS_LOGGED_IN "is-logged-in" #define IDO_USER_MENU_ITEM_PROP_IS_CURRENT_USER "is-current-user" -- cgit v1.2.3