listctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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->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_object* object, struct rtgui_event* event)
  172. {
  173. struct rtgui_listctrl* ctrl;
  174. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  175. ctrl = RTGUI_LISTCTRL(object);
  176. switch (event->type)
  177. {
  178. case RTGUI_EVENT_PAINT:
  179. _rtgui_listctrl_ondraw(ctrl);
  180. return RT_FALSE;
  181. case RTGUI_EVENT_RESIZE:
  182. {
  183. struct rtgui_event_resize* resize;
  184. resize = (struct rtgui_event_resize*)event;
  185. /* recalculate page items */
  186. ctrl->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  187. }
  188. break;
  189. case RTGUI_EVENT_MOUSE_BUTTON:
  190. {
  191. rtgui_rect_t rect;
  192. struct rtgui_event_mouse* emouse;
  193. emouse = (struct rtgui_event_mouse*)event;
  194. /* get scrollbar rect */
  195. _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
  196. rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect);
  197. if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
  198. {
  199. _rtgui_listctrl_scrollbar_onmouse(ctrl, emouse);
  200. return RT_TRUE;
  201. }
  202. /* calculate selected item */
  203. /* get physical extent information */
  204. _rtgui_listctrl_get_rect(ctrl, &rect);
  205. rtgui_widget_rect_to_device(widget, &rect);
  206. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) &&
  207. (ctrl->items_count > 0))
  208. {
  209. rt_uint16_t index;
  210. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  211. /* set focus */
  212. rtgui_widget_focus(widget);
  213. {
  214. struct rtgui_rect rect;
  215. struct rtgui_dc* dc;
  216. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl));
  217. if (dc != RT_NULL)
  218. {
  219. /* get widget rect */
  220. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  221. /* update focus border */
  222. rect.x2 -= 1; rect.y2 -= 1;
  223. rtgui_dc_end_drawing(dc);
  224. }
  225. }
  226. if ((index < ctrl->page_items) &&
  227. (ctrl->current_item/ctrl->page_items)* ctrl->page_items + index < ctrl->items_count)
  228. {
  229. rt_uint16_t old_item;
  230. old_item = ctrl->current_item;
  231. /* set selected item */
  232. ctrl->current_item = (ctrl->current_item/ctrl->page_items) * ctrl->page_items + index;
  233. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  234. {
  235. /* down event */
  236. rtgui_listctrl_update_current(ctrl, old_item);
  237. }
  238. else
  239. {
  240. /* up event */
  241. if (ctrl->on_item != RT_NULL)
  242. {
  243. ctrl->on_item(RTGUI_OBJECT(ctrl), RT_NULL);
  244. }
  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. rt_uint16_t old_item;
  256. old_item = ctrl->current_item;
  257. switch (ekbd->key)
  258. {
  259. case RTGUIK_LEFT:
  260. if (ctrl->current_item - ctrl->page_items >= 0)
  261. ctrl->current_item -= ctrl->page_items;
  262. rtgui_listctrl_update_current(ctrl, old_item);
  263. return RT_TRUE;
  264. case RTGUIK_UP:
  265. if (ctrl->current_item > 0)
  266. ctrl->current_item --;
  267. rtgui_listctrl_update_current(ctrl, old_item);
  268. return RT_TRUE;
  269. case RTGUIK_RIGHT:
  270. if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
  271. ctrl->current_item += ctrl->page_items;
  272. else
  273. {
  274. if ((((ctrl->current_item/ctrl->page_items) + 1) * ctrl->page_items) < ctrl->items_count - 1)
  275. ctrl->current_item = ((ctrl->current_item / ctrl->page_items) + 1) * ctrl->page_items;
  276. }
  277. rtgui_listctrl_update_current(ctrl, old_item);
  278. return RT_TRUE;
  279. case RTGUIK_DOWN:
  280. if (ctrl->current_item < ctrl->items_count - 1)
  281. ctrl->current_item ++;
  282. rtgui_listctrl_update_current(ctrl, old_item);
  283. return RT_TRUE;
  284. case RTGUIK_RETURN:
  285. if (ctrl->on_item != RT_NULL)
  286. {
  287. return ctrl->on_item(RTGUI_OBJECT(ctrl), RT_NULL);
  288. }
  289. return RT_FALSE;
  290. default:
  291. break;
  292. }
  293. }
  294. }
  295. return RT_FALSE;
  296. }
  297. /* use ctrl event handler */
  298. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  299. }
  300. rtgui_listctrl_t* rtgui_listctrl_create(rt_uint32_t items, rt_uint16_t count, rtgui_rect_t *rect,
  301. rtgui_onitem_draw_t ondraw)
  302. {
  303. struct rtgui_listctrl* ctrl = RT_NULL;
  304. ctrl = (struct rtgui_listctrl*) rtgui_widget_create(RTGUI_LISTCTRL_TYPE);
  305. if (ctrl != RT_NULL)
  306. {
  307. ctrl->items = items;
  308. ctrl->items_count = count;
  309. ctrl->on_item_draw = ondraw;
  310. ctrl->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  311. rtgui_widget_set_rect(RTGUI_WIDGET(ctrl), rect);
  312. }
  313. return ctrl;
  314. }
  315. void rtgui_listctrl_destroy(rtgui_listctrl_t* ctrl)
  316. {
  317. /* destroy ctrl */
  318. rtgui_widget_destroy(RTGUI_WIDGET(ctrl));
  319. }
  320. void rtgui_listctrl_set_onitem(rtgui_listctrl_t* ctrl, rtgui_event_handler_ptr func)
  321. {
  322. RT_ASSERT(ctrl != RT_NULL);
  323. ctrl->on_item = func;
  324. }
  325. void rtgui_listctrl_set_items(rtgui_listctrl_t* ctrl, rt_uint32_t items, rt_uint16_t count)
  326. {
  327. rtgui_rect_t rect;
  328. ctrl->items = items;
  329. ctrl->items_count = count;
  330. ctrl->current_item = 0;
  331. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  332. ctrl->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  333. rtgui_widget_update(RTGUI_WIDGET(ctrl));
  334. }
  335. rt_bool_t rtgui_listctrl_get_item_rect(rtgui_listctrl_t* ctrl, rt_uint16_t item, rtgui_rect_t* item_rect)
  336. {
  337. if (item < ctrl->items_count)
  338. {
  339. rt_uint16_t index;
  340. /* check whether this item in current page */
  341. index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items;
  342. if (index > item || index + ctrl->page_items <= item) return RT_FALSE;
  343. rtgui_widget_get_extent(RTGUI_WIDGET(ctrl), item_rect);
  344. item_rect->y1 -= 2;
  345. item_rect->y1 += (item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  346. item_rect->y2 = item_rect->y1 + (2 + rtgui_theme_get_selected_height());
  347. return RT_TRUE;
  348. }
  349. return RT_FALSE;
  350. }