TermOx
text_view.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_TEXT_DISPLAY_HPP
2 #define TERMOX_WIDGET_WIDGETS_TEXT_DISPLAY_HPP
3 #include <memory>
4 #include <vector>
5 
6 #include <signals_light/signal.hpp>
7 
8 #include <termox/painter/brush.hpp>
9 #include <termox/painter/glyph_string.hpp>
10 #include <termox/painter/painter.hpp>
11 #include <termox/widget/align.hpp>
12 #include <termox/widget/point.hpp>
13 #include <termox/widget/widget.hpp>
14 #include <termox/widget/wrap.hpp>
15 
16 namespace ox {
17 
19 
21 class Text_view : public Widget {
22  public:
23  struct Parameters {
24  Glyph_string text = U"";
25  Align alignment = Align::Left;
26  Wrap wrap = Wrap::Word;
27  Brush insert_brush = Brush{};
28  };
29 
30  public:
32 
34 
36  sl::Signal<void(int n)> scrolled_up;
37 
39  sl::Signal<void(int n)> scrolled_down;
40 
42  sl::Signal<void(int n)> scrolled_to;
43 
45  sl::Signal<void(Glyph_string const&)> contents_modified;
46 
48  sl::Signal<void(int)> line_count_changed;
49 
50  public:
52 
53  explicit Text_view(Glyph_string text = U"",
54  Align alignment = Align::Left,
55  Wrap wrap = Wrap::Word,
57 
58  explicit Text_view(Parameters p);
59 
60  public:
62 
65 
67 
70  [[nodiscard]] auto text() -> Glyph_string&;
71 
73  [[nodiscard]] auto text() const -> Glyph_string const&;
74 
76 
78  void set_alignment(Align type);
79 
81  [[nodiscard]] auto alignment() const -> Align;
82 
84  void set_wrap(Wrap w);
85 
87  [[nodiscard]] auto wrap() const -> Wrap;
88 
89  public:
91 
94  void insert(Glyph_string text, int index);
95 
97 
98  void append(Glyph_string text);
99 
101  void erase(int index, int length = Glyph_string::npos);
102 
104  void pop_back();
105 
107 
108  void clear();
109 
111 
112  virtual void scroll_up(int n = 1);
113 
115 
116  virtual void scroll_down(int n = 1);
117 
119 
120  [[nodiscard]] auto row_length(int y) const -> int;
121 
123  [[nodiscard]] auto display_height() const -> int;
124 
126  [[nodiscard]] auto line_count() const -> int;
127 
129  void set_top_line(int n);
130 
132 
135  [[nodiscard]] auto index_at(Point position) const -> int;
136 
138 
140  [[nodiscard]] auto display_position(int index) const -> Point;
141 
143  void update() override;
144 
145  protected:
147  auto paint_event(Painter& p) -> bool override;
148 
150  [[nodiscard]] auto line_at(int index) const -> int;
151 
153  [[nodiscard]] auto top_line() const -> int;
154 
156  [[nodiscard]] auto bottom_line() const -> int;
157 
159  [[nodiscard]] auto last_line() const -> int;
160 
162  [[nodiscard]] auto first_index_at(int line) const -> int;
163 
165  [[nodiscard]] auto last_index_at(int line) const -> int;
166 
168  [[nodiscard]] auto line_length(int line) const -> int;
169 
171  [[nodiscard]] auto end_index() const -> int;
172 
174 
176  void update_display(int from_line = 0);
177 
178  private:
180  struct Line_info {
181  int start_index;
182  int length;
183  };
184 
185  private:
186  Glyph_string contents_;
187  Align alignment_;
188  Wrap wrap_;
189 
190  int top_line_ = 0; // Index into display_state_.
191  std::vector<Line_info> display_state_ = {Line_info{0, 0}};
192 };
193 
195 [[nodiscard]] auto text_view(Glyph_string text = U"",
196  Align alignment = Align::Left,
197  Wrap wrap = Wrap::Word)
198  -> std::unique_ptr<Text_view>;
199 
201 [[nodiscard]] auto text_view(Text_view::Parameters p)
202  -> std::unique_ptr<Text_view>;
203 
204 } // namespace ox
205 #endif // TERMOX_WIDGET_WIDGETS_TEXT_DISPLAY_HPP
Holds the look of any paintable object with Traits and Colors.
Definition: brush.hpp:13
Holds a collection of Glyphs with a similar interface to std::string.
Definition: glyph_string.hpp:19
Contains functions to paint Glyphs to a Widget's screen area.
Definition: painter.hpp:21
Non-interactive box to display a given Glyph_string.
Definition: text_view.hpp:21
void update_display(int from_line=0)
Recalculate the text layout via display_state_.
Definition: text_view.cpp:298
auto alignment() const -> Align
Return the currently used Alignment.
Definition: text_view.cpp:53
auto index_at(Point position) const -> int
Return the index into the contents from a physical Point on the Widget.
Definition: text_view.cpp:166
sl::Signal< void(int n)> scrolled_down
Emitted when text is scrolled down. Sends number of lines scrolled by.
Definition: text_view.hpp:39
auto paint_event(Painter &p) -> bool override
Paint the portion of contents that is currently visible on screen.
Definition: text_view.cpp:211
sl::Signal< void(int n)> scrolled_to
Emitted when text is scrolled, sends the top line.
Definition: text_view.hpp:42
Brush insert_brush
Brush to be applied to all new incoming Glyphs, but not existing Glyphs.
Definition: text_view.hpp:33
auto display_position(int index) const -> Point
Return the position of the Glyph at index.
Definition: text_view.cpp:183
void set_wrap(Wrap w)
Set the type of text wrapping at line boundaries.
Definition: text_view.cpp:55
auto last_index_at(int line) const -> int
Returns one past the last index of the last Glyph at line number line.
Definition: text_view.cpp:281
void pop_back()
Remove the last Glyph from the current contents. No-op if this->empty();.
Definition: text_view.cpp:101
auto wrap() const -> Wrap
Return the currently set text Wrap type.
Definition: text_view.cpp:61
void insert(Glyph_string text, int index)
Inserts text starting at index into the current contents.
Definition: text_view.cpp:63
auto bottom_line() const -> int
Return line number that is being displayed at the bottom of the Widget.
Definition: text_view.cpp:266
void set_top_line(int n)
Set the top line, by row index.
Definition: text_view.cpp:159
sl::Signal< void(int)> line_count_changed
Emitted when total line count changes.
Definition: text_view.hpp:48
auto first_index_at(int line) const -> int
Return the index of the first Glyph at line number line.
Definition: text_view.cpp:274
void update() override
Add call to Text_view::update_display() before posting Paint_event.
Definition: text_view.cpp:201
auto line_at(int index) const -> int
Return the line number that contains index.
Definition: text_view.cpp:252
virtual void scroll_up(int n=1)
Scroll the display up by n lines.
Definition: text_view.cpp:119
void append(Glyph_string text)
Inserts text to the end of the current contents.
Definition: text_view.cpp:79
auto end_index() const -> int
Return the index of the last Glyph in contents.
Definition: text_view.cpp:296
void erase(int index, int length=Glyph_string::npos)
Remove Glyphs from contents starting at index, for length Glyphs.
Definition: text_view.cpp:88
auto row_length(int y) const -> int
Return the length of the line at row y.
Definition: text_view.cpp:145
auto display_height() const -> int
Return the number of lines currently displayed.
Definition: text_view.cpp:151
virtual void scroll_down(int n=1)
Scroll the display down by n lines.
Definition: text_view.cpp:132
void clear()
Remove all Glyphs from the this text display.
Definition: text_view.cpp:110
auto text() -> Glyph_string &
Return the entire contents of the Text_view.
Definition: text_view.cpp:43
sl::Signal< void(Glyph_string const &)> contents_modified
Emitted when contents are modified. Sends a reference to the contents.
Definition: text_view.hpp:45
auto line_count() const -> int
Return the total number of lines in display_state_.
Definition: text_view.cpp:157
sl::Signal< void(int n)> scrolled_up
Emitted when text is scrolled up. Sends number of lines scrolled by.
Definition: text_view.hpp:36
Text_view(Glyph_string text=U"", Align alignment=Align::Left, Wrap wrap=Wrap::Word, Brush insert_brush=Brush{})
Construct a Text_view with initial Glyph_string text.
Definition: text_view.cpp:19
auto top_line() const -> int
Return the line number that is being displayed at the top of the Widget.
Definition: text_view.cpp:264
void set_alignment(Align type)
Set the Alignment, changing how the contents are displayed.
Definition: text_view.cpp:47
void set_text(Glyph_string text)
Replace the current contents with text.
Definition: text_view.cpp:34
auto last_line() const -> int
Return the index into display_state_ of the last line.
Definition: text_view.cpp:272
auto line_length(int line) const -> int
Return the number of Glyphs contained at line index line.
Definition: text_view.cpp:289
Definition: widget.hpp:31
Definition: text_view.hpp:23