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/layout_control.cpp | |
parent | afa2a079562d810e5ef611071b21a3568da9dfca (diff) | |
download | cru-962dc18ee4827b464764ec3708be3d00a9143971.tar.gz cru-962dc18ee4827b464764ec3708be3d00a9143971.tar.bz2 cru-962dc18ee4827b464764ec3708be3d00a9143971.zip |
...
Diffstat (limited to 'src/ui/layout_control.cpp')
-rw-r--r-- | src/ui/layout_control.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/ui/layout_control.cpp b/src/ui/layout_control.cpp new file mode 100644 index 00000000..d2b430dd --- /dev/null +++ b/src/ui/layout_control.cpp @@ -0,0 +1,52 @@ +#include "layout_control.hpp" + +#include "window.hpp" + +namespace cru::ui { +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."); +} + +LayoutControl::~LayoutControl() { + for (const auto child : children_) delete child; +} + +void LayoutControl::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 LayoutControl::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 LayoutControl::OnAddChild(Control* child, int position) {} + +void LayoutControl::OnRemoveChild(Control* child, int position) {} +} // namespace cru::ui |