listctrl.c 12 KB

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