Numworks Epsilon  1.4.1
Graphing Calculator Operating System
text_area.h
Go to the documentation of this file.
1 #ifndef ESCHER_TEXT_AREA_H
2 #define ESCHER_TEXT_AREA_H
3 
4 #include <escher/text_input.h>
6 #include <assert.h>
7 #include <string.h>
8 
9 class TextArea : public TextInput {
10 public:
11  TextArea(Responder * parentResponder, char * textBuffer = nullptr, size_t textBufferSize = 0, TextAreaDelegate * delegate = nullptr, KDText::FontSize fontSize = KDText::FontSize::Large,
13  void setDelegate(TextAreaDelegate * delegate) { m_delegate = delegate; }
14  bool handleEvent(Ion::Events::Event event) override;
15  bool handleEventWithText(const char * text, bool indentation = false) override;
16  void setText(char * textBuffer, size_t textBufferSize);
17 private:
18  int indentationBeforeCursor() const;
19  bool insertTextWithIndentation(const char * textBuffer, int location);
20  TextInputDelegate * delegate() override {
21  return m_delegate;
22  }
23  class Text {
24  public:
25  Text(char * buffer, size_t bufferSize);
26  void setText(char * buffer, size_t bufferSize);
27  const char * text() const { return const_cast<const char *>(m_buffer); }
28  class Line {
29  public:
30  Line(const char * text);
31  const char * text() const { return m_text; }
32  size_t length() const { return m_length; }
33  bool contains(const char * c) const;
34  private:
35  const char * m_text;
36  size_t m_length;
37  };
38 
39  class LineIterator {
40  public:
41  LineIterator(const char * text) : m_line(text) {}
42  Line operator*() { return m_line; }
44  bool operator!=(const LineIterator& it) const { return m_line.text() != it.m_line.text(); }
45  private:
46  Line m_line;
47  };
48 
49  class Position {
50  public:
51  Position(int column, int line) : m_column(column), m_line(line) {}
52  int column() const { return m_column; }
53  int line() const { return m_line; }
54  private:
55  int m_column;
56  int m_line;
57  };
58 
59  LineIterator begin() const { return LineIterator(m_buffer); };
60  LineIterator end() const { return LineIterator(nullptr); };
61 
62  Position span() const;
63 
64  Position positionAtIndex(size_t index) const;
65  size_t indexAtPosition(Position p);
66 
67  void insertChar(char c, size_t index);
68  char removeChar(size_t index);
69  int removeRemainingLine(size_t index, int direction);
70  char operator[](size_t index) {
71  assert(index < m_bufferSize);
72  return m_buffer[index];
73  }
74  size_t bufferSize() const {
75  return m_bufferSize;
76  }
77  size_t textLength() const {
78  return strlen(m_buffer);
79  }
80  private:
81  char * m_buffer;
82  size_t m_bufferSize;
83  };
84 
85  class ContentView : public TextInput::ContentView {
86  public:
87  ContentView(char * textBuffer, size_t textBufferSize, KDText::FontSize size,
88  KDColor textColor, KDColor backgroundColor);
89  void drawRect(KDContext * ctx, KDRect rect) const override;
90  KDSize minimalSizeForOptimalDisplay() const override;
91  void setText(char * textBuffer, size_t textBufferSize);
92  const char * text() const override;
93  const Text * getText() const { return &m_text; }
94  size_t editedTextLength() const override {
95  return m_text.textLength();
96  }
97  bool insertTextAtLocation(const char * text, int location) override;
98  void moveCursorGeo(int deltaX, int deltaY);
99  bool removeChar() override;
100  bool removeEndOfLine() override;
101  bool removeStartOfLine();
102  private:
103  KDRect characterFrameAtIndex(size_t index) const override;
104  Text m_text;
105  };
106  const ContentView * nonEditableContentView() const override { return &m_contentView; }
107  ContentView m_contentView;
108  TextAreaDelegate * m_delegate;
109 };
110 
111 #endif
Line(const char *text)
Definition: text_area.cpp:25
#define assert(e)
Definition: assert.h:9
bool contains(const char *c) const
Definition: text_area.cpp:37
Responder * parentResponder() const
Definition: responder.cpp:12
Position(int column, int line)
Definition: text_area.h:51
LineIterator(const char *text)
Definition: text_area.h:41
Definition: size.h:6
c(generic_all_nodes)
constexpr KDColor KDColorWhite
Definition: color.h:42
size_t strlen(const char *s)
Definition: strlen.c:3
void setDelegate(TextAreaDelegate *delegate)
Definition: text_area.h:13
virtual bool removeEndOfLine()
Definition: text_input.cpp:114
bool insertTextAtLocation(const char *textBuffer, int location)
Definition: text_input.cpp:103
LineIterator & operator++()
Definition: text_area.cpp:41
void drawRect(KDContext *ctx, KDRect rect) const override
Definition: scroll_view.cpp:64
constexpr KDColor KDColorBlack
Definition: color.h:41
bool handleEvent(Ion::Events::Event event) override
Definition: text_area.cpp:303
bool removeChar()
Definition: text_input.cpp:80
const char * text() const
Definition: text_area.h:31
const char * text() const
Definition: text_input.h:14
virtual KDSize minimalSizeForOptimalDisplay() const
Definition: view.cpp:183
void setText(char *textBuffer, size_t textBufferSize)
Definition: text_area.cpp:340
Definition: rect.h:26
bool operator!=(const LineIterator &it) const
Definition: text_area.h:44
Definition: color.h:6
size_t length() const
Definition: text_area.h:32
KDColor backgroundColor() const
Definition: text_input.h:16
ContentView(KDText::FontSize size, KDColor textColor, KDColor backgroundColor)
Definition: text_input.cpp:6
FontSize
Definition: text.h:10
bool handleEventWithText(const char *text, bool indentation=false) override
Definition: text_area.cpp:294
TextArea(Responder *parentResponder, char *textBuffer=nullptr, size_t textBufferSize=0, TextAreaDelegate *delegate=nullptr, KDText::FontSize fontSize=KDText::FontSize::Large, KDColor textColor=KDColorBlack, KDColor backgroundColor=KDColorWhite)
Definition: text_area.cpp:284