/*-------------------------------------------------------------------------------------------------*
 * clipboard.h
 * http://cdebrock.free.fr
 * Accès presse-papier
 * version MFC

 	CString str2;

	CClipboard Clip(this);
	Clip.SetText("Toto was here");
	Clip.GetText(str2);
	TRACE(str2);

	Clip.AppendText("\nSo do I...\n");
	Clip.GetText(str2);
	TRACE(str2);

	Clip<< ""
		<< "Hello "
		<< "World!"
		<< "\n"
		>> str2;
	TRACE(str2);

	Clip = "hello world!";
	Clip += "\nHELLO WORLD!";

	str2 = Clip;
	TRACE(str2);
 *
 *-------------------------------------------------------------------------------------------------*/
class CClipboard
{
public:
	// Constructeur
	CClipboard(HWND hOwner, BOOL bOpenNow=1, BOOL bEmpty=0)
	{
		m_hOwner = hOwner;
		m_bOpened = FALSE;
		if (bOpenNow)
			Open();

		if (bEmpty && m_bOpened)
			Empty();
	};

	//------------------------------------------------------------
	~CClipboard()
	{
		Close();
	}

	// Opens the clipboard for examination and prevents other applications from modifying the clipboard content.
	BOOL Open()
	{
		m_bOpened = OpenClipboard(m_hOwner);
		return m_bOpened;
	}

	// Empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the window that currently has the clipboard open.
	BOOL Empty()
	{
		//if (m_bOpened)
		return EmptyClipboard();
	};

	// Closes the clipboard.This enables other windows to access the clipboard.
	BOOL Close()
	{
		if (m_bOpened)
			m_bOpened = !CloseClipboard();

		return !m_bOpened;
	}

	// Retrieves data from the clipboard in a plain text format.
	BOOL GetText (CString& strText)
	{
		BOOL bret(0);

		if (!m_bOpened)
			Open();

		if (m_bOpened)
		{
			HGLOBAL h;

			h = GetClipboardData(CF_TEXT);
			strText.Empty();

			if (h)
			{
				LPCTSTR p = (LPCTSTR) GlobalLock(h);
				strText=p;
				GlobalUnlock(h);

				bret=TRUE;
			}
		}

		return TRUE;
	};

	// Places data on the clipboard in a plain text format. Replaces previous text.
	BOOL SetText (LPCTSTR lpszText)
	{
		BOOL bret(0);

		if (!m_bOpened)
			Open();

		if (m_bOpened)
		{
			HGLOBAL h;
			h = GlobalAlloc(GHND|GMEM_MOVEABLE, lstrlen(lpszText)+1);
			if (h)
			{
				LPTSTR p;
				p = (LPTSTR) GlobalLock(h);
				if (p)
				{
					lstrcpy(p, lpszText);
					GlobalUnlock(h);

					bret = (NULL!=SetClipboardData(CF_TEXT,h));
				}
				else
				{
					// error
					GlobalFree(h);
				}
			}

			Close();
		}

		return bret;
	}

	// Appends data on the clipboard in a plain text format.
	BOOL AppendText(LPCTSTR lpszText)
	{
		CString strFull;

		GetText(strFull);
		strFull += lpszText;

		return SetText(strFull);
	}

	// Places data on the clipboard in a plain text format. Replaces previous text.
	CClipboard& operator =(LPCTSTR lpszText)
	{
		SetText(lpszText);
		return *this;
	};

	// Retrieves data from the clipboard in a plain text format.
	operator CString()
	{
		CString str;
		GetText(str);
		return str;
	};

	// Appends data on the clipboard in a plain text format.
	CClipboard& operator +=(LPCTSTR lpszText)
	{
		AppendText(lpszText);
		return *this;
	};

	// Retrieves data from the clipboard in a plain text format.
	CClipboard& operator <<(LPCTSTR lpszText)
	{
		AppendText(lpszText);
		return *this;
	};

	// Appends data on the clipboard in a plain text format.
	CClipboard& operator >>(CString& str)
	{
		GetText(str);
		return *this;
	};


protected:
	//------------------------------------------------------------
	HWND	m_hOwner;
	BOOL	m_bOpened;
};