1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/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
) )
113 PasteboardRef pasteboard
;
115 if ( GetDragPasteboard( (DragReference
)m_currentDrag
, &pasteboard
) == noErr
)
117 supported
= m_dataObject
->HasDataInPasteboard( pasteboard
);
125 bool wxDropTarget::GetData()
127 if (m_dataObject
== NULL
)
130 if ( !CurrentDragHasSupportedFormat() )
133 bool transferred
= false;
134 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
136 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
140 size_t formatcount
= data
->GetFormatCount();
141 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
142 data
->GetAllFormats( array
);
143 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
145 wxDataFormat format
= array
[i
];
146 if ( m_dataObject
->IsSupported( format
) )
148 int size
= data
->GetDataSize( format
);
153 m_dataObject
->SetData( format
, 0, 0 );
157 char *d
= new char[size
];
158 data
->GetDataHere( format
, (void*)d
);
159 m_dataObject
->SetData( format
, size
, d
);
172 PasteboardRef pasteboard
;
174 if ( GetDragPasteboard( (DragReference
)m_currentDrag
, &pasteboard
) == noErr
)
176 transferred
= m_dataObject
->GetFromPasteboard( pasteboard
);
184 //-------------------------------------------------------------------------
186 //-------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
191 wxDropSource::wxDropSource(wxWindow
*win
,
192 const wxCursor
&cursorCopy
,
193 const wxCursor
&cursorMove
,
194 const wxCursor
&cursorStop
)
195 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
197 wxMacEnsureTrackingHandlersInstalled();
202 wxDropSource::wxDropSource(wxDataObject
& data
,
204 const wxCursor
&cursorCopy
,
205 const wxCursor
&cursorMove
,
206 const wxCursor
&cursorStop
)
207 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
209 wxMacEnsureTrackingHandlersInstalled();
215 wxDropSource::~wxDropSource()
219 OSStatus
wxMacPromiseKeeper(PasteboardRef
WXUNUSED(inPasteboard
),
220 PasteboardItemID
WXUNUSED(inItem
),
221 CFStringRef
WXUNUSED(inFlavorType
),
222 void * WXUNUSED(inContext
))
224 OSStatus err
= noErr
;
226 // we might add promises here later, inContext is the wxDropSource*
231 wxDragResult
wxDropSource::DoDragDrop(int flags
)
233 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
235 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
236 return (wxDragResult
)wxDragNone
;
239 DragReference theDrag
;
240 RgnHandle dragRegion
;
241 OSStatus err
= noErr
;
242 PasteboardRef pasteboard
;
246 err
= PasteboardCreate( kPasteboardUniqueName
, &pasteboard
);
250 // we add a dummy promise keeper because of strange messages when linking against carbon debug
251 err
= PasteboardSetPromiseKeeper( pasteboard
, wxMacPromiseKeeper
, this );
254 CFRelease( pasteboard
);
258 err
= PasteboardClear( pasteboard
);
261 CFRelease( pasteboard
);
264 PasteboardSynchronize( pasteboard
);
266 m_data
->AddToPasteboard( pasteboard
, 1 );
268 if (NewDragWithPasteboard( pasteboard
, &theDrag
) != noErr
)
270 CFRelease( pasteboard
);
274 dragRegion
= NewRgn();
275 RgnHandle tempRgn
= NewRgn();
278 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent(), &rec
);
280 const short dragRegionOuterBoundary
= 10;
281 const short dragRegionInnerBoundary
= 9;
285 rec
.where
.h
- dragRegionOuterBoundary
,
286 rec
.where
.v
- dragRegionOuterBoundary
,
287 rec
.where
.h
+ dragRegionOuterBoundary
,
288 rec
.where
.v
+ dragRegionOuterBoundary
);
292 rec
.where
.h
- dragRegionInnerBoundary
,
293 rec
.where
.v
- dragRegionInnerBoundary
,
294 rec
.where
.h
+ dragRegionInnerBoundary
,
295 rec
.where
.v
+ dragRegionInnerBoundary
);
297 DiffRgn( dragRegion
, tempRgn
, dragRegion
);
298 DisposeRgn( tempRgn
);
300 // TODO: work with promises in order to return data
301 // only when drag was successfully completed
303 gTrackingGlobals
.m_currentSource
= this;
304 gTrackingGlobals
.m_result
= wxDragNone
;
305 gTrackingGlobals
.m_flags
= flags
;
307 err
= TrackDrag( theDrag
, &rec
, dragRegion
);
309 DisposeRgn( dragRegion
);
310 DisposeDrag( theDrag
);
311 CFRelease( pasteboard
);
312 gTrackingGlobals
.m_currentSource
= NULL
;
315 return gTrackingGlobals
.m_result
;
318 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect
)
320 const wxCursor
& cursor
= GetCursor(effect
);
321 bool result
= cursor
.Ok();
329 bool gTrackingGlobalsInstalled
= false;
331 // passing the globals via refcon is not needed by the CFM and later architectures anymore
332 // but I'll leave it in there, just in case...
335 pascal OSErr
wxMacWindowDragTrackingHandler(
336 DragTrackingMessage theMessage
, WindowPtr theWindow
,
337 void *handlerRefCon
, DragReference theDrag
);
338 pascal OSErr
wxMacWindowDragReceiveHandler(
339 WindowPtr theWindow
, void *handlerRefCon
,
340 DragReference theDrag
);
343 void wxMacEnsureTrackingHandlersInstalled()
346 if ( !gTrackingGlobalsInstalled
)
350 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
353 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
356 gTrackingGlobalsInstalled
= true;
362 pascal OSErr
wxMacWindowDragTrackingHandler(
363 DragTrackingMessage theMessage
, WindowPtr theWindow
,
364 void *handlerRefCon
, DragReference theDrag
)
366 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
368 Point mouse
, localMouse
;
369 DragAttributes attributes
;
371 GetDragAttributes( theDrag
, &attributes
);
373 wxNonOwnedWindow
* toplevel
= wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) theWindow
);
375 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
376 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
380 case kDragTrackingEnterHandler
:
381 case kDragTrackingLeaveHandler
:
384 case kDragTrackingEnterWindow
:
385 if (trackingGlobals
!= NULL
)
387 trackingGlobals
->m_currentTargetWindow
= NULL
;
388 trackingGlobals
->m_currentTarget
= NULL
;
392 case kDragTrackingInWindow
:
393 if (trackingGlobals
== NULL
)
395 if (toplevel
== NULL
)
398 GetDragMouse( theDrag
, &mouse
, 0L );
401 toplevel
->GetNonOwnedPeer()->ScreenToWindow( &x
, &y
);
406 wxWindow
*win
= NULL
;
407 ControlPartCode controlPart
;
408 ControlRef control
= FindControlUnderMouse( localMouse
, theWindow
, &controlPart
);
410 win
= wxFindWindowFromWXWidget( (WXWidget
) control
);
415 localx
= localMouse
.h
;
416 localy
= localMouse
.v
;
419 win
->MacRootWindowToWindow( &localx
, &localy
);
420 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
422 if ( trackingGlobals
->m_currentTargetWindow
)
424 // this window is left
425 if ( trackingGlobals
->m_currentTarget
)
428 HideDragHilite( theDrag
);
430 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
431 trackingGlobals
->m_currentTarget
->OnLeave();
432 trackingGlobals
->m_currentTarget
= NULL
;
433 trackingGlobals
->m_currentTargetWindow
= NULL
;
439 // this window is entered
440 trackingGlobals
->m_currentTargetWindow
= win
;
441 trackingGlobals
->m_currentTarget
= win
->GetDropTarget();
443 if ( trackingGlobals
->m_currentTarget
)
445 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
446 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
);
449 if ( result
!= wxDragNone
)
454 win
->MacWindowToRootWindow( &x
, &y
);
455 RgnHandle hiliteRgn
= NewRgn();
456 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
};
457 RectRgn( hiliteRgn
, &r
);
459 ShowDragHilite( theDrag
, hiliteRgn
, true );
461 DisposeRgn( hiliteRgn
);
468 if ( trackingGlobals
->m_currentTarget
)
470 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
471 result
= trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
);
475 // set cursor for OnEnter and OnDragOver
476 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
478 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
480 wxStockCursor cursorID
= wxCURSOR_NONE
;
485 cursorID
= wxCURSOR_COPY_ARROW
;
489 cursorID
= wxCURSOR_ARROW
;
493 cursorID
= wxCURSOR_NO_ENTRY
;
500 // put these here to make gcc happy
504 if (cursorID
!= wxCURSOR_NONE
)
506 wxCursor
cursor( cursorID
);
514 case kDragTrackingLeaveWindow
:
515 if (trackingGlobals
== NULL
)
518 if (trackingGlobals
->m_currentTarget
)
520 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
521 trackingGlobals
->m_currentTarget
->OnLeave();
523 HideDragHilite( theDrag
);
525 trackingGlobals
->m_currentTarget
= NULL
;
527 trackingGlobals
->m_currentTargetWindow
= NULL
;
537 pascal OSErr
wxMacWindowDragReceiveHandler(
540 DragReference theDrag
)
542 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
543 if ( trackingGlobals
->m_currentTarget
)
545 Point mouse
, localMouse
;
548 trackingGlobals
->m_currentTarget
->SetCurrentDrag( theDrag
);
549 GetDragMouse( theDrag
, &mouse
, 0L );
551 localx
= localMouse
.h
;
552 localy
= localMouse
.v
;
553 wxNonOwnedWindow
* tlw
= wxNonOwnedWindow::GetFromWXWindow((WXWindow
) theWindow
);
555 tlw
->GetNonOwnedPeer()->ScreenToWindow( &localx
, &localy
);
557 // TODO : should we use client coordinates?
558 if ( trackingGlobals
->m_currentTargetWindow
)
559 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
560 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
562 // the option key indicates copy in Mac UI, if it's not pressed do
563 // move by default if it's allowed at all
565 result
= !(trackingGlobals
->m_flags
& wxDrag_AllowMove
) ||
566 (GetCurrentKeyModifiers() & optionKey
)
569 trackingGlobals
->m_result
=
570 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);
578 #endif // wxUSE_DRAG_AND_DROP