Custom data to clipboard

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Custom data to clipboard

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Custom data to clipboard

Post 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!
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 :D
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: Custom data to clipboard

Post 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.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Custom data to clipboard

Post 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
Last edited by Danilo on Thu Nov 08, 2012 11:14 am, edited 2 times in total.
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Custom data to clipboard

Post 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.
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 :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Custom data to clipboard

Post 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?
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Custom data to clipboard

Post 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.
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 :D
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: Custom data to clipboard

Post 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.
Post Reply