]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/dnd.mm
solving include order problems for stl and xti
[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$
8 // Copyright: (c) 1998 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/object.h"
16 #endif
17
18 #if wxUSE_DRAG_AND_DROP
19
20 #include "wx/dnd.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/app.h"
24 #include "wx/toplevel.h"
25 #include "wx/gdicmn.h"
26 #include "wx/wx.h"
27 #endif // WX_PRECOMP
28
29 #include "wx/evtloop.h"
30
31 #include "wx/osx/private.h"
32
33 wxDropSource* gCurrentSource = NULL;
34
35 wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
36 {
37 switch (code)
38 {
39 case NSDragOperationCopy:
40 return wxDragCopy;
41 case NSDragOperationMove:
42 return wxDragMove;
43 case NSDragOperationLink:
44 return wxDragLink;
45 case NSDragOperationNone:
46 return wxDragNone;
47 default:
48 wxFAIL_MSG("Unexpected result code");
49 }
50 return wxDragNone;
51 }
52
53 @interface DropSourceDelegate : NSObject
54 {
55 BOOL dragFinished;
56 int resultCode;
57 wxDropSource* impl;
58 }
59
60 - (void)setImplementation: (wxDropSource *)dropSource;
61 - (BOOL)finished;
62 - (NSDragOperation)code;
63 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint;
64 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
65 @end
66
67 @implementation DropSourceDelegate
68
69 - (id)init
70 {
71 self = [super init];
72 dragFinished = NO;
73 resultCode = NSDragOperationNone;
74 impl = 0;
75 return self;
76 }
77
78 - (void)setImplementation: (wxDropSource *)dropSource
79 {
80 impl = dropSource;
81 }
82
83 - (BOOL)finished
84 {
85 return dragFinished;
86 }
87
88 - (NSDragOperation)code
89 {
90 return resultCode;
91 }
92
93 - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint
94 {
95 wxUnusedVar( anImage );
96 wxUnusedVar( aPoint );
97
98 bool optionDown = GetCurrentKeyModifiers() & optionKey;
99 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
100
101 if (wxDropSource* source = impl)
102 {
103 if (!source->GiveFeedback(result))
104 {
105 wxStockCursor cursorID = wxCURSOR_NONE;
106
107 switch (result)
108 {
109 case wxDragCopy:
110 cursorID = wxCURSOR_COPY_ARROW;
111 break;
112
113 case wxDragMove:
114 cursorID = wxCURSOR_ARROW;
115 break;
116
117 case wxDragNone:
118 cursorID = wxCURSOR_NO_ENTRY;
119 break;
120
121 case wxDragError:
122 case wxDragLink:
123 case wxDragCancel:
124 default:
125 // put these here to make gcc happy
126 ;
127 }
128
129 if (cursorID != wxCURSOR_NONE)
130 {
131 // TODO under 10.6 the os itself deals with the cursor, remove if things
132 // work properly everywhere
133 #if 0
134 wxCursor cursor( cursorID );
135 cursor.MacInstall();
136 #endif
137 }
138 }
139 }
140 }
141
142 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
143 {
144 wxUnusedVar( anImage );
145 wxUnusedVar( aPoint );
146
147 resultCode = operation;
148 dragFinished = YES;
149 }
150
151 @end
152
153 wxDropTarget::wxDropTarget( wxDataObject *data )
154 : wxDropTargetBase( data )
155 {
156
157 }
158
159 //-------------------------------------------------------------------------
160 // wxDropSource
161 //-------------------------------------------------------------------------
162
163 wxDropSource::wxDropSource(wxWindow *win,
164 const wxCursor &cursorCopy,
165 const wxCursor &cursorMove,
166 const wxCursor &cursorStop)
167 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
168 {
169 m_window = win;
170 }
171
172 wxDropSource::wxDropSource(wxDataObject& data,
173 wxWindow *win,
174 const wxCursor &cursorCopy,
175 const wxCursor &cursorMove,
176 const wxCursor &cursorStop)
177 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
178 {
179 SetData( data );
180 m_window = win;
181 }
182
183 wxDropSource* wxDropSource::GetCurrentDropSource()
184 {
185 return gCurrentSource;
186 }
187
188 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
189 {
190 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
191
192 wxDragResult result = wxDragNone;
193 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
194 return result;
195
196 NSView* view = m_window->GetPeer()->GetWXWidget();
197 if (view)
198 {
199 NSPasteboard *pboard;
200
201 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
202
203 OSStatus err = noErr;
204 PasteboardRef pboardRef;
205 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
206
207 err = PasteboardClear( pboardRef );
208 if ( err != noErr )
209 {
210 CFRelease( pboardRef );
211 return wxDragNone;
212 }
213 PasteboardSynchronize( pboardRef );
214
215 m_data->AddToPasteboard( pboardRef, 1 );
216
217 NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
218 wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
219
220 NSPoint down = [theEvent locationInWindow];
221 NSPoint p = [view convertPoint:down toView:nil];
222
223 gCurrentSource = this;
224
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];
230
231 [image lockFocus];
232
233 [[[NSColor whiteColor] colorWithAlphaComponent:0.8] set];
234 NSRectFill(fillRect);
235 [[NSColor blackColor] set];
236 NSFrameRectWithWidthUsingOperation(fillRect,1.0f,NSCompositeDestinationOver);
237
238 [image unlockFocus];
239
240
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];
245
246 wxEventLoopBase * const loop = wxEventLoop::GetActive();
247 while ( ![delegate finished] )
248 loop->Dispatch();
249
250 result = NSDragOperationToWxDragResult([delegate code]);
251 [delegate release];
252 [image release];
253 gCurrentSource = NULL;
254 }
255
256
257 return result;
258 }
259
260 #endif // wxUSE_DRAG_AND_DROP
261