ArduinoLibs
|
00001 /* 00002 * Copyright (C) 2012 Southern Storm Software, Pty Ltd. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00020 * DEALINGS IN THE SOFTWARE. 00021 */ 00022 00023 #include "Form.h" 00024 #include "Field.h" 00025 00047 Form::Form(LiquidCrystal &lcd) 00048 : _lcd(&lcd) 00049 , first(0) 00050 , last(0) 00051 , current(0) 00052 { 00053 } 00054 00058 Form::~Form() 00059 { 00060 Field *field = first; 00061 Field *next; 00062 while (field != 0) { 00063 next = field->next; 00064 field->_form = 0; 00065 field->next = 0; 00066 field->prev = 0; 00067 field = next; 00068 } 00069 } 00070 00099 int Form::dispatch(int event) 00100 { 00101 if (current) { 00102 int exitval = current->dispatch(event); 00103 if (exitval >= 0) 00104 return exitval; 00105 } 00106 if (event == LCD_BUTTON_LEFT) 00107 prevField(); 00108 else if (event == LCD_BUTTON_RIGHT) 00109 nextField(); 00110 return 0; 00111 } 00112 00118 void Form::nextField() 00119 { 00120 Field *field = current; 00121 if (!field) 00122 field = first; 00123 if (field && field->next) 00124 field = field->next; 00125 else 00126 field = first; 00127 setCurrentField(field); 00128 } 00129 00135 void Form::prevField() 00136 { 00137 Field *field = current; 00138 if (!field) 00139 field = last; 00140 if (field && field->prev) 00141 field = field->prev; 00142 else 00143 field = last; 00144 setCurrentField(field); 00145 } 00146 00152 void Form::defaultField() 00153 { 00154 setCurrentField(first); 00155 } 00156 00165 void Form::addField(Field *field) 00166 { 00167 if (field->_form) 00168 return; // Already added to a form. 00169 field->_form = this; 00170 field->next = 0; 00171 field->prev = last; 00172 if (last) 00173 last->next = field; 00174 else 00175 first = field; 00176 last = field; 00177 } 00178 00187 void Form::removeField(Field *field) 00188 { 00189 if (field->_form != this) 00190 return; // Not a member of this form. 00191 if (current == field) { 00192 if (field->next) 00193 setCurrentField(field->next); 00194 else if (field->prev) 00195 setCurrentField(field->prev); 00196 else 00197 setCurrentField(0); 00198 } 00199 if (field->next) 00200 field->next->prev = field->prev; 00201 else 00202 last = field->prev; 00203 if (field->prev) 00204 field->prev->next = field->next; 00205 else 00206 first = field->next; 00207 field->_form = 0; 00208 field->next = 0; 00209 field->prev = 0; 00210 } 00211 00230 void Form::setCurrentField(Field *field) 00231 { 00232 if (field && field->_form != this) 00233 return; // Wrong form. 00234 if (visible) { 00235 bool reverse = false; 00236 if (current) { 00237 current->exitField(); 00238 if (field->next == current) 00239 reverse = true; 00240 else if (!field->next && current == first) 00241 reverse = true; 00242 } 00243 current = field; 00244 _lcd->clear(); 00245 if (current) 00246 current->enterField(reverse); 00247 } else { 00248 current = field; 00249 } 00250 } 00251 00274 void Form::show() 00275 { 00276 if (!visible) { 00277 if (!current) 00278 current = first; 00279 visible = true; 00280 _lcd->clear(); 00281 if (current) 00282 current->enterField(false); 00283 } 00284 } 00285 00293 void Form::hide() 00294 { 00295 if (visible) { 00296 if (current) 00297 current->exitField(); 00298 visible = false; 00299 _lcd->clear(); 00300 } 00301 } 00302