Re: Copy textstring to Clipboard <SOLVED>

Fernand Vanrie <[email protected]> Fri, 03 Dec 2010 09:08:39 +0100
Newsgroups gmane.comp.openoffice.devel.api
Message-ID <[email protected]>
Steffen ,

No need to translate, before it worked with the SystemClipboard instance
i uses this sub to copy from in a WRITERdoc, who is basicly what you are 
doing.

Sub xCopyToClipboard(mytext as string)
dim document   as object
dim dispatcher as object
dim oDoc, oVC, Mark, var
oVC = ThisComponent.CurrentController.getViewCursor

If Not isEmpty(oVC.TextTable) then  ' Cursor is in een Tabel
otext = oVC.texttable.getcellbyname(oVC.cell.cellname,true).text
textincell = oText.string
oText.string = ""
otext.insertstring(oVC,mytext,false)
oVC.gotostart(true)
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, 
".uno:Copy", "", 0, Array())
otext.string = textincell ' oude text terug zetten !
else
If Not isEmpty(oVC.TextFrame)then
oVC.String = mytext
Mark = oVC.TextFrame.text.createTextCursorbyRange(oVC)
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, 
".uno:Copy", "", 0, Array())
'oVC.gotostart(true)
oVC.String = ""
else
'gewoon in de text
oVC.String = mytext
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(ThisComponent.CurrentController.Frame, 
".uno:Copy", "", 0, Array())
oVC.String = ""
endif
endif

>  Hi Fernand,
>
> i wonder which method to copy contents is recommended. I achieve the 
> same but I use a different approach with XSelectionSupplier in 
> combination with XDispatchHelper and the XTextViewCursor. Code is 
> similar to:
>
>     public void copyContent(XCellRange cellRange, XTextRange textRange)
>     {
>         // Get current controller
>         XController currController = this.xModel.getCurrentController();
>
>         // Get selection supplier
>         XSelectionSupplier xSelectionSupplier = 
> (XSelectionSupplier)UnoRuntime.queryInterface(XSelectionSupplier.class, currController); 
>
>
>             // Select the cell range constructed above
>             
> xSelectionSupplier.select(AnyConverter.toObject(XCellRange.class, 
> cellRange));  // Here you could throw in any (selectable) object you 
> want to copy!
>             // Get Dispatch Helper to dispatch commands
>             XFrame xFrame = xModel.getCurrentController().getFrame();
>             XDispatchProvider xDispatchProvider = (XDispatchProvider) 
> UnoRuntime.queryInterface(
>                     XDispatchProvider.class, xFrame);
>
>             Object dispatchHelperObject = 
> this.xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.DispatchHelper", 
> this.xComponentContext);
>             XDispatchHelper xDispatchHelper = 
> (com.sun.star.frame.XDispatchHelper) UnoRuntime.queryInterface(
>                 com.sun.star.frame.XDispatchHelper.class, 
> dispatchHelperObject);
>
>             // Excute copy command
>             xDispatchHelper.executeDispatch(xDispatchProvider, 
> ".uno:Copy", "", 0, new PropertyValue[] { new PropertyValue() });
>
>             // IMPORTANT!! the textViewCursor has to be set AFTER the 
> uno:copy command!!!
>             // We have the target selection
>             
> this.xTextViewCursorSupplier.getViewCursor().gotoRange(textRange, false);
>
>             // Perform Paste command
>             xDispatchHelper.executeDispatch(xDispatchProvider, 
> ".uno:Paste", "", 0, new PropertyValue[] { new PropertyValue() });
>     }
>
> I encounter a serious performance issue if i use this approach for a 
> few hundred cell ranges. Maybe your approach doesn't suffer from this. 
> A comment from the devs would be great on which method is recommended 
> for copying stuff around.
>
> Greetings,
> Steffen
>
> Am 02.12.2010 12:15, schrieb Fernand Vanrie:
>> Somewhere on the web (http://hermione.s41.xrea.com/pukiwiki/) i found 
>> finaly the correct Basic code to copy a text to the clipboard without 
>> opening ghidden docs etc...
>> I hope its usefull for others as well
>>
>> Fernand
>>
>> |Global sTxtCString AsString
>>
>> Sub  clipboard_1
>>   sText ="123456"
>>   CopyToClipBoard(sText)
>> End  Sub
>>
>> Sub  CopyToClipBoard( sText )
>>   ' 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 = sText
>>   'oClip.flushClipboard() ' does not work
>> End  Sub
>>
>> Function  Tr_getTransferData(aFlavor as 
>> com.sun.star.datatransfer.DataFlavor)
>>   If  (aFlavor.MimeType ="text/plain;charset=utf-16")Then
>>     Tr_getTransferData() = sTxtCString
>>   End  If
>> End  Function
>>
>> Function  Tr_getTransferDataFlavors()
>>   Dim  aFlavor As new com.sun.star.datatransfer.DataFlavor
>>   aFlavor.MimeType ="text/plain;charset=utf-16"
>>   aFlavor.HumanPresentableName ="Unicode-Text"
>>   Tr_getTransferDataFlavors() = array(aFlavor)
>> End  Function
>>
>> Function  Tr_isDataFlavorSupported(aFlavor as 
>> com.sun.star.datatransfer.DataFlavor) as Boolean
>>   If  aFlavor.MimeType ="text/plain;charset=utf-16"  Then
>>     Tr_isDataFlavorSupported = true
>>   Else
>>     Tr_isDataFlavorSupported = false
>>   End  If
>> End  Function|
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]