/*	XGWindowFactory.cpp
 *
 *		This is exactly like the view factory, but for constructing
 *	windows instead.
 */

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

  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 <XApplication.h>
#include <XError.h>
#include <XWindow.h>
#include <XView.h>
#include <XDataUtil.h>
#include <XViewFormat.h>
#include <XEvent.h>
#include <XDocument.h>

/************************************************************************/
/*																		*/
/*	Globals																*/
/*																		*/
/************************************************************************/

XGSDynArray<XGSWFactory> XGWindowFactory::gList;

/************************************************************************/
/*																		*/
/*	View Construction Support											*/
/*																		*/
/************************************************************************/

/*$	XGWindowFactory::CreateFromData
 *
 *		Given the data, this scans and creates the window. Note that
 *	I use the same header information for the window as the view
 *	(the XGSViewHeader record), even though this is a separate
 *	collection of routines.
 */

XGWindow *XGWindowFactory::CreateFromData(const unsigned char *data, XGForm *form)
{
	XGSViewHeader h;
	const unsigned char *nptr;
	XGWindow *nwindow;
	long i,len;
	XGView *view;
	XGSViewHeader *ptr;
	
	/*
	 *	Get and parse the header, initialize the data stream
	 */
	
	h = *((XGSViewHeader *)data);

#if OPT_NATIVEORDER != 1
	h.type     = SwapLong(h.type);
	h.sibling  = SwapLong(h.sibling);
	h.children = SwapLong(h.children);
	h.length   = SwapLong(h.length);
#endif
	
	ptr = (XGSViewHeader *)data;
	XGArgStream stream((unsigned char *)&(ptr->type),h.length + sizeof(long));
	
	/*
	 *	Now create the window. If not found, this will alert the
	 *	user and will create the generic 'window' instead.
	 */
	
	nwindow = NULL;
	len = gList.Length();
	for (i = 0; i < len; i++) {
		if (h.type == gList[i].type) {
			nwindow = (gList[i].proc)(stream, form);
			break;
		}
	}
	if (nwindow == NULL) {
		Warning(KXGWindowFactoryError,h.type);
		nwindow = new XGWindow(stream, form);
	}
	
	/*
	 *	I have the window. This will only have one child, the view
	 *	object which is to be contained in this window. Scan for
	 *	the child object and construct.
	 */
	
	if (h.children) {
		nptr = data + h.children;
		for (;;) {
			view = XGViewFactory::CreateFromData(nptr,nwindow);
			h = *((XGSViewHeader *)nptr);
			
#if OPT_NATIVEORDER != 1
			h.sibling = SwapLong(h.sibling);
#endif

			if (!h.sibling) break;
			nptr += h.sibling;
		}
	}
	
	/*
	 *	Done creating me and my children.
	 */
	
	return nwindow;		
}

/*$	XGWindowFactory::CreateFromResource
 *
 *		This creates the specified view from the resource identifier
 *	provided.
 */

XGWindow *XGWindowFactory::CreateFromResource(short resID, XGForm *form)
{
#if OPT_MACOS == 1
	Handle h;
	XGWindow *w;
	
	h = ::GetResource('VRes',resID);
	Assert(h != NULL);
	
	::HLock(h);
	w = CreateFromData((unsigned char *)(*h),form);
	::HUnlock(h);
	::ReleaseResource(h);
	return w;
#endif

#if OPT_WINOS == 1
	HRSRC hsrc;
	HGLOBAL h;
	void *data;
	XGWindow *w;
	
	hsrc = ::FindResource(_GInstance,MAKEINTRESOURCE(resID),"VRes");
	Assert(hsrc != NULL);
	h = LoadResource(_GInstance,hsrc);
	Assert(h != NULL);
	data = LockResource(h);
	
	/*
	 *	Now create the object
	 */
	
	w = CreateFromData((unsigned char *)data,form);
	
	/*
	 *	Release the resource
	 */
	
	UnlockResource(h);
	FreeResource(h);
	return w;
#endif

#if OPT_XWIN == 1
	void *data;
	
	data = GetResourceData('VRes',resID);
	Assert(data != NULL);
	return CreateFromData((unsigned char *)data,form);
#endif
}

/************************************************************************/
/*																		*/
/*	Construction														*/
/*																		*/
/************************************************************************/

/*$	XGWindowFactory::Register
 *
 *		register with me the construction proc. This is called
 *	internally by YAAF.
 */

void XGWindowFactory::Register(long id, XGWConstructorProc *proc)
{
	XGSWFactory f;
	
	f.type = id;
	f.proc = proc;
	gList += f;
}
