From b53527fbe50a953ad0e3225cc812eb76b8a1f82d Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 27 Jun 2019 17:02:58 +0800 Subject: ... --- src/win/exception.cpp | 2 +- src/win/graph/direct/CMakeLists.txt | 1 - src/win/graph/direct/exception.cpp | 29 --- src/win/native/CMakeLists.txt | 16 +- src/win/native/dpi_util.hpp | 4 +- src/win/native/god_window.cpp | 12 +- src/win/native/god_window_message.hpp | 2 +- src/win/native/native_window.cpp | 340 ++++++++++++++++++++++++++++++++ src/win/native/timer.cpp | 4 +- src/win/native/timer.hpp | 2 +- src/win/native/ui_application.cpp | 121 ++++++++++++ src/win/native/win_application.cpp | 117 ----------- src/win/native/win_native_window.cpp | 340 -------------------------------- src/win/native/window_class.cpp | 6 +- src/win/native/window_d2d_painter.cpp | 33 ++++ src/win/native/window_d2d_painter.hpp | 21 ++ src/win/native/window_manager.cpp | 15 +- src/win/native/window_manager.hpp | 8 +- src/win/native/window_painter.cpp | 31 --- src/win/native/window_painter.hpp | 21 -- src/win/native/window_render_target.cpp | 12 +- 21 files changed, 557 insertions(+), 580 deletions(-) delete mode 100644 src/win/graph/direct/exception.cpp create mode 100644 src/win/native/native_window.cpp create mode 100644 src/win/native/ui_application.cpp delete mode 100644 src/win/native/win_application.cpp delete mode 100644 src/win/native/win_native_window.cpp create mode 100644 src/win/native/window_d2d_painter.cpp create mode 100644 src/win/native/window_d2d_painter.hpp delete mode 100644 src/win/native/window_painter.cpp delete mode 100644 src/win/native/window_painter.hpp (limited to 'src/win') diff --git a/src/win/exception.cpp b/src/win/exception.cpp index 62305329..4cac66bb 100644 --- a/src/win/exception.cpp +++ b/src/win/exception.cpp @@ -2,7 +2,7 @@ #include "cru/common/format.hpp" -namespace cru::win { +namespace cru::platform::win { using util::Format; inline std::string HResultMakeMessage(HRESULT h_result, diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt index 7cbbb080..7228802c 100644 --- a/src/win/graph/direct/CMakeLists.txt +++ b/src/win/graph/direct/CMakeLists.txt @@ -2,7 +2,6 @@ set(CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graph/direct) add_library(cru_win_graph_direct STATIC brush.cpp - exception.cpp font.cpp geometry.cpp graph_factory.cpp diff --git a/src/win/graph/direct/exception.cpp b/src/win/graph/direct/exception.cpp deleted file mode 100644 index 23c267c1..00000000 --- a/src/win/graph/direct/exception.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "cru/win/graph/direct/exception.hpp" - -#include "cru/common/format.hpp" - -namespace cru::platform::graph::win::direct { -using util::Format; - -inline std::string HResultMakeMessage(HRESULT h_result, - const std::string_view* message) { - char buffer[10]; - sprintf_s(buffer, "%#08x", h_result); - - if (message) - return Format( - "An HResultError is thrown. HRESULT: {}.\nAdditional message: {}\n", - buffer, *message); - else - return Format("An HResultError is thrown. HRESULT: {}.\n", buffer); -} - -HResultError::HResultError(HRESULT h_result) - : PlatformException(HResultMakeMessage(h_result, nullptr)), - h_result_(h_result) {} - -HResultError::HResultError(HRESULT h_result, - const std::string_view& additional_message) - : PlatformException(HResultMakeMessage(h_result, &additional_message)), - h_result_(h_result) {} -} // namespace cru::platform::graph::win::direct diff --git a/src/win/native/CMakeLists.txt b/src/win/native/CMakeLists.txt index a9ee84e7..757eae57 100644 --- a/src/win/native/CMakeLists.txt +++ b/src/win/native/CMakeLists.txt @@ -4,24 +4,26 @@ add_library(cru_win_native STATIC dpi_util.hpp god_window_message.hpp timer.hpp + window_d2d_painter.hpp window_manager.hpp - window_painter.hpp god_window.cpp + native_window.cpp timer.cpp - win_application.cpp - win_native_window.cpp + ui_application.cpp window_class.cpp + window_d2d_painter.cpp window_manager.cpp - window_painter.cpp window_render_target.cpp ) target_sources(cru_win_native PUBLIC + ${CRU_WIN_NATIVE_INCLUDE_DIR}/exception.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/god_window.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/win_application.hpp - ${CRU_WIN_NATIVE_INCLUDE_DIR}/win_native_window.hpp + ${CRU_WIN_NATIVE_INCLUDE_DIR}/native_window.hpp + ${CRU_WIN_NATIVE_INCLUDE_DIR}/platform_id.hpp + ${CRU_WIN_NATIVE_INCLUDE_DIR}/ui_application.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/window_class.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/window_native_message_event_args.hpp ${CRU_WIN_NATIVE_INCLUDE_DIR}/window_render_target.hpp ) -target_link_libraries(cru_win_native PUBLIC cru_win_graph) +target_link_libraries(cru_win_native PUBLIC cru_win_graph_direct) diff --git a/src/win/native/dpi_util.hpp b/src/win/native/dpi_util.hpp index d459e8c6..a642fd79 100644 --- a/src/win/native/dpi_util.hpp +++ b/src/win/native/dpi_util.hpp @@ -5,7 +5,7 @@ // The dpi awareness needs to be implemented in the future. Currently we use 96 // as default. -namespace cru::win::native { +namespace cru::platform::native::win { inline platform::native::Dpi GetDpi() { return platform::native::Dpi{96.0f, 96.0f}; } @@ -33,4 +33,4 @@ inline float PixelToDipX(const int pixel_x) { inline float PixelToDipY(const int pixel_y) { return DipToPixelInternal(pixel_y, GetDpi().y); } -} // namespace cru::win::native +} // namespace cru::platform::native::win diff --git a/src/win/native/god_window.cpp b/src/win/native/god_window.cpp index 58fb663c..5f1d7b53 100644 --- a/src/win/native/god_window.cpp +++ b/src/win/native/god_window.cpp @@ -1,17 +1,17 @@ #include "cru/win/native/god_window.hpp" -#include "cru/win/exception.hpp" -#include "cru/win/native/win_application.hpp" +#include "cru/win/native/exception.hpp" +#include "cru/win/native/ui_application.hpp" #include "cru/win/native/window_class.hpp" #include "god_window_message.hpp" #include "timer.hpp" -namespace cru::win::native { +namespace cru::platform::native::win { constexpr auto god_window_class_name = L"GodWindowClass"; LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - const auto app = WinApplication::GetInstance(); + const auto app = WinUiApplication::GetInstance(); if (app) { LRESULT result; @@ -25,7 +25,7 @@ LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, return DefWindowProcW(hWnd, uMsg, wParam, lParam); } -GodWindow::GodWindow(WinApplication* application) { +GodWindow::GodWindow(WinUiApplication* application) { application_ = application; const auto h_instance = application->GetInstanceHandle(); @@ -70,4 +70,4 @@ bool GodWindow::HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, } return false; } -} // namespace cru::win::native +} // namespace cru::platform::native::win diff --git a/src/win/native/god_window_message.hpp b/src/win/native/god_window_message.hpp index 10268625..591270d9 100644 --- a/src/win/native/god_window_message.hpp +++ b/src/win/native/god_window_message.hpp @@ -1,6 +1,6 @@ #pragma once #include "cru/win/win_pre_config.hpp" -namespace cru::win::native { +namespace cru::platform::native::win { constexpr int invoke_later_message_id = WM_USER + 2000; } diff --git a/src/win/native/native_window.cpp b/src/win/native/native_window.cpp new file mode 100644 index 00000000..fd29f9c5 --- /dev/null +++ b/src/win/native/native_window.cpp @@ -0,0 +1,340 @@ +#include "cru/win/native/native_window.hpp" + +#include "cru/win/graph/direct/graph_factory.hpp" +#include "cru/win/native/exception.hpp" +#include "cru/win/native/ui_application.hpp" +#include "cru/win/native/window_class.hpp" +#include "cru/win/native/window_render_target.hpp" +#include "dpi_util.hpp" +#include "window_d2d_painter.hpp" +#include "window_manager.hpp" + +#include +#include + +namespace cru::platform::native::win { +WinNativeWindow::WinNativeWindow(WinUiApplication* application, + std::shared_ptr 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(graph::win::direct::DirectGraphFactory::GetInstance(), 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); +} +Size WinNativeWindow::GetClientSize() { + if (!IsValid()) return Size{}; + + const auto pixel_rect = GetClientRectPixel(); + return Size(PixelToDipX(pixel_rect.right), + PixelToDipY(pixel_rect.bottom)); +} + +void WinNativeWindow::SetClientSize(const Size& size) { + if (IsValid()) { + const auto window_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); + const auto window_ex_style = + static_cast(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."); + } +} + +Rect WinNativeWindow::GetWindowRect() { + if (!IsValid()) return Rect{}; + + RECT rect; + if (!::GetWindowRect(hwnd_, &rect)) + throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect."); + + return Rect::FromVertices(PixelToDipX(rect.left), PixelToDipY(rect.top), + PixelToDipX(rect.right), + PixelToDipY(rect.bottom)); +} + +void WinNativeWindow::SetWindowRect(const 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."); + } +} + +graph::Painter* WinNativeWindow::BeginPaint() { + return new WindowD2DPainter(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(platform::native::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(platform::native::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(platform::native::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(platform::native::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(platform::native::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(platform::native::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(w_param)); + *result = 0; + return true; + case WM_KEYUP: + OnKeyUpInternal(static_cast(w_param)); + *result = 0; + return true; + case WM_CHAR: + OnCharInternal(static_cast(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; + destroy_event_.Raise(nullptr); + if (delete_this_on_destroy_) + application_->InvokeLater([this] { delete this; }); +} + +void WinNativeWindow::OnPaintInternal() { + paint_event_.Raise(nullptr); + 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( + 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 Point PiToDip(const POINT& pi_point) { + return 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); + } + + mouse_move_event_.Raise(PiToDip(point)); +} + +void WinNativeWindow::OnMouseLeaveInternal() { + is_mouse_in_ = false; + mouse_enter_leave_event_.Raise(false); +} + +void WinNativeWindow::OnMouseDownInternal(platform::native::MouseButton button, + POINT point) { + const auto dip_point = PiToDip(point); + mouse_down_event_.Raise({button, dip_point}); +} + +void WinNativeWindow::OnMouseUpInternal(platform::native::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::native::win diff --git a/src/win/native/timer.cpp b/src/win/native/timer.cpp index 9e3bbde6..ed2ae069 100644 --- a/src/win/native/timer.cpp +++ b/src/win/native/timer.cpp @@ -1,6 +1,6 @@ #include "timer.hpp" -namespace cru::win::native { +namespace cru::platform::native::win { TimerManager::TimerManager(GodWindow* god_window) { god_window_ = god_window; } UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, @@ -25,4 +25,4 @@ std::optional> TimerManager::GetAction( if (find_result == map_.cend()) return std::nullopt; return find_result->second; } -} // namespace cru::win::native +} // namespace cru::platform::native::win diff --git a/src/win/native/timer.hpp b/src/win/native/timer.hpp index f30d9b91..08749768 100644 --- a/src/win/native/timer.hpp +++ b/src/win/native/timer.hpp @@ -9,7 +9,7 @@ #include #include -namespace cru::win::native { +namespace cru::platform::native::win { using TimerAction = std::function; class TimerManager : public Object { diff --git a/src/win/native/ui_application.cpp b/src/win/native/ui_application.cpp new file mode 100644 index 00000000..360f6e41 --- /dev/null +++ b/src/win/native/ui_application.cpp @@ -0,0 +1,121 @@ +#include "cru/win/native/ui_application.hpp" + +#include "cru/win/graph/direct/graph_factory.hpp" +#include "cru/win/native/exception.hpp" +#include "cru/win/native/god_window.hpp" +#include "cru/win/native/native_window.hpp" +#include "god_window_message.hpp" +#include "timer.hpp" +#include "window_manager.hpp" + +#include +#include + +namespace cru::platform::native::win { +namespace { +WinUiApplication* instance = nullptr; +} +} // namespace cru::platform::native::win + +namespace cru::platform::native { +UiApplication* UiApplication::CreateInstance() { + auto& i = + ::cru::platform::native::win::instance; // avoid long namespace prefix + assert(i == nullptr); + i = new win::WinUiApplication(::GetModuleHandleW(nullptr)); + return i; +} + +UiApplication* UiApplication::GetInstance() { + return ::cru::platform::native::win::instance; +} +} // namespace cru::platform::native + +namespace cru::platform::native::win { +WinUiApplication* WinUiApplication::GetInstance() { return instance; } + +WinUiApplication::WinUiApplication(HINSTANCE h_instance) + : h_instance_(h_instance) { + assert(instance == nullptr); + + if (!::IsWindows8OrGreater()) + throw std::runtime_error("Must run on Windows 8 or later."); + + const auto graph_factory = graph::GraphFactory::CreateInstance(); + graph_factory->SetAutoDelete(true); + + god_window_ = std::make_shared(this); + timer_manager_ = std::make_shared(god_window_.get()); + window_manager_ = std::make_shared(this); +} + +WinUiApplication::~WinUiApplication() { instance = nullptr; } + +int WinUiApplication::Run() { + MSG msg; + while (GetMessageW(&msg, nullptr, 0, 0)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + + for (const auto& handler : quit_handlers_) handler(); + + if (auto_delete_) delete this; + + return static_cast(msg.wParam); +} + +void WinUiApplication::Quit(const int quit_code) { + ::PostQuitMessage(quit_code); +} + +void WinUiApplication::AddOnQuitHandler(const std::function& handler) { + quit_handlers_.push_back(handler); +} + +void WinUiApplication::InvokeLater(const std::function& action) { + // copy the action to a safe place + auto p_action_copy = new std::function(action); + + if (PostMessageW(GetGodWindow()->GetHandle(), invoke_later_message_id, + reinterpret_cast(p_action_copy), 0) == 0) + throw Win32Error(::GetLastError(), "InvokeLater failed to post message."); +} + +unsigned long WinUiApplication::SetTimeout( + std::chrono::milliseconds milliseconds, + const std::function& action) { + return static_cast(timer_manager_->CreateTimer( + static_cast(milliseconds.count()), false, action)); +} + +unsigned long WinUiApplication::SetInterval( + std::chrono::milliseconds milliseconds, + const std::function& action) { + return static_cast(timer_manager_->CreateTimer( + static_cast(milliseconds.count()), true, action)); +} + +void WinUiApplication::CancelTimer(unsigned long id) { + timer_manager_->KillTimer(static_cast(id)); +} + +std::vector WinUiApplication::GetAllWindow() { + const auto&& windows = window_manager_->GetAllWindows(); + std::vector result; + for (const auto w : windows) { + result.push_back(static_cast(w)); + } + return result; +} + +NativeWindow* WinUiApplication::CreateWindow(NativeWindow* parent) { + WinNativeWindow* p = nullptr; + if (parent != nullptr) { + p = dynamic_cast(parent); + assert(p); + } + return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), + WS_OVERLAPPEDWINDOW, p); +} +} // namespace cru::platform::native::win diff --git a/src/win/native/win_application.cpp b/src/win/native/win_application.cpp deleted file mode 100644 index 586af331..00000000 --- a/src/win/native/win_application.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "cru/win/native/win_application.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_graph_factory.hpp" -#include "cru/win/native/god_window.hpp" -#include "cru/win/native/win_native_window.hpp" -#include "god_window_message.hpp" -#include "timer.hpp" -#include "window_manager.hpp" - -#include -#include - -namespace cru::win::native { -namespace { -WinApplication* instance = nullptr; -} -} // namespace cru::win::native - -namespace cru::platform::native { -IUiApplication* IUiApplication::CreateInstance() { - auto& i = ::cru::win::native::instance; // avoid long namespace prefix - assert(i == nullptr); - i = new win::native::WinApplication(::GetModuleHandleW(nullptr)); - return i; -} - -IUiApplication* IUiApplication::GetInstance() { - return ::cru::win::native::instance; -} -} // namespace cru::platform::native - -namespace cru::win::native { -WinApplication* WinApplication::GetInstance() { return instance; } - -WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { - assert(instance == nullptr); - - if (!::IsWindows8OrGreater()) - throw std::runtime_error("Must run on Windows 8 or later."); - - const auto graph_factory = platform::graph::IGraphFactory::CreateInstance(); - graph_factory->SetAutoDelete(true); - - god_window_ = std::make_shared(this); - timer_manager_ = std::make_shared(god_window_.get()); - window_manager_ = std::make_shared(this); -} - -WinApplication::~WinApplication() { instance = nullptr; } - -int WinApplication::Run() { - MSG msg; - while (GetMessageW(&msg, nullptr, 0, 0)) { - TranslateMessage(&msg); - DispatchMessageW(&msg); - } - - for (const auto& handler : quit_handlers_) handler(); - - if (auto_delete_) delete this; - - return static_cast(msg.wParam); -} - -void WinApplication::Quit(const int quit_code) { ::PostQuitMessage(quit_code); } - -void WinApplication::AddOnQuitHandler(const std::function& handler) { - quit_handlers_.push_back(handler); -} - -void WinApplication::InvokeLater(const std::function& action) { - // copy the action to a safe place - auto p_action_copy = new std::function(action); - - if (PostMessageW(GetGodWindow()->GetHandle(), invoke_later_message_id, - reinterpret_cast(p_action_copy), 0) == 0) - throw Win32Error(::GetLastError(), "InvokeLater failed to post message."); -} - -unsigned long WinApplication::SetTimeout(std::chrono::milliseconds milliseconds, - const std::function& action) { - return static_cast(timer_manager_->CreateTimer( - static_cast(milliseconds.count()), false, action)); -} - -unsigned long WinApplication::SetInterval( - std::chrono::milliseconds milliseconds, - const std::function& action) { - return static_cast(timer_manager_->CreateTimer( - static_cast(milliseconds.count()), true, action)); -} - -void WinApplication::CancelTimer(unsigned long id) { - timer_manager_->KillTimer(static_cast(id)); -} - -std::vector WinApplication::GetAllWindow() { - const auto&& windows = window_manager_->GetAllWindows(); - std::vector result; - for (const auto w : windows) { - result.push_back(static_cast(w)); - } - return result; -} - -platform::native::INativeWindow* WinApplication::CreateWindow( - platform::native::INativeWindow* parent) { - WinNativeWindow* p = nullptr; - if (parent != nullptr) { - p = dynamic_cast(parent); - assert(p); - } - return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), - WS_OVERLAPPEDWINDOW, p); -} -} // namespace cru::win::native diff --git a/src/win/native/win_native_window.cpp b/src/win/native/win_native_window.cpp deleted file mode 100644 index 9ca37321..00000000 --- a/src/win/native/win_native_window.cpp +++ /dev/null @@ -1,340 +0,0 @@ -#include "cru/win/native/win_native_window.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_graph_factory.hpp" -#include "cru/win/native/win_application.hpp" -#include "cru/win/native/window_class.hpp" -#include "cru/win/native/window_render_target.hpp" -#include "dpi_util.hpp" -#include "window_manager.hpp" -#include "window_painter.hpp" - -#include -#include - -namespace cru::win::native { -WinNativeWindow::WinNativeWindow(WinApplication* application, - std::shared_ptr 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(graph::WinGraphFactory::GetInstance(), 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(GetWindowLongPtr(hwnd_, GWL_STYLE)); - const auto window_ex_style = - static_cast(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."); - } -} - -platform::graph::IPainter* WinNativeWindow::BeginPaint() { - return new WindowPainter(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(platform::native::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(platform::native::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(platform::native::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(platform::native::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(platform::native::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(platform::native::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(w_param)); - *result = 0; - return true; - case WM_KEYUP: - OnKeyUpInternal(static_cast(w_param)); - *result = 0; - return true; - case WM_CHAR: - OnCharInternal(static_cast(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; - destroy_event_.Raise(nullptr); - if (delete_this_on_destroy_) - application_->InvokeLater([this] { delete this; }); -} - -void WinNativeWindow::OnPaintInternal() { - paint_event_.Raise(nullptr); - 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); - } - - mouse_move_event_.Raise(PiToDip(point)); -} - -void WinNativeWindow::OnMouseLeaveInternal() { - is_mouse_in_ = false; - mouse_enter_leave_event_.Raise(false); -} - -void WinNativeWindow::OnMouseDownInternal(platform::native::MouseButton button, - POINT point) { - const auto dip_point = PiToDip(point); - mouse_down_event_.Raise({button, dip_point}); -} - -void WinNativeWindow::OnMouseUpInternal(platform::native::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::win::native diff --git a/src/win/native/window_class.cpp b/src/win/native/window_class.cpp index 6c7d0d3d..d69160ab 100644 --- a/src/win/native/window_class.cpp +++ b/src/win/native/window_class.cpp @@ -1,8 +1,8 @@ #include "cru/win/native/window_class.hpp" -#include "cru/win/exception.hpp" +#include "cru/win/native/exception.hpp" -namespace cru::win::native { +namespace cru::platform::native::win { WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, HINSTANCE h_instance) : name_(name) { @@ -25,4 +25,4 @@ WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, if (atom_ == 0) throw Win32Error(::GetLastError(), "Failed to create window class."); } -} // namespace cru::win::native +} // namespace cru::platform::native::win diff --git a/src/win/native/window_d2d_painter.cpp b/src/win/native/window_d2d_painter.cpp new file mode 100644 index 00000000..16d276ef --- /dev/null +++ b/src/win/native/window_d2d_painter.cpp @@ -0,0 +1,33 @@ +#include "window_d2d_painter.hpp" + +#include "cru/win/graph/direct/direct_factory.hpp" +#include "cru/win/graph/direct/exception.hpp" +#include "cru/win/native/window_render_target.hpp" + +#include + +namespace cru::platform::native::win { +using namespace cru::platform::graph::win::direct; + +WindowD2DPainter::WindowD2DPainter(WinNativeWindow* window) + : D2DPainter(window->GetWindowRenderTarget() + ->GetWinNativeFactory() + ->GetD2D1DeviceContext()), + window_(window) { + window->GetWindowRenderTarget()->SetAsTarget(); + window->GetWindowRenderTarget() + ->GetWinNativeFactory() + ->GetD2D1DeviceContext() + ->BeginDraw(); +} + +WindowD2DPainter::~WindowD2DPainter() { EndDraw(); } + +void WindowD2DPainter::DoEndDraw() { + ThrowIfFailed(window_->GetWindowRenderTarget() + ->GetWinNativeFactory() + ->GetD2D1DeviceContext() + ->EndDraw()); + window_->GetWindowRenderTarget()->Present(); +} +} // namespace cru::platform::native::win diff --git a/src/win/native/window_d2d_painter.hpp b/src/win/native/window_d2d_painter.hpp new file mode 100644 index 00000000..1c90e8ab --- /dev/null +++ b/src/win/native/window_d2d_painter.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "cru/win/graph/direct/painter.hpp" +#include "cru/win/native/native_window.hpp" + +namespace cru::platform::native::win { +class WindowD2DPainter : public graph::win::direct::D2DPainter { + public: + explicit WindowD2DPainter(WinNativeWindow* window); + WindowD2DPainter(const WindowD2DPainter& other) = delete; + WindowD2DPainter& operator=(const WindowD2DPainter& other) = delete; + WindowD2DPainter(WindowD2DPainter&& other) = delete; + WindowD2DPainter& operator=(WindowD2DPainter&& other) = delete; + ~WindowD2DPainter() override; + + protected: + void DoEndDraw() override; + + private: + WinNativeWindow* window_; +}; +} // namespace cru::win::native diff --git a/src/win/native/window_manager.cpp b/src/win/native/window_manager.cpp index 5fea5a27..7f70d3cc 100644 --- a/src/win/native/window_manager.cpp +++ b/src/win/native/window_manager.cpp @@ -1,16 +1,16 @@ #include "window_manager.hpp" -#include "cru/win/native/win_application.hpp" -#include "cru/win/native/win_native_window.hpp" +#include "cru/win/native/ui_application.hpp" +#include "cru/win/native/native_window.hpp" #include "cru/win/native/window_class.hpp" #include -namespace cru::win::native { +namespace cru::platform::native::win { LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { auto window = - WinApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd); + WinUiApplication::GetInstance()->GetWindowManager()->FromHandle(hWnd); LRESULT result; if (window != nullptr && @@ -20,15 +20,14 @@ LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, return DefWindowProc(hWnd, Msg, wParam, lParam); } -WindowManager::WindowManager(WinApplication* application) { +WindowManager::WindowManager(WinUiApplication* application) { application_ = application; general_window_class_ = std::make_shared( L"CruUIWindowClass", GeneralWndProc, application->GetInstanceHandle()); } WindowManager::~WindowManager() { - for (const auto [key, window] : window_map_) - delete window; + for (const auto [key, window] : window_map_) delete window; } void WindowManager::RegisterWindow(HWND hwnd, WinNativeWindow* window) { @@ -56,4 +55,4 @@ std::vector WindowManager::GetAllWindows() const { for (auto [key, value] : window_map_) windows.push_back(value); return windows; } -} // namespace cru::win::native +} // namespace cru::platform::native::win diff --git a/src/win/native/window_manager.hpp b/src/win/native/window_manager.hpp index 8fab9cfc..a0661556 100644 --- a/src/win/native/window_manager.hpp +++ b/src/win/native/window_manager.hpp @@ -7,14 +7,14 @@ #include #include -namespace cru::win::native { -class WinApplication; +namespace cru::platform::native::win { +class WinUiApplication; class WinNativeWindow; class WindowClass; class WindowManager : public Object { public: - WindowManager(WinApplication* application); + WindowManager(WinUiApplication* application); WindowManager(const WindowManager& other) = delete; WindowManager(WindowManager&& other) = delete; WindowManager& operator=(const WindowManager& other) = delete; @@ -43,7 +43,7 @@ class WindowManager : public Object { std::vector GetAllWindows() const; private: - WinApplication* application_; + WinUiApplication* application_; std::shared_ptr general_window_class_; std::map window_map_; diff --git a/src/win/native/window_painter.cpp b/src/win/native/window_painter.cpp deleted file mode 100644 index 463be128..00000000 --- a/src/win/native/window_painter.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "window_painter.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_native_factory.hpp" -#include "cru/win/native/window_render_target.hpp" - -#include - -namespace cru::win::native { -WindowPainter::WindowPainter(WinNativeWindow* window) - : WinPainter(window->GetWindowRenderTarget() - ->GetWinNativeFactory() - ->GetD2D1DeviceContext()), - window_(window) { - window->GetWindowRenderTarget()->SetAsTarget(); - window->GetWindowRenderTarget() - ->GetWinNativeFactory() - ->GetD2D1DeviceContext() - ->BeginDraw(); -} - -WindowPainter::~WindowPainter() { End(); } - -void WindowPainter::DoEndDraw() { - ThrowIfFailed(window_->GetWindowRenderTarget() - ->GetWinNativeFactory() - ->GetD2D1DeviceContext() - ->EndDraw()); - window_->GetWindowRenderTarget()->Present(); -} -} // namespace cru::win::native diff --git a/src/win/native/window_painter.hpp b/src/win/native/window_painter.hpp deleted file mode 100644 index 78731ef3..00000000 --- a/src/win/native/window_painter.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "cru/win/graph/win_painter.hpp" -#include "cru/win/native/win_native_window.hpp" - -namespace cru::win::native { -class WindowPainter : public graph::WinPainter { - public: - explicit WindowPainter(WinNativeWindow* window); - WindowPainter(const WindowPainter& other) = delete; - WindowPainter& operator=(const WindowPainter& other) = delete; - WindowPainter(WindowPainter&& other) = delete; - WindowPainter& operator=(WindowPainter&& other) = delete; - ~WindowPainter() override; - - protected: - void DoEndDraw() override; - - private: - WinNativeWindow* window_; -}; -} // namespace cru::win::native diff --git a/src/win/native/window_render_target.cpp b/src/win/native/window_render_target.cpp index 606b51f8..426afdf6 100644 --- a/src/win/native/window_render_target.cpp +++ b/src/win/native/window_render_target.cpp @@ -1,14 +1,14 @@ #include "cru/win/native/window_render_target.hpp" -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/direct/direct_factory.hpp" +#include "cru/win/graph/direct/exception.hpp" #include "dpi_util.hpp" #include -namespace cru::win::native { -WindowRenderTarget::WindowRenderTarget(graph::IWinNativeFactory* factory, - HWND hwnd) { +namespace cru::platform::native::win { +using namespace cru::platform::graph::win::direct; +WindowRenderTarget::WindowRenderTarget(IDirectFactory* factory, HWND hwnd) { this->factory_ = factory; const auto d3d11_device = factory->GetD3D11Device(); @@ -86,4 +86,4 @@ void WindowRenderTarget::CreateTargetBitmap() { ThrowIfFailed(factory_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); } -} // namespace cru::win::native +} // namespace cru::platform::native::win -- cgit v1.2.3