aboutsummaryrefslogtreecommitdiff
path: root/src/win/native/window_manager.cpp
blob: e1b14f4b4657bfcce879f7dcd80821fdec6c92d6 (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
#include "window_manager.hpp"

#include "cru/win/native/ui_application.hpp"
#include "cru/win/native/window.hpp"
#include "cru/win/native/window_class.hpp"

namespace cru::platform::native::win {
LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam,
                                 LPARAM lParam) {
  auto window =
      WinUiApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd);

  LRESULT result;
  if (window != nullptr &&
      window->HandleNativeWindowMessage(hWnd, Msg, wParam, lParam, &result))
    return result;

  return DefWindowProc(hWnd, Msg, wParam, lParam);
}

WindowManager::WindowManager(WinUiApplication* application) {
  application_ = application;
  general_window_class_ = std::make_unique<WindowClass>(
      L"CruUIWindowClass", GeneralWndProc, application->GetInstanceHandle());
}

WindowManager::~WindowManager() {
  for (const auto [key, window] : window_map_) delete window;
}

void WindowManager::RegisterWindow(HWND hwnd, WinNativeWindow* window) {
  Expects(window_map_.count(hwnd) == 0);  // The hwnd is already in the map.
  window_map_.emplace(hwnd, window);
}

void WindowManager::UnregisterWindow(HWND hwnd) {
  const auto find_result = window_map_.find(hwnd);
  Expects(find_result != window_map_.end());  // The hwnd is not in the map.
  window_map_.erase(find_result);
  if (window_map_.empty()) application_->RequestQuit(0);
}

WinNativeWindow* WindowManager::FromHandle(HWND hwnd) {
  const auto find_result = window_map_.find(hwnd);
  if (find_result == window_map_.end())
    return nullptr;
  else
    return find_result->second;
}

std::vector<WinNativeWindow*> WindowManager::GetAllWindows() const {
  std::vector<WinNativeWindow*> windows;
  for (auto [key, value] : window_map_) windows.push_back(value);
  return windows;
}
}  // namespace cru::platform::native::win