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