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/toplevel.h"
21 #include "wx/gdicmn.h"
25 #include "wx/evtloop.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 wxUnusedVar( anImage );
91 wxUnusedVar( aPoint );
93 bool optionDown = GetCurrentKeyModifiers() & optionKey;
94 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
96 if (wxDropSource* source = impl)
98 if (!source->GiveFeedback(result))
100 wxStockCursor cursorID = wxCURSOR_NONE;
105 cursorID = wxCURSOR_COPY_ARROW;
109 cursorID = wxCURSOR_ARROW;
113 cursorID = wxCURSOR_NO_ENTRY;
120 // put these here to make gcc happy
124 if (cursorID != wxCURSOR_NONE)
126 // TODO under 10.6 the os itself deals with the cursor, remove if things
127 // work properly everywhere
129 wxCursor cursor( cursorID );
137 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
139 wxUnusedVar( anImage );
140 wxUnusedVar( aPoint );
142 resultCode = operation;
148 wxDropTarget::wxDropTarget( wxDataObject *data )
149 : wxDropTargetBase( data )
154 //-------------------------------------------------------------------------
156 //-------------------------------------------------------------------------
158 wxDropSource::wxDropSource(wxWindow *win,
159 const wxCursor &cursorCopy,
160 const wxCursor &cursorMove,
161 const wxCursor &cursorStop)
162 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
167 wxDropSource::wxDropSource(wxDataObject& data,
169 const wxCursor &cursorCopy,
170 const wxCursor &cursorMove,
171 const wxCursor &cursorStop)
172 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
178 wxDropSource* wxDropSource::GetCurrentDropSource()
180 return gCurrentSource;
183 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
185 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
187 wxDragResult result = wxDragNone;
188 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
191 NSView* view = m_window->GetPeer()->GetWXWidget();
194 NSPasteboard *pboard;
196 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
198 OSStatus err = noErr;
199 PasteboardRef pboardRef;
200 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
202 err = PasteboardClear( pboardRef );
205 CFRelease( pboardRef );
208 PasteboardSynchronize( pboardRef );
210 m_data->AddToPasteboard( pboardRef, 1 );
212 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
213 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
215 NSPoint down = [theEvent locationInWindow];
216 NSPoint p = [view convertPoint:down toView:nil];
218 gCurrentSource = this;
220 // add a dummy square as dragged image for the moment,
221 // TODO: proper drag image for data
222 NSSize sz = NSMakeSize(16,16);
223 NSRect fillRect = NSMakeRect(0, 0, 16, 16);
224 NSImage* image = [[NSImage alloc] initWithSize: sz];
228 [[[NSColor whiteColor] colorWithAlphaComponent:0.8] set];
229 NSRectFill(fillRect);
230 [[NSColor blackColor] set];
231 NSFrameRectWithWidthUsingOperation(fillRect,1.0f,NSCompositeDestinationOver);
236 DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init];
237 [delegate setImplementation: this];
238 [view dragImage:image at:p offset:NSMakeSize(0.0,0.0)
239 event: theEvent pasteboard: pboard source:delegate slideBack: NO];
241 wxEventLoopBase * const loop = wxEventLoop::GetActive();
242 while ( ![delegate finished] )
245 result = NSDragOperationToWxDragResult([delegate code]);
248 gCurrentSource = NULL;
255 #endif // wxUSE_DRAG_AND_DROP