home *** CD-ROM | disk | FTP | other *** search
- // ClibBrd.lib - Functions for reading from and writing to the Windows
- // clipboard.
- //
- // This library provides the following routines:
- // 1) GetClipboardData(ClipboardDataFormat[,ClipboardDataSize])
- // Copy data from the clipnoard, where:
- // ClipboardDataFormat - one of the following data types
- #define CF_TEXT 1
- #define CF_BITMAP 2
- #define CF_METAFILEPICT 3
- #define CF_SYLK 4
- #define CF_DIF 5
- #define CF_TIFF 6
- #define CF_OEMTEXT 7
- #define CF_DIB 8
- #define CF_PALETTE 9
- #define CF_OWNERDISPLAY 0x0080
- #define CF_DSPTEXT 0x0081
- #define CF_DSPBITMAP 0x0082
- #define CF_DSPMETAFILEPICT 0x0083
- // ClipboardDataSize - sets to the size of data returned; optional
- // Returns: NULL if no data in clipboard or not of correct type, else
- // returns data in buffer or BLOb of size ClipboardDataSize
- //
- // 2) PutClipboardData(ClipboardData,ClipboardDataSize,ClipboardDataFormat)
- // Copy data to the clipboard, where:
- // ClipboardData - buffer or BLOb of data to copy to the clipboard.
- // If this is NULL then clipboard is emptied.
- // ClipboardDataSize - size of data in ClipboardData, if this is zero
- // then the clipboard is emptied
- // ClipboardDataFormat - one of the data types #define'ed above
- // Returns: True if data successfully copied, else False
-
- GetClipboardData(ClipboardDataFormat,ClipboardDataSize)
- {
- _clipData = NULL; // assume failure
- if ( OpenClipboard() ) {
- // get clipboard text memory handle
- if ( _clipDataHandle = DynamicLink("USER","GETCLIPBOARDDATA",UWORD16,PASCAL,ClipboardDataFormat) ) {
- // lock the global memory
- if ( _clipDataPtr = DynamicLink("KERNEL","GLOBALLOCK",UWORD32,PASCAL,_clipDataHandle) ) {
- if ( _clipDataSize = DynamicLink("KERNEL","GLOBALSIZE",UWORD32,PASCAL,_clipDataHandle) ) {
- _clipData = peek(_clipDataPtr,_clipDataSize);
- if ( 1 < va_arg() )
- ClipboardDataSize = _clipDataSize;
- }
- // free the global memory now that we're done with it
- DynamicLink("KERNEL","GLOBALUNLOCK",UWORD32,PASCAL,_clipDataHandle);
- }
- }
- CloseClipboard();
- }
- return( _clipData );
- }
-
- PutClipboardData(ClipboardData,ClipboardDataSize,ClipboardDataFormat)
- {
- bool PutClipSuccess = FALSE; // assume failure
- if ( NULL == ClipboardData || 0 == ClipboardDataSize ) {
- // no data to put, so only needed to empty clipboard
- _clipDataHandle = NULL;
- PutClipSuccess = TRUE;
- } else {
- // allocate and fill a memory handle with this data
- #define GMEM_MOVEABLE 0x0002
- #define GMEM_ZEROINIT 0x0040
- if ( _clipDataHandle = DynamicLink("KERNEL","GLOBALALLOC",UWORD16,PASCAL,
- GMEM_MOVEABLE | GMEM_ZEROINIT,
- LoWord(ClipboardDataSize),HiWord(ClipboardDataSize)) ) {
- _clipDataPtr = DynamicLink("KERNEL","GLOBALLOCK",UWORD32,PASCAL,_clipDataHandle);
- poke(_clipDataPtr,ClipboardData,ClipboardDataSize);
- DynamicLink("KERNEL","GLOBALUNLOCK",UWORD32,PASCAL,_clipDataHandle);
- PutClipSuccess = TRUE;
- }
- }
- if ( OpenClipboard() ) {
- // empty clipboard of its current contents
- DynamicLink("USER","EMPTYCLIPBOARD",SWORD16,PASCAL);
- // give memory handle to the clipboard
- if ( NULL != _clipDataHandle )
- DynamicLink("USER","SETCLIPBOARDDATA",UWORD16,PASCAL,ClipboardDataFormat,_clipDataHandle);
- CloseClipboard();
- } else
- PutClipSucces = FALSE;
- return PutClipSuccess;
- }
-
- /******* ROUTINES USED BY THE ABOVE FUNCTIONS *******/
-
- LoWord(dword)
- {
- return dword & 0xFFFF
- }
-
- HiWord(dword)
- {
- return (dword >> 16) & 0xFFFF
- }
-
- // make sure clipboard gets closed before we exit
-
- ClipboardIsOpen = FALSE;
- atexit("CloseClipboardIfOpen");
-
- CloseClipboardIfOpen()
- {
- if ( ClipboardIsOpen )
- CloseClipboard();
- }
-
- OpenClipboard()
- {
- assert( !ClipboardIsOpen );
- MultiTask(FALSE);
- ClipboardIsOpen = DynamicLink("USER","OPENCLIPBOARD",SWORD16,PASCAL,
- ScreenHandle());
- if ( !ClipboardIsOpen )
- MultiTask(TRUE);
- return ClipboardIsOpen ;
- }
-
- CloseClipboard()
- {
- assert( ClipboardIsOpen );
- ClipboardIsOpen = FALSE;
- assert( DynamicLink("USER","CLOSECLIPBOARD",SWORD16,PASCAL) );
- MultiTask(TRUE);
- }
-