listbox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * File : listbox.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, 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. * 2010-01-06 Bernard first version
  13. */
  14. #include <rtgui/rtgui_theme.h>
  15. #include <rtgui/widgets/listbox.h>
  16. #define LIST_MARGIN 5
  17. static rt_bool_t rtgui_listbox_onunfocus(struct rtgui_object* object, rtgui_event_t* event);
  18. static void _rtgui_listbox_constructor(struct rtgui_listbox *box)
  19. {
  20. /* set default widget rect and set event handler */
  21. rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_listbox_event_handler);
  22. rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_listbox_onunfocus);
  23. RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  24. box->current_item = -1;
  25. box->items_count = 0;
  26. box->page_items = 1;
  27. box->on_item = 0;
  28. RTGUI_WIDGET_BACKGROUND(box) = white;
  29. RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL;
  30. }
  31. DEFINE_CLASS_TYPE(listbox, "listbox",
  32. RTGUI_WIDGET_TYPE,
  33. _rtgui_listbox_constructor,
  34. RT_NULL,
  35. sizeof(struct rtgui_listbox));
  36. void rtgui_listbox_ondraw(struct rtgui_listbox* box)
  37. {
  38. struct rtgui_dc* dc;
  39. rt_uint16_t page_index, index;
  40. const struct rtgui_listbox_item* item;
  41. struct rtgui_rect rect, item_rect, image_rect;
  42. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  43. if (dc == RT_NULL) return;
  44. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  45. rtgui_dc_fill_rect(dc, &rect);
  46. rect.x2 -= 1; rect.y2 -= 1;
  47. /* draw focused border */
  48. if (RTGUI_WIDGET_IS_FOCUSED(box))
  49. rtgui_dc_draw_focus_rect(dc, &rect);
  50. /* get item base rect */
  51. item_rect = rect;
  52. item_rect.x1 += 1; item_rect.x2 -= 1;
  53. item_rect.y1 += 2;
  54. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  55. /* get current page */
  56. if (box->current_item == -1)
  57. page_index = 0;
  58. else
  59. page_index = (box->current_item / box->page_items) * box->page_items;
  60. for (index = 0; index < box->page_items; index ++)
  61. {
  62. if (page_index + index >= box->items_count) break;
  63. item = &(box->items[page_index + index]);
  64. if (page_index + index == box->current_item)
  65. {
  66. rtgui_theme_draw_selected(dc, &item_rect);
  67. }
  68. item_rect.x1 += LIST_MARGIN;
  69. if (item->image != RT_NULL)
  70. {
  71. rtgui_image_get_rect(item->image, &image_rect);
  72. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  73. rtgui_image_blit(item->image, dc, &image_rect);
  74. item_rect.x1 += item->image->w + 2;
  75. }
  76. /* draw text */
  77. rtgui_dc_draw_text(dc, item->name, &item_rect);
  78. if (item->image != RT_NULL)
  79. item_rect.x1 -= (item->image->w + 2);
  80. item_rect.x1 -= LIST_MARGIN;
  81. /* move to next item position */
  82. item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
  83. item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
  84. }
  85. rtgui_dc_end_drawing(dc);
  86. }
  87. RTM_EXPORT(rtgui_listbox_ondraw);
  88. static void rtgui_listbox_update_current(struct rtgui_listbox* box, rt_int16_t old_item)
  89. {
  90. struct rtgui_dc* dc;
  91. const struct rtgui_listbox_item* item;
  92. rtgui_rect_t rect, item_rect, image_rect;
  93. if ((old_item == -1) || (old_item/box->page_items != box->current_item/box->page_items))
  94. {
  95. /* it's not a same page, update all */
  96. rtgui_widget_update(RTGUI_WIDGET(box));
  97. return;
  98. }
  99. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  100. if (dc == RT_NULL) return;
  101. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  102. rect.x2 -= 1; rect.y2 -= 1;
  103. item_rect = rect;
  104. /* get old item's rect */
  105. item_rect.x1 += 1; item_rect.x2 -= 1;
  106. item_rect.y1 += 2;
  107. item_rect.y1 += (old_item % box->page_items) * (2 + rtgui_theme_get_selected_height());
  108. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  109. /* draw old item */
  110. rtgui_dc_fill_rect(dc, &item_rect);
  111. item_rect.x1 += LIST_MARGIN;
  112. item = &(box->items[old_item]);
  113. if (item->image != RT_NULL)
  114. {
  115. rtgui_image_get_rect(item->image, &image_rect);
  116. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  117. rtgui_image_blit(item->image, dc, &image_rect);
  118. item_rect.x1 += item->image->w + 2;
  119. }
  120. rtgui_dc_draw_text(dc, item->name, &item_rect);
  121. /* draw current item */
  122. item_rect = rect;
  123. /* get current item's rect */
  124. item_rect.x1 += 1; item_rect.x2 -= 1;
  125. item_rect.y1 += 2;
  126. item_rect.y1 += (box->current_item % box->page_items) * (2 + rtgui_theme_get_selected_height());
  127. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  128. /* draw current item */
  129. rtgui_theme_draw_selected(dc, &item_rect);
  130. item_rect.x1 += LIST_MARGIN;
  131. item = &(box->items[box->current_item]);
  132. if (item->image != RT_NULL)
  133. {
  134. rtgui_image_get_rect(item->image, &image_rect);
  135. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  136. rtgui_image_blit(item->image, dc, &image_rect);
  137. item_rect.x1 += (item->image->w + 2);
  138. }
  139. rtgui_dc_draw_text(dc, item->name, &item_rect);
  140. rtgui_dc_end_drawing(dc);
  141. }
  142. rt_bool_t rtgui_listbox_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  143. {
  144. struct rtgui_listbox* box;
  145. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  146. box = RTGUI_LISTBOX(object);
  147. switch (event->type)
  148. {
  149. case RTGUI_EVENT_PAINT:
  150. rtgui_listbox_ondraw(box);
  151. return RT_FALSE;
  152. case RTGUI_EVENT_RESIZE:
  153. {
  154. struct rtgui_event_resize* resize;
  155. resize = (struct rtgui_event_resize*)event;
  156. /* recalculate page items */
  157. box->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  158. }
  159. break;
  160. case RTGUI_EVENT_MOUSE_BUTTON:
  161. {
  162. rtgui_rect_t rect;
  163. struct rtgui_event_mouse* emouse;
  164. emouse = (struct rtgui_event_mouse*)event;
  165. /* calculate selected item */
  166. /* get physical extent information */
  167. rtgui_widget_get_rect(widget, &rect);
  168. rtgui_widget_rect_to_device(widget, &rect);
  169. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (box->items_count > 0))
  170. {
  171. rt_uint16_t index;
  172. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  173. /* set focus */
  174. rtgui_widget_focus(widget);
  175. {
  176. struct rtgui_rect rect;
  177. struct rtgui_dc* dc;
  178. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  179. if (dc != RT_NULL)
  180. {
  181. /* get widget rect */
  182. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  183. /* update focus border */
  184. rect.x2 -= 1; rect.y2 -= 1;
  185. /* draw focused border */
  186. if (RTGUI_WIDGET_IS_FOCUSED(box))
  187. rtgui_dc_draw_focus_rect(dc, &rect);
  188. rtgui_dc_end_drawing(dc);
  189. }
  190. }
  191. if ((index < box->items_count) && (index < box->page_items))
  192. {
  193. rt_uint16_t old_item;
  194. old_item = box->current_item;
  195. /* set selected item */
  196. box->current_item = (box->current_item/box->page_items) * box->page_items + index;
  197. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  198. {
  199. /* down event */
  200. rtgui_listbox_update_current(box, old_item);
  201. }
  202. else
  203. {
  204. /* up event */
  205. if (box->on_item != RT_NULL)
  206. {
  207. box->on_item(RTGUI_OBJECT(box), RT_NULL);
  208. }
  209. }
  210. }
  211. }
  212. return RT_TRUE;
  213. }
  214. case RTGUI_EVENT_KBD:
  215. {
  216. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  217. if ((ekbd->type == RTGUI_KEYDOWN) && (box->items_count > 0))
  218. {
  219. rt_int16_t old_item;
  220. if (box->current_item == -1)
  221. {
  222. /* set a initial item */
  223. if ((box->items_count > 0) &&
  224. (ekbd->key == RTGUIK_UP || ekbd->key == RTGUIK_DOWN))
  225. {
  226. box->current_item = 0;
  227. rtgui_listbox_update_current(box, -1);
  228. return RT_TRUE;
  229. }
  230. else
  231. return RT_FALSE;
  232. }
  233. old_item = box->current_item;
  234. switch (ekbd->key)
  235. {
  236. case RTGUIK_LEFT:
  237. if (box->current_item - box->page_items >= 0)
  238. box->current_item -= box->page_items;
  239. rtgui_listbox_update_current(box, old_item);
  240. return RT_TRUE;
  241. case RTGUIK_UP:
  242. if (box->current_item > 0)
  243. box->current_item --;
  244. rtgui_listbox_update_current(box, old_item);
  245. return RT_TRUE;
  246. case RTGUIK_RIGHT:
  247. if (box->current_item + box->page_items < box->items_count - 1)
  248. box->current_item += box->page_items;
  249. rtgui_listbox_update_current(box, old_item);
  250. return RT_TRUE;
  251. case RTGUIK_DOWN:
  252. if (box->current_item < box->items_count - 1)
  253. box->current_item ++;
  254. rtgui_listbox_update_current(box, old_item);
  255. return RT_TRUE;
  256. case RTGUIK_RETURN:
  257. if (box->on_item != RT_NULL)
  258. box->on_item(RTGUI_OBJECT(box), RT_NULL);
  259. return RT_TRUE;
  260. default:
  261. break;
  262. }
  263. }
  264. }
  265. return RT_FALSE;
  266. }
  267. /* use box event handler */
  268. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  269. }
  270. RTM_EXPORT(rtgui_listbox_event_handler);
  271. rtgui_listbox_t* rtgui_listbox_create(const struct rtgui_listbox_item* items, rt_uint16_t count, rtgui_rect_t *rect)
  272. {
  273. struct rtgui_listbox* box = RT_NULL;
  274. box = (struct rtgui_listbox*) rtgui_widget_create(RTGUI_LISTBOX_TYPE);
  275. if (box != RT_NULL)
  276. {
  277. box->items = items;
  278. box->items_count = count;
  279. box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  280. if (box->page_items == 0) box->page_items = 1;
  281. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  282. }
  283. return box;
  284. }
  285. RTM_EXPORT(rtgui_listbox_create);
  286. void rtgui_listbox_destroy(rtgui_listbox_t* box)
  287. {
  288. /* destroy box */
  289. rtgui_widget_destroy(RTGUI_WIDGET(box));
  290. }
  291. RTM_EXPORT(rtgui_listbox_destroy);
  292. void rtgui_listbox_set_onitem(rtgui_listbox_t* box, rtgui_event_handler_ptr func)
  293. {
  294. RT_ASSERT(box != RT_NULL);
  295. box->on_item = func;
  296. }
  297. RTM_EXPORT(rtgui_listbox_set_onitem);
  298. void rtgui_listbox_set_items(rtgui_listbox_t* box, struct rtgui_listbox_item* items, rt_uint16_t count)
  299. {
  300. rtgui_rect_t rect;
  301. box->items = items;
  302. box->items_count = count;
  303. box->current_item = -1;
  304. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  305. box->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  306. rtgui_widget_update(RTGUI_WIDGET(box));
  307. }
  308. RTM_EXPORT(rtgui_listbox_set_items);
  309. void rtgui_listbox_set_current_item(rtgui_listbox_t* box, int index)
  310. {
  311. RT_ASSERT(box != RT_NULL);
  312. if (index != box->current_item)
  313. {
  314. int old_item;
  315. old_item = box->current_item;
  316. box->current_item = index;
  317. rtgui_listbox_update_current(box, old_item);
  318. }
  319. }
  320. RTM_EXPORT(rtgui_listbox_set_current_item);
  321. static rt_bool_t rtgui_listbox_onunfocus(struct rtgui_object* object, rtgui_event_t* event)
  322. {
  323. rtgui_rect_t rect;
  324. rtgui_widget_t *widget;
  325. struct rtgui_dc *dc;
  326. RT_ASSERT(object);
  327. widget = RTGUI_WIDGET(object);
  328. dc = rtgui_dc_begin_drawing(widget);
  329. if(dc == RT_NULL) return RT_FALSE;
  330. rtgui_widget_get_rect(widget, &rect);
  331. if(!RTGUI_WIDGET_IS_FOCUSED(widget))
  332. {
  333. /* only clear focus rect */
  334. rtgui_color_t color;
  335. rect.x2 -= 1; rect.y2 -= 1;
  336. color = RTGUI_DC_FC(dc);
  337. RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc);
  338. rtgui_dc_draw_focus_rect(dc, &rect);
  339. RTGUI_DC_FC(dc) = color;
  340. }
  341. rtgui_dc_end_drawing(dc);
  342. return RT_TRUE;
  343. }