// // KMFileInsertPositionWindowController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/6/12. // import Cocoa class KMFileInsertPositionWindowController: NSWindowController { @IBOutlet var positionLabel: NSTextField! @IBOutlet var firstButton: NSButton! @IBOutlet var lastButton: NSButton! @IBOutlet var pageButton: NSButton! @IBOutlet var pageTextField: NSTextField! @IBOutlet var byPageStepper: NSStepper! @IBOutlet var pageCountLabel: NSTextField! @IBOutlet var locationRangePopUpButton: NSPopUpButton! @IBOutlet var cancelButton: NSButton! @IBOutlet var insertButton: NSButton! var pdfDocument: CPDFDocument? var currentPage: Int = 1 var insertLocation: Int = 0 private var _password: String? private var _insertPageIndex: Int = 0 var callback: ((CPDFDocument?, String?, _ pageIndex: Int) -> Void)? deinit { #if DEBUG print("\(self.className) deinit") #endif } convenience init(document: CPDFDocument) { self.init(windowNibName: "KMFileInsertPositionWindowController") // self.type = KMPDFEditPageEdit; self.pdfDocument = document; } override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.positionLabel.stringValue = KMLocalizedString("Position", nil) self.firstButton.title = KMLocalizedString("First", nil) self.lastButton.title = KMLocalizedString("Last", nil) self.pageButton.title = KMLocalizedString("Page", nil) let menu = NSMenu() _ = menu.addItem(title: KMLocalizedString("After", nil), action: #selector(after_Action), target: self) _ = menu.addItem(title: KMLocalizedString("Before", nil), action: #selector(before_Action), target: self) self.locationRangePopUpButton.menu = menu self.locationRangePopUpButton.isEnabled = false self.locationRangePopUpButton.isEnabled = false self.pageTextField.isEditable = false self.byPageStepper.isEnabled = false self.insertLocation = 1 if (self.insertLocation == 2) { self.lastButton.state = .on } else if (self.insertLocation == 3) { self.pageButton.state = .on self.locationRangePopUpButton.isEnabled = true self.pageTextField.isEditable = true self.byPageStepper.isEnabled = true } else { self.firstButton.state = .on } if let data = self.pdfDocument?.isLocked, data { DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { } } else { self.byPageStepper.minValue = 1.0 self.byPageStepper.maxValue = Double(self.pdfDocument?.pageCount ?? 0) self.pageCountLabel.stringValue = String(format: "/%ld", self.pdfDocument?.pageCount ?? 0) } self.pageTextField.formatter = TextFieldFormatter() self.pageTextField.delegate = self } //MARK: IBAction @IBAction func buttonItemClicked_Location(_ sender: AnyObject?) { if (self.pageButton.isEqual(to: sender)) { self.pageTextField.isEditable = true self.locationRangePopUpButton.isEnabled = true self.byPageStepper.isEnabled = true } else{ self.pageTextField.isEditable = false self.locationRangePopUpButton.isEnabled = false self.byPageStepper.isEnabled = false } } @IBAction func byPageStepperAction(_ sender: AnyObject?) { self.pageTextField.stringValue = String(format: "%ld", self.byPageStepper.integerValue) } @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject?) { self.pdfDocument = nil guard let block = self.callback else { return } block(self.pdfDocument, nil, -1) } @IBAction func buttonItemClicked_Insert(_ sender: AnyObject?) { self.window?.makeFirstResponder(nil) var index = 0 if (self.firstButton.state == .on) { index = 0 } else if (self.lastButton.state == .on) { index = Int(self.pdfDocument?.pageCount ?? 0) } else { index = Int(self.pageTextField.stringValue) ?? 0 if ((1 == self.locationRangePopUpButton.indexOfSelectedItem)) { index = index - 1 } } self._insertPageIndex = index guard let block = self.callback else { return } block(self.pdfDocument, self._password, self._insertPageIndex) } @IBAction func after_Action(_ sender: AnyObject?) { } @IBAction func before_Action(_ sender: AnyObject?) { } } extension KMFileInsertPositionWindowController: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { let textField = obj.object as? NSTextField if (textField == self.pageTextField) { if let sValue = Int(textField!.stringValue), sValue <= 0 { self.pageTextField.stringValue = "1" self.byPageStepper.integerValue = 1 } else if let sValue = Double(textField!.stringValue), sValue > self.byPageStepper.maxValue { self.pageTextField.stringValue = String(format: "%.0f", self.byPageStepper.maxValue) self.byPageStepper.doubleValue = self.byPageStepper.maxValue } else { self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0 } } } }