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

extern UINT cfTimeData ;
extern SYSTEMTIME lt ;

/*
constructor/destructor 
*/

CTimeData::CTimeData()
{
	m_RefCount = 1 ; 
	MyOwnLockServer ( ) ;

    return;
}


CTimeData::~CTimeData(void)
{
	MyOwnUnlockServer ( ) ;
    return;
}


/*
IUnknown methods
*/

HRESULT CTimeData::QueryInterface(REFIID riid, LPVOID FAR *ppv)
{
    if (IsEqualIID(riid, IID_IUnknown)
        || IsEqualIID(riid, IID_IDataObject))
    {
        *ppv = (LPVOID)this;  
        AddRef();
        return S_OK ;
    }
    else
    {
    	*ppv = NULL;       
    	return  E_NOINTERFACE ;
    }
}


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


STDMETHODIMP_(ULONG) CTimeData::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 CTimeData::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 CTimeData::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 CTimeData::DAdvise (FORMATETC FAR* lpfe, DWORD dw, LPADVISESINK lpas, 
	DWORD *lpdw) 
{
	return 	E_NOTIMPL ;
}

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

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

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

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

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

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

