button.c 5.8 KB

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