2007-12-23 14:27:56 +01:00
|
|
|
/*
|
2008-03-26 03:18:21 +01:00
|
|
|
* progressbar.c - progressbar widget
|
2007-12-23 14:27:56 +01:00
|
|
|
*
|
2008-01-02 16:59:43 +01:00
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
2008-03-26 03:18:21 +01:00
|
|
|
* Copyright © 2007-2008 Marco Candrian <mac@calmar.ws>
|
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 "widget.h"
|
2008-03-19 12:05:36 +01:00
|
|
|
|
2008-03-26 03:18:21 +01:00
|
|
|
#include "widgets/common.h"
|
2008-01-02 17:41:03 +01:00
|
|
|
#include "screen.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.h"
|
2008-03-19 12:05:36 +01:00
|
|
|
#include "common/configopts.h"
|
2007-12-23 14:27:56 +01:00
|
|
|
|
2008-03-26 03:18:21 +01:00
|
|
|
|
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;
|
2008-02-17 08:42:11 +01:00
|
|
|
/** data_title of the data */
|
|
|
|
char **data_title;
|
|
|
|
/** Width of the data_items */
|
2008-01-01 16:23:28 +01:00
|
|
|
int width;
|
2008-03-26 03:18:21 +01:00
|
|
|
/** Pixel between data items (bars) */
|
2008-01-01 16:23:28 +01:00
|
|
|
int gap;
|
2008-03-26 03:18:21 +01:00
|
|
|
/** border width in pixels */
|
|
|
|
int border_width;
|
|
|
|
/** padding between border and ticks/bar */
|
|
|
|
int border_padding;
|
|
|
|
/** gap/distance between the individual ticks */
|
|
|
|
int ticks_gap;
|
|
|
|
/** total number of ticks */
|
|
|
|
int ticks_count;
|
2008-03-02 13:13:12 +01:00
|
|
|
/** reverse drawing */
|
2008-03-21 16:50:17 +01:00
|
|
|
bool *reverse;
|
2008-03-02 21:53:18 +01:00
|
|
|
/** 90 Degree's turned */
|
2008-03-21 16:50:17 +01:00
|
|
|
bool vertical;
|
2008-02-17 08:42:11 +01:00
|
|
|
/** Number of data_items (bars) */
|
|
|
|
int data_items;
|
2008-01-01 16:23:28 +01:00
|
|
|
/** Height 0-1, where 1 is height of statusbar */
|
|
|
|
float height;
|
|
|
|
/** Foreground color */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *fg;
|
2008-03-26 03:18:21 +01:00
|
|
|
/** Foreground color of turned-off ticks */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *fg_off;
|
2008-02-05 01:27:39 +01:00
|
|
|
/** Foreground color when bar is half-full */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t **pfg_center;
|
2008-02-04 11:16:30 +01:00
|
|
|
/** Foreground color when bar is full */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t **pfg_end;
|
2008-01-01 16:23:28 +01:00
|
|
|
/** Background color */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *bg;
|
2008-01-01 16:23:28 +01:00
|
|
|
/** Border color */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t *bordercolor;
|
2007-12-23 14:27:56 +01:00
|
|
|
} Data;
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
static bool
|
2008-03-26 03:18:21 +01:00
|
|
|
check_settings(Data *d, int status_height)
|
|
|
|
{
|
2008-04-02 15:10:11 +02:00
|
|
|
int tmp, h_total;
|
2008-03-26 03:18:21 +01:00
|
|
|
|
|
|
|
h_total = (int)(status_height * d->height + 0.5);
|
|
|
|
|
|
|
|
if(!d->vertical) /* horizontal */
|
|
|
|
{
|
2008-04-02 15:10:11 +02:00
|
|
|
tmp = d->width - 2 * (d->border_width + d->border_padding) - 1;
|
2008-04-20 00:48:49 +02:00
|
|
|
if((d->ticks_count && tmp - (d->ticks_count - 1) * d->ticks_gap - d->ticks_count + 1 < 0)
|
|
|
|
|| (!d->ticks_count && tmp < 0))
|
2008-03-26 03:18:21 +01:00
|
|
|
{
|
|
|
|
warn("progressbar's 'width' is too small for the given configuration options\n");
|
2008-03-21 16:50:17 +01:00
|
|
|
return false;
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
2008-04-20 00:48:49 +02:00
|
|
|
tmp = h_total - d->data_items * (2 * (d->border_width + d->border_padding) + 1)
|
|
|
|
- (d->data_items - 1) * d->gap;
|
2008-04-02 15:10:11 +02:00
|
|
|
if(tmp < 0)
|
2008-03-26 03:18:21 +01:00
|
|
|
{
|
|
|
|
warn("progressbar's 'height' is too small for the given configuration options\n");
|
2008-03-21 16:50:17 +01:00
|
|
|
return false;
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* vertical / standing up */
|
|
|
|
{
|
2008-04-02 15:10:11 +02:00
|
|
|
tmp = h_total - 2 * (d->border_width + d->border_padding) - 1;
|
2008-04-20 00:48:49 +02:00
|
|
|
if((d->ticks_count && tmp - (d->ticks_count - 1) * d->ticks_gap - d->ticks_count + 1 < 0)
|
|
|
|
|| (!d->ticks_count && tmp < 0))
|
2008-03-26 03:18:21 +01:00
|
|
|
{
|
|
|
|
warn("progressbar's 'height' is too small for the given configuration options\n");
|
2008-03-21 16:50:17 +01:00
|
|
|
return false;
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
2008-04-20 00:48:49 +02:00
|
|
|
tmp = d->width - d->data_items * (2 * (d->border_width + d->border_padding) + 1)
|
|
|
|
- (d->data_items - 1) * d->gap;
|
2008-04-02 15:10:11 +02:00
|
|
|
if(tmp < 0)
|
2008-03-26 03:18:21 +01:00
|
|
|
{
|
|
|
|
warn("progressbar's 'width' is too small for the given configuration options\n");
|
2008-03-21 16:50:17 +01:00
|
|
|
return false;
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-21 16:50:17 +01:00
|
|
|
return true;
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 14:27:56 +01:00
|
|
|
static int
|
2008-04-11 11:26:37 +02:00
|
|
|
progressbar_draw(widget_t *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-03-26 03:18:21 +01:00
|
|
|
/* pb_.. values points to the widget inside a potential border */
|
|
|
|
int i, percent_ticks, pb_x, pb_y, pb_height, pb_width, pb_progress, pb_offset;
|
2008-03-26 10:59:06 +01:00
|
|
|
int unit = 0; /* tick + gap */
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t rectangle, pattern_rect;
|
2007-12-23 14:27:56 +01:00
|
|
|
Data *d = widget->data;
|
|
|
|
|
2008-04-20 00:48:49 +02:00
|
|
|
if(!d->data_items)
|
2008-01-01 15:33:30 +01:00
|
|
|
return 0;
|
|
|
|
|
2008-04-20 00:48:49 +02:00
|
|
|
if(d->vertical)
|
|
|
|
{
|
|
|
|
pb_width = (int) ((d->width - 2 * (d->border_width + d->border_padding) * d->data_items
|
|
|
|
- d->gap * (d->data_items - 1)) / d->data_items);
|
|
|
|
widget->area.width = d->data_items
|
|
|
|
* (pb_width + 2 * (d->border_width + d->border_padding)
|
|
|
|
+ d->gap) - d->gap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pb_width = d->width - 2 * (d->border_width + d->border_padding);
|
|
|
|
if(d->ticks_count && d->ticks_gap)
|
|
|
|
{
|
|
|
|
unit = (pb_width + d->ticks_gap) / d->ticks_count;
|
|
|
|
pb_width = unit * d->ticks_count - d->ticks_gap; /* rounded to match ticks... */
|
|
|
|
}
|
|
|
|
widget->area.width = pb_width + 2 * (d->border_width + d->border_padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!widget->user_supplied_x)
|
|
|
|
widget->area.x = widget_calculate_offset(widget->statusbar->width,
|
|
|
|
widget->area.width,
|
|
|
|
offset,
|
|
|
|
widget->alignment);
|
|
|
|
if(!widget->user_supplied_y)
|
|
|
|
widget->area.y = 0;
|
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
/* for a 'reversed' progressbar:
|
2008-03-26 03:18:21 +01:00
|
|
|
* basic progressbar:
|
2008-03-02 21:53:18 +01:00
|
|
|
* 1. the full space gets the size of the formerly empty one
|
|
|
|
* 2. the pattern must be mirrored
|
|
|
|
* 3. the formerly 'empty' side gets drawed with fg colors, the 'full' with bg-color
|
2008-04-02 02:59:29 +02:00
|
|
|
*
|
|
|
|
* ticks:
|
|
|
|
* 1. round the values to a full tick accordingly
|
|
|
|
* 2. finally draw the gaps
|
2008-03-02 21:53:18 +01:00
|
|
|
*/
|
2008-03-26 03:18:21 +01:00
|
|
|
|
2008-04-02 02:59:29 +02:00
|
|
|
pb_x = widget->area.x + d->border_width + d->border_padding;
|
2008-03-26 03:18:21 +01:00
|
|
|
pb_offset = 0;
|
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
if(d->vertical)
|
|
|
|
{
|
2008-04-20 00:48:49 +02:00
|
|
|
/* TODO: maybe prevent to calculate that stuff below over and over again
|
|
|
|
* (->use static-values) */
|
|
|
|
pb_height = (int) (widget->statusbar->height * d->height + 0.5)
|
|
|
|
- 2 * (d->border_width + d->border_padding);
|
2008-03-26 03:18:21 +01:00
|
|
|
if(d->ticks_count && d->ticks_gap)
|
|
|
|
{
|
2008-04-12 06:49:15 +02:00
|
|
|
/* '+ d->ticks_gap' because a unit includes a ticks + ticks_gap */
|
2008-03-26 03:18:21 +01:00
|
|
|
unit = (pb_height + d->ticks_gap) / d->ticks_count;
|
2008-04-17 07:22:46 +02:00
|
|
|
pb_height = unit * d->ticks_count - d->ticks_gap;
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
|
|
|
|
2008-04-20 00:48:49 +02:00
|
|
|
pb_y = widget->area.y + ((int) (widget->statusbar->height * (1 - d->height)) / 2)
|
|
|
|
+ d->border_width + d->border_padding;
|
2008-03-26 03:18:21 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
for(i = 0; i < d->data_items; i++)
|
2008-01-12 23:38:31 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
if(d->ticks_count && d->ticks_gap)
|
|
|
|
{
|
|
|
|
percent_ticks = (int)(d->ticks_count * (float)d->percent[i] / 100 + 0.5);
|
|
|
|
if(percent_ticks)
|
|
|
|
pb_progress = percent_ticks * unit - d->ticks_gap;
|
|
|
|
else
|
|
|
|
pb_progress = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pb_progress = (int)(pb_height * d->percent[i] / 100.0 + 0.5);
|
|
|
|
|
|
|
|
if(d->border_width)
|
|
|
|
{
|
|
|
|
/* border rectangle */
|
2008-04-17 21:50:44 +02:00
|
|
|
rectangle.x = pb_x + pb_offset - d->border_width - d->border_padding;
|
|
|
|
rectangle.y = pb_y - d->border_width - d->border_padding;
|
|
|
|
rectangle.width = pb_width + 2 * (d->border_padding + d->border_width);
|
|
|
|
rectangle.height = pb_height + 2 * (d->border_padding + d->border_width);
|
2008-03-26 03:18:21 +01:00
|
|
|
|
2008-04-02 03:02:16 +02:00
|
|
|
if(d->border_padding)
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->bg[i]);
|
|
|
|
draw_rectangle(ctx, rectangle, d->border_width, false, d->bordercolor[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
2008-03-02 21:53:18 +01:00
|
|
|
|
|
|
|
/* new value/progress in px + pattern setup */
|
|
|
|
if(!d->reverse[i])
|
|
|
|
{
|
|
|
|
/* bottom to top */
|
|
|
|
pattern_rect.x = pb_x;
|
2008-04-17 07:22:47 +02:00
|
|
|
pattern_rect.y = pb_y + pb_height;
|
2008-03-02 21:53:18 +01:00
|
|
|
pattern_rect.width = 0;
|
|
|
|
pattern_rect.height = -pb_height;
|
|
|
|
}
|
2008-03-02 13:13:12 +01:00
|
|
|
else
|
2008-03-02 21:53:18 +01:00
|
|
|
{
|
2008-04-17 07:22:47 +02:00
|
|
|
/* invert: top with bottom part */
|
|
|
|
pb_progress = pb_height - pb_progress;
|
|
|
|
pattern_rect.x = pb_x;
|
|
|
|
pattern_rect.y = pb_y;
|
2008-03-02 21:53:18 +01:00
|
|
|
pattern_rect.width = 0;
|
|
|
|
pattern_rect.height = pb_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bottom part */
|
|
|
|
if(pb_progress > 0)
|
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
rectangle.x = pb_x + pb_offset;
|
|
|
|
rectangle.y = pb_y + pb_height - pb_progress;
|
|
|
|
rectangle.width = pb_width;
|
2008-03-02 21:53:18 +01:00
|
|
|
rectangle.height = pb_progress;
|
|
|
|
|
|
|
|
/* fg color */
|
|
|
|
if(!d->reverse[i])
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect,
|
2008-03-02 21:53:18 +01:00
|
|
|
&(d->fg[i]), d->pfg_center[i], d->pfg_end[i]);
|
|
|
|
else /*REV: bg */
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->fg_off[i]);
|
2008-03-02 21:53:18 +01:00
|
|
|
}
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
/* top part */
|
2008-03-26 03:18:21 +01:00
|
|
|
if(pb_height - pb_progress > 0) /* not filled area */
|
2008-03-02 21:53:18 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
rectangle.x = pb_x + pb_offset;
|
|
|
|
rectangle.y = pb_y;
|
|
|
|
rectangle.width = pb_width;
|
|
|
|
rectangle.height = pb_height - pb_progress;
|
2008-03-02 21:53:18 +01:00
|
|
|
|
|
|
|
/* bg color */
|
|
|
|
if(!d->reverse[i])
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->fg_off[i]);
|
2008-04-17 07:22:47 +02:00
|
|
|
else /* REV: fg */
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect,
|
2008-03-02 21:53:18 +01:00
|
|
|
&(d->fg[i]), d->pfg_center[i], d->pfg_end[i]);
|
|
|
|
}
|
2008-03-26 03:18:21 +01:00
|
|
|
/* draw gaps TODO: improve e.g all in one */
|
|
|
|
if(d->ticks_count && d->ticks_gap)
|
|
|
|
{
|
|
|
|
rectangle.width = pb_width;
|
|
|
|
rectangle.height = d->ticks_gap;
|
|
|
|
rectangle.x = pb_x + pb_offset;
|
|
|
|
for(rectangle.y = pb_y + (unit - d->ticks_gap);
|
|
|
|
pb_y + pb_height - d->ticks_gap >= rectangle.y;
|
|
|
|
rectangle.y += unit)
|
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->bg[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pb_offset += pb_width + d->gap + 2 * (d->border_width + d->border_padding);
|
2008-03-02 21:53:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* a horizontal progressbar */
|
|
|
|
{
|
2008-04-20 00:48:49 +02:00
|
|
|
pb_height = (int) ((widget->statusbar->height * d->height
|
|
|
|
- d->data_items * 2 * (d->border_width + d->border_padding)
|
|
|
|
- (d->gap * (d->data_items - 1))) / d->data_items + 0.5);
|
|
|
|
pb_y = widget->area.y + ((int) (widget->statusbar->height * (1 - d->height)) / 2)
|
|
|
|
+ d->border_width + d->border_padding;
|
2008-03-26 03:18:21 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
for(i = 0; i < d->data_items; i++)
|
2008-01-12 23:38:31 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
if(d->ticks_count && d->ticks_gap)
|
|
|
|
{
|
|
|
|
/* +0.5 rounds up ticks -> turn on a tick when half of it is reached */
|
2008-04-20 00:48:49 +02:00
|
|
|
percent_ticks = (int) (d->ticks_count * (float) d->percent[i] / 100 + 0.5);
|
2008-03-26 03:18:21 +01:00
|
|
|
if(percent_ticks)
|
2008-04-17 07:22:46 +02:00
|
|
|
pb_progress = percent_ticks * unit - d->ticks_gap;
|
2008-03-26 03:18:21 +01:00
|
|
|
else
|
|
|
|
pb_progress = 0;
|
2008-04-20 00:48:49 +02:00
|
|
|
}
|
2008-03-26 03:18:21 +01:00
|
|
|
else
|
2008-04-20 00:48:49 +02:00
|
|
|
pb_progress = (int) (pb_width * d->percent[i] / 100.0 + 0.5);
|
2008-03-26 03:18:21 +01:00
|
|
|
|
|
|
|
if(d->border_width)
|
|
|
|
{
|
|
|
|
/* border rectangle */
|
2008-04-17 21:50:44 +02:00
|
|
|
rectangle.x = pb_x - d->border_width - d->border_padding;
|
|
|
|
rectangle.y = pb_y + pb_offset - d->border_width - d->border_padding;
|
|
|
|
rectangle.width = pb_width + 2 * (d->border_padding + d->border_width);
|
|
|
|
rectangle.height = pb_height + 2 * (d->border_padding + d->border_width);
|
2008-03-02 21:53:18 +01:00
|
|
|
|
2008-04-02 03:02:16 +02:00
|
|
|
if(d->border_padding)
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->bg[i]);
|
|
|
|
draw_rectangle(ctx, rectangle, d->border_width, false, d->bordercolor[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
2008-03-02 21:53:18 +01:00
|
|
|
/* new value/progress in px + pattern setup */
|
|
|
|
if(!d->reverse[i])
|
|
|
|
{
|
|
|
|
/* left to right */
|
2008-03-26 03:18:21 +01:00
|
|
|
pattern_rect.x = pb_x;
|
|
|
|
pattern_rect.y = pb_y;
|
2008-03-02 21:53:18 +01:00
|
|
|
pattern_rect.width = pb_width;
|
|
|
|
pattern_rect.height = 0;
|
|
|
|
}
|
2008-03-02 13:13:12 +01:00
|
|
|
else
|
2008-03-02 21:53:18 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
/* reverse: right to left */
|
|
|
|
pb_progress = pb_width - pb_progress;
|
|
|
|
pattern_rect.x = pb_x + pb_width;
|
|
|
|
pattern_rect.y = pb_y;
|
2008-03-02 21:53:18 +01:00
|
|
|
pattern_rect.width = -pb_width;
|
|
|
|
pattern_rect.height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* left part */
|
|
|
|
if(pb_progress > 0)
|
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
rectangle.x = pb_x;
|
|
|
|
rectangle.y = pb_y + pb_offset;
|
2008-03-02 21:53:18 +01:00
|
|
|
rectangle.width = pb_progress;
|
2008-03-26 03:18:21 +01:00
|
|
|
rectangle.height = pb_height;
|
2008-03-02 21:53:18 +01:00
|
|
|
|
|
|
|
/* fg color */
|
|
|
|
if(!d->reverse[i])
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect,
|
2008-03-02 21:53:18 +01:00
|
|
|
&(d->fg[i]), d->pfg_center[i], d->pfg_end[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
else /* reverse: bg */
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->fg_off[i]);
|
2008-03-02 21:53:18 +01:00
|
|
|
}
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
/* right part */
|
2008-03-26 03:18:21 +01:00
|
|
|
if(pb_width - pb_progress > 0)
|
2008-03-02 21:53:18 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
rectangle.x = pb_x + pb_progress;
|
|
|
|
rectangle.y = pb_y + pb_offset;
|
|
|
|
rectangle.width = pb_width - pb_progress;
|
|
|
|
rectangle.height = pb_height;
|
2008-03-02 21:53:18 +01:00
|
|
|
|
|
|
|
/* bg color */
|
|
|
|
if(!d->reverse[i])
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->fg_off[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
else /* reverse: fg */
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect,
|
2008-03-02 21:53:18 +01:00
|
|
|
&(d->fg[i]), d->pfg_center[i], d->pfg_end[i]);
|
|
|
|
}
|
2008-03-26 03:18:21 +01:00
|
|
|
/* draw gaps TODO: improve e.g all in one */
|
|
|
|
if(d->ticks_count && d->ticks_gap)
|
|
|
|
{
|
|
|
|
rectangle.width = d->ticks_gap;
|
|
|
|
rectangle.height = pb_height;
|
|
|
|
rectangle.y = pb_y + pb_offset;
|
|
|
|
for(rectangle.x = pb_x + (unit - d->ticks_gap);
|
|
|
|
pb_x + pb_width - d->ticks_gap >= rectangle.x;
|
|
|
|
rectangle.x += unit)
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->bg[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pb_offset += pb_height + d->gap + 2 * (d->border_width + d->border_padding);
|
2008-03-02 21:53:18 +01:00
|
|
|
}
|
2008-01-01 15:33:30 +01:00
|
|
|
}
|
2007-12-23 14:27:56 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-03-04 19:06:04 +01:00
|
|
|
static widget_tell_status_t
|
2008-04-23 23:44:55 +02:00
|
|
|
progressbar_tell(widget_t *widget, char *property, char *new_value)
|
2007-12-23 14:27:56 +01:00
|
|
|
{
|
|
|
|
Data *d = widget->data;
|
2008-03-06 14:30:18 +01:00
|
|
|
int i = 0, percent, tmp;
|
2008-02-17 08:42:11 +01:00
|
|
|
char *title, *setting;
|
2008-03-26 03:18:21 +01:00
|
|
|
float tmpf;
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-03-06 14:30:18 +01:00
|
|
|
if(!d->data_items)
|
|
|
|
return WIDGET_ERROR_CUSTOM; /* error already printed on _new */
|
|
|
|
|
2008-04-23 23:44:55 +02:00
|
|
|
if(new_value == NULL)
|
2008-04-23 05:35:55 +02:00
|
|
|
return WIDGET_ERROR_NOVALUE;
|
|
|
|
|
2008-02-17 08:42:11 +01:00
|
|
|
if(!a_strcmp(property, "data"))
|
2008-01-01 15:33:30 +01:00
|
|
|
{
|
2008-04-23 23:44:55 +02:00
|
|
|
title = strtok(new_value, " ");
|
2008-03-06 14:30:18 +01:00
|
|
|
if(!(setting = strtok(NULL, " ")))
|
|
|
|
return WIDGET_ERROR_NOVALUE;
|
2008-02-17 08:42:11 +01:00
|
|
|
for(i = 0; i < d->data_items; i++)
|
|
|
|
if(!a_strcmp(title, d->data_title[i]))
|
|
|
|
{
|
|
|
|
percent = atoi(setting);
|
|
|
|
d->percent[i] = (percent < 0 ? 0 : (percent > 100 ? 100 : percent));
|
2008-03-04 19:06:04 +01:00
|
|
|
return WIDGET_NOERROR;
|
2008-02-17 08:42:11 +01:00
|
|
|
}
|
2008-03-04 19:06:04 +01:00
|
|
|
return WIDGET_ERROR_FORMAT_SECTION;
|
2008-01-01 15:33:30 +01:00
|
|
|
}
|
2008-02-17 08:42:11 +01:00
|
|
|
else if(!a_strcmp(property, "fg"))
|
2008-04-23 23:44:55 +02:00
|
|
|
return widget_set_color_for_data(widget, d->fg, new_value, d->data_items, d->data_title);
|
2008-03-26 03:18:21 +01:00
|
|
|
else if(!a_strcmp(property, "fg_off"))
|
2008-04-23 23:44:55 +02:00
|
|
|
return widget_set_color_for_data(widget, d->fg_off, new_value, d->data_items, d->data_title);
|
2008-02-17 08:42:11 +01:00
|
|
|
else if(!a_strcmp(property, "bg"))
|
2008-04-23 23:44:55 +02:00
|
|
|
return widget_set_color_for_data(widget, d->bg, new_value, d->data_items, d->data_title);
|
2008-03-26 03:18:21 +01:00
|
|
|
else if(!a_strcmp(property, "bordercolor"))
|
2008-04-23 23:44:55 +02:00
|
|
|
return widget_set_color_for_data(widget, d->bordercolor, new_value, d->data_items, d->data_title);
|
2008-02-17 08:42:11 +01:00
|
|
|
else if(!a_strcmp(property, "fg_center"))
|
2008-04-23 23:44:55 +02:00
|
|
|
return widget_set_color_pointer_for_data(widget, d->pfg_center, new_value, d->data_items, d->data_title);
|
2008-02-17 08:42:11 +01:00
|
|
|
else if(!a_strcmp(property, "fg_end"))
|
2008-04-23 23:44:55 +02:00
|
|
|
return widget_set_color_pointer_for_data(widget, d->pfg_end, new_value, d->data_items, d->data_title);
|
2008-02-17 08:42:11 +01:00
|
|
|
else if(!a_strcmp(property, "gap"))
|
2008-04-23 23:44:55 +02:00
|
|
|
d->gap = atoi(new_value);
|
2008-02-17 08:42:11 +01:00
|
|
|
else if(!a_strcmp(property, "width"))
|
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
tmp = d->width;
|
2008-04-23 23:44:55 +02:00
|
|
|
d->width = atoi(new_value);
|
2008-03-26 03:18:21 +01:00
|
|
|
if(!check_settings(d, widget->statusbar->height))
|
2008-03-06 14:30:18 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
d->width = tmp; /* restore */
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_ERROR_CUSTOM;
|
|
|
|
}
|
2008-02-17 08:42:11 +01:00
|
|
|
}
|
|
|
|
else if(!a_strcmp(property, "height"))
|
2008-03-06 14:30:18 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
tmpf = d->height;
|
2008-04-23 23:44:55 +02:00
|
|
|
d->height = atof(new_value);
|
2008-03-26 03:18:21 +01:00
|
|
|
if(!check_settings(d, widget->statusbar->height))
|
2008-03-06 14:30:18 +01:00
|
|
|
{
|
2008-03-26 03:18:21 +01:00
|
|
|
d->height = tmpf; /* restore */
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_ERROR_CUSTOM;
|
|
|
|
}
|
2008-03-26 03:18:21 +01:00
|
|
|
}
|
2008-02-17 08:42:11 +01:00
|
|
|
else
|
2008-03-04 19:06:04 +01:00
|
|
|
return WIDGET_ERROR;
|
|
|
|
|
|
|
|
return WIDGET_NOERROR;
|
2007-12-23 14:27:56 +01:00
|
|
|
}
|
|
|
|
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *
|
2008-04-11 11:27:36 +02:00
|
|
|
progressbar_new(statusbar_t *statusbar, cfg_t *config)
|
2007-12-23 14:27:56 +01:00
|
|
|
{
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *w;
|
2007-12-23 14:27:56 +01:00
|
|
|
Data *d;
|
|
|
|
char *color;
|
2008-03-21 10:53:17 +01:00
|
|
|
int i;
|
2008-01-01 15:33:30 +01:00
|
|
|
cfg_t *cfg;
|
|
|
|
|
2008-03-26 03:18:21 +01:00
|
|
|
|
2008-04-11 11:26:37 +02:00
|
|
|
w = p_new(widget_t, 1);
|
2007-12-23 14:27:56 +01:00
|
|
|
widget_common_new(w, statusbar, config);
|
|
|
|
w->draw = progressbar_draw;
|
|
|
|
w->tell = progressbar_tell;
|
|
|
|
d = w->data = p_new(Data, 1);
|
2008-03-06 14:30:18 +01:00
|
|
|
|
2008-02-17 08:42:11 +01:00
|
|
|
d->height = cfg_getfloat(config, "height");
|
2008-03-26 03:18:21 +01:00
|
|
|
d->width = cfg_getint(config, "width");
|
|
|
|
|
|
|
|
d->border_padding = cfg_getint(config, "border_padding");
|
|
|
|
d->ticks_gap = cfg_getint(config, "ticks_gap");
|
|
|
|
d->ticks_count = cfg_getint(config, "ticks_count");
|
|
|
|
d->border_width = cfg_getint(config, "border_width");
|
|
|
|
|
2008-03-02 21:53:18 +01:00
|
|
|
if(!(d->vertical = cfg_getbool(config, "vertical")))
|
2008-03-21 16:50:17 +01:00
|
|
|
d->vertical = false;
|
2008-02-17 08:42:11 +01:00
|
|
|
|
2008-03-26 03:18:21 +01:00
|
|
|
d->gap = cfg_getint(config, "gap");
|
|
|
|
|
2008-03-19 12:05:36 +01:00
|
|
|
w->alignment = cfg_getalignment(config, "align");
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-02-17 08:42:11 +01:00
|
|
|
if(!(d->data_items = cfg_size(config, "data")))
|
2008-01-01 15:33:30 +01:00
|
|
|
{
|
2008-03-04 19:06:04 +01:00
|
|
|
warn("progressbar widget needs at least one bar section\n");
|
2008-01-01 15:33:30 +01:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2008-04-02 15:10:11 +02:00
|
|
|
if(!check_settings(d, statusbar->height))
|
|
|
|
{
|
|
|
|
d->data_items = 0;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
d->fg = p_new(xcolor_t, d->data_items);
|
|
|
|
d->pfg_end = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->pfg_center = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->fg_off = p_new(xcolor_t, d->data_items);
|
|
|
|
d->bg = p_new(xcolor_t, d->data_items);
|
|
|
|
d->bordercolor = p_new(xcolor_t, d->data_items);
|
2008-02-17 08:42:11 +01:00
|
|
|
d->percent = p_new(int, d->data_items);
|
2008-03-21 16:50:17 +01:00
|
|
|
d->reverse = p_new(bool, d->data_items);
|
2008-02-17 08:42:11 +01:00
|
|
|
d->data_title = p_new(char *, d->data_items);
|
2008-01-01 16:23:28 +01:00
|
|
|
|
2008-02-17 08:42:11 +01:00
|
|
|
for(i = 0; i < d->data_items; i++)
|
2008-01-01 15:33:30 +01:00
|
|
|
{
|
2008-02-17 08:42:11 +01:00
|
|
|
cfg = cfg_getnsec(config, "data", i);
|
|
|
|
|
|
|
|
d->data_title[i] = a_strdup(cfg_title(cfg));
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-03-02 13:13:12 +01:00
|
|
|
if(!(d->reverse[i] = cfg_getbool(cfg, "reverse")))
|
2008-03-21 16:50:17 +01:00
|
|
|
d->reverse[i] = false;
|
2008-03-02 13:13:12 +01:00
|
|
|
|
2008-01-01 15:33:30 +01:00
|
|
|
if((color = cfg_getstr(cfg, "fg")))
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, &d->fg[i]);
|
2008-01-01 15:33:30 +01:00
|
|
|
else
|
2008-03-14 08:35:06 +01:00
|
|
|
d->fg[i] = globalconf.screens[statusbar->screen].styles.normal.fg;
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-02-06 01:07:27 +01:00
|
|
|
if((color = cfg_getstr(cfg, "fg_center")))
|
2008-02-05 01:27:39 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
d->pfg_center[i] = p_new(xcolor_t, 1);
|
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, d->pfg_center[i]);
|
2008-02-05 01:27:39 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 01:07:27 +01:00
|
|
|
if((color = cfg_getstr(cfg, "fg_end")))
|
2008-02-05 01:27:39 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
d->pfg_end[i] = p_new(xcolor_t, 1);
|
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, d->pfg_end[i]);
|
2008-02-05 01:27:39 +01:00
|
|
|
}
|
2008-02-04 11:16:30 +01:00
|
|
|
|
2008-03-26 03:18:21 +01:00
|
|
|
if((color = cfg_getstr(cfg, "fg_off")))
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, &d->fg_off[i]);
|
2008-03-26 03:18:21 +01:00
|
|
|
else
|
|
|
|
d->fg_off[i] = globalconf.screens[statusbar->screen].styles.normal.bg;
|
|
|
|
|
2008-01-01 15:33:30 +01:00
|
|
|
if((color = cfg_getstr(cfg, "bg")))
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, &d->bg[i]);
|
2008-01-01 15:33:30 +01:00
|
|
|
else
|
2008-03-14 08:35:06 +01:00
|
|
|
d->bg[i] = globalconf.screens[statusbar->screen].styles.normal.bg;
|
2008-01-01 15:33:30 +01:00
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
if((color = cfg_getstr(cfg, "bordercolor")))
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, &d->bordercolor[i]);
|
2008-01-01 15:33:30 +01:00
|
|
|
else
|
2008-01-06 18:23:56 +01:00
|
|
|
d->bordercolor[i] = d->fg[i];
|
2008-02-05 01:27:39 +01:00
|
|
|
}
|
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
|