demo_view.c 4.3 KB

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