From b8863c403a44c1c7ac35f1a1da92bbf3c8858552 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 9 Feb 2022 23:53:18 +0800 Subject: ... --- src/ui/controls/Control.cpp | 93 ++++++--------------------------------------- 1 file changed, 11 insertions(+), 82 deletions(-) (limited to 'src/ui/controls/Control.cpp') 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& 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 cursor) { std::shared_ptr 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( - 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( - 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 -- cgit v1.2.3