]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/dnd.mm
Moved wxPGProperty::SetFlag() to protected API since it does not have any side-effect...
[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/toplevel.h"
21 #include "wx/gdicmn.h"
22 #include "wx/wx.h"
23 #endif // WX_PRECOMP
24
25 #include "wx/evtloop.h"
26
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 wxUnusedVar( anImage );
91 wxUnusedVar( aPoint );
92
93 bool optionDown = GetCurrentKeyModifiers() & optionKey;
94 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
95
96 if (wxDropSource* source = impl)
97 {
98 if (!source->GiveFeedback(result))
99 {
100 wxStockCursor cursorID = wxCURSOR_NONE;
101
102 switch (result)
103 {
104 case wxDragCopy:
105 cursorID = wxCURSOR_COPY_ARROW;
106 break;
107
108 case wxDragMove:
109 cursorID = wxCURSOR_ARROW;
110 break;
111
112 case wxDragNone:
113 cursorID = wxCURSOR_NO_ENTRY;
114 break;
115
116 case wxDragError:
117 case wxDragLink:
118 case wxDragCancel:
119 default:
120 // put these here to make gcc happy
121 ;
122 }
123
124 if (cursorID != wxCURSOR_NONE)
125 {
126 // TODO under 10.6 the os itself deals with the cursor, remove if things
127 // work properly everywhere
128 #if 0
129 wxCursor cursor( cursorID );
130 cursor.MacInstall();
131 #endif
132 }
133 }
134 }
135 }
136
137 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
138 {
139 wxUnusedVar( anImage );
140 wxUnusedVar( aPoint );
141
142 resultCode = operation;
143 dragFinished = YES;
144 }
145
146 @end
147
148 wxDropTarget::wxDropTarget( wxDataObject *data )
149 : wxDropTargetBase( data )
150 {
151
152 }
153
154 //-------------------------------------------------------------------------
155 // wxDropSource
156 //-------------------------------------------------------------------------
157
158 wxDropSource::wxDropSource(wxWindow *win,
159 const wxCursor &cursorCopy,
160 const wxCursor &cursorMove,
161 const wxCursor &cursorStop)
162 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
163 {
164 m_window = win;
165 }
166
167 wxDropSource::wxDropSource(wxDataObject& data,
168 wxWindow *win,
169 const wxCursor &cursorCopy,
170 const wxCursor &cursorMove,
171 const wxCursor &cursorStop)
172 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
173 {
174 SetData( data );
175 m_window = win;
176 }
177
178 wxDropSource* wxDropSource::GetCurrentDropSource()
179 {
180 return gCurrentSource;
181 }
182
183 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
184 {
185 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
186
187 wxDragResult result = wxDragNone;
188 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
189 return result;
190
191 NSView* view = m_window->GetPeer()->GetWXWidget();
192 if (view)
193 {
194 NSPasteboard *pboard;
195
196 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
197
198 OSStatus err = noErr;
199 PasteboardRef pboardRef;
200 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
201
202 err = PasteboardClear( pboardRef );
203 if ( err != noErr )
204 {
205 CFRelease( pboardRef );
206 return wxDragNone;
207 }
208 PasteboardSynchronize( pboardRef );
209
210 m_data->AddToPasteboard( pboardRef, 1 );
211
212 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
213 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
214
215 NSPoint down = [theEvent locationInWindow];
216 NSPoint p = [view convertPoint:down toView:nil];
217
218 gCurrentSource = this;
219
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];
225
226 [image lockFocus];
227
228 [[[NSColor whiteColor] colorWithAlphaComponent:0.8] set];
229 NSRectFill(fillRect);
230 [[NSColor blackColor] set];
231 NSFrameRectWithWidthUsingOperation(fillRect,1.0f,NSCompositeDestinationOver);
232
233 [image unlockFocus];
234
235
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];
240
241 wxEventLoopBase * const loop = wxEventLoop::GetActive();
242 while ( ![delegate finished] )
243 loop->Dispatch();
244
245 result = NSDragOperationToWxDragResult([delegate code]);
246 [delegate release];
247 [image release];
248 gCurrentSource = NULL;
249 }
250
251
252 return result;
253 }
254
255 #endif // wxUSE_DRAG_AND_DROP
256