Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
nsWebBrowserFind Class Reference

#include <nsWebBrowserFind.h>

Inheritance diagram for nsWebBrowserFind:
Inheritance graph
[legend]
Collaboration diagram for nsWebBrowserFind:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsWebBrowserFind ()
virtual ~nsWebBrowserFind ()
boolean findNext ()
 findNext

Public Attributes

attribute wstring searchString
 searchString
attribute boolean findBackwards
 findBackwards
attribute boolean wrapFind
 wrapFind
attribute boolean entireWord
 entireWord
attribute boolean matchCase
 matchCase
attribute boolean searchFrames
 searchFrames
attribute nsIDOMWindow currentSearchFrame
 currentSearchFrame
attribute nsIDOMWindow rootSearchFrame
 rootSearchFrame
attribute boolean searchSubframes
 searchSubframes
attribute boolean searchParentFrames
 searchParentFrames

Protected Member Functions

PRBool CanFindNext ()
nsresult SearchInFrame (nsIDOMWindow *aWindow, PRBool aWrapping, PRBool *didFind)
nsresult OnStartSearchFrame (nsIDOMWindow *aWindow)
nsresult OnEndSearchFrame (nsIDOMWindow *aWindow)
void GetFrameSelection (nsIDOMWindow *aWindow, nsISelection **aSel)
nsresult ClearFrameSelection (nsIDOMWindow *aWindow)
nsresult OnFind (nsIDOMWindow *aFoundWindow)
nsIDocShellGetDocShellFromWindow (nsIDOMWindow *inWindow)
void SetSelectionAndScroll (nsIDOMWindow *aWindow, nsIDOMRange *aRange)
nsresult GetRootNode (nsIDOMDocument *aDomDoc, nsIDOMNode **aNode)
nsresult GetSearchLimits (nsIDOMRange *aRange, nsIDOMRange *aStartPt, nsIDOMRange *aEndPt, nsIDOMDocument *aDoc, nsISelection *aSel, PRBool aWrap)
nsresult SetRangeAroundDocument (nsIDOMRange *aSearchRange, nsIDOMRange *aStartPoint, nsIDOMRange *aEndPoint, nsIDOMDocument *aDoc)

Protected Attributes

nsString mSearchString
PRPackedBool mFindBackwards
PRPackedBool mWrapFind
PRPackedBool mEntireWord
PRPackedBool mMatchCase
PRPackedBool mSearchSubFrames
PRPackedBool mSearchParentFrames
nsWeakPtr mCurrentSearchFrame
nsWeakPtr mRootSearchFrame
nsWeakPtr mLastFocusedWindow
nsCOMPtr< nsIFindmFind

Detailed Description

Definition at line 68 of file nsWebBrowserFind.h.


Constructor & Destructor Documentation

Definition at line 107 of file nsWebBrowserFind.cpp.

{
}

Member Function Documentation

PRBool nsWebBrowserFind::CanFindNext ( ) [inline, protected]

Definition at line 87 of file nsWebBrowserFind.h.

                { return mSearchString.Length() != 0; }

Definition at line 894 of file nsWebBrowserFind.cpp.

{
    NS_ENSURE_ARG(aWindow);
    nsCOMPtr<nsISelection> selection;
    GetFrameSelection(aWindow, getter_AddRefs(selection));
    if (selection)
        selection->RemoveAllRanges();
    
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

findNext

Finds, highlights, and scrolls into view the next occurrence of the search string, using the current search settings. Fails if the search string is empty.

Returns:
Whether an occurrence was found

Definition at line 937 of file nsWebBrowserFind.cpp.

{
  nsCOMPtr<nsIScriptGlobalObject> scriptGO(do_QueryInterface(inWindow));
  if (!scriptGO) return nsnull;

  return scriptGO->GetDocShell();
}

Here is the call graph for this function:

void nsWebBrowserFind::GetFrameSelection ( nsIDOMWindow aWindow,
nsISelection **  aSel 
) [protected]

Definition at line 840 of file nsWebBrowserFind.cpp.

{
  *aSel = nsnull;

  nsCOMPtr<nsIDOMDocument> domDoc;    
  aWindow->GetDocument(getter_AddRefs(domDoc));
  if (!domDoc) return;

  nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));
  nsIPresShell* presShell = doc->GetShellAt(0);
  if (!presShell) return;

  // text input controls have their independent selection controllers
  // that we must use when they have focus.
  nsPresContext *presContext = presShell->GetPresContext();

  nsIFrame *frame = nsnull;
  presContext->EventStateManager()->GetFocusedFrame(&frame);
  if (!frame) {
    nsCOMPtr<nsPIDOMWindow> ourWindow = 
      do_QueryInterface(doc->GetScriptGlobalObject());
    if (ourWindow) {
      nsIFocusController *focusController =
          ourWindow->GetRootFocusController();
      if (focusController) {
        nsCOMPtr<nsIDOMElement> focusedElement;
        focusController->GetFocusedElement(getter_AddRefs(focusedElement));
        if (focusedElement) {
            nsCOMPtr<nsIContent> content(do_QueryInterface(focusedElement));
            presShell->GetPrimaryFrameFor(content, &frame);
        }
      }
    }
  }

  nsCOMPtr<nsISelectionController> selCon;
  if (frame) {
    frame->GetSelectionController(presContext, getter_AddRefs(selCon));
    selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, aSel);
    if (*aSel) {
      PRInt32 count = -1;
      (*aSel)->GetRangeCount(&count);
      if (count > 0) {
        return;
      }
      NS_RELEASE(*aSel);
    }
  }

  selCon = do_QueryInterface(presShell);
  selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, aSel);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsWebBrowserFind::GetRootNode ( nsIDOMDocument aDomDoc,
nsIDOMNode **  aNode 
) [protected]

Definition at line 488 of file nsWebBrowserFind.cpp.

{
  nsresult rv;

  NS_ENSURE_ARG_POINTER(aNode);
  *aNode = 0;

  nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryInterface(aDomDoc);
  if (htmlDoc)
  {
    // For HTML documents, the content root node is the body.
    nsCOMPtr<nsIDOMHTMLElement> bodyElement;
    rv = htmlDoc->GetBody(getter_AddRefs(bodyElement));
    NS_ENSURE_SUCCESS(rv, rv);
    NS_ENSURE_ARG_POINTER(bodyElement);
    return bodyElement->QueryInterface(NS_GET_IID(nsIDOMNode),
                                       (void **)aNode);
  }

  // For non-HTML documents, the content root node will be the doc element.
  nsCOMPtr<nsIDOMElement> docElement;
  rv = aDomDoc->GetDocumentElement(getter_AddRefs(docElement));
  NS_ENSURE_SUCCESS(rv, rv);
  NS_ENSURE_ARG_POINTER(docElement);
  return docElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void **)aNode);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsWebBrowserFind::GetSearchLimits ( nsIDOMRange aRange,
nsIDOMRange aStartPt,
nsIDOMRange aEndPt,
nsIDOMDocument aDoc,
nsISelection aSel,
PRBool  aWrap 
) [protected]

Definition at line 554 of file nsWebBrowserFind.cpp.

{
    NS_ENSURE_ARG_POINTER(aSel);

    // There is a selection.
    PRInt32 count = -1;
    nsresult rv = aSel->GetRangeCount(&count);
    if (count < 1)
        return SetRangeAroundDocument(aSearchRange, aStartPt, aEndPt, aDoc);

    // Need bodyNode, for the start/end of the document
    nsCOMPtr<nsIDOMNode> bodyNode;
    rv = GetRootNode(aDoc, getter_AddRefs(bodyNode));
    nsCOMPtr<nsIContent> bodyContent (do_QueryInterface(bodyNode));
    NS_ENSURE_ARG_POINTER(bodyContent);

    PRUint32 childCount = bodyContent->GetChildCount();

    // There are four possible range endpoints we might use:
    // DocumentStart, SelectionStart, SelectionEnd, DocumentEnd.

    nsCOMPtr<nsIDOMRange> range;
    nsCOMPtr<nsIDOMNode> node;
    PRInt32 offset;

    // Forward, not wrapping: SelEnd to DocEnd
    if (!mFindBackwards && !aWrap)
    {
        // This isn't quite right, since the selection's ranges aren't
        // necessarily in order; but they usually will be.
        aSel->GetRangeAt(count-1, getter_AddRefs(range));
        if (!range) return NS_ERROR_UNEXPECTED;
        range->GetEndContainer(getter_AddRefs(node));
        if (!node) return NS_ERROR_UNEXPECTED;
        range->GetEndOffset(&offset);

        aSearchRange->SetStart(node, offset);
        aSearchRange->SetEnd(bodyNode, childCount);
        aStartPt->SetStart(node, offset);
        aStartPt->SetEnd(node, offset);
        aEndPt->SetStart(bodyNode, childCount);
        aEndPt->SetEnd(bodyNode, childCount);
    }
    // Backward, not wrapping: DocStart to SelStart
    else if (mFindBackwards && !aWrap)
    {
        aSel->GetRangeAt(0, getter_AddRefs(range));
        if (!range) return NS_ERROR_UNEXPECTED;
        range->GetStartContainer(getter_AddRefs(node));
        if (!node) return NS_ERROR_UNEXPECTED;
        range->GetStartOffset(&offset);

        aSearchRange->SetStart(bodyNode, 0);
        aSearchRange->SetEnd(bodyNode, childCount);
        aStartPt->SetStart(node, offset);
        aStartPt->SetEnd(node, offset);
        aEndPt->SetStart(bodyNode, 0);
        aEndPt->SetEnd(bodyNode, 0);
    }
    // Forward, wrapping: DocStart to SelEnd
    else if (!mFindBackwards && aWrap)
    {
        aSel->GetRangeAt(count-1, getter_AddRefs(range));
        if (!range) return NS_ERROR_UNEXPECTED;
        range->GetEndContainer(getter_AddRefs(node));
        if (!node) return NS_ERROR_UNEXPECTED;
        range->GetEndOffset(&offset);

        aSearchRange->SetStart(bodyNode, 0);
        aSearchRange->SetEnd(bodyNode, childCount);
        aStartPt->SetStart(bodyNode, 0);
        aStartPt->SetEnd(bodyNode, 0);
        aEndPt->SetStart(node, offset);
        aEndPt->SetEnd(node, offset);
    }
    // Backward, wrapping: SelStart to DocEnd
    else if (mFindBackwards && aWrap)
    {
        aSel->GetRangeAt(0, getter_AddRefs(range));
        if (!range) return NS_ERROR_UNEXPECTED;
        range->GetStartContainer(getter_AddRefs(node));
        if (!node) return NS_ERROR_UNEXPECTED;
        range->GetStartOffset(&offset);

        aSearchRange->SetStart(bodyNode, 0);
        aSearchRange->SetEnd(bodyNode, childCount);
        aStartPt->SetStart(bodyNode, childCount);
        aStartPt->SetEnd(bodyNode, childCount);
        aEndPt->SetStart(node, offset);
        aEndPt->SetEnd(node, offset);
    }
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 834 of file nsWebBrowserFind.cpp.

{
    return NS_OK;
}
nsresult nsWebBrowserFind::OnFind ( nsIDOMWindow aFoundWindow) [protected]

Definition at line 905 of file nsWebBrowserFind.cpp.

{
    SetCurrentSearchFrame(aFoundWindow);

    // We don't want a selection to appear in two frames simultaneously
    nsCOMPtr<nsIDOMWindow> lastFocusedWindow = do_QueryReferent(mLastFocusedWindow);
    if (lastFocusedWindow && lastFocusedWindow != aFoundWindow)
        ClearFrameSelection(lastFocusedWindow);

    // focus the frame we found in
    nsCOMPtr<nsPIDOMWindow> ourWindow = do_QueryInterface(aFoundWindow);
    nsIFocusController *focusController = nsnull;
    if (ourWindow)
        focusController = ourWindow->GetRootFocusController();
    if (focusController)
    {
        nsCOMPtr<nsIDOMWindowInternal> windowInt = do_QueryInterface(aFoundWindow);
        focusController->SetFocusedWindow(windowInt);
        mLastFocusedWindow = do_GetWeakReference(aFoundWindow);
    }

    return NS_OK;
}

Here is the call graph for this function:

Definition at line 827 of file nsWebBrowserFind.cpp.

{
    return ClearFrameSelection(aWindow);
}

Here is the call graph for this function:

nsresult nsWebBrowserFind::SearchInFrame ( nsIDOMWindow aWindow,
PRBool  aWrapping,
PRBool didFind 
) [protected]

Definition at line 737 of file nsWebBrowserFind.cpp.

{
    NS_ENSURE_ARG(aWindow);
    NS_ENSURE_ARG_POINTER(aDidFind);

    *aDidFind = PR_FALSE;

    nsCOMPtr<nsIDOMDocument> domDoc;    
    nsresult rv = aWindow->GetDocument(getter_AddRefs(domDoc));
    NS_ENSURE_SUCCESS(rv, rv);
    if (!domDoc) return NS_ERROR_FAILURE;

    // Do security check, to ensure that the frame we're searching
    // is from the same origin as the frame from which the Find is
    // being run.

    // get a uri for the window
    nsCOMPtr<nsIDocument> theDoc = do_QueryInterface(domDoc);
    if (!theDoc) return NS_ERROR_FAILURE;

    nsIURI *docURI = theDoc->GetDocumentURI();
    NS_ENSURE_TRUE(docURI, NS_ERROR_FAILURE);

    // Get the security manager and do the same-origin check
    nsCOMPtr<nsIScriptSecurityManager> secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    rv = secMan->CheckSameOrigin(nsnull, docURI);
    if (NS_FAILED(rv)) return rv;

    if (!mFind) {
        mFind = do_CreateInstance(NS_FIND_CONTRACTID, &rv);
        NS_ENSURE_SUCCESS(rv, rv);
    }

    (void) mFind->SetCaseSensitive(mMatchCase);
    (void) mFind->SetFindBackwards(mFindBackwards);

    // XXX Make and set a line breaker here, once that's implemented.
    (void) mFind->SetWordBreaker(0);

    // Now make sure the content (for actual finding) and frame (for
    // selection) models are up to date.
    theDoc->FlushPendingNotifications(Flush_Frames);

    nsCOMPtr<nsISelection> sel;
    GetFrameSelection(aWindow, getter_AddRefs(sel));
    NS_ENSURE_ARG_POINTER(sel);

    nsCOMPtr<nsIDOMRange> searchRange (do_CreateInstance(kRangeCID));
    NS_ENSURE_ARG_POINTER(searchRange);
    nsCOMPtr<nsIDOMRange> startPt (do_CreateInstance(kRangeCID));
    NS_ENSURE_ARG_POINTER(startPt);
    nsCOMPtr<nsIDOMRange> endPt (do_CreateInstance(kRangeCID));
    NS_ENSURE_ARG_POINTER(endPt);

    nsCOMPtr<nsIDOMRange> foundRange;

    // If !aWrapping, search from selection to end
    if (!aWrapping)
        rv = GetSearchLimits(searchRange, startPt, endPt, domDoc, sel,
                             PR_FALSE);

    // If aWrapping, search the part of the starting frame
    // up to the point where we left off.
    else
        rv = GetSearchLimits(searchRange, startPt, endPt, domDoc, sel,
                             PR_TRUE);

    NS_ENSURE_SUCCESS(rv, rv);

    rv =  mFind->Find(mSearchString.get(), searchRange, startPt, endPt,
                      getter_AddRefs(foundRange));

    if (NS_SUCCEEDED(rv) && foundRange)
    {
        *aDidFind = PR_TRUE;
        sel->RemoveAllRanges();
        SetSelectionAndScroll(aWindow, foundRange);
    }

    return rv;
}

Here is the call graph for this function:

nsresult nsWebBrowserFind::SetRangeAroundDocument ( nsIDOMRange aSearchRange,
nsIDOMRange aStartPoint,
nsIDOMRange aEndPoint,
nsIDOMDocument aDoc 
) [protected]

Definition at line 516 of file nsWebBrowserFind.cpp.

{
    nsCOMPtr<nsIDOMNode> bodyNode;
    nsresult rv = GetRootNode(aDoc, getter_AddRefs(bodyNode));
    nsCOMPtr<nsIContent> bodyContent (do_QueryInterface(bodyNode));
    NS_ENSURE_SUCCESS(rv, rv);
    NS_ENSURE_ARG_POINTER(bodyContent);

    PRUint32 childCount = bodyContent->GetChildCount();

    aSearchRange->SetStart(bodyNode, 0);
    aSearchRange->SetEnd(bodyNode, childCount);

    if (mFindBackwards)
    {
        aStartPt->SetStart(bodyNode, childCount);
        aStartPt->SetEnd(bodyNode, childCount);
        aEndPt->SetStart(bodyNode, 0);
        aEndPt->SetEnd(bodyNode, 0);
    }
    else
    {
        aStartPt->SetStart(bodyNode, 0);
        aStartPt->SetEnd(bodyNode, 0);
        aEndPt->SetStart(bodyNode, childCount);
        aEndPt->SetEnd(bodyNode, childCount);
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsWebBrowserFind::SetSelectionAndScroll ( nsIDOMWindow aWindow,
nsIDOMRange aRange 
) [protected]

Definition at line 424 of file nsWebBrowserFind.cpp.

{
  nsCOMPtr<nsIDOMDocument> domDoc;    
  aWindow->GetDocument(getter_AddRefs(domDoc));
  if (!domDoc) return;

  nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));
  nsIPresShell* presShell = doc->GetShellAt(0);
  if (!presShell) return;

  // since the match could be an anonymous textnode inside a
  // <textarea> or text <input>, we need to get the outer frame
  nsIFrame *frame = nsnull;
  nsITextControlFrame *tcFrame = nsnull;
  nsCOMPtr<nsIDOMNode> node;
  aRange->GetStartContainer(getter_AddRefs(node));
  nsCOMPtr<nsIContent> content(do_QueryInterface(node));
  for ( ; content; content = content->GetParent()) {
    if (!IsNativeAnonymous(content)) {
      presShell->GetPrimaryFrameFor(content, &frame);
      if (!frame)
        return;
      CallQueryInterface(frame, &tcFrame);

      break;
    }
  }

  nsCOMPtr<nsISelection> selection;
  nsCOMPtr<nsISelectionController> selCon;
  if (!tcFrame) {
    selCon = do_QueryInterface(presShell);
  }
  else {
    tcFrame->GetSelectionContr(getter_AddRefs(selCon));
  }

  selCon->SetDisplaySelection(nsISelectionController::SELECTION_ON);
  selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
    getter_AddRefs(selection));
  if (selection) {
    selection->RemoveAllRanges();
    selection->AddRange(aRange);

    if (tcFrame) {
      FocusElementButNotDocument(doc, content);
    }
    else {
      nsCOMPtr<nsPresContext> presContext = presShell->GetPresContext();
      PRBool isSelectionWithFocus;
      presContext->EventStateManager()->
        MoveFocusToCaret(PR_TRUE, &isSelectionWithFocus);
    }

    // Scroll if necessary to make the selection visible:
    // Must be the last thing to do - bug 242056
    selCon->ScrollSelectionIntoView
      (nsISelectionController::SELECTION_NORMAL,
       nsISelectionController::SELECTION_FOCUS_REGION, PR_TRUE);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

currentSearchFrame

Frame at which to start the search. Once the search is done, this will be set to be the last frame searched, whether or not a result was found. Has to be equal to or contained within the rootSearchFrame.

Definition at line 151 of file nsIWebBrowserFind.idl.

entireWord

Whether to match entire words only. Default is false.

Definition at line 107 of file nsIWebBrowserFind.idl.

findBackwards

Whether to find backwards (towards the beginning of the document). Default is false (search forward).

Definition at line 90 of file nsIWebBrowserFind.idl.

matchCase

Whether to match case (case sensitive) when searching. Default is false.

Definition at line 114 of file nsIWebBrowserFind.idl.

Definition at line 130 of file nsWebBrowserFind.h.

Definition at line 124 of file nsWebBrowserFind.h.

Definition at line 134 of file nsWebBrowserFind.h.

Definition at line 122 of file nsWebBrowserFind.h.

Definition at line 132 of file nsWebBrowserFind.h.

Definition at line 125 of file nsWebBrowserFind.h.

Definition at line 131 of file nsWebBrowserFind.h.

Definition at line 128 of file nsWebBrowserFind.h.

Definition at line 120 of file nsWebBrowserFind.h.

Definition at line 127 of file nsWebBrowserFind.h.

Definition at line 123 of file nsWebBrowserFind.h.

rootSearchFrame

Frame within which to confine the search (normally the content area frame). Set this to only search a subtree of the frame hierarchy.

Definition at line 159 of file nsIWebBrowserFind.idl.

searchFrames

Whether to search through all frames in the content area. Default is true.

Note that you can control whether the search propagates into child or parent frames explicitly using nsIWebBrowserFindInFrames, but if one, but not both, of searchSubframes and searchParentFrames are set, this returns false.

Definition at line 126 of file nsIWebBrowserFind.idl.

searchParentFrames

Whether to allow the search to propagate out of the currentSearchFrame into its parent frame(s). Search is always confined within the rootSearchFrame. Default is true.

Setting nsIWebBrowserfind.searchFrames to true sets this to true.

Definition at line 179 of file nsIWebBrowserFind.idl.

searchString

The string to search for. This must be non-empty to search.

Definition at line 82 of file nsIWebBrowserFind.idl.

searchSubframes

Whether to recurse down into subframes while searching. Default is true.

Setting nsIWebBrowserfind.searchFrames to true sets this to true.

Definition at line 168 of file nsIWebBrowserFind.idl.

wrapFind

Whether the search wraps around to the start (or end) of the document if no match was found between the current position and the end (or beginning). Works correctly when searching backwards. Default is false.

Definition at line 100 of file nsIWebBrowserFind.idl.


The documentation for this class was generated from the following files: