Revision: 14597
          http://sourceforge.net/p/skim-app/code/14597
Author:   hofman
Date:     2024-10-28 09:54:58 +0000 (Mon, 28 Oct 2024)
Log Message:
-----------
Drop image files to add image note

Modified Paths:
--------------
    trunk/SKPDFView.m

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2024-10-28 09:34:23 UTC (rev 14596)
+++ trunk/SKPDFView.m   2024-10-28 09:54:58 UTC (rev 14597)
@@ -299,7 +299,7 @@
     if ([self respondsToSelector:@selector(enableSwipeGestures:)])
         [self enableSwipeGestures:YES];
     
-    [self registerForDraggedTypes:@[NSPasteboardTypeColor, 
SKPasteboardTypeLineStyle]];
+    [self registerForDraggedTypes:@[NSPasteboardTypeColor, 
SKPasteboardTypeLineStyle, NSPasteboardTypeFileURL]];
     
     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
     [nc addObserver:self selector:@selector(handlePageChangedNotification:)
@@ -2153,7 +2153,7 @@
 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
     NSDragOperation dragOp = NSDragOperationNone;
     NSPasteboard *pboard = [sender draggingPasteboard];
-    if ([self hideNotes] == NO && [pboard 
canReadItemWithDataConformingToTypes:@[NSPasteboardTypeColor, 
SKPasteboardTypeLineStyle]]) {
+    if ([self hideNotes] == NO && ([pboard 
canReadItemWithDataConformingToTypes:@[NSPasteboardTypeColor, 
SKPasteboardTypeLineStyle]] || [pboard canReadObjectForClasses:@[[NSURL class]] 
options:@{NSPasteboardURLReadingFileURLsOnlyKey:@YES, 
NSPasteboardURLReadingContentsConformToTypesKey:@[(__bridge NSString 
*)kUTTypeImage]}])) {
         return [self draggingUpdated:sender];
     } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
         dragOp = [super draggingEntered:sender];
@@ -2184,6 +2184,10 @@
         }
         if (dragOp == NSDragOperationNone)
             [self setHighlightAnnotation:nil];
+    } else if ([self hideNotes] == NO && [pboard 
canReadObjectForClasses:@[[NSURL class]] 
options:@{NSPasteboardURLReadingFileURLsOnlyKey:@YES, 
NSPasteboardURLReadingContentsConformToTypesKey:@[(__bridge NSString 
*)kUTTypeImage]}]) {
+        NSPoint location = [self convertPoint:[sender draggingLocation] 
fromView:nil];
+        if ([self pageForPoint:location nearest:NO])
+            dragOp = NSDragOperationGeneric;
     } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
         dragOp = [super draggingUpdated:sender];
     }
@@ -2192,10 +2196,11 @@
 
 - (void)draggingExited:(id <NSDraggingInfo>)sender {
     NSPasteboard *pboard = [sender draggingPasteboard];
-    if ([self hideNotes] == NO && [pboard 
canReadItemWithDataConformingToTypes:@[NSPasteboardTypeColor, 
SKPasteboardTypeLineStyle]])
+    if ([self hideNotes] == NO && ([pboard 
canReadItemWithDataConformingToTypes:@[NSPasteboardTypeColor, 
SKPasteboardTypeLineStyle]] || [pboard canReadObjectForClasses:@[[NSURL class]] 
options:@{NSPasteboardURLReadingFileURLsOnlyKey:@YES, 
NSPasteboardURLReadingContentsConformToTypesKey:@[(__bridge NSString 
*)kUTTypeImage]}])) {
         [self setHighlightAnnotation:nil];
-    else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd])
+    } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
         [super draggingExited:sender];
+    }
 }
 
 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
@@ -2227,6 +2232,33 @@
             }
             [self setHighlightAnnotation:nil];
         }
+    } else if ([self hideNotes] == NO && [pboard 
canReadObjectForClasses:@[[NSURL class]] 
options:@{NSPasteboardURLReadingFileURLsOnlyKey:@YES, 
NSPasteboardURLReadingContentsConformToTypesKey:@[(__bridge NSString 
*)kUTTypeImage]}]) {
+        NSPoint location = [self convertPoint:[sender draggingLocation] 
fromView:nil];
+        PDFPage *page = [self pageForPoint:location nearest:NO];
+        if (page) {
+            location = [self convertPoint:location toPage:page];
+            NSURL *fileURL =  [[pboard readObjectsForClasses:@[[NSURL class]] 
options:@{NSPasteboardURLReadingFileURLsOnlyKey:@YES, 
NSPasteboardURLReadingContentsConformToTypesKey:@[(__bridge NSString 
*)kUTTypeImage]}] firstObject];
+            NSImage *image = fileURL ? [[NSImage alloc] 
initWithContentsOfURL:fileURL] : nil;
+            if (image) {
+                NSRect bounds = SKRectFromCenterAndSize(location, [image 
size]);
+                bounds.origin = SKIntegralPoint(bounds.origin);
+                bounds = SKConstrainRect(bounds, [page boundsForBox:[self 
displayBox]]);
+                
+                PDFAnnotation *newAnnotation = [PDFAnnotation 
newSkimNoteWithBounds:bounds forType:SKNNoteString];
+                [(SKNPDFAnnotationNote *)newAnnotation setImage:image];
+                [(SKNPDFAnnotationNote *)newAnnotation 
setExtendedIconType:kSKNPDFTextAnnotationIconImage];
+                
+                [newAnnotation registerUserName];
+                [self beginNewUndoGroupIfNeededWithCommit:YES];
+                [[self document] addAnnotation:newAnnotation toPage:page];
+                [self setUndoActionName:NSLocalizedString(@"Add Note", @"Undo 
action name")];
+
+                if (toolMode == SKToolModeText || toolMode == SKToolModeNote)
+                    [self setCurrentAnnotation:newAnnotation];
+                
+                performedDrag = YES;
+            }
+        }
     } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
         performedDrag = [super performDragOperation:sender];
     }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to