draw: remove margin markup, move to textbox widget

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-12-02 18:14:51 +01:00
parent f099e7de27
commit 8e9f0c8857
7 changed files with 36 additions and 32 deletions

View File

@ -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

View File

@ -43,6 +43,7 @@ len
line
Lock
machine
margin
maximized_horizontal
maximized_vertical
middle

31
draw.c
View File

@ -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)

3
draw.h
View File

@ -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 *);

View File

@ -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 = "<margin left=\"2\" right=\"2\"/><span font_desc='"..font.."'>"
local text = "<span font_desc='"..font.."'>"
local name
if client.floating.get(c) and floating_icon then
text = text.."<bg image=\"" .. floating_icon .. "\" align=\"" .. floating_icon_align .. "\"/>"

View File

@ -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('<margin right="'..config.margin..'" left="'..config.margin..'"/><span font_desc="%s"><b>%s</b>%s</span>', font, title, text)
textbox:margin({ right = config.margin, left = config.margin })
textbox.text = string.format('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text)
if hover_timeout then textbox.mouse_enter = hover_destroy end
-- create iconbox

View File

@ -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;