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 wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
33 case NSDragOperationCopy:
35 case NSDragOperationMove:
37 case NSDragOperationLink:
39 case NSDragOperationNone:
42 wxFAIL_MSG("Unexpected result code");
46 @interface DropSourceDelegate : NSObject
53 - (void)setImplementation: (wxDropSource *)dropSource;
55 - (NSDragOperation)code;
56 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
59 @implementation DropSourceDelegate
65 resultCode = NSDragOperationNone;
70 - (void)setImplementation: (wxDropSource *)dropSource
80 - (NSDragOperation)code
85 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
87 resultCode = operation;
93 wxDropTarget::wxDropTarget( wxDataObject *data )
94 : wxDropTargetBase( data )
99 bool wxDropTarget::CurrentDragHasSupportedFormat()
101 if (m_dataObject == NULL)
104 return m_dataObject->HasDataInPasteboard( m_currentDragPasteboard );
107 bool wxDropTarget::GetData()
109 if (m_dataObject == NULL)
112 if ( !CurrentDragHasSupportedFormat() )
115 return m_dataObject->GetFromPasteboard( m_currentDragPasteboard );
118 //-------------------------------------------------------------------------
120 //-------------------------------------------------------------------------
122 wxDropSource::wxDropSource(wxWindow *win,
123 const wxCursor &cursorCopy,
124 const wxCursor &cursorMove,
125 const wxCursor &cursorStop)
126 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
131 wxDropSource::wxDropSource(wxDataObject& data,
133 const wxCursor &cursorCopy,
134 const wxCursor &cursorMove,
135 const wxCursor &cursorStop)
136 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
142 wxDragResult wxDropSource::DoDragDrop(int flags)
144 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
146 wxDragResult result = wxDragNone;
147 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
150 NSView* view = m_window->GetPeer()->GetWXWidget();
153 NSPasteboard *pboard;
155 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
157 OSStatus err = noErr;
158 PasteboardRef pboardRef;
159 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
161 err = PasteboardClear( pboardRef );
164 CFRelease( pboardRef );
167 PasteboardSynchronize( pboardRef );
169 m_data->AddToPasteboard( pboardRef, 1 );
171 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
172 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
174 NSImage* image = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
175 DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init];
176 [delegate setImplementation: this];
177 [view dragImage:image at:NSMakePoint(0.0, 16.0) offset:NSMakeSize(0.0,0.0)
178 event: theEvent pasteboard: pboard source:delegate slideBack: NO];
180 wxEventLoopBase * const loop = wxEventLoop::GetActive();
181 while ( ![delegate finished] )
184 result = NSDragOperationToWxDragResult([delegate code]);
193 #endif // wxUSE_DRAG_AND_DROP