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( wxCoord
WXUNUSED(x
),
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( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
72 if (m_dataObject
== NULL
)
75 if (!CurrentDragHasSupportedFormat())
78 return GetData() ? def
: wxDragNone
;
81 bool wxDropTarget::CurrentDragHasSupportedFormat()
83 bool supported
= false ;
85 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
87 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
91 size_t formatcount
= data
->GetFormatCount() ;
92 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
93 data
->GetAllFormats( array
);
94 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
96 wxDataFormat format
= array
[i
] ;
97 if ( m_dataObject
->IsSupported( format
) )
112 ItemReference theItem
;
116 CountDragItems( (DragReference
)m_currentDrag
, &items
);
117 for (UInt16 index
= 1; index
<= items
&& !supported
; ++index
)
120 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
121 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
123 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
125 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
126 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
138 bool wxDropTarget::GetData()
140 if (m_dataObject
== NULL
)
143 if ( !CurrentDragHasSupportedFormat() )
146 bool transferred
= false;
147 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
149 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
153 size_t formatcount
= data
->GetFormatCount();
154 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
155 data
->GetAllFormats( array
);
156 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
158 wxDataFormat format
= array
[i
];
159 if ( m_dataObject
->IsSupported( format
) )
161 int size
= data
->GetDataSize( format
);
166 m_dataObject
->SetData( format
, 0, 0 );
170 char *d
= new char[size
];
171 data
->GetDataHere( format
, (void*)d
);
172 m_dataObject
->SetData( format
, size
, d
);
186 ItemReference theItem
;
188 FlavorFlags theFlags
;
190 bool firstFileAdded
= false ;
192 CountDragItems( (DragReference
)m_currentDrag
, &items
);
193 for (UInt16 index
= 1; index
<= items
; ++index
)
196 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
197 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
198 wxDataFormat preferredFormat
= m_dataObject
->GetPreferredFormat( wxDataObject::Set
);
199 bool hasPreferredFormat
= false ;
201 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
203 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
204 wxDataFormat
format( theType
);
205 if ( preferredFormat
== format
)
207 hasPreferredFormat
= true ;
212 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
214 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
215 wxDataFormat
format( theType
) ;
216 if ( (hasPreferredFormat
&& format
== preferredFormat
)
217 || (!hasPreferredFormat
&& m_dataObject
->IsSupportedFormat( format
)))
219 result
= GetFlavorFlags( (DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
225 GetFlavorDataSize( (DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
226 if ( theType
== kScrapFlavorTypeText
)
228 // this increment is only valid for allocating:
229 // on the next GetFlavorData call it is reset again to the original value
232 else if ( theType
== kScrapFlavorTypeUnicode
)
234 // this increment is only valid for allocating:
235 // on the next GetFlavorData call it is reset again to the original value
241 theData
= new char[dataSize
];
245 GetFlavorData( (DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L );
246 if ( theType
== kScrapFlavorTypeText
)
248 theData
[dataSize
] = 0 ;
249 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
252 else if ( theType
== kScrapFlavorTypeUnicode
)
254 theData
[dataSize
+ 0] =
255 theData
[dataSize
+ 1] = 0 ;
256 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
259 else if ( theType
== kDragFlavorTypeHFS
)
261 wxFileDataObject
*fdo
= dynamic_cast<wxFileDataObject
*>(m_dataObject
);
262 wxASSERT( fdo
!= NULL
);
264 if ((theData
!= NULL
) && (fdo
!= NULL
))
266 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
267 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
269 if ( !firstFileAdded
)
272 fdo
->SetData( 0 , "" ) ;
273 firstFileAdded
= true ;
277 fdo
->AddFile( name
) ;
282 m_dataObject
->SetData( format
, dataSize
, theData
);
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") );
336 return (wxDragResult
) wxDragNone
;
337 if (m_data
->GetFormatCount() == 0)
338 return (wxDragResult
) wxDragNone
;
341 DragReference theDrag
;
342 RgnHandle dragRegion
;
343 if ((result
= NewDrag(&theDrag
)) != noErr
)
347 size_t formatCount
= m_data
->GetFormatCount() ;
348 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
349 m_data
->GetAllFormats( formats
) ;
350 ItemReference theItem
= 1 ;
352 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
354 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
355 Ptr dataPtr
= new char[dataSize
] ;
356 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
357 OSType type
= formats
[i
].GetFormatId() ;
358 if ( type
== 'TEXT' || type
== 'utxt' )
362 dataPtr
[ dataSize
] = 0 ;
363 if ( type
== 'utxt' )
367 dataPtr
[ dataSize
] = 0 ;
370 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
372 else if (type
== kDragFlavorTypeHFS
)
374 HFSFlavor theFlavor
;
378 wxMacFilename2FSSpec( wxString( dataPtr
, *wxConvCurrent
), &theFlavor
.fileSpec
) ;
380 memset( &cat
, 0, sizeof(cat
) );
381 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
382 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
383 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
384 cat
.hFileInfo
.ioFDirIndex
= 0;
385 err
= PBGetCatInfoSync( &cat
);
388 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
389 if (theFlavor
.fileSpec
.parID
== fsRtParID
)
391 theFlavor
.fileCreator
= 'MACS';
392 theFlavor
.fileType
= 'disk';
394 else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0)
396 theFlavor
.fileCreator
= 'MACS';
397 theFlavor
.fileType
= 'fold';
401 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
402 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
405 AddDragItemFlavor( theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0 );
410 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
418 dragRegion
= NewRgn();
419 RgnHandle tempRgn
= NewRgn() ;
421 EventRecord
* ev
= NULL
;
423 #if !TARGET_CARBON // TODO
424 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
429 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
433 const short dragRegionOuterBoundary
= 10 ;
434 const short dragRegionInnerBoundary
= 9 ;
437 dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
438 ev
->where
.v
- dragRegionOuterBoundary
,
439 ev
->where
.h
+ dragRegionOuterBoundary
,
440 ev
->where
.v
+ dragRegionOuterBoundary
) ;
443 tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
444 ev
->where
.v
- dragRegionInnerBoundary
,
445 ev
->where
.h
+ dragRegionInnerBoundary
,
446 ev
->where
.v
+ dragRegionInnerBoundary
) ;
448 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
449 DisposeRgn( tempRgn
) ;
451 // TODO: work with promises in order to return data
452 // only when drag was successfully completed
454 gTrackingGlobals
.m_currentSource
= this;
455 result
= TrackDrag( theDrag
, ev
, dragRegion
);
456 DisposeRgn( dragRegion
);
457 DisposeDrag( theDrag
);
458 gTrackingGlobals
.m_currentSource
= NULL
;
460 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
461 wxDragResult dndresult
= wxDragCopy
;
462 if ( flags
!= wxDrag_CopyOnly
)
463 // on mac the option key is always the indication for copy
464 dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
469 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
471 const wxCursor
& cursor
= GetCursor(effect
);
472 bool result
= cursor
.Ok();
480 bool gTrackingGlobalsInstalled
= false ;
482 // passing the globals via refcon is not needed by the CFM and later architectures anymore
483 // but I'll leave it in there, just in case...
485 pascal OSErr
wxMacWindowDragTrackingHandler(
486 DragTrackingMessage theMessage
, WindowPtr theWindow
,
487 void *handlerRefCon
, DragReference theDrag
);
488 pascal OSErr
wxMacWindowDragReceiveHandler(
489 WindowPtr theWindow
, void *handlerRefCon
,
490 DragReference theDrag
);
492 void wxMacEnsureTrackingHandlersInstalled()
494 if ( !gTrackingGlobalsInstalled
)
498 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
501 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
504 gTrackingGlobalsInstalled
= true;
508 pascal OSErr
wxMacWindowDragTrackingHandler(
509 DragTrackingMessage theMessage
, WindowPtr theWindow
,
510 void *handlerRefCon
, DragReference theDrag
)
512 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
514 Point mouse
, localMouse
;
515 DragAttributes attributes
;
517 GetDragAttributes( theDrag
, &attributes
);
519 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
521 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
522 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
526 case kDragTrackingEnterHandler
:
527 case kDragTrackingLeaveHandler
:
530 case kDragTrackingEnterWindow
:
531 if (trackingGlobals
!= NULL
)
533 trackingGlobals
->m_currentTargetWindow
= NULL
;
534 trackingGlobals
->m_currentTarget
= NULL
;
538 case kDragTrackingInWindow
:
539 if (trackingGlobals
== NULL
)
541 if (toplevel
== NULL
)
544 GetDragMouse( theDrag
, &mouse
, 0L );
546 GlobalToLocal( &localMouse
);
549 wxWindow
*win
= NULL
;
550 ControlPartCode controlPart
;
551 ControlRef control
= wxMacFindControlUnderMouse(
552 toplevel
, localMouse
, theWindow
, &controlPart
) ;
554 win
= wxFindControlFromMacControl( control
) ;
558 int localx
, localy
;
559 localx
= localMouse
.h
;
560 localy
= localMouse
.v
;
563 win
->MacRootWindowToWindow( &localx
, &localy
) ;
564 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
566 if ( trackingGlobals
->m_currentTargetWindow
)
568 // this window is left
569 if ( trackingGlobals
->m_currentTarget
)
571 HideDragHilite( theDrag
);
572 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
573 trackingGlobals
->m_currentTarget
->OnLeave();
574 trackingGlobals
->m_currentTarget
= NULL
;
575 trackingGlobals
->m_currentTargetWindow
= NULL
;
581 // this window is entered
582 trackingGlobals
->m_currentTargetWindow
= win
;
583 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
585 if ( trackingGlobals
->m_currentTarget
)
587 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
588 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
) ;
591 if ( result
!= wxDragNone
)
596 win
->MacWindowToRootWindow( &x
, &y
) ;
597 RgnHandle hiliteRgn
= NewRgn() ;
598 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
} ;
599 RectRgn( hiliteRgn
, &r
) ;
600 ShowDragHilite( theDrag
, hiliteRgn
, true );
601 DisposeRgn( hiliteRgn
) ;
608 if ( trackingGlobals
->m_currentTarget
)
610 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
611 trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
) ;
615 // set cursor for OnEnter and OnDragOver
616 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
618 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
624 wxCursor
cursor(wxCURSOR_COPY_ARROW
) ;
625 cursor
.MacInstall() ;
631 wxCursor
cursor(wxCURSOR_ARROW
) ;
632 cursor
.MacInstall() ;
638 wxCursor
cursor(wxCURSOR_NO_ENTRY
) ;
639 cursor
.MacInstall() ;
647 // put these here to make gcc happy
655 case kDragTrackingLeaveWindow
:
656 if (trackingGlobals
== NULL
)
659 if (trackingGlobals
->m_currentTarget
)
661 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
662 trackingGlobals
->m_currentTarget
->OnLeave();
663 HideDragHilite( theDrag
);
664 trackingGlobals
->m_currentTarget
= NULL
;
666 trackingGlobals
->m_currentTargetWindow
= NULL
;
676 pascal OSErr
wxMacWindowDragReceiveHandler(
679 DragReference theDrag
)
681 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
682 if ( trackingGlobals
->m_currentTarget
)
684 Point mouse
, localMouse
;
687 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
688 GetDragMouse(theDrag
, &mouse
, 0L);
690 GlobalToLocal(&localMouse
);
691 localx
= localMouse
.h
;
692 localy
= localMouse
.v
;
694 // TODO : should we use client coordinates?
695 if ( trackingGlobals
->m_currentTargetWindow
)
696 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
697 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
699 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
700 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
701 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
) ;