aboutsummaryrefslogtreecommitdiff
path: root/CruUI/ui/controls/text_block.h
blob: d6d173ba64df6a6843095fc8eb31b480c4e9f213 (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
#pragma once

#include <memory>

#include "ui/control.h"

namespace cru
{
    namespace ui
    {
        namespace controls
        {
            struct TextRange
            {
                TextRange() = default;
                TextRange(const int position, const int count)
                    : position(position), count(count)
                {
                    
                }
                TextRange(const TextRange& other) = default;
                TextRange(TextRange&& other) = default;
                TextRange& operator=(const TextRange& other) = default;
                TextRange& operator=(TextRange&& other) = default;
                ~TextRange() = default;

                unsigned position;
                unsigned count;

                operator DWRITE_TEXT_RANGE() const
                {
                    DWRITE_TEXT_RANGE result;
                    result.startPosition = position;
                    result.length = count;
                    return result;
                }
            };

            class TextBlock : public Control
            {
            public:
                using TextLayoutHandler = Action<Microsoft::WRL::ComPtr<IDWriteTextLayout>>;

                static TextBlock* Create(
                    const String& text = L"",
                    const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr,
                    const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush = nullptr)
                {
                    const auto text_block = new TextBlock(init_text_format, init_brush);
                    text_block->SetText(text);
                    return text_block;
                }

            private:
                explicit TextBlock(
                    const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr,
                    const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush = nullptr
                );
            public:
                TextBlock(const TextBlock& other) = delete;
                TextBlock(TextBlock&& other) = delete;
                TextBlock& operator=(const TextBlock& other) = delete;
                TextBlock& operator=(TextBlock&& other) = delete;
                ~TextBlock() override;

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


                void AddTextLayoutHandler(std::shared_ptr<TextLayoutHandler> handler)
                {
                    text_layout_handlers_.push_back(std::move(handler));
                }
                void RemoveTextLayoutHandler(const std::shared_ptr<TextLayoutHandler>& handler)
                {
                    const auto find_result = std::find(text_layout_handlers_.cbegin(), text_layout_handlers_.cend(), handler);
                    if (find_result != text_layout_handlers_.cend())
                        text_layout_handlers_.erase(find_result);
                }

            protected:
                void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final;
                void OnDraw(ID2D1DeviceContext* device_context) override;

                void OnMouseDownCore(events::MouseButtonEventArgs& args) override;
                void OnMouseMoveCore(events::MouseEventArgs& args) override;

                Size OnMeasure(const Size& available_size) override;

            private:
                void OnTextChangedCore(const String& old_text, const String& new_text);

                void CreateDefaultBrush();
                void CreateDefaultTextFormat();
                void RecreateTextLayout();

            private:
                String text_;

                Microsoft::WRL::ComPtr<ID2D1Brush> brush_;
                Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
                Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_;

                Vector<std::shared_ptr<TextLayoutHandler>> text_layout_handlers_;

                unsigned mouse_down_position_;
                std::optional<TextRange> selected_range_;

                bool is_mouse_down_ = false; //TODO!!!
            };
        }
    }
}