demo_gui_window.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/*
 * 程序清单:窗口演示
 *
 * 这个例子会先创建出一个演示用的view,当点击上面的按钮时会不同的模式创建窗口
 */

#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"
#include <string.h>

static rtgui_timer_t *timer;
static char label_text[80];
static rt_uint8_t cnt = 5;

/* 获取一个递增的窗口标题 */
static char* get_win_title(void)
{
	static rt_uint8_t win_no = 0;
	static char win_title[16];

	rt_sprintf(win_title, "win %d", ++win_no);
	return win_title;
}

rt_bool_t auto_window_close(PVOID wdt, rtgui_event_t* event)
{
	if(timer != RT_NULL)
	{
		rtgui_timer_stop(timer);
		rtgui_timer_destory(timer);
	}
	return RT_TRUE;
}

/* 关闭对话框时的回调函数 */
void diag_close(struct rtgui_timer* timer, void* parameter)
{
	rtgui_label_t *label;

	label = (rtgui_label_t*)parameter;
	cnt --;
	rt_sprintf(label_text, "closed then %d s", cnt);

	/* 设置标签文本并更新控件 */
	rtgui_label_set_text(label, label_text);
	rtgui_widget_update(RTGUI_WIDGET(label));

	if (cnt == 0)
	{
		rtgui_win_t *win;
		win = rtgui_win_get_win_by_widget(label);
		if(win == RT_NULL) return;
		/* 超时,关闭对话框 */
		rtgui_win_close(win, RT_NULL);

		/* 停止并删除定时器 */
		rtgui_timer_stop(timer);
		rtgui_timer_destory(timer);
	}
}

static rt_uint16_t delta_x = 20;
static rt_uint16_t delta_y = 40;

/* 触发正常窗口显示 */
static void demo_win_onbutton(PVOID wdt, rtgui_event_t* event)
{
	rtgui_win_t *win;
	PVOID parent;
	rtgui_rect_t rect={0, 0, 180, 120};

	parent = rtgui_widget_get_toplevel(wdt);
	rtgui_rect_moveto(&rect, delta_x, delta_y);
	delta_x += 20;
	delta_y += 20;

	/* 创建一个窗口 */
	win = rtgui_win_create(parent, get_win_title(), &rect, RTGUI_WIN_DEFAULT);

	/* 添加一个文本标签 */
	rtgui_label_create(win, "这是一个普通窗口", 10, 30, 150, 20);

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

/* 触发自动窗口显示 */
static void demo_autowin_onbutton(PVOID wdt, rtgui_event_t* event)
{
	PVOID parent;
	rtgui_label_t *label = RT_NULL;
	rtgui_win_t *msgbox;
	struct rtgui_rect rect ={50, 50, 200, 200};

	parent = rtgui_widget_get_toplevel(wdt);
	msgbox = rtgui_win_create(parent, "Information", &rect, RTGUI_WIN_DEFAULT);
	if (msgbox != RT_NULL)
	{
		cnt = 5;
		rt_sprintf(label_text, "closed then %d s", cnt);
		label = rtgui_label_create(msgbox, label_text, 10,30,120,20);
		/* 设置关闭窗口时的动作 */
		rtgui_win_set_onclose(msgbox, auto_window_close);

		rtgui_win_show(msgbox, RT_FALSE);
	}

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

/* 触发模态窗口显示 */
static void demo_modalwin_onbutton(PVOID wdt, rtgui_event_t* event)
{
	rtgui_win_t *win;
	PVOID parent;
	rtgui_rect_t rect = {0, 0, 180, 120};

	parent = rtgui_widget_get_toplevel(wdt);
	rtgui_rect_moveto(&rect, (rtgui_widget_get_width(parent) -180)/2, 
							 (rtgui_widget_get_height(parent)-120)/2);

	/* 创建一个窗口 */
	win = rtgui_win_create(parent,get_win_title(), &rect, RTGUI_WIN_DEFAULT);

	rect.x1 += 20;
	rect.x2 -= 5;
	rect.y1 += 5;
	rect.y2 = rect.y1 + 20;

	rtgui_label_create(win, "这是一个模式窗口", 20, 30, 150,20);

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

/* 触发无标题窗口显示 */
static void demo_ntitlewin_onbutton(PVOID wdt, rtgui_event_t* event)
{
	rtgui_win_t *win;
	rtgui_label_t *label;
	rtgui_button_t *button;
	PVOID parent;
	rtgui_rect_t rect = {0, 0, 180, 120};

	parent = rtgui_widget_get_toplevel(wdt);
	rtgui_rect_moveto(&rect, delta_x, delta_y);
	delta_x += 20;
	delta_y += 20;
	/* 创建一个窗口,风格为无标题及无边框 */
	win = rtgui_win_create(parent,"no title", &rect, RTGUI_WIN_NOBORDER);
	RTGUI_WIDGET_BACKGROUND(win) = white;
	/* 创建一个文本标签 */
	label = rtgui_label_create(win, "无边框窗口", 10, 20, 100, 20);
	RTGUI_WIDGET_BACKGROUND(label) = white;

	button = rtgui_button_create(win,"关闭", 65, 85, 60, 25);
	rtgui_button_set_onbutton(button, rtgui_win_close);

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

rtgui_view_t* demo_gui_window(rtgui_view_t* parent_view)
{
	rtgui_view_t* view;
	rtgui_button_t *button;

	/* 创建一个演示用的视图 */
	view = demo_view_create(parent_view, "Window Demo");


	button = rtgui_button_create(view, "Normal Win", 10, 40, 100, 25);
	rtgui_button_set_onbutton(button, demo_win_onbutton);

	button = rtgui_button_create(view, "Auto Win", 10, 70, 100, 25);
	rtgui_button_set_onbutton(button, demo_autowin_onbutton);

	button = rtgui_button_create(view, "Modal Win", 10, 100, 100, 25);
	rtgui_button_set_onbutton(button, demo_modalwin_onbutton);

	button = rtgui_button_create(view, "NoTitle Win", 10, 130, 100, 25);
	rtgui_button_set_onbutton(button, demo_ntitlewin_onbutton);

	return view;
}