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