aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/text_render_object.hpp
blob: ac874b7560eef507a164c9cfee6b38b9e6016794 (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
#pragma once
#include "pre.hpp"

#include <wrl/client.h>  // for ComPtr

#include "render_object.hpp"

// forward declarations
struct ID2D1Brush;
struct IDWriteTextFormat;
struct IDWriteTextLayout;

namespace cru::ui::render {
class TextRenderObject : public RenderObject {
 public:
  TextRenderObject(Microsoft::WRL::ComPtr<ID2D1Brush> brush,
                   Microsoft::WRL::ComPtr<IDWriteTextFormat> format,
                   Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush);
  TextRenderObject(const TextRenderObject& other) = delete;
  TextRenderObject(TextRenderObject&& other) = delete;
  TextRenderObject& operator=(const TextRenderObject& other) = delete;
  TextRenderObject& operator=(TextRenderObject&& other) = delete;
  ~TextRenderObject() override = default;

  String GetText() const { return text_; }
  void SetText(String new_text) {
    text_ = std::move(new_text);
    RecreateTextLayout();
  }

  Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const { return brush_; }
  void SetBrush(Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) {
    brush_ = std::move(new_brush);
  }

  Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const {
    return text_format_;
  }
  void SetTextFormat(
      Microsoft::WRL::ComPtr<IDWriteTextFormat> new_text_format) {
    text_format_ = std::move(new_text_format);
    RecreateTextLayout();
  }

  std::optional<TextRange> GetSelectionRange() const {
    return selection_range_;
  }
  void SetSelectionRange(std::optional<TextRange> new_range) {
    selection_range_ = std::move(new_range);
  }

  Microsoft::WRL::ComPtr<ID2D1Brush> GetSelectionBrush() const {
    return selection_brush_;
  }
  void SetSelectionBrush(Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) {
    selection_brush_ = std::move(new_brush);
  }

  void Draw(ID2D1RenderTarget* render_target) override;

  RenderObject* HitTest(const Point& point) override;

 protected:
  void OnSizeChanged(const Size& old_size, const Size& new_size) override;

  Size OnMeasureContent(const Size& available_size) override;
  void OnLayoutContent(const Rect& content_rect) override;

 private:
  void RecreateTextLayout();

 private:
  String text_;

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

  std::optional<TextRange> selection_range_ = std::nullopt;
  Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_;
};
}  // namespace cru::ui::render