|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsCycleCollectionParticipant.h" |
|
7 #include "nsISelectionController.h" |
|
8 #include "nsIController.h" |
|
9 #include "nsIControllers.h" |
|
10 #include "nsIObserver.h" |
|
11 #include "nsUnicharUtils.h" |
|
12 #include "nsIFind.h" |
|
13 #include "nsIWebBrowserFind.h" |
|
14 #include "nsWeakReference.h" |
|
15 #include "nsISelection.h" |
|
16 #include "nsIDOMRange.h" |
|
17 #include "nsIDocShellTreeItem.h" |
|
18 #include "nsITypeAheadFind.h" |
|
19 #include "nsISound.h" |
|
20 |
|
21 class nsIPresShell; |
|
22 class nsPresContext; |
|
23 |
|
24 #define TYPEAHEADFIND_NOTFOUND_WAV_URL \ |
|
25 "chrome://global/content/notfound.wav" |
|
26 |
|
27 class nsTypeAheadFind : public nsITypeAheadFind, |
|
28 public nsIObserver, |
|
29 public nsSupportsWeakReference |
|
30 { |
|
31 public: |
|
32 nsTypeAheadFind(); |
|
33 virtual ~nsTypeAheadFind(); |
|
34 |
|
35 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
36 NS_DECL_NSITYPEAHEADFIND |
|
37 NS_DECL_NSIOBSERVER |
|
38 |
|
39 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTypeAheadFind, nsITypeAheadFind) |
|
40 |
|
41 protected: |
|
42 nsresult PrefsReset(); |
|
43 |
|
44 void SaveFind(); |
|
45 void PlayNotFoundSound(); |
|
46 nsresult GetWebBrowserFind(nsIDocShell *aDocShell, |
|
47 nsIWebBrowserFind **aWebBrowserFind); |
|
48 |
|
49 void RangeStartsInsideLink(nsIDOMRange *aRange, nsIPresShell *aPresShell, |
|
50 bool *aIsInsideLink, bool *aIsStartingLink); |
|
51 |
|
52 void GetSelection(nsIPresShell *aPresShell, nsISelectionController **aSelCon, |
|
53 nsISelection **aDomSel); |
|
54 // *aNewRange may not be collapsed. If you want to collapse it in a |
|
55 // particular way, you need to do it yourself. |
|
56 bool IsRangeVisible(nsIPresShell *aPresShell, nsPresContext *aPresContext, |
|
57 nsIDOMRange *aRange, bool aMustBeVisible, |
|
58 bool aGetTopVisibleLeaf, nsIDOMRange **aNewRange, |
|
59 bool *aUsesIndependentSelection); |
|
60 nsresult FindItNow(nsIPresShell *aPresShell, bool aIsLinksOnly, |
|
61 bool aIsFirstVisiblePreferred, bool aFindPrev, |
|
62 uint16_t* aResult); |
|
63 nsresult GetSearchContainers(nsISupports *aContainer, |
|
64 nsISelectionController *aSelectionController, |
|
65 bool aIsFirstVisiblePreferred, |
|
66 bool aFindPrev, nsIPresShell **aPresShell, |
|
67 nsPresContext **aPresContext); |
|
68 |
|
69 // Get the pres shell from mPresShell and return it only if it is still |
|
70 // attached to the DOM window. |
|
71 NS_HIDDEN_(already_AddRefed<nsIPresShell>) GetPresShell(); |
|
72 |
|
73 // Current find state |
|
74 nsString mTypeAheadBuffer; |
|
75 nsCString mNotFoundSoundURL; |
|
76 |
|
77 // PRBools are used instead of PRPackedBools because the address of the |
|
78 // boolean variable is getting passed into a method. |
|
79 bool mStartLinksOnlyPref; |
|
80 bool mCaretBrowsingOn; |
|
81 nsCOMPtr<nsIDOMElement> mFoundLink; // Most recent elem found, if a link |
|
82 nsCOMPtr<nsIDOMElement> mFoundEditable; // Most recent elem found, if editable |
|
83 nsCOMPtr<nsIDOMWindow> mCurrentWindow; |
|
84 // mLastFindLength is the character length of the last find string. It is used for |
|
85 // disabling the "not found" sound when using backspace or delete |
|
86 uint32_t mLastFindLength; |
|
87 |
|
88 // Sound is played asynchronously on some platforms. |
|
89 // If we destroy mSoundInterface before sound has played, it won't play |
|
90 nsCOMPtr<nsISound> mSoundInterface; |
|
91 bool mIsSoundInitialized; |
|
92 |
|
93 // where selection was when user started the find |
|
94 nsCOMPtr<nsIDOMRange> mStartFindRange; |
|
95 nsCOMPtr<nsIDOMRange> mSearchRange; |
|
96 nsCOMPtr<nsIDOMRange> mStartPointRange; |
|
97 nsCOMPtr<nsIDOMRange> mEndPointRange; |
|
98 |
|
99 // Cached useful interfaces |
|
100 nsCOMPtr<nsIFind> mFind; |
|
101 |
|
102 bool mCaseSensitive; |
|
103 |
|
104 bool EnsureFind() { |
|
105 if (mFind) { |
|
106 return true; |
|
107 } |
|
108 |
|
109 mFind = do_CreateInstance("@mozilla.org/embedcomp/rangefind;1"); |
|
110 if (!mFind) { |
|
111 return false; |
|
112 } |
|
113 |
|
114 mFind->SetCaseSensitive(mCaseSensitive); |
|
115 mFind->SetWordBreaker(nullptr); |
|
116 |
|
117 return true; |
|
118 } |
|
119 |
|
120 nsCOMPtr<nsIWebBrowserFind> mWebBrowserFind; |
|
121 |
|
122 // The focused content window that we're listening to and its cached objects |
|
123 nsWeakPtr mDocShell; |
|
124 nsWeakPtr mPresShell; |
|
125 nsWeakPtr mSelectionController; |
|
126 // Most recent match's controller |
|
127 }; |