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