2008-01-06 18:23:56 +01:00
|
|
|
/*
|
|
|
|
* graph.c - a graph widget
|
|
|
|
*
|
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
|
|
|
* Copyright © 2007-2008 Marco Candrian <mac@calmar.ws>
|
|
|
|
*
|
|
|
|
* 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 "widget.h"
|
|
|
|
#include "screen.h"
|
2008-01-21 18:14:59 +01:00
|
|
|
#include "common/util.h"
|
2008-03-19 11:58:43 +01:00
|
|
|
#include "common/draw.h"
|
|
|
|
#include "common/configopts.h"
|
2008-03-21 16:50:17 +01:00
|
|
|
#include "common/xutil.h"
|
2008-01-06 18:23:56 +01:00
|
|
|
|
|
|
|
extern AwesomeConf globalconf;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2008-01-25 21:39:08 +01:00
|
|
|
/* general layout */
|
2008-02-17 08:49:37 +01:00
|
|
|
|
|
|
|
char **data_title; /** data_title of the data sections */
|
2008-02-05 15:33:42 +01:00
|
|
|
float *max; /** Represents a full graph */
|
|
|
|
int width; /** Width of the widget */
|
|
|
|
float height; /** Height of graph (0-1; 1 = height of statusbar) */
|
|
|
|
int box_height; /** Height of the innerbox in pixels */
|
|
|
|
int size; /** Size of lines-array (also innerbox-lenght) */
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t bg; /** Background color */
|
|
|
|
xcolor_t bordercolor; /** Border color */
|
2008-04-11 11:19:23 +02:00
|
|
|
position_t grow; /** grow: Left or Right */
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
/* markers... */
|
2008-02-17 08:49:37 +01:00
|
|
|
int *index; /** Index of current (new) value */
|
2008-02-05 15:33:42 +01:00
|
|
|
int *max_index; /** Index of the actual maximum value */
|
|
|
|
float *current_max; /** Pointer to current maximum value itself */
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
/* all data is stored here */
|
2008-02-05 15:33:42 +01:00
|
|
|
int data_items; /** Number of data-input items */
|
|
|
|
int **lines; /** Keeps the calculated values (line-length); */
|
|
|
|
float **values; /** Actual values */
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
/* additional data + a pointer to **lines accordingly */
|
2008-02-17 08:49:37 +01:00
|
|
|
int **fillbottom; /** Data array pointer (like *lines) */
|
|
|
|
int **fillbottom_index; /** Points to some index[i] */
|
2008-02-05 15:33:42 +01:00
|
|
|
int fillbottom_total; /** Total of them */
|
2008-03-21 16:50:17 +01:00
|
|
|
bool *fillbottom_vertical_grad; /** Create a vertical color gradient */
|
|
|
|
xcolor_t *fillbottom_color; /** Color of them */
|
|
|
|
xcolor_t **fillbottom_pcolor_center; /** Color at middle of graph */
|
|
|
|
xcolor_t **fillbottom_pcolor_end; /** Color at end of graph */
|
2008-02-17 08:49:37 +01:00
|
|
|
int **filltop; /** Data array pointer (like *lines) */
|
|
|
|
int **filltop_index; /** Points to some index[i] */
|
2008-02-05 15:33:42 +01:00
|
|
|
int filltop_total; /** Total of them */
|
2008-03-21 16:50:17 +01:00
|
|
|
bool *filltop_vertical_grad; /** Create a vertical color gradient */
|
|
|
|
xcolor_t *filltop_color; /** Color of them */
|
|
|
|
xcolor_t **filltop_pcolor_center; /** Color at center of graph */
|
|
|
|
xcolor_t **filltop_pcolor_end; /** Color at end of graph */
|
2008-02-17 08:49:37 +01:00
|
|
|
int **drawline; /** Data array pointer (like *lines) */
|
|
|
|
int **drawline_index; /** Points to some index[i] */
|
2008-02-05 15:33:42 +01:00
|
|
|
int drawline_total; /** Total of them */
|
2008-03-21 16:50:17 +01:00
|
|
|
bool *drawline_vertical_grad; /** Create a vertical color gradient */
|
|
|
|
xcolor_t *drawline_color; /** Color of them */
|
|
|
|
xcolor_t **drawline_pcolor_center; /** Color at middle of graph */
|
|
|
|
xcolor_t **drawline_pcolor_end; /** Color at end of graph */
|
2008-02-05 15:33:42 +01:00
|
|
|
|
|
|
|
int *draw_from; /** Preparation/tmp array for draw_graph(); */
|
|
|
|
int *draw_to; /** Preparation/tmp array for draw_graph(); */
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
} Data;
|
|
|
|
|
|
|
|
static int
|
2008-04-11 11:26:37 +02:00
|
|
|
graph_draw(widget_t *widget, DrawCtx *ctx, int offset,
|
2008-01-06 18:23:56 +01:00
|
|
|
int used __attribute__ ((unused)))
|
|
|
|
{
|
2008-03-31 10:04:03 +02:00
|
|
|
int margin_top;
|
2008-03-10 14:30:40 +01:00
|
|
|
int z, y, x, tmp, cur_index, test_index;
|
2008-01-06 18:23:56 +01:00
|
|
|
Data *d = widget->data;
|
2008-03-14 09:37:25 +01:00
|
|
|
area_t rectangle, pattern_area;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-03-06 14:30:18 +01:00
|
|
|
if(!d->data_items)
|
2008-01-22 14:56:03 +01:00
|
|
|
return 0;
|
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
if(!widget->user_supplied_x)
|
|
|
|
widget->area.x = widget_calculate_offset(widget->statusbar->width,
|
|
|
|
d->width,
|
|
|
|
offset,
|
|
|
|
widget->alignment);
|
|
|
|
if(!widget->user_supplied_y)
|
|
|
|
widget->area.y = 0;
|
|
|
|
|
2008-03-12 15:19:50 +01:00
|
|
|
/* box = the graph inside the rectangle */
|
2008-01-09 09:59:42 +01:00
|
|
|
if(!(d->box_height))
|
|
|
|
d->box_height = (int) (widget->statusbar->height * d->height + 0.5) - 2;
|
|
|
|
|
2008-03-12 15:19:50 +01:00
|
|
|
margin_top = (int)((widget->statusbar->height - (d->box_height + 2)) / 2 + 0.5) + widget->area.y;
|
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* draw background */
|
|
|
|
rectangle.x = widget->area.x + 1;
|
|
|
|
rectangle.y = margin_top + 1;
|
2008-03-09 23:49:03 +01:00
|
|
|
rectangle.width = d->size;
|
2008-04-12 08:56:14 +02:00
|
|
|
rectangle.height = d->box_height;
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, d->bg);
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
/* for graph drawing */
|
|
|
|
rectangle.y = margin_top + d->box_height + 1; /* bottom left corner as starting point */
|
|
|
|
rectangle.width = d->size; /* rectangle.height is not used */
|
|
|
|
|
2008-01-31 19:40:47 +01:00
|
|
|
draw_graph_setup(ctx); /* setup some drawing options */
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-04-14 03:38:59 +02:00
|
|
|
/* gradient begin either left or on the right of the rectangle */
|
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.x = rectangle.x + rectangle.width;
|
|
|
|
else
|
|
|
|
pattern_area.x = rectangle.x;
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
if(d->filltop_total)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-03-09 23:49:03 +01:00
|
|
|
pattern_area.y = rectangle.y - rectangle.height;
|
2008-04-14 03:38:59 +02:00
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
/* draw style = top */
|
|
|
|
for(z = 0; z < d->filltop_total; z++)
|
|
|
|
{
|
2008-04-18 01:30:12 +02:00
|
|
|
if(d->filltop_vertical_grad[z])
|
|
|
|
{
|
|
|
|
pattern_area.width = 0;
|
|
|
|
pattern_area.height = rectangle.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern_area.height = 0;
|
|
|
|
|
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.width = -rectangle.width;
|
|
|
|
else
|
|
|
|
pattern_area.width = rectangle.width;
|
|
|
|
}
|
|
|
|
|
2008-03-10 14:30:40 +01:00
|
|
|
cur_index = *(d->filltop_index[z]);
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
for(y = 0; y < d->size; y++)
|
|
|
|
{
|
|
|
|
/* draw this filltop data thing [z]. But figure out the part
|
|
|
|
* what shall be visible. Therefore find the next smaller value
|
2008-03-10 14:30:40 +01:00
|
|
|
* on this index (draw_from) - might be 0 (then draw from start) */
|
2008-03-09 23:49:03 +01:00
|
|
|
for(tmp = 0, x = 0; x < d->filltop_total; x++)
|
|
|
|
{
|
|
|
|
if (x == z) /* no need to compare with itself */
|
|
|
|
continue;
|
|
|
|
|
2008-03-10 14:30:40 +01:00
|
|
|
/* current index's can be different (widget_tell might shift
|
|
|
|
* some with a different frequenzy), so calculate
|
|
|
|
* offset and compare accordingly finally */
|
|
|
|
test_index = cur_index + (*(d->filltop_index[x]) - *(d->filltop_index[z]));
|
|
|
|
|
|
|
|
if (test_index < 0)
|
|
|
|
test_index = d->size + test_index; /* text_index is minus, since < 0 */
|
|
|
|
else if(test_index >= d->size)
|
|
|
|
test_index -= d->size;
|
|
|
|
|
|
|
|
/* ... (test_)index to test for a smaller value found. */
|
|
|
|
|
|
|
|
/* if such a smaller value (to not overdraw!) is there, store into 'tmp' */
|
|
|
|
if(d->filltop[x][test_index] > tmp && d->filltop[x][test_index] < d->filltop[z][cur_index])
|
|
|
|
tmp = d->filltop[x][test_index];
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
}
|
2008-03-10 14:30:40 +01:00
|
|
|
/* reverse values (because drawing from top) */
|
|
|
|
d->draw_from[cur_index] = d->box_height - tmp; /* i.e. no smaller value -> from top of box */
|
|
|
|
d->draw_to[cur_index] = d->box_height - d->filltop[z][cur_index]; /* i.e. on full graph -> 0 = bottom */
|
|
|
|
|
|
|
|
if (--cur_index < 0) /* next index to compare to other values */
|
|
|
|
cur_index = d->size - 1;
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-03-18 03:15:50 +01:00
|
|
|
draw_graph(ctx, rectangle , d->draw_from, d->draw_to, *(d->filltop_index[z]), d->grow, pattern_area,
|
2008-03-09 23:49:03 +01:00
|
|
|
&(d->filltop_color[z]), d->filltop_pcolor_center[z], d->filltop_pcolor_end[z]);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
pattern_area.y = rectangle.y;
|
|
|
|
|
|
|
|
if(d->fillbottom_total)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-03-09 23:49:03 +01:00
|
|
|
/* draw style = bottom */
|
|
|
|
for(z = 0; z < d->fillbottom_total; z++)
|
|
|
|
{
|
2008-04-18 01:30:12 +02:00
|
|
|
if(d->fillbottom_vertical_grad[z])
|
|
|
|
{
|
|
|
|
pattern_area.width = 0;
|
|
|
|
pattern_area.height = -rectangle.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern_area.height = 0;
|
|
|
|
|
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.width = -rectangle.width;
|
|
|
|
else
|
|
|
|
pattern_area.width = rectangle.width;
|
|
|
|
}
|
|
|
|
|
2008-03-10 14:30:40 +01:00
|
|
|
cur_index = *(d->fillbottom_index[z]);
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
for(y = 0; y < d->size; y++)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-03-09 23:49:03 +01:00
|
|
|
for(tmp = 0, x = 0; x < d->fillbottom_total; x++)
|
|
|
|
{
|
|
|
|
if (x == z)
|
|
|
|
continue;
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-03-10 14:30:40 +01:00
|
|
|
test_index = cur_index + (*(d->fillbottom_index[x]) - *(d->fillbottom_index[z]));
|
|
|
|
|
|
|
|
if (test_index < 0)
|
|
|
|
test_index = d->size + test_index;
|
|
|
|
else if(test_index >= d->size)
|
|
|
|
test_index -= d->size;
|
|
|
|
|
|
|
|
if(d->fillbottom[x][test_index] > tmp && d->fillbottom[x][test_index] < d->fillbottom[z][cur_index])
|
|
|
|
tmp = d->fillbottom[x][test_index];
|
2008-03-09 23:49:03 +01:00
|
|
|
}
|
2008-03-10 14:30:40 +01:00
|
|
|
d->draw_from[cur_index] = tmp;
|
|
|
|
if (--cur_index < 0)
|
|
|
|
cur_index = d->size - 1;
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-03-18 03:15:50 +01:00
|
|
|
draw_graph(ctx, rectangle, d->draw_from, d->fillbottom[z], *(d->fillbottom_index[z]), d->grow,
|
|
|
|
pattern_area, &(d->fillbottom_color[z]), d->fillbottom_pcolor_center[z], d->fillbottom_pcolor_end[z]);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
if(d->drawline_total)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-03-09 23:49:03 +01:00
|
|
|
/* draw style = line */
|
|
|
|
for(z = 0; z < d->drawline_total; z++)
|
|
|
|
{
|
2008-04-18 01:30:12 +02:00
|
|
|
if(d->drawline_vertical_grad[z])
|
|
|
|
{
|
|
|
|
pattern_area.width = 0;
|
|
|
|
pattern_area.height = -rectangle.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern_area.height = 0;
|
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.width = -rectangle.width;
|
|
|
|
else
|
|
|
|
pattern_area.width = rectangle.width;
|
|
|
|
}
|
|
|
|
|
2008-03-18 03:15:50 +01:00
|
|
|
draw_graph_line(ctx, rectangle, d->drawline[z], *(d->drawline_index[z]), d->grow, pattern_area,
|
|
|
|
&(d->drawline_color[z]), d->drawline_pcolor_center[z], d->drawline_pcolor_end[z]);
|
2008-03-09 23:49:03 +01:00
|
|
|
}
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* draw border (after line-drawing, what paints 0-values to the border) */
|
|
|
|
rectangle.x = widget->area.x;
|
|
|
|
rectangle.y = margin_top;
|
|
|
|
rectangle.width = d->size + 2;
|
|
|
|
rectangle.height = d->box_height + 2;
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, false, d->bordercolor);
|
2008-04-18 00:58:42 +02:00
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
widget->area.width = d->width;
|
|
|
|
widget->area.height = widget->statusbar->height;
|
|
|
|
return widget->area.width;
|
|
|
|
}
|
|
|
|
|
2008-03-04 19:06:04 +01:00
|
|
|
static widget_tell_status_t
|
2008-04-23 23:44:55 +02:00
|
|
|
graph_tell(widget_t *widget, char *property, char *new_value)
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
|
|
|
Data *d = widget->data;
|
2008-02-17 08:49:37 +01:00
|
|
|
int i, u;
|
|
|
|
float value;
|
|
|
|
char *title, *setting;
|
2008-01-06 18:23:56 +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:49:37 +01:00
|
|
|
if(!a_strcmp(property, "data"))
|
2008-01-25 21:39:08 +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-01-25 21:39:08 +01:00
|
|
|
|
2008-02-17 08:49:37 +01:00
|
|
|
for(i = 0; i < d->data_items; i++)
|
|
|
|
{
|
|
|
|
if(!a_strcmp(title, d->data_title[i]))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-02-17 08:49:37 +01:00
|
|
|
value = MAX(atof(setting), 0);
|
|
|
|
|
|
|
|
if(++d->index[i] >= d->size) /* cycle inside the array */
|
|
|
|
d->index[i] = 0;
|
|
|
|
|
|
|
|
if(d->values[i]) /* scale option is true */
|
|
|
|
{
|
|
|
|
d->values[i][d->index[i]] = value;
|
|
|
|
|
|
|
|
if(value > d->current_max[i]) /* a new maximum value found */
|
|
|
|
{
|
|
|
|
d->max_index[i] = d->index[i];
|
|
|
|
d->current_max[i] = value;
|
|
|
|
|
|
|
|
/* recalculate */
|
|
|
|
for (u = 0; u < d->size; u++)
|
|
|
|
d->lines[i][u] = (int) (d->values[i][u] * (d->box_height) / d->current_max[i] + 0.5);
|
|
|
|
}
|
|
|
|
/* old max_index reached + current_max > normal, re-check/generate */
|
|
|
|
else if(d->max_index[i] == d->index[i] && d->current_max[i] > d->max[i])
|
|
|
|
{
|
|
|
|
/* find the new max */
|
|
|
|
for (u = 0; u < d->size; u++)
|
|
|
|
if (d->values[i][u] > d->values[i][d->max_index[i]])
|
|
|
|
d->max_index[i] = u;
|
|
|
|
|
|
|
|
d->current_max[i] = MAX(d->values[i][d->max_index[i]], d->max[i]);
|
|
|
|
|
|
|
|
/* recalculate */
|
|
|
|
for (u = 0; u < d->size; u++)
|
|
|
|
d->lines[i][u] = (int) (d->values[i][u] * d->box_height / d->current_max[i] + 0.5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
d->lines[i][d->index[i]] = (int) (value * d->box_height / d->current_max[i] + 0.5);
|
|
|
|
}
|
|
|
|
else /* scale option is false - limit to d->box_height */
|
|
|
|
{
|
|
|
|
if (value < d->current_max[i])
|
|
|
|
d->lines[i][d->index[i]] = (int) (value * d->box_height / d->current_max[i] + 0.5);
|
|
|
|
else
|
|
|
|
d->lines[i][d->index[i]] = d->box_height;
|
|
|
|
}
|
2008-03-04 19:06:04 +01:00
|
|
|
return WIDGET_NOERROR;
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-02-17 08:49:37 +01:00
|
|
|
}
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_ERROR_FORMAT_SECTION;
|
2008-02-17 08:49:37 +01:00
|
|
|
}
|
|
|
|
else if(!a_strcmp(property, "height"))
|
2008-04-23 23:44:55 +02:00
|
|
|
d->height = atof(new_value);
|
2008-02-17 08:49:37 +01:00
|
|
|
else if(!a_strcmp(property, "bg"))
|
2008-03-06 14:30:18 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
if(!draw_color_new(globalconf.connection,
|
2008-03-21 10:53:17 +01:00
|
|
|
widget->statusbar->phys_screen,
|
2008-04-23 23:44:55 +02:00
|
|
|
new_value, &d->bg))
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_ERROR_FORMAT_COLOR;
|
|
|
|
}
|
2008-02-17 08:49:37 +01:00
|
|
|
else if(!a_strcmp(property, "bordercolor"))
|
2008-03-06 14:30:18 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
if(!draw_color_new(globalconf.connection,
|
2008-03-21 10:53:17 +01:00
|
|
|
widget->statusbar->phys_screen,
|
2008-04-23 23:44:55 +02:00
|
|
|
new_value, &d->bordercolor))
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_ERROR_FORMAT_COLOR;
|
|
|
|
}
|
2008-03-18 03:15:50 +01:00
|
|
|
else if(!a_strcmp(property, "grow"))
|
2008-04-23 23:44:55 +02:00
|
|
|
switch((d->grow = position_get_from_str(new_value)))
|
2008-03-18 03:15:50 +01:00
|
|
|
{
|
2008-03-19 11:15:58 +01:00
|
|
|
case Left:
|
|
|
|
case Right:
|
|
|
|
break;
|
|
|
|
default:
|
2008-03-18 03:15:50 +01:00
|
|
|
warn("error changing property %s of widget %s, must be 'left' or 'right'\n",
|
|
|
|
property, widget->name);
|
|
|
|
return WIDGET_ERROR_CUSTOM;
|
|
|
|
}
|
2008-02-17 08:49:37 +01:00
|
|
|
else
|
2008-03-04 19:06:04 +01:00
|
|
|
return WIDGET_ERROR;
|
2008-02-17 08:49:37 +01:00
|
|
|
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_NOERROR;
|
2008-01-06 18:23:56 +01:00
|
|
|
}
|
|
|
|
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *
|
2008-04-11 11:27:36 +02:00
|
|
|
graph_new(statusbar_t *statusbar, cfg_t *config)
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *w;
|
2008-01-06 18:23:56 +01:00
|
|
|
Data *d;
|
2008-01-25 21:39:08 +01:00
|
|
|
cfg_t *cfg;
|
2008-01-06 18:23:56 +01:00
|
|
|
char *color;
|
2008-01-25 21:39:08 +01:00
|
|
|
int i;
|
|
|
|
char *type;
|
2008-03-21 16:50:17 +01:00
|
|
|
xcolor_t tmp_color = { 0, 0, 0, 0 };
|
|
|
|
xcolor_t *ptmp_color_center;
|
|
|
|
xcolor_t *ptmp_color_end;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-04-11 11:26:37 +02:00
|
|
|
w = p_new(widget_t, 1);
|
2008-01-06 18:23:56 +01:00
|
|
|
widget_common_new(w, statusbar, config);
|
|
|
|
|
|
|
|
w->draw = graph_draw;
|
|
|
|
w->tell = graph_tell;
|
2008-03-19 12:05:36 +01:00
|
|
|
w->alignment = cfg_getalignment(config, "align");
|
2008-01-06 18:23:56 +01:00
|
|
|
d = w->data = p_new(Data, 1);
|
|
|
|
|
|
|
|
d->width = cfg_getint(config, "width");
|
2008-01-09 09:59:42 +01:00
|
|
|
d->height = cfg_getfloat(config, "height");
|
2008-03-31 14:31:43 +02:00
|
|
|
d->size = d->width - 2;
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
if(!(d->data_items = cfg_size(config, "data")))
|
2008-01-22 14:56:03 +01:00
|
|
|
{
|
2008-01-25 21:39:08 +01:00
|
|
|
warn("graph widget needs at least one data section\n");
|
2008-01-22 14:56:03 +01:00
|
|
|
return w;
|
|
|
|
}
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-03-19 11:58:43 +01:00
|
|
|
d->grow = cfg_getposition(config, "grow");
|
2008-03-18 03:15:50 +01:00
|
|
|
if(d->grow != Left && d->grow != Right)
|
|
|
|
{
|
|
|
|
warn("graph widget: 'grow' argument must be 'left' or 'right'\n");
|
|
|
|
d->data_items = 0; /* disable widget drawing */
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2008-01-25 21:39:08 +01:00
|
|
|
d->draw_from = p_new(int, d->size);
|
|
|
|
d->draw_to = p_new(int, d->size);
|
2008-01-09 09:59:42 +01:00
|
|
|
|
2008-01-25 21:39:08 +01:00
|
|
|
d->fillbottom = p_new(int *, d->size);
|
2008-02-17 08:49:37 +01:00
|
|
|
d->fillbottom_index = p_new(int *, d->size);
|
2008-01-25 21:39:08 +01:00
|
|
|
d->filltop = p_new(int *, d->size);
|
2008-02-17 08:49:37 +01:00
|
|
|
d->filltop_index = p_new(int *, d->size);
|
2008-01-25 21:39:08 +01:00
|
|
|
d->drawline = p_new(int *, d->size);
|
2008-02-17 08:49:37 +01:00
|
|
|
d->drawline_index = p_new(int *, d->size);
|
2008-01-09 11:48:12 +01:00
|
|
|
|
2008-02-17 08:49:37 +01:00
|
|
|
d->data_title = p_new(char *, d->data_items);
|
2008-01-25 21:39:08 +01:00
|
|
|
d->values = p_new(float *, d->data_items);
|
|
|
|
d->lines = p_new(int *, d->data_items);
|
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
d->filltop_color = p_new(xcolor_t, d->data_items);
|
|
|
|
d->filltop_pcolor_center = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->filltop_pcolor_end = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->filltop_vertical_grad = p_new(bool, d->data_items);
|
|
|
|
d->fillbottom_color = p_new(xcolor_t, d->data_items);
|
|
|
|
d->fillbottom_pcolor_center = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->fillbottom_pcolor_end = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->fillbottom_vertical_grad = p_new(bool, d->data_items);
|
|
|
|
d->drawline_color = p_new(xcolor_t, d->data_items);
|
|
|
|
d->drawline_pcolor_center = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->drawline_pcolor_end = p_new(xcolor_t *, d->data_items);
|
|
|
|
d->drawline_vertical_grad = p_new(bool, d->data_items);
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
d->max_index = p_new(int, d->data_items);
|
2008-02-17 08:49:37 +01:00
|
|
|
d->index = p_new(int, d->data_items);
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
d->current_max = p_new(float, d->data_items);
|
|
|
|
d->max = p_new(float, d->data_items);
|
|
|
|
|
|
|
|
for(i = 0; i < d->data_items; i++)
|
|
|
|
{
|
2008-02-06 01:07:27 +01:00
|
|
|
ptmp_color_center = ptmp_color_end = NULL;
|
2008-02-05 15:33:42 +01:00
|
|
|
|
2008-01-25 21:39:08 +01:00
|
|
|
cfg = cfg_getnsec(config, "data", i);
|
|
|
|
|
2008-02-17 08:49:37 +01:00
|
|
|
d->data_title[i] = a_strdup(cfg_title(cfg));
|
|
|
|
|
2008-01-25 21:39:08 +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, &tmp_color);
|
2008-01-25 21:39:08 +01:00
|
|
|
else
|
2008-03-14 08:35:06 +01:00
|
|
|
tmp_color = globalconf.screens[statusbar->screen].styles.normal.fg;
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-02-06 01:07:27 +01:00
|
|
|
if((color = cfg_getstr(cfg, "fg_center")))
|
2008-02-05 15:33:42 +01:00
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
ptmp_color_center = p_new(xcolor_t, 1);
|
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, ptmp_color_center);
|
2008-02-05 15:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if((color = cfg_getstr(cfg, "fg_end")))
|
|
|
|
{
|
2008-03-21 16:50:17 +01:00
|
|
|
ptmp_color_end = p_new(xcolor_t, 1);
|
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, ptmp_color_end);
|
2008-02-05 15:33:42 +01:00
|
|
|
}
|
|
|
|
|
2008-01-25 21:39:08 +01:00
|
|
|
if (cfg_getbool(cfg, "scale"))
|
|
|
|
d->values[i] = p_new(float, d->size); /* not null -> scale = true */
|
|
|
|
|
|
|
|
/* prevent: division by zero */
|
|
|
|
d->current_max[i] = d->max[i] = cfg_getfloat(cfg, "max");
|
|
|
|
d->lines[i] = p_new(int, d->size);
|
|
|
|
|
|
|
|
/* filter each style-typ into it's own array (for easy looping later)*/
|
|
|
|
|
2008-03-15 17:44:14 +01:00
|
|
|
if ((type = cfg_getstr(cfg, "draw_style")))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-01-31 19:40:47 +01:00
|
|
|
if(!a_strncmp(type, "bottom", sizeof("bottom")))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
|
|
|
d->fillbottom[d->fillbottom_total] = d->lines[i];
|
2008-02-17 08:49:37 +01:00
|
|
|
d->fillbottom_index[d->fillbottom_total] = &d->index[i];
|
2008-01-25 21:39:08 +01:00
|
|
|
d->fillbottom_color[d->fillbottom_total] = tmp_color;
|
2008-02-06 01:07:27 +01:00
|
|
|
d->fillbottom_pcolor_center[d->fillbottom_total] = ptmp_color_center;
|
2008-02-05 15:33:42 +01:00
|
|
|
d->fillbottom_pcolor_end[d->fillbottom_total] = ptmp_color_end;
|
2008-03-09 23:49:03 +01:00
|
|
|
d->fillbottom_vertical_grad[d->fillbottom_total] = cfg_getbool(cfg, "vertical_gradient");
|
2008-01-25 21:39:08 +01:00
|
|
|
d->fillbottom_total++;
|
|
|
|
}
|
2008-01-31 19:40:47 +01:00
|
|
|
else if (!a_strncmp(type, "top", sizeof("top")))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
|
|
|
d->filltop[d->filltop_total] = d->lines[i];
|
2008-02-17 08:49:37 +01:00
|
|
|
d->filltop_index[d->filltop_total] = &d->index[i];
|
2008-01-25 21:39:08 +01:00
|
|
|
d->filltop_color[d->filltop_total] = tmp_color;
|
2008-02-17 08:49:37 +01:00
|
|
|
d->filltop_pcolor_center[d->filltop_total] = ptmp_color_center;
|
|
|
|
d->filltop_pcolor_end[d->filltop_total] = ptmp_color_end;
|
2008-03-09 23:49:03 +01:00
|
|
|
d->filltop_vertical_grad[d->filltop_total] = cfg_getbool(cfg, "vertical_gradient");
|
2008-01-25 21:39:08 +01:00
|
|
|
d->filltop_total++;
|
|
|
|
}
|
2008-01-31 19:40:47 +01:00
|
|
|
else if (!a_strncmp(type, "line", sizeof("line")))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
|
|
|
d->drawline[d->drawline_total] = d->lines[i];
|
2008-02-17 08:49:37 +01:00
|
|
|
d->drawline_index[d->drawline_total] = &d->index[i];
|
2008-01-25 21:39:08 +01:00
|
|
|
d->drawline_color[d->drawline_total] = tmp_color;
|
2008-02-17 08:49:37 +01:00
|
|
|
d->drawline_pcolor_center[d->drawline_total] = ptmp_color_center;
|
|
|
|
d->drawline_pcolor_end[d->drawline_total] = ptmp_color_end;
|
2008-03-09 23:49:03 +01:00
|
|
|
d->drawline_vertical_grad[d->drawline_total] = cfg_getbool(cfg, "vertical_gradient");
|
2008-01-25 21:39:08 +01:00
|
|
|
d->drawline_total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-06 18:23:56 +01:00
|
|
|
|
|
|
|
if((color = cfg_getstr(config, "bg")))
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, &d->bg);
|
2008-01-06 18:23:56 +01:00
|
|
|
else
|
2008-03-14 08:35:06 +01:00
|
|
|
d->bg = globalconf.screens[statusbar->screen].styles.normal.bg;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
|
|
|
if((color = cfg_getstr(config, "bordercolor")))
|
2008-03-21 16:50:17 +01:00
|
|
|
draw_color_new(globalconf.connection, statusbar->phys_screen, color, &d->bordercolor);
|
2008-01-06 18:23:56 +01:00
|
|
|
else
|
2008-01-25 21:39:08 +01:00
|
|
|
d->bordercolor = tmp_color;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|