|
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
8 |
|
9 var gFindBundle; |
|
10 |
|
11 function nsFindInstData() {} |
|
12 nsFindInstData.prototype = |
|
13 { |
|
14 // set the next three attributes on your object to override the defaults |
|
15 browser : null, |
|
16 |
|
17 get rootSearchWindow() { return this._root || this.window.content; }, |
|
18 set rootSearchWindow(val) { this._root = val; }, |
|
19 |
|
20 get currentSearchWindow() { |
|
21 if (this._current) |
|
22 return this._current; |
|
23 |
|
24 var focusedWindow = this.window.document.commandDispatcher.focusedWindow; |
|
25 if (!focusedWindow || focusedWindow == this.window) |
|
26 focusedWindow = this.window.content; |
|
27 |
|
28 return focusedWindow; |
|
29 }, |
|
30 set currentSearchWindow(val) { this._current = val; }, |
|
31 |
|
32 get webBrowserFind() { return this.browser.webBrowserFind; }, |
|
33 |
|
34 init : function() { |
|
35 var findInst = this.webBrowserFind; |
|
36 // set up the find to search the focussedWindow, bounded by the content window. |
|
37 var findInFrames = findInst.QueryInterface(Components.interfaces.nsIWebBrowserFindInFrames); |
|
38 findInFrames.rootSearchFrame = this.rootSearchWindow; |
|
39 findInFrames.currentSearchFrame = this.currentSearchWindow; |
|
40 |
|
41 // always search in frames for now. We could add a checkbox to the dialog for this. |
|
42 findInst.searchFrames = true; |
|
43 }, |
|
44 |
|
45 window : window, |
|
46 _root : null, |
|
47 _current : null |
|
48 } |
|
49 |
|
50 // browser is the <browser> element |
|
51 // rootSearchWindow is the window to constrain the search to (normally window.content) |
|
52 // currentSearchWindow is the frame to start searching (can be, and normally, rootSearchWindow) |
|
53 function findInPage(findInstData) |
|
54 { |
|
55 // is the dialog up already? |
|
56 if ("findDialog" in window && window.findDialog) |
|
57 window.findDialog.focus(); |
|
58 else |
|
59 { |
|
60 findInstData.init(); |
|
61 window.findDialog = window.openDialog("chrome://global/content/finddialog.xul", "_blank", "chrome,resizable=no,dependent=yes", findInstData); |
|
62 } |
|
63 } |
|
64 |
|
65 function findAgainInPage(findInstData, reverse) |
|
66 { |
|
67 if ("findDialog" in window && window.findDialog) |
|
68 window.findDialog.focus(); |
|
69 else |
|
70 { |
|
71 // get the find service, which stores global find state, and init the |
|
72 // nsIWebBrowser find with it. We don't assume that there was a previous |
|
73 // Find that set this up. |
|
74 var findService = Components.classes["@mozilla.org/find/find_service;1"] |
|
75 .getService(Components.interfaces.nsIFindService); |
|
76 |
|
77 var searchString = findService.searchString; |
|
78 if (searchString.length == 0) { |
|
79 // no previous find text |
|
80 findInPage(findInstData); |
|
81 return; |
|
82 } |
|
83 |
|
84 findInstData.init(); |
|
85 var findInst = findInstData.webBrowserFind; |
|
86 findInst.searchString = searchString; |
|
87 findInst.matchCase = findService.matchCase; |
|
88 findInst.wrapFind = findService.wrapFind; |
|
89 findInst.entireWord = findService.entireWord; |
|
90 findInst.findBackwards = findService.findBackwards ^ reverse; |
|
91 |
|
92 var found = findInst.findNext(); |
|
93 if (!found) { |
|
94 if (!gFindBundle) |
|
95 gFindBundle = document.getElementById("findBundle"); |
|
96 |
|
97 Services.prompt.alert(window, gFindBundle.getString("notFoundTitle"), gFindBundle.getString("notFoundWarning")); |
|
98 } |
|
99 |
|
100 // Reset to normal value, otherwise setting can get changed in find dialog |
|
101 findInst.findBackwards = findService.findBackwards; |
|
102 } |
|
103 } |
|
104 |
|
105 function canFindAgainInPage() |
|
106 { |
|
107 var findService = Components.classes["@mozilla.org/find/find_service;1"] |
|
108 .getService(Components.interfaces.nsIFindService); |
|
109 return (findService.searchString.length > 0); |
|
110 } |
|
111 |