From 8ca0873597eb05a2f120d3ea107660abcff4533c Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 31 Mar 2019 19:48:20 +0800 Subject: ... --- include/cru/platform/win/god_window.hpp | 5 +- include/cru/platform/win/win_application.hpp | 10 +- include/cru/platform/win/win_native_window.hpp | 123 +++++++++++++++++++++ .../win/window_native_message_event_args.hpp | 45 ++++++++ 4 files changed, 177 insertions(+), 6 deletions(-) create mode 100644 include/cru/platform/win/win_native_window.hpp create mode 100644 include/cru/platform/win/window_native_message_event_args.hpp (limited to 'include/cru/platform/win') 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 -#include #include "cru/common/base.hpp" @@ -21,8 +20,8 @@ class GodWindow : public Object { HWND GetHandle() const { return hwnd_; } - std::optional 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& action) override; unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - const std::function& action) override; + const std::function& action) override; unsigned long SetInterval(std::chrono::milliseconds milliseconds, - const std::function& action) override; + const std::function& 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 god_window_; std::shared_ptr timer_manager_; + std::shared_ptr 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 + +namespace cru::platform::win { +class WinApplication; +class WindowClass; +class WindowManager; + +class WinNativeWindow : public Object, public virtual NativeWindow { + public: + WinNativeWindow(WinApplication* application, + std::shared_ptr 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* ResizeEvent() override { return &resize_event_; } + Event<>* PaintEvent() override { return &paint_event_; } + Event* FocusEvent() override { return &focus_event_; } + Event* MouseEnterLeaveEvent() override { + return &mouse_enter_leave_event_; + } + Event* MouseMoveEvent() override { return &mouse_move_event_; } + Event* MouseDownEvent() override { + return &mouse_down_event_; + } + Event* MouseUpEvent() override { + return &mouse_up_event_; + } + Event* KeyDownEvent() override { return &key_down_event_; } + Event* KeyUpEvent() override { return &key_up_event_; } + + Event* 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 resize_event_; + Event<> paint_event_; + Event focus_event_; + Event mouse_enter_leave_event_; + Event mouse_move_event_; + Event mouse_down_event_; + Event mouse_up_event_; + Event key_down_event_; + Event key_up_event_; + + Event native_message_event_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/window_native_message_event_args.hpp b/include/cru/platform/win/window_native_message_event_args.hpp new file mode 100644 index 00000000..b3419c24 --- /dev/null +++ b/include/cru/platform/win/window_native_message_event_args.hpp @@ -0,0 +1,45 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "cru/common/base.hpp" + +namespace cru::platform::win { +struct WindowNativeMessage { + HWND hwnd; + UINT msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public Object { + public: + WindowNativeMessageEventArgs(const WindowNativeMessage& message) + : message_(message) {} + WindowNativeMessageEventArgs(const WindowNativeMessageEventArgs& other) = + default; + WindowNativeMessageEventArgs(WindowNativeMessageEventArgs&& other) = default; + WindowNativeMessageEventArgs& operator=( + const WindowNativeMessageEventArgs& other) = default; + WindowNativeMessageEventArgs& operator=( + WindowNativeMessageEventArgs&& other) = default; + ~WindowNativeMessageEventArgs() override = default; + + WindowNativeMessage GetWindowMessage() const { return message_; } + + LRESULT GetResult() const { return result_; } + void SetResult(LRESULT result) { result_ = result; } + + bool IsHandled() const { return handled_; } + void SetHandled(bool handled) { handled_ = handled; } + + void HandledAndSetResult(LRESULT result) { + handled_ = true; + result_ = result; + } + + private: + WindowNativeMessage message_; + LRESULT result_; + bool handled_ = false; +}; +} // namespace cru::platform::win -- cgit v1.2.3