1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/dnd.mm
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"
15 #include "wx/object.h"
18 #if wxUSE_DRAG_AND_DROP
24 #include "wx/toplevel.h"
25 #include "wx/gdicmn.h"
29 #include "wx/evtloop.h"
31 #include "wx/osx/private.h"
33 wxDropSource* gCurrentSource = NULL;
35 wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
39 case NSDragOperationCopy:
41 case NSDragOperationMove:
43 case NSDragOperationLink:
45 case NSDragOperationNone:
48 wxFAIL_MSG("Unexpected result code");
53 @interface DropSourceDelegate : NSObject
60 - (void)setImplementation: (wxDropSource *)dropSource;
62 - (NSDragOperation)code;
63 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint;
64 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
67 @implementation DropSourceDelegate
73 resultCode = NSDragOperationNone;
78 - (void)setImplementation: (wxDropSource *)dropSource
88 - (NSDragOperation)code
93 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint
95 wxUnusedVar( anImage );
96 wxUnusedVar( aPoint );
98 bool optionDown = GetCurrentKeyModifiers() & optionKey;
99 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
101 if (wxDropSource* source = impl)
103 if (!source->GiveFeedback(result))
105 wxStockCursor cursorID = wxCURSOR_NONE;
110 cursorID = wxCURSOR_COPY_ARROW;
114 cursorID = wxCURSOR_ARROW;
118 cursorID = wxCURSOR_NO_ENTRY;
125 // put these here to make gcc happy
129 if (cursorID != wxCURSOR_NONE)
131 // TODO under 10.6 the os itself deals with the cursor, remove if things
132 // work properly everywhere
134 wxCursor cursor( cursorID );
142 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
144 wxUnusedVar( anImage );
145 wxUnusedVar( aPoint );
147 resultCode = operation;
153 wxDropTarget::wxDropTarget( wxDataObject *data )
154 : wxDropTargetBase( data )
159 //-------------------------------------------------------------------------
161 //-------------------------------------------------------------------------
163 wxDropSource::wxDropSource(wxWindow *win,
164 const wxCursor &cursorCopy,
165 const wxCursor &cursorMove,
166 const wxCursor &cursorStop)
167 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
172 wxDropSource::wxDropSource(wxDataObject& data,
174 const wxCursor &cursorCopy,
175 const wxCursor &cursorMove,
176 const wxCursor &cursorStop)
177 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
183 wxDropSource* wxDropSource::GetCurrentDropSource()
185 return gCurrentSource;
188 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
190 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
192 wxDragResult result = wxDragNone;
193 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
196 NSView* view = m_window->GetPeer()->GetWXWidget();
199 NSPasteboard *pboard;
201 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
203 OSStatus err = noErr;
204 PasteboardRef pboardRef;
205 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
207 err = PasteboardClear( pboardRef );
210 CFRelease( pboardRef );
213 PasteboardSynchronize( pboardRef );
215 m_data->AddToPasteboard( pboardRef, 1 );
217 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
218 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
220 NSPoint down = [theEvent locationInWindow];
221 NSPoint p = [view convertPoint:down toView:nil];
223 gCurrentSource = this;
225 // add a dummy square as dragged image for the moment,
226 // TODO: proper drag image for data
227 NSSize sz = NSMakeSize(16,16);
228 NSRect fillRect = NSMakeRect(0, 0, 16, 16);
229 NSImage* image = [[NSImage alloc] initWithSize: sz];
233 [[[NSColor whiteColor] colorWithAlphaComponent:0.8] set];
234 NSRectFill(fillRect);
235 [[NSColor blackColor] set];
236 NSFrameRectWithWidthUsingOperation(fillRect,1.0f,NSCompositeDestinationOver);
241 DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init];
242 [delegate setImplementation: this];
243 [view dragImage:image at:p offset:NSMakeSize(0.0,0.0)
244 event: theEvent pasteboard: pboard source:delegate slideBack: NO];
246 wxEventLoopBase * const loop = wxEventLoop::GetActive();
247 while ( ![delegate finished] )
250 result = NSDragOperationToWxDragResult([delegate code]);
253 gCurrentSource = NULL;
260 #endif // wxUSE_DRAG_AND_DROP