diff --git a/draw.c b/draw.c index c997cbcf..761dd5d5 100644 --- a/draw.c +++ b/draw.c @@ -201,41 +201,32 @@ draw_context_init(draw_context_t *d, int phys_screen, * \param wrap Wrap mode. * \param align Text alignment. * \param valign Vertical text alignment. - * \param margin Margin to respect when drawing text. * \param area Area to draw to. * \param ext Text extents. */ void draw_text(draw_context_t *ctx, draw_text_context_t *data, PangoEllipsizeMode ellip, PangoWrapMode wrap, - alignment_t align, alignment_t valign, padding_t *margin, area_t area, area_t *ext) + alignment_t align, alignment_t valign, area_t area, area_t *ext) { - int x, y; - pango_layout_set_text(ctx->layout, data->text, data->len); pango_layout_set_width(ctx->layout, - pango_units_from_double(area.width - - (margin->left - + margin->right))); - pango_layout_set_height(ctx->layout, pango_units_from_double(area.height) - - (margin->top + margin->bottom)); + pango_units_from_double(area.width)); + pango_layout_set_height(ctx->layout, pango_units_from_double(area.height)); pango_layout_set_ellipsize(ctx->layout, ellip); pango_layout_set_wrap(ctx->layout, wrap); pango_layout_set_attributes(ctx->layout, data->attr_list); pango_layout_set_font_description(ctx->layout, globalconf.font->desc); - x = area.x + margin->left; - y = area.y + margin->top; - /* only honors alignment if enough space */ if(ext->width < area.width) switch(align) { case AlignCenter: - x += (area.width - ext->width) / 2; + area.x += (area.width - ext->width) / 2; break; case AlignRight: - x += area.width - ext->width; + area.x += area.width - ext->width; break; default: break; @@ -244,16 +235,16 @@ draw_text(draw_context_t *ctx, draw_text_context_t *data, switch(valign) { case AlignCenter: - y += (area.height - ext->height) / 2; + area.y += (area.height - ext->height) / 2; break; case AlignBottom: - y += area.height - ext->height; + area.y += area.height - ext->height; break; default: break; } - cairo_move_to(ctx->cr, x, y); + cairo_move_to(ctx->cr, area.x, area.y); cairo_set_source_rgba(ctx->cr, ctx->fg.red / 65535.0, diff --git a/draw.h b/draw.h index d1752b21..34240a0f 100644 --- a/draw.h +++ b/draw.h @@ -1,7 +1,7 @@ /* * draw.h - draw functions header * - * Copyright © 2007-2008 Julien Danjou + * Copyright © 2007-2009 Julien Danjou * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,19 +31,6 @@ #include "color.h" #include "common/array.h" -/** Padding type */ -typedef struct -{ - /** Padding at top */ - int top; - /** Padding at bottom */ - int bottom; - /** Padding at left */ - int left; - /** Padding at right */ - int right; -} padding_t; - typedef enum { AlignLeft = (0), @@ -159,7 +146,7 @@ typedef struct } draw_text_context_t; bool draw_text_context_init(draw_text_context_t *, const char *, ssize_t); -void draw_text(draw_context_t *, draw_text_context_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, alignment_t, padding_t *, area_t, area_t *); +void draw_text(draw_context_t *, draw_text_context_t *, PangoEllipsizeMode, PangoWrapMode, alignment_t, alignment_t, area_t, area_t *); void draw_rectangle(draw_context_t *, area_t, float, bool, const color_t *); void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t, const color_t *, const color_t *, const color_t *); diff --git a/luaa.h b/luaa.h index bc0a04c5..c03aba45 100644 --- a/luaa.h +++ b/luaa.h @@ -181,47 +181,6 @@ luaA_warn(lua_State *L, const char *fmt, ...) fprintf(stderr, "\n"); } -/** Get an optional padding table from a Lua table. - * \param L The Lua VM state. - * \param idx The table index on the stack. - * \param dpadding The default padding value to use. - */ -static inline padding_t -luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding) -{ - padding_t padding; - - luaA_checktable(L, idx); - - padding.right = luaA_getopt_number(L, idx, "right", dpadding->right); - padding.left = luaA_getopt_number(L, idx, "left", dpadding->left); - padding.top = luaA_getopt_number(L, idx, "top", dpadding->top); - padding.bottom = luaA_getopt_number(L, idx, "bottom", dpadding->bottom); - - return padding; -} - - -/** Push a padding structure into a table on the Lua stack. - * \param L The Lua VM state. - * \param padding The padding struct pointer. - * \return The number of elements pushed on stack. - */ -static inline int -luaA_pushpadding(lua_State *L, padding_t *padding) -{ - lua_createtable(L, 0, 4); - lua_pushnumber(L, padding->right); - lua_setfield(L, -2, "right"); - lua_pushnumber(L, padding->left); - lua_setfield(L, -2, "left"); - lua_pushnumber(L, padding->top); - lua_setfield(L, -2, "top"); - lua_pushnumber(L, padding->bottom); - lua_setfield(L, -2, "bottom"); - return 1; -} - void luaA_init(xdgHandle *); bool luaA_parserc(xdgHandle *, const char *, bool); void luaA_on_timer(EV_P_ ev_timer *, int); diff --git a/widgets/textbox.c b/widgets/textbox.c index 238d0464..5ce8c20c 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -1,7 +1,7 @@ /* * textbox.c - text box widget * - * Copyright © 2007-2008 Julien Danjou + * Copyright © 2007-2009 Julien Danjou * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,19 @@ #include "luaa.h" #include "common/tokenize.h" +/** Padding type */ +typedef struct +{ + /** Padding at top */ + int top; + /** Padding at bottom */ + int bottom; + /** Padding at left */ + int left; + /** Padding at right */ + int right; +} padding_t; + /** The textbox private data structure */ typedef struct { @@ -54,6 +67,47 @@ typedef struct alignment_t bg_align; } textbox_data_t; +/** Get an optional padding table from a Lua table. + * \param L The Lua VM state. + * \param idx The table index on the stack. + * \param dpadding The default padding value to use. + */ +static inline padding_t +luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding) +{ + padding_t padding; + + luaA_checktable(L, idx); + + padding.right = luaA_getopt_number(L, idx, "right", dpadding->right); + padding.left = luaA_getopt_number(L, idx, "left", dpadding->left); + padding.top = luaA_getopt_number(L, idx, "top", dpadding->top); + padding.bottom = luaA_getopt_number(L, idx, "bottom", dpadding->bottom); + + return padding; +} + + +/** Push a padding structure into a table on the Lua stack. + * \param L The Lua VM state. + * \param padding The padding struct pointer. + * \return The number of elements pushed on stack. + */ +static inline int +luaA_pushpadding(lua_State *L, padding_t *padding) +{ + lua_createtable(L, 0, 4); + lua_pushnumber(L, padding->right); + lua_setfield(L, -2, "right"); + lua_pushnumber(L, padding->left); + lua_setfield(L, -2, "left"); + lua_pushnumber(L, padding->top); + lua_setfield(L, -2, "top"); + lua_pushnumber(L, padding->bottom); + lua_setfield(L, -2, "bottom"); + return 1; +} + static area_t textbox_extents(lua_State *L, widget_t *widget) { @@ -122,7 +176,12 @@ textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p) } } - draw_text(ctx, &d->data, d->ellip, d->wrap, d->align, d->valign, &d->margin, geometry, &d->extents); + geometry.x += d->margin.left; + geometry.y += d->margin.top; + geometry.width -= d->margin.left + d->margin.right; + geometry.height -= d->margin.top + d->margin.bottom; + + draw_text(ctx, &d->data, d->ellip, d->wrap, d->align, d->valign, geometry, &d->extents); } /** Delete a textbox widget.