#include "config.h"
#include "RenderLayer.h"
#include "CSSPropertyNames.h"
#include "Document.h"
#include "EventNames.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameTree.h"
#include "GraphicsContext.h"
#include "HTMLMarqueeElement.h"
#include "HTMLNames.h"
#include "OverflowEvent.h"
#include "PlatformMouseEvent.h"
#include "RenderArena.h"
#include "RenderFormElement.h"
#include "RenderInline.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "SelectionController.h"
#if SVG_SUPPORT
#include "SVGNames.h"
#endif
#define LINE_STEP 40
#define PAGE_KEEP 40
#define MIN_INTERSECT_FOR_REVEAL 32
using namespace std;
namespace WebCore {
using namespace EventNames;
using namespace HTMLNames;
ScrollBar* RenderLayer::gScrollBar = 0;
#ifndef NDEBUG
static bool inRenderLayerDestroy;
#endif
const RenderLayer::ScrollAlignment RenderLayer::gAlignCenterIfNeeded = { RenderLayer::noScroll, RenderLayer::alignCenter, RenderLayer::alignToClosestEdge };
const RenderLayer::ScrollAlignment RenderLayer::gAlignToEdgeIfNeeded = { RenderLayer::noScroll, RenderLayer::alignToClosestEdge, RenderLayer::alignToClosestEdge };
const RenderLayer::ScrollAlignment RenderLayer::gAlignCenterAlways = { RenderLayer::alignCenter, RenderLayer::alignCenter, RenderLayer::alignCenter };
const RenderLayer::ScrollAlignment RenderLayer::gAlignTopAlways = { RenderLayer::alignTop, RenderLayer::alignTop, RenderLayer::alignTop };
const RenderLayer::ScrollAlignment RenderLayer::gAlignBottomAlways = { RenderLayer::alignBottom, RenderLayer::alignBottom, RenderLayer::alignBottom };
const int MinimumWidthWhileResizing = 100;
const int MinimumHeightWhileResizing = 40;
void* ClipRects::operator new(size_t sz, RenderArena* renderArena) throw()
{
return renderArena->allocate(sz);
}
void ClipRects::operator delete(void* ptr, size_t sz)
{
*(size_t *)ptr = sz;
}
void ClipRects::destroy(RenderArena* renderArena)
{
delete this;
renderArena->free(*(size_t *)this, this);
}
RenderLayer::RenderLayer(RenderObject* object)
: m_object(object),
m_parent(0),
m_previous(0),
m_next(0),
m_first(0),
m_last(0),
m_relX(0),
m_relY(0),
m_x(0),
m_y(0),
m_width(0),
m_height(0),
m_scrollX(0),
m_scrollY(0),
m_scrollOriginX(0),
m_scrollLeftOverflow(0),
m_scrollWidth(0),
m_scrollHeight(0),
m_hBar(0),
m_vBar(0),
m_inResizeMode(false),
m_resizeCornerImage(0),
m_posZOrderList(0),
m_negZOrderList(0),
m_overflowList(0),
m_clipRects(0) ,
m_scrollDimensionsDirty(true),
m_zOrderListsDirty(true),
m_overflowListDirty(true),
m_isOverflowOnly(shouldBeOverflowOnly()),
m_usedTransparency(false),
m_inOverflowRelayout(false),
m_repaintOverflowOnResize(false),
m_overflowStatusDirty(true),
m_marquee(0)
{
}
RenderLayer::~RenderLayer()
{
delete m_hBar;
delete m_vBar;
delete m_resizeCornerImage;
delete m_posZOrderList;
delete m_negZOrderList;
delete m_overflowList;
delete m_marquee;
assert(!m_clipRects);
}
void RenderLayer::checkForRepaintOnResize()
{
m_repaintOverflowOnResize = m_object->selfNeedsLayout() || m_object->hasOverflowClip() && m_object->normalChildNeedsLayout();
for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
child->checkForRepaintOnResize();
}
void RenderLayer::updateLayerPositions(bool doFullRepaint, bool checkForRepaint)
{
if (doFullRepaint) {
m_object->repaint();
checkForRepaint = doFullRepaint = false;
}
updateLayerPosition();
positionResizeControl();
if (m_hBar || m_vBar) {
int x = 0;
int y = 0;
convertToLayerCoords(root(), x, y);
IntRect layerBounds = IntRect(x,y,width(),height());
positionScrollbars(layerBounds);
}
if (m_object->style()->visibility() == VISIBLE) {
int x, y;
m_object->absolutePosition(x, y);
IntRect newRect, newFullRect;
m_object->getAbsoluteRepaintRectIncludingFloats(newRect, newFullRect);
if (checkForRepaint) {
RenderView *c = m_object->view();
if (c && !c->printingMode()) {
bool didMove = x != m_repaintX || y != m_repaintY;
if (!didMove && !m_repaintOverflowOnResize)
m_object->repaintAfterLayoutIfNeeded(m_repaintRect, m_fullRepaintRect);
else if (didMove || newRect != m_repaintRect) {
c->repaintViewRectangle(m_fullRepaintRect);
c->repaintViewRectangle(newFullRect);
}
}
}
m_repaintRect = newRect;
m_fullRepaintRect = newFullRect;
m_repaintX = x;
m_repaintY = y;
} else {
m_repaintRect = IntRect();
m_fullRepaintRect = IntRect();
}
for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
child->updateLayerPositions(doFullRepaint, checkForRepaint);
if (m_marquee)
m_marquee->updateMarqueePosition();
}
void RenderLayer::updateLayerPosition()
{
clearClipRect();
if (renderer()->isRenderView())
return;
int x = m_object->xPos();
int y = m_object->yPos() - m_object->borderTopExtra();
if (!m_object->isPositioned()) {
RenderObject* curr = m_object->parent();
while (curr && !curr->layer()) {
if (!curr->isTableRow()) {
x += curr->xPos();
y += curr->yPos();
}
curr = curr->parent();
}
y += curr->borderTopExtra();
if (curr->isTableRow()) {
x -= curr->xPos();
y -= curr->yPos();
}
}
m_relX = m_relY = 0;
if (m_object->isRelPositioned()) {
m_relX = static_cast<RenderBox*>(m_object)->relativePositionOffsetX();
m_relY = static_cast<RenderBox*>(m_object)->relativePositionOffsetY();
x += m_relX; y += m_relY;
}
if (m_object->isPositioned() && enclosingPositionedAncestor()) {
RenderLayer* positionedParent = enclosingPositionedAncestor();
positionedParent->subtractScrollOffset(x, y);
if (m_object->isPositioned() && positionedParent->renderer()->isRelPositioned() &&
positionedParent->renderer()->isInlineFlow()) {
RenderFlow* flow = static_cast<RenderFlow*>(positionedParent->renderer());
int sx = 0, sy = 0;
if (flow->firstLineBox()) {
sx = flow->firstLineBox()->xPos();
sy = flow->firstLineBox()->yPos();
}
else {
sx = flow->staticX();
sy = flow->staticY();
}
bool isInlineType = m_object->style()->isOriginalDisplayInlineType();
if (!m_object->hasStaticX())
x += sx;
if (m_object->hasStaticX() && !isInlineType)
x += sx - (m_object->containingBlock()->borderLeft() + m_object->containingBlock()->paddingLeft());
if (!m_object->hasStaticY())
y += sy;
}
}
else if (parent())
parent()->subtractScrollOffset(x, y);
setPos(x,y);
setWidth(m_object->width());
setHeight(m_object->height() + m_object->borderTopExtra() + m_object->borderBottomExtra());
if (!m_object->hasOverflowClip()) {
if (m_object->overflowWidth() > m_object->width())
setWidth(m_object->overflowWidth());
if (m_object->overflowHeight() > m_object->height())
setHeight(m_object->overflowHeight());
}
}
RenderLayer *RenderLayer::stackingContext() const
{
RenderLayer* curr = parent();
for ( ; curr && !curr->m_object->isRenderView() && !curr->m_object->isRoot() &&
curr->m_object->style()->hasAutoZIndex();
curr = curr->parent());
return curr;
}
RenderLayer*
RenderLayer::enclosingPositionedAncestor() const
{
RenderLayer* curr = parent();
for ( ; curr && !curr->m_object->isRenderView() && !curr->m_object->isRoot() &&
!curr->m_object->isPositioned() && !curr->m_object->isRelPositioned();
curr = curr->parent());
return curr;
}
bool
RenderLayer::isTransparent() const
{
#if SVG_SUPPORT
if (m_object->node()->namespaceURI() == SVGNames::svgNamespaceURI)
return false;
#endif
return m_object->isTransparent();
}
RenderLayer*
RenderLayer::transparentAncestor()
{
RenderLayer* curr = parent();
for ( ; curr && !curr->isTransparent(); curr = curr->parent());
return curr;
}
static IntRect transparencyClipBox(RenderLayer* l)
{
IntRect clipRect = l->absoluteBoundingBox();
for (RenderLayer* curr = l->firstChild(); curr; curr = curr->nextSibling()) {
if (!curr->isTransparent())
clipRect.unite(curr->absoluteBoundingBox());
}
return clipRect;
}
void RenderLayer::beginTransparencyLayers(GraphicsContext* p, const IntRect& paintDirtyRect)
{
if (p->paintingDisabled() || (isTransparent() && m_usedTransparency))
return;
RenderLayer* ancestor = transparentAncestor();
if (ancestor)
ancestor->beginTransparencyLayers(p, paintDirtyRect);
if (isTransparent()) {
m_usedTransparency = true;
IntRect clipRect = transparencyClipBox(this);
clipRect.intersect(paintDirtyRect);
p->save();
p->addClip(clipRect);
p->beginTransparencyLayer(renderer()->opacity());
}
}
void* RenderLayer::operator new(size_t sz, RenderArena* renderArena) throw()
{
return renderArena->allocate(sz);
}
void RenderLayer::operator delete(void* ptr, size_t sz)
{
assert(inRenderLayerDestroy);
*(size_t *)ptr = sz;
}
void RenderLayer::destroy(RenderArena* renderArena)
{
#ifndef NDEBUG
inRenderLayerDestroy = true;
#endif
delete this;
#ifndef NDEBUG
inRenderLayerDestroy = false;
#endif
renderArena->free(*(size_t *)this, this);
}
void RenderLayer::addChild(RenderLayer *child, RenderLayer* beforeChild)
{
RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
if (prevSibling) {
child->setPreviousSibling(prevSibling);
prevSibling->setNextSibling(child);
}
else
setFirstChild(child);
if (beforeChild) {
beforeChild->setPreviousSibling(child);
child->setNextSibling(beforeChild);
}
else
setLastChild(child);
child->setParent(this);
if (child->isOverflowOnly())
dirtyOverflowList();
else {
RenderLayer* stackingContext = child->stackingContext();
if (stackingContext)
stackingContext->dirtyZOrderLists();
}
}
RenderLayer* RenderLayer::removeChild(RenderLayer* oldChild)
{
if (oldChild->previousSibling())
oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
if (oldChild->nextSibling())
oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
if (m_first == oldChild)
m_first = oldChild->nextSibling();
if (m_last == oldChild)
m_last = oldChild->previousSibling();
if (oldChild->isOverflowOnly())
dirtyOverflowList();
else {
RenderLayer* stackingContext = oldChild->stackingContext();
if (stackingContext)
stackingContext->dirtyZOrderLists();
}
oldChild->setPreviousSibling(0);
oldChild->setNextSibling(0);
oldChild->setParent(0);
return oldChild;
}
void RenderLayer::removeOnlyThisLayer()
{
if (!m_parent)
return;
clearClipRects();
RenderLayer* parent = m_parent;
RenderLayer* nextSib = nextSibling();
parent->removeChild(this);
RenderLayer* current = m_first;
while (current) {
RenderLayer* next = current->nextSibling();
removeChild(current);
parent->addChild(current, nextSib);
current->updateLayerPositions();
current = next;
}
destroy(renderer()->renderArena());
}
void RenderLayer::insertOnlyThisLayer()
{
if (!m_parent && renderer()->parent()) {
RenderLayer* parentLayer = renderer()->parent()->enclosingLayer();
if (parentLayer)
parentLayer->addChild(this,
renderer()->parent()->findNextLayer(parentLayer, renderer()));
}
for (RenderObject* curr = renderer()->firstChild(); curr; curr = curr->nextSibling())
curr->moveLayers(m_parent, this);
clearClipRects();
}
void
RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, int& x, int& y) const
{
if (ancestorLayer == this)
return;
if (m_object->style()->position() == FixedPosition) {
int xOff, yOff;
m_object->absolutePosition(xOff, yOff, true);
x += xOff;
y += yOff;
return;
}
RenderLayer* parentLayer;
if (m_object->style()->position() == AbsolutePosition)
parentLayer = enclosingPositionedAncestor();
else
parentLayer = parent();
if (!parentLayer) return;
parentLayer->convertToLayerCoords(ancestorLayer, x, y);
x += xPos();
y += yPos();
}
void
RenderLayer::scrollOffset(int& x, int& y)
{
x += scrollXOffset() + m_scrollLeftOverflow;
y += scrollYOffset();
}
void
RenderLayer::subtractScrollOffset(int& x, int& y)
{
x -= scrollXOffset() + m_scrollLeftOverflow;
y -= scrollYOffset();
}
void RenderLayer::scrollToOffset(int x, int y, bool updateScrollbars, bool repaint)
{
if (renderer()->style()->overflowX() != OMARQUEE) {
if (x < 0) x = 0;
if (y < 0) y = 0;
int maxX = scrollWidth() - m_object->clientWidth();
int maxY = scrollHeight() - m_object->clientHeight();
if (x > maxX) x = maxX;
if (y > maxY) y = maxY;
static int purpleCaretWidth = 3;
if (adjustForPurpleCaretWhenScrolling()) {
if (x > maxX - purpleCaretWidth) {
x += purpleCaretWidth;
if (x <= purpleCaretWidth)
x = 0;
}
else if (x < m_scrollX - purpleCaretWidth) {
x -= purpleCaretWidth;
}
}
}
m_scrollX = x - m_scrollOriginX;
m_scrollY = y;
for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
child->updateLayerPositions(false, false);
RenderView* view = renderer()->view();
if (view) {
#if __APPLE__
view->frameView()->updateDashboardRegions();
#endif
m_object->view()->updateWidgetPositions();
}
if (repaint)
m_object->repaint();
if (updateScrollbars) {
if (m_hBar)
m_hBar->setValue(scrollXOffset());
if (m_vBar)
m_vBar->setValue(m_scrollY);
}
EventTargetNodeCast(m_object->element())->dispatchHTMLEvent(scrollEvent, true, false);
}
void RenderLayer::scrollRectToVisible(const IntRect &rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
{
RenderLayer* parentLayer = 0;
IntRect newRect = rect;
int xOffset = 0, yOffset = 0;
if (m_object->hasOverflowClip()) {
IntRect layerBounds = IntRect(xPos() + scrollXOffset(), yPos() + scrollYOffset(),
width() - verticalScrollbarWidth(), height() - horizontalScrollbarHeight());
IntRect exposeRect = IntRect(rect.x() + scrollXOffset(), rect.y() + scrollYOffset(), rect.width(), rect.height());
IntRect r = getRectToExpose(layerBounds, exposeRect, alignX, alignY);
xOffset = r.x() - xPos();
yOffset = r.y() - yPos();
xOffset = max(0, min(scrollWidth() - width(), xOffset));
yOffset = max(0, min(scrollHeight() - height(), yOffset));
if (xOffset != scrollXOffset() || yOffset != scrollYOffset()) {
int diffX = scrollXOffset();
int diffY = scrollYOffset();
scrollToOffset(xOffset, yOffset);
diffX = scrollXOffset() - diffX;
diffY = scrollYOffset() - diffY;
newRect.setX(rect.x() - diffX);
newRect.setY(rect.y() - diffY);
}
if (m_object->parent())
parentLayer = m_object->parent()->enclosingLayer();
} else {
FrameView* view = m_object->document()->view();
if (view) {
IntRect viewRect = enclosingIntRect(view->visibleContentRect());
IntRect r = getRectToExpose(viewRect, rect, alignX, alignY);
xOffset = r.x();
yOffset = r.y();
xOffset = max(0, min(view->contentsWidth(), xOffset));
yOffset = max(0, min(view->contentsHeight(), yOffset));
if (m_object->document() && m_object->document()->ownerElement() && m_object->document()->ownerElement()->renderer()) {
view->setContentsPos(xOffset, yOffset);
parentLayer = m_object->document()->ownerElement()->renderer()->enclosingLayer();
newRect.setX(rect.x() - view->contentsX() + view->x());
newRect.setY(rect.y() - view->contentsY() + view->y());
} else {
view->scrollPointRecursively(xOffset, yOffset);
}
}
}
if (parentLayer)
parentLayer->scrollRectToVisible(newRect, alignX, alignY);
}
IntRect RenderLayer::getRectToExpose(const IntRect &visibleRect, const IntRect &exposeRect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
{
ScrollBehavior scrollX;
IntRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
int intersectWidth = intersection(visibleRect, exposeRectX).width();
if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL)
scrollX = getVisibleBehavior(alignX);
else if (intersectWidth == visibleRect.width()) {
scrollX = getVisibleBehavior(alignX);
if (scrollX == alignCenter)
scrollX = noScroll;
} else if (intersectWidth > 0)
scrollX = getPartialBehavior(alignX);
else
scrollX = getHiddenBehavior(alignX);
if (scrollX == alignToClosestEdge && exposeRect.right() > visibleRect.right() && exposeRect.width() < visibleRect.width())
scrollX = alignRight;
int x;
if (scrollX == noScroll)
x = visibleRect.x();
else if (scrollX == alignRight)
x = exposeRect.right() - visibleRect.width();
else if (scrollX == alignCenter)
x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
else
x = exposeRect.x();
ScrollBehavior scrollY;
IntRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
int intersectHeight = intersection(visibleRect, exposeRectY).height();
if (intersectHeight == exposeRect.height())
scrollY = getVisibleBehavior(alignY);
else if (intersectHeight == visibleRect.height()) {
scrollY = getVisibleBehavior(alignY);
if (scrollY == alignCenter)
scrollY = noScroll;
} else if (intersectHeight > 0)
scrollY = getPartialBehavior(alignY);
else
scrollY = getHiddenBehavior(alignY);
if (scrollY == alignToClosestEdge && exposeRect.bottom() > visibleRect.bottom() && exposeRect.height() < visibleRect.height())
scrollY = alignBottom;
int y;
if (scrollY == noScroll)
y = visibleRect.y();
else if (scrollY == alignBottom)
y = exposeRect.bottom() - visibleRect.height();
else if (scrollY == alignCenter)
y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
else
y = exposeRect.y();
return IntRect(IntPoint(x, y), visibleRect.size());
}
void RenderLayer::autoscroll()
{
int xOffset = scrollXOffset();
int yOffset = scrollYOffset();
SelectionController sel = renderer()->document()->frame()->selection();
IntRect extentRect = SelectionController(sel.extent(), sel.affinity()).caretRect();
extentRect.move(xOffset, yOffset);
IntRect bounds = IntRect(xPos() + xOffset, yPos() + yOffset, width() - verticalScrollbarWidth(), height() - horizontalScrollbarHeight());
int diffX = 0;
if (extentRect.right() > bounds.right())
diffX = extentRect.right() - bounds.right();
else if (extentRect.x() < bounds.x())
diffX = extentRect.x() - bounds.x();
int diffY = 0;
if (extentRect.bottom() > bounds.bottom())
diffY = extentRect.bottom() - bounds.bottom();
else if (extentRect.y() < bounds.y())
diffY = extentRect.y() - bounds.y();
scrollToOffset(xOffset + diffX, yOffset + diffY);
}
bool RenderLayer::shouldAutoscroll()
{
if (renderer()->hasOverflowClip() && (m_object->scrollsOverflow() || renderer()->node()->isContentEditable()))
return true;
return false;
}
void RenderLayer::resize(const PlatformMouseEvent& evt, const IntSize& offsetFromResizeCorner)
{
if (!inResizeMode() || !renderer()->hasOverflowClip() || m_object->style()->resize() == RESIZE_NONE)
return;
if (!m_object->document()->frame()->view()->mousePressed())
return;
IntPoint currentPoint = m_object->document()->view()->viewportToContents(evt.pos());
currentPoint += offsetFromResizeCorner;
int x;
int y;
m_object->absolutePosition(x, y);
int right = x + m_object->width();
int bottom = y + m_object->height();
int diffWidth = max(currentPoint.x() - right, min(0, MinimumWidthWhileResizing - m_object->width()));
int diffHeight = max(currentPoint.y() - bottom, min(0, MinimumHeightWhileResizing - m_object->height()));
ExceptionCode ec = 0;
RenderObject* renderer = m_object->node()->shadowAncestorNode()->renderer();
if (diffWidth && (m_object->style()->resize() == RESIZE_HORIZONTAL || m_object->style()->resize() == RESIZE_BOTH)) {
CSSStyleDeclaration* style = static_cast<Element*>(m_object->node()->shadowAncestorNode())->style();
if (renderer->style()->hasAppearance() || renderer->isFormElement() && static_cast<RenderFormElement*>(renderer)->canHaveIntrinsicMargins()) {
style->setProperty(CSS_PROP_MARGIN_LEFT, String::number(renderer->marginLeft()) + "px", false, ec);
style->setProperty(CSS_PROP_MARGIN_RIGHT, String::number(renderer->marginRight()) + "px", false, ec);
}
int baseWidth = renderer->width() - (renderer->style()->boxSizing() == BORDER_BOX ? 0 : renderer->borderLeft() + renderer->paddingLeft() + renderer->borderRight() + renderer->paddingRight());
style->setProperty(CSS_PROP_WIDTH, String::number(baseWidth + diffWidth) + "px", false, ec);
}
if (diffHeight && (m_object->style()->resize() == RESIZE_VERTICAL || m_object->style()->resize() == RESIZE_BOTH)) {
CSSStyleDeclaration* style = static_cast<Element*>(m_object->node()->shadowAncestorNode())->style();
if (renderer->style()->hasAppearance() || renderer->isFormElement() && static_cast<RenderFormElement*>(renderer)->canHaveIntrinsicMargins()) {
style->setProperty(CSS_PROP_MARGIN_TOP, String::number(renderer->marginTop()) + "px", false, ec);
style->setProperty(CSS_PROP_MARGIN_BOTTOM, String::number(renderer->marginBottom()) + "px", false, ec);
}
int baseHeight = renderer->height() - (renderer->style()->boxSizing() == BORDER_BOX ? 0 : renderer->borderTop() + renderer->paddingTop() + renderer->borderBottom() + renderer->paddingBottom());
style->setProperty(CSS_PROP_HEIGHT, String::number(baseHeight + diffHeight) + "px", false, ec);
}
ASSERT(ec == 0);
if (m_object->style()->resize() != RESIZE_NONE) {
m_object->setNeedsLayout(true);
m_object->node()->shadowAncestorNode()->renderer()->setNeedsLayout(true);
m_object->document()->updateLayout();
}
}
void RenderLayer::valueChanged(Widget*)
{
bool needUpdate = false;
int newX = scrollXOffset();
int newY = m_scrollY;
if (m_hBar) {
newX = m_hBar->value();
if (newX != scrollXOffset())
needUpdate = true;
}
if (m_vBar) {
newY = m_vBar->value();
if (newY != m_scrollY)
needUpdate = true;
}
if (needUpdate)
scrollToOffset(newX, newY, false);
}
void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
{
if (hasScrollbar == (m_hBar != 0))
return;
if (hasScrollbar) {
FrameView* scrollView = m_object->element()->document()->view();
m_hBar = new ScrollBar(HorizontalScrollBar);
m_hBar->setClient(this);
scrollView->addChild(m_hBar, 0, -50000);
} else {
FrameView* scrollView = m_object->element()->document()->view();
scrollView->removeChild(m_hBar);
delete m_hBar;
m_hBar = 0;
}
#if __APPLE__
if (m_object->document()->hasDashboardRegions())
m_object->document()->setDashboardRegionsDirty(true);
#endif
}
void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
{
if (hasScrollbar == (m_vBar != 0))
return;
if (hasScrollbar) {
FrameView* scrollView = m_object->document()->view();
m_vBar = new ScrollBar(VerticalScrollBar);
m_vBar->setClient(this);
scrollView->addChild(m_vBar, 0, -50000);
} else {
FrameView* scrollView = m_object->document()->view();
scrollView->removeChild(m_vBar);
delete m_vBar;
m_vBar = 0;
}
#if __APPLE__
if (m_object->document()->hasDashboardRegions())
m_object->document()->setDashboardRegionsDirty(true);
#endif
}
int
RenderLayer::verticalScrollbarWidth()
{
if (!m_vBar)
return 0;
return m_vBar->width();
}
int
RenderLayer::horizontalScrollbarHeight()
{
if (!m_hBar)
return 0;
return m_hBar->height();
}
bool RenderLayer::isPointInResizeControl(const IntPoint& p)
{
return resizeControlRect().contains(IntRect(p, IntSize()));
}
IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& p) const
{
int x = width();
int y = height();
convertToLayerCoords(root(), x, y);
return IntSize(x - p.x(), y - p.y());
}
void RenderLayer::positionResizeControl()
{
if (m_object->hasOverflowClip() && m_object->style()->resize() != RESIZE_NONE) {
int x = 0;
int y = 0;
convertToLayerCoords(root(), x, y);
int scrollbarSize = 15;
setResizeControlRect(IntRect(x + width() - scrollbarSize - m_object->style()->borderRightWidth() - 1,
y + height() - scrollbarSize - m_object->style()->borderBottomWidth() - 1, scrollbarSize, scrollbarSize));
}
}
void
RenderLayer::positionScrollbars(const IntRect& absBounds)
{
int resizeControlSize = max(resizeControlRect().height() - 1, 0);
if (m_vBar) {
m_vBar->move(absBounds.right() - m_object->borderRight() - m_vBar->width(),
absBounds.y() + m_object->borderTop());
m_vBar->resize(m_vBar->width(), absBounds.height() - (m_object->borderTop() + m_object->borderBottom()) - (m_hBar ? m_hBar->height() - 1 : resizeControlSize));
}
resizeControlSize = max(resizeControlRect().width() - 1, 0);
if (m_hBar) {
m_hBar->move(absBounds.x() + m_object->borderLeft(),
absBounds.bottom() - m_object->borderBottom() - m_hBar->height());
m_hBar->resize(absBounds.width() - (m_object->borderLeft() + m_object->borderRight()) - (m_vBar ? m_vBar->width() - 1 : resizeControlSize), m_hBar->height());
}
}
int RenderLayer::scrollWidth()
{
if (m_scrollDimensionsDirty)
computeScrollDimensions();
return m_scrollWidth;
}
int RenderLayer::scrollHeight()
{
if (m_scrollDimensionsDirty)
computeScrollDimensions();
return m_scrollHeight;
}
void RenderLayer::computeScrollDimensions(bool* needHBar, bool* needVBar)
{
m_scrollDimensionsDirty = false;
bool ltr = m_object->style()->direction() == LTR;
int clientWidth = m_object->clientWidth();
int clientHeight = m_object->clientHeight();
m_scrollLeftOverflow = ltr ? 0 : min(0, m_object->leftmostPosition(true, false) - m_object->borderLeft());
int rightPos = ltr ?
m_object->rightmostPosition(true, false) - m_object->borderLeft() :
clientWidth - m_scrollLeftOverflow;
int bottomPos = m_object->lowestPosition(true, false) - m_object->borderTop();
m_scrollWidth = max(rightPos, clientWidth);
m_scrollHeight = max(bottomPos, clientHeight);
m_scrollOriginX = ltr ? 0 : m_scrollWidth - clientWidth;
if (needHBar)
*needHBar = rightPos > clientWidth;
if (needVBar)
*needVBar = bottomPos > clientHeight;
}
void RenderLayer::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
{
if (m_overflowStatusDirty) {
m_horizontalOverflow = horizontalOverflow;
m_verticalOverflow = verticalOverflow;
m_overflowStatusDirty = false;
return;
}
bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
if (horizontalOverflowChanged || verticalOverflowChanged) {
m_horizontalOverflow = horizontalOverflow;
m_verticalOverflow = verticalOverflow;
m_object->element()->document()->frame()->view()->scheduleEvent(new OverflowEvent(horizontalOverflowChanged, horizontalOverflow, verticalOverflowChanged, verticalOverflow),
EventTargetNodeCast(m_object->element()), true);
}
}
void
RenderLayer::updateScrollInfoAfterLayout()
{
m_scrollDimensionsDirty = true;
bool horizontalOverflow, verticalOverflow;
computeScrollDimensions(&horizontalOverflow, &verticalOverflow);
if (m_object->style()->overflowX() != OMARQUEE) {
int newX = max(0, min(scrollXOffset(), scrollWidth() - m_object->clientWidth()));
int newY = max(0, min(m_scrollY, scrollHeight() - m_object->clientHeight()));
setAdjustForPurpleCaretWhenScrolling(true);
if (newX != scrollXOffset() || newY != m_scrollY)
scrollToOffset(newX, newY);
setAdjustForPurpleCaretWhenScrolling(false);
}
bool haveHorizontalBar = m_hBar;
bool haveVerticalBar = m_vBar;
if (m_object->style()->overflowX() == OSCROLL)
m_hBar->setEnabled(horizontalOverflow);
if (m_object->style()->overflowY() == OSCROLL)
m_vBar->setEnabled(verticalOverflow);
if (m_object->style()->overflowX() == OHIDDEN && haveHorizontalBar)
setHasHorizontalScrollbar(false);
if (m_object->style()->overflowY() == OHIDDEN && haveVerticalBar)
setHasVerticalScrollbar(false);
bool scrollbarsChanged = (m_object->hasAutoHorizontalScrollbar() && haveHorizontalBar != horizontalOverflow) ||
(m_object->hasAutoVerticalScrollbar() && haveVerticalBar != verticalOverflow);
if (scrollbarsChanged) {
if (m_object->hasAutoHorizontalScrollbar())
setHasHorizontalScrollbar(horizontalOverflow);
if (m_object->hasAutoVerticalScrollbar())
setHasVerticalScrollbar(verticalOverflow);
#if __APPLE__
if (m_object->document()->hasDashboardRegions())
m_object->document()->setDashboardRegionsDirty(true);
#endif
m_object->repaint();
if (m_object->style()->overflowX() == OAUTO || m_object->style()->overflowY() == OAUTO) {
if (!m_inOverflowRelayout) {
m_inOverflowRelayout = true;
m_object->setNeedsLayout(true);
if (m_object->isRenderBlock())
static_cast<RenderBlock*>(m_object)->layoutBlock(true);
else
m_object->layout();
m_inOverflowRelayout = false;
}
}
}
if (m_hBar) {
int clientWidth = m_object->clientWidth();
int pageStep = (clientWidth-PAGE_KEEP);
if (pageStep < 0) pageStep = clientWidth;
m_hBar->setSteps(LINE_STEP, pageStep);
m_hBar->setKnobProportion(clientWidth, m_scrollWidth);
m_hBar->setValue(scrollXOffset());
}
if (m_vBar) {
int clientHeight = m_object->clientHeight();
int pageStep = (clientHeight-PAGE_KEEP);
if (pageStep < 0) pageStep = clientHeight;
m_vBar->setSteps(LINE_STEP, pageStep);
m_vBar->setKnobProportion(clientHeight, m_scrollHeight);
m_object->repaintRectangle(IntRect(m_object->borderLeft() + m_object->clientWidth(),
m_object->borderTop(), verticalScrollbarWidth(),
m_object->height() - m_object->borderTop() - m_object->borderBottom()));
}
if (m_object->element() && m_object->element()->document()->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
updateOverflowStatus(horizontalOverflow, verticalOverflow);
}
void
RenderLayer::paintScrollbars(GraphicsContext* p, const IntRect& damageRect)
{
positionResizeControl();
if (m_hBar || m_vBar) {
int x = 0;
int y = 0;
convertToLayerCoords(root(), x, y);
IntRect layerBounds = IntRect(x, y, width(), height());
positionScrollbars(layerBounds);
}
if (m_hBar)
m_hBar->paint(p, damageRect);
if (m_vBar)
m_vBar->paint(p, damageRect);
}
void RenderLayer::paintResizeControl(GraphicsContext* c)
{
if (resizeControlRect().isEmpty())
return;
if (!m_resizeCornerImage)
m_resizeCornerImage = Image::loadResource("textAreaResizeCorner");
IntPoint imagePoint(resizeControlRect().right() - m_resizeCornerImage->width(), resizeControlRect().bottom() - m_resizeCornerImage->height());
c->drawImage(m_resizeCornerImage, imagePoint);
}
bool RenderLayer::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
{
bool didHorizontalScroll = false;
bool didVerticalScroll = false;
if (m_hBar != 0) {
if (granularity == ScrollByDocument) {
didHorizontalScroll = m_hBar->scroll(ScrollLeft, ScrollByDocument, multiplier);
} else {
didHorizontalScroll = m_hBar->scroll(direction, granularity, multiplier);
}
}
if (m_vBar != 0) {
didVerticalScroll = m_vBar->scroll(direction, granularity, multiplier);
}
return (didHorizontalScroll || didVerticalScroll);
}
void
RenderLayer::paint(GraphicsContext* p, const IntRect& damageRect, PaintRestriction paintRestriction, RenderObject *paintingRoot)
{
paintLayer(this, p, damageRect, false, paintRestriction, paintingRoot);
}
static void setClip(GraphicsContext* p, const IntRect& paintDirtyRect, const IntRect& clipRect)
{
p->setFocusRingClip(clipRect);
if (paintDirtyRect == clipRect)
return;
p->save();
p->addClip(clipRect);
}
static void restoreClip(GraphicsContext* p, const IntRect& paintDirtyRect, const IntRect& clipRect)
{
p->clearFocusRingClip();
if (paintDirtyRect == clipRect)
return;
p->restore();
}
void
RenderLayer::paintLayer(RenderLayer* rootLayer, GraphicsContext* p,
const IntRect& paintDirtyRect, bool haveTransparency, PaintRestriction paintRestriction,
RenderObject *paintingRoot)
{
IntRect layerBounds, damageRect, clipRectToApply, outlineRect;
calculateRects(rootLayer, paintDirtyRect, layerBounds, damageRect, clipRectToApply, outlineRect);
int x = layerBounds.x();
int y = layerBounds.y();
int tx = x - renderer()->xPos();
int ty = y - renderer()->yPos() + renderer()->borderTopExtra();
updateZOrderLists();
updateOverflowList();
if (!m_object->opacity())
return;
bool selectionOnly = paintRestriction == PaintRestrictionSelectionOnly || paintRestriction == PaintRestrictionSelectionOnlyWhiteText;
bool forceWhiteText = paintRestriction == PaintRestrictionSelectionOnlyWhiteText;
if (isTransparent())
haveTransparency = true;
RenderObject *paintingRootForRenderer = 0;
if (paintingRoot && !m_object->hasAncestor(paintingRoot))
paintingRootForRenderer = paintingRoot;
bool shouldPaint = intersectsDamageRect(layerBounds, damageRect);
if (shouldPaint && !selectionOnly && !damageRect.isEmpty()) {
if (haveTransparency)
beginTransparencyLayers(p, paintDirtyRect);
setClip(p, paintDirtyRect, damageRect);
RenderObject::PaintInfo info(p, damageRect, PaintPhaseBlockBackground, false, paintingRootForRenderer, 0);
renderer()->paint(info, tx, ty);
paintScrollbars(p, damageRect);
restoreClip(p, paintDirtyRect, damageRect);
}
if (m_negZOrderList)
for (Vector<RenderLayer*>::iterator it = m_negZOrderList->begin(); it != m_negZOrderList->end(); ++it)
it[0]->paintLayer(rootLayer, p, paintDirtyRect, haveTransparency, paintRestriction, paintingRoot);
if (shouldPaint && !clipRectToApply.isEmpty()) {
if (haveTransparency)
beginTransparencyLayers(p, paintDirtyRect);
setClip(p, paintDirtyRect, clipRectToApply);
RenderObject::PaintInfo info(p, clipRectToApply,
selectionOnly ? PaintPhaseSelection : PaintPhaseChildBlockBackgrounds,
forceWhiteText,
paintingRootForRenderer, 0);
renderer()->paint(info, tx, ty);
if (!selectionOnly) {
info.phase = PaintPhaseFloat;
renderer()->paint(info, tx, ty);
info.phase = PaintPhaseForeground;
renderer()->paint(info, tx, ty);
info.phase = PaintPhaseChildOutlines;
renderer()->paint(info, tx, ty);
}
restoreClip(p, paintDirtyRect, clipRectToApply);
}
if (!outlineRect.isEmpty()) {
RenderObject::PaintInfo info(p, outlineRect, PaintPhaseSelfOutline, false, paintingRootForRenderer, 0);
setClip(p, paintDirtyRect, outlineRect);
renderer()->paint(info, tx, ty);
restoreClip(p, paintDirtyRect, outlineRect);
}
if (m_overflowList)
for (Vector<RenderLayer*>::iterator it = m_overflowList->begin(); it != m_overflowList->end(); ++it)
it[0]->paintLayer(rootLayer, p, paintDirtyRect, haveTransparency, paintRestriction, paintingRoot);
if (m_posZOrderList)
for (Vector<RenderLayer*>::iterator it = m_posZOrderList->begin(); it != m_posZOrderList->end(); ++it)
it[0]->paintLayer(rootLayer, p, paintDirtyRect, haveTransparency, paintRestriction, paintingRoot);
if (isTransparent() && m_usedTransparency) {
p->endTransparencyLayer();
p->restore();
m_usedTransparency = false;
}
}
static inline bool isSubframeCanvas(RenderObject* renderer)
{
return renderer->isRenderView() && renderer->node()->document()->frame()->tree()->parent();
}
static inline IntRect frameVisibleRect(RenderObject* renderer)
{
return enclosingIntRect(renderer->node()->document()->frame()->view()->visibleContentRect());
}
bool
RenderLayer::hitTest(RenderObject::NodeInfo& info, const IntPoint& point)
{
gScrollBar = 0;
renderer()->document()->updateLayout();
IntRect boundsRect(m_x, m_y, width(), height());
if (isSubframeCanvas(renderer()))
boundsRect.intersect(frameVisibleRect(renderer()));
RenderLayer* insideLayer = hitTestLayer(this, info, point, boundsRect);
Node* node = info.innerNode();
while (node) {
if (node->isLink() && !info.URLElement())
info.setURLElement(static_cast<Element*>(node));
node = node->parentNode();
}
updateHoverActiveState(info);
return insideLayer;
}
static inline bool shouldApplyImplicitCapture(RenderObject* renderer, bool mouseDown)
{
return mouseDown && renderer->isRoot();
}
RenderLayer*
RenderLayer::hitTestLayer(RenderLayer* rootLayer, RenderObject::NodeInfo& info,
const IntPoint& mousePos, const IntRect& hitTestRect)
{
IntRect layerBounds;
IntRect bgRect;
IntRect fgRect;
IntRect outlineRect;
calculateRects(rootLayer, hitTestRect, layerBounds, bgRect, fgRect, outlineRect);
updateZOrderLists();
updateOverflowList();
RenderLayer* insideLayer = 0;
if (m_posZOrderList) {
for (int i = m_posZOrderList->size() - 1; i >= 0; --i) {
insideLayer = m_posZOrderList->at(i)->hitTestLayer(rootLayer, info, mousePos, hitTestRect);
if (insideLayer)
return insideLayer;
}
}
if (m_overflowList) {
for (int i = m_overflowList->size() - 1; i >= 0; --i) {
insideLayer = m_overflowList->at(i)->hitTestLayer(rootLayer, info, mousePos, hitTestRect);
if (insideLayer)
return insideLayer;
}
}
if (fgRect.contains(mousePos) &&
renderer()->hitTest(info, mousePos.x(), mousePos.y(),
layerBounds.x() - renderer()->xPos(),
layerBounds.y() - renderer()->yPos() + m_object->borderTopExtra(), HitTestDescendants)) {
if (!info.innerNode()) {
for (RenderObject *r = renderer(); r != NULL; r = r->parent()) {
if (r->element()) {
info.setInnerNode(r->element());
break;
}
}
}
if (!info.innerNonSharedNode()) {
for (RenderObject *r = renderer(); r != NULL; r = r->parent()) {
if (r->element()) {
info.setInnerNonSharedNode(r->element());
break;
}
}
}
return this;
}
if (m_negZOrderList) {
for (int i = m_negZOrderList->size() - 1; i >= 0; --i) {
insideLayer = m_negZOrderList->at(i)->hitTestLayer(rootLayer, info, mousePos, hitTestRect);
if (insideLayer)
return insideLayer;
}
}
if ((bgRect.contains(mousePos) || shouldApplyImplicitCapture(renderer(), info.active())) &&
renderer()->hitTest(info, mousePos.x(), mousePos.y(),
layerBounds.x() - renderer()->xPos(),
layerBounds.y() - renderer()->yPos() + m_object->borderTopExtra(),
HitTestSelf))
return this;
return 0;
}
void RenderLayer::calculateClipRects(const RenderLayer* rootLayer)
{
if (m_clipRects)
return;
if (!parent()) {
m_clipRects = new (m_object->renderArena()) ClipRects(IntRect(0,0,width(),height()));
m_clipRects->ref();
return;
}
parent()->calculateClipRects(rootLayer);
IntRect posClipRect(parent()->clipRects()->posClipRect());
IntRect overflowClipRect(parent()->clipRects()->overflowClipRect());
IntRect fixedClipRect(parent()->clipRects()->fixedClipRect());
if (m_object->style()->position() == FixedPosition) {
posClipRect = fixedClipRect;
overflowClipRect = fixedClipRect;
}
else if (m_object->style()->position() == RelativePosition)
posClipRect = overflowClipRect;
else if (m_object->style()->position() == AbsolutePosition)
overflowClipRect = posClipRect;
if (m_object->hasOverflowClip() || m_object->hasClip()) {
int x = 0;
int y = 0;
convertToLayerCoords(rootLayer, x, y);
if (m_object->hasOverflowClip()) {
IntRect newOverflowClip = m_object->getOverflowClipRect(x,y);
overflowClipRect.intersect(newOverflowClip);
if (m_object->isPositioned() || m_object->isRelPositioned())
posClipRect.intersect(newOverflowClip);
}
if (m_object->hasClip()) {
IntRect newPosClip = m_object->getClipRect(x,y);
posClipRect.intersect(newPosClip);
overflowClipRect.intersect(newPosClip);
fixedClipRect.intersect(newPosClip);
}
}
if (posClipRect == parent()->clipRects()->posClipRect() &&
overflowClipRect == parent()->clipRects()->overflowClipRect() &&
fixedClipRect == parent()->clipRects()->fixedClipRect())
m_clipRects = parent()->clipRects();
else
m_clipRects = new (m_object->renderArena()) ClipRects(overflowClipRect, fixedClipRect, posClipRect);
m_clipRects->ref();
}
void RenderLayer::calculateRects(const RenderLayer* rootLayer, const IntRect& paintDirtyRect, IntRect& layerBounds,
IntRect& backgroundRect, IntRect& foregroundRect, IntRect& outlineRect)
{
if (parent()) {
parent()->calculateClipRects(rootLayer);
backgroundRect = m_object->style()->position() == FixedPosition ? parent()->clipRects()->fixedClipRect() :
(m_object->isPositioned() ? parent()->clipRects()->posClipRect() :
parent()->clipRects()->overflowClipRect());
backgroundRect.intersect(paintDirtyRect);
} else
backgroundRect = paintDirtyRect;
foregroundRect = backgroundRect;
outlineRect = backgroundRect;
int x = 0;
int y = 0;
convertToLayerCoords(rootLayer, x, y);
layerBounds = IntRect(x,y,width(),height());
if (m_object->hasOverflowClip() || m_object->hasClip()) {
if (m_object->hasOverflowClip())
foregroundRect.intersect(m_object->getOverflowClipRect(x,y));
if (m_object->hasClip()) {
IntRect newPosClip = m_object->getClipRect(x,y);
backgroundRect.intersect(newPosClip);
foregroundRect.intersect(newPosClip);
outlineRect.intersect(newPosClip);
}
backgroundRect.intersect(layerBounds);
}
}
bool RenderLayer::intersectsDamageRect(const IntRect& layerBounds, const IntRect& damageRect) const
{
if (renderer()->isRenderView() || renderer()->isRoot())
return true;
if (!renderer()->isInlineFlow()) {
IntRect b = layerBounds;
b.inflate(renderer()->view()->maximalOutlineSize());
if (b.intersects(damageRect))
return true;
}
return absoluteBoundingBox().intersects(damageRect);
}
IntRect RenderLayer::absoluteBoundingBox() const
{
IntRect result;
if (renderer()->isInlineFlow()) {
RenderInline* inlineFlow = static_cast<RenderInline*>(renderer());
InlineFlowBox* firstBox = inlineFlow->firstLineBox();
if (!firstBox)
return result;
int top = firstBox->root()->topOverflow();
int bottom = inlineFlow->lastLineBox()->root()->bottomOverflow();
int left = firstBox->xPos();
for (InlineRunBox* curr = firstBox->nextLineBox(); curr; curr = curr->nextLineBox())
left = min(left, curr->xPos());
result = IntRect(m_x + left, m_y + (top - renderer()->yPos()), width(), bottom - top);
} else if (renderer()->isTableRow()) {
for (RenderObject* child = renderer()->firstChild(); child; child = child->nextSibling()) {
if (child->isTableCell()) {
IntRect bbox = child->borderBox();
bbox.move(0, child->borderTopExtra());
result.unite(bbox);
IntRect overflowRect = renderer()->overflowRect(false);
overflowRect.move(0, child->borderTopExtra());
if (bbox != overflowRect)
result.unite(overflowRect);
}
}
result.move(m_x, m_y);
} else {
IntRect bbox = renderer()->borderBox();
result = bbox;
IntRect overflowRect = renderer()->overflowRect(false);
if (bbox != overflowRect)
result.unite(overflowRect);
IntRect floatRect = renderer()->floatRect();
if (bbox != floatRect)
result.unite(floatRect);
result.move(m_x, m_y + renderer()->borderTopExtra());
}
int absX = 0, absY = 0;
convertToLayerCoords(root(), absX, absY);
result.move(absX - m_x, absY - m_y);
result.inflate(renderer()->view()->maximalOutlineSize());
return result;
}
void RenderLayer::clearClipRects()
{
if (!m_clipRects)
return;
clearClipRect();
for (RenderLayer* l = firstChild(); l; l = l->nextSibling())
l->clearClipRects();
}
void RenderLayer::clearClipRect()
{
if (m_clipRects) {
m_clipRects->deref(m_object->renderArena());
m_clipRects = 0;
}
}
static RenderObject* commonAncestor(RenderObject* obj1, RenderObject* obj2)
{
if (!obj1 || !obj2)
return 0;
for (RenderObject* currObj1 = obj1; currObj1; currObj1 = currObj1->hoverAncestor())
for (RenderObject* currObj2 = obj2; currObj2; currObj2 = currObj2->hoverAncestor())
if (currObj1 == currObj2)
return currObj1;
return 0;
}
void RenderLayer::updateHoverActiveState(RenderObject::NodeInfo& info)
{
if (info.readonly())
return;
Document* doc = renderer()->document();
if (!doc) return;
Node* activeNode = doc->activeNode();
if (activeNode && !info.active()) {
for (RenderObject* curr = activeNode->renderer(); curr; curr = curr->parent()) {
if (curr->element() && !curr->isText())
curr->element()->setInActiveChain(false);
}
doc->setActiveNode(0);
} else {
Node* newActiveNode = info.innerNode();
if (!activeNode && newActiveNode && info.active()) {
for (RenderObject* curr = newActiveNode->renderer(); curr; curr = curr->parent()) {
if (curr->element() && !curr->isText()) {
curr->element()->setInActiveChain(true);
}
}
doc->setActiveNode(newActiveNode);
}
}
bool mustBeInActiveChain = info.active() && info.mouseMove();
Node* oldHoverNode = doc->hoverNode();
Node* newHoverNode = info.innerNode();
doc->setHoverNode(newHoverNode);
RenderObject* oldHoverObj = oldHoverNode ? oldHoverNode->renderer() : 0;
RenderObject* newHoverObj = newHoverNode ? newHoverNode->renderer() : 0;
RenderObject* ancestor = commonAncestor(oldHoverObj, newHoverObj);
if (oldHoverObj != newHoverObj) {
for (RenderObject* curr = oldHoverObj; curr && curr != ancestor; curr = curr->hoverAncestor()) {
if (curr->element() && !curr->isText() && (!mustBeInActiveChain || curr->element()->inActiveChain())) {
curr->element()->setActive(false);
curr->element()->setHovered(false);
}
}
}
for (RenderObject* curr = newHoverObj; curr; curr = curr->hoverAncestor()) {
if (curr->element() && !curr->isText() && (!mustBeInActiveChain || curr->element()->inActiveChain())) {
curr->element()->setActive(info.active());
curr->element()->setHovered(true);
}
}
}
static inline bool compareZIndex(RenderLayer* first, RenderLayer* second)
{
return first->zIndex() < second->zIndex();
}
void RenderLayer::dirtyZOrderLists()
{
if (m_posZOrderList)
m_posZOrderList->clear();
if (m_negZOrderList)
m_negZOrderList->clear();
m_zOrderListsDirty = true;
}
void RenderLayer::dirtyOverflowList()
{
if (m_overflowList)
m_overflowList->clear();
m_overflowListDirty = true;
}
void RenderLayer::updateZOrderLists()
{
if (!isStackingContext() || !m_zOrderListsDirty)
return;
for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
child->collectLayers(m_posZOrderList, m_negZOrderList);
if (m_posZOrderList)
std::stable_sort(m_posZOrderList->begin(), m_posZOrderList->end(), compareZIndex);
if (m_negZOrderList)
std::stable_sort(m_negZOrderList->begin(), m_negZOrderList->end(), compareZIndex);
m_zOrderListsDirty = false;
}
void RenderLayer::updateOverflowList()
{
if (!m_overflowListDirty)
return;
for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
if (child->isOverflowOnly()) {
if (!m_overflowList)
m_overflowList = new Vector<RenderLayer*>;
m_overflowList->append(child);
}
}
m_overflowListDirty = false;
}
void RenderLayer::collectLayers(Vector<RenderLayer*>*& posBuffer, Vector<RenderLayer*>*& negBuffer)
{
if (renderer()->style()->visibility() != VISIBLE)
return;
if (!isOverflowOnly()) {
Vector<RenderLayer*>*& buffer = (zIndex() >= 0) ? posBuffer : negBuffer;
if (!buffer)
buffer = new Vector<RenderLayer*>;
buffer->append(this);
}
if (!isStackingContext())
for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
child->collectLayers(posBuffer, negBuffer);
}
void RenderLayer::repaintIncludingDescendants()
{
m_object->repaint();
for (RenderLayer* curr = firstChild(); curr; curr = curr->nextSibling())
curr->repaintIncludingDescendants();
}
bool RenderLayer::shouldBeOverflowOnly() const
{
return renderer()->hasOverflowClip() &&
!renderer()->isPositioned() &&
!renderer()->isRelPositioned() &&
!isTransparent();
}
void RenderLayer::styleChanged()
{
bool isOverflowOnly = shouldBeOverflowOnly();
if (isOverflowOnly != m_isOverflowOnly) {
m_isOverflowOnly = isOverflowOnly;
RenderLayer* p = parent();
RenderLayer* sc = stackingContext();
if (p)
p->dirtyOverflowList();
if (sc)
sc->dirtyZOrderLists();
}
if (m_object->style()->overflowX() == OMARQUEE && m_object->style()->marqueeBehavior() != MNONE) {
if (!m_marquee)
m_marquee = new Marquee(this);
m_marquee->updateMarqueeStyle();
}
else if (m_marquee) {
delete m_marquee;
m_marquee = 0;
}
}
void RenderLayer::suspendMarquees()
{
if (m_marquee)
m_marquee->suspend();
for (RenderLayer* curr = firstChild(); curr; curr = curr->nextSibling())
curr->suspendMarquees();
}
Marquee::Marquee(RenderLayer* l)
: m_layer(l), m_currentLoop(0)
, m_timer(this, &Marquee::timerFired)
, m_start(0), m_end(0), m_speed(0), m_reset(false)
, m_suspended(false), m_stopped(false), m_direction(MAUTO)
{
}
int Marquee::marqueeSpeed() const
{
int result = m_layer->renderer()->style()->marqueeSpeed();
Node* elt = m_layer->renderer()->element();
if (elt && elt->hasTagName(marqueeTag)) {
HTMLMarqueeElement* marqueeElt = static_cast<HTMLMarqueeElement*>(elt);
result = max(result, marqueeElt->minimumDelay());
}
return result;
}
EMarqueeDirection Marquee::direction() const
{
EMarqueeDirection result = m_layer->renderer()->style()->marqueeDirection();
TextDirection dir = m_layer->renderer()->style()->direction();
if (result == MAUTO)
result = MBACKWARD;
if (result == MFORWARD)
result = (dir == LTR) ? MRIGHT : MLEFT;
if (result == MBACKWARD)
result = (dir == LTR) ? MLEFT : MRIGHT;
Length increment = m_layer->renderer()->style()->marqueeIncrement();
if (increment.value() < 0)
result = static_cast<EMarqueeDirection>(-result);
return result;
}
bool Marquee::isHorizontal() const
{
return direction() == MLEFT || direction() == MRIGHT;
}
int Marquee::computePosition(EMarqueeDirection dir, bool stopAtContentEdge)
{
RenderObject* o = m_layer->renderer();
RenderStyle* s = o->style();
if (isHorizontal()) {
bool ltr = s->direction() == LTR;
int clientWidth = o->clientWidth();
int contentWidth = ltr ? o->rightmostPosition(true, false) : o->leftmostPosition(true, false);
if (ltr)
contentWidth += (o->paddingRight() - o->borderLeft());
else {
contentWidth = o->width() - contentWidth;
contentWidth += (o->paddingLeft() - o->borderRight());
}
if (dir == MRIGHT) {
if (stopAtContentEdge)
return max(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
else
return ltr ? contentWidth : clientWidth;
}
else {
if (stopAtContentEdge)
return min(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
else
return ltr ? -clientWidth : -contentWidth;
}
}
else {
int contentHeight = m_layer->renderer()->lowestPosition(true, false) -
m_layer->renderer()->borderTop() + m_layer->renderer()->paddingBottom();
int clientHeight = m_layer->renderer()->clientHeight();
if (dir == MUP) {
if (stopAtContentEdge)
return min(contentHeight - clientHeight, 0);
else
return -clientHeight;
}
else {
if (stopAtContentEdge)
return max(contentHeight - clientHeight, 0);
else
return contentHeight;
}
}
}
void Marquee::start()
{
if (m_timer.isActive() || m_layer->renderer()->style()->marqueeIncrement().value() == 0)
return;
if (!m_suspended && !m_stopped) {
if (isHorizontal())
m_layer->scrollToOffset(m_start, 0, false, false);
else
m_layer->scrollToOffset(0, m_start, false, false);
}
else {
m_suspended = false;
m_stopped = false;
}
m_timer.startRepeating(speed() * 0.001);
}
void Marquee::suspend()
{
m_timer.stop();
m_suspended = true;
}
void Marquee::stop()
{
m_timer.stop();
m_stopped = true;
}
void Marquee::updateMarqueePosition()
{
bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
if (activate) {
EMarqueeBehavior behavior = m_layer->renderer()->style()->marqueeBehavior();
m_start = computePosition(direction(), behavior == MALTERNATE);
m_end = computePosition(reverseDirection(), behavior == MALTERNATE || behavior == MSLIDE);
if (!m_stopped)
start();
}
}
void Marquee::updateMarqueeStyle()
{
RenderStyle* s = m_layer->renderer()->style();
if (m_direction != s->marqueeDirection() || (m_totalLoops != s->marqueeLoopCount() && m_currentLoop >= m_totalLoops))
m_currentLoop = 0;
m_totalLoops = s->marqueeLoopCount();
m_direction = s->marqueeDirection();
if (m_layer->renderer()->isHTMLMarquee()) {
if (m_totalLoops <= 0 && s->marqueeBehavior() == MSLIDE)
m_totalLoops = 1;
if (isHorizontal() && m_layer->renderer()->childrenInline()) {
s->setWhiteSpace(NOWRAP);
s->setTextAlign(TAAUTO);
}
}
if (isHorizontal()) {
if (s->height().isFixed() && s->height().value() < s->fontSize())
s->setHeight(Length(s->fontSize(),Fixed));
} else if (s->height().isAuto()) s->setHeight(Length(200, Fixed));
if (speed() != marqueeSpeed()) {
m_speed = marqueeSpeed();
if (m_timer.isActive())
m_timer.startRepeating(speed() * 0.001);
}
bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
if (activate && !m_timer.isActive())
m_layer->renderer()->setNeedsLayout(true);
else if (!activate && m_timer.isActive())
m_timer.stop();
}
void Marquee::timerFired(Timer<Marquee>*)
{
if (m_layer->renderer()->needsLayout())
return;
if (m_reset) {
m_reset = false;
if (isHorizontal())
m_layer->scrollToXOffset(m_start);
else
m_layer->scrollToYOffset(m_start);
return;
}
RenderStyle* s = m_layer->renderer()->style();
int endPoint = m_end;
int range = m_end - m_start;
int newPos;
if (range == 0)
newPos = m_end;
else {
bool addIncrement = direction() == MUP || direction() == MLEFT;
bool isReversed = s->marqueeBehavior() == MALTERNATE && m_currentLoop % 2;
if (isReversed) {
endPoint = m_start;
range = -range;
addIncrement = !addIncrement;
}
bool positive = range > 0;
int clientSize = (isHorizontal() ? m_layer->renderer()->clientWidth() : m_layer->renderer()->clientHeight());
int increment = max(1, abs(m_layer->renderer()->style()->marqueeIncrement().calcValue(clientSize)));
int currentPos = (isHorizontal() ? m_layer->scrollXOffset() : m_layer->scrollYOffset());
newPos = currentPos + (addIncrement ? increment : -increment);
if (positive)
newPos = min(newPos, endPoint);
else
newPos = max(newPos, endPoint);
}
if (newPos == endPoint) {
m_currentLoop++;
if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops)
m_timer.stop();
else if (s->marqueeBehavior() != MALTERNATE)
m_reset = true;
}
if (isHorizontal())
m_layer->scrollToXOffset(newPos);
else
m_layer->scrollToYOffset(newPos);
}
}