naughty: icon_size added to config and notify()
[awesome.git] / widgets / textbox.c
blobab92f02b90e57a7c09db93c891cd77bb1bc8bfd2
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 "common/tokenize.h"
25 extern awesome_t globalconf;
27 /** The textbox private data structure */
28 typedef struct
30 /** Textbox text */
31 char *text;
32 /** Textbox text length */
33 size_t len;
34 /** Textbox width */
35 int width;
36 /** Extents */
37 int extents;
38 /** Draw parser data */
39 draw_parser_data_t pdata;
40 } textbox_data_t;
42 static area_t
43 textbox_geometry(widget_t *widget, int screen, int height, int width)
45 area_t geometry;
46 textbox_data_t *d = widget->data;
48 geometry.height = height;
50 if(d->width)
51 geometry.width = d->width;
52 else if(widget->align == AlignFlex)
53 geometry.width = width;
54 else
56 geometry.width = MIN(d->extents, width);
58 if(d->pdata.bg_image)
59 geometry.width = MAX(geometry.width,
60 d->pdata.bg_resize ? ((double) d->pdata.bg_image->width / (double) d->pdata.bg_image->height) * geometry.height : d->pdata.bg_image->width);
63 return geometry;
66 /** Draw a textbox widget.
67 * \param widget The widget.
68 * \param ctx The draw context.
69 * \param screen The screen.
70 * \param p A pointer to the object we're draw onto.
72 static void
73 textbox_draw(widget_t *widget, draw_context_t *ctx, area_t geometry,
74 int screen, wibox_t *p)
76 textbox_data_t *d = widget->data;
77 draw_text(ctx, globalconf.font, geometry, d->text, d->len, &d->pdata);
80 /** Delete a textbox widget.
81 * \param w The widget to destroy.
83 static void
84 textbox_destructor(widget_t *w)
86 textbox_data_t *d = w->data;
87 draw_parser_data_wipe(&d->pdata);
88 p_delete(&d->text);
89 p_delete(&d);
92 /** Textbox widget.
93 * \param L The Lua VM state.
94 * \param token The key token.
95 * \return The number of elements pushed on stack.
96 * \luastack
97 * \lfield text The text to display.
98 * \lfield width The width of the textbox. Set to 0 for auto.
100 static int
101 luaA_textbox_index(lua_State *L, awesome_token_t token)
103 widget_t **widget = luaA_checkudata(L, 1, "widget");
104 textbox_data_t *d = (*widget)->data;
106 switch(token)
108 case A_TK_TEXT:
109 lua_pushstring(L, d->text);
110 return 1;
111 case A_TK_WIDTH:
112 lua_pushnumber(L, d->width);
113 return 1;
114 default:
115 return 0;
119 /** The __newindex method for a textbox object.
120 * \param L The Lua VM state.
121 * \param token The key token.
122 * \return The number of elements pushed on stack.
124 static int
125 luaA_textbox_newindex(lua_State *L, awesome_token_t token)
127 size_t len = 0;
128 widget_t **widget = luaA_checkudata(L, 1, "widget");
129 const char *buf = NULL;
130 textbox_data_t *d = (*widget)->data;
132 switch(token)
134 case A_TK_TEXT:
135 if(lua_isnil(L, 3)
136 || (buf = luaL_checklstring(L, 3, &len)))
138 /* delete */
139 draw_parser_data_wipe(&d->pdata);
140 /* reinit since we are giving it as arg to draw_text unconditionally */
141 draw_parser_data_init(&d->pdata);
142 p_delete(&d->text);
144 /* re-init */
145 d->len = len;
146 if(buf)
148 a_iso2utf8(&d->text, buf, len);
149 d->extents = draw_text_extents(globalconf.default_screen,
150 globalconf.font, d->text, d->len, &d->pdata).width;
152 else
153 d->extents = 0;
155 break;
156 case A_TK_WIDTH:
157 d->width = luaL_checknumber(L, 3);
158 break;
159 default:
160 return 0;
163 widget_invalidate_bywidget(*widget);
165 return 0;
168 /** Create a new textbox widget.
169 * \param align Widget alignment.
170 * \return A brand new widget.
172 widget_t *
173 textbox_new(alignment_t align)
175 widget_t *w;
176 textbox_data_t *d;
178 w = p_new(widget_t, 1);
179 widget_common_new(w);
180 w->align = align;
181 w->align_supported |= AlignFlex;
182 w->draw = textbox_draw;
183 w->index = luaA_textbox_index;
184 w->newindex = luaA_textbox_newindex;
185 w->destructor = textbox_destructor;
186 w->geometry = textbox_geometry;
187 w->data = d = p_new(textbox_data_t, 1);
189 return w;
192 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80