1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) 1998 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dnd.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"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 void wxMacEnsureTrackingHandlersInstalled() ;
35 wxWindow
* m_currentTargetWindow
;
36 wxDropTarget
* m_currentTarget
;
37 wxDropSource
* m_currentSource
;
38 } MacTrackingGlobals
;
40 MacTrackingGlobals gTrackingGlobals
;
42 //----------------------------------------------------------------------------
44 //----------------------------------------------------------------------------
46 wxDropTarget::wxDropTarget( wxDataObject
*data
)
47 : wxDropTargetBase( data
)
49 wxMacEnsureTrackingHandlersInstalled() ;
52 wxDragResult
wxDropTarget::OnDragOver( wxCoord
WXUNUSED(x
),
57 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
60 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
65 return CurrentDragHasSupportedFormat() ;
68 wxDragResult
wxDropTarget::OnData( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
74 if (!CurrentDragHasSupportedFormat())
77 return GetData() ? def
: wxDragNone
;
80 bool wxDropTarget::CurrentDragHasSupportedFormat()
82 bool supported
= false ;
83 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
85 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
89 size_t formatcount
= data
->GetFormatCount() ;
90 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
91 data
->GetAllFormats( array
);
92 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
94 wxDataFormat format
= array
[i
] ;
95 if ( m_dataObject
->IsSupported( format
) )
108 CountDragItems((DragReference
)m_currentDrag
, &items
);
109 for (UInt16 index
= 1; index
<= items
&& supported
== false ; ++index
)
111 ItemReference theItem
;
114 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
115 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
116 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
118 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
119 if ( m_dataObject
->IsSupportedFormat( wxDataFormat( theType
) ) )
130 bool wxDropTarget::GetData()
135 if ( !CurrentDragHasSupportedFormat() )
138 bool transferred
= false ;
139 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
141 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject() ;
145 size_t formatcount
= data
->GetFormatCount() ;
146 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
147 data
->GetAllFormats( array
);
148 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
150 wxDataFormat format
= array
[i
] ;
151 if ( m_dataObject
->IsSupported( format
) )
153 int size
= data
->GetDataSize( format
);
158 m_dataObject
->SetData(format
, 0 , 0 ) ;
162 char *d
= new char[size
];
163 data
->GetDataHere( format
, (void*) d
);
164 m_dataObject
->SetData( format
, size
, d
) ;
176 bool firstFileAdded
= false ;
177 CountDragItems((DragReference
)m_currentDrag
, &items
);
178 for (UInt16 index
= 1; index
<= items
; ++index
)
180 ItemReference theItem
;
183 GetDragItemReferenceNumber((DragReference
)m_currentDrag
, index
, &theItem
);
184 CountDragItemFlavors( (DragReference
)m_currentDrag
, theItem
, &flavors
) ;
185 for ( UInt16 flavor
= 1 ; flavor
<= flavors
; ++flavor
)
187 result
= GetFlavorType((DragReference
)m_currentDrag
, theItem
, flavor
, &theType
);
188 wxDataFormat
format(theType
) ;
189 if ( m_dataObject
->IsSupportedFormat( format
) )
191 FlavorFlags theFlags
;
192 result
= GetFlavorFlags((DragReference
)m_currentDrag
, theItem
, theType
, &theFlags
);
197 GetFlavorDataSize((DragReference
)m_currentDrag
, theItem
, theType
, &dataSize
);
198 if ( theType
== kScrapFlavorTypeText
)
200 // this increment is only valid for allocating, on the next GetFlavorData
201 // call it is reset again to the original value
204 else if ( theType
== kScrapFlavorTypeUnicode
)
206 // this increment is only valid for allocating, on the next GetFlavorData
207 // call it is reset again to the original value
211 theData
= new char[dataSize
];
212 GetFlavorData((DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L);
213 if( theType
== kScrapFlavorTypeText
)
215 theData
[dataSize
]=0 ;
216 m_dataObject
->SetData( wxDataFormat(wxDF_TEXT
), dataSize
, theData
);
219 else if ( theType
== kScrapFlavorTypeUnicode
)
221 theData
[dataSize
]=0 ;
222 theData
[dataSize
+1]=0 ;
223 m_dataObject
->SetData( wxDataFormat(wxDF_UNICODETEXT
), dataSize
, theData
);
226 else if ( theType
== kDragFlavorTypeHFS
)
228 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
229 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
230 if ( !firstFileAdded
)
233 ((wxFileDataObject
*)m_dataObject
)->SetData( 0 , "" ) ;
234 firstFileAdded
= true ;
236 ((wxFileDataObject
*)m_dataObject
)->AddFile( name
) ;
240 m_dataObject
->SetData( format
, dataSize
, theData
);
252 //-------------------------------------------------------------------------
254 //-------------------------------------------------------------------------
256 //-----------------------------------------------------------------------------
259 wxDropSource::wxDropSource(wxWindow
*win
,
260 const wxCursor
&cursorCopy
,
261 const wxCursor
&cursorMove
,
262 const wxCursor
&cursorStop
)
263 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
265 wxMacEnsureTrackingHandlersInstalled() ;
269 wxDropSource::wxDropSource(wxDataObject
& data
,
271 const wxCursor
&cursorCopy
,
272 const wxCursor
&cursorMove
,
273 const wxCursor
&cursorStop
)
274 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
276 wxMacEnsureTrackingHandlersInstalled() ;
281 wxDropSource::~wxDropSource()
286 wxDragResult
wxDropSource::DoDragDrop(int WXUNUSED(flags
))
288 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
291 return (wxDragResult
) wxDragNone
;
293 if (m_data
->GetFormatCount() == 0)
294 return (wxDragResult
) wxDragNone
;
297 DragReference theDrag
;
298 RgnHandle dragRegion
;
299 if ((result
= NewDrag(&theDrag
)))
304 size_t formatCount
= m_data
->GetFormatCount() ;
305 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
306 m_data
->GetAllFormats( formats
) ;
307 ItemReference theItem
= 1 ;
308 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
310 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
311 Ptr dataPtr
= new char[dataSize
] ;
312 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
313 OSType type
= formats
[i
].GetFormatId() ;
314 if ( type
== 'TEXT' )
317 dataPtr
[ dataSize
] = 0 ;
318 wxString
st( (wxChar
*) dataPtr
) ;
319 wxCharBuffer buf
= st
.mb_str( wxConvLocal
) ;
320 AddDragItemFlavor(theDrag
, theItem
, type
, buf
.data(), strlen(buf
), 0);
322 else if (type
== kDragFlavorTypeHFS
)
324 HFSFlavor theFlavor
;
328 wxMacFilename2FSSpec( dataPtr
, &theFlavor
.fileSpec
) ;
330 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
331 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
332 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
333 cat
.hFileInfo
.ioFDirIndex
= 0;
334 err
= PBGetCatInfoSync(&cat
);
337 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
338 if (theFlavor
.fileSpec
.parID
== fsRtParID
) {
339 theFlavor
.fileCreator
= 'MACS';
340 theFlavor
.fileType
= 'disk';
341 } else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0) {
342 theFlavor
.fileCreator
= 'MACS';
343 theFlavor
.fileType
= 'fold';
345 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
346 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
348 AddDragItemFlavor(theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0);
353 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
359 dragRegion
= NewRgn();
360 RgnHandle tempRgn
= NewRgn() ;
362 EventRecord
* ev
= NULL
;
363 #if !TARGET_CARBON // TODO
364 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
368 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
370 const short dragRegionOuterBoundary
= 10 ;
371 const short dragRegionInnerBoundary
= 9 ;
373 SetRectRgn( dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
374 ev
->where
.v
- dragRegionOuterBoundary
,
375 ev
->where
.h
+ dragRegionOuterBoundary
,
376 ev
->where
.v
+ dragRegionOuterBoundary
) ;
378 SetRectRgn( tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
379 ev
->where
.v
- dragRegionInnerBoundary
,
380 ev
->where
.h
+ dragRegionInnerBoundary
,
381 ev
->where
.v
+ dragRegionInnerBoundary
) ;
383 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
384 DisposeRgn( tempRgn
) ;
386 // TODO:work with promises in order to return data only when drag
387 // was successfully completed
389 gTrackingGlobals
.m_currentSource
= this ;
390 result
= TrackDrag(theDrag
, ev
, dragRegion
);
391 DisposeRgn(dragRegion
);
392 DisposeDrag(theDrag
);
393 gTrackingGlobals
.m_currentSource
= NULL
;
397 bool optionDown
= keymap
[1] & 4;
398 wxDragResult dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
402 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
404 const wxCursor
& cursor
= GetCursor(effect
);
407 cursor
.MacInstall() ;
417 bool gTrackingGlobalsInstalled
= false ;
419 // passing the globals via refcon is not needed by the CFM and later architectures anymore
420 // but I'll leave it in there, just in case...
422 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
423 void *handlerRefCon
, DragReference theDrag
) ;
424 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
425 DragReference theDrag
) ;
427 void wxMacEnsureTrackingHandlersInstalled()
429 if( !gTrackingGlobalsInstalled
)
433 result
= InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L,&gTrackingGlobals
);
434 wxASSERT( result
== noErr
) ;
435 result
= InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
436 wxASSERT( result
== noErr
) ;
438 gTrackingGlobalsInstalled
= true ;
442 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
443 void *handlerRefCon
, DragReference theDrag
)
445 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
446 Point mouse
, localMouse
;
447 DragAttributes attributes
;
448 GetDragAttributes(theDrag
, &attributes
);
449 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
) ;
453 bool optionDown
= keymap
[1] & 4;
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( 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
) )
620 bool optionDown
= keymap
[1] & 4;
621 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
622 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
) ;