cd3893a555d60f1ab89f26163e70e427a258dca4
[WebKit-https.git] / Source / WebCore / rendering / RenderView.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "RenderView.h"
23
24 #include "ColumnInfo.h"
25 #include "Document.h"
26 #include "Element.h"
27 #include "FloatQuad.h"
28 #include "Frame.h"
29 #include "FrameView.h"
30 #include "GraphicsContext.h"
31 #include "HTMLFrameOwnerElement.h"
32 #include "HitTestResult.h"
33 #include "Page.h"
34 #include "RenderFlowThread.h"
35 #include "RenderLayer.h"
36 #include "RenderSelectionInfo.h"
37 #include "RenderWidget.h"
38 #include "RenderWidgetProtector.h"
39 #include "TransformState.h"
40
41 #if USE(ACCELERATED_COMPOSITING)
42 #include "RenderLayerCompositor.h"
43 #endif
44
45 namespace WebCore {
46
47 RenderView::RenderView(Node* node, FrameView* view)
48     : RenderBlock(node)
49     , m_frameView(view)
50     , m_selectionStart(0)
51     , m_selectionEnd(0)
52     , m_selectionStartPos(-1)
53     , m_selectionEndPos(-1)
54     , m_maximalOutlineSize(0)
55     , m_pageLogicalHeight(0)
56     , m_pageLogicalHeightChanged(false)
57     , m_isRenderFlowThreadOrderDirty(false)
58     , m_layoutState(0)
59     , m_layoutStateDisableCount(0)
60     , m_currentRenderFlowThread(0)
61 {
62     // Clear our anonymous bit, set because RenderObject assumes
63     // any renderer with document as the node is anonymous.
64     setIsAnonymous(false);
65
66     // init RenderObject attributes
67     setInline(false);
68     
69     m_minPreferredLogicalWidth = 0;
70     m_maxPreferredLogicalWidth = 0;
71
72     setPreferredLogicalWidthsDirty(true, false);
73     
74     setPositioned(true); // to 0,0 :)
75 }
76
77 RenderView::~RenderView()
78 {
79 }
80
81 void RenderView::computeLogicalHeight()
82 {
83     if (!printing() && m_frameView)
84         setLogicalHeight(viewLogicalHeight());
85 }
86
87 void RenderView::computeLogicalWidth()
88 {
89     if (!printing() && m_frameView)
90         setLogicalWidth(viewLogicalWidth());
91 }
92
93 void RenderView::computePreferredLogicalWidths()
94 {
95     ASSERT(preferredLogicalWidthsDirty());
96
97     RenderBlock::computePreferredLogicalWidths();
98
99     m_maxPreferredLogicalWidth = m_minPreferredLogicalWidth;
100 }
101
102 bool RenderView::isChildAllowed(RenderObject* child, RenderStyle*) const
103 {
104     return child->isBox();
105 }
106
107 void RenderView::layout()
108 {
109     if (!document()->paginated())
110         setPageLogicalHeight(0);
111
112     if (printing())
113         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = logicalWidth();
114
115     // Use calcWidth/Height to get the new width/height, since this will take the full page zoom factor into account.
116     bool relayoutChildren = !printing() && (!m_frameView || width() != viewWidth() || height() != viewHeight());
117     if (relayoutChildren) {
118         setChildNeedsLayout(true, false);
119         for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
120             if (child->style()->logicalHeight().isPercent() || child->style()->logicalMinHeight().isPercent() || child->style()->logicalMaxHeight().isPercent())
121                 child->setChildNeedsLayout(true, false);
122         }
123     }
124
125     ASSERT(!m_layoutState);
126     LayoutState state;
127     // FIXME: May be better to push a clip and avoid issuing offscreen repaints.
128     state.m_clipped = false;
129     state.m_pageLogicalHeight = m_pageLogicalHeight;
130     state.m_pageLogicalHeightChanged = m_pageLogicalHeightChanged;
131     state.m_isPaginated = state.m_pageLogicalHeight;
132     m_pageLogicalHeightChanged = false;
133     m_layoutState = &state;
134
135     if (needsLayout()) {
136         RenderBlock::layout();
137         if (hasRenderFlowThreads())
138             layoutRenderFlowThreads();
139     }
140
141     ASSERT(layoutDelta() == LayoutSize());
142     ASSERT(m_layoutStateDisableCount == 0);
143     ASSERT(m_layoutState == &state);
144     m_layoutState = 0;
145     setNeedsLayout(false);
146 }
147
148 void RenderView::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState, bool* wasFixed) const
149 {
150     // If a container was specified, and was not 0 or the RenderView,
151     // then we should have found it by now.
152     ASSERT_ARG(repaintContainer, !repaintContainer || repaintContainer == this);
153     ASSERT_UNUSED(wasFixed, !wasFixed || *wasFixed == fixed);
154
155     if (!repaintContainer && useTransforms && shouldUseTransformFromContainer(0)) {
156         TransformationMatrix t;
157         getTransformFromContainer(0, LayoutSize(), t);
158         transformState.applyTransform(t);
159     }
160     
161     if (fixed && m_frameView)
162         transformState.move(m_frameView->scrollOffsetForFixedPosition());
163 }
164
165 void RenderView::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
166 {
167     if (fixed && m_frameView)
168         transformState.move(m_frameView->scrollOffsetForFixedPosition());
169
170     if (useTransforms && shouldUseTransformFromContainer(0)) {
171         TransformationMatrix t;
172         getTransformFromContainer(0, LayoutSize(), t);
173         transformState.applyTransform(t);
174     }
175 }
176
177 bool RenderView::requiresColumns(int desiredColumnCount) const
178 {
179     if (m_frameView) {
180         if (Frame* frame = m_frameView->frame()) {
181             if (Page* page = frame->page())
182                 return frame == page->mainFrame() && page->pagination().mode != Page::Pagination::Unpaginated;
183         }
184     }
185     return RenderBlock::requiresColumns(desiredColumnCount);
186 }
187
188 void RenderView::calcColumnWidth()
189 {
190     int columnWidth = contentLogicalWidth();
191     if (m_frameView && style()->hasInlineColumnAxis()) {
192         if (Frame* frame = m_frameView->frame()) {
193             if (Page* page = frame->page()) {
194                 if (int pageLength = page->pagination().pageLength)
195                     columnWidth = pageLength;
196             }
197         }
198     }
199     setDesiredColumnCountAndWidth(1, columnWidth);
200 }
201
202 ColumnInfo::PaginationUnit RenderView::paginationUnit() const
203 {
204     if (m_frameView) {
205         if (Frame* frame = m_frameView->frame()) {
206             if (Page* page = frame->page())
207                 return (frame == page->mainFrame() && page->pagination().behavesLikeColumns) ? ColumnInfo::Column : ColumnInfo::Page;
208         }
209     }
210     return ColumnInfo::Page;
211 }
212
213 void RenderView::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
214 {
215     // If we ever require layout but receive a paint anyway, something has gone horribly wrong.
216     ASSERT(!needsLayout());
217     paintObject(paintInfo, paintOffset);
218 }
219
220 static inline bool isComposited(RenderObject* object)
221 {
222     return object->hasLayer() && toRenderBoxModelObject(object)->layer()->isComposited();
223 }
224
225 static inline bool rendererObscuresBackground(RenderObject* object)
226 {
227     return object && object->style()->visibility() == VISIBLE
228         && object->style()->opacity() == 1
229         && !object->style()->hasTransform()
230         && !isComposited(object);
231 }
232     
233 void RenderView::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint&)
234 {
235     // Check to see if we are enclosed by a layer that requires complex painting rules.  If so, we cannot blit
236     // when scrolling, and we need to use slow repaints.  Examples of layers that require this are transparent layers,
237     // layers with reflections, or transformed layers.
238     // FIXME: This needs to be dynamic.  We should be able to go back to blitting if we ever stop being inside
239     // a transform, transparency layer, etc.
240     Element* elt;
241     for (elt = document()->ownerElement(); view() && elt && elt->renderer(); elt = elt->document()->ownerElement()) {
242         RenderLayer* layer = elt->renderer()->enclosingLayer();
243         if (layer->cannotBlitToWindow()) {
244             frameView()->setCannotBlitToWindow();
245             break;
246         }
247
248 #if USE(ACCELERATED_COMPOSITING)
249         if (RenderLayer* compositingLayer = layer->enclosingCompositingLayer()) {
250             if (!compositingLayer->backing()->paintingGoesToWindow()) {
251                 frameView()->setCannotBlitToWindow();
252                 break;
253             }
254         }
255 #endif
256     }
257
258     if (document()->ownerElement() || !view())
259         return;
260
261     bool rootFillsViewport = false;
262     Node* documentElement = document()->documentElement();
263     if (RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0) {
264         // The document element's renderer is currently forced to be a block, but may not always be.
265         RenderBox* rootBox = rootRenderer->isBox() ? toRenderBox(rootRenderer) : 0;
266         rootFillsViewport = rootBox && !rootBox->x() && !rootBox->y() && rootBox->width() >= width() && rootBox->height() >= height();
267     }
268     
269     Page* page = document()->page();
270     float pageScaleFactor = page ? page->pageScaleFactor() : 1;
271
272     // If painting will entirely fill the view, no need to fill the background.
273     if (rootFillsViewport && rendererObscuresBackground(firstChild()) && pageScaleFactor >= 1)
274         return;
275
276     // This code typically only executes if the root element's visibility has been set to hidden,
277     // if there is a transform on the <html>, or if there is a page scale factor less than 1.
278     // Only fill with the base background color (typically white) if we're the root document, 
279     // since iframes/frames with no background in the child document should show the parent's background.
280     if (frameView()->isTransparent()) // FIXME: This needs to be dynamic.  We should be able to go back to blitting if we ever stop being transparent.
281         frameView()->setCannotBlitToWindow(); // The parent must show behind the child.
282     else {
283         Color baseColor = frameView()->baseBackgroundColor();
284         if (baseColor.alpha() > 0) {
285             CompositeOperator previousOperator = paintInfo.context->compositeOperation();
286             paintInfo.context->setCompositeOperation(CompositeCopy);
287             paintInfo.context->fillRect(paintInfo.rect, baseColor, style()->colorSpace());
288             paintInfo.context->setCompositeOperation(previousOperator);
289         } else
290             paintInfo.context->clearRect(paintInfo.rect);
291     }
292 }
293
294 bool RenderView::shouldRepaint(const IntRect& r) const
295 {
296     if (printing() || r.width() == 0 || r.height() == 0)
297         return false;
298
299     if (!m_frameView)
300         return false;
301     
302     return true;
303 }
304
305 void RenderView::repaintViewRectangle(const IntRect& ur, bool immediate)
306 {
307     if (!shouldRepaint(ur))
308         return;
309
310     // We always just invalidate the root view, since we could be an iframe that is clipped out
311     // or even invisible.
312     Element* elt = document()->ownerElement();
313     if (!elt)
314         m_frameView->repaintContentRectangle(ur, immediate);
315     else if (RenderBox* obj = elt->renderBox()) {
316         IntRect vr = viewRect();
317         IntRect r = intersection(ur, vr);
318         
319         // Subtract out the contentsX and contentsY offsets to get our coords within the viewing
320         // rectangle.
321         r.moveBy(-vr.location());
322         
323         // FIXME: Hardcoded offsets here are not good.
324         r.move(obj->borderLeft() + obj->paddingLeft(),
325                obj->borderTop() + obj->paddingTop());
326         obj->repaintRectangle(r, immediate);
327     }
328 }
329
330 void RenderView::repaintRectangleInViewAndCompositedLayers(const IntRect& ur, bool immediate)
331 {
332     if (!shouldRepaint(ur))
333         return;
334
335     repaintViewRectangle(ur, immediate);
336     
337 #if USE(ACCELERATED_COMPOSITING)
338     if (compositor()->inCompositingMode())
339         compositor()->repaintCompositedLayersAbsoluteRect(ur);
340 #endif
341 }
342
343 void RenderView::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed) const
344 {
345     // If a container was specified, and was not 0 or the RenderView,
346     // then we should have found it by now.
347     ASSERT_ARG(repaintContainer, !repaintContainer || repaintContainer == this);
348
349     if (printing())
350         return;
351
352     if (style()->isFlippedBlocksWritingMode()) {
353         // We have to flip by hand since the view's logical height has not been determined.  We
354         // can use the viewport width and height.
355         if (style()->isHorizontalWritingMode())
356             rect.setY(viewHeight() - rect.maxY());
357         else
358             rect.setX(viewWidth() - rect.maxX());
359     }
360
361     if (fixed && m_frameView)
362         rect.move(m_frameView->scrollXForFixedPosition(), m_frameView->scrollYForFixedPosition());
363         
364     // Apply our transform if we have one (because of full page zooming).
365     if (!repaintContainer && m_layer && m_layer->transform())
366         rect = m_layer->transform()->mapRect(rect);
367 }
368
369 void RenderView::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
370 {
371     rects.append(pixelSnappedIntRect(accumulatedOffset, m_layer->size()));
372 }
373
374 void RenderView::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
375 {
376     if (wasFixed)
377         *wasFixed = false;
378     quads.append(FloatRect(FloatPoint(), m_layer->size()));
379 }
380
381 static RenderObject* rendererAfterPosition(RenderObject* object, unsigned offset)
382 {
383     if (!object)
384         return 0;
385
386     RenderObject* child = object->childAt(offset);
387     return child ? child : object->nextInPreOrderAfterChildren();
388 }
389
390 IntRect RenderView::selectionBounds(bool clipToVisibleContent) const
391 {
392     document()->updateStyleIfNeeded();
393
394     typedef HashMap<RenderObject*, RenderSelectionInfo*> SelectionMap;
395     SelectionMap selectedObjects;
396
397     RenderObject* os = m_selectionStart;
398     RenderObject* stop = rendererAfterPosition(m_selectionEnd, m_selectionEndPos);
399     while (os && os != stop) {
400         if ((os->canBeSelectionLeaf() || os == m_selectionStart || os == m_selectionEnd) && os->selectionState() != SelectionNone) {
401             // Blocks are responsible for painting line gaps and margin gaps. They must be examined as well.
402             selectedObjects.set(os, new RenderSelectionInfo(os, clipToVisibleContent));
403             RenderBlock* cb = os->containingBlock();
404             while (cb && !cb->isRenderView()) {
405                 RenderSelectionInfo* blockInfo = selectedObjects.get(cb);
406                 if (blockInfo)
407                     break;
408                 selectedObjects.set(cb, new RenderSelectionInfo(cb, clipToVisibleContent));
409                 cb = cb->containingBlock();
410             }
411         }
412
413         os = os->nextInPreOrder();
414     }
415
416     // Now create a single bounding box rect that encloses the whole selection.
417     IntRect selRect;
418     SelectionMap::iterator end = selectedObjects.end();
419     for (SelectionMap::iterator i = selectedObjects.begin(); i != end; ++i) {
420         RenderSelectionInfo* info = i->second;
421         // RenderSelectionInfo::rect() is in the coordinates of the repaintContainer, so map to page coordinates.
422         IntRect currRect = info->rect();
423         if (RenderBoxModelObject* repaintContainer = info->repaintContainer()) {
424             FloatQuad absQuad = repaintContainer->localToAbsoluteQuad(FloatRect(currRect));
425             currRect = absQuad.enclosingBoundingBox(); 
426         }
427         selRect.unite(currRect);
428         delete info;
429     }
430     return selRect;
431 }
432
433 #if USE(ACCELERATED_COMPOSITING)
434 // Compositing layer dimensions take outline size into account, so we have to recompute layer
435 // bounds when it changes.
436 // FIXME: This is ugly; it would be nice to have a better way to do this.
437 void RenderView::setMaximalOutlineSize(int o)
438 {
439     if (o != m_maximalOutlineSize) {
440         m_maximalOutlineSize = o;
441
442         // maximalOutlineSize affects compositing layer dimensions.
443         compositor()->setCompositingLayersNeedRebuild();    // FIXME: this really just needs to be a geometry update.
444     }
445 }
446 #endif
447
448 void RenderView::setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode blockRepaintMode)
449 {
450     // Make sure both our start and end objects are defined.
451     // Check www.msnbc.com and try clicking around to find the case where this happened.
452     if ((start && !end) || (end && !start))
453         return;
454
455     // Just return if the selection hasn't changed.
456     if (m_selectionStart == start && m_selectionStartPos == startPos &&
457         m_selectionEnd == end && m_selectionEndPos == endPos)
458         return;
459
460     // Record the old selected objects.  These will be used later
461     // when we compare against the new selected objects.
462     int oldStartPos = m_selectionStartPos;
463     int oldEndPos = m_selectionEndPos;
464
465     // Objects each have a single selection rect to examine.
466     typedef HashMap<RenderObject*, RenderSelectionInfo*> SelectedObjectMap;
467     SelectedObjectMap oldSelectedObjects;
468     SelectedObjectMap newSelectedObjects;
469
470     // Blocks contain selected objects and fill gaps between them, either on the left, right, or in between lines and blocks.
471     // In order to get the repaint rect right, we have to examine left, middle, and right rects individually, since otherwise
472     // the union of those rects might remain the same even when changes have occurred.
473     typedef HashMap<RenderBlock*, RenderBlockSelectionInfo*> SelectedBlockMap;
474     SelectedBlockMap oldSelectedBlocks;
475     SelectedBlockMap newSelectedBlocks;
476
477     RenderObject* os = m_selectionStart;
478     RenderObject* stop = rendererAfterPosition(m_selectionEnd, m_selectionEndPos);
479     while (os && os != stop) {
480         if ((os->canBeSelectionLeaf() || os == m_selectionStart || os == m_selectionEnd) && os->selectionState() != SelectionNone) {
481             // Blocks are responsible for painting line gaps and margin gaps.  They must be examined as well.
482             oldSelectedObjects.set(os, new RenderSelectionInfo(os, true));
483             if (blockRepaintMode == RepaintNewXOROld) {
484                 RenderBlock* cb = os->containingBlock();
485                 while (cb && !cb->isRenderView()) {
486                     RenderBlockSelectionInfo* blockInfo = oldSelectedBlocks.get(cb);
487                     if (blockInfo)
488                         break;
489                     oldSelectedBlocks.set(cb, new RenderBlockSelectionInfo(cb));
490                     cb = cb->containingBlock();
491                 }
492             }
493         }
494
495         os = os->nextInPreOrder();
496     }
497
498     // Now clear the selection.
499     SelectedObjectMap::iterator oldObjectsEnd = oldSelectedObjects.end();
500     for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i)
501         i->first->setSelectionStateIfNeeded(SelectionNone);
502
503     // set selection start and end
504     m_selectionStart = start;
505     m_selectionStartPos = startPos;
506     m_selectionEnd = end;
507     m_selectionEndPos = endPos;
508
509     // Update the selection status of all objects between m_selectionStart and m_selectionEnd
510     if (start && start == end)
511         start->setSelectionStateIfNeeded(SelectionBoth);
512     else {
513         if (start)
514             start->setSelectionStateIfNeeded(SelectionStart);
515         if (end)
516             end->setSelectionStateIfNeeded(SelectionEnd);
517     }
518
519     RenderObject* o = start;
520     stop = rendererAfterPosition(end, endPos);
521
522     while (o && o != stop) {
523         if (o != start && o != end && o->canBeSelectionLeaf())
524             o->setSelectionStateIfNeeded(SelectionInside);
525         o = o->nextInPreOrder();
526     }
527
528     if (blockRepaintMode != RepaintNothing)
529         m_layer->clearBlockSelectionGapsBounds();
530
531     // Now that the selection state has been updated for the new objects, walk them again and
532     // put them in the new objects list.
533     o = start;
534     while (o && o != stop) {
535         if ((o->canBeSelectionLeaf() || o == start || o == end) && o->selectionState() != SelectionNone) {
536             newSelectedObjects.set(o, new RenderSelectionInfo(o, true));
537             RenderBlock* cb = o->containingBlock();
538             while (cb && !cb->isRenderView()) {
539                 RenderBlockSelectionInfo* blockInfo = newSelectedBlocks.get(cb);
540                 if (blockInfo)
541                     break;
542                 newSelectedBlocks.set(cb, new RenderBlockSelectionInfo(cb));
543                 cb = cb->containingBlock();
544             }
545         }
546
547         o = o->nextInPreOrder();
548     }
549
550     if (!m_frameView || blockRepaintMode == RepaintNothing) {
551         // We built the maps, but we aren't going to use them.
552         // We need to delete the values, otherwise they'll all leak!
553         deleteAllValues(oldSelectedObjects);
554         deleteAllValues(newSelectedObjects);
555         deleteAllValues(oldSelectedBlocks);
556         deleteAllValues(newSelectedBlocks);
557         return;
558     }
559
560     m_frameView->beginDeferredRepaints();
561
562     // Have any of the old selected objects changed compared to the new selection?
563     for (SelectedObjectMap::iterator i = oldSelectedObjects.begin(); i != oldObjectsEnd; ++i) {
564         RenderObject* obj = i->first;
565         RenderSelectionInfo* newInfo = newSelectedObjects.get(obj);
566         RenderSelectionInfo* oldInfo = i->second;
567         if (!newInfo || oldInfo->rect() != newInfo->rect() || oldInfo->state() != newInfo->state() ||
568             (m_selectionStart == obj && oldStartPos != m_selectionStartPos) ||
569             (m_selectionEnd == obj && oldEndPos != m_selectionEndPos)) {
570             oldInfo->repaint();
571             if (newInfo) {
572                 newInfo->repaint();
573                 newSelectedObjects.remove(obj);
574                 delete newInfo;
575             }
576         }
577         delete oldInfo;
578     }
579
580     // Any new objects that remain were not found in the old objects dict, and so they need to be updated.
581     SelectedObjectMap::iterator newObjectsEnd = newSelectedObjects.end();
582     for (SelectedObjectMap::iterator i = newSelectedObjects.begin(); i != newObjectsEnd; ++i) {
583         RenderSelectionInfo* newInfo = i->second;
584         newInfo->repaint();
585         delete newInfo;
586     }
587
588     // Have any of the old blocks changed?
589     SelectedBlockMap::iterator oldBlocksEnd = oldSelectedBlocks.end();
590     for (SelectedBlockMap::iterator i = oldSelectedBlocks.begin(); i != oldBlocksEnd; ++i) {
591         RenderBlock* block = i->first;
592         RenderBlockSelectionInfo* newInfo = newSelectedBlocks.get(block);
593         RenderBlockSelectionInfo* oldInfo = i->second;
594         if (!newInfo || oldInfo->rects() != newInfo->rects() || oldInfo->state() != newInfo->state()) {
595             oldInfo->repaint();
596             if (newInfo) {
597                 newInfo->repaint();
598                 newSelectedBlocks.remove(block);
599                 delete newInfo;
600             }
601         }
602         delete oldInfo;
603     }
604
605     // Any new blocks that remain were not found in the old blocks dict, and so they need to be updated.
606     SelectedBlockMap::iterator newBlocksEnd = newSelectedBlocks.end();
607     for (SelectedBlockMap::iterator i = newSelectedBlocks.begin(); i != newBlocksEnd; ++i) {
608         RenderBlockSelectionInfo* newInfo = i->second;
609         newInfo->repaint();
610         delete newInfo;
611     }
612
613     m_frameView->endDeferredRepaints();
614 }
615
616 void RenderView::getSelection(RenderObject*& startRenderer, int& startOffset, RenderObject*& endRenderer, int& endOffset) const
617 {
618     startRenderer = m_selectionStart;
619     startOffset = m_selectionStartPos;
620     endRenderer = m_selectionEnd;
621     endOffset = m_selectionEndPos;
622 }
623
624 void RenderView::clearSelection()
625 {
626     m_layer->repaintBlockSelectionGaps();
627     setSelection(0, -1, 0, -1, RepaintNewMinusOld);
628 }
629
630 void RenderView::selectionStartEnd(int& startPos, int& endPos) const
631 {
632     startPos = m_selectionStartPos;
633     endPos = m_selectionEndPos;
634 }
635
636 bool RenderView::printing() const
637 {
638     return document()->printing();
639 }
640
641 size_t RenderView::getRetainedWidgets(Vector<RenderWidget*>& renderWidgets)
642 {
643     size_t size = m_widgets.size();
644
645     renderWidgets.reserveCapacity(size);
646
647     RenderWidgetSet::const_iterator end = m_widgets.end();
648     for (RenderWidgetSet::const_iterator it = m_widgets.begin(); it != end; ++it) {
649         renderWidgets.uncheckedAppend(*it);
650         (*it)->ref();
651     }
652     
653     return size;
654 }
655
656 void RenderView::releaseWidgets(Vector<RenderWidget*>& renderWidgets)
657 {
658     size_t size = renderWidgets.size();
659
660     for (size_t i = 0; i < size; ++i)
661         renderWidgets[i]->deref(renderArena());
662 }
663
664 void RenderView::updateWidgetPositions()
665 {
666     // updateWidgetPosition() can possibly cause layout to be re-entered (via plug-ins running
667     // scripts in response to NPP_SetWindow, for example), so we need to keep the Widgets
668     // alive during enumeration.    
669
670     Vector<RenderWidget*> renderWidgets;
671     size_t size = getRetainedWidgets(renderWidgets);
672     
673     for (size_t i = 0; i < size; ++i)
674         renderWidgets[i]->updateWidgetPosition();
675
676     for (size_t i = 0; i < size; ++i)
677         renderWidgets[i]->widgetPositionsUpdated();
678
679     releaseWidgets(renderWidgets);
680 }
681
682 void RenderView::addWidget(RenderWidget* o)
683 {
684     m_widgets.add(o);
685 }
686
687 void RenderView::removeWidget(RenderWidget* o)
688 {
689     m_widgets.remove(o);
690 }
691
692 void RenderView::notifyWidgets(WidgetNotification notification)
693 {
694     Vector<RenderWidget*> renderWidgets;
695     size_t size = getRetainedWidgets(renderWidgets);
696
697     for (size_t i = 0; i < size; ++i)
698         renderWidgets[i]->notifyWidget(notification);
699
700     releaseWidgets(renderWidgets);
701 }
702
703 IntRect RenderView::viewRect() const
704 {
705     if (printing())
706         return IntRect(IntPoint(), size());
707     if (m_frameView)
708         return m_frameView->visibleContentRect();
709     return IntRect();
710 }
711
712
713 IntRect RenderView::unscaledDocumentRect() const
714 {
715     IntRect overflowRect(layoutOverflowRect());
716     flipForWritingMode(overflowRect);
717     return overflowRect;
718 }
719
720 LayoutRect RenderView::backgroundRect(RenderBox* backgroundRenderer) const
721 {
722     if (!hasColumns())
723         return unscaledDocumentRect();
724
725     ColumnInfo* columnInfo = this->columnInfo();
726     LayoutRect backgroundRect(0, 0, columnInfo->desiredColumnWidth(), columnInfo->columnHeight() * columnInfo->columnCount());
727     if (!isHorizontalWritingMode())
728         backgroundRect = backgroundRect.transposedRect();
729     backgroundRenderer->flipForWritingMode(backgroundRect);
730
731     return backgroundRect;
732 }
733
734 IntRect RenderView::documentRect() const
735 {
736     IntRect overflowRect(unscaledDocumentRect());
737     if (hasTransform())
738         overflowRect = layer()->currentTransform().mapRect(overflowRect);
739     return overflowRect;
740 }
741
742 int RenderView::viewHeight() const
743 {
744     int height = 0;
745     if (!printing() && m_frameView) {
746         height = m_frameView->layoutHeight();
747         height = m_frameView->useFixedLayout() ? ceilf(style()->effectiveZoom() * float(height)) : height;
748     }
749     return height;
750 }
751
752 int RenderView::viewWidth() const
753 {
754     int width = 0;
755     if (!printing() && m_frameView) {
756         width = m_frameView->layoutWidth();
757         width = m_frameView->useFixedLayout() ? ceilf(style()->effectiveZoom() * float(width)) : width;
758     }
759     return width;
760 }
761
762 int RenderView::viewLogicalHeight() const
763 {
764     int height = style()->isHorizontalWritingMode() ? viewHeight() : viewWidth();
765
766     if (hasColumns() && !style()->hasInlineColumnAxis()) {
767         if (Frame* frame = m_frameView->frame()) {
768             if (Page* page = frame->page()) {
769                 if (frame == page->mainFrame()) {
770                     if (int pageLength = page->pagination().pageLength)
771                         height = pageLength;
772                 }
773             }
774         }
775     }
776
777     return height;
778 }
779
780 float RenderView::zoomFactor() const
781 {
782     Frame* frame = m_frameView->frame();
783     return frame ? frame->pageZoomFactor() : 1;
784 }
785
786 void RenderView::pushLayoutState(RenderObject* root)
787 {
788     ASSERT(m_layoutStateDisableCount == 0);
789     ASSERT(m_layoutState == 0);
790
791     m_layoutState = new (renderArena()) LayoutState(root);
792 }
793
794 void RenderView::pushLayoutState(RenderFlowThread* flowThread, bool regionsChanged)
795 {
796     m_layoutState = new (renderArena()) LayoutState(m_layoutState, flowThread, regionsChanged);
797 }
798
799 bool RenderView::shouldDisableLayoutStateForSubtree(RenderObject* renderer) const
800 {
801     RenderObject* o = renderer;
802     while (o) {
803         if (o->hasColumns() || o->hasTransform() || o->hasReflection())
804             return true;
805         o = o->container();
806     }
807     return false;
808 }
809
810 void RenderView::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
811 {
812     if (result.innerNode())
813         return;
814
815     Node* node = document()->documentElement();
816     if (node) {
817         result.setInnerNode(node);
818         if (!result.innerNonSharedNode())
819             result.setInnerNonSharedNode(node);
820         result.setLocalPoint(point);
821     }
822 }
823
824 // FIXME: This function is obsolete and only used by embedded WebViews inside AppKit NSViews.
825 // Do not add callers of this function!
826 // The idea here is to take into account what object is moving the pagination point, and
827 // thus choose the best place to chop it.
828 void RenderView::setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak)
829 {
830     // Nobody else can set a page break once we have a forced break.
831     if (m_legacyPrinting.m_forcedPageBreak)
832         return;
833
834     // Forced breaks always win over unforced breaks.
835     if (forcedBreak) {
836         m_legacyPrinting.m_forcedPageBreak = true;
837         m_legacyPrinting.m_bestTruncatedAt = y;
838         return;
839     }
840
841     // Prefer the widest object that tries to move the pagination point
842     IntRect boundingBox = forRenderer->borderBoundingBox();
843     if (boundingBox.width() > m_legacyPrinting.m_truncatorWidth) {
844         m_legacyPrinting.m_truncatorWidth = boundingBox.width();
845         m_legacyPrinting.m_bestTruncatedAt = y;
846     }
847 }
848
849 #if USE(ACCELERATED_COMPOSITING)
850 bool RenderView::usesCompositing() const
851 {
852     return m_compositor && m_compositor->inCompositingMode();
853 }
854
855 RenderLayerCompositor* RenderView::compositor()
856 {
857     if (!m_compositor)
858         m_compositor = adoptPtr(new RenderLayerCompositor(this));
859
860     return m_compositor.get();
861 }
862 #endif
863
864 void RenderView::didMoveOnscreen()
865 {
866 #if USE(ACCELERATED_COMPOSITING)
867     if (m_compositor)
868         m_compositor->didMoveOnscreen();
869 #endif
870 }
871
872 void RenderView::willMoveOffscreen()
873 {
874 #if USE(ACCELERATED_COMPOSITING)
875     if (m_compositor)
876         m_compositor->willMoveOffscreen();
877 #endif
878 }
879
880 void RenderView::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
881 {
882     RenderBlock::styleDidChange(diff, oldStyle);
883     
884     for (RenderObject* renderer = firstChild(); renderer; renderer = renderer->nextSibling()) {
885         if (renderer->isRenderFlowThread()) {
886             RenderFlowThread* flowRenderer = toRenderFlowThread(renderer);
887             flowRenderer->setStyle(RenderFlowThread::createFlowThreadStyle(style()));
888         }
889     }
890 }
891
892 RenderFlowThread* RenderView::ensureRenderFlowThreadWithName(const AtomicString& flowThread)
893 {
894     if (!m_renderFlowThreadList)
895         m_renderFlowThreadList = adoptPtr(new RenderFlowThreadList());
896     else {
897         for (RenderFlowThreadList::iterator iter = m_renderFlowThreadList->begin(); iter != m_renderFlowThreadList->end(); ++iter) {
898             RenderFlowThread* flowRenderer = *iter;
899             if (flowRenderer->flowThread() == flowThread)
900                 return flowRenderer;
901         }
902     }
903
904     RenderFlowThread* flowRenderer = new (renderArena()) RenderFlowThread(document(), flowThread);
905     flowRenderer->setStyle(RenderFlowThread::createFlowThreadStyle(style()));
906     addChild(flowRenderer);
907
908     m_renderFlowThreadList->add(flowRenderer);
909     setIsRenderFlowThreadOrderDirty(true);
910
911     return flowRenderer;
912 }
913
914 void RenderView::layoutRenderFlowThreads()
915 {
916     ASSERT(m_renderFlowThreadList);
917
918     if (isRenderFlowThreadOrderDirty()) {
919         // Arrange the thread list according to dependencies.
920         RenderFlowThreadList sortedList;
921         for (RenderFlowThreadList::iterator iter = m_renderFlowThreadList->begin(); iter != m_renderFlowThreadList->end(); ++iter) {
922             RenderFlowThread* flowRenderer = *iter;
923             if (sortedList.contains(flowRenderer))
924                 continue;
925             flowRenderer->pushDependencies(sortedList);
926             sortedList.add(flowRenderer);
927         }
928         m_renderFlowThreadList->swap(sortedList);
929         setIsRenderFlowThreadOrderDirty(false);
930     }
931
932     for (RenderFlowThreadList::iterator iter = m_renderFlowThreadList->begin(); iter != m_renderFlowThreadList->end(); ++iter) {
933         RenderFlowThread* flowRenderer = *iter;
934         flowRenderer->layoutIfNeeded();
935     }
936 }
937
938 RenderBlock::IntervalArena* RenderView::intervalArena()
939 {
940     if (!m_intervalArena)
941         m_intervalArena = IntervalArena::create();
942     return m_intervalArena.get();
943 }
944
945 } // namespace WebCore