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