demo_view_window.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #if 0
  37. struct rtgui_rect rect = {50, 50, 200, 200};
  38. msgbox = rtgui_win_create(parent, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
  39. if (msgbox != RT_NULL)
  40. {
  41. struct rtgui_box* box = rtgui_box_create(RTGUI_VERTICAL, RT_NULL);
  42. cnt = 5;
  43. sprintf(label_text, "closed then %d second!", cnt);
  44. label = rtgui_label_create(label_text);
  45. rtgui_win_set_box(msgbox, box);
  46. RTGUI_WIDGET(label)->align = RTGUI_ALIGN_CENTER_HORIZONTAL |
  47. RTGUI_ALIGN_CENTER_VERTICAL;
  48. rtgui_widget_set_miniwidth(RTGUI_WIDGET(label),130);
  49. rtgui_box_append(box, RTGUI_WIDGET(label));
  50. rtgui_box_layout(box);
  51. rtgui_win_show(msgbox, RT_FALSE);
  52. }
  53. timer = rtgui_timer_create(200, RT_TIMER_FLAG_PERIODIC, diag_close, RT_NULL);
  54. rtgui_timer_start(timer);
  55. #endif
  56. }
  57. static rt_uint16_t delta_x = 20;
  58. static rt_uint16_t delta_y = 40;
  59. /* 显示正常窗口 */
  60. void window_demo_normal(rtgui_toplevel_t* parent)
  61. {
  62. rtgui_win_t *win;
  63. rtgui_label_t *label;
  64. rtgui_rect_t rect = {0, 0, 150, 80};
  65. rtgui_rect_moveto(&rect, delta_x, delta_y);
  66. delta_x += 20; delta_y += 20;
  67. /* 创建一个窗口 */
  68. win = rtgui_win_create(parent,
  69. "窗口", &rect, RTGUI_WIN_STYLE_DEFAULT);
  70. rect.x1 += 20; rect.x2 -= 5;
  71. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  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. void window_demo_modal(rtgui_toplevel_t* parent)
  79. {
  80. rtgui_win_t *win;
  81. rtgui_label_t *label;
  82. rtgui_rect_t rect = {0, 0, 150, 80};
  83. rtgui_rect_moveto(&rect, delta_x, delta_y);
  84. delta_x += 20; delta_y += 20;
  85. /* 创建一个窗口 */
  86. win = rtgui_win_create(parent,
  87. "模式窗口", &rect, RTGUI_WIN_STYLE_DEFAULT);
  88. rect.x1 += 20; rect.x2 -= 5;
  89. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  90. label = rtgui_label_create("这是一个模式窗口");
  91. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  92. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  93. /* 模态显示窗口 */
  94. rtgui_win_show(win, RT_TRUE);
  95. }
  96. void window_demo_notitle(rtgui_toplevel_t* parent)
  97. {
  98. rtgui_win_t *win;
  99. rtgui_label_t *label;
  100. rtgui_button_t *button;
  101. rtgui_rect_t rect = {0, 0, 150, 80};
  102. rtgui_rect_moveto(&rect, delta_x, delta_y);
  103. delta_x += 20; delta_y += 20;
  104. /* 创建一个窗口 */
  105. win = rtgui_win_create(parent,
  106. "no title", &rect, RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  107. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win)) = white;
  108. /* 创建一个文本标签 */
  109. rect.x1 += 20; rect.x2 -= 5;
  110. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  111. label = rtgui_label_create("无边框窗口");
  112. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  113. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
  114. /* 创建一个关闭按钮 */
  115. rect.x1 += 20; rect.x2 = rect.x1 + 40;
  116. rect.y1 += 35; rect.y2 = rect.y1 + 20;
  117. button = rtgui_button_create("关闭");
  118. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  119. rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(button));
  120. rtgui_button_set_onbutton(button, window_demo_close);
  121. /* 非模态显示窗口 */
  122. rtgui_win_show(win, RT_FALSE);
  123. }
  124. static void demo_win_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  125. {
  126. window_demo_normal(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  127. }
  128. static void demo_autowin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  129. {
  130. // window_demo_autoclose(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  131. }
  132. static void demo_modalwin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  133. {
  134. window_demo_modal(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  135. }
  136. static void demo_ntitlewin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
  137. {
  138. window_demo_notitle(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget)));
  139. }
  140. rtgui_view_t* demo_view_window(rtgui_workbench_t* workbench)
  141. {
  142. rtgui_rect_t rect;
  143. rtgui_view_t* view;
  144. rtgui_button_t *button;
  145. view = demo_view(workbench, "Window Demo");
  146. demo_view_get_rect(view, &rect);
  147. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  148. rect.y1 += 5; rect.y2 = rect.y1 + 20;
  149. button = rtgui_button_create("Normal Win");
  150. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  151. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  152. rtgui_button_set_onbutton(button, demo_win_onbutton);
  153. demo_view_get_rect(view, &rect);
  154. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  155. rect.y1 += 5 + 25; rect.y2 = rect.y1 + 20;
  156. button = rtgui_button_create("Auto Win");
  157. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  158. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  159. rtgui_button_set_onbutton(button, demo_autowin_onbutton);
  160. demo_view_get_rect(view, &rect);
  161. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  162. rect.y1 += 5 + 25 + 25; rect.y2 = rect.y1 + 20;
  163. button = rtgui_button_create("Modal Win");
  164. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  165. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  166. rtgui_button_set_onbutton(button, demo_modalwin_onbutton);
  167. demo_view_get_rect(view, &rect);
  168. rect.x1 += 5; rect.x2 = rect.x1 + 100;
  169. rect.y1 += 5 + 25 + 25 + 25; rect.y2 = rect.y1 + 20;
  170. button = rtgui_button_create("NoTitle Win");
  171. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  172. rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
  173. rtgui_button_set_onbutton(button, demo_ntitlewin_onbutton);
  174. return view;
  175. }