]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dnd.cpp
Various fixes for Textctrl and Popup Menus.
[wxWidgets.git] / src / mac / carbon / dnd.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dnd.cpp
3 // Purpose: wxDropTarget, wxDropSource, wxDataObject implementation
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 AUTHOR
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 int 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 int 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 CountDragItems((DragReference)m_currentDrag, &items);
177 for (UInt16 index = 1; index <= items; ++index)
178 {
179 ItemReference theItem;
180 FlavorType theType ;
181 UInt16 flavors = 0 ;
182 GetDragItemReferenceNumber((DragReference)m_currentDrag, index, &theItem);
183 CountDragItemFlavors( (DragReference)m_currentDrag, theItem , &flavors ) ;
184 for ( UInt16 flavor = 1 ; flavor <= flavors ; ++flavor )
185 {
186 result = GetFlavorType((DragReference)m_currentDrag, theItem, flavor , &theType);
187 wxDataFormat format(theType) ;
188 if ( m_dataObject->IsSupportedFormat( format ) )
189 {
190 FlavorFlags theFlags;
191 result = GetFlavorFlags((DragReference)m_currentDrag, theItem, theType, &theFlags);
192 if (result == noErr)
193 {
194 Size dataSize ;
195 Ptr theData ;
196 GetFlavorDataSize((DragReference)m_currentDrag, theItem, theType, &dataSize);
197 if ( theType == 'TEXT' )
198 dataSize++ ;
199 theData = new char[dataSize];
200 GetFlavorData((DragReference)m_currentDrag, theItem, theType, (void*) theData, &dataSize, 0L);
201 if( theType == 'TEXT' )
202 {
203 theData[dataSize]=0 ;
204 if ( wxApp::s_macDefaultEncodingIsPC )
205 {
206 wxMacConvertToPC((char*)theData) ;
207 }
208 m_dataObject->SetData( format, dataSize, theData );
209 }
210 else if ( theType == kDragFlavorTypeHFS )
211 {
212 HFSFlavor* theFile = (HFSFlavor*) theData ;
213 wxString name = wxMacFSSpec2MacFilename( &theFile->fileSpec ) ;
214 m_dataObject->SetData( format , name.Length() + 1, name ) ;
215 }
216 else
217 {
218 m_dataObject->SetData( format, dataSize, theData );
219 }
220 delete[] theData;
221 }
222 break ;
223 }
224 }
225 }
226 }
227 return TRUE ;
228 }
229
230 //-------------------------------------------------------------------------
231 // wxDropSource
232 //-------------------------------------------------------------------------
233
234 //-----------------------------------------------------------------------------
235 // drag request
236
237 wxDropSource::wxDropSource(wxWindow *win,
238 const wxIcon &iconCopy,
239 const wxIcon &iconMove,
240 const wxIcon &iconNone)
241 {
242 wxMacEnsureTrackingHandlersInstalled() ;
243 m_window = win;
244 }
245
246 wxDropSource::wxDropSource(wxDataObject& data,
247 wxWindow *win,
248 const wxIcon &iconCopy,
249 const wxIcon &iconMove,
250 const wxIcon &iconNone)
251 {
252 wxMacEnsureTrackingHandlersInstalled() ;
253 SetData( data );
254 m_window = win;
255 }
256
257 wxDropSource::~wxDropSource()
258 {
259 }
260
261
262 wxDragResult wxDropSource::DoDragDrop( bool allowMove )
263 {
264 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
265
266 if (!m_data)
267 return (wxDragResult) wxDragNone;
268
269 if (m_data->GetFormatCount() == 0)
270 return (wxDragResult) wxDragNone;
271
272 OSErr result;
273 DragReference theDrag;
274 RgnHandle dragRegion;
275 if (result = NewDrag(&theDrag))
276 {
277 return wxDragNone ;
278 }
279 // add data to drag
280 size_t formatCount = m_data->GetFormatCount() ;
281 wxDataFormat *formats = new wxDataFormat[formatCount] ;
282 m_data->GetAllFormats( formats ) ;
283 ItemReference theItem = 1 ;
284 for ( int i = 0 ; i < formatCount ; ++i )
285 {
286 size_t dataSize = m_data->GetDataSize( formats[i] ) ;
287 Ptr dataPtr = new char[dataSize] ;
288 m_data->GetDataHere( formats[i] , dataPtr ) ;
289 OSType type = formats[i].GetFormatId() ;
290 if ( type == 'TEXT' )
291 {
292 dataSize-- ;
293 if ( wxApp::s_macDefaultEncodingIsPC )
294 {
295 wxMacConvertFromPC((char*)dataPtr) ;
296 }
297 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
298 }
299 else if (type == kDragFlavorTypeHFS )
300 {
301 HFSFlavor theFlavor ;
302 OSErr err = noErr;
303 CInfoPBRec cat;
304
305 wxMacFilename2FSSpec( dataPtr , &theFlavor.fileSpec ) ;
306
307 cat.hFileInfo.ioNamePtr = theFlavor.fileSpec.name;
308 cat.hFileInfo.ioVRefNum = theFlavor.fileSpec.vRefNum;
309 cat.hFileInfo.ioDirID = theFlavor.fileSpec.parID;
310 cat.hFileInfo.ioFDirIndex = 0;
311 err = PBGetCatInfoSync(&cat);
312 if (err == noErr )
313 {
314 theFlavor.fdFlags = cat.hFileInfo.ioFlFndrInfo.fdFlags;
315 if (theFlavor.fileSpec.parID == fsRtParID) {
316 theFlavor.fileCreator = 'MACS';
317 theFlavor.fileType = 'disk';
318 } else if ((cat.hFileInfo.ioFlAttrib & ioDirMask) != 0) {
319 theFlavor.fileCreator = 'MACS';
320 theFlavor.fileType = 'fold';
321 } else {
322 theFlavor.fileCreator = cat.hFileInfo.ioFlFndrInfo.fdCreator;
323 theFlavor.fileType = cat.hFileInfo.ioFlFndrInfo.fdType;
324 }
325 AddDragItemFlavor(theDrag, theItem, type , &theFlavor, sizeof(theFlavor), 0);
326 }
327 }
328 else
329 {
330 AddDragItemFlavor(theDrag, theItem, type , dataPtr, dataSize, 0);
331 }
332 delete[] dataPtr ;
333 }
334 delete[] formats ;
335
336 dragRegion = NewRgn();
337 RgnHandle tempRgn = NewRgn() ;
338
339 EventRecord* ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ;
340 const short dragRegionOuterBoundary = 10 ;
341 const short dragRegionInnerBoundary = 9 ;
342
343 SetRectRgn( dragRegion , ev->where.h - dragRegionOuterBoundary ,
344 ev->where.v - dragRegionOuterBoundary ,
345 ev->where.h + dragRegionOuterBoundary ,
346 ev->where.v + dragRegionOuterBoundary ) ;
347
348 SetRectRgn( tempRgn , ev->where.h - dragRegionInnerBoundary ,
349 ev->where.v - dragRegionInnerBoundary ,
350 ev->where.h + dragRegionInnerBoundary ,
351 ev->where.v + dragRegionInnerBoundary ) ;
352
353 DiffRgn( dragRegion , tempRgn , dragRegion ) ;
354 DisposeRgn( tempRgn ) ;
355
356 // TODO:work with promises in order to return data only when drag
357 // was successfully completed
358
359 gTrackingGlobals.m_currentSource = this ;
360 result = TrackDrag(theDrag, ev , dragRegion);
361 DisposeRgn(dragRegion);
362 DisposeDrag(theDrag);
363 gTrackingGlobals.m_currentSource = NULL ;
364
365 return wxDragCopy ;
366 }
367
368 bool gTrackingGlobalsInstalled = false ;
369
370 // passing the globals via refcon is not needed by the CFM and later architectures anymore
371 // but I'll leave it in there, just in case...
372
373 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
374 void *handlerRefCon, DragReference theDrag) ;
375 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
376 DragReference theDrag) ;
377
378 void wxMacEnsureTrackingHandlersInstalled()
379 {
380 if( !gTrackingGlobalsInstalled )
381 {
382 OSErr result;
383
384 result = InstallTrackingHandler(NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L,&gTrackingGlobals);
385 wxASSERT( result == noErr ) ;
386 result = InstallReceiveHandler(NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals);
387 wxASSERT( result == noErr ) ;
388
389 gTrackingGlobalsInstalled = true ;
390 }
391 }
392
393 pascal OSErr wxMacWindowDragTrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
394 void *handlerRefCon, DragReference theDrag)
395 {
396 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
397 Point mouse, localMouse;
398 DragAttributes attributes;
399 RgnHandle hiliteRgn;
400 GetDragAttributes(theDrag, &attributes);
401 wxTopLevelWindowMac* toplevel = wxFindWinFromMacWindow( theWindow ) ;
402 switch(theMessage)
403 {
404 case kDragTrackingEnterHandler:
405 break;
406 case kDragTrackingLeaveHandler:
407 break;
408 case kDragTrackingEnterWindow:
409 trackingGlobals->m_currentTargetWindow = NULL ;
410 trackingGlobals->m_currentTarget = NULL ;
411 break;
412 case kDragTrackingInWindow:
413 if (toplevel == NULL)
414 break;
415
416 GetDragMouse(theDrag, &mouse, 0L);
417 localMouse = mouse;
418 GlobalToLocal(&localMouse);
419
420 // if (attributes & kDragHasLeftSenderWindow)
421 {
422 wxPoint point(localMouse.h , localMouse.v) ;
423 wxWindow *win = NULL ;
424 toplevel->MacGetWindowFromPointSub( point , &win ) ;
425 int localx , localy ;
426 localx = localMouse.h ;
427 localy = localMouse.v ;
428 //TODO : should we use client coordinates
429 if ( win )
430 win->MacRootWindowToWindow( &localx , &localy ) ;
431 if ( win != trackingGlobals->m_currentTargetWindow )
432 {
433 if ( trackingGlobals->m_currentTargetWindow )
434 {
435 // this window is left
436 if ( trackingGlobals->m_currentTarget )
437 {
438 HideDragHilite(theDrag);
439 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
440 trackingGlobals->m_currentTarget->OnLeave() ;
441 trackingGlobals->m_currentTarget = NULL;
442 trackingGlobals->m_currentTargetWindow = NULL ;
443 }
444 }
445 if ( win )
446 {
447 // this window is entered
448 trackingGlobals->m_currentTargetWindow = win ;
449 trackingGlobals->m_currentTarget = win->GetDropTarget() ;
450 if ( trackingGlobals->m_currentTarget )
451 {
452 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
453 if ( trackingGlobals->m_currentTarget->OnEnter(
454 localx , localy , wxDragCopy ) != wxDragNone )
455 {
456 int x , y ;
457 x = y = 0 ;
458 win->MacWindowToRootWindow( &x , &y ) ;
459 RgnHandle hiliteRgn = NewRgn() ;
460 SetRectRgn( hiliteRgn , x , y , x+win->GetSize().x ,y+win->GetSize().y) ;
461 ShowDragHilite(theDrag, hiliteRgn, true);
462 DisposeRgn( hiliteRgn ) ;
463 }
464 }
465 }
466 }
467 else
468 {
469 if( trackingGlobals->m_currentTarget )
470 {
471 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
472 trackingGlobals->m_currentTarget->OnDragOver(
473 localx , localy , wxDragCopy ) ;
474 }
475 }
476
477 }
478 // MyTrackItemUnderMouse(localMouse, theWindow);
479 break;
480 case kDragTrackingLeaveWindow:
481 if (trackingGlobals->m_currentTarget)
482 {
483 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
484 trackingGlobals->m_currentTarget->OnLeave() ;
485 HideDragHilite(theDrag);
486 trackingGlobals->m_currentTarget = NULL ;
487 }
488 trackingGlobals->m_currentTargetWindow = NULL ;
489 break;
490 }
491 return(noErr);
492 }
493
494 pascal OSErr wxMacWindowDragReceiveHandler(WindowPtr theWindow, void *handlerRefCon,
495 DragReference theDrag)
496 {
497 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
498 if ( trackingGlobals->m_currentTarget )
499 {
500 Point mouse,localMouse ;
501 int localx,localy ;
502
503 trackingGlobals->m_currentTarget->SetCurrentDrag( theDrag ) ;
504 GetDragMouse(theDrag, &mouse, 0L);
505 localMouse = mouse;
506 GlobalToLocal(&localMouse);
507 localx = localMouse.h ;
508 localy = localMouse.v ;
509 //TODO : should we use client coordinates
510 if ( trackingGlobals->m_currentTargetWindow )
511 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx , &localy ) ;
512 if ( trackingGlobals->m_currentTarget->OnDrop( localx , localy ) )
513 {
514 trackingGlobals->m_currentTarget->OnData( localx , localy , wxDragCopy ) ;
515 }
516 }
517 return(noErr);
518 }
519 #endif