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

		BasicCOMImplementations.cpp

		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Author:	Ray Dream
		Date:	3/2/98

		Revision History

		when		who			what & why
		==========	==========	======================================================
		8/10/99		cph			remove TBasicExtension (now part of TBasicDataExchanger)
		05/04/99	del			added CloneData() methods
		2/25/99		Alec		moved extension specific TBasicObjectFactory and 
									extension calls to BasicCOMExtImplementations
		02/12/99	Mathieu/Nba	fixed exception (wrong Release) if QueryInterface fails
		12/15/98	Nba			added ReleaseIfNotNull_ in QueryInterfaces
		11/23/98	pbk			Separating ComSupport from MCFrame
		10/14/98	cph			removed MCAssert and call to ActivateDialogs (now a com utility for mcassert calls)
		10/9/98		Alec		fixed warnings about missing prototypes for DLL routines
		10/5/98		Alec		commented out MCAssert for non-debug
		10/5/98		Alec		added MCAssert and call to ActivateDialogs
		10/1/98		Eml			Added IMCUnknown* pUnkOuter to Clone
		8/25/98		Eml			Added TBasicListPart
		6/22/98		cph			ifdef'd out 3D utilities stuff for Muse
		6/18/98		Greg		Added SelfPrepareMenus to TBasicPart
		6/17/98		Eml			Split Basic3DCOMImplementations and BasicCOMImplementations
		6/17/98		Greg		Added SelfMenuAction to TBasicPart, TBasicModule
		5/8/98		Eml			Added Basic objects for Render Features
		5/4/98		Greg		Updated to new COM macros and added TBasicModule
								and TBasicPart
		4/8/98		Emil		Fixed "kRealMinusOne" problem

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


#include "MCException.h"
#include "MCCountedPtr.h"
#include "MCCountedPtrHelper.h"
#include "MCBasicGUID.h"

#include "IShComponent.h"
#include "BasicCOMImplementations.h"

int32 gBasicComRefCount = 0;


//**************************************************************************************************
//  Implementation of TBasicUnknown
//**************************************************************************************************

TBasicUnknown::TBasicUnknown()
{
	fRefCount = 1;
	gBasicComRefCount++;
	fControllingUnknown = NULL;
}


TBasicUnknown::~TBasicUnknown()
{
	gBasicComRefCount--;
}


MCCOMErr TBasicUnknown::QueryInterface(const MCIID& riid, void** ppvObj)
{
	if (fControllingUnknown)
	{
		return fControllingUnknown->QueryInterface(riid, ppvObj);
	}
	else
	{
		if (MCIsEqualIID(riid, IID_IMCUnknown))
		{
			TMCCountedGetHelper<IMCUnknown> result(ppvObj);
			result = this;
			return MC_S_OK;
		}
		else
		{
			TMCCountedGetHelper<IMCUnknown> result(ppvObj);
			return MC_E_NOINTERFACE;
		}
	}
}

void TBasicUnknown::SetControllingUnknown(IMCUnknown* controllingUnknown)
{
	fControllingUnknown = controllingUnknown;
}

void TBasicUnknown::CloneData(TBasicUnknown *destUnknown, IMCUnknown *pUnkOuter)
{
	destUnknown->SetControllingUnknown(pUnkOuter);
}


//**************************************************************************************************
//  Implementation of TBasicDataExchanger
//**************************************************************************************************
MCCOMErr TBasicDataExchanger::QueryInterface(const MCIID& riid, void** ppvObj)
{
	if (MCIsEqualIID(riid, IID_IExDataExchanger))
	{
		TMCCountedGetHelper<IExDataExchanger> result(ppvObj);
		result = this;
		return MC_S_OK;
	}
	return TBasicUnknown::QueryInterface(riid, ppvObj);
}

void TBasicDataExchanger::Clone(IExDataExchanger**res,IMCUnknown* pUnkOuter)
{
	TMCCountedGetHelper<IExDataExchanger> result(res);
}

void TBasicDataExchanger::CloneData(TBasicDataExchanger *destExtension, IMCUnknown* pUnkOuter)
{
	TBasicUnknown::CloneData(destExtension,pUnkOuter);
}

boolean TBasicDataExchanger::IsEqual(IExDataExchanger * otherDataExchanger)
{
	return true;
}

MCCOMErr TBasicDataExchanger::GetParameter(IDType keyword, void *parameter)
{
	return MC_E_NOTIMPL;
}

MCCOMErr TBasicDataExchanger::SetParameter(IDType keyword, void *parameter)
{
	return MC_E_NOTIMPL;
}

void* TBasicDataExchanger::GetExtensionDataBuffer()
{
	MCAssertMessage("TBasicDataExchanger::GetExtensionDataBuffer() should NEVER be called");
	return NULL;
}

MCCOMErr TBasicDataExchanger::ExtensionDataChanged()
{
	return MC_S_OK;
}

MCCOMErr TBasicDataExchanger::HandleEvent(MessageID message, IMFResponder* source, void* data)
{
	return MC_E_NOTIMPL;
}

MCCOMErr TBasicDataExchanger::SimpleHandleEvent(MessageID message, IMFResponder* source, void* data)
{
	return MC_E_NOTIMPL;
}

void TBasicDataExchanger::GetUIHandler(TUIHandlerFunctionPtr& outHandler)
{
	outHandler= NULL;
}

int16 TBasicDataExchanger::GetResID()
{
	return 0;
}

int16 TBasicDataExchanger::GetMiniPartID()
{
	return 0;
}

MCCOMErr TBasicDataExchanger::OverridesMiniPartID()
{
	return MC_E_NOTIMPL;
}


IShParameterComponent* TBasicDataExchanger::GetMyPrefsComponent()
{
	//go straight to fControllingUnknown to get the IShComponent interface
	TMCCountedPtr<IShComponent> thisComp;
	TBasicUnknown::QueryInterface(IID_IShComponent, (void**)&thisComp); 
	MCAssert(thisComp);

	return thisComp->GetPrefComponent();
}



//**************************************************************************************************
//  Implementation of TBasicUnknown
//**************************************************************************************************

MCCOMErr TBasicRegisterer::QueryInterface(const MCIID &riid, void** ppvObj)
{
	if (MCIsEqualIID(riid, IID_IExRegisterer))
	{
		TMCCountedGetHelper<IExRegisterer> result(ppvObj);
		result = this;
		return MC_S_OK;
	}
	return TBasicUnknown::QueryInterface(riid, ppvObj);
}
uint32 TBasicRegisterer::GetComponentCount()
{
	return 0;
}

MCCOMErr TBasicRegisterer::GetComponentData(uint32 index, APICOMPData & compData,TMCResource*& privateData, APIGUIDData & guidData)
{
	return MC_E_NOTIMPL;
}

MCCOMErr MCCOMAPI TBasicRegisterer::UnregisterComponents()
{
	return MC_E_NOTIMPL;
}



