demo_view_window.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. static rt_uint16_t delta_x = 20;
  53. static rt_uint16_t delta_y = 40;
  54. /* 触发正常窗口显示 */
  55. static void demo_win_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  56. {
  57. rtgui_win_t *win;
  58. rtgui_label_t *label;
  59. rtgui_toplevel_t *parent;
  60. rtgui_rect_t rect = {0, 0, 150, 80};
  61. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  62. rtgui_rect_moveto(&rect, delta_x, delta_y);
  63. delta_x += 20;
  64. delta_y += 20;
  65. /* 创建一个窗口 */
  66. win = rtgui_win_create(parent,
  67. get_win_title(), &rect, RTGUI_WIN_STYLE_DEFAULT);
  68. rect.x1 += 20;
  69. rect.x2 -= 5;
  70. rect.y1 += 5;
  71. rect.y2 = rect.y1 + 20;
  72. /* 添加一个文本标签 */
  73. label = rtgui_label_create("这是一个普通窗口");
  74. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  75. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  76. /* 非模态显示窗口 */
  77. rtgui_win_show(win, RT_FALSE);
  78. }
  79. /* 触发自动窗口显示 */
  80. static void demo_autowin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  81. {
  82. rtgui_toplevel_t *parent;
  83. struct rtgui_rect rect ={50, 50, 200, 200};
  84. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  85. msgbox = rtgui_win_create(parent, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
  86. if (msgbox != RT_NULL)
  87. {
  88. cnt = 5;
  89. rt_sprintf(label_text, "closed then %d second!", cnt);
  90. label = rtgui_label_create(label_text);
  91. rect.x1 += 5;
  92. rect.x2 -= 5;
  93. rect.y1 += 5;
  94. rect.y2 = rect.y1 + 20;
  95. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  96. rtgui_container_add_child(RTGUI_CONTAINER(msgbox), RTGUI_WIDGET(label));
  97. rtgui_win_show(msgbox, RT_FALSE);
  98. }
  99. /* 创建一个定时器 */
  100. timer = rtgui_timer_create(100, RT_TIMER_FLAG_PERIODIC, diag_close, RT_NULL);
  101. rtgui_timer_start(timer);
  102. }
  103. /* 触发模态窗口显示 */
  104. static void demo_modalwin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  105. {
  106. rtgui_win_t *win;
  107. rtgui_label_t *label;
  108. rtgui_toplevel_t *parent;
  109. rtgui_rect_t rect = {0, 0, 150, 80};
  110. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  111. rtgui_rect_moveto(&rect, delta_x, delta_y);
  112. delta_x += 20;
  113. delta_y += 20;
  114. /* 创建一个窗口 */
  115. win = rtgui_win_create(parent,
  116. get_win_title(), &rect, RTGUI_WIN_STYLE_DEFAULT);
  117. rect.x1 += 20;
  118. rect.x2 -= 5;
  119. rect.y1 += 5;
  120. rect.y2 = rect.y1 + 20;
  121. label = rtgui_label_create("这是一个模式窗口");
  122. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  123. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  124. /* 模态显示窗口 */
  125. rtgui_win_show(win, RT_TRUE);
  126. /* 采用模态显示窗口,关闭时不会自行删除窗口,需要主动删除窗口 */
  127. rtgui_win_destroy(win);
  128. }
  129. /* 触发无标题窗口显示 */
  130. static void demo_ntitlewin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  131. {
  132. rtgui_win_t *win;
  133. rtgui_label_t *label;
  134. rtgui_button_t *button;
  135. rtgui_toplevel_t *parent;
  136. rtgui_rect_t widget_rect, rect = {0, 0, 150, 80};
  137. parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
  138. rtgui_rect_moveto(&rect, delta_x, delta_y);
  139. delta_x += 20;
  140. delta_y += 20;
  141. /* 创建一个窗口,风格为无标题及无边框 */
  142. win = rtgui_win_create(parent,
  143. "no title", &rect, RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  144. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win)) = white;
  145. /* 创建一个文本标签 */
  146. label = rtgui_label_create("无边框窗口");
  147. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(label)), "无边框窗口", &widget_rect);
  148. rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
  149. widget_rect.y1 += 20;
  150. widget_rect.y2 += 20;
  151. rtgui_widget_set_rect(RTGUI_WIDGET(label), &widget_rect);
  152. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  153. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
  154. /* 创建一个关闭按钮 */
  155. widget_rect.x1 = 0;
  156. widget_rect.y1 = 0;
  157. widget_rect.x2 = 40;
  158. widget_rect.y2 = 20;
  159. rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
  160. widget_rect.y1 += 40;
  161. widget_rect.y2 += 40;
  162. button = rtgui_button_create("关闭");
  163. rtgui_widget_set_rect(RTGUI_WIDGET(button), &widget_rect);
  164. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(button));
  165. rtgui_button_set_onbutton(button, window_demo_close);
  166. /* 非模态显示窗口 */
  167. rtgui_win_show(win, RT_FALSE);
  168. }
  169. rtgui_view_t* demo_view_window(rtgui_workbench_t* workbench)
  170. {
  171. rtgui_rect_t rect;
  172. rtgui_view_t* view;
  173. rtgui_button_t *button;
  174. /* 创建一个演示用的视图 */
  175. view = demo_view(workbench, "Window Demo");
  176. demo_view_get_rect(view, &rect);
  177. rect.x1 += 5;
  178. rect.x2 = rect.x1 + 100;
  179. rect.y1 += 5;
  180. rect.y2 = rect.y1 + 20;
  181. /* 创建按钮用于显示正常窗口 */
  182. button = rtgui_button_create("Normal Win");
  183. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  184. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  185. /* 设置onbutton为demo_win_onbutton函数 */
  186. rtgui_button_set_onbutton(button, demo_win_onbutton);
  187. demo_view_get_rect(view, &rect);
  188. rect.x1 += 5;
  189. rect.x2 = rect.x1 + 100;
  190. rect.y1 += 5 + 25;
  191. rect.y2 = rect.y1 + 20;
  192. /* 创建按钮用于显示一个自动关闭的窗口 */
  193. button = rtgui_button_create("Auto Win");
  194. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  195. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  196. /* 设置onbutton为demo_autowin_onbutton函数 */
  197. rtgui_button_set_onbutton(button, demo_autowin_onbutton);
  198. demo_view_get_rect(view, &rect);
  199. rect.x1 += 5;
  200. rect.x2 = rect.x1 + 100;
  201. rect.y1 += 5 + 25 + 25;
  202. rect.y2 = rect.y1 + 20;
  203. /* 创建按钮用于触发一个模式窗口 */
  204. button = rtgui_button_create("Modal Win");
  205. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  206. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  207. /* 设置onbutton为demo_modalwin_onbutton函数 */
  208. rtgui_button_set_onbutton(button, demo_modalwin_onbutton);
  209. demo_view_get_rect(view, &rect);
  210. rect.x1 += 5;
  211. rect.x2 = rect.x1 + 100;
  212. rect.y1 += 5 + 25 + 25 + 25;
  213. rect.y2 = rect.y1 + 20;
  214. /* 创建按钮用于触发一个不包含标题的窗口 */
  215. button = rtgui_button_create("NoTitle Win");
  216. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  217. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  218. /* 设置onbutton为demo_ntitlewin_onbutton函数 */
  219. rtgui_button_set_onbutton(button, demo_ntitlewin_onbutton);
  220. return view;
  221. }