aboutsummaryrefslogtreecommitdiff
path: root/src/win/gui/Window.cpp
blob: 9c86441ec48b129a703226c20d82b9f1c75c2e2d (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "cru/win/gui/Window.h"

#include "WindowManager.h"
#include "cru/common/log/Logger.h"
#include "cru/platform/Check.h"
#include "cru/platform/graphics/NullPainter.h"
#include "cru/platform/gui/Base.h"
#include "cru/platform/gui/DebugFlags.h"
#include "cru/platform/gui/Window.h"
#include "cru/win/graphics/direct/WindowPainter.h"
#include "cru/win/gui/Cursor.h"
#include "cru/win/gui/Exception.h"
#include "cru/win/gui/InputMethod.h"
#include "cru/win/gui/Keyboard.h"
#include "cru/win/gui/UiApplication.h"
#include "cru/win/gui/WindowClass.h"

#include <windowsx.h>
#include <winuser.h>
#include <memory>

namespace cru::platform::gui::win {
namespace {
inline int DipToPixel(const float dip, const float dpi) {
  return static_cast<int>(dip * dpi / 96.0f);
}

inline float PixelToDip(const int pixel, const float dpi) {
  return static_cast<float>(pixel) * 96.0f / dpi;
}

DWORD CalcWindowStyle(WindowStyleFlag flag) {
  return flag & WindowStyleFlags::NoCaptionAndBorder ? WS_POPUP
                                                     : WS_OVERLAPPEDWINDOW;
}

Rect CalcWindowRectFromClient(const Rect& rect, WindowStyleFlag style_flag,
                              float dpi) {
  RECT r;
  r.left = DipToPixel(rect.left, dpi);
  r.top = DipToPixel(rect.top, dpi);
  r.right = DipToPixel(rect.GetRight(), dpi);
  r.bottom = DipToPixel(rect.GetBottom(), dpi);
  if (!AdjustWindowRectEx(&r, CalcWindowStyle(style_flag), FALSE, 0))
    throw Win32Error(::GetLastError(), u"Failed to invoke AdjustWindowRectEx.");

  Rect result =
      Rect::FromVertices(PixelToDip(r.left, dpi), PixelToDip(r.top, dpi),
                         PixelToDip(r.right, dpi), PixelToDip(r.bottom, dpi));
  return result;
}

Rect CalcClientRectFromWindow(const Rect& rect, WindowStyleFlag style_flag,
                              float dpi) {
  RECT o{100, 100, 500, 500};
  RECT s = o;
  if (!AdjustWindowRectEx(&s, CalcWindowStyle(style_flag), FALSE, 0))
    throw Win32Error(::GetLastError(), u"Failed to invoke AdjustWindowRectEx.");

  Rect result = rect;
  result.Shrink(Thickness(PixelToDip(s.left - o.left, dpi),
                          PixelToDip(o.top - s.top, dpi),
                          PixelToDip(s.right - o.right, dpi),
                          PixelToDip(s.bottom - o.bottom, dpi)));

  return result;
}
}  // namespace

WinNativeWindow::WinNativeWindow(WinUiApplication* application)
    : application_(application) {
  Expects(application);  // application can't be null.
}

WinNativeWindow::~WinNativeWindow() { Close(); }

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

void WinNativeWindow::SetParent(INativeWindow* parent) {
  auto p = CheckPlatform<WinNativeWindow>(parent, GetPlatformId());
  parent_window_ = p;

  if (hwnd_) {
    ::SetParent(hwnd_, parent_window_->hwnd_);
  }
}

String WinNativeWindow::GetTitle() { return title_; }

void WinNativeWindow::SetTitle(String title) {
  title_ = title;

  if (hwnd_) {
    ::SetWindowTextW(hwnd_, title_.WinCStr());
  }
}

void WinNativeWindow::SetStyleFlag(WindowStyleFlag flag) {
  if (flag == style_flag_) return;

  style_flag_ = flag;
  if (hwnd_) {
    SetWindowLongPtrW(hwnd_, GWL_STYLE,
                      static_cast<LONG_PTR>(CalcWindowStyle(style_flag_)));
  }
}

void WinNativeWindow::SetVisibility(WindowVisibilityType visibility) {
  if (visibility == visibility_) return;
  visibility_ = visibility;

  if (!hwnd_) {
    RecreateWindow();
  }

  if (visibility == WindowVisibilityType::Show) {
    ShowWindow(hwnd_, SW_SHOWNORMAL);
  } else if (visibility == WindowVisibilityType::Hide) {
    ShowWindow(hwnd_, SW_HIDE);
  } else if (visibility == WindowVisibilityType::Minimize) {
    ShowWindow(hwnd_, SW_MINIMIZE);
  }
}

Size WinNativeWindow::GetClientSize() { return GetClientRect().GetSize(); }

void WinNativeWindow::SetClientSize(const Size& size) {
  client_rect_.SetSize(size);

  if (hwnd_) {
    RECT rect =
        DipToPixel(CalcWindowRectFromClient(client_rect_, style_flag_, dpi_));

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

Rect WinNativeWindow::GetClientRect() { return client_rect_; }

void WinNativeWindow::SetClientRect(const Rect& rect) {
  client_rect_ = rect;

  if (hwnd_) {
    RECT r =
        DipToPixel(CalcWindowRectFromClient(client_rect_, style_flag_, dpi_));

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

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

    return Rect::FromVertices(PixelToDip(rect.left), PixelToDip(rect.top),
                              PixelToDip(rect.right), PixelToDip(rect.bottom));
  } else {
    return CalcWindowRectFromClient(client_rect_, style_flag_, dpi_);
  }
}

void WinNativeWindow::SetWindowRect(const Rect& rect) {
  client_rect_ = CalcClientRectFromWindow(rect, style_flag_, dpi_);

  if (hwnd_) {
    if (!SetWindowPos(hwnd_, nullptr, DipToPixel(rect.left),
                      DipToPixel(rect.top), DipToPixel(rect.GetRight()),
                      DipToPixel(rect.GetBottom()), SWP_NOZORDER))
      throw Win32Error(::GetLastError(), u"Failed to invoke SetWindowPos.");
  }
}

bool WinNativeWindow::RequestFocus() {
  if (hwnd_) {
    SetFocus(hwnd_);
    return true;
  }
  return false;
}

Point WinNativeWindow::GetMousePosition() {
  POINT p;
  if (!::GetCursorPos(&p))
    throw Win32Error(::GetLastError(), u"Failed to get cursor position.");
  if (!::ScreenToClient(hwnd_, &p))
    throw Win32Error(::GetLastError(), u"Failed to call ScreenToClient.");
  return PixelToDip(p);
}

bool WinNativeWindow::CaptureMouse() {
  ::SetCapture(hwnd_);
  return true;
}

bool WinNativeWindow::ReleaseMouse() {
  const auto result = ::ReleaseCapture();
  return result != 0;
}

void WinNativeWindow::RequestRepaint() {
  if constexpr (DebugFlags::paint) {
    CRU_LOG_DEBUG(u"A repaint is requested.");
  }
  if (!::InvalidateRect(hwnd_, nullptr, FALSE))
    throw Win32Error(::GetLastError(), u"Failed to invalidate window.");
  if (!::UpdateWindow(hwnd_))
    throw Win32Error(::GetLastError(), u"Failed to update window.");
}

std::unique_ptr<graphics::IPainter> WinNativeWindow::BeginPaint() {
  if (hwnd_)
    return std::make_unique<graphics::win::direct::D2DWindowPainter>(
        window_render_target_.get());
  else
    return std::make_unique<graphics::NullPainter>();
}

void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) {
  if (cursor == nullptr) {
    throw std::runtime_error("Can't use a nullptr as cursor.");
  }

  cursor_ = CheckPlatform<WinCursor>(cursor, GetPlatformId());

  if (hwnd_) return;

  if (!::SetClassLongPtrW(hwnd_, GCLP_HCURSOR,
                          reinterpret_cast<LONG_PTR>(cursor_->GetHandle()))) {
    CRU_LOG_WARN(
                 u"Failed to set cursor because failed to set class long. Last "
                 u"error code: {}.",
                 ::GetLastError());
    return;
  }

  if (GetVisibility() != WindowVisibilityType::Show) return;

  auto lg = [](StringView reason) {
    CRU_LOG_WARN(
        
        u"Failed to set cursor because {} when window is visible. (We need to "
        u"update cursor if it is inside the window.) Last error code: {}.",
        reason, ::GetLastError());
  };

  ::POINT point;
  if (!::GetCursorPos(&point)) {
    lg(u"failed to get cursor pos");
    return;
  }

  ::RECT rect;
  if (!::GetClientRect(hwnd_, &rect)) {
    lg(u"failed to get window's client rect");
    return;
  }

  ::POINT lefttop{rect.left, rect.top};
  ::POINT rightbottom{rect.right, rect.bottom};
  if (!::ClientToScreen(hwnd_, &lefttop)) {
    lg(u"failed to call ClientToScreen on lefttop");
    return;
  }

  if (!::ClientToScreen(hwnd_, &rightbottom)) {
    lg(u"failed to call ClientToScreen on rightbottom");
    return;
  }

  if (point.x >= lefttop.x && point.y >= lefttop.y &&
      point.x <= rightbottom.x && point.y <= rightbottom.y) {
    ::SetCursor(cursor_->GetHandle());
  }
}

void WinNativeWindow::SetToForeground() {
  if (hwnd_) {
    if (!::SetForegroundWindow(hwnd_))
      throw Win32Error(::GetLastError(),
                       u"Failed to set window to foreground.");
  }
}

IInputMethodContext* WinNativeWindow::GetInputMethodContext() {
  return static_cast<IInputMethodContext*>(input_method_context_.get());
}

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(platform::gui::mouse_buttons::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(platform::gui::mouse_buttons::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(platform::gui::mouse_buttons::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(platform::gui::mouse_buttons::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(platform::gui::mouse_buttons::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(platform::gui::mouse_buttons::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_SYSKEYDOWN:
      if (l_param & (1 << 29)) {
        OnKeyDownInternal(static_cast<int>(w_param));
        *result = 0;
        return true;
      }
      return false;
    case WM_SYSKEYUP:
      if (l_param & (1 << 29)) {
        OnKeyUpInternal(static_cast<int>(w_param));
        *result = 0;
        return true;
      }
      return false;
    case WM_CREATE:
      OnCreateInternal();
      *result = 0;
      return true;
    case WM_MOVE:
      OnMoveInternal(LOWORD(l_param), HIWORD(l_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;
    case WM_IME_SETCONTEXT:
      l_param &= ~ISC_SHOWUICOMPOSITIONWINDOW;
      *result = ::DefWindowProcW(hwnd, msg, w_param, l_param);
      return true;
    // We must block these message from DefWindowProc or it will create
    // an ugly composition window.
    case WM_IME_STARTCOMPOSITION:
    case WM_IME_COMPOSITION:
      *result = 0;
      return true;
    case WM_DPICHANGED: {
      dpi_ = static_cast<float>(LOWORD(w_param));
      const RECT* suggest_rect = reinterpret_cast<const RECT*>(l_param);
      window_render_target_->SetDpi(dpi_, dpi_);
      SetWindowPos(hwnd_, NULL, suggest_rect->left, suggest_rect->top,
                   suggest_rect->right - suggest_rect->left,
                   suggest_rect->bottom - suggest_rect->top,
                   SWP_NOZORDER | SWP_NOACTIVATE);
    }
    default:
      return false;
  }
}

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

void WinNativeWindow::RecreateWindow() {
  const auto window_manager = application_->GetWindowManager();
  auto window_class = window_manager->GetGeneralWindowClass();

  hwnd_ = CreateWindowExW(
      0, window_class->GetName(), L"", CalcWindowStyle(style_flag_),
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
      parent_window_ == nullptr ? nullptr : parent_window_->GetWindowHandle(),
      nullptr, application_->GetInstanceHandle(), nullptr);

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

  auto dpi = ::GetDpiForWindow(hwnd_);
  if (dpi == 0)
    throw Win32Error(::GetLastError(), u"Failed to get dpi of window.");
  dpi_ = static_cast<float>(dpi);
  log::Debug(u"Dpi of window is {}.", dpi_);

  window_manager->RegisterWindow(hwnd_, this);

  SetCursor(application_->GetCursorManager()->GetSystemCursor(
      cru::platform::gui::SystemCursorType::Arrow));

  ::SetWindowTextW(hwnd_, title_.WinCStr());

  window_render_target_ =
      std::make_unique<graphics::win::direct::D2DWindowRenderTarget>(
          application_->GetDirectFactory(), hwnd_);
  window_render_target_->SetDpi(dpi_, dpi_);

  input_method_context_ = std::make_unique<WinInputMethodContext>(this);
  input_method_context_->DisableIME();
}

void WinNativeWindow::OnCreateInternal() { create_event_.Raise(nullptr); }

void WinNativeWindow::OnDestroyInternal() {
  destroy_event_.Raise(nullptr);
  application_->GetWindowManager()->UnregisterWindow(hwnd_);
  hwnd_ = nullptr;
}

void WinNativeWindow::OnPaintInternal() {
  paint_event_.Raise(nullptr);
  ValidateRect(hwnd_, nullptr);
  if constexpr (DebugFlags::paint) {
    CRU_LOG_DEBUG(u"A repaint is finished.");
  }
}

void WinNativeWindow::OnMoveInternal(const int new_left, const int new_top) {
  client_rect_.left = PixelToDip(new_left);
  client_rect_.top = PixelToDip(new_top);
}

void WinNativeWindow::OnResizeInternal(const int new_width,
                                       const int new_height) {
  client_rect_.width = PixelToDip(new_width);
  client_rect_.height = PixelToDip(new_height);
  if (!(new_width == 0 && new_height == 0)) {
    window_render_target_->ResizeBuffer(new_width, new_height);
    resize_event_.Raise(Size{PixelToDip(new_width), PixelToDip(new_height)});
  }
}

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

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

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(MouseEnterLeaveType::Enter);
  }

  mouse_move_event_.Raise(PixelToDip(point));
}

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

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

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

void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) {
  const auto dip_point = PixelToDip(point);
  const float d = -((float)delta / 120.f);
  mouse_wheel_event_.Raise({d, dip_point, RetrieveKeyMofifier()});
}

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

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

void WinNativeWindow::OnActivatedInternal() {}

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