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