diff --git a/mozilla/embedding/browser/webBrowser/nsIWebBrowserFind.idl b/mozilla/embedding/browser/webBrowser/nsIWebBrowserFind.idl new file mode 100644 index 00000000000..88456fb7cd4 --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsIWebBrowserFind.idl @@ -0,0 +1,54 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 2001 Netscape Communications Corporation. All + * Rights Reserved. + * + * Author: + * Conrad Carlen + * + * Contributor(s): + */ + +#include "nsISupports.idl" + + +// +// nsIWebBrowserFind interface +// +// Searches for text in a web browser. +// + +[scriptable, uuid(2f977d44-5485-11d4-87e2-0010a4e75ef2)] +interface nsIWebBrowserFind : nsISupports +{ + /** + Finds the next occurance of the string using the + current attributes. At least the searchString + attribute must have been set before calling this + or NS_ERROR_NOT_INITIALIZED will be returned. + All other attributes can be defaults. + */ + + boolean findNext(); + + attribute wstring searchString; + + attribute boolean findBackwards; + attribute boolean wrapFind; + attribute boolean entireWord; + attribute boolean matchCase; +}; diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowserFind.cpp b/mozilla/embedding/browser/webBrowser/nsWebBrowserFind.cpp new file mode 100644 index 00000000000..5dac6df3a0b --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowserFind.cpp @@ -0,0 +1,140 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Mozilla browser. + * + * The Initial Developer of the Original Code is Netscape + * Communications, Inc. Portions created by Netscape are + * Copyright (C) 1999, Mozilla. All Rights Reserved. + * + * Author: + * Conrad Carlen + */ + +#include "nsWebBrowserFind.h" + +#include "nsCOMPtr.h" +#include "nsIComponentManager.h" +#include "nsITextServicesDocument.h" +#include "nsTextServicesCID.h" +#include "nsIScriptGlobalObject.h" +#include "nsIDOMWindow.h" +#include "nsIDocShell.h" +#include "nsIPresShell.h" +#include "nsIDocument.h" +#include "nsIDOMDocument.h" + + +static NS_DEFINE_CID(kCTextServicesDocumentCID, NS_TEXTSERVICESDOCUMENT_CID); + +//***************************************************************************** +// nsWebBrowserFindImpl +//***************************************************************************** + + +nsWebBrowserFindImpl::nsWebBrowserFindImpl() : + mFindBackwards(PR_FALSE), mWrapFind(PR_FALSE), + mEntireWord(PR_FALSE), mMatchCase(PR_FALSE) +{ +} + +nsWebBrowserFindImpl::~nsWebBrowserFindImpl() +{ +} + +nsresult nsWebBrowserFindImpl::Init() +{ + nsresult rv; + mTSFind = do_CreateInstance(NS_FINDANDREPLACE_CONTRACTID, &rv); + return rv; +} + +nsresult nsWebBrowserFindImpl::SetSearchString(const PRUnichar* aString) +{ + mSearchString = aString; + return NS_OK; +} + +nsresult nsWebBrowserFindImpl::GetSearchString(PRUnichar** aString) +{ + NS_ENSURE_ARG_POINTER(aString); + *aString = mSearchString.ToNewUnicode(); + return *aString ? NS_OK : NS_ERROR_FAILURE; +} + +nsresult nsWebBrowserFindImpl::DoFind(nsIDOMWindow* aWindow, PRBool* aDidFind) +{ + NS_ENSURE_TRUE(mTSFind, NS_ERROR_NOT_INITIALIZED); + + nsresult rv; + nsCOMPtr txtDoc; + rv = MakeTSDocument(aWindow, getter_AddRefs(txtDoc)); + if (NS_FAILED(rv) || !txtDoc) + return rv; + + (void) mTSFind->SetCaseSensitive(mMatchCase); + (void) mTSFind->SetFindBackwards(mFindBackwards); + (void) mTSFind->SetWrapFind(mWrapFind); + (void) mTSFind->SetEntireWord(mEntireWord); + + rv = mTSFind->SetTsDoc(txtDoc); + if (NS_FAILED(rv)) + return rv; + + rv = mTSFind->Find(mSearchString.GetUnicode(), aDidFind); + + mTSFind->SetTsDoc(nsnull); + + return rv; +} + +nsresult nsWebBrowserFindImpl::MakeTSDocument(nsIDOMWindow* aWindow, nsITextServicesDocument** aDoc) +{ + NS_ENSURE_ARG(aWindow); + NS_ENSURE_ARG_POINTER(aDoc); + + nsresult rv; + *aDoc = NULL; + + nsCOMPtr tempDoc(do_CreateInstance(kCTextServicesDocumentCID, &rv)); + if (NS_FAILED(rv) || !tempDoc) + return rv; + + nsCOMPtr globalObj = do_QueryInterface(aWindow, &rv); + if (NS_FAILED(rv) || !globalObj) + return NS_ERROR_FAILURE; + + nsCOMPtr docShell; + globalObj->GetDocShell(getter_AddRefs(docShell)); + if (!docShell) + return NS_ERROR_FAILURE; + + nsCOMPtr presShell; + docShell->GetPresShell(getter_AddRefs(presShell)); + if (!presShell) + return NS_ERROR_FAILURE; + + nsCOMPtr domDoc; + rv = aWindow->GetDocument(getter_AddRefs(domDoc)); + if (!domDoc) + return NS_ERROR_FAILURE; + + rv = tempDoc->InitWithDocument(domDoc, presShell); + if (NS_FAILED(rv)) + return rv; + + // Return the resulting text services document. + *aDoc = tempDoc; + NS_IF_ADDREF(*aDoc); + + return rv; +} diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowserFind.h b/mozilla/embedding/browser/webBrowser/nsWebBrowserFind.h new file mode 100644 index 00000000000..6c3a6f16ccf --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowserFind.h @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Mozilla browser. + * + * The Initial Developer of the Original Code is Netscape + * Communications, Inc. Portions created by Netscape are + * Copyright (C) 1999, Mozilla. All Rights Reserved. + * + * Author: + * Conrad Carlen + */ + +#ifndef nsWebBrowserFindImpl_h__ +#define nsWebBrowserFindImpl_h__ + +#include "nsString.h" +#include "nsCOMPtr.h" +#include "nsIFindAndReplace.h" + +class nsIDOMWindow; +class nsITextServicesDocument; + +//***************************************************************************** +// class nsWebBrowserFindImpl +//***************************************************************************** + +class nsWebBrowserFindImpl +{ +public: + nsWebBrowserFindImpl(); + ~nsWebBrowserFindImpl(); + + nsresult Init(); // Must be called after constructor + + nsresult SetSearchString(const PRUnichar* aString); + nsresult GetSearchString(PRUnichar** aString); + + PRBool GetFindBackwards() + { return mFindBackwards; } + void SetFindBackwards(PRBool aFindBackwards) + { mFindBackwards = aFindBackwards; } + + PRBool GetWrapFind() + { return mWrapFind; } + void SetWrapFind(PRBool aWrapFind) + { mWrapFind = aWrapFind; } + + PRBool GetEntireWord() + { return mEntireWord; } + void SetEntireWord(PRBool aEntireWord) + { mEntireWord = aEntireWord; } + + PRBool GetMatchCase() + { return mMatchCase; } + void SetMatchCase(PRBool aMatchCase) + { mMatchCase = aMatchCase; } + + PRBool CanFindNext() + { return mSearchString.Length() != 0; } + + nsresult DoFind(nsIDOMWindow* aWindow, PRBool* didFind); + +private: + nsresult MakeTSDocument(nsIDOMWindow* aWindow, nsITextServicesDocument** aDoc); + + nsString mSearchString; + PRBool mFindBackwards, mWrapFind, mEntireWord, mMatchCase; + + nsCOMPtr mTSFind; +}; + +#endif