1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dnd.cpp
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
20 #include "wx/toplevel.h"
21 #include "wx/gdicmn.h"
24 #include "wx/mac/private.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
37 wxWindow
*m_currentTargetWindow
;
38 wxDropTarget
*m_currentTarget
;
39 wxDropSource
*m_currentSource
;
42 MacTrackingGlobals gTrackingGlobals
;
44 void wxMacEnsureTrackingHandlersInstalled();
46 //----------------------------------------------------------------------------
48 //----------------------------------------------------------------------------
50 wxDropTarget::wxDropTarget( wxDataObject
*data
)
51 : wxDropTargetBase( data
)
53 wxMacEnsureTrackingHandlersInstalled();
56 wxDragResult
wxDropTarget::OnDragOver(
57 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
60 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
63 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
65 if (m_dataObject
== NULL
)
68 return CurrentDragHasSupportedFormat();
71 wxDragResult
wxDropTarget::OnData(
72 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
75 if (m_dataObject
== NULL
)
78 if (!CurrentDragHasSupportedFormat())
81 return GetData() ? def
: wxDragNone
;
84 bool wxDropTarget::CurrentDragHasSupportedFormat()
86 bool supported
= false;
88 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
90 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
94 size_t formatcount
= data
->GetFormatCount();
95 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
96 data
->GetAllFormats( array
);
97 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
99 wxDataFormat format
= array
[i
];
100 if ( m_dataObject
->IsSupported( format
) )
114 ItemReference theItem
;
118 CountDragItems( (DragReference
)m_currentDrag
, &items
);
119 for (UInt16 index
= 1; index
<= items
&& !supported
; ++index
)
122 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
123 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
125 for ( UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
127 GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
128 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
140 bool wxDropTarget::GetData()
142 if (m_dataObject
== NULL
)
145 if ( !CurrentDragHasSupportedFormat() )
148 bool transferred
= false;
149 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
151 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
155 size_t formatcount
= data
->GetFormatCount();
156 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
157 data
->GetAllFormats( array
);
158 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
160 wxDataFormat format
= array
[i
];
161 if ( m_dataObject
->IsSupported( format
) )
163 int size
= data
->GetDataSize( format
);
168 m_dataObject
->SetData( format
, 0, 0 );
172 char *d
= new char[size
];
173 data
->GetDataHere( format
, (void*)d
);
174 m_dataObject
->SetData( format
, size
, d
);
188 ItemReference theItem
;
190 FlavorFlags theFlags
;
192 wxString filenamesPassed
;
194 CountDragItems( (DragReference
)m_currentDrag
, &items
);
195 for (UInt16 index
= 1; index
<= items
; ++index
)
198 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
199 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
200 wxDataFormat preferredFormat
= m_dataObject
->GetPreferredFormat( wxDataObject::Set
);
201 bool hasPreferredFormat
= false;
203 for (UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
205 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
206 wxDataFormat
format( theType
);
207 if (preferredFormat
== format
)
209 hasPreferredFormat
= true;
214 for (UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
216 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
217 wxDataFormat
format( theType
);
218 if ((hasPreferredFormat
&& format
== preferredFormat
)
219 || (!hasPreferredFormat
&& m_dataObject
->IsSupportedFormat( format
)))
221 result
= GetFlavorFlags( (DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
227 GetFlavorDataSize( (DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
228 if (theType
== kScrapFlavorTypeText
)
230 // this increment is only valid for allocating:
231 // on the next GetFlavorData call it is reset again to the original value
234 else if (theType
== kScrapFlavorTypeUnicode
)
236 // this increment is only valid for allocating:
237 // on the next GetFlavorData call it is reset again to the original value
243 theData
= new char[dataSize
];
247 GetFlavorData( (DragReference
)m_currentDrag
, theItem
, theType
, (void*)theData
, &dataSize
, 0L );
250 case kScrapFlavorTypeText
:
251 theData
[dataSize
] = 0;
252 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
256 case kScrapFlavorTypeUnicode
:
257 theData
[dataSize
+ 0] =
258 theData
[dataSize
+ 1] = 0;
259 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
263 case kDragFlavorTypeHFS
:
266 HFSFlavor
* theFile
= (HFSFlavor
*)theData
;
267 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
);
270 filenamesPassed
+= name
+ wxT("\n");
275 m_dataObject
->SetData( format
, dataSize
, theData
);
286 if (filenamesPassed
.length() > 0)
288 wxCharBuffer buf
= filenamesPassed
.fn_str();
289 m_dataObject
->SetData( wxDataFormat(wxDF_FILENAME
), strlen( buf
), (const char*)buf
);
296 //-------------------------------------------------------------------------
298 //-------------------------------------------------------------------------
300 //-----------------------------------------------------------------------------
303 wxDropSource::wxDropSource(wxWindow
*win
,
304 const wxCursor
&cursorCopy
,
305 const wxCursor
&cursorMove
,
306 const wxCursor
&cursorStop
)
307 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
309 wxMacEnsureTrackingHandlersInstalled();
314 wxDropSource::wxDropSource(wxDataObject
& data
,
316 const wxCursor
&cursorCopy
,
317 const wxCursor
&cursorMove
,
318 const wxCursor
&cursorStop
)
319 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
321 wxMacEnsureTrackingHandlersInstalled();
327 wxDropSource::~wxDropSource()
331 wxDragResult
wxDropSource::DoDragDrop(int flags
)
333 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
335 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
336 return (wxDragResult
)wxDragNone
;
338 DragReference theDrag
;
339 RgnHandle dragRegion
;
341 if (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 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
);