From 8e9f0c8857eaca901a22633cb7603d0980c9429e Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 2 Dec 2008 18:14:51 +0100 Subject: [PATCH] draw: remove margin markup, move to textbox widget Signed-off-by: Julien Danjou --- awesomerc.5.txt | 4 ---- common/tokenize.gperf | 1 + draw.c | 31 ++++++++----------------------- draw.h | 3 +-- lib/awful/widget.lua.in | 3 ++- lib/naughty.lua.in | 3 ++- widgets/textbox.c | 23 ++++++++++++++++++++++- 7 files changed, 36 insertions(+), 32 deletions(-) diff --git a/awesomerc.5.txt b/awesomerc.5.txt index 1a9d78503..06039f946 100644 --- a/awesomerc.5.txt +++ b/awesomerc.5.txt @@ -49,10 +49,6 @@ List of *awesome* markup elements and their attributes: - image: path to a background image - align: background image alignment - resize: resize background image to text size -* margin - - left: left margin in pixel - - right: right margin in pixel - - top: top margin in pixel * text - shadow: shadow color - shadow_offset: shadow color offset in pixel diff --git a/common/tokenize.gperf b/common/tokenize.gperf index 36f2238c9..b956fc113 100644 --- a/common/tokenize.gperf +++ b/common/tokenize.gperf @@ -43,6 +43,7 @@ len line Lock machine +margin maximized_horizontal maximized_vertical middle diff --git a/draw.c b/draw.c index f09815035..3b97d5d2d 100644 --- a/draw.c +++ b/draw.c @@ -217,22 +217,6 @@ draw_markup_on_element(markup_parser_data_t *p, const char *elem, break; } break; - case 'm': /* margin */ - for (; *names; names++, values++) - switch(a_tokenize(*names, -1)) - { - case A_TK_LEFT: - data->margin.left = atoi(*values); - break; - case A_TK_RIGHT: - data->margin.right = atoi(*values); - break; - case A_TK_TOP: - data->margin.top = atoi(*values); - default: - break; - } - break; } for(i = 0; i <= reqs_nbr; i++) @@ -246,7 +230,7 @@ static bool draw_text_markup_expand(draw_parser_data_t *data, const char *str, ssize_t slen) { - static char const * const elements[] = { "bg", "bg_margin", "text", "margin", NULL }; + static char const * const elements[] = { "bg", "bg_margin", "text", NULL }; markup_parser_data_t p = { .elements = elements, @@ -312,6 +296,7 @@ draw_context_init(draw_context_t *d, int phys_screen, * \param elip Ellipsize mode. * \param wrap Wrap mode. * \param align Text alignment. + * \param margin Margin to respect when drawing text. * \param area Area to draw to. * \param text Text to draw. * \param len Text to draw length. @@ -320,7 +305,7 @@ draw_context_init(draw_context_t *d, int phys_screen, */ void draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrapMode wrap, - alignment_t align, area_t area, const char *text, ssize_t len, + alignment_t align, padding_t *margin, area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata, area_t *ext) { int x, y; @@ -369,19 +354,19 @@ draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrap pango_layout_set_text(ctx->layout, pdata->text, pdata->len); pango_layout_set_width(ctx->layout, pango_units_from_double(area.width - - (pdata->margin.left - + pdata->margin.right))); - pango_layout_set_height(ctx->layout, pango_units_from_double(area.height) - pdata->margin.top); + - (margin->left + + margin->right))); + pango_layout_set_height(ctx->layout, pango_units_from_double(area.height) - margin->top); pango_layout_set_ellipsize(ctx->layout, ellip); pango_layout_set_wrap(ctx->layout, wrap); pango_layout_set_attributes(ctx->layout, pdata->attr_list); pango_layout_set_font_description(ctx->layout, font->desc); - x = area.x + pdata->margin.left; + x = area.x + margin->left; /* + 1 is added for rounding, so that in any case of doubt we rather draw * the text 1px lower than too high which usually results in a better type * face */ - y = area.y + (ctx->height - ext->height + 1) / 2 + pdata->margin.top; + y = area.y + (ctx->height - ext->height + 1) / 2 + margin->top; /* only honors alignment if enough space */ if(ext->width < area.width) diff --git a/draw.h b/draw.h index 0953f2ad5..2979e1a25 100644 --- a/draw.h +++ b/draw.h @@ -161,7 +161,6 @@ typedef struct PangoAttrList *attr_list; char *text; ssize_t len; - padding_t margin; struct { int top, left; @@ -178,7 +177,7 @@ typedef struct } shadow; } draw_parser_data_t; -void draw_text(draw_context_t *, font_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, area_t, const char *, ssize_t len, draw_parser_data_t *, area_t *); +void draw_text(draw_context_t *, font_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, padding_t *, area_t, const char *, ssize_t len, draw_parser_data_t *, area_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, vector_t, const xcolor_t *, const xcolor_t *, const xcolor_t *); diff --git a/lib/awful/widget.lua.in b/lib/awful/widget.lua.in index 6c280c925..a9006e23d 100644 --- a/lib/awful/widget.lua.in +++ b/lib/awful/widget.lua.in @@ -194,6 +194,7 @@ local function tasklist_update(w, buttons, label, data) for i = len * 2 + 1, #clients * 2, 2 do w[i] = capi.widget({ type = "imagebox", align = "flex" }) w[i + 1] = capi.widget({ type = "textbox", align = "flex" }) + w[i + 1]:margin({ left = 2, right = 2 }) end -- Remove widgets elseif len > #clients then @@ -276,7 +277,7 @@ local function widget_tasklist_label_common(c, args) local floating_icon_align = args.floating_icon_align or theme.tasklist_floating_icon_align or "right" local font = args.font or theme.tasklist_font or theme.font or "" local bg = nil - local text = "" + local text = "" local name if client.floating.get(c) and floating_icon then text = text.."" diff --git a/lib/naughty.lua.in b/lib/naughty.lua.in index a1157f234..89a281351 100644 --- a/lib/naughty.lua.in +++ b/lib/naughty.lua.in @@ -308,7 +308,8 @@ function notify(args) local textbox = widget({ type = "textbox", align = "flex" }) textbox:buttons({ button({ }, 1, run), button({ }, 3, die) }) - textbox.text = string.format('%s%s', font, title, text) + textbox:margin({ right = config.margin, left = config.margin }) + textbox.text = string.format('%s%s', font, title, text) if hover_timeout then textbox.mouse_enter = hover_destroy end -- create iconbox diff --git a/widgets/textbox.c b/widgets/textbox.c index 8b68e6780..5f8c92492 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -47,6 +47,8 @@ typedef struct } border; /** Text alignment */ alignment_t align; + /** Margin */ + padding_t margin; } textbox_data_t; static area_t @@ -88,7 +90,7 @@ textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, if(d->border.width > 0) draw_rectangle(ctx, geometry, d->border.width, false, &d->border.color); - draw_text(ctx, globalconf.font, d->ellip, d->wrap, d->align, + draw_text(ctx, globalconf.font, d->ellip, d->wrap, d->align, &d->margin, geometry, d->text, d->len, &d->pdata, &d->extents); } @@ -104,6 +106,21 @@ textbox_destructor(widget_t *w) p_delete(&d); } +static int +luaA_textbox_margin(lua_State *L) +{ + widget_t **widget = luaA_checkudata(L, 1, "widget"); + textbox_data_t *d = (*widget)->data; + + if(lua_gettop(L) == 2) + { + d->margin = luaA_getopt_padding(L, 3, &d->margin); + widget_invalidate_bywidget(*widget); + } + + return luaA_pushpadding(L, &d->margin); +} + /** Textbox widget. * \param L The Lua VM state. * \param token The key token. @@ -116,6 +133,7 @@ textbox_destructor(widget_t *w) * \lfield border_width The border width to draw around. * \lfield border_color The border color. * \lfield align Text alignment, left, center or right. + * \lfield margin Method to pass text margin: a table with top, left, right and bottom keys. */ static int luaA_textbox_index(lua_State *L, awesome_token_t token) @@ -125,6 +143,9 @@ luaA_textbox_index(lua_State *L, awesome_token_t token) switch(token) { + case A_TK_MARGIN: + lua_pushcfunction(L, luaA_textbox_margin); + return 1; case A_TK_ALIGN: lua_pushstring(L, draw_align_tostr(d->align)); return 1;