In the original code, MimeStr was the constant "text/plain;charset=utf-16" and HumanStr was "Unicode-Text". I've made them variables so that I could play around with other options. Both ClipStr() and sTxtCString() are Byte arrays, as this is required for rich text (and plain text as well.) I first ask the user what he wants to do with the copied string (a special single-line representation of a chess position. An example is:)
rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1
I set the values of MimeStr and HumanStr depending upon how the user wants to use it. In the special case where he wants to use it as an RTF string, I first wrap the string in the minimum text required to make it an RTF 'file'. I then convert the string to a simple 8-bit ASCII string. In the last function, the Mime string is kept as a constant, "text/richtext", regardless of whether we want to use plain text or RTF. (My guess is that RTF is just a higher order of plain text, and this is what allows us to commandeer it and simply alter its MimeType to get plain text.)
Now, if I copy the string to the clipboard as plain text, I can paste it into NotePad and ChessBase, but NOT into Writer itself. Alternatively, if I copy the string as RTF, then I can paste it into Writer or WordPad, but NOT ChessBase.
What I'd like to ask is if anyone can help to further modify the code below so that Writer can also understand the plain text. For example, if you copy some text directly from Writer, then click on the small down arrow of the Paste Icon, you'll see a number of paste options, including Unformatted Text. Does this use a special MimeType string? Alternatively, I wonder if it would be possible to copy both instances on the clipboard. Any ideas?
Here is the modified code:
Code: Select all
Global sTxtCString() As Byte
Global ClipStr(120) As Byte
Global MimeStr, HumanStr As String
Sub CopyToClipBoard(sText)
Dim i, j, cnum As Integer
' Ask the user what he wants to do with the copied string (a FEN)
i = MsgBox("Are you using the FEN as plain text (ChessBase)?", 4+32, "Copy FEN")
If i = 6 Then ' Yes -- I'll use it as 8-bit plain text for an external application
MimeStr = "text/plain"
HumanStr = "Text"
Else ' No -- make it an RTF string so that I can use it in Writer (or WordPad, etc.)
MimeStr = "text/richtext"
HumanStr = "Rich Text Format"
sText = "{\rtf1\ansi " & sText & "}" ' wrap the plain text with the minimum needed for an RTF file
End If
' Convert the string to a simple 8-bit ASCII string instead of a 16-bit char string
j = Len(sText)
i = 0
Do While i < j
ClipStr(i) = CByte(Asc(Mid(sText, i + 1, 1)))
i = i + 1
Loop
ClipStr(i) = 0 ' Mark the end of the string
' create SystemClipboard instance
oClip = CreateUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
oTR = createUnoListener("Tr_", "com.sun.star.datatransfer.XTransferable")
' set data
oClip.setContents(oTR,Null)
sTxtCString = ClipStr
End Sub
Function Tr_getTransferData(aFlavor as com.sun.star.datatransfer.DataFlavor)
If (aFlavor.MimeType = MimeStr) Then
Tr_getTransferData() = sTxtCString
End If
End Function
Function Tr_getTransferDataFlavors()
Dim aFlavor As new com.sun.star.datatransfer.DataFlavor
aFlavor.MimeType = MimeStr
aFlavor.HumanPresentableName = HumanStr
Tr_getTransferDataFlavors() = array(aFlavor)
End Function
Function Tr_isDataFlavorSupported(aFlavor as com.sun.star.datatransfer.DataFlavor) as Boolean
If aFlavor.MimeType = "text/richtext" Then ' Note that we stick to "text/richtext" here!
Tr_isDataFlavorSupported = true
Else
Tr_isDataFlavorSupported = false
End If
End Function