]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/dnd.mm
Since wxDialog::Show(false) only calls wxWindow::Show, we must explicitly hide the...
[wxWidgets.git] / src / osx / cocoa / dnd.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/dnd.mm
3 // Purpose: wxDropTarget, wxDropSource implementations
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
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 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_DRAG_AND_DROP
15
16 #include "wx/dnd.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/evtloop.h"
21 #include "wx/toplevel.h"
22 #include "wx/gdicmn.h"
23 #include "wx/wx.h"
24 #endif // WX_PRECOMP
25
26 #include <AppKit/AppKit.h>
27 #include "wx/osx/private.h"
28
29 wxDropSource* gCurrentSource = NULL;
30
31 wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
32 {
33 switch (code)
34 {
35 case NSDragOperationCopy:
36 return wxDragCopy;
37 case NSDragOperationMove:
38 return wxDragMove;
39 case NSDragOperationLink:
40 return wxDragLink;
41 case NSDragOperationNone:
42 return wxDragNone;
43 default:
44 wxFAIL_MSG("Unexpected result code");
45 }
46 }
47
48 @interface DropSourceDelegate : NSObject
49 {
50 BOOL dragFinished;
51 int resultCode;
52 wxDropSource* impl;
53 }
54
55 - (void)setImplementation: (wxDropSource *)dropSource;
56 - (BOOL)finished;
57 - (NSDragOperation)code;
58 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint;
59 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
60 @end
61
62 @implementation DropSourceDelegate
63
64 - (id)init
65 {
66 [super init];
67 dragFinished = NO;
68 resultCode = NSDragOperationNone;
69 impl = 0;
70 return self;
71 }
72
73 - (void)setImplementation: (wxDropSource *)dropSource
74 {
75 impl = dropSource;
76 }
77
78 - (BOOL)finished
79 {
80 return dragFinished;
81 }
82
83 - (NSDragOperation)code
84 {
85 return resultCode;
86 }
87
88 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint
89 {
90 bool optionDown = GetCurrentKeyModifiers() & optionKey;
91 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
92
93 if (wxDropSource* source = impl)
94 {
95 if (!source->GiveFeedback(result))
96 {
97 wxStockCursor cursorID = wxCURSOR_NONE;
98
99 switch (result)
100 {
101 case wxDragCopy:
102 cursorID = wxCURSOR_COPY_ARROW;
103 break;
104
105 case wxDragMove:
106 cursorID = wxCURSOR_ARROW;
107 break;
108
109 case wxDragNone:
110 cursorID = wxCURSOR_NO_ENTRY;
111 break;
112
113 case wxDragError:
114 case wxDragLink:
115 case wxDragCancel:
116 default:
117 // put these here to make gcc happy
118 ;
119 }
120
121 if (cursorID != wxCURSOR_NONE)
122 {
123 wxCursor cursor( cursorID );
124 cursor.MacInstall();
125 }
126 }
127 }
128 }
129
130 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
131 {
132 resultCode = operation;
133 dragFinished = YES;
134 }
135
136 @end
137
138 wxDropTarget::wxDropTarget( wxDataObject *data )
139 : wxDropTargetBase( data )
140 {
141
142 }
143
144 //-------------------------------------------------------------------------
145 // wxDropSource
146 //-------------------------------------------------------------------------
147
148 wxDropSource::wxDropSource(wxWindow *win,
149 const wxCursor &cursorCopy,
150 const wxCursor &cursorMove,
151 const wxCursor &cursorStop)
152 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
153 {
154 m_window = win;
155 }
156
157 wxDropSource::wxDropSource(wxDataObject& data,
158 wxWindow *win,
159 const wxCursor &cursorCopy,
160 const wxCursor &cursorMove,
161 const wxCursor &cursorStop)
162 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
163 {
164 SetData( data );
165 m_window = win;
166 }
167
168 wxDropSource* wxDropSource::GetCurrentDropSource()
169 {
170 return gCurrentSource;
171 }
172
173 wxDragResult wxDropSource::DoDragDrop(int flags)
174 {
175 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
176
177 wxDragResult result = wxDragNone;
178 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
179 return result;
180
181 NSView* view = m_window->GetPeer()->GetWXWidget();
182 if (view)
183 {
184 NSPasteboard *pboard;
185
186 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
187
188 OSStatus err = noErr;
189 PasteboardRef pboardRef;
190 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
191
192 err = PasteboardClear( pboardRef );
193 if ( err != noErr )
194 {
195 CFRelease( pboardRef );
196 return wxDragNone;
197 }
198 PasteboardSynchronize( pboardRef );
199
200 m_data->AddToPasteboard( pboardRef, 1 );
201
202 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
203 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
204
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];
211
212 wxEventLoopBase * const loop = wxEventLoop::GetActive();
213 while ( ![delegate finished] )
214 loop->Dispatch();
215
216 result = NSDragOperationToWxDragResult([delegate code]);
217 [delegate release];
218 [image release];
219 gCurrentSource = NULL;
220 }
221
222
223 return result;
224 }
225
226 #endif // wxUSE_DRAG_AND_DROP
227