mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-06-14 13:13:57 +08:00
db06460208
The full log is at https://github.com/RTGUI/RTGUI/commits/merge_1 and it's difficult to merge the new tree commit by commit. I also converted all the file into unix eol so there are many fake diff. Big changes are noted in rtgui/doc/road_map.txt and rtgui/doc/attention.txt. Keep an eye on them if you want to migrate your old code. Note that the work is still in progress and the bsp is not prepared in trunk so far. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2092 bbd45198-f89e-11dd-88c7-29a3b14d5316
137 lines
3.6 KiB
C
137 lines
3.6 KiB
C
#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_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;
|
|
}
|
|
|
|
/* é¼ æ ‡äº‹ä»¶å¤„ç�†å‡½æ•° */
|
|
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);
|
|
}
|
|
}
|
|
|
|
/* mywidget控件的事件处�函�*/
|
|
rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
|
|
{
|
|
/* 调用事件处�函数时,widget指针指�控件本身,所以先获得相应控件对象的指�*/
|
|
struct rtgui_mywidget* me = 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:
|
|
return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
|
|
}
|
|
|
|
return RT_FALSE;
|
|
}
|
|
|
|
/* è‡ªå®šä¹‰æŽ§ä»¶çš„æž„é€ å‡½æ•?*/
|
|
static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
|
|
{
|
|
/* 默认这个控件接收�焦 */
|
|
RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
|
/* �始化控件并设置事件处�函数 */
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(mywidget), rtgui_mywidget_event_handler);
|
|
|
|
/* �始状�时OFF */
|
|
mywidget->status = MYWIDGET_STATUS_OFF;
|
|
}
|
|
|
|
DEFINE_CLASS_TYPE(mywidget, "mywidget",
|
|
RTGUI_WIDGET_TYPE,
|
|
_rtgui_mywidget_constructor,
|
|
RT_NULL,
|
|
sizeof(struct rtgui_mywidget));
|
|
|
|
/* 创建一个自定义控件 */
|
|
struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
|
|
{
|
|
struct rtgui_mywidget* me;
|
|
|
|
/* 让rtgui_widget创建出一个指定类型:RTGUI_MYWIDGET_TYPE类型的控�*/
|
|
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));
|
|
}
|