]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dnd.cpp
fixed outdated filenames
[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);
a07c1212
SC
197 if ( theType == 'TEXT' )
198 dataSize++ ;
199 theData = new char[dataSize];
76a5e5d2 200 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
a07c1212
SC
201 if( theType == 'TEXT' )
202 {
203 theData[dataSize]=0 ;
204 if ( wxApp::s_macDefaultEncodingIsPC )
205 {
206 wxMacConvertToPC((char*)theData) ;
207 }
208 m_dataObject->SetData( format, dataSize, theData );
209 }
210 else if ( theType == kDragFlavorTypeHFS )
211 {
212 HFSFlavor* theFile = (HFSFlavor*) theData ;
213 wxString name = wxMacFSSpec2MacFilename( &theFile->fileSpec ) ;
3538f4df 214 ((wxFileDataObject*)m_dataObject)->AddFile( name ) ;
a07c1212
SC
215 }
216 else
217 {
218 m_dataObject->SetData( format, dataSize, theData );
219 }
220 delete[] theData;
221 }
222 break ;
223 }
224 }
225 }
226 }
227 return TRUE ;
e9576ca5
SC
228}
229
230//-------------------------------------------------------------------------
231// wxDropSource
232//-------------------------------------------------------------------------
233
234//-----------------------------------------------------------------------------
235// drag request
236
a07c1212
SC
237wxDropSource::wxDropSource(wxWindow *win,
238 const wxIcon &iconCopy,
239 const wxIcon &iconMove,
240 const wxIcon &iconNone)
e9576ca5 241{
a07c1212
SC
242 wxMacEnsureTrackingHandlersInstalled() ;
243 m_window = win;
244}
e9576ca5 245
a07c1212
SC
246wxDropSource::wxDropSource(wxDataObject& data,
247 wxWindow *win,
248 const wxIcon &iconCopy,
249 const wxIcon &iconMove,
250 const wxIcon &iconNone)
251{
252 wxMacEnsureTrackingHandlersInstalled() ;
253 SetData( data );
254 m_window = win;
255}
e9576ca5 256
a07c1212 257wxDropSource::~wxDropSource()
e9576ca5 258{
a07c1212 259}
e9576ca5 260
e9576ca5 261
a07c1212 262wxDragResult wxDropSource::DoDragDrop( bool allowMove )
e9576ca5 263{
a07c1212 264 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
e9576ca5 265
a07c1212
SC
266 if (!m_data)
267 return (wxDragResult) wxDragNone;
268
269 if (m_data->GetFormatCount() == 0)
270 return (wxDragResult) wxDragNone;
271
272 OSErr result;
273 DragReference theDrag;
274 RgnHandle dragRegion;
0cd51b2d 275 if ((result = NewDrag(&theDrag)))
a07c1212
SC
276 {
277 return wxDragNone ;
278 }
279 // add data to drag
280 size_t formatCount = m_data->GetFormatCount() ;
281 wxDataFormat *formats = new wxDataFormat[formatCount] ;
282 m_data->GetAllFormats( formats ) ;
283 ItemReference theItem = 1 ;
0cd51b2d 284 for ( size_t i = 0 ; i < formatCount ; ++i )
a07c1212
SC
285 {
286 size_t dataSize = m_data->GetDataSize( formats[i] ) ;
287 Ptr dataPtr = new char[dataSize] ;
288 m_data->GetDataHere( formats[i] , dataPtr ) ;
289 OSType type = formats[i].GetFormatId() ;
290 if ( type == 'TEXT' )
291 {
292 dataSize-- ;
293 if ( wxApp::s_macDefaultEncodingIsPC )
294 {
295 wxMacConvertFromPC((char*)dataPtr) ;
296 }
297 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
298 }
299 else if (type == kDragFlavorTypeHFS )
300 {
301 HFSFlavor theFlavor ;
302 OSErr err = noErr;
303 CInfoPBRec cat;
304
305 wxMacFilename2FSSpec( dataPtr , &theFlavor.fileSpec ) ;
306
307 cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name;
308 cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum;
309 cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID;
310 cat.hFileInfo.ioFDirIndex = 0;
311 err = PBGetCatInfoSync(&cat);
312 if (err == noErr )
313 {
314 theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags;
315 if (theFlavor.fileSpec.parID == fsRtParID) {
316 theFlavor.fileCreator = 'MACS';
317 theFlavor.fileType = 'disk';
318 } else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) {
319 theFlavor.fileCreator = 'MACS';
320 theFlavor.fileType = 'fold';
321 } else {
322 theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
323 theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType;
324 }
325 AddDragItemFlavor(theDrag, theItem, type , &theFlavor, sizeof(theFlavor), 0);
326 }
327 }
328 else
329 {
330 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
331 }
332 delete[] dataPtr ;
333 }
334 delete[] formats ;
335
336 dragRegion = NewRgn();
337 RgnHandle tempRgn = NewRgn() ;
338
76a5e5d2 339 EventRecord* ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
a07c1212
SC
340 const short dragRegionOuterBoundary = 10 ;
341 const short dragRegionInnerBoundary = 9 ;
342
343 SetRectRgn( dragRegion , ev->where.h - dragRegionOuterBoundary ,
344 ev->where.v - dragRegionOuterBoundary ,
345 ev->where.h + dragRegionOuterBoundary ,
346 ev->where.v + dragRegionOuterBoundary ) ;
347
348 SetRectRgn( tempRgn , ev->where.h - dragRegionInnerBoundary ,
349 ev->where.v - dragRegionInnerBoundary ,
350 ev->where.h + dragRegionInnerBoundary ,
351 ev->where.v + dragRegionInnerBoundary ) ;
352
353 DiffRgn( dragRegion , tempRgn , dragRegion ) ;
354 DisposeRgn( tempRgn ) ;
355
356 // TODO:work with promises in order to return data only when drag
357 // was successfully completed
358
359 gTrackingGlobals.m_currentSource = this ;
360 result = TrackDrag(theDrag, ev , dragRegion);
361 DisposeRgn(dragRegion);
362 DisposeDrag(theDrag);
363 gTrackingGlobals.m_currentSource = NULL ;
364
365 return wxDragCopy ;
366}
367
368bool gTrackingGlobalsInstalled = false ;
369
370// passing the globals via refcon is not needed by the CFM and later architectures anymore
371// but I'll leave it in there, just in case...
372
373pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
374 void *handlerRefCon, DragReference theDrag) ;
375pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
376DragReference theDrag) ;
377
378void wxMacEnsureTrackingHandlersInstalled()
e9576ca5 379{
a07c1212
SC
380 if( !gTrackingGlobalsInstalled )
381 {
382 OSErr result;
383
384 result = InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L,&gTrackingGlobals);
385 wxASSERT( result == noErr ) ;
386 result = InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals);
387 wxASSERT( result == noErr ) ;
e9576ca5 388
a07c1212
SC
389 gTrackingGlobalsInstalled = true ;
390 }
391}
392
393pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
394 void *handlerRefCon, DragReference theDrag)
395{
396 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
397 Point mouse, localMouse;
398 DragAttributes attributes;
a07c1212
SC
399 GetDragAttributes(theDrag, &attributes);
400 wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ) ;
401 switch(theMessage)
402 {
403 case kDragTrackingEnterHandler:
404 break;
405 case kDragTrackingLeaveHandler:
406 break;
407 case kDragTrackingEnterWindow:
408 trackingGlobals->m_currentTargetWindow = NULL ;
409 trackingGlobals->m_currentTarget = NULL ;
410 break;
411 case kDragTrackingInWindow:
412 if (toplevel == NULL)
413 break;
414
415 GetDragMouse(theDrag, &mouse, 0L);
416 localMouse = mouse;
417 GlobalToLocal(&localMouse);
418
419// if (attributes & kDragHasLeftSenderWindow)
420 {
421 wxPoint point(localMouse.h , localMouse.v) ;
422 wxWindow *win = NULL ;
423 toplevel->MacGetWindowFromPointSub( point , &win ) ;
424 int localx , localy ;
425 localx = localMouse.h ;
426 localy = localMouse.v ;
427 //TODO : should we use client coordinates
428 if ( win )
429 win->MacRootWindowToWindow( &localx , &localy ) ;
430 if ( win != trackingGlobals->m_currentTargetWindow )
431 {
432 if ( trackingGlobals->m_currentTargetWindow )
433 {
434 // this window is left
435 if ( trackingGlobals->m_currentTarget )
436 {
437 HideDragHilite(theDrag);
438 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
439 trackingGlobals->m_currentTarget->OnLeave() ;
440 trackingGlobals->m_currentTarget = NULL;
441 trackingGlobals->m_currentTargetWindow = NULL ;
442 }
443 }
444 if ( win )
445 {
446 // this window is entered
447 trackingGlobals->m_currentTargetWindow = win ;
448 trackingGlobals->m_currentTarget = win->GetDropTarget() ;
449 if ( trackingGlobals->m_currentTarget )
450 {
451 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
452 if ( trackingGlobals->m_currentTarget->OnEnter(
453 localx , localy , wxDragCopy ) != wxDragNone )
454 {
455 int x , y ;
456 x = y = 0 ;
457 win->MacWindowToRootWindow( &x , &y ) ;
458 RgnHandle hiliteRgn = NewRgn() ;
459 SetRectRgn( hiliteRgn , x , y , x+win->GetSize().x ,y+win->GetSize().y) ;
460 ShowDragHilite(theDrag, hiliteRgn, true);
461 DisposeRgn( hiliteRgn ) ;
462 }
463 }
464 }
465 }
466 else
467 {
468 if( trackingGlobals->m_currentTarget )
469 {
470 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
471 trackingGlobals->m_currentTarget->OnDragOver(
472 localx , localy , wxDragCopy ) ;
473 }
474 }
475
476 }
477 // MyTrackItemUnderMouse(localMouse, theWindow);
478 break;
479 case kDragTrackingLeaveWindow:
480 if (trackingGlobals->m_currentTarget)
481 {
482 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
483 trackingGlobals->m_currentTarget->OnLeave() ;
484 HideDragHilite(theDrag);
485 trackingGlobals->m_currentTarget = NULL ;
486 }
487 trackingGlobals->m_currentTargetWindow = NULL ;
488 break;
489 }
490 return(noErr);
491}
492
493pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
494DragReference theDrag)
495{
496 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
497 if ( trackingGlobals->m_currentTarget )
498 {
499 Point mouse,localMouse ;
500 int localx,localy ;
501
502 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
503 GetDragMouse(theDrag, &mouse, 0L);
504 localMouse = mouse;
505 GlobalToLocal(&localMouse);
506 localx = localMouse.h ;
507 localy = localMouse.v ;
508 //TODO : should we use client coordinates
509 if ( trackingGlobals->m_currentTargetWindow )
510 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
511 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
512 {
513 trackingGlobals->m_currentTarget->OnData( localx , localy , wxDragCopy ) ;
514 }
515 }
516 return(noErr);
517}
03e11df5 518#endif