listctrl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * File : listctrl.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/listctrl.h>
  16. static void _rtgui_listctrl_constructor(struct rtgui_listctrl *ctrl)
  17. {
  18. /* set default widget rect and set event handler */
  19. rtgui_widget_set_event_handler(RTGUI_WIDGET(ctrl),rtgui_listctrl_event_handler);
  20. RTGUI_WIDGET(ctrl)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  21. ctrl->current_item = 0;
  22. ctrl->items_count = 0;
  23. ctrl->page_items = 0;
  24. ctrl->on_item = 0;
  25. ctrl->on_item_draw = RT_NULL;
  26. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(ctrl)) = white;
  27. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(ctrl)) = RTGUI_ALIGN_CENTER_VERTICAL;
  28. }
  29. rtgui_type_t *rtgui_listctrl_type_get(void)
  30. {
  31. static rtgui_type_t *listctrl_type = RT_NULL;
  32. if (!listctrl_type)
  33. {
  34. listctrl_type = rtgui_type_create("listctrl", RTGUI_WIDGET_TYPE,
  35. sizeof(rtgui_listctrl_t), RTGUI_CONSTRUCTOR(_rtgui_listctrl_constructor), RT_NULL);
  36. }
  37. return listctrl_type;
  38. }
  39. void rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl)
  40. {
  41. struct rtgui_rect rect, item_rect;
  42. struct rtgui_dc* dc;
  43. rt_uint16_t page_index, index;
  44. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  45. if (dc == RT_NULL) return;
  46. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  47. rtgui_dc_fill_rect(dc, &rect);
  48. rect.x2 -= 1; rect.y2 -= 1;
  49. /* draw focused border */
  50. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(ctrl)))
  51. rtgui_dc_draw_focus_rect(dc, &rect);
  52. /* get item base rect */
  53. item_rect = rect;
  54. item_rect.x1 += 1; item_rect.x2 -= 1;
  55. item_rect.y1 += 2;
  56. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  57. /* get current page */
  58. page_index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items;
  59. for (index = 0; index < ctrl->page_items; index ++)
  60. {
  61. if (page_index + index >= ctrl->items_count) break;
  62. if (page_index + index == ctrl->current_item)
  63. {
  64. rtgui_theme_draw_selected(dc, &item_rect);
  65. }
  66. if (ctrl->on_item_draw != RT_NULL)
  67. {
  68. ctrl->on_item_draw(ctrl, dc, &item_rect, page_index + index);
  69. }
  70. /* move to next item position */
  71. item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
  72. item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
  73. }
  74. rtgui_dc_end_drawing(dc);
  75. }
  76. void rtgui_listctrl_update_current(struct rtgui_listctrl* ctrl, rt_uint16_t old_item)
  77. {
  78. struct rtgui_dc* dc;
  79. rtgui_rect_t rect, item_rect;
  80. if (old_item/ctrl->page_items != ctrl->current_item/ctrl->page_items)
  81. {
  82. /* it's not a same page, update all */
  83. rtgui_widget_update(RTGUI_WIDGET(ctrl));
  84. return;
  85. }
  86. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  87. if (dc == RT_NULL) return;
  88. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  89. rect.x2 -= 1; rect.y2 -= 1;
  90. item_rect = rect;
  91. /* get old item's rect */
  92. item_rect.x1 += 1; item_rect.x2 -= 1;
  93. item_rect.y1 += 2;
  94. item_rect.y1 += (old_item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  95. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  96. /* draw old item */
  97. rtgui_dc_fill_rect(dc, &item_rect);
  98. if (ctrl->on_item_draw != RT_NULL)
  99. ctrl->on_item_draw(ctrl, dc, &item_rect, old_item);
  100. /* draw current item */
  101. item_rect = rect;
  102. /* get current item's rect */
  103. item_rect.x1 += 1; item_rect.x2 -= 1;
  104. item_rect.y1 += 2;
  105. item_rect.y1 += (ctrl->current_item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  106. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  107. /* draw current item */
  108. rtgui_theme_draw_selected(dc, &item_rect);
  109. if (ctrl->on_item_draw != RT_NULL)
  110. ctrl->on_item_draw(ctrl, dc, &item_rect, ctrl->current_item);
  111. rtgui_dc_end_drawing(dc);
  112. }
  113. rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  114. {
  115. struct rtgui_listctrl* ctrl = RT_NULL;
  116. ctrl = RTGUI_LISTCTRL(widget);
  117. switch (event->type)
  118. {
  119. case RTGUI_EVENT_PAINT:
  120. rtgui_listctrl_ondraw(ctrl);
  121. return RT_FALSE;
  122. case RTGUI_EVENT_RESIZE:
  123. {
  124. struct rtgui_event_resize* resize;
  125. resize = (struct rtgui_event_resize*)event;
  126. /* recalculate page items */
  127. ctrl->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  128. }
  129. break;
  130. case RTGUI_EVENT_MOUSE_BUTTON:
  131. {
  132. rtgui_rect_t rect;
  133. struct rtgui_event_mouse* emouse;
  134. emouse = (struct rtgui_event_mouse*)event;
  135. /* calculate selected item */
  136. /* get physical extent information */
  137. rtgui_widget_get_rect(widget, &rect);
  138. rtgui_widget_rect_to_device(widget, &rect);
  139. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (ctrl->items_count > 0))
  140. {
  141. rt_uint16_t index;
  142. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  143. /* set focus */
  144. rtgui_widget_focus(widget);
  145. {
  146. struct rtgui_rect rect;
  147. struct rtgui_dc* dc;
  148. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  149. if (dc != RT_NULL)
  150. {
  151. /* get widget rect */
  152. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  153. /* update focus border */
  154. rect.x2 -= 1; rect.y2 -= 1;
  155. /* draw focused border */
  156. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(ctrl)))
  157. rtgui_dc_draw_focus_rect(dc, &rect);
  158. rtgui_dc_end_drawing(dc);
  159. }
  160. }
  161. if ((index < ctrl->items_count) && (index < ctrl->page_items))
  162. {
  163. rt_uint16_t old_item;
  164. old_item = ctrl->current_item;
  165. /* set selected item */
  166. ctrl->current_item = (ctrl->current_item/ctrl->page_items) * ctrl->page_items + index;
  167. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  168. {
  169. /* down event */
  170. rtgui_listctrl_update_current(ctrl, old_item);
  171. }
  172. else
  173. {
  174. /* up event */
  175. if (ctrl->on_item != RT_NULL)
  176. {
  177. ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
  178. }
  179. }
  180. }
  181. }
  182. return RT_TRUE;
  183. }
  184. case RTGUI_EVENT_KBD:
  185. {
  186. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  187. if ((ekbd->type == RTGUI_KEYDOWN) && (ctrl->items_count > 0))
  188. {
  189. rt_uint16_t old_item;
  190. old_item = ctrl->current_item;
  191. switch (ekbd->key)
  192. {
  193. case RTGUIK_LEFT:
  194. if (ctrl->current_item - ctrl->page_items >= 0)
  195. ctrl->current_item -= ctrl->page_items;
  196. rtgui_listctrl_update_current(ctrl, old_item);
  197. return RT_FALSE;
  198. case RTGUIK_UP:
  199. if (ctrl->current_item > 0)
  200. ctrl->current_item --;
  201. rtgui_listctrl_update_current(ctrl, old_item);
  202. return RT_FALSE;
  203. case RTGUIK_RIGHT:
  204. if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
  205. ctrl->current_item += ctrl->page_items;
  206. rtgui_listctrl_update_current(ctrl, old_item);
  207. return RT_FALSE;
  208. case RTGUIK_DOWN:
  209. if (ctrl->current_item < ctrl->items_count - 1)
  210. ctrl->current_item ++;
  211. rtgui_listctrl_update_current(ctrl, old_item);
  212. return RT_FALSE;
  213. case RTGUIK_RETURN:
  214. if (ctrl->on_item != RT_NULL)
  215. {
  216. ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
  217. }
  218. return RT_FALSE;
  219. default:
  220. break;
  221. }
  222. }
  223. }
  224. return RT_FALSE;
  225. }
  226. /* use ctrl event handler */
  227. return rtgui_widget_event_handler(widget, event);
  228. }
  229. rtgui_listctrl_t* rtgui_listctrl_create(rt_uint32_t items, rt_uint16_t count, rtgui_rect_t *rect,
  230. rtgui_onitem_draw_t ondraw)
  231. {
  232. struct rtgui_listctrl* ctrl = RT_NULL;
  233. ctrl = (struct rtgui_listctrl*) rtgui_widget_create(RTGUI_LISTCTRL_TYPE);
  234. if (ctrl != RT_NULL)
  235. {
  236. ctrl->items = items;
  237. ctrl->items_count = count;
  238. ctrl->on_item_draw = ondraw;
  239. ctrl->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  240. rtgui_widget_set_rect(RTGUI_WIDGET(ctrl), rect);
  241. }
  242. return ctrl;
  243. }
  244. void rtgui_listctrl_destroy(rtgui_listctrl_t* ctrl)
  245. {
  246. /* destroy ctrl */
  247. rtgui_widget_destroy(RTGUI_WIDGET(ctrl));
  248. }
  249. void rtgui_listctrl_set_onitem(rtgui_listctrl_t* ctrl, rtgui_onitem_func_t func)
  250. {
  251. RT_ASSERT(ctrl != RT_NULL);
  252. ctrl->on_item = func;
  253. }
  254. void rtgui_listctrl_set_items(rtgui_listctrl_t* ctrl, rt_uint32_t items, rt_uint16_t count)
  255. {
  256. rtgui_rect_t rect;
  257. ctrl->items = items;
  258. ctrl->items_count = count;
  259. ctrl->current_item = 0;
  260. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  261. ctrl->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  262. rtgui_widget_update(RTGUI_WIDGET(ctrl));
  263. }