]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dnd.cpp
regenerated the makefiles with the latest filelist.txt
[wxWidgets.git] / src / mac / carbon / dnd.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: dnd.cpp
3// Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) 1998 Stefan Csomor
e9576ca5
SC
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 {
e40298d5
JS
85 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
86
87 if ( data )
a07c1212 88 {
e40298d5
JS
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++)
a07c1212 93 {
e40298d5
JS
94 wxDataFormat format = array[i] ;
95 if ( m_dataObject->IsSupported( format ) )
96 {
97 supported = true ;
98 break ;
99 }
a07c1212 100 }
e40298d5 101 delete[] array ;
a07c1212 102 }
a07c1212
SC
103 }
104 if ( !supported )
105 {
e40298d5
JS
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 )
a07c1212 117 {
e40298d5
JS
118 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
119 if ( m_dataObject->IsSupportedFormat( wxDataFormat( theType ) ) )
120 {
121 supported = true ;
122 break ;
123 }
a07c1212 124 }
e40298d5 125 }
a07c1212
SC
126 }
127 return supported ;
e9576ca5
SC
128}
129
a07c1212 130bool wxDropTarget::GetData()
e9576ca5 131{
a07c1212
SC
132 if (!m_dataObject)
133 return FALSE;
e40298d5 134
a07c1212
SC
135 if ( !CurrentDragHasSupportedFormat() )
136 return FALSE ;
e40298d5 137
a07c1212
SC
138 bool transferred = false ;
139 if ( gTrackingGlobals.m_currentSource != NULL )
140 {
e40298d5
JS
141 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
142
143 if ( data )
a07c1212 144 {
e40298d5
JS
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++)
a07c1212 149 {
e40298d5
JS
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 }
a07c1212 168 }
e40298d5
JS
169 delete[] array ;
170 }
a07c1212
SC
171 }
172 if ( !transferred )
173 {
e40298d5
JS
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 )
a07c1212 186 {
e40298d5
JS
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)
a07c1212 194 {
e40298d5
JS
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;
a07c1212 232 }
e40298d5
JS
233 break ;
234 }
a07c1212 235 }
e40298d5 236 }
a07c1212
SC
237 }
238 return TRUE ;
e9576ca5
SC
239}
240
241//-------------------------------------------------------------------------
242// wxDropSource
243//-------------------------------------------------------------------------
244
245//-----------------------------------------------------------------------------
246// drag request
247
a07c1212 248wxDropSource::wxDropSource(wxWindow *win,
da804130
SC
249 const wxCursor &cursorCopy,
250 const wxCursor &cursorMove,
251 const wxCursor &cursorStop)
252 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
e9576ca5 253{
a07c1212
SC
254 wxMacEnsureTrackingHandlersInstalled() ;
255 m_window = win;
256}
e9576ca5 257
a07c1212
SC
258wxDropSource::wxDropSource(wxDataObject& data,
259 wxWindow *win,
da804130
SC
260 const wxCursor &cursorCopy,
261 const wxCursor &cursorMove,
262 const wxCursor &cursorStop)
263 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
a07c1212
SC
264{
265 wxMacEnsureTrackingHandlersInstalled() ;
266 SetData( data );
267 m_window = win;
268}
e9576ca5 269
a07c1212 270wxDropSource::~wxDropSource()
e9576ca5 271{
a07c1212 272}
e9576ca5 273
e9576ca5 274
2245b2b2 275wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
e9576ca5 276{
a07c1212 277 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
e40298d5 278
a07c1212
SC
279 if (!m_data)
280 return (wxDragResult) wxDragNone;
e40298d5 281
a07c1212
SC
282 if (m_data->GetFormatCount() == 0)
283 return (wxDragResult) wxDragNone;
e40298d5 284
a07c1212
SC
285 OSErr result;
286 DragReference theDrag;
287 RgnHandle dragRegion;
0cd51b2d 288 if ((result = NewDrag(&theDrag)))
a07c1212
SC
289 {
290 return wxDragNone ;
291 }
292 // add data to drag
293 size_t formatCount = m_data->GetFormatCount() ;
294 wxDataFormat *formats = new wxDataFormat[formatCount] ;
295 m_data->GetAllFormats( formats ) ;
296 ItemReference theItem = 1 ;
0cd51b2d 297 for ( size_t i = 0 ; i < formatCount ; ++i )
a07c1212
SC
298 {
299 size_t dataSize = m_data->GetDataSize( formats[i] ) ;
300 Ptr dataPtr = new char[dataSize] ;
301 m_data->GetDataHere( formats[i] , dataPtr ) ;
302 OSType type = formats[i].GetFormatId() ;
303 if ( type == 'TEXT' )
304 {
e40298d5
JS
305 dataSize-- ;
306 if ( wxApp::s_macDefaultEncodingIsPC )
307 {
308 wxMacConvertFromPC((char*)dataPtr,(char*)dataPtr,dataSize) ;
309 }
310 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
a07c1212
SC
311 }
312 else if (type == kDragFlavorTypeHFS )
313 {
e40298d5
JS
314 HFSFlavor theFlavor ;
315 OSErr err = noErr;
316 CInfoPBRec cat;
317
318 wxMacFilename2FSSpec( dataPtr , &theFlavor.fileSpec ) ;
319
320 cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name;
321 cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum;
322 cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID;
323 cat.hFileInfo.ioFDirIndex = 0;
324 err = PBGetCatInfoSync(&cat);
325 if (err == noErr )
326 {
327 theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags;
328 if (theFlavor.fileSpec.parID == fsRtParID) {
329 theFlavor.fileCreator = 'MACS';
330 theFlavor.fileType = 'disk';
331 } else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) {
332 theFlavor.fileCreator = 'MACS';
333 theFlavor.fileType = 'fold';
334 } else {
335 theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
336 theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType;
337 }
338 AddDragItemFlavor(theDrag, theItem, type , &theFlavor, sizeof(theFlavor), 0);
339 }
a07c1212
SC
340 }
341 else
342 {
e40298d5 343 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
a07c1212
SC
344 }
345 delete[] dataPtr ;
346 }
347 delete[] formats ;
348
349 dragRegion = NewRgn();
350 RgnHandle tempRgn = NewRgn() ;
351
45a0fd37
SC
352 EventRecord* ev = NULL ;
353#if !TARGET_CARBON // TODO
e40298d5 354 ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
45a0fd37 355#else
e40298d5
JS
356 EventRecord rec ;
357 ev = &rec ;
358 wxMacConvertEventToRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
45a0fd37 359#endif
a07c1212
SC
360 const short dragRegionOuterBoundary = 10 ;
361 const short dragRegionInnerBoundary = 9 ;
362
363 SetRectRgn( dragRegion , ev->where.h - dragRegionOuterBoundary ,
e40298d5
JS
364 ev->where.v - dragRegionOuterBoundary ,
365 ev->where.h + dragRegionOuterBoundary ,
366 ev->where.v + dragRegionOuterBoundary ) ;
367
a07c1212 368 SetRectRgn( tempRgn , ev->where.h - dragRegionInnerBoundary ,
e40298d5
JS
369 ev->where.v - dragRegionInnerBoundary ,
370 ev->where.h + dragRegionInnerBoundary ,
371 ev->where.v + dragRegionInnerBoundary ) ;
372
a07c1212
SC
373 DiffRgn( dragRegion , tempRgn , dragRegion ) ;
374 DisposeRgn( tempRgn ) ;
e40298d5 375
a07c1212
SC
376 // TODO:work with promises in order to return data only when drag
377 // was successfully completed
e40298d5 378
a07c1212
SC
379 gTrackingGlobals.m_currentSource = this ;
380 result = TrackDrag(theDrag, ev , dragRegion);
381 DisposeRgn(dragRegion);
382 DisposeDrag(theDrag);
383 gTrackingGlobals.m_currentSource = NULL ;
e40298d5 384
a07c1212
SC
385 return wxDragCopy ;
386}
387
da804130
SC
388bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect)
389{
390 const wxCursor& cursor = GetCursor(effect);
391 if ( cursor.Ok() )
392 {
393 cursor.MacInstall() ;
394
395 return TRUE;
396 }
397 else
398 {
399 return FALSE;
400 }
401}
402
a07c1212
SC
403bool gTrackingGlobalsInstalled = false ;
404
405// passing the globals via refcon is not needed by the CFM and later architectures anymore
406// but I'll leave it in there, just in case...
407
408pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
409 void *handlerRefCon, DragReference theDrag) ;
410pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
411DragReference theDrag) ;
412
413void wxMacEnsureTrackingHandlersInstalled()
e9576ca5 414{
a07c1212
SC
415 if( !gTrackingGlobalsInstalled )
416 {
417 OSErr result;
418
419 result = InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L,&gTrackingGlobals);
420 wxASSERT( result == noErr ) ;
421 result = InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals);
422 wxASSERT( result == noErr ) ;
e9576ca5 423
a07c1212
SC
424 gTrackingGlobalsInstalled = true ;
425 }
426}
427
428pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
429 void *handlerRefCon, DragReference theDrag)
430{
431 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
432 Point mouse, localMouse;
433 DragAttributes attributes;
a07c1212
SC
434 GetDragAttributes(theDrag, &attributes);
435 wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ) ;
436 switch(theMessage)
437 {
438 case kDragTrackingEnterHandler:
439 break;
440 case kDragTrackingLeaveHandler:
441 break;
442 case kDragTrackingEnterWindow:
e40298d5 443 trackingGlobals->m_currentTargetWindow = NULL ;
a07c1212
SC
444 trackingGlobals->m_currentTarget = NULL ;
445 break;
446 case kDragTrackingInWindow:
447 if (toplevel == NULL)
448 break;
449
450 GetDragMouse(theDrag, &mouse, 0L);
451 localMouse = mouse;
452 GlobalToLocal(&localMouse);
453
454// if (attributes & kDragHasLeftSenderWindow)
455 {
456 wxPoint point(localMouse.h , localMouse.v) ;
457 wxWindow *win = NULL ;
458 toplevel->MacGetWindowFromPointSub( point , &win ) ;
459 int localx , localy ;
460 localx = localMouse.h ;
461 localy = localMouse.v ;
462 //TODO : should we use client coordinates
463 if ( win )
464 win->MacRootWindowToWindow( &localx , &localy ) ;
465 if ( win != trackingGlobals->m_currentTargetWindow )
466 {
467 if ( trackingGlobals->m_currentTargetWindow )
468 {
469 // this window is left
470 if ( trackingGlobals->m_currentTarget )
471 {
472 HideDragHilite(theDrag);
473 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
474 trackingGlobals->m_currentTarget->OnLeave() ;
475 trackingGlobals->m_currentTarget = NULL;
476 trackingGlobals->m_currentTargetWindow = NULL ;
477 }
478 }
479 if ( win )
480 {
481 // this window is entered
482 trackingGlobals->m_currentTargetWindow = win ;
483 trackingGlobals->m_currentTarget = win->GetDropTarget() ;
a07c1212 484 {
da804130
SC
485 wxDragResult result = wxDragNone ;
486 if ( trackingGlobals->m_currentTarget )
487 {
488 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
489 result = trackingGlobals->m_currentTarget->OnEnter(
490 localx , localy , wxDragCopy ) ;
491 }
492
493 if ( trackingGlobals->m_currentSource && trackingGlobals->m_currentSource->GiveFeedback( result ) == FALSE )
494 {
495 if ( trackingGlobals->m_currentSource->MacInstallDefaultCursor( result ) == FALSE )
496 {
497 switch( result )
498 {
499 case wxDragCopy :
500 {
501 wxCursor cursor(wxCURSOR_COPY_ARROW) ;
502 cursor.MacInstall() ;
503 }
504 break ;
505 case wxDragMove :
506 {
507 wxCursor cursor(wxCURSOR_ARROW) ;
508 cursor.MacInstall() ;
509 }
510 break ;
511 case wxDragNone :
512 {
513 wxCursor cursor(wxCURSOR_NO_ENTRY) ;
514 cursor.MacInstall() ;
515 }
516 break ;
517 }
518 }
519 }
520
521 if ( result != wxDragNone )
a07c1212 522 {
da804130
SC
523 int x , y ;
524 x = y = 0 ;
525 win->MacWindowToRootWindow( &x , &y ) ;
526 RgnHandle hiliteRgn = NewRgn() ;
527 SetRectRgn( hiliteRgn , x , y , x+win->GetSize().x ,y+win->GetSize().y) ;
528 ShowDragHilite(theDrag, hiliteRgn, true);
529 DisposeRgn( hiliteRgn ) ;
a07c1212
SC
530 }
531 }
532 }
533 }
534 else
535 {
536 if( trackingGlobals->m_currentTarget )
537 {
538 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
539 trackingGlobals->m_currentTarget->OnDragOver(
540 localx , localy , wxDragCopy ) ;
541 }
542 }
543
544 }
545 // MyTrackItemUnderMouse(localMouse, theWindow);
546 break;
547 case kDragTrackingLeaveWindow:
548 if (trackingGlobals->m_currentTarget)
549 {
550 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
551 trackingGlobals->m_currentTarget->OnLeave() ;
552 HideDragHilite(theDrag);
553 trackingGlobals->m_currentTarget = NULL ;
554 }
555 trackingGlobals->m_currentTargetWindow = NULL ;
556 break;
557 }
558 return(noErr);
559}
560
f2af4afb
GD
561pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
562 void *handlerRefCon,
563 DragReference theDrag)
a07c1212
SC
564{
565 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
566 if ( trackingGlobals->m_currentTarget )
567 {
568 Point mouse,localMouse ;
569 int localx,localy ;
570
571 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
572 GetDragMouse(theDrag, &mouse, 0L);
573 localMouse = mouse;
574 GlobalToLocal(&localMouse);
575 localx = localMouse.h ;
576 localy = localMouse.v ;
577 //TODO : should we use client coordinates
578 if ( trackingGlobals->m_currentTargetWindow )
579 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
580 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
581 {
582 trackingGlobals->m_currentTarget->OnData( localx , localy , wxDragCopy ) ;
583 }
584 }
585 return(noErr);
586}
03e11df5 587#endif