ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Form.cpp
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "Form.h"
24 #include "Field.h"
25 
47 Form::Form(LiquidCrystal &lcd)
48  : _lcd(&lcd)
49  , first(0)
50  , last(0)
51  , current(0)
52 {
53 }
54 
59 {
60  Field *field = first;
61  Field *next;
62  while (field != 0) {
63  next = field->next;
64  field->_form = 0;
65  field->next = 0;
66  field->prev = 0;
67  field = next;
68  }
69 }
70 
99 int Form::dispatch(int event)
100 {
101  if (current) {
102  int exitval = current->dispatch(event);
103  if (exitval >= 0)
104  return exitval;
105  }
106  if (event == LCD_BUTTON_LEFT)
107  prevField();
108  else if (event == LCD_BUTTON_RIGHT)
109  nextField();
110  return 0;
111 }
112 
119 {
120  Field *field = current;
121  if (!field)
122  field = first;
123  if (field && field->next)
124  field = field->next;
125  else
126  field = first;
127  setCurrentField(field);
128 }
129 
136 {
137  Field *field = current;
138  if (!field)
139  field = last;
140  if (field && field->prev)
141  field = field->prev;
142  else
143  field = last;
144  setCurrentField(field);
145 }
146 
153 {
154  setCurrentField(first);
155 }
156 
165 void Form::addField(Field *field)
166 {
167  if (field->_form)
168  return; // Already added to a form.
169  field->_form = this;
170  field->next = 0;
171  field->prev = last;
172  if (last)
173  last->next = field;
174  else
175  first = field;
176  last = field;
177 }
178 
188 {
189  if (field->_form != this)
190  return; // Not a member of this form.
191  if (current == field) {
192  if (field->next)
193  setCurrentField(field->next);
194  else if (field->prev)
195  setCurrentField(field->prev);
196  else
197  setCurrentField(0);
198  }
199  if (field->next)
200  field->next->prev = field->prev;
201  else
202  last = field->prev;
203  if (field->prev)
204  field->prev->next = field->next;
205  else
206  first = field->next;
207  field->_form = 0;
208  field->next = 0;
209  field->prev = 0;
210 }
211 
231 {
232  if (field && field->_form != this)
233  return; // Wrong form.
234  if (visible) {
235  bool reverse = false;
236  if (current) {
237  current->exitField();
238  if (field->next == current)
239  reverse = true;
240  else if (!field->next && current == first)
241  reverse = true;
242  }
243  current = field;
244  _lcd->clear();
245  if (current)
246  current->enterField(reverse);
247  } else {
248  current = field;
249  }
250 }
251 
275 {
276  if (!visible) {
277  if (!current)
278  current = first;
279  visible = true;
280  _lcd->clear();
281  if (current)
282  current->enterField(false);
283  }
284 }
285 
294 {
295  if (visible) {
296  if (current)
297  current->exitField();
298  visible = false;
299  _lcd->clear();
300  }
301 }
302 
Form(LiquidCrystal &lcd)
Constructs a new form and associates it with lcd.
Definition: Form.cpp:47
Manages a single data input/output field within a Form.
Definition: Field.h:28
int dispatch(int event)
Dispatches event to the currently active field using Field::dispatch().
Definition: Form.cpp:99
void nextField()
Changes to the next field in the "tab order".
Definition: Form.cpp:118
virtual void enterField(bool reverse)
Enters the field due to form navigation.
Definition: Field.cpp:116
virtual int dispatch(int event)
Dispatches event via this field.
Definition: Field.cpp:96
void addField(Field *field)
Adds field to this form.
Definition: Form.cpp:165
void hide()
Hides the form, or does nothing if the form is not on-screen.
Definition: Form.cpp:293
virtual void exitField()
Exits the field due to form navigation.
Definition: Field.cpp:129
void removeField(Field *field)
Removes field from this form.
Definition: Form.cpp:187
void setCurrentField(Field *field)
Sets the current field that is displayed on-screen.
Definition: Form.cpp:230
~Form()
Detaches all remaining fields and destroys this form.
Definition: Form.cpp:58
void show()
Shows the form, or does nothing if the form is already on-screen.
Definition: Form.cpp:274
void defaultField()
Changes to default field (i.e., the first field).
Definition: Form.cpp:152
void prevField()
Changes to the previous field in the "tab order".
Definition: Form.cpp:135