awful.widget.taglist: use attached_add_signal
[awesome.git] / widgets / textbox.c
blob269167b4314eceef3dbce1db77fa304acdbbddcf
1 /*
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.
22 #include "widget.h"
23 #include "wibox.h"
24 #include "luaa.h"
25 #include "common/tokenize.h"
27 /** The textbox private data structure */
28 typedef struct
30 draw_text_context_t data;
31 /** Textbox width */
32 int width;
33 /** Extents */
34 area_t extents;
35 PangoEllipsizeMode ellip;
36 PangoWrapMode wrap;
37 /** Border */
38 struct
40 int width;
41 color_t color;
42 } border;
43 /** Text alignment */
44 alignment_t align;
45 /** Margin */
46 padding_t margin;
47 /** Background color */
48 color_t bg;
49 /** Background image */
50 image_t *bg_image;
51 /** Background resize to wibox height. */
52 bool bg_resize;
53 /** Background alignment */
54 alignment_t bg_align;
55 } textbox_data_t;
57 static area_t
58 textbox_geometry(widget_t *widget, int screen)
60 textbox_data_t *d = widget->data;
61 area_t geometry = d->extents;
62 geometry.width += d->margin.left + d->margin.left;
63 geometry.height += d->margin.bottom + d->margin.top;
65 if(d->bg_image)
67 int bgi_height = image_getheight(d->bg_image);
68 int bgi_width = image_getwidth(d->bg_image);
69 double ratio = d->bg_resize ? (double) geometry.height / bgi_height : 1;
70 geometry.width = MAX(d->extents.width + d->margin.left + d->margin.right, MAX(d->width, bgi_width * ratio));
73 geometry.x = geometry.y = 0;
75 return geometry;
78 static area_t
79 textbox_extents(lua_State *L, widget_t *widget)
81 return textbox_geometry(widget, 0);
84 /** Draw a textbox widget.
85 * \param widget The widget.
86 * \param ctx The draw context.
87 * \param p A pointer to the object we're draw onto.
89 static void
90 textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry, wibox_t *p)
92 textbox_data_t *d = widget->data;
94 if(d->bg.initialized)
95 draw_rectangle(ctx, geometry, 1.0, true, &d->bg);
97 if(d->border.width > 0)
98 draw_rectangle(ctx, geometry, d->border.width, false, &d->border.color);
100 if(d->bg_image)
102 int bgi_height = image_getheight(d->bg_image);
103 int bgi_width = image_getwidth(d->bg_image);
104 double ratio = d->bg_resize ? (double) geometry.height / bgi_height : 1;
105 /* check there is enough space to draw the image */
106 if(ratio * bgi_width <= geometry.width)
108 int x = geometry.x;
109 int y = geometry.y;
110 switch(d->bg_align)
112 case AlignCenter:
113 x += (geometry.width - bgi_width * ratio) / 2;
114 break;
115 case AlignRight:
116 x += geometry.width - bgi_width * ratio;
117 break;
118 case AlignBottom:
119 y += geometry.height - bgi_height * ratio;
120 break;
121 case AlignMiddle:
122 y += (geometry.height - bgi_height * ratio) / 2;
123 break;
124 default:
125 break;
127 draw_image(ctx, x, y, ratio, d->bg_image);
131 geometry.y += (geometry.height - textbox_geometry(widget, ctx->phys_screen).height) / 2;
132 draw_text(ctx, &d->data, d->ellip, d->wrap, d->align, &d->margin, geometry, &d->extents);
135 /** Delete a textbox widget.
136 * \param w The widget to destroy.
138 static void
139 textbox_destructor(widget_t *w)
141 textbox_data_t *d = w->data;
142 draw_text_context_wipe(&d->data);
143 p_delete(&d);
146 static int
147 luaA_textbox_margin(lua_State *L)
149 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
150 textbox_data_t *d = widget->data;
152 if(lua_gettop(L) == 2)
154 d->margin = luaA_getopt_padding(L, 2, &d->margin);
155 widget_invalidate_bywidget(widget);
158 return luaA_pushpadding(L, &d->margin);
161 /** Textbox widget.
162 * \param L The Lua VM state.
163 * \param token The key token.
164 * \return The number of elements pushed on stack.
165 * \luastack
166 * \lfield text The text to display.
167 * \lfield width The width of the textbox. Set to 0 for auto.
168 * \lfield wrap The wrap mode: word, char, word_char.
169 * \lfield ellipsize The ellipsize mode: start, middle or end.
170 * \lfield border_width The border width to draw around.
171 * \lfield border_color The border color.
172 * \lfield align Text alignment, left, center or right.
173 * \lfield margin Method to pass text margin: a table with top, left, right and bottom keys.
174 * \lfield bg Background color.
175 * \lfield bg_image Background image.
176 * \lfield bg_align Background image alignment, left, center, right, bottom, top or middle
177 * \lfield bg_resize Background resize.
179 static int
180 luaA_textbox_index(lua_State *L, awesome_token_t token)
182 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
183 textbox_data_t *d = widget->data;
185 switch(token)
187 case A_TK_BG_RESIZE:
188 lua_pushboolean(L, d->bg_resize);
189 return 1;
190 case A_TK_BG_ALIGN:
191 lua_pushstring(L, draw_align_tostr(d->bg_align));
192 return 1;
193 case A_TK_BG_IMAGE:
194 return luaA_object_push(L, d->bg_image);
195 case A_TK_BG:
196 return luaA_pushcolor(L, &d->bg);
197 case A_TK_MARGIN:
198 lua_pushcfunction(L, luaA_textbox_margin);
199 return 1;
200 case A_TK_ALIGN:
201 lua_pushstring(L, draw_align_tostr(d->align));
202 return 1;
203 case A_TK_BORDER_WIDTH:
204 lua_pushnumber(L, d->border.width);
205 return 1;
206 case A_TK_BORDER_COLOR:
207 luaA_pushcolor(L, &d->border.color);
208 return 1;
209 case A_TK_TEXT:
210 if(d->data.len > 0)
212 lua_pushlstring(L, d->data.text, d->data.len);
213 return 1;
215 return 0;
216 case A_TK_WIDTH:
217 lua_pushnumber(L, d->width);
218 return 1;
219 case A_TK_WRAP:
220 switch(d->wrap)
222 default:
223 lua_pushliteral(L, "word");
224 break;
225 case A_TK_CHAR:
226 lua_pushliteral(L, "char");
227 break;
228 case A_TK_WORD_CHAR:
229 lua_pushliteral(L, "word_char");
230 break;
232 return 1;
233 case A_TK_ELLIPSIZE:
234 switch(d->ellip)
236 case A_TK_START:
237 lua_pushliteral(L, "start");
238 break;
239 case A_TK_MIDDLE:
240 lua_pushliteral(L, "middle");
241 break;
242 default:
243 lua_pushliteral(L, "end");
244 break;
246 return 1;
247 default:
248 return 0;
252 /** The __newindex method for a textbox object.
253 * \param L The Lua VM state.
254 * \param token The key token.
255 * \return The number of elements pushed on stack.
257 static int
258 luaA_textbox_newindex(lua_State *L, awesome_token_t token)
260 size_t len = 0;
261 widget_t *widget = luaA_checkudata(L, 1, &widget_class);
262 const char *buf = NULL;
263 textbox_data_t *d = widget->data;
265 switch(token)
267 case A_TK_BG_ALIGN:
268 buf = luaL_checklstring(L, 3, &len);
269 d->bg_align = draw_align_fromstr(buf, len);
270 break;
271 case A_TK_BG_RESIZE:
272 d->bg_resize = luaA_checkboolean(L, 3);
273 break;
274 case A_TK_BG_IMAGE:
275 luaA_object_unref_item(L, 1, d->bg_image);
276 d->bg_image = luaA_object_ref_item(L, 1, 3);
277 break;
278 case A_TK_BG:
279 if(lua_isnil(L, 3))
280 p_clear(&d->bg, 1);
281 else if((buf = luaL_checklstring(L, 3, &len)))
282 color_init_reply(color_init_unchecked(&d->bg, buf, len));
283 break;
284 case A_TK_ALIGN:
285 if((buf = luaL_checklstring(L, 3, &len)))
286 d->align = draw_align_fromstr(buf, len);
287 break;
288 case A_TK_BORDER_COLOR:
289 if((buf = luaL_checklstring(L, 3, &len)))
290 color_init_reply(color_init_unchecked(&d->border.color, buf, len));
291 break;
292 case A_TK_BORDER_WIDTH:
293 d->border.width = luaL_checknumber(L, 3);
294 break;
295 case A_TK_TEXT:
296 if(lua_isnil(L, 3)
297 || (buf = luaL_checklstring(L, 3, &len)))
299 /* delete */
300 draw_text_context_wipe(&d->data);
301 p_clear(&d->data, 1);
303 if(buf)
305 char *text;
306 ssize_t tlen;
307 /* if text has been converted to UTF-8 */
308 if(draw_iso2utf8(buf, len, &text, &tlen))
310 draw_text_context_init(&d->data, text, tlen);
311 p_delete(&text);
313 else
314 draw_text_context_init(&d->data, buf, len);
316 d->extents = draw_text_extents(&d->data);
318 else
319 p_clear(&d->extents, 1);
321 break;
322 case A_TK_WIDTH:
323 d->width = luaL_checknumber(L, 3);
324 break;
325 case A_TK_WRAP:
326 if((buf = luaL_checklstring(L, 3, &len)))
327 switch(a_tokenize(buf, len))
329 case A_TK_WORD:
330 d->wrap = PANGO_WRAP_WORD;
331 break;
332 case A_TK_CHAR:
333 d->wrap = PANGO_WRAP_CHAR;
334 break;
335 case A_TK_WORD_CHAR:
336 d->wrap = PANGO_WRAP_WORD_CHAR;
337 break;
338 default:
339 break;
341 break;
342 case A_TK_ELLIPSIZE:
343 if((buf = luaL_checklstring(L, 3, &len)))
344 switch(a_tokenize(buf, len))
346 case A_TK_START:
347 d->ellip = PANGO_ELLIPSIZE_START;
348 break;
349 case A_TK_MIDDLE:
350 d->ellip = PANGO_ELLIPSIZE_MIDDLE;
351 break;
352 case A_TK_END:
353 d->ellip = PANGO_ELLIPSIZE_END;
354 break;
355 default:
356 break;
358 break;
359 default:
360 return 0;
363 widget_invalidate_bywidget(widget);
365 return 0;
368 /** Create a new textbox widget.
369 * \param w The widget to initialize.
370 * \return A brand new widget.
372 widget_t *
373 widget_textbox(widget_t *w)
375 w->draw = textbox_draw;
376 w->index = luaA_textbox_index;
377 w->newindex = luaA_textbox_newindex;
378 w->destructor = textbox_destructor;
379 w->geometry = textbox_geometry;
380 w->extents = textbox_extents;
382 textbox_data_t *d = w->data = p_new(textbox_data_t, 1);
383 d->ellip = PANGO_ELLIPSIZE_END;
385 return w;
388 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80