Numworks Epsilon  1.4.1
Graphing Calculator Operating System
input_view_controller.cpp
Go to the documentation of this file.
2 #include <escher/app.h>
3 #include <escher/palette.h>
4 #include <assert.h>
5 
6 InputViewController::TextFieldController::ContentView::ContentView(Responder * parentResponder, TextFieldDelegate * textFieldDelegate) :
7  Responder(parentResponder),
8  View(),
9  m_textField(this, m_textBody, m_textBody, TextField::maxBufferSize(), textFieldDelegate, false)
10 {
11  m_textBody[0] = 0;
12 }
13 
14 void InputViewController::TextFieldController::ContentView::didBecomeFirstResponder() {
15  app()->setFirstResponder(&m_textField);
16 }
17 
18 TextField * InputViewController::TextFieldController::ContentView::textField() {
19  return &m_textField;
20 }
21 
22 void InputViewController::TextFieldController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
23  ctx->fillRect(KDRect(0, 0, bounds().width(), k_separatorThickness), Palette::GreyMiddle);
24  ctx->fillRect(KDRect(0, k_separatorThickness, k_textMargin, bounds().height()-k_separatorThickness), m_textField.backgroundColor());
25 }
26 
27 KDSize InputViewController::TextFieldController::ContentView::minimalSizeForOptimalDisplay() const {
28  return KDSize(0, k_inputHeight);
29 }
30 
31 int InputViewController::TextFieldController::ContentView::numberOfSubviews() const {
32  return 1;
33 }
34 
35 View * InputViewController::TextFieldController::ContentView::subviewAtIndex(int index) {
36  return &m_textField;
37 }
38 
39 void InputViewController::TextFieldController::ContentView::layoutSubviews() {
40  m_textField.setFrame(KDRect(k_textMargin, k_separatorThickness, bounds().width()-k_textMargin, bounds().height()));
41 }
42 
43 InputViewController::TextFieldController::TextFieldController(Responder * parentResponder, TextFieldDelegate * textFieldDelegate) :
44  ViewController(parentResponder),
45  m_view(this, textFieldDelegate)
46 {
47 }
48 
49 View * InputViewController::TextFieldController::view() {
50  return &m_view;
51 }
52 
53 void InputViewController::TextFieldController::didBecomeFirstResponder() {
54  app()->setFirstResponder(&m_view);
55 }
56 
57 TextField * InputViewController::TextFieldController::textField() {
58  return m_view.textField();
59 }
60 
61 InputViewController::InputViewController(Responder * parentResponder, ViewController * child, TextFieldDelegate * textFieldDelegate) :
62  ModalViewController(parentResponder, child),
63  m_textFieldController(this, this),
64  m_successAction(Invocation(nullptr, nullptr)),
65  m_failureAction(Invocation(nullptr, nullptr)),
66  m_textFieldDelegate(textFieldDelegate)
67 {
68 }
69 
71  return m_textFieldController.textField()->text();
72 }
73 
74 void InputViewController::edit(Responder * caller, Ion::Events::Event event, void * context, const char * initialText, Invocation::Action successAction, Invocation::Action failureAction) {
75  m_successAction = Invocation(successAction, context);
76  m_failureAction = Invocation(failureAction, context);
77  displayModalViewController(&m_textFieldController, 1.0f, 1.0f);
78  m_textFieldController.textField()->handleEvent(event);
79  if (initialText != nullptr) {
80  m_textFieldController.textField()->handleEventWithText(initialText);
81  }
82 }
83 
85  m_textFieldController.textField()->setEditing(false);
87 }
88 
90  return event == Ion::Events::OK || event == Ion::Events::EXE;
91 }
92 
94  m_successAction.perform(this);
96  return true;
97 }
98 
99 bool InputViewController::textFieldDidAbortEditing(TextField * textField, const char * text) {
100  m_failureAction.perform(this);
102  return true;
103 }
104 
106  return m_textFieldDelegate->textFieldDidReceiveEvent(textField, event);
107 }
108 
110  return m_textFieldDelegate->toolboxForTextInput(input);
111 }
void perform(void *sender)
Definition: invocation.cpp:9
void(* Action)(void *context, void *sender)
Definition: invocation.h:6
bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event) override
void edit(Responder *caller, Ion::Events::Event event, void *context, const char *initialText, Invocation::Action successAction, Invocation::Action failureAction)
void setFrame(KDRect frame)
Definition: view.cpp:125
bool textFieldDidAbortEditing(TextField *textField, const char *text) override
constexpr Event EXE
Definition: events.h:114
virtual bool textFieldDidReceiveEvent(TextField *textField, Ion::Events::Event event)=0
Definition: size.h:6
#define false
Definition: stdbool.h:9
static constexpr KDColor GreyMiddle
Definition: palette.h:14
Toolbox * toolboxForTextInput(TextInput *textInput) override
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
void displayModalViewController(ViewController *vc, float verticalAlignment, float horizontalAlignment, KDCoordinate topMargin=0, KDCoordinate leftMargin=0, KDCoordinate bottomMargin=0, KDCoordinate rightMargin=0)
Definition: view.h:23
bool textFieldDidFinishEditing(TextField *textField, const char *text, Ion::Events::Event event) override
InputViewController(Responder *parentResponder, ViewController *child, TextFieldDelegate *textFieldDelegate)
constexpr Event OK
Definition: events.h:65
virtual Toolbox * toolboxForTextInput(TextInput *textInput)=0
bool textFieldDidReceiveEvent(TextField *textField, Ion::Events::Event event) override