listctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. rtgui_theme_draw_selected(dc, &rect);
  73. }
  74. static void _rtgui_listctrl_scrollbar_onmouse(struct rtgui_listctrl* ctrl, struct rtgui_event_mouse* mouse)
  75. {
  76. rtgui_rect_t rect;
  77. rt_uint32_t height, y1;
  78. rt_uint16_t old_item;
  79. /* get scrollbar rect */
  80. _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
  81. height = rtgui_rect_height(rect);
  82. height = height / ((ctrl->items_count + (ctrl->page_items - 1))/ctrl->page_items);
  83. y1 = (ctrl->current_item / ctrl->page_items) * height;
  84. rect.y1 = rect.y1 + y1; 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. 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 + rtgui_theme_get_selected_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 += (rtgui_theme_get_selected_height() + 2);
  130. item_rect.y2 += (rtgui_theme_get_selected_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 + rtgui_theme_get_selected_height());
  155. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_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 + rtgui_theme_get_selected_height());
  166. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_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. rt_bool_t rtgui_listctrl_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  174. {
  175. struct rtgui_listctrl* ctrl = RT_NULL;
  176. ctrl = RTGUI_LISTCTRL(widget);
  177. switch (event->type)
  178. {
  179. case RTGUI_EVENT_PAINT:
  180. _rtgui_listctrl_ondraw(ctrl);
  181. return RT_FALSE;
  182. case RTGUI_EVENT_RESIZE:
  183. {
  184. struct rtgui_event_resize* resize;
  185. resize = (struct rtgui_event_resize*)event;
  186. /* recalculate page items */
  187. ctrl->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  188. }
  189. break;
  190. case RTGUI_EVENT_MOUSE_BUTTON:
  191. {
  192. rtgui_rect_t rect;
  193. struct rtgui_event_mouse* emouse;
  194. emouse = (struct rtgui_event_mouse*)event;
  195. /* get scrollbar rect */
  196. _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect);
  197. rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect);
  198. if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
  199. {
  200. _rtgui_listctrl_scrollbar_onmouse(ctrl, emouse);
  201. return RT_TRUE;
  202. }
  203. /* calculate selected item */
  204. /* get physical extent information */
  205. _rtgui_listctrl_get_rect(ctrl, &rect);
  206. rtgui_widget_rect_to_device(widget, &rect);
  207. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (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->items_count) && (index < ctrl->page_items))
  227. {
  228. rt_uint16_t old_item;
  229. old_item = ctrl->current_item;
  230. /* set selected item */
  231. ctrl->current_item = (ctrl->current_item/ctrl->page_items) * ctrl->page_items + index;
  232. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  233. {
  234. /* down event */
  235. rtgui_listctrl_update_current(ctrl, old_item);
  236. }
  237. else
  238. {
  239. /* up event */
  240. if (ctrl->on_item != RT_NULL)
  241. {
  242. ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
  243. }
  244. }
  245. }
  246. }
  247. return RT_TRUE;
  248. }
  249. case RTGUI_EVENT_KBD:
  250. {
  251. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  252. if ((ekbd->type == RTGUI_KEYDOWN) && (ctrl->items_count > 0))
  253. {
  254. rt_uint16_t old_item;
  255. old_item = ctrl->current_item;
  256. switch (ekbd->key)
  257. {
  258. case RTGUIK_LEFT:
  259. if (ctrl->current_item - ctrl->page_items >= 0)
  260. ctrl->current_item -= ctrl->page_items;
  261. rtgui_listctrl_update_current(ctrl, old_item);
  262. return RT_FALSE;
  263. case RTGUIK_UP:
  264. if (ctrl->current_item > 0)
  265. ctrl->current_item --;
  266. rtgui_listctrl_update_current(ctrl, old_item);
  267. return RT_FALSE;
  268. case RTGUIK_RIGHT:
  269. if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1)
  270. ctrl->current_item += ctrl->page_items;
  271. rtgui_listctrl_update_current(ctrl, old_item);
  272. return RT_FALSE;
  273. case RTGUIK_DOWN:
  274. if (ctrl->current_item < ctrl->items_count - 1)
  275. ctrl->current_item ++;
  276. rtgui_listctrl_update_current(ctrl, old_item);
  277. return RT_FALSE;
  278. case RTGUIK_RETURN:
  279. if (ctrl->on_item != RT_NULL)
  280. {
  281. ctrl->on_item(RTGUI_WIDGET(ctrl), RT_NULL);
  282. }
  283. return RT_FALSE;
  284. default:
  285. break;
  286. }
  287. }
  288. }
  289. return RT_FALSE;
  290. }
  291. /* use ctrl event handler */
  292. return rtgui_widget_event_handler(widget, event);
  293. }
  294. rtgui_listctrl_t* rtgui_listctrl_create(rt_uint32_t items, rt_uint16_t count, rtgui_rect_t *rect,
  295. rtgui_onitem_draw_t ondraw)
  296. {
  297. struct rtgui_listctrl* ctrl = RT_NULL;
  298. ctrl = (struct rtgui_listctrl*) rtgui_widget_create(RTGUI_LISTCTRL_TYPE);
  299. if (ctrl != RT_NULL)
  300. {
  301. ctrl->items = items;
  302. ctrl->items_count = count;
  303. ctrl->on_item_draw = ondraw;
  304. ctrl->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  305. rtgui_widget_set_rect(RTGUI_WIDGET(ctrl), rect);
  306. }
  307. return ctrl;
  308. }
  309. void rtgui_listctrl_destroy(rtgui_listctrl_t* ctrl)
  310. {
  311. /* destroy ctrl */
  312. rtgui_widget_destroy(RTGUI_WIDGET(ctrl));
  313. }
  314. void rtgui_listctrl_set_onitem(rtgui_listctrl_t* ctrl, rtgui_onitem_func_t func)
  315. {
  316. RT_ASSERT(ctrl != RT_NULL);
  317. ctrl->on_item = func;
  318. }
  319. void rtgui_listctrl_set_items(rtgui_listctrl_t* ctrl, rt_uint32_t items, rt_uint16_t count)
  320. {
  321. rtgui_rect_t rect;
  322. ctrl->items = items;
  323. ctrl->items_count = count;
  324. ctrl->current_item = 0;
  325. rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect);
  326. ctrl->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  327. rtgui_widget_update(RTGUI_WIDGET(ctrl));
  328. }
  329. rt_bool_t rtgui_listctrl_get_item_rect(rtgui_listctrl_t* ctrl, rt_uint16_t item, rtgui_rect_t* item_rect)
  330. {
  331. if (item < ctrl->items_count)
  332. {
  333. rt_uint16_t index;
  334. /* check whether this item in current page */
  335. index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items;
  336. if (index > item || index + ctrl->page_items <= item) return RT_FALSE;
  337. rtgui_widget_get_extent(RTGUI_WIDGET(ctrl), item_rect);
  338. item_rect->y1 -= 2;
  339. item_rect->y1 += (item % ctrl->page_items) * (2 + rtgui_theme_get_selected_height());
  340. item_rect->y2 = item_rect->y1 + (2 + rtgui_theme_get_selected_height());
  341. return RT_TRUE;
  342. }
  343. return RT_FALSE;
  344. }