textbox.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * File : textbox.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/rtgui_theme.h>
  16. #include <rtgui/rtgui_system.h>
  17. #include <rtgui/widgets/textbox.h>
  18. #ifndef _WIN32
  19. #include <ctype.h>
  20. #endif
  21. #define RTGUI_TEXTBOX_LINE_MAX 64
  22. #define RTGUI_TEXTBOX_MARGIN 3
  23. static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event);
  24. static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event);
  25. static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event);
  26. static void _rtgui_textbox_caret_timeout(struct rtgui_timer* timer, void* parameter)
  27. {
  28. rtgui_textbox_t* box;
  29. box = (rtgui_textbox_t*)parameter;
  30. /* set caret flag */
  31. if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
  32. box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
  33. else
  34. box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
  35. /* re-darw textbox */
  36. rtgui_theme_draw_textbox(box);
  37. return ;
  38. }
  39. static void _rtgui_textbox_constructor(rtgui_textbox_t *box)
  40. {
  41. rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
  42. rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);
  43. RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  44. rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_textbox_event_handler);
  45. rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus);
  46. rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus);
  47. /* set default text align */
  48. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(box)) = RTGUI_ALIGN_CENTER_VERTICAL;
  49. /* set proper of control */
  50. box->caret_timer = rtgui_timer_create(RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC,
  51. _rtgui_textbox_caret_timeout, box);
  52. box->line = box->line_begin = box->position = 0;
  53. box->flag = RTGUI_TEXTBOX_SINGLE;
  54. /* allocate default line buffer */
  55. box->text = RT_NULL;
  56. rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
  57. box->font_width = rtgui_rect_width(rect);
  58. }
  59. static void _rtgui_textbox_deconstructor(rtgui_textbox_t *box)
  60. {
  61. if (box->text != RT_NULL)
  62. {
  63. rt_free(box->text);
  64. box->text = RT_NULL;
  65. }
  66. rtgui_timer_destory(box->caret_timer);
  67. box->caret_timer = RT_NULL;
  68. }
  69. rtgui_type_t *rtgui_textbox_type_get(void)
  70. {
  71. static rtgui_type_t *textbox_type = RT_NULL;
  72. if (!textbox_type)
  73. {
  74. textbox_type = rtgui_type_create("textbox", RTGUI_WIDGET_TYPE,
  75. sizeof(rtgui_textbox_t),
  76. RTGUI_CONSTRUCTOR(_rtgui_textbox_constructor),
  77. RTGUI_DESTRUCTOR(_rtgui_textbox_deconstructor));
  78. }
  79. return textbox_type;
  80. }
  81. static void rtgui_textbox_onmouse(struct rtgui_textbox* box, struct rtgui_event_mouse* event)
  82. {
  83. rt_size_t length;
  84. RT_ASSERT(box != RT_NULL);
  85. RT_ASSERT(event != RT_NULL);
  86. length = rt_strlen((const char*)box->text);
  87. if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
  88. event->button & RTGUI_MOUSE_BUTTON_DOWN)
  89. {
  90. rt_int32_t x;
  91. /* set caret position */
  92. x = event->x - RTGUI_WIDGET(box)->extent.x1;
  93. if (x < 0)
  94. {
  95. box->position = 0;
  96. }
  97. else if (x > length * box->font_width)
  98. {
  99. box->position = length;
  100. }
  101. else
  102. {
  103. box->position = x / box->font_width;
  104. }
  105. /* set widget focus */
  106. rtgui_widget_focus(RTGUI_WIDGET(box));
  107. }
  108. }
  109. static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event)
  110. {
  111. rt_size_t length;
  112. RT_ASSERT(box != RT_NULL);
  113. RT_ASSERT(event != RT_NULL);
  114. if (event->type != RTGUI_KEYUP)
  115. return ;
  116. length = rt_strlen((const char*)box->text);
  117. if (event->key == RTGUIK_DELETE)
  118. {
  119. if (box->position == length - 1)
  120. {
  121. box->text[box->position] = '\0';
  122. }
  123. else
  124. {
  125. unsigned char *c;
  126. /* remove character */
  127. for (c = &box->text[box->position]; c[1] != '\0'; c++)
  128. *c = c[1];
  129. *c = '\0';
  130. }
  131. }
  132. else if (event->key == RTGUIK_BACKSPACE)
  133. {
  134. if (box->position == length - 1)
  135. {
  136. box->text[box->position] = '\0';
  137. box->position --;
  138. }
  139. else if (box->position != 0)
  140. {
  141. /* remove current character */
  142. if (box->position != 0)
  143. {
  144. unsigned char *c;
  145. /* remove character */
  146. for (c = &box->text[box->position - 1]; c[1] != '\0'; c++)
  147. *c = c[1];
  148. *c = '\0';
  149. }
  150. box->position --;
  151. }
  152. }
  153. else if (event->key == RTGUIK_LEFT)
  154. {
  155. if (box->position > 0) box->position --;
  156. }
  157. else if (event->key == RTGUIK_RIGHT)
  158. {
  159. if (box->position < length) box->position ++;
  160. }
  161. else if (event->key == RTGUIK_HOME)
  162. {
  163. box->position = 0;
  164. }
  165. else if (event->key == RTGUIK_END)
  166. {
  167. box->position = length;
  168. }
  169. else if (event->key == RTGUIK_RETURN)
  170. {
  171. if (box->on_enter != RT_NULL)
  172. {
  173. box->on_enter(RTGUI_WIDGET(box), RT_NULL);
  174. }
  175. }
  176. else
  177. {
  178. if (isprint(event->key) || isdigit(event->key))
  179. {
  180. /* no buffer on this line */
  181. if (length + 1 > box->line_length) return;
  182. if (box->position < length - 1)
  183. {
  184. unsigned char* c;
  185. for (c = &box->text[length]; c != &box->text[box->position]; c--)
  186. *c = *(c-1);
  187. box->text[length + 1] = '\0';
  188. }
  189. box->text[box->position] = event->key;
  190. box->position ++;
  191. }
  192. }
  193. /* set caret to show */
  194. box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
  195. /* re-draw text box */
  196. rtgui_theme_draw_textbox(box);
  197. }
  198. static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event)
  199. {
  200. struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
  201. /* start caret timer */
  202. rtgui_timer_start(box->caret_timer);
  203. return RT_TRUE;
  204. }
  205. static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event)
  206. {
  207. struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
  208. /* stop caret timer */
  209. rtgui_timer_stop(box->caret_timer);
  210. /* hide caret */
  211. return RT_TRUE;
  212. }
  213. rt_bool_t rtgui_textbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  214. {
  215. struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
  216. switch (event->type)
  217. {
  218. case RTGUI_EVENT_PAINT:
  219. #ifndef RTGUI_USING_SMALL_SIZE
  220. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  221. else
  222. #endif
  223. rtgui_theme_draw_textbox(box);
  224. break;
  225. case RTGUI_EVENT_MOUSE_BUTTON:
  226. #ifndef RTGUI_USING_SMALL_SIZE
  227. if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
  228. else
  229. #endif
  230. rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event);
  231. return RT_TRUE;
  232. case RTGUI_EVENT_KBD:
  233. #ifndef RTGUI_USING_SMALL_SIZE
  234. if (widget->on_key != RT_NULL) widget->on_key(widget, event);
  235. else
  236. #endif
  237. rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event);
  238. return RT_TRUE;
  239. }
  240. return RT_FALSE;
  241. }
  242. struct rtgui_textbox* rtgui_textbox_create(const char* text)
  243. {
  244. struct rtgui_textbox* box;
  245. box = (struct rtgui_textbox*) rtgui_widget_create (RTGUI_TEXTBOX_TYPE);
  246. if (box != RT_NULL)
  247. {
  248. rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
  249. /* allocate default line buffer */
  250. rtgui_textbox_set_value(box, text);
  251. rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
  252. }
  253. return box;
  254. }
  255. void rtgui_textbox_destroy(struct rtgui_textbox* box)
  256. {
  257. rtgui_widget_destroy(RTGUI_WIDGET(box));
  258. }
  259. void rtgui_textbox_set_value(struct rtgui_textbox* box, const char* text)
  260. {
  261. if (box->text != RT_NULL)
  262. {
  263. if (box->line_length > rt_strlen(text) + 1)
  264. {
  265. rt_memcpy(box->text, text, rt_strlen(text) + 1);
  266. return;
  267. }
  268. else
  269. {
  270. /* free the old text */
  271. rtgui_free(box->text);
  272. box->text = RT_NULL;
  273. }
  274. }
  275. box->line_length = RTGUI_TEXTBOX_LINE_MAX > rt_strlen(text) + 1 ?
  276. RTGUI_TEXTBOX_LINE_MAX : rt_strlen(text) + 1;
  277. /* allocate line buffer */
  278. box->text = rtgui_malloc(box->line_length);
  279. rt_memset(box->text, 0, box->line_length);
  280. /* copy text */
  281. rt_memcpy(box->text, text, rt_strlen(text) + 1);
  282. /* set current position */
  283. box->position = 0;
  284. }
  285. const char* rtgui_textbox_get_value(struct rtgui_textbox* box)
  286. {
  287. return (const char*)box->text;
  288. }
  289. void rtgui_widget_set_line_length(struct rtgui_textbox* box, rt_size_t length)
  290. {
  291. rt_uint8_t* new_line;
  292. RT_ASSERT(box != RT_NULL);
  293. /* invalid length */
  294. if (length <= 0) return;
  295. new_line = rtgui_malloc(length);
  296. if (length < box->line_length)
  297. {
  298. rt_memcpy(new_line, box->text, length - 1);
  299. new_line[length] = '\0';
  300. }
  301. else
  302. {
  303. rt_memcpy(new_line, (const char*)box->text, rt_strlen((const char*)box->text));
  304. }
  305. /* set line length */
  306. box->line_length = length;
  307. }