button.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. rtgui_type_t *rtgui_button_type_get(void)
  34. {
  35. static rtgui_type_t *button_type = RT_NULL;
  36. if (!button_type)
  37. {
  38. button_type = rtgui_type_create("button", RTGUI_LABEL_TYPE,
  39. sizeof(rtgui_button_t), RTGUI_CONSTRUCTOR(_rtgui_button_constructor), RT_NULL);
  40. }
  41. return button_type;
  42. }
  43. rt_bool_t rtgui_button_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  44. {
  45. struct rtgui_button* btn;
  46. RT_ASSERT(widget != RT_NULL);
  47. btn = (struct rtgui_button*) widget;
  48. switch (event->type)
  49. {
  50. case RTGUI_EVENT_PAINT:
  51. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  52. else rtgui_theme_draw_button(btn);
  53. break;
  54. case RTGUI_EVENT_MOUSE_BUTTON:
  55. {
  56. struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
  57. if (btn->flag & RTGUI_BUTTON_TYPE_PUSH)
  58. {
  59. /* it's a push button */
  60. if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
  61. {
  62. if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  63. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  64. else
  65. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  66. /* draw button */
  67. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  68. else rtgui_theme_draw_button(btn);
  69. /* invokes call back */
  70. if (widget->on_mouseclick != RT_NULL &&
  71. emouse->button & RTGUI_MOUSE_BUTTON_UP)
  72. return widget->on_mouseclick(widget, event);
  73. }
  74. }
  75. else
  76. {
  77. if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
  78. {
  79. /* it's a normal button */
  80. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  81. {
  82. btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
  83. }
  84. else
  85. {
  86. btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
  87. }
  88. /* draw button */
  89. if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
  90. else rtgui_theme_draw_button(btn);
  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. }
  96. }
  97. return RT_TRUE;
  98. }
  99. }
  100. return RT_FALSE;
  101. }
  102. rtgui_button_t* rtgui_button_create(unsigned char* text)
  103. {
  104. struct rtgui_button* btn;
  105. btn = (struct rtgui_button*) rtgui_widget_create (RTGUI_BUTTON_TYPE);
  106. if (btn != RT_NULL)
  107. {
  108. rtgui_rect_t rect;
  109. /* set default rect */
  110. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  111. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  112. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  113. rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
  114. rtgui_label_set_text(RTGUI_LABEL(btn), text);
  115. }
  116. return btn;
  117. }
  118. rtgui_button_t* rtgui_pushbutton_create(unsigned char* text)
  119. {
  120. rtgui_button_t* btn;
  121. btn = rtgui_button_create(text);
  122. if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
  123. return btn;
  124. }
  125. void rtgui_button_destroy(rtgui_button_t* btn)
  126. {
  127. rtgui_widget_destroy(RTGUI_WIDGET(btn));
  128. }
  129. void rtgui_button_set_pressed_image(rtgui_button_t* btn, rtgui_image_t* image)
  130. {
  131. RT_ASSERT(btn != RT_NULL);
  132. btn->pressed_image = image;
  133. }
  134. void rtgui_button_set_unpressed_image(rtgui_button_t* btn, rtgui_image_t* image)
  135. {
  136. RT_ASSERT(btn != RT_NULL);
  137. btn->unpressed_image = image;
  138. }