[futurebasic] Re: [FB] Append a String to the Clipboard in Carbon

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2002 : Group Archive : Group : All Groups

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Sat, 15 Jun 2002 15:00:36 -0400
Rich Love asked,

>I am trying to append a string to the clipboard (add to the clipboard
>instead of clearing it first. I am running carbon standard basic.
>
>The following routine should only clear the clipboard if boolNewClip=1
>
>Can someone tell me why this routine does not work?
>(or provide a routine that does).


Rich,

This OS X Carbon demo uses clipboard functions you should be able to
pull out and drop into your own code. The only tricky part is the
containers used to beat the 255-byte string limitation.

If you or any on the list has trouble understanding the code, let me
know and I'll forward some abbreviated demos of each function.

Ken

Please watch for constant underscores lost on the Associate server
and e-mail line breaks.

/*

   Simple OS X clipboard functions for copying
   and pasting text using containers to eliminate
   the 255-character string limitation.

   Error checking in this demo is minimal.

   Ken Shmidheiser
   Somerset, KY
   6-15-02

*/

#if ( CarbonLib == 0 ) or ( ndef _appearanceRuntime )
compile shutdown "Compiled as Appearance Compliant with Carbon"
#endif

dim system 3000000

begin globals
dim as container gClipC, gAppendC
end globals

gClipC   = ""
gAppendC = ""

local fn ClearScrap
end fn = fn ClearCurrentScrap

local fn ShowScrapFlavors$
dim as ScrapRef       @ scrap
dim as UInt32         @ kount
dim as long           @ size
dim as str255           outputStr
dim as long             j             
dim as OSStatus         osStatus
dim as ScrapFlavorType  flavor        
xref info(_maxInt) as ScrapFlavorInfo

osStatus = fn GetCurrentScrap( scrap )
osStatus = fn GetScrapFlavorCount( scrap, kount )
outputStr = "Scrap flavors found: " + str$( kount )
long if kount
info = fn NewPtr( kount*sizeof( ScrapFlavorInfo ) )
long if info
osStatus = fn GetScrapFlavorInfoList(scrap,kount,#info)
for j = 0 to kount - 1
osStatus = fn GetScrapFlavorSize( scrap,¬
            info.flavorType(j), size )
outputStr = outputStr + chr$(13) +¬
mki$( info.flavorType(j) )+"  "+str$(size)+" bytes"
next
DisposePtr( info )
end if
end if

end fn = outputStr

local fn GetClipboardText
dim as handle     scrapH
dim as OSStatus   osStatus
dim as long     @ length
dim as ScrapRef @ theScrapRef
dim as str255     clipStr

scrapH = 0
length = 0
osStatus = fn GetCurrentScrap( theScrapRef )
osStatus = fn GetScrapFlavorSize( theScrapRef,¬
            _"TEXT", length )

if length <= 0 then exit fn
scrapH = fn TEscrapHandle
hunlock( scrapH )
SetHandleSize( scrapH, length )
hlock( scrapH )
osStatus = fn GetScrapFlavorData( theScrapRef,¬
            _"TEXT", length, #[scrapH] )
hunlock( scrapH )

gClipC = &scrapH

end fn

local fn TextToClipboard( textH as handle, size as Long )
dim as long       clipH
dim as OSErr      err
dim as pointer  @ dataPtr
dim as ScrapRef @ theScrapRef

clipH = fn ClearScrap
HLock( textH )
err = fn GetCurrentScrap( theScrapRef )
err = fn PutScrapFlavor( theScrapRef, _"TEXT",¬
       _kScrapFlavorMaskNone, size, #[textH] )
HUnlock( textH )

DisposeHandle( textH )

end fn

local fn ContainerToClipboard
dim as long   size
dim as handle textH

textH = [@gClipC]
size = len(gClipC)
long if textH
fn TextToClipboard( textH, size )
end if

end fn

local fn AppendContainerToClipboard

fn GetClipboardText
fn ClearScrap
gClipC = gClipC + gAppendC
fn ContainerToClipboard
gClipC = ""
gAppendC = ""

end fn

local fn buildMenus

apple menu "(Ken's OS X Clipboard Demo..."

menu 1, 0, _enable, "File"

gFBEditSelectAll = _zTrue
edit menu 2

end fn

local fn buildWindow
dim as rect r

setrect( r, 0, 0, 500, 370)
appearance window -1,"Ken's Clipboard Tester",@r,¬
_kDocumentWindowClass, _kWindowStandardFloatingAttributes

def SetWindowBackground( _kThemeActiveDialogBackgroundBrush,_zTrue)

text _applFont, 12
edit = 4
setrect( r, 20, 20, 280, 34)
edit field -1,"What would you like to do?",@r,_statNoframed,_leftJust

setrect( r, 300, 50, 462, 310)
edit field -2,"",@r,_framed,_leftJust
setrect( r, 463, 47, 480, 313)
SCROLL BUTTON -2,0,0,0,0, @r, _scrollOther

setrect( r, 24, 232, 276, 246)
edit field -3,"",@r,_statNoframed,_leftJust

setrect( r, 24, 258, 263, 332)
edit field -4,"",@r,_framed,_leftJust
setrect( r, 263, 256, 278, 334)
SCROLL BUTTON -4,0,0,0,0, @r, _scrollOther


setrect( r, 20, 50, 280, 74)
button 10,1,"View clipboard text",@r,_push

offsetrect( r, 0, 36 )
button 20,1,"Clear clipboard",@r,_push

offsetrect( r, 0, 36 )
button 30,1,"Check clipboard scrap flavors",@r,_push

offsetrect( r, 0, 36 )
button 40,1,"Enter text for clipboard paste",@r,_push

offsetrect( r, 0, 36 )
button 50,1,"Enter text for clipboard append",@r,_push


setrect( r, 300, 330, 480, 350)
button 60,1,"",@r,_push

setrect( r, 300, 330, 480, 350)
button 70,1,"",@r,_push

button -60
button -70

edit field 0

window 1

end fn

local fn ClearButtons
edit$(4) = ""
button -60
button -70
end fn

local fn ViewClipboardText
gClipC = ""
fn ClearButtons
fn GetClipboardText
Edit$(2) = #gClipC
end fn

local fn ClearClipboard
fn ClearButtons
fn ClearScrap
Edit$(2) = ""
end fn

local fn PasteTextToClipboard
fn ClearButtons
edit$(3) = "Please enter text to paste here:"
edit field 4
button 60,1,"Paste text to clipboard"
end fn

local fn AppendTextToClipboard
fn ClearButtons
edit$(3) = "Please enter text to append here:"
edit field 4
button 70,1,"Append text to clipboard"
end fn

local fn CheckFlavors
fn ClearButtons
edit$(2) = fn ShowScrapFlavors$
end fn

local fn DoPaste
gClipC = edit$(4)
fn ContainerToClipboard
fn GetClipboardText
Edit$(2) = #gClipC
end fn

local fn DoAppend
gAppendC = edit$(4)
fn AppendContainerToClipboard
fn GetClipboardText
Edit$(2) = #gClipC
end fn

local fn DoDialog
dim as long evnt, id

evnt = dialog(0)
id = dialog(evnt)

select case( evnt )
case _wndClose
select( id )
case 1 :  gFBQuit = _zTrue
end select

case _btnClick
select( id )
case 10 : fn ViewClipboardText
case 20 : fn ClearClipboard
case 30 : fn CheckFlavors
case 40 : fn PasteTextToClipboard
case 50 : fn AppendTextToClipboard
case 60 : fn DoPaste
case 70 : fn DoAppend
end select

end select

end fn

local fn DoMenu
dim as long menuID, itemID

menuID = menu(_menuID)
itemID = menu(_itemID)

select case( menuID )
case 1
end select
menu

end fn

on dialog fn DoDialog
on menu   fn DoMenu

fn buildMenus
fn buildWindow

do
handleevents
until gFBQuit
end