aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_control.cpp
blob: fcfcb90c6a8f51aea0551d3433a639d517b0cfb9 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "text_control.hpp"

#include <cassert>

#include "ui/window.hpp"
#include "graph/graph.hpp"
#include "exception.hpp"
#include "ui/ui_manager.hpp"

namespace cru::ui::controls
{
    namespace
    {
        unsigned TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point)
        {
            BOOL is_trailing, is_inside;
            DWRITE_HIT_TEST_METRICS metrics{};
            text_layout->HitTestPoint(point.x, point.y, &is_trailing, &is_inside, &metrics);
            return is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1;
        }


    }

    TextControl::TextControl(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format,
        const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush)
    {
        text_format_ = init_text_format;

        RecreateTextLayout();

        brush_ = init_brush;

        selection_brush_ = UiManager::GetInstance()->GetPredefineResources()->text_control_selection_brush;

        SetClipContent(true);

        draw_content_event.AddHandler([this](events::DrawEventArgs& args)
        {
        });

        mouse_down_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args)
        {
             if (is_selectable_ && args.GetMouseButton() == MouseButton::Left && GetRect(RectRange::Padding).IsPointInside(args.GetPoint(this, RectRange::Margin)))
            {
                selected_range_ = std::nullopt;
                const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this));
                RequestChangeCaretPosition(hit_test_result);
                mouse_down_position_ = hit_test_result;
                is_selecting_ = true;
                GetWindow()->CaptureMouseFor(this);
                InvalidateDraw();
            }
        });

        mouse_move_event.bubble.AddHandler([this](events::MouseEventArgs& args)
        {
            if (is_selecting_)
            {
                const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this));
                RequestChangeCaretPosition(hit_test_result);
                selected_range_ = TextRange::FromTwoSides(hit_test_result, mouse_down_position_);
                InvalidateDraw();
            }
            UpdateCursor(args.GetPoint(this, RectRange::Margin));
        });


        mouse_up_event.bubble.AddHandler([this](events::MouseButtonEventArgs& args)
        {
            if (args.GetMouseButton() == MouseButton::Left)
            {
                if (is_selecting_)
                {
                    is_selecting_ = false;
                    GetWindow()->ReleaseCurrentMouseCapture();
                }
            }
        });

        lose_focus_event.direct.AddHandler([this](events::FocusChangeEventArgs& args)
        {
            if (is_selecting_)
            {
                is_selecting_ = false;
                GetWindow()->ReleaseCurrentMouseCapture();
            }
            if (!args.IsWindow()) // If the focus lose is triggered window-wide, then save the selection state. Otherwise, clear selection.
            {
                selected_range_ = std::nullopt;
                InvalidateDraw();
            }
        });
    }


    void TextControl::SetText(const String& text)
    {
        if (text_ != text)
        {
            const auto old_text = text_;
            text_ = text;
            OnTextChangedCore(old_text, text);
        }
    }

    void TextControl::SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush)
    {
        brush_ = brush;
        InvalidateDraw();
    }

    void TextControl::SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format)
    {
        text_format_ = text_format;
        RecreateTextLayout();
        InvalidateDraw();
    }

    void TextControl::SetSelectable(const bool is_selectable)
    {
        if (is_selectable_ != is_selectable)
        {
            if (!is_selectable)
            {
                if (is_selecting_)
                {
                    is_selecting_ = false;
                    GetWindow()->ReleaseCurrentMouseCapture();
                }
                selected_range_ = std::nullopt;
                InvalidateDraw();
            }
            is_selectable_ = is_selectable;
            UpdateCursor(std::nullopt);
        }
    }

    void TextControl::SetSelectedRange(std::optional<TextRange> text_range)
    {
        if (is_selectable_)
        {
            selected_range_ = text_range;
            InvalidateDraw();
        }
    }



    void TextControl::RequestChangeCaretPosition(unsigned position)
    {

    }

    void TextControl::OnRectChange(const Rect& old_rect, const Rect& new_rect)
    {
        const auto content = GetRect(RectRange::Content);
        ThrowIfFailed(text_layout_->SetMaxWidth(content.width));
        ThrowIfFailed(text_layout_->SetMaxHeight(content.height));
    }

    void TextControl::OnTextChangedCore(const String& old_text, const String& new_text)
    {
        RecreateTextLayout();
        InvalidateLayout();
        InvalidateDraw();
    }

    void TextControl::UpdateCursor(const std::optional<Point>& point)
    {
        if (!is_selectable_)
        {
            SetCursor(nullptr);
            return;
        }

        const auto window = GetWindow();
        if (window == nullptr)
        {
            SetCursor(nullptr);
            return;
        }

        if (is_selecting_)
        {
            SetCursor(cursors::i_beam);
            return;
        }

        const auto p = point.value_or(WindowToControl(window->GetMousePosition()));
        if (GetRect(RectRange::Padding).IsPointInside(p))
            SetCursor(cursors::i_beam);
        else
            SetCursor(nullptr);
    }
}