RenderReplaced.cpp [plain text]
#include "config.h"
#include "RenderReplaced.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "LayoutRepainter.h"
#include "Page.h"
#include "RenderBlock.h"
#include "RenderLayer.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "VisiblePosition.h"
using namespace std;
namespace WebCore {
const int cDefaultWidth = 300;
const int cDefaultHeight = 150;
RenderReplaced::RenderReplaced(Node* node)
: RenderBox(node)
, m_intrinsicSize(cDefaultWidth, cDefaultHeight)
{
setReplaced(true);
}
RenderReplaced::RenderReplaced(Node* node, const IntSize& intrinsicSize)
: RenderBox(node)
, m_intrinsicSize(intrinsicSize)
{
setReplaced(true);
}
RenderReplaced::~RenderReplaced()
{
}
void RenderReplaced::willBeDestroyed()
{
if (!documentBeingDestroyed() && parent())
parent()->dirtyLinesFromChangedChild(this);
RenderBox::willBeDestroyed();
}
void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBox::styleDidChange(diff, oldStyle);
bool hadStyle = (oldStyle != 0);
float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
if (style() && style()->effectiveZoom() != oldZoom)
intrinsicSizeChanged();
}
void RenderReplaced::layout()
{
ASSERT(needsLayout());
LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
setHeight(minimumReplacedHeight());
computeLogicalWidth();
computeLogicalHeight();
m_overflow.clear();
addVisualEffectOverflow();
updateLayerTransform();
repainter.repaintAfterLayout();
setNeedsLayout(false);
}
void RenderReplaced::intrinsicSizeChanged()
{
int scaledWidth = static_cast<int>(cDefaultWidth * style()->effectiveZoom());
int scaledHeight = static_cast<int>(cDefaultHeight * style()->effectiveZoom());
m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
setNeedsLayoutAndPrefWidthsRecalc();
}
void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!shouldPaint(paintInfo, paintOffset))
return;
LayoutPoint adjustedPaintOffset = paintOffset + location();
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, adjustedPaintOffset);
if (paintInfo.phase == PaintPhaseMask) {
paintMask(paintInfo, adjustedPaintOffset);
return;
}
LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size());
if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style()->outlineWidth())
paintOutline(paintInfo.context, paintRect);
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && !canHaveChildren())
return;
if (!paintInfo.shouldPaintWithinRoot(this))
return;
bool drawSelectionTint = selectionState() != SelectionNone && !document()->printing();
if (paintInfo.phase == PaintPhaseSelection) {
if (selectionState() == SelectionNone)
return;
drawSelectionTint = false;
}
bool completelyClippedOut = false;
if (style()->hasBorderRadius()) {
LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
if (borderRect.isEmpty())
completelyClippedOut = true;
else {
paintInfo.context->save();
paintInfo.context->addRoundedRectClip(style()->getRoundedBorderFor(paintRect, view()));
}
}
if (!completelyClippedOut) {
paintReplaced(paintInfo, adjustedPaintOffset);
if (style()->hasBorderRadius())
paintInfo.context->restore();
}
if (drawSelectionTint) {
LayoutRect selectionPaintingRect = localSelectionRect();
selectionPaintingRect.moveBy(adjustedPaintOffset);
paintInfo.context->fillRect(pixelSnappedIntRect(selectionPaintingRect), selectionBackgroundColor(), style()->colorSpace());
}
}
bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
&& paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask)
return false;
if (!paintInfo.shouldPaintWithinRoot(this))
return false;
if (style()->visibility() != VISIBLE)
return false;
LayoutPoint adjustedPaintOffset = paintOffset + location();
LayoutUnit top = adjustedPaintOffset.y() + minYVisualOverflow();
LayoutUnit bottom = adjustedPaintOffset.y() + maxYVisualOverflow();
if (isSelected() && m_inlineBoxWrapper) {
LayoutUnit selTop = paintOffset.y() + m_inlineBoxWrapper->root()->selectionTop();
LayoutUnit selBottom = paintOffset.y() + selTop + m_inlineBoxWrapper->root()->selectionHeight();
top = min(selTop, top);
bottom = max(selBottom, bottom);
}
LayoutRect localRepaintRect = paintInfo.rect;
localRepaintRect.inflate(maximalOutlineSize(paintInfo.phase));
if (adjustedPaintOffset.x() + minXVisualOverflow() >= localRepaintRect.maxX() || adjustedPaintOffset.x() + maxXVisualOverflow() <= localRepaintRect.x())
return false;
if (top >= localRepaintRect.maxY() || bottom <= localRepaintRect.y())
return false;
return true;
}
static inline RenderBlock* firstContainingBlockWithLogicalWidth(const RenderReplaced* replaced)
{
RenderBlock* containingBlock = replaced->containingBlock();
if (!containingBlock)
return 0;
for (; !containingBlock->isRenderView() && !containingBlock->isBody(); containingBlock = containingBlock->containingBlock()) {
if (containingBlock->style()->logicalWidth().isSpecified())
return containingBlock;
}
return 0;
}
bool RenderReplaced::hasReplacedLogicalWidth() const
{
if (style()->logicalWidth().isSpecified())
return true;
if (style()->logicalWidth().isAuto())
return false;
return firstContainingBlockWithLogicalWidth(this);
}
static inline bool hasAutoHeightOrContainingBlockWithAutoHeight(const RenderReplaced* replaced)
{
Length logicalHeightLength = replaced->style()->logicalHeight();
if (logicalHeightLength.isAuto())
return true;
if (!logicalHeightLength.isPercent() || replaced->isPositioned() || replaced->document()->inQuirksMode())
return false;
for (RenderBlock* cb = replaced->containingBlock(); !cb->isRenderView(); cb = cb->containingBlock()) {
if (cb->isTableCell() || (!cb->style()->logicalHeight().isAuto() || (!cb->style()->top().isAuto() && !cb->style()->bottom().isAuto())))
return false;
}
return true;
}
bool RenderReplaced::hasReplacedLogicalHeight() const
{
if (style()->logicalHeight().isAuto())
return false;
if (style()->logicalHeight().isSpecified()) {
if (hasAutoHeightOrContainingBlockWithAutoHeight(this))
return false;
return true;
}
return false;
}
static inline bool rendererHasAspectRatio(const RenderObject* renderer)
{
ASSERT(renderer);
return renderer->isImage() || renderer->isCanvas() || renderer->isVideo();
}
void RenderReplaced::computeIntrinsicRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
{
if (contentRenderer) {
contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
if (intrinsicRatio)
ASSERT(!isPercentageIntrinsicSize);
if (!isPercentageIntrinsicSize)
intrinsicSize.scale(style()->effectiveZoom());
if (intrinsicRatio && !isHorizontalWritingMode())
intrinsicRatio = 1 / intrinsicRatio;
if (rendererHasAspectRatio(this) && isPercentageIntrinsicSize)
intrinsicRatio = 1;
return;
}
computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
ASSERT(!isPercentageIntrinsicSize);
}
void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
{
ASSERT(!embeddedContentBox());
isPercentageIntrinsicSize = false;
intrinsicSize = FloatSize(intrinsicLogicalWidth(), intrinsicLogicalHeight());
if (intrinsicSize.isEmpty() || !rendererHasAspectRatio(this))
return;
intrinsicRatio = intrinsicSize.width() / intrinsicSize.height();
if (style()->logicalWidth().isAuto() && style()->logicalHeight().isAuto()) {
intrinsicSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicLogicalWidth() / intrinsicLogicalHeight());
intrinsicSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicLogicalHeight() / intrinsicLogicalWidth());
}
}
LayoutUnit RenderReplaced::computeReplacedLogicalWidth(bool includeMaxWidth) const
{
if (style()->logicalWidth().isSpecified())
return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(style()->logicalWidth()), includeMaxWidth);
RenderBox* contentRenderer = embeddedContentBox();
bool isPercentageIntrinsicSize = false;
double intrinsicRatio = 0;
FloatSize intrinsicSize;
computeIntrinsicRatioInformationForRenderBox(contentRenderer, intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
if (style()->logicalWidth().isAuto()) {
bool heightIsAuto = style()->logicalHeight().isAuto();
bool hasIntrinsicWidth = !isPercentageIntrinsicSize && intrinsicSize.width() > 0;
if (heightIsAuto && hasIntrinsicWidth)
return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(intrinsicSize.width()), includeMaxWidth);
bool hasIntrinsicHeight = !isPercentageIntrinsicSize && intrinsicSize.height() > 0;
if (intrinsicRatio || isPercentageIntrinsicSize) {
if (intrinsicRatio && ((heightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !heightIsAuto)) {
LayoutUnit logicalHeight = computeReplacedLogicalHeightUsing(style()->logicalHeight());
return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)));
}
if (heightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight && contentRenderer) {
LayoutUnit logicalWidth;
if (RenderBlock* blockWithWidth = firstContainingBlockWithLogicalWidth(this))
logicalWidth = blockWithWidth->computeReplacedLogicalWidthRespectingMinMaxWidth(blockWithWidth->computeReplacedLogicalWidthUsing(blockWithWidth->style()->logicalWidth()), false);
else
logicalWidth = containingBlock()->availableLogicalWidth();
LayoutUnit marginStart = minimumValueForLength(style()->marginStart(), logicalWidth);
LayoutUnit marginEnd = minimumValueForLength(style()->marginEnd(), logicalWidth);
logicalWidth = max(ZERO_LAYOUT_UNIT, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
if (isPercentageIntrinsicSize)
logicalWidth = roundToInt(logicalWidth * intrinsicSize.width() / 100);
return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, includeMaxWidth);
}
}
if (hasIntrinsicWidth)
return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(intrinsicSize.width()), includeMaxWidth);
}
return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), includeMaxWidth);
}
LayoutUnit RenderReplaced::computeReplacedLogicalHeight() const
{
if (hasReplacedLogicalHeight())
return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(style()->logicalHeight()));
RenderBox* contentRenderer = embeddedContentBox();
bool isPercentageIntrinsicSize = false;
double intrinsicRatio = 0;
FloatSize intrinsicSize;
computeIntrinsicRatioInformationForRenderBox(contentRenderer, intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
bool widthIsAuto = style()->logicalWidth().isAuto();
bool hasIntrinsicHeight = !isPercentageIntrinsicSize && intrinsicSize.height() > 0;
if (widthIsAuto && hasIntrinsicHeight)
return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(intrinsicSize.height()));
if (intrinsicRatio)
return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(availableLogicalWidth() / intrinsicRatio)));
if (hasIntrinsicHeight)
return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(intrinsicSize.height()));
return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight());
}
void RenderReplaced::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
LayoutUnit borderAndPadding = borderAndPaddingWidth();
m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(false) + borderAndPadding;
if (style()->maxWidth().isFixed())
m_maxPreferredLogicalWidth = min<LayoutUnit>(m_maxPreferredLogicalWidth, style()->maxWidth().value() + (style()->boxSizing() == CONTENT_BOX ? borderAndPadding : ZERO_LAYOUT_UNIT));
if (hasRelativeDimensions())
m_minPreferredLogicalWidth = 0;
else
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth;
setPreferredLogicalWidthsDirty(false);
}
VisiblePosition RenderReplaced::positionForPoint(const LayoutPoint& point)
{
InlineBox* box = inlineBoxWrapper();
RootInlineBox* rootBox = box ? box->root() : 0;
LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop();
LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom();
LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x();
LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y();
if (blockDirectionPosition < top)
return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
if (blockDirectionPosition >= bottom)
return createVisiblePosition(caretMaxOffset(), DOWNSTREAM);
if (node()) {
if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
return createVisiblePosition(0, DOWNSTREAM);
return createVisiblePosition(1, DOWNSTREAM);
}
return RenderBox::positionForPoint(point);
}
LayoutRect RenderReplaced::selectionRectForRepaint(RenderBoxModelObject* repaintContainer, bool clipToVisibleContent)
{
ASSERT(!needsLayout());
if (!isSelected())
return LayoutRect();
LayoutRect rect = localSelectionRect();
if (clipToVisibleContent)
computeRectForRepaint(repaintContainer, rect);
else
rect = localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
return rect;
}
LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const
{
if (checkWhetherSelected && !isSelected())
return LayoutRect();
if (!m_inlineBoxWrapper)
return LayoutRect(LayoutPoint(), size());
RootInlineBox* root = m_inlineBoxWrapper->root();
LayoutUnit newLogicalTop = root->block()->style()->isFlippedBlocksWritingMode() ? m_inlineBoxWrapper->logicalBottom() - root->selectionBottom() : root->selectionTop() - m_inlineBoxWrapper->logicalTop();
if (root->block()->style()->isHorizontalWritingMode())
return LayoutRect(0, newLogicalTop, width(), root->selectionHeight());
return LayoutRect(newLogicalTop, 0, root->selectionHeight(), height());
}
void RenderReplaced::setSelectionState(SelectionState state)
{
RenderBox::setSelectionState(state);
if (m_inlineBoxWrapper && canUpdateSelectionOnRootLineBoxes())
if (RootInlineBox* root = m_inlineBoxWrapper->root())
root->setHasSelectedChildren(isSelected());
}
bool RenderReplaced::isSelected() const
{
SelectionState s = selectionState();
if (s == SelectionNone)
return false;
if (s == SelectionInside)
return true;
int selectionStart, selectionEnd;
selectionStartEnd(selectionStart, selectionEnd);
if (s == SelectionStart)
return selectionStart == 0;
int end = node()->hasChildNodes() ? node()->childNodeCount() : 1;
if (s == SelectionEnd)
return selectionEnd == end;
if (s == SelectionBoth)
return selectionStart == 0 && selectionEnd == end;
ASSERT(0);
return false;
}
LayoutRect RenderReplaced::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
{
if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return LayoutRect();
LayoutRect r = unionRect(localSelectionRect(false), visualOverflowRect());
RenderView* v = view();
if (v) {
r.move(v->layoutDelta());
}
if (style()) {
if (v)
r.inflate(style()->outlineSize());
}
computeRectForRepaint(repaintContainer, r);
return r;
}
}