update GUI examples.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@344 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-01-27 09:42:19 +00:00
parent 646db738d5
commit 49868fc5e4
9 changed files with 380 additions and 36 deletions

28
examples/gui/SConscript Normal file
View File

@ -0,0 +1,28 @@
Import('env')
src_local = Split("""
demo_fnview.c
demo_listview.c
demo_panel_single.c
demo_view.c
demo_view_box.c
demo_view_button.c
demo_view_checkbox.c
demo_view_dc.c
demo_view_image.c
demo_view_label.c
demo_view_mywidget.c
demo_view_progressbar.c
demo_view_radiobox.c
demo_view_slider.c
demo_view_textbox.c
demo_view_window.c
demo_workbench.c
gui_init.c
mywidget.c
""")
# The set of source files associated with this SConscript file.
obj = env.Object(src_local)
Return('obj')

View File

@ -8,10 +8,8 @@ static rtgui_view_t* demo_view_list[32];
static rt_uint32_t demo_view_current = 0;
static rt_uint32_t demo_view_number = 0;
static void demo_view_next(struct rtgui_widget* widget, rtgui_event_t *event)
void demo_view_next(struct rtgui_widget* widget, rtgui_event_t *event)
{
RT_ASSERT(widget != RT_NULL);
if (demo_view_current + 1< demo_view_number)
{
demo_view_current ++;
@ -19,10 +17,8 @@ static void demo_view_next(struct rtgui_widget* widget, rtgui_event_t *event)
}
}
static void demo_view_prev(struct rtgui_widget* widget, rtgui_event_t *event)
void demo_view_prev(struct rtgui_widget* widget, rtgui_event_t *event)
{
RT_ASSERT(widget != RT_NULL);
if (demo_view_current != 0)
{
demo_view_current --;

View File

@ -12,7 +12,7 @@ rt_bool_t dc_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
rt_uint32_t vx[] = {20, 50, 60, 45, 60, 20};
rt_uint32_t vy[] = {150, 50, 90, 60, 45, 50};
/* 因为用的是demo view上面本身有一部分控件所以在绘图时先要让先绘图 */
/* 因为用的是demo view上面本身有一部分控件所以在绘图时先要让demo view先绘图 */
rtgui_view_event_handler(widget, event);
/************************************************************************/
@ -35,6 +35,10 @@ rt_bool_t dc_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
rtgui_dc_set_color(dc, green);
rtgui_dc_fill_circle(dc, rect.x1 + 30, rect.y1 + 10, 10);
/* 画一个圆弧 */
rtgui_dc_set_color(dc, RTGUI_RGB(250, 120, 120));
rtgui_dc_draw_arc(dc, rect.x1 + 120, rect.y1 + 60, 30, 0, 120);
/* ¶à±ßÐÎ */
rtgui_dc_set_color(dc, blue);
rtgui_dc_draw_polygon(dc, vx, vy, 6);
@ -56,6 +60,7 @@ rtgui_view_t *demo_view_dc(rtgui_workbench_t* workbench)
rtgui_view_t *view;
view = demo_view(workbench, "DC Demo");
if (view != RT_NULL)
rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler);
return view;

View File

@ -0,0 +1,98 @@
#include "demo_view.h"
#include <rtgui/widgets/button.h>
#include <rtgui/widgets/filelist_view.h>
static rtgui_image_t* image = RT_NULL;
static rtgui_view_t* _view = RT_NULL;
static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
{
/* create a file list view */
rtgui_filelist_view_t *view;
rtgui_workbench_t *workbench;
rtgui_rect_t rect;
workbench = RTGUI_WORKBENCH(rtgui_widget_get_toplevel(widget));
rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
#ifdef _WIN32
view = rtgui_filelist_view_create(workbench, "d:\\", "*.*", &rect);
#else
view = rtgui_filelist_view_create(workbench, "/", "*.*", &rect);
#endif
if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
{
char path[32], image_type[8];
/* set label */
rtgui_filelist_get_fullpath(view, path, sizeof(path));
if (image != RT_NULL) rtgui_image_destroy(image);
rt_memset(image_type, 0, sizeof(image_type));
/* 获得图像的类型 */
if (rt_strstr(path, ".png") != RT_NULL) strcat(image_type, "png");
if (rt_strstr(path, ".jpg") != RT_NULL) strcat(image_type, "jpeg");
if (rt_strstr(path, ".hdc") != RT_NULL) strcat(image_type, "hdc");
if (image_type[0] != '\0')
image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
}
/* 删除 文件列表 视图 */
rtgui_view_destroy(RTGUI_VIEW(view));
rtgui_view_show(_view, RT_FALSE);
}
static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
{
rt_bool_t result;
/* 用默认的事件处理函数 */
result = rtgui_view_event_handler(widget, event);
if (event->type == RTGUI_EVENT_PAINT)
{
struct rtgui_dc* dc;
rtgui_rect_t rect;
/* 获得控件所属的DC */
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL) /* 如果不能正常获得DC返回如果控件或父控件是隐藏状态DC是获取不成功的 */
return RT_FALSE;
/* 获得demo view允许绘图的区域 */
demo_view_get_rect(RTGUI_VIEW(widget), &rect);
/* 获得图像显示区域 */
rect.x1 += 5; rect.x2 -= 5;
rect.y1 += 30;
if (image != RT_NULL)
rtgui_image_blit(image, dc, &rect);
/* 绘图完成 */
rtgui_dc_end_drawing(dc);
}
return result;
}
rtgui_view_t* demo_view_image(rtgui_workbench_t* workbench)
{
rtgui_rect_t rect;
rtgui_button_t* open_btn;
_view = demo_view(workbench, "图像演示");
if (_view != RT_NULL)
rtgui_widget_set_event_handler(RTGUI_WIDGET(_view), demo_view_event_handler);
demo_view_get_rect(_view, &rect);
rect.x1 += 5; rect.x2 = rect.x1 + 120;
rect.y2 = rect.y1 + 20;
open_btn = rtgui_button_create("打开图像文件");
rtgui_container_add_child(RTGUI_CONTAINER(_view), RTGUI_WIDGET(open_btn));
rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
return _view;
}

View File

@ -0,0 +1,28 @@
#include "demo_view.h"
#include "mywidget.h"
rtgui_view_t *demo_view_mywidget(rtgui_workbench_t* workbench)
{
rtgui_view_t *view;
rtgui_rect_t rect;
rtgui_mywidget_t *mywidget;
/* create a demo view */
view = demo_view(workbench, "MyWidget View");
/* get demo view rect */
demo_view_get_rect(view, &rect);
rect.x1 += 5; rect.x2 = rect.y1 + 80;
rect.y1 += 5; rect.y2 = rect.y1 + 80;
mywidget = rtgui_mywidget_create(&rect);
rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(mywidget));
/* get demo view rect */
demo_view_get_rect(view, &rect);
rect.x1 += 25; rect.x2 = rect.y1 + 40;
rect.y1 += 5 + 100; rect.y2 = rect.y1 + 40;
mywidget = rtgui_mywidget_create(&rect);
rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(mywidget));
return view;
}

View File

@ -4,33 +4,30 @@
#include <rtgui/widgets/view.h>
#include <rtgui/widgets/workbench.h>
static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
static rt_bool_t demo_workbench_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
/* 我们目前只对绘制事件感兴趣 */
if (event->type == RTGUI_EVENT_PAINT)
/* 我们目前只对按键事件感兴趣 */
if (event->type == RTGUI_EVENT_KBD)
{
struct rtgui_dc* dc;
struct rtgui_rect rect;
struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
/* 获得一个设备上下文 */
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL) return RT_FALSE; /* 如果获取失败代表什么?这个控件是隐藏的或... */
rtgui_widget_get_rect(widget, &rect); /* 获得控件的可视区域 */
/* 先对所在可视区域全部填充为背景色 */
rtgui_dc_fill_rect(dc, &rect);
/* 绘制一个hello! */
rtgui_dc_draw_text(dc, "hello world", &rect);
/* 通知RTGUI绘制结束 */
rtgui_dc_end_drawing(dc);
return RT_FALSE;
if (ekbd->type == RTGUI_KEYDOWN)
{
if (ekbd->key == RTGUIK_RIGHT)
{
demo_view_next(RT_NULL, RT_NULL);
return RT_TRUE;
}
else if (ekbd->key == RTGUIK_LEFT)
{
demo_view_prev(RT_NULL, RT_NULL);
return RT_TRUE;
}
}
}
/* 如果不是绘制事件使用view原来的事件处理函数处理 */
return rtgui_view_event_handler(widget, event);
return rtgui_workbench_event_handler(widget, event);
}
static void workbench_entry(void* parameter)
@ -48,13 +45,9 @@ static void workbench_entry(void* parameter)
workbench = rtgui_workbench_create("main", "workbench");
if (workbench == RT_NULL) return;
/* 创建一个工作台上的一个视图 */
view = rtgui_view_create("view");
rtgui_widget_set_event_handler(RTGUI_WIDGET(view), view_event_handler);
/* 在工作台上添加一个视图 */
rtgui_workbench_add_view(workbench, view);
rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), demo_workbench_event_handler);
/* 初始化各个例子的视图 */
demo_view_dc(workbench);
demo_view_window(workbench);
demo_view_label(workbench);
@ -64,6 +57,8 @@ static void workbench_entry(void* parameter)
demo_view_radiobox(workbench);
demo_view_textbox(workbench);
demo_view_slider(workbench);
demo_view_mywidget(workbench);
demo_view_image(workbench);
demo_listview_view(workbench);
demo_fn_view(workbench);

25
examples/gui/gui_init.c Normal file
View File

@ -0,0 +1,25 @@
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_server.h>
extern void rt_hw_lcd_init(void);
extern void rt_hw_key_init(void);
extern void workbench_init(void);
extern void panel_init(void);
/* GUI相关演示入口需放在线程中进行初始化 */
void rtgui_startup()
{
/* GUI系统初始化 */
rtgui_system_server_init();
/* 按键初始化 */
rt_hw_key_init();
/* LCD驱动初始化 */
rt_hw_lcd_init();
/* 各个面板初始化 */
panel_init();
/* 启动workbench */
workbench_init();
}

View File

@ -0,0 +1,139 @@
#include <rtgui/dc.h>
#include "mywidget.h"
/* 控件绘图函数 */
static void rtgui_mywidget_ondraw(struct rtgui_mywidget* me)
{
struct rtgui_dc* dc;
struct rtgui_rect rect;
rt_uint16_t x, y;
/* 获得目标DC开始绘图 */
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
if (dc == RT_NULL) return;
/* 获得窗口的尺寸 */
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
/* 绘制背景色 */
rtgui_dc_set_color(dc, white);
rtgui_dc_fill_rect(dc, &rect);
/* 计算中心原点 */
x = (rect.x2 + rect.x1)/2; y = (rect.y2 + rect.y1)/2;
/* 绘制十字架 */
rtgui_dc_set_color(dc, black);
rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
/* 根据状态绘制圆圈 */
if (me->status == MYWIDGET_STATUS_ON)
rtgui_dc_set_color(dc, green);
else
rtgui_dc_set_color(dc, red);
rtgui_dc_fill_circle(dc, x, y, 5);
/* 结束绘图 */
rtgui_dc_end_drawing(dc);
return;
}
/* 鼠标事件处理函数 */
static void rtgui_mywidget_onmouse(struct rtgui_mywidget* me, struct rtgui_event_mouse* mouse)
{
struct rtgui_rect rect;
rt_uint16_t x, y;
/* 仅对鼠标抬起动作进行处理 */
if (!(mouse->button & RTGUI_MOUSE_BUTTON_UP)) return;
/* 获得控件的位置 */
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
/* get_rect函数获得是控件的相对位置而鼠标事件给出的坐标是绝对坐标需要做一个转换 */
rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
/* 计算中心原点 */
x = (rect.x2 + rect.x1)/2; y = (rect.y2 + rect.y1)/2;
/* 比较鼠标坐标是否在圈内 */
if ((mouse->x < x + 5 && mouse->x > x - 5) &&
(mouse->y < y + 5 && mouse->y > y - 5))
{
/* 更改控件状态 */
if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
else me->status = MYWIDGET_STATUS_ON;
/* 刷新(重新绘制)控件 */
rtgui_mywidget_ondraw(me);
}
}
static rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
struct rtgui_mywidget* me = (struct rtgui_mywidget*)widget;
switch (event->type)
{
case RTGUI_EVENT_PAINT:
/* 绘制事件,调用绘图函数绘制 */
rtgui_mywidget_ondraw(me);
break;
case RTGUI_EVENT_MOUSE_BUTTON:
/* 鼠标事件 */
rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
break;
/* 其他事件调用父类的事件处理函数 */
default:
rtgui_widget_event_handler(widget, event);
}
return RT_FALSE;
}
static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
{
/* 初始化控件并设置事件处理函数 */
RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
rtgui_widget_set_event_handler(RTGUI_WIDGET(mywidget), rtgui_mywidget_event_handler);
mywidget->status = MYWIDGET_STATUS_OFF;
}
static void _rtgui_mywidget_destructor(rtgui_mywidget_t *mywidget)
{
}
rtgui_type_t *rtgui_mywidget_type_get(void)
{
static rtgui_type_t *mywidget_type = RT_NULL;
if (!mywidget_type)
{
mywidget_type = rtgui_type_create("mywidget", RTGUI_WIDGET_TYPE,
sizeof(rtgui_mywidget_t),
RTGUI_CONSTRUCTOR(_rtgui_mywidget_constructor),
RTGUI_DESTRUCTOR(_rtgui_mywidget_destructor));
}
return mywidget_type;
}
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
{
struct rtgui_mywidget* me;
me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
if (me != RT_NULL)
{
rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
}
return me;
}
void rtgui_mywidget_destroy(struct rtgui_mywidget* me)
{
rtgui_widget_destroy(RTGUI_WIDGET(me));
}

View File

@ -0,0 +1,30 @@
#ifndef __MY_WIDGET_H__
#define __MY_WIDGET_H__
#include <rtgui/rtgui.h>
#include <rtgui/widgets/widget.h>
#define MYWIDGET_STATUS_ON 1
#define MYWIDGET_STATUS_OFF 0
/** Gets the type of a button */
#define RTGUI_MYWIDGET_TYPE (rtgui_mywidget_type_get())
/** Casts the object to an rtgui_button */
#define RTGUI_MYWIDGET(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_MYWIDGET_TYPE, rtgui_mywidget_t))
/** Checks if the object is an rtgui_button */
#define RTGUI_IS_MYWIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_MYWIDGET_TYPE))
/* 个性化控件 */
struct rtgui_mywidget
{
struct rtgui_widget parent;
/* 状态ON、OFF */
rt_uint8_t status;
};
typedef struct rtgui_mywidget rtgui_mywidget_t;
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r);
void rtgui_mywidget_destroy(struct rtgui_mywidget* me);
#endif