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 bool firstFileAdded
= false;
193 wxString filenamesPassed
;
195 CountDragItems( (DragReference
)m_currentDrag
, &items
);
196 for (UInt16 index
= 1; index
<= items
; ++index
)
199 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
200 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
201 wxDataFormat preferredFormat
= m_dataObject
->GetPreferredFormat( wxDataObject::Set
);
202 bool hasPreferredFormat
= false;
204 for ( UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
206 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
207 wxDataFormat
format( theType
);
208 if ( preferredFormat
== format
)
210 hasPreferredFormat
= true;
215 for ( UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
217 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
218 wxDataFormat
format( theType
);
219 if ( (hasPreferredFormat
&& format
== preferredFormat
)
220 || (!hasPreferredFormat
&& m_dataObject
->IsSupportedFormat( format
)))
222 result
= GetFlavorFlags( (DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
228 GetFlavorDataSize( (DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
229 if ( theType
== kScrapFlavorTypeText
)
231 // this increment is only valid for allocating:
232 // on the next GetFlavorData call it is reset again to the original value
235 else if ( theType
== kScrapFlavorTypeUnicode
)
237 // this increment is only valid for allocating:
238 // on the next GetFlavorData call it is reset again to the original value
244 theData
= new char[dataSize
];
248 GetFlavorData( (DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L );
251 case kScrapFlavorTypeText
:
252 theData
[dataSize
] = 0;
253 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
257 case kScrapFlavorTypeUnicode
:
258 theData
[dataSize
+ 0] =
259 theData
[dataSize
+ 1] = 0;
260 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
264 case kDragFlavorTypeHFS
:
268 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
269 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
);
272 filenamesPassed
+= name
+ wxT("\n");
278 m_dataObject
->SetData( format
, dataSize
, theData
);
288 if ( filenamesPassed
.Len() > 0 )
290 wxCharBuffer buf
= filenamesPassed
.fn_str();
291 m_dataObject
->SetData( wxDataFormat( wxDF_FILENAME
) , strlen( buf
) , (const char*) buf
);
298 //-------------------------------------------------------------------------
300 //-------------------------------------------------------------------------
302 //-----------------------------------------------------------------------------
305 wxDropSource::wxDropSource(wxWindow
*win
,
306 const wxCursor
&cursorCopy
,
307 const wxCursor
&cursorMove
,
308 const wxCursor
&cursorStop
)
309 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
311 wxMacEnsureTrackingHandlersInstalled();
316 wxDropSource::wxDropSource(wxDataObject
& data
,
318 const wxCursor
&cursorCopy
,
319 const wxCursor
&cursorMove
,
320 const wxCursor
&cursorStop
)
321 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
323 wxMacEnsureTrackingHandlersInstalled();
329 wxDropSource::~wxDropSource()
333 wxDragResult
wxDropSource::DoDragDrop(int flags
)
335 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
338 return (wxDragResult
) wxDragNone
;
339 if (m_data
->GetFormatCount() == 0)
340 return (wxDragResult
) wxDragNone
;
343 DragReference theDrag
;
344 RgnHandle dragRegion
;
345 if ((result
= NewDrag(&theDrag
)) != noErr
)
349 size_t formatCount
= m_data
->GetFormatCount();
350 wxDataFormat
*formats
= new wxDataFormat
[formatCount
];
351 m_data
->GetAllFormats( formats
);
352 ItemReference theItem
= 1;
354 for ( size_t i
= 0; i
< formatCount
; ++i
)
356 size_t dataSize
= m_data
->GetDataSize( formats
[i
] );
357 Ptr dataPtr
= new char[dataSize
];
358 m_data
->GetDataHere( formats
[i
], dataPtr
);
359 OSType type
= formats
[i
].GetFormatId();
360 if ( type
== 'TEXT' || type
== 'utxt' )
364 dataPtr
[ dataSize
] = 0;
365 if ( type
== 'utxt' )
369 dataPtr
[ dataSize
] = 0;
372 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
374 else if (type
== kDragFlavorTypeHFS
)
380 wxMacFilename2FSSpec( wxString( dataPtr
, *wxConvCurrent
), &theFlavor
.fileSpec
);
382 memset( &cat
, 0, sizeof(cat
) );
383 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
384 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
385 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
386 cat
.hFileInfo
.ioFDirIndex
= 0;
387 err
= PBGetCatInfoSync( &cat
);
390 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
391 if (theFlavor
.fileSpec
.parID
== fsRtParID
)
393 theFlavor
.fileCreator
= 'MACS';
394 theFlavor
.fileType
= 'disk';
396 else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0)
398 theFlavor
.fileCreator
= 'MACS';
399 theFlavor
.fileType
= 'fold';
403 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
404 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
407 AddDragItemFlavor( theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0 );
412 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
420 dragRegion
= NewRgn();
421 RgnHandle tempRgn
= NewRgn();
423 EventRecord
* ev
= NULL
;
425 #if !TARGET_CARBON // TODO
426 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent();
430 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent(), &rec
);
433 const short dragRegionOuterBoundary
= 10;
434 const short dragRegionInnerBoundary
= 9;
438 ev
->where
.h
- dragRegionOuterBoundary
,
439 ev
->where
.v
- dragRegionOuterBoundary
,
440 ev
->where
.h
+ dragRegionOuterBoundary
,
441 ev
->where
.v
+ dragRegionOuterBoundary
);
445 ev
->where
.h
- dragRegionInnerBoundary
,
446 ev
->where
.v
- dragRegionInnerBoundary
,
447 ev
->where
.h
+ dragRegionInnerBoundary
,
448 ev
->where
.v
+ dragRegionInnerBoundary
);
450 DiffRgn( dragRegion
, tempRgn
, dragRegion
);
451 DisposeRgn( tempRgn
);
453 // TODO: work with promises in order to return data
454 // only when drag was successfully completed
456 gTrackingGlobals
.m_currentSource
= this;
457 result
= TrackDrag( theDrag
, ev
, dragRegion
);
458 DisposeRgn( dragRegion
);
459 DisposeDrag( theDrag
);
460 gTrackingGlobals
.m_currentSource
= NULL
;
462 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
463 wxDragResult dndresult
= wxDragCopy
;
464 if ( flags
!= wxDrag_CopyOnly
)
465 // on mac the option key is always the indication for copy
466 dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
471 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
473 const wxCursor
& cursor
= GetCursor(effect
);
474 bool result
= cursor
.Ok();
482 bool gTrackingGlobalsInstalled
= false;
484 // passing the globals via refcon is not needed by the CFM and later architectures anymore
485 // but I'll leave it in there, just in case...
487 pascal OSErr
wxMacWindowDragTrackingHandler(
488 DragTrackingMessage theMessage
, WindowPtr theWindow
,
489 void *handlerRefCon
, DragReference theDrag
);
490 pascal OSErr
wxMacWindowDragReceiveHandler(
491 WindowPtr theWindow
, void *handlerRefCon
,
492 DragReference theDrag
);
494 void wxMacEnsureTrackingHandlersInstalled()
496 if ( !gTrackingGlobalsInstalled
)
500 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
503 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
506 gTrackingGlobalsInstalled
= true;
510 pascal OSErr
wxMacWindowDragTrackingHandler(
511 DragTrackingMessage theMessage
, WindowPtr theWindow
,
512 void *handlerRefCon
, DragReference theDrag
)
514 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
516 Point mouse
, localMouse
;
517 DragAttributes attributes
;
519 GetDragAttributes( theDrag
, &attributes
);
521 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
);
523 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
524 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
528 case kDragTrackingEnterHandler
:
529 case kDragTrackingLeaveHandler
:
532 case kDragTrackingEnterWindow
:
533 if (trackingGlobals
!= NULL
)
535 trackingGlobals
->m_currentTargetWindow
= NULL
;
536 trackingGlobals
->m_currentTarget
= NULL
;
540 case kDragTrackingInWindow
:
541 if (trackingGlobals
== NULL
)
543 if (toplevel
== NULL
)
546 GetDragMouse( theDrag
, &mouse
, 0L );
548 GlobalToLocal( &localMouse
);
551 wxWindow
*win
= NULL
;
552 ControlPartCode controlPart
;
553 ControlRef control
= wxMacFindControlUnderMouse(
554 toplevel
, localMouse
, theWindow
, &controlPart
);
556 win
= wxFindControlFromMacControl( control
);
561 localx
= localMouse
.h
;
562 localy
= localMouse
.v
;
565 win
->MacRootWindowToWindow( &localx
, &localy
);
566 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
568 if ( trackingGlobals
->m_currentTargetWindow
)
570 // this window is left
571 if ( trackingGlobals
->m_currentTarget
)
573 HideDragHilite( theDrag
);
574 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
575 trackingGlobals
->m_currentTarget
->OnLeave();
576 trackingGlobals
->m_currentTarget
= NULL
;
577 trackingGlobals
->m_currentTargetWindow
= NULL
;
583 // this window is entered
584 trackingGlobals
->m_currentTargetWindow
= win
;
585 trackingGlobals
->m_currentTarget
= win
->GetDropTarget();
587 if ( trackingGlobals
->m_currentTarget
)
589 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
590 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
);
593 if ( result
!= wxDragNone
)
598 win
->MacWindowToRootWindow( &x
, &y
);
599 RgnHandle hiliteRgn
= NewRgn();
600 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
};
601 RectRgn( hiliteRgn
, &r
);
602 ShowDragHilite( theDrag
, hiliteRgn
, true );
603 DisposeRgn( hiliteRgn
);
610 if ( trackingGlobals
->m_currentTarget
)
612 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
613 trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
);
617 // set cursor for OnEnter and OnDragOver
618 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
620 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
626 wxCursor
cursor(wxCURSOR_COPY_ARROW
);
633 wxCursor
cursor(wxCURSOR_ARROW
);
640 wxCursor
cursor(wxCURSOR_NO_ENTRY
);
649 // put these here to make gcc happy
657 case kDragTrackingLeaveWindow
:
658 if (trackingGlobals
== NULL
)
661 if (trackingGlobals
->m_currentTarget
)
663 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
664 trackingGlobals
->m_currentTarget
->OnLeave();
665 HideDragHilite( theDrag
);
666 trackingGlobals
->m_currentTarget
= NULL
;
668 trackingGlobals
->m_currentTargetWindow
= NULL
;
678 pascal OSErr
wxMacWindowDragReceiveHandler(
681 DragReference theDrag
)
683 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
684 if ( trackingGlobals
->m_currentTarget
)
686 Point mouse
, localMouse
;
689 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
690 GetDragMouse( theDrag
, &mouse
, 0L );
692 GlobalToLocal( &localMouse
);
693 localx
= localMouse
.h
;
694 localy
= localMouse
.v
;
696 // TODO : should we use client coordinates?
697 if ( trackingGlobals
->m_currentTargetWindow
)
698 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
699 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
701 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
702 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
703 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);