From 49868fc5e452f5cc41bfe1969e1cd3f41f91a7c5 Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Wed, 27 Jan 2010 09:42:19 +0000 Subject: [PATCH] update GUI examples. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@344 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- examples/gui/SConscript | 28 ++++++ examples/gui/demo_view.c | 8 +- examples/gui/demo_view_dc.c | 11 ++- examples/gui/demo_view_image.c | 98 +++++++++++++++++++++ examples/gui/demo_view_mywidget.c | 28 ++++++ examples/gui/demo_workbench.c | 49 +++++------ examples/gui/gui_init.c | 25 ++++++ examples/gui/mywidget.c | 139 ++++++++++++++++++++++++++++++ examples/gui/mywidget.h | 30 +++++++ 9 files changed, 380 insertions(+), 36 deletions(-) create mode 100644 examples/gui/SConscript create mode 100644 examples/gui/gui_init.c diff --git a/examples/gui/SConscript b/examples/gui/SConscript new file mode 100644 index 0000000000..3d1ff19426 --- /dev/null +++ b/examples/gui/SConscript @@ -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') diff --git a/examples/gui/demo_view.c b/examples/gui/demo_view.c index 7fe9e0df6f..7842324422 100644 --- a/examples/gui/demo_view.c +++ b/examples/gui/demo_view.c @@ -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 --; diff --git a/examples/gui/demo_view_dc.c b/examples/gui/demo_view_dc.c index 78b217f285..be0abdd1da 100644 --- a/examples/gui/demo_view_dc.c +++ b/examples/gui/demo_view_dc.c @@ -12,11 +12,11 @@ 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); /************************************************************************/ - /* 下面的是DC的处理 */ + /* 下面的是DC的处理 */ /************************************************************************/ /* 获得控件所属的DC */ @@ -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,7 +60,8 @@ rtgui_view_t *demo_view_dc(rtgui_workbench_t* workbench) rtgui_view_t *view; view = demo_view(workbench, "DC Demo"); - rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler); + if (view != RT_NULL) + rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler); return view; } diff --git a/examples/gui/demo_view_image.c b/examples/gui/demo_view_image.c index e69de29bb2..925e6b93a6 100644 --- a/examples/gui/demo_view_image.c +++ b/examples/gui/demo_view_image.c @@ -0,0 +1,98 @@ +#include "demo_view.h" +#include +#include + +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; +} diff --git a/examples/gui/demo_view_mywidget.c b/examples/gui/demo_view_mywidget.c index e69de29bb2..9adbfd471e 100644 --- a/examples/gui/demo_view_mywidget.c +++ b/examples/gui/demo_view_mywidget.c @@ -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; +} diff --git a/examples/gui/demo_workbench.c b/examples/gui/demo_workbench.c index 053c3d24a0..1afba3d7fb 100644 --- a/examples/gui/demo_workbench.c +++ b/examples/gui/demo_workbench.c @@ -4,33 +4,30 @@ #include #include -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; /* 濡傛灉鑾峰彇澶辫触浠h〃浠涔堬紵杩欎釜鎺т欢鏄殣钘忕殑鎴... */ - rtgui_widget_get_rect(widget, &rect); /* 鑾峰緱鎺т欢鐨勫彲瑙嗗尯鍩 */ - - /* 鍏堝鎵鍦ㄥ彲瑙嗗尯鍩熷叏閮ㄥ~鍏呬负鑳屾櫙鑹 */ - rtgui_dc_fill_rect(dc, &rect); - - /* 缁樺埗涓涓猦ello! */ - 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; + } + } } /* 濡傛灉涓嶆槸缁樺埗浜嬩欢锛屼娇鐢╲iew鍘熸潵鐨勪簨浠跺鐞嗗嚱鏁板鐞 */ - 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); diff --git a/examples/gui/gui_init.c b/examples/gui/gui_init.c new file mode 100644 index 0000000000..3761e7a8be --- /dev/null +++ b/examples/gui/gui_init.c @@ -0,0 +1,25 @@ +#include +#include + +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(); +} diff --git a/examples/gui/mywidget.c b/examples/gui/mywidget.c index e69de29bb2..04b7f296fc 100644 --- a/examples/gui/mywidget.c +++ b/examples/gui/mywidget.c @@ -0,0 +1,139 @@ +#include +#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)); +} diff --git a/examples/gui/mywidget.h b/examples/gui/mywidget.h index e69de29bb2..1e25e5ad4e 100644 --- a/examples/gui/mywidget.h +++ b/examples/gui/mywidget.h @@ -0,0 +1,30 @@ +#ifndef __MY_WIDGET_H__ +#define __MY_WIDGET_H__ + +#include +#include + +#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銆丱FF */ + 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