KMMainViewController+MenuAction.swift 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. //
  2. // KMMainViewController+MenuAction.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/2/19.
  6. //
  7. import Foundation
  8. // MARK: File Menu
  9. extension KMMainViewController {
  10. @IBAction func menuItemClick_mergePDF(_ sender: Any) {
  11. self.toolbarController.delegate?.clickChildTool?(type: .merge, index: 0)
  12. }
  13. @IBAction func menuItemClick_Compress(_ sender: Any) {
  14. self.toolbarController.delegate?.clickChildTool?(type: .compress, index: 0)
  15. }
  16. @IBAction func menuItemClick_Convert(_ sender: Any) {
  17. }
  18. @IBAction func menuItemAction_ConvertToWord(_ sender: Any) {
  19. self.toolbarController.delegate?.clickChildTool?(type: .word, index: 0)
  20. }
  21. @IBAction func menuItemAction_ConvertToExcel(_ sender: Any) {
  22. self.toolbarController.delegate?.clickChildTool?(type: .excel, index: 0)
  23. }
  24. @IBAction func menuItemAction_ConvertToPPT(_ sender: Any) {
  25. self.toolbarController.delegate?.clickChildTool?(type: .ppt, index: 0)
  26. }
  27. @IBAction func menuItemAction_ConvertToRTF(_ sender: Any) {
  28. self.toolbarController.delegate?.clickChildTool?(type: .rtf, index: 0)
  29. }
  30. @IBAction func menuItemAction_ConvertToHTML(_ sender: Any) {
  31. self.toolbarController.delegate?.clickChildTool?(type: .html, index: 0)
  32. }
  33. @IBAction func menuItemAction_ConvertToText(_ sender: Any) {
  34. self.toolbarController.delegate?.clickChildTool?(type: .conversion_text, index: 0)
  35. }
  36. @IBAction func menuItemAction_ConvertToCSV(_ sender: Any) {
  37. self.toolbarController.delegate?.clickChildTool?(type: .csv, index: 0)
  38. }
  39. @IBAction func menuItemAction_ConvertToImage(_ sender: Any) {
  40. self.toolbarController.delegate?.clickChildTool?(type: .conversion_image, index: 0)
  41. }
  42. @IBAction func menuItemClick_SettingPassword(_ sender: Any) {
  43. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 1)
  44. }
  45. @IBAction func menuItemClick_RemovePassword(_ sender: Any) {
  46. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 2)
  47. }
  48. @IBAction func menuItemAction_closeWindow(_ sender: Any) {
  49. self.view.window?.close()
  50. }
  51. @IBAction func menuItemAction_closeAllWindows(_ sender: Any) {
  52. for window in NSApp.windows {
  53. window.close()
  54. }
  55. }
  56. @IBAction func menuItemAction_closeTagPage(_ sender: Any) {
  57. self.browserWindowController?.browser.closeTab()
  58. }
  59. @IBAction func menuItemAction_showInFinder(_ sender: Any) {
  60. NSWorkspace.shared.activateFileViewerSelecting([self.listView.document.documentURL])
  61. }
  62. @IBAction func menuItemAction_property(_ sender: Any) {
  63. KMInfoWindowController.shared.showWindow(sender)
  64. }
  65. @IBAction func menuItemAction_print(_ sender: Any) {
  66. self.showPrintWindow()
  67. }
  68. }
  69. extension KMMainViewController: KMSystemFileMenuProtocol {
  70. @IBAction func menuItemClick_saveAsFlattenedPDF(_ sender: Any) {
  71. Task { @MainActor in
  72. if IAPProductsManager.default().isAvailableAllFunction() == false {
  73. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  74. return
  75. }
  76. self.saveAsFlattenedPDFAction()
  77. }
  78. }
  79. func saveAsFlattenedPDFAction(limit: Bool = false) {
  80. DispatchQueue.main.async {
  81. NSPanel.savePanel(self.view.window!, true) { panel in
  82. panel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_flatten" + ".pdf"
  83. } completion: { response, url, isOpen in
  84. if (response == .cancel) {
  85. return
  86. }
  87. var result = false
  88. if (limit) {
  89. let data = KMTools.saveWatermarkDocumentForFlatten(document: self.listView.document, to: url!)
  90. result = data != nil
  91. } else {
  92. result = self.listView.document.writeFlatten(to: url!)
  93. }
  94. if (!result) {
  95. return
  96. }
  97. if (isOpen) {
  98. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  99. }
  100. } else {
  101. NSWorkspace.shared.activateFileViewerSelecting([url!])
  102. }
  103. }
  104. }
  105. }
  106. }
  107. // MARK: Edit Menu
  108. @objc protocol KMEditMenuProtocol: NSObjectProtocol {
  109. @objc optional func undo(_ sender: Any)
  110. @objc optional func redo(_ sender: Any)
  111. @objc optional func cut(_ sender: Any)
  112. @objc optional func copy(_ sender: Any)
  113. @objc optional func paste(_ sender: Any)
  114. @objc optional func delete(_ sender: Any)
  115. }
  116. extension KMMainViewController: KMEditMenuProtocol {
  117. @IBAction func menuItemAction_find(_ sender: Any) {
  118. if (self.leftSideViewController.isShowPanel) {
  119. self.toolbarController.cancelSelected(KMLeftControlToolbarItemIdentifier)
  120. self.toolbarController.toolbarType = .None
  121. self.leftSideViewController.showPanelView(show: false)
  122. }
  123. // else {
  124. let isSearch = self.leftSideViewController.type.methodType == .Search
  125. self.leftSideViewController.refreshMethodType(methodType: .Search)
  126. if (isSearch) { // 关闭
  127. self.view.window?.makeFirstResponder(self.listView)
  128. }
  129. // }
  130. }
  131. }
  132. // MARK: View Menu
  133. extension KMMainViewController {
  134. // scale
  135. @IBAction func menuItemAction_adjustWidth(_ sender: Any) {
  136. self.selectZoom(.width)
  137. }
  138. @IBAction func menuItemAction_adjustPage(_ sender: Any) {
  139. self.selectZoom(.fit)
  140. }
  141. @IBAction func menuItemAction_size(_ sender: Any) {
  142. self.selectZoom(.actualSize)
  143. }
  144. @IBAction func menuItemAction_zoomOut(_ sender: Any) {
  145. if (self.listView.canZoomOut) {
  146. self.listView.zoomOut(nil)
  147. }
  148. }
  149. @IBAction func menuItemAction_zoomIn(_ sender: Any) {
  150. if (self.listView.canZoomIn) {
  151. self.listView.zoomIn(nil)
  152. }
  153. }
  154. // page show
  155. @IBAction func menuItemAction_singlePage(_ sender: Any) {
  156. self.selectDisplay(display: .singlePage)
  157. }
  158. @IBAction func menuItemAction_singlePageContinue(_ sender: Any) {
  159. self.selectDisplay(display: .singlePageContinuous)
  160. }
  161. @IBAction func menuItemAction_doublePage(_ sender: Any) {
  162. self.selectDisplay(display: .twoUp)
  163. }
  164. @IBAction func menuItemAction_doublePageContinue(_ sender: Any) {
  165. self.selectDisplay(display: .twoUpContinuous)
  166. }
  167. @IBAction func menuItemAction_bookMode(_ sender: Any) {
  168. self.selectDisplay(display: .bookMode)
  169. }
  170. @IBAction func menuItemAction_readMode(_ sender: Any) {
  171. if (self.isReadMode) {
  172. self.closeReadModel()
  173. } else {
  174. self.openReadModel()
  175. }
  176. }
  177. @IBAction func menuItemAction_showSplitPage(_ sender: Any) {
  178. self.listView?.displaysPageBreaks = !(self.listView?.displaysPageBreaks ?? false)
  179. self.listView.layoutDocumentView()
  180. }
  181. @IBAction func menuItemAction_autoScrol(_ sender: Any) {
  182. }
  183. @IBAction func menuItemAction_autoScrolSetting(_ sender: Any) {
  184. }
  185. // rotate
  186. @IBAction func menuItemAction_rotateLeft(_ sender: Any) {
  187. Task { @MainActor in
  188. #if VERSION_DMG
  189. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  190. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  191. return
  192. }
  193. #endif
  194. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  195. let rotation = page.rotation
  196. page.leftRotate()
  197. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  198. }
  199. }
  200. func rotatePageItems(pageItems: [KMThumbnailPageItem]) {
  201. let tempPageItems = pageItems
  202. for pageItem in tempPageItems {
  203. pageItem.page.rotation = pageItem.rotate
  204. }
  205. self.listView.layoutDocumentView()
  206. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMPDFViewRotatePage"), object: self.listView.document)
  207. self.listView.undoManager?.registerUndo(withTarget: self) { [unowned self] targetType in
  208. var tempPageItems: [KMThumbnailPageItem] = []
  209. for pageItem in pageItems {
  210. let item = KMThumbnailPageItem(page: pageItem.page, rotate: pageItem.oldRotate, oldRotate: pageItem.rotate)
  211. tempPageItems.append(item)
  212. }
  213. self.rotatePageItems(pageItems: tempPageItems)
  214. }
  215. }
  216. @IBAction func menuItemAction_rotateRight(_ sender: Any) {
  217. Task { @MainActor in
  218. #if VERSION_DMG
  219. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  220. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  221. return
  222. }
  223. #endif
  224. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  225. let rotation = page.rotation
  226. page.rightRotate()
  227. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  228. }
  229. }
  230. @IBAction func menuItemAction_rotateAllPageLeft(_ sender: Any) {
  231. Task { @MainActor in
  232. #if VERSION_DMG
  233. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  234. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  235. return
  236. }
  237. #endif
  238. var pageItems: [KMThumbnailPageItem] = []
  239. for index in 0 ... self.listView.document.pageCount - 1 {
  240. let page: CPDFPage = self.listView.document.page(at: index)
  241. let rotation = page.rotation
  242. page.leftRotate()
  243. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  244. }
  245. self.rotatePageItems(pageItems: pageItems)
  246. }
  247. }
  248. @IBAction func menuItemAction_rotateAllPageRight(_ sender: Any) {
  249. Task { @MainActor in
  250. #if VERSION_DMG
  251. if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  252. let _ = KMComparativeTableViewController.show(window: self.view.window!)
  253. return
  254. }
  255. #endif
  256. var pageItems: [KMThumbnailPageItem] = []
  257. for index in 0 ... self.listView.document.pageCount - 1 {
  258. let page : CPDFPage = self.listView.document.page(at: index)
  259. let rotation = page.rotation
  260. page.rightRotate()
  261. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  262. }
  263. self.rotatePageItems(pageItems: pageItems)
  264. }
  265. }
  266. // split screen
  267. @IBAction func menuItemAction_splitScreenVerti(_ sender: Any) {
  268. }
  269. @IBAction func menuItemAction_splitScreenHorti(_ sender: Any) {
  270. }
  271. @IBAction func menuItemAction_splitScreenNo(_ sender: Any) {
  272. }
  273. @IBAction func menuItemAction_view_readMode(_ sender: Any) {
  274. if (self.isReadMode) {
  275. self.closeReadModel()
  276. } else {
  277. self.openReadModel()
  278. }
  279. }
  280. @IBAction func menuItemAction_enterFullScreen(_ sender: Any) {
  281. }
  282. @IBAction func menuItemAction_hiddenLeftSide(_ sender: Any) {
  283. if (self.leftSideViewController.type.methodType != .None) {
  284. self.leftSideViewController.selectType(self.leftSideViewController.type.methodType)
  285. } else {
  286. self.toolbarController.selectItem(KMLeftControlToolbarItemIdentifier)
  287. }
  288. }
  289. @IBAction func menuItemAction_hiddenRightSide(_ sender: Any) {
  290. self.toolbarController.selectItem(KMRightControlToolbarItemIdentifier)
  291. }
  292. @IBAction func menuItemAction_thumai(_ sender: Any) {
  293. self.leftSideViewController.switchType(.Thumbnail)
  294. }
  295. @IBAction func menuItemAction_outline(_ sender: Any) {
  296. self.leftSideViewController.switchType(.Outline)
  297. }
  298. @IBAction func menuItemAction_bookmark(_ sender: Any) {
  299. self.leftSideViewController.switchType(.Annotation)
  300. }
  301. @IBAction func menuItemAction_annotation(_ sender: Any) {
  302. self.leftSideViewController.switchType(.Annotation)
  303. }
  304. @IBAction func menuItemAction_search(_ sender: Any) {
  305. self.leftSideViewController.switchType(.Search)
  306. }
  307. @IBAction func menuItemAction_topic(_ sender: Any) {
  308. }
  309. @IBAction func menuItemAction_hiddenPageIndicator(_ sender: Any) {
  310. }
  311. @IBAction func togglePresentation(_ sender: Any?) {
  312. KMPrint("togglePresentation")
  313. if self.canExitPresentation() {
  314. exitFullScreen("")
  315. } else if self.canEnterPresentation(){
  316. enterPresentation()
  317. }
  318. }
  319. @IBAction func chooseTransition(_ sender: Any?) {
  320. KMPrint("chooseTransition")
  321. // presentationSheetController = [[SKPresentationOptionsSheetController alloc] initForController:self];
  322. //
  323. // [presentationSheetController beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
  324. // SKDESTROY(presentationSheetController);
  325. // }];
  326. let presentationSheetController = SKPresentationOptionsSheetController(windowNibName: "TransitionSheet")
  327. // let presentationSheetController = SKPresentationOptionsSheetController(for: MainWindowController)
  328. presentationSheetController.controller = self
  329. let window = NSWindow.currentWindow()
  330. window.km_beginSheet(windowC: presentationSheetController)
  331. presentationSheetController.closeCallBack = { vc in
  332. window.km_quick_endSheet()
  333. }
  334. }
  335. @IBAction func toggleAutoFlow(_ sender: Any?) {
  336. if (self.listView.isAutoFlow()) {
  337. self.listView.stopAutoFlow()
  338. } else {
  339. self.listView.startAutoFlow()
  340. }
  341. }
  342. @IBAction func chooseAutoFlowSetting(_ sender: Any?) {
  343. KMPrint("chooseAutoFlowSetting")
  344. autoFlowOptionsSheetController = KMAutoFlowOptionsSheetController(windowNibName: "KMAutoFlowOptionsSheetController")
  345. NSWindow.currentWindow().beginSheet((autoFlowOptionsSheetController?.window)!) { responce in
  346. }
  347. }
  348. @IBAction func toggleReadingBar(_ sender: Any?) {
  349. KMPrint("toggleReadingBar")
  350. }
  351. @IBAction func toggleLeftSidePane(_ sender: Any?) {
  352. if self.leftPanelOpen {
  353. self.closeLeftPane()
  354. } else {
  355. self.openLeftPane()
  356. }
  357. }
  358. @IBAction func changeLeftSidePaneState(_ sender: Any?) {
  359. guard let item = sender as? NSMenuItem else {
  360. NSSound.beep()
  361. return
  362. }
  363. if item.tag == 3 {
  364. self.leftSideViewController.switchType(.snapshot)
  365. }
  366. }
  367. @IBAction func splitViewAction(_ sender: Any?) {
  368. guard let item = sender as? NSMenuItem else {
  369. return
  370. }
  371. // let index = item.tag
  372. if item.tag < 2 && IAPProductsManager.default().isAvailableAllFunction() == false {
  373. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  374. return
  375. }
  376. // if (toolbarViewController.splitScreenPopover != nil) {
  377. // [toolbarViewController.splitScreenPopover close];
  378. // }
  379. self.splitView(withTag: item.tag)
  380. // [[NSNotificationCenter defaultCenter] postNotificationName:@"kKMToolsItemSelectStateChange" object:self.pdfView];
  381. }
  382. @IBAction func themesColor(_ sender: Any?) {
  383. guard let item = sender as? NSMenuItem else {
  384. __NSBeep()
  385. return
  386. }
  387. let mode = item.tag
  388. if mode == 0 {
  389. self.listView.displayMode = .normal
  390. } else if mode == 1 {
  391. self.listView.displayMode = .soft
  392. } else if mode == 2 {
  393. self.listView.displayMode = .night
  394. } else if mode == 3 {
  395. self.listView.displayMode = .green
  396. }
  397. self.listView.setNeedsDisplayForVisiblePages()
  398. }
  399. @IBAction func toggleStatusBar(_ sender: Any?) {
  400. KMPrint("toggleStatusBar ...")
  401. }
  402. @IBAction func toggleToolbarShow(_ sender: Any?) {
  403. KMPrint("toggleToolbarShow ...")
  404. }
  405. }
  406. // MARK: Annotation Menu
  407. extension KMMainViewController {
  408. @IBAction func menuItemAction_highlight(_ sender: Any) {
  409. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarHighlightAnnotationItemIdentifier)
  410. }
  411. @IBAction func menuItemAction_underline(_ sender: Any) {
  412. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarUnderlineAnnotationItemIdentifier)
  413. }
  414. @IBAction func menuItemAction_deleteLine(_ sender: Any) {
  415. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarStrikeOutAnnotationItemIdentifier)
  416. }
  417. @IBAction func menuItemAction_freehand(_ sender: Any) {
  418. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarInkAnnotationItemIdentifier)
  419. }
  420. @IBAction func menuItemAction_text(_ sender: Any) {
  421. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarFreeTextAnnotationItemIdentifier)
  422. }
  423. @IBAction func menuItemAction_note(_ sender: Any) {
  424. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarAnchoredAnnotationItemIdentifier)
  425. }
  426. @IBAction func menuItemAction_squre(_ sender: Any) {
  427. UserDefaults.standard.set(6, forKey: KMToolBarToolPDFShapeStyle)
  428. if (self.listView.annotationType == .square) {
  429. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  430. } else {
  431. if (self.listView.annotationType == .circle || self.listView.annotationType == .line || self.listView.annotationType == .arrow) {
  432. self.listView.annotationType = .square
  433. self.rightSideViewController.annotationProperties.annotationMode = .square
  434. } else {
  435. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  436. }
  437. }
  438. }
  439. @IBAction func menuItemAction_circle(_ sender: Any) {
  440. UserDefaults.standard.set(7, forKey: KMToolBarToolPDFShapeStyle)
  441. if (self.listView.annotationType == .circle) { // 取消
  442. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  443. } else {
  444. if (self.listView.annotationType == .square || self.listView.annotationType == .line || self.listView.annotationType == .arrow) { // 切换
  445. self.listView.annotationType = .circle
  446. self.rightSideViewController.annotationProperties.annotationMode = .circle
  447. } else { // 选中
  448. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  449. }
  450. }
  451. }
  452. @IBAction func menuItemAction_arrow(_ sender: Any) {
  453. UserDefaults.standard.set(5, forKey: KMToolBarToolPDFShapeStyle)
  454. if (self.listView.annotationType == .arrow) { // 取消
  455. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  456. } else { // 选中
  457. if (self.listView.annotationType == .square || self.listView.annotationType == .circle || self.listView.annotationType == .line) { // 切换
  458. self.listView.annotationType = .arrow
  459. self.rightSideViewController.annotationProperties.annotationMode = .arrow
  460. } else {
  461. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  462. }
  463. }
  464. }
  465. @IBAction func menuItemAction_line(_ sender: Any) {
  466. UserDefaults.standard.set(4, forKey: KMToolBarToolPDFShapeStyle)
  467. if (self.listView.annotationType == .line) { // 取消
  468. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  469. } else { // 选中
  470. if (self.listView.annotationType == .square || self.listView.annotationType == .circle || self.listView.annotationType == .arrow) { // 切换
  471. self.listView.annotationType = .line
  472. self.rightSideViewController.annotationProperties.annotationMode = .line
  473. } else {
  474. self.toolbarController.clickItem(KMToolbarSquareAnnotationItemIdentifier)
  475. }
  476. }
  477. }
  478. // link
  479. @IBAction func menuItemAction_link(_ sender: Any) {
  480. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  481. }
  482. @IBAction func menuItemAction_linkPage(_ sender: Any) {
  483. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  484. }
  485. @IBAction func menuItemAction_linkHttps(_ sender: Any) {
  486. }
  487. @IBAction func menuItemAction_linkEmail(_ sender: Any) {
  488. }
  489. @IBAction func notesLink(_ sender: Any?) {
  490. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarLinkAnnotationItemIdentifier)
  491. }
  492. // stamp
  493. @IBAction func menuItemAction_stamp(_ sender: Any) {
  494. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  495. }
  496. @IBAction func menuItemAction_stampStandard(_ sender: Any) {
  497. }
  498. @IBAction func menuItemAction_stampDynamic(_ sender: Any) {
  499. }
  500. @IBAction func menuItemAction_stampCustom(_ sender: Any) {
  501. }
  502. @IBAction func notesStamp(_ sender: Any?) {
  503. self.mainMenuUpdateAnnotationStyle(identifier: KMAnnotationStampToolbarItemIdentifier)
  504. }
  505. @IBAction func menuItemAction_signure(_ sender: Any) {
  506. self.mainMenuUpdateAnnotationStyle(identifier: KMToolbarSignSignatureAnnotationItemIdentifier)
  507. }
  508. @IBAction func image(_ sender: Any?) {
  509. self.toolbarController.clickItem(KMAnnotationImageToolbarItemIdentifier)
  510. }
  511. @IBAction func table(_ sender: Any?) {
  512. self.toolbarController.clickItem(KMAnnotationTableToolbarItemIdentifier)
  513. }
  514. @IBAction func addForm(_ sender: Any?) {
  515. guard let item = sender as? NSMenuItem else {
  516. NSSound.beep()
  517. return
  518. }
  519. var identifiers: String?
  520. let idx = item.tag
  521. if idx == 0 { // raido
  522. identifiers = KMToolbarToolRadioButtonItemIdentifier
  523. } else if idx == 1 { // checkbox
  524. identifiers = KMToolbarToolCheckBoxItemIdentifier
  525. } else if idx == 2 { // textfiled
  526. identifiers = KMToolbarToolTextFieldItemIdentifier
  527. } else if idx == 3 { // combobox
  528. identifiers = KMToolbarToolPullDownmenuItemIdentifier
  529. } else if idx == 4 { // listbox
  530. identifiers = KMToolbarToolListBoxItemIdentifier
  531. } else if idx == 5 { // button
  532. identifiers = KMToolbarToolButtonIdentifier
  533. } else if idx == 6 { // sign
  534. identifiers = KMToolbarToolSignBoxIdentifier
  535. }
  536. if let itemID = identifiers {
  537. self.toolbarController.clickItem(itemID)
  538. }
  539. }
  540. @IBAction func menuItemAction_hiddenAllAnnotation(_ sender: Any) {
  541. self.showOrHideNotes()
  542. }
  543. @IBAction func menuItemAction_clearAllAnnotation(_ sender: Any) {
  544. self.removeAllAnnotations()
  545. }
  546. private func mainMenuUpdateAnnotationStyle(identifier : String) {
  547. if self.toolbarController.toolbarType != .Annatiton {
  548. let item : KMToolbarItemView = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentAnnotationToolbarItemIdentifier))!
  549. self.toolbarController.mainToolBarView?.delegate?.toolbarViewController?(self.toolbarController.mainToolBarView!, clickMode: .Annatiton, toolbar: item, [])
  550. }
  551. let childitem : KMToolbarItemView = (self.toolbarController.childToolBarView?.toolbarItemFindItemIdentifiers(value: identifier))!
  552. self.toolbarController.mainToolBarView?.delegate?.changeAnnotationModeAction?(item: childitem.clickButton)
  553. }
  554. }
  555. // MARK: - KMSystemPDFMenuProtocol
  556. extension KMMainViewController: KMSystemPDFMenuProtocol {
  557. func changeDisplayMode(_ sender: Any?) {
  558. guard let item = sender as? NSMenuItem else {
  559. NSSound.beep()
  560. return
  561. }
  562. let mode = item.tag
  563. if mode == 0 {
  564. self.selectDisplay(display: .singlePage)
  565. } else if mode == 1 {
  566. self.selectDisplay(display: .singlePageContinuous)
  567. } else if mode == 2 {
  568. self.selectDisplay(display: .twoUp)
  569. } else if mode == 3 {
  570. self.selectDisplay(display: .twoUpContinuous)
  571. }
  572. }
  573. func toggleDisplayAsBook(_ sender: Any?) {
  574. self.selectDisplay(display: .bookMode)
  575. }
  576. func toggleDisplayPageBreaks(_ sender: Any?) {
  577. self.listView?.displaysPageBreaks = !(self.listView?.displaysPageBreaks ?? false)
  578. self.listView.layoutDocumentView()
  579. }
  580. func changeDisplayBox(_ sender: Any?) {
  581. guard let item = sender as? NSMenuItem else {
  582. NSSound.beep()
  583. return
  584. }
  585. let mode = item.tag
  586. if mode == 0 {
  587. self.listView.displayBox = .mediaBox
  588. } else if mode == 1 {
  589. self.listView.displayBox = .cropBox
  590. }
  591. }
  592. func toggleAutoScale(_ sender: Any?) {
  593. // if ([self interactionMode] == SKPresentationMode)
  594. // [pdfView toggleAutoActualSize:sender];
  595. // else
  596. // [pdfView setAutoScales:[pdfView autoScales] == NO];
  597. self.listView.autoScales = !self.listView.autoScales
  598. }
  599. func doZoomToActualSize(_ sender: Any?) {
  600. self.selectZoom(.actualSize)
  601. }
  602. func doZoomToFit(_ sender: Any?) {
  603. self.selectZoom(.fit)
  604. }
  605. func doZoomIn(_ sender: Any?) {
  606. if (self.listView.canZoomIn) {
  607. self.listView.zoomIn(nil)
  608. }
  609. }
  610. func doZoomOut(_ sender: Any?) {
  611. if (self.listView.canZoomOut) {
  612. self.listView.zoomOut(nil)
  613. }
  614. }
  615. func doMarqueeZoomTool(_ sender: Any?) {
  616. let isSelected = self.toolbarController.findItem(KMToolbarZoomToSelectionItemIdentifier)?.isSelected ?? false
  617. if isSelected == false {
  618. self.toolbarController.clickItem(KMToolbarZoomToSelectionItemIdentifier)
  619. }
  620. }
  621. func highlightFormFiled(_ sender: Any?) {
  622. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  623. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  624. self.listView.setNeedsDisplayForVisiblePages()
  625. }
  626. func highlightLinks(_ sender: Any?) {
  627. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  628. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(!enabled)
  629. self.listView.setNeedsDisplayForVisiblePages()
  630. }
  631. func resetForm(_ sender: Any?) {
  632. self.listView.resetFormAnnotation()
  633. }
  634. func removeAllAnnotation(_ sender: Any?) {
  635. self.listView.removeAllAnnotation()
  636. }
  637. func savePDFSettingToDefaults(_ sender: Any?) {
  638. if self.interactionMode == .SKNormalMode {
  639. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: SKDefaultPDFDisplaySettingsKey)
  640. } else if self.interactionMode != .SKPresentationMode {
  641. UserDefaults.standard.sync_setValue(self.currentPDFSettings(), forKey: SKDefaultFullScreenPDFDisplaySettingsKey)
  642. }
  643. }
  644. }
  645. // MARK: goto Menu
  646. extension KMMainViewController {
  647. @IBAction func menuItemAction_nextPage(_ sender: Any) {
  648. if (self.listView.canGoToNextPage()) {
  649. self.listView.goToNextPage(nil)
  650. }
  651. }
  652. @IBAction func menuItemAction_forwardPage(_ sender: Any) {
  653. if (self.listView.canGoToPreviousPage()) {
  654. self.listView.goToPreviousPage(nil)
  655. }
  656. }
  657. @IBAction func menuItemAction_firstPage(_ sender: Any) {
  658. if (self.listView.canGoToFirstPage()) {
  659. self.listView.goToFirstPage(nil)
  660. }
  661. }
  662. @IBAction func menuItemAction_lastPage(_ sender: Any) {
  663. if (self.listView.canGoToLastPage()) {
  664. self.listView.goToLastPage(nil)
  665. }
  666. }
  667. @IBAction func menuItemAction_forward(_ sender: Any) {
  668. if (self.listView.km_canGoForward()) {
  669. self.listView.km_goForward(nil)
  670. }
  671. }
  672. @IBAction func menuItemAction_goback(_ sender: Any) {
  673. if (self.listView.km_canGoBack()) {
  674. self.listView.km_goBack(nil)
  675. }
  676. }
  677. @IBAction func menuItemAction_gotoPage(_ sender: Any) {
  678. let sheet = KMTextFieldSheetController.init(windowNibName: "PageSheet")
  679. var pages : [String] = []
  680. for i in 0 ..< self.listView.document.pageCount {
  681. pages.append("\(i)")
  682. }
  683. sheet.callback = { [weak self] stringValue in
  684. if (stringValue == nil) {
  685. return
  686. }
  687. guard let index = Int(stringValue) else {
  688. return
  689. }
  690. if (self?.listView == nil) {
  691. return
  692. }
  693. if (index > 0 && index <= self!.listView.document.pageCount) {
  694. self?.listView.go(toPageIndex: index-1, animated: true)
  695. }
  696. }
  697. sheet.showWindow(nil)
  698. sheet.pageBox.addItems(withObjectValues: pages)
  699. sheet.stringValue = "\(self.listView.currentPageIndex+1)"
  700. }
  701. @IBAction func goToMarkedPage(_ sender: Any?) {
  702. guard let pdfDoc = self.listView.document, pdfDoc.isLocked == false && pdfDoc.pageCount > 0 else {
  703. NSSound.beep()
  704. return
  705. }
  706. guard let currentPageIndex = self.listView.currentPage()?.pageIndex() else {
  707. NSSound.beep()
  708. return
  709. }
  710. // if (markedPageIndex == NSNotFound || [pdfDoc isLocked] || [pdfDoc pageCount] == 0) {
  711. // NSBeep();
  712. // } else if (beforeMarkedPageIndex != NSNotFound) {
  713. if self.beforeMarkedPageIndex != NSNotFound {
  714. self.listView.goToPage(at: min(UInt(self.beforeMarkedPageIndex), pdfDoc.pageCount-1), point: self.beforeMarkedPagePoint)
  715. } else if (currentPageIndex != self.markedPageIndex) {
  716. // NSUInteger lastPageIndex = [pdfView currentPageIndexAndPoint:&beforeMarkedPagePoint rotated:NULL];
  717. let lastPageIndex = self.listView.currentPageIndexAndPoint(&self.beforeMarkedPagePoint, rotated: nil)
  718. self.listView.goToPage(at: min(UInt(self.markedPageIndex), pdfDoc.pageCount-1), point: self.markedPagePoint)
  719. self.beforeMarkedPageIndex = Int(lastPageIndex)
  720. }
  721. }
  722. @IBAction func markPage(_ sender: Any?) {
  723. self.markedPageIndex = Int(self.listView.currentPageIndexAndPoint(&self.markedPagePoint, rotated: nil))
  724. self.beforeMarkedPageIndex = NSNotFound
  725. }
  726. }
  727. // MARK: tool Menu
  728. extension KMMainViewController: KMSystemToolMenuProtocol {
  729. func getInfo(_ sender: Any?) {
  730. KMInfoWindowController.shared.showWindow(sender)
  731. }
  732. @IBAction func menuItemAction_textTool(_ sender: Any) {
  733. self.toolbarController.selectItem(KMToolbarZoomToSelectionItemIdentifier)
  734. }
  735. @IBAction func menuItemAction_scrolTool(_ sender: Any) {
  736. self.toolbarController.selectItem(KMToolbarMoveToolModeItemIdentifier)
  737. }
  738. @IBAction func menuItemAction_zoomOutTool(_ sender: Any) {
  739. self.toolbarController.selectItem(KMToolbarMagnifyToolModeItemIdentifier)
  740. }
  741. @IBAction func menuItemAction_selectTool(_ sender: Any) {
  742. self.toolbarController.selectItem(KMToolbarSelectToolModeItemIdentifier)
  743. }
  744. func changeToolMode(_ sender: Any?) {
  745. let isSelected = self.toolbarController.findItem(KMToolbarHighlightAnnotationItemIdentifier)?.isSelected ?? false
  746. if isSelected == false {
  747. self.toolbarController.selectItem(KMToolbarHighlightAnnotationItemIdentifier)
  748. }
  749. }
  750. @IBAction func menuItemAction_redact(_ sender: Any) {
  751. self.toolbarController.clickItem(KMDocumentRedactToolbarItemIdentifier)
  752. }
  753. @IBAction func menuItemAction_warkmark(_ sender: Any) {
  754. self.toolbarController.delegate?.clickChildTool?(type: .watermark, index: 0)
  755. }
  756. @IBAction func menuItemAction_background(_ sender: Any) {
  757. self.toolbarController.delegate?.clickChildTool?(type: .background, index: 0)
  758. }
  759. @IBAction func menuItemAction_headerfooter(_ sender: Any) {
  760. self.toolbarController.delegate?.clickChildTool?(type: .headerAndFooter, index: 0)
  761. }
  762. @IBAction func menuItemAction_bates(_ sender: Any) {
  763. self.toolbarController.delegate?.clickChildTool?(type: .bates, index: 0)
  764. }
  765. @IBAction func menuItemAction_batch(_ sender: Any) {
  766. // KMBatchWindowController.openFile(self.listView.document?.documentURL, .Batch)
  767. }
  768. func compression(_ sender: Any?) {
  769. self.toolbarController.delegate?.clickChildTool?(type: .compress, index: 0)
  770. }
  771. func merge(_ sender: Any?) {
  772. self.toolbarController.delegate?.clickChildTool?(type: .merge, index: 0)
  773. }
  774. func convert(_ sender: Any?) {
  775. self.toolbarController.delegate?.clickChildTool?(type: .word, index: 0)
  776. }
  777. func imageToPDF(_ sender: Any?) {
  778. KMApp.ShowImageToPDFWindow()
  779. }
  780. func ocr(_ sender: Any?) {
  781. self.toolbarController.clickItem(KMDocumentScanOCRToolbarItemIdentifier)
  782. }
  783. func extractImage(_ sender: Any?) {
  784. self.extractImageAction(num: 1)
  785. }
  786. func pageEdit(_ sender: Any?) {
  787. self.toolbarController.clickItem(KMDocumentPageToolbarItemIdentifier)
  788. }
  789. func textEditAction(_ sender: Any?) {
  790. self.toolbarController.clickItem(KMDocumentEditToolbarItemIdentifier)
  791. }
  792. func security(_ sender: Any?) {
  793. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 1)
  794. }
  795. func decryption(_ sender: Any?) {
  796. self.toolbarController.delegate?.clickChildTool?(type: .secure, index: 2)
  797. }
  798. func cropAll(_ sender: Any?) {
  799. // NSRect rect[2] = {NSIntegralRect([pdfView currentSelectionRect]), NSZeroRect};
  800. // NSPointerArray *rectArray = [NSPointerArray rectPointerArray];
  801. // BOOL emptySelection = NSIsEmptyRect(rect[0]);
  802. //
  803. // if (emptySelection) {
  804. // NSInteger i, j, count = [[pdfView document] pageCount];
  805. // rect[0] = rect[1] = NSZeroRect;
  806. //
  807. // if (count == 0)
  808. // return;
  809. //
  810. // [self beginProgressSheetWithMessage:[NSLocalizedString(@"Cropping Pages", @"Message for progress sheet") stringByAppendingEllipsis] maxValue:MIN(18, count)];
  811. //
  812. // if (count == 1) {
  813. // rect[0] = [[[pdfView document] pageAtIndex:0] foregroundBox];
  814. // [self incrementProgressSheet];
  815. // } else if (count < 19) {
  816. // for (i = 0; i < count; i++) {
  817. // rect[i % 2] = NSUnionRect(rect[i % 2], [[[pdfView document] pageAtIndex:i] foregroundBox]);
  818. // [self incrementProgressSheet];
  819. // }
  820. // } else {
  821. // NSInteger start[3] = {1, (count - 5) / 2, count - 6};
  822. // for (j = 0; j < 3; j++) {
  823. // for (i = start[j]; i < start[j] + 6; i++) {
  824. // rect[i % 2] = NSUnionRect(rect[i % 2], [[[pdfView document] pageAtIndex:i] foregroundBox]);
  825. // [self incrementProgressSheet];
  826. // }
  827. // }
  828. // }
  829. // CGFloat w = fmax(NSWidth(rect[0]), NSWidth(rect[1]));
  830. // CGFloat h = fmax(NSHeight(rect[0]), NSHeight(rect[1]));
  831. // for (j = 0; j < 2; j++)
  832. // rect[j] = NSMakeRect(floor(NSMidX(rect[j]) - 0.5 * w), floor(NSMidY(rect[j]) - 0.5 * h), w, h);
  833. // [rectArray addPointer:rect];
  834. // [rectArray addPointer:rect + 1];
  835. // } else {
  836. // [rectArray addPointer:rect];
  837. // }
  838. //
  839. // [self cropPagesToRects:rectArray];
  840. // [pdfView setCurrentSelectionRect:NSZeroRect];
  841. self.cropAllPage()
  842. }
  843. func crop(_ sender: Any?) {
  844. self.cropCurrentPage()
  845. }
  846. func autoCropAll(_ sender: Any?) {
  847. #warning("暂时这么处理,后续调整")
  848. self.cropAllPage()
  849. }
  850. func smartAutoCropAll(_ sender: Any?) {
  851. #warning("暂时这么处理,后续调整")
  852. self.cropAllPage()
  853. }
  854. func cropOptionsModel(_ sender: Any?) {
  855. KMPrint("cropOptionsModel ...")
  856. }
  857. func selectToolModel(_ sender: Any?) {
  858. self.toolbarController.clickItem(KMToolbarSelectToolModeItemIdentifier)
  859. }
  860. func takeSnapshot(_ sender: Any?) {
  861. self.listView.takeSnapshot(sender)
  862. }
  863. func takeSnapshotSelectContent(_ sender: Any?) {
  864. self.listView.takeSnapshotSelectContent(sender)
  865. }
  866. func changeAnnotationMode(_ sender: Any?) {
  867. if let data = sender as? NSMenuItem {
  868. self.changeAnnotationMode_itemAction(sender: data)
  869. }
  870. }
  871. func addWatermark(_ sender: Any?) {
  872. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  873. if item == nil {
  874. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  875. }
  876. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  877. }
  878. func removeWatermark(_ sender: Any?) {
  879. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  880. if item == nil {
  881. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  882. }
  883. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  884. }
  885. func batchAddWatermark(_ sender: Any?) {
  886. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  887. if item == nil {
  888. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  889. }
  890. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  891. }
  892. func batchRemoveWatermark(_ sender: Any?) {
  893. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  894. if item == nil {
  895. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  896. }
  897. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  898. }
  899. func templateWatermark(_ sender: Any?) {
  900. var item = self.toolbarController.findItem(KMToolbarToolWatermarkItemIdentifier)
  901. if item == nil {
  902. item = KMToolbarItemView(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  903. }
  904. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  905. }
  906. func addBackground(_ sender: Any?) {
  907. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  908. if item == nil {
  909. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  910. }
  911. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  912. }
  913. func removeBackground(_ sender: Any?) {
  914. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  915. if item == nil {
  916. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  917. }
  918. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  919. }
  920. func batchAddBackground(_ sender: Any?) {
  921. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  922. if item == nil {
  923. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  924. }
  925. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  926. }
  927. func batchRemoveBackground(_ sender: Any?) {
  928. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  929. if item == nil {
  930. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  931. }
  932. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil) }
  933. func templateBackground(_ sender: Any?) {
  934. var item = self.toolbarController.findItem(KMToolbarToolBackgroundItemIdentifier)
  935. if item == nil {
  936. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  937. }
  938. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  939. }
  940. func addHeaderFooter(_ sender: Any?) {
  941. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  942. if item == nil {
  943. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  944. }
  945. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  946. }
  947. func removeHeaderFooter(_ sender: Any?) {
  948. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  949. if item == nil {
  950. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  951. }
  952. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  953. }
  954. func batchAddHeaderFooter(_ sender: Any?) {
  955. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  956. if item == nil {
  957. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  958. }
  959. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  960. }
  961. func batchRemoveHeaderFooter(_ sender: Any?) {
  962. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  963. if item == nil {
  964. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  965. }
  966. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  967. }
  968. func templateHeaderFooter(_ sender: Any?) {
  969. var item = self.toolbarController.findItem(KMToolbarToolHeaderFooterItemIdentifier)
  970. if item == nil {
  971. item = KMToolbarItemView(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  972. }
  973. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  974. }
  975. func addHeaderFooterBates(_ sender: Any?) {
  976. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  977. if item == nil {
  978. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  979. }
  980. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 1, info: nil)
  981. }
  982. func removeHeaderFooterBates(_ sender: Any?) {
  983. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  984. if item == nil {
  985. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  986. }
  987. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 2, info: nil)
  988. }
  989. func batchAddBates(_ sender: Any?) {
  990. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  991. if item == nil {
  992. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  993. }
  994. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 3, info: nil)
  995. }
  996. func batchRemoveBates(_ sender: Any?) {
  997. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  998. if item == nil {
  999. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  1000. }
  1001. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 4, info: nil)
  1002. }
  1003. func templateBates(_ sender: Any?) {
  1004. var item = self.toolbarController.findItem(KMToolbarToolBatesItemIdentifier)
  1005. if item == nil {
  1006. item = KMToolbarItemView(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  1007. }
  1008. self.toolbarController.toolbarViewController(self.toolbarController.childToolBarView!, menuItemDidClick: item!, index: 5, info: nil)
  1009. }
  1010. func togglePoster(_ sender: Any?) {
  1011. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .poster)
  1012. }
  1013. func toggleMultiple(_ sender: Any?) {
  1014. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .multipage)
  1015. }
  1016. func toggleBooklet(_ sender: Any?) {
  1017. KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
  1018. }
  1019. func rotateLeft(_ sender: Any?) {
  1020. Task { @MainActor in
  1021. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  1022. let rotation = page.rotation
  1023. page.leftRotate()
  1024. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  1025. self.handlePageBoundsDidChange(page: page, action: CPDFPage.Action.rotate)
  1026. }
  1027. }
  1028. func rotateRight(_ sender: Any?) {
  1029. Task { @MainActor in
  1030. let page : CPDFPage = self.listView?.currentPage() ?? CPDFPage()
  1031. let rotation = page.rotation
  1032. page.rightRotate()
  1033. self.rotatePageItems(pageItems: [KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation)])
  1034. self.handlePageBoundsDidChange(page: page, action: CPDFPage.Action.rotate)
  1035. }
  1036. }
  1037. func rotateAllLeft(_ sender: Any?) {
  1038. Task { @MainActor in
  1039. var pageItems: [KMThumbnailPageItem] = []
  1040. for index in 0 ... self.listView.document.pageCount - 1 {
  1041. let page: CPDFPage = self.listView.document.page(at: index)
  1042. let rotation = page.rotation
  1043. page.leftRotate()
  1044. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1045. }
  1046. self.rotatePageItems(pageItems: pageItems)
  1047. self.handlePageBoundsDidChange(page: nil, action: CPDFPage.Action.rotate)
  1048. }
  1049. }
  1050. func rotateAllRight(_ sender: Any?) {
  1051. Task { @MainActor in
  1052. var pageItems: [KMThumbnailPageItem] = []
  1053. for index in 0 ... self.listView.document.pageCount - 1 {
  1054. let page: CPDFPage = self.listView.document.page(at: index)
  1055. let rotation = page.rotation
  1056. page.leftRotate()
  1057. pageItems.append(KMThumbnailPageItem(page: page, rotate: page.rotation, oldRotate: rotation))
  1058. }
  1059. self.rotatePageItems(pageItems: pageItems)
  1060. self.handlePageBoundsDidChange(page: nil, action: CPDFPage.Action.rotate)
  1061. }
  1062. }
  1063. func autoSelectContent(_ sender: Any?) {
  1064. self.listView.autoSelectContent(sender)
  1065. }
  1066. }
  1067. // MARK: window Menu
  1068. extension KMMainViewController {
  1069. @IBAction func menuItemAction_showForwardTagPage(_ sender: Any) {
  1070. (self.myDocument as? KMMainDocument)?.browser.selectPreviousTab()
  1071. }
  1072. @IBAction func menuItemAction_showNextTagPage(_ sender: Any) {
  1073. (self.myDocument as? KMMainDocument)?.browser.selectNextTab()
  1074. }
  1075. @IBAction func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
  1076. self.browserWindowController?.openNewWindow(sender)
  1077. }
  1078. @IBAction func menuItemAction_mergeAllWindow(_ sender: Any) {
  1079. ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController)?.mergeAllWindow(sender)
  1080. }
  1081. @IBAction func menuItemAction_currentWindowName(_ sender: Any) {
  1082. }
  1083. }
  1084. extension KMMainViewController: NSMenuItemValidation, NSMenuDelegate {
  1085. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  1086. if (menuItem.action == #selector(menuItemAction_currentWindowName)) {
  1087. menuItem.title = (self.myDocument?.fileURL!.lastPathComponent)!
  1088. return true
  1089. }
  1090. if (menuItem.action == #selector(menuItemAction_thumai) ||
  1091. menuItem.action == #selector(menuItemAction_outline) ||
  1092. menuItem.action == #selector(menuItemAction_bookmark) ||
  1093. menuItem.action == #selector(menuItemAction_annotation) ||
  1094. menuItem.action == #selector(menuItemAction_search)) {
  1095. menuItem.state = .off
  1096. let type = self.leftSideViewController.type.methodType
  1097. if (menuItem.action == #selector(menuItemAction_thumai) && type == .Thumbnail) {
  1098. menuItem.state = .on
  1099. }
  1100. if (menuItem.action == #selector(menuItemAction_outline) && type == .Outline) {
  1101. menuItem.state = .on
  1102. }
  1103. if (menuItem.action == #selector(menuItemAction_bookmark) && type == .Annotation) {
  1104. menuItem.state = .on
  1105. }
  1106. if (menuItem.action == #selector(menuItemAction_annotation) && type == .Annotation) {
  1107. menuItem.state = .on
  1108. }
  1109. if (menuItem.action == #selector(menuItemAction_search) && type == .Search) {
  1110. menuItem.state = .on
  1111. }
  1112. }
  1113. if (menuItem.action == #selector(menuItemAction_hiddenLeftSide)) {
  1114. if (self.leftPanelOpen) {
  1115. menuItem.title = NSLocalizedString("Hide Left Side Panel", comment: "")
  1116. } else {
  1117. menuItem.title = NSLocalizedString("Show Left Side Panel", comment: "")
  1118. }
  1119. }
  1120. if (menuItem.action == #selector(menuItemAction_hiddenRightSide)) {
  1121. if (self.rightPanelIsOpen) {
  1122. menuItem.title = NSLocalizedString("Hide Right Side Panel", comment: "")
  1123. } else {
  1124. menuItem.title = NSLocalizedString("Show Right Side Panel", comment: "")
  1125. }
  1126. }
  1127. if (menuItem.action == #selector(menuItemAction_hiddenAllAnnotation)) {
  1128. if (self.listView.hideNotes) {
  1129. menuItem.title = NSLocalizedString("Show Notes", comment: "")
  1130. } else {
  1131. menuItem.title = NSLocalizedString("Hide Notes", comment: "")
  1132. }
  1133. }
  1134. if (menuItem.action == #selector(menuItemAction_showForwardTagPage) ||
  1135. menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1136. let browser = self.browserWindowController?.browser
  1137. if (browser != nil && (browser is KMBrowser)) {
  1138. if (menuItem.action == #selector(menuItemAction_showForwardTagPage)) {
  1139. return (browser as! KMBrowser).canSelectPreviousTab()
  1140. }
  1141. if (menuItem.action == #selector(menuItemAction_showNextTagPage)) {
  1142. return (browser as! KMBrowser).canSelectNextTab()
  1143. }
  1144. }
  1145. }
  1146. if (menuItem.action == #selector(menuItemAction_mergeAllWindow)) {
  1147. if let _browserWindowC = ((self.myDocument as? KMMainDocument)?.browser.windowController as? KMBrowserWindowController) {
  1148. return _browserWindowC.canMergeAllWindow()
  1149. }
  1150. }
  1151. if (menuItem.action == #selector(menuItemAction_nextPage) ||
  1152. menuItem.action == #selector(menuItemAction_forwardPage) ||
  1153. menuItem.action == #selector(menuItemAction_firstPage) ||
  1154. menuItem.action == #selector(menuItemAction_lastPage) ||
  1155. menuItem.action == #selector(menuItemAction_forward) ||
  1156. menuItem.action == #selector(menuItemAction_goback)) {
  1157. if (menuItem.action == #selector(menuItemAction_nextPage)) {
  1158. if self.listView.isEditing() && self.listView.isEditable() {
  1159. return false
  1160. }
  1161. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage())
  1162. }
  1163. if (menuItem.action == #selector(menuItemAction_forwardPage)) {
  1164. if self.listView.isEditing() && self.listView.isEditable() {
  1165. return false
  1166. }
  1167. return (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage())
  1168. }
  1169. if (menuItem.action == #selector(menuItemAction_firstPage)) {
  1170. return self.listView.canGoToFirstPage()
  1171. }
  1172. if (menuItem.action == #selector(menuItemAction_lastPage)) {
  1173. return self.listView.canGoToLastPage()
  1174. }
  1175. if (menuItem.action == #selector(menuItemAction_forward)) {
  1176. return self.listView.km_canGoForward()
  1177. }
  1178. if (menuItem.action == #selector(menuItemAction_goback)) {
  1179. return self.listView.km_canGoBack()
  1180. }
  1181. }
  1182. if menuItem.action == #selector(changeAnnotationMode) || menuItem.action == #selector(changeAnnotationMode_itemAction) {
  1183. // if ([[menuItem menu] numberOfItems] > ANNOTATION_MODE_COUNT)
  1184. // [menuItem setState:[self toolMode] == SKNoteToolMode && [self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1185. // else
  1186. // [menuItem setState:[self annotationMode] == (SKNoteType)[menuItem tag] ? NSOnState : NSOffState];
  1187. if self.listView.annotationType.rawValue == menuItem.tag {
  1188. menuItem.state = .on
  1189. } else {
  1190. menuItem.state = .off
  1191. }
  1192. return true
  1193. }
  1194. guard let action = menuItem.action else {
  1195. return false
  1196. }
  1197. if KMSystemMenu.isToolSelector(sel: action) {
  1198. if action == KMSystemMenu.Tool.autoSelectContentSelector {
  1199. let toolMode = self.listView?.toolMode ?? .none
  1200. return toolMode == .selectToolMode
  1201. }
  1202. }
  1203. if KMSystemMenu.isViewSelector(sel: action) {
  1204. if action == KMSystemMenu.View.toggleLeftSidePaneSelector {
  1205. if self.leftPanelOpen {
  1206. menuItem.title = NSLocalizedString("Hide Panel", comment: "Menu item title")
  1207. } else {
  1208. menuItem.title = NSLocalizedString("Show Panel", comment: "Menu item title")
  1209. }
  1210. return true
  1211. } else if action == KMSystemMenu.View.changeLeftSidePaneStateSelector {
  1212. if menuItem.tag == 3 { // 快照
  1213. menuItem.state = self.leftSideViewController.type.methodType == .snapshot ? .on : .off
  1214. }
  1215. return true
  1216. } else if action == KMSystemMenu.View.themesColorSelector {
  1217. let mode = menuItem.tag
  1218. if mode == 0 {
  1219. menuItem.state = self.listView.displayMode == .normal ? .on : .off
  1220. } else if mode == 1 {
  1221. menuItem.state = self.listView.displayMode == .soft ? .on : .off
  1222. } else if mode == 2 {
  1223. menuItem.state = self.listView.displayMode == .night ? .on : .off
  1224. } else if mode == 3 {
  1225. menuItem.state = self.listView.displayMode == .green ? .on : .off
  1226. }
  1227. return true
  1228. } else if action == KMSystemMenu.View.splitViewActionSelector {
  1229. let mode = menuItem.tag
  1230. if mode == 0 { // hor
  1231. menuItem.state = self.listView.viewSplitMode == .horizontal ? .on : .off
  1232. } else if mode == 1 { // verti
  1233. menuItem.state = self.listView.viewSplitMode == .vertical ? .on : .off
  1234. } else if mode == 2 { // single
  1235. menuItem.state = self.listView.viewSplitMode == .disable ? .on : .off
  1236. }
  1237. }
  1238. }
  1239. return true
  1240. }
  1241. }