xref: /AOO41X/main/vcl/aqua/source/dtrans/DropTarget.cxx (revision 2dae3561214a89110eeff04fdd6793d377fb4d04)
19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
27cdf0e10cSrcweir #include <com/sun/star/datatransfer/XTransferable.hpp>
28cdf0e10cSrcweir #include <com/sun/star/datatransfer/dnd/DropTargetDragEnterEvent.hpp>
29cdf0e10cSrcweir #include <rtl/unload.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #ifndef COMPHELPER_MAKESEQUENCE_HXX_INCLUDED
32cdf0e10cSrcweir #include "comphelper/makesequence.hxx"
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "aqua_clipboard.hxx"
37cdf0e10cSrcweir #include "DropTarget.hxx"
38cdf0e10cSrcweir #include "DragActionConversion.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include "DragSource.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <rtl/ustring.h>
43cdf0e10cSrcweir #include <stdio.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <premac.h>
46cdf0e10cSrcweir #include <Carbon/Carbon.h>
47cdf0e10cSrcweir #include <postmac.h>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <aqua/salframe.h>
50cdf0e10cSrcweir #include <aqua/salframeview.h>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using namespace rtl;
53cdf0e10cSrcweir using namespace cppu;
54cdf0e10cSrcweir using namespace osl;
55cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
56cdf0e10cSrcweir using namespace com::sun::star::datatransfer::dnd;
57cdf0e10cSrcweir using namespace com::sun::star::datatransfer::dnd::DNDConstants;
58cdf0e10cSrcweir using namespace com::sun::star::datatransfer::clipboard;
59cdf0e10cSrcweir using namespace com::sun::star::lang;
60cdf0e10cSrcweir using namespace com::sun::star::uno;
61cdf0e10cSrcweir using namespace com::sun::star;
62cdf0e10cSrcweir using namespace comphelper;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir OUString dropTarget_getImplementationName()
65cdf0e10cSrcweir {
66cdf0e10cSrcweir   return OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.datatransfer.dnd.OleDropTarget_V1"));
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir Sequence<OUString> dropTarget_getSupportedServiceNames()
71cdf0e10cSrcweir {
72cdf0e10cSrcweir   return makeSequence(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.datatransfer.dnd.OleDropTarget")));
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir namespace /* private */
77cdf0e10cSrcweir {
78cdf0e10cSrcweir   // Cocoa's coordinate system has its origin lower-left, VCL's
79cdf0e10cSrcweir   // coordinate system upper-left hence we need to transform
80cdf0e10cSrcweir   // coordinates
81cdf0e10cSrcweir 
82cdf0e10cSrcweir   inline void CocoaToVCL(NSPoint& rPoint, const NSRect& bounds)
83cdf0e10cSrcweir   {
84cdf0e10cSrcweir 	rPoint.y = bounds.size.height - rPoint.y;
85cdf0e10cSrcweir   }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir   inline void CocoaToVCL(NSRect& rRect, const NSRect& bounds)
88cdf0e10cSrcweir   {
89cdf0e10cSrcweir 	rRect.origin.y = bounds.size.height - (rRect.origin.y + rRect.size.height);
90cdf0e10cSrcweir   }
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir @implementation DropTargetHelper
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
97cdf0e10cSrcweir -(DropTargetHelper*)initWithDropTarget:(DropTarget*)pdt
98cdf0e10cSrcweir {
99cdf0e10cSrcweir   self = [super init];
100cdf0e10cSrcweir 
101cdf0e10cSrcweir   if (self)
102cdf0e10cSrcweir 	{
103cdf0e10cSrcweir 	  mDropTarget = pdt;
104cdf0e10cSrcweir 	}
105cdf0e10cSrcweir 
106cdf0e10cSrcweir   return self;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 
110cdf0e10cSrcweir -(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
111cdf0e10cSrcweir {
112cdf0e10cSrcweir   return mDropTarget->draggingEntered(sender);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
116cdf0e10cSrcweir -(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
117cdf0e10cSrcweir {
118cdf0e10cSrcweir   return mDropTarget->draggingUpdated(sender);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
122cdf0e10cSrcweir -(void)draggingExited:(id <NSDraggingInfo>)sender
123cdf0e10cSrcweir {
124cdf0e10cSrcweir   mDropTarget->draggingExited(sender);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir -(BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
129cdf0e10cSrcweir {
130cdf0e10cSrcweir   return mDropTarget->prepareForDragOperation(sender);
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 
134cdf0e10cSrcweir -(BOOL)performDragOperation:(id <NSDraggingInfo>)sender
135cdf0e10cSrcweir {
136cdf0e10cSrcweir   return mDropTarget->performDragOperation(sender);
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir -(void)concludeDragOperation:(id <NSDraggingInfo>)sender
141cdf0e10cSrcweir {
142cdf0e10cSrcweir   mDropTarget->concludeDragOperation(sender);
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 
146cdf0e10cSrcweir @end
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 
149cdf0e10cSrcweir DropTarget::DropTarget() :
150cdf0e10cSrcweir   WeakComponentImplHelper5<XInitialization, XDropTarget, XDropTargetDragContext, XDropTargetDropContext, XServiceInfo>(m_aMutex),
151cdf0e10cSrcweir   mView(nil),
152cdf0e10cSrcweir   mpFrame(NULL),
153cdf0e10cSrcweir   mDropTargetHelper(nil),
154cdf0e10cSrcweir   mbActive(false),
155cdf0e10cSrcweir   mDragSourceSupportedActions(DNDConstants::ACTION_NONE),
156cdf0e10cSrcweir   mSelectedDropAction(DNDConstants::ACTION_NONE),
157cdf0e10cSrcweir   mDefaultActions(DNDConstants::ACTION_COPY_OR_MOVE | DNDConstants::ACTION_LINK | DNDConstants::ACTION_DEFAULT)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir   mDataFlavorMapper = DataFlavorMapperPtr_t(new DataFlavorMapper());
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
163cdf0e10cSrcweir DropTarget::~DropTarget()
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     if( AquaSalFrame::isAlive( mpFrame ) )
166cdf0e10cSrcweir         [(id <DraggingDestinationHandler>)mView unregisterDraggingDestinationHandler:mDropTargetHelper];
167cdf0e10cSrcweir     [mDropTargetHelper release];
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 
171cdf0e10cSrcweir sal_Int8 DropTarget::determineDropAction(sal_Int8 dropActions, id sender) const
172cdf0e10cSrcweir {
173cdf0e10cSrcweir   sal_Int8 dropAct = dropActions;
174cdf0e10cSrcweir   bool srcAndDestEqual = false;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir   if ([sender draggingSource] != nil)
177cdf0e10cSrcweir 	{
178cdf0e10cSrcweir 	  // Internal DnD
179cdf0e10cSrcweir 	  NSView* destView = [[sender draggingDestinationWindow] contentView];
180cdf0e10cSrcweir 	  srcAndDestEqual = (DragSource::g_DragSourceView == destView);
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 
183cdf0e10cSrcweir   // If ACTION_DEFAULT is set this means NSDragOperationGeneric
184cdf0e10cSrcweir   // has been set and we map this to ACTION_MOVE or ACTION_COPY
185cdf0e10cSrcweir   // depending on whether or not source and dest are equal,
186cdf0e10cSrcweir   // this hopefully satisfies all parties
187cdf0e10cSrcweir   if( (dropActions == DNDConstants::ACTION_DEFAULT)
188cdf0e10cSrcweir   || ((dropActions == mDragSourceSupportedActions)
189cdf0e10cSrcweir      && !(~mDragSourceSupportedActions & DNDConstants::ACTION_COPY_OR_MOVE ) ) )
190cdf0e10cSrcweir 	{
191cdf0e10cSrcweir 	  dropAct = srcAndDestEqual ? DNDConstants::ACTION_MOVE :
192cdf0e10cSrcweir 		DNDConstants::ACTION_COPY;
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir      // if more than one drop actions have been specified
195cdf0e10cSrcweir      // set ACTION_DEFAULT in order to let the drop target
196cdf0e10cSrcweir      // decide which one to use
197cdf0e10cSrcweir   else if (dropActions != DNDConstants::ACTION_NONE &&
198cdf0e10cSrcweir 		   dropActions != DNDConstants::ACTION_MOVE &&
199cdf0e10cSrcweir 		   dropActions != DNDConstants::ACTION_COPY &&
200cdf0e10cSrcweir 		   dropActions != DNDConstants::ACTION_LINK)
201cdf0e10cSrcweir 	{
202cdf0e10cSrcweir 	  if (srcAndDestEqual)
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir           dropAct = dropActions;
205cdf0e10cSrcweir 		}
206cdf0e10cSrcweir 	  else // source and destination are different
207cdf0e10cSrcweir 		{
208cdf0e10cSrcweir 		  if (dropActions & DNDConstants::ACTION_COPY)
209cdf0e10cSrcweir 			dropAct = DNDConstants::ACTION_COPY;
210cdf0e10cSrcweir 		  else if (dropActions & DNDConstants::ACTION_MOVE)
211cdf0e10cSrcweir 			dropAct = DNDConstants::ACTION_MOVE;
212cdf0e10cSrcweir 		  else if (dropActions & DNDConstants::ACTION_LINK)
213cdf0e10cSrcweir 			dropAct = DNDConstants::ACTION_LINK;
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	  dropAct |= DNDConstants::ACTION_DEFAULT;
217cdf0e10cSrcweir 	}
218cdf0e10cSrcweir 
219cdf0e10cSrcweir   return dropAct;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 
223cdf0e10cSrcweir NSDragOperation DropTarget::draggingEntered(id sender)
224cdf0e10cSrcweir {
225cdf0e10cSrcweir   // Initially when DnD will be started no modifier key can be pressed yet
226cdf0e10cSrcweir   // thus we are getting all actions that the drag source supports, we save
227cdf0e10cSrcweir   // this value because later the system masks the drag source actions if
228cdf0e10cSrcweir   // a modifier key will be pressed
229cdf0e10cSrcweir   mDragSourceSupportedActions = SystemToOfficeDragActions([sender draggingSourceOperationMask]);
230cdf0e10cSrcweir 
231cdf0e10cSrcweir   // Only if the drop target is really interessted in the drag actions
232cdf0e10cSrcweir   // supported by the source
233cdf0e10cSrcweir   if (mDragSourceSupportedActions & mDefaultActions)
234cdf0e10cSrcweir 	{
235cdf0e10cSrcweir 	  sal_Int8 currentAction = determineDropAction(mDragSourceSupportedActions, sender);
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	  NSRect bounds = [mView bounds];
238cdf0e10cSrcweir 	  NSPoint dragLocation = [sender draggedImageLocation];
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 	  CocoaToVCL(dragLocation, bounds);
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	  sal_Int32 posX = static_cast<sal_Int32>(dragLocation.x);
243cdf0e10cSrcweir 	  sal_Int32 posY = static_cast<sal_Int32>(dragLocation.y);
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	  NSPasteboard* dragPboard = [sender draggingPasteboard];
246cdf0e10cSrcweir 	  mXCurrentDragClipboard = new AquaClipboard(dragPboard, false);
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	  uno::Reference<XTransferable> xTransferable = DragSource::g_XTransferable.is() ?
249cdf0e10cSrcweir 		DragSource::g_XTransferable : mXCurrentDragClipboard->getContents();
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 	  DropTargetDragEnterEvent dtdee(static_cast<OWeakObject*>(this),
252cdf0e10cSrcweir 									 0,
253cdf0e10cSrcweir 									 this,
254cdf0e10cSrcweir 									 currentAction,
255cdf0e10cSrcweir 									 posX,
256cdf0e10cSrcweir 									 posY,
257cdf0e10cSrcweir 									 mDragSourceSupportedActions,
258cdf0e10cSrcweir 									 xTransferable->getTransferDataFlavors());
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	  fire_dragEnter(dtdee);
261cdf0e10cSrcweir 	}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir   return OfficeToSystemDragActions(mSelectedDropAction);
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 
267cdf0e10cSrcweir NSDragOperation DropTarget::draggingUpdated(id sender)
268cdf0e10cSrcweir {
269cdf0e10cSrcweir   sal_Int8 currentDragSourceActions =
270cdf0e10cSrcweir 	SystemToOfficeDragActions([sender draggingSourceOperationMask]);
271cdf0e10cSrcweir   NSDragOperation dragOp = NSDragOperationNone;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir   if (currentDragSourceActions & mDefaultActions)
274cdf0e10cSrcweir 	{
275cdf0e10cSrcweir 	  sal_Int8 currentAction = determineDropAction(currentDragSourceActions, sender);
276cdf0e10cSrcweir 	  NSRect bounds = [mView bounds];
277cdf0e10cSrcweir 	  NSPoint dragLocation = [sender draggedImageLocation];
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	  CocoaToVCL(dragLocation, bounds);
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 	  sal_Int32 posX = static_cast<sal_Int32>(dragLocation.x);
282cdf0e10cSrcweir 	  sal_Int32 posY = static_cast<sal_Int32>(dragLocation.y);
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 	  DropTargetDragEvent dtde(static_cast<OWeakObject*>(this),
285cdf0e10cSrcweir 							   0,
286cdf0e10cSrcweir 							   this,
287cdf0e10cSrcweir 							   currentAction,
288cdf0e10cSrcweir 							   posX,
289cdf0e10cSrcweir 							   posY,
290cdf0e10cSrcweir 							   mDragSourceSupportedActions);
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 	  fire_dragOver(dtde);
293cdf0e10cSrcweir 
294cdf0e10cSrcweir       // drag over callbacks likely have rendered something
295cdf0e10cSrcweir       [mView setNeedsDisplay: TRUE];
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	  dragOp = OfficeToSystemDragActions(mSelectedDropAction);
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 	  //NSLog(@"Drag update: Source actions: %x proposed action %x selected action %x", mDragSourceSupportedActions, currentAction, mSelectedDropAction);
300cdf0e10cSrcweir 	}
301cdf0e10cSrcweir 
302cdf0e10cSrcweir   if (dragOp == NSDragOperationNone)
303*2dae3561SHerbert Dürr 	[[NSCursor operationNotAllowedCursor] set];
304cdf0e10cSrcweir   else if (dragOp == NSDragOperationCopy)
305*2dae3561SHerbert Dürr 	[[NSCursor dragCopyCursor] set];
306cdf0e10cSrcweir   else
307*2dae3561SHerbert Dürr 	[[NSCursor arrowCursor] set];
308cdf0e10cSrcweir 
309cdf0e10cSrcweir   return dragOp;
310cdf0e10cSrcweir }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 
313cdf0e10cSrcweir void DropTarget::draggingExited(id /*sender*/)
314cdf0e10cSrcweir {
315cdf0e10cSrcweir 	DropTargetEvent dte(static_cast<OWeakObject*>(this), 0);
316cdf0e10cSrcweir 	fire_dragExit(dte);
317cdf0e10cSrcweir 	mDragSourceSupportedActions = DNDConstants::ACTION_NONE;
318cdf0e10cSrcweir 	mSelectedDropAction = DNDConstants::ACTION_NONE;
319*2dae3561SHerbert Dürr 	[[NSCursor arrowCursor] set];
320cdf0e10cSrcweir }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 
323cdf0e10cSrcweir BOOL DropTarget::prepareForDragOperation(id /*sender*/)
324cdf0e10cSrcweir {
325cdf0e10cSrcweir 	return 1;
326cdf0e10cSrcweir }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 
329cdf0e10cSrcweir BOOL DropTarget::performDragOperation(id sender)
330cdf0e10cSrcweir {
331cdf0e10cSrcweir   bool bSuccess = false;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir   if (mSelectedDropAction != DNDConstants::ACTION_NONE)
334cdf0e10cSrcweir 	{
335cdf0e10cSrcweir 	    uno::Reference<XTransferable> xTransferable = DragSource::g_XTransferable;
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 	  if (!DragSource::g_XTransferable.is())
338cdf0e10cSrcweir 		{
339cdf0e10cSrcweir 		  xTransferable = mXCurrentDragClipboard->getContents();
340cdf0e10cSrcweir 		}
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 	  NSRect bounds = [mView bounds];
343cdf0e10cSrcweir 	  NSPoint dragLocation = [sender draggedImageLocation];
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 	  CocoaToVCL(dragLocation, bounds);
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	  sal_Int32 posX = static_cast<sal_Int32>(dragLocation.x);
348cdf0e10cSrcweir 	  sal_Int32 posY = static_cast<sal_Int32>(dragLocation.y);
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 	  DropTargetDropEvent dtde(static_cast<OWeakObject*>(this),
351cdf0e10cSrcweir 							   0,
352cdf0e10cSrcweir 							   this,
353cdf0e10cSrcweir 							   mSelectedDropAction,
354cdf0e10cSrcweir 							   posX,
355cdf0e10cSrcweir 							   posY,
356cdf0e10cSrcweir 							   mDragSourceSupportedActions,
357cdf0e10cSrcweir 							   xTransferable);
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 	  fire_drop(dtde);
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 	  bSuccess = true;
362cdf0e10cSrcweir 	}
363cdf0e10cSrcweir 
364cdf0e10cSrcweir   return bSuccess;
365cdf0e10cSrcweir }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 
368cdf0e10cSrcweir void DropTarget::concludeDragOperation(id /*sender*/)
369cdf0e10cSrcweir {
370cdf0e10cSrcweir 	mDragSourceSupportedActions = DNDConstants::ACTION_NONE;
371cdf0e10cSrcweir 	mSelectedDropAction = DNDConstants::ACTION_NONE;
372cdf0e10cSrcweir 	mXCurrentDragClipboard = uno::Reference<XClipboard>();
373*2dae3561SHerbert Dürr 	[[NSCursor arrowCursor] set];
374cdf0e10cSrcweir }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 
377cdf0e10cSrcweir   // called from WeakComponentImplHelperX::dispose
378cdf0e10cSrcweir   // WeakComponentImplHelper calls disposing before it destroys
379cdf0e10cSrcweir   // itself.
380cdf0e10cSrcweir   void SAL_CALL DropTarget::disposing()
381cdf0e10cSrcweir   {
382cdf0e10cSrcweir   }
383cdf0e10cSrcweir 
384cdf0e10cSrcweir 
385cdf0e10cSrcweir   void SAL_CALL DropTarget::initialize(const Sequence< Any >& aArguments)
386cdf0e10cSrcweir 	throw(Exception)
387cdf0e10cSrcweir   {
388cdf0e10cSrcweir 	if (aArguments.getLength() < 2)
389cdf0e10cSrcweir 	  {
390cdf0e10cSrcweir 		throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM("DropTarget::initialize: Cannot install window event handler")),
391cdf0e10cSrcweir 							   static_cast<OWeakObject*>(this));
392cdf0e10cSrcweir 	  }
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 	Any pNSView = aArguments[0];
395cdf0e10cSrcweir 	sal_uInt64 tmp = 0;
396cdf0e10cSrcweir 	pNSView >>= tmp;
397cdf0e10cSrcweir 	mView = (id)tmp;
398cdf0e10cSrcweir 	mpFrame = [(SalFrameView*)mView getSalFrame];
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 	mDropTargetHelper = [[DropTargetHelper alloc] initWithDropTarget: this];
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 	[(id <DraggingDestinationHandler>)mView registerDraggingDestinationHandler:mDropTargetHelper];
403cdf0e10cSrcweir 	[mView registerForDraggedTypes: mDataFlavorMapper->getAllSupportedPboardTypes()];
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 	id wnd = [mView window];
406cdf0e10cSrcweir 	NSWindow* parentWnd = [wnd parentWindow];
407cdf0e10cSrcweir 	unsigned int topWndStyle = (NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask);
408cdf0e10cSrcweir 	unsigned int wndStyles = [wnd styleMask] & topWndStyle;
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 	if (parentWnd == nil && (wndStyles == topWndStyle))
411cdf0e10cSrcweir 	  {
412cdf0e10cSrcweir 		[wnd registerDraggingDestinationHandler:mDropTargetHelper];
413cdf0e10cSrcweir 		[wnd registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
414cdf0e10cSrcweir 	  }
415cdf0e10cSrcweir   }
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 
418cdf0e10cSrcweir   void SAL_CALL DropTarget::addDropTargetListener(const uno::Reference<XDropTargetListener>& dtl)
419cdf0e10cSrcweir 	throw(RuntimeException)
420cdf0e10cSrcweir   {
421cdf0e10cSrcweir 	rBHelper.addListener(::getCppuType(&dtl), dtl);
422cdf0e10cSrcweir   }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 
425cdf0e10cSrcweir   void SAL_CALL DropTarget::removeDropTargetListener(const uno::Reference<XDropTargetListener>& dtl)
426cdf0e10cSrcweir 	throw(RuntimeException)
427cdf0e10cSrcweir   {
428cdf0e10cSrcweir 	rBHelper.removeListener(::getCppuType(&dtl), dtl);
429cdf0e10cSrcweir   }
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 
432cdf0e10cSrcweir   sal_Bool SAL_CALL DropTarget::isActive(  ) throw(RuntimeException)
433cdf0e10cSrcweir   {
434cdf0e10cSrcweir 	return mbActive;
435cdf0e10cSrcweir   }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 
438cdf0e10cSrcweir   void SAL_CALL DropTarget::setActive(sal_Bool active) throw(RuntimeException)
439cdf0e10cSrcweir   {
440cdf0e10cSrcweir 	mbActive = active;
441cdf0e10cSrcweir   }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 
444cdf0e10cSrcweir   sal_Int8 SAL_CALL DropTarget::getDefaultActions() throw(RuntimeException)
445cdf0e10cSrcweir   {
446cdf0e10cSrcweir 	return mDefaultActions;
447cdf0e10cSrcweir   }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 
450cdf0e10cSrcweir   void SAL_CALL DropTarget::setDefaultActions(sal_Int8 actions) throw(RuntimeException)
451cdf0e10cSrcweir   {
452cdf0e10cSrcweir 	OSL_ENSURE( actions < 8, "No valid default actions");
453cdf0e10cSrcweir 	mDefaultActions= actions;
454cdf0e10cSrcweir   }
455cdf0e10cSrcweir 
456cdf0e10cSrcweir 
457cdf0e10cSrcweir   // XDropTargetDragContext
458cdf0e10cSrcweir 
459cdf0e10cSrcweir   void SAL_CALL DropTarget::acceptDrag(sal_Int8 dragOperation) throw (RuntimeException)
460cdf0e10cSrcweir   {
461cdf0e10cSrcweir 	mSelectedDropAction = dragOperation;
462cdf0e10cSrcweir   }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 
465cdf0e10cSrcweir   void SAL_CALL DropTarget::rejectDrag() throw (RuntimeException)
466cdf0e10cSrcweir   {
467cdf0e10cSrcweir 	mSelectedDropAction = DNDConstants::ACTION_NONE;
468cdf0e10cSrcweir   }
469cdf0e10cSrcweir 
470cdf0e10cSrcweir 
471cdf0e10cSrcweir   //XDropTargetDropContext
472cdf0e10cSrcweir 
473cdf0e10cSrcweir   void SAL_CALL DropTarget::acceptDrop(sal_Int8 dropOperation) throw( RuntimeException)
474cdf0e10cSrcweir   {
475cdf0e10cSrcweir 	mSelectedDropAction = dropOperation;
476cdf0e10cSrcweir   }
477cdf0e10cSrcweir 
478cdf0e10cSrcweir 
479cdf0e10cSrcweir   void SAL_CALL DropTarget::rejectDrop() throw (RuntimeException)
480cdf0e10cSrcweir   {
481cdf0e10cSrcweir 	mSelectedDropAction = DNDConstants::ACTION_NONE;
482cdf0e10cSrcweir   }
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 
485cdf0e10cSrcweir   void SAL_CALL DropTarget::dropComplete(sal_Bool success) throw (RuntimeException)
486cdf0e10cSrcweir   {
487cdf0e10cSrcweir 	// Reset the internal transferable used as shortcut in case this is
488cdf0e10cSrcweir 	// an internal D&D operation
489cdf0e10cSrcweir 	DragSource::g_XTransferable = uno::Reference<XTransferable>();
490cdf0e10cSrcweir     DragSource::g_DropSuccessSet = true;
491cdf0e10cSrcweir     DragSource::g_DropSuccess = success;
492cdf0e10cSrcweir   }
493cdf0e10cSrcweir 
494cdf0e10cSrcweir 
495cdf0e10cSrcweir   void DropTarget::fire_drop( const DropTargetDropEvent& dte)
496cdf0e10cSrcweir   {
497cdf0e10cSrcweir       OInterfaceContainerHelper* pContainer= rBHelper.getContainer( getCppuType( (uno::Reference<XDropTargetListener>* )0 ) );
498cdf0e10cSrcweir 	if( pContainer)
499cdf0e10cSrcweir 	  {
500cdf0e10cSrcweir 		OInterfaceIteratorHelper iter( *pContainer);
501cdf0e10cSrcweir 		while( iter.hasMoreElements())
502cdf0e10cSrcweir 		  {
503cdf0e10cSrcweir 		      uno::Reference<XDropTargetListener> listener( static_cast<XDropTargetListener*>( iter.next()));
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 			try { listener->drop( dte); }
506cdf0e10cSrcweir 			catch(RuntimeException&) {}
507cdf0e10cSrcweir 		  }
508cdf0e10cSrcweir 	  }
509cdf0e10cSrcweir   }
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 
512cdf0e10cSrcweir   void DropTarget::fire_dragEnter(const DropTargetDragEnterEvent& e)
513cdf0e10cSrcweir   {
514cdf0e10cSrcweir       OInterfaceContainerHelper* pContainer= rBHelper.getContainer( getCppuType( (uno::Reference<XDropTargetListener>* )0 ) );
515cdf0e10cSrcweir 	if( pContainer)
516cdf0e10cSrcweir 	  {
517cdf0e10cSrcweir 		OInterfaceIteratorHelper iter( *pContainer);
518cdf0e10cSrcweir 		while( iter.hasMoreElements())
519cdf0e10cSrcweir 		  {
520cdf0e10cSrcweir 		      uno::Reference<XDropTargetListener> listener( static_cast<XDropTargetListener*>( iter.next()));
521cdf0e10cSrcweir 
522cdf0e10cSrcweir 			try { listener->dragEnter( e); }
523cdf0e10cSrcweir 			catch (RuntimeException&) {}
524cdf0e10cSrcweir 		  }
525cdf0e10cSrcweir 	  }
526cdf0e10cSrcweir   }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 
529cdf0e10cSrcweir   void DropTarget::fire_dragExit(const DropTargetEvent& dte)
530cdf0e10cSrcweir   {
531cdf0e10cSrcweir       OInterfaceContainerHelper* pContainer= rBHelper.getContainer( getCppuType( (uno::Reference<XDropTargetListener>* )0 ) );
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 	if( pContainer)
534cdf0e10cSrcweir 	  {
535cdf0e10cSrcweir 		OInterfaceIteratorHelper iter( *pContainer);
536cdf0e10cSrcweir 		while( iter.hasMoreElements())
537cdf0e10cSrcweir 		  {
538cdf0e10cSrcweir 		      uno::Reference<XDropTargetListener> listener( static_cast<XDropTargetListener*>( iter.next()));
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 			try { listener->dragExit( dte); }
541cdf0e10cSrcweir 			catch (RuntimeException&) {}
542cdf0e10cSrcweir 		  }
543cdf0e10cSrcweir 	  }
544cdf0e10cSrcweir   }
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 
547cdf0e10cSrcweir   void DropTarget::fire_dragOver(const DropTargetDragEvent& dtde)
548cdf0e10cSrcweir   {
549cdf0e10cSrcweir       OInterfaceContainerHelper* pContainer= rBHelper.getContainer( getCppuType( (uno::Reference<XDropTargetListener>* )0 ) );
550cdf0e10cSrcweir 	if( pContainer)
551cdf0e10cSrcweir 	  {
552cdf0e10cSrcweir 		OInterfaceIteratorHelper iter( *pContainer );
553cdf0e10cSrcweir 		while( iter.hasMoreElements())
554cdf0e10cSrcweir 		  {
555cdf0e10cSrcweir 		      uno::Reference<XDropTargetListener> listener( static_cast<XDropTargetListener*>( iter.next()));
556cdf0e10cSrcweir 
557cdf0e10cSrcweir 			try { listener->dragOver( dtde); }
558cdf0e10cSrcweir 			catch (RuntimeException&) {}
559cdf0e10cSrcweir 		  }
560cdf0e10cSrcweir 	  }
561cdf0e10cSrcweir   }
562cdf0e10cSrcweir 
563cdf0e10cSrcweir 
564cdf0e10cSrcweir   void DropTarget::fire_dropActionChanged(const DropTargetDragEvent& dtde)
565cdf0e10cSrcweir   {
566cdf0e10cSrcweir       OInterfaceContainerHelper* pContainer= rBHelper.getContainer( getCppuType( (uno::Reference<XDropTargetListener>* )0 ) );
567cdf0e10cSrcweir 	if( pContainer)
568cdf0e10cSrcweir 	  {
569cdf0e10cSrcweir 		OInterfaceIteratorHelper iter( *pContainer);
570cdf0e10cSrcweir 		while( iter.hasMoreElements())
571cdf0e10cSrcweir 		  {
572cdf0e10cSrcweir 		      uno::Reference<XDropTargetListener> listener( static_cast<XDropTargetListener*>( iter.next()));
573cdf0e10cSrcweir 
574cdf0e10cSrcweir 			try { listener->dropActionChanged( dtde); }
575cdf0e10cSrcweir 			catch (RuntimeException&) {}
576cdf0e10cSrcweir 		  }
577cdf0e10cSrcweir 	  }
578cdf0e10cSrcweir   }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 
581cdf0e10cSrcweir   // XServiceInfo
582cdf0e10cSrcweir 
583cdf0e10cSrcweir   OUString SAL_CALL DropTarget::getImplementationName() throw (RuntimeException)
584cdf0e10cSrcweir   {
585cdf0e10cSrcweir 	return dropTarget_getImplementationName();
586cdf0e10cSrcweir   }
587cdf0e10cSrcweir 
588cdf0e10cSrcweir 
589cdf0e10cSrcweir   sal_Bool SAL_CALL DropTarget::supportsService( const OUString& ServiceName ) throw (RuntimeException)
590cdf0e10cSrcweir   {
591cdf0e10cSrcweir 	return ServiceName.equals(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.datatransfer.dnd.OleDropTarget")));
592cdf0e10cSrcweir   }
593cdf0e10cSrcweir 
594cdf0e10cSrcweir 
595cdf0e10cSrcweir   Sequence< OUString > SAL_CALL DropTarget::getSupportedServiceNames(  ) throw (RuntimeException)
596cdf0e10cSrcweir   {
597cdf0e10cSrcweir 	return dropTarget_getSupportedServiceNames();
598cdf0e10cSrcweir   }
599cdf0e10cSrcweir 
600