#include <kglobal.h>
#include "render_arena.h"
#include "render_inline.h"
#include "render_block.h"
#include "xml/dom_nodeimpl.h"
using namespace khtml;
RenderInline::RenderInline(DOM::NodeImpl* node)
:RenderFlow(node)
{}
RenderInline::~RenderInline()
{}
void RenderInline::setStyle(RenderStyle* _style)
{
RenderFlow::setStyle(_style);
setInline(true);
RenderFlow* currCont = continuation();
while (currCont) {
if (currCont->isInline()) {
RenderFlow* nextCont = currCont->continuation();
currCont->setContinuation(0);
currCont->setStyle(style());
currCont->setContinuation(nextCont);
}
currCont = currCont->continuation();
}
updatePseudoChild(RenderStyle::BEFORE, firstChild());
updatePseudoChild(RenderStyle::AFTER, lastChild());
}
bool RenderInline::isInlineContinuation() const
{
return m_isContinuation;
}
void RenderInline::addChildToFlow(RenderObject* newChild, RenderObject* beforeChild)
{
if (!beforeChild && lastChild() && lastChild()->style()->styleType() == RenderStyle::AFTER)
beforeChild = lastChild();
setNeedsLayout(true);
if (!newChild->isText() && newChild->style()->position() != STATIC)
setOverhangingContents();
if (!newChild->isInline() && !newChild->isFloatingOrPositioned() )
{
RenderStyle *newStyle = new RenderStyle();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
RenderBlock *newBox = new (renderArena()) RenderBlock(0 );
newBox->setStyle(newStyle);
newBox->setIsAnonymousBox(true);
RenderFlow* oldContinuation = continuation();
setContinuation(newBox);
bool isLastChild = (beforeChild == lastChild());
updatePseudoChild(RenderStyle::AFTER, lastChild());
if (isLastChild && beforeChild != lastChild())
beforeChild = 0;
splitFlow(beforeChild, newBox, newChild, oldContinuation);
return;
}
RenderBox::addChild(newChild,beforeChild);
newChild->setNeedsLayoutAndMinMaxRecalc();
}
RenderInline* RenderInline::cloneInline(RenderFlow* src)
{
RenderInline *o = new (src->renderArena()) RenderInline(src->element());
o->m_isContinuation = true;
o->setStyle(src->style());
return o;
}
void RenderInline::splitInlines(RenderBlock* fromBlock, RenderBlock* toBlock,
RenderBlock* middleBlock,
RenderObject* beforeChild, RenderFlow* oldCont)
{
RenderInline* clone = cloneInline(this);
clone->setContinuation(oldCont);
RenderObject* o = beforeChild;
while (o) {
RenderObject* tmp = o;
o = tmp->nextSibling();
clone->addChildToFlow(removeChildNode(tmp), 0);
tmp->setNeedsLayoutAndMinMaxRecalc();
}
middleBlock->setContinuation(clone);
RenderFlow* curr = static_cast<RenderFlow*>(parent());
RenderFlow* currChild = this;
while (curr && curr != fromBlock) {
RenderInline* cloneChild = clone;
clone = cloneInline(curr);
clone->addChildToFlow(cloneChild, 0);
RenderFlow* oldCont = curr->continuation();
curr->setContinuation(clone);
clone->setContinuation(oldCont);
curr->updatePseudoChild(RenderStyle::AFTER, curr->lastChild());
o = currChild->nextSibling();
while (o) {
RenderObject* tmp = o;
o = tmp->nextSibling();
clone->addChildToFlow(curr->removeChildNode(tmp), 0);
tmp->setNeedsLayoutAndMinMaxRecalc();
}
currChild = curr;
curr = static_cast<RenderFlow*>(curr->parent());
}
toBlock->appendChildNode(clone);
o = currChild->nextSibling();
while (o) {
RenderObject* tmp = o;
o = tmp->nextSibling();
toBlock->appendChildNode(fromBlock->removeChildNode(tmp));
}
}
void RenderInline::splitFlow(RenderObject* beforeChild, RenderBlock* newBlockBox,
RenderObject* newChild, RenderFlow* oldCont)
{
RenderBlock* pre = 0;
RenderBlock* block = containingBlock();
bool madeNewBeforeBlock = false;
if (block->isAnonymousBox()) {
pre = block;
block = block->containingBlock();
}
else {
pre = block->createAnonymousBlock();
madeNewBeforeBlock = true;
}
RenderBlock* post = block->createAnonymousBlock();
RenderObject* boxFirst = madeNewBeforeBlock ? block->firstChild() : pre->nextSibling();
if (madeNewBeforeBlock)
block->insertChildNode(pre, boxFirst);
block->insertChildNode(newBlockBox, boxFirst);
block->insertChildNode(post, boxFirst);
block->setChildrenInline(false);
if (madeNewBeforeBlock) {
RenderObject* o = boxFirst;
while (o)
{
RenderObject* no = o;
o = no->nextSibling();
pre->appendChildNode(block->removeChildNode(no));
no->setNeedsLayoutAndMinMaxRecalc();
}
}
splitInlines(pre, post, newBlockBox, beforeChild, oldCont);
newBlockBox->setChildrenInline(false);
newBlockBox->addChildToFlow(newChild, 0);
pre->close();
pre->setPos(0, -500000);
pre->setNeedsLayout(true);
newBlockBox->close();
newBlockBox->setPos(0, -500000);
newBlockBox->setNeedsLayout(true);
post->close();
post->setPos(0, -500000);
post->setNeedsLayout(true);
block->setNeedsLayoutAndMinMaxRecalc();
}
void RenderInline::paint(QPainter *p, int _x, int _y, int _w, int _h,
int _tx, int _ty, PaintAction paintAction)
{
paintObject(p, _x, _y, _w, _h, _tx, _ty, paintAction);
}
void RenderInline::paintObject(QPainter *p, int _x, int _y,
int _w, int _h, int _tx, int _ty, PaintAction paintAction)
{
#ifdef DEBUG_LAYOUT
#endif
if (isRoot() &&
(paintAction == PaintActionElementBackground || paintAction == PaintActionChildBackground) &&
shouldPaintBackgroundOrBorder() && style()->visibility() == VISIBLE) {
paintRootBoxDecorations(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);
RenderObject *child = firstChild();
while(child != 0)
{
if(!child->layer() && !child->isFloating())
child->paint(p, _x, _y, _w, _h, _tx, _ty, paintAction);
child = child->nextSibling();
}
paintLineBoxDecorations(p, _x, _y, _w, _h, _tx, _ty, paintAction);
}
void RenderInline::calcMinMaxWidth()
{
KHTMLAssert( !minMaxKnown() );
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << renderName() << "(RenderInline)::calcMinMaxWidth() this=" << this << endl;
#endif
m_minWidth = 0;
m_maxWidth = 0;
setMinMaxKnown();
}
bool RenderInline::requiresLayer() {
return isRoot() || isRelPositioned() || style()->opacity() < 1.0f;
}
short RenderInline::width() const
{
short leftSide = 0;
short rightSide = 0;
for (InlineRunBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) {
if (curr == firstLineBox() || curr->xPos() < leftSide)
leftSide = curr->xPos();
if (curr == firstLineBox() || curr->xPos() + curr->width() > rightSide)
rightSide = curr->xPos() + curr->width();
}
return rightSide - leftSide;
}
int RenderInline::height() const
{
int h = 0;
if (firstLineBox())
h = lastLineBox()->yPos() + lastLineBox()->height() - firstLineBox()->yPos();
return h;
}
int RenderInline::offsetLeft() const
{
int x = RenderFlow::offsetLeft();
if (firstLineBox())
x += firstLineBox()->xPos();
return x;
}
int RenderInline::offsetTop() const
{
int y = RenderFlow::offsetTop();
if (firstLineBox())
y += firstLineBox()->yPos();
return y;
}
const char *RenderInline::renderName() const
{
if (isRelPositioned())
return "RenderInline (relative positioned)";
if (isAnonymousBox())
return "RenderInline (anonymous)";
return "RenderInline";
}
bool RenderInline::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty, bool inside)
{
for (RenderObject* child = lastChild(); child; child = child->previousSibling())
if (!child->layer() && !child->isFloating() && child->nodeAtPoint(info, _x, _y, _tx, _ty))
inside = true;
if (!inside && style()->visibility() != HIDDEN) {
for (InlineRunBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) {
if((_y >=_ty + curr->m_y) && (_y < _ty + curr->m_y + curr->m_height) &&
(_x >= _tx + curr->m_x) && (_x <_tx + curr->m_x + curr->m_width) ) {
inside = true;
break;
}
}
}
if (inside && element()) {
if (info.innerNode() && info.innerNode()->renderer() &&
!info.innerNode()->renderer()->isInline()) {
info.setInnerNode(element());
info.setInnerNonSharedNode(0);
info.setURLElement(0);
}
if (!info.innerNode())
info.setInnerNode(element());
if(!info.innerNonSharedNode())
info.setInnerNonSharedNode(element());
if (!info.URLElement()) {
RenderObject* p = this;
while (p) {
if (p->element() && p->element()->hasAnchor()) {
info.setURLElement(p->element());
break;
}
if (!isFloatingOrPositioned()) break;
p = p->parent();
}
}
}
if (!info.readonly()) {
bool oldinside = mouseInside();
setMouseInside(inside);
setHoverAndActive(info, oldinside, inside);
if (!isInline() && continuation())
continuation()->setHoverAndActive(info, oldinside, inside);
}
return inside;
}