aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-03 23:36:45 +0800
committercrupest <crupest@outlook.com>2020-03-03 23:36:45 +0800
commit0dcf8e686b93cca54a424affe0455d0a97d6c2ef (patch)
tree744897a3b6a29f6142f1943dab5d9957e670919b /include
parent47053829c322c43032244937cb63f9da178b852d (diff)
downloadcru-0dcf8e686b93cca54a424affe0455d0a97d6c2ef.tar.gz
cru-0dcf8e686b93cca54a424affe0455d0a97d6c2ef.tar.bz2
cru-0dcf8e686b93cca54a424affe0455d0a97d6c2ef.zip
...
Diffstat (limited to 'include')
-rw-r--r--include/cru/platform/graph/font.hpp4
-rw-r--r--include/cru/ui/controls/text_block.hpp23
-rw-r--r--include/cru/ui/controls/text_box.hpp2
-rw-r--r--include/cru/ui/controls/text_common.hpp66
-rw-r--r--include/cru/ui/render/layout_utility.hpp7
-rw-r--r--include/cru/ui/render/render_object.hpp9
-rw-r--r--include/cru/ui/render/text_render_object.hpp7
-rw-r--r--include/cru/ui/ui_event.hpp3
-rw-r--r--include/cru/ui/ui_manager.hpp1
-rw-r--r--include/cru/win/graph/direct/font.hpp2
10 files changed, 110 insertions, 14 deletions
diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp
index 98ce80e7..d0aa2d28 100644
--- a/include/cru/platform/graph/font.hpp
+++ b/include/cru/platform/graph/font.hpp
@@ -2,5 +2,7 @@
#include "resource.hpp"
namespace cru::platform::graph {
-struct IFont : virtual IGraphResource {};
+struct IFont : virtual IGraphResource {
+ virtual float GetFontSize() = 0;
+};
} // namespace cru::platform::graph
diff --git a/include/cru/ui/controls/text_block.hpp b/include/cru/ui/controls/text_block.hpp
index 708b62f1..02e0eb2b 100644
--- a/include/cru/ui/controls/text_block.hpp
+++ b/include/cru/ui/controls/text_block.hpp
@@ -1,14 +1,18 @@
#pragma once
#include "../no_child_control.hpp"
+#include "text_common.hpp"
+
#include <memory>
namespace cru::ui::render {
+class StackLayoutRenderObject;
class TextRenderObject;
-}
+class CanvasRenderObject;
+} // namespace cru::ui::render
namespace cru::ui::controls {
-class TextBlock : public NoChildControl {
+class TextBlock : public NoChildControl, public virtual ITextControl {
public:
static constexpr std::string_view control_type = "TextBlock";
@@ -24,16 +28,23 @@ class TextBlock : public NoChildControl {
TextBlock& operator=(TextBlock&& other) = delete;
~TextBlock() override;
- std::string_view GetControlType() const final {
- return control_type;
- }
+ std::string_view GetControlType() const final { return control_type; }
render::RenderObject* GetRenderObject() const override;
std::string GetText() const;
void SetText(std::string text);
+ render::TextRenderObject* GetTextRenderObject() override;
+ render::CanvasRenderObject* GetCaretRenderObject() override;
+ platform::graph::IBrush* GetCaretBrush() override;
+
private:
- std::unique_ptr<render::TextRenderObject> render_object_;
+ std::unique_ptr<render::StackLayoutRenderObject> root_render_object_;
+ std::unique_ptr<render::TextRenderObject> text_render_object_;
+ std::unique_ptr<render::CanvasRenderObject> caret_render_object_;
+ std::shared_ptr<platform::graph::IBrush> caret_brush_;
+
+ std::unique_ptr<TextControlService> service_;
};
} // namespace cru::ui::controls
diff --git a/include/cru/ui/controls/text_box.hpp b/include/cru/ui/controls/text_box.hpp
index 76a6299e..888a9527 100644
--- a/include/cru/ui/controls/text_box.hpp
+++ b/include/cru/ui/controls/text_box.hpp
@@ -5,6 +5,7 @@
namespace cru::ui::render {
class BorderRenderObject;
+class StackLayoutRenderObject;
class TextRenderObject;
class CanvasRenderObject;
} // namespace cru::ui::render
@@ -27,6 +28,7 @@ class TextBox : public NoChildControl {
private:
std::unique_ptr<render::BorderRenderObject> border_render_object_;
+ std::unique_ptr<render::StackLayoutRenderObject> stack_layout_render_object_;
std::unique_ptr<render::TextRenderObject> text_render_object_;
std::unique_ptr<render::CanvasRenderObject> caret_render_object_;
};
diff --git a/include/cru/ui/controls/text_common.hpp b/include/cru/ui/controls/text_common.hpp
new file mode 100644
index 00000000..7398764f
--- /dev/null
+++ b/include/cru/ui/controls/text_common.hpp
@@ -0,0 +1,66 @@
+#pragma once
+#include "../base.hpp"
+
+#include "cru/ui/ui_event.hpp"
+
+#include <functional>
+#include <optional>
+
+namespace cru::platform::graph {
+struct IBrush;
+}
+
+namespace cru::ui::render {
+class TextRenderObject;
+class CanvasRenderObject;
+} // namespace cru::ui::render
+
+namespace cru::ui::controls {
+
+struct ITextControl : virtual Interface {
+ virtual render::TextRenderObject* GetTextRenderObject() = 0;
+ virtual render::CanvasRenderObject* GetCaretRenderObject() = 0;
+ virtual platform::graph::IBrush* GetCaretBrush() = 0;
+};
+
+class TextControlService : public Object {
+ public:
+ TextControlService(Control* control, ITextControl* text_control);
+
+ CRU_DELETE_COPY(TextControlService)
+ CRU_DELETE_MOVE(TextControlService)
+
+ ~TextControlService();
+
+ public:
+ int GetCaretPosition() { return caret_position_; }
+ void SetCaretPosition(int position) { caret_position_ = position; }
+
+ bool IsCaretVisible() { return caret_visible_; }
+ void SetCaretVisible(bool visible);
+
+ // please set correct offset before calling this
+ void DrawCaret(platform::graph::IPainter* painter);
+
+ private:
+ void SetupCaretTimer();
+ void TearDownCaretTimer();
+
+ private:
+ Control* control_;
+ ITextControl* text_control_;
+ std::vector<EventRevokerGuard> event_revoker_guards_;
+
+ bool caret_visible_ = false;
+ int caret_position_ = 0;
+ unsigned long caret_timer_tag_;
+ // this is used for blinking of caret
+ bool caret_show_ = true;
+
+ // nullopt means not selecting
+ std::optional<MouseButton> select_down_button_;
+
+ // before the char
+ int select_start_position_;
+};
+} // namespace cru::ui::controls
diff --git a/include/cru/ui/render/layout_utility.hpp b/include/cru/ui/render/layout_utility.hpp
index 38191ad5..5cf74e71 100644
--- a/include/cru/ui/render/layout_utility.hpp
+++ b/include/cru/ui/render/layout_utility.hpp
@@ -1,6 +1,7 @@
+#pragma once
#include "../base.hpp"
namespace cru::ui::render {
- Size Min(const Size& left, const Size& right);
- Size Max(const Size& left, const Size& right);
-}
+Size Min(const Size& left, const Size& right);
+Size Max(const Size& left, const Size& right);
+} // namespace cru::ui::render
diff --git a/include/cru/ui/render/render_object.hpp b/include/cru/ui/render/render_object.hpp
index 0f0cb8df..d9851757 100644
--- a/include/cru/ui/render/render_object.hpp
+++ b/include/cru/ui/render/render_object.hpp
@@ -70,6 +70,8 @@ class RenderObject : public Object {
Point GetOffset() const { return offset_; }
void SetOffset(const Point& offset) { offset_ = offset; }
+ Point GetTotalOffset() const;
+ Point FromRootToContent(const Point& point) const;
Size GetSize() const { return size_; }
void SetSize(const Size& size) { size_ = size; }
@@ -91,9 +93,7 @@ class RenderObject : public Object {
virtual RenderObject* HitTest(const Point& point) = 0;
- protected:
- void SetChildMode(ChildMode mode) { child_mode_ = mode; }
-
+ public:
void InvalidateLayout() const {
if (render_host_ != nullptr) render_host_->InvalidateLayout();
}
@@ -103,6 +103,9 @@ class RenderObject : public Object {
}
protected:
+ void SetChildMode(ChildMode mode) { child_mode_ = mode; }
+
+ protected:
virtual void OnParentChanged(RenderObject* old_parent,
RenderObject* new_parent);
diff --git a/include/cru/ui/render/text_render_object.hpp b/include/cru/ui/render/text_render_object.hpp
index 5394ee0a..33d34f78 100644
--- a/include/cru/ui/render/text_render_object.hpp
+++ b/include/cru/ui/render/text_render_object.hpp
@@ -1,6 +1,8 @@
#pragma once
#include "render_object.hpp"
+#include "cru/platform/graph/text_layout.hpp"
+
#include <memory>
#include <string>
@@ -8,7 +10,6 @@
namespace cru::platform::graph {
struct IBrush;
struct IFont;
-struct ITextLayout;
} // namespace cru::platform::graph
namespace cru::ui::render {
@@ -34,6 +35,10 @@ class TextRenderObject : public RenderObject {
std::shared_ptr<platform::graph::IFont> GetFont() const;
void SetFont(std::shared_ptr<platform::graph::IFont> font);
+ std::vector<Rect> TextRangeRect(const TextRange& text_range);
+ Point TextSingleRect(int position, bool trailing);
+ platform::graph::TextHitTestResult TextHitTest(const Point& point);
+
std::optional<TextRange> GetSelectionRange() const {
return selection_range_;
}
diff --git a/include/cru/ui/ui_event.hpp b/include/cru/ui/ui_event.hpp
index 147d3da6..c5af2b61 100644
--- a/include/cru/ui/ui_event.hpp
+++ b/include/cru/ui/ui_event.hpp
@@ -1,6 +1,8 @@
#pragma once
#include "base.hpp"
+#include "cru/common/event.hpp"
+
#include <memory>
#include <optional>
#include <type_traits>
@@ -80,6 +82,7 @@ class MouseEventArgs : public UiEventArgs {
MouseEventArgs& operator=(MouseEventArgs&& other) = default;
~MouseEventArgs() override = default;
+ // This point is relative to window client lefttop.
Point GetPoint() const { return point_.value_or(Point{}); }
private:
diff --git a/include/cru/ui/ui_manager.hpp b/include/cru/ui/ui_manager.hpp
index f196444c..8e5c7075 100644
--- a/include/cru/ui/ui_manager.hpp
+++ b/include/cru/ui/ui_manager.hpp
@@ -15,6 +15,7 @@ struct ThemeResources {
std::shared_ptr<platform::graph::IFont> default_font;
std::shared_ptr<platform::graph::IBrush> text_brush;
std::shared_ptr<platform::graph::IBrush> text_selection_brush;
+ std::shared_ptr<platform::graph::IBrush> caret_brush;
controls::ButtonStyle button_style;
};
diff --git a/include/cru/win/graph/direct/font.hpp b/include/cru/win/graph/direct/font.hpp
index 08213ca5..ee2e319b 100644
--- a/include/cru/win/graph/direct/font.hpp
+++ b/include/cru/win/graph/direct/font.hpp
@@ -24,6 +24,8 @@ class DWriteFont : public DirectGraphResource,
return text_format_.Get();
}
+ float GetFontSize() override;
+
private:
Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
};