Page 1 of 1
Custom data to clipboard
Posted: Thu Nov 08, 2012 4:28 am
by wombats
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.
Re: Custom data to clipboard
Posted: Thu Nov 08, 2012 9:53 am
by TI-994A
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...
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:
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
wombats wrote:It would also be useful to check to see if the clipboard is empty.
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:
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
It's not much, but at least it's a start. And cross-platform too!
Re: Custom data to clipboard
Posted: Thu Nov 08, 2012 10:05 am
by wombats
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.
Re: Custom data to clipboard
Posted: Thu Nov 08, 2012 10:48 am
by Danilo
wombats wrote:If the Windows clipboard only supports text and bitmaps, how do other applications handle copying and pasting their own types between instances?
On Windows, you are able to register own clipboard formats.
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
Re: Custom data to clipboard
Posted: Thu Nov 08, 2012 10:49 am
by TI-994A
wombats wrote:If the Windows clipboard only supports text and bitmaps, how do other applications handle copying and pasting their own types between instances?
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.
Danilo wrote:On Windows, you are able to register own clipboard formats.
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.
Re: Custom data to clipboard
Posted: Thu Nov 08, 2012 11:17 am
by Danilo
TI-994A wrote:However, the RegisterClipboardFormat() function actually registers proprietary formats known only to our own family of apps.
Of course. That's how I understood wombats:
wombats wrote:how do other applications handle copying and pasting their own types between instances?
Re: Custom data to clipboard
Posted: Thu Nov 08, 2012 11:30 am
by TI-994A
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?
Broad question, but you're right; RegisterClipboardFormat() would fit that requirement.
Re: Custom data to clipboard
Posted: Sat Nov 10, 2012 6:10 am
by wombats
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.