1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) 1998 Stefan Csomor
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 bool firstFileAdded
= false ;
177 CountDragItems((DragReference
)m_currentDrag
, &items
);
178 for (UInt16 index
= 1; index
<= items
; ++index
)
180 ItemReference theItem
;
183 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
184 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
185 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
187 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
188 wxDataFormat
format(theType
) ;
189 if ( m_dataObject
->IsSupportedFormat( format
) )
191 FlavorFlags theFlags
;
192 result
= GetFlavorFlags((DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
197 GetFlavorDataSize((DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
198 if ( theType
== 'TEXT' )
200 // this increment is only valid for allocating, on the next GetFlavorData
201 // call it is reset again to the original value
204 theData
= new char[dataSize
];
205 GetFlavorData((DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L);
206 if( theType
== 'TEXT' )
208 theData
[dataSize
]=0 ;
209 if ( wxApp::s_macDefaultEncodingIsPC
)
211 wxMacConvertToPC((char*)theData
,(char*)theData
,dataSize
) ;
213 m_dataObject
->SetData( format
, dataSize
, theData
);
215 else if ( theType
== kDragFlavorTypeHFS
)
217 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
218 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
219 if ( firstFileAdded
)
220 ((wxFileDataObject
*)m_dataObject
)->AddFile( name
) ;
223 ((wxFileDataObject
*)m_dataObject
)->SetData( 0 , name
.c_str() ) ;
224 firstFileAdded
= true ;
229 m_dataObject
->SetData( format
, dataSize
, theData
);
241 //-------------------------------------------------------------------------
243 //-------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
248 wxDropSource::wxDropSource(wxWindow
*win
,
249 const wxCursor
&cursorCopy
,
250 const wxCursor
&cursorMove
,
251 const wxCursor
&cursorStop
)
252 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
254 wxMacEnsureTrackingHandlersInstalled() ;
258 wxDropSource::wxDropSource(wxDataObject
& data
,
260 const wxCursor
&cursorCopy
,
261 const wxCursor
&cursorMove
,
262 const wxCursor
&cursorStop
)
263 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
265 wxMacEnsureTrackingHandlersInstalled() ;
270 wxDropSource::~wxDropSource()
275 wxDragResult
wxDropSource::DoDragDrop(int WXUNUSED(flags
))
277 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
280 return (wxDragResult
) wxDragNone
;
282 if (m_data
->GetFormatCount() == 0)
283 return (wxDragResult
) wxDragNone
;
286 DragReference theDrag
;
287 RgnHandle dragRegion
;
288 if ((result
= NewDrag(&theDrag
)))
293 size_t formatCount
= m_data
->GetFormatCount() ;
294 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
295 m_data
->GetAllFormats( formats
) ;
296 ItemReference theItem
= 1 ;
297 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
299 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
300 Ptr dataPtr
= new char[dataSize
] ;
301 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
302 OSType type
= formats
[i
].GetFormatId() ;
303 if ( type
== 'TEXT' )
306 if ( wxApp::s_macDefaultEncodingIsPC
)
308 wxMacConvertFromPC((char*)dataPtr
,(char*)dataPtr
,dataSize
) ;
310 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
312 else if (type
== kDragFlavorTypeHFS
)
314 HFSFlavor theFlavor
;
318 wxMacFilename2FSSpec( dataPtr
, &theFlavor
.fileSpec
) ;
320 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
321 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
322 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
323 cat
.hFileInfo
.ioFDirIndex
= 0;
324 err
= PBGetCatInfoSync(&cat
);
327 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
328 if (theFlavor
.fileSpec
.parID
== fsRtParID
) {
329 theFlavor
.fileCreator
= 'MACS';
330 theFlavor
.fileType
= 'disk';
331 } else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0) {
332 theFlavor
.fileCreator
= 'MACS';
333 theFlavor
.fileType
= 'fold';
335 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
336 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
338 AddDragItemFlavor(theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0);
343 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
349 dragRegion
= NewRgn();
350 RgnHandle tempRgn
= NewRgn() ;
352 EventRecord
* ev
= NULL
;
353 #if !TARGET_CARBON // TODO
354 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
358 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
360 const short dragRegionOuterBoundary
= 10 ;
361 const short dragRegionInnerBoundary
= 9 ;
363 SetRectRgn( dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
364 ev
->where
.v
- dragRegionOuterBoundary
,
365 ev
->where
.h
+ dragRegionOuterBoundary
,
366 ev
->where
.v
+ dragRegionOuterBoundary
) ;
368 SetRectRgn( tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
369 ev
->where
.v
- dragRegionInnerBoundary
,
370 ev
->where
.h
+ dragRegionInnerBoundary
,
371 ev
->where
.v
+ dragRegionInnerBoundary
) ;
373 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
374 DisposeRgn( tempRgn
) ;
376 // TODO:work with promises in order to return data only when drag
377 // was successfully completed
379 gTrackingGlobals
.m_currentSource
= this ;
380 result
= TrackDrag(theDrag
, ev
, dragRegion
);
381 DisposeRgn(dragRegion
);
382 DisposeDrag(theDrag
);
383 gTrackingGlobals
.m_currentSource
= NULL
;
388 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
390 const wxCursor
& cursor
= GetCursor(effect
);
393 cursor
.MacInstall() ;
403 bool gTrackingGlobalsInstalled
= false ;
405 // passing the globals via refcon is not needed by the CFM and later architectures anymore
406 // but I'll leave it in there, just in case...
408 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
409 void *handlerRefCon
, DragReference theDrag
) ;
410 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
411 DragReference theDrag
) ;
413 void wxMacEnsureTrackingHandlersInstalled()
415 if( !gTrackingGlobalsInstalled
)
419 result
= InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L,&gTrackingGlobals
);
420 wxASSERT( result
== noErr
) ;
421 result
= InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
422 wxASSERT( result
== noErr
) ;
424 gTrackingGlobalsInstalled
= true ;
428 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
429 void *handlerRefCon
, DragReference theDrag
)
431 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
432 Point mouse
, localMouse
;
433 DragAttributes attributes
;
434 GetDragAttributes(theDrag
, &attributes
);
435 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
438 case kDragTrackingEnterHandler
:
440 case kDragTrackingLeaveHandler
:
442 case kDragTrackingEnterWindow
:
443 trackingGlobals
->m_currentTargetWindow
= NULL
;
444 trackingGlobals
->m_currentTarget
= NULL
;
446 case kDragTrackingInWindow
:
447 if (toplevel
== NULL
)
450 GetDragMouse(theDrag
, &mouse
, 0L);
452 GlobalToLocal(&localMouse
);
454 // if (attributes & kDragHasLeftSenderWindow)
456 wxPoint
point(localMouse
.h
, localMouse
.v
) ;
457 wxWindow
*win
= NULL
;
458 toplevel
->MacGetWindowFromPointSub( point
, &win
) ;
459 int localx
, localy
;
460 localx
= localMouse
.h
;
461 localy
= localMouse
.v
;
462 //TODO : should we use client coordinates
464 win
->MacRootWindowToWindow( &localx
, &localy
) ;
465 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
467 if ( trackingGlobals
->m_currentTargetWindow
)
469 // this window is left
470 if ( trackingGlobals
->m_currentTarget
)
472 HideDragHilite(theDrag
);
473 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
474 trackingGlobals
->m_currentTarget
->OnLeave() ;
475 trackingGlobals
->m_currentTarget
= NULL
;
476 trackingGlobals
->m_currentTargetWindow
= NULL
;
481 // this window is entered
482 trackingGlobals
->m_currentTargetWindow
= win
;
483 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
485 wxDragResult result
= wxDragNone
;
486 if ( trackingGlobals
->m_currentTarget
)
488 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
489 result
= trackingGlobals
->m_currentTarget
->OnEnter(
490 localx
, localy
, wxDragCopy
) ;
493 if ( trackingGlobals
->m_currentSource
&& trackingGlobals
->m_currentSource
->GiveFeedback( result
) == FALSE
)
495 if ( trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) == FALSE
)
501 wxCursor
cursor(wxCURSOR_COPY_ARROW
) ;
502 cursor
.MacInstall() ;
507 wxCursor
cursor(wxCURSOR_ARROW
) ;
508 cursor
.MacInstall() ;
513 wxCursor
cursor(wxCURSOR_NO_ENTRY
) ;
514 cursor
.MacInstall() ;
521 if ( result
!= wxDragNone
)
525 win
->MacWindowToRootWindow( &x
, &y
) ;
526 RgnHandle hiliteRgn
= NewRgn() ;
527 SetRectRgn( hiliteRgn
, x
, y
, x
+win
->GetSize().x
,y
+win
->GetSize().y
) ;
528 ShowDragHilite(theDrag
, hiliteRgn
, true);
529 DisposeRgn( hiliteRgn
) ;
536 if( trackingGlobals
->m_currentTarget
)
538 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
539 trackingGlobals
->m_currentTarget
->OnDragOver(
540 localx
, localy
, wxDragCopy
) ;
545 // MyTrackItemUnderMouse(localMouse, theWindow);
547 case kDragTrackingLeaveWindow
:
548 if (trackingGlobals
->m_currentTarget
)
550 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
551 trackingGlobals
->m_currentTarget
->OnLeave() ;
552 HideDragHilite(theDrag
);
553 trackingGlobals
->m_currentTarget
= NULL
;
555 trackingGlobals
->m_currentTargetWindow
= NULL
;
561 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
,
563 DragReference theDrag
)
565 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
566 if ( trackingGlobals
->m_currentTarget
)
568 Point mouse
,localMouse
;
571 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
572 GetDragMouse(theDrag
, &mouse
, 0L);
574 GlobalToLocal(&localMouse
);
575 localx
= localMouse
.h
;
576 localy
= localMouse
.v
;
577 //TODO : should we use client coordinates
578 if ( trackingGlobals
->m_currentTargetWindow
)
579 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
580 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
582 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, wxDragCopy
) ;