widgets: use vector_t for the color-gradients course.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
marco candrian 2008-07-02 12:25:52 +02:00 committed by Julien Danjou
parent 3074c3fdf4
commit 2b767c2352
4 changed files with 87 additions and 71 deletions

View File

@ -415,14 +415,14 @@ draw_text(draw_context_t *ctx, font_t *font,
/** Setup color-source for cairo (gradient or mono). /** Setup color-source for cairo (gradient or mono).
* \param ctx Draw context. * \param ctx Draw context.
* \param rect x, y to x + x_offset, y + y_offset. * \param gradient_vector x, y to x + x_offset, y + y_offset.
* \param pcolor Color to use at start (x, y). * \param pcolor Color to use at start of gradient_vector.
* \param pcolor_center Color at 50% of width. * \param pcolor_center Color at center of gradient_vector.
* \param pcolor_end Color at pattern end (x + x_offset, y + y_offset). * \param pcolor_end Color at end of gradient_vector.
* \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed. * \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
*/ */
static cairo_pattern_t * static cairo_pattern_t *
draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect, draw_setup_cairo_color_source(draw_context_t *ctx, vector_t gradient_vector,
const xcolor_t *pcolor, const xcolor_t *pcolor_center, const xcolor_t *pcolor, const xcolor_t *pcolor_center,
const xcolor_t *pcolor_end) const xcolor_t *pcolor_end)
{ {
@ -439,7 +439,10 @@ draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect,
pcolor->alpha / 65535.0); pcolor->alpha / 65535.0);
else else
{ {
pat = cairo_pattern_create_linear(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); pat = cairo_pattern_create_linear(gradient_vector.x,
gradient_vector.y,
gradient_vector.x + gradient_vector.x_offset,
gradient_vector.y + gradient_vector.y_offset);
/* pcolor is always set (so far in awesome) */ /* pcolor is always set (so far in awesome) */
cairo_pattern_add_color_stop_rgba(pat, 0.0, cairo_pattern_add_color_stop_rgba(pat, 0.0,
@ -507,18 +510,18 @@ draw_rectangle(draw_context_t *ctx, area_t geometry,
} }
/** Draw rectangle with gradient colors /** Draw rectangle with gradient colors
* \param ctx Draw context * \param ctx Draw context.
* \param geometry geometry * \param geometry Geometry.
* \param line_width line width * \param line_width Line width.
* \param filled filled rectangle? * \param filled Filled rectangle?
* \param pattern_rect pattern geometry * \param gradient_vector Color-gradient course.
* \param pcolor color to use at start * \param pcolor Color at start of gradient_vector.
* \param pcolor_center color at 50% of width * \param pcolor_center Color in the center.
* \param pcolor_end color at pattern_start + pattern_width * \param pcolor_end Color at end of gradient_vector.
*/ */
void void
draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled, draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width, bool filled,
area_t pattern_rect, const xcolor_t *pcolor, vector_t gradient_vector, const xcolor_t *pcolor,
const xcolor_t *pcolor_center, const xcolor_t *pcolor_end) const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
{ {
cairo_pattern_t *pat; cairo_pattern_t *pat;
@ -528,7 +531,7 @@ draw_rectangle_gradient(draw_context_t *ctx, area_t geometry, float line_width,
cairo_set_miter_limit(ctx->cr, 10.0); cairo_set_miter_limit(ctx->cr, 10.0);
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER); cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
pat = draw_setup_cairo_color_source(ctx, pattern_rect, pcolor, pcolor_center, pcolor_end); pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
if(filled) if(filled)
{ {
@ -566,21 +569,21 @@ draw_graph_setup(draw_context_t *ctx)
* \param to Array of end-point offsets to draw a graph lines. * \param to Array of end-point offsets to draw a graph lines.
* \param cur_index Current position in data-array (cycles around). * \param cur_index Current position in data-array (cycles around).
* \param grow Put new values to the left or to the right. * \param grow Put new values to the left or to the right.
* \param patt_rect Pattern geometry. * \param gradient_vector Color-Gradient course.
* \param pcolor Color at the left. * \param pcolor Color at start of gradient_vector.
* \param pcolor_center Color in the center. * \param pcolor_center Color in the center.
* \param pcolor_end Color at the right. * \param pcolor_end Color at end of gradient_vector.
*/ */
void void
draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index, draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
position_t grow, area_t patt_rect, const xcolor_t *pcolor, position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
const xcolor_t *pcolor_center, const xcolor_t *pcolor_end) const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
{ {
int i = -1; int i = -1;
float x = rect.x + 0.5; /* middle of a pixel */ float x = rect.x + 0.5; /* middle of a pixel */
cairo_pattern_t *pat; cairo_pattern_t *pat;
pat = draw_setup_cairo_color_source(ctx, patt_rect, pat = draw_setup_cairo_color_source(ctx, gradient_vector,
pcolor, pcolor_center, pcolor_end); pcolor, pcolor_center, pcolor_end);
if(grow == Right) /* draw from right to left */ if(grow == Right) /* draw from right to left */
@ -619,14 +622,14 @@ draw_graph(draw_context_t *ctx, area_t rect, int *from, int *to, int cur_index,
* \param to array of offsets to draw the line through... * \param to array of offsets to draw the line through...
* \param cur_index current position in data-array (cycles around) * \param cur_index current position in data-array (cycles around)
* \param grow put new values to the left or to the right * \param grow put new values to the left or to the right
* \param patt_rect Pattern geometry. * \param gradient_vector Color-gradient course.
* \param pcolor color at the left * \param pcolor Color at start of gradient_vector.
* \param pcolor_center color in the center * \param pcolor_center Color in the center.
* \param pcolor_end color at the right * \param pcolor_end Color at end of gradient_vector.
*/ */
void void
draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index, draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
position_t grow, area_t patt_rect, const xcolor_t *pcolor, position_t grow, vector_t gradient_vector, const xcolor_t *pcolor,
const xcolor_t *pcolor_center, const xcolor_t *pcolor_end) const xcolor_t *pcolor_center, const xcolor_t *pcolor_end)
{ {
int i, w; int i, w;
@ -641,7 +644,7 @@ draw_graph_line(draw_context_t *ctx, area_t rect, int *to, int cur_index,
/* a nicer, better visible line compared to 1.0 */ /* a nicer, better visible line compared to 1.0 */
cairo_set_line_width(ctx->cr, 1.25); cairo_set_line_width(ctx->cr, 1.25);
pat = draw_setup_cairo_color_source(ctx, patt_rect, pcolor, pcolor_center, pcolor_end); pat = draw_setup_cairo_color_source(ctx, gradient_vector, pcolor, pcolor_center, pcolor_end);
/* path through the centers of pixels */ /* path through the centers of pixels */
x = rect.x + 0.5; x = rect.x + 0.5;

View File

@ -50,6 +50,17 @@ typedef enum
AlignAuto AlignAuto
} alignment_t; } alignment_t;
typedef struct vector_t vector_t;
struct vector_t
{
/** Co-ords of starting point */
int16_t x;
int16_t y;
/** Offset to starting point */
int16_t x_offset;
int16_t y_offset;
};
typedef struct area_t area_t; typedef struct area_t area_t;
struct area_t struct area_t
{ {
@ -185,13 +196,13 @@ void draw_parser_data_wipe(draw_parser_data_t *);
void draw_text(draw_context_t *, font_t *, area_t, const char *, draw_parser_data_t *); void draw_text(draw_context_t *, font_t *, area_t, const char *, draw_parser_data_t *);
void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *); void draw_rectangle(draw_context_t *, area_t, float, bool, const xcolor_t *);
void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, area_t, void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *); const xcolor_t *, const xcolor_t *, const xcolor_t *);
void draw_graph_setup(draw_context_t *); void draw_graph_setup(draw_context_t *);
void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, area_t, void draw_graph(draw_context_t *, area_t, int *, int *, int, position_t, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *); const xcolor_t *, const xcolor_t *, const xcolor_t *);
void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, area_t, void draw_graph_line(draw_context_t *, area_t, int *, int, position_t, vector_t,
const xcolor_t *, const xcolor_t *, const xcolor_t *); const xcolor_t *, const xcolor_t *, const xcolor_t *);
void draw_circle(draw_context_t *, int, int, int, bool, const xcolor_t *); void draw_circle(draw_context_t *, int, int, int, bool, const xcolor_t *);
draw_image_t *draw_image_new(const char *); draw_image_t *draw_image_new(const char *);

View File

@ -148,7 +148,8 @@ graph_draw(draw_context_t *ctx,
{ {
int margin_top, y; int margin_top, y;
graph_data_t *d = w->widget->data; graph_data_t *d = w->widget->data;
area_t rectangle, pattern_area; area_t rectangle;
vector_t color_gradient;
plot_t *plot; plot_t *plot;
if(!d->plots) if(!d->plots)
@ -180,28 +181,28 @@ graph_draw(draw_context_t *ctx,
/* gradient begin either left or on the right of the rectangle */ /* gradient begin either left or on the right of the rectangle */
if(d->grow == Right) if(d->grow == Right)
pattern_area.x = rectangle.x + rectangle.width; color_gradient.x = rectangle.x + rectangle.width;
else else
pattern_area.x = rectangle.x; color_gradient.x = rectangle.x;
for(plot = d->plots; plot; plot = plot->next) for(plot = d->plots; plot; plot = plot->next)
switch(plot->draw_style) switch(plot->draw_style)
{ {
case Top_Style: case Top_Style:
pattern_area.y = rectangle.y - rectangle.height; color_gradient.y = rectangle.y - rectangle.height;
if(plot->vertical_gradient) if(plot->vertical_gradient)
{ {
pattern_area.width = 0; color_gradient.x_offset = 0;
pattern_area.height = rectangle.height; color_gradient.y_offset = rectangle.height;
} }
else else
{ {
pattern_area.height = 0; color_gradient.y_offset = 0;
if(d->grow == Right) if(d->grow == Right)
pattern_area.width = - rectangle.width; color_gradient.x_offset = - rectangle.width;
else else
pattern_area.width = rectangle.width; color_gradient.x_offset = rectangle.width;
} }
for(y = 0; y < d->size; y++) for(y = 0; y < d->size; y++)
@ -210,47 +211,47 @@ graph_draw(draw_context_t *ctx,
d->draw_from[y] = d->box_height; /* i.e. no smaller value -> from top of box */ d->draw_from[y] = d->box_height; /* i.e. no smaller value -> from top of box */
d->draw_to[y] = d->box_height - plot->lines[y]; /* i.e. on full plot -> 0 = bottom */ d->draw_to[y] = d->box_height - plot->lines[y]; /* i.e. on full plot -> 0 = bottom */
} }
draw_graph(ctx, rectangle , d->draw_from, d->draw_to, plot->index, d->grow, pattern_area, draw_graph(ctx, rectangle , d->draw_from, d->draw_to, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end); &plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
break; break;
case Bottom_Style: case Bottom_Style:
pattern_area.y = rectangle.y; color_gradient.y = rectangle.y;
if(plot->vertical_gradient) if(plot->vertical_gradient)
{ {
pattern_area.width = 0; color_gradient.x_offset = 0;
pattern_area.height = - rectangle.height; color_gradient.y_offset = - rectangle.height;
} }
else else
{ {
pattern_area.height = 0; color_gradient.y_offset = 0;
if(d->grow == Right) if(d->grow == Right)
pattern_area.width = - rectangle.width; color_gradient.x_offset = - rectangle.width;
else else
pattern_area.width = rectangle.width; color_gradient.x_offset = rectangle.width;
} }
p_clear(d->draw_from, d->size); p_clear(d->draw_from, d->size);
draw_graph(ctx, rectangle, d->draw_from, plot->lines, plot->index, d->grow, pattern_area, draw_graph(ctx, rectangle, d->draw_from, plot->lines, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end); &plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
break; break;
case Line_Style: case Line_Style:
pattern_area.y = rectangle.y; color_gradient.y = rectangle.y;
if(plot->vertical_gradient) if(plot->vertical_gradient)
{ {
pattern_area.width = 0; color_gradient.x_offset = 0;
pattern_area.height = -rectangle.height; color_gradient.y_offset = -rectangle.height;
} }
else else
{ {
pattern_area.height = 0; color_gradient.y_offset = 0;
if(d->grow == Right) if(d->grow == Right)
pattern_area.width = - rectangle.width; color_gradient.x_offset = - rectangle.width;
else else
pattern_area.width = rectangle.width; color_gradient.x_offset = rectangle.width;
} }
draw_graph_line(ctx, rectangle, plot->lines, plot->index, d->grow, pattern_area, draw_graph_line(ctx, rectangle, plot->lines, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end); &plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
break; break;
} }

View File

@ -133,7 +133,8 @@ progressbar_draw(draw_context_t *ctx,
/* pb_.. values points to the widget inside a potential border */ /* pb_.. values points to the widget inside a potential border */
int values_ticks, pb_x, pb_y, pb_height, pb_width, pb_progress, pb_offset; int values_ticks, pb_x, pb_y, pb_height, pb_width, pb_progress, pb_offset;
int unit = 0, nbbars = 0; /* tick + gap */ int unit = 0, nbbars = 0; /* tick + gap */
area_t rectangle, pattern_rect; area_t rectangle;
vector_t color_gradient;
progressbar_data_t *d = w->widget->data; progressbar_data_t *d = w->widget->data;
bar_t *bar; bar_t *bar;
@ -230,22 +231,22 @@ progressbar_draw(draw_context_t *ctx,
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color); draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
} }
pattern_rect.x = pb_x; color_gradient.x = pb_x;
pattern_rect.width = 0; color_gradient.x_offset = 0;
pattern_rect.y = pb_y; color_gradient.y = pb_y;
/* new value/progress in px + pattern setup */ /* new value/progress in px + pattern setup */
if(bar->reverse) if(bar->reverse)
{ {
/* invert: top with bottom part */ /* invert: top with bottom part */
pb_progress = pb_height - pb_progress; pb_progress = pb_height - pb_progress;
pattern_rect.height = pb_height; color_gradient.y_offset = pb_height;
} }
else else
{ {
/* bottom to top */ /* bottom to top */
pattern_rect.y += pb_height; color_gradient.y += pb_height;
pattern_rect.height = - pb_height; color_gradient.y_offset = - pb_height;
} }
/* bottom part */ /* bottom part */
@ -260,7 +261,7 @@ progressbar_draw(draw_context_t *ctx,
if(bar->reverse) if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
else else
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end); &bar->fg, &bar->fg_center, &bar->fg_end);
} }
@ -274,7 +275,7 @@ progressbar_draw(draw_context_t *ctx,
/* bg color */ /* bg color */
if(bar->reverse) if(bar->reverse)
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end); &bar->fg, &bar->fg_center, &bar->fg_end);
else else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
@ -330,21 +331,21 @@ progressbar_draw(draw_context_t *ctx,
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color); draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
} }
pattern_rect.y = pb_y; color_gradient.y = pb_y;
pattern_rect.height = 0; color_gradient.y_offset = 0;
pattern_rect.x = pb_x; color_gradient.x = pb_x;
/* new value/progress in px + pattern setup */ /* new value/progress in px + pattern setup */
if(bar->reverse) if(bar->reverse)
{ {
/* reverse: right to left */ /* reverse: right to left */
pb_progress = pb_width - pb_progress; pb_progress = pb_width - pb_progress;
pattern_rect.x += pb_width; color_gradient.x += pb_width;
pattern_rect.width = - pb_width; color_gradient.x_offset = - pb_width;
} }
else else
/* left to right */ /* left to right */
pattern_rect.width = pb_width; color_gradient.x_offset = pb_width;
/* left part */ /* left part */
if(pb_progress > 0) if(pb_progress > 0)
@ -358,7 +359,7 @@ progressbar_draw(draw_context_t *ctx,
if(bar->reverse) if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
else else
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end); &bar->fg, &bar->fg_center, &bar->fg_end);
} }
@ -372,7 +373,7 @@ progressbar_draw(draw_context_t *ctx,
/* bg color */ /* bg color */
if(bar->reverse) if(bar->reverse)
draw_rectangle_gradient(ctx, rectangle, 1.0, true, pattern_rect, draw_rectangle_gradient(ctx, rectangle, 1.0, true, color_gradient,
&bar->fg, &bar->fg_center, &bar->fg_end); &bar->fg, &bar->fg_center, &bar->fg_end);
else else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off); draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);