diff options
author | crupest <crupest@outlook.com> | 2019-03-31 19:48:20 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-03-31 19:48:20 +0800 |
commit | 8ca0873597eb05a2f120d3ea107660abcff4533c (patch) | |
tree | f2089ad1a420ae0f21ba0d84b5031de3b5e489ca | |
parent | 9cc0f5d9192288116443254d790aa9ab36572b8d (diff) | |
download | cru-8ca0873597eb05a2f120d3ea107660abcff4533c.tar.gz cru-8ca0873597eb05a2f120d3ea107660abcff4533c.tar.bz2 cru-8ca0873597eb05a2f120d3ea107660abcff4533c.zip |
...
-rw-r--r-- | include/cru/common/event.hpp | 46 | ||||
-rw-r--r-- | include/cru/platform/basic_types.hpp | 11 | ||||
-rw-r--r-- | include/cru/platform/dpi_util.hpp | 32 | ||||
-rw-r--r-- | include/cru/platform/native_window.hpp | 19 | ||||
-rw-r--r-- | include/cru/platform/ui_applicaition.hpp | 10 | ||||
-rw-r--r-- | include/cru/platform/win/god_window.hpp | 5 | ||||
-rw-r--r-- | include/cru/platform/win/win_application.hpp | 10 | ||||
-rw-r--r-- | include/cru/platform/win/win_native_window.hpp | 123 | ||||
-rw-r--r-- | include/cru/platform/win/window_native_message_event_args.hpp (renamed from src/ui/events/window_event.hpp) | 35 | ||||
-rw-r--r-- | src/cru_event.hpp | 68 | ||||
-rw-r--r-- | src/platform_win/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/platform_win/god_window.cpp | 26 | ||||
-rw-r--r-- | src/platform_win/win_native_window.cpp | 331 | ||||
-rw-r--r-- | src/platform_win/window_manager.cpp | 53 | ||||
-rw-r--r-- | src/platform_win/window_manager.hpp | 52 | ||||
-rw-r--r-- | src/ui/window.cpp | 154 | ||||
-rw-r--r-- | src/ui/window.hpp | 65 |
17 files changed, 720 insertions, 324 deletions
diff --git a/include/cru/common/event.hpp b/include/cru/common/event.hpp new file mode 100644 index 00000000..ce014fb8 --- /dev/null +++ b/include/cru/common/event.hpp @@ -0,0 +1,46 @@ +#pragma once +#include "base.hpp" + +#include <functional> +#include <map> +#include <utility> + +namespace cru { +// A non-copyable non-movable Event class. +// It stores a list of event handlers. +template <typename... TArgs> +class Event { + public: + using EventHandler = std::function<void(TArgs...)>; + using EventHandlerToken = long; + + Event() = default; + Event(const Event&) = delete; + Event& operator=(const Event&) = delete; + Event(Event&&) = delete; + Event& operator=(Event&&) = delete; + ~Event() = default; + + EventHandlerToken AddHandler(const EventHandler& handler) { + const auto token = current_token_++; + handlers_.emplace(token, handler); + return token; + } + + void RemoveHandler(const EventHandlerToken token) { + auto find_result = handlers_.find(token); + if (find_result != handlers_.cend()) handlers_.erase(find_result); + } + + template <typename... Args> + void Raise(Args&&... args) { + for (const auto& handler : handlers_) + (handler.second)(std::forward<Args>(args)...); + } + + private: + std::map<EventHandlerToken, EventHandler> handlers_; + + EventHandlerToken current_token_ = 0; +}; +} // namespace cru diff --git a/include/cru/platform/basic_types.hpp b/include/cru/platform/basic_types.hpp new file mode 100644 index 00000000..67637b83 --- /dev/null +++ b/include/cru/platform/basic_types.hpp @@ -0,0 +1,11 @@ +#include "cru/common/pre_config.hpp" + +namespace cru::platform { +struct Dpi { + float x; + float y; +}; + +enum class MouseButton { Left, Right, Middle }; + +} // namespace cru::platform diff --git a/include/cru/platform/dpi_util.hpp b/include/cru/platform/dpi_util.hpp new file mode 100644 index 00000000..3c0ae6ca --- /dev/null +++ b/include/cru/platform/dpi_util.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "ui_applicaition.hpp" + +namespace cru::platform { +inline Dpi GetDpi() { + return UiApplication::GetInstance()->GetDpi(); +} + +inline int DipToPixelInternal(const float dip, const float dpi) { + return static_cast<int>(dip * dpi / 96.0f); +} + +inline int DipToPixelX(const float dip_x) { + return DipToPixelInternal(dip_x, GetDpi().x); +} + +inline int DipToPixelY(const float dip_y) { + return DipToPixelInternal(dip_y, GetDpi().y); +} + +inline float DipToPixelInternal(const int pixel, const float dpi) { + return static_cast<float>(pixel) * 96.0f / dpi; +} + +inline float PixelToDipX(const int pixel_x) { + return DipToPixelInternal(pixel_x, GetDpi().x); +} + +inline float PixelToDipY(const int pixel_y) { + return DipToPixelInternal(pixel_y, GetDpi().y); +} +} // namespace cru::platform diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native_window.hpp index 1ed9a25e..5e8897ab 100644 --- a/include/cru/platform/native_window.hpp +++ b/include/cru/platform/native_window.hpp @@ -1,18 +1,24 @@ #pragma once #include "cru/common/base.hpp" + +#include "basic_types.hpp" +#include "cru/common/event.hpp" #include "cru/common/ui_base.hpp" namespace cru::platform { struct Painter; struct NativeWindow : public virtual Interface { + // Return if the window is still valid, that is, hasn't been closed or + // destroyed. virtual bool IsValid() = 0; + virtual void SetDeleteThisOnDestroy(bool value) = 0; virtual void Close() = 0; virtual NativeWindow* GetParent() = 0; - virtual bool IsVisible() const = 0; + virtual bool IsVisible() = 0; virtual void SetVisible(bool is_visible) = 0; virtual ui::Size GetClientSize() = 0; @@ -27,5 +33,16 @@ struct NativeWindow : public virtual Interface { virtual void SetWindowRect(const ui::Rect& rect) = 0; virtual Painter* GetPainter() = 0; + + virtual Event<>* DestroyEvent() = 0; + virtual Event<ui::Size>* ResizeEvent() = 0; + virtual Event<>* PaintEvent() = 0; + virtual Event<bool>* FocusEvent() = 0; + virtual Event<bool>* MouseEnterLeaveEvent() = 0; + virtual Event<ui::Point>* MouseMoveEvent() = 0; + virtual Event<MouseButton, ui::Point>* MouseDownEvent() = 0; + virtual Event<MouseButton, ui::Point>* MouseUpEvent() = 0; + virtual Event<int>* KeyDownEvent() = 0; + virtual Event<int>* KeyUpEvent() = 0; }; } // namespace cru::platform diff --git a/include/cru/platform/ui_applicaition.hpp b/include/cru/platform/ui_applicaition.hpp index 1345d38c..e149166b 100644 --- a/include/cru/platform/ui_applicaition.hpp +++ b/include/cru/platform/ui_applicaition.hpp @@ -1,8 +1,11 @@ #pragma once #include "cru/common/base.hpp" +#include "basic_types.hpp" + #include <chrono> #include <functional> +#include <vector> namespace cru::platform { struct NativeWindow; @@ -16,13 +19,16 @@ struct UiApplication : public virtual Interface { virtual void InvokeLater(const std::function<void()>& action) = 0; virtual unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - const std::function<void()>& action) = 0; + const std::function<void()>& action) = 0; virtual unsigned long SetInterval(std::chrono::milliseconds milliseconds, - const std::function<void()>& action) = 0; + const std::function<void()>& action) = 0; virtual void CancelTimer(unsigned long id) = 0; + virtual std::vector<NativeWindow*> GetAllWindow() = 0; virtual NativeWindow* CreateWindow() = 0; + virtual Dpi GetDpi() = 0; + virtual GraphFactory* GetGraphFactory() = 0; }; } // namespace cru::platform diff --git a/include/cru/platform/win/god_window.hpp b/include/cru/platform/win/god_window.hpp index 534dfedb..95a253e9 100644 --- a/include/cru/platform/win/god_window.hpp +++ b/include/cru/platform/win/god_window.hpp @@ -2,7 +2,6 @@ #include "win_pre_config.hpp" #include <memory> -#include <optional> #include "cru/common/base.hpp" @@ -21,8 +20,8 @@ class GodWindow : public Object { HWND GetHandle() const { return hwnd_; } - std::optional<LRESULT> HandleGodWindowMessage(HWND hwnd, int msg, - WPARAM w_param, LPARAM l_param); + bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); private: WinApplication* application_; diff --git a/include/cru/platform/win/win_application.hpp b/include/cru/platform/win/win_application.hpp index 363ae170..fcc0a7c9 100644 --- a/include/cru/platform/win/win_application.hpp +++ b/include/cru/platform/win/win_application.hpp @@ -9,6 +9,7 @@ namespace cru::platform::win { class GodWindow; class TimerManager; +class WindowManager; class WinApplication : public Object, public virtual UiApplication { public: @@ -33,21 +34,24 @@ class WinApplication : public Object, public virtual UiApplication { void InvokeLater(const std::function<void()>& action) override; unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - const std::function<void()>& action) override; + const std::function<void()>& action) override; unsigned long SetInterval(std::chrono::milliseconds milliseconds, - const std::function<void()>& action) override; + const std::function<void()>& action) override; void CancelTimer(unsigned long id) override; HINSTANCE GetInstanceHandle() const { return h_instance_; } GodWindow* GetGodWindow() const { return god_window_.get(); } - TimerManager* GetTimerManager() const; + TimerManager* GetTimerManager() const { return timer_manager_.get(); } + + WindowManager* GetWindowManager() const { return window_manager_.get(); } private: HINSTANCE h_instance_; std::shared_ptr<GodWindow> god_window_; std::shared_ptr<TimerManager> timer_manager_; + std::shared_ptr<WindowManager> window_manager_; }; } // namespace cru::platform::win diff --git a/include/cru/platform/win/win_native_window.hpp b/include/cru/platform/win/win_native_window.hpp new file mode 100644 index 00000000..9deac767 --- /dev/null +++ b/include/cru/platform/win/win_native_window.hpp @@ -0,0 +1,123 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../native_window.hpp" +#include "window_native_message_event_args.hpp" + +#include <memory> + +namespace cru::platform::win { +class WinApplication; +class WindowClass; +class WindowManager; + +class WinNativeWindow : public Object, public virtual NativeWindow { + public: + WinNativeWindow(WinApplication* application, + std::shared_ptr<WindowClass> window_class, DWORD window_style, + WinNativeWindow* parent); + WinNativeWindow(const WinNativeWindow& other) = delete; + WinNativeWindow(WinNativeWindow&& other) = delete; + WinNativeWindow& operator=(const WinNativeWindow& other) = delete; + WinNativeWindow& operator=(WinNativeWindow&& other) = delete; + ~WinNativeWindow() override; + + bool IsValid() override; + void SetDeleteThisOnDestroy(bool value) override; + + void Close() override; + + NativeWindow* GetParent() override { return parent_window_; } + + bool IsVisible() override; + void SetVisible(bool is_visible) override; + + ui::Size GetClientSize() override; + void SetClientSize(const ui::Size& size) override; + + // Get the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + ui::Rect GetWindowRect() override; + + // Set the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + void SetWindowRect(const ui::Rect& rect) override; + + Event<>* DestroyEvent() override { return &destroy_event_; } + Event<ui::Size>* ResizeEvent() override { return &resize_event_; } + Event<>* PaintEvent() override { return &paint_event_; } + Event<bool>* FocusEvent() override { return &focus_event_; } + Event<bool>* MouseEnterLeaveEvent() override { + return &mouse_enter_leave_event_; + } + Event<ui::Point>* MouseMoveEvent() override { return &mouse_move_event_; } + Event<MouseButton, ui::Point>* MouseDownEvent() override { + return &mouse_down_event_; + } + Event<MouseButton, ui::Point>* MouseUpEvent() override { + return &mouse_up_event_; + } + Event<int>* KeyDownEvent() override { return &key_down_event_; } + Event<int>* KeyUpEvent() override { return &key_up_event_; } + + Event<WindowNativeMessageEventArgs&>* NativeMessageEvent() { + return &native_message_event_; + } + + // Get the handle of the window. Return null if window is invalid. + HWND GetWindowHandle() const { return hwnd_; } + + bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + private: + // Get the client rect in pixel. + RECT GetClientRectPixel(); + + //*************** region: native messages *************** + + void OnDestroyInternal(); + void OnPaintInternal(); + void OnResizeInternal(int new_width, int new_height); + + void OnSetFocusInternal(); + void OnKillFocusInternal(); + + void OnMouseMoveInternal(POINT point); + void OnMouseLeaveInternal(); + void OnMouseDownInternal(MouseButton button, POINT point); + void OnMouseUpInternal(MouseButton button, POINT point); + + void OnMouseWheelInternal(short delta, POINT point); + void OnKeyDownInternal(int virtual_code); + void OnKeyUpInternal(int virtual_code); + void OnCharInternal(wchar_t c); + + void OnActivatedInternal(); + void OnDeactivatedInternal(); + + private: + WinApplication* application_; + + bool delete_this_on_destroy_ = true; + + HWND hwnd_; + WinNativeWindow* parent_window_; + + bool has_focus_ = false; + bool is_mouse_in_ = false; + + Event<> destroy_event_; + Event<ui::Size> resize_event_; + Event<> paint_event_; + Event<bool> focus_event_; + Event<bool> mouse_enter_leave_event_; + Event<ui::Point> mouse_move_event_; + Event<MouseButton, ui::Point> mouse_down_event_; + Event<MouseButton, ui::Point> mouse_up_event_; + Event<int> key_down_event_; + Event<int> key_up_event_; + + Event<WindowNativeMessageEventArgs&> native_message_event_; +}; +} // namespace cru::platform::win diff --git a/src/ui/events/window_event.hpp b/include/cru/platform/win/window_native_message_event_args.hpp index 21c644af..b3419c24 100644 --- a/src/ui/events/window_event.hpp +++ b/include/cru/platform/win/window_native_message_event_args.hpp @@ -1,25 +1,20 @@ #pragma once -#include "pre.hpp" +#include "win_pre_config.hpp" -#include <Windows.h> +#include "cru/common/base.hpp" -#include "ui_event.hpp" - -namespace cru::ui::events { +namespace cru::platform::win { struct WindowNativeMessage { HWND hwnd; - int msg; + UINT msg; WPARAM w_param; LPARAM l_param; }; -class WindowNativeMessageEventArgs : public UiEventArgs { +class WindowNativeMessageEventArgs : public Object { public: - WindowNativeMessageEventArgs(Object* sender, Object* original_sender, - const WindowNativeMessage& message) - : UiEventArgs(sender, original_sender), - message_(message), - result_(std::nullopt) {} + WindowNativeMessageEventArgs(const WindowNativeMessage& message) + : message_(message) {} WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = default; WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; @@ -31,12 +26,20 @@ class WindowNativeMessageEventArgs : public UiEventArgs { WindowNativeMessage GetWindowMessage() const { return message_; } - std::optional<LRESULT> GetResult() const { return result_; } + LRESULT GetResult() const { return result_; } + void SetResult(LRESULT result) { result_ = result; } + + bool IsHandled() const { return handled_; } + void SetHandled(bool handled) { handled_ = handled; } - void SetResult(const std::optional<LRESULT> result) { result_ = result; } + void HandledAndSetResult(LRESULT result) { + handled_ = true; + result_ = result; + } private: WindowNativeMessage message_; - std::optional<LRESULT> result_; + LRESULT result_; + bool handled_ = false; }; -} +} // namespace cru::platform::win diff --git a/src/cru_event.hpp b/src/cru_event.hpp deleted file mode 100644 index a669695f..00000000 --- a/src/cru_event.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include <functional> -#include <map> -#include <type_traits> - -#include "base.hpp" - -namespace cru { -// Base class of all event args. -class BasicEventArgs : public Object { - public: - explicit BasicEventArgs(Object* sender) : sender_(sender) {} - BasicEventArgs(const BasicEventArgs& other) = default; - BasicEventArgs(BasicEventArgs&& other) = default; - BasicEventArgs& operator=(const BasicEventArgs& other) = default; - BasicEventArgs& operator=(BasicEventArgs&& other) = default; - ~BasicEventArgs() override = default; - - // Get the sender of the event. - Object* GetSender() const { return sender_; } - - private: - Object* sender_; -}; - -// A non-copyable non-movable Event class. -// It stores a list of event handlers. -// TArgsType must be subclass of BasicEventArgs. -template <typename TArgsType> -class Event { - public: - static_assert(std::is_base_of_v<BasicEventArgs, TArgsType>, - "TArgsType must be subclass of BasicEventArgs."); - - using ArgsType = TArgsType; - using EventHandler = std::function<void(ArgsType&)>; - using EventHandlerToken = long; - - Event() = default; - Event(const Event&) = delete; - Event& operator=(const Event&) = delete; - Event(Event&&) = delete; - Event& operator=(Event&&) = delete; - ~Event() = default; - - EventHandlerToken AddHandler(const EventHandler& handler) { - const auto token = current_token_++; - handlers_.emplace(token, handler); - return token; - } - - void RemoveHandler(const EventHandlerToken token) { - auto find_result = handlers_.find(token); - if (find_result != handlers_.cend()) handlers_.erase(find_result); - } - - void Raise(ArgsType& args) { - for (const auto& handler : handlers_) (handler.second)(args); - } - - private: - std::map<EventHandlerToken, EventHandler> handlers_; - - EventHandlerToken current_token_ = 0; -}; -} // namespace cru diff --git a/src/platform_win/CMakeLists.txt b/src/platform_win/CMakeLists.txt index fbcf1c00..9e7d8a89 100644 --- a/src/platform_win/CMakeLists.txt +++ b/src/platform_win/CMakeLists.txt @@ -4,7 +4,9 @@ add_library(cru_platform_win STATIC god_window.cpp timer.cpp win_application.cpp - window_class.cpp) + win_native_window.cpp + window_class.cpp + window_manager.cpp) target_include_directories(cru_platform_win PUBLIC ${PROJECT_SOURCE_DIR}/include .) target_link_libraries(cru_platform_win PRIVATE D3D11 D2d1 DWrite) target_compile_definitions(cru_platform_win PUBLIC UNICODE _UNICODE) # use unicode diff --git a/src/platform_win/god_window.cpp b/src/platform_win/god_window.cpp index 2b4fbe48..0cb1a0e4 100644 --- a/src/platform_win/god_window.cpp +++ b/src/platform_win/god_window.cpp @@ -14,10 +14,11 @@ LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, const auto app = WinApplication::GetInstance(); if (app) { - const auto result = - app->GetGodWindow()->HandleGodWindowMessage(hWnd, uMsg, wParam, lParam); - if (result.has_value()) - return result.value(); + LRESULT result; + const auto handled = + app->GetGodWindow()->HandleGodWindowMessage(hWnd, uMsg, wParam, lParam, &result); + if (handled) + return result; else return DefWindowProcW(hWnd, uMsg, wParam, lParam); } else @@ -41,15 +42,15 @@ GodWindow::GodWindow(WinApplication* application) { GodWindow::~GodWindow() { ::DestroyWindow(hwnd_); } -std::optional<LRESULT> GodWindow::HandleGodWindowMessage(HWND hwnd, int msg, - WPARAM w_param, - LPARAM l_param) { +bool GodWindow::HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result) { switch (msg) { case invoke_later_message_id: { const auto p_action = reinterpret_cast<std::function<void()>*>(w_param); (*p_action)(); delete p_action; - return 0; + *result = 0; + return true; } case WM_TIMER: { const auto id = static_cast<UINT_PTR>(w_param); @@ -58,13 +59,14 @@ std::optional<LRESULT> GodWindow::HandleGodWindowMessage(HWND hwnd, int msg, (action.value().second)(); if (!action.value().first) application_->GetTimerManager()->KillTimer(id); - return 0; + result = 0; + return true; } break; } default: - return std::nullopt; + return false; } - return std::nullopt; + return false; } -} // namespace cru::platform::win
\ No newline at end of file +} // namespace cru::platform::win diff --git a/src/platform_win/win_native_window.cpp b/src/platform_win/win_native_window.cpp new file mode 100644 index 00000000..74d9466f --- /dev/null +++ b/src/platform_win/win_native_window.cpp @@ -0,0 +1,331 @@ +#include "cru/platform/win/win_native_window.hpp" + +#include "cru/platform/dpi_util.hpp" +#include "cru/platform/win/exception.hpp" +#include "cru/platform/win/win_application.hpp" +#include "cru/platform/win/window_class.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); +} + +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."); + } +} + +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) { + // render_target_->ResizeBuffer(new_width, new_height); + if (!(new_width == 0 && new_height == 0)) + 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 diff --git a/src/platform_win/window_manager.cpp b/src/platform_win/window_manager.cpp new file mode 100644 index 00000000..62a73499 --- /dev/null +++ b/src/platform_win/window_manager.cpp @@ -0,0 +1,53 @@ +#include "window_manager.hpp" + +#include "cru/platform/win/win_application.hpp" +#include "cru/platform/win/win_native_window.hpp" + +#include <assert.h> + +namespace cru::platform::win { +LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, + LPARAM lParam) { + auto window = WinApplication::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(WinApplication* application) { + application_ = application; + general_window_class_ = std::make_shared<WindowClass>( + L"CruUIWindowClass", GeneralWndProc, + application->GetInstanceHandle()); +} + +void WindowManager::RegisterWindow(HWND hwnd, WinNativeWindow* window) { + assert(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); + assert(find_result != window_map_.end()); // The hwnd is not in the map. + window_map_.erase(find_result); + if (window_map_.empty()) application_->Quit(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; +} +} diff --git a/src/platform_win/window_manager.hpp b/src/platform_win/window_manager.hpp new file mode 100644 index 00000000..6e36ead1 --- /dev/null +++ b/src/platform_win/window_manager.hpp @@ -0,0 +1,52 @@ +#pragma once +#include "cru/platform/win/win_pre_config.hpp" + +#include "cru/common/base.hpp" + +#include <map> +#include <memory> +#include <vector> + +namespace cru::platform::win { +class WinApplication; +class WinNativeWindow; +class WindowClass; + +class WindowManager : public Object { + public: + WindowManager(WinApplication* application); + WindowManager(const WindowManager& other) = delete; + WindowManager(WindowManager&& other) = delete; + WindowManager& operator=(const WindowManager& other) = delete; + WindowManager& operator=(WindowManager&& other) = delete; + ~WindowManager() override = default; + + // Get the general window class for creating ordinary window. + std::shared_ptr<WindowClass> GetGeneralWindowClass() const { + return general_window_class_; + } + + // Register a window newly created. + // This function adds the hwnd to hwnd-window map. + // It should be called immediately after a window was created. + void RegisterWindow(HWND hwnd, WinNativeWindow* window); + + // Unregister a window that is going to be destroyed. + // This function removes the hwnd from the hwnd-window map. + // It should be called immediately before a window is going to be destroyed, + void UnregisterWindow(HWND hwnd); + + // Return a pointer to the Window object related to the HWND or nullptr if the + // hwnd is not in the map. + WinNativeWindow* FromHandle(HWND hwnd); + + std::vector<WinNativeWindow*> GetAllWindows() const; + + private: + WinApplication* application_; + + std::shared_ptr<WindowClass> general_window_class_; + std::map<HWND, WinNativeWindow*> window_map_; +}; + +} // namespace cru::platform::win diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 91319db7..d138424f 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -97,54 +97,6 @@ Control* FindLowestCommonAncestor(Control* left, Control* right) { } } // namespace -LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, - LPARAM lParam) { - auto window = WindowManager::GetInstance()->FromHandle(hWnd); - - LRESULT result; - if (window != nullptr && - window->HandleWindowMessage(hWnd, Msg, wParam, lParam, result)) - return result; - - return DefWindowProc(hWnd, Msg, wParam, lParam); -} - -WindowManager* WindowManager::GetInstance() { - return Application::GetInstance()->ResolveSingleton<WindowManager>( - [](auto) { return new WindowManager{}; }); -} - -WindowManager::WindowManager() { - general_window_class_ = std::make_unique<WindowClass>( - L"CruUIWindowClass", GeneralWndProc, - Application::GetInstance()->GetInstanceHandle()); -} - -void WindowManager::RegisterWindow(HWND hwnd, Window* window) { - assert(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); - assert(find_result != window_map_.end()); // The hwnd is not in the map. - window_map_.erase(find_result); - if (window_map_.empty()) Application::GetInstance()->Quit(0); -} - -Window* 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<Window*> WindowManager::GetAllWindows() const { - std::vector<Window*> windows; - for (auto [key, value] : window_map_) windows.push_back(value); - return windows; -} inline Point PiToDip(const POINT& pi_point) { return Point(graph::PixelToDipX(pi_point.x), graph::PixelToDipY(pi_point.y)); @@ -217,10 +169,7 @@ void Window::AfterCreateHwnd(WindowManager* window_manager) { } Window::~Window() { - if (IsWindowValid()) { - SetDeleteThisOnDestroy(false); // avoid double delete. - Close(); - } + TraverseDescendants( [this](Control* control) { control->OnDetachToWindow(this); }); } @@ -231,107 +180,6 @@ render::RenderObject* Window::GetRenderObject() const { return render_object_.get(); } -void Window::SetDeleteThisOnDestroy(bool value) { - delete_this_on_destroy_ = value; -} - -void Window::Close() { - if (IsWindowValid()) DestroyWindow(hwnd_); -} - -void Window::InvalidateDraw() { - if (IsWindowValid()) { - InvalidateRect(hwnd_, nullptr, false); - } -} - -void Window::Show() { - if (IsWindowValid()) { - ShowWindow(hwnd_, SW_SHOWNORMAL); - } -} - -void Window::Hide() { - if (IsWindowValid()) { - ShowWindow(hwnd_, SW_HIDE); - } -} - -Size Window::GetClientSize() { - if (!IsWindowValid()) return Size(); - - const auto pixel_rect = GetClientRectPixel(); - return Size(graph::PixelToDipX(pixel_rect.right), - graph::PixelToDipY(pixel_rect.bottom)); -} - -void Window::SetClientSize(const Size& size) { - if (IsWindowValid()) { - 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 = graph::DipToPixelX(size.width); - rect.bottom = graph::DipToPixelY(size.height); - AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style); - - SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, - rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE); - } -} - -Rect Window::GetWindowRect() { - if (!IsWindowValid()) return Rect(); - - RECT rect; - ::GetWindowRect(hwnd_, &rect); - - return Rect::FromVertices( - graph::PixelToDipX(rect.left), graph::PixelToDipY(rect.top), - graph::PixelToDipX(rect.right), graph::PixelToDipY(rect.bottom)); -} - -void Window::SetWindowRect(const Rect& rect) { - if (IsWindowValid()) { - SetWindowPos(hwnd_, nullptr, graph::DipToPixelX(rect.left), - graph::DipToPixelY(rect.top), - graph::DipToPixelX(rect.GetRight()), - graph::DipToPixelY(rect.GetBottom()), SWP_NOZORDER); - } -} - -void Window::SetWindowPosition(const Point& position) { - if (IsWindowValid()) { - SetWindowPos(hwnd_, nullptr, graph::DipToPixelX(position.x), - graph::DipToPixelY(position.y), 0, 0, - SWP_NOZORDER | SWP_NOSIZE); - } -} - -Point Window::PointToScreen(const Point& point) { - if (!IsWindowValid()) return Point::Zero(); - - auto p = DipToPi(point); - if (::ClientToScreen(GetWindowHandle(), &p) == 0) - throw Win32Error(::GetLastError(), - "Failed transform point from window to screen."); - return PiToDip(p); -} - -Point Window::PointFromScreen(const Point& point) { - if (!IsWindowValid()) return Point::Zero(); - - auto p = DipToPi(point); - if (::ScreenToClient(GetWindowHandle(), &p) == 0) - throw Win32Error(::GetLastError(), - "Failed transform point from screen to window."); - return PiToDip(p); -} - bool Window::HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT& result) { events::WindowNativeMessageEventArgs args(this, this, diff --git a/src/ui/window.hpp b/src/ui/window.hpp index f1d5386f..1e480454 100644 --- a/src/ui/window.hpp +++ b/src/ui/window.hpp @@ -19,45 +19,7 @@ class WindowRenderObject; } namespace cru::ui { -class WindowManager : public Object { - public: - static WindowManager* GetInstance(); - - private: - WindowManager(); - - public: - WindowManager(const WindowManager& other) = delete; - WindowManager(WindowManager&& other) = delete; - WindowManager& operator=(const WindowManager& other) = delete; - WindowManager& operator=(WindowManager&& other) = delete; - ~WindowManager() override = default; - - // Get the general window class for creating ordinary window. - WindowClass* GetGeneralWindowClass() const { - return general_window_class_.get(); - } - - // Register a window newly created. - // This function adds the hwnd to hwnd-window map. - // It should be called immediately after a window was created. - void RegisterWindow(HWND hwnd, Window* window); - - // Unregister a window that is going to be destroyed. - // This function removes the hwnd from the hwnd-window map. - // It should be called immediately before a window is going to be destroyed, - void UnregisterWindow(HWND hwnd); - // Return a pointer to the Window object related to the HWND or nullptr if the - // hwnd is not in the map. - Window* FromHandle(HWND hwnd); - - std::vector<Window*> GetAllWindows() const; - - private: - std::unique_ptr<WindowClass> general_window_class_; - std::map<HWND, Window*> window_map_; -}; class Window final : public ContentControl { friend class WindowManager; @@ -91,34 +53,11 @@ class Window final : public ContentControl { render::RenderObject* GetRenderObject() const override; - void SetDeleteThisOnDestroy(bool value); - - //*************** region: handle *************** - // Get the handle of the window. Return null if window is invalid. - HWND GetWindowHandle() const { return hwnd_; } - // Return if the window is still valid, that is, hasn't been closed or - // destroyed. - bool IsWindowValid() const { return hwnd_ != nullptr; } //*************** region: window operations *************** - Window* GetParentWindow() const { return parent_window_; } - - // Close and destroy the window if the window is valid. - void Close(); - - // Send a repaint message to the window's message queue which may make the - // window repaint. - void InvalidateDraw(); - - // Show the window. - void Show(); - - // Hide thw window. - void Hide(); - // Get the client size. Size GetClientSize(); @@ -222,10 +161,6 @@ class Window final : public ContentControl { const Point& point); private: - bool delete_this_on_destroy_ = true; - - HWND hwnd_ = nullptr; - Window* parent_window_ = nullptr; std::shared_ptr<graph::WindowRenderTarget> render_target_{}; std::shared_ptr<render::WindowRenderObject> render_object_{}; |