button.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/widgets/container.h>
  17. #include <rtgui/rtgui_theme.h>
  18. static rt_bool_t rtgui_button_onunfocus(PVOID wdt, 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_FLAG(button) |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  23. rtgui_widget_set_event_handler(button, rtgui_button_event_handler);
  24. rtgui_widget_set_onunfocus(button, rtgui_button_onunfocus);
  25. /* un-press button */
  26. button->flag = 0;
  27. rtgui_widget_set_style(button,RTGUI_BORDER_UP);
  28. /* set flag and on_button event handler */
  29. button->image = RT_NULL;
  30. button->on_button = RT_NULL;
  31. /* set gc */
  32. RTGUI_WIDGET_FOREGROUND(button) = default_foreground;
  33. RTGUI_WIDGET_BACKGROUND(button) = default_background;
  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->image != RT_NULL)
  39. {
  40. rtgui_image_destroy(button->image);
  41. button->image = RT_NULL;
  42. }
  43. }
  44. rtgui_type_t *rtgui_button_type_get(void)
  45. {
  46. static rtgui_type_t *button_type = RT_NULL;
  47. if(!button_type)
  48. {
  49. button_type = rtgui_type_create("button", RTGUI_LABEL_TYPE,
  50. sizeof(rtgui_button_t),
  51. RTGUI_CONSTRUCTOR(_rtgui_button_constructor),
  52. RTGUI_DESTRUCTOR(_rtgui_button_destructor));
  53. }
  54. return button_type;
  55. }
  56. rtgui_button_t* rtgui_button_create(PVOID parent, char* text,int left, int top, int w, int h)
  57. {
  58. rtgui_button_t* btn;
  59. RT_ASSERT(parent != RT_NULL);
  60. btn = rtgui_widget_create(RTGUI_BUTTON_TYPE);
  61. if(btn != RT_NULL)
  62. {
  63. rtgui_rect_t rect;
  64. /* set default rect */
  65. rtgui_font_get_string_rect(RTGUI_WIDGET_FONT(btn), text, &rect);
  66. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  67. rtgui_widget_get_rect(parent, &rect);
  68. rtgui_widget_rect_to_device(parent,&rect);
  69. rect.x1 += left;
  70. rect.y1 += top;
  71. rect.x2 = rect.x1+w;
  72. rect.y2 = rect.y1+h;
  73. rtgui_widget_set_rect(btn, &rect);
  74. rtgui_container_add_child(parent, btn);
  75. }
  76. return btn;
  77. }
  78. void rtgui_button_destroy(rtgui_button_t* btn)
  79. {
  80. rtgui_widget_destroy(btn);
  81. }
  82. rt_bool_t rtgui_button_event_handler(PVOID wdt, rtgui_event_t* event)
  83. {
  84. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  85. rtgui_button_t* btn = (rtgui_button_t*) widget;
  86. if(btn == RT_NULL)return RT_FALSE;
  87. switch (event->type)
  88. {
  89. case RTGUI_EVENT_PAINT:
  90. if(widget->on_draw != RT_NULL )
  91. widget->on_draw(widget, event);
  92. else
  93. rtgui_theme_draw_button(btn);
  94. break;
  95. case RTGUI_EVENT_KBD:
  96. {
  97. rtgui_event_kbd_t* ekbd = (rtgui_event_kbd_t*) event;
  98. if((ekbd->key == RTGUIK_RETURN) || (ekbd->key == RTGUIK_SPACE))
  99. {
  100. if(RTGUI_KBD_IS_DOWN(ekbd))
  101. {
  102. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  103. }
  104. else
  105. {
  106. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  107. }
  108. /* draw button */
  109. rtgui_theme_draw_button(btn);
  110. if((btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL))
  111. {
  112. /* call on button handler */
  113. btn->on_button(widget, event);
  114. }
  115. }
  116. }
  117. break;
  118. case RTGUI_EVENT_MOUSE_BUTTON:
  119. {
  120. rtgui_event_mouse_t* emouse = (rtgui_event_mouse_t*)event;
  121. if(emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
  122. {
  123. rtgui_widget_focus(widget);
  124. /* it's a normal button */
  125. if(emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  126. {
  127. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  128. }
  129. else
  130. {
  131. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  132. }
  133. /* draw button */
  134. if(widget->on_draw != RT_NULL )
  135. widget->on_draw(widget, event);
  136. else
  137. {
  138. rtgui_theme_draw_button(btn);
  139. }
  140. /* invokes call back */
  141. if(widget->on_mouseclick != RT_NULL && emouse->button & RTGUI_MOUSE_BUTTON_UP)
  142. return widget->on_mouseclick(widget, event);
  143. if(!(btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL))
  144. {
  145. /* call on button handler */
  146. btn->on_button(widget, event);
  147. }
  148. }
  149. return RT_TRUE;
  150. }
  151. default:
  152. break;
  153. }
  154. return RT_FALSE;
  155. }
  156. void rtgui_button_set_image(rtgui_button_t* btn, rtgui_image_t* image)
  157. {
  158. if(btn == RT_NULL)return;
  159. btn->image = image;
  160. }
  161. void rtgui_button_set_onbutton(rtgui_button_t* btn, rtgui_onbutton_func_t func)
  162. {
  163. RT_ASSERT(btn != RT_NULL);
  164. btn->on_button = func;
  165. }
  166. static rt_bool_t rtgui_button_onunfocus(PVOID wdt, rtgui_event_t* event)
  167. {
  168. rtgui_rect_t rect;
  169. rtgui_button_t *btn = (rtgui_button_t*)wdt;
  170. rtgui_dc_t *dc;
  171. RT_ASSERT(wdt != RT_NULL);
  172. dc = rtgui_dc_begin_drawing(wdt);
  173. if(dc == RT_NULL)return RT_FALSE;
  174. rtgui_widget_get_rect(btn, &rect);
  175. if(!RTGUI_WIDGET_IS_FOCUSED(btn))
  176. {//Çå³ý½¹µã¿ò
  177. rtgui_color_t color;
  178. rtgui_rect_inflate(&rect, -2);
  179. color = RTGUI_DC_FC(dc);
  180. RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc);
  181. rtgui_dc_draw_focus_rect(dc,&rect);
  182. RTGUI_DC_FC(dc) = color;
  183. }
  184. rtgui_dc_end_drawing(dc);
  185. return RT_TRUE;
  186. }
  187. void rtgui_button_set_text(rtgui_button_t* btn, const char* text)
  188. {
  189. if(btn == RT_NULL)return;
  190. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  191. /* update widget */
  192. rtgui_theme_draw_button(btn);
  193. }