1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/dnd.mm
3 // Purpose: wxDropTarget, wxDropSource implementations
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: dnd.cpp 61724 2009-08-21 10:41:26Z VZ $
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/evtloop.h"
21 #include "wx/toplevel.h"
22 #include "wx/gdicmn.h"
26 #include <AppKit/AppKit.h>
27 #include "wx/osx/private.h"
29 wxDropSource* gCurrentSource = NULL;
31 wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
35 case NSDragOperationCopy:
37 case NSDragOperationMove:
39 case NSDragOperationLink:
41 case NSDragOperationNone:
44 wxFAIL_MSG("Unexpected result code");
48 @interface DropSourceDelegate : NSObject
55 - (void)setImplementation: (wxDropSource *)dropSource;
57 - (NSDragOperation)code;
58 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint;
59 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
62 @implementation DropSourceDelegate
68 resultCode = NSDragOperationNone;
73 - (void)setImplementation: (wxDropSource *)dropSource
83 - (NSDragOperation)code
88 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint
90 bool optionDown = GetCurrentKeyModifiers() & optionKey;
91 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
93 if (wxDropSource* source = impl)
95 if (!source->GiveFeedback(result))
97 wxStockCursor cursorID = wxCURSOR_NONE;
102 cursorID = wxCURSOR_COPY_ARROW;
106 cursorID = wxCURSOR_ARROW;
110 cursorID = wxCURSOR_NO_ENTRY;
117 // put these here to make gcc happy
121 if (cursorID != wxCURSOR_NONE)
123 wxCursor cursor( cursorID );
130 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
132 resultCode = operation;
138 wxDropTarget::wxDropTarget( wxDataObject *data )
139 : wxDropTargetBase( data )
144 //-------------------------------------------------------------------------
146 //-------------------------------------------------------------------------
148 wxDropSource::wxDropSource(wxWindow *win,
149 const wxCursor &cursorCopy,
150 const wxCursor &cursorMove,
151 const wxCursor &cursorStop)
152 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
157 wxDropSource::wxDropSource(wxDataObject& data,
159 const wxCursor &cursorCopy,
160 const wxCursor &cursorMove,
161 const wxCursor &cursorStop)
162 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
168 wxDropSource* wxDropSource::GetCurrentDropSource()
170 return gCurrentSource;
173 wxDragResult wxDropSource::DoDragDrop(int flags)
175 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
177 wxDragResult result = wxDragNone;
178 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
181 NSView* view = m_window->GetPeer()->GetWXWidget();
184 NSPasteboard *pboard;
186 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
188 OSStatus err = noErr;
189 PasteboardRef pboardRef;
190 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
192 err = PasteboardClear( pboardRef );
195 CFRelease( pboardRef );
198 PasteboardSynchronize( pboardRef );
200 m_data->AddToPasteboard( pboardRef, 1 );
202 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
203 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
205 gCurrentSource = this;
206 NSImage* image = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
207 DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init];
208 [delegate setImplementation: this];
209 [view dragImage:image at:NSMakePoint(0.0, 16.0) offset:NSMakeSize(0.0,0.0)
210 event: theEvent pasteboard: pboard source:delegate slideBack: NO];
212 wxEventLoopBase * const loop = wxEventLoop::GetActive();
213 while ( ![delegate finished] )
216 result = NSDragOperationToWxDragResult([delegate code]);
219 gCurrentSource = NULL;
226 #endif // wxUSE_DRAG_AND_DROP