listctrl.c 15 KB

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