Main Page   Class Hierarchy   File List  

ocControls.h

00001 #ifndef OC_CONTROLS_H
00002 #define OC_CONTROLS_H
00003 #include "ocString.h"
00004 
00005 class ocBase
00006 {
00007 protected:
00008   string content;
00009   string tag;
00010   string attr;
00011   bool container;
00012 
00013 public:
00014   ocBase():container(true){;}
00015   ocBase(string inTag, string attrIn = ""):container(true)
00016   {
00017     tag = inTag;
00018     attr = attrIn;
00019   }
00020   ~ocBase(){;}
00021   string getContent( void ) { return content; }
00022   ocBase & setContent( string inContent )
00023   {
00024     content = inContent;
00025     return *this;
00026   }
00027   virtual string getHtml( void )
00028   {
00029     string result;
00030     if( tag.length() > 0 )
00031     {
00032       result += "<";
00033       result += tag;
00034       if( attr.length() > 0 )
00035       {
00036         result += " ";
00037         result += attr;
00038       }
00039       result += ">";
00040     }
00041     result += content;
00042     if( tag.length() > 0 && container )
00043     {
00044       result += "</";
00045       result += tag;
00046       result += ">";
00047     }
00048     return result;
00049   }
00050 };
00051 
00052 class ocTextbox: public ocBase
00053 {
00054 public:
00055   ocTextbox ( string name,
00056               string size = "",
00057               string max = "",
00058               string attrIn = "" )
00059   {
00060     tag = "input type='text'";
00061     if ( name.length() )
00062     {
00063       attr += " name='";
00064       attr += name;
00065       attr += "'";
00066     }
00067     if ( size.length() )
00068     {
00069       attr += " size='" ;
00070       attr += size ;
00071       attr += "'";
00072     }
00073     if ( max.length() )
00074     {
00075       attr += " maxlength='";
00076       attr += max ;
00077       attr += "'";
00078     }
00079     if ( attrIn.length() )
00080     {
00081       attr += " " ;
00082       attr += attrIn;
00083     }
00084 
00085     // unary tag - not a container
00086     container = false;
00087   }
00088   ~ocTextbox(){;}
00089   string getHtml( void )
00090   {
00091     string result;
00092     if( tag.length() > 0 )
00093     {
00094       result += "<";
00095       result += tag;
00096       if( attr.length() > 0 )
00097       {
00098         result += " ";
00099         result += attr;
00100       }
00101       if( content.length() )
00102       {
00103         result += " value=\"";
00104         result += content;
00105         result += "\"";
00106       }
00107       result += ">";
00108     }
00109     return result;
00110   }
00111 };
00112 
00113 class ocFilebox: public ocBase
00114 {
00115 public:
00116   ocFilebox ( string name,
00117               string size = "",
00118               string max = "",
00119               string attrIn = "" )
00120   {
00121     tag = "input type='file'";
00122     if ( name.length() )
00123     {
00124       attr += " name='";
00125       attr += name;
00126       attr += "'";
00127     }
00128     if ( size.length() )
00129     {
00130       attr += " size='" ;
00131       attr += size ;
00132       attr += "'";
00133     }
00134     if ( max.length() )
00135     {
00136       attr += " maxlength='";
00137       attr += max ;
00138       attr += "'";
00139     }
00140     if ( attrIn.length() )
00141     {
00142       attr += " " ;
00143       attr += attrIn;
00144     }
00145 
00146     // unary tag - not a container
00147     container = false;
00148   }
00149   ~ocFilebox(){;}
00150   string getHtml( void )
00151   {
00152     string result;
00153     if( tag.length() > 0 )
00154     {
00155       result += "<";
00156       result += tag;
00157       if( attr.length() > 0 )
00158       {
00159         result += " ";
00160         result += attr;
00161       }
00162       if( content.length() )
00163       {
00164         result += " value=\"";
00165         result += content;
00166         result += "\"";
00167       }
00168       result += ">";
00169     }
00170     return result;
00171   }
00172 };
00173 
00174 class ocHidden: public ocBase
00175 {
00176 public:
00177   ocHidden ( string name  )
00178   {
00179     tag = "input type='hidden'";
00180     if ( name.length() )
00181     {
00182       attr += " name='";
00183       attr += name;
00184       attr += "'";
00185     }
00186     // unary tag - not a container
00187     container = false;
00188   }
00189   ~ocHidden(){;}
00190   string getHtml( void )
00191   {
00192     string result;
00193     if( tag.length() > 0 )
00194     {
00195       result += "<";
00196       result += tag;
00197       if ( attr.length() )
00198       {
00199         result += attr;
00200       }
00201       if( content.length() )
00202       {
00203         result += " value=\"";
00204         result += content;
00205         result += "\"";
00206       }
00207       result += ">";
00208     }
00209     return result;
00210   }
00211 };
00212 
00213 class ocPassword: public ocBase
00214 {
00215 public:
00216   ocPassword( string name,
00217               string size = "",
00218               string max = "",
00219               string attrIn = "" )
00220   {
00221     tag = "input type='password'";
00222     if ( name.length() )
00223     {
00224       attr += " name='";
00225       attr += name;
00226       attr += "'";
00227     }
00228     if ( size.length() )
00229     {
00230       attr += " size='" ;
00231       attr += size ;
00232       attr += "'";
00233     }
00234     if ( max.length() )
00235     {
00236       attr += " maxlength='";
00237       attr += max ;
00238       attr += "'";
00239     }
00240     if ( attrIn.length() )
00241     {
00242       attr += " " ;
00243       attr += attrIn;
00244     }
00245 
00246     // unary tag - not a container
00247     container = false;
00248   }
00249   ~ocPassword(){;}
00250   string getHtml( void )
00251   {
00252     string result;
00253     if( tag.length() > 0 )
00254     {
00255       result += "<";
00256       result += tag;
00257       if( attr.length() > 0 )
00258       {
00259         result += " ";
00260         result += attr;
00261       }
00262       if( content.length() )
00263       {
00264         result += " value=\"";
00265         result += content;
00266         result += "\"";
00267       }
00268       result += ">";
00269     }
00270     return result;
00271   }
00272 };
00273 
00274 class ocSelect: public ocBase
00275 {
00276 protected:
00277   vector< string > option_labels;
00278   vector< string > option_values;
00279   map< string, string > option_attrs;
00280 
00281 public:
00282   ocSelect(string attr = ""):ocBase( "select", attr )
00283   {
00284     ;
00285   }
00286   void addOption( string label, string value, string attr = "" )
00287   {
00288      option_labels.push_back( label );
00289      option_values.push_back( value );
00290      if( attr.length() )
00291      {
00292        option_attrs.insert( make_pair( label, attr ) );
00293      }
00294   }
00295   ~ocSelect(){;}
00296   string getHtml( void )
00297   {
00298     string result;
00299     result += "<";
00300     result += tag;
00301     if( attr.length() > 0 )
00302     {
00303       result += " ";
00304       result += attr;
00305     }
00306     result += ">";
00307     for( int i=0; i<option_labels.size(); i++ )
00308     {
00309       result += "<option";
00310       string & label =  option_labels[i];
00311       if( option_attrs.find(label) !=  option_attrs.end() )
00312       {
00313         result += " " ;
00314         result += option_attrs[label];
00315       }
00316       if( isSelected(i) )
00317       {
00318         result += " selected ";
00319       }
00320       result += " value=\"" ;
00321       result += option_values[i];
00322       result += "\">" ;
00323       result += label ;
00324       result += "</option>";
00325     } // end for each option
00326     result += "</";
00327     result += tag;
00328     result += ">";
00329     return result;
00330   }
00331   bool isSelected( int iOPtion )
00332   {
00333     bool bRet = false;
00334     if( content.length() )
00335     {
00336       string & option = option_values[iOPtion];
00337       ocString ocContent = content;
00338       while( ocContent.endOfParse() == false )
00339       {
00340         string cVal = ocContent.parse("|");
00341         if( cVal == option )
00342         {
00343           bRet = true;
00344           break;
00345         }
00346       }
00347     }
00348     return bRet;
00349   }
00350 };
00351 
00352 class ocTextarea: public ocBase
00353 {
00354 
00355 public:
00356   ocTextarea(string attr = ""):ocBase( "textarea", attr.length()>0?attr:" rows='10' cols='80'" )
00357   {
00358     ;
00359   }
00360   ~ocTextarea(){;}
00361 };
00362 
00363 class ocBoolbox: public ocBase
00364 {
00365   string svrTrue;
00366 public:
00367   ocBoolbox( string name, string attrIn = "",
00368              string usrTrue="true", string svrTrueIn="Yes")
00369   {
00370     svrTrue = svrTrueIn;
00371     tag = "input type='checkbox'";
00372     if ( name.length() )
00373     {
00374       attr += " name='";
00375       attr += name;
00376       attr += "'";
00377     }
00378     // if it's checked, its true
00379     attr += " value=\"";
00380     attr += usrTrue;
00381     attr += "\" ";
00382 
00383     if ( attrIn.length() )
00384     {
00385       attr += " " ;
00386       attr += attrIn;
00387     }
00388     // unary tag - not a container
00389     container = false;
00390   }
00391   ~ocBoolbox(){;}
00392 
00393   virtual string getHtml( void )
00394   {
00395     string result;
00396     if( tag.length() > 0 )
00397     {
00398       result += "<";
00399       result += tag;
00400       if( attr.length() > 0 )
00401       {
00402         result += " ";
00403         result += attr;
00404       }
00405 
00406       if( content == svrTrue )
00407       {
00408         result += " checked ";
00409       }
00410       result += ">";
00411     }
00412     // result += content;
00413 
00414     return result;
00415   }
00416 };
00417 
00418 #endif

Generated on Tue Jan 20 09:03:27 2004 for OpenTools by doxygen1.2.18