demo_view.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/widgets/view.h>
  3. #include <rtgui/widgets/button.h>
  4. #include <rtgui/widgets/workbench.h>
  5. #include <rtgui/widgets/staticline.h>
  6. /* 用于存放演示视图的数组,最多可创建32个演示视图 */
  7. static rtgui_view_t* demo_view_list[32];
  8. /* 当前演示视图索引 */
  9. static rt_uint16_t demo_view_current = 0;
  10. /* 总共包括的演示视图数目 */
  11. static rt_uint16_t demo_view_number = 0;
  12. /* 显示下一个演示视图 */
  13. void demo_view_next(struct rtgui_widget* widget, rtgui_event_t *event)
  14. {
  15. if (demo_view_current + 1< demo_view_number)
  16. {
  17. demo_view_current ++;
  18. rtgui_view_show(demo_view_list[demo_view_current], RT_FALSE);
  19. }
  20. }
  21. /* 显示前一个演示视图 */
  22. void demo_view_prev(struct rtgui_widget* widget, rtgui_event_t *event)
  23. {
  24. if (demo_view_current != 0)
  25. {
  26. demo_view_current --;
  27. rtgui_view_show(demo_view_list[demo_view_current], RT_FALSE);
  28. }
  29. }
  30. /* 创建一个演示视图,需提供父workbench和演示用的标题 */
  31. rtgui_view_t* demo_view(rtgui_workbench_t* workbench, const char* title)
  32. {
  33. struct rtgui_view* view;
  34. /* 设置视图的名称 */
  35. view = rtgui_view_create(title);
  36. if (view == RT_NULL) return RT_NULL;
  37. /* 创建成功后,添加到数组中 */
  38. demo_view_list[demo_view_number] = view;
  39. demo_view_number ++;
  40. /* 添加到父workbench中 */
  41. rtgui_workbench_add_view(workbench, view);
  42. /* 添加下一个视图和前一个视图按钮 */
  43. {
  44. struct rtgui_rect rect;
  45. struct rtgui_button *next_btn, *prev_btn;
  46. struct rtgui_label *label;
  47. struct rtgui_staticline *line;
  48. /* 获得视图的位置信息(在加入到workbench中时,workbench会自动调整视图的大小) */
  49. rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect);
  50. rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect);
  51. rect.x1 += 5;
  52. rect.y1 += 5;
  53. rect.x2 -= 5;
  54. rect.y2 = rect.y1 + 20;
  55. /* 创建标题用的标签 */
  56. label = rtgui_label_create(title);
  57. /* 设置标签位置信息 */
  58. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  59. /* 添加标签到视图中 */
  60. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label));
  61. rect.y1 += 20;
  62. rect.y2 += 20;
  63. /* 创建一个水平的staticline线 */
  64. line = rtgui_staticline_create(RTGUI_HORIZONTAL);
  65. /* 设置静态线的位置信息 */
  66. rtgui_widget_set_rect(RTGUI_WIDGET(line), &rect);
  67. /* 添加静态线到视图中 */
  68. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(line));
  69. /* 获得视图的位置信息 */
  70. rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect);
  71. rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect);
  72. rect.x2 -= 5;
  73. rect.y2 -= 5;
  74. rect.x1 = rect.x2 - 100;
  75. rect.y1 = rect.y2 - 25;
  76. /* 创建"下一个"按钮 */
  77. next_btn = rtgui_button_create("下一个");
  78. /* 设置onbutton动作到demo_view_next函数 */
  79. rtgui_button_set_onbutton(next_btn, demo_view_next);
  80. /* 设置按钮的位置信息 */
  81. rtgui_widget_set_rect(RTGUI_WIDGET(next_btn), &rect);
  82. /* 添加按钮到视图中 */
  83. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(next_btn));
  84. /* 获得视图的位置信息 */
  85. rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect);
  86. rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect);
  87. rect.x1 += 5;
  88. rect.y2 -= 5;
  89. rect.x2 = rect.x1 + 100;
  90. rect.y1 = rect.y2 - 25;
  91. /* 创建"上一个"按钮 */
  92. prev_btn = rtgui_button_create("上一个");
  93. /* 设置onbutton动作到demo_view_prev函数 */
  94. rtgui_button_set_onbutton(prev_btn, demo_view_prev);
  95. /* 设置按钮的位置信息 */
  96. rtgui_widget_set_rect(RTGUI_WIDGET(prev_btn), &rect);
  97. /* 添加按钮到视图中 */
  98. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(prev_btn));
  99. }
  100. /* 返回创建的视图 */
  101. return view;
  102. }
  103. /* 这个函数用于返回演示视图的对外可用区域 */
  104. void demo_view_get_rect(rtgui_view_t* view, rtgui_rect_t *rect)
  105. {
  106. RT_ASSERT(view != RT_NULL);
  107. RT_ASSERT(rect != RT_NULL);
  108. rtgui_widget_get_rect(RTGUI_WIDGET(view), rect);
  109. rtgui_widget_rect_to_device(RTGUI_WIDGET(view), rect);
  110. /* 去除演示标题和下方按钮的区域 */
  111. rect->y1 += 45;
  112. rect->y2 -= 35;
  113. }
  114. void demo_view_get_logic_rect(rtgui_view_t* view, rtgui_rect_t *rect)
  115. {
  116. RT_ASSERT(view != RT_NULL);
  117. RT_ASSERT(rect != RT_NULL);
  118. rtgui_widget_get_rect(RTGUI_WIDGET(view), rect);
  119. /* 去除演示标题和下方按钮的区域 */
  120. rect->y1 += 45;
  121. rect->y2 -= 35;
  122. }
  123. /* 当是标准版本时,这个函数用于返回自动布局引擎box控件 */
  124. #ifndef RTGUI_USING_SMALL_SIZE
  125. rtgui_box_t* demo_view_create_box(rtgui_view_t* view, int orient)
  126. {
  127. rtgui_rect_t rect;
  128. rtgui_box_t* box;
  129. /* 获得视图的位置信息 */
  130. rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect);
  131. rect.y1 += 45;
  132. rect.y2 -= 25;
  133. /* 创建一个自动布局引擎 */
  134. box = rtgui_box_create(orient, &rect);
  135. /* 添加box控件到视图中 */
  136. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(box));
  137. return box;
  138. }
  139. #endif
  140. /* 这个函数用于显示当前的视图 */
  141. void demo_view_show()
  142. {
  143. if (demo_view_number != 0)
  144. {
  145. rtgui_view_show(demo_view_list[demo_view_current], RT_FALSE);
  146. }
  147. }