demo_view_window.c 7.5 KB

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