demo_view_window.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/window.h>
  4. #include <rtgui/widgets/label.h>
  5. #include <rtgui/widgets/button.h>
  6. #include "demo_view.h"
  7. static struct rtgui_timer *timer;
  8. static struct rtgui_label* label;
  9. static struct rtgui_win* msgbox = RT_NULL;
  10. static rt_uint8_t label_text[80];
  11. static rt_uint8_t cnt = 5;
  12. /* 获取一个递增的窗口标题 */
  13. static char* get_win_title()
  14. {
  15. static rt_uint8_t win_no = 0;
  16. static char win_title[16];
  17. rt_sprintf(win_title, "窗口 %d", ++win_no);
  18. return win_title;
  19. }
  20. /* 窗口关闭时的事件处理 */
  21. void window_demo_close(struct rtgui_widget* widget, rtgui_event_t *even)
  22. {
  23. rtgui_win_t* win;
  24. /* 获得最顶层控件 */
  25. win = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
  26. /* 销毁窗口 */
  27. rtgui_win_destroy(win);
  28. }
  29. /* 关闭对话框时的回调函数 */
  30. void diag_close(struct rtgui_timer* timer, void* parameter)
  31. {
  32. cnt --;
  33. sprintf(label_text, "closed then %d second!", cnt);
  34. /* 设置标签文本并更新控件 */
  35. rtgui_label_set_text(label, label_text);
  36. rtgui_widget_update(RTGUI_WIDGET(label));
  37. if (cnt == 0)
  38. {
  39. /* 超时,关闭对话框 */
  40. rtgui_win_destroy(msgbox);
  41. /* 停止并删除定时器 */
  42. rtgui_timer_stop(timer);
  43. rtgui_timer_destory(timer);
  44. }
  45. }
  46. static rt_uint16_t delta_x = 20;
  47. static rt_uint16_t delta_y = 40;
  48. /* 触发正常窗口显示 */
  49. static void demo_win_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  50. {
  51. rtgui_win_t *win;
  52. rtgui_label_t *label;
  53. rtgui_toplevel_t *parent;
  54. rtgui_rect_t rect = {0, 0, 150, 80};
  55. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  56. rtgui_rect_moveto(&rect, delta_x, delta_y);
  57. delta_x += 20;
  58. delta_y += 20;
  59. /* 创建一个窗口 */
  60. win = rtgui_win_create(parent,
  61. get_win_title(), &rect, RTGUI_WIN_STYLE_DEFAULT);
  62. rect.x1 += 20;
  63. rect.x2 -= 5;
  64. rect.y1 += 5;
  65. rect.y2 = rect.y1 + 20;
  66. label = rtgui_label_create("这是一个普通窗口");
  67. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  68. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  69. /* 非模态显示窗口 */
  70. rtgui_win_show(win, RT_FALSE);
  71. }
  72. /* 触发自动窗口显示 */
  73. static void demo_autowin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  74. {
  75. rtgui_toplevel_t *parent;
  76. struct rtgui_rect rect =
  77. {
  78. 50, 50, 200, 200
  79. };
  80. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  81. msgbox = rtgui_win_create(parent, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
  82. if (msgbox != RT_NULL)
  83. {
  84. cnt = 5;
  85. sprintf(label_text, "closed then %d second!", cnt);
  86. label = rtgui_label_create(label_text);
  87. rect.x1 += 5;
  88. rect.x2 -= 5;
  89. rect.y1 += 5;
  90. rect.y2 = rect.y1 + 20;
  91. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  92. rtgui_container_add_child(RTGUI_CONTAINER(msgbox), RTGUI_WIDGET(label));
  93. rtgui_win_show(msgbox, RT_FALSE);
  94. }
  95. /* 创建一个定时器 */
  96. timer = rtgui_timer_create(100, RT_TIMER_FLAG_PERIODIC, diag_close, RT_NULL);
  97. rtgui_timer_start(timer);
  98. }
  99. /* 触发模态窗口显示 */
  100. static void demo_modalwin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  101. {
  102. rtgui_win_t *win;
  103. rtgui_label_t *label;
  104. rtgui_toplevel_t *parent;
  105. rtgui_rect_t rect = {0, 0, 150, 80};
  106. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  107. rtgui_rect_moveto(&rect, delta_x, delta_y);
  108. delta_x += 20;
  109. delta_y += 20;
  110. /* 创建一个窗口 */
  111. win = rtgui_win_create(parent,
  112. get_win_title(), &rect, RTGUI_WIN_STYLE_DEFAULT);
  113. rect.x1 += 20;
  114. rect.x2 -= 5;
  115. rect.y1 += 5;
  116. rect.y2 = rect.y1 + 20;
  117. label = rtgui_label_create("这是一个模式窗口");
  118. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  119. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  120. /* 模态显示窗口 */
  121. rtgui_win_show(win, RT_TRUE);
  122. /* 采用模态显示窗口,关闭时不会自行删除窗口,需要主动删除窗口 */
  123. rtgui_win_destroy(win);
  124. }
  125. /* 触发无标题窗口显示 */
  126. static void demo_ntitlewin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  127. {
  128. rtgui_win_t *win;
  129. rtgui_label_t *label;
  130. rtgui_button_t *button;
  131. rtgui_toplevel_t *parent;
  132. rtgui_rect_t widget_rect, rect = {0, 0, 150, 80};
  133. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  134. rtgui_rect_moveto(&rect, delta_x, delta_y);
  135. delta_x += 20;
  136. delta_y += 20;
  137. /* 创建一个窗口,风格为无标题及无边框 */
  138. win = rtgui_win_create(parent,
  139. "no title", &rect, RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  140. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win)) = white;
  141. /* 创建一个文本标签 */
  142. label = rtgui_label_create("无边框窗口");
  143. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(label)), "无边框窗口", &widget_rect);
  144. rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
  145. widget_rect.y1 += 20;
  146. widget_rect.y2 += 20;
  147. rtgui_widget_set_rect(RTGUI_WIDGET(label), &widget_rect);
  148. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  149. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
  150. /* 创建一个关闭按钮 */
  151. widget_rect.x1 = 0;
  152. widget_rect.y1 = 0;
  153. widget_rect.x2 = 40;
  154. widget_rect.y2 = 20;
  155. rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
  156. widget_rect.y1 += 40;
  157. widget_rect.y2 += 40;
  158. button = rtgui_button_create("关闭");
  159. rtgui_widget_set_rect(RTGUI_WIDGET(button), &widget_rect);
  160. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(button));
  161. rtgui_button_set_onbutton(button, window_demo_close);
  162. /* 非模态显示窗口 */
  163. rtgui_win_show(win, RT_FALSE);
  164. }
  165. rtgui_view_t* demo_view_window(rtgui_workbench_t* workbench)
  166. {
  167. rtgui_rect_t rect;
  168. rtgui_view_t* view;
  169. rtgui_button_t *button;
  170. /* 创建一个演示用的视图 */
  171. view = demo_view(workbench, "Window Demo");
  172. demo_view_get_rect(view, &rect);
  173. rect.x1 += 5;
  174. rect.x2 = rect.x1 + 100;
  175. rect.y1 += 5;
  176. rect.y2 = rect.y1 + 20;
  177. /* 创建按钮用于显示正常窗口 */
  178. button = rtgui_button_create("Normal Win");
  179. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  180. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  181. /* 设置onbutton为demo_win_onbutton函数 */
  182. rtgui_button_set_onbutton(button, demo_win_onbutton);
  183. demo_view_get_rect(view, &rect);
  184. rect.x1 += 5;
  185. rect.x2 = rect.x1 + 100;
  186. rect.y1 += 5 + 25;
  187. rect.y2 = rect.y1 + 20;
  188. /* 创建按钮用于显示一个自动关闭的窗口 */
  189. button = rtgui_button_create("Auto Win");
  190. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  191. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  192. /* 设置onbutton为demo_autowin_onbutton函数 */
  193. rtgui_button_set_onbutton(button, demo_autowin_onbutton);
  194. demo_view_get_rect(view, &rect);
  195. rect.x1 += 5;
  196. rect.x2 = rect.x1 + 100;
  197. rect.y1 += 5 + 25 + 25;
  198. rect.y2 = rect.y1 + 20;
  199. /* 创建按钮用于触发一个模式窗口 */
  200. button = rtgui_button_create("Modal Win");
  201. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  202. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  203. /* 设置onbutton为demo_modalwin_onbutton函数 */
  204. rtgui_button_set_onbutton(button, demo_modalwin_onbutton);
  205. demo_view_get_rect(view, &rect);
  206. rect.x1 += 5;
  207. rect.x2 = rect.x1 + 100;
  208. rect.y1 += 5 + 25 + 25 + 25;
  209. rect.y2 = rect.y1 + 20;
  210. /* 创建按钮用于触发一个不包含标题的窗口 */
  211. button = rtgui_button_create("NoTitle Win");
  212. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  213. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  214. /* 设置onbutton为demo_ntitlewin_onbutton函数 */
  215. rtgui_button_set_onbutton(button, demo_ntitlewin_onbutton);
  216. return view;
  217. }