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
240 theData
= new char[dataSize
];
242 GetFlavorData( (DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L );
243 if ( theType
== kScrapFlavorTypeText
)
245 theData
[dataSize
] = 0 ;
246 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
249 else if ( theType
== kScrapFlavorTypeUnicode
)
251 theData
[dataSize
+ 0] =
252 theData
[dataSize
+ 1] = 0 ;
253 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
256 else if ( theType
== kDragFlavorTypeHFS
)
258 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
259 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
260 if ( !firstFileAdded
)
263 ((wxFileDataObject
*)m_dataObject
)->SetData( 0 , "" ) ;
264 firstFileAdded
= true ;
267 ((wxFileDataObject
*)m_dataObject
)->AddFile( name
) ;
271 m_dataObject
->SetData( format
, dataSize
, theData
);
285 //-------------------------------------------------------------------------
287 //-------------------------------------------------------------------------
289 //-----------------------------------------------------------------------------
292 wxDropSource::wxDropSource(wxWindow
*win
,
293 const wxCursor
&cursorCopy
,
294 const wxCursor
&cursorMove
,
295 const wxCursor
&cursorStop
)
296 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
298 wxMacEnsureTrackingHandlersInstalled() ;
303 wxDropSource::wxDropSource(wxDataObject
& data
,
305 const wxCursor
&cursorCopy
,
306 const wxCursor
&cursorMove
,
307 const wxCursor
&cursorStop
)
308 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
310 wxMacEnsureTrackingHandlersInstalled() ;
316 wxDropSource::~wxDropSource()
320 wxDragResult
wxDropSource::DoDragDrop(int flags
)
322 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
325 return (wxDragResult
) wxDragNone
;
326 if (m_data
->GetFormatCount() == 0)
327 return (wxDragResult
) wxDragNone
;
330 DragReference theDrag
;
331 RgnHandle dragRegion
;
332 if ((result
= NewDrag(&theDrag
)) != noErr
)
336 size_t formatCount
= m_data
->GetFormatCount() ;
337 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
338 m_data
->GetAllFormats( formats
) ;
339 ItemReference theItem
= 1 ;
341 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
343 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
344 Ptr dataPtr
= new char[dataSize
] ;
345 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
346 OSType type
= formats
[i
].GetFormatId() ;
347 if ( type
== 'TEXT' || type
== 'utxt' )
351 dataPtr
[ dataSize
] = 0 ;
352 if ( type
== 'utxt' )
356 dataPtr
[ dataSize
] = 0 ;
359 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
361 else if (type
== kDragFlavorTypeHFS
)
363 HFSFlavor theFlavor
;
367 wxMacFilename2FSSpec( wxString( dataPtr
, *wxConvCurrent
), &theFlavor
.fileSpec
) ;
369 memset( &cat
, 0, sizeof(cat
) );
370 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
371 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
372 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
373 cat
.hFileInfo
.ioFDirIndex
= 0;
374 err
= PBGetCatInfoSync( &cat
);
377 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
378 if (theFlavor
.fileSpec
.parID
== fsRtParID
)
380 theFlavor
.fileCreator
= 'MACS';
381 theFlavor
.fileType
= 'disk';
383 else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0)
385 theFlavor
.fileCreator
= 'MACS';
386 theFlavor
.fileType
= 'fold';
390 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
391 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
394 AddDragItemFlavor( theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0 );
399 AddDragItemFlavor( theDrag
, theItem
, type
, dataPtr
, dataSize
, 0 );
407 dragRegion
= NewRgn();
408 RgnHandle tempRgn
= NewRgn() ;
410 EventRecord
* ev
= NULL
;
412 #if !TARGET_CARBON // TODO
413 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
418 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
422 const short dragRegionOuterBoundary
= 10 ;
423 const short dragRegionInnerBoundary
= 9 ;
426 dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
427 ev
->where
.v
- dragRegionOuterBoundary
,
428 ev
->where
.h
+ dragRegionOuterBoundary
,
429 ev
->where
.v
+ dragRegionOuterBoundary
) ;
432 tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
433 ev
->where
.v
- dragRegionInnerBoundary
,
434 ev
->where
.h
+ dragRegionInnerBoundary
,
435 ev
->where
.v
+ dragRegionInnerBoundary
) ;
437 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
438 DisposeRgn( tempRgn
) ;
440 // TODO: work with promises in order to return data
441 // only when drag was successfully completed
443 gTrackingGlobals
.m_currentSource
= this;
444 result
= TrackDrag( theDrag
, ev
, dragRegion
);
445 DisposeRgn( dragRegion
);
446 DisposeDrag( theDrag
);
447 gTrackingGlobals
.m_currentSource
= NULL
;
449 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
450 wxDragResult dndresult
= wxDragCopy
;
451 if ( flags
!= wxDrag_CopyOnly
)
452 // on mac the option key is always the indication for copy
453 dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
458 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
460 const wxCursor
& cursor
= GetCursor(effect
);
461 bool result
= cursor
.Ok();
469 bool gTrackingGlobalsInstalled
= false ;
471 // passing the globals via refcon is not needed by the CFM and later architectures anymore
472 // but I'll leave it in there, just in case...
474 pascal OSErr
wxMacWindowDragTrackingHandler(
475 DragTrackingMessage theMessage
, WindowPtr theWindow
,
476 void *handlerRefCon
, DragReference theDrag
);
477 pascal OSErr
wxMacWindowDragReceiveHandler(
478 WindowPtr theWindow
, void *handlerRefCon
,
479 DragReference theDrag
);
481 void wxMacEnsureTrackingHandlersInstalled()
483 if ( !gTrackingGlobalsInstalled
)
487 result
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
488 wxASSERT( result
== noErr
);
490 result
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
491 wxASSERT( result
== noErr
);
493 gTrackingGlobalsInstalled
= true;
497 pascal OSErr
wxMacWindowDragTrackingHandler(
498 DragTrackingMessage theMessage
, WindowPtr theWindow
,
499 void *handlerRefCon
, DragReference theDrag
)
501 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
503 Point mouse
, localMouse
;
504 DragAttributes attributes
;
506 GetDragAttributes( theDrag
, &attributes
);
508 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
510 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
511 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
515 case kDragTrackingEnterHandler
:
516 case kDragTrackingLeaveHandler
:
519 case kDragTrackingEnterWindow
:
520 if (trackingGlobals
!= NULL
)
522 trackingGlobals
->m_currentTargetWindow
= NULL
;
523 trackingGlobals
->m_currentTarget
= NULL
;
527 case kDragTrackingInWindow
:
528 if (trackingGlobals
== NULL
)
530 if (toplevel
== NULL
)
533 GetDragMouse( theDrag
, &mouse
, 0L );
535 GlobalToLocal( &localMouse
);
538 wxWindow
*win
= NULL
;
539 ControlPartCode controlPart
;
540 ControlRef control
= wxMacFindControlUnderMouse(
541 toplevel
, localMouse
, theWindow
, &controlPart
) ;
543 win
= wxFindControlFromMacControl( control
) ;
547 int localx
, localy
;
548 localx
= localMouse
.h
;
549 localy
= localMouse
.v
;
552 win
->MacRootWindowToWindow( &localx
, &localy
) ;
553 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
555 if ( trackingGlobals
->m_currentTargetWindow
)
557 // this window is left
558 if ( trackingGlobals
->m_currentTarget
)
560 HideDragHilite( theDrag
);
561 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
562 trackingGlobals
->m_currentTarget
->OnLeave();
563 trackingGlobals
->m_currentTarget
= NULL
;
564 trackingGlobals
->m_currentTargetWindow
= NULL
;
570 // this window is entered
571 trackingGlobals
->m_currentTargetWindow
= win
;
572 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
574 if ( trackingGlobals
->m_currentTarget
)
576 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
577 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
) ;
580 if ( result
!= wxDragNone
)
585 win
->MacWindowToRootWindow( &x
, &y
) ;
586 RgnHandle hiliteRgn
= NewRgn() ;
587 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
} ;
588 RectRgn( hiliteRgn
, &r
) ;
589 ShowDragHilite( theDrag
, hiliteRgn
, true );
590 DisposeRgn( hiliteRgn
) ;
597 if ( trackingGlobals
->m_currentTarget
)
599 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
600 trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
) ;
604 // set cursor for OnEnter and OnDragOver
605 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
607 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
613 wxCursor
cursor(wxCURSOR_COPY_ARROW
) ;
614 cursor
.MacInstall() ;
620 wxCursor
cursor(wxCURSOR_ARROW
) ;
621 cursor
.MacInstall() ;
627 wxCursor
cursor(wxCURSOR_NO_ENTRY
) ;
628 cursor
.MacInstall() ;
636 // put these here to make gcc happy
644 case kDragTrackingLeaveWindow
:
645 if (trackingGlobals
== NULL
)
648 if (trackingGlobals
->m_currentTarget
)
650 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
651 trackingGlobals
->m_currentTarget
->OnLeave();
652 HideDragHilite( theDrag
);
653 trackingGlobals
->m_currentTarget
= NULL
;
655 trackingGlobals
->m_currentTargetWindow
= NULL
;
665 pascal OSErr
wxMacWindowDragReceiveHandler(
668 DragReference theDrag
)
670 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
671 if ( trackingGlobals
->m_currentTarget
)
673 Point mouse
, localMouse
;
676 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
677 GetDragMouse(theDrag
, &mouse
, 0L);
679 GlobalToLocal(&localMouse
);
680 localx
= localMouse
.h
;
681 localy
= localMouse
.v
;
683 // TODO : should we use client coordinates?
684 if ( trackingGlobals
->m_currentTargetWindow
)
685 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
686 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
688 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
689 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
690 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
) ;