//
//	VCOM Provider LibraryLibrary.cpp
//
//	Main entry point for VectorWorks Plug-in Library code.
//
//	Copyright © Nemetschek North America, Inc  2005.
//	All Rights Reserved.


#include "StdAfx.h"

#include "../Interfaces/IVCOMSampleInterface.h"

// ------------------------------------------------------------------------------------------------------------------------------------
// {133A3E9E-3E2D-4893-95F0-769B0C701DF5}
static const VWIID IID_VCOMSampleData = { 0x133a3e9e, 0x3e2d, 0x4893, { 0x95, 0xf0, 0x76, 0x9b, 0xc, 0x70, 0x1d, 0xf5 } };

class CVCOMSampleData : public IVCOMSampleData
{
public:
								CVCOMSampleData(CallBackPtr pCBP)	{ fRefCnt = 0; fCBP = pCBP; }
	virtual						~CVCOMSampleData()					{ }
	virtual long VCOM_CALLTYPE	AddRef()							{ fRefCnt ++; return fRefCnt; }
	virtual long VCOM_CALLTYPE	Release()							{ if ( fRefCnt > 0 ) { fRefCnt --; } return fRefCnt; }

public:
	virtual VCOMError VCOM_CALLTYPE Get(Uint32& outValue)
	{
		outValue = 234;
		return kVCOMError_NoError;
	}

private:
	long			fRefCnt;
	CallBackPtr		fCBP;

};

// ------------------------------------------------------------------------------------------------------------------------------------
// {B5706042-BF87-4cff-9BD9-3A08B01C183C}
class CVCOMSample : public IVCOMSample
{
public:
								CVCOMSample(CallBackPtr pCBP)	{ fRefCnt = 0; fCBP = pCBP; }
	virtual						~CVCOMSample()					{ }
	virtual long VCOM_CALLTYPE	AddRef()						{ fRefCnt ++; return fRefCnt; }
	virtual long VCOM_CALLTYPE	Release()						{ if ( fRefCnt > 0 ) { fRefCnt --; } return fRefCnt; }

public:
	virtual VCOMError VCOM_CALLTYPE Function()
	{
		MCObjectHandle	hHeader	= ::GS_GetDrawingHeader( fCBP );
		return kVCOMError_NoError;
	}

	virtual VCOMError VCOM_CALLTYPE GetData(IVCOMSampleData** ppOutData)
	{
		if ( ppOutData == NULL ) return kVCOMError_InvalidArg;

		return ::GS_VWQueryInterface( IID_VCOMSampleData, (IVWUnknown**) ppOutData );
	}

private:
	long			fRefCnt;
	CallBackPtr		fCBP;
};

//------------------------------------------------------------------
// The following constants are used to branch to the routined specified by the 
// routineSelector argument.  The values of these constants must match the 
// resource ids of the 'VLIB' resources that define each library routine.
// They must begin at 0 and proceed in sequence without any gaps in the numbering.
// These constants must be kept in sync with the 'VLIB' resources.


//------------------------------------------------------------------
long PluginLibraryMain(long routineSelector, void* argTable, long &ioUserData)
//
//	This is the main entry point for the library.  
//	The library may contain more than one routine, so branch to the specified routine now.
//
{
	// initialize VCOM mechanizm
	::GS_InitializeVCOM( gCBP );

	long	reply	= 0L;

	switch (routineSelector) {
		case kPluginLIBInitGlobals:
			break;
		case kPluginLIBDisposeGlobals:
			break;
		
		// ----------------------------------------------------------------
		// VCOM support
		case kVCOMRegisterInterfaces: {
				// register the putblic interface
				::GS_VWRegisterInterface( IID_VCOMSample, argTable );
				// register the private interface
				::GS_VWRegisterInterface( IID_VCOMSampleData, argTable );
				reply			= kVCOMError_NoError;
			} break;

		// handle creation of interfaces of this library
		case kVCOMQueryInterface: {
				VWIID*		pIID		= (VWIID*) argTable;
				if ( pIID == NULL )
					return kVCOMError_InvalidArg;

				IVWUnknown*	pInterface	= 0;
				reply					= kVCOMError_NoInstance;
				// recognize the interface
				if ( IID_VCOMSample == *pIID ) { 
					pInterface		= new CVCOMSample( gCBP );
					reply			= kVCOMError_NoError;
				}
				else if ( IID_VCOMSampleData  == *pIID ) {
					pInterface		= new CVCOMSampleData( gCBP );
					reply			= kVCOMError_NoError;
				}

				ioUserData		= (long) pInterface;
			} break;

		// handle destruction of the interfaces of this library
		case kVCOMReleaseInterface: {
				VWIID*		pIID		= (VWIID*) argTable;
				IVWUnknown*	pInterface	= (IVWUnknown*) ioUserData;
				if ( pInterface == NULL || pIID == NULL )
					return kVCOMError_InvalidArg;

				reply					= kVCOMError_NoInstance;
				// recognize the interface
				if ( IID_VCOMSample == *pIID ) {
					CVCOMSample*		pImpl	= (CVCOMSample*) pInterface;
					delete pImpl;
					reply			= kVCOMError_NoError;
				}
				else if ( IID_VCOMSampleData  == *pIID ) {
					CVCOMSampleData*	pImpl	= (CVCOMSampleData*) pInterface;
					delete pImpl;
					reply			= kVCOMError_NoError;
				}
			} break;

		default:
			break;
	}

	return reply;
}
