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

#include "ui/control.h"

namespace cru
{
    namespace ui
    {
        namespace controls
        {
            class TextBlock : public Control
            {
            public:
                explicit TextBlock(
                    const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr,
                    const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush = nullptr
                );
                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);

            protected:
                void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final;
                void OnDraw(ID2D1DeviceContext* device_context) 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_;
            };
        }
    }
}