#include "sample14.h"
#include "dialog.h"

#include XMessageBox_i
#include XMenuBar_i
#include XPoint_i
#include XException_i
#include XModelessDialog_i
#include XModalDialog_i

#include <stdlib.h>

MyApp * app;


class MyModelessDialog: public XModelessDialog
{
	public:
		MyModelessDialog( XResource * r, XWindow * w):XModelessDialog( r, w)
				{ GetWindow(PUSH_OK)->SetText("OK");}	//init the dialog
		BOOL DoCommand( LONG com );
};


BOOL MyModelessDialog :: DoCommand( LONG com )
{
   delete this; //a command was posted, destroy the dialog in every case
	return TRUE;
}


class MyModalDialog: public XModalDialog
{
	public:
		MyModalDialog( XResource * r, XWindow * w):XModalDialog( r, w) 
				{ GetWindow(PUSH_OK)->SetText("OK");} //init the dialog
		BOOL QueryForClose( SHORT com );
};


BOOL MyModalDialog :: QueryForClose( SHORT com )
{
	return TRUE; //a command was posted, return TRUE to destroy the dialog in every case in this sample
}


MyAppWindow :: MyAppWindow( XApplication * app, XResource * r ): XScrollWindow( r, "Sample14 - Dialogs", XFrameWindow::defaultStyle | FRM_TASKLIST | FRM_CENTER | FRM_MENU)
{
   Activate();
}


MyAppWindow :: ~MyAppWindow()
{
}


/* here the commands of the menu are posted */
BOOL MyAppWindow :: DoCommand( LONG com)
{
   switch( com )
      {
         case IDM_MODELESS:	//show the modeless dialog
            {
               try
               {
	               XResource res( IDM_MODELESSDIALOG, app->GetResourceLibrary());
						MyModelessDialog * dlg = new MyModelessDialog( &res, this);
					}			
					catch( XException e)
					{
						XMessageBox( e.GetErrorMessage()); 
					}
				}
				break;
         case IDM_MODAL:		//show the modeless dialog
            {
               try
               {
						XResource res( IDM_MODALDIALOG, app->GetResourceLibrary());
						MyModalDialog * dlg = new MyModalDialog( &res, this);
						dlg->Start();	//dont forgett to make it modal
					}			
					catch( XException& e)
					{
						XMessageBox( e.GetErrorMessage()); 
					}
				}
				break;
         default:					//destroy the application
            {
               app->Terminate();
            }
            break;
      }
   return TRUE;
}


MyApp :: MyApp(): XApplication()
{
   //we create a resource with our DLL which contains the id of the resource-window (see sample5.dlg)
   XResource r( IDM_MENU, GetResourceLibrary());
   app = this;

   //create the window
   window = new MyAppWindow( this, &r );

   //Set the size
   XRect rec(200,200, 300, 200);
   window->SetSize( &rec );
}


void MyAppWindow :: Draw( void )
{
	FillBackground();
}


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