textbox.c 8.1 KB

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