diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/cru/common/StringUtil.hpp | 39 | ||||
-rw-r--r-- | include/cru/platform/GraphBase.hpp | 1 | ||||
-rw-r--r-- | include/cru/ui/Base.hpp | 4 | ||||
-rw-r--r-- | include/cru/ui/UiEvent.hpp | 5 | ||||
-rw-r--r-- | include/cru/ui/render/ScrollRenderObject.hpp | 3 | ||||
-rw-r--r-- | include/cru/win/String.hpp | 46 |
6 files changed, 55 insertions, 43 deletions
diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp new file mode 100644 index 00000000..a44ae6b4 --- /dev/null +++ b/include/cru/common/StringUtil.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "Base.hpp" + +namespace cru { +using CodePoint = std::int32_t; +constexpr CodePoint k_code_point_end = -1; + +class TextEncodeException : public std::runtime_error { + public: + using runtime_error::runtime_error; +}; + +class Utf8Iterator : public Object { + public: + explicit Utf8Iterator(const std::string_view& string) : string_(string) {} + Utf8Iterator(const std::string_view& string, Index position) + : string_(string), position_(position) {} + + CRU_DEFAULT_COPY(Utf8Iterator) + CRU_DEFAULT_MOVE(Utf8Iterator) + + ~Utf8Iterator() = default; + + public: + void SetToHead() { position_ = 0; } + void SetPosition(Index position) { position_ = position; } + + // Advance current position and get next code point. Return k_code_point_end + // if there is no next code unit(point). Throw TextEncodeException if decoding + // fails. + CodePoint Next(); + + Index CurrentPosition() const { return this->position_; } + + private: + std::string_view string_; + Index position_ = 0; +}; +} // namespace cru diff --git a/include/cru/platform/GraphBase.hpp b/include/cru/platform/GraphBase.hpp index 0d4effd4..28dab1e6 100644 --- a/include/cru/platform/GraphBase.hpp +++ b/include/cru/platform/GraphBase.hpp @@ -249,6 +249,7 @@ struct TextRange final { constexpr TextRange(const gsl::index position, const gsl::index count = 0) : position(position), count(count) {} + gsl::index GetStart() const { return position; } gsl::index GetEnd() const { return position + count; } TextRange Normalize() const { diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index c59d1b60..9036a47b 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -185,6 +185,10 @@ class Control; class ClickDetector; class UiHost; +namespace render { +class RenderObject; +} + //-------------------- region: basic types -------------------- namespace internal { constexpr int align_start = 0; diff --git a/include/cru/ui/UiEvent.hpp b/include/cru/ui/UiEvent.hpp index 79d0f7e3..39f26aee 100644 --- a/include/cru/ui/UiEvent.hpp +++ b/include/cru/ui/UiEvent.hpp @@ -13,10 +13,6 @@ namespace cru::platform::graph { struct IPainter; } -namespace cru::ui { -class Control; -} - namespace cru::ui::event { class UiEventArgs : public Object { public: @@ -88,6 +84,7 @@ class MouseEventArgs : public UiEventArgs { // This point is relative to window client lefttop. Point GetPoint() const { return point_.value_or(Point{}); } + Point GetPointToContent(render::RenderObject* render_target) const; private: std::optional<Point> point_; diff --git a/include/cru/ui/render/ScrollRenderObject.hpp b/include/cru/ui/render/ScrollRenderObject.hpp index 0c1fb16b..20b7278c 100644 --- a/include/cru/ui/render/ScrollRenderObject.hpp +++ b/include/cru/ui/render/ScrollRenderObject.hpp @@ -28,6 +28,9 @@ class ScrollRenderObject : public RenderObject { void SetScrollOffset(const Point& offset); Point GetRawScrollOffset() const { return scroll_offset_; } + // Rect lefttop relative to content rect. + void ScrollToContain(const Rect& rect); + protected: void OnDrawCore(platform::graph::IPainter* painter) override; diff --git a/include/cru/win/String.hpp b/include/cru/win/String.hpp index 3d68cff7..ac07f57b 100644 --- a/include/cru/win/String.hpp +++ b/include/cru/win/String.hpp @@ -16,7 +16,7 @@ way.) #pragma once #include "WinPreConfig.hpp" -#include "cru/common/Base.hpp" +#include "cru/common/StringUtil.hpp" #include <cstdint> #include <stdexcept> @@ -37,38 +37,6 @@ inline bool IsSurrogatePairTrailing(wchar_t c) { return c >= 0xDC00 && c <= 0xDFFF; } -using CodePoint = std::int32_t; -constexpr CodePoint k_code_point_end = -1; - -class TextEncodeException : public std::runtime_error { - public: - using runtime_error::runtime_error; -}; - -class Utf8Iterator : public Object { - public: - Utf8Iterator(const std::string_view& string) : string_(string) {} - - CRU_DEFAULT_COPY(Utf8Iterator) - CRU_DEFAULT_MOVE(Utf8Iterator) - - ~Utf8Iterator() = default; - - public: - void SetToHead() { position_ = 0; } - - // Advance current position and get next code point. Return k_code_point_end - // if there is no next code unit(point). Throw TextEncodeException if decoding - // fails. - CodePoint Next(); - - int CurrentPosition() const { return this->position_; } - - private: - std::string_view string_; - int position_ = 0; -}; - class Utf16Iterator : public Object { static_assert( sizeof(wchar_t) == 2, @@ -91,17 +59,17 @@ class Utf16Iterator : public Object { // fails. CodePoint Next(); - int CurrentPosition() const { return this->position_; } + Index CurrentPosition() const { return this->position_; } private: std::wstring_view string_; - int position_ = 0; + Index position_ = 0; }; -int IndexUtf8ToUtf16(const std::string_view& utf8_string, int utf8_index, - const std::wstring_view& utf16_string); +Index IndexUtf8ToUtf16(const std::string_view& utf8_string, Index utf8_index, + const std::wstring_view& utf16_string); -int IndexUtf16ToUtf8(const std::wstring_view& utf16_string, int utf16_index, - const std::string_view& utf8_string); +Index IndexUtf16ToUtf8(const std::wstring_view& utf16_string, Index utf16_index, + const std::string_view& utf8_string); } // namespace cru::platform::win |