demo_listview.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * 程序清单:列表视图演示
  3. *
  4. * 这个例子会先创建出一个演示用的view,当点击上面的按钮时会按照模式显示的形式显示
  5. * 新的列表视图
  6. */
  7. #include "demo_view.h"
  8. #include <rtgui/widgets/label.h>
  9. #include <rtgui/widgets/button.h>
  10. #include <rtgui/widgets/window.h>
  11. #include <rtgui/widgets/list_view.h>
  12. static rtgui_workbench_t* workbench = RT_NULL;
  13. static rtgui_list_view_t* _view = RT_NULL;
  14. // static rtgui_image_t* return_image = RT_NULL;
  15. /* 列表项的动作函数 */
  16. #if RT_VERSION == 4
  17. static void listitem_action(rtgui_widget_t *widget, void* parameter)
  18. #else
  19. static void listitem_action(void* parameter)
  20. #endif
  21. {
  22. char label_text[32];
  23. rtgui_win_t *win;
  24. rtgui_label_t *label;
  25. rtgui_rect_t rect = {0, 0, 150, 80};
  26. int no = (int)parameter;
  27. rtgui_rect_moveto(&rect, 20, 50);
  28. /* 显示消息窗口 */
  29. win = rtgui_win_create(RTGUI_TOPLEVEL(workbench),
  30. "窗口", &rect, RTGUI_WIN_STYLE_DEFAULT);
  31. rect.x1 += 20;
  32. rect.x2 -= 5;
  33. rect.y1 += 5;
  34. rect.y2 = rect.y1 + 20;
  35. /* 添加相应的标签 */
  36. rt_sprintf(label_text, "动作 %d", no);
  37. label = rtgui_label_create(label_text);
  38. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  39. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  40. /* 非模态显示窗口 */
  41. rtgui_win_show(win, RT_FALSE);
  42. }
  43. /* 返回功能的动作函数 */
  44. #if RT_VERSION == 4
  45. static void return_action(rtgui_widget_t* widget, void* parameter)
  46. #else
  47. static void return_action(void* parameter)
  48. #endif
  49. {
  50. rtgui_view_end_modal(RTGUI_VIEW(_view), RTGUI_MODAL_OK);
  51. }
  52. /* 各个列表项定义 */
  53. static struct rtgui_list_item items[] =
  54. {
  55. {"列表项1", RT_NULL, listitem_action, (void*)1},
  56. {"列表项2", RT_NULL, listitem_action, (void*)2},
  57. {"列表项3", RT_NULL, listitem_action, (void*)3},
  58. {"列表项4", RT_NULL, listitem_action, (void*)4},
  59. {"列表项5", RT_NULL, listitem_action, (void*)5},
  60. {"返回", RT_NULL, return_action, RT_NULL},
  61. };
  62. /* 打开列表视图用的按钮触发函数 */
  63. static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
  64. {
  65. rtgui_rect_t rect;
  66. /* 获得顶层的workbench */
  67. workbench = RTGUI_WORKBENCH(rtgui_widget_get_toplevel(widget));
  68. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  69. /* 创建一个列表视图, 项指定为items */
  70. _view = rtgui_list_view_create(items, sizeof(items)/sizeof(struct rtgui_list_item),
  71. &rect, RTGUI_LIST_VIEW_LIST);
  72. /* 在workbench中添加相应的视图 */
  73. rtgui_workbench_add_view(workbench, RTGUI_VIEW(_view));
  74. /* 模式显示视图 */
  75. rtgui_view_show(RTGUI_VIEW(_view), RT_TRUE);
  76. rtgui_view_destroy(RTGUI_VIEW(_view));
  77. _view = RT_NULL;
  78. }
  79. /* 创建用于演示列表视图的视图 */
  80. rtgui_view_t* demo_listview_view(rtgui_workbench_t* workbench)
  81. {
  82. rtgui_rect_t rect;
  83. rtgui_view_t *view;
  84. rtgui_button_t* open_btn;
  85. view = demo_view(workbench, "列表视图演示");
  86. /* 添加动作按钮 */
  87. demo_view_get_rect(view, &rect);
  88. rect.x1 += 5;
  89. rect.x2 = rect.x1 + 80;
  90. rect.y1 += 30;
  91. rect.y2 = rect.y1 + 20;
  92. open_btn = rtgui_button_create("打开列表");
  93. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(open_btn));
  94. rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
  95. rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
  96. return view;
  97. }