123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // 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
- }
- }
- }
- }
|