1
0

demo_view_window.c 7.9 KB

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