]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/dnd.cpp
better native types for carbon
[wxWidgets.git] / src / osx / carbon / dnd.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/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
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/toplevel.h"
21 #include "wx/gdicmn.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/osx/private.h"
25
26 // ----------------------------------------------------------------------------
27 // globals
28 // ----------------------------------------------------------------------------
29
30 typedef struct
31 {
32 wxWindow *m_currentTargetWindow;
33 wxDropTarget *m_currentTarget;
34 wxDropSource *m_currentSource;
35 wxDragResult m_result;
36 int m_flags;
37 } MacTrackingGlobals;
38
39 MacTrackingGlobals gTrackingGlobals;
40
41 void wxMacEnsureTrackingHandlersInstalled();
42
43 //----------------------------------------------------------------------------
44 // wxDropTarget
45 //----------------------------------------------------------------------------
46
47 wxDropTarget::wxDropTarget( wxDataObject *data )
48 : wxDropTargetBase( data )
49 {
50 wxMacEnsureTrackingHandlersInstalled();
51 }
52
53 wxDragResult wxDropTarget::OnDragOver(
54 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
55 wxDragResult def )
56 {
57 return CurrentDragHasSupportedFormat() ? def : wxDragNone;
58 }
59
60 wxDataFormat wxDropTarget::GetMatchingPair()
61 {
62 wxFAIL_MSG("wxDropTarget::GetMatchingPair() not implemented in src/osx/carbon/dnd.cpp");
63 return wxDF_INVALID;
64 }
65
66 bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
67 {
68 if (m_dataObject == NULL)
69 return false;
70
71 return CurrentDragHasSupportedFormat();
72 }
73
74 wxDragResult wxDropTarget::OnData(
75 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
76 wxDragResult def )
77 {
78 if (m_dataObject == NULL)
79 return wxDragNone;
80
81 if (!CurrentDragHasSupportedFormat())
82 return wxDragNone;
83
84 return GetData() ? def : wxDragNone;
85 }
86
87 bool wxDropTarget::CurrentDragHasSupportedFormat()
88 {
89 bool supported = false;
90 if (m_dataObject == NULL)
91 return false;
92
93 if ( gTrackingGlobals.m_currentSource != NULL )
94 {
95 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject();
96
97 if ( data )
98 {
99 size_t formatcount = data->GetFormatCount();
100 wxDataFormat *array = new wxDataFormat[formatcount];
101 data->GetAllFormats( array );
102 for (size_t i = 0; !supported && i < formatcount; i++)
103 {
104 wxDataFormat format = array[i];
105 if ( m_dataObject->IsSupported( format ) )
106 {
107 supported = true;
108 break;
109 }
110 }
111
112 delete [] array;
113 }
114 }
115
116 if ( !supported )
117 {
118 supported = m_dataObject->HasDataInPasteboard( m_currentDragPasteboard );
119 }
120
121 return supported;
122 }
123
124 bool wxDropTarget::GetData()
125 {
126 if (m_dataObject == NULL)
127 return false;
128
129 if ( !CurrentDragHasSupportedFormat() )
130 return false;
131
132 bool transferred = false;
133 if ( gTrackingGlobals.m_currentSource != NULL )
134 {
135 wxDataObject* data = gTrackingGlobals.m_currentSource->GetDataObject();
136
137 if (data != NULL)
138 {
139 size_t formatcount = data->GetFormatCount();
140 wxDataFormat *array = new wxDataFormat[formatcount];
141 data->GetAllFormats( array );
142 for (size_t i = 0; !transferred && i < formatcount; i++)
143 {
144 wxDataFormat format = array[i];
145 if ( m_dataObject->IsSupported( format ) )
146 {
147 int size = data->GetDataSize( format );
148 transferred = true;
149
150 if (size == 0)
151 {
152 m_dataObject->SetData( format, 0, 0 );
153 }
154 else
155 {
156 char *d = new char[size];
157 data->GetDataHere( format, (void*)d );
158 m_dataObject->SetData( format, size, d );
159 delete [] d;
160 }
161 }
162 }
163
164 delete [] array;
165 }
166 }
167
168 if ( !transferred )
169 {
170 transferred = m_dataObject->GetFromPasteboard( m_currentDragPasteboard );
171 }
172
173 return transferred;
174 }
175
176 //-------------------------------------------------------------------------
177 // wxDropSource
178 //-------------------------------------------------------------------------
179
180 //-----------------------------------------------------------------------------
181 // drag request
182
183 wxDropSource::wxDropSource(wxWindow *win,
184 const wxCursor &cursorCopy,
185 const wxCursor &cursorMove,
186 const wxCursor &cursorStop)
187 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
188 {
189 wxMacEnsureTrackingHandlersInstalled();
190
191 m_window = win;
192 }
193
194 wxDropSource::wxDropSource(wxDataObject& data,
195 wxWindow *win,
196 const wxCursor &cursorCopy,
197 const wxCursor &cursorMove,
198 const wxCursor &cursorStop)
199 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
200 {
201 wxMacEnsureTrackingHandlersInstalled();
202
203 SetData( data );
204 m_window = win;
205 }
206
207 wxDropSource::~wxDropSource()
208 {
209 }
210
211 OSStatus wxMacPromiseKeeper(PasteboardRef WXUNUSED(inPasteboard),
212 PasteboardItemID WXUNUSED(inItem),
213 CFStringRef WXUNUSED(inFlavorType),
214 void * WXUNUSED(inContext))
215 {
216 OSStatus err = noErr;
217
218 // we might add promises here later, inContext is the wxDropSource*
219
220 return err;
221 }
222
223 wxDragResult wxDropSource::DoDragDrop(int flags)
224 {
225 wxASSERT_MSG( m_data, wxT("Drop source: no data") );
226
227 if ((m_data == NULL) || (m_data->GetFormatCount() == 0))
228 return (wxDragResult)wxDragNone;
229
230 #if wxOSX_USE_CARBON
231 DragReference theDrag;
232 RgnHandle dragRegion;
233 OSStatus err = noErr;
234 PasteboardRef pasteboard;
235
236 // add data to drag
237
238 err = PasteboardCreate( kPasteboardUniqueName, &pasteboard );
239 if ( err != noErr )
240 return wxDragNone;
241
242 // we add a dummy promise keeper because of strange messages when linking against carbon debug
243 err = PasteboardSetPromiseKeeper( pasteboard, wxMacPromiseKeeper, this );
244 if ( err != noErr )
245 {
246 CFRelease( pasteboard );
247 return wxDragNone;
248 }
249
250 err = PasteboardClear( pasteboard );
251 if ( err != noErr )
252 {
253 CFRelease( pasteboard );
254 return wxDragNone;
255 }
256 PasteboardSynchronize( pasteboard );
257
258 m_data->AddToPasteboard( pasteboard, 1 );
259
260 if (NewDragWithPasteboard( pasteboard , &theDrag) != noErr)
261 {
262 CFRelease( pasteboard );
263 return wxDragNone;
264 }
265
266 dragRegion = NewRgn();
267 RgnHandle tempRgn = NewRgn();
268
269 EventRecord rec;
270 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent(), &rec );
271
272 const short dragRegionOuterBoundary = 10;
273 const short dragRegionInnerBoundary = 9;
274
275 SetRectRgn(
276 dragRegion,
277 rec.where.h - dragRegionOuterBoundary,
278 rec.where.v - dragRegionOuterBoundary,
279 rec.where.h + dragRegionOuterBoundary,
280 rec.where.v + dragRegionOuterBoundary );
281
282 SetRectRgn(
283 tempRgn,
284 rec.where.h - dragRegionInnerBoundary,
285 rec.where.v - dragRegionInnerBoundary,
286 rec.where.h + dragRegionInnerBoundary,
287 rec.where.v + dragRegionInnerBoundary );
288
289 DiffRgn( dragRegion, tempRgn, dragRegion );
290 DisposeRgn( tempRgn );
291
292 // TODO: work with promises in order to return data
293 // only when drag was successfully completed
294
295 gTrackingGlobals.m_currentSource = this;
296 gTrackingGlobals.m_result = wxDragNone;
297 gTrackingGlobals.m_flags = flags;
298
299 err = TrackDrag( theDrag, &rec, dragRegion );
300
301 DisposeRgn( dragRegion );
302 DisposeDrag( theDrag );
303 CFRelease( pasteboard );
304 gTrackingGlobals.m_currentSource = NULL;
305 #else
306 wxUnusedVar(flags);
307 #endif
308
309 return gTrackingGlobals.m_result;
310 }
311
312 bool wxDropSource::MacInstallDefaultCursor(wxDragResult effect)
313 {
314 const wxCursor& cursor = GetCursor(effect);
315 bool result = cursor.Ok();
316
317 if ( result )
318 cursor.MacInstall();
319
320 return result;
321 }
322
323 bool gTrackingGlobalsInstalled = false;
324
325 // passing the globals via refcon is not needed by the CFM and later architectures anymore
326 // but I'll leave it in there, just in case...
327
328 #if wxOSX_USE_CARBON
329 pascal OSErr wxMacWindowDragTrackingHandler(
330 DragTrackingMessage theMessage, WindowPtr theWindow,
331 void *handlerRefCon, DragReference theDrag );
332 pascal OSErr wxMacWindowDragReceiveHandler(
333 WindowPtr theWindow, void *handlerRefCon,
334 DragReference theDrag );
335 #endif
336
337 void wxMacEnsureTrackingHandlersInstalled()
338 {
339 #if wxOSX_USE_CARBON
340 if ( !gTrackingGlobalsInstalled )
341 {
342 OSStatus err;
343
344 err = InstallTrackingHandler( NewDragTrackingHandlerUPP(wxMacWindowDragTrackingHandler), 0L, &gTrackingGlobals );
345 verify_noerr( err );
346
347 err = InstallReceiveHandler( NewDragReceiveHandlerUPP(wxMacWindowDragReceiveHandler), 0L, &gTrackingGlobals );
348 verify_noerr( err );
349
350 gTrackingGlobalsInstalled = true;
351 }
352 #endif
353 }
354
355 #if wxOSX_USE_CARBON
356 pascal OSErr wxMacWindowDragTrackingHandler(
357 DragTrackingMessage theMessage, WindowPtr theWindow,
358 void *handlerRefCon, DragReference theDrag )
359 {
360 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*) handlerRefCon;
361
362 Point mouse, localMouse;
363 DragAttributes attributes;
364
365 GetDragAttributes( theDrag, &attributes );
366 PasteboardRef pasteboard = 0;
367 GetDragPasteboard( theDrag, &pasteboard );
368 wxNonOwnedWindow* toplevel = wxNonOwnedWindow::GetFromWXWindow( (WXWindow) theWindow );
369
370 bool optionDown = GetCurrentKeyModifiers() & optionKey;
371 wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
372
373 switch (theMessage)
374 {
375 case kDragTrackingEnterHandler:
376 case kDragTrackingLeaveHandler:
377 break;
378
379 case kDragTrackingEnterWindow:
380 if (trackingGlobals != NULL)
381 {
382 trackingGlobals->m_currentTargetWindow = NULL;
383 trackingGlobals->m_currentTarget = NULL;
384 }
385 break;
386
387 case kDragTrackingInWindow:
388 if (trackingGlobals == NULL)
389 break;
390 if (toplevel == NULL)
391 break;
392
393 GetDragMouse( theDrag, &mouse, 0L );
394 int x = mouse.h ;
395 int y = mouse.v ;
396 toplevel->GetNonOwnedPeer()->ScreenToWindow( &x, &y );
397 localMouse.h = x;
398 localMouse.v = y;
399
400 {
401 wxWindow *win = NULL;
402 ControlPartCode controlPart;
403 ControlRef control = FindControlUnderMouse( localMouse, theWindow, &controlPart );
404 if ( control )
405 win = wxFindWindowFromWXWidget( (WXWidget) control );
406 else
407 win = toplevel;
408
409 int localx, localy;
410 localx = localMouse.h;
411 localy = localMouse.v;
412
413 if ( win )
414 win->MacRootWindowToWindow( &localx, &localy );
415 if ( win != trackingGlobals->m_currentTargetWindow )
416 {
417 if ( trackingGlobals->m_currentTargetWindow )
418 {
419 // this window is left
420 if ( trackingGlobals->m_currentTarget )
421 {
422 #ifndef __LP64__
423 HideDragHilite( theDrag );
424 #endif
425 trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard );
426 trackingGlobals->m_currentTarget->OnLeave();
427 trackingGlobals->m_currentTarget = NULL;
428 trackingGlobals->m_currentTargetWindow = NULL;
429 }
430 }
431
432 if ( win )
433 {
434 // this window is entered
435 trackingGlobals->m_currentTargetWindow = win;
436 trackingGlobals->m_currentTarget = win->GetDropTarget();
437 {
438 if ( trackingGlobals->m_currentTarget )
439 {
440 trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard );
441 result = trackingGlobals->m_currentTarget->OnEnter( localx, localy, result );
442 }
443
444 if ( result != wxDragNone )
445 {
446 int x, y;
447
448 x = y = 0;
449 win->MacWindowToRootWindow( &x, &y );
450 RgnHandle hiliteRgn = NewRgn();
451 Rect r = { y, x, y + win->GetSize().y, x + win->GetSize().x };
452 RectRgn( hiliteRgn, &r );
453 #ifndef __LP64__
454 ShowDragHilite( theDrag, hiliteRgn, true );
455 #endif
456 DisposeRgn( hiliteRgn );
457 }
458 }
459 }
460 }
461 else
462 {
463 if ( trackingGlobals->m_currentTarget )
464 {
465 trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard );
466 result = trackingGlobals->m_currentTarget->OnDragOver( localx, localy, result );
467 }
468 }
469
470 // set cursor for OnEnter and OnDragOver
471 if ( trackingGlobals->m_currentSource && !trackingGlobals->m_currentSource->GiveFeedback( result ) )
472 {
473 if ( !trackingGlobals->m_currentSource->MacInstallDefaultCursor( result ) )
474 {
475 wxStockCursor cursorID = wxCURSOR_NONE;
476
477 switch (result)
478 {
479 case wxDragCopy:
480 cursorID = wxCURSOR_COPY_ARROW;
481 break;
482
483 case wxDragMove:
484 cursorID = wxCURSOR_ARROW;
485 break;
486
487 case wxDragNone:
488 cursorID = wxCURSOR_NO_ENTRY;
489 break;
490
491 case wxDragError:
492 case wxDragLink:
493 case wxDragCancel:
494 default:
495 // put these here to make gcc happy
496 ;
497 }
498
499 if (cursorID != wxCURSOR_NONE)
500 {
501 wxCursor cursor( cursorID );
502 cursor.MacInstall();
503 }
504 }
505 }
506 }
507 break;
508
509 case kDragTrackingLeaveWindow:
510 if (trackingGlobals == NULL)
511 break;
512
513 if (trackingGlobals->m_currentTarget)
514 {
515 trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard );
516 trackingGlobals->m_currentTarget->OnLeave();
517 #ifndef __LP64__
518 HideDragHilite( theDrag );
519 #endif
520 trackingGlobals->m_currentTarget = NULL;
521 }
522 trackingGlobals->m_currentTargetWindow = NULL;
523 break;
524
525 default:
526 break;
527 }
528
529 return noErr;
530 }
531
532 pascal OSErr wxMacWindowDragReceiveHandler(
533 WindowPtr theWindow,
534 void *handlerRefCon,
535 DragReference theDrag)
536 {
537 MacTrackingGlobals* trackingGlobals = (MacTrackingGlobals*)handlerRefCon;
538 if ( trackingGlobals->m_currentTarget )
539 {
540 Point mouse, localMouse;
541 int localx, localy;
542
543 PasteboardRef pasteboard = 0;
544 GetDragPasteboard( theDrag, &pasteboard );
545 trackingGlobals->m_currentTarget->SetCurrentDragPasteboard( pasteboard );
546 GetDragMouse( theDrag, &mouse, 0L );
547 localMouse = mouse;
548 localx = localMouse.h;
549 localy = localMouse.v;
550 wxNonOwnedWindow* tlw = wxNonOwnedWindow::GetFromWXWindow((WXWindow) theWindow);
551 if ( tlw )
552 tlw->GetNonOwnedPeer()->ScreenToWindow( &localx, &localy );
553
554 // TODO : should we use client coordinates?
555 if ( trackingGlobals->m_currentTargetWindow )
556 trackingGlobals->m_currentTargetWindow->MacRootWindowToWindow( &localx, &localy );
557 if ( trackingGlobals->m_currentTarget->OnDrop( localx, localy ) )
558 {
559 // the option key indicates copy in Mac UI, if it's not pressed do
560 // move by default if it's allowed at all
561 wxDragResult
562 result = !(trackingGlobals->m_flags & wxDrag_AllowMove) ||
563 (GetCurrentKeyModifiers() & optionKey)
564 ? wxDragCopy
565 : wxDragMove;
566 trackingGlobals->m_result =
567 trackingGlobals->m_currentTarget->OnData( localx, localy, result );
568 }
569 }
570
571 return noErr;
572 }
573 #endif
574
575 #endif // wxUSE_DRAG_AND_DROP
576