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

#include "cru/platform/native/basic_types.hpp"
#include "cru/ui/base.hpp"
#include "cru/ui/event/ui_event.hpp"
#include "cru/ui/window.hpp"
#include "routed_event_dispatch.hpp"

#include <cassert>

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);

  MouseDownEvent()->Direct()->AddHandler(
      [this](event::MouseButtonEventArgs& args) {
        switch (args.GetMouseButton()) {
          case MouseButton::Left:
            click_map_.left = true;
            OnMouseClickBegin(MouseButton::Left);
            break;
          case MouseButton::Middle:
            click_map_.middle = true;
            OnMouseClickBegin(MouseButton::Middle);
            break;
          case MouseButton::Right:
            click_map_.right = true;
            OnMouseClickBegin(MouseButton::Right);
            break;
        }
      });

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

  MouseLeaveEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) {
    this->is_mouse_over_ = false;
    if (click_map_.left) {
      OnMouseClickCancel(MouseButton::Left);
    }
    if (click_map_.middle) {
      OnMouseClickCancel(MouseButton::Middle);
    }
    if (click_map_.right) {
      OnMouseClickCancel(MouseButton::Right);
    }
    click_map_.left = click_map_.middle = click_map_.right = false;
  });

  MouseUpEvent()->Direct()->AddHandler([this](event::MouseButtonEventArgs& args) {
    switch (args.GetMouseButton()) {
      case MouseButton::Left:
        if (click_map_.left) {
          click_map_.left = false;
          OnMouseClickEnd(MouseButton::Left);
          DispatchEvent(this, &Control::MouseClickEvent, nullptr, args.GetPoint(), args.GetMouseButton());
        }
        break;
      case MouseButton::Middle:
        if (click_map_.middle) {
          click_map_.middle = false;
          OnMouseClickEnd(MouseButton::Middle);
          DispatchEvent(this, &Control::MouseClickEvent, nullptr, args.GetPoint(), args.GetMouseButton());
        }
        break;
      case MouseButton::Right:
        if (click_map_.right) {
          click_map_.right = false;
          OnMouseClickEnd(MouseButton::Right);
          DispatchEvent(this, &Control::MouseClickEvent, nullptr, args.GetPoint(), args.GetMouseButton());
        }
        break;
    }
  });
}  // namespace cru::ui

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) {
  _TraverseDescendants(this, predicate);
}

void Control::_TraverseDescendants(
    Control* control, const std::function<void(Control*)>& predicate) {
  predicate(control);
  for (auto c : control->GetChildren()) _TraverseDescendants(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::OnParentChanged(Control* old_parent, Control* new_parent) {}

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

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

void Control::OnMouseClickBegin(platform::native::MouseButton button) {}

void Control::OnMouseClickEnd(platform::native::MouseButton button) {}

void Control::OnMouseClickCancel(platform::native::MouseButton button) {}
}  // namespace cru::ui