listbox.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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(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_object* object, struct rtgui_event* event)
  140. {
  141. struct rtgui_listbox* box;
  142. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  143. box = RTGUI_LISTBOX(object);
  144. switch (event->type)
  145. {
  146. case RTGUI_EVENT_PAINT:
  147. rtgui_listbox_ondraw(box);
  148. return RT_FALSE;
  149. case RTGUI_EVENT_RESIZE:
  150. {
  151. struct rtgui_event_resize* resize;
  152. resize = (struct rtgui_event_resize*)event;
  153. /* recalculate page items */
  154. box->page_items = resize->h / (2 + rtgui_theme_get_selected_height());
  155. }
  156. break;
  157. case RTGUI_EVENT_MOUSE_BUTTON:
  158. {
  159. rtgui_rect_t rect;
  160. struct rtgui_event_mouse* emouse;
  161. emouse = (struct rtgui_event_mouse*)event;
  162. /* calculate selected item */
  163. /* get physical extent information */
  164. rtgui_widget_get_rect(widget, &rect);
  165. rtgui_widget_rect_to_device(widget, &rect);
  166. if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (box->items_count > 0))
  167. {
  168. rt_uint16_t index;
  169. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  170. /* set focus */
  171. rtgui_widget_focus(widget);
  172. {
  173. struct rtgui_rect rect;
  174. struct rtgui_dc* dc;
  175. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  176. if (dc != RT_NULL)
  177. {
  178. /* get widget rect */
  179. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  180. /* update focus border */
  181. rect.x2 -= 1; rect.y2 -= 1;
  182. /* draw focused border */
  183. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(box)))
  184. rtgui_dc_draw_focus_rect(dc, &rect);
  185. rtgui_dc_end_drawing(dc);
  186. }
  187. }
  188. if ((index < box->items_count) && (index < box->page_items))
  189. {
  190. rt_uint16_t old_item;
  191. old_item = box->current_item;
  192. /* set selected item */
  193. box->current_item = (box->current_item/box->page_items) * box->page_items + index;
  194. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  195. {
  196. /* down event */
  197. rtgui_listbox_update_current(box, old_item);
  198. }
  199. else
  200. {
  201. /* up event */
  202. if (box->on_item != RT_NULL)
  203. {
  204. box->on_item(RTGUI_OBJECT(box), RT_NULL);
  205. }
  206. }
  207. }
  208. }
  209. return RT_TRUE;
  210. }
  211. case RTGUI_EVENT_KBD:
  212. {
  213. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  214. if ((ekbd->type == RTGUI_KEYDOWN) && (box->items_count > 0))
  215. {
  216. rt_int16_t old_item;
  217. if (box->current_item == -1)
  218. {
  219. /* set a initial item */
  220. if ((box->items_count > 0) &&
  221. (ekbd->key == RTGUIK_UP || ekbd->key == RTGUIK_DOWN))
  222. {
  223. box->current_item = 0;
  224. rtgui_listbox_update_current(box, -1);
  225. return RT_TRUE;
  226. }
  227. else
  228. return RT_FALSE;
  229. }
  230. old_item = box->current_item;
  231. switch (ekbd->key)
  232. {
  233. case RTGUIK_LEFT:
  234. if (box->current_item - box->page_items >= 0)
  235. box->current_item -= box->page_items;
  236. rtgui_listbox_update_current(box, old_item);
  237. return RT_TRUE;
  238. case RTGUIK_UP:
  239. if (box->current_item > 0)
  240. box->current_item --;
  241. rtgui_listbox_update_current(box, old_item);
  242. return RT_TRUE;
  243. case RTGUIK_RIGHT:
  244. if (box->current_item + box->page_items < box->items_count - 1)
  245. box->current_item += box->page_items;
  246. rtgui_listbox_update_current(box, old_item);
  247. return RT_TRUE;
  248. case RTGUIK_DOWN:
  249. if (box->current_item < box->items_count - 1)
  250. box->current_item ++;
  251. rtgui_listbox_update_current(box, old_item);
  252. return RT_TRUE;
  253. case RTGUIK_RETURN:
  254. if (box->on_item != RT_NULL)
  255. box->on_item(RTGUI_OBJECT(box), RT_NULL);
  256. return RT_TRUE;
  257. default:
  258. break;
  259. }
  260. }
  261. }
  262. return RT_FALSE;
  263. }
  264. /* use box event handler */
  265. return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
  266. }
  267. rtgui_listbox_t* rtgui_listbox_create(const struct rtgui_listbox_item* items, rt_uint16_t count, rtgui_rect_t *rect)
  268. {
  269. struct rtgui_listbox* box = RT_NULL;
  270. box = (struct rtgui_listbox*) rtgui_widget_create(RTGUI_LISTBOX_TYPE);
  271. if (box != RT_NULL)
  272. {
  273. box->items = items;
  274. box->items_count = count;
  275. box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height());
  276. if (box->page_items == 0) box->page_items = 1;
  277. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  278. }
  279. return box;
  280. }
  281. void rtgui_listbox_destroy(rtgui_listbox_t* box)
  282. {
  283. /* destroy box */
  284. rtgui_widget_destroy(RTGUI_WIDGET(box));
  285. }
  286. void rtgui_listbox_set_onitem(rtgui_listbox_t* box, rtgui_event_handler_ptr func)
  287. {
  288. RT_ASSERT(box != RT_NULL);
  289. box->on_item = func;
  290. }
  291. void rtgui_listbox_set_items(rtgui_listbox_t* box, struct rtgui_listbox_item* items, rt_uint16_t count)
  292. {
  293. rtgui_rect_t rect;
  294. box->items = items;
  295. box->items_count = count;
  296. box->current_item = -1;
  297. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  298. box->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height());
  299. rtgui_widget_update(RTGUI_WIDGET(box));
  300. }
  301. void rtgui_listbox_set_current_item(rtgui_listbox_t* box, int index)
  302. {
  303. RT_ASSERT(box != RT_NULL);
  304. if (index != box->current_item)
  305. {
  306. int old_item;
  307. old_item = box->current_item;
  308. box->current_item = index;
  309. rtgui_listbox_update_current(box, old_item);
  310. }
  311. }