#include "config.h"
#include "RenderFlow.h"
#include "Document.h"
#include "GraphicsContext.h"
#include "InlineTextBox.h"
#include "HTMLNames.h"
#include "RenderArena.h"
#include "RenderView.h"
#include "RenderInline.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderFlow* RenderFlow::createAnonymousFlow(Document* doc, RenderStyle* style)
{
RenderFlow* result;
if (style->display() == INLINE)
result = new (doc->renderArena()) RenderInline(doc);
else
result = new (doc->renderArena()) RenderBlock(doc);
result->setStyle(style);
return result;
}
RenderFlow* RenderFlow::continuationBefore(RenderObject* beforeChild)
{
if (beforeChild && beforeChild->parent() == this)
return this;
RenderFlow* curr = continuation();
RenderFlow* nextToLast = this;
RenderFlow* last = this;
while (curr) {
if (beforeChild && beforeChild->parent() == curr) {
if (curr->firstChild() == beforeChild)
return last;
return curr;
}
nextToLast = last;
last = curr;
curr = curr->continuation();
}
if (!beforeChild && !last->firstChild())
return nextToLast;
return last;
}
void RenderFlow::addChildWithContinuation(RenderObject* newChild, RenderObject* beforeChild)
{
RenderFlow* flow = continuationBefore(beforeChild);
ASSERT(!beforeChild || beforeChild->parent()->isRenderBlock() ||
beforeChild->parent()->isRenderInline());
RenderFlow* beforeChildParent = beforeChild ? static_cast<RenderFlow*>(beforeChild->parent()) :
(flow->continuation() ? flow->continuation() : flow);
if (newChild->isFloatingOrPositioned())
return beforeChildParent->addChildToFlow(newChild, beforeChild);
bool childInline = newChild->isInline();
bool bcpInline = beforeChildParent->isInline();
bool flowInline = flow->isInline();
if (flow == beforeChildParent)
return flow->addChildToFlow(newChild, beforeChild);
else {
if (childInline == bcpInline)
return beforeChildParent->addChildToFlow(newChild, beforeChild);
else if (flowInline == childInline)
return flow->addChildToFlow(newChild, 0); else
return beforeChildParent->addChildToFlow(newChild, beforeChild);
}
}
void RenderFlow::addChild(RenderObject *newChild, RenderObject *beforeChild)
{
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << renderName() << "(RenderFlow)::addChild( " << newChild->renderName() <<
", " << (beforeChild ? beforeChild->renderName() : "0") << " )" << endl;
kdDebug( 6040 ) << "current height = " << m_height << endl;
#endif
if (continuation())
return addChildWithContinuation(newChild, beforeChild);
return addChildToFlow(newChild, beforeChild);
}
void RenderFlow::extractLineBox(InlineFlowBox* box)
{
m_lastLineBox = box->prevFlowBox();
if (box == m_firstLineBox)
m_firstLineBox = 0;
if (box->prevLineBox())
box->prevLineBox()->setNextLineBox(0);
box->setPreviousLineBox(0);
for (InlineRunBox* curr = box; curr; curr = curr->nextLineBox())
curr->setExtracted();
}
void RenderFlow::attachLineBox(InlineFlowBox* box)
{
if (m_lastLineBox) {
m_lastLineBox->setNextLineBox(box);
box->setPreviousLineBox(m_lastLineBox);
}
else
m_firstLineBox = box;
InlineFlowBox* last = box;
for (InlineFlowBox* curr = box; curr; curr = curr->nextFlowBox()) {
curr->setExtracted(false);
last = curr;
}
m_lastLineBox = last;
}
void RenderFlow::removeLineBox(InlineFlowBox* box)
{
if (box == m_firstLineBox)
m_firstLineBox = box->nextFlowBox();
if (box == m_lastLineBox)
m_lastLineBox = box->prevFlowBox();
if (box->nextLineBox())
box->nextLineBox()->setPreviousLineBox(box->prevLineBox());
if (box->prevLineBox())
box->prevLineBox()->setNextLineBox(box->nextLineBox());
}
void RenderFlow::deleteLineBoxes()
{
if (m_firstLineBox) {
RenderArena* arena = renderArena();
InlineRunBox *curr=m_firstLineBox, *next=0;
while (curr) {
next = curr->nextLineBox();
curr->destroy(arena);
curr = next;
}
m_firstLineBox = 0;
m_lastLineBox = 0;
}
}
void RenderFlow::destroy()
{
if (m_continuation)
m_continuation->destroy();
m_continuation = 0;
RenderContainer::destroyLeftoverChildren();
if (!documentBeingDestroyed()) {
if (m_firstLineBox) {
if (isSelectionBorder())
view()->clearSelection();
if (m_firstLineBox->parent()) {
for (InlineRunBox* box = m_firstLineBox; box; box = box->nextLineBox())
box->remove();
}
if (isAnonymousBlock()) {
for (InlineFlowBox* box = m_firstLineBox; box; box = box->nextFlowBox()) {
while (InlineBox *childBox = box->firstChild()) {
childBox->remove();
}
}
}
}
else if (isInline() && parent())
parent()->dirtyLinesFromChangedChild(this);
}
deleteLineBoxes();
RenderContainer::destroy();
}
void RenderFlow::dirtyLinesFromChangedChild(RenderObject* child)
{
if (!parent() || (selfNeedsLayout() && !isInlineFlow()) || isTable())
return;
if (isInline() && !firstLineBox())
return parent()->dirtyLinesFromChangedChild(this);
RootInlineBox* box = 0;
RenderObject* curr = 0;
for (curr = child->previousSibling(); curr; curr = curr->previousSibling()) {
if (curr->isFloatingOrPositioned())
continue;
if (curr->isReplaced()) {
InlineBox* wrapper = curr->inlineBoxWrapper();
if (wrapper)
box = wrapper->root();
}
else if (curr->isText()) {
InlineTextBox* textBox = static_cast<RenderText*>(curr)->lastTextBox();
if (textBox)
box = textBox->root();
}
else if (curr->isInlineFlow()) {
InlineRunBox* runBox = static_cast<RenderFlow*>(curr)->lastLineBox();
if (runBox)
box = runBox->root();
}
if (box)
break;
}
if (!box && firstLineBox())
box = firstLineBox()->root();
if (box) {
RootInlineBox* adjacentBox;
box->markDirty();
adjacentBox = box->prevRootBox();
if (adjacentBox)
adjacentBox->markDirty();
if (child->isBR() || (curr && curr->isBR())) {
adjacentBox = box->nextRootBox();
if (adjacentBox)
adjacentBox->markDirty();
}
}
}
short RenderFlow::lineHeight(bool firstLine, bool isRootLineBox) const
{
if (firstLine) {
RenderStyle* s = style(firstLine);
Length lh = s->lineHeight();
if (lh.value() < 0) {
if (s == style()) {
if (m_lineHeight == -1)
m_lineHeight = RenderObject::lineHeight(false);
return m_lineHeight;
}
return s->font().lineSpacing();
}
if (lh.isPercent())
return lh.calcMinValue(s->fontSize());
return lh.value();
}
if (m_lineHeight == -1)
m_lineHeight = RenderObject::lineHeight(false);
return m_lineHeight;
}
void RenderFlow::dirtyLineBoxes(bool fullLayout, bool isRootLineBox)
{
if (!isRootLineBox && isReplaced())
return RenderContainer::dirtyLineBoxes(fullLayout, isRootLineBox);
if (fullLayout)
deleteLineBoxes();
else {
for (InlineRunBox* curr = firstLineBox(); curr; curr = curr->nextLineBox())
curr->dirtyLineBoxes();
}
}
InlineBox* RenderFlow::createInlineBox(bool makePlaceHolderBox, bool isRootLineBox, bool isOnlyRun)
{
if (!isRootLineBox &&
(isReplaced() || makePlaceHolderBox)) return RenderContainer::createInlineBox(false, isRootLineBox);
InlineFlowBox* flowBox = 0;
if (isInlineFlow())
flowBox = new (renderArena()) InlineFlowBox(this);
else
flowBox = new (renderArena()) RootInlineBox(this);
if (!m_firstLineBox)
m_firstLineBox = m_lastLineBox = flowBox;
else {
m_lastLineBox->setNextLineBox(flowBox);
flowBox->setPreviousLineBox(m_lastLineBox);
m_lastLineBox = flowBox;
}
return flowBox;
}
void RenderFlow::paintLines(PaintInfo& i, int _tx, int _ty)
{
if (i.phase != PaintPhaseForeground && i.phase != PaintPhaseSelection && i.phase != PaintPhaseOutline
&& i.phase != PaintPhaseSelfOutline && i.phase != PaintPhaseChildOutlines)
return;
bool inlineFlow = isInlineFlow();
if (inlineFlow)
ASSERT(m_layer);
if (!firstLineBox())
return;
int yPos = firstLineBox()->root()->topOverflow() - maximalOutlineSize(i.phase);
int h = maximalOutlineSize(i.phase) + lastLineBox()->root()->bottomOverflow() - yPos;
yPos += _ty;
if (yPos >= i.r.bottom() || yPos + h <= i.r.y())
return;
PaintInfo info(i);
RenderFlowSequencedSet outlineObjects;
info.outlineObjects = &outlineObjects;
bool isPrinting = document()->printing();
for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextFlowBox()) {
if (isPrinting) {
RenderView* c = view();
if (curr->root()->bottomOverflow() - curr->root()->topOverflow() <= c->printRect().height()) {
if (_ty + curr->root()->bottomOverflow() > c->printRect().bottom()) {
if (_ty + curr->root()->topOverflow() < c->truncatedAt())
c->setBestTruncatedAt(_ty + curr->root()->topOverflow(), this);
if (_ty + curr->root()->topOverflow() >= c->truncatedAt())
break;
}
}
}
int top = min(curr->root()->topOverflow(), curr->root()->selectionTop()) - maximalOutlineSize(info.phase);
int bottom = curr->root()->bottomOverflow() + maximalOutlineSize(info.phase);
h = bottom - top;
yPos = _ty + top;
if (yPos < info.r.bottom() && yPos + h > info.r.y())
curr->paint(info, _tx, _ty);
}
if (info.phase == PaintPhaseOutline || info.phase == PaintPhaseSelfOutline || info.phase == PaintPhaseChildOutlines) {
RenderFlowSequencedSet::iterator end = info.outlineObjects->end();
for (RenderFlowSequencedSet::iterator it = info.outlineObjects->begin(); it != end; ++it) {
RenderFlow* flow = *it;
if (flow->style()->outlineStyleIsAuto())
flow->paintFocusRing(info.p, _tx, _ty);
else
flow->paintOutlines(info.p, _tx, _ty);
}
info.outlineObjects->clear();
}
}
bool RenderFlow::hitTestLines(NodeInfo& i, int x, int y, int tx, int ty, HitTestAction hitTestAction)
{
if (hitTestAction != HitTestForeground)
return false;
bool inlineFlow = isInlineFlow();
if (inlineFlow)
ASSERT(m_layer);
if (!firstLineBox())
return false;
if ((y >= ty + lastLineBox()->root()->bottomOverflow()) || (y < ty + firstLineBox()->root()->topOverflow()))
return false;
for (InlineFlowBox* curr = lastLineBox(); curr; curr = curr->prevFlowBox()) {
if (y >= ty + curr->root()->topOverflow() && y < ty + curr->root()->bottomOverflow()) {
bool inside = curr->nodeAtPoint(i, x, y, tx, ty);
if (inside) {
setInnerNode(i);
return true;
}
}
}
return false;
}
IntRect RenderFlow::getAbsoluteRepaintRect()
{
if (isInlineFlow()) {
int left = 0;
int top = firstLineBox() ? firstLineBox()->yPos() : 0;
for (InlineRunBox* curr = firstLineBox(); curr; curr = curr->nextLineBox())
if (curr == firstLineBox() || curr->xPos() < left)
left = curr->xPos();
int ow = style() ? style()->outlineSize() : 0;
if (isCompact())
left -= m_x;
RenderBlock* cb = containingBlock();
for (RenderObject* inlineFlow = this; inlineFlow && inlineFlow->isInlineFlow() && inlineFlow != cb;
inlineFlow = inlineFlow->parent()) {
if (inlineFlow->style()->position() == RelativePosition && inlineFlow->layer())
inlineFlow->layer()->relativePositionOffset(left, top);
}
IntRect r(-ow+left, -ow+top, width()+ow*2, height()+ow*2);
if (cb->hasOverflowClip()) {
int x = r.x();
int y = r.y();
IntRect boxRect(0, 0, cb->layer()->width(), cb->layer()->height());
cb->layer()->subtractScrollOffset(x, y); IntRect repaintRect(x, y, r.width(), r.height());
r = intersection(repaintRect, boxRect);
}
cb->computeAbsoluteRepaintRect(r);
if (ow) {
for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
if (!curr->isText()) {
IntRect childRect = curr->getAbsoluteRepaintRectWithOutline(ow);
r.unite(childRect);
}
}
if (continuation() && !continuation()->isInline()) {
IntRect contRect = continuation()->getAbsoluteRepaintRectWithOutline(ow);
r.unite(contRect);
}
}
return r;
} else
return RenderContainer::getAbsoluteRepaintRect();
}
int
RenderFlow::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
{
assert(!isInlineFlow());
int bottom = includeSelf && m_width > 0 ? m_height : 0;
if (!includeOverflowInterior && hasOverflowClip())
return bottom;
for (RenderObject *c = firstChild(); c; c = c->nextSibling()) {
if (!c->isFloatingOrPositioned() && !c->isText() && !c->isInlineFlow()) {
int lp = c->yPos() + c->lowestPosition(false);
bottom = max(bottom, lp);
}
}
if (isRelPositioned())
bottom += relativePositionOffsetY();
return bottom;
}
int RenderFlow::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
assert(!isInlineFlow());
int right = includeSelf && m_height > 0 ? m_width : 0;
if (!includeOverflowInterior && hasOverflowClip())
return right;
for (RenderObject *c = firstChild(); c; c = c->nextSibling()) {
if (!c->isFloatingOrPositioned() && !c->isText() && !c->isInlineFlow()) {
int rp = c->xPos() + c->rightmostPosition(false);
right = max(right, rp);
}
}
if (isRelPositioned())
right += relativePositionOffsetX();
return right;
}
int RenderFlow::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
assert(!isInlineFlow());
int left = includeSelf && m_height > 0 ? 0 : m_width;
if (!includeOverflowInterior && hasOverflowClip())
return left;
for (RenderObject *c = firstChild(); c; c = c->nextSibling()) {
if (!c->isFloatingOrPositioned() && !c->isText() && !c->isInlineFlow()) {
int lp = c->xPos() + c->leftmostPosition(false);
left = min(left, lp);
}
}
if (isRelPositioned())
left += relativePositionOffsetX();
return left;
}
IntRect RenderFlow::caretRect(int offset, EAffinity affinity, int *extraWidthToEndOfLine)
{
if (firstChild() || style()->display() == INLINE)
return RenderContainer::caretRect(offset, affinity, extraWidthToEndOfLine);
RenderStyle *currentStyle = firstLineStyle();
int height = lineHeight(true);
const int caretWidth = 3;
enum CaretAlignment { alignLeft, alignRight, alignCenter };
CaretAlignment alignment = alignLeft;
switch (currentStyle->textAlign()) {
case TAAUTO:
case JUSTIFY:
if (currentStyle->direction() == RTL)
alignment = alignRight;
break;
case LEFT:
case WEBKIT_LEFT:
break;
case CENTER:
case WEBKIT_CENTER:
alignment = alignCenter;
break;
case RIGHT:
case WEBKIT_RIGHT:
alignment = alignRight;
break;
}
int x = borderLeft() + paddingLeft();
int w = width();
switch (alignment) {
case alignLeft:
break;
case alignCenter:
x = (x + w - (borderRight() + paddingRight())) / 2;
break;
case alignRight:
x = w - (borderRight() + paddingRight());
break;
}
if (extraWidthToEndOfLine) {
if (isRenderBlock()) {
*extraWidthToEndOfLine = w - (x + caretWidth);
} else {
int myRight = x + caretWidth;
int ignore;
absolutePositionForContent(myRight, ignore);
int containerRight = containingBlock()->xPos() + containingBlockWidth();
absolutePositionForContent(containerRight, ignore);
*extraWidthToEndOfLine = containerRight - myRight;
}
}
int absx, absy;
absolutePositionForContent(absx, absy);
x += absx;
int y = absy + paddingTop() + borderTop();
return IntRect(x, y, caretWidth, height);
}
void RenderFlow::addFocusRingRects(GraphicsContext* p, int _tx, int _ty)
{
if (isRenderBlock())
p->addFocusRingRect(IntRect(_tx, _ty, width(), height()));
if (!hasOverflowClip()) {
for (InlineRunBox* curr = firstLineBox(); curr; curr = curr->nextLineBox())
p->addFocusRingRect(IntRect(_tx + curr->xPos(), _ty + curr->yPos(), curr->width(), curr->height()));
for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
if (!curr->isText() && !curr->isListMarker())
curr->addFocusRingRects(p, _tx + curr->xPos(), _ty + curr->yPos());
}
if (continuation())
continuation()->addFocusRingRects(p,
_tx - containingBlock()->xPos() + continuation()->xPos(),
_ty - containingBlock()->yPos() + continuation()->yPos());
}
void RenderFlow::paintFocusRing(GraphicsContext* p, int tx, int ty)
{
int ow = style()->outlineWidth();
Color oc = style()->outlineColor();
if (!oc.isValid())
oc = style()->color();
p->initFocusRing(ow, style()->outlineOffset());
addFocusRingRects(p, tx, ty);
p->drawFocusRing(oc);
p->clearFocusRing();
}
void RenderFlow::paintOutlines(GraphicsContext* p, int _tx, int _ty)
{
if (style()->outlineStyle() <= BHIDDEN)
return;
DeprecatedPtrList <IntRect> rects;
rects.setAutoDelete(true);
rects.append(new IntRect);
for (InlineRunBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) {
rects.append(new IntRect(curr->xPos(), curr->yPos(), curr->width(), curr->height()));
}
rects.append(new IntRect);
for (unsigned int i = 1; i < rects.count() - 1; i++)
paintOutlineForLine(p, _tx, _ty, *rects.at(i-1), *rects.at(i), *rects.at(i+1));
}
void RenderFlow::paintOutlineForLine(GraphicsContext* p, int tx, int ty, const IntRect &lastline, const IntRect &thisline, const IntRect &nextline)
{
int ow = style()->outlineWidth();
EBorderStyle os = style()->outlineStyle();
Color oc = style()->outlineColor();
if (!oc.isValid())
oc = style()->color();
int offset = style()->outlineOffset();
int t = ty + thisline.y() - offset;
int l = tx + thisline.x() - offset;
int b = ty + thisline.bottom() + offset;
int r = tx + thisline.right() + offset;
drawBorder(p,
l - ow,
t - (lastline.isEmpty() || thisline.x() < lastline.x() || (lastline.right() - 1) <= thisline.x() ? ow : 0),
l,
b + (nextline.isEmpty() || thisline.x() <= nextline.x() || (nextline.right() - 1) <= thisline.x() ? ow : 0),
BSLeft,
oc, style()->color(), os,
(lastline.isEmpty() || thisline.x() < lastline.x() || (lastline.right() - 1) <= thisline.x() ? ow : -ow),
(nextline.isEmpty() || thisline.x() <= nextline.x() || (nextline.right() - 1) <= thisline.x() ? ow : -ow),
true);
drawBorder(p,
r,
t - (lastline.isEmpty() || lastline.right() < thisline.right() || (thisline.right() - 1) <= lastline.x() ? ow : 0),
r + ow,
b + (nextline.isEmpty() || nextline.right() <= thisline.right() || (thisline.right() - 1) <= nextline.x() ? ow : 0),
BSRight,
oc, style()->color(), os,
(lastline.isEmpty() || lastline.right() < thisline.right() || (thisline.right() - 1) <= lastline.x() ? ow : -ow),
(nextline.isEmpty() || nextline.right() <= thisline.right() || (thisline.right() - 1) <= nextline.x() ? ow : -ow),
true);
if (thisline.x() < lastline.x())
drawBorder(p,
l - ow,
t - ow,
min(r+ow, (lastline.isEmpty() ? 1000000 : tx + lastline.x())),
t ,
BSTop, oc, style()->color(), os,
ow,
(!lastline.isEmpty() && tx + lastline.x() + 1 < r + ow) ? -ow : ow,
true);
if (lastline.right() < thisline.right())
drawBorder(p,
max(lastline.isEmpty() ? -1000000 : tx + lastline.right(), l - ow),
t - ow,
r + ow,
t ,
BSTop, oc, style()->color(), os,
(!lastline.isEmpty() && l - ow < tx + lastline.right()) ? -ow : ow,
ow,
true);
if (thisline.x() < nextline.x())
drawBorder(p,
l - ow,
b,
min(r + ow, !nextline.isEmpty() ? tx + nextline.x() + 1 : 1000000),
b + ow,
BSBottom, oc, style()->color(), os,
ow,
(!nextline.isEmpty() && tx + nextline.x() + 1 < r + ow) ? -ow : ow,
true);
if (nextline.right() < thisline.right())
drawBorder(p,
max(!nextline.isEmpty() ? tx + nextline.right() : -1000000, l - ow),
b,
r + ow,
b + ow,
BSBottom, oc, style()->color(), os,
(!nextline.isEmpty() && l - ow < tx + nextline.right()) ? -ow : ow,
ow,
true);
}
}