gecko-dev/layout/tables/nsTableOuterFrame.cpp

1197 строки
44 KiB
C++
Исходник Обычный вид История

1998-04-14 00:24:54 +04:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsTableOuterFrame.h"
#include "nsTableFrame.h"
#include "nsBodyFrame.h"
1998-06-09 08:51:44 +04:00
#include "nsIReflowCommand.h"
1998-04-14 00:24:54 +04:00
#include "nsIStyleContext.h"
#include "nsStyleConsts.h"
#include "nsIPresContext.h"
#include "nsIRenderingContext.h"
#include "nsCSSRendering.h"
#include "nsIContent.h"
#include "nsCSSLayout.h"
#include "nsVoidArray.h"
#include "nsIPtr.h"
#include "prinrval.h"
#include "nsHTMLIIDs.h"
1998-04-14 00:24:54 +04:00
#ifdef NS_DEBUG
static PRBool gsDebug = PR_FALSE;
static PRBool gsTiming = PR_FALSE;
static PRBool gsDebugIR = PR_TRUE;
1998-04-14 00:24:54 +04:00
//#define NOISY
//#define NOISY_FLOW
#define NOISY_MARGINS
1998-04-14 00:24:54 +04:00
#else
static const PRBool gsDebug = PR_FALSE;
static const PRBool gsTiming = PR_FALSE;
static const PRBool gsDebugIR = PR_FALSE;
1998-04-14 00:24:54 +04:00
#endif
NS_DEF_PTR(nsIStyleContext);
NS_DEF_PTR(nsIContent);
1998-04-14 00:24:54 +04:00
struct OuterTableReflowState {
// The presentation context
nsIPresContext *pc;
// Our reflow state
const nsHTMLReflowState& reflowState;
1998-04-14 00:24:54 +04:00
// The total available size (computed from the parent)
nsSize availSize;
// The available size for the inner table frame
nsSize innerTableMaxSize;
// Margin tracking information
nscoord prevMaxPosBottomMargin;
nscoord prevMaxNegBottomMargin;
// Flags for whether the max size is unconstrained
PRBool unconstrainedWidth;
PRBool unconstrainedHeight;
// Running y-offset
nscoord y;
OuterTableReflowState(nsIPresContext& aPresContext,
const nsHTMLReflowState& aReflowState)
: reflowState(aReflowState)
1998-04-14 00:24:54 +04:00
{
pc = &aPresContext;
availSize.width = reflowState.maxSize.width;
availSize.height = reflowState.maxSize.height;
1998-04-14 00:24:54 +04:00
prevMaxPosBottomMargin = 0;
prevMaxNegBottomMargin = 0;
y=0; // border/padding/margin???
unconstrainedWidth = PRBool(aReflowState.maxSize.width == NS_UNCONSTRAINEDSIZE);
unconstrainedHeight = PRBool(aReflowState.maxSize.height == NS_UNCONSTRAINEDSIZE);
innerTableMaxSize.width=0;
innerTableMaxSize.height=0;
1998-04-14 00:24:54 +04:00
}
~OuterTableReflowState() {
}
};
/* ----------- nsTableOuterFrame ---------- */
/**
*/
nsTableOuterFrame::nsTableOuterFrame(nsIContent* aContent, nsIFrame* aParentFrame)
: nsContainerFrame(aContent, aParentFrame),
1998-04-14 00:24:54 +04:00
mInnerTableFrame(nsnull),
1998-07-08 08:49:01 +04:00
mCaptionFrame(nsnull),
1998-04-14 00:24:54 +04:00
mMinCaptionWidth(0),
mDesiredSize(nsnull)
1998-04-14 00:24:54 +04:00
{
}
1998-09-16 21:19:20 +04:00
NS_IMETHODIMP nsTableOuterFrame::Init(nsIPresContext& aPresContext, nsIFrame* aChildList)
{
mFirstChild = aChildList;
// Set our internal member data
mInnerTableFrame = mFirstChild;
if (2 == LengthOf(mFirstChild)) {
mFirstChild->GetNextSibling(mCaptionFrame);
1998-09-16 21:19:20 +04:00
}
return NS_OK;
}
NS_METHOD nsTableOuterFrame::Paint(nsIPresContext& aPresContext,
nsIRenderingContext& aRenderingContext,
const nsRect& aDirtyRect)
1998-04-14 00:24:54 +04:00
{
// for debug...
if (nsIFrame::GetShowFrameBorders()) {
aRenderingContext.SetColor(NS_RGB(255,0,0));
aRenderingContext.DrawRect(0, 0, mRect.width, mRect.height);
}
PaintChildren(aPresContext, aRenderingContext, aDirtyRect);
return NS_OK;
1998-04-14 00:24:54 +04:00
}
PRBool nsTableOuterFrame::NeedsReflow(const nsSize& aMaxSize)
{
PRBool result=PR_TRUE;
if (nsnull!=mInnerTableFrame)
result = ((nsTableFrame *)mInnerTableFrame)->NeedsReflow(aMaxSize);
1998-04-14 00:24:54 +04:00
return result;
}
// Recover the reflow state to what it should be if aKidFrame is about
// to be reflowed
nsresult nsTableOuterFrame::RecoverState(OuterTableReflowState& aReflowState,
nsIFrame* aKidFrame)
{
#if 0
// Get aKidFrame's previous sibling
nsIFrame* prevKidFrame = nsnull;
for (nsIFrame* frame = mFirstChild; frame != aKidFrame;) {
prevKidFrame = frame;
frame->GetNextSibling(frame);
}
if (nsnull != prevKidFrame) {
nsRect rect;
// Set our running y-offset
prevKidFrame->GetRect(rect);
aReflowState.y = rect.YMost();
// Adjust the available space
if (PR_FALSE == aReflowState.unconstrainedHeight) {
aReflowState.availSize.height -= aReflowState.y;
}
// Get the previous frame's bottom margin
const nsStyleSpacing* kidSpacing;
prevKidFrame->GetStyleData(eStyleStruct_Spacing, (nsStyleStruct *&)kidSpacing);
nsMargin margin;
kidSpacing->CalcMarginFor(prevKidFrame, margin);
if (margin.bottom < 0) {
aReflowState.prevMaxNegBottomMargin = -margin.bottom;
} else {
aReflowState.prevMaxPosBottomMargin = margin.bottom;
}
}
#endif
aReflowState.y=0;
// Set the inner table max size
nsSize innerTableSize(0,0);
mInnerTableFrame->GetSize(innerTableSize);
aReflowState.innerTableMaxSize.width = innerTableSize.width;
aReflowState.innerTableMaxSize.height = aReflowState.reflowState.maxSize.height;
return NS_OK;
}
nsresult nsTableOuterFrame::AdjustSiblingsAfterReflow(nsIPresContext& aPresContext,
OuterTableReflowState& aReflowState,
nsIFrame* aKidFrame,
nscoord aDeltaY)
{
// XXX: this is now dead code
nsIFrame* lastKidFrame = aKidFrame;
if (aDeltaY != 0) {
// Move the frames that follow aKidFrame by aDeltaY
nsIFrame* kidFrame;
aKidFrame->GetNextSibling(kidFrame);
while (nsnull != kidFrame) {
nsPoint origin;
// XXX We can't just slide the child if it has a next-in-flow
kidFrame->GetOrigin(origin);
origin.y += aDeltaY;
// XXX We need to send move notifications to the frame...
nsIHTMLReflow* htmlReflow;
if (NS_OK == kidFrame->QueryInterface(kIHTMLReflowIID, (void**)&htmlReflow)) {
htmlReflow->WillReflow(aPresContext);
}
kidFrame->MoveTo(origin.x, origin.y);
// Get the next frame
lastKidFrame = kidFrame;
kidFrame->GetNextSibling(kidFrame);
}
} else {
// Get the last frame
lastKidFrame = LastFrame(mFirstChild);
}
// Update our running y-offset to reflect the bottommost child
nsRect rect;
lastKidFrame->GetRect(rect);
aReflowState.y = rect.YMost();
// Get the bottom margin for the last child frame
const nsStyleSpacing* kidSpacing;
lastKidFrame->GetStyleData(eStyleStruct_Spacing, (nsStyleStruct *&)kidSpacing);
nsMargin margin;
kidSpacing->CalcMarginFor(lastKidFrame, margin);
if (margin.bottom < 0) {
aReflowState.prevMaxNegBottomMargin = -margin.bottom;
} else {
aReflowState.prevMaxPosBottomMargin = margin.bottom;
}
return NS_OK;
}
nsresult nsTableOuterFrame::IncrementalReflow(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus)
{
nsresult rv = NS_OK;
// determine if this frame is the target or not
nsIFrame *target=nsnull;
rv = aReflowState.reflowState.reflowCommand->GetTarget(target);
if ((PR_TRUE==NS_SUCCEEDED(rv)) && (nsnull!=target))
{
if (this==target)
rv = IR_TargetIsMe(aPresContext, aDesiredSize, aReflowState, aStatus);
else
{
// Get the next frame in the reflow chain
nsIFrame* nextFrame;
aReflowState.reflowState.reflowCommand->GetNext(nextFrame);
// Recover our reflow state
RecoverState(aReflowState, nextFrame);
rv = IR_TargetIsChild(aPresContext, aDesiredSize, aReflowState, aStatus, nextFrame);
}
}
return rv;
}
nsresult nsTableOuterFrame::IR_TargetIsChild(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus,
nsIFrame * aNextFrame)
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: IR_TargetIsChild\n");
nsresult rv;
if (aNextFrame==mInnerTableFrame)
rv = IR_TargetIsInnerTableFrame(aPresContext, aDesiredSize, aReflowState, aStatus);
else if (aNextFrame==mCaptionFrame)
rv = IR_TargetIsCaptionFrame(aPresContext, aDesiredSize, aReflowState, aStatus);
else
rv = NS_ERROR_ILLEGAL_VALUE;
return rv;
}
nsresult nsTableOuterFrame::IR_TargetIsInnerTableFrame(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus)
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: IR_TargetIsInnerTableFrame\n");
nsresult rv = IR_InnerTableReflow(aPresContext, aDesiredSize, aReflowState, aStatus);
return rv;
}
nsresult nsTableOuterFrame::IR_TargetIsCaptionFrame(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus)
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: IR_TargetIsCaptionFrame\n");
nsresult rv;
return rv;
}
nsresult nsTableOuterFrame::IR_TargetIsMe(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus)
{
nsresult rv = NS_OK;
nsIReflowCommand::ReflowType type;
aReflowState.reflowState.reflowCommand->GetType(type);
nsIFrame *objectFrame;
aReflowState.reflowState.reflowCommand->GetChildFrame(objectFrame);
if (PR_TRUE==gsDebugIR) printf("TOF IR: IncrementalReflow_TargetIsMe with type=%d\n", type);
switch (type)
{
case nsIReflowCommand::FrameAppended :
case nsIReflowCommand::FrameInserted :
{
const nsStyleDisplay *childDisplay;
objectFrame->GetStyleData(eStyleStruct_Display, ((nsStyleStruct *&)childDisplay));
if (NS_STYLE_DISPLAY_TABLE_CAPTION == childDisplay->mDisplay)
{
rv = IR_CaptionInserted(aPresContext, aDesiredSize, aReflowState, aStatus,
objectFrame, PR_FALSE);
}
else
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: calling inner table reflow.\n");
rv = IR_InnerTableReflow(aPresContext, aDesiredSize, aReflowState, aStatus);
}
break;
}
/*
case nsIReflowCommand::FrameReplaced :
// if the frame to be replaced is mCaptionFrame
if (mCaptionFrame==objectFrame)
{
// verify that the new frame is a caption frame
const nsStyleDisplay *newFrameDisplay;
newFrame->GetStyleData(eStyleStruct_Display, ((nsStyleStruct *&)newFrameDisplay));
NS_ASSERTION(NS_STYLE_DISPLAY_TABLE_CAPTION == childDisplay->mDisplay, "bad new frame");
if (NS_STYLE_DISPLAY_TABLE_CAPTION == childDisplay->mDisplay)
{
rv = IR_CaptionInserted(aPresContext, aDesiredSize, aReflowState, aStatus,
objectFrame, PR_TRUE);
}
else
rv = NS_ERROR_ILLEGAL_VALUE;
}
else
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: calling inner table reflow.\n");
rv = IR_InnerTableReflow(aPresContext, aDesiredSize, aReflowState, aStatus);
}
*/
case nsIReflowCommand::FrameRemoved :
if (mCaptionFrame==objectFrame)
{
rv = IR_CaptionDeleted(aPresContext, aDesiredSize, aReflowState, aStatus);
}
else
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: calling inner table reflow.\n");
rv = IR_InnerTableReflow(aPresContext, aDesiredSize, aReflowState, aStatus);
}
break;
case nsIReflowCommand::StyleChanged :
rv = NS_ERROR_NOT_IMPLEMENTED;
if (PR_TRUE==gsDebugIR) printf("TOF IR: StyleChanged not implemented.\n");
break;
case nsIReflowCommand::ContentChanged :
NS_ASSERTION(PR_FALSE, "illegal reflow type: ContentChanged");
rv = NS_ERROR_ILLEGAL_VALUE;
break;
case nsIReflowCommand::PullupReflow:
case nsIReflowCommand::PushReflow:
case nsIReflowCommand::CheckPullupReflow :
case nsIReflowCommand::UserDefined :
rv = NS_ERROR_NOT_IMPLEMENTED;
if (PR_TRUE==gsDebugIR) printf("TOF IR: reflow command not implemented.\n");
break;
}
return rv;
}
nsresult nsTableOuterFrame::IR_InnerTableReflow(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus)
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: IR_InnerTableReflow\n");
nsresult rv = NS_OK;
const nsStyleText* captionTextStyle=nsnull;
nsMargin captionMargin;
// remember the old width and height
nsRect priorInnerTableRect;
mInnerTableFrame->GetRect(priorInnerTableRect);
if (PR_TRUE==gsDebugIR) printf("TOF IR: prior width=%d height=%d\n", priorInnerTableRect.width, priorInnerTableRect.height);
// pass along the reflow command to the inner table
if (PR_TRUE==gsDebugIR) printf("TOF IR: passing down incremental reflow command to inner table.\n");
nsHTMLReflowMetrics innerSize(aDesiredSize.maxElementSize);
nscoord tableMaxWidth = PR_MAX(aReflowState.reflowState.maxSize.width, mMinCaptionWidth);
if (PR_TRUE==gsDebugIR) printf("TOF IR: mincaptionWidth=%d, tableMaxWidth=%d.\n", mMinCaptionWidth, tableMaxWidth);
nsHTMLReflowState innerReflowState(mInnerTableFrame, aReflowState.reflowState,
nsSize(tableMaxWidth, aReflowState.reflowState.maxSize.height));
rv = ReflowChild(mInnerTableFrame, aPresContext, innerSize, innerReflowState, aStatus);
if (PR_TRUE==gsDebugIR) printf("TOF IR: inner table reflow returned %d with width=%d height=%d\n",
rv, innerSize.width, innerSize.height);
// if there is a caption and the width or height of the inner table changed from a successful reflow,
// then reflow or move the caption as needed
if ((nsnull!=mCaptionFrame) && (PR_TRUE==NS_SUCCEEDED(rv)))
{
// remember the old caption height
nsRect oldCaptionRect;
mCaptionFrame->GetRect(oldCaptionRect);
if (PR_TRUE==gsDebugIR) printf("TOF IR: prior caption height = %d\n", oldCaptionRect.height);
nsHTMLReflowMetrics captionSize(nsnull); // don't ask for MES, it hasn't changed
PRBool captionDimChanged=PR_FALSE;
PRBool captionWasReflowed=PR_FALSE;
if (priorInnerTableRect.width!=innerSize.width)
{ // the table width changed, so reflow the caption
nsHTMLReflowState captionReflowState(mCaptionFrame, aReflowState.reflowState,
nsSize(innerSize.width,
aReflowState.reflowState.maxSize.height),
eReflowReason_Resize);
nsIHTMLReflow* htmlReflow;
if (NS_OK == mCaptionFrame->QueryInterface(kIHTMLReflowIID, (void**)&htmlReflow))
{ // reflow the caption
if (PR_TRUE==gsDebugIR) printf("TOF IR: table width changed, resize-reflowing caption.\n");
htmlReflow->WillReflow(aPresContext);
rv = htmlReflow->Reflow(aPresContext, captionSize, captionReflowState, aStatus);
captionWasReflowed=PR_TRUE;
if (PR_TRUE==gsDebugIR) printf("TOF IR: caption reflow returned %d with width=%d height=%d\n",
rv, captionSize.width, captionSize.height);
if ((oldCaptionRect.height!=captionSize.height) || (oldCaptionRect.width!=captionSize.width))
captionDimChanged=PR_TRUE;
}
}
if (PR_TRUE==gsDebugIR) printf("TOF IR: captionDimChanged = %d\n", captionDimChanged);
// XXX: make this it's own method
// find where to place the caption
const nsStyleSpacing* spacing;
mCaptionFrame->GetStyleData(eStyleStruct_Spacing, (nsStyleStruct *&)spacing);
spacing->CalcMarginFor(mCaptionFrame, captionMargin);
mCaptionFrame->GetStyleData(eStyleStruct_Text, ((nsStyleStruct *&)captionTextStyle));
if ((priorInnerTableRect.height!=innerSize.height) || (PR_TRUE==captionDimChanged))
{
// Compute the caption's y-origin
nscoord captionY = captionMargin.top;
if ((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM))
{
captionY += innerSize.height;
}
// Place the caption
nsRect captionRect(captionMargin.left, captionY, 0, 0);
if (PR_TRUE==captionWasReflowed)
captionRect.SizeTo(captionSize.width, captionSize.height);
else
captionRect.SizeTo(oldCaptionRect.width, oldCaptionRect.height);
mCaptionFrame->SetRect(captionRect);
if (PR_TRUE==gsDebugIR)
{
printf("TOF IR: captionRect=");
stdout << captionRect;
printf("\n");
}
}
}
// if anything above failed, we just want to return an error at this point
if (NS_FAILED(rv))
return rv;
// Place the inner table
nsRect updatedCaptionRect(0,0,0,0);
if (nsnull!=mCaptionFrame)
mCaptionFrame->GetRect(updatedCaptionRect);
nscoord innerY; // innerY is the y-offset of the inner table
if (nsnull!=mCaptionFrame)
{ // factor in caption and it's margin
// we're guaranteed that captionMargin and captionTextStyle are set at this point
if ((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM))
{ // bottom caption
innerY = 0; // the inner table goes at the top of the outer table
// the total v-space consumed is the inner table height + the caption height + the margin between them
aReflowState.y = innerSize.height + updatedCaptionRect.YMost() + captionMargin.top;
}
else
{ // top caption
innerY = updatedCaptionRect.YMost() + captionMargin.bottom;
// the total v-space consumed is the inner table height + the caption height + the margin between them
aReflowState.y = innerY + innerSize.height;
}
}
else
{ // no caption
innerY=0;
aReflowState.y = innerSize.height;
}
nsRect innerRect(0, innerY, innerSize.width, innerSize.height);
mInnerTableFrame->SetRect(innerRect);
if (PR_TRUE==gsDebugIR)
{
printf("TOF IR: innerRect=");
stdout << innerRect;
printf("\n");
}
// set out params
// XXX: make this its own method
/* if we're incomplete, take up all the remaining height so we don't waste time
* trying to lay out in a slot that we know isn't tall enough to fit our minimum.
* otherwise, we're as tall as our kids want us to be */
if (NS_FRAME_IS_NOT_COMPLETE(aStatus))
aDesiredSize.height = aReflowState.reflowState.maxSize.height;
else
{ // this should be the caption height + inner table height + space between them
aDesiredSize.height = aReflowState.y;
}
// Return our desired rect
aDesiredSize.width = aReflowState.innerTableMaxSize.width;
aDesiredSize.ascent = aDesiredSize.height;
aDesiredSize.descent = 0;
if (PR_TRUE==gsDebugIR) printf("TOF IR: aDS.width=%d, aDS.height=%d\n",
aDesiredSize.width, aDesiredSize.height);
return rv;
}
/* the only difference between an insert and a replace is a replace
checks the old maxElementSize and reflows the table only if it
has changed
*/
nsresult nsTableOuterFrame::IR_CaptionInserted(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus,
nsIFrame * aCaptionFrame,
PRBool aReplace)
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: CaptionInserted\n");
nsresult rv = NS_OK;
nscoord oldCaptionMES = 0;
NS_PRECONDITION(nsnull!=aCaptionFrame, "null arg: aCaptionFrame");
if (PR_TRUE==aReplace && nsnull!=mCaptionFrame)
oldCaptionMES = mMinCaptionWidth;
// make aCaptionFrame this table's caption
mCaptionFrame = aCaptionFrame;
// reflow the caption frame, getting it's MES
nsSize maxElementSize;
nsHTMLReflowMetrics captionSize(&maxElementSize);
nsHTMLReflowState captionReflowState(mCaptionFrame, aReflowState.reflowState,
nsSize(mRect.width, aReflowState.reflowState.maxSize.height),
eReflowReason_Initial);
nsIHTMLReflow* htmlReflow;
if (NS_OK == mCaptionFrame->QueryInterface(kIHTMLReflowIID, (void**)&htmlReflow))
{ // initial reflow of the caption
htmlReflow->WillReflow(aPresContext);
htmlReflow->Reflow(aPresContext, captionSize, captionReflowState, aStatus);
mMinCaptionWidth = maxElementSize.width;
// get the caption's alignment
const nsStyleText* captionTextStyle;
mCaptionFrame->GetStyleData(eStyleStruct_Text, ((nsStyleStruct *&)captionTextStyle));
if ((captionTextStyle->mVerticalAlign.GetUnit()!=eStyleUnit_Enumerated) || // default is "top"
((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
((captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_TOP) ||
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM))))
{
// XXX: caption align = left|right ignored here!
// if the caption's MES > TMW, reflow the inner table
nscoord tableMaxWidth = ((nsTableFrame *)mInnerTableFrame)->GetMaxTableWidth();
nsHTMLReflowMetrics innerSize(aDesiredSize.maxElementSize);
if ((oldCaptionMES != mMinCaptionWidth) && (mMinCaptionWidth > tableMaxWidth))
{
nsHTMLReflowState innerReflowState(mInnerTableFrame, aReflowState.reflowState,
nsSize(mMinCaptionWidth, aReflowState.reflowState.maxSize.height));
rv = ReflowChild(mInnerTableFrame, aPresContext, innerSize, innerReflowState, aStatus);
}
else
{ // set innerSize as if the inner table were reflowed
innerSize.height = mRect.height;
innerSize.width = mRect.width;
}
// set maxElementSize width if requested
if (nsnull != aDesiredSize.maxElementSize) {
if (mMinCaptionWidth > aDesiredSize.maxElementSize->width) {
aDesiredSize.maxElementSize->width = mMinCaptionWidth;
}
}
// find where to place the caption
nsMargin captionMargin;
const nsStyleSpacing* spacing;
mCaptionFrame->GetStyleData(eStyleStruct_Spacing, (nsStyleStruct *&)spacing);
spacing->CalcMarginFor(mCaptionFrame, captionMargin);
// Compute the caption's y-origin
nscoord captionY = captionMargin.top;
const nsStyleText* captionTextStyle;
mCaptionFrame->GetStyleData(eStyleStruct_Text, ((nsStyleStruct *&)captionTextStyle));
if ((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM))
{
captionY += innerSize.height;
}
// Place the caption
nsRect captionRect(captionMargin.left, captionY, 0, 0);
captionRect.SizeTo(captionSize.width, captionSize.height);
mCaptionFrame->SetRect(captionRect);
// Place the inner table
nscoord innerY;
if ((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM))
{ // bottom caption
innerY = 0;
aReflowState.y = captionRect.YMost() + captionMargin.bottom;
}
else
{ // top caption
innerY = captionRect.YMost() + captionMargin.bottom;
aReflowState.y = innerY + innerSize.height;
}
nsRect innerRect(0, innerY, innerSize.width, innerSize.height);
mInnerTableFrame->SetRect(innerRect);
/* if we're incomplete, take up all the remaining height so we don't waste time
* trying to lay out in a slot that we know isn't tall enough to fit our minimum.
* otherwise, we're as tall as our kids want us to be */
if (NS_FRAME_IS_NOT_COMPLETE(aStatus))
aDesiredSize.height = aReflowState.reflowState.maxSize.height;
else
aDesiredSize.height = aReflowState.y;
// Return our desired rect
aDesiredSize.width = aReflowState.innerTableMaxSize.width;
aDesiredSize.ascent = aDesiredSize.height;
aDesiredSize.descent = 0;
}
else
{
rv = NS_ERROR_NOT_IMPLEMENTED;
if (PR_TRUE==gsDebugIR) printf("TOF IR: left|right caption not implemented.\n");
}
}
return rv;
}
nsresult nsTableOuterFrame::IR_CaptionDeleted(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
OuterTableReflowState& aReflowState,
nsReflowStatus& aStatus)
{
if (PR_TRUE==gsDebugIR) printf("TOF IR: CaptionDeleted\n");
nsresult rv = NS_OK;
if (nsnull!=mCaptionFrame)
{
// get the caption's alignment
const nsStyleText* captionTextStyle;
mCaptionFrame->GetStyleData(eStyleStruct_Text, ((nsStyleStruct *&)captionTextStyle));
// if the caption's MES > TMW, reflow the inner table minus the MES
nscoord tableMaxWidth = ((nsTableFrame *)mInnerTableFrame)->GetMaxTableWidth();
nsHTMLReflowMetrics innerSize(aDesiredSize.maxElementSize);
nscoord oldMinCaptionWidth = mMinCaptionWidth;
// set the cached min caption width to 0 here, so it can't effect inner table reflow
mMinCaptionWidth = 0;
mCaptionFrame=nsnull;
if (oldMinCaptionWidth > tableMaxWidth)
{ // the old caption width had an effect on the inner table width, so reflow the inner table
nsHTMLReflowState innerReflowState(mInnerTableFrame, aReflowState.reflowState,
nsSize(aReflowState.reflowState.maxSize.width,
aReflowState.reflowState.maxSize.height));
// ReflowChild sets MES
ReflowChild(mInnerTableFrame, aPresContext, innerSize, innerReflowState, aStatus);
}
// if the caption was a top caption, move the inner table to the correct offset
if ((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM))
{
mInnerTableFrame->MoveTo(0,0);
}
// Return our desired rect
aDesiredSize.width = innerSize.width; //aReflowState.innerTableMaxSize.width;
aDesiredSize.height = innerSize.height;
aDesiredSize.ascent = aDesiredSize.height;
aDesiredSize.descent = 0;
}
else
rv = NS_ERROR_ILLEGAL_VALUE;
return rv;
}
1998-04-14 00:24:54 +04:00
/**
* Called by the Reflow() member function to compute the table width
*/
nscoord nsTableOuterFrame::GetTableWidth(const nsHTMLReflowState& aReflowState)
{
nscoord maxWidth;
// Figure out the overall table width constraint. Default case, get 100% of
// available space
if (NS_UNCONSTRAINEDSIZE == aReflowState.maxSize.width) {
maxWidth = aReflowState.maxSize.width;
} else {
const nsStylePosition* position =
(const nsStylePosition*)mStyleContext->GetStyleData(eStyleStruct_Position);
switch (position->mWidth.GetUnit()) {
case eStyleUnit_Coord:
maxWidth = position->mWidth.GetCoordValue();
break;
case eStyleUnit_Auto:
maxWidth = aReflowState.maxSize.width;
break;
case eStyleUnit_Percent:
maxWidth = (nscoord)((float)aReflowState.maxSize.width *
position->mWidth.GetPercentValue());
break;
case eStyleUnit_Proportional:
case eStyleUnit_Inherit:
// XXX for now these fall through
default:
maxWidth = aReflowState.maxSize.width;
break;
}
// Subtract out border and padding
const nsStyleSpacing* spacing =
(const nsStyleSpacing*)mStyleContext->GetStyleData(eStyleStruct_Spacing);
nsMargin borderPadding;
spacing->CalcBorderPaddingFor(this, borderPadding);
maxWidth -= borderPadding.left + borderPadding.right;
if (maxWidth <= 0) {
// Nonsense style specification
maxWidth = 0;
}
}
return maxWidth;
}
/**
* Reflow is a multi-step process.
* 1. First we reflow the caption frame and get its maximum element size. We
* do this once during our initial reflow and whenever the caption changes
* incrementally
* 2. Next we reflow the inner table. This gives us the actual table width.
* The table must be at least as wide as the caption maximum element size
* 3. Now that we have the table width we reflow the caption and gets its
* desired height
* 4. Then we place the caption and the inner table
*
* If the table height is constrained, e.g. page mode, then it's possible the
* inner table no longer fits and has to be reflowed again this time with s
* smaller available height
1998-04-14 00:24:54 +04:00
*/
NS_METHOD nsTableOuterFrame::Reflow(nsIPresContext& aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
1998-04-14 00:24:54 +04:00
{
1998-04-17 02:25:09 +04:00
if (PR_TRUE==gsDebug)
printf("%p: nsTableOuterFrame::Reflow : maxSize=%d,%d\n",
this, aReflowState.maxSize.width, aReflowState.maxSize.height);
1998-04-14 00:24:54 +04:00
PRIntervalTime startTime;
if (gsTiming) {
startTime = PR_IntervalNow();
}
// Initialize out parameters
aDesiredSize.width = 0;
aDesiredSize.height = 0;
if (nsnull != aDesiredSize.maxElementSize) {
aDesiredSize.maxElementSize->width = 0;
aDesiredSize.maxElementSize->height = 0;
1998-04-14 00:24:54 +04:00
}
aStatus = NS_FRAME_COMPLETE;
1998-04-14 00:24:54 +04:00
// Initialize our local reflow state
OuterTableReflowState state(aPresContext, aReflowState);
if (eReflowReason_Incremental == aReflowState.reason) {
IncrementalReflow(aPresContext, aDesiredSize, state, aStatus);
1998-04-14 00:24:54 +04:00
// Return our desired rect
aDesiredSize.width = state.innerTableMaxSize.width;
} else {
if (eReflowReason_Initial == aReflowState.reason) {
//KIPP/TROY: uncomment the following line for your own testing, do not check it in
// NS_ASSERTION(nsnull == mFirstChild, "unexpected reflow reason");
// Set up our kids. They're already present, on an overflow list,
// or there are none so we'll create them now
MoveOverflowToChildList();
// Lay out the caption and get its maximum element size
1998-07-09 00:21:17 +04:00
if (nsnull != mCaptionFrame) {
nsSize maxElementSize;
nsHTMLReflowMetrics captionSize(&maxElementSize);
nsHTMLReflowState captionReflowState(mCaptionFrame, aReflowState,
nsSize(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE),
eReflowReason_Initial);
nsIHTMLReflow* htmlReflow;
if (NS_OK == mCaptionFrame->QueryInterface(kIHTMLReflowIID, (void**)&htmlReflow)) {
htmlReflow->WillReflow(aPresContext);
htmlReflow->Reflow(aPresContext, captionSize, captionReflowState, aStatus);
mMinCaptionWidth = maxElementSize.width;
}
1998-07-09 00:21:17 +04:00
}
}
// At this point, we must have an inner table frame, and we might have a caption
1998-07-09 00:21:17 +04:00
NS_ASSERTION(nsnull != mFirstChild, "no children");
NS_ASSERTION(nsnull != mInnerTableFrame, "no mInnerTableFrame");
// Compute the width to use for the table. In the case of an auto sizing
// table this represents the maximum available width
nscoord tableWidth = GetTableWidth(aReflowState);
// If the caption max element size is larger, then use it instead.
// XXX: caption align = left|right ignored here!
if (mMinCaptionWidth > tableWidth) {
tableWidth = mMinCaptionWidth;
}
// First reflow the inner table
nsHTMLReflowState innerReflowState(mInnerTableFrame, aReflowState,
nsSize(tableWidth, aReflowState.maxSize.height));
nsHTMLReflowMetrics innerSize(aDesiredSize.maxElementSize);
ReflowChild(mInnerTableFrame, aPresContext, innerSize, innerReflowState, aStatus);
// Table's max element size is the MAX of the caption's max element size
// and the inner table's max element size...
if (nsnull != aDesiredSize.maxElementSize) {
if (mMinCaptionWidth > aDesiredSize.maxElementSize->width) {
aDesiredSize.maxElementSize->width = mMinCaptionWidth;
1998-04-14 00:24:54 +04:00
}
}
state.innerTableMaxSize.width = innerSize.width;
// Now that we know the table width we can reflow the caption, and
// place the caption and the inner table
if (nsnull != mCaptionFrame) {
// Get the caption's margin
nsMargin captionMargin;
const nsStyleSpacing* spacing;
mCaptionFrame->GetStyleData(eStyleStruct_Spacing, (nsStyleStruct *&)spacing);
spacing->CalcMarginFor(mCaptionFrame, captionMargin);
// Compute the caption's y-origin
nscoord captionY = captionMargin.top;
const nsStyleText* captionTextStyle;
mCaptionFrame->GetStyleData(eStyleStruct_Text, ((nsStyleStruct *&)captionTextStyle));
if ((captionTextStyle->mVerticalAlign.GetUnit()==eStyleUnit_Enumerated) &&
(captionTextStyle->mVerticalAlign.GetIntValue()==NS_STYLE_VERTICAL_ALIGN_BOTTOM)) {
captionY += innerSize.height;
}
// Reflow the caption. Let it be as high as it wants
nsHTMLReflowState captionReflowState(mCaptionFrame, state.reflowState,
nsSize(innerSize.width, NS_UNCONSTRAINEDSIZE),
eReflowReason_Resize);
nsHTMLReflowMetrics captionSize(nsnull);
nsIHTMLReflow* htmlReflow;
nsRect captionRect(captionMargin.left, captionY, 0, 0);
if (NS_OK == mCaptionFrame->QueryInterface(kIHTMLReflowIID, (void**)&htmlReflow)) {
nsReflowStatus captionStatus;
htmlReflow->WillReflow(aPresContext);
htmlReflow->Reflow(aPresContext, captionSize, captionReflowState,
captionStatus);
NS_ASSERTION(NS_FRAME_IS_COMPLETE(captionStatus), "unexpected reflow status");
// XXX If the height is constrained then we need to check whether the inner
// table still fits...
// Place the caption
captionRect.SizeTo(captionSize.width, captionSize.height);
mCaptionFrame->SetRect(captionRect);
}
// Place the inner table
nscoord innerY;
if ((captionTextStyle->mVerticalAlign.GetUnit()!=eStyleUnit_Enumerated) ||
(captionTextStyle->mVerticalAlign.GetIntValue()!=NS_STYLE_VERTICAL_ALIGN_BOTTOM)) {
// top caption
innerY = captionRect.YMost() + captionMargin.bottom;
state.y = innerY + innerSize.height;
} else {
// bottom caption
innerY = 0;
state.y = captionRect.YMost() + captionMargin.bottom;
}
nsRect innerRect(0, innerY, innerSize.width, innerSize.height);
mInnerTableFrame->SetRect(innerRect);
} else {
// Place the inner table
nsRect innerRect(0, 0, innerSize.width, innerSize.height);
mInnerTableFrame->SetRect(innerRect);
state.y = innerSize.height;
}
}
/* if we're incomplete, take up all the remaining height so we don't waste time
* trying to lay out in a slot that we know isn't tall enough to fit our minimum.
* otherwise, we're as tall as our kids want us to be */
if (NS_FRAME_IS_NOT_COMPLETE(aStatus))
aDesiredSize.height = aReflowState.maxSize.height;
else
aDesiredSize.height = state.y;
// Return our desired rect
aDesiredSize.width = state.innerTableMaxSize.width;
aDesiredSize.ascent = aDesiredSize.height;
aDesiredSize.descent = 0;
if (gsDebug==PR_TRUE)
{
if (nsnull!=aDesiredSize.maxElementSize)
printf("%p: Outer frame Reflow complete, returning %s with aDesiredSize = %d,%d and aMaxElementSize=%d,%d\n",
this, NS_FRAME_IS_COMPLETE(aStatus)? "Complete" : "Not Complete",
aDesiredSize.width, aDesiredSize.height,
aDesiredSize.maxElementSize->width, aDesiredSize.maxElementSize->height);
else
printf("%p: Outer frame Reflow complete, returning %s with aDesiredSize = %d,%d and NSNULL aMaxElementSize\n",
this, NS_FRAME_IS_COMPLETE(aStatus)? "Complete" : "Not Complete",
aDesiredSize.width, aDesiredSize.height);
1998-04-14 00:24:54 +04:00
}
if (gsTiming) {
PRIntervalTime endTime = PR_IntervalNow();
printf("Table reflow took %ld ticks for frame %d\n",
endTime-startTime, this);/* XXX need to use LL_* macros! */
}
return NS_OK;
1998-04-14 00:24:54 +04:00
}
// Position and size aKidFrame and update our reflow state. The origin of
// aKidRect is relative to the upper-left origin of our frame, and includes
// any left/top margin.
void nsTableOuterFrame::PlaceChild( OuterTableReflowState& aReflowState,
nsIFrame* aKidFrame,
const nsRect& aKidRect,
nsSize* aMaxElementSize,
nsSize& aKidMaxElementSize)
1998-04-14 00:24:54 +04:00
{
1998-04-21 02:49:15 +04:00
if (PR_TRUE==gsDebug)
printf ("outer table place child: %p with aKidRect %d %d %d %d\n",
1998-04-14 00:24:54 +04:00
aKidFrame, aKidRect.x, aKidRect.y,
aKidRect.width, aKidRect.height);
// Place and size the child
aKidFrame->SetRect(aKidRect);
// Adjust the running y-offset
aReflowState.y += aKidRect.height;
1998-04-14 00:24:54 +04:00
// If our height is constrained then update the available height
if (PR_FALSE == aReflowState.unconstrainedHeight) {
aReflowState.availSize.height -= aKidRect.height;
1998-04-14 00:24:54 +04:00
}
/* Update the maximum element size, which is the sum of:
* the maxElementSize of our first row
* plus the maxElementSize of the top caption if we include it
* plus the maxElementSize of the bottom caption if we include it
*/
if (aKidFrame == mCaptionFrame)
1998-04-14 00:24:54 +04:00
{
if (nsnull != aMaxElementSize) {
aMaxElementSize->width = aKidMaxElementSize.width;
aMaxElementSize->height = aKidMaxElementSize.height;
}
}
}
NS_METHOD
nsTableOuterFrame::CreateContinuingFrame(nsIPresContext& aPresContext,
nsIFrame* aParent,
nsIStyleContext* aStyleContext,
nsIFrame*& aContinuingFrame)
1998-04-14 00:24:54 +04:00
{
nsTableOuterFrame* cf = new nsTableOuterFrame(mContent, aParent);
if (nsnull == cf) {
return NS_ERROR_OUT_OF_MEMORY;
}
PrepareContinuingFrame(aPresContext, aParent, aStyleContext, cf);
if (PR_TRUE==gsDebug)
printf("nsTableOuterFrame::CCF parent = %p, this=%p, cf=%p\n", aParent, this, cf);
aContinuingFrame = cf;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
void
nsTableOuterFrame::PrepareContinuingFrame(nsIPresContext& aPresContext,
nsIFrame* aParent,
nsIStyleContext* aStyleContext,
nsTableOuterFrame* aContFrame)
1998-04-14 00:24:54 +04:00
{
// Append the continuing frame to the flow
aContFrame->AppendToFlow(this);
aContFrame->SetStyleContext(&aPresContext, aStyleContext);
1998-04-14 00:24:54 +04:00
}
NS_METHOD nsTableOuterFrame::VerifyTree() const
1998-04-14 00:24:54 +04:00
{
#ifdef NS_DEBUG
#endif
return NS_OK;
1998-04-14 00:24:54 +04:00
}
/**
* Remove and delete aChild's next-in-flow(s). Updates the sibling and flow
* pointers.
*
* Updates the child count and content offsets of all containers that are
* affected
*
* Overloaded here because nsContainerFrame makes assumptions about pseudo-frames
* that are not true for tables.
*
* @param aChild child this child's next-in-flow
* @return PR_TRUE if successful and PR_FALSE otherwise
*/
PRBool nsTableOuterFrame::DeleteChildsNextInFlow(nsIPresContext& aPresContext, nsIFrame* aChild)
{
NS_PRECONDITION(IsChild(aChild), "bad geometric parent");
nsIFrame* nextInFlow;
aChild->GetNextInFlow(nextInFlow);
NS_PRECONDITION(nsnull != nextInFlow, "null next-in-flow");
nsTableOuterFrame* parent;
nextInFlow->GetGeometricParent((nsIFrame*&)parent);
// If the next-in-flow has a next-in-flow then delete it too (and
// delete it first).
nsIFrame* nextNextInFlow;
nextInFlow->GetNextInFlow(nextNextInFlow);
if (nsnull != nextNextInFlow) {
parent->DeleteChildsNextInFlow(aPresContext, nextInFlow);
}
#ifdef NS_DEBUG
PRInt32 childCount;
nsIFrame* firstChild;
nextInFlow->FirstChild(firstChild);
childCount = LengthOf(firstChild);
NS_ASSERTION(childCount == 0, "deleting !empty next-in-flow");
NS_ASSERTION((0 == childCount) && (nsnull == firstChild), "deleting !empty next-in-flow");
#endif
// Disconnect the next-in-flow from the flow list
nextInFlow->BreakFromPrevFlow();
// Take the next-in-flow out of the parent's child list
if (parent->mFirstChild == nextInFlow) {
nextInFlow->GetNextSibling(parent->mFirstChild);
} else {
nsIFrame* nextSibling;
// Because the next-in-flow is not the first child of the parent
// we know that it shares a parent with aChild. Therefore, we need
// to capture the next-in-flow's next sibling (in case the
// next-in-flow is the last next-in-flow for aChild AND the
// next-in-flow is not the last child in parent)
NS_ASSERTION(parent->IsChild(aChild), "screwy flow");
aChild->GetNextSibling(nextSibling);
NS_ASSERTION(nextSibling == nextInFlow, "unexpected sibling");
nextInFlow->GetNextSibling(nextSibling);
aChild->SetNextSibling(nextSibling);
}
// Delete the next-in-flow frame and adjust it's parent's child count
nextInFlow->DeleteFrame(aPresContext);
#ifdef NS_DEBUG
aChild->GetNextInFlow(nextInFlow);
NS_POSTCONDITION(nsnull == nextInFlow, "non null next-in-flow");
#endif
return PR_TRUE;
}
/* ----- global methods ----- */
1998-04-14 00:24:54 +04:00
nsresult
NS_NewTableOuterFrame( nsIContent* aContent,
nsIFrame* aParentFrame,
nsIFrame*& aResult)
1998-04-14 00:24:54 +04:00
{
nsIFrame* it = new nsTableOuterFrame(aContent, aParentFrame);
1998-04-14 00:24:54 +04:00
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
aResult = it;
1998-04-14 00:24:54 +04:00
return NS_OK;
}
/* ----- debugging methods ----- */
NS_METHOD nsTableOuterFrame::List(FILE* out, PRInt32 aIndent, nsIListFilter *aFilter) const
{
// if a filter is present, only output this frame if the filter says we should
// since this could be any "tag" with the right display type, we'll
// just pretend it's a tbody
if (nsnull==aFilter)
return nsContainerFrame::List(out, aIndent, aFilter);
nsAutoString tagString("tbody");
PRBool outputMe = aFilter->OutputTag(&tagString);
if (PR_TRUE==outputMe)
{
// Indent
for (PRInt32 i = aIndent; --i >= 0; ) fputs(" ", out);
// Output the tag and rect
nsIAtom* tag;
mContent->GetTag(tag);
if (tag != nsnull) {
nsAutoString buf;
tag->ToString(buf);
fputs(buf, out);
NS_RELEASE(tag);
}
fprintf(out, "(%d)", ContentIndexInContainer(this));
out << mRect;
if (0 != mState) {
fprintf(out, " [state=%08x]", mState);
}
fputs("\n", out);
}
// Output the children
if (nsnull != mFirstChild) {
if (PR_TRUE==outputMe)
{
if (0 != mState) {
fprintf(out, " [state=%08x]\n", mState);
}
}
for (nsIFrame* child = mFirstChild; child; child->GetNextSibling(child)) {
child->List(out, aIndent + 1, aFilter);
}
} else {
if (PR_TRUE==outputMe)
{
if (0 != mState) {
fprintf(out, " [state=%08x]\n", mState);
}
}
}
return NS_OK;
}