diff options
author | crupest <crupest@outlook.com> | 2018-09-25 13:37:33 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-09-25 13:37:33 +0800 |
commit | ccbc293c0158d4450c0db344474193f17925403f (patch) | |
tree | 1ecde3132b3299ab2f272f541a76bced67f983b1 /src/ui/controls/text_control.h | |
parent | 184c3b2b39d3fa34f9349a7d7fbebe49bc62f7fc (diff) | |
parent | ea4b0966d8eb5a8e76dfbe4d833a07a4797a3284 (diff) | |
download | cru-ccbc293c0158d4450c0db344474193f17925403f.tar.gz cru-ccbc293c0158d4450c0db344474193f17925403f.tar.bz2 cru-ccbc293c0158d4450c0db344474193f17925403f.zip |
Merge branch 'master' into issue4-dev
Diffstat (limited to 'src/ui/controls/text_control.h')
-rw-r--r-- | src/ui/controls/text_control.h | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/ui/controls/text_control.h b/src/ui/controls/text_control.h new file mode 100644 index 00000000..ca23ec95 --- /dev/null +++ b/src/ui/controls/text_control.h @@ -0,0 +1,97 @@ +#pragma once + +#include "ui/control.h" + +namespace cru::ui::controls +{ + class TextControl : public Control + { + protected: + TextControl( + const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format, + const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush + ); + public: + using TextLayoutHandlerPtr = FunctionPtr<void(Microsoft::WRL::ComPtr<IDWriteTextLayout>)>; + + TextControl(const TextControl& other) = delete; + TextControl(TextControl&& other) = delete; + TextControl& operator=(const TextControl& other) = delete; + TextControl& operator=(TextControl&& other) = delete; + ~TextControl() override = default; + + String GetText() const + { + return text_; + } + + void SetText(const String& text); + + Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const + { + return brush_; + } + + void SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush); + + Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const + { + return text_format_; + } + + void SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format); + + + void AddTextLayoutHandler(TextLayoutHandlerPtr handler); + + void RemoveTextLayoutHandler(const TextLayoutHandlerPtr& handler); + + bool IsSelectable() const + { + return is_selectable_; + } + + void SetSelectable(bool is_selectable); + + std::optional<TextRange> GetSelectedRange() const + { + return selected_range_; + } + + void SetSelectedRange(std::optional<TextRange> text_range); + + protected: + void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final; + void OnDraw(ID2D1DeviceContext* device_context) override; + + void OnMouseDownCore(events::MouseButtonEventArgs& args) override final; + void OnMouseMoveCore(events::MouseEventArgs& args) override final; + void OnMouseUpCore(events::MouseButtonEventArgs& args) override final; + + void OnLoseFocusCore(events::FocusChangeEventArgs& args) override final; + + Size OnMeasure(const Size& available_size) override final; + + + private: + void OnTextChangedCore(const String& old_text, const String& new_text); + + void RecreateTextLayout(); + + private: + String text_; + + Microsoft::WRL::ComPtr<ID2D1Brush> brush_; + Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_; + Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; + Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; + + Vector<TextLayoutHandlerPtr> text_layout_handlers_; + + bool is_selectable_ = false; + + bool is_selecting_ = false; + unsigned mouse_down_position_ = 0; + std::optional<TextRange> selected_range_ = std::nullopt; + }; +} |