From 47695fe9eec4ec5eab29d3efc9ef13d076082cdd Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Sat, 2 Aug 2014 21:16:10 -0300 Subject: [PATCH] Show batery status using a GtkProgressBar to show percentage. Based on scalemenuitem of same code. Also fix a bit code styles. Mainly use tab instead of spaces. But only in the code involved in the changes. I did not want modify other code --- panel-plugins/battery/battery-button.c | 68 ++++--- panel-plugins/battery/progressmenuitem.c | 327 +++++++++++++++++++++++++++++++ panel-plugins/battery/progressmenuitem.h | 80 ++++++++ panel-plugins/battery/xfce/Makefile.am | 2 + 4 files changed, 448 insertions(+), 29 deletions(-) create mode 100644 panel-plugins/battery/progressmenuitem.c create mode 100644 panel-plugins/battery/progressmenuitem.h diff --git a/panel-plugins/battery/battery-button.c b/panel-plugins/battery/battery-button.c index 65f5e94..e55d464 100644 --- a/panel-plugins/battery/battery-button.c +++ b/panel-plugins/battery/battery-button.c @@ -39,6 +39,7 @@ #include "common/xfpm-brightness.h" #include "battery-button.h" +#include "progressmenuitem.h" #include "scalemenuitem.h" @@ -194,6 +195,7 @@ battery_button_update_device_icon_and_details (BatteryButton *button, UpDevice * gchar *details, *icon_name; GdkPixbuf *pix; guint type = 0; + gdouble percentage; TRACE("entering for %s", object_path); @@ -202,55 +204,60 @@ battery_button_update_device_icon_and_details (BatteryButton *button, UpDevice * item = find_device_in_list (button, object_path); if (item == NULL) - return; + return; battery_device = item->data; /* hack, this depends on XFPM_DEVICE_TYPE_* being in sync with UP_DEVICE_KIND_* */ g_object_get (device, - "kind", &type, - NULL); + "kind", &type, + "percentage", &percentage, + NULL); icon_name = get_device_icon_name (button->priv->upower, device); details = get_device_description(button->priv->upower, device); pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), - icon_name, - 32, - GTK_ICON_LOOKUP_USE_BUILTIN, - NULL); + icon_name, + 32, + GTK_ICON_LOOKUP_USE_BUILTIN, + NULL); if (battery_device->details) - g_free(battery_device->details); + g_free(battery_device->details); battery_device->details = details; if (battery_device->pix) - g_object_unref (battery_device->pix); + g_object_unref (battery_device->pix); battery_device->pix = pix; /* Get the display device, which may now be this one */ display_device = get_display_device (button); if ( battery_device == display_device) { - DBG("this is the display device, updating"); - /* it is! update the panel button */ - g_free(button->priv->panel_icon_name); - button->priv->panel_icon_name = icon_name; - battery_button_set_icon (button); - /* update tooltip */ - battery_button_set_tooltip (button); + DBG("this is the display device, updating"); + /* it is! update the panel button */ + g_free(button->priv->panel_icon_name); + button->priv->panel_icon_name = icon_name; + battery_button_set_icon (button); + /* update tooltip */ + battery_button_set_tooltip (button); } /* If the menu is being displayed, update it */ if (button->priv->menu && battery_device->menu_item) { - GtkWidget *img; + GtkWidget *img; - gtk_menu_item_set_label (GTK_MENU_ITEM (battery_device->menu_item), details); + /* Set description */ + progress_menu_item_set_description_label (PROGRESS_MENU_ITEM (battery_device->menu_item), details); /* update the image */ img = gtk_image_new_from_pixbuf(battery_device->pix); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(battery_device->menu_item), img); + + /* Set percentage */ + progress_menu_item_set_value (PROGRESS_MENU_ITEM (battery_device->menu_item), percentage); } } @@ -308,7 +315,7 @@ battery_button_add_device (UpDevice *device, BatteryButton *button) /* If the menu is being shown, add this new device to it */ if (button->priv->menu) { - battery_button_menu_add_device (button, battery_device, FALSE); + battery_button_menu_add_device (button, battery_device, FALSE); } } @@ -721,29 +728,32 @@ menu_item_activate_cb(GtkWidget *object, gpointer user_data) static void battery_button_menu_add_device (BatteryButton *button, BatteryDevice *battery_device, gboolean append) { - GtkWidget *mi, *label, *img; + GtkWidget *mi, *img; + gdouble percentage; guint type = 0; /* We need a menu to attach it to */ g_return_if_fail (button->priv->menu); g_object_get (battery_device->device, - "kind", &type, - NULL); + "kind", &type, + "percentage", &percentage, + NULL); /* Don't add the display device or line power to the menu */ if (type == UP_DEVICE_KIND_LINE_POWER || battery_device->device == button->priv->display_device) - return; + return; - mi = gtk_image_menu_item_new_with_label(battery_device->details); - /* Make the menu item be bold and multi-line */ - label = gtk_bin_get_child(GTK_BIN(mi)); - gtk_label_set_use_markup(GTK_LABEL(label), TRUE); + mi = progress_menu_item_new (); + progress_menu_item_set_description_label (PROGRESS_MENU_ITEM(mi), battery_device->details); /* add the image */ img = gtk_image_new_from_pixbuf(battery_device->pix); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mi), img); + /* Set porcentage */ + progress_menu_item_set_value (PROGRESS_MENU_ITEM (mi), percentage); + /* keep track of the menu item in the battery_device so we can update it */ battery_device->menu_item = mi; g_signal_connect(G_OBJECT(mi), "destroy", G_CALLBACK(menu_item_destroyed_cb), button); @@ -754,9 +764,9 @@ battery_button_menu_add_device (BatteryButton *button, BatteryDevice *battery_de /* Add it to the menu */ gtk_widget_show(mi); if (append) - gtk_menu_shell_append(GTK_MENU_SHELL(button->priv->menu), mi); + gtk_menu_shell_append(GTK_MENU_SHELL(button->priv->menu), mi); else - gtk_menu_shell_prepend(GTK_MENU_SHELL(button->priv->menu), mi); + gtk_menu_shell_prepend(GTK_MENU_SHELL(button->priv->menu), mi); } static void diff --git a/panel-plugins/battery/progressmenuitem.c b/panel-plugins/battery/progressmenuitem.c new file mode 100644 index 0000000..174c502 --- /dev/null +++ b/panel-plugins/battery/progressmenuitem.c @@ -0,0 +1,327 @@ +/* -*- c-basic-offset: 2 -*- vi:set ts=2 sts=2 sw=2: + * * Copyright (C) 2014 Eric Koegel + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +/* + * Based on the scale menu item implementation of the indicator applet: + * Authors: + * Cody Russell + * http://bazaar.launchpad.net/~indicator-applet-developers/ido/trunk.14.10/view/head:/src/idoscalemenuitem.h + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "progressmenuitem.h" + +#include +#include + +/* for DBG/TRACE */ +#include + + +static void update_packing (ProgressMenuItem *self); + + +struct _ProgressMenuItemPrivate { + GtkWidget *progress; + GtkWidget *description_label; + GtkWidget *percentage_label; + GtkWidget *vbox; + GtkWidget *hbox; +}; + + +G_DEFINE_TYPE (ProgressMenuItem, progress_menu_item, GTK_TYPE_IMAGE_MENU_ITEM) + + +#define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_PROGRESS_MENU_ITEM, ProgressMenuItemPrivate)) + + +static void +progress_menu_item_class_init (ProgressMenuItemClass *item_class) +{ + g_type_class_add_private (item_class, sizeof (ProgressMenuItemPrivate)); +} + +static void +remove_children (GtkContainer *container) +{ + GList * children = gtk_container_get_children (container); + GList * l; + for (l=children; l!=NULL; l=l->next) { + g_object_ref (l->data); + gtk_container_remove (container, l->data); + } + g_list_free (children); +} + +static void +update_packing (ProgressMenuItem *self) +{ + ProgressMenuItemPrivate *priv = GET_PRIVATE (self); + GtkBox *hbox = GTK_BOX (gtk_hbox_new (FALSE, 0)); + GtkBox *vbox = GTK_BOX (gtk_vbox_new (FALSE, 0)); + + TRACE("entering"); + + if(priv->hbox) + remove_children (GTK_CONTAINER (priv->hbox)); + if(priv->vbox) + { + remove_children (GTK_CONTAINER (priv->vbox)); + gtk_container_remove (GTK_CONTAINER (self), priv->vbox); + } + + priv->hbox = GTK_WIDGET(hbox); + priv->vbox = GTK_WIDGET(vbox); + + /* add the new layout */ + if (priv->description_label && priv->percentage_label) + { + /* [IC] Description + * [ON] <----progress----> [percentage]% + */ + gtk_box_pack_start (vbox, priv->description_label, FALSE, FALSE, 0); + gtk_box_pack_start (vbox, priv->hbox, TRUE, TRUE, 0); + gtk_box_pack_start (hbox, priv->progress, TRUE, TRUE, 0); + gtk_box_pack_start (hbox, priv->percentage_label, FALSE, FALSE, 0); + } + else if (priv->description_label) + { + /* [IC] Description + * [ON] <----progress----> + */ + gtk_box_pack_start (vbox, priv->description_label, FALSE, FALSE, 0); + gtk_box_pack_start (vbox, priv->hbox, TRUE, TRUE, 0); + gtk_box_pack_start (hbox, priv->progress, TRUE, TRUE, 0); + } + else if (priv->percentage_label) + { + /* [ICON] <----progress----> [percentage]% */ + gtk_box_pack_start (vbox, priv->hbox, TRUE, TRUE, 0); + gtk_box_pack_start (hbox, priv->progress, TRUE, TRUE, 0); + gtk_box_pack_start (hbox, priv->percentage_label, FALSE, FALSE, 0); + } + else + { + /* [ICON] <----progress----> */ + gtk_box_pack_start (vbox, priv->hbox, TRUE, TRUE, 0); + gtk_box_pack_start (hbox, priv->progress, TRUE, TRUE, 0); + } + + + gtk_widget_show_all (priv->vbox); + gtk_widget_show_all (priv->hbox); + + gtk_container_add (GTK_CONTAINER (self), priv->vbox); +} + +static void +progress_menu_item_init (ProgressMenuItem *self) +{ +} + + +GtkWidget* +progress_menu_item_new (void) +{ + ProgressMenuItem *progress_item; + ProgressMenuItemPrivate *priv; + + TRACE("entering"); + + progress_item = PROGRESS_MENU_ITEM (g_object_new (TYPE_PROGRESS_MENU_ITEM, NULL)); + + priv = GET_PRIVATE (progress_item); + + priv->progress = gtk_progress_bar_new (); + priv->vbox = NULL; + priv->hbox = NULL; + + g_object_ref (priv->progress); + + gtk_widget_set_size_request (priv->progress, 100, 8); + + update_packing (progress_item); + + return GTK_WIDGET(progress_item); +} + +/** + * progress_menu_item_get_scale: + * @menuitem: The #ProgressMenuItem + * + * Retrieves the scale widget. + * + * Return Value: (transfer none) + **/ +GtkWidget* +progress_menu_item_get_scale (ProgressMenuItem *menuitem) +{ + ProgressMenuItemPrivate *priv; + + g_return_val_if_fail (IS_PROGRESS_MENU_ITEM (menuitem), NULL); + + priv = GET_PRIVATE (menuitem); + + return priv->progress; +} + +/** + * progress_menu_item_get_description_label: + * @menuitem: The #ProgressMenuItem + * + * Retrieves a string of the text for the description label widget. + * + * Return Value: The label text. + **/ +const gchar* +progress_menu_item_get_description_label (ProgressMenuItem *menuitem) +{ + ProgressMenuItemPrivate *priv; + + g_return_val_if_fail (IS_PROGRESS_MENU_ITEM (menuitem), NULL); + + priv = GET_PRIVATE (menuitem); + + return gtk_label_get_text (GTK_LABEL (priv->description_label)); +} + +/** + * progress_menu_item_get_percentage_label: + * @menuitem: The #ProgressMenuItem + * + * Retrieves a string of the text for the percentage label widget. + * + * Return Value: The label text. + **/ +const gchar* +progress_menu_item_get_percentage_label (ProgressMenuItem *menuitem) +{ + ProgressMenuItemPrivate *priv; + + g_return_val_if_fail (IS_PROGRESS_MENU_ITEM (menuitem), NULL); + + priv = GET_PRIVATE (menuitem); + + return gtk_label_get_text (GTK_LABEL (priv->percentage_label)); +} + +/** + * progress_menu_item_set_description_label: + * @menuitem: The #ProgressMenuItem + * @label: The label text + * + * Sets the text for the description label widget. If label is NULL + * then the description label is removed from the #ProgressMenuItem. + **/ +void +progress_menu_item_set_description_label (ProgressMenuItem *menuitem, + const gchar *label) +{ + ProgressMenuItemPrivate *priv; + + g_return_if_fail (IS_PROGRESS_MENU_ITEM (menuitem)); + + priv = GET_PRIVATE (menuitem); + + if (label == NULL && priv->description_label) + { + /* remove label */ + g_object_unref (priv->description_label); + priv->description_label = NULL; + return; + } + + if (priv->description_label && label) + { + gtk_label_set_markup (GTK_LABEL (priv->description_label), label); + } + else if(label) + { + /* create label */ + priv->description_label = gtk_label_new (NULL); + gtk_label_set_markup (GTK_LABEL (priv->description_label), label); + + /* align left */ + gtk_misc_set_alignment (GTK_MISC(priv->description_label), 0, 0); + } + + update_packing (menuitem); +} + + +/** + * progress_menu_item_set_percentage_label: + * @menuitem: The #ProgressMenuItem + * @label: The label text + * + * Sets the text for the percentage label widget. If label is NULL + * then the percentage label is removed from the #ProgressMenuItem. + **/ +void +progress_menu_item_set_percentage_label (ProgressMenuItem *menuitem, + const gchar *label) +{ + ProgressMenuItemPrivate *priv; + + g_return_if_fail (IS_PROGRESS_MENU_ITEM (menuitem)); + + priv = GET_PRIVATE (menuitem); + + if (label == NULL && priv->percentage_label) + { + /* remove label */ + g_object_unref (priv->percentage_label); + priv->percentage_label = NULL; + return; + } + + if (priv->percentage_label && label) + { + gtk_label_set_text (GTK_LABEL (priv->percentage_label), label); + } + else if(label) + { + /* create label */ + priv->percentage_label = gtk_label_new (label); + /* align left */ + gtk_misc_set_alignment (GTK_MISC(priv->percentage_label), 0, 0); + } + + update_packing (menuitem); +} + + +/** + * progress_menu_item_set_value: + * + * Sets the value of the scale inside @item to @value, without emitting + * "value-changed". + */ +void +progress_menu_item_set_value (ProgressMenuItem *item, + gdouble value) +{ + ProgressMenuItemPrivate *priv = GET_PRIVATE (item); + + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress), value/100); +} diff --git a/panel-plugins/battery/progressmenuitem.h b/panel-plugins/battery/progressmenuitem.h new file mode 100644 index 0000000..3386a86 --- /dev/null +++ b/panel-plugins/battery/progressmenuitem.h @@ -0,0 +1,80 @@ +/* -*- c-basic-offset: 2 -*- vi:set ts=2 sts=2 sw=2: + * * Copyright (C) 2014 Eric Koegel + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +/* + * Based on the scale menu item implementation of the indicator applet: + * Authors: + * Cody Russell + * http://bazaar.launchpad.net/~indicator-applet-developers/ido/trunk.14.10/view/head:/src/idoscalemenuitem.h + */ + + +#ifndef _PROGRESS_MENU_ITEM_H_ +#define _PROGRESS_MENU_ITEM_H_ + +#include + +G_BEGIN_DECLS + +#define TYPE_PROGRESS_MENU_ITEM (progress_menu_item_get_type ()) +#define PROGRESS_MENU_ITEM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TYPE_PROGRESS_MENU_ITEM, ProgressMenuItem)) +#define PROGRESS_MENU_ITEM_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), TYPE_PROGRESS_MENU_ITEM, ProgressMenuItemClass)) +#define IS_PROGRESS_MENU_ITEM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TYPE_PROGRESS_MENU_ITEM)) +#define IS_PROGRESS_MENU_ITEM_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), TYPE_PROGRESS_MENU_ITEM)) +#define PROGRESS_MENU_ITEM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), TYPE_PROGRESS_MENU_ITEM, ProgressMenuItemClass)) + + +typedef struct _ProgressMenuItem ProgressMenuItem; +typedef struct _ProgressMenuItemClass ProgressMenuItemClass; +typedef struct _ProgressMenuItemPrivate ProgressMenuItemPrivate; + +struct _ProgressMenuItem +{ + GtkMenuItem parent_instance; + + ProgressMenuItemPrivate *priv; +}; + +struct _ProgressMenuItemClass +{ + GtkMenuItemClass parent_class; +}; + + +GType progress_menu_item_get_type (void) G_GNUC_CONST; + +GtkWidget *progress_menu_item_new (void); + +GtkWidget *progress_menu_item_get_scale (ProgressMenuItem *menuitem); + +const gchar *progress_menu_item_get_description_label (ProgressMenuItem *menuitem); +const gchar *progress_menu_item_get_percentage_label (ProgressMenuItem *menuitem); + +void progress_menu_item_set_description_label (ProgressMenuItem *menuitem, + const gchar *label); +void progress_menu_item_set_percentage_label (ProgressMenuItem *menuitem, + const gchar *label); + +void progress_menu_item_set_value (ProgressMenuItem *item, + gdouble value); + + +G_END_DECLS + +#endif /* _PROGRESS_MENU_ITEM_H_ */ diff --git a/panel-plugins/battery/xfce/Makefile.am b/panel-plugins/battery/xfce/Makefile.am index a2746ed..c10ed51 100644 --- a/panel-plugins/battery/xfce/Makefile.am +++ b/panel-plugins/battery/xfce/Makefile.am @@ -17,6 +17,8 @@ libxfce4battery_la_SOURCES = \ battery-plugin.c \ ../battery-button.c \ ../battery-button.h \ + ../progressmenuitem.c \ + ../progressmenuitem.h \ ../scalemenuitem.c \ ../scalemenuitem.h -- 1.9.3