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