button.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/rtgui_theme.h>
  17. static void _rtgui_button_constructor(rtgui_button_t *button)
  18. {
  19. /* init widget and set event handler */
  20. RTGUI_WIDGET(button)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  21. rtgui_widget_set_event_handler(RTGUI_WIDGET(button), rtgui_button_event_handler);
  22. /* un-press button */
  23. button->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  24. /* set flag and on_button event handler */
  25. button->pressed_image = RT_NULL;
  26. button->unpressed_image = RT_NULL;
  27. button->on_button = RT_NULL;
  28. /* set gc */
  29. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(button)) = default_foreground;
  30. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(button)) = RTGUI_RGB(212, 208, 200);
  31. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(button)) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
  32. }
  33. static void _rtgui_button_destructor(rtgui_button_t *button)
  34. {
  35. if (button->pressed_image != RT_NULL)
  36. {
  37. rtgui_image_destroy(button->pressed_image);
  38. button->pressed_image = RT_NULL;
  39. }
  40. if (button->unpressed_image != RT_NULL)
  41. {
  42. rtgui_image_destroy(button->unpressed_image);
  43. button->unpressed_image = RT_NULL;
  44. }
  45. }
  46. rtgui_type_t *rtgui_button_type_get(void)
  47. {
  48. static rtgui_type_t *button_type = RT_NULL;
  49. if (!button_type)
  50. {
  51. button_type = rtgui_type_create("button", RTGUI_LABEL_TYPE,
  52. sizeof(rtgui_button_t),
  53. RTGUI_CONSTRUCTOR(_rtgui_button_constructor),
  54. RTGUI_DESTRUCTOR(_rtgui_button_destructor));
  55. }
  56. return button_type;
  57. }
  58. rt_bool_t rtgui_button_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  59. {
  60. struct rtgui_button* btn;
  61. RT_ASSERT(widget != RT_NULL);
  62. btn = (struct rtgui_button*) widget;
  63. switch (event->type)
  64. {
  65. case RTGUI_EVENT_PAINT:
  66. #ifndef RTGUI_USING_SMALL_SIZE
  67. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  68. else
  69. #endif
  70. rtgui_theme_draw_button(btn);
  71. break;
  72. case RTGUI_EVENT_MOUSE_BUTTON:
  73. {
  74. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  75. if (btn->flag & RTGUI_BUTTON_TYPE_PUSH)
  76. {
  77. /* it's a push button */
  78. if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
  79. {
  80. if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  81. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  82. else
  83. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  84. /* draw button */
  85. #ifndef RTGUI_USING_SMALL_SIZE
  86. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  87. else
  88. #endif
  89. rtgui_theme_draw_button(btn);
  90. #ifndef RTGUI_USING_SMALL_SIZE
  91. /* invokes call back */
  92. if (widget->on_mouseclick != RT_NULL &&
  93. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  94. return widget->on_mouseclick(widget, event);
  95. #endif
  96. }
  97. }
  98. else
  99. {
  100. if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
  101. {
  102. /* it's a normal button */
  103. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  104. {
  105. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  106. }
  107. else
  108. {
  109. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  110. }
  111. /* draw button */
  112. #ifndef RTGUI_USING_SMALL_SIZE
  113. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  114. else
  115. #endif
  116. rtgui_theme_draw_button(btn);
  117. #ifndef RTGUI_USING_SMALL_SIZE
  118. /* invokes call back */
  119. if (widget->on_mouseclick != RT_NULL &&
  120. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  121. return widget->on_mouseclick(widget, event);
  122. #endif
  123. }
  124. }
  125. return RT_TRUE;
  126. }
  127. }
  128. return RT_FALSE;
  129. }
  130. rtgui_button_t* rtgui_button_create(unsigned char* text)
  131. {
  132. struct rtgui_button* btn;
  133. btn = (struct rtgui_button*) rtgui_widget_create (RTGUI_BUTTON_TYPE);
  134. if (btn != RT_NULL)
  135. {
  136. rtgui_rect_t rect;
  137. /* set default rect */
  138. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  139. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  140. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  141. rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
  142. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  143. }
  144. return btn;
  145. }
  146. rtgui_button_t* rtgui_pushbutton_create(unsigned char* text)
  147. {
  148. rtgui_button_t* btn;
  149. btn = rtgui_button_create(text);
  150. if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
  151. return btn;
  152. }
  153. void rtgui_button_destroy(rtgui_button_t* btn)
  154. {
  155. rtgui_widget_destroy(RTGUI_WIDGET(btn));
  156. }
  157. void rtgui_button_set_pressed_image(rtgui_button_t* btn, rtgui_image_t* image)
  158. {
  159. RT_ASSERT(btn != RT_NULL);
  160. btn->pressed_image = image;
  161. }
  162. void rtgui_button_set_unpressed_image(rtgui_button_t* btn, rtgui_image_t* image)
  163. {
  164. RT_ASSERT(btn != RT_NULL);
  165. btn->unpressed_image = image;
  166. }