[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 22:19:59 -0400
Rich wrote,

>I appreciate the information but I am running carbon standard basic (not
>appearance compliant and the routine you supplied will not work.

Rich,

Try this modification. Runs here in  Carbon/Standard BASIC.

The code you posted will limit you to 255k strings. This uses containers.

Ken


/*

   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.

   Modified to run in Carbon/Standard BASIC

   Ken Shmidheiser
   Somerset, KY
   6-15-02

*/

dim system 3000000

begin globals
dim as container gClipC, gAppendC
end globals

gClipC   = ""
gAppendC = ""

local fn ClearScrap
end fn = fn ClearCurrentScrap

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 )

end fn = scrapH

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 Clipboard Demo..."

menu 1, 0, _enable, "File"
menu 1, 1, _enable, "Quit"

edit menu 2

end fn

local fn buildWindow
dim as rect r

setrect( r, 0, 0, 500, 370)
window -1,"Ken's Clipboard Tester",@r,_docNoGrow

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, 60 )
button 20,1,"Clear clipboard",@r,_push

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

setrect( r, 300, 330, 480, 350)
button 40,0,"Append text to clipboard",@r,_push

edit field 0

window 1

end fn

local fn ClearButtons
edit$(4) = ""
button 40,0
end fn

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

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

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

local fn DoAppend
gAppendC = edit$(4)
fn AppendContainerToClipboard
fn GetClipboardText
gClipC = &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 AppendTextToClipboard
case 40 : 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 : gFBQuit = _zTrue
end select
menu

end fn

on dialog fn DoDialog
on menu   fn DoMenu

fn buildMenus
fn buildWindow

do
handleevents
until gFBQuit
end