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
|
#include "cru/ui/controls/Control.h"
#include "cru/base/Base.h"
#include "cru/base/log/Logger.h"
#include "cru/platform/gui/Cursor.h"
#include "cru/platform/gui/UiApplication.h"
#include "cru/ui/controls/ControlHost.h"
#include "cru/ui/style/StyleRuleSet.h"
#include <algorithm>
#include <format>
namespace cru::ui::controls {
using platform::gui::ICursor;
using platform::gui::IUiApplication;
using platform::gui::SystemCursorType;
Control::Control(std::string name)
: name_(std::move(name)), host_(nullptr), parent_(nullptr) {
style_rule_set_ = std::make_shared<style::StyleRuleSet>();
style_rule_set_bind_ =
std::make_unique<style::StyleRuleSetBind>(this, style_rule_set_);
}
Control::~Control() {
if (host_ && host_->IsInEventHandling()) {
CRU_LOG_TAG_WARN(
"Better use delete later to delete control during event handling.");
}
RemoveFromParent();
RemoveAllChild();
}
std::string Control::GetName() { return name_; }
std::string Control::GetDebugId() {
return std::format("{}({})", GetName(), static_cast<const void*>(this));
}
ControlHost* Control::GetControlHost() { return host_; }
Control* Control::GetParent() { return parent_; }
bool Control::HasAncestor(Control* control) {
auto parent = this;
while (parent) {
if (parent == control) return true;
parent = parent->GetParent();
}
return false;
}
const std::vector<Control*>& Control::GetChildren() { return children_; }
Index Control::IndexOfChild(Control* control) {
const auto& children = GetChildren();
auto iter = std::ranges::find(children, control);
if (iter == children.cend()) {
return -1;
}
return iter - children.begin();
}
void Control::RemoveChild(Control* child) {
auto iter = std::ranges::find(children_, child);
if (iter != children_.cend()) {
RemoveChildAt(iter - children_.cbegin());
}
}
void Control::RemoveAllChild() {
while (!GetChildren().empty()) {
RemoveChildAt(GetChildren().size() - 1);
}
}
void Control::RemoveFromParent() {
if (parent_) {
parent_->RemoveChild(this);
}
}
controls::Control* Control::HitTest(const Point& point) {
const auto render_object = GetRenderObject()->HitTest(point);
if (render_object) {
const auto control = render_object->GetAttachedControl();
assert(control);
return control;
}
return nullptr;
}
void Control::InsertChildAt(Control* control, Index index) {
if (index < 0 || index > children_.size()) {
throw Exception("Child control index out of range.");
}
if (control->parent_) {
throw Exception("Control already has a parent.");
}
children_.insert(children_.cbegin() + index, control);
control->parent_ = this;
control->TraverseDescendents(
[this](Control* control) { control->host_ = host_; }, true);
if (host_) {
host_->NotifyControlParentChange(control, nullptr, this);
}
control->OnParentChanged(nullptr, this);
OnChildInserted(control, index);
if (host_) {
host_->ScheduleRelayout();
}
}
void Control::RemoveChildAt(Index index) {
if (index < 0 || index >= children_.size()) {
throw Exception("Child control index out of range.");
}
auto control = children_[index];
children_.erase(children_.cbegin() + index);
control->parent_ = nullptr;
control->TraverseDescendents(
[this](Control* control) { control->host_ = nullptr; }, true);
if (host_) {
host_->NotifyControlParentChange(control, this, nullptr);
}
control->OnParentChanged(this, nullptr);
OnChildRemoved(control, index);
if (host_) {
host_->ScheduleRelayout();
}
}
void Control::AddChild(Control* control) {
InsertChildAt(control, GetChildren().size());
}
bool Control::HasFocus() {
if (!host_) return false;
return host_->GetFocusControl() == this;
}
void Control::SetFocus() {
if (!host_) return;
host_->SetFocusControl(this);
}
bool Control::IsMouseOver() {
if (!host_) return false;
return host_->GetMouseHoverControl() == this;
}
bool Control::CaptureMouse() {
if (!host_) return false;
return host_->SetMouseCaptureControl(this);
}
bool Control::ReleaseMouse() {
if (!host_) return false;
if (!IsMouseCaptured()) return false;
return host_->SetMouseCaptureControl(nullptr);
}
bool Control::IsMouseCaptured() {
if (!host_) return false;
return host_->GetMouseCaptureControl() == this;
}
std::shared_ptr<ICursor> Control::GetCursor() { return cursor_; }
std::shared_ptr<ICursor> Control::GetInheritedCursor() {
Control* control = this;
while (control != nullptr) {
const auto cursor = control->GetCursor();
if (cursor != nullptr) return cursor;
control = control->GetParent();
}
return IUiApplication::GetInstance()->GetCursorManager()->GetSystemCursor(
SystemCursorType::Arrow);
}
void Control::SetCursor(std::shared_ptr<ICursor> cursor) {
cursor_ = std::move(cursor);
if (host_) {
host_->UpdateCursor();
}
}
std::shared_ptr<style::StyleRuleSet> Control::GetStyleRuleSet() {
return style_rule_set_;
}
void Control::OnParentChanged(Control* old_parent, Control* new_parent) {}
void Control::OnChildInserted(Control* control, Index index) {}
void Control::OnChildRemoved(Control* control, Index index) {}
} // namespace cru::ui::controls
|