New, improved Find that keys off the selection, rather than storing offsets inside the find context.

This commit is contained in:
sfraser%netscape.com 1999-06-04 21:48:47 +00:00
parent 8652111341
commit 44ffed7214
5 changed files with 468 additions and 321 deletions

View File

@ -21,6 +21,7 @@
#include "nsIAppShellComponent.h"
class nsIWebShell;
class nsIEditor;
// a6cf90ee-15b3-11d2-932e-00805f8add32
#define NS_IFINDCOMPONENT_IID \
@ -63,13 +64,16 @@ struct nsIFindComponent : public nsIAppShellComponent {
| FindNext requests that provide the returned search context will find |
| the appropriate search string in aWebShell. |
| |
| The editor that's passed in is only required for replace. If you pass |
| in a read-only webshell, then pass nsnull for the editor. |
| |
| The result is of the xpcom equivalent of an opaque type. It's true type |
| is defined by the implementation of this interface. Clients ought never |
| have to do QueryInterface to convert this to something more elaborate. |
| Clients do have to call Release() when they're no longer interested in |
| this search context. |
--------------------------------------------------------------------------*/
NS_IMETHOD CreateContext( nsIWebShell *aWebShell,
NS_IMETHOD CreateContext( nsIWebShell *aWebShell, nsIEditor* aEditor,
nsISupports **aResult ) = 0;
/*--------------------------------- Find -----------------------------------
@ -83,7 +87,17 @@ struct nsIFindComponent : public nsIAppShellComponent {
| case" and "search backward") is up to the implementation of this |
| component. |
--------------------------------------------------------------------------*/
NS_IMETHOD Find( nsISupports *aContext ) = 0;
NS_IMETHOD Find( nsISupports *aContext, PRBool *aDidFind) = 0;
/*--------------------------------- Replace -----------------------------------
| Replace the currently selected text. It is intended that the string used |
| for replacement is sourced internally to the component (e.g. from a |
| search/replace dialog). |
| |
| Returns an error if the current context does not allow replacement. |
| |
--------------------------------------------------------------------------*/
NS_IMETHOD Replace( nsISupports *aContext ) = 0;
/*------------------------------- FindNext ---------------------------------
| Finds the next occurrence (of the previously searched for string) in |
@ -92,24 +106,17 @@ struct nsIFindComponent : public nsIAppShellComponent {
| If no previous Find has been performed with this context, then the |
| find component will use the last find performed for any context. |
--------------------------------------------------------------------------*/
NS_IMETHOD FindNext( nsISupports *aContext ) = 0;
NS_IMETHOD FindNext( nsISupports *aContext, PRBool *aDidFind) = 0;
/*----------------------------- ResetContext -------------------------------
| Reset the given search context to search a new web shell. Generally, |
| this will be the equivalent of calling Release() on the old context and |
| then creating a new one for aNewWebShell. |
--------------------------------------------------------------------------*/
NS_IMETHOD ResetContext( nsISupports *aContext,
NS_IMETHOD ResetContext( nsISupports *aContext, nsIEditor* aEditor,
nsIWebShell *aNewWebShell ) = 0;
}; // nsIFindComponent
#define NS_DECL_IFINDCOMPONENT \
NS_IMETHOD CreateContext( nsIWebShell *aWebShell, \
nsISupports **aResult ); \
NS_IMETHOD Find( nsISupports *aContext ); \
NS_IMETHOD FindNext( nsISupports *aContext ); \
NS_IMETHOD ResetContext( nsISupports *aContext, \
nsIWebShell *aNewWebShell );
#endif

View File

@ -41,58 +41,9 @@
#include "nsFindComponent.h"
#include "nsFindDialog.h"
#ifdef DEBUG
#define DEBUG_FIND
// ctor
nsFindComponent::nsFindComponent()
: mLastSearchString(),
mLastIgnoreCase( PR_FALSE ),
mLastSearchBackwards( PR_FALSE ),
mLastWrapSearch( PR_FALSE )
{
NS_INIT_REFCNT();
// Initialize "last" stuff from prefs, if we wanted to be really clever...
}
// dtor
nsFindComponent::~nsFindComponent()
{
}
NS_IMETHODIMP
nsFindComponent::CreateContext( nsIWebShell *aWebShell,
nsISupports **aResult )
{
if (!aResult)
return NS_ERROR_NULL_POINTER;
// Construct a new Context with this document.
Context *newContext = new Context();
if (!newContext)
return NS_ERROR_OUT_OF_MEMORY;
// Do the expected AddRef on behalf of caller.
newContext->AddRef();
nsresult rv = newContext->Init( aWebShell,
mLastSearchString,
mLastIgnoreCase,
mLastSearchBackwards,
mLastWrapSearch);
if (NS_FAILED(rv))
{
NS_RELEASE(newContext);
return rv;
}
*aResult = newContext;
return NS_OK;
}
#ifdef XP_MAC
#pragma mark -
#endif
@ -105,25 +56,25 @@ nsFindComponent::Context::Context()
nsFindComponent::Context::~Context()
{
// the nsCOMPtr will do its thing to release the document
}
NS_IMETHODIMP
nsFindComponent::Context::Init( nsIWebShell *aWebShell,
const nsString &lastSearchString,
PRBool lastIgnoreCase,
nsIEditor* aEditor,
const nsString& lastSearchString,
const nsString& lastReplaceString,
PRBool lastCaseSensitive,
PRBool lastSearchBackward,
PRBool lastWrapSearch)
{
if (!aWebShell)
return NS_ERROR_INVALID_ARG;
mWebShell = nsDontQueryInterface<nsIWebShell>( aWebShell );
mLastBlockIndex = 0;
mLastBlockOffset = -1;
mEditor = aEditor; // don't AddRef
mTargetWebShell = aWebShell; // don't AddRef
mSearchString = lastSearchString;
mIgnoreCase = lastIgnoreCase;
mReplaceString = lastReplaceString;
mCaseSensitive = lastCaseSensitive;
mSearchBackwards = lastSearchBackward;
mWrapSearch = lastWrapSearch;
@ -132,105 +83,66 @@ nsFindComponent::Context::Init( nsIWebShell *aWebShell,
static NS_DEFINE_CID(kCTextServicesDocumentCID, NS_TEXTSERVICESDOCUMENT_CID);
static NS_DEFINE_IID(kITextServicesDocumentIID, NS_ITEXTSERVICESDOCUMENT_IID);
nsCOMPtr<nsITextServicesDocument>
nsFindComponent::Context::MakeTSDocument() {
nsCOMPtr<nsITextServicesDocument> result;
NS_IMETHODIMP
nsFindComponent::Context::MakeTSDocument(nsIWebShell* aWebShell, nsITextServicesDocument** aDoc)
{
if (!aWebShell)
return NS_ERROR_INVALID_ARG;
if (!aDoc)
return NS_ERROR_NULL_POINTER;
// Create the text services document.
nsresult rv = nsComponentManager::CreateInstance( kCTextServicesDocumentCID,
*aDoc = NULL;
// Create the text services document.
nsCOMPtr<nsITextServicesDocument> tempDoc;
nsresult rv = nsComponentManager::CreateInstance(kCTextServicesDocumentCID,
nsnull,
kITextServicesDocumentIID,
(void **)getter_AddRefs( result ) );
if ( NS_SUCCEEDED(rv) ) {
nsCOMPtr<nsIDocument> document;
nsCOMPtr<nsIPresShell> presShell;
// Get content viewer from the web shell.
nsCOMPtr<nsIContentViewer> contentViewer;
rv = mWebShell ? mWebShell->GetContentViewer(getter_AddRefs(contentViewer))
: NS_ERROR_NULL_POINTER;
if ( contentViewer ) {
// Up-cast to a document viewer.
nsCOMPtr<nsIDocumentViewer> docViewer(do_QueryInterface(contentViewer));
if ( docViewer ) {
// Get the document and pres shell from the doc viewer.
rv = docViewer->GetDocument(*getter_AddRefs(document));
if ( document ) {
rv = docViewer->GetPresShell(*getter_AddRefs(presShell));
}
}
}
if ( document && presShell ) {
// Compare this document with the one we searched last time.
if ( document.get() == mLastDocument ) {
// Same document, use the block index/offset we have remembered.
} else {
// New document, remember it and reset saved index/offset.
mLastDocument = document;
mLastBlockIndex = 0;
mLastBlockOffset = -1;
}
nsITextServicesDocument::GetIID(),
getter_AddRefs(tempDoc));
if (NS_FAILED(rv) || !tempDoc)
return rv;
// Upcast document to a DOM document.
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(document);
// Get content viewer from the web shell.
nsCOMPtr<nsIContentViewer> contentViewer;
rv = aWebShell->GetContentViewer(getter_AddRefs(contentViewer));
if (NS_FAILED(rv) || !contentViewer)
return rv;
if ( domDoc ) {
// Initialize the text services document.
rv = result->InitWithDocument( domDoc, presShell );
if ( NS_SUCCEEDED( rv ) ) {
// Position to remembered location.
if ( mLastBlockIndex ) {
// Sign indicates whether we're off first vs. last.
if ( mLastBlockIndex > 0 ) {
// Relative to first block.
rv = result->FirstBlock();
for( PRInt32 i = 0; NS_SUCCEEDED(rv) && i < mLastBlockIndex; i++ ) {
rv = result->NextBlock();
}
} else {
// Relative to last block.
rv = result->LastBlock();
for( PRInt32 i = 0; NS_SUCCEEDED(rv) && i > mLastBlockIndex; i-- ) {
rv = result->PrevBlock();
}
}
} else {
// Initialize, based on whether which direction we're searching in.
if (mSearchBackwards)
rv = result->LastBlock();
else
rv = result->FirstBlock();
}
// If something went wrong, reset result.
if ( NS_FAILED( rv ) ) {
result = nsDontQueryInterface<nsITextServicesDocument>( 0 );
}
} else {
// Give up.
result = nsDontQueryInterface<nsITextServicesDocument>( 0 );
}
} else {
// Give up.
result = nsDontQueryInterface<nsITextServicesDocument>( 0 );
}
} else {
// Error, release the result and return null.
result = nsDontQueryInterface<nsITextServicesDocument>( 0 );
}
}
// 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));
// Return the resulting text services document.
return result;
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;
// Initialize the text services document.
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;
}
// ----------------------------------------------------------------
// CharsMatch
//
@ -238,7 +150,7 @@ nsFindComponent::Context::MakeTSDocument() {
// non whitespace and same char.
// ----------------------------------------------------------------
static PRBool CharsMatch(PRUnichar c1, PRUnichar c2)
inline static PRBool CharsMatch(PRUnichar c1, PRUnichar c2)
{
return (nsString::IsSpace(c1) && nsString::IsSpace(c2)) ||
(c1 == c2);
@ -263,6 +175,9 @@ static PRBool CharsMatch(PRUnichar c1, PRUnichar c2)
// If we are ignoring case, the strings have already been lowercased
// at this point.
//
// startOffset is the offset in the search string to start seraching
// at. If -1, it means search from the start (forwards) or end (backwards).
//
// Returns -1 if the string is not found, or if the pattern is an
// empty string, or if startOffset is off the end of the string.
// ----------------------------------------------------------------
@ -278,9 +193,9 @@ static PRInt32 FindInString(const nsString &searchStr, const nsString &patternSt
return -1;
if (startOffset < 0)
startOffset = 0;
startOffset = (searchBackwards) ? searchStrLen : 0;
if (startOffset >= searchStrLen) // bad start offset
if (startOffset > searchStrLen) // bad start offset
return -1;
if (patternLen > searchStrLen) // pattern is longer than string to search
@ -295,7 +210,7 @@ static PRInt32 FindInString(const nsString &searchStr, const nsString &patternSt
if (searchBackwards)
{
// searching backwards
const PRUnichar *s = searchEnd - patternLen - 1;
const PRUnichar *s = searchBuf + startOffset - patternLen - 1;
while (s >= searchBuf)
{
@ -386,116 +301,327 @@ done:
return foundOffset;
}
// utility method to discover which block we're in. The TSDoc interface doesn't give
// us this, because it can't assume a read-only document.
NS_IMETHODIMP
nsFindComponent::Context::GetCurrentBlockIndex(nsITextServicesDocument *aDoc, PRInt32 *outBlockIndex)
{
PRInt32 blockIndex = 0;
PRBool isDone = PR_FALSE;
while (NS_SUCCEEDED(aDoc->IsDone(&isDone)) && !isDone)
{
aDoc->PrevBlock();
blockIndex ++;
}
*outBlockIndex = blockIndex;
return NS_OK;
}
NS_IMETHODIMP
nsFindComponent::Context::SetupDocForSearch(nsITextServicesDocument *aDoc, PRInt32 *outBlockOffset)
{
nsresult rv;
nsITextServicesDocument::TSDBlockSelectionStatus blockStatus;
PRInt32 selOffset;
PRInt32 selLength;
if (!mSearchBackwards) // searching forwards
{
rv = aDoc->LastSelectedBlock(&blockStatus, &selOffset, &selLength);
if (NS_SUCCEEDED(rv) && (blockStatus != nsITextServicesDocument::eBlockNotFound))
{
switch (blockStatus)
{
case nsITextServicesDocument::eBlockOutside: // No TB in S, but found one before/after S.
case nsITextServicesDocument::eBlockPartial: // S begins or ends in TB but extends outside of TB.
// the TS doc points to the block we want.
*outBlockOffset = selOffset + selLength;
break;
case nsITextServicesDocument::eBlockInside: // S extends beyond the start and end of TB.
// we want the block after this one.
rv = aDoc->NextBlock();
*outBlockOffset = 0;
break;
case nsITextServicesDocument::eBlockContains: // TB contains entire S.
*outBlockOffset = selOffset + selLength;
break;
case nsITextServicesDocument::eBlockNotFound: // There is no text block (TB) in or before the selection (S).
default:
NS_NOTREACHED("Shouldn't ever get this status");
}
}
else //failed to get last sel block. Just start at beginning
{
rv = aDoc->FirstBlock();
}
}
else // searching backwards
{
rv = aDoc->FirstSelectedBlock(&blockStatus, &selOffset, &selLength);
if (NS_SUCCEEDED(rv) && (blockStatus != nsITextServicesDocument::eBlockNotFound))
{
switch (blockStatus)
{
case nsITextServicesDocument::eBlockOutside: // No TB in S, but found one before/after S.
case nsITextServicesDocument::eBlockPartial: // S begins or ends in TB but extends outside of TB.
// the TS doc points to the block we want.
*outBlockOffset = selOffset;
break;
case nsITextServicesDocument::eBlockInside: // S extends beyond the start and end of TB.
// we want the block before this one.
rv = aDoc->PrevBlock();
*outBlockOffset = -1;
break;
case nsITextServicesDocument::eBlockContains: // TB contains entire S.
*outBlockOffset = selOffset;
break;
case nsITextServicesDocument::eBlockNotFound: // There is no text block (TB) in or before the selection (S).
default:
NS_NOTREACHED("Shouldn't ever get this status");
}
}
else
{
rv = aDoc->LastBlock();
}
}
return rv;
}
NS_IMETHODIMP
nsFindComponent::Context::DoFind()
nsFindComponent::Context::DoFind(PRBool *aDidFind)
{
if (!mWebShell)
#ifdef DEBUG_FIND
printf("Doing find\n");
#endif
if (!aDidFind)
return NS_ERROR_NULL_POINTER;
if (!mTargetWebShell)
return NS_ERROR_NOT_INITIALIZED;
*aDidFind = PR_FALSE;
nsAutoString matchString = mSearchString;
if (mIgnoreCase)
if (!mCaseSensitive)
matchString.ToLowerCase();
nsresult rv = NS_OK;
nsString str;
nsresult rv = NS_OK;
// Construct a text services document to use.
nsCOMPtr<nsITextServicesDocument> txtDoc = MakeTSDocument();
// 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));
if (NS_FAILED(rv) || !txtDoc)
return rv;
// Set up the TSDoc. We are going to start searching thus:
//
// Searching forwards:
// Look forward from the end of the selection
// Searching backwards:
// Look backwards from the start of the selection
//
PRInt32 selOffset = 0;
rv = SetupDocForSearch(txtDoc, &selOffset);
if (NS_FAILED(rv))
return rv;
// find out where we started
PRInt32 blockIndex;
rv = GetCurrentBlockIndex(txtDoc, &blockIndex);
if (NS_FAILED(rv))
return rv;
PRBool done = ( txtDoc == 0 );
// remember where we started
PRInt32 startingBlockIndex = blockIndex;
// and set the starting position again (hopefully, in future we won't have to do this)
rv = SetupDocForSearch(txtDoc, &selOffset);
if (NS_FAILED(rv))
return rv;
PRBool wrappedOnce = PR_FALSE; // Remember whether we've already wrapped
PRBool done = PR_FALSE;
// Loop till we find a match or fail.
while ( !done ) {
// Remember whether we've reset to beginning/end.
PRBool reset = PR_FALSE;
// Look for next match.
PRBool atEnd = PR_FALSE;
while ( NS_SUCCEEDED( txtDoc->IsDone( &atEnd ) ) && !atEnd ) {
rv = txtDoc->GetCurrentTextBlock(&str);
if (NS_FAILED(rv))
return rv;
if (mIgnoreCase)
str.ToLowerCase();
PRInt32 foundOffset = FindInString(str, matchString, (mLastBlockOffset == -1) ? 0 : mLastBlockOffset + 1, mSearchBackwards);
mLastBlockOffset = -1;
if (foundOffset != -1) {
// Match found. Select it, remember where it was, and quit.
txtDoc->SetSelection(foundOffset, mSearchString.Length());
mLastBlockOffset = foundOffset;
// Loop till we find a match or fail.
while ( !done )
{
PRBool atExtremum = PR_FALSE; // are we at the end (or start)
while ( NS_SUCCEEDED(txtDoc->IsDone(&atExtremum)) && !atExtremum )
{
nsString str;
rv = txtDoc->GetCurrentTextBlock(&str);
if (NS_FAILED(rv))
return rv;
if (!mCaseSensitive)
str.ToLowerCase();
PRInt32 foundOffset = FindInString(str, matchString, selOffset, mSearchBackwards);
selOffset = -1; // reset for next block
if (foundOffset != -1)
{
// Match found. Select it, remember where it was, and quit.
txtDoc->SetSelection(foundOffset, mSearchString.Length());
done = PR_TRUE;
*aDidFind = PR_TRUE;
break;
}
else
{
// have we already been around once?
if (wrappedOnce && (blockIndex == startingBlockIndex))
{
done = PR_TRUE;
break;
} else {
// No match found in this block, try the next (or previous) one.
if (mSearchBackwards) {
txtDoc->PrevBlock();
mLastBlockIndex--;
} else {
txtDoc->NextBlock();
mLastBlockIndex++;
}
}
}
// At end (or matched). Decide which it was...
if ( !done ) {
// Hit end without a match. If we haven't passed this way already,
// then reset to the first/last block (depending on search direction).
if ( !reset ) {
// Reset now.
reset = PR_TRUE;
// If not wrapping, give up.
if ( !mWrapSearch ) {
done = PR_TRUE;
} else {
if ( mSearchBackwards ) {
// Reset to last block.
rv = txtDoc->LastBlock();
} else {
// Reset to first block.
rv = txtDoc->FirstBlock();
}
// Reset last block index.
mLastBlockIndex = 0;
// Reset offset in block.
mLastBlockOffset = -1;
}
} else {
// already reset. This means no matches were found.
done = PR_TRUE;
}
}
}
}
// No match found in this block, try the next (or previous) one.
if (mSearchBackwards) {
txtDoc->PrevBlock();
blockIndex--;
} else {
txtDoc->NextBlock();
blockIndex++;
}
}
} // while !atExtremum
// At end (or matched). Decide which it was...
if (!done)
{
// Hit end without a match. If we haven't passed this way already,
// then reset to the first/last block (depending on search direction).
if (!wrappedOnce)
{
// Reset now.
wrappedOnce = PR_TRUE;
// If not wrapping, give up.
if ( !mWrapSearch ) {
done = PR_TRUE;
}
else
{
if ( mSearchBackwards ) {
// Reset to last block.
rv = txtDoc->LastBlock();
// ugh
rv = GetCurrentBlockIndex(txtDoc, &blockIndex);
rv = txtDoc->LastBlock();
} else {
// Reset to first block.
rv = txtDoc->FirstBlock();
blockIndex = 0;
}
}
} else
{
// already wrapped. This means no matches were found.
done = PR_TRUE;
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsFindComponent::Context::DoReplace()
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsFindComponent::Context::Reset( nsIWebShell *aNewWebShell )
{
if (!aNewWebShell)
return NS_ERROR_INVALID_ARG;
mWebShell = nsDontQueryInterface<nsIWebShell>( aNewWebShell );
mTargetWebShell = aNewWebShell; // don't AddRef
return NS_OK;
}
#ifdef XP_MAC
#pragma mark -
#endif
// ctor
nsFindComponent::nsFindComponent()
: mLastSearchString(),
mLastCaseSensitive( PR_TRUE ),
mLastSearchBackwards( PR_FALSE ),
mLastWrapSearch( PR_FALSE )
{
NS_INIT_REFCNT();
// Initialize "last" stuff from prefs, if we wanted to be really clever...
}
// dtor
nsFindComponent::~nsFindComponent()
{
}
NS_IMETHODIMP
nsFindComponent::CreateContext( nsIWebShell *aWebShell, nsIEditor* aEditor,
nsISupports **aResult )
{
if (!aResult)
return NS_ERROR_NULL_POINTER;
// Construct a new Context with this document.
Context *newContext = new Context();
if (!newContext)
return NS_ERROR_OUT_OF_MEMORY;
// Do the expected AddRef on behalf of caller.
newContext->AddRef();
nsresult rv = newContext->Init( aWebShell,
aEditor,
mLastSearchString,
mLastReplaceString,
mLastCaseSensitive,
mLastSearchBackwards,
mLastWrapSearch);
if (NS_FAILED(rv))
{
NS_RELEASE(newContext);
return rv;
}
*aResult = newContext;
return NS_OK;
}
NS_IMETHODIMP
nsFindComponent::Find( nsISupports *aContext )
nsFindComponent::Find(nsISupports *aContext, PRBool *aDidFind)
{
nsresult rv = NS_OK;
if ( aContext && mAppShell ) {
if ( aContext && mAppShell )
{
Context *context = (Context*)aContext;
// Open Find dialog and prompt for search parameters.
@ -517,8 +643,8 @@ nsFindComponent::Find( nsISupports *aContext )
newWindow,
nsnull,
dialog,
425,
200 );
0,
0 );
if ( NS_SUCCEEDED( rv ) ) {
// Tell the dialog its nsIWebShellWindow.
@ -535,7 +661,21 @@ nsFindComponent::Find( nsISupports *aContext )
}
NS_IMETHODIMP
nsFindComponent::FindNext(nsISupports *aContext)
nsFindComponent::Replace( nsISupports *aContext )
{
nsresult rv = NS_OK;
if (!aContext)
return NS_ERROR_NULL_POINTER;
// For now, just record request to console.
Context *context = (Context*)aContext;
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsFindComponent::FindNext(nsISupports *aContext, PRBool *aDidFind)
{
nsresult rv = NS_OK;
@ -545,16 +685,16 @@ nsFindComponent::FindNext(nsISupports *aContext)
// For now, just record request to console.
Context *context = (Context*)aContext;
#ifdef DEBUG_FIND
printf( "nsFindComponent::FindNext\n\tkey=%s\n\tignoreCase=%ld\tsearchBackward=%ld\n",
printf( "nsFindComponent::FindNext\n\tkey=%s\n\tcaseSensitive=%ld\tsearchBackward=%ld\n",
(const char *)nsAutoCString( context->mSearchString ),
context->mIgnoreCase, context->mSearchBackwards);
context->mCaseSensitive, context->mSearchBackwards);
#endif
context->DoFind();
context->DoFind(aDidFind);
// Record this for out-of-the-blue FindNext calls.
mLastSearchString = context->mSearchString;
mLastIgnoreCase = context->mIgnoreCase;
mLastCaseSensitive = context->mCaseSensitive;
mLastSearchBackwards = context->mSearchBackwards;
mLastWrapSearch = context->mWrapSearch;
@ -562,8 +702,9 @@ nsFindComponent::FindNext(nsISupports *aContext)
}
NS_IMETHODIMP
nsFindComponent::ResetContext( nsISupports *aContext,
nsIWebShell *aNewWebShell ) {
nsFindComponent::ResetContext( nsISupports *aContext, nsIEditor* aEditor,
nsIWebShell *aNewWebShell )
{
nsresult rv = NS_OK;
if ( aContext && aNewWebShell ) {
// Pass on the new document to the context.
@ -575,10 +716,6 @@ nsFindComponent::ResetContext( nsISupports *aContext,
return rv;
}
#ifdef XP_MAC
#pragma mark -
#endif
// nsFindComponent::Context implementation...
NS_IMPL_ISUPPORTS( nsFindComponent::Context, nsISupports::GetIID() )

View File

@ -29,8 +29,8 @@ public:
NS_DEFINE_STATIC_CID_ACCESSOR( NS_FINDCOMPONENT_CID );
// ctor/dtor
nsFindComponent();
virtual ~nsFindComponent();
nsFindComponent();
virtual ~nsFindComponent();
// This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS
@ -39,7 +39,11 @@ public:
NS_DECL_IAPPSHELLCOMPONENT
// This class implements the nsIFindComponent interface functions.
NS_DECL_IFINDCOMPONENT
NS_IMETHOD CreateContext(nsIWebShell *aWebShell, nsIEditor* aEditor, nsISupports **aResult);
NS_IMETHOD Find(nsISupports *aContext, PRBool *aDidFind);
NS_IMETHOD Replace(nsISupports *aContext);
NS_IMETHOD FindNext(nsISupports *aContext, PRBool *aDidFind);
NS_IMETHOD ResetContext(nsISupports *aContext, nsIEditor* aEditor, nsIWebShell *aNewWebShell);
// "Context" for this implementation.
class Context : public nsISupports
@ -50,32 +54,36 @@ public:
Context();
virtual ~Context();
NS_IMETHOD Init( nsIWebShell *aWebShell,
const nsString &lastSearchString,
PRBool lastIgnoreCase,
nsIEditor* aEditor,
const nsString& lastSearchString,
const nsString& lastReplaceString,
PRBool lastCaseSensitive,
PRBool lastSearchBackwards,
PRBool lastWrapSearch);
NS_IMETHOD Reset( nsIWebShell *aNewWebShell );
NS_IMETHOD DoFind();
NS_IMETHOD Reset(nsIWebShell *aNewWebShell);
NS_IMETHOD DoFind(PRBool *aDidFind);
NS_IMETHOD DoReplace();
// Utility to construct new TS document from our webshell.
nsCOMPtr<nsITextServicesDocument> MakeTSDocument();
// Maybe add Find/FindNext functions here?
// Utility to construct new TS document from our webshell.
NS_IMETHOD MakeTSDocument(nsIWebShell* aWebShell, 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;
nsCOMPtr<nsIWebShell> mWebShell;
nsIDocument *mLastDocument; // Document last searched.
nsString mSearchString;
PRBool mIgnoreCase;
PRBool mSearchBackwards;
PRBool mWrapSearch;
PRUint32 mLastBlockOffset; // last offset within the cur block that we found something
PRInt32 mLastBlockIndex; // last block (negative indicates it's relative to last block)
}; // nsFindComponent::Context
protected:
nsString mLastSearchString;
PRBool mLastIgnoreCase;
nsString mLastReplaceString;
PRBool mLastCaseSensitive;
PRBool mLastSearchBackwards;
PRBool mLastWrapSearch;
nsInstanceCounter mInstanceCounter;

View File

@ -37,8 +37,6 @@
#include "nsFindDialog.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
// Standard implementations of addref/release.
NS_IMPL_ADDREF( nsFindDialog );
NS_IMPL_RELEASE( nsFindDialog );
@ -59,7 +57,7 @@ nsFindDialog::QueryInterface( REFNSIID anIID, void **anInstancePtr)
} else if ( anIID.Equals( nsIDocumentObserver::GetIID() ) ) {
*anInstancePtr = (void*) ((nsIDocumentObserver*)this);
NS_ADDREF_THIS();
} else if ( anIID.Equals( kISupportsIID ) ) {
} else if ( anIID.Equals( nsISupports::GetIID() ) ) {
*anInstancePtr = (void*) ((nsISupports*)(nsIDocumentObserver*)this);
NS_ADDREF_THIS();
} else {
@ -76,10 +74,11 @@ nsFindDialog::QueryInterface( REFNSIID anIID, void **anInstancePtr)
// ctor
nsFindDialog::nsFindDialog( nsIFindComponent *aComponent,
nsFindComponent::Context *aContext )
: mComponent( nsDontQueryInterface<nsIFindComponent>( aComponent ) ),
: mComponent( dont_QueryInterface(aComponent) ),
mContext( aContext ),
mWebShell(),
mWindow() {
mDialogWebShell(),
mDialogWindow()
{
// Initialize ref count.
NS_INIT_REFCNT();
mContext->AddRef();
@ -88,7 +87,7 @@ nsFindDialog::nsFindDialog( nsIFindComponent *aComponent,
// This is cribbed from nsBrowserAppCore.cpp also (and should be put somewhere once
// and reused)...
static int APP_DEBUG = 0;
static const int APP_DEBUG = 0;
static nsresult setAttribute( nsIWebShell *shell,
const char *id,
const char *name,
@ -140,27 +139,28 @@ static nsresult setAttribute( nsIWebShell *shell,
// Do startup stuff from C++ side.
NS_IMETHODIMP
nsFindDialog::ConstructBeforeJavaScript(nsIWebShell *aWebShell) {
nsFindDialog::ConstructBeforeJavaScript(nsIWebShell *aWebShell)
{
nsresult rv = NS_OK;
// Save web shell pointer.
mWebShell = nsDontQueryInterface<nsIWebShell>( aWebShell );
mDialogWebShell = dont_QueryInterface(aWebShell);
// Store instance information into dialog's DOM.
if ( mContext ) {
setAttribute( mWebShell,
"data.searchString",
setAttribute( mDialogWebShell,
"data.findKey",
"value",
mContext->mSearchString );
setAttribute( mWebShell,
"data.ignoreCase",
setAttribute( mDialogWebShell,
"data.caseSensitive",
"value",
mContext->mIgnoreCase ? "true" : "false" );
setAttribute( mWebShell,
mContext->mCaseSensitive ? "true" : "false" );
setAttribute( mDialogWebShell,
"data.searchBackward",
"value",
mContext->mSearchBackwards ? "true" : "false" );
setAttribute( mWebShell,
setAttribute( mDialogWebShell,
"data.wrap",
"value",
mContext->mWrapSearch ? "true" : "false" );
@ -168,7 +168,7 @@ nsFindDialog::ConstructBeforeJavaScript(nsIWebShell *aWebShell) {
// Add as observer of the xul document.
nsCOMPtr<nsIContentViewer> cv;
rv = mWebShell->GetContentViewer(getter_AddRefs(cv));
rv = mDialogWebShell->GetContentViewer(getter_AddRefs(cv));
if ( cv ) {
// Up-cast.
nsCOMPtr<nsIDocumentViewer> docv(do_QueryInterface(cv));
@ -188,14 +188,6 @@ nsFindDialog::ConstructBeforeJavaScript(nsIWebShell *aWebShell) {
return rv;
}
// Utility function to close a window given a root nsIWebShell.
static void closeWindow( nsIWebShellWindow *aWebShellWindow ) {
if ( aWebShellWindow ) {
// crashes!
aWebShellWindow->Close();
}
}
// Handle attribute changing; we only care about the element "data.execute"
// which is used to signal command execution from the UI.
NS_IMETHODIMP
@ -205,12 +197,12 @@ nsFindDialog::AttributeChanged( nsIDocument *aDocument,
PRInt32 aHint ) {
nsresult rv = NS_OK;
// Look for data.execute command changing.
nsString id;
nsCOMPtr<nsIAtom> atomId = nsDontQueryInterface<nsIAtom>( NS_NewAtom("id") );
aContent->GetAttribute( kNameSpaceID_None, atomId, id );
if ( id == "data.execute" ) {
nsString idStr;
nsCOMPtr<nsIAtom> atomId = dont_QueryInterface(NS_NewAtom("id"));
aContent->GetAttribute( kNameSpaceID_None, atomId, idStr );
if ( idStr == "data.execute" ) {
nsString cmd;
nsCOMPtr<nsIAtom> atomCommand = nsDontQueryInterface<nsIAtom>( NS_NewAtom("command") );
nsCOMPtr<nsIAtom> atomCommand = dont_QueryInterface(NS_NewAtom("command"));
aContent->GetAttribute( kNameSpaceID_None, atomCommand, cmd );
// Reset command so we detect next request.
aContent->SetAttribute( kNameSpaceID_None, atomCommand, "", PR_FALSE );
@ -231,31 +223,32 @@ nsFindDialog::AttributeChanged( nsIDocument *aDocument,
void
nsFindDialog::OnFind( nsIContent *aContent )
{
if ( mWebShell && mContext )
if ( mDialogWebShell && mContext )
{
nsAutoString valueStr;
// Get arguments and store into the search context.
nsCOMPtr<nsIAtom> atomKey = nsDontQueryInterface<nsIAtom>( NS_NewAtom("key") );
nsCOMPtr<nsIAtom> atomKey = dont_QueryInterface(NS_NewAtom("findKey"));
aContent->GetAttribute( kNameSpaceID_None, atomKey, mContext->mSearchString );
nsCOMPtr<nsIAtom> atomIgnoreCase = nsDontQueryInterface<nsIAtom>( NS_NewAtom("ignoreCase") );
aContent->GetAttribute( kNameSpaceID_None, atomIgnoreCase, valueStr );
mContext->mIgnoreCase = (valueStr == "true");
nsCOMPtr<nsIAtom> atomCaseSensitive = dont_QueryInterface(NS_NewAtom("caseSensitive"));
aContent->GetAttribute( kNameSpaceID_None, atomCaseSensitive, valueStr );
mContext->mCaseSensitive = (valueStr == "true");
nsCOMPtr<nsIAtom> atomSearchBackwards = nsDontQueryInterface<nsIAtom>( NS_NewAtom("searchBackwards") );
nsCOMPtr<nsIAtom> atomSearchBackwards = dont_QueryInterface(NS_NewAtom("searchBackwards"));
aContent->GetAttribute( kNameSpaceID_None, atomSearchBackwards, valueStr );
mContext->mSearchBackwards = (valueStr == "true");
nsCOMPtr<nsIAtom> atomWrapSearch = nsDontQueryInterface<nsIAtom>( NS_NewAtom("wrap") );
nsCOMPtr<nsIAtom> atomWrapSearch = dont_QueryInterface(NS_NewAtom("wrap"));
aContent->GetAttribute( kNameSpaceID_None, atomWrapSearch, valueStr );
mContext->mWrapSearch = (valueStr == "true");
// Search for next occurrence.
if ( mComponent )
{
PRBool foundMatch;
// Find next occurrence in this context.
mComponent->FindNext(mContext);
mComponent->FindNext(mContext, &foundMatch);
}
}
}
@ -264,23 +257,25 @@ nsFindDialog::OnFind( nsIContent *aContent )
void
nsFindDialog::OnNext()
{
if ( mContext && mComponent )
{
// Find next occurrence in this context.
mComponent->FindNext(mContext);
}
if ( mContext && mComponent )
{
PRBool foundMatch;
// Find next occurrence in this context.
mComponent->FindNext(mContext, &foundMatch);
}
}
void
nsFindDialog::OnCancel()
{
// Close the window.
closeWindow( mWindow );
if (mDialogWindow)
mDialogWindow->Close();
}
void
nsFindDialog::SetWindow( nsIWebShellWindow *aWindow )
{
mWindow = nsDontQueryInterface<nsIWebShellWindow>(aWindow);
mDialogWindow = nsDontQueryInterface<nsIWebShellWindow>(aWindow);
}

View File

@ -101,7 +101,7 @@ public:
protected:
nsCOMPtr<nsIFindComponent> mComponent;
nsFindComponent::Context *mContext;
nsCOMPtr<nsIWebShell> mWebShell;
nsCOMPtr<nsIWebShellWindow> mWindow;
nsCOMPtr<nsIWebShell> mDialogWebShell;
nsCOMPtr<nsIWebShellWindow> mDialogWindow;
}; // nsFindDialog