2 * textbox.c - text box widget
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "common/tokenize.h"
25 extern awesome_t globalconf
;
27 /** The textbox private data structure */
32 /** Textbox text length */
38 PangoEllipsizeMode ellip
;
40 /** Draw parser data */
41 draw_parser_data_t pdata
;
45 textbox_geometry(widget_t
*widget
, int screen
, int height
, int width
)
48 textbox_data_t
*d
= widget
->data
;
50 geometry
.height
= height
;
53 geometry
.width
= d
->width
;
54 else if(widget
->align
== AlignFlex
)
55 geometry
.width
= width
;
58 geometry
.width
= MIN(d
->extents
, width
);
61 geometry
.width
= MAX(geometry
.width
,
62 d
->pdata
.bg_resize
? ((double) d
->pdata
.bg_image
->width
/ (double) d
->pdata
.bg_image
->height
) * geometry
.height
: d
->pdata
.bg_image
->width
);
68 /** Draw a textbox widget.
69 * \param widget The widget.
70 * \param ctx The draw context.
71 * \param screen The screen.
72 * \param p A pointer to the object we're draw onto.
75 textbox_draw(widget_t
*widget
, draw_context_t
*ctx
, area_t geometry
,
76 int screen
, wibox_t
*p
)
78 textbox_data_t
*d
= widget
->data
;
79 draw_text(ctx
, globalconf
.font
, d
->ellip
, d
->wrap
, geometry
, d
->text
, d
->len
, &d
->pdata
);
82 /** Delete a textbox widget.
83 * \param w The widget to destroy.
86 textbox_destructor(widget_t
*w
)
88 textbox_data_t
*d
= w
->data
;
89 draw_parser_data_wipe(&d
->pdata
);
95 * \param L The Lua VM state.
96 * \param token The key token.
97 * \return The number of elements pushed on stack.
99 * \lfield text The text to display.
100 * \lfield width The width of the textbox. Set to 0 for auto.
101 * \lfield wrap The wrap mode: word, char, word_char.
102 * \lfield ellipsize The ellipsize mode: start, middle or end.
105 luaA_textbox_index(lua_State
*L
, awesome_token_t token
)
107 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
108 textbox_data_t
*d
= (*widget
)->data
;
113 lua_pushstring(L
, d
->text
);
116 lua_pushnumber(L
, d
->width
);
122 lua_pushliteral(L
, "word");
125 lua_pushliteral(L
, "char");
128 lua_pushliteral(L
, "word_char");
136 lua_pushliteral(L
, "start");
139 lua_pushliteral(L
, "middle");
142 lua_pushliteral(L
, "end");
151 /** The __newindex method for a textbox object.
152 * \param L The Lua VM state.
153 * \param token The key token.
154 * \return The number of elements pushed on stack.
157 luaA_textbox_newindex(lua_State
*L
, awesome_token_t token
)
160 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
161 const char *buf
= NULL
;
162 textbox_data_t
*d
= (*widget
)->data
;
168 || (buf
= luaL_checklstring(L
, 3, &len
)))
171 draw_parser_data_wipe(&d
->pdata
);
172 /* reinit since we are giving it as arg to draw_text unconditionally */
173 draw_parser_data_init(&d
->pdata
);
180 a_iso2utf8(&d
->text
, buf
, len
);
181 d
->extents
= draw_text_extents(globalconf
.font
, d
->text
, d
->len
, &d
->pdata
).width
;
188 d
->width
= luaL_checknumber(L
, 3);
191 if((buf
= luaL_checklstring(L
, 3, &len
)))
192 switch(a_tokenize(buf
, len
))
195 d
->wrap
= PANGO_WRAP_WORD
;
198 d
->wrap
= PANGO_WRAP_CHAR
;
201 d
->wrap
= PANGO_WRAP_WORD_CHAR
;
208 if((buf
= luaL_checklstring(L
, 3, &len
)))
209 switch(a_tokenize(buf
, len
))
212 d
->ellip
= PANGO_ELLIPSIZE_START
;
215 d
->ellip
= PANGO_ELLIPSIZE_MIDDLE
;
218 d
->ellip
= PANGO_ELLIPSIZE_END
;
228 widget_invalidate_bywidget(*widget
);
233 /** Create a new textbox widget.
234 * \param align Widget alignment.
235 * \return A brand new widget.
238 textbox_new(alignment_t align
)
243 w
= p_new(widget_t
, 1);
244 widget_common_new(w
);
246 w
->align_supported
|= AlignFlex
;
247 w
->draw
= textbox_draw
;
248 w
->index
= luaA_textbox_index
;
249 w
->newindex
= luaA_textbox_newindex
;
250 w
->destructor
= textbox_destructor
;
251 w
->geometry
= textbox_geometry
;
252 w
->data
= d
= p_new(textbox_data_t
, 1);
253 d
->ellip
= PANGO_ELLIPSIZE_END
;
258 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80