home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / samples / sample10 / sample10.cpp < prev    next >
C/C++ Source or Header  |  1997-03-28  |  5KB  |  149 lines

  1. #include "sample10.h"
  2.  
  3. #include XColor_i
  4. #include XMessageBox_i
  5. #include XException_i
  6. #include XString_i
  7. #include XControlEvent_i
  8. #include XContainerInfo_i
  9. #include XContainerColumn_i
  10. #include XContainerObject_i
  11. #include XContainerHandler_i
  12. #include XContainerEvent_i
  13. #include XTable_i
  14. #include XTableException_i
  15.  
  16. #include <stdlib.h>
  17.  
  18.  
  19. class ContainerItem: public XContainerObject
  20. {
  21.       XString name, street, phone, zip, city;
  22.       XDate date;
  23.    public:
  24.       ContainerItem( char * n, char * s, char * p, char * z, char * c, XDate * d, XContainerControl * cont);
  25. };
  26.  
  27.  
  28. ContainerItem :: ContainerItem( char * n, char * s, char * p, char * z, char * c, XDate * d, XContainerControl * cont): XContainerObject( cont, 6, CON_RECORDREADONLY)
  29.    name=n; 
  30.    city=c; 
  31.    street=s;
  32.    phone = p;
  33.    zip = z;
  34.     date = *d;
  35.  
  36.    SetColumnData(0, (char*) name);
  37.    SetColumnData(1, (char*) phone);
  38.    SetColumnData(2, (char*) street);
  39.    SetColumnData(3, (char*) zip);
  40.    SetColumnData(4, (char*) city); 
  41.    SetColumnData(5, &date);
  42. }
  43.  
  44.  
  45. MyAppWindow :: MyAppWindow( XApplication * app, XResource * r ): XFrameWindow( r, "Sample10 - DB/2 access", XFrameWindow::defaultDialogStyle | FRM_TASKLIST )
  46. {
  47.    XColor c( COL_PALEGRAY);              //background-color
  48.    SetBackgroundColor( &c);
  49.    XRect re( 100, 100, 500, 400);
  50.    SetSize( &re);                        //size
  51.  
  52.    XRect r1(10, 30, 470, 330);             //create a container
  53.    XContainerControl * cont = new XContainerControl( this, &r1, 0, WIN_VISIBLE, "8.Helv");
  54.  
  55.    //setup (detail view)
  56.    XContainerInfo info2( "Database entries", CO_DETAIL | CO_TITLE | CO_DETAILTITLES );
  57.    cont->SetInfo( &info2);
  58.  
  59.    XContainerColumn * col = new XContainerColumn( cont, "Name", 0, COL_HORZSEPARATOR | COL_STRING | COL_SEPARATOR, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  60.    cont->InsertColumn( col);
  61.    XContainerColumn * col2 = new XContainerColumn( cont, "Phone", 1, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  62.    cont->InsertColumn( col2, col);
  63.    XContainerColumn * col3 = new XContainerColumn( cont, "Street", 2, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  64.    cont->InsertColumn( col3, col2);
  65.    XContainerColumn * col4 = new XContainerColumn( cont, "ZIP", 3, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  66.    cont->InsertColumn( col4, col3);
  67.    XContainerColumn * col5 = new XContainerColumn( cont, "City", 4, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  68.    cont->InsertColumn( col5, col4);
  69.    XContainerColumn * col6 = new XContainerColumn( cont, "Date", 5, COL_HORZSEPARATOR | COL_DATE, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
  70.    cont->InsertColumn( col6, col5);
  71.  
  72.    //update columns
  73.    cont->UpdateColumns();
  74.  
  75.    XString name, phone, zip, city, street;
  76.    XDate date;
  77.    try
  78.    {
  79.       XTable::Connect("TESTIT");//replace here TESTIT with the name of your database
  80.  
  81.       XTable table("ADRESS");
  82.  
  83.       table.Select( "ID > 0");
  84.  
  85.       while( table.IsEOF() == FALSE)
  86.       {
  87.          table.GetField( "NAME", &name);
  88.          table.GetField( "PHONE", &phone);
  89.          table.GetField( "ZIPCODE", &zip);
  90.          table.GetField( "STREET", &street);
  91.          table.GetField( "CITY", &city);
  92.             table.GetField( "BIRTHDAY", &date);
  93.             ContainerItem * item = new ContainerItem( name, street, phone, zip, city, &date, cont);
  94.          cont->AddObject( item );
  95.          table.MoveNext();
  96.       };
  97.    }
  98.    catch( XTableException e)
  99.    {
  100.       XString error = e.GetErrorMessage();
  101.       error += ", code: ";
  102.       error += (LONG) e.GetErrorCode();
  103.       XMessageBox((char*) error, "Error!");
  104.    }
  105.  
  106.    Activate();
  107. }
  108.  
  109.  
  110. MyAppWindow :: ~MyAppWindow()
  111. {
  112.    XTable::DisConnect();
  113. }
  114.  
  115.  
  116. //we only fill the background
  117. void MyAppWindow :: Draw( void )
  118. {
  119.    FillBackground( );
  120. }
  121.  
  122.  
  123. MyApp :: MyApp(): XApplication()
  124. {
  125.    XResource r( 0, GetResourceLibrary());
  126.    window = new MyAppWindow( this, &r );   //create new framewindow (see above)
  127. }
  128.  
  129.  
  130. void main ( void)
  131. {
  132.    try
  133.    {
  134.       MyApp * app = new MyApp();  //create a new application
  135.        app->Start();               //let the application work
  136.    }
  137.    catch( XTableException e)
  138.    {
  139.       XMessageBox( e.GetErrorMessage());
  140.       exit(-1);
  141.    }
  142.    catch( XException e)
  143.    {
  144.       XMessageBox( e.GetErrorMessage());
  145.       exit(-1);
  146.    }
  147. }
  148.