mywidget.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <rtgui/dc.h>
  2. #include "mywidget.h"
  3. static void rtgui_mywidget_ondraw(struct rtgui_mywidget *me)
  4. {
  5. struct rtgui_dc *dc;
  6. struct rtgui_rect rect;
  7. rt_uint16_t x, y;
  8. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(me));
  9. if (dc == RT_NULL) return;
  10. rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
  11. RTGUI_DC_BC(dc) = white;
  12. rtgui_dc_fill_rect(dc, &rect);
  13. x = (rect.x2 + rect.x1) / 2;
  14. y = (rect.y2 + rect.y1) / 2;
  15. RTGUI_DC_FC(dc) = black;
  16. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, y);
  17. rtgui_dc_draw_vline(dc, x, rect.y1, rect.y2);
  18. if (me->status == MYWIDGET_STATUS_ON)
  19. RTGUI_DC_FC(dc) = green;
  20. else
  21. RTGUI_DC_FC(dc) = red;
  22. rtgui_dc_fill_circle(dc, x, y, 5);
  23. rtgui_dc_end_drawing(dc);
  24. return;
  25. }
  26. static void rtgui_mywidget_onmouse(struct rtgui_mywidget *me, struct rtgui_event_mouse *mouse)
  27. {
  28. struct rtgui_rect rect;
  29. rt_uint16_t x, y;
  30. if (!(mouse->button & RTGUI_MOUSE_BUTTON_UP)) return;
  31. rtgui_widget_get_rect(RTGUI_WIDGET(me), &rect);
  32. rtgui_widget_rect_to_device(RTGUI_WIDGET(me), &rect);
  33. x = (rect.x2 + rect.x1) / 2;
  34. y = (rect.y2 + rect.y1) / 2;
  35. if ((mouse->x < x + 5 && mouse->x > x - 5) &&
  36. (mouse->y < y + 5 && mouse->y > y - 5))
  37. {
  38. if (me->status & MYWIDGET_STATUS_ON) me->status = MYWIDGET_STATUS_OFF;
  39. else me->status = MYWIDGET_STATUS_ON;
  40. rtgui_mywidget_ondraw(me);
  41. }
  42. }
  43. rt_bool_t rtgui_mywidget_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  44. {
  45. struct rtgui_mywidget *me = RTGUI_MYWIDGET(object);
  46. switch (event->type)
  47. {
  48. case RTGUI_EVENT_PAINT:
  49. rtgui_mywidget_ondraw(me);
  50. break;
  51. case RTGUI_EVENT_MOUSE_BUTTON:
  52. rtgui_mywidget_onmouse(RTGUI_MYWIDGET(me), (struct rtgui_event_mouse *) event);
  53. break;
  54. default:
  55. return rtgui_widget_event_handler(object, event);
  56. }
  57. return RT_FALSE;
  58. }
  59. static void _rtgui_mywidget_constructor(rtgui_mywidget_t *mywidget)
  60. {
  61. RTGUI_WIDGET(mywidget)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  62. rtgui_object_set_event_handler(RTGUI_OBJECT(mywidget), rtgui_mywidget_event_handler);
  63. mywidget->status = MYWIDGET_STATUS_OFF;
  64. }
  65. DEFINE_CLASS_TYPE(mywidget, "mywidget",
  66. RTGUI_WIDGET_TYPE,
  67. _rtgui_mywidget_constructor,
  68. RT_NULL,
  69. sizeof(struct rtgui_mywidget));
  70. struct rtgui_mywidget *rtgui_mywidget_create(rtgui_rect_t *r)
  71. {
  72. struct rtgui_mywidget *me;
  73. me = (struct rtgui_mywidget *) rtgui_widget_create(RTGUI_MYWIDGET_TYPE);
  74. if (me != RT_NULL)
  75. {
  76. rtgui_widget_set_rect(RTGUI_WIDGET(me), r);
  77. }
  78. return me;
  79. }
  80. void rtgui_mywidget_destroy(struct rtgui_mywidget *me)
  81. {
  82. rtgui_widget_destroy(RTGUI_WIDGET(me));
  83. }