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-03-19 11:58:43 +01:00
|
|
|
#include "common/draw.h"
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-05-24 08:59:27 +02:00
|
|
|
extern awesome_t globalconf;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
Top_Style = 1,
|
|
|
|
Bottom_Style,
|
|
|
|
Line_Style
|
|
|
|
} draw_style_t;
|
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2008-01-25 21:39:08 +01:00
|
|
|
/* general layout */
|
2008-02-17 08:49:37 +01:00
|
|
|
|
2008-05-31 14:38:27 +02: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 */
|
2008-06-02 12:18:17 +02:00
|
|
|
float height; /** Height of graph (0.0-1.0; 1.0 = height of bar) */
|
2008-02-05 15:33:42 +01:00
|
|
|
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-05-31 14:38:27 +02:00
|
|
|
position_t grow; /** grow: Left or Right */
|
|
|
|
|
|
|
|
bool *scale; /** Scale the graph */
|
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-06-03 04:51:48 +02:00
|
|
|
draw_style_t *draw_style; /** Draw style of according index */
|
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
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
bool *vertical_gradient; /** Create a vertical color gradient */
|
|
|
|
|
|
|
|
xcolor_t *color_start; /** Color of them */
|
|
|
|
xcolor_t **pcolor_center; /** Color at middle of graph */
|
|
|
|
xcolor_t **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-06 18:23:56 +01:00
|
|
|
} Data;
|
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
/* the same as the progressbar_pcolor_set may use a common function */
|
|
|
|
static void
|
|
|
|
graph_pcolor_set(xcolor_t **ppcolor, char *new_color)
|
|
|
|
{
|
|
|
|
bool flag = false;
|
|
|
|
if(!*ppcolor)
|
|
|
|
{
|
|
|
|
flag = true; /* p_delete && restore to NULL, if xcolor_new unsuccessful */
|
|
|
|
*ppcolor = p_new(xcolor_t, 1);
|
|
|
|
}
|
|
|
|
if(!(xcolor_new(globalconf.connection,
|
|
|
|
globalconf.default_screen,
|
|
|
|
new_color, *ppcolor))
|
|
|
|
&& flag)
|
|
|
|
p_delete(ppcolor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
graph_data_add(Data *d, const char *new_data_title)
|
|
|
|
{
|
|
|
|
d->data_items++;
|
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
/* memory (re-)allocating + initialising */
|
2008-05-31 14:38:27 +02:00
|
|
|
p_realloc(&(d->values), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->values[d->data_items - 1] = p_new(float, d->size);
|
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
p_realloc(&(d->lines), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->lines[d->data_items - 1] = p_new(int, d->size);
|
2008-05-31 14:38:27 +02:00
|
|
|
|
|
|
|
p_realloc(&(d->scale), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->scale[d->data_items - 1] = false;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
|
|
|
p_realloc(&(d->index), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->index[d->data_items - 1] = 0;
|
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
p_realloc(&(d->max_index), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->max_index[d->data_items - 1] = 0;
|
|
|
|
|
|
|
|
p_realloc(&(d->current_max), d->data_items);
|
|
|
|
d->current_max[d->data_items - 1] = 0;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
|
|
|
p_realloc(&(d->max), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->max[d->data_items - 1] = 100.0;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
|
|
|
p_realloc(&(d->data_title), d->data_items);
|
2008-06-03 04:51:48 +02:00
|
|
|
d->data_title[d->data_items - 1] = a_strdup(new_data_title);
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
p_realloc(&(d->color_start), d->data_items);
|
|
|
|
d->color_start[d->data_items - 1] = globalconf.colors.fg;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
p_realloc(&(d->pcolor_center), d->data_items);
|
|
|
|
d->pcolor_center[d->data_items - 1] = NULL;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
p_realloc(&(d->pcolor_end), d->data_items);
|
|
|
|
d->pcolor_end[d->data_items - 1] = NULL;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
p_realloc(&(d->vertical_gradient), d->data_items);
|
|
|
|
d->vertical_gradient[d->data_items - 1] = true;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
p_realloc(&(d->draw_style), d->data_items);
|
|
|
|
d->draw_style[d->data_items - 1] = Bottom_Style;
|
2008-05-31 14:38:27 +02:00
|
|
|
}
|
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
static int
|
2008-06-02 12:18:17 +02:00
|
|
|
graph_draw(draw_context_t *ctx,
|
|
|
|
int screen __attribute__ ((unused)),
|
|
|
|
widget_node_t *w,
|
|
|
|
int width, int height, int offset,
|
|
|
|
int used __attribute__ ((unused)),
|
|
|
|
void *p __attribute__ ((unused)))
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
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-05-20 15:39:47 +02:00
|
|
|
Data *d = w->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-06-02 12:18:17 +02:00
|
|
|
w->area.x = widget_calculate_offset(width,
|
2008-05-20 15:39:47 +02:00
|
|
|
d->width, offset,
|
|
|
|
w->widget->align);
|
|
|
|
w->area.y = 0;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
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))
|
2008-06-02 12:18:17 +02:00
|
|
|
d->box_height = (int) (height * d->height + 0.5) - 2;
|
2008-01-09 09:59:42 +01:00
|
|
|
|
2008-06-02 12:18:17 +02:00
|
|
|
margin_top = (int)((height - (d->box_height + 2)) / 2 + 0.5) + w->area.y;
|
2008-03-12 15:19:50 +01:00
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* draw background */
|
2008-05-20 15:39:47 +02:00
|
|
|
rectangle.x = w->area.x + 1;
|
2008-04-18 00:58:42 +02:00
|
|
|
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-06-03 04:51:48 +02:00
|
|
|
pattern_area.y = rectangle.y - rectangle.height;
|
|
|
|
|
|
|
|
/* draw style = top */
|
|
|
|
for(z = 0; z < d->data_items; z++)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->draw_style[z] != Top_Style)
|
|
|
|
continue;
|
2008-04-14 03:38:59 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->vertical_gradient[z])
|
2008-03-09 23:49:03 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
pattern_area.width = 0;
|
|
|
|
pattern_area.height = rectangle.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern_area.height = 0;
|
2008-04-18 01:30:12 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.width = -rectangle.width;
|
|
|
|
else
|
|
|
|
pattern_area.width = rectangle.width;
|
|
|
|
}
|
2008-04-18 01:30:12 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
cur_index = d->index[z];
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02: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
|
|
|
|
* on this index (draw_from) - might be 0 (then draw from start) */
|
|
|
|
for(tmp = 0, x = 0; x < d->data_items; x++)
|
2008-03-09 23:49:03 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->draw_style[x] != Top_Style)
|
|
|
|
continue;
|
|
|
|
if (x == z) /* no need to compare with itself */
|
|
|
|
continue;
|
2008-03-09 23:49:03 +01:00
|
|
|
|
2008-06-03 04:51:48 +02: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->index[x] - d->index[z]);
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
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;
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
/* ... (test_)index to test for a smaller value found. */
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
/* if such a smaller value (to not overdraw!) is there, store into 'tmp' */
|
|
|
|
if(d->lines[x][test_index] > tmp && d->lines[x][test_index] < d->lines[z][cur_index])
|
|
|
|
tmp = d->lines[x][test_index];
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-06-03 04:51:48 +02: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->lines[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-06-03 04:51:48 +02:00
|
|
|
draw_graph(ctx, rectangle , d->draw_from, d->draw_to, d->index[z], d->grow, pattern_area,
|
|
|
|
&(d->color_start[z]), d->pcolor_center[z], d->pcolor_end[z]);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
2008-03-09 23:49:03 +01:00
|
|
|
pattern_area.y = rectangle.y;
|
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
/* draw style = bottom */
|
|
|
|
for(z = 0; z < d->data_items; z++)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->draw_style[z] != Bottom_Style)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(d->vertical_gradient[z])
|
2008-03-09 23:49:03 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
pattern_area.width = 0;
|
|
|
|
pattern_area.height = -rectangle.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern_area.height = 0;
|
2008-04-18 01:30:12 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.width = -rectangle.width;
|
|
|
|
else
|
|
|
|
pattern_area.width = rectangle.width;
|
|
|
|
}
|
2008-04-18 01:30:12 +02:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
cur_index = d->index[z];
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
for(y = 0; y < d->size; y++)
|
|
|
|
{
|
|
|
|
for(tmp = 0, x = 0; x < d->data_items; x++)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->draw_style[x] != Bottom_Style)
|
|
|
|
continue;
|
|
|
|
if (x == z)
|
|
|
|
continue;
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
test_index = cur_index + (d->index[x] - d->index[z]);
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
if (test_index < 0)
|
|
|
|
test_index = d->size + test_index;
|
|
|
|
else if(test_index >= d->size)
|
|
|
|
test_index -= d->size;
|
2008-03-10 14:30:40 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->lines[x][test_index] > tmp && d->lines[x][test_index] < d->lines[z][cur_index])
|
|
|
|
tmp = d->lines[x][test_index];
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-06-03 04:51:48 +02: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-06-03 04:51:48 +02:00
|
|
|
draw_graph(ctx, rectangle, d->draw_from, d->lines[z], d->index[z], d->grow, pattern_area,
|
|
|
|
&(d->color_start[z]), d->pcolor_center[z], d->pcolor_end[z]);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
/* draw style = line */
|
|
|
|
for(z = 0; z < d->data_items; z++)
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
if(d->draw_style[z] != Line_Style)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(d->vertical_gradient[z])
|
2008-03-09 23:49:03 +01:00
|
|
|
{
|
2008-06-03 04:51:48 +02:00
|
|
|
pattern_area.width = 0;
|
|
|
|
pattern_area.height = -rectangle.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pattern_area.height = 0;
|
|
|
|
if(d->grow == Right)
|
|
|
|
pattern_area.width = -rectangle.width;
|
2008-04-18 01:30:12 +02:00
|
|
|
else
|
2008-06-03 04:51:48 +02:00
|
|
|
pattern_area.width = rectangle.width;
|
2008-03-09 23:49:03 +01:00
|
|
|
}
|
2008-06-03 04:51:48 +02:00
|
|
|
|
|
|
|
draw_graph_line(ctx, rectangle, d->lines[z], d->index[z], d->grow, pattern_area,
|
|
|
|
&(d->color_start[z]), d->pcolor_center[z], d->pcolor_end[z]);
|
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) */
|
2008-05-20 15:39:47 +02:00
|
|
|
rectangle.x = w->area.x;
|
2008-04-18 00:58:42 +02:00
|
|
|
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-05-20 15:39:47 +02:00
|
|
|
w->area.width = d->width;
|
2008-06-02 12:18:17 +02:00
|
|
|
w->area.height = height;
|
2008-05-20 15:39:47 +02:00
|
|
|
return w->area.width;
|
2008-01-06 18:23:56 +01:00
|
|
|
}
|
|
|
|
|
2008-03-04 19:06:04 +01:00
|
|
|
static widget_tell_status_t
|
2008-05-20 15:39:47 +02:00
|
|
|
graph_tell(widget_t *widget, const char *property, const char *new_value)
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
|
|
|
Data *d = widget->data;
|
2008-05-31 14:38:27 +02:00
|
|
|
int i, u, fi;
|
2008-02-17 08:49:37 +01:00
|
|
|
float value;
|
|
|
|
char *title, *setting;
|
2008-05-31 14:38:27 +02:00
|
|
|
char *new_val;
|
|
|
|
bool found;
|
2008-03-06 14:30:18 +01:00
|
|
|
|
2008-05-02 16:07:29 +02:00
|
|
|
if(!new_value)
|
2008-04-23 05:35:55 +02:00
|
|
|
return WIDGET_ERROR_NOVALUE;
|
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
/* following properties need a datasection */
|
|
|
|
else if(!a_strcmp(property, "data")
|
|
|
|
|| !a_strcmp(property, "fg")
|
|
|
|
|| !a_strcmp(property, "fg_center")
|
|
|
|
|| !a_strcmp(property, "fg_end")
|
|
|
|
|| !a_strcmp(property, "vertical_gradient")
|
|
|
|
|| !a_strcmp(property, "scale")
|
|
|
|
|| !a_strcmp(property, "max")
|
2008-06-03 04:51:48 +02:00
|
|
|
|| !a_strcmp(property, "draw_style"))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-05-31 14:38:27 +02:00
|
|
|
/* check if this section is defined already */
|
|
|
|
new_val = a_strdup(new_value);
|
|
|
|
title = strtok(new_val, " ");
|
2008-03-06 14:30:18 +01:00
|
|
|
if(!(setting = strtok(NULL, " ")))
|
2008-05-31 14:38:27 +02:00
|
|
|
{
|
|
|
|
p_delete(&new_val);
|
2008-03-06 14:30:18 +01:00
|
|
|
return WIDGET_ERROR_NOVALUE;
|
2008-05-31 14:38:27 +02:00
|
|
|
}
|
|
|
|
for(found = false, fi = 0; fi < d->data_items; fi++)
|
|
|
|
{
|
|
|
|
if(!a_strcmp(title, d->data_title[fi]))
|
2008-01-25 21:39:08 +01:00
|
|
|
{
|
2008-05-31 14:38:27 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no section found -> create one */
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
graph_data_add(d, title);
|
|
|
|
fi = d->data_items - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* change values accordingly... */
|
|
|
|
if(!a_strcmp(property, "data"))
|
|
|
|
{
|
|
|
|
/* assign incoming value */
|
|
|
|
value = MAX(atof(setting), 0);
|
|
|
|
|
|
|
|
if(++d->index[fi] >= d->size) /* cycle inside the array */
|
|
|
|
d->index[fi] = 0;
|
2008-02-17 08:49:37 +01:00
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
if(d->scale[fi]) /* scale option is true */
|
|
|
|
{
|
|
|
|
d->values[fi][d->index[fi]] = value;
|
2008-02-17 08:49:37 +01:00
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
if(value > d->current_max[fi]) /* a new maximum value found */
|
2008-02-17 08:49:37 +01:00
|
|
|
{
|
2008-05-31 14:38:27 +02:00
|
|
|
d->max_index[fi] = d->index[fi];
|
|
|
|
d->current_max[fi] = value;
|
|
|
|
|
|
|
|
/* recalculate */
|
|
|
|
for (u = 0; u < d->size; u++)
|
|
|
|
d->lines[fi][u] = (int) (d->values[fi][u] * (d->box_height) / d->current_max[fi] + 0.5);
|
2008-02-17 08:49:37 +01:00
|
|
|
}
|
2008-05-31 14:38:27 +02:00
|
|
|
/* old max_index reached + current_max > normal, re-check/generate */
|
|
|
|
else if(d->max_index[fi] == d->index[fi] && d->current_max[fi] > d->max[fi])
|
2008-02-17 08:49:37 +01:00
|
|
|
{
|
2008-05-31 14:38:27 +02:00
|
|
|
/* find the new max */
|
|
|
|
for(u = 0; u < d->size; u++)
|
|
|
|
if(d->values[fi][u] > d->values[fi][d->max_index[fi]])
|
|
|
|
d->max_index[fi] = u;
|
|
|
|
|
|
|
|
d->current_max[fi] = MAX(d->values[fi][d->max_index[fi]], d->max[fi]);
|
|
|
|
|
|
|
|
/* recalculate */
|
|
|
|
for(u = 0; u < d->size; u++)
|
|
|
|
d->lines[fi][u] = (int) (d->values[fi][u] * d->box_height / d->current_max[fi] + 0.5);
|
2008-02-17 08:49:37 +01:00
|
|
|
}
|
2008-05-31 14:38:27 +02:00
|
|
|
else
|
|
|
|
d->lines[fi][d->index[fi]] = (int) (value * d->box_height / d->current_max[fi] + 0.5);
|
2008-01-25 21:39:08 +01:00
|
|
|
}
|
2008-05-31 14:38:27 +02:00
|
|
|
else /* scale option is false - limit to d->box_height */
|
|
|
|
{
|
|
|
|
if (value < d->max[fi])
|
|
|
|
d->lines[fi][d->index[fi]] = (int) (value * d->box_height / d->max[fi] + 0.5);
|
|
|
|
else
|
|
|
|
d->lines[fi][d->index[fi]] = d->box_height;
|
|
|
|
}
|
|
|
|
p_delete(&new_val);
|
|
|
|
return WIDGET_NOERROR;
|
|
|
|
}
|
|
|
|
else if(!a_strcmp(property, "fg"))
|
2008-06-03 04:51:48 +02:00
|
|
|
xcolor_new(globalconf.connection, globalconf.default_screen, setting, &(d->color_start[fi]));
|
2008-05-31 14:38:27 +02:00
|
|
|
else if(!a_strcmp(property, "fg_center"))
|
2008-06-03 04:51:48 +02:00
|
|
|
graph_pcolor_set(&(d->pcolor_center[fi]), setting);
|
2008-05-31 14:38:27 +02:00
|
|
|
else if(!a_strcmp(property, "fg_end"))
|
2008-06-03 04:51:48 +02:00
|
|
|
graph_pcolor_set(&(d->pcolor_end[fi]), setting);
|
2008-05-31 14:38:27 +02:00
|
|
|
else if(!a_strcmp(property, "vertical_gradient"))
|
2008-06-03 04:51:48 +02:00
|
|
|
d->vertical_gradient[fi] = a_strtobool(setting);
|
2008-05-31 14:38:27 +02:00
|
|
|
else if(!a_strcmp(property, "scale"))
|
|
|
|
d->scale[fi] = a_strtobool(setting);
|
|
|
|
else if(!a_strcmp(property, "max"))
|
|
|
|
{
|
|
|
|
d->max[fi] = atof(setting);
|
|
|
|
d->current_max[fi] = d->max[fi];
|
|
|
|
}
|
2008-06-03 04:51:48 +02:00
|
|
|
else if(!a_strcmp(property, "draw_style"))
|
|
|
|
{
|
|
|
|
if(!a_strcmp(setting, "bottom"))
|
|
|
|
d->draw_style[fi] = Bottom_Style;
|
|
|
|
else if(!a_strcmp(setting, "line"))
|
|
|
|
d->draw_style[fi] = Line_Style;
|
|
|
|
else if(!a_strcmp(setting, "top"))
|
|
|
|
d->draw_style[fi] = Top_Style;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
warn("'error changing property %s of widget %s, must be 'bottom', 'top' or 'line'",
|
|
|
|
property, widget->name);
|
|
|
|
p_delete(&new_val);
|
|
|
|
return WIDGET_ERROR_CUSTOM;
|
|
|
|
}
|
|
|
|
}
|
2008-05-31 14:38:27 +02:00
|
|
|
p_delete(&new_val);
|
|
|
|
return WIDGET_NOERROR;
|
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-05-31 14:38:27 +02:00
|
|
|
else if(!a_strcmp(property, "width"))
|
|
|
|
{
|
|
|
|
d->width = atoi(new_value);
|
|
|
|
d->size = d->width - 2;
|
|
|
|
/* re-allocate/initialise necessary values */
|
|
|
|
for(i = 0; i < d->data_items; i++)
|
|
|
|
{
|
|
|
|
p_delete(&d->values[i]);
|
|
|
|
p_delete(&d->lines[i]);
|
|
|
|
d->values[i] = p_new(float, d->size);
|
|
|
|
d->lines[i] = p_new(int, d->size);
|
|
|
|
|
|
|
|
d->index[i] = 0;
|
|
|
|
d->current_max[i] = 0;
|
|
|
|
d->max_index[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-02-17 08:49:37 +01:00
|
|
|
else if(!a_strcmp(property, "bg"))
|
2008-03-06 14:30:18 +01:00
|
|
|
{
|
2008-05-30 12:34:38 +02:00
|
|
|
if(!xcolor_new(globalconf.connection,
|
2008-06-03 04:51:48 +02:00
|
|
|
globalconf.default_screen,
|
|
|
|
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-05-30 12:34:38 +02:00
|
|
|
if(!xcolor_new(globalconf.connection,
|
2008-06-03 04:51:48 +02:00
|
|
|
globalconf.default_screen,
|
|
|
|
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-05-23 22:53:59 +02:00
|
|
|
warn("error changing property %s of widget %s, must be 'left' or 'right'",
|
2008-03-18 03:15:50 +01:00
|
|
|
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-05-20 15:39:47 +02:00
|
|
|
graph_new(alignment_t align)
|
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-04-11 11:26:37 +02:00
|
|
|
w = p_new(widget_t, 1);
|
2008-05-20 15:39:47 +02:00
|
|
|
widget_common_new(w);
|
2008-01-06 18:23:56 +01:00
|
|
|
|
|
|
|
w->draw = graph_draw;
|
|
|
|
w->tell = graph_tell;
|
2008-05-20 15:39:47 +02:00
|
|
|
w->align = align;
|
2008-01-06 18:23:56 +01:00
|
|
|
d = w->data = p_new(Data, 1);
|
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
d->width = 80;
|
|
|
|
d->height = 0.80;
|
2008-03-31 14:31:43 +02:00
|
|
|
d->size = d->width - 2;
|
2008-05-31 14:38:27 +02:00
|
|
|
d->grow = Left;
|
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-05-31 14:38:27 +02:00
|
|
|
d->bg = globalconf.colors.bg;
|
|
|
|
d->bordercolor = globalconf.colors.fg;
|
2008-01-25 21:39:08 +01:00
|
|
|
|
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
|