1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) 1998 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dnd.h"
16 #include "wx/wxprec.h"
18 #if wxUSE_DRAG_AND_DROP
21 #include "wx/window.h"
22 #include "wx/toplevel.h"
24 #include "wx/gdicmn.h"
25 #include "wx/mac/private.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 void wxMacEnsureTrackingHandlersInstalled() ;
39 wxWindow
* m_currentTargetWindow
;
40 wxDropTarget
* m_currentTarget
;
41 wxDropSource
* m_currentSource
;
42 } MacTrackingGlobals
;
44 MacTrackingGlobals gTrackingGlobals
;
46 //----------------------------------------------------------------------------
48 //----------------------------------------------------------------------------
50 wxDropTarget::wxDropTarget( wxDataObject
*data
)
51 : wxDropTargetBase( data
)
53 wxMacEnsureTrackingHandlersInstalled() ;
56 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
61 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
64 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
69 return CurrentDragHasSupportedFormat() ;
72 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
78 if (!CurrentDragHasSupportedFormat())
81 return GetData() ? def
: wxDragNone
;
84 bool wxDropTarget::CurrentDragHasSupportedFormat()
86 bool supported
= false ;
87 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
89 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
93 size_t formatcount
= data
->GetFormatCount() ;
94 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
95 data
->GetAllFormats( array
);
96 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
98 wxDataFormat format
= array
[i
] ;
99 if ( m_dataObject
->IsSupported( format
) )
112 CountDragItems((DragReference
)m_currentDrag
, &items
);
113 for (UInt16 index
= 1; index
<= items
&& supported
== false ; ++index
)
115 ItemReference theItem
;
118 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
119 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
120 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
122 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
123 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
134 bool wxDropTarget::GetData()
139 if ( !CurrentDragHasSupportedFormat() )
142 bool transferred
= false ;
143 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
145 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
149 size_t formatcount
= data
->GetFormatCount() ;
150 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
151 data
->GetAllFormats( array
);
152 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
154 wxDataFormat format
= array
[i
] ;
155 if ( m_dataObject
->IsSupported( format
) )
157 int size
= data
->GetDataSize( format
);
162 m_dataObject
->SetData(format
, 0 , 0 ) ;
166 char *d
= new char[size
];
167 data
->GetDataHere( format
, (void*) d
);
168 m_dataObject
->SetData( format
, size
, d
) ;
180 bool firstFileAdded
= false ;
181 CountDragItems((DragReference
)m_currentDrag
, &items
);
182 for (UInt16 index
= 1; index
<= items
; ++index
)
184 ItemReference theItem
;
187 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
188 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
189 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
191 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
192 wxDataFormat
format(theType
) ;
193 if ( m_dataObject
->IsSupportedFormat( format
) )
195 FlavorFlags theFlags
;
196 result
= GetFlavorFlags((DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
201 GetFlavorDataSize((DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
202 if ( theType
== kScrapFlavorTypeText
)
204 // this increment is only valid for allocating, on the next GetFlavorData
205 // call it is reset again to the original value
208 else if ( theType
== kScrapFlavorTypeUnicode
)
210 // this increment is only valid for allocating, on the next GetFlavorData
211 // call it is reset again to the original value
215 theData
= new char[dataSize
];
216 GetFlavorData((DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L);
217 if( theType
== kScrapFlavorTypeText
)
219 theData
[dataSize
]=0 ;
220 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
223 else if ( theType
== kScrapFlavorTypeUnicode
)
225 theData
[dataSize
]=0 ;
226 theData
[dataSize
+1]=0 ;
227 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
230 else if ( theType
== kDragFlavorTypeHFS
)
232 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
233 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
234 if ( !firstFileAdded
)
237 ((wxFileDataObject
*)m_dataObject
)->SetData( 0 , "" ) ;
238 firstFileAdded
= true ;
240 ((wxFileDataObject
*)m_dataObject
)->AddFile( name
) ;
244 m_dataObject
->SetData( format
, dataSize
, theData
);
256 //-------------------------------------------------------------------------
258 //-------------------------------------------------------------------------
260 //-----------------------------------------------------------------------------
263 wxDropSource::wxDropSource(wxWindow
*win
,
264 const wxCursor
&cursorCopy
,
265 const wxCursor
&cursorMove
,
266 const wxCursor
&cursorStop
)
267 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
269 wxMacEnsureTrackingHandlersInstalled() ;
273 wxDropSource::wxDropSource(wxDataObject
& data
,
275 const wxCursor
&cursorCopy
,
276 const wxCursor
&cursorMove
,
277 const wxCursor
&cursorStop
)
278 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
280 wxMacEnsureTrackingHandlersInstalled() ;
285 wxDropSource::~wxDropSource()
290 wxDragResult
wxDropSource::DoDragDrop(int WXUNUSED(flags
))
292 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
295 return (wxDragResult
) wxDragNone
;
297 if (m_data
->GetFormatCount() == 0)
298 return (wxDragResult
) wxDragNone
;
301 DragReference theDrag
;
302 RgnHandle dragRegion
;
303 if ((result
= NewDrag(&theDrag
)))
308 size_t formatCount
= m_data
->GetFormatCount() ;
309 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
310 m_data
->GetAllFormats( formats
) ;
311 ItemReference theItem
= 1 ;
312 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
314 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
315 Ptr dataPtr
= new char[dataSize
] ;
316 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
317 OSType type
= formats
[i
].GetFormatId() ;
318 if ( type
== 'TEXT' )
321 dataPtr
[ dataSize
] = 0 ;
322 wxString
st( (wxChar
*) dataPtr
) ;
323 wxCharBuffer buf
= st
.mb_str( wxConvLocal
) ;
324 AddDragItemFlavor(theDrag
, theItem
, type
, buf
.data(), strlen(buf
), 0);
326 else if (type
== kDragFlavorTypeHFS
)
328 HFSFlavor theFlavor
;
332 wxMacFilename2FSSpec( wxString( dataPtr
, *wxConvCurrent
) , &theFlavor
.fileSpec
) ;
334 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
335 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
336 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
337 cat
.hFileInfo
.ioFDirIndex
= 0;
338 err
= PBGetCatInfoSync(&cat
);
341 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
342 if (theFlavor
.fileSpec
.parID
== fsRtParID
) {
343 theFlavor
.fileCreator
= 'MACS';
344 theFlavor
.fileType
= 'disk';
345 } else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0) {
346 theFlavor
.fileCreator
= 'MACS';
347 theFlavor
.fileType
= 'fold';
349 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
350 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
352 AddDragItemFlavor(theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0);
357 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
363 dragRegion
= NewRgn();
364 RgnHandle tempRgn
= NewRgn() ;
366 EventRecord
* ev
= NULL
;
367 #if !TARGET_CARBON // TODO
368 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
372 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
374 const short dragRegionOuterBoundary
= 10 ;
375 const short dragRegionInnerBoundary
= 9 ;
377 SetRectRgn( dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
378 ev
->where
.v
- dragRegionOuterBoundary
,
379 ev
->where
.h
+ dragRegionOuterBoundary
,
380 ev
->where
.v
+ dragRegionOuterBoundary
) ;
382 SetRectRgn( tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
383 ev
->where
.v
- dragRegionInnerBoundary
,
384 ev
->where
.h
+ dragRegionInnerBoundary
,
385 ev
->where
.v
+ dragRegionInnerBoundary
) ;
387 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
388 DisposeRgn( tempRgn
) ;
390 // TODO:work with promises in order to return data only when drag
391 // was successfully completed
393 gTrackingGlobals
.m_currentSource
= this ;
394 result
= TrackDrag(theDrag
, ev
, dragRegion
);
395 DisposeRgn(dragRegion
);
396 DisposeDrag(theDrag
);
397 gTrackingGlobals
.m_currentSource
= NULL
;
399 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
400 wxDragResult dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
404 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
406 const wxCursor
& cursor
= GetCursor(effect
);
409 cursor
.MacInstall() ;
419 bool gTrackingGlobalsInstalled
= false ;
421 // passing the globals via refcon is not needed by the CFM and later architectures anymore
422 // but I'll leave it in there, just in case...
424 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
425 void *handlerRefCon
, DragReference theDrag
) ;
426 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
427 DragReference theDrag
) ;
429 void wxMacEnsureTrackingHandlersInstalled()
431 if( !gTrackingGlobalsInstalled
)
435 result
= InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L,&gTrackingGlobals
);
436 wxASSERT( result
== noErr
) ;
437 result
= InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
438 wxASSERT( result
== noErr
) ;
440 gTrackingGlobalsInstalled
= true ;
444 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
445 void *handlerRefCon
, DragReference theDrag
)
447 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
448 Point mouse
, localMouse
;
449 DragAttributes attributes
;
450 GetDragAttributes(theDrag
, &attributes
);
451 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
453 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
454 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
458 case kDragTrackingEnterHandler
:
460 case kDragTrackingLeaveHandler
:
462 case kDragTrackingEnterWindow
:
463 trackingGlobals
->m_currentTargetWindow
= NULL
;
464 trackingGlobals
->m_currentTarget
= NULL
;
466 case kDragTrackingInWindow
:
467 if (toplevel
== NULL
)
470 GetDragMouse(theDrag
, &mouse
, 0L);
472 GlobalToLocal(&localMouse
);
476 // if (attributes & kDragHasLeftSenderWindow)
478 // wxPoint point(localMouse.h , localMouse.v) ;
479 wxWindow
*win
= NULL
;
480 ControlPartCode controlPart
;
481 ControlRef control
= wxMacFindControlUnderMouse( toplevel
, localMouse
,
482 theWindow
, &controlPart
) ;
484 win
= wxFindControlFromMacControl( control
) ;
485 // TODO toplevel->MacGetWindowFromPointSub( point , &win ) ;
486 int localx
, localy
;
487 localx
= localMouse
.h
;
488 localy
= localMouse
.v
;
489 //TODO : should we use client coordinates
491 win
->MacRootWindowToWindow( &localx
, &localy
) ;
492 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
494 if ( trackingGlobals
->m_currentTargetWindow
)
496 // this window is left
497 if ( trackingGlobals
->m_currentTarget
)
499 HideDragHilite(theDrag
);
500 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
501 trackingGlobals
->m_currentTarget
->OnLeave() ;
502 trackingGlobals
->m_currentTarget
= NULL
;
503 trackingGlobals
->m_currentTargetWindow
= NULL
;
508 // this window is entered
509 trackingGlobals
->m_currentTargetWindow
= win
;
510 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
513 if ( trackingGlobals
->m_currentTarget
)
515 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
516 result
= trackingGlobals
->m_currentTarget
->OnEnter(
517 localx
, localy
, result
) ;
521 if ( result
!= wxDragNone
)
525 win
->MacWindowToRootWindow( &x
, &y
) ;
526 RgnHandle hiliteRgn
= NewRgn() ;
527 Rect r
= { y
, x
, y
+win
->GetSize().y
, x
+win
->GetSize().x
} ;
528 RectRgn( hiliteRgn
, &r
) ;
529 ShowDragHilite(theDrag
, hiliteRgn
, true);
530 DisposeRgn( hiliteRgn
) ;
537 if( trackingGlobals
->m_currentTarget
)
539 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
540 trackingGlobals
->m_currentTarget
->OnDragOver(
541 localx
, localy
, result
) ;
545 // set cursor for OnEnter and OnDragOver
546 if ( trackingGlobals
->m_currentSource
&& trackingGlobals
->m_currentSource
->GiveFeedback( result
) == FALSE
)
548 if ( trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) == FALSE
)
554 wxCursor
cursor(wxCURSOR_COPY_ARROW
) ;
555 cursor
.MacInstall() ;
560 wxCursor
cursor(wxCURSOR_ARROW
) ;
561 cursor
.MacInstall() ;
566 wxCursor
cursor(wxCURSOR_NO_ENTRY
) ;
567 cursor
.MacInstall() ;
574 // put these here to make gcc happy
581 // MyTrackItemUnderMouse(localMouse, theWindow);
583 case kDragTrackingLeaveWindow
:
584 if (trackingGlobals
->m_currentTarget
)
586 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
587 trackingGlobals
->m_currentTarget
->OnLeave() ;
588 HideDragHilite(theDrag
);
589 trackingGlobals
->m_currentTarget
= NULL
;
591 trackingGlobals
->m_currentTargetWindow
= NULL
;
597 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
,
599 DragReference theDrag
)
601 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
602 if ( trackingGlobals
->m_currentTarget
)
604 Point mouse
,localMouse
;
607 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
608 GetDragMouse(theDrag
, &mouse
, 0L);
610 GlobalToLocal(&localMouse
);
611 localx
= localMouse
.h
;
612 localy
= localMouse
.v
;
613 //TODO : should we use client coordinates
614 if ( trackingGlobals
->m_currentTargetWindow
)
615 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
616 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
618 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
619 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
620 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
) ;