[widgets] Make draw functions statusbar-less dependents

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-02 12:18:17 +02:00
parent 930997898b
commit d8a5b43f26
12 changed files with 121 additions and 94 deletions

View File

@ -113,10 +113,14 @@ draw_screen_default_visual(xcb_screen_t *s)
* \param width width
* \param height height
* \param dw Drawable object to store in draw_context_t
* \param fg Foreground color.
* \param bg Background color.
* \return draw context ref
*/
draw_context_t *
draw_context_new(xcb_connection_t *conn, int phys_screen, int width, int height, xcb_drawable_t dw)
draw_context_new(xcb_connection_t *conn, int phys_screen,
int width, int height, xcb_drawable_t dw,
xcolor_t fg, xcolor_t bg)
{
draw_context_t *d = p_new(draw_context_t, 1);
xcb_screen_t *s = xcb_aux_get_screen(conn, phys_screen);
@ -131,6 +135,8 @@ draw_context_new(xcb_connection_t *conn, int phys_screen, int width, int height,
d->surface = cairo_xcb_surface_create(conn, dw, d->visual, width, height);
d->cr = cairo_create(d->surface);
d->layout = pango_cairo_create_layout(d->cr);
d->fg = fg;
d->bg = bg;
return d;
};
@ -286,7 +292,7 @@ draw_text_markup_expand(draw_parser_data_t *data,
*/
void
draw_text(draw_context_t *ctx, font_t *font,
xcolor_t *fg, area_t area, const char *text)
area_t area, const char *text)
{
int x, y;
ssize_t len, olen;
@ -361,10 +367,10 @@ draw_text(draw_context_t *ctx, font_t *font,
cairo_move_to(ctx->cr, x, y);
cairo_set_source_rgba(ctx->cr,
fg->red / 65535.0,
fg->green / 65535.0,
fg->blue / 65535.0,
fg->alpha / 65535.0);
ctx->fg.red / 65535.0,
ctx->fg.green / 65535.0,
ctx->fg.blue / 65535.0,
ctx->fg.alpha / 65535.0);
pango_cairo_update_layout(ctx->cr, ctx->layout);
pango_cairo_show_layout(ctx->cr, ctx->layout);

View File

@ -106,9 +106,11 @@ typedef struct
cairo_t *cr;
cairo_surface_t *surface;
PangoLayout *layout;
xcolor_t fg;
xcolor_t bg;
} draw_context_t;
draw_context_t *draw_context_new(xcb_connection_t *, int, int, int, xcb_drawable_t);
draw_context_t *draw_context_new(xcb_connection_t *, int, int, int, xcb_drawable_t, xcolor_t, xcolor_t);
/** Delete a draw context
* \param ctx draw_context_t to delete
*/
@ -130,7 +132,7 @@ draw_context_delete(draw_context_t **ctx)
font_t *draw_font_new(xcb_connection_t *, int, const char *);
void draw_font_delete(font_t **);
void draw_text(draw_context_t *, font_t *, xcolor_t *, area_t, const char *);
void draw_text(draw_context_t *, font_t *, area_t, const char *);
void draw_rectangle(draw_context_t *, area_t, float, bool, xcolor_t);
void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, area_t, xcolor_t *, xcolor_t *, xcolor_t *);

View File

@ -145,7 +145,7 @@ mouse_resizebar_draw(draw_context_t *ctx,
snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
geometry.x, geometry.y, geometry.width, geometry.height);
draw_rectangle(ctx, draw_geometry, 1.0, true, globalconf.colors.bg);
draw_text(ctx, globalconf.font, &globalconf.colors.fg, draw_geometry, size);
draw_text(ctx, globalconf.font, draw_geometry, size);
simplewindow_move(sw,
geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
@ -180,7 +180,9 @@ mouse_resizebar_new(int phys_screen, int border, area_t geometry,
*ctx = draw_context_new(globalconf.connection, sw->phys_screen,
sw->geometry.width, sw->geometry.height,
sw->drawable);
sw->drawable,
globalconf.colors.fg,
globalconf.colors.bg);
xcb_map_window(globalconf.connection, sw->window);
mouse_resizebar_draw(*ctx, sw, geometry, border);

View File

@ -119,16 +119,16 @@ statusbar_draw(statusbar_t *statusbar)
for(w = statusbar->widgets; w; w = w->next)
if(w->widget->isvisible && w->widget->align == AlignLeft)
left += w->widget->draw(w, statusbar, left, (left + right));
left += w->widget->draw(statusbar->ctx, statusbar->screen, w, statusbar->width, statusbar->height, left, (left + right), statusbar);
/* renders right widget from last to first */
for(w = *widget_node_list_last(&statusbar->widgets); w; w = w->prev)
if(w->widget->isvisible && w->widget->align == AlignRight)
right += w->widget->draw(w, statusbar, right, (left + right));
right += w->widget->draw(statusbar->ctx, statusbar->screen, w, statusbar->width, statusbar->height, right, (left + right), statusbar);
for(w = statusbar->widgets; w; w = w->next)
if(w->widget->isvisible && w->widget->align == AlignFlex)
left += w->widget->draw(w, statusbar, left, (left + right));
left += w->widget->draw(statusbar->ctx, statusbar->screen, w, statusbar->width, statusbar->height, left, (left + right), statusbar);
switch(statusbar->position)
{
@ -280,7 +280,9 @@ statusbar_position_update(statusbar_t *statusbar, position_t position)
statusbar->phys_screen,
statusbar->width,
statusbar->height,
dw);
dw,
statusbar->colors.fg,
statusbar->colors.bg);
break;
default:
if(statusbar->width <= 0)
@ -292,7 +294,9 @@ statusbar_position_update(statusbar_t *statusbar, position_t position)
statusbar->phys_screen,
statusbar->width,
statusbar->height,
statusbar->sw->drawable);
statusbar->sw->drawable,
statusbar->colors.fg,
statusbar->colors.bg);
break;
}

View File

@ -136,7 +136,7 @@ struct widget_t
/** widget_t name */
char *name;
/** Draw function */
int (*draw)(widget_node_t *, statusbar_t *, int, int);
int (*draw)(draw_context_t *, int, widget_node_t *, int, int, int, int, void *);
/** Update function */
widget_tell_status_t (*tell)(widget_t *, const char *, const char *);
/** ButtonPressedEvent handler */

View File

@ -86,7 +86,9 @@ titlebar_draw(client_t *c)
ctx = draw_context_new(globalconf.connection, c->titlebar_sw->phys_screen,
c->titlebar_sw->geometry.height,
c->titlebar_sw->geometry.width,
dw);
dw,
globalconf.colors.fg,
globalconf.colors.bg);
geometry.width = c->titlebar_sw->geometry.height;
geometry.height = c->titlebar_sw->geometry.width;
break;
@ -94,7 +96,9 @@ titlebar_draw(client_t *c)
ctx = draw_context_new(globalconf.connection, c->titlebar_sw->phys_screen,
c->titlebar_sw->geometry.width,
c->titlebar_sw->geometry.height,
c->titlebar_sw->drawable);
c->titlebar_sw->drawable,
globalconf.colors.fg,
globalconf.colors.bg);
geometry = c->titlebar_sw->geometry;
break;
}
@ -102,7 +106,7 @@ titlebar_draw(client_t *c)
text = titlebar_text(c);
geometry.x = geometry.y = 0;
draw_rectangle(ctx, geometry, 1.0, true, globalconf.colors.bg);
draw_text(ctx, globalconf.font, &globalconf.colors.fg, geometry, text);
draw_text(ctx, globalconf.font, geometry, text);
p_delete(&text);
switch(c->titlebar.position)

View File

@ -33,7 +33,7 @@ typedef struct
char **data_title; /** Data title of the data sections */
float *max; /** Represents a full graph */
int width; /** Width of the widget */
float height; /** Height of graph (0.0-1.0; 1.0 = height of statusbar) */
float height; /** Height of graph (0.0-1.0; 1.0 = height of bar) */
int box_height; /** Height of the innerbox in pixels */
int size; /** Size of lines-array (also innerbox-lenght) */
xcolor_t bg; /** Background color */
@ -162,28 +162,31 @@ graph_data_add(Data *d, const char *new_data_title)
}
static int
graph_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
int used __attribute__ ((unused)))
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)))
{
int margin_top;
int z, y, x, tmp, cur_index, test_index;
Data *d = w->widget->data;
area_t rectangle, pattern_area;
draw_context_t *ctx = statusbar->ctx;
if(!d->data_items)
return 0;
w->area.x = widget_calculate_offset(statusbar->width,
w->area.x = widget_calculate_offset(width,
d->width, offset,
w->widget->align);
w->area.y = 0;
/* box = the graph inside the rectangle */
if(!(d->box_height))
d->box_height = (int) (statusbar->height * d->height + 0.5) - 2;
d->box_height = (int) (height * d->height + 0.5) - 2;
margin_top = (int)((statusbar->height - (d->box_height + 2)) / 2 + 0.5) + w->area.y;
margin_top = (int)((height - (d->box_height + 2)) / 2 + 0.5) + w->area.y;
/* draw background */
rectangle.x = w->area.x + 1;
@ -349,7 +352,7 @@ graph_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
draw_rectangle(ctx, rectangle, 1.0, false, d->bordercolor);
w->area.width = d->width;
w->area.height = statusbar->height;
w->area.height = height;
return w->area.width;
}

View File

@ -29,8 +29,11 @@ typedef struct
} Data;
static int
iconbox_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
int used __attribute__ ((unused)))
iconbox_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)))
{
Data *d = w->widget->data;
area_t area = draw_get_image_size(d->image);
@ -40,24 +43,24 @@ iconbox_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
return (w->area.width = 0);
if(d->resize)
w->area.width = ((double) statusbar->height / area.height) * area.width;
w->area.width = ((double) height / area.height) * area.width;
else
w->area.width = area.width;
if(w->area.width > statusbar->width - used)
if(w->area.width > width - used)
return (w->area.width = 0);
w->area.height = statusbar->height;
w->area.height = height;
w->area.x = widget_calculate_offset(statusbar->width,
w->area.x = widget_calculate_offset(width,
w->area.width,
offset,
w->widget->align);
w->area.y = 0;
draw_image(statusbar->ctx, w->area.x, w->area.y,
d->resize ? statusbar->height : 0, d->image);
draw_image(ctx, w->area.x, w->area.y,
d->resize ? height : 0, d->image);
return w->area.width;
}

View File

@ -53,7 +53,7 @@ typedef struct
bool vertical;
/** Number of data_items (bars) */
int data_items;
/** Height 0-1, where 1.0 is height of statusbar */
/** Height 0-1, where 1.0 is height of bar */
float height;
/** Foreground color */
xcolor_t *fg;
@ -120,14 +120,17 @@ progressbar_data_add(Data *d, const char *new_data_title)
}
static int
progressbar_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
int used __attribute__ ((unused)))
progressbar_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)))
{
/* pb_.. values points to the widget inside a potential border */
int i, values_ticks, pb_x, pb_y, pb_height, pb_width, pb_progress, pb_offset;
int unit = 0; /* tick + gap */
area_t rectangle, pattern_rect;
draw_context_t *ctx = statusbar->ctx;
Data *d = w->widget->data;
if(!d->data_items)
@ -152,7 +155,7 @@ progressbar_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
w->area.width = pb_width + 2 * (d->border_width + d->border_padding);
}
w->area.x = widget_calculate_offset(statusbar->width,
w->area.x = widget_calculate_offset(width,
w->area.width,
offset,
w->widget->align);
@ -176,7 +179,7 @@ progressbar_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
{
/** \todo maybe prevent to calculate that stuff below over and over again
* (->use static-values) */
pb_height = (int) (statusbar->height * d->height + 0.5)
pb_height = (int) (height * d->height + 0.5)
- 2 * (d->border_width + d->border_padding);
if(d->ticks_count && d->ticks_gap)
{
@ -185,7 +188,7 @@ progressbar_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
pb_height = unit * d->ticks_count - d->ticks_gap;
}
pb_y = w->area.y + ((int) (statusbar->height * (1 - d->height)) / 2)
pb_y = w->area.y + ((int) (height * (1 - d->height)) / 2)
+ d->border_width + d->border_padding;
for(i = 0; i < d->data_items; i++)
@ -286,10 +289,10 @@ progressbar_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
}
else /* a horizontal progressbar */
{
pb_height = (int) ((statusbar->height * d->height
pb_height = (int) ((height * d->height
- d->data_items * 2 * (d->border_width + d->border_padding)
- (d->gap * (d->data_items - 1))) / d->data_items + 0.5);
pb_y = w->area.y + ((int) (statusbar->height * (1 - d->height)) / 2)
pb_y = w->area.y + ((int) (height * (1 - d->height)) / 2)
+ d->border_width + d->border_padding;
for(i = 0; i < d->data_items; i++)
@ -386,7 +389,7 @@ progressbar_draw(widget_node_t *w, statusbar_t *statusbar, int offset,
}
}
w->area.height = statusbar->height;
w->area.height = height;
return w->area.width;
}

View File

@ -32,7 +32,7 @@ extern awesome_t globalconf;
typedef struct taglist_drawn_area_t taglist_drawn_area_t;
struct taglist_drawn_area_t
{
statusbar_t *statusbar;
void *object;
area_t *area;
taglist_drawn_area_t *next, *prev;
};
@ -114,16 +114,15 @@ taglist_text_get(tag_t *tag, taglist_data_t *data)
}
static int
taglist_draw(widget_node_t *w,
statusbar_t *statusbar,
int offset,
int used __attribute__ ((unused)))
taglist_draw(draw_context_t *ctx, int screen, widget_node_t *w,
int width, int height, int offset,
int used __attribute__ ((unused)),
void *object)
{
tag_t *tag;
taglist_data_t *data = w->widget->data;
client_t *sel = globalconf.focus->client;
screen_t *vscreen = &globalconf.screens[statusbar->screen];
draw_context_t *ctx = statusbar->ctx;
screen_t *vscreen = &globalconf.screens[screen];
int i = 0, prev_width = 0;
area_t *area, rectangle = { 0, 0, 0, 0, NULL, NULL };
char **text = NULL;
@ -132,17 +131,15 @@ taglist_draw(widget_node_t *w,
w->area.width = w->area.y = 0;
/* Lookup for our taglist_drawn_area.
* This will be used to store area where we draw tag list for each
* statusbar.
*/
for(tda = data->drawn_area; tda && tda->statusbar != statusbar; tda = tda->next);
* This will be used to store area where we draw tag list for each object. */
for(tda = data->drawn_area; tda && tda->object != object; tda = tda->next);
/* Oh, we did not find a drawn area for our statusbar. First time? */
/* Oh, we did not find a drawn area for our object. First time? */
if(!tda)
{
/** \todo delete this when the widget is removed from the statusbar */
/** \todo delete this when the widget is removed from the object */
tda = p_new(taglist_drawn_area_t, 1);
tda->statusbar = statusbar;
tda->object = object;
taglist_drawn_area_list_push(&data->drawn_area, tda);
}
@ -162,7 +159,7 @@ taglist_draw(widget_node_t *w,
}
/* Now that we have widget width we can compute widget x coordinate */
w->area.x = widget_calculate_offset(statusbar->width, w->area.width,
w->area.x = widget_calculate_offset(width, w->area.width,
offset, w->widget->align);
for(area = tda->area, tag = vscreen->tags, i = 0;
@ -174,7 +171,7 @@ taglist_draw(widget_node_t *w,
area->x = w->area.x + prev_width;
prev_width += area->width;
draw_text(ctx, globalconf.font, &statusbar->colors.fg, *area, text[i]);
draw_text(ctx, globalconf.font, *area, text[i]);
p_delete(&text[i]);
if(tag_isoccupied(tag))
@ -183,13 +180,13 @@ taglist_draw(widget_node_t *w,
rectangle.x = area->x;
rectangle.y = area->y;
draw_rectangle(ctx, rectangle, 1.0,
sel && is_client_tagged(sel, tag), statusbar->colors.fg);
sel && is_client_tagged(sel, tag), ctx->fg);
}
}
p_delete(&text);
w->area.height = statusbar->height;
w->area.height = height;
return w->area.width;
}
@ -205,7 +202,7 @@ taglist_button_press(widget_node_t *w, statusbar_t *statusbar,
tag_t *tag;
/* Find the good drawn area list */
for(tda = data->drawn_area; tda && tda->statusbar != statusbar; tda = tda->next);
for(tda = data->drawn_area; tda && tda->object != statusbar; tda = tda->next);
area = tda->area;
for(b = w->widget->buttons; b; b = b->next)

View File

@ -63,7 +63,9 @@ tasklist_isvisible(client_t *c, int screen, showclient_t show)
}
static int
tasklist_draw(widget_node_t *w, statusbar_t *statusbar, int offset, int used)
tasklist_draw(draw_context_t *ctx, int screen,
widget_node_t *w, int width, int height,
int offset, int used, void *p __attribute__ ((unused)))
{
client_t *c;
Data *d = w->widget->data;
@ -72,27 +74,27 @@ tasklist_draw(widget_node_t *w, statusbar_t *statusbar, int offset, int used)
int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0;
NetWMIcon *icon;
if(used >= statusbar->width)
if(used >= width)
return (w->area.width = 0);
for(c = globalconf.clients; c; c = c->next)
if(tasklist_isvisible(c, statusbar->screen, d->show))
if(tasklist_isvisible(c, screen, d->show))
n++;
if(!n)
return (w->area.width = 0);
box_width = (statusbar->width - used) / n;
box_width = (width - used) / n;
/* compute how many pixel we left empty */
box_width_rest = (statusbar->width - used) % n;
box_width_rest = (width - used) % n;
w->area.x = widget_calculate_offset(statusbar->width,
w->area.x = widget_calculate_offset(width,
0, offset, w->widget->align);
w->area.y = 0;
for(c = globalconf.clients; c; c = c->next)
if(tasklist_isvisible(c, statusbar->screen, d->show))
if(tasklist_isvisible(c, screen, d->show))
{
icon_width = 0;
@ -110,7 +112,7 @@ tasklist_draw(widget_node_t *w, statusbar_t *statusbar, int offset, int used)
/* draw a background for icons */
area.x = w->area.x + box_width * i;
area.y = w->area.y;
area.height = statusbar->height;
area.height = height;
area.width = box_width;
if(c->icon_path)
@ -118,24 +120,24 @@ tasklist_draw(widget_node_t *w, statusbar_t *statusbar, int offset, int used)
area = draw_get_image_size(c->icon_path);
if(area.width > 0 && area.height > 0)
{
icon_width = ((double) statusbar->height / (double) area.height) * area.width;
draw_image(statusbar->ctx,
icon_width = ((double) height / (double) area.height) * area.width;
draw_image(ctx,
w->area.x + box_width * i,
w->area.y,
statusbar->height,
height,
c->icon_path);
}
}
if(!icon_width && (icon = ewmh_get_window_icon(c->win)))
{
icon_width = ((double) statusbar->height / (double) icon->height)
icon_width = ((double) height / (double) icon->height)
* icon->width;
draw_image_from_argb_data(statusbar->ctx,
draw_image_from_argb_data(ctx,
w->area.x + box_width * i,
w->area.y,
icon->width, icon->height,
statusbar->height, icon->image);
height, icon->image);
p_delete(&icon->image);
p_delete(&icon);
}
@ -144,28 +146,27 @@ tasklist_draw(widget_node_t *w, statusbar_t *statusbar, int offset, int used)
area.x = w->area.x + icon_width + box_width * i;
area.y = w->area.y;
area.width = box_width - icon_width;
area.height = statusbar->height;
area.height = height;
/* if we're on last elem, it has the last pixels left */
if(i == n - 1)
area.width += box_width_rest;
draw_text(statusbar->ctx, globalconf.font,
&statusbar->colors.fg,
draw_text(ctx, globalconf.font,
area, text);
p_delete(&text);
if(c->isfloating || c->ismax)
draw_circle(statusbar->ctx, w->area.x + icon_width + box_width * i,
draw_circle(ctx, w->area.x + icon_width + box_width * i,
w->area.y,
(globalconf.font->height + 2) / 4,
c->ismax, statusbar->colors.fg);
c->ismax, ctx->fg);
i++;
}
w->area.width = statusbar->width - used;
w->area.height = statusbar->height;
w->area.width = width - used;
w->area.height = height;
return w->area.width;
}

View File

@ -34,30 +34,32 @@ typedef struct
} Data;
static int
textbox_draw(widget_node_t *w, statusbar_t *statusbar, int offset, int used)
textbox_draw(draw_context_t *ctx, int screen __attribute__ ((unused)),
widget_node_t *w,
int width, int height, int offset, int used,
void *p __attribute__ ((unused)))
{
Data *d = w->widget->data;
if(d->width)
w->area.width = d->width;
else if(w->widget->align == AlignFlex)
w->area.width = statusbar->width - used;
w->area.width = width - used;
else
w->area.width = MIN(draw_text_extents(statusbar->ctx->connection,
statusbar->ctx->phys_screen,
w->area.width = MIN(draw_text_extents(ctx->connection,
ctx->phys_screen,
globalconf.font, d->text).width,
statusbar->width - used);
width - used);
w->area.height = statusbar->height;
w->area.height = height;
w->area.x = widget_calculate_offset(statusbar->width,
w->area.x = widget_calculate_offset(width,
w->area.width,
offset,
w->widget->align);
w->area.y = 0;
draw_text(statusbar->ctx, globalconf.font,
&statusbar->colors.fg,
draw_text(ctx, globalconf.font,
w->area, d->text);
return w->area.width;