/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540.3430
*	Web: http://www.rollthunder.com
*
*	File Name: CTXTDATA.CPP
*
*	Purpose: Implementation of simple IDataObject containing text
*
*********************************************************************
*/

#include <windows.h>
#include "ctxtdata.h"
#include "cenum.h"

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

CTextData::CTextData(LPSTR lpString)
{
	m_RefCount = 1 ; 
	m_length = lstrlen (lpString) + 1;
	m_cp = (LPSTR) GlobalAlloc (GMEM_FIXED, m_length) ;
	lstrcpy (m_cp, lpString) ;

    return;
}


CTextData::~CTextData(void)
{
	GlobalFree (m_cp) ;											  
    return;
}




/*
 * CTextData::QueryInterface
 * CTextData::AddRef
 * CTextData::Release
 *
 * Purpose:
 *  IUnknown members for CTextData object.
 */


HRESULT CTextData::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) CTextData::AddRef(void)
{
    m_RefCount ++ ;
    return m_RefCount ;
}


STDMETHODIMP_(ULONG) CTextData::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 CTextData::GetData (LPFORMATETC lpfe, LPSTGMEDIUM lpstg) 
{

/*
We support text format, transferred through a global, with aspect
content. Allocate a new global, copy our text into it, and fill in the 
elements of the STGMEDIUM structure that we were passed
*/

	if (lpfe->cfFormat == CF_TEXT 
		&& (lpfe->tymed & TYMED_HGLOBAL)
		&& lpfe->dwAspect == DVASPECT_CONTENT)
	{
		LPSTR cpout ; 
		cpout = (LPSTR) GlobalAlloc(GMEM_FIXED, m_length);
		if (!cpout)
		{
    		return E_OUTOFMEMORY ;
		}	                                       
		
		lstrcpy (cpout, m_cp) ;
	    
	    lpstg->tymed = TYMED_HGLOBAL ;
    	lpstg->hGlobal = (HGLOBAL) cpout ;
	   	lpstg->pUnkForRelease = NULL ;
    	
    	return S_OK ;

	}

/*
We also support the metafile format (in tasteful colors of yellow on 
green.
*/
	else if (lpfe->cfFormat == CF_METAFILEPICT
		&& (lpfe->tymed & TYMED_MFPICT)
		&& lpfe->dwAspect == DVASPECT_CONTENT)
	{
		HMETAFILE hMF ; HDC hDC ;

/*
Create a DC mapped to a metafile, draw our text on the DC, then close 
the DC to convert it into a memory metafile
*/
		hDC = CreateMetaFile (NULL) ;
		

		SetBkColor (hDC, RGB (0, 255, 0)) ;
		SetTextColor (hDC, RGB (255, 255, 0)) ;
		TextOut (hDC, 0, 0, m_cp, m_length-1) ;
		
		hMF = CloseMetaFile (hDC) ;

/*
Allocate a METAFILEPICT structure to described the picture, fill in 
its elements, then fill in the elements of STGMEDIUM structure to 
return data to caller.
*/


		METAFILEPICT *pMF = (METAFILEPICT *) GlobalAlloc (GPTR, 
			sizeof (METAFILEPICT));

		

		pMF->hMF = hMF ;
		pMF->mm = MM_TEXT ;
		pMF->xExt = 100 ;
		pMF->yExt = 100 ;

		lpstg->tymed = TYMED_MFPICT ;
		lpstg->hMetaFilePict = (HMETAFILEPICT) pMF ;
		lpstg->pUnkForRelease = NULL ;

    	return S_OK ;

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



STDMETHODIMP CTextData::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 == CF_TEXT 
		&& (lpfe->tymed & TYMED_HGLOBAL)
		&& lpfe->dwAspect == DVASPECT_CONTENT)
	{
		return S_OK ;
	}

/*
Or we'll give the the same text drawn in a metafile, also with aspect content.  Return success code.
*/

	else if (lpfe->cfFormat == CF_METAFILEPICT
		&& (lpfe->tymed & TYMED_MFPICT)
		&& lpfe->dwAspect == DVASPECT_CONTENT)
	{
		return S_OK ;
	}
	
/*
But we won't give you anything else
*/
	return DATA_E_FORMATETC ;
    
}

STDMETHODIMP CTextData::EnumFormatEtc (DWORD dwDir, LPENUMFORMATETC * lpEnum) 
{

	*lpEnum = (LPENUMFORMATETC) new CEnumerator (this, feArray, 2, 
		sizeof (FORMATETC), IID_IEnumFORMATETC) ;

	if (*lpEnum == NULL)	
	{
		return E_FAIL ;
	}
	
	return S_OK ;
}

/*
Unimplemented members
*/

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

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

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

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

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

STDMETHODIMP CTextData::EnumDAdvise  (LPENUMSTATDATA * lpen)
{
	*lpen = NULL ;
	return E_NOTIMPL ;
}
