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