From 7351020a582d70a1495249fba87d342c8a1fb634 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Apr 2019 19:42:46 +0800 Subject: Refactor. --- art/project_structure.png | Bin 0 -> 81434 bytes include/cru/platform/basic_types.hpp | 11 - include/cru/platform/brush.hpp | 13 - include/cru/platform/debug.hpp | 2 +- include/cru/platform/exception.hpp | 11 + include/cru/platform/font.hpp | 8 - include/cru/platform/geometry.hpp | 26 -- include/cru/platform/graph/brush.hpp | 13 + include/cru/platform/graph/font.hpp | 8 + include/cru/platform/graph/geometry.hpp | 25 ++ include/cru/platform/graph/graph_factory.hpp | 26 ++ include/cru/platform/graph/painter.hpp | 26 ++ include/cru/platform/graph/painter_util.hpp | 18 ++ include/cru/platform/graph/text_layout.hpp | 24 ++ include/cru/platform/graph_factory.hpp | 24 -- include/cru/platform/native/basic_types.hpp | 11 + include/cru/platform/native/native_window.hpp | 50 +++ include/cru/platform/native/ui_applicaition.hpp | 31 ++ include/cru/platform/native_window.hpp | 48 --- include/cru/platform/painter.hpp | 26 -- include/cru/platform/painter_util.hpp | 18 -- include/cru/platform/string_util.hpp | 2 +- include/cru/platform/text_layout.hpp | 24 -- include/cru/platform/ui_applicaition.hpp | 32 -- include/cru/platform/win/d2d_util.hpp | 108 ------- include/cru/platform/win/exception.hpp | 49 --- include/cru/platform/win/god_window.hpp | 32 -- include/cru/platform/win/graph_manager.hpp | 44 --- include/cru/platform/win/win_application.hpp | 67 ---- include/cru/platform/win/win_brush.hpp | 31 -- include/cru/platform/win/win_font.hpp | 26 -- include/cru/platform/win/win_geometry.hpp | 45 --- include/cru/platform/win/win_graph_factory.hpp | 28 -- include/cru/platform/win/win_native_window.hpp | 132 -------- include/cru/platform/win/win_painter.hpp | 42 --- include/cru/platform/win/win_pre_config.hpp | 13 - include/cru/platform/win/win_text_layout.hpp | 42 --- include/cru/platform/win/window_class.hpp | 26 -- .../win/window_native_message_event_args.hpp | 45 --- include/cru/platform/win/window_render_target.hpp | 44 --- include/cru/ui/control.hpp | 6 +- include/cru/ui/event/ui_event.hpp | 23 +- include/cru/ui/render/border_render_object.hpp | 16 +- .../cru/ui/render/flex_layout_render_object.hpp | 2 +- include/cru/ui/render/render_object.hpp | 4 +- include/cru/ui/render/text_render_object.hpp | 32 +- include/cru/ui/render/window_render_object.hpp | 2 +- include/cru/ui/ui_manager.hpp | 10 +- include/cru/ui/window.hpp | 13 +- include/cru/win/exception.hpp | 51 ++++ include/cru/win/graph/d2d_util.hpp | 108 +++++++ include/cru/win/graph/graph_manager.hpp | 51 ++++ include/cru/win/graph/win_brush.hpp | 33 ++ include/cru/win/graph/win_font.hpp | 28 ++ include/cru/win/graph/win_geometry.hpp | 48 +++ include/cru/win/graph/win_graph_factory.hpp | 30 ++ include/cru/win/graph/win_painter.hpp | 48 +++ include/cru/win/graph/win_text_layout.hpp | 42 +++ include/cru/win/native/god_window.hpp | 32 ++ include/cru/win/native/win_application.hpp | 59 ++++ include/cru/win/native/win_native_window.hpp | 137 +++++++++ include/cru/win/native/window_class.hpp | 27 ++ .../native/window_native_message_event_args.hpp | 45 +++ include/cru/win/native/window_render_target.hpp | 45 +++ include/cru/win/win_pre_config.hpp | 13 + src/CMakeLists.txt | 22 +- src/main.cpp | 6 +- src/platform_win/CMakeLists.txt | 21 -- src/platform_win/debug.cpp | 10 - src/platform_win/dpi_util.hpp | 32 -- src/platform_win/exception.cpp | 50 --- src/platform_win/god_window.cpp | 72 ----- src/platform_win/god_window_message.hpp | 6 - src/platform_win/graph_manager.cpp | 51 ---- src/platform_win/string_util.cpp | 20 -- src/platform_win/timer.cpp | 28 -- src/platform_win/timer.hpp | 34 --- src/platform_win/win_application.cpp | 105 ------- src/platform_win/win_brush.cpp | 21 -- src/platform_win/win_font.cpp | 25 -- src/platform_win/win_geometry.cpp | 65 ---- src/platform_win/win_graph_factory.cpp | 46 --- src/platform_win/win_native_window.cpp | 336 -------------------- src/platform_win/win_painter.cpp | 104 ------- src/platform_win/win_text_layout.cpp | 92 ------ src/platform_win/window_class.cpp | 28 -- src/platform_win/window_manager.cpp | 54 ---- src/platform_win/window_manager.hpp | 52 ---- src/platform_win/window_render_target.cpp | 89 ------ src/ui/CMakeLists.txt | 22 ++ src/ui/control.cpp | 4 +- src/ui/render/border_render_object.cpp | 32 +- src/ui/render/flex_layout_render_object.cpp | 11 +- src/ui/render/render_object.cpp | 8 +- src/ui/render/text_render_object.cpp | 28 +- src/ui/render/window_render_object.cpp | 8 +- src/ui/ui_manager.cpp | 17 +- src/ui/window.cpp | 16 +- src/win/CMakeLists.txt | 10 + src/win/debug.cpp | 9 + src/win/exception.cpp | 50 +++ src/win/graph/CMakeLists.txt | 9 + src/win/graph/graph_manager.cpp | 59 ++++ src/win/graph/win_brush.cpp | 23 ++ src/win/graph/win_font.cpp | 28 ++ src/win/graph/win_geometry.cpp | 66 ++++ src/win/graph/win_graph_factory.cpp | 46 +++ src/win/graph/win_painter.cpp | 99 ++++++ src/win/graph/win_text_layout.cpp | 94 ++++++ src/win/native/CMakeLists.txt | 10 + src/win/native/dpi_util.hpp | 36 +++ src/win/native/god_window.cpp | 73 +++++ src/win/native/god_window_message.hpp | 6 + src/win/native/timer.cpp | 28 ++ src/win/native/timer.hpp | 34 +++ src/win/native/win_application.cpp | 100 ++++++ src/win/native/win_native_window.cpp | 340 +++++++++++++++++++++ src/win/native/window_class.cpp | 28 ++ src/win/native/window_manager.cpp | 54 ++++ src/win/native/window_manager.hpp | 52 ++++ src/win/native/window_painter.cpp | 24 ++ src/win/native/window_painter.hpp | 21 ++ src/win/native/window_render_target.cpp | 90 ++++++ src/win/string_util.cpp | 20 ++ 124 files changed, 2593 insertions(+), 2487 deletions(-) create mode 100644 art/project_structure.png delete mode 100644 include/cru/platform/basic_types.hpp delete mode 100644 include/cru/platform/brush.hpp create mode 100644 include/cru/platform/exception.hpp delete mode 100644 include/cru/platform/font.hpp delete mode 100644 include/cru/platform/geometry.hpp create mode 100644 include/cru/platform/graph/brush.hpp create mode 100644 include/cru/platform/graph/font.hpp create mode 100644 include/cru/platform/graph/geometry.hpp create mode 100644 include/cru/platform/graph/graph_factory.hpp create mode 100644 include/cru/platform/graph/painter.hpp create mode 100644 include/cru/platform/graph/painter_util.hpp create mode 100644 include/cru/platform/graph/text_layout.hpp delete mode 100644 include/cru/platform/graph_factory.hpp create mode 100644 include/cru/platform/native/basic_types.hpp create mode 100644 include/cru/platform/native/native_window.hpp create mode 100644 include/cru/platform/native/ui_applicaition.hpp delete mode 100644 include/cru/platform/native_window.hpp delete mode 100644 include/cru/platform/painter.hpp delete mode 100644 include/cru/platform/painter_util.hpp delete mode 100644 include/cru/platform/text_layout.hpp delete mode 100644 include/cru/platform/ui_applicaition.hpp delete mode 100644 include/cru/platform/win/d2d_util.hpp delete mode 100644 include/cru/platform/win/exception.hpp delete mode 100644 include/cru/platform/win/god_window.hpp delete mode 100644 include/cru/platform/win/graph_manager.hpp delete mode 100644 include/cru/platform/win/win_application.hpp delete mode 100644 include/cru/platform/win/win_brush.hpp delete mode 100644 include/cru/platform/win/win_font.hpp delete mode 100644 include/cru/platform/win/win_geometry.hpp delete mode 100644 include/cru/platform/win/win_graph_factory.hpp delete mode 100644 include/cru/platform/win/win_native_window.hpp delete mode 100644 include/cru/platform/win/win_painter.hpp delete mode 100644 include/cru/platform/win/win_pre_config.hpp delete mode 100644 include/cru/platform/win/win_text_layout.hpp delete mode 100644 include/cru/platform/win/window_class.hpp delete mode 100644 include/cru/platform/win/window_native_message_event_args.hpp delete mode 100644 include/cru/platform/win/window_render_target.hpp create mode 100644 include/cru/win/exception.hpp create mode 100644 include/cru/win/graph/d2d_util.hpp create mode 100644 include/cru/win/graph/graph_manager.hpp create mode 100644 include/cru/win/graph/win_brush.hpp create mode 100644 include/cru/win/graph/win_font.hpp create mode 100644 include/cru/win/graph/win_geometry.hpp create mode 100644 include/cru/win/graph/win_graph_factory.hpp create mode 100644 include/cru/win/graph/win_painter.hpp create mode 100644 include/cru/win/graph/win_text_layout.hpp create mode 100644 include/cru/win/native/god_window.hpp create mode 100644 include/cru/win/native/win_application.hpp create mode 100644 include/cru/win/native/win_native_window.hpp create mode 100644 include/cru/win/native/window_class.hpp create mode 100644 include/cru/win/native/window_native_message_event_args.hpp create mode 100644 include/cru/win/native/window_render_target.hpp create mode 100644 include/cru/win/win_pre_config.hpp delete mode 100644 src/platform_win/CMakeLists.txt delete mode 100644 src/platform_win/debug.cpp delete mode 100644 src/platform_win/dpi_util.hpp delete mode 100644 src/platform_win/exception.cpp delete mode 100644 src/platform_win/god_window.cpp delete mode 100644 src/platform_win/god_window_message.hpp delete mode 100644 src/platform_win/graph_manager.cpp delete mode 100644 src/platform_win/string_util.cpp delete mode 100644 src/platform_win/timer.cpp delete mode 100644 src/platform_win/timer.hpp delete mode 100644 src/platform_win/win_application.cpp delete mode 100644 src/platform_win/win_brush.cpp delete mode 100644 src/platform_win/win_font.cpp delete mode 100644 src/platform_win/win_geometry.cpp delete mode 100644 src/platform_win/win_graph_factory.cpp delete mode 100644 src/platform_win/win_native_window.cpp delete mode 100644 src/platform_win/win_painter.cpp delete mode 100644 src/platform_win/win_text_layout.cpp delete mode 100644 src/platform_win/window_class.cpp delete mode 100644 src/platform_win/window_manager.cpp delete mode 100644 src/platform_win/window_manager.hpp delete mode 100644 src/platform_win/window_render_target.cpp create mode 100644 src/ui/CMakeLists.txt create mode 100644 src/win/CMakeLists.txt create mode 100644 src/win/debug.cpp create mode 100644 src/win/exception.cpp create mode 100644 src/win/graph/CMakeLists.txt create mode 100644 src/win/graph/graph_manager.cpp create mode 100644 src/win/graph/win_brush.cpp create mode 100644 src/win/graph/win_font.cpp create mode 100644 src/win/graph/win_geometry.cpp create mode 100644 src/win/graph/win_graph_factory.cpp create mode 100644 src/win/graph/win_painter.cpp create mode 100644 src/win/graph/win_text_layout.cpp create mode 100644 src/win/native/CMakeLists.txt create mode 100644 src/win/native/dpi_util.hpp create mode 100644 src/win/native/god_window.cpp create mode 100644 src/win/native/god_window_message.hpp create mode 100644 src/win/native/timer.cpp create mode 100644 src/win/native/timer.hpp create mode 100644 src/win/native/win_application.cpp create mode 100644 src/win/native/win_native_window.cpp create mode 100644 src/win/native/window_class.cpp create mode 100644 src/win/native/window_manager.cpp create mode 100644 src/win/native/window_manager.hpp create mode 100644 src/win/native/window_painter.cpp create mode 100644 src/win/native/window_painter.hpp create mode 100644 src/win/native/window_render_target.cpp create mode 100644 src/win/string_util.cpp diff --git a/art/project_structure.png b/art/project_structure.png new file mode 100644 index 00000000..51e15d95 Binary files /dev/null and b/art/project_structure.png differ diff --git a/include/cru/platform/basic_types.hpp b/include/cru/platform/basic_types.hpp deleted file mode 100644 index e6ab4b59..00000000 --- a/include/cru/platform/basic_types.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#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/brush.hpp b/include/cru/platform/brush.hpp deleted file mode 100644 index 713d302d..00000000 --- a/include/cru/platform/brush.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" - -namespace cru::platform { -struct Brush : public virtual Interface {}; - -struct SolidColorBrush : public virtual Brush { - virtual ui::Color GetColor() = 0; - virtual void SetColor(const ui::Color& color) = 0; -}; -} // namespace cru::platform diff --git a/include/cru/platform/debug.hpp b/include/cru/platform/debug.hpp index 21c2b646..18472201 100644 --- a/include/cru/platform/debug.hpp +++ b/include/cru/platform/debug.hpp @@ -3,6 +3,6 @@ #include -namespace cru::platform::debug { +namespace cru::platform { void DebugMessage(const std::wstring_view& message); } diff --git a/include/cru/platform/exception.hpp b/include/cru/platform/exception.hpp new file mode 100644 index 00000000..6dae08c5 --- /dev/null +++ b/include/cru/platform/exception.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "cru/common/pre_config.hpp" + +#include + +namespace cru::platform { +class PlatformException : public std::runtime_error { + public: + using runtime_error::runtime_error; // inherent constructors +}; +} // namespace cru::platform diff --git a/include/cru/platform/font.hpp b/include/cru/platform/font.hpp deleted file mode 100644 index 750aaae1..00000000 --- a/include/cru/platform/font.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -namespace cru::platform { -struct FontDescriptor : virtual Interface { - -}; -} diff --git a/include/cru/platform/geometry.hpp b/include/cru/platform/geometry.hpp deleted file mode 100644 index aa93fe37..00000000 --- a/include/cru/platform/geometry.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include "basic_types.hpp" -#include "cru/common/ui_base.hpp" - -namespace cru::platform { -struct Geometry : virtual Interface { - virtual bool FillContains(const ui::Point& point) = 0; -}; - -struct GeometryBuilder : virtual Interface { - virtual bool IsValid() = 0; - virtual void BeginFigure(const ui::Point& point) = 0; - virtual void LineTo(const ui::Point& point) = 0; - virtual void QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) = 0; - virtual void CloseFigure(bool close) = 0; - virtual Geometry* Build() = 0; - - Geometry* BuildAndDeleteThis() { - Build(); - delete this; - } -}; -} // namespace cru::platform diff --git a/include/cru/platform/graph/brush.hpp b/include/cru/platform/graph/brush.hpp new file mode 100644 index 00000000..8ffbfc97 --- /dev/null +++ b/include/cru/platform/graph/brush.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform::graph { +struct Brush : public virtual Interface {}; + +struct SolidColorBrush : public virtual Brush { + virtual ui::Color GetColor() = 0; + virtual void SetColor(const ui::Color& color) = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp new file mode 100644 index 00000000..5af369e5 --- /dev/null +++ b/include/cru/platform/graph/font.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "cru/common/base.hpp" + +namespace cru::platform::graph { +struct FontDescriptor : virtual Interface { + +}; +} diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp new file mode 100644 index 00000000..7a7ad8b2 --- /dev/null +++ b/include/cru/platform/graph/geometry.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform::graph { +struct Geometry : virtual Interface { + virtual bool FillContains(const ui::Point& point) = 0; +}; + +struct GeometryBuilder : virtual Interface { + virtual bool IsValid() = 0; + virtual void BeginFigure(const ui::Point& point) = 0; + virtual void LineTo(const ui::Point& point) = 0; + virtual void QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) = 0; + virtual void CloseFigure(bool close) = 0; + virtual Geometry* Build() = 0; + + Geometry* BuildAndDeleteThis() { + Build(); + delete this; + } +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp new file mode 100644 index 00000000..51cf6f15 --- /dev/null +++ b/include/cru/platform/graph/graph_factory.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +#include +#include +#include + +namespace cru::platform::graph { +struct SolidColorBrush; +struct GeometryBuilder; +struct FontDescriptor; +struct TextLayout; + +struct GraphFactory : virtual Interface { + static GraphFactory* GetInstance(); + + virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; + virtual GeometryBuilder* CreateGeometryBuilder() = 0; + virtual FontDescriptor* CreateFontDescriptor( + const std::wstring_view& font_family, float font_size) = 0; + virtual TextLayout* CreateTextLayout(std::shared_ptr font, + std::wstring text) = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp new file mode 100644 index 00000000..199ebbad --- /dev/null +++ b/include/cru/platform/graph/painter.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" +#include "../matrix.hpp" + +namespace cru::platform::graph { +struct Brush; +struct Geometry; +struct TextLayout; + +struct Painter : virtual Interface { + virtual Matrix GetTransform() = 0; + virtual void SetTransform(const Matrix& matrix) = 0; + virtual void Clear(const ui::Color& color) = 0; + virtual void StrokeRectangle(const ui::Rect& rectangle, Brush* brush, + float width) = 0; + virtual void FillRectangle(const ui::Rect& rectangle, Brush* brush) = 0; + virtual void StrokeGeometry(Geometry* geometry, Brush* brush, + float width) = 0; + virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; + virtual void DrawText(const ui::Point& offset, TextLayout* text_layout, Brush* brush) = 0; + virtual void EndDraw() = 0; + virtual bool IsDisposed() = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph/painter_util.hpp b/include/cru/platform/graph/painter_util.hpp new file mode 100644 index 00000000..c137e8cb --- /dev/null +++ b/include/cru/platform/graph/painter_util.hpp @@ -0,0 +1,18 @@ +#pragma once +#include "painter.hpp" + +#include +#include + +namespace cru::platform::graph::util { +template +inline void WithTransform(Painter* painter, const Matrix& matrix, + const Fn& action) { + static_assert(std::is_invocable_v, + "Action must can be be invoked with painter."); + const auto old = painter->GetTransform(); + painter->SetTransform(old * matrix); + action(painter); + painter->SetTransform(old); +} +} // namespace cru::platform::util diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp new file mode 100644 index 00000000..eb13c115 --- /dev/null +++ b/include/cru/platform/graph/text_layout.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +#include +#include +#include + +namespace cru::platform::graph { +struct FontDescriptor; + +struct TextLayout : virtual Interface { + virtual std::wstring GetText() = 0; + virtual void SetText(std::wstring new_text) = 0; + virtual std::shared_ptr GetFont() = 0; + virtual void SetFont(std::shared_ptr font) = 0; + virtual void SetMaxWidth(float max_width) = 0; + virtual void SetMaxHeight(float max_height) = 0; + virtual ui::Rect GetTextBounds() = 0; + virtual std::vector TextRangeRect( + const ui::TextRange& text_range) = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph_factory.hpp b/include/cru/platform/graph_factory.hpp deleted file mode 100644 index f6122241..00000000 --- a/include/cru/platform/graph_factory.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" - -#include -#include -#include - -namespace cru::platform { -struct SolidColorBrush; -struct GeometryBuilder; -struct FontDescriptor; -struct TextLayout; - -struct GraphFactory : virtual Interface { - virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; - virtual GeometryBuilder* CreateGeometryBuilder() = 0; - virtual FontDescriptor* CreateFontDescriptor( - const std::wstring_view& font_family, float font_size) = 0; - virtual TextLayout* CreateTextLayout(std::shared_ptr font, - std::wstring text) = 0; -}; -} // namespace cru::platform diff --git a/include/cru/platform/native/basic_types.hpp b/include/cru/platform/native/basic_types.hpp new file mode 100644 index 00000000..573a9e76 --- /dev/null +++ b/include/cru/platform/native/basic_types.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "cru/common/pre_config.hpp" + +namespace cru::platform::native { +struct Dpi { + float x; + float y; +}; + +enum class MouseButton { Left, Right, Middle }; +} // namespace cru::platform diff --git a/include/cru/platform/native/native_window.hpp b/include/cru/platform/native/native_window.hpp new file mode 100644 index 00000000..a82e7a23 --- /dev/null +++ b/include/cru/platform/native/native_window.hpp @@ -0,0 +1,50 @@ +#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::graph { +struct Painter; +} + +namespace cru::platform::native { +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() = 0; + virtual void SetVisible(bool is_visible) = 0; + + virtual ui::Size GetClientSize() = 0; + virtual void SetClientSize(const ui::Size& size) = 0; + + // Get the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + virtual ui::Rect GetWindowRect() = 0; + + // Set the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + virtual void SetWindowRect(const ui::Rect& rect) = 0; + + virtual graph::Painter* BeginPaint() = 0; + + virtual Event<>* DestroyEvent() = 0; + virtual Event* ResizeEvent() = 0; + virtual Event<>* PaintEvent() = 0; + virtual Event* FocusEvent() = 0; + virtual Event* MouseEnterLeaveEvent() = 0; + virtual Event* MouseMoveEvent() = 0; + virtual Event* MouseDownEvent() = 0; + virtual Event* MouseUpEvent() = 0; + virtual Event* KeyDownEvent() = 0; + virtual Event* KeyUpEvent() = 0; +}; +} // namespace cru::platform::ui diff --git a/include/cru/platform/native/ui_applicaition.hpp b/include/cru/platform/native/ui_applicaition.hpp new file mode 100644 index 00000000..8d42d813 --- /dev/null +++ b/include/cru/platform/native/ui_applicaition.hpp @@ -0,0 +1,31 @@ +#pragma once +#include "cru/common/base.hpp" + +#include +#include +#include + +namespace cru::platform::graph { +struct GraphFactory; +} + +namespace cru::platform::native { +struct NativeWindow; + +struct UiApplication : public virtual Interface { + static UiApplication* GetInstance(); + + virtual int Run() = 0; + virtual void Quit(int quite_code) = 0; + + virtual void InvokeLater(const std::function& action) = 0; + virtual unsigned long SetTimeout(std::chrono::milliseconds milliseconds, + const std::function& action) = 0; + virtual unsigned long SetInterval(std::chrono::milliseconds milliseconds, + const std::function& action) = 0; + virtual void CancelTimer(unsigned long id) = 0; + + virtual std::vector GetAllWindow() = 0; + virtual NativeWindow* CreateWindow(NativeWindow* parent) = 0; +}; +} // namespace cru::platform::ui diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native_window.hpp deleted file mode 100644 index f68fd3a4..00000000 --- a/include/cru/platform/native_window.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#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() = 0; - virtual void SetVisible(bool is_visible) = 0; - - virtual ui::Size GetClientSize() = 0; - virtual void SetClientSize(const ui::Size& size) = 0; - - // Get the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - virtual ui::Rect GetWindowRect() = 0; - - // Set the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - virtual void SetWindowRect(const ui::Rect& rect) = 0; - - virtual Painter* BeginPaint() = 0; - - virtual Event<>* DestroyEvent() = 0; - virtual Event* ResizeEvent() = 0; - virtual Event<>* PaintEvent() = 0; - virtual Event* FocusEvent() = 0; - virtual Event* MouseEnterLeaveEvent() = 0; - virtual Event* MouseMoveEvent() = 0; - virtual Event* MouseDownEvent() = 0; - virtual Event* MouseUpEvent() = 0; - virtual Event* KeyDownEvent() = 0; - virtual Event* KeyUpEvent() = 0; -}; -} // namespace cru::platform diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/painter.hpp deleted file mode 100644 index b7b89fc9..00000000 --- a/include/cru/platform/painter.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" -#include "matrix.hpp" - -namespace cru::platform { -struct Brush; -struct Geometry; -struct TextLayout; - -struct Painter : virtual Interface { - virtual Matrix GetTransform() = 0; - virtual void SetTransform(const Matrix& matrix) = 0; - virtual void Clear(const ui::Color& color) = 0; - virtual void StrokeRectangle(const ui::Rect& rectangle, Brush* brush, - float width) = 0; - virtual void FillRectangle(const ui::Rect& rectangle, Brush* brush) = 0; - virtual void StrokeGeometry(Geometry* geometry, Brush* brush, - float width) = 0; - virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; - virtual void DrawText(const ui::Point& offset, TextLayout* text_layout, Brush* brush) = 0; - virtual void EndDraw() = 0; - virtual bool IsDisposed() = 0; -}; -} // namespace cru::platform diff --git a/include/cru/platform/painter_util.hpp b/include/cru/platform/painter_util.hpp deleted file mode 100644 index 5711d099..00000000 --- a/include/cru/platform/painter_util.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "painter.hpp" - -#include -#include - -namespace cru::platform::util { -template -inline void WithTransform(Painter* painter, const Matrix& matrix, - const Fn& action) { - static_assert(std::is_invocable_v, - "Action must can be be invoked with painter."); - const auto old = painter->GetTransform(); - painter->SetTransform(old * matrix); - action(painter); - painter->SetTransform(old); -} -} // namespace cru::platform::util diff --git a/include/cru/platform/string_util.hpp b/include/cru/platform/string_util.hpp index c3b9f93b..822c0c95 100644 --- a/include/cru/platform/string_util.hpp +++ b/include/cru/platform/string_util.hpp @@ -4,6 +4,6 @@ #include #include -namespace cru::platform::util { +namespace cru::platform { std::string ToUtf8String(const std::wstring_view& string); } diff --git a/include/cru/platform/text_layout.hpp b/include/cru/platform/text_layout.hpp deleted file mode 100644 index e43adfde..00000000 --- a/include/cru/platform/text_layout.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" - -#include -#include -#include - -namespace cru::platform { -struct FontDescriptor; - -struct TextLayout : virtual Interface { - virtual std::wstring GetText() = 0; - virtual void SetText(std::wstring new_text) = 0; - virtual std::shared_ptr GetFont() = 0; - virtual void SetFont(std::shared_ptr font) = 0; - virtual void SetMaxWidth(float max_width) = 0; - virtual void SetMaxHeight(float max_height) = 0; - virtual ui::Rect GetTextBounds() = 0; - virtual std::vector TextRangeRect( - const ui::TextRange& text_range) = 0; -}; -} // namespace cru::platform diff --git a/include/cru/platform/ui_applicaition.hpp b/include/cru/platform/ui_applicaition.hpp deleted file mode 100644 index 16739f9d..00000000 --- a/include/cru/platform/ui_applicaition.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "cru/common/base.hpp" - -#include "basic_types.hpp" - -#include -#include -#include - -namespace cru::platform { -struct NativeWindow; -struct GraphFactory; - -struct UiApplication : public virtual Interface { - static UiApplication* GetInstance(); - - virtual int Run() = 0; - virtual void Quit(int quite_code) = 0; - - virtual void InvokeLater(const std::function& action) = 0; - virtual unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - const std::function& action) = 0; - virtual unsigned long SetInterval(std::chrono::milliseconds milliseconds, - const std::function& action) = 0; - virtual void CancelTimer(unsigned long id) = 0; - - virtual std::vector GetAllWindow() = 0; - virtual NativeWindow* CreateWindow(NativeWindow* parent) = 0; - - virtual GraphFactory* GetGraphFactory() = 0; -}; -} // namespace cru::platform diff --git a/include/cru/platform/win/d2d_util.hpp b/include/cru/platform/win/d2d_util.hpp deleted file mode 100644 index 51aea4f8..00000000 --- a/include/cru/platform/win/d2d_util.hpp +++ /dev/null @@ -1,108 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../matrix.hpp" -#include "cru/common/ui_base.hpp" - -namespace cru::platform::win::util { -inline D2D1_MATRIX_3X2_F Convert(const 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 Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { - return 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(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::platform::win::util diff --git a/include/cru/platform/win/exception.hpp b/include/cru/platform/win/exception.hpp deleted file mode 100644 index 01b139b4..00000000 --- a/include/cru/platform/win/exception.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include -#include - -namespace cru::platform::win { -class HResultError : public std::runtime_error { - 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 std::runtime_error { - 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::platform::win diff --git a/include/cru/platform/win/god_window.hpp b/include/cru/platform/win/god_window.hpp deleted file mode 100644 index 95a253e9..00000000 --- a/include/cru/platform/win/god_window.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include - -#include "cru/common/base.hpp" - -namespace cru::platform::win { -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 god_window_class_; - HWND hwnd_; -}; -} // namespace cru::platform::win \ No newline at end of file diff --git a/include/cru/platform/win/graph_manager.hpp b/include/cru/platform/win/graph_manager.hpp deleted file mode 100644 index f2e29c22..00000000 --- a/include/cru/platform/win/graph_manager.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../basic_types.hpp" -#include "cru/common/base.hpp" - -namespace cru::platform::win { -class WindowRenderTarget; - -class GraphManager final : public Object { - 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(); - } - - private: - Microsoft::WRL::ComPtr d3d11_device_; - Microsoft::WRL::ComPtr d2d1_factory_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_factory_; - Microsoft::WRL::ComPtr dwrite_factory_; - Microsoft::WRL::ComPtr dwrite_system_font_collection_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_application.hpp b/include/cru/platform/win/win_application.hpp deleted file mode 100644 index f8150aed..00000000 --- a/include/cru/platform/win/win_application.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../ui_applicaition.hpp" -#include "cru/common/base.hpp" - -#include - -namespace cru::platform::win { -class GodWindow; -class TimerManager; -class WindowManager; -class GraphManager; -class WinGraphFactory; - -class WinApplication : public Object, public virtual 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& action) override; - unsigned long SetTimeout(std::chrono::milliseconds milliseconds, - const std::function& action) override; - unsigned long SetInterval(std::chrono::milliseconds milliseconds, - const std::function& action) override; - void CancelTimer(unsigned long id) override; - - std::vector GetAllWindow() override; - NativeWindow* CreateWindow(NativeWindow* parent) override; - - GraphFactory* GetGraphFactory() 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(); } - GraphManager* GetGraphManager() const { return graph_manager_.get(); } - WinGraphFactory* GetWinGraphFactory() const { return graph_factory_.get(); } - - private: - HINSTANCE h_instance_; - - std::shared_ptr god_window_; - std::shared_ptr timer_manager_; - std::shared_ptr window_manager_; - std::shared_ptr graph_manager_; - std::shared_ptr graph_factory_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_brush.hpp b/include/cru/platform/win/win_brush.hpp deleted file mode 100644 index d32f6bbc..00000000 --- a/include/cru/platform/win/win_brush.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../brush.hpp" - -namespace cru::platform::win { -struct WinBrush : virtual Brush { - virtual ID2D1Brush* GetD2DBrush() = 0; -}; - -class WinSolidColorBrush : public Object, - public virtual SolidColorBrush, - public virtual WinBrush { - public: - explicit WinSolidColorBrush( - Microsoft::WRL::ComPtr brush); - 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 brush_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_font.hpp b/include/cru/platform/win/win_font.hpp deleted file mode 100644 index f03ff3c8..00000000 --- a/include/cru/platform/win/win_font.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../font.hpp" - -#include - -namespace cru::platform::win { -class GraphManager; - -class WinFontDescriptor : public Object, public virtual 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 text_format_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_geometry.hpp b/include/cru/platform/win/win_geometry.hpp deleted file mode 100644 index e8ab7796..00000000 --- a/include/cru/platform/win/win_geometry.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../geometry.hpp" - -namespace cru::platform::win { -class WinGeometryBuilder : public Object, public virtual GeometryBuilder { - public: - explicit WinGeometryBuilder(Microsoft::WRL::ComPtr geometry); - 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; - Geometry* Build() override; - - private: - Microsoft::WRL::ComPtr geometry_; - Microsoft::WRL::ComPtr geometry_sink_; -}; - -class WinGeometry : public Object, public virtual Geometry { - public: - explicit WinGeometry(Microsoft::WRL::ComPtr 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 geometry_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_graph_factory.hpp b/include/cru/platform/win/win_graph_factory.hpp deleted file mode 100644 index ca83be86..00000000 --- a/include/cru/platform/win/win_graph_factory.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../graph_factory.hpp" - -namespace cru::platform::win { -class GraphManager; - -class WinGraphFactory : public Object, public virtual 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; - - SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) override; - GeometryBuilder* CreateGeometryBuilder() override; - FontDescriptor* CreateFontDescriptor(const std::wstring_view& font_family, - float font_size); - TextLayout* CreateTextLayout(std::shared_ptr font, - std::wstring text); - - private: - GraphManager* graph_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 deleted file mode 100644 index 7b93fd5c..00000000 --- a/include/cru/platform/win/win_native_window.hpp +++ /dev/null @@ -1,132 +0,0 @@ -#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 WindowRenderTarget; - -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; - - Painter* BeginPaint() 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); - - 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(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; - - std::shared_ptr window_render_target_; - - 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/win_painter.hpp b/include/cru/platform/win/win_painter.hpp deleted file mode 100644 index 3c37ccb2..00000000 --- a/include/cru/platform/win/win_painter.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../painter.hpp" - -namespace cru::platform::win { -class WinNativeWindow; - -class WinPainter : public Object, public virtual Painter { - public: - explicit WinPainter(WinNativeWindow* window); - WinPainter(const WinPainter& other) = delete; - WinPainter(WinPainter&& other) = delete; - WinPainter& operator=(const WinPainter& other) = delete; - WinPainter& operator=(WinPainter&& other) = delete; - ~WinPainter() override; - - Matrix GetTransform() override; - void SetTransform(const Matrix& matrix) override; - void Clear(const ui::Color& color) override; - void StrokeRectangle(const ui::Rect& rectangle, Brush* brush, - float width) override; - void FillRectangle(const ui::Rect& rectangle, Brush* brush) override; - void StrokeGeometry(Geometry* geometry, Brush* brush, float width) override; - void FillGeometry(Geometry* geometry, Brush* brush) override; - void DrawText(const ui::Point& offset, TextLayout* text_layout, - Brush* brush) override; - void EndDraw() override; - bool IsDisposed() override { return is_disposed; } - - void EndDrawAndDeleteThis() { - EndDraw(); - delete this; - } - - private: - WinNativeWindow* window_; - ID2D1RenderTarget* render_target_; - - bool is_disposed = false; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_pre_config.hpp b/include/cru/platform/win/win_pre_config.hpp deleted file mode 100644 index 6962eb7b..00000000 --- a/include/cru/platform/win/win_pre_config.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "cru/common/pre_config.hpp" - -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN -#include -#undef CreateWindow -#undef DrawText - -#include -#include -#include -#include -#include diff --git a/include/cru/platform/win/win_text_layout.hpp b/include/cru/platform/win/win_text_layout.hpp deleted file mode 100644 index dfb54264..00000000 --- a/include/cru/platform/win/win_text_layout.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "../text_layout.hpp" -#include "win_font.hpp" - -#include - -namespace cru::platform::win { -class GraphManager; - -class WinTextLayout : public Object, public virtual TextLayout { - public: - explicit WinTextLayout(GraphManager* graph_manager, - std::shared_ptr 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 GetFont(); - void SetFont(std::shared_ptr font); - void SetMaxWidth(float max_width) override; - void SetMaxHeight(float max_height) override; - ui::Rect GetTextBounds() override; - std::vector TextRangeRect( - const ui::TextRange& text_range) override; - - IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } - - private: - GraphManager* graph_manager_; - std::wstring text_; - std::shared_ptr font_descriptor_; - float max_width_ = 0.0f; - float max_height_ = 0.0f; - Microsoft::WRL::ComPtr text_layout_; -}; -} // namespace cru::platform::win diff --git a/include/cru/platform/win/window_class.hpp b/include/cru/platform/win/window_class.hpp deleted file mode 100644 index be79af13..00000000 --- a/include/cru/platform/win/window_class.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "cru/common/base.hpp" - -#include - -namespace cru::platform::win { -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::ui diff --git a/include/cru/platform/win/window_native_message_event_args.hpp b/include/cru/platform/win/window_native_message_event_args.hpp deleted file mode 100644 index b3419c24..00000000 --- a/include/cru/platform/win/window_native_message_event_args.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#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 diff --git a/include/cru/platform/win/window_render_target.hpp b/include/cru/platform/win/window_render_target.hpp deleted file mode 100644 index f328a84a..00000000 --- a/include/cru/platform/win/window_render_target.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include "win_pre_config.hpp" - -#include "cru/common/base.hpp" - - -namespace cru::platform::win { -class GraphManager; - -// Represents a window render target. -class WindowRenderTarget : public Object { - public: - WindowRenderTarget(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. - 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: - GraphManager* graph_manager_; - Microsoft::WRL::ComPtr dxgi_swap_chain_; - Microsoft::WRL::ComPtr target_bitmap_; -}; -} // namespace cru::graph diff --git a/include/cru/ui/control.hpp b/include/cru/ui/control.hpp index 684af2f0..652efe89 100644 --- a/include/cru/ui/control.hpp +++ b/include/cru/ui/control.hpp @@ -1,7 +1,7 @@ #pragma once #include "cru/common/base.hpp" -#include "cru/platform/basic_types.hpp" +#include "cru/platform/native/basic_types.hpp" #include "event/ui_event.hpp" #include @@ -127,8 +127,8 @@ class Control : public Object { //*************** region: additional mouse event *************** protected: - virtual void OnMouseClickBegin(platform::MouseButton button); - virtual void OnMouseClickEnd(platform::MouseButton button); + virtual void OnMouseClickBegin(platform::native::MouseButton button); + virtual void OnMouseClickEnd(platform::native::MouseButton button); private: Window* window_ = nullptr; diff --git a/include/cru/ui/event/ui_event.hpp b/include/cru/ui/event/ui_event.hpp index ba8940cb..c84116b9 100644 --- a/include/cru/ui/event/ui_event.hpp +++ b/include/cru/ui/event/ui_event.hpp @@ -3,11 +3,11 @@ #include "cru/common/event.hpp" #include "cru/common/ui_base.hpp" -#include "cru/platform/basic_types.hpp" +#include "cru/platform/native/basic_types.hpp" #include -namespace cru::platform { +namespace cru::platform::graph { struct Painter; } @@ -19,9 +19,7 @@ namespace cru::ui::event { class UiEventArgs : public Object { public: UiEventArgs(Object* sender, Object* original_sender) - : sender_(sender), - original_sender_(original_sender), - handled_(false) {} + : sender_(sender), original_sender_(original_sender), handled_(false) {} UiEventArgs(const UiEventArgs& other) = default; UiEventArgs(UiEventArgs&& other) = default; @@ -80,7 +78,8 @@ class MouseEventArgs : public UiEventArgs { class MouseButtonEventArgs : public MouseEventArgs { public: MouseButtonEventArgs(Object* sender, Object* original_sender, - const Point& point, const platform::MouseButton button) + const Point& point, + const platform::native::MouseButton button) : MouseEventArgs(sender, original_sender, point), button_(button) {} MouseButtonEventArgs(const MouseButtonEventArgs& other) = default; MouseButtonEventArgs(MouseButtonEventArgs&& other) = default; @@ -88,10 +87,10 @@ class MouseButtonEventArgs : public MouseEventArgs { MouseButtonEventArgs& operator=(MouseButtonEventArgs&& other) = default; ~MouseButtonEventArgs() override = default; - platform::MouseButton GetMouseButton() const { return button_; } + platform::native::MouseButton GetMouseButton() const { return button_; } private: - platform::MouseButton button_; + platform::native::MouseButton button_; }; class MouseWheelEventArgs : public MouseEventArgs { @@ -114,7 +113,7 @@ class MouseWheelEventArgs : public MouseEventArgs { class PaintEventArgs : public UiEventArgs { public: PaintEventArgs(Object* sender, Object* original_sender, - platform::Painter* painter) + platform::graph::Painter* painter) : UiEventArgs(sender, original_sender), painter_(painter) {} PaintEventArgs(const PaintEventArgs& other) = default; PaintEventArgs(PaintEventArgs&& other) = default; @@ -122,10 +121,10 @@ class PaintEventArgs : public UiEventArgs { PaintEventArgs& operator=(PaintEventArgs&& other) = default; ~PaintEventArgs() = default; - platform::Painter* GetPainter() const { return painter_; } + platform::graph::Painter* GetPainter() const { return painter_; } private: - platform::Painter* painter_; + platform::graph::Painter* painter_; }; class FocusChangeEventArgs : public UiEventArgs { @@ -197,4 +196,4 @@ class CharEventArgs : public UiEventArgs { wchar_t c_; }; */ -} // namespace cru::ui::events +} // namespace cru::ui::event diff --git a/include/cru/ui/render/border_render_object.hpp b/include/cru/ui/render/border_render_object.hpp index 35b98948..44382c63 100644 --- a/include/cru/ui/render/border_render_object.hpp +++ b/include/cru/ui/render/border_render_object.hpp @@ -3,7 +3,7 @@ #include -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct Geometry; } // namespace cru::platform @@ -32,7 +32,7 @@ struct CornerRadius { class BorderRenderObject : public RenderObject { public: - explicit BorderRenderObject(std::shared_ptr brush); + explicit BorderRenderObject(std::shared_ptr brush); BorderRenderObject(const BorderRenderObject& other) = delete; BorderRenderObject(BorderRenderObject&& other) = delete; BorderRenderObject& operator=(const BorderRenderObject& other) = delete; @@ -42,8 +42,8 @@ class BorderRenderObject : public RenderObject { bool IsEnabled() const { return is_enabled_; } void SetEnabled(bool enabled) { is_enabled_ = enabled; } - std::shared_ptr GetBrush() const { return border_brush_; } - void SetBrush(std::shared_ptr new_brush) { + std::shared_ptr GetBrush() const { return border_brush_; } + void SetBrush(std::shared_ptr new_brush) { border_brush_ = std::move(new_brush); } @@ -59,7 +59,7 @@ class BorderRenderObject : public RenderObject { void Refresh() { RecreateGeometry(); } - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -83,11 +83,11 @@ class BorderRenderObject : public RenderObject { private: bool is_enabled_ = false; - std::shared_ptr border_brush_ = nullptr; + std::shared_ptr border_brush_ = nullptr; Thickness border_thickness_{}; CornerRadius corner_radius_{}; - std::shared_ptr geometry_ = nullptr; - std::shared_ptr border_outer_geometry_ = nullptr; + std::shared_ptr geometry_ = nullptr; + std::shared_ptr border_outer_geometry_ = nullptr; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/flex_layout_render_object.hpp b/include/cru/ui/render/flex_layout_render_object.hpp index 278bf8c2..d225e679 100644 --- a/include/cru/ui/render/flex_layout_render_object.hpp +++ b/include/cru/ui/render/flex_layout_render_object.hpp @@ -38,7 +38,7 @@ class FlexLayoutRenderObject : public RenderObject { FlexChildLayoutData* GetChildLayoutData(int position); - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp index 3cd14a6a..7f5f7ac6 100644 --- a/include/cru/ui/render/render_object.hpp +++ b/include/cru/ui/render/render_object.hpp @@ -10,7 +10,7 @@ namespace cru::ui { class Control; } -namespace cru::platform { +namespace cru::platform::graph { struct Painter; } @@ -58,7 +58,7 @@ class RenderObject : public Object { void Measure(const Size& available_size); void Layout(const Rect& rect); - virtual void Draw(platform::Painter* render_target) = 0; + virtual void Draw(platform::graph::Painter* painter) = 0; virtual RenderObject* HitTest(const Point& point) = 0; diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp index 527fcd71..9f03551d 100644 --- a/include/cru/ui/render/text_render_object.hpp +++ b/include/cru/ui/render/text_render_object.hpp @@ -5,18 +5,18 @@ #include // forward declarations -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct FontDescriptor; struct TextLayout; -} // namespace cru::platform +} // namespace cru::platform::graph namespace cru::ui::render { class TextRenderObject : public RenderObject { public: - TextRenderObject(std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush); + TextRenderObject(std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr selection_brush); TextRenderObject(const TextRenderObject& other) = delete; TextRenderObject(TextRenderObject&& other) = delete; TextRenderObject& operator=(const TextRenderObject& other) = delete; @@ -26,13 +26,13 @@ class TextRenderObject : public RenderObject { std::wstring GetText() const; void SetText(std::wstring new_text); - std::shared_ptr GetBrush() const { return brush_; } - void SetBrush(std::shared_ptr new_brush) { + std::shared_ptr GetBrush() const { return brush_; } + void SetBrush(std::shared_ptr new_brush) { new_brush.swap(brush_); } - std::shared_ptr GetFont() const; - void SetFont(std::shared_ptr font); + std::shared_ptr GetFont() const; + void SetFont(std::shared_ptr font); std::optional GetSelectionRange() const { return selection_range_; @@ -41,14 +41,14 @@ class TextRenderObject : public RenderObject { selection_range_ = std::move(new_range); } - std::shared_ptr GetSelectionBrush() const { + std::shared_ptr GetSelectionBrush() const { return selection_brush_; } - void SetSelectionBrush(std::shared_ptr new_brush) { + void SetSelectionBrush(std::shared_ptr new_brush) { new_brush.swap(selection_brush_); } - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; @@ -59,11 +59,11 @@ class TextRenderObject : public RenderObject { void OnLayoutContent(const Rect& content_rect) override; private: - std::shared_ptr brush_; - std::shared_ptr font_; - std::shared_ptr text_layout_; + std::shared_ptr brush_; + std::shared_ptr font_; + std::shared_ptr text_layout_; std::optional selection_range_ = std::nullopt; - std::shared_ptr selection_brush_; + std::shared_ptr selection_brush_; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/window_render_object.hpp b/include/cru/ui/render/window_render_object.hpp index e0a7c74e..dfeae487 100644 --- a/include/cru/ui/render/window_render_object.hpp +++ b/include/cru/ui/render/window_render_object.hpp @@ -17,7 +17,7 @@ class WindowRenderObject : public RenderObject { void MeasureAndLayout(); - void Draw(platform::Painter* painter) override; + void Draw(platform::graph::Painter* painter) override; RenderObject* HitTest(const Point& point) override; diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp index 6f4a3bc0..5317f579 100644 --- a/include/cru/ui/ui_manager.hpp +++ b/include/cru/ui/ui_manager.hpp @@ -3,7 +3,7 @@ #include -namespace cru::platform { +namespace cru::platform::graph { struct Brush; struct FontDescriptor; } // namespace cru::platform @@ -19,12 +19,12 @@ class PredefineResources : public Object { ~PredefineResources() override = default; // region Button - std::shared_ptr button_normal_border_brush; + std::shared_ptr button_normal_border_brush; // region TextBlock - std::shared_ptr text_block_selection_brush; - std::shared_ptr text_block_text_brush; - std::shared_ptr text_block_font; + std::shared_ptr text_block_selection_brush; + std::shared_ptr text_block_text_brush; + std::shared_ptr text_block_font; }; class UiManager : public Object { diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 9a76f30f..0a81e2e1 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -5,7 +5,7 @@ #include -namespace cru::platform { +namespace cru::platform::native { struct NativeWindow; } @@ -38,7 +38,7 @@ class Window final : public ContentControl { render::RenderObject* GetRenderObject() const override; - platform::NativeWindow* GetNativeWindow() const { return native_window_; } + platform::native::NativeWindow* GetNativeWindow() const { return native_window_; } Control* GetMouseHoverControl() const { return mouse_hover_control_; } @@ -66,13 +66,14 @@ class Window final : public ContentControl { void OnNativeMouseEnterLeave(bool enter); void OnNativeMouseMove(const Point& point); - void OnNativeMouseDown(platform::MouseButton button, const Point& point); - void OnNativeMouseUp(platform::MouseButton button, const Point& point); + void OnNativeMouseDown(platform::native::MouseButton button, + const Point& point); + void OnNativeMouseUp(platform::native::MouseButton button, + const Point& point); void OnNativeKeyDown(int virtual_code); void OnNativeKeyUp(int virtual_code); - //*************** region: event dispatcher helper *************** void DispatchMouseHoverControlChangeEvent(Control* old_control, @@ -80,7 +81,7 @@ class Window final : public ContentControl { const Point& point); private: - platform::NativeWindow* native_window_; + platform::native::NativeWindow* native_window_; EventRevokerGuard event_revoker_guard_; std::shared_ptr render_object_; 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 +#include + +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(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 + +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 d3d11_device_; + Microsoft::WRL::ComPtr d2d1_factory_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_factory_; + Microsoft::WRL::ComPtr dwrite_factory_; + Microsoft::WRL::ComPtr dwrite_system_font_collection_; + + std::unique_ptr 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 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 + +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 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 geometry_; + Microsoft::WRL::ComPtr geometry_sink_; +}; + +class WinGeometry : public Object, public virtual platform::graph::Geometry { + public: + explicit WinGeometry(Microsoft::WRL::ComPtr 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 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 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 + +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 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 GetFont(); + void SetFont(std::shared_ptr font); + void SetMaxWidth(float max_width) override; + void SetMaxHeight(float max_height) override; + ui::Rect GetTextBounds() override; + std::vector TextRangeRect( + const ui::TextRange& text_range) override; + + IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } + + private: + GraphManager* graph_manager_; + std::wstring text_; + std::shared_ptr font_descriptor_; + float max_width_ = 0.0f; + float max_height_ = 0.0f; + Microsoft::WRL::ComPtr 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 + +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 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 + +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& action) override; + unsigned long SetTimeout(std::chrono::milliseconds milliseconds, + const std::function& action) override; + unsigned long SetInterval(std::chrono::milliseconds milliseconds, + const std::function& action) override; + void CancelTimer(unsigned long id) override; + + std::vector 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 god_window_; + std::shared_ptr timer_manager_; + std::shared_ptr 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 + +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 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* 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); + + 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 window_render_target_; + + 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::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 + +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 dxgi_swap_chain_; + Microsoft::WRL::ComPtr 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 +#undef CreateWindow +#undef DrawText + +#include +#include +#include +#include +#include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8f29ddc9..b2209bb3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,27 +1,9 @@ -add_library(cru_ui STATIC - ui/content_control.cpp - ui/control.cpp - ui/layout_control.cpp - ui/no_child_control.cpp - ui/ui_manager.cpp - ui/window.cpp - ui/controls/button.cpp - ui/controls/flex_layout.cpp - ui/controls/text_block.cpp - ui/render/border_render_object.cpp - ui/render/flex_layout_render_object.cpp - ui/render/render_object.cpp - ui/render/text_render_object.cpp - ui/render/window_render_object.cpp) - -target_include_directories(cru_ui PUBLIC ${PROJECT_SOURCE_DIR}/include .) +add_subdirectory(ui) if(WIN32) -add_subdirectory(platform_win) -target_link_libraries(cru_ui PUBLIC cru_platform_win) +add_subdirectory(win) endif() - add_executable(demo main.cpp) target_link_libraries(demo PRIVATE cru_ui) diff --git a/src/main.cpp b/src/main.cpp index c0c90002..ecaebda5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,11 @@ -#include "cru/platform/native_window.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/native/native_window.hpp" +#include "cru/platform/native/ui_applicaition.hpp" #include "cru/ui/controls/button.hpp" #include "cru/ui/controls/flex_layout.hpp" #include "cru/ui/controls/text_block.hpp" #include "cru/ui/window.hpp" -using cru::platform::UiApplication; +using cru::platform::native::UiApplication; using cru::ui::Rect; using cru::ui::Thickness; using cru::ui::Window; diff --git a/src/platform_win/CMakeLists.txt b/src/platform_win/CMakeLists.txt deleted file mode 100644 index 27e93091..00000000 --- a/src/platform_win/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -add_library(cru_platform_win STATIC - debug.cpp - exception.cpp - god_window.cpp - graph_manager.cpp - string_util.cpp - timer.cpp - win_application.cpp - win_brush.cpp - win_font.cpp - win_geometry.cpp - win_graph_factory.cpp - win_native_window.cpp - win_painter.cpp - win_text_layout.cpp - window_class.cpp - window_manager.cpp - window_render_target.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/debug.cpp b/src/platform_win/debug.cpp deleted file mode 100644 index f52d41bd..00000000 --- a/src/platform_win/debug.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "cru/platform/win/win_pre_config.hpp" - -#include "cru/platform/debug.hpp" - - -namespace cru::platform::debug { -void DebugMessage(const std::wstring_view& message) { - ::OutputDebugStringW(message.data()); -} -} // namespace cru::debug diff --git a/src/platform_win/dpi_util.hpp b/src/platform_win/dpi_util.hpp deleted file mode 100644 index 92819e0f..00000000 --- a/src/platform_win/dpi_util.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -// The dpi awareness needs to be implemented in the future. Currently we use 96 -// as default. - -namespace cru::platform { -inline Dpi GetDpi() { return Dpi{96.0f, 96.0f}; } - -inline int DipToPixelInternal(const float dip, const float dpi) { - return static_cast(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(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/src/platform_win/exception.cpp b/src/platform_win/exception.cpp deleted file mode 100644 index 3db88b8b..00000000 --- a/src/platform_win/exception.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "cru/platform/win/exception.hpp" - -#include "cru/common/format.hpp" - -namespace cru::platform::win { -using util::Format; - -inline std::string HResultMakeMessage(HRESULT h_result, - const std::string_view* message) { - char buffer[10]; - sprintf_s(buffer, "%#08x", h_result); - - if (message) - return Format( - "An HResultError is thrown. HRESULT: {}.\nAdditional message: {}\n", - buffer, *message); - else - return Format("An HResultError is thrown. HRESULT: {}.\n", buffer); -} - -HResultError::HResultError(HRESULT h_result) - : runtime_error(HResultMakeMessage(h_result, nullptr)), - h_result_(h_result) {} - -HResultError::HResultError(HRESULT h_result, - const std::string_view& additional_message) - : runtime_error(HResultMakeMessage(h_result, &additional_message)), - h_result_(h_result) {} - -inline std::string Win32MakeMessage(DWORD error_code, - const std::string_view* message) { - char buffer[10]; - sprintf_s(buffer, "%#04x", error_code); - - if (message) - return Format("Last error code: {}.\nAdditional message: {}\n", buffer, - *message); - else - return Format("Last error code: {}.\n", buffer); -} - -Win32Error::Win32Error(DWORD error_code) - : runtime_error(Win32MakeMessage(error_code, nullptr)), - error_code_(error_code) {} - -Win32Error::Win32Error(DWORD error_code, - const std::string_view& additional_message) - : runtime_error(Win32MakeMessage(error_code, &additional_message)), - error_code_(error_code) {} -} // namespace cru::platform::win diff --git a/src/platform_win/god_window.cpp b/src/platform_win/god_window.cpp deleted file mode 100644 index 0cb1a0e4..00000000 --- a/src/platform_win/god_window.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "cru/platform/win/god_window.hpp" - -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/win_application.hpp" -#include "cru/platform/win/window_class.hpp" -#include "god_window_message.hpp" -#include "timer.hpp" - -namespace cru::platform::win { -constexpr auto god_window_class_name = L"GodWindowClass"; - -LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, - LPARAM lParam) { - const auto app = WinApplication::GetInstance(); - - if (app) { - 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 - return DefWindowProcW(hWnd, uMsg, wParam, lParam); -} - -GodWindow::GodWindow(WinApplication* application) { - application_ = application; - - const auto h_instance = application->GetInstanceHandle(); - - god_window_class_ = std::make_shared(god_window_class_name, - GodWndProc, h_instance); - - hwnd_ = CreateWindowEx(0, god_window_class_name, L"", 0, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - HWND_MESSAGE, nullptr, h_instance, nullptr); - - if (hwnd_ == nullptr) throw Win32Error(::GetLastError(), "Failed to create god window."); -} - -GodWindow::~GodWindow() { ::DestroyWindow(hwnd_); } - -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*>(w_param); - (*p_action)(); - delete p_action; - *result = 0; - return true; - } - case WM_TIMER: { - const auto id = static_cast(w_param); - const auto action = application_->GetTimerManager()->GetAction(id); - if (action.has_value()) { - (action.value().second)(); - if (!action.value().first) - application_->GetTimerManager()->KillTimer(id); - result = 0; - return true; - } - break; - } - default: - return false; - } - return false; -} -} // namespace cru::platform::win diff --git a/src/platform_win/god_window_message.hpp b/src/platform_win/god_window_message.hpp deleted file mode 100644 index a906a3b7..00000000 --- a/src/platform_win/god_window_message.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -#include "cru/platform/win/win_pre_config.hpp" - -namespace cru::platform::win { -constexpr int invoke_later_message_id = WM_USER + 2000; -} diff --git a/src/platform_win/graph_manager.cpp b/src/platform_win/graph_manager.cpp deleted file mode 100644 index 4961867b..00000000 --- a/src/platform_win/graph_manager.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "cru/platform/win/graph_manager.hpp" - -#include "cru/platform/win/exception.hpp" - -namespace cru::platform::win { -GraphManager::GraphManager() { - UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; - -#ifdef CRU_DEBUG - creation_flags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - const D3D_FEATURE_LEVEL feature_levels[] = { - D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1}; - - Microsoft::WRL::ComPtr d3d11_device_context; - - ThrowIfFailed(D3D11CreateDevice( - nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, - feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, - &d3d11_device_, nullptr, &d3d11_device_context)); - - Microsoft::WRL::ComPtr dxgi_device; - ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); - - ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, - IID_PPV_ARGS(&d2d1_factory_))); - - Microsoft::WRL::ComPtr d2d1_device; - - ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); - - ThrowIfFailed(d2d1_device->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); - - // Identify the physical adapter (GPU or card) this device is runs on. - Microsoft::WRL::ComPtr dxgi_adapter; - ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); - - // Get the factory object that created the DXGI device. - ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); - - ThrowIfFailed( - DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(dwrite_factory_.GetAddressOf()))); - - ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(&dwrite_system_font_collection_)); -} -} // namespace cru::graph diff --git a/src/platform_win/string_util.cpp b/src/platform_win/string_util.cpp deleted file mode 100644 index 8ae069c0..00000000 --- a/src/platform_win/string_util.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "cru/platform/string_util.hpp" - -#include "cru/platform/win/exception.hpp" - -namespace cru::platform::util { -std::string ToUtf8String(const std::wstring_view& string) { - if (string.empty()) return std::string(); - - const auto length = ::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, - nullptr, 0, nullptr, nullptr); - std::string result; - result.resize(length); - if (::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, result.data(), - static_cast(result.size()), nullptr, - nullptr) == 0) - throw win::Win32Error(::GetLastError(), - "Failed to convert wide string to UTF-8."); - return result; -} -} // namespace cru::platform::util diff --git a/src/platform_win/timer.cpp b/src/platform_win/timer.cpp deleted file mode 100644 index 280d1aed..00000000 --- a/src/platform_win/timer.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "timer.hpp" - -namespace cru::platform::win { -TimerManager::TimerManager(GodWindow* god_window) { god_window_ = god_window; } - -UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, - const TimerAction& action) { - const auto id = current_count_++; - ::SetTimer(god_window_->GetHandle(), id, milliseconds, nullptr); - map_.emplace(id, std::make_pair(loop, action)); - return id; -} - -void TimerManager::KillTimer(const UINT_PTR id) { - const auto find_result = map_.find(id); - if (find_result != map_.cend()) { - ::KillTimer(god_window_->GetHandle(), id); - map_.erase(find_result); - } -} - -std::optional> TimerManager::GetAction( - const UINT_PTR id) { - const auto find_result = map_.find(id); - if (find_result == map_.cend()) return std::nullopt; - return find_result->second; -} -} // namespace cru::platform::win diff --git a/src/platform_win/timer.hpp b/src/platform_win/timer.hpp deleted file mode 100644 index 95468b8d..00000000 --- a/src/platform_win/timer.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include "cru/platform/win/win_pre_config.hpp" - -#include -#include -#include -#include - -#include "cru/common/base.hpp" -#include "cru/platform/win/god_window.hpp" - -namespace cru::platform::win { -using TimerAction = std::function; - -class TimerManager : public Object { - public: - TimerManager(GodWindow* god_window); - TimerManager(const TimerManager& other) = delete; - TimerManager(TimerManager&& other) = delete; - TimerManager& operator=(const TimerManager& other) = delete; - TimerManager& operator=(TimerManager&& other) = delete; - ~TimerManager() override = default; - - UINT_PTR CreateTimer(UINT milliseconds, bool loop, const TimerAction& action); - void KillTimer(UINT_PTR id); - std::optional> GetAction(UINT_PTR id); - - private: - GodWindow* god_window_; - - std::map> map_{}; - UINT_PTR current_count_ = 0; -}; -} // namespace cru::platform::win diff --git a/src/platform_win/win_application.cpp b/src/platform_win/win_application.cpp deleted file mode 100644 index ce187136..00000000 --- a/src/platform_win/win_application.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "cru/platform/win/win_application.hpp" - -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/god_window.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "cru/platform/win/win_graph_factory.hpp" -#include "cru/platform/win/win_native_window.hpp" -#include "god_window_message.hpp" -#include "timer.hpp" -#include "window_manager.hpp" - -#include -#include - -namespace cru::platform { -UiApplication* UiApplication::GetInstance() { - return win::WinApplication::GetInstance(); -} -} // namespace cru::platform - -namespace cru::platform::win { -WinApplication* WinApplication::instance_ = nullptr; - -WinApplication* WinApplication::GetInstance() { - if (instance_ == nullptr) - instance_ = new WinApplication(::GetModuleHandleW(nullptr)); - return instance_; -} - -WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { - if (instance_) - throw std::runtime_error("A application instance already exists."); - - instance_ = this; - - if (!::IsWindows8OrGreater()) - throw std::runtime_error("Must run on Windows 8 or later."); - - god_window_ = std::make_shared(this); - timer_manager_ = std::make_shared(god_window_.get()); - window_manager_ = std::make_shared(this); - graph_manager_ = std::make_shared(); - graph_factory_ = std::make_shared(graph_manager_.get()); -} - -WinApplication::~WinApplication() { instance_ = nullptr; } - -int WinApplication::Run() { - MSG msg; - while (GetMessageW(&msg, nullptr, 0, 0)) { - TranslateMessage(&msg); - DispatchMessageW(&msg); - } - return static_cast(msg.wParam); -} - -void WinApplication::Quit(const int quit_code) { ::PostQuitMessage(quit_code); } - -void WinApplication::InvokeLater(const std::function& action) { - // copy the action to a safe place - auto p_action_copy = new std::function(action); - - if (PostMessageW(GetGodWindow()->GetHandle(), invoke_later_message_id, - reinterpret_cast(p_action_copy), 0) == 0) - throw Win32Error(::GetLastError(), "InvokeLater failed to post message."); -} - -unsigned long WinApplication::SetTimeout(std::chrono::milliseconds milliseconds, - const std::function& action) { - return static_cast(timer_manager_->CreateTimer( - static_cast(milliseconds.count()), false, action)); -} - -unsigned long WinApplication::SetInterval( - std::chrono::milliseconds milliseconds, - const std::function& action) { - return static_cast(timer_manager_->CreateTimer( - static_cast(milliseconds.count()), true, action)); -} - -void WinApplication::CancelTimer(unsigned long id) { - timer_manager_->KillTimer(static_cast(id)); -} - -std::vector WinApplication::GetAllWindow() { - const auto&& windows = window_manager_->GetAllWindows(); - std::vector result; - for (const auto w : windows) { - result.push_back(static_cast(w)); - } - return result; -} - -NativeWindow* WinApplication::CreateWindow(NativeWindow* parent) { - WinNativeWindow* p = nullptr; - if (parent != nullptr) { - p = dynamic_cast(parent); - assert(p); - } - return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), - WS_OVERLAPPEDWINDOW, p); -} - -GraphFactory* WinApplication::GetGraphFactory() { return graph_factory_.get(); } -} // namespace cru::platform::win diff --git a/src/platform_win/win_brush.cpp b/src/platform_win/win_brush.cpp deleted file mode 100644 index 189dfd57..00000000 --- a/src/platform_win/win_brush.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "cru/platform/win/win_brush.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" - -#include - -namespace cru::platform::win { -WinSolidColorBrush::WinSolidColorBrush( - Microsoft::WRL::ComPtr brush) { - assert(brush); - brush_ = std::move(brush); -} - -ui::Color cru::platform::win::WinSolidColorBrush::GetColor() { - return util::Convert(brush_->GetColor()); -} -void WinSolidColorBrush::SetColor(const ui::Color& color) { - brush_->SetColor(util::Convert(color)); -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_font.cpp b/src/platform_win/win_font.cpp deleted file mode 100644 index bca70b9f..00000000 --- a/src/platform_win/win_font.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "cru/platform/win/win_font.hpp" - -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" - -#include -#include - -namespace cru::platform::win { -WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager, - const std::wstring_view& font_family, - float font_size) { - std::array buffer; - if (!::GetUserDefaultLocaleName(buffer.data(), buffer.size())) - throw Win32Error(::GetLastError(), "Failed to get locale."); - - ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat( - font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, - DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size, - buffer.data(), &text_format_)); - - ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); - ThrowIfFailed(text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_geometry.cpp b/src/platform_win/win_geometry.cpp deleted file mode 100644 index 1078700b..00000000 --- a/src/platform_win/win_geometry.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "cru/platform/win/win_geometry.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" - -#include - -namespace cru::platform::win { -WinGeometryBuilder::WinGeometryBuilder( - Microsoft::WRL::ComPtr geometry) { - assert(geometry); - ThrowIfFailed(geometry->Open(&geometry_sink_)); - geometry_ = std::move(geometry); -} - -WinGeometryBuilder::~WinGeometryBuilder() { - if (geometry_sink_) { - ThrowIfFailed(geometry_sink_->Close()); - } -} - -void WinGeometryBuilder::BeginFigure(const ui::Point& point) { - assert(IsValid()); - geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); -} - -void WinGeometryBuilder::LineTo(const ui::Point& point) { - assert(IsValid()); - geometry_sink_->AddLine(util::Convert(point)); -} - -void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) { - assert(IsValid()); - geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - util::Convert(control_point), util::Convert(end_point))); -} - -void WinGeometryBuilder::CloseFigure(bool close) { - assert(IsValid()); - geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED - : D2D1_FIGURE_END_OPEN); -} - -Geometry* WinGeometryBuilder::Build() { - assert(IsValid()); - ThrowIfFailed(geometry_sink_->Close()); - geometry_sink_ = nullptr; - const auto geometry = new WinGeometry(geometry_); - geometry_ = nullptr; - return geometry; -} - -WinGeometry::WinGeometry(Microsoft::WRL::ComPtr geometry) { - assert(geometry); - geometry_ = std::move(geometry); -} - -bool WinGeometry::FillContains(const ui::Point& point) { - BOOL result; - ThrowIfFailed(geometry_->FillContainsPoint( - util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); - return result != 0; -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_graph_factory.cpp b/src/platform_win/win_graph_factory.cpp deleted file mode 100644 index 03031b32..00000000 --- a/src/platform_win/win_graph_factory.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "cru/platform/win/win_graph_factory.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "cru/platform/win/win_brush.hpp" -#include "cru/platform/win/win_font.hpp" -#include "cru/platform/win/win_geometry.hpp" -#include "cru/platform/win/win_text_layout.hpp" - -#include -#include - -namespace cru::platform::win { -WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) { - assert(graph_manager); - graph_manager_ = graph_manager; -} - -SolidColorBrush* WinGraphFactory::CreateSolidColorBrush( - const ui::Color& color) { - Microsoft::WRL::ComPtr brush; - ThrowIfFailed(graph_manager_->GetD2D1DeviceContext()->CreateSolidColorBrush( - util::Convert(color), &brush)); - return new WinSolidColorBrush(std::move(brush)); -} - -GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { - Microsoft::WRL::ComPtr geometry; - ThrowIfFailed( - graph_manager_->GetD2D1Factory()->CreatePathGeometry(&geometry)); - return new WinGeometryBuilder(std::move(geometry)); -} - -FontDescriptor* WinGraphFactory::CreateFontDescriptor( - const std::wstring_view& font_family, float font_size) { - return new WinFontDescriptor(graph_manager_, font_family, font_size); -} - -TextLayout* WinGraphFactory::CreateTextLayout( - std::shared_ptr font, std::wstring text) { - const auto f = std::dynamic_pointer_cast(font); - assert(f); - return new WinTextLayout(graph_manager_, std::move(f), std::move(text)); -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_native_window.cpp b/src/platform_win/win_native_window.cpp deleted file mode 100644 index 3f34717f..00000000 --- a/src/platform_win/win_native_window.cpp +++ /dev/null @@ -1,336 +0,0 @@ -#include "cru/platform/win/win_native_window.hpp" - -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/win_application.hpp" -#include "cru/platform/win/win_painter.hpp" -#include "cru/platform/win/window_class.hpp" -#include "cru/platform/win/window_render_target.hpp" -#include "dpi_util.hpp" -#include "window_manager.hpp" - -#include -#include - -namespace cru::platform::win { -WinNativeWindow::WinNativeWindow(WinApplication* application, - std::shared_ptr window_class, - DWORD window_style, WinNativeWindow* parent) { - assert(application); // application can't be null. - assert(parent == nullptr || - parent->IsValid()); // Parent window is not valid. - - application_ = application; - parent_window_ = parent; - - const auto window_manager = application->GetWindowManager(); - - hwnd_ = CreateWindowExW( - 0, window_manager->GetGeneralWindowClass()->GetName(), L"", window_style, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, - application->GetInstanceHandle(), nullptr); - - if (hwnd_ == nullptr) - throw Win32Error(::GetLastError(), "Failed to create window."); - - window_manager->RegisterWindow(hwnd_, this); - - window_render_target_.reset( - new WindowRenderTarget(application->GetGraphManager(), hwnd_)); -} - -WinNativeWindow::~WinNativeWindow() { - if (IsValid()) { - SetDeleteThisOnDestroy(false); // avoid double delete. - Close(); - } -} - -bool WinNativeWindow::IsValid() { return hwnd_ != nullptr; } - -void WinNativeWindow::SetDeleteThisOnDestroy(bool value) { - delete_this_on_destroy_ = value; -} - -void WinNativeWindow::Close() { - if (IsValid()) DestroyWindow(hwnd_); -} - -bool WinNativeWindow::IsVisible() { - if (IsValid()) return ::IsWindowVisible(hwnd_); - return false; -} - -void WinNativeWindow::SetVisible(bool is_visible) { - if (!IsValid()) return; - is_visible ? ShowWindow(hwnd_, SW_SHOWNORMAL) : ShowWindow(hwnd_, SW_HIDE); -} -ui::Size WinNativeWindow::GetClientSize() { - if (!IsValid()) return ui::Size{}; - - const auto pixel_rect = GetClientRectPixel(); - return ui::Size(PixelToDipX(pixel_rect.right), - PixelToDipY(pixel_rect.bottom)); -} - -void WinNativeWindow::SetClientSize(const ui::Size& size) { - if (IsValid()) { - const auto window_style = - static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); - const auto window_ex_style = - static_cast(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); - - RECT rect; - rect.left = 0; - rect.top = 0; - rect.right = DipToPixelX(size.width); - rect.bottom = DipToPixelY(size.height); - if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style)) - throw Win32Error(::GetLastError(), - "Failed to invoke AdjustWindowRectEx."); - - if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, - rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE)) - throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); - } -} - -ui::Rect WinNativeWindow::GetWindowRect() { - if (!IsValid()) return ui::Rect{}; - - RECT rect; - if (!::GetWindowRect(hwnd_, &rect)) - throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect."); - - return ui::Rect::FromVertices(PixelToDipX(rect.left), PixelToDipY(rect.top), - PixelToDipX(rect.right), - PixelToDipY(rect.bottom)); -} - -void WinNativeWindow::SetWindowRect(const ui::Rect& rect) { - if (IsValid()) { - if (!SetWindowPos(hwnd_, nullptr, DipToPixelX(rect.left), - DipToPixelY(rect.top), DipToPixelX(rect.GetRight()), - DipToPixelY(rect.GetBottom()), SWP_NOZORDER)) - throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); - } -} - -Painter* WinNativeWindow::BeginPaint() { - return new WinPainter(this); } - -bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, - WPARAM w_param, LPARAM l_param, - LRESULT* result) { - WindowNativeMessageEventArgs args{ - WindowNativeMessage{hwnd, msg, w_param, l_param}}; - native_message_event_.Raise(args); - if (args.IsHandled()) { - *result = args.GetResult(); - return true; - } - - switch (msg) { - case WM_PAINT: - OnPaintInternal(); - *result = 0; - return true; - case WM_ERASEBKGND: - *result = 1; - return true; - case WM_SETFOCUS: - OnSetFocusInternal(); - *result = 0; - return true; - case WM_KILLFOCUS: - OnKillFocusInternal(); - *result = 0; - return true; - case WM_MOUSEMOVE: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseMoveInternal(point); - *result = 0; - return true; - } - case WM_LBUTTONDOWN: { - POINT point; - point.x = GET_X_LPARAM(l_param); - point.y = GET_Y_LPARAM(l_param); - OnMouseDownInternal(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(w_param)); - *result = 0; - return true; - case WM_KEYUP: - OnKeyUpInternal(static_cast(w_param)); - *result = 0; - return true; - case WM_CHAR: - OnCharInternal(static_cast(w_param)); - *result = 0; - return true; - case WM_SIZE: - OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); - *result = 0; - return true; - case WM_ACTIVATE: - if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) - OnActivatedInternal(); - else if (w_param == WA_INACTIVE) - OnDeactivatedInternal(); - *result = 0; - return true; - case WM_DESTROY: - OnDestroyInternal(); - *result = 0; - return true; - default: - return false; - } -} - -RECT WinNativeWindow::GetClientRectPixel() { - RECT rect; - if (!GetClientRect(hwnd_, &rect)) - throw Win32Error(::GetLastError(), "Failed to invoke GetClientRect."); - return rect; -} - -void WinNativeWindow::OnDestroyInternal() { - application_->GetWindowManager()->UnregisterWindow(hwnd_); - hwnd_ = nullptr; - 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) { - if (!(new_width == 0 && new_height == 0)) { - window_render_target_->ResizeBuffer(new_width, new_height); - resize_event_.Raise( - ui::Size{PixelToDipX(new_width), PixelToDipY(new_height)}); - } -} - -void WinNativeWindow::OnSetFocusInternal() { - has_focus_ = true; - focus_event_.Raise(true); -} - -void WinNativeWindow::OnKillFocusInternal() { - has_focus_ = false; - focus_event_.Raise(false); -} - -inline ui::Point PiToDip(const POINT& pi_point) { - return ui::Point(PixelToDipX(pi_point.x), PixelToDipY(pi_point.y)); -} - -void WinNativeWindow::OnMouseMoveInternal(const POINT point) { - // when mouse was previous outside the window - if (!is_mouse_in_) { - // invoke TrackMouseEvent to have WM_MOUSELEAVE sent. - TRACKMOUSEEVENT tme; - tme.cbSize = sizeof tme; - tme.dwFlags = TME_LEAVE; - tme.hwndTrack = hwnd_; - - TrackMouseEvent(&tme); - - is_mouse_in_ = true; - mouse_enter_leave_event_.Raise(true); - } - - 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/win_painter.cpp b/src/platform_win/win_painter.cpp deleted file mode 100644 index 7c99146a..00000000 --- a/src/platform_win/win_painter.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "cru/platform/win/win_painter.hpp" - -#include "cru/platform/win/d2d_util.hpp" -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "cru/platform/win/win_brush.hpp" -#include "cru/platform/win/win_geometry.hpp" -#include "cru/platform/win/win_native_window.hpp" -#include "cru/platform/win/win_text_layout.hpp" -#include "cru/platform/win/window_render_target.hpp" - -#include - -namespace cru::platform::win { -WinPainter::WinPainter(WinNativeWindow* window) { - assert(window); - window_ = window; - const auto window_render_target = window_->GetWindowRenderTarget(); - render_target_ = - window_render_target->GetGraphManager()->GetD2D1DeviceContext(); - window_render_target->SetAsTarget(); - render_target_->BeginDraw(); -} - -WinPainter::~WinPainter() { - if (!IsDisposed()) { - ThrowIfFailed(render_target_->EndDraw()); - } -} - -Matrix WinPainter::GetTransform() { - assert(!IsDisposed()); - D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); - return util::Convert(m); -} - -void WinPainter::SetTransform(const Matrix& matrix) { - assert(!IsDisposed()); - render_target_->SetTransform(util::Convert(matrix)); -} - -void WinPainter::Clear(const ui::Color& color) { - assert(!IsDisposed()); - render_target_->Clear(util::Convert(color)); -} - -void WinPainter::StrokeRectangle(const ui::Rect& rectangle, Brush* brush, - float width) { - assert(!IsDisposed()); - const auto b = dynamic_cast(brush); - assert(b); - render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), - width); -} - -void WinPainter::FillRectangle(const ui::Rect& rectangle, Brush* brush) { - assert(!IsDisposed()); - const auto b = dynamic_cast(brush); - assert(b); - render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); -} - -void WinPainter::StrokeGeometry(Geometry* geometry, Brush* brush, float width) { - assert(!IsDisposed()); - const auto g = dynamic_cast(geometry); - assert(g); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); -} - -void WinPainter::FillGeometry(Geometry* geometry, Brush* brush) { - assert(!IsDisposed()); - const auto g = dynamic_cast(geometry); - assert(g); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); -} - -void WinPainter::DrawText(const ui::Point& offset, TextLayout* text_layout, - Brush* brush) { - assert(!IsDisposed()); - const auto t = dynamic_cast(text_layout); - assert(t); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->DrawTextLayout(util::Convert(offset), - t->GetDWriteTextLayout(), b->GetD2DBrush()); -} - -void WinPainter::EndDraw() { - if (!IsDisposed()) { - ThrowIfFailed(render_target_->EndDraw()); - render_target_ = nullptr; - window_->GetWindowRenderTarget()->Present(); - is_disposed = true; - } -} -} // namespace cru::platform::win diff --git a/src/platform_win/win_text_layout.cpp b/src/platform_win/win_text_layout.cpp deleted file mode 100644 index 04b5e928..00000000 --- a/src/platform_win/win_text_layout.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include "cru/platform/win/win_text_layout.hpp" - -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" - -#include -#include - -namespace cru::platform::win { -WinTextLayout::WinTextLayout(GraphManager* graph_manager, - std::shared_ptr font, - std::wstring text) { - assert(graph_manager); - assert(font); - graph_manager_ = graph_manager; - text_.swap(text); - font_descriptor_.swap(font); - - ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), static_cast(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); -} - -std::wstring WinTextLayout::GetText() { return text_; } - -void WinTextLayout::SetText(std::wstring new_text) { - text_.swap(new_text); - ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), static_cast(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); -} - -std::shared_ptr WinTextLayout::GetFont() { - return font_descriptor_; -} - -void WinTextLayout::SetFont(std::shared_ptr font) { - auto f = std::dynamic_pointer_cast(font); - assert(f); - f.swap(font_descriptor_); - ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), static_cast(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); -} - -void WinTextLayout::SetMaxWidth(float max_width) { - max_width_ = max_width; - ThrowIfFailed(text_layout_->SetMaxWidth(max_width_)); -} - -void WinTextLayout::SetMaxHeight(float max_height) { - max_height_ = max_height; - ThrowIfFailed(text_layout_->SetMaxHeight(max_height_)); -} - -ui::Rect WinTextLayout::GetTextBounds() { - DWRITE_TEXT_METRICS metrics; - ThrowIfFailed(text_layout_->GetMetrics(&metrics)); - return ui::Rect{metrics.left, metrics.top, metrics.width, metrics.height}; -} - -std::vector WinTextLayout::TextRangeRect( - const ui::TextRange& text_range) { - DWRITE_TEXT_METRICS text_metrics; - ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); - const auto metrics_count = - text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; - - std::vector hit_test_metrics(metrics_count); - UINT32 actual_count; - text_layout_->HitTestTextRange(text_range.position, text_range.count, 0, 0, - hit_test_metrics.data(), metrics_count, - &actual_count); - - hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, - hit_test_metrics.cend()); - - std::vector result; - result.reserve(actual_count); - - for (const auto& metrics : hit_test_metrics) { - result.push_back(ui::Rect{metrics.left, metrics.top, - metrics.left + metrics.width, - metrics.top + metrics.height}); - } - - return result; -} -} // namespace cru::platform::win diff --git a/src/platform_win/window_class.cpp b/src/platform_win/window_class.cpp deleted file mode 100644 index b58f53b2..00000000 --- a/src/platform_win/window_class.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "cru/platform/win/window_class.hpp" - -#include "cru/platform/win/exception.hpp" - -namespace cru::platform::win { -WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, - HINSTANCE h_instance) - : name_(name) { - WNDCLASSEX window_class; - window_class.cbSize = sizeof(WNDCLASSEX); - - window_class.style = CS_HREDRAW | CS_VREDRAW; - window_class.lpfnWndProc = window_proc; - window_class.cbClsExtra = 0; - window_class.cbWndExtra = 0; - window_class.hInstance = h_instance; - window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); - window_class.hCursor = LoadCursor(NULL, IDC_ARROW); - window_class.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); - window_class.lpszMenuName = NULL; - window_class.lpszClassName = name.c_str(); - window_class.hIconSm = NULL; - - atom_ = ::RegisterClassExW(&window_class); - if (atom_ == 0) - throw Win32Error(::GetLastError(), "Failed to create window class."); -} -} // namespace cru::ui diff --git a/src/platform_win/window_manager.cpp b/src/platform_win/window_manager.cpp deleted file mode 100644 index e6c255c3..00000000 --- a/src/platform_win/window_manager.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "window_manager.hpp" - -#include "cru/platform/win/win_application.hpp" -#include "cru/platform/win/win_native_window.hpp" -#include "cru/platform/win/window_class.hpp" - -#include - -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( - 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 WindowManager::GetAllWindows() const { - std::vector windows; - for (auto [key, value] : window_map_) windows.push_back(value); - return windows; -} -} // namespace cru::platform::win diff --git a/src/platform_win/window_manager.hpp b/src/platform_win/window_manager.hpp deleted file mode 100644 index 6e36ead1..00000000 --- a/src/platform_win/window_manager.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once -#include "cru/platform/win/win_pre_config.hpp" - -#include "cru/common/base.hpp" - -#include -#include -#include - -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 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 GetAllWindows() const; - - private: - WinApplication* application_; - - std::shared_ptr general_window_class_; - std::map window_map_; -}; - -} // namespace cru::platform::win diff --git a/src/platform_win/window_render_target.cpp b/src/platform_win/window_render_target.cpp deleted file mode 100644 index f4836d17..00000000 --- a/src/platform_win/window_render_target.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "cru/platform/win/window_render_target.hpp" - -#include "cru/platform/win/exception.hpp" -#include "cru/platform/win/graph_manager.hpp" -#include "dpi_util.hpp" - -#include - -namespace cru::platform::win { -WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) { - this->graph_manager_ = graph_manager; - - const auto d3d11_device = graph_manager->GetD3D11Device(); - const auto dxgi_factory = graph_manager->GetDxgiFactory(); - - // Allocate a descriptor. - DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; - swap_chain_desc.Width = 0; // use automatic sizing - swap_chain_desc.Height = 0; - swap_chain_desc.Format = - DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format - swap_chain_desc.Stereo = false; - swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling - swap_chain_desc.SampleDesc.Quality = 0; - swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swap_chain_desc.BufferCount = 2; // use double buffering to enable flip - swap_chain_desc.Scaling = DXGI_SCALING_NONE; - swap_chain_desc.SwapEffect = - DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect - swap_chain_desc.Flags = 0; - - // Get the final swap chain for this window from the DXGI factory. - ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd( - d3d11_device, hwnd, &swap_chain_desc, nullptr, nullptr, - &dxgi_swap_chain_)); - - CreateTargetBitmap(); -} - -void WindowRenderTarget::ResizeBuffer(const int width, const int height) { - const auto graph_manager = graph_manager_; - const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); - - Microsoft::WRL::ComPtr old_target; - d2d1_device_context->GetTarget(&old_target); - const auto target_this = old_target == this->target_bitmap_; - if (target_this) d2d1_device_context->SetTarget(nullptr); - - old_target = nullptr; - target_bitmap_ = nullptr; - - ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, - DXGI_FORMAT_UNKNOWN, 0)); - - CreateTargetBitmap(); - - if (target_this) d2d1_device_context->SetTarget(target_bitmap_.Get()); -} - -void WindowRenderTarget::SetAsTarget() { - graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get()); -} - -void WindowRenderTarget::Present() { - ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); -} - -void WindowRenderTarget::CreateTargetBitmap() { - assert(target_bitmap_ == nullptr); // target bitmap must not exist. - - // Direct2D needs the dxgi version of the backbuffer surface pointer. - Microsoft::WRL::ComPtr dxgi_back_buffer; - ThrowIfFailed( - dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); - - const auto dpi = GetDpi(); - - auto bitmap_properties = D2D1::BitmapProperties1( - D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, - D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), - dpi.x, dpi.y); - - // Get a D2D surface from the DXGI back buffer to use as the D2D render - // target. - ThrowIfFailed( - graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( - dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); -} -} // namespace cru::platform::win diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt new file mode 100644 index 00000000..900948ca --- /dev/null +++ b/src/ui/CMakeLists.txt @@ -0,0 +1,22 @@ +add_library(cru_ui STATIC + content_control.cpp + control.cpp + layout_control.cpp + no_child_control.cpp + ui_manager.cpp + window.cpp + controls/button.cpp + controls/flex_layout.cpp + controls/text_block.cpp + render/border_render_object.cpp + render/flex_layout_render_object.cpp + render/render_object.cpp + render/text_render_object.cpp + render/window_render_object.cpp) + +target_include_directories(cru_ui PUBLIC ${PROJECT_SOURCE_DIR}/include .) + +if(WIN32) +target_link_libraries(cru_ui PUBLIC cru_win_native) +endif() + diff --git a/src/ui/control.cpp b/src/ui/control.cpp index f915af07..7563dc91 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -63,7 +63,7 @@ void Control::OnAttachToWindow(Window* window) {} void Control::OnDetachToWindow(Window* window) {} -void Control::OnMouseClickBegin(platform::MouseButton button) {} +void Control::OnMouseClickBegin(platform::native::MouseButton button) {} -void Control::OnMouseClickEnd(platform::MouseButton button) {} +void Control::OnMouseClickEnd(platform::native::MouseButton button) {} } // namespace cru::ui diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index 97e386dd..541be473 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -1,26 +1,26 @@ - #include "cru/ui/render/border_render_object.hpp" +#include "cru/ui/render/border_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/geometry.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/painter_util.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/geometry.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/graph/painter_util.hpp" #include #include namespace cru::ui::render { -BorderRenderObject::BorderRenderObject(std::shared_ptr brush) { +BorderRenderObject::BorderRenderObject( + std::shared_ptr brush) { assert(brush); this->border_brush_ = std::move(brush); RecreateGeometry(); } -void BorderRenderObject::Draw(platform::Painter* painter) { +void BorderRenderObject::Draw(platform::graph::Painter* painter) { painter->FillGeometry(geometry_.get(), border_brush_.get()); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform( + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto p) { child->Draw(p); }); } @@ -75,13 +75,13 @@ void BorderRenderObject::OnMeasureCore(const Size& available_size) { auto coerced_margin_border_padding_size = margin_border_padding_size; if (coerced_margin_border_padding_size.width > available_size.width) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: horizontal length of padding, border and margin is bigger " L"than available length."); coerced_margin_border_padding_size.width = available_size.width; } if (coerced_margin_border_padding_size.height > available_size.height) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: vertical length of padding, border and margin is bigger " L"than available length."); coerced_margin_border_padding_size.height = available_size.height; @@ -113,13 +113,13 @@ void BorderRenderObject::OnLayoutCore(const Rect& rect) { auto coerced_content_available_size = content_available_size; if (coerced_content_available_size.width < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: horizontal length of padding, border and margin is bigger " L"than available length."); coerced_content_available_size.width = 0; } if (coerced_content_available_size.height < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: vertical length of padding, border and margin is bigger " L"than " L"available length."); @@ -154,7 +154,7 @@ void BorderRenderObject::RecreateGeometry() { geometry_.reset(); border_outer_geometry_.reset(); - auto f = [](platform::GeometryBuilder* builder, const Rect& rect, + auto f = [](platform::graph::GeometryBuilder* builder, const Rect& rect, const CornerRadius& corner) { builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top)); builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top)); @@ -181,9 +181,9 @@ void BorderRenderObject::RecreateGeometry() { const Rect outer_rect{margin.left, margin.top, size.width - margin.GetHorizontalTotal(), size.height - margin.GetVerticalTotal()}; - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); - std::unique_ptr builder{graph_factory->CreateGeometryBuilder()}; + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); + std::unique_ptr builder{ + graph_factory->CreateGeometryBuilder()}; f(builder.get(), outer_rect, corner_radius_); border_outer_geometry_.reset(builder->Build()); builder.reset(); diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp index e7840b7e..7528439f 100644 --- a/src/ui/render/flex_layout_render_object.cpp +++ b/src/ui/render/flex_layout_render_object.cpp @@ -1,15 +1,13 @@ #include "cru/ui/render/flex_layout_render_object.hpp" #include "cru/platform/debug.hpp" -#include "cru/platform/painter_util.hpp" +#include "cru/platform/graph/painter_util.hpp" #include #include #include namespace cru::ui::render { -using namespace platform; - FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { assert(position >= 0 && position < child_layout_data_.size()); // Position out of bound. @@ -17,11 +15,12 @@ FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) { return &child_layout_data_[position]; } -void FlexLayoutRenderObject::Draw(platform::Painter* painter) { +void FlexLayoutRenderObject::Draw(platform::graph::Painter* painter) { for (const auto child : GetChildren()) { auto offset = child->GetOffset(); - util::WithTransform(painter, Matrix::Translation(offset.x, offset.y), - [child](auto p) { child->Draw(p); }); + platform::graph::util::WithTransform( + painter, platform::Matrix::Translation(offset.x, offset.y), + [child](auto p) { child->Draw(p); }); } } diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp index 111128b2..3cb7e4df 100644 --- a/src/ui/render/render_object.cpp +++ b/src/ui/render/render_object.cpp @@ -54,13 +54,13 @@ void RenderObject::OnMeasureCore(const Size& available_size) { auto coerced_margin_padding_size = margin_padding_size; if (coerced_margin_padding_size.width > available_size.width) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: horizontal length of padding and margin is bigger than " L"available length."); coerced_margin_padding_size.width = available_size.width; } if (coerced_margin_padding_size.height > available_size.height) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Measure: vertical length of padding and margin is bigger than " L"available length."); coerced_margin_padding_size.height = available_size.height; @@ -82,13 +82,13 @@ void RenderObject::OnLayoutCore(const Rect& rect) { auto coerced_content_available_size = content_available_size; if (coerced_content_available_size.width < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: horizontal length of padding and margin is bigger than " L"available length."); coerced_content_available_size.width = 0; } if (coerced_content_available_size.height < 0) { - platform::debug::DebugMessage( + platform::DebugMessage( L"Layout: vertical length of padding and margin is bigger than " L"available length."); coerced_content_available_size.height = 0; diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp index bdf48c9a..c886ee7a 100644 --- a/src/ui/render/text_render_object.cpp +++ b/src/ui/render/text_render_object.cpp @@ -1,18 +1,17 @@ #include "cru/ui/render/text_render_object.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/painter_util.hpp" -#include "cru/platform/text_layout.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/graph/text_layout.hpp" #include #include namespace cru::ui::render { TextRenderObject::TextRenderObject( - std::shared_ptr brush, - std::shared_ptr font, - std::shared_ptr selection_brush) { + std::shared_ptr brush, + std::shared_ptr font, + std::shared_ptr selection_brush) { assert(brush); assert(font); assert(selection_brush); @@ -21,8 +20,7 @@ TextRenderObject::TextRenderObject( font.swap(font_); selection_brush.swap(selection_brush_); - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); text_layout_.reset(graph_factory->CreateTextLayout(font_, L"")); } @@ -35,20 +33,22 @@ void TextRenderObject::SetText(std::wstring new_text) { text_layout_->SetText(std::move(new_text)); } -std::shared_ptr TextRenderObject::GetFont() const { +std::shared_ptr TextRenderObject::GetFont() + const { return text_layout_->GetFont(); } -void TextRenderObject::SetFont(std::shared_ptr font) { +void TextRenderObject::SetFont( + std::shared_ptr font) { text_layout_->SetFont(std::move(font)); } -void TextRenderObject::Draw(platform::Painter* painter) { - platform::util::WithTransform( +void TextRenderObject::Draw(platform::graph::Painter* painter) { + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(GetMargin().left + GetPadding().left, GetMargin().top + GetPadding().top), - [this](platform::Painter* p) { + [this](platform::graph::Painter* p) { if (this->selection_range_.has_value()) { const auto&& rects = text_layout_->TextRangeRect(this->selection_range_.value()); diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 781087aa..4fc57ad1 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -1,7 +1,7 @@ #include "cru/ui/render/window_render_object.hpp" -#include "cru/platform/native_window.hpp" -#include "cru/platform/painter_util.hpp" +#include "cru/platform/graph/painter_util.hpp" +#include "cru/platform/native/native_window.hpp" #include "cru/ui/window.hpp" #include @@ -13,11 +13,11 @@ void WindowRenderObject::MeasureAndLayout() { Layout(Rect{Point{}, client_size}); } -void WindowRenderObject::Draw(platform::Painter* painter) { +void WindowRenderObject::Draw(platform::graph::Painter* painter) { painter->Clear(colors::white); if (const auto child = GetChild()) { auto offset = child->GetOffset(); - platform::util::WithTransform( + platform::graph::util::WithTransform( painter, platform::Matrix::Translation(offset.x, offset.y), [child](auto rt) { child->Draw(rt); }); } diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp index b1132ea8..2ac1c8e7 100644 --- a/src/ui/ui_manager.cpp +++ b/src/ui/ui_manager.cpp @@ -1,20 +1,19 @@ #include "cru/ui/ui_manager.hpp" -#include "cru/platform/brush.hpp" -#include "cru/platform/font.hpp" -#include "cru/platform/graph_factory.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/brush.hpp" +#include "cru/platform/graph/font.hpp" +#include "cru/platform/graph/graph_factory.hpp" +#include "cru/platform/native/ui_applicaition.hpp" namespace cru::ui { PredefineResources::PredefineResources() { - const auto graph_factory = - platform::UiApplication::GetInstance()->GetGraphFactory(); + const auto graph_factory = platform::graph::GraphFactory::GetInstance(); - button_normal_border_brush.reset(static_cast( + button_normal_border_brush.reset(static_cast( graph_factory->CreateSolidColorBrush(colors::black))); - text_block_selection_brush.reset(static_cast( + text_block_selection_brush.reset(static_cast( graph_factory->CreateSolidColorBrush(colors::skyblue))); - text_block_text_brush.reset(static_cast( + text_block_text_brush.reset(static_cast( graph_factory->CreateSolidColorBrush(colors::black))); text_block_font.reset(graph_factory->CreateFontDescriptor(L"等线", 24.0f)); } diff --git a/src/ui/window.cpp b/src/ui/window.cpp index f909e0f9..347a7a49 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -1,8 +1,8 @@ #include "cru/ui/window.hpp" -#include "cru/platform/native_window.hpp" -#include "cru/platform/painter.hpp" -#include "cru/platform/ui_applicaition.hpp" +#include "cru/platform/graph/painter.hpp" +#include "cru/platform/native/native_window.hpp" +#include "cru/platform/native/ui_applicaition.hpp" #include "cru/ui/render/window_render_object.hpp" #include @@ -102,7 +102,7 @@ Window::Window(tag_overlapped_constructor) { using namespace std::placeholders; native_window_ = - platform::UiApplication::GetInstance()->CreateWindow(nullptr); + platform::native::UiApplication::GetInstance()->CreateWindow(nullptr); render_object_.reset(new render::WindowRenderObject(this)); event_revoker_guard_.Add(native_window_->DestroyEvent()->AddHandler( @@ -124,7 +124,7 @@ Window::Window(tag_overlapped_constructor) { event_revoker_guard_.Add(native_window_->KeyDownEvent()->AddHandler( std::bind(&Window::OnNativeKeyDown, this, _1))); event_revoker_guard_.Add(native_window_->KeyUpEvent()->AddHandler( - std::bind(&Window::OnNativeKeyUp, this, _1))); + std::bind(&Window::OnNativeKeyUp, this, _1))); } Window::~Window() { @@ -168,7 +168,7 @@ void Window::OnNativeDestroy() { delete this; } void Window::OnNativePaint() { const auto painter = - std::unique_ptr(native_window_->BeginPaint()); + std::unique_ptr(native_window_->BeginPaint()); render_object_->Draw(painter.get()); painter->EndDraw(); } @@ -202,13 +202,13 @@ void Window::OnNativeMouseMove(const Point& point) { point); } -void Window::OnNativeMouseDown(platform::MouseButton button, +void Window::OnNativeMouseDown(platform::native::MouseButton button, const Point& point) { Control* control = HitTest(point); DispatchEvent(control, &Control::MouseDownEvent, nullptr, point, button); } -void Window::OnNativeMouseUp(platform::MouseButton button, const Point& point) { +void Window::OnNativeMouseUp(platform::native::MouseButton button, const Point& point) { Control* control = HitTest(point); DispatchEvent(control, &Control::MouseUpEvent, nullptr, point, button); } diff --git a/src/win/CMakeLists.txt b/src/win/CMakeLists.txt new file mode 100644 index 00000000..1ca1f544 --- /dev/null +++ b/src/win/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(cru_win_base STATIC + debug.cpp + exception.cpp + string_util.cpp +) +target_compile_definitions(cru_win_base PUBLIC UNICODE _UNICODE) # use unicode +target_include_directories(cru_win_base PUBLIC ${PROJECT_SOURCE_DIR}/include .) + +add_subdirectory(graph) +add_subdirectory(native) diff --git a/src/win/debug.cpp b/src/win/debug.cpp new file mode 100644 index 00000000..e15b1f6c --- /dev/null +++ b/src/win/debug.cpp @@ -0,0 +1,9 @@ +#include "cru/win/win_pre_config.hpp" + +#include "cru/platform/debug.hpp" + +namespace cru::platform { +void DebugMessage(const std::wstring_view& message) { + ::OutputDebugStringW(message.data()); +} +} // namespace cru::platform diff --git a/src/win/exception.cpp b/src/win/exception.cpp new file mode 100644 index 00000000..62305329 --- /dev/null +++ b/src/win/exception.cpp @@ -0,0 +1,50 @@ +#include "cru/win/exception.hpp" + +#include "cru/common/format.hpp" + +namespace cru::win { +using util::Format; + +inline std::string HResultMakeMessage(HRESULT h_result, + const std::string_view* message) { + char buffer[10]; + sprintf_s(buffer, "%#08x", h_result); + + if (message) + return Format( + "An HResultError is thrown. HRESULT: {}.\nAdditional message: {}\n", + buffer, *message); + else + return Format("An HResultError is thrown. HRESULT: {}.\n", buffer); +} + +HResultError::HResultError(HRESULT h_result) + : PlatformException(HResultMakeMessage(h_result, nullptr)), + h_result_(h_result) {} + +HResultError::HResultError(HRESULT h_result, + const std::string_view& additional_message) + : PlatformException(HResultMakeMessage(h_result, &additional_message)), + h_result_(h_result) {} + +inline std::string Win32MakeMessage(DWORD error_code, + const std::string_view* message) { + char buffer[10]; + sprintf_s(buffer, "%#04x", error_code); + + if (message) + return Format("Last error code: {}.\nAdditional message: {}\n", buffer, + *message); + else + return Format("Last error code: {}.\n", buffer); +} + +Win32Error::Win32Error(DWORD error_code) + : PlatformException(Win32MakeMessage(error_code, nullptr)), + error_code_(error_code) {} + +Win32Error::Win32Error(DWORD error_code, + const std::string_view& additional_message) + : PlatformException(Win32MakeMessage(error_code, &additional_message)), + error_code_(error_code) {} +} // namespace cru::win diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt new file mode 100644 index 00000000..7781e09a --- /dev/null +++ b/src/win/graph/CMakeLists.txt @@ -0,0 +1,9 @@ +add_library(cru_win_graph STATIC + graph_manager.cpp + win_brush.cpp + win_font.cpp + win_geometry.cpp + win_graph_factory.cpp + win_painter.cpp + win_text_layout.cpp) +target_link_libraries(cru_win_graph PUBLIC D3D11 D2d1 DWrite cru_win_base) diff --git a/src/win/graph/graph_manager.cpp b/src/win/graph/graph_manager.cpp new file mode 100644 index 00000000..a4306b1b --- /dev/null +++ b/src/win/graph/graph_manager.cpp @@ -0,0 +1,59 @@ +#include "cru/win/graph/graph_manager.hpp" + +#include "cru/win/exception.hpp" + +namespace cru::win::graph { +GraphManager* GraphManager::GetInstance() { + static GraphManager instance; + return &instance; +} + +GraphManager::GraphManager() { + UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; + +#ifdef CRU_DEBUG + creation_flags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + const D3D_FEATURE_LEVEL feature_levels[] = { + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1}; + + Microsoft::WRL::ComPtr d3d11_device_context; + + ThrowIfFailed(D3D11CreateDevice( + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, + feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, + &d3d11_device_, nullptr, &d3d11_device_context)); + + Microsoft::WRL::ComPtr dxgi_device; + ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); + + ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, + IID_PPV_ARGS(&d2d1_factory_))); + + Microsoft::WRL::ComPtr d2d1_device; + + ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); + + ThrowIfFailed(d2d1_device->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); + + // Identify the physical adapter (GPU or card) this device is runs on. + Microsoft::WRL::ComPtr dxgi_adapter; + ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); + + // Get the factory object that created the DXGI device. + ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); + + ThrowIfFailed(DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(dwrite_factory_.GetAddressOf()))); + + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( + &dwrite_system_font_collection_)); + + graph_factory_.reset(new WinGraphFactory(this)); +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_brush.cpp b/src/win/graph/win_brush.cpp new file mode 100644 index 00000000..262c3302 --- /dev/null +++ b/src/win/graph/win_brush.cpp @@ -0,0 +1,23 @@ +#include "cru/win/graph/win_brush.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" + +#include + +namespace cru::win::graph { +WinSolidColorBrush::WinSolidColorBrush(GraphManager* graph_manager, + const ui::Color& color) { + assert(graph_manager); + ThrowIfFailed(graph_manager->GetD2D1DeviceContext()->CreateSolidColorBrush( + util::Convert(color), &brush_)); +} + +ui::Color WinSolidColorBrush::GetColor() { + return util::Convert(brush_->GetColor()); +} +void WinSolidColorBrush::SetColor(const ui::Color& color) { + brush_->SetColor(util::Convert(color)); +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_font.cpp b/src/win/graph/win_font.cpp new file mode 100644 index 00000000..0eb5e6b2 --- /dev/null +++ b/src/win/graph/win_font.cpp @@ -0,0 +1,28 @@ +#include "cru/win/graph/win_font.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" + +#include +#include +#include + +namespace cru::win::graph { +WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager, + const std::wstring_view& font_family, + float font_size) { + assert(graph_manager); + std::array buffer; + if (!::GetUserDefaultLocaleName(buffer.data(), buffer.size())) + throw Win32Error(::GetLastError(), "Failed to get locale."); + + ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat( + font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size, + buffer.data(), &text_format_)); + + ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); + ThrowIfFailed( + text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_geometry.cpp b/src/win/graph/win_geometry.cpp new file mode 100644 index 00000000..22c4b8a7 --- /dev/null +++ b/src/win/graph/win_geometry.cpp @@ -0,0 +1,66 @@ +#include "cru/win/graph/win_geometry.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" + +#include + +namespace cru::win::graph { +WinGeometryBuilder::WinGeometryBuilder(GraphManager* graph_manager) { + assert(graph_manager); + ThrowIfFailed( + graph_manager->GetD2D1Factory()->CreatePathGeometry(&geometry_)); + ThrowIfFailed(geometry_->Open(&geometry_sink_)); +} + +WinGeometryBuilder::~WinGeometryBuilder() { + if (geometry_sink_) { + ThrowIfFailed(geometry_sink_->Close()); + } +} + +void WinGeometryBuilder::BeginFigure(const ui::Point& point) { + assert(IsValid()); + geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); +} + +void WinGeometryBuilder::LineTo(const ui::Point& point) { + assert(IsValid()); + geometry_sink_->AddLine(util::Convert(point)); +} + +void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) { + assert(IsValid()); + geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + util::Convert(control_point), util::Convert(end_point))); +} + +void WinGeometryBuilder::CloseFigure(bool close) { + assert(IsValid()); + geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED + : D2D1_FIGURE_END_OPEN); +} + +platform::graph::Geometry* WinGeometryBuilder::Build() { + assert(IsValid()); + ThrowIfFailed(geometry_sink_->Close()); + geometry_sink_ = nullptr; + const auto geometry = new WinGeometry(geometry_); + geometry_ = nullptr; + return geometry; +} + +WinGeometry::WinGeometry(Microsoft::WRL::ComPtr geometry) { + assert(geometry); + geometry_ = std::move(geometry); +} + +bool WinGeometry::FillContains(const ui::Point& point) { + BOOL result; + ThrowIfFailed(geometry_->FillContainsPoint( + util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); + return result != 0; +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_graph_factory.cpp b/src/win/graph/win_graph_factory.cpp new file mode 100644 index 00000000..5ab905b6 --- /dev/null +++ b/src/win/graph/win_graph_factory.cpp @@ -0,0 +1,46 @@ +#include "cru/win/graph/win_graph_factory.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_font.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include +#include + +namespace cru::platform::graph { +GraphFactory* GraphFactory::GetInstance() { + return win::graph::GraphManager::GetInstance()->GetGraphFactory(); +} +} + +namespace cru::win::graph { +WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) { + assert(graph_manager); + graph_manager_ = graph_manager; +} + +platform::graph::SolidColorBrush* WinGraphFactory::CreateSolidColorBrush( + const ui::Color& color) { + return new WinSolidColorBrush(graph_manager_, color); +} + +platform::graph::GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { + return new WinGeometryBuilder(graph_manager_); +} + +platform::graph::FontDescriptor* WinGraphFactory::CreateFontDescriptor( + const std::wstring_view& font_family, float font_size) { + return new WinFontDescriptor(graph_manager_, font_family, font_size); +} + +platform::graph::TextLayout* WinGraphFactory::CreateTextLayout( + std::shared_ptr font, std::wstring text) { + const auto f = std::dynamic_pointer_cast(font); + assert(f); + return new WinTextLayout(graph_manager_, std::move(f), std::move(text)); +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_painter.cpp b/src/win/graph/win_painter.cpp new file mode 100644 index 00000000..69ce1141 --- /dev/null +++ b/src/win/graph/win_painter.cpp @@ -0,0 +1,99 @@ +#include "cru/win/graph/win_painter.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/d2d_util.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include + +namespace cru::win::graph { +WinPainter::WinPainter(ID2D1RenderTarget* render_target) { + assert(render_target); + render_target_ = render_target; +} + +WinPainter::~WinPainter() { EndDraw(); } + +platform::Matrix WinPainter::GetTransform() { + assert(!IsDisposed()); + D2D1_MATRIX_3X2_F m; + render_target_->GetTransform(&m); + return util::Convert(m); +} + +void WinPainter::SetTransform(const platform::Matrix& matrix) { + assert(!IsDisposed()); + render_target_->SetTransform(util::Convert(matrix)); +} + +void WinPainter::Clear(const ui::Color& color) { + assert(!IsDisposed()); + render_target_->Clear(util::Convert(color)); +} + +void WinPainter::StrokeRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush, float width) { + assert(!IsDisposed()); + const auto b = dynamic_cast(brush); + assert(b); + render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), + width); +} + +void WinPainter::FillRectangle(const ui::Rect& rectangle, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto b = dynamic_cast(brush); + assert(b); + render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); +} + +void WinPainter::StrokeGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush, float width) { + assert(!IsDisposed()); + const auto g = dynamic_cast(geometry); + assert(g); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); +} + +void WinPainter::FillGeometry(platform::graph::Geometry* geometry, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto g = dynamic_cast(geometry); + assert(g); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); +} + +void WinPainter::DrawText(const ui::Point& offset, + platform::graph::TextLayout* text_layout, + platform::graph::Brush* brush) { + assert(!IsDisposed()); + const auto t = dynamic_cast(text_layout); + assert(t); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->DrawTextLayout(util::Convert(offset), + t->GetDWriteTextLayout(), b->GetD2DBrush()); +} + +void WinPainter::EndDraw() { + if (!IsDisposed()) { + DoEndDraw(); + } +} + +void WinPainter::DoEndDraw() { + ThrowIfFailed(render_target_->EndDraw()); + is_disposed_ = true; +} +} // namespace cru::win::graph diff --git a/src/win/graph/win_text_layout.cpp b/src/win/graph/win_text_layout.cpp new file mode 100644 index 00000000..0506320a --- /dev/null +++ b/src/win/graph/win_text_layout.cpp @@ -0,0 +1,94 @@ +#include "cru/win/graph/win_text_layout.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/graph/win_font.hpp" + +#include +#include + +namespace cru::win::graph { +WinTextLayout::WinTextLayout(GraphManager* graph_manager, + std::shared_ptr font, + std::wstring text) { + assert(graph_manager); + assert(font); + graph_manager_ = graph_manager; + text_.swap(text); + font_descriptor_.swap(font); + + ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); +} + +std::wstring WinTextLayout::GetText() { return text_; } + +void WinTextLayout::SetText(std::wstring new_text) { + text_.swap(new_text); + ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); +} + +std::shared_ptr WinTextLayout::GetFont() { + return font_descriptor_; +} + +void WinTextLayout::SetFont( + std::shared_ptr font) { + auto f = std::dynamic_pointer_cast(font); + assert(f); + f.swap(font_descriptor_); + ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); +} + +void WinTextLayout::SetMaxWidth(float max_width) { + max_width_ = max_width; + ThrowIfFailed(text_layout_->SetMaxWidth(max_width_)); +} + +void WinTextLayout::SetMaxHeight(float max_height) { + max_height_ = max_height; + ThrowIfFailed(text_layout_->SetMaxHeight(max_height_)); +} + +ui::Rect WinTextLayout::GetTextBounds() { + DWRITE_TEXT_METRICS metrics; + ThrowIfFailed(text_layout_->GetMetrics(&metrics)); + return ui::Rect{metrics.left, metrics.top, metrics.width, metrics.height}; +} + +std::vector WinTextLayout::TextRangeRect( + const ui::TextRange& text_range) { + DWRITE_TEXT_METRICS text_metrics; + ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); + const auto metrics_count = + text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; + + std::vector hit_test_metrics(metrics_count); + UINT32 actual_count; + text_layout_->HitTestTextRange(text_range.position, text_range.count, 0, 0, + hit_test_metrics.data(), metrics_count, + &actual_count); + + hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, + hit_test_metrics.cend()); + + std::vector result; + result.reserve(actual_count); + + for (const auto& metrics : hit_test_metrics) { + result.push_back(ui::Rect{metrics.left, metrics.top, + metrics.left + metrics.width, + metrics.top + metrics.height}); + } + + return result; +} +} // namespace cru::win::graph diff --git a/src/win/native/CMakeLists.txt b/src/win/native/CMakeLists.txt new file mode 100644 index 00000000..421f7515 --- /dev/null +++ b/src/win/native/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(cru_win_native STATIC + god_window.cpp + timer.cpp + win_application.cpp + win_native_window.cpp + window_class.cpp + window_manager.cpp + window_painter.cpp + window_render_target.cpp) +target_link_libraries(cru_win_native PUBLIC cru_win_graph) diff --git a/src/win/native/dpi_util.hpp b/src/win/native/dpi_util.hpp new file mode 100644 index 00000000..d459e8c6 --- /dev/null +++ b/src/win/native/dpi_util.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "cru/platform/native/basic_types.hpp" + +// The dpi awareness needs to be implemented in the future. Currently we use 96 +// as default. + +namespace cru::win::native { +inline platform::native::Dpi GetDpi() { + return platform::native::Dpi{96.0f, 96.0f}; +} + +inline int DipToPixelInternal(const float dip, const float dpi) { + return static_cast(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(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::win::native diff --git a/src/win/native/god_window.cpp b/src/win/native/god_window.cpp new file mode 100644 index 00000000..58fb663c --- /dev/null +++ b/src/win/native/god_window.cpp @@ -0,0 +1,73 @@ +#include "cru/win/native/god_window.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/native/win_application.hpp" +#include "cru/win/native/window_class.hpp" +#include "god_window_message.hpp" +#include "timer.hpp" + +namespace cru::win::native { +constexpr auto god_window_class_name = L"GodWindowClass"; + +LRESULT CALLBACK GodWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, + LPARAM lParam) { + const auto app = WinApplication::GetInstance(); + + if (app) { + 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 + return DefWindowProcW(hWnd, uMsg, wParam, lParam); +} + +GodWindow::GodWindow(WinApplication* application) { + application_ = application; + + const auto h_instance = application->GetInstanceHandle(); + + god_window_class_ = std::make_shared(god_window_class_name, + GodWndProc, h_instance); + + hwnd_ = CreateWindowEx(0, god_window_class_name, L"", 0, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + HWND_MESSAGE, nullptr, h_instance, nullptr); + + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create god window."); +} + +GodWindow::~GodWindow() { ::DestroyWindow(hwnd_); } + +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*>(w_param); + (*p_action)(); + delete p_action; + *result = 0; + return true; + } + case WM_TIMER: { + const auto id = static_cast(w_param); + const auto action = application_->GetTimerManager()->GetAction(id); + if (action.has_value()) { + (action.value().second)(); + if (!action.value().first) + application_->GetTimerManager()->KillTimer(id); + result = 0; + return true; + } + break; + } + default: + return false; + } + return false; +} +} // namespace cru::win::native diff --git a/src/win/native/god_window_message.hpp b/src/win/native/god_window_message.hpp new file mode 100644 index 00000000..10268625 --- /dev/null +++ b/src/win/native/god_window_message.hpp @@ -0,0 +1,6 @@ +#pragma once +#include "cru/win/win_pre_config.hpp" + +namespace cru::win::native { +constexpr int invoke_later_message_id = WM_USER + 2000; +} diff --git a/src/win/native/timer.cpp b/src/win/native/timer.cpp new file mode 100644 index 00000000..9e3bbde6 --- /dev/null +++ b/src/win/native/timer.cpp @@ -0,0 +1,28 @@ +#include "timer.hpp" + +namespace cru::win::native { +TimerManager::TimerManager(GodWindow* god_window) { god_window_ = god_window; } + +UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, + const TimerAction& action) { + const auto id = current_count_++; + ::SetTimer(god_window_->GetHandle(), id, milliseconds, nullptr); + map_.emplace(id, std::make_pair(loop, action)); + return id; +} + +void TimerManager::KillTimer(const UINT_PTR id) { + const auto find_result = map_.find(id); + if (find_result != map_.cend()) { + ::KillTimer(god_window_->GetHandle(), id); + map_.erase(find_result); + } +} + +std::optional> TimerManager::GetAction( + const UINT_PTR id) { + const auto find_result = map_.find(id); + if (find_result == map_.cend()) return std::nullopt; + return find_result->second; +} +} // namespace cru::win::native diff --git a/src/win/native/timer.hpp b/src/win/native/timer.hpp new file mode 100644 index 00000000..f30d9b91 --- /dev/null +++ b/src/win/native/timer.hpp @@ -0,0 +1,34 @@ +#pragma once +#include "cru/win/win_pre_config.hpp" + +#include "cru/common/base.hpp" +#include "cru/win/native/god_window.hpp" + +#include +#include +#include +#include + +namespace cru::win::native { +using TimerAction = std::function; + +class TimerManager : public Object { + public: + TimerManager(GodWindow* god_window); + TimerManager(const TimerManager& other) = delete; + TimerManager(TimerManager&& other) = delete; + TimerManager& operator=(const TimerManager& other) = delete; + TimerManager& operator=(TimerManager&& other) = delete; + ~TimerManager() override = default; + + UINT_PTR CreateTimer(UINT milliseconds, bool loop, const TimerAction& action); + void KillTimer(UINT_PTR id); + std::optional> GetAction(UINT_PTR id); + + private: + GodWindow* god_window_; + + std::map> map_{}; + UINT_PTR current_count_ = 0; +}; +} // namespace cru::win::native diff --git a/src/win/native/win_application.cpp b/src/win/native/win_application.cpp new file mode 100644 index 00000000..72e12fa0 --- /dev/null +++ b/src/win/native/win_application.cpp @@ -0,0 +1,100 @@ +#include "cru/win/native/win_application.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/native/god_window.hpp" +#include "cru/win/native/win_native_window.hpp" +#include "god_window_message.hpp" +#include "timer.hpp" +#include "window_manager.hpp" + +#include +#include + +namespace cru::platform::native { +UiApplication* UiApplication::GetInstance() { + return win::native::WinApplication::GetInstance(); +} +} // namespace cru::platform::native + +namespace cru::win::native { +WinApplication* WinApplication::instance_ = nullptr; + +WinApplication* WinApplication::GetInstance() { + if (instance_ == nullptr) + instance_ = new WinApplication(::GetModuleHandleW(nullptr)); + return instance_; +} + +WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { + if (instance_) + throw std::runtime_error("A application instance already exists."); + + instance_ = this; + + if (!::IsWindows8OrGreater()) + throw std::runtime_error("Must run on Windows 8 or later."); + + god_window_ = std::make_shared(this); + timer_manager_ = std::make_shared(god_window_.get()); + window_manager_ = std::make_shared(this); +} + +WinApplication::~WinApplication() { instance_ = nullptr; } + +int WinApplication::Run() { + MSG msg; + while (GetMessageW(&msg, nullptr, 0, 0)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + return static_cast(msg.wParam); +} + +void WinApplication::Quit(const int quit_code) { ::PostQuitMessage(quit_code); } + +void WinApplication::InvokeLater(const std::function& action) { + // copy the action to a safe place + auto p_action_copy = new std::function(action); + + if (PostMessageW(GetGodWindow()->GetHandle(), invoke_later_message_id, + reinterpret_cast(p_action_copy), 0) == 0) + throw Win32Error(::GetLastError(), "InvokeLater failed to post message."); +} + +unsigned long WinApplication::SetTimeout(std::chrono::milliseconds milliseconds, + const std::function& action) { + return static_cast(timer_manager_->CreateTimer( + static_cast(milliseconds.count()), false, action)); +} + +unsigned long WinApplication::SetInterval( + std::chrono::milliseconds milliseconds, + const std::function& action) { + return static_cast(timer_manager_->CreateTimer( + static_cast(milliseconds.count()), true, action)); +} + +void WinApplication::CancelTimer(unsigned long id) { + timer_manager_->KillTimer(static_cast(id)); +} + +std::vector WinApplication::GetAllWindow() { + const auto&& windows = window_manager_->GetAllWindows(); + std::vector result; + for (const auto w : windows) { + result.push_back(static_cast(w)); + } + return result; +} + +platform::native::NativeWindow* WinApplication::CreateWindow( + platform::native::NativeWindow* parent) { + WinNativeWindow* p = nullptr; + if (parent != nullptr) { + p = dynamic_cast(parent); + assert(p); + } + return new WinNativeWindow(this, window_manager_->GetGeneralWindowClass(), + WS_OVERLAPPEDWINDOW, p); +} +} // namespace cru::win::native diff --git a/src/win/native/win_native_window.cpp b/src/win/native/win_native_window.cpp new file mode 100644 index 00000000..0afad4e6 --- /dev/null +++ b/src/win/native/win_native_window.cpp @@ -0,0 +1,340 @@ +#include "cru/win/native/win_native_window.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/native/win_application.hpp" +#include "cru/win/native/window_class.hpp" +#include "cru/win/native/window_render_target.hpp" +#include "dpi_util.hpp" +#include "window_manager.hpp" +#include "window_painter.hpp" + +#include +#include + +namespace cru::win::native { +WinNativeWindow::WinNativeWindow(WinApplication* application, + std::shared_ptr window_class, + DWORD window_style, WinNativeWindow* parent) { + assert(application); // application can't be null. + assert(parent == nullptr || + parent->IsValid()); // Parent window is not valid. + + application_ = application; + parent_window_ = parent; + + const auto window_manager = application->GetWindowManager(); + + hwnd_ = CreateWindowExW( + 0, window_manager->GetGeneralWindowClass()->GetName(), L"", window_style, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + parent == nullptr ? nullptr : parent->GetWindowHandle(), nullptr, + application->GetInstanceHandle(), nullptr); + + if (hwnd_ == nullptr) + throw Win32Error(::GetLastError(), "Failed to create window."); + + window_manager->RegisterWindow(hwnd_, this); + + window_render_target_.reset( + new WindowRenderTarget(graph::GraphManager::GetInstance(), hwnd_)); +} + +WinNativeWindow::~WinNativeWindow() { + if (IsValid()) { + SetDeleteThisOnDestroy(false); // avoid double delete. + Close(); + } +} + +bool WinNativeWindow::IsValid() { return hwnd_ != nullptr; } + +void WinNativeWindow::SetDeleteThisOnDestroy(bool value) { + delete_this_on_destroy_ = value; +} + +void WinNativeWindow::Close() { + if (IsValid()) DestroyWindow(hwnd_); +} + +bool WinNativeWindow::IsVisible() { + if (IsValid()) return ::IsWindowVisible(hwnd_); + return false; +} + +void WinNativeWindow::SetVisible(bool is_visible) { + if (!IsValid()) return; + is_visible ? ShowWindow(hwnd_, SW_SHOWNORMAL) : ShowWindow(hwnd_, SW_HIDE); +} +ui::Size WinNativeWindow::GetClientSize() { + if (!IsValid()) return ui::Size{}; + + const auto pixel_rect = GetClientRectPixel(); + return ui::Size(PixelToDipX(pixel_rect.right), + PixelToDipY(pixel_rect.bottom)); +} + +void WinNativeWindow::SetClientSize(const ui::Size& size) { + if (IsValid()) { + const auto window_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_STYLE)); + const auto window_ex_style = + static_cast(GetWindowLongPtr(hwnd_, GWL_EXSTYLE)); + + RECT rect; + rect.left = 0; + rect.top = 0; + rect.right = DipToPixelX(size.width); + rect.bottom = DipToPixelY(size.height); + if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style)) + throw Win32Error(::GetLastError(), + "Failed to invoke AdjustWindowRectEx."); + + if (!SetWindowPos(hwnd_, nullptr, 0, 0, rect.right - rect.left, + rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE)) + throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); + } +} + +ui::Rect WinNativeWindow::GetWindowRect() { + if (!IsValid()) return ui::Rect{}; + + RECT rect; + if (!::GetWindowRect(hwnd_, &rect)) + throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect."); + + return ui::Rect::FromVertices(PixelToDipX(rect.left), PixelToDipY(rect.top), + PixelToDipX(rect.right), + PixelToDipY(rect.bottom)); +} + +void WinNativeWindow::SetWindowRect(const ui::Rect& rect) { + if (IsValid()) { + if (!SetWindowPos(hwnd_, nullptr, DipToPixelX(rect.left), + DipToPixelY(rect.top), DipToPixelX(rect.GetRight()), + DipToPixelY(rect.GetBottom()), SWP_NOZORDER)) + throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos."); + } +} + +platform::graph::Painter* WinNativeWindow::BeginPaint() { + return new WindowPainter(this); +} + +bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, + WPARAM w_param, LPARAM l_param, + LRESULT* result) { + WindowNativeMessageEventArgs args{ + WindowNativeMessage{hwnd, msg, w_param, l_param}}; + native_message_event_.Raise(args); + if (args.IsHandled()) { + *result = args.GetResult(); + return true; + } + + switch (msg) { + case WM_PAINT: + OnPaintInternal(); + *result = 0; + return true; + case WM_ERASEBKGND: + *result = 1; + return true; + case WM_SETFOCUS: + OnSetFocusInternal(); + *result = 0; + return true; + case WM_KILLFOCUS: + OnKillFocusInternal(); + *result = 0; + return true; + case WM_MOUSEMOVE: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseMoveInternal(point); + *result = 0; + return true; + } + case WM_LBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(platform::native::MouseButton::Left, point); + *result = 0; + return true; + } + case WM_LBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(platform::native::MouseButton::Left, point); + *result = 0; + return true; + } + case WM_RBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(platform::native::MouseButton::Right, point); + *result = 0; + return true; + } + case WM_RBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(platform::native::MouseButton::Right, point); + *result = 0; + return true; + } + case WM_MBUTTONDOWN: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseDownInternal(platform::native::MouseButton::Middle, point); + *result = 0; + return true; + } + case WM_MBUTTONUP: { + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + OnMouseUpInternal(platform::native::MouseButton::Middle, point); + *result = 0; + return true; + } + case WM_MOUSEWHEEL: + POINT point; + point.x = GET_X_LPARAM(l_param); + point.y = GET_Y_LPARAM(l_param); + ScreenToClient(hwnd, &point); + OnMouseWheelInternal(GET_WHEEL_DELTA_WPARAM(w_param), point); + *result = 0; + return true; + case WM_KEYDOWN: + OnKeyDownInternal(static_cast(w_param)); + *result = 0; + return true; + case WM_KEYUP: + OnKeyUpInternal(static_cast(w_param)); + *result = 0; + return true; + case WM_CHAR: + OnCharInternal(static_cast(w_param)); + *result = 0; + return true; + case WM_SIZE: + OnResizeInternal(LOWORD(l_param), HIWORD(l_param)); + *result = 0; + return true; + case WM_ACTIVATE: + if (w_param == WA_ACTIVE || w_param == WA_CLICKACTIVE) + OnActivatedInternal(); + else if (w_param == WA_INACTIVE) + OnDeactivatedInternal(); + *result = 0; + return true; + case WM_DESTROY: + OnDestroyInternal(); + *result = 0; + return true; + default: + return false; + } +} + +RECT WinNativeWindow::GetClientRectPixel() { + RECT rect; + if (!GetClientRect(hwnd_, &rect)) + throw Win32Error(::GetLastError(), "Failed to invoke GetClientRect."); + return rect; +} + +void WinNativeWindow::OnDestroyInternal() { + application_->GetWindowManager()->UnregisterWindow(hwnd_); + hwnd_ = nullptr; + 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) { + if (!(new_width == 0 && new_height == 0)) { + window_render_target_->ResizeBuffer(new_width, new_height); + resize_event_.Raise( + ui::Size{PixelToDipX(new_width), PixelToDipY(new_height)}); + } +} + +void WinNativeWindow::OnSetFocusInternal() { + has_focus_ = true; + focus_event_.Raise(true); +} + +void WinNativeWindow::OnKillFocusInternal() { + has_focus_ = false; + focus_event_.Raise(false); +} + +inline ui::Point PiToDip(const POINT& pi_point) { + return ui::Point(PixelToDipX(pi_point.x), PixelToDipY(pi_point.y)); +} + +void WinNativeWindow::OnMouseMoveInternal(const POINT point) { + // when mouse was previous outside the window + if (!is_mouse_in_) { + // invoke TrackMouseEvent to have WM_MOUSELEAVE sent. + TRACKMOUSEEVENT tme; + tme.cbSize = sizeof tme; + tme.dwFlags = TME_LEAVE; + tme.hwndTrack = hwnd_; + + TrackMouseEvent(&tme); + + is_mouse_in_ = true; + mouse_enter_leave_event_.Raise(true); + } + + 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(platform::native::MouseButton button, + POINT point) { + const auto dip_point = PiToDip(point); + mouse_down_event_.Raise(button, dip_point); +} + +void WinNativeWindow::OnMouseUpInternal(platform::native::MouseButton button, + POINT point) { + const auto dip_point = PiToDip(point); + mouse_up_event_.Raise(button, dip_point); +} + +void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) {} + +void WinNativeWindow::OnKeyDownInternal(int virtual_code) { + key_down_event_.Raise(virtual_code); +} + +void WinNativeWindow::OnKeyUpInternal(int virtual_code) { + key_up_event_.Raise(virtual_code); +} + +void WinNativeWindow::OnCharInternal(wchar_t c) {} + +void WinNativeWindow::OnActivatedInternal() {} + +void WinNativeWindow::OnDeactivatedInternal() {} +} // namespace cru::win::native diff --git a/src/win/native/window_class.cpp b/src/win/native/window_class.cpp new file mode 100644 index 00000000..6c7d0d3d --- /dev/null +++ b/src/win/native/window_class.cpp @@ -0,0 +1,28 @@ +#include "cru/win/native/window_class.hpp" + +#include "cru/win/exception.hpp" + +namespace cru::win::native { +WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, + HINSTANCE h_instance) + : name_(name) { + WNDCLASSEXW window_class; + window_class.cbSize = sizeof(WNDCLASSEXW); + + window_class.style = CS_HREDRAW | CS_VREDRAW; + window_class.lpfnWndProc = window_proc; + window_class.cbClsExtra = 0; + window_class.cbWndExtra = 0; + window_class.hInstance = h_instance; + window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); + window_class.hCursor = LoadCursor(NULL, IDC_ARROW); + window_class.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); + window_class.lpszMenuName = NULL; + window_class.lpszClassName = name.c_str(); + window_class.hIconSm = NULL; + + atom_ = ::RegisterClassExW(&window_class); + if (atom_ == 0) + throw Win32Error(::GetLastError(), "Failed to create window class."); +} +} // namespace cru::win::native diff --git a/src/win/native/window_manager.cpp b/src/win/native/window_manager.cpp new file mode 100644 index 00000000..a2fcdb54 --- /dev/null +++ b/src/win/native/window_manager.cpp @@ -0,0 +1,54 @@ +#include "window_manager.hpp" + +#include "cru/win/native/win_application.hpp" +#include "cru/win/native/win_native_window.hpp" +#include "cru/win/native/window_class.hpp" + +#include + +namespace cru::win::native { +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( + 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 WindowManager::GetAllWindows() const { + std::vector windows; + for (auto [key, value] : window_map_) windows.push_back(value); + return windows; +} +} // namespace cru::win::native diff --git a/src/win/native/window_manager.hpp b/src/win/native/window_manager.hpp new file mode 100644 index 00000000..fa5bbe9d --- /dev/null +++ b/src/win/native/window_manager.hpp @@ -0,0 +1,52 @@ +#pragma once +#include "cru/win/win_pre_config.hpp" + +#include "cru/common/base.hpp" + +#include +#include +#include + +namespace cru::win::native { +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 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 GetAllWindows() const; + + private: + WinApplication* application_; + + std::shared_ptr general_window_class_; + std::map window_map_; +}; + +} // namespace cru::win::native diff --git a/src/win/native/window_painter.cpp b/src/win/native/window_painter.cpp new file mode 100644 index 00000000..46364cdd --- /dev/null +++ b/src/win/native/window_painter.cpp @@ -0,0 +1,24 @@ +#include "window_painter.hpp" + +#include "cru/win/graph/graph_manager.hpp" +#include "cru/win/native/window_render_target.hpp" + +#include + +namespace cru::win::native { +WindowPainter::WindowPainter(WinNativeWindow* window) + : WinPainter(window->GetWindowRenderTarget() + ->GetGraphManager() + ->GetD2D1DeviceContext()), + window_(window) { + window->GetWindowRenderTarget()->SetAsTarget(); + window->GetWindowRenderTarget() + ->GetGraphManager() + ->GetD2D1DeviceContext() + ->BeginDraw(); +} + +void WindowPainter::DoEndDraw() { + WinPainter::DoEndDraw(); + window_->GetWindowRenderTarget()->Present(); } +} // namespace cru::win::native diff --git a/src/win/native/window_painter.hpp b/src/win/native/window_painter.hpp new file mode 100644 index 00000000..0e6ab2cb --- /dev/null +++ b/src/win/native/window_painter.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "cru/win/graph/win_painter.hpp" +#include "cru/win/native/win_native_window.hpp" + +namespace cru::win::native { +class WindowPainter : public graph::WinPainter { + public: + explicit WindowPainter(WinNativeWindow* window); + WindowPainter(const WindowPainter& other) = delete; + WindowPainter& operator=(const WindowPainter& other) = delete; + WindowPainter(WindowPainter&& other) = delete; + WindowPainter& operator=(WindowPainter&& other) = delete; + ~WindowPainter() override = default; + + protected: + void DoEndDraw() override; + + private: + WinNativeWindow* window_; +}; +} // namespace cru::win::native diff --git a/src/win/native/window_render_target.cpp b/src/win/native/window_render_target.cpp new file mode 100644 index 00000000..c49a920e --- /dev/null +++ b/src/win/native/window_render_target.cpp @@ -0,0 +1,90 @@ +#include "cru/win/native/window_render_target.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/graph_manager.hpp" +#include "dpi_util.hpp" + +#include + +namespace cru::win::native { +WindowRenderTarget::WindowRenderTarget(graph::GraphManager* graph_manager, + HWND hwnd) { + this->graph_manager_ = graph_manager; + + const auto d3d11_device = graph_manager->GetD3D11Device(); + const auto dxgi_factory = graph_manager->GetDxgiFactory(); + + // Allocate a descriptor. + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; + swap_chain_desc.Width = 0; // use automatic sizing + swap_chain_desc.Height = 0; + swap_chain_desc.Format = + DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format + swap_chain_desc.Stereo = false; + swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling + swap_chain_desc.SampleDesc.Quality = 0; + swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + swap_chain_desc.BufferCount = 2; // use double buffering to enable flip + swap_chain_desc.Scaling = DXGI_SCALING_NONE; + swap_chain_desc.SwapEffect = + DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect + swap_chain_desc.Flags = 0; + + // Get the final swap chain for this window from the DXGI factory. + ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd( + d3d11_device, hwnd, &swap_chain_desc, nullptr, nullptr, + &dxgi_swap_chain_)); + + CreateTargetBitmap(); +} + +void WindowRenderTarget::ResizeBuffer(const int width, const int height) { + const auto graph_manager = graph_manager_; + const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); + + Microsoft::WRL::ComPtr old_target; + d2d1_device_context->GetTarget(&old_target); + const auto target_this = old_target == this->target_bitmap_; + if (target_this) d2d1_device_context->SetTarget(nullptr); + + old_target = nullptr; + target_bitmap_ = nullptr; + + ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, + DXGI_FORMAT_UNKNOWN, 0)); + + CreateTargetBitmap(); + + if (target_this) d2d1_device_context->SetTarget(target_bitmap_.Get()); +} + +void WindowRenderTarget::SetAsTarget() { + graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get()); +} + +void WindowRenderTarget::Present() { + ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); +} + +void WindowRenderTarget::CreateTargetBitmap() { + assert(target_bitmap_ == nullptr); // target bitmap must not exist. + + // Direct2D needs the dxgi version of the backbuffer surface pointer. + Microsoft::WRL::ComPtr dxgi_back_buffer; + ThrowIfFailed( + dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); + + const auto dpi = GetDpi(); + + auto bitmap_properties = D2D1::BitmapProperties1( + D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), + dpi.x, dpi.y); + + // Get a D2D surface from the DXGI back buffer to use as the D2D render + // target. + ThrowIfFailed( + graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( + dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); +} +} // namespace cru::win::native diff --git a/src/win/string_util.cpp b/src/win/string_util.cpp new file mode 100644 index 00000000..dd513b12 --- /dev/null +++ b/src/win/string_util.cpp @@ -0,0 +1,20 @@ +#include "cru/platform/string_util.hpp" + +#include "cru/win/exception.hpp" + +namespace cru::platform { +std::string ToUtf8String(const std::wstring_view& string) { + if (string.empty()) return std::string(); + + const auto length = ::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, + nullptr, 0, nullptr, nullptr); + std::string result; + result.resize(length); + if (::WideCharToMultiByte(CP_UTF8, 0, string.data(), -1, result.data(), + static_cast(result.size()), nullptr, + nullptr) == 0) + throw win::Win32Error(::GetLastError(), + "Failed to convert wide string to UTF-8."); + return result; +} +} // namespace cru::platform -- cgit v1.2.3