textbox.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_object* object, struct rtgui_event* event);
  26. static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object* object, 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_object_set_event_handler(RTGUI_OBJECT(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_object* object, struct rtgui_event* event)
  194. {
  195. struct rtgui_textbox* box;
  196. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  197. box = RTGUI_TEXTBOX(object);
  198. /* set caret to show */
  199. box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
  200. /* start caret timer */
  201. rtgui_timer_start(box->caret_timer);
  202. return RT_TRUE;
  203. }
  204. static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object* object, struct rtgui_event* event)
  205. {
  206. struct rtgui_textbox* box;
  207. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  208. box = RTGUI_TEXTBOX(object);
  209. /* stop caret timer */
  210. rtgui_timer_stop(box->caret_timer);
  211. /* set caret to hide */
  212. box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
  213. return RT_TRUE;
  214. }
  215. rt_bool_t rtgui_textbox_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  216. {
  217. struct rtgui_textbox* box;
  218. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  219. box = RTGUI_TEXTBOX(object);
  220. switch (event->type)
  221. {
  222. case RTGUI_EVENT_PAINT:
  223. #ifndef RTGUI_USING_SMALL_SIZE
  224. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  225. else
  226. #endif
  227. rtgui_theme_draw_textbox(box);
  228. break;
  229. case RTGUI_EVENT_MOUSE_BUTTON:
  230. if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
  231. #ifndef RTGUI_USING_SMALL_SIZE
  232. if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
  233. else
  234. #endif
  235. rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event);
  236. return RT_TRUE;
  237. case RTGUI_EVENT_KBD:
  238. if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
  239. #ifndef RTGUI_USING_SMALL_SIZE
  240. if (widget->on_key != RT_NULL) widget->on_key(widget, event);
  241. else
  242. #endif
  243. rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event);
  244. return RT_TRUE;
  245. }
  246. return RT_FALSE;
  247. }
  248. struct rtgui_textbox* rtgui_textbox_create(const char* text, rt_uint8_t flag)
  249. {
  250. struct rtgui_textbox* box;
  251. box = (struct rtgui_textbox*) rtgui_widget_create (RTGUI_TEXTBOX_TYPE);
  252. if (box != RT_NULL)
  253. {
  254. rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
  255. /* allocate default line buffer */
  256. rtgui_textbox_set_value(box, text);
  257. box->flag = flag;
  258. rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
  259. }
  260. return box;
  261. }
  262. void rtgui_textbox_destroy(struct rtgui_textbox* box)
  263. {
  264. rtgui_widget_destroy(RTGUI_WIDGET(box));
  265. }
  266. void rtgui_textbox_set_value(struct rtgui_textbox* box, const char* text)
  267. {
  268. if (box->text != RT_NULL)
  269. {
  270. if (box->line_length > rt_strlen(text) + 1)
  271. {
  272. rt_memcpy(box->text, text, rt_strlen(text) + 1);
  273. /* set current position */
  274. box->position = 0;
  275. return;
  276. }
  277. else
  278. {
  279. /* free the old text */
  280. rtgui_free(box->text);
  281. box->text = RT_NULL;
  282. }
  283. }
  284. box->line_length = RTGUI_TEXTBOX_LINE_MAX > rt_strlen(text) + 1 ?
  285. RTGUI_TEXTBOX_LINE_MAX : rt_strlen(text) + 1;
  286. /* allocate line buffer */
  287. box->text = rtgui_malloc(box->line_length);
  288. rt_memset(box->text, 0, box->line_length);
  289. /* copy text */
  290. rt_memcpy(box->text, text, rt_strlen(text) + 1);
  291. /* set current position */
  292. box->position = 0;
  293. }
  294. const char* rtgui_textbox_get_value(struct rtgui_textbox* box)
  295. {
  296. return (const char*)box->text;
  297. }
  298. void rtgui_textbox_set_line_length(struct rtgui_textbox* box, rt_size_t length)
  299. {
  300. rt_uint8_t* new_line;
  301. RT_ASSERT(box != RT_NULL);
  302. /* invalid length */
  303. if (length <= 0) return;
  304. new_line = rtgui_malloc(length);
  305. if (length < box->line_length)
  306. {
  307. rt_memcpy(new_line, box->text, length - 1);
  308. new_line[length] = '\0';
  309. }
  310. else
  311. {
  312. rt_memcpy(new_line, (const char*)box->text, rt_strlen((const char*)box->text));
  313. }
  314. /* set line length */
  315. box->line_length = length;
  316. }