direct calculating of the line length to draw

the graph_tell() does not modify directly the line-lenght to draw, rather
then calculating the value in percent and in the graph_draw, calculating
the line length ouf of that the percent.

(while trying to fix something little, a whole new scale thing resulted;
also I didn't really understand the old system, my fault that is)
This commit is contained in:
marco candrian 2008-01-09 09:59:42 +01:00 committed by Julien Danjou
parent bbc94b97f8
commit 4f05f954d0

View file

@ -37,13 +37,14 @@ typedef struct
XColor fg; /* Foreground color */
XColor bg; /* Background color */
XColor bordercolor; /* Border color */
int box_height; /* Height of the innerbox */
int *lines; /* keeps the calculated values (line-length); */
int lines_index; /* pointer to current val */
int lines_size; /* size of lines-array */
float *line_values; /* actual values */
int line_max_index; /* index of the current maximum value */
int line_max; /* maximum value */
int current_max; /* curent maximum value */
} Data;
@ -52,12 +53,9 @@ static int
graph_draw(Widget *widget, DrawCtx *ctx, int offset,
int used __attribute__ ((unused)))
{
int width, height; /* width/height (pixel) to draw the outer box */
int margin_top, left_offset;
Data *d = widget->data;
width = d->lines_size + 2;
if(!widget->user_supplied_x)
widget->area.x = widget_calculate_offset(widget->statusbar->width,
d->width,
@ -67,27 +65,27 @@ graph_draw(Widget *widget, DrawCtx *ctx, int offset,
widget->area.y = 0;
margin_top = (int) (widget->statusbar->height * (1 - d->height)) / 2 + 0.5 + widget->area.y;
height = (int) (widget->statusbar->height * d->height + 0.5);
left_offset = widget->area.x + d->padding_left;
if(!(d->box_height))
d->box_height = (int) (widget->statusbar->height * d->height + 0.5) - 2;
draw_rectangle(ctx,
left_offset, margin_top,
width, height,
d->lines_size + 2, d->box_height + 2,
False, d->bordercolor);
draw_rectangle(ctx,
left_offset + 1, margin_top + 1,
width - 2, height - 2,
d->lines_size, d->box_height,
True, d->bg);
/* computes line-length to draw and store into lines[] */
d->lines[d->lines_index] = (int) (d->percent) * (height - 2 ) / 100 + 0.5;
if(d->lines[d->lines_index] < 0)
d->lines[d->lines_index] = 0;
draw_graph(ctx,
left_offset + 2, margin_top + height - 1,
left_offset + 2, margin_top + d->box_height + 1,
d->lines_size, d->lines, d->lines_index,
d->fg);
@ -100,9 +98,7 @@ static void
graph_tell(Widget *widget, char *command)
{
Data *d = widget->data;
int percent, i, height, omax, nmax;
height = (int) (widget->statusbar->height * d->height + 0.5);
int value, i;
if(!command || d->width < 1)
return;
@ -110,50 +106,44 @@ graph_tell(Widget *widget, char *command)
if(++d->lines_index >= d->lines_size) /* cycle inside the array */
d->lines_index = 0;
if (d->line_values && d->line_max_index == d->lines_index &&
d->line_values[d->line_max_index]) {
omax = d->line_values[d->line_max_index];
d->line_values[d->line_max_index] = 0;
for (i = 0; i < d->lines_size; i++)
if (d->line_values[i] > d->line_values[d->line_max_index]) {
d->line_max_index = i;
}
nmax = MAX(d->line_values[d->line_max_index], d->line_max);
if (d->line_values[d->line_max_index]) {
for (i = 0; i < d->lines_size; i++) {
d->lines[i] = (int) (100*d->line_values[i] / nmax) * (height -
2) / 100 + 0.5;
}
}
}
value = MAX(atoi(command), 0); /*TODO atofloat */
percent = atoi(command);
if (percent < 0)
if(d->line_values) /* scale option = true */
{
if (d->line_values)
d->line_values[d->lines_index] = 0;
d->percent = 0;
}
else if (d->line_values)
{
d->line_values[d->lines_index] = percent;
nmax = MAX(d->line_max, d->line_values[d->line_max_index]);
if (d->line_values[d->line_max_index] < percent) {
// Increase the current max
d->line_max_index = d->lines_index;
if (d->line_max < d->line_values[d->line_max_index]) {
for (i = 0; i < d->lines_size; i++) {
d->lines[i] = (int)
(100*d->line_values[i]/d->line_values[d->line_max_index])
* (height - 2) / 100 + 0.5;
}
}
d->line_values[d->lines_index] = value;
if(value > d->current_max) /* a new maximum value found */
{
d->line_max_index = d->lines_index;
d->current_max = MAX(value, 1);
/* recalculate: value * (height-2pixel) / max */
for (i = 0; i < d->lines_size; i++)
d->lines[i] = (int) ((float)(d->line_values[i]) * (d->box_height) / d->current_max + 0.5);
}
d->percent = (int) (100.0 * d->line_values[d->lines_index] / nmax);
else if(d->line_max_index == d->lines_index) /* old max_index reached, re-check/generate */
{
/* find the new max */
for (i = 0; i < d->lines_size; i++)
if (d->line_values[i] > d->line_values[d->line_max_index])
d->line_max_index = i;
d->current_max = MAX(d->line_values[d->line_max_index], d->line_max);
/* recalculate */
for (i = 0; i < d->lines_size; i++)
d->lines[i] = (int) ((float)(d->line_values[i]) * d->box_height / d->current_max + 0.5);
}
else
d->lines[d->lines_index] = (int) ((float)(value) * d->box_height / d->current_max + 0.5);
}
else {
d->percent = (percent > d->line_max ? 100 :
(int) (100 * ((float)percent) / d->line_max));
else /* scale option = false */
{
if (value < d->current_max)
d->lines[d->lines_index] = (int) ((float)(value) * d->box_height / d->current_max + 0.5);
else
d->lines[d->lines_index] = d->box_height;
}
}
@ -172,12 +162,10 @@ graph_new(Statusbar *statusbar, cfg_t *config)
w->tell = graph_tell;
d = w->data = p_new(Data, 1);
d->height = cfg_getfloat(config, "height");
d->width = cfg_getint(config, "width");
d->height = cfg_getfloat(config, "height");
d->padding_left = cfg_getint(config, "padding_left");
d->lines_size = d->width - d->padding_left - 2;
d->lines_index = -1; /* graph_tell will increment it to 0 (to begin with...) */
if(d->lines_size < 1)
{
@ -186,10 +174,11 @@ graph_new(Statusbar *statusbar, cfg_t *config)
}
d->lines = p_new(int, d->lines_size);
if (cfg_getbool(config, "scale"))
d->line_values = p_new(float, d->lines_size);
d->line_max = cfg_getfloat(config, "max");
d->line_max = MAX(cfg_getfloat(config, "max"), 0.001); /* prevent div / 0 */
d->current_max = d->line_max;
if((color = cfg_getstr(config, "fg")))
d->fg = initxcolor(phys_screen, color);
else