home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / samples / sample2 / sample2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-29  |  4.2 KB  |  159 lines

  1. #define INCL_OOL_WIN
  2. #define INCL_OOL_DBASE
  3. #include "ool.h"
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. class MyAppWindow: public XFrameWindow
  9. {
  10.    public:
  11.       MyAppWindow( char* );
  12. };
  13.  
  14.  
  15. MyAppWindow :: MyAppWindow( char * f ): XFrameWindow( (ULONG) 0, "Sample2", XFrameWindow::defaultStyle | FRM_TASKLIST )
  16. {
  17.    XRect rect;
  18.    //setup the container
  19.    XContainerControl * c = new XContainerControl(this, rect,0, 0, "8.Helv");
  20.    XContainerInfo info( "", CO_DETAIL | CO_DETAILTITLES );
  21.    c->SetInfo( &info);
  22.  
  23.    SetClient(c);
  24.    XRect re( 100,100,500,400);
  25.    SetSize( &re);
  26.    try
  27.    {
  28.       if(f == NULL) //no filename given, create a new file
  29.       {
  30.          //create the file
  31.          XDBaseFile file( "test.dbf", TRUE, TRUE);
  32.          //add three columns
  33.          file.AddCharacterField( "Name", 30);
  34.          file.AddNumericField( "Number", 20);
  35.          file.AddDateField( "GebDat");
  36.          //save all
  37.          file.SaveHeader( "test.dbf" );
  38.          f = "test.dbf";
  39.       }
  40.  
  41.       XDBaseFile file( (char*) f, TRUE);
  42.       //add a new record
  43.       XDBaseRecord record( &file);
  44.       XString string("Test");
  45.       XDate date;
  46.       date.GetCurrentDate();
  47.       //set the data
  48.       record.SetFieldData( 2, date);
  49.       record.SetFieldData( 0, string);
  50.       record.SetFieldData( 1, (LONG) rand());
  51.       //save it
  52.       record.Save();
  53.  
  54.       int fieldCount = file.GetFieldCount(), records = file.GetRecordCount();
  55.  
  56.       XString title = f;
  57.       title += " ";
  58.       title += (LONG) records;
  59.       title += " Record(s)";
  60.       SetText( title );
  61.  
  62.       if( records > 100)
  63.       {
  64.          XString s = "The file contains ";
  65.          s+= (LONG) records;
  66.          s+= " records. Display all records?";
  67.            XMessageBox m( (char*) s, "FrontLine - dBase III", MBOX_YESNO);
  68.          if( m.GetCommand() == MDID_NO)
  69.             delete this;
  70.       }
  71.       XContainerColumn * lastCol = NULL;
  72.       // for each column in the DBase-file a column is created in the container
  73.       for( int i = 0; i < fieldCount; i++)
  74.       {
  75.          LONG type= COL_HORZSEPARATOR;
  76.          if( i < file.GetFieldCount() - 1)
  77.             type |= COL_SEPARATOR;
  78.  
  79.          SHORT res = file.GetFieldType(i);
  80.  
  81.          if(res == XDBASE_FIELD_NUMERIC)
  82.             type |= COL_RIGHT;
  83.          else
  84.             type |= COL_LEFT;
  85.  
  86.          if( res == XDBASE_FIELD_DATE)
  87.             type |= COL_DATE;
  88.          else
  89.             type |= COL_STRING;
  90.  
  91.          XString  * ti = new XString();
  92.          file.GetFieldName(i, ti);
  93.          *ti += "(";
  94.          char dummy[2];
  95.          dummy[0] = file.GetFieldType(i);
  96.          dummy[1] = 0;
  97.          *ti += dummy;
  98.          *ti += ")";
  99.  
  100.          XContainerColumn * col = new XContainerColumn( c, (char*) *ti, i, type, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  101.          c->InsertColumn( col, lastCol);
  102.          lastCol = col;
  103.       }
  104.       c->UpdateColumns();
  105.       c->EnableWindowUpdate(FALSE);
  106.       //read alll records
  107.       for(int j = 1; j <= file.GetRecordCount(); j++)
  108.       {
  109.          XContainerObject * obj = new XContainerObject(c, file.GetFieldCount());
  110.          try
  111.          {
  112.             XDBaseRecord record( &file, j);
  113.             for(int i = 0; i < fieldCount; i++)
  114.             {
  115.                if( file.GetFieldType(i) != XDBASE_FIELD_DATE)
  116.                {
  117.                   XString * s = new XString();
  118.                   record.GetFieldData(i, *s);
  119.                   obj->SetColumnData( i, (const char*) *s);
  120.                }
  121.                else
  122.                {
  123.                   XDate d;
  124.                   record.GetFieldData(i, d);
  125.                   obj->SetColumnData( i, &d);
  126.                }
  127.             }
  128.             c->AddObject( obj );
  129.          } /* end try */
  130.          catch( XDBaseException e)  //if a record is marked as "deleted" an exception is thrown
  131.          {
  132.  
  133.          } /* end catch */
  134.       }
  135.       c->EnableWindowUpdate();
  136.    }
  137.    catch( XDBaseException e)
  138.    {
  139.       XMessageBox( (char*) e.GetErrorMessage());
  140.    }
  141.    Activate();
  142. }
  143.  
  144.  
  145. main(int argc, char ** argv)
  146. {
  147.    try
  148.    {
  149.       MyAppWindow * win = new MyAppWindow( (char*) argv[1]);
  150.       XApplication::GetApplication()->Start();
  151.    }
  152.    catch( XException e)
  153.    {
  154.       e.ShowError();
  155.       exit(-1);
  156.    }
  157.    return 0;
  158. }
  159.