// // KMRedactSelectPagesWindowController.swift // PDF Reader Pro // // Created by tangchao on 2023/12/19. // import Cocoa class KMRedactSelectPagesWindowController: NSWindowController { var pdfDocument: CPDFDocument? var selectedPages = NSMutableArray() @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var allPagesButton: NSButton! @IBOutlet weak var oddPagesButton: NSButton! @IBOutlet weak var evenPagesButton: NSButton! @IBOutlet weak var otherButton: NSButton! @IBOutlet weak var otherTextField: NSTextField! @IBOutlet weak var pagesLabel: NSTextField! @IBOutlet weak var okButton: NSButton! @IBOutlet weak var cancelButton: NSButton! var callback: ((NSMutableArray)->Void)? convenience init(document: CPDFDocument) { self.init(windowNibName: "KMRedactSelectPagesWindowController") self.pdfDocument = document } override func windowDidLoad() { super.windowDidLoad() self.titleLabel.stringValue = KMLocalizedString("Repeat Mark Across Pages") self.allPagesButton.title = KMLocalizedString("All Pages") self.oddPagesButton.title = KMLocalizedString("Odd Pages Only") self.evenPagesButton.title = KMLocalizedString("Even Pages Only") self.otherTextField.placeholderString = KMLocalizedString("e.g. 1,3-5,10") self.cancelButton.title = KMLocalizedString("Cancel") self.okButton.title = KMLocalizedString("OK") if(self.pdfDocument != nil) { self.pagesLabel.stringValue = String(format: "/%ld", self.pdfDocument!.pageCount) } self.otherTextField.isEnabled = false self.allPagesButton.state = .on } @IBAction func allpages_button(_ sender: NSButton) { if(sender.state == .on) { self.oddPagesButton.state = .off self.evenPagesButton.state = .off self.otherButton.state = .off self.otherTextField.isEnabled = false } else { self.allPagesButton.state = .on } } @IBAction func oddpages_button(_ sender: NSButton) { if(sender.state == .on) { self.allPagesButton.state = .off self.evenPagesButton.state = .off self.otherButton.state = .off self.otherTextField.isEnabled = false } else { self.oddPagesButton.state = .on } } @IBAction func evenPages_button(_ sender: NSButton) { if(sender.state == .on) { self.oddPagesButton.state = .off self.allPagesButton.state = .off self.otherButton.state = .off self.otherTextField.isEnabled = false } else { self.evenPagesButton.state = .on } } @IBAction func other_button(_ sender: NSButton) { if(sender.state == .on) { self.oddPagesButton.state = .off self.evenPagesButton.state = .off self.allPagesButton.state = .off self.otherTextField.isEnabled = true } else { self.otherButton.state = .on } } @IBAction func sure_button(_ sender: NSButton) { self.selectedPages = NSMutableArray() var hasError = false if (self.otherButton.state == .on) { // hasError = [self isInvalidString:self.otherTextField.stringValue]; hasError = KMPageRangeTools.isValidPagesString(pagesString: self.otherTextField.stringValue) == false let pages = KMPageRangeTools.findSelectPage(pageRangeString: self.otherTextField.stringValue, pageCount: Int(self.pdfDocument?.pageCount ?? 0)) for i in pages { self.selectedPages.add(NSNumber(integerLiteral: i)) } } else if (self.evenPagesButton.state == .on) { var tempPagesArray = NSMutableArray() for i in 0 ..< self.pdfDocument!.pageCount { if ((i+1)%2 == 0) { tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1)) } } self.selectedPages = tempPagesArray } else if (self.oddPagesButton.state == .on) { var tempPagesArray = NSMutableArray() for i in 0 ..< self.pdfDocument!.pageCount { if ((i+1)%2 == 1) { tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1)) } } self.selectedPages = tempPagesArray } else { var tempPagesArray = NSMutableArray() for i in 0 ..< self.pdfDocument!.pageCount { tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1)) } self.selectedPages = tempPagesArray } if (hasError || self.selectedPages.count < 1) { let message = String(format: "%@ %@", self.pdfDocument?.documentURL.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")) KMAlertTool.runModelForMainThread(message: message) return; } if self.callback != nil{ self.callback!(self.selectedPages) } self.km_quick_endSheet() } @IBAction func cancel_button(_ sender: AnyObject?) { if self.callback != nil { self.callback!(NSMutableArray()) } self.km_quick_endSheet() } /* - (BOOL)isInvalidString:(NSString *)text { NSInteger pageNumber = self.pdfDocument.pageCount; NSMutableArray *pageNumbers = [[[NSMutableArray alloc] init] autorelease]; BOOL isInvalid = NO; if (!isInvalid) { NSArray *array = [text componentsSeparatedByString:@","]; for (NSString *s in array) { if ([s isEqualToString:@""]) { isInvalid = YES; break; } else { NSArray *pages = [s componentsSeparatedByString:@"-"]; if ([pages count] > 2) { isInvalid = YES; break; } else if ([pages count] == 1) { NSString *p = [pages objectAtIndex:0]; if ([p isEqualToString:@""] || [p intValue] > pageNumber || [p integerValue] == 0) { isInvalid = YES; break; } else { BOOL isEqual = NO; for (NSNumber *pageNumber in pageNumbers) { if ([pageNumber integerValue] == [p integerValue]) { isEqual = YES; isInvalid = YES; break; } } if (!isEqual) { [pageNumbers addObject:[NSNumber numberWithInteger:[p integerValue]]]; } } } else if([pages count] == 2) { NSString *p1 = [pages objectAtIndex:0]; NSString *p2 = [pages objectAtIndex:1]; if ([p1 isEqualToString:@""] || [p2 isEqualToString:@""] || [p1 intValue] > [p2 intValue] || [p2 intValue] > pageNumber || [p1 integerValue] == 0) { isInvalid = YES; break; } else { BOOL isEqual = NO; for (int i=[p1 intValue]; i<=[p2 intValue]; i++) { for (NSNumber *pageNumber in pageNumbers) { if ([pageNumber integerValue] == i) { isEqual = YES; isInvalid = YES; break; } } } if (!isEqual) { for (int i=[p1 intValue]; i<=[p2 intValue]; i++) { [pageNumbers addObject:[NSNumber numberWithInteger:i]]; } } } } } } } if ([text length] == 0) { isInvalid = YES; } if (!isInvalid) { self.selectedPages = pageNumbers; } else { self.selectedPages = nil; } return isInvalid; } */ }