]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dnd.cpp
52b0cbbf9043e0df8d651a045a48260e08f66c83
[wxWidgets.git] / src / mac / carbon / dnd.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dnd.cpp
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dnd.h"
14 #endif
15
16 #include "wx/wxprec.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 #ifndef __DARWIN__
28 #include <Scrap.h>
29 #endif
30
31 // ----------------------------------------------------------------------------
32 // global
33 // ----------------------------------------------------------------------------
34
35 void wxMacEnsureTrackingHandlersInstalled() ;
36
37 typedef struct
38 {
39 wxWindow* m_currentTargetWindow ;
40 wxDropTarget* m_currentTarget ;
41 wxDropSource* m_currentSource ;
42 } MacTrackingGlobals ;
43
44 MacTrackingGlobals gTrackingGlobals ;
45
46 //----------------------------------------------------------------------------
47 // wxDropTarget
48 //----------------------------------------------------------------------------
49
50 wxDropTarget::wxDropTarget( wxDataObject *data )
51 : wxDropTargetBase( data )
52 {
53 wxMacEnsureTrackingHandlersInstalled() ;
54 }
55
56 wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x),
57 wxCoord WXUNUSED(y),
58 wxDragResult def )
59 {
60
61 return CurrentDragHasSupportedFormat() ? def : wxDragNone;
62 }
63
64 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
65 {
66 if (!m_dataObject)
67 return FALSE;
68
69 return CurrentDragHasSupportedFormat() ;
70 }
71
72 wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
73 wxDragResult def )
74 {
75 if (!m_dataObject)
76 return wxDragNone;
77
78 if (!CurrentDragHasSupportedFormat())
79 return wxDragNone;
80
81 return GetData() ? def : wxDragNone;
82 }
83
84 bool wxDropTarget::CurrentDragHasSupportedFormat()
85 {
86 bool supported = false ;
87 if ( gTrackingGlobals.m_currentSource != NULL )
88 {
89 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
90
91 if ( data )
92 {
93 size_t formatcount = data->GetFormatCount() ;
94 wxDataFormat *array = new wxDataFormat[ formatcount ];
95 data->GetAllFormats( array );
96 for (size_t i = 0; !supported && i < formatcount ; i++)
97 {
98 wxDataFormat format = array[i] ;
99 if ( m_dataObject->IsSupported( format ) )
100 {
101 supported = true ;
102 break ;
103 }
104 }
105 delete[] array ;
106 }
107 }
108 if ( !supported )
109 {
110 UInt16 items ;
111 OSErr result;
112 CountDragItems((DragReference)m_currentDrag, &items);
113 for (UInt16 index = 1; index <= items && supported == false ; ++index)
114 {
115 ItemReference theItem;
116 FlavorType theType ;
117 UInt16 flavors = 0 ;
118 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
119 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
120 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
121 {
122 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
123 if ( m_dataObject->IsSupportedFormat( wxDataFormat( theType ) ) )
124 {
125 supported = true ;
126 break ;
127 }
128 }
129 }
130 }
131 return supported ;
132 }
133
134 bool wxDropTarget::GetData()
135 {
136 if (!m_dataObject)
137 return FALSE;
138
139 if ( !CurrentDragHasSupportedFormat() )
140 return FALSE ;
141
142 bool transferred = false ;
143 if ( gTrackingGlobals.m_currentSource != NULL )
144 {
145 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject() ;
146
147 if ( data )
148 {
149 size_t formatcount = data->GetFormatCount() ;
150 wxDataFormat *array = new wxDataFormat[ formatcount ];
151 data->GetAllFormats( array );
152 for (size_t i = 0; !transferred && i < formatcount ; i++)
153 {
154 wxDataFormat format = array[i] ;
155 if ( m_dataObject->IsSupported( format ) )
156 {
157 int size = data->GetDataSize( format );
158 transferred = true ;
159
160 if (size == 0)
161 {
162 m_dataObject->SetData(format , 0 , 0 ) ;
163 }
164 else
165 {
166 char *d = new char[size];
167 data->GetDataHere( format , (void*) d );
168 m_dataObject->SetData( format , size , d ) ;
169 delete[] d ;
170 }
171 }
172 }
173 delete[] array ;
174 }
175 }
176 if ( !transferred )
177 {
178 UInt16 items ;
179 OSErr result;
180 bool firstFileAdded = false ;
181 CountDragItems((DragReference)m_currentDrag, &items);
182 for (UInt16 index = 1; index <= items; ++index)
183 {
184 ItemReference theItem;
185 FlavorType theType ;
186 UInt16 flavors = 0 ;
187 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
188 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
189 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
190 {
191 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
192 wxDataFormat format(theType) ;
193 if ( m_dataObject->IsSupportedFormat( format ) )
194 {
195 FlavorFlags theFlags;
196 result = GetFlavorFlags((DragReference)m_currentDrag, theItem, theType, &theFlags);
197 if (result == noErr)
198 {
199 Size dataSize ;
200 Ptr theData ;
201 GetFlavorDataSize((DragReference)m_currentDrag, theItem, theType, &dataSize);
202 if ( theType == kScrapFlavorTypeText )
203 {
204 // this increment is only valid for allocating, on the next GetFlavorData
205 // call it is reset again to the original value
206 dataSize++ ;
207 }
208 else if ( theType == kScrapFlavorTypeUnicode )
209 {
210 // this increment is only valid for allocating, on the next GetFlavorData
211 // call it is reset again to the original value
212 dataSize++ ;
213 dataSize++ ;
214 }
215 theData = new char[dataSize];
216 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
217 if( theType == kScrapFlavorTypeText )
218 {
219 theData[dataSize]=0 ;
220 m_dataObject->SetData( wxDataFormat(wxDF_TEXT), dataSize , theData );
221 }
222 #if wxUSE_UNICODE
223 else if ( theType == kScrapFlavorTypeUnicode )
224 {
225 theData[dataSize]=0 ;
226 theData[dataSize+1]=0 ;
227 m_dataObject->SetData( wxDataFormat(wxDF_UNICODETEXT), dataSize , theData );
228 }
229 #endif
230 else if ( theType == kDragFlavorTypeHFS )
231 {
232 HFSFlavor* theFile = (HFSFlavor*) theData ;
233 wxString name = wxMacFSSpec2MacFilename( &theFile->fileSpec ) ;
234 if ( !firstFileAdded )
235 {
236 // reset file list
237 ((wxFileDataObject*)m_dataObject)->SetData( 0 , "" ) ;
238 firstFileAdded = true ;
239 }
240 ((wxFileDataObject*)m_dataObject)->AddFile( name ) ;
241 }
242 else
243 {
244 m_dataObject->SetData( format, dataSize, theData );
245 }
246 delete[] theData;
247 }
248 break ;
249 }
250 }
251 }
252 }
253 return TRUE ;
254 }
255
256 //-------------------------------------------------------------------------
257 // wxDropSource
258 //-------------------------------------------------------------------------
259
260 //-----------------------------------------------------------------------------
261 // drag request
262
263 wxDropSource::wxDropSource(wxWindow *win,
264 const wxCursor &cursorCopy,
265 const wxCursor &cursorMove,
266 const wxCursor &cursorStop)
267 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
268 {
269 wxMacEnsureTrackingHandlersInstalled() ;
270 m_window = win;
271 }
272
273 wxDropSource::wxDropSource(wxDataObject& data,
274 wxWindow *win,
275 const wxCursor &cursorCopy,
276 const wxCursor &cursorMove,
277 const wxCursor &cursorStop)
278 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
279 {
280 wxMacEnsureTrackingHandlersInstalled() ;
281 SetData( data );
282 m_window = win;
283 }
284
285 wxDropSource::~wxDropSource()
286 {
287 }
288
289
290 wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
291 {
292 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
293
294 if (!m_data)
295 return (wxDragResult) wxDragNone;
296
297 if (m_data->GetFormatCount() == 0)
298 return (wxDragResult) wxDragNone;
299
300 OSErr result;
301 DragReference theDrag;
302 RgnHandle dragRegion;
303 if ((result = NewDrag(&theDrag)))
304 {
305 return wxDragNone ;
306 }
307 // add data to drag
308 size_t formatCount = m_data->GetFormatCount() ;
309 wxDataFormat *formats = new wxDataFormat[formatCount] ;
310 m_data->GetAllFormats( formats ) ;
311 ItemReference theItem = 1 ;
312 for ( size_t i = 0 ; i < formatCount ; ++i )
313 {
314 size_t dataSize = m_data->GetDataSize( formats[i] ) ;
315 Ptr dataPtr = new char[dataSize] ;
316 m_data->GetDataHere( formats[i] , dataPtr ) ;
317 OSType type = formats[i].GetFormatId() ;
318 if ( type == 'TEXT' || type == 'utxt' )
319 {
320 if ( dataSize > 0 )
321 dataSize-- ;
322 dataPtr[ dataSize ] = 0 ;
323 if ( type == 'utxt' )
324 {
325 if ( dataSize > 0 )
326 dataSize-- ;
327 dataPtr[ dataSize ] = 0 ;
328 }
329 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
330 }
331 else if (type == kDragFlavorTypeHFS )
332 {
333 HFSFlavor theFlavor ;
334 OSErr err = noErr;
335 CInfoPBRec cat;
336
337 wxMacFilename2FSSpec( wxString( dataPtr , *wxConvCurrent ) , &theFlavor.fileSpec ) ;
338
339 cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name;
340 cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum;
341 cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID;
342 cat.hFileInfo.ioFDirIndex = 0;
343 err = PBGetCatInfoSync(&cat);
344 if (err == noErr )
345 {
346 theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags;
347 if (theFlavor.fileSpec.parID == fsRtParID) {
348 theFlavor.fileCreator = 'MACS';
349 theFlavor.fileType = 'disk';
350 } else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) {
351 theFlavor.fileCreator = 'MACS';
352 theFlavor.fileType = 'fold';
353 } else {
354 theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
355 theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType;
356 }
357 AddDragItemFlavor(theDrag, theItem, type , &theFlavor, sizeof(theFlavor), 0);
358 }
359 }
360 else
361 {
362 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
363 }
364 delete[] dataPtr ;
365 }
366 delete[] formats ;
367
368 dragRegion = NewRgn();
369 RgnHandle tempRgn = NewRgn() ;
370
371 EventRecord* ev = NULL ;
372 #if !TARGET_CARBON // TODO
373 ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
374 #else
375 EventRecord rec ;
376 ev = &rec ;
377 wxMacConvertEventToRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
378 #endif
379 const short dragRegionOuterBoundary = 10 ;
380 const short dragRegionInnerBoundary = 9 ;
381
382 SetRectRgn( dragRegion , ev->where.h - dragRegionOuterBoundary ,
383 ev->where.v - dragRegionOuterBoundary ,
384 ev->where.h + dragRegionOuterBoundary ,
385 ev->where.v + dragRegionOuterBoundary ) ;
386
387 SetRectRgn( tempRgn , ev->where.h - dragRegionInnerBoundary ,
388 ev->where.v - dragRegionInnerBoundary ,
389 ev->where.h + dragRegionInnerBoundary ,
390 ev->where.v + dragRegionInnerBoundary ) ;
391
392 DiffRgn( dragRegion , tempRgn , dragRegion ) ;
393 DisposeRgn( tempRgn ) ;
394
395 // TODO:work with promises in order to return data only when drag
396 // was successfully completed
397
398 gTrackingGlobals.m_currentSource = this ;
399 result = TrackDrag(theDrag, ev , dragRegion);
400 DisposeRgn(dragRegion);
401 DisposeDrag(theDrag);
402 gTrackingGlobals.m_currentSource = NULL ;
403
404 bool optionDown = GetCurrentKeyModifiers() & optionKey ;
405 wxDragResult dndresult = optionDown ? wxDragCopy : wxDragMove;
406 return dndresult;
407 }
408
409 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect)
410 {
411 const wxCursor& cursor = GetCursor(effect);
412 if ( cursor.Ok() )
413 {
414 cursor.MacInstall() ;
415
416 return TRUE;
417 }
418 else
419 {
420 return FALSE;
421 }
422 }
423
424 bool gTrackingGlobalsInstalled = false ;
425
426 // passing the globals via refcon is not needed by the CFM and later architectures anymore
427 // but I'll leave it in there, just in case...
428
429 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
430 void *handlerRefCon, DragReference theDrag) ;
431 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
432 DragReference theDrag) ;
433
434 void wxMacEnsureTrackingHandlersInstalled()
435 {
436 if( !gTrackingGlobalsInstalled )
437 {
438 OSErr result;
439
440 result = InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L,&gTrackingGlobals);
441 wxASSERT( result == noErr ) ;
442 result = InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals);
443 wxASSERT( result == noErr ) ;
444
445 gTrackingGlobalsInstalled = true ;
446 }
447 }
448
449 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
450 void *handlerRefCon, DragReference theDrag)
451 {
452 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
453 Point mouse, localMouse;
454 DragAttributes attributes;
455 GetDragAttributes(theDrag, &attributes);
456 wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ) ;
457
458 bool optionDown = GetCurrentKeyModifiers() & optionKey ;
459 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
460
461 switch(theMessage)
462 {
463 case kDragTrackingEnterHandler:
464 break;
465 case kDragTrackingLeaveHandler:
466 break;
467 case kDragTrackingEnterWindow:
468 trackingGlobals->m_currentTargetWindow = NULL ;
469 trackingGlobals->m_currentTarget = NULL ;
470 break;
471 case kDragTrackingInWindow:
472 if (toplevel == NULL)
473 break;
474
475 GetDragMouse(theDrag, &mouse, 0L);
476 localMouse = mouse;
477 GlobalToLocal(&localMouse);
478
479
480
481 // if (attributes & kDragHasLeftSenderWindow)
482 {
483 // wxPoint point(localMouse.h , localMouse.v) ;
484 wxWindow *win = NULL ;
485 ControlPartCode controlPart ;
486 ControlRef control = wxMacFindControlUnderMouse( toplevel , localMouse ,
487 theWindow , &controlPart ) ;
488 if ( control )
489 win = wxFindControlFromMacControl( control ) ;
490 // TODO toplevel->MacGetWindowFromPointSub( point , &win ) ;
491 int localx , localy ;
492 localx = localMouse.h ;
493 localy = localMouse.v ;
494 //TODO : should we use client coordinates
495 if ( win )
496 win->MacRootWindowToWindow( &localx , &localy ) ;
497 if ( win != trackingGlobals->m_currentTargetWindow )
498 {
499 if ( trackingGlobals->m_currentTargetWindow )
500 {
501 // this window is left
502 if ( trackingGlobals->m_currentTarget )
503 {
504 HideDragHilite(theDrag);
505 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
506 trackingGlobals->m_currentTarget->OnLeave() ;
507 trackingGlobals->m_currentTarget = NULL;
508 trackingGlobals->m_currentTargetWindow = NULL ;
509 }
510 }
511 if ( win )
512 {
513 // this window is entered
514 trackingGlobals->m_currentTargetWindow = win ;
515 trackingGlobals->m_currentTarget = win->GetDropTarget() ;
516 {
517
518 if ( trackingGlobals->m_currentTarget )
519 {
520 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
521 result = trackingGlobals->m_currentTarget->OnEnter(
522 localx , localy , result ) ;
523 }
524
525
526 if ( result != wxDragNone )
527 {
528 int x , y ;
529 x = y = 0 ;
530 win->MacWindowToRootWindow( &x , &y ) ;
531 RgnHandle hiliteRgn = NewRgn() ;
532 Rect r = { y , x , y+win->GetSize().y , x+win->GetSize().x } ;
533 RectRgn( hiliteRgn , &r ) ;
534 ShowDragHilite(theDrag, hiliteRgn, true);
535 DisposeRgn( hiliteRgn ) ;
536 }
537 }
538 }
539 }
540 else
541 {
542 if( trackingGlobals->m_currentTarget )
543 {
544 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
545 trackingGlobals->m_currentTarget->OnDragOver(
546 localx , localy , result ) ;
547 }
548 }
549
550 // set cursor for OnEnter and OnDragOver
551 if ( trackingGlobals->m_currentSource && trackingGlobals->m_currentSource->GiveFeedback( result ) == FALSE )
552 {
553 if ( trackingGlobals->m_currentSource->MacInstallDefaultCursor( result ) == FALSE )
554 {
555 switch( result )
556 {
557 case wxDragCopy :
558 {
559 wxCursor cursor(wxCURSOR_COPY_ARROW) ;
560 cursor.MacInstall() ;
561 }
562 break ;
563 case wxDragMove :
564 {
565 wxCursor cursor(wxCURSOR_ARROW) ;
566 cursor.MacInstall() ;
567 }
568 break ;
569 case wxDragNone :
570 {
571 wxCursor cursor(wxCURSOR_NO_ENTRY) ;
572 cursor.MacInstall() ;
573 }
574 break ;
575
576 case wxDragError:
577 case wxDragLink:
578 case wxDragCancel:
579 // put these here to make gcc happy
580 ;
581 }
582 }
583 }
584
585 }
586 // MyTrackItemUnderMouse(localMouse, theWindow);
587 break;
588 case kDragTrackingLeaveWindow:
589 if (trackingGlobals->m_currentTarget)
590 {
591 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
592 trackingGlobals->m_currentTarget->OnLeave() ;
593 HideDragHilite(theDrag);
594 trackingGlobals->m_currentTarget = NULL ;
595 }
596 trackingGlobals->m_currentTargetWindow = NULL ;
597 break;
598 }
599 return(noErr);
600 }
601
602 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
603 void *handlerRefCon,
604 DragReference theDrag)
605 {
606 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
607 if ( trackingGlobals->m_currentTarget )
608 {
609 Point mouse,localMouse ;
610 int localx,localy ;
611
612 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
613 GetDragMouse(theDrag, &mouse, 0L);
614 localMouse = mouse;
615 GlobalToLocal(&localMouse);
616 localx = localMouse.h ;
617 localy = localMouse.v ;
618 //TODO : should we use client coordinates
619 if ( trackingGlobals->m_currentTargetWindow )
620 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
621 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
622 {
623 bool optionDown = GetCurrentKeyModifiers() & optionKey ;
624 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
625 trackingGlobals->m_currentTarget->OnData( localx , localy , result ) ;
626 }
627 }
628 return(noErr);
629 }
630 #endif