1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget, wxDropSource implementations
4 // Author: Stefan Csomor
8 // Copyright: (c) 1998 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if wxUSE_DRAG_AND_DROP
17 #include "wx/window.h"
18 #include "wx/toplevel.h"
20 #include "wx/gdicmn.h"
21 #include "wx/mac/private.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
34 wxWindow
*m_currentTargetWindow
;
35 wxDropTarget
*m_currentTarget
;
36 wxDropSource
*m_currentSource
;
40 MacTrackingGlobals gTrackingGlobals
;
42 void wxMacEnsureTrackingHandlersInstalled();
44 //----------------------------------------------------------------------------
46 //----------------------------------------------------------------------------
48 wxDropTarget::wxDropTarget( wxDataObject
*data
)
49 : wxDropTargetBase( data
)
51 wxMacEnsureTrackingHandlersInstalled();
54 wxDragResult
wxDropTarget::OnDragOver(
55 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
58 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
61 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
63 if (m_dataObject
== NULL
)
66 return CurrentDragHasSupportedFormat();
69 wxDragResult
wxDropTarget::OnData(
70 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
73 if (m_dataObject
== NULL
)
76 if (!CurrentDragHasSupportedFormat())
79 return GetData() ? def
: wxDragNone
;
82 bool wxDropTarget::CurrentDragHasSupportedFormat()
84 bool supported
= false;
86 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
88 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
92 size_t formatcount
= data
->GetFormatCount();
93 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
94 data
->GetAllFormats( array
);
95 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
97 wxDataFormat format
= array
[i
];
98 if ( m_dataObject
->IsSupported( format
) )
113 ItemReference theItem
;
117 CountDragItems( (DragReference
)m_currentDrag
, &items
);
118 for (UInt16 index
= 1; index
<= items
&& !supported
; ++index
)
121 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
122 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
124 for ( UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
126 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
127 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
139 bool wxDropTarget::GetData()
141 if (m_dataObject
== NULL
)
144 if ( !CurrentDragHasSupportedFormat() )
147 bool transferred
= false;
148 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
150 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
154 size_t formatcount
= data
->GetFormatCount();
155 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
156 data
->GetAllFormats( array
);
157 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
159 wxDataFormat format
= array
[i
];
160 if ( m_dataObject
->IsSupported( format
) )
162 int size
= data
->GetDataSize( format
);
167 m_dataObject
->SetData( format
, 0, 0 );
171 char *d
= new char[size
];
172 data
->GetDataHere( format
, (void*)d
);
173 m_dataObject
->SetData( format
, size
, d
);
187 ItemReference theItem
;
189 FlavorFlags theFlags
;
191 wxString filenamesPassed
;
193 CountDragItems( (DragReference
)m_currentDrag
, &items
);
194 for (UInt16 index
= 1; index
<= items
; ++index
)
197 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
198 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
199 wxDataFormat preferredFormat
= m_dataObject
->GetPreferredFormat( wxDataObject::Set
);
200 bool hasPreferredFormat
= false;
202 for (UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
204 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
205 wxDataFormat
format( theType
);
206 if (preferredFormat
== format
)
208 hasPreferredFormat
= true;
213 for (UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
215 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
216 wxDataFormat
format( theType
);
217 if ((hasPreferredFormat
&& format
== preferredFormat
)
218 || (!hasPreferredFormat
&& m_dataObject
->IsSupportedFormat( format
)))
220 result
= GetFlavorFlags( (DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
226 GetFlavorDataSize( (DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
227 if (theType
== kScrapFlavorTypeText
)
229 // this increment is only valid for allocating:
230 // on the next GetFlavorData call it is reset again to the original value
233 else if (theType
== kScrapFlavorTypeUnicode
)
235 // this increment is only valid for allocating:
236 // on the next GetFlavorData call it is reset again to the original value
242 theData
= new char[dataSize
];
246 GetFlavorData( (DragReference
)m_currentDrag
, theItem
, theType
, (void*)theData
, &dataSize
, 0L );
249 case kScrapFlavorTypeText
:
250 theData
[dataSize
] = 0;
251 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
255 case kScrapFlavorTypeUnicode
:
256 theData
[dataSize
+ 0] =
257 theData
[dataSize
+ 1] = 0;
258 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
262 case kDragFlavorTypeHFS
:
265 HFSFlavor
* theFile
= (HFSFlavor
*)theData
;
266 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
);
269 filenamesPassed
+= name
+ wxT("\n");
274 m_dataObject
->SetData( format
, dataSize
, theData
);
285 if (filenamesPassed
.Len() > 0)
287 wxCharBuffer buf
= filenamesPassed
.fn_str();
288 m_dataObject
->SetData( wxDataFormat(wxDF_FILENAME
), strlen( buf
), (const char*)buf
);
295 //-------------------------------------------------------------------------
297 //-------------------------------------------------------------------------
299 //-----------------------------------------------------------------------------
302 wxDropSource::wxDropSource(wxWindow
*win
,
303 const wxCursor
&cursorCopy
,
304 const wxCursor
&cursorMove
,
305 const wxCursor
&cursorStop
)
306 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
308 wxMacEnsureTrackingHandlersInstalled();
313 wxDropSource::wxDropSource(wxDataObject
& data
,
315 const wxCursor
&cursorCopy
,
316 const wxCursor
&cursorMove
,
317 const wxCursor
&cursorStop
)
318 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
320 wxMacEnsureTrackingHandlersInstalled();
326 wxDropSource::~wxDropSource()
330 wxDragResult
wxDropSource::DoDragDrop(int flags
)
332 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
334 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
335 return (wxDragResult
)wxDragNone
;
338 DragReference theDrag
;
339 RgnHandle dragRegion
;
341 if ((result
= NewDrag( &theDrag
)) != noErr
)
345 size_t formatCount
= m_data
->GetFormatCount();
346 wxDataFormat
*formats
= new wxDataFormat
[formatCount
];
347 m_data
->GetAllFormats( formats
);
348 ItemReference theItem
= 1;
350 for ( size_t i
= 0; i
< formatCount
; ++i
)
352 size_t dataSize
= m_data
->GetDataSize( formats
[i
] );
353 Ptr dataPtr
= new char[dataSize
];
354 m_data
->GetDataHere( formats
[i
], dataPtr
);
355 OSType type
= formats
[i
].GetFormatId();
356 if ( type
== 'TEXT' || type
== 'utxt' )
360 dataPtr
[ dataSize
] = 0;
361 if ( type
== 'utxt' )
365 dataPtr
[ dataSize
] = 0;
368 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
370 else if (type
== kDragFlavorTypeHFS
)
376 wxMacFilename2FSSpec( wxString( dataPtr
, *wxConvCurrent
), &theFlavor
.fileSpec
);
378 memset( &cat
, 0, sizeof(cat
) );
379 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
380 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
381 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
382 cat
.hFileInfo
.ioFDirIndex
= 0;
383 err
= PBGetCatInfoSync( &cat
);
386 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
387 if (theFlavor
.fileSpec
.parID
== fsRtParID
)
389 theFlavor
.fileCreator
= 'MACS';
390 theFlavor
.fileType
= 'disk';
392 else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0)
394 theFlavor
.fileCreator
= 'MACS';
395 theFlavor
.fileType
= 'fold';
399 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
400 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
403 AddDragItemFlavor( theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0 );
408 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
416 dragRegion
= NewRgn();
417 RgnHandle tempRgn
= NewRgn();
419 EventRecord
* ev
= NULL
;
421 #if !TARGET_CARBON // TODO
422 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent();
426 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent(), &rec
);
429 const short dragRegionOuterBoundary
= 10;
430 const short dragRegionInnerBoundary
= 9;
434 ev
->where
.h
- dragRegionOuterBoundary
,
435 ev
->where
.v
- dragRegionOuterBoundary
,
436 ev
->where
.h
+ dragRegionOuterBoundary
,
437 ev
->where
.v
+ dragRegionOuterBoundary
);
441 ev
->where
.h
- dragRegionInnerBoundary
,
442 ev
->where
.v
- dragRegionInnerBoundary
,
443 ev
->where
.h
+ dragRegionInnerBoundary
,
444 ev
->where
.v
+ dragRegionInnerBoundary
);
446 DiffRgn( dragRegion
, tempRgn
, dragRegion
);
447 DisposeRgn( tempRgn
);
449 // TODO: work with promises in order to return data
450 // only when drag was successfully completed
452 gTrackingGlobals
.m_currentSource
= this;
453 result
= TrackDrag( theDrag
, ev
, dragRegion
);
454 DisposeRgn( dragRegion
);
455 DisposeDrag( theDrag
);
456 gTrackingGlobals
.m_currentSource
= NULL
;
458 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
459 wxDragResult dndresult
= wxDragCopy
;
460 if ( flags
!= wxDrag_CopyOnly
)
461 // on mac the option key is always the indication for copy
462 dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
467 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
469 const wxCursor
& cursor
= GetCursor(effect
);
470 bool result
= cursor
.Ok();
478 bool gTrackingGlobalsInstalled
= false;
480 // passing the globals via refcon is not needed by the CFM and later architectures anymore
481 // but I'll leave it in there, just in case...
483 pascal OSErr
wxMacWindowDragTrackingHandler(
484 DragTrackingMessage theMessage
, WindowPtr theWindow
,
485 void *handlerRefCon
, DragReference theDrag
);
486 pascal OSErr
wxMacWindowDragReceiveHandler(
487 WindowPtr theWindow
, void *handlerRefCon
,
488 DragReference theDrag
);
490 void wxMacEnsureTrackingHandlersInstalled()
492 if ( !gTrackingGlobalsInstalled
)
496 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
499 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
502 gTrackingGlobalsInstalled
= true;
506 pascal OSErr
wxMacWindowDragTrackingHandler(
507 DragTrackingMessage theMessage
, WindowPtr theWindow
,
508 void *handlerRefCon
, DragReference theDrag
)
510 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
512 Point mouse
, localMouse
;
513 DragAttributes attributes
;
515 GetDragAttributes( theDrag
, &attributes
);
517 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
);
519 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
520 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
524 case kDragTrackingEnterHandler
:
525 case kDragTrackingLeaveHandler
:
528 case kDragTrackingEnterWindow
:
529 if (trackingGlobals
!= NULL
)
531 trackingGlobals
->m_currentTargetWindow
= NULL
;
532 trackingGlobals
->m_currentTarget
= NULL
;
536 case kDragTrackingInWindow
:
537 if (trackingGlobals
== NULL
)
539 if (toplevel
== NULL
)
542 GetDragMouse( theDrag
, &mouse
, 0L );
544 GlobalToLocal( &localMouse
);
547 wxWindow
*win
= NULL
;
548 ControlPartCode controlPart
;
549 ControlRef control
= wxMacFindControlUnderMouse(
550 toplevel
, localMouse
, theWindow
, &controlPart
);
552 win
= wxFindControlFromMacControl( control
);
557 localx
= localMouse
.h
;
558 localy
= localMouse
.v
;
561 win
->MacRootWindowToWindow( &localx
, &localy
);
562 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
564 if ( trackingGlobals
->m_currentTargetWindow
)
566 // this window is left
567 if ( trackingGlobals
->m_currentTarget
)
569 HideDragHilite( theDrag
);
570 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
571 trackingGlobals
->m_currentTarget
->OnLeave();
572 trackingGlobals
->m_currentTarget
= NULL
;
573 trackingGlobals
->m_currentTargetWindow
= NULL
;
579 // this window is entered
580 trackingGlobals
->m_currentTargetWindow
= win
;
581 trackingGlobals
->m_currentTarget
= win
->GetDropTarget();
583 if ( trackingGlobals
->m_currentTarget
)
585 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
586 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
);
589 if ( result
!= wxDragNone
)
594 win
->MacWindowToRootWindow( &x
, &y
);
595 RgnHandle hiliteRgn
= NewRgn();
596 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
};
597 RectRgn( hiliteRgn
, &r
);
598 ShowDragHilite( theDrag
, hiliteRgn
, true );
599 DisposeRgn( hiliteRgn
);
606 if ( trackingGlobals
->m_currentTarget
)
608 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
609 trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
);
613 // set cursor for OnEnter and OnDragOver
614 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
616 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
618 int cursorID
= wxCURSOR_NONE
;
623 cursorID
= wxCURSOR_COPY_ARROW
;
627 cursorID
= wxCURSOR_ARROW
;
631 cursorID
= wxCURSOR_NO_ENTRY
;
638 // put these here to make gcc happy
642 if (cursorID
!= wxCURSOR_NONE
)
644 wxCursor
cursor( cursorID
);
652 case kDragTrackingLeaveWindow
:
653 if (trackingGlobals
== NULL
)
656 if (trackingGlobals
->m_currentTarget
)
658 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
659 trackingGlobals
->m_currentTarget
->OnLeave();
660 HideDragHilite( theDrag
);
661 trackingGlobals
->m_currentTarget
= NULL
;
663 trackingGlobals
->m_currentTargetWindow
= NULL
;
673 pascal OSErr
wxMacWindowDragReceiveHandler(
676 DragReference theDrag
)
678 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
679 if ( trackingGlobals
->m_currentTarget
)
681 Point mouse
, localMouse
;
684 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
685 GetDragMouse( theDrag
, &mouse
, 0L );
687 GlobalToLocal( &localMouse
);
688 localx
= localMouse
.h
;
689 localy
= localMouse
.v
;
691 // TODO : should we use client coordinates?
692 if ( trackingGlobals
->m_currentTargetWindow
)
693 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
694 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
696 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
697 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
698 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);