button.c 8.5 KB

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