]>
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" | |
20 | #include "wx/evtloop.h" | |
21 | #include "wx/toplevel.h" | |
22 | #include "wx/gdicmn.h" | |
23 | #include "wx/wx.h" | |
24 | #endif // WX_PRECOMP | |
25 | ||
26 | #include <AppKit/AppKit.h> | |
27 | #include "wx/osx/private.h" | |
28 | ||
29 | wxDragResult NSDragOperationToWxDragResult(NSDragOperation code) | |
30 | { | |
31 | switch (code) | |
32 | { | |
33 | case NSDragOperationCopy: | |
34 | return wxDragCopy; | |
35 | case NSDragOperationMove: | |
36 | return wxDragMove; | |
37 | case NSDragOperationLink: | |
38 | return wxDragLink; | |
39 | case NSDragOperationNone: | |
40 | return wxDragNone; | |
41 | default: | |
42 | wxFAIL_MSG("Unexpected result code"); | |
43 | } | |
44 | } | |
45 | ||
46 | @interface DropSourceDelegate : NSObject | |
47 | { | |
48 | BOOL dragFinished; | |
49 | int resultCode; | |
50 | wxDropSource* impl; | |
51 | } | |
52 | ||
53 | - (void)setImplementation: (wxDropSource *)dropSource; | |
54 | - (BOOL)finished; | |
55 | - (NSDragOperation)code; | |
56 | - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation; | |
57 | @end | |
58 | ||
59 | @implementation DropSourceDelegate | |
60 | ||
61 | - (id)init | |
62 | { | |
63 | [super init]; | |
64 | dragFinished = NO; | |
65 | resultCode = NSDragOperationNone; | |
66 | impl = 0; | |
67 | return self; | |
68 | } | |
69 | ||
70 | - (void)setImplementation: (wxDropSource *)dropSource | |
71 | { | |
72 | impl = dropSource; | |
73 | } | |
74 | ||
75 | - (BOOL)finished | |
76 | { | |
77 | return dragFinished; | |
78 | } | |
79 | ||
80 | - (NSDragOperation)code | |
81 | { | |
82 | return resultCode; | |
83 | } | |
84 | ||
85 | - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation | |
86 | { | |
87 | resultCode = operation; | |
88 | dragFinished = YES; | |
89 | } | |
90 | ||
91 | @end | |
92 | ||
93 | wxDropTarget::wxDropTarget( wxDataObject *data ) | |
94 | : wxDropTargetBase( data ) | |
95 | { | |
96 | ||
97 | } | |
98 | ||
99 | bool wxDropTarget::CurrentDragHasSupportedFormat() | |
100 | { | |
101 | if (m_dataObject == NULL) | |
102 | return false; | |
103 | ||
104 | return m_dataObject->HasDataInPasteboard( m_currentDragPasteboard ); | |
105 | } | |
106 | ||
107 | bool wxDropTarget::GetData() | |
108 | { | |
109 | if (m_dataObject == NULL) | |
110 | return false; | |
111 | ||
112 | if ( !CurrentDragHasSupportedFormat() ) | |
113 | return false; | |
114 | ||
115 | return m_dataObject->GetFromPasteboard( m_currentDragPasteboard ); | |
116 | } | |
117 | ||
118 | //------------------------------------------------------------------------- | |
119 | // wxDropSource | |
120 | //------------------------------------------------------------------------- | |
121 | ||
122 | wxDropSource::wxDropSource(wxWindow *win, | |
123 | const wxCursor &cursorCopy, | |
124 | const wxCursor &cursorMove, | |
125 | const wxCursor &cursorStop) | |
126 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
127 | { | |
128 | m_window = win; | |
129 | } | |
130 | ||
131 | wxDropSource::wxDropSource(wxDataObject& data, | |
132 | wxWindow *win, | |
133 | const wxCursor &cursorCopy, | |
134 | const wxCursor &cursorMove, | |
135 | const wxCursor &cursorStop) | |
136 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
137 | { | |
138 | SetData( data ); | |
139 | m_window = win; | |
140 | } | |
141 | ||
142 | wxDragResult wxDropSource::DoDragDrop(int flags) | |
143 | { | |
144 | wxASSERT_MSG( m_data, wxT("Drop source: no data") ); | |
145 | ||
146 | wxDragResult result = wxDragNone; | |
147 | if ((m_data == NULL) || (m_data->GetFormatCount() == 0)) | |
148 | return result; | |
149 | ||
150 | NSView* view = m_window->GetPeer()->GetWXWidget(); | |
151 | if (view) | |
152 | { | |
153 | NSPasteboard *pboard; | |
154 | ||
155 | pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; | |
156 | ||
157 | OSStatus err = noErr; | |
158 | PasteboardRef pboardRef; | |
159 | PasteboardCreate((CFStringRef)[pboard name], &pboardRef); | |
160 | ||
161 | err = PasteboardClear( pboardRef ); | |
162 | if ( err != noErr ) | |
163 | { | |
164 | CFRelease( pboardRef ); | |
165 | return wxDragNone; | |
166 | } | |
167 | PasteboardSynchronize( pboardRef ); | |
168 | ||
169 | m_data->AddToPasteboard( pboardRef, 1 ); | |
170 | ||
171 | NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent(); | |
172 | wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event."); | |
173 | ||
174 | NSImage* image = [[NSImage alloc] initWithSize: NSMakeSize(16,16)]; | |
175 | DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init]; | |
176 | [delegate setImplementation: this]; | |
177 | [view dragImage:image at:NSMakePoint(0.0, 16.0) offset:NSMakeSize(0.0,0.0) | |
178 | event: theEvent pasteboard: pboard source:delegate slideBack: NO]; | |
179 | ||
180 | wxEventLoopBase * const loop = wxEventLoop::GetActive(); | |
181 | while ( ![delegate finished] ) | |
182 | loop->Dispatch(); | |
183 | ||
184 | result = NSDragOperationToWxDragResult([delegate code]); | |
185 | [delegate release]; | |
186 | [image release]; | |
187 | } | |
188 | ||
189 | ||
190 | return result; | |
191 | } | |
192 | ||
193 | #endif // wxUSE_DRAG_AND_DROP | |
194 |