checkbox.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_theme.h>
  3. #include <rtgui/widgets/checkbox.h>
  4. static void _rtgui_checkbox_constructor(rtgui_checkbox_t *box)
  5. {
  6. /* init widget and set event handler */
  7. RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  8. rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_checkbox_event_handler);
  9. /* set status */
  10. box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED;
  11. /* set default gc */
  12. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(box)) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
  13. }
  14. rtgui_type_t *rtgui_checkbox_type_get(void)
  15. {
  16. static rtgui_type_t *checkbox_type = RT_NULL;
  17. if (!checkbox_type)
  18. {
  19. checkbox_type = rtgui_type_create("checkbox", RTGUI_LABEL_TYPE,
  20. sizeof(rtgui_checkbox_t), RTGUI_CONSTRUCTOR(_rtgui_checkbox_constructor), RT_NULL);
  21. }
  22. return checkbox_type;
  23. }
  24. rt_bool_t rtgui_checkbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  25. {
  26. struct rtgui_checkbox* box = (struct rtgui_checkbox*)widget;
  27. switch (event->type)
  28. {
  29. case RTGUI_EVENT_PAINT:
  30. #ifndef RTGUI_USING_SMALL_SIZE
  31. if (widget->on_draw != RT_NULL)
  32. {
  33. return widget->on_draw(widget, event);
  34. }
  35. else
  36. #endif
  37. rtgui_theme_draw_checkbox(box);
  38. break;
  39. case RTGUI_EVENT_MOUSE_BUTTON:
  40. {
  41. if (RTGUI_WIDGET_IS_ENABLE(widget) && !RTGUI_WIDGET_IS_HIDE(widget))
  42. {
  43. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  44. if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT &&
  45. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  46. {
  47. /* set focus */
  48. RTGUI_WIDGET_FOCUS(widget);
  49. if (box->status_down & RTGUI_CHECKBOX_STATUS_UNCHECKED)
  50. {
  51. /* check it */
  52. box->status_down = RTGUI_CHECKBOX_STATUS_CHECKED;
  53. }
  54. else
  55. {
  56. /* un-check it */
  57. box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED;
  58. }
  59. }
  60. /* draw checkbox */
  61. rtgui_theme_draw_checkbox(box);
  62. #ifndef RTGUI_USING_SMALL_SIZE
  63. /* call user callback */
  64. if (widget->on_mouseclick != RT_NULL)
  65. {
  66. return widget->on_mouseclick(widget, event);
  67. }
  68. #endif
  69. }
  70. return RT_TRUE;
  71. }
  72. }
  73. return RT_FALSE;
  74. }
  75. struct rtgui_checkbox* rtgui_checkbox_create(unsigned char* text)
  76. {
  77. struct rtgui_checkbox* box;
  78. box = (struct rtgui_checkbox*) rtgui_widget_create (RTGUI_CHECKBOX_TYPE);
  79. if (box != RT_NULL)
  80. {
  81. rtgui_rect_t rect;
  82. /* set default rect */
  83. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  84. rect.x2 += RTGUI_BORDER_DEFAULT_WIDTH + 5 + (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  85. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  86. rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);
  87. rtgui_label_set_text(RTGUI_LABEL(box), text);
  88. }
  89. return box;
  90. }
  91. void rtgui_checkbox_destroy(rtgui_checkbox_t* box)
  92. {
  93. rtgui_widget_destroy(RTGUI_WIDGET(box));
  94. }