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/window.h"
21 #include "wx/toplevel.h"
22 #include "wx/gdicmn.h"
25 #include "wx/mac/private.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
38 wxWindow
*m_currentTargetWindow
;
39 wxDropTarget
*m_currentTarget
;
40 wxDropSource
*m_currentSource
;
44 MacTrackingGlobals gTrackingGlobals
;
46 void wxMacEnsureTrackingHandlersInstalled();
48 //----------------------------------------------------------------------------
50 //----------------------------------------------------------------------------
52 wxDropTarget::wxDropTarget( wxDataObject
*data
)
53 : wxDropTargetBase( data
)
55 wxMacEnsureTrackingHandlersInstalled();
58 wxDragResult
wxDropTarget::OnDragOver(
59 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
62 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
65 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
67 if (m_dataObject
== NULL
)
70 return CurrentDragHasSupportedFormat();
73 wxDragResult
wxDropTarget::OnData(
74 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
77 if (m_dataObject
== NULL
)
80 if (!CurrentDragHasSupportedFormat())
83 return GetData() ? def
: wxDragNone
;
86 bool wxDropTarget::CurrentDragHasSupportedFormat()
88 bool supported
= false;
90 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
92 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
96 size_t formatcount
= data
->GetFormatCount();
97 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
98 data
->GetAllFormats( array
);
99 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
101 wxDataFormat format
= array
[i
];
102 if ( m_dataObject
->IsSupported( format
) )
117 ItemReference theItem
;
121 CountDragItems( (DragReference
)m_currentDrag
, &items
);
122 for (UInt16 index
= 1; index
<= items
&& !supported
; ++index
)
125 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
126 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
128 for ( UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
130 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
131 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
143 bool wxDropTarget::GetData()
145 if (m_dataObject
== NULL
)
148 if ( !CurrentDragHasSupportedFormat() )
151 bool transferred
= false;
152 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
154 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
158 size_t formatcount
= data
->GetFormatCount();
159 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
160 data
->GetAllFormats( array
);
161 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
163 wxDataFormat format
= array
[i
];
164 if ( m_dataObject
->IsSupported( format
) )
166 int size
= data
->GetDataSize( format
);
171 m_dataObject
->SetData( format
, 0, 0 );
175 char *d
= new char[size
];
176 data
->GetDataHere( format
, (void*)d
);
177 m_dataObject
->SetData( format
, size
, d
);
191 ItemReference theItem
;
193 FlavorFlags theFlags
;
195 wxString filenamesPassed
;
197 CountDragItems( (DragReference
)m_currentDrag
, &items
);
198 for (UInt16 index
= 1; index
<= items
; ++index
)
201 GetDragItemReferenceNumber( (DragReference
)m_currentDrag
, index
, &theItem
);
202 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
);
203 wxDataFormat preferredFormat
= m_dataObject
->GetPreferredFormat( wxDataObject::Set
);
204 bool hasPreferredFormat
= false;
206 for (UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
208 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
209 wxDataFormat
format( theType
);
210 if (preferredFormat
== format
)
212 hasPreferredFormat
= true;
217 for (UInt16 flavor
= 1; flavor
<= flavors
; ++flavor
)
219 result
= GetFlavorType( (DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
220 wxDataFormat
format( theType
);
221 if ((hasPreferredFormat
&& format
== preferredFormat
)
222 || (!hasPreferredFormat
&& m_dataObject
->IsSupportedFormat( format
)))
224 result
= GetFlavorFlags( (DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
230 GetFlavorDataSize( (DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
231 if (theType
== kScrapFlavorTypeText
)
233 // this increment is only valid for allocating:
234 // on the next GetFlavorData call it is reset again to the original value
237 else if (theType
== kScrapFlavorTypeUnicode
)
239 // this increment is only valid for allocating:
240 // on the next GetFlavorData call it is reset again to the original value
246 theData
= new char[dataSize
];
250 GetFlavorData( (DragReference
)m_currentDrag
, theItem
, theType
, (void*)theData
, &dataSize
, 0L );
253 case kScrapFlavorTypeText
:
254 theData
[dataSize
] = 0;
255 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
259 case kScrapFlavorTypeUnicode
:
260 theData
[dataSize
+ 0] =
261 theData
[dataSize
+ 1] = 0;
262 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
266 case kDragFlavorTypeHFS
:
269 HFSFlavor
* theFile
= (HFSFlavor
*)theData
;
270 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
);
273 filenamesPassed
+= name
+ wxT("\n");
278 m_dataObject
->SetData( format
, dataSize
, theData
);
289 if (filenamesPassed
.length() > 0)
291 wxCharBuffer buf
= filenamesPassed
.fn_str();
292 m_dataObject
->SetData( wxDataFormat(wxDF_FILENAME
), strlen( buf
), (const char*)buf
);
299 //-------------------------------------------------------------------------
301 //-------------------------------------------------------------------------
303 //-----------------------------------------------------------------------------
306 wxDropSource::wxDropSource(wxWindow
*win
,
307 const wxCursor
&cursorCopy
,
308 const wxCursor
&cursorMove
,
309 const wxCursor
&cursorStop
)
310 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
312 wxMacEnsureTrackingHandlersInstalled();
317 wxDropSource::wxDropSource(wxDataObject
& data
,
319 const wxCursor
&cursorCopy
,
320 const wxCursor
&cursorMove
,
321 const wxCursor
&cursorStop
)
322 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
324 wxMacEnsureTrackingHandlersInstalled();
330 wxDropSource::~wxDropSource()
334 wxDragResult
wxDropSource::DoDragDrop(int flags
)
336 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
338 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
339 return (wxDragResult
)wxDragNone
;
342 DragReference theDrag
;
343 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
) )
622 int cursorID
= wxCURSOR_NONE
;
627 cursorID
= wxCURSOR_COPY_ARROW
;
631 cursorID
= wxCURSOR_ARROW
;
635 cursorID
= wxCURSOR_NO_ENTRY
;
642 // put these here to make gcc happy
646 if (cursorID
!= wxCURSOR_NONE
)
648 wxCursor
cursor( cursorID
);
656 case kDragTrackingLeaveWindow
:
657 if (trackingGlobals
== NULL
)
660 if (trackingGlobals
->m_currentTarget
)
662 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
663 trackingGlobals
->m_currentTarget
->OnLeave();
664 HideDragHilite( theDrag
);
665 trackingGlobals
->m_currentTarget
= NULL
;
667 trackingGlobals
->m_currentTargetWindow
= NULL
;
677 pascal OSErr
wxMacWindowDragReceiveHandler(
680 DragReference theDrag
)
682 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
683 if ( trackingGlobals
->m_currentTarget
)
685 Point mouse
, localMouse
;
688 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
689 GetDragMouse( theDrag
, &mouse
, 0L );
691 GlobalToLocal( &localMouse
);
692 localx
= localMouse
.h
;
693 localy
= localMouse
.v
;
695 // TODO : should we use client coordinates?
696 if ( trackingGlobals
->m_currentTargetWindow
)
697 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
698 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
700 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
701 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
702 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);