Back to index

lightning-sunbird  0.9+nobinonly
nsTypeAheadFind.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Aaron Leventhal (aaronl@netscape.com)
00024  *   Blake Ross      (blake@cs.stanford.edu)
00025  *   Masayuki Nakano (masayuki@d-toybox.com)
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsISelectionController.h"
00042 #include "nsIController.h"
00043 #include "nsIControllers.h"
00044 #include "nsIObserver.h"
00045 #include "nsUnicharUtils.h"
00046 #include "nsIFind.h"
00047 #include "nsIWebBrowserFind.h"
00048 #include "nsWeakReference.h"
00049 #include "nsIPresShell.h"
00050 #include "nsPresContext.h"
00051 #include "nsISelection.h"
00052 #include "nsIDOMRange.h"
00053 #include "nsIDocShellTreeItem.h"
00054 #include "nsITypeAheadFind.h"
00055 #include "nsISupportsArray.h"
00056 #include "nsISound.h"
00057 
00058 #define TYPEAHEADFIND_NOTFOUND_WAV_URL \
00059         "chrome://global/content/notfound.wav"
00060 
00061 enum {
00062   eRepeatingNone,
00063   eRepeatingChar,
00064   eRepeatingCharReverse,
00065   eRepeatingForward,
00066   eRepeatingReverse
00067 }; 
00068 
00069 const int kMaxBadCharsBeforeCancel = 3;
00070 
00071 class nsTypeAheadFind : public nsITypeAheadFind_MOZILLA_1_8_BRANCH,
00072                         public nsIObserver,
00073                         public nsSupportsWeakReference
00074 {
00075 public:
00076   nsTypeAheadFind();
00077   virtual ~nsTypeAheadFind();
00078 
00079   NS_DECL_ISUPPORTS
00080   NS_DECL_NSITYPEAHEADFIND
00081   NS_DECL_NSITYPEAHEADFIND_MOZILLA_1_8_BRANCH
00082   NS_DECL_NSIOBSERVER
00083 
00084 protected:
00085   nsresult PrefsReset();
00086 
00087   void SaveFind();
00088   void PlayNotFoundSound(); 
00089   nsresult FindInternal(PRBool aFindBackwards, PRUint16* aResult);
00090   nsresult GetWebBrowserFind(nsIDocShell *aDocShell,
00091                              nsIWebBrowserFind **aWebBrowserFind);
00092 
00093   void RangeStartsInsideLink(nsIDOMRange *aRange, nsIPresShell *aPresShell, 
00094                              PRBool *aIsInsideLink, PRBool *aIsStartingLink);
00095 
00096   void GetSelection(nsIPresShell *aPresShell, nsISelectionController **aSelCon, 
00097                     nsISelection **aDomSel);
00098   PRBool FindFieldHasFocus(nsPresContext *aPresContext);
00099   PRBool IsRangeVisible(nsIPresShell *aPresShell, nsPresContext *aPresContext,
00100                         nsIDOMRange *aRange, PRBool aMustBeVisible, 
00101                         PRBool aGetTopVisibleLeaf, nsIDOMRange **aNewRange,
00102                         PRBool *aUsesIndependentSelection);
00103   nsresult FindItNow(nsIPresShell *aPresShell, PRBool aIsRepeatingSameChar, 
00104                      PRBool aIsLinksOnly, PRBool aIsFirstVisiblePreferred, 
00105                      PRBool aFindNext, PRUint16* aResult);
00106   nsresult GetSearchContainers(nsISupports *aContainer,
00107                                nsISelectionController *aSelectionController,
00108                                PRBool aIsRepeatingSameChar,
00109                                PRBool aIsFirstVisiblePreferred, 
00110                                nsIPresShell **aPresShell, 
00111                                nsPresContext **aPresContext);
00112 
00113   nsresult Cancel();
00114 
00115   // Get the pres shell from mPresShell and return it only if it is still
00116   // attached to the DOM window.
00117   NS_HIDDEN_(already_AddRefed<nsIPresShell>) GetPresShell();
00118 
00119   // Current find state
00120   nsString mTypeAheadBuffer;
00121   nsCString mNotFoundSoundURL;
00122 
00123   // PRBool's are used instead of PRPackedBool's where the address of the
00124   // boolean variable is getting passed into a method. For example:
00125   // GetBoolPref("accessibility.typeaheadfind.linksonly", &mLinksOnlyPref);
00126   PRBool mLinksOnlyPref;
00127   PRBool mStartLinksOnlyPref;
00128   PRPackedBool mLinksOnly;
00129   PRBool mCaretBrowsingOn;
00130   nsCOMPtr<nsIDOMElement> mFoundLink;     // Most recent elem found, if a link
00131   nsCOMPtr<nsIDOMElement> mFoundEditable; // Most recent elem found, if editable
00132   nsCOMPtr<nsIDOMWindow> mCurrentWindow;
00133   PRPackedBool mLiteralTextSearchOnly;
00134   PRPackedBool mDontTryExactMatch;
00135   // mAllTheSame Char starts out PR_TRUE, becomes false when 
00136   // at least 2 different chars typed
00137   PRPackedBool mAllTheSameChar;
00138   PRInt32 mRepeatingMode;
00139   // mLastFindLength is the character length of the last find string.  It is used for
00140   // disabling the "not found" sound when using backspace or delete
00141   PRUint32 mLastFindLength;
00142 
00143   // Sound is played asynchronously on some platforms.
00144   // If we destroy mSoundInterface before sound has played, it won't play
00145   nsCOMPtr<nsISound> mSoundInterface;
00146   PRBool mIsSoundInitialized;
00147   
00148   // where selection was when user started the find
00149   nsCOMPtr<nsIDOMRange> mStartFindRange;
00150   nsCOMPtr<nsIDOMRange> mSearchRange;
00151   nsCOMPtr<nsIDOMRange> mStartPointRange;
00152   nsCOMPtr<nsIDOMRange> mEndPointRange;
00153 
00154   // Cached useful interfaces
00155   nsCOMPtr<nsIFind> mFind;
00156   nsCOMPtr<nsIWebBrowserFind> mWebBrowserFind;
00157 
00158   // The focused content window that we're listening to and its cached objects
00159   nsWeakPtr mDocShell;
00160   nsWeakPtr mPresShell;
00161   nsWeakPtr mSelectionController;
00162                                           // Most recent match's controller
00163 };