awesome/widgets/progressbar.c

191 lines
5.3 KiB
C
Raw Normal View History

2007-12-23 14:27:56 +01:00
/*
* progressbar.c - progress bar widget
*
2008-01-02 16:59:43 +01:00
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
2007-12-23 14:27:56 +01:00
*
* 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.
*
*/
2008-01-01 15:33:30 +01:00
#include <string.h>
2007-12-23 14:27:56 +01:00
#include "util.h"
#include "draw.h"
#include "widget.h"
#include "xutil.h"
2008-01-02 17:41:03 +01:00
#include "screen.h"
2007-12-23 14:27:56 +01:00
extern AwesomeConf globalconf;
typedef struct
{
2008-01-01 16:23:28 +01:00
/** Percent 0 to 100 */
int *percent;
/** Width of the bars */
int width;
/** Left padding */
int padding_left;
2008-01-01 16:23:28 +01:00
/** Pixel between bars */
int gap;
/** Number of bars */
int bars;
/** Height 0-1, where 1 is height of statusbar */
float height;
/** Foreground color */
2008-01-01 15:33:30 +01:00
XColor *fg;
2008-01-01 16:23:28 +01:00
/** Background color */
2008-01-01 15:33:30 +01:00
XColor *bg;
2008-01-01 16:23:28 +01:00
/** Border color */
XColor *bordercolor;
2007-12-23 14:27:56 +01:00
} Data;
static int
progressbar_draw(Widget *widget, DrawCtx *ctx, int offset,
2007-12-23 14:51:55 +01:00
int used __attribute__ ((unused)))
2007-12-23 14:27:56 +01:00
{
2008-01-01 15:33:30 +01:00
int i, width, pwidth, margin_top, pb_height, left_offset;
2008-01-12 23:38:31 +01:00
Area rectangle;
2008-01-01 15:33:30 +01:00
2007-12-23 14:27:56 +01:00
Data *d = widget->data;
2008-01-01 15:33:30 +01:00
if (!(d->bars))
return 0;
width = d->width - d->padding_left;
2007-12-23 14:27:56 +01:00
if(!widget->user_supplied_x)
widget->area.x = widget_calculate_offset(widget->statusbar->width,
d->width,
offset,
widget->alignment);
2007-12-23 14:27:56 +01:00
if(!widget->user_supplied_y)
widget->area.y = 0;
margin_top = (int) (widget->statusbar->height * (1 - d->height)) / 2 + 0.5 + widget->area.y;
2008-01-07 06:08:20 +01:00
pb_height = (int) ((widget->statusbar->height * d->height - (d->gap * (d->bars - 1))) / d->bars + 0.5);
left_offset = widget->area.x + d->padding_left;
2007-12-23 14:27:56 +01:00
for(i = 0; i < d->bars; i++)
2008-01-01 15:33:30 +01:00
{
pwidth = (int) d->percent[i] ? ((width - 2) * d->percent[i]) / 100 : 0;
2007-12-23 14:51:55 +01:00
2008-01-12 23:38:31 +01:00
rectangle.x = left_offset;
rectangle.y = margin_top;
rectangle.width = width;
rectangle.height = pb_height;
draw_rectangle(ctx, rectangle, False, d->bordercolor[i]);
2007-12-23 14:27:56 +01:00
2008-01-01 15:33:30 +01:00
if(pwidth > 0)
2008-01-12 23:38:31 +01:00
{
rectangle.x = left_offset + 1;
rectangle.y = margin_top + 1;
rectangle.width = pwidth;
rectangle.height = pb_height - 2;
draw_rectangle(ctx, rectangle, True, d->fg[i]);
}
2008-01-01 15:33:30 +01:00
if(width - 2 - pwidth > 0) /* not filled area */
2008-01-12 23:38:31 +01:00
{
rectangle.x = left_offset + 1 + pwidth;
rectangle.y = margin_top + 1;
rectangle.width = width - 2 - pwidth;
rectangle.height = pb_height - 2;
draw_rectangle(ctx, rectangle, True, d->bg[i]);
}
2008-01-01 15:33:30 +01:00
margin_top += (pb_height + d->gap);
}
2007-12-23 14:27:56 +01:00
2008-01-04 21:46:25 +01:00
widget->area.width = d->width;
2008-01-05 12:44:14 +01:00
widget->area.height = widget->statusbar->height;
2008-01-04 21:46:25 +01:00
return widget->area.width;
2007-12-23 14:27:56 +01:00
}
static void
progressbar_tell(Widget *widget, char *command)
{
Data *d = widget->data;
2008-01-01 16:23:28 +01:00
int i = 0, percent;
2008-01-01 15:33:30 +01:00
char * tok;
2008-01-01 16:23:28 +01:00
if(!command || !d->bars)
2007-12-23 14:27:56 +01:00
return;
for (tok = strtok(command, ","); tok && i < d->bars; tok = strtok(NULL, ","), i++)
2008-01-01 15:33:30 +01:00
{
percent = atoi(tok);
d->percent[i] = (percent < 0 ? 0 : (percent > 100 ? 100 : percent));
2008-01-01 15:33:30 +01:00
}
2007-12-23 14:27:56 +01:00
}
Widget *
progressbar_new(Statusbar *statusbar, cfg_t *config)
{
Widget *w;
Data *d;
char *color;
2008-01-02 17:41:03 +01:00
int i, phys_screen = get_phys_screen(statusbar->screen);
2008-01-01 15:33:30 +01:00
cfg_t *cfg;
2007-12-23 14:27:56 +01:00
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");
2008-01-01 15:33:30 +01:00
2008-01-01 16:23:28 +01:00
if(!(d->bars = cfg_size(config, "bar")))
2008-01-01 15:33:30 +01:00
{
warn("progressbar widget needs at least one bar section\n");
2008-01-01 15:33:30 +01:00
return w;
}
2008-01-01 16:23:28 +01:00
d->bg = p_new(XColor, d->bars);
d->fg = p_new(XColor, d->bars);
d->bordercolor = p_new(XColor, d->bars);
2008-01-01 16:23:28 +01:00
d->percent = p_new(int, d->bars);
for(i = 0; i < d->bars; i++)
2008-01-01 15:33:30 +01:00
{
cfg = cfg_getnsec(config, "bar", i);
if((color = cfg_getstr(cfg, "fg")))
2008-01-02 17:41:03 +01:00
d->fg[i] = initxcolor(phys_screen, color);
2008-01-01 15:33:30 +01:00
else
d->fg[i] = globalconf.screens[statusbar->screen].colors_normal[ColFG];
if((color = cfg_getstr(cfg, "bg")))
2008-01-02 17:41:03 +01:00
d->bg[i] = initxcolor(phys_screen, color);
2008-01-01 15:33:30 +01:00
else
d->bg[i] = globalconf.screens[statusbar->screen].colors_normal[ColBG];
if((color = cfg_getstr(cfg, "bordercolor")))
d->bordercolor[i] = initxcolor(phys_screen, color);
2008-01-01 15:33:30 +01:00
else
d->bordercolor[i] = d->fg[i];
2008-01-01 15:33:30 +01:00
}
d->height = cfg_getfloat(config, "height");
d->gap = cfg_getint(config, "gap");
d->padding_left = cfg_getint(config, "padding_left");
2007-12-23 14:27:56 +01:00
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80