button.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * File : button.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/widgets/button.h>
  16. #include <rtgui/rtgui_theme.h>
  17. static void _rtgui_button_constructor(rtgui_button_t *button)
  18. {
  19. /* init widget and set event handler */
  20. RTGUI_WIDGET(button)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  21. rtgui_widget_set_event_handler(RTGUI_WIDGET(button), rtgui_button_event_handler);
  22. /* un-press button */
  23. button->flag = 0;
  24. /* set flag and on_button event handler */
  25. button->pressed_image = RT_NULL;
  26. button->unpressed_image = RT_NULL;
  27. button->on_button = RT_NULL;
  28. /* set gc */
  29. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(button)) = default_foreground;
  30. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(button)) = RTGUI_RGB(212, 208, 200);
  31. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(button)) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
  32. }
  33. static void _rtgui_button_destructor(rtgui_button_t *button)
  34. {
  35. if (button->pressed_image != RT_NULL)
  36. {
  37. rtgui_image_destroy(button->pressed_image);
  38. button->pressed_image = RT_NULL;
  39. }
  40. if (button->unpressed_image != RT_NULL)
  41. {
  42. rtgui_image_destroy(button->unpressed_image);
  43. button->unpressed_image = RT_NULL;
  44. }
  45. }
  46. DEFINE_CLASS_TYPE(button, "button",
  47. RTGUI_LABEL_TYPE,
  48. _rtgui_button_constructor,
  49. _rtgui_button_destructor,
  50. sizeof(struct rtgui_button));
  51. rt_bool_t rtgui_button_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  52. {
  53. struct rtgui_button* btn;
  54. RT_ASSERT(widget != RT_NULL);
  55. btn = (struct rtgui_button*) widget;
  56. switch (event->type)
  57. {
  58. case RTGUI_EVENT_PAINT:
  59. #ifndef RTGUI_USING_SMALL_SIZE
  60. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  61. else
  62. #endif
  63. rtgui_theme_draw_button(btn);
  64. break;
  65. case RTGUI_EVENT_KBD:
  66. {
  67. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*) event;
  68. if (RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
  69. if ((ekbd->key == RTGUIK_RETURN) || (ekbd->key == RTGUIK_SPACE))
  70. {
  71. if (RTGUI_KBD_IS_DOWN(ekbd))
  72. {
  73. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  74. }
  75. else
  76. {
  77. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  78. }
  79. /* draw button */
  80. rtgui_theme_draw_button(btn);
  81. if ((btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL))
  82. {
  83. /* call on button handler */
  84. btn->on_button(widget, event);
  85. }
  86. }
  87. }
  88. break;
  89. case RTGUI_EVENT_MOUSE_BUTTON:
  90. if (RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
  91. {
  92. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  93. if (btn->flag & RTGUI_BUTTON_TYPE_PUSH)
  94. {
  95. /* it's a push button */
  96. if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
  97. {
  98. if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  99. {
  100. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  101. }
  102. else
  103. {
  104. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  105. }
  106. /* draw button */
  107. #ifndef RTGUI_USING_SMALL_SIZE
  108. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  109. else
  110. #endif
  111. rtgui_theme_draw_button(btn);
  112. if (btn->on_button != RT_NULL)
  113. {
  114. /* call on button handler */
  115. btn->on_button(widget, event);
  116. }
  117. #ifndef RTGUI_USING_SMALL_SIZE
  118. /* invokes call back */
  119. if (widget->on_mouseclick != RT_NULL &&
  120. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  121. return widget->on_mouseclick(widget, event);
  122. #endif
  123. }
  124. }
  125. else
  126. {
  127. if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
  128. {
  129. /* it's a normal button */
  130. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  131. {
  132. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  133. }
  134. else
  135. {
  136. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  137. }
  138. /* draw button */
  139. #ifndef RTGUI_USING_SMALL_SIZE
  140. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  141. else
  142. #endif
  143. rtgui_theme_draw_button(btn);
  144. #ifndef RTGUI_USING_SMALL_SIZE
  145. /* invokes call back */
  146. if (widget->on_mouseclick != RT_NULL &&
  147. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  148. return widget->on_mouseclick(widget, event);
  149. #endif
  150. if (!(btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL))
  151. {
  152. /* call on button handler */
  153. btn->on_button(widget, event);
  154. }
  155. }
  156. }
  157. return RT_TRUE;
  158. }
  159. }
  160. return RT_FALSE;
  161. }
  162. rtgui_button_t* rtgui_button_create(const char* text)
  163. {
  164. struct rtgui_button* btn;
  165. btn = (struct rtgui_button*) rtgui_widget_create (RTGUI_BUTTON_TYPE);
  166. if (btn != RT_NULL)
  167. {
  168. rtgui_rect_t rect;
  169. /* set default rect */
  170. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  171. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  172. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  173. rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
  174. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  175. }
  176. return btn;
  177. }
  178. rtgui_button_t* rtgui_pushbutton_create(const char* text)
  179. {
  180. rtgui_button_t* btn;
  181. btn = rtgui_button_create(text);
  182. if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
  183. return btn;
  184. }
  185. void rtgui_button_destroy(rtgui_button_t* btn)
  186. {
  187. rtgui_widget_destroy(RTGUI_WIDGET(btn));
  188. }
  189. void rtgui_button_set_pressed_image(rtgui_button_t* btn, rtgui_image_t* image)
  190. {
  191. RT_ASSERT(btn != RT_NULL);
  192. btn->pressed_image = image;
  193. }
  194. void rtgui_button_set_unpressed_image(rtgui_button_t* btn, rtgui_image_t* image)
  195. {
  196. RT_ASSERT(btn != RT_NULL);
  197. btn->unpressed_image = image;
  198. }
  199. void rtgui_button_set_onbutton(rtgui_button_t* btn, rtgui_onbutton_func_t func)
  200. {
  201. RT_ASSERT(btn != RT_NULL);
  202. btn->on_button = func;
  203. }