michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * base class for rendering objects that can be split across lines, michael@0: * columns, or pages michael@0: */ michael@0: michael@0: #include "nsSplittableFrame.h" michael@0: #include "nsContainerFrame.h" michael@0: #include "nsIFrameInlines.h" michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsSplittableFrame) michael@0: michael@0: void michael@0: nsSplittableFrame::Init(nsIContent* aContent, michael@0: nsIFrame* aParent, michael@0: nsIFrame* aPrevInFlow) michael@0: { michael@0: nsFrame::Init(aContent, aParent, aPrevInFlow); michael@0: michael@0: if (aPrevInFlow) { michael@0: // Hook the frame into the flow michael@0: SetPrevInFlow(aPrevInFlow); michael@0: aPrevInFlow->SetNextInFlow(this); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsSplittableFrame::DestroyFrom(nsIFrame* aDestructRoot) michael@0: { michael@0: // Disconnect from the flow list michael@0: if (mPrevContinuation || mNextContinuation) { michael@0: RemoveFromFlow(this); michael@0: } michael@0: michael@0: // Let the base class destroy the frame michael@0: nsFrame::DestroyFrom(aDestructRoot); michael@0: } michael@0: michael@0: nsSplittableType michael@0: nsSplittableFrame::GetSplittableType() const michael@0: { michael@0: return NS_FRAME_SPLITTABLE; michael@0: } michael@0: michael@0: nsIFrame* nsSplittableFrame::GetPrevContinuation() const michael@0: { michael@0: return mPrevContinuation; michael@0: } michael@0: michael@0: void michael@0: nsSplittableFrame::SetPrevContinuation(nsIFrame* aFrame) michael@0: { michael@0: NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a prev continuation with incorrect type!"); michael@0: NS_ASSERTION (!IsInPrevContinuationChain(aFrame, this), "creating a loop in continuation chain!"); michael@0: mPrevContinuation = aFrame; michael@0: RemoveStateBits(NS_FRAME_IS_FLUID_CONTINUATION); michael@0: } michael@0: michael@0: nsIFrame* nsSplittableFrame::GetNextContinuation() const michael@0: { michael@0: return mNextContinuation; michael@0: } michael@0: michael@0: void michael@0: nsSplittableFrame::SetNextContinuation(nsIFrame* aFrame) michael@0: { michael@0: NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a next continuation with incorrect type!"); michael@0: NS_ASSERTION (!IsInNextContinuationChain(aFrame, this), "creating a loop in continuation chain!"); michael@0: mNextContinuation = aFrame; michael@0: if (aFrame) michael@0: aFrame->RemoveStateBits(NS_FRAME_IS_FLUID_CONTINUATION); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsSplittableFrame::FirstContinuation() const michael@0: { michael@0: nsSplittableFrame* firstContinuation = const_cast(this); michael@0: while (firstContinuation->mPrevContinuation) { michael@0: firstContinuation = static_cast(firstContinuation->mPrevContinuation); michael@0: } michael@0: MOZ_ASSERT(firstContinuation, "post-condition failed"); michael@0: return firstContinuation; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsSplittableFrame::LastContinuation() const michael@0: { michael@0: nsSplittableFrame* lastContinuation = const_cast(this); michael@0: while (lastContinuation->mNextContinuation) { michael@0: lastContinuation = static_cast(lastContinuation->mNextContinuation); michael@0: } michael@0: MOZ_ASSERT(lastContinuation, "post-condition failed"); michael@0: return lastContinuation; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: bool nsSplittableFrame::IsInPrevContinuationChain(nsIFrame* aFrame1, nsIFrame* aFrame2) michael@0: { michael@0: int32_t iterations = 0; michael@0: while (aFrame1 && iterations < 10) { michael@0: // Bail out after 10 iterations so we don't bog down debug builds too much michael@0: if (aFrame1 == aFrame2) michael@0: return true; michael@0: aFrame1 = aFrame1->GetPrevContinuation(); michael@0: ++iterations; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool nsSplittableFrame::IsInNextContinuationChain(nsIFrame* aFrame1, nsIFrame* aFrame2) michael@0: { michael@0: int32_t iterations = 0; michael@0: while (aFrame1 && iterations < 10) { michael@0: // Bail out after 10 iterations so we don't bog down debug builds too much michael@0: if (aFrame1 == aFrame2) michael@0: return true; michael@0: aFrame1 = aFrame1->GetNextContinuation(); michael@0: ++iterations; michael@0: } michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: nsIFrame* nsSplittableFrame::GetPrevInFlow() const michael@0: { michael@0: return (GetStateBits() & NS_FRAME_IS_FLUID_CONTINUATION) ? mPrevContinuation : nullptr; michael@0: } michael@0: michael@0: void michael@0: nsSplittableFrame::SetPrevInFlow(nsIFrame* aFrame) michael@0: { michael@0: NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a prev in flow with incorrect type!"); michael@0: NS_ASSERTION (!IsInPrevContinuationChain(aFrame, this), "creating a loop in continuation chain!"); michael@0: mPrevContinuation = aFrame; michael@0: AddStateBits(NS_FRAME_IS_FLUID_CONTINUATION); michael@0: } michael@0: michael@0: nsIFrame* nsSplittableFrame::GetNextInFlow() const michael@0: { michael@0: return mNextContinuation && (mNextContinuation->GetStateBits() & NS_FRAME_IS_FLUID_CONTINUATION) ? michael@0: mNextContinuation : nullptr; michael@0: } michael@0: michael@0: void michael@0: nsSplittableFrame::SetNextInFlow(nsIFrame* aFrame) michael@0: { michael@0: NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a next in flow with incorrect type!"); michael@0: NS_ASSERTION (!IsInNextContinuationChain(aFrame, this), "creating a loop in continuation chain!"); michael@0: mNextContinuation = aFrame; michael@0: if (aFrame) michael@0: aFrame->AddStateBits(NS_FRAME_IS_FLUID_CONTINUATION); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsSplittableFrame::FirstInFlow() const michael@0: { michael@0: nsSplittableFrame* firstInFlow = const_cast(this); michael@0: while (nsIFrame* prev = firstInFlow->GetPrevInFlow()) { michael@0: firstInFlow = static_cast(prev); michael@0: } michael@0: MOZ_ASSERT(firstInFlow, "post-condition failed"); michael@0: return firstInFlow; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsSplittableFrame::LastInFlow() const michael@0: { michael@0: nsSplittableFrame* lastInFlow = const_cast(this); michael@0: while (nsIFrame* next = lastInFlow->GetNextInFlow()) { michael@0: lastInFlow = static_cast(next); michael@0: } michael@0: MOZ_ASSERT(lastInFlow, "post-condition failed"); michael@0: return lastInFlow; michael@0: } michael@0: michael@0: // Remove this frame from the flow. Connects prev in flow and next in flow michael@0: void michael@0: nsSplittableFrame::RemoveFromFlow(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame* prevContinuation = aFrame->GetPrevContinuation(); michael@0: nsIFrame* nextContinuation = aFrame->GetNextContinuation(); michael@0: michael@0: // The new continuation is fluid only if the continuation on both sides michael@0: // of the removed frame was fluid michael@0: if (aFrame->GetPrevInFlow() && aFrame->GetNextInFlow()) { michael@0: if (prevContinuation) { michael@0: prevContinuation->SetNextInFlow(nextContinuation); michael@0: } michael@0: if (nextContinuation) { michael@0: nextContinuation->SetPrevInFlow(prevContinuation); michael@0: } michael@0: } else { michael@0: if (prevContinuation) { michael@0: prevContinuation->SetNextContinuation(nextContinuation); michael@0: } michael@0: if (nextContinuation) { michael@0: nextContinuation->SetPrevContinuation(prevContinuation); michael@0: } michael@0: } michael@0: michael@0: aFrame->SetPrevInFlow(nullptr); michael@0: aFrame->SetNextInFlow(nullptr); michael@0: } michael@0: michael@0: nscoord michael@0: nsSplittableFrame::GetConsumedHeight() const michael@0: { michael@0: nscoord height = 0; michael@0: michael@0: // Reduce the height by the computed height of prev-in-flows. michael@0: for (nsIFrame* prev = GetPrevInFlow(); prev; prev = prev->GetPrevInFlow()) { michael@0: height += prev->GetRect().height; michael@0: } michael@0: michael@0: return height; michael@0: } michael@0: michael@0: nscoord michael@0: nsSplittableFrame::GetEffectiveComputedHeight(const nsHTMLReflowState& aReflowState, michael@0: nscoord aConsumedHeight) const michael@0: { michael@0: nscoord height = aReflowState.ComputedHeight(); michael@0: if (height == NS_INTRINSICSIZE) { michael@0: return NS_INTRINSICSIZE; michael@0: } michael@0: michael@0: if (aConsumedHeight == NS_INTRINSICSIZE) { michael@0: aConsumedHeight = GetConsumedHeight(); michael@0: } michael@0: michael@0: height -= aConsumedHeight; michael@0: michael@0: if (aConsumedHeight != 0 && aConsumedHeight != NS_INTRINSICSIZE) { michael@0: // We just subtracted our top-border padding, since it was included in the michael@0: // first frame's height. Add it back to get the content height. michael@0: height += aReflowState.ComputedPhysicalBorderPadding().top; michael@0: } michael@0: michael@0: // We may have stretched the frame beyond its computed height. Oh well. michael@0: height = std::max(0, height); michael@0: michael@0: return height; michael@0: } michael@0: michael@0: int michael@0: nsSplittableFrame::GetLogicalSkipSides(const nsHTMLReflowState* aReflowState) const michael@0: { michael@0: if (IS_TRUE_OVERFLOW_CONTAINER(this)) { michael@0: return LOGICAL_SIDES_B_BOTH; michael@0: } michael@0: michael@0: int skip = 0; michael@0: michael@0: if (GetPrevInFlow()) { michael@0: skip |= LOGICAL_SIDE_B_START; michael@0: } michael@0: michael@0: if (aReflowState) { michael@0: // We're in the midst of reflow right now, so it's possible that we haven't michael@0: // created a nif yet. If our content height is going to exceed our available michael@0: // height, though, then we're going to need a next-in-flow, it just hasn't michael@0: // been created yet. michael@0: michael@0: if (NS_UNCONSTRAINEDSIZE != aReflowState->AvailableHeight()) { michael@0: nscoord effectiveCH = this->GetEffectiveComputedHeight(*aReflowState); michael@0: if (effectiveCH > aReflowState->AvailableHeight()) { michael@0: // Our content height is going to exceed our available height, so we're michael@0: // going to need a next-in-flow. michael@0: skip |= LOGICAL_SIDE_B_END; michael@0: } michael@0: } michael@0: } else { michael@0: nsIFrame* nif = GetNextInFlow(); michael@0: if (nif && !IS_TRUE_OVERFLOW_CONTAINER(nif)) { michael@0: skip |= LOGICAL_SIDE_B_END; michael@0: } michael@0: } michael@0: michael@0: return skip; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void michael@0: nsSplittableFrame::DumpBaseRegressionData(nsPresContext* aPresContext, FILE* out, int32_t aIndent) michael@0: { michael@0: nsFrame::DumpBaseRegressionData(aPresContext, out, aIndent); michael@0: if (nullptr != mNextContinuation) { michael@0: IndentBy(out, aIndent); michael@0: fprintf(out, "\n", (void*)mNextContinuation); michael@0: } michael@0: if (nullptr != mPrevContinuation) { michael@0: IndentBy(out, aIndent); michael@0: fprintf(out, "\n", (void*)mPrevContinuation); michael@0: } michael@0: michael@0: } michael@0: #endif