[api-dev] Copy textstring to Clipboard <SOLVED>

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[api-dev] Copy textstring to Clipboard <SOLVED>

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|



Reply | Threaded
Open this post in threaded view
|

Re: [api-dev] Copy textstring to Clipboard <SOLVED>

Steffen Boersig-2
  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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [api-dev] Copy textstring to Clipboard <SOLVED>

Ariel Constenla-Haile
Hello steb,

On Thursday 02 December 2010, 09:44, steb wrote:
>   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)

this is Java! so why are you using the dispatch API?
Look at the Developer's Guide
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Common_Application_Features
and the example in the OOo SDK
</opt/openoffice.org/basis3.4/>sdk/examples/DevelopersGuide/OfficeDev/Clipboard/

Regards
--
Ariel Constenla-Haile
La Plata, Argentina

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [api-dev] Copy textstring to Clipboard <SOLVED>

Fernand Vanrie
In reply to this post by Steffen Boersig-2
Steffen,

I learned only to use the dispatcher when there is no API code available
:-). (performance they say)
Java is not realy my cup of tea, i trie to translate to Basic, i was
also told that for  some documents there is no Viewcursor (draw)
The bigest advantage of using the SystemClipboard instance stuff is the
fact that the code can been  been used without opening any document.

I comeback when translated to Basic

Thanks for your respons

Fernand


>  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.:
>
>     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: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [api-dev] Copy textstring to Clipboard <SOLVED>

Fernand Vanrie
In reply to this post by Steffen Boersig-2
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: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [api-dev] Copy textstring to Clipboard <SOLVED>

Steffen Boersig-2
In reply to this post by Ariel Constenla-Haile
  Hello Ariel,

Am 02.12.2010 14:03, schrieb Ariel Constenla-Haile:

> Hello steb,
>
> On Thursday 02 December 2010, 09:44, steb wrote:
>>    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)
> this is Java! so why are you using the dispatch API?
> Look at the Developer's Guide
> http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Common_Application_Features
> and the example in the OOo SDK
> </opt/openoffice.org/basis3.4/>sdk/examples/DevelopersGuide/OfficeDev/Clipboard/
>
> Regards
I'm sorry for not answering sooner, but I had some extended vacation :)
. Well at least from work, had to study for my exams anyway ;).
But back on topic:

I do use this approach because I didn't find information on how to copy
complex cell ranges with formatting inside the cells etc. All the
examples I found where for text/strings only. But that's not something
of interest for me.
I need a method to copy a whole table row with different content types,
for example XTables, XTextFields (with different formatting like size or
color) in it.

The only thing I could come up with, was the dispatch API since that can
copy selections "as is" instead of merely copying strings. The forum ,
SDK and the wiki couldn't show me a way to use the SystemClipboard with
more complex data structures. (or I wasn't capable of recognizing how to
use it properply.)
If you know how to copy for example XCellRanges with all content
"untouched" inside of it, without the dispatch API , I would love to
know how since my code is really executing poorly if used on a few
hundred/thousand copy actions.

@ Fernand
Thank you for sharing your code, but it's the same problem as described
above: it's just copying strings, not generally XSelectable Objects.

Regards,

Steffen Börsig

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]