textbox: add ellipsize and wrap mode support

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-11-06 17:04:23 +01:00
parent 67c56d35a4
commit a5cb98162d
5 changed files with 82 additions and 7 deletions

View File

@ -11,6 +11,7 @@ bottomleft
bottomright
button
center
char
class
client
color
@ -18,6 +19,8 @@ Control
coords
Ctrl
east
ellipsize
end
fg
flex
floating
@ -41,6 +44,7 @@ len
line
Lock
machine
middle
minimize
Mod1
Mod2
@ -50,9 +54,9 @@ Mod5
mouse_enter
mouse_leave
mwfact
name
ncol
nmaster
name
on
ontop
opacity
@ -75,6 +79,7 @@ show_icons
sizehints
skip_taskbar
south
start
sticky
text
ticks_count
@ -91,5 +96,8 @@ visible
vertical
widgets
width
word
word_char
workarea
wrap
yes

8
draw.c
View File

@ -330,13 +330,15 @@ draw_context_init(draw_context_t *d, int phys_screen,
/** Draw text into a draw context.
* \param ctx Draw context to draw to.
* \param font The font to use.
* \param elip Ellipsize mode.
* \param wrap Wrap mode.
* \param area Area to draw to.
* \param text Text to draw.
* \param len Text to draw length.
* \param data Optional parser data.
*/
void
draw_text(draw_context_t *ctx, font_t *font,
draw_text(draw_context_t *ctx, font_t *font, PangoEllipsizeMode ellip, PangoWrapMode wrap,
area_t area, const char *text, ssize_t len, draw_parser_data_t *pdata)
{
int x, y;
@ -392,8 +394,8 @@ draw_text(draw_context_t *ctx, font_t *font,
- (pdata->margin.left
+ pdata->margin.right)));
pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
pango_layout_set_ellipsize(ctx->layout, PANGO_ELLIPSIZE_END);
pango_layout_set_wrap(ctx->layout, PANGO_WRAP_WORD_CHAR);
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);
pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);

2
draw.h
View File

@ -177,7 +177,7 @@ typedef struct
} border;
} draw_parser_data_t;
void draw_text(draw_context_t *, font_t *, area_t, const char *, ssize_t len, draw_parser_data_t *);
void draw_text(draw_context_t *, font_t *, PangoEllipsizeMode, PangoWrapMode, area_t, const char *, ssize_t len, draw_parser_data_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

@ -258,7 +258,7 @@ mouse_infobox_draw(simple_window_t *sw, area_t geometry, int border)
len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
geometry.width, geometry.height, geometry.x, geometry.y);
draw_rectangle(&sw->ctx, draw_geometry, 1.0, true, &color_without_alpha);
draw_text(&sw->ctx, globalconf.font, draw_geometry, size, len, NULL);
draw_text(&sw->ctx, globalconf.font, PANGO_ELLIPSIZE_NONE, PANGO_WRAP_WORD, draw_geometry, size, len, NULL);
simplewindow_move(sw,
geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);

View File

@ -35,6 +35,8 @@ typedef struct
int width;
/** Extents */
int extents;
PangoEllipsizeMode ellip;
PangoWrapMode wrap;
/** Draw parser data */
draw_parser_data_t pdata;
} textbox_data_t;
@ -74,7 +76,7 @@ textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry,
int screen, wibox_t *p)
{
textbox_data_t *d = widget->data;
draw_text(ctx, globalconf.font, geometry, d->text, d->len, &d->pdata);
draw_text(ctx, globalconf.font, d->ellip, d->wrap, geometry, d->text, d->len, &d->pdata);
}
/** Delete a textbox widget.
@ -111,6 +113,34 @@ luaA_textbox_index(lua_State *L, awesome_token_t token)
case A_TK_WIDTH:
lua_pushnumber(L, d->width);
return 1;
case A_TK_WRAP:
switch(d->wrap)
{
default:
lua_pushliteral(L, "word");
break;
case A_TK_CHAR:
lua_pushliteral(L, "char");
break;
case A_TK_WORD_CHAR:
lua_pushliteral(L, "word_char");
break;
}
return 1;
case A_TK_ELLIPSIZE:
switch(d->ellip)
{
case A_TK_START:
lua_pushliteral(L, "start");
break;
case A_TK_MIDDLE:
lua_pushliteral(L, "middle");
break;
default:
lua_pushliteral(L, "end");
break;
}
return 1;
default:
return 0;
}
@ -155,6 +185,40 @@ luaA_textbox_newindex(lua_State *L, awesome_token_t token)
case A_TK_WIDTH:
d->width = luaL_checknumber(L, 3);
break;
case A_TK_WRAP:
if((buf = luaL_checklstring(L, 3, &len)))
switch(a_tokenize(buf, len))
{
case A_TK_WORD:
d->wrap = PANGO_WRAP_WORD;
break;
case A_TK_CHAR:
d->wrap = PANGO_WRAP_CHAR;
break;
case A_TK_WORD_CHAR:
d->wrap = PANGO_WRAP_WORD_CHAR;
break;
default:
break;
}
break;
case A_TK_ELLIPSIZE:
if((buf = luaL_checklstring(L, 3, &len)))
switch(a_tokenize(buf, len))
{
case A_TK_START:
d->ellip = PANGO_ELLIPSIZE_START;
break;
case A_TK_MIDDLE:
d->ellip = PANGO_ELLIPSIZE_MIDDLE;
break;
case A_TK_END:
d->ellip = PANGO_ELLIPSIZE_END;
break;
default:
break;
}
break;
default:
return 0;
}
@ -184,6 +248,7 @@ textbox_new(alignment_t align)
w->destructor = textbox_destructor;
w->geometry = textbox_geometry;
w->data = d = p_new(textbox_data_t, 1);
d->ellip = PANGO_ELLIPSIZE_END;
return w;
}