aboutsummaryrefslogtreecommitdiff
path: root/src/ui/layout_control.cpp
blob: 9d78967091655fe84dc6621846d1337ec3e97115 (plain)
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
#include "cru/ui/layout_control.hpp"

#include "window.hpp"

#include <cassert>

namespace cru::ui {
LayoutControl::~LayoutControl() {
  for (const auto child : children_) delete child;
}

void LayoutControl::AddChild(Control* control, const int position) {
  assert(control->GetParent() == nullptr);  // The control already has a parent.
  assert(!dynamic_cast<Window*>(control));  // Can't add a window as child.
  assert(position >= 0 ||
         position <= this->children_.size());  // 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) {
  assert(position >= 0 &&
         position < this->children_.size());  // 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