demo_listview.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "demo_view.h"
  2. #include <rtgui/widgets/label.h>
  3. #include <rtgui/widgets/button.h>
  4. #include <rtgui/widgets/window.h>
  5. #include <rtgui/widgets/list_view.h>
  6. static rtgui_workbench_t* workbench = RT_NULL;
  7. static rtgui_list_view_t* _view = RT_NULL;
  8. static rtgui_image_t* return_image = RT_NULL;
  9. static void listitem_action(void* parameter)
  10. {
  11. char label_text[32];
  12. rtgui_win_t *win;
  13. rtgui_label_t *label;
  14. rtgui_rect_t rect = {0, 0, 150, 80};
  15. int no = (int)parameter;
  16. rtgui_rect_moveto(&rect, 20, 50);
  17. /* 显示消息窗口 */
  18. win = rtgui_win_create(RTGUI_TOPLEVEL(workbench),
  19. "窗口", &rect, RTGUI_WIN_STYLE_DEFAULT);
  20. rect.x1 += 20;
  21. rect.x2 -= 5;
  22. rect.y1 += 5;
  23. rect.y2 = rect.y1 + 20;
  24. rt_sprintf(label_text, "动作 %d", no);
  25. label = rtgui_label_create(label_text);
  26. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  27. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  28. /* 非模态显示窗口 */
  29. rtgui_win_show(win, RT_FALSE);
  30. }
  31. static void return_action(void* parameter)
  32. {
  33. if (_view != RT_NULL)
  34. {
  35. rtgui_view_destroy(RTGUI_VIEW(_view));
  36. _view = RT_NULL;
  37. }
  38. }
  39. static struct rtgui_list_item items[] =
  40. {
  41. {"列表项1", RT_NULL, listitem_action, (void*)1},
  42. {"列表项2", RT_NULL, listitem_action, (void*)2},
  43. {"列表项3", RT_NULL, listitem_action, (void*)3},
  44. {"列表项4", RT_NULL, listitem_action, (void*)4},
  45. {"列表项5", RT_NULL, listitem_action, (void*)5},
  46. {"返回", RT_NULL, return_action, RT_NULL},
  47. };
  48. static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
  49. {
  50. /* create a file list view */
  51. rtgui_rect_t rect;
  52. workbench = RTGUI_WORKBENCH(rtgui_widget_get_toplevel(widget));
  53. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  54. _view = rtgui_list_view_create(items, sizeof(items)/sizeof(struct rtgui_list_item),
  55. &rect);
  56. rtgui_workbench_add_view(workbench, RTGUI_VIEW(_view));
  57. /* 模式显示视图 */
  58. rtgui_view_show(RTGUI_VIEW(_view), RT_FALSE);
  59. }
  60. rtgui_view_t* demo_listview_view(rtgui_workbench_t* workbench)
  61. {
  62. rtgui_rect_t rect;
  63. rtgui_view_t *view;
  64. rtgui_button_t* open_btn;
  65. view = demo_view(workbench, "列表视图演示");
  66. demo_view_get_rect(view, &rect);
  67. rect.x1 += 5;
  68. rect.x2 = rect.x1 + 80;
  69. rect.y1 += 30;
  70. rect.y2 = rect.y1 + 20;
  71. open_btn = rtgui_button_create("打开列表");
  72. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(open_btn));
  73. rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
  74. rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
  75. return view;
  76. }