draw: move padding stuff inside textbox

This is only used by textbox.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-08-25 20:24:38 +02:00
parent a71c5efd10
commit 7ecf1afdfd
4 changed files with 71 additions and 75 deletions

25
draw.c
View File

@ -201,41 +201,32 @@ draw_context_init(draw_context_t *d, int phys_screen,
* \param wrap Wrap mode. * \param wrap Wrap mode.
* \param align Text alignment. * \param align Text alignment.
* \param valign Vertical text alignment. * \param valign Vertical text alignment.
* \param margin Margin to respect when drawing text.
* \param area Area to draw to. * \param area Area to draw to.
* \param ext Text extents. * \param ext Text extents.
*/ */
void void
draw_text(draw_context_t *ctx, draw_text_context_t *data, draw_text(draw_context_t *ctx, draw_text_context_t *data,
PangoEllipsizeMode ellip, PangoWrapMode wrap, 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_text(ctx->layout, data->text, data->len);
pango_layout_set_width(ctx->layout, pango_layout_set_width(ctx->layout,
pango_units_from_double(area.width pango_units_from_double(area.width));
- (margin->left pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
+ margin->right)));
pango_layout_set_height(ctx->layout, pango_units_from_double(area.height)
- (margin->top + margin->bottom));
pango_layout_set_ellipsize(ctx->layout, ellip); pango_layout_set_ellipsize(ctx->layout, ellip);
pango_layout_set_wrap(ctx->layout, wrap); pango_layout_set_wrap(ctx->layout, wrap);
pango_layout_set_attributes(ctx->layout, data->attr_list); pango_layout_set_attributes(ctx->layout, data->attr_list);
pango_layout_set_font_description(ctx->layout, globalconf.font->desc); 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 */ /* only honors alignment if enough space */
if(ext->width < area.width) if(ext->width < area.width)
switch(align) switch(align)
{ {
case AlignCenter: case AlignCenter:
x += (area.width - ext->width) / 2; area.x += (area.width - ext->width) / 2;
break; break;
case AlignRight: case AlignRight:
x += area.width - ext->width; area.x += area.width - ext->width;
break; break;
default: default:
break; break;
@ -244,16 +235,16 @@ draw_text(draw_context_t *ctx, draw_text_context_t *data,
switch(valign) switch(valign)
{ {
case AlignCenter: case AlignCenter:
y += (area.height - ext->height) / 2; area.y += (area.height - ext->height) / 2;
break; break;
case AlignBottom: case AlignBottom:
y += area.height - ext->height; area.y += area.height - ext->height;
break; break;
default: default:
break; break;
} }
cairo_move_to(ctx->cr, x, y); cairo_move_to(ctx->cr, area.x, area.y);
cairo_set_source_rgba(ctx->cr, cairo_set_source_rgba(ctx->cr,
ctx->fg.red / 65535.0, ctx->fg.red / 65535.0,

17
draw.h
View File

@ -1,7 +1,7 @@
/* /*
* draw.h - draw functions header * draw.h - draw functions header
* *
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info> * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -31,19 +31,6 @@
#include "color.h" #include "color.h"
#include "common/array.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 typedef enum
{ {
AlignLeft = (0), AlignLeft = (0),
@ -159,7 +146,7 @@ typedef struct
} draw_text_context_t; } draw_text_context_t;
bool draw_text_context_init(draw_text_context_t *, const char *, ssize_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(draw_context_t *, area_t, float, bool, const color_t *);
void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t, void draw_rectangle_gradient(draw_context_t *, area_t, float, bool, vector_t,
const color_t *, const color_t *, const color_t *); const color_t *, const color_t *, const color_t *);

41
luaa.h
View File

@ -181,47 +181,6 @@ luaA_warn(lua_State *L, const char *fmt, ...)
fprintf(stderr, "\n"); 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 *); void luaA_init(xdgHandle *);
bool luaA_parserc(xdgHandle *, const char *, bool); bool luaA_parserc(xdgHandle *, const char *, bool);
void luaA_on_timer(EV_P_ ev_timer *, int); void luaA_on_timer(EV_P_ ev_timer *, int);

View File

@ -1,7 +1,7 @@
/* /*
* textbox.c - text box widget * textbox.c - text box widget
* *
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info> * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -24,6 +24,19 @@
#include "luaa.h" #include "luaa.h"
#include "common/tokenize.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 */ /** The textbox private data structure */
typedef struct typedef struct
{ {
@ -54,6 +67,47 @@ typedef struct
alignment_t bg_align; alignment_t bg_align;
} textbox_data_t; } 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 static area_t
textbox_extents(lua_State *L, widget_t *widget) 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. /** Delete a textbox widget.