aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_box.cpp
blob: 605e1a24b4dccef4865c87b0ae4df78c55efe61f (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
197
198
199
200
201
202
203
204
205
#include "text_box.hpp"

#include <cwctype>
#include <cassert>

#include "graph/graph.hpp"
#include "exception.hpp"

namespace cru::ui::controls
{
    using graph::CreateSolidBrush;

    inline Microsoft::WRL::ComPtr<IDWriteFactory> GetDWriteFactory()
    {
        return graph::GraphManager::GetInstance()->GetDWriteFactory();
    }

    TextBox::TextBox(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format,
        const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : TextControl(init_text_format, init_brush)
    {
        SetSelectable(true);

        caret_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black));

        SetBordered(true);
    }

    TextBox::~TextBox() = default;

    StringView TextBox::GetControlType() const
    {
        return control_type;
    }

    void TextBox::OnDrawContent(ID2D1DeviceContext* device_context)
    {
        TextControl::OnDrawContent(device_context);
        if (is_caret_show_)
        {
            const auto caret_half_width = Application::GetInstance()->GetCaretInfo().half_caret_width;
            FLOAT x, y;
            DWRITE_HIT_TEST_METRICS metrics{};
            ThrowIfFailed(text_layout_->HitTestTextPosition(caret_position_, FALSE, &x, &y, &metrics));
            device_context->FillRectangle(D2D1::RectF(metrics.left - caret_half_width, metrics.top, metrics.left + caret_half_width, metrics.top + metrics.height), caret_brush_.Get());
        }
    }

    void TextBox::OnGetFocusCore(events::FocusChangeEventArgs& args)
    {
        TextControl::OnGetFocusCore(args);
        assert(!caret_timer_.has_value());
        is_caret_show_ = true;
        caret_timer_ = SetInterval(Application::GetInstance()->GetCaretInfo().caret_blink_duration, [this]
        {
            is_caret_show_ = !is_caret_show_;
            Repaint();
        });
    }

    void TextBox::OnLoseFocusCore(events::FocusChangeEventArgs& args)
    {
        Control::OnLoseFocusCore(args);
        assert(caret_timer_.has_value());
        caret_timer_->Cancel();
        caret_timer_ = std::nullopt;
        is_caret_show_ = false;
    }

    void TextBox::OnKeyDownCore(events::KeyEventArgs& args)
    {
        Control::OnKeyDownCore(args);
        if (args.GetVirtualCode() == VK_LEFT && caret_position_ > 0)
        {
            if (IsKeyDown(VK_SHIFT))
            {
                if (GetCaretSelectionSide())
                    ShiftLeftSelectionRange(-1);
                else
                    ShiftRightSelectionRange(-1);
            }
            else
            {
                const auto selection = GetSelectedRange();
                if (selection.has_value())
                {
                    ClearSelection();
                    caret_position_ = selection.value().position;
                }
                else
                    caret_position_--;
            }
            Repaint();
        }

        if (args.GetVirtualCode() == VK_RIGHT && caret_position_ < GetText().size())
        {
            if (IsKeyDown(VK_SHIFT))
            {
                if (GetCaretSelectionSide())
                    ShiftLeftSelectionRange(1);
                else
                    ShiftRightSelectionRange(1);
            }
            else
            {
                const auto selection = GetSelectedRange();
                if (selection.has_value())
                {
                    ClearSelection();
                    caret_position_ = selection.value().position + selection.value().count;
                }
                else
                    caret_position_++;
            }
        }
    }

    void TextBox::OnCharCore(events::CharEventArgs& args)
    {
        Control::OnCharCore(args);
        if (args.GetChar() == L'\b')
        {
            if (GetSelectedRange().has_value())
            {
                const auto selection_range = GetSelectedRange().value();
                auto text = GetText();
                text.erase(text.cbegin() + selection_range.position, text.cbegin() + selection_range.position + selection_range.count);
                SetText(text);
                caret_position_ = selection_range.position;
                ClearSelection();
            }
            else
            {
                if (caret_position_ > 0)
                {
                    auto text = GetText();
                    if (!text.empty())
                    {
                        const auto position = --caret_position_;
                        text.erase(text.cbegin() + position);
                        SetText(text);
                    }
                }
            }
            return;
        }

        if (std::iswprint(args.GetChar()))
        {
            if (GetSelectedRange().has_value())
            {
                const auto selection_range = GetSelectedRange().value();
                auto text = GetText();
                text.erase(selection_range.position, selection_range.count);
                text.insert(text.cbegin() + selection_range.position, args.GetChar());
                SetText(text);
                caret_position_ = selection_range.position + 1;
                ClearSelection();
            }
            else
            {
                ClearSelection();
                const auto position = caret_position_++;
                auto text = GetText();
                text.insert(text.cbegin() + position, { args.GetChar() });
                SetText(text);
            }
        }
    }

    void TextBox::RequestChangeCaretPosition(const unsigned position)
    {
        caret_position_ = position;
        Repaint();
    }

    bool TextBox::GetCaretSelectionSide() const
    {
        const auto selection = TextRange::ToTwoSides(GetSelectedRange(), caret_position_);
        if (selection.first == caret_position_)
            return true;
        if (selection.second == caret_position_)
            return false;
        assert(false);
        return true;
    }

    void TextBox::ShiftLeftSelectionRange(const int count)
    {
        const auto selection_range_side = TextRange::ToTwoSides(GetSelectedRange(), caret_position_);
        int new_left = selection_range_side.first + count;
        new_left = new_left < 0 ? 0 : new_left; // at least 0
        caret_position_ = new_left;
        SetSelectedRange(TextRange::FromTwoSides(static_cast<unsigned>(new_left), selection_range_side.second));
    }

    void TextBox::ShiftRightSelectionRange(const int count)
    {
        const auto selection_range_side = TextRange::ToTwoSides(GetSelectedRange(), caret_position_);
        int new_right = selection_range_side.second + count;
        new_right = new_right < 0 ? 0 : new_right; // at least 0
        caret_position_ = new_right;
        SetSelectedRange(TextRange::FromTwoSides(selection_range_side.first, static_cast<unsigned>(new_right)));
    }
}