checkbox.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_theme.h>
  3. #include <rtgui/widgets/container.h>
  4. #include <rtgui/widgets/checkbox.h>
  5. static rt_bool_t rtgui_checkbox_onunfocus(PVOID wdt, rtgui_event_t* event);
  6. static void _rtgui_checkbox_constructor(rtgui_checkbox_t *checkbox)
  7. {
  8. /* init widget and set event handler */
  9. RTGUI_WIDGET_FLAG(checkbox) |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  10. rtgui_widget_set_event_handler(checkbox, rtgui_checkbox_event_handler);
  11. rtgui_widget_set_onunfocus(checkbox, rtgui_checkbox_onunfocus);
  12. /* set status */
  13. checkbox->value = 0;
  14. /* set default gc */
  15. RTGUI_WIDGET_TEXTALIGN(checkbox) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
  16. }
  17. static void _rtgui_checkbox_destructor(rtgui_checkbox_t *checkbox)
  18. {
  19. }
  20. rtgui_type_t *rtgui_checkbox_type_get(void)
  21. {
  22. static rtgui_type_t *checkbox_type = RT_NULL;
  23. if(!checkbox_type)
  24. {
  25. checkbox_type = rtgui_type_create("checkbox", RTGUI_LABEL_TYPE,
  26. sizeof(rtgui_checkbox_t),
  27. RTGUI_CONSTRUCTOR(_rtgui_checkbox_constructor),
  28. RTGUI_DESTRUCTOR(_rtgui_checkbox_destructor));
  29. }
  30. return checkbox_type;
  31. }
  32. rtgui_checkbox_t* rtgui_checkbox_create(PVOID parent,const char* text, rt_bool_t checked,rtgui_color_t fc,int left,int top)
  33. {
  34. rtgui_checkbox_t* box;
  35. RT_ASSERT(parent != RT_NULL);
  36. box = rtgui_widget_create (RTGUI_CHECKBOX_TYPE);
  37. if(box != RT_NULL)
  38. {
  39. rtgui_rect_t rect,text_rect;
  40. rtgui_widget_get_rect(parent, &rect);
  41. rtgui_widget_rect_to_device(parent,&rect);
  42. /* set default rect */
  43. rtgui_font_get_string_rect(RTGUI_WIDGET_FONT(box), text, &text_rect);
  44. text_rect.x2 += RTGUI_WIDGET_BORDER(box) + 5 + (RTGUI_WIDGET_BORDER(box) << 1);
  45. text_rect.y2 += (RTGUI_WIDGET_BORDER(box) << 1);
  46. rect.x1 += left;
  47. rect.y1 += top;
  48. rect.x2 = rect.x1 + CHECK_BOX_W + rtgui_rect_width(text_rect);
  49. rect.y2 = rect.y1 + rtgui_rect_height(text_rect);
  50. rtgui_widget_set_rect(box, &rect);
  51. rtgui_checkbox_set_text(box, text);
  52. RTGUI_WIDGET_FOREGROUND(box) = fc;
  53. if(checked == RT_TRUE)
  54. box->value = 1;
  55. else
  56. box->value = 0;
  57. rtgui_container_add_child(parent, box);
  58. }
  59. return box;
  60. }
  61. void rtgui_checkbox_destroy(rtgui_checkbox_t* box)
  62. {
  63. rtgui_widget_destroy(box);
  64. }
  65. rt_bool_t rtgui_checkbox_event_handler(PVOID wdt, rtgui_event_t* event)
  66. {
  67. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  68. rtgui_checkbox_t* box = (rtgui_checkbox_t*)widget;
  69. switch (event->type)
  70. {
  71. case RTGUI_EVENT_PAINT:
  72. if(widget->on_draw != RT_NULL)
  73. {
  74. return widget->on_draw(widget, event);
  75. }
  76. else
  77. rtgui_theme_draw_checkbox(box);
  78. break;
  79. case RTGUI_EVENT_MOUSE_BUTTON:
  80. if(RTGUI_WIDGET_IS_ENABLE(widget) && !RTGUI_WIDGET_IS_HIDE(widget))
  81. {
  82. rtgui_event_mouse_t* emouse = (rtgui_event_mouse_t*)event;
  83. if(emouse->button & RTGUI_MOUSE_BUTTON_LEFT && emouse->button & RTGUI_MOUSE_BUTTON_UP)
  84. {
  85. if(box->value)
  86. {
  87. /* check it */
  88. box->value = 0;
  89. }
  90. else
  91. {
  92. /* un-check it */
  93. box->value = 1;
  94. }
  95. }
  96. else if(emouse->button & RTGUI_MOUSE_BUTTON_LEFT && emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  97. {
  98. /* set focus */
  99. rtgui_widget_focus(widget);
  100. }
  101. /* draw checkbox */
  102. rtgui_theme_draw_checkbox(box);
  103. /* call user callback */
  104. if(widget->on_mouseclick != RT_NULL)
  105. {
  106. return widget->on_mouseclick(widget, event);
  107. }
  108. }
  109. return RT_TRUE;
  110. default:
  111. break;
  112. }
  113. return RT_FALSE;
  114. }
  115. void rtgui_checkbox_set_checked(rtgui_checkbox_t* checkbox, rt_bool_t checked)
  116. {
  117. RT_ASSERT(checkbox != RT_NULL);
  118. if(checked == RT_TRUE)
  119. checkbox->value = 1;
  120. else
  121. checkbox->value = 0;
  122. }
  123. rt_bool_t rtgui_checkbox_get_checked(rtgui_checkbox_t* checkbox)
  124. {
  125. RT_ASSERT(checkbox != RT_NULL);
  126. if(checkbox->value)
  127. return RT_TRUE;
  128. return RT_FALSE;
  129. }
  130. static rt_bool_t rtgui_checkbox_onunfocus(PVOID wdt, rtgui_event_t* event)
  131. {
  132. rtgui_rect_t rect;
  133. rtgui_checkbox_t* box = (rtgui_checkbox_t*)wdt;
  134. rtgui_dc_t *dc;
  135. RT_ASSERT(box != RT_NULL);
  136. dc = rtgui_dc_begin_drawing(box);
  137. if(dc == RT_NULL)return RT_FALSE;
  138. rtgui_widget_get_rect(box, &rect);
  139. if(!RTGUI_WIDGET_IS_FOCUSED(box))
  140. {//Çĺłý˝šľăżň
  141. rtgui_rect_t tmp_rect;
  142. rtgui_color_t color;
  143. rtgui_font_get_string_rect(RTGUI_WIDGET_FONT(box), rtgui_label_get_text(RTGUI_LABEL(box)), &tmp_rect);
  144. rtgui_rect_moveto(&tmp_rect,rect.x1+CHECK_BOX_W+7, rect.y1);
  145. tmp_rect.x1 -= 1;
  146. tmp_rect.x2 += 2;
  147. tmp_rect.y2 = rect.y2-1;
  148. /* draw focused border */
  149. color = RTGUI_DC_FC(dc);
  150. RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc);
  151. rtgui_dc_draw_focus_rect(dc,&tmp_rect);
  152. RTGUI_DC_FC(dc) = color;
  153. }
  154. rtgui_dc_end_drawing(dc);
  155. return RT_TRUE;
  156. }
  157. void rtgui_checkbox_set_text(rtgui_checkbox_t *box, const char* text)
  158. {
  159. RT_ASSERT(box != RT_NULL);
  160. if(RTGUI_LABEL(box)->text != RT_NULL)
  161. {
  162. /* release old text memory */
  163. rt_free(RTGUI_LABEL(box)->text);
  164. RTGUI_LABEL(box)->text = RT_NULL;
  165. }
  166. if(text != RT_NULL)
  167. RTGUI_LABEL(box)->text = rt_strdup(text);
  168. else
  169. RTGUI_LABEL(box)->text = RT_NULL;
  170. }