#include <kdebug.h>
#include "rendering/render_text.h"
#include "rendering/render_table.h"
#include "rendering/render_canvas.h"
#include "xml/dom_nodeimpl.h"
#include "xml/dom_docimpl.h"
#include "html/html_formimpl.h"
#include "render_block.h"
#include "khtmlview.h"
#include "htmltags.h"
using namespace DOM;
namespace khtml {
RenderBlock::RenderBlock(DOM::NodeImpl* node)
:RenderFlow(node)
{
m_childrenInline = true;
m_floatingObjects = 0;
m_positionedObjects = 0;
m_pre = false;
m_firstLine = false;
m_clearStatus = CNONE;
m_maxTopPosMargin = m_maxTopNegMargin = m_maxBottomPosMargin = m_maxBottomNegMargin = 0;
m_topMarginQuirk = m_bottomMarginQuirk = false;
m_overflowHeight = 0;
m_overflowWidth = 0;
}
RenderBlock::~RenderBlock()
{
delete m_floatingObjects;
delete m_positionedObjects;
}
void RenderBlock::setStyle(RenderStyle* _style)
{
RenderFlow::setStyle(_style);
setReplaced(style()->display()==INLINE_BLOCK);
m_pre = false;
if (_style->whiteSpace() == PRE)
m_pre = true;
RenderObject *child = firstChild();
while (child != 0)
{
if (child->isAnonymousBlock())
{
RenderStyle* newStyle = new RenderStyle();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
child->setStyle(newStyle);
}
child = child->nextSibling();
}
updatePseudoChild(RenderStyle::BEFORE, firstChild());
updatePseudoChild(RenderStyle::AFTER, lastChild());
}
void RenderBlock::addChildToFlow(RenderObject* newChild, RenderObject* beforeChild)
{
if (!beforeChild && lastChild() && lastChild()->style()->styleType() == RenderStyle::AFTER)
beforeChild = lastChild();
bool madeBoxesNonInline = FALSE;
if (beforeChild && beforeChild->parent() != this) {
KHTMLAssert(beforeChild->parent());
KHTMLAssert(beforeChild->parent()->isAnonymousBlock());
if (newChild->isInline()) {
beforeChild->parent()->addChild(newChild,beforeChild);
return;
}
else if (beforeChild->parent()->firstChild() != beforeChild)
return beforeChild->parent()->addChild(newChild, beforeChild);
else
return addChildToFlow(newChild, beforeChild->parent());
}
if (!newChild->isInline())
newChild->setPos(newChild->xPos(), -500000);
if (!newChild->isText() && newChild->style()->position() != STATIC)
setOverhangingContents();
if ( m_childrenInline && !newChild->isInline() && !newChild->isFloatingOrPositioned() )
{
makeChildrenNonInline(beforeChild);
madeBoxesNonInline = true;
if (beforeChild && beforeChild->parent() != this) {
beforeChild = beforeChild->parent();
KHTMLAssert(beforeChild->isAnonymousBlock());
KHTMLAssert(beforeChild->parent() == this);
}
}
else if (!m_childrenInline && !newChild->isFloatingOrPositioned())
{
if (newChild->isInline()) {
if (beforeChild) {
if (beforeChild->previousSibling() && beforeChild->previousSibling()->isAnonymousBlock()) {
beforeChild->previousSibling()->addChild(newChild);
return;
}
}
else {
if (m_last && m_last->isAnonymousBlock()) {
m_last->addChild(newChild);
return;
}
}
RenderBlock* newBox = createAnonymousBlock();
RenderBox::addChild(newBox,beforeChild);
newBox->addChild(newChild);
return;
}
}
RenderBox::addChild(newChild,beforeChild);
if ( madeBoxesNonInline )
removeLeftoverAnonymousBoxes();
}
static void getInlineRun(RenderObject* start, RenderObject* stop,
RenderObject*& inlineRunStart,
RenderObject*& inlineRunEnd)
{
inlineRunStart = inlineRunEnd = 0;
RenderObject * curr = start;
while (curr && !curr->isInline())
curr = curr->nextSibling();
if (!curr)
return;
inlineRunStart = inlineRunEnd = curr;
curr = curr->nextSibling();
while (curr && curr->isInline() && (curr != stop)) {
inlineRunEnd = curr;
curr = curr->nextSibling();
}
}
void RenderBlock::makeChildrenNonInline(RenderObject *insertionPoint)
{
KHTMLAssert(isInlineBlockOrInlineTable() || !isInline());
KHTMLAssert(!insertionPoint || insertionPoint->parent() == this);
m_childrenInline = false;
RenderObject *child = firstChild();
while (child) {
RenderObject *inlineRunStart, *inlineRunEnd;
getInlineRun(child, insertionPoint, inlineRunStart, inlineRunEnd);
if (!inlineRunStart)
break;
child = inlineRunEnd->nextSibling();
RenderBlock* box = createAnonymousBlock();
insertChildNode(box, inlineRunStart);
RenderObject* o = inlineRunStart;
while(o != inlineRunEnd)
{
RenderObject* no = o;
o = no->nextSibling();
box->appendChildNode(removeChildNode(no));
}
box->appendChildNode(removeChildNode(inlineRunEnd));
box->close();
box->setPos(box->xPos(), -500000);
}
}
void RenderBlock::removeChild(RenderObject *oldChild)
{
RenderObject* prev = oldChild->previousSibling();
RenderObject* next = oldChild->nextSibling();
bool mergedBlocks = false;
if (document()->renderer() && !isInline() && !oldChild->isInline() && !oldChild->continuation() &&
prev && prev->isAnonymousBlock() && prev->childrenInline() &&
next && next->isAnonymousBlock() && next->childrenInline()) {
RenderObject* o = next->firstChild();
while (o) {
RenderObject* no = o;
o = no->nextSibling();
prev->appendChildNode(next->removeChildNode(no));
no->setNeedsLayoutAndMinMaxRecalc();
}
prev->setNeedsLayoutAndMinMaxRecalc();
next->detach();
mergedBlocks = true;
}
RenderFlow::removeChild(oldChild);
if (mergedBlocks && prev && !prev->previousSibling() && !prev->nextSibling()) {
RenderObject* anonBlock = removeChildNode(prev);
m_childrenInline = true;
RenderObject* o = anonBlock->firstChild();
while (o) {
RenderObject* no = o;
o = no->nextSibling();
appendChildNode(anonBlock->removeChildNode(no));
no->setNeedsLayoutAndMinMaxRecalc();
}
anonBlock->detach();
}
}
bool RenderBlock::requiresLayer() {
return !isTableCell() && (RenderObject::requiresLayer() || (style()->hidesOverflow() && !isBody()));
}
bool RenderBlock::isSelfCollapsingBlock() const
{
if (m_height > 0 ||
isTable() || (borderBottom() + paddingBottom() + borderTop() + paddingTop()) != 0 ||
style()->minHeight().value > 0)
return false;
if (style()->height().isVariable() ||
(style()->height().isFixed() && style()->height().value == 0)) {
if (childrenInline())
return !firstLineBox();
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isFloatingOrPositioned())
continue;
if (!child->isSelfCollapsingBlock())
return false;
}
return true;
}
return false;
}
void RenderBlock::layout()
{
layoutBlock(false);
}
void RenderBlock::layoutBlock(bool relayoutChildren)
{
KHTMLAssert( needsLayout() );
KHTMLAssert( minMaxKnown() );
if (isInline() && !isInlineBlockOrInlineTable()) return;
if (!relayoutChildren && posChildNeedsLayout() && !normalChildNeedsLayout() && !selfNeedsLayout()) {
layoutPositionedObjects(relayoutChildren);
setNeedsLayout(false);
return;
}
#ifdef INCREMENTAL_REPAINTING
QRect oldBounds, oldFullBounds;
bool checkForRepaint = checkForRepaintDuringLayout();
if (checkForRepaint)
getAbsoluteRepaintRectIncludingFloats(oldBounds, oldFullBounds);
#endif
int oldWidth = m_width;
calcWidth();
m_overflowWidth = m_width;
if ( oldWidth != m_width )
relayoutChildren = true;
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << renderName() << "(RenderBlock) " << this << " ::layout() width=" << m_width << ", needsLayout=" << needsLayout() << endl;
if(containingBlock() == static_cast<RenderObject *>(this))
kdDebug( 6040 ) << renderName() << ": containingBlock == this" << endl;
#endif
clearFloats();
m_height = 0;
m_overflowHeight = 0;
m_clearStatus = CNONE;
if (!isTableCell()) {
initMaxMarginValues();
m_topMarginQuirk = style()->marginTop().quirk;
m_bottomMarginQuirk = style()->marginBottom().quirk;
if (element() && element()->id() == ID_FORM && element()->isMalformed())
m_maxBottomPosMargin = m_maxBottomNegMargin = 0;
}
if (style()->scrollsOverflow() && m_layer) {
if (style()->overflow() == OSCROLL) {
m_layer->setHasHorizontalScrollbar(true);
m_layer->setHasVerticalScrollbar(true);
}
m_layer->moveScrollbarsAside();
}
if (childrenInline())
layoutInlineChildren( relayoutChildren );
else
layoutBlockChildren( relayoutChildren );
int toAdd = borderBottom() + paddingBottom();
if (style()->hidesOverflow() && m_layer)
toAdd += m_layer->horizontalScrollbarHeight();
if ( hasOverhangingFloats() && (isInlineBlockOrInlineTable() || isFloatingOrPositioned() || style()->hidesOverflow() ||
(parent() && parent()->isFlexibleBox())) )
m_height = floatBottom() + toAdd;
int oldHeight = m_height;
calcHeight();
if (oldHeight != m_height) {
relayoutChildren = true;
if (m_overflowHeight > m_height)
m_overflowHeight -= paddingBottom() + borderBottom();
if (m_overflowHeight < m_height)
m_overflowHeight = m_height;
}
if (isTableCell()) {
if (lastChild() && lastChild()->hasOverhangingFloats()) {
KHTMLAssert(lastChild()->isRenderBlock());
m_height = lastChild()->yPos() + static_cast<RenderBlock*>(lastChild())->floatBottom();
m_height += borderBottom() + paddingBottom();
}
if (m_overflowHeight > m_height && !style()->hidesOverflow())
m_height = m_overflowHeight + borderBottom() + paddingBottom();
}
if( hasOverhangingFloats() && (isFloating() || isTableCell())) {
m_height = floatBottom();
m_height += borderBottom() + paddingBottom();
}
layoutPositionedObjects( relayoutChildren );
if (m_overflowWidth < m_width)
m_overflowWidth = m_width;
if (m_overflowHeight < m_height)
m_overflowHeight = m_height;
if (style()->hidesOverflow() && m_layer)
m_layer->updateScrollInfoAfterLayout();
#ifdef INCREMENTAL_REPAINTING
if (checkForRepaint)
repaintAfterLayoutIfNeeded(oldBounds, oldFullBounds);
#endif
setNeedsLayout(false);
}
void RenderBlock::layoutBlockChildren( bool relayoutChildren )
{
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << renderName() << " layoutBlockChildren( " << this <<" ), relayoutChildren="<< relayoutChildren << endl;
#endif
int xPos = borderLeft() + paddingLeft();
if( style()->direction() == RTL )
xPos = m_width - paddingRight() - borderRight();
int toAdd = borderBottom() + paddingBottom();
if (style()->hidesOverflow() && m_layer)
toAdd += m_layer->horizontalScrollbarHeight();
m_height = borderTop() + paddingTop();
RenderObject* legend = layoutLegend(relayoutChildren);
int minHeight = m_height + toAdd;
m_overflowHeight = m_height;
RenderObject* child = firstChild();
RenderBlock* prevFlow = 0;
RenderObject* prevBlock = 0;
RenderObject* compactChild = 0;
RenderObject* blockForCompactChild = 0;
bool treatCompactAsBlock = false;
bool canCollapseWithChildren = !isCanvas() && !isRoot() && !isPositioned() &&
!isFloating() && !isTableCell() && !style()->hidesOverflow() && !isInlineBlockOrInlineTable();
bool canCollapseTopWithChildren = canCollapseWithChildren && (m_height == 0);
bool canCollapseBottomWithChildren = canCollapseWithChildren && (toAdd == 0) &&
(style()->height().isVariable() && style()->height().value == 0);
bool quirkContainer = isTableCell() || isBody();
bool shouldCollapseChild = true;
bool topMarginContributor = true;
int prevPosMargin = canCollapseTopWithChildren ? maxTopMargin(true) : 0;
int prevNegMargin = canCollapseTopWithChildren ? maxTopMargin(false) : 0;
bool clearOccurred = false;
bool selfCollapsingBlockClearedFloat = false;
bool topChildQuirk = false;
bool bottomChildQuirk = false;
bool determinedTopQuirk = false;
bool strictMode = !style()->htmlHacks();
while( child != 0 )
{
shouldCollapseChild = true;
int oldTopPosMargin = m_maxTopPosMargin;
int oldTopNegMargin = m_maxTopNegMargin;
if (legend == child) {
child = child->nextSibling();
continue; }
if (relayoutChildren || floatBottom() > m_y ||
(child->isReplaced() && (child->style()->width().isPercent() || child->style()->height().isPercent())) ||
(child->isRenderBlock() && child->style()->height().isPercent()))
child->setChildNeedsLayout(true);
if (child->isPositioned())
{
child->containingBlock()->insertPositionedObject(child);
if (child->hasStaticX()) {
if (style()->direction() == LTR)
child->setStaticX(xPos);
else
child->setStaticX(borderRight()+paddingRight());
}
if (child->hasStaticY()) {
int yPosEstimate = m_height + (!topMarginContributor ? (prevPosMargin - prevNegMargin) : 0);
child->setStaticY(yPosEstimate);
}
child = child->nextSibling();
continue;
} else if (child->isReplaced())
child->layoutIfNeeded();
if ( child->isFloating() ) {
insertFloatingObject( child );
int marginOffset = !topMarginContributor ? (prevPosMargin - prevNegMargin) : 0;
m_height += marginOffset;
positionNewFloats();
m_height -= marginOffset;
child = child->nextSibling();
continue;
}
if (child->isCompact() && !compactChild && (child->childrenInline() || child->isReplaced())) {
RenderObject* next = child->nextSibling();
RenderObject* curr = next;
while (curr && curr->isFloatingOrPositioned())
curr = curr->nextSibling();
if (curr && curr->isRenderBlock() && !curr->isCompact() && !curr->isRunIn()) {
curr->calcWidth(); child->style()->setDisplay(BLOCK);
child->calcWidth();
child->style()->setDisplay(COMPACT);
int childMargins = child->marginLeft() + child->marginRight();
int margin = style()->direction() == LTR ? curr->marginLeft() : curr->marginRight();
if (margin < (childMargins + child->maxWidth())) {
child->style()->setDisplay(BLOCK);
treatCompactAsBlock = true;
}
else {
blockForCompactChild = curr;
compactChild = child;
child->setInline(true);
child->setPos(0,0);
RenderObject* next = child->nextSibling();
removeChildNode(child);
curr->insertChildNode(child, curr->firstChild());
child = next;
continue;
}
}
}
if (child->isRunIn() && (child->childrenInline() || child->isReplaced())) {
RenderObject* curr = child->nextSibling();
while (curr && curr->isFloatingOrPositioned())
curr = curr->nextSibling();
if (curr && (curr->isRenderBlock() && curr->childrenInline() && !curr->isCompact() && !curr->isRunIn())) {
child->setInline(true);
child->setPos(0,0);
RenderObject* next = child->nextSibling();
removeChildNode(child);
curr->insertChildNode(child, curr->firstChild());
child = next;
continue;
}
}
child->calcVerticalMargins();
int yPosEstimate = m_height;
if (prevBlock) {
yPosEstimate += kMax(prevBlock->collapsedMarginBottom(), child->marginTop());
if (prevFlow) {
if (prevFlow->yPos() + prevFlow->floatBottom() > yPosEstimate)
child->setChildNeedsLayout(true);
else
prevFlow = 0;
}
}
else if (!canCollapseTopWithChildren || !topMarginContributor)
yPosEstimate += child->marginTop();
int fb = floatBottom();
if (child->avoidsFloats() && style()->width().isFixed() && child->minWidth() > lineWidth(m_height)) {
if (fb > m_height) {
m_height = yPosEstimate = fb;
shouldCollapseChild = false;
clearOccurred = true;
prevFlow = 0;
prevBlock = 0;
}
}
if (fb > m_height)
child->setChildNeedsLayout(true);
int oldChildX = child->xPos();
int oldChildY = child->yPos();
child->setPos(child->xPos(), yPosEstimate);
child->layoutIfNeeded();
if (shouldCollapseChild) {
int posTop = child->maxTopMargin(true);
int negTop = child->maxTopMargin(false);
if (child->isSelfCollapsingBlock()) {
if (child->maxBottomMargin(true) > posTop)
posTop = child->maxBottomMargin(true);
if (child->maxBottomMargin(false) > negTop)
negTop = child->maxBottomMargin(false);
}
bool topQuirk = child->isTopMarginQuirk();
if (canCollapseTopWithChildren && topMarginContributor && !clearOccurred) {
if (strictMode || !quirkContainer || !topQuirk) {
if (posTop > m_maxTopPosMargin)
m_maxTopPosMargin = posTop;
if (negTop > m_maxTopNegMargin)
m_maxTopNegMargin = negTop;
}
if (!determinedTopQuirk && !topQuirk && (posTop-negTop)) {
m_topMarginQuirk = false;
determinedTopQuirk = true;
}
if (!determinedTopQuirk && topQuirk && marginTop() == 0)
m_topMarginQuirk = true;
}
if (quirkContainer && topMarginContributor && (posTop-negTop))
topChildQuirk = topQuirk;
int ypos = m_height;
if (child->isSelfCollapsingBlock()) {
int collapsedTopPos = prevPosMargin;
int collapsedTopNeg = prevNegMargin;
if (child->maxTopMargin(true) > prevPosMargin)
collapsedTopPos = prevPosMargin = child->maxTopMargin(true);
if (child->maxTopMargin(false) > prevNegMargin)
collapsedTopNeg = prevNegMargin = child->maxTopMargin(false);
if (child->maxBottomMargin(true) > prevPosMargin)
prevPosMargin = child->maxBottomMargin(true);
if (child->maxBottomMargin(false) > prevNegMargin)
prevNegMargin = child->maxBottomMargin(false);
if (!canCollapseTopWithChildren || !topMarginContributor)
ypos = m_height + collapsedTopPos - collapsedTopNeg;
}
else {
if (!topMarginContributor ||
(!canCollapseTopWithChildren
&& (strictMode || !quirkContainer || !topChildQuirk)
)) {
int absPos = prevPosMargin > posTop ? prevPosMargin : posTop;
int absNeg = prevNegMargin > negTop ? prevNegMargin : negTop;
int collapsedMargin = absPos - absNeg;
m_height += collapsedMargin;
ypos = m_height;
}
prevPosMargin = child->maxBottomMargin(true);
prevNegMargin = child->maxBottomMargin(false);
if (prevPosMargin-prevNegMargin) {
bottomChildQuirk = child->isBottomMarginQuirk();
}
selfCollapsingBlockClearedFloat = false;
}
child->setPos(child->xPos(), ypos);
if (ypos != yPosEstimate) {
if (child->style()->width().isPercent() && child->usesLineWidth())
child->setChildNeedsLayout(true);
if (child->containsFloats() || containsFloats())
child->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
}
}
else
selfCollapsingBlockClearedFloat = false;
int heightIncrease = getClearDelta(child);
if (heightIncrease) {
child->setPos(child->xPos(), child->yPos()+heightIncrease);
clearOccurred = true;
if (!child->isSelfCollapsingBlock())
m_height += heightIncrease;
else {
prevPosMargin = QMAX(0, child->yPos() - m_height);
prevNegMargin = 0;
selfCollapsingBlockClearedFloat = true;
}
if (topMarginContributor && canCollapseTopWithChildren) {
m_maxTopPosMargin = oldTopPosMargin;
m_maxTopNegMargin = oldTopNegMargin;
topMarginContributor = false;
}
if (child->style()->width().isPercent() && child->usesLineWidth())
child->setChildNeedsLayout(true);
if (child->containsFloats())
child->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
}
if (topMarginContributor && !child->isSelfCollapsingBlock())
topMarginContributor = false;
int chPos = xPos;
if (style()->direction() == LTR) {
chPos += child->marginLeft();
if (child->avoidsFloats()) {
int leftOff = leftOffset(m_height);
if (style()->textAlign() != KHTML_CENTER && child->style()->marginLeft().type != Variable) {
if (child->marginLeft() < 0)
leftOff += child->marginLeft();
chPos = kMax(chPos, leftOff); }
else if (leftOff != xPos) {
int cw = lineWidth( child->yPos() );
static_cast<RenderBox*>(child)->calcHorizontalMargins
( child->style()->marginLeft(), child->style()->marginRight(), cw);
chPos = leftOff + child->marginLeft();
}
}
} else {
chPos -= child->width() + child->marginRight();
if (child->avoidsFloats()) {
int rightOff = rightOffset(m_height);
if (style()->textAlign() != KHTML_CENTER && child->style()->marginRight().type != Variable) {
if (child->marginRight() < 0)
rightOff -= child->marginRight();
chPos = kMin(chPos, rightOff - child->width()); } else if (rightOff != xPos) {
int cw = lineWidth( child->yPos() );
static_cast<RenderBox*>(child)->calcHorizontalMargins
( child->style()->marginLeft(), child->style()->marginRight(), cw);
chPos = rightOff - child->marginRight() - child->width();
}
}
}
child->setPos(chPos, child->yPos());
m_height += child->height();
int overflowDelta = child->overflowHeight(false) - child->height();
if (m_height + overflowDelta > m_overflowHeight)
m_overflowHeight = m_height + overflowDelta;
prevBlock = child;
if (child->isRenderBlock() && !child->avoidsFloats())
prevFlow = static_cast<RenderBlock*>(child);
if (child->hasOverhangingFloats() && !child->style()->hidesOverflow()) {
addOverHangingFloats( static_cast<RenderBlock *>(child), -child->xPos(), -child->yPos(), true );
}
int rightChildPos = child->xPos() + QMAX(child->overflowWidth(false), child->width());
if (rightChildPos > m_overflowWidth)
m_overflowWidth = rightChildPos;
if (child == blockForCompactChild) {
blockForCompactChild = 0;
if (compactChild) {
int compactXPos = xPos+compactChild->marginLeft();
if (style()->direction() == RTL) {
compactChild->calcWidth(); compactXPos = width() - borderRight() - paddingRight() - marginRight() -
compactChild->width() - compactChild->marginRight();
}
compactXPos -= child->xPos(); compactChild->setPos(compactXPos, compactChild->yPos()); compactChild = 0;
}
}
if (treatCompactAsBlock) {
child->style()->setDisplay(COMPACT);
treatCompactAsBlock = false;
}
#ifdef INCREMENTAL_REPAINTING
if (!selfNeedsLayout() && checkForRepaintDuringLayout())
child->repaintDuringLayoutIfMoved(oldChildX, oldChildY);
#endif
child = child->nextSibling();
}
if (selfCollapsingBlockClearedFloat)
canCollapseBottomWithChildren = false;
if (!canCollapseBottomWithChildren
&& (strictMode || !quirkContainer || !bottomChildQuirk))
m_height += prevPosMargin - prevNegMargin;
m_height += toAdd;
if (m_height < minHeight)
m_height = minHeight;
if (m_overflowHeight < m_height)
m_overflowHeight = m_height;
if (canCollapseBottomWithChildren && !topMarginContributor) {
if (prevPosMargin > m_maxBottomPosMargin)
m_maxBottomPosMargin = prevPosMargin;
if (prevNegMargin > m_maxBottomNegMargin)
m_maxBottomNegMargin = prevNegMargin;
if (!bottomChildQuirk)
m_bottomMarginQuirk = false;
if (bottomChildQuirk && marginBottom() == 0)
m_bottomMarginQuirk = true;
}
setNeedsLayout(false);
}
void RenderBlock::layoutPositionedObjects(bool relayoutChildren)
{
if (m_positionedObjects) {
RenderObject* r;
QPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
if ( relayoutChildren )
r->setChildNeedsLayout(true);
r->layoutIfNeeded();
}
}
}
#ifdef INCREMENTAL_REPAINTING
void RenderBlock::getAbsoluteRepaintRectIncludingFloats(QRect& bounds, QRect& fullBounds)
{
bounds = fullBounds = getAbsoluteRepaintRect();
if (hasOverhangingFloats() && m_floatingObjects) {
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (!r->noPaint && !r->node->layer()) {
QRect childRect, childFullRect;
r->node->getAbsoluteRepaintRectIncludingFloats(childRect, childFullRect);
fullBounds = fullBounds.unite(childFullRect);
}
}
}
}
void RenderBlock::repaintFloatingDescendants()
{
if (hasOverhangingFloats()) {
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (!r->noPaint && !r->node->layer()) {
r->node->repaint();
r->node->repaintFloatingDescendants();
}
}
}
}
void RenderBlock::repaintObjectsBeforeLayout()
{
RenderFlow::repaintObjectsBeforeLayout();
if (!needsLayout())
return;
if (m_positionedObjects) {
RenderObject* r;
QPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it )
r->repaintObjectsBeforeLayout();
}
}
#endif
void RenderBlock::paint(QPainter* p, int _x, int _y, int _w, int _h, int _tx, int _ty, PaintAction paintAction)
{
_tx += m_x;
_ty += m_y;
if (!isRoot() && !isInlineFlow() && !overhangingContents() && !isRelPositioned() && !isPositioned() )
{
int h = m_overflowHeight;
int yPos = _ty;
if (m_floatingObjects && floatBottom() > h)
h = floatBottom();
if (m_firstLineBox && m_firstLineBox->topOverflow() < 0)
yPos += m_firstLineBox->topOverflow();
int os = 2*maximalOutlineSize(paintAction);
if( (yPos >= _y + _h + os) || (_ty + h <= _y - os))
return;
}
paintObject(p, _x, _y, _w, _h, _tx, _ty, paintAction);
}
void RenderBlock::paintObject(QPainter *p, int _x, int _y,
int _w, int _h, int _tx, int _ty, PaintAction paintAction)
{
#ifdef DEBUG_LAYOUT
#endif
bool inlineFlow = isInlineFlow();
bool isPrinting = (p->device()->devType() == QInternal::Printer);
if (!inlineFlow &&
(paintAction == PaintActionElementBackground || paintAction == PaintActionChildBackground) &&
shouldPaintBackgroundOrBorder() && style()->visibility() == VISIBLE) {
paintBoxDecorations(p, _x, _y, _w, _h, _tx, _ty);
}
if (paintAction == PaintActionElementBackground)
return;
if (paintAction == PaintActionChildBackgrounds)
paintAction = PaintActionChildBackground;
paintLineBoxBackgroundBorder(p, _x, _y, _w, _h, _tx, _ty, paintAction);
int scrolledX = _tx;
int scrolledY = _ty;
if (style()->hidesOverflow() && m_layer)
m_layer->subtractScrollOffset(scrolledX, scrolledY);
for (RenderObject *child = firstChild(); child; child = child->nextSibling()) {
if (isPrinting && !childrenInline() && child->style()->pageBreakBefore() == PBALWAYS &&
inRootBlockContext() && (_ty + child->yPos()) > _y && (_ty + child->yPos()) < _y + _h) {
canvas()->setBestTruncatedAt(_ty + child->yPos(), this, true);
return;
}
if (!child->layer() && !child->isFloating())
child->paint(p, _x, _y, _w, _h, scrolledX, scrolledY, paintAction);
if (isPrinting && !childrenInline() && child->style()->pageBreakAfter() == PBALWAYS &&
inRootBlockContext() && (_ty + child->yPos() + child->height()) > _y &&
(_ty + child->yPos() + child->height()) < _y + _h) {
canvas()->setBestTruncatedAt(_ty + child->yPos() + child->height() + child->collapsedMarginBottom(), this, true);
return;
}
}
paintLineBoxDecorations(p, _x, _y, _w, _h, scrolledX, scrolledY, paintAction);
if (!inlineFlow && (paintAction == PaintActionFloat || paintAction == PaintActionSelection))
paintFloats(p, _x, _y, _w, _h, scrolledX, scrolledY, paintAction == PaintActionSelection);
if (!inlineFlow && paintAction == PaintActionOutline &&
style()->outlineWidth() && style()->visibility() == VISIBLE)
paintOutline(p, _tx, _ty, width(), height(), style());
#ifdef BOX_DEBUG
if ( style() && style()->visibility() == VISIBLE ) {
if(isAnonymous())
outlineBox(p, _tx, _ty, "green");
if(isFloating())
outlineBox(p, _tx, _ty, "yellow");
else
outlineBox(p, _tx, _ty);
}
#endif
}
void RenderBlock::paintFloats(QPainter *p, int _x, int _y,
int _w, int _h, int _tx, int _ty, bool paintSelection)
{
if (!m_floatingObjects)
return;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (!r->noPaint && !r->node->layer()) {
if (paintSelection) {
r->node->paint(p, _x, _y, _w, _h,
_tx + r->left - r->node->xPos() + r->node->marginLeft(),
_ty + r->startY - r->node->yPos() + r->node->marginTop(),
PaintActionSelection);
}
else {
r->node->paint(p, _x, _y, _w, _h,
_tx + r->left - r->node->xPos() + r->node->marginLeft(),
_ty + r->startY - r->node->yPos() + r->node->marginTop(),
PaintActionElementBackground);
r->node->paint(p, _x, _y, _w, _h,
_tx + r->left - r->node->xPos() + r->node->marginLeft(),
_ty + r->startY - r->node->yPos() + r->node->marginTop(),
PaintActionChildBackgrounds);
r->node->paint(p, _x, _y, _w, _h,
_tx + r->left - r->node->xPos() + r->node->marginLeft(),
_ty + r->startY - r->node->yPos() + r->node->marginTop(),
PaintActionFloat);
r->node->paint(p, _x, _y, _w, _h,
_tx + r->left - r->node->xPos() + r->node->marginLeft(),
_ty + r->startY - r->node->yPos() + r->node->marginTop(),
PaintActionForeground);
r->node->paint(p, _x, _y, _w, _h,
_tx + r->left - r->node->xPos() + r->node->marginLeft(),
_ty + r->startY - r->node->yPos() + r->node->marginTop(),
PaintActionOutline);
}
}
}
}
void RenderBlock::insertPositionedObject(RenderObject *o)
{
if (!m_positionedObjects) {
m_positionedObjects = new QPtrList<RenderObject>;
m_positionedObjects->setAutoDelete(false);
}
else {
QPtrListIterator<RenderObject> it(*m_positionedObjects);
RenderObject* f;
while ( (f = it.current()) ) {
if (f == o) return;
++it;
}
}
setOverhangingContents();
m_positionedObjects->append(o);
}
void RenderBlock::removePositionedObject(RenderObject *o)
{
if (m_positionedObjects) {
QPtrListIterator<RenderObject> it(*m_positionedObjects);
while (it.current()) {
if (it.current() == o)
m_positionedObjects->removeRef(it.current());
++it;
}
}
}
void RenderBlock::insertFloatingObject(RenderObject *o)
{
if (!m_floatingObjects) {
m_floatingObjects = new QPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
}
else {
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
FloatingObject* f;
while ( (f = it.current()) ) {
if (f->node == o) return;
++it;
}
}
FloatingObject *newObj;
if (o->isFloating()) {
o->layoutIfNeeded();
if(o->style()->floating() == FLEFT)
newObj = new FloatingObject(FloatingObject::FloatLeft);
else
newObj = new FloatingObject(FloatingObject::FloatRight);
newObj->startY = -1;
newObj->endY = -1;
newObj->width = o->width() + o->marginLeft() + o->marginRight();
}
else {
KHTMLAssert(false);
newObj = 0; }
newObj->node = o;
m_floatingObjects->append(newObj);
}
void RenderBlock::removeFloatingObject(RenderObject *o)
{
if (m_floatingObjects) {
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->node == o)
m_floatingObjects->removeRef(it.current());
++it;
}
}
}
void RenderBlock::positionNewFloats()
{
if(!m_floatingObjects) return;
FloatingObject *f = m_floatingObjects->getLast();
if(!f || f->startY != -1) return;
FloatingObject *lastFloat;
while(1)
{
lastFloat = m_floatingObjects->prev();
if (!lastFloat || lastFloat->startY != -1) {
m_floatingObjects->next();
break;
}
f = lastFloat;
}
int y = m_height;
if(lastFloat && lastFloat->startY > y)
y = lastFloat->startY;
while(f)
{
if (f->node->containingBlock()!=this)
{
f = m_floatingObjects->next();
continue;
}
RenderObject *o = f->node;
int _height = o->height() + o->marginTop() + o->marginBottom();
int ro = rightOffset(); int lo = leftOffset(); int fwidth = f->width; if (ro - lo < fwidth)
fwidth = ro - lo;
#ifdef INCREMENTAL_REPAINTING
int oldChildX = o->xPos();
int oldChildY = o->yPos();
#endif
if (o->style()->floating() == FLEFT)
{
if ( o->style()->clear() & CLEFT )
y = QMAX( leftBottom(), y );
int heightRemainingLeft = 1;
int heightRemainingRight = 1;
int fx = leftRelOffset(y,lo, false, &heightRemainingLeft);
while (rightRelOffset(y,ro, false, &heightRemainingRight)-fx < fwidth)
{
y += QMIN( heightRemainingLeft, heightRemainingRight );
fx = leftRelOffset(y,lo, false, &heightRemainingLeft);
}
if (fx<0) fx=0;
f->left = fx;
o->setPos(fx + o->marginLeft(), y + o->marginTop());
}
else
{
if ( o->style()->clear() & CRIGHT )
y = QMAX( rightBottom(), y );
int heightRemainingLeft = 1;
int heightRemainingRight = 1;
int fx = rightRelOffset(y,ro, false, &heightRemainingRight);
while (fx - leftRelOffset(y,lo, false, &heightRemainingLeft) < fwidth)
{
y += QMIN(heightRemainingLeft, heightRemainingRight);
fx = rightRelOffset(y,ro, false, &heightRemainingRight);
}
if (fx<f->width) fx=f->width;
f->left = fx - f->width;
o->setPos(fx - o->marginRight() - o->width(), y + o->marginTop());
}
f->startY = y;
f->endY = f->startY + _height;
#ifdef INCREMENTAL_REPAINTING
if (checkForRepaintDuringLayout())
o->repaintDuringLayoutIfMoved(oldChildX, oldChildY);
#endif
f = m_floatingObjects->next();
}
}
void RenderBlock::newLine()
{
positionNewFloats();
int newY = 0;
switch(m_clearStatus)
{
case CLEFT:
newY = leftBottom();
break;
case CRIGHT:
newY = rightBottom();
break;
case CBOTH:
newY = floatBottom();
default:
break;
}
if(m_height < newY)
{
m_height = newY;
}
m_clearStatus = CNONE;
}
int
RenderBlock::leftOffset() const
{
return borderLeft()+paddingLeft();
}
int
RenderBlock::leftRelOffset(int y, int fixedOffset, bool applyTextIndent,
int *heightRemaining ) const
{
int left = fixedOffset;
if (m_floatingObjects) {
if ( heightRemaining ) *heightRemaining = 1;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
{
if (r->startY <= y && r->endY > y &&
r->type == FloatingObject::FloatLeft &&
r->left + r->width > left) {
left = r->left + r->width;
if ( heightRemaining ) *heightRemaining = r->endY - y;
}
}
}
if (applyTextIndent && m_firstLine && style()->direction() == LTR) {
int cw=0;
if (style()->textIndent().isPercent())
cw = containingBlock()->contentWidth();
left += style()->textIndent().minWidth(cw);
}
return left;
}
int
RenderBlock::rightOffset() const
{
int right = m_width - borderRight() - paddingRight();
if (style()->scrollsOverflow() && m_layer)
right -= m_layer->verticalScrollbarWidth();
return right;
}
int
RenderBlock::rightRelOffset(int y, int fixedOffset, bool applyTextIndent,
int *heightRemaining ) const
{
int right = fixedOffset;
if (m_floatingObjects) {
if (heightRemaining) *heightRemaining = 1;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
{
if (r->startY <= y && r->endY > y &&
r->type == FloatingObject::FloatRight &&
r->left < right) {
right = r->left;
if ( heightRemaining ) *heightRemaining = r->endY - y;
}
}
}
if (applyTextIndent && m_firstLine && style()->direction() == RTL) {
int cw=0;
if (style()->textIndent().isPercent())
cw = containingBlock()->contentWidth();
right += style()->textIndent().minWidth(cw);
}
return right;
}
unsigned short
RenderBlock::lineWidth(int y) const
{
int result = rightOffset(y) - leftOffset(y);
return (result < 0) ? 0 : result;
}
int
RenderBlock::nearestFloatBottom(int height) const
{
if (!m_floatingObjects) return 0;
int bottom = 0;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->endY>height && (r->endY<bottom || bottom==0))
bottom=r->endY;
return QMAX(bottom, height);
}
int
RenderBlock::floatBottom() const
{
if (!m_floatingObjects) return 0;
int bottom=0;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->endY>bottom)
bottom=r->endY;
return bottom;
}
int
RenderBlock::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
{
int bottom = RenderFlow::lowestPosition(includeOverflowInterior, includeSelf);
if (!includeOverflowInterior && style()->hidesOverflow())
return bottom;
if (includeSelf && m_overflowHeight > bottom)
bottom = m_overflowHeight;
if (m_floatingObjects) {
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (!r->noPaint) {
int lp = r->startY + r->node->marginTop() + r->node->lowestPosition(false);
bottom = kMax(bottom, lp);
}
}
}
if (m_positionedObjects && !isCanvas()) {
RenderObject* r;
QPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
int lp = r->yPos() + r->lowestPosition(false);
bottom = kMax(bottom, lp);
}
}
if (!includeSelf && lastLineBox()) {
int lp = lastLineBox()->yPos() + lastLineBox()->height();
bottom = kMax(bottom, lp);
}
return bottom;
}
int RenderBlock::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
int right = RenderFlow::rightmostPosition(includeOverflowInterior, includeSelf);
if (!includeOverflowInterior && style()->hidesOverflow())
return right;
if (includeSelf && m_overflowWidth > right)
right = m_overflowWidth;
if (m_floatingObjects) {
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (!r->noPaint) {
int rp = r->left + r->node->marginLeft() + r->node->rightmostPosition(false);
right = kMax(right, rp);
}
}
}
if (m_positionedObjects && !isCanvas()) {
RenderObject* r;
QPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
int rp = r->xPos() + r->rightmostPosition(false);
right = kMax(right, rp);
}
}
if (!includeSelf && firstLineBox()) {
for (InlineRunBox* currBox = firstLineBox(); currBox; currBox = currBox->nextLineBox()) {
int rp = currBox->xPos() + currBox->width();
right = kMax(right, rp);
}
}
return right;
}
int RenderBlock::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
int left = RenderFlow::leftmostPosition(includeOverflowInterior, includeSelf);
if (!includeOverflowInterior && style()->hidesOverflow())
return left;
if (m_floatingObjects) {
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (!r->noPaint) {
int lp = r->left + r->node->marginLeft() + r->node->leftmostPosition(false);
left = kMin(left, lp);
}
}
}
if (m_positionedObjects && !isCanvas()) {
RenderObject* r;
QPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
int lp = r->xPos() + r->leftmostPosition(false);
left = kMin(left, lp);
}
}
if (!includeSelf && firstLineBox()) {
for (InlineRunBox* currBox = firstLineBox(); currBox; currBox = currBox->nextLineBox())
left = kMin(left, (int)currBox->xPos());
}
return left;
}
int
RenderBlock::leftBottom()
{
if (!m_floatingObjects) return 0;
int bottom=0;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->endY>bottom && r->type == FloatingObject::FloatLeft)
bottom=r->endY;
return bottom;
}
int
RenderBlock::rightBottom()
{
if (!m_floatingObjects) return 0;
int bottom=0;
FloatingObject* r;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->endY>bottom && r->type == FloatingObject::FloatRight)
bottom=r->endY;
return bottom;
}
void
RenderBlock::clearFloats()
{
if (m_floatingObjects)
m_floatingObjects->clear();
if (avoidsFloats() || isRoot() || isCanvas() || isFloatingOrPositioned() || isTableCell())
return;
bool parentHasFloats = false;
RenderObject *prev = previousSibling();
while (prev && (!prev->isRenderBlock() || prev->avoidsFloats() || prev->isFloatingOrPositioned())) {
if (prev->isFloating())
parentHasFloats = true;
prev = prev->previousSibling();
}
int offset = m_y;
if (parentHasFloats)
addOverHangingFloats( static_cast<RenderBlock *>( parent() ),
parent()->borderLeft() + parent()->paddingLeft(), offset, false );
int xoffset = 0;
if (prev)
offset -= prev->yPos();
else {
prev = parent();
xoffset += prev->borderLeft() + prev->paddingLeft();
}
if (!prev->isRenderBlock()) return;
RenderBlock* block = static_cast<RenderBlock *>(prev);
if (!block->m_floatingObjects) return;
if (block->floatBottom() > offset)
addOverHangingFloats(block, xoffset, offset);
}
void RenderBlock::addOverHangingFloats( RenderBlock *flow, int xoff, int offset, bool child )
{
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << (void *)this << ": adding overhanging floats xoff=" << xoff << " offset=" << offset << " child=" << child << endl;
#endif
if ( !flow->m_floatingObjects || (child && flow->isRoot()) )
return;
if (!m_floatingObjects) {
m_floatingObjects = new QPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
}
QPtrListIterator<FloatingObject> it(*flow->m_floatingObjects);
FloatingObject *r;
for ( ; (r = it.current()); ++it ) {
if ( ( !child && r->endY > offset ) ||
( child && flow->yPos() + r->endY > height() ) ) {
if (child && (flow->enclosingLayer() == enclosingLayer()))
r->noPaint = true;
FloatingObject* f = 0;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
while ( (f = it.current()) ) {
if (f->node == r->node) break;
++it;
}
if ( !f ) {
FloatingObject *floatingObj = new FloatingObject(r->type);
floatingObj->startY = r->startY - offset;
floatingObj->endY = r->endY - offset;
floatingObj->left = r->left - xoff;
if (!child && flow != parent())
floatingObj->left += flow->marginLeft();
if ( !child ) {
floatingObj->left -= marginLeft();
floatingObj->noPaint = true;
}
else
floatingObj->noPaint = !r->noPaint;
floatingObj->width = r->width;
floatingObj->node = r->node;
m_floatingObjects->append(floatingObj);
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << "addOverHangingFloats x/y= (" << floatingObj->left << "/" << floatingObj->startY << "-" << floatingObj->width << "/" << floatingObj->endY - floatingObj->startY << ")" << endl;
#endif
}
}
}
}
bool RenderBlock::containsFloat(RenderObject* o)
{
if (m_floatingObjects) {
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->node == o)
return true;
++it;
}
}
return false;
}
void RenderBlock::markAllDescendantsWithFloatsForLayout(RenderObject* floatToRemove)
{
setNeedsLayout(true);
if (floatToRemove)
removeFloatingObject(floatToRemove);
if (!childrenInline()) {
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (isBlockFlow() && !child->isFloatingOrPositioned() &&
(floatToRemove ? child->containsFloat(floatToRemove) : child->containsFloats()))
child->markAllDescendantsWithFloatsForLayout(floatToRemove);
}
}
}
int RenderBlock::getClearDelta(RenderObject *child)
{
int bottom = 0;
switch(child->style()->clear())
{
case CNONE:
return 0;
case CLEFT:
bottom = leftBottom();
break;
case CRIGHT:
bottom = rightBottom();
break;
case CBOTH:
bottom = floatBottom();
break;
}
return QMAX(0, bottom-(child->yPos()));
}
bool RenderBlock::isPointInScrollbar(int _x, int _y, int _tx, int _ty)
{
if (!style()->scrollsOverflow() || !m_layer)
return false;
if (m_layer->verticalScrollbarWidth()) {
QRect vertRect(_tx + width() - borderRight() - m_layer->verticalScrollbarWidth(),
_ty + borderTop(),
m_layer->verticalScrollbarWidth(),
height()-borderTop()-borderBottom());
if (vertRect.contains(_x, _y)) {
RenderLayer::gScrollBar = m_layer->verticalScrollbar();
return true;
}
}
if (m_layer->horizontalScrollbarHeight()) {
QRect horizRect(_tx + borderLeft(),
_ty + height() - borderBottom() - m_layer->horizontalScrollbarHeight(),
width()-borderLeft()-borderRight(),
m_layer->horizontalScrollbarHeight());
if (horizRect.contains(_x, _y)) {
RenderLayer::gScrollBar = m_layer->horizontalScrollbar();
return true;
}
}
return false;
}
bool RenderBlock::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty,
HitTestAction hitTestAction, bool inBox)
{
bool inScrollbar = isPointInScrollbar(_x, _y, _tx+xPos(), _ty+yPos());
if (inScrollbar && hitTestAction != HitTestChildrenOnly)
inBox = true;
if (hitTestAction != HitTestSelfOnly && m_floatingObjects && !inScrollbar) {
int stx = _tx + xPos();
int sty = _ty + yPos();
if (style()->hidesOverflow() && m_layer)
m_layer->subtractScrollOffset(stx, sty);
if (isCanvas()) {
stx += static_cast<RenderCanvas*>(this)->view()->contentsX();
sty += static_cast<RenderCanvas*>(this)->view()->contentsY();
}
FloatingObject* o;
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (it.toLast(); (o = it.current()); --it)
if (!o->noPaint && !o->node->layer())
inBox |= o->node->nodeAtPoint(info, _x, _y,
stx+o->left + o->node->marginLeft() - o->node->xPos(),
sty+o->startY + o->node->marginTop() - o->node->yPos());
}
inBox |= RenderFlow::nodeAtPoint(info, _x, _y, _tx, _ty, hitTestAction, inBox);
return inBox;
}
void RenderBlock::calcMinMaxWidth()
{
KHTMLAssert( !minMaxKnown() );
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << renderName() << "(RenderBlock)::calcMinMaxWidth() this=" << this << endl;
#endif
m_minWidth = 0;
m_maxWidth = 0;
bool preOrNowrap = style()->whiteSpace() != NORMAL;
if (childrenInline())
calcInlineMinMaxWidth();
else
calcBlockMinMaxWidth();
if(m_maxWidth < m_minWidth) m_maxWidth = m_minWidth;
if (preOrNowrap && childrenInline()) {
m_minWidth = m_maxWidth;
if (style()->overflow() == OMARQUEE && m_layer && m_layer->marquee() &&
m_layer->marquee()->isHorizontal() && !m_layer->marquee()->isUnfurlMarquee())
m_minWidth = 0;
}
if (style()->width().isFixed() && style()->width().value > 0) {
if (isTableCell())
m_maxWidth = KMAX(m_minWidth,short(style()->width().value));
else
m_minWidth = m_maxWidth = short(style()->width().value);
}
if (style()->minWidth().isFixed() && style()->minWidth().value > 0) {
m_maxWidth = KMAX(m_maxWidth, short(style()->minWidth().value));
m_minWidth = KMAX(m_minWidth, short(style()->minWidth().value));
}
if (style()->maxWidth().isFixed() && style()->maxWidth().value != UNDEFINED) {
m_maxWidth = KMIN(m_maxWidth, short(style()->maxWidth().value));
m_minWidth = KMIN(m_minWidth, short(style()->maxWidth().value));
}
int toAdd = 0;
toAdd = borderLeft() + borderRight() + paddingLeft() + paddingRight();
m_minWidth += toAdd;
m_maxWidth += toAdd;
setMinMaxKnown();
}
struct InlineMinMaxIterator
{
RenderObject* parent;
RenderObject* current;
bool endOfInline;
InlineMinMaxIterator(RenderObject* p, RenderObject* o, bool end = false)
:parent(p), current(o), endOfInline(end) {}
RenderObject* next();
};
RenderObject* InlineMinMaxIterator::next()
{
RenderObject* result = 0;
bool oldEndOfInline = endOfInline;
endOfInline = false;
while (current != 0 || (current == parent))
{
if (!oldEndOfInline &&
(current == parent ||
(!current->isFloating() && !current->isReplaced() && !current->isPositioned())))
result = current->firstChild();
if (!result) {
if (!oldEndOfInline && current->isInlineFlow()) {
result = current;
endOfInline = true;
break;
}
while (current && current != parent) {
result = current->nextSibling();
if (result) break;
current = current->parent();
if (current && current != parent && current->isInlineFlow()) {
result = current;
endOfInline = true;
break;
}
}
}
if (!result) break;
if (result->isText() || result->isBR() ||
result->isFloating() || result->isReplaced() ||
result->isInlineFlow())
break;
current = result;
result = 0;
}
current = result;
return current;
}
static int getBPMWidth(int childValue, Length cssUnit)
{
if (cssUnit.type != Variable)
return (cssUnit.type == Fixed ? cssUnit.value : childValue);
return 0;
}
static int getBorderPaddingMargin(RenderObject* child, bool endOfInline)
{
RenderStyle* cstyle = child->style();
int result = 0;
bool leftSide = (cstyle->direction() == LTR) ? !endOfInline : endOfInline;
result += getBPMWidth((leftSide ? child->marginLeft() : child->marginRight()),
(leftSide ? cstyle->marginLeft() :
cstyle->marginRight()));
result += getBPMWidth((leftSide ? child->paddingLeft() : child->paddingRight()),
(leftSide ? cstyle->paddingLeft() :
cstyle->paddingRight()));
result += leftSide ? child->borderLeft() : child->borderRight();
return result;
}
static void stripTrailingSpace(bool pre,
int& inlineMax, int& inlineMin,
RenderObject* trailingSpaceChild)
{
if (!pre && trailingSpaceChild && trailingSpaceChild->isText()) {
RenderText* t = static_cast<RenderText *>(trailingSpaceChild);
const Font *f = t->htmlFont( false );
QChar space[1]; space[0] = ' ';
int spaceWidth = f->width(space, 1, 0);
inlineMax -= spaceWidth;
if (inlineMin > inlineMax)
inlineMin = inlineMax;
}
}
void RenderBlock::calcInlineMinMaxWidth()
{
int inlineMax=0;
int inlineMin=0;
int cw = containingBlock()->contentWidth();
bool stripFrontSpaces = true;
RenderObject* trailingSpaceChild = 0;
bool normal, oldnormal;
normal = oldnormal = style()->whiteSpace() == NORMAL;
InlineMinMaxIterator childIterator(this, this);
bool addedTextIndent = false; while (RenderObject* child = childIterator.next())
{
normal = child->style()->whiteSpace() == NORMAL;
if( !child->isBR() )
{
RenderStyle* cstyle = child->style();
short childMin = 0;
short childMax = 0;
if (!child->isText()) {
if (child->isInlineFlow()) {
int bpm = getBorderPaddingMargin(child, childIterator.endOfInline);
childMin += bpm;
childMax += bpm;
inlineMin += childMin;
inlineMax += childMax;
}
else {
int margins = 0;
LengthType type = cstyle->marginLeft().type;
if ( type != Variable )
margins += (type == Fixed ? cstyle->marginLeft().value : child->marginLeft());
type = cstyle->marginRight().type;
if ( type != Variable )
margins += (type == Fixed ? cstyle->marginRight().value : child->marginRight());
childMin += margins;
childMax += margins;
}
}
if (!child->isRenderInline() && !child->isText()) {
childMin += child->minWidth();
childMax += child->maxWidth();
if (normal || oldnormal) {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = 0;
}
int ti = 0;
if (!addedTextIndent) {
addedTextIndent = true;
ti = style()->textIndent().minWidth(cw);
childMin+=ti;
childMax+=ti;
}
inlineMax += childMax;
if (!normal)
inlineMin += childMin;
else {
inlineMin = childMin;
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = 0;
}
if (!child->isFloating()) {
stripFrontSpaces = false;
trailingSpaceChild = 0;
}
}
else if (child->isText())
{
RenderText* t = static_cast<RenderText *>(child);
bool hasBreakableChar, hasBreak;
short beginMin, endMin;
bool beginWS, endWS;
short beginMax, endMax;
t->trimmedMinMaxWidth(beginMin, beginWS, endMin, endWS, hasBreakableChar,
hasBreak, beginMax, endMax,
childMin, childMax, stripFrontSpaces);
if (!hasBreak && childMax == 0) continue;
if (stripFrontSpaces)
trailingSpaceChild = child;
else
trailingSpaceChild = 0;
int ti = 0;
if (!addedTextIndent) {
addedTextIndent = true;
ti = style()->textIndent().minWidth(cw);
childMin+=ti; beginMin += ti;
childMax+=ti; beginMax += ti;
}
if (!hasBreakableChar) {
inlineMin += childMin;
}
else {
if (beginWS) {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
}
else {
inlineMin += beginMin;
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
childMin -= ti;
}
inlineMin = childMin;
if (endWS) {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = 0;
}
else {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = endMin;
}
}
if (hasBreak) {
inlineMax += beginMax;
if (m_maxWidth < inlineMax) m_maxWidth = inlineMax;
if (m_maxWidth < childMax) m_maxWidth = childMax;
inlineMax = endMax;
}
else
inlineMax += childMax;
}
}
else
{
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
if(m_maxWidth < inlineMax) m_maxWidth = inlineMax;
inlineMin = inlineMax = 0;
stripFrontSpaces = true;
trailingSpaceChild = 0;
}
oldnormal = normal;
}
stripTrailingSpace(m_pre, inlineMax, inlineMin, trailingSpaceChild);
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
if(m_maxWidth < inlineMax) m_maxWidth = inlineMax;
}
#define BLOCK_MAX_WIDTH 15000
void RenderBlock::calcBlockMinMaxWidth()
{
bool nowrap = style()->whiteSpace() == NOWRAP;
RenderObject *child = firstChild();
while(child != 0)
{
if (child->isPositioned()) {
child = child->nextSibling();
continue;
}
Length ml = child->style()->marginLeft();
Length mr = child->style()->marginRight();
if (ml.type == Percent || mr.type == Percent)
calcWidth();
int margin = 0;
if (ml.type == Fixed)
margin += ml.value;
else if (ml.type == Percent)
margin += child->marginLeft();
if (mr.type == Fixed)
margin += mr.value;
else if (mr.type == Percent)
margin += child->marginRight();
if (margin < 0) margin = 0;
int w = child->minWidth() + margin;
if(m_minWidth < w) m_minWidth = w;
if ( nowrap && !child->isTable() && m_maxWidth < w )
m_maxWidth = w;
w = child->maxWidth() + margin;
if(m_maxWidth < w) m_maxWidth = w;
if (style()->htmlHacks() && child->style()->width().type == Percent &&
!isTableCell() && child->isTable() && m_maxWidth < BLOCK_MAX_WIDTH) {
RenderBlock* cb = containingBlock();
while (!cb->isCanvas() && !cb->isTableCell())
cb = cb->containingBlock();
if (!cb->isTableCell())
m_maxWidth = BLOCK_MAX_WIDTH;
}
child = child->nextSibling();
}
}
short RenderBlock::lineHeight(bool b, bool isRootLineBox) const
{
if (isReplaced() && !isRootLineBox)
return height()+marginTop()+marginBottom();
return RenderFlow::lineHeight(b, isRootLineBox);
}
short RenderBlock::baselinePosition(bool b, bool isRootLineBox) const
{
if (isReplaced() && !isRootLineBox)
return height()+marginTop()+marginBottom();
return RenderFlow::baselinePosition(b, isRootLineBox);
}
int RenderBlock::getBaselineOfFirstLineBox()
{
if (m_firstLineBox)
return m_firstLineBox->yPos() + m_firstLineBox->baseline();
if (isInline())
return -1;
for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
int result = curr->getBaselineOfFirstLineBox();
if (result != -1)
return curr->yPos() + result; }
return -1;
}
InlineFlowBox* RenderBlock::getFirstLineBox()
{
if (m_firstLineBox)
return m_firstLineBox;
if (isInline())
return 0;
for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
InlineFlowBox* result = curr->getFirstLineBox();
if (result)
return result;
}
return 0;
}
RenderBlock* RenderBlock::firstLineBlock() const
{
const RenderObject* firstLineBlock = this;
bool hasPseudo = false;
while (true) {
hasPseudo = firstLineBlock->style()->hasPseudoStyle(RenderStyle::FIRST_LINE);
if (hasPseudo)
break;
RenderObject* parentBlock = firstLineBlock->parent();
if (firstLineBlock->isReplaced() || !parentBlock || parentBlock->firstChild() != firstLineBlock ||
!parentBlock->isBlockFlow())
break;
firstLineBlock = parentBlock;
}
if (!hasPseudo)
return 0;
return (RenderBlock*)(firstLineBlock);
}
void RenderBlock::updateFirstLetter()
{
RenderObject* firstLetterBlock = this;
bool hasPseudoStyle = false;
while (true) {
hasPseudoStyle = firstLetterBlock->style()->hasPseudoStyle(RenderStyle::FIRST_LETTER);
if (hasPseudoStyle)
break;
RenderObject* parentBlock = firstLetterBlock->parent();
if (firstLetterBlock->isReplaced() || !parentBlock || parentBlock->firstChild() != firstLetterBlock ||
!parentBlock->isBlockFlow())
break;
firstLetterBlock = parentBlock;
}
if (!hasPseudoStyle)
return;
RenderObject* currChild = firstLetterBlock->firstChild();
while (currChild && currChild->needsLayout() && !currChild->isReplaced() && !currChild->isText())
currChild = currChild->firstChild();
if (currChild && currChild->isText() && !currChild->isBR() &&
currChild->parent()->style()->styleType() != RenderStyle::FIRST_LETTER) {
RenderObject* firstLetterContainer = currChild->parent();
if (!firstLetterContainer)
firstLetterContainer = this;
RenderText* textObj = static_cast<RenderText*>(currChild);
RenderStyle* pseudoStyle = firstLetterBlock->getPseudoStyle(RenderStyle::FIRST_LETTER,
firstLetterContainer->style(true));
pseudoStyle->setDisplay( pseudoStyle->isFloating() ? BLOCK : INLINE);
pseudoStyle->setPosition( STATIC );
RenderObject* firstLetter = RenderFlow::createAnonymousFlow(document(), pseudoStyle); firstLetterContainer->addChild(firstLetter, firstLetterContainer->firstChild());
DOMStringImpl* oldText = textObj->originalString();
if (oldText->l >= 1) {
unsigned int length = 0;
while ( length < oldText->l &&
( (oldText->s+length)->isSpace() || (oldText->s+length)->isPunct() ) )
length++;
length++;
RenderTextFragment* remainingText =
new (renderArena()) RenderTextFragment(textObj->node(), oldText, length, oldText->l-length);
remainingText->setStyle(textObj->style());
if (remainingText->element())
remainingText->element()->setRenderer(remainingText);
RenderObject* nextObj = textObj->nextSibling();
firstLetterContainer->removeChild(textObj);
firstLetterContainer->addChild(remainingText, nextObj);
RenderTextFragment* letter =
new (renderArena()) RenderTextFragment(remainingText->node(), oldText, 0, length);
RenderStyle* newStyle = new RenderStyle();
newStyle->inheritFrom(pseudoStyle);
letter->setStyle(newStyle);
firstLetter->addChild(letter);
}
}
}
bool RenderBlock::inRootBlockContext() const
{
if (isTableCell() || isFloatingOrPositioned() || style()->hidesOverflow())
return false;
if (isRoot() || isCanvas())
return true;
return containingBlock()->inRootBlockContext();
}
const char *RenderBlock::renderName() const
{
if (isBody())
return "RenderBody";
if (isFloating())
return "RenderBlock (floating)";
if (isPositioned())
return "RenderBlock (positioned)";
if (isAnonymousBlock())
return "RenderBlock (anonymous)";
else if (isAnonymous())
return "RenderBlock (generated)";
if (isRelPositioned())
return "RenderBlock (relative positioned)";
if (isCompact())
return "RenderBlock (compact)";
if (isRunIn())
return "RenderBlock (run-in)";
return "RenderBlock";
}
#ifndef NDEBUG
void RenderBlock::printTree(int indent) const
{
RenderFlow::printTree(indent);
if (m_floatingObjects)
{
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
FloatingObject *r;
for ( ; (r = it.current()); ++it )
{
QString s;
s.fill(' ', indent);
kdDebug() << s << renderName() << ": " <<
(r->type == FloatingObject::FloatLeft ? "FloatLeft" : "FloatRight" ) <<
"[" << r->node->renderName() << ": " << (void*)r->node << "] (" << r->startY << " - " << r->endY << ")" << "width: " << r->width <<
endl;
}
}
}
void RenderBlock::dump(QTextStream *stream, QString ind) const
{
if (m_childrenInline) { *stream << " childrenInline"; }
if (m_pre) { *stream << " pre"; }
if (m_firstLine) { *stream << " firstLine"; }
if (m_floatingObjects && !m_floatingObjects->isEmpty())
{
*stream << " special(";
QPtrListIterator<FloatingObject> it(*m_floatingObjects);
FloatingObject *r;
bool first = true;
for ( ; (r = it.current()); ++it )
{
if (!first)
*stream << ",";
*stream << r->node->renderName();
first = false;
}
*stream << ")";
}
RenderFlow::dump(stream,ind);
}
#endif
#undef DEBUG
#undef DEBUG_LAYOUT
#undef BOX_DEBUG
}