Clipboard data

BlitzMax Forums/BlitzMax Programming/Clipboard data

Is it at all possible to read and write data from the clipboard without the gui module?

I just need to enable cut/copy/paste from within a windowed app under Windows and Linux.

TIA

Windows version:
' Clipboard Text - Copy/Paste functions

Strict

Extern "Win32"
	Function OpenClipboard%(hwnd%)
	Function CloseClipboard%()
	Function EmptyClipboard%()
	Function IsClipboardFormatAvailable%(format%)
	Function GetClipboardData:Byte Ptr(Format:Int)
	Function SetClipboardData(format%, hMem:Byte Ptr)
	Function GlobalAlloc(Flags:Int, Bytes:Int)
	Function GlobalFree(Mem:Int)
	Function GlobalLock:Byte Ptr(Mem:Int)
	Function GlobalUnlock(Mem:Int)
End Extern 

' -----------------------------------------------

Function TextFromClipboard:String()
	Const CF_TEXT%=$1
	If Not OpenClipboard(0)	Return ""
	Local TextBuf:Byte Ptr = GetClipboardData(CF_TEXT)
	CloseClipboard()
	Return String.FromCString(TextBuf)
End Function 
	
Function TextToClipboard(txt:String)
	Const CF_TEXT%=$1
	Const GMEM_MOVEABLE%=$2
	Const GMEM_DDESHARE%=$2000
	If txt$="" Return
	Local TextBuf:Byte Ptr = Txt.ToCString()
	Local Memblock:Int = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, txt.Length+1)
	Local DataBuf:Byte Ptr = GlobalLock(Memblock)
	MemCopy DataBuf, TextBuf, Txt.length
	If OpenClipboard(0)
		EmptyClipboard
		SetClipboardData CF_TEXT, DataBuf
		CloseClipboard
	EndIf
	GlobalUnlock Memblock
	GlobalFree Memblock
End Function


' ********************************************


' test

Print "Clipboard Test." 
Print "===============" 
Print "Enter a message for the clipboard." 
Print "Alternatively, leave BLANK to read clipboard." 

Local a:String

a=Input$(">") 

If a="" 
	a=TextFromClipboard() 
	Print a
Else 
	TextToClipboard a
	Print "Text sent to clipboard. Open NotePad and paste!"
EndIf

Print Chr$(13)+"---------------------------------"

a=Input$("Press RETURN to end ...") 

End