demo_gui_main.c 4.5 KB
Newer Older
1 2
/*
 * 这个一个RTGUI的例子,演示了如何创建一个RTGUI程序
I
 
iamyhw@gmail.com 已提交
3 4
 * 在rtgui_win这个分支中,没有toplevel控件,默认panel
 * 作为toplevel级别控件,可以把它看作一个“桌面”
5 6 7 8 9 10 11 12 13 14
 */
#include <rtthread.h>
#include <rtgui/rtgui.h>
#include <panel.h>
#include <rtgui/event.h>
#include <rtgui/widgets/widget.h>
#include <rtgui/widgets/button.h>
#include <rtgui/widgets/view.h>
#include <rtgui/rtgui_theme.h>

15
#include "demo_view.h"
I
 
iamyhw@gmail.com 已提交
16

17 18 19 20 21 22
/* 用于存放演示视图的数组,最多可创建32个演示视图 */
static rtgui_view_t* demo_list[32];
/* 当前演示视图索引 */
static rt_uint16_t demo_current = 0;
/* 总共包括的演示视图数目 */
static rt_uint16_t demo_number = 0;
23

24 25
/* 显示前一个演示视图 */
void demo_gui_prev(PVOID wdt, rtgui_event_t *event)
26
{
27 28 29 30 31 32 33 34
	if (demo_current != 0)
	{
		RTGUI_WIDGET_HIDE(demo_list[demo_current]);
		demo_current --;
		RTGUI_WIDGET_UNHIDE(demo_list[demo_current]);
		rtgui_widget_update(demo_list[demo_current]);
	}
}
35

36 37
/* 显示下一个演示视图 */
void demo_gui_next(PVOID wdt, rtgui_event_t *event)
38
{
39
	if (demo_current + 1< demo_number)
40
	{
41 42 43 44 45
		RTGUI_WIDGET_HIDE(demo_list[demo_current]);
		demo_current ++;
		RTGUI_WIDGET_UNHIDE(demo_list[demo_current]);
		rtgui_widget_update(demo_list[demo_current]);
	}
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
rtgui_view_t* demo_view_create(rtgui_view_t* parent_view, const char* title)
{
	rtgui_view_t* view;

	/* 设置视图的名称 */
	view = rtgui_view_create(parent_view, title, 0,0,
				rtgui_widget_get_width(parent_view),
				rtgui_widget_get_height(parent_view));
	if (view == RT_NULL) return RT_NULL;
	rtgui_widget_set_style(view, RTGUI_BORDER_SIMPLE);
	RTGUI_WIDGET_HIDE(view);

	/* 创建标题用的标签 */
	rtgui_label_create(view, title, 10, 5, 200, 20);
	/* 创建一个水平的staticline线 */
	rtgui_staticline_create(view, 10, 30, 2, rtgui_widget_get_width(view)-20, RTGUI_HORIZONTAL);

	/* 创建成功后,添加到数组中 */
	demo_list[demo_number] = view;
	demo_number ++;

	return view;
}
71 72 73 74 75 76 77 78 79 80 81
static void rtgui_panel_entry(void* parameter)
{
	const struct rtgui_graphic_driver* gd = rtgui_graphic_driver_get_default();
	struct rt_messagequeue* mq;
	rtgui_panel_t *panel;
	rtgui_view_t *view;
	rtgui_button_t *button;
	
	/* 创建GUI应用需要的消息队列 */
	mq = rt_mq_create("Panel", 256, 32, RT_IPC_FLAG_FIFO);
	/* 注册当前线程为GUI线程 */
82
	rtgui_thread_register(rt_thread_self(), mq);
83 84
	panel = rtgui_panel_create(0,0,gd->width,gd->height);

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
	//{{{ TODO: START ADD CODE HERE.

	view = rtgui_view_create(panel, "demo_view", 5, 5, gd->width-10,gd->height-40);

	button = rtgui_button_create(panel, "Prev", 5,gd->height-30,50,25);
	rtgui_button_set_onbutton(button, demo_gui_prev);
	button = rtgui_button_create(panel, "Next", gd->width-55,gd->height-30,50,25);
	rtgui_button_set_onbutton(button, demo_gui_next);


	/* 初始化各个例子的视图 */
//#if RT_VERSION == 4
//	demo_view_benchmark(view);
//#endif

//	demo_view_dc(view);
//#if RT_VERSION == 4
//#ifdef RTGUI_USING_TTF
//	demo_view_ttf(view);
//#endif
//#endif

#ifndef RTGUI_USING_SMALL_SIZE
	demo_gui_dc_buffer(view);
#endif
//	demo_gui_animation(view);
//#ifndef RTGUI_USING_SMALL_SIZE
//	demo_view_buffer_animation(view);
//	// demo_view_instrument_panel(view);
//#endif
	demo_gui_window(view);
	demo_gui_label(view);
	demo_gui_button(view);
	demo_gui_checkbox(view);
	demo_gui_progressbar(view);
	demo_gui_scrollbar(view);
	demo_gui_radiobox(view);
	demo_gui_textbox(view);
	demo_gui_listbox(view);
//////	demo_gui_menu(view); /* debugging */
//////	demo_gui_listctrl(view); /* debugging */
	demo_gui_combobox(view);
	demo_gui_slider(view);
128

129 130 131 132 133 134 135 136 137 138 139 140 141
//////#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
//////	demo_gui_image(view); /* debugging */
//////#endif
//#ifdef RT_USING_MODULE	
//#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
//	demo_gui_module(view);
//#endif
//#endif
//	demo_gui_listview(view);
//	demo_gui_listview_icon(view);
//#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
//	demo_gui_fn(view);
//#endif
142

143
	rtgui_view_show(demo_list[demo_current]);
144

145
	//}}} END ADD CODE.
I
 
iamyhw@gmail.com 已提交
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
	rtgui_panel_show(panel);	

	/* 执行工作台事件循环 */
	rtgui_panel_event_loop(panel);
	
	/* 去注册GUI线程 */
	rtgui_thread_deregister(rt_thread_self());
	rt_mq_delete(mq);
}

void rtgui_panel_init(void)
{
	static rt_bool_t main_inited = RT_FALSE;

	if(main_inited == RT_FALSE) /* 避免重复初始化而做的保护 */
	{
		struct rt_thread* tid;

		tid = rt_thread_create("Panel", rtgui_panel_entry, RT_NULL, 4096, 4, 5);

		if(tid != RT_NULL) rt_thread_startup(tid);

		main_inited = RT_TRUE;
	}
}