diff options
author | crupest <crupest@outlook.com> | 2019-03-21 22:22:02 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-03-21 22:22:02 +0800 |
commit | 962dc18ee4827b464764ec3708be3d00a9143971 (patch) | |
tree | 3eea13894a14a26f2e2de07d3d6bc7789d409864 /src/ui/control.cpp | |
parent | afa2a079562d810e5ef611071b21a3568da9dfca (diff) | |
download | cru-962dc18ee4827b464764ec3708be3d00a9143971.tar.gz cru-962dc18ee4827b464764ec3708be3d00a9143971.tar.bz2 cru-962dc18ee4827b464764ec3708be3d00a9143971.zip |
...
Diffstat (limited to 'src/ui/control.cpp')
-rw-r--r-- | src/ui/control.cpp | 110 |
1 files changed, 3 insertions, 107 deletions
diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 98986d3c..e19754dc 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -35,14 +35,14 @@ void Control::_SetDescendantWindow(Window* window) { void Control::TraverseDescendants( const std::function<void(Control*)>& predicate) { - TraverseDescendantsInternal(this, predicate); + _TraverseDescendants(this, predicate); } -void Control::TraverseDescendantsInternal( +void Control::_TraverseDescendants( Control* control, const std::function<void(Control*)>& predicate) { predicate(control); for (auto c : control->GetChildren()) - TraverseDescendantsInternal(c, predicate); + _TraverseDescendants(c, predicate); } bool Control::RequestFocus() { auto window = GetWindow(); @@ -76,108 +76,4 @@ void Control::OnDetachToWindow(Window* window) {} void Control::OnMouseClickBegin(MouseButton button) {} void Control::OnMouseClickEnd(MouseButton button) {} - -const std::vector<Control*> NoChildControl::empty_control_vector{}; - -ContentControl::ContentControl() - : child_vector_{nullptr}, child_(child_vector_[0]) {} - -ContentControl::~ContentControl() { delete child_; } - -void ContentControl::SetChild(Control* child) { - if (child == child_) return; - - const auto window = GetWindow(); - const auto old_child = child_; - child_ = child; - if (old_child) { - old_child->_SetParent(nullptr); - old_child->_SetDescendantWindow(nullptr); - } - if (child) { - child->_SetParent(this); - child->_SetDescendantWindow(window); - } - OnChildChanged(old_child, child); -} - -void ContentControl::OnChildChanged(Control* old_child, Control* new_child) {} - -void ControlAddChildCheck(Control* control) { - if (control->GetParent() != nullptr) - throw std::invalid_argument("The control already has a parent."); - - if (dynamic_cast<Window*>(control)) - throw std::invalid_argument("Can't add a window as child."); -} - -Layout::~Layout() { - for (const auto child : children_) delete child; -} - -void Layout::AddChild(Control* control, const int position) { - ControlAddChildCheck(control); - - if (position < 0 || static_cast<decltype(children_.size())>(position) > - this->children_.size()) - throw std::invalid_argument("The position is out of range."); - - children_.insert(this->children_.cbegin() + position, control); - - control->_SetParent(this); - control->_SetDescendantWindow(GetWindow()); - - OnAddChild(control, position); -} - -void Layout::RemoveChild(const int position) { - if (position < 0 || static_cast<decltype(this->children_.size())>(position) >= - this->children_.size()) - throw std::invalid_argument("The position is out of range."); - - const auto i = children_.cbegin() + position; - const auto child = *i; - - children_.erase(i); - - child->_SetParent(nullptr); - child->_SetDescendantWindow(nullptr); - - OnRemoveChild(child, position); -} - -void Layout::OnAddChild(Control* child, int position) {} - -void Layout::OnRemoveChild(Control* child, int position) {} - -std::list<Control*> GetAncestorList(Control* control) { - std::list<Control*> l; - while (control != nullptr) { - l.push_front(control); - control = control->GetParent(); - } - return l; -} - -Control* FindLowestCommonAncestor(Control* left, Control* right) { - if (left == nullptr || right == nullptr) return nullptr; - - auto&& left_list = GetAncestorList(left); - auto&& right_list = GetAncestorList(right); - - // the root is different - if (left_list.front() != right_list.front()) return nullptr; - - // find the last same control or the last control (one is ancestor of the - // other) - auto left_i = left_list.cbegin(); - auto right_i = right_list.cbegin(); - while (true) { - if (left_i == left_list.cend()) return *(--left_i); - if (right_i == right_list.cend()) return *(--right_i); - if (*left_i != *right_i) return *(--left_i); - ++left_i; - ++right_i; - } -} } // namespace cru::ui |