00001 #ifndef OC_CONTROLS_H
00002 #define OC_CONTROLS_H
00003 #include <vector>
00004 #include "ocString.h"
00005
00006 class ocBase
00007 {
00008 protected:
00009 string content;
00010 string tag;
00011 string attr;
00012 bool container;
00013 ocBase * next;
00014
00015 public:
00016 ocBase():container(true),next(NULL){;}
00017 ocBase(string inTag, string attrIn = ""):container(true),next(NULL)
00018 {
00019 tag = inTag;
00020 attr = attrIn;
00021 }
00022
00023
00024 ocBase * addNext(ocBase * newnext)
00025 {
00026 ocBase ** next = &(this->next);
00027 while( *next )
00028 {
00029 next = &((*next)->next);
00030 }
00031
00032 *next = newnext;
00033 return this;
00034 }
00035 ocBase * getNext( void )
00036 {
00037 return next;
00038 }
00039 virtual ~ocBase(){
00040 erase(next);
00041 }
00042
00043 void erase(ocBase *toErase)
00044 {
00045 if( toErase )
00046 {
00047 if( toErase->next ) toErase->erase(toErase->next);
00048 delete next;
00049 next=NULL;
00050 }
00051 }
00052
00053 string getContent( void ) { return content; }
00054 ocBase & setContent( string inContent )
00055 {
00056 content = inContent;
00057 return *this;
00058 }
00059 virtual string getHtml( void )
00060 {
00061 string result;
00062 if( tag.length() > 0 )
00063 {
00064 result += "<";
00065 result += tag;
00066 if( attr.length() > 0 )
00067 {
00068 result += " ";
00069 result += attr;
00070 }
00071 result += ">";
00072 }
00073 result += content;
00074 if( tag.length() > 0 && container )
00075 {
00076 result += "</";
00077 result += tag;
00078 result += ">";
00079 }
00080 return result;
00081 }
00082 };
00083
00084 class ocTextbox: public ocBase
00085 {
00086 public:
00087 ocTextbox ( string name,
00088 string size = "",
00089 string max = "",
00090 string attrIn = "" ):ocBase()
00091 {
00092 tag = "input type='text'";
00093 if ( name.length() )
00094 {
00095 attr += " name='";
00096 attr += name;
00097 attr += "'";
00098 }
00099 if ( size.length() )
00100 {
00101 attr += " size='" ;
00102 attr += size ;
00103 attr += "'";
00104 }
00105 if ( max.length() )
00106 {
00107 attr += " maxlength='";
00108 attr += max ;
00109 attr += "'";
00110 }
00111 if ( attrIn.length() )
00112 {
00113 attr += " " ;
00114 attr += attrIn;
00115 }
00116
00117
00118 container = false;
00119 }
00120 virtual ~ocTextbox(){;}
00121 string getHtml( void )
00122 {
00123 string result;
00124 if( tag.length() > 0 )
00125 {
00126 result += "<";
00127 result += tag;
00128 if( attr.length() > 0 )
00129 {
00130 result += " ";
00131 result += attr;
00132 }
00133 if( content.length() )
00134 {
00135 result += " value=\"";
00136 result += content;
00137 result += "\"";
00138 }
00139 result += ">";
00140 }
00141 return result;
00142 }
00143 };
00144
00145 class ocFilebox: public ocBase
00146 {
00147 public:
00148 ocFilebox ( string name,
00149 string size = "",
00150 string max = "",
00151 string attrIn = "" ):ocBase()
00152 {
00153 tag = "input type='file'";
00154 if ( name.length() )
00155 {
00156 attr += " name='";
00157 attr += name;
00158 attr += "'";
00159 }
00160 if ( size.length() )
00161 {
00162 attr += " size='" ;
00163 attr += size ;
00164 attr += "'";
00165 }
00166 if ( max.length() )
00167 {
00168 attr += " maxlength='";
00169 attr += max ;
00170 attr += "'";
00171 }
00172 if ( attrIn.length() )
00173 {
00174 attr += " " ;
00175 attr += attrIn;
00176 }
00177
00178
00179 container = false;
00180 }
00181 virtual ~ocFilebox(){;}
00182 string getHtml( void )
00183 {
00184 string result;
00185 if( tag.length() > 0 )
00186 {
00187 result += "<";
00188 result += tag;
00189 if( attr.length() > 0 )
00190 {
00191 result += " ";
00192 result += attr;
00193 }
00194 if( content.length() )
00195 {
00196 result += " value=\"";
00197 result += content;
00198 result += "\"";
00199 }
00200 result += ">";
00201 }
00202 return result;
00203 }
00204 };
00205
00206 class ocHidden: public ocBase
00207 {
00208 public:
00209 ocHidden ( string name ):ocBase()
00210 {
00211 tag = "input type='hidden'";
00212 if ( name.length() )
00213 {
00214 attr += " name='";
00215 attr += name;
00216 attr += "' id='";
00217 attr += name;
00218 attr += "'";
00219 }
00220
00221 container = false;
00222 }
00223 virtual ~ocHidden(){;}
00224 string getHtml( void )
00225 {
00226 string result;
00227 if( tag.length() > 0 )
00228 {
00229 result += "<";
00230 result += tag;
00231 if ( attr.length() )
00232 {
00233 result += attr;
00234 }
00235 if( content.length() )
00236 {
00237 result += " value=\"";
00238 result += content;
00239 result += "\"";
00240 }
00241 result += ">";
00242 }
00243 return result;
00244 }
00245 };
00246
00247 class ocPassword: public ocBase
00248 {
00249 public:
00250 ocPassword( string name,
00251 string size = "",
00252 string max = "",
00253 string attrIn = "" ):ocBase()
00254 {
00255 tag = "input type='password'";
00256 if ( name.length() )
00257 {
00258 attr += " name='";
00259 attr += name;
00260 attr += "'";
00261 }
00262 if ( size.length() )
00263 {
00264 attr += " size='" ;
00265 attr += size ;
00266 attr += "'";
00267 }
00268 if ( max.length() )
00269 {
00270 attr += " maxlength='";
00271 attr += max ;
00272 attr += "'";
00273 }
00274 if ( attrIn.length() )
00275 {
00276 attr += " " ;
00277 attr += attrIn;
00278 }
00279
00280
00281 container = false;
00282 }
00283 virtual ~ocPassword(){;}
00284 string getHtml( void )
00285 {
00286 string result;
00287 if( tag.length() > 0 )
00288 {
00289 result += "<";
00290 result += tag;
00291 if( attr.length() > 0 )
00292 {
00293 result += " ";
00294 result += attr;
00295 }
00296 if( content.length() )
00297 {
00298 result += " value=\"";
00299 result += content;
00300 result += "\"";
00301 }
00302 result += ">";
00303 }
00304 return result;
00305 }
00306 };
00307
00308 class ocSelect: public ocBase
00309 {
00310 protected:
00311 vector< string > option_labels;
00312 vector< string > option_values;
00313 map< string, string > option_attrs;
00314
00315 public:
00316 ocSelect(string attr = ""):ocBase( "select", attr )
00317 {
00318 ;
00319 }
00320 void addOption( string label, string value, string attr = "" )
00321 {
00322 option_labels.push_back( label );
00323 option_values.push_back( value );
00324 if( attr.length() )
00325 {
00326 option_attrs.insert( make_pair( label, attr ) );
00327 }
00328 }
00329 virtual ~ocSelect(){;}
00330 string getHtml( void )
00331 {
00332 string result;
00333 result += "<";
00334 result += tag;
00335 if( attr.length() > 0 )
00336 {
00337 result += " ";
00338 result += attr;
00339 }
00340 result += ">";
00341 for( int i=0; i<option_labels.size(); i++ )
00342 {
00343 result += "<option";
00344 string & label = option_labels[i];
00345 if( option_attrs.find(label) != option_attrs.end() )
00346 {
00347 result += " " ;
00348 result += option_attrs[label];
00349 }
00350 if( isSelected(i) )
00351 {
00352 result += " selected ";
00353 }
00354 result += " value=\"" ;
00355 result += option_values[i];
00356 result += "\">" ;
00357 result += label ;
00358 result += "</option>";
00359 }
00360 result += "</";
00361 result += tag;
00362 result += ">";
00363 return result;
00364 }
00365 bool isSelected( int iOPtion )
00366 {
00367 bool bRet = false;
00368 if( content.length() )
00369 {
00370 string & option = option_values[iOPtion];
00371 ocString ocContent = content;
00372 while( ocContent.endOfParse() == false )
00373 {
00374 string cVal = ocContent.parse("|");
00375 if( cVal == option )
00376 {
00377 bRet = true;
00378 break;
00379 }
00380 }
00381 }
00382 return bRet;
00383 }
00384 };
00385
00386
00387
00388
00389 class ocRadio: public ocBase
00390 {
00391 protected:
00392 vector< string > option_labels;
00393 vector< string > option_values;
00394 map< string, string > option_attrs;
00395 string name;
00396
00397 public:
00398 ocRadio(string name, string attr = ""):ocBase( "div", attr ),name(name)
00399 {
00400 ;
00401 }
00402 void addOption( string label, string value, string attr = "" )
00403 {
00404 option_labels.push_back( label );
00405 option_values.push_back( value );
00406 if( attr.length() )
00407 {
00408 option_attrs.insert( make_pair( label, attr ) );
00409 }
00410 }
00411 virtual ~ocRadio(){;}
00412 string getHtml( void )
00413 {
00414 string result;
00415 result += "<";
00416 result += tag;
00417 if( attr.length() > 0 )
00418 {
00419 result += " ";
00420 result += attr;
00421 }
00422
00423 result += " id='";
00424 result += name;
00425 result += "'>";
00426 for( int i=0; i<option_labels.size(); i++ )
00427 {
00428 result += "<input type='radio' name='";
00429 result += name;
00430 result += "'";
00431 string & label = option_labels[i];
00432 if( option_attrs.find(label) != option_attrs.end() )
00433 {
00434 result += " " ;
00435 result += option_attrs[label];
00436 }
00437 if( isSelected(i) )
00438 {
00439 result += " checked='true' ";
00440 }
00441 result += " value=\"" ;
00442 result += option_values[i];
00443 result += "\"> " ;
00444 result += label ;
00445 result += " ";
00446 }
00447 result += "</";
00448 result += tag;
00449 result += ">";
00450 return result;
00451 }
00452
00453 bool isSelected( int iOPtion )
00454 {
00455 bool bRet = false;
00456 if( content.length() )
00457 {
00458 string & option = option_values[iOPtion];
00459 ocString ocContent = content;
00460 while( ocContent.endOfParse() == false )
00461 {
00462 string cVal = ocContent.parse("|");
00463 if( cVal == option )
00464 {
00465 bRet = true;
00466 break;
00467 }
00468 }
00469 }
00470 return bRet;
00471 }
00472 };
00473
00474
00475
00476
00477 class ocCheckBoxes: public ocBase
00478 {
00479 protected:
00480 vector< string > option_labels;
00481 vector< string > option_values;
00482 map< string, string > option_attrs;
00483 string name;
00484 public:
00485 ocCheckBoxes(string name, string attr = ""):ocBase( "div", attr ),name(name)
00486 {
00487 ;
00488 }
00489 void addOption( string label, string value, string attr = "" )
00490 {
00491 option_labels.push_back( label );
00492 option_values.push_back( value );
00493 if( attr.length() )
00494 {
00495 option_attrs.insert( make_pair( label, attr ) );
00496 }
00497 }
00498 virtual ~ocCheckBoxes(){;}
00499 string getHtml( void )
00500 {
00501 string result;
00502 result += "<";
00503 result += tag;
00504 if( attr.length() > 0 )
00505 {
00506 result += " ";
00507 result += attr;
00508 }
00509 result += ">";
00510 for( int i=0; i<option_labels.size(); i++ )
00511 {
00512 result += "<input type='checkbox' name='";
00513 result += name;
00514 result += "'";
00515 string & label = option_labels[i];
00516 if( option_attrs.find(label) != option_attrs.end() )
00517 {
00518 result += " " ;
00519 result += option_attrs[label];
00520 }
00521 if( isSelected(i) )
00522 {
00523 result += " checked='true' ";
00524 }
00525 result += " value=\"" ;
00526 result += option_values[i];
00527 result += "\"> " ;
00528 result += label ;
00529 result += " ";
00530 }
00531 result += "</";
00532 result += tag;
00533 result += ">";
00534 return result;
00535 }
00536 bool isSelected( int iOPtion )
00537 {
00538 bool bRet = false;
00539 if( content.length() )
00540 {
00541 string & option = option_values[iOPtion];
00542 ocString ocContent = content;
00543 while( ocContent.endOfParse() == false )
00544 {
00545 string cVal = ocContent.parse("|");
00546 if( cVal == option )
00547 {
00548 bRet = true;
00549 break;
00550 }
00551 }
00552 }
00553 return bRet;
00554 }
00555 };
00556
00557
00558 class ocTextarea: public ocBase
00559 {
00560
00561 public:
00562 ocTextarea(string attr = ""):ocBase( "textarea", attr.length()>0?attr:" rows='10' cols='80'" )
00563 {
00564 ;
00565 }
00566 virtual ~ocTextarea(){;}
00567 };
00568
00569 class ocBoolbox: public ocBase
00570 {
00571 string svrTrue;
00572 public:
00573 ocBoolbox( string name, string attrIn = "",
00574 string usrTrue="true", string svrTrueIn="Yes"):ocBase()
00575 {
00576 svrTrue = svrTrueIn;
00577 tag = "input type='checkbox'";
00578 if ( name.length() )
00579 {
00580 attr += " name='";
00581 attr += name;
00582 attr += "'";
00583 }
00584
00585 attr += " value=\"";
00586 attr += usrTrue;
00587 attr += "\" ";
00588
00589 if ( attrIn.length() )
00590 {
00591 attr += " " ;
00592 attr += attrIn;
00593 }
00594
00595 container = false;
00596 }
00597 virtual ~ocBoolbox(){;}
00598
00599 virtual string getHtml( void )
00600 {
00601 string result;
00602 if( tag.length() > 0 )
00603 {
00604 result += "<";
00605 result += tag;
00606 if( attr.length() > 0 )
00607 {
00608 result += " ";
00609 result += attr;
00610 }
00611
00612 if( content == svrTrue )
00613 {
00614 result += " checked ";
00615 }
00616 result += ">";
00617 }
00618
00619
00620 return result;
00621 }
00622 };
00623
00624 #endif