demo_view_window.c 8.1 KB

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