1
0

mywidget.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_BC(dc) = white;
  16. rtgui_dc_fill_rect(dc, &rect);
  17. /* 计算中心原点 */
  18. x = (rect.x2 + rect.x1)/2;
  19. y = (rect.y2 + rect.y1)/2;
  20. /* 绘制�字�*/
  21. RTGUI_DC_FC(dc) = black;
  22. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
  23. rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
  24. /* 根�状�绘制圆�*/
  25. if (me->status == MYWIDGET_STATUS_ON)
  26. RTGUI_DC_FC(dc) = green;
  27. else
  28. RTGUI_DC_FC(dc) = red;
  29. rtgui_dc_fill_circle(dc, x, y, 5);
  30. /* 结�绘图 */
  31. rtgui_dc_end_drawing(dc);
  32. return;
  33. }
  34. /* 鼠标事件处�函数 */
  35. static void rtgui_mywidget_onmouse(struct rtgui_mywidget* me, struct rtgui_event_mouse* mouse)
  36. {
  37. struct rtgui_rect rect;
  38. rt_uint16_t x, y;
  39. /* 仅对鼠标抬起动作进行处� */
  40. if (!(mouse->button & RTGUI_MOUSE_BUTTON_UP)) return;
  41. /* 获得控件的��*/
  42. rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
  43. /* get_rect函数获得是控件的相对�置,而鼠标事件给出的�标是�对�标,需��一个转�*/
  44. rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
  45. /* 计算中心原点 */
  46. x = (rect.x2 + rect.x1)/2;
  47. y = (rect.y2 + rect.y1)/2;
  48. /* 比较鼠标�标是�在圈�*/
  49. if ((mouse->x < x + 5 && mouse->x > x - 5) &&
  50. (mouse->y < y + 5 && mouse->y > y - 5))
  51. {
  52. /* 更改控件状�*/
  53. if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
  54. else me->status = MYWIDGET_STATUS_ON;
  55. /* 刷新(�新绘制)控件 */
  56. rtgui_mywidget_ondraw(me);
  57. }
  58. }
  59. /* mywidget控件的事件处�函�*/
  60. rt_bool_t rtgui_mywidget_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  61. {
  62. /* 调用事件处�函数时,widget指针指�控件本身,所以先获得相应控件对象的指�*/
  63. struct rtgui_mywidget* me = RTGUI_MYWIDGET(widget);
  64. switch (event->type)
  65. {
  66. case RTGUI_EVENT_PAINT:
  67. /* 绘制事件,调用绘图函数绘�*/
  68. rtgui_mywidget_ondraw(me);
  69. break;
  70. case RTGUI_EVENT_MOUSE_BUTTON:
  71. /* 鼠标事件 */
  72. rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse*) event);
  73. break;
  74. /* 其他事件调用父类的事件处�函�*/
  75. default:
  76. return rtgui_widget_event_handler(widget, event);
  77. }
  78. return RT_FALSE;
  79. }
  80. /* 自定义控件的构造函�*/
  81. static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
  82. {
  83. /* 默认这个控件接收�焦 */
  84. RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  85. /* �始化控件并设置事件处�函数 */
  86. rtgui_widget_set_event_handler(RTGUI_WIDGET(mywidget), rtgui_mywidget_event_handler);
  87. /* �始状�时OFF */
  88. mywidget->status = MYWIDGET_STATUS_OFF;
  89. }
  90. DEFINE_CLASS_TYPE(mywidget, "mywidget",
  91. RTGUI_WIDGET_TYPE,
  92. _rtgui_mywidget_constructor,
  93. RT_NULL,
  94. sizeof(struct rtgui_mywidget));
  95. /* 创建一个自定义控件 */
  96. struct rtgui_mywidget* rtgui_mywidget_create(rtgui_rect_t* r)
  97. {
  98. struct rtgui_mywidget* me;
  99. /* 让rtgui_widget创建出一个指定类型:RTGUI_MYWIDGET_TYPE类型的控�*/
  100. me = (struct rtgui_mywidget*) rtgui_widget_create (RTGUI_MYWIDGET_TYPE);
  101. if (me != RT_NULL)
  102. {
  103. rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
  104. }
  105. return me;
  106. }
  107. /* 删除一个自定义控件 */
  108. void rtgui_mywidget_destroy(struct rtgui_mywidget* me)
  109. {
  110. rtgui_widget_destroy(RTGUI_WIDGET(me));
  111. }