aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_control.hpp
blob: 58d48c1391c3f979f75cd12bf9330a389176b100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

// ReSharper disable once CppUnusedIncludeDirective
#include "pre.hpp"

#include "ui/control.hpp"

namespace cru::ui::controls
{
    class TextControl : public NoChildControl
    {
    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);

        Size OnMeasureContent(const Size& available_size, const AdditionalMeasureInfo&) override final;

        virtual void RequestChangeCaretPosition(unsigned position);

        void OnRectChange(const Rect& old_rect, const Rect& new_rect) override;

    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;
    };
}