1
0

button.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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))
  93. return RT_FALSE;
  94. {
  95. struct rtgui_event_mouse *emouse = (struct rtgui_event_mouse *)event;
  96. /* it's not this widget event, clean status */
  97. if (rtgui_rect_contains_point(&(RTGUI_WIDGET(btn)->extent),
  98. emouse->x, emouse->y) != RT_EOK)
  99. {
  100. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  101. /* draw button */
  102. rtgui_theme_draw_button(btn);
  103. break;
  104. }
  105. if (btn->flag & RTGUI_BUTTON_TYPE_PUSH)
  106. {
  107. /* it's a push button */
  108. if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
  109. {
  110. if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  111. {
  112. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  113. }
  114. else
  115. {
  116. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  117. }
  118. /* draw button */
  119. rtgui_theme_draw_button(btn);
  120. if (btn->on_button != RT_NULL)
  121. {
  122. /* call on button handler */
  123. btn->on_button(RTGUI_OBJECT(widget), event);
  124. }
  125. #ifndef RTGUI_USING_SMALL_SIZE
  126. /* invokes call back */
  127. if (widget->on_mouseclick != RT_NULL &&
  128. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  129. return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
  130. #endif
  131. }
  132. }
  133. else
  134. {
  135. if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
  136. {
  137. /* set the last mouse event handled widget */
  138. struct rtgui_win *win;
  139. /* need callback */
  140. rt_bool_t need_cb = RT_FALSE;
  141. /* we need to decide whether the callback will be invoked
  142. * before the flag has changed. Moreover, we cannot invoke
  143. * it directly here, because the button might be destroyed
  144. * in the callback. If that happens, program will crash on
  145. * the following code. We need to make sure that the
  146. * callbacks are invoke at the very last step. */
  147. if ((btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  148. && (emouse->button & RTGUI_MOUSE_BUTTON_UP))
  149. {
  150. need_cb = RT_TRUE;
  151. }
  152. /* if the button will handle the mouse up event here, it
  153. * should not be the last_mevent_widget. Take care that
  154. * don't overwrite other widgets. */
  155. win = RTGUI_WIN(RTGUI_WIDGET(btn)->toplevel);
  156. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  157. {
  158. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  159. win->last_mevent_widget = RTGUI_WIDGET(btn);
  160. }
  161. else
  162. {
  163. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  164. if (win->last_mevent_widget == RTGUI_WIDGET(btn))
  165. win->last_mevent_widget = RT_NULL;
  166. }
  167. /* draw button */
  168. rtgui_theme_draw_button(btn);
  169. if (need_cb)
  170. {
  171. if (btn->on_button)
  172. btn->on_button(RTGUI_OBJECT(widget), event);
  173. #ifndef RTGUI_USING_SMALL_SIZE
  174. if (widget->on_mouseclick)
  175. return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
  176. #endif
  177. }
  178. }
  179. }
  180. return RT_TRUE;
  181. }
  182. default:
  183. return rtgui_widget_event_handler(object, event);
  184. }
  185. return RT_FALSE;
  186. }
  187. RTM_EXPORT(rtgui_button_event_handler);
  188. rtgui_button_t *rtgui_button_create(const char *text)
  189. {
  190. struct rtgui_button *btn;
  191. btn = (struct rtgui_button *) rtgui_widget_create(RTGUI_BUTTON_TYPE);
  192. if (btn != RT_NULL)
  193. {
  194. rtgui_rect_t rect;
  195. /* set default rect */
  196. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  197. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  198. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  199. rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
  200. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  201. }
  202. return btn;
  203. }
  204. RTM_EXPORT(rtgui_button_create);
  205. rtgui_button_t *rtgui_pushbutton_create(const char *text)
  206. {
  207. rtgui_button_t *btn;
  208. btn = rtgui_button_create(text);
  209. if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
  210. return btn;
  211. }
  212. RTM_EXPORT(rtgui_pushbutton_create);
  213. void rtgui_button_destroy(rtgui_button_t *btn)
  214. {
  215. rtgui_widget_destroy(RTGUI_WIDGET(btn));
  216. }
  217. RTM_EXPORT(rtgui_button_destroy);
  218. void rtgui_button_set_pressed_image(rtgui_button_t *btn, rtgui_image_t *image)
  219. {
  220. RT_ASSERT(btn != RT_NULL);
  221. btn->pressed_image = image;
  222. }
  223. RTM_EXPORT(rtgui_button_set_pressed_image);
  224. void rtgui_button_set_unpressed_image(rtgui_button_t *btn, rtgui_image_t *image)
  225. {
  226. RT_ASSERT(btn != RT_NULL);
  227. btn->unpressed_image = image;
  228. }
  229. RTM_EXPORT(rtgui_button_set_unpressed_image);
  230. void rtgui_button_set_onbutton(rtgui_button_t *btn, rtgui_onbutton_func_t func)
  231. {
  232. RT_ASSERT(btn != RT_NULL);
  233. btn->on_button = func;
  234. }
  235. RTM_EXPORT(rtgui_button_set_onbutton);
  236. static rt_bool_t rtgui_button_onunfocus(struct rtgui_object *object, rtgui_event_t *event)
  237. {
  238. rtgui_rect_t rect;
  239. rtgui_widget_t *widget;
  240. struct rtgui_dc *dc;
  241. RT_ASSERT(object);
  242. widget = RTGUI_WIDGET(object);
  243. dc = rtgui_dc_begin_drawing(widget);
  244. if (dc == RT_NULL) return RT_FALSE;
  245. rtgui_widget_get_rect(widget, &rect);
  246. if (!RTGUI_WIDGET_IS_FOCUSED(widget))
  247. {
  248. /* only clear focus rect */
  249. rtgui_color_t color;
  250. rtgui_rect_inflate(&rect, -2);
  251. color = RTGUI_DC_FC(dc);
  252. RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc);
  253. rtgui_dc_draw_focus_rect(dc, &rect);
  254. RTGUI_DC_FC(dc) = color;
  255. }
  256. rtgui_dc_end_drawing(dc);
  257. return RT_TRUE;
  258. }