aboutsummaryrefslogtreecommitdiff
path: root/include/cru/win
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-10 19:42:46 +0800
committercrupest <crupest@outlook.com>2019-04-10 19:42:46 +0800
commit7351020a582d70a1495249fba87d342c8a1fb634 (patch)
treee80f225041dc3816b3dce21c7e15aadbb211602e /include/cru/win
parenta94a806f69586e08a30fff0cdb3e52b0ce7acfa5 (diff)
downloadcru-7351020a582d70a1495249fba87d342c8a1fb634.tar.gz
cru-7351020a582d70a1495249fba87d342c8a1fb634.tar.bz2
cru-7351020a582d70a1495249fba87d342c8a1fb634.zip
Refactor.
Diffstat (limited to 'include/cru/win')
-rw-r--r--include/cru/win/exception.hpp51
-rw-r--r--include/cru/win/graph/d2d_util.hpp108
-rw-r--r--include/cru/win/graph/graph_manager.hpp51
-rw-r--r--include/cru/win/graph/win_brush.hpp33
-rw-r--r--include/cru/win/graph/win_font.hpp28
-rw-r--r--include/cru/win/graph/win_geometry.hpp48
-rw-r--r--include/cru/win/graph/win_graph_factory.hpp30
-rw-r--r--include/cru/win/graph/win_painter.hpp48
-rw-r--r--include/cru/win/graph/win_text_layout.hpp42
-rw-r--r--include/cru/win/native/god_window.hpp32
-rw-r--r--include/cru/win/native/win_application.hpp59
-rw-r--r--include/cru/win/native/win_native_window.hpp137
-rw-r--r--include/cru/win/native/window_class.hpp27
-rw-r--r--include/cru/win/native/window_native_message_event_args.hpp45
-rw-r--r--include/cru/win/native/window_render_target.hpp45
-rw-r--r--include/cru/win/win_pre_config.hpp13
16 files changed, 797 insertions, 0 deletions
diff --git a/include/cru/win/exception.hpp b/include/cru/win/exception.hpp
new file mode 100644
index 00000000..39c52c45
--- /dev/null
+++ b/include/cru/win/exception.hpp
@@ -0,0 +1,51 @@
+#pragma once
+#include "win_pre_config.hpp"
+
+#include "cru/platform/exception.hpp"
+
+#include <stdexcept>
+#include <string_view>
+
+namespace cru::win {
+class HResultError : public platform::PlatformException {
+ public:
+ explicit HResultError(HRESULT h_result);
+ explicit HResultError(HRESULT h_result,
+ const std::string_view& additional_message);
+ HResultError(const HResultError& other) = default;
+ HResultError(HResultError&& other) = default;
+ HResultError& operator=(const HResultError& other) = default;
+ HResultError& operator=(HResultError&& other) = default;
+ ~HResultError() override = default;
+
+ HRESULT GetHResult() const { return h_result_; }
+
+ private:
+ HRESULT h_result_;
+};
+
+inline void ThrowIfFailed(const HRESULT h_result) {
+ if (FAILED(h_result)) throw HResultError(h_result);
+}
+
+inline void ThrowIfFailed(const HRESULT h_result,
+ const std::string_view& message) {
+ if (FAILED(h_result)) throw HResultError(h_result, message);
+}
+
+class Win32Error : public platform::PlatformException {
+ public:
+ explicit Win32Error(DWORD error_code);
+ Win32Error(DWORD error_code, const std::string_view& additional_message);
+ Win32Error(const Win32Error& other) = default;
+ Win32Error(Win32Error&& other) = default;
+ Win32Error& operator=(const Win32Error& other) = default;
+ Win32Error& operator=(Win32Error&& other) = default;
+ ~Win32Error() override = default;
+
+ HRESULT GetErrorCode() const { return error_code_; }
+
+ private:
+ DWORD error_code_;
+};
+} // namespace cru::win
diff --git a/include/cru/win/graph/d2d_util.hpp b/include/cru/win/graph/d2d_util.hpp
new file mode 100644
index 00000000..7a2da91f
--- /dev/null
+++ b/include/cru/win/graph/d2d_util.hpp
@@ -0,0 +1,108 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/common/ui_base.hpp"
+#include "cru/platform/matrix.hpp"
+
+namespace cru::win::graph::util {
+inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) {
+ D2D1_MATRIX_3X2_F m;
+ m._11 = matrix.m11;
+ m._12 = matrix.m12;
+ m._21 = matrix.m21;
+ m._22 = matrix.m22;
+ m._31 = matrix.m31;
+ m._32 = matrix.m32;
+ return m;
+}
+
+inline D2D1_COLOR_F Convert(const ui::Color& color) {
+ return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f,
+ color.blue / 255.0f, color.alpha / 255.0f);
+}
+
+inline D2D1_POINT_2F Convert(const ui::Point& point) {
+ return D2D1::Point2F(point.x, point.y);
+}
+
+inline D2D1_RECT_F Convert(const ui::Rect& rect) {
+ return D2D1::RectF(rect.left, rect.top, rect.left + rect.width,
+ rect.top + rect.height);
+}
+
+inline D2D1_ROUNDED_RECT Convert(const ui::RoundedRect& rounded_rect) {
+ return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x,
+ rounded_rect.radius_y);
+}
+
+inline D2D1_ELLIPSE Convert(const ui::Ellipse& ellipse) {
+ return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x,
+ ellipse.radius_y);
+}
+
+inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) {
+ return platform::Matrix{matrix._11, matrix._12, matrix._21,
+ matrix._22, matrix._31, matrix._32};
+}
+
+inline ui::Color Convert(const D2D1_COLOR_F& color) {
+ auto floor = [](float n) { return static_cast<std::uint8_t>(n + 0.5f); };
+ return ui::Color{floor(color.r * 255.0f), floor(color.g * 255.0f),
+ floor(color.b * 255.0f), floor(color.a * 255.0f)};
+}
+
+inline ui::Point Convert(const D2D1_POINT_2F& point) {
+ return ui::Point(point.x, point.y);
+}
+
+inline ui::Rect Convert(const D2D1_RECT_F& rect) {
+ return ui::Rect(rect.left, rect.top, rect.right - rect.left,
+ rect.bottom - rect.top);
+}
+
+inline ui::RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) {
+ return ui::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX,
+ rounded_rect.radiusY);
+}
+
+inline ui::Ellipse Convert(const D2D1_ELLIPSE& ellipse) {
+ return ui::Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY);
+}
+
+inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) {
+ return left.x == right.x && left.y == right.y;
+}
+
+inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) {
+ return !(left == right);
+}
+
+inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) {
+ return left.left == right.left && left.top == right.top &&
+ left.right == right.right && left.bottom == right.bottom;
+}
+
+inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) {
+ return !(left == right);
+}
+
+inline bool operator==(const D2D1_ROUNDED_RECT& left,
+ const D2D1_ROUNDED_RECT& right) {
+ return left.rect == right.rect && left.radiusX == right.radiusX &&
+ left.radiusY == right.radiusY;
+}
+
+inline bool operator!=(const D2D1_ROUNDED_RECT& left,
+ const D2D1_ROUNDED_RECT& right) {
+ return !(left == right);
+}
+
+inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) {
+ return left.point == right.point && left.radiusX == right.radiusX &&
+ left.radiusY == right.radiusY;
+}
+
+inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) {
+ return !(left == right);
+}
+} // namespace cru::win::graph::util
diff --git a/include/cru/win/graph/graph_manager.hpp b/include/cru/win/graph/graph_manager.hpp
new file mode 100644
index 00000000..bf14b802
--- /dev/null
+++ b/include/cru/win/graph/graph_manager.hpp
@@ -0,0 +1,51 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/common/base.hpp"
+#include "cru/win/graph/win_graph_factory.hpp"
+
+#include <memory>
+
+namespace cru::win::graph {
+class GraphManager final : public Object {
+ public:
+ static GraphManager* GetInstance();
+
+ public:
+ GraphManager();
+ GraphManager(const GraphManager& other) = delete;
+ GraphManager(GraphManager&& other) = delete;
+ GraphManager& operator=(const GraphManager& other) = delete;
+ GraphManager& operator=(GraphManager&& other) = delete;
+ ~GraphManager() override = default;
+
+ public:
+ ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); }
+
+ ID2D1DeviceContext* GetD2D1DeviceContext() const {
+ return d2d1_device_context_.Get();
+ }
+
+ ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); }
+
+ IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); }
+
+ IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); }
+
+ IDWriteFontCollection* GetSystemFontCollection() const {
+ return dwrite_system_font_collection_.Get();
+ }
+
+ WinGraphFactory* GetGraphFactory() const { return graph_factory_.get(); }
+
+ private:
+ Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
+ Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_;
+ Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_;
+ Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_;
+ Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_;
+ Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_;
+
+ std::unique_ptr<WinGraphFactory> graph_factory_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/win_brush.hpp b/include/cru/win/graph/win_brush.hpp
new file mode 100644
index 00000000..69cd952d
--- /dev/null
+++ b/include/cru/win/graph/win_brush.hpp
@@ -0,0 +1,33 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/brush.hpp"
+
+namespace cru::win::graph {
+class GraphManager;
+
+struct WinBrush : virtual platform::graph::Brush {
+ virtual ID2D1Brush* GetD2DBrush() = 0;
+};
+
+class WinSolidColorBrush : public Object,
+ public virtual platform::graph::SolidColorBrush,
+ public virtual WinBrush {
+ public:
+ explicit WinSolidColorBrush(GraphManager* graph_manager,
+ const ui::Color& color);
+ WinSolidColorBrush(const WinSolidColorBrush& other) = delete;
+ WinSolidColorBrush(WinSolidColorBrush&& other) = delete;
+ WinSolidColorBrush& operator=(const WinSolidColorBrush& other) = delete;
+ WinSolidColorBrush& operator=(WinSolidColorBrush&& other) = delete;
+ ~WinSolidColorBrush() override = default;
+
+ ui::Color GetColor() override;
+ void SetColor(const ui::Color& color) override;
+
+ ID2D1Brush* GetD2DBrush() override { return brush_.Get(); }
+
+ private:
+ Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/win_font.hpp b/include/cru/win/graph/win_font.hpp
new file mode 100644
index 00000000..3301e8da
--- /dev/null
+++ b/include/cru/win/graph/win_font.hpp
@@ -0,0 +1,28 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/font.hpp"
+
+#include <string_view>
+
+namespace cru::win::graph {
+class GraphManager;
+
+class WinFontDescriptor : public Object,
+ public virtual platform::graph::FontDescriptor {
+ public:
+ explicit WinFontDescriptor(GraphManager* graph_manager,
+ const std::wstring_view& font_family,
+ float font_size);
+ WinFontDescriptor(const WinFontDescriptor& other) = delete;
+ WinFontDescriptor(WinFontDescriptor&& other) = delete;
+ WinFontDescriptor& operator=(const WinFontDescriptor& other) = delete;
+ WinFontDescriptor& operator=(WinFontDescriptor&& other) = delete;
+ ~WinFontDescriptor() override = default;
+
+ IDWriteTextFormat* GetDWriteTextFormat() const { return text_format_.Get(); }
+
+ private:
+ Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/win_geometry.hpp b/include/cru/win/graph/win_geometry.hpp
new file mode 100644
index 00000000..005a4384
--- /dev/null
+++ b/include/cru/win/graph/win_geometry.hpp
@@ -0,0 +1,48 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/geometry.hpp"
+
+namespace cru::win::graph {
+class GraphManager;
+
+class WinGeometryBuilder : public Object,
+ public virtual platform::graph::GeometryBuilder {
+ public:
+ explicit WinGeometryBuilder(GraphManager* graph_manger);
+ WinGeometryBuilder(const WinGeometryBuilder& other) = delete;
+ WinGeometryBuilder(WinGeometryBuilder&& other) = delete;
+ WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete;
+ WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete;
+ ~WinGeometryBuilder() override;
+
+ bool IsValid() override { return geometry_ != nullptr; }
+ void BeginFigure(const ui::Point& point) override;
+ void LineTo(const ui::Point& point) override;
+ void QuadraticBezierTo(const ui::Point& control_point,
+ const ui::Point& end_point) override;
+ void CloseFigure(bool close) override;
+ platform::graph::Geometry* Build() override;
+
+ private:
+ Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_;
+ Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_;
+};
+
+class WinGeometry : public Object, public virtual platform::graph::Geometry {
+ public:
+ explicit WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry);
+ WinGeometry(const WinGeometry& other) = delete;
+ WinGeometry(WinGeometry&& other) = delete;
+ WinGeometry& operator=(const WinGeometry& other) = delete;
+ WinGeometry& operator=(WinGeometry&& other) = delete;
+ ~WinGeometry() override = default;
+
+ bool FillContains(const ui::Point& point) override;
+
+ ID2D1PathGeometry* GetNative() const { return geometry_.Get(); }
+
+ private:
+ Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/win_graph_factory.hpp b/include/cru/win/graph/win_graph_factory.hpp
new file mode 100644
index 00000000..f8a07819
--- /dev/null
+++ b/include/cru/win/graph/win_graph_factory.hpp
@@ -0,0 +1,30 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/graph_factory.hpp"
+
+namespace cru::win::graph {
+class GraphManager;
+
+class WinGraphFactory : public Object,
+ public virtual platform::graph::GraphFactory {
+ public:
+ explicit WinGraphFactory(GraphManager* graph_manager);
+ WinGraphFactory(const WinGraphFactory& other) = delete;
+ WinGraphFactory(WinGraphFactory&& other) = delete;
+ WinGraphFactory& operator=(const WinGraphFactory& other) = delete;
+ WinGraphFactory& operator=(WinGraphFactory&& other) = delete;
+ ~WinGraphFactory() override = default;
+
+ platform::graph::SolidColorBrush* CreateSolidColorBrush(
+ const ui::Color& color) override;
+ platform::graph::GeometryBuilder* CreateGeometryBuilder() override;
+ platform::graph::FontDescriptor* CreateFontDescriptor(
+ const std::wstring_view& font_family, float font_size);
+ platform::graph::TextLayout* CreateTextLayout(
+ std::shared_ptr<platform::graph::FontDescriptor> font, std::wstring text);
+
+ private:
+ GraphManager* graph_manager_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/win_painter.hpp b/include/cru/win/graph/win_painter.hpp
new file mode 100644
index 00000000..8351cb7b
--- /dev/null
+++ b/include/cru/win/graph/win_painter.hpp
@@ -0,0 +1,48 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/painter.hpp"
+
+namespace cru::win::graph {
+class GraphManager;
+
+class WinPainter : public Object, public virtual platform::graph::Painter {
+ public:
+ explicit WinPainter(ID2D1RenderTarget* render_target);
+ WinPainter(const WinPainter& other) = delete;
+ WinPainter(WinPainter&& other) = delete;
+ WinPainter& operator=(const WinPainter& other) = delete;
+ WinPainter& operator=(WinPainter&& other) = delete;
+ ~WinPainter() override;
+
+ platform::Matrix GetTransform() override;
+ void SetTransform(const platform::Matrix& matrix) override;
+ void Clear(const ui::Color& color) override;
+ void StrokeRectangle(const ui::Rect& rectangle, platform::graph::Brush* brush,
+ float width) override;
+ void FillRectangle(const ui::Rect& rectangle,
+ platform::graph::Brush* brush) override;
+ void StrokeGeometry(platform::graph::Geometry* geometry,
+ platform::graph::Brush* brush, float width) override;
+ void FillGeometry(platform::graph::Geometry* geometry,
+ platform::graph::Brush* brush) override;
+ void DrawText(const ui::Point& offset,
+ platform::graph::TextLayout* text_layout,
+ platform::graph::Brush* brush) override;
+ void EndDraw() override;
+ bool IsDisposed() override { return is_disposed_; }
+
+ void EndDrawAndDeleteThis() {
+ EndDraw();
+ delete this;
+ }
+
+ protected:
+ virtual void DoEndDraw();
+
+ private:
+ ID2D1RenderTarget* render_target_;
+
+ bool is_disposed_ = false;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/win_text_layout.hpp b/include/cru/win/graph/win_text_layout.hpp
new file mode 100644
index 00000000..b861fda7
--- /dev/null
+++ b/include/cru/win/graph/win_text_layout.hpp
@@ -0,0 +1,42 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/text_layout.hpp"
+
+#include <memory>
+
+namespace cru::win::graph {
+class GraphManager;
+class WinFontDescriptor;
+
+class WinTextLayout : public Object, public virtual platform::graph::TextLayout {
+ public:
+ explicit WinTextLayout(GraphManager* graph_manager,
+ std::shared_ptr<WinFontDescriptor> font, std::wstring text);
+ WinTextLayout(const WinTextLayout& other) = delete;
+ WinTextLayout(WinTextLayout&& other) = delete;
+ WinTextLayout& operator=(const WinTextLayout& other) = delete;
+ WinTextLayout& operator=(WinTextLayout&& other) = delete;
+ ~WinTextLayout() override = default;
+
+ std::wstring GetText() override;
+ void SetText(std::wstring new_text) override;
+ std::shared_ptr<platform::graph::FontDescriptor> GetFont();
+ void SetFont(std::shared_ptr<platform::graph::FontDescriptor> font);
+ void SetMaxWidth(float max_width) override;
+ void SetMaxHeight(float max_height) override;
+ ui::Rect GetTextBounds() override;
+ std::vector<ui::Rect> TextRangeRect(
+ const ui::TextRange& text_range) override;
+
+ IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); }
+
+ private:
+ GraphManager* graph_manager_;
+ std::wstring text_;
+ std::shared_ptr<WinFontDescriptor> font_descriptor_;
+ float max_width_ = 0.0f;
+ float max_height_ = 0.0f;
+ Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_;
+};
+} // namespace cru::platform::win
diff --git a/include/cru/win/native/god_window.hpp b/include/cru/win/native/god_window.hpp
new file mode 100644
index 00000000..9ac49858
--- /dev/null
+++ b/include/cru/win/native/god_window.hpp
@@ -0,0 +1,32 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/common/base.hpp"
+
+#include <memory>
+
+namespace cru::win::native {
+class WinApplication;
+class WindowClass;
+
+class GodWindow : public Object {
+ public:
+ explicit GodWindow(WinApplication* application);
+ GodWindow(const GodWindow& other) = delete;
+ GodWindow(GodWindow&& other) = delete;
+ GodWindow& operator=(const GodWindow& other) = delete;
+ GodWindow& operator=(GodWindow&& other) = delete;
+ ~GodWindow() override;
+
+ HWND GetHandle() const { return hwnd_; }
+
+ bool HandleGodWindowMessage(HWND hwnd, UINT msg, WPARAM w_param,
+ LPARAM l_param, LRESULT* result);
+
+ private:
+ WinApplication* application_;
+
+ std::shared_ptr<WindowClass> god_window_class_;
+ HWND hwnd_;
+};
+} // namespace cru::win::native \ No newline at end of file
diff --git a/include/cru/win/native/win_application.hpp b/include/cru/win/native/win_application.hpp
new file mode 100644
index 00000000..458b10ae
--- /dev/null
+++ b/include/cru/win/native/win_application.hpp
@@ -0,0 +1,59 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/native/ui_applicaition.hpp"
+
+#include <memory>
+
+namespace cru::win::native {
+class GodWindow;
+class TimerManager;
+class WindowManager;
+
+class WinApplication : public Object,
+ public virtual platform::native::UiApplication {
+ public:
+ static WinApplication* GetInstance();
+
+ private:
+ static WinApplication* instance_;
+
+ private:
+ explicit WinApplication(HINSTANCE h_instance);
+
+ public:
+ WinApplication(const WinApplication&) = delete;
+ WinApplication(WinApplication&&) = delete;
+ WinApplication& operator=(const WinApplication&) = delete;
+ WinApplication& operator=(WinApplication&&) = delete;
+ ~WinApplication() override;
+
+ public:
+ int Run() override;
+ void Quit(int quit_code) override;
+
+ void InvokeLater(const std::function<void()>& action) override;
+ unsigned long SetTimeout(std::chrono::milliseconds milliseconds,
+ const std::function<void()>& action) override;
+ unsigned long SetInterval(std::chrono::milliseconds milliseconds,
+ const std::function<void()>& action) override;
+ void CancelTimer(unsigned long id) override;
+
+ std::vector<platform::native::NativeWindow*> GetAllWindow() override;
+ platform::native::NativeWindow* CreateWindow(
+ platform::native::NativeWindow* parent) override;
+
+ HINSTANCE GetInstanceHandle() const { return h_instance_; }
+
+ GodWindow* GetGodWindow() const { return god_window_.get(); }
+ 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::win::native
diff --git a/include/cru/win/native/win_native_window.hpp b/include/cru/win/native/win_native_window.hpp
new file mode 100644
index 00000000..2b9be25d
--- /dev/null
+++ b/include/cru/win/native/win_native_window.hpp
@@ -0,0 +1,137 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/native/native_window.hpp"
+#include "window_native_message_event_args.hpp"
+
+#include <memory>
+
+namespace cru::win::native {
+class WinApplication;
+class WindowClass;
+class WindowManager;
+class WindowRenderTarget;
+
+class WinNativeWindow : public Object,
+ public virtual platform::native::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;
+
+ platform::graph::Painter* BeginPaint() override;
+
+ Event<>* DestroyEvent() override { return &destroy_event_; }
+ Event<const 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<const ui::Point&>* MouseMoveEvent() override {
+ return &mouse_move_event_;
+ }
+ Event<platform::native::MouseButton, const ui::Point&>* MouseDownEvent()
+ override {
+ return &mouse_down_event_;
+ }
+ Event<platform::native::MouseButton, const 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);
+
+ WindowRenderTarget* GetWindowRenderTarget() const {
+ return window_render_target_.get();
+ }
+
+ 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(platform::native::MouseButton button, POINT point);
+ void OnMouseUpInternal(platform::native::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;
+
+ std::shared_ptr<WindowRenderTarget> window_render_target_;
+
+ Event<> destroy_event_;
+ Event<const ui::Size&> resize_event_;
+ Event<> paint_event_;
+ Event<bool> focus_event_;
+ Event<bool> mouse_enter_leave_event_;
+ Event<const ui::Point&> mouse_move_event_;
+ Event<platform::native::MouseButton, const ui::Point&> mouse_down_event_;
+ Event<platform::native::MouseButton, const ui::Point&> mouse_up_event_;
+ Event<int> key_down_event_;
+ Event<int> key_up_event_;
+
+ Event<WindowNativeMessageEventArgs&> native_message_event_;
+};
+} // namespace cru::win::native
diff --git a/include/cru/win/native/window_class.hpp b/include/cru/win/native/window_class.hpp
new file mode 100644
index 00000000..17712958
--- /dev/null
+++ b/include/cru/win/native/window_class.hpp
@@ -0,0 +1,27 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/common/base.hpp"
+
+#include <string>
+
+namespace cru::win::native {
+class WindowClass : public Object {
+ public:
+ WindowClass(const std::wstring& name, WNDPROC window_proc,
+ HINSTANCE h_instance);
+ WindowClass(const WindowClass& other) = delete;
+ WindowClass(WindowClass&& other) = delete;
+ WindowClass& operator=(const WindowClass& other) = delete;
+ WindowClass& operator=(WindowClass&& other) = delete;
+ ~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::win::native
diff --git a/include/cru/win/native/window_native_message_event_args.hpp b/include/cru/win/native/window_native_message_event_args.hpp
new file mode 100644
index 00000000..37149f36
--- /dev/null
+++ b/include/cru/win/native/window_native_message_event_args.hpp
@@ -0,0 +1,45 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/common/base.hpp"
+
+namespace cru::win::native {
+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::win::native
diff --git a/include/cru/win/native/window_render_target.hpp b/include/cru/win/native/window_render_target.hpp
new file mode 100644
index 00000000..c55055c6
--- /dev/null
+++ b/include/cru/win/native/window_render_target.hpp
@@ -0,0 +1,45 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/common/base.hpp"
+
+namespace cru::win::graph {
+class GraphManager;
+}
+
+namespace cru::win::native {
+// Represents a window render target.
+class WindowRenderTarget : public Object {
+ public:
+ WindowRenderTarget(graph::GraphManager* graph_manager, HWND hwnd);
+ WindowRenderTarget(const WindowRenderTarget& other) = delete;
+ WindowRenderTarget(WindowRenderTarget&& other) = delete;
+ WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete;
+ WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete;
+ ~WindowRenderTarget() override = default;
+
+ public:
+ // Get the graph manager that created the render target.
+ graph::GraphManager* GetGraphManager() const { return graph_manager_; }
+
+ // Get the target bitmap which can be set as the ID2D1DeviceContext's target.
+ ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.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::GraphManager* graph_manager_;
+ Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_;
+ Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_;
+};
+} // namespace cru::win::native
diff --git a/include/cru/win/win_pre_config.hpp b/include/cru/win/win_pre_config.hpp
new file mode 100644
index 00000000..6962eb7b
--- /dev/null
+++ b/include/cru/win/win_pre_config.hpp
@@ -0,0 +1,13 @@
+#include "cru/common/pre_config.hpp"
+
+#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+#undef CreateWindow
+#undef DrawText
+
+#include <d2d1_2.h>
+#include <d3d11.h>
+#include <dwrite.h>
+#include <dxgi1_2.h>
+#include <wrl/client.h>