#include <windows.h>
#include "ctimedta.h"

extern UINT cfTimeData ;
extern SYSTEMTIME lt ;
extern int g_ObjectCount ;


FORMATETC feArray []= {
						{ CF_TEXT, NULL, DVASPECT_CONTENT,  -1, TYMED_HGLOBAL },
					  };
/*
constructor/destructor 
*/

CTimeDataOuter::CTimeDataOuter()
{
	m_RefCount = 1 ; 
	g_ObjectCount ++ ;

/*
Create inner contained IDispatch 
*/
	
	CLSID clsid ;
	CLSIDFromProgID (L"EssenceOfOle.HelloAutomation", &clsid) ;

	CoCreateInstance (
		clsid, 
		this,			// outer IUnknown for aggregation
		CLSCTX_LOCAL_SERVER, 
		IID_IUnknown,	// ask for IUnknown to get non-delegating one
		(LPVOID *) &m_pInnerUnknown) ;	


    return;
}


CTimeDataOuter::~CTimeDataOuter(void)
{  
	g_ObjectCount -- ;

/*
Release inner contained IDispatch
*/
	if (m_pInnerUnknown)
	{
		m_pInnerUnknown->Release ( ) ;
	}


    return;
}


/*
IUnknown methods
*/

HRESULT CTimeDataOuter::QueryInterface(REFIID riid, LPVOID FAR *ppv)
{
    if (riid == IID_IUnknown || riid == IID_IDataObject)
    {
        *ppv = (LPVOID)this;  
        AddRef();
        return S_OK ;
    }
	else if (riid == IID_IDispatch)
	{
		return m_pInnerUnknown->QueryInterface (riid, ppv) ;
	}
    else
    {
    	*ppv = NULL;       
    	return  E_NOINTERFACE ;
    }
}


STDMETHODIMP_(ULONG) CTimeDataOuter::AddRef(void)
{
    m_RefCount ++ ;
    return m_RefCount ;
}


STDMETHODIMP_(ULONG) CTimeDataOuter::Release(void)
{
    
    m_RefCount -- ;
    
    if (m_RefCount == 0L)
    {
        delete this;
    }	return 0 ;

    return m_RefCount ;
}


/*
GetData Method.  Caller is asking if we can supply data in a given 
format and medium
*/

STDMETHODIMP CTimeDataOuter::GetData (LPFORMATETC lpfe, LPSTGMEDIUM lpstg) 
{


/*
We support a private clipboard format, identified by the UINT cfTimeData.
This is actually a structure of type SYSTEMTIME. 
Allocate a new global, copy our global system time into it, and fill in the 
elements of the STGMEDIUM structure that we were passed
*/

	if (lpfe->cfFormat == cfTimeData
		&& lpfe->tymed == TYMED_HGLOBAL
		&& lpfe->dwAspect == DVASPECT_CONTENT)
	{
		SYSTEMTIME *pSt ;
			
/*
If lindex == -1, then we are simply transferring a SYSTEMTIME structure.
Any other value means that we are doing a performance test.  In this
case, the value of lindex is the amount fo memory to allocate, while
the calling app measures response time.
*/
		if (lpfe->lindex == -1)
		{
			pSt = (SYSTEMTIME *)GlobalAlloc(GMEM_FIXED, sizeof (SYSTEMTIME));
		}
		else
		{
			pSt = (SYSTEMTIME *)GlobalAlloc(GMEM_FIXED, lpfe->lindex);
		}

		if (!pSt)
		{
    		return E_OUTOFMEMORY ;
		}	                                       
		
		memmove (pSt, &lt, sizeof (SYSTEMTIME)) ;
	    
	    lpstg->tymed = TYMED_HGLOBAL ;
    	lpstg->hGlobal = (HGLOBAL) pSt ;
	   	lpstg->pUnkForRelease = NULL ; ;
    	
    	return  S_OK ;

	}


/*
User asked for a format that we couldn't supply.  Return error code.
*/
	
	return DATA_E_FORMATETC ;
}



STDMETHODIMP CTimeDataOuter::QueryGetData (LPFORMATETC lpfe) 
{

/*
We'll give you text, transferred through a global, with aspect
content.  If the user is asking for this, return success code.
*/

	if (lpfe->cfFormat == cfTimeData 
		&& lpfe->tymed == TYMED_HGLOBAL
		&& lpfe->dwAspect == DVASPECT_CONTENT)
	{
		return S_OK ;
	}


/*
But we won't give you anything else
*/
	return DATA_E_FORMATETC ;
    
}


/*
Unimplemented members
*/

STDMETHODIMP CTimeDataOuter::EnumFormatEtc (DWORD dwDir, LPENUMFORMATETC FAR* lpEnum) 
{
	return E_NOTIMPL ;
}


STDMETHODIMP CTimeDataOuter::DAdvise (FORMATETC FAR* lpfe, DWORD dw, LPADVISESINK lpas, 
	DWORD *lpdw) 
{
	return E_NOTIMPL ;
}

STDMETHODIMP CTimeDataOuter::DUnadvise  (DWORD dw)
{
	return E_NOTIMPL ;
}

STDMETHODIMP CTimeDataOuter::EnumDAdvise  (LPENUMSTATDATA *lpen)
{
	return E_NOTIMPL ;
}

STDMETHODIMP CTimeDataOuter::GetDataHere (LPFORMATETC lpfe, LPSTGMEDIUM lpst)
{
	return E_NOTIMPL ;
}

STDMETHODIMP CTimeDataOuter::GetCanonicalFormatEtc (LPFORMATETC lpout, LPFORMATETC lpin)
{
	return E_NOTIMPL ;
}

STDMETHODIMP CTimeDataOuter::SetData (LPFORMATETC lpfe, STGMEDIUM FAR * lpst, BOOL fRelease)
{
	return E_NOTIMPL ;
}
