aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/text_common.hpp
blob: fbef6b063fb89fe71377c2741e0fd360842281ed (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
#pragma once
#include "../base.hpp"

#include "cru/ui/ui_event.hpp"

#include <functional>
#include <optional>

namespace cru::platform::graph {
struct IBrush;
}

namespace cru::ui::render {
class TextRenderObject;
class CanvasRenderObject;
}  // namespace cru::ui::render

namespace cru::ui::controls {

struct ITextControl : virtual Interface {
  virtual render::TextRenderObject* GetTextRenderObject() = 0;
  virtual render::CanvasRenderObject* GetCaretRenderObject() = 0;
  virtual platform::graph::IBrush* GetCaretBrush() = 0;
};

class TextControlService : public Object {
 public:
  TextControlService(Control* control, ITextControl* text_control);

  CRU_DELETE_COPY(TextControlService)
  CRU_DELETE_MOVE(TextControlService)

  ~TextControlService();

 public:
  bool IsEnabled() { return enable_; }
  void SetEnabled(bool enable);

  int GetCaretPosition() { return caret_position_; }
  void SetCaretPosition(int position) { caret_position_ = position; }

  bool IsCaretVisible() { return caret_visible_; }
  void SetCaretVisible(bool visible);

  // please set correct offset before calling this
  void DrawCaret(platform::graph::IPainter* painter);

 private:
  void AbortSelection();

  void SetupCaretTimer();
  void TearDownCaretTimer();

  void SetupHandlers();

  void MouseMoveHandler(event::MouseEventArgs& args);
  void MouseDownHandler(event::MouseButtonEventArgs& args);
  void MouseUpHandler(event::MouseButtonEventArgs& args);
  void LoseFocusHandler(event::FocusChangeEventArgs& args);

 private:
  Control* control_;
  ITextControl* text_control_;
  std::vector<EventRevokerGuard> event_revoker_guards_;

  bool enable_ = false;

  bool caret_visible_ = false;
  int caret_position_ = 0;
#ifdef CRU_DEBUG
  bool caret_timer_set_ = false;
#endif
  unsigned long caret_timer_tag_;
  // this is used for blinking of caret
  bool caret_show_ = true;

  // nullopt means not selecting
  std::optional<MouseButton> select_down_button_;

  // before the char
  int select_start_position_;
};
}  // namespace cru::ui::controls