#include "xmodlg.h"
#include "xmodal.h"
#include "xres.h"
#include "xreslib.h"
#include "xexcept.h"

extern LONG MAXX;
extern LONG MAXY;
extern SHORT cxDlgFrame;
extern SHORT cyDlgFrame;
extern SHORT cxBorder;
extern SHORT cyBorder;
extern SHORT cxSizeBorder;
extern SHORT cySizeBorder;

void BuildChilds(HWND dlgHandle);
MRESULT HandleDefault(XWindow * w, ULONG msg, MPARAM mp1, MPARAM mp2, BOOL & handled);


void SetCentered( HWND winhandle)
{
  	if (MAXX == 0)
	{
		MAXX = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
		MAXY = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
		cxDlgFrame = WinQuerySysValue(HWND_DESKTOP, SV_CXDLGFRAME);
		cyDlgFrame = WinQuerySysValue(HWND_DESKTOP, SV_CYDLGFRAME);
		cxBorder = WinQuerySysValue(HWND_DESKTOP, SV_CXBORDER);
		cyBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYBORDER);
		cxSizeBorder = WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER);
		cySizeBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
	}
	SWP swp;
	WinQueryWindowPos(winhandle, &swp);
	WinSetWindowPos( winhandle, HWND_TOP, MAXX / 2 - swp.cx / 2, MAXY / 2 - swp.cy / 2, swp.cx, swp.cy, SWP_MOVE);
}


MRESULT EXPENTRY ModelessDProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	XModelessDialog *w = (XModelessDialog *) WinQueryWindowPtr(hwnd, 0);

	if (w)
	{
		BOOL handeld = FALSE;
		MRESULT mr = HandleDefault(w, msg, mp1, mp2, handeld);

		if (handeld)
			return mr;
	}
	return WinDefDlgProc(hwnd, msg, mp1, mp2);
}


/*@
@class XModelesDialog
@parent XDialog
@type overview
@symbol _
*/


/*@ XModelessDialog :: XModelessDialog( const XResource * id, const XWindow * owner)
@group constructors/destructors
@remarks Construct a modeless dialog.
@parameters 
<t 'ø' c=2>
øXResource * id		øa resource which describes the dialog template<BR>
øXWindow * owner		øa pointer to the owner-window (can be NULL)
</t>
*/
XModelessDialog :: XModelessDialog( const XResource * id, const XWindow * owner, BOOL center)
{
   HWND hwnd = HWND_DESKTOP;
	if(owner)
		hwnd = owner->GetHandle();
	winhandle = WinLoadDlg( HWND_DESKTOP, hwnd, (PFNWP) ModelessDProc, id->GetResourceLibrary()->GetModuleHandle(), id->GetID(), NULL);
	if (winhandle == 0)
	{
		OOLThrow("could not create dialog from resources!", 0);
		return;
	}
	WinSetWindowPtr(winhandle, 0, this);

	if(center)
		SetCentered( winhandle );

	BuildChilds(winhandle);
	process = id->GetResourceLibrary()->GetProcess();
	process->AddWindow(this);
}


MRESULT EXPENTRY ModalDProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	XModalDialog *w = (XModalDialog *) WinQueryWindowPtr(hwnd, 0);

	if (w)
	{

		if(msg == WM_COMMAND )
		{
			if( w->QueryForClose( SHORT1FROMMP(mp1) ) == TRUE)
			{
      		w->command = SHORT1FROMMP(mp1);
				WinDismissDlg( w->winhandle, SHORT1FROMMP(mp1) );
				delete w;
				return (MRESULT) TRUE;
			}
			else
				return (MRESULT) FALSE;
		}
		BOOL handeld = FALSE;
		MRESULT mr = HandleDefault(w, msg, mp1, mp2, handeld);

		if (handeld)
			return (MRESULT) FALSE;// mr;
		
	}
	return WinDefDlgProc(hwnd, msg, mp1, mp2);
}


/*@
@class XModalDialog
@parent XDialog
@type overview
@symbol _
*/

/*@ XModalDialog :: XModalDialog( const XResource * id, const XWindow * owner)
@group constructors/destructors
@remarks Construct a modal dialog. You must call XModalDialog::Start() after 
constructing an instance to make the dialog work. When constructing the dialog you can initialize
the dialog controls.
@parameters 
<t 'ø' c=2>
øXResource * id		øA resource which describes the dialog template<BR>
øXWindow * owner		øA pointer to the owner-window. If now owner is given an error occures.<BR>
øBOOL center			øDisplay the dialog centered.
</t>
*/
XModalDialog :: XModalDialog( const XResource * id, const XWindow * owner, BOOL center)
{
   HWND hwnd = HWND_DESKTOP;
	if(owner)
		hwnd = owner->GetHandle();
	else
		OOLThrow("XModalDialog need a parent window!", 0);

	winhandle = WinLoadDlg( HWND_DESKTOP, hwnd, (PFNWP) ModalDProc, id->GetResourceLibrary()->GetModuleHandle(), id->GetID(), NULL);
	if (winhandle == 0)
	{
		OOLThrow("could not create dialog from resources!", 0);
		return;
	}
	WinSetWindowPtr(winhandle, 0, this);

	if( center )
		SetCentered(winhandle);

	BuildChilds(winhandle);
	process = id->GetResourceLibrary()->GetProcess();
	process->AddWindow(this);
}



/*@ XModalDialog :: Start( void )
@group misc
@remarks To make a modal dialog work you must call Start().
*/
void XModalDialog :: Start( void )
{
	WinProcessDlg( winhandle);
}


//Docs only
/*@ 
@class XDialog
@type overview
@symbol _
@remarks XDialog is the base class for XModalDialog and XModelessDialog.
You cannot construct an instance of XDialog directly, you have to chose
one of the child-classes.
*/


/*@ XDialog::GetCommand()
@remarks If a command was received by the dialog, eg. from a button the value is
stored and can be get by the application with this function.
@returns SHORT command
*/


/*@ XModalDialog::QueryForClose()
@remarks If a command was received by the modal dialog, eg. from a button, you must
decide if the modal dialog can be destroyed or not. Override this function, return TRUE if the dialog
can be destroyed, otherwise return FALSE.
@parameters SHORT command	the command received from a dialog-control
@returns BOOL canDestroy
*/

