Drag and drop into OpenOffice.org Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
jpatokal
Posts: 6
Joined: Fri Mar 26, 2010 6:03 am

Drag and drop into OpenOffice.org Writer

Post by jpatokal »

Simple task: I want to drag and drop "something" from an external source (HTML5 Web app) into an OpenOffice Writer document, so it turns into a ConditionalText element. I have full control over what "something" will be. From what I've been able to figure out so far, the flow should be like this:

1) Register a XDropTargetListener on my target document window
2) Wait until I get a DropTargetDropEvent
3) Extract what I need from the event and manipulate the document

First question is, how do I get the listener set up? Apparently I need to go through XDataTransferProviderAccess and do something like this:

Code: Select all

css::uno::Reference< css::awt::XDataTransferProviderAccess > xTransfer( xFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.awt.Toolkit") ), css::uno::UNO_QUERY );
if( xTransfer.is() ) {
  css::uno::Reference< css::datatransfer::dnd::XDropTarget > xDropTarget = xTransfer->getDropTarget( xFrame->getContainerWindow() );
  if( xDropTarget.is() ) {
    xDropTarget->addDropTargetListener( xDropListener );
    xDropTarget->setActive( sal_True );
  }
}
That's C++ and copied from here, but I presume converting it to Java won't be too hard. Since I'm doing this from an extension, is the xFrame I need the one I get here on initialization?

Code: Select all

public synchronized void initialize( Object[] args ) throws com.sun.star.uno.Exception {        
  xFrame = ( XFrame )UnoRuntime.queryInterface( XFrame.class, args[0] );
}
Once I do get the DropTargetDropEvent, how do I signal that I'm handling this event (instead of the default handler) and how do I map the LocationX/LocationY coordinates into a place in the document?

A pointer to some existing code would be lovely...
OpenOffice 3.1 on Ubuntu 9.10
jpatokal
Posts: 6
Joined: Fri Mar 26, 2010 6:03 am

Re: Drag and drop into OpenOffice Writer

Post by jpatokal »

Well, I ported the code over to Java:

Code: Select all

    public synchronized void initialize( Object[] args ) throws com.sun.star.uno.Exception 
            ...
            System.out.println("Registering DropTargetListener...");
            XMultiComponentFactory xmcf = m_xContext.getServiceManager();
            Object oMSF =
                xmcf.createInstanceWithContext(
                "com.sun.star.lang.MultiServiceFactory",
                m_xContext);
            XMultiServiceFactory xmsf = (XMultiServiceFactory) UnoRuntime.queryInterface ( XMultiServiceFactory.class, oMSF);
            XDataTransferProviderAccess xdtp = (XDataTransferProviderAccess) UnoRuntime.queryInterface(XDataTransferProviderAccess.class, xmsf.createInstance("com.sun.star.awt.Toolkit"));
            if (xdtp != null)
            {
               XDropTarget xDropTarget = xdtp.getDropTarget(m_xFrame.getContainerWindow());
               xDropTarget.addDropTargetListener(new MyDropTargetListener());
               xDropTarget.setActive(true);
               System.out.println("DropTargetListener registered.");
            }
But while the DropTargetListener is registered fine, it doesn't actually get any events. If the post below is to be believed, this is on purpose, and you need to go trawling through the window hierarchy through some bizarre accessibility UI loophole and registers listeners on everything in sight:

http://www.openoffice.org/servlets/Read ... sgNo=17623

Am I missing something, or is this really so complicated? :ucrazy:
OpenOffice 3.1 on Ubuntu 9.10
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Drag and drop into OpenOffice Writer

Post by ms777 »

Hi,

have a look into http://www.oooforum.org/forum/viewtopic.phtml?t=82016 . Maybe that helps a bit ...

Good luck,

ms777
Simon Castle
Posts: 4
Joined: Mon May 18, 2020 10:44 am

Re: Drag and drop into OpenOffice.org Writer

Post by Simon Castle »

Is there any new information or solution to this. I'm trying to do the same thing, without success.

Thanks.
... Simon
OpenOffice 2.4 on Ubuntu 9.04
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Drag and drop into OpenOffice.org Writer

Post by JeJe »

Well, that's two of you in ten years. And none of the links work any more...

In Basic, as that's what I know, and quickly looking at the above code and the listener page,

https://www.openoffice.org/api/docs/com ... tener.html

and knowing that the main Writer window is found through the accessible context as w1 below,

The listener works as I get the msgbox.

Code: Select all


REM  *****  BASIC  *****

Sub Main

w1 = thiscomponent.CurrentController.frame.componentwindow.accessiblecontext.getaccessiblechild(0).windows(0) '.windows(0
dt =thiscomponent.CurrentController.frame.containerwindow.gettoolkit.getdroptarget(w1) 
DDListener = CreateUnoListener("DDListener_","com.sun.star.datatransfer.dnd.XDropTargetListener")
dt.addDropTargetListener(DDListener)
end sub


function DDListener_drop(ev) ' 	The drag operation has terminated with a drop on this drop target.  
msgbox 4
end function
sub DDListener_dragEnter(ev) ' 	Called when a drag operation has encountered the drop target.  
end sub
sub DDListener_dragExit(ev) ' 	The drag operation has departed the drop target without dropping.  
end sub
sub DDListener_dragOver(ev) ' 	Called when a drag operation is ongoing on the drop target.  
end sub
sub DDListener_dropActionChanged(ev) '
end sub
sub DDListener_disposing(ev)
end sub
EDIT: i SHOULD HAVE PUT DISPOSING, NOT DISPOSE, CORRECTED
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Drag and drop into OpenOffice.org Writer

Post by JeJe »

I should have put disposing, not dispose - corrected, so the listener closes properly.

It looks like you'll need to use the window's accessiblecontext again with the x,y event coordinates to work out where the drop is - unless the transfer is unique enough to find in another way, such as a search/find. You get at least read access to the transfer data/types. The listener seems to work fine and is stable.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
lorsch
Posts: 6
Joined: Mon Sep 21, 2020 2:10 pm

Re: Drag and drop into OpenOffice.org Writer

Post by lorsch »

I am currently facing a similar problem, this thread helped me to get started. Registering drops in a writer document as it is described here works fine, however I am trying to register drops into a cell of a spreadsheet document.
The listener that is described here registers fine, but can't register any drops into cells of the spreadsheet document. To which object could I add the listener so that it can register drops to a specific cell / a cell range / the spreadsheet or which method could help me to find it out by myself?
LibreOffice 6.3.6.2 on Windows Server 2019
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Drag and drop into OpenOffice.org Writer

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Drag and drop into OpenOffice.org Writer

Post by JeJe »

lorsh: I took my earlier code and went... 0... 1... 2... and found I got the message box on 7.

I don't know if that index will vary. I suggest MRI and examining the windows for a unique way of finding that one... I couldn't see an obvious one. Its accessible role is PANEL = 40, with no children, and it will likely be the biggest window.

Code: Select all

    w1 = thiscomponent.CurrentController.frame.componentwindow.accessiblecontext.getaccessiblechild(0).windows(7)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
lorsch
Posts: 6
Joined: Mon Sep 21, 2020 2:10 pm

Re: Drag and drop into OpenOffice.org Writer

Post by lorsch »

Thanks, this worked!

I haven't had much success with MRI, even after reading the guide I am still not sure how I should use it.

I tried editing the string I dropped with my macro as soon as my listener reacted, but I realised acceptdrop() from the xDropTargetContext Interface needed to be called first, otherwise the macro would run before the string would end up in the cell.
This did not work out for me, so I called a modify listener from the drop() method and removed it as soon as a modification of a cell would happen. Not the most elegant way, but it works now.
LibreOffice 6.3.6.2 on Windows Server 2019
Post Reply