-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget.h
298 lines (255 loc) · 7.12 KB
/
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
PPPlay - an old-fashioned module player
Copyright (C) 2010 Steffen Ohrendorf <steffen.ohrendorf@gmx.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PPG_WIDGET_H
#define PPG_WIDGET_H
/**
* @ingroup Ppg
* @{
*/
#include <stuff/utils.h>
#include "rect.h"
#include "color.h"
#include <list>
#include <mutex>
namespace ppg
{
/**
* @class Widget
* @brief The abstract base class for all PeePeeGUI classes
* @details
* Every PeePeeGUI class is derived from this class.
*
* If you create widgets on the stack (so to speak @e not by using @c operator @c new), make
* sure to call @code Widget::setAutoDelete(false); @endcode
*
* Every widget "owns" its children, so that it @c deletes them when it gets destructed. You may prevent
* auto-deletion by the procedure described above.
*/
class Widget
{
public:
DISABLE_COPY( Widget )
typedef std::list<Widget*> List; //!< @brief List of widgets
private:
mutable std::recursive_mutex m_mutex; //!< @brief Widget mutex
bool m_visible; //!< @brief @c false if this widget and it's children should not be drawn
Widget* m_parent; //!< @brief Pointer to the parent widget, or @c nullptr if it's the top widget
Rect m_area; //!< @brief Area of this widget within the parent's space
List m_children; //!< @brief Children within this widget
bool m_autodelete; //!< @brief If @c true, the parent widget should delete this when destroyed. Default is @c true
virtual void drawThis() = 0; //!< @brief Internal drawing method, called by PppWidet::draw() @see draw()
public:
class LockGuard;
/**
* @brief Constructor
* @param[in] parent Parent widget
*/
explicit Widget(Widget* parent);
/**
* @brief Destructor. Deletes all children.
*/
virtual ~Widget();
/**
* @brief Calls drawThis(), but only when m_visible is @c true
*/
void draw();
/**
* @brief Make this widget visible
*/
void show() noexcept;
/**
* @brief Make this widget invisible
*/
void hide() noexcept;
/**
* @brief Draw a char relative to the widget's position
* @param[in] x Left position
* @param[in] y Top position
* @param[in] c The char to draw
*/
virtual void drawChar(int x, int y, char c);
/**
* @brief Is this widget visible?
* @return m_visible
*/
bool isVisible() const noexcept;
/**
* @brief Sets the foreground color relative to the widget's position
* @param[in] x Left position
* @param[in] y Top position
* @param[in] c Dos Color Value
*/
virtual void setFgColorAt(int x, int y, Color c);
/**
* @brief Sets the background color relative to the widget's position
* @param[in] x Left position
* @param[in] y Top position
* @param[in] c Dos Color Value
*/
virtual void setBgColorAt(int x, int y, Color c);
/**
* @brief Sets the widget's left position
* @param[in] x Left position
* @param[in] absolute Set to @c true to calculate the position relative to the top parent widget
* @return New left position
*/
virtual int setLeft(int x, bool absolute);
/**
* @brief Sets the widget's top position
* @param[in] y Top position
* @param[in] absolute Set to @c true to calculate the position relative to the top parent widget
* @return New top position
*/
virtual int setTop(int y, bool absolute);
/**
* @brief Sets the widget's position
* @param[in] x Left position
* @param[in] y Top position
* @param[in] absolute Set to @c true to calculate the position relative to the top parent widget
* @return @c false if the position did not change
*/
virtual bool setPosition(int x, int y, bool absolute);
/**
* @overload
* @param[in] pos Position
* @param[in] absolute Set to @c true to calculate the position relative to the top parent widget
* @return @c false if the position did not change
*/
virtual bool setPosition(const Point& pos, bool absolute);
/**
* @brief Sets the widget's width
* @param[in] w Wanted width
* @return New width
* @pre @c w>0
*/
virtual int setWidth(int w);
/**
* @brief Sets the widget's height
* @param[in] h Wanted height
* @return New height
* @pre @c h>0
*/
virtual int setHeight(int h);
/**
* @brief Sets the widget's size
* @param[in] w Width
* @param[in] h Height
* @return @c false if nothing changed
* @pre @c w>0
* @pre @c h>0
*/
virtual bool setSize(int w, int h);
/**
* @overload
* @param[in] pt New size
* @return @c false if nothing changed
*/
virtual bool setSize(const Point& pt);
/**
* @brief Get the widget's area
* @return The widget's area
*/
Rect area() const noexcept;
/**
* @overload
* @return The widget's area
*/
Rect& area() noexcept;
/**
* @brief Get the top parent
* @return Pointer to the top parent
*/
virtual Widget* getTopParent() const;
/**
* @brief Map widget's coordinates to the parent ones
* @param[in,out] x Left coordinate
* @param[in,out] y Top coordinate
*/
virtual void mapToParent(int* x, int* y) const;
/**
* @overload
* @param[in] pt Coordinate
*/
virtual void mapToParent(Point* pt) const;
/**
* @brief Map widget's coordinates to the top parent ones
* @param[in,out] x Left coordinate
* @param[in,out] y Top coordinate
*/
virtual void mapToAbsolute(int* x, int* y) const;
/**
* @overload
* @param[in] pt Coordinate
*/
virtual void mapToAbsolute(Point* pt) const;
/**
* @brief Move an element to the top
* @param[in] vp Pointer to the element to move to the top
*/
virtual void toTop(Widget* vp);
/**
* @brief Mouse movement event handler
* @param[in] x X coordinate
* @param[in] y Y coordinate
* @return @c true if the event was handled
* @note Coordinates are relative to the parent's ones
* @details
* The default implementation calls the children's event handlers
*/
virtual bool onMouseMove(int x, int y);
/**
* @brief Sets the value of m_autoDelete
* @param[in] value The new value
*/
void setAutoDelete(bool value) noexcept;
};
class Widget::LockGuard final
{
private:
const Widget* const m_widget;
public:
DISABLE_COPY( LockGuard )
explicit LockGuard(const Widget* const widget)
: m_widget( widget )
{
if( widget != nullptr )
{
widget->m_mutex.lock();
}
}
~LockGuard()
{
unlock();
}
void lock()
{
if( m_widget != nullptr )
{
m_widget->m_mutex.lock();
}
}
void unlock()
{
if( m_widget != nullptr )
{
m_widget->m_mutex.unlock();
}
}
};
} // namespace ppg
/**
* @}
*/
#endif // ppgwidgetH