]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dnd.cpp
code changed along msw lines
[wxWidgets.git] / src / mac / dnd.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: dnd.cpp
3// Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 AUTHOR
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "dnd.h"
14#endif
15
ff143598
GD
16#include "wx/defs.h"
17
18#if wxUSE_DRAG_AND_DROP
19
e9576ca5
SC
20#include "wx/dnd.h"
21#include "wx/window.h"
ff143598 22#include "wx/toplevel.h"
e9576ca5
SC
23#include "wx/app.h"
24#include "wx/gdicmn.h"
76a5e5d2 25#include "wx/mac/private.h"
e9576ca5
SC
26
27// ----------------------------------------------------------------------------
28// global
29// ----------------------------------------------------------------------------
30
a07c1212 31void wxMacEnsureTrackingHandlersInstalled() ;
e9576ca5 32
a07c1212 33typedef struct
e9576ca5 34{
a07c1212
SC
35 wxWindow* m_currentTargetWindow ;
36 wxDropTarget* m_currentTarget ;
37 wxDropSource* m_currentSource ;
38} MacTrackingGlobals ;
e9576ca5 39
a07c1212 40MacTrackingGlobals gTrackingGlobals ;
e9576ca5 41
a07c1212
SC
42//----------------------------------------------------------------------------
43// wxDropTarget
44//----------------------------------------------------------------------------
e9576ca5 45
a07c1212
SC
46wxDropTarget::wxDropTarget( wxDataObject *data )
47 : wxDropTargetBase( data )
e9576ca5 48{
a07c1212 49 wxMacEnsureTrackingHandlersInstalled() ;
e9576ca5
SC
50}
51
a07c1212
SC
52wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
53 wxCoord WXUNUSED(y),
54 wxDragResult def )
e9576ca5 55{
e9576ca5 56
a07c1212
SC
57 return CurrentDragHasSupportedFormat() ? def : wxDragNone;
58}
e9576ca5 59
a07c1212 60bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
e9576ca5 61{
a07c1212
SC
62 if (!m_dataObject)
63 return FALSE;
64
65 return CurrentDragHasSupportedFormat() ;
e9576ca5
SC
66}
67
a07c1212
SC
68wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
69 wxDragResult def )
e9576ca5 70{
a07c1212
SC
71 if (!m_dataObject)
72 return wxDragNone;
73
74 if (!CurrentDragHasSupportedFormat())
75 return wxDragNone;
76
77 return GetData() ? def : wxDragNone;
e9576ca5
SC
78}
79
a07c1212 80bool wxDropTarget::CurrentDragHasSupportedFormat()
e9576ca5 81{
a07c1212
SC
82 bool supported = false ;
83 if ( gTrackingGlobals.m_currentSource != NULL )
84 {
85 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
86
87 if ( data )
88 {
0cd51b2d 89 size_t formatcount = data->GetFormatCount() ;
a07c1212
SC
90 wxDataFormat *array = new wxDataFormat[ formatcount ];
91 data->GetAllFormats( array );
92 for (size_t i = 0; !supported && i < formatcount ; i++)
93 {
94 wxDataFormat format = array[i] ;
95 if ( m_dataObject->IsSupported( format ) )
96 {
97 supported = true ;
98 break ;
99 }
100 }
101 delete[] array ;
102 }
103 }
104 if ( !supported )
105 {
106 UInt16 items ;
107 OSErr result;
76a5e5d2 108 CountDragItems((DragReference)m_currentDrag, &items);
a07c1212
SC
109 for (UInt16 index = 1; index <= items && supported == false ; ++index)
110 {
111 ItemReference theItem;
112 FlavorType theType ;
113 UInt16 flavors = 0 ;
76a5e5d2
SC
114 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
115 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
a07c1212
SC
116 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
117 {
76a5e5d2 118 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
a07c1212
SC
119 if ( m_dataObject->IsSupportedFormat( wxDataFormat( theType ) ) )
120 {
121 supported = true ;
122 break ;
123 }
124 }
125 }
126 }
127 return supported ;
e9576ca5
SC
128}
129
a07c1212 130bool wxDropTarget::GetData()
e9576ca5 131{
a07c1212
SC
132 if (!m_dataObject)
133 return FALSE;
134
135 if ( !CurrentDragHasSupportedFormat() )
136 return FALSE ;
137
138 bool transferred = false ;
139 if ( gTrackingGlobals.m_currentSource != NULL )
140 {
141 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
142
143 if ( data )
144 {
0cd51b2d 145 size_t formatcount = data->GetFormatCount() ;
a07c1212
SC
146 wxDataFormat *array = new wxDataFormat[ formatcount ];
147 data->GetAllFormats( array );
148 for (size_t i = 0; !transferred && i < formatcount ; i++)
149 {
150 wxDataFormat format = array[i] ;
151 if ( m_dataObject->IsSupported( format ) )
152 {
153 int size = data->GetDataSize( format );
154 transferred = true ;
155
156 if (size == 0)
157 {
158 m_dataObject->SetData(format , 0 , 0 ) ;
159 }
160 else
161 {
162 char *d = new char[size];
163 data->GetDataHere( format , (void*) d );
164 m_dataObject->SetData( format , size , d ) ;
165 delete[] d ;
166 }
167 }
168 }
169 delete[] array ;
170 }
171 }
172 if ( !transferred )
173 {
174 UInt16 items ;
175 OSErr result;
76a5e5d2 176 CountDragItems((DragReference)m_currentDrag, &items);
a07c1212
SC
177 for (UInt16 index = 1; index <= items; ++index)
178 {
179 ItemReference theItem;
180 FlavorType theType ;
181 UInt16 flavors = 0 ;
76a5e5d2
SC
182 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
183 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
a07c1212
SC
184 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
185 {
76a5e5d2 186 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
a07c1212
SC
187 wxDataFormat format(theType) ;
188 if ( m_dataObject->IsSupportedFormat( format ) )
189 {
190 FlavorFlags theFlags;
76a5e5d2 191 result = GetFlavorFlags((DragReference)m_currentDrag, theItem, theType, &theFlags);
a07c1212
SC
192 if (result == noErr)
193 {
194 Size dataSize ;
195 Ptr theData ;
76a5e5d2 196 GetFlavorDataSize((DragReference)m_currentDrag, theItem, theType, &dataSize);
45a0fd37
SC
197 if ( theType == 'TEXT' )
198 {
199 // this increment is only valid for allocating, on the next GetFlavorData
200 // call it is reset again to the original value
201 dataSize++ ;
202 }
a07c1212 203 theData = new char[dataSize];
76a5e5d2 204 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
a07c1212
SC
205 if( theType == 'TEXT' )
206 {
207 theData[dataSize]=0 ;
208 if ( wxApp::s_macDefaultEncodingIsPC )
209 {
45a0fd37 210 wxMacConvertToPC((char*)theData,(char*)theData,dataSize) ;
a07c1212
SC
211 }
212 m_dataObject->SetData( format, dataSize, theData );
213 }
214 else if ( theType == kDragFlavorTypeHFS )
215 {
216 HFSFlavor* theFile = (HFSFlavor*) theData ;
217 wxString name = wxMacFSSpec2MacFilename( &theFile->fileSpec ) ;
3538f4df 218 ((wxFileDataObject*)m_dataObject)->AddFile( name ) ;
a07c1212
SC
219 }
220 else
221 {
222 m_dataObject->SetData( format, dataSize, theData );
223 }
224 delete[] theData;
225 }
226 break ;
227 }
228 }
229 }
230 }
231 return TRUE ;
e9576ca5
SC
232}
233
234//-------------------------------------------------------------------------
235// wxDropSource
236//-------------------------------------------------------------------------
237
238//-----------------------------------------------------------------------------
239// drag request
240
a07c1212
SC
241wxDropSource::wxDropSource(wxWindow *win,
242 const wxIcon &iconCopy,
243 const wxIcon &iconMove,
244 const wxIcon &iconNone)
e9576ca5 245{
a07c1212
SC
246 wxMacEnsureTrackingHandlersInstalled() ;
247 m_window = win;
248}
e9576ca5 249
a07c1212
SC
250wxDropSource::wxDropSource(wxDataObject& data,
251 wxWindow *win,
252 const wxIcon &iconCopy,
253 const wxIcon &iconMove,
254 const wxIcon &iconNone)
255{
256 wxMacEnsureTrackingHandlersInstalled() ;
257 SetData( data );
258 m_window = win;
259}
e9576ca5 260
a07c1212 261wxDropSource::~wxDropSource()
e9576ca5 262{
a07c1212 263}
e9576ca5 264
e9576ca5 265
2245b2b2 266wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
e9576ca5 267{
a07c1212 268 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
e9576ca5 269
a07c1212
SC
270 if (!m_data)
271 return (wxDragResult) wxDragNone;
272
273 if (m_data->GetFormatCount() == 0)
274 return (wxDragResult) wxDragNone;
275
276 OSErr result;
277 DragReference theDrag;
278 RgnHandle dragRegion;
0cd51b2d 279 if ((result = NewDrag(&theDrag)))
a07c1212
SC
280 {
281 return wxDragNone ;
282 }
283 // add data to drag
284 size_t formatCount = m_data->GetFormatCount() ;
285 wxDataFormat *formats = new wxDataFormat[formatCount] ;
286 m_data->GetAllFormats( formats ) ;
287 ItemReference theItem = 1 ;
0cd51b2d 288 for ( size_t i = 0 ; i < formatCount ; ++i )
a07c1212
SC
289 {
290 size_t dataSize = m_data->GetDataSize( formats[i] ) ;
291 Ptr dataPtr = new char[dataSize] ;
292 m_data->GetDataHere( formats[i] , dataPtr ) ;
293 OSType type = formats[i].GetFormatId() ;
294 if ( type == 'TEXT' )
295 {
296 dataSize-- ;
297 if ( wxApp::s_macDefaultEncodingIsPC )
298 {
45a0fd37 299 wxMacConvertFromPC((char*)dataPtr,(char*)dataPtr,dataSize) ;
a07c1212
SC
300 }
301 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
302 }
303 else if (type == kDragFlavorTypeHFS )
304 {
305 HFSFlavor theFlavor ;
306 OSErr err = noErr;
307 CInfoPBRec cat;
308
309 wxMacFilename2FSSpec( dataPtr , &theFlavor.fileSpec ) ;
310
311 cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name;
312 cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum;
313 cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID;
314 cat.hFileInfo.ioFDirIndex = 0;
315 err = PBGetCatInfoSync(&cat);
316 if (err == noErr )
317 {
318 theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags;
319 if (theFlavor.fileSpec.parID == fsRtParID) {
320 theFlavor.fileCreator = 'MACS';
321 theFlavor.fileType = 'disk';
322 } else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) {
323 theFlavor.fileCreator = 'MACS';
324 theFlavor.fileType = 'fold';
325 } else {
326 theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
327 theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType;
328 }
329 AddDragItemFlavor(theDrag, theItem, type , &theFlavor, sizeof(theFlavor), 0);
330 }
331 }
332 else
333 {
334 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
335 }
336 delete[] dataPtr ;
337 }
338 delete[] formats ;
339
340 dragRegion = NewRgn();
341 RgnHandle tempRgn = NewRgn() ;
342
45a0fd37
SC
343 EventRecord* ev = NULL ;
344#if !TARGET_CARBON // TODO
345 ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
346#else
347 EventRecord rec ;
348 ev = &rec ;
349 wxMacConvertEventToRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
350#endif
a07c1212
SC
351 const short dragRegionOuterBoundary = 10 ;
352 const short dragRegionInnerBoundary = 9 ;
353
354 SetRectRgn( dragRegion , ev->where.h - dragRegionOuterBoundary ,
355 ev->where.v - dragRegionOuterBoundary ,
356 ev->where.h + dragRegionOuterBoundary ,
357 ev->where.v + dragRegionOuterBoundary ) ;
358
359 SetRectRgn( tempRgn , ev->where.h - dragRegionInnerBoundary ,
360 ev->where.v - dragRegionInnerBoundary ,
361 ev->where.h + dragRegionInnerBoundary ,
362 ev->where.v + dragRegionInnerBoundary ) ;
363
364 DiffRgn( dragRegion , tempRgn , dragRegion ) ;
365 DisposeRgn( tempRgn ) ;
366
367 // TODO:work with promises in order to return data only when drag
368 // was successfully completed
369
370 gTrackingGlobals.m_currentSource = this ;
371 result = TrackDrag(theDrag, ev , dragRegion);
372 DisposeRgn(dragRegion);
373 DisposeDrag(theDrag);
374 gTrackingGlobals.m_currentSource = NULL ;
375
376 return wxDragCopy ;
377}
378
379bool gTrackingGlobalsInstalled = false ;
380
381// passing the globals via refcon is not needed by the CFM and later architectures anymore
382// but I'll leave it in there, just in case...
383
384pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
385 void *handlerRefCon, DragReference theDrag) ;
386pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
387DragReference theDrag) ;
388
389void wxMacEnsureTrackingHandlersInstalled()
e9576ca5 390{
a07c1212
SC
391 if( !gTrackingGlobalsInstalled )
392 {
393 OSErr result;
394
395 result = InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L,&gTrackingGlobals);
396 wxASSERT( result == noErr ) ;
397 result = InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals);
398 wxASSERT( result == noErr ) ;
e9576ca5 399
a07c1212
SC
400 gTrackingGlobalsInstalled = true ;
401 }
402}
403
404pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
405 void *handlerRefCon, DragReference theDrag)
406{
407 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
408 Point mouse, localMouse;
409 DragAttributes attributes;
a07c1212
SC
410 GetDragAttributes(theDrag, &attributes);
411 wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ) ;
412 switch(theMessage)
413 {
414 case kDragTrackingEnterHandler:
415 break;
416 case kDragTrackingLeaveHandler:
417 break;
418 case kDragTrackingEnterWindow:
419 trackingGlobals->m_currentTargetWindow = NULL ;
420 trackingGlobals->m_currentTarget = NULL ;
421 break;
422 case kDragTrackingInWindow:
423 if (toplevel == NULL)
424 break;
425
426 GetDragMouse(theDrag, &mouse, 0L);
427 localMouse = mouse;
428 GlobalToLocal(&localMouse);
429
430// if (attributes & kDragHasLeftSenderWindow)
431 {
432 wxPoint point(localMouse.h , localMouse.v) ;
433 wxWindow *win = NULL ;
434 toplevel->MacGetWindowFromPointSub( point , &win ) ;
435 int localx , localy ;
436 localx = localMouse.h ;
437 localy = localMouse.v ;
438 //TODO : should we use client coordinates
439 if ( win )
440 win->MacRootWindowToWindow( &localx , &localy ) ;
441 if ( win != trackingGlobals->m_currentTargetWindow )
442 {
443 if ( trackingGlobals->m_currentTargetWindow )
444 {
445 // this window is left
446 if ( trackingGlobals->m_currentTarget )
447 {
448 HideDragHilite(theDrag);
449 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
450 trackingGlobals->m_currentTarget->OnLeave() ;
451 trackingGlobals->m_currentTarget = NULL;
452 trackingGlobals->m_currentTargetWindow = NULL ;
453 }
454 }
455 if ( win )
456 {
457 // this window is entered
458 trackingGlobals->m_currentTargetWindow = win ;
459 trackingGlobals->m_currentTarget = win->GetDropTarget() ;
460 if ( trackingGlobals->m_currentTarget )
461 {
462 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
463 if ( trackingGlobals->m_currentTarget->OnEnter(
464 localx , localy , wxDragCopy ) != wxDragNone )
465 {
466 int x , y ;
467 x = y = 0 ;
468 win->MacWindowToRootWindow( &x , &y ) ;
469 RgnHandle hiliteRgn = NewRgn() ;
470 SetRectRgn( hiliteRgn , x , y , x+win->GetSize().x ,y+win->GetSize().y) ;
471 ShowDragHilite(theDrag, hiliteRgn, true);
472 DisposeRgn( hiliteRgn ) ;
473 }
474 }
475 }
476 }
477 else
478 {
479 if( trackingGlobals->m_currentTarget )
480 {
481 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
482 trackingGlobals->m_currentTarget->OnDragOver(
483 localx , localy , wxDragCopy ) ;
484 }
485 }
486
487 }
488 // MyTrackItemUnderMouse(localMouse, theWindow);
489 break;
490 case kDragTrackingLeaveWindow:
491 if (trackingGlobals->m_currentTarget)
492 {
493 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
494 trackingGlobals->m_currentTarget->OnLeave() ;
495 HideDragHilite(theDrag);
496 trackingGlobals->m_currentTarget = NULL ;
497 }
498 trackingGlobals->m_currentTargetWindow = NULL ;
499 break;
500 }
501 return(noErr);
502}
503
f2af4afb
GD
504pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
505 void *handlerRefCon,
506 DragReference theDrag)
a07c1212
SC
507{
508 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
509 if ( trackingGlobals->m_currentTarget )
510 {
511 Point mouse,localMouse ;
512 int localx,localy ;
513
514 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
515 GetDragMouse(theDrag, &mouse, 0L);
516 localMouse = mouse;
517 GlobalToLocal(&localMouse);
518 localx = localMouse.h ;
519 localy = localMouse.v ;
520 //TODO : should we use client coordinates
521 if ( trackingGlobals->m_currentTargetWindow )
522 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
523 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
524 {
525 trackingGlobals->m_currentTarget->OnData( localx , localy , wxDragCopy ) ;
526 }
527 }
528 return(noErr);
529}
03e11df5 530#endif