1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/dnd.cpp
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) 1998 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_DRAG_AND_DROP
24 #include "wx/window.h"
27 #include "wx/toplevel.h"
28 #include "wx/gdicmn.h"
29 #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
== 'TEXT' )
204 // this increment is only valid for allocating, on the next GetFlavorData
205 // call it is reset again to the original value
208 theData
= new char[dataSize
];
209 GetFlavorData((DragReference
)m_currentDrag
, theItem
, theType
, (void*) theData
, &dataSize
, 0L);
210 if( theType
== 'TEXT' )
212 theData
[dataSize
]=0 ;
213 wxString
convert( theData
, wxConvLocal
) ;
214 m_dataObject
->SetData( format
, convert
.length() * sizeof(wxChar
), (const wxChar
*) convert
);
216 else if ( theType
== kDragFlavorTypeHFS
)
218 HFSFlavor
* theFile
= (HFSFlavor
*) theData
;
219 wxString name
= wxMacFSSpec2MacFilename( &theFile
->fileSpec
) ;
220 if ( firstFileAdded
)
221 ((wxFileDataObject
*)m_dataObject
)->AddFile( name
) ;
224 ((wxFileDataObject
*)m_dataObject
)->SetData( 0 , name
.c_str() ) ;
225 firstFileAdded
= true ;
230 m_dataObject
->SetData( format
, dataSize
, theData
);
242 //-------------------------------------------------------------------------
244 //-------------------------------------------------------------------------
246 //-----------------------------------------------------------------------------
249 wxDropSource::wxDropSource(wxWindow
*win
,
250 const wxCursor
&cursorCopy
,
251 const wxCursor
&cursorMove
,
252 const wxCursor
&cursorStop
)
253 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
255 wxMacEnsureTrackingHandlersInstalled() ;
259 wxDropSource::wxDropSource(wxDataObject
& data
,
261 const wxCursor
&cursorCopy
,
262 const wxCursor
&cursorMove
,
263 const wxCursor
&cursorStop
)
264 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
266 wxMacEnsureTrackingHandlersInstalled() ;
271 wxDropSource::~wxDropSource()
276 wxDragResult
wxDropSource::DoDragDrop(int WXUNUSED(flags
))
278 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
281 return (wxDragResult
) wxDragNone
;
283 if (m_data
->GetFormatCount() == 0)
284 return (wxDragResult
) wxDragNone
;
287 DragReference theDrag
;
288 RgnHandle dragRegion
;
289 if ((result
= NewDrag(&theDrag
)))
294 size_t formatCount
= m_data
->GetFormatCount() ;
295 wxDataFormat
*formats
= new wxDataFormat
[formatCount
] ;
296 m_data
->GetAllFormats( formats
) ;
297 ItemReference theItem
= 1 ;
298 for ( size_t i
= 0 ; i
< formatCount
; ++i
)
300 size_t dataSize
= m_data
->GetDataSize( formats
[i
] ) ;
301 Ptr dataPtr
= new char[dataSize
] ;
302 m_data
->GetDataHere( formats
[i
] , dataPtr
) ;
303 OSType type
= formats
[i
].GetFormatId() ;
304 if ( type
== 'TEXT' )
307 dataPtr
[ dataSize
] = 0 ;
308 wxString
st( (wxChar
*) dataPtr
) ;
309 wxCharBuffer buf
= st
.mb_str( wxConvLocal
) ;
310 AddDragItemFlavor(theDrag
, theItem
, type
, buf
.data(), strlen(buf
), 0);
312 else if (type
== kDragFlavorTypeHFS
)
314 HFSFlavor theFlavor
;
318 wxMacFilename2FSSpec( dataPtr
, &theFlavor
.fileSpec
) ;
320 cat
.hFileInfo
.ioNamePtr
= theFlavor
.fileSpec
.name
;
321 cat
.hFileInfo
.ioVRefNum
= theFlavor
.fileSpec
.vRefNum
;
322 cat
.hFileInfo
.ioDirID
= theFlavor
.fileSpec
.parID
;
323 cat
.hFileInfo
.ioFDirIndex
= 0;
324 err
= PBGetCatInfoSync(&cat
);
327 theFlavor
.fdFlags
= cat
.hFileInfo
.ioFlFndrInfo
.fdFlags
;
328 if (theFlavor
.fileSpec
.parID
== fsRtParID
) {
329 theFlavor
.fileCreator
= 'MACS';
330 theFlavor
.fileType
= 'disk';
331 } else if ((cat
.hFileInfo
.ioFlAttrib
& ioDirMask
) != 0) {
332 theFlavor
.fileCreator
= 'MACS';
333 theFlavor
.fileType
= 'fold';
335 theFlavor
.fileCreator
= cat
.hFileInfo
.ioFlFndrInfo
.fdCreator
;
336 theFlavor
.fileType
= cat
.hFileInfo
.ioFlFndrInfo
.fdType
;
338 AddDragItemFlavor(theDrag
, theItem
, type
, &theFlavor
, sizeof(theFlavor
), 0);
343 AddDragItemFlavor(theDrag
, theItem
, type
, dataPtr
, dataSize
, 0);
349 dragRegion
= NewRgn();
350 RgnHandle tempRgn
= NewRgn() ;
352 EventRecord
* ev
= NULL
;
353 #if !TARGET_CARBON // TODO
354 ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
358 wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
360 const short dragRegionOuterBoundary
= 10 ;
361 const short dragRegionInnerBoundary
= 9 ;
363 SetRectRgn( dragRegion
, ev
->where
.h
- dragRegionOuterBoundary
,
364 ev
->where
.v
- dragRegionOuterBoundary
,
365 ev
->where
.h
+ dragRegionOuterBoundary
,
366 ev
->where
.v
+ dragRegionOuterBoundary
) ;
368 SetRectRgn( tempRgn
, ev
->where
.h
- dragRegionInnerBoundary
,
369 ev
->where
.v
- dragRegionInnerBoundary
,
370 ev
->where
.h
+ dragRegionInnerBoundary
,
371 ev
->where
.v
+ dragRegionInnerBoundary
) ;
373 DiffRgn( dragRegion
, tempRgn
, dragRegion
) ;
374 DisposeRgn( tempRgn
) ;
376 // TODO:work with promises in order to return data only when drag
377 // was successfully completed
379 gTrackingGlobals
.m_currentSource
= this ;
380 result
= TrackDrag(theDrag
, ev
, dragRegion
);
381 DisposeRgn(dragRegion
);
382 DisposeDrag(theDrag
);
383 gTrackingGlobals
.m_currentSource
= NULL
;
387 bool optionDown
= keymap
[1] & 4;
388 wxDragResult dndresult
= optionDown
? wxDragCopy
: wxDragMove
;
392 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
394 const wxCursor
& cursor
= GetCursor(effect
);
397 cursor
.MacInstall() ;
407 bool gTrackingGlobalsInstalled
= false ;
409 // passing the globals via refcon is not needed by the CFM and later architectures anymore
410 // but I'll leave it in there, just in case...
412 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
413 void *handlerRefCon
, DragReference theDrag
) ;
414 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
, void *handlerRefCon
,
415 DragReference theDrag
) ;
417 void wxMacEnsureTrackingHandlersInstalled()
419 if( !gTrackingGlobalsInstalled
)
423 result
= InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L,&gTrackingGlobals
);
424 wxASSERT( result
== noErr
) ;
425 result
= InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
426 wxASSERT( result
== noErr
) ;
428 gTrackingGlobalsInstalled
= true ;
432 pascal OSErr
wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage
, WindowPtr theWindow
,
433 void *handlerRefCon
, DragReference theDrag
)
435 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
436 Point mouse
, localMouse
;
437 DragAttributes attributes
;
438 GetDragAttributes(theDrag
, &attributes
);
439 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( (WXWindow
) theWindow
) ;
443 bool optionDown
= keymap
[1] & 4;
444 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
448 case kDragTrackingEnterHandler
:
450 case kDragTrackingLeaveHandler
:
452 case kDragTrackingEnterWindow
:
453 trackingGlobals
->m_currentTargetWindow
= NULL
;
454 trackingGlobals
->m_currentTarget
= NULL
;
456 case kDragTrackingInWindow
:
457 if (toplevel
== NULL
)
460 GetDragMouse(theDrag
, &mouse
, 0L);
462 GlobalToLocal(&localMouse
);
466 // if (attributes & kDragHasLeftSenderWindow)
468 wxPoint
point(localMouse
.h
, localMouse
.v
) ;
469 wxWindow
*win
= NULL
;
470 toplevel
->MacGetWindowFromPointSub( point
, &win
) ;
471 int localx
, localy
;
472 localx
= localMouse
.h
;
473 localy
= localMouse
.v
;
474 //TODO : should we use client coordinates
476 win
->MacRootWindowToWindow( &localx
, &localy
) ;
477 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
479 if ( trackingGlobals
->m_currentTargetWindow
)
481 // this window is left
482 if ( trackingGlobals
->m_currentTarget
)
484 HideDragHilite(theDrag
);
485 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
486 trackingGlobals
->m_currentTarget
->OnLeave() ;
487 trackingGlobals
->m_currentTarget
= NULL
;
488 trackingGlobals
->m_currentTargetWindow
= NULL
;
493 // this window is entered
494 trackingGlobals
->m_currentTargetWindow
= win
;
495 trackingGlobals
->m_currentTarget
= win
->GetDropTarget() ;
498 if ( trackingGlobals
->m_currentTarget
)
500 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
501 result
= trackingGlobals
->m_currentTarget
->OnEnter(
502 localx
, localy
, result
) ;
506 if ( result
!= wxDragNone
)
510 win
->MacWindowToRootWindow( &x
, &y
) ;
511 RgnHandle hiliteRgn
= NewRgn() ;
512 SetRectRgn( hiliteRgn
, x
, y
, x
+win
->GetSize().x
,y
+win
->GetSize().y
) ;
513 ShowDragHilite(theDrag
, hiliteRgn
, true);
514 DisposeRgn( hiliteRgn
) ;
521 if( trackingGlobals
->m_currentTarget
)
523 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
524 trackingGlobals
->m_currentTarget
->OnDragOver(
525 localx
, localy
, result
) ;
529 // set cursor for OnEnter and OnDragOver
530 if ( trackingGlobals
->m_currentSource
&& trackingGlobals
->m_currentSource
->GiveFeedback( result
) == FALSE
)
532 if ( trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) == FALSE
)
538 wxCursor
cursor(wxCURSOR_COPY_ARROW
) ;
539 cursor
.MacInstall() ;
544 wxCursor
cursor(wxCURSOR_ARROW
) ;
545 cursor
.MacInstall() ;
550 wxCursor
cursor(wxCURSOR_NO_ENTRY
) ;
551 cursor
.MacInstall() ;
558 // put these here to make gcc happy
565 // MyTrackItemUnderMouse(localMouse, theWindow);
567 case kDragTrackingLeaveWindow
:
568 if (trackingGlobals
->m_currentTarget
)
570 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
571 trackingGlobals
->m_currentTarget
->OnLeave() ;
572 HideDragHilite(theDrag
);
573 trackingGlobals
->m_currentTarget
= NULL
;
575 trackingGlobals
->m_currentTargetWindow
= NULL
;
581 pascal OSErr
wxMacWindowDragReceiveHandler(WindowPtr theWindow
,
583 DragReference theDrag
)
585 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
586 if ( trackingGlobals
->m_currentTarget
)
588 Point mouse
,localMouse
;
591 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
) ;
592 GetDragMouse(theDrag
, &mouse
, 0L);
594 GlobalToLocal(&localMouse
);
595 localx
= localMouse
.h
;
596 localy
= localMouse
.v
;
597 //TODO : should we use client coordinates
598 if ( trackingGlobals
->m_currentTargetWindow
)
599 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
) ;
600 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
604 bool optionDown
= keymap
[1] & 4;
605 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
606 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
) ;