diff options
author | Yuqian Yang <crupest@outlook.com> | 2018-09-25 10:13:48 +0000 |
---|---|---|
committer | Yuqian Yang <crupest@outlook.com> | 2018-09-25 10:13:48 +0000 |
commit | c8ddc4bc7d96b67aac9526089ade64b31007fa3e (patch) | |
tree | 27317ae0d9ac5b1409408ef165595f9c127aa027 /src/ui/controls/text_control.h | |
parent | ea4b0966d8eb5a8e76dfbe4d833a07a4797a3284 (diff) | |
parent | da768af31952a28aa7aca8f60acd85bf2832465c (diff) | |
download | cru-c8ddc4bc7d96b67aac9526089ade64b31007fa3e.tar.gz cru-c8ddc4bc7d96b67aac9526089ade64b31007fa3e.tar.bz2 cru-c8ddc4bc7d96b67aac9526089ade64b31007fa3e.zip |
Merge branch 'issue4-dev' into 'master'
Resolve "Abstract out the common part of TextBlock and TextBox."
Closes #4
See merge request crupest/CruUI!3
Diffstat (limited to 'src/ui/controls/text_control.h')
-rw-r--r-- | src/ui/controls/text_control.h | 106 |
1 files changed, 106 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..a24766dc --- /dev/null +++ b/src/ui/controls/text_control.h @@ -0,0 +1,106 @@ +#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_; + } + + std::optional<TextRange> GetSelectedRange() const + { + return selected_range_; + } + + void SetSelectedRange(std::optional<TextRange> text_range); + + void ClearSelection() + { + SetSelectedRange(std::nullopt); + } + + protected: + void SetSelectable(bool is_selectable); + + 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; + + Size OnMeasure(const Size& available_size) override final; + + virtual void RequestChangeCaretPosition(unsigned position); + + 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_; + protected: + Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; + + private: + 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; + }; +} |