Implement row height distribution as it was described in the comment. Consider to give space to empty unconstrained rows as one option bug 197391 r/sr=roc a=mtschrep

This commit is contained in:
bmlk@gmx.de 2007-12-15 08:08:52 -08:00
Родитель f03a324bb4
Коммит 1dbce7da4c
1 изменённых файлов: 60 добавлений и 35 удалений

Просмотреть файл

@ -3278,7 +3278,8 @@ nsTableFrame::DistributeHeightToRows(const nsHTMLReflowState& aReflowState,
return; return;
} }
// get the first row without a style height where its row group has an unconstrianed height // get the first row without a style height where its row group has an
// unconstrained height
nsTableRowGroupFrame* firstUnStyledRG = nsnull; nsTableRowGroupFrame* firstUnStyledRG = nsnull;
nsTableRowFrame* firstUnStyledRow = nsnull; nsTableRowFrame* firstUnStyledRow = nsnull;
for (rgX = 0; rgX < rowGroups.Length() && !firstUnStyledRG; rgX++) { for (rgX = 0; rgX < rowGroups.Length() && !firstUnStyledRG; rgX++) {
@ -3296,33 +3297,48 @@ nsTableFrame::DistributeHeightToRows(const nsHTMLReflowState& aReflowState,
} }
} }
nsTableRowFrame* lastElligibleRow = nsnull; nsTableRowFrame* lastEligibleRow = nsnull;
// accumulate the correct divisor. This will be the total of all unstyled rows inside // Accumulate the correct divisor. This will be the total total height of all
// unstyled row groups, unless there are none, in which case, it will be all rows // unstyled rows inside unstyled row groups, unless there are none, in which
// case, it will be number of all rows. If the unstyled rows don't have a
// height, divide the space equally among them.
nscoord divisor = 0; nscoord divisor = 0;
PRUint32 rowCount = 0; PRInt32 eligibleRows = 0;
PRBool expandEmptyRows = PR_FALSE;
if (!firstUnStyledRow) {
// there is no unstyled row
divisor = GetRowCount();
}
else {
for (rgX = 0; rgX < rowGroups.Length(); rgX++) { for (rgX = 0; rgX < rowGroups.Length(); rgX++) {
nsTableRowGroupFrame* rgFrame = rowGroups[rgX]; nsTableRowGroupFrame* rgFrame = rowGroups[rgX];
if (!firstUnStyledRG || !rgFrame->HasStyleHeight()) { if (!firstUnStyledRG || !rgFrame->HasStyleHeight()) {
nsTableRowFrame* rowFrame = rgFrame->GetFirstRow(); nsTableRowFrame* rowFrame = rgFrame->GetFirstRow();
while (rowFrame) { while (rowFrame) {
if (!firstUnStyledRG || !rowFrame->HasStyleHeight()) { if (!firstUnStyledRG || !rowFrame->HasStyleHeight()) {
NS_ASSERTION(rowFrame->GetSize().height >= 0, "negative height"); NS_ASSERTION(rowFrame->GetSize().height >= 0,
"negative row frame height");
divisor += rowFrame->GetSize().height; divisor += rowFrame->GetSize().height;
++rowCount; eligibleRows++;
lastElligibleRow = rowFrame; lastEligibleRow = rowFrame;
} }
rowFrame = rowFrame->GetNextRow(); rowFrame = rowFrame->GetNextRow();
} }
} }
} }
if (divisor < 0) { if (divisor <= 0) {
if (eligibleRows > 0) {
expandEmptyRows = PR_TRUE;
}
else {
NS_ERROR("invalid divisor"); NS_ERROR("invalid divisor");
return; return;
} }
}
}
// allocate the extra height to the unstyled row groups and rows // allocate the extra height to the unstyled row groups and rows
pctBasis = aAmount - amountUsed; nscoord heightToDistribute = aAmount - amountUsed;
yOriginRG = borderPadding.top + cellSpacingY; yOriginRG = borderPadding.top + cellSpacingY;
yEndRG = yOriginRG; yEndRG = yOriginRG;
for (rgX = 0; rgX < rowGroups.Length(); rgX++) { for (rgX = 0; rgX < rowGroups.Length(); rgX++) {
@ -3330,23 +3346,32 @@ nsTableFrame::DistributeHeightToRows(const nsHTMLReflowState& aReflowState,
nscoord amountUsedByRG = 0; nscoord amountUsedByRG = 0;
nscoord yOriginRow = 0; nscoord yOriginRow = 0;
nsRect rgRect = rgFrame->GetRect(); nsRect rgRect = rgFrame->GetRect();
// see if there is an eligible row group // see if there is an eligible row group or we distribute to all rows
if (!firstUnStyledRG || !rgFrame->HasStyleHeight()) { if (!firstUnStyledRG || !rgFrame->HasStyleHeight() || !eligibleRows) {
nsTableRowFrame* rowFrame = rgFrame->GetFirstRow(); nsTableRowFrame* rowFrame = rgFrame->GetFirstRow();
while (rowFrame) { while (rowFrame) {
nsRect rowRect = rowFrame->GetRect(); nsRect rowRect = rowFrame->GetRect();
// see if there is an eligible row // see if there is an eligible row or we distribute to all rows
if (!firstUnStyledRow || !rowFrame->HasStyleHeight()) { if (!firstUnStyledRow || !rowFrame->HasStyleHeight() || !eligibleRows) {
// The amount of additional space each row gets is proportional to its height float ratio;
float percent; if (eligibleRows) {
if (divisor != 0) { if (!expandEmptyRows) {
percent = float(rowRect.height) / float(divisor); // The amount of additional space each row gets is proportional to
// its height
ratio = float(rowRect.height) / float(divisor);
} else { } else {
percent = 1.0f / float(rowCount); // empty rows get all the same additional space
ratio = 1.0f / float(eligibleRows);
} }
// give rows their percentage, except for the last row which gets the remainder }
nscoord amountForRow = (rowFrame == lastElligibleRow) else {
? aAmount - amountUsed : NSToCoordRound(((float)(pctBasis)) * percent); // all rows get the same additional space
ratio = 1.0f / float(divisor);
}
// give rows their additional space, except for the last row which
// gets the remainder
nscoord amountForRow = (rowFrame == lastEligibleRow)
? aAmount - amountUsed : NSToCoordRound(((float)(heightToDistribute)) * ratio);
amountForRow = PR_MIN(amountForRow, aAmount - amountUsed); amountForRow = PR_MIN(amountForRow, aAmount - amountUsed);
// update the row height // update the row height
nsRect newRowRect(rowRect.x, yOriginRow, rowRect.width, rowRect.height + amountForRow); nsRect newRowRect(rowRect.x, yOriginRow, rowRect.width, rowRect.height + amountForRow);