#include "config.h"
#include "RenderBox.h"
#include "CachedImage.h"
#include "ChromeClient.h"
#include "Document.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "htmlediting.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "ImageBuffer.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "Page.h"
#include "RenderArena.h"
#include "RenderFlexibleBox.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderTableCell.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "TransformState.h"
#include <algorithm>
#include <math.h>
#if ENABLE(WML)
#include "WMLNames.h"
#endif
using namespace std;
namespace WebCore {
using namespace HTMLNames;
typedef WTF::HashMap<const RenderBox*, int> OverrideSizeMap;
static OverrideSizeMap* gOverrideSizeMap = 0;
bool RenderBox::s_hadOverflowClip = false;
RenderBox::RenderBox(Node* node)
: RenderBoxModelObject(node)
, m_marginLeft(0)
, m_marginRight(0)
, m_marginTop(0)
, m_marginBottom(0)
, m_minPrefWidth(-1)
, m_maxPrefWidth(-1)
, m_inlineBoxWrapper(0)
{
setIsBox();
}
RenderBox::~RenderBox()
{
}
void RenderBox::destroy()
{
if (hasOverrideSize())
gOverrideSizeMap->remove(this);
if (style() && (style()->height().isPercent() || style()->minHeight().isPercent() || style()->maxHeight().isPercent()))
RenderBlock::removePercentHeightDescendant(this);
RenderBoxModelObject::destroy();
}
void RenderBox::removeFloatingOrPositionedChildFromBlockLists()
{
ASSERT(isFloatingOrPositioned());
if (documentBeingDestroyed())
return;
if (isFloating()) {
RenderBlock* outermostBlock = containingBlock();
for (RenderBlock* p = outermostBlock; p && !p->isRenderView(); p = p->containingBlock()) {
if (p->containsFloat(this))
outermostBlock = p;
}
if (outermostBlock)
outermostBlock->markAllDescendantsWithFloatsForLayout(this, false);
}
if (isPositioned()) {
RenderObject* p;
for (p = parent(); p; p = p->parent()) {
if (p->isRenderBlock())
toRenderBlock(p)->removePositionedObject(this);
}
}
}
void RenderBox::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
s_hadOverflowClip = hasOverflowClip();
if (style()) {
if (diff >= StyleDifferenceRepaint && node() &&
(node()->hasTagName(htmlTag) || node()->hasTagName(bodyTag)))
view()->repaint();
if (diff == StyleDifferenceLayout && parent() && style()->position() != newStyle->position()) {
markContainingBlocksForLayout();
if (style()->position() == StaticPosition)
repaint();
if (isFloating() && !isPositioned() && (newStyle->position() == AbsolutePosition || newStyle->position() == FixedPosition))
removeFloatingOrPositionedChildFromBlockLists();
}
}
RenderBoxModelObject::styleWillChange(diff, newStyle);
}
void RenderBox::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBoxModelObject::styleDidChange(diff, oldStyle);
if (needsLayout() && oldStyle && (oldStyle->height().isPercent() || oldStyle->minHeight().isPercent() || oldStyle->maxHeight().isPercent()))
RenderBlock::removePercentHeightDescendant(this);
if (hasOverflowClip() && oldStyle && style() && oldStyle->effectiveZoom() != style()->effectiveZoom()) {
int left = scrollLeft();
if (left) {
left = (left / oldStyle->effectiveZoom()) * style()->effectiveZoom();
setScrollLeft(left);
}
int top = scrollTop();
if (top) {
top = (top / oldStyle->effectiveZoom()) * style()->effectiveZoom();
setScrollTop(top);
}
}
if (isBody())
document()->setTextColor(style()->color());
}
void RenderBox::updateBoxModelInfoFromStyle()
{
RenderBoxModelObject::updateBoxModelInfoFromStyle();
bool isRootObject = isRoot();
bool isViewObject = isRenderView();
if (isRootObject || isViewObject)
setHasBoxDecorations(true);
setPositioned(style()->position() == AbsolutePosition || style()->position() == FixedPosition);
setFloating(!isPositioned() && style()->isFloating());
if (style()->overflowX() != OVISIBLE && !isRootObject && (isRenderBlock() || isTableRow() || isTableSection())) {
bool boxHasOverflowClip = true;
if (isBody()) {
if (document()->documentElement()->hasTagName(htmlTag) &&
document()->body() == node() &&
document()->documentElement()->renderer()->style()->overflowX() == OVISIBLE)
boxHasOverflowClip = false;
}
if (boxHasOverflowClip) {
if (!s_hadOverflowClip)
repaint();
setHasOverflowClip();
}
}
setHasTransform(style()->hasTransformRelatedProperty());
setHasReflection(style()->boxReflect());
}
void RenderBox::layout()
{
ASSERT(needsLayout());
RenderObject* child = firstChild();
if (!child) {
setNeedsLayout(false);
return;
}
LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
while (child) {
child->layoutIfNeeded();
ASSERT(!child->needsLayout());
child = child->nextSibling();
}
statePusher.pop();
setNeedsLayout(false);
}
int RenderBox::clientWidth() const
{
return width() - borderLeft() - borderRight() - verticalScrollbarWidth();
}
int RenderBox::clientHeight() const
{
return height() - borderTop() - borderBottom() - horizontalScrollbarHeight();
}
int RenderBox::scrollWidth() const
{
if (hasOverflowClip())
return layer()->scrollWidth();
if (style()->direction() == LTR)
return max(clientWidth(), rightmostPosition(true, false) - borderLeft());
return clientWidth() - min(0, leftmostPosition(true, false) - borderLeft());
}
int RenderBox::scrollHeight() const
{
if (hasOverflowClip())
return layer()->scrollHeight();
return max(clientHeight(), lowestPosition(true, false) - borderTop());
}
int RenderBox::scrollLeft() const
{
return hasOverflowClip() ? layer()->scrollXOffset() : 0;
}
int RenderBox::scrollTop() const
{
return hasOverflowClip() ? layer()->scrollYOffset() : 0;
}
void RenderBox::setScrollLeft(int newLeft)
{
if (hasOverflowClip())
layer()->scrollToXOffset(newLeft);
}
void RenderBox::setScrollTop(int newTop)
{
if (hasOverflowClip())
layer()->scrollToYOffset(newTop);
}
void RenderBox::absoluteRects(Vector<IntRect>& rects, int tx, int ty)
{
rects.append(IntRect(tx, ty, width(), height()));
}
void RenderBox::absoluteQuads(Vector<FloatQuad>& quads)
{
quads.append(localToAbsoluteQuad(FloatRect(0, 0, width(), height())));
}
IntRect RenderBox::absoluteContentBox() const
{
IntRect rect = contentBoxRect();
FloatPoint absPos = localToAbsolute(FloatPoint());
rect.move(absPos.x(), absPos.y());
return rect;
}
FloatQuad RenderBox::absoluteContentQuad() const
{
IntRect rect = contentBoxRect();
return localToAbsoluteQuad(FloatRect(rect));
}
IntRect RenderBox::outlineBoundsForRepaint(RenderBoxModelObject* repaintContainer) const
{
IntRect box = borderBoundingBox();
adjustRectForOutlineAndShadow(box);
FloatQuad containerRelativeQuad = localToContainerQuad(FloatRect(box), repaintContainer);
box = containerRelativeQuad.enclosingBoundingBox();
box.move(view()->layoutDelta());
return box;
}
void RenderBox::addFocusRingRects(GraphicsContext* graphicsContext, int tx, int ty)
{
graphicsContext->addFocusRingRect(IntRect(tx, ty, width(), height()));
}
IntRect RenderBox::reflectionBox() const
{
IntRect result;
if (!style()->boxReflect())
return result;
IntRect box = borderBoxRect();
result = box;
switch (style()->boxReflect()->direction()) {
case ReflectionBelow:
result.move(0, box.height() + reflectionOffset());
break;
case ReflectionAbove:
result.move(0, -box.height() - reflectionOffset());
break;
case ReflectionLeft:
result.move(-box.width() - reflectionOffset(), 0);
break;
case ReflectionRight:
result.move(box.width() + reflectionOffset(), 0);
break;
}
return result;
}
int RenderBox::reflectionOffset() const
{
if (!style()->boxReflect())
return 0;
if (style()->boxReflect()->direction() == ReflectionLeft || style()->boxReflect()->direction() == ReflectionRight)
return style()->boxReflect()->offset().calcValue(borderBoxRect().width());
return style()->boxReflect()->offset().calcValue(borderBoxRect().height());
}
IntRect RenderBox::reflectedRect(const IntRect& r) const
{
if (!style()->boxReflect())
return IntRect();
IntRect box = borderBoxRect();
IntRect result = r;
switch (style()->boxReflect()->direction()) {
case ReflectionBelow:
result.setY(box.bottom() + reflectionOffset() + (box.bottom() - r.bottom()));
break;
case ReflectionAbove:
result.setY(box.y() - reflectionOffset() - box.height() + (box.bottom() - r.bottom()));
break;
case ReflectionLeft:
result.setX(box.x() - reflectionOffset() - box.width() + (box.right() - r.right()));
break;
case ReflectionRight:
result.setX(box.right() + reflectionOffset() + (box.right() - r.right()));
break;
}
return result;
}
int RenderBox::verticalScrollbarWidth() const
{
return includeVerticalScrollbarSize() ? layer()->verticalScrollbarWidth() : 0;
}
int RenderBox::horizontalScrollbarHeight() const
{
return includeHorizontalScrollbarSize() ? layer()->horizontalScrollbarHeight() : 0;
}
bool RenderBox::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
{
RenderLayer* l = layer();
if (l && l->scroll(direction, granularity, multiplier)) {
if (stopNode)
*stopNode = node();
return true;
}
if (stopNode && *stopNode && *stopNode == node())
return true;
RenderBlock* b = containingBlock();
if (b && !b->isRenderView())
return b->scroll(direction, granularity, multiplier, stopNode);
return false;
}
bool RenderBox::canBeProgramaticallyScrolled(bool) const
{
return (hasOverflowClip() && (scrollsOverflow() || (node() && node()->isContentEditable()))) || (node() && node()->isDocumentNode());
}
void RenderBox::autoscroll()
{
if (layer())
layer()->autoscroll();
}
void RenderBox::panScroll(const IntPoint& source)
{
if (layer())
layer()->panScrollFromPoint(source);
}
int RenderBox::minPrefWidth() const
{
if (prefWidthsDirty())
const_cast<RenderBox*>(this)->calcPrefWidths();
return m_minPrefWidth;
}
int RenderBox::maxPrefWidth() const
{
if (prefWidthsDirty())
const_cast<RenderBox*>(this)->calcPrefWidths();
return m_maxPrefWidth;
}
int RenderBox::overrideSize() const
{
if (!hasOverrideSize())
return -1;
return gOverrideSizeMap->get(this);
}
void RenderBox::setOverrideSize(int s)
{
if (s == -1) {
if (hasOverrideSize()) {
setHasOverrideSize(false);
gOverrideSizeMap->remove(this);
}
} else {
if (!gOverrideSizeMap)
gOverrideSizeMap = new OverrideSizeMap();
setHasOverrideSize(true);
gOverrideSizeMap->set(this, s);
}
}
int RenderBox::overrideWidth() const
{
return hasOverrideSize() ? overrideSize() : width();
}
int RenderBox::overrideHeight() const
{
return hasOverrideSize() ? overrideSize() : height();
}
int RenderBox::calcBorderBoxWidth(int width) const
{
int bordersPlusPadding = borderLeft() + borderRight() + paddingLeft() + paddingRight();
if (style()->boxSizing() == CONTENT_BOX)
return width + bordersPlusPadding;
return max(width, bordersPlusPadding);
}
int RenderBox::calcBorderBoxHeight(int height) const
{
int bordersPlusPadding = borderTop() + borderBottom() + paddingTop() + paddingBottom();
if (style()->boxSizing() == CONTENT_BOX)
return height + bordersPlusPadding;
return max(height, bordersPlusPadding);
}
int RenderBox::calcContentBoxWidth(int width) const
{
if (style()->boxSizing() == BORDER_BOX)
width -= (borderLeft() + borderRight() + paddingLeft() + paddingRight());
return max(0, width);
}
int RenderBox::calcContentBoxHeight(int height) const
{
if (style()->boxSizing() == BORDER_BOX)
height -= (borderTop() + borderBottom() + paddingTop() + paddingBottom());
return max(0, height);
}
bool RenderBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction action)
{
tx += x();
ty += y();
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (!child->hasLayer() && child->nodeAtPoint(request, result, xPos, yPos, tx, ty, action)) {
updateHitTestResult(result, IntPoint(xPos - tx, yPos - ty));
return true;
}
}
if (visibleToHitTesting() && action == HitTestForeground && IntRect(tx, ty, width(), height()).contains(xPos, yPos)) {
updateHitTestResult(result, IntPoint(xPos - tx, yPos - ty));
return true;
}
return false;
}
void RenderBox::paint(PaintInfo& paintInfo, int tx, int ty)
{
tx += x();
ty += y();
PaintInfo childInfo(paintInfo);
childInfo.paintingRoot = paintingRootForChildren(paintInfo);
for (RenderObject* child = firstChild(); child; child = child->nextSibling())
child->paint(childInfo, tx, ty);
}
void RenderBox::paintRootBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
{
const FillLayer* bgLayer = style()->backgroundLayers();
Color bgColor = style()->backgroundColor();
if (!style()->hasBackground() && node() && node()->hasTagName(HTMLNames::htmlTag)) {
HTMLElement* body = document()->body();
RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
if (bodyObject) {
bgLayer = bodyObject->style()->backgroundLayers();
bgColor = bodyObject->style()->backgroundColor();
}
}
int w = width();
int h = height();
int rw;
int rh;
if (view()->frameView()) {
rw = view()->frameView()->contentsWidth();
rh = view()->frameView()->contentsHeight();
} else {
rw = view()->width();
rh = view()->height();
}
int bx = tx - marginLeft();
int by = ty - marginTop();
int bw = max(w + marginLeft() + marginRight() + borderLeft() + borderRight(), rw);
int bh = max(h + marginTop() + marginBottom() + borderTop() + borderBottom(), rh);
paintFillLayers(paintInfo, bgColor, bgLayer, bx, by, bw, bh);
if (style()->hasBorder() && style()->display() != INLINE)
paintBorder(paintInfo.context, tx, ty, w, h, style());
}
void RenderBox::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
{
if (!shouldPaintWithinRoot(paintInfo))
return;
if (isRoot()) {
paintRootBoxDecorations(paintInfo, tx, ty);
return;
}
int w = width();
int h = height();
borderFitAdjust(tx, w);
paintBoxShadow(paintInfo.context, tx, ty, w, h, style());
bool themePainted = style()->hasAppearance() && !theme()->paint(this, paintInfo, IntRect(tx, ty, w, h));
if (!themePainted) {
if (!isBody() || document()->documentElement()->renderer()->style()->hasBackground())
paintFillLayers(paintInfo, style()->backgroundColor(), style()->backgroundLayers(), tx, ty, w, h);
if (style()->hasAppearance())
theme()->paintDecorations(this, paintInfo, IntRect(tx, ty, w, h));
}
if ((!style()->hasAppearance() || (!themePainted && theme()->paintBorderOnly(this, paintInfo, IntRect(tx, ty, w, h)))) && style()->hasBorder())
paintBorder(paintInfo.context, tx, ty, w, h, style());
}
void RenderBox::paintMask(PaintInfo& paintInfo, int tx, int ty)
{
if (!shouldPaintWithinRoot(paintInfo) || style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
return;
int w = width();
int h = height();
borderFitAdjust(tx, w);
paintMaskImages(paintInfo, tx, ty, w, h);
}
void RenderBox::paintMaskImages(const PaintInfo& paintInfo, int tx, int ty, int w, int h)
{
bool pushTransparencyLayer = false;
StyleImage* maskBoxImage = style()->maskBoxImage().image();
if (maskBoxImage && style()->maskLayers()->hasImage()) {
pushTransparencyLayer = true;
} else {
for (const FillLayer* fillLayer = style()->maskLayers()->next(); fillLayer; fillLayer = fillLayer->next()) {
if (fillLayer->hasImage() && fillLayer->image()->canRender(style()->effectiveZoom())) {
pushTransparencyLayer = true;
break;
}
}
}
CompositeOperator compositeOp = CompositeDestinationIn;
if (pushTransparencyLayer) {
paintInfo.context->setCompositeOperation(CompositeDestinationIn);
paintInfo.context->beginTransparencyLayer(1.0f);
compositeOp = CompositeSourceOver;
}
paintFillLayers(paintInfo, Color(), style()->maskLayers(), tx, ty, w, h, compositeOp);
paintNinePieceImage(paintInfo.context, tx, ty, w, h, style(), style()->maskBoxImage(), compositeOp);
if (pushTransparencyLayer)
paintInfo.context->endTransparencyLayer();
}
IntRect RenderBox::maskClipRect()
{
IntRect bbox = borderBoxRect();
if (style()->maskBoxImage().image())
return bbox;
IntRect result;
for (const FillLayer* maskLayer = style()->maskLayers(); maskLayer; maskLayer = maskLayer->next()) {
if (maskLayer->image()) {
IntRect maskRect;
IntPoint phase;
IntSize tileSize;
calculateBackgroundImageGeometry(maskLayer, bbox.x(), bbox.y(), bbox.width(), bbox.height(), maskRect, phase, tileSize);
result.unite(maskRect);
}
}
return result;
}
void RenderBox::paintFillLayers(const PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, int tx, int ty, int width, int height, CompositeOperator op)
{
if (!fillLayer)
return;
paintFillLayers(paintInfo, c, fillLayer->next(), tx, ty, width, height, op);
paintFillLayer(paintInfo, c, fillLayer, tx, ty, width, height, op);
}
void RenderBox::paintFillLayer(const PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, int tx, int ty, int width, int height, CompositeOperator op)
{
paintFillLayerExtended(paintInfo, c, fillLayer, tx, ty, width, height, 0, op);
}
void RenderBox::imageChanged(WrappedImagePtr image, const IntRect*)
{
if (!parent())
return;
if ((style()->borderImage().image() && style()->borderImage().image()->data() == image) ||
(style()->maskBoxImage().image() && style()->maskBoxImage().image()->data() == image)) {
repaint();
return;
}
bool didFullRepaint = repaintLayerRectsForImage(image, style()->backgroundLayers(), true);
if (!didFullRepaint)
repaintLayerRectsForImage(image, style()->maskLayers(), false);
}
bool RenderBox::repaintLayerRectsForImage(WrappedImagePtr image, const FillLayer* layers, bool drawingBackground)
{
IntRect rendererRect;
RenderBox* layerRenderer = 0;
for (const FillLayer* curLayer = layers; curLayer; curLayer = curLayer->next()) {
if (curLayer->image() && image == curLayer->image()->data() && curLayer->image()->canRender(style()->effectiveZoom())) {
if (!layerRenderer) {
bool drawingRootBackground = drawingBackground && (isRoot() || (isBody() && !document()->documentElement()->renderer()->style()->hasBackground()));
if (drawingRootBackground) {
layerRenderer = view();
int rw;
int rh;
if (FrameView* frameView = toRenderView(layerRenderer)->frameView()) {
rw = frameView->contentsWidth();
rh = frameView->contentsHeight();
} else {
rw = layerRenderer->width();
rh = layerRenderer->height();
}
rendererRect = IntRect(-layerRenderer->marginLeft(),
-layerRenderer->marginTop(),
max(layerRenderer->width() + layerRenderer->marginLeft() + layerRenderer->marginRight() + layerRenderer->borderLeft() + layerRenderer->borderRight(), rw),
max(layerRenderer->height() + layerRenderer->marginTop() + layerRenderer->marginBottom() + layerRenderer->borderTop() + layerRenderer->borderBottom(), rh));
} else {
layerRenderer = this;
rendererRect = borderBoxRect();
}
}
IntRect repaintRect;
IntPoint phase;
IntSize tileSize;
layerRenderer->calculateBackgroundImageGeometry(curLayer, rendererRect.x(), rendererRect.y(), rendererRect.width(), rendererRect.height(), repaintRect, phase, tileSize);
layerRenderer->repaintRectangle(repaintRect);
if (repaintRect == rendererRect)
return true;
}
}
return false;
}
#if PLATFORM(MAC)
void RenderBox::paintCustomHighlight(int tx, int ty, const AtomicString& type, bool behindText)
{
Frame* frame = document()->frame();
if (!frame)
return;
Page* page = frame->page();
if (!page)
return;
InlineBox* boxWrap = inlineBoxWrapper();
RootInlineBox* r = boxWrap ? boxWrap->root() : 0;
if (r) {
FloatRect rootRect(tx + r->x(), ty + r->selectionTop(), r->width(), r->selectionHeight());
FloatRect imageRect(tx + x(), rootRect.y(), width(), rootRect.height());
page->chrome()->client()->paintCustomHighlight(node(), type, imageRect, rootRect, behindText, false);
} else {
FloatRect imageRect(tx + x(), ty + y(), width(), height());
page->chrome()->client()->paintCustomHighlight(node(), type, imageRect, imageRect, behindText, false);
}
}
#endif
bool RenderBox::pushContentsClip(PaintInfo& paintInfo, int tx, int ty)
{
if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseSelfOutline || paintInfo.phase == PaintPhaseMask)
return false;
bool isControlClip = hasControlClip();
bool isOverflowClip = hasOverflowClip() && !layer()->isSelfPaintingLayer();
if (!isControlClip && !isOverflowClip)
return false;
if (paintInfo.phase == PaintPhaseOutline)
paintInfo.phase = PaintPhaseChildOutlines;
else if (paintInfo.phase == PaintPhaseChildBlockBackground) {
paintInfo.phase = PaintPhaseBlockBackground;
paintObject(paintInfo, tx, ty);
paintInfo.phase = PaintPhaseChildBlockBackgrounds;
}
IntRect clipRect(isControlClip ? controlClipRect(tx, ty) : overflowClipRect(tx, ty));
paintInfo.context->save();
if (style()->hasBorderRadius()) {
IntSize topLeft, topRight, bottomLeft, bottomRight;
IntRect borderRect = IntRect(tx, ty, width(), height());
style()->getBorderRadiiForRect(borderRect, topLeft, topRight, bottomLeft, bottomRight);
paintInfo.context->addRoundedRectClip(borderRect, topLeft, topRight, bottomLeft, bottomRight);
}
paintInfo.context->clip(clipRect);
return true;
}
void RenderBox::popContentsClip(PaintInfo& paintInfo, PaintPhase originalPhase, int tx, int ty)
{
ASSERT(hasControlClip() || (hasOverflowClip() && !layer()->isSelfPaintingLayer()));
paintInfo.context->restore();
if (originalPhase == PaintPhaseOutline) {
paintInfo.phase = PaintPhaseSelfOutline;
paintObject(paintInfo, tx, ty);
paintInfo.phase = originalPhase;
} else if (originalPhase == PaintPhaseChildBlockBackground)
paintInfo.phase = originalPhase;
}
IntRect RenderBox::overflowClipRect(int tx, int ty)
{
int bLeft = borderLeft();
int bTop = borderTop();
int clipX = tx + bLeft;
int clipY = ty + bTop;
int clipWidth = width() - bLeft - borderRight();
int clipHeight = height() - bTop - borderBottom();
if (layer()) {
clipWidth -= layer()->verticalScrollbarWidth();
clipHeight -= layer()->horizontalScrollbarHeight();
}
return IntRect(clipX, clipY, clipWidth, clipHeight);
}
IntRect RenderBox::clipRect(int tx, int ty)
{
int clipX = tx;
int clipY = ty;
int clipWidth = width();
int clipHeight = height();
if (!style()->clipLeft().isAuto()) {
int c = style()->clipLeft().calcValue(width());
clipX += c;
clipWidth -= c;
}
if (!style()->clipRight().isAuto())
clipWidth -= width() - style()->clipRight().calcValue(width());
if (!style()->clipTop().isAuto()) {
int c = style()->clipTop().calcValue(height());
clipY += c;
clipHeight -= c;
}
if (!style()->clipBottom().isAuto())
clipHeight -= height() - style()->clipBottom().calcValue(height());
return IntRect(clipX, clipY, clipWidth, clipHeight);
}
int RenderBox::containingBlockWidthForContent() const
{
RenderBlock* cb = containingBlock();
if (shrinkToAvoidFloats())
return cb->lineWidth(y(), false);
return cb->availableWidth();
}
void RenderBox::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState) const
{
if (repaintContainer == this)
return;
if (RenderView* v = view()) {
if (v->layoutStateEnabled() && !repaintContainer) {
LayoutState* layoutState = v->layoutState();
IntSize offset = layoutState->m_offset;
offset.expand(x(), y());
if (style()->position() == RelativePosition && layer())
offset += layer()->relativePositionOffset();
transformState.move(offset);
return;
}
}
if (style()->position() == FixedPosition)
fixed = true;
RenderObject* o = container();
if (!o)
return;
bool hasTransform = hasLayer() && layer()->transform();
if (hasTransform)
fixed = false;
IntSize containerOffset = offsetFromContainer(o);
bool preserve3D = useTransforms && (o->style()->preserves3D() || style()->preserves3D());
if (useTransforms && shouldUseTransformFromContainer(o)) {
TransformationMatrix t;
getTransformFromContainer(o, containerOffset, t);
transformState.applyTransform(t, preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
} else
transformState.move(containerOffset.width(), containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
o->mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
}
void RenderBox::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
{
ASSERT(!view() || !view()->layoutStateEnabled());
if (style()->position() == FixedPosition)
fixed = true;
bool hasTransform = hasLayer() && layer()->transform();
if (hasTransform)
fixed = false;
RenderObject* o = container();
if (!o)
return;
o->mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
IntSize containerOffset = offsetFromContainer(o);
bool preserve3D = useTransforms && (o->style()->preserves3D() || style()->preserves3D());
if (useTransforms && shouldUseTransformFromContainer(o)) {
TransformationMatrix t;
getTransformFromContainer(o, containerOffset, t);
transformState.applyTransform(t, preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
} else
transformState.move(-containerOffset.width(), -containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
}
IntSize RenderBox::offsetFromContainer(RenderObject* o) const
{
ASSERT(o == container());
IntSize offset;
if (isRelPositioned())
offset += relativePositionOffset();
if (!isInline() || isReplaced()) {
RenderBlock* cb;
if (o->isBlockFlow() && style()->position() != AbsolutePosition && style()->position() != FixedPosition
&& (cb = toRenderBlock(o))->hasColumns()) {
IntRect rect(x(), y(), 1, 1);
cb->adjustRectForColumns(rect);
offset.expand(rect.x(), rect.y());
} else
offset.expand(x(), y());
}
if (o->hasOverflowClip())
offset -= toRenderBox(o)->layer()->scrolledContentOffset();
if (style()->position() == AbsolutePosition && o->isRelPositioned() && o->isRenderInline())
offset += toRenderInline(o)->relativePositionedInlineOffset(this);
return offset;
}
InlineBox* RenderBox::createInlineBox()
{
return new (renderArena()) InlineBox(this);
}
void RenderBox::dirtyLineBoxes(bool fullLayout)
{
if (m_inlineBoxWrapper) {
if (fullLayout) {
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
} else
m_inlineBoxWrapper->dirtyLineBoxes();
}
}
void RenderBox::positionLineBox(InlineBox* box)
{
if (isPositioned()) {
bool wasInline = style()->isOriginalDisplayInlineType();
if (wasInline && style()->hasStaticX()) {
layer()->setStaticX(box->x());
setChildNeedsLayout(true, false); } else if (!wasInline && style()->hasStaticY()) {
layer()->setStaticY(box->y());
setChildNeedsLayout(true, false); }
box->remove();
box->destroy(renderArena());
} else if (isReplaced()) {
setLocation(box->x(), box->y());
m_inlineBoxWrapper = box;
}
}
void RenderBox::deleteLineBoxWrapper()
{
if (m_inlineBoxWrapper) {
if (!documentBeingDestroyed())
m_inlineBoxWrapper->remove();
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
}
}
IntRect RenderBox::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
{
if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return IntRect();
IntRect r = overflowRect(false);
RenderView* v = view();
if (v) {
r.move(v->layoutDelta());
}
if (style()) {
if (style()->hasAppearance())
theme()->adjustRepaintRect(this, r);
if (v) {
ASSERT(style()->outlineSize() <= v->maximalOutlineSize());
r.inflate(v->maximalOutlineSize());
}
}
computeRectForRepaint(repaintContainer, r);
return r;
}
void RenderBox::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed)
{
if (RenderView* v = view()) {
if (v->layoutStateEnabled() && !repaintContainer) {
LayoutState* layoutState = v->layoutState();
if (layer() && layer()->transform())
rect = layer()->transform()->mapRect(rect);
if (style()->position() == RelativePosition && layer())
rect.move(layer()->relativePositionOffset());
rect.move(x(), y());
rect.move(layoutState->m_offset);
if (layoutState->m_clipped)
rect.intersect(layoutState->m_clipRect);
return;
}
}
if (hasReflection())
rect.unite(reflectedRect(rect));
if (repaintContainer == this)
return;
RenderObject* o = container();
if (!o)
return;
IntPoint topLeft = rect.location();
topLeft.move(x(), y());
if (style()->position() == FixedPosition)
fixed = true;
if (o->isBlockFlow() && style()->position() != AbsolutePosition && style()->position() != FixedPosition) {
RenderBlock* cb = toRenderBlock(o);
if (cb->hasColumns()) {
IntRect repaintRect(topLeft, rect.size());
cb->adjustRectForColumns(repaintRect);
topLeft = repaintRect.location();
rect = repaintRect;
}
}
if (layer() && layer()->transform()) {
fixed = false;
rect = layer()->transform()->mapRect(rect);
topLeft = rect.location();
topLeft.move(x(), y());
}
if (style()->position() == AbsolutePosition && o->isRelPositioned() && o->isRenderInline())
topLeft += toRenderInline(o)->relativePositionedInlineOffset(this);
else if (style()->position() == RelativePosition && layer()) {
topLeft += layer()->relativePositionOffset();
}
if (o->hasOverflowClip()) {
RenderBox* containerBox = toRenderBox(o);
topLeft -= containerBox->layer()->scrolledContentOffset();
IntRect repaintRect(topLeft, rect.size());
IntRect boxRect(0, 0, containerBox->layer()->width(), containerBox->layer()->height());
rect = intersection(repaintRect, boxRect);
if (rect.isEmpty())
return;
} else
rect.setLocation(topLeft);
o->computeRectForRepaint(repaintContainer, rect, fixed);
}
void RenderBox::repaintDuringLayoutIfMoved(const IntRect& rect)
{
int newX = x();
int newY = y();
int newWidth = width();
int newHeight = height();
if (rect.x() != newX || rect.y() != newY) {
m_frameRect = rect;
repaint();
repaintOverhangingFloats(true);
m_frameRect = IntRect(newX, newY, newWidth, newHeight);
repaint();
repaintOverhangingFloats(true);
}
}
void RenderBox::calcWidth()
{
if (isPositioned()) {
calcAbsoluteHorizontal();
return;
}
if (node() && view()->frameView() && view()->frameView()->layoutRoot(true) == this)
return;
if (hasOverrideSize() && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isFlexibleBox() && parent()->isFlexingChildren()) {
setWidth(overrideSize());
return;
}
bool inVerticalBox = parent()->isFlexibleBox() && (parent()->style()->boxOrient() == VERTICAL);
bool stretching = (parent()->style()->boxAlign() == BSTRETCH);
bool treatAsReplaced = shouldCalculateSizeAsReplaced() && (!inVerticalBox || !stretching);
Length w = (treatAsReplaced) ? Length(calcReplacedWidth(), Fixed) : style()->width();
RenderBlock* cb = containingBlock();
int containerWidth = max(0, containingBlockWidthForContent());
Length marginLeft = style()->marginLeft();
Length marginRight = style()->marginRight();
if (isInline() && !isInlineBlockOrInlineTable()) {
m_marginLeft = marginLeft.calcMinValue(containerWidth);
m_marginRight = marginRight.calcMinValue(containerWidth);
if (treatAsReplaced)
setWidth(max(w.value() + borderLeft() + borderRight() + paddingLeft() + paddingRight(), minPrefWidth()));
return;
}
if (treatAsReplaced)
setWidth(w.value() + borderLeft() + borderRight() + paddingLeft() + paddingRight());
else {
setWidth(calcWidthUsing(Width, containerWidth));
if (!style()->maxWidth().isUndefined()) {
int maxW = calcWidthUsing(MaxWidth, containerWidth);
if (width() > maxW) {
setWidth(maxW);
w = style()->maxWidth();
}
}
int minW = calcWidthUsing(MinWidth, containerWidth);
if (width() < minW) {
setWidth(minW);
w = style()->minWidth();
}
}
if (stretchesToMinIntrinsicWidth()) {
setWidth(max(width(), minPrefWidth()));
w = Length(width(), Fixed);
}
if (w.isAuto()) {
m_marginLeft = marginLeft.calcMinValue(containerWidth);
m_marginRight = marginRight.calcMinValue(containerWidth);
} else {
m_marginLeft = 0;
m_marginRight = 0;
calcHorizontalMargins(marginLeft, marginRight, containerWidth);
}
if (containerWidth && containerWidth != (width() + m_marginLeft + m_marginRight)
&& !isFloating() && !isInline() && !cb->isFlexibleBox()) {
if (cb->style()->direction() == LTR)
m_marginRight = containerWidth - width() - m_marginLeft;
else
m_marginLeft = containerWidth - width() - m_marginRight;
}
}
int RenderBox::calcWidthUsing(WidthType widthType, int cw)
{
int widthResult = width();
Length w;
if (widthType == Width)
w = style()->width();
else if (widthType == MinWidth)
w = style()->minWidth();
else
w = style()->maxWidth();
if (w.isIntrinsicOrAuto()) {
int marginLeft = style()->marginLeft().calcMinValue(cw);
int marginRight = style()->marginRight().calcMinValue(cw);
if (cw)
widthResult = cw - marginLeft - marginRight;
if (sizesToIntrinsicWidth(widthType)) {
widthResult = max(widthResult, minPrefWidth());
widthResult = min(widthResult, maxPrefWidth());
}
} else
widthResult = calcBorderBoxWidth(w.calcValue(cw));
return widthResult;
}
bool RenderBox::sizesToIntrinsicWidth(WidthType widthType) const
{
if (isFloating() || (isInlineBlockOrInlineTable() && !isHTMLMarquee()))
return true;
Length width = (widthType == MaxWidth) ? style()->maxWidth() : style()->width();
if (width.type() == Intrinsic)
return true;
if (parent()->style()->overflowX() == OMARQUEE) {
EMarqueeDirection dir = parent()->style()->marqueeDirection();
if (dir == MAUTO || dir == MFORWARD || dir == MBACKWARD || dir == MLEFT || dir == MRIGHT)
return true;
}
if (parent()->isFlexibleBox()
&& (parent()->style()->boxOrient() == HORIZONTAL || parent()->style()->boxAlign() != BSTRETCH))
return true;
return false;
}
void RenderBox::calcHorizontalMargins(const Length& marginLeft, const Length& marginRight, int containerWidth)
{
if (isFloating() || isInline()) {
m_marginLeft = marginLeft.calcMinValue(containerWidth);
m_marginRight = marginRight.calcMinValue(containerWidth);
return;
}
if ((marginLeft.isAuto() && marginRight.isAuto() && width() < containerWidth)
|| (!marginLeft.isAuto() && !marginRight.isAuto() && containingBlock()->style()->textAlign() == WEBKIT_CENTER)) {
m_marginLeft = max(0, (containerWidth - width()) / 2);
m_marginRight = containerWidth - width() - m_marginLeft;
} else if ((marginRight.isAuto() && width() < containerWidth)
|| (!marginLeft.isAuto() && containingBlock()->style()->direction() == RTL && containingBlock()->style()->textAlign() == WEBKIT_LEFT)) {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = containerWidth - width() - m_marginLeft;
} else if ((marginLeft.isAuto() && width() < containerWidth)
|| (!marginRight.isAuto() && containingBlock()->style()->direction() == LTR && containingBlock()->style()->textAlign() == WEBKIT_RIGHT)) {
m_marginRight = marginRight.calcValue(containerWidth);
m_marginLeft = containerWidth - width() - m_marginRight;
} else {
m_marginLeft = marginLeft.calcMinValue(containerWidth);
m_marginRight = marginRight.calcMinValue(containerWidth);
}
}
void RenderBox::calcHeight()
{
if (isTableCell() || (isInline() && !isReplaced()))
return;
Length h;
if (isPositioned())
calcAbsoluteVertical();
else {
calcVerticalMargins();
if (isTable())
return;
bool inHorizontalBox = parent()->isFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL;
bool stretching = parent()->style()->boxAlign() == BSTRETCH;
bool treatAsReplaced = shouldCalculateSizeAsReplaced() && (!inHorizontalBox || !stretching);
bool checkMinMaxHeight = false;
if (hasOverrideSize() && parent()->isFlexibleBox() && parent()->style()->boxOrient() == VERTICAL
&& parent()->isFlexingChildren())
h = Length(overrideSize() - borderTop() - borderBottom() - paddingTop() - paddingBottom(), Fixed);
else if (treatAsReplaced)
h = Length(calcReplacedHeight(), Fixed);
else {
h = style()->height();
checkMinMaxHeight = true;
}
if (h.isAuto() && parent()->isFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isStretchingChildren()) {
h = Length(parentBox()->contentHeight() - marginTop() - marginBottom() -
borderTop() - paddingTop() - borderBottom() - paddingBottom(), Fixed);
checkMinMaxHeight = false;
}
int heightResult;
if (checkMinMaxHeight) {
heightResult = calcHeightUsing(style()->height());
if (heightResult == -1)
heightResult = height();
int minH = calcHeightUsing(style()->minHeight()); int maxH = style()->maxHeight().isUndefined() ? heightResult : calcHeightUsing(style()->maxHeight());
if (maxH == -1)
maxH = heightResult;
heightResult = min(maxH, heightResult);
heightResult = max(minH, heightResult);
} else {
heightResult = h.value() + borderTop() + borderBottom() + paddingTop() + paddingBottom();
}
setHeight(heightResult);
}
bool printingNeedsBaseHeight = document()->printing() && h.isPercent()
&& (isRoot() || isBody() && document()->documentElement()->renderer()->style()->height().isPercent());
if (stretchesToViewHeight() || printingNeedsBaseHeight) {
int margins = collapsedMarginTop() + collapsedMarginBottom();
int visHeight = document()->printing() ? view()->frameView()->visibleHeight() : view()->viewHeight();
if (isRoot())
setHeight(max(height(), visHeight - margins));
else {
int marginsBordersPadding = margins + parentBox()->marginTop() + parentBox()->marginBottom()
+ parentBox()->borderTop() + parentBox()->borderBottom()
+ parentBox()->paddingTop() + parentBox()->paddingBottom();
setHeight(max(height(), visHeight - marginsBordersPadding));
}
}
}
int RenderBox::calcHeightUsing(const Length& h)
{
int height = -1;
if (!h.isAuto()) {
if (h.isFixed())
height = h.value();
else if (h.isPercent())
height = calcPercentageHeight(h);
if (height != -1) {
height = calcBorderBoxHeight(height);
return height;
}
}
return height;
}
int RenderBox::calcPercentageHeight(const Length& height)
{
int result = -1;
bool includeBorderPadding = isTable();
RenderBlock* cb = containingBlock();
if (style()->htmlHacks()) {
while (!cb->isRenderView() && !cb->isBody() && !cb->isTableCell() && !cb->isPositioned() && cb->style()->height().isAuto()) {
cb = cb->containingBlock();
cb->addPercentHeightDescendant(this);
}
}
bool isPositionedWithSpecifiedHeight = cb->isPositioned() && (!cb->style()->height().isAuto() || (!cb->style()->top().isAuto() && !cb->style()->bottom().isAuto()));
if (cb->isTableCell()) {
result = cb->overrideSize();
if (result == -1) {
RenderTableCell* cell = static_cast<RenderTableCell*>(cb);
if (scrollsOverflowY() && (!cell->style()->height().isAuto() || !cell->table()->style()->height().isAuto()))
return 0;
return -1;
}
includeBorderPadding = true;
}
else if (cb->style()->height().isFixed())
result = cb->calcContentBoxHeight(cb->style()->height().value());
else if (cb->style()->height().isPercent() && !isPositionedWithSpecifiedHeight) {
result = cb->calcPercentageHeight(cb->style()->height());
if (result != -1)
result = cb->calcContentBoxHeight(result);
} else if (cb->isRenderView() || (cb->isBody() && style()->htmlHacks()) || isPositionedWithSpecifiedHeight) {
int oldHeight = cb->height();
cb->calcHeight();
result = cb->contentHeight();
cb->setHeight(oldHeight);
} else if (cb->isRoot() && isPositioned())
result = cb->calcContentBoxHeight(cb->availableHeight());
if (result != -1) {
result = height.calcValue(result);
if (includeBorderPadding) {
result -= (borderTop() + paddingTop() + borderBottom() + paddingBottom());
result = max(0, result);
}
}
return result;
}
int RenderBox::calcReplacedWidth(bool includeMaxWidth) const
{
int width = calcReplacedWidthUsing(style()->width());
int minW = calcReplacedWidthUsing(style()->minWidth());
int maxW = !includeMaxWidth || style()->maxWidth().isUndefined() ? width : calcReplacedWidthUsing(style()->maxWidth());
return max(minW, min(width, maxW));
}
int RenderBox::calcReplacedWidthUsing(Length width) const
{
switch (width.type()) {
case Fixed:
return calcContentBoxWidth(width.value());
case Percent: {
const int cw = isPositioned() ? containingBlockWidthForPositioned(toRenderBoxModelObject(container())) : containingBlockWidthForContent();
if (cw > 0)
return calcContentBoxWidth(width.calcMinValue(cw));
}
default:
return intrinsicSize().width();
}
}
int RenderBox::calcReplacedHeight() const
{
int height = calcReplacedHeightUsing(style()->height());
int minH = calcReplacedHeightUsing(style()->minHeight());
int maxH = style()->maxHeight().isUndefined() ? height : calcReplacedHeightUsing(style()->maxHeight());
return max(minH, min(height, maxH));
}
int RenderBox::calcReplacedHeightUsing(Length height) const
{
switch (height.type()) {
case Fixed:
return calcContentBoxHeight(height.value());
case Percent:
{
RenderObject* cb = isPositioned() ? container() : containingBlock();
while (cb->isAnonymous()) {
cb = cb->containingBlock();
toRenderBlock(cb)->addPercentHeightDescendant(const_cast<RenderBox*>(this));
}
if (cb->isPositioned() && cb->style()->height().isAuto() && !(cb->style()->top().isAuto() || cb->style()->bottom().isAuto())) {
ASSERT(cb->isRenderBlock());
RenderBlock* block = toRenderBlock(cb);
int oldHeight = block->height();
block->calcHeight();
int newHeight = block->calcContentBoxHeight(block->contentHeight());
block->setHeight(oldHeight);
return calcContentBoxHeight(height.calcValue(newHeight));
}
int availableHeight = isPositioned() ? containingBlockHeightForPositioned(toRenderBoxModelObject(cb)) : toRenderBox(cb)->availableHeight();
if (cb->isTableCell() && (cb->style()->height().isAuto() || cb->style()->height().isPercent())) {
availableHeight = max(availableHeight, intrinsicSize().height());
return height.calcValue(availableHeight - (borderTop() + borderBottom()
+ paddingTop() + paddingBottom()));
}
return calcContentBoxHeight(height.calcValue(availableHeight));
}
default:
return intrinsicSize().height();
}
}
int RenderBox::availableHeight() const
{
return availableHeightUsing(style()->height());
}
int RenderBox::availableHeightUsing(const Length& h) const
{
if (h.isFixed())
return calcContentBoxHeight(h.value());
if (isRenderView())
return toRenderView(this)->frameView()->visibleHeight();
if (isTableCell() && (h.isAuto() || h.isPercent()))
return overrideSize() - (borderLeft() + borderRight() + paddingLeft() + paddingRight());
if (h.isPercent())
return calcContentBoxHeight(h.calcValue(containingBlock()->availableHeight()));
if (isRenderBlock() && isPositioned() && style()->height().isAuto() && !(style()->top().isAuto() || style()->bottom().isAuto())) {
RenderBlock* block = const_cast<RenderBlock*>(toRenderBlock(this));
int oldHeight = block->height();
block->calcHeight();
int newHeight = block->calcContentBoxHeight(block->contentHeight());
block->setHeight(oldHeight);
return calcContentBoxHeight(newHeight);
}
return containingBlock()->availableHeight();
}
void RenderBox::calcVerticalMargins()
{
if (isTableCell()) {
m_marginTop = 0;
m_marginBottom = 0;
return;
}
int cw = containingBlock()->contentWidth();
m_marginTop = style()->marginTop().calcMinValue(cw);
m_marginBottom = style()->marginBottom().calcMinValue(cw);
}
int RenderBox::containingBlockWidthForPositioned(const RenderBoxModelObject* containingBlock) const
{
if (containingBlock->isBox()) {
const RenderBox* containingBlockBox = toRenderBox(containingBlock);
return containingBlockBox->width() - containingBlockBox->borderLeft() - containingBlockBox->borderRight() - containingBlockBox->verticalScrollbarWidth();
}
ASSERT(containingBlock->isRenderInline() && containingBlock->isRelPositioned());
const RenderInline* flow = toRenderInline(containingBlock);
InlineFlowBox* first = flow->firstLineBox();
InlineFlowBox* last = flow->lastLineBox();
if (!first || !last)
return 0;
int fromLeft;
int fromRight;
if (containingBlock->style()->direction() == LTR) {
fromLeft = first->x() + first->borderLeft();
fromRight = last->x() + last->width() - last->borderRight();
} else {
fromRight = first->x() + first->width() - first->borderRight();
fromLeft = last->x() + last->borderLeft();
}
return max(0, (fromRight - fromLeft));
}
int RenderBox::containingBlockHeightForPositioned(const RenderBoxModelObject* containingBlock) const
{
int heightResult = 0;
if (containingBlock->isBox())
heightResult = toRenderBox(containingBlock)->height();
else if (containingBlock->isRenderInline()) {
ASSERT(containingBlock->isRelPositioned());
heightResult = toRenderInline(containingBlock)->linesBoundingBox().height();
}
return heightResult - containingBlock->borderTop() - containingBlock->borderBottom();
}
void RenderBox::calcAbsoluteHorizontal()
{
if (isReplaced()) {
calcAbsoluteHorizontalReplaced();
return;
}
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerWidth = containingBlockWidthForPositioned(containerBlock);
TextDirection containerDirection = (style()->htmlHacks()) ? parent()->style()->direction() : containerBlock->style()->direction();
const int bordersPlusPadding = borderLeft() + borderRight() + paddingLeft() + paddingRight();
const Length marginLeft = style()->marginLeft();
const Length marginRight = style()->marginRight();
Length left = style()->left();
Length right = style()->right();
if (left.isAuto() && right.isAuto()) {
if (containerDirection == LTR) {
int staticPosition = layer()->staticX() - containerBlock->borderLeft();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent()) {
if (po->isBox())
staticPosition += toRenderBox(po)->x();
}
left.setValue(Fixed, staticPosition);
} else {
RenderObject* po = parent();
int staticPosition = layer()->staticX() + containerWidth + containerBlock->borderRight();
if (po->isBox())
staticPosition -= toRenderBox(po)->width();
for (; po && po != containerBlock; po = po->parent()) {
if (po->isBox())
staticPosition -= toRenderBox(po)->x();
}
right.setValue(Fixed, staticPosition);
}
}
int widthResult;
int xResult;
calcAbsoluteHorizontalValues(style()->width(), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
widthResult, m_marginLeft, m_marginRight, xResult);
setWidth(widthResult);
setX(xResult);
if (!style()->maxWidth().isUndefined()) {
int maxWidth;
int maxMarginLeft;
int maxMarginRight;
int maxXPos;
calcAbsoluteHorizontalValues(style()->maxWidth(), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
maxWidth, maxMarginLeft, maxMarginRight, maxXPos);
if (width() > maxWidth) {
setWidth(maxWidth);
m_marginLeft = maxMarginLeft;
m_marginRight = maxMarginRight;
m_frameRect.setX(maxXPos);
}
}
if (!style()->minWidth().isZero()) {
int minWidth;
int minMarginLeft;
int minMarginRight;
int minXPos;
calcAbsoluteHorizontalValues(style()->minWidth(), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
minWidth, minMarginLeft, minMarginRight, minXPos);
if (width() < minWidth) {
setWidth(minWidth);
m_marginLeft = minMarginLeft;
m_marginRight = minMarginRight;
m_frameRect.setX(minXPos);
}
}
if (stretchesToMinIntrinsicWidth() && width() < minPrefWidth() - bordersPlusPadding) {
calcAbsoluteHorizontalValues(Length(minPrefWidth() - bordersPlusPadding, Fixed), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
widthResult, m_marginLeft, m_marginRight, xResult);
setWidth(widthResult);
setX(xResult);
}
setWidth(width() + bordersPlusPadding);
}
void RenderBox::calcAbsoluteHorizontalValues(Length width, const RenderBoxModelObject* containerBlock, TextDirection containerDirection,
const int containerWidth, const int bordersPlusPadding,
const Length left, const Length right, const Length marginLeft, const Length marginRight,
int& widthValue, int& marginLeftValue, int& marginRightValue, int& xPos)
{
ASSERT(!(left.isAuto() && right.isAuto()));
int leftValue = 0;
bool widthIsAuto = width.isIntrinsicOrAuto();
bool leftIsAuto = left.isAuto();
bool rightIsAuto = right.isAuto();
if (!leftIsAuto && !widthIsAuto && !rightIsAuto) {
leftValue = left.calcValue(containerWidth);
widthValue = calcContentBoxWidth(width.calcValue(containerWidth));
const int availableSpace = containerWidth - (leftValue + widthValue + right.calcValue(containerWidth) + bordersPlusPadding);
if (marginLeft.isAuto() && marginRight.isAuto()) {
if (availableSpace >= 0) {
marginLeftValue = availableSpace / 2; marginRightValue = availableSpace - marginLeftValue; } else {
if (containerDirection == LTR) {
marginLeftValue = 0;
marginRightValue = availableSpace; } else {
marginLeftValue = availableSpace; marginRightValue = 0;
}
}
} else if (marginLeft.isAuto()) {
marginRightValue = marginRight.calcValue(containerWidth);
marginLeftValue = availableSpace - marginRightValue;
} else if (marginRight.isAuto()) {
marginLeftValue = marginLeft.calcValue(containerWidth);
marginRightValue = availableSpace - marginLeftValue;
} else {
marginLeftValue = marginLeft.calcValue(containerWidth);
marginRightValue = marginRight.calcValue(containerWidth);
if (containerDirection == RTL)
leftValue = (availableSpace + leftValue) - marginLeftValue - marginRightValue;
}
} else {
marginLeftValue = marginLeft.calcMinValue(containerWidth);
marginRightValue = marginRight.calcMinValue(containerWidth);
const int availableSpace = containerWidth - (marginLeftValue + marginRightValue + bordersPlusPadding);
if (leftIsAuto && widthIsAuto && !rightIsAuto) {
int rightValue = right.calcValue(containerWidth);
int preferredWidth = maxPrefWidth() - bordersPlusPadding;
int preferredMinWidth = minPrefWidth() - bordersPlusPadding;
int availableWidth = availableSpace - rightValue;
widthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
leftValue = availableSpace - (widthValue + rightValue);
} else if (!leftIsAuto && widthIsAuto && rightIsAuto) {
leftValue = left.calcValue(containerWidth);
int preferredWidth = maxPrefWidth() - bordersPlusPadding;
int preferredMinWidth = minPrefWidth() - bordersPlusPadding;
int availableWidth = availableSpace - leftValue;
widthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
} else if (leftIsAuto && !width.isAuto() && !rightIsAuto) {
widthValue = calcContentBoxWidth(width.calcValue(containerWidth));
leftValue = availableSpace - (widthValue + right.calcValue(containerWidth));
} else if (!leftIsAuto && widthIsAuto && !rightIsAuto) {
leftValue = left.calcValue(containerWidth);
widthValue = availableSpace - (leftValue + right.calcValue(containerWidth));
} else if (!leftIsAuto&& !widthIsAuto && rightIsAuto) {
leftValue = left.calcValue(containerWidth);
widthValue = calcContentBoxWidth(width.calcValue(containerWidth));
}
}
if (containerBlock->isRenderInline() && containerBlock->style()->direction() == RTL) {
const RenderInline* flow = toRenderInline(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
xPos = leftValue + marginLeftValue + lastLine->borderLeft() + (lastLine->x() - firstLine->x());
return;
}
}
xPos = leftValue + marginLeftValue + containerBlock->borderLeft();
}
void RenderBox::calcAbsoluteVertical()
{
if (isReplaced()) {
calcAbsoluteVerticalReplaced();
return;
}
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerHeight = containingBlockHeightForPositioned(containerBlock);
const int bordersPlusPadding = borderTop() + borderBottom() + paddingTop() + paddingBottom();
const Length marginTop = style()->marginTop();
const Length marginBottom = style()->marginBottom();
Length top = style()->top();
Length bottom = style()->bottom();
if (top.isAuto() && bottom.isAuto()) {
int staticTop = layer()->staticY() - containerBlock->borderTop();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent()) {
if (po->isBox() && !po->isTableRow())
staticTop += toRenderBox(po)->y();
}
top.setValue(Fixed, staticTop);
}
int h; int y;
calcAbsoluteVerticalValues(style()->height(), containerBlock, containerHeight, bordersPlusPadding,
top, bottom, marginTop, marginBottom,
h, m_marginTop, m_marginBottom, y);
setY(y);
if (!style()->maxHeight().isUndefined()) {
int maxHeight;
int maxMarginTop;
int maxMarginBottom;
int maxYPos;
calcAbsoluteVerticalValues(style()->maxHeight(), containerBlock, containerHeight, bordersPlusPadding,
top, bottom, marginTop, marginBottom,
maxHeight, maxMarginTop, maxMarginBottom, maxYPos);
if (h > maxHeight) {
h = maxHeight;
m_marginTop = maxMarginTop;
m_marginBottom = maxMarginBottom;
m_frameRect.setY(maxYPos);
}
}
if (!style()->minHeight().isZero()) {
int minHeight;
int minMarginTop;
int minMarginBottom;
int minYPos;
calcAbsoluteVerticalValues(style()->minHeight(), containerBlock, containerHeight, bordersPlusPadding,
top, bottom, marginTop, marginBottom,
minHeight, minMarginTop, minMarginBottom, minYPos);
if (h < minHeight) {
h = minHeight;
m_marginTop = minMarginTop;
m_marginBottom = minMarginBottom;
m_frameRect.setY(minYPos);
}
}
setHeight(h + bordersPlusPadding);
}
void RenderBox::calcAbsoluteVerticalValues(Length h, const RenderBoxModelObject* containerBlock,
const int containerHeight, const int bordersPlusPadding,
const Length top, const Length bottom, const Length marginTop, const Length marginBottom,
int& heightValue, int& marginTopValue, int& marginBottomValue, int& yPos)
{
ASSERT(!(top.isAuto() && bottom.isAuto()));
int contentHeight = height() - bordersPlusPadding;
int topValue = 0;
bool heightIsAuto = h.isAuto();
bool topIsAuto = top.isAuto();
bool bottomIsAuto = bottom.isAuto();
if (isTable()) {
h.setValue(Fixed, contentHeight);
heightIsAuto = false;
}
if (!topIsAuto && !heightIsAuto && !bottomIsAuto) {
heightValue = calcContentBoxHeight(h.calcValue(containerHeight));
topValue = top.calcValue(containerHeight);
const int availableSpace = containerHeight - (topValue + heightValue + bottom.calcValue(containerHeight) + bordersPlusPadding);
if (marginTop.isAuto() && marginBottom.isAuto()) {
marginTopValue = availableSpace / 2; marginBottomValue = availableSpace - marginTopValue; } else if (marginTop.isAuto()) {
marginBottomValue = marginBottom.calcValue(containerHeight);
marginTopValue = availableSpace - marginBottomValue;
} else if (marginBottom.isAuto()) {
marginTopValue = marginTop.calcValue(containerHeight);
marginBottomValue = availableSpace - marginTopValue;
} else {
marginTopValue = marginTop.calcValue(containerHeight);
marginBottomValue = marginBottom.calcValue(containerHeight);
}
} else {
marginTopValue = marginTop.calcMinValue(containerHeight);
marginBottomValue = marginBottom.calcMinValue(containerHeight);
const int availableSpace = containerHeight - (marginTopValue + marginBottomValue + bordersPlusPadding);
if (topIsAuto && heightIsAuto && !bottomIsAuto) {
heightValue = contentHeight;
topValue = availableSpace - (heightValue + bottom.calcValue(containerHeight));
} else if (!topIsAuto && heightIsAuto && bottomIsAuto) {
topValue = top.calcValue(containerHeight);
heightValue = contentHeight;
} else if (topIsAuto && !heightIsAuto && !bottomIsAuto) {
heightValue = calcContentBoxHeight(h.calcValue(containerHeight));
topValue = availableSpace - (heightValue + bottom.calcValue(containerHeight));
} else if (!topIsAuto && heightIsAuto && !bottomIsAuto) {
topValue = top.calcValue(containerHeight);
heightValue = max(0, availableSpace - (topValue + bottom.calcValue(containerHeight)));
} else if (!topIsAuto && !heightIsAuto && bottomIsAuto) {
heightValue = calcContentBoxHeight(h.calcValue(containerHeight));
topValue = top.calcValue(containerHeight);
}
}
yPos = topValue + marginTopValue + containerBlock->borderTop();
}
void RenderBox::calcAbsoluteHorizontalReplaced()
{
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerWidth = containingBlockWidthForPositioned(containerBlock);
TextDirection containerDirection = (style()->htmlHacks()) ? parent()->style()->direction() : containerBlock->style()->direction();
Length left = style()->left();
Length right = style()->right();
Length marginLeft = style()->marginLeft();
Length marginRight = style()->marginRight();
setWidth(calcReplacedWidth() + borderLeft() + borderRight() + paddingLeft() + paddingRight());
const int availableSpace = containerWidth - width();
if (left.isAuto() && right.isAuto()) {
if (containerDirection == LTR) {
int staticPosition = layer()->staticX() - containerBlock->borderLeft();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent()) {
if (po->isBox())
staticPosition += toRenderBox(po)->x();
}
left.setValue(Fixed, staticPosition);
} else {
RenderObject* po = parent();
int staticPosition = layer()->staticX() + containerWidth + containerBlock->borderRight();
for ( ; po && po != containerBlock; po = po->parent()) {
if (po->isBox())
staticPosition += toRenderBox(po)->x();
}
right.setValue(Fixed, staticPosition);
}
}
if (left.isAuto() || right.isAuto()) {
if (marginLeft.isAuto())
marginLeft.setValue(Fixed, 0);
if (marginRight.isAuto())
marginRight.setValue(Fixed, 0);
}
int leftValue = 0;
int rightValue = 0;
if (marginLeft.isAuto() && marginRight.isAuto()) {
ASSERT(!(left.isAuto() && right.isAuto()));
leftValue = left.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
int difference = availableSpace - (leftValue + rightValue);
if (difference > 0) {
m_marginLeft = difference / 2; m_marginRight = difference - m_marginLeft; } else {
if (containerDirection == LTR) {
m_marginLeft = 0;
m_marginRight = difference; } else {
m_marginLeft = difference; m_marginRight = 0;
}
}
} else if (left.isAuto()) {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = marginRight.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
leftValue = availableSpace - (rightValue + m_marginLeft + m_marginRight);
} else if (right.isAuto()) {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = marginRight.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
rightValue = availableSpace - (leftValue + m_marginLeft + m_marginRight);
} else if (marginLeft.isAuto()) {
m_marginRight = marginRight.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
m_marginLeft = availableSpace - (leftValue + rightValue + m_marginRight);
} else if (marginRight.isAuto()) {
m_marginLeft = marginLeft.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
m_marginRight = availableSpace - (leftValue + rightValue + m_marginLeft);
} else {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = marginRight.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
}
int totalWidth = width() + leftValue + rightValue + m_marginLeft + m_marginRight;
if (totalWidth > containerWidth && (containerDirection == RTL))
leftValue = containerWidth - (totalWidth - leftValue);
if (containerBlock->isRenderInline() && containerBlock->style()->direction() == RTL) {
const RenderInline* flow = toRenderInline(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
m_frameRect.setX(leftValue + m_marginLeft + lastLine->borderLeft() + (lastLine->x() - firstLine->x()));
return;
}
}
m_frameRect.setX(leftValue + m_marginLeft + containerBlock->borderLeft());
}
void RenderBox::calcAbsoluteVerticalReplaced()
{
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerHeight = containingBlockHeightForPositioned(containerBlock);
Length top = style()->top();
Length bottom = style()->bottom();
Length marginTop = style()->marginTop();
Length marginBottom = style()->marginBottom();
setHeight(calcReplacedHeight() + borderTop() + borderBottom() + paddingTop() + paddingBottom());
const int availableSpace = containerHeight - height();
if (top.isAuto() && bottom.isAuto()) {
int staticTop = layer()->staticY() - containerBlock->borderTop();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent()) {
if (po->isBox() && !po->isTableRow())
staticTop += toRenderBox(po)->y();
}
top.setValue(Fixed, staticTop);
}
if (top.isAuto() || bottom.isAuto()) {
if (marginTop.isAuto())
marginTop.setValue(Fixed, 0);
if (marginBottom.isAuto())
marginBottom.setValue(Fixed, 0);
}
int topValue = 0;
int bottomValue = 0;
if (marginTop.isAuto() && marginBottom.isAuto()) {
ASSERT(!(top.isAuto() || bottom.isAuto()));
topValue = top.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
int difference = availableSpace - (topValue + bottomValue);
m_marginTop = difference / 2; m_marginBottom = difference - m_marginTop;
} else if (top.isAuto()) {
m_marginTop = marginTop.calcValue(containerHeight);
m_marginBottom = marginBottom.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
topValue = availableSpace - (bottomValue + m_marginTop + m_marginBottom);
} else if (bottom.isAuto()) {
m_marginTop = marginTop.calcValue(containerHeight);
m_marginBottom = marginBottom.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
} else if (marginTop.isAuto()) {
m_marginBottom = marginBottom.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
m_marginTop = availableSpace - (topValue + bottomValue + m_marginBottom);
} else if (marginBottom.isAuto()) {
m_marginTop = marginTop.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
m_marginBottom = availableSpace - (topValue + bottomValue + m_marginTop);
} else {
m_marginTop = marginTop.calcValue(containerHeight);
m_marginBottom = marginBottom.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
}
m_frameRect.setY(topValue + m_marginTop + containerBlock->borderTop());
}
IntRect RenderBox::localCaretRect(InlineBox* box, int caretOffset, int* extraWidthToEndOfLine)
{
IntRect rect(x(), y(), caretWidth, height());
TextDirection direction = box ? box->direction() : style()->direction();
if ((!caretOffset) ^ (direction == LTR))
rect.move(IntSize(width() - caretWidth, 0));
if (box) {
RootInlineBox* rootBox = box->root();
int top = rootBox->topOverflow();
rect.setY(top);
rect.setHeight(rootBox->bottomOverflow() - top);
}
int fontHeight = style()->font().height();
if (fontHeight > rect.height() || (!isReplaced() && !isTable()))
rect.setHeight(fontHeight);
if (extraWidthToEndOfLine)
*extraWidthToEndOfLine = x() + width() - rect.right();
rect.move(-x(), -y());
return rect;
}
int RenderBox::lowestPosition(bool , bool includeSelf) const
{
if (!includeSelf || !width())
return 0;
int bottom = height();
if (isRelPositioned())
bottom += relativePositionOffsetY();
return bottom;
}
int RenderBox::rightmostPosition(bool , bool includeSelf) const
{
if (!includeSelf || !height())
return 0;
int right = width();
if (isRelPositioned())
right += relativePositionOffsetX();
return right;
}
int RenderBox::leftmostPosition(bool , bool includeSelf) const
{
if (!includeSelf || !height())
return width();
int left = 0;
if (isRelPositioned())
left += relativePositionOffsetX();
return left;
}
bool RenderBox::isAfterContent(RenderObject* child) const
{
return (child && child->style()->styleType() == AFTER && (!child->isText() || child->isBR()));
}
VisiblePosition RenderBox::positionForPoint(const IntPoint& point)
{
if (!firstChild())
return createVisiblePosition(firstDeepEditingPositionForNode(node()));
int xPos = point.x();
int yPos = point.y();
if (isTable() && node()) {
int right = contentWidth() + borderRight() + paddingRight() + borderLeft() + paddingLeft();
int bottom = contentHeight() + borderTop() + paddingTop() + borderBottom() + paddingBottom();
if (xPos < 0 || xPos > right || yPos < 0 || yPos > bottom) {
if (xPos <= right / 2)
return createVisiblePosition(firstDeepEditingPositionForNode(node()));
return createVisiblePosition(lastDeepEditingPositionForNode(node()));
}
}
int minDist = INT_MAX;
RenderBox* closestRenderer = 0;
int newX = xPos;
int newY = yPos;
if (isTableRow()) {
newX += x();
newY += y();
}
for (RenderObject* renderObject = firstChild(); renderObject; renderObject = renderObject->nextSibling()) {
if ((!renderObject->firstChild() && !renderObject->isInline() && !renderObject->isBlockFlow() )
|| renderObject->style()->visibility() != VISIBLE)
continue;
if (!renderObject->isBox())
continue;
RenderBox* renderer = toRenderBox(renderObject);
int top = renderer->borderTop() + renderer->paddingTop() + (isTableRow() ? 0 : renderer->y());
int bottom = top + renderer->contentHeight();
int left = renderer->borderLeft() + renderer->paddingLeft() + (isTableRow() ? 0 : renderer->x());
int right = left + renderer->contentWidth();
if (xPos <= right && xPos >= left && yPos <= top && yPos >= bottom) {
if (renderer->isTableRow())
return renderer->positionForCoordinates(xPos + newX - renderer->x(), yPos + newY - renderer->y());
return renderer->positionForCoordinates(xPos - renderer->x(), yPos - renderer->y());
}
IntPoint cmp;
if (xPos > right) {
if (yPos < top)
cmp = IntPoint(right, top);
else if (yPos > bottom)
cmp = IntPoint(right, bottom);
else
cmp = IntPoint(right, yPos);
} else if (xPos < left) {
if (yPos < top)
cmp = IntPoint(left, top);
else if (yPos > bottom)
cmp = IntPoint(left, bottom);
else
cmp = IntPoint(left, yPos);
} else {
if (yPos < top)
cmp = IntPoint(xPos, top);
else
cmp = IntPoint(xPos, bottom);
}
int x1minusx2 = cmp.x() - xPos;
int y1minusy2 = cmp.y() - yPos;
int dist = x1minusx2 * x1minusx2 + y1minusy2 * y1minusy2;
if (dist < minDist) {
closestRenderer = renderer;
minDist = dist;
}
}
if (closestRenderer)
return closestRenderer->positionForCoordinates(newX - closestRenderer->x(), newY - closestRenderer->y());
return createVisiblePosition(firstDeepEditingPositionForNode(node()));
}
bool RenderBox::shrinkToAvoidFloats() const
{
if ((isInline() && !isHTMLMarquee()) || !avoidsFloats())
return false;
return style()->width().isAuto();
}
bool RenderBox::avoidsFloats() const
{
return isReplaced() || hasOverflowClip() || isHR();
}
#if ENABLE(SVG)
TransformationMatrix RenderBox::localTransform() const
{
return TransformationMatrix(1, 0, 0, 1, x(), y());
}
#endif
}