listbox.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 = -1;
  23. box->items_count = 0;
  24. box->page_items = 1;
  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. if (box->current_item == -1)
  60. page_index = 0;
  61. else
  62. page_index = (box->current_item / box->page_items) * box->page_items;
  63. for (index = 0; index < box->page_items; index ++)
  64. {
  65. if (page_index + index >= box->items_count) break;
  66. item = &(box->items[page_index + index]);
  67. if (page_index + index == box->current_item)
  68. {
  69. rtgui_theme_draw_selected(dc, &item_rect);
  70. }
  71. item_rect.x1 += LIST_MARGIN;
  72. if (item->image != RT_NULL)
  73. {
  74. rtgui_image_blit(item->image, dc, &item_rect);
  75. item_rect.x1 += item->image->w + 2;
  76. }
  77. /* draw text */
  78. rtgui_dc_draw_text(dc, item->name, &item_rect);
  79. if (item->image != RT_NULL)
  80. item_rect.x1 -= (item->image->w + 2);
  81. item_rect.x1 -= LIST_MARGIN;
  82. /* move to next item position */
  83. item_rect.y1 += (rtgui_theme_get_selected_height() + 2);
  84. item_rect.y2 += (rtgui_theme_get_selected_height() + 2);
  85. }
  86. rtgui_dc_end_drawing(dc);
  87. }
  88. static void rtgui_listbox_update_current(struct rtgui_listbox* box, rt_int16_t old_item)
  89. {
  90. struct rtgui_dc* dc;
  91. const struct rtgui_listbox_item* item;
  92. rtgui_rect_t rect, item_rect;
  93. if ((old_item == -1) || (old_item/box->page_items != box->current_item/box->page_items))
  94. {
  95. /* it's not a same page, update all */
  96. rtgui_widget_update(RTGUI_WIDGET(box));
  97. return;
  98. }
  99. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  100. if (dc == RT_NULL) return;
  101. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  102. rect.x2 -= 1; rect.y2 -= 1;
  103. item_rect = rect;
  104. /* get old item's rect */
  105. item_rect.x1 += 1; item_rect.x2 -= 1;
  106. item_rect.y1 += 2;
  107. item_rect.y1 += (old_item % box->page_items) * (2 + rtgui_theme_get_selected_height());
  108. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  109. /* draw old item */
  110. rtgui_dc_fill_rect(dc, &item_rect);
  111. item_rect.x1 += LIST_MARGIN;
  112. item = &(box->items[old_item]);
  113. if (item->image != RT_NULL)
  114. {
  115. rtgui_image_blit(item->image, dc, &item_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_blit(item->image, dc, &item_rect);
  133. item_rect.x1 += (item->image->w + 2);
  134. }
  135. rtgui_dc_draw_text(dc, item->name, &item_rect);
  136. rtgui_dc_end_drawing(dc);
  137. }
  138. rt_bool_t rtgui_listbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  139. {
  140. struct rtgui_listbox* box = RT_NULL;
  141. box = RTGUI_LISTBOX(widget);
  142. switch (event->type)
  143. {
  144. case RTGUI_EVENT_PAINT:
  145. rtgui_listbox_ondraw(box);
  146. return RT_FALSE;
  147. case RTGUI_EVENT_RESIZE:
  148. {
  149. struct rtgui_event_resize* resize;
  150. resize = (struct rtgui_event_resize*)event;
  151. /* recalculate page items */
  152. box->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  153. }
  154. break;
  155. case RTGUI_EVENT_MOUSE_BUTTON:
  156. {
  157. rtgui_rect_t rect;
  158. struct rtgui_event_mouse* emouse;
  159. emouse = (struct rtgui_event_mouse*)event;
  160. /* calculate selected item */
  161. /* get physical extent information */
  162. rtgui_widget_get_rect(widget, &rect);
  163. rtgui_widget_rect_to_device(widget, &rect);
  164. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (box->items_count > 0))
  165. {
  166. rt_uint16_t index;
  167. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  168. /* set focus */
  169. rtgui_widget_focus(widget);
  170. {
  171. struct rtgui_rect rect;
  172. struct rtgui_dc* dc;
  173. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  174. if (dc != RT_NULL)
  175. {
  176. /* get widget rect */
  177. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  178. /* update focus border */
  179. rect.x2 -= 1; rect.y2 -= 1;
  180. /* draw focused border */
  181. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(box)))
  182. rtgui_dc_draw_focus_rect(dc, &rect);
  183. rtgui_dc_end_drawing(dc);
  184. }
  185. }
  186. if ((index < box->items_count) && (index < box->page_items))
  187. {
  188. rt_uint16_t old_item;
  189. old_item = box->current_item;
  190. /* set selected item */
  191. box->current_item = (box->current_item/box->page_items) * box->page_items + index;
  192. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  193. {
  194. /* down event */
  195. rtgui_listbox_update_current(box, old_item);
  196. }
  197. else
  198. {
  199. /* up event */
  200. if (box->on_item != RT_NULL)
  201. {
  202. box->on_item(RTGUI_WIDGET(box), RT_NULL);
  203. }
  204. }
  205. }
  206. }
  207. return RT_TRUE;
  208. }
  209. case RTGUI_EVENT_KBD:
  210. {
  211. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  212. if ((ekbd->type == RTGUI_KEYDOWN) && (box->items_count > 0))
  213. {
  214. rt_int16_t old_item;
  215. if (box->current_item == -1) return RT_FALSE;
  216. old_item = box->current_item;
  217. switch (ekbd->key)
  218. {
  219. case RTGUIK_LEFT:
  220. if (box->current_item - box->page_items >= 0)
  221. box->current_item -= box->page_items;
  222. rtgui_listbox_update_current(box, old_item);
  223. return RT_FALSE;
  224. case RTGUIK_UP:
  225. if (box->current_item > 0)
  226. box->current_item --;
  227. rtgui_listbox_update_current(box, old_item);
  228. return RT_FALSE;
  229. case RTGUIK_RIGHT:
  230. if (box->current_item + box->page_items < box->items_count - 1)
  231. box->current_item += box->page_items;
  232. rtgui_listbox_update_current(box, old_item);
  233. return RT_FALSE;
  234. case RTGUIK_DOWN:
  235. if (box->current_item < box->items_count - 1)
  236. box->current_item ++;
  237. rtgui_listbox_update_current(box, old_item);
  238. return RT_FALSE;
  239. case RTGUIK_RETURN:
  240. if (box->on_item != RT_NULL)
  241. {
  242. box->on_item(RTGUI_WIDGET(box), RT_NULL);
  243. }
  244. return RT_FALSE;
  245. default:
  246. break;
  247. }
  248. }
  249. }
  250. return RT_FALSE;
  251. }
  252. /* use box event handler */
  253. return rtgui_widget_event_handler(widget, event);
  254. }
  255. rtgui_listbox_t* rtgui_listbox_create(const struct rtgui_listbox_item* items, rt_uint16_t count, rtgui_rect_t *rect)
  256. {
  257. struct rtgui_listbox* box = RT_NULL;
  258. box = (struct rtgui_listbox*) rtgui_widget_create(RTGUI_LISTBOX_TYPE);
  259. if (box != RT_NULL)
  260. {
  261. box->items = items;
  262. box->items_count = count;
  263. box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  264. if (box->page_items == 0) box->page_items = 1;
  265. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  266. }
  267. return box;
  268. }
  269. void rtgui_listbox_destroy(rtgui_listbox_t* box)
  270. {
  271. /* destroy box */
  272. rtgui_widget_destroy(RTGUI_WIDGET(box));
  273. }
  274. void rtgui_listbox_set_onitem(rtgui_listbox_t* box, rtgui_onitem_func_t func)
  275. {
  276. RT_ASSERT(box != RT_NULL);
  277. box->on_item = func;
  278. }
  279. void rtgui_listbox_set_items(rtgui_listbox_t* box, struct rtgui_listbox_item* items, rt_uint16_t count)
  280. {
  281. rtgui_rect_t rect;
  282. box->items = items;
  283. box->items_count = count;
  284. box->current_item = -1;
  285. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  286. box->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  287. rtgui_widget_update(RTGUI_WIDGET(box));
  288. }