aboutsummaryrefslogtreecommitdiff
path: root/src/ui/control.cpp
blob: 98986d3cfbfb8f33ce1f2f390af4e0173ffb5672 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "control.hpp"

#include <cassert>

#include "window.hpp"

namespace cru::ui {

void Control::_SetParent(Control* parent) {
  const auto old_parent = GetParent();
  parent_ = parent;
  const auto new_parent = GetParent();
  if (old_parent != new_parent) OnParentChanged(old_parent, new_parent);
}

void Control::_SetDescendantWindow(Window* window) {
  if (window == nullptr && window_ == nullptr) return;

  // You can only attach or detach window.
  assert((window != nullptr && window_ == nullptr) ||
         (window == nullptr && window_ != nullptr));

  if (window == nullptr) {
    const auto old = window_;
    TraverseDescendants([old](Control* control) {
      control->window_ = nullptr;
      control->OnDetachToWindow(old);
    });
  } else
    TraverseDescendants([window](Control* control) {
      control->window_ = window;
      control->OnAttachToWindow(window);
    });
}

void Control::TraverseDescendants(
    const std::function<void(Control*)>& predicate) {
  TraverseDescendantsInternal(this, predicate);
}

void Control::TraverseDescendantsInternal(
    Control* control, const std::function<void(Control*)>& predicate) {
  predicate(control);
  for (auto c : control->GetChildren())
    TraverseDescendantsInternal(c, predicate);
}
bool Control::RequestFocus() {
  auto window = GetWindow();
  if (window == nullptr) return false;

  return window->RequestFocusFor(this);
}

bool Control::HasFocus() {
  auto window = GetWindow();
  if (window == nullptr) return false;

  return window->GetFocusControl() == this;
}

void Control::SetCursor(const Cursor::Ptr& cursor) {
  if (cursor != cursor_) {
    cursor_ = cursor;
    const auto window = GetWindow();
    if (window && window->GetMouseHoverControl() == this)
      window->UpdateCursor();
  }
}

void Control::OnParentChanged(Control* old_parent, Control* new_parent) {}

void Control::OnAttachToWindow(Window* window) {}

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