aboutsummaryrefslogtreecommitdiff
path: root/src/platform_win/win_native_window.cpp
blob: 3f34717f0154af90b56778f1e8e9a4ffd7a374b0 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "cru/platform/win/win_native_window.hpp"

#include "cru/platform/win/exception.hpp"
#include "cru/platform/win/win_application.hpp"
#include "cru/platform/win/win_painter.hpp"
#include "cru/platform/win/window_class.hpp"
#include "cru/platform/win/window_render_target.hpp"
#include "dpi_util.hpp"
#include "window_manager.hpp"

#include <assert.h>
#include <windowsx.h>

namespace cru::platform::win {
WinNativeWindow::WinNativeWindow(WinApplication* application,
                                 std::shared_ptr<WindowClass> window_class,
                                 DWORD window_style, WinNativeWindow* parent) {
  assert(application);  // application can't be null.
  assert(parent == nullptr ||
         parent->IsValid());  // Parent window is not valid.

  application_ = application;
  parent_window_ = parent;

  const auto window_manager = application->GetWindowManager();

  hwnd_ = CreateWindowExW(
      0, window_manager->GetGeneralWindowClass()->GetName(), L"", window_style,
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
      parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr,
      application->GetInstanceHandle(), nullptr);

  if (hwnd_ == nullptr)
    throw Win32Error(::GetLastError(), "Failed to create window.");

  window_manager->RegisterWindow(hwnd_, this);

  window_render_target_.reset(
      new WindowRenderTarget(application->GetGraphManager(), hwnd_));
}

WinNativeWindow::~WinNativeWindow() {
  if (IsValid()) {
    SetDeleteThisOnDestroy(false);  // avoid double delete.
    Close();
  }
}

bool WinNativeWindow::IsValid() { return hwnd_ != nullptr; }

void WinNativeWindow::SetDeleteThisOnDestroy(bool value) {
  delete_this_on_destroy_ = value;
}

void WinNativeWindow::Close() {
  if (IsValid()) DestroyWindow(hwnd_);
}

bool WinNativeWindow::IsVisible() {
  if (IsValid()) return ::IsWindowVisible(hwnd_);
  return false;
}

void WinNativeWindow::SetVisible(bool is_visible) {
  if (!IsValid()) return;
  is_visible ? ShowWindow(hwnd_, SW_SHOWNORMAL) : ShowWindow(hwnd_, SW_HIDE);
}
ui::Size WinNativeWindow::GetClientSize() {
  if (!IsValid()) return ui::Size{};

  const auto pixel_rect = GetClientRectPixel();
  return ui::Size(PixelToDipX(pixel_rect.right),
                  PixelToDipY(pixel_rect.bottom));
}

void WinNativeWindow::SetClientSize(const ui::Size& size) {
  if (IsValid()) {
    const auto window_style =
        static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_STYLE));
    const auto window_ex_style =
        static_cast<DWORD>(GetWindowLongPtr(hwnd_, GWL_EXSTYLE));

    RECT rect;
    rect.left = 0;
    rect.top = 0;
    rect.right = DipToPixelX(size.width);
    rect.bottom = DipToPixelY(size.height);
    if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style))
      throw Win32Error(::GetLastError(),
                       "Failed to invoke AdjustWindowRectEx.");

    if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left,
                      rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE))
      throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
  }
}

ui::Rect WinNativeWindow::GetWindowRect() {
  if (!IsValid()) return ui::Rect{};

  RECT rect;
  if (!::GetWindowRect(hwnd_, &rect))
    throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect.");

  return ui::Rect::FromVertices(PixelToDipX(rect.left), PixelToDipY(rect.top),
                                PixelToDipX(rect.right),
                                PixelToDipY(rect.bottom));
}

void WinNativeWindow::SetWindowRect(const ui::Rect& rect) {
  if (IsValid()) {
    if (!SetWindowPos(hwnd_, nullptr, DipToPixelX(rect.left),
                      DipToPixelY(rect.top), DipToPixelX(rect.GetRight()),
                      DipToPixelY(rect.GetBottom()), SWP_NOZORDER))
      throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
  }
}

Painter* WinNativeWindow::BeginPaint() { 
  return new WinPainter(this); }

bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg,
                                                WPARAM w_param, LPARAM l_param,
                                                LRESULT* result) {
  WindowNativeMessageEventArgs args{
      WindowNativeMessage{hwnd, msg, w_param, l_param}};
  native_message_event_.Raise(args);
  if (args.IsHandled()) {
    *result = args.GetResult();
    return true;
  }

  switch (msg) {
    case WM_PAINT:
      OnPaintInternal();
      *result = 0;
      return true;
    case WM_ERASEBKGND:
      *result = 1;
      return true;
    case WM_SETFOCUS:
      OnSetFocusInternal();
      *result = 0;
      return true;
    case WM_KILLFOCUS:
      OnKillFocusInternal();
      *result = 0;
      return true;
    case WM_MOUSEMOVE: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseMoveInternal(point);
      *result = 0;
      return true;
    }
    case WM_LBUTTONDOWN: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseDownInternal(MouseButton::Left, point);
      *result = 0;
      return true;
    }
    case WM_LBUTTONUP: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseUpInternal(MouseButton::Left, point);
      *result = 0;
      return true;
    }
    case WM_RBUTTONDOWN: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseDownInternal(MouseButton::Right, point);
      *result = 0;
      return true;
    }
    case WM_RBUTTONUP: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseUpInternal(MouseButton::Right, point);
      *result = 0;
      return true;
    }
    case WM_MBUTTONDOWN: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseDownInternal(MouseButton::Middle, point);
      *result = 0;
      return true;
    }
    case WM_MBUTTONUP: {
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      OnMouseUpInternal(MouseButton::Middle, point);
      *result = 0;
      return true;
    }
    case WM_MOUSEWHEEL:
      POINT point;
      point.x = GET_X_LPARAM(l_param);
      point.y = GET_Y_LPARAM(l_param);
      ScreenToClient(hwnd, &point);
      OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point);
      *result = 0;
      return true;
    case WM_KEYDOWN:
      OnKeyDownInternal(static_cast<int>(w_param));
      *result = 0;
      return true;
    case WM_KEYUP:
      OnKeyUpInternal(static_cast<int>(w_param));
      *result = 0;
      return true;
    case WM_CHAR:
      OnCharInternal(static_cast<wchar_t>(w_param));
      *result = 0;
      return true;
    case WM_SIZE:
      OnResizeInternal(LOWORD(l_param), HIWORD(l_param));
      *result = 0;
      return true;
    case WM_ACTIVATE:
      if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE)
        OnActivatedInternal();
      else if (w_param == WA_INACTIVE)
        OnDeactivatedInternal();
      *result = 0;
      return true;
    case WM_DESTROY:
      OnDestroyInternal();
      *result = 0;
      return true;
    default:
      return false;
  }
}

RECT WinNativeWindow::GetClientRectPixel() {
  RECT rect;
  if (!GetClientRect(hwnd_, &rect))
    throw Win32Error(::GetLastError(), "Failed to invoke GetClientRect.");
  return rect;
}

void WinNativeWindow::OnDestroyInternal() {
  application_->GetWindowManager()->UnregisterWindow(hwnd_);
  hwnd_ = nullptr;
  if (delete_this_on_destroy_)
    application_->InvokeLater([this] { delete this; });
}

void WinNativeWindow::OnPaintInternal() {
  paint_event_.Raise();
  ValidateRect(hwnd_, nullptr);
}

void WinNativeWindow::OnResizeInternal(const int new_width,
                                       const int new_height) {
  if (!(new_width == 0 && new_height == 0)) {
    window_render_target_->ResizeBuffer(new_width, new_height);
    resize_event_.Raise(
        ui::Size{PixelToDipX(new_width), PixelToDipY(new_height)});
  }
}

void WinNativeWindow::OnSetFocusInternal() {
  has_focus_ = true;
  focus_event_.Raise(true);
}

void WinNativeWindow::OnKillFocusInternal() {
  has_focus_ = false;
  focus_event_.Raise(false);
}

inline ui::Point PiToDip(const POINT& pi_point) {
  return ui::Point(PixelToDipX(pi_point.x), PixelToDipY(pi_point.y));
}

void WinNativeWindow::OnMouseMoveInternal(const POINT point) {
  // when mouse was previous outside the window
  if (!is_mouse_in_) {
    // invoke TrackMouseEvent to have WM_MOUSELEAVE sent.
    TRACKMOUSEEVENT tme;
    tme.cbSize = sizeof tme;
    tme.dwFlags = TME_LEAVE;
    tme.hwndTrack = hwnd_;

    TrackMouseEvent(&tme);

    is_mouse_in_ = true;
    mouse_enter_leave_event_.Raise(true);
  }

  const auto dip_point = PiToDip(point);
  mouse_move_event_.Raise(dip_point);
}

void WinNativeWindow::OnMouseLeaveInternal() {
  is_mouse_in_ = false;
  mouse_enter_leave_event_.Raise(false);
}

void WinNativeWindow::OnMouseDownInternal(MouseButton button, POINT point) {
  const auto dip_point = PiToDip(point);
  mouse_down_event_.Raise(button, dip_point);
}

void WinNativeWindow::OnMouseUpInternal(MouseButton button, POINT point) {
  const auto dip_point = PiToDip(point);
  mouse_up_event_.Raise(button, dip_point);
}

void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) {}

void WinNativeWindow::OnKeyDownInternal(int virtual_code) {
  key_down_event_.Raise(virtual_code);
}

void WinNativeWindow::OnKeyUpInternal(int virtual_code) {
  key_up_event_.Raise(virtual_code);
}

void WinNativeWindow::OnCharInternal(wchar_t c) {}

void WinNativeWindow::OnActivatedInternal() {}

void WinNativeWindow::OnDeactivatedInternal() {}
}  // namespace cru::platform::win