textbox.c 8.7 KB

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