]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dnd.cpp
#ifed wxLog occurrences
[wxWidgets.git] / src / mac / carbon / 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;
e56aba8d 176 bool firstFileAdded = false ;
76a5e5d2 177 CountDragItems((DragReference)m_currentDrag, &items);
a07c1212
SC
178 for (UInt16 index = 1; index <= items; ++index)
179 {
180 ItemReference theItem;
181 FlavorType theType ;
182 UInt16 flavors = 0 ;
76a5e5d2
SC
183 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
184 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
a07c1212
SC
185 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
186 {
76a5e5d2 187 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
a07c1212
SC
188 wxDataFormat format(theType) ;
189 if ( m_dataObject->IsSupportedFormat( format ) )
190 {
191 FlavorFlags theFlags;
76a5e5d2 192 result = GetFlavorFlags((DragReference)m_currentDrag, theItem, theType, &theFlags);
a07c1212
SC
193 if (result == noErr)
194 {
195 Size dataSize ;
196 Ptr theData ;
76a5e5d2 197 GetFlavorDataSize((DragReference)m_currentDrag, theItem, theType, &dataSize);
45a0fd37
SC
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 }
a07c1212 204 theData = new char[dataSize];
76a5e5d2 205 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
a07c1212
SC
206 if( theType == 'TEXT' )
207 {
208 theData[dataSize]=0 ;
209 if ( wxApp::s_macDefaultEncodingIsPC )
210 {
45a0fd37 211 wxMacConvertToPC((char*)theData,(char*)theData,dataSize) ;
a07c1212
SC
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 ) ;
e56aba8d
SC
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 }
a07c1212
SC
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 ;
e9576ca5
SC
239}
240
241//-------------------------------------------------------------------------
242// wxDropSource
243//-------------------------------------------------------------------------
244
245//-----------------------------------------------------------------------------
246// drag request
247
a07c1212
SC
248wxDropSource::wxDropSource(wxWindow *win,
249 const wxIcon &iconCopy,
250 const wxIcon &iconMove,
251 const wxIcon &iconNone)
e9576ca5 252{
a07c1212
SC
253 wxMacEnsureTrackingHandlersInstalled() ;
254 m_window = win;
255}
e9576ca5 256
a07c1212
SC
257wxDropSource::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}
e9576ca5 267
a07c1212 268wxDropSource::~wxDropSource()
e9576ca5 269{
a07c1212 270}
e9576ca5 271
e9576ca5 272
2245b2b2 273wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
e9576ca5 274{
a07c1212 275 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
e9576ca5 276
a07c1212
SC
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;
0cd51b2d 286 if ((result = NewDrag(&theDrag)))
a07c1212
SC
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 ;
0cd51b2d 295 for ( size_t i = 0 ; i < formatCount ; ++i )
a07c1212
SC
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 {
45a0fd37 306 wxMacConvertFromPC((char*)dataPtr,(char*)dataPtr,dataSize) ;
a07c1212
SC
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
45a0fd37
SC
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
a07c1212
SC
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
386bool 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
391pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
392 void *handlerRefCon, DragReference theDrag) ;
393pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
394DragReference theDrag) ;
395
396void wxMacEnsureTrackingHandlersInstalled()
e9576ca5 397{
a07c1212
SC
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 ) ;
e9576ca5 406
a07c1212
SC
407 gTrackingGlobalsInstalled = true ;
408 }
409}
410
411pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
412 void *handlerRefCon, DragReference theDrag)
413{
414 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
415 Point mouse, localMouse;
416 DragAttributes attributes;
a07c1212
SC
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
f2af4afb
GD
511pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
512 void *handlerRefCon,
513 DragReference theDrag)
a07c1212
SC
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}
03e11df5 537#endif