[futurebasic] Re: [FB] Using The Clipboard

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2011 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Mon, 28 Mar 2011 14:53:23 +1300
Warren Lanford wrote:

> I would like to know :
> The Include Files necessary to use these options:


None.

> The commands to place TEXT on, and retrieve TEXT from, the clipboard would also be of great help.

See PutClipboardTextP () and GetClipboardTextH(). Note that PutClipboardTextP() takes pointer and length parameters, not a Handle.

'--------------------
// Put data from textP into scrap as 'TEXT' flavor
// Returns a non-zero error code if anything went wrong
local mode
local fn PutClipboardTextP( txtP as ptr, nBytes as long )
'~'1
dim as OSStatus  err : err = _paramErr
dim as ScrapRef  scrap   

long if ( txtP != 0 and nBytes > 0 )
fn ClearCurrentScrap()
fn GetCurrentScrap( @scrap )
err = fn PutScrapFlavor( scrap, _"TEXT", _kScrapFlavorMaskNone, nBytes, #txtP )
end if
end fn = err


// Returns new handle containing text from scrap.
// Caller should dispose of the handle after use.
// If no text in scrap, or any other error, returns 0 (no handle created)
local mode
local fn GetClipboardTextH as Handle
'~'1
dim as OSStatus  err     
dim as ScrapRef  scrap
dim as Handle    txtH : txtH = 0
dim as long      nBytes        

fn GetCurrentScrap( @scrap )
err = fn GetScrapFlavorSize( scrap, _"TEXT", @nBytes )
if ( err != _noErr or nBytes == 0 ) then exit fn // no data on scrap
txtH = fn NewHandle( nBytes )
long if ( txtH )
err = fn GetScrapFlavorData( scrap, _"TEXT", nBytes, #[txtH] )
if ( err ) then fn DisposeH( txtH ) // dump the handle we created
end if
end fn = txtH



> The commands necessary to put an image on the Clipboard.
> The commands to retrieve an image from the Clipboard in order to Paste it into an existing image or Gworld.

Other contributors are trying to persuade you to adopt the newer Pasteboard API, while suggesting that you simultaneously replace all your QuickDraw code by CoreGraphics. Phew, what a lot of work...
It's true that the PICT format is now a quaint anachronism (for example, Preview.app no longer offers PICT in the Save As dialog). But QuickDraw is still with us. You may prefer (at least for now) to keep your present drawing code and use PutClipboardPictH() and GetClipboardPictH() to communicate with the clipboard.


// Put data from pictH into scrap as 'PICT' flavor
// Returns a non-zero error code if anything went wrong
local mode
local fn PutClipboardPictH( pictH as Handle )
'~'1
dim as OSStatus  err : err = _paramErr
dim as ScrapRef  scrap   
dim as long      nBytes

long if ( pictH )
fn ClearCurrentScrap()
fn GetCurrentScrap( @scrap )
nBytes = fn GetHandleSize( pictH )
err = fn PutScrapFlavor( scrap, _"PICT", _kScrapFlavorMaskNone, nBytes, #[pictH] )
end if
end fn = err


// Returns new handle containing PICT from scrap.
// Caller should dispose of the handle after use.
// If no PICT in scrap, or any other error, returns 0 (no handle created)
local mode
Local fn GetClipboardPictH as Handle
'~'1
dim as OSStatus  err
dim as ScrapRef  scrap
dim as Handle    pictH : pictH = 0
dim as long      nBytes

fn GetCurrentScrap( @scrap )
err = fn GetScrapFlavorSize( scrap, _"PICT", @nBytes )
if ( err != _noErr or nBytes == 0 ) then exit fn // no data on scrap
pictH = fn NewHandle( nBytes )
long if ( pictH )
err = fn GetScrapFlavorData( scrap, _"PICT", nBytes, #[pictH] )
if ( err ) then fn DisposeH( pictH ) // dump the handle we created
end if
end fn = pictH
'--------------------

Robert P.