Numworks Epsilon  1.4.1
Graphing Calculator Operating System
variable_box_controller.cpp
Go to the documentation of this file.
2 #include "script.h"
3 #include "../shared/toolbox_helpers.h"
4 #include <apps/i18n.h>
5 #include <assert.h>
7 #include <escher/palette.h>
8 #include <string.h>
9 
10 namespace Code {
11 
12 /* ContentViewController */
13 
14 VariableBoxController::ContentViewController::ContentViewController(Responder * parentResponder, MenuController * menuController, ScriptStore * scriptStore) :
15  ViewController(parentResponder),
16  m_scriptNodesCount(0),
17  m_menuController(menuController),
18  m_scriptStore(scriptStore),
19  m_selectableTableView(this)
20 {
21  m_selectableTableView.setMargins(0);
22  m_selectableTableView.setShowsIndicators(false);
23  for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
24  m_leafCells[i].setScriptStore(scriptStore);
25  }
26 }
27 
28 void VariableBoxController::ContentViewController::setTextInputCaller(TextInput * textInput) {
29  m_textInputCaller = textInput;
30 }
31 
32 void VariableBoxController::ContentViewController::reloadData() {
33  m_selectableTableView.reloadData();
34 }
35 
36 void VariableBoxController::ContentViewController::addFunctionAtIndex(const char * functionName, int scriptIndex) {
37  m_scriptNodes[m_scriptNodesCount] = ScriptNode::FunctionNode(functionName, scriptIndex);
38  m_scriptNodesCount++;
39 }
40 
41 void VariableBoxController::ContentViewController::addVariableAtIndex(const char * variableName, int scriptIndex) {
42  m_scriptNodes[m_scriptNodesCount] = ScriptNode::VariableNode(variableName, scriptIndex);
43  m_scriptNodesCount++;
44 }
45 
46 const char * VariableBoxController::ContentViewController::title() {
47  return I18n::translate(I18n::Message::FunctionsAndVariables);
48 }
49 
51  m_menuController->loadPythonIfNeeded();
52  m_scriptNodesCount = 0;
53  m_scriptStore->scanScriptsForFunctionsAndVariables(
54  this,
55  [](void * context, const char * functionName, int scriptIndex) {
56  VariableBoxController::ContentViewController * cvc = static_cast<VariableBoxController::ContentViewController *>(context);
57  cvc->addFunctionAtIndex(functionName, scriptIndex);},
58  [](void * context, const char * variableName, int scriptIndex) {
59  VariableBoxController::ContentViewController * cvc = static_cast<VariableBoxController::ContentViewController *>(context);
60  cvc->addVariableAtIndex(variableName, scriptIndex);});
61 }
62 
63 void VariableBoxController::ContentViewController::viewDidDisappear() {
64  m_selectableTableView.deselectTable();
66 }
67 
68 void VariableBoxController::ContentViewController::didBecomeFirstResponder() {
69  m_selectableTableView.reloadData();
70  m_selectableTableView.scrollToCell(0,0);
71  selectCellAtLocation(0, 0);
72  app()->setFirstResponder(&m_selectableTableView);
73 }
74 
75 bool VariableBoxController::ContentViewController::handleEvent(Ion::Events::Event event) {
76  if (event == Ion::Events::Back) {
77  app()->dismissModalViewController();
78  return true;
79  }
80  if (event == Ion::Events::Left) {
81  return true;
82  }
83  if (event == Ion::Events::OK || event == Ion::Events::EXE) {
84  if (m_selectableTableView.selectedRow() < 0 || m_selectableTableView.selectedRow() >= m_scriptNodesCount) {
85  return false;
86  }
87  ScriptNode selectedScriptNode = m_scriptNodes[m_selectableTableView.selectedRow()];
88  insertTextInCaller(selectedScriptNode.name());
89  if (selectedScriptNode.type() == ScriptNode::Type::Function) {
90  insertTextInCaller(ScriptNodeCell::k_parentheses);
91  }
92  m_selectableTableView.deselectTable();
93  app()->dismissModalViewController();
94  return true;
95  }
96  return false;
97 }
98 
100  return m_scriptNodesCount < k_maxScriptNodesCount ? m_scriptNodesCount : k_maxScriptNodesCount;
101 }
102 
103 HighlightCell * VariableBoxController::ContentViewController::reusableCell(int index) {
104  assert(index >= 0 && index < k_maxNumberOfDisplayedRows);
105  return &m_leafCells[index];
106 }
107 
108 int VariableBoxController::ContentViewController::reusableCellCount() {
109  return k_maxNumberOfDisplayedRows;
110 }
111 
112 void VariableBoxController::ContentViewController::willDisplayCellForIndex(HighlightCell * cell, int index) {
113  ScriptNodeCell * myCell = static_cast<ScriptNodeCell *>(cell);
114  myCell->setScriptNode(&m_scriptNodes[index]);
115 }
116 
117 void VariableBoxController::ContentViewController::insertTextInCaller(const char * text) {
118  char commandBuffer[strlen(text)+1];
120  m_textInputCaller->handleEventWithText(commandBuffer);
121 }
122 
124  StackViewController(nullptr, &m_contentViewController, KDColorWhite, Palette::PurpleBright, Palette::PurpleDark),
125  m_contentViewController(this, menuController, scriptStore)
126 {
127 }
128 
130  app()->setFirstResponder(&m_contentViewController);
131 }
132 
134  m_contentViewController.setTextInputCaller(textInput);
135 }
136 
139  m_contentViewController.reloadData();
140 }
141 
144 }
145 
146 }
static ScriptNode FunctionNode(const char *name, uint16_t scriptIndex)
Definition: script_node.h:16
void TextToInsertForCommandText(const char *command, char *buffer)
#define assert(e)
Definition: assert.h:9
constexpr Event EXE
Definition: events.h:114
constexpr Event Back
Definition: events.h:66
enum Message uint16_t enum Language uint16_t const char * translate(Message m, Language l=(Language) 0)
Definition: i18n.cpp:5
void viewDidDisappear() override
Definition: app.cpp:7
constexpr KDColor KDColorWhite
Definition: color.h:42
size_t strlen(const char *s)
Definition: strlen.c:3
virtual void viewWillAppear()
static constexpr char k_parentheses[]
constexpr Event Left
Definition: events.h:61
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
VariableBoxController(MenuController *menuController, ScriptStore *scriptStore)
virtual void viewDidDisappear()
constexpr uint8_t numberOfRows
Definition: keyboard.h:35
static ScriptNode VariableNode(const char *name, uint16_t scriptIndex)
Definition: script_node.h:19
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
void setTextInputCaller(TextInput *textInput)
Definition: palette.h:6