Numworks Epsilon  1.4.1
Graphing Calculator Operating System
toolbox.cpp
Go to the documentation of this file.
1 #include <escher/toolbox.h>
2 #include <escher/metric.h>
3 #include <assert.h>
4 #include <string.h>
5 
6 /* State */
7 
8 Toolbox::Stack::State::State(int selectedRow, KDCoordinate verticalScroll) :
9  m_selectedRow(selectedRow),
10  m_verticalScroll(verticalScroll)
11 {
12 }
13 
15  if (m_selectedRow == -1) {
16  return true;
17  }
18  return false;
19 }
20 
21 /* Stack */
22 
23 void Toolbox::Stack::push(int selectedRow, KDCoordinate verticalScroll) {
24  int i = 0;
25  while (!m_statesStack[i].isNull() && i < k_maxModelTreeDepth) {
26  i++;
27  }
28  assert(m_statesStack[i].isNull());
29  m_statesStack[i] = State(selectedRow, verticalScroll);
30 }
31 
33  return &m_statesStack[index];
34 }
35 
37  int depth = 0;
38  for (int i = 0; i < k_maxModelTreeDepth; i++) {
39  depth += (!m_statesStack[i].isNull());
40  }
41  return depth;
42 }
43 
45  int stackDepth = depth();
46  if (stackDepth == 0) {
47  return;
48  }
49  m_statesStack[stackDepth-1] = State();
50 }
51 
53  for (int i = 0; i < k_maxModelTreeDepth; i++) {
54  m_statesStack[i] = State();
55  }
56 }
57 
58 /* List Controller */
59 
62  m_selectableTableView(tableView),
63  m_firstSelectedRow(0),
64  m_title(title)
65 {
66 }
67 
69  return m_title;
70 }
71 
73  return m_selectableTableView;
74 }
75 
78  m_selectableTableView->selectCellAtLocation(0, m_firstSelectedRow);
80 }
81 
83  m_firstSelectedRow = firstSelectedRow;
84 }
85 
86 /* Toolbox */
87 
92  m_messageTreeModel(nullptr),
93  m_sender(nullptr)
94 {
97 }
98 
99 void Toolbox::setSender(Responder * sender) {
100  m_sender = sender;
101 }
102 
104  return handleEventForRow(event, selectedRow());
105 }
106 
109 }
110 
117 }
118 
122 }
123 
126 }
127 
129  if (m_messageTreeModel == nullptr) {
131  }
133 }
134 
135 HighlightCell * Toolbox::reusableCell(int index, int type) {
136  assert(type < 2);
137  assert(index >= 0);
138  assert(index < maxNumberOfDisplayedRows());
139  if (type == LeafCellType) {
140  return leafCellAtIndex(index);
141  }
142  return nodeCellAtIndex(index);
143 }
144 
146  return maxNumberOfDisplayedRows();
147 }
148 
151  if (messageTree->numberOfChildren() == 0) {
153  myCell->setMessage(messageTree->label());
154  myCell->setAccessoryMessage(messageTree->text());
156  return;
157  }
158  MessageTableCell * myCell = (MessageTableCell *)cell;
159  myCell->setMessage(messageTree->label());
160 }
161 
163  int result = 0;
164  for (int k = 0; k < j; k++) {
165  result += rowHeight(k);
166  }
167  return result;
168 }
169 
171  int result = 0;
172  int j = 0;
173  while (result < offsetY && j < numberOfRows()) {
174  result += rowHeight(j++);
175  }
176  return (result < offsetY || offsetY == 0) ? j : j - 1;
177 }
178 
179 int Toolbox::typeAtLocation(int i, int j) {
180  MessageTree * messageTree = (MessageTree *)m_messageTreeModel->children(j);
181  if (messageTree->numberOfChildren() == 0) {
182  return LeafCellType;
183  }
184  return NodeCellType;
185 }
186 
188  return m_stack.depth();
189 }
190 
191 bool Toolbox::handleEventForRow(Ion::Events::Event event, int selectedRow) {
192  int depth = m_stack.depth();
193  if ((event == Ion::Events::Back || event == Ion::Events::Left) && depth > 0) {
194  return returnToPreviousMenu();
195  }
197  if ((event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right) && selectedMessageTree->numberOfChildren() > 0) {
198  return selectSubMenu(selectedMessageTree);
199  }
200  if ((event == Ion::Events::OK || event == Ion::Events::EXE) && selectedMessageTree->numberOfChildren() == 0) {
201  return selectLeaf(selectedMessageTree);
202  }
203  return false;
204 }
205 
206 bool Toolbox::selectSubMenu(ToolboxMessageTree * selectedMessageTree) {
209  m_messageTreeModel = selectedMessageTree;
212  return true;
213 }
214 
216  int currentDepth = m_stack.depth();
217  int index = 0;
218  // We want to get the current ToolboxMessageTree's parent ToolboxMessageTree,
219  // but there is no ToolboxMessageTree::getParent() method. We thus have to
220  // start from the root ToolboxMessageTree and sequentially get the selected
221  // child until it has the wanted depth.
222  ToolboxMessageTree * parentMessageTree = (ToolboxMessageTree *)rootModel();
223  Stack::State * previousState = m_stack.stateAtIndex(index++);;
224  while (currentDepth-- > 1) {
225  parentMessageTree = (ToolboxMessageTree *)parentMessageTree->children(previousState->selectedRow());
226  previousState = m_stack.stateAtIndex(index++);
227  }
229  m_messageTreeModel = parentMessageTree;
232  m_selectableTableView.setContentOffset(KDPoint(scroll.x(), previousState->verticalScroll()));
233  m_stack.pop();
235  return true;
236 }
237 
239  return m_sender;
240 }
virtual MessageTableCellWithChevron * nodeCellAtIndex(int index)=0
void setShowsIndicators(bool s)
Definition: scroll_view.h:28
KDCoordinate y() const
Definition: point.h:11
virtual int maxNumberOfDisplayedRows()=0
bool handleEventForRow(Ion::Events::Event event, int selectedRow)
Definition: toolbox.cpp:191
#define assert(e)
Definition: assert.h:9
ListController m_listController
Definition: toolbox.h:83
int numberOfRows() override
Definition: toolbox.cpp:128
int typeAtLocation(int i, int j) override
Definition: toolbox.cpp:179
SelectableTableView m_selectableTableView
Definition: toolbox.h:81
void setAccessoryMessage(I18n::Message textBody)
constexpr Event EXE
Definition: events.h:114
int16_t KDCoordinate
Definition: coordinate.h:6
void willDisplayCellForIndex(HighlightCell *cell, int index) override
Definition: toolbox.cpp:149
Responder * parentResponder() const
Definition: responder.cpp:12
static constexpr KDColor GreyDark
Definition: palette.h:15
int indexFromCumulatedHeight(KDCoordinate offsetY) override
Definition: toolbox.cpp:170
bool handleEvent(Ion::Events::Event event) override
Definition: toolbox.cpp:103
void didBecomeFirstResponder() override
Definition: toolbox.cpp:76
bool selectCellAtLocation(int i, int j, bool setFirstResponder=true)
State(int selectedRow=-1, KDCoordinate verticalScroll=0)
Definition: toolbox.cpp:8
void resetStack()
Definition: toolbox.cpp:52
bool selectSubMenu(ToolboxMessageTree *selectedMessageTree)
Definition: toolbox.cpp:206
const char * title() override
int numberOfChildren() const
Definition: message_tree.cpp:3
void setFirstSelectedRow(int firstSelectedRow)
Definition: toolbox.cpp:82
constexpr Event Back
Definition: events.h:66
Definition: point.h:6
int reusableCellCount(int type) override
Definition: toolbox.cpp:145
void reloadData(bool setFirstResponder=true)
Toolbox(Responder *parentResponder, const char *title=0)
Definition: toolbox.cpp:88
void viewDidDisappear() override
Stack m_stack
Definition: toolbox.h:82
constexpr KDColor KDColorWhite
Definition: color.h:42
virtual bool selectLeaf(ToolboxMessageTree *selectedMessageTree)=0
I18n::Message label() const
Definition: message_tree.cpp:7
virtual Responder * sender()
Definition: toolbox.cpp:238
KDCoordinate verticalScroll()
Definition: toolbox.h:41
bool returnToPreviousMenu()
Definition: toolbox.cpp:215
void didBecomeFirstResponder() override
Definition: toolbox.cpp:107
void viewWillAppear() override
Definition: toolbox.cpp:111
ToolboxMessageTree * m_messageTreeModel
Definition: toolbox.h:84
View * view() override
Definition: toolbox.cpp:72
void setSender(Responder *sender)
Definition: toolbox.cpp:99
KDCoordinate cumulatedHeightFromIndex(int j) override
Definition: toolbox.cpp:162
void setContentOffset(KDPoint offset, bool forceRelayout=false)
static constexpr int LeafCellType
Definition: toolbox.h:69
int stackDepth()
Definition: toolbox.cpp:187
constexpr Event Left
Definition: events.h:61
const char * title() override
Definition: toolbox.cpp:68
void setMargins(KDCoordinate top, KDCoordinate right, KDCoordinate bottom, KDCoordinate left)
Definition: scroll_view.h:22
virtual const ToolboxMessageTree * rootModel()=0
void setMessage(I18n::Message message)
static constexpr int NodeCellType
Definition: toolbox.h:70
HighlightCell * reusableCell(int index, int type) override
Definition: toolbox.cpp:135
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
KDPoint contentOffset() const
Definition: scroll_view.h:41
Definition: view.h:23
static constexpr KDCoordinate ToolboxRowHeight
Definition: metric.h:27
constexpr Event Right
Definition: events.h:64
I18n::Message text() const
void push(int selectedRow, KDCoordinate verticalScroll)
Definition: toolbox.cpp:23
KDCoordinate x() const
Definition: point.h:10
const MessageTree * children(int index) const override
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
virtual KDCoordinate rowHeight(int j) override
Definition: toolbox.cpp:124
ListController(Responder *parentResponder, SelectableTableView *tableView, const char *title)
Definition: toolbox.cpp:60
virtual MessageTableCellWithMessage * leafCellAtIndex(int index)=0
void pop()
Definition: toolbox.cpp:44
void viewDidDisappear() override
Definition: toolbox.cpp:119
State * stateAtIndex(int index)
Definition: toolbox.cpp:32
Definition: palette.h:6