aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/Control.cpp
blob: c1316a6227645d7fe795390466526b43243e4439 (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
#include "cru/ui/controls/Control.hpp"

#include "cru/common/Base.hpp"
#include "cru/platform/gui/Cursor.hpp"
#include "cru/platform/gui/UiApplication.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/host/WindowHost.hpp"
#include "cru/ui/render/RenderObject.hpp"

#include <memory>

namespace cru::ui::controls {
using platform::gui::ICursor;
using platform::gui::IUiApplication;
using platform::gui::SystemCursorType;

Control::Control() {
  MouseEnterEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) {
    this->is_mouse_over_ = true;
    this->OnMouseHoverChange(true);
  });

  MouseLeaveEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) {
    this->is_mouse_over_ = false;
    this->OnMouseHoverChange(true);
  });
}

Control::~Control() {
  for (const auto child : children_) delete child;
}

host::WindowHost* Control::GetWindowHost() const { return window_host_; }

void Control::TraverseDescendants(
    const std::function<void(Control*)>& predicate) {
  predicate(this);
  for (auto c : GetChildren()) c->TraverseDescendants(predicate);
}

bool Control::HasFocus() {
  auto host = GetWindowHost();
  if (host == nullptr) return false;

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

bool Control::CaptureMouse() {
  auto host = GetWindowHost();
  if (host == nullptr) return false;

  return host->CaptureMouseFor(this);
}

void Control::SetFocus() {
  auto host = GetWindowHost();
  if (host == nullptr) return;

  host->SetFocusControl(this);
}

bool Control::ReleaseMouse() {
  auto host = GetWindowHost();
  if (host == nullptr) return false;

  return host->CaptureMouseFor(nullptr);
}

bool Control::IsMouseCaptured() {
  auto host = GetWindowHost();
  if (host == nullptr) return false;

  return host->GetMouseCaptureControl() == this;
}

std::shared_ptr<ICursor> Control::GetCursor() { return cursor_; }

std::shared_ptr<ICursor> Control::GetInheritedCursor() {
  Control* control = this;
  while (control != nullptr) {
    const auto cursor = control->GetCursor();
    if (cursor != nullptr) return cursor;
    control = control->GetParent();
  }
  return IUiApplication::GetInstance()->GetCursorManager()->GetSystemCursor(
      SystemCursorType::Arrow);
}

void Control::SetCursor(std::shared_ptr<ICursor> cursor) {
  cursor_ = std::move(cursor);
  const auto host = GetWindowHost();
  if (host != nullptr) {
    host->UpdateCursor();
  }
}

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<Index>(
                          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<Index>(
                         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