Revision: 13424
          http://sourceforge.net/p/skim-app/code/13424
Author:   hofman
Date:     2023-04-28 15:23:22 +0000 (Fri, 28 Apr 2023)
Log Message:
-----------
Allow selecting multiple pages in overview. Drag and copy multiple page pdf 
data when selecting multiple pages.

Modified Paths:
--------------
    trunk/PDFPage_SKExtensions.h
    trunk/PDFPage_SKExtensions.m
    trunk/SKMainWindowController.m
    trunk/SKMainWindowController_UI.m
    trunk/SKThumbnailView.m

Modified: trunk/PDFPage_SKExtensions.h
===================================================================
--- trunk/PDFPage_SKExtensions.h        2023-04-20 09:15:32 UTC (rev 13423)
+++ trunk/PDFPage_SKExtensions.h        2023-04-28 15:23:22 UTC (rev 13424)
@@ -73,8 +73,8 @@
 - (NSData *)PDFDataForRect:(NSRect)rect;
 - (NSData *)TIFFDataForRect:(NSRect)rect;
 
-- (id<NSPasteboardWriting>)filePromise;
-- (void)writeToClipboard;
+- (id<NSPasteboardWriting>)filePromiseForPageIndexes:(NSIndexSet *)pageIndexes;
+- (void)writeToClipboardForPageIndexes:(NSIndexSet *)pageIndexes;
 
 - (NSURL *)skimURL;
 

Modified: trunk/PDFPage_SKExtensions.m
===================================================================
--- trunk/PDFPage_SKExtensions.m        2023-04-20 09:15:32 UTC (rev 13423)
+++ trunk/PDFPage_SKExtensions.m        2023-04-28 15:23:22 UTC (rev 13424)
@@ -75,6 +75,8 @@
 #define SKAutoCropBoxMarginWidthKey @"SKAutoCropBoxMarginWidth"
 #define SKAutoCropBoxMarginHeightKey @"SKAutoCropBoxMarginHeight"
 
+#define SKPasteboardTypePageIndexes 
@"net.sourceforge.skim-app.pasteboard.page-indexes"
+
 @implementation PDFPage (SKExtensions) 
 
 - (void)fallback_transformContext:(CGContextRef)context 
forBox:(PDFDisplayBox)box {
@@ -310,6 +312,23 @@
     return [image TIFFRepresentation];
 }
 
+- (NSData *)dataRepresentationForPageIndexes:(NSIndexSet *)pageIndexes {
+    NSData *data = nil;
+    if (pageIndexes) {
+        PDFDocument *pdfDoc = [[PDFDocument alloc] init];
+        [pageIndexes enumerateIndexesUsingBlock:^(NSUInteger i, BOOL *stop){
+            PDFPage *aPage = [[[self document] pageAtIndex:i] copy];
+            [pdfDoc insertPage:aPage atIndex:[pdfDoc pageCount]];
+            [aPage release];
+        }];
+        data = [pdfDoc dataRepresentation];
+        [pdfDoc release];
+    } else {
+        data = [self dataRepresentation];
+    }
+    return data;
+}
+
 // the page is set as owner in -filePromise
 - (void)pasteboard:(NSPasteboard *)pboard item:(NSPasteboardItem *)item 
provideDataForType:(NSString *)type {
     if ([type isEqualToString:(NSString *)kPasteboardTypeFileURLPromise]) {
@@ -321,7 +340,11 @@
         
         if ([[self document] allowsPrinting]) {
             pathExt = @"pdf";
-            data = [self dataRepresentation];
+            NSIndexSet *pageIndexes = nil;
+            NSData *indexData = [item dataForType:SKPasteboardTypePageIndexes];
+            if (indexData)
+                pageIndexes = [NSKeyedUnarchiver 
unarchiveObjectWithData:indexData];
+            data = [self dataRepresentationForPageIndexes:pageIndexes];
         } else {
             pathExt = @"tiff";
             data = [self TIFFDataForRect:[self 
boundsForBox:kPDFDisplayBoxCropBox]];
@@ -331,7 +354,11 @@
         if ([data writeToURL:fileURL atomically:YES])
             [item setString:[fileURL absoluteString] forType:type];
     } else if ([type isEqualToString:NSPasteboardTypePDF]) {
-        [item setData:[self dataRepresentation] forType:type];
+        NSIndexSet *pageIndexes = nil;
+        NSData *indexData = [item dataForType:SKPasteboardTypePageIndexes];
+        if (indexData)
+            pageIndexes = [NSKeyedUnarchiver 
unarchiveObjectWithData:indexData];
+        [item setData:[self dataRepresentationForPageIndexes:pageIndexes] 
forType:type];
     } else if ([type isEqualToString:NSPasteboardTypeTIFF]) {
         [item setData:[self TIFFDataForRect:[self 
boundsForBox:kPDFDisplayBoxCropBox]] forType:type];
     }
@@ -349,25 +376,31 @@
 - (void)filePromiseProvider:(NSFilePromiseProvider *)filePromiseProvider 
writePromiseToURL:(NSURL *)fileURL completionHandler:(void (^)(NSError 
*))completionHandler {
     NSData *data = nil;
     NSError *error = nil;
-    if ([[self document] allowsPrinting])
-        data = [self dataRepresentation];
-    else
+    if ([[self document] allowsPrinting]) {
+        NSIndexSet *pageIndexes = [filePromiseProvider userInfo];
+        data = [self dataRepresentationForPageIndexes:pageIndexes];
+    } else
         data = [self TIFFDataForRect:[self 
boundsForBox:kPDFDisplayBoxCropBox]];
     [data writeToURL:fileURL options:NSDataWritingAtomic error:&error];
     completionHandler(error);
 }
 
-- (id<NSPasteboardWriting>)filePromise {
+- (id<NSPasteboardWriting>)filePromiseForPageIndexes:(NSIndexSet *)pageIndexes 
{
     if ([[self document] isLocked] == NO) {
         NSString *fileUTI = [[self document] allowsPrinting] ? (NSString 
*)kUTTypePDF : (NSString *)kUTTypeTIFF;
         Class promiseClass = NSClassFromString(@"NSFilePromiseProvider");
         if (promiseClass) {
-            return [[[promiseClass alloc] initWithFileType:fileUTI 
delegate:self] autorelease];
+            NSFilePromiseProvider *item = [[[promiseClass alloc] 
initWithFileType:fileUTI delegate:self] autorelease];
+            if (pageIndexes)
+                [item setUserInfo:pageIndexes];
+            return item;
         } else {
             NSString *pdfType = [[self document] allowsPrinting] ? 
NSPasteboardTypePDF : nil;
             NSPasteboardItem *item = [[[NSPasteboardItem alloc] init] 
autorelease];
             [item setString:fileUTI forType:(NSString 
*)kPasteboardTypeFilePromiseContent];
             [item setDataProvider:self forTypes:@[(NSString 
*)kPasteboardTypeFileURLPromise, NSPasteboardTypeTIFF, pdfType]];
+            if (pageIndexes)
+                [item setData:[NSKeyedArchiver 
archivedDataWithRootObject:pageIndexes] forType:SKPasteboardTypePageIndexes];
             return item;
         }
     }
@@ -374,7 +407,7 @@
     return nil;
 }
 
-- (void)writeToClipboard {
+- (void)writeToClipboardForPageIndexes:(NSIndexSet *)pageIndexes {
     if ([[self document] isLocked] == NO) {
         NSData *tiffData = [self TIFFDataForRect:[self 
boundsForBox:kPDFDisplayBoxCropBox]];
         NSPasteboardItem *pboardItem = [[[NSPasteboardItem alloc] init] 
autorelease];

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2023-04-20 09:15:32 UTC (rev 13423)
+++ trunk/SKMainWindowController.m      2023-04-28 15:23:22 UTC (rev 13424)
@@ -1696,6 +1696,7 @@
         [scrollView release];
         [overviewView setItemPrototype:[[[SKThumbnailItem alloc] init] 
autorelease]];
         [overviewView setSelectable:YES];
+        [overviewView setAllowsMultipleSelection:YES];
         [self updateOverviewItemSize];
         [overviewView setContent:[self thumbnails]];
         [overviewView setSelectionIndexes:[NSIndexSet 
indexSetWithIndex:[[pdfView currentPage] pageIndex]]];

Modified: trunk/SKMainWindowController_UI.m
===================================================================
--- trunk/SKMainWindowController_UI.m   2023-04-20 09:15:32 UTC (rev 13423)
+++ trunk/SKMainWindowController_UI.m   2023-04-28 15:23:22 UTC (rev 13424)
@@ -397,7 +397,7 @@
     if ([tv isEqual:leftSideController.thumbnailTableView]) {
         if ([[pdfView document] isLocked] == NO) {
             PDFPage *page = [[pdfView document] pageAtIndex:row];
-            return [page filePromise];
+            return [page filePromiseForPageIndexes:nil];
         }
     } else if ([tv isEqual:rightSideController.snapshotTableView]) {
         SKSnapshotWindowController *snapshot = 
[[rightSideController.snapshotArrayController arrangedObjects] 
objectAtIndex:row];
@@ -545,7 +545,7 @@
         NSUInteger idx = [rowIndexes firstIndex];
         if (idx != NSNotFound && [[pdfView document] isLocked] == NO) {
             PDFPage *page = [[pdfView document] pageAtIndex:idx];
-            [page writeToClipboard];
+            [page writeToClipboardForPageIndexes:nil];
         }
     } else if ([tv isEqual:leftSideController.findTableView]) {
         NSMutableString *string = [NSMutableString string];

Modified: trunk/SKThumbnailView.m
===================================================================
--- trunk/SKThumbnailView.m     2023-04-20 09:15:32 UTC (rev 13423)
+++ trunk/SKThumbnailView.m     2023-04-28 15:23:22 UTC (rev 13424)
@@ -410,9 +410,15 @@
 - (void)mouseDown:(NSEvent *)theEvent {
     if ([NSApp willDragMouse]) {
         
-        id<NSPasteboardWriting> item = [[[self thumbnail] page] filePromise];
+        PDFPage *page = [[self thumbnail] page];
+        NSIndexSet *selectionIndexes = [[[self controller] collectionView] 
selectionIndexes];
+        if ([selectionIndexes count] < 2 || [selectionIndexes 
containsIndex:[page pageIndex]] == NO)
+            selectionIndexes = nil;
         
+        id<NSPasteboardWriting> item = [page 
filePromiseForPageIndexes:selectionIndexes];
+        
         if (item) {
+            
             NSRect rect = [imageView frame];
             NSBitmapImageRep *imageRep = [imageView 
bitmapImageRepCachingDisplayInRect:[imageView bounds]];
             NSImage *dragImage = [[[NSImage alloc] initWithSize:rect.size] 
autorelease];
@@ -431,16 +437,40 @@
 }
 
 - (void)copyPage:(id)sender {
-    [[[self thumbnail] page] writeToClipboard];
+    PDFPage *page = [[self thumbnail] page];
+    NSIndexSet *selectionIndexes = [[[self controller] collectionView] 
selectionIndexes];
+    if ([selectionIndexes count] < 2 || [selectionIndexes containsIndex:[page 
pageIndex]] == NO)
+        selectionIndexes = nil;
+    [[[self thumbnail] page] writeToClipboardForPageIndexes:selectionIndexes];
 }
 
 - (void)copyPageURL:(id)sender {
     PDFPage *page = [[self thumbnail] page];
-    NSURL *skimURL = [page skimURL];
-    if (skimURL != nil) {
+    NSIndexSet *selectionIndexes = [[[self controller] collectionView] 
selectionIndexes];
+    if ([selectionIndexes count] < 2 || [selectionIndexes containsIndex:[page 
pageIndex]] == NO)
+        selectionIndexes = nil;
+    NSMutableArray *urls = [NSMutableArray array];
+    NSMutableArray *names = [NSMutableArray array];
+    NSString *name = [[[[self window] windowController] document] displayName];
+    if (selectionIndexes) {
+        [selectionIndexes enumerateIndexesUsingBlock:^(NSUInteger i, BOOL 
*stop){
+            NSURL *url = [[[page document] pageAtIndex:i] skimURL];
+            if (url) {
+                [urls addObject:url];
+                [names addObject:name];
+            }
+        }];
+    } else {
+        NSURL *url = [page skimURL];
+        if (url) {
+            [urls addObject:url];
+            [names addObject:name];
+        }
+    }
+    if ([urls count]) {
         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
         [pboard clearContents];
-        [pboard writeURLs:@[skimURL] names:@[[[[[self window] 
windowController] document] displayName]]];
+        [pboard writeURLs:urls names:names];
     }
 }
 

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