]>
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 | |
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" | |
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 | } | |
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; | |
f9022ad2 | 58 | - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint; |
7dab9892 KO |
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 | ||
f9022ad2 KO |
88 | - (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint |
89 | { | |
0b6f851f SC |
90 | wxUnusedVar( anImage ); |
91 | wxUnusedVar( aPoint ); | |
92 | ||
f9022ad2 KO |
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 | { | |
0b6f851f SC |
126 | // TODO under 10.6 the os itself deals with the cursor, remove if things |
127 | // work properly everywhere | |
128 | #if 0 | |
f9022ad2 KO |
129 | wxCursor cursor( cursorID ); |
130 | cursor.MacInstall(); | |
0b6f851f | 131 | #endif |
f9022ad2 KO |
132 | } |
133 | } | |
134 | } | |
135 | } | |
136 | ||
7dab9892 KO |
137 | - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation |
138 | { | |
0b6f851f SC |
139 | wxUnusedVar( anImage ); |
140 | wxUnusedVar( aPoint ); | |
141 | ||
7dab9892 KO |
142 | resultCode = operation; |
143 | dragFinished = YES; | |
144 | } | |
145 | ||
146 | @end | |
147 | ||
148 | wxDropTarget::wxDropTarget( wxDataObject *data ) | |
149 | : wxDropTargetBase( data ) | |
150 | { | |
151 | ||
152 | } | |
153 | ||
7dab9892 KO |
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 | ||
808cbd17 KO |
178 | wxDropSource* wxDropSource::GetCurrentDropSource() |
179 | { | |
180 | return gCurrentSource; | |
181 | } | |
182 | ||
0b6f851f | 183 | wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags)) |
7dab9892 KO |
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."); | |
0b6f851f SC |
214 | |
215 | NSPoint down = [theEvent locationInWindow]; | |
216 | NSPoint p = [view convertPoint:down toView:nil]; | |
217 | ||
808cbd17 | 218 | gCurrentSource = this; |
0b6f851f SC |
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 | ||
7dab9892 KO |
236 | DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init]; |
237 | [delegate setImplementation: this]; | |
0b6f851f | 238 | [view dragImage:image at:p offset:NSMakeSize(0.0,0.0) |
7dab9892 KO |
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]; | |
808cbd17 | 248 | gCurrentSource = NULL; |
7dab9892 KO |
249 | } |
250 | ||
251 | ||
252 | return result; | |
253 | } | |
254 | ||
255 | #endif // wxUSE_DRAG_AND_DROP | |
256 |