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
299
300
301
302
303
304
305
306
307
308
|
#pragma once
#include "system_headers.h"
#include <optional>
#include <unordered_map>
#include "base.h"
#include "ui_base.h"
#include "layout_base.h"
#include "events/ui_event.h"
namespace cru
{
namespace ui
{
class Control;
class Window;
//the position cache
struct ControlPositionCache
{
//The lefttop relative to the ancestor.
Point lefttop_position_absolute;
};
class Control : public Object
{
friend class Window;
friend class LayoutManager;
protected:
struct WindowConstructorTag {}; //Used for constructor for class Window.
explicit Control(bool container = false);
// Used only for creating Window. It will set window_ as window.
Control(WindowConstructorTag, Window* window);
public:
Control(const Control& other) = delete;
Control(Control&& other) = delete;
Control& operator=(const Control& other) = delete;
Control& operator=(Control&& other) = delete;
~Control() override;
public:
//*************** region: tree ***************
bool IsContainer() const
{
return is_container_;
}
//Get parent of control, return nullptr if it has no parent.
Control* GetParent() const
{
return parent_;
}
//Traverse the children
void ForeachChild(Function<void(Control*)>&& predicate) const;
void ForeachChild(Function<FlowControl(Control*)>&& predicate) const;
//Return a vector of all children. This function will create a
//temporary copy of vector of children. If you just want to
//traverse all children, just call ForeachChild.
Vector<Control*> GetChildren() const
{
return children_;
}
//Add a child at tail.
void AddChild(Control* control);
//Add a child before the position.
void AddChild(Control* control, int position);
//Remove a child.
void RemoveChild(Control* child);
//Remove a child at specified position.
void RemoveChild(int position);
//Get the ancestor of the control.
Control* GetAncestor();
//Get the window if attached, otherwise, return nullptr.
Window* GetWindow() const
{
return window_;
}
//Traverse the tree rooted the control including itself.
void TraverseDescendants(Function<void(Control*)>&& predicate);
//*************** region: position and size ***************
// Position and size part must be isolated from layout part.
// All the operations in this part must be done independently.
// And layout part must use api of this part.
//Get the lefttop relative to its parent.
virtual Point GetPositionRelative();
//Set the lefttop relative to its parent.
virtual void SetPositionRelative(const Point& position);
//Get the actual size.
virtual Size GetSize();
//Set the actual size directly without re-layout.
virtual void SetSize(const Size& size);
//Get lefttop relative to ancestor. This is only valid when
//attached to window. Notice that the value is cached.
//You can invalidate and recalculate it by calling "InvalidatePositionCache".
Point GetPositionAbsolute() const;
//Local point to absolute point.
Point LocalToAbsolute(const Point& point) const;
//Absolute point to local point.
Point AbsoluteToLocal(const Point& point) const;
virtual bool IsPointInside(const Point& point);
//*************** region: graphic ***************
//Draw this control and its child controls.
void Draw(ID2D1DeviceContext* device_context);
virtual void Repaint();
//*************** region: focus ***************
bool RequestFocus();
bool HasFocus();
//*************** region: layout ***************
void Relayout();
void Measure(const Size& available_size);
void Layout(const Rect& rect);
Size GetDesiredSize() const;
void SetDesiredSize(const Size& desired_size);
template<typename TLayoutParams = BasicLayoutParams>
std::shared_ptr<TLayoutParams> GetLayoutParams()
{
static_assert(std::is_base_of_v<BasicLayoutParams, TLayoutParams>, "TLayoutParams must be subclass of BasicLayoutParams.");
return static_cast<std::shared_ptr<BasicLayoutParams>>(layout_params_);
}
template<typename TLayoutParams = BasicLayoutParams,
typename = std::enable_if_t<std::is_base_of_v<BasicLayoutParams, TLayoutParams>>>
void SetLayoutParams(std::shared_ptr<TLayoutParams> basic_layout_params)
{
static_assert(std::is_base_of_v<BasicLayoutParams, TLayoutParams>, "TLayoutParams must be subclass of BasicLayoutParams.");
layout_params_ = basic_layout_params;
}
//*************** region: events ***************
//Raised when mouse enter the control.
events::MouseEvent mouse_enter_event;
//Raised when mouse is leave the control.
events::MouseEvent mouse_leave_event;
//Raised when mouse is move in the control.
events::MouseEvent mouse_move_event;
//Raised when a mouse button is pressed in the control.
events::MouseButtonEvent mouse_down_event;
//Raised when a mouse button is released in the control.
events::MouseButtonEvent mouse_up_event;
//Raised when a mouse button is pressed in the control and released in the control with mouse not leaving it between two operations.
events::MouseButtonEvent mouse_click_event;
events::FocusChangeEvent get_focus_event;
events::FocusChangeEvent lose_focus_event;
events::DrawEvent draw_event;
events::PositionChangedEvent position_changed_event;
events::SizeChangedEvent size_changed_event;
protected:
//Invoked when a child is added. Overrides should invoke base.
virtual void OnAddChild(Control* child);
//Invoked when a child is removed. Overrides should invoke base.
virtual void OnRemoveChild(Control* child);
//Invoked when the control is attached to a window. Overrides should invoke base.
virtual void OnAttachToWindow(Window* window);
//Invoked when the control is detached to a window. Overrides should invoke base.
virtual void OnDetachToWindow(Window* window);
virtual void OnDraw(ID2D1DeviceContext* device_context);
// For a event, the window event system will first dispatch event to core functions.
// Therefore for particular controls, you should do essential actions in core functions,
// and override version should invoke base version. The base core function
// in "Control" class will call corresponding non-core function and call "Raise" on
// event objects. So user custom actions should be done by overriding non-core function
// and calling the base version is optional.
//*************** region: position and size event ***************
virtual void OnPositionChanged(events::PositionChangedEventArgs& args);
virtual void OnSizeChanged(events::SizeChangedEventArgs& args);
virtual void OnPositionChangedCore(events::PositionChangedEventArgs& args);
virtual void OnSizeChangedCore(events::SizeChangedEventArgs& args);
void OnPositionChangedInternal(events::PositionChangedEventArgs& args);
void OnSizeChangedInternal(events::SizeChangedEventArgs& args);
//*************** region: mouse event ***************
virtual void OnMouseEnter(events::MouseEventArgs& args);
virtual void OnMouseLeave(events::MouseEventArgs& args);
virtual void OnMouseMove(events::MouseEventArgs& args);
virtual void OnMouseDown(events::MouseButtonEventArgs& args);
virtual void OnMouseUp(events::MouseButtonEventArgs& args);
virtual void OnMouseClick(events::MouseButtonEventArgs& args);
virtual void OnMouseEnterCore(events::MouseEventArgs& args);
virtual void OnMouseLeaveCore(events::MouseEventArgs& args);
virtual void OnMouseMoveCore(events::MouseEventArgs& args);
virtual void OnMouseDownCore(events::MouseButtonEventArgs& args);
virtual void OnMouseUpCore(events::MouseButtonEventArgs& args);
virtual void OnMouseClickCore(events::MouseButtonEventArgs& args);
void OnMouseEnterInternal(events::MouseEventArgs& args);
void OnMouseLeaveInternal(events::MouseEventArgs& args);
void OnMouseMoveInternal(events::MouseEventArgs& args);
void OnMouseDownInternal(events::MouseButtonEventArgs& args);
void OnMouseUpInternal(events::MouseButtonEventArgs& args);
void OnMouseClickInternal(events::MouseButtonEventArgs& args);
//*************** region: focus event ***************
virtual void OnGetFocus(events::FocusChangeEventArgs& args);
virtual void OnLoseFocus(events::FocusChangeEventArgs& args);
virtual void OnGetFocusCore(events::FocusChangeEventArgs& args);
virtual void OnLoseFocusCore(events::FocusChangeEventArgs& args);
void OnGetFocusInternal(events::FocusChangeEventArgs& args);
void OnLoseFocusInternal(events::FocusChangeEventArgs& args);
//*************** region: layout ***************
virtual Size OnMeasure(const Size& available_size);
virtual void OnLayout(const Rect& rect);
private:
// Only for layout manager to use.
// Check if the old position is updated to current position.
// If not, then a notify of position change and update will
// be done.
void CheckAndNotifyPositionChanged();
void ThrowIfNotContainer() const
{
if (!is_container_)
throw std::runtime_error("You can't perform such operation on a non-container control.");
}
private:
bool is_container_;
protected:
Window * window_; // protected for Window class to write it as itself in constructor.
private:
Control * parent_;
Vector<Control*> children_;
// When position is changed and notification hasn't been
// sent, it will be the old position. When position is changed
// more than once, it will be the oldest position since last
// notification. If notification has been sent, it will be updated
// to position_.
Point old_position_;
Point position_;
Size size_;
ControlPositionCache position_cache_;
bool is_mouse_inside_;
std::unordered_map<MouseButton, bool> is_mouse_leave_; // used for clicking determination
std::shared_ptr<BasicLayoutParams> layout_params_;
Size desired_size_;
};
// Find the lowest common ancestor.
// Return nullptr if "left" and "right" are not in the same tree.
Control* FindLowestCommonAncestor(Control* left, Control* right);
// Return the ancestor if one control is the ancestor of the other one, otherwise nullptr.
Control* IsAncestorOrDescendant(Control* left, Control* right);
}
}
|