From 13ee3e3b3d09e3c16f4ce8504ea50f0219340991 Mon Sep 17 00:00:00 2001 From: "rods%netscape.com" Date: Wed, 3 Nov 1999 00:07:01 +0000 Subject: [PATCH] Add CountAllChild to count all the option and optgroup elements so lists get sized correctly when no rows are specified r=kmcclusk, bug=4050 git-svn-id: svn://10.0.0.236/trunk@52622 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/layout/forms/nsListControlFrame.cpp | 61 +++++++++++++++++-- mozilla/layout/forms/nsListControlFrame.h | 1 + .../html/forms/src/nsListControlFrame.cpp | 61 +++++++++++++++++-- .../html/forms/src/nsListControlFrame.h | 1 + 4 files changed, 116 insertions(+), 8 deletions(-) diff --git a/mozilla/layout/forms/nsListControlFrame.cpp b/mozilla/layout/forms/nsListControlFrame.cpp index 0b2774452e6..f1e72136194 100644 --- a/mozilla/layout/forms/nsListControlFrame.cpp +++ b/mozilla/layout/forms/nsListControlFrame.cpp @@ -178,6 +178,55 @@ nsListControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr) return nsScrollFrame::QueryInterface(aIID, aInstancePtr); } +//--------------------------------------------------------- +nsresult nsListControlFrame::CountAllChild(nsIDOMNode * aNode, PRInt32& aCount) +{ + nsresult status = NS_OK; + nsIDOMNode * child; + aNode->GetFirstChild(&child); + while (child) { + + // note: both optgroup and option elements can have DOM child + // option elements have text nodes as COM child, but they don't have too + nsCOMPtr optGroup; + child->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) &optGroup); + if (optGroup) { + aCount++; + + // check for children + PRBool hasChildren; + status = child->HasChildNodes(&hasChildren); + if (NS_FAILED(status)) { + NS_RELEASE(child); + return status; + } + if (hasChildren) { + status = CountAllChild(child, aCount); + if (NS_FAILED(status)) { + NS_RELEASE(child); + return status; + } + } + } else { + // don't query interface againa if it was an optgroup + nsCOMPtr option; + child->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) &option); + if (option) { + aCount++; + } + } + + nsIDOMNode * tmp = child; + status = child->GetNextSibling(&child); + if (NS_FAILED(status)) { + NS_RELEASE(tmp); + return status; + } + NS_RELEASE(tmp); + } + return status; +} + //--------------------------------------------------------- // Reflow is overriden to constrain the listbox height to the number of rows and columns // specified. @@ -365,11 +414,15 @@ nsListControlFrame::Reflow(nsIPresContext& aPresContext, PRInt32 numRows = 1; GetSizeAttribute(&numRows); if (numRows == kNoSizeSpecified) { - visibleHeight = aReflowState.mComputedHeight; - visibleHeight -= (border.top + border.bottom + padding.top + padding.bottom); - } else { - visibleHeight = numRows * heightOfARow; + nsIDOMNode* node; + nsresult rv = mContent->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) &node); + if (node && NS_SUCCEEDED(rv)) { + numRows = 0; + CountAllChild(node, numRows); + NS_RELEASE(node); + } } + visibleHeight = numRows * heightOfARow; } } diff --git a/mozilla/layout/forms/nsListControlFrame.h b/mozilla/layout/forms/nsListControlFrame.h index 11b977ea088..588f6a46ab9 100644 --- a/mozilla/layout/forms/nsListControlFrame.h +++ b/mozilla/layout/forms/nsListControlFrame.h @@ -160,6 +160,7 @@ public: protected: NS_IMETHOD GetSelectedIndexFromDOM(PRInt32* aIndex); // from DOM NS_IMETHOD IsTargetOptionDisabled(PRBool &aIsDisabled); + nsresult CountAllChild(nsIDOMNode * aNode, PRInt32& aCount); nsListControlFrame(); virtual ~nsListControlFrame(); diff --git a/mozilla/layout/html/forms/src/nsListControlFrame.cpp b/mozilla/layout/html/forms/src/nsListControlFrame.cpp index 0b2774452e6..f1e72136194 100644 --- a/mozilla/layout/html/forms/src/nsListControlFrame.cpp +++ b/mozilla/layout/html/forms/src/nsListControlFrame.cpp @@ -178,6 +178,55 @@ nsListControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr) return nsScrollFrame::QueryInterface(aIID, aInstancePtr); } +//--------------------------------------------------------- +nsresult nsListControlFrame::CountAllChild(nsIDOMNode * aNode, PRInt32& aCount) +{ + nsresult status = NS_OK; + nsIDOMNode * child; + aNode->GetFirstChild(&child); + while (child) { + + // note: both optgroup and option elements can have DOM child + // option elements have text nodes as COM child, but they don't have too + nsCOMPtr optGroup; + child->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) &optGroup); + if (optGroup) { + aCount++; + + // check for children + PRBool hasChildren; + status = child->HasChildNodes(&hasChildren); + if (NS_FAILED(status)) { + NS_RELEASE(child); + return status; + } + if (hasChildren) { + status = CountAllChild(child, aCount); + if (NS_FAILED(status)) { + NS_RELEASE(child); + return status; + } + } + } else { + // don't query interface againa if it was an optgroup + nsCOMPtr option; + child->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) &option); + if (option) { + aCount++; + } + } + + nsIDOMNode * tmp = child; + status = child->GetNextSibling(&child); + if (NS_FAILED(status)) { + NS_RELEASE(tmp); + return status; + } + NS_RELEASE(tmp); + } + return status; +} + //--------------------------------------------------------- // Reflow is overriden to constrain the listbox height to the number of rows and columns // specified. @@ -365,11 +414,15 @@ nsListControlFrame::Reflow(nsIPresContext& aPresContext, PRInt32 numRows = 1; GetSizeAttribute(&numRows); if (numRows == kNoSizeSpecified) { - visibleHeight = aReflowState.mComputedHeight; - visibleHeight -= (border.top + border.bottom + padding.top + padding.bottom); - } else { - visibleHeight = numRows * heightOfARow; + nsIDOMNode* node; + nsresult rv = mContent->QueryInterface(nsCOMTypeInfo::GetIID(),(void**) &node); + if (node && NS_SUCCEEDED(rv)) { + numRows = 0; + CountAllChild(node, numRows); + NS_RELEASE(node); + } } + visibleHeight = numRows * heightOfARow; } } diff --git a/mozilla/layout/html/forms/src/nsListControlFrame.h b/mozilla/layout/html/forms/src/nsListControlFrame.h index 11b977ea088..588f6a46ab9 100644 --- a/mozilla/layout/html/forms/src/nsListControlFrame.h +++ b/mozilla/layout/html/forms/src/nsListControlFrame.h @@ -160,6 +160,7 @@ public: protected: NS_IMETHOD GetSelectedIndexFromDOM(PRInt32* aIndex); // from DOM NS_IMETHOD IsTargetOptionDisabled(PRBool &aIsDisabled); + nsresult CountAllChild(nsIDOMNode * aNode, PRInt32& aCount); nsListControlFrame(); virtual ~nsListControlFrame();