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 int 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 int 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 theData
= new char[dataSize
];
200 GetFlavorData((DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L);
201 if( theType
== 'TEXT' )
203 theData
[dataSize
]=0 ;
204 if ( wxApp::s_macDefaultEncodingIsPC
)
206 wxMacConvertToPC((char*)theData
) ;
208 m_dataObject
->SetData( format
, dataSize
, theData
);
210 else if ( theType
== kDragFlavorTypeHFS
)
212 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
213 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
214 m_dataObject
->SetData( format
, name
.Length() + 1, name
) ;
218 m_dataObject
->SetData( format
, dataSize
, theData
);
230 //-------------------------------------------------------------------------
232 //-------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
237 wxDropSource::wxDropSource(wxWindow
*win
,
238 const wxIcon
&iconCopy
,
239 const wxIcon
&iconMove
,
240 const wxIcon
&iconNone
)
242 wxMacEnsureTrackingHandlersInstalled() ;
246 wxDropSource::wxDropSource(wxDataObject
& data
,
248 const wxIcon
&iconCopy
,
249 const wxIcon
&iconMove
,
250 const wxIcon
&iconNone
)
252 wxMacEnsureTrackingHandlersInstalled() ;
257 wxDropSource::~wxDropSource()
262 wxDragResult
wxDropSource::DoDragDrop( bool allowMove
)
264 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
267 return (wxDragResult
) wxDragNone
;
269 if (m_data
->GetFormatCount() == 0)
270 return (wxDragResult
) wxDragNone
;
273 DragReference theDrag
;
274 RgnHandle dragRegion
;
275 if (result
= NewDrag(&theDrag
))
280 size_t formatCount
= m_data
->GetFormatCount() ;
281 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
282 m_data
->GetAllFormats( formats
) ;
283 ItemReference theItem
= 1 ;
284 for ( int i
= 0 ; i
< formatCount
; ++i
)
286 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
287 Ptr dataPtr
= new char[dataSize
] ;
288 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
289 OSType type
= formats
[i
].GetFormatId() ;
290 if ( type
== 'TEXT' )
293 if ( wxApp::s_macDefaultEncodingIsPC
)
295 wxMacConvertFromPC((char*)dataPtr
) ;
297 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
299 else if (type
== kDragFlavorTypeHFS
)
301 HFSFlavor theFlavor
;
305 wxMacFilename2FSSpec( dataPtr
, &theFlavor
.fileSpec
) ;
307 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
308 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
309 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
310 cat
.hFileInfo
.ioFDirIndex
= 0;
311 err
= PBGetCatInfoSync(&cat
);
314 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
315 if (theFlavor
.fileSpec
.parID
== fsRtParID
) {
316 theFlavor
.fileCreator
= 'MACS';
317 theFlavor
.fileType
= 'disk';
318 } else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0) {
319 theFlavor
.fileCreator
= 'MACS';
320 theFlavor
.fileType
= 'fold';
322 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
323 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
325 AddDragItemFlavor(theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0);
330 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
336 dragRegion
= NewRgn();
337 RgnHandle tempRgn
= NewRgn() ;
339 EventRecord
* ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
340 const short dragRegionOuterBoundary
= 10 ;
341 const short dragRegionInnerBoundary
= 9 ;
343 SetRectRgn( dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
344 ev
->where
.v
- dragRegionOuterBoundary
,
345 ev
->where
.h
+ dragRegionOuterBoundary
,
346 ev
->where
.v
+ dragRegionOuterBoundary
) ;
348 SetRectRgn( tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
349 ev
->where
.v
- dragRegionInnerBoundary
,
350 ev
->where
.h
+ dragRegionInnerBoundary
,
351 ev
->where
.v
+ dragRegionInnerBoundary
) ;
353 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
354 DisposeRgn( tempRgn
) ;
356 // TODO:work with promises in order to return data only when drag
357 // was successfully completed
359 gTrackingGlobals
.m_currentSource
= this ;
360 result
= TrackDrag(theDrag
, ev
, dragRegion
);
361 DisposeRgn(dragRegion
);
362 DisposeDrag(theDrag
);
363 gTrackingGlobals
.m_currentSource
= NULL
;
368 bool gTrackingGlobalsInstalled
= false ;
370 // passing the globals via refcon is not needed by the CFM and later architectures anymore
371 // but I'll leave it in there, just in case...
373 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
374 void *handlerRefCon
, DragReference theDrag
) ;
375 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
376 DragReference theDrag
) ;
378 void wxMacEnsureTrackingHandlersInstalled()
380 if( !gTrackingGlobalsInstalled
)
384 result
= InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L,&gTrackingGlobals
);
385 wxASSERT( result
== noErr
) ;
386 result
= InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
387 wxASSERT( result
== noErr
) ;
389 gTrackingGlobalsInstalled
= true ;
393 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
394 void *handlerRefCon
, DragReference theDrag
)
396 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
397 Point mouse
, localMouse
;
398 DragAttributes attributes
;
400 GetDragAttributes(theDrag
, &attributes
);
401 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
404 case kDragTrackingEnterHandler
:
406 case kDragTrackingLeaveHandler
:
408 case kDragTrackingEnterWindow
:
409 trackingGlobals
->m_currentTargetWindow
= NULL
;
410 trackingGlobals
->m_currentTarget
= NULL
;
412 case kDragTrackingInWindow
:
413 if (toplevel
== NULL
)
416 GetDragMouse(theDrag
, &mouse
, 0L);
418 GlobalToLocal(&localMouse
);
420 // if (attributes & kDragHasLeftSenderWindow)
422 wxPoint
point(localMouse
.h
, localMouse
.v
) ;
423 wxWindow
*win
= NULL
;
424 toplevel
->MacGetWindowFromPointSub( point
, &win
) ;
425 int localx
, localy
;
426 localx
= localMouse
.h
;
427 localy
= localMouse
.v
;
428 //TODO : should we use client coordinates
430 win
->MacRootWindowToWindow( &localx
, &localy
) ;
431 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
433 if ( trackingGlobals
->m_currentTargetWindow
)
435 // this window is left
436 if ( trackingGlobals
->m_currentTarget
)
438 HideDragHilite(theDrag
);
439 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
440 trackingGlobals
->m_currentTarget
->OnLeave() ;
441 trackingGlobals
->m_currentTarget
= NULL
;
442 trackingGlobals
->m_currentTargetWindow
= NULL
;
447 // this window is entered
448 trackingGlobals
->m_currentTargetWindow
= win
;
449 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
450 if ( trackingGlobals
->m_currentTarget
)
452 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
453 if ( trackingGlobals
->m_currentTarget
->OnEnter(
454 localx
, localy
, wxDragCopy
) != wxDragNone
)
458 win
->MacWindowToRootWindow( &x
, &y
) ;
459 RgnHandle hiliteRgn
= NewRgn() ;
460 SetRectRgn( hiliteRgn
, x
, y
, x
+win
->GetSize().x
,y
+win
->GetSize().y
) ;
461 ShowDragHilite(theDrag
, hiliteRgn
, true);
462 DisposeRgn( hiliteRgn
) ;
469 if( trackingGlobals
->m_currentTarget
)
471 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
472 trackingGlobals
->m_currentTarget
->OnDragOver(
473 localx
, localy
, wxDragCopy
) ;
478 // MyTrackItemUnderMouse(localMouse, theWindow);
480 case kDragTrackingLeaveWindow
:
481 if (trackingGlobals
->m_currentTarget
)
483 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
484 trackingGlobals
->m_currentTarget
->OnLeave() ;
485 HideDragHilite(theDrag
);
486 trackingGlobals
->m_currentTarget
= NULL
;
488 trackingGlobals
->m_currentTargetWindow
= NULL
;
494 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
495 DragReference theDrag
)
497 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
498 if ( trackingGlobals
->m_currentTarget
)
500 Point mouse
,localMouse
;
503 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
504 GetDragMouse(theDrag
, &mouse
, 0L);
506 GlobalToLocal(&localMouse
);
507 localx
= localMouse
.h
;
508 localy
= localMouse
.v
;
509 //TODO : should we use client coordinates
510 if ( trackingGlobals
->m_currentTargetWindow
)
511 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
512 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
514 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, wxDragCopy
) ;