aboutsummaryrefslogtreecommitdiff
path: root/src/ui/window.cpp
blob: 1065a3fb8b691b711e9f27f947defabdb66cd99d (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "cru/ui/window.hpp"

#include "cru/ui/render/window_render_object.hpp"
#include "cru/platform/ui_applicaition.hpp"
#include "cru/platform/native_window.hpp"

#include <cassert>

namespace cru::ui {
namespace {
// Dispatch the event.
//
// This will raise routed event of the control and its parent and parent's
// parent ... (until "last_receiver" if it's not nullptr) with appropriate args.
//
// First tunnel from top to bottom possibly stopped by "handled" flag in
// EventArgs. Second bubble from bottom to top possibly stopped by "handled"
// flag in EventArgs. Last direct to each control.
//
// Args is of type "EventArgs". The first init argument is "sender", which is
// automatically bound to each receiving control. The second init argument is
// "original_sender", which is unchanged. And "args" will be perfectly forwarded
// as the rest arguments.
template <typename EventArgs, typename... Args>
void DispatchEvent(Control* const original_sender,
                   event::RoutedEvent<EventArgs>* (Control::*event_ptr)(),
                   Control* const last_receiver, Args&&... args) {
  std::list<Control*> receive_list;

  auto parent = original_sender;
  while (parent != last_receiver) {
    receive_list.push_back(parent);
    parent = parent->GetParent();
  }

  auto handled = false;

  // tunnel
  for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) {
    EventArgs event_args(*i, original_sender, std::forward<Args>(args)...);
    (*i->*event_ptr)()->tunnel.Raise(event_args);
    if (event_args.IsHandled()) {
      handled = true;
      break;
    }
  }

  // bubble
  if (!handled) {
    for (auto i : receive_list) {
      EventArgs event_args(i, original_sender, std::forward<Args>(args)...);
      (i->*event_ptr)()->bubble.Raise(event_args);
      if (event_args.IsHandled()) break;
    }
  }

  // direct
  for (auto i : receive_list) {
    EventArgs event_args(i, original_sender, std::forward<Args>(args)...);
    (i->*event_ptr)()->direct.Raise(event_args);
  }
}

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

Window* Window::CreateOverlapped() {
  return new Window(tag_overlapped_constructor{});
}


Window::Window(tag_overlapped_constructor) {
  native_window_ = platform::UiApplication::GetInstance()->CreateWindow(nullptr);
  render_object_.reset(new render::WindowRenderObject(this));
}

Window::~Window() {
  TraverseDescendants(
      [this](Control* control) { control->OnDetachToWindow(this); });
}

std::wstring_view Window::GetControlType() const { return control_type; }

render::RenderObject* Window::GetRenderObject() const {
  return render_object_.get();
}

bool Window::RequestFocusFor(Control* control) {
  assert(control != nullptr);  // The control to request focus can't be null.
                               // You can set it as the window.

  if (focus_control_ == control) return true;

  DispatchEvent(focus_control_, &Control::LoseFocusEvent, nullptr, false);

  focus_control_ = control;

  DispatchEvent(control, &Control::GainFocusEvent, nullptr, false);

  return true;
}

Control* Window::GetFocusControl() { return focus_control_; }

void Window::OnChildChanged(Control* old_child, Control* new_child) {
  if (old_child) render_object_->RemoveChild(0);
  if (new_child) render_object_->AddChild(new_child->GetRenderObject(), 0);
}

Control* Window::HitTest(const Point& point) {
  return render_object_->HitTest(point)->GetAttachedControl();
}

void Window::OnNativeDestroy() { delete this; }

void Window::OnNativePaint() {
  render_target_->SetAsTarget();

  auto device_context =
      render_target_->GetGraphManager()->GetD2D1DeviceContext();
  device_context->BeginDraw();
  // Clear the background.
  device_context->Clear(D2D1::ColorF(D2D1::ColorF::White));
  render_object_->Draw(device_context);
  ThrowIfFailed(device_context->EndDraw(), "Failed to draw window.");
  render_target_->Present();

  ValidateRect(hwnd_, nullptr);
}

void Window::OnResizeInternal(const int new_width, const int new_height) {
  render_target_->ResizeBuffer(new_width, new_height);
  if (!(new_width == 0 && new_height == 0)) render_object_->MeasureAndLayout();
}

void Window::OnSetFocusInternal() {
  window_focus_ = true;
  DispatchEvent(focus_control_, &Control::GainFocusEvent, nullptr, true);
}

void Window::OnKillFocusInternal() {
  window_focus_ = false;
  DispatchEvent(focus_control_, &Control::LoseFocusEvent, nullptr, true);
}

void Window::OnMouseMoveInternal(const POINT point) {
  const auto dip_point = PiToDip(point);

  // when mouse was previous outside the window
  if (mouse_hover_control_ == nullptr) {
    // invoke TrackMouseEvent to have WM_MOUSELEAVE sent.
    TRACKMOUSEEVENT tme;
    tme.cbSize = sizeof tme;
    tme.dwFlags = TME_LEAVE;
    tme.hwndTrack = hwnd_;

    TrackMouseEvent(&tme);
  }

  // Find the first control that hit test succeed.
  const auto new_control_mouse_hover = HitTest(dip_point);
  const auto old_control_mouse_hover = mouse_hover_control_;
  mouse_hover_control_ = new_control_mouse_hover;

  if (mouse_capture_control_)  // if mouse is captured
  {
    DispatchEvent(mouse_capture_control_, &Control::MouseMoveEvent, nullptr,
                  dip_point);
  } else {
    DispatchMouseHoverControlChangeEvent(old_control_mouse_hover,
                                         new_control_mouse_hover, dip_point);
    DispatchEvent(new_control_mouse_hover, &Control::MouseMoveEvent, nullptr,
                  dip_point);
  }
}

void Window::OnMouseLeaveInternal() {
  DispatchEvent(mouse_hover_control_, &Control::MouseLeaveEvent, nullptr);
  mouse_hover_control_ = nullptr;
}

void Window::OnMouseDownInternal(MouseButton button, POINT point) {
  const auto dip_point = PiToDip(point);

  Control* control;

  if (mouse_capture_control_)
    control = mouse_capture_control_;
  else
    control = HitTest(dip_point);

  DispatchEvent(control, &Control::MouseDownEvent, nullptr, dip_point,
                button);
}

void Window::OnMouseUpInternal(MouseButton button, POINT point) {
  const auto dip_point = PiToDip(point);

  Control* control;

  if (mouse_capture_control_)
    control = mouse_capture_control_;
  else
    control = HitTest(dip_point);

  DispatchEvent(control, &Control::MouseUpEvent, nullptr, dip_point, button);
}

void Window::OnMouseWheelInternal(short delta, POINT point) {
  const auto dip_point = PiToDip(point);

  Control* control;

  if (mouse_capture_control_)
    control = mouse_capture_control_;
  else
    control = HitTest(dip_point);

  DispatchEvent(control, &Control::MouseWheelEvent, nullptr, dip_point,
                static_cast<float>(delta));
}

void Window::OnKeyDownInternal(int virtual_code) {
  DispatchEvent(focus_control_, &Control::KeyDownEvent, nullptr,
                virtual_code);
}

void Window::OnKeyUpInternal(int virtual_code) {
  DispatchEvent(focus_control_, &Control::KeyUpEvent, nullptr, virtual_code);
}

void Window::OnCharInternal(wchar_t c) {
  DispatchEvent(focus_control_, &Control::CharEvent, nullptr, c);
}

void Window::OnActivatedInternal() {
  events::UiEventArgs args(this, this);
  activated_event_.Raise(args);
}

void Window::OnDeactivatedInternal() {
  events::UiEventArgs args(this, this);
  deactivated_event_.Raise(args);
}

void Window::DispatchMouseHoverControlChangeEvent(Control* old_control,
                                                  Control* new_control,
                                                  const Point& point) {
  if (new_control != old_control)  // if the mouse-hover-on control changed
  {
    const auto lowest_common_ancestor =
        FindLowestCommonAncestor(old_control, new_control);
    if (old_control != nullptr)  // if last mouse-hover-on control exists
      DispatchEvent(old_control, &Control::MouseLeaveEvent,
                    lowest_common_ancestor);  // dispatch mouse leave event.
    if (new_control != nullptr) {
      DispatchEvent(new_control, &Control::MouseEnterEvent,
                    lowest_common_ancestor,
                    point);  // dispatch mouse enter event.
    }
  }
}
}  // namespace cru::ui