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 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 ((wxFileDataObject
*)m_dataObject
)->AddFile( 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(int WXUNUSED(flags
))
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 ( size_t 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
;
399 GetDragAttributes(theDrag
, &attributes
);
400 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
403 case kDragTrackingEnterHandler
:
405 case kDragTrackingLeaveHandler
:
407 case kDragTrackingEnterWindow
:
408 trackingGlobals
->m_currentTargetWindow
= NULL
;
409 trackingGlobals
->m_currentTarget
= NULL
;
411 case kDragTrackingInWindow
:
412 if (toplevel
== NULL
)
415 GetDragMouse(theDrag
, &mouse
, 0L);
417 GlobalToLocal(&localMouse
);
419 // if (attributes & kDragHasLeftSenderWindow)
421 wxPoint
point(localMouse
.h
, localMouse
.v
) ;
422 wxWindow
*win
= NULL
;
423 toplevel
->MacGetWindowFromPointSub( point
, &win
) ;
424 int localx
, localy
;
425 localx
= localMouse
.h
;
426 localy
= localMouse
.v
;
427 //TODO : should we use client coordinates
429 win
->MacRootWindowToWindow( &localx
, &localy
) ;
430 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
432 if ( trackingGlobals
->m_currentTargetWindow
)
434 // this window is left
435 if ( trackingGlobals
->m_currentTarget
)
437 HideDragHilite(theDrag
);
438 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
439 trackingGlobals
->m_currentTarget
->OnLeave() ;
440 trackingGlobals
->m_currentTarget
= NULL
;
441 trackingGlobals
->m_currentTargetWindow
= NULL
;
446 // this window is entered
447 trackingGlobals
->m_currentTargetWindow
= win
;
448 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
449 if ( trackingGlobals
->m_currentTarget
)
451 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
452 if ( trackingGlobals
->m_currentTarget
->OnEnter(
453 localx
, localy
, wxDragCopy
) != wxDragNone
)
457 win
->MacWindowToRootWindow( &x
, &y
) ;
458 RgnHandle hiliteRgn
= NewRgn() ;
459 SetRectRgn( hiliteRgn
, x
, y
, x
+win
->GetSize().x
,y
+win
->GetSize().y
) ;
460 ShowDragHilite(theDrag
, hiliteRgn
, true);
461 DisposeRgn( hiliteRgn
) ;
468 if( trackingGlobals
->m_currentTarget
)
470 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
471 trackingGlobals
->m_currentTarget
->OnDragOver(
472 localx
, localy
, wxDragCopy
) ;
477 // MyTrackItemUnderMouse(localMouse, theWindow);
479 case kDragTrackingLeaveWindow
:
480 if (trackingGlobals
->m_currentTarget
)
482 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
483 trackingGlobals
->m_currentTarget
->OnLeave() ;
484 HideDragHilite(theDrag
);
485 trackingGlobals
->m_currentTarget
= NULL
;
487 trackingGlobals
->m_currentTargetWindow
= NULL
;
493 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
,
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
) ;