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