]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dnd.cpp
As reported by Chris Elliott some releases of Lesstif crash
[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 bool hasPreferredFormat = false ;
190 wxDataFormat preferredFormat = m_dataObject->GetPreferredFormat( wxDataObject::Set ) ;
191
192 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
193 {
194 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
195 wxDataFormat format(theType) ;
196 if ( preferredFormat == format )
197 {
198 hasPreferredFormat = true ;
199 break ;
200 }
201 }
202
203 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
204 {
205 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
206 wxDataFormat format(theType) ;
207 if ( (hasPreferredFormat && format==preferredFormat) || (!hasPreferredFormat && m_dataObject->IsSupportedFormat( format )))
208 {
209 FlavorFlags theFlags;
210 result = GetFlavorFlags((DragReference)m_currentDrag, theItem, theType, &theFlags);
211 if (result == noErr)
212 {
213 Size dataSize ;
214 Ptr theData ;
215 GetFlavorDataSize((DragReference)m_currentDrag, theItem, theType, &dataSize);
216 if ( theType == kScrapFlavorTypeText )
217 {
218 // this increment is only valid for allocating, on the next GetFlavorData
219 // call it is reset again to the original value
220 dataSize++ ;
221 }
222 else if ( theType == kScrapFlavorTypeUnicode )
223 {
224 // this increment is only valid for allocating, on the next GetFlavorData
225 // call it is reset again to the original value
226 dataSize++ ;
227 dataSize++ ;
228 }
229 theData = new char[dataSize];
230 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
231 if( theType == kScrapFlavorTypeText )
232 {
233 theData[dataSize]=0 ;
234 m_dataObject->SetData( wxDataFormat(wxDF_TEXT), dataSize , theData );
235 }
236 #if wxUSE_UNICODE
237 else if ( theType == kScrapFlavorTypeUnicode )
238 {
239 theData[dataSize]=0 ;
240 theData[dataSize+1]=0 ;
241 m_dataObject->SetData( wxDataFormat(wxDF_UNICODETEXT), dataSize , theData );
242 }
243 #endif
244 else if ( theType == kDragFlavorTypeHFS )
245 {
246 HFSFlavor* theFile = (HFSFlavor*) theData ;
247 wxString name = wxMacFSSpec2MacFilename( &theFile->fileSpec ) ;
248 if ( !firstFileAdded )
249 {
250 // reset file list
251 ((wxFileDataObject*)m_dataObject)->SetData( 0 , "" ) ;
252 firstFileAdded = true ;
253 }
254 ((wxFileDataObject*)m_dataObject)->AddFile( name ) ;
255 }
256 else
257 {
258 m_dataObject->SetData( format, dataSize, theData );
259 }
260 delete[] theData;
261 }
262 break ;
263 }
264 }
265 }
266 }
267 return TRUE ;
268 }
269
270 //-------------------------------------------------------------------------
271 // wxDropSource
272 //-------------------------------------------------------------------------
273
274 //-----------------------------------------------------------------------------
275 // drag request
276
277 wxDropSource::wxDropSource(wxWindow *win,
278 const wxCursor &cursorCopy,
279 const wxCursor &cursorMove,
280 const wxCursor &cursorStop)
281 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
282 {
283 wxMacEnsureTrackingHandlersInstalled() ;
284 m_window = win;
285 }
286
287 wxDropSource::wxDropSource(wxDataObject& data,
288 wxWindow *win,
289 const wxCursor &cursorCopy,
290 const wxCursor &cursorMove,
291 const wxCursor &cursorStop)
292 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
293 {
294 wxMacEnsureTrackingHandlersInstalled() ;
295 SetData( data );
296 m_window = win;
297 }
298
299 wxDropSource::~wxDropSource()
300 {
301 }
302
303
304 wxDragResult wxDropSource::DoDragDrop(int flags)
305 {
306 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
307
308 if (!m_data)
309 return (wxDragResult) wxDragNone;
310
311 if (m_data->GetFormatCount() == 0)
312 return (wxDragResult) wxDragNone;
313
314 OSErr result;
315 DragReference theDrag;
316 RgnHandle dragRegion;
317 if ((result = NewDrag(&theDrag)))
318 {
319 return wxDragNone ;
320 }
321 // add data to drag
322 size_t formatCount = m_data->GetFormatCount() ;
323 wxDataFormat *formats = new wxDataFormat[formatCount] ;
324 m_data->GetAllFormats( formats ) ;
325 ItemReference theItem = 1 ;
326 for ( size_t i = 0 ; i < formatCount ; ++i )
327 {
328 size_t dataSize = m_data->GetDataSize( formats[i] ) ;
329 Ptr dataPtr = new char[dataSize] ;
330 m_data->GetDataHere( formats[i] , dataPtr ) ;
331 OSType type = formats[i].GetFormatId() ;
332 if ( type == 'TEXT' || type == 'utxt' )
333 {
334 if ( dataSize > 0 )
335 dataSize-- ;
336 dataPtr[ dataSize ] = 0 ;
337 if ( type == 'utxt' )
338 {
339 if ( dataSize > 0 )
340 dataSize-- ;
341 dataPtr[ dataSize ] = 0 ;
342 }
343 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
344 }
345 else if (type == kDragFlavorTypeHFS )
346 {
347 HFSFlavor theFlavor ;
348 OSErr err = noErr;
349 CInfoPBRec cat;
350
351 wxMacFilename2FSSpec( wxString( dataPtr , *wxConvCurrent ) , &theFlavor.fileSpec ) ;
352
353 cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name;
354 cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum;
355 cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID;
356 cat.hFileInfo.ioFDirIndex = 0;
357 err = PBGetCatInfoSync(&cat);
358 if (err == noErr )
359 {
360 theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags;
361 if (theFlavor.fileSpec.parID == fsRtParID) {
362 theFlavor.fileCreator = 'MACS';
363 theFlavor.fileType = 'disk';
364 } else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) {
365 theFlavor.fileCreator = 'MACS';
366 theFlavor.fileType = 'fold';
367 } else {
368 theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
369 theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType;
370 }
371 AddDragItemFlavor(theDrag, theItem, type , &theFlavor, sizeof(theFlavor), 0);
372 }
373 }
374 else
375 {
376 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
377 }
378 delete[] dataPtr ;
379 }
380 delete[] formats ;
381
382 dragRegion = NewRgn();
383 RgnHandle tempRgn = NewRgn() ;
384
385 EventRecord* ev = NULL ;
386 #if !TARGET_CARBON // TODO
387 ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
388 #else
389 EventRecord rec ;
390 ev = &rec ;
391 wxMacConvertEventToRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
392 #endif
393 const short dragRegionOuterBoundary = 10 ;
394 const short dragRegionInnerBoundary = 9 ;
395
396 SetRectRgn( dragRegion , ev->where.h - dragRegionOuterBoundary ,
397 ev->where.v - dragRegionOuterBoundary ,
398 ev->where.h + dragRegionOuterBoundary ,
399 ev->where.v + dragRegionOuterBoundary ) ;
400
401 SetRectRgn( tempRgn , ev->where.h - dragRegionInnerBoundary ,
402 ev->where.v - dragRegionInnerBoundary ,
403 ev->where.h + dragRegionInnerBoundary ,
404 ev->where.v + dragRegionInnerBoundary ) ;
405
406 DiffRgn( dragRegion , tempRgn , dragRegion ) ;
407 DisposeRgn( tempRgn ) ;
408
409 // TODO:work with promises in order to return data only when drag
410 // was successfully completed
411
412 gTrackingGlobals.m_currentSource = this ;
413 result = TrackDrag(theDrag, ev , dragRegion);
414 DisposeRgn(dragRegion);
415 DisposeDrag(theDrag);
416 gTrackingGlobals.m_currentSource = NULL ;
417
418 bool optionDown = GetCurrentKeyModifiers() & optionKey ;
419 wxDragResult dndresult = wxDragCopy ;
420 if ( flags != wxDrag_CopyOnly )
421 {
422 // on mac the option key is always the indication for copy
423 dndresult = optionDown ? wxDragCopy : wxDragMove;
424 }
425 return dndresult;
426 }
427
428 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect)
429 {
430 const wxCursor& cursor = GetCursor(effect);
431 if ( cursor.Ok() )
432 {
433 cursor.MacInstall() ;
434
435 return TRUE;
436 }
437 else
438 {
439 return FALSE;
440 }
441 }
442
443 bool gTrackingGlobalsInstalled = false ;
444
445 // passing the globals via refcon is not needed by the CFM and later architectures anymore
446 // but I'll leave it in there, just in case...
447
448 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
449 void *handlerRefCon, DragReference theDrag) ;
450 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
451 DragReference theDrag) ;
452
453 void wxMacEnsureTrackingHandlersInstalled()
454 {
455 if( !gTrackingGlobalsInstalled )
456 {
457 OSErr result;
458
459 result = InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L,&gTrackingGlobals);
460 wxASSERT( result == noErr ) ;
461 result = InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals);
462 wxASSERT( result == noErr ) ;
463
464 gTrackingGlobalsInstalled = true ;
465 }
466 }
467
468 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
469 void *handlerRefCon, DragReference theDrag)
470 {
471 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
472 Point mouse, localMouse;
473 DragAttributes attributes;
474 GetDragAttributes(theDrag, &attributes);
475 wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ) ;
476
477 bool optionDown = GetCurrentKeyModifiers() & optionKey ;
478 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
479
480 switch(theMessage)
481 {
482 case kDragTrackingEnterHandler:
483 break;
484 case kDragTrackingLeaveHandler:
485 break;
486 case kDragTrackingEnterWindow:
487 trackingGlobals->m_currentTargetWindow = NULL ;
488 trackingGlobals->m_currentTarget = NULL ;
489 break;
490 case kDragTrackingInWindow:
491 if (toplevel == NULL)
492 break;
493
494 GetDragMouse(theDrag, &mouse, 0L);
495 localMouse = mouse;
496 GlobalToLocal(&localMouse);
497
498
499
500 {
501 wxWindow *win = NULL ;
502 ControlPartCode controlPart ;
503 ControlRef control = wxMacFindControlUnderMouse( toplevel , localMouse ,
504 theWindow , &controlPart ) ;
505 if ( control )
506 win = wxFindControlFromMacControl( control ) ;
507 else
508 win = toplevel ;
509
510 int localx , localy ;
511 localx = localMouse.h ;
512 localy = localMouse.v ;
513
514 if ( win )
515 win->MacRootWindowToWindow( &localx , &localy ) ;
516 if ( win != trackingGlobals->m_currentTargetWindow )
517 {
518 if ( trackingGlobals->m_currentTargetWindow )
519 {
520 // this window is left
521 if ( trackingGlobals->m_currentTarget )
522 {
523 HideDragHilite(theDrag);
524 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
525 trackingGlobals->m_currentTarget->OnLeave() ;
526 trackingGlobals->m_currentTarget = NULL;
527 trackingGlobals->m_currentTargetWindow = NULL ;
528 }
529 }
530 if ( win )
531 {
532 // this window is entered
533 trackingGlobals->m_currentTargetWindow = win ;
534 trackingGlobals->m_currentTarget = win->GetDropTarget() ;
535 {
536
537 if ( trackingGlobals->m_currentTarget )
538 {
539 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
540 result = trackingGlobals->m_currentTarget->OnEnter(
541 localx , localy , result ) ;
542 }
543
544
545 if ( result != wxDragNone )
546 {
547 int x , y ;
548 x = y = 0 ;
549 win->MacWindowToRootWindow( &x , &y ) ;
550 RgnHandle hiliteRgn = NewRgn() ;
551 Rect r = { y , x , y+win->GetSize().y , x+win->GetSize().x } ;
552 RectRgn( hiliteRgn , &r ) ;
553 ShowDragHilite(theDrag, hiliteRgn, true);
554 DisposeRgn( hiliteRgn ) ;
555 }
556 }
557 }
558 }
559 else
560 {
561 if( trackingGlobals->m_currentTarget )
562 {
563 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
564 trackingGlobals->m_currentTarget->OnDragOver(
565 localx , localy , result ) ;
566 }
567 }
568
569 // set cursor for OnEnter and OnDragOver
570 if ( trackingGlobals->m_currentSource && trackingGlobals->m_currentSource->GiveFeedback( result ) == FALSE )
571 {
572 if ( trackingGlobals->m_currentSource->MacInstallDefaultCursor( result ) == FALSE )
573 {
574 switch( result )
575 {
576 case wxDragCopy :
577 {
578 wxCursor cursor(wxCURSOR_COPY_ARROW) ;
579 cursor.MacInstall() ;
580 }
581 break ;
582 case wxDragMove :
583 {
584 wxCursor cursor(wxCURSOR_ARROW) ;
585 cursor.MacInstall() ;
586 }
587 break ;
588 case wxDragNone :
589 {
590 wxCursor cursor(wxCURSOR_NO_ENTRY) ;
591 cursor.MacInstall() ;
592 }
593 break ;
594
595 case wxDragError:
596 case wxDragLink:
597 case wxDragCancel:
598 // put these here to make gcc happy
599 ;
600 }
601 }
602 }
603
604 }
605 break;
606 case kDragTrackingLeaveWindow:
607 if (trackingGlobals->m_currentTarget)
608 {
609 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
610 trackingGlobals->m_currentTarget->OnLeave() ;
611 HideDragHilite(theDrag);
612 trackingGlobals->m_currentTarget = NULL ;
613 }
614 trackingGlobals->m_currentTargetWindow = NULL ;
615 break;
616 }
617 return(noErr);
618 }
619
620 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow,
621 void *handlerRefCon,
622 DragReference theDrag)
623 {
624 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
625 if ( trackingGlobals->m_currentTarget )
626 {
627 Point mouse,localMouse ;
628 int localx,localy ;
629
630 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
631 GetDragMouse(theDrag, &mouse, 0L);
632 localMouse = mouse;
633 GlobalToLocal(&localMouse);
634 localx = localMouse.h ;
635 localy = localMouse.v ;
636 //TODO : should we use client coordinates
637 if ( trackingGlobals->m_currentTargetWindow )
638 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
639 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
640 {
641 bool optionDown = GetCurrentKeyModifiers() & optionKey ;
642 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
643 trackingGlobals->m_currentTarget->OnData( localx , localy , result ) ;
644 }
645 }
646 return(noErr);
647 }
648 #endif