button.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. /* need callback */
  139. rt_bool_t need_cb = RT_FALSE;
  140. win = RTGUI_WIN(RTGUI_WIDGET(btn)->toplevel);
  141. win->last_mevent_widget = RTGUI_WIDGET(btn);
  142. /* we need to decide whether the callback will be invoked
  143. * before the flag has changed. Moreover, we cannot invoke
  144. * it directly here, because the button might be destroyed
  145. * in the callback. If that happens, program will crash on
  146. * the following code. We need to make sure that the
  147. * callbacks are invoke at the very last step. */
  148. if ((btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  149. && (emouse->button & RTGUI_MOUSE_BUTTON_UP))
  150. {
  151. need_cb = RT_TRUE;
  152. }
  153. /* it's a normal button */
  154. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  155. {
  156. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  157. }
  158. else
  159. {
  160. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  161. }
  162. /* draw button */
  163. rtgui_theme_draw_button(btn);
  164. if (need_cb)
  165. {
  166. if (btn->on_button)
  167. btn->on_button(RTGUI_OBJECT(widget), event);
  168. #ifndef RTGUI_USING_SMALL_SIZE
  169. if (widget->on_mouseclick)
  170. return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
  171. #endif
  172. }
  173. }
  174. }
  175. return RT_TRUE;
  176. }
  177. default:
  178. return rtgui_widget_event_handler(object, event);
  179. }
  180. return RT_FALSE;
  181. }
  182. RTM_EXPORT(rtgui_button_event_handler);
  183. rtgui_button_t *rtgui_button_create(const char *text)
  184. {
  185. struct rtgui_button *btn;
  186. btn = (struct rtgui_button *) rtgui_widget_create(RTGUI_BUTTON_TYPE);
  187. if (btn != RT_NULL)
  188. {
  189. rtgui_rect_t rect;
  190. /* set default rect */
  191. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  192. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  193. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  194. rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
  195. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  196. }
  197. return btn;
  198. }
  199. RTM_EXPORT(rtgui_button_create);
  200. rtgui_button_t *rtgui_pushbutton_create(const char *text)
  201. {
  202. rtgui_button_t *btn;
  203. btn = rtgui_button_create(text);
  204. if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
  205. return btn;
  206. }
  207. RTM_EXPORT(rtgui_pushbutton_create);
  208. void rtgui_button_destroy(rtgui_button_t *btn)
  209. {
  210. rtgui_widget_destroy(RTGUI_WIDGET(btn));
  211. }
  212. RTM_EXPORT(rtgui_button_destroy);
  213. void rtgui_button_set_pressed_image(rtgui_button_t *btn, rtgui_image_t *image)
  214. {
  215. RT_ASSERT(btn != RT_NULL);
  216. btn->pressed_image = image;
  217. }
  218. RTM_EXPORT(rtgui_button_set_pressed_image);
  219. void rtgui_button_set_unpressed_image(rtgui_button_t *btn, rtgui_image_t *image)
  220. {
  221. RT_ASSERT(btn != RT_NULL);
  222. btn->unpressed_image = image;
  223. }
  224. RTM_EXPORT(rtgui_button_set_unpressed_image);
  225. void rtgui_button_set_onbutton(rtgui_button_t *btn, rtgui_onbutton_func_t func)
  226. {
  227. RT_ASSERT(btn != RT_NULL);
  228. btn->on_button = func;
  229. }
  230. RTM_EXPORT(rtgui_button_set_onbutton);
  231. static rt_bool_t rtgui_button_onunfocus(struct rtgui_object *object, rtgui_event_t *event)
  232. {
  233. rtgui_rect_t rect;
  234. rtgui_widget_t *widget;
  235. struct rtgui_dc *dc;
  236. RT_ASSERT(object);
  237. widget = RTGUI_WIDGET(object);
  238. dc = rtgui_dc_begin_drawing(widget);
  239. if (dc == RT_NULL) return RT_FALSE;
  240. rtgui_widget_get_rect(widget, &rect);
  241. if (!RTGUI_WIDGET_IS_FOCUSED(widget))
  242. {
  243. /* only clear focus rect */
  244. rtgui_color_t color;
  245. rtgui_rect_inflate(&rect, -2);
  246. color = RTGUI_DC_FC(dc);
  247. RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc);
  248. rtgui_dc_draw_focus_rect(dc, &rect);
  249. RTGUI_DC_FC(dc) = color;
  250. }
  251. rtgui_dc_end_drawing(dc);
  252. return RT_TRUE;
  253. }