Refactor some DND code in wxOSX/Cocoa

To reduce duplication of the same changes, merge the nearly identical
wxWidgetCocoaImpl::draggingEntered and draggingUpdated into private
function wxOnDraggingEnteredOrUpdated.
This commit is contained in:
Dimitri Schoolwerth
2015-05-27 18:52:54 +04:00
parent 35a68ef4ce
commit ddbfe5720e

View File

@@ -1164,13 +1164,18 @@ void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
} }
#if wxUSE_DRAG_AND_DROP #if wxUSE_DRAG_AND_DROP
unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
namespace
{
unsigned int wxOnDraggingEnteredOrUpdated(wxWidgetCocoaImpl* viewImpl,
void *s, bool entered)
{ {
id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
NSPasteboard *pboard = [sender draggingPasteboard]; NSPasteboard *pboard = [sender draggingPasteboard];
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
wxWindow* wxpeer = GetWXPeer(); wxWindow* wxpeer = viewImpl->GetWXPeer();
if ( wxpeer == NULL ) if ( wxpeer == NULL )
return NSDragOperationNone; return NSDragOperationNone;
@@ -1179,8 +1184,8 @@ unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf),
return NSDragOperationNone; return NSDragOperationNone;
wxDragResult result = wxDragNone; wxDragResult result = wxDragNone;
NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil]; NSPoint nspoint = [viewImpl->GetWXWidget() convertPoint:[sender draggingLocation] fromView:nil];
wxPoint pt = wxFromNSPoint( m_osxView, nspoint ); wxPoint pt = wxFromNSPoint( viewImpl->GetWXWidget(), nspoint );
if ( sourceDragMask & NSDragOperationLink ) if ( sourceDragMask & NSDragOperationLink )
result = wxDragLink; result = wxDragLink;
@@ -1192,7 +1197,17 @@ unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf),
PasteboardRef pboardRef; PasteboardRef pboardRef;
PasteboardCreate((CFStringRef)[pboard name], &pboardRef); PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
target->SetCurrentDragPasteboard(pboardRef); target->SetCurrentDragPasteboard(pboardRef);
result = target->OnEnter(pt.x, pt.y, result); if (entered)
{
// Drag entered
result = target->OnEnter(pt.x, pt.y, result);
}
else
{
// Drag updated
result = target->OnDragOver(pt.x, pt.y, result);
}
CFRelease(pboardRef); CFRelease(pboardRef);
NSDragOperation nsresult = NSDragOperationNone; NSDragOperation nsresult = NSDragOperationNone;
@@ -1210,6 +1225,13 @@ unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf),
return nsresult; return nsresult;
} }
} // anonymous namespace
unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
{
return wxOnDraggingEnteredOrUpdated(this, s, true /*entered*/);
}
void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
{ {
id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
@@ -1232,48 +1254,7 @@ void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WX
unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
{ {
id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s; return wxOnDraggingEnteredOrUpdated(this, s, false /*updated*/);
NSPasteboard *pboard = [sender draggingPasteboard];
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
wxWindow* wxpeer = GetWXPeer();
if ( wxpeer == NULL )
return NSDragOperationNone;
wxDropTarget* target = wxpeer->GetDropTarget();
if ( target == NULL )
return NSDragOperationNone;
wxDragResult result = wxDragNone;
NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
if ( sourceDragMask & NSDragOperationLink )
result = wxDragLink;
else if ( sourceDragMask & NSDragOperationCopy )
result = wxDragCopy;
else if ( sourceDragMask & NSDragOperationMove )
result = wxDragMove;
PasteboardRef pboardRef;
PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
target->SetCurrentDragPasteboard(pboardRef);
result = target->OnDragOver(pt.x, pt.y, result);
CFRelease(pboardRef);
NSDragOperation nsresult = NSDragOperationNone;
switch (result )
{
case wxDragLink:
nsresult = NSDragOperationLink;
case wxDragMove:
nsresult = NSDragOperationMove;
case wxDragCopy:
nsresult = NSDragOperationCopy;
default :
break;
}
return nsresult;
} }
bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd)) bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))