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