#include "config.h"
#include "RenderView.h"
#include "Document.h"
#include "Element.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "RenderLayer.h"
#include "RenderSelectionInfo.h"
#include "RenderWidget.h"
#include "TransformState.h"
#if USE(ACCELERATED_COMPOSITING)
#include "RenderLayerCompositor.h"
#endif
namespace WebCore {
RenderView::RenderView(Node* node, FrameView* view)
: RenderBlock(node)
, m_frameView(view)
, m_selectionStart(0)
, m_selectionEnd(0)
, m_selectionStartPos(-1)
, m_selectionEndPos(-1)
, m_printImages(true)
, m_maximalOutlineSize(0)
, m_layoutState(0)
, m_layoutStateDisableCount(0)
{
setIsAnonymous(false);
setInline(false);
m_minPrefWidth = 0;
m_maxPrefWidth = 0;
setPrefWidthsDirty(true, false);
setPositioned(true); }
RenderView::~RenderView()
{
}
void RenderView::calcHeight()
{
if (!printing() && m_frameView)
setHeight(viewHeight());
}
void RenderView::calcWidth()
{
if (!printing() && m_frameView)
setWidth(viewWidth());
m_marginLeft = 0;
m_marginRight = 0;
}
void RenderView::calcPrefWidths()
{
ASSERT(prefWidthsDirty());
RenderBlock::calcPrefWidths();
m_maxPrefWidth = m_minPrefWidth;
}
void RenderView::layout()
{
if (printing())
m_minPrefWidth = m_maxPrefWidth = width();
bool relayoutChildren = !printing() && (!m_frameView || width() != viewWidth() || height() != viewHeight());
if (relayoutChildren) {
setChildNeedsLayout(true, false);
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->style()->height().isPercent() || child->style()->minHeight().isPercent() || child->style()->maxHeight().isPercent())
child->setChildNeedsLayout(true, false);
}
}
ASSERT(!m_layoutState);
LayoutState state;
state.m_clipped = false;
m_layoutState = &state;
if (needsLayout())
RenderBlock::layout();
m_overflow.clear();
addLayoutOverflow(IntRect(0, 0, docWidth(), docHeight()));
ASSERT(layoutDelta() == IntSize());
ASSERT(m_layoutStateDisableCount == 0);
ASSERT(m_layoutState == &state);
m_layoutState = 0;
setNeedsLayout(false);
}
void RenderView::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool , TransformState& transformState) const
{
ASSERT_UNUSED(repaintContainer, !repaintContainer || repaintContainer == this);
if (fixed && m_frameView)
transformState.move(m_frameView->scrollOffset());
}
void RenderView::mapAbsoluteToLocalPoint(bool fixed, bool , TransformState& transformState) const
{
if (fixed && m_frameView)
transformState.move(-m_frameView->scrollOffset());
}
void RenderView::paint(PaintInfo& paintInfo, int tx, int ty)
{
ASSERT(!needsLayout());
if (printing())
setPrintRect(paintInfo.rect);
else
setPrintRect(IntRect());
paintObject(paintInfo, tx, ty);
}
static inline bool rendererObscuresBackground(RenderObject* object)
{
return object && object->style()->visibility() == VISIBLE && object->style()->opacity() == 1 && !object->style()->hasTransform();
}
void RenderView::paintBoxDecorations(PaintInfo& paintInfo, int, int)
{
Element* elt;
for (elt = document()->ownerElement(); view() && elt && elt->renderer(); elt = elt->document()->ownerElement()) {
RenderLayer* layer = elt->renderer()->enclosingLayer();
if (layer->requiresSlowRepaints()) {
frameView()->setUseSlowRepaints();
break;
}
}
if (elt || rendererObscuresBackground(firstChild()) || !view())
return;
if (view()->isTransparent()) frameView()->setUseSlowRepaints(); else {
Color baseColor = frameView()->baseBackgroundColor();
if (baseColor.alpha() > 0) {
paintInfo.context->save();
paintInfo.context->setCompositeOperation(CompositeCopy);
paintInfo.context->fillRect(paintInfo.rect, baseColor, style()->colorSpace());
paintInfo.context->restore();
} else
paintInfo.context->clearRect(paintInfo.rect);
}
}
bool RenderView::shouldRepaint(const IntRect& r) const
{
if (printing() || r.width() == 0 || r.height() == 0)
return false;
if (!m_frameView)
return false;
return true;
}
void RenderView::repaintViewRectangle(const IntRect& ur, bool immediate)
{
if (!shouldRepaint(ur))
return;
Element* elt = document()->ownerElement();
if (!elt)
m_frameView->repaintContentRectangle(ur, immediate);
else if (RenderBox* obj = elt->renderBox()) {
IntRect vr = viewRect();
IntRect r = ur;
r.move(-vr.x(), -vr.y());
r.move(obj->borderLeft() + obj->paddingLeft(),
obj->borderTop() + obj->paddingTop());
obj->repaintRectangle(r, immediate);
}
}
void RenderView::repaintRectangleInViewAndCompositedLayers(const IntRect& ur, bool immediate)
{
if (!shouldRepaint(ur))
return;
repaintViewRectangle(ur, immediate);
#if USE(ACCELERATED_COMPOSITING)
if (document()->ownerElement())
return;
if (compositor()->inCompositingMode())
compositor()->repaintCompositedLayersAbsoluteRect(ur);
#endif
}
void RenderView::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed)
{
ASSERT_UNUSED(repaintContainer, !repaintContainer || repaintContainer == this);
if (printing())
return;
if (fixed && m_frameView)
rect.move(m_frameView->scrollX(), m_frameView->scrollY());
if (m_layer && m_layer->transform())
rect = m_layer->transform()->mapRect(rect);
}
void RenderView::absoluteRects(Vector<IntRect>& rects, int tx, int ty)
{
rects.append(IntRect(tx, ty, m_layer->width(), m_layer->height()));
}
void RenderView::absoluteQuads(Vector<FloatQuad>& quads)
{
quads.append(FloatRect(0, 0, m_layer->width(), m_layer->height()));
}
static RenderObject* rendererAfterPosition(RenderObject* object, unsigned offset)
{
if (!object)
return 0;
RenderObject* child = object->childAt(offset);
return child ? child : object->nextInPreOrderAfterChildren();
}
IntRect RenderView::selectionBounds(bool clipToVisibleContent) const
{
document()->updateStyleIfNeeded();
typedef HashMap<RenderObject*, RenderSelectionInfo*> SelectionMap;
SelectionMap selectedObjects;
RenderObject* os = m_selectionStart;
RenderObject* stop = rendererAfterPosition(m_selectionEnd, m_selectionEndPos);
while (os && os != stop) {
if ((os->canBeSelectionLeaf() || os == m_selectionStart || os == m_selectionEnd) && os->selectionState() != SelectionNone) {
selectedObjects.set(os, new RenderSelectionInfo(os, clipToVisibleContent));
RenderBlock* cb = os->containingBlock();
while (cb && !cb->isRenderView()) {
RenderSelectionInfo* blockInfo = selectedObjects.get(cb);
if (blockInfo)
break;
selectedObjects.set(cb, new RenderSelectionInfo(cb, clipToVisibleContent));
cb = cb->containingBlock();
}
}
os = os->nextInPreOrder();
}
IntRect selRect;
SelectionMap::iterator end = selectedObjects.end();
for (SelectionMap::iterator i = selectedObjects.begin(); i != end; ++i) {
RenderSelectionInfo* info = i->second;
IntRect currRect = info->rect();
if (RenderBoxModelObject* repaintContainer = info->repaintContainer()) {
FloatQuad absQuad = repaintContainer->localToAbsoluteQuad(FloatRect(currRect));
currRect = absQuad.enclosingBoundingBox();
}
selRect.unite(currRect);
delete info;
}
return selRect;
}
#if USE(ACCELERATED_COMPOSITING)
void RenderView::setMaximalOutlineSize(int o)
{
if (o != m_maximalOutlineSize) {
m_maximalOutlineSize = o;
compositor()->setCompositingLayersNeedRebuild(); }
}
#endif
void RenderView::setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode blockRepaintMode)
{
if ((start && !end) || (end && !start))
return;
if (m_selectionStart == start && m_selectionStartPos == startPos &&
m_selectionEnd == end && m_selectionEndPos == endPos)
return;
int oldStartPos = m_selectionStartPos;
int oldEndPos = m_selectionEndPos;
typedef HashMap<RenderObject*, RenderSelectionInfo*> SelectedObjectMap;
SelectedObjectMap oldSelectedObjects;
SelectedObjectMap newSelectedObjects;
typedef HashMap<RenderBlock*, RenderBlockSelectionInfo*> SelectedBlockMap;
SelectedBlockMap oldSelectedBlocks;
SelectedBlockMap newSelectedBlocks;
RenderObject* os = m_selectionStart;
RenderObject* stop = rendererAfterPosition(m_selectionEnd, m_selectionEndPos);
while (os && os != stop) {
if ((os->canBeSelectionLeaf() || os == m_selectionStart || os == m_selectionEnd) && os->selectionState() != SelectionNone) {
oldSelectedObjects.set(os, new RenderSelectionInfo(os, true));
RenderBlock* cb = os->containingBlock();
while (cb && !cb->isRenderView()) {
RenderBlockSelectionInfo* blockInfo = oldSelectedBlocks.get(cb);
if (blockInfo)
break;
oldSelectedBlocks.set(cb, new RenderBlockSelectionInfo(cb));
cb = cb->containingBlock();
}
}
os = os->nextInPreOrder();
}
SelectedObjectMap::iterator oldObjectsEnd = oldSelectedObjects.end();
for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i)
i->first->setSelectionState(SelectionNone);
m_selectionStart = start;
m_selectionStartPos = startPos;
m_selectionEnd = end;
m_selectionEndPos = endPos;
if (start && start == end)
start->setSelectionState(SelectionBoth);
else {
if (start)
start->setSelectionState(SelectionStart);
if (end)
end->setSelectionState(SelectionEnd);
}
RenderObject* o = start;
stop = rendererAfterPosition(end, endPos);
while (o && o != stop) {
if (o != start && o != end && o->canBeSelectionLeaf())
o->setSelectionState(SelectionInside);
o = o->nextInPreOrder();
}
m_layer->clearBlockSelectionGapsBounds();
o = start;
while (o && o != stop) {
if ((o->canBeSelectionLeaf() || o == start || o == end) && o->selectionState() != SelectionNone) {
newSelectedObjects.set(o, new RenderSelectionInfo(o, true));
RenderBlock* cb = o->containingBlock();
while (cb && !cb->isRenderView()) {
RenderBlockSelectionInfo* blockInfo = newSelectedBlocks.get(cb);
if (blockInfo)
break;
newSelectedBlocks.set(cb, new RenderBlockSelectionInfo(cb));
cb = cb->containingBlock();
}
}
o = o->nextInPreOrder();
}
if (!m_frameView) {
deleteAllValues(oldSelectedObjects);
deleteAllValues(newSelectedObjects);
deleteAllValues(oldSelectedBlocks);
deleteAllValues(newSelectedBlocks);
return;
}
m_frameView->beginDeferredRepaints();
for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i) {
RenderObject* obj = i->first;
RenderSelectionInfo* newInfo = newSelectedObjects.get(obj);
RenderSelectionInfo* oldInfo = i->second;
if (!newInfo || oldInfo->rect() != newInfo->rect() || oldInfo->state() != newInfo->state() ||
(m_selectionStart == obj && oldStartPos != m_selectionStartPos) ||
(m_selectionEnd == obj && oldEndPos != m_selectionEndPos)) {
oldInfo->repaint();
if (newInfo) {
newInfo->repaint();
newSelectedObjects.remove(obj);
delete newInfo;
}
}
delete oldInfo;
}
SelectedObjectMap::iterator newObjectsEnd = newSelectedObjects.end();
for (SelectedObjectMap::iterator i = newSelectedObjects.begin(); i != newObjectsEnd; ++i) {
RenderSelectionInfo* newInfo = i->second;
newInfo->repaint();
delete newInfo;
}
SelectedBlockMap::iterator oldBlocksEnd = oldSelectedBlocks.end();
for (SelectedBlockMap::iterator i = oldSelectedBlocks.begin(); i != oldBlocksEnd; ++i) {
RenderBlock* block = i->first;
RenderBlockSelectionInfo* newInfo = newSelectedBlocks.get(block);
RenderBlockSelectionInfo* oldInfo = i->second;
if (!newInfo || oldInfo->rects() != newInfo->rects() || oldInfo->state() != newInfo->state()) {
if (blockRepaintMode == RepaintNewXOROld)
oldInfo->repaint();
if (newInfo) {
newInfo->repaint();
newSelectedBlocks.remove(block);
delete newInfo;
}
}
delete oldInfo;
}
SelectedBlockMap::iterator newBlocksEnd = newSelectedBlocks.end();
for (SelectedBlockMap::iterator i = newSelectedBlocks.begin(); i != newBlocksEnd; ++i) {
RenderBlockSelectionInfo* newInfo = i->second;
newInfo->repaint();
delete newInfo;
}
m_frameView->endDeferredRepaints();
}
void RenderView::clearSelection()
{
m_layer->repaintBlockSelectionGaps();
setSelection(0, -1, 0, -1, RepaintNewMinusOld);
}
void RenderView::selectionStartEnd(int& startPos, int& endPos) const
{
startPos = m_selectionStartPos;
endPos = m_selectionEndPos;
}
bool RenderView::printing() const
{
return document()->printing();
}
void RenderView::updateWidgetPositions()
{
RenderWidgetSet::iterator end = m_widgets.end();
for (RenderWidgetSet::iterator it = m_widgets.begin(); it != end; ++it)
(*it)->updateWidgetPosition();
}
void RenderView::addWidget(RenderWidget* o)
{
m_widgets.add(o);
}
void RenderView::removeWidget(RenderWidget* o)
{
m_widgets.remove(o);
}
IntRect RenderView::viewRect() const
{
if (printing())
return IntRect(0, 0, width(), height());
if (m_frameView)
return m_frameView->visibleContentRect();
return IntRect();
}
int RenderView::docHeight() const
{
int h = lowestPosition();
int dh = 0;
for (RenderBox* c = firstChildBox(); c; c = c->nextSiblingBox())
dh += c->height() + c->marginTop() + c->marginBottom();
if (dh > h)
h = dh;
return h;
}
int RenderView::docWidth() const
{
int w = rightmostPosition();
for (RenderBox* c = firstChildBox(); c; c = c->nextSiblingBox()) {
int dw = c->width() + c->marginLeft() + c->marginRight();
if (dw > w)
w = dw;
}
return w;
}
int RenderView::viewHeight() const
{
int height = 0;
if (!printing() && m_frameView) {
height = m_frameView->layoutHeight();
height = m_frameView->useFixedLayout() ? ceilf(style()->effectiveZoom() * float(height)) : height;
}
return height;
}
int RenderView::viewWidth() const
{
int width = 0;
if (!printing() && m_frameView) {
width = m_frameView->layoutWidth();
width = m_frameView->useFixedLayout() ? ceilf(style()->effectiveZoom() * float(width)) : width;
}
return width;
}
float RenderView::zoomFactor() const
{
if (m_frameView->frame() && m_frameView->frame()->shouldApplyPageZoom())
return m_frameView->frame()->zoomFactor();
return 1.0f;
}
void RenderView::setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak)
{
if (m_forcedPageBreak)
return;
if (forcedBreak) {
m_forcedPageBreak = true;
m_bestTruncatedAt = y;
return;
}
IntRect boundingBox = forRenderer->borderBoundingBox();
if (boundingBox.width() > m_truncatorWidth) {
m_truncatorWidth = boundingBox.width();
m_bestTruncatedAt = y;
}
}
void RenderView::pushLayoutState(RenderObject* root)
{
ASSERT(!doingFullRepaint());
ASSERT(m_layoutStateDisableCount == 0);
ASSERT(m_layoutState == 0);
m_layoutState = new (renderArena()) LayoutState(root);
}
bool RenderView::shouldDisableLayoutStateForSubtree(RenderObject* renderer) const
{
RenderObject* o = renderer;
while (o) {
if (o->hasColumns() || o->hasTransform() || o->hasReflection())
return true;
o = o->container();
}
return false;
}
void RenderView::updateHitTestResult(HitTestResult& result, const IntPoint& point)
{
if (result.innerNode())
return;
Node* node = document()->documentElement();
if (node) {
result.setInnerNode(node);
if (!result.innerNonSharedNode())
result.setInnerNonSharedNode(node);
result.setLocalPoint(point);
}
}
#if USE(ACCELERATED_COMPOSITING)
bool RenderView::usesCompositing() const
{
return m_compositor && m_compositor->inCompositingMode();
}
RenderLayerCompositor* RenderView::compositor()
{
if (!m_compositor)
m_compositor.set(new RenderLayerCompositor(this));
return m_compositor.get();
}
#endif
void RenderView::didMoveOnscreen()
{
#if USE(ACCELERATED_COMPOSITING)
if (m_compositor)
m_compositor->didMoveOnscreen();
#endif
}
void RenderView::willMoveOffscreen()
{
#if USE(ACCELERATED_COMPOSITING)
if (m_compositor)
m_compositor->willMoveOffscreen();
#endif
}
}