/*	XGForm.cpp
 *
 *		This is the code for handling the form framework
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <stdlib.h>
#include <XForm.h>
#include <XWindow.h>
#include <XApplication.h>
#include <XAutoPtr.h>
#include <XError.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGForm::XGForm
 *
 *		Form construction. This initializes the form object. To create
 *	a form and associate it with the XGWindow object, you would first
 *	create this form, and then call AttachWindow to attach the windows.
 */

XGForm::XGForm()
{
	fWindow = NULL;
}

/*$	XGForm::~XGForm
 *
 *		Delete me. This should only be called by the XGWindow class.
 *	If it's called elsewhere, fry the associated window and close.
 */

XGForm::~XGForm()
{
	if (fWindow) {
		fWindow->fForm = NULL;
		delete fWindow;
	}
}

/************************************************************************/
/*																		*/
/*	Event dispatch router												*/
/*																		*/
/************************************************************************/

/*$	XGForm::GetParentDispatch
 *
 *		Who gets my messages after me? The application does!
 */

XGDispatch *XGForm::GetParentDispatch()
{
	return XGAppCore::GetApplication();
}

/************************************************************************/
/*																		*/
/*	Create Form Mechanism												*/
/*																		*/
/************************************************************************/

/*$	XGForm::AttachWindow
 *
 *		This version creates a new window and attaches it to the form.
 *	This is used during initialization of a form and a window.
 */

void XGForm::AttachWindow(short resID)
{
	XGView *v;
	Assert(fWindow == NULL);
	
	fWindow = XGWindowFactory::CreateFromResource(resID,this);
	DoFormInitialize();
	v = fWindow->FindFirstView();
	if (v) v->SetFocus();
}

/*$	XGForm::AttachWindow
 *
 *		This version attaches an existing window. I don't know why
 *	we would do this, but here it is anyways
 */

void XGForm::AttachWindow(XGWindow *w)
{
	Assert(w != NULL);
	Assert(w->fForm == NULL);
	
	w->fForm = this;
	DoFormInitialize();
}

/************************************************************************/
/*																		*/
/*	Default initialize/delete methods									*/
/*																		*/
/************************************************************************/

/*$	XGForm::DoFormInitialize
 *
 *		This method should be overridden by the application developer
 *	for forms which have their own initialization section.
 */

void XGForm::DoFormInitialize(void)
{
}

/*$	XGForm::DoFormTerminate
 *
 *		This method should be overridden by the application developer
 *	for forms which have their own shutdown section.
 */

void XGForm::DoFormTerminate(void)
{
}

/*$	XGForm::DoPreCreateWindow
 *
 *		This is called just before the window that this form is
 *	associated with is created. By default, does nothing.
 */

void XGForm::DoPreCreateWindow(XGSWindowInitRecord *)
{
}

/*$	XGForm::DoQueryDestroyWindow
 *
 *		This is called just before the window is closed using
 *	the 'CloseWindow' method. Return 'false' if the window is
 *	not to be closed.
 */

bool XGForm::DoQueryDestroyWindow()
{
	return true;
}

/************************************************************************/
/*																		*/
/*	Event placeholder													*/
/*																		*/
/************************************************************************/

BEGINDISPATCH(XGForm,XGDispatch)
ENDDISPATCH
