listctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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_update_current(struct rtgui_listctrl* ctrl, rt_uint16_t old_item);
  17. static void _rtgui_listctrl_constructor(struct rtgui_listctrl *ctrl)
  18. {
  19. /* set default widget rect and set event handler */
  20. rtgui_widget_set_event_handler(RTGUI_WIDGET(ctrl),rtgui_listctrl_event_handler);
  21. RTGUI_WIDGET(ctrl)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  22. ctrl->current_item = -1;
  23. ctrl->items_count = 0;
  24. ctrl->page_items = 0;
  25. ctrl->on_item = 0;
  26. ctrl->on_item_draw = RT_NULL;
  27. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(ctrl)) = white;
  28. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(ctrl)) = RTGUI_ALIGN_CENTER_VERTICAL;
  29. }
  30. DEFINE_CLASS_TYPE(listctrl, "listctrl",
  31. RTGUI_WIDGET_TYPE,
  32. _rtgui_listctrl_constructor,
  33. RT_NULL,
  34. sizeof(struct rtgui_listctrl));
  35. static void _rtgui_listctrl_get_rect(struct rtgui_listctrl* ctrl, rtgui_rect_t* rect)
  36. {
  37. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), rect);
  38. if (ctrl->items_count > rtgui_rect_height(*rect)/rtgui_theme_get_selected_height())
  39. {
  40. rect->x2 = rect->x2 - 8;
  41. }
  42. }
  43. static void _rtgui_listctrl_get_scrollbar_rect(struct rtgui_listctrl* ctrl, rtgui_rect_t* rect)
  44. {
  45. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), rect);
  46. if (ctrl->items_count > rtgui_rect_height(*rect)/rtgui_theme_get_selected_height())
  47. {
  48. rect->x1 = rect->x2 - 8;
  49. }
  50. else
  51. {
  52. /* no scrollbar */
  53. rt_memset(rect, 0, sizeof(rtgui_rect_t));
  54. }
  55. }
  56. static void _rtgui_listctrl_scrollbar_ondraw(struct rtgui_listctrl* ctrl, struct rtgui_dc* dc)
  57. {
  58. rtgui_rect_t rect;
  59. rt_uint32_t height, y1;
  60. /* get scrollbar rect */
  61. _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
  62. rtgui_dc_fill_rect(dc, &rect);
  63. height = rtgui_rect_height(rect);
  64. height = height / ((ctrl->items_count + (ctrl->page_items - 1))/ctrl->page_items);
  65. y1 = (ctrl->current_item / ctrl->page_items) * height;
  66. rect.y1 = rect.y1 + y1; rect.y2 = rect.y1 + height;
  67. rect.x1 -= 3;
  68. rtgui_theme_draw_selected(dc, &rect);
  69. }
  70. static void _rtgui_listctrl_scrollbar_onmouse(struct rtgui_listctrl* ctrl, struct rtgui_event_mouse* mouse)
  71. {
  72. rtgui_rect_t rect;
  73. rt_uint32_t height, y1;
  74. rt_uint16_t old_item;
  75. /* get scrollbar rect */
  76. _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
  77. height = rtgui_rect_height(rect);
  78. height = height / ((ctrl->items_count + (ctrl->page_items - 1))/ctrl->page_items);
  79. y1 = (ctrl->current_item / ctrl->page_items) * height;
  80. rect.y1 = rect.y1 + y1; rect.y2 = rect.y1 + height;
  81. rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect);
  82. old_item = ctrl->current_item;
  83. if (mouse->y < rect.y1)
  84. {
  85. if (ctrl->current_item - ctrl->page_items >= 0)
  86. ctrl->current_item -= ctrl->page_items;
  87. rtgui_listctrl_update_current(ctrl, old_item);
  88. }
  89. else if (mouse->y > rect.y2)
  90. {
  91. if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
  92. ctrl->current_item += ctrl->page_items;
  93. else
  94. ctrl->current_item = ((ctrl->current_item / ctrl->page_items) + 1) * ctrl->page_items;
  95. rtgui_listctrl_update_current(ctrl, old_item);
  96. }
  97. }
  98. static void _rtgui_listctrl_ondraw(struct rtgui_listctrl* ctrl)
  99. {
  100. struct rtgui_rect rect, item_rect;
  101. struct rtgui_dc* dc;
  102. rt_uint16_t page_index, index;
  103. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  104. if (dc == RT_NULL) return;
  105. _rtgui_listctrl_get_rect(ctrl, &rect);
  106. rtgui_dc_fill_rect(dc, &rect);
  107. rect.x2 -= 1; rect.y2 -= 1;
  108. /* get item base rect */
  109. item_rect = rect;
  110. item_rect.x1 += 1; item_rect.x2 -= 1;
  111. item_rect.y1 += 2;
  112. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  113. /* get current page */
  114. page_index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items;
  115. for (index = 0; index < ctrl->page_items; index ++)
  116. {
  117. if (page_index + index >= ctrl->items_count) break;
  118. if (page_index + index == ctrl->current_item)
  119. {
  120. rtgui_theme_draw_selected(dc, &item_rect);
  121. }
  122. if (ctrl->on_item_draw != RT_NULL)
  123. {
  124. ctrl->on_item_draw(ctrl, dc, &item_rect, page_index + index);
  125. }
  126. /* move to next item position */
  127. item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
  128. item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
  129. }
  130. /* draw scrollbar */
  131. _rtgui_listctrl_scrollbar_ondraw(ctrl, dc);
  132. rtgui_dc_end_drawing(dc);
  133. }
  134. void rtgui_listctrl_update_current(struct rtgui_listctrl* ctrl, rt_uint16_t old_item)
  135. {
  136. struct rtgui_dc* dc;
  137. rtgui_rect_t rect, item_rect;
  138. if (old_item/ctrl->page_items != ctrl->current_item/ctrl->page_items)
  139. {
  140. /* it's not a same page, update all */
  141. rtgui_widget_update(RTGUI_WIDGET(ctrl));
  142. return;
  143. }
  144. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  145. if (dc == RT_NULL) return;
  146. _rtgui_listctrl_get_rect(ctrl, &rect);
  147. rect.x2 -= 1; rect.y2 -= 1;
  148. item_rect = rect;
  149. /* get old item's rect */
  150. item_rect.x1 += 1; item_rect.x2 -= 1;
  151. item_rect.y1 += 2;
  152. item_rect.y1 += (old_item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  153. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  154. /* draw old item */
  155. rtgui_dc_fill_rect(dc, &item_rect);
  156. if (ctrl->on_item_draw != RT_NULL)
  157. ctrl->on_item_draw(ctrl, dc, &item_rect, old_item);
  158. /* draw current item */
  159. item_rect = rect;
  160. /* get current item's rect */
  161. item_rect.x1 += 1; item_rect.x2 -= 1;
  162. item_rect.y1 += 2;
  163. item_rect.y1 += (ctrl->current_item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  164. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  165. /* draw current item */
  166. rtgui_theme_draw_selected(dc, &item_rect);
  167. if (ctrl->on_item_draw != RT_NULL)
  168. ctrl->on_item_draw(ctrl, dc, &item_rect, ctrl->current_item);
  169. rtgui_dc_end_drawing(dc);
  170. }
  171. rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  172. {
  173. struct rtgui_listctrl* ctrl = RT_NULL;
  174. ctrl = RTGUI_LISTCTRL(widget);
  175. switch (event->type)
  176. {
  177. case RTGUI_EVENT_PAINT:
  178. _rtgui_listctrl_ondraw(ctrl);
  179. return RT_FALSE;
  180. case RTGUI_EVENT_RESIZE:
  181. {
  182. struct rtgui_event_resize* resize;
  183. resize = (struct rtgui_event_resize*)event;
  184. /* recalculate page items */
  185. ctrl->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  186. }
  187. break;
  188. case RTGUI_EVENT_MOUSE_BUTTON:
  189. {
  190. rtgui_rect_t rect;
  191. struct rtgui_event_mouse* emouse;
  192. emouse = (struct rtgui_event_mouse*)event;
  193. /* get scrollbar rect */
  194. _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
  195. rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect);
  196. if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
  197. {
  198. _rtgui_listctrl_scrollbar_onmouse(ctrl, emouse);
  199. return RT_TRUE;
  200. }
  201. /* calculate selected item */
  202. /* get physical extent information */
  203. _rtgui_listctrl_get_rect(ctrl, &rect);
  204. rtgui_widget_rect_to_device(widget, &rect);
  205. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) &&
  206. (ctrl->items_count > 0))
  207. {
  208. rt_uint16_t index;
  209. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  210. /* set focus */
  211. rtgui_widget_focus(widget);
  212. {
  213. struct rtgui_rect rect;
  214. struct rtgui_dc* dc;
  215. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  216. if (dc != RT_NULL)
  217. {
  218. /* get widget rect */
  219. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  220. /* update focus border */
  221. rect.x2 -= 1; rect.y2 -= 1;
  222. rtgui_dc_end_drawing(dc);
  223. }
  224. }
  225. if ((index < ctrl->page_items) &&
  226. (ctrl->current_item/ctrl->page_items)* ctrl->page_items + index < ctrl->items_count)
  227. {
  228. rt_uint16_t old_item;
  229. old_item = ctrl->current_item;
  230. /* set selected item */
  231. ctrl->current_item = (ctrl->current_item/ctrl->page_items) * ctrl->page_items + index;
  232. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  233. {
  234. /* down event */
  235. rtgui_listctrl_update_current(ctrl, old_item);
  236. }
  237. else
  238. {
  239. /* up event */
  240. if (ctrl->on_item != RT_NULL)
  241. {
  242. ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
  243. }
  244. }
  245. }
  246. }
  247. return RT_TRUE;
  248. }
  249. case RTGUI_EVENT_KBD:
  250. {
  251. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  252. if ((ekbd->type == RTGUI_KEYDOWN) && (ctrl->items_count > 0))
  253. {
  254. rt_uint16_t old_item;
  255. old_item = ctrl->current_item;
  256. switch (ekbd->key)
  257. {
  258. case RTGUIK_LEFT:
  259. if (ctrl->current_item - ctrl->page_items >= 0)
  260. ctrl->current_item -= ctrl->page_items;
  261. rtgui_listctrl_update_current(ctrl, old_item);
  262. return RT_FALSE;
  263. case RTGUIK_UP:
  264. if (ctrl->current_item > 0)
  265. ctrl->current_item --;
  266. rtgui_listctrl_update_current(ctrl, old_item);
  267. return RT_FALSE;
  268. case RTGUIK_RIGHT:
  269. if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
  270. ctrl->current_item += ctrl->page_items;
  271. else
  272. {
  273. if ((((ctrl->current_item/ctrl->page_items) + 1) * ctrl->page_items) < ctrl->items_count - 1)
  274. ctrl->current_item = ((ctrl->current_item / ctrl->page_items) + 1) * ctrl->page_items;
  275. }
  276. rtgui_listctrl_update_current(ctrl, old_item);
  277. return RT_FALSE;
  278. case RTGUIK_DOWN:
  279. if (ctrl->current_item < ctrl->items_count - 1)
  280. ctrl->current_item ++;
  281. rtgui_listctrl_update_current(ctrl, old_item);
  282. return RT_FALSE;
  283. case RTGUIK_RETURN:
  284. if (ctrl->on_item != RT_NULL)
  285. {
  286. ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
  287. }
  288. return RT_FALSE;
  289. default:
  290. break;
  291. }
  292. }
  293. }
  294. return RT_FALSE;
  295. }
  296. /* use ctrl event handler */
  297. return rtgui_widget_event_handler(widget, event);
  298. }
  299. rtgui_listctrl_t* rtgui_listctrl_create(rt_uint32_t items, rt_uint16_t count, rtgui_rect_t *rect,
  300. rtgui_onitem_draw_t ondraw)
  301. {
  302. struct rtgui_listctrl* ctrl = RT_NULL;
  303. ctrl = (struct rtgui_listctrl*) rtgui_widget_create(RTGUI_LISTCTRL_TYPE);
  304. if (ctrl != RT_NULL)
  305. {
  306. ctrl->items = items;
  307. ctrl->items_count = count;
  308. ctrl->on_item_draw = ondraw;
  309. ctrl->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  310. rtgui_widget_set_rect(RTGUI_WIDGET(ctrl), rect);
  311. }
  312. return ctrl;
  313. }
  314. void rtgui_listctrl_destroy(rtgui_listctrl_t* ctrl)
  315. {
  316. /* destroy ctrl */
  317. rtgui_widget_destroy(RTGUI_WIDGET(ctrl));
  318. }
  319. void rtgui_listctrl_set_onitem(rtgui_listctrl_t* ctrl, rtgui_onitem_func_t func)
  320. {
  321. RT_ASSERT(ctrl != RT_NULL);
  322. ctrl->on_item = func;
  323. }
  324. void rtgui_listctrl_set_items(rtgui_listctrl_t* ctrl, rt_uint32_t items, rt_uint16_t count)
  325. {
  326. rtgui_rect_t rect;
  327. ctrl->items = items;
  328. ctrl->items_count = count;
  329. ctrl->current_item = 0;
  330. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  331. ctrl->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  332. rtgui_widget_update(RTGUI_WIDGET(ctrl));
  333. }
  334. rt_bool_t rtgui_listctrl_get_item_rect(rtgui_listctrl_t* ctrl, rt_uint16_t item, rtgui_rect_t* item_rect)
  335. {
  336. if (item < ctrl->items_count)
  337. {
  338. rt_uint16_t index;
  339. /* check whether this item in current page */
  340. index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items;
  341. if (index > item || index + ctrl->page_items <= item) return RT_FALSE;
  342. rtgui_widget_get_extent(RTGUI_WIDGET(ctrl), item_rect);
  343. item_rect->y1 -= 2;
  344. item_rect->y1 += (item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  345. item_rect->y2 = item_rect->y1 + (2 + rtgui_theme_get_selected_height());
  346. return RT_TRUE;
  347. }
  348. return RT_FALSE;
  349. }