Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @IBAction func startSearch(sender: AnyObject) {
- let searchTerm: String = searchBox.stringValue
- if (searchTerm.isEmpty) {
- endSearch(self)
- return
- }
- // the user can move forward to the next match by hitting enter/return
- if (canMoveToMatch(true)) {
- moveToMatch()
- return
- }
- let doc: PDFDocument = model!.doc!
- found = doc.findString(searchTerm, withOptions: getSearchOption())
- searchMode = true
- searchResultsInfoBox.hidden = false
- searchResultsCount.stringValue = "Found \(found.count) matches"
- moveToMatch()
- }
- func getSearchOption() -> Int {
- if (backwardSearchOption.state == NSOnState) {
- println("searching backwards")
- return backwardSearch
- }
- println("searching normally")
- return caseInsensitiveSearch
- }
- @IBAction func navigateSearchResults(sender: AnyObject) {
- let selected: Int = sender.selectedSegment
- let back: Bool = backwardSearchOption.state == NSOnState
- if (selected == 0) {
- if (canMoveToMatch(back)) {
- moveToMatch()
- }
- } else {
- if (canMoveToMatch(!back)) {
- moveToMatch()
- }
- }
- }
- func canMoveToMatch(next: Bool) -> Bool {
- if (searchMode) {
- if (next) {
- if (matchNum >= 0 && matchNum < (found.count - 1)) {
- matchNum++
- return true
- }
- } else {
- if (matchNum > 0 && matchNum <= found.count) {
- matchNum--
- return true
- }
- }
- }
- return false
- }
- func moveToMatch() {
- // highlight the occurrence of the search term
- documentView.setCurrentSelection(found[matchNum] as! PDFSelection)
- // scroll to the highlighted occurrence
- documentView.scrollSelectionToVisible(self)
- }
- @IBAction func endSearch(sender: AnyObject) {
- let doc: PDFDocument = model!.doc!
- documentView.setCurrentSelection(nil)
- searchResultsInfoBox.hidden = true
- searchBox.stringValue = ""
- if (backwardSearchOption.state == NSOnState) {
- backwardSearchOption.state = NSOffState
- }
- window?.makeFirstResponder(nil)
- found.removeAll()
- matchNum = 0
- searchMode = false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement