2010-04-01 James Robinson <jamesr@chromium.org>
[WebKit-https.git] / WebCore / page / FrameView.cpp
1 /*
2  * Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3  *                     1999 Lars Knoll <knoll@kde.org>
4  *                     1999 Antti Koivisto <koivisto@kde.org>
5  *                     2000 Dirk Mueller <mueller@kde.org>
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
7  *           (C) 2006 Graham Dennis (graham.dennis@gmail.com)
8  *           (C) 2006 Alexey Proskuryakov (ap@nypop.com)
9  * Copyright (C) 2009 Google Inc. All rights reserved.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public License
22  * along with this library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26
27 #include "config.h"
28 #include "FrameView.h"
29
30 #include "AXObjectCache.h"
31 #include "CSSStyleSelector.h"
32 #include "Chrome.h"
33 #include "ChromeClient.h"
34 #include "DocLoader.h"
35 #include "EventHandler.h"
36 #include "FloatRect.h"
37 #include "FocusController.h"
38 #include "Frame.h"
39 #include "FrameLoader.h"
40 #include "FrameLoaderClient.h"
41 #include "FrameTree.h"
42 #include "GraphicsContext.h"
43 #include "HTMLDocument.h"
44 #include "HTMLFrameElement.h"
45 #include "HTMLFrameSetElement.h"
46 #include "HTMLNames.h"
47 #include "InspectorTimelineAgent.h"
48 #include "OverflowEvent.h"
49 #include "RenderEmbeddedObject.h"
50 #include "RenderPart.h"
51 #include "RenderScrollbar.h"
52 #include "RenderScrollbarPart.h"
53 #include "RenderTheme.h"
54 #include "RenderView.h"
55 #include "Settings.h"
56 #include "TextResourceDecoder.h"
57 #include <wtf/CurrentTime.h>
58
59 #if USE(ACCELERATED_COMPOSITING)
60 #include "RenderLayerCompositor.h"
61 #endif
62
63 #if ENABLE(SVG)
64 #include "SVGDocument.h"
65 #include "SVGLocatable.h"
66 #include "SVGNames.h"
67 #include "SVGPreserveAspectRatio.h"
68 #include "SVGSVGElement.h"
69 #include "SVGViewElement.h"
70 #include "SVGViewSpec.h"
71 #endif
72
73 #if ENABLE(TILED_BACKING_STORE)
74 #include "TiledBackingStore.h"
75 #endif
76
77 namespace WebCore {
78
79 using namespace HTMLNames;
80
81 double FrameView::sCurrentPaintTimeStamp = 0.0;
82
83 #if ENABLE(REPAINT_THROTTLING)
84 // Normal delay
85 static const double deferredRepaintDelay = 0.025;
86 // Negative value would mean that first few repaints happen without a delay
87 static const double initialDeferredRepaintDelayDuringLoading = 0;
88 // The delay grows on each repaint to this maximum value
89 static const double maxDeferredRepaintDelayDuringLoading = 2.5;
90 // On each repaint the delay increses by this amount
91 static const double deferredRepaintDelayIncrementDuringLoading = 0.5;
92 #else
93 // FIXME: Repaint throttling could be good to have on all platform.
94 // The balance between CPU use and repaint frequency will need some tuning for desktop.
95 // More hooks may be needed to reset the delay on things like GIF and CSS animations.
96 static const double deferredRepaintDelay = 0;
97 static const double initialDeferredRepaintDelayDuringLoading = 0;
98 static const double maxDeferredRepaintDelayDuringLoading = 0;
99 static const double deferredRepaintDelayIncrementDuringLoading = 0;
100 #endif
101
102 // The maximum number of updateWidgets iterations that should be done before returning.
103 static const unsigned maxUpdateWidgetsIterations = 2;
104
105 struct ScheduledEvent : Noncopyable {
106     RefPtr<Event> m_event;
107     RefPtr<Node> m_eventTarget;
108 };
109
110 FrameView::FrameView(Frame* frame)
111     : m_frame(frame)
112     , m_canHaveScrollbars(true)
113     , m_slowRepaintObjectCount(0)
114     , m_fixedObjectCount(0)
115     , m_layoutTimer(this, &FrameView::layoutTimerFired)
116     , m_layoutRoot(0)
117     , m_postLayoutTasksTimer(this, &FrameView::postLayoutTimerFired)
118     , m_isTransparent(false)
119     , m_baseBackgroundColor(Color::white)
120     , m_mediaType("screen")
121     , m_enqueueEvents(0)
122     , m_overflowStatusDirty(true)
123     , m_viewportRenderer(0)
124     , m_wasScrolledByUser(false)
125     , m_inProgrammaticScroll(false)
126     , m_deferredRepaintTimer(this, &FrameView::deferredRepaintTimerFired)
127     , m_shouldUpdateWhileOffscreen(true)
128     , m_deferSetNeedsLayouts(0)
129     , m_setNeedsLayoutWasDeferred(false)
130     , m_scrollCorner(0)
131 {
132     init();
133 }
134
135 PassRefPtr<FrameView> FrameView::create(Frame* frame)
136 {
137     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
138     view->show();
139     return view.release();
140 }
141
142 PassRefPtr<FrameView> FrameView::create(Frame* frame, const IntSize& initialSize)
143 {
144     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
145     view->Widget::setFrameRect(IntRect(view->pos(), initialSize));
146     view->show();
147     return view.release();
148 }
149
150 FrameView::~FrameView()
151 {
152     if (m_postLayoutTasksTimer.isActive()) {
153         m_postLayoutTasksTimer.stop();
154         m_scheduledEvents.clear();
155         m_enqueueEvents = 0;
156     }
157
158     resetScrollbars();
159
160     // Custom scrollbars should already be destroyed at this point
161     ASSERT(!horizontalScrollbar() || !horizontalScrollbar()->isCustomScrollbar());
162     ASSERT(!verticalScrollbar() || !verticalScrollbar()->isCustomScrollbar());
163
164     setHasHorizontalScrollbar(false); // Remove native scrollbars now before we lose the connection to the HostWindow.
165     setHasVerticalScrollbar(false);
166     
167     ASSERT(!m_scrollCorner);
168     ASSERT(m_scheduledEvents.isEmpty());
169     ASSERT(!m_enqueueEvents);
170
171     if (m_frame) {
172         ASSERT(m_frame->view() != this || !m_frame->contentRenderer());
173         RenderPart* renderer = m_frame->ownerRenderer();
174         if (renderer && renderer->widget() == this)
175             renderer->setWidget(0);
176     }
177 }
178
179 void FrameView::reset()
180 {
181     m_useSlowRepaints = false;
182     m_isOverlapped = false;
183     m_contentIsOpaque = false;
184     m_borderX = 30;
185     m_borderY = 30;
186     m_layoutTimer.stop();
187     m_layoutRoot = 0;
188     m_delayedLayout = false;
189     m_doFullRepaint = true;
190     m_layoutSchedulingEnabled = true;
191     m_midLayout = false;
192     m_layoutCount = 0;
193     m_nestedLayoutCount = 0;
194     m_postLayoutTasksTimer.stop();
195     m_firstLayout = true;
196     m_firstLayoutCallbackPending = false;
197     m_wasScrolledByUser = false;
198     m_lastLayoutSize = IntSize();
199     m_lastZoomFactor = 1.0f;
200     m_deferringRepaints = 0;
201     m_repaintCount = 0;
202     m_repaintRects.clear();
203     m_deferredRepaintDelay = initialDeferredRepaintDelayDuringLoading;
204     m_deferredRepaintTimer.stop();
205     m_lastPaintTime = 0;
206     m_paintBehavior = PaintBehaviorNormal;
207     m_isPainting = false;
208     m_isVisuallyNonEmpty = false;
209     m_firstVisuallyNonEmptyLayoutCallbackPending = true;
210     m_maintainScrollPositionAnchor = 0;
211 }
212
213 bool FrameView::isFrameView() const 
214
215     return true; 
216 }
217
218 void FrameView::clearFrame()
219 {
220     m_frame = 0;
221 }
222
223 void FrameView::resetScrollbars()
224 {
225     // Reset the document's scrollbars back to our defaults before we yield the floor.
226     m_firstLayout = true;
227     setScrollbarsSuppressed(true);
228     if (m_canHaveScrollbars)
229         setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
230     else
231         setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
232     setScrollbarsSuppressed(false);
233 }
234
235 void FrameView::init()
236 {
237     reset();
238
239     m_margins = IntSize(-1, -1); // undefined
240     m_size = IntSize();
241
242     // Propagate the marginwidth/height and scrolling modes to the view.
243     Element* ownerElement = m_frame && m_frame->document() ? m_frame->document()->ownerElement() : 0;
244     if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
245         HTMLFrameElement* frameElt = static_cast<HTMLFrameElement*>(ownerElement);
246         if (frameElt->scrollingMode() == ScrollbarAlwaysOff)
247             setCanHaveScrollbars(false);
248         int marginWidth = frameElt->getMarginWidth();
249         int marginHeight = frameElt->getMarginHeight();
250         if (marginWidth != -1)
251             setMarginWidth(marginWidth);
252         if (marginHeight != -1)
253             setMarginHeight(marginHeight);
254     }
255 }
256
257 void FrameView::detachCustomScrollbars()
258 {
259     if (!m_frame)
260         return;
261
262     Scrollbar* horizontalBar = horizontalScrollbar();
263     if (horizontalBar && horizontalBar->isCustomScrollbar() && !toRenderScrollbar(horizontalBar)->owningRenderer()->isRenderPart())
264         setHasHorizontalScrollbar(false);
265
266     Scrollbar* verticalBar = verticalScrollbar();
267     if (verticalBar && verticalBar->isCustomScrollbar() && !toRenderScrollbar(verticalBar)->owningRenderer()->isRenderPart())
268         setHasVerticalScrollbar(false);
269
270     if (m_scrollCorner) {
271         m_scrollCorner->destroy();
272         m_scrollCorner = 0;
273     }
274 }
275
276 void FrameView::clear()
277 {
278     setCanBlitOnScroll(true);
279     
280     reset();
281
282     if (m_frame) {
283         if (RenderPart* renderer = m_frame->ownerRenderer())
284             renderer->viewCleared();
285     }
286
287     setScrollbarsSuppressed(true);
288 }
289
290 bool FrameView::didFirstLayout() const
291 {
292     return !m_firstLayout;
293 }
294
295 void FrameView::invalidateRect(const IntRect& rect)
296 {
297     if (!parent()) {
298         if (hostWindow())
299             hostWindow()->invalidateContentsAndWindow(rect, false /*immediate*/);
300         return;
301     }
302
303     if (!m_frame)
304         return;
305
306     RenderPart* renderer = m_frame->ownerRenderer();
307     if (!renderer)
308         return;
309
310     IntRect repaintRect = rect;
311     repaintRect.move(renderer->borderLeft() + renderer->paddingLeft(),
312                      renderer->borderTop() + renderer->paddingTop());
313     renderer->repaintRectangle(repaintRect);
314 }
315
316 void FrameView::setMarginWidth(int w)
317 {
318     // make it update the rendering area when set
319     m_margins.setWidth(w);
320 }
321
322 void FrameView::setMarginHeight(int h)
323 {
324     // make it update the rendering area when set
325     m_margins.setHeight(h);
326 }
327
328 bool FrameView::avoidScrollbarCreation()
329 {
330     ASSERT(m_frame);
331
332     // with frame flattening no subframe can have scrollbars
333     // but we also cannot turn scrollbars of as we determine
334     // our flattening policy using that.
335
336     if (!m_frame->ownerElement())
337         return false;
338
339     if (!m_frame->settings() || m_frame->settings()->frameFlatteningEnabled())
340         return true;
341
342     return false;
343 }
344
345 void FrameView::setCanHaveScrollbars(bool canHaveScrollbars)
346 {
347     m_canHaveScrollbars = canHaveScrollbars;
348     ScrollView::setCanHaveScrollbars(canHaveScrollbars);
349 }
350
351 void FrameView::updateCanHaveScrollbars()
352 {
353     ScrollbarMode hMode;
354     ScrollbarMode vMode;
355     scrollbarModes(hMode, vMode);
356     if (hMode == ScrollbarAlwaysOff && vMode == ScrollbarAlwaysOff)
357         m_canHaveScrollbars = false;
358     else
359         m_canHaveScrollbars = true;
360 }
361
362 PassRefPtr<Scrollbar> FrameView::createScrollbar(ScrollbarOrientation orientation)
363 {
364     // FIXME: We need to update the scrollbar dynamically as documents change (or as doc elements and bodies get discovered that have custom styles).
365     Document* doc = m_frame->document();
366
367     // Try the <body> element first as a scrollbar source.
368     Element* body = doc ? doc->body() : 0;
369     if (body && body->renderer() && body->renderer()->style()->hasPseudoStyle(SCROLLBAR))
370         return RenderScrollbar::createCustomScrollbar(this, orientation, body->renderer()->enclosingBox());
371     
372     // If the <body> didn't have a custom style, then the root element might.
373     Element* docElement = doc ? doc->documentElement() : 0;
374     if (docElement && docElement->renderer() && docElement->renderer()->style()->hasPseudoStyle(SCROLLBAR))
375         return RenderScrollbar::createCustomScrollbar(this, orientation, docElement->renderBox());
376         
377     // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
378     RenderPart* frameRenderer = m_frame->ownerRenderer();
379     if (frameRenderer && frameRenderer->style()->hasPseudoStyle(SCROLLBAR))
380         return RenderScrollbar::createCustomScrollbar(this, orientation, frameRenderer);
381     
382     // Nobody set a custom style, so we just use a native scrollbar.
383     return ScrollView::createScrollbar(orientation);
384 }
385
386 void FrameView::setContentsSize(const IntSize& size)
387 {
388     if (size == contentsSize())
389         return;
390
391     m_deferSetNeedsLayouts++;
392
393     ScrollView::setContentsSize(size);
394
395     Page* page = frame() ? frame()->page() : 0;
396     if (!page)
397         return;
398
399     page->chrome()->contentsSizeChanged(frame(), size); //notify only
400     
401     m_deferSetNeedsLayouts--;
402     
403     if (!m_deferSetNeedsLayouts)
404         m_setNeedsLayoutWasDeferred = false; // FIXME: Find a way to make the deferred layout actually happen.
405 }
406
407 void FrameView::adjustViewSize()
408 {
409     ASSERT(m_frame->view() == this);
410     RenderView* root = m_frame->contentRenderer();
411     if (!root)
412         return;
413     setContentsSize(IntSize(root->rightLayoutOverflow(), root->bottomLayoutOverflow()));
414 }
415
416 void FrameView::applyOverflowToViewport(RenderObject* o, ScrollbarMode& hMode, ScrollbarMode& vMode)
417 {
418     // Handle the overflow:hidden/scroll case for the body/html elements.  WinIE treats
419     // overflow:hidden and overflow:scroll on <body> as applying to the document's
420     // scrollbars.  The CSS2.1 draft states that HTML UAs should use the <html> or <body> element and XML/XHTML UAs should
421     // use the root element.
422     switch (o->style()->overflowX()) {
423         case OHIDDEN:
424             hMode = ScrollbarAlwaysOff;
425             break;
426         case OSCROLL:
427             hMode = ScrollbarAlwaysOn;
428             break;
429         case OAUTO:
430             hMode = ScrollbarAuto;
431             break;
432         default:
433             // Don't set it at all.
434             ;
435     }
436     
437      switch (o->style()->overflowY()) {
438         case OHIDDEN:
439             vMode = ScrollbarAlwaysOff;
440             break;
441         case OSCROLL:
442             vMode = ScrollbarAlwaysOn;
443             break;
444         case OAUTO:
445             vMode = ScrollbarAuto;
446             break;
447         default:
448             // Don't set it at all.
449             ;
450     }
451
452     m_viewportRenderer = o;
453 }
454
455 #if USE(ACCELERATED_COMPOSITING)
456 void FrameView::updateCompositingLayers()
457 {
458     RenderView* view = m_frame->contentRenderer();
459     if (!view)
460         return;
461
462     // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
463     view->compositor()->cacheAcceleratedCompositingFlags();
464     
465     if (!view->usesCompositing())
466         return;
467
468     view->compositor()->updateCompositingLayers(CompositingUpdateAfterLayoutOrStyleChange);
469 }
470
471 void FrameView::setNeedsOneShotDrawingSynchronization()
472 {
473     Page* page = frame() ? frame()->page() : 0;
474     if (page)
475         page->chrome()->client()->setNeedsOneShotDrawingSynchronization();
476 }
477 #endif // USE(ACCELERATED_COMPOSITING)
478
479 bool FrameView::syncCompositingStateRecursive()
480 {
481 #if USE(ACCELERATED_COMPOSITING)
482     ASSERT(m_frame->view() == this);
483     RenderView* contentRenderer = m_frame->contentRenderer();
484     if (!contentRenderer)
485         return true;    // We don't want to keep trying to update layers if we have no renderer.
486
487     if (m_layoutTimer.isActive()) {
488         // Don't sync layers if there's a layout pending.
489         return false;
490     }
491     
492     if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
493         rootLayer->syncCompositingState();
494
495     bool allSubframesSynced = true;
496     const HashSet<RefPtr<Widget> >* viewChildren = children();
497     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
498     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
499         Widget* widget = (*current).get();
500         if (widget->isFrameView()) {
501             bool synced = static_cast<FrameView*>(widget)->syncCompositingStateRecursive();
502             allSubframesSynced &= synced;
503         }
504     }
505     return allSubframesSynced;
506 #else // USE(ACCELERATED_COMPOSITING)
507     return true;
508 #endif
509 }
510
511 bool FrameView::isSoftwareRenderable() const
512 {
513 #if USE(ACCELERATED_COMPOSITING)
514     RenderView* view = m_frame->contentRenderer();
515     if (!view)
516         return true;
517
518     return !view->compositor()->has3DContent();
519 #else
520     return true;
521 #endif
522 }
523
524 void FrameView::didMoveOnscreen()
525 {
526     RenderView* view = m_frame->contentRenderer();
527     if (view)
528         view->didMoveOnscreen();
529 }
530
531 void FrameView::willMoveOffscreen()
532 {
533     RenderView* view = m_frame->contentRenderer();
534     if (view)
535         view->willMoveOffscreen();
536 }
537
538 RenderObject* FrameView::layoutRoot(bool onlyDuringLayout) const
539 {
540     return onlyDuringLayout && layoutPending() ? 0 : m_layoutRoot;
541 }
542
543 void FrameView::layout(bool allowSubtree)
544 {
545     if (m_midLayout)
546         return;
547
548     m_layoutTimer.stop();
549     m_delayedLayout = false;
550     m_setNeedsLayoutWasDeferred = false;
551
552     // Protect the view from being deleted during layout (in recalcStyle)
553     RefPtr<FrameView> protector(this);
554
555     if (!m_frame) {
556         // FIXME: Do we need to set m_size.width here?
557         // FIXME: Should we set m_size.height here too?
558         m_size.setWidth(layoutWidth());
559         return;
560     }
561     
562     // we shouldn't enter layout() while painting
563     ASSERT(!isPainting());
564     if (isPainting())
565         return;
566
567 #if ENABLE(INSPECTOR)    
568     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
569         timelineAgent->willLayout();
570 #endif
571
572     if (!allowSubtree && m_layoutRoot) {
573         m_layoutRoot->markContainingBlocksForLayout(false);
574         m_layoutRoot = 0;
575     }
576
577     ASSERT(m_frame->view() == this);
578     // This early return should be removed when rdar://5598072 is resolved. In the meantime, there is a
579     // gigantic CrashTracer because of this issue, and the early return will hopefully cause graceful 
580     // failure instead.  
581     if (m_frame->view() != this)
582         return;
583
584     Document* document = m_frame->document();
585
586     m_layoutSchedulingEnabled = false;
587
588     if (!m_nestedLayoutCount && m_postLayoutTasksTimer.isActive()) {
589         // This is a new top-level layout. If there are any remaining tasks from the previous
590         // layout, finish them now.
591         m_postLayoutTasksTimer.stop();
592         performPostLayoutTasks();
593     }
594
595     // Viewport-dependent media queries may cause us to need completely different style information.
596     // Check that here.
597     if (document->styleSelector()->affectedByViewportChange())
598         document->updateStyleSelector();
599
600     // Always ensure our style info is up-to-date.  This can happen in situations where
601     // the layout beats any sort of style recalc update that needs to occur.
602     if (m_frame->needsReapplyStyles())
603         m_frame->reapplyStyles();
604     else if (document->childNeedsStyleRecalc())
605         document->recalcStyle();
606     
607     bool subtree = m_layoutRoot;
608
609     // If there is only one ref to this view left, then its going to be destroyed as soon as we exit, 
610     // so there's no point to continuing to layout
611     if (protector->hasOneRef())
612         return;
613
614     RenderObject* root = subtree ? m_layoutRoot : document->renderer();
615     if (!root) {
616         // FIXME: Do we need to set m_size here?
617         m_layoutSchedulingEnabled = true;
618         return;
619     }
620
621     m_nestedLayoutCount++;
622
623     ScrollbarMode hMode;
624     ScrollbarMode vMode;
625     if (m_canHaveScrollbars) {
626         hMode = ScrollbarAuto;
627         vMode = ScrollbarAuto;
628     } else {
629         hMode = ScrollbarAlwaysOff;
630         vMode = ScrollbarAlwaysOff;
631     }
632
633     if (!subtree) {
634         Node* documentElement = document->documentElement();
635         RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0;
636         Node* body = document->body();
637         if (body && body->renderer()) {
638             if (body->hasTagName(framesetTag) && !m_frame->settings()->frameFlatteningEnabled()) {
639                 body->renderer()->setChildNeedsLayout(true);
640                 vMode = ScrollbarAlwaysOff;
641                 hMode = ScrollbarAlwaysOff;
642             } else if (body->hasTagName(bodyTag)) {
643                 if (!m_firstLayout && m_size.height() != layoutHeight() && body->renderer()->enclosingBox()->stretchesToViewHeight())
644                     body->renderer()->setChildNeedsLayout(true);
645                 // It's sufficient to just check the X overflow,
646                 // since it's illegal to have visible in only one direction.
647                 RenderObject* o = rootRenderer->style()->overflowX() == OVISIBLE && document->documentElement()->hasTagName(htmlTag) ? body->renderer() : rootRenderer;
648                 applyOverflowToViewport(o, hMode, vMode);
649             }
650         } else if (rootRenderer) {
651 #if ENABLE(SVG)
652             if (documentElement->isSVGElement()) {
653                 if (!m_firstLayout && (m_size.width() != layoutWidth() || m_size.height() != layoutHeight()))
654                     rootRenderer->setChildNeedsLayout(true);
655             } else
656                 applyOverflowToViewport(rootRenderer, hMode, vMode);
657 #else
658             applyOverflowToViewport(rootRenderer, hMode, vMode);
659 #endif
660         }
661 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
662         if (m_firstLayout && !document->ownerElement())
663             printf("Elapsed time before first layout: %d\n", document->elapsedTime());
664 #endif
665     }
666
667     m_doFullRepaint = !subtree && (m_firstLayout || toRenderView(root)->printing());
668
669     if (!subtree) {
670         // Now set our scrollbar state for the layout.
671         ScrollbarMode currentHMode = horizontalScrollbarMode();
672         ScrollbarMode currentVMode = verticalScrollbarMode();
673
674         if (m_firstLayout || (hMode != currentHMode || vMode != currentVMode)) {
675             if (m_firstLayout) {
676                 setScrollbarsSuppressed(true);
677
678                 m_firstLayout = false;
679                 m_firstLayoutCallbackPending = true;
680                 m_lastLayoutSize = IntSize(width(), height());
681                 m_lastZoomFactor = root->style()->zoom();
682
683                 // Set the initial vMode to AlwaysOn if we're auto.
684                 if (vMode == ScrollbarAuto)
685                     setVerticalScrollbarMode(ScrollbarAlwaysOn); // This causes a vertical scrollbar to appear.
686                 // Set the initial hMode to AlwaysOff if we're auto.
687                 if (hMode == ScrollbarAuto)
688                     setHorizontalScrollbarMode(ScrollbarAlwaysOff); // This causes a horizontal scrollbar to disappear.
689
690                 setScrollbarModes(hMode, vMode);
691                 setScrollbarsSuppressed(false, true);
692             } else
693                 setScrollbarModes(hMode, vMode);
694         }
695
696         IntSize oldSize = m_size;
697
698         m_size = IntSize(layoutWidth(), layoutHeight());
699
700         if (oldSize != m_size)
701             m_doFullRepaint = true;
702     }
703
704     RenderLayer* layer = root->enclosingLayer();
705
706     pauseScheduledEvents();
707
708     bool disableLayoutState = false;
709     if (subtree) {
710         RenderView* view = root->view();
711         disableLayoutState = view->shouldDisableLayoutStateForSubtree(root);
712         view->pushLayoutState(root);
713         if (disableLayoutState)
714             view->disableLayoutState();
715     }
716         
717     m_midLayout = true;
718     beginDeferredRepaints();
719     root->layout();
720     endDeferredRepaints();
721     m_midLayout = false;
722
723     if (subtree) {
724         RenderView* view = root->view();
725         view->popLayoutState();
726         if (disableLayoutState)
727             view->enableLayoutState();
728     }
729     m_layoutRoot = 0;
730
731     m_frame->selection()->setNeedsLayout();
732     m_frame->selection()->updateAppearance();
733    
734     m_layoutSchedulingEnabled = true;
735
736     if (!subtree && !toRenderView(root)->printing())
737         adjustViewSize();
738
739     // Now update the positions of all layers.
740     beginDeferredRepaints();
741     IntPoint cachedOffset;
742     layer->updateLayerPositions((m_doFullRepaint ? RenderLayer::DoFullRepaint : 0)
743                                 | RenderLayer::CheckForRepaint
744                                 | RenderLayer::IsCompositingUpdateRoot
745                                 | RenderLayer::UpdateCompositingLayers,
746                                 subtree ? 0 : &cachedOffset);
747     endDeferredRepaints();
748
749 #if USE(ACCELERATED_COMPOSITING)
750     updateCompositingLayers();
751 #endif
752     
753     m_layoutCount++;
754
755 #if PLATFORM(MAC)
756     if (AXObjectCache::accessibilityEnabled())
757         root->document()->axObjectCache()->postNotification(root, AXObjectCache::AXLayoutComplete, true);
758 #endif
759 #if ENABLE(DASHBOARD_SUPPORT)
760     updateDashboardRegions();
761 #endif
762
763     ASSERT(!root->needsLayout());
764
765     setCanBlitOnScroll(!useSlowRepaints());
766
767     if (document->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
768         updateOverflowStatus(layoutWidth() < contentsWidth(),
769                              layoutHeight() < contentsHeight());
770
771     if (!m_postLayoutTasksTimer.isActive()) {
772         // Calls resumeScheduledEvents()
773         performPostLayoutTasks();
774
775         if (!m_postLayoutTasksTimer.isActive() && needsLayout()) {
776             // Post-layout widget updates or an event handler made us need layout again.
777             // Lay out again, but this time defer widget updates and event dispatch until after
778             // we return.
779             m_postLayoutTasksTimer.startOneShot(0);
780             pauseScheduledEvents();
781             layout();
782         }
783     } else {
784         resumeScheduledEvents();
785         ASSERT(m_enqueueEvents);
786     }
787
788 #if ENABLE(INSPECTOR)
789     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
790         timelineAgent->didLayout();
791 #endif
792
793     m_nestedLayoutCount--;
794 }
795
796 void FrameView::addWidgetToUpdate(RenderEmbeddedObject* object)
797 {
798     if (!m_widgetUpdateSet)
799         m_widgetUpdateSet.set(new RenderEmbeddedObjectSet);
800
801     m_widgetUpdateSet->add(object);
802 }
803
804 void FrameView::removeWidgetToUpdate(RenderEmbeddedObject* object)
805 {
806     if (!m_widgetUpdateSet)
807         return;
808
809     m_widgetUpdateSet->remove(object);
810 }
811
812 void FrameView::setMediaType(const String& mediaType)
813 {
814     m_mediaType = mediaType;
815 }
816
817 String FrameView::mediaType() const
818 {
819     // See if we have an override type.
820     String overrideType = m_frame->loader()->client()->overrideMediaType();
821     if (!overrideType.isNull())
822         return overrideType;
823     return m_mediaType;
824 }
825
826 void FrameView::adjustMediaTypeForPrinting(bool printing)
827 {
828     if (printing) {
829         if (m_mediaTypeWhenNotPrinting.isNull())
830             m_mediaTypeWhenNotPrinting = mediaType();
831             setMediaType("print");
832     } else {
833         if (!m_mediaTypeWhenNotPrinting.isNull())
834             setMediaType(m_mediaTypeWhenNotPrinting);
835         m_mediaTypeWhenNotPrinting = String();
836     }
837 }
838
839 bool FrameView::useSlowRepaints() const
840 {
841     return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || m_isOverlapped || !m_contentIsOpaque;
842 }
843
844 bool FrameView::useSlowRepaintsIfNotOverlapped() const
845 {
846     return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || !m_contentIsOpaque;
847 }
848
849 void FrameView::setUseSlowRepaints()
850 {
851     m_useSlowRepaints = true;
852     setCanBlitOnScroll(false);
853 }
854
855 void FrameView::addSlowRepaintObject()
856 {
857     if (!m_slowRepaintObjectCount)
858         setCanBlitOnScroll(false);
859     m_slowRepaintObjectCount++;
860 }
861
862 void FrameView::removeSlowRepaintObject()
863 {
864     ASSERT(m_slowRepaintObjectCount > 0);
865     m_slowRepaintObjectCount--;
866     if (!m_slowRepaintObjectCount)
867         setCanBlitOnScroll(!useSlowRepaints());
868 }
869
870 void FrameView::addFixedObject()
871 {
872     if (!m_fixedObjectCount && platformWidget())
873         setCanBlitOnScroll(false);
874     ++m_fixedObjectCount;
875 }
876
877 void FrameView::removeFixedObject()
878 {
879     ASSERT(m_fixedObjectCount > 0);
880     --m_fixedObjectCount;
881     if (!m_fixedObjectCount)
882         setCanBlitOnScroll(!useSlowRepaints());
883 }
884
885 bool FrameView::scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
886 {
887     const size_t fixedObjectThreshold = 5;
888
889     ListHashSet<RenderBox*>* positionedObjects = 0;
890     if (RenderView* root = m_frame->contentRenderer())
891         positionedObjects = root->positionedObjects();
892
893     if (!positionedObjects || positionedObjects->isEmpty()) {
894         hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
895         return true;
896     }
897
898     // Get the rects of the fixed objects visible in the rectToScroll
899     Vector<IntRect, fixedObjectThreshold> subRectToUpdate;
900     bool updateInvalidatedSubRect = true;
901     ListHashSet<RenderBox*>::const_iterator end = positionedObjects->end();
902     for (ListHashSet<RenderBox*>::const_iterator it = positionedObjects->begin(); it != end; ++it) {
903         RenderBox* renderBox = *it;
904         if (renderBox->style()->position() != FixedPosition)
905             continue;
906         IntRect topLevelRect;
907         IntRect updateRect = renderBox->paintingRootRect(topLevelRect);
908         updateRect.move(-scrollX(), -scrollY());
909         updateRect.intersect(rectToScroll);
910         if (!updateRect.isEmpty()) {
911             if (subRectToUpdate.size() >= fixedObjectThreshold) {
912                 updateInvalidatedSubRect = false;
913                 break;
914             }
915             subRectToUpdate.append(updateRect);
916         }
917     }
918
919     // Scroll the view
920     if (updateInvalidatedSubRect) {
921         // 1) scroll
922         hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
923
924         // 2) update the area of fixed objects that has been invalidated
925         size_t fixObjectsCount = subRectToUpdate.size();
926         for (size_t i = 0; i < fixObjectsCount; ++i) {
927             IntRect updateRect = subRectToUpdate[i];
928             IntRect scrolledRect = updateRect;
929             scrolledRect.move(scrollDelta);
930             updateRect.unite(scrolledRect);
931             updateRect.intersect(rectToScroll);
932             hostWindow()->invalidateContentsAndWindow(updateRect, false);
933         }
934         return true;
935     }
936
937     // the number of fixed objects exceed the threshold, we cannot use the fast path
938     return false;
939 }
940
941 void FrameView::setIsOverlapped(bool isOverlapped)
942 {
943     if (isOverlapped == m_isOverlapped)
944         return;
945
946     m_isOverlapped = isOverlapped;
947     setCanBlitOnScroll(!useSlowRepaints());
948 }
949
950 void FrameView::setContentIsOpaque(bool contentIsOpaque)
951 {
952     if (contentIsOpaque == m_contentIsOpaque)
953         return;
954
955     m_contentIsOpaque = contentIsOpaque;
956     setCanBlitOnScroll(!useSlowRepaints());
957 }
958
959 void FrameView::restoreScrollbar()
960 {
961     setScrollbarsSuppressed(false);
962 }
963
964 bool FrameView::scrollToFragment(const KURL& url)
965 {
966     // If our URL has no ref, then we have no place we need to jump to.
967     // OTOH If CSS target was set previously, we want to set it to 0, recalc
968     // and possibly repaint because :target pseudo class may have been
969     // set (see bug 11321).
970     if (!url.hasFragmentIdentifier() && !m_frame->document()->cssTarget())
971         return false;
972
973     String fragmentIdentifier = url.fragmentIdentifier();
974     if (scrollToAnchor(fragmentIdentifier))
975         return true;
976
977     // Try again after decoding the ref, based on the document's encoding.
978     if (TextResourceDecoder* decoder = m_frame->document()->decoder())
979         return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
980
981     return false;
982 }
983
984 bool FrameView::scrollToAnchor(const String& name)
985 {
986     ASSERT(m_frame->document());
987
988     if (!m_frame->document()->haveStylesheetsLoaded()) {
989         m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(true);
990         return false;
991     }
992
993     m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(false);
994
995     Element* anchorNode = m_frame->document()->findAnchor(name);
996
997 #if ENABLE(SVG)
998     if (m_frame->document()->isSVGDocument()) {
999         if (name.startsWith("xpointer(")) {
1000             // We need to parse the xpointer reference here
1001         } else if (name.startsWith("svgView(")) {
1002             RefPtr<SVGSVGElement> svg = static_cast<SVGDocument*>(m_frame->document())->rootElement();
1003             if (!svg->currentView()->parseViewSpec(name))
1004                 return false;
1005             svg->setUseCurrentView(true);
1006         } else {
1007             if (anchorNode && anchorNode->hasTagName(SVGNames::viewTag)) {
1008                 RefPtr<SVGViewElement> viewElement = anchorNode->hasTagName(SVGNames::viewTag) ? static_cast<SVGViewElement*>(anchorNode) : 0;
1009                 if (viewElement.get()) {
1010                     RefPtr<SVGSVGElement> svg = static_cast<SVGSVGElement*>(SVGLocatable::nearestViewportElement(viewElement.get()));
1011                     svg->inheritViewAttributes(viewElement.get());
1012                 }
1013             }
1014         }
1015         // FIXME: need to decide which <svg> to focus on, and zoom to that one
1016         // FIXME: need to actually "highlight" the viewTarget(s)
1017     }
1018 #endif
1019
1020     m_frame->document()->setCSSTarget(anchorNode); // Setting to null will clear the current target.
1021   
1022     // Implement the rule that "" and "top" both mean top of page as in other browsers.
1023     if (!anchorNode && !(name.isEmpty() || equalIgnoringCase(name, "top")))
1024         return false;
1025
1026     maintainScrollPositionAtAnchor(anchorNode ? static_cast<Node*>(anchorNode) : m_frame->document());
1027     return true;
1028 }
1029
1030 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
1031 {
1032     m_maintainScrollPositionAnchor = anchorNode;
1033     if (!m_maintainScrollPositionAnchor)
1034         return;
1035
1036     // We need to update the layout before scrolling, otherwise we could
1037     // really mess things up if an anchor scroll comes at a bad moment.
1038     m_frame->document()->updateStyleIfNeeded();
1039     // Only do a layout if changes have occurred that make it necessary.
1040     if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout())
1041         layout();
1042     else
1043         scrollToAnchor();
1044 }
1045
1046 void FrameView::scrollRectIntoViewRecursively(const IntRect& r)
1047 {
1048     bool wasInProgrammaticScroll = m_inProgrammaticScroll;
1049     m_inProgrammaticScroll = true;
1050     m_maintainScrollPositionAnchor = 0;
1051     ScrollView::scrollRectIntoViewRecursively(r);
1052     m_inProgrammaticScroll = wasInProgrammaticScroll;
1053 }
1054
1055 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
1056 {
1057     bool wasInProgrammaticScroll = m_inProgrammaticScroll;
1058     m_inProgrammaticScroll = true;
1059     m_maintainScrollPositionAnchor = 0;
1060     ScrollView::setScrollPosition(scrollPoint);
1061     m_inProgrammaticScroll = wasInProgrammaticScroll;
1062 }
1063
1064 void FrameView::scrollPositionChanged()
1065 {
1066     frame()->eventHandler()->sendScrollEvent();
1067
1068     // For fixed position elements, update widget positions and compositing layers after scrolling,
1069     // but only if we're not inside of layout.
1070     // FIXME: we could skip this if we knew the page had no fixed position elements.
1071     if (!m_nestedLayoutCount) {
1072         if (RenderView* root = m_frame->contentRenderer()) {
1073             root->updateWidgetPositions();
1074 #if USE(ACCELERATED_COMPOSITING)
1075             if (root->usesCompositing())
1076                 root->compositor()->updateCompositingLayers(CompositingUpdateOnScroll);
1077 #endif
1078         }
1079     }
1080 }
1081
1082 HostWindow* FrameView::hostWindow() const
1083 {
1084     Page* page = frame() ? frame()->page() : 0;
1085     if (!page)
1086         return 0;
1087     return page->chrome();
1088 }
1089
1090 const unsigned cRepaintRectUnionThreshold = 25;
1091
1092 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
1093 {
1094     ASSERT(!m_frame->document()->ownerElement());
1095
1096     double delay = adjustedDeferredRepaintDelay();
1097     if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
1098         IntRect paintRect = r;
1099         if (!paintsEntireContents())
1100             paintRect.intersect(visibleContentRect());
1101         if (paintRect.isEmpty())
1102             return;
1103         if (m_repaintCount == cRepaintRectUnionThreshold) {
1104             IntRect unionedRect;
1105             for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
1106                 unionedRect.unite(m_repaintRects[i]);
1107             m_repaintRects.clear();
1108             m_repaintRects.append(unionedRect);
1109         }
1110         if (m_repaintCount < cRepaintRectUnionThreshold)
1111             m_repaintRects.append(paintRect);
1112         else
1113             m_repaintRects[0].unite(paintRect);
1114         m_repaintCount++;
1115     
1116         if (!m_deferringRepaints && !m_deferredRepaintTimer.isActive())
1117              m_deferredRepaintTimer.startOneShot(delay);
1118         return;
1119     }
1120     
1121     if (!immediate && isOffscreen() && !shouldUpdateWhileOffscreen())
1122         return;
1123
1124 #if ENABLE(TILED_BACKING_STORE)
1125     if (frame()->tiledBackingStore()) {
1126         frame()->tiledBackingStore()->invalidate(r);
1127         return;
1128     }
1129 #endif
1130     ScrollView::repaintContentRectangle(r, immediate);
1131 }
1132
1133 void FrameView::visibleContentsResized()
1134 {
1135     // We check to make sure the view is attached to a frame() as this method can
1136     // be triggered before the view is attached by Frame::createView(...) setting
1137     // various values such as setScrollBarModes(...) for example.  An ASSERT is
1138     // triggered when a view is layout before being attached to a frame().
1139     if (!frame()->view())
1140         return;
1141
1142     if (needsLayout())
1143         layout();
1144 }
1145
1146 void FrameView::beginDeferredRepaints()
1147 {
1148     Page* page = m_frame->page();
1149     if (page->mainFrame() != m_frame)
1150         return page->mainFrame()->view()->beginDeferredRepaints();
1151
1152     m_deferringRepaints++;
1153 }
1154
1155
1156 void FrameView::endDeferredRepaints()
1157 {
1158     Page* page = m_frame->page();
1159     if (page->mainFrame() != m_frame)
1160         return page->mainFrame()->view()->endDeferredRepaints();
1161
1162     ASSERT(m_deferringRepaints > 0);
1163
1164     if (--m_deferringRepaints)
1165         return;
1166     
1167     if (m_deferredRepaintTimer.isActive())
1168         return;
1169
1170     if (double delay = adjustedDeferredRepaintDelay()) {
1171         m_deferredRepaintTimer.startOneShot(delay);
1172         return;
1173     }
1174     
1175     doDeferredRepaints();
1176 }
1177
1178 void FrameView::checkStopDelayingDeferredRepaints()
1179 {
1180     if (!m_deferredRepaintTimer.isActive())
1181         return;
1182
1183     Document* document = m_frame->document();
1184     if (document && (document->parsing() || document->docLoader()->requestCount()))
1185         return;
1186     
1187     m_deferredRepaintTimer.stop();
1188
1189     doDeferredRepaints();
1190 }
1191     
1192 void FrameView::doDeferredRepaints()
1193 {
1194     ASSERT(!m_deferringRepaints);
1195     if (isOffscreen() && !shouldUpdateWhileOffscreen()) {
1196         m_repaintRects.clear();
1197         m_repaintCount = 0;
1198         return;
1199     }
1200     unsigned size = m_repaintRects.size();
1201     for (unsigned i = 0; i < size; i++) {
1202 #if ENABLE(TILED_BACKING_STORE)
1203         if (frame()->tiledBackingStore()) {
1204             frame()->tiledBackingStore()->invalidate(m_repaintRects[i]);
1205             continue;
1206         }
1207 #endif
1208         ScrollView::repaintContentRectangle(m_repaintRects[i], false);
1209     }
1210     m_repaintRects.clear();
1211     m_repaintCount = 0;
1212     
1213     updateDeferredRepaintDelay();
1214 }
1215
1216 void FrameView::updateDeferredRepaintDelay()
1217 {
1218     Document* document = m_frame->document();
1219     if (!document || (!document->parsing() && !document->docLoader()->requestCount())) {
1220         m_deferredRepaintDelay = deferredRepaintDelay;
1221         return;
1222     }
1223     if (m_deferredRepaintDelay < maxDeferredRepaintDelayDuringLoading) {
1224         m_deferredRepaintDelay += deferredRepaintDelayIncrementDuringLoading;
1225         if (m_deferredRepaintDelay > maxDeferredRepaintDelayDuringLoading)
1226             m_deferredRepaintDelay = maxDeferredRepaintDelayDuringLoading;
1227     }
1228 }
1229
1230 void FrameView::resetDeferredRepaintDelay()
1231 {
1232     m_deferredRepaintDelay = 0;
1233     if (m_deferredRepaintTimer.isActive()) {
1234         m_deferredRepaintTimer.stop();
1235         if (!m_deferringRepaints)
1236             doDeferredRepaints();
1237     }
1238 }
1239
1240 double FrameView::adjustedDeferredRepaintDelay() const
1241 {
1242     if (!m_deferredRepaintDelay)
1243         return 0;
1244     double timeSinceLastPaint = currentTime() - m_lastPaintTime;
1245     return max(0., m_deferredRepaintDelay - timeSinceLastPaint);
1246 }
1247     
1248 void FrameView::deferredRepaintTimerFired(Timer<FrameView>*)
1249 {
1250     doDeferredRepaints();
1251 }    
1252
1253 void FrameView::layoutTimerFired(Timer<FrameView>*)
1254 {
1255 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1256     if (!m_frame->document()->ownerElement())
1257         printf("Layout timer fired at %d\n", m_frame->document()->elapsedTime());
1258 #endif
1259     layout();
1260 }
1261
1262 void FrameView::scheduleRelayout()
1263 {
1264     // FIXME: We should assert the page is not in the page cache, but that is causing
1265     // too many false assertions.  See <rdar://problem/7218118>.
1266     ASSERT(m_frame->view() == this);
1267
1268     if (m_layoutRoot) {
1269         m_layoutRoot->markContainingBlocksForLayout(false);
1270         m_layoutRoot = 0;
1271     }
1272     if (!m_layoutSchedulingEnabled)
1273         return;
1274     if (!needsLayout())
1275         return;
1276     if (!m_frame->document()->shouldScheduleLayout())
1277         return;
1278
1279     // When frameset flattening is enabled, the contents of the frame affects layout of the parent frames.
1280     // Also invalidate parent frame starting from the owner element of this frame.
1281     if (m_frame->settings()->frameFlatteningEnabled() && m_frame->ownerRenderer() && m_frame->ownerElement()->hasTagName(frameTag))
1282         m_frame->ownerRenderer()->setNeedsLayout(true, true);
1283
1284     int delay = m_frame->document()->minimumLayoutDelay();
1285     if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
1286         unscheduleRelayout();
1287     if (m_layoutTimer.isActive())
1288         return;
1289
1290     m_delayedLayout = delay != 0;
1291
1292 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1293     if (!m_frame->document()->ownerElement())
1294         printf("Scheduling layout for %d\n", delay);
1295 #endif
1296
1297     m_layoutTimer.startOneShot(delay * 0.001);
1298 }
1299
1300 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
1301 {
1302     for (RenderObject* r = descendant; r; r = r->container()) {
1303         if (r == ancestor)
1304             return true;
1305     }
1306     return false;
1307 }
1308
1309 void FrameView::scheduleRelayoutOfSubtree(RenderObject* relayoutRoot)
1310 {
1311     ASSERT(m_frame->view() == this);
1312
1313     if (!m_layoutSchedulingEnabled || (m_frame->contentRenderer()
1314             && m_frame->contentRenderer()->needsLayout())) {
1315         if (relayoutRoot)
1316             relayoutRoot->markContainingBlocksForLayout(false);
1317         return;
1318     }
1319
1320     if (layoutPending()) {
1321         if (m_layoutRoot != relayoutRoot) {
1322             if (isObjectAncestorContainerOf(m_layoutRoot, relayoutRoot)) {
1323                 // Keep the current root
1324                 relayoutRoot->markContainingBlocksForLayout(false, m_layoutRoot);
1325             } else if (m_layoutRoot && isObjectAncestorContainerOf(relayoutRoot, m_layoutRoot)) {
1326                 // Re-root at relayoutRoot
1327                 m_layoutRoot->markContainingBlocksForLayout(false, relayoutRoot);
1328                 m_layoutRoot = relayoutRoot;
1329             } else {
1330                 // Just do a full relayout
1331                 if (m_layoutRoot)
1332                     m_layoutRoot->markContainingBlocksForLayout(false);
1333                 m_layoutRoot = 0;
1334                 relayoutRoot->markContainingBlocksForLayout(false);
1335             }
1336         }
1337     } else {
1338         int delay = m_frame->document()->minimumLayoutDelay();
1339         m_layoutRoot = relayoutRoot;
1340         m_delayedLayout = delay != 0;
1341         m_layoutTimer.startOneShot(delay * 0.001);
1342     }
1343 }
1344
1345 bool FrameView::layoutPending() const
1346 {
1347     return m_layoutTimer.isActive();
1348 }
1349
1350 bool FrameView::needsLayout() const
1351 {
1352     // This can return true in cases where the document does not have a body yet.
1353     // Document::shouldScheduleLayout takes care of preventing us from scheduling
1354     // layout in that case.
1355     if (!m_frame)
1356         return false;
1357     RenderView* root = m_frame->contentRenderer();
1358     Document* document = m_frame->document();
1359     return layoutPending()
1360         || (root && root->needsLayout())
1361         || m_layoutRoot
1362         || (document && document->childNeedsStyleRecalc()) // can occur when using WebKit ObjC interface
1363         || m_frame->needsReapplyStyles()
1364         || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
1365 }
1366
1367 void FrameView::setNeedsLayout()
1368 {
1369     if (m_deferSetNeedsLayouts) {
1370         m_setNeedsLayoutWasDeferred = true;
1371         return;
1372     }
1373     RenderView* root = m_frame->contentRenderer();
1374     if (root)
1375         root->setNeedsLayout(true);
1376 }
1377
1378 void FrameView::unscheduleRelayout()
1379 {
1380     if (!m_layoutTimer.isActive())
1381         return;
1382
1383 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1384     if (!m_frame->document()->ownerElement())
1385         printf("Layout timer unscheduled at %d\n", m_frame->document()->elapsedTime());
1386 #endif
1387     
1388     m_layoutTimer.stop();
1389     m_delayedLayout = false;
1390 }
1391
1392 bool FrameView::isTransparent() const
1393 {
1394     return m_isTransparent;
1395 }
1396
1397 void FrameView::setTransparent(bool isTransparent)
1398 {
1399     m_isTransparent = isTransparent;
1400 }
1401
1402 Color FrameView::baseBackgroundColor() const
1403 {
1404     return m_baseBackgroundColor;
1405 }
1406
1407 void FrameView::setBaseBackgroundColor(Color bc)
1408 {
1409     if (!bc.isValid())
1410         bc = Color::white;
1411     m_baseBackgroundColor = bc;
1412 }
1413
1414 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
1415 {
1416     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1417         FrameView* view = frame->view();
1418         if (!view)
1419             continue;
1420
1421         view->setTransparent(transparent);
1422         view->setBaseBackgroundColor(backgroundColor);
1423     }
1424 }
1425
1426 bool FrameView::shouldUpdateWhileOffscreen() const
1427 {
1428     return m_shouldUpdateWhileOffscreen;
1429 }
1430
1431 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
1432 {
1433     m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
1434 }
1435
1436 void FrameView::scheduleEvent(PassRefPtr<Event> event, PassRefPtr<Node> eventTarget)
1437 {
1438     if (!m_enqueueEvents) {
1439         ExceptionCode ec = 0;
1440         eventTarget->dispatchEvent(event, ec);
1441         return;
1442     }
1443
1444     ScheduledEvent* scheduledEvent = new ScheduledEvent;
1445     scheduledEvent->m_event = event;
1446     scheduledEvent->m_eventTarget = eventTarget;
1447     m_scheduledEvents.append(scheduledEvent);
1448 }
1449
1450 void FrameView::pauseScheduledEvents()
1451 {
1452     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1453     m_enqueueEvents++;
1454 }
1455
1456 void FrameView::resumeScheduledEvents()
1457 {
1458     m_enqueueEvents--;
1459     if (!m_enqueueEvents)
1460         dispatchScheduledEvents();
1461     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1462 }
1463
1464 void FrameView::scrollToAnchor()
1465 {
1466     RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
1467     if (!anchorNode)
1468         return;
1469
1470     if (!anchorNode->renderer())
1471         return;
1472
1473     IntRect rect;
1474     if (anchorNode != m_frame->document())
1475         rect = anchorNode->getRect();
1476
1477     // Scroll nested layers and frames to reveal the anchor.
1478     // Align to the top and to the closest side (this matches other browsers).
1479     anchorNode->renderer()->enclosingLayer()->scrollRectToVisible(rect, true, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
1480
1481     if (AXObjectCache::accessibilityEnabled())
1482         m_frame->document()->axObjectCache()->handleScrolledToAnchor(anchorNode.get());
1483
1484     // scrollRectToVisible can call into scrollRectIntoViewRecursively(), which resets m_maintainScrollPositionAnchor.
1485     m_maintainScrollPositionAnchor = anchorNode;
1486 }
1487
1488 bool FrameView::updateWidgets()
1489 {
1490     if (m_nestedLayoutCount > 1 || !m_widgetUpdateSet || m_widgetUpdateSet->isEmpty())
1491         return true;
1492     
1493     size_t size = m_widgetUpdateSet->size();
1494
1495     Vector<RenderEmbeddedObject*> objects;
1496     objects.reserveCapacity(size);
1497
1498     RenderEmbeddedObjectSet::const_iterator end = m_widgetUpdateSet->end();
1499     for (RenderEmbeddedObjectSet::const_iterator it = m_widgetUpdateSet->begin(); it != end; ++it) {
1500         objects.uncheckedAppend(*it);
1501         (*it)->ref();
1502     }
1503
1504     for (size_t i = 0; i < size; ++i) {
1505         RenderEmbeddedObject* object = objects[i];
1506
1507         // The object may have been destroyed, but our manual ref() keeps the object from being deleted.
1508         object->updateWidget(false);
1509         object->updateWidgetPosition();
1510
1511         m_widgetUpdateSet->remove(object);
1512     }
1513
1514     RenderArena* arena = m_frame->document()->renderArena();
1515     for (size_t i = 0; i < size; ++i)
1516         objects[i]->deref(arena);
1517     
1518     return m_widgetUpdateSet->isEmpty();
1519 }
1520     
1521 void FrameView::performPostLayoutTasks()
1522 {
1523     if (m_firstLayoutCallbackPending) {
1524         m_firstLayoutCallbackPending = false;
1525         m_frame->loader()->didFirstLayout();
1526     }
1527
1528     if (m_isVisuallyNonEmpty && m_firstVisuallyNonEmptyLayoutCallbackPending) {
1529         m_firstVisuallyNonEmptyLayoutCallbackPending = false;
1530         m_frame->loader()->didFirstVisuallyNonEmptyLayout();
1531     }
1532
1533     RenderView* root = m_frame->contentRenderer();
1534
1535     root->updateWidgetPositions();
1536     
1537     for (unsigned i = 0; i < maxUpdateWidgetsIterations; i++) {
1538         if (updateWidgets())
1539             break;
1540     }
1541
1542     scrollToAnchor();
1543
1544     resumeScheduledEvents();
1545
1546     if (!root->printing()) {
1547         IntSize currentSize = IntSize(width(), height());
1548         float currentZoomFactor = root->style()->zoom();
1549         bool resized = !m_firstLayout && (currentSize != m_lastLayoutSize || currentZoomFactor != m_lastZoomFactor);
1550         m_lastLayoutSize = currentSize;
1551         m_lastZoomFactor = currentZoomFactor;
1552         if (resized)
1553             m_frame->eventHandler()->sendResizeEvent();
1554     }
1555 }
1556
1557 void FrameView::postLayoutTimerFired(Timer<FrameView>*)
1558 {
1559     performPostLayoutTasks();
1560 }
1561
1562 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
1563 {
1564     if (!m_viewportRenderer)
1565         return;
1566     
1567     if (m_overflowStatusDirty) {
1568         m_horizontalOverflow = horizontalOverflow;
1569         m_verticalOverflow = verticalOverflow;
1570         m_overflowStatusDirty = false;
1571         return;
1572     }
1573     
1574     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
1575     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
1576     
1577     if (horizontalOverflowChanged || verticalOverflowChanged) {
1578         m_horizontalOverflow = horizontalOverflow;
1579         m_verticalOverflow = verticalOverflow;
1580         
1581         scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
1582             verticalOverflowChanged, verticalOverflow),
1583             m_viewportRenderer->node());
1584     }
1585     
1586 }
1587
1588 void FrameView::dispatchScheduledEvents()
1589 {
1590     if (m_scheduledEvents.isEmpty())
1591         return;
1592
1593     Vector<ScheduledEvent*> scheduledEventsCopy = m_scheduledEvents;
1594     m_scheduledEvents.clear();
1595     
1596     Vector<ScheduledEvent*>::iterator end = scheduledEventsCopy.end();
1597     for (Vector<ScheduledEvent*>::iterator it = scheduledEventsCopy.begin(); it != end; ++it) {
1598         ScheduledEvent* scheduledEvent = *it;
1599         
1600         ExceptionCode ec = 0;
1601         
1602         // Only dispatch events to nodes that are in the document
1603         if (scheduledEvent->m_eventTarget->inDocument())
1604             scheduledEvent->m_eventTarget->dispatchEvent(scheduledEvent->m_event, ec);
1605         
1606         delete scheduledEvent;
1607     }
1608 }
1609
1610 IntRect FrameView::windowClipRect(bool clipToContents) const
1611 {
1612     ASSERT(m_frame->view() == this);
1613
1614     // Set our clip rect to be our contents.
1615     IntRect clipRect = contentsToWindow(visibleContentRect(!clipToContents));
1616     if (!m_frame || !m_frame->document()->ownerElement())
1617         return clipRect;
1618
1619     // Take our owner element and get the clip rect from the enclosing layer.
1620     Element* elt = m_frame->document()->ownerElement();
1621     RenderLayer* layer = elt->renderer()->enclosingLayer();
1622     // FIXME: layer should never be null, but sometimes seems to be anyway.
1623     if (!layer)
1624         return clipRect;
1625     FrameView* parentView = elt->document()->view();
1626     clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
1627     return clipRect;
1628 }
1629
1630 IntRect FrameView::windowClipRectForLayer(const RenderLayer* layer, bool clipToLayerContents) const
1631 {
1632     // If we have no layer, just return our window clip rect.
1633     if (!layer)
1634         return windowClipRect();
1635
1636     // Apply the clip from the layer.
1637     IntRect clipRect;
1638     if (clipToLayerContents)
1639         clipRect = layer->childrenClipRect();
1640     else
1641         clipRect = layer->selfClipRect();
1642     clipRect = contentsToWindow(clipRect); 
1643     return intersection(clipRect, windowClipRect());
1644 }
1645
1646 bool FrameView::isActive() const
1647 {
1648     Page* page = frame()->page();
1649     return page && page->focusController()->isActive();
1650 }
1651
1652 void FrameView::valueChanged(Scrollbar* bar)
1653 {
1654     // Figure out if we really moved.
1655     IntSize offset = scrollOffset();
1656     ScrollView::valueChanged(bar);
1657     if (offset != scrollOffset())
1658         frame()->eventHandler()->sendScrollEvent();
1659     frame()->loader()->client()->didChangeScrollOffset();
1660 }
1661
1662 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
1663 {
1664     // Add in our offset within the FrameView.
1665     IntRect dirtyRect = rect;
1666     dirtyRect.move(scrollbar->x(), scrollbar->y());
1667     invalidateRect(dirtyRect);
1668 }
1669
1670 void FrameView::getTickmarks(Vector<IntRect>& tickmarks) const
1671 {
1672     tickmarks = frame()->document()->renderedRectsForMarkers(DocumentMarker::TextMatch);
1673 }
1674
1675 IntRect FrameView::windowResizerRect() const
1676 {
1677     Page* page = frame() ? frame()->page() : 0;
1678     if (!page)
1679         return IntRect();
1680     return page->chrome()->windowResizerRect();
1681 }
1682
1683 #if ENABLE(DASHBOARD_SUPPORT)
1684 void FrameView::updateDashboardRegions()
1685 {
1686     Document* document = m_frame->document();
1687     if (!document->hasDashboardRegions())
1688         return;
1689     Vector<DashboardRegionValue> newRegions;
1690     document->renderBox()->collectDashboardRegions(newRegions);
1691     if (newRegions == document->dashboardRegions())
1692         return;
1693     document->setDashboardRegions(newRegions);
1694     Page* page = m_frame->page();
1695     if (!page)
1696         return;
1697     page->chrome()->client()->dashboardRegionsChanged();
1698 }
1699 #endif
1700
1701 void FrameView::invalidateScrollCorner()
1702 {
1703     invalidateRect(scrollCornerRect());
1704 }
1705
1706 void FrameView::updateScrollCorner()
1707 {
1708     RenderObject* renderer = 0;
1709     RefPtr<RenderStyle> cornerStyle;
1710     
1711     if (!scrollCornerRect().isEmpty()) {
1712         // Try the <body> element first as a scroll corner source.
1713         Document* doc = m_frame->document();
1714         Element* body = doc ? doc->body() : 0;
1715         if (body && body->renderer()) {
1716             renderer = body->renderer();
1717             cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1718         }
1719         
1720         if (!cornerStyle) {
1721             // If the <body> didn't have a custom style, then the root element might.
1722             Element* docElement = doc ? doc->documentElement() : 0;
1723             if (docElement && docElement->renderer()) {
1724                 renderer = docElement->renderer();
1725                 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1726             }
1727         }
1728         
1729         if (!cornerStyle) {
1730             // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
1731             if (RenderPart* renderer = m_frame->ownerRenderer())
1732                 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1733         }
1734     }
1735
1736     if (cornerStyle) {
1737         if (!m_scrollCorner)
1738             m_scrollCorner = new (renderer->renderArena()) RenderScrollbarPart(renderer->document());
1739         m_scrollCorner->setStyle(cornerStyle.release());
1740         invalidateRect(scrollCornerRect());
1741     } else if (m_scrollCorner) {
1742         m_scrollCorner->destroy();
1743         m_scrollCorner = 0;
1744     }
1745 }
1746
1747 void FrameView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
1748 {
1749     if (context->updatingControlTints()) {
1750         updateScrollCorner();
1751         return;
1752     }
1753
1754     if (m_scrollCorner) {
1755         m_scrollCorner->paintIntoRect(context, cornerRect.x(), cornerRect.y(), cornerRect);
1756         return;
1757     }
1758
1759     ScrollView::paintScrollCorner(context, cornerRect);
1760 }
1761
1762 bool FrameView::hasCustomScrollbars() const
1763 {
1764     const HashSet<RefPtr<Widget> >* viewChildren = children();
1765     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1766     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1767         Widget* widget = current->get();
1768         if (widget->isFrameView()) {
1769             if (static_cast<FrameView*>(widget)->hasCustomScrollbars())
1770                 return true;
1771         } else if (widget->isScrollbar()) {
1772             Scrollbar* scrollbar = static_cast<Scrollbar*>(widget);
1773             if (scrollbar->isCustomScrollbar())
1774                 return true;
1775         }
1776     }
1777
1778     return false;
1779 }
1780
1781 void FrameView::updateControlTints()
1782 {
1783     // This is called when control tints are changed from aqua/graphite to clear and vice versa.
1784     // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
1785     // This is only done if the theme supports control tinting. It's up to the theme and platform
1786     // to define when controls get the tint and to call this function when that changes.
1787     
1788     // Optimize the common case where we bring a window to the front while it's still empty.
1789     if (!m_frame || m_frame->loader()->url().isEmpty())
1790         return;
1791
1792     if ((m_frame->contentRenderer() && m_frame->contentRenderer()->theme()->supportsControlTints()) || hasCustomScrollbars())  {
1793         if (needsLayout())
1794             layout();
1795         PlatformGraphicsContext* const noContext = 0;
1796         GraphicsContext context(noContext);
1797         context.setUpdatingControlTints(true);
1798         if (platformWidget())
1799             paintContents(&context, visibleContentRect());
1800         else
1801             paint(&context, frameRect());
1802     }
1803 }
1804
1805 bool FrameView::wasScrolledByUser() const
1806 {
1807     return m_wasScrolledByUser;
1808 }
1809
1810 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
1811 {
1812     if (m_inProgrammaticScroll)
1813         return;
1814     m_maintainScrollPositionAnchor = 0;
1815     m_wasScrolledByUser = wasScrolledByUser;
1816 }
1817
1818 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
1819 {
1820     if (!frame())
1821         return;
1822
1823 #if ENABLE(INSPECTOR)
1824     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
1825         timelineAgent->willPaint(rect);
1826 #endif
1827
1828     Document* document = frame()->document();
1829
1830 #ifndef NDEBUG
1831     bool fillWithRed;
1832     if (document->printing())
1833         fillWithRed = false; // Printing, don't fill with red (can't remember why).
1834     else if (document->ownerElement())
1835         fillWithRed = false; // Subframe, don't fill with red.
1836     else if (isTransparent())
1837         fillWithRed = false; // Transparent, don't fill with red.
1838     else if (m_paintBehavior & PaintBehaviorSelectionOnly)
1839         fillWithRed = false; // Selections are transparent, don't fill with red.
1840     else if (m_nodeToDraw)
1841         fillWithRed = false; // Element images are transparent, don't fill with red.
1842     else
1843         fillWithRed = true;
1844     
1845     if (fillWithRed)
1846         p->fillRect(rect, Color(0xFF, 0, 0), DeviceColorSpace);
1847 #endif
1848
1849     bool isTopLevelPainter = !sCurrentPaintTimeStamp;
1850     if (isTopLevelPainter)
1851         sCurrentPaintTimeStamp = currentTime();
1852     
1853     RenderView* contentRenderer = frame()->contentRenderer();
1854     if (!contentRenderer) {
1855         LOG_ERROR("called Frame::paint with nil renderer");
1856         return;
1857     }
1858
1859     ASSERT(!needsLayout());
1860     if (needsLayout())
1861         return;
1862
1863 #if USE(ACCELERATED_COMPOSITING)
1864     if (!p->paintingDisabled()) {
1865         if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
1866             rootLayer->syncCompositingState();
1867     }
1868 #endif
1869
1870     ASSERT(!m_isPainting);
1871         
1872     m_isPainting = true;
1873         
1874     // m_nodeToDraw is used to draw only one element (and its descendants)
1875     RenderObject* eltRenderer = m_nodeToDraw ? m_nodeToDraw->renderer() : 0;
1876
1877     PaintBehavior paintBehavior = m_paintBehavior;
1878     if (paintBehavior == PaintBehaviorNormal)
1879         document->invalidateRenderedRectsForMarkersInRect(rect);
1880
1881     if (document->printing())
1882         paintBehavior |= PaintBehaviorFlattenCompositingLayers;
1883
1884     contentRenderer->layer()->paint(p, rect, paintBehavior, eltRenderer);
1885     
1886     m_isPainting = false;
1887     m_lastPaintTime = currentTime();
1888
1889 #if ENABLE(DASHBOARD_SUPPORT)
1890     // Regions may have changed as a result of the visibility/z-index of element changing.
1891     if (document->dashboardRegionsDirty())
1892         updateDashboardRegions();
1893 #endif
1894
1895     if (isTopLevelPainter)
1896         sCurrentPaintTimeStamp = 0;
1897
1898 #if ENABLE(INSPECTOR)
1899     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
1900         timelineAgent->didPaint();
1901 #endif
1902 }
1903
1904 void FrameView::setPaintBehavior(PaintBehavior behavior)
1905 {
1906     m_paintBehavior = behavior;
1907 }
1908
1909 PaintBehavior FrameView::paintBehavior() const
1910 {
1911     return m_paintBehavior;
1912 }
1913
1914 bool FrameView::isPainting() const
1915 {
1916     return m_isPainting;
1917 }
1918
1919 void FrameView::setNodeToDraw(Node* node)
1920 {
1921     m_nodeToDraw = node;
1922 }
1923
1924 void FrameView::layoutIfNeededRecursive()
1925 {
1926     // We have to crawl our entire tree looking for any FrameViews that need
1927     // layout and make sure they are up to date.
1928     // Mac actually tests for intersection with the dirty region and tries not to
1929     // update layout for frames that are outside the dirty region.  Not only does this seem
1930     // pointless (since those frames will have set a zero timer to layout anyway), but
1931     // it is also incorrect, since if two frames overlap, the first could be excluded from the dirty
1932     // region but then become included later by the second frame adding rects to the dirty region
1933     // when it lays out.
1934
1935     if (needsLayout())
1936         layout();
1937
1938     const HashSet<RefPtr<Widget> >* viewChildren = children();
1939     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1940     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1941         Widget* widget = (*current).get();
1942         if (widget->isFrameView())
1943             static_cast<FrameView*>(widget)->layoutIfNeededRecursive();
1944     }
1945
1946     // layoutIfNeededRecursive is called when we need to make sure layout is up-to-date before
1947     // painting, so we need to flush out any deferred repaints too.
1948     flushDeferredRepaints();
1949 }
1950     
1951 void FrameView::flushDeferredRepaints()
1952 {
1953     if (!m_deferredRepaintTimer.isActive())
1954         return;
1955     m_deferredRepaintTimer.stop();
1956     doDeferredRepaints();
1957 }
1958
1959 void FrameView::forceLayout(bool allowSubtree)
1960 {
1961     layout(allowSubtree);
1962     // We cannot unschedule a pending relayout, since the force can be called with
1963     // a tiny rectangle from a drawRect update.  By unscheduling we in effect
1964     // "validate" and stop the necessary full repaint from occurring.  Basically any basic
1965     // append/remove DHTML is broken by this call.  For now, I have removed the optimization
1966     // until we have a better invalidation stategy. -dwh
1967     //unscheduleRelayout();
1968 }
1969
1970 void FrameView::forceLayoutWithPageWidthRange(float minPageWidth, float maxPageWidth, bool _adjustViewSize)
1971 {
1972     // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
1973     // the state of things before and after the layout
1974     RenderView *root = toRenderView(m_frame->document()->renderer());
1975     if (root) {
1976         // This magic is basically copied from khtmlview::print
1977         int pageW = (int)ceilf(minPageWidth);
1978         root->setWidth(pageW);
1979         root->setNeedsLayoutAndPrefWidthsRecalc();
1980         forceLayout();
1981
1982         // If we don't fit in the minimum page width, we'll lay out again. If we don't fit in the
1983         // maximum page width, we will lay out to the maximum page width and clip extra content.
1984         // FIXME: We are assuming a shrink-to-fit printing implementation.  A cropping
1985         // implementation should not do this!
1986         int rightmostPos = root->rightmostPosition();
1987         if (rightmostPos > minPageWidth) {
1988             pageW = std::min(rightmostPos, (int)ceilf(maxPageWidth));
1989             root->setWidth(pageW);
1990             root->setNeedsLayoutAndPrefWidthsRecalc();
1991             forceLayout();
1992         }
1993     }
1994
1995     if (_adjustViewSize)
1996         adjustViewSize();
1997 }
1998
1999 void FrameView::adjustPageHeight(float *newBottom, float oldTop, float oldBottom, float /*bottomLimit*/)
2000 {
2001     RenderView* root = m_frame->contentRenderer();
2002     if (root) {
2003         // Use a context with painting disabled.
2004         GraphicsContext context((PlatformGraphicsContext*)0);
2005         root->setTruncatedAt((int)floorf(oldBottom));
2006         IntRect dirtyRect(0, (int)floorf(oldTop), root->rightLayoutOverflow(), (int)ceilf(oldBottom - oldTop));
2007         root->layer()->paint(&context, dirtyRect);
2008         *newBottom = root->bestTruncatedAt();
2009         if (*newBottom == 0)
2010             *newBottom = oldBottom;
2011     } else
2012         *newBottom = oldBottom;
2013 }
2014
2015 IntRect FrameView::convertFromRenderer(const RenderObject* renderer, const IntRect& rendererRect) const
2016 {
2017     IntRect rect = renderer->localToAbsoluteQuad(FloatRect(rendererRect)).enclosingBoundingBox();
2018
2019     // Convert from page ("absolute") to FrameView coordinates.
2020     rect.move(-scrollX(), -scrollY());
2021
2022     return rect;
2023 }
2024
2025 IntRect FrameView::convertToRenderer(const RenderObject* renderer, const IntRect& viewRect) const
2026 {
2027     IntRect rect = viewRect;
2028     
2029     // Convert from FrameView coords into page ("absolute") coordinates.
2030     rect.move(scrollX(), scrollY());
2031
2032     // FIXME: we don't have a way to map an absolute rect down to a local quad, so just
2033     // move the rect for now.
2034     rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), false, true /* use transforms */)));
2035     return rect;
2036 }
2037
2038 IntPoint FrameView::convertFromRenderer(const RenderObject* renderer, const IntPoint& rendererPoint) const
2039 {
2040     IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, false, true /* use transforms */));
2041
2042     // Convert from page ("absolute") to FrameView coordinates.
2043     point.move(-scrollX(), -scrollY());
2044     return point;
2045 }
2046
2047 IntPoint FrameView::convertToRenderer(const RenderObject* renderer, const IntPoint& viewPoint) const
2048 {
2049     IntPoint point = viewPoint;
2050     
2051     // Convert from FrameView coords into page ("absolute") coordinates.
2052     point += IntSize(scrollX(), scrollY());
2053
2054     return roundedIntPoint(renderer->absoluteToLocal(point, false, true /* use transforms */));
2055 }
2056
2057 IntRect FrameView::convertToContainingView(const IntRect& localRect) const
2058 {
2059     if (const ScrollView* parentScrollView = parent()) {
2060         if (parentScrollView->isFrameView()) {
2061             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2062             // Get our renderer in the parent view
2063             RenderPart* renderer = m_frame->ownerRenderer();
2064             if (!renderer)
2065                 return localRect;
2066                 
2067             IntRect rect(localRect);
2068             // Add borders and padding??
2069             rect.move(renderer->borderLeft() + renderer->paddingLeft(),
2070                       renderer->borderTop() + renderer->paddingTop());
2071             return parentView->convertFromRenderer(renderer, rect);
2072         }
2073         
2074         return Widget::convertToContainingView(localRect);
2075     }
2076     
2077     return localRect;
2078 }
2079
2080 IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
2081 {
2082     if (const ScrollView* parentScrollView = parent()) {
2083         if (parentScrollView->isFrameView()) {
2084             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2085
2086             // Get our renderer in the parent view
2087             RenderPart* renderer = m_frame->ownerRenderer();
2088             if (!renderer)
2089                 return parentRect;
2090
2091             IntRect rect = parentView->convertToRenderer(renderer, parentRect);
2092             // Subtract borders and padding
2093             rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
2094                       -renderer->borderTop() - renderer->paddingTop());
2095             return rect;
2096         }
2097         
2098         return Widget::convertFromContainingView(parentRect);
2099     }
2100     
2101     return parentRect;
2102 }
2103
2104 IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
2105 {
2106     if (const ScrollView* parentScrollView = parent()) {
2107         if (parentScrollView->isFrameView()) {
2108             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2109
2110             // Get our renderer in the parent view
2111             RenderPart* renderer = m_frame->ownerRenderer();
2112             if (!renderer)
2113                 return localPoint;
2114                 
2115             IntPoint point(localPoint);
2116
2117             // Add borders and padding
2118             point.move(renderer->borderLeft() + renderer->paddingLeft(),
2119                        renderer->borderTop() + renderer->paddingTop());
2120             return parentView->convertFromRenderer(renderer, point);
2121         }
2122         
2123         return Widget::convertToContainingView(localPoint);
2124     }
2125     
2126     return localPoint;
2127 }
2128
2129 IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
2130 {
2131     if (const ScrollView* parentScrollView = parent()) {
2132         if (parentScrollView->isFrameView()) {
2133             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2134
2135             // Get our renderer in the parent view
2136             RenderPart* renderer = m_frame->ownerRenderer();
2137             if (!renderer)
2138                 return parentPoint;
2139
2140             IntPoint point = parentView->convertToRenderer(renderer, parentPoint);
2141             // Subtract borders and padding
2142             point.move(-renderer->borderLeft() - renderer->paddingLeft(),
2143                        -renderer->borderTop() - renderer->paddingTop());
2144             return point;
2145         }
2146         
2147         return Widget::convertFromContainingView(parentPoint);
2148     }
2149     
2150     return parentPoint;
2151 }
2152
2153 } // namespace WebCore