Numworks Epsilon  1.4.1
Graphing Calculator Operating System
text_field.h
Go to the documentation of this file.
1 #ifndef ESCHER_TEXT_FIELD_H
2 #define ESCHER_TEXT_FIELD_H
3 
4 #include <escher/text_input.h>
6 #include <string.h>
7 
8 class TextField : public TextInput {
9 public:
10  TextField(Responder * parentResponder, char * textBuffer, char * draftTextBuffer, size_t textBufferSize,
11  TextFieldDelegate * delegate = nullptr, bool hasTwoBuffers = true, KDText::FontSize size = KDText::FontSize::Large, float horizontalAlignment = 0.0f,
12  float verticalAlignment = 0.5f, KDColor textColor = KDColorBlack, KDColor = KDColorWhite);
13  void setDelegate(TextFieldDelegate * delegate) { m_delegate = delegate; }
14  void setDraftTextBuffer(char * draftTextBuffer);
15  bool isEditing() const;
16  size_t draftTextLength() const;
17  void setText(const char * text);
18  void setAlignment(float horizontalAlignment, float verticalAlignment);
19  virtual void setEditing(bool isEditing, bool reinitDraftBuffer = true);
20  KDSize minimalSizeForOptimalDisplay() const override;
21  bool handleEventWithText(const char * text, bool indenting = false) override;
22  bool handleEvent(Ion::Events::Event event) override;
23  constexpr static int maxBufferSize() {
25  }
26  void scrollToCursor() override;
27  bool textFieldShouldFinishEditing(Ion::Events::Event event) { return m_delegate->textFieldShouldFinishEditing(this, event); }
28 protected:
30  public:
31  ContentView(char * textBuffer, char * draftTextBuffer, size_t textBufferSize, KDText::FontSize size, float horizontalAlignment = 0.0f, float verticalAlignment = 0.5f, KDColor textColor = KDColorBlack, KDColor = KDColorWhite);
33  void drawRect(KDContext * ctx, KDRect rect) const override;
34  bool isEditing() const { return m_isEditing; }
35  const char * text() const override;
36  size_t editedTextLength() const override;
37  char * textBuffer() { return m_textBuffer; }
38  char * draftTextBuffer() { return m_draftTextBuffer; }
39  int bufferSize() { return k_maxBufferSize; }
40  void setText(const char * text);
41  void setAlignment(float horizontalAlignment, float verticalAlignment);
42  void setEditing(bool isEditing, bool reinitDraftBuffer);
43  void reinitDraftTextBuffer();
44  /* If the text to be appended is too long to be added without overflowing the
45  * buffer, nothing is done (not even adding few letters from the text to reach
46  * the maximum buffer capacity) and false is returned. */
47  bool insertTextAtLocation(const char * text, int location) override;
48  KDSize minimalSizeForOptimalDisplay() const override;
49  bool removeChar() override;
50  bool removeEndOfLine() override;
51  /* In some app (ie Calculation), text fields record expression results whose
52  * lengths can reach 70 (ie
53  * [[1.234567e-123*e^(1.234567e-123*i), 1.234567e-123*e^(1.234567e-123*i)]]).
54  * In order to be able to record those output text, k_maxBufferSize must be
55  * over 70. */
56  constexpr static int k_maxBufferSize = 152;
57  private:
58  void layoutSubviews() override;
59  KDRect characterFrameAtIndex(size_t index) const override;
60  bool m_isEditing;
61  char * m_textBuffer;
62  char * m_draftTextBuffer;
63  size_t m_currentDraftTextLength;
64  size_t m_textBufferSize;
65  float m_horizontalAlignment;
66  float m_verticalAlignment;
67  };
68  const ContentView * nonEditableContentView() const override { return &m_contentView; }
70 private:
71  bool privateHandleEvent(Ion::Events::Event event);
72  TextInputDelegate * delegate() override {
73  return m_delegate;
74  }
75  bool m_hasTwoBuffers;
76  TextFieldDelegate * m_delegate;
77 };
78 
79 #endif
bool isEditing() const
Definition: text_field.cpp:175
void setDraftTextBuffer(char *draftTextBuffer)
Definition: text_field.cpp:21
static constexpr int maxBufferSize()
Definition: text_field.h:23
void setDelegate(TextFieldDelegate *delegate)
Definition: text_field.h:13
void setText(const char *text)
Definition: text_field.cpp:184
TextField(Responder *parentResponder, char *textBuffer, char *draftTextBuffer, size_t textBufferSize, TextFieldDelegate *delegate=nullptr, bool hasTwoBuffers=true, KDText::FontSize size=KDText::FontSize::Large, float horizontalAlignment=0.0f, float verticalAlignment=0.5f, KDColor textColor=KDColorBlack, KDColor=KDColorWhite)
Definition: text_field.cpp:161
static constexpr int k_maxBufferSize
Definition: text_field.h:56
size_t editedTextLength() const override
Definition: text_field.cpp:41
Responder * parentResponder() const
Definition: responder.cpp:12
bool removeChar() override
Definition: text_field.cpp:113
void setDraftTextBuffer(char *draftTextBuffer)
Definition: text_field.cpp:171
void setAlignment(float horizontalAlignment, float verticalAlignment)
Definition: text_field.cpp:192
void setText(const char *text)
Definition: text_field.cpp:45
void setEditing(bool isEditing, bool reinitDraftBuffer)
Definition: text_field.cpp:63
bool removeEndOfLine() override
Definition: text_field.cpp:133
bool textFieldShouldFinishEditing(Ion::Events::Event event)
Definition: text_field.h:27
virtual void setEditing(bool isEditing, bool reinitDraftBuffer=true)
Definition: text_field.cpp:196
const char * text() const override
Definition: text_field.cpp:34
Definition: size.h:6
constexpr KDColor KDColorWhite
Definition: color.h:42
char * draftTextBuffer()
Definition: text_field.h:38
bool isEditing() const
Definition: text_field.h:34
void drawRect(KDContext *ctx, KDRect rect) const override
Definition: text_field.cpp:25
ContentView(char *textBuffer, char *draftTextBuffer, size_t textBufferSize, KDText::FontSize size, float horizontalAlignment=0.0f, float verticalAlignment=0.5f, KDColor textColor=KDColorBlack, KDColor=KDColorWhite)
Definition: text_field.cpp:8
constexpr KDColor KDColorBlack
Definition: color.h:41
KDSize minimalSizeForOptimalDisplay() const override
Definition: text_field.cpp:105
KDSize minimalSizeForOptimalDisplay() const override
Definition: text_field.cpp:273
const char * text() const
Definition: text_input.h:14
Definition: rect.h:26
ContentView m_contentView
Definition: text_field.h:69
Definition: color.h:6
bool handleEventWithText(const char *text, bool indenting=false) override
Definition: text_field.cpp:303
size_t draftTextLength() const
Definition: text_field.cpp:179
bool handleEvent(Ion::Events::Event event) override
Definition: text_field.cpp:277
virtual bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event)=0
bool insertTextAtLocation(const char *text, int location) override
Definition: text_field.cpp:81
void setAlignment(float horizontalAlignment, float verticalAlignment)
Definition: text_field.cpp:57
const ContentView * nonEditableContentView() const override
Definition: text_field.h:68
FontSize
Definition: text.h:10
void scrollToCursor() override
Definition: text_field.cpp:296