michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: var gFindBundle; michael@0: michael@0: function nsFindInstData() {} michael@0: nsFindInstData.prototype = michael@0: { michael@0: // set the next three attributes on your object to override the defaults michael@0: browser : null, michael@0: michael@0: get rootSearchWindow() { return this._root || this.window.content; }, michael@0: set rootSearchWindow(val) { this._root = val; }, michael@0: michael@0: get currentSearchWindow() { michael@0: if (this._current) michael@0: return this._current; michael@0: michael@0: var focusedWindow = this.window.document.commandDispatcher.focusedWindow; michael@0: if (!focusedWindow || focusedWindow == this.window) michael@0: focusedWindow = this.window.content; michael@0: michael@0: return focusedWindow; michael@0: }, michael@0: set currentSearchWindow(val) { this._current = val; }, michael@0: michael@0: get webBrowserFind() { return this.browser.webBrowserFind; }, michael@0: michael@0: init : function() { michael@0: var findInst = this.webBrowserFind; michael@0: // set up the find to search the focussedWindow, bounded by the content window. michael@0: var findInFrames = findInst.QueryInterface(Components.interfaces.nsIWebBrowserFindInFrames); michael@0: findInFrames.rootSearchFrame = this.rootSearchWindow; michael@0: findInFrames.currentSearchFrame = this.currentSearchWindow; michael@0: michael@0: // always search in frames for now. We could add a checkbox to the dialog for this. michael@0: findInst.searchFrames = true; michael@0: }, michael@0: michael@0: window : window, michael@0: _root : null, michael@0: _current : null michael@0: } michael@0: michael@0: // browser is the element michael@0: // rootSearchWindow is the window to constrain the search to (normally window.content) michael@0: // currentSearchWindow is the frame to start searching (can be, and normally, rootSearchWindow) michael@0: function findInPage(findInstData) michael@0: { michael@0: // is the dialog up already? michael@0: if ("findDialog" in window && window.findDialog) michael@0: window.findDialog.focus(); michael@0: else michael@0: { michael@0: findInstData.init(); michael@0: window.findDialog = window.openDialog("chrome://global/content/finddialog.xul", "_blank", "chrome,resizable=no,dependent=yes", findInstData); michael@0: } michael@0: } michael@0: michael@0: function findAgainInPage(findInstData, reverse) michael@0: { michael@0: if ("findDialog" in window && window.findDialog) michael@0: window.findDialog.focus(); michael@0: else michael@0: { michael@0: // get the find service, which stores global find state, and init the michael@0: // nsIWebBrowser find with it. We don't assume that there was a previous michael@0: // Find that set this up. michael@0: var findService = Components.classes["@mozilla.org/find/find_service;1"] michael@0: .getService(Components.interfaces.nsIFindService); michael@0: michael@0: var searchString = findService.searchString; michael@0: if (searchString.length == 0) { michael@0: // no previous find text michael@0: findInPage(findInstData); michael@0: return; michael@0: } michael@0: michael@0: findInstData.init(); michael@0: var findInst = findInstData.webBrowserFind; michael@0: findInst.searchString = searchString; michael@0: findInst.matchCase = findService.matchCase; michael@0: findInst.wrapFind = findService.wrapFind; michael@0: findInst.entireWord = findService.entireWord; michael@0: findInst.findBackwards = findService.findBackwards ^ reverse; michael@0: michael@0: var found = findInst.findNext(); michael@0: if (!found) { michael@0: if (!gFindBundle) michael@0: gFindBundle = document.getElementById("findBundle"); michael@0: michael@0: Services.prompt.alert(window, gFindBundle.getString("notFoundTitle"), gFindBundle.getString("notFoundWarning")); michael@0: } michael@0: michael@0: // Reset to normal value, otherwise setting can get changed in find dialog michael@0: findInst.findBackwards = findService.findBackwards; michael@0: } michael@0: } michael@0: michael@0: function canFindAgainInPage() michael@0: { michael@0: var findService = Components.classes["@mozilla.org/find/find_service;1"] michael@0: .getService(Components.interfaces.nsIFindService); michael@0: return (findService.searchString.length > 0); michael@0: } michael@0: