demo_view.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_app.h>
  3. #include <rtgui/widgets/container.h>
  4. #include <rtgui/widgets/notebook.h>
  5. #include <rtgui/widgets/button.h>
  6. #include <rtgui/widgets/staticline.h>
  7. #include <rtgui/widgets/box.h>
  8. extern struct rtgui_notebook *the_notebook;
  9. void demo_view_next(struct rtgui_object *object, struct rtgui_event *event)
  10. {
  11. rtgui_notebook_set_current_by_index(the_notebook,
  12. (rtgui_notebook_get_current_index(the_notebook) + 1) %
  13. rtgui_notebook_get_count(the_notebook));
  14. }
  15. void demo_view_prev(struct rtgui_object *object, struct rtgui_event *event)
  16. {
  17. rt_int16_t cur_idx = rtgui_notebook_get_current_index(the_notebook);
  18. if (cur_idx == 0)
  19. rtgui_notebook_set_current_by_index(the_notebook,
  20. rtgui_notebook_get_count(the_notebook) - 1);
  21. else
  22. rtgui_notebook_set_current_by_index(the_notebook,
  23. --cur_idx);
  24. }
  25. rtgui_container_t *demo_view(const char *title)
  26. {
  27. struct rtgui_container *container;
  28. struct rtgui_label *label;
  29. struct rtgui_staticline *line;
  30. struct rtgui_button *next_btn, *prev_btn;
  31. struct rtgui_rect rect;
  32. container = rtgui_container_create();
  33. if (container == RT_NULL)
  34. return RT_NULL;
  35. rtgui_notebook_add(the_notebook, title, RTGUI_WIDGET(container));
  36. /* 获得视图的位置信息(在加入到 notebook 中时,notebook 会自动调整 container
  37. * 的大小) */
  38. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  39. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect);
  40. rect.x1 += 5;
  41. rect.y1 += 5;
  42. rect.x2 = rect.x1 + rt_strlen(title)*8;
  43. rect.y2 = rect.y1 + 20;
  44. /* 创建标题用的标签 */
  45. label = rtgui_label_create(title);
  46. /* 设置标签位置信息 */
  47. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  48. /* 添加标签到视图中 */
  49. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  50. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  51. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect);
  52. rect.y1 += 20 + 5;
  53. rect.y2 = rect.y1 + 2;
  54. /* 创建一个水平的 staticline 线 */
  55. line = rtgui_staticline_create(RTGUI_HORIZONTAL);
  56. /* 设置静态线的位置信息 */
  57. rtgui_widget_set_rect(RTGUI_WIDGET(line), &rect);
  58. /* 添加静态线到视图中 */
  59. rtgui_container_add_child(container, RTGUI_WIDGET(line));
  60. /* 获得视图的位置信息 */
  61. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  62. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect);
  63. rect.x2 -= 5;
  64. rect.y2 -= 5;
  65. rect.x1 = rect.x2 - 100;
  66. rect.y1 = rect.y2 - 25;
  67. /* 创建"下一个"按钮 */
  68. next_btn = rtgui_button_create("下一个");
  69. /* 设置onbutton动作到demo_view_next函数 */
  70. rtgui_button_set_onbutton(next_btn, demo_view_next);
  71. /* 设置按钮的位置信息 */
  72. rtgui_widget_set_rect(RTGUI_WIDGET(next_btn), &rect);
  73. /* 添加按钮到视图中 */
  74. rtgui_container_add_child(container, RTGUI_WIDGET(next_btn));
  75. /* 获得视图的位置信息 */
  76. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  77. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect);
  78. rect.x1 += 5;
  79. rect.y2 -= 5;
  80. rect.x2 = rect.x1 + 100;
  81. rect.y1 = rect.y2 - 25;
  82. /* 创建"上一个"按钮 */
  83. prev_btn = rtgui_button_create("上一个");
  84. /* 设置onbutton动作到demo_view_prev函数 */
  85. rtgui_button_set_onbutton(prev_btn, demo_view_prev);
  86. /* 设置按钮的位置信息 */
  87. rtgui_widget_set_rect(RTGUI_WIDGET(prev_btn), &rect);
  88. /* 添加按钮到视图中 */
  89. rtgui_container_add_child(container, RTGUI_WIDGET(prev_btn));
  90. /* 返回创建的视图 */
  91. return container;
  92. }
  93. /* 这个函数用于返回演示视图的对外可用区域 */
  94. void demo_view_get_rect(rtgui_container_t *container, rtgui_rect_t *rect)
  95. {
  96. RT_ASSERT(container != RT_NULL);
  97. RT_ASSERT(rect != RT_NULL);
  98. rtgui_widget_get_rect(RTGUI_WIDGET(container), rect);
  99. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), rect);
  100. /* 去除演示标题和下方按钮的区域 */
  101. rect->y1 += 45;
  102. rect->y2 -= 35;
  103. }
  104. void demo_view_get_logic_rect(rtgui_container_t *container, rtgui_rect_t *rect)
  105. {
  106. RT_ASSERT(container != RT_NULL);
  107. RT_ASSERT(rect != RT_NULL);
  108. rtgui_widget_get_rect(RTGUI_WIDGET(container), rect);
  109. /* 去除演示标题和下方按钮的区域 */
  110. rect->y1 += 45;
  111. rect->y2 -= 35;
  112. }