pm_widget.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * File : pm_widget.h
  3. * COPYRIGHT (C) 2012-2017, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-05 realthread the first version
  8. */
  9. #pragma once
  10. #include <rtgui/rtgui.h>
  11. #include <rtgui/widgets/widget.h>
  12. #include <gesture.h>
  13. #include <persimmon.h>
  14. #include <pm_rect.h>
  15. #include <pm_image.h>
  16. namespace Persimmon
  17. {
  18. namespace utils
  19. {
  20. /* Borrowed from Boost::noncopyable. Add the template to give each noncopyable
  21. * a different signature. */
  22. template <typename T>
  23. class noncopyable
  24. {
  25. protected:
  26. noncopyable() {}
  27. ~noncopyable() {}
  28. private: // emphasize the following members are private
  29. noncopyable( const noncopyable& );
  30. noncopyable& operator=( const noncopyable& );
  31. };
  32. #define DEFINE_CLASS_ENUM_FLAG_OPERATORS(ENUMTYPE) \
  33. friend inline ENUMTYPE operator | (ENUMTYPE a, ENUMTYPE b) \
  34. { return ENUMTYPE(((int)a)|((int)b)); } \
  35. friend inline ENUMTYPE operator |= (ENUMTYPE &a, ENUMTYPE b) \
  36. { return (ENUMTYPE &)(((int &)a) |= ((int)b)); } \
  37. friend inline ENUMTYPE operator & (ENUMTYPE a, ENUMTYPE b) \
  38. { return ENUMTYPE(((int)a)&((int)b)); } \
  39. friend inline ENUMTYPE operator &= (ENUMTYPE &a, ENUMTYPE b) \
  40. { return (ENUMTYPE &)(((int &)a) &= ((int)b)); } \
  41. friend inline ENUMTYPE operator ~ (ENUMTYPE a) \
  42. { return (ENUMTYPE)(~((int)a)); } \
  43. friend inline ENUMTYPE operator ^ (ENUMTYPE a, ENUMTYPE b) \
  44. { return ENUMTYPE(((int)a)^((int)b)); } \
  45. friend inline ENUMTYPE operator ^= (ENUMTYPE &a, ENUMTYPE b) \
  46. { return (ENUMTYPE &)(((int &)a) ^= ((int)b)); }
  47. #define DEFINE_CLASS_ENUM_FLAG_OPERATORS2(ENUMTYPE) \
  48. inline ENUMTYPE operator | (ENUMTYPE a, ENUMTYPE b) \
  49. { return ENUMTYPE(((int)a)|((int)b)); } \
  50. inline ENUMTYPE operator |= (ENUMTYPE &a, ENUMTYPE b) \
  51. { return (ENUMTYPE &)(((int &)a) |= ((int)b)); } \
  52. inline ENUMTYPE operator & (ENUMTYPE a, ENUMTYPE b) \
  53. { return ENUMTYPE(((int)a)&((int)b)); } \
  54. inline ENUMTYPE operator &= (ENUMTYPE &a, ENUMTYPE b) \
  55. { return (ENUMTYPE &)(((int &)a) &= ((int)b)); } \
  56. inline ENUMTYPE operator ~ (ENUMTYPE a) \
  57. { return (ENUMTYPE)(~((int)a)); } \
  58. inline ENUMTYPE operator ^ (ENUMTYPE a, ENUMTYPE b) \
  59. { return ENUMTYPE(((int)a)^((int)b)); } \
  60. inline ENUMTYPE operator ^= (ENUMTYPE &a, ENUMTYPE b) \
  61. { return (ENUMTYPE &)(((int &)a) ^= ((int)b)); }
  62. }
  63. class Container;
  64. class Window;
  65. /**
  66. * Persimmon UI Widget
  67. */
  68. class Widget: private utils::noncopyable<Widget>
  69. {
  70. public:
  71. Widget();
  72. Widget(const rtgui_type_t *widget_type);
  73. virtual ~Widget();
  74. Widget *getParent(void)
  75. {
  76. if (widget->parent) return (Widget*)(widget->parent->user_data);
  77. return NULL;
  78. }
  79. Widget* setFont(struct rtgui_font* font)
  80. {
  81. RTGUI_WIDGET_FONT(this->widget) = font;
  82. return this;
  83. }
  84. struct rtgui_font* getFont(void)
  85. {
  86. return RTGUI_WIDGET_FONT(this->widget);
  87. }
  88. Widget* setBackground(rtgui_color_t color)
  89. {
  90. RTGUI_WIDGET_BACKGROUND(widget) = color;
  91. /* check whether has alpha channel */
  92. if (RTGUI_RGB_A(color) != 255)
  93. {
  94. widget->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT;
  95. rtgui_widget_clip_return(widget);
  96. }
  97. else
  98. {
  99. if (backgroundDc)
  100. rtgui_dc_destory(backgroundDc);
  101. backgroundDc = RT_NULL;
  102. if (widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)
  103. {
  104. widget->flag &= ~RTGUI_WIDGET_FLAG_TRANSPARENT;
  105. rtgui_widget_clip_parent(widget);
  106. }
  107. else
  108. {
  109. widget->flag &= ~RTGUI_WIDGET_FLAG_TRANSPARENT;
  110. }
  111. }
  112. return this;
  113. }
  114. Widget* setBackground(Image *image)
  115. {
  116. if (backgroundDc)
  117. rtgui_dc_destory(backgroundDc);
  118. backgroundDc = RT_NULL;
  119. if (image)
  120. {
  121. backgroundDc = rtgui_dc_buffer_create(image->getWidth(), image->getHeight());
  122. if (backgroundDc)
  123. rtgui_image_blit(image->getImage(), backgroundDc, RT_NULL);
  124. /* release this image */
  125. delete image;
  126. }
  127. return this;
  128. }
  129. Widget* setTransparentBackground(Image *image)
  130. {
  131. if (backgroundDc)
  132. rtgui_dc_destory(backgroundDc);
  133. backgroundDc = RT_NULL;
  134. if (image)
  135. {
  136. backgroundDc = rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888, image->getWidth(), image->getHeight());
  137. if (backgroundDc)
  138. rtgui_image_blit(image->getImage(), backgroundDc, RT_NULL);
  139. /* release this image */
  140. delete image;
  141. }
  142. return this;
  143. }
  144. Widget* setBackground(struct rtgui_dc* bg)
  145. {
  146. if (backgroundDc)
  147. rtgui_dc_destory(backgroundDc);
  148. backgroundDc = bg;
  149. return this;
  150. }
  151. Widget* setForeground(rtgui_color_t color)
  152. {
  153. RTGUI_WIDGET_FOREGROUND(this->widget) = color;
  154. return this;
  155. }
  156. Widget* setTextAlign(int align)
  157. {
  158. RTGUI_WIDGET_TEXTALIGN(widget) = align;
  159. return this;
  160. }
  161. struct rtgui_widget* getWidget(void)
  162. {
  163. return RTGUI_WIDGET(this->widget);
  164. }
  165. Rect getRect(void) const;
  166. void getRect(struct rtgui_rect* rect) const;
  167. void setRect(struct rtgui_rect* rect);
  168. virtual void setRect(const Rect &rect);
  169. Point getPosition(void) const;
  170. Size getSize(void) const;
  171. void setSize(const Size &size);
  172. Rect getExtent(void) const;
  173. void getExtent(struct rtgui_rect* rect) const;
  174. Widget* getVisiableParent(void);
  175. bool isInAnimation(void)
  176. {
  177. return (rtgui_widget_is_in_animation(getWidget()) == RT_TRUE) ? true:false ;
  178. }
  179. bool isTransparent(void)
  180. {
  181. return (widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) ? true:false;
  182. }
  183. void refresh(bool update = true);
  184. void freeze(void);
  185. void thaw(void);
  186. void setLastFocus(bool focus);
  187. void move(int x, int y);
  188. void moveTo(int x, int y);
  189. virtual void render(struct rtgui_dc* dc, const Point &dcPoint = Point(),
  190. const Rect &srcRect = Rect(), RenderFlag flags = DrawNormal);
  191. virtual void renderBackground(struct rtgui_dc* dc, const Point &dcPoint = Point(),
  192. const Rect &srcRect = Rect(), RenderFlag flags = DrawNormal);
  193. struct rtgui_dc* getBufferDrawing(rt_uint8_t pixel_format, RenderFlag flags = DrawNormal);
  194. struct rtgui_dc* getBufferDrawing(RenderFlag flags = DrawNormal);
  195. void hide()
  196. {
  197. rtgui_widget_hide(widget);
  198. }
  199. virtual rt_bool_t onHide(struct rtgui_event *event);
  200. bool isHide()
  201. {
  202. return RTGUI_WIDGET_IS_HIDE(widget);
  203. }
  204. void show()
  205. {
  206. rtgui_widget_show(widget);
  207. }
  208. virtual rt_bool_t onShow(struct rtgui_event *event);
  209. virtual rt_bool_t eventHandler(struct rtgui_event *event);
  210. virtual Widget* getMouseOwner(int x, int y);
  211. void setInterestGesture(enum rtgui_gesture_type tp)
  212. {
  213. gest_tp = tp;
  214. }
  215. enum rtgui_gesture_type getInterestGesture();
  216. virtual bool handleGestureEvent(struct rtgui_event_gesture *gev,
  217. const struct rtgui_gesture *gest);
  218. Widget* getParent() const
  219. {
  220. return parent;
  221. }
  222. void setParent(Widget *w)
  223. {
  224. parent = w;
  225. rtgui_widget_set_parent(widget, w ? w->widget : NULL);
  226. }
  227. virtual void setGestureRect(const Rect &rect, bool enable = true)
  228. {
  229. gestureRect = rect;
  230. enableGestureRect = enable;
  231. }
  232. void renderFloating(struct rtgui_dc *dc);
  233. protected:
  234. static void renderParentBackground(Widget* widget, Widget* parent, struct rtgui_dc* dc, const Point &dcPoint, const Rect &srcRect);
  235. struct rtgui_widget* widget;
  236. struct rtgui_dc *backgroundDc;
  237. private:
  238. /* Set by Container::addChild. */
  239. Widget *parent;
  240. bool enableGestureRect;
  241. Rect gestureRect;
  242. friend class Window;
  243. enum rtgui_gesture_type gest_tp;
  244. };
  245. }
  246. DEFINE_CLASS_ENUM_FLAG_OPERATORS2(rtgui_gesture_type);