It would be amazing if we had functions in PB to set the clipboard to some kind of custom data.
SetClipboardData(Type.s, Data)
GetClipboardData()
GetClipboardDataType()
It would also be useful to check to see if the clipboard is empty.
I don't know how possible all of this is (wxWidgets and Qt do it, but I've no clue about the behind-the-scenes work, especially it being three different platforms), but I would truly love to see it done. It's one of the very few things that holds me back from using PureBasic all the time.
Custom data to clipboard
Re: Custom data to clipboard
Hi wombats. I'm not sure about the other platforms, but intrinsically, the Windows Clipboard only handles text and bitmaps, so setting custom data types may not be possible. However, text strings copied from the clipboard can be parsed and evaluated to determine their types. For example:wombats wrote:It would be amazing if we had functions in PB to set the clipboard to some kind of custom data.
SetClipboardData(Type.s, Data)
GetClipboardData()
GetClipboardDataType()
It would also be useful to check to see if the clipboard is empty...
Code: Select all
; simple routine to determine clipboard data type
; incomplete evaluations - for example purposes only
; negatives, hexadecimals, etc. not evaluated
cbText.s = Trim(GetClipboardText())
If cbText
MessageRequester("Clipboard Status:", "Text: " + cbText)
For L = 1 To Len(cbText)
If (Asc(Mid(cbText, L, 1)) < 48 Or Asc(Mid(cbText, L, 1)) > 57)
If Asc(Mid(cbText, L, 1)) = 46
pointFlag + 1
If pointFlag < 2
Continue
EndIf
EndIf
MessageRequester("Clipboard Status:", "Clipboard contains non-numerical value")
End
EndIf
Next L
If pointFlag
MessageRequester("Clipboard Status:", "Clipboard data type: Float Or Double")
;do type coversions here
Else
MessageRequester("Clipboard Status:", "Clipboard data type: other numerical types")
;do further evaluations here
EndIf
EndIf
There's clearly no single function to do this in PureBasic, but a zero result with GetClipboardImage() and a null string result with GetClipboardText() can pretty much confirm that the clipboard is empty:wombats wrote:It would also be useful to check to see if the clipboard is empty.
Code: Select all
Procedure.i IsClipboardEmpty()
If GetClipboardImage(#PB_Any) = 0 And GetClipboardText() = #NULL$
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
If IsClipboardEmpty()
MessageRequester("Clipboard Status:", "Clipboard is empty...")
Else
MessageRequester("Clipboard Status:", "Clipboard is not empty...")
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Custom data to clipboard
Hi,
Thanks for the reply. If the Windows clipboard only supports text and bitmaps, how do other applications handle copying and pasting their own types between instances?
Thanks.
Thanks for the reply. If the Windows clipboard only supports text and bitmaps, how do other applications handle copying and pasting their own types between instances?
Thanks.
Re: Custom data to clipboard
On Windows, you are able to register own clipboard formats.wombats wrote:If the Windows clipboard only supports text and bitmaps, how do other applications handle copying and pasting their own types between instances?
Quick code (not debugged):
Code: Select all
Procedure SetClipboardData(Type.s, *theData, theDataLen)
If OpenClipboard_(0)
format = RegisterClipboardFormat_(@Type)
If format
hMem = GlobalAlloc_(#GMEM_MOVEABLE|#GMEM_DDESHARE,theDataLen)
If hMem
mem = GlobalLock_(hMem)
If mem
CopyMemory(*theData,mem,theDataLen)
If SetClipboardData_(format,hMem)
result = format
EndIf
GlobalUnlock_(hMem)
EndIf
EndIf
EndIf
CloseClipboard_()
EndIf
ProcedureReturn result
EndProcedure
Procedure GetClipboardFormat(Type.s)
ProcedureReturn RegisterClipboardFormat_(@Type)
EndProcedure
Procedure GetClipboardData(format)
If OpenClipboard_(0)
hMem = GetClipboardData_(format)
If hMem
dataSize = GlobalSize_(hMem)
src = GlobalLock_(hMem)
If src
mem = AllocateMemory( dataSize )
If mem
CopyMemory(src,mem,dataSize)
EndIf
GlobalUnlock_(hMem)
EndIf
EndIf
CloseClipboard_()
EndIf
ProcedureReturn mem
EndProcedure
quad.q = $8899AABBCCDDEEFF
format = SetClipboardData("MyClipboardFormat",@quad,8)
Debug format
Debug GetClipboardFormat("MyClipboardFormat")
Debug "-----"
If format
mem = GetClipboardData(format)
Debug mem
If mem
size = MemorySize(mem)
Debug size
If size
size - 1
Debug "-----"
For i = 0 To size
Debug Hex(PeekA(mem+i),#PB_Ascii)
Next
EndIf
FreeMemory(mem)
EndIf
EndIf
Last edited by Danilo on Thu Nov 08, 2012 11:14 am, edited 2 times in total.
Re: Custom data to clipboard
Hello again wombats. Such applications handle their own cut, copy & paste operations. They would intercept these operations and process the required evaluations before allowing a subsequent paste. Even Microsoft Office has its own exclusive Office Clipboard that has far greater capabilities than the standard Windows Clipboard.wombats wrote:If the Windows clipboard only supports text and bitmaps, how do other applications handle copying and pasting their own types between instances?
Hi Danilo. As always, nice code. However, the RegisterClipboardFormat() function actually registers proprietary formats known only to our own family of apps. We're still pretty much bound to text & bitmaps when dealing with common clipboard content, unless we intercept and process them first.Danilo wrote:On Windows, you are able to register own clipboard formats.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Custom data to clipboard
Of course. That's how I understood wombats:TI-994A wrote:However, the RegisterClipboardFormat() function actually registers proprietary formats known only to our own family of apps.
wombats wrote:how do other applications handle copying and pasting their own types between instances?
Re: Custom data to clipboard
Broad question, but you're right; RegisterClipboardFormat() would fit that requirement.Danilo wrote:Of course. That's how I understood wombats:wombats wrote:how do other applications handle copying and pasting their own types between instances?
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 

Re: Custom data to clipboard
I guess I was confused about the whole clipboard thing. Thanks to all of you for the information, and the code, Danilo and TI-994A.