]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/dnd.mm
always use hw-accel, fixes #15536, applied with thanks
[wxWidgets.git] / src / osx / cocoa / dnd.mm
index 4429d80708eb1ec62c52e465b31c2c4770a1e28c..47faae9b16414af54576e430370c67d63a66fd0c 100644 (file)
@@ -4,32 +4,39 @@
 // Author:      Stefan Csomor
 // Modified by:
 // Created:     1998-01-01
-// RCS-ID:      $Id: dnd.cpp 61724 2009-08-21 10:41:26Z VZ $
 // Copyright:   (c) 1998 Stefan Csomor
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "wx/wxprec.h"
 
+#ifndef WX_PRECOMP
+#include "wx/object.h"
+#endif
+
 #if wxUSE_DRAG_AND_DROP
 
 #include "wx/dnd.h"
 
 #ifndef WX_PRECOMP
     #include "wx/app.h"
-    #include "wx/evtloop.h"
     #include "wx/toplevel.h"
     #include "wx/gdicmn.h"
     #include "wx/wx.h"
 #endif // WX_PRECOMP
 
-#include <AppKit/AppKit.h>
+#include "wx/evtloop.h"
+
 #include "wx/osx/private.h"
 
+wxDropSource* gCurrentSource = NULL;
+
 wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
 {
     switch (code)
     {
+        case NSDragOperationGeneric:
+            return wxDragCopy;
         case NSDragOperationCopy:
             return wxDragCopy;
         case NSDragOperationMove:
@@ -41,6 +48,7 @@ wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
         default:
             wxFAIL_MSG("Unexpected result code");
     }
+    return wxDragNone;
 }
 
 @interface DropSourceDelegate : NSObject
@@ -53,6 +61,7 @@ wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
 - (void)setImplementation: (wxDropSource *)dropSource;
 - (BOOL)finished;
 - (NSDragOperation)code;
+- (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint;
 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation;
 @end
 
@@ -60,7 +69,7 @@ wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
 
 - (id)init
 {
-    [super init];
+    self = [super init];
     dragFinished = NO;
     resultCode = NSDragOperationNone;
     impl = 0;
@@ -82,8 +91,60 @@ wxDragResult NSDragOperationToWxDragResult(NSDragOperation code)
     return resultCode;
 }
 
+- (void)draggedImage:(NSImage *)anImage movedTo:(NSPoint)aPoint
+{
+    wxUnusedVar( anImage );
+    wxUnusedVar( aPoint );
+    
+    bool optionDown = GetCurrentKeyModifiers() & optionKey;
+    wxDragResult result = optionDown ? wxDragCopy : wxDragMove;
+    
+    if (wxDropSource* source = impl)
+    {
+        if (!source->GiveFeedback(result))
+        {
+            wxStockCursor cursorID = wxCURSOR_NONE;
+
+            switch (result)
+            {
+                case wxDragCopy:
+                    cursorID = wxCURSOR_COPY_ARROW;
+                    break;
+
+                case wxDragMove:
+                    cursorID = wxCURSOR_ARROW;
+                    break;
+
+                case wxDragNone:
+                    cursorID = wxCURSOR_NO_ENTRY;
+                    break;
+
+                case wxDragError:
+                case wxDragLink:
+                case wxDragCancel:
+                default:
+                    // put these here to make gcc happy
+                    ;
+            }
+
+            if (cursorID != wxCURSOR_NONE)
+            {
+                // TODO under 10.6 the os itself deals with the cursor, remove if things
+                // work properly everywhere
+#if 0
+                wxCursor cursor( cursorID );
+                cursor.MacInstall();
+#endif
+            }
+        }
+    }
+}
+
 - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
 {
+    wxUnusedVar( anImage );
+    wxUnusedVar( aPoint );
+    
     resultCode = operation;
     dragFinished = YES;
 }
@@ -96,25 +157,6 @@ wxDropTarget::wxDropTarget( wxDataObject *data )
 
 }
 
-bool wxDropTarget::CurrentDragHasSupportedFormat()
-{
-    if (m_dataObject == NULL)
-        return false;
-
-    return m_dataObject->HasDataInPasteboard( m_currentDragPasteboard );
-}
-
-bool wxDropTarget::GetData()
-{
-    if (m_dataObject == NULL)
-        return false;
-
-    if ( !CurrentDragHasSupportedFormat() )
-        return false;
-
-    return m_dataObject->GetFromPasteboard( m_currentDragPasteboard );
-}
-
 //-------------------------------------------------------------------------
 // wxDropSource
 //-------------------------------------------------------------------------
@@ -139,7 +181,12 @@ wxDropSource::wxDropSource(wxDataObject& data,
     m_window = win;
 }
 
-wxDragResult wxDropSource::DoDragDrop(int flags)
+wxDropSource* wxDropSource::GetCurrentDropSource()
+{
+    return gCurrentSource;
+}
+
+wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
 {
     wxASSERT_MSG( m_data, wxT("Drop source: no data") );
 
@@ -170,11 +217,31 @@ wxDragResult wxDropSource::DoDragDrop(int flags)
         
         NSEvent* theEvent = (NSEvent*)wxTheApp->MacGetCurrentEvent();
         wxASSERT_MSG(theEvent, "DoDragDrop must be called in response to a mouse down or drag event.");
+
+        NSPoint down = [theEvent locationInWindow];
+        NSPoint p = [view convertPoint:down fromView:nil];
+                
+        gCurrentSource = this;
+        
+        // add a dummy square as dragged image for the moment, 
+        // TODO: proper drag image for data
+        NSSize sz = NSMakeSize(16,16);
+        NSRect fillRect = NSMakeRect(0, 0, 16, 16);
+        NSImage* image = [[NSImage alloc] initWithSize: sz];
+        [image lockFocus];
+        
+        [[[NSColor whiteColor] colorWithAlphaComponent:0.8] set];
+        NSRectFill(fillRect);
+        [[NSColor blackColor] set];
+        NSFrameRectWithWidthUsingOperation(fillRect,1.0f,NSCompositeDestinationOver);
+        
+        [image unlockFocus];        
+        
         
-        NSImage* image = [[NSImage alloc] initWithSize: NSMakeSize(16,16)];
         DropSourceDelegate* delegate = [[DropSourceDelegate alloc] init];
         [delegate setImplementation: this];
-        [view dragImage:image at:NSMakePoint(0.0, 16.0) offset:NSMakeSize(0.0,0.0) 
+        [view dragImage:image at:p offset:NSMakeSize(0.0,0.0) 
             event: theEvent pasteboard: pboard source:delegate slideBack: NO];
         
         wxEventLoopBase * const loop = wxEventLoop::GetActive();
@@ -184,6 +251,24 @@ wxDragResult wxDropSource::DoDragDrop(int flags)
         result = NSDragOperationToWxDragResult([delegate code]);
         [delegate release];
         [image release];
+        
+        wxWindow* mouseUpTarget = wxWindow::GetCapture();
+
+        if ( mouseUpTarget == NULL )
+        {
+            mouseUpTarget = m_window;
+        }
+        
+        if ( mouseUpTarget != NULL )
+        {
+            wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
+            ((wxWidgetCocoaImpl*)mouseUpTarget->GetPeer())->SetupMouseEvent(wxevent , theEvent) ;
+            wxevent.SetEventType(wxEVT_LEFT_UP);
+        
+            mouseUpTarget->HandleWindowEvent(wxevent);
+        }
+        
+        gCurrentSource = NULL;
     }