]>
Commit | Line | Data |
---|---|---|
7dab9892 KO |
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 | |
a9a4f229 | 7 | // RCS-ID: $Id$ |
7dab9892 KO |
8 | // Copyright: (c) 1998 Stefan Csomor |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
d6eb3ff8 SC |
14 | #ifndef WX_PRECOMP |
15 | #include "wx/object.h" | |
16 | #endif | |
17 | ||
7dab9892 KO |
18 | #if wxUSE_DRAG_AND_DROP |
19 | ||
20 | #include "wx/dnd.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/app.h" | |
7dab9892 KO |
24 | #include "wx/toplevel.h" |
25 | #include "wx/gdicmn.h" | |
26 | #include "wx/wx.h" | |
27 | #endif // WX_PRECOMP | |
28 | ||
8a484d9e SC |
29 | #include "wx/evtloop.h" |
30 | ||
7dab9892 KO |
31 | #include "wx/osx/private.h" |
32 | ||
808cbd17 KO |
33 | wxDropSource* gCurrentSource = NULL; |
34 | ||
7dab9892 KO |
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 | } | |
3a853d9e | 50 | return wxDragNone; |
7dab9892 KO |
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; | |
f9022ad2 | 63 | - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint; |
7dab9892 KO |
64 | - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation; |
65 | @end | |
66 | ||
67 | @implementation DropSourceDelegate | |
68 | ||
69 | - (id)init | |
70 | { | |
668e3f70 | 71 | self = [super init]; |
7dab9892 KO |
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 | ||
f9022ad2 KO |
93 | - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint |
94 | { | |
0b6f851f SC |
95 | wxUnusedVar( anImage ); |
96 | wxUnusedVar( aPoint ); | |
97 | ||
f9022ad2 KO |
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 | { | |
0b6f851f SC |
131 | // TODO under 10.6 the os itself deals with the cursor, remove if things |
132 | // work properly everywhere | |
133 | #if 0 | |
f9022ad2 KO |
134 | wxCursor cursor( cursorID ); |
135 | cursor.MacInstall(); | |
0b6f851f | 136 | #endif |
f9022ad2 KO |
137 | } |
138 | } | |
139 | } | |
140 | } | |
141 | ||
7dab9892 KO |
142 | - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation |
143 | { | |
0b6f851f SC |
144 | wxUnusedVar( anImage ); |
145 | wxUnusedVar( aPoint ); | |
146 | ||
7dab9892 KO |
147 | resultCode = operation; |
148 | dragFinished = YES; | |
149 | } | |
150 | ||
151 | @end | |
152 | ||
153 | wxDropTarget::wxDropTarget( wxDataObject *data ) | |
154 | : wxDropTargetBase( data ) | |
155 | { | |
156 | ||
157 | } | |
158 | ||
7dab9892 KO |
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 | ||
808cbd17 KO |
183 | wxDropSource* wxDropSource::GetCurrentDropSource() |
184 | { | |
185 | return gCurrentSource; | |
186 | } | |
187 | ||
0b6f851f | 188 | wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags)) |
7dab9892 KO |
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."); | |
0b6f851f SC |
219 | |
220 | NSPoint down = [theEvent locationInWindow]; | |
221 | NSPoint p = [view convertPoint:down toView:nil]; | |
222 | ||
808cbd17 | 223 | gCurrentSource = this; |
0b6f851f SC |
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 | ||
7dab9892 KO |
241 | DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init]; |
242 | [delegate setImplementation: this]; | |
0b6f851f | 243 | [view dragImage:image at:p offset:NSMakeSize(0.0,0.0) |
7dab9892 KO |
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]; | |
808cbd17 | 253 | gCurrentSource = NULL; |
7dab9892 KO |
254 | } |
255 | ||
256 | ||
257 | return result; | |
258 | } | |
259 | ||
260 | #endif // wxUSE_DRAG_AND_DROP | |
261 |