From d86a71f79afe0e4dac768f61d6bff690567aca5b Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 May 2020 01:40:02 +0800 Subject: ... --- include/cru/win/native/GodWindow.hpp | 27 ++++++ include/cru/win/native/InputMethod.hpp | 98 ++++++++++++++++++++++ include/cru/win/native/UiApplication.hpp | 76 +++++++++++++++++ include/cru/win/native/WindowClass.hpp | 24 ++++++ .../win/native/WindowNativeMessageEventArgs.hpp | 40 +++++++++ include/cru/win/native/WindowRenderTarget.hpp | 47 +++++++++++ include/cru/win/native/base.hpp | 4 +- include/cru/win/native/cursor.hpp | 4 +- include/cru/win/native/exception.hpp | 2 +- include/cru/win/native/god_window.hpp | 27 ------ include/cru/win/native/input_method.hpp | 98 ---------------------- include/cru/win/native/keyboard.hpp | 4 +- include/cru/win/native/resource.hpp | 4 +- include/cru/win/native/ui_application.hpp | 76 ----------------- include/cru/win/native/window.hpp | 6 +- include/cru/win/native/window_class.hpp | 24 ------ .../native/window_native_message_event_args.hpp | 40 --------- include/cru/win/native/window_render_target.hpp | 47 ----------- 18 files changed, 324 insertions(+), 324 deletions(-) create mode 100644 include/cru/win/native/GodWindow.hpp create mode 100644 include/cru/win/native/InputMethod.hpp create mode 100644 include/cru/win/native/UiApplication.hpp create mode 100644 include/cru/win/native/WindowClass.hpp create mode 100644 include/cru/win/native/WindowNativeMessageEventArgs.hpp create mode 100644 include/cru/win/native/WindowRenderTarget.hpp delete mode 100644 include/cru/win/native/god_window.hpp delete mode 100644 include/cru/win/native/input_method.hpp delete mode 100644 include/cru/win/native/ui_application.hpp delete mode 100644 include/cru/win/native/window_class.hpp delete mode 100644 include/cru/win/native/window_native_message_event_args.hpp delete mode 100644 include/cru/win/native/window_render_target.hpp (limited to 'include/cru/win/native') diff --git a/include/cru/win/native/GodWindow.hpp b/include/cru/win/native/GodWindow.hpp new file mode 100644 index 00000000..1dd99661 --- /dev/null +++ b/include/cru/win/native/GodWindow.hpp @@ -0,0 +1,27 @@ +#pragma once +#include "Base.hpp" + +#include + +namespace cru::platform::native::win { +class GodWindow : public Object { + public: + explicit GodWindow(WinUiApplication* application); + + CRU_DELETE_COPY(GodWindow) + CRU_DELETE_MOVE(GodWindow) + + ~GodWindow() override; + + HWND GetHandle() const { return hwnd_; } + + bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, + LPARAM l_param, LRESULT* result); + + private: + WinUiApplication* application_; + + std::unique_ptr god_window_class_; + HWND hwnd_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/InputMethod.hpp b/include/cru/win/native/InputMethod.hpp new file mode 100644 index 00000000..0e9634aa --- /dev/null +++ b/include/cru/win/native/InputMethod.hpp @@ -0,0 +1,98 @@ +// Some useful information can be found from chromium code: +// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.h +// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.cc + +#pragma once +#include "Resource.hpp" + +#include "cru/platform/native/InputMethod.hpp" +#include "WindowNativeMessageEventArgs.hpp" + +#include + +namespace cru::platform::native::win { +class AutoHIMC : public Object { + public: + explicit AutoHIMC(HWND hwnd); + + CRU_DELETE_COPY(AutoHIMC) + + AutoHIMC(AutoHIMC&& other); + AutoHIMC& operator=(AutoHIMC&& other); + + ~AutoHIMC() override; + + HWND GetHwnd() const { return hwnd_; } + + HIMC Get() const { return handle_; } + + private: + HWND hwnd_; + HIMC handle_; +}; + +class WinInputMethodContext : public WinNativeResource, + public virtual IInputMethodContext { + public: + WinInputMethodContext(gsl::not_null window); + + CRU_DELETE_COPY(WinInputMethodContext) + CRU_DELETE_MOVE(WinInputMethodContext) + + ~WinInputMethodContext() override; + + bool ShouldManuallyDrawCompositionText() override { return true; } + + void EnableIME() override; + + void DisableIME() override; + + void CompleteComposition() override; + + void CancelComposition() override; + + CompositionText GetCompositionText() override; + + void SetCandidateWindowPosition(const Point& point) override; + + IEvent* CompositionStartEvent() override; + + IEvent* CompositionEndEvent() override; + + IEvent* CompositionEvent() override; + + IEvent* TextEvent() override; + + private: + void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); + + std::string GetResultString(); + + std::optional TryGetHIMC(); + + private: + std::shared_ptr native_window_resolver_; + + std::vector event_revoker_guards_; + + Event composition_start_event_; + Event composition_end_event_; + Event composition_event_; + Event text_event_; +}; + +class WinInputMethodManager : public WinNativeResource, + public virtual IInputMethodManager { + public: + WinInputMethodManager(WinUiApplication* application); + + CRU_DELETE_COPY(WinInputMethodManager) + CRU_DELETE_MOVE(WinInputMethodManager) + + ~WinInputMethodManager() override; + + public: + std::unique_ptr GetContext( + INativeWindow* window) override; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/UiApplication.hpp b/include/cru/win/native/UiApplication.hpp new file mode 100644 index 00000000..cbc08af7 --- /dev/null +++ b/include/cru/win/native/UiApplication.hpp @@ -0,0 +1,76 @@ +#pragma once +#include "Resource.hpp" + +#include "cru/platform/native/UiApplication.hpp" + +#include + +namespace cru::platform::graph::win::direct { +class DirectGraphFactory; +} + +namespace cru::platform::native::win { +class WinUiApplication : public WinNativeResource, + public virtual IUiApplication { + public: + static WinUiApplication* GetInstance() { return instance; } + + private: + static WinUiApplication* instance; + + public: + WinUiApplication(); + + CRU_DELETE_COPY(WinUiApplication) + CRU_DELETE_MOVE(WinUiApplication) + + ~WinUiApplication() override; + + public: + int Run() override; + void RequestQuit(int quit_code) override; + + void AddOnQuitHandler(std::function handler) override; + + void InvokeLater(std::function action) override; + long long SetTimeout(std::chrono::milliseconds milliseconds, + std::function action) override; + long long SetInterval(std::chrono::milliseconds milliseconds, + std::function action) override; + void CancelTimer(long long id) override; + + std::vector GetAllWindow() override; + std::shared_ptr CreateWindow( + INativeWindow* parent) override; + + cru::platform::graph::IGraphFactory* GetGraphFactory() override; + + cru::platform::graph::win::direct::DirectGraphFactory* GetDirectFactory() { + return graph_factory_.get(); + } + + ICursorManager* GetCursorManager() override; + IInputMethodManager* GetInputMethodManager() override; + + HINSTANCE GetInstanceHandle() const { return instance_handle_; } + + GodWindow* GetGodWindow() const { return god_window_.get(); } + TimerManager* GetTimerManager() const { return timer_manager_.get(); } + WindowManager* GetWindowManager() const { return window_manager_.get(); } + + private: + HINSTANCE instance_handle_; + + std::unique_ptr + graph_factory_; + + std::unique_ptr god_window_; + std::unique_ptr timer_manager_; + std::unique_ptr window_manager_; + + std::unique_ptr cursor_manager_; + std::unique_ptr input_method_manager_; + + std::vector> quit_handlers_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowClass.hpp b/include/cru/win/native/WindowClass.hpp new file mode 100644 index 00000000..fdd55065 --- /dev/null +++ b/include/cru/win/native/WindowClass.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Base.hpp" + +#include + +namespace cru::platform::native::win { +class WindowClass : public Object { + public: + WindowClass(std::wstring name, WNDPROC window_proc, HINSTANCE h_instance); + + CRU_DELETE_COPY(WindowClass) + CRU_DELETE_MOVE(WindowClass) + + ~WindowClass() override = default; + + const wchar_t* GetName() const { return name_.c_str(); } + + ATOM GetAtom() const { return atom_; } + + private: + std::wstring name_; + ATOM atom_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowNativeMessageEventArgs.hpp b/include/cru/win/native/WindowNativeMessageEventArgs.hpp new file mode 100644 index 00000000..84a7a123 --- /dev/null +++ b/include/cru/win/native/WindowNativeMessageEventArgs.hpp @@ -0,0 +1,40 @@ +#pragma once +#include "../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::native::win { +struct WindowNativeMessage { + HWND hwnd; + UINT msg; + WPARAM w_param; + LPARAM l_param; +}; + +class WindowNativeMessageEventArgs : public Object { + public: + WindowNativeMessageEventArgs(const WindowNativeMessage& message) + : message_(message) {} + CRU_DEFAULT_COPY(WindowNativeMessageEventArgs) + CRU_DEFAULT_MOVE(WindowNativeMessageEventArgs) + ~WindowNativeMessageEventArgs() override = default; + + const 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 HandleWithResult(LRESULT result) { + handled_ = true; + result_ = result; + } + + private: + WindowNativeMessage message_; + LRESULT result_; + bool handled_ = false; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/WindowRenderTarget.hpp b/include/cru/win/native/WindowRenderTarget.hpp new file mode 100644 index 00000000..83ac1e03 --- /dev/null +++ b/include/cru/win/native/WindowRenderTarget.hpp @@ -0,0 +1,47 @@ +#pragma once +#include "Base.hpp" + +namespace cru::platform::graph::win::direct { +class DirectGraphFactory; +} + +namespace cru::platform::native::win { +// Represents a window render target. +class WindowRenderTarget : public Object { + public: + WindowRenderTarget(graph::win::direct::DirectGraphFactory* factory, + HWND hwnd); + + CRU_DELETE_COPY(WindowRenderTarget) + CRU_DELETE_MOVE(WindowRenderTarget) + + ~WindowRenderTarget() override = default; + + public: + graph::win::direct::DirectGraphFactory* GetDirectFactory() const { + return factory_; + } + + ID2D1DeviceContext* GetD2D1DeviceContext() { + return d2d1_device_context_.Get(); + } + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Set this render target as the d2d device context's target. + void SetAsTarget(); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + graph::win::direct::DirectGraphFactory* factory_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_swap_chain_; + Microsoft::WRL::ComPtr target_bitmap_; +}; +} // namespace cru::platform::native::win diff --git a/include/cru/win/native/base.hpp b/include/cru/win/native/base.hpp index 61c9f4da..a50c6dd1 100644 --- a/include/cru/win/native/base.hpp +++ b/include/cru/win/native/base.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../win_pre_config.hpp" +#include "../WinPreConfig.hpp" -#include "cru/common/base.hpp" +#include "cru/common/Base.hpp" namespace cru::platform::native::win { class GodWindow; diff --git a/include/cru/win/native/cursor.hpp b/include/cru/win/native/cursor.hpp index e5728b1c..152374d8 100644 --- a/include/cru/win/native/cursor.hpp +++ b/include/cru/win/native/cursor.hpp @@ -1,7 +1,7 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" -#include "cru/platform/native/cursor.hpp" +#include "cru/platform/native/Cursor.hpp" #include diff --git a/include/cru/win/native/exception.hpp b/include/cru/win/native/exception.hpp index ac15ab2d..6a5265c1 100644 --- a/include/cru/win/native/exception.hpp +++ b/include/cru/win/native/exception.hpp @@ -1,5 +1,5 @@ #pragma once -#include "../exception.hpp" +#include "../Exception.hpp" namespace cru::platform::native::win { using platform::win::Win32Error; diff --git a/include/cru/win/native/god_window.hpp b/include/cru/win/native/god_window.hpp deleted file mode 100644 index 33218a55..00000000 --- a/include/cru/win/native/god_window.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include "base.hpp" - -#include - -namespace cru::platform::native::win { -class GodWindow : public Object { - public: - explicit GodWindow(WinUiApplication* application); - - CRU_DELETE_COPY(GodWindow) - CRU_DELETE_MOVE(GodWindow) - - ~GodWindow() override; - - HWND GetHandle() const { return hwnd_; } - - bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, - LPARAM l_param, LRESULT* result); - - private: - WinUiApplication* application_; - - std::unique_ptr god_window_class_; - HWND hwnd_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/input_method.hpp b/include/cru/win/native/input_method.hpp deleted file mode 100644 index ff867f3f..00000000 --- a/include/cru/win/native/input_method.hpp +++ /dev/null @@ -1,98 +0,0 @@ -// Some useful information can be found from chromium code: -// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.h -// https://chromium.googlesource.com/chromium/chromium/+/refs/heads/master/ui/base/win/ime_input.cc - -#pragma once -#include "resource.hpp" - -#include "cru/platform/native/input_method.hpp" -#include "window_native_message_event_args.hpp" - -#include - -namespace cru::platform::native::win { -class AutoHIMC : public Object { - public: - explicit AutoHIMC(HWND hwnd); - - CRU_DELETE_COPY(AutoHIMC) - - AutoHIMC(AutoHIMC&& other); - AutoHIMC& operator=(AutoHIMC&& other); - - ~AutoHIMC() override; - - HWND GetHwnd() const { return hwnd_; } - - HIMC Get() const { return handle_; } - - private: - HWND hwnd_; - HIMC handle_; -}; - -class WinInputMethodContext : public WinNativeResource, - public virtual IInputMethodContext { - public: - WinInputMethodContext(gsl::not_null window); - - CRU_DELETE_COPY(WinInputMethodContext) - CRU_DELETE_MOVE(WinInputMethodContext) - - ~WinInputMethodContext() override; - - bool ShouldManuallyDrawCompositionText() override { return true; } - - void EnableIME() override; - - void DisableIME() override; - - void CompleteComposition() override; - - void CancelComposition() override; - - CompositionText GetCompositionText() override; - - void SetCandidateWindowPosition(const Point& point) override; - - IEvent* CompositionStartEvent() override; - - IEvent* CompositionEndEvent() override; - - IEvent* CompositionEvent() override; - - IEvent* TextEvent() override; - - private: - void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); - - std::string GetResultString(); - - std::optional TryGetHIMC(); - - private: - std::shared_ptr native_window_resolver_; - - std::vector event_revoker_guards_; - - Event composition_start_event_; - Event composition_end_event_; - Event composition_event_; - Event text_event_; -}; - -class WinInputMethodManager : public WinNativeResource, - public virtual IInputMethodManager { - public: - WinInputMethodManager(WinUiApplication* application); - - CRU_DELETE_COPY(WinInputMethodManager) - CRU_DELETE_MOVE(WinInputMethodManager) - - ~WinInputMethodManager() override; - - public: - std::unique_ptr GetContext( - INativeWindow* window) override; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/keyboard.hpp b/include/cru/win/native/keyboard.hpp index afa51c92..790e0015 100644 --- a/include/cru/win/native/keyboard.hpp +++ b/include/cru/win/native/keyboard.hpp @@ -1,7 +1,7 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" -#include "cru/platform/native/keyboard.hpp" +#include "cru/platform/native/Keyboard.hpp" namespace cru::platform::native::win { KeyCode VirtualKeyToKeyCode(int virtual_key); diff --git a/include/cru/win/native/resource.hpp b/include/cru/win/native/resource.hpp index 5601e40e..7afaca0f 100644 --- a/include/cru/win/native/resource.hpp +++ b/include/cru/win/native/resource.hpp @@ -1,7 +1,7 @@ #pragma once -#include "base.hpp" +#include "Base.hpp" -#include "cru/platform/resource.hpp" +#include "cru/platform/Resource.hpp" namespace cru::platform::native::win { class WinNativeResource : public Object, public virtual INativeResource { diff --git a/include/cru/win/native/ui_application.hpp b/include/cru/win/native/ui_application.hpp deleted file mode 100644 index 250e855c..00000000 --- a/include/cru/win/native/ui_application.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once -#include "resource.hpp" - -#include "cru/platform/native/ui_application.hpp" - -#include - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory; -} - -namespace cru::platform::native::win { -class WinUiApplication : public WinNativeResource, - public virtual IUiApplication { - public: - static WinUiApplication* GetInstance() { return instance; } - - private: - static WinUiApplication* instance; - - public: - WinUiApplication(); - - CRU_DELETE_COPY(WinUiApplication) - CRU_DELETE_MOVE(WinUiApplication) - - ~WinUiApplication() override; - - public: - int Run() override; - void RequestQuit(int quit_code) override; - - void AddOnQuitHandler(std::function handler) override; - - void InvokeLater(std::function action) override; - long long SetTimeout(std::chrono::milliseconds milliseconds, - std::function action) override; - long long SetInterval(std::chrono::milliseconds milliseconds, - std::function action) override; - void CancelTimer(long long id) override; - - std::vector GetAllWindow() override; - std::shared_ptr CreateWindow( - INativeWindow* parent) override; - - cru::platform::graph::IGraphFactory* GetGraphFactory() override; - - cru::platform::graph::win::direct::DirectGraphFactory* GetDirectFactory() { - return graph_factory_.get(); - } - - ICursorManager* GetCursorManager() override; - IInputMethodManager* GetInputMethodManager() override; - - HINSTANCE GetInstanceHandle() const { return instance_handle_; } - - GodWindow* GetGodWindow() const { return god_window_.get(); } - TimerManager* GetTimerManager() const { return timer_manager_.get(); } - WindowManager* GetWindowManager() const { return window_manager_.get(); } - - private: - HINSTANCE instance_handle_; - - std::unique_ptr - graph_factory_; - - std::unique_ptr god_window_; - std::unique_ptr timer_manager_; - std::unique_ptr window_manager_; - - std::unique_ptr cursor_manager_; - std::unique_ptr input_method_manager_; - - std::vector> quit_handlers_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/window.hpp b/include/cru/win/native/window.hpp index 59b38ab5..80bee39e 100644 --- a/include/cru/win/native/window.hpp +++ b/include/cru/win/native/window.hpp @@ -1,8 +1,8 @@ #pragma once -#include "resource.hpp" +#include "Resource.hpp" -#include "cru/platform/native/window.hpp" -#include "window_native_message_event_args.hpp" +#include "cru/platform/native/Window.hpp" +#include "WindowNativeMessageEventArgs.hpp" #include diff --git a/include/cru/win/native/window_class.hpp b/include/cru/win/native/window_class.hpp deleted file mode 100644 index 2140c304..00000000 --- a/include/cru/win/native/window_class.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "base.hpp" - -#include - -namespace cru::platform::native::win { -class WindowClass : public Object { - public: - WindowClass(std::wstring name, WNDPROC window_proc, HINSTANCE h_instance); - - CRU_DELETE_COPY(WindowClass) - CRU_DELETE_MOVE(WindowClass) - - ~WindowClass() override = default; - - const wchar_t* GetName() const { return name_.c_str(); } - - ATOM GetAtom() const { return atom_; } - - private: - std::wstring name_; - ATOM atom_; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/window_native_message_event_args.hpp b/include/cru/win/native/window_native_message_event_args.hpp deleted file mode 100644 index f5476735..00000000 --- a/include/cru/win/native/window_native_message_event_args.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/common/base.hpp" - -namespace cru::platform::native::win { -struct WindowNativeMessage { - HWND hwnd; - UINT msg; - WPARAM w_param; - LPARAM l_param; -}; - -class WindowNativeMessageEventArgs : public Object { - public: - WindowNativeMessageEventArgs(const WindowNativeMessage& message) - : message_(message) {} - CRU_DEFAULT_COPY(WindowNativeMessageEventArgs) - CRU_DEFAULT_MOVE(WindowNativeMessageEventArgs) - ~WindowNativeMessageEventArgs() override = default; - - const 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 HandleWithResult(LRESULT result) { - handled_ = true; - result_ = result; - } - - private: - WindowNativeMessage message_; - LRESULT result_; - bool handled_ = false; -}; -} // namespace cru::platform::native::win diff --git a/include/cru/win/native/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp deleted file mode 100644 index 552e87bc..00000000 --- a/include/cru/win/native/window_render_target.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include "base.hpp" - -namespace cru::platform::graph::win::direct { -class DirectGraphFactory; -} - -namespace cru::platform::native::win { -// Represents a window render target. -class WindowRenderTarget : public Object { - public: - WindowRenderTarget(graph::win::direct::DirectGraphFactory* factory, - HWND hwnd); - - CRU_DELETE_COPY(WindowRenderTarget) - CRU_DELETE_MOVE(WindowRenderTarget) - - ~WindowRenderTarget() override = default; - - public: - graph::win::direct::DirectGraphFactory* GetDirectFactory() const { - return factory_; - } - - ID2D1DeviceContext* GetD2D1DeviceContext() { - return d2d1_device_context_.Get(); - } - - // Resize the underlying buffer. - void ResizeBuffer(int width, int height); - - // Set this render target as the d2d device context's target. - void SetAsTarget(); - - // Present the data of the underlying buffer to the window. - void Present(); - - private: - void CreateTargetBitmap(); - - private: - graph::win::direct::DirectGraphFactory* factory_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_swap_chain_; - Microsoft::WRL::ComPtr target_bitmap_; -}; -} // namespace cru::platform::native::win -- cgit v1.2.3