listbox.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 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(RTGUI_WIDGET(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. static void rtgui_listbox_update_current(struct rtgui_listbox* box, rt_int16_t old_item)
  86. {
  87. struct rtgui_dc* dc;
  88. const struct rtgui_listbox_item* item;
  89. rtgui_rect_t rect, item_rect, image_rect;
  90. if ((old_item == -1) || (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_get_rect(item->image, &image_rect);
  113. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  114. rtgui_image_blit(item->image, dc, &image_rect);
  115. item_rect.x1 += item->image->w + 2;
  116. }
  117. rtgui_dc_draw_text(dc, item->name, &item_rect);
  118. /* draw current item */
  119. item_rect = rect;
  120. /* get current item's rect */
  121. item_rect.x1 += 1; item_rect.x2 -= 1;
  122. item_rect.y1 += 2;
  123. item_rect.y1 += (box->current_item % box->page_items) * (2 + rtgui_theme_get_selected_height());
  124. item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height());
  125. /* draw current item */
  126. rtgui_theme_draw_selected(dc, &item_rect);
  127. item_rect.x1 += LIST_MARGIN;
  128. item = &(box->items[box->current_item]);
  129. if (item->image != RT_NULL)
  130. {
  131. rtgui_image_get_rect(item->image, &image_rect);
  132. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  133. rtgui_image_blit(item->image, dc, &image_rect);
  134. item_rect.x1 += (item->image->w + 2);
  135. }
  136. rtgui_dc_draw_text(dc, item->name, &item_rect);
  137. rtgui_dc_end_drawing(dc);
  138. }
  139. rt_bool_t rtgui_listbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  140. {
  141. struct rtgui_listbox* box = RT_NULL;
  142. box = RTGUI_LISTBOX(widget);
  143. switch (event->type)
  144. {
  145. case RTGUI_EVENT_PAINT:
  146. rtgui_listbox_ondraw(box);
  147. return RT_FALSE;
  148. case RTGUI_EVENT_RESIZE:
  149. {
  150. struct rtgui_event_resize* resize;
  151. resize = (struct rtgui_event_resize*)event;
  152. /* recalculate page items */
  153. box->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  154. }
  155. break;
  156. case RTGUI_EVENT_MOUSE_BUTTON:
  157. {
  158. rtgui_rect_t rect;
  159. struct rtgui_event_mouse* emouse;
  160. emouse = (struct rtgui_event_mouse*)event;
  161. /* calculate selected item */
  162. /* get physical extent information */
  163. rtgui_widget_get_rect(widget, &rect);
  164. rtgui_widget_rect_to_device(widget, &rect);
  165. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (box->items_count > 0))
  166. {
  167. rt_uint16_t index;
  168. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  169. /* set focus */
  170. rtgui_widget_focus(widget);
  171. {
  172. struct rtgui_rect rect;
  173. struct rtgui_dc* dc;
  174. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  175. if (dc != RT_NULL)
  176. {
  177. /* get widget rect */
  178. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  179. /* update focus border */
  180. rect.x2 -= 1; rect.y2 -= 1;
  181. /* draw focused border */
  182. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(box)))
  183. rtgui_dc_draw_focus_rect(dc, &rect);
  184. rtgui_dc_end_drawing(dc);
  185. }
  186. }
  187. if ((index < box->items_count) && (index < box->page_items))
  188. {
  189. rt_uint16_t old_item;
  190. old_item = box->current_item;
  191. /* set selected item */
  192. box->current_item = (box->current_item/box->page_items) * box->page_items + index;
  193. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  194. {
  195. /* down event */
  196. rtgui_listbox_update_current(box, old_item);
  197. }
  198. else
  199. {
  200. /* up event */
  201. if (box->on_item != RT_NULL)
  202. {
  203. box->on_item(RTGUI_WIDGET(box), RT_NULL);
  204. }
  205. }
  206. }
  207. }
  208. return RT_TRUE;
  209. }
  210. case RTGUI_EVENT_KBD:
  211. {
  212. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  213. if ((ekbd->type == RTGUI_KEYDOWN) && (box->items_count > 0))
  214. {
  215. rt_int16_t old_item;
  216. if (box->current_item == -1) return RT_FALSE;
  217. old_item = box->current_item;
  218. switch (ekbd->key)
  219. {
  220. case RTGUIK_LEFT:
  221. if (box->current_item - box->page_items >= 0)
  222. box->current_item -= box->page_items;
  223. rtgui_listbox_update_current(box, old_item);
  224. return RT_FALSE;
  225. case RTGUIK_UP:
  226. if (box->current_item > 0)
  227. box->current_item --;
  228. rtgui_listbox_update_current(box, old_item);
  229. return RT_FALSE;
  230. case RTGUIK_RIGHT:
  231. if (box->current_item + box->page_items < box->items_count - 1)
  232. box->current_item += box->page_items;
  233. rtgui_listbox_update_current(box, old_item);
  234. return RT_FALSE;
  235. case RTGUIK_DOWN:
  236. if (box->current_item < box->items_count - 1)
  237. box->current_item ++;
  238. rtgui_listbox_update_current(box, old_item);
  239. return RT_FALSE;
  240. case RTGUIK_RETURN:
  241. if (box->on_item != RT_NULL)
  242. {
  243. box->on_item(RTGUI_WIDGET(box), RT_NULL);
  244. }
  245. return RT_FALSE;
  246. default:
  247. break;
  248. }
  249. }
  250. }
  251. return RT_FALSE;
  252. }
  253. /* use box event handler */
  254. return rtgui_widget_event_handler(widget, event);
  255. }
  256. rtgui_listbox_t* rtgui_listbox_create(const struct rtgui_listbox_item* items, rt_uint16_t count, rtgui_rect_t *rect)
  257. {
  258. struct rtgui_listbox* box = RT_NULL;
  259. box = (struct rtgui_listbox*) rtgui_widget_create(RTGUI_LISTBOX_TYPE);
  260. if (box != RT_NULL)
  261. {
  262. box->items = items;
  263. box->items_count = count;
  264. box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  265. if (box->page_items == 0) box->page_items = 1;
  266. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  267. }
  268. return box;
  269. }
  270. void rtgui_listbox_destroy(rtgui_listbox_t* box)
  271. {
  272. /* destroy box */
  273. rtgui_widget_destroy(RTGUI_WIDGET(box));
  274. }
  275. void rtgui_listbox_set_onitem(rtgui_listbox_t* box, rtgui_onitem_func_t func)
  276. {
  277. RT_ASSERT(box != RT_NULL);
  278. box->on_item = func;
  279. }
  280. void rtgui_listbox_set_items(rtgui_listbox_t* box, struct rtgui_listbox_item* items, rt_uint16_t count)
  281. {
  282. rtgui_rect_t rect;
  283. box->items = items;
  284. box->items_count = count;
  285. box->current_item = -1;
  286. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  287. box->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  288. rtgui_widget_update(RTGUI_WIDGET(box));
  289. }