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 OSStatus
wxMacPromiseKeeper(PasteboardRef
WXUNUSED(inPasteboard
),
44 PasteboardItemID
WXUNUSED(inItem
),
45 CFStringRef
WXUNUSED(inFlavorType
),
46 void * WXUNUSED(inContext
))
50 // we might add promises here later, inContext is the wxDropSource*
55 wxDropTarget::wxDropTarget( wxDataObject
*data
)
56 : wxDropTargetBase( data
)
58 wxMacEnsureTrackingHandlersInstalled();
61 bool wxDropTarget::CurrentDragHasSupportedFormat()
63 bool supported
= false;
64 if (m_dataObject
== NULL
)
67 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
69 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
73 size_t formatcount
= data
->GetFormatCount();
74 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
75 data
->GetAllFormats( array
);
76 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
78 wxDataFormat format
= array
[i
];
79 if ( m_dataObject
->IsSupported( format
) )
92 supported
= m_dataObject
->HasDataInPasteboard( m_currentDragPasteboard
);
98 bool wxDropTarget::GetData()
100 if (m_dataObject
== NULL
)
103 if ( !CurrentDragHasSupportedFormat() )
106 bool transferred
= false;
107 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
109 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
113 size_t formatcount
= data
->GetFormatCount();
114 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
115 data
->GetAllFormats( array
);
116 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
118 wxDataFormat format
= array
[i
];
119 if ( m_dataObject
->IsSupported( format
) )
121 int size
= data
->GetDataSize( format
);
126 m_dataObject
->SetData( format
, 0, 0 );
130 char *d
= new char[size
];
131 data
->GetDataHere( format
, (void*)d
);
132 m_dataObject
->SetData( format
, size
, d
);
144 transferred
= m_dataObject
->GetFromPasteboard( m_currentDragPasteboard
);
150 //-------------------------------------------------------------------------
152 //-------------------------------------------------------------------------
154 wxDropSource::wxDropSource(wxWindow
*win
,
155 const wxCursor
&cursorCopy
,
156 const wxCursor
&cursorMove
,
157 const wxCursor
&cursorStop
)
158 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
160 wxMacEnsureTrackingHandlersInstalled();
165 wxDropSource::wxDropSource(wxDataObject
& data
,
167 const wxCursor
&cursorCopy
,
168 const wxCursor
&cursorMove
,
169 const wxCursor
&cursorStop
)
170 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
172 wxMacEnsureTrackingHandlersInstalled();
178 wxDragResult
wxDropSource::DoDragDrop(int flags
)
180 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
182 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
183 return (wxDragResult
)wxDragNone
;
185 DragReference theDrag
;
186 RgnHandle dragRegion
;
187 OSStatus err
= noErr
;
188 PasteboardRef pasteboard
;
192 err
= PasteboardCreate( kPasteboardUniqueName
, &pasteboard
);
196 // we add a dummy promise keeper because of strange messages when linking against carbon debug
197 err
= PasteboardSetPromiseKeeper( pasteboard
, wxMacPromiseKeeper
, this );
200 CFRelease( pasteboard
);
204 err
= PasteboardClear( pasteboard
);
207 CFRelease( pasteboard
);
210 PasteboardSynchronize( pasteboard
);
212 m_data
->AddToPasteboard( pasteboard
, 1 );
214 if (NewDragWithPasteboard( pasteboard
, &theDrag
) != noErr
)
216 CFRelease( pasteboard
);
220 dragRegion
= NewRgn();
221 RgnHandle tempRgn
= NewRgn();
224 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent(), &rec
);
226 const short dragRegionOuterBoundary
= 10;
227 const short dragRegionInnerBoundary
= 9;
231 rec
.where
.h
- dragRegionOuterBoundary
,
232 rec
.where
.v
- dragRegionOuterBoundary
,
233 rec
.where
.h
+ dragRegionOuterBoundary
,
234 rec
.where
.v
+ dragRegionOuterBoundary
);
238 rec
.where
.h
- dragRegionInnerBoundary
,
239 rec
.where
.v
- dragRegionInnerBoundary
,
240 rec
.where
.h
+ dragRegionInnerBoundary
,
241 rec
.where
.v
+ dragRegionInnerBoundary
);
243 DiffRgn( dragRegion
, tempRgn
, dragRegion
);
244 DisposeRgn( tempRgn
);
246 // TODO: work with promises in order to return data
247 // only when drag was successfully completed
249 gTrackingGlobals
.m_currentSource
= this;
250 gTrackingGlobals
.m_result
= wxDragNone
;
251 gTrackingGlobals
.m_flags
= flags
;
253 err
= TrackDrag( theDrag
, &rec
, dragRegion
);
255 DisposeRgn( dragRegion
);
256 DisposeDrag( theDrag
);
257 CFRelease( pasteboard
);
258 gTrackingGlobals
.m_currentSource
= NULL
;
260 return gTrackingGlobals
.m_result
;
263 bool gTrackingGlobalsInstalled
= false;
265 // passing the globals via refcon is not needed by the CFM and later architectures anymore
266 // but I'll leave it in there, just in case...
268 pascal OSErr
wxMacWindowDragTrackingHandler(
269 DragTrackingMessage theMessage
, WindowPtr theWindow
,
270 void *handlerRefCon
, DragReference theDrag
);
271 pascal OSErr
wxMacWindowDragReceiveHandler(
272 WindowPtr theWindow
, void *handlerRefCon
,
273 DragReference theDrag
);
275 void wxMacEnsureTrackingHandlersInstalled()
277 if ( !gTrackingGlobalsInstalled
)
281 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
284 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
287 gTrackingGlobalsInstalled
= true;
291 pascal OSErr
wxMacWindowDragTrackingHandler(
292 DragTrackingMessage theMessage
, WindowPtr theWindow
,
293 void *handlerRefCon
, DragReference theDrag
)
295 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
297 Point mouse
, localMouse
;
298 DragAttributes attributes
;
300 GetDragAttributes( theDrag
, &attributes
);
301 PasteboardRef pasteboard
= 0;
302 GetDragPasteboard( theDrag
, &pasteboard
);
303 wxNonOwnedWindow
* toplevel
= wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) theWindow
);
305 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
306 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
310 case kDragTrackingEnterHandler
:
311 case kDragTrackingLeaveHandler
:
314 case kDragTrackingEnterWindow
:
315 if (trackingGlobals
!= NULL
)
317 trackingGlobals
->m_currentTargetWindow
= NULL
;
318 trackingGlobals
->m_currentTarget
= NULL
;
322 case kDragTrackingInWindow
:
323 if (trackingGlobals
== NULL
)
325 if (toplevel
== NULL
)
328 GetDragMouse( theDrag
, &mouse
, 0L );
332 toplevel
->GetNonOwnedPeer()->ScreenToWindow( &x
, &y
);
337 wxWindow
*win
= NULL
;
338 ControlPartCode controlPart
;
339 ControlRef control
= FindControlUnderMouse( localMouse
, theWindow
, &controlPart
);
341 win
= wxFindWindowFromWXWidget( (WXWidget
) control
);
346 localx
= localMouse
.h
;
347 localy
= localMouse
.v
;
350 win
->MacRootWindowToWindow( &localx
, &localy
);
351 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
353 if ( trackingGlobals
->m_currentTargetWindow
)
355 // this window is left
356 if ( trackingGlobals
->m_currentTarget
)
358 HideDragHilite( theDrag
);
359 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
360 trackingGlobals
->m_currentTarget
->OnLeave();
361 trackingGlobals
->m_currentTarget
= NULL
;
362 trackingGlobals
->m_currentTargetWindow
= NULL
;
368 // this window is entered
369 trackingGlobals
->m_currentTargetWindow
= win
;
370 trackingGlobals
->m_currentTarget
= win
->GetDropTarget();
372 if ( trackingGlobals
->m_currentTarget
)
374 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
375 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
);
378 if ( result
!= wxDragNone
)
383 win
->MacWindowToRootWindow( &x
, &y
);
384 RgnHandle hiliteRgn
= NewRgn();
385 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
};
386 RectRgn( hiliteRgn
, &r
);
387 ShowDragHilite( theDrag
, hiliteRgn
, true );
388 DisposeRgn( hiliteRgn
);
395 if ( trackingGlobals
->m_currentTarget
)
397 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
398 result
= trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
);
402 // set cursor for OnEnter and OnDragOver
403 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
405 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
407 wxStockCursor cursorID
= wxCURSOR_NONE
;
412 cursorID
= wxCURSOR_COPY_ARROW
;
416 cursorID
= wxCURSOR_ARROW
;
420 cursorID
= wxCURSOR_NO_ENTRY
;
427 // put these here to make gcc happy
431 if (cursorID
!= wxCURSOR_NONE
)
433 wxCursor
cursor( cursorID
);
442 case kDragTrackingLeaveWindow
:
443 if (trackingGlobals
== NULL
)
446 if (trackingGlobals
->m_currentTarget
)
448 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
449 trackingGlobals
->m_currentTarget
->OnLeave();
450 HideDragHilite( theDrag
);
451 trackingGlobals
->m_currentTarget
= NULL
;
453 trackingGlobals
->m_currentTargetWindow
= NULL
;
463 pascal OSErr
wxMacWindowDragReceiveHandler(
466 DragReference theDrag
)
468 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
469 if ( trackingGlobals
->m_currentTarget
)
471 Point mouse
, localMouse
;
474 PasteboardRef pasteboard
= 0;
475 GetDragPasteboard( theDrag
, &pasteboard
);
476 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
477 GetDragMouse( theDrag
, &mouse
, 0L );
479 localx
= localMouse
.h
;
480 localy
= localMouse
.v
;
481 wxNonOwnedWindow
* tlw
= wxNonOwnedWindow::GetFromWXWindow((WXWindow
) theWindow
);
483 tlw
->GetNonOwnedPeer()->ScreenToWindow( &localx
, &localy
);
485 // TODO : should we use client coordinates?
486 if ( trackingGlobals
->m_currentTargetWindow
)
487 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
488 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
490 // the option key indicates copy in Mac UI, if it's not pressed do
491 // move by default if it's allowed at all
493 result
= !(trackingGlobals
->m_flags
& wxDrag_AllowMove
) ||
494 (GetCurrentKeyModifiers() & optionKey
)
497 trackingGlobals
->m_result
=
498 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);
505 #endif // wxUSE_DRAG_AND_DROP