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 /** Draw parser data */
39 draw_parser_data_t pdata
;
43 textbox_geometry(widget_t
*widget
, int screen
, int height
, int width
)
46 textbox_data_t
*d
= widget
->data
;
48 geometry
.height
= height
;
51 geometry
.width
= d
->width
;
52 else if(widget
->align
== AlignFlex
)
53 geometry
.width
= width
;
56 geometry
.width
= MIN(d
->extents
, width
);
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
);
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.
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.
84 textbox_destructor(widget_t
*w
)
86 textbox_data_t
*d
= w
->data
;
87 draw_parser_data_wipe(&d
->pdata
);
93 * \param L The Lua VM state.
94 * \param token The key token.
95 * \return The number of elements pushed on stack.
97 * \lfield text The text to display.
98 * \lfield width The width of the textbox. Set to 0 for auto.
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
;
109 lua_pushstring(L
, d
->text
);
112 lua_pushnumber(L
, d
->width
);
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.
125 luaA_textbox_newindex(lua_State
*L
, awesome_token_t token
)
128 widget_t
**widget
= luaA_checkudata(L
, 1, "widget");
129 const char *buf
= NULL
;
130 textbox_data_t
*d
= (*widget
)->data
;
136 || (buf
= luaL_checklstring(L
, 3, &len
)))
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
);
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
;
157 d
->width
= luaL_checknumber(L
, 3);
163 widget_invalidate_bywidget(*widget
);
168 /** Create a new textbox widget.
169 * \param align Widget alignment.
170 * \return A brand new widget.
173 textbox_new(alignment_t align
)
178 w
= p_new(widget_t
, 1);
179 widget_common_new(w
);
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);
192 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80