[Obj-C++] Drag & Drop 実装 01 : mouseDownメソッド

Drag & Dropの実装はApple公式ドキュメントのコードが古くなっていることもあり一筋縄ではいきません。一つ一つ解決していきます。

NSString *filePath1, *filePath2; // Assume these exist
 
- (void)mouseDown:(NSEvent *)theEvent
{
    NSImage *dragImage;
    NSPoint dragPosition;
 
    // Write data to the pasteboard
    NSArray *fileList = [NSArray arrayWithObjects:filePath1, filePath2, nil];
    NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
    [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
            owner:nil];
    [pboard setPropertyList:fileList forType:NSFilenamesPboardType];
 
    // Start the drag operation
    dragImage = [[NSWorkspace sharedWorkspace] iconForFile:filePath1];
    dragPosition = [self convertPoint:[theEvent locationInWindow]
                        fromView:nil];
    dragPosition.x -= 16;
    dragPosition.y -= 16;
    [self dragImage:dragImage
            at:dragPosition
            offset:NSZeroSize
            event:theEvent
            pasteboard:pboard
            source:self
            slideBack:YES];
}
警告1
/XlsxConvertor.mm:292:61: warning: 
'NSDragPboard' is deprecated: first deprecated in macOS 10.13 
[-Wdeprecated-declarations]
    NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
                                                            ^~~~~~~~~~~~
                                                            NSPasteboardNameDrag
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPasteboard.h:338:32: note: 'NSDragPboard' has been explicitly marked deprecated here
APPKIT_EXTERN NSPasteboardName NSDragPboard API_DEPRECATED_WITH_REPLACEMENT("NSPasteboardNameDrag", macos(10.0,10.13));
                               ^
警告2
/XlsxConvertor.mm:293:51: warning: 'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 - Create multiple pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead [-Wdeprecated-declarations]
    [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
                                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPasteboard.h:312:32: note:
'NSFilenamesPboardType' has been explicitly marked deprecated here
APPKIT_EXTERN NSPasteboardType NSFilenamesPboardType API_DEPRECATED("Create multiple pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead", macos(10.0,10.14));
                               ^
警告3
/XlsxConvertor.mm:295:46: warning: 
'NSFilenamesPboardType' is deprecated: first deprecated in macOS 10.14 
- Create multiple pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead [-Wdeprecated-declarations]
    [pboard setPropertyList:fileList forType:NSFilenamesPboardType];
                                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPasteboard.h:312:32: note: 
'NSFilenamesPboardType' has been explicitly marked deprecated here
APPKIT_EXTERN NSPasteboardType NSFilenamesPboardType API_DEPRECATED("Create multiple pasteboard items with NSPasteboardTypeFileURL or kUTTypeFileURL instead", macos(10.0,10.14));
                               ^
警告4
/XlsxConvertor.mm:299:26: warning: instance method '-convertPoint:fromView:' not found (return type defaults to 'id') [-Wobjc-method-access]
    dragPosition = [self convertPoint:[theEvent locationInWindow]
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
注意1
/XlsxConvertor.mm:7:12: note: receiver is instance of class declared here
@interface Window : NSWindow {
           ^
エラー1
/XlsxConvertor.mm:299:18: error: no viable overloaded '='
    dragPosition = [self convertPoint:[theEvent locationInWindow]
    ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGGeometry.h:20:1: note: 
candidate function (the implicit copy assignment operator) not viable: no known conversion from 'id' to 'const CGPoint' for 1st argument
CGPoint {
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGGeometry.h:20:1: note: 
candidate function (the implicit move assignment operator) not viable: no known conversion from 'id' to 'CGPoint' for 1st argument
4 warnings and 1 error generated.

Drag and Drop Programming Topics
Dragging Files