#include "sample10.h"

#include XColor_i
#include XMessageBox_i
#include XException_i
#include XString_i
#include XControlEvent_i
#include XContainerInfo_i
#include XContainerColumn_i
#include XContainerObject_i
#include XContainerHandler_i
#include XContainerEvent_i
#include XTable_i
#include XTableException_i

#include <stdlib.h>


class ContainerItem: public XContainerObject
{
      XString name, street, phone, zip, city;
      XDate date;
   public:
      ContainerItem( char * n, char * s, char * p, char * z, char * c, XDate * d, XContainerControl * cont);
};


ContainerItem :: ContainerItem( char * n, char * s, char * p, char * z, char * c, XDate * d, XContainerControl * cont): XContainerObject( cont, 6, CON_RECORDREADONLY)
{ 
   name=n; 
   city=c; 
   street=s;
   phone = p;
   zip = z;
	date = *d;

   SetColumnData(0, (char*) name);
   SetColumnData(1, (char*) phone);
   SetColumnData(2, (char*) street);
   SetColumnData(3, (char*) zip);
   SetColumnData(4, (char*) city); 
   SetColumnData(5, &date);
}


MyAppWindow :: MyAppWindow( XApplication * app, XResource * r ): XFrameWindow( r, "Sample10 - DB/2 access", XFrameWindow::defaultDialogStyle | FRM_TASKLIST )
{
   XColor c( COL_PALEGRAY);              //background-color
   SetBackgroundColor( &c);
   XRect re( 100, 100, 500, 400);
   SetSize( &re);                        //size

   XRect r1(10, 30, 470, 330);             //create a container
   XContainerControl * cont = new XContainerControl( this, &r1, 0, WIN_VISIBLE, "8.Helv");

   //setup (detail view)
   XContainerInfo info2( "Database entries", CO_DETAIL | CO_TITLE | CO_DETAILTITLES );
   cont->SetInfo( &info2);

   XContainerColumn * col = new XContainerColumn( cont, "Name", 0, COL_HORZSEPARATOR | COL_STRING | COL_SEPARATOR, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
   cont->InsertColumn( col);
   XContainerColumn * col2 = new XContainerColumn( cont, "Phone", 1, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
   cont->InsertColumn( col2, col);
   XContainerColumn * col3 = new XContainerColumn( cont, "Street", 2, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
   cont->InsertColumn( col3, col2);
   XContainerColumn * col4 = new XContainerColumn( cont, "ZIP", 3, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
   cont->InsertColumn( col4, col3);
   XContainerColumn * col5 = new XContainerColumn( cont, "City", 4, COL_SEPARATOR | COL_HORZSEPARATOR | COL_STRING, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
   cont->InsertColumn( col5, col4);
   XContainerColumn * col6 = new XContainerColumn( cont, "Date", 5, COL_HORZSEPARATOR | COL_DATE, COL_LEFT | COL_FITITLEREADONLY | COL_HORZSEPARATOR | COL_TOP );
   cont->InsertColumn( col6, col5);

   //update columns
   cont->UpdateColumns();

   XString name, phone, zip, city, street;
   XDate date;
   try
   {
      XTable::Connect("TESTIT");//replace here TESTIT with the name of your database

      XTable table("ADRESS");

      table.Select( "ID > 0");

      while( table.IsEOF() == FALSE)
      {
         table.GetField( "NAME", &name);
         table.GetField( "PHONE", &phone);
         table.GetField( "ZIPCODE", &zip);
         table.GetField( "STREET", &street);
         table.GetField( "CITY", &city);
			table.GetField( "BIRTHDAY", &date);
			ContainerItem * item = new ContainerItem( name, street, phone, zip, city, &date, cont);
         cont->AddObject( item );
         table.MoveNext();
      };
   }
   catch( XTableException e)
   {
      XString error = e.GetErrorMessage();
      error += ", code: ";
      error += (LONG) e.GetErrorCode();
      XMessageBox((char*) error, "Error!");
   }

   Activate();
}


MyAppWindow :: ~MyAppWindow()
{
   XTable::DisConnect();
}


//we only fill the background
void MyAppWindow :: Draw( void )
{
   FillBackground( );
}


MyApp :: MyApp(): XApplication()
{
   XResource r( 0, GetResourceLibrary());
   window = new MyAppWindow( this, &r );   //create new framewindow (see above)
}


void main ( void)
{
   try
   {
      MyApp * app = new MyApp();  //create a new application
	   app->Start();               //let the application work
   }
   catch( XTableException e)
   {
      XMessageBox( e.GetErrorMessage());
      exit(-1);
   }
   catch( XException e)
   {
      XMessageBox( e.GetErrorMessage());
      exit(-1);
   }
}
