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