Change the Find interface to use an nsIDOMWindow instead of a webshell, for better scriptability.

git-svn-id: svn://10.0.0.236/trunk@68016 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
sfraser%netscape.com 2000-05-03 02:24:16 +00:00
parent b4417a1d04
commit 5f7584275a
8 changed files with 110 additions and 143 deletions

View File

@ -50,7 +50,6 @@
#include "nsNetUtil.h"
#include "nsIScriptGlobalObject.h"
#include "nsIWebShellWindow.h"
#include "nsIWebNavigation.h"
#include "nsCOMPtr.h"
@ -2572,8 +2571,7 @@ nsEditorShell::DoFind(PRBool aFindNext)
// make the search context if we need to
if (!mSearchContext)
{
nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(mContentAreaDocShell));
rv = findComponent->CreateContext( webShell, nsnull, getter_AddRefs(mSearchContext));
rv = findComponent->CreateContext(mContentWindow, nsnull, getter_AddRefs(mSearchContext));
}
if (NS_SUCCEEDED(rv))

View File

@ -50,7 +50,6 @@
#include "nsNetUtil.h"
#include "nsIScriptGlobalObject.h"
#include "nsIWebShellWindow.h"
#include "nsIWebNavigation.h"
#include "nsCOMPtr.h"
@ -2572,8 +2571,7 @@ nsEditorShell::DoFind(PRBool aFindNext)
// make the search context if we need to
if (!mSearchContext)
{
nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(mContentAreaDocShell));
rv = findComponent->CreateContext( webShell, nsnull, getter_AddRefs(mSearchContext));
rv = findComponent->CreateContext(mContentWindow, nsnull, getter_AddRefs(mSearchContext));
}
if (NS_SUCCEEDED(rv))

View File

@ -372,9 +372,18 @@ nsMessenger::InitializeSearch( nsIFindComponent *finder )
nsresult rv = NS_OK;
if (!finder) return NS_ERROR_NULL_POINTER;
if (!mSearchContext ) {
if (!mSearchContext )
{
nsCOMPtr<nsIInterfaceRequestor> docShellIR = do_QueryInterface(mWebShell);
if (!docShellIR) return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMWindow> domWindow;
docShellIR->GetInterface(NS_GET_IID(nsIDOMWindow), getter_AddRefs(domWindow));
if (!domWindow) return NS_ERROR_FAILURE;
// we need to get the nsIDOMWindow for mWebShell
// Create the search context for this browser window.
rv = finder->CreateContext( mWebShell, nsnull, getter_AddRefs(mSearchContext));
rv = finder->CreateContext(domWindow, nsnull, getter_AddRefs(mSearchContext));
}
return rv;

View File

@ -1528,8 +1528,7 @@ nsBrowserInstance::InitializeSearch( nsIFindComponent *finder )
if (!mSearchContext ) {
// Create the search context for this browser window.
nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(mContentAreaDocShell));
rv = finder->CreateContext( webShell, nsnull, getter_AddRefs(mSearchContext));
rv = finder->CreateContext(mContentWindow, nsnull, getter_AddRefs(mSearchContext));
}
return rv;
}

View File

@ -22,12 +22,11 @@
#include "nsIAppShellComponent.idl"
[ptr] native nsIWebShell( nsIWebShell );
[ptr] native nsIEditor( nsIEditor );
#include "nsIEditorShell.idl"
%{C++
class nsIWebShell;
class nsIEditor;
class nsIDOMWindow;
class nsIEditorShell;
%}
/*----------------------------- nsIFindComponent -------------------------------
@ -58,7 +57,8 @@ class nsIEditor;
| browser window closes. |
------------------------------------------------------------------------------*/
[scriptable, uuid(a6cf90ee-15b3-11d2-932e-00805f8add32)]
interface nsIFindComponent : nsIAppShellComponent {
interface nsIFindComponent : nsIAppShellComponent
{
/*---------------------------- CreateContext -------------------------------
| Create a "search context" for the given document. Subsequent Find and |
@ -74,8 +74,7 @@ interface nsIFindComponent : nsIAppShellComponent {
| Clients do have to call Release() when they're no longer interested in |
| this search context. |
--------------------------------------------------------------------------*/
[noscript] nsISupports CreateContext( in nsIWebShell aWebShell,
in nsIEditor aEditor );
nsISupports CreateContext(in nsIDOMWindow aWindow, in nsIEditorShell aEditor);
/*--------------------------------- Find -----------------------------------
| Finds the "first" occurrence of a string in the given search context |
@ -113,9 +112,8 @@ interface nsIFindComponent : nsIAppShellComponent {
| this will be the equivalent of calling Release() on the old context and |
| then creating a new one for aNewWebShell. |
--------------------------------------------------------------------------*/
[noscript] void ResetContext( in nsISupports aContext,
in nsIWebShell aNewWebShell,
in nsIEditor aEditor );
void ResetContext(in nsISupports aContext, in nsIDOMWindow aWindow,
in nsIEditorShell aEditor );
};
%{C++

View File

@ -47,7 +47,7 @@ interface nsISearchContext : nsISupports {
attribute boolean searchBackwards;
attribute boolean caseSensitive;
attribute boolean wrapSearch;
[noscript] readonly attribute nsIWebShell targetWebShell;
readonly attribute nsIDOMWindow targetWindow;
attribute nsIDOMWindow findDialog;
void ConvertToWeakReference();
void ConvertToOwningReference();

View File

@ -78,25 +78,25 @@ nsFindComponent::Context::~Context()
}
NS_IMETHODIMP
nsFindComponent::Context::Init( nsIWebShell *aWebShell,
nsIEditor* aEditor,
nsFindComponent::Context::Init( nsIDOMWindow *aWindow,
nsIEditorShell* aEditorShell,
const nsString& lastSearchString,
const nsString& lastReplaceString,
PRBool lastCaseSensitive,
PRBool lastSearchBackward,
PRBool lastWrapSearch)
{
if (!aWebShell)
if (!aWindow)
return NS_ERROR_INVALID_ARG;
mEditor = aEditor; // don't AddRef
mTargetWebShell = aWebShell; // don't AddRef
mEditorShell = aEditorShell; // don't AddRef
mTargetWindow = aWindow; // don't AddRef
mSearchString = lastSearchString;
mReplaceString = lastReplaceString;
mCaseSensitive = lastCaseSensitive;
mSearchBackwards = lastSearchBackward;
mWrapSearch = lastWrapSearch;
mFindDialog = 0;
mFindDialog = 0;
return NS_OK;
}
@ -106,9 +106,9 @@ static NS_DEFINE_CID(kCTextServicesDocumentCID, NS_TEXTSERVICESDOCUMENT_CID);
NS_IMETHODIMP
nsFindComponent::Context::MakeTSDocument(nsIWebShell* aWebShell, nsITextServicesDocument** aDoc)
nsFindComponent::Context::MakeTSDocument(nsIDOMWindow* aWindow, nsITextServicesDocument** aDoc)
{
if (!aWebShell)
if (!aWindow)
return NS_ERROR_INVALID_ARG;
if (!aDoc)
@ -125,32 +125,25 @@ nsFindComponent::Context::MakeTSDocument(nsIWebShell* aWebShell, nsITextServices
if (NS_FAILED(rv) || !tempDoc)
return rv;
// Get content viewer from the web shell.
nsCOMPtr<nsIContentViewer> contentViewer;
nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aWebShell));
rv = docShell->GetContentViewer(getter_AddRefs(contentViewer));
if (NS_FAILED(rv) || !contentViewer)
return rv;
nsCOMPtr<nsIScriptGlobalObject> globalObj = do_QueryInterface(aWindow, &rv);
if (NS_FAILED(rv) || !globalObj)
return NS_ERROR_FAILURE;
// Up-cast to a document viewer.
nsCOMPtr<nsIDocumentViewer> docViewer = do_QueryInterface(contentViewer, &rv);
if (NS_FAILED(rv) || !docViewer)
return rv;
// Get the document and pres shell from the doc viewer.
nsCOMPtr<nsIDocument> document;
nsCOMPtr<nsIPresShell> presShell;
rv = docViewer->GetDocument(*getter_AddRefs(document));
if (document)
rv = docViewer->GetPresShell(*getter_AddRefs(presShell));
nsCOMPtr<nsIDocShell> docShell;
globalObj->GetDocShell(getter_AddRefs(docShell));
if (!docShell)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIPresShell> presShell;
docShell->GetPresShell(getter_AddRefs(presShell));
if (!presShell)
return NS_ERROR_FAILURE;
if (NS_FAILED(rv) || !document || !presShell)
return rv;
// Upcast document to a DOM document.
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(document, &rv);
if (NS_FAILED(rv) || !domDoc)
return rv;
nsCOMPtr<nsIDocument> document;
presShell->GetDocument(getter_AddRefs(document));
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(document);
if (!domDoc)
return NS_ERROR_FAILURE;
// Initialize the text services document.
rv = tempDoc->InitWithDocument(domDoc, presShell);
@ -432,7 +425,7 @@ nsFindComponent::Context::DoFind(PRBool *aDidFind)
if (!aDidFind)
return NS_ERROR_NULL_POINTER;
if (!mTargetWebShell)
if (!mTargetWindow)
return NS_ERROR_NOT_INITIALIZED;
*aDidFind = PR_FALSE;
@ -446,7 +439,7 @@ nsFindComponent::Context::DoFind(PRBool *aDidFind)
// Construct a text services document to use. This is freed when we
// return from this function.
nsCOMPtr<nsITextServicesDocument> txtDoc;
rv = MakeTSDocument(mTargetWebShell, getter_AddRefs(txtDoc));
rv = MakeTSDocument(mTargetWindow, getter_AddRefs(txtDoc));
if (NS_FAILED(rv) || !txtDoc)
return rv;
@ -574,12 +567,12 @@ nsFindComponent::Context::DoReplace()
}
NS_IMETHODIMP
nsFindComponent::Context::Reset( nsIWebShell *aNewWebShell )
nsFindComponent::Context::Reset( nsIDOMWindow *aNewWindow )
{
if (!aNewWebShell)
if (!aNewWindow)
return NS_ERROR_INVALID_ARG;
mTargetWebShell = aNewWebShell; // don't AddRef
mTargetWindow = aNewWindow; // don't AddRef
return NS_OK;
}
@ -673,56 +666,47 @@ nsFindComponent::Context::GetWrapSearch(PRBool *aBool) {
}
NS_IMETHODIMP
nsFindComponent::Context::SetWrapSearch(PRBool aBool) {
nsFindComponent::Context::SetWrapSearch(PRBool aBool)
{
nsresult rv = NS_OK;
mWrapSearch = aBool;
return rv;
}
NS_IMETHODIMP
nsFindComponent::Context::GetTargetWebShell( nsIWebShell * *aWebShell) {
nsresult rv = NS_OK;
if ( aWebShell ) {
*aWebShell = mTargetWebShell;
if ( mTargetWebShell ) {
mTargetWebShell->AddRef();
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
nsFindComponent::Context::GetTargetWindow( nsIDOMWindow * *aWindow)
{
NS_ENSURE_ARG_POINTER(aWindow);
NS_IF_ADDREF(*aWindow = mTargetWindow);
return NS_OK;
}
NS_IMETHODIMP
nsFindComponent::Context::GetFindDialog( nsIDOMWindow * *aDialog) {
nsresult rv = NS_OK;
if ( aDialog ) {
*aDialog = mFindDialog;
if ( mFindDialog ) {
mFindDialog->AddRef();
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
nsFindComponent::Context::GetFindDialog( nsIDOMWindow * *aDialog)
{
NS_ENSURE_ARG_POINTER(aDialog);
NS_IF_ADDREF(*aDialog = mFindDialog);
return NS_OK;
}
NS_IMETHODIMP
nsFindComponent::Context::SetFindDialog( nsIDOMWindow *aDialog ) {
nsresult rv = NS_OK;
mFindDialog = aDialog;
return rv;
nsFindComponent::Context::SetFindDialog( nsIDOMWindow *aDialog )
{
mFindDialog = aDialog;
return NS_OK;
}
NS_IMETHODIMP
nsFindComponent::Context::ConvertToWeakReference() {
nsFindComponent::Context::ConvertToWeakReference()
{
NS_ASSERTION( mRefCnt >= 1, "Can't convert last reference to a weak one!" );
this->Release();
return NS_OK;
};
NS_IMETHODIMP
nsFindComponent::Context::ConvertToOwningReference() {
nsFindComponent::Context::ConvertToOwningReference()
{
this->AddRef();
return NS_OK;
};
@ -749,7 +733,7 @@ nsFindComponent::~nsFindComponent()
}
NS_IMETHODIMP
nsFindComponent::CreateContext( nsIWebShell *aWebShell, nsIEditor* aEditor,
nsFindComponent::CreateContext( nsIDOMWindow *aWindow, nsIEditorShell* aEditorShell,
nsISupports **aResult )
{
@ -764,8 +748,8 @@ nsFindComponent::CreateContext( nsIWebShell *aWebShell, nsIEditor* aEditor,
// Do the expected AddRef on behalf of caller.
newContext->AddRef();
nsresult rv = newContext->Init( aWebShell,
aEditor,
nsresult rv = newContext->Init( aWindow,
aEditorShell,
mLastSearchString,
mLastReplaceString,
mLastCaseSensitive,
@ -864,45 +848,27 @@ nsFindComponent::Find(nsISupports *aContext, PRBool *aDidFind)
}
}
if ( aContext && GetAppShell() )
if (aContext && GetAppShell())
{
nsCOMPtr<nsISearchContext> context = do_QueryInterface( aContext, &rv );
if ( NS_FAILED( rv ) ) {
if (NS_FAILED(rv))
return rv;
}
// Open Find dialog and prompt for search parameters.
char * urlStr = "chrome://global/content/finddialog.xul";
// We need the parent's nsIDOMWindow...
// 1. Get topLevelWindow nsIWebShellContainer (chrome included).
nsCOMPtr<nsIWebShell> ws;
rv = context->GetTargetWebShell( getter_AddRefs( ws ) );
if ( NS_SUCCEEDED( rv ) && ws ) {
nsCOMPtr<nsIWebShellContainer> topLevelWindow;
rv = ws->GetTopLevelWindow(getter_AddRefs(topLevelWindow));
// 3. Convert that to an nsIWebShellWindow.
nsCOMPtr<nsIWebShellWindow> rootWindow(do_QueryInterface(topLevelWindow));
if ( rootWindow ) {
// 4. Convert window to nsIDOMWindow.
nsCOMPtr<nsIDOMWindow> domWindow;
rv = rootWindow->GetDOMWindow(getter_AddRefs( domWindow ) );
if ( NS_SUCCEEDED( rv ) && domWindow ) {
// Whew. Now open dialog with search context as argument.
rv = OpenDialogWithArg( domWindow, context, urlStr );
} else {
DEBUG_PRINTF( PR_STDOUT, "%s %d: Error getting DOM window from web shell, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
}
} else {
DEBUG_PRINTF( PR_STDOUT, "%s %d: QueryInterface (for nsIWebShellWindow) failed, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
}
} else {
DEBUG_PRINTF( PR_STDOUT, "%s %d: GetTargetWebShell failed, rv=0x%08X\n",
__FILE__, (int)__LINE__, (int)rv );
nsCOMPtr<nsIDOMWindow> window;
rv = context->GetTargetWindow( getter_AddRefs( window ) );
if ( NS_SUCCEEDED( rv ) && window )
{
nsCOMPtr<nsIDOMWindow> topWindow;
window->GetTop(getter_AddRefs(topWindow));
if (topWindow)
rv = OpenDialogWithArg(topWindow, context, urlStr );
}
} else {
} else {
rv = NS_ERROR_NULL_POINTER;
}
@ -947,18 +913,17 @@ nsFindComponent::FindNext(nsISupports *aContext, PRBool *aDidFind)
NS_IMETHODIMP
nsFindComponent::ResetContext( nsISupports *aContext,
nsIWebShell *aNewWebShell,
nsIEditor* aEditor )
nsIDOMWindow *aNewWindow,
nsIEditorShell* aEditorShell )
{
nsresult rv = NS_OK;
if ( aContext && aNewWebShell ) {
// Pass on the new document to the context.
Context *context = (Context*)aContext;
context->Reset( aNewWebShell );
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
NS_ENSURE_ARG(aContext);
NS_ENSURE_ARG(aNewWindow);
// Pass on the new document to the context.
Context *context = (Context*)aContext;
context->Reset(aNewWindow);
return NS_OK;
}
// nsFindComponent::Context implementation...

View File

@ -56,31 +56,31 @@ public:
Context();
virtual ~Context();
NS_IMETHOD Init( nsIWebShell *aWebShell,
nsIEditor* aEditor,
NS_IMETHOD Init( nsIDOMWindow *aWindow,
nsIEditorShell* aEditorShell,
const nsString& lastSearchString,
const nsString& lastReplaceString,
PRBool lastCaseSensitive,
PRBool lastSearchBackwards,
PRBool lastWrapSearch);
NS_IMETHOD Reset(nsIWebShell *aNewWebShell);
NS_IMETHOD Reset(nsIDOMWindow *aNewWindow);
NS_IMETHOD DoFind(PRBool *aDidFind);
NS_IMETHOD DoReplace();
// Utility to construct new TS document from our webshell.
NS_IMETHOD MakeTSDocument(nsIWebShell* aWebShell, nsITextServicesDocument** aDoc);
NS_IMETHOD MakeTSDocument(nsIDOMWindow* aWindow, nsITextServicesDocument** aDoc);
NS_IMETHOD GetCurrentBlockIndex(nsITextServicesDocument *aDoc, PRInt32 *outBlockIndex);
NS_IMETHOD SetupDocForSearch(nsITextServicesDocument *aDoc, PRInt32 *outBlockOffset);
nsIWebShell* mTargetWebShell; // weak link. Don't hold a reference
nsIEditor* mEditor; // weak link. Don't hold a reference
nsString mSearchString;
nsString mReplaceString;
PRBool mCaseSensitive;
PRBool mSearchBackwards;
PRBool mWrapSearch;
nsIDOMWindow *mFindDialog;
nsIDOMWindow* mTargetWindow; // weak link. Don't hold a reference
nsIEditorShell* mEditorShell; // weak link. Don't hold a reference
nsString mSearchString;
nsString mReplaceString;
PRBool mCaseSensitive;
PRBool mSearchBackwards;
PRBool mWrapSearch;
nsIDOMWindow* mFindDialog;
}; // nsFindComponent::Context