mywidget.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <rtgui/dc.h>
  2. #include "mywidget.h"
  3. /* 控件绘图函数 */
  4. static void rtgui_mywidget_ondraw(struct rtgui_mywidget* me)
  5. {
  6. struct rtgui_dc* dc;
  7. struct rtgui_rect rect;
  8. rt_uint16_t x, y;
  9. /* 获得目标DC,开始绘图 */
  10. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
  11. if (dc == RT_NULL) return;
  12. /* 获得窗口的尺寸 */
  13. rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
  14. /* 绘制背景色 */
  15. rtgui_dc_set_color(dc, white);
  16. rtgui_dc_fill_rect(dc, &rect);
  17. /* 计算中心原点 */
  18. x = (rect.x2 + rect.x1)/2; y = (rect.y2 + rect.y1)/2;
  19. /* 绘制十字架 */
  20. rtgui_dc_set_color(dc, black);
  21. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
  22. rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
  23. /* 根据状态绘制圆圈 */
  24. if (me->status == MYWIDGET_STATUS_ON)
  25. rtgui_dc_set_color(dc, green);
  26. else
  27. rtgui_dc_set_color(dc, red);
  28. rtgui_dc_fill_circle(dc, x, y, 5);
  29. /* 结束绘图 */
  30. rtgui_dc_end_drawing(dc);
  31. return;
  32. }
  33. /* 鼠标事件处理函数 */
  34. static void rtgui_mywidget_onmouse(struct rtgui_mywidget* me, struct rtgui_event_mouse* mouse)
  35. {
  36. struct rtgui_rect rect;
  37. rt_uint16_t x, y;
  38. /* 仅对鼠标抬起动作进行处理 */
  39. if (!(mouse->button & RTGUI_MOUSE_BUTTON_UP)) return;
  40. /* 获得控件的位置 */
  41. rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
  42. /* get_rect函数获得是控件的相对位置,而鼠标事件给出的坐标是绝对坐标,需要做一个转换 */
  43. rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
  44. /* 计算中心原点 */
  45. x = (rect.x2 + rect.x1)/2; y = (rect.y2 + rect.y1)/2;
  46. /* 比较鼠标坐标是否在圈内 */
  47. if ((mouse->x < x + 5 && mouse->x > x - 5) &&
  48. (mouse->y < y + 5 && mouse->y > y - 5))
  49. {
  50. /* 更改控件状态 */
  51. if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
  52. else me->status = MYWIDGET_STATUS_ON;
  53. /* 刷新(重新绘制)控件 */
  54. rtgui_mywidget_ondraw(me);
  55. }
  56. }
  57. /* mywidget控件的事件处理函数 */
  58. rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  59. {
  60. /* 调用事件处理函数时,widget指针指向控件本身,所以先获得相应控件对象的指针 */
  61. struct rtgui_mywidget* me = RTGUI_MYWIDGET(widget);
  62. switch (event->type)
  63. {
  64. case RTGUI_EVENT_PAINT:
  65. /* 绘制事件,调用绘图函数绘制 */
  66. rtgui_mywidget_ondraw(me);
  67. break;
  68. case RTGUI_EVENT_MOUSE_BUTTON:
  69. /* 鼠标事件 */
  70. rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
  71. break;
  72. /* 其他事件调用父类的事件处理函数 */
  73. default:
  74. return rtgui_widget_event_handler(widget, event);
  75. }
  76. return RT_FALSE;
  77. }
  78. /* 自定义控件的构造函数 */
  79. static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
  80. {
  81. /* 默认这个控件接收聚焦 */
  82. RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  83. /* 初始化控件并设置事件处理函数 */
  84. rtgui_widget_set_event_handler(RTGUI_WIDGET(mywidget), rtgui_mywidget_event_handler);
  85. /* 初始状态时OFF */
  86. mywidget->status = MYWIDGET_STATUS_OFF;
  87. }
  88. /* 获得控件的类型 */
  89. rtgui_type_t *rtgui_mywidget_type_get(void)
  90. {
  91. /* 控件的类型是一个静态变量,默认是NULL */
  92. static rtgui_type_t *mywidget_type = RT_NULL;
  93. if (!mywidget_type)
  94. {
  95. /* 当控件类型不存在时,创建它,并指定这种类型数据的大小及指定相应的构造函数和析构函数 */
  96. mywidget_type = rtgui_type_create("mywidget", RTGUI_WIDGET_TYPE,
  97. sizeof(rtgui_mywidget_t),
  98. RTGUI_CONSTRUCTOR(_rtgui_mywidget_constructor), RT_NULL);
  99. }
  100. return mywidget_type;
  101. }
  102. /* 创建一个自定义控件 */
  103. struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
  104. {
  105. struct rtgui_mywidget* me;
  106. /* 让rtgui_widget创建出一个指定类型:RTGUI_MYWIDGET_TYPE类型的控件 */
  107. me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
  108. if (me != RT_NULL)
  109. {
  110. rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
  111. }
  112. return me;
  113. }
  114. /* 删除一个自定义控件 */
  115. void rtgui_mywidget_destroy(struct rtgui_mywidget* me)
  116. {
  117. rtgui_widget_destroy(RTGUI_WIDGET(me));
  118. }