aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_box.cpp
blob: 0d65f1ad20fa20a922dc497b4cf51d60b3c9c3ad (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
#include "text_box.h"

#include <cwctype>

#include "graph/graph.h"
#include "exception.h"

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

        caret_action_ = CreateActionPtr([this]
        {
            is_caret_show_ = !is_caret_show_;
            Repaint();
        });

        border_delegate_ = std::make_unique<BorderDelegate>(this);
    }

    TextBox::~TextBox() = default;

    void TextBox::OnDraw(ID2D1DeviceContext* device_context)
    {
        border_delegate_->Draw(device_context, GetSize());
        const auto border_thickness = border_delegate_->GetBorderThickness();
        graph::WithTransform(device_context, D2D1::Matrix3x2F::Translation(border_thickness.left, border_thickness.top), [this](ID2D1DeviceContext* device_context)
        {
            TextControl::OnDraw(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_ == nullptr);
        is_caret_show_ = true;
        caret_timer_ = SetInterval(Application::GetInstance()->GetCaretInfo().caret_blink_duration, caret_action_);
    }

    void TextBox::OnLoseFocusCore(events::FocusChangeEventArgs& args)
    {
        Control::OnLoseFocusCore(args);
        assert(caret_timer_ != nullptr);
        caret_timer_->Cancel();
        caret_timer_ = nullptr;
        is_caret_show_ = false;
    }

    void TextBox::OnKeyDownCore(events::KeyEventArgs& args)
    {
        Control::OnKeyDownCore(args);
        if (args.GetVirtualCode() == VK_LEFT && caret_position_ > 0)
        {
            ClearSelection();
            caret_position_--;
            Repaint();
        }

        if (args.GetVirtualCode() == VK_RIGHT && caret_position_ < GetText().size())
        {
            ClearSelection();
            caret_position_++;
            Repaint();
        }
    }

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

    Size TextBox::OnMeasure(const Size& available_size)
    {
        return TextMeasureWithPadding(available_size, border_delegate_->GetBorderThickness());
    }

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