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