]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dnd.cpp
corrected getting FSRef associated with the POSIX path in wxString
[wxWidgets.git] / src / mac / dnd.cpp
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
16 #include "wx/defs.h"
17
18 #if wxUSE_DRAG_AND_DROP
19
20 #include "wx/dnd.h"
21 #include "wx/window.h"
22 #include "wx/toplevel.h"
23 #include "wx/app.h"
24 #include "wx/gdicmn.h"
25 #include "wx/mac/private.h"
26
27 // ----------------------------------------------------------------------------
28 // global
29 // ----------------------------------------------------------------------------
30
31 void wxMacEnsureTrackingHandlersInstalled() ;
32
33 typedef struct
34 {
35 wxWindow* m_currentTargetWindow ;
36 wxDropTarget* m_currentTarget ;
37 wxDropSource* m_currentSource ;
38 } MacTrackingGlobals ;
39
40 MacTrackingGlobals gTrackingGlobals ;
41
42 //----------------------------------------------------------------------------
43 // wxDropTarget
44 //----------------------------------------------------------------------------
45
46 wxDropTarget::wxDropTarget( wxDataObject *data )
47 : wxDropTargetBase( data )
48 {
49 wxMacEnsureTrackingHandlersInstalled() ;
50 }
51
52 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
53 wxCoord WXUNUSED(y),
54 wxDragResult def )
55 {
56
57 return CurrentDragHasSupportedFormat() ? def : wxDragNone;
58 }
59
60 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
61 {
62 if (!m_dataObject)
63 return FALSE;
64
65 return CurrentDragHasSupportedFormat() ;
66 }
67
68 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
69 wxDragResult def )
70 {
71 if (!m_dataObject)
72 return wxDragNone;
73
74 if (!CurrentDragHasSupportedFormat())
75 return wxDragNone;
76
77 return GetData() ? def : wxDragNone;
78 }
79
80 bool wxDropTarget::CurrentDragHasSupportedFormat()
81 {
82 bool supported = false ;
83 if ( gTrackingGlobals.m_currentSource != NULL )
84 {
85 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
86
87 if ( data )
88 {
89 size_t formatcount = data->GetFormatCount() ;
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;
108 CountDragItems((DragReference)m_currentDrag, &items);
109 for (UInt16 index = 1; index <= items && supported == false ; ++index)
110 {
111 ItemReference theItem;
112 FlavorType theType ;
113 UInt16 flavors = 0 ;
114 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
115 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
116 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
117 {
118 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
119 if ( m_dataObject->IsSupportedFormat( wxDataFormat( theType ) ) )
120 {
121 supported = true ;
122 break ;
123 }
124 }
125 }
126 }
127 return supported ;
128 }
129
130 bool wxDropTarget::GetData()
131 {
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 {
145 size_t formatcount = data->GetFormatCount() ;
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;
176 CountDragItems((DragReference)m_currentDrag, &items);
177 for (UInt16 index = 1; index <= items; ++index)
178 {
179 ItemReference theItem;
180 FlavorType theType ;
181 UInt16 flavors = 0 ;
182 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
183 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
184 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
185 {
186 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
187 wxDataFormat format(theType) ;
188 if ( m_dataObject->IsSupportedFormat( format ) )
189 {
190 FlavorFlags theFlags;
191 result = GetFlavorFlags((DragReference)m_currentDrag, theItem, theType, &theFlags);
192 if (result == noErr)
193 {
194 Size dataSize ;
195 Ptr theData ;
196 GetFlavorDataSize((DragReference)m_currentDrag, theItem, theType, &dataSize);
197 if ( theType == 'TEXT' )
198 dataSize++ ;
199 theData = new char[dataSize];
200 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
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 ) ;
214 ((wxFileDataObject*)m_dataObject)->AddFile( name ) ;
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 ;
228 }
229
230 //-------------------------------------------------------------------------
231 // wxDropSource
232 //-------------------------------------------------------------------------
233
234 //-----------------------------------------------------------------------------
235 // drag request
236
237 wxDropSource::wxDropSource(wxWindow *win,
238 const wxIcon &iconCopy,
239 const wxIcon &iconMove,
240 const wxIcon &iconNone)
241 {
242 wxMacEnsureTrackingHandlersInstalled() ;
243 m_window = win;
244 }
245
246 wxDropSource::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 }
256
257 wxDropSource::~wxDropSource()
258 {
259 }
260
261
262 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
263 {
264 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
265
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;
275 if ((result = NewDrag(&theDrag)))
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 ;
284 for ( size_t i = 0 ; i < formatCount ; ++i )
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
339 EventRecord* ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
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
368 bool 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
373 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
374 void *handlerRefCon, DragReference theDrag) ;
375 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
376 DragReference theDrag) ;
377
378 void wxMacEnsureTrackingHandlersInstalled()
379 {
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 ) ;
388
389 gTrackingGlobalsInstalled = true ;
390 }
391 }
392
393 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
394 void *handlerRefCon, DragReference theDrag)
395 {
396 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
397 Point mouse, localMouse;
398 DragAttributes attributes;
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
493 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
494 void *handlerRefCon,
495 DragReference theDrag)
496 {
497 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
498 if ( trackingGlobals->m_currentTarget )
499 {
500 Point mouse,localMouse ;
501 int localx,localy ;
502
503 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
504 GetDragMouse(theDrag, &mouse, 0L);
505 localMouse = mouse;
506 GlobalToLocal(&localMouse);
507 localx = localMouse.h ;
508 localy = localMouse.v ;
509 //TODO : should we use client coordinates
510 if ( trackingGlobals->m_currentTargetWindow )
511 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
512 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
513 {
514 trackingGlobals->m_currentTarget->OnData( localx , localy , wxDragCopy ) ;
515 }
516 }
517 return(noErr);
518 }
519 #endif