/*	XGViewFactory.cpp
 *
 *		This is the engine for creating views on the fly.
 */

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

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


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

XGSDynArray<XGSFactory> XGViewFactory::gList;

#if OPT_WINOS
extern "C" {
		extern HINSTANCE _GInstance;
}
#endif

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

/*$	XGViewFactory::CreateFromData
 *
 *		Given the data, this scans and creates the view.
 */

XGView *XGViewFactory::CreateFromData(const unsigned char *data, XGView *parent)
{
	XGSViewHeader h;
	XGSViewHeader *ptr;
	const unsigned char *nptr;
	XGView *nview;
	long i,len;
	
	/*
	 *	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 view. If not found, this will alert the
	 *	user and will create the generic 'view' instead.
	 */
	
	nview = NULL;
	len = gList.Length();
	for (i = 0; i < len; i++) {
		if (h.type == gList[i].type) {
			nview = (gList[i].proc)(parent,stream);
			break;
		}
	}
	if (nview == NULL) {
		Warning(KXGViewFactoryError,h.type);
		nview = new XGView(parent,stream);
	}
	
	/*
	 *	I have the view. Start looking for the children of this
	 *	thing. (The default view constructor will add the child
	 *	view to the specified parent view, so nothing needs to be
	 *	done here.)
	 */
	
	if (h.children) {
		nptr = data + h.children;

		for (;;) {
			CreateFromData(nptr,nview);
			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
	 */
	
	return nview;		
}

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

XGView *XGViewFactory::CreateFromResource(short resID, XGView *parent)
{
#if OPT_MACOS == 1
	Handle h;
	XGView *v;
	
	h = ::GetResource('VRes',resID);
	Assert(h != NULL);
	
	::HLock(h);
	v = CreateFromData((unsigned char *)(*h),parent);
	::HUnlock(h);
	::ReleaseResource(h);
	return v;
#endif

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

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

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

/*$	XGViewFactory::Register
 *
 *		Register with me the construction proc. Normally this is called
 *	internally by YAAF.
 */

void XGViewFactory::Register(long id, XGConstructorProc *proc)
{
	XGSFactory f;
	
	int len = gList.Length();
	int i;
	for (i = 0; i < len; i++) {
		if (id == gList[i].type) {
			gList[i].proc = proc;
			return;
		}
	}
	
	f.type = id;
	f.proc = proc;
	gList += f;
}
