]>
Commit | Line | Data |
---|---|---|
e9576ca5 | 1 | /////////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/mac/carbon/dnd.cpp |
6eae1f7d | 3 | // Purpose: wxDropTarget, wxDropSource implementations |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) 1998 Stefan Csomor |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 | 12 | #include "wx/wxprec.h" |
ff143598 GD |
13 | |
14 | #if wxUSE_DRAG_AND_DROP | |
15 | ||
e9576ca5 | 16 | #include "wx/dnd.h" |
670f9935 WS |
17 | |
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/app.h" | |
20 | #endif // WX_PRECOMP | |
21 | ||
e9576ca5 | 22 | #include "wx/window.h" |
ff143598 | 23 | #include "wx/toplevel.h" |
e9576ca5 | 24 | #include "wx/gdicmn.h" |
76a5e5d2 | 25 | #include "wx/mac/private.h" |
e9576ca5 | 26 | |
64179ebc DS |
27 | #ifndef __DARWIN__ |
28 | #include <Scrap.h> | |
29 | #endif | |
30 | ||
e22ab33a | 31 | |
e9576ca5 | 32 | // ---------------------------------------------------------------------------- |
6eae1f7d | 33 | // globals |
e9576ca5 SC |
34 | // ---------------------------------------------------------------------------- |
35 | ||
6eae1f7d | 36 | typedef struct |
e9576ca5 | 37 | { |
51d4293d DS |
38 | wxWindow *m_currentTargetWindow; |
39 | wxDropTarget *m_currentTarget; | |
40 | wxDropSource *m_currentSource; | |
6eae1f7d | 41 | } |
e22ab33a | 42 | MacTrackingGlobals; |
e9576ca5 | 43 | |
e22ab33a | 44 | MacTrackingGlobals gTrackingGlobals; |
6eae1f7d | 45 | |
51d4293d | 46 | void wxMacEnsureTrackingHandlersInstalled(); |
e9576ca5 | 47 | |
a07c1212 SC |
48 | //---------------------------------------------------------------------------- |
49 | // wxDropTarget | |
50 | //---------------------------------------------------------------------------- | |
e9576ca5 | 51 | |
a07c1212 SC |
52 | wxDropTarget::wxDropTarget( wxDataObject *data ) |
53 | : wxDropTargetBase( data ) | |
e9576ca5 | 54 | { |
51d4293d | 55 | wxMacEnsureTrackingHandlersInstalled(); |
e9576ca5 SC |
56 | } |
57 | ||
51d4293d DS |
58 | wxDragResult wxDropTarget::OnDragOver( |
59 | wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), | |
60 | wxDragResult def ) | |
e9576ca5 | 61 | { |
a07c1212 SC |
62 | return CurrentDragHasSupportedFormat() ? def : wxDragNone; |
63 | } | |
e9576ca5 | 64 | |
a07c1212 | 65 | bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) ) |
e9576ca5 | 66 | { |
876b960a | 67 | if (m_dataObject == NULL) |
6eae1f7d | 68 | return false; |
a07c1212 | 69 | |
51d4293d | 70 | return CurrentDragHasSupportedFormat(); |
e9576ca5 SC |
71 | } |
72 | ||
51d4293d DS |
73 | wxDragResult wxDropTarget::OnData( |
74 | wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), | |
75 | wxDragResult def ) | |
e9576ca5 | 76 | { |
876b960a | 77 | if (m_dataObject == NULL) |
a07c1212 SC |
78 | return wxDragNone; |
79 | ||
80 | if (!CurrentDragHasSupportedFormat()) | |
81 | return wxDragNone; | |
82 | ||
83 | return GetData() ? def : wxDragNone; | |
e9576ca5 SC |
84 | } |
85 | ||
6eae1f7d | 86 | bool wxDropTarget::CurrentDragHasSupportedFormat() |
e9576ca5 | 87 | { |
51d4293d | 88 | bool supported = false; |
876b960a | 89 | |
a07c1212 SC |
90 | if ( gTrackingGlobals.m_currentSource != NULL ) |
91 | { | |
51d4293d | 92 | wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject(); |
6eae1f7d | 93 | |
e40298d5 | 94 | if ( data ) |
a07c1212 | 95 | { |
51d4293d | 96 | size_t formatcount = data->GetFormatCount(); |
876b960a | 97 | wxDataFormat *array = new wxDataFormat[formatcount]; |
e40298d5 | 98 | data->GetAllFormats( array ); |
51d4293d | 99 | for (size_t i = 0; !supported && i < formatcount; i++) |
a07c1212 | 100 | { |
51d4293d | 101 | wxDataFormat format = array[i]; |
6eae1f7d | 102 | if ( m_dataObject->IsSupported( format ) ) |
e40298d5 | 103 | { |
51d4293d DS |
104 | supported = true; |
105 | break; | |
e40298d5 | 106 | } |
a07c1212 | 107 | } |
6eae1f7d | 108 | |
51d4293d | 109 | delete [] array; |
a07c1212 | 110 | } |
a07c1212 | 111 | } |
6eae1f7d | 112 | |
a07c1212 SC |
113 | if ( !supported ) |
114 | { | |
51d4293d | 115 | UInt16 items; |
e40298d5 | 116 | OSErr result; |
6eae1f7d | 117 | ItemReference theItem; |
51d4293d DS |
118 | FlavorType theType; |
119 | UInt16 flavors = 0; | |
6eae1f7d | 120 | |
e22ab33a | 121 | CountDragItems( (DragReference)m_currentDrag, &items ); |
876b960a | 122 | for (UInt16 index = 1; index <= items && !supported; ++index) |
e40298d5 | 123 | { |
51d4293d | 124 | flavors = 0; |
876b960a DS |
125 | GetDragItemReferenceNumber( (DragReference)m_currentDrag, index, &theItem ); |
126 | CountDragItemFlavors( (DragReference)m_currentDrag, theItem, &flavors ); | |
6eae1f7d | 127 | |
51d4293d | 128 | for ( UInt16 flavor = 1; flavor <= flavors; ++flavor ) |
a07c1212 | 129 | { |
876b960a | 130 | result = GetFlavorType( (DragReference)m_currentDrag, theItem, flavor, &theType ); |
e40298d5 JS |
131 | if ( m_dataObject->IsSupportedFormat( wxDataFormat( theType ) ) ) |
132 | { | |
51d4293d DS |
133 | supported = true; |
134 | break; | |
e40298d5 | 135 | } |
a07c1212 | 136 | } |
e40298d5 | 137 | } |
a07c1212 | 138 | } |
6eae1f7d | 139 | |
51d4293d | 140 | return supported; |
e9576ca5 SC |
141 | } |
142 | ||
a07c1212 | 143 | bool wxDropTarget::GetData() |
e9576ca5 | 144 | { |
e22ab33a | 145 | if (m_dataObject == NULL) |
6eae1f7d DS |
146 | return false; |
147 | ||
a07c1212 | 148 | if ( !CurrentDragHasSupportedFormat() ) |
2e91d506 | 149 | return false; |
6eae1f7d | 150 | |
2e91d506 | 151 | bool transferred = false; |
a07c1212 SC |
152 | if ( gTrackingGlobals.m_currentSource != NULL ) |
153 | { | |
2e91d506 | 154 | wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject(); |
6eae1f7d | 155 | |
2e91d506 | 156 | if (data != NULL) |
a07c1212 | 157 | { |
2e91d506 | 158 | size_t formatcount = data->GetFormatCount(); |
6eae1f7d | 159 | wxDataFormat *array = new wxDataFormat[formatcount]; |
e40298d5 | 160 | data->GetAllFormats( array ); |
2e91d506 | 161 | for (size_t i = 0; !transferred && i < formatcount; i++) |
a07c1212 | 162 | { |
2e91d506 | 163 | wxDataFormat format = array[i]; |
6eae1f7d | 164 | if ( m_dataObject->IsSupported( format ) ) |
e40298d5 JS |
165 | { |
166 | int size = data->GetDataSize( format ); | |
2e91d506 | 167 | transferred = true; |
6eae1f7d DS |
168 | |
169 | if (size == 0) | |
e40298d5 | 170 | { |
2e91d506 | 171 | m_dataObject->SetData( format, 0, 0 ); |
e40298d5 JS |
172 | } |
173 | else | |
174 | { | |
175 | char *d = new char[size]; | |
2e91d506 DS |
176 | data->GetDataHere( format, (void*)d ); |
177 | m_dataObject->SetData( format, size, d ); | |
178 | delete [] d; | |
e40298d5 JS |
179 | } |
180 | } | |
a07c1212 | 181 | } |
6eae1f7d | 182 | |
2e91d506 | 183 | delete [] array; |
e40298d5 | 184 | } |
a07c1212 | 185 | } |
6eae1f7d | 186 | |
a07c1212 SC |
187 | if ( !transferred ) |
188 | { | |
51d4293d | 189 | UInt16 items; |
e40298d5 | 190 | OSErr result; |
6eae1f7d | 191 | ItemReference theItem; |
51d4293d | 192 | FlavorType theType; |
6eae1f7d | 193 | FlavorFlags theFlags; |
51d4293d | 194 | UInt16 flavors; |
de5e599c | 195 | wxString filenamesPassed; |
6eae1f7d | 196 | |
876b960a | 197 | CountDragItems( (DragReference)m_currentDrag, &items ); |
6eae1f7d | 198 | for (UInt16 index = 1; index <= items; ++index) |
e40298d5 | 199 | { |
51d4293d | 200 | flavors = 0; |
876b960a | 201 | GetDragItemReferenceNumber( (DragReference)m_currentDrag, index, &theItem ); |
51d4293d | 202 | CountDragItemFlavors( (DragReference)m_currentDrag, theItem, &flavors ); |
e22ab33a | 203 | wxDataFormat preferredFormat = m_dataObject->GetPreferredFormat( wxDataObject::Set ); |
51d4293d | 204 | bool hasPreferredFormat = false; |
6eae1f7d | 205 | |
de5e599c | 206 | for (UInt16 flavor = 1; flavor <= flavors; ++flavor) |
a07c1212 | 207 | { |
876b960a DS |
208 | result = GetFlavorType( (DragReference)m_currentDrag, theItem, flavor, &theType ); |
209 | wxDataFormat format( theType ); | |
de5e599c | 210 | if (preferredFormat == format) |
6db6bfd1 | 211 | { |
51d4293d DS |
212 | hasPreferredFormat = true; |
213 | break; | |
6db6bfd1 SC |
214 | } |
215 | } | |
6eae1f7d | 216 | |
de5e599c | 217 | for (UInt16 flavor = 1; flavor <= flavors; ++flavor) |
6db6bfd1 | 218 | { |
e22ab33a | 219 | result = GetFlavorType( (DragReference)m_currentDrag, theItem, flavor, &theType ); |
51d4293d | 220 | wxDataFormat format( theType ); |
de5e599c | 221 | if ((hasPreferredFormat && format == preferredFormat) |
876b960a | 222 | || (!hasPreferredFormat && m_dataObject->IsSupportedFormat( format ))) |
e40298d5 | 223 | { |
876b960a | 224 | result = GetFlavorFlags( (DragReference)m_currentDrag, theItem, theType, &theFlags ); |
6eae1f7d | 225 | if (result == noErr) |
a07c1212 | 226 | { |
51d4293d DS |
227 | Size dataSize; |
228 | Ptr theData; | |
6eae1f7d | 229 | |
876b960a | 230 | GetFlavorDataSize( (DragReference)m_currentDrag, theItem, theType, &dataSize ); |
de5e599c | 231 | if (theType == kScrapFlavorTypeText) |
e40298d5 | 232 | { |
e22ab33a DS |
233 | // this increment is only valid for allocating: |
234 | // on the next GetFlavorData call it is reset again to the original value | |
51d4293d | 235 | dataSize++; |
e40298d5 | 236 | } |
de5e599c | 237 | else if (theType == kScrapFlavorTypeUnicode) |
2b2832ef | 238 | { |
e22ab33a DS |
239 | // this increment is only valid for allocating: |
240 | // on the next GetFlavorData call it is reset again to the original value | |
51d4293d DS |
241 | dataSize++; |
242 | dataSize++; | |
2b2832ef | 243 | } |
6eae1f7d | 244 | |
2e91d506 DS |
245 | if (dataSize > 0) |
246 | theData = new char[dataSize]; | |
247 | else | |
248 | theData = NULL; | |
876b960a | 249 | |
de5e599c | 250 | GetFlavorData( (DragReference)m_currentDrag, theItem, theType, (void*)theData, &dataSize, 0L ); |
51d4293d | 251 | switch (theType) |
2b2832ef | 252 | { |
51d4293d DS |
253 | case kScrapFlavorTypeText: |
254 | theData[dataSize] = 0; | |
255 | m_dataObject->SetData( wxDataFormat(wxDF_TEXT), dataSize, theData ); | |
256 | break; | |
257 | ||
63c6e6c8 | 258 | #if wxUSE_UNICODE |
51d4293d | 259 | case kScrapFlavorTypeUnicode: |
876b960a | 260 | theData[dataSize + 0] = |
51d4293d DS |
261 | theData[dataSize + 1] = 0; |
262 | m_dataObject->SetData( wxDataFormat(wxDF_UNICODETEXT), dataSize, theData ); | |
263 | break; | |
63c6e6c8 | 264 | #endif |
63c6e6c8 | 265 | |
51d4293d | 266 | case kDragFlavorTypeHFS: |
de5e599c | 267 | if (theData != NULL) |
e40298d5 | 268 | { |
de5e599c DS |
269 | HFSFlavor* theFile = (HFSFlavor*)theData; |
270 | wxString name = wxMacFSSpec2MacFilename( &theFile->fileSpec ); | |
271 | ||
670f9935 | 272 | if (!name.empty()) |
de5e599c | 273 | filenamesPassed += name + wxT("\n"); |
e40298d5 | 274 | } |
51d4293d | 275 | break; |
de5e599c | 276 | |
51d4293d | 277 | default: |
e40298d5 | 278 | m_dataObject->SetData( format, dataSize, theData ); |
51d4293d | 279 | break; |
e40298d5 | 280 | } |
6eae1f7d DS |
281 | |
282 | delete [] theData; | |
a07c1212 | 283 | } |
51d4293d | 284 | break; |
e40298d5 | 285 | } |
a07c1212 | 286 | } |
e40298d5 | 287 | } |
de5e599c DS |
288 | |
289 | if (filenamesPassed.Len() > 0) | |
55f5548f SC |
290 | { |
291 | wxCharBuffer buf = filenamesPassed.fn_str(); | |
de5e599c | 292 | m_dataObject->SetData( wxDataFormat(wxDF_FILENAME), strlen( buf ), (const char*)buf ); |
55f5548f | 293 | } |
a07c1212 | 294 | } |
6eae1f7d | 295 | |
51d4293d | 296 | return true; |
e9576ca5 SC |
297 | } |
298 | ||
299 | //------------------------------------------------------------------------- | |
300 | // wxDropSource | |
301 | //------------------------------------------------------------------------- | |
302 | ||
303 | //----------------------------------------------------------------------------- | |
304 | // drag request | |
305 | ||
a07c1212 | 306 | wxDropSource::wxDropSource(wxWindow *win, |
da804130 SC |
307 | const wxCursor &cursorCopy, |
308 | const wxCursor &cursorMove, | |
309 | const wxCursor &cursorStop) | |
310 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
e9576ca5 | 311 | { |
51d4293d | 312 | wxMacEnsureTrackingHandlersInstalled(); |
e22ab33a | 313 | |
a07c1212 SC |
314 | m_window = win; |
315 | } | |
e9576ca5 | 316 | |
a07c1212 SC |
317 | wxDropSource::wxDropSource(wxDataObject& data, |
318 | wxWindow *win, | |
da804130 SC |
319 | const wxCursor &cursorCopy, |
320 | const wxCursor &cursorMove, | |
321 | const wxCursor &cursorStop) | |
322 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
a07c1212 | 323 | { |
51d4293d | 324 | wxMacEnsureTrackingHandlersInstalled(); |
e22ab33a | 325 | |
a07c1212 SC |
326 | SetData( data ); |
327 | m_window = win; | |
328 | } | |
e9576ca5 | 329 | |
a07c1212 | 330 | wxDropSource::~wxDropSource() |
e9576ca5 | 331 | { |
a07c1212 | 332 | } |
e9576ca5 | 333 | |
525ae3fe | 334 | wxDragResult wxDropSource::DoDragDrop(int flags) |
e9576ca5 | 335 | { |
a07c1212 | 336 | wxASSERT_MSG( m_data, wxT("Drop source: no data") ); |
6eae1f7d | 337 | |
de5e599c DS |
338 | if ((m_data == NULL) || (m_data->GetFormatCount() == 0)) |
339 | return (wxDragResult)wxDragNone; | |
6eae1f7d | 340 | |
2e91d506 | 341 | OSStatus result; |
a07c1212 SC |
342 | DragReference theDrag; |
343 | RgnHandle dragRegion; | |
de5e599c DS |
344 | |
345 | if ((result = NewDrag( &theDrag )) != noErr) | |
51d4293d | 346 | return wxDragNone; |
6eae1f7d | 347 | |
a07c1212 | 348 | // add data to drag |
51d4293d DS |
349 | size_t formatCount = m_data->GetFormatCount(); |
350 | wxDataFormat *formats = new wxDataFormat[formatCount]; | |
351 | m_data->GetAllFormats( formats ); | |
352 | ItemReference theItem = 1; | |
6eae1f7d | 353 | |
51d4293d | 354 | for ( size_t i = 0; i < formatCount; ++i ) |
a07c1212 | 355 | { |
51d4293d DS |
356 | size_t dataSize = m_data->GetDataSize( formats[i] ); |
357 | Ptr dataPtr = new char[dataSize]; | |
358 | m_data->GetDataHere( formats[i], dataPtr ); | |
359 | OSType type = formats[i].GetFormatId(); | |
6ac23e25 | 360 | if ( type == 'TEXT' || type == 'utxt' ) |
a07c1212 | 361 | { |
6ac23e25 | 362 | if ( dataSize > 0 ) |
51d4293d DS |
363 | dataSize--; |
364 | dataPtr[ dataSize ] = 0; | |
6ac23e25 SC |
365 | if ( type == 'utxt' ) |
366 | { | |
367 | if ( dataSize > 0 ) | |
51d4293d DS |
368 | dataSize--; |
369 | dataPtr[ dataSize ] = 0; | |
6ac23e25 | 370 | } |
6eae1f7d | 371 | |
51d4293d | 372 | AddDragItemFlavor( theDrag, theItem, type, dataPtr, dataSize, 0 ); |
a07c1212 SC |
373 | } |
374 | else if (type == kDragFlavorTypeHFS ) | |
375 | { | |
51d4293d | 376 | HFSFlavor theFlavor; |
e40298d5 JS |
377 | OSErr err = noErr; |
378 | CInfoPBRec cat; | |
6eae1f7d | 379 | |
51d4293d | 380 | wxMacFilename2FSSpec( wxString( dataPtr, *wxConvCurrent ), &theFlavor.fileSpec ); |
6eae1f7d DS |
381 | |
382 | memset( &cat, 0, sizeof(cat) ); | |
e40298d5 JS |
383 | cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name; |
384 | cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum; | |
385 | cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID; | |
386 | cat.hFileInfo.ioFDirIndex = 0; | |
e22ab33a | 387 | err = PBGetCatInfoSync( &cat ); |
6eae1f7d | 388 | if (err == noErr) |
e40298d5 JS |
389 | { |
390 | theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags; | |
6eae1f7d DS |
391 | if (theFlavor.fileSpec.parID == fsRtParID) |
392 | { | |
e40298d5 JS |
393 | theFlavor.fileCreator = 'MACS'; |
394 | theFlavor.fileType = 'disk'; | |
6eae1f7d DS |
395 | } |
396 | else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) | |
397 | { | |
e40298d5 JS |
398 | theFlavor.fileCreator = 'MACS'; |
399 | theFlavor.fileType = 'fold'; | |
6eae1f7d DS |
400 | } |
401 | else | |
402 | { | |
e40298d5 JS |
403 | theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator; |
404 | theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType; | |
405 | } | |
6eae1f7d | 406 | |
51d4293d | 407 | AddDragItemFlavor( theDrag, theItem, type, &theFlavor, sizeof(theFlavor), 0 ); |
6eae1f7d | 408 | } |
a07c1212 SC |
409 | } |
410 | else | |
411 | { | |
51d4293d | 412 | AddDragItemFlavor( theDrag, theItem, type, dataPtr, dataSize, 0 ); |
a07c1212 | 413 | } |
6eae1f7d | 414 | |
51d4293d | 415 | delete [] dataPtr; |
a07c1212 | 416 | } |
6eae1f7d | 417 | |
51d4293d | 418 | delete [] formats; |
6eae1f7d | 419 | |
a07c1212 | 420 | dragRegion = NewRgn(); |
51d4293d | 421 | RgnHandle tempRgn = NewRgn(); |
6eae1f7d | 422 | |
51d4293d | 423 | EventRecord* ev = NULL; |
6eae1f7d | 424 | |
45a0fd37 | 425 | #if !TARGET_CARBON // TODO |
51d4293d | 426 | ev = (EventRecord*) wxTheApp->MacGetCurrentEvent(); |
45a0fd37 | 427 | #else |
db28a493 SC |
428 | EventRecord rec; |
429 | ev = &rec; | |
430 | wxMacConvertEventToRecord( (EventRef) wxTheApp->MacGetCurrentEvent(), &rec ); | |
45a0fd37 | 431 | #endif |
6eae1f7d | 432 | |
51d4293d DS |
433 | const short dragRegionOuterBoundary = 10; |
434 | const short dragRegionInnerBoundary = 9; | |
6eae1f7d DS |
435 | |
436 | SetRectRgn( | |
51d4293d DS |
437 | dragRegion, |
438 | ev->where.h - dragRegionOuterBoundary, | |
439 | ev->where.v - dragRegionOuterBoundary, | |
440 | ev->where.h + dragRegionOuterBoundary, | |
441 | ev->where.v + dragRegionOuterBoundary ); | |
6eae1f7d DS |
442 | |
443 | SetRectRgn( | |
51d4293d DS |
444 | tempRgn, |
445 | ev->where.h - dragRegionInnerBoundary, | |
446 | ev->where.v - dragRegionInnerBoundary, | |
447 | ev->where.h + dragRegionInnerBoundary, | |
448 | ev->where.v + dragRegionInnerBoundary ); | |
6eae1f7d | 449 | |
51d4293d DS |
450 | DiffRgn( dragRegion, tempRgn, dragRegion ); |
451 | DisposeRgn( tempRgn ); | |
6eae1f7d | 452 | |
e22ab33a DS |
453 | // TODO: work with promises in order to return data |
454 | // only when drag was successfully completed | |
6eae1f7d | 455 | |
e22ab33a DS |
456 | gTrackingGlobals.m_currentSource = this; |
457 | result = TrackDrag( theDrag, ev, dragRegion ); | |
458 | DisposeRgn( dragRegion ); | |
459 | DisposeDrag( theDrag ); | |
460 | gTrackingGlobals.m_currentSource = NULL; | |
6eae1f7d | 461 | |
e22ab33a DS |
462 | bool optionDown = GetCurrentKeyModifiers() & optionKey; |
463 | wxDragResult dndresult = wxDragCopy; | |
6eae1f7d | 464 | if ( flags != wxDrag_CopyOnly ) |
525ae3fe SC |
465 | // on mac the option key is always the indication for copy |
466 | dndresult = optionDown ? wxDragCopy : wxDragMove; | |
6eae1f7d | 467 | |
f43084de | 468 | return dndresult; |
a07c1212 SC |
469 | } |
470 | ||
da804130 SC |
471 | bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect) |
472 | { | |
473 | const wxCursor& cursor = GetCursor(effect); | |
6eae1f7d DS |
474 | bool result = cursor.Ok(); |
475 | ||
476 | if ( result ) | |
e22ab33a | 477 | cursor.MacInstall(); |
da804130 | 478 | |
6eae1f7d | 479 | return result; |
da804130 SC |
480 | } |
481 | ||
51d4293d | 482 | bool gTrackingGlobalsInstalled = false; |
a07c1212 SC |
483 | |
484 | // passing the globals via refcon is not needed by the CFM and later architectures anymore | |
485 | // but I'll leave it in there, just in case... | |
486 | ||
6eae1f7d DS |
487 | pascal OSErr wxMacWindowDragTrackingHandler( |
488 | DragTrackingMessage theMessage, WindowPtr theWindow, | |
e22ab33a | 489 | void *handlerRefCon, DragReference theDrag ); |
6eae1f7d DS |
490 | pascal OSErr wxMacWindowDragReceiveHandler( |
491 | WindowPtr theWindow, void *handlerRefCon, | |
e22ab33a | 492 | DragReference theDrag ); |
a07c1212 SC |
493 | |
494 | void wxMacEnsureTrackingHandlersInstalled() | |
e9576ca5 | 495 | { |
6eae1f7d | 496 | if ( !gTrackingGlobalsInstalled ) |
a07c1212 | 497 | { |
2e91d506 | 498 | OSStatus err; |
a07c1212 | 499 | |
2e91d506 DS |
500 | err = InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L, &gTrackingGlobals ); |
501 | verify_noerr( err ); | |
6eae1f7d | 502 | |
2e91d506 DS |
503 | err = InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals ); |
504 | verify_noerr( err ); | |
e9576ca5 | 505 | |
e22ab33a | 506 | gTrackingGlobalsInstalled = true; |
a07c1212 SC |
507 | } |
508 | } | |
509 | ||
6eae1f7d DS |
510 | pascal OSErr wxMacWindowDragTrackingHandler( |
511 | DragTrackingMessage theMessage, WindowPtr theWindow, | |
e22ab33a | 512 | void *handlerRefCon, DragReference theDrag ) |
6eae1f7d | 513 | { |
a07c1212 | 514 | MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon; |
e22ab33a | 515 | |
a07c1212 SC |
516 | Point mouse, localMouse; |
517 | DragAttributes attributes; | |
e22ab33a DS |
518 | |
519 | GetDragAttributes( theDrag, &attributes ); | |
520 | ||
51d4293d | 521 | wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ); |
f43084de | 522 | |
51d4293d | 523 | bool optionDown = GetCurrentKeyModifiers() & optionKey; |
f43084de JS |
524 | wxDragResult result = optionDown ? wxDragCopy : wxDragMove; |
525 | ||
6eae1f7d | 526 | switch (theMessage) |
a07c1212 SC |
527 | { |
528 | case kDragTrackingEnterHandler: | |
a07c1212 SC |
529 | case kDragTrackingLeaveHandler: |
530 | break; | |
6eae1f7d | 531 | |
a07c1212 | 532 | case kDragTrackingEnterWindow: |
e22ab33a DS |
533 | if (trackingGlobals != NULL) |
534 | { | |
51d4293d DS |
535 | trackingGlobals->m_currentTargetWindow = NULL; |
536 | trackingGlobals->m_currentTarget = NULL; | |
e22ab33a | 537 | } |
a07c1212 | 538 | break; |
6eae1f7d | 539 | |
a07c1212 | 540 | case kDragTrackingInWindow: |
e22ab33a DS |
541 | if (trackingGlobals == NULL) |
542 | break; | |
a07c1212 SC |
543 | if (toplevel == NULL) |
544 | break; | |
545 | ||
e22ab33a | 546 | GetDragMouse( theDrag, &mouse, 0L ); |
a07c1212 | 547 | localMouse = mouse; |
e22ab33a | 548 | GlobalToLocal( &localMouse ); |
f43084de | 549 | |
a07c1212 | 550 | { |
51d4293d DS |
551 | wxWindow *win = NULL; |
552 | ControlPartCode controlPart; | |
6eae1f7d | 553 | ControlRef control = wxMacFindControlUnderMouse( |
51d4293d | 554 | toplevel, localMouse, theWindow, &controlPart ); |
2877659b | 555 | if ( control ) |
51d4293d | 556 | win = wxFindControlFromMacControl( control ); |
7adabb9a | 557 | else |
51d4293d | 558 | win = toplevel; |
6eae1f7d | 559 | |
51d4293d DS |
560 | int localx, localy; |
561 | localx = localMouse.h; | |
562 | localy = localMouse.v; | |
7adabb9a | 563 | |
a07c1212 | 564 | if ( win ) |
51d4293d | 565 | win->MacRootWindowToWindow( &localx, &localy ); |
a07c1212 SC |
566 | if ( win != trackingGlobals->m_currentTargetWindow ) |
567 | { | |
568 | if ( trackingGlobals->m_currentTargetWindow ) | |
569 | { | |
570 | // this window is left | |
571 | if ( trackingGlobals->m_currentTarget ) | |
572 | { | |
6eae1f7d | 573 | HideDragHilite( theDrag ); |
876b960a DS |
574 | trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); |
575 | trackingGlobals->m_currentTarget->OnLeave(); | |
a07c1212 | 576 | trackingGlobals->m_currentTarget = NULL; |
876b960a | 577 | trackingGlobals->m_currentTargetWindow = NULL; |
a07c1212 SC |
578 | } |
579 | } | |
6eae1f7d | 580 | |
a07c1212 SC |
581 | if ( win ) |
582 | { | |
583 | // this window is entered | |
51d4293d DS |
584 | trackingGlobals->m_currentTargetWindow = win; |
585 | trackingGlobals->m_currentTarget = win->GetDropTarget(); | |
a07c1212 | 586 | { |
6eae1f7d DS |
587 | if ( trackingGlobals->m_currentTarget ) |
588 | { | |
51d4293d DS |
589 | trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); |
590 | result = trackingGlobals->m_currentTarget->OnEnter( localx, localy, result ); | |
da804130 | 591 | } |
6eae1f7d | 592 | |
da804130 | 593 | if ( result != wxDragNone ) |
a07c1212 | 594 | { |
51d4293d | 595 | int x, y; |
6eae1f7d | 596 | |
51d4293d DS |
597 | x = y = 0; |
598 | win->MacWindowToRootWindow( &x, &y ); | |
599 | RgnHandle hiliteRgn = NewRgn(); | |
600 | Rect r = { y, x, y + win->GetSize().y, x + win->GetSize().x }; | |
601 | RectRgn( hiliteRgn, &r ); | |
876b960a | 602 | ShowDragHilite( theDrag, hiliteRgn, true ); |
51d4293d | 603 | DisposeRgn( hiliteRgn ); |
a07c1212 SC |
604 | } |
605 | } | |
606 | } | |
607 | } | |
608 | else | |
609 | { | |
6eae1f7d | 610 | if ( trackingGlobals->m_currentTarget ) |
a07c1212 | 611 | { |
51d4293d DS |
612 | trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); |
613 | trackingGlobals->m_currentTarget->OnDragOver( localx, localy, result ); | |
f43084de JS |
614 | } |
615 | } | |
616 | ||
617 | // set cursor for OnEnter and OnDragOver | |
bf473da7 | 618 | if ( trackingGlobals->m_currentSource && !trackingGlobals->m_currentSource->GiveFeedback( result ) ) |
f43084de | 619 | { |
6eae1f7d | 620 | if ( !trackingGlobals->m_currentSource->MacInstallDefaultCursor( result ) ) |
f43084de | 621 | { |
de5e599c DS |
622 | int cursorID = wxCURSOR_NONE; |
623 | ||
624 | switch (result) | |
f43084de | 625 | { |
2e91d506 | 626 | case wxDragCopy: |
de5e599c | 627 | cursorID = wxCURSOR_COPY_ARROW; |
51d4293d | 628 | break; |
6eae1f7d | 629 | |
2e91d506 | 630 | case wxDragMove: |
de5e599c | 631 | cursorID = wxCURSOR_ARROW; |
51d4293d | 632 | break; |
6eae1f7d | 633 | |
2e91d506 | 634 | case wxDragNone: |
de5e599c | 635 | cursorID = wxCURSOR_NO_ENTRY; |
51d4293d | 636 | break; |
eb7f8ac5 VZ |
637 | |
638 | case wxDragError: | |
639 | case wxDragLink: | |
640 | case wxDragCancel: | |
6eae1f7d | 641 | default: |
eb7f8ac5 VZ |
642 | // put these here to make gcc happy |
643 | ; | |
f43084de | 644 | } |
de5e599c DS |
645 | |
646 | if (cursorID != wxCURSOR_NONE) | |
647 | { | |
648 | wxCursor cursor( cursorID ); | |
649 | cursor.MacInstall(); | |
650 | } | |
6eae1f7d | 651 | } |
a07c1212 | 652 | } |
7adabb9a | 653 | } |
a07c1212 | 654 | break; |
6eae1f7d | 655 | |
a07c1212 | 656 | case kDragTrackingLeaveWindow: |
e22ab33a DS |
657 | if (trackingGlobals == NULL) |
658 | break; | |
659 | ||
6eae1f7d | 660 | if (trackingGlobals->m_currentTarget) |
a07c1212 | 661 | { |
876b960a DS |
662 | trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); |
663 | trackingGlobals->m_currentTarget->OnLeave(); | |
664 | HideDragHilite( theDrag ); | |
665 | trackingGlobals->m_currentTarget = NULL; | |
a07c1212 | 666 | } |
876b960a | 667 | trackingGlobals->m_currentTargetWindow = NULL; |
a07c1212 | 668 | break; |
6eae1f7d DS |
669 | |
670 | default: | |
671 | break; | |
a07c1212 | 672 | } |
6eae1f7d DS |
673 | |
674 | return noErr; | |
a07c1212 SC |
675 | } |
676 | ||
6eae1f7d DS |
677 | pascal OSErr wxMacWindowDragReceiveHandler( |
678 | WindowPtr theWindow, | |
679 | void *handlerRefCon, | |
680 | DragReference theDrag) | |
681 | { | |
682 | MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*)handlerRefCon; | |
a07c1212 SC |
683 | if ( trackingGlobals->m_currentTarget ) |
684 | { | |
51d4293d DS |
685 | Point mouse, localMouse; |
686 | int localx, localy; | |
6eae1f7d | 687 | |
51d4293d DS |
688 | trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ); |
689 | GetDragMouse( theDrag, &mouse, 0L ); | |
a07c1212 | 690 | localMouse = mouse; |
51d4293d DS |
691 | GlobalToLocal( &localMouse ); |
692 | localx = localMouse.h; | |
693 | localy = localMouse.v; | |
6eae1f7d DS |
694 | |
695 | // TODO : should we use client coordinates? | |
a07c1212 | 696 | if ( trackingGlobals->m_currentTargetWindow ) |
51d4293d DS |
697 | trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx, &localy ); |
698 | if ( trackingGlobals->m_currentTarget->OnDrop( localx, localy ) ) | |
a07c1212 | 699 | { |
51d4293d | 700 | bool optionDown = GetCurrentKeyModifiers() & optionKey; |
f43084de | 701 | wxDragResult result = optionDown ? wxDragCopy : wxDragMove; |
51d4293d | 702 | trackingGlobals->m_currentTarget->OnData( localx, localy, result ); |
a07c1212 SC |
703 | } |
704 | } | |
6eae1f7d DS |
705 | |
706 | return noErr; | |
a07c1212 | 707 | } |
6eae1f7d | 708 | |
03e11df5 | 709 | #endif |