listctrl.c 12 KB

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