demo_view.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 -= 5;
  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. rect.y1 += 20;
  51. rect.y2 += 20;
  52. /* 创建一个水平的 staticline 线 */
  53. line = rtgui_staticline_create(RTGUI_HORIZONTAL);
  54. /* 设置静态线的位置信息 */
  55. rtgui_widget_set_rect(RTGUI_WIDGET(line), &rect);
  56. /* 添加静态线到视图中 */
  57. rtgui_container_add_child(container, RTGUI_WIDGET(line));
  58. /* 获得视图的位置信息 */
  59. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  60. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect);
  61. rect.x2 -= 5;
  62. rect.y2 -= 5;
  63. rect.x1 = rect.x2 - 100;
  64. rect.y1 = rect.y2 - 25;
  65. /* 创建"下一个"按钮 */
  66. next_btn = rtgui_button_create("下一个");
  67. /* 设置onbutton动作到demo_view_next函数 */
  68. rtgui_button_set_onbutton(next_btn, demo_view_next);
  69. /* 设置按钮的位置信息 */
  70. rtgui_widget_set_rect(RTGUI_WIDGET(next_btn), &rect);
  71. /* 添加按钮到视图中 */
  72. rtgui_container_add_child(container, RTGUI_WIDGET(next_btn));
  73. /* 获得视图的位置信息 */
  74. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  75. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect);
  76. rect.x1 += 5;
  77. rect.y2 -= 5;
  78. rect.x2 = rect.x1 + 100;
  79. rect.y1 = rect.y2 - 25;
  80. /* 创建"上一个"按钮 */
  81. prev_btn = rtgui_button_create("上一个");
  82. /* 设置onbutton动作到demo_view_prev函数 */
  83. rtgui_button_set_onbutton(prev_btn, demo_view_prev);
  84. /* 设置按钮的位置信息 */
  85. rtgui_widget_set_rect(RTGUI_WIDGET(prev_btn), &rect);
  86. /* 添加按钮到视图中 */
  87. rtgui_container_add_child(container, RTGUI_WIDGET(prev_btn));
  88. /* 返回创建的视图 */
  89. return container;
  90. }
  91. /* 这个函数用于返回演示视图的对外可用区域 */
  92. void demo_view_get_rect(rtgui_container_t* container, rtgui_rect_t *rect)
  93. {
  94. RT_ASSERT(container != RT_NULL);
  95. RT_ASSERT(rect != RT_NULL);
  96. rtgui_widget_get_rect(RTGUI_WIDGET(container), rect);
  97. rtgui_widget_rect_to_device(RTGUI_WIDGET(container), rect);
  98. /* 去除演示标题和下方按钮的区域 */
  99. rect->y1 += 45;
  100. rect->y2 -= 35;
  101. }
  102. void demo_view_get_logic_rect(rtgui_container_t* container, rtgui_rect_t *rect)
  103. {
  104. RT_ASSERT(container != RT_NULL);
  105. RT_ASSERT(rect != RT_NULL);
  106. rtgui_widget_get_rect(RTGUI_WIDGET(container), rect);
  107. /* 去除演示标题和下方按钮的区域 */
  108. rect->y1 += 45;
  109. rect->y2 -= 35;
  110. }
  111. /* 当是标准版本时,这个函数用于返回自动布局引擎box控件 */
  112. #ifndef RTGUI_USING_SMALL_SIZE
  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, 5);
  123. /* 添加box控件到视图中 */
  124. rtgui_container_add_child(container, RTGUI_WIDGET(box));
  125. return box;
  126. }
  127. #endif