home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / CLIPBRD.LIB < prev    next >
Text File  |  1994-05-22  |  5KB  |  130 lines

  1. // ClibBrd.lib - Functions for reading from and
  2. // ver.2         writing to the OS/2 clipboard.
  3. //
  4. // This library provides the following routines:
  5. // 1) GetClipboardData(ClipboardDataFormat[,ClipboardDataSize])
  6. //     Copy data from the clipnoard, where:
  7. //       ClipboardDataFormat - one of the following data types
  8.             #define CF_TEXT         1
  9.             #define CF_BITMAP       2
  10.             #define CF_DSPTEXT      3
  11.             #define CF_DSPBITMAP    4
  12.             #define CF_METAFILE     5
  13.             #define CF_DSPMETAFILE  6
  14.             #define CF_PALETTE      9
  15. //       ClipboardDataSize - sets to the size of data returned; optional
  16. //       Returns: NULL if no data in clipboard or not of correct type, else
  17. //                returns data in buffer or BLOb of size ClipboardDataSize
  18. //
  19. // 2) PutClipboardData(ClipboardData,ClipboardDataSize,ClipboardDataFormat)
  20. //     Copy data to the clipboard, where:
  21. //       ClipboardData - buffer or BLOb of data to copy to the clipboard.
  22. //                       If this is NULL then clipboard is emptied.
  23. //       ClipboardDataSize - size of data in ClipboardData, if this is zero
  24. //                           then the clipboard is emptied
  25. //       ClipboardDataFormat - one of the data types #define'ed above
  26. //       Returns: True if data successfully copied, else False
  27.  
  28.  
  29. GetClipboardData(pClipboardDataFormat,pClipboardDataSize)
  30. {
  31.    lData = NULL;  // assume failure
  32.    if ( OpenClipboard() ) {
  33.  
  34.       #define ORD_WIN32QUERYCLIPBRDDATA  806
  35.       lDataPtr = PMDynamicLink("PMWIN",ORD_WIN32QUERYCLIPBRDDATA,BIT32,CDECL,
  36.                                PMInfo().hab,pClipboardDataFormat);
  37.  
  38.       if ( NULL != lDataPtr ) {
  39.          // query memory for how big lDataPtr is
  40.          BLObPut(lenBLOb,16384,UWORD32);
  41.          #define ORD_DOS32QUERYMEM               306
  42.          if ( 0 == PMDynamicLink("DOSCALLS",ORD_DOS32QUERYMEM,BIT32,CDECL,
  43.                                  lDataTextPtr,lenBLOb,flags) ) {
  44.             if ( 0 != (lLen = BLObGet(lenBLOb,0,UWORD32)) ) {
  45.                lData = PMpeek(lDataPtr,lLen);
  46.                if ( 1 < va_arg() )
  47.                   pClipboardDataSize = lLen;
  48.             }
  49.          }
  50.       }
  51.       CloseClipboard();
  52.    }
  53.    return(lData);
  54. }
  55.  
  56. PutClipboardData(ClipboardData,pClipboardDataSize,pClipboardDataFormat)
  57. {
  58.    bool PutClipSuccess = FALSE;  // assume failure
  59.    if ( OpenClipboard() ) {
  60.  
  61.       // empty clipboard of its current contents
  62.       #define ORD_WIN32EMPTYCLIPBRD 733
  63.       PMDynamicLink("PMWIN",ORD_WIN32EMPTYCLIPBRD,BIT32,CDECL,PMInfo().hab);
  64.  
  65.       if ( NULL == ClipboardData  ||  0 == pClipboardDataSize ) {
  66.          // no data to put, so only needed to empty clipboard
  67.          PutClipSuccess = TRUE;
  68.       } else {
  69.  
  70.          // allocate giveable shared memory, and copy this data to there
  71.          #define ORD_DOS32ALLOCSHAREDMEM  300
  72.          #define PAG_COMMIT   0x10
  73.          #define OBJ_GIVEABLE 0x200
  74.          #define PAG_WRITE    0x2
  75.          if ( 0 == PMDynamicLink("DOSCALLS",ORD_DOS32ALLOCSHAREDMEM,BIT32,CDECL,
  76.                                  lGiveMem,NULL,pClipboardDataSize,
  77.                                  PAG_COMMIT|OBJ_GIVEABLE|PAG_WRITE) ) {
  78.             PMpoke(lGiveMem,ClipboardData,pClipboardDataSize);
  79.  
  80.             // finally, give this to the clipboard
  81.             #define ORD_WIN32SETCLIPBRDDATA  854
  82.             #define CFI_POINTER  0x0400
  83.             if ( PMDynamicLink("PMWIN",ORD_WIN32SETCLIPBRDDATA,BIT32,CDECL,
  84.                                PMInfo().hab,lGiveMem,pClipboardDataFormat,CFI_POINTER) ) {
  85.                PutClipSuccess = True;
  86.             } else {
  87.                // error giving data to clipboad, and so must free it here
  88.                #define ORD_DOS32FREEMEM   304
  89.                PMDynamicLink("DOSCALLS",ORD_DOS32FREEMEM,BIT32,CDECL,lGiveMem);
  90.             }
  91.          }
  92.       }
  93.       CloseClipboard();
  94.    }
  95.    return PutClipSuccess;
  96. }
  97.  
  98.  
  99. /******* ROUTINES USED BY THE ABOVE FUNCTIONS *******/
  100.  
  101. // make sure clipboard gets closed before we exit
  102.  
  103.    gClipboardIsOpen = FALSE;
  104.    atexit("CloseClipboardIfOpen");
  105.  
  106.    CloseClipboardIfOpen()
  107.    {
  108.       if ( gClipboardIsOpen )
  109.          CloseClipboard();
  110.    }
  111.  
  112. OpenClipboard() // Return TRUE if open, else FALSE if cannot open
  113. {
  114.    assert( !gClipboardIsOpen );
  115.    #define ORD_WIN32OPENCLIPBRD  793
  116.    if ( PMDynamicLink("PMWIN",ORD_WIN32OPENCLIPBRD,BIT32,CDECL,PMInfo().hab) )
  117.       gClipboardIsOpen = TRUE;
  118.    return( gClipboardIsOpen );
  119. }
  120.  
  121. CloseClipboard()  // close clipboard if we have it open
  122. {
  123.    if ( gClipboardIsOpen ) {
  124.       #define ORD_WIN32CLOSECLIPBRD 707
  125.       assert( PMDynamicLink("PMWIN",ORD_WIN32CLOSECLIPBRD,BIT32,CDECL,PMInfo().hab) );
  126.       gClipboardIsOpen = FALSE;
  127.    }
  128. }
  129.  
  130.