aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/Control.cpp
blob: 9c0fc537f47e0173690b470245112a70dcfa3996 (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
#include "cru/ui/controls/Control.h"
#include "cru/base/log/Logger.h"
#include "cru/ui/controls/Window.h"

#include "cru/platform/gui/Cursor.h"
#include "cru/platform/gui/UiApplication.h"
#include "cru/ui/style/StyleRuleSet.h"

#include <format>

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

Control::Control() {
  style_rule_set_ = std::make_shared<style::StyleRuleSet>();
  style_rule_set_bind_ =
      std::make_unique<style::StyleRuleSetBind>(this, style_rule_set_);

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

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

Control::~Control() {
  if (auto window = GetWindow()) {
    if (window->IsInEventHandling()) {
      CRU_LOG_TAG_WARN(
          "Better use delete later to delete control during event handling.");
    }
  }

  if (auto window = GetWindow()) {
    window->NotifyControlDestroyed(this);
  }
  RemoveFromParent();
}

std::string Control::GetDebugId() const {
  return std::format("{}({})", GetControlType(),
                     static_cast<const void*>(this));
}

Window* Control::GetWindow() {
  auto parent = this;
  while (parent) {
    if (auto window = dynamic_cast<Window*>(parent)) {
      return window;
    }
    parent = parent->GetParent();
  }
  return nullptr;
}

void Control::SetParent(Control* parent) {
  if (parent_ == parent) return;
  auto old_parent = parent_;
  parent_ = parent;
  OnParentChanged(old_parent, parent);
}

bool Control::HasAncestor(Control* control) {
  auto parent = this;
  while (parent) {
    if (parent == control) return true;
    parent = parent->GetParent();
  }
  return false;
}

void Control::RemoveFromParent() {
  if (parent_) {
    parent_->RemoveChild(this);
  }
}

controls::Control* Control::HitTest(const Point& point) {
  const auto render_object = GetRenderObject()->HitTest(point);
  if (render_object) {
    const auto control = render_object->GetAttachedControl();
    assert(control);
    return control;
  }
  return nullptr;
}

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

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

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

  return window->SetMouseCaptureControl(this);
}

void Control::SetFocus() {
  auto window = GetWindow();
  if (window == nullptr) return;

  window->SetFocusControl(this);
}

bool Control::ReleaseMouse() {
  auto window = GetWindow();
  if (window == nullptr) return false;
  if (window->GetMouseCaptureControl() != this) return false;
  return window->SetMouseCaptureControl(nullptr);
}

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

  return window->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 window = GetWindow();
  if (window != nullptr) {
    window->UpdateCursor();
  }
}

std::shared_ptr<style::StyleRuleSet> Control::GetStyleRuleSet() {
  return style_rule_set_;
}
}  // namespace cru::ui::controls