b1c5865df01cfb698e6b8ec64005b6b625324e9c
[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
414     setContentsSize(IntSize(root->rightLayoutOverflow(), root->bottomLayoutOverflow()));
415 }
416
417 void FrameView::applyOverflowToViewport(RenderObject* o, ScrollbarMode& hMode, ScrollbarMode& vMode)
418 {
419     // Handle the overflow:hidden/scroll case for the body/html elements.  WinIE treats
420     // overflow:hidden and overflow:scroll on <body> as applying to the document's
421     // scrollbars.  The CSS2.1 draft states that HTML UAs should use the <html> or <body> element and XML/XHTML UAs should
422     // use the root element.
423     switch (o->style()->overflowX()) {
424         case OHIDDEN:
425             hMode = ScrollbarAlwaysOff;
426             break;
427         case OSCROLL:
428             hMode = ScrollbarAlwaysOn;
429             break;
430         case OAUTO:
431             hMode = ScrollbarAuto;
432             break;
433         default:
434             // Don't set it at all.
435             ;
436     }
437     
438      switch (o->style()->overflowY()) {
439         case OHIDDEN:
440             vMode = ScrollbarAlwaysOff;
441             break;
442         case OSCROLL:
443             vMode = ScrollbarAlwaysOn;
444             break;
445         case OAUTO:
446             vMode = ScrollbarAuto;
447             break;
448         default:
449             // Don't set it at all.
450             ;
451     }
452
453     m_viewportRenderer = o;
454 }
455
456 #if USE(ACCELERATED_COMPOSITING)
457 void FrameView::updateCompositingLayers()
458 {
459     RenderView* view = m_frame->contentRenderer();
460     if (!view)
461         return;
462
463     // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
464     view->compositor()->cacheAcceleratedCompositingFlags();
465     
466     if (!view->usesCompositing())
467         return;
468
469     view->compositor()->updateCompositingLayers(CompositingUpdateAfterLayoutOrStyleChange);
470 }
471
472 void FrameView::setNeedsOneShotDrawingSynchronization()
473 {
474     Page* page = frame() ? frame()->page() : 0;
475     if (page)
476         page->chrome()->client()->setNeedsOneShotDrawingSynchronization();
477 }
478
479 #endif // USE(ACCELERATED_COMPOSITING)
480
481 bool FrameView::hasCompositedContent() const
482 {
483 #if USE(ACCELERATED_COMPOSITING)
484     if (RenderView* view = m_frame->contentRenderer())
485         return view->compositor()->inCompositingMode();
486 #endif
487     return false;
488 }
489
490 // Sometimes (for plug-ins) we need to eagerly go into compositing mode.
491 void FrameView::enterCompositingMode()
492 {
493 #if USE(ACCELERATED_COMPOSITING)
494     if (RenderView* view = m_frame->contentRenderer())
495         return view->compositor()->enableCompositingMode();
496 #endif
497 }
498
499 bool FrameView::isEnclosedInCompositingLayer() const
500 {
501 #if USE(ACCELERATED_COMPOSITING)
502     RenderObject* frameOwnerRenderer = m_frame->ownerRenderer();
503     return frameOwnerRenderer && frameOwnerRenderer->containerForRepaint();
504 #else
505     return false;
506 #endif
507 }
508
509 bool FrameView::syncCompositingStateRecursive()
510 {
511 #if USE(ACCELERATED_COMPOSITING)
512     ASSERT(m_frame->view() == this);
513     RenderView* contentRenderer = m_frame->contentRenderer();
514     if (!contentRenderer)
515         return true;    // We don't want to keep trying to update layers if we have no renderer.
516
517     if (m_layoutTimer.isActive()) {
518         // Don't sync layers if there's a layout pending.
519         return false;
520     }
521     
522     if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
523         rootLayer->syncCompositingState();
524
525     bool allSubframesSynced = true;
526     const HashSet<RefPtr<Widget> >* viewChildren = children();
527     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
528     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
529         Widget* widget = (*current).get();
530         if (widget->isFrameView()) {
531             bool synced = static_cast<FrameView*>(widget)->syncCompositingStateRecursive();
532             allSubframesSynced &= synced;
533         }
534     }
535     return allSubframesSynced;
536 #else // USE(ACCELERATED_COMPOSITING)
537     return true;
538 #endif
539 }
540
541 bool FrameView::isSoftwareRenderable() const
542 {
543 #if USE(ACCELERATED_COMPOSITING)
544     RenderView* view = m_frame->contentRenderer();
545     if (!view)
546         return true;
547
548     return !view->compositor()->has3DContent();
549 #else
550     return true;
551 #endif
552 }
553
554 void FrameView::didMoveOnscreen()
555 {
556     RenderView* view = m_frame->contentRenderer();
557     if (view)
558         view->didMoveOnscreen();
559 }
560
561 void FrameView::willMoveOffscreen()
562 {
563     RenderView* view = m_frame->contentRenderer();
564     if (view)
565         view->willMoveOffscreen();
566 }
567
568 RenderObject* FrameView::layoutRoot(bool onlyDuringLayout) const
569 {
570     return onlyDuringLayout && layoutPending() ? 0 : m_layoutRoot;
571 }
572
573 void FrameView::layout(bool allowSubtree)
574 {
575     if (m_midLayout)
576         return;
577
578     m_layoutTimer.stop();
579     m_delayedLayout = false;
580     m_setNeedsLayoutWasDeferred = false;
581
582     // Protect the view from being deleted during layout (in recalcStyle)
583     RefPtr<FrameView> protector(this);
584
585     if (!m_frame) {
586         // FIXME: Do we need to set m_size.width here?
587         // FIXME: Should we set m_size.height here too?
588         m_size.setWidth(layoutWidth());
589         return;
590     }
591     
592     // we shouldn't enter layout() while painting
593     ASSERT(!isPainting());
594     if (isPainting())
595         return;
596
597 #if ENABLE(INSPECTOR)    
598     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
599         timelineAgent->willLayout();
600 #endif
601
602     if (!allowSubtree && m_layoutRoot) {
603         m_layoutRoot->markContainingBlocksForLayout(false);
604         m_layoutRoot = 0;
605     }
606
607     ASSERT(m_frame->view() == this);
608     // This early return should be removed when rdar://5598072 is resolved. In the meantime, there is a
609     // gigantic CrashTracer because of this issue, and the early return will hopefully cause graceful 
610     // failure instead.  
611     if (m_frame->view() != this)
612         return;
613
614     Document* document = m_frame->document();
615
616     m_layoutSchedulingEnabled = false;
617
618     if (!m_nestedLayoutCount && m_postLayoutTasksTimer.isActive()) {
619         // This is a new top-level layout. If there are any remaining tasks from the previous
620         // layout, finish them now.
621         m_postLayoutTasksTimer.stop();
622         performPostLayoutTasks();
623     }
624
625     // Viewport-dependent media queries may cause us to need completely different style information.
626     // Check that here.
627     if (document->styleSelector()->affectedByViewportChange())
628         document->updateStyleSelector();
629
630     // Always ensure our style info is up-to-date.  This can happen in situations where
631     // the layout beats any sort of style recalc update that needs to occur.
632     if (m_frame->needsReapplyStyles())
633         m_frame->reapplyStyles();
634     else if (document->childNeedsStyleRecalc())
635         document->recalcStyle();
636     
637     bool subtree = m_layoutRoot;
638
639     // If there is only one ref to this view left, then its going to be destroyed as soon as we exit, 
640     // so there's no point to continuing to layout
641     if (protector->hasOneRef())
642         return;
643
644     RenderObject* root = subtree ? m_layoutRoot : document->renderer();
645     if (!root) {
646         // FIXME: Do we need to set m_size here?
647         m_layoutSchedulingEnabled = true;
648         return;
649     }
650
651     m_nestedLayoutCount++;
652
653     ScrollbarMode hMode;
654     ScrollbarMode vMode;
655     if (m_canHaveScrollbars) {
656         hMode = ScrollbarAuto;
657         vMode = ScrollbarAuto;
658     } else {
659         hMode = ScrollbarAlwaysOff;
660         vMode = ScrollbarAlwaysOff;
661     }
662
663     if (!subtree) {
664         Node* documentElement = document->documentElement();
665         RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0;
666         Node* body = document->body();
667         if (body && body->renderer()) {
668             if (body->hasTagName(framesetTag) && !m_frame->settings()->frameFlatteningEnabled()) {
669                 body->renderer()->setChildNeedsLayout(true);
670                 vMode = ScrollbarAlwaysOff;
671                 hMode = ScrollbarAlwaysOff;
672             } else if (body->hasTagName(bodyTag)) {
673                 if (!m_firstLayout && m_size.height() != layoutHeight() && body->renderer()->enclosingBox()->stretchesToViewHeight())
674                     body->renderer()->setChildNeedsLayout(true);
675                 // It's sufficient to just check the X overflow,
676                 // since it's illegal to have visible in only one direction.
677                 RenderObject* o = rootRenderer->style()->overflowX() == OVISIBLE && document->documentElement()->hasTagName(htmlTag) ? body->renderer() : rootRenderer;
678                 applyOverflowToViewport(o, hMode, vMode);
679             }
680         } else if (rootRenderer) {
681 #if ENABLE(SVG)
682             if (documentElement->isSVGElement()) {
683                 if (!m_firstLayout && (m_size.width() != layoutWidth() || m_size.height() != layoutHeight()))
684                     rootRenderer->setChildNeedsLayout(true);
685             } else
686                 applyOverflowToViewport(rootRenderer, hMode, vMode);
687 #else
688             applyOverflowToViewport(rootRenderer, hMode, vMode);
689 #endif
690         }
691 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
692         if (m_firstLayout && !document->ownerElement())
693             printf("Elapsed time before first layout: %d\n", document->elapsedTime());
694 #endif
695     }
696
697     m_doFullRepaint = !subtree && (m_firstLayout || toRenderView(root)->printing());
698
699     if (!subtree) {
700         // Now set our scrollbar state for the layout.
701         ScrollbarMode currentHMode = horizontalScrollbarMode();
702         ScrollbarMode currentVMode = verticalScrollbarMode();
703
704         if (m_firstLayout || (hMode != currentHMode || vMode != currentVMode)) {
705             if (m_firstLayout) {
706                 setScrollbarsSuppressed(true);
707
708                 m_firstLayout = false;
709                 m_firstLayoutCallbackPending = true;
710                 m_lastLayoutSize = IntSize(width(), height());
711                 m_lastZoomFactor = root->style()->zoom();
712
713                 // Set the initial vMode to AlwaysOn if we're auto.
714                 if (vMode == ScrollbarAuto)
715                     setVerticalScrollbarMode(ScrollbarAlwaysOn); // This causes a vertical scrollbar to appear.
716                 // Set the initial hMode to AlwaysOff if we're auto.
717                 if (hMode == ScrollbarAuto)
718                     setHorizontalScrollbarMode(ScrollbarAlwaysOff); // This causes a horizontal scrollbar to disappear.
719
720                 setScrollbarModes(hMode, vMode);
721                 setScrollbarsSuppressed(false, true);
722             } else
723                 setScrollbarModes(hMode, vMode);
724         }
725
726         IntSize oldSize = m_size;
727
728         m_size = IntSize(layoutWidth(), layoutHeight());
729
730         if (oldSize != m_size)
731             m_doFullRepaint = true;
732     }
733
734     RenderLayer* layer = root->enclosingLayer();
735
736     pauseScheduledEvents();
737
738     bool disableLayoutState = false;
739     if (subtree) {
740         RenderView* view = root->view();
741         disableLayoutState = view->shouldDisableLayoutStateForSubtree(root);
742         view->pushLayoutState(root);
743         if (disableLayoutState)
744             view->disableLayoutState();
745     }
746         
747     m_midLayout = true;
748     beginDeferredRepaints();
749     root->layout();
750     endDeferredRepaints();
751     m_midLayout = false;
752
753     if (subtree) {
754         RenderView* view = root->view();
755         view->popLayoutState();
756         if (disableLayoutState)
757             view->enableLayoutState();
758     }
759     m_layoutRoot = 0;
760
761     m_frame->selection()->setNeedsLayout();
762     m_frame->selection()->updateAppearance();
763    
764     m_layoutSchedulingEnabled = true;
765
766     if (!subtree && !toRenderView(root)->printing())
767         adjustViewSize();
768
769     // Now update the positions of all layers.
770     beginDeferredRepaints();
771     IntPoint cachedOffset;
772     layer->updateLayerPositions((m_doFullRepaint ? RenderLayer::DoFullRepaint : 0)
773                                 | RenderLayer::CheckForRepaint
774                                 | RenderLayer::IsCompositingUpdateRoot
775                                 | RenderLayer::UpdateCompositingLayers,
776                                 subtree ? 0 : &cachedOffset);
777     endDeferredRepaints();
778
779 #if USE(ACCELERATED_COMPOSITING)
780     updateCompositingLayers();
781 #endif
782     
783     m_layoutCount++;
784
785 #if PLATFORM(MAC)
786     if (AXObjectCache::accessibilityEnabled())
787         root->document()->axObjectCache()->postNotification(root, AXObjectCache::AXLayoutComplete, true);
788 #endif
789 #if ENABLE(DASHBOARD_SUPPORT)
790     updateDashboardRegions();
791 #endif
792
793     ASSERT(!root->needsLayout());
794
795     setCanBlitOnScroll(!useSlowRepaints());
796
797     if (document->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
798         updateOverflowStatus(layoutWidth() < contentsWidth(),
799                              layoutHeight() < contentsHeight());
800
801     if (!m_postLayoutTasksTimer.isActive()) {
802         // Calls resumeScheduledEvents()
803         performPostLayoutTasks();
804
805         if (!m_postLayoutTasksTimer.isActive() && needsLayout()) {
806             // Post-layout widget updates or an event handler made us need layout again.
807             // Lay out again, but this time defer widget updates and event dispatch until after
808             // we return.
809             m_postLayoutTasksTimer.startOneShot(0);
810             pauseScheduledEvents();
811             layout();
812         }
813     } else {
814         resumeScheduledEvents();
815         ASSERT(m_enqueueEvents);
816     }
817
818 #if ENABLE(INSPECTOR)
819     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
820         timelineAgent->didLayout();
821 #endif
822
823     m_nestedLayoutCount--;
824 }
825
826 void FrameView::addWidgetToUpdate(RenderEmbeddedObject* object)
827 {
828     if (!m_widgetUpdateSet)
829         m_widgetUpdateSet.set(new RenderEmbeddedObjectSet);
830
831     m_widgetUpdateSet->add(object);
832 }
833
834 void FrameView::removeWidgetToUpdate(RenderEmbeddedObject* object)
835 {
836     if (!m_widgetUpdateSet)
837         return;
838
839     m_widgetUpdateSet->remove(object);
840 }
841
842 void FrameView::setMediaType(const String& mediaType)
843 {
844     m_mediaType = mediaType;
845 }
846
847 String FrameView::mediaType() const
848 {
849     // See if we have an override type.
850     String overrideType = m_frame->loader()->client()->overrideMediaType();
851     if (!overrideType.isNull())
852         return overrideType;
853     return m_mediaType;
854 }
855
856 void FrameView::adjustMediaTypeForPrinting(bool printing)
857 {
858     if (printing) {
859         if (m_mediaTypeWhenNotPrinting.isNull())
860             m_mediaTypeWhenNotPrinting = mediaType();
861             setMediaType("print");
862     } else {
863         if (!m_mediaTypeWhenNotPrinting.isNull())
864             setMediaType(m_mediaTypeWhenNotPrinting);
865         m_mediaTypeWhenNotPrinting = String();
866     }
867 }
868
869 bool FrameView::useSlowRepaints() const
870 {
871     return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || m_isOverlapped || !m_contentIsOpaque;
872 }
873
874 bool FrameView::useSlowRepaintsIfNotOverlapped() const
875 {
876     return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || !m_contentIsOpaque;
877 }
878
879 void FrameView::setUseSlowRepaints()
880 {
881     m_useSlowRepaints = true;
882     setCanBlitOnScroll(false);
883 }
884
885 void FrameView::addSlowRepaintObject()
886 {
887     if (!m_slowRepaintObjectCount)
888         setCanBlitOnScroll(false);
889     m_slowRepaintObjectCount++;
890 }
891
892 void FrameView::removeSlowRepaintObject()
893 {
894     ASSERT(m_slowRepaintObjectCount > 0);
895     m_slowRepaintObjectCount--;
896     if (!m_slowRepaintObjectCount)
897         setCanBlitOnScroll(!useSlowRepaints());
898 }
899
900 void FrameView::addFixedObject()
901 {
902     ++m_fixedObjectCount;
903 }
904
905 void FrameView::removeFixedObject()
906 {
907     ASSERT(m_fixedObjectCount > 0);
908     --m_fixedObjectCount;
909 }
910
911 void FrameView::setIsOverlapped(bool isOverlapped)
912 {
913     if (isOverlapped == m_isOverlapped)
914         return;
915
916     m_isOverlapped = isOverlapped;
917     setCanBlitOnScroll(!useSlowRepaints());
918     
919 #if USE(ACCELERATED_COMPOSITING)
920     // Overlap can affect compositing tests, so if it changes, we need to trigger
921     // a recalcStyle in the parent document.
922     if (hasCompositedContent()) {
923         if (Element* ownerElement = m_frame->document()->ownerElement())
924             ownerElement->setNeedsStyleRecalc(SyntheticStyleChange);
925     }
926 #endif    
927 }
928
929 void FrameView::setContentIsOpaque(bool contentIsOpaque)
930 {
931     if (contentIsOpaque == m_contentIsOpaque)
932         return;
933
934     m_contentIsOpaque = contentIsOpaque;
935     setCanBlitOnScroll(!useSlowRepaints());
936 }
937
938 void FrameView::restoreScrollbar()
939 {
940     setScrollbarsSuppressed(false);
941 }
942
943 bool FrameView::scrollToFragment(const KURL& url)
944 {
945     // If our URL has no ref, then we have no place we need to jump to.
946     // OTOH If CSS target was set previously, we want to set it to 0, recalc
947     // and possibly repaint because :target pseudo class may have been
948     // set (see bug 11321).
949     if (!url.hasFragmentIdentifier() && !m_frame->document()->cssTarget())
950         return false;
951
952     String fragmentIdentifier = url.fragmentIdentifier();
953     if (scrollToAnchor(fragmentIdentifier))
954         return true;
955
956     // Try again after decoding the ref, based on the document's encoding.
957     if (TextResourceDecoder* decoder = m_frame->document()->decoder())
958         return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
959
960     return false;
961 }
962
963 bool FrameView::scrollToAnchor(const String& name)
964 {
965     ASSERT(m_frame->document());
966
967     if (!m_frame->document()->haveStylesheetsLoaded()) {
968         m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(true);
969         return false;
970     }
971
972     m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(false);
973
974     Element* anchorNode = m_frame->document()->findAnchor(name);
975
976 #if ENABLE(SVG)
977     if (m_frame->document()->isSVGDocument()) {
978         if (name.startsWith("xpointer(")) {
979             // We need to parse the xpointer reference here
980         } else if (name.startsWith("svgView(")) {
981             RefPtr<SVGSVGElement> svg = static_cast<SVGDocument*>(m_frame->document())->rootElement();
982             if (!svg->currentView()->parseViewSpec(name))
983                 return false;
984             svg->setUseCurrentView(true);
985         } else {
986             if (anchorNode && anchorNode->hasTagName(SVGNames::viewTag)) {
987                 RefPtr<SVGViewElement> viewElement = anchorNode->hasTagName(SVGNames::viewTag) ? static_cast<SVGViewElement*>(anchorNode) : 0;
988                 if (viewElement.get()) {
989                     RefPtr<SVGSVGElement> svg = static_cast<SVGSVGElement*>(SVGLocatable::nearestViewportElement(viewElement.get()));
990                     svg->inheritViewAttributes(viewElement.get());
991                 }
992             }
993         }
994         // FIXME: need to decide which <svg> to focus on, and zoom to that one
995         // FIXME: need to actually "highlight" the viewTarget(s)
996     }
997 #endif
998
999     m_frame->document()->setCSSTarget(anchorNode); // Setting to null will clear the current target.
1000   
1001     // Implement the rule that "" and "top" both mean top of page as in other browsers.
1002     if (!anchorNode && !(name.isEmpty() || equalIgnoringCase(name, "top")))
1003         return false;
1004
1005     maintainScrollPositionAtAnchor(anchorNode ? static_cast<Node*>(anchorNode) : m_frame->document());
1006     return true;
1007 }
1008
1009 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
1010 {
1011     m_maintainScrollPositionAnchor = anchorNode;
1012     if (!m_maintainScrollPositionAnchor)
1013         return;
1014
1015     // We need to update the layout before scrolling, otherwise we could
1016     // really mess things up if an anchor scroll comes at a bad moment.
1017     m_frame->document()->updateStyleIfNeeded();
1018     // Only do a layout if changes have occurred that make it necessary.
1019     if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout())
1020         layout();
1021     else
1022         scrollToAnchor();
1023 }
1024
1025 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
1026 {
1027     bool wasInProgrammaticScroll = m_inProgrammaticScroll;
1028     m_inProgrammaticScroll = true;
1029     m_maintainScrollPositionAnchor = 0;
1030     ScrollView::setScrollPosition(scrollPoint);
1031     m_inProgrammaticScroll = wasInProgrammaticScroll;
1032 }
1033
1034 void FrameView::scrollPositionChanged()
1035 {
1036     frame()->eventHandler()->sendScrollEvent();
1037
1038     // For fixed position elements, update widget positions and compositing layers after scrolling,
1039     // but only if we're not inside of layout.
1040     if (!m_nestedLayoutCount && hasFixedObjects()) {
1041         if (RenderView* root = m_frame->contentRenderer()) {
1042             root->updateWidgetPositions();
1043             root->layer()->updateRepaintRectsAfterScroll();
1044 #if USE(ACCELERATED_COMPOSITING)
1045             if (root->usesCompositing())
1046                 root->compositor()->updateCompositingLayers(CompositingUpdateOnScroll);
1047 #endif
1048         }
1049     }
1050 }
1051
1052 HostWindow* FrameView::hostWindow() const
1053 {
1054     Page* page = frame() ? frame()->page() : 0;
1055     if (!page)
1056         return 0;
1057     return page->chrome();
1058 }
1059
1060 const unsigned cRepaintRectUnionThreshold = 25;
1061
1062 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
1063 {
1064     ASSERT(!m_frame->document()->ownerElement());
1065
1066     double delay = adjustedDeferredRepaintDelay();
1067     if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
1068         IntRect paintRect = r;
1069         if (!paintsEntireContents())
1070             paintRect.intersect(visibleContentRect());
1071         if (paintRect.isEmpty())
1072             return;
1073         if (m_repaintCount == cRepaintRectUnionThreshold) {
1074             IntRect unionedRect;
1075             for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
1076                 unionedRect.unite(m_repaintRects[i]);
1077             m_repaintRects.clear();
1078             m_repaintRects.append(unionedRect);
1079         }
1080         if (m_repaintCount < cRepaintRectUnionThreshold)
1081             m_repaintRects.append(paintRect);
1082         else
1083             m_repaintRects[0].unite(paintRect);
1084         m_repaintCount++;
1085     
1086         if (!m_deferringRepaints && !m_deferredRepaintTimer.isActive())
1087              m_deferredRepaintTimer.startOneShot(delay);
1088         return;
1089     }
1090     
1091     if (!immediate && isOffscreen() && !shouldUpdateWhileOffscreen())
1092         return;
1093
1094 #if ENABLE(TILED_BACKING_STORE)
1095     if (frame()->tiledBackingStore()) {
1096         frame()->tiledBackingStore()->invalidate(r);
1097         return;
1098     }
1099 #endif
1100     ScrollView::repaintContentRectangle(r, immediate);
1101 }
1102
1103 void FrameView::visibleContentsResized()
1104 {
1105     // We check to make sure the view is attached to a frame() as this method can
1106     // be triggered before the view is attached by Frame::createView(...) setting
1107     // various values such as setScrollBarModes(...) for example.  An ASSERT is
1108     // triggered when a view is layout before being attached to a frame().
1109     if (!frame()->view())
1110         return;
1111
1112     if (needsLayout())
1113         layout();
1114 }
1115
1116 void FrameView::beginDeferredRepaints()
1117 {
1118     Page* page = m_frame->page();
1119     if (page->mainFrame() != m_frame)
1120         return page->mainFrame()->view()->beginDeferredRepaints();
1121
1122     m_deferringRepaints++;
1123 }
1124
1125
1126 void FrameView::endDeferredRepaints()
1127 {
1128     Page* page = m_frame->page();
1129     if (page->mainFrame() != m_frame)
1130         return page->mainFrame()->view()->endDeferredRepaints();
1131
1132     ASSERT(m_deferringRepaints > 0);
1133
1134     if (--m_deferringRepaints)
1135         return;
1136     
1137     if (m_deferredRepaintTimer.isActive())
1138         return;
1139
1140     if (double delay = adjustedDeferredRepaintDelay()) {
1141         m_deferredRepaintTimer.startOneShot(delay);
1142         return;
1143     }
1144     
1145     doDeferredRepaints();
1146 }
1147
1148 void FrameView::checkStopDelayingDeferredRepaints()
1149 {
1150     if (!m_deferredRepaintTimer.isActive())
1151         return;
1152
1153     Document* document = m_frame->document();
1154     if (document && (document->parsing() || document->docLoader()->requestCount()))
1155         return;
1156     
1157     m_deferredRepaintTimer.stop();
1158
1159     doDeferredRepaints();
1160 }
1161     
1162 void FrameView::doDeferredRepaints()
1163 {
1164     ASSERT(!m_deferringRepaints);
1165     if (isOffscreen() && !shouldUpdateWhileOffscreen()) {
1166         m_repaintRects.clear();
1167         m_repaintCount = 0;
1168         return;
1169     }
1170     unsigned size = m_repaintRects.size();
1171     for (unsigned i = 0; i < size; i++) {
1172 #if ENABLE(TILED_BACKING_STORE)
1173         if (frame()->tiledBackingStore()) {
1174             frame()->tiledBackingStore()->invalidate(m_repaintRects[i]);
1175             continue;
1176         }
1177 #endif
1178         ScrollView::repaintContentRectangle(m_repaintRects[i], false);
1179     }
1180     m_repaintRects.clear();
1181     m_repaintCount = 0;
1182     
1183     updateDeferredRepaintDelay();
1184 }
1185
1186 void FrameView::updateDeferredRepaintDelay()
1187 {
1188     Document* document = m_frame->document();
1189     if (!document || (!document->parsing() && !document->docLoader()->requestCount())) {
1190         m_deferredRepaintDelay = deferredRepaintDelay;
1191         return;
1192     }
1193     if (m_deferredRepaintDelay < maxDeferredRepaintDelayDuringLoading) {
1194         m_deferredRepaintDelay += deferredRepaintDelayIncrementDuringLoading;
1195         if (m_deferredRepaintDelay > maxDeferredRepaintDelayDuringLoading)
1196             m_deferredRepaintDelay = maxDeferredRepaintDelayDuringLoading;
1197     }
1198 }
1199
1200 void FrameView::resetDeferredRepaintDelay()
1201 {
1202     m_deferredRepaintDelay = 0;
1203     if (m_deferredRepaintTimer.isActive()) {
1204         m_deferredRepaintTimer.stop();
1205         if (!m_deferringRepaints)
1206             doDeferredRepaints();
1207     }
1208 }
1209
1210 double FrameView::adjustedDeferredRepaintDelay() const
1211 {
1212     if (!m_deferredRepaintDelay)
1213         return 0;
1214     double timeSinceLastPaint = currentTime() - m_lastPaintTime;
1215     return max(0., m_deferredRepaintDelay - timeSinceLastPaint);
1216 }
1217     
1218 void FrameView::deferredRepaintTimerFired(Timer<FrameView>*)
1219 {
1220     doDeferredRepaints();
1221 }    
1222
1223 void FrameView::layoutTimerFired(Timer<FrameView>*)
1224 {
1225 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1226     if (!m_frame->document()->ownerElement())
1227         printf("Layout timer fired at %d\n", m_frame->document()->elapsedTime());
1228 #endif
1229     layout();
1230 }
1231
1232 void FrameView::scheduleRelayout()
1233 {
1234     // FIXME: We should assert the page is not in the page cache, but that is causing
1235     // too many false assertions.  See <rdar://problem/7218118>.
1236     ASSERT(m_frame->view() == this);
1237
1238     if (m_layoutRoot) {
1239         m_layoutRoot->markContainingBlocksForLayout(false);
1240         m_layoutRoot = 0;
1241     }
1242     if (!m_layoutSchedulingEnabled)
1243         return;
1244     if (!needsLayout())
1245         return;
1246     if (!m_frame->document()->shouldScheduleLayout())
1247         return;
1248
1249     // When frame flattening is enabled, the contents of the frame affects layout of the parent frames.
1250     // Also invalidate parent frame starting from the owner element of this frame.
1251     if (m_frame->settings()->frameFlatteningEnabled() && m_frame->ownerRenderer()) {
1252         if (m_frame->ownerElement()->hasTagName(iframeTag) || m_frame->ownerElement()->hasTagName(frameTag))
1253             m_frame->ownerRenderer()->setNeedsLayout(true, true);
1254     }
1255
1256     int delay = m_frame->document()->minimumLayoutDelay();
1257     if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
1258         unscheduleRelayout();
1259     if (m_layoutTimer.isActive())
1260         return;
1261
1262     m_delayedLayout = delay != 0;
1263
1264 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1265     if (!m_frame->document()->ownerElement())
1266         printf("Scheduling layout for %d\n", delay);
1267 #endif
1268
1269     m_layoutTimer.startOneShot(delay * 0.001);
1270 }
1271
1272 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
1273 {
1274     for (RenderObject* r = descendant; r; r = r->container()) {
1275         if (r == ancestor)
1276             return true;
1277     }
1278     return false;
1279 }
1280
1281 void FrameView::scheduleRelayoutOfSubtree(RenderObject* relayoutRoot)
1282 {
1283     ASSERT(m_frame->view() == this);
1284
1285     if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout()) {
1286         if (relayoutRoot)
1287             relayoutRoot->markContainingBlocksForLayout(false);
1288         return;
1289     }
1290
1291     if (layoutPending() || !m_layoutSchedulingEnabled) {
1292         if (m_layoutRoot != relayoutRoot) {
1293             if (isObjectAncestorContainerOf(m_layoutRoot, relayoutRoot)) {
1294                 // Keep the current root
1295                 relayoutRoot->markContainingBlocksForLayout(false, m_layoutRoot);
1296             } else if (m_layoutRoot && isObjectAncestorContainerOf(relayoutRoot, m_layoutRoot)) {
1297                 // Re-root at relayoutRoot
1298                 m_layoutRoot->markContainingBlocksForLayout(false, relayoutRoot);
1299                 m_layoutRoot = relayoutRoot;
1300             } else {
1301                 // Just do a full relayout
1302                 if (m_layoutRoot)
1303                     m_layoutRoot->markContainingBlocksForLayout(false);
1304                 m_layoutRoot = 0;
1305                 relayoutRoot->markContainingBlocksForLayout(false);
1306             }
1307         }
1308     } else if (m_layoutSchedulingEnabled) {
1309         int delay = m_frame->document()->minimumLayoutDelay();
1310         m_layoutRoot = relayoutRoot;
1311         m_delayedLayout = delay != 0;
1312         m_layoutTimer.startOneShot(delay * 0.001);
1313     }
1314 }
1315
1316 bool FrameView::layoutPending() const
1317 {
1318     return m_layoutTimer.isActive();
1319 }
1320
1321 bool FrameView::needsLayout() const
1322 {
1323     // This can return true in cases where the document does not have a body yet.
1324     // Document::shouldScheduleLayout takes care of preventing us from scheduling
1325     // layout in that case.
1326     if (!m_frame)
1327         return false;
1328     RenderView* root = m_frame->contentRenderer();
1329     Document* document = m_frame->document();
1330     return layoutPending()
1331         || (root && root->needsLayout())
1332         || m_layoutRoot
1333         || (document && document->childNeedsStyleRecalc()) // can occur when using WebKit ObjC interface
1334         || m_frame->needsReapplyStyles()
1335         || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
1336 }
1337
1338 void FrameView::setNeedsLayout()
1339 {
1340     if (m_deferSetNeedsLayouts) {
1341         m_setNeedsLayoutWasDeferred = true;
1342         return;
1343     }
1344     RenderView* root = m_frame->contentRenderer();
1345     if (root)
1346         root->setNeedsLayout(true);
1347 }
1348
1349 void FrameView::unscheduleRelayout()
1350 {
1351     if (!m_layoutTimer.isActive())
1352         return;
1353
1354 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1355     if (!m_frame->document()->ownerElement())
1356         printf("Layout timer unscheduled at %d\n", m_frame->document()->elapsedTime());
1357 #endif
1358     
1359     m_layoutTimer.stop();
1360     m_delayedLayout = false;
1361 }
1362
1363 bool FrameView::isTransparent() const
1364 {
1365     return m_isTransparent;
1366 }
1367
1368 void FrameView::setTransparent(bool isTransparent)
1369 {
1370     m_isTransparent = isTransparent;
1371 }
1372
1373 Color FrameView::baseBackgroundColor() const
1374 {
1375     return m_baseBackgroundColor;
1376 }
1377
1378 void FrameView::setBaseBackgroundColor(Color bc)
1379 {
1380     if (!bc.isValid())
1381         bc = Color::white;
1382     m_baseBackgroundColor = bc;
1383 }
1384
1385 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
1386 {
1387     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1388         FrameView* view = frame->view();
1389         if (!view)
1390             continue;
1391
1392         view->setTransparent(transparent);
1393         view->setBaseBackgroundColor(backgroundColor);
1394     }
1395 }
1396
1397 bool FrameView::shouldUpdateWhileOffscreen() const
1398 {
1399     return m_shouldUpdateWhileOffscreen;
1400 }
1401
1402 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
1403 {
1404     m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
1405 }
1406
1407 void FrameView::scheduleEvent(PassRefPtr<Event> event, PassRefPtr<Node> eventTarget)
1408 {
1409     if (!m_enqueueEvents) {
1410         ExceptionCode ec = 0;
1411         eventTarget->dispatchEvent(event, ec);
1412         return;
1413     }
1414
1415     ScheduledEvent* scheduledEvent = new ScheduledEvent;
1416     scheduledEvent->m_event = event;
1417     scheduledEvent->m_eventTarget = eventTarget;
1418     m_scheduledEvents.append(scheduledEvent);
1419 }
1420
1421 void FrameView::pauseScheduledEvents()
1422 {
1423     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1424     m_enqueueEvents++;
1425 }
1426
1427 void FrameView::resumeScheduledEvents()
1428 {
1429     m_enqueueEvents--;
1430     if (!m_enqueueEvents)
1431         dispatchScheduledEvents();
1432     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1433 }
1434
1435 void FrameView::scrollToAnchor()
1436 {
1437     RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
1438     if (!anchorNode)
1439         return;
1440
1441     if (!anchorNode->renderer())
1442         return;
1443
1444     IntRect rect;
1445     if (anchorNode != m_frame->document())
1446         rect = anchorNode->getRect();
1447
1448     // Scroll nested layers and frames to reveal the anchor.
1449     // Align to the top and to the closest side (this matches other browsers).
1450     anchorNode->renderer()->enclosingLayer()->scrollRectToVisible(rect, true, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
1451
1452     if (AXObjectCache::accessibilityEnabled())
1453         m_frame->document()->axObjectCache()->handleScrolledToAnchor(anchorNode.get());
1454
1455     // scrollRectToVisible can call into setScrollPosition(), which resets m_maintainScrollPositionAnchor.
1456     m_maintainScrollPositionAnchor = anchorNode;
1457 }
1458
1459 bool FrameView::updateWidgets()
1460 {
1461     if (m_nestedLayoutCount > 1 || !m_widgetUpdateSet || m_widgetUpdateSet->isEmpty())
1462         return true;
1463     
1464     size_t size = m_widgetUpdateSet->size();
1465
1466     Vector<RenderEmbeddedObject*> objects;
1467     objects.reserveCapacity(size);
1468
1469     RenderEmbeddedObjectSet::const_iterator end = m_widgetUpdateSet->end();
1470     for (RenderEmbeddedObjectSet::const_iterator it = m_widgetUpdateSet->begin(); it != end; ++it) {
1471         objects.uncheckedAppend(*it);
1472         (*it)->ref();
1473     }
1474
1475     for (size_t i = 0; i < size; ++i) {
1476         RenderEmbeddedObject* object = objects[i];
1477
1478         // The object may have been destroyed, but our manual ref() keeps the object from being deleted.
1479         object->updateWidget(false);
1480         object->updateWidgetPosition();
1481
1482         m_widgetUpdateSet->remove(object);
1483     }
1484
1485     RenderArena* arena = m_frame->document()->renderArena();
1486     for (size_t i = 0; i < size; ++i)
1487         objects[i]->deref(arena);
1488     
1489     return m_widgetUpdateSet->isEmpty();
1490 }
1491     
1492 void FrameView::performPostLayoutTasks()
1493 {
1494     if (m_firstLayoutCallbackPending) {
1495         m_firstLayoutCallbackPending = false;
1496         m_frame->loader()->didFirstLayout();
1497     }
1498
1499     if (m_isVisuallyNonEmpty && m_firstVisuallyNonEmptyLayoutCallbackPending) {
1500         m_firstVisuallyNonEmptyLayoutCallbackPending = false;
1501         m_frame->loader()->didFirstVisuallyNonEmptyLayout();
1502     }
1503
1504     RenderView* root = m_frame->contentRenderer();
1505
1506     root->updateWidgetPositions();
1507     
1508     for (unsigned i = 0; i < maxUpdateWidgetsIterations; i++) {
1509         if (updateWidgets())
1510             break;
1511     }
1512
1513     scrollToAnchor();
1514
1515     resumeScheduledEvents();
1516
1517     if (!root->printing()) {
1518         IntSize currentSize = IntSize(width(), height());
1519         float currentZoomFactor = root->style()->zoom();
1520         bool resized = !m_firstLayout && (currentSize != m_lastLayoutSize || currentZoomFactor != m_lastZoomFactor);
1521         m_lastLayoutSize = currentSize;
1522         m_lastZoomFactor = currentZoomFactor;
1523         if (resized)
1524             m_frame->eventHandler()->sendResizeEvent();
1525     }
1526 }
1527
1528 void FrameView::postLayoutTimerFired(Timer<FrameView>*)
1529 {
1530     performPostLayoutTasks();
1531 }
1532
1533 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
1534 {
1535     if (!m_viewportRenderer)
1536         return;
1537     
1538     if (m_overflowStatusDirty) {
1539         m_horizontalOverflow = horizontalOverflow;
1540         m_verticalOverflow = verticalOverflow;
1541         m_overflowStatusDirty = false;
1542         return;
1543     }
1544     
1545     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
1546     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
1547     
1548     if (horizontalOverflowChanged || verticalOverflowChanged) {
1549         m_horizontalOverflow = horizontalOverflow;
1550         m_verticalOverflow = verticalOverflow;
1551         
1552         scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
1553             verticalOverflowChanged, verticalOverflow),
1554             m_viewportRenderer->node());
1555     }
1556     
1557 }
1558
1559 void FrameView::dispatchScheduledEvents()
1560 {
1561     if (m_scheduledEvents.isEmpty())
1562         return;
1563
1564     Vector<ScheduledEvent*> scheduledEventsCopy = m_scheduledEvents;
1565     m_scheduledEvents.clear();
1566     
1567     Vector<ScheduledEvent*>::iterator end = scheduledEventsCopy.end();
1568     for (Vector<ScheduledEvent*>::iterator it = scheduledEventsCopy.begin(); it != end; ++it) {
1569         ScheduledEvent* scheduledEvent = *it;
1570         
1571         ExceptionCode ec = 0;
1572         
1573         // Only dispatch events to nodes that are in the document
1574         if (scheduledEvent->m_eventTarget->inDocument())
1575             scheduledEvent->m_eventTarget->dispatchEvent(scheduledEvent->m_event, ec);
1576         
1577         delete scheduledEvent;
1578     }
1579 }
1580
1581 IntRect FrameView::windowClipRect(bool clipToContents) const
1582 {
1583     ASSERT(m_frame->view() == this);
1584
1585     // Set our clip rect to be our contents.
1586     IntRect clipRect = contentsToWindow(visibleContentRect(!clipToContents));
1587     if (!m_frame || !m_frame->document()->ownerElement())
1588         return clipRect;
1589
1590     // Take our owner element and get the clip rect from the enclosing layer.
1591     Element* elt = m_frame->document()->ownerElement();
1592     RenderLayer* layer = elt->renderer()->enclosingLayer();
1593     // FIXME: layer should never be null, but sometimes seems to be anyway.
1594     if (!layer)
1595         return clipRect;
1596     FrameView* parentView = elt->document()->view();
1597     clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
1598     return clipRect;
1599 }
1600
1601 IntRect FrameView::windowClipRectForLayer(const RenderLayer* layer, bool clipToLayerContents) const
1602 {
1603     // If we have no layer, just return our window clip rect.
1604     if (!layer)
1605         return windowClipRect();
1606
1607     // Apply the clip from the layer.
1608     IntRect clipRect;
1609     if (clipToLayerContents)
1610         clipRect = layer->childrenClipRect();
1611     else
1612         clipRect = layer->selfClipRect();
1613     clipRect = contentsToWindow(clipRect); 
1614     return intersection(clipRect, windowClipRect());
1615 }
1616
1617 bool FrameView::isActive() const
1618 {
1619     Page* page = frame()->page();
1620     return page && page->focusController()->isActive();
1621 }
1622
1623 void FrameView::valueChanged(Scrollbar* bar)
1624 {
1625     // Figure out if we really moved.
1626     IntSize offset = scrollOffset();
1627     ScrollView::valueChanged(bar);
1628     if (offset != scrollOffset())
1629         frame()->eventHandler()->sendScrollEvent();
1630     frame()->loader()->client()->didChangeScrollOffset();
1631 }
1632
1633 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
1634 {
1635     // Add in our offset within the FrameView.
1636     IntRect dirtyRect = rect;
1637     dirtyRect.move(scrollbar->x(), scrollbar->y());
1638     invalidateRect(dirtyRect);
1639 }
1640
1641 void FrameView::getTickmarks(Vector<IntRect>& tickmarks) const
1642 {
1643     tickmarks = frame()->document()->renderedRectsForMarkers(DocumentMarker::TextMatch);
1644 }
1645
1646 IntRect FrameView::windowResizerRect() const
1647 {
1648     Page* page = frame() ? frame()->page() : 0;
1649     if (!page)
1650         return IntRect();
1651     return page->chrome()->windowResizerRect();
1652 }
1653
1654 #if ENABLE(DASHBOARD_SUPPORT)
1655 void FrameView::updateDashboardRegions()
1656 {
1657     Document* document = m_frame->document();
1658     if (!document->hasDashboardRegions())
1659         return;
1660     Vector<DashboardRegionValue> newRegions;
1661     document->renderBox()->collectDashboardRegions(newRegions);
1662     if (newRegions == document->dashboardRegions())
1663         return;
1664     document->setDashboardRegions(newRegions);
1665     Page* page = m_frame->page();
1666     if (!page)
1667         return;
1668     page->chrome()->client()->dashboardRegionsChanged();
1669 }
1670 #endif
1671
1672 void FrameView::invalidateScrollCorner()
1673 {
1674     invalidateRect(scrollCornerRect());
1675 }
1676
1677 void FrameView::updateScrollCorner()
1678 {
1679     RenderObject* renderer = 0;
1680     RefPtr<RenderStyle> cornerStyle;
1681     
1682     if (!scrollCornerRect().isEmpty()) {
1683         // Try the <body> element first as a scroll corner source.
1684         Document* doc = m_frame->document();
1685         Element* body = doc ? doc->body() : 0;
1686         if (body && body->renderer()) {
1687             renderer = body->renderer();
1688             cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1689         }
1690         
1691         if (!cornerStyle) {
1692             // If the <body> didn't have a custom style, then the root element might.
1693             Element* docElement = doc ? doc->documentElement() : 0;
1694             if (docElement && docElement->renderer()) {
1695                 renderer = docElement->renderer();
1696                 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1697             }
1698         }
1699         
1700         if (!cornerStyle) {
1701             // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
1702             if (RenderPart* renderer = m_frame->ownerRenderer())
1703                 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1704         }
1705     }
1706
1707     if (cornerStyle) {
1708         if (!m_scrollCorner)
1709             m_scrollCorner = new (renderer->renderArena()) RenderScrollbarPart(renderer->document());
1710         m_scrollCorner->setStyle(cornerStyle.release());
1711         invalidateRect(scrollCornerRect());
1712     } else if (m_scrollCorner) {
1713         m_scrollCorner->destroy();
1714         m_scrollCorner = 0;
1715     }
1716 }
1717
1718 void FrameView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
1719 {
1720     if (context->updatingControlTints()) {
1721         updateScrollCorner();
1722         return;
1723     }
1724
1725     if (m_scrollCorner) {
1726         m_scrollCorner->paintIntoRect(context, cornerRect.x(), cornerRect.y(), cornerRect);
1727         return;
1728     }
1729
1730     ScrollView::paintScrollCorner(context, cornerRect);
1731 }
1732
1733 bool FrameView::hasCustomScrollbars() const
1734 {
1735     const HashSet<RefPtr<Widget> >* viewChildren = children();
1736     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1737     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1738         Widget* widget = current->get();
1739         if (widget->isFrameView()) {
1740             if (static_cast<FrameView*>(widget)->hasCustomScrollbars())
1741                 return true;
1742         } else if (widget->isScrollbar()) {
1743             Scrollbar* scrollbar = static_cast<Scrollbar*>(widget);
1744             if (scrollbar->isCustomScrollbar())
1745                 return true;
1746         }
1747     }
1748
1749     return false;
1750 }
1751
1752 void FrameView::updateControlTints()
1753 {
1754     // This is called when control tints are changed from aqua/graphite to clear and vice versa.
1755     // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
1756     // This is only done if the theme supports control tinting. It's up to the theme and platform
1757     // to define when controls get the tint and to call this function when that changes.
1758     
1759     // Optimize the common case where we bring a window to the front while it's still empty.
1760     if (!m_frame || m_frame->loader()->url().isEmpty())
1761         return;
1762
1763     if ((m_frame->contentRenderer() && m_frame->contentRenderer()->theme()->supportsControlTints()) || hasCustomScrollbars())  {
1764         if (needsLayout())
1765             layout();
1766         PlatformGraphicsContext* const noContext = 0;
1767         GraphicsContext context(noContext);
1768         context.setUpdatingControlTints(true);
1769         if (platformWidget())
1770             paintContents(&context, visibleContentRect());
1771         else
1772             paint(&context, frameRect());
1773     }
1774 }
1775
1776 bool FrameView::wasScrolledByUser() const
1777 {
1778     return m_wasScrolledByUser;
1779 }
1780
1781 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
1782 {
1783     if (m_inProgrammaticScroll)
1784         return;
1785     m_maintainScrollPositionAnchor = 0;
1786     m_wasScrolledByUser = wasScrolledByUser;
1787 }
1788
1789 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
1790 {
1791     if (!frame())
1792         return;
1793
1794 #if ENABLE(INSPECTOR)
1795     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
1796         timelineAgent->willPaint(rect);
1797 #endif
1798
1799     Document* document = frame()->document();
1800
1801 #ifndef NDEBUG
1802     bool fillWithRed;
1803     if (document->printing())
1804         fillWithRed = false; // Printing, don't fill with red (can't remember why).
1805     else if (document->ownerElement())
1806         fillWithRed = false; // Subframe, don't fill with red.
1807     else if (isTransparent())
1808         fillWithRed = false; // Transparent, don't fill with red.
1809     else if (m_paintBehavior & PaintBehaviorSelectionOnly)
1810         fillWithRed = false; // Selections are transparent, don't fill with red.
1811     else if (m_nodeToDraw)
1812         fillWithRed = false; // Element images are transparent, don't fill with red.
1813     else
1814         fillWithRed = true;
1815     
1816     if (fillWithRed)
1817         p->fillRect(rect, Color(0xFF, 0, 0), DeviceColorSpace);
1818 #endif
1819
1820     bool isTopLevelPainter = !sCurrentPaintTimeStamp;
1821     if (isTopLevelPainter)
1822         sCurrentPaintTimeStamp = currentTime();
1823     
1824     RenderView* contentRenderer = frame()->contentRenderer();
1825     if (!contentRenderer) {
1826         LOG_ERROR("called Frame::paint with nil renderer");
1827         return;
1828     }
1829
1830     ASSERT(!needsLayout());
1831     if (needsLayout())
1832         return;
1833
1834 #if USE(ACCELERATED_COMPOSITING)
1835     if (!p->paintingDisabled()) {
1836         if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
1837             rootLayer->syncCompositingState();
1838     }
1839 #endif
1840
1841     ASSERT(!m_isPainting);
1842         
1843     m_isPainting = true;
1844         
1845     // m_nodeToDraw is used to draw only one element (and its descendants)
1846     RenderObject* eltRenderer = m_nodeToDraw ? m_nodeToDraw->renderer() : 0;
1847
1848     PaintBehavior oldPaintBehavior = m_paintBehavior;
1849     if (m_paintBehavior == PaintBehaviorNormal)
1850         document->invalidateRenderedRectsForMarkersInRect(rect);
1851
1852     if (document->printing())
1853         m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
1854
1855     contentRenderer->layer()->paint(p, rect, m_paintBehavior, eltRenderer);
1856     
1857     m_paintBehavior = oldPaintBehavior;
1858     
1859     m_isPainting = false;
1860     m_lastPaintTime = currentTime();
1861
1862 #if ENABLE(DASHBOARD_SUPPORT)
1863     // Regions may have changed as a result of the visibility/z-index of element changing.
1864     if (document->dashboardRegionsDirty())
1865         updateDashboardRegions();
1866 #endif
1867
1868     if (isTopLevelPainter)
1869         sCurrentPaintTimeStamp = 0;
1870
1871 #if ENABLE(INSPECTOR)
1872     if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
1873         timelineAgent->didPaint();
1874 #endif
1875 }
1876
1877 void FrameView::setPaintBehavior(PaintBehavior behavior)
1878 {
1879     m_paintBehavior = behavior;
1880 }
1881
1882 PaintBehavior FrameView::paintBehavior() const
1883 {
1884     return m_paintBehavior;
1885 }
1886
1887 bool FrameView::isPainting() const
1888 {
1889     return m_isPainting;
1890 }
1891
1892 void FrameView::setNodeToDraw(Node* node)
1893 {
1894     m_nodeToDraw = node;
1895 }
1896
1897 void FrameView::layoutIfNeededRecursive()
1898 {
1899     // We have to crawl our entire tree looking for any FrameViews that need
1900     // layout and make sure they are up to date.
1901     // Mac actually tests for intersection with the dirty region and tries not to
1902     // update layout for frames that are outside the dirty region.  Not only does this seem
1903     // pointless (since those frames will have set a zero timer to layout anyway), but
1904     // it is also incorrect, since if two frames overlap, the first could be excluded from the dirty
1905     // region but then become included later by the second frame adding rects to the dirty region
1906     // when it lays out.
1907
1908     if (needsLayout())
1909         layout();
1910
1911     const HashSet<RefPtr<Widget> >* viewChildren = children();
1912     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1913     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1914         Widget* widget = (*current).get();
1915         if (widget->isFrameView())
1916             static_cast<FrameView*>(widget)->layoutIfNeededRecursive();
1917     }
1918
1919     // layoutIfNeededRecursive is called when we need to make sure layout is up-to-date before
1920     // painting, so we need to flush out any deferred repaints too.
1921     flushDeferredRepaints();
1922 }
1923     
1924 void FrameView::flushDeferredRepaints()
1925 {
1926     if (!m_deferredRepaintTimer.isActive())
1927         return;
1928     m_deferredRepaintTimer.stop();
1929     doDeferredRepaints();
1930 }
1931
1932 void FrameView::forceLayout(bool allowSubtree)
1933 {
1934     layout(allowSubtree);
1935     // We cannot unschedule a pending relayout, since the force can be called with
1936     // a tiny rectangle from a drawRect update.  By unscheduling we in effect
1937     // "validate" and stop the necessary full repaint from occurring.  Basically any basic
1938     // append/remove DHTML is broken by this call.  For now, I have removed the optimization
1939     // until we have a better invalidation stategy. -dwh
1940     //unscheduleRelayout();
1941 }
1942
1943 void FrameView::forceLayoutWithPageWidthRange(float minPageWidth, float maxPageWidth, bool _adjustViewSize)
1944 {
1945     // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
1946     // the state of things before and after the layout
1947     RenderView *root = toRenderView(m_frame->document()->renderer());
1948     if (root) {
1949         // This magic is basically copied from khtmlview::print
1950         int pageW = (int)ceilf(minPageWidth);
1951         root->setWidth(pageW);
1952         root->setNeedsLayoutAndPrefWidthsRecalc();
1953         forceLayout();
1954
1955         // If we don't fit in the minimum page width, we'll lay out again. If we don't fit in the
1956         // maximum page width, we will lay out to the maximum page width and clip extra content.
1957         // FIXME: We are assuming a shrink-to-fit printing implementation.  A cropping
1958         // implementation should not do this!
1959         int rightmostPos = root->rightmostPosition();
1960         if (rightmostPos > minPageWidth) {
1961             pageW = std::min(rightmostPos, (int)ceilf(maxPageWidth));
1962             root->setWidth(pageW);
1963             root->setNeedsLayoutAndPrefWidthsRecalc();
1964             forceLayout();
1965         }
1966     }
1967
1968     if (_adjustViewSize)
1969         adjustViewSize();
1970 }
1971
1972 void FrameView::adjustPageHeight(float *newBottom, float oldTop, float oldBottom, float /*bottomLimit*/)
1973 {
1974     RenderView* root = m_frame->contentRenderer();
1975     if (root) {
1976         // Use a context with painting disabled.
1977         GraphicsContext context((PlatformGraphicsContext*)0);
1978         root->setTruncatedAt((int)floorf(oldBottom));
1979         IntRect dirtyRect(0, (int)floorf(oldTop), root->rightLayoutOverflow(), (int)ceilf(oldBottom - oldTop));
1980         root->layer()->paint(&context, dirtyRect);
1981         *newBottom = root->bestTruncatedAt();
1982         if (*newBottom == 0)
1983             *newBottom = oldBottom;
1984     } else
1985         *newBottom = oldBottom;
1986 }
1987
1988 IntRect FrameView::convertFromRenderer(const RenderObject* renderer, const IntRect& rendererRect) const
1989 {
1990     IntRect rect = renderer->localToAbsoluteQuad(FloatRect(rendererRect)).enclosingBoundingBox();
1991
1992     // Convert from page ("absolute") to FrameView coordinates.
1993     rect.move(-scrollX(), -scrollY());
1994
1995     return rect;
1996 }
1997
1998 IntRect FrameView::convertToRenderer(const RenderObject* renderer, const IntRect& viewRect) const
1999 {
2000     IntRect rect = viewRect;
2001     
2002     // Convert from FrameView coords into page ("absolute") coordinates.
2003     rect.move(scrollX(), scrollY());
2004
2005     // FIXME: we don't have a way to map an absolute rect down to a local quad, so just
2006     // move the rect for now.
2007     rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), false, true /* use transforms */)));
2008     return rect;
2009 }
2010
2011 IntPoint FrameView::convertFromRenderer(const RenderObject* renderer, const IntPoint& rendererPoint) const
2012 {
2013     IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, false, true /* use transforms */));
2014
2015     // Convert from page ("absolute") to FrameView coordinates.
2016     point.move(-scrollX(), -scrollY());
2017     return point;
2018 }
2019
2020 IntPoint FrameView::convertToRenderer(const RenderObject* renderer, const IntPoint& viewPoint) const
2021 {
2022     IntPoint point = viewPoint;
2023     
2024     // Convert from FrameView coords into page ("absolute") coordinates.
2025     point += IntSize(scrollX(), scrollY());
2026
2027     return roundedIntPoint(renderer->absoluteToLocal(point, false, true /* use transforms */));
2028 }
2029
2030 IntRect FrameView::convertToContainingView(const IntRect& localRect) const
2031 {
2032     if (const ScrollView* parentScrollView = parent()) {
2033         if (parentScrollView->isFrameView()) {
2034             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2035             // Get our renderer in the parent view
2036             RenderPart* renderer = m_frame->ownerRenderer();
2037             if (!renderer)
2038                 return localRect;
2039                 
2040             IntRect rect(localRect);
2041             // Add borders and padding??
2042             rect.move(renderer->borderLeft() + renderer->paddingLeft(),
2043                       renderer->borderTop() + renderer->paddingTop());
2044             return parentView->convertFromRenderer(renderer, rect);
2045         }
2046         
2047         return Widget::convertToContainingView(localRect);
2048     }
2049     
2050     return localRect;
2051 }
2052
2053 IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
2054 {
2055     if (const ScrollView* parentScrollView = parent()) {
2056         if (parentScrollView->isFrameView()) {
2057             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2058
2059             // Get our renderer in the parent view
2060             RenderPart* renderer = m_frame->ownerRenderer();
2061             if (!renderer)
2062                 return parentRect;
2063
2064             IntRect rect = parentView->convertToRenderer(renderer, parentRect);
2065             // Subtract borders and padding
2066             rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
2067                       -renderer->borderTop() - renderer->paddingTop());
2068             return rect;
2069         }
2070         
2071         return Widget::convertFromContainingView(parentRect);
2072     }
2073     
2074     return parentRect;
2075 }
2076
2077 IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
2078 {
2079     if (const ScrollView* parentScrollView = parent()) {
2080         if (parentScrollView->isFrameView()) {
2081             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2082
2083             // Get our renderer in the parent view
2084             RenderPart* renderer = m_frame->ownerRenderer();
2085             if (!renderer)
2086                 return localPoint;
2087                 
2088             IntPoint point(localPoint);
2089
2090             // Add borders and padding
2091             point.move(renderer->borderLeft() + renderer->paddingLeft(),
2092                        renderer->borderTop() + renderer->paddingTop());
2093             return parentView->convertFromRenderer(renderer, point);
2094         }
2095         
2096         return Widget::convertToContainingView(localPoint);
2097     }
2098     
2099     return localPoint;
2100 }
2101
2102 IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
2103 {
2104     if (const ScrollView* parentScrollView = parent()) {
2105         if (parentScrollView->isFrameView()) {
2106             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2107
2108             // Get our renderer in the parent view
2109             RenderPart* renderer = m_frame->ownerRenderer();
2110             if (!renderer)
2111                 return parentPoint;
2112
2113             IntPoint point = parentView->convertToRenderer(renderer, parentPoint);
2114             // Subtract borders and padding
2115             point.move(-renderer->borderLeft() - renderer->paddingLeft(),
2116                        -renderer->borderTop() - renderer->paddingTop());
2117             return point;
2118         }
2119         
2120         return Widget::convertFromContainingView(parentPoint);
2121     }
2122     
2123     return parentPoint;
2124 }
2125
2126 } // namespace WebCore