textbox.c 8.5 KB

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