listbox.c 8.9 KB

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