Numworks Epsilon  1.4.1
Graphing Calculator Operating System
text_input.cpp
Go to the documentation of this file.
1 #include <escher/text_input.h>
2 #include <assert.h>
3 
4 /* TextInput::ContentView */
5 
7  View(),
8  m_cursorView(),
9  m_fontSize(size),
10  m_textColor(textColor),
11  m_backgroundColor(backgroundColor),
12  m_cursorIndex(0)
13 {
14 }
15 
17  m_backgroundColor = backgroundColor;
19 }
20 
22  m_textColor = textColor;
24 }
25 
27  int adjustedLocation = location < 0 ? 0 : location;
28  adjustedLocation = adjustedLocation > (signed int)editedTextLength() ? (signed int)editedTextLength() : adjustedLocation;
29  m_cursorIndex = adjustedLocation;
31 }
32 
34  return characterFrameAtIndex(m_cursorIndex);
35 }
36 
37 int TextInput::ContentView::numberOfSubviews() const {
38  return 1;
39 }
40 
41 View * TextInput::ContentView::subviewAtIndex(int index) {
42  return &m_cursorView;
43 }
44 
46  m_cursorView.setFrame(cursorRect());
47 }
48 
49 void TextInput::ContentView::reloadRectFromCursorPosition(size_t index, bool lineBreak) {
50  KDRect charRect = characterFrameAtIndex(index);
51  KDRect dirtyRect = KDRect(charRect.x(), charRect.y(), bounds().width() - charRect.x(), charRect.height());
52  if (lineBreak) {
53  dirtyRect = dirtyRect.unionedWith(KDRect(0, charRect.bottom()+1, bounds().width(), bounds().height()-charRect.bottom()-1));
54  }
55  markRectAsDirty(dirtyRect);
56 }
57 
58 /* TextInput */
59 
62 {
63 }
64 
66  if (delegate()) {
67  return delegate()->toolboxForTextInput(this);
68  }
69  return nullptr;
70 }
71 
72 void TextInput::setBackgroundColor(KDColor backgroundColor) {
74 }
75 
77  contentView()->setTextColor(textColor);
78 }
79 
83  return true;
84 }
85 
87  /* Technically, we do not need to overscroll in text input. However,
88  * logically, we should layout the scroll view before calling
89  * scrollToContentRect in case the size of the scroll view has changed and
90  * then call scrollToContentRect which call another layout of the scroll view
91  * if the offset has evolved. In order to avoid requiring two layouts, we
92  * allow overscrolling in scrollToContentRect and the last layout of the
93  * scroll view corrects the size of the scroll view only once. */
94  scrollToContentRect(contentView()->cursorRect(), true);
95 }
96 
97 bool TextInput::setCursorLocation(int location) {
98  contentView()->setCursorLocation(location);
100  return true;
101 }
102 
103 bool TextInput::insertTextAtLocation(const char * text, int location) {
104  if (contentView()->insertTextAtLocation(text, location)) {
105  /* We layout the scrollable view before scrolling to cursor because the
106  * content size might have changed. */
107  layoutSubviews();
108  scrollToCursor();
109  return true;
110  }
111  return false;
112 }
113 
115  if (contentView()->removeEndOfLine()) {
116  scrollToCursor();
117  return true;
118  }
119  return false;
120 }
bool setCursorLocation(int location)
Definition: text_input.cpp:97
void setTextColor(KDColor textColor)
Definition: text_input.cpp:21
void setTextColor(KDColor textColor)
Definition: text_input.cpp:76
void scrollToContentRect(KDRect rect, bool allowOverscroll=false)
KDCoordinate x() const
Definition: rect.h:36
void setBackgroundColor(KDColor backgroundColor)
Definition: text_input.cpp:16
virtual bool removeChar()=0
Responder * parentResponder() const
Definition: responder.cpp:12
void markRectAsDirty(KDRect rect)
Definition: view.cpp:39
void setBackgroundColor(KDColor backgroundColor)
Definition: text_input.cpp:72
KDRect unionedWith(const KDRect &other) const
Definition: rect.cpp:71
virtual void scrollToCursor()
Definition: text_input.cpp:86
void setCursorLocation(int cursorLocation)
Definition: text_input.cpp:26
Toolbox * toolbox() override
Definition: text_input.cpp:65
virtual bool removeEndOfLine()
Definition: text_input.cpp:114
bool insertTextAtLocation(const char *textBuffer, int location)
Definition: text_input.cpp:103
ContentView * contentView()
Definition: text_input.h:57
bool removeChar()
Definition: text_input.cpp:80
const char * text() const
Definition: text_input.h:14
KDCoordinate y() const
Definition: rect.h:37
Definition: rect.h:26
Definition: color.h:6
Definition: view.h:23
void reloadRectFromCursorPosition(size_t index, bool lineBreak=false)
Definition: text_input.cpp:49
KDCoordinate width() const
Definition: rect.h:39
void layoutSubviews() override
KDColor backgroundColor() const
Definition: text_input.h:16
ContentView(KDText::FontSize size, KDColor textColor, KDColor backgroundColor)
Definition: text_input.cpp:6
TextInput(Responder *parentResponder, View *contentView)
Definition: text_input.cpp:60
virtual Toolbox * toolboxForTextInput(TextInput *textInput)=0
virtual void layoutSubviews() override
Definition: text_input.cpp:45
FontSize
Definition: text.h:10
KDCoordinate height() const
Definition: rect.h:40
KDRect bounds() const
Definition: view.cpp:157
KDCoordinate bottom() const
Definition: rect.h:44