widget.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * File : widget.h
  3. * This file is part of RT-Thread GUI
  4. * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-04 Bernard first version
  23. */
  24. #ifndef __RTGUI_WIDGET_H__
  25. #define __RTGUI_WIDGET_H__
  26. #include <rtgui/rtgui.h>
  27. #include <rtgui/list.h>
  28. #include <rtgui/region.h>
  29. #include <rtgui/event.h>
  30. #include <rtgui/color.h>
  31. #include <rtgui/font.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #define RTGUI_WIDGET_FLAG_DEFAULT 0x0000
  36. #define RTGUI_WIDGET_FLAG_SHOWN 0x0001
  37. #define RTGUI_WIDGET_FLAG_DISABLE 0x0002
  38. #define RTGUI_WIDGET_FLAG_FOCUS 0x0004
  39. #define RTGUI_WIDGET_FLAG_TRANSPARENT 0x0008
  40. #define RTGUI_WIDGET_FLAG_FOCUSABLE 0x0010
  41. #define RTGUI_WIDGET_FLAG_DC_VISIBLE 0x0100
  42. #define RTGUI_WIDGET_FLAG_IN_ANIM 0x0200
  43. /* rtgui widget attribute */
  44. #define RTGUI_WIDGET_FOREGROUND(w) (RTGUI_WIDGET(w)->gc.foreground)
  45. #define RTGUI_WIDGET_BACKGROUND(w) (RTGUI_WIDGET(w)->gc.background)
  46. #define RTGUI_WIDGET_TEXTALIGN(w) (RTGUI_WIDGET(w)->gc.textalign)
  47. #define RTGUI_WIDGET_FONT(w) (RTGUI_WIDGET(w)->gc.font)
  48. #define RTGUI_WIDGET_FLAG(w) (RTGUI_WIDGET(w)->flag)
  49. #define RTGUI_WIDGET_ALIGN(w) (RTGUI_WIDGET(w)->align)
  50. #define RTGUI_WIDGET_BORDER(w) (RTGUI_WIDGET(w)->border)
  51. #define RTGUI_WIDGET_BORDER_STYLE(w) (RTGUI_WIDGET(w)->border_style)
  52. #define RTGUI_WIDGET_UNHIDE(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_SHOWN
  53. #define RTGUI_WIDGET_HIDE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_SHOWN
  54. #define RTGUI_WIDGET_IS_HIDE(w) (!(RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_SHOWN))
  55. #define RTGUI_WIDGET_ENABLE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_DISABLE
  56. #define RTGUI_WIDGET_DISABLE(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_DISABLE
  57. #define RTGUI_WIDGET_IS_ENABLE(w) (!((RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_DISABLE)))
  58. #define RTGUI_WIDGET_UNFOCUS(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_FOCUS
  59. #define RTGUI_WIDGET_FOCUS(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_FOCUS
  60. #define RTGUI_WIDGET_IS_FOCUSED(w) (RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_FOCUS)
  61. #define RTGUI_WIDGET_IS_FOCUSABLE(w) (RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_FOCUSABLE)
  62. #define RTGUI_WIDGET_SET_UNFOCUSABLE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_FOCUSABLE
  63. #define RTGUI_WIDGET_IS_DC_VISIBLE(w) (RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_DC_VISIBLE)
  64. #define RTGUI_WIDGET_DC_SET_VISIBLE(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_DC_VISIBLE
  65. #define RTGUI_WIDGET_DC_SET_UNVISIBLE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_DC_VISIBLE
  66. #define RTGUI_WIDGET_DC(w) ((struct rtgui_dc*)&((w)->dc_type))
  67. DECLARE_CLASS_TYPE(widget);
  68. /** Gets the type of a widget */
  69. #define RTGUI_WIDGET_TYPE (RTGUI_TYPE(widget))
  70. /** Casts the object to a rtgui_widget */
  71. #define RTGUI_WIDGET(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_WIDGET_TYPE, rtgui_widget_t))
  72. /** Check if the object is a rtgui_widget */
  73. #define RTGUI_IS_WIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIDGET_TYPE))
  74. /*
  75. * the base widget object
  76. */
  77. struct rtgui_widget
  78. {
  79. /* inherit from rtgui_object */
  80. struct rtgui_object object;
  81. /* the widget that contains this widget */
  82. struct rtgui_widget *parent;
  83. /* the window that contains this widget */
  84. struct rtgui_win *toplevel;
  85. /* the widget children and sibling */
  86. rtgui_list_t sibling;
  87. /* widget flag */
  88. rt_int32_t flag;
  89. /* hardware device context */
  90. rt_uint32_t dc_type;
  91. const struct rtgui_dc_engine *dc_engine;
  92. /* the graphic context of widget */
  93. rtgui_gc_t gc;
  94. /* the widget extent */
  95. rtgui_rect_t extent;
  96. /* minimal width and height of widget */
  97. rt_int16_t min_width, min_height;
  98. /* widget align */
  99. rt_int32_t align;
  100. rt_uint16_t border;
  101. rt_uint16_t border_style;
  102. /* the rect clip */
  103. rtgui_region_t clip;
  104. /* call back */
  105. rt_bool_t (*on_focus_in)(struct rtgui_object *widget, struct rtgui_event *event);
  106. rt_bool_t (*on_focus_out)(struct rtgui_object *widget, struct rtgui_event *event);
  107. rt_bool_t (*on_paint)(struct rtgui_object *widget, struct rtgui_event *event);
  108. /* user private data */
  109. rt_uint32_t user_data;
  110. };
  111. typedef struct rtgui_widget rtgui_widget_t;
  112. rtgui_widget_t *rtgui_widget_create(const rtgui_type_t *widget_type);
  113. void rtgui_widget_destroy(rtgui_widget_t *widget);
  114. rt_bool_t rtgui_widget_event_handler(struct rtgui_object *object, rtgui_event_t *event);
  115. /* focus and unfocus */
  116. void rtgui_widget_focus(rtgui_widget_t *widget);
  117. void rtgui_widget_unfocus(rtgui_widget_t *widget);
  118. /* event handler for each command */
  119. void rtgui_widget_set_onfocus(rtgui_widget_t *widget, rtgui_event_handler_ptr handler);
  120. void rtgui_widget_set_onunfocus(rtgui_widget_t *widget, rtgui_event_handler_ptr handler);
  121. void rtgui_widget_set_onpaint(rtgui_widget_t *widget, rtgui_event_handler_ptr handler);
  122. /* get and set rect of widget */
  123. void rtgui_widget_get_rect(rtgui_widget_t *widget, rtgui_rect_t *rect);
  124. void rtgui_widget_set_border(rtgui_widget_t *widget, rt_uint32_t style);
  125. void rtgui_widget_set_rect(rtgui_widget_t *widget, const rtgui_rect_t *rect);
  126. void rtgui_widget_set_rectangle(rtgui_widget_t *widget, int x, int y, int width, int height);
  127. void rtgui_widget_get_extent(rtgui_widget_t *widget, rtgui_rect_t *rect);
  128. void rtgui_widget_set_minsize(rtgui_widget_t *widget, int width, int height);
  129. void rtgui_widget_set_minwidth(rtgui_widget_t *widget, int width);
  130. void rtgui_widget_set_minheight(rtgui_widget_t *widget, int height);
  131. void rtgui_widget_set_parent(rtgui_widget_t *widget, rtgui_widget_t *parent);
  132. /* get the physical position of a logic point on widget */
  133. void rtgui_widget_point_to_device(rtgui_widget_t *widget, rtgui_point_t *point);
  134. /* get the physical position of a logic rect on widget */
  135. void rtgui_widget_rect_to_device(rtgui_widget_t *widget, rtgui_rect_t *rect);
  136. /* get the logic position of a physical point on widget */
  137. void rtgui_widget_point_to_logic(rtgui_widget_t *widget, rtgui_point_t *point);
  138. /* get the logic position of a physical rect on widget */
  139. void rtgui_widget_rect_to_logic(rtgui_widget_t *widget, rtgui_rect_t *rect);
  140. void rtgui_widget_clip_parent(rtgui_widget_t *widget);
  141. void rtgui_widget_clip_return(rtgui_widget_t *widget);
  142. /* move widget and its children to a logic point */
  143. void rtgui_widget_move_to_logic(rtgui_widget_t *widget, int dx, int dy);
  144. /* update the clip info of widget */
  145. void rtgui_widget_update_clip(rtgui_widget_t *widget);
  146. /* get the toplevel widget of widget */
  147. struct rtgui_win *rtgui_widget_get_toplevel(rtgui_widget_t *widget);
  148. rt_bool_t rtgui_widget_onupdate_toplvl(struct rtgui_object *object, struct rtgui_event *event);
  149. void rtgui_widget_show(rtgui_widget_t *widget);
  150. rt_bool_t rtgui_widget_onshow(struct rtgui_object *object, struct rtgui_event *event);
  151. void rtgui_widget_hide(rtgui_widget_t *widget);
  152. rt_bool_t rtgui_widget_onhide(struct rtgui_object *object, struct rtgui_event *event);
  153. void rtgui_widget_update(rtgui_widget_t *widget);
  154. rt_bool_t rtgui_widget_onpaint(struct rtgui_object *object, struct rtgui_event *event);
  155. /* get parent color */
  156. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t *widget);
  157. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t *widget);
  158. /* get the next sibling of widget */
  159. rtgui_widget_t *rtgui_widget_get_next_sibling(rtgui_widget_t *widget);
  160. /* get the prev sibling of widget */
  161. rtgui_widget_t *rtgui_widget_get_prev_sibling(rtgui_widget_t *widget);
  162. rt_bool_t rtgui_widget_is_in_animation(rtgui_widget_t *widget);
  163. /* dump widget information */
  164. void rtgui_widget_dump(rtgui_widget_t *widget);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif