listctrl.c 14 KB

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