aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/Control.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-02-09 23:53:18 +0800
committercrupest <crupest@outlook.com>2022-02-09 23:53:18 +0800
commitb8863c403a44c1c7ac35f1a1da92bbf3c8858552 (patch)
tree7e38f029f0657e6c1210a53f1cba331cdb8feab6 /src/ui/controls/Control.cpp
parentd18b5453d7ffd19667ee8ac125b34ab5328f0dc3 (diff)
downloadcru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.tar.gz
cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.tar.bz2
cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.zip
...
Diffstat (limited to 'src/ui/controls/Control.cpp')
-rw-r--r--src/ui/controls/Control.cpp93
1 files changed, 11 insertions, 82 deletions
diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp
index 36d5cd60..df71f660 100644
--- a/src/ui/controls/Control.cpp
+++ b/src/ui/controls/Control.cpp
@@ -31,31 +31,21 @@ Control::Control() {
});
}
-Control::~Control() {
- if (auto_delete_children_) {
- for (const auto child : children_) {
- delete child;
- }
- }
-}
-
-host::WindowHost* Control::GetWindowHost() const { return window_host_; }
+Control::~Control() {}
-Index Control::IndexOf(Control* child) const {
- for (Index i = 0; i < children_.size(); ++i) {
- if (children_[i] == child) return i;
+host::WindowHost* Control::GetWindowHost() const {
+ auto parent = GetParent();
+ if (parent) {
+ return parent->GetWindowHost();
}
- return -1;
+ return nullptr;
}
-void Control::TraverseDescendants(
- const std::function<void(Control*)>& predicate) {
- predicate(this);
- for (auto c : GetChildren()) c->TraverseDescendants(predicate);
-}
-
-void Control::RemoveFromParent() {
- if (parent_) parent_->RemoveChild(parent_->IndexOf(this));
+void Control::SetParent(Control* parent) {
+ if (parent_ == parent) return;
+ auto old_parent = parent_;
+ parent_ = parent;
+ OnParentChanged(old_parent, parent);
}
bool Control::HasFocus() {
@@ -117,65 +107,4 @@ void Control::SetCursor(std::shared_ptr<ICursor> cursor) {
std::shared_ptr<style::StyleRuleSet> Control::GetStyleRuleSet() {
return style_rule_set_;
}
-
-void Control::AddChild(Control* control, const Index position) {
- Expects(control->GetParent() ==
- nullptr); // The control already has a parent.
- Expects(position >= 0);
- Expects(position <= static_cast<Index>(
- children_.size())); // The position is out of range.
-
- children_.insert(children_.cbegin() + position, control);
-
- const auto old_parent = control->parent_;
- control->parent_ = this;
-
- OnAddChild(control, position);
- control->OnParentChanged(old_parent, this);
-
- if (window_host_)
- control->TraverseDescendants([this](Control* control) {
- control->window_host_ = window_host_;
- control->OnAttachToHost(window_host_);
- });
-}
-
-void Control::RemoveChild(const Index position) {
- Expects(position >= 0);
- Expects(position < static_cast<Index>(
- children_.size())); // The position is out of range.
-
- const auto i = children_.cbegin() + position;
- const auto control = *i;
-
- children_.erase(i);
- control->parent_ = nullptr;
-
- OnRemoveChild(control, position);
- control->OnParentChanged(this, nullptr);
-
- if (window_host_)
- control->TraverseDescendants([this](Control* control) {
- control->window_host_ = nullptr;
- control->OnDetachFromHost(window_host_);
- });
-}
-
-void Control::OnAddChild(Control* child, Index position) {
- CRU_UNUSED(child)
- CRU_UNUSED(position)
-}
-void Control::OnRemoveChild(Control* child, Index position) {
- CRU_UNUSED(child)
- CRU_UNUSED(position)
-}
-
-void Control::OnParentChanged(Control* old_parent, Control* new_parent) {
- CRU_UNUSED(old_parent)
- CRU_UNUSED(new_parent)
-}
-
-void Control::OnAttachToHost(host::WindowHost* host) { CRU_UNUSED(host) }
-
-void Control::OnDetachFromHost(host::WindowHost* host) { CRU_UNUSED(host) }
} // namespace cru::ui::controls