Repository navigation
Expand file tree
/
Copy pathtextbox.c
More file actions
258 lines (237 loc) · 6.79 KB
/
Copy pathtextbox.c
File metadata and controls
258 lines (237 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* textbox.c - text box widget
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "widget.h"
#include "common/tokenize.h"
extern awesome_t globalconf;
/** The textbox private data structure */
typedef struct
{
/** Textbox text */
char *text;
/** Textbox text length */
size_t len;
/** Textbox width */
int width;
/** Extents */
int extents;
PangoEllipsizeMode ellip;
PangoWrapMode wrap;
/** Draw parser data */
draw_parser_data_t pdata;
} textbox_data_t;
static area_t
textbox_geometry(widget_t *widget, int screen, int height, int width)
{
area_t geometry;
textbox_data_t *d = widget->data;
geometry.height = height;
if(d->width)
geometry.width = d->width;
else if(widget->align == AlignFlex)
geometry.width = width;
else
{
geometry.width = MIN(d->extents, width);
if(d->pdata.bg_image)
geometry.width = MAX(geometry.width,
d->pdata.bg_resize ? ((double) d->pdata.bg_image->width / (double) d->pdata.bg_image->height) * geometry.height : d->pdata.bg_image->width);
}
return geometry;
}
/** Draw a textbox widget.
* \param widget The widget.
* \param ctx The draw context.
* \param screen The screen.
* \param p A pointer to the object we're draw onto.
*/
static void
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, d->ellip, d->wrap, geometry, d->text, d->len, &d->pdata);
}
/** Delete a textbox widget.
* \param w The widget to destroy.
*/
static void
textbox_destructor(widget_t *w)
{
textbox_data_t *d = w->data;
draw_parser_data_wipe(&d->pdata);
p_delete(&d->text);
p_delete(&d);
}
/** Textbox widget.
* \param L The Lua VM state.
* \param token The key token.
* \return The number of elements pushed on stack.
* \luastack
* \lfield text The text to display.
* \lfield width The width of the textbox. Set to 0 for auto.
* \lfield wrap The wrap mode: word, char, word_char.
* \lfield ellipsize The ellipsize mode: start, middle or end.
*/
static int
luaA_textbox_index(lua_State *L, awesome_token_t token)
{
widget_t **widget = luaA_checkudata(L, 1, "widget");
textbox_data_t *d = (*widget)->data;
switch(token)
{
case A_TK_TEXT:
lua_pushstring(L, d->text);
return 1;
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;
}
}
/** The __newindex method for a textbox object.
* \param L The Lua VM state.
* \param token The key token.
* \return The number of elements pushed on stack.
*/
static int
luaA_textbox_newindex(lua_State *L, awesome_token_t token)
{
size_t len = 0;
widget_t **widget = luaA_checkudata(L, 1, "widget");
const char *buf = NULL;
textbox_data_t *d = (*widget)->data;
switch(token)
{
case A_TK_TEXT:
if(lua_isnil(L, 3)
|| (buf = luaL_checklstring(L, 3, &len)))
{
/* delete */
draw_parser_data_wipe(&d->pdata);
/* reinit since we are giving it as arg to draw_text unconditionally */
draw_parser_data_init(&d->pdata);
p_delete(&d->text);
/* re-init */
d->len = len;
if(buf)
{
a_iso2utf8(&d->text, buf, len);
d->extents = draw_text_extents(globalconf.font, d->text, d->len, &d->pdata).width;
}
else
d->extents = 0;
}
break;
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;
}
widget_invalidate_bywidget(*widget);
return 0;
}
/** Create a new textbox widget.
* \param align Widget alignment.
* \return A brand new widget.
*/
widget_t *
textbox_new(alignment_t align)
{
widget_t *w;
textbox_data_t *d;
w = p_new(widget_t, 1);
widget_common_new(w);
w->align = align;
w->align_supported |= AlignFlex;
w->draw = textbox_draw;
w->index = luaA_textbox_index;
w->newindex = luaA_textbox_newindex;
w->destructor = textbox_destructor;
w->geometry = textbox_geometry;
w->data = d = p_new(textbox_data_t, 1);
d->ellip = PANGO_ELLIPSIZE_END;
return w;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80