RenderMultiColumnBlock.cpp [plain text]
#include "config.h"
#include "RenderMultiColumnBlock.h"
#include "RenderMultiColumnFlowThread.h"
#include "RenderMultiColumnSet.h"
#include "RenderView.h"
#include "StyleInheritedData.h"
using namespace std;
namespace WebCore {
RenderMultiColumnBlock::RenderMultiColumnBlock(Element* element)
: RenderBlock(element)
, m_flowThread(0)
, m_columnCount(1)
, m_columnWidth(0)
, m_columnHeightAvailable(0)
, m_inBalancingPass(false)
{
}
void RenderMultiColumnBlock::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox())
child->setStyle(RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK));
}
void RenderMultiColumnBlock::computeColumnCountAndWidth()
{
m_columnCount = 1;
m_columnWidth = contentLogicalWidth();
ASSERT(!style()->hasAutoColumnCount() || !style()->hasAutoColumnWidth());
LayoutUnit availWidth = m_columnWidth;
LayoutUnit colGap = columnGap();
LayoutUnit colWidth = max<LayoutUnit>(1, LayoutUnit(style()->columnWidth()));
int colCount = max<int>(1, style()->columnCount());
if (style()->hasAutoColumnWidth() && !style()->hasAutoColumnCount()) {
m_columnCount = colCount;
m_columnWidth = max<LayoutUnit>(0, (availWidth - ((m_columnCount - 1) * colGap)) / m_columnCount);
} else if (!style()->hasAutoColumnWidth() && style()->hasAutoColumnCount()) {
m_columnCount = max<LayoutUnit>(1, (availWidth + colGap) / (colWidth + colGap));
m_columnWidth = ((availWidth + colGap) / m_columnCount) - colGap;
} else {
m_columnCount = max<LayoutUnit>(min<LayoutUnit>(colCount, (availWidth + colGap) / (colWidth + colGap)), 1);
m_columnWidth = ((availWidth + colGap) / m_columnCount) - colGap;
}
}
bool RenderMultiColumnBlock::updateLogicalWidthAndColumnWidth()
{
bool relayoutChildren = RenderBlock::updateLogicalWidthAndColumnWidth();
LayoutUnit oldColumnWidth = m_columnWidth;
computeColumnCountAndWidth();
if (m_columnWidth != oldColumnWidth)
relayoutChildren = true;
return relayoutChildren;
}
void RenderMultiColumnBlock::checkForPaginationLogicalHeightChange(LayoutUnit& , bool& , bool& )
{
updateLogicalHeight();
m_columnHeightAvailable = max<LayoutUnit>(contentLogicalHeight(), 0);
setLogicalHeight(0);
}
bool RenderMultiColumnBlock::relayoutForPagination(bool, LayoutUnit, LayoutStateMaintainer& statePusher)
{
if (m_inBalancingPass || !requiresBalancing())
return false;
m_inBalancingPass = true;
bool needsRelayout;
bool neededRelayout = false;
bool firstPass = true;
do {
needsRelayout = false;
for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox())
if (childBox != m_flowThread && childBox->isRenderMultiColumnSet()) {
RenderMultiColumnSet* multicolSet = toRenderMultiColumnSet(childBox);
if (multicolSet->calculateBalancedHeight(firstPass)) {
multicolSet->setChildNeedsLayout(true, MarkOnlyThis);
needsRelayout = true;
}
}
if (needsRelayout) {
neededRelayout = true;
m_flowThread->setChildNeedsLayout(true, MarkOnlyThis);
setChildNeedsLayout(true, MarkOnlyThis);
if (firstPass)
statePusher.pop();
layoutBlock(false);
}
firstPass = false;
} while (needsRelayout);
m_inBalancingPass = false;
return neededRelayout;
}
void RenderMultiColumnBlock::addChild(RenderObject* newChild, RenderObject* beforeChild)
{
if (!m_flowThread) {
m_flowThread = RenderMultiColumnFlowThread::createAnonymous(document());
m_flowThread->setStyle(RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK));
RenderBlock::addChild(m_flowThread);
}
m_flowThread->addChild(newChild, beforeChild);
}
RenderObject* RenderMultiColumnBlock::layoutSpecialExcludedChild(bool relayoutChildren)
{
if (!m_flowThread)
return 0;
bool shouldInvalidateRegions = false;
for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
if (childBox == m_flowThread)
continue;
if (relayoutChildren || childBox->needsLayout()) {
if (!m_inBalancingPass && childBox->isRenderMultiColumnSet())
toRenderMultiColumnSet(childBox)->prepareForLayout();
shouldInvalidateRegions = true;
}
}
if (shouldInvalidateRegions)
m_flowThread->invalidateRegions();
if (relayoutChildren)
m_flowThread->setChildNeedsLayout(true, MarkOnlyThis);
setLogicalTopForChild(m_flowThread, borderAndPaddingBefore());
m_flowThread->layoutIfNeeded();
determineLogicalLeftPositionForChild(m_flowThread);
return m_flowThread;
}
const char* RenderMultiColumnBlock::renderName() const
{
if (isFloating())
return "RenderMultiColumnBlock (floating)";
if (isOutOfFlowPositioned())
return "RenderMultiColumnBlock (positioned)";
if (isAnonymousBlock())
return "RenderMultiColumnBlock (anonymous)";
if (isPseudoElement())
return "RenderMultiColumnBlock (generated)";
if (isAnonymous())
return "RenderMultiColumnBlock (generated)";
if (isRelPositioned())
return "RenderMultiColumnBlock (relative positioned)";
return "RenderMultiColumnBlock";
}
}