Page 1 of 1

Drag and drop into OpenOffice.org Writer

PostPosted: Tue Apr 27, 2010 6:09 am
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   Expand viewCollapse view
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   Expand viewCollapse view
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...

Re: Drag and drop into OpenOffice Writer

PostPosted: Wed Apr 28, 2010 7:59 am
by jpatokal
Well, I ported the code over to Java:
Code: Select all   Expand viewCollapse view
    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:

Re: Drag and drop into OpenOffice Writer

PostPosted: Sat May 01, 2010 1:33 pm
by ms777
Hi,

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

Good luck,

ms777

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Mon May 18, 2020 10:48 am
by Simon Castle
Is there any new information or solution to this. I'm trying to do the same thing, without success.

Thanks.
... Simon

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Mon May 18, 2020 4:02 pm
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   Expand viewCollapse view

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

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Mon May 18, 2020 6:25 pm
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.

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Mon Sep 21, 2020 2:24 pm
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?

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Mon Sep 21, 2020 3:13 pm
by Villeroy

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Tue Sep 22, 2020 10:29 pm
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   Expand viewCollapse view
    w1 = thiscomponent.CurrentController.frame.componentwindow.accessiblecontext.getaccessiblechild(0).windows(7)

Re: Drag and drop into OpenOffice.org Writer

PostPosted: Fri Oct 02, 2020 3:51 pm
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.