draw: xcolor_t store color name

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-28 13:30:17 +02:00
parent 823ed27b82
commit 35e948f53b
9 changed files with 137 additions and 89 deletions

View File

@ -1005,8 +1005,11 @@ luaA_client_border_set(lua_State *L)
if(colorstr if(colorstr
&& xcolor_new(globalconf.connection, (*c)->phys_screen, colorstr, &color)) && xcolor_new(globalconf.connection, (*c)->phys_screen, colorstr, &color))
{
xcb_change_window_attributes(globalconf.connection, (*c)->win, XCB_CW_BORDER_PIXEL, xcb_change_window_attributes(globalconf.connection, (*c)->win, XCB_CW_BORDER_PIXEL,
&color.pixel); &color.pixel);
xcolor_wipe(&color);
}
return 0; return 0;
} }

View File

@ -1138,7 +1138,7 @@ xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t
color->red = hexa_color->red; color->red = hexa_color->red;
color->green = hexa_color->green; color->green = hexa_color->green;
color->blue = hexa_color->blue; color->blue = hexa_color->blue;
color->name = a_strdup(colstr);
p_delete(&hexa_color); p_delete(&hexa_color);
return true; return true;
} }
@ -1159,6 +1159,7 @@ xcolor_new(xcb_connection_t *conn, int phys_screen, const char *colstr, xcolor_t
color->green = named_color->visual_green; color->green = named_color->visual_green;
color->blue = named_color->visual_blue; color->blue = named_color->visual_blue;
color->alpha = 0xffff; color->alpha = 0xffff;
color->name = a_strdup(colstr);
p_delete(&named_color); p_delete(&named_color);
return true; return true;

View File

@ -33,6 +33,8 @@
typedef struct typedef struct
{ {
/** Color name */
char *name;
uint32_t pixel; uint32_t pixel;
uint16_t red; uint16_t red;
uint16_t green; uint16_t green;
@ -195,6 +197,17 @@ void draw_image_from_argb_data(draw_context_t *, int, int, int, int, int, unsign
void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int); void draw_rotate(draw_context_t *, xcb_drawable_t, xcb_drawable_t, int, int, int, int, double, int, int);
area_t draw_text_extents(xcb_connection_t *, int, font_t *, const char *, draw_parser_data_t *); area_t draw_text_extents(xcb_connection_t *, int, font_t *, const char *, draw_parser_data_t *);
alignment_t draw_align_fromstr(const char *, ssize_t); alignment_t draw_align_fromstr(const char *, ssize_t);
/** Wipe a color resources.
* \param color The color to wipe out.
*/
static inline void
xcolor_wipe(xcolor_t *color)
{
if(color)
p_delete(&color->name);
}
bool xcolor_new(xcb_connection_t *, int, const char *, xcolor_t *); bool xcolor_new(xcb_connection_t *, int, const char *, xcolor_t *);
void area_array_remove(area_array_t *, area_t); void area_array_remove(area_array_t *, area_t);

25
lua.c
View File

@ -394,14 +394,25 @@ luaA_font_set(lua_State *L)
static int static int
luaA_colors_set(lua_State *L) luaA_colors_set(lua_State *L)
{ {
const char *fg, *bg; const char *buf;
xcolor_t color;
luaA_checktable(L, 1); luaA_checktable(L, 1);
if((fg = luaA_getopt_string(L, 1, "fg", NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, if((buf = luaA_getopt_string(L, 1, "fg", NULL))
fg, &globalconf.colors.fg); && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
if((bg = luaA_getopt_string(L, 1, "bg", NULL))) {
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_wipe(&globalconf.colors.fg);
bg, &globalconf.colors.bg); globalconf.colors.fg = color;
}
if((buf = luaA_getopt_string(L, 1, "bg", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{
xcolor_wipe(&globalconf.colors.bg);
globalconf.colors.bg = color;
}
return 0; return 0;
} }

View File

@ -350,13 +350,15 @@ luaA_statusbar_colors_set(lua_State *L)
{ {
statusbar_t **sb = luaA_checkudata(L, 1, "statusbar"); statusbar_t **sb = luaA_checkudata(L, 1, "statusbar");
const char *buf; const char *buf;
xcolor_t color;
luaA_checktable(L, 2); luaA_checktable(L, 2);
if ((buf = luaA_getopt_string(L, 2, "fg", NULL))) if((buf = luaA_getopt_string(L, 2, "fg", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{ {
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_wipe(&(*sb)->colors.fg);
buf, &(*sb)->colors.fg); (*sb)->colors.fg = color;
if((*sb)->ctx) if((*sb)->ctx)
(*sb)->ctx->fg = (*sb)->colors.fg; (*sb)->ctx->fg = (*sb)->colors.fg;
@ -364,10 +366,11 @@ luaA_statusbar_colors_set(lua_State *L)
(*sb)->need_update = true; (*sb)->need_update = true;
} }
if ((buf = luaA_getopt_string(L, 2, "bg", NULL))) if((buf = luaA_getopt_string(L, 2, "bg", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{ {
xcolor_new(globalconf.connection, globalconf.default_screen, xcolor_wipe(&(*sb)->colors.bg);
buf, &(*sb)->colors.bg); (*sb)->colors.bg = color;
if((*sb)->ctx) if((*sb)->ctx)
(*sb)->ctx->bg = (*sb)->colors.bg; (*sb)->ctx->bg = (*sb)->colors.bg;
@ -541,16 +544,14 @@ luaA_statusbar_new(lua_State *L)
sb->name = luaA_name_init(L); sb->name = luaA_name_init(L);
if((buf = luaA_getopt_string(L, 1, "fg", NULL))) if(!(buf = luaA_getopt_string(L, 1, "fg", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, || !xcolor_new(globalconf.connection, globalconf.default_screen,
buf, &sb->colors.fg); buf, &sb->colors.fg))
else
sb->colors.fg = globalconf.colors.fg; sb->colors.fg = globalconf.colors.fg;
if((buf = luaA_getopt_string(L, 1, "bg", NULL))) if(!(buf = luaA_getopt_string(L, 1, "bg", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, || !xcolor_new(globalconf.connection, globalconf.default_screen,
buf, &sb->colors.bg); buf, &sb->colors.bg))
else
sb->colors.bg = globalconf.colors.bg; sb->colors.bg = globalconf.colors.bg;
buf = luaA_getopt_lstring(L, 1, "align", "left", &len); buf = luaA_getopt_lstring(L, 1, "align", "left", &len);

View File

@ -314,21 +314,20 @@ luaA_titlebar_new(lua_State *L)
buf = luaA_getopt_lstring(L, 1, "position", "top", &len); buf = luaA_getopt_lstring(L, 1, "position", "top", &len);
tb->position = position_fromstr(buf, len); tb->position = position_fromstr(buf, len);
if((buf = luaA_getopt_string(L, -1, "fg", NULL))) if(!(buf = luaA_getopt_string(L, -1, "fg", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, || !xcolor_new(globalconf.connection, globalconf.default_screen,
buf, &tb->colors.fg); buf, &tb->colors.fg))
else
tb->colors.fg = globalconf.colors.fg; tb->colors.fg = globalconf.colors.fg;
if((buf = luaA_getopt_string(L, 1, "bg", NULL))) if(!(buf = luaA_getopt_string(L, -1, "bg", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, || !xcolor_new(globalconf.connection, globalconf.default_screen,
buf, &tb->colors.bg); buf, &tb->colors.bg))
else
tb->colors.bg = globalconf.colors.bg; tb->colors.bg = globalconf.colors.bg;
if((buf = luaA_getopt_string(L, 1, "border_color", NULL))) if(!(buf = luaA_getopt_string(L, -1, "border_color", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, || !xcolor_new(globalconf.connection, globalconf.default_screen,
buf, &tb->border.color); buf, &tb->border.color))
tb->border.color = globalconf.colors.fg;
tb->border.width = luaA_getopt_number(L, 1, "border_width", 0); tb->border.width = luaA_getopt_number(L, 1, "border_width", 0);

View File

@ -110,22 +110,6 @@ typedef struct
plot_t *plots; plot_t *plots;
} graph_data_t; } graph_data_t;
static void
plot_pcolor_set(xcolor_t **ppcolor, const 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);
}
/** Add a plot to a graph. /** Add a plot to a graph.
* \param d The graph private data. * \param d The graph private data.
* \param title The plot title. * \param title The plot title.
@ -302,6 +286,7 @@ luaA_graph_plot_properties_set(lua_State *L)
const char *title, *buf; const char *title, *buf;
size_t len; size_t len;
plot_t *plot; plot_t *plot;
xcolor_t color;
title = luaL_checkstring(L, 2); title = luaL_checkstring(L, 2);
luaA_checktable(L, 3); luaA_checktable(L, 3);
@ -313,12 +298,26 @@ luaA_graph_plot_properties_set(lua_State *L)
if(!plot) if(!plot)
plot = graph_plot_add(d, title); plot = graph_plot_add(d, title);
if((buf = luaA_getopt_string(L, 3, "fg", NULL))) if((buf = luaA_getopt_string(L, 3, "fg", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, buf, &plot->color_start); && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))) {
plot_pcolor_set(&plot->pcolor_center, buf); xcolor_wipe(&plot->color_start);
if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))) plot->color_start = color;
plot_pcolor_set(&plot->pcolor_end, buf); }
if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{
xcolor_wipe(plot->pcolor_center);
plot->pcolor_center = p_dup(&color, 1);
}
if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{
xcolor_wipe(plot->pcolor_end);
plot->pcolor_end = p_dup(&color, 1);
}
plot->vertical_gradient = luaA_getopt_boolean(L, 3, "vertical_gradient", plot->vertical_gradient); plot->vertical_gradient = luaA_getopt_boolean(L, 3, "vertical_gradient", plot->vertical_gradient);
plot->scale = luaA_getopt_boolean(L, 3, "scale", plot->scale); plot->scale = luaA_getopt_boolean(L, 3, "scale", plot->scale);
@ -483,6 +482,7 @@ luaA_graph_newindex(lua_State *L)
int width; int width;
plot_t *plot; plot_t *plot;
position_t pos; position_t pos;
xcolor_t color;
switch(a_tokenize(attr, len)) switch(a_tokenize(attr, len))
{ {
@ -510,10 +510,22 @@ luaA_graph_newindex(lua_State *L)
return 0; return 0;
break; break;
case A_TK_BG: case A_TK_BG:
xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &d->bg); if(xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &color))
{
xcolor_wipe(&d->bg);
d->bg = color;
}
else
return 0;
break; break;
case A_TK_BORDER_COLOR: case A_TK_BORDER_COLOR:
xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &d->border_color); if(xcolor_new(globalconf.connection, globalconf.default_screen, luaL_checkstring(L, 3), &color))
{
xcolor_wipe(&d->border_color);
d->border_color = color;
}
else
return 0;
break; break;
case A_TK_GROW: case A_TK_GROW:
buf = luaL_checklstring(L, 3, &len); buf = luaL_checklstring(L, 3, &len);

View File

@ -51,7 +51,7 @@ struct bar_t
/** Background color */ /** Background color */
xcolor_t bg; xcolor_t bg;
/** Border color */ /** Border color */
xcolor_t bordercolor; xcolor_t border_color;
/** The next and previous bar in the list */ /** The next and previous bar in the list */
bar_t *next, *prev; bar_t *next, *prev;
}; };
@ -93,22 +93,6 @@ typedef struct
bar_t *bars; bar_t *bars;
} progressbar_data_t; } progressbar_data_t;
static void
progressbar_pcolor_set(xcolor_t **ppcolor, const 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);
}
/** Add a new bar to the progressbar private data structure. /** Add a new bar to the progressbar private data structure.
* \param d The private data structure. * \param d The private data structure.
* \param title The graph title. * \param title The graph title.
@ -122,7 +106,7 @@ progressbar_bar_add(progressbar_data_t *d, const char *title)
bar->fg = globalconf.colors.fg; bar->fg = globalconf.colors.fg;
bar->fg_off = globalconf.colors.bg; bar->fg_off = globalconf.colors.bg;
bar->bg = globalconf.colors.bg; bar->bg = globalconf.colors.bg;
bar->bordercolor = globalconf.colors.fg; bar->border_color = globalconf.colors.fg;
bar->max_value = 100.0; bar->max_value = 100.0;
/* append the bar in the list */ /* append the bar in the list */
@ -245,7 +229,7 @@ progressbar_draw(draw_context_t *ctx,
if(d->border_padding) if(d->border_padding)
draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); draw_rectangle(ctx, rectangle, 1.0, true, bar->bg);
draw_rectangle(ctx, rectangle, d->border_width, false, bar->bordercolor); draw_rectangle(ctx, rectangle, d->border_width, false, bar->border_color);
} }
pattern_rect.x = pb_x; pattern_rect.x = pb_x;
@ -345,7 +329,7 @@ progressbar_draw(draw_context_t *ctx,
if(d->border_padding) if(d->border_padding)
draw_rectangle(ctx, rectangle, 1.0, true, bar->bg); draw_rectangle(ctx, rectangle, 1.0, true, bar->bg);
draw_rectangle(ctx, rectangle, d->border_width, false, bar->bordercolor); draw_rectangle(ctx, rectangle, d->border_width, false, bar->border_color);
} }
pattern_rect.y = pb_y; pattern_rect.y = pb_y;
@ -462,6 +446,7 @@ luaA_progressbar_bar_properties_set(lua_State *L)
const char *buf, *title = luaL_checkstring(L, 2); const char *buf, *title = luaL_checkstring(L, 2);
bar_t *bar; bar_t *bar;
progressbar_data_t *d = (*widget)->data; progressbar_data_t *d = (*widget)->data;
xcolor_t color;
luaA_checktable(L, 3); luaA_checktable(L, 3);
@ -474,18 +459,40 @@ luaA_progressbar_bar_properties_set(lua_State *L)
if(!bar) if(!bar)
bar = progressbar_bar_add(d, title); bar = progressbar_bar_add(d, title);
if((buf = luaA_getopt_string(L, 3, "fg", NULL))) if((buf = luaA_getopt_string(L, 3, "fg", NULL))
xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->fg); && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
if((buf = luaA_getopt_string(L, 3, "bg", NULL))) {
xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->bg); xcolor_wipe(&bar->fg);
if((buf = luaA_getopt_string(L, 3, "fg_off", NULL))) bar->fg = color;
xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->fg_off); }
if((buf = luaA_getopt_string(L, 3, "border_color", NULL)))
xcolor_new(globalconf.connection, globalconf.default_screen, buf, &bar->bordercolor); if((buf = luaA_getopt_string(L, 3, "bg", NULL))
if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))) && xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
progressbar_pcolor_set(&bar->pfg_center, buf); {
if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))) xcolor_wipe(&bar->bg);
progressbar_pcolor_set(&bar->pfg_end, buf); bar->bg = color;
}
if((buf = luaA_getopt_string(L, 3, "border_color", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{
xcolor_wipe(&bar->border_color);
bar->border_color = color;
}
if((buf = luaA_getopt_string(L, 3, "fg_center", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{
xcolor_wipe(bar->pfg_center);
bar->pfg_end = p_dup(&color, 1);;
}
if((buf = luaA_getopt_string(L, 3, "fg_end", NULL))
&& xcolor_new(globalconf.connection, globalconf.default_screen, buf, &color))
{
xcolor_wipe(bar->pfg_end);
bar->pfg_end = p_dup(&color, 1);;
}
bar->min_value = luaA_getopt_number(L, 3, "min_value", bar->min_value); bar->min_value = luaA_getopt_number(L, 3, "min_value", bar->min_value);
/* hack to prevent max_value beeing less than min_value /* hack to prevent max_value beeing less than min_value

View File

@ -94,6 +94,7 @@ tasklist_markup_on_elem(markup_parser_data_t *p, const char *elem,
xcolor_t bg_color; xcolor_t bg_color;
xcolor_new(ctx->connection, ctx->phys_screen, *values, &bg_color); xcolor_new(ctx->connection, ctx->phys_screen, *values, &bg_color);
draw_rectangle(ctx, *data->area, 1.0, true, bg_color); draw_rectangle(ctx, *data->area, 1.0, true, bg_color);
xcolor_wipe(&bg_color);
break; break;
} }
} }