From e80f9376883dd0fe9b216320d2c1280846c447a6 Mon Sep 17 00:00:00 2001 From: rods Date: Fri, 8 May 1998 15:07:41 +0000 Subject: [PATCH] added Selection methods to nsICodment, nsDocument, nsiSplittableFRame --- content/base/public/nsIDocument.h | 10 +++ content/base/src/nsDocument.cpp | 99 ++++++++++++++++++++++++++- content/base/src/nsDocument.h | 17 +++++ layout/base/public/nsIDocument.h | 10 +++ layout/base/src/nsDocument.cpp | 99 ++++++++++++++++++++++++++- layout/base/src/nsDocument.h | 17 +++++ layout/base/src/nsSplittableFrame.cpp | 9 +++ layout/base/src/nsSplittableFrame.h | 4 ++ 8 files changed, 263 insertions(+), 2 deletions(-) diff --git a/content/base/public/nsIDocument.h b/content/base/public/nsIDocument.h index b955197dc109..fff910f64126 100644 --- a/content/base/public/nsIDocument.h +++ b/content/base/public/nsIDocument.h @@ -154,6 +154,16 @@ public: * Returns the Selection Object */ virtual nsISelection * GetSelection() = 0; + + /** + * Selects all the Content + */ + virtual void SelectAll() = 0; + + /** + * Copies all text from the selection + */ + virtual void GetSelectionText(nsString & aText) = 0; }; // XXX Belongs somewhere else diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index b4147f8c5197..b55c32284a62 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -26,6 +26,8 @@ #include "nsIDocumentObserver.h" #include "nsSelection.h" +#include "nsIDOMText.h" +static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID); static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); @@ -638,8 +640,103 @@ nsresult nsDocument::GetElementsByTagName(nsString &aTagname, nsIDOMNodeIterator * Returns the Selection Object */ nsISelection * nsDocument::GetSelection() { + if (mSelection != nsnull) { + NS_ADDREF(mSelection); + } return mSelection; -} +} + +/** + * Selects all the Content + */ +void nsDocument::SelectAll() { + nsIContent * start = mRootContent; + nsIContent * end = mRootContent; + + // Find Very first Piece of Content + while (start->ChildCount() > 0) { + start = start->ChildAt(0); + } + + // Last piece of Content + PRInt32 count = end->ChildCount(); + while (count > 0) { + end = end->ChildAt(count-1); + count = end->ChildCount(); + } + nsSelectionRange * range = mSelection->GetRange(); + nsSelectionPoint * startPnt = range->GetStartPoint(); + nsSelectionPoint * endPnt = range->GetEndPoint(); + startPnt->SetPoint(start, -1, PR_TRUE); + endPnt->SetPoint(end, -1, PR_FALSE); + +} + +void nsDocument::TraverseTree(nsString & aText, + nsIContent * aContent, + nsIContent * aStart, + nsIContent * aEnd, + PRBool & aInRange) { + + if (aContent == aStart) { + aInRange = PR_TRUE; + } + + PRBool addReturn = PR_FALSE; + if (aInRange) { + nsIDOMText * textContent; + nsresult status = aContent->QueryInterface(kIDOMTextIID, (void**) &textContent); + if (NS_OK == status) { + nsString text; + textContent->GetData(text); + aText.Append(text); + //NS_IF_RELEASE(textContent); + } else { + nsIAtom * atom = aContent->GetTag(); + if (atom != nsnull) { + nsString str; + atom->ToString(str); + //char * s = str.ToNewCString(); + if (str.Equals("P") || + str.Equals("OL") || + str.Equals("LI") || + str.Equals("TD") || + str.Equals("H1") || + str.Equals("H2") || + str.Equals("H3") || + str.Equals("H4") || + str.Equals("H5") || + str.Equals("H6") || + str.Equals("TABLE")) { + addReturn = PR_TRUE; + } + //printf("[%s]\n", s); + //delete s; + } + } + } + + for (PRInt32 i=0;iChildCount();i++) { + TraverseTree(aText, aContent->ChildAt(i), aStart, aEnd, aInRange); + } + if (addReturn) { + aText.Append("\n"); + } + if (aContent == aEnd) { + aInRange = PR_FALSE; + } + +} + +void nsDocument::GetSelectionText(nsString & aText) { + nsSelectionRange * range = mSelection->GetRange(); + nsSelectionPoint * startPnt = range->GetStartPoint(); + nsSelectionPoint * endPnt = range->GetEndPoint(); + + PRBool inRange = PR_FALSE; + TraverseTree(aText, mRootContent, startPnt->GetContent(), endPnt->GetContent(), inRange); +} + NS_LAYOUT nsresult NS_NewSelection(nsISelection** aInstancePtrResult) diff --git a/content/base/src/nsDocument.h b/content/base/src/nsDocument.h index 0f1ec827a22a..4b8cdc4ef375 100644 --- a/content/base/src/nsDocument.h +++ b/content/base/src/nsDocument.h @@ -133,6 +133,23 @@ public: */ virtual nsISelection * GetSelection(); + /** + * Selects all the Content + */ + virtual void SelectAll(); + + /** + * Copies all text from the selection + */ + virtual void GetSelectionText(nsString & aText); + + void TraverseTree(nsString & aText, + nsIContent * aContent, + nsIContent * aStart, + nsIContent * aEnd, + PRBool & aInRange); + + public: virtual nsresult GetScriptObject(JSContext *aContext, void** aScriptObject); diff --git a/layout/base/public/nsIDocument.h b/layout/base/public/nsIDocument.h index b955197dc109..fff910f64126 100644 --- a/layout/base/public/nsIDocument.h +++ b/layout/base/public/nsIDocument.h @@ -154,6 +154,16 @@ public: * Returns the Selection Object */ virtual nsISelection * GetSelection() = 0; + + /** + * Selects all the Content + */ + virtual void SelectAll() = 0; + + /** + * Copies all text from the selection + */ + virtual void GetSelectionText(nsString & aText) = 0; }; // XXX Belongs somewhere else diff --git a/layout/base/src/nsDocument.cpp b/layout/base/src/nsDocument.cpp index b4147f8c5197..b55c32284a62 100644 --- a/layout/base/src/nsDocument.cpp +++ b/layout/base/src/nsDocument.cpp @@ -26,6 +26,8 @@ #include "nsIDocumentObserver.h" #include "nsSelection.h" +#include "nsIDOMText.h" +static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID); static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); @@ -638,8 +640,103 @@ nsresult nsDocument::GetElementsByTagName(nsString &aTagname, nsIDOMNodeIterator * Returns the Selection Object */ nsISelection * nsDocument::GetSelection() { + if (mSelection != nsnull) { + NS_ADDREF(mSelection); + } return mSelection; -} +} + +/** + * Selects all the Content + */ +void nsDocument::SelectAll() { + nsIContent * start = mRootContent; + nsIContent * end = mRootContent; + + // Find Very first Piece of Content + while (start->ChildCount() > 0) { + start = start->ChildAt(0); + } + + // Last piece of Content + PRInt32 count = end->ChildCount(); + while (count > 0) { + end = end->ChildAt(count-1); + count = end->ChildCount(); + } + nsSelectionRange * range = mSelection->GetRange(); + nsSelectionPoint * startPnt = range->GetStartPoint(); + nsSelectionPoint * endPnt = range->GetEndPoint(); + startPnt->SetPoint(start, -1, PR_TRUE); + endPnt->SetPoint(end, -1, PR_FALSE); + +} + +void nsDocument::TraverseTree(nsString & aText, + nsIContent * aContent, + nsIContent * aStart, + nsIContent * aEnd, + PRBool & aInRange) { + + if (aContent == aStart) { + aInRange = PR_TRUE; + } + + PRBool addReturn = PR_FALSE; + if (aInRange) { + nsIDOMText * textContent; + nsresult status = aContent->QueryInterface(kIDOMTextIID, (void**) &textContent); + if (NS_OK == status) { + nsString text; + textContent->GetData(text); + aText.Append(text); + //NS_IF_RELEASE(textContent); + } else { + nsIAtom * atom = aContent->GetTag(); + if (atom != nsnull) { + nsString str; + atom->ToString(str); + //char * s = str.ToNewCString(); + if (str.Equals("P") || + str.Equals("OL") || + str.Equals("LI") || + str.Equals("TD") || + str.Equals("H1") || + str.Equals("H2") || + str.Equals("H3") || + str.Equals("H4") || + str.Equals("H5") || + str.Equals("H6") || + str.Equals("TABLE")) { + addReturn = PR_TRUE; + } + //printf("[%s]\n", s); + //delete s; + } + } + } + + for (PRInt32 i=0;iChildCount();i++) { + TraverseTree(aText, aContent->ChildAt(i), aStart, aEnd, aInRange); + } + if (addReturn) { + aText.Append("\n"); + } + if (aContent == aEnd) { + aInRange = PR_FALSE; + } + +} + +void nsDocument::GetSelectionText(nsString & aText) { + nsSelectionRange * range = mSelection->GetRange(); + nsSelectionPoint * startPnt = range->GetStartPoint(); + nsSelectionPoint * endPnt = range->GetEndPoint(); + + PRBool inRange = PR_FALSE; + TraverseTree(aText, mRootContent, startPnt->GetContent(), endPnt->GetContent(), inRange); +} + NS_LAYOUT nsresult NS_NewSelection(nsISelection** aInstancePtrResult) diff --git a/layout/base/src/nsDocument.h b/layout/base/src/nsDocument.h index 0f1ec827a22a..4b8cdc4ef375 100644 --- a/layout/base/src/nsDocument.h +++ b/layout/base/src/nsDocument.h @@ -133,6 +133,23 @@ public: */ virtual nsISelection * GetSelection(); + /** + * Selects all the Content + */ + virtual void SelectAll(); + + /** + * Copies all text from the selection + */ + virtual void GetSelectionText(nsString & aText); + + void TraverseTree(nsString & aText, + nsIContent * aContent, + nsIContent * aStart, + nsIContent * aEnd, + PRBool & aInRange); + + public: virtual nsresult GetScriptObject(JSContext *aContext, void** aScriptObject); diff --git a/layout/base/src/nsSplittableFrame.cpp b/layout/base/src/nsSplittableFrame.cpp index 7287010fd250..606874551b95 100644 --- a/layout/base/src/nsSplittableFrame.cpp +++ b/layout/base/src/nsSplittableFrame.cpp @@ -186,3 +186,12 @@ NS_METHOD nsSplittableFrame::BreakFromNextFlow() return NS_OK; } +nsIFrame * nsSplittableFrame::GetPrevInFlow() +{ + return mPrevInFlow; +} + +nsIFrame * nsSplittableFrame::GetNextInFlow() +{ + return mNextInFlow; +} diff --git a/layout/base/src/nsSplittableFrame.h b/layout/base/src/nsSplittableFrame.h index 78b504a72c5e..34b092c98b46 100644 --- a/layout/base/src/nsSplittableFrame.h +++ b/layout/base/src/nsSplittableFrame.h @@ -54,6 +54,10 @@ public: NS_IMETHOD BreakFromPrevFlow(); NS_IMETHOD BreakFromNextFlow(); + nsIFrame * GetPrevInFlow(); + nsIFrame * GetNextInFlow(); + + protected: // Constructor. Takes as arguments the content object, the index in parent, // and the Frame for the content parent