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