Numworks Epsilon  1.4.1
Graphing Calculator Operating System
console_controller.h
Go to the documentation of this file.
1 #ifndef CODE_CONSOLE_CONTROLLER_H
2 #define CODE_CONSOLE_CONTROLLER_H
3 
4 #include <escher.h>
5 #include <python/port/port.h>
6 
7 #include "console_edit_cell.h"
8 #include "console_line_cell.h"
9 #include "console_store.h"
10 #include "sandbox_controller.h"
11 #include "script_store.h"
12 
13 namespace Code {
14 
16 public:
18 
20 #if EPSILON_GETOPT
21  , bool m_lockOnConsole
22 #endif
23  );
25  ConsoleController(const ConsoleController& other) = delete;
26  ConsoleController(ConsoleController&& other) = delete;
27  ConsoleController operator=(const ConsoleController& other) = delete;
29 
30  bool loadPythonEnvironment(bool autoImportScripts = true);
33 
34  void autoImport();
35  void autoImportScript(Script script, bool force = false);
36  void runAndPrintForCommand(const char * command);
37 
38  // ViewController
39  View * view() override { return &m_selectableTableView; }
40  void viewWillAppear() override;
41  void didBecomeFirstResponder() override;
42  bool handleEvent(Ion::Events::Event event) override;
44 
45  // ListViewDataSource
46  int numberOfRows() override;
47  KDCoordinate rowHeight(int j) override;
48  KDCoordinate cumulatedHeightFromIndex(int j) override;
49  int indexFromCumulatedHeight(KDCoordinate offsetY) override;
50  HighlightCell * reusableCell(int index, int type) override;
51  int reusableCellCount(int type) override;
52  int typeAtLocation(int i, int j) override;
53  void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
54 
55  // SelectableTableViewDelegate
56  void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override;
57 
58  // TextFieldDelegate
59  bool textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) override;
60  bool textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) override;
61  bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override;
62  bool textFieldDidAbortEditing(TextField * textField, const char * text) override;
63  Toolbox * toolboxForTextInput(TextInput * textInput) override;
64 
65  // MicroPython::ExecutionEnvironment
66  void displaySandbox() override;
67  void printText(const char * text, size_t length) override;
68  const char * inputText(const char * prompt) override;
69 
70 #if EPSILON_GETOPT
71  bool locked() const {
72  return m_locked;
73  }
74 #endif
75 private:
76  static constexpr const char * k_importCommand1 = "from ";
77  static constexpr const char * k_importCommand2 = " import *";
78  static constexpr size_t k_maxImportCommandSize = 5 + 9 + TextField::maxBufferSize(); // strlen(k_importCommand1) + strlen(k_importCommand2) + TextField::maxBufferSize()
79  bool inputRunLoopActive() { return m_inputRunLoopActive; }
80  void askInputRunLoopTermination() { m_inputRunLoopActive = false; }
81  static constexpr int LineCellType = 0;
82  static constexpr int EditCellType = 1;
83  static constexpr int k_numberOfLineCells = 15; // May change depending on the screen height
84  static constexpr int k_pythonHeapSize = 16384;
85  static constexpr int k_outputAccumulationBufferSize = 100;
86  void flushOutputAccumulationBufferToStore();
87  void appendTextToOutputAccumulationBuffer(const char * text, size_t length);
88  void emptyOutputAccumulationBuffer();
89  size_t firstNewLineCharIndex(const char * text, size_t length);
90  StackViewController * stackViewController();
91  bool copyCurrentLineToClipboard();
92  int m_rowHeight;
93  bool m_importScriptsWhenViewAppears;
94  ConsoleStore m_consoleStore;
95  SelectableTableView m_selectableTableView;
96  ConsoleLineCell m_cells[k_numberOfLineCells];
97  ConsoleEditCell m_editCell;
98  char * m_pythonHeap;
99  char m_outputAccumulationBuffer[k_outputAccumulationBufferSize];
100  /* The Python machine might call printText several times to print a single
101  * string. We thus use m_outputAccumulationBuffer to store and concatenate the
102  * different strings until a new line char appears in the text. When this
103  * happens, or when m_outputAccumulationBuffer is full, we create a new
104  * ConsoleLine in the ConsoleStore and empty m_outputAccumulationBuffer. */
105  ScriptStore * m_scriptStore;
106  SandboxController m_sandboxController;
107  bool m_inputRunLoopActive;
108 #if EPSILON_GETOPT
109  bool m_locked;
110 #endif
111 };
112 }
113 
114 #endif
void tableViewDidChangeSelection(SelectableTableView *t, int previousSelectedCellX, int previousSelectedCellY) override
void willDisplayCellAtLocation(HighlightCell *cell, int i, int j) override
bool textFieldDidReceiveEvent(TextField *textField, Ion::Events::Event event) override
HighlightCell * reusableCell(int index, int type) override
int typeAtLocation(int i, int j) override
KDCoordinate cumulatedHeightFromIndex(int j) override
static constexpr int maxBufferSize()
Definition: text_field.h:23
int16_t KDCoordinate
Definition: coordinate.h:6
Responder * parentResponder() const
Definition: responder.cpp:12
void autoImportScript(Script script, bool force=false)
bool textFieldDidAbortEditing(TextField *textField, const char *text) override
const char * inputText(const char *prompt) override
Toolbox * toolboxForTextInput(TextInput *textInput) override
bool textFieldDidFinishEditing(TextField *textField, const char *text, Ion::Events::Event event) override
void printText(const char *text, size_t length) override
Definition: app.cpp:7
bool handleEvent(Ion::Events::Event event) override
ConsoleController(Responder *parentResponder, ScriptStore *scriptStore)
ViewController::DisplayParameter displayParameter() override
bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event) override
KDCoordinate rowHeight(int j) override
static constexpr KDText::FontSize k_fontSize
void runAndPrintForCommand(const char *command)
Definition: view.h:23
bool loadPythonEnvironment(bool autoImportScripts=true)
FontSize
Definition: text.h:10
ConsoleController operator=(const ConsoleController &other)=delete
int reusableCellCount(int type) override
void didBecomeFirstResponder() override
int indexFromCumulatedHeight(KDCoordinate offsetY) override