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/toplevel.h"
21 #include "wx/gdicmn.h"
24 #include "wx/mac/private.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
32 wxWindow
*m_currentTargetWindow
;
33 wxDropTarget
*m_currentTarget
;
34 wxDropSource
*m_currentSource
;
35 wxDragResult m_result
;
39 MacTrackingGlobals gTrackingGlobals
;
41 void wxMacEnsureTrackingHandlersInstalled();
43 //----------------------------------------------------------------------------
45 //----------------------------------------------------------------------------
47 wxDropTarget::wxDropTarget( wxDataObject
*data
)
48 : wxDropTargetBase( data
)
50 wxMacEnsureTrackingHandlersInstalled();
53 wxDragResult
wxDropTarget::OnDragOver(
54 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
57 return CurrentDragHasSupportedFormat() ? def
: wxDragNone
;
60 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
62 if (m_dataObject
== NULL
)
65 return CurrentDragHasSupportedFormat();
68 wxDragResult
wxDropTarget::OnData(
69 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;
84 if (m_dataObject
== NULL
)
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 PasteboardRef pasteboard
;
114 if ( GetDragPasteboard( (DragReference
)m_currentDrag
, &pasteboard
) == noErr
)
116 supported
= m_dataObject
->HasDataInPasteboard( pasteboard
);
123 bool wxDropTarget::GetData()
125 if (m_dataObject
== NULL
)
128 if ( !CurrentDragHasSupportedFormat() )
131 bool transferred
= false;
132 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
134 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
138 size_t formatcount
= data
->GetFormatCount();
139 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
140 data
->GetAllFormats( array
);
141 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
143 wxDataFormat format
= array
[i
];
144 if ( m_dataObject
->IsSupported( format
) )
146 int size
= data
->GetDataSize( format
);
151 m_dataObject
->SetData( format
, 0, 0 );
155 char *d
= new char[size
];
156 data
->GetDataHere( format
, (void*)d
);
157 m_dataObject
->SetData( format
, size
, d
);
169 PasteboardRef pasteboard
;
171 if ( GetDragPasteboard( (DragReference
)m_currentDrag
, &pasteboard
) == noErr
)
173 transferred
= m_dataObject
->GetFromPasteboard( pasteboard
);
180 //-------------------------------------------------------------------------
182 //-------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
187 wxDropSource::wxDropSource(wxWindow
*win
,
188 const wxCursor
&cursorCopy
,
189 const wxCursor
&cursorMove
,
190 const wxCursor
&cursorStop
)
191 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
193 wxMacEnsureTrackingHandlersInstalled();
198 wxDropSource::wxDropSource(wxDataObject
& data
,
200 const wxCursor
&cursorCopy
,
201 const wxCursor
&cursorMove
,
202 const wxCursor
&cursorStop
)
203 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
205 wxMacEnsureTrackingHandlersInstalled();
211 wxDropSource::~wxDropSource()
215 OSStatus
wxMacPromiseKeeper( PasteboardRef inPasteboard
, PasteboardItemID inItem
, CFStringRef inFlavorType
,
218 OSStatus err
= noErr
;
220 // we might add promises here later, inContext is the wxDropSource*
225 wxDragResult
wxDropSource::DoDragDrop(int flags
)
227 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
229 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
230 return (wxDragResult
)wxDragNone
;
232 DragReference theDrag
;
233 RgnHandle dragRegion
;
234 OSStatus err
= noErr
;
235 PasteboardRef pasteboard
;
239 err
= PasteboardCreate( kPasteboardUniqueName
, &pasteboard
);
243 // we add a dummy promise keeper because of strange messages when linking against carbon debug
244 err
= PasteboardSetPromiseKeeper( pasteboard
, wxMacPromiseKeeper
, this );
247 CFRelease( pasteboard
);
251 err
= PasteboardClear( pasteboard
);
254 CFRelease( pasteboard
);
257 PasteboardSynchronize( pasteboard
);
259 m_data
->AddToPasteboard( pasteboard
, 1 );
261 if (NewDragWithPasteboard( pasteboard
, &theDrag
) != noErr
)
263 CFRelease( pasteboard
);
267 dragRegion
= NewRgn();
268 RgnHandle tempRgn
= NewRgn();
271 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent(), &rec
);
273 const short dragRegionOuterBoundary
= 10;
274 const short dragRegionInnerBoundary
= 9;
278 rec
.where
.h
- dragRegionOuterBoundary
,
279 rec
.where
.v
- dragRegionOuterBoundary
,
280 rec
.where
.h
+ dragRegionOuterBoundary
,
281 rec
.where
.v
+ dragRegionOuterBoundary
);
285 rec
.where
.h
- dragRegionInnerBoundary
,
286 rec
.where
.v
- dragRegionInnerBoundary
,
287 rec
.where
.h
+ dragRegionInnerBoundary
,
288 rec
.where
.v
+ dragRegionInnerBoundary
);
290 DiffRgn( dragRegion
, tempRgn
, dragRegion
);
291 DisposeRgn( tempRgn
);
293 // TODO: work with promises in order to return data
294 // only when drag was successfully completed
296 gTrackingGlobals
.m_currentSource
= this;
297 gTrackingGlobals
.m_result
= wxDragNone
;
298 gTrackingGlobals
.m_flags
= flags
;
300 err
= TrackDrag( theDrag
, &rec
, dragRegion
);
302 DisposeRgn( dragRegion
);
303 DisposeDrag( theDrag
);
304 CFRelease( pasteboard
);
305 gTrackingGlobals
.m_currentSource
= NULL
;
307 return gTrackingGlobals
.m_result
;
310 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
312 const wxCursor
& cursor
= GetCursor(effect
);
313 bool result
= cursor
.Ok();
321 bool gTrackingGlobalsInstalled
= false;
323 // passing the globals via refcon is not needed by the CFM and later architectures anymore
324 // but I'll leave it in there, just in case...
326 pascal OSErr
wxMacWindowDragTrackingHandler(
327 DragTrackingMessage theMessage
, WindowPtr theWindow
,
328 void *handlerRefCon
, DragReference theDrag
);
329 pascal OSErr
wxMacWindowDragReceiveHandler(
330 WindowPtr theWindow
, void *handlerRefCon
,
331 DragReference theDrag
);
333 void wxMacEnsureTrackingHandlersInstalled()
335 if ( !gTrackingGlobalsInstalled
)
339 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
342 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
345 gTrackingGlobalsInstalled
= true;
349 pascal OSErr
wxMacWindowDragTrackingHandler(
350 DragTrackingMessage theMessage
, WindowPtr theWindow
,
351 void *handlerRefCon
, DragReference theDrag
)
353 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
355 Point mouse
, localMouse
;
356 DragAttributes attributes
;
358 GetDragAttributes( theDrag
, &attributes
);
360 wxTopLevelWindowMac
* toplevel
= wxFindWinFromMacWindow( theWindow
);
362 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
363 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
367 case kDragTrackingEnterHandler
:
368 case kDragTrackingLeaveHandler
:
371 case kDragTrackingEnterWindow
:
372 if (trackingGlobals
!= NULL
)
374 trackingGlobals
->m_currentTargetWindow
= NULL
;
375 trackingGlobals
->m_currentTarget
= NULL
;
379 case kDragTrackingInWindow
:
380 if (trackingGlobals
== NULL
)
382 if (toplevel
== NULL
)
385 GetDragMouse( theDrag
, &mouse
, 0L );
387 wxMacGlobalToLocal( theWindow
, &localMouse
);
390 wxWindow
*win
= NULL
;
391 ControlPartCode controlPart
;
392 ControlRef control
= wxMacFindControlUnderMouse(
393 toplevel
, localMouse
, theWindow
, &controlPart
);
395 win
= wxFindControlFromMacControl( control
);
400 localx
= localMouse
.h
;
401 localy
= localMouse
.v
;
404 win
->MacRootWindowToWindow( &localx
, &localy
);
405 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
407 if ( trackingGlobals
->m_currentTargetWindow
)
409 // this window is left
410 if ( trackingGlobals
->m_currentTarget
)
413 HideDragHilite( theDrag
);
415 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
416 trackingGlobals
->m_currentTarget
->OnLeave();
417 trackingGlobals
->m_currentTarget
= NULL
;
418 trackingGlobals
->m_currentTargetWindow
= NULL
;
424 // this window is entered
425 trackingGlobals
->m_currentTargetWindow
= win
;
426 trackingGlobals
->m_currentTarget
= win
->GetDropTarget();
428 if ( trackingGlobals
->m_currentTarget
)
430 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
431 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
);
434 if ( result
!= wxDragNone
)
439 win
->MacWindowToRootWindow( &x
, &y
);
440 RgnHandle hiliteRgn
= NewRgn();
441 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
};
442 RectRgn( hiliteRgn
, &r
);
444 ShowDragHilite( theDrag
, hiliteRgn
, true );
446 DisposeRgn( hiliteRgn
);
453 if ( trackingGlobals
->m_currentTarget
)
455 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
456 result
= trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
);
460 // set cursor for OnEnter and OnDragOver
461 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
463 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
465 int cursorID
= wxCURSOR_NONE
;
470 cursorID
= wxCURSOR_COPY_ARROW
;
474 cursorID
= wxCURSOR_ARROW
;
478 cursorID
= wxCURSOR_NO_ENTRY
;
485 // put these here to make gcc happy
489 if (cursorID
!= wxCURSOR_NONE
)
491 wxCursor
cursor( cursorID
);
499 case kDragTrackingLeaveWindow
:
500 if (trackingGlobals
== NULL
)
503 if (trackingGlobals
->m_currentTarget
)
505 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
506 trackingGlobals
->m_currentTarget
->OnLeave();
508 HideDragHilite( theDrag
);
510 trackingGlobals
->m_currentTarget
= NULL
;
512 trackingGlobals
->m_currentTargetWindow
= NULL
;
522 pascal OSErr
wxMacWindowDragReceiveHandler(
525 DragReference theDrag
)
527 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
528 if ( trackingGlobals
->m_currentTarget
)
530 Point mouse
, localMouse
;
533 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
534 GetDragMouse( theDrag
, &mouse
, 0L );
536 wxMacGlobalToLocal( theWindow
, &localMouse
);
537 localx
= localMouse
.h
;
538 localy
= localMouse
.v
;
540 // TODO : should we use client coordinates?
541 if ( trackingGlobals
->m_currentTargetWindow
)
542 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
543 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
545 // the option key indicates copy in Mac UI, if it's not pressed do
546 // move by default if it's allowed at all
548 result
= !(trackingGlobals
->m_flags
& wxDrag_AllowMove
) ||
549 (GetCurrentKeyModifiers() & optionKey
)
552 trackingGlobals
->m_result
=
553 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);
560 #endif // wxUSE_DRAG_AND_DROP