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).
* \param ctx Draw context.
* \param rect x, y to x + x_offset, y + y_offset.
* \param pcolor Color to use at start (x, y).
* \param pcolor_center Color at 50% of width.
* \param pcolor_end Color at pattern end (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 of gradient_vector.
* \param pcolor_center Color at center of gradient_vector.
* \param pcolor_end Color at end of gradient_vector.
* \return pat Pattern or NULL, needs to get cairo_pattern_destroy()'ed.
*/
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_end)
{
@ -439,7 +439,10 @@ draw_setup_cairo_color_source(draw_context_t *ctx, area_t rect,
pcolor->alpha / 65535.0);
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) */
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
* \param ctx Draw context
* \param geometry geometry
* \param line_width line width
* \param filled filled rectangle?
* \param pattern_rect pattern geometry
* \param pcolor color to use at start
* \param pcolor_center color at 50% of width
* \param pcolor_end color at pattern_start + pattern_width
* \param ctx Draw context.
* \param geometry Geometry.
* \param line_width Line width.
* \param filled Filled rectangle?
* \param gradient_vector Color-gradient course.
* \param pcolor Color at start of gradient_vector.
* \param pcolor_center Color in the center.
* \param pcolor_end Color at end of gradient_vector.
*/
void
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)
{
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_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)
{
@ -566,21 +569,21 @@ draw_graph_setup(draw_context_t *ctx)
* \param to Array of end-point offsets to draw a graph lines.
* \param cur_index Current position in data-array (cycles around).
* \param grow Put new values to the left or to the right.
* \param patt_rect Pattern geometry.
* \param pcolor Color at the left.
* \param gradient_vector Color-Gradient course.
* \param pcolor Color at start of gradient_vector.
* \param pcolor_center Color in the center.
* \param pcolor_end Color at the right.
* \param pcolor_end Color at end of gradient_vector.
*/
void
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)
{
int i = -1;
float x = rect.x + 0.5; /* middle of a pixel */
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);
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 cur_index current position in data-array (cycles around)
* \param grow put new values to the left or to the right
* \param patt_rect Pattern geometry.
* \param pcolor color at the left
* \param pcolor_center color in the center
* \param pcolor_end color at the right
* \param gradient_vector Color-gradient course.
* \param pcolor Color at start of gradient_vector.
* \param pcolor_center Color in the center.
* \param pcolor_end Color at end of gradient_vector.
*/
void
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)
{
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 */
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 */
x = rect.x + 0.5;

View File

@ -50,6 +50,17 @@ typedef enum
AlignAuto
} 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;
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_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 *);
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 *);
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 *);
void draw_circle(draw_context_t *, int, int, int, bool, const xcolor_t *);
draw_image_t *draw_image_new(const char *);

View File

@ -148,7 +148,8 @@ graph_draw(draw_context_t *ctx,
{
int margin_top, y;
graph_data_t *d = w->widget->data;
area_t rectangle, pattern_area;
area_t rectangle;
vector_t color_gradient;
plot_t *plot;
if(!d->plots)
@ -180,28 +181,28 @@ graph_draw(draw_context_t *ctx,
/* gradient begin either left or on the right of the rectangle */
if(d->grow == Right)
pattern_area.x = rectangle.x + rectangle.width;
color_gradient.x = rectangle.x + rectangle.width;
else
pattern_area.x = rectangle.x;
color_gradient.x = rectangle.x;
for(plot = d->plots; plot; plot = plot->next)
switch(plot->draw_style)
{
case Top_Style:
pattern_area.y = rectangle.y - rectangle.height;
color_gradient.y = rectangle.y - rectangle.height;
if(plot->vertical_gradient)
{
pattern_area.width = 0;
pattern_area.height = rectangle.height;
color_gradient.x_offset = 0;
color_gradient.y_offset = rectangle.height;
}
else
{
pattern_area.height = 0;
color_gradient.y_offset = 0;
if(d->grow == Right)
pattern_area.width = - rectangle.width;
color_gradient.x_offset = - rectangle.width;
else
pattern_area.width = rectangle.width;
color_gradient.x_offset = rectangle.width;
}
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_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);
break;
case Bottom_Style:
pattern_area.y = rectangle.y;
color_gradient.y = rectangle.y;
if(plot->vertical_gradient)
{
pattern_area.width = 0;
pattern_area.height = - rectangle.height;
color_gradient.x_offset = 0;
color_gradient.y_offset = - rectangle.height;
}
else
{
pattern_area.height = 0;
color_gradient.y_offset = 0;
if(d->grow == Right)
pattern_area.width = - rectangle.width;
color_gradient.x_offset = - rectangle.width;
else
pattern_area.width = rectangle.width;
color_gradient.x_offset = rectangle.width;
}
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);
break;
case Line_Style:
pattern_area.y = rectangle.y;
color_gradient.y = rectangle.y;
if(plot->vertical_gradient)
{
pattern_area.width = 0;
pattern_area.height = -rectangle.height;
color_gradient.x_offset = 0;
color_gradient.y_offset = -rectangle.height;
}
else
{
pattern_area.height = 0;
color_gradient.y_offset = 0;
if(d->grow == Right)
pattern_area.width = - rectangle.width;
color_gradient.x_offset = - rectangle.width;
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);
break;
}

View File

@ -133,7 +133,8 @@ progressbar_draw(draw_context_t *ctx,
/* 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 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;
bar_t *bar;
@ -230,22 +231,22 @@ progressbar_draw(draw_context_t *ctx,
draw_rectangle(ctx, rectangle, d->border_width, false, &bar->border_color);
}
pattern_rect.x = pb_x;
pattern_rect.width = 0;
pattern_rect.y = pb_y;
color_gradient.x = pb_x;
color_gradient.x_offset = 0;
color_gradient.y = pb_y;
/* new value/progress in px + pattern setup */
if(bar->reverse)
{
/* invert: top with bottom part */
pb_progress = pb_height - pb_progress;
pattern_rect.height = pb_height;
color_gradient.y_offset = pb_height;
}
else
{
/* bottom to top */
pattern_rect.y += pb_height;
pattern_rect.height = - pb_height;
color_gradient.y += pb_height;
color_gradient.y_offset = - pb_height;
}
/* bottom part */
@ -260,7 +261,7 @@ progressbar_draw(draw_context_t *ctx,
if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
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);
}
@ -274,7 +275,7 @@ progressbar_draw(draw_context_t *ctx,
/* bg color */
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);
else
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);
}
pattern_rect.y = pb_y;
pattern_rect.height = 0;
pattern_rect.x = pb_x;
color_gradient.y = pb_y;
color_gradient.y_offset = 0;
color_gradient.x = pb_x;
/* new value/progress in px + pattern setup */
if(bar->reverse)
{
/* reverse: right to left */
pb_progress = pb_width - pb_progress;
pattern_rect.x += pb_width;
pattern_rect.width = - pb_width;
color_gradient.x += pb_width;
color_gradient.x_offset = - pb_width;
}
else
/* left to right */
pattern_rect.width = pb_width;
color_gradient.x_offset = pb_width;
/* left part */
if(pb_progress > 0)
@ -358,7 +359,7 @@ progressbar_draw(draw_context_t *ctx,
if(bar->reverse)
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);
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);
}
@ -372,7 +373,7 @@ progressbar_draw(draw_context_t *ctx,
/* bg color */
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);
else
draw_rectangle(ctx, rectangle, 1.0, true, &bar->fg_off);