1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
8 // Copyright: (c) 1998 AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dnd.h"
18 #if wxUSE_DRAG_AND_DROP
21 #include "wx/window.h"
22 #include "wx/toplevel.h"
24 #include "wx/gdicmn.h"
25 #include "wx/mac/private.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 void wxMacEnsureTrackingHandlersInstalled() ;
35 wxWindow
* m_currentTargetWindow
;
36 wxDropTarget
* m_currentTarget
;
37 wxDropSource
* m_currentSource
;
38 } MacTrackingGlobals
;
40 MacTrackingGlobals gTrackingGlobals
;
42 //----------------------------------------------------------------------------
44 //----------------------------------------------------------------------------
46 wxDropTarget::wxDropTarget( wxDataObject
*data
)
47 : wxDropTargetBase( data
)
49 wxMacEnsureTrackingHandlersInstalled() ;
52 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
57 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
60 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
65 return CurrentDragHasSupportedFormat() ;
68 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
74 if (!CurrentDragHasSupportedFormat())
77 return GetData() ? def
: wxDragNone
;
80 bool wxDropTarget::CurrentDragHasSupportedFormat()
82 bool supported
= false ;
83 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
85 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
89 size_t formatcount
= data
->GetFormatCount() ;
90 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
91 data
->GetAllFormats( array
);
92 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
94 wxDataFormat format
= array
[i
] ;
95 if ( m_dataObject
->IsSupported( format
) )
108 CountDragItems((DragReference
)m_currentDrag
, &items
);
109 for (UInt16 index
= 1; index
<= items
&& supported
== false ; ++index
)
111 ItemReference theItem
;
114 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
115 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
116 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
118 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
119 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
130 bool wxDropTarget::GetData()
135 if ( !CurrentDragHasSupportedFormat() )
138 bool transferred
= false ;
139 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
141 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
145 size_t formatcount
= data
->GetFormatCount() ;
146 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
147 data
->GetAllFormats( array
);
148 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
150 wxDataFormat format
= array
[i
] ;
151 if ( m_dataObject
->IsSupported( format
) )
153 int size
= data
->GetDataSize( format
);
158 m_dataObject
->SetData(format
, 0 , 0 ) ;
162 char *d
= new char[size
];
163 data
->GetDataHere( format
, (void*) d
);
164 m_dataObject
->SetData( format
, size
, d
) ;
176 CountDragItems((DragReference
)m_currentDrag
, &items
);
177 for (UInt16 index
= 1; index
<= items
; ++index
)
179 ItemReference theItem
;
182 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
183 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
184 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
186 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
187 wxDataFormat
format(theType
) ;
188 if ( m_dataObject
->IsSupportedFormat( format
) )
190 FlavorFlags theFlags
;
191 result
= GetFlavorFlags((DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
196 GetFlavorDataSize((DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
197 if ( theType
== 'TEXT' )
199 // this increment is only valid for allocating, on the next GetFlavorData
200 // call it is reset again to the original value
203 theData
= new char[dataSize
];
204 GetFlavorData((DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L);
205 if( theType
== 'TEXT' )
207 theData
[dataSize
]=0 ;
208 if ( wxApp::s_macDefaultEncodingIsPC
)
210 wxMacConvertToPC((char*)theData
,(char*)theData
,dataSize
) ;
212 m_dataObject
->SetData( format
, dataSize
, theData
);
214 else if ( theType
== kDragFlavorTypeHFS
)
216 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
217 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
218 ((wxFileDataObject
*)m_dataObject
)->AddFile( name
) ;
222 m_dataObject
->SetData( format
, dataSize
, theData
);
234 //-------------------------------------------------------------------------
236 //-------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
241 wxDropSource::wxDropSource(wxWindow
*win
,
242 const wxIcon
&iconCopy
,
243 const wxIcon
&iconMove
,
244 const wxIcon
&iconNone
)
246 wxMacEnsureTrackingHandlersInstalled() ;
250 wxDropSource::wxDropSource(wxDataObject
& data
,
252 const wxIcon
&iconCopy
,
253 const wxIcon
&iconMove
,
254 const wxIcon
&iconNone
)
256 wxMacEnsureTrackingHandlersInstalled() ;
261 wxDropSource::~wxDropSource()
266 wxDragResult
wxDropSource::DoDragDrop(int WXUNUSED(flags
))
268 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
271 return (wxDragResult
) wxDragNone
;
273 if (m_data
->GetFormatCount() == 0)
274 return (wxDragResult
) wxDragNone
;
277 DragReference theDrag
;
278 RgnHandle dragRegion
;
279 if ((result
= NewDrag(&theDrag
)))
284 size_t formatCount
= m_data
->GetFormatCount() ;
285 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
286 m_data
->GetAllFormats( formats
) ;
287 ItemReference theItem
= 1 ;
288 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
290 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
291 Ptr dataPtr
= new char[dataSize
] ;
292 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
293 OSType type
= formats
[i
].GetFormatId() ;
294 if ( type
== 'TEXT' )
297 if ( wxApp::s_macDefaultEncodingIsPC
)
299 wxMacConvertFromPC((char*)dataPtr
,(char*)dataPtr
,dataSize
) ;
301 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
303 else if (type
== kDragFlavorTypeHFS
)
305 HFSFlavor theFlavor
;
309 wxMacFilename2FSSpec( dataPtr
, &theFlavor
.fileSpec
) ;
311 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
312 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
313 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
314 cat
.hFileInfo
.ioFDirIndex
= 0;
315 err
= PBGetCatInfoSync(&cat
);
318 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
319 if (theFlavor
.fileSpec
.parID
== fsRtParID
) {
320 theFlavor
.fileCreator
= 'MACS';
321 theFlavor
.fileType
= 'disk';
322 } else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0) {
323 theFlavor
.fileCreator
= 'MACS';
324 theFlavor
.fileType
= 'fold';
326 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
327 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
329 AddDragItemFlavor(theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0);
334 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
340 dragRegion
= NewRgn();
341 RgnHandle tempRgn
= NewRgn() ;
343 EventRecord
* ev
= NULL
;
344 #if !TARGET_CARBON // TODO
345 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
349 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
351 const short dragRegionOuterBoundary
= 10 ;
352 const short dragRegionInnerBoundary
= 9 ;
354 SetRectRgn( dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
355 ev
->where
.v
- dragRegionOuterBoundary
,
356 ev
->where
.h
+ dragRegionOuterBoundary
,
357 ev
->where
.v
+ dragRegionOuterBoundary
) ;
359 SetRectRgn( tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
360 ev
->where
.v
- dragRegionInnerBoundary
,
361 ev
->where
.h
+ dragRegionInnerBoundary
,
362 ev
->where
.v
+ dragRegionInnerBoundary
) ;
364 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
365 DisposeRgn( tempRgn
) ;
367 // TODO:work with promises in order to return data only when drag
368 // was successfully completed
370 gTrackingGlobals
.m_currentSource
= this ;
371 result
= TrackDrag(theDrag
, ev
, dragRegion
);
372 DisposeRgn(dragRegion
);
373 DisposeDrag(theDrag
);
374 gTrackingGlobals
.m_currentSource
= NULL
;
379 bool gTrackingGlobalsInstalled
= false ;
381 // passing the globals via refcon is not needed by the CFM and later architectures anymore
382 // but I'll leave it in there, just in case...
384 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
385 void *handlerRefCon
, DragReference theDrag
) ;
386 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
387 DragReference theDrag
) ;
389 void wxMacEnsureTrackingHandlersInstalled()
391 if( !gTrackingGlobalsInstalled
)
395 result
= InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L,&gTrackingGlobals
);
396 wxASSERT( result
== noErr
) ;
397 result
= InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
398 wxASSERT( result
== noErr
) ;
400 gTrackingGlobalsInstalled
= true ;
404 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
405 void *handlerRefCon
, DragReference theDrag
)
407 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
408 Point mouse
, localMouse
;
409 DragAttributes attributes
;
410 GetDragAttributes(theDrag
, &attributes
);
411 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
414 case kDragTrackingEnterHandler
:
416 case kDragTrackingLeaveHandler
:
418 case kDragTrackingEnterWindow
:
419 trackingGlobals
->m_currentTargetWindow
= NULL
;
420 trackingGlobals
->m_currentTarget
= NULL
;
422 case kDragTrackingInWindow
:
423 if (toplevel
== NULL
)
426 GetDragMouse(theDrag
, &mouse
, 0L);
428 GlobalToLocal(&localMouse
);
430 // if (attributes & kDragHasLeftSenderWindow)
432 wxPoint
point(localMouse
.h
, localMouse
.v
) ;
433 wxWindow
*win
= NULL
;
434 toplevel
->MacGetWindowFromPointSub( point
, &win
) ;
435 int localx
, localy
;
436 localx
= localMouse
.h
;
437 localy
= localMouse
.v
;
438 //TODO : should we use client coordinates
440 win
->MacRootWindowToWindow( &localx
, &localy
) ;
441 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
443 if ( trackingGlobals
->m_currentTargetWindow
)
445 // this window is left
446 if ( trackingGlobals
->m_currentTarget
)
448 HideDragHilite(theDrag
);
449 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
450 trackingGlobals
->m_currentTarget
->OnLeave() ;
451 trackingGlobals
->m_currentTarget
= NULL
;
452 trackingGlobals
->m_currentTargetWindow
= NULL
;
457 // this window is entered
458 trackingGlobals
->m_currentTargetWindow
= win
;
459 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
460 if ( trackingGlobals
->m_currentTarget
)
462 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
463 if ( trackingGlobals
->m_currentTarget
->OnEnter(
464 localx
, localy
, wxDragCopy
) != wxDragNone
)
468 win
->MacWindowToRootWindow( &x
, &y
) ;
469 RgnHandle hiliteRgn
= NewRgn() ;
470 SetRectRgn( hiliteRgn
, x
, y
, x
+win
->GetSize().x
,y
+win
->GetSize().y
) ;
471 ShowDragHilite(theDrag
, hiliteRgn
, true);
472 DisposeRgn( hiliteRgn
) ;
479 if( trackingGlobals
->m_currentTarget
)
481 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
482 trackingGlobals
->m_currentTarget
->OnDragOver(
483 localx
, localy
, wxDragCopy
) ;
488 // MyTrackItemUnderMouse(localMouse, theWindow);
490 case kDragTrackingLeaveWindow
:
491 if (trackingGlobals
->m_currentTarget
)
493 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
494 trackingGlobals
->m_currentTarget
->OnLeave() ;
495 HideDragHilite(theDrag
);
496 trackingGlobals
->m_currentTarget
= NULL
;
498 trackingGlobals
->m_currentTargetWindow
= NULL
;
504 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
,
506 DragReference theDrag
)
508 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
509 if ( trackingGlobals
->m_currentTarget
)
511 Point mouse
,localMouse
;
514 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
515 GetDragMouse(theDrag
, &mouse
, 0L);
517 GlobalToLocal(&localMouse
);
518 localx
= localMouse
.h
;
519 localy
= localMouse
.v
;
520 //TODO : should we use client coordinates
521 if ( trackingGlobals
->m_currentTargetWindow
)
522 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
523 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
525 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, wxDragCopy
) ;