demo_view_window.c 7.4 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/window.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/button.h>
#include "demo_view.h"

static struct rtgui_timer *timer;
static struct rtgui_label* label;
B
bernard.xiong 已提交
10
static struct rtgui_win* msgbox = RT_NULL;
B
bernard.xiong 已提交
11
static rt_uint8_t label_text[80];
B
bernard.xiong 已提交
12 13
static rt_uint8_t cnt = 5;

B
bernard.xiong 已提交
14
/* 获取一个递增的窗口标题 */
B
bernard.xiong 已提交
15 16 17 18 19 20 21 22
static char* get_win_title()
{
	static rt_uint8_t win_no = 0;
	static char win_title[16];

	rt_sprintf(win_title, "窗口 %d", ++win_no);
	return win_title;
}
B
bernard.xiong 已提交
23

B
bernard.xiong 已提交
24
/* 窗口关闭时的事件处理 */
B
bernard.xiong 已提交
25 26 27 28 29 30 31 32 33 34 35 36
void window_demo_close(struct rtgui_widget* widget, rtgui_event_t *even)
{
	rtgui_win_t* win;

	/* 获得最顶层控件 */
	win = RTGUI_WIN(rtgui_widget_get_toplevel(widget));

	/* 销毁窗口 */
	rtgui_win_destroy(win);
}

/* 关闭对话框时的回调函数 */
B
bernard.xiong 已提交
37 38
void diag_close(struct rtgui_timer* timer, void* parameter)
{
B
bernard.xiong 已提交
39
	cnt --;
B
bernard.xiong 已提交
40 41
	sprintf(label_text, "closed then %d second!", cnt);

B
bernard.xiong 已提交
42
	/* 设置标签文本并更新控件 */
B
bernard.xiong 已提交
43 44
	rtgui_label_set_text(label, label_text);
	rtgui_widget_update(RTGUI_WIDGET(label));
B
bernard.xiong 已提交
45

B
bernard.xiong 已提交
46 47
	if (cnt == 0)
	{
B
bernard.xiong 已提交
48
		/* 超时,关闭对话框 */
B
bernard.xiong 已提交
49
		rtgui_win_destroy(msgbox);
B
bernard.xiong 已提交
50 51

		/* 停止并删除定时器 */
B
bernard.xiong 已提交
52 53 54 55 56
		rtgui_timer_stop(timer);
		rtgui_timer_destory(timer);
	}
}

B
bernard.xiong 已提交
57 58 59
static rt_uint16_t delta_x = 20;
static rt_uint16_t delta_y = 40;

B
bernard.xiong 已提交
60 61
/* 触发正常窗口显示 */
static void demo_win_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
B
bernard.xiong 已提交
62
{
B
bernard.xiong 已提交
63 64
	rtgui_win_t *win;
	rtgui_label_t *label;
B
bernard.xiong 已提交
65
	rtgui_toplevel_t *parent;
B
bernard.xiong 已提交
66 67
	rtgui_rect_t rect = {0, 0, 150, 80};

B
bernard.xiong 已提交
68
	parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
B
bernard.xiong 已提交
69
	rtgui_rect_moveto(&rect, delta_x, delta_y);
B
bernard.xiong 已提交
70 71
	delta_x += 20;
	delta_y += 20;
B
bernard.xiong 已提交
72 73 74

	/* 创建一个窗口 */
	win = rtgui_win_create(parent,
B
bernard.xiong 已提交
75
		get_win_title(), &rect, RTGUI_WIN_STYLE_DEFAULT);
B
bernard.xiong 已提交
76

B
bernard.xiong 已提交
77 78 79 80
	rect.x1 += 20;
	rect.x2 -= 5;
	rect.y1 += 5;
	rect.y2 = rect.y1 + 20;
B
bernard.xiong 已提交
81 82 83 84 85 86 87 88 89

	label = rtgui_label_create("这是一个普通窗口");
	rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
	rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));

	/* 非模态显示窗口 */
	rtgui_win_show(win, RT_FALSE);
}

B
bernard.xiong 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/* 触发自动窗口显示 */
static void demo_autowin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
{
	rtgui_toplevel_t *parent;
	struct rtgui_rect rect =
		{
			50, 50, 200, 200
		};

	parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
	msgbox = rtgui_win_create(parent, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
	if (msgbox != RT_NULL)
	{
		cnt = 5;
		sprintf(label_text, "closed then %d second!", cnt);
		label = rtgui_label_create(label_text);
		rect.x1 += 5;
		rect.x2 -= 5;
		rect.y1 += 5;
		rect.y2 = rect.y1 + 20;
		rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
		rtgui_container_add_child(RTGUI_CONTAINER(msgbox), RTGUI_WIDGET(label));

		rtgui_win_show(msgbox, RT_FALSE);
	}

	/* 创建一个定时器 */
	timer = rtgui_timer_create(100, RT_TIMER_FLAG_PERIODIC, diag_close, RT_NULL);
	rtgui_timer_start(timer);
}

/* 触发模态窗口显示 */
static void demo_modalwin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
B
bernard.xiong 已提交
123 124 125
{
	rtgui_win_t *win;
	rtgui_label_t *label;
B
bernard.xiong 已提交
126
	rtgui_toplevel_t *parent;
B
bernard.xiong 已提交
127 128
	rtgui_rect_t rect = {0, 0, 150, 80};

B
bernard.xiong 已提交
129
	parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
B
bernard.xiong 已提交
130
	rtgui_rect_moveto(&rect, delta_x, delta_y);
B
bernard.xiong 已提交
131 132
	delta_x += 20;
	delta_y += 20;
B
bernard.xiong 已提交
133 134 135

	/* 创建一个窗口 */
	win = rtgui_win_create(parent,
B
bernard.xiong 已提交
136
		get_win_title(), &rect, RTGUI_WIN_STYLE_DEFAULT);
B
bernard.xiong 已提交
137

B
bernard.xiong 已提交
138 139 140 141
	rect.x1 += 20;
	rect.x2 -= 5;
	rect.y1 += 5;
	rect.y2 = rect.y1 + 20;
B
bernard.xiong 已提交
142 143 144 145 146 147 148

	label = rtgui_label_create("这是一个模式窗口");
	rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
	rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));

	/* 模态显示窗口 */
	rtgui_win_show(win, RT_TRUE);
B
bernard.xiong 已提交
149 150
	/* 采用模态显示窗口,关闭时不会自行删除窗口,需要主动删除窗口 */
	rtgui_win_destroy(win);
B
bernard.xiong 已提交
151 152
}

B
bernard.xiong 已提交
153 154
/* 触发无标题窗口显示 */
static void demo_ntitlewin_onbutton(struct rtgui_widget* widget, rtgui_event_t* event)
B
bernard.xiong 已提交
155 156 157 158
{
	rtgui_win_t *win;
	rtgui_label_t *label;
	rtgui_button_t *button;
B
bernard.xiong 已提交
159
	rtgui_toplevel_t *parent;
B
bernard.xiong 已提交
160
	rtgui_rect_t widget_rect, rect = {0, 0, 150, 80};
B
bernard.xiong 已提交
161

B
bernard.xiong 已提交
162
	parent = RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(widget));
B
bernard.xiong 已提交
163
	rtgui_rect_moveto(&rect, delta_x, delta_y);
B
bernard.xiong 已提交
164 165
	delta_x += 20;
	delta_y += 20;
B
bernard.xiong 已提交
166

B
bernard.xiong 已提交
167
	/* 创建一个窗口,风格为无标题及无边框 */
B
bernard.xiong 已提交
168 169 170 171 172 173
	win = rtgui_win_create(parent,
		"no title", &rect, RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
	RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win)) = white;

	/* 创建一个文本标签 */
	label = rtgui_label_create("无边框窗口");
B
bernard.xiong 已提交
174 175
	rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(label)), "无边框窗口", &widget_rect);
	rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
B
bernard.xiong 已提交
176 177
	widget_rect.y1 += 20;
	widget_rect.y2 += 20;
B
bernard.xiong 已提交
178
	rtgui_widget_set_rect(RTGUI_WIDGET(label), &widget_rect);
B
bernard.xiong 已提交
179
	rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
B
bernard.xiong 已提交
180
	RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white;
B
bernard.xiong 已提交
181 182

	/* 创建一个关闭按钮 */
B
bernard.xiong 已提交
183 184 185 186
	widget_rect.x1 = 0;
	widget_rect.y1 = 0;
	widget_rect.x2 = 40;
	widget_rect.y2 = 20;
B
bernard.xiong 已提交
187
	rtgui_rect_moveto_align(&rect, &widget_rect, RTGUI_ALIGN_CENTER_HORIZONTAL);
B
bernard.xiong 已提交
188 189
	widget_rect.y1 += 40;
	widget_rect.y2 += 40;
B
bernard.xiong 已提交
190
	button = rtgui_button_create("关闭");
B
bernard.xiong 已提交
191
	rtgui_widget_set_rect(RTGUI_WIDGET(button), &widget_rect);
B
bernard.xiong 已提交
192 193 194 195 196
	rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(button));
	rtgui_button_set_onbutton(button, window_demo_close);

	/* 非模态显示窗口 */
	rtgui_win_show(win, RT_FALSE);
B
bernard.xiong 已提交
197 198 199 200
}

rtgui_view_t* demo_view_window(rtgui_workbench_t* workbench)
{
B
bernard.xiong 已提交
201 202 203
	rtgui_rect_t  rect;
	rtgui_view_t* view;
	rtgui_button_t *button;
B
bernard.xiong 已提交
204

B
bernard.xiong 已提交
205 206
	/* 创建一个演示用的视图 */
	view = demo_view(workbench, "Window Demo");
B
bernard.xiong 已提交
207 208

	demo_view_get_rect(view, &rect);
B
bernard.xiong 已提交
209 210 211 212 213
	rect.x1 += 5;
	rect.x2 = rect.x1 + 100;
	rect.y1 += 5;
	rect.y2 = rect.y1 + 20;
	/* 创建按钮用于显示正常窗口 */
B
bernard.xiong 已提交
214 215 216
	button = rtgui_button_create("Normal Win");
	rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
	rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
B
bernard.xiong 已提交
217 218
	/* 设置onbutton为demo_win_onbutton函数 */
	rtgui_button_set_onbutton(button, demo_win_onbutton);
B
bernard.xiong 已提交
219 220

	demo_view_get_rect(view, &rect);
B
bernard.xiong 已提交
221 222 223 224 225
	rect.x1 += 5;
	rect.x2 = rect.x1 + 100;
	rect.y1 += 5 + 25;
	rect.y2 = rect.y1 + 20;
	/* 创建按钮用于显示一个自动关闭的窗口 */
B
bernard.xiong 已提交
226 227 228
	button = rtgui_button_create("Auto Win");
	rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
	rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
B
bernard.xiong 已提交
229 230
	/* 设置onbutton为demo_autowin_onbutton函数 */
	rtgui_button_set_onbutton(button, demo_autowin_onbutton);
B
bernard.xiong 已提交
231 232

	demo_view_get_rect(view, &rect);
B
bernard.xiong 已提交
233 234 235 236 237
	rect.x1 += 5;
	rect.x2 = rect.x1 + 100;
	rect.y1 += 5 + 25 + 25;
	rect.y2 = rect.y1 + 20;
	/* 创建按钮用于触发一个模式窗口 */
B
bernard.xiong 已提交
238 239 240
	button = rtgui_button_create("Modal Win");
	rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
	rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
B
bernard.xiong 已提交
241 242
	/* 设置onbutton为demo_modalwin_onbutton函数 */
	rtgui_button_set_onbutton(button, demo_modalwin_onbutton);
B
bernard.xiong 已提交
243 244

	demo_view_get_rect(view, &rect);
B
bernard.xiong 已提交
245 246 247 248 249
	rect.x1 += 5;
	rect.x2 = rect.x1 + 100;
	rect.y1 += 5 + 25 + 25 + 25;
	rect.y2 = rect.y1 + 20;
	/* 创建按钮用于触发一个不包含标题的窗口 */
B
bernard.xiong 已提交
250 251 252
	button = rtgui_button_create("NoTitle Win");
	rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
	rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(button));
B
bernard.xiong 已提交
253 254
	/* 设置onbutton为demo_ntitlewin_onbutton函数 */
	rtgui_button_set_onbutton(button, demo_ntitlewin_onbutton);
B
bernard.xiong 已提交
255

B
bernard.xiong 已提交
256
	return view;
B
bernard.xiong 已提交
257
}