aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_control.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-11-07 21:40:04 +0800
committercrupest <crupest@outlook.com>2018-11-07 21:40:04 +0800
commitefdce672123284847bd7fb6f12ac1ec96f28f3ef (patch)
tree298e6313e9a48c5867b2355242b78d3cd23fdc61 /src/ui/controls/text_control.hpp
parent634dab6ad2c9e4675beacfb77ac02b2d43cab132 (diff)
downloadcru-efdce672123284847bd7fb6f12ac1ec96f28f3ef.tar.gz
cru-efdce672123284847bd7fb6f12ac1ec96f28f3ef.tar.bz2
cru-efdce672123284847bd7fb6f12ac1ec96f28f3ef.zip
Make all header *.hpp .
Diffstat (limited to 'src/ui/controls/text_control.hpp')
-rw-r--r--src/ui/controls/text_control.hpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/ui/controls/text_control.hpp b/src/ui/controls/text_control.hpp
new file mode 100644
index 00000000..93120a44
--- /dev/null
+++ b/src/ui/controls/text_control.hpp
@@ -0,0 +1,101 @@
+#pragma once
+
+#include "ui/control.hpp"
+
+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:
+ 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);
+
+ 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 OnDrawContent(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 OnMeasureContent(const Size& available_size) override;
+
+
+ virtual void RequestChangeCaretPosition(unsigned position);
+
+ private:
+ void OnTextChangedCore(const String& old_text, const String& new_text);
+
+ void RecreateTextLayout();
+
+ // param point is the mouse point relative to this control.
+ void UpdateCursor(const std::optional<Point>& point);
+
+ 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:
+ bool is_selectable_ = false;
+
+ bool is_selecting_ = false;
+ unsigned mouse_down_position_ = 0;
+ std::optional<TextRange> selected_range_ = std::nullopt;
+ };
+}