demo_view_listbox.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * 程序清单:label控件演示
  3. *
  4. * 这个例子会在创建出的view上添加几个不同类型的label控件
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/widgets/label.h>
  8. #include <rtgui/widgets/listbox.h>
  9. const static struct rtgui_listbox_item items[] =
  10. {
  11. {"list #0", RT_NULL},
  12. {"list #1", RT_NULL},
  13. {"list #2", RT_NULL},
  14. {"list #3", RT_NULL},
  15. };
  16. static void on_items(rtgui_widget_t* widget, struct rtgui_event* event)
  17. {
  18. rtgui_listbox_t* box;
  19. /* get listbox */
  20. box = RTGUI_LISTBOX(widget);
  21. /* 打印当前的项 */
  22. rt_kprintf("current item: %d\n", box->current_item);
  23. }
  24. /* 创建用于演示label控件的视图 */
  25. rtgui_view_t* demo_view_listbox(rtgui_workbench_t* workbench)
  26. {
  27. rtgui_rect_t rect;
  28. rtgui_view_t* view;
  29. rtgui_label_t* label;
  30. rtgui_listbox_t* box;
  31. /* 先创建一个演示用的视图 */
  32. view = demo_view(workbench, "ListBox Demo");
  33. /* 获得视图的位置信息 */
  34. demo_view_get_rect(view, &rect);
  35. rect.x1 += 5;
  36. rect.x2 -= 5;
  37. rect.y1 += 5;
  38. rect.y2 = rect.y1 + 20;
  39. /* 创建一个label控件 */
  40. label = rtgui_label_create("listbox: ");
  41. /* 设置label的位置 */
  42. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  43. /* view是一个container控件,调用add_child方法添加这个label控件 */
  44. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label));
  45. rect.y1 = rect.y2 + 3;
  46. rect.y2 = 250;
  47. box = rtgui_listbox_create(items, sizeof(items)/sizeof(struct rtgui_listbox_item), &rect);
  48. rtgui_listbox_set_onitem(box, on_items);
  49. /* view是一个container控件,调用add_child方法添加这个listbox控件 */
  50. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(box));
  51. return view;
  52. }