#include "XClipBrd.h"
#include "XWindow.h"
#include "XBitmap.h"
#include "XString.h"


/*@ XClipBoard :: XClipBoard( const XWindow * win)
@group constructors/destructors
@remarks Open the clipboard
@parameters XWindow * theOwner          the owning window
*/
XClipBoard :: XClipBoard(const XWindow * win)
{
	isOpen = FALSE;
	hab = WinQueryAnchorBlock(win->GetHandle());
	if (WinOpenClipbrd(hab) == FALSE)
		return;
	isOpen = TRUE;
}


/*@ XClipBoard :: GetText( XString * buffer )
@group get data
@remarks Get the text in the clipboard
@parameters    XString * buffer     buffer to hold the data
*/
void XClipBoard::GetText(XString * buffer)
{
	*buffer = (char *) WinQueryClipbrdData(hab, CF_TEXT);
}


/*@ XClipBoard :: GetBitmap( XBitmap * bmp )
@group get data
@remarks Get a bitmap from the clipboard
@parameters XBitmap *   buffer to hold the data
*/
void XClipBoard::GetBitmap(XBitmap * bmp)
{
	bmp->hbm = WinQueryClipbrdData(hab, CF_BITMAP);
	XSize size;
	bmp->GetDimensions(&size);
	bmp->width = bmp->cx = size.GetWidth();
	bmp->height = bmp->cy = size.GetHeight();
}


/*@ XClipBoard :: SetBitmap( const XBitmap * bmp)
@group set data
@remarks Set a bitmap to the clipboard
@parameters XBitmap *                    the bitmap
@returns    BOOL                         success
*/
BOOL XClipBoard::SetBitmap(const XBitmap * bmp)
{
	return WinSetClipbrdData(hab, (ULONG) bmp->GetHandle(), CF_BITMAP, CFI_HANDLE);
}


/*@ XClipBoard :: SetText( const char * p, const ULONG len)
@group set data
@remarks Set text to the clipboard
@parameters char * theText               text<BR>
            ULONG len                    length of the text (default is 0)
@returns    BOOL                         success
*/
BOOL XClipBoard::SetText(const char *p, const ULONG len)
{
	void *pp;

	ULONG l = (len == 0 ? strlen(p) + 1 : len + 1);

	DosAllocSharedMem(&pp, NULL, l, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE);
	strcpy((char *) pp, p);
	return WinSetClipbrdData(hab, (ULONG) pp, CF_TEXT, CFI_POINTER);
}


/*@ XClipBoard :: ~XClipBoard()
@group constructor/destructor
@remarks Never forget to close the clipboard by calling the destructor!
*/
XClipBoard :: ~XClipBoard()
{
	if (isOpen)
		WinCloseClipbrd(hab);
}
