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: #include "nsTableColGroupFrame.h" michael@0: #include "nsTableColFrame.h" michael@0: #include "nsTableFrame.h" michael@0: #include "nsStyleContext.h" michael@0: #include "nsStyleConsts.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsHTMLParts.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCSSRendering.h" michael@0: #include "nsIPresShell.h" michael@0: michael@0: #define COL_GROUP_TYPE_BITS (NS_FRAME_STATE_BIT(30) | \ michael@0: NS_FRAME_STATE_BIT(31)) michael@0: #define COL_GROUP_TYPE_OFFSET 30 michael@0: michael@0: nsTableColGroupType michael@0: nsTableColGroupFrame::GetColType() const michael@0: { michael@0: return (nsTableColGroupType)((mState & COL_GROUP_TYPE_BITS) >> COL_GROUP_TYPE_OFFSET); michael@0: } michael@0: michael@0: void nsTableColGroupFrame::SetColType(nsTableColGroupType aType) michael@0: { michael@0: NS_ASSERTION(GetColType() == eColGroupContent, michael@0: "should only call nsTableColGroupFrame::SetColType with aType " michael@0: "!= eColGroupContent once"); michael@0: uint32_t type = aType - eColGroupContent; michael@0: RemoveStateBits(COL_GROUP_TYPE_BITS); michael@0: AddStateBits(nsFrameState(type << COL_GROUP_TYPE_OFFSET)); michael@0: } michael@0: michael@0: void nsTableColGroupFrame::ResetColIndices(nsIFrame* aFirstColGroup, michael@0: int32_t aFirstColIndex, michael@0: nsIFrame* aStartColFrame) michael@0: { michael@0: nsTableColGroupFrame* colGroupFrame = (nsTableColGroupFrame*)aFirstColGroup; michael@0: int32_t colIndex = aFirstColIndex; michael@0: while (colGroupFrame) { michael@0: if (nsGkAtoms::tableColGroupFrame == colGroupFrame->GetType()) { michael@0: // reset the starting col index for the first cg only if we should reset michael@0: // the whole colgroup (aStartColFrame defaults to nullptr) or if michael@0: // aFirstColIndex is smaller than the existing starting col index michael@0: if ((colIndex != aFirstColIndex) || michael@0: (colIndex < colGroupFrame->GetStartColumnIndex()) || michael@0: !aStartColFrame) { michael@0: colGroupFrame->SetStartColumnIndex(colIndex); michael@0: } michael@0: nsIFrame* colFrame = aStartColFrame; michael@0: if (!colFrame || (colIndex != aFirstColIndex)) { michael@0: colFrame = colGroupFrame->GetFirstPrincipalChild(); michael@0: } michael@0: while (colFrame) { michael@0: if (nsGkAtoms::tableColFrame == colFrame->GetType()) { michael@0: ((nsTableColFrame*)colFrame)->SetColIndex(colIndex); michael@0: colIndex++; michael@0: } michael@0: colFrame = colFrame->GetNextSibling(); michael@0: } michael@0: } michael@0: colGroupFrame = static_cast michael@0: (colGroupFrame->GetNextSibling()); michael@0: } michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: nsTableColGroupFrame::AddColsToTable(int32_t aFirstColIndex, michael@0: bool aResetSubsequentColIndices, michael@0: const nsFrameList::Slice& aCols) michael@0: { michael@0: nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this); michael@0: michael@0: tableFrame->InvalidateFrameSubtree(); michael@0: michael@0: // set the col indices of the col frames and and add col info to the table michael@0: int32_t colIndex = aFirstColIndex; michael@0: nsFrameList::Enumerator e(aCols); michael@0: for (; !e.AtEnd(); e.Next()) { michael@0: ((nsTableColFrame*)e.get())->SetColIndex(colIndex); michael@0: mColCount++; michael@0: tableFrame->InsertCol((nsTableColFrame &)*e.get(), colIndex); michael@0: colIndex++; michael@0: } michael@0: michael@0: for (nsFrameList::Enumerator eTail = e.GetUnlimitedEnumerator(); michael@0: !eTail.AtEnd(); michael@0: eTail.Next()) { michael@0: ((nsTableColFrame*)eTail.get())->SetColIndex(colIndex); michael@0: colIndex++; michael@0: } michael@0: michael@0: // We have already set the colindex for all the colframes in this michael@0: // colgroup that come after the first inserted colframe, but there could michael@0: // be other colgroups following this one and their colframes need michael@0: // correct colindices too. michael@0: if (aResetSubsequentColIndices && GetNextSibling()) { michael@0: ResetColIndices(GetNextSibling(), colIndex); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: nsTableColGroupFrame* michael@0: nsTableColGroupFrame::GetLastRealColGroup(nsTableFrame* aTableFrame) michael@0: { michael@0: nsFrameList colGroups = aTableFrame->GetColGroups(); michael@0: michael@0: nsIFrame* nextToLastColGroup = nullptr; michael@0: nsFrameList::FrameLinkEnumerator link(colGroups); michael@0: for ( ; !link.AtEnd(); link.Next()) { michael@0: nextToLastColGroup = link.PrevFrame(); michael@0: } michael@0: michael@0: if (!link.PrevFrame()) { michael@0: return nullptr; // there are no col group frames michael@0: } michael@0: michael@0: nsTableColGroupType lastColGroupType = michael@0: static_cast(link.PrevFrame())->GetColType(); michael@0: if (eColGroupAnonymousCell == lastColGroupType) { michael@0: return static_cast(nextToLastColGroup); michael@0: } michael@0: michael@0: return static_cast(link.PrevFrame()); michael@0: } michael@0: michael@0: // don't set mColCount here, it is done in AddColsToTable michael@0: nsresult michael@0: nsTableColGroupFrame::SetInitialChildList(ChildListID aListID, michael@0: nsFrameList& aChildList) michael@0: { michael@0: if (!mFrames.IsEmpty()) { michael@0: // We already have child frames which means we've already been michael@0: // initialized michael@0: NS_NOTREACHED("unexpected second call to SetInitialChildList"); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: if (aListID != kPrincipalList) { michael@0: // All we know about is the principal child list. michael@0: NS_NOTREACHED("unknown frame list"); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this); michael@0: if (aChildList.IsEmpty()) { michael@0: tableFrame->AppendAnonymousColFrames(this, GetSpan(), eColAnonymousColGroup, michael@0: false); michael@0: return NS_OK; michael@0: } michael@0: michael@0: mFrames.AppendFrames(this, aChildList); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* virtual */ void michael@0: nsTableColGroupFrame::DidSetStyleContext(nsStyleContext* aOldStyleContext) michael@0: { michael@0: nsContainerFrame::DidSetStyleContext(aOldStyleContext); michael@0: michael@0: if (!aOldStyleContext) //avoid this on init michael@0: return; michael@0: michael@0: nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this); michael@0: if (tableFrame->IsBorderCollapse() && michael@0: tableFrame->BCRecalcNeeded(aOldStyleContext, StyleContext())) { michael@0: int32_t colCount = GetColCount(); michael@0: if (!colCount) michael@0: return; // this is a degenerated colgroup michael@0: nsIntRect damageArea(GetFirstColumn()->GetColIndex(), 0, colCount, michael@0: tableFrame->GetRowCount()); michael@0: tableFrame->AddBCDamageArea(damageArea); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsTableColGroupFrame::AppendFrames(ChildListID aListID, michael@0: nsFrameList& aFrameList) michael@0: { michael@0: NS_ASSERTION(aListID == kPrincipalList, "unexpected child list"); michael@0: michael@0: nsTableColFrame* col = GetFirstColumn(); michael@0: nsTableColFrame* nextCol; michael@0: while (col && col->GetColType() == eColAnonymousColGroup) { michael@0: // this colgroup spans one or more columns but now that there is a michael@0: // real column below, spanned anonymous columns should be removed, michael@0: // since the HTML spec says to ignore the span of a colgroup if it michael@0: // has content columns in it. michael@0: nextCol = col->GetNextCol(); michael@0: RemoveFrame(kPrincipalList, col); michael@0: col = nextCol; michael@0: } michael@0: michael@0: const nsFrameList::Slice& newFrames = michael@0: mFrames.AppendFrames(this, aFrameList); michael@0: InsertColsReflow(GetStartColumnIndex() + mColCount, newFrames); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsTableColGroupFrame::InsertFrames(ChildListID aListID, michael@0: nsIFrame* aPrevFrame, michael@0: nsFrameList& aFrameList) michael@0: { michael@0: NS_ASSERTION(aListID == kPrincipalList, "unexpected child list"); michael@0: NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this, michael@0: "inserting after sibling frame with different parent"); michael@0: michael@0: nsTableColFrame* col = GetFirstColumn(); michael@0: nsTableColFrame* nextCol; michael@0: while (col && col->GetColType() == eColAnonymousColGroup) { michael@0: // this colgroup spans one or more columns but now that there is a michael@0: // real column below, spanned anonymous columns should be removed, michael@0: // since the HTML spec says to ignore the span of a colgroup if it michael@0: // has content columns in it. michael@0: nextCol = col->GetNextCol(); michael@0: if (col == aPrevFrame) { michael@0: // This can happen when we're being appended to michael@0: NS_ASSERTION(!nextCol || nextCol->GetColType() != eColAnonymousColGroup, michael@0: "Inserting in the middle of our anonymous cols?"); michael@0: // We'll want to insert at the beginning michael@0: aPrevFrame = nullptr; michael@0: } michael@0: RemoveFrame(kPrincipalList, col); michael@0: col = nextCol; michael@0: } michael@0: michael@0: NS_ASSERTION(!aPrevFrame || aPrevFrame == aPrevFrame->LastContinuation(), michael@0: "Prev frame should be last in continuation chain"); michael@0: NS_ASSERTION(!aPrevFrame || !GetNextColumn(aPrevFrame) || michael@0: GetNextColumn(aPrevFrame)->GetColType() != eColAnonymousCol, michael@0: "Shouldn't be inserting before a spanned colframe"); michael@0: michael@0: const nsFrameList::Slice& newFrames = michael@0: mFrames.InsertFrames(this, aPrevFrame, aFrameList); michael@0: nsIFrame* prevFrame = nsTableFrame::GetFrameAtOrBefore(this, aPrevFrame, michael@0: nsGkAtoms::tableColFrame); michael@0: michael@0: int32_t colIndex = (prevFrame) ? ((nsTableColFrame*)prevFrame)->GetColIndex() + 1 : GetStartColumnIndex(); michael@0: InsertColsReflow(colIndex, newFrames); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsTableColGroupFrame::InsertColsReflow(int32_t aColIndex, michael@0: const nsFrameList::Slice& aCols) michael@0: { michael@0: AddColsToTable(aColIndex, true, aCols); michael@0: michael@0: PresContext()->PresShell()->FrameNeedsReflow(this, michael@0: nsIPresShell::eTreeChange, michael@0: NS_FRAME_HAS_DIRTY_CHILDREN); michael@0: } michael@0: michael@0: void michael@0: nsTableColGroupFrame::RemoveChild(nsTableColFrame& aChild, michael@0: bool aResetSubsequentColIndices) michael@0: { michael@0: int32_t colIndex = 0; michael@0: nsIFrame* nextChild = nullptr; michael@0: if (aResetSubsequentColIndices) { michael@0: colIndex = aChild.GetColIndex(); michael@0: nextChild = aChild.GetNextSibling(); michael@0: } michael@0: mFrames.DestroyFrame(&aChild); michael@0: mColCount--; michael@0: if (aResetSubsequentColIndices) { michael@0: if (nextChild) { // reset inside this and all following colgroups michael@0: ResetColIndices(this, colIndex, nextChild); michael@0: } michael@0: else { michael@0: nsIFrame* nextGroup = GetNextSibling(); michael@0: if (nextGroup) // reset next and all following colgroups michael@0: ResetColIndices(nextGroup, colIndex); michael@0: } michael@0: } michael@0: michael@0: PresContext()->PresShell()->FrameNeedsReflow(this, michael@0: nsIPresShell::eTreeChange, michael@0: NS_FRAME_HAS_DIRTY_CHILDREN); michael@0: } michael@0: michael@0: nsresult michael@0: nsTableColGroupFrame::RemoveFrame(ChildListID aListID, michael@0: nsIFrame* aOldFrame) michael@0: { michael@0: NS_ASSERTION(aListID == kPrincipalList, "unexpected child list"); michael@0: michael@0: if (!aOldFrame) return NS_OK; michael@0: bool contentRemoval = false; michael@0: michael@0: if (nsGkAtoms::tableColFrame == aOldFrame->GetType()) { michael@0: nsTableColFrame* colFrame = (nsTableColFrame*)aOldFrame; michael@0: if (colFrame->GetColType() == eColContent) { michael@0: contentRemoval = true; michael@0: // Remove any anonymous column frames this produced via a colspan michael@0: nsTableColFrame* col = colFrame->GetNextCol(); michael@0: nsTableColFrame* nextCol; michael@0: while (col && col->GetColType() == eColAnonymousCol) { michael@0: #ifdef DEBUG michael@0: nsIFrame* providerFrame = colFrame->GetParentStyleContextFrame(); michael@0: if (colFrame->StyleContext()->GetParent() == michael@0: providerFrame->StyleContext()) { michael@0: NS_ASSERTION(col->StyleContext() == colFrame->StyleContext() && michael@0: col->GetContent() == colFrame->GetContent(), michael@0: "How did that happen??"); michael@0: } michael@0: // else colFrame is being removed because of a frame michael@0: // reconstruct on it, and its style context is still the old michael@0: // one, so we can't assert anything about how it compares to michael@0: // col's style context. michael@0: #endif michael@0: nextCol = col->GetNextCol(); michael@0: RemoveFrame(kPrincipalList, col); michael@0: col = nextCol; michael@0: } michael@0: } michael@0: michael@0: int32_t colIndex = colFrame->GetColIndex(); michael@0: // The RemoveChild call handles calling FrameNeedsReflow on us. michael@0: RemoveChild(*colFrame, true); michael@0: michael@0: nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this); michael@0: tableFrame->RemoveCol(this, colIndex, true, true); michael@0: if (mFrames.IsEmpty() && contentRemoval && michael@0: GetColType() == eColGroupContent) { michael@0: tableFrame->AppendAnonymousColFrames(this, GetSpan(), michael@0: eColAnonymousColGroup, true); michael@0: } michael@0: } michael@0: else { michael@0: mFrames.DestroyFrame(aOldFrame); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: int michael@0: nsTableColGroupFrame::GetLogicalSkipSides(const nsHTMLReflowState* aReflowState) const michael@0: { michael@0: int skip = 0; michael@0: if (nullptr != GetPrevInFlow()) { michael@0: skip |= 1 << LOGICAL_SIDE_B_START; michael@0: } michael@0: if (nullptr != GetNextInFlow()) { michael@0: skip |= 1 << LOGICAL_SIDE_B_END; michael@0: } michael@0: return skip; michael@0: } michael@0: michael@0: nsresult nsTableColGroupFrame::Reflow(nsPresContext* aPresContext, michael@0: nsHTMLReflowMetrics& aDesiredSize, michael@0: const nsHTMLReflowState& aReflowState, michael@0: nsReflowStatus& aStatus) michael@0: { michael@0: DO_GLOBAL_REFLOW_COUNT("nsTableColGroupFrame"); michael@0: DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus); michael@0: NS_ASSERTION(nullptr!=mContent, "bad state -- null content for frame"); michael@0: nsresult rv=NS_OK; michael@0: michael@0: const nsStyleVisibility* groupVis = StyleVisibility(); michael@0: bool collapseGroup = (NS_STYLE_VISIBILITY_COLLAPSE == groupVis->mVisible); michael@0: if (collapseGroup) { michael@0: nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this); michael@0: tableFrame->SetNeedToCollapse(true); michael@0: } michael@0: // for every content child that (is a column thingy and does not already have a frame) michael@0: // create a frame and adjust it's style michael@0: michael@0: for (nsIFrame *kidFrame = mFrames.FirstChild(); kidFrame; michael@0: kidFrame = kidFrame->GetNextSibling()) { michael@0: // Give the child frame a chance to reflow, even though we know it'll have 0 size michael@0: nsHTMLReflowMetrics kidSize(aReflowState); michael@0: nsHTMLReflowState kidReflowState(aPresContext, aReflowState, kidFrame, michael@0: nsSize(0,0)); michael@0: michael@0: nsReflowStatus status; michael@0: ReflowChild(kidFrame, aPresContext, kidSize, kidReflowState, 0, 0, 0, status); michael@0: FinishReflowChild(kidFrame, aPresContext, kidSize, nullptr, 0, 0, 0); michael@0: } michael@0: michael@0: aDesiredSize.Width() = 0; michael@0: aDesiredSize.Height() = 0; michael@0: aStatus = NS_FRAME_COMPLETE; michael@0: NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize); michael@0: return rv; michael@0: } michael@0: michael@0: nsTableColFrame * nsTableColGroupFrame::GetFirstColumn() michael@0: { michael@0: return GetNextColumn(nullptr); michael@0: } michael@0: michael@0: nsTableColFrame * nsTableColGroupFrame::GetNextColumn(nsIFrame *aChildFrame) michael@0: { michael@0: nsTableColFrame *result = nullptr; michael@0: nsIFrame *childFrame = aChildFrame; michael@0: if (!childFrame) { michael@0: childFrame = mFrames.FirstChild(); michael@0: } michael@0: else { michael@0: childFrame = childFrame->GetNextSibling(); michael@0: } michael@0: while (childFrame) michael@0: { michael@0: if (NS_STYLE_DISPLAY_TABLE_COLUMN == michael@0: childFrame->StyleDisplay()->mDisplay) michael@0: { michael@0: result = (nsTableColFrame *)childFrame; michael@0: break; michael@0: } michael@0: childFrame = childFrame->GetNextSibling(); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: int32_t nsTableColGroupFrame::GetSpan() michael@0: { michael@0: return StyleTable()->mSpan; michael@0: } michael@0: michael@0: void nsTableColGroupFrame::SetContinuousBCBorderWidth(uint8_t aForSide, michael@0: BCPixelSize aPixelValue) michael@0: { michael@0: switch (aForSide) { michael@0: case NS_SIDE_TOP: michael@0: mTopContBorderWidth = aPixelValue; michael@0: return; michael@0: case NS_SIDE_BOTTOM: michael@0: mBottomContBorderWidth = aPixelValue; michael@0: return; michael@0: default: michael@0: NS_ERROR("invalid side arg"); michael@0: } michael@0: } michael@0: michael@0: void nsTableColGroupFrame::GetContinuousBCBorderWidth(nsMargin& aBorder) michael@0: { michael@0: int32_t aPixelsToTwips = nsPresContext::AppUnitsPerCSSPixel(); michael@0: nsTableFrame* table = nsTableFrame::GetTableFrame(this); michael@0: nsTableColFrame* col = table->GetColFrame(mStartColIndex + mColCount - 1); michael@0: col->GetContinuousBCBorderWidth(aBorder); michael@0: aBorder.top = BC_BORDER_BOTTOM_HALF_COORD(aPixelsToTwips, michael@0: mTopContBorderWidth); michael@0: aBorder.bottom = BC_BORDER_TOP_HALF_COORD(aPixelsToTwips, michael@0: mBottomContBorderWidth); michael@0: } michael@0: michael@0: /* ----- global methods ----- */ michael@0: michael@0: nsIFrame* michael@0: NS_NewTableColGroupFrame(nsIPresShell* aPresShell, nsStyleContext* aContext) michael@0: { michael@0: return new (aPresShell) nsTableColGroupFrame(aContext); michael@0: } michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsTableColGroupFrame) michael@0: michael@0: nsIAtom* michael@0: nsTableColGroupFrame::GetType() const michael@0: { michael@0: return nsGkAtoms::tableColGroupFrame; michael@0: } michael@0: michael@0: void michael@0: nsTableColGroupFrame::InvalidateFrame(uint32_t aDisplayItemKey) michael@0: { michael@0: nsIFrame::InvalidateFrame(aDisplayItemKey); michael@0: GetParent()->InvalidateFrameWithRect(GetVisualOverflowRect() + GetPosition(), aDisplayItemKey); michael@0: } michael@0: michael@0: void michael@0: nsTableColGroupFrame::InvalidateFrameWithRect(const nsRect& aRect, uint32_t aDisplayItemKey) michael@0: { michael@0: nsIFrame::InvalidateFrameWithRect(aRect, aDisplayItemKey); michael@0: // If we have filters applied that would affects our bounds, then michael@0: // we get an inactive layer created and this is computed michael@0: // within FrameLayerBuilder michael@0: GetParent()->InvalidateFrameWithRect(aRect + GetPosition(), aDisplayItemKey); michael@0: } michael@0: michael@0: #ifdef DEBUG_FRAME_DUMP michael@0: nsresult michael@0: nsTableColGroupFrame::GetFrameName(nsAString& aResult) const michael@0: { michael@0: return MakeFrameName(NS_LITERAL_STRING("TableColGroup"), aResult); michael@0: } michael@0: michael@0: void nsTableColGroupFrame::Dump(int32_t aIndent) michael@0: { michael@0: char* indent = new char[aIndent + 1]; michael@0: if (!indent) return; michael@0: for (int32_t i = 0; i < aIndent + 1; i++) { michael@0: indent[i] = ' '; michael@0: } michael@0: indent[aIndent] = 0; michael@0: michael@0: printf("%s**START COLGROUP DUMP**\n%s startcolIndex=%d colcount=%d span=%d coltype=", michael@0: indent, indent, GetStartColumnIndex(), GetColCount(), GetSpan()); michael@0: nsTableColGroupType colType = GetColType(); michael@0: switch (colType) { michael@0: case eColGroupContent: michael@0: printf(" content "); michael@0: break; michael@0: case eColGroupAnonymousCol: michael@0: printf(" anonymous-column "); michael@0: break; michael@0: case eColGroupAnonymousCell: michael@0: printf(" anonymous-cell "); michael@0: break; michael@0: } michael@0: // verify the colindices michael@0: int32_t j = GetStartColumnIndex(); michael@0: nsTableColFrame* col = GetFirstColumn(); michael@0: while (col) { michael@0: NS_ASSERTION(j == col->GetColIndex(), "wrong colindex on col frame"); michael@0: col = col->GetNextCol(); michael@0: j++; michael@0: } michael@0: NS_ASSERTION((j - GetStartColumnIndex()) == GetColCount(), michael@0: "number of cols out of sync"); michael@0: printf("\n%s**END COLGROUP DUMP** ", indent); michael@0: delete [] indent; michael@0: } michael@0: #endif