]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dnd.cpp
bug fixes
[wxWidgets.git] / src / mac / carbon / 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 {
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 }
203 theData = new char[dataSize];
204 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
205 if( theType == 'TEXT' )
206 {
207 theData[dataSize]=0 ;
208 if ( wxApp::s_macDefaultEncodingIsPC )
209 {
210 wxMacConvertToPC((char*)theData,(char*)theData,dataSize) ;
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 ) ;
218 ((wxFileDataObject*)m_dataObject)->AddFile( name ) ;
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 ;
232 }
233
234 //-------------------------------------------------------------------------
235 // wxDropSource
236 //-------------------------------------------------------------------------
237
238 //-----------------------------------------------------------------------------
239 // drag request
240
241 wxDropSource::wxDropSource(wxWindow *win,
242 const wxIcon &iconCopy,
243 const wxIcon &iconMove,
244 const wxIcon &iconNone)
245 {
246 wxMacEnsureTrackingHandlersInstalled() ;
247 m_window = win;
248 }
249
250 wxDropSource::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 }
260
261 wxDropSource::~wxDropSource()
262 {
263 }
264
265
266 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
267 {
268 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
269
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;
279 if ((result = NewDrag(&theDrag)))
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 ;
288 for ( size_t i = 0 ; i < formatCount ; ++i )
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 {
299 wxMacConvertFromPC((char*)dataPtr,(char*)dataPtr,dataSize) ;
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
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
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
379 bool 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
384 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
385 void *handlerRefCon, DragReference theDrag) ;
386 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
387 DragReference theDrag) ;
388
389 void wxMacEnsureTrackingHandlersInstalled()
390 {
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 ) ;
399
400 gTrackingGlobalsInstalled = true ;
401 }
402 }
403
404 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
405 void *handlerRefCon, DragReference theDrag)
406 {
407 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
408 Point mouse, localMouse;
409 DragAttributes attributes;
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
504 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
505 void *handlerRefCon,
506 DragReference theDrag)
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 }
530 #endif