2012-04-18 23:06:12 +08:00
|
|
|
#include <rtgui/dc.h>
|
|
|
|
#include "mywidget.h"
|
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
static void rtgui_mywidget_ondraw(struct rtgui_mywidget *me)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-08-13 16:32:01 +08:00
|
|
|
struct rtgui_dc *dc;
|
|
|
|
struct rtgui_rect rect;
|
|
|
|
rt_uint16_t x, y;
|
|
|
|
|
|
|
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
|
|
|
|
if (dc == RT_NULL) return;
|
|
|
|
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
|
|
|
|
RTGUI_DC_BC(dc) = white;
|
|
|
|
rtgui_dc_fill_rect(dc, &rect);
|
|
|
|
|
|
|
|
x = (rect.x2 + rect.x1) / 2;
|
|
|
|
y = (rect.y2 + rect.y1) / 2;
|
|
|
|
|
|
|
|
RTGUI_DC_FC(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_FC(dc) = green;
|
|
|
|
else
|
|
|
|
RTGUI_DC_FC(dc) = red;
|
|
|
|
rtgui_dc_fill_circle(dc, x, y, 5);
|
|
|
|
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
return;
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
static void rtgui_mywidget_onmouse(struct rtgui_mywidget *me, struct rtgui_event_mouse *mouse)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-08-13 16:32:01 +08:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_object *object, struct rtgui_event *event)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-08-13 16:32:01 +08:00
|
|
|
struct rtgui_mywidget *me = RTGUI_MYWIDGET(object);
|
|
|
|
|
|
|
|
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:
|
|
|
|
return rtgui_widget_event_handler(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
|
|
|
|
{
|
2012-08-13 16:32:01 +08:00
|
|
|
RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(mywidget), rtgui_mywidget_event_handler);
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
mywidget->status = MYWIDGET_STATUS_OFF;
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
DEFINE_CLASS_TYPE(mywidget, "mywidget",
|
|
|
|
RTGUI_WIDGET_TYPE,
|
|
|
|
_rtgui_mywidget_constructor,
|
|
|
|
RT_NULL,
|
|
|
|
sizeof(struct rtgui_mywidget));
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
struct rtgui_mywidget *rtgui_mywidget_create(rtgui_rect_t *r)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-08-13 16:32:01 +08:00
|
|
|
struct rtgui_mywidget *me;
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
me = (struct rtgui_mywidget *) rtgui_widget_create(RTGUI_MYWIDGET_TYPE);
|
|
|
|
if (me != RT_NULL)
|
|
|
|
{
|
|
|
|
rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
|
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
return me;
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-13 16:32:01 +08:00
|
|
|
void rtgui_mywidget_destroy(struct rtgui_mywidget *me)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
2012-08-13 16:32:01 +08:00
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(me));
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|