From ddbdbb2ed3736715af769c96230590231c29c41f Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 23 Dec 2007 14:27:56 +0100 Subject: [PATCH] new width: progressbar --- config.c | 8 +++ config.mk | 2 +- widget.c | 1 + widget.h | 1 + widgets/progressbar.c | 119 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 widgets/progressbar.c diff --git a/config.c b/config.c index 657f1ef0..86531ccc 100644 --- a/config.c +++ b/config.c @@ -346,6 +346,13 @@ config_parse(const char *confpatharg) CFG_STR((char *) "bg", (char *) NULL, CFGF_NONE), CFG_END() }; + static cfg_opt_t widget_progressbar_opts[] = + { + CFG_INT((char *) "width", 102, CFGF_NONE), + CFG_STR((char *) "fg", (char *) NULL, CFGF_NONE), + CFG_STR((char *) "bg", (char *) NULL, CFGF_NONE), + CFG_END() + }; static cfg_opt_t statusbar_opts[] = { CFG_STR((char *) "position", (char *) "top", CFGF_NONE), @@ -355,6 +362,7 @@ config_parse(const char *confpatharg) CFG_SEC((char *) "layoutinfo", widget_opts, CFGF_TITLE | CFGF_MULTI), CFG_SEC((char *) "iconbox", widget_iconbox_opts, CFGF_TITLE | CFGF_MULTI), CFG_SEC((char *) "netwmicon", widget_opts, CFGF_TITLE | CFGF_MULTI), + CFG_SEC((char *) "progressbar", widget_progressbar_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; static cfg_opt_t tag_opts[] = diff --git a/config.mk b/config.mk index 60cdec5f..3510f6c6 100644 --- a/config.mk +++ b/config.mk @@ -7,7 +7,7 @@ RELEASE = "Productivity Breaker" # additional layouts LAYOUTS = layouts/tile.c layouts/floating.c layouts/max.c layouts/fibonacci.c # additional widgets -WIDGETS = widgets/taglist.c widgets/layoutinfo.c widgets/textbox.c widgets/focustitle.c widgets/iconbox.c widgets/netwmicon.c +WIDGETS = widgets/taglist.c widgets/layoutinfo.c widgets/textbox.c widgets/focustitle.c widgets/iconbox.c widgets/netwmicon.c widgets/progressbar.c # paths PREFIX = /usr/local diff --git a/widget.c b/widget.c index 6a30cd20..2cd69de3 100644 --- a/widget.c +++ b/widget.c @@ -34,6 +34,7 @@ const NameFuncLink WidgetList[] = {"textbox", textbox_new}, {"iconbox", iconbox_new}, {"netwmicon", netwmicon_new}, + {"progressbar", progressbar_new}, {NULL, NULL} }; diff --git a/widget.h b/widget.h index a24de5ad..e2876550 100644 --- a/widget.h +++ b/widget.h @@ -42,6 +42,7 @@ WidgetConstructor textbox_new; WidgetConstructor focustitle_new; WidgetConstructor iconbox_new; WidgetConstructor netwmicon_new; +WidgetConstructor progressbar_new; UICB_PROTO(uicb_widget_tell); diff --git a/widgets/progressbar.c b/widgets/progressbar.c new file mode 100644 index 00000000..f410e964 --- /dev/null +++ b/widgets/progressbar.c @@ -0,0 +1,119 @@ +/* + * progressbar.c - progress bar widget + * + * Copyright © 2007 Julien Danjou + * + * 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. + * + */ + +#include +#include "util.h" +#include "draw.h" +#include "widget.h" +#include "xutil.h" + +extern AwesomeConf globalconf; + +typedef struct +{ + int percent; + int width; + XColor fg; + XColor bg; +} Data; + +static int +progressbar_draw(Widget *widget, DrawCtx *ctx, int offset, + int used __attribute__ ((unused))) +{ + VirtScreen vscreen = globalconf.screens[widget->statusbar->screen]; + int location, width, pwidth, y, height; + Data *d = widget->data; + + width = d->width - 6; + + height = vscreen.statusbar->height / 2; + y = (vscreen.statusbar->height - height) / 2 - 1; + + location = widget_calculate_offset(vscreen.statusbar->width, + d->width, + offset, + widget->alignment) + 1; + + pwidth = d->percent ? (width * d->percent) / 100 : 0; + + draw_rectangle(ctx, + location + 1, y, + width, height, + False, d->fg); + if(pwidth > 0) + draw_rectangle(ctx, + location + 1, y + 1, + pwidth, height - 2, + True, d->fg); + + if(width - pwidth - 2 > 0) + draw_rectangle(ctx, + location + pwidth + 2, y + 1, + width - pwidth - 2, height - 2, + True, d->bg); + + return d->width; +} + +static void +progressbar_tell(Widget *widget, char *command) +{ + Data *d = widget->data; + int percent; + + if(!command) + return; + + percent = atoi(command); + + if(percent <= 100 && percent >= 0) + d->percent = percent; +} + +Widget * +progressbar_new(Statusbar *statusbar, cfg_t *config) +{ + Widget *w; + Data *d; + char *color; + + w = p_new(Widget, 1); + widget_common_new(w, statusbar, config); + w->draw = progressbar_draw; + w->tell = progressbar_tell; + d = w->data = p_new(Data, 1); + d->width = cfg_getint(config, "width"); + d->percent = 0; + + if((color = cfg_getstr(config, "fg"))) + d->fg = initxcolor(statusbar->screen, color); + else + d->fg = globalconf.screens[statusbar->screen].colors_normal[ColFG]; + + if((color = cfg_getstr(config, "bg"))) + d->bg = initxcolor(statusbar->screen, color); + else + d->bg = globalconf.screens[statusbar->screen].colors_normal[ColBG]; + + return w; +} +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80