demo_view_window.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/window.h>
  4. #include <rtgui/widgets/label.h>
  5. #include <rtgui/widgets/button.h>
  6. #include "demo_view.h"
  7. static struct rtgui_timer *timer;
  8. static struct rtgui_label* label;
  9. static struct rtgui_win* msgbox = RT_NULL;
  10. static rt_uint8_t label_text[80];
  11. static int cnt = 5;
  12. void window_demo_close(struct rtgui_widget* widget, rtgui_event_t *even)
  13. {
  14. rtgui_win_t* win;
  15. /* 获得最顶层控件 */
  16. win = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
  17. /* 销毁窗口 */
  18. rtgui_win_destroy(win);
  19. }
  20. /* 关闭对话框时的回调函数 */
  21. void diag_close(struct rtgui_timer* timer, void* parameter)
  22. {
  23. sprintf(label_text, "closed then %d second!", cnt);
  24. rtgui_label_set_text(label, label_text);
  25. rtgui_widget_update(RTGUI_WIDGET(label));
  26. if (cnt == 0)
  27. {
  28. rtgui_win_destroy(msgbox);
  29. rtgui_timer_stop(timer);
  30. rtgui_timer_destory(timer);
  31. }
  32. cnt --;
  33. }
  34. void window_demo_autoclose(rtgui_toplevel_t* parent)
  35. {
  36. struct rtgui_rect rect = {50, 50, 200, 200};
  37. msgbox = rtgui_win_create(parent, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
  38. if (msgbox != RT_NULL)
  39. {
  40. struct rtgui_box* box = rtgui_box_create(RTGUI_VERTICAL, RT_NULL);
  41. cnt = 5;
  42. sprintf(label_text, "closed then %d second!", cnt);
  43. label = rtgui_label_create(label_text);
  44. rtgui_win_set_box(msgbox, box);
  45. RTGUI_WIDGET(label)->align = RTGUI_ALIGN_CENTER_HORIZONTAL |
  46. RTGUI_ALIGN_CENTER_VERTICAL;
  47. rtgui_widget_set_miniwidth(RTGUI_WIDGET(label),130);
  48. rtgui_box_append(box, RTGUI_WIDGET(label));
  49. rtgui_box_layout(box);
  50. rtgui_win_show(msgbox, RT_FALSE);
  51. }
  52. timer = rtgui_timer_create(100, RT_TIMER_FLAG_PERIODIC, diag_close, RT_NULL);
  53. rtgui_timer_start(timer);
  54. }
  55. static rt_uint16_t delta_x = 20;
  56. static rt_uint16_t delta_y = 40;
  57. /* 显示正常窗口 */
  58. void window_demo_normal(rtgui_toplevel_t* parent)
  59. {
  60. rtgui_win_t *win;
  61. rtgui_label_t *label;
  62. rtgui_rect_t rect = {0, 0, 150, 80};
  63. rtgui_rect_moveto(&rect, delta_x, delta_y);
  64. delta_x += 20; delta_y += 20;
  65. /* 创建一个窗口 */
  66. win = rtgui_win_create(parent,
  67. "窗口", &rect, RTGUI_WIN_STYLE_DEFAULT);
  68. rect.x1 += 20; rect.x2 -= 5;
  69. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  70. label = rtgui_label_create("这是一个普通窗口");
  71. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  72. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  73. /* 非模态显示窗口 */
  74. rtgui_win_show(win, RT_FALSE);
  75. }
  76. void window_demo_modal(rtgui_toplevel_t* parent)
  77. {
  78. rtgui_win_t *win;
  79. rtgui_label_t *label;
  80. rtgui_rect_t rect = {0, 0, 150, 80};
  81. rtgui_rect_moveto(&rect, delta_x, delta_y);
  82. delta_x += 20; delta_y += 20;
  83. /* 创建一个窗口 */
  84. win = rtgui_win_create(parent,
  85. "模式窗口", &rect, RTGUI_WIN_STYLE_DEFAULT);
  86. rect.x1 += 20; rect.x2 -= 5;
  87. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  88. label = rtgui_label_create("这是一个模式窗口");
  89. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  90. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  91. /* 模态显示窗口 */
  92. rtgui_win_show(win, RT_TRUE);
  93. }
  94. void window_demo_notitle(rtgui_toplevel_t* parent)
  95. {
  96. rtgui_win_t *win;
  97. rtgui_label_t *label;
  98. rtgui_button_t *button;
  99. rtgui_rect_t widget_rect, rect = {0, 0, 150, 80};
  100. rtgui_rect_moveto(&rect, delta_x, delta_y);
  101. delta_x += 20; delta_y += 20;
  102. /* 创建一个窗口 */
  103. win = rtgui_win_create(parent,
  104. "no title", &rect, RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  105. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win)) = white;
  106. /* 创建一个文本标签 */
  107. label = rtgui_label_create("无边框窗口");
  108. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(label)), "无边框窗口", &widget_rect);
  109. rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
  110. widget_rect.y1 += 20; widget_rect.y2 += 20;
  111. rtgui_widget_set_rect(RTGUI_WIDGET(label), &widget_rect);
  112. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  113. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
  114. /* 创建一个关闭按钮 */
  115. widget_rect.x1 = 0; widget_rect.y1 = 0;
  116. widget_rect.x2 = 40; widget_rect.y2 = 20;
  117. rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
  118. widget_rect.y1 += 40; widget_rect.y2 += 40;
  119. button = rtgui_button_create("关闭");
  120. rtgui_widget_set_rect(RTGUI_WIDGET(button), &widget_rect);
  121. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(button));
  122. rtgui_button_set_onbutton(button, window_demo_close);
  123. /* 非模态显示窗口 */
  124. rtgui_win_show(win, RT_FALSE);
  125. }
  126. static void demo_win_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  127. {
  128. window_demo_normal(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  129. }
  130. static void demo_autowin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  131. {
  132. window_demo_autoclose(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  133. }
  134. static void demo_modalwin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  135. {
  136. window_demo_modal(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  137. }
  138. static void demo_ntitlewin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  139. {
  140. window_demo_notitle(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  141. }
  142. rtgui_view_t* demo_view_window(rtgui_workbench_t* workbench)
  143. {
  144. rtgui_rect_t rect;
  145. rtgui_view_t* view;
  146. rtgui_button_t *button;
  147. view = demo_view(workbench, "Window Demo");
  148. demo_view_get_rect(view, &rect);
  149. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  150. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  151. button = rtgui_button_create("Normal Win");
  152. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  153. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  154. rtgui_button_set_onbutton(button, demo_win_onbutton);
  155. demo_view_get_rect(view, &rect);
  156. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  157. rect.y1 += 5 + 25; rect.y2 = rect.y1 + 20;
  158. button = rtgui_button_create("Auto Win");
  159. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  160. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  161. rtgui_button_set_onbutton(button, demo_autowin_onbutton);
  162. demo_view_get_rect(view, &rect);
  163. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  164. rect.y1 += 5 + 25 + 25; rect.y2 = rect.y1 + 20;
  165. button = rtgui_button_create("Modal Win");
  166. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  167. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  168. rtgui_button_set_onbutton(button, demo_modalwin_onbutton);
  169. demo_view_get_rect(view, &rect);
  170. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  171. rect.y1 += 5 + 25 + 25 + 25; rect.y2 = rect.y1 + 20;
  172. button = rtgui_button_create("NoTitle Win");
  173. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  174. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  175. rtgui_button_set_onbutton(button, demo_ntitlewin_onbutton);
  176. return view;
  177. }