listbox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * File : listbox.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/listbox.h>
  16. #define LIST_MARGIN 5
  17. static void _rtgui_listbox_constructor(struct rtgui_listbox *box)
  18. {
  19. /* set default widget rect and set event handler */
  20. rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_listbox_event_handler);
  21. RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  22. box->current_item = -1;
  23. box->items_count = 0;
  24. box->page_items = 1;
  25. box->on_item = 0;
  26. RTGUI_WIDGET_BACKGROUND(box) = white;
  27. RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL;
  28. }
  29. DEFINE_CLASS_TYPE(listbox, "listbox",
  30. RTGUI_WIDGET_TYPE,
  31. _rtgui_listbox_constructor,
  32. RT_NULL,
  33. sizeof(struct rtgui_listbox));
  34. void rtgui_listbox_ondraw(struct rtgui_listbox* box)
  35. {
  36. struct rtgui_dc* dc;
  37. rt_uint16_t page_index, index;
  38. const struct rtgui_listbox_item* item;
  39. struct rtgui_rect rect, item_rect, image_rect;
  40. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  41. if (dc == RT_NULL) return;
  42. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  43. rtgui_dc_fill_rect(dc, &rect);
  44. rect.x2 -= 1; rect.y2 -= 1;
  45. /* draw focused border */
  46. if (RTGUI_WIDGET_IS_FOCUSED(box))
  47. rtgui_dc_draw_focus_rect(dc, &rect);
  48. /* get item base rect */
  49. item_rect = rect;
  50. item_rect.x1 += 1; item_rect.x2 -= 1;
  51. item_rect.y1 += 2;
  52. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  53. /* get current page */
  54. if (box->current_item == -1)
  55. page_index = 0;
  56. else
  57. page_index = (box->current_item / box->page_items) * box->page_items;
  58. for (index = 0; index < box->page_items; index ++)
  59. {
  60. if (page_index + index >= box->items_count) break;
  61. item = &(box->items[page_index + index]);
  62. if (page_index + index == box->current_item)
  63. {
  64. rtgui_theme_draw_selected(dc, &item_rect);
  65. }
  66. item_rect.x1 += LIST_MARGIN;
  67. if (item->image != RT_NULL)
  68. {
  69. rtgui_image_get_rect(item->image, &image_rect);
  70. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  71. rtgui_image_blit(item->image, dc, &image_rect);
  72. item_rect.x1 += item->image->w + 2;
  73. }
  74. /* draw text */
  75. rtgui_dc_draw_text(dc, item->name, &item_rect);
  76. if (item->image != RT_NULL)
  77. item_rect.x1 -= (item->image->w + 2);
  78. item_rect.x1 -= LIST_MARGIN;
  79. /* move to next item position */
  80. item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
  81. item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
  82. }
  83. rtgui_dc_end_drawing(dc);
  84. }
  85. RTM_EXPORT(rtgui_listbox_ondraw);
  86. static void rtgui_listbox_update_current(struct rtgui_listbox* box, rt_int16_t old_item)
  87. {
  88. struct rtgui_dc* dc;
  89. const struct rtgui_listbox_item* item;
  90. rtgui_rect_t rect, item_rect, image_rect;
  91. if ((old_item == -1) || (old_item/box->page_items != box->current_item/box->page_items))
  92. {
  93. /* it's not a same page, update all */
  94. rtgui_widget_update(RTGUI_WIDGET(box));
  95. return;
  96. }
  97. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  98. if (dc == RT_NULL) return;
  99. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  100. rect.x2 -= 1; rect.y2 -= 1;
  101. item_rect = rect;
  102. /* get old item's rect */
  103. item_rect.x1 += 1; item_rect.x2 -= 1;
  104. item_rect.y1 += 2;
  105. item_rect.y1 += (old_item % box->page_items) * (2 + rtgui_theme_get_selected_height());
  106. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  107. /* draw old item */
  108. rtgui_dc_fill_rect(dc, &item_rect);
  109. item_rect.x1 += LIST_MARGIN;
  110. item = &(box->items[old_item]);
  111. if (item->image != RT_NULL)
  112. {
  113. rtgui_image_get_rect(item->image, &image_rect);
  114. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  115. rtgui_image_blit(item->image, dc, &image_rect);
  116. item_rect.x1 += item->image->w + 2;
  117. }
  118. rtgui_dc_draw_text(dc, item->name, &item_rect);
  119. /* draw current item */
  120. item_rect = rect;
  121. /* get current item's rect */
  122. item_rect.x1 += 1; item_rect.x2 -= 1;
  123. item_rect.y1 += 2;
  124. item_rect.y1 += (box->current_item % box->page_items) * (2 + rtgui_theme_get_selected_height());
  125. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  126. /* draw current item */
  127. rtgui_theme_draw_selected(dc, &item_rect);
  128. item_rect.x1 += LIST_MARGIN;
  129. item = &(box->items[box->current_item]);
  130. if (item->image != RT_NULL)
  131. {
  132. rtgui_image_get_rect(item->image, &image_rect);
  133. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  134. rtgui_image_blit(item->image, dc, &image_rect);
  135. item_rect.x1 += (item->image->w + 2);
  136. }
  137. rtgui_dc_draw_text(dc, item->name, &item_rect);
  138. rtgui_dc_end_drawing(dc);
  139. }
  140. rt_bool_t rtgui_listbox_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  141. {
  142. struct rtgui_listbox* box;
  143. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  144. box = RTGUI_LISTBOX(object);
  145. switch (event->type)
  146. {
  147. case RTGUI_EVENT_PAINT:
  148. rtgui_listbox_ondraw(box);
  149. return RT_FALSE;
  150. case RTGUI_EVENT_RESIZE:
  151. {
  152. struct rtgui_event_resize* resize;
  153. resize = (struct rtgui_event_resize*)event;
  154. /* recalculate page items */
  155. box->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  156. }
  157. break;
  158. case RTGUI_EVENT_MOUSE_BUTTON:
  159. {
  160. rtgui_rect_t rect;
  161. struct rtgui_event_mouse* emouse;
  162. emouse = (struct rtgui_event_mouse*)event;
  163. /* calculate selected item */
  164. /* get physical extent information */
  165. rtgui_widget_get_rect(widget, &rect);
  166. rtgui_widget_rect_to_device(widget, &rect);
  167. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (box->items_count > 0))
  168. {
  169. rt_uint16_t index;
  170. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  171. /* set focus */
  172. rtgui_widget_focus(widget);
  173. {
  174. struct rtgui_rect rect;
  175. struct rtgui_dc* dc;
  176. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  177. if (dc != RT_NULL)
  178. {
  179. /* get widget rect */
  180. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  181. /* update focus border */
  182. rect.x2 -= 1; rect.y2 -= 1;
  183. /* draw focused border */
  184. if (RTGUI_WIDGET_IS_FOCUSED(box))
  185. rtgui_dc_draw_focus_rect(dc, &rect);
  186. rtgui_dc_end_drawing(dc);
  187. }
  188. }
  189. if ((index < box->items_count) && (index < box->page_items))
  190. {
  191. rt_uint16_t old_item;
  192. old_item = box->current_item;
  193. /* set selected item */
  194. box->current_item = (box->current_item/box->page_items) * box->page_items + index;
  195. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  196. {
  197. /* down event */
  198. rtgui_listbox_update_current(box, old_item);
  199. }
  200. else
  201. {
  202. /* up event */
  203. if (box->on_item != RT_NULL)
  204. {
  205. box->on_item(RTGUI_OBJECT(box), RT_NULL);
  206. }
  207. }
  208. }
  209. }
  210. return RT_TRUE;
  211. }
  212. case RTGUI_EVENT_KBD:
  213. {
  214. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  215. if ((ekbd->type == RTGUI_KEYDOWN) && (box->items_count > 0))
  216. {
  217. rt_int16_t old_item;
  218. if (box->current_item == -1)
  219. {
  220. /* set a initial item */
  221. if ((box->items_count > 0) &&
  222. (ekbd->key == RTGUIK_UP || ekbd->key == RTGUIK_DOWN))
  223. {
  224. box->current_item = 0;
  225. rtgui_listbox_update_current(box, -1);
  226. return RT_TRUE;
  227. }
  228. else
  229. return RT_FALSE;
  230. }
  231. old_item = box->current_item;
  232. switch (ekbd->key)
  233. {
  234. case RTGUIK_LEFT:
  235. if (box->current_item - box->page_items >= 0)
  236. box->current_item -= box->page_items;
  237. rtgui_listbox_update_current(box, old_item);
  238. return RT_TRUE;
  239. case RTGUIK_UP:
  240. if (box->current_item > 0)
  241. box->current_item --;
  242. rtgui_listbox_update_current(box, old_item);
  243. return RT_TRUE;
  244. case RTGUIK_RIGHT:
  245. if (box->current_item + box->page_items < box->items_count - 1)
  246. box->current_item += box->page_items;
  247. rtgui_listbox_update_current(box, old_item);
  248. return RT_TRUE;
  249. case RTGUIK_DOWN:
  250. if (box->current_item < box->items_count - 1)
  251. box->current_item ++;
  252. rtgui_listbox_update_current(box, old_item);
  253. return RT_TRUE;
  254. case RTGUIK_RETURN:
  255. if (box->on_item != RT_NULL)
  256. box->on_item(RTGUI_OBJECT(box), RT_NULL);
  257. return RT_TRUE;
  258. default:
  259. break;
  260. }
  261. }
  262. }
  263. return RT_FALSE;
  264. }
  265. /* use box event handler */
  266. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  267. }
  268. RTM_EXPORT(rtgui_listbox_event_handler);
  269. rtgui_listbox_t* rtgui_listbox_create(const struct rtgui_listbox_item* items, rt_uint16_t count, rtgui_rect_t *rect)
  270. {
  271. struct rtgui_listbox* box = RT_NULL;
  272. box = (struct rtgui_listbox*) rtgui_widget_create(RTGUI_LISTBOX_TYPE);
  273. if (box != RT_NULL)
  274. {
  275. box->items = items;
  276. box->items_count = count;
  277. box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  278. if (box->page_items == 0) box->page_items = 1;
  279. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  280. }
  281. return box;
  282. }
  283. RTM_EXPORT(rtgui_listbox_create);
  284. void rtgui_listbox_destroy(rtgui_listbox_t* box)
  285. {
  286. /* destroy box */
  287. rtgui_widget_destroy(RTGUI_WIDGET(box));
  288. }
  289. RTM_EXPORT(rtgui_listbox_destroy);
  290. void rtgui_listbox_set_onitem(rtgui_listbox_t* box, rtgui_event_handler_ptr func)
  291. {
  292. RT_ASSERT(box != RT_NULL);
  293. box->on_item = func;
  294. }
  295. RTM_EXPORT(rtgui_listbox_set_onitem);
  296. void rtgui_listbox_set_items(rtgui_listbox_t* box, struct rtgui_listbox_item* items, rt_uint16_t count)
  297. {
  298. rtgui_rect_t rect;
  299. box->items = items;
  300. box->items_count = count;
  301. box->current_item = -1;
  302. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  303. box->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  304. rtgui_widget_update(RTGUI_WIDGET(box));
  305. }
  306. RTM_EXPORT(rtgui_listbox_set_items);
  307. void rtgui_listbox_set_current_item(rtgui_listbox_t* box, int index)
  308. {
  309. RT_ASSERT(box != RT_NULL);
  310. if (index != box->current_item)
  311. {
  312. int old_item;
  313. old_item = box->current_item;
  314. box->current_item = index;
  315. rtgui_listbox_update_current(box, old_item);
  316. }
  317. }
  318. RTM_EXPORT(rtgui_listbox_set_current_item);