mywidget.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. static rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  58. {
  59. struct rtgui_mywidget* me = (struct rtgui_mywidget*)widget;
  60. switch (event->type)
  61. {
  62. case RTGUI_EVENT_PAINT:
  63. /* 绘制事件,调用绘图函数绘制 */
  64. rtgui_mywidget_ondraw(me);
  65. break;
  66. case RTGUI_EVENT_MOUSE_BUTTON:
  67. /* 鼠标事件 */
  68. rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
  69. break;
  70. /* 其他事件调用父类的事件处理函数 */
  71. default:
  72. rtgui_widget_event_handler(widget, event);
  73. }
  74. return RT_FALSE;
  75. }
  76. static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
  77. {
  78. /* 初始化控件并设置事件处理函数 */
  79. RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  80. rtgui_widget_set_event_handler(RTGUI_WIDGET(mywidget), rtgui_mywidget_event_handler);
  81. mywidget->status = MYWIDGET_STATUS_OFF;
  82. }
  83. static void _rtgui_mywidget_destructor(rtgui_mywidget_t *mywidget)
  84. {
  85. }
  86. rtgui_type_t *rtgui_mywidget_type_get(void)
  87. {
  88. static rtgui_type_t *mywidget_type = RT_NULL;
  89. if (!mywidget_type)
  90. {
  91. mywidget_type = rtgui_type_create("mywidget", RTGUI_WIDGET_TYPE,
  92. sizeof(rtgui_mywidget_t),
  93. RTGUI_CONSTRUCTOR(_rtgui_mywidget_constructor),
  94. RTGUI_DESTRUCTOR(_rtgui_mywidget_destructor));
  95. }
  96. return mywidget_type;
  97. }
  98. struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
  99. {
  100. struct rtgui_mywidget* me;
  101. me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
  102. if (me != RT_NULL)
  103. {
  104. rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
  105. }
  106. return me;
  107. }
  108. void rtgui_mywidget_destroy(struct rtgui_mywidget* me)
  109. {
  110. rtgui_widget_destroy(RTGUI_WIDGET(me));
  111. }