Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. @IBAction func startSearch(sender: AnyObject) {
  2. let searchTerm: String = searchBox.stringValue
  3. if (searchTerm.isEmpty) {
  4. endSearch(self)
  5. return
  6. }
  7.  
  8. // the user can move forward to the next match by hitting enter/return
  9. if (canMoveToMatch(true)) {
  10. moveToMatch()
  11. return
  12. }
  13.  
  14. let doc: PDFDocument = model!.doc!
  15. found = doc.findString(searchTerm, withOptions: getSearchOption())
  16. searchMode = true
  17. searchResultsInfoBox.hidden = false
  18. searchResultsCount.stringValue = "Found \(found.count) matches"
  19. moveToMatch()
  20. }
  21.  
  22. func getSearchOption() -> Int {
  23. if (backwardSearchOption.state == NSOnState) {
  24. println("searching backwards")
  25. return backwardSearch
  26. }
  27. println("searching normally")
  28. return caseInsensitiveSearch
  29. }
  30.  
  31. @IBAction func navigateSearchResults(sender: AnyObject) {
  32. let selected: Int = sender.selectedSegment
  33. let back: Bool = backwardSearchOption.state == NSOnState
  34.  
  35. if (selected == 0) {
  36. if (canMoveToMatch(back)) {
  37. moveToMatch()
  38. }
  39. } else {
  40. if (canMoveToMatch(!back)) {
  41. moveToMatch()
  42. }
  43. }
  44. }
  45.  
  46. func canMoveToMatch(next: Bool) -> Bool {
  47. if (searchMode) {
  48. if (next) {
  49. if (matchNum >= 0 && matchNum < (found.count - 1)) {
  50. matchNum++
  51. return true
  52. }
  53. } else {
  54. if (matchNum > 0 && matchNum <= found.count) {
  55. matchNum--
  56. return true
  57. }
  58. }
  59. }
  60. return false
  61. }
  62.  
  63. func moveToMatch() {
  64. // highlight the occurrence of the search term
  65. documentView.setCurrentSelection(found[matchNum] as! PDFSelection)
  66. // scroll to the highlighted occurrence
  67. documentView.scrollSelectionToVisible(self)
  68. }
  69.  
  70. @IBAction func endSearch(sender: AnyObject) {
  71. let doc: PDFDocument = model!.doc!
  72. documentView.setCurrentSelection(nil)
  73. searchResultsInfoBox.hidden = true
  74. searchBox.stringValue = ""
  75. if (backwardSearchOption.state == NSOnState) {
  76. backwardSearchOption.state = NSOffState
  77. }
  78. window?.makeFirstResponder(nil)
  79. found.removeAll()
  80. matchNum = 0
  81. searchMode = false
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement