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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/*
* File : textbox.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2009-10-16 Bernard first version
* 2011-01-224 Bernard fix backspace issue.
*/
#include <rtgui/dc.h>
#include <rtgui/rtgui_theme.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/textbox.h>
#ifndef _WIN32
#include <ctype.h>
#endif
#define RTGUI_TEXTBOX_LINE_MAX 64
#define RTGUI_TEXTBOX_MARGIN 3
static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event);
static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event);
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event);
static void _rtgui_textbox_caret_timeout(struct rtgui_timer* timer, void* parameter)
{
rtgui_textbox_t* box;
box = (rtgui_textbox_t*)parameter;
/* set caret flag */
if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
else
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
/* re-darw textbox */
rtgui_theme_draw_textbox(box);
return ;
}
static void _rtgui_textbox_constructor(rtgui_textbox_t *box)
{
rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);
RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_textbox_event_handler);
rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus);
rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus);
/* set default text align */
RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(box)) = RTGUI_ALIGN_CENTER_VERTICAL;
/* set proper of control */
box->caret_timer = rtgui_timer_create(RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC,
_rtgui_textbox_caret_timeout, box);
box->line = box->line_begin = box->position = 0;
box->flag = RTGUI_TEXTBOX_SINGLE;
/* allocate default line buffer */
box->text = RT_NULL;
rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
box->font_width = rtgui_rect_width(rect);
}
static void _rtgui_textbox_deconstructor(rtgui_textbox_t *box)
{
if (box->text != RT_NULL)
{
rt_free(box->text);
box->text = RT_NULL;
}
rtgui_timer_destory(box->caret_timer);
box->caret_timer = RT_NULL;
}
DEFINE_CLASS_TYPE(textbox, "textbox",
RTGUI_WIDGET_TYPE,
_rtgui_textbox_constructor,
_rtgui_textbox_deconstructor,
sizeof(struct rtgui_textbox));
static void rtgui_textbox_onmouse(struct rtgui_textbox* box, struct rtgui_event_mouse* event)
{
rt_size_t length;
RT_ASSERT(box != RT_NULL);
RT_ASSERT(event != RT_NULL);
length = rt_strlen((const char*)box->text);
if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
event->button & RTGUI_MOUSE_BUTTON_DOWN)
{
rt_int32_t x;
/* set caret position */
x = event->x - RTGUI_WIDGET(box)->extent.x1;
if (x < 0)
{
box->position = 0;
}
else if (x > length * box->font_width)
{
box->position = length;
}
else
{
box->position = x / box->font_width;
}
/* set caret to show */
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
/* set widget focus */
rtgui_widget_focus(RTGUI_WIDGET(box));
/* re-draw text box */
rtgui_theme_draw_textbox(box);
}
}
static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event)
{
rt_size_t length;
RT_ASSERT(box != RT_NULL);
RT_ASSERT(event != RT_NULL);
if (event->type != RTGUI_KEYUP)
return ;
length = rt_strlen((const char*)box->text);
if (event->key == RTGUIK_DELETE)
{
if (box->position == length - 1)
{
box->text[box->position] = '\0';
}
else
{
char *c;
/* remove character */
for (c = &box->text[box->position]; c[1] != '\0'; c++)
*c = c[1];
*c = '\0';
}
}
else if (event->key == RTGUIK_BACKSPACE)
{
if ((box->position == length - 1) && length != 1)
{
box->text[box->position] = '\0';
box->position --;
}
else if (box->position != 0)
{
/* remove current character */
char *c;
/* remove character */
for (c = &box->text[box->position - 1]; c[1] != '\0'; c++)
*c = c[1];
*c = '\0';
box->position --;
}
}
else if (event->key == RTGUIK_LEFT)
{
if (box->position > 0) box->position --;
}
else if (event->key == RTGUIK_RIGHT)
{
if (box->position < length) box->position ++;
}
else if (event->key == RTGUIK_HOME)
{
box->position = 0;
}
else if (event->key == RTGUIK_END)
{
box->position = length;
}
else if (event->key == RTGUIK_RETURN)
{
if (box->on_enter != RT_NULL)
{
box->on_enter(RTGUI_WIDGET(box), RT_NULL);
}
}
else
{
if (isprint(event->key) || isdigit(event->key))
{
/* no buffer on this line */
if (length + 1 > box->line_length) return;
if (box->position < length - 1)
{
char* c;
for (c = &box->text[length]; c != &box->text[box->position]; c--)
*c = *(c-1);
box->text[length + 1] = '\0';
}
box->text[box->position] = event->key;
box->position ++;
}
}
/* set caret to show */
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
/* re-draw text box */
rtgui_theme_draw_textbox(box);
}
static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event)
{
struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
/* set caret to show */
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
/* start caret timer */
rtgui_timer_start(box->caret_timer);
return RT_TRUE;
}
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event)
{
struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
/* stop caret timer */
rtgui_timer_stop(box->caret_timer);
/* set caret to hide */
box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
return RT_TRUE;
}
rt_bool_t rtgui_textbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
switch (event->type)
{
case RTGUI_EVENT_PAINT:
#ifndef RTGUI_USING_SMALL_SIZE
if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
else
#endif
rtgui_theme_draw_textbox(box);
break;
case RTGUI_EVENT_MOUSE_BUTTON:
if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
#ifndef RTGUI_USING_SMALL_SIZE
if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
else
#endif
rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event);
return RT_TRUE;
case RTGUI_EVENT_KBD:
if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
#ifndef RTGUI_USING_SMALL_SIZE
if (widget->on_key != RT_NULL) widget->on_key(widget, event);
else
#endif
rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event);
return RT_TRUE;
}
return RT_FALSE;
}
struct rtgui_textbox* rtgui_textbox_create(const char* text, rt_uint8_t flag)
{
struct rtgui_textbox* box;
box = (struct rtgui_textbox*) rtgui_widget_create (RTGUI_TEXTBOX_TYPE);
if (box != RT_NULL)
{
rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
/* allocate default line buffer */
rtgui_textbox_set_value(box, text);
box->flag = flag;
rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
}
return box;
}
void rtgui_textbox_destroy(struct rtgui_textbox* box)
{
rtgui_widget_destroy(RTGUI_WIDGET(box));
}
void rtgui_textbox_set_value(struct rtgui_textbox* box, const char* text)
{
if (box->text != RT_NULL)
{
if (box->line_length > rt_strlen(text) + 1)
{
rt_memcpy(box->text, text, rt_strlen(text) + 1);
/* set current position */
box->position = 0;
return;
}
else
{
/* free the old text */
rtgui_free(box->text);
box->text = RT_NULL;
}
}
box->line_length = RTGUI_TEXTBOX_LINE_MAX > rt_strlen(text) + 1 ?
RTGUI_TEXTBOX_LINE_MAX : rt_strlen(text) + 1;
/* allocate line buffer */
box->text = rtgui_malloc(box->line_length);
rt_memset(box->text, 0, box->line_length);
/* copy text */
rt_memcpy(box->text, text, rt_strlen(text) + 1);
/* set current position */
box->position = 0;
}
const char* rtgui_textbox_get_value(struct rtgui_textbox* box)
{
return (const char*)box->text;
}
void rtgui_textbox_set_line_length(struct rtgui_textbox* box, rt_size_t length)
{
rt_uint8_t* new_line;
RT_ASSERT(box != RT_NULL);
/* invalid length */
if (length <= 0) return;
new_line = rtgui_malloc(length);
if (length < box->line_length)
{
rt_memcpy(new_line, box->text, length - 1);
new_line[length] = '\0';
}
else
{
rt_memcpy(new_line, (const char*)box->text, rt_strlen((const char*)box->text));
}
/* set line length */
box->line_length = length;
}
|