2010-01-27 17:42:19 +08:00
|
|
|
|
#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;
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 获得目标DC,开始绘<E5A78B><E7BB98>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
|
|
|
|
|
if (dc == RT_NULL) return;
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 获得窗口的尺<E79A84><E5B0BA>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 绘制背景<E8838C><E699AF>?*/
|
2010-04-12 22:09:56 +08:00
|
|
|
|
RTGUI_DC_BC(dc) = white;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_dc_fill_rect(dc, &rect);
|
|
|
|
|
|
|
|
|
|
/* 计算中心原点 */
|
2010-03-14 14:36:46 +08:00
|
|
|
|
x = (rect.x2 + rect.x1)/2;
|
|
|
|
|
y = (rect.y2 + rect.y1)/2;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 绘制十字<E58D81><E5AD97>?*/
|
2010-05-15 22:38:12 +08:00
|
|
|
|
RTGUI_DC_FC(dc) = black;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
|
|
|
|
|
rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 根据状态绘制圆<E588B6><E59C86>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
if (me->status == MYWIDGET_STATUS_ON)
|
2010-05-15 22:38:12 +08:00
|
|
|
|
RTGUI_DC_FC(dc) = green;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
else
|
2010-05-15 22:38:12 +08:00
|
|
|
|
RTGUI_DC_FC(dc) = red;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 获得控件的位<E79A84><E4BD8D>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* get_rect函数获得是控件的相对位置,而鼠标事件给出的坐标是绝对坐标,需要做一个转<E4B8AA><E8BDAC>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
|
|
|
|
|
|
|
|
|
|
/* 计算中心原点 */
|
2010-03-14 14:36:46 +08:00
|
|
|
|
x = (rect.x2 + rect.x1)/2;
|
|
|
|
|
y = (rect.y2 + rect.y1)/2;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 比较鼠标坐标是否在圈<E59CA8><E59C88>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
if ((mouse->x < x + 5 && mouse->x > x - 5) &&
|
2010-03-14 14:36:46 +08:00
|
|
|
|
(mouse->y < y + 5 && mouse->y > y - 5))
|
2010-01-27 17:42:19 +08:00
|
|
|
|
{
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 更改控件状<E4BBB6><E78AB6>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
|
|
|
|
|
else me->status = MYWIDGET_STATUS_ON;
|
|
|
|
|
|
|
|
|
|
/* 刷新(重新绘制)控件 */
|
|
|
|
|
rtgui_mywidget_ondraw(me);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* mywidget控件的事件处理函<E79086><E587BD>?*/
|
2010-03-08 07:30:15 +08:00
|
|
|
|
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
|
2010-01-27 17:42:19 +08:00
|
|
|
|
{
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 调用事件处理函数时,widget指针指向控件本身,所以先获得相应控件对象的指<E79A84><E68C87>?*/
|
2010-03-08 07:30:15 +08:00
|
|
|
|
struct rtgui_mywidget* me = RTGUI_MYWIDGET(widget);
|
2010-01-27 17:42:19 +08:00
|
|
|
|
|
|
|
|
|
switch (event->type)
|
|
|
|
|
{
|
|
|
|
|
case RTGUI_EVENT_PAINT:
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 绘制事件,调用绘图函数绘<E695B0><E7BB98>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_mywidget_ondraw(me);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RTGUI_EVENT_MOUSE_BUTTON:
|
|
|
|
|
/* 鼠标事件 */
|
|
|
|
|
rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
|
|
|
|
|
break;
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 其他事件调用父类的事件处理函<E79086><E587BD>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
default:
|
2010-03-08 07:30:15 +08:00
|
|
|
|
return rtgui_widget_event_handler(widget, event);
|
2010-01-27 17:42:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 自定义控件的构造函<E980A0><E587BD>?*/
|
2010-01-27 17:42:19 +08:00
|
|
|
|
static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
|
|
|
|
|
{
|
2010-03-14 14:36:46 +08:00
|
|
|
|
/* 默认这个控件接收聚焦 */
|
2010-01-27 17:42:19 +08:00
|
|
|
|
RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
2010-03-08 07:30:15 +08:00
|
|
|
|
/* 初始化控件并设置事件处理函数 */
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_widget_set_event_handler(RTGUI_WIDGET(mywidget), rtgui_mywidget_event_handler);
|
|
|
|
|
|
2010-03-14 14:36:46 +08:00
|
|
|
|
/* 初始状态时OFF */
|
2010-01-27 17:42:19 +08:00
|
|
|
|
mywidget->status = MYWIDGET_STATUS_OFF;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
DEFINE_CLASS_TYPE(mywidget, "mywidget",
|
|
|
|
|
RTGUI_WIDGET_TYPE,
|
|
|
|
|
_rtgui_mywidget_constructor,
|
|
|
|
|
RT_NULL,
|
|
|
|
|
sizeof(struct rtgui_mywidget));
|
2010-01-27 17:42:19 +08:00
|
|
|
|
|
2010-03-08 07:30:15 +08:00
|
|
|
|
/* 创建一个自定义控件 */
|
2010-01-27 17:42:19 +08:00
|
|
|
|
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
|
|
|
|
|
{
|
2010-03-14 14:36:46 +08:00
|
|
|
|
struct rtgui_mywidget* me;
|
2010-01-27 17:42:19 +08:00
|
|
|
|
|
2011-05-13 18:58:37 +08:00
|
|
|
|
/* 让rtgui_widget创建出一个指定类型:RTGUI_MYWIDGET_TYPE类型的控<E79A84><E68EA7>?*/
|
2010-03-14 14:36:46 +08:00
|
|
|
|
me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
|
|
|
|
|
if (me != RT_NULL)
|
|
|
|
|
{
|
2010-01-27 17:42:19 +08:00
|
|
|
|
rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
|
2010-03-14 14:36:46 +08:00
|
|
|
|
}
|
2010-01-27 17:42:19 +08:00
|
|
|
|
|
|
|
|
|
return me;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 07:30:15 +08:00
|
|
|
|
/* 删除一个自定义控件 */
|
2010-01-27 17:42:19 +08:00
|
|
|
|
void rtgui_mywidget_destroy(struct rtgui_mywidget* me)
|
|
|
|
|
{
|
|
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(me));
|
|
|
|
|
}
|