#include "config.h"
#include "RenderView.h"
#include "Document.h"
#include "Element.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "RenderLayer.h"
#if ENABLE(HW_COMP)
#include "LCRootLayer.h"
#endif
#if ENABLE(HW_COMP)
#include "Frame.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)
#if ENABLE(HW_COMP)
, m_rootLayer(0)
, m_rootLayerAttached(false)
#endif
{
setIsAnonymous(false);
setInline(false);
m_width = 0;
m_height = 0;
m_minPrefWidth = 0;
m_maxPrefWidth = 0;
setPrefWidthsDirty(true, false);
setPositioned(true);
m_layer = new (node->document()->renderArena()) RenderLayer(this);
setHasLayer(true);
}
RenderView::~RenderView()
{
#if ENABLE(HW_COMP)
ASSERT(!m_rootLayerAttached);
delete m_rootLayer;
#endif
}
void RenderView::calcHeight()
{
if (!printing() && m_frameView)
m_height = m_frameView->visibleHeight();
}
void RenderView::calcWidth()
{
if (!printing() && m_frameView)
m_width = m_frameView->visibleWidth();
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 = m_width;
bool relayoutChildren = !printing() && (!m_frameView || m_width != m_frameView->visibleWidth() || m_height != m_frameView->visibleHeight());
if (relayoutChildren)
setChildNeedsLayout(true, false);
ASSERT(!m_layoutState);
LayoutState state;
state.m_clipped = false;
m_layoutState = &state;
if (needsLayout())
RenderBlock::layout();
setOverflowWidth(m_width);
setOverflowHeight(m_height);
setOverflowWidth(docWidth());
setOverflowHeight(docHeight());
ASSERT(m_layoutStateDisableCount == 0);
ASSERT(m_layoutState == &state);
m_layoutState = 0;
setNeedsLayout(false);
}
bool RenderView::absolutePosition(int& xPos, int& yPos, bool fixed) const
{
if (fixed && m_frameView) {
xPos = m_frameView->contentsX();
yPos = m_frameView->contentsY();
} else
xPos = yPos = 0;
return true;
}
void RenderView::paint(PaintInfo& paintInfo, int tx, int ty)
{
#if !ENABLE(HW_COMP)
ASSERT(!needsLayout());
#endif
if (printing())
setPrintRect(paintInfo.rect);
else
setPrintRect(IntRect());
paintObject(paintInfo, tx, ty);
}
void RenderView::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
{
Element* elt = document()->ownerElement();
if (view() && elt && elt->renderer()) {
RenderLayer* layer = elt->renderer()->enclosingLayer();
if (layer->isTransparent() || layer->transparentAncestor())
frameView()->setUseSlowRepaints();
}
if (elt || (firstChild() && firstChild()->style()->visibility() == VISIBLE) || !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);
paintInfo.context->restore();
} else
paintInfo.context->clearRect(paintInfo.rect);
}
}
void RenderView::repaintViewRectangle(const IntRect& ur, bool immediate)
{
if (printing() || ur.width() == 0 || ur.height() == 0)
return;
if (!m_frameView)
return;
Element* elt = document()->ownerElement();
if (!elt)
m_frameView->repaintRectangle(ur, immediate);
else if (RenderObject* obj = elt->renderer()) {
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::computeAbsoluteRepaintRect(IntRect& rect, bool fixed)
{
if (printing())
return;
if (fixed && m_frameView)
rect.move(m_frameView->contentsX(), m_frameView->contentsY());
}
void RenderView::absoluteRects(Vector<IntRect>& rects, int tx, int ty, bool)
{
rects.append(IntRect(tx, ty, m_layer->width(), m_layer->height()));
}
void RenderView::absoluteQuads(Vector<FloatQuad>& quads, int tx, int ty, bool)
{
FloatRect curRect(tx, ty, m_layer->width(), m_layer->height());
quads.append(convertRectToPageQuad(curRect, tx, ty));
}
RenderObject* rendererAfterPosition(RenderObject* object, unsigned offset)
{
if (!object)
return 0;
RenderObject* child = object->childAt(offset);
return child ? child : object->nextInPreOrderAfterChildren();
}
IntRect RenderView::selectionRect(bool clipToVisibleContent) const
{
document()->updateRendering();
typedef HashMap<RenderObject*, SelectionInfo*> 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 SelectionInfo(os, clipToVisibleContent));
RenderBlock* cb = os->containingBlock();
while (cb && !cb->isRenderView()) {
SelectionInfo* blockInfo = selectedObjects.get(cb);
if (blockInfo)
break;
selectedObjects.set(cb, new SelectionInfo(cb, clipToVisibleContent));
cb = cb->containingBlock();
}
}
os = os->nextInPreOrder();
}
IntRect selRect;
SelectionMap::iterator end = selectedObjects.end();
for (SelectionMap::iterator i = selectedObjects.begin(); i != end; ++i) {
SelectionInfo* info = i->second;
selRect.unite(info->rect());
delete info;
}
return selRect;
}
#if ENABLE(HW_COMP)
void RenderView::setMaximalOutlineSize(int o)
{
if (o != m_maximalOutlineSize) {
m_maximalOutlineSize = o;
if (m_frameView)
m_frameView->updateLayers();
}
}
#endif
void RenderView::setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos)
{
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*, SelectionInfo*> SelectedObjectMap;
SelectedObjectMap oldSelectedObjects;
SelectedObjectMap newSelectedObjects;
typedef HashMap<RenderBlock*, BlockSelectionInfo*> 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 SelectionInfo(os, true));
RenderBlock* cb = os->containingBlock();
while (cb && !cb->isRenderView()) {
BlockSelectionInfo* blockInfo = oldSelectedBlocks.get(cb);
if (blockInfo)
break;
oldSelectedBlocks.set(cb, new BlockSelectionInfo(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();
}
o = start;
while (o && o != stop) {
if ((o->canBeSelectionLeaf() || o == start || o == end) && o->selectionState() != SelectionNone) {
newSelectedObjects.set(o, new SelectionInfo(o, true));
RenderBlock* cb = o->containingBlock();
while (cb && !cb->isRenderView()) {
BlockSelectionInfo* blockInfo = newSelectedBlocks.get(cb);
if (blockInfo)
break;
newSelectedBlocks.set(cb, new BlockSelectionInfo(cb));
cb = cb->containingBlock();
}
}
o = o->nextInPreOrder();
}
if (!m_frameView) {
deleteAllValues(oldSelectedObjects);
deleteAllValues(newSelectedObjects);
deleteAllValues(oldSelectedBlocks);
deleteAllValues(newSelectedBlocks);
return;
}
for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i) {
RenderObject* obj = i->first;
SelectionInfo* newInfo = newSelectedObjects.get(obj);
SelectionInfo* 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)) {
m_frameView->updateContents(oldInfo->rect());
if (newInfo) {
m_frameView->updateContents(newInfo->rect());
newSelectedObjects.remove(obj);
delete newInfo;
}
}
delete oldInfo;
}
SelectedObjectMap::iterator newObjectsEnd = newSelectedObjects.end();
for (SelectedObjectMap::iterator i = newSelectedObjects.begin(); i != newObjectsEnd; ++i) {
SelectionInfo* newInfo = i->second;
m_frameView->updateContents(newInfo->rect());
delete newInfo;
}
SelectedBlockMap::iterator oldBlocksEnd = oldSelectedBlocks.end();
for (SelectedBlockMap::iterator i = oldSelectedBlocks.begin(); i != oldBlocksEnd; ++i) {
RenderBlock* block = i->first;
BlockSelectionInfo* newInfo = newSelectedBlocks.get(block);
BlockSelectionInfo* oldInfo = i->second;
if (!newInfo || oldInfo->rects() != newInfo->rects() || oldInfo->state() != newInfo->state()) {
m_frameView->updateContents(oldInfo->rects());
if (newInfo) {
m_frameView->updateContents(newInfo->rects());
newSelectedBlocks.remove(block);
delete newInfo;
}
}
delete oldInfo;
}
SelectedBlockMap::iterator newBlocksEnd = newSelectedBlocks.end();
for (SelectedBlockMap::iterator i = newSelectedBlocks.begin(); i != newBlocksEnd; ++i) {
BlockSelectionInfo* newInfo = i->second;
m_frameView->updateContents(newInfo->rects());
delete newInfo;
}
}
void RenderView::clearSelection()
{
setSelection(0, -1, 0, -1);
}
void RenderView::selectionStartEnd(int& startPos, int& endPos) const
{
startPos = m_selectionStartPos;
endPos = m_selectionEndPos;
}
bool RenderView::printing() const
{
return document()->printing();
}
void RenderView::updateWidgetPositions()
{
#if ENABLE(HW_COMP)
if (m_rootLayer) {
FloatRect frameRect(0, 0, m_frameView->width(), m_frameView->height());
m_rootLayer->setBounds(frameRect);
m_rootLayer->setPosition(FloatPoint(m_frameView->x(), m_frameView->y()));
}
#endif
RenderObjectSet::iterator end = m_widgets.end();
for (RenderObjectSet::iterator it = m_widgets.begin(); it != end; ++it)
(*it)->updateWidgetPosition();
}
void RenderView::addWidget(RenderObject* o)
{
m_widgets.add(o);
}
void RenderView::removeWidget(RenderObject* o)
{
m_widgets.remove(o);
}
IntRect RenderView::viewRect() const
{
if (printing())
return IntRect(0, 0, m_width, m_height);
if (m_frameView)
return enclosingIntRect(m_frameView->visibleContentRect());
return IntRect();
}
int RenderView::docHeight() const
{
int h;
if (printing() || !m_frameView)
h = m_height;
else
h = m_frameView->visibleHeight();
int lowestPos = lowestPosition();
if (lowestPos > h)
h = lowestPos;
int dh = 0;
for (RenderObject* c = firstChild(); c; c = c->nextSibling())
dh += c->height() + c->marginTop() + c->marginBottom();
if (dh > h)
h = dh;
return h;
}
int RenderView::docWidth() const
{
int w;
if (printing() || !m_frameView)
w = m_width;
else
w = m_frameView->visibleWidth();
int rightmostPos = rightmostPosition();
if (rightmostPos > w)
w = rightmostPos;
for (RenderObject *c = firstChild(); c; c = c->nextSibling()) {
int dw = c->width() + c->marginLeft() + c->marginRight();
if (dw > w)
w = dw;
}
return w;
}
void RenderView::setBestTruncatedAt(int y, RenderObject* forRenderer, bool forcedBreak)
{
if (m_forcedPageBreak)
return;
if (forcedBreak) {
m_forcedPageBreak = true;
m_bestTruncatedAt = y;
return;
}
int width = forRenderer->width();
if (width > m_truncatorWidth) {
m_truncatorWidth = width;
m_bestTruncatedAt = y;
}
}
void RenderView::pushLayoutState(RenderObject* root)
{
ASSERT(!m_frameView->needsFullRepaint());
ASSERT(m_layoutStateDisableCount == 0);
ASSERT(m_layoutState == 0);
m_layoutState = new (renderArena()) LayoutState(root);
}
#if ENABLE(HW_COMP)
void RenderView::ensureRootCompositingLayer()
{
if (m_rootLayer)
return;
m_rootLayer = LCRootLayer::rootLayer(FloatRect(0, 0, m_frameView->width(), m_frameView->height()));
m_rootLayer->setPosition(FloatPoint(m_frameView->x(), m_frameView->y()));
m_rootLayer->setName("Root Layer");
wasAttached();
}
LCLayer* RenderView::rootCompositingLayer() const
{
return m_rootLayer;
}
void RenderView::noteUpdateContents(const IntRect& r)
{
if (m_layer && (r.width() > 0 && r.height() > 0)) {
m_layer->repaintAbsoluteRectIncludingDescendants(r);
}
}
void RenderView::noteUpdate()
{
if (m_layer) {
m_layer->repaintIncludingDescendants();
}
}
void RenderView::willBeDetached()
{
if (m_rootLayer && m_rootLayerAttached) {
m_rootLayer->setParentLayer(0);
m_rootLayerAttached = false;
}
}
void RenderView::wasAttached()
{
if (m_rootLayer) {
m_rootLayer->setParentLayer(frameView()->frame()->contentsHostingLayer());
m_rootLayerAttached = true;
}
}
#endif
}