use new object type implementation.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1403 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
8457439dc1
commit
bfd56fffea
|
@ -8,13 +8,13 @@ static void rtgui_mywidget_ondraw(struct rtgui_mywidget* me)
|
||||||
struct rtgui_rect rect;
|
struct rtgui_rect rect;
|
||||||
rt_uint16_t x, y;
|
rt_uint16_t x, y;
|
||||||
|
|
||||||
/* 获得目标DC,开始绘图 */
|
/* èŽ·å¾—ç›®æ ‡DC,开始绘å›?*/
|
||||||
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
|
||||||
if (dc == RT_NULL) return;
|
if (dc == RT_NULL) return;
|
||||||
|
|
||||||
/* 获得窗口的尺寸 */
|
/* 获得窗å<EFBFBD>£çš„å°ºå¯?*/
|
||||||
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
||||||
/* 绘制背景色 */
|
/* 绘制背景�*/
|
||||||
RTGUI_DC_BC(dc) = white;
|
RTGUI_DC_BC(dc) = white;
|
||||||
rtgui_dc_fill_rect(dc, &rect);
|
rtgui_dc_fill_rect(dc, &rect);
|
||||||
|
|
||||||
|
@ -22,12 +22,12 @@ static void rtgui_mywidget_ondraw(struct rtgui_mywidget* me)
|
||||||
x = (rect.x2 + rect.x1)/2;
|
x = (rect.x2 + rect.x1)/2;
|
||||||
y = (rect.y2 + rect.y1)/2;
|
y = (rect.y2 + rect.y1)/2;
|
||||||
|
|
||||||
/* 绘制十字架 */
|
/* 绘制å<EFBFBD><EFBFBD>å—æž?*/
|
||||||
RTGUI_DC_FC(dc) = black;
|
RTGUI_DC_FC(dc) = black;
|
||||||
rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
|
rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
|
||||||
rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
|
rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
|
||||||
|
|
||||||
/* 根据状态绘制圆圈 */
|
/* æ ¹æ<EFBFBD>®çŠ¶æ€<EFBFBD>绘制圆åœ?*/
|
||||||
if (me->status == MYWIDGET_STATUS_ON)
|
if (me->status == MYWIDGET_STATUS_ON)
|
||||||
RTGUI_DC_FC(dc) = green;
|
RTGUI_DC_FC(dc) = green;
|
||||||
else
|
else
|
||||||
|
@ -48,20 +48,20 @@ static void rtgui_mywidget_onmouse(struct rtgui_mywidget* me, struct rtgui_event
|
||||||
/* 仅对鼠标抬起动作进行处理 */
|
/* 仅对鼠标抬起动作进行处理 */
|
||||||
if (!(mouse->button & RTGUI_MOUSE_BUTTON_UP)) return;
|
if (!(mouse->button & RTGUI_MOUSE_BUTTON_UP)) return;
|
||||||
|
|
||||||
/* 获得控件的位置 */
|
/* 获得控件的ä½<EFBFBD>ç½?*/
|
||||||
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
||||||
/* get_rect函数获得是控件的相对位置,而鼠标事件给出的坐标是绝对坐标,需要做一个转换 */
|
/* get_rect函数获得是控件的相对ä½<EFBFBD>ç½®ï¼Œè€Œé¼ æ ‡äº‹ä»¶ç»™å‡ºçš„å<EFBFBD><EFBFBD>æ ‡æ˜¯ç»<EFBFBD>对å<EFBFBD><EFBFBD>æ ‡ï¼Œéœ€è¦<EFBFBD>å<EFBFBD>šä¸€ä¸ªè½¬æ<EFBFBD>?*/
|
||||||
rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
|
rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
|
||||||
|
|
||||||
/* 计算中心原点 */
|
/* 计算中心原点 */
|
||||||
x = (rect.x2 + rect.x1)/2;
|
x = (rect.x2 + rect.x1)/2;
|
||||||
y = (rect.y2 + rect.y1)/2;
|
y = (rect.y2 + rect.y1)/2;
|
||||||
|
|
||||||
/* 比较鼠标坐标是否在圈内 */
|
/* æ¯”è¾ƒé¼ æ ‡å<EFBFBD><EFBFBD>æ ‡æ˜¯å<EFBFBD>¦åœ¨åœˆå†?*/
|
||||||
if ((mouse->x < x + 5 && mouse->x > x - 5) &&
|
if ((mouse->x < x + 5 && mouse->x > x - 5) &&
|
||||||
(mouse->y < y + 5 && mouse->y > y - 5))
|
(mouse->y < y + 5 && mouse->y > y - 5))
|
||||||
{
|
{
|
||||||
/* 更改控件状态 */
|
/* 更改控件状�*/
|
||||||
if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
|
if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
|
||||||
else me->status = MYWIDGET_STATUS_ON;
|
else me->status = MYWIDGET_STATUS_ON;
|
||||||
|
|
||||||
|
@ -70,16 +70,16 @@ static void rtgui_mywidget_onmouse(struct rtgui_mywidget* me, struct rtgui_event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mywidget控件的事件处理函数 */
|
/* mywidget控件的事件处ç<EFBFBD>†å‡½æ•?*/
|
||||||
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
|
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
|
||||||
{
|
{
|
||||||
/* 调用事件处理函数时,widget指针指向控件本身,所以先获得相应控件对象的指针 */
|
/* 调用事件处ç<EFBFBD>†å‡½æ•°æ—¶ï¼Œwidget指针指å<EFBFBD>‘控件本身,所以先获得相应控件对象的指é’?*/
|
||||||
struct rtgui_mywidget* me = RTGUI_MYWIDGET(widget);
|
struct rtgui_mywidget* me = RTGUI_MYWIDGET(widget);
|
||||||
|
|
||||||
switch (event->type)
|
switch (event->type)
|
||||||
{
|
{
|
||||||
case RTGUI_EVENT_PAINT:
|
case RTGUI_EVENT_PAINT:
|
||||||
/* 绘制事件,调用绘图函数绘制 */
|
/* 绘制事件,调用绘图函数绘�*/
|
||||||
rtgui_mywidget_ondraw(me);
|
rtgui_mywidget_ondraw(me);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui
|
||||||
rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
|
rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* 其他事件调用父类的事件处理函数 */
|
/* 其他事件调用父类的事件处ç<EFBFBD>†å‡½æ•?*/
|
||||||
default:
|
default:
|
||||||
return rtgui_widget_event_handler(widget, event);
|
return rtgui_widget_event_handler(widget, event);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui
|
||||||
return RT_FALSE;
|
return RT_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 自定义控件的构造函数 */
|
/* è‡ªå®šä¹‰æŽ§ä»¶çš„æž„é€ å‡½æ•?*/
|
||||||
static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
|
static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
|
||||||
{
|
{
|
||||||
/* 默认这个控件接收聚焦 */
|
/* 默认这个控件接收聚焦 */
|
||||||
|
@ -108,29 +108,18 @@ static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
|
||||||
mywidget->status = MYWIDGET_STATUS_OFF;
|
mywidget->status = MYWIDGET_STATUS_OFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获得控件的类型 */
|
DEFINE_CLASS_TYPE(mywidget, "mywidget",
|
||||||
rtgui_type_t *rtgui_mywidget_type_get(void)
|
RTGUI_WIDGET_TYPE,
|
||||||
{
|
_rtgui_mywidget_constructor,
|
||||||
/* 控件的类型是一个静态变量,默认是NULL */
|
RT_NULL,
|
||||||
static rtgui_type_t *mywidget_type = RT_NULL;
|
sizeof(struct rtgui_mywidget));
|
||||||
|
|
||||||
if (!mywidget_type)
|
|
||||||
{
|
|
||||||
/* 当控件类型不存在时,创建它,并指定这种类型数据的大小及指定相应的构造函数和析构函数 */
|
|
||||||
mywidget_type = rtgui_type_create("mywidget", RTGUI_WIDGET_TYPE,
|
|
||||||
sizeof(rtgui_mywidget_t),
|
|
||||||
RTGUI_CONSTRUCTOR(_rtgui_mywidget_constructor), RT_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
return mywidget_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 创建一个自定义控件 */
|
/* 创建一个自定义控件 */
|
||||||
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
|
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
|
||||||
{
|
{
|
||||||
struct rtgui_mywidget* me;
|
struct rtgui_mywidget* me;
|
||||||
|
|
||||||
/* 让rtgui_widget创建出一个指定类型:RTGUI_MYWIDGET_TYPE类型的控件 */
|
/* 让rtgui_widget创建出一个指定类型:RTGUI_MYWIDGET_TYPE类型的控�*/
|
||||||
me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
|
me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
|
||||||
if (me != RT_NULL)
|
if (me != RT_NULL)
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
* |
|
* |
|
||||||
* --o--
|
* --o--
|
||||||
* |
|
* |
|
||||||
* 的形状,中间的o色彩表示了当前的状态,ON状态时是绿色,OFF状态时是红色。
|
* 的形状,ä¸é—´çš„o色彩表示了当å‰<EFBFBD>的状æ€<EFBFBD>,ON状æ€<EFBFBD>时是绿色,OFF状æ€<EFBFBD>时是红色ã€?
|
||||||
* 并且,这个o位置接受鼠标点击,点击下切换下相应的状态。
|
* 并且,这个oä½<EFBFBD>置接å<EFBFBD>—é¼ æ ‡ç‚¹å‡»ï¼Œç‚¹å‡»ä¸‹åˆ‡æ<EFBFBD>¢ä¸‹ç›¸åº”的状æ€<EFBFBD>ã€?
|
||||||
*/
|
*/
|
||||||
#ifndef __MY_WIDGET_H__
|
#ifndef __MY_WIDGET_H__
|
||||||
#define __MY_WIDGET_H__
|
#define __MY_WIDGET_H__
|
||||||
|
@ -14,18 +14,19 @@
|
||||||
#include <rtgui/rtgui.h>
|
#include <rtgui/rtgui.h>
|
||||||
#include <rtgui/widgets/widget.h>
|
#include <rtgui/widgets/widget.h>
|
||||||
|
|
||||||
/* 自定义控件的状态值定义 */
|
/* 自定义控件的状æ€<EFBFBD>值定ä¹?*/
|
||||||
#define MYWIDGET_STATUS_ON 1
|
#define MYWIDGET_STATUS_ON 1
|
||||||
#define MYWIDGET_STATUS_OFF 0
|
#define MYWIDGET_STATUS_OFF 0
|
||||||
|
|
||||||
/** 每个控件会有一个类型,通过如下的宏获得控件相应的类型信息 */
|
DECLARE_CLASS_TYPE(mywidget);
|
||||||
#define RTGUI_MYWIDGET_TYPE (rtgui_mywidget_type_get())
|
/** æ¯<C3A6>个控件会有一个类型,通过如下的å®<C3A5>获得控件相应的类型信æ<C2A1>?*/
|
||||||
|
#define RTGUI_MYWIDGET_TYPE (RTGUI_TYPE(mywidget))
|
||||||
/** 对一个对象实例,可以通过下面的宏实现类型转换 */
|
/** 对一个对象实例,可以通过下面的宏实现类型转换 */
|
||||||
#define RTGUI_MYWIDGET(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_MYWIDGET_TYPE, rtgui_mywidget_t))
|
#define RTGUI_MYWIDGET(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_MYWIDGET_TYPE, rtgui_mywidget_t))
|
||||||
/** 可以通过下面的宏以决定一个具体实例是否是自定义控件类型 */
|
/** å<EFBFBD>¯ä»¥é€šè¿‡ä¸‹é<EFBFBD>¢çš„å®<EFBFBD>以决定一个具体实例是å<EFBFBD>¦æ˜¯è‡ªå®šä¹‰æŽ§ä»¶ç±»åž?*/
|
||||||
#define RTGUI_IS_MYWIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_MYWIDGET_TYPE))
|
#define RTGUI_IS_MYWIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_MYWIDGET_TYPE))
|
||||||
|
|
||||||
/* 个性化控件类定义 */
|
/* 个性化控件类定�*/
|
||||||
struct rtgui_mywidget
|
struct rtgui_mywidget
|
||||||
{
|
{
|
||||||
/* 这个控件是继承自rtgui_widget控件 */
|
/* 这个控件是继承自rtgui_widget控件 */
|
||||||
|
@ -36,16 +37,13 @@ struct rtgui_mywidget
|
||||||
};
|
};
|
||||||
typedef struct rtgui_mywidget rtgui_mywidget_t;
|
typedef struct rtgui_mywidget rtgui_mywidget_t;
|
||||||
|
|
||||||
/* 这个函数用于获得自定义控件的类型 */
|
|
||||||
rtgui_type_t *rtgui_mywidget_type_get(void);
|
|
||||||
|
|
||||||
/* 控件的创建和删除 */
|
/* 控件的创建和删除 */
|
||||||
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r);
|
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r);
|
||||||
void rtgui_mywidget_destroy(struct rtgui_mywidget* me);
|
void rtgui_mywidget_destroy(struct rtgui_mywidget* me);
|
||||||
|
|
||||||
/* 控件的默认事件处理函数。
|
/* 控件的默认事件处ç<EFBFBD>†å‡½æ•°ã€?
|
||||||
* 对一个控件而言,如果派生自它的子控件很可能会调用父控件的事件处理函数,
|
* 对一个控件而言,如果派生自它的子控件很可能会调用父控件的事件处理函数,
|
||||||
* 所以这里采用公开声明的方式。
|
* 所以这里采用公开声明的方å¼<EFBFBD>ã€?
|
||||||
*/
|
*/
|
||||||
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event);
|
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue