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 wxDataFormat
wxDropTarget::GetMatchingPair()
62 wxFAIL_MSG("wxDropTarget::GetMatchingPair() not implemented in src/osx/carbon/dnd.cpp");
66 bool wxDropTarget::OnDrop( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) )
68 if (m_dataObject
== NULL
)
71 return CurrentDragHasSupportedFormat();
74 wxDragResult
wxDropTarget::OnData(
75 wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
78 if (m_dataObject
== NULL
)
81 if (!CurrentDragHasSupportedFormat())
84 return GetData() ? def
: wxDragNone
;
87 bool wxDropTarget::CurrentDragHasSupportedFormat()
89 bool supported
= false;
90 if (m_dataObject
== NULL
)
93 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
95 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
99 size_t formatcount
= data
->GetFormatCount();
100 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
101 data
->GetAllFormats( array
);
102 for (size_t i
= 0; !supported
&& i
< formatcount
; i
++)
104 wxDataFormat format
= array
[i
];
105 if ( m_dataObject
->IsSupported( format
) )
118 supported
= m_dataObject
->HasDataInPasteboard( m_currentDragPasteboard
);
124 bool wxDropTarget::GetData()
126 if (m_dataObject
== NULL
)
129 if ( !CurrentDragHasSupportedFormat() )
132 bool transferred
= false;
133 if ( gTrackingGlobals
.m_currentSource
!= NULL
)
135 wxDataObject
* data
= gTrackingGlobals
.m_currentSource
->GetDataObject();
139 size_t formatcount
= data
->GetFormatCount();
140 wxDataFormat
*array
= new wxDataFormat
[formatcount
];
141 data
->GetAllFormats( array
);
142 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
144 wxDataFormat format
= array
[i
];
145 if ( m_dataObject
->IsSupported( format
) )
147 int size
= data
->GetDataSize( format
);
152 m_dataObject
->SetData( format
, 0, 0 );
156 char *d
= new char[size
];
157 data
->GetDataHere( format
, (void*)d
);
158 m_dataObject
->SetData( format
, size
, d
);
170 transferred
= m_dataObject
->GetFromPasteboard( m_currentDragPasteboard
);
176 //-------------------------------------------------------------------------
178 //-------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
183 wxDropSource::wxDropSource(wxWindow
*win
,
184 const wxCursor
&cursorCopy
,
185 const wxCursor
&cursorMove
,
186 const wxCursor
&cursorStop
)
187 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
189 wxMacEnsureTrackingHandlersInstalled();
194 wxDropSource::wxDropSource(wxDataObject
& data
,
196 const wxCursor
&cursorCopy
,
197 const wxCursor
&cursorMove
,
198 const wxCursor
&cursorStop
)
199 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
201 wxMacEnsureTrackingHandlersInstalled();
207 wxDropSource::~wxDropSource()
211 OSStatus
wxMacPromiseKeeper(PasteboardRef
WXUNUSED(inPasteboard
),
212 PasteboardItemID
WXUNUSED(inItem
),
213 CFStringRef
WXUNUSED(inFlavorType
),
214 void * WXUNUSED(inContext
))
216 OSStatus err
= noErr
;
218 // we might add promises here later, inContext is the wxDropSource*
223 wxDragResult
wxDropSource::DoDragDrop(int flags
)
225 wxASSERT_MSG( m_data
, wxT("Drop source: no data") );
227 if ((m_data
== NULL
) || (m_data
->GetFormatCount() == 0))
228 return (wxDragResult
)wxDragNone
;
231 DragReference theDrag
;
232 RgnHandle dragRegion
;
233 OSStatus err
= noErr
;
234 PasteboardRef pasteboard
;
238 err
= PasteboardCreate( kPasteboardUniqueName
, &pasteboard
);
242 // we add a dummy promise keeper because of strange messages when linking against carbon debug
243 err
= PasteboardSetPromiseKeeper( pasteboard
, wxMacPromiseKeeper
, this );
246 CFRelease( pasteboard
);
250 err
= PasteboardClear( pasteboard
);
253 CFRelease( pasteboard
);
256 PasteboardSynchronize( pasteboard
);
258 m_data
->AddToPasteboard( pasteboard
, 1 );
260 if (NewDragWithPasteboard( pasteboard
, &theDrag
) != noErr
)
262 CFRelease( pasteboard
);
266 dragRegion
= NewRgn();
267 RgnHandle tempRgn
= NewRgn();
270 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent(), &rec
);
272 const short dragRegionOuterBoundary
= 10;
273 const short dragRegionInnerBoundary
= 9;
277 rec
.where
.h
- dragRegionOuterBoundary
,
278 rec
.where
.v
- dragRegionOuterBoundary
,
279 rec
.where
.h
+ dragRegionOuterBoundary
,
280 rec
.where
.v
+ dragRegionOuterBoundary
);
284 rec
.where
.h
- dragRegionInnerBoundary
,
285 rec
.where
.v
- dragRegionInnerBoundary
,
286 rec
.where
.h
+ dragRegionInnerBoundary
,
287 rec
.where
.v
+ dragRegionInnerBoundary
);
289 DiffRgn( dragRegion
, tempRgn
, dragRegion
);
290 DisposeRgn( tempRgn
);
292 // TODO: work with promises in order to return data
293 // only when drag was successfully completed
295 gTrackingGlobals
.m_currentSource
= this;
296 gTrackingGlobals
.m_result
= wxDragNone
;
297 gTrackingGlobals
.m_flags
= flags
;
299 err
= TrackDrag( theDrag
, &rec
, dragRegion
);
301 DisposeRgn( dragRegion
);
302 DisposeDrag( theDrag
);
303 CFRelease( pasteboard
);
304 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...
327 pascal OSErr
wxMacWindowDragTrackingHandler(
328 DragTrackingMessage theMessage
, WindowPtr theWindow
,
329 void *handlerRefCon
, DragReference theDrag
);
330 pascal OSErr
wxMacWindowDragReceiveHandler(
331 WindowPtr theWindow
, void *handlerRefCon
,
332 DragReference theDrag
);
335 void wxMacEnsureTrackingHandlersInstalled()
338 if ( !gTrackingGlobalsInstalled
)
342 err
= InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler
), 0L, &gTrackingGlobals
);
345 err
= InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler
), 0L, &gTrackingGlobals
);
348 gTrackingGlobalsInstalled
= true;
354 pascal OSErr
wxMacWindowDragTrackingHandler(
355 DragTrackingMessage theMessage
, WindowPtr theWindow
,
356 void *handlerRefCon
, DragReference theDrag
)
358 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*) handlerRefCon
;
360 Point mouse
, localMouse
;
361 DragAttributes attributes
;
363 GetDragAttributes( theDrag
, &attributes
);
364 PasteboardRef pasteboard
= 0;
365 GetDragPasteboard( theDrag
, &pasteboard
);
366 wxNonOwnedWindow
* toplevel
= wxNonOwnedWindow::GetFromWXWindow( (WXWindow
) theWindow
);
368 bool optionDown
= GetCurrentKeyModifiers() & optionKey
;
369 wxDragResult result
= optionDown
? wxDragCopy
: wxDragMove
;
373 case kDragTrackingEnterHandler
:
374 case kDragTrackingLeaveHandler
:
377 case kDragTrackingEnterWindow
:
378 if (trackingGlobals
!= NULL
)
380 trackingGlobals
->m_currentTargetWindow
= NULL
;
381 trackingGlobals
->m_currentTarget
= NULL
;
385 case kDragTrackingInWindow
:
386 if (trackingGlobals
== NULL
)
388 if (toplevel
== NULL
)
391 GetDragMouse( theDrag
, &mouse
, 0L );
394 toplevel
->GetNonOwnedPeer()->ScreenToWindow( &x
, &y
);
399 wxWindow
*win
= NULL
;
400 ControlPartCode controlPart
;
401 ControlRef control
= FindControlUnderMouse( localMouse
, theWindow
, &controlPart
);
403 win
= wxFindWindowFromWXWidget( (WXWidget
) control
);
408 localx
= localMouse
.h
;
409 localy
= localMouse
.v
;
412 win
->MacRootWindowToWindow( &localx
, &localy
);
413 if ( win
!= trackingGlobals
->m_currentTargetWindow
)
415 if ( trackingGlobals
->m_currentTargetWindow
)
417 // this window is left
418 if ( trackingGlobals
->m_currentTarget
)
421 HideDragHilite( theDrag
);
423 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
424 trackingGlobals
->m_currentTarget
->OnLeave();
425 trackingGlobals
->m_currentTarget
= NULL
;
426 trackingGlobals
->m_currentTargetWindow
= NULL
;
432 // this window is entered
433 trackingGlobals
->m_currentTargetWindow
= win
;
434 trackingGlobals
->m_currentTarget
= win
->GetDropTarget();
436 if ( trackingGlobals
->m_currentTarget
)
438 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
439 result
= trackingGlobals
->m_currentTarget
->OnEnter( localx
, localy
, result
);
442 if ( result
!= wxDragNone
)
447 win
->MacWindowToRootWindow( &x
, &y
);
448 RgnHandle hiliteRgn
= NewRgn();
449 Rect r
= { y
, x
, y
+ win
->GetSize().y
, x
+ win
->GetSize().x
};
450 RectRgn( hiliteRgn
, &r
);
452 ShowDragHilite( theDrag
, hiliteRgn
, true );
454 DisposeRgn( hiliteRgn
);
461 if ( trackingGlobals
->m_currentTarget
)
463 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
464 result
= trackingGlobals
->m_currentTarget
->OnDragOver( localx
, localy
, result
);
468 // set cursor for OnEnter and OnDragOver
469 if ( trackingGlobals
->m_currentSource
&& !trackingGlobals
->m_currentSource
->GiveFeedback( result
) )
471 if ( !trackingGlobals
->m_currentSource
->MacInstallDefaultCursor( result
) )
473 wxStockCursor cursorID
= wxCURSOR_NONE
;
478 cursorID
= wxCURSOR_COPY_ARROW
;
482 cursorID
= wxCURSOR_ARROW
;
486 cursorID
= wxCURSOR_NO_ENTRY
;
493 // put these here to make gcc happy
497 if (cursorID
!= wxCURSOR_NONE
)
499 wxCursor
cursor( cursorID
);
507 case kDragTrackingLeaveWindow
:
508 if (trackingGlobals
== NULL
)
511 if (trackingGlobals
->m_currentTarget
)
513 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
514 trackingGlobals
->m_currentTarget
->OnLeave();
516 HideDragHilite( theDrag
);
518 trackingGlobals
->m_currentTarget
= NULL
;
520 trackingGlobals
->m_currentTargetWindow
= NULL
;
530 pascal OSErr
wxMacWindowDragReceiveHandler(
533 DragReference theDrag
)
535 MacTrackingGlobals
* trackingGlobals
= (MacTrackingGlobals
*)handlerRefCon
;
536 if ( trackingGlobals
->m_currentTarget
)
538 Point mouse
, localMouse
;
541 PasteboardRef pasteboard
= 0;
542 GetDragPasteboard( theDrag
, &pasteboard
);
543 trackingGlobals
->m_currentTarget
->SetCurrentDragPasteboard( pasteboard
);
544 GetDragMouse( theDrag
, &mouse
, 0L );
546 localx
= localMouse
.h
;
547 localy
= localMouse
.v
;
548 wxNonOwnedWindow
* tlw
= wxNonOwnedWindow::GetFromWXWindow((WXWindow
) theWindow
);
550 tlw
->GetNonOwnedPeer()->ScreenToWindow( &localx
, &localy
);
552 // TODO : should we use client coordinates?
553 if ( trackingGlobals
->m_currentTargetWindow
)
554 trackingGlobals
->m_currentTargetWindow
->MacRootWindowToWindow( &localx
, &localy
);
555 if ( trackingGlobals
->m_currentTarget
->OnDrop( localx
, localy
) )
557 // the option key indicates copy in Mac UI, if it's not pressed do
558 // move by default if it's allowed at all
560 result
= !(trackingGlobals
->m_flags
& wxDrag_AllowMove
) ||
561 (GetCurrentKeyModifiers() & optionKey
)
564 trackingGlobals
->m_result
=
565 trackingGlobals
->m_currentTarget
->OnData( localx
, localy
, result
);
573 #endif // wxUSE_DRAG_AND_DROP