ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Field.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 "Field.h"
24 
40 Field::Field(const String &label)
41  : _label(label)
42  , _form(0)
43  , next(0)
44  , prev(0)
45 {
46 }
47 
52 Field::Field(Form &form, const String &label)
53  : _label(label)
54  , _form(0)
55  , next(0)
56  , prev(0)
57 {
58  form.addField(this);
59 }
60 
67 {
68  if (_form)
69  _form->removeField(this);
70 }
71 
96 int Field::dispatch(int event)
97 {
98  // Nothing to do here.
99  return -1;
100 }
101 
116 void Field::enterField(bool reverse)
117 {
118  lcd()->print(_label);
119 }
120 
130 {
131  // Nothing to do here.
132 }
133 
146 void Field::setLabel(const String &label)
147 {
148  if (isCurrent()) {
149  unsigned int prevLen = _label.length();
150  unsigned int newLen = label.length();
151  _label = label;
152  lcd()->setCursor(0, 0);
153  lcd()->print(label);
154  while (newLen++ < prevLen)
155  lcd()->write(' ');
156  updateCursor();
157  } else {
158  _label = label;
159  }
160 }
161 
169 bool Field::isCurrent() const
170 {
171  if (!_form->isVisible())
172  return false;
173  return _form->currentField() == this;
174 }
175 
192 {
193  // Nothing to do here.
194 }
bool isVisible() const
Returns true if the form is shown; false if the form is hidden.
Definition: Form.h:53
Manager for a form containing data input/output fields.
Definition: Form.h:32
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
const String & label() const
Returns the label to display in the first line of this field.
Definition: Field.h:41
LiquidCrystal * lcd() const
Returns the LCD that this field is being drawn on.
Definition: Field.h:47
Field(const String &label)
Constructs a new field with a specific label.
Definition: Field.cpp:40
virtual void exitField()
Exits the field due to form navigation.
Definition: Field.cpp:129
virtual void updateCursor()
Updates the cursor position after the label has been drawn by setLabel().
Definition: Field.cpp:191
void removeField(Field *field)
Removes field from this form.
Definition: Form.cpp:187
void setLabel(const String &label)
Sets the label to display in the first line of this field.
Definition: Field.cpp:146
~Field()
Destroys this field and removes it from its owning Form.
Definition: Field.cpp:66
bool isCurrent() const
Returns true if this field is the currently-displayed field in its owning form; false otherwise...
Definition: Field.cpp:169
Field * currentField() const
Returns the current field that is displayed on-screen.
Definition: Form.h:46