demo_view_window.c 8.9 KB

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