Blame view

Giac_maj/epsilon-giac/escher/src/input_view_controller.cpp 4.07 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <escher/input_view_controller.h>
  #include <escher/app.h>
  #include <escher/palette.h>
  #include <assert.h>
  
  InputViewController::TextFieldController::ContentView::ContentView(Responder * parentResponder, TextFieldDelegate * textFieldDelegate) :
    Responder(parentResponder),
    View(),
    m_textField(this, m_textBody, m_textBody, TextField::maxBufferSize(), textFieldDelegate, false)
  {
    m_textBody[0] = 0;
  }
  
  void InputViewController::TextFieldController::ContentView::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_textField);
  }
  
  TextField * InputViewController::TextFieldController::ContentView::textField() {
    return &m_textField;
  }
  
  void  InputViewController::TextFieldController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
    ctx->fillRect(KDRect(0, 0, bounds().width(), k_separatorThickness), Palette::GreyMiddle);
    ctx->fillRect(KDRect(0, k_separatorThickness, k_textMargin, bounds().height()-k_separatorThickness), m_textField.backgroundColor());
  }
  
  KDSize InputViewController::TextFieldController::ContentView::minimalSizeForOptimalDisplay() const {
    return KDSize(0, k_inputHeight);
  }
  
  int  InputViewController::TextFieldController::ContentView::numberOfSubviews() const {
    return 1;
  }
  
  View * InputViewController::TextFieldController::ContentView::subviewAtIndex(int index) {
    return &m_textField;
  }
  
  void  InputViewController::TextFieldController::ContentView::layoutSubviews() {
    m_textField.setFrame(KDRect(k_textMargin, k_separatorThickness, bounds().width()-k_textMargin, bounds().height()));
  }
  
  InputViewController::TextFieldController::TextFieldController(Responder * parentResponder, TextFieldDelegate * textFieldDelegate) :
    ViewController(parentResponder),
    m_view(this, textFieldDelegate)
  {
  }
  
  View * InputViewController::TextFieldController::view() {
    return &m_view;
  }
  
  void InputViewController::TextFieldController::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_view);
  }
  
  TextField * InputViewController::TextFieldController::textField() {
    return m_view.textField();
  }
  
  InputViewController::InputViewController(Responder * parentResponder, ViewController * child, TextFieldDelegate * textFieldDelegate) :
    ModalViewController(parentResponder, child),
    m_textFieldController(this, this),
    m_successAction(Invocation(nullptr, nullptr)),
    m_failureAction(Invocation(nullptr, nullptr)),
    m_textFieldDelegate(textFieldDelegate)
  {
  }
  
  const char * InputViewController::textBody() {
    return m_textFieldController.textField()->text();
  }
  
  void InputViewController::edit(Responder * caller, Ion::Events::Event event, void * context, const char * initialText, Invocation::Action successAction, Invocation::Action failureAction) {
    m_successAction = Invocation(successAction, context);
    m_failureAction = Invocation(failureAction, context);
    displayModalViewController(&m_textFieldController, 1.0f, 1.0f);
    m_textFieldController.textField()->handleEvent(event);
    if (initialText != nullptr) {
      m_textFieldController.textField()->insertTextAtLocation(initialText, 0);
      m_textFieldController.textField()->setCursorLocation(strlen(initialText));
    }
  }
  
  void InputViewController::abortTextFieldEditionAndDismiss() {
    m_textFieldController.textField()->setEditing(false);
    dismissModalViewController();
  }
  
  bool InputViewController::textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) {
    return event == Ion::Events::OK || event == Ion::Events::EXE;
  }
  
  bool InputViewController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
    m_successAction.perform(this);
    dismissModalViewController();
    return true;
  }
  
  bool InputViewController::textFieldDidAbortEditing(TextField * textField, const char * text) {
    m_failureAction.perform(this);
    dismissModalViewController();
    return true;
  }
  
  bool InputViewController::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) {
    return m_textFieldDelegate->textFieldDidReceiveEvent(textField, event);
  }
  
  Toolbox * InputViewController::toolboxForTextField(TextField * textField) {
    return m_textFieldDelegate->toolboxForTextField(textField);
  }